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