blob: f2b781773eed39da911bd7a6a4f4f5500e46fbbf [file] [log] [blame]
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -08001/*
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.
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -080011 */
12
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000013#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -080015#include <linux/kernel.h>
Andrzej Pietrasiewiczfcbdf122013-05-23 10:51:11 +020016#include <linux/module.h>
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -080017#include <linux/device.h>
18
19#include <linux/netdevice.h>
20#include <linux/if_ether.h>
21#include <linux/if_phonet.h>
22#include <linux/if_arp.h>
23
24#include <linux/usb/ch9.h>
25#include <linux/usb/cdc.h>
26#include <linux/usb/composite.h>
27
28#include "u_phonet.h"
Andrzej Pietrasiewicz83408742013-05-23 10:51:15 +020029#include "u_ether.h"
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -080030
31#define PN_MEDIA_USB 0x1B
Rémi Denis-Courmontb91cd142009-08-06 21:56:44 +000032#define MAXPACKET 512
33#if (PAGE_SIZE % MAXPACKET)
34#error MAXPACKET must divide PAGE_SIZE!
35#endif
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -080036
37/*-------------------------------------------------------------------------*/
38
39struct phonet_port {
40 struct f_phonet *usb;
41 spinlock_t lock;
42};
43
44struct f_phonet {
45 struct usb_function function;
Rémi Denis-Courmontb91cd142009-08-06 21:56:44 +000046 struct {
47 struct sk_buff *skb;
48 spinlock_t lock;
49 } rx;
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -080050 struct net_device *dev;
51 struct usb_ep *in_ep, *out_ep;
52
53 struct usb_request *in_req;
54 struct usb_request *out_reqv[0];
55};
56
Rémi Denis-Courmontb91cd142009-08-06 21:56:44 +000057static int phonet_rxq_size = 17;
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -080058
59static inline struct f_phonet *func_to_pn(struct usb_function *f)
60{
61 return container_of(f, struct f_phonet, function);
62}
63
64/*-------------------------------------------------------------------------*/
65
66#define USB_CDC_SUBCLASS_PHONET 0xfe
67#define USB_CDC_PHONET_TYPE 0xab
68
69static struct usb_interface_descriptor
70pn_control_intf_desc = {
71 .bLength = sizeof pn_control_intf_desc,
72 .bDescriptorType = USB_DT_INTERFACE,
73
74 /* .bInterfaceNumber = DYNAMIC, */
75 .bInterfaceClass = USB_CLASS_COMM,
76 .bInterfaceSubClass = USB_CDC_SUBCLASS_PHONET,
77};
78
79static const struct usb_cdc_header_desc
80pn_header_desc = {
81 .bLength = sizeof pn_header_desc,
82 .bDescriptorType = USB_DT_CS_INTERFACE,
83 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
Harvey Harrison551509d2009-02-11 14:11:36 -080084 .bcdCDC = cpu_to_le16(0x0110),
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -080085};
86
87static const struct usb_cdc_header_desc
88pn_phonet_desc = {
89 .bLength = sizeof pn_phonet_desc,
90 .bDescriptorType = USB_DT_CS_INTERFACE,
91 .bDescriptorSubType = USB_CDC_PHONET_TYPE,
Harvey Harrison551509d2009-02-11 14:11:36 -080092 .bcdCDC = cpu_to_le16(0x1505), /* ??? */
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -080093};
94
95static struct usb_cdc_union_desc
96pn_union_desc = {
97 .bLength = sizeof pn_union_desc,
98 .bDescriptorType = USB_DT_CS_INTERFACE,
99 .bDescriptorSubType = USB_CDC_UNION_TYPE,
100
101 /* .bMasterInterface0 = DYNAMIC, */
102 /* .bSlaveInterface0 = DYNAMIC, */
103};
104
105static struct usb_interface_descriptor
106pn_data_nop_intf_desc = {
107 .bLength = sizeof pn_data_nop_intf_desc,
108 .bDescriptorType = USB_DT_INTERFACE,
109
110 /* .bInterfaceNumber = DYNAMIC, */
111 .bAlternateSetting = 0,
112 .bNumEndpoints = 0,
113 .bInterfaceClass = USB_CLASS_CDC_DATA,
114};
115
116static struct usb_interface_descriptor
117pn_data_intf_desc = {
118 .bLength = sizeof pn_data_intf_desc,
119 .bDescriptorType = USB_DT_INTERFACE,
120
121 /* .bInterfaceNumber = DYNAMIC, */
122 .bAlternateSetting = 1,
123 .bNumEndpoints = 2,
124 .bInterfaceClass = USB_CLASS_CDC_DATA,
125};
126
127static struct usb_endpoint_descriptor
128pn_fs_sink_desc = {
129 .bLength = USB_DT_ENDPOINT_SIZE,
130 .bDescriptorType = USB_DT_ENDPOINT,
131
132 .bEndpointAddress = USB_DIR_OUT,
133 .bmAttributes = USB_ENDPOINT_XFER_BULK,
134};
135
136static struct usb_endpoint_descriptor
137pn_hs_sink_desc = {
138 .bLength = USB_DT_ENDPOINT_SIZE,
139 .bDescriptorType = USB_DT_ENDPOINT,
140
141 .bEndpointAddress = USB_DIR_OUT,
142 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Rémi Denis-Courmontb91cd142009-08-06 21:56:44 +0000143 .wMaxPacketSize = cpu_to_le16(MAXPACKET),
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800144};
145
146static struct usb_endpoint_descriptor
147pn_fs_source_desc = {
148 .bLength = USB_DT_ENDPOINT_SIZE,
149 .bDescriptorType = USB_DT_ENDPOINT,
150
151 .bEndpointAddress = USB_DIR_IN,
152 .bmAttributes = USB_ENDPOINT_XFER_BULK,
153};
154
155static struct usb_endpoint_descriptor
156pn_hs_source_desc = {
157 .bLength = USB_DT_ENDPOINT_SIZE,
158 .bDescriptorType = USB_DT_ENDPOINT,
159
160 .bEndpointAddress = USB_DIR_IN,
161 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800162 .wMaxPacketSize = cpu_to_le16(512),
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800163};
164
165static struct usb_descriptor_header *fs_pn_function[] = {
166 (struct usb_descriptor_header *) &pn_control_intf_desc,
167 (struct usb_descriptor_header *) &pn_header_desc,
168 (struct usb_descriptor_header *) &pn_phonet_desc,
169 (struct usb_descriptor_header *) &pn_union_desc,
170 (struct usb_descriptor_header *) &pn_data_nop_intf_desc,
171 (struct usb_descriptor_header *) &pn_data_intf_desc,
172 (struct usb_descriptor_header *) &pn_fs_sink_desc,
173 (struct usb_descriptor_header *) &pn_fs_source_desc,
174 NULL,
175};
176
177static struct usb_descriptor_header *hs_pn_function[] = {
178 (struct usb_descriptor_header *) &pn_control_intf_desc,
179 (struct usb_descriptor_header *) &pn_header_desc,
180 (struct usb_descriptor_header *) &pn_phonet_desc,
181 (struct usb_descriptor_header *) &pn_union_desc,
182 (struct usb_descriptor_header *) &pn_data_nop_intf_desc,
183 (struct usb_descriptor_header *) &pn_data_intf_desc,
184 (struct usb_descriptor_header *) &pn_hs_sink_desc,
185 (struct usb_descriptor_header *) &pn_hs_source_desc,
186 NULL,
187};
188
189/*-------------------------------------------------------------------------*/
190
191static int pn_net_open(struct net_device *dev)
192{
Rémi Denis-Courmontc69367f2009-06-01 01:18:56 +0000193 netif_wake_queue(dev);
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800194 return 0;
195}
196
197static int pn_net_close(struct net_device *dev)
198{
199 netif_stop_queue(dev);
200 return 0;
201}
202
203static void pn_tx_complete(struct usb_ep *ep, struct usb_request *req)
204{
205 struct f_phonet *fp = ep->driver_data;
206 struct net_device *dev = fp->dev;
207 struct sk_buff *skb = req->context;
208
209 switch (req->status) {
210 case 0:
211 dev->stats.tx_packets++;
212 dev->stats.tx_bytes += skb->len;
213 break;
214
215 case -ESHUTDOWN: /* disconnected */
216 case -ECONNRESET: /* disabled */
217 dev->stats.tx_aborted_errors++;
218 default:
219 dev->stats.tx_errors++;
220 }
221
222 dev_kfree_skb_any(skb);
Rémi Denis-Courmontc69367f2009-06-01 01:18:56 +0000223 netif_wake_queue(dev);
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800224}
225
226static int pn_net_xmit(struct sk_buff *skb, struct net_device *dev)
227{
228 struct phonet_port *port = netdev_priv(dev);
229 struct f_phonet *fp;
230 struct usb_request *req;
231 unsigned long flags;
232
233 if (skb->protocol != htons(ETH_P_PHONET))
234 goto out;
235
236 spin_lock_irqsave(&port->lock, flags);
237 fp = port->usb;
238 if (unlikely(!fp)) /* race with carrier loss */
239 goto out_unlock;
240
241 req = fp->in_req;
242 req->buf = skb->data;
243 req->length = skb->len;
244 req->complete = pn_tx_complete;
245 req->zero = 1;
246 req->context = skb;
247
248 if (unlikely(usb_ep_queue(fp->in_ep, req, GFP_ATOMIC)))
249 goto out_unlock;
250
251 netif_stop_queue(dev);
252 skb = NULL;
253
254out_unlock:
255 spin_unlock_irqrestore(&port->lock, flags);
256out:
257 if (unlikely(skb)) {
Rémi Denis-Courmontfa202592009-06-01 01:18:55 +0000258 dev_kfree_skb(skb);
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800259 dev->stats.tx_dropped++;
260 }
Patrick McHardy6ed10652009-06-23 06:03:08 +0000261 return NETDEV_TX_OK;
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800262}
263
264static int pn_net_mtu(struct net_device *dev, int new_mtu)
265{
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800266 if ((new_mtu < PHONET_MIN_MTU) || (new_mtu > PHONET_MAX_MTU))
267 return -EINVAL;
Rémi Denis-Courmont5da63cc2009-08-06 21:56:43 +0000268 dev->mtu = new_mtu;
269 return 0;
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800270}
271
Stephen Hemmingerab638e62009-01-07 17:24:34 -0800272static const struct net_device_ops pn_netdev_ops = {
273 .ndo_open = pn_net_open,
274 .ndo_stop = pn_net_close,
275 .ndo_start_xmit = pn_net_xmit,
276 .ndo_change_mtu = pn_net_mtu,
277};
278
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800279static void pn_net_setup(struct net_device *dev)
280{
281 dev->features = 0;
282 dev->type = ARPHRD_PHONET;
283 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
284 dev->mtu = PHONET_DEV_MTU;
285 dev->hard_header_len = 1;
286 dev->dev_addr[0] = PN_MEDIA_USB;
287 dev->addr_len = 1;
288 dev->tx_queue_len = 1;
289
Stephen Hemmingerab638e62009-01-07 17:24:34 -0800290 dev->netdev_ops = &pn_netdev_ops;
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800291 dev->destructor = free_netdev;
292 dev->header_ops = &phonet_header_ops;
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800293}
294
295/*-------------------------------------------------------------------------*/
296
297/*
298 * Queue buffer for data from the host
299 */
300static int
301pn_rx_submit(struct f_phonet *fp, struct usb_request *req, gfp_t gfp_flags)
302{
Rémi Denis-Courmontb91cd142009-08-06 21:56:44 +0000303 struct page *page;
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800304 int err;
305
Mel Gorman06140022012-07-31 16:44:24 -0700306 page = __skb_alloc_page(gfp_flags | __GFP_NOMEMALLOC, NULL);
Rémi Denis-Courmontb91cd142009-08-06 21:56:44 +0000307 if (!page)
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800308 return -ENOMEM;
309
Rémi Denis-Courmontb91cd142009-08-06 21:56:44 +0000310 req->buf = page_address(page);
311 req->length = PAGE_SIZE;
312 req->context = page;
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800313
314 err = usb_ep_queue(fp->out_ep, req, gfp_flags);
315 if (unlikely(err))
Eric Dumazet1f2149c2011-11-22 10:57:41 +0000316 put_page(page);
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800317 return err;
318}
319
320static void pn_rx_complete(struct usb_ep *ep, struct usb_request *req)
321{
322 struct f_phonet *fp = ep->driver_data;
323 struct net_device *dev = fp->dev;
Rémi Denis-Courmontb91cd142009-08-06 21:56:44 +0000324 struct page *page = req->context;
325 struct sk_buff *skb;
326 unsigned long flags;
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800327 int status = req->status;
328
329 switch (status) {
330 case 0:
Rémi Denis-Courmontb91cd142009-08-06 21:56:44 +0000331 spin_lock_irqsave(&fp->rx.lock, flags);
332 skb = fp->rx.skb;
333 if (!skb)
334 skb = fp->rx.skb = netdev_alloc_skb(dev, 12);
335 if (req->actual < req->length) /* Last fragment */
336 fp->rx.skb = NULL;
337 spin_unlock_irqrestore(&fp->rx.lock, flags);
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800338
Rémi Denis-Courmontb91cd142009-08-06 21:56:44 +0000339 if (unlikely(!skb))
340 break;
Rémi Denis-Courmontf5a45322011-02-23 02:51:33 +0000341
342 if (skb->len == 0) { /* First fragment */
343 skb->protocol = htons(ETH_P_PHONET);
344 skb_reset_mac_header(skb);
345 /* Can't use pskb_pull() on page in IRQ */
346 memcpy(skb_put(skb, 1), page_address(page), 1);
347 }
348
349 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
Eric Dumazet21dcda62012-03-27 03:04:02 +0000350 skb->len <= 1, req->actual, PAGE_SIZE);
Rémi Denis-Courmontb91cd142009-08-06 21:56:44 +0000351 page = NULL;
352
353 if (req->actual < req->length) { /* Last fragment */
Rémi Denis-Courmontb91cd142009-08-06 21:56:44 +0000354 skb->dev = dev;
355 dev->stats.rx_packets++;
356 dev->stats.rx_bytes += skb->len;
357
358 netif_rx(skb);
359 }
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800360 break;
361
362 /* Do not resubmit in these cases: */
363 case -ESHUTDOWN: /* disconnect */
364 case -ECONNABORTED: /* hw reset */
365 case -ECONNRESET: /* dequeued (unlink or netif down) */
366 req = NULL;
367 break;
368
369 /* Do resubmit in these cases: */
370 case -EOVERFLOW: /* request buffer overflow */
371 dev->stats.rx_over_errors++;
372 default:
373 dev->stats.rx_errors++;
374 break;
375 }
376
Rémi Denis-Courmontb91cd142009-08-06 21:56:44 +0000377 if (page)
Eric Dumazet1f2149c2011-11-22 10:57:41 +0000378 put_page(page);
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800379 if (req)
Eric Dumazet1f2149c2011-11-22 10:57:41 +0000380 pn_rx_submit(fp, req, GFP_ATOMIC | __GFP_COLD);
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800381}
382
383/*-------------------------------------------------------------------------*/
384
385static void __pn_reset(struct usb_function *f)
386{
387 struct f_phonet *fp = func_to_pn(f);
388 struct net_device *dev = fp->dev;
389 struct phonet_port *port = netdev_priv(dev);
390
391 netif_carrier_off(dev);
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800392 port->usb = NULL;
393
394 usb_ep_disable(fp->out_ep);
395 usb_ep_disable(fp->in_ep);
Rémi Denis-Courmontb91cd142009-08-06 21:56:44 +0000396 if (fp->rx.skb) {
397 dev_kfree_skb_irq(fp->rx.skb);
398 fp->rx.skb = NULL;
399 }
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800400}
401
402static int pn_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
403{
404 struct f_phonet *fp = func_to_pn(f);
405 struct usb_gadget *gadget = fp->function.config->cdev->gadget;
406
407 if (intf == pn_control_intf_desc.bInterfaceNumber)
408 /* control interface, no altsetting */
409 return (alt > 0) ? -EINVAL : 0;
410
411 if (intf == pn_data_intf_desc.bInterfaceNumber) {
412 struct net_device *dev = fp->dev;
413 struct phonet_port *port = netdev_priv(dev);
414
415 /* data intf (0: inactive, 1: active) */
416 if (alt > 1)
417 return -EINVAL;
418
419 spin_lock(&port->lock);
420 __pn_reset(f);
421 if (alt == 1) {
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800422 int i;
423
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300424 if (config_ep_by_speed(gadget, f, fp->in_ep) ||
425 config_ep_by_speed(gadget, f, fp->out_ep)) {
426 fp->in_ep->desc = NULL;
427 fp->out_ep->desc = NULL;
Sebastian Andrzej Siewiorbb8070c2011-08-05 22:14:46 +0200428 spin_unlock(&port->lock);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300429 return -EINVAL;
430 }
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300431 usb_ep_enable(fp->out_ep);
432 usb_ep_enable(fp->in_ep);
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800433
434 port->usb = fp;
435 fp->out_ep->driver_data = fp;
436 fp->in_ep->driver_data = fp;
437
438 netif_carrier_on(dev);
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800439 for (i = 0; i < phonet_rxq_size; i++)
Eric Dumazet1f2149c2011-11-22 10:57:41 +0000440 pn_rx_submit(fp, fp->out_reqv[i], GFP_ATOMIC | __GFP_COLD);
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800441 }
442 spin_unlock(&port->lock);
443 return 0;
444 }
445
446 return -EINVAL;
447}
448
449static int pn_get_alt(struct usb_function *f, unsigned intf)
450{
451 struct f_phonet *fp = func_to_pn(f);
452
453 if (intf == pn_control_intf_desc.bInterfaceNumber)
454 return 0;
455
456 if (intf == pn_data_intf_desc.bInterfaceNumber) {
457 struct phonet_port *port = netdev_priv(fp->dev);
458 u8 alt;
459
460 spin_lock(&port->lock);
461 alt = port->usb != NULL;
462 spin_unlock(&port->lock);
463 return alt;
464 }
465
466 return -EINVAL;
467}
468
469static void pn_disconnect(struct usb_function *f)
470{
471 struct f_phonet *fp = func_to_pn(f);
472 struct phonet_port *port = netdev_priv(fp->dev);
473 unsigned long flags;
474
475 /* remain disabled until set_alt */
476 spin_lock_irqsave(&port->lock, flags);
477 __pn_reset(f);
478 spin_unlock_irqrestore(&port->lock, flags);
479}
480
481/*-------------------------------------------------------------------------*/
482
Andrzej Pietrasiewiczfcbdf122013-05-23 10:51:11 +0200483static int pn_bind(struct usb_configuration *c, struct usb_function *f)
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800484{
485 struct usb_composite_dev *cdev = c->cdev;
486 struct usb_gadget *gadget = cdev->gadget;
487 struct f_phonet *fp = func_to_pn(f);
488 struct usb_ep *ep;
489 int status, i;
490
Andrzej Pietrasiewiczfcbdf122013-05-23 10:51:11 +0200491 struct f_phonet_opts *phonet_opts;
492
493 phonet_opts = container_of(f->fi, struct f_phonet_opts, func_inst);
494
495 /*
496 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
497 * configurations are bound in sequence with list_for_each_entry,
498 * in each configuration its functions are bound in sequence
499 * with list_for_each_entry, so we assume no race condition
500 * with regard to phonet_opts->bound access
501 */
502 if (!phonet_opts->bound) {
503 gphonet_set_gadget(phonet_opts->net, gadget);
504 status = gphonet_register_netdev(phonet_opts->net);
505 if (status)
506 return status;
507 phonet_opts->bound = true;
508 }
Andrzej Pietrasiewiczfcbdf122013-05-23 10:51:11 +0200509
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800510 /* Reserve interface IDs */
511 status = usb_interface_id(c, f);
512 if (status < 0)
513 goto err;
514 pn_control_intf_desc.bInterfaceNumber = status;
515 pn_union_desc.bMasterInterface0 = status;
516
517 status = usb_interface_id(c, f);
518 if (status < 0)
519 goto err;
520 pn_data_nop_intf_desc.bInterfaceNumber = status;
521 pn_data_intf_desc.bInterfaceNumber = status;
522 pn_union_desc.bSlaveInterface0 = status;
523
524 /* Reserve endpoints */
525 status = -ENODEV;
526 ep = usb_ep_autoconfig(gadget, &pn_fs_sink_desc);
527 if (!ep)
528 goto err;
529 fp->out_ep = ep;
530 ep->driver_data = fp; /* Claim */
531
532 ep = usb_ep_autoconfig(gadget, &pn_fs_source_desc);
533 if (!ep)
534 goto err;
535 fp->in_ep = ep;
536 ep->driver_data = fp; /* Claim */
537
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200538 pn_hs_sink_desc.bEndpointAddress = pn_fs_sink_desc.bEndpointAddress;
539 pn_hs_source_desc.bEndpointAddress = pn_fs_source_desc.bEndpointAddress;
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800540
541 /* Do not try to bind Phonet twice... */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200542 status = usb_assign_descriptors(f, fs_pn_function, hs_pn_function,
543 NULL);
544 if (status)
545 goto err;
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800546
547 /* Incoming USB requests */
548 status = -ENOMEM;
549 for (i = 0; i < phonet_rxq_size; i++) {
550 struct usb_request *req;
551
552 req = usb_ep_alloc_request(fp->out_ep, GFP_KERNEL);
553 if (!req)
Sebastian Andrzej Siewiord0eca712012-10-22 22:15:04 +0200554 goto err_req;
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800555
556 req->complete = pn_rx_complete;
557 fp->out_reqv[i] = req;
558 }
559
560 /* Outgoing USB requests */
561 fp->in_req = usb_ep_alloc_request(fp->in_ep, GFP_KERNEL);
562 if (!fp->in_req)
Sebastian Andrzej Siewiord0eca712012-10-22 22:15:04 +0200563 goto err_req;
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800564
565 INFO(cdev, "USB CDC Phonet function\n");
566 INFO(cdev, "using %s, OUT %s, IN %s\n", cdev->gadget->name,
567 fp->out_ep->name, fp->in_ep->name);
568 return 0;
569
Sebastian Andrzej Siewiord0eca712012-10-22 22:15:04 +0200570err_req:
571 for (i = 0; i < phonet_rxq_size && fp->out_reqv[i]; i++)
572 usb_ep_free_request(fp->out_ep, fp->out_reqv[i]);
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800573err:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200574 usb_free_all_descriptors(f);
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800575 if (fp->out_ep)
576 fp->out_ep->driver_data = NULL;
577 if (fp->in_ep)
578 fp->in_ep->driver_data = NULL;
579 ERROR(cdev, "USB CDC Phonet: cannot autoconfigure\n");
580 return status;
581}
582
Andrzej Pietrasiewicz83408742013-05-23 10:51:15 +0200583static inline struct f_phonet_opts *to_f_phonet_opts(struct config_item *item)
584{
585 return container_of(to_config_group(item), struct f_phonet_opts,
586 func_inst.group);
587}
588
589CONFIGFS_ATTR_STRUCT(f_phonet_opts);
590static ssize_t f_phonet_attr_show(struct config_item *item,
591 struct configfs_attribute *attr,
592 char *page)
593{
594 struct f_phonet_opts *opts = to_f_phonet_opts(item);
595 struct f_phonet_opts_attribute *f_phonet_opts_attr =
596 container_of(attr, struct f_phonet_opts_attribute, attr);
597 ssize_t ret = 0;
598
599 if (f_phonet_opts_attr->show)
600 ret = f_phonet_opts_attr->show(opts, page);
601 return ret;
602}
603
604static void phonet_attr_release(struct config_item *item)
605{
606 struct f_phonet_opts *opts = to_f_phonet_opts(item);
607
608 usb_put_function_instance(&opts->func_inst);
609}
610
611static struct configfs_item_operations phonet_item_ops = {
612 .release = phonet_attr_release,
613 .show_attribute = f_phonet_attr_show,
614};
615
616static ssize_t f_phonet_ifname_show(struct f_phonet_opts *opts, char *page)
617{
618 return gether_get_ifname(opts->net, page, PAGE_SIZE);
619}
620
621static struct f_phonet_opts_attribute f_phonet_ifname =
622 __CONFIGFS_ATTR_RO(ifname, f_phonet_ifname_show);
623
624static struct configfs_attribute *phonet_attrs[] = {
625 &f_phonet_ifname.attr,
626 NULL,
627};
628
629static struct config_item_type phonet_func_type = {
630 .ct_item_ops = &phonet_item_ops,
631 .ct_attrs = phonet_attrs,
632 .ct_owner = THIS_MODULE,
633};
634
Andrzej Pietrasiewiczfcbdf122013-05-23 10:51:11 +0200635static void phonet_free_inst(struct usb_function_instance *f)
636{
637 struct f_phonet_opts *opts;
638
639 opts = container_of(f, struct f_phonet_opts, func_inst);
640 if (opts->bound)
641 gphonet_cleanup(opts->net);
642 else
643 free_netdev(opts->net);
644 kfree(opts);
645}
646
647static struct usb_function_instance *phonet_alloc_inst(void)
648{
649 struct f_phonet_opts *opts;
650
651 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
652 if (!opts)
653 return ERR_PTR(-ENOMEM);
654
655 opts->func_inst.free_func_inst = phonet_free_inst;
656 opts->net = gphonet_setup_default();
Andrzej Pietrasiewicz3b45b2a2013-07-25 09:13:18 +0200657 if (IS_ERR(opts->net)) {
658 struct net_device *net = opts->net;
659 kfree(opts);
660 return ERR_CAST(net);
661 }
Andrzej Pietrasiewiczfcbdf122013-05-23 10:51:11 +0200662
Andrzej Pietrasiewicz83408742013-05-23 10:51:15 +0200663 config_group_init_type_name(&opts->func_inst.group, "",
664 &phonet_func_type);
665
Andrzej Pietrasiewiczfcbdf122013-05-23 10:51:11 +0200666 return &opts->func_inst;
667}
668
669static void phonet_free(struct usb_function *f)
670{
671 struct f_phonet *phonet;
672
673 phonet = func_to_pn(f);
674 kfree(phonet);
675}
676
677static void pn_unbind(struct usb_configuration *c, struct usb_function *f)
678{
679 struct f_phonet *fp = func_to_pn(f);
680 int i;
681
682 /* We are already disconnected */
683 if (fp->in_req)
684 usb_ep_free_request(fp->in_ep, fp->in_req);
685 for (i = 0; i < phonet_rxq_size; i++)
686 if (fp->out_reqv[i])
687 usb_ep_free_request(fp->out_ep, fp->out_reqv[i]);
688
689 usb_free_all_descriptors(f);
690}
691
Jingoo Hanc3af8222013-12-16 18:44:07 +0900692static struct usb_function *phonet_alloc(struct usb_function_instance *fi)
Andrzej Pietrasiewiczfcbdf122013-05-23 10:51:11 +0200693{
694 struct f_phonet *fp;
695 struct f_phonet_opts *opts;
696 int size;
697
698 size = sizeof(*fp) + (phonet_rxq_size * sizeof(struct usb_request *));
699 fp = kzalloc(size, GFP_KERNEL);
700 if (!fp)
701 return ERR_PTR(-ENOMEM);
702
703 opts = container_of(fi, struct f_phonet_opts, func_inst);
704
705 fp->dev = opts->net;
706 fp->function.name = "phonet";
707 fp->function.bind = pn_bind;
708 fp->function.unbind = pn_unbind;
709 fp->function.set_alt = pn_set_alt;
710 fp->function.get_alt = pn_get_alt;
711 fp->function.disable = pn_disconnect;
712 fp->function.free_func = phonet_free;
713 spin_lock_init(&fp->rx.lock);
714
715 return &fp->function;
716}
717
718struct net_device *gphonet_setup_default(void)
719{
720 struct net_device *dev;
721 struct phonet_port *port;
722
723 /* Create net device */
724 dev = alloc_netdev(sizeof(*port), "upnlink%d", pn_net_setup);
725 if (!dev)
726 return ERR_PTR(-ENOMEM);
727
728 port = netdev_priv(dev);
729 spin_lock_init(&port->lock);
730 netif_carrier_off(dev);
731
732 return dev;
733}
734
735void gphonet_set_gadget(struct net_device *net, struct usb_gadget *g)
736{
737 SET_NETDEV_DEV(net, &g->dev);
738}
739
740int gphonet_register_netdev(struct net_device *net)
741{
742 int status;
743
744 status = register_netdev(net);
745 if (status)
746 free_netdev(net);
747
748 return status;
749}
750
Andrzej Pietrasiewicz0189e632013-05-23 10:51:10 +0200751void gphonet_cleanup(struct net_device *dev)
Rémi Denis-Courmont9732d522008-12-17 15:49:09 -0800752{
753 unregister_netdev(dev);
754}
Andrzej Pietrasiewicz03830702013-05-23 10:51:13 +0200755
756DECLARE_USB_FUNCTION_INIT(phonet, phonet_alloc_inst, phonet_alloc);
757MODULE_AUTHOR("Rémi Denis-Courmont");
758MODULE_LICENSE("GPL");