x86/amd-iommu: Add function to complete a tlb flush
[linux/fpc-iii.git] / drivers / net / usb / cdc-phonet.c
blob33d5c579c5add542772afb6bf4327bdbb0ebc90b
1 /*
2 * phonet.c -- USB CDC Phonet host driver
4 * Copyright (C) 2008-2009 Nokia Corporation. All rights reserved.
6 * Author: Rémi Denis-Courmont
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.
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.
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
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>
30 #include <linux/phonet.h>
32 #define PN_MEDIA_USB 0x1B
34 static const unsigned rxq_size = 17;
36 struct usbpn_dev {
37 struct net_device *dev;
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;
45 unsigned tx_queue;
46 spinlock_t tx_lock;
48 spinlock_t rx_lock;
49 struct sk_buff *rx_skb;
50 struct urb *urbs[0];
53 static void tx_complete(struct urb *req);
54 static void rx_complete(struct urb *req);
57 * Network device callbacks
59 static netdev_tx_t usbpn_xmit(struct sk_buff *skb, struct net_device *dev)
61 struct usbpn_dev *pnd = netdev_priv(dev);
62 struct urb *req = NULL;
63 unsigned long flags;
64 int err;
66 if (skb->protocol != htons(ETH_P_PHONET))
67 goto drop;
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;
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);
86 return NETDEV_TX_OK;
88 drop:
89 dev_kfree_skb(skb);
90 dev->stats.tx_dropped++;
91 return NETDEV_TX_OK;
94 static void tx_complete(struct urb *req)
96 struct sk_buff *skb = req->context;
97 struct net_device *dev = skb->dev;
98 struct usbpn_dev *pnd = netdev_priv(dev);
100 switch (req->status) {
101 case 0:
102 dev->stats.tx_bytes += skb->len;
103 break;
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);
113 dev->stats.tx_packets++;
115 spin_lock(&pnd->tx_lock);
116 pnd->tx_queue--;
117 netif_wake_queue(dev);
118 spin_unlock(&pnd->tx_lock);
120 dev_kfree_skb_any(skb);
121 usb_free_urb(req);
124 static int rx_submit(struct usbpn_dev *pnd, struct urb *req, gfp_t gfp_flags)
126 struct net_device *dev = pnd->dev;
127 struct page *page;
128 int err;
130 page = __netdev_alloc_page(dev, gfp_flags);
131 if (!page)
132 return -ENOMEM;
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);
142 return err;
145 static void rx_complete(struct urb *req)
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;
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;
166 } else {
167 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
168 page, 0, req->actual_length);
169 page = NULL;
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;
184 netif_rx(skb);
186 goto resubmit;
188 case -ENOENT:
189 case -ECONNRESET:
190 case -ESHUTDOWN:
191 req = NULL;
192 break;
194 case -EOVERFLOW:
195 dev->stats.rx_over_errors++;
196 dev_dbg(&dev->dev, "RX overflow\n");
197 break;
199 case -EILSEQ:
200 dev->stats.rx_crc_errors++;
201 break;
204 dev->stats.rx_errors++;
205 resubmit:
206 if (page)
207 netdev_free_page(dev, page);
208 if (req)
209 rx_submit(pnd, req, GFP_ATOMIC);
212 static int usbpn_close(struct net_device *dev);
214 static int usbpn_open(struct net_device *dev)
216 struct usbpn_dev *pnd = netdev_priv(dev);
217 int err;
218 unsigned i;
219 unsigned num = pnd->data_intf->cur_altsetting->desc.bInterfaceNumber;
221 err = usb_set_interface(pnd->usb, num, pnd->active_setting);
222 if (err)
223 return err;
225 for (i = 0; i < rxq_size; i++) {
226 struct urb *req = usb_alloc_urb(0, GFP_KERNEL);
228 if (!req || rx_submit(pnd, req, GFP_KERNEL)) {
229 usbpn_close(dev);
230 return -ENOMEM;
232 pnd->urbs[i] = req;
235 netif_wake_queue(dev);
236 return 0;
239 static int usbpn_close(struct net_device *dev)
241 struct usbpn_dev *pnd = netdev_priv(dev);
242 unsigned i;
243 unsigned num = pnd->data_intf->cur_altsetting->desc.bInterfaceNumber;
245 netif_stop_queue(dev);
247 for (i = 0; i < rxq_size; i++) {
248 struct urb *req = pnd->urbs[i];
250 if (!req)
251 continue;
252 usb_kill_urb(req);
253 usb_free_urb(req);
254 pnd->urbs[i] = NULL;
257 return usb_set_interface(pnd->usb, num, !pnd->active_setting);
260 static int usbpn_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
262 struct if_phonet_req *req = (struct if_phonet_req *)ifr;
264 switch (cmd) {
265 case SIOCPNGAUTOCONF:
266 req->ifr_phonet_autoconf.device = PN_DEV_PC;
267 return 0;
269 return -ENOIOCTLCMD;
272 static int usbpn_set_mtu(struct net_device *dev, int new_mtu)
274 if ((new_mtu < PHONET_MIN_MTU) || (new_mtu > PHONET_MAX_MTU))
275 return -EINVAL;
277 dev->mtu = new_mtu;
278 return 0;
281 static const struct net_device_ops usbpn_ops = {
282 .ndo_open = usbpn_open,
283 .ndo_stop = usbpn_close,
284 .ndo_start_xmit = usbpn_xmit,
285 .ndo_do_ioctl = usbpn_ioctl,
286 .ndo_change_mtu = usbpn_set_mtu,
289 static void usbpn_setup(struct net_device *dev)
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;
302 dev->destructor = free_netdev;
306 * USB driver callbacks
308 static struct usb_device_id usbpn_ids[] = {
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,
317 { },
320 MODULE_DEVICE_TABLE(usb, usbpn_ids);
322 static struct usb_driver usbpn_driver;
324 int usbpn_probe(struct usb_interface *intf, const struct usb_device_id *id)
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;
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;
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;
362 data += dlen;
363 len -= dlen;
366 if (!union_header || !phonet_header)
367 return -EINVAL;
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;
375 if (data_intf->altsetting[0].desc.bNumEndpoints == 0
376 && data_intf->altsetting[1].desc.bNumEndpoints == 2)
377 data_desc = data_intf->altsetting + 1;
378 else
379 if (data_intf->altsetting[0].desc.bNumEndpoints == 2
380 && data_intf->altsetting[1].desc.bNumEndpoints == 0)
381 data_desc = data_intf->altsetting;
382 else
383 return -EINVAL;
385 dev = alloc_netdev(sizeof(*pnd) + sizeof(pnd->urbs[0]) * rxq_size,
386 ifname, usbpn_setup);
387 if (!dev)
388 return -ENOMEM;
390 pnd = netdev_priv(dev);
391 SET_NETDEV_DEV(dev, &intf->dev);
392 netif_stop_queue(dev);
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);
412 pnd->active_setting = data_desc - data_intf->altsetting;
414 err = usb_driver_claim_interface(&usbpn_driver, data_intf, pnd);
415 if (err)
416 goto out;
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);
423 err = register_netdev(dev);
424 if (err) {
425 usb_driver_release_interface(&usbpn_driver, data_intf);
426 goto out;
429 dev_dbg(&dev->dev, "USB CDC Phonet device found\n");
430 return 0;
432 out:
433 usb_set_intfdata(intf, NULL);
434 free_netdev(dev);
435 return err;
438 static void usbpn_disconnect(struct usb_interface *intf)
440 struct usbpn_dev *pnd = usb_get_intfdata(intf);
441 struct usb_device *usb = pnd->usb;
443 if (pnd->disconnected)
444 return;
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);
453 static struct usb_driver usbpn_driver = {
454 .name = "cdc_phonet",
455 .probe = usbpn_probe,
456 .disconnect = usbpn_disconnect,
457 .id_table = usbpn_ids,
460 static int __init usbpn_init(void)
462 return usb_register(&usbpn_driver);
465 static void __exit usbpn_exit(void)
467 usb_deregister(&usbpn_driver);
470 module_init(usbpn_init);
471 module_exit(usbpn_exit);
473 MODULE_AUTHOR("Remi Denis-Courmont");
474 MODULE_DESCRIPTION("USB CDC Phonet host interface");
475 MODULE_LICENSE("GPL");