f_phonet.c 17 KB

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