Thomas Gleixner | af873fc | 2019-05-28 09:57:21 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 2 | /* |
| 3 | * CAIF USB handler |
| 4 | * Copyright (C) ST-Ericsson AB 2011 |
sjur.brandeland@stericsson.com | 26ee65e | 2013-04-22 23:57:01 +0000 | [diff] [blame] | 5 | * Author: Sjur Brendeland |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/netdevice.h> |
| 12 | #include <linux/slab.h> |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 13 | #include <linux/mii.h> |
| 14 | #include <linux/usb.h> |
| 15 | #include <linux/usb/usbnet.h> |
David S. Miller | 14e4814 | 2014-01-21 22:54:01 -0800 | [diff] [blame] | 16 | #include <linux/etherdevice.h> |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 17 | #include <net/netns/generic.h> |
| 18 | #include <net/caif/caif_dev.h> |
| 19 | #include <net/caif/caif_layer.h> |
| 20 | #include <net/caif/cfpkt.h> |
| 21 | #include <net/caif/cfcnfg.h> |
| 22 | |
| 23 | MODULE_LICENSE("GPL"); |
| 24 | |
| 25 | #define CFUSB_PAD_DESCR_SZ 1 /* Alignment descriptor length */ |
| 26 | #define CFUSB_ALIGNMENT 4 /* Number of bytes to align. */ |
| 27 | #define CFUSB_MAX_HEADLEN (CFUSB_PAD_DESCR_SZ + CFUSB_ALIGNMENT-1) |
| 28 | #define STE_USB_VID 0x04cc /* USB Product ID for ST-Ericsson */ |
sjur.brandeland@stericsson.com | 3371bb3 | 2012-01-17 03:03:13 +0000 | [diff] [blame] | 29 | #define STE_USB_PID_CAIF 0x230f /* Product id for CAIF Modems */ |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 30 | |
| 31 | struct cfusbl { |
| 32 | struct cflayer layer; |
| 33 | u8 tx_eth_hdr[ETH_HLEN]; |
| 34 | }; |
| 35 | |
| 36 | static bool pack_added; |
| 37 | |
| 38 | static int cfusbl_receive(struct cflayer *layr, struct cfpkt *pkt) |
| 39 | { |
| 40 | u8 hpad; |
| 41 | |
| 42 | /* Remove padding. */ |
| 43 | cfpkt_extr_head(pkt, &hpad, 1); |
| 44 | cfpkt_extr_head(pkt, NULL, hpad); |
| 45 | return layr->up->receive(layr->up, pkt); |
| 46 | } |
| 47 | |
| 48 | static int cfusbl_transmit(struct cflayer *layr, struct cfpkt *pkt) |
| 49 | { |
| 50 | struct caif_payload_info *info; |
| 51 | u8 hpad; |
| 52 | u8 zeros[CFUSB_ALIGNMENT]; |
| 53 | struct sk_buff *skb; |
| 54 | struct cfusbl *usbl = container_of(layr, struct cfusbl, layer); |
| 55 | |
| 56 | skb = cfpkt_tonative(pkt); |
| 57 | |
| 58 | skb_reset_network_header(skb); |
| 59 | skb->protocol = htons(ETH_P_IP); |
| 60 | |
| 61 | info = cfpkt_info(pkt); |
| 62 | hpad = (info->hdr_len + CFUSB_PAD_DESCR_SZ) & (CFUSB_ALIGNMENT - 1); |
| 63 | |
| 64 | if (skb_headroom(skb) < ETH_HLEN + CFUSB_PAD_DESCR_SZ + hpad) { |
Colin Ian King | 43d8877 | 2020-01-23 00:45:55 +0000 | [diff] [blame] | 65 | pr_warn("Headroom too small\n"); |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 66 | kfree_skb(skb); |
| 67 | return -EIO; |
| 68 | } |
| 69 | memset(zeros, 0, hpad); |
| 70 | |
| 71 | cfpkt_add_head(pkt, zeros, hpad); |
| 72 | cfpkt_add_head(pkt, &hpad, 1); |
| 73 | cfpkt_add_head(pkt, usbl->tx_eth_hdr, sizeof(usbl->tx_eth_hdr)); |
| 74 | return layr->dn->transmit(layr->dn, pkt); |
| 75 | } |
| 76 | |
| 77 | static void cfusbl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl, |
Silviu-Mihai Popescu | 3bffc47 | 2013-03-06 19:39:57 +0000 | [diff] [blame] | 78 | int phyid) |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 79 | { |
| 80 | if (layr->up && layr->up->ctrlcmd) |
| 81 | layr->up->ctrlcmd(layr->up, ctrl, layr->id); |
| 82 | } |
| 83 | |
Jakub Kicinski | 5520fb4 | 2021-10-22 16:21:01 -0700 | [diff] [blame] | 84 | static struct cflayer *cfusbl_create(int phyid, const u8 ethaddr[ETH_ALEN], |
Silviu-Mihai Popescu | d2123be | 2013-03-03 21:09:31 +0000 | [diff] [blame] | 85 | u8 braddr[ETH_ALEN]) |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 86 | { |
| 87 | struct cfusbl *this = kmalloc(sizeof(struct cfusbl), GFP_ATOMIC); |
| 88 | |
Fabian Frederick | 7970f19 | 2014-10-14 19:00:55 +0200 | [diff] [blame] | 89 | if (!this) |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 90 | return NULL; |
Fabian Frederick | 7970f19 | 2014-10-14 19:00:55 +0200 | [diff] [blame] | 91 | |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 92 | caif_assert(offsetof(struct cfusbl, layer) == 0); |
| 93 | |
Fabian Frederick | 91c4467 | 2014-10-14 19:01:14 +0200 | [diff] [blame] | 94 | memset(&this->layer, 0, sizeof(this->layer)); |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 95 | this->layer.receive = cfusbl_receive; |
| 96 | this->layer.transmit = cfusbl_transmit; |
| 97 | this->layer.ctrlcmd = cfusbl_ctrlcmd; |
| 98 | snprintf(this->layer.name, CAIF_LAYER_NAME_SZ, "usb%d", phyid); |
| 99 | this->layer.id = phyid; |
| 100 | |
| 101 | /* |
| 102 | * Construct TX ethernet header: |
| 103 | * 0-5 destination address |
| 104 | * 5-11 source address |
| 105 | * 12-13 protocol type |
| 106 | */ |
Joe Perches | 34b2cff | 2014-01-20 09:52:17 -0800 | [diff] [blame] | 107 | ether_addr_copy(&this->tx_eth_hdr[ETH_ALEN], braddr); |
| 108 | ether_addr_copy(&this->tx_eth_hdr[ETH_ALEN], ethaddr); |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 109 | this->tx_eth_hdr[12] = cpu_to_be16(ETH_P_802_EX1) & 0xff; |
| 110 | this->tx_eth_hdr[13] = (cpu_to_be16(ETH_P_802_EX1) >> 8) & 0xff; |
| 111 | pr_debug("caif ethernet TX-header dst:%pM src:%pM type:%02x%02x\n", |
| 112 | this->tx_eth_hdr, this->tx_eth_hdr + ETH_ALEN, |
| 113 | this->tx_eth_hdr[12], this->tx_eth_hdr[13]); |
| 114 | |
| 115 | return (struct cflayer *) this; |
| 116 | } |
| 117 | |
Pavel Skripkin | 7f5d866 | 2021-06-03 19:39:35 +0300 | [diff] [blame] | 118 | static void cfusbl_release(struct cflayer *layer) |
| 119 | { |
| 120 | kfree(layer); |
| 121 | } |
| 122 | |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 123 | static struct packet_type caif_usb_type __read_mostly = { |
| 124 | .type = cpu_to_be16(ETH_P_802_EX1), |
| 125 | }; |
| 126 | |
| 127 | static int cfusbl_device_notify(struct notifier_block *me, unsigned long what, |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 128 | void *ptr) |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 129 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 130 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 131 | struct caif_dev_common common; |
| 132 | struct cflayer *layer, *link_support; |
Ben Hutchings | 40663634 | 2012-12-07 06:17:26 +0000 | [diff] [blame] | 133 | struct usbnet *usbnet; |
| 134 | struct usb_device *usbdev; |
Pavel Skripkin | 7f5d866 | 2021-06-03 19:39:35 +0300 | [diff] [blame] | 135 | int res; |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 136 | |
Ben Hutchings | 65d2897 | 2012-12-07 06:20:27 +0000 | [diff] [blame] | 137 | /* Check whether we have a NCM device, and find its VID/PID. */ |
| 138 | if (!(dev->dev.parent && dev->dev.parent->driver && |
| 139 | strcmp(dev->dev.parent->driver->name, "cdc_ncm") == 0)) |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 140 | return 0; |
| 141 | |
Ben Hutchings | 40663634 | 2012-12-07 06:17:26 +0000 | [diff] [blame] | 142 | usbnet = netdev_priv(dev); |
| 143 | usbdev = usbnet->udev; |
| 144 | |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 145 | pr_debug("USB CDC NCM device VID:0x%4x PID:0x%4x\n", |
| 146 | le16_to_cpu(usbdev->descriptor.idVendor), |
| 147 | le16_to_cpu(usbdev->descriptor.idProduct)); |
| 148 | |
| 149 | /* Check for VID/PID that supports CAIF */ |
| 150 | if (!(le16_to_cpu(usbdev->descriptor.idVendor) == STE_USB_VID && |
| 151 | le16_to_cpu(usbdev->descriptor.idProduct) == STE_USB_PID_CAIF)) |
| 152 | return 0; |
| 153 | |
| 154 | if (what == NETDEV_UNREGISTER) |
| 155 | module_put(THIS_MODULE); |
| 156 | |
| 157 | if (what != NETDEV_REGISTER) |
| 158 | return 0; |
| 159 | |
| 160 | __module_get(THIS_MODULE); |
| 161 | |
| 162 | memset(&common, 0, sizeof(common)); |
| 163 | common.use_frag = false; |
| 164 | common.use_fcs = false; |
| 165 | common.use_stx = false; |
| 166 | common.link_select = CAIF_LINK_HIGH_BANDW; |
| 167 | common.flowctrl = NULL; |
| 168 | |
| 169 | link_support = cfusbl_create(dev->ifindex, dev->dev_addr, |
| 170 | dev->broadcast); |
| 171 | |
| 172 | if (!link_support) |
| 173 | return -ENOMEM; |
| 174 | |
| 175 | if (dev->num_tx_queues > 1) |
| 176 | pr_warn("USB device uses more than one tx queue\n"); |
| 177 | |
Pavel Skripkin | 7f5d866 | 2021-06-03 19:39:35 +0300 | [diff] [blame] | 178 | res = caif_enroll_dev(dev, &common, link_support, CFUSB_MAX_HEADLEN, |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 179 | &layer, &caif_usb_type.func); |
Pavel Skripkin | 7f5d866 | 2021-06-03 19:39:35 +0300 | [diff] [blame] | 180 | if (res) |
| 181 | goto err; |
| 182 | |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 183 | if (!pack_added) |
| 184 | dev_add_pack(&caif_usb_type); |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 185 | pack_added = true; |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 186 | |
Xiongfeng Wang | b0d55b5 | 2018-01-09 19:58:18 +0800 | [diff] [blame] | 187 | strlcpy(layer->name, dev->name, sizeof(layer->name)); |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 188 | |
| 189 | return 0; |
Pavel Skripkin | 7f5d866 | 2021-06-03 19:39:35 +0300 | [diff] [blame] | 190 | err: |
| 191 | cfusbl_release(link_support); |
| 192 | return res; |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static struct notifier_block caif_device_notifier = { |
| 196 | .notifier_call = cfusbl_device_notify, |
| 197 | .priority = 0, |
| 198 | }; |
| 199 | |
| 200 | static int __init cfusbl_init(void) |
| 201 | { |
| 202 | return register_netdevice_notifier(&caif_device_notifier); |
| 203 | } |
| 204 | |
| 205 | static void __exit cfusbl_exit(void) |
| 206 | { |
| 207 | unregister_netdevice_notifier(&caif_device_notifier); |
| 208 | dev_remove_pack(&caif_usb_type); |
| 209 | } |
| 210 | |
| 211 | module_init(cfusbl_init); |
| 212 | module_exit(cfusbl_exit); |