usb: r8a66597-udc disable interrupts on shutdown fix
[safe/jmp/linux-2.6] / drivers / usb / gadget / f_phonet.c
1 /*
2  * f_phonet.c -- USB CDC Phonet function
3  *
4  * Copyright (C) 2007-2008 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/device.h>
25
26 #include <linux/netdevice.h>
27 #include <linux/if_ether.h>
28 #include <linux/if_phonet.h>
29 #include <linux/if_arp.h>
30
31 #include <linux/usb/ch9.h>
32 #include <linux/usb/cdc.h>
33 #include <linux/usb/composite.h>
34
35 #include "u_phonet.h"
36
37 #define PN_MEDIA_USB    0x1B
38
39 /*-------------------------------------------------------------------------*/
40
41 struct phonet_port {
42         struct f_phonet                 *usb;
43         spinlock_t                      lock;
44 };
45
46 struct f_phonet {
47         struct usb_function             function;
48         struct net_device               *dev;
49         struct usb_ep                   *in_ep, *out_ep;
50
51         struct usb_request              *in_req;
52         struct usb_request              *out_reqv[0];
53 };
54
55 static int phonet_rxq_size = 2;
56
57 static inline struct f_phonet *func_to_pn(struct usb_function *f)
58 {
59         return container_of(f, struct f_phonet, function);
60 }
61
62 /*-------------------------------------------------------------------------*/
63
64 #define USB_CDC_SUBCLASS_PHONET 0xfe
65 #define USB_CDC_PHONET_TYPE     0xab
66
67 static struct usb_interface_descriptor
68 pn_control_intf_desc = {
69         .bLength =              sizeof pn_control_intf_desc,
70         .bDescriptorType =      USB_DT_INTERFACE,
71
72         /* .bInterfaceNumber =  DYNAMIC, */
73         .bInterfaceClass =      USB_CLASS_COMM,
74         .bInterfaceSubClass =   USB_CDC_SUBCLASS_PHONET,
75 };
76
77 static const struct usb_cdc_header_desc
78 pn_header_desc = {
79         .bLength =              sizeof pn_header_desc,
80         .bDescriptorType =      USB_DT_CS_INTERFACE,
81         .bDescriptorSubType =   USB_CDC_HEADER_TYPE,
82         .bcdCDC =               cpu_to_le16(0x0110),
83 };
84
85 static const struct usb_cdc_header_desc
86 pn_phonet_desc = {
87         .bLength =              sizeof pn_phonet_desc,
88         .bDescriptorType =      USB_DT_CS_INTERFACE,
89         .bDescriptorSubType =   USB_CDC_PHONET_TYPE,
90         .bcdCDC =               cpu_to_le16(0x1505), /* ??? */
91 };
92
93 static struct usb_cdc_union_desc
94 pn_union_desc = {
95         .bLength =              sizeof pn_union_desc,
96         .bDescriptorType =      USB_DT_CS_INTERFACE,
97         .bDescriptorSubType =   USB_CDC_UNION_TYPE,
98
99         /* .bMasterInterface0 = DYNAMIC, */
100         /* .bSlaveInterface0 =  DYNAMIC, */
101 };
102
103 static struct usb_interface_descriptor
104 pn_data_nop_intf_desc = {
105         .bLength =              sizeof pn_data_nop_intf_desc,
106         .bDescriptorType =      USB_DT_INTERFACE,
107
108         /* .bInterfaceNumber =  DYNAMIC, */
109         .bAlternateSetting =    0,
110         .bNumEndpoints =        0,
111         .bInterfaceClass =      USB_CLASS_CDC_DATA,
112 };
113
114 static struct usb_interface_descriptor
115 pn_data_intf_desc = {
116         .bLength =              sizeof pn_data_intf_desc,
117         .bDescriptorType =      USB_DT_INTERFACE,
118
119         /* .bInterfaceNumber =  DYNAMIC, */
120         .bAlternateSetting =    1,
121         .bNumEndpoints =        2,
122         .bInterfaceClass =      USB_CLASS_CDC_DATA,
123 };
124
125 static struct usb_endpoint_descriptor
126 pn_fs_sink_desc = {
127         .bLength =              USB_DT_ENDPOINT_SIZE,
128         .bDescriptorType =      USB_DT_ENDPOINT,
129
130         .bEndpointAddress =     USB_DIR_OUT,
131         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
132 };
133
134 static struct usb_endpoint_descriptor
135 pn_hs_sink_desc = {
136         .bLength =              USB_DT_ENDPOINT_SIZE,
137         .bDescriptorType =      USB_DT_ENDPOINT,
138
139         .bEndpointAddress =     USB_DIR_OUT,
140         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
141         .wMaxPacketSize =       cpu_to_le16(512),
142 };
143
144 static struct usb_endpoint_descriptor
145 pn_fs_source_desc = {
146         .bLength =              USB_DT_ENDPOINT_SIZE,
147         .bDescriptorType =      USB_DT_ENDPOINT,
148
149         .bEndpointAddress =     USB_DIR_IN,
150         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
151 };
152
153 static struct usb_endpoint_descriptor
154 pn_hs_source_desc = {
155         .bLength =              USB_DT_ENDPOINT_SIZE,
156         .bDescriptorType =      USB_DT_ENDPOINT,
157
158         .bEndpointAddress =     USB_DIR_IN,
159         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
160         .wMaxPacketSize =       cpu_to_le16(512),
161 };
162
163 static struct usb_descriptor_header *fs_pn_function[] = {
164         (struct usb_descriptor_header *) &pn_control_intf_desc,
165         (struct usb_descriptor_header *) &pn_header_desc,
166         (struct usb_descriptor_header *) &pn_phonet_desc,
167         (struct usb_descriptor_header *) &pn_union_desc,
168         (struct usb_descriptor_header *) &pn_data_nop_intf_desc,
169         (struct usb_descriptor_header *) &pn_data_intf_desc,
170         (struct usb_descriptor_header *) &pn_fs_sink_desc,
171         (struct usb_descriptor_header *) &pn_fs_source_desc,
172         NULL,
173 };
174
175 static struct usb_descriptor_header *hs_pn_function[] = {
176         (struct usb_descriptor_header *) &pn_control_intf_desc,
177         (struct usb_descriptor_header *) &pn_header_desc,
178         (struct usb_descriptor_header *) &pn_phonet_desc,
179         (struct usb_descriptor_header *) &pn_union_desc,
180         (struct usb_descriptor_header *) &pn_data_nop_intf_desc,
181         (struct usb_descriptor_header *) &pn_data_intf_desc,
182         (struct usb_descriptor_header *) &pn_hs_sink_desc,
183         (struct usb_descriptor_header *) &pn_hs_source_desc,
184         NULL,
185 };
186
187 /*-------------------------------------------------------------------------*/
188
189 static int pn_net_open(struct net_device *dev)
190 {
191         netif_wake_queue(dev);
192         return 0;
193 }
194
195 static int pn_net_close(struct net_device *dev)
196 {
197         netif_stop_queue(dev);
198         return 0;
199 }
200
201 static void pn_tx_complete(struct usb_ep *ep, struct usb_request *req)
202 {
203         struct f_phonet *fp = ep->driver_data;
204         struct net_device *dev = fp->dev;
205         struct sk_buff *skb = req->context;
206
207         switch (req->status) {
208         case 0:
209                 dev->stats.tx_packets++;
210                 dev->stats.tx_bytes += skb->len;
211                 break;
212
213         case -ESHUTDOWN: /* disconnected */
214         case -ECONNRESET: /* disabled */
215                 dev->stats.tx_aborted_errors++;
216         default:
217                 dev->stats.tx_errors++;
218         }
219
220         dev_kfree_skb_any(skb);
221         netif_wake_queue(dev);
222 }
223
224 static int pn_net_xmit(struct sk_buff *skb, struct net_device *dev)
225 {
226         struct phonet_port *port = netdev_priv(dev);
227         struct f_phonet *fp;
228         struct usb_request *req;
229         unsigned long flags;
230
231         if (skb->protocol != htons(ETH_P_PHONET))
232                 goto out;
233
234         spin_lock_irqsave(&port->lock, flags);
235         fp = port->usb;
236         if (unlikely(!fp)) /* race with carrier loss */
237                 goto out_unlock;
238
239         req = fp->in_req;
240         req->buf = skb->data;
241         req->length = skb->len;
242         req->complete = pn_tx_complete;
243         req->zero = 1;
244         req->context = skb;
245
246         if (unlikely(usb_ep_queue(fp->in_ep, req, GFP_ATOMIC)))
247                 goto out_unlock;
248
249         netif_stop_queue(dev);
250         skb = NULL;
251
252 out_unlock:
253         spin_unlock_irqrestore(&port->lock, flags);
254 out:
255         if (unlikely(skb)) {
256                 dev_kfree_skb(skb);
257                 dev->stats.tx_dropped++;
258         }
259         return 0;
260 }
261
262 static int pn_net_mtu(struct net_device *dev, int new_mtu)
263 {
264         struct phonet_port *port = netdev_priv(dev);
265         unsigned long flags;
266         int err = -EBUSY;
267
268         if ((new_mtu < PHONET_MIN_MTU) || (new_mtu > PHONET_MAX_MTU))
269                 return -EINVAL;
270
271         spin_lock_irqsave(&port->lock, flags);
272         if (!netif_carrier_ok(dev)) {
273                 dev->mtu = new_mtu;
274                 err = 0;
275         }
276         spin_unlock_irqrestore(&port->lock, flags);
277         return err;
278 }
279
280 static const struct net_device_ops pn_netdev_ops = {
281         .ndo_open       = pn_net_open,
282         .ndo_stop       = pn_net_close,
283         .ndo_start_xmit = pn_net_xmit,
284         .ndo_change_mtu = pn_net_mtu,
285 };
286
287 static void pn_net_setup(struct net_device *dev)
288 {
289         dev->features           = 0;
290         dev->type               = ARPHRD_PHONET;
291         dev->flags              = IFF_POINTOPOINT | IFF_NOARP;
292         dev->mtu                = PHONET_DEV_MTU;
293         dev->hard_header_len    = 1;
294         dev->dev_addr[0]        = PN_MEDIA_USB;
295         dev->addr_len           = 1;
296         dev->tx_queue_len       = 1;
297
298         dev->netdev_ops         = &pn_netdev_ops;
299         dev->destructor         = free_netdev;
300         dev->header_ops         = &phonet_header_ops;
301 }
302
303 /*-------------------------------------------------------------------------*/
304
305 /*
306  * Queue buffer for data from the host
307  */
308 static int
309 pn_rx_submit(struct f_phonet *fp, struct usb_request *req, gfp_t gfp_flags)
310 {
311         struct sk_buff *skb;
312         const size_t size = fp->dev->mtu;
313         int err;
314
315         skb = alloc_skb(size, gfp_flags);
316         if (!skb)
317                 return -ENOMEM;
318
319         req->buf = skb->data;
320         req->length = size;
321         req->context = skb;
322
323         err = usb_ep_queue(fp->out_ep, req, gfp_flags);
324         if (unlikely(err))
325                 dev_kfree_skb_any(skb);
326         return err;
327 }
328
329 static void pn_rx_complete(struct usb_ep *ep, struct usb_request *req)
330 {
331         struct f_phonet *fp = ep->driver_data;
332         struct net_device *dev = fp->dev;
333         struct sk_buff *skb = req->context;
334         int status = req->status;
335
336         switch (status) {
337         case 0:
338                 if (unlikely(!netif_running(dev)))
339                         break;
340                 if (unlikely(req->actual < 1))
341                         break;
342                 skb_put(skb, req->actual);
343                 skb->protocol = htons(ETH_P_PHONET);
344                 skb_reset_mac_header(skb);
345                 __skb_pull(skb, 1);
346                 skb->dev = dev;
347                 dev->stats.rx_packets++;
348                 dev->stats.rx_bytes += skb->len;
349
350                 netif_rx(skb);
351                 skb = NULL;
352                 break;
353
354         /* Do not resubmit in these cases: */
355         case -ESHUTDOWN: /* disconnect */
356         case -ECONNABORTED: /* hw reset */
357         case -ECONNRESET: /* dequeued (unlink or netif down) */
358                 req = NULL;
359                 break;
360
361         /* Do resubmit in these cases: */
362         case -EOVERFLOW: /* request buffer overflow */
363                 dev->stats.rx_over_errors++;
364         default:
365                 dev->stats.rx_errors++;
366                 break;
367         }
368
369         if (skb)
370                 dev_kfree_skb_any(skb);
371         if (req)
372                 pn_rx_submit(fp, req, GFP_ATOMIC);
373 }
374
375 /*-------------------------------------------------------------------------*/
376
377 static void __pn_reset(struct usb_function *f)
378 {
379         struct f_phonet *fp = func_to_pn(f);
380         struct net_device *dev = fp->dev;
381         struct phonet_port *port = netdev_priv(dev);
382
383         netif_carrier_off(dev);
384         port->usb = NULL;
385
386         usb_ep_disable(fp->out_ep);
387         usb_ep_disable(fp->in_ep);
388 }
389
390 static int pn_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
391 {
392         struct f_phonet *fp = func_to_pn(f);
393         struct usb_gadget *gadget = fp->function.config->cdev->gadget;
394
395         if (intf == pn_control_intf_desc.bInterfaceNumber)
396                 /* control interface, no altsetting */
397                 return (alt > 0) ? -EINVAL : 0;
398
399         if (intf == pn_data_intf_desc.bInterfaceNumber) {
400                 struct net_device *dev = fp->dev;
401                 struct phonet_port *port = netdev_priv(dev);
402
403                 /* data intf (0: inactive, 1: active) */
404                 if (alt > 1)
405                         return -EINVAL;
406
407                 spin_lock(&port->lock);
408                 __pn_reset(f);
409                 if (alt == 1) {
410                         struct usb_endpoint_descriptor *out, *in;
411                         int i;
412
413                         out = ep_choose(gadget,
414                                         &pn_hs_sink_desc,
415                                         &pn_fs_sink_desc);
416                         in = ep_choose(gadget,
417                                         &pn_hs_source_desc,
418                                         &pn_fs_source_desc);
419                         usb_ep_enable(fp->out_ep, out);
420                         usb_ep_enable(fp->in_ep, in);
421
422                         port->usb = fp;
423                         fp->out_ep->driver_data = fp;
424                         fp->in_ep->driver_data = fp;
425
426                         netif_carrier_on(dev);
427                         for (i = 0; i < phonet_rxq_size; i++)
428                                 pn_rx_submit(fp, fp->out_reqv[i], GFP_ATOMIC);
429                 }
430                 spin_unlock(&port->lock);
431                 return 0;
432         }
433
434         return -EINVAL;
435 }
436
437 static int pn_get_alt(struct usb_function *f, unsigned intf)
438 {
439         struct f_phonet *fp = func_to_pn(f);
440
441         if (intf == pn_control_intf_desc.bInterfaceNumber)
442                 return 0;
443
444         if (intf == pn_data_intf_desc.bInterfaceNumber) {
445                 struct phonet_port *port = netdev_priv(fp->dev);
446                 u8 alt;
447
448                 spin_lock(&port->lock);
449                 alt = port->usb != NULL;
450                 spin_unlock(&port->lock);
451                 return alt;
452         }
453
454         return -EINVAL;
455 }
456
457 static void pn_disconnect(struct usb_function *f)
458 {
459         struct f_phonet *fp = func_to_pn(f);
460         struct phonet_port *port = netdev_priv(fp->dev);
461         unsigned long flags;
462
463         /* remain disabled until set_alt */
464         spin_lock_irqsave(&port->lock, flags);
465         __pn_reset(f);
466         spin_unlock_irqrestore(&port->lock, flags);
467 }
468
469 /*-------------------------------------------------------------------------*/
470
471 static __init
472 int pn_bind(struct usb_configuration *c, struct usb_function *f)
473 {
474         struct usb_composite_dev *cdev = c->cdev;
475         struct usb_gadget *gadget = cdev->gadget;
476         struct f_phonet *fp = func_to_pn(f);
477         struct usb_ep *ep;
478         int status, i;
479
480         /* Reserve interface IDs */
481         status = usb_interface_id(c, f);
482         if (status < 0)
483                 goto err;
484         pn_control_intf_desc.bInterfaceNumber = status;
485         pn_union_desc.bMasterInterface0 = status;
486
487         status = usb_interface_id(c, f);
488         if (status < 0)
489                 goto err;
490         pn_data_nop_intf_desc.bInterfaceNumber = status;
491         pn_data_intf_desc.bInterfaceNumber = status;
492         pn_union_desc.bSlaveInterface0 = status;
493
494         /* Reserve endpoints */
495         status = -ENODEV;
496         ep = usb_ep_autoconfig(gadget, &pn_fs_sink_desc);
497         if (!ep)
498                 goto err;
499         fp->out_ep = ep;
500         ep->driver_data = fp; /* Claim */
501
502         ep = usb_ep_autoconfig(gadget, &pn_fs_source_desc);
503         if (!ep)
504                 goto err;
505         fp->in_ep = ep;
506         ep->driver_data = fp; /* Claim */
507
508         pn_hs_sink_desc.bEndpointAddress =
509                 pn_fs_sink_desc.bEndpointAddress;
510         pn_hs_source_desc.bEndpointAddress =
511                 pn_fs_source_desc.bEndpointAddress;
512
513         /* Do not try to bind Phonet twice... */
514         fp->function.descriptors = fs_pn_function;
515         fp->function.hs_descriptors = hs_pn_function;
516
517         /* Incoming USB requests */
518         status = -ENOMEM;
519         for (i = 0; i < phonet_rxq_size; i++) {
520                 struct usb_request *req;
521
522                 req = usb_ep_alloc_request(fp->out_ep, GFP_KERNEL);
523                 if (!req)
524                         goto err;
525
526                 req->complete = pn_rx_complete;
527                 fp->out_reqv[i] = req;
528         }
529
530         /* Outgoing USB requests */
531         fp->in_req = usb_ep_alloc_request(fp->in_ep, GFP_KERNEL);
532         if (!fp->in_req)
533                 goto err;
534
535         INFO(cdev, "USB CDC Phonet function\n");
536         INFO(cdev, "using %s, OUT %s, IN %s\n", cdev->gadget->name,
537                 fp->out_ep->name, fp->in_ep->name);
538         return 0;
539
540 err:
541         if (fp->out_ep)
542                 fp->out_ep->driver_data = NULL;
543         if (fp->in_ep)
544                 fp->in_ep->driver_data = NULL;
545         ERROR(cdev, "USB CDC Phonet: cannot autoconfigure\n");
546         return status;
547 }
548
549 static void
550 pn_unbind(struct usb_configuration *c, struct usb_function *f)
551 {
552         struct f_phonet *fp = func_to_pn(f);
553         int i;
554
555         /* We are already disconnected */
556         if (fp->in_req)
557                 usb_ep_free_request(fp->in_ep, fp->in_req);
558         for (i = 0; i < phonet_rxq_size; i++)
559                 if (fp->out_reqv[i])
560                         usb_ep_free_request(fp->out_ep, fp->out_reqv[i]);
561
562         kfree(fp);
563 }
564
565 /*-------------------------------------------------------------------------*/
566
567 static struct net_device *dev;
568
569 int __init phonet_bind_config(struct usb_configuration *c)
570 {
571         struct f_phonet *fp;
572         int err, size;
573
574         size = sizeof(*fp) + (phonet_rxq_size * sizeof(struct usb_request *));
575         fp = kzalloc(size, GFP_KERNEL);
576         if (!fp)
577                 return -ENOMEM;
578
579         fp->dev = dev;
580         fp->function.name = "phonet";
581         fp->function.bind = pn_bind;
582         fp->function.unbind = pn_unbind;
583         fp->function.set_alt = pn_set_alt;
584         fp->function.get_alt = pn_get_alt;
585         fp->function.disable = pn_disconnect;
586
587         err = usb_add_function(c, &fp->function);
588         if (err)
589                 kfree(fp);
590         return err;
591 }
592
593 int __init gphonet_setup(struct usb_gadget *gadget)
594 {
595         struct phonet_port *port;
596         int err;
597
598         /* Create net device */
599         BUG_ON(dev);
600         dev = alloc_netdev(sizeof(*port), "upnlink%d", pn_net_setup);
601         if (!dev)
602                 return -ENOMEM;
603
604         port = netdev_priv(dev);
605         spin_lock_init(&port->lock);
606         netif_carrier_off(dev);
607         SET_NETDEV_DEV(dev, &gadget->dev);
608
609         err = register_netdev(dev);
610         if (err)
611                 free_netdev(dev);
612         return err;
613 }
614
615 void gphonet_cleanup(void)
616 {
617         unregister_netdev(dev);
618 }