f_phonet.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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 int pn_net_mtu(struct net_device *dev, int new_mtu)
  217. {
  218. if ((new_mtu < PHONET_MIN_MTU) || (new_mtu > PHONET_MAX_MTU))
  219. return -EINVAL;
  220. dev->mtu = new_mtu;
  221. return 0;
  222. }
  223. static const struct net_device_ops pn_netdev_ops = {
  224. .ndo_open = pn_net_open,
  225. .ndo_stop = pn_net_close,
  226. .ndo_start_xmit = pn_net_xmit,
  227. .ndo_change_mtu = pn_net_mtu,
  228. };
  229. static void pn_net_setup(struct net_device *dev)
  230. {
  231. dev->features = 0;
  232. dev->type = ARPHRD_PHONET;
  233. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  234. dev->mtu = PHONET_DEV_MTU;
  235. dev->hard_header_len = 1;
  236. dev->dev_addr[0] = PN_MEDIA_USB;
  237. dev->addr_len = 1;
  238. dev->tx_queue_len = 1;
  239. dev->netdev_ops = &pn_netdev_ops;
  240. dev->destructor = free_netdev;
  241. dev->header_ops = &phonet_header_ops;
  242. }
  243. /*-------------------------------------------------------------------------*/
  244. /*
  245. * Queue buffer for data from the host
  246. */
  247. static int
  248. pn_rx_submit(struct f_phonet *fp, struct usb_request *req, gfp_t gfp_flags)
  249. {
  250. struct page *page;
  251. int err;
  252. page = __dev_alloc_page(gfp_flags | __GFP_NOMEMALLOC);
  253. if (!page)
  254. return -ENOMEM;
  255. req->buf = page_address(page);
  256. req->length = PAGE_SIZE;
  257. req->context = page;
  258. err = usb_ep_queue(fp->out_ep, req, gfp_flags);
  259. if (unlikely(err))
  260. put_page(page);
  261. return err;
  262. }
  263. static void pn_rx_complete(struct usb_ep *ep, struct usb_request *req)
  264. {
  265. struct f_phonet *fp = ep->driver_data;
  266. struct net_device *dev = fp->dev;
  267. struct page *page = req->context;
  268. struct sk_buff *skb;
  269. unsigned long flags;
  270. int status = req->status;
  271. switch (status) {
  272. case 0:
  273. spin_lock_irqsave(&fp->rx.lock, flags);
  274. skb = fp->rx.skb;
  275. if (!skb)
  276. skb = fp->rx.skb = netdev_alloc_skb(dev, 12);
  277. if (req->actual < req->length) /* Last fragment */
  278. fp->rx.skb = NULL;
  279. spin_unlock_irqrestore(&fp->rx.lock, flags);
  280. if (unlikely(!skb))
  281. break;
  282. if (skb->len == 0) { /* First fragment */
  283. skb->protocol = htons(ETH_P_PHONET);
  284. skb_reset_mac_header(skb);
  285. /* Can't use pskb_pull() on page in IRQ */
  286. memcpy(skb_put(skb, 1), page_address(page), 1);
  287. }
  288. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
  289. skb->len <= 1, req->actual, PAGE_SIZE);
  290. page = NULL;
  291. if (req->actual < req->length) { /* Last fragment */
  292. skb->dev = dev;
  293. dev->stats.rx_packets++;
  294. dev->stats.rx_bytes += skb->len;
  295. netif_rx(skb);
  296. }
  297. break;
  298. /* Do not resubmit in these cases: */
  299. case -ESHUTDOWN: /* disconnect */
  300. case -ECONNABORTED: /* hw reset */
  301. case -ECONNRESET: /* dequeued (unlink or netif down) */
  302. req = NULL;
  303. break;
  304. /* Do resubmit in these cases: */
  305. case -EOVERFLOW: /* request buffer overflow */
  306. dev->stats.rx_over_errors++;
  307. default:
  308. dev->stats.rx_errors++;
  309. break;
  310. }
  311. if (page)
  312. put_page(page);
  313. if (req)
  314. pn_rx_submit(fp, req, GFP_ATOMIC);
  315. }
  316. /*-------------------------------------------------------------------------*/
  317. static void __pn_reset(struct usb_function *f)
  318. {
  319. struct f_phonet *fp = func_to_pn(f);
  320. struct net_device *dev = fp->dev;
  321. struct phonet_port *port = netdev_priv(dev);
  322. netif_carrier_off(dev);
  323. port->usb = NULL;
  324. usb_ep_disable(fp->out_ep);
  325. usb_ep_disable(fp->in_ep);
  326. if (fp->rx.skb) {
  327. dev_kfree_skb_irq(fp->rx.skb);
  328. fp->rx.skb = NULL;
  329. }
  330. }
  331. static int pn_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  332. {
  333. struct f_phonet *fp = func_to_pn(f);
  334. struct usb_gadget *gadget = fp->function.config->cdev->gadget;
  335. if (intf == pn_control_intf_desc.bInterfaceNumber)
  336. /* control interface, no altsetting */
  337. return (alt > 0) ? -EINVAL : 0;
  338. if (intf == pn_data_intf_desc.bInterfaceNumber) {
  339. struct net_device *dev = fp->dev;
  340. struct phonet_port *port = netdev_priv(dev);
  341. /* data intf (0: inactive, 1: active) */
  342. if (alt > 1)
  343. return -EINVAL;
  344. spin_lock(&port->lock);
  345. if (fp->in_ep->enabled)
  346. __pn_reset(f);
  347. if (alt == 1) {
  348. int i;
  349. if (config_ep_by_speed(gadget, f, fp->in_ep) ||
  350. config_ep_by_speed(gadget, f, fp->out_ep)) {
  351. fp->in_ep->desc = NULL;
  352. fp->out_ep->desc = NULL;
  353. spin_unlock(&port->lock);
  354. return -EINVAL;
  355. }
  356. usb_ep_enable(fp->out_ep);
  357. usb_ep_enable(fp->in_ep);
  358. port->usb = fp;
  359. fp->out_ep->driver_data = fp;
  360. fp->in_ep->driver_data = fp;
  361. netif_carrier_on(dev);
  362. for (i = 0; i < phonet_rxq_size; i++)
  363. pn_rx_submit(fp, fp->out_reqv[i], GFP_ATOMIC);
  364. }
  365. spin_unlock(&port->lock);
  366. return 0;
  367. }
  368. return -EINVAL;
  369. }
  370. static int pn_get_alt(struct usb_function *f, unsigned intf)
  371. {
  372. struct f_phonet *fp = func_to_pn(f);
  373. if (intf == pn_control_intf_desc.bInterfaceNumber)
  374. return 0;
  375. if (intf == pn_data_intf_desc.bInterfaceNumber) {
  376. struct phonet_port *port = netdev_priv(fp->dev);
  377. u8 alt;
  378. spin_lock(&port->lock);
  379. alt = port->usb != NULL;
  380. spin_unlock(&port->lock);
  381. return alt;
  382. }
  383. return -EINVAL;
  384. }
  385. static void pn_disconnect(struct usb_function *f)
  386. {
  387. struct f_phonet *fp = func_to_pn(f);
  388. struct phonet_port *port = netdev_priv(fp->dev);
  389. unsigned long flags;
  390. /* remain disabled until set_alt */
  391. spin_lock_irqsave(&port->lock, flags);
  392. __pn_reset(f);
  393. spin_unlock_irqrestore(&port->lock, flags);
  394. }
  395. /*-------------------------------------------------------------------------*/
  396. static int pn_bind(struct usb_configuration *c, struct usb_function *f)
  397. {
  398. struct usb_composite_dev *cdev = c->cdev;
  399. struct usb_gadget *gadget = cdev->gadget;
  400. struct f_phonet *fp = func_to_pn(f);
  401. struct usb_ep *ep;
  402. int status, i;
  403. struct f_phonet_opts *phonet_opts;
  404. phonet_opts = container_of(f->fi, struct f_phonet_opts, func_inst);
  405. /*
  406. * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
  407. * configurations are bound in sequence with list_for_each_entry,
  408. * in each configuration its functions are bound in sequence
  409. * with list_for_each_entry, so we assume no race condition
  410. * with regard to phonet_opts->bound access
  411. */
  412. if (!phonet_opts->bound) {
  413. gphonet_set_gadget(phonet_opts->net, gadget);
  414. status = gphonet_register_netdev(phonet_opts->net);
  415. if (status)
  416. return status;
  417. phonet_opts->bound = true;
  418. }
  419. /* Reserve interface IDs */
  420. status = usb_interface_id(c, f);
  421. if (status < 0)
  422. goto err;
  423. pn_control_intf_desc.bInterfaceNumber = status;
  424. pn_union_desc.bMasterInterface0 = status;
  425. status = usb_interface_id(c, f);
  426. if (status < 0)
  427. goto err;
  428. pn_data_nop_intf_desc.bInterfaceNumber = status;
  429. pn_data_intf_desc.bInterfaceNumber = status;
  430. pn_union_desc.bSlaveInterface0 = status;
  431. /* Reserve endpoints */
  432. status = -ENODEV;
  433. ep = usb_ep_autoconfig(gadget, &pn_fs_sink_desc);
  434. if (!ep)
  435. goto err;
  436. fp->out_ep = ep;
  437. ep = usb_ep_autoconfig(gadget, &pn_fs_source_desc);
  438. if (!ep)
  439. goto err;
  440. fp->in_ep = ep;
  441. pn_hs_sink_desc.bEndpointAddress = pn_fs_sink_desc.bEndpointAddress;
  442. pn_hs_source_desc.bEndpointAddress = pn_fs_source_desc.bEndpointAddress;
  443. /* Do not try to bind Phonet twice... */
  444. status = usb_assign_descriptors(f, fs_pn_function, hs_pn_function,
  445. NULL);
  446. if (status)
  447. goto err;
  448. /* Incoming USB requests */
  449. status = -ENOMEM;
  450. for (i = 0; i < phonet_rxq_size; i++) {
  451. struct usb_request *req;
  452. req = usb_ep_alloc_request(fp->out_ep, GFP_KERNEL);
  453. if (!req)
  454. goto err_req;
  455. req->complete = pn_rx_complete;
  456. fp->out_reqv[i] = req;
  457. }
  458. /* Outgoing USB requests */
  459. fp->in_req = usb_ep_alloc_request(fp->in_ep, GFP_KERNEL);
  460. if (!fp->in_req)
  461. goto err_req;
  462. INFO(cdev, "USB CDC Phonet function\n");
  463. INFO(cdev, "using %s, OUT %s, IN %s\n", cdev->gadget->name,
  464. fp->out_ep->name, fp->in_ep->name);
  465. return 0;
  466. err_req:
  467. for (i = 0; i < phonet_rxq_size && fp->out_reqv[i]; i++)
  468. usb_ep_free_request(fp->out_ep, fp->out_reqv[i]);
  469. usb_free_all_descriptors(f);
  470. err:
  471. ERROR(cdev, "USB CDC Phonet: cannot autoconfigure\n");
  472. return status;
  473. }
  474. static inline struct f_phonet_opts *to_f_phonet_opts(struct config_item *item)
  475. {
  476. return container_of(to_config_group(item), struct f_phonet_opts,
  477. func_inst.group);
  478. }
  479. static void phonet_attr_release(struct config_item *item)
  480. {
  481. struct f_phonet_opts *opts = to_f_phonet_opts(item);
  482. usb_put_function_instance(&opts->func_inst);
  483. }
  484. static struct configfs_item_operations phonet_item_ops = {
  485. .release = phonet_attr_release,
  486. };
  487. static ssize_t f_phonet_ifname_show(struct config_item *item, char *page)
  488. {
  489. return gether_get_ifname(to_f_phonet_opts(item)->net, page, PAGE_SIZE);
  490. }
  491. CONFIGFS_ATTR_RO(f_phonet_, ifname);
  492. static struct configfs_attribute *phonet_attrs[] = {
  493. &f_phonet_attr_ifname,
  494. NULL,
  495. };
  496. static struct config_item_type phonet_func_type = {
  497. .ct_item_ops = &phonet_item_ops,
  498. .ct_attrs = phonet_attrs,
  499. .ct_owner = THIS_MODULE,
  500. };
  501. static void phonet_free_inst(struct usb_function_instance *f)
  502. {
  503. struct f_phonet_opts *opts;
  504. opts = container_of(f, struct f_phonet_opts, func_inst);
  505. if (opts->bound)
  506. gphonet_cleanup(opts->net);
  507. else
  508. free_netdev(opts->net);
  509. kfree(opts);
  510. }
  511. static struct usb_function_instance *phonet_alloc_inst(void)
  512. {
  513. struct f_phonet_opts *opts;
  514. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  515. if (!opts)
  516. return ERR_PTR(-ENOMEM);
  517. opts->func_inst.free_func_inst = phonet_free_inst;
  518. opts->net = gphonet_setup_default();
  519. if (IS_ERR(opts->net)) {
  520. struct net_device *net = opts->net;
  521. kfree(opts);
  522. return ERR_CAST(net);
  523. }
  524. config_group_init_type_name(&opts->func_inst.group, "",
  525. &phonet_func_type);
  526. return &opts->func_inst;
  527. }
  528. static void phonet_free(struct usb_function *f)
  529. {
  530. struct f_phonet *phonet;
  531. phonet = func_to_pn(f);
  532. kfree(phonet);
  533. }
  534. static void pn_unbind(struct usb_configuration *c, struct usb_function *f)
  535. {
  536. struct f_phonet *fp = func_to_pn(f);
  537. int i;
  538. /* We are already disconnected */
  539. if (fp->in_req)
  540. usb_ep_free_request(fp->in_ep, fp->in_req);
  541. for (i = 0; i < phonet_rxq_size; i++)
  542. if (fp->out_reqv[i])
  543. usb_ep_free_request(fp->out_ep, fp->out_reqv[i]);
  544. usb_free_all_descriptors(f);
  545. }
  546. static struct usb_function *phonet_alloc(struct usb_function_instance *fi)
  547. {
  548. struct f_phonet *fp;
  549. struct f_phonet_opts *opts;
  550. int size;
  551. size = sizeof(*fp) + (phonet_rxq_size * sizeof(struct usb_request *));
  552. fp = kzalloc(size, GFP_KERNEL);
  553. if (!fp)
  554. return ERR_PTR(-ENOMEM);
  555. opts = container_of(fi, struct f_phonet_opts, func_inst);
  556. fp->dev = opts->net;
  557. fp->function.name = "phonet";
  558. fp->function.bind = pn_bind;
  559. fp->function.unbind = pn_unbind;
  560. fp->function.set_alt = pn_set_alt;
  561. fp->function.get_alt = pn_get_alt;
  562. fp->function.disable = pn_disconnect;
  563. fp->function.free_func = phonet_free;
  564. spin_lock_init(&fp->rx.lock);
  565. return &fp->function;
  566. }
  567. struct net_device *gphonet_setup_default(void)
  568. {
  569. struct net_device *dev;
  570. struct phonet_port *port;
  571. /* Create net device */
  572. dev = alloc_netdev(sizeof(*port), "upnlink%d", NET_NAME_UNKNOWN,
  573. pn_net_setup);
  574. if (!dev)
  575. return ERR_PTR(-ENOMEM);
  576. port = netdev_priv(dev);
  577. spin_lock_init(&port->lock);
  578. netif_carrier_off(dev);
  579. return dev;
  580. }
  581. void gphonet_set_gadget(struct net_device *net, struct usb_gadget *g)
  582. {
  583. SET_NETDEV_DEV(net, &g->dev);
  584. }
  585. int gphonet_register_netdev(struct net_device *net)
  586. {
  587. int status;
  588. status = register_netdev(net);
  589. if (status)
  590. free_netdev(net);
  591. return status;
  592. }
  593. void gphonet_cleanup(struct net_device *dev)
  594. {
  595. unregister_netdev(dev);
  596. }
  597. DECLARE_USB_FUNCTION_INIT(phonet, phonet_alloc_inst, phonet_alloc);
  598. MODULE_AUTHOR("Rémi Denis-Courmont");
  599. MODULE_LICENSE("GPL");