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