caif_usb.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * CAIF USB handler
  4. * Copyright (C) ST-Ericsson AB 2011
  5. * Author: Sjur Brendeland
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
  8. #include <linux/module.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/slab.h>
  11. #include <linux/mii.h>
  12. #include <linux/usb.h>
  13. #include <linux/usb/usbnet.h>
  14. #include <linux/etherdevice.h>
  15. #include <net/netns/generic.h>
  16. #include <net/caif/caif_dev.h>
  17. #include <net/caif/caif_layer.h>
  18. #include <net/caif/cfpkt.h>
  19. #include <net/caif/cfcnfg.h>
  20. MODULE_LICENSE("GPL");
  21. #define CFUSB_PAD_DESCR_SZ 1 /* Alignment descriptor length */
  22. #define CFUSB_ALIGNMENT 4 /* Number of bytes to align. */
  23. #define CFUSB_MAX_HEADLEN (CFUSB_PAD_DESCR_SZ + CFUSB_ALIGNMENT-1)
  24. #define STE_USB_VID 0x04cc /* USB Product ID for ST-Ericsson */
  25. #define STE_USB_PID_CAIF 0x230f /* Product id for CAIF Modems */
  26. struct cfusbl {
  27. struct cflayer layer;
  28. u8 tx_eth_hdr[ETH_HLEN];
  29. };
  30. static bool pack_added;
  31. static int cfusbl_receive(struct cflayer *layr, struct cfpkt *pkt)
  32. {
  33. u8 hpad;
  34. /* Remove padding. */
  35. cfpkt_extr_head(pkt, &hpad, 1);
  36. cfpkt_extr_head(pkt, NULL, hpad);
  37. return layr->up->receive(layr->up, pkt);
  38. }
  39. static int cfusbl_transmit(struct cflayer *layr, struct cfpkt *pkt)
  40. {
  41. struct caif_payload_info *info;
  42. u8 hpad;
  43. u8 zeros[CFUSB_ALIGNMENT];
  44. struct sk_buff *skb;
  45. struct cfusbl *usbl = container_of(layr, struct cfusbl, layer);
  46. skb = cfpkt_tonative(pkt);
  47. skb_reset_network_header(skb);
  48. skb->protocol = htons(ETH_P_IP);
  49. info = cfpkt_info(pkt);
  50. hpad = (info->hdr_len + CFUSB_PAD_DESCR_SZ) & (CFUSB_ALIGNMENT - 1);
  51. if (skb_headroom(skb) < ETH_HLEN + CFUSB_PAD_DESCR_SZ + hpad) {
  52. pr_warn("Headroom too small\n");
  53. kfree_skb(skb);
  54. return -EIO;
  55. }
  56. memset(zeros, 0, hpad);
  57. cfpkt_add_head(pkt, zeros, hpad);
  58. cfpkt_add_head(pkt, &hpad, 1);
  59. cfpkt_add_head(pkt, usbl->tx_eth_hdr, sizeof(usbl->tx_eth_hdr));
  60. return layr->dn->transmit(layr->dn, pkt);
  61. }
  62. static void cfusbl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
  63. int phyid)
  64. {
  65. if (layr->up && layr->up->ctrlcmd)
  66. layr->up->ctrlcmd(layr->up, ctrl, layr->id);
  67. }
  68. static struct cflayer *cfusbl_create(int phyid, const u8 ethaddr[ETH_ALEN],
  69. u8 braddr[ETH_ALEN])
  70. {
  71. struct cfusbl *this = kmalloc(sizeof(struct cfusbl), GFP_ATOMIC);
  72. if (!this)
  73. return NULL;
  74. caif_assert(offsetof(struct cfusbl, layer) == 0);
  75. memset(&this->layer, 0, sizeof(this->layer));
  76. this->layer.receive = cfusbl_receive;
  77. this->layer.transmit = cfusbl_transmit;
  78. this->layer.ctrlcmd = cfusbl_ctrlcmd;
  79. snprintf(this->layer.name, CAIF_LAYER_NAME_SZ, "usb%d", phyid);
  80. this->layer.id = phyid;
  81. /*
  82. * Construct TX ethernet header:
  83. * 0-5 destination address
  84. * 5-11 source address
  85. * 12-13 protocol type
  86. */
  87. ether_addr_copy(&this->tx_eth_hdr[ETH_ALEN], braddr);
  88. ether_addr_copy(&this->tx_eth_hdr[ETH_ALEN], ethaddr);
  89. this->tx_eth_hdr[12] = cpu_to_be16(ETH_P_802_EX1) & 0xff;
  90. this->tx_eth_hdr[13] = (cpu_to_be16(ETH_P_802_EX1) >> 8) & 0xff;
  91. pr_debug("caif ethernet TX-header dst:%pM src:%pM type:%02x%02x\n",
  92. this->tx_eth_hdr, this->tx_eth_hdr + ETH_ALEN,
  93. this->tx_eth_hdr[12], this->tx_eth_hdr[13]);
  94. return (struct cflayer *) this;
  95. }
  96. static void cfusbl_release(struct cflayer *layer)
  97. {
  98. kfree(layer);
  99. }
  100. static struct packet_type caif_usb_type __read_mostly = {
  101. .type = cpu_to_be16(ETH_P_802_EX1),
  102. };
  103. static int cfusbl_device_notify(struct notifier_block *me, unsigned long what,
  104. void *ptr)
  105. {
  106. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  107. struct caif_dev_common common;
  108. struct cflayer *layer, *link_support;
  109. struct usbnet *usbnet;
  110. struct usb_device *usbdev;
  111. int res;
  112. if (what == NETDEV_UNREGISTER && dev->reg_state >= NETREG_UNREGISTERED)
  113. return 0;
  114. /* Check whether we have a NCM device, and find its VID/PID. */
  115. if (!(dev->dev.parent && dev->dev.parent->driver &&
  116. strcmp(dev->dev.parent->driver->name, "cdc_ncm") == 0))
  117. return 0;
  118. usbnet = netdev_priv(dev);
  119. usbdev = usbnet->udev;
  120. pr_debug("USB CDC NCM device VID:0x%4x PID:0x%4x\n",
  121. le16_to_cpu(usbdev->descriptor.idVendor),
  122. le16_to_cpu(usbdev->descriptor.idProduct));
  123. /* Check for VID/PID that supports CAIF */
  124. if (!(le16_to_cpu(usbdev->descriptor.idVendor) == STE_USB_VID &&
  125. le16_to_cpu(usbdev->descriptor.idProduct) == STE_USB_PID_CAIF))
  126. return 0;
  127. if (what == NETDEV_UNREGISTER)
  128. module_put(THIS_MODULE);
  129. if (what != NETDEV_REGISTER)
  130. return 0;
  131. __module_get(THIS_MODULE);
  132. memset(&common, 0, sizeof(common));
  133. common.use_frag = false;
  134. common.use_fcs = false;
  135. common.use_stx = false;
  136. common.link_select = CAIF_LINK_HIGH_BANDW;
  137. common.flowctrl = NULL;
  138. link_support = cfusbl_create(dev->ifindex, dev->dev_addr,
  139. dev->broadcast);
  140. if (!link_support)
  141. return -ENOMEM;
  142. if (dev->num_tx_queues > 1)
  143. pr_warn("USB device uses more than one tx queue\n");
  144. res = caif_enroll_dev(dev, &common, link_support, CFUSB_MAX_HEADLEN,
  145. &layer, &caif_usb_type.func);
  146. if (res)
  147. goto err;
  148. if (!pack_added)
  149. dev_add_pack(&caif_usb_type);
  150. pack_added = true;
  151. strscpy(layer->name, dev->name, sizeof(layer->name));
  152. return 0;
  153. err:
  154. cfusbl_release(link_support);
  155. return res;
  156. }
  157. static struct notifier_block caif_device_notifier = {
  158. .notifier_call = cfusbl_device_notify,
  159. .priority = 0,
  160. };
  161. static int __init cfusbl_init(void)
  162. {
  163. return register_netdevice_notifier(&caif_device_notifier);
  164. }
  165. static void __exit cfusbl_exit(void)
  166. {
  167. unregister_netdevice_notifier(&caif_device_notifier);
  168. dev_remove_pack(&caif_usb_type);
  169. }
  170. module_init(cfusbl_init);
  171. module_exit(cfusbl_exit);