f_phonet.c 17 KB

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