xref: /openbmc/linux/net/caif/caif_usb.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1af873fceSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27ad65bf6Ssjur.brandeland@stericsson.com /*
37ad65bf6Ssjur.brandeland@stericsson.com  * CAIF USB handler
47ad65bf6Ssjur.brandeland@stericsson.com  * Copyright (C) ST-Ericsson AB 2011
526ee65e6Ssjur.brandeland@stericsson.com  * Author:	Sjur Brendeland
67ad65bf6Ssjur.brandeland@stericsson.com  */
77ad65bf6Ssjur.brandeland@stericsson.com 
87ad65bf6Ssjur.brandeland@stericsson.com #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
97ad65bf6Ssjur.brandeland@stericsson.com 
107ad65bf6Ssjur.brandeland@stericsson.com #include <linux/module.h>
117ad65bf6Ssjur.brandeland@stericsson.com #include <linux/netdevice.h>
127ad65bf6Ssjur.brandeland@stericsson.com #include <linux/slab.h>
137ad65bf6Ssjur.brandeland@stericsson.com #include <linux/mii.h>
147ad65bf6Ssjur.brandeland@stericsson.com #include <linux/usb.h>
157ad65bf6Ssjur.brandeland@stericsson.com #include <linux/usb/usbnet.h>
1614e48144SDavid S. Miller #include <linux/etherdevice.h>
177ad65bf6Ssjur.brandeland@stericsson.com #include <net/netns/generic.h>
187ad65bf6Ssjur.brandeland@stericsson.com #include <net/caif/caif_dev.h>
197ad65bf6Ssjur.brandeland@stericsson.com #include <net/caif/caif_layer.h>
207ad65bf6Ssjur.brandeland@stericsson.com #include <net/caif/cfpkt.h>
217ad65bf6Ssjur.brandeland@stericsson.com #include <net/caif/cfcnfg.h>
227ad65bf6Ssjur.brandeland@stericsson.com 
237ad65bf6Ssjur.brandeland@stericsson.com MODULE_LICENSE("GPL");
247ad65bf6Ssjur.brandeland@stericsson.com 
257ad65bf6Ssjur.brandeland@stericsson.com #define CFUSB_PAD_DESCR_SZ 1	/* Alignment descriptor length */
267ad65bf6Ssjur.brandeland@stericsson.com #define CFUSB_ALIGNMENT 4	/* Number of bytes to align. */
277ad65bf6Ssjur.brandeland@stericsson.com #define CFUSB_MAX_HEADLEN (CFUSB_PAD_DESCR_SZ + CFUSB_ALIGNMENT-1)
287ad65bf6Ssjur.brandeland@stericsson.com #define STE_USB_VID 0x04cc	/* USB Product ID for ST-Ericsson */
293371bb3fSsjur.brandeland@stericsson.com #define STE_USB_PID_CAIF 0x230f	/* Product id for CAIF Modems */
307ad65bf6Ssjur.brandeland@stericsson.com 
317ad65bf6Ssjur.brandeland@stericsson.com struct cfusbl {
327ad65bf6Ssjur.brandeland@stericsson.com 	struct cflayer layer;
337ad65bf6Ssjur.brandeland@stericsson.com 	u8 tx_eth_hdr[ETH_HLEN];
347ad65bf6Ssjur.brandeland@stericsson.com };
357ad65bf6Ssjur.brandeland@stericsson.com 
367ad65bf6Ssjur.brandeland@stericsson.com static bool pack_added;
377ad65bf6Ssjur.brandeland@stericsson.com 
cfusbl_receive(struct cflayer * layr,struct cfpkt * pkt)387ad65bf6Ssjur.brandeland@stericsson.com static int cfusbl_receive(struct cflayer *layr, struct cfpkt *pkt)
397ad65bf6Ssjur.brandeland@stericsson.com {
407ad65bf6Ssjur.brandeland@stericsson.com 	u8 hpad;
417ad65bf6Ssjur.brandeland@stericsson.com 
427ad65bf6Ssjur.brandeland@stericsson.com 	/* Remove padding. */
437ad65bf6Ssjur.brandeland@stericsson.com 	cfpkt_extr_head(pkt, &hpad, 1);
447ad65bf6Ssjur.brandeland@stericsson.com 	cfpkt_extr_head(pkt, NULL, hpad);
457ad65bf6Ssjur.brandeland@stericsson.com 	return layr->up->receive(layr->up, pkt);
467ad65bf6Ssjur.brandeland@stericsson.com }
477ad65bf6Ssjur.brandeland@stericsson.com 
cfusbl_transmit(struct cflayer * layr,struct cfpkt * pkt)487ad65bf6Ssjur.brandeland@stericsson.com static int cfusbl_transmit(struct cflayer *layr, struct cfpkt *pkt)
497ad65bf6Ssjur.brandeland@stericsson.com {
507ad65bf6Ssjur.brandeland@stericsson.com 	struct caif_payload_info *info;
517ad65bf6Ssjur.brandeland@stericsson.com 	u8 hpad;
527ad65bf6Ssjur.brandeland@stericsson.com 	u8 zeros[CFUSB_ALIGNMENT];
537ad65bf6Ssjur.brandeland@stericsson.com 	struct sk_buff *skb;
547ad65bf6Ssjur.brandeland@stericsson.com 	struct cfusbl *usbl = container_of(layr, struct cfusbl, layer);
557ad65bf6Ssjur.brandeland@stericsson.com 
567ad65bf6Ssjur.brandeland@stericsson.com 	skb = cfpkt_tonative(pkt);
577ad65bf6Ssjur.brandeland@stericsson.com 
587ad65bf6Ssjur.brandeland@stericsson.com 	skb_reset_network_header(skb);
597ad65bf6Ssjur.brandeland@stericsson.com 	skb->protocol = htons(ETH_P_IP);
607ad65bf6Ssjur.brandeland@stericsson.com 
617ad65bf6Ssjur.brandeland@stericsson.com 	info = cfpkt_info(pkt);
627ad65bf6Ssjur.brandeland@stericsson.com 	hpad = (info->hdr_len + CFUSB_PAD_DESCR_SZ) & (CFUSB_ALIGNMENT - 1);
637ad65bf6Ssjur.brandeland@stericsson.com 
647ad65bf6Ssjur.brandeland@stericsson.com 	if (skb_headroom(skb) < ETH_HLEN + CFUSB_PAD_DESCR_SZ + hpad) {
6543d88774SColin Ian King 		pr_warn("Headroom too small\n");
667ad65bf6Ssjur.brandeland@stericsson.com 		kfree_skb(skb);
677ad65bf6Ssjur.brandeland@stericsson.com 		return -EIO;
687ad65bf6Ssjur.brandeland@stericsson.com 	}
697ad65bf6Ssjur.brandeland@stericsson.com 	memset(zeros, 0, hpad);
707ad65bf6Ssjur.brandeland@stericsson.com 
717ad65bf6Ssjur.brandeland@stericsson.com 	cfpkt_add_head(pkt, zeros, hpad);
727ad65bf6Ssjur.brandeland@stericsson.com 	cfpkt_add_head(pkt, &hpad, 1);
737ad65bf6Ssjur.brandeland@stericsson.com 	cfpkt_add_head(pkt, usbl->tx_eth_hdr, sizeof(usbl->tx_eth_hdr));
747ad65bf6Ssjur.brandeland@stericsson.com 	return layr->dn->transmit(layr->dn, pkt);
757ad65bf6Ssjur.brandeland@stericsson.com }
767ad65bf6Ssjur.brandeland@stericsson.com 
cfusbl_ctrlcmd(struct cflayer * layr,enum caif_ctrlcmd ctrl,int phyid)777ad65bf6Ssjur.brandeland@stericsson.com static void cfusbl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
787ad65bf6Ssjur.brandeland@stericsson.com 			   int phyid)
797ad65bf6Ssjur.brandeland@stericsson.com {
807ad65bf6Ssjur.brandeland@stericsson.com 	if (layr->up && layr->up->ctrlcmd)
817ad65bf6Ssjur.brandeland@stericsson.com 		layr->up->ctrlcmd(layr->up, ctrl, layr->id);
827ad65bf6Ssjur.brandeland@stericsson.com }
837ad65bf6Ssjur.brandeland@stericsson.com 
cfusbl_create(int phyid,const u8 ethaddr[ETH_ALEN],u8 braddr[ETH_ALEN])845520fb42SJakub Kicinski static struct cflayer *cfusbl_create(int phyid, const u8 ethaddr[ETH_ALEN],
857ad65bf6Ssjur.brandeland@stericsson.com 				      u8 braddr[ETH_ALEN])
867ad65bf6Ssjur.brandeland@stericsson.com {
877ad65bf6Ssjur.brandeland@stericsson.com 	struct cfusbl *this = kmalloc(sizeof(struct cfusbl), GFP_ATOMIC);
887ad65bf6Ssjur.brandeland@stericsson.com 
897970f191SFabian Frederick 	if (!this)
907ad65bf6Ssjur.brandeland@stericsson.com 		return NULL;
917970f191SFabian Frederick 
927ad65bf6Ssjur.brandeland@stericsson.com 	caif_assert(offsetof(struct cfusbl, layer) == 0);
937ad65bf6Ssjur.brandeland@stericsson.com 
9491c4467eSFabian Frederick 	memset(&this->layer, 0, sizeof(this->layer));
957ad65bf6Ssjur.brandeland@stericsson.com 	this->layer.receive = cfusbl_receive;
967ad65bf6Ssjur.brandeland@stericsson.com 	this->layer.transmit = cfusbl_transmit;
977ad65bf6Ssjur.brandeland@stericsson.com 	this->layer.ctrlcmd = cfusbl_ctrlcmd;
987ad65bf6Ssjur.brandeland@stericsson.com 	snprintf(this->layer.name, CAIF_LAYER_NAME_SZ, "usb%d", phyid);
997ad65bf6Ssjur.brandeland@stericsson.com 	this->layer.id = phyid;
1007ad65bf6Ssjur.brandeland@stericsson.com 
1017ad65bf6Ssjur.brandeland@stericsson.com 	/*
1027ad65bf6Ssjur.brandeland@stericsson.com 	 * Construct TX ethernet header:
1037ad65bf6Ssjur.brandeland@stericsson.com 	 *	0-5	destination address
1047ad65bf6Ssjur.brandeland@stericsson.com 	 *	5-11	source address
1057ad65bf6Ssjur.brandeland@stericsson.com 	 *	12-13	protocol type
1067ad65bf6Ssjur.brandeland@stericsson.com 	 */
10734b2cff4SJoe Perches 	ether_addr_copy(&this->tx_eth_hdr[ETH_ALEN], braddr);
10834b2cff4SJoe Perches 	ether_addr_copy(&this->tx_eth_hdr[ETH_ALEN], ethaddr);
1097ad65bf6Ssjur.brandeland@stericsson.com 	this->tx_eth_hdr[12] = cpu_to_be16(ETH_P_802_EX1) & 0xff;
1107ad65bf6Ssjur.brandeland@stericsson.com 	this->tx_eth_hdr[13] = (cpu_to_be16(ETH_P_802_EX1) >> 8) & 0xff;
1117ad65bf6Ssjur.brandeland@stericsson.com 	pr_debug("caif ethernet TX-header dst:%pM src:%pM type:%02x%02x\n",
1127ad65bf6Ssjur.brandeland@stericsson.com 			this->tx_eth_hdr, this->tx_eth_hdr + ETH_ALEN,
1137ad65bf6Ssjur.brandeland@stericsson.com 			this->tx_eth_hdr[12], this->tx_eth_hdr[13]);
1147ad65bf6Ssjur.brandeland@stericsson.com 
1157ad65bf6Ssjur.brandeland@stericsson.com 	return (struct cflayer *) this;
1167ad65bf6Ssjur.brandeland@stericsson.com }
1177ad65bf6Ssjur.brandeland@stericsson.com 
cfusbl_release(struct cflayer * layer)1187f5d8666SPavel Skripkin static void cfusbl_release(struct cflayer *layer)
1197f5d8666SPavel Skripkin {
1207f5d8666SPavel Skripkin 	kfree(layer);
1217f5d8666SPavel Skripkin }
1227f5d8666SPavel Skripkin 
1237ad65bf6Ssjur.brandeland@stericsson.com static struct packet_type caif_usb_type __read_mostly = {
1247ad65bf6Ssjur.brandeland@stericsson.com 	.type = cpu_to_be16(ETH_P_802_EX1),
1257ad65bf6Ssjur.brandeland@stericsson.com };
1267ad65bf6Ssjur.brandeland@stericsson.com 
cfusbl_device_notify(struct notifier_block * me,unsigned long what,void * ptr)1277ad65bf6Ssjur.brandeland@stericsson.com static int cfusbl_device_notify(struct notifier_block *me, unsigned long what,
128351638e7SJiri Pirko 				void *ptr)
1297ad65bf6Ssjur.brandeland@stericsson.com {
130351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1317ad65bf6Ssjur.brandeland@stericsson.com 	struct caif_dev_common common;
1327ad65bf6Ssjur.brandeland@stericsson.com 	struct cflayer *layer, *link_support;
13340663634SBen Hutchings 	struct usbnet *usbnet;
13440663634SBen Hutchings 	struct usb_device *usbdev;
1357f5d8666SPavel Skripkin 	int res;
1367ad65bf6Ssjur.brandeland@stericsson.com 
137*9781e98aSShigeru Yoshida 	if (what == NETDEV_UNREGISTER && dev->reg_state >= NETREG_UNREGISTERED)
138*9781e98aSShigeru Yoshida 		return 0;
139*9781e98aSShigeru Yoshida 
14065d2897cSBen Hutchings 	/* Check whether we have a NCM device, and find its VID/PID. */
14165d2897cSBen Hutchings 	if (!(dev->dev.parent && dev->dev.parent->driver &&
14265d2897cSBen Hutchings 	      strcmp(dev->dev.parent->driver->name, "cdc_ncm") == 0))
1437ad65bf6Ssjur.brandeland@stericsson.com 		return 0;
1447ad65bf6Ssjur.brandeland@stericsson.com 
14540663634SBen Hutchings 	usbnet = netdev_priv(dev);
14640663634SBen Hutchings 	usbdev = usbnet->udev;
14740663634SBen Hutchings 
1487ad65bf6Ssjur.brandeland@stericsson.com 	pr_debug("USB CDC NCM device VID:0x%4x PID:0x%4x\n",
1497ad65bf6Ssjur.brandeland@stericsson.com 		le16_to_cpu(usbdev->descriptor.idVendor),
1507ad65bf6Ssjur.brandeland@stericsson.com 		le16_to_cpu(usbdev->descriptor.idProduct));
1517ad65bf6Ssjur.brandeland@stericsson.com 
1527ad65bf6Ssjur.brandeland@stericsson.com 	/* Check for VID/PID that supports CAIF */
1537ad65bf6Ssjur.brandeland@stericsson.com 	if (!(le16_to_cpu(usbdev->descriptor.idVendor) == STE_USB_VID &&
1547ad65bf6Ssjur.brandeland@stericsson.com 		le16_to_cpu(usbdev->descriptor.idProduct) == STE_USB_PID_CAIF))
1557ad65bf6Ssjur.brandeland@stericsson.com 		return 0;
1567ad65bf6Ssjur.brandeland@stericsson.com 
1577ad65bf6Ssjur.brandeland@stericsson.com 	if (what == NETDEV_UNREGISTER)
1587ad65bf6Ssjur.brandeland@stericsson.com 		module_put(THIS_MODULE);
1597ad65bf6Ssjur.brandeland@stericsson.com 
1607ad65bf6Ssjur.brandeland@stericsson.com 	if (what != NETDEV_REGISTER)
1617ad65bf6Ssjur.brandeland@stericsson.com 		return 0;
1627ad65bf6Ssjur.brandeland@stericsson.com 
1637ad65bf6Ssjur.brandeland@stericsson.com 	__module_get(THIS_MODULE);
1647ad65bf6Ssjur.brandeland@stericsson.com 
1657ad65bf6Ssjur.brandeland@stericsson.com 	memset(&common, 0, sizeof(common));
1667ad65bf6Ssjur.brandeland@stericsson.com 	common.use_frag = false;
1677ad65bf6Ssjur.brandeland@stericsson.com 	common.use_fcs = false;
1687ad65bf6Ssjur.brandeland@stericsson.com 	common.use_stx = false;
1697ad65bf6Ssjur.brandeland@stericsson.com 	common.link_select = CAIF_LINK_HIGH_BANDW;
1707ad65bf6Ssjur.brandeland@stericsson.com 	common.flowctrl = NULL;
1717ad65bf6Ssjur.brandeland@stericsson.com 
1727ad65bf6Ssjur.brandeland@stericsson.com 	link_support = cfusbl_create(dev->ifindex, dev->dev_addr,
1737ad65bf6Ssjur.brandeland@stericsson.com 					dev->broadcast);
1747ad65bf6Ssjur.brandeland@stericsson.com 
1757ad65bf6Ssjur.brandeland@stericsson.com 	if (!link_support)
1767ad65bf6Ssjur.brandeland@stericsson.com 		return -ENOMEM;
1777ad65bf6Ssjur.brandeland@stericsson.com 
1787ad65bf6Ssjur.brandeland@stericsson.com 	if (dev->num_tx_queues > 1)
1797ad65bf6Ssjur.brandeland@stericsson.com 		pr_warn("USB device uses more than one tx queue\n");
1807ad65bf6Ssjur.brandeland@stericsson.com 
1817f5d8666SPavel Skripkin 	res = caif_enroll_dev(dev, &common, link_support, CFUSB_MAX_HEADLEN,
1827ad65bf6Ssjur.brandeland@stericsson.com 			&layer, &caif_usb_type.func);
1837f5d8666SPavel Skripkin 	if (res)
1847f5d8666SPavel Skripkin 		goto err;
1857f5d8666SPavel Skripkin 
1867ad65bf6Ssjur.brandeland@stericsson.com 	if (!pack_added)
1877ad65bf6Ssjur.brandeland@stericsson.com 		dev_add_pack(&caif_usb_type);
1883db1cd5cSRusty Russell 	pack_added = true;
1897ad65bf6Ssjur.brandeland@stericsson.com 
190df207b00SWolfram Sang 	strscpy(layer->name, dev->name, sizeof(layer->name));
1917ad65bf6Ssjur.brandeland@stericsson.com 
1927ad65bf6Ssjur.brandeland@stericsson.com 	return 0;
1937f5d8666SPavel Skripkin err:
1947f5d8666SPavel Skripkin 	cfusbl_release(link_support);
1957f5d8666SPavel Skripkin 	return res;
1967ad65bf6Ssjur.brandeland@stericsson.com }
1977ad65bf6Ssjur.brandeland@stericsson.com 
1987ad65bf6Ssjur.brandeland@stericsson.com static struct notifier_block caif_device_notifier = {
1997ad65bf6Ssjur.brandeland@stericsson.com 	.notifier_call = cfusbl_device_notify,
2007ad65bf6Ssjur.brandeland@stericsson.com 	.priority = 0,
2017ad65bf6Ssjur.brandeland@stericsson.com };
2027ad65bf6Ssjur.brandeland@stericsson.com 
cfusbl_init(void)2037ad65bf6Ssjur.brandeland@stericsson.com static int __init cfusbl_init(void)
2047ad65bf6Ssjur.brandeland@stericsson.com {
2057ad65bf6Ssjur.brandeland@stericsson.com 	return register_netdevice_notifier(&caif_device_notifier);
2067ad65bf6Ssjur.brandeland@stericsson.com }
2077ad65bf6Ssjur.brandeland@stericsson.com 
cfusbl_exit(void)2087ad65bf6Ssjur.brandeland@stericsson.com static void __exit cfusbl_exit(void)
2097ad65bf6Ssjur.brandeland@stericsson.com {
2107ad65bf6Ssjur.brandeland@stericsson.com 	unregister_netdevice_notifier(&caif_device_notifier);
2117ad65bf6Ssjur.brandeland@stericsson.com 	dev_remove_pack(&caif_usb_type);
2127ad65bf6Ssjur.brandeland@stericsson.com }
2137ad65bf6Ssjur.brandeland@stericsson.com 
2147ad65bf6Ssjur.brandeland@stericsson.com module_init(cfusbl_init);
2157ad65bf6Ssjur.brandeland@stericsson.com module_exit(cfusbl_exit);
216