f_phonet.c 16 KB

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