f_phonet.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/device.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/if_ether.h>
  26. #include <linux/if_phonet.h>
  27. #include <linux/if_arp.h>
  28. #include <linux/usb/ch9.h>
  29. #include <linux/usb/cdc.h>
  30. #include <linux/usb/composite.h>
  31. #include "u_phonet.h"
  32. #define PN_MEDIA_USB 0x1B
  33. #define MAXPACKET 512
  34. #if (PAGE_SIZE % MAXPACKET)
  35. #error MAXPACKET must divide PAGE_SIZE!
  36. #endif
  37. /*-------------------------------------------------------------------------*/
  38. struct phonet_port {
  39. struct f_phonet *usb;
  40. spinlock_t lock;
  41. };
  42. struct f_phonet {
  43. struct usb_function function;
  44. struct {
  45. struct sk_buff *skb;
  46. spinlock_t lock;
  47. } rx;
  48. struct net_device *dev;
  49. struct usb_ep *in_ep, *out_ep;
  50. struct usb_request *in_req;
  51. struct usb_request *out_reqv[0];
  52. };
  53. static int phonet_rxq_size = 17;
  54. static inline struct f_phonet *func_to_pn(struct usb_function *f)
  55. {
  56. return container_of(f, struct f_phonet, function);
  57. }
  58. /*-------------------------------------------------------------------------*/
  59. #define USB_CDC_SUBCLASS_PHONET 0xfe
  60. #define USB_CDC_PHONET_TYPE 0xab
  61. static struct usb_interface_descriptor
  62. pn_control_intf_desc = {
  63. .bLength = sizeof pn_control_intf_desc,
  64. .bDescriptorType = USB_DT_INTERFACE,
  65. /* .bInterfaceNumber = DYNAMIC, */
  66. .bInterfaceClass = USB_CLASS_COMM,
  67. .bInterfaceSubClass = USB_CDC_SUBCLASS_PHONET,
  68. };
  69. static const struct usb_cdc_header_desc
  70. pn_header_desc = {
  71. .bLength = sizeof pn_header_desc,
  72. .bDescriptorType = USB_DT_CS_INTERFACE,
  73. .bDescriptorSubType = USB_CDC_HEADER_TYPE,
  74. .bcdCDC = cpu_to_le16(0x0110),
  75. };
  76. static const struct usb_cdc_header_desc
  77. pn_phonet_desc = {
  78. .bLength = sizeof pn_phonet_desc,
  79. .bDescriptorType = USB_DT_CS_INTERFACE,
  80. .bDescriptorSubType = USB_CDC_PHONET_TYPE,
  81. .bcdCDC = cpu_to_le16(0x1505), /* ??? */
  82. };
  83. static struct usb_cdc_union_desc
  84. pn_union_desc = {
  85. .bLength = sizeof pn_union_desc,
  86. .bDescriptorType = USB_DT_CS_INTERFACE,
  87. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  88. /* .bMasterInterface0 = DYNAMIC, */
  89. /* .bSlaveInterface0 = DYNAMIC, */
  90. };
  91. static struct usb_interface_descriptor
  92. pn_data_nop_intf_desc = {
  93. .bLength = sizeof pn_data_nop_intf_desc,
  94. .bDescriptorType = USB_DT_INTERFACE,
  95. /* .bInterfaceNumber = DYNAMIC, */
  96. .bAlternateSetting = 0,
  97. .bNumEndpoints = 0,
  98. .bInterfaceClass = USB_CLASS_CDC_DATA,
  99. };
  100. static struct usb_interface_descriptor
  101. pn_data_intf_desc = {
  102. .bLength = sizeof pn_data_intf_desc,
  103. .bDescriptorType = USB_DT_INTERFACE,
  104. /* .bInterfaceNumber = DYNAMIC, */
  105. .bAlternateSetting = 1,
  106. .bNumEndpoints = 2,
  107. .bInterfaceClass = USB_CLASS_CDC_DATA,
  108. };
  109. static struct usb_endpoint_descriptor
  110. pn_fs_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. };
  116. static struct usb_endpoint_descriptor
  117. pn_hs_sink_desc = {
  118. .bLength = USB_DT_ENDPOINT_SIZE,
  119. .bDescriptorType = USB_DT_ENDPOINT,
  120. .bEndpointAddress = USB_DIR_OUT,
  121. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  122. .wMaxPacketSize = cpu_to_le16(MAXPACKET),
  123. };
  124. static struct usb_endpoint_descriptor
  125. pn_fs_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. };
  131. static struct usb_endpoint_descriptor
  132. pn_hs_source_desc = {
  133. .bLength = USB_DT_ENDPOINT_SIZE,
  134. .bDescriptorType = USB_DT_ENDPOINT,
  135. .bEndpointAddress = USB_DIR_IN,
  136. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  137. .wMaxPacketSize = cpu_to_le16(512),
  138. };
  139. static struct usb_descriptor_header *fs_pn_function[] = {
  140. (struct usb_descriptor_header *) &pn_control_intf_desc,
  141. (struct usb_descriptor_header *) &pn_header_desc,
  142. (struct usb_descriptor_header *) &pn_phonet_desc,
  143. (struct usb_descriptor_header *) &pn_union_desc,
  144. (struct usb_descriptor_header *) &pn_data_nop_intf_desc,
  145. (struct usb_descriptor_header *) &pn_data_intf_desc,
  146. (struct usb_descriptor_header *) &pn_fs_sink_desc,
  147. (struct usb_descriptor_header *) &pn_fs_source_desc,
  148. NULL,
  149. };
  150. static struct usb_descriptor_header *hs_pn_function[] = {
  151. (struct usb_descriptor_header *) &pn_control_intf_desc,
  152. (struct usb_descriptor_header *) &pn_header_desc,
  153. (struct usb_descriptor_header *) &pn_phonet_desc,
  154. (struct usb_descriptor_header *) &pn_union_desc,
  155. (struct usb_descriptor_header *) &pn_data_nop_intf_desc,
  156. (struct usb_descriptor_header *) &pn_data_intf_desc,
  157. (struct usb_descriptor_header *) &pn_hs_sink_desc,
  158. (struct usb_descriptor_header *) &pn_hs_source_desc,
  159. NULL,
  160. };
  161. /*-------------------------------------------------------------------------*/
  162. static int pn_net_open(struct net_device *dev)
  163. {
  164. netif_wake_queue(dev);
  165. return 0;
  166. }
  167. static int pn_net_close(struct net_device *dev)
  168. {
  169. netif_stop_queue(dev);
  170. return 0;
  171. }
  172. static void pn_tx_complete(struct usb_ep *ep, struct usb_request *req)
  173. {
  174. struct f_phonet *fp = ep->driver_data;
  175. struct net_device *dev = fp->dev;
  176. struct sk_buff *skb = req->context;
  177. switch (req->status) {
  178. case 0:
  179. dev->stats.tx_packets++;
  180. dev->stats.tx_bytes += skb->len;
  181. break;
  182. case -ESHUTDOWN: /* disconnected */
  183. case -ECONNRESET: /* disabled */
  184. dev->stats.tx_aborted_errors++;
  185. default:
  186. dev->stats.tx_errors++;
  187. }
  188. dev_kfree_skb_any(skb);
  189. netif_wake_queue(dev);
  190. }
  191. static int pn_net_xmit(struct sk_buff *skb, struct net_device *dev)
  192. {
  193. struct phonet_port *port = netdev_priv(dev);
  194. struct f_phonet *fp;
  195. struct usb_request *req;
  196. unsigned long flags;
  197. if (skb->protocol != htons(ETH_P_PHONET))
  198. goto out;
  199. spin_lock_irqsave(&port->lock, flags);
  200. fp = port->usb;
  201. if (unlikely(!fp)) /* race with carrier loss */
  202. goto out_unlock;
  203. req = fp->in_req;
  204. req->buf = skb->data;
  205. req->length = skb->len;
  206. req->complete = pn_tx_complete;
  207. req->zero = 1;
  208. req->context = skb;
  209. if (unlikely(usb_ep_queue(fp->in_ep, req, GFP_ATOMIC)))
  210. goto out_unlock;
  211. netif_stop_queue(dev);
  212. skb = NULL;
  213. out_unlock:
  214. spin_unlock_irqrestore(&port->lock, flags);
  215. out:
  216. if (unlikely(skb)) {
  217. dev_kfree_skb(skb);
  218. dev->stats.tx_dropped++;
  219. }
  220. return NETDEV_TX_OK;
  221. }
  222. static int pn_net_mtu(struct net_device *dev, int new_mtu)
  223. {
  224. if ((new_mtu < PHONET_MIN_MTU) || (new_mtu > PHONET_MAX_MTU))
  225. return -EINVAL;
  226. dev->mtu = new_mtu;
  227. return 0;
  228. }
  229. static const struct net_device_ops pn_netdev_ops = {
  230. .ndo_open = pn_net_open,
  231. .ndo_stop = pn_net_close,
  232. .ndo_start_xmit = pn_net_xmit,
  233. .ndo_change_mtu = pn_net_mtu,
  234. };
  235. static void pn_net_setup(struct net_device *dev)
  236. {
  237. dev->features = 0;
  238. dev->type = ARPHRD_PHONET;
  239. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  240. dev->mtu = PHONET_DEV_MTU;
  241. dev->hard_header_len = 1;
  242. dev->dev_addr[0] = PN_MEDIA_USB;
  243. dev->addr_len = 1;
  244. dev->tx_queue_len = 1;
  245. dev->netdev_ops = &pn_netdev_ops;
  246. dev->destructor = free_netdev;
  247. dev->header_ops = &phonet_header_ops;
  248. }
  249. /*-------------------------------------------------------------------------*/
  250. /*
  251. * Queue buffer for data from the host
  252. */
  253. static int
  254. pn_rx_submit(struct f_phonet *fp, struct usb_request *req, gfp_t gfp_flags)
  255. {
  256. struct net_device *dev = fp->dev;
  257. struct page *page;
  258. int err;
  259. page = __netdev_alloc_page(dev, gfp_flags);
  260. if (!page)
  261. return -ENOMEM;
  262. req->buf = page_address(page);
  263. req->length = PAGE_SIZE;
  264. req->context = page;
  265. err = usb_ep_queue(fp->out_ep, req, gfp_flags);
  266. if (unlikely(err))
  267. netdev_free_page(dev, page);
  268. return err;
  269. }
  270. static void pn_rx_complete(struct usb_ep *ep, struct usb_request *req)
  271. {
  272. struct f_phonet *fp = ep->driver_data;
  273. struct net_device *dev = fp->dev;
  274. struct page *page = req->context;
  275. struct sk_buff *skb;
  276. unsigned long flags;
  277. int status = req->status;
  278. switch (status) {
  279. case 0:
  280. spin_lock_irqsave(&fp->rx.lock, flags);
  281. skb = fp->rx.skb;
  282. if (!skb)
  283. skb = fp->rx.skb = netdev_alloc_skb(dev, 12);
  284. if (req->actual < req->length) /* Last fragment */
  285. fp->rx.skb = NULL;
  286. spin_unlock_irqrestore(&fp->rx.lock, flags);
  287. if (unlikely(!skb))
  288. break;
  289. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, 0,
  290. req->actual);
  291. page = NULL;
  292. if (req->actual < req->length) { /* Last fragment */
  293. skb->protocol = htons(ETH_P_PHONET);
  294. skb_reset_mac_header(skb);
  295. pskb_pull(skb, 1);
  296. skb->dev = dev;
  297. dev->stats.rx_packets++;
  298. dev->stats.rx_bytes += skb->len;
  299. netif_rx(skb);
  300. }
  301. break;
  302. /* Do not resubmit in these cases: */
  303. case -ESHUTDOWN: /* disconnect */
  304. case -ECONNABORTED: /* hw reset */
  305. case -ECONNRESET: /* dequeued (unlink or netif down) */
  306. req = NULL;
  307. break;
  308. /* Do resubmit in these cases: */
  309. case -EOVERFLOW: /* request buffer overflow */
  310. dev->stats.rx_over_errors++;
  311. default:
  312. dev->stats.rx_errors++;
  313. break;
  314. }
  315. if (page)
  316. netdev_free_page(dev, page);
  317. if (req)
  318. pn_rx_submit(fp, req, GFP_ATOMIC);
  319. }
  320. /*-------------------------------------------------------------------------*/
  321. static void __pn_reset(struct usb_function *f)
  322. {
  323. struct f_phonet *fp = func_to_pn(f);
  324. struct net_device *dev = fp->dev;
  325. struct phonet_port *port = netdev_priv(dev);
  326. netif_carrier_off(dev);
  327. port->usb = NULL;
  328. usb_ep_disable(fp->out_ep);
  329. usb_ep_disable(fp->in_ep);
  330. if (fp->rx.skb) {
  331. dev_kfree_skb_irq(fp->rx.skb);
  332. fp->rx.skb = NULL;
  333. }
  334. }
  335. static int pn_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  336. {
  337. struct f_phonet *fp = func_to_pn(f);
  338. struct usb_gadget *gadget = fp->function.config->cdev->gadget;
  339. if (intf == pn_control_intf_desc.bInterfaceNumber)
  340. /* control interface, no altsetting */
  341. return (alt > 0) ? -EINVAL : 0;
  342. if (intf == pn_data_intf_desc.bInterfaceNumber) {
  343. struct net_device *dev = fp->dev;
  344. struct phonet_port *port = netdev_priv(dev);
  345. /* data intf (0: inactive, 1: active) */
  346. if (alt > 1)
  347. return -EINVAL;
  348. spin_lock(&port->lock);
  349. __pn_reset(f);
  350. if (alt == 1) {
  351. struct usb_endpoint_descriptor *out, *in;
  352. int i;
  353. out = ep_choose(gadget,
  354. &pn_hs_sink_desc,
  355. &pn_fs_sink_desc);
  356. in = ep_choose(gadget,
  357. &pn_hs_source_desc,
  358. &pn_fs_source_desc);
  359. usb_ep_enable(fp->out_ep, out);
  360. usb_ep_enable(fp->in_ep, in);
  361. port->usb = fp;
  362. fp->out_ep->driver_data = fp;
  363. fp->in_ep->driver_data = fp;
  364. netif_carrier_on(dev);
  365. for (i = 0; i < phonet_rxq_size; i++)
  366. pn_rx_submit(fp, fp->out_reqv[i], GFP_ATOMIC);
  367. }
  368. spin_unlock(&port->lock);
  369. return 0;
  370. }
  371. return -EINVAL;
  372. }
  373. static int pn_get_alt(struct usb_function *f, unsigned intf)
  374. {
  375. struct f_phonet *fp = func_to_pn(f);
  376. if (intf == pn_control_intf_desc.bInterfaceNumber)
  377. return 0;
  378. if (intf == pn_data_intf_desc.bInterfaceNumber) {
  379. struct phonet_port *port = netdev_priv(fp->dev);
  380. u8 alt;
  381. spin_lock(&port->lock);
  382. alt = port->usb != NULL;
  383. spin_unlock(&port->lock);
  384. return alt;
  385. }
  386. return -EINVAL;
  387. }
  388. static void pn_disconnect(struct usb_function *f)
  389. {
  390. struct f_phonet *fp = func_to_pn(f);
  391. struct phonet_port *port = netdev_priv(fp->dev);
  392. unsigned long flags;
  393. /* remain disabled until set_alt */
  394. spin_lock_irqsave(&port->lock, flags);
  395. __pn_reset(f);
  396. spin_unlock_irqrestore(&port->lock, flags);
  397. }
  398. /*-------------------------------------------------------------------------*/
  399. static __init
  400. int pn_bind(struct usb_configuration *c, struct usb_function *f)
  401. {
  402. struct usb_composite_dev *cdev = c->cdev;
  403. struct usb_gadget *gadget = cdev->gadget;
  404. struct f_phonet *fp = func_to_pn(f);
  405. struct usb_ep *ep;
  406. int status, i;
  407. /* Reserve interface IDs */
  408. status = usb_interface_id(c, f);
  409. if (status < 0)
  410. goto err;
  411. pn_control_intf_desc.bInterfaceNumber = status;
  412. pn_union_desc.bMasterInterface0 = status;
  413. status = usb_interface_id(c, f);
  414. if (status < 0)
  415. goto err;
  416. pn_data_nop_intf_desc.bInterfaceNumber = status;
  417. pn_data_intf_desc.bInterfaceNumber = status;
  418. pn_union_desc.bSlaveInterface0 = status;
  419. /* Reserve endpoints */
  420. status = -ENODEV;
  421. ep = usb_ep_autoconfig(gadget, &pn_fs_sink_desc);
  422. if (!ep)
  423. goto err;
  424. fp->out_ep = ep;
  425. ep->driver_data = fp; /* Claim */
  426. ep = usb_ep_autoconfig(gadget, &pn_fs_source_desc);
  427. if (!ep)
  428. goto err;
  429. fp->in_ep = ep;
  430. ep->driver_data = fp; /* Claim */
  431. pn_hs_sink_desc.bEndpointAddress =
  432. pn_fs_sink_desc.bEndpointAddress;
  433. pn_hs_source_desc.bEndpointAddress =
  434. pn_fs_source_desc.bEndpointAddress;
  435. /* Do not try to bind Phonet twice... */
  436. fp->function.descriptors = fs_pn_function;
  437. fp->function.hs_descriptors = hs_pn_function;
  438. /* Incoming USB requests */
  439. status = -ENOMEM;
  440. for (i = 0; i < phonet_rxq_size; i++) {
  441. struct usb_request *req;
  442. req = usb_ep_alloc_request(fp->out_ep, GFP_KERNEL);
  443. if (!req)
  444. goto err;
  445. req->complete = pn_rx_complete;
  446. fp->out_reqv[i] = req;
  447. }
  448. /* Outgoing USB requests */
  449. fp->in_req = usb_ep_alloc_request(fp->in_ep, GFP_KERNEL);
  450. if (!fp->in_req)
  451. goto err;
  452. INFO(cdev, "USB CDC Phonet function\n");
  453. INFO(cdev, "using %s, OUT %s, IN %s\n", cdev->gadget->name,
  454. fp->out_ep->name, fp->in_ep->name);
  455. return 0;
  456. err:
  457. if (fp->out_ep)
  458. fp->out_ep->driver_data = NULL;
  459. if (fp->in_ep)
  460. fp->in_ep->driver_data = NULL;
  461. ERROR(cdev, "USB CDC Phonet: cannot autoconfigure\n");
  462. return status;
  463. }
  464. static void
  465. pn_unbind(struct usb_configuration *c, struct usb_function *f)
  466. {
  467. struct f_phonet *fp = func_to_pn(f);
  468. int i;
  469. /* We are already disconnected */
  470. if (fp->in_req)
  471. usb_ep_free_request(fp->in_ep, fp->in_req);
  472. for (i = 0; i < phonet_rxq_size; i++)
  473. if (fp->out_reqv[i])
  474. usb_ep_free_request(fp->out_ep, fp->out_reqv[i]);
  475. kfree(fp);
  476. }
  477. /*-------------------------------------------------------------------------*/
  478. static struct net_device *dev;
  479. int __init phonet_bind_config(struct usb_configuration *c)
  480. {
  481. struct f_phonet *fp;
  482. int err, size;
  483. size = sizeof(*fp) + (phonet_rxq_size * sizeof(struct usb_request *));
  484. fp = kzalloc(size, GFP_KERNEL);
  485. if (!fp)
  486. return -ENOMEM;
  487. fp->dev = dev;
  488. fp->function.name = "phonet";
  489. fp->function.bind = pn_bind;
  490. fp->function.unbind = pn_unbind;
  491. fp->function.set_alt = pn_set_alt;
  492. fp->function.get_alt = pn_get_alt;
  493. fp->function.disable = pn_disconnect;
  494. spin_lock_init(&fp->rx.lock);
  495. err = usb_add_function(c, &fp->function);
  496. if (err)
  497. kfree(fp);
  498. return err;
  499. }
  500. int __init gphonet_setup(struct usb_gadget *gadget)
  501. {
  502. struct phonet_port *port;
  503. int err;
  504. /* Create net device */
  505. BUG_ON(dev);
  506. dev = alloc_netdev(sizeof(*port), "upnlink%d", pn_net_setup);
  507. if (!dev)
  508. return -ENOMEM;
  509. port = netdev_priv(dev);
  510. spin_lock_init(&port->lock);
  511. netif_carrier_off(dev);
  512. SET_NETDEV_DEV(dev, &gadget->dev);
  513. err = register_netdev(dev);
  514. if (err)
  515. free_netdev(dev);
  516. return err;
  517. }
  518. void gphonet_cleanup(void)
  519. {
  520. unregister_netdev(dev);
  521. }