blob: 187c144c5e5bc23f3fd9b6286374349f7883862f [file] [log] [blame]
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +00001/*
2 * phonet.c -- USB CDC Phonet host driver
3 *
4 * Copyright (C) 2008-2009 Nokia Corporation. All rights reserved.
5 *
6 * Author: Rémi Denis-Courmont
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 */
22
23#include <linux/kernel.h>
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000024#include <linux/mm.h>
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +000025#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/gfp.h>
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +000027#include <linux/usb.h>
28#include <linux/usb/cdc.h>
29#include <linux/netdevice.h>
30#include <linux/if_arp.h>
31#include <linux/if_phonet.h>
Rémi Denis-Courmont02571f82009-09-09 00:00:06 +000032#include <linux/phonet.h>
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +000033
34#define PN_MEDIA_USB 0x1B
35
36static const unsigned rxq_size = 17;
37
38struct usbpn_dev {
39 struct net_device *dev;
40
41 struct usb_interface *intf, *data_intf;
42 struct usb_device *usb;
43 unsigned int tx_pipe, rx_pipe;
44 u8 active_setting;
45 u8 disconnected;
46
47 unsigned tx_queue;
48 spinlock_t tx_lock;
49
50 spinlock_t rx_lock;
51 struct sk_buff *rx_skb;
52 struct urb *urbs[0];
53};
54
55static void tx_complete(struct urb *req);
56static void rx_complete(struct urb *req);
57
58/*
59 * Network device callbacks
60 */
Stephen Hemminger25a79c42009-08-31 19:50:45 +000061static netdev_tx_t usbpn_xmit(struct sk_buff *skb, struct net_device *dev)
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +000062{
63 struct usbpn_dev *pnd = netdev_priv(dev);
64 struct urb *req = NULL;
65 unsigned long flags;
66 int err;
67
68 if (skb->protocol != htons(ETH_P_PHONET))
69 goto drop;
70
71 req = usb_alloc_urb(0, GFP_ATOMIC);
72 if (!req)
73 goto drop;
74 usb_fill_bulk_urb(req, pnd->usb, pnd->tx_pipe, skb->data, skb->len,
75 tx_complete, skb);
76 req->transfer_flags = URB_ZERO_PACKET;
77 err = usb_submit_urb(req, GFP_ATOMIC);
78 if (err) {
79 usb_free_urb(req);
80 goto drop;
81 }
82
83 spin_lock_irqsave(&pnd->tx_lock, flags);
84 pnd->tx_queue++;
85 if (pnd->tx_queue >= dev->tx_queue_len)
86 netif_stop_queue(dev);
87 spin_unlock_irqrestore(&pnd->tx_lock, flags);
Stephen Hemminger25a79c42009-08-31 19:50:45 +000088 return NETDEV_TX_OK;
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +000089
90drop:
91 dev_kfree_skb(skb);
92 dev->stats.tx_dropped++;
Stephen Hemminger25a79c42009-08-31 19:50:45 +000093 return NETDEV_TX_OK;
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +000094}
95
96static void tx_complete(struct urb *req)
97{
98 struct sk_buff *skb = req->context;
99 struct net_device *dev = skb->dev;
100 struct usbpn_dev *pnd = netdev_priv(dev);
Oliver Neukumc31fd6c2010-07-02 15:51:55 +0200101 int status = req->status;
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000102
Oliver Neukumc31fd6c2010-07-02 15:51:55 +0200103 switch (status) {
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000104 case 0:
105 dev->stats.tx_bytes += skb->len;
106 break;
107
108 case -ENOENT:
109 case -ECONNRESET:
110 case -ESHUTDOWN:
111 dev->stats.tx_aborted_errors++;
112 default:
113 dev->stats.tx_errors++;
Oliver Neukumc31fd6c2010-07-02 15:51:55 +0200114 dev_dbg(&dev->dev, "TX error (%d)\n", status);
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000115 }
116 dev->stats.tx_packets++;
117
118 spin_lock(&pnd->tx_lock);
119 pnd->tx_queue--;
120 netif_wake_queue(dev);
121 spin_unlock(&pnd->tx_lock);
122
123 dev_kfree_skb_any(skb);
124 usb_free_urb(req);
125}
126
127static int rx_submit(struct usbpn_dev *pnd, struct urb *req, gfp_t gfp_flags)
128{
129 struct net_device *dev = pnd->dev;
130 struct page *page;
131 int err;
132
Eric Dumazet1f2149c2011-11-22 10:57:41 +0000133 page = alloc_page(gfp_flags);
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000134 if (!page)
135 return -ENOMEM;
136
137 usb_fill_bulk_urb(req, pnd->usb, pnd->rx_pipe, page_address(page),
138 PAGE_SIZE, rx_complete, dev);
139 req->transfer_flags = 0;
140 err = usb_submit_urb(req, gfp_flags);
141 if (unlikely(err)) {
142 dev_dbg(&dev->dev, "RX submit error (%d)\n", err);
Eric Dumazet1f2149c2011-11-22 10:57:41 +0000143 put_page(page);
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000144 }
145 return err;
146}
147
148static void rx_complete(struct urb *req)
149{
150 struct net_device *dev = req->context;
151 struct usbpn_dev *pnd = netdev_priv(dev);
152 struct page *page = virt_to_page(req->transfer_buffer);
153 struct sk_buff *skb;
154 unsigned long flags;
Oliver Neukumc31fd6c2010-07-02 15:51:55 +0200155 int status = req->status;
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000156
Oliver Neukumc31fd6c2010-07-02 15:51:55 +0200157 switch (status) {
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000158 case 0:
159 spin_lock_irqsave(&pnd->rx_lock, flags);
160 skb = pnd->rx_skb;
161 if (!skb) {
162 skb = pnd->rx_skb = netdev_alloc_skb(dev, 12);
163 if (likely(skb)) {
164 /* Can't use pskb_pull() on page in IRQ */
165 memcpy(skb_put(skb, 1), page_address(page), 1);
166 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
Eric Dumazet50269e12012-03-23 23:59:33 +0000167 page, 1, req->actual_length,
Eric Dumazet094b5852012-03-27 03:17:26 +0000168 PAGE_SIZE);
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000169 page = NULL;
170 }
171 } else {
172 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
Eric Dumazet50269e12012-03-23 23:59:33 +0000173 page, 0, req->actual_length,
Eric Dumazet094b5852012-03-27 03:17:26 +0000174 PAGE_SIZE);
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000175 page = NULL;
176 }
177 if (req->actual_length < PAGE_SIZE)
178 pnd->rx_skb = NULL; /* Last fragment */
179 else
180 skb = NULL;
181 spin_unlock_irqrestore(&pnd->rx_lock, flags);
182 if (skb) {
183 skb->protocol = htons(ETH_P_PHONET);
184 skb_reset_mac_header(skb);
185 __skb_pull(skb, 1);
186 skb->dev = dev;
187 dev->stats.rx_packets++;
188 dev->stats.rx_bytes += skb->len;
189
190 netif_rx(skb);
191 }
192 goto resubmit;
193
194 case -ENOENT:
195 case -ECONNRESET:
196 case -ESHUTDOWN:
197 req = NULL;
198 break;
199
200 case -EOVERFLOW:
201 dev->stats.rx_over_errors++;
202 dev_dbg(&dev->dev, "RX overflow\n");
203 break;
204
205 case -EILSEQ:
206 dev->stats.rx_crc_errors++;
207 break;
208 }
209
210 dev->stats.rx_errors++;
211resubmit:
212 if (page)
Eric Dumazet1f2149c2011-11-22 10:57:41 +0000213 put_page(page);
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000214 if (req)
Eric Dumazet1f2149c2011-11-22 10:57:41 +0000215 rx_submit(pnd, req, GFP_ATOMIC | __GFP_COLD);
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000216}
217
218static int usbpn_close(struct net_device *dev);
219
220static int usbpn_open(struct net_device *dev)
221{
222 struct usbpn_dev *pnd = netdev_priv(dev);
223 int err;
224 unsigned i;
225 unsigned num = pnd->data_intf->cur_altsetting->desc.bInterfaceNumber;
226
227 err = usb_set_interface(pnd->usb, num, pnd->active_setting);
228 if (err)
229 return err;
230
231 for (i = 0; i < rxq_size; i++) {
232 struct urb *req = usb_alloc_urb(0, GFP_KERNEL);
233
Eric Dumazet1f2149c2011-11-22 10:57:41 +0000234 if (!req || rx_submit(pnd, req, GFP_KERNEL | __GFP_COLD)) {
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000235 usbpn_close(dev);
236 return -ENOMEM;
237 }
238 pnd->urbs[i] = req;
239 }
240
241 netif_wake_queue(dev);
242 return 0;
243}
244
245static int usbpn_close(struct net_device *dev)
246{
247 struct usbpn_dev *pnd = netdev_priv(dev);
248 unsigned i;
249 unsigned num = pnd->data_intf->cur_altsetting->desc.bInterfaceNumber;
250
251 netif_stop_queue(dev);
252
253 for (i = 0; i < rxq_size; i++) {
254 struct urb *req = pnd->urbs[i];
255
256 if (!req)
257 continue;
258 usb_kill_urb(req);
259 usb_free_urb(req);
260 pnd->urbs[i] = NULL;
261 }
262
263 return usb_set_interface(pnd->usb, num, !pnd->active_setting);
264}
265
Rémi Denis-Courmont02571f82009-09-09 00:00:06 +0000266static int usbpn_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
267{
268 struct if_phonet_req *req = (struct if_phonet_req *)ifr;
269
270 switch (cmd) {
271 case SIOCPNGAUTOCONF:
272 req->ifr_phonet_autoconf.device = PN_DEV_PC;
Rémi Denis-Courmont02571f82009-09-09 00:00:06 +0000273 return 0;
274 }
275 return -ENOIOCTLCMD;
276}
277
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000278static int usbpn_set_mtu(struct net_device *dev, int new_mtu)
279{
280 if ((new_mtu < PHONET_MIN_MTU) || (new_mtu > PHONET_MAX_MTU))
281 return -EINVAL;
282
283 dev->mtu = new_mtu;
284 return 0;
285}
286
287static const struct net_device_ops usbpn_ops = {
288 .ndo_open = usbpn_open,
289 .ndo_stop = usbpn_close,
290 .ndo_start_xmit = usbpn_xmit,
Rémi Denis-Courmont02571f82009-09-09 00:00:06 +0000291 .ndo_do_ioctl = usbpn_ioctl,
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000292 .ndo_change_mtu = usbpn_set_mtu,
293};
294
295static void usbpn_setup(struct net_device *dev)
296{
297 dev->features = 0;
298 dev->netdev_ops = &usbpn_ops,
299 dev->header_ops = &phonet_header_ops;
300 dev->type = ARPHRD_PHONET;
301 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
302 dev->mtu = PHONET_MAX_MTU;
303 dev->hard_header_len = 1;
304 dev->dev_addr[0] = PN_MEDIA_USB;
305 dev->addr_len = 1;
306 dev->tx_queue_len = 3;
307
308 dev->destructor = free_netdev;
309}
310
311/*
312 * USB driver callbacks
313 */
314static struct usb_device_id usbpn_ids[] = {
315 {
316 .match_flags = USB_DEVICE_ID_MATCH_VENDOR
317 | USB_DEVICE_ID_MATCH_INT_CLASS
318 | USB_DEVICE_ID_MATCH_INT_SUBCLASS,
319 .idVendor = 0x0421, /* Nokia */
320 .bInterfaceClass = USB_CLASS_COMM,
321 .bInterfaceSubClass = 0xFE,
322 },
323 { },
324};
325
326MODULE_DEVICE_TABLE(usb, usbpn_ids);
327
328static struct usb_driver usbpn_driver;
329
330int usbpn_probe(struct usb_interface *intf, const struct usb_device_id *id)
331{
332 static const char ifname[] = "usbpn%d";
333 const struct usb_cdc_union_desc *union_header = NULL;
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000334 const struct usb_host_interface *data_desc;
335 struct usb_interface *data_intf;
336 struct usb_device *usbdev = interface_to_usbdev(intf);
337 struct net_device *dev;
338 struct usbpn_dev *pnd;
339 u8 *data;
Jiri Slaby468c3f92011-03-13 06:54:31 +0000340 int phonet = 0;
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000341 int len, err;
342
343 data = intf->altsetting->extra;
344 len = intf->altsetting->extralen;
345 while (len >= 3) {
346 u8 dlen = data[0];
347 if (dlen < 3)
348 return -EINVAL;
349
350 /* bDescriptorType */
351 if (data[1] == USB_DT_CS_INTERFACE) {
352 /* bDescriptorSubType */
353 switch (data[2]) {
354 case USB_CDC_UNION_TYPE:
355 if (union_header || dlen < 5)
356 break;
357 union_header =
358 (struct usb_cdc_union_desc *)data;
359 break;
360 case 0xAB:
Jiri Slaby468c3f92011-03-13 06:54:31 +0000361 phonet = 1;
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000362 break;
363 }
364 }
365 data += dlen;
366 len -= dlen;
367 }
368
Jiri Slaby468c3f92011-03-13 06:54:31 +0000369 if (!union_header || !phonet)
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000370 return -EINVAL;
371
372 data_intf = usb_ifnum_to_if(usbdev, union_header->bSlaveInterface0);
373 if (data_intf == NULL)
374 return -ENODEV;
375 /* Data interface has one inactive and one active setting */
376 if (data_intf->num_altsetting != 2)
377 return -EINVAL;
Joe Perches8e95a202009-12-03 07:58:21 +0000378 if (data_intf->altsetting[0].desc.bNumEndpoints == 0 &&
379 data_intf->altsetting[1].desc.bNumEndpoints == 2)
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000380 data_desc = data_intf->altsetting + 1;
381 else
Joe Perches8e95a202009-12-03 07:58:21 +0000382 if (data_intf->altsetting[0].desc.bNumEndpoints == 2 &&
383 data_intf->altsetting[1].desc.bNumEndpoints == 0)
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000384 data_desc = data_intf->altsetting;
385 else
386 return -EINVAL;
387
388 dev = alloc_netdev(sizeof(*pnd) + sizeof(pnd->urbs[0]) * rxq_size,
389 ifname, usbpn_setup);
390 if (!dev)
391 return -ENOMEM;
392
393 pnd = netdev_priv(dev);
394 SET_NETDEV_DEV(dev, &intf->dev);
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000395
396 pnd->dev = dev;
tom.leiming@gmail.com50e7d152012-06-11 15:19:41 +0000397 pnd->usb = usbdev;
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000398 pnd->intf = intf;
399 pnd->data_intf = data_intf;
400 spin_lock_init(&pnd->tx_lock);
401 spin_lock_init(&pnd->rx_lock);
402 /* Endpoints */
403 if (usb_pipein(data_desc->endpoint[0].desc.bEndpointAddress)) {
404 pnd->rx_pipe = usb_rcvbulkpipe(usbdev,
405 data_desc->endpoint[0].desc.bEndpointAddress);
406 pnd->tx_pipe = usb_sndbulkpipe(usbdev,
407 data_desc->endpoint[1].desc.bEndpointAddress);
408 } else {
409 pnd->rx_pipe = usb_rcvbulkpipe(usbdev,
410 data_desc->endpoint[1].desc.bEndpointAddress);
411 pnd->tx_pipe = usb_sndbulkpipe(usbdev,
412 data_desc->endpoint[0].desc.bEndpointAddress);
413 }
414 pnd->active_setting = data_desc - data_intf->altsetting;
415
416 err = usb_driver_claim_interface(&usbpn_driver, data_intf, pnd);
417 if (err)
418 goto out;
419
420 /* Force inactive mode until the network device is brought UP */
421 usb_set_interface(usbdev, union_header->bSlaveInterface0,
422 !pnd->active_setting);
423 usb_set_intfdata(intf, pnd);
424
425 err = register_netdev(dev);
426 if (err) {
427 usb_driver_release_interface(&usbpn_driver, data_intf);
428 goto out;
429 }
430
431 dev_dbg(&dev->dev, "USB CDC Phonet device found\n");
432 return 0;
433
434out:
435 usb_set_intfdata(intf, NULL);
436 free_netdev(dev);
437 return err;
438}
439
440static void usbpn_disconnect(struct usb_interface *intf)
441{
442 struct usbpn_dev *pnd = usb_get_intfdata(intf);
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000443
444 if (pnd->disconnected)
445 return;
446
447 pnd->disconnected = 1;
448 usb_driver_release_interface(&usbpn_driver,
449 (pnd->intf == intf) ? pnd->data_intf : pnd->intf);
450 unregister_netdev(pnd->dev);
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000451}
452
453static struct usb_driver usbpn_driver = {
454 .name = "cdc_phonet",
455 .probe = usbpn_probe,
456 .disconnect = usbpn_disconnect,
457 .id_table = usbpn_ids,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -0700458 .disable_hub_initiated_lpm = 1,
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000459};
460
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -0800461module_usb_driver(usbpn_driver);
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000462
463MODULE_AUTHOR("Remi Denis-Courmont");
464MODULE_DESCRIPTION("USB CDC Phonet host interface");
465MODULE_LICENSE("GPL");