f_phonet.c 15 KB

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