blob: 5cd44f001f6479a5e5ec9d02cfc417b899b05e9d [file] [log] [blame]
sjur.brandeland@stericsson.com7ad65bf2011-12-04 11:22:53 +00001/*
2 * CAIF USB handler
3 * Copyright (C) ST-Ericsson AB 2011
sjur.brandeland@stericsson.com26ee65e2013-04-22 23:57:01 +00004 * Author: Sjur Brendeland
sjur.brandeland@stericsson.com7ad65bf2011-12-04 11:22:53 +00005 * License terms: GNU General Public License (GPL) version 2
6 *
7 */
8
9#define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
10
11#include <linux/module.h>
12#include <linux/netdevice.h>
13#include <linux/slab.h>
sjur.brandeland@stericsson.com7ad65bf2011-12-04 11:22:53 +000014#include <linux/mii.h>
15#include <linux/usb.h>
16#include <linux/usb/usbnet.h>
David S. Miller14e48142014-01-21 22:54:01 -080017#include <linux/etherdevice.h>
sjur.brandeland@stericsson.com7ad65bf2011-12-04 11:22:53 +000018#include <net/netns/generic.h>
19#include <net/caif/caif_dev.h>
20#include <net/caif/caif_layer.h>
21#include <net/caif/cfpkt.h>
22#include <net/caif/cfcnfg.h>
23
24MODULE_LICENSE("GPL");
25
26#define CFUSB_PAD_DESCR_SZ 1 /* Alignment descriptor length */
27#define CFUSB_ALIGNMENT 4 /* Number of bytes to align. */
28#define CFUSB_MAX_HEADLEN (CFUSB_PAD_DESCR_SZ + CFUSB_ALIGNMENT-1)
29#define STE_USB_VID 0x04cc /* USB Product ID for ST-Ericsson */
sjur.brandeland@stericsson.com3371bb32012-01-17 03:03:13 +000030#define STE_USB_PID_CAIF 0x230f /* Product id for CAIF Modems */
sjur.brandeland@stericsson.com7ad65bf2011-12-04 11:22:53 +000031
32struct cfusbl {
33 struct cflayer layer;
34 u8 tx_eth_hdr[ETH_HLEN];
35};
36
37static bool pack_added;
38
39static int cfusbl_receive(struct cflayer *layr, struct cfpkt *pkt)
40{
41 u8 hpad;
42
43 /* Remove padding. */
44 cfpkt_extr_head(pkt, &hpad, 1);
45 cfpkt_extr_head(pkt, NULL, hpad);
46 return layr->up->receive(layr->up, pkt);
47}
48
49static int cfusbl_transmit(struct cflayer *layr, struct cfpkt *pkt)
50{
51 struct caif_payload_info *info;
52 u8 hpad;
53 u8 zeros[CFUSB_ALIGNMENT];
54 struct sk_buff *skb;
55 struct cfusbl *usbl = container_of(layr, struct cfusbl, layer);
56
57 skb = cfpkt_tonative(pkt);
58
59 skb_reset_network_header(skb);
60 skb->protocol = htons(ETH_P_IP);
61
62 info = cfpkt_info(pkt);
63 hpad = (info->hdr_len + CFUSB_PAD_DESCR_SZ) & (CFUSB_ALIGNMENT - 1);
64
65 if (skb_headroom(skb) < ETH_HLEN + CFUSB_PAD_DESCR_SZ + hpad) {
66 pr_warn("Headroom to small\n");
67 kfree_skb(skb);
68 return -EIO;
69 }
70 memset(zeros, 0, hpad);
71
72 cfpkt_add_head(pkt, zeros, hpad);
73 cfpkt_add_head(pkt, &hpad, 1);
74 cfpkt_add_head(pkt, usbl->tx_eth_hdr, sizeof(usbl->tx_eth_hdr));
75 return layr->dn->transmit(layr->dn, pkt);
76}
77
78static void cfusbl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +000079 int phyid)
sjur.brandeland@stericsson.com7ad65bf2011-12-04 11:22:53 +000080{
81 if (layr->up && layr->up->ctrlcmd)
82 layr->up->ctrlcmd(layr->up, ctrl, layr->id);
83}
84
Silviu-Mihai Popescud2123be2013-03-03 21:09:31 +000085static struct cflayer *cfusbl_create(int phyid, u8 ethaddr[ETH_ALEN],
86 u8 braddr[ETH_ALEN])
sjur.brandeland@stericsson.com7ad65bf2011-12-04 11:22:53 +000087{
88 struct cfusbl *this = kmalloc(sizeof(struct cfusbl), GFP_ATOMIC);
89
Fabian Frederick7970f192014-10-14 19:00:55 +020090 if (!this)
sjur.brandeland@stericsson.com7ad65bf2011-12-04 11:22:53 +000091 return NULL;
Fabian Frederick7970f192014-10-14 19:00:55 +020092
sjur.brandeland@stericsson.com7ad65bf2011-12-04 11:22:53 +000093 caif_assert(offsetof(struct cfusbl, layer) == 0);
94
Fabian Frederick91c44672014-10-14 19:01:14 +020095 memset(&this->layer, 0, sizeof(this->layer));
sjur.brandeland@stericsson.com7ad65bf2011-12-04 11:22:53 +000096 this->layer.receive = cfusbl_receive;
97 this->layer.transmit = cfusbl_transmit;
98 this->layer.ctrlcmd = cfusbl_ctrlcmd;
99 snprintf(this->layer.name, CAIF_LAYER_NAME_SZ, "usb%d", phyid);
100 this->layer.id = phyid;
101
102 /*
103 * Construct TX ethernet header:
104 * 0-5 destination address
105 * 5-11 source address
106 * 12-13 protocol type
107 */
Joe Perches34b2cff2014-01-20 09:52:17 -0800108 ether_addr_copy(&this->tx_eth_hdr[ETH_ALEN], braddr);
109 ether_addr_copy(&this->tx_eth_hdr[ETH_ALEN], ethaddr);
sjur.brandeland@stericsson.com7ad65bf2011-12-04 11:22:53 +0000110 this->tx_eth_hdr[12] = cpu_to_be16(ETH_P_802_EX1) & 0xff;
111 this->tx_eth_hdr[13] = (cpu_to_be16(ETH_P_802_EX1) >> 8) & 0xff;
112 pr_debug("caif ethernet TX-header dst:%pM src:%pM type:%02x%02x\n",
113 this->tx_eth_hdr, this->tx_eth_hdr + ETH_ALEN,
114 this->tx_eth_hdr[12], this->tx_eth_hdr[13]);
115
116 return (struct cflayer *) this;
117}
118
119static struct packet_type caif_usb_type __read_mostly = {
120 .type = cpu_to_be16(ETH_P_802_EX1),
121};
122
123static int cfusbl_device_notify(struct notifier_block *me, unsigned long what,
Jiri Pirko351638e2013-05-28 01:30:21 +0000124 void *ptr)
sjur.brandeland@stericsson.com7ad65bf2011-12-04 11:22:53 +0000125{
Jiri Pirko351638e2013-05-28 01:30:21 +0000126 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
sjur.brandeland@stericsson.com7ad65bf2011-12-04 11:22:53 +0000127 struct caif_dev_common common;
128 struct cflayer *layer, *link_support;
Ben Hutchings40663632012-12-07 06:17:26 +0000129 struct usbnet *usbnet;
130 struct usb_device *usbdev;
sjur.brandeland@stericsson.com7ad65bf2011-12-04 11:22:53 +0000131
Ben Hutchings65d28972012-12-07 06:20:27 +0000132 /* Check whether we have a NCM device, and find its VID/PID. */
133 if (!(dev->dev.parent && dev->dev.parent->driver &&
134 strcmp(dev->dev.parent->driver->name, "cdc_ncm") == 0))
sjur.brandeland@stericsson.com7ad65bf2011-12-04 11:22:53 +0000135 return 0;
136
Ben Hutchings40663632012-12-07 06:17:26 +0000137 usbnet = netdev_priv(dev);
138 usbdev = usbnet->udev;
139
sjur.brandeland@stericsson.com7ad65bf2011-12-04 11:22:53 +0000140 pr_debug("USB CDC NCM device VID:0x%4x PID:0x%4x\n",
141 le16_to_cpu(usbdev->descriptor.idVendor),
142 le16_to_cpu(usbdev->descriptor.idProduct));
143
144 /* Check for VID/PID that supports CAIF */
145 if (!(le16_to_cpu(usbdev->descriptor.idVendor) == STE_USB_VID &&
146 le16_to_cpu(usbdev->descriptor.idProduct) == STE_USB_PID_CAIF))
147 return 0;
148
149 if (what == NETDEV_UNREGISTER)
150 module_put(THIS_MODULE);
151
152 if (what != NETDEV_REGISTER)
153 return 0;
154
155 __module_get(THIS_MODULE);
156
157 memset(&common, 0, sizeof(common));
158 common.use_frag = false;
159 common.use_fcs = false;
160 common.use_stx = false;
161 common.link_select = CAIF_LINK_HIGH_BANDW;
162 common.flowctrl = NULL;
163
164 link_support = cfusbl_create(dev->ifindex, dev->dev_addr,
165 dev->broadcast);
166
167 if (!link_support)
168 return -ENOMEM;
169
170 if (dev->num_tx_queues > 1)
171 pr_warn("USB device uses more than one tx queue\n");
172
173 caif_enroll_dev(dev, &common, link_support, CFUSB_MAX_HEADLEN,
174 &layer, &caif_usb_type.func);
175 if (!pack_added)
176 dev_add_pack(&caif_usb_type);
Rusty Russell3db1cd52011-12-19 13:56:45 +0000177 pack_added = true;
sjur.brandeland@stericsson.com7ad65bf2011-12-04 11:22:53 +0000178
179 strncpy(layer->name, dev->name,
180 sizeof(layer->name) - 1);
181 layer->name[sizeof(layer->name) - 1] = 0;
182
183 return 0;
184}
185
186static struct notifier_block caif_device_notifier = {
187 .notifier_call = cfusbl_device_notify,
188 .priority = 0,
189};
190
191static int __init cfusbl_init(void)
192{
193 return register_netdevice_notifier(&caif_device_notifier);
194}
195
196static void __exit cfusbl_exit(void)
197{
198 unregister_netdevice_notifier(&caif_device_notifier);
199 dev_remove_pack(&caif_usb_type);
200}
201
202module_init(cfusbl_init);
203module_exit(cfusbl_exit);