Lines Matching +full:super +full:- +full:frames

1 // SPDX-License-Identifier: GPL-2.0+
3 * f_ncm.c -- USB CDC Network (NCM) link function driver
10 * Copyright (C) 2003-2005,2008 David Brownell
33 * NCM is intended to be used with high-speed network attachments.
40 /* to trigger crc/non-crc ndp signature */
74 /* For multi-frame NDP TX */
86 /*-------------------------------------------------------------------------*/
89 * We cannot group frames so use just the minimal size which ok to put
90 * one max-size ethernet frame.
91 * If the host can group frames, allow it to do that, 16K is selected,
102 * throughput and will be mstly sending smaller infrequent frames.
333 /* super speed support: */
418 .language = 0x0409, /* en-us */
431 * in NDP32 -- 2 16bit words wide. Also signatures are different.
518 /*-------------------------------------------------------------------------*/
522 ncm->parser_opts = &ndp16_opts; in ncm_reset_values()
523 ncm->is_crc = false; in ncm_reset_values()
524 ncm->ndp_sign = ncm->parser_opts->ndp_sign; in ncm_reset_values()
525 ncm->port.cdc_filter = DEFAULT_FILTER; in ncm_reset_values()
528 ncm->port.header_len = 0; in ncm_reset_values()
530 ncm->port.fixed_out_len = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize); in ncm_reset_values()
531 ncm->port.fixed_in_len = NTB_DEFAULT_IN_SIZE; in ncm_reset_values()
535 * Context: ncm->lock held
539 struct usb_request *req = ncm->notify_req; in ncm_do_notify()
541 struct usb_composite_dev *cdev = ncm->port.func.config->cdev; in ncm_do_notify()
546 if (atomic_read(&ncm->notify_count)) in ncm_do_notify()
549 event = req->buf; in ncm_do_notify()
550 switch (ncm->notify_state) { in ncm_do_notify()
555 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION; in ncm_do_notify()
556 if (ncm->is_open) in ncm_do_notify()
557 event->wValue = cpu_to_le16(1); in ncm_do_notify()
559 event->wValue = cpu_to_le16(0); in ncm_do_notify()
560 event->wLength = 0; in ncm_do_notify()
561 req->length = sizeof *event; in ncm_do_notify()
564 str_true_false(ncm->is_open)); in ncm_do_notify()
565 ncm->notify_state = NCM_NOTIFY_NONE; in ncm_do_notify()
569 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE; in ncm_do_notify()
570 event->wValue = cpu_to_le16(0); in ncm_do_notify()
571 event->wLength = cpu_to_le16(8); in ncm_do_notify()
572 req->length = NCM_STATUS_BYTECOUNT; in ncm_do_notify()
575 data = req->buf + sizeof *event; in ncm_do_notify()
576 data[0] = cpu_to_le32(gether_bitrate(cdev->gadget)); in ncm_do_notify()
579 DBG(cdev, "notify speed %u\n", gether_bitrate(cdev->gadget)); in ncm_do_notify()
580 ncm->notify_state = NCM_NOTIFY_CONNECT; in ncm_do_notify()
583 event->bmRequestType = 0xA1; in ncm_do_notify()
584 event->wIndex = cpu_to_le16(ncm->ctrl_id); in ncm_do_notify()
586 atomic_inc(&ncm->notify_count); in ncm_do_notify()
593 spin_unlock(&ncm->lock); in ncm_do_notify()
594 status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC); in ncm_do_notify()
595 spin_lock(&ncm->lock); in ncm_do_notify()
597 atomic_dec(&ncm->notify_count); in ncm_do_notify()
598 DBG(cdev, "notify --> %d\n", status); in ncm_do_notify()
603 * Context: ncm->lock held
608 * NOTE on most versions of Linux, host side cdc-ethernet in ncm_notify()
617 ncm->notify_state = NCM_NOTIFY_SPEED; in ncm_notify()
623 struct f_ncm *ncm = req->context; in ncm_notify_complete()
624 struct usb_composite_dev *cdev = ncm->port.func.config->cdev; in ncm_notify_complete()
625 struct usb_cdc_notification *event = req->buf; in ncm_notify_complete()
627 spin_lock(&ncm->lock); in ncm_notify_complete()
628 switch (req->status) { in ncm_notify_complete()
631 event->bNotificationType); in ncm_notify_complete()
632 atomic_dec(&ncm->notify_count); in ncm_notify_complete()
634 case -ECONNRESET: in ncm_notify_complete()
635 case -ESHUTDOWN: in ncm_notify_complete()
636 atomic_set(&ncm->notify_count, 0); in ncm_notify_complete()
637 ncm->notify_state = NCM_NOTIFY_NONE; in ncm_notify_complete()
640 DBG(cdev, "event %02x --> %d\n", in ncm_notify_complete()
641 event->bNotificationType, req->status); in ncm_notify_complete()
642 atomic_dec(&ncm->notify_count); in ncm_notify_complete()
646 spin_unlock(&ncm->lock); in ncm_notify_complete()
653 struct usb_function *f = req->context; in ncm_ep0out_complete()
655 struct usb_composite_dev *cdev = f->config->cdev; in ncm_ep0out_complete()
657 req->context = NULL; in ncm_ep0out_complete()
658 if (req->status || req->actual != req->length) { in ncm_ep0out_complete()
659 DBG(cdev, "Bad control-OUT transfer\n"); in ncm_ep0out_complete()
663 in_size = get_unaligned_le32(req->buf); in ncm_ep0out_complete()
670 ncm->port.fixed_in_len = in_size; in ncm_ep0out_complete()
682 struct usb_composite_dev *cdev = f->config->cdev; in ncm_setup()
683 struct usb_request *req = cdev->req; in ncm_setup()
684 int value = -EOPNOTSUPP; in ncm_setup()
685 u16 w_index = le16_to_cpu(ctrl->wIndex); in ncm_setup()
686 u16 w_value = le16_to_cpu(ctrl->wValue); in ncm_setup()
687 u16 w_length = le16_to_cpu(ctrl->wLength); in ncm_setup()
693 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { in ncm_setup()
700 if (w_length != 0 || w_index != ncm->ctrl_id) in ncm_setup()
708 ncm->port.cdc_filter = w_value; in ncm_setup()
724 if (w_length == 0 || w_value != 0 || w_index != ncm->ctrl_id) in ncm_setup()
728 memcpy(req->buf, &ntb_parameters, value); in ncm_setup()
735 if (w_length < 4 || w_value != 0 || w_index != ncm->ctrl_id) in ncm_setup()
737 put_unaligned_le32(ncm->port.fixed_in_len, req->buf); in ncm_setup()
740 ncm->port.fixed_in_len); in ncm_setup()
746 if (w_length != 4 || w_value != 0 || w_index != ncm->ctrl_id) in ncm_setup()
748 req->complete = ncm_ep0out_complete; in ncm_setup()
749 req->length = w_length; in ncm_setup()
750 req->context = f; in ncm_setup()
752 value = req->length; in ncm_setup()
761 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id) in ncm_setup()
763 format = (ncm->parser_opts == &ndp16_opts) ? 0x0000 : 0x0001; in ncm_setup()
764 put_unaligned_le16(format, req->buf); in ncm_setup()
773 if (w_length != 0 || w_index != ncm->ctrl_id) in ncm_setup()
777 ncm->parser_opts = &ndp16_opts; in ncm_setup()
781 ncm->parser_opts = &ndp32_opts; in ncm_setup()
795 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id) in ncm_setup()
797 is_crc = ncm->is_crc ? 0x0001 : 0x0000; in ncm_setup()
798 put_unaligned_le16(is_crc, req->buf); in ncm_setup()
807 if (w_length != 0 || w_index != ncm->ctrl_id) in ncm_setup()
811 ncm->is_crc = false; in ncm_setup()
812 DBG(cdev, "non-CRC mode selected\n"); in ncm_setup()
815 ncm->is_crc = true; in ncm_setup()
834 ctrl->bRequestType, ctrl->bRequest, in ncm_setup()
837 ncm->ndp_sign = ncm->parser_opts->ndp_sign | in ncm_setup()
838 (ncm->is_crc ? NCM_NDP_HDR_CRC : 0); in ncm_setup()
843 ctrl->bRequestType, ctrl->bRequest, in ncm_setup()
845 req->zero = 0; in ncm_setup()
846 req->length = value; in ncm_setup()
847 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); in ncm_setup()
850 ctrl->bRequestType, ctrl->bRequest, in ncm_setup()
862 struct usb_composite_dev *cdev = f->config->cdev; in ncm_set_alt()
865 if (intf == ncm->ctrl_id) { in ncm_set_alt()
870 usb_ep_disable(ncm->notify); in ncm_set_alt()
872 if (!(ncm->notify->desc)) { in ncm_set_alt()
874 if (config_ep_by_speed(cdev->gadget, f, ncm->notify)) in ncm_set_alt()
877 usb_ep_enable(ncm->notify); in ncm_set_alt()
880 } else if (intf == ncm->data_id) { in ncm_set_alt()
884 if (ncm->netdev) { in ncm_set_alt()
886 ncm->netdev = NULL; in ncm_set_alt()
887 gether_disconnect(&ncm->port); in ncm_set_alt()
892 * CDC Network only sends data in non-default altsettings. in ncm_set_alt()
898 if (!ncm->port.in_ep->desc || in ncm_set_alt()
899 !ncm->port.out_ep->desc) { in ncm_set_alt()
901 if (config_ep_by_speed(cdev->gadget, f, in ncm_set_alt()
902 ncm->port.in_ep) || in ncm_set_alt()
903 config_ep_by_speed(cdev->gadget, f, in ncm_set_alt()
904 ncm->port.out_ep)) { in ncm_set_alt()
905 ncm->port.in_ep->desc = NULL; in ncm_set_alt()
906 ncm->port.out_ep->desc = NULL; in ncm_set_alt()
915 ncm->port.is_zlp_ok = in ncm_set_alt()
916 gadget_is_zlp_supported(cdev->gadget); in ncm_set_alt()
917 ncm->port.cdc_filter = DEFAULT_FILTER; in ncm_set_alt()
919 net = gether_connect(&ncm->port); in ncm_set_alt()
922 ncm->netdev = net; in ncm_set_alt()
925 spin_lock(&ncm->lock); in ncm_set_alt()
927 spin_unlock(&ncm->lock); in ncm_set_alt()
933 return -EINVAL; in ncm_set_alt()
944 if (intf == ncm->ctrl_id) in ncm_get_alt()
946 return ncm->port.in_ep->enabled ? 1 : 0; in ncm_get_alt()
957 const struct ndp_parser_opts *opts = ncm->parser_opts; in package_for_tx()
959 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len; in package_for_tx()
962 hrtimer_try_to_cancel(&ncm->task_timer); in package_for_tx()
964 ndp_pad = ALIGN(ncm->skb_tx_data->len, ndp_align) - in package_for_tx()
965 ncm->skb_tx_data->len; in package_for_tx()
966 ndp_index = ncm->skb_tx_data->len + ndp_pad; in package_for_tx()
967 new_len = ndp_index + dgram_idx_len + ncm->skb_tx_ndp->len; in package_for_tx()
970 ntb_iter = (void *) ncm->skb_tx_data->data; in package_for_tx()
973 put_ncm(&ntb_iter, opts->block_length, new_len); in package_for_tx()
974 put_ncm(&ntb_iter, opts->ndp_index, ndp_index); in package_for_tx()
977 new_len = opts->ndp_size + in package_for_tx()
978 (ncm->ndp_dgram_count * dgram_idx_len); in package_for_tx()
979 ncm->ndp_dgram_count = 0; in package_for_tx()
981 ntb_iter = (void *) ncm->skb_tx_ndp->data; in package_for_tx()
986 swap(skb2, ncm->skb_tx_data); in package_for_tx()
987 if (ncm->skb_tx_data) { in package_for_tx()
988 dev_consume_skb_any(ncm->skb_tx_data); in package_for_tx()
989 ncm->skb_tx_data = NULL; in package_for_tx()
996 skb_put_data(skb2, ncm->skb_tx_ndp->data, ncm->skb_tx_ndp->len); in package_for_tx()
997 dev_consume_skb_any(ncm->skb_tx_ndp); in package_for_tx()
998 ncm->skb_tx_ndp = NULL; in package_for_tx()
1009 struct f_ncm *ncm = func_to_ncm(&port->func); in ncm_wrap_ntb()
1018 unsigned max_size = ncm->port.fixed_in_len; in ncm_wrap_ntb()
1019 const struct ndp_parser_opts *opts = ncm->parser_opts; in ncm_wrap_ntb()
1023 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len; in ncm_wrap_ntb()
1026 if (ncm->is_crc) { in ncm_wrap_ntb()
1031 skb->data, in ncm_wrap_ntb()
1032 skb->len); in ncm_wrap_ntb()
1042 if (ncm->skb_tx_data in ncm_wrap_ntb()
1043 && (ncm->ndp_dgram_count >= TX_MAX_NUM_DPE in ncm_wrap_ntb()
1044 || (ncm->skb_tx_data->len + in ncm_wrap_ntb()
1045 div + rem + skb->len + in ncm_wrap_ntb()
1046 ncm->skb_tx_ndp->len + ndp_align + (2 * dgram_idx_len)) in ncm_wrap_ntb()
1053 if (!ncm->skb_tx_data) { in ncm_wrap_ntb()
1054 ncb_len = opts->nth_size; in ncm_wrap_ntb()
1055 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len; in ncm_wrap_ntb()
1059 ncm->skb_tx_data = alloc_skb(max_size, GFP_ATOMIC); in ncm_wrap_ntb()
1060 if (!ncm->skb_tx_data) in ncm_wrap_ntb()
1063 ncm->skb_tx_data->dev = ncm->netdev; in ncm_wrap_ntb()
1064 ntb_data = skb_put_zero(ncm->skb_tx_data, ncb_len); in ncm_wrap_ntb()
1066 put_unaligned_le32(opts->nth_sign, ntb_data); in ncm_wrap_ntb()
1069 put_unaligned_le16(opts->nth_size, ntb_data++); in ncm_wrap_ntb()
1075 ncm->skb_tx_ndp = alloc_skb((int)(opts->ndp_size in ncm_wrap_ntb()
1076 + opts->dpe_size in ncm_wrap_ntb()
1079 if (!ncm->skb_tx_ndp) in ncm_wrap_ntb()
1082 ncm->skb_tx_ndp->dev = ncm->netdev; in ncm_wrap_ntb()
1083 ntb_ndp = skb_put(ncm->skb_tx_ndp, opts->ndp_size); in ncm_wrap_ntb()
1086 put_unaligned_le32(ncm->ndp_sign, ntb_ndp); in ncm_wrap_ntb()
1090 ncm->ndp_dgram_count = 1; in ncm_wrap_ntb()
1092 /* Note: we skip opts->next_ndp_index */ in ncm_wrap_ntb()
1095 hrtimer_start(&ncm->task_timer, TX_TIMEOUT_NSECS, in ncm_wrap_ntb()
1100 ntb_ndp = skb_put_zero(ncm->skb_tx_ndp, dgram_idx_len); in ncm_wrap_ntb()
1102 ncb_len = ncm->skb_tx_data->len; in ncm_wrap_ntb()
1103 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len; in ncm_wrap_ntb()
1107 put_ncm(&ntb_ndp, opts->dgram_item_len, ncb_len); in ncm_wrap_ntb()
1109 put_ncm(&ntb_ndp, opts->dgram_item_len, skb->len); in ncm_wrap_ntb()
1110 ncm->ndp_dgram_count++; in ncm_wrap_ntb()
1113 skb_put_zero(ncm->skb_tx_data, dgram_pad); in ncm_wrap_ntb()
1114 skb_put_data(ncm->skb_tx_data, skb->data, skb->len); in ncm_wrap_ntb()
1118 } else if (ncm->skb_tx_data) { in ncm_wrap_ntb()
1121 * ncm_tx_timeout() - hence, this is our signal to flush/send. in ncm_wrap_ntb()
1131 ncm->netdev->stats.tx_dropped++; in ncm_wrap_ntb()
1135 if (ncm->skb_tx_data) in ncm_wrap_ntb()
1136 dev_kfree_skb_any(ncm->skb_tx_data); in ncm_wrap_ntb()
1137 if (ncm->skb_tx_ndp) in ncm_wrap_ntb()
1138 dev_kfree_skb_any(ncm->skb_tx_ndp); in ncm_wrap_ntb()
1150 struct net_device *netdev = READ_ONCE(ncm->netdev); in ncm_tx_timeout()
1155 * XXX that the dev->wrap() invocations to build SKBs is transparent in ncm_tx_timeout()
1161 netdev->netdev_ops->ndo_start_xmit(NULL, netdev); in ncm_tx_timeout()
1170 struct f_ncm *ncm = func_to_ncm(&port->func); in ncm_unwrap_ntb()
1171 unsigned char *ntb_ptr = skb->data; in ncm_unwrap_ntb()
1179 int ret = -EINVAL; in ncm_unwrap_ntb()
1182 const struct ndp_parser_opts *opts = ncm->parser_opts; in ncm_unwrap_ntb()
1183 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0; in ncm_unwrap_ntb()
1185 int to_process = skb->len; in ncm_unwrap_ntb()
1188 ncm_opts = container_of(port->func.fi, struct f_ncm_opts, func_inst); in ncm_unwrap_ntb()
1189 frame_max = ncm_opts->max_segment_size; in ncm_unwrap_ntb()
1195 if (get_unaligned_le32(tmp) != opts->nth_sign) { in ncm_unwrap_ntb()
1196 INFO(port->func.config->cdev, "Wrong NTH SIGN, skblen %d\n", in ncm_unwrap_ntb()
1197 skb->len); in ncm_unwrap_ntb()
1199 skb->data, 32, false); in ncm_unwrap_ntb()
1205 if (get_unaligned_le16(tmp++) != opts->nth_size) { in ncm_unwrap_ntb()
1206 INFO(port->func.config->cdev, "Wrong NTB headersize\n"); in ncm_unwrap_ntb()
1211 block_len = get_ncm(&tmp, opts->block_length); in ncm_unwrap_ntb()
1214 INFO(port->func.config->cdev, "OUT size exceeded\n"); in ncm_unwrap_ntb()
1218 ndp_index = get_ncm(&tmp, opts->ndp_index); in ncm_unwrap_ntb()
1227 (ndp_index < opts->nth_size) || in ncm_unwrap_ntb()
1228 (ndp_index > (block_len - in ncm_unwrap_ntb()
1229 opts->ndp_size))) { in ncm_unwrap_ntb()
1230 INFO(port->func.config->cdev, "Bad index: %#X\n", in ncm_unwrap_ntb()
1240 if (get_unaligned_le32(tmp) != ncm->ndp_sign) { in ncm_unwrap_ntb()
1241 INFO(port->func.config->cdev, "Wrong NDP SIGN\n"); in ncm_unwrap_ntb()
1251 * item size is 16/32 bits, opts->dgram_item_len * 2 bytes in ncm_unwrap_ntb()
1255 if ((ndp_len < opts->ndp_size in ncm_unwrap_ntb()
1256 + 2 * 2 * (opts->dgram_item_len * 2)) || in ncm_unwrap_ntb()
1257 (ndp_len % opts->ndplen_align != 0)) { in ncm_unwrap_ntb()
1258 INFO(port->func.config->cdev, "Bad NDP length: %#X\n", in ncm_unwrap_ntb()
1262 tmp += opts->reserved1; in ncm_unwrap_ntb()
1264 ndp_index = get_ncm(&tmp, opts->next_ndp_index); in ncm_unwrap_ntb()
1265 tmp += opts->reserved2; in ncm_unwrap_ntb()
1267 ndp_len -= opts->ndp_size; in ncm_unwrap_ntb()
1268 index2 = get_ncm(&tmp, opts->dgram_item_len); in ncm_unwrap_ntb()
1269 dg_len2 = get_ncm(&tmp, opts->dgram_item_len); in ncm_unwrap_ntb()
1275 if ((index < opts->nth_size) || in ncm_unwrap_ntb()
1276 (index > block_len - opts->dpe_size)) { in ncm_unwrap_ntb()
1277 INFO(port->func.config->cdev, in ncm_unwrap_ntb()
1289 INFO(port->func.config->cdev, in ncm_unwrap_ntb()
1293 if (ncm->is_crc) { in ncm_unwrap_ntb()
1297 index + dg_len - in ncm_unwrap_ntb()
1301 dg_len - crc_len); in ncm_unwrap_ntb()
1303 INFO(port->func.config->cdev, in ncm_unwrap_ntb()
1309 index2 = get_ncm(&tmp, opts->dgram_item_len); in ncm_unwrap_ntb()
1310 dg_len2 = get_ncm(&tmp, opts->dgram_item_len); in ncm_unwrap_ntb()
1313 if (index2 > block_len - opts->dpe_size) { in ncm_unwrap_ntb()
1314 INFO(port->func.config->cdev, in ncm_unwrap_ntb()
1323 skb2 = netdev_alloc_skb_ip_align(ncm->netdev, in ncm_unwrap_ntb()
1324 dg_len - crc_len); in ncm_unwrap_ntb()
1328 dg_len - crc_len); in ncm_unwrap_ntb()
1332 ndp_len -= 2 * (opts->dgram_item_len * 2); in ncm_unwrap_ntb()
1337 } while (ndp_len > 2 * (opts->dgram_item_len * 2)); in ncm_unwrap_ntb()
1340 VDBG(port->func.config->cdev, in ncm_unwrap_ntb()
1341 "Parsed NTB with %d frames\n", dgram_counter); in ncm_unwrap_ntb()
1343 to_process -= block_len; in ncm_unwrap_ntb()
1346 * Windows NCM driver avoids USB ZLPs by adding a 1-byte in ncm_unwrap_ntb()
1351 to_process--; in ncm_unwrap_ntb()
1369 struct usb_composite_dev *cdev = f->config->cdev; in ncm_disable()
1373 if (ncm->netdev) { in ncm_disable()
1374 ncm->netdev = NULL; in ncm_disable()
1375 gether_disconnect(&ncm->port); in ncm_disable()
1378 if (ncm->notify->enabled) { in ncm_disable()
1379 usb_ep_disable(ncm->notify); in ncm_disable()
1380 ncm->notify->desc = NULL; in ncm_disable()
1384 /*-------------------------------------------------------------------------*/
1393 * - disconnected/unconfigured
1394 * - configured but inactive (data alt 0)
1395 * - configured and active (data alt 1)
1406 struct f_ncm *ncm = func_to_ncm(&geth->func); in ncm_open()
1408 DBG(ncm->port.func.config->cdev, "%s\n", __func__); in ncm_open()
1410 spin_lock(&ncm->lock); in ncm_open()
1411 ncm->is_open = true; in ncm_open()
1413 spin_unlock(&ncm->lock); in ncm_open()
1418 struct f_ncm *ncm = func_to_ncm(&geth->func); in ncm_close()
1420 DBG(ncm->port.func.config->cdev, "%s\n", __func__); in ncm_close()
1422 spin_lock(&ncm->lock); in ncm_close()
1423 ncm->is_open = false; in ncm_close()
1425 spin_unlock(&ncm->lock); in ncm_close()
1428 /*-------------------------------------------------------------------------*/
1434 struct usb_composite_dev *cdev = c->cdev; in ncm_bind()
1445 if (!can_support_ecm(cdev->gadget)) in ncm_bind()
1446 return -EINVAL; in ncm_bind()
1448 ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst); in ncm_bind()
1450 if (cdev->use_os_string) { in ncm_bind()
1453 return -ENOMEM; in ncm_bind()
1456 scoped_guard(mutex, &ncm_opts->lock) in ncm_bind()
1457 if (ncm_opts->bind_count == 0) { in ncm_bind()
1458 if (!device_is_registered(&ncm_opts->net->dev)) { in ncm_bind()
1459 ncm_opts->net->mtu = (ncm_opts->max_segment_size - ETH_HLEN); in ncm_bind()
1460 gether_set_gadget(ncm_opts->net, cdev->gadget); in ncm_bind()
1461 status = gether_register_netdev(ncm_opts->net); in ncm_bind()
1463 status = gether_attach_gadget(ncm_opts->net, cdev->gadget); in ncm_bind()
1467 net = ncm_opts->net; in ncm_bind()
1470 ncm_string_defs[1].s = ncm->ethaddr; in ncm_bind()
1483 /* allocate instance-specific interface IDs */ in ncm_bind()
1487 ncm->ctrl_id = status; in ncm_bind()
1496 ncm->data_id = status; in ncm_bind()
1502 ecm_desc.wMaxSegmentSize = cpu_to_le16(ncm_opts->max_segment_size); in ncm_bind()
1504 /* allocate instance-specific endpoints */ in ncm_bind()
1505 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc); in ncm_bind()
1507 return -ENODEV; in ncm_bind()
1508 ncm->port.in_ep = ep; in ncm_bind()
1510 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc); in ncm_bind()
1512 return -ENODEV; in ncm_bind()
1513 ncm->port.out_ep = ep; in ncm_bind()
1515 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc); in ncm_bind()
1517 return -ENODEV; in ncm_bind()
1518 ncm->notify = ep; in ncm_bind()
1523 return -ENOMEM; in ncm_bind()
1524 request->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL); in ncm_bind()
1525 if (!request->buf) in ncm_bind()
1526 return -ENOMEM; in ncm_bind()
1527 request->context = ncm; in ncm_bind()
1528 request->complete = ncm_notify_complete; in ncm_bind()
1532 * hardware is dual speed, all bulk-capable endpoints work at in ncm_bind()
1556 ncm->port.open = ncm_open; in ncm_bind()
1557 ncm->port.close = ncm_close; in ncm_bind()
1559 hrtimer_setup(&ncm->task_timer, ncm_tx_timeout, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT); in ncm_bind()
1561 if (cdev->use_os_string) { in ncm_bind()
1562 os_desc_table[0].os_desc = &ncm_opts->ncm_os_desc; in ncm_bind()
1564 f->os_desc_table = no_free_ptr(os_desc_table); in ncm_bind()
1565 f->os_desc_n = 1; in ncm_bind()
1567 ncm->notify_req = no_free_ptr(request); in ncm_bind()
1569 ncm_opts->bind_count++; in ncm_bind()
1573 ncm->port.in_ep->name, ncm->port.out_ep->name, in ncm_bind()
1574 ncm->notify->name); in ncm_bind()
1605 mutex_lock(&opts->lock); in ncm_opts_max_segment_size_show()
1606 segment_size = opts->max_segment_size; in ncm_opts_max_segment_size_show()
1607 mutex_unlock(&opts->lock); in ncm_opts_max_segment_size_show()
1619 mutex_lock(&opts->lock); in ncm_opts_max_segment_size_store()
1620 if (opts->refcnt) { in ncm_opts_max_segment_size_store()
1621 ret = -EBUSY; in ncm_opts_max_segment_size_store()
1630 ret = -EINVAL; in ncm_opts_max_segment_size_store()
1634 opts->max_segment_size = segment_size; in ncm_opts_max_segment_size_store()
1637 mutex_unlock(&opts->lock); in ncm_opts_max_segment_size_store()
1663 if (device_is_registered(&opts->net->dev)) in ncm_free_inst()
1664 gether_cleanup(netdev_priv(opts->net)); in ncm_free_inst()
1666 free_netdev(opts->net); in ncm_free_inst()
1667 kfree(opts->ncm_interf_group); in ncm_free_inst()
1680 return ERR_PTR(-ENOMEM); in ncm_alloc_inst()
1681 opts->ncm_os_desc.ext_compat_id = opts->ncm_ext_compat_id; in ncm_alloc_inst()
1683 mutex_init(&opts->lock); in ncm_alloc_inst()
1684 opts->func_inst.free_func_inst = ncm_free_inst; in ncm_alloc_inst()
1685 opts->net = gether_setup_default(); in ncm_alloc_inst()
1686 if (IS_ERR(opts->net)) { in ncm_alloc_inst()
1687 struct net_device *net = opts->net; in ncm_alloc_inst()
1691 opts->max_segment_size = ETH_FRAME_LEN; in ncm_alloc_inst()
1692 INIT_LIST_HEAD(&opts->ncm_os_desc.ext_prop); in ncm_alloc_inst()
1694 descs[0] = &opts->ncm_os_desc; in ncm_alloc_inst()
1697 config_group_init_type_name(&opts->func_inst.group, "", &ncm_func_type); in ncm_alloc_inst()
1699 usb_os_desc_prepare_interf_dir(&opts->func_inst.group, 1, descs, in ncm_alloc_inst()
1702 ncm_free_inst(&opts->func_inst); in ncm_alloc_inst()
1705 opts->ncm_interf_group = ncm_interf_group; in ncm_alloc_inst()
1707 return &opts->func_inst; in ncm_alloc_inst()
1716 opts = container_of(f->fi, struct f_ncm_opts, func_inst); in ncm_free()
1718 mutex_lock(&opts->lock); in ncm_free()
1719 opts->refcnt--; in ncm_free()
1720 mutex_unlock(&opts->lock); in ncm_free()
1728 DBG(c->cdev, "ncm unbind\n"); in ncm_unbind()
1730 ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst); in ncm_unbind()
1732 hrtimer_cancel(&ncm->task_timer); in ncm_unbind()
1734 kfree(f->os_desc_table); in ncm_unbind()
1735 f->os_desc_n = 0; in ncm_unbind()
1740 if (atomic_read(&ncm->notify_count)) { in ncm_unbind()
1741 usb_ep_dequeue(ncm->notify, ncm->notify_req); in ncm_unbind()
1742 atomic_set(&ncm->notify_count, 0); in ncm_unbind()
1745 kfree(ncm->notify_req->buf); in ncm_unbind()
1746 usb_ep_free_request(ncm->notify, ncm->notify_req); in ncm_unbind()
1748 ncm_opts->bind_count--; in ncm_unbind()
1749 if (ncm_opts->bind_count == 0) in ncm_unbind()
1750 gether_detach_gadget(ncm_opts->net); in ncm_unbind()
1762 return ERR_PTR(-ENOMEM); in ncm_alloc()
1765 mutex_lock(&opts->lock); in ncm_alloc()
1766 opts->refcnt++; in ncm_alloc()
1769 status = gether_get_host_addr_cdc(opts->net, ncm->ethaddr, in ncm_alloc()
1770 sizeof(ncm->ethaddr)); in ncm_alloc()
1773 mutex_unlock(&opts->lock); in ncm_alloc()
1774 return ERR_PTR(-EINVAL); in ncm_alloc()
1777 spin_lock_init(&ncm->lock); in ncm_alloc()
1779 ncm->port.ioport = netdev_priv(opts->net); in ncm_alloc()
1780 mutex_unlock(&opts->lock); in ncm_alloc()
1781 ncm->port.is_fixed = true; in ncm_alloc()
1782 ncm->port.supports_multi_frame = true; in ncm_alloc()
1784 ncm->port.func.name = "cdc_network"; in ncm_alloc()
1785 /* descriptors are per-instance copies */ in ncm_alloc()
1786 ncm->port.func.bind = ncm_bind; in ncm_alloc()
1787 ncm->port.func.unbind = ncm_unbind; in ncm_alloc()
1788 ncm->port.func.set_alt = ncm_set_alt; in ncm_alloc()
1789 ncm->port.func.get_alt = ncm_get_alt; in ncm_alloc()
1790 ncm->port.func.setup = ncm_setup; in ncm_alloc()
1791 ncm->port.func.disable = ncm_disable; in ncm_alloc()
1792 ncm->port.func.free_func = ncm_free; in ncm_alloc()
1794 ncm->port.wrap = ncm_wrap_ntb; in ncm_alloc()
1795 ncm->port.unwrap = ncm_unwrap_ntb; in ncm_alloc()
1797 return &ncm->port.func; in ncm_alloc()