Lines Matching +full:merge +full:- +full:fifo +full:- +full:en

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
30 * NCM is intended to be used with high-speed network attachments.
37 /* to trigger crc/non-crc ndp signature */
71 /* For multi-frame NDP TX */
83 /*-------------------------------------------------------------------------*/
87 * one max-size ethernet frame.
406 .language = 0x0409, /* en-us */
419 * in NDP32 -- 2 16bit words wide. Also signatures are different.
506 /*-------------------------------------------------------------------------*/
510 ncm->parser_opts = &ndp16_opts; in ncm_reset_values()
511 ncm->is_crc = false; in ncm_reset_values()
512 ncm->ndp_sign = ncm->parser_opts->ndp_sign; in ncm_reset_values()
513 ncm->port.cdc_filter = DEFAULT_FILTER; in ncm_reset_values()
516 ncm->port.header_len = 0; in ncm_reset_values()
518 ncm->port.fixed_out_len = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize); in ncm_reset_values()
519 ncm->port.fixed_in_len = NTB_DEFAULT_IN_SIZE; in ncm_reset_values()
523 * Context: ncm->lock held
527 struct usb_request *req = ncm->notify_req; in ncm_do_notify()
529 struct usb_composite_dev *cdev = ncm->port.func.config->cdev; in ncm_do_notify()
534 if (atomic_read(&ncm->notify_count)) in ncm_do_notify()
537 event = req->buf; in ncm_do_notify()
538 switch (ncm->notify_state) { in ncm_do_notify()
543 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION; in ncm_do_notify()
544 if (ncm->is_open) in ncm_do_notify()
545 event->wValue = cpu_to_le16(1); in ncm_do_notify()
547 event->wValue = cpu_to_le16(0); in ncm_do_notify()
548 event->wLength = 0; in ncm_do_notify()
549 req->length = sizeof *event; in ncm_do_notify()
552 ncm->is_open ? "true" : "false"); in ncm_do_notify()
553 ncm->notify_state = NCM_NOTIFY_NONE; in ncm_do_notify()
557 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE; in ncm_do_notify()
558 event->wValue = cpu_to_le16(0); in ncm_do_notify()
559 event->wLength = cpu_to_le16(8); in ncm_do_notify()
560 req->length = NCM_STATUS_BYTECOUNT; in ncm_do_notify()
563 data = req->buf + sizeof *event; in ncm_do_notify()
564 data[0] = cpu_to_le32(gether_bitrate(cdev->gadget)); in ncm_do_notify()
567 DBG(cdev, "notify speed %u\n", gether_bitrate(cdev->gadget)); in ncm_do_notify()
568 ncm->notify_state = NCM_NOTIFY_CONNECT; in ncm_do_notify()
571 event->bmRequestType = 0xA1; in ncm_do_notify()
572 event->wIndex = cpu_to_le16(ncm->ctrl_id); in ncm_do_notify()
574 atomic_inc(&ncm->notify_count); in ncm_do_notify()
577 * In double buffering if there is a space in FIFO, in ncm_do_notify()
581 spin_unlock(&ncm->lock); in ncm_do_notify()
582 status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC); in ncm_do_notify()
583 spin_lock(&ncm->lock); in ncm_do_notify()
585 atomic_dec(&ncm->notify_count); in ncm_do_notify()
586 DBG(cdev, "notify --> %d\n", status); in ncm_do_notify()
591 * Context: ncm->lock held
596 * NOTE on most versions of Linux, host side cdc-ethernet in ncm_notify()
598 * The first notification then sits in the FIFO for a long in ncm_notify()
605 ncm->notify_state = NCM_NOTIFY_SPEED; in ncm_notify()
611 struct f_ncm *ncm = req->context; in ncm_notify_complete()
612 struct usb_composite_dev *cdev = ncm->port.func.config->cdev; in ncm_notify_complete()
613 struct usb_cdc_notification *event = req->buf; in ncm_notify_complete()
615 spin_lock(&ncm->lock); in ncm_notify_complete()
616 switch (req->status) { in ncm_notify_complete()
619 event->bNotificationType); in ncm_notify_complete()
620 atomic_dec(&ncm->notify_count); in ncm_notify_complete()
622 case -ECONNRESET: in ncm_notify_complete()
623 case -ESHUTDOWN: in ncm_notify_complete()
624 atomic_set(&ncm->notify_count, 0); in ncm_notify_complete()
625 ncm->notify_state = NCM_NOTIFY_NONE; in ncm_notify_complete()
628 DBG(cdev, "event %02x --> %d\n", in ncm_notify_complete()
629 event->bNotificationType, req->status); in ncm_notify_complete()
630 atomic_dec(&ncm->notify_count); in ncm_notify_complete()
634 spin_unlock(&ncm->lock); in ncm_notify_complete()
641 struct usb_function *f = req->context; in ncm_ep0out_complete()
643 struct usb_composite_dev *cdev = f->config->cdev; in ncm_ep0out_complete()
645 req->context = NULL; in ncm_ep0out_complete()
646 if (req->status || req->actual != req->length) { in ncm_ep0out_complete()
647 DBG(cdev, "Bad control-OUT transfer\n"); in ncm_ep0out_complete()
651 in_size = get_unaligned_le32(req->buf); in ncm_ep0out_complete()
658 ncm->port.fixed_in_len = in_size; in ncm_ep0out_complete()
670 struct usb_composite_dev *cdev = f->config->cdev; in ncm_setup()
671 struct usb_request *req = cdev->req; in ncm_setup()
672 int value = -EOPNOTSUPP; in ncm_setup()
673 u16 w_index = le16_to_cpu(ctrl->wIndex); in ncm_setup()
674 u16 w_value = le16_to_cpu(ctrl->wValue); in ncm_setup()
675 u16 w_length = le16_to_cpu(ctrl->wLength); in ncm_setup()
681 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { in ncm_setup()
688 if (w_length != 0 || w_index != ncm->ctrl_id) in ncm_setup()
696 ncm->port.cdc_filter = w_value; in ncm_setup()
712 if (w_length == 0 || w_value != 0 || w_index != ncm->ctrl_id) in ncm_setup()
716 memcpy(req->buf, &ntb_parameters, value); in ncm_setup()
723 if (w_length < 4 || w_value != 0 || w_index != ncm->ctrl_id) in ncm_setup()
725 put_unaligned_le32(ncm->port.fixed_in_len, req->buf); in ncm_setup()
728 ncm->port.fixed_in_len); in ncm_setup()
734 if (w_length != 4 || w_value != 0 || w_index != ncm->ctrl_id) in ncm_setup()
736 req->complete = ncm_ep0out_complete; in ncm_setup()
737 req->length = w_length; in ncm_setup()
738 req->context = f; in ncm_setup()
740 value = req->length; in ncm_setup()
749 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id) in ncm_setup()
751 format = (ncm->parser_opts == &ndp16_opts) ? 0x0000 : 0x0001; in ncm_setup()
752 put_unaligned_le16(format, req->buf); in ncm_setup()
761 if (w_length != 0 || w_index != ncm->ctrl_id) in ncm_setup()
765 ncm->parser_opts = &ndp16_opts; in ncm_setup()
769 ncm->parser_opts = &ndp32_opts; in ncm_setup()
783 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id) in ncm_setup()
785 is_crc = ncm->is_crc ? 0x0001 : 0x0000; in ncm_setup()
786 put_unaligned_le16(is_crc, req->buf); in ncm_setup()
795 if (w_length != 0 || w_index != ncm->ctrl_id) in ncm_setup()
799 ncm->is_crc = false; in ncm_setup()
800 DBG(cdev, "non-CRC mode selected\n"); in ncm_setup()
803 ncm->is_crc = true; in ncm_setup()
822 ctrl->bRequestType, ctrl->bRequest, in ncm_setup()
825 ncm->ndp_sign = ncm->parser_opts->ndp_sign | in ncm_setup()
826 (ncm->is_crc ? NCM_NDP_HDR_CRC : 0); in ncm_setup()
831 ctrl->bRequestType, ctrl->bRequest, in ncm_setup()
833 req->zero = 0; in ncm_setup()
834 req->length = value; in ncm_setup()
835 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); in ncm_setup()
838 ctrl->bRequestType, ctrl->bRequest, in ncm_setup()
850 struct usb_composite_dev *cdev = f->config->cdev; in ncm_set_alt()
853 if (intf == ncm->ctrl_id) { in ncm_set_alt()
858 usb_ep_disable(ncm->notify); in ncm_set_alt()
860 if (!(ncm->notify->desc)) { in ncm_set_alt()
862 if (config_ep_by_speed(cdev->gadget, f, ncm->notify)) in ncm_set_alt()
865 usb_ep_enable(ncm->notify); in ncm_set_alt()
868 } else if (intf == ncm->data_id) { in ncm_set_alt()
872 if (ncm->netdev) { in ncm_set_alt()
874 ncm->netdev = NULL; in ncm_set_alt()
875 gether_disconnect(&ncm->port); in ncm_set_alt()
880 * CDC Network only sends data in non-default altsettings. in ncm_set_alt()
886 if (!ncm->port.in_ep->desc || in ncm_set_alt()
887 !ncm->port.out_ep->desc) { in ncm_set_alt()
889 if (config_ep_by_speed(cdev->gadget, f, in ncm_set_alt()
890 ncm->port.in_ep) || in ncm_set_alt()
891 config_ep_by_speed(cdev->gadget, f, in ncm_set_alt()
892 ncm->port.out_ep)) { in ncm_set_alt()
893 ncm->port.in_ep->desc = NULL; in ncm_set_alt()
894 ncm->port.out_ep->desc = NULL; in ncm_set_alt()
903 ncm->port.is_zlp_ok = in ncm_set_alt()
904 gadget_is_zlp_supported(cdev->gadget); in ncm_set_alt()
905 ncm->port.cdc_filter = DEFAULT_FILTER; in ncm_set_alt()
907 net = gether_connect(&ncm->port); in ncm_set_alt()
910 ncm->netdev = net; in ncm_set_alt()
913 spin_lock(&ncm->lock); in ncm_set_alt()
915 spin_unlock(&ncm->lock); in ncm_set_alt()
921 return -EINVAL; in ncm_set_alt()
932 if (intf == ncm->ctrl_id) in ncm_get_alt()
934 return ncm->port.in_ep->enabled ? 1 : 0; in ncm_get_alt()
945 const struct ndp_parser_opts *opts = ncm->parser_opts; in package_for_tx()
947 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len; in package_for_tx()
950 hrtimer_try_to_cancel(&ncm->task_timer); in package_for_tx()
952 ndp_pad = ALIGN(ncm->skb_tx_data->len, ndp_align) - in package_for_tx()
953 ncm->skb_tx_data->len; in package_for_tx()
954 ndp_index = ncm->skb_tx_data->len + ndp_pad; in package_for_tx()
955 new_len = ndp_index + dgram_idx_len + ncm->skb_tx_ndp->len; in package_for_tx()
958 ntb_iter = (void *) ncm->skb_tx_data->data; in package_for_tx()
961 put_ncm(&ntb_iter, opts->block_length, new_len); in package_for_tx()
962 put_ncm(&ntb_iter, opts->ndp_index, ndp_index); in package_for_tx()
965 new_len = opts->ndp_size + in package_for_tx()
966 (ncm->ndp_dgram_count * dgram_idx_len); in package_for_tx()
967 ncm->ndp_dgram_count = 0; in package_for_tx()
969 ntb_iter = (void *) ncm->skb_tx_ndp->data; in package_for_tx()
973 /* Merge the skbs */ in package_for_tx()
974 swap(skb2, ncm->skb_tx_data); in package_for_tx()
975 if (ncm->skb_tx_data) { in package_for_tx()
976 dev_consume_skb_any(ncm->skb_tx_data); in package_for_tx()
977 ncm->skb_tx_data = NULL; in package_for_tx()
984 skb_put_data(skb2, ncm->skb_tx_ndp->data, ncm->skb_tx_ndp->len); in package_for_tx()
985 dev_consume_skb_any(ncm->skb_tx_ndp); in package_for_tx()
986 ncm->skb_tx_ndp = NULL; in package_for_tx()
997 struct f_ncm *ncm = func_to_ncm(&port->func); in ncm_wrap_ntb()
1006 unsigned max_size = ncm->port.fixed_in_len; in ncm_wrap_ntb()
1007 const struct ndp_parser_opts *opts = ncm->parser_opts; in ncm_wrap_ntb()
1011 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len; in ncm_wrap_ntb()
1014 if (ncm->is_crc) { in ncm_wrap_ntb()
1019 skb->data, in ncm_wrap_ntb()
1020 skb->len); in ncm_wrap_ntb()
1030 if (ncm->skb_tx_data in ncm_wrap_ntb()
1031 && (ncm->ndp_dgram_count >= TX_MAX_NUM_DPE in ncm_wrap_ntb()
1032 || (ncm->skb_tx_data->len + in ncm_wrap_ntb()
1033 div + rem + skb->len + in ncm_wrap_ntb()
1034 ncm->skb_tx_ndp->len + ndp_align + (2 * dgram_idx_len)) in ncm_wrap_ntb()
1041 if (!ncm->skb_tx_data) { in ncm_wrap_ntb()
1042 ncb_len = opts->nth_size; in ncm_wrap_ntb()
1043 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len; in ncm_wrap_ntb()
1047 ncm->skb_tx_data = alloc_skb(max_size, GFP_ATOMIC); in ncm_wrap_ntb()
1048 if (!ncm->skb_tx_data) in ncm_wrap_ntb()
1051 ncm->skb_tx_data->dev = ncm->netdev; in ncm_wrap_ntb()
1052 ntb_data = skb_put_zero(ncm->skb_tx_data, ncb_len); in ncm_wrap_ntb()
1054 put_unaligned_le32(opts->nth_sign, ntb_data); in ncm_wrap_ntb()
1057 put_unaligned_le16(opts->nth_size, ntb_data++); in ncm_wrap_ntb()
1063 ncm->skb_tx_ndp = alloc_skb((int)(opts->ndp_size in ncm_wrap_ntb()
1064 + opts->dpe_size in ncm_wrap_ntb()
1067 if (!ncm->skb_tx_ndp) in ncm_wrap_ntb()
1070 ncm->skb_tx_ndp->dev = ncm->netdev; in ncm_wrap_ntb()
1071 ntb_ndp = skb_put(ncm->skb_tx_ndp, opts->ndp_size); in ncm_wrap_ntb()
1074 put_unaligned_le32(ncm->ndp_sign, ntb_ndp); in ncm_wrap_ntb()
1078 ncm->ndp_dgram_count = 1; in ncm_wrap_ntb()
1080 /* Note: we skip opts->next_ndp_index */ in ncm_wrap_ntb()
1083 hrtimer_start(&ncm->task_timer, TX_TIMEOUT_NSECS, in ncm_wrap_ntb()
1088 ntb_ndp = skb_put_zero(ncm->skb_tx_ndp, dgram_idx_len); in ncm_wrap_ntb()
1090 ncb_len = ncm->skb_tx_data->len; in ncm_wrap_ntb()
1091 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len; in ncm_wrap_ntb()
1095 put_ncm(&ntb_ndp, opts->dgram_item_len, ncb_len); in ncm_wrap_ntb()
1097 put_ncm(&ntb_ndp, opts->dgram_item_len, skb->len); in ncm_wrap_ntb()
1098 ncm->ndp_dgram_count++; in ncm_wrap_ntb()
1101 skb_put_zero(ncm->skb_tx_data, dgram_pad); in ncm_wrap_ntb()
1102 skb_put_data(ncm->skb_tx_data, skb->data, skb->len); in ncm_wrap_ntb()
1106 } else if (ncm->skb_tx_data) { in ncm_wrap_ntb()
1109 * ncm_tx_timeout() - hence, this is our signal to flush/send. in ncm_wrap_ntb()
1119 ncm->netdev->stats.tx_dropped++; in ncm_wrap_ntb()
1123 if (ncm->skb_tx_data) in ncm_wrap_ntb()
1124 dev_kfree_skb_any(ncm->skb_tx_data); in ncm_wrap_ntb()
1125 if (ncm->skb_tx_ndp) in ncm_wrap_ntb()
1126 dev_kfree_skb_any(ncm->skb_tx_ndp); in ncm_wrap_ntb()
1138 struct net_device *netdev = READ_ONCE(ncm->netdev); in ncm_tx_timeout()
1143 * XXX that the dev->wrap() invocations to build SKBs is transparent in ncm_tx_timeout()
1149 netdev->netdev_ops->ndo_start_xmit(NULL, netdev); in ncm_tx_timeout()
1158 struct f_ncm *ncm = func_to_ncm(&port->func); in ncm_unwrap_ntb()
1159 unsigned char *ntb_ptr = skb->data; in ncm_unwrap_ntb()
1167 int ret = -EINVAL; in ncm_unwrap_ntb()
1170 const struct ndp_parser_opts *opts = ncm->parser_opts; in ncm_unwrap_ntb()
1171 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0; in ncm_unwrap_ntb()
1173 int to_process = skb->len; in ncm_unwrap_ntb()
1179 if (get_unaligned_le32(tmp) != opts->nth_sign) { in ncm_unwrap_ntb()
1180 INFO(port->func.config->cdev, "Wrong NTH SIGN, skblen %d\n", in ncm_unwrap_ntb()
1181 skb->len); in ncm_unwrap_ntb()
1183 skb->data, 32, false); in ncm_unwrap_ntb()
1189 if (get_unaligned_le16(tmp++) != opts->nth_size) { in ncm_unwrap_ntb()
1190 INFO(port->func.config->cdev, "Wrong NTB headersize\n"); in ncm_unwrap_ntb()
1195 block_len = get_ncm(&tmp, opts->block_length); in ncm_unwrap_ntb()
1198 INFO(port->func.config->cdev, "OUT size exceeded\n"); in ncm_unwrap_ntb()
1202 ndp_index = get_ncm(&tmp, opts->ndp_index); in ncm_unwrap_ntb()
1211 (ndp_index < opts->nth_size) || in ncm_unwrap_ntb()
1212 (ndp_index > (block_len - in ncm_unwrap_ntb()
1213 opts->ndp_size))) { in ncm_unwrap_ntb()
1214 INFO(port->func.config->cdev, "Bad index: %#X\n", in ncm_unwrap_ntb()
1224 if (get_unaligned_le32(tmp) != ncm->ndp_sign) { in ncm_unwrap_ntb()
1225 INFO(port->func.config->cdev, "Wrong NDP SIGN\n"); in ncm_unwrap_ntb()
1235 * item size is 16/32 bits, opts->dgram_item_len * 2 bytes in ncm_unwrap_ntb()
1239 if ((ndp_len < opts->ndp_size in ncm_unwrap_ntb()
1240 + 2 * 2 * (opts->dgram_item_len * 2)) || in ncm_unwrap_ntb()
1241 (ndp_len % opts->ndplen_align != 0)) { in ncm_unwrap_ntb()
1242 INFO(port->func.config->cdev, "Bad NDP length: %#X\n", in ncm_unwrap_ntb()
1246 tmp += opts->reserved1; in ncm_unwrap_ntb()
1248 ndp_index = get_ncm(&tmp, opts->next_ndp_index); in ncm_unwrap_ntb()
1249 tmp += opts->reserved2; in ncm_unwrap_ntb()
1251 ndp_len -= opts->ndp_size; in ncm_unwrap_ntb()
1252 index2 = get_ncm(&tmp, opts->dgram_item_len); in ncm_unwrap_ntb()
1253 dg_len2 = get_ncm(&tmp, opts->dgram_item_len); in ncm_unwrap_ntb()
1259 if ((index < opts->nth_size) || in ncm_unwrap_ntb()
1260 (index > block_len - opts->dpe_size)) { in ncm_unwrap_ntb()
1261 INFO(port->func.config->cdev, in ncm_unwrap_ntb()
1273 INFO(port->func.config->cdev, in ncm_unwrap_ntb()
1277 if (ncm->is_crc) { in ncm_unwrap_ntb()
1281 index + dg_len - in ncm_unwrap_ntb()
1285 dg_len - crc_len); in ncm_unwrap_ntb()
1287 INFO(port->func.config->cdev, in ncm_unwrap_ntb()
1293 index2 = get_ncm(&tmp, opts->dgram_item_len); in ncm_unwrap_ntb()
1294 dg_len2 = get_ncm(&tmp, opts->dgram_item_len); in ncm_unwrap_ntb()
1297 if (index2 > block_len - opts->dpe_size) { in ncm_unwrap_ntb()
1298 INFO(port->func.config->cdev, in ncm_unwrap_ntb()
1307 skb2 = netdev_alloc_skb_ip_align(ncm->netdev, in ncm_unwrap_ntb()
1308 dg_len - crc_len); in ncm_unwrap_ntb()
1312 dg_len - crc_len); in ncm_unwrap_ntb()
1316 ndp_len -= 2 * (opts->dgram_item_len * 2); in ncm_unwrap_ntb()
1321 } while (ndp_len > 2 * (opts->dgram_item_len * 2)); in ncm_unwrap_ntb()
1324 VDBG(port->func.config->cdev, in ncm_unwrap_ntb()
1327 to_process -= block_len; in ncm_unwrap_ntb()
1330 * Windows NCM driver avoids USB ZLPs by adding a 1-byte in ncm_unwrap_ntb()
1335 to_process--; in ncm_unwrap_ntb()
1353 struct usb_composite_dev *cdev = f->config->cdev; in ncm_disable()
1357 if (ncm->netdev) { in ncm_disable()
1358 ncm->netdev = NULL; in ncm_disable()
1359 gether_disconnect(&ncm->port); in ncm_disable()
1362 if (ncm->notify->enabled) { in ncm_disable()
1363 usb_ep_disable(ncm->notify); in ncm_disable()
1364 ncm->notify->desc = NULL; in ncm_disable()
1368 /*-------------------------------------------------------------------------*/
1377 * - disconnected/unconfigured
1378 * - configured but inactive (data alt 0)
1379 * - configured and active (data alt 1)
1390 struct f_ncm *ncm = func_to_ncm(&geth->func); in ncm_open()
1392 DBG(ncm->port.func.config->cdev, "%s\n", __func__); in ncm_open()
1394 spin_lock(&ncm->lock); in ncm_open()
1395 ncm->is_open = true; in ncm_open()
1397 spin_unlock(&ncm->lock); in ncm_open()
1402 struct f_ncm *ncm = func_to_ncm(&geth->func); in ncm_close()
1404 DBG(ncm->port.func.config->cdev, "%s\n", __func__); in ncm_close()
1406 spin_lock(&ncm->lock); in ncm_close()
1407 ncm->is_open = false; in ncm_close()
1409 spin_unlock(&ncm->lock); in ncm_close()
1412 /*-------------------------------------------------------------------------*/
1418 struct usb_composite_dev *cdev = c->cdev; in ncm_bind()
1425 if (!can_support_ecm(cdev->gadget)) in ncm_bind()
1426 return -EINVAL; in ncm_bind()
1428 ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst); in ncm_bind()
1430 if (cdev->use_os_string) { in ncm_bind()
1431 f->os_desc_table = kzalloc(sizeof(*f->os_desc_table), in ncm_bind()
1433 if (!f->os_desc_table) in ncm_bind()
1434 return -ENOMEM; in ncm_bind()
1435 f->os_desc_n = 1; in ncm_bind()
1436 f->os_desc_table[0].os_desc = &ncm_opts->ncm_os_desc; in ncm_bind()
1439 mutex_lock(&ncm_opts->lock); in ncm_bind()
1440 gether_set_gadget(ncm_opts->net, cdev->gadget); in ncm_bind()
1441 if (!ncm_opts->bound) in ncm_bind()
1442 status = gether_register_netdev(ncm_opts->net); in ncm_bind()
1443 mutex_unlock(&ncm_opts->lock); in ncm_bind()
1448 ncm_opts->bound = true; in ncm_bind()
1462 /* allocate instance-specific interface IDs */ in ncm_bind()
1466 ncm->ctrl_id = status; in ncm_bind()
1472 if (cdev->use_os_string) in ncm_bind()
1473 f->os_desc_table[0].if_id = in ncm_bind()
1479 ncm->data_id = status; in ncm_bind()
1485 status = -ENODEV; in ncm_bind()
1487 /* allocate instance-specific endpoints */ in ncm_bind()
1488 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc); in ncm_bind()
1491 ncm->port.in_ep = ep; in ncm_bind()
1493 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc); in ncm_bind()
1496 ncm->port.out_ep = ep; in ncm_bind()
1498 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc); in ncm_bind()
1501 ncm->notify = ep; in ncm_bind()
1503 status = -ENOMEM; in ncm_bind()
1506 ncm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL); in ncm_bind()
1507 if (!ncm->notify_req) in ncm_bind()
1509 ncm->notify_req->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL); in ncm_bind()
1510 if (!ncm->notify_req->buf) in ncm_bind()
1512 ncm->notify_req->context = ncm; in ncm_bind()
1513 ncm->notify_req->complete = ncm_notify_complete; in ncm_bind()
1517 * hardware is dual speed, all bulk-capable endpoints work at in ncm_bind()
1541 ncm->port.open = ncm_open; in ncm_bind()
1542 ncm->port.close = ncm_close; in ncm_bind()
1544 hrtimer_init(&ncm->task_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT); in ncm_bind()
1545 ncm->task_timer.function = ncm_tx_timeout; in ncm_bind()
1548 ncm->port.in_ep->name, ncm->port.out_ep->name, in ncm_bind()
1549 ncm->notify->name); in ncm_bind()
1553 kfree(f->os_desc_table); in ncm_bind()
1554 f->os_desc_n = 0; in ncm_bind()
1556 if (ncm->notify_req) { in ncm_bind()
1557 kfree(ncm->notify_req->buf); in ncm_bind()
1558 usb_ep_free_request(ncm->notify, ncm->notify_req); in ncm_bind()
1561 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); in ncm_bind()
1606 if (opts->bound) in ncm_free_inst()
1607 gether_cleanup(netdev_priv(opts->net)); in ncm_free_inst()
1609 free_netdev(opts->net); in ncm_free_inst()
1610 kfree(opts->ncm_interf_group); in ncm_free_inst()
1623 return ERR_PTR(-ENOMEM); in ncm_alloc_inst()
1624 opts->ncm_os_desc.ext_compat_id = opts->ncm_ext_compat_id; in ncm_alloc_inst()
1626 mutex_init(&opts->lock); in ncm_alloc_inst()
1627 opts->func_inst.free_func_inst = ncm_free_inst; in ncm_alloc_inst()
1628 opts->net = gether_setup_default(); in ncm_alloc_inst()
1629 if (IS_ERR(opts->net)) { in ncm_alloc_inst()
1630 struct net_device *net = opts->net; in ncm_alloc_inst()
1634 INIT_LIST_HEAD(&opts->ncm_os_desc.ext_prop); in ncm_alloc_inst()
1636 descs[0] = &opts->ncm_os_desc; in ncm_alloc_inst()
1639 config_group_init_type_name(&opts->func_inst.group, "", &ncm_func_type); in ncm_alloc_inst()
1641 usb_os_desc_prepare_interf_dir(&opts->func_inst.group, 1, descs, in ncm_alloc_inst()
1644 ncm_free_inst(&opts->func_inst); in ncm_alloc_inst()
1647 opts->ncm_interf_group = ncm_interf_group; in ncm_alloc_inst()
1649 return &opts->func_inst; in ncm_alloc_inst()
1658 opts = container_of(f->fi, struct f_ncm_opts, func_inst); in ncm_free()
1660 mutex_lock(&opts->lock); in ncm_free()
1661 opts->refcnt--; in ncm_free()
1662 mutex_unlock(&opts->lock); in ncm_free()
1669 DBG(c->cdev, "ncm unbind\n"); in ncm_unbind()
1671 hrtimer_cancel(&ncm->task_timer); in ncm_unbind()
1673 kfree(f->os_desc_table); in ncm_unbind()
1674 f->os_desc_n = 0; in ncm_unbind()
1679 if (atomic_read(&ncm->notify_count)) { in ncm_unbind()
1680 usb_ep_dequeue(ncm->notify, ncm->notify_req); in ncm_unbind()
1681 atomic_set(&ncm->notify_count, 0); in ncm_unbind()
1684 kfree(ncm->notify_req->buf); in ncm_unbind()
1685 usb_ep_free_request(ncm->notify, ncm->notify_req); in ncm_unbind()
1697 return ERR_PTR(-ENOMEM); in ncm_alloc()
1700 mutex_lock(&opts->lock); in ncm_alloc()
1701 opts->refcnt++; in ncm_alloc()
1704 status = gether_get_host_addr_cdc(opts->net, ncm->ethaddr, in ncm_alloc()
1705 sizeof(ncm->ethaddr)); in ncm_alloc()
1708 mutex_unlock(&opts->lock); in ncm_alloc()
1709 return ERR_PTR(-EINVAL); in ncm_alloc()
1711 ncm_string_defs[STRING_MAC_IDX].s = ncm->ethaddr; in ncm_alloc()
1713 spin_lock_init(&ncm->lock); in ncm_alloc()
1715 ncm->port.ioport = netdev_priv(opts->net); in ncm_alloc()
1716 mutex_unlock(&opts->lock); in ncm_alloc()
1717 ncm->port.is_fixed = true; in ncm_alloc()
1718 ncm->port.supports_multi_frame = true; in ncm_alloc()
1720 ncm->port.func.name = "cdc_network"; in ncm_alloc()
1721 /* descriptors are per-instance copies */ in ncm_alloc()
1722 ncm->port.func.bind = ncm_bind; in ncm_alloc()
1723 ncm->port.func.unbind = ncm_unbind; in ncm_alloc()
1724 ncm->port.func.set_alt = ncm_set_alt; in ncm_alloc()
1725 ncm->port.func.get_alt = ncm_get_alt; in ncm_alloc()
1726 ncm->port.func.setup = ncm_setup; in ncm_alloc()
1727 ncm->port.func.disable = ncm_disable; in ncm_alloc()
1728 ncm->port.func.free_func = ncm_free; in ncm_alloc()
1730 ncm->port.wrap = ncm_wrap_ntb; in ncm_alloc()
1731 ncm->port.unwrap = ncm_unwrap_ntb; in ncm_alloc()
1733 return &ncm->port.func; in ncm_alloc()