blob: dc9444525b4901c2035d50e5c84b7f39b510bc6d [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>
24#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/gfp.h>
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +000026#include <linux/usb.h>
27#include <linux/usb/cdc.h>
28#include <linux/netdevice.h>
29#include <linux/if_arp.h>
30#include <linux/if_phonet.h>
Rémi Denis-Courmont02571f82009-09-09 00:00:06 +000031#include <linux/phonet.h>
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +000032
33#define PN_MEDIA_USB 0x1B
34
35static const unsigned rxq_size = 17;
36
37struct usbpn_dev {
38 struct net_device *dev;
39
40 struct usb_interface *intf, *data_intf;
41 struct usb_device *usb;
42 unsigned int tx_pipe, rx_pipe;
43 u8 active_setting;
44 u8 disconnected;
45
46 unsigned tx_queue;
47 spinlock_t tx_lock;
48
49 spinlock_t rx_lock;
50 struct sk_buff *rx_skb;
51 struct urb *urbs[0];
52};
53
54static void tx_complete(struct urb *req);
55static void rx_complete(struct urb *req);
56
57/*
58 * Network device callbacks
59 */
Stephen Hemminger25a79c42009-08-31 19:50:45 +000060static netdev_tx_t usbpn_xmit(struct sk_buff *skb, struct net_device *dev)
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +000061{
62 struct usbpn_dev *pnd = netdev_priv(dev);
63 struct urb *req = NULL;
64 unsigned long flags;
65 int err;
66
67 if (skb->protocol != htons(ETH_P_PHONET))
68 goto drop;
69
70 req = usb_alloc_urb(0, GFP_ATOMIC);
71 if (!req)
72 goto drop;
73 usb_fill_bulk_urb(req, pnd->usb, pnd->tx_pipe, skb->data, skb->len,
74 tx_complete, skb);
75 req->transfer_flags = URB_ZERO_PACKET;
76 err = usb_submit_urb(req, GFP_ATOMIC);
77 if (err) {
78 usb_free_urb(req);
79 goto drop;
80 }
81
82 spin_lock_irqsave(&pnd->tx_lock, flags);
83 pnd->tx_queue++;
84 if (pnd->tx_queue >= dev->tx_queue_len)
85 netif_stop_queue(dev);
86 spin_unlock_irqrestore(&pnd->tx_lock, flags);
Stephen Hemminger25a79c42009-08-31 19:50:45 +000087 return NETDEV_TX_OK;
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +000088
89drop:
90 dev_kfree_skb(skb);
91 dev->stats.tx_dropped++;
Stephen Hemminger25a79c42009-08-31 19:50:45 +000092 return NETDEV_TX_OK;
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +000093}
94
95static void tx_complete(struct urb *req)
96{
97 struct sk_buff *skb = req->context;
98 struct net_device *dev = skb->dev;
99 struct usbpn_dev *pnd = netdev_priv(dev);
100
101 switch (req->status) {
102 case 0:
103 dev->stats.tx_bytes += skb->len;
104 break;
105
106 case -ENOENT:
107 case -ECONNRESET:
108 case -ESHUTDOWN:
109 dev->stats.tx_aborted_errors++;
110 default:
111 dev->stats.tx_errors++;
112 dev_dbg(&dev->dev, "TX error (%d)\n", req->status);
113 }
114 dev->stats.tx_packets++;
115
116 spin_lock(&pnd->tx_lock);
117 pnd->tx_queue--;
118 netif_wake_queue(dev);
119 spin_unlock(&pnd->tx_lock);
120
121 dev_kfree_skb_any(skb);
122 usb_free_urb(req);
123}
124
125static int rx_submit(struct usbpn_dev *pnd, struct urb *req, gfp_t gfp_flags)
126{
127 struct net_device *dev = pnd->dev;
128 struct page *page;
129 int err;
130
131 page = __netdev_alloc_page(dev, gfp_flags);
132 if (!page)
133 return -ENOMEM;
134
135 usb_fill_bulk_urb(req, pnd->usb, pnd->rx_pipe, page_address(page),
136 PAGE_SIZE, rx_complete, dev);
137 req->transfer_flags = 0;
138 err = usb_submit_urb(req, gfp_flags);
139 if (unlikely(err)) {
140 dev_dbg(&dev->dev, "RX submit error (%d)\n", err);
141 netdev_free_page(dev, page);
142 }
143 return err;
144}
145
146static void rx_complete(struct urb *req)
147{
148 struct net_device *dev = req->context;
149 struct usbpn_dev *pnd = netdev_priv(dev);
150 struct page *page = virt_to_page(req->transfer_buffer);
151 struct sk_buff *skb;
152 unsigned long flags;
153
154 switch (req->status) {
155 case 0:
156 spin_lock_irqsave(&pnd->rx_lock, flags);
157 skb = pnd->rx_skb;
158 if (!skb) {
159 skb = pnd->rx_skb = netdev_alloc_skb(dev, 12);
160 if (likely(skb)) {
161 /* Can't use pskb_pull() on page in IRQ */
162 memcpy(skb_put(skb, 1), page_address(page), 1);
163 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
164 page, 1, req->actual_length);
165 page = NULL;
166 }
167 } else {
168 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
169 page, 0, req->actual_length);
170 page = NULL;
171 }
172 if (req->actual_length < PAGE_SIZE)
173 pnd->rx_skb = NULL; /* Last fragment */
174 else
175 skb = NULL;
176 spin_unlock_irqrestore(&pnd->rx_lock, flags);
177 if (skb) {
178 skb->protocol = htons(ETH_P_PHONET);
179 skb_reset_mac_header(skb);
180 __skb_pull(skb, 1);
181 skb->dev = dev;
182 dev->stats.rx_packets++;
183 dev->stats.rx_bytes += skb->len;
184
185 netif_rx(skb);
186 }
187 goto resubmit;
188
189 case -ENOENT:
190 case -ECONNRESET:
191 case -ESHUTDOWN:
192 req = NULL;
193 break;
194
195 case -EOVERFLOW:
196 dev->stats.rx_over_errors++;
197 dev_dbg(&dev->dev, "RX overflow\n");
198 break;
199
200 case -EILSEQ:
201 dev->stats.rx_crc_errors++;
202 break;
203 }
204
205 dev->stats.rx_errors++;
206resubmit:
207 if (page)
208 netdev_free_page(dev, page);
209 if (req)
210 rx_submit(pnd, req, GFP_ATOMIC);
211}
212
213static int usbpn_close(struct net_device *dev);
214
215static int usbpn_open(struct net_device *dev)
216{
217 struct usbpn_dev *pnd = netdev_priv(dev);
218 int err;
219 unsigned i;
220 unsigned num = pnd->data_intf->cur_altsetting->desc.bInterfaceNumber;
221
222 err = usb_set_interface(pnd->usb, num, pnd->active_setting);
223 if (err)
224 return err;
225
226 for (i = 0; i < rxq_size; i++) {
227 struct urb *req = usb_alloc_urb(0, GFP_KERNEL);
228
229 if (!req || rx_submit(pnd, req, GFP_KERNEL)) {
230 usbpn_close(dev);
231 return -ENOMEM;
232 }
233 pnd->urbs[i] = req;
234 }
235
236 netif_wake_queue(dev);
237 return 0;
238}
239
240static int usbpn_close(struct net_device *dev)
241{
242 struct usbpn_dev *pnd = netdev_priv(dev);
243 unsigned i;
244 unsigned num = pnd->data_intf->cur_altsetting->desc.bInterfaceNumber;
245
246 netif_stop_queue(dev);
247
248 for (i = 0; i < rxq_size; i++) {
249 struct urb *req = pnd->urbs[i];
250
251 if (!req)
252 continue;
253 usb_kill_urb(req);
254 usb_free_urb(req);
255 pnd->urbs[i] = NULL;
256 }
257
258 return usb_set_interface(pnd->usb, num, !pnd->active_setting);
259}
260
Rémi Denis-Courmont02571f82009-09-09 00:00:06 +0000261static int usbpn_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
262{
263 struct if_phonet_req *req = (struct if_phonet_req *)ifr;
264
265 switch (cmd) {
266 case SIOCPNGAUTOCONF:
267 req->ifr_phonet_autoconf.device = PN_DEV_PC;
Rémi Denis-Courmont02571f82009-09-09 00:00:06 +0000268 return 0;
269 }
270 return -ENOIOCTLCMD;
271}
272
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000273static int usbpn_set_mtu(struct net_device *dev, int new_mtu)
274{
275 if ((new_mtu < PHONET_MIN_MTU) || (new_mtu > PHONET_MAX_MTU))
276 return -EINVAL;
277
278 dev->mtu = new_mtu;
279 return 0;
280}
281
282static const struct net_device_ops usbpn_ops = {
283 .ndo_open = usbpn_open,
284 .ndo_stop = usbpn_close,
285 .ndo_start_xmit = usbpn_xmit,
Rémi Denis-Courmont02571f82009-09-09 00:00:06 +0000286 .ndo_do_ioctl = usbpn_ioctl,
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000287 .ndo_change_mtu = usbpn_set_mtu,
288};
289
290static void usbpn_setup(struct net_device *dev)
291{
292 dev->features = 0;
293 dev->netdev_ops = &usbpn_ops,
294 dev->header_ops = &phonet_header_ops;
295 dev->type = ARPHRD_PHONET;
296 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
297 dev->mtu = PHONET_MAX_MTU;
298 dev->hard_header_len = 1;
299 dev->dev_addr[0] = PN_MEDIA_USB;
300 dev->addr_len = 1;
301 dev->tx_queue_len = 3;
302
303 dev->destructor = free_netdev;
304}
305
306/*
307 * USB driver callbacks
308 */
309static struct usb_device_id usbpn_ids[] = {
310 {
311 .match_flags = USB_DEVICE_ID_MATCH_VENDOR
312 | USB_DEVICE_ID_MATCH_INT_CLASS
313 | USB_DEVICE_ID_MATCH_INT_SUBCLASS,
314 .idVendor = 0x0421, /* Nokia */
315 .bInterfaceClass = USB_CLASS_COMM,
316 .bInterfaceSubClass = 0xFE,
317 },
318 { },
319};
320
321MODULE_DEVICE_TABLE(usb, usbpn_ids);
322
323static struct usb_driver usbpn_driver;
324
325int usbpn_probe(struct usb_interface *intf, const struct usb_device_id *id)
326{
327 static const char ifname[] = "usbpn%d";
328 const struct usb_cdc_union_desc *union_header = NULL;
329 const struct usb_cdc_header_desc *phonet_header = NULL;
330 const struct usb_host_interface *data_desc;
331 struct usb_interface *data_intf;
332 struct usb_device *usbdev = interface_to_usbdev(intf);
333 struct net_device *dev;
334 struct usbpn_dev *pnd;
335 u8 *data;
336 int len, err;
337
338 data = intf->altsetting->extra;
339 len = intf->altsetting->extralen;
340 while (len >= 3) {
341 u8 dlen = data[0];
342 if (dlen < 3)
343 return -EINVAL;
344
345 /* bDescriptorType */
346 if (data[1] == USB_DT_CS_INTERFACE) {
347 /* bDescriptorSubType */
348 switch (data[2]) {
349 case USB_CDC_UNION_TYPE:
350 if (union_header || dlen < 5)
351 break;
352 union_header =
353 (struct usb_cdc_union_desc *)data;
354 break;
355 case 0xAB:
356 if (phonet_header || dlen < 5)
357 break;
358 phonet_header =
359 (struct usb_cdc_header_desc *)data;
360 break;
361 }
362 }
363 data += dlen;
364 len -= dlen;
365 }
366
367 if (!union_header || !phonet_header)
368 return -EINVAL;
369
370 data_intf = usb_ifnum_to_if(usbdev, union_header->bSlaveInterface0);
371 if (data_intf == NULL)
372 return -ENODEV;
373 /* Data interface has one inactive and one active setting */
374 if (data_intf->num_altsetting != 2)
375 return -EINVAL;
Joe Perches8e95a202009-12-03 07:58:21 +0000376 if (data_intf->altsetting[0].desc.bNumEndpoints == 0 &&
377 data_intf->altsetting[1].desc.bNumEndpoints == 2)
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000378 data_desc = data_intf->altsetting + 1;
379 else
Joe Perches8e95a202009-12-03 07:58:21 +0000380 if (data_intf->altsetting[0].desc.bNumEndpoints == 2 &&
381 data_intf->altsetting[1].desc.bNumEndpoints == 0)
Rémi Denis-Courmont87cf6562009-07-21 01:58:35 +0000382 data_desc = data_intf->altsetting;
383 else
384 return -EINVAL;
385
386 dev = alloc_netdev(sizeof(*pnd) + sizeof(pnd->urbs[0]) * rxq_size,
387 ifname, usbpn_setup);
388 if (!dev)
389 return -ENOMEM;
390
391 pnd = netdev_priv(dev);
392 SET_NETDEV_DEV(dev, &intf->dev);
393 netif_stop_queue(dev);
394
395 pnd->dev = dev;
396 pnd->usb = usb_get_dev(usbdev);
397 pnd->intf = intf;
398 pnd->data_intf = data_intf;
399 spin_lock_init(&pnd->tx_lock);
400 spin_lock_init(&pnd->rx_lock);
401 /* Endpoints */
402 if (usb_pipein(data_desc->endpoint[0].desc.bEndpointAddress)) {
403 pnd->rx_pipe = usb_rcvbulkpipe(usbdev,
404 data_desc->endpoint[0].desc.bEndpointAddress);
405 pnd->tx_pipe = usb_sndbulkpipe(usbdev,
406 data_desc->endpoint[1].desc.bEndpointAddress);
407 } else {
408 pnd->rx_pipe = usb_rcvbulkpipe(usbdev,
409 data_desc->endpoint[1].desc.bEndpointAddress);
410 pnd->tx_pipe = usb_sndbulkpipe(usbdev,
411 data_desc->endpoint[0].desc.bEndpointAddress);
412 }
413 pnd->active_setting = data_desc - data_intf->altsetting;
414
415 err = usb_driver_claim_interface(&usbpn_driver, data_intf, pnd);
416 if (err)
417 goto out;
418
419 /* Force inactive mode until the network device is brought UP */
420 usb_set_interface(usbdev, union_header->bSlaveInterface0,
421 !pnd->active_setting);
422 usb_set_intfdata(intf, pnd);
423
424 err = register_netdev(dev);
425 if (err) {
426 usb_driver_release_interface(&usbpn_driver, data_intf);
427 goto out;
428 }
429
430 dev_dbg(&dev->dev, "USB CDC Phonet device found\n");
431 return 0;
432
433out:
434 usb_set_intfdata(intf, NULL);
435 free_netdev(dev);
436 return err;
437}
438
439static void usbpn_disconnect(struct usb_interface *intf)
440{
441 struct usbpn_dev *pnd = usb_get_intfdata(intf);
442 struct usb_device *usb = pnd->usb;
443
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);
451 usb_put_dev(usb);
452}
453
454static struct usb_driver usbpn_driver = {
455 .name = "cdc_phonet",
456 .probe = usbpn_probe,
457 .disconnect = usbpn_disconnect,
458 .id_table = usbpn_ids,
459};
460
461static int __init usbpn_init(void)
462{
463 return usb_register(&usbpn_driver);
464}
465
466static void __exit usbpn_exit(void)
467{
468 usb_deregister(&usbpn_driver);
469}
470
471module_init(usbpn_init);
472module_exit(usbpn_exit);
473
474MODULE_AUTHOR("Remi Denis-Courmont");
475MODULE_DESCRIPTION("USB CDC Phonet host interface");
476MODULE_LICENSE("GPL");