f_phonet.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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 = __skb_alloc_page(gfp_flags | __GFP_NOMEMALLOC, NULL);
  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 | __GFP_COLD);
  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. __pn_reset(f);
  346. if (alt == 1) {
  347. int i;
  348. if (config_ep_by_speed(gadget, f, fp->in_ep) ||
  349. config_ep_by_speed(gadget, f, fp->out_ep)) {
  350. fp->in_ep->desc = NULL;
  351. fp->out_ep->desc = NULL;
  352. spin_unlock(&port->lock);
  353. return -EINVAL;
  354. }
  355. usb_ep_enable(fp->out_ep);
  356. usb_ep_enable(fp->in_ep);
  357. port->usb = fp;
  358. fp->out_ep->driver_data = fp;
  359. fp->in_ep->driver_data = fp;
  360. netif_carrier_on(dev);
  361. for (i = 0; i < phonet_rxq_size; i++)
  362. pn_rx_submit(fp, fp->out_reqv[i], GFP_ATOMIC | __GFP_COLD);
  363. }
  364. spin_unlock(&port->lock);
  365. return 0;
  366. }
  367. return -EINVAL;
  368. }
  369. static int pn_get_alt(struct usb_function *f, unsigned intf)
  370. {
  371. struct f_phonet *fp = func_to_pn(f);
  372. if (intf == pn_control_intf_desc.bInterfaceNumber)
  373. return 0;
  374. if (intf == pn_data_intf_desc.bInterfaceNumber) {
  375. struct phonet_port *port = netdev_priv(fp->dev);
  376. u8 alt;
  377. spin_lock(&port->lock);
  378. alt = port->usb != NULL;
  379. spin_unlock(&port->lock);
  380. return alt;
  381. }
  382. return -EINVAL;
  383. }
  384. static void pn_disconnect(struct usb_function *f)
  385. {
  386. struct f_phonet *fp = func_to_pn(f);
  387. struct phonet_port *port = netdev_priv(fp->dev);
  388. unsigned long flags;
  389. /* remain disabled until set_alt */
  390. spin_lock_irqsave(&port->lock, flags);
  391. __pn_reset(f);
  392. spin_unlock_irqrestore(&port->lock, flags);
  393. }
  394. /*-------------------------------------------------------------------------*/
  395. static int pn_bind(struct usb_configuration *c, struct usb_function *f)
  396. {
  397. struct usb_composite_dev *cdev = c->cdev;
  398. struct usb_gadget *gadget = cdev->gadget;
  399. struct f_phonet *fp = func_to_pn(f);
  400. struct usb_ep *ep;
  401. int status, i;
  402. #ifndef USBF_PHONET_INCLUDED
  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. #endif
  420. /* Reserve interface IDs */
  421. status = usb_interface_id(c, f);
  422. if (status < 0)
  423. goto err;
  424. pn_control_intf_desc.bInterfaceNumber = status;
  425. pn_union_desc.bMasterInterface0 = status;
  426. status = usb_interface_id(c, f);
  427. if (status < 0)
  428. goto err;
  429. pn_data_nop_intf_desc.bInterfaceNumber = status;
  430. pn_data_intf_desc.bInterfaceNumber = status;
  431. pn_union_desc.bSlaveInterface0 = status;
  432. /* Reserve endpoints */
  433. status = -ENODEV;
  434. ep = usb_ep_autoconfig(gadget, &pn_fs_sink_desc);
  435. if (!ep)
  436. goto err;
  437. fp->out_ep = ep;
  438. ep->driver_data = fp; /* Claim */
  439. ep = usb_ep_autoconfig(gadget, &pn_fs_source_desc);
  440. if (!ep)
  441. goto err;
  442. fp->in_ep = ep;
  443. ep->driver_data = fp; /* Claim */
  444. pn_hs_sink_desc.bEndpointAddress = pn_fs_sink_desc.bEndpointAddress;
  445. pn_hs_source_desc.bEndpointAddress = pn_fs_source_desc.bEndpointAddress;
  446. /* Do not try to bind Phonet twice... */
  447. status = usb_assign_descriptors(f, fs_pn_function, hs_pn_function,
  448. NULL);
  449. if (status)
  450. goto err;
  451. /* Incoming USB requests */
  452. status = -ENOMEM;
  453. for (i = 0; i < phonet_rxq_size; i++) {
  454. struct usb_request *req;
  455. req = usb_ep_alloc_request(fp->out_ep, GFP_KERNEL);
  456. if (!req)
  457. goto err_req;
  458. req->complete = pn_rx_complete;
  459. fp->out_reqv[i] = req;
  460. }
  461. /* Outgoing USB requests */
  462. fp->in_req = usb_ep_alloc_request(fp->in_ep, GFP_KERNEL);
  463. if (!fp->in_req)
  464. goto err_req;
  465. INFO(cdev, "USB CDC Phonet function\n");
  466. INFO(cdev, "using %s, OUT %s, IN %s\n", cdev->gadget->name,
  467. fp->out_ep->name, fp->in_ep->name);
  468. return 0;
  469. err_req:
  470. for (i = 0; i < phonet_rxq_size && fp->out_reqv[i]; i++)
  471. usb_ep_free_request(fp->out_ep, fp->out_reqv[i]);
  472. err:
  473. usb_free_all_descriptors(f);
  474. if (fp->out_ep)
  475. fp->out_ep->driver_data = NULL;
  476. if (fp->in_ep)
  477. fp->in_ep->driver_data = NULL;
  478. ERROR(cdev, "USB CDC Phonet: cannot autoconfigure\n");
  479. return status;
  480. }
  481. static inline struct f_phonet_opts *to_f_phonet_opts(struct config_item *item)
  482. {
  483. return container_of(to_config_group(item), struct f_phonet_opts,
  484. func_inst.group);
  485. }
  486. CONFIGFS_ATTR_STRUCT(f_phonet_opts);
  487. static ssize_t f_phonet_attr_show(struct config_item *item,
  488. struct configfs_attribute *attr,
  489. char *page)
  490. {
  491. struct f_phonet_opts *opts = to_f_phonet_opts(item);
  492. struct f_phonet_opts_attribute *f_phonet_opts_attr =
  493. container_of(attr, struct f_phonet_opts_attribute, attr);
  494. ssize_t ret = 0;
  495. if (f_phonet_opts_attr->show)
  496. ret = f_phonet_opts_attr->show(opts, page);
  497. return ret;
  498. }
  499. static void phonet_attr_release(struct config_item *item)
  500. {
  501. struct f_phonet_opts *opts = to_f_phonet_opts(item);
  502. usb_put_function_instance(&opts->func_inst);
  503. }
  504. static struct configfs_item_operations phonet_item_ops = {
  505. .release = phonet_attr_release,
  506. .show_attribute = f_phonet_attr_show,
  507. };
  508. static ssize_t f_phonet_ifname_show(struct f_phonet_opts *opts, char *page)
  509. {
  510. return gether_get_ifname(opts->net, page, PAGE_SIZE);
  511. }
  512. static struct f_phonet_opts_attribute f_phonet_ifname =
  513. __CONFIGFS_ATTR_RO(ifname, f_phonet_ifname_show);
  514. static struct configfs_attribute *phonet_attrs[] = {
  515. &f_phonet_ifname.attr,
  516. NULL,
  517. };
  518. static struct config_item_type phonet_func_type = {
  519. .ct_item_ops = &phonet_item_ops,
  520. .ct_attrs = phonet_attrs,
  521. .ct_owner = THIS_MODULE,
  522. };
  523. static void phonet_free_inst(struct usb_function_instance *f)
  524. {
  525. struct f_phonet_opts *opts;
  526. opts = container_of(f, struct f_phonet_opts, func_inst);
  527. if (opts->bound)
  528. gphonet_cleanup(opts->net);
  529. else
  530. free_netdev(opts->net);
  531. kfree(opts);
  532. }
  533. static struct usb_function_instance *phonet_alloc_inst(void)
  534. {
  535. struct f_phonet_opts *opts;
  536. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  537. if (!opts)
  538. return ERR_PTR(-ENOMEM);
  539. opts->func_inst.free_func_inst = phonet_free_inst;
  540. opts->net = gphonet_setup_default();
  541. if (IS_ERR(opts->net))
  542. return ERR_PTR(PTR_ERR(opts->net));
  543. config_group_init_type_name(&opts->func_inst.group, "",
  544. &phonet_func_type);
  545. return &opts->func_inst;
  546. }
  547. static void phonet_free(struct usb_function *f)
  548. {
  549. struct f_phonet *phonet;
  550. phonet = func_to_pn(f);
  551. kfree(phonet);
  552. }
  553. static void pn_unbind(struct usb_configuration *c, struct usb_function *f)
  554. {
  555. struct f_phonet *fp = func_to_pn(f);
  556. int i;
  557. /* We are already disconnected */
  558. if (fp->in_req)
  559. usb_ep_free_request(fp->in_ep, fp->in_req);
  560. for (i = 0; i < phonet_rxq_size; i++)
  561. if (fp->out_reqv[i])
  562. usb_ep_free_request(fp->out_ep, fp->out_reqv[i]);
  563. usb_free_all_descriptors(f);
  564. }
  565. struct usb_function *phonet_alloc(struct usb_function_instance *fi)
  566. {
  567. struct f_phonet *fp;
  568. struct f_phonet_opts *opts;
  569. int size;
  570. size = sizeof(*fp) + (phonet_rxq_size * sizeof(struct usb_request *));
  571. fp = kzalloc(size, GFP_KERNEL);
  572. if (!fp)
  573. return ERR_PTR(-ENOMEM);
  574. opts = container_of(fi, struct f_phonet_opts, func_inst);
  575. fp->dev = opts->net;
  576. fp->function.name = "phonet";
  577. fp->function.bind = pn_bind;
  578. fp->function.unbind = pn_unbind;
  579. fp->function.set_alt = pn_set_alt;
  580. fp->function.get_alt = pn_get_alt;
  581. fp->function.disable = pn_disconnect;
  582. fp->function.free_func = phonet_free;
  583. spin_lock_init(&fp->rx.lock);
  584. return &fp->function;
  585. }
  586. struct net_device *gphonet_setup_default(void)
  587. {
  588. struct net_device *dev;
  589. struct phonet_port *port;
  590. /* Create net device */
  591. dev = alloc_netdev(sizeof(*port), "upnlink%d", pn_net_setup);
  592. if (!dev)
  593. return ERR_PTR(-ENOMEM);
  594. port = netdev_priv(dev);
  595. spin_lock_init(&port->lock);
  596. netif_carrier_off(dev);
  597. return dev;
  598. }
  599. void gphonet_set_gadget(struct net_device *net, struct usb_gadget *g)
  600. {
  601. SET_NETDEV_DEV(net, &g->dev);
  602. }
  603. int gphonet_register_netdev(struct net_device *net)
  604. {
  605. int status;
  606. status = register_netdev(net);
  607. if (status)
  608. free_netdev(net);
  609. return status;
  610. }
  611. void gphonet_cleanup(struct net_device *dev)
  612. {
  613. unregister_netdev(dev);
  614. }
  615. DECLARE_USB_FUNCTION_INIT(phonet, phonet_alloc_inst, phonet_alloc);
  616. MODULE_AUTHOR("Rémi Denis-Courmont");
  617. MODULE_LICENSE("GPL");