f_phonet.c 15 KB

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