0001
0002
0003
0004
0005
0006
0007
0008 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
0009
0010 #include <linux/module.h>
0011 #include <linux/netdevice.h>
0012 #include <linux/slab.h>
0013 #include <linux/mii.h>
0014 #include <linux/usb.h>
0015 #include <linux/usb/usbnet.h>
0016 #include <linux/etherdevice.h>
0017 #include <net/netns/generic.h>
0018 #include <net/caif/caif_dev.h>
0019 #include <net/caif/caif_layer.h>
0020 #include <net/caif/cfpkt.h>
0021 #include <net/caif/cfcnfg.h>
0022
0023 MODULE_LICENSE("GPL");
0024
0025 #define CFUSB_PAD_DESCR_SZ 1
0026 #define CFUSB_ALIGNMENT 4
0027 #define CFUSB_MAX_HEADLEN (CFUSB_PAD_DESCR_SZ + CFUSB_ALIGNMENT-1)
0028 #define STE_USB_VID 0x04cc
0029 #define STE_USB_PID_CAIF 0x230f
0030
0031 struct cfusbl {
0032 struct cflayer layer;
0033 u8 tx_eth_hdr[ETH_HLEN];
0034 };
0035
0036 static bool pack_added;
0037
0038 static int cfusbl_receive(struct cflayer *layr, struct cfpkt *pkt)
0039 {
0040 u8 hpad;
0041
0042
0043 cfpkt_extr_head(pkt, &hpad, 1);
0044 cfpkt_extr_head(pkt, NULL, hpad);
0045 return layr->up->receive(layr->up, pkt);
0046 }
0047
0048 static int cfusbl_transmit(struct cflayer *layr, struct cfpkt *pkt)
0049 {
0050 struct caif_payload_info *info;
0051 u8 hpad;
0052 u8 zeros[CFUSB_ALIGNMENT];
0053 struct sk_buff *skb;
0054 struct cfusbl *usbl = container_of(layr, struct cfusbl, layer);
0055
0056 skb = cfpkt_tonative(pkt);
0057
0058 skb_reset_network_header(skb);
0059 skb->protocol = htons(ETH_P_IP);
0060
0061 info = cfpkt_info(pkt);
0062 hpad = (info->hdr_len + CFUSB_PAD_DESCR_SZ) & (CFUSB_ALIGNMENT - 1);
0063
0064 if (skb_headroom(skb) < ETH_HLEN + CFUSB_PAD_DESCR_SZ + hpad) {
0065 pr_warn("Headroom too small\n");
0066 kfree_skb(skb);
0067 return -EIO;
0068 }
0069 memset(zeros, 0, hpad);
0070
0071 cfpkt_add_head(pkt, zeros, hpad);
0072 cfpkt_add_head(pkt, &hpad, 1);
0073 cfpkt_add_head(pkt, usbl->tx_eth_hdr, sizeof(usbl->tx_eth_hdr));
0074 return layr->dn->transmit(layr->dn, pkt);
0075 }
0076
0077 static void cfusbl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
0078 int phyid)
0079 {
0080 if (layr->up && layr->up->ctrlcmd)
0081 layr->up->ctrlcmd(layr->up, ctrl, layr->id);
0082 }
0083
0084 static struct cflayer *cfusbl_create(int phyid, const u8 ethaddr[ETH_ALEN],
0085 u8 braddr[ETH_ALEN])
0086 {
0087 struct cfusbl *this = kmalloc(sizeof(struct cfusbl), GFP_ATOMIC);
0088
0089 if (!this)
0090 return NULL;
0091
0092 caif_assert(offsetof(struct cfusbl, layer) == 0);
0093
0094 memset(&this->layer, 0, sizeof(this->layer));
0095 this->layer.receive = cfusbl_receive;
0096 this->layer.transmit = cfusbl_transmit;
0097 this->layer.ctrlcmd = cfusbl_ctrlcmd;
0098 snprintf(this->layer.name, CAIF_LAYER_NAME_SZ, "usb%d", phyid);
0099 this->layer.id = phyid;
0100
0101
0102
0103
0104
0105
0106
0107 ether_addr_copy(&this->tx_eth_hdr[ETH_ALEN], braddr);
0108 ether_addr_copy(&this->tx_eth_hdr[ETH_ALEN], ethaddr);
0109 this->tx_eth_hdr[12] = cpu_to_be16(ETH_P_802_EX1) & 0xff;
0110 this->tx_eth_hdr[13] = (cpu_to_be16(ETH_P_802_EX1) >> 8) & 0xff;
0111 pr_debug("caif ethernet TX-header dst:%pM src:%pM type:%02x%02x\n",
0112 this->tx_eth_hdr, this->tx_eth_hdr + ETH_ALEN,
0113 this->tx_eth_hdr[12], this->tx_eth_hdr[13]);
0114
0115 return (struct cflayer *) this;
0116 }
0117
0118 static void cfusbl_release(struct cflayer *layer)
0119 {
0120 kfree(layer);
0121 }
0122
0123 static struct packet_type caif_usb_type __read_mostly = {
0124 .type = cpu_to_be16(ETH_P_802_EX1),
0125 };
0126
0127 static int cfusbl_device_notify(struct notifier_block *me, unsigned long what,
0128 void *ptr)
0129 {
0130 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
0131 struct caif_dev_common common;
0132 struct cflayer *layer, *link_support;
0133 struct usbnet *usbnet;
0134 struct usb_device *usbdev;
0135 int res;
0136
0137
0138 if (!(dev->dev.parent && dev->dev.parent->driver &&
0139 strcmp(dev->dev.parent->driver->name, "cdc_ncm") == 0))
0140 return 0;
0141
0142 usbnet = netdev_priv(dev);
0143 usbdev = usbnet->udev;
0144
0145 pr_debug("USB CDC NCM device VID:0x%4x PID:0x%4x\n",
0146 le16_to_cpu(usbdev->descriptor.idVendor),
0147 le16_to_cpu(usbdev->descriptor.idProduct));
0148
0149
0150 if (!(le16_to_cpu(usbdev->descriptor.idVendor) == STE_USB_VID &&
0151 le16_to_cpu(usbdev->descriptor.idProduct) == STE_USB_PID_CAIF))
0152 return 0;
0153
0154 if (what == NETDEV_UNREGISTER)
0155 module_put(THIS_MODULE);
0156
0157 if (what != NETDEV_REGISTER)
0158 return 0;
0159
0160 __module_get(THIS_MODULE);
0161
0162 memset(&common, 0, sizeof(common));
0163 common.use_frag = false;
0164 common.use_fcs = false;
0165 common.use_stx = false;
0166 common.link_select = CAIF_LINK_HIGH_BANDW;
0167 common.flowctrl = NULL;
0168
0169 link_support = cfusbl_create(dev->ifindex, dev->dev_addr,
0170 dev->broadcast);
0171
0172 if (!link_support)
0173 return -ENOMEM;
0174
0175 if (dev->num_tx_queues > 1)
0176 pr_warn("USB device uses more than one tx queue\n");
0177
0178 res = caif_enroll_dev(dev, &common, link_support, CFUSB_MAX_HEADLEN,
0179 &layer, &caif_usb_type.func);
0180 if (res)
0181 goto err;
0182
0183 if (!pack_added)
0184 dev_add_pack(&caif_usb_type);
0185 pack_added = true;
0186
0187 strlcpy(layer->name, dev->name, sizeof(layer->name));
0188
0189 return 0;
0190 err:
0191 cfusbl_release(link_support);
0192 return res;
0193 }
0194
0195 static struct notifier_block caif_device_notifier = {
0196 .notifier_call = cfusbl_device_notify,
0197 .priority = 0,
0198 };
0199
0200 static int __init cfusbl_init(void)
0201 {
0202 return register_netdevice_notifier(&caif_device_notifier);
0203 }
0204
0205 static void __exit cfusbl_exit(void)
0206 {
0207 unregister_netdevice_notifier(&caif_device_notifier);
0208 dev_remove_pack(&caif_usb_type);
0209 }
0210
0211 module_init(cfusbl_init);
0212 module_exit(cfusbl_exit);