18 #include <linux/kernel.h>
19 #include <linux/device.h>
38 #define NCM_NDP_HDR_CRC_MASK 0x01000000
39 #define NCM_NDP_HDR_CRC 0x01000000
40 #define NCM_NDP_HDR_NOCRC 0x00000000
75 static inline unsigned ncm_bitrate(
struct usb_gadget *g)
78 return 13 * 512 * 8 * 1000 * 8;
80 return 19 * 64 * 1 * 1000 * 8;
91 #define NTB_DEFAULT_IN_SIZE USB_CDC_NCM_NTB_MIN_IN_SIZE
92 #define NTB_OUT_SIZE 16384
99 #define MAX_TX_NONFIXED (512 * 3)
101 #define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
102 USB_CDC_NCM_NTB32_SUPPORTED)
105 .wLength =
sizeof ntb_parameters,
124 #define LOG2_STATUS_INTERVAL_MSEC 5
125 #define NCM_STATUS_BYTECOUNT 16
128 .bLength =
sizeof ncm_iad_desc,
132 .bInterfaceCount = 2,
142 .
bLength =
sizeof ncm_control_intf,
154 .
bLength =
sizeof ncm_header_desc,
162 .
bLength =
sizeof(ncm_union_desc),
179 .bNumberPowerFilters = 0,
182 #define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE)
191 .bmNetworkCapabilities =
NCAPS,
197 .
bLength =
sizeof ncm_data_nop_intf,
200 .bInterfaceNumber = 1,
201 .bAlternateSetting = 0,
204 .bInterfaceSubClass = 0,
212 .
bLength =
sizeof ncm_data_intf,
215 .bInterfaceNumber = 1,
216 .bAlternateSetting = 1,
219 .bInterfaceSubClass = 0,
317 #define STRING_CTRL_IDX 0
318 #define STRING_MAC_IDX 1
319 #define STRING_DATA_IDX 2
320 #define STRING_IAD_IDX 3
322 static struct usb_string ncm_string_defs[] = {
332 .strings = ncm_string_defs,
364 #define INIT_NDP16_OPTS { \
365 .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
366 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
367 .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
368 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
370 .dgram_item_len = 1, \
375 .next_fp_index = 1, \
379 #define INIT_NDP32_OPTS { \
380 .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
381 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
382 .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
383 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
385 .dgram_item_len = 2, \
390 .next_fp_index = 2, \
396 static inline void put_ncm(
__le16 **
p,
unsigned size,
unsigned val)
400 put_unaligned_le16((
u16)val, *p);
413 static inline unsigned get_ncm(
__le16 **
p,
unsigned size)
419 tmp = get_unaligned_le16(*p);
434 static inline void ncm_reset_values(
struct f_ncm *ncm)
441 ncm->
port.header_len = 0;
450 static void ncm_do_notify(
struct f_ncm *ncm)
476 DBG(cdev,
"notify connect %s\n",
477 ncm->
is_open ?
"true" :
"false");
492 DBG(cdev,
"notify speed %d\n", ncm_bitrate(cdev->
gadget));
496 event->bmRequestType = 0xA1;
505 spin_unlock(&ncm->
lock);
507 spin_lock(&ncm->
lock);
510 DBG(cdev,
"notify --> %d\n", status);
517 static void ncm_notify(
struct f_ncm *ncm)
539 spin_lock(&ncm->
lock);
542 VDBG(cdev,
"Notification %02x sent\n",
550 DBG(cdev,
"event %02x --> %d\n",
556 spin_unlock(&ncm->
lock);
564 struct f_ncm *ncm = func_to_ncm(f);
569 DBG(cdev,
"Bad control-OUT transfer\n");
576 DBG(cdev,
"Got wrong INPUT SIZE (%d) from host\n", in_size);
580 ncm->
port.fixed_in_len = in_size;
581 VDBG(cdev,
"Set NTB INPUT SIZE %d\n", in_size);
591 struct f_ncm *ncm = func_to_ncm(f);
610 if (w_length != 0 || w_index != ncm->
ctrl_id)
612 DBG(cdev,
"packet filter %02x\n", w_value);
618 ncm->
port.cdc_filter = w_value;
634 if (w_length == 0 || w_value != 0 || w_index != ncm->
ctrl_id)
636 value = w_length >
sizeof ntb_parameters ?
638 memcpy(req->
buf, &ntb_parameters, value);
639 VDBG(cdev,
"Host asked NTB parameters\n");
645 if (w_length < 4 || w_value != 0 || w_index != ncm->ctrl_id)
649 VDBG(cdev,
"Host asked INPUT SIZE, sending %d\n",
650 ncm->
port.fixed_in_len);
656 if (w_length != 4 || w_value != 0 || w_index != ncm->
ctrl_id)
658 req->
complete = ncm_ep0out_complete;
671 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
673 format = (ncm->
parser_opts == &ndp16_opts) ? 0x0000 : 0x0001;
674 put_unaligned_le16(format, req->
buf);
676 VDBG(cdev,
"Host asked NTB FORMAT, sending %d\n", format);
683 if (w_length != 0 || w_index != ncm->
ctrl_id)
688 DBG(cdev,
"NCM16 selected\n");
692 DBG(cdev,
"NCM32 selected\n");
705 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
707 is_crc = ncm->
is_crc ? 0x0001 : 0x0000;
708 put_unaligned_le16(is_crc, req->
buf);
710 VDBG(cdev,
"Host asked CRC MODE, sending %d\n", is_crc);
719 if (w_length != 0 || w_index != ncm->
ctrl_id)
725 DBG(cdev,
"non-CRC mode selected\n");
730 DBG(cdev,
"CRC mode selected\n");
749 DBG(cdev,
"invalid control req%02x.%02x v%04x i%04x l%d\n",
751 w_value, w_index, w_length);
756 DBG(cdev,
"ncm req%02x.%02x v%04x i%04x l%d\n",
758 w_value, w_index, w_length);
763 ERROR(cdev,
"ncm req %02x.%02x response err %d\n",
775 struct f_ncm *ncm = func_to_ncm(f);
783 if (ncm->
notify->driver_data) {
784 DBG(cdev,
"reset ncm control %d\n", intf);
785 usb_ep_disable(ncm->
notify);
788 if (!(ncm->
notify->desc)) {
789 DBG(cdev,
"init ncm ctrl %d\n", intf);
793 usb_ep_enable(ncm->
notify);
794 ncm->
notify->driver_data = ncm;
797 }
else if (intf == ncm->
data_id) {
801 if (ncm->
port.in_ep->driver_data) {
802 DBG(cdev,
"reset ncm\n");
804 ncm_reset_values(ncm);
814 if (!ncm->
port.in_ep->desc ||
815 !ncm->
port.out_ep->desc) {
816 DBG(cdev,
"init ncm\n");
831 ncm->
port.is_zlp_ok = !(
835 DBG(cdev,
"activate ncm\n");
841 spin_lock(&ncm->
lock);
843 spin_unlock(&ncm->
lock);
856 static int ncm_get_alt(
struct usb_function *f,
unsigned intf)
858 struct f_ncm *ncm = func_to_ncm(f);
862 return ncm->
port.in_ep->driver_data ? 1 : 0;
868 struct f_ncm *ncm = func_to_ncm(&port->
func);
882 ndp_pad =
ALIGN(ncb_len, ndp_align) - ncb_len;
887 pad =
ALIGN(ncb_len, div) + rem - ncb_len;
890 if (ncb_len + skb->
len + crc_len > max_size) {
896 max_size - skb->
len - ncb_len - crc_len,
904 tmp = (
void *)
skb_push(skb, ncb_len);
910 put_unaligned_le16(opts->
nth_size, tmp++);
917 tmp = (
void *)tmp + ndp_pad;
923 put_unaligned_le16(ncb_len - opts->
nth_size - pad, tmp++);
947 0, max_size - skb->
len);
952 static int ncm_unwrap_ntb(
struct gether *port,
956 struct f_ncm *ncm = func_to_ncm(&port->
func);
959 unsigned dg_len, dg_len2;
970 INFO(port->
func.config->cdev,
"Wrong NTH SIGN, skblen %d\n",
973 skb->
data, 32,
false);
979 if (get_unaligned_le16(tmp++) != opts->
nth_size) {
980 INFO(port->
func.config->cdev,
"Wrong NTB headersize\n");
987 INFO(port->
func.config->cdev,
"OUT size exceeded\n");
991 index = get_ncm(&tmp, opts->
fp_index);
993 if (((index % 4) != 0) && (index < opts->
nth_size)) {
994 INFO(port->
func.config->cdev,
"Bad index: %x\n",
1002 INFO(port->
func.config->cdev,
"Wrong NDP SIGN\n");
1007 ndp_len = get_unaligned_le16(tmp++);
1016 INFO(port->
func.config->cdev,
"Bad NDP length: %x\n", ndp_len);
1031 if (dg_len < 14 + crc_len) {
1032 INFO(port->
func.config->cdev,
"Bad dgram length: %x\n",
1040 index + dg_len - crc_len);
1045 INFO(port->
func.config->cdev,
"Bad CRC\n");
1053 if (
index2 == 0 || dg_len2 == 0) {
1073 if (
index2 == 0 || dg_len2 == 0)
1078 "Parsed NTB with %d frames\n", dgram_counter);
1088 struct f_ncm *ncm = func_to_ncm(f);
1091 DBG(cdev,
"ncm deactivated\n");
1093 if (ncm->
port.in_ep->driver_data)
1096 if (ncm->
notify->driver_data) {
1097 usb_ep_disable(ncm->
notify);
1123 static void ncm_open(
struct gether *geth)
1125 struct f_ncm *ncm = func_to_ncm(&geth->
func);
1127 DBG(ncm->
port.func.config->cdev,
"%s\n", __func__);
1129 spin_lock(&ncm->
lock);
1132 spin_unlock(&ncm->
lock);
1135 static void ncm_close(
struct gether *geth)
1137 struct f_ncm *ncm = func_to_ncm(&geth->
func);
1139 DBG(ncm->
port.func.config->cdev,
"%s\n", __func__);
1141 spin_lock(&ncm->
lock);
1144 spin_unlock(&ncm->
lock);
1155 struct f_ncm *ncm = func_to_ncm(f);
1164 ncm_iad_desc.bFirstInterface =
status;
1166 ncm_control_intf.bInterfaceNumber =
status;
1167 ncm_union_desc.bMasterInterface0 =
status;
1174 ncm_data_nop_intf.bInterfaceNumber =
status;
1175 ncm_data_intf.bInterfaceNumber =
status;
1176 ncm_union_desc.bSlaveInterface0 =
status;
1184 ncm->
port.in_ep = ep;
1190 ncm->
port.out_ep = ep;
1209 ncm->
notify_req->complete = ncm_notify_complete;
1221 if (gadget_is_dualspeed(c->
cdev->gadget)) {
1222 hs_ncm_in_desc.bEndpointAddress =
1223 fs_ncm_in_desc.bEndpointAddress;
1224 hs_ncm_out_desc.bEndpointAddress =
1225 fs_ncm_out_desc.bEndpointAddress;
1226 hs_ncm_notify_desc.bEndpointAddress =
1227 fs_ncm_notify_desc.bEndpointAddress;
1241 ncm->
port.open = ncm_open;
1242 ncm->
port.close = ncm_close;
1244 DBG(cdev,
"CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
1245 gadget_is_dualspeed(c->
cdev->gadget) ?
"dual" :
"full",
1246 ncm->
port.in_ep->name, ncm->
port.out_ep->name,
1262 if (ncm->
port.out_ep->desc)
1263 ncm->
port.out_ep->driver_data =
NULL;
1264 if (ncm->
port.in_ep->desc)
1265 ncm->
port.in_ep->driver_data =
NULL;
1267 ERROR(cdev,
"%s: can't bind, err %d\n", f->
name, status);
1275 struct f_ncm *ncm = func_to_ncm(f);
1279 if (gadget_is_dualspeed(c->
cdev->gadget))
1286 ncm_string_defs[1].
s =
NULL;
1307 if (!can_support_ecm(c->
cdev->gadget) || !
ethaddr)
1311 if (ncm_string_defs[0].
id == 0) {
1318 ncm_control_intf.iInterface =
status;
1325 ncm_data_nop_intf.iInterface =
status;
1326 ncm_data_intf.iInterface =
status;
1333 ecm_desc.iMACAddress =
status;
1340 ncm_iad_desc.iFunction =
status;
1350 ncm_string_defs[1].
s = ncm->
ethaddr;
1353 ncm_reset_values(ncm);
1354 ncm->
port.is_fixed =
true;
1356 ncm->
port.func.name =
"cdc_network";
1357 ncm->
port.func.strings = ncm_strings;
1359 ncm->
port.func.bind = ncm_bind;
1360 ncm->
port.func.unbind = ncm_unbind;
1361 ncm->
port.func.set_alt = ncm_set_alt;
1362 ncm->
port.func.get_alt = ncm_get_alt;
1363 ncm->
port.func.setup = ncm_setup;
1364 ncm->
port.func.disable = ncm_disable;
1366 ncm->
port.wrap = ncm_wrap_ntb;
1367 ncm->
port.unwrap = ncm_unwrap_ntb;
1371 ncm_string_defs[1].
s =
NULL;