Linux Audio

Check our new training course

Loading...
  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
  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>
 13#include <linux/mii.h>
 14#include <linux/usb.h>
 15#include <linux/usb/usbnet.h>
 16#include <linux/etherdevice.h>
 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
 
 23MODULE_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 */
 29#define STE_USB_PID_CAIF 0x230f	/* Product id for CAIF Modems */
 30
 31struct cfusbl {
 32	struct cflayer layer;
 33	u8 tx_eth_hdr[ETH_HLEN];
 34};
 35
 36static bool pack_added;
 37
 38static 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
 48static 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) {
 65		pr_warn("Headroom to small\n");
 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
 77static void cfusbl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
 78			   int phyid)
 79{
 80	if (layr->up && layr->up->ctrlcmd)
 81		layr->up->ctrlcmd(layr->up, ctrl, layr->id);
 82}
 83
 84static struct cflayer *cfusbl_create(int phyid, u8 ethaddr[ETH_ALEN],
 85				      u8 braddr[ETH_ALEN])
 86{
 87	struct cfusbl *this = kmalloc(sizeof(struct cfusbl), GFP_ATOMIC);
 88
 89	if (!this)
 90		return NULL;
 91
 92	caif_assert(offsetof(struct cfusbl, layer) == 0);
 93
 94	memset(&this->layer, 0, sizeof(this->layer));
 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	 */
107	ether_addr_copy(&this->tx_eth_hdr[ETH_ALEN], braddr);
108	ether_addr_copy(&this->tx_eth_hdr[ETH_ALEN], ethaddr);
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
 
 
 
 
 
118static struct packet_type caif_usb_type __read_mostly = {
119	.type = cpu_to_be16(ETH_P_802_EX1),
120};
121
122static int cfusbl_device_notify(struct notifier_block *me, unsigned long what,
123				void *ptr)
124{
125	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
126	struct caif_dev_common common;
127	struct cflayer *layer, *link_support;
128	struct usbnet *usbnet;
129	struct usb_device *usbdev;
 
 
 
 
130
131	/* Check whether we have a NCM device, and find its VID/PID. */
132	if (!(dev->dev.parent && dev->dev.parent->driver &&
133	      strcmp(dev->dev.parent->driver->name, "cdc_ncm") == 0))
134		return 0;
135
136	usbnet = netdev_priv(dev);
137	usbdev = usbnet->udev;
138
139	pr_debug("USB CDC NCM device VID:0x%4x PID:0x%4x\n",
140		le16_to_cpu(usbdev->descriptor.idVendor),
141		le16_to_cpu(usbdev->descriptor.idProduct));
142
143	/* Check for VID/PID that supports CAIF */
144	if (!(le16_to_cpu(usbdev->descriptor.idVendor) == STE_USB_VID &&
145		le16_to_cpu(usbdev->descriptor.idProduct) == STE_USB_PID_CAIF))
146		return 0;
147
148	if (what == NETDEV_UNREGISTER)
149		module_put(THIS_MODULE);
150
151	if (what != NETDEV_REGISTER)
152		return 0;
153
154	__module_get(THIS_MODULE);
155
156	memset(&common, 0, sizeof(common));
157	common.use_frag = false;
158	common.use_fcs = false;
159	common.use_stx = false;
160	common.link_select = CAIF_LINK_HIGH_BANDW;
161	common.flowctrl = NULL;
162
163	link_support = cfusbl_create(dev->ifindex, dev->dev_addr,
164					dev->broadcast);
165
166	if (!link_support)
167		return -ENOMEM;
168
169	if (dev->num_tx_queues > 1)
170		pr_warn("USB device uses more than one tx queue\n");
171
172	caif_enroll_dev(dev, &common, link_support, CFUSB_MAX_HEADLEN,
173			&layer, &caif_usb_type.func);
 
 
 
174	if (!pack_added)
175		dev_add_pack(&caif_usb_type);
176	pack_added = true;
177
178	strlcpy(layer->name, dev->name, sizeof(layer->name));
179
180	return 0;
 
 
 
181}
182
183static struct notifier_block caif_device_notifier = {
184	.notifier_call = cfusbl_device_notify,
185	.priority = 0,
186};
187
188static int __init cfusbl_init(void)
189{
190	return register_netdevice_notifier(&caif_device_notifier);
191}
192
193static void __exit cfusbl_exit(void)
194{
195	unregister_netdevice_notifier(&caif_device_notifier);
196	dev_remove_pack(&caif_usb_type);
197}
198
199module_init(cfusbl_init);
200module_exit(cfusbl_exit);
  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
  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>
 13#include <linux/mii.h>
 14#include <linux/usb.h>
 15#include <linux/usb/usbnet.h>
 16#include <linux/etherdevice.h>
 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
 23MODULE_DESCRIPTION("ST-Ericsson CAIF modem protocol USB support");
 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 */
 30#define STE_USB_PID_CAIF 0x230f	/* Product id for CAIF Modems */
 31
 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 too 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,
 79			   int phyid)
 80{
 81	if (layr->up && layr->up->ctrlcmd)
 82		layr->up->ctrlcmd(layr->up, ctrl, layr->id);
 83}
 84
 85static struct cflayer *cfusbl_create(int phyid, const u8 ethaddr[ETH_ALEN],
 86				      u8 braddr[ETH_ALEN])
 87{
 88	struct cfusbl *this = kmalloc(sizeof(struct cfusbl), GFP_ATOMIC);
 89
 90	if (!this)
 91		return NULL;
 92
 93	caif_assert(offsetof(struct cfusbl, layer) == 0);
 94
 95	memset(&this->layer, 0, sizeof(this->layer));
 96	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	 */
108	ether_addr_copy(&this->tx_eth_hdr[ETH_ALEN], braddr);
109	ether_addr_copy(&this->tx_eth_hdr[ETH_ALEN], ethaddr);
110	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 void cfusbl_release(struct cflayer *layer)
120{
121	kfree(layer);
122}
123
124static struct packet_type caif_usb_type __read_mostly = {
125	.type = cpu_to_be16(ETH_P_802_EX1),
126};
127
128static int cfusbl_device_notify(struct notifier_block *me, unsigned long what,
129				void *ptr)
130{
131	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
132	struct caif_dev_common common;
133	struct cflayer *layer, *link_support;
134	struct usbnet *usbnet;
135	struct usb_device *usbdev;
136	int res;
137
138	if (what == NETDEV_UNREGISTER && dev->reg_state >= NETREG_UNREGISTERED)
139		return 0;
140
141	/* Check whether we have a NCM device, and find its VID/PID. */
142	if (!(dev->dev.parent && dev->dev.parent->driver &&
143	      strcmp(dev->dev.parent->driver->name, "cdc_ncm") == 0))
144		return 0;
145
146	usbnet = netdev_priv(dev);
147	usbdev = usbnet->udev;
148
149	pr_debug("USB CDC NCM device VID:0x%4x PID:0x%4x\n",
150		le16_to_cpu(usbdev->descriptor.idVendor),
151		le16_to_cpu(usbdev->descriptor.idProduct));
152
153	/* Check for VID/PID that supports CAIF */
154	if (!(le16_to_cpu(usbdev->descriptor.idVendor) == STE_USB_VID &&
155		le16_to_cpu(usbdev->descriptor.idProduct) == STE_USB_PID_CAIF))
156		return 0;
157
158	if (what == NETDEV_UNREGISTER)
159		module_put(THIS_MODULE);
160
161	if (what != NETDEV_REGISTER)
162		return 0;
163
164	__module_get(THIS_MODULE);
165
166	memset(&common, 0, sizeof(common));
167	common.use_frag = false;
168	common.use_fcs = false;
169	common.use_stx = false;
170	common.link_select = CAIF_LINK_HIGH_BANDW;
171	common.flowctrl = NULL;
172
173	link_support = cfusbl_create(dev->ifindex, dev->dev_addr,
174					dev->broadcast);
175
176	if (!link_support)
177		return -ENOMEM;
178
179	if (dev->num_tx_queues > 1)
180		pr_warn("USB device uses more than one tx queue\n");
181
182	res = caif_enroll_dev(dev, &common, link_support, CFUSB_MAX_HEADLEN,
183			&layer, &caif_usb_type.func);
184	if (res)
185		goto err;
186
187	if (!pack_added)
188		dev_add_pack(&caif_usb_type);
189	pack_added = true;
190
191	strscpy(layer->name, dev->name, sizeof(layer->name));
192
193	return 0;
194err:
195	cfusbl_release(link_support);
196	return res;
197}
198
199static struct notifier_block caif_device_notifier = {
200	.notifier_call = cfusbl_device_notify,
201	.priority = 0,
202};
203
204static int __init cfusbl_init(void)
205{
206	return register_netdevice_notifier(&caif_device_notifier);
207}
208
209static void __exit cfusbl_exit(void)
210{
211	unregister_netdevice_notifier(&caif_device_notifier);
212	dev_remove_pack(&caif_usb_type);
213}
214
215module_init(cfusbl_init);
216module_exit(cfusbl_exit);