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