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[];
  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. fallthrough;
  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. const u8 addr = PN_MEDIA_USB;
  222. dev->features = 0;
  223. dev->type = ARPHRD_PHONET;
  224. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  225. dev->mtu = PHONET_DEV_MTU;
  226. dev->min_mtu = PHONET_MIN_MTU;
  227. dev->max_mtu = PHONET_MAX_MTU;
  228. dev->hard_header_len = 1;
  229. dev->addr_len = 1;
  230. dev_addr_set(dev, &addr);
  231. dev->tx_queue_len = 1;
  232. dev->netdev_ops = &pn_netdev_ops;
  233. dev->needs_free_netdev = true;
  234. dev->header_ops = &phonet_header_ops;
  235. }
  236. /*-------------------------------------------------------------------------*/
  237. /*
  238. * Queue buffer for data from the host
  239. */
  240. static int
  241. pn_rx_submit(struct f_phonet *fp, struct usb_request *req, gfp_t gfp_flags)
  242. {
  243. struct page *page;
  244. int err;
  245. page = __dev_alloc_page(gfp_flags | __GFP_NOMEMALLOC);
  246. if (!page)
  247. return -ENOMEM;
  248. req->buf = page_address(page);
  249. req->length = PAGE_SIZE;
  250. req->context = page;
  251. err = usb_ep_queue(fp->out_ep, req, gfp_flags);
  252. if (unlikely(err))
  253. put_page(page);
  254. return err;
  255. }
  256. static void pn_rx_complete(struct usb_ep *ep, struct usb_request *req)
  257. {
  258. struct f_phonet *fp = ep->driver_data;
  259. struct net_device *dev = fp->dev;
  260. struct page *page = req->context;
  261. struct sk_buff *skb;
  262. unsigned long flags;
  263. int status = req->status;
  264. switch (status) {
  265. case 0:
  266. spin_lock_irqsave(&fp->rx.lock, flags);
  267. skb = fp->rx.skb;
  268. if (!skb)
  269. skb = fp->rx.skb = netdev_alloc_skb(dev, 12);
  270. if (req->actual < req->length) /* Last fragment */
  271. fp->rx.skb = NULL;
  272. spin_unlock_irqrestore(&fp->rx.lock, flags);
  273. if (unlikely(!skb))
  274. break;
  275. if (skb->len == 0) { /* First fragment */
  276. skb->protocol = htons(ETH_P_PHONET);
  277. skb_reset_mac_header(skb);
  278. /* Can't use pskb_pull() on page in IRQ */
  279. skb_put_data(skb, page_address(page), 1);
  280. }
  281. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
  282. skb->len <= 1, req->actual, PAGE_SIZE);
  283. page = NULL;
  284. if (req->actual < req->length) { /* Last fragment */
  285. skb->dev = dev;
  286. dev->stats.rx_packets++;
  287. dev->stats.rx_bytes += skb->len;
  288. netif_rx(skb);
  289. }
  290. break;
  291. /* Do not resubmit in these cases: */
  292. case -ESHUTDOWN: /* disconnect */
  293. case -ECONNABORTED: /* hw reset */
  294. case -ECONNRESET: /* dequeued (unlink or netif down) */
  295. req = NULL;
  296. break;
  297. /* Do resubmit in these cases: */
  298. case -EOVERFLOW: /* request buffer overflow */
  299. dev->stats.rx_over_errors++;
  300. fallthrough;
  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 const 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. fp = kzalloc(struct_size(fp, out_reqv, phonet_rxq_size), GFP_KERNEL);
  545. if (!fp)
  546. return ERR_PTR(-ENOMEM);
  547. opts = container_of(fi, struct f_phonet_opts, func_inst);
  548. fp->dev = opts->net;
  549. fp->function.name = "phonet";
  550. fp->function.bind = pn_bind;
  551. fp->function.unbind = pn_unbind;
  552. fp->function.set_alt = pn_set_alt;
  553. fp->function.get_alt = pn_get_alt;
  554. fp->function.disable = pn_disconnect;
  555. fp->function.free_func = phonet_free;
  556. spin_lock_init(&fp->rx.lock);
  557. return &fp->function;
  558. }
  559. struct net_device *gphonet_setup_default(void)
  560. {
  561. struct net_device *dev;
  562. struct phonet_port *port;
  563. /* Create net device */
  564. dev = alloc_netdev(sizeof(*port), "upnlink%d", NET_NAME_UNKNOWN,
  565. pn_net_setup);
  566. if (!dev)
  567. return ERR_PTR(-ENOMEM);
  568. port = netdev_priv(dev);
  569. spin_lock_init(&port->lock);
  570. netif_carrier_off(dev);
  571. return dev;
  572. }
  573. void gphonet_set_gadget(struct net_device *net, struct usb_gadget *g)
  574. {
  575. SET_NETDEV_DEV(net, &g->dev);
  576. }
  577. int gphonet_register_netdev(struct net_device *net)
  578. {
  579. int status;
  580. status = register_netdev(net);
  581. if (status)
  582. free_netdev(net);
  583. return status;
  584. }
  585. void gphonet_cleanup(struct net_device *dev)
  586. {
  587. unregister_netdev(dev);
  588. }
  589. DECLARE_USB_FUNCTION_INIT(phonet, phonet_alloc_inst, phonet_alloc);
  590. MODULE_AUTHOR("Rémi Denis-Courmont");
  591. MODULE_LICENSE("GPL");