f_phonet.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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/slab.h>
  23. #include <linux/kernel.h>
  24. #include <linux/device.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/if_ether.h>
  27. #include <linux/if_phonet.h>
  28. #include <linux/if_arp.h>
  29. #include <linux/usb/ch9.h>
  30. #include <linux/usb/cdc.h>
  31. #include <linux/usb/composite.h>
  32. #include "u_phonet.h"
  33. #define PN_MEDIA_USB 0x1B
  34. #define MAXPACKET 512
  35. #if (PAGE_SIZE % MAXPACKET)
  36. #error MAXPACKET must divide PAGE_SIZE!
  37. #endif
  38. /*-------------------------------------------------------------------------*/
  39. struct phonet_port {
  40. struct f_phonet *usb;
  41. spinlock_t lock;
  42. };
  43. struct f_phonet {
  44. struct usb_function function;
  45. struct {
  46. struct sk_buff *skb;
  47. spinlock_t lock;
  48. } rx;
  49. struct net_device *dev;
  50. struct usb_ep *in_ep, *out_ep;
  51. struct usb_request *in_req;
  52. struct usb_request *out_reqv[0];
  53. };
  54. static int phonet_rxq_size = 17;
  55. static inline struct f_phonet *func_to_pn(struct usb_function *f)
  56. {
  57. return container_of(f, struct f_phonet, function);
  58. }
  59. /*-------------------------------------------------------------------------*/
  60. #define USB_CDC_SUBCLASS_PHONET 0xfe
  61. #define USB_CDC_PHONET_TYPE 0xab
  62. static struct usb_interface_descriptor
  63. pn_control_intf_desc = {
  64. .bLength = sizeof pn_control_intf_desc,
  65. .bDescriptorType = USB_DT_INTERFACE,
  66. /* .bInterfaceNumber = DYNAMIC, */
  67. .bInterfaceClass = USB_CLASS_COMM,
  68. .bInterfaceSubClass = USB_CDC_SUBCLASS_PHONET,
  69. };
  70. static const struct usb_cdc_header_desc
  71. pn_header_desc = {
  72. .bLength = sizeof pn_header_desc,
  73. .bDescriptorType = USB_DT_CS_INTERFACE,
  74. .bDescriptorSubType = USB_CDC_HEADER_TYPE,
  75. .bcdCDC = cpu_to_le16(0x0110),
  76. };
  77. static const struct usb_cdc_header_desc
  78. pn_phonet_desc = {
  79. .bLength = sizeof pn_phonet_desc,
  80. .bDescriptorType = USB_DT_CS_INTERFACE,
  81. .bDescriptorSubType = USB_CDC_PHONET_TYPE,
  82. .bcdCDC = cpu_to_le16(0x1505), /* ??? */
  83. };
  84. static struct usb_cdc_union_desc
  85. pn_union_desc = {
  86. .bLength = sizeof pn_union_desc,
  87. .bDescriptorType = USB_DT_CS_INTERFACE,
  88. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  89. /* .bMasterInterface0 = DYNAMIC, */
  90. /* .bSlaveInterface0 = DYNAMIC, */
  91. };
  92. static struct usb_interface_descriptor
  93. pn_data_nop_intf_desc = {
  94. .bLength = sizeof pn_data_nop_intf_desc,
  95. .bDescriptorType = USB_DT_INTERFACE,
  96. /* .bInterfaceNumber = DYNAMIC, */
  97. .bAlternateSetting = 0,
  98. .bNumEndpoints = 0,
  99. .bInterfaceClass = USB_CLASS_CDC_DATA,
  100. };
  101. static struct usb_interface_descriptor
  102. pn_data_intf_desc = {
  103. .bLength = sizeof pn_data_intf_desc,
  104. .bDescriptorType = USB_DT_INTERFACE,
  105. /* .bInterfaceNumber = DYNAMIC, */
  106. .bAlternateSetting = 1,
  107. .bNumEndpoints = 2,
  108. .bInterfaceClass = USB_CLASS_CDC_DATA,
  109. };
  110. static struct usb_endpoint_descriptor
  111. pn_fs_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. };
  117. static struct usb_endpoint_descriptor
  118. pn_hs_sink_desc = {
  119. .bLength = USB_DT_ENDPOINT_SIZE,
  120. .bDescriptorType = USB_DT_ENDPOINT,
  121. .bEndpointAddress = USB_DIR_OUT,
  122. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  123. .wMaxPacketSize = cpu_to_le16(MAXPACKET),
  124. };
  125. static struct usb_endpoint_descriptor
  126. pn_fs_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. };
  132. static struct usb_endpoint_descriptor
  133. pn_hs_source_desc = {
  134. .bLength = USB_DT_ENDPOINT_SIZE,
  135. .bDescriptorType = USB_DT_ENDPOINT,
  136. .bEndpointAddress = USB_DIR_IN,
  137. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  138. .wMaxPacketSize = cpu_to_le16(512),
  139. };
  140. static struct usb_descriptor_header *fs_pn_function[] = {
  141. (struct usb_descriptor_header *) &pn_control_intf_desc,
  142. (struct usb_descriptor_header *) &pn_header_desc,
  143. (struct usb_descriptor_header *) &pn_phonet_desc,
  144. (struct usb_descriptor_header *) &pn_union_desc,
  145. (struct usb_descriptor_header *) &pn_data_nop_intf_desc,
  146. (struct usb_descriptor_header *) &pn_data_intf_desc,
  147. (struct usb_descriptor_header *) &pn_fs_sink_desc,
  148. (struct usb_descriptor_header *) &pn_fs_source_desc,
  149. NULL,
  150. };
  151. static struct usb_descriptor_header *hs_pn_function[] = {
  152. (struct usb_descriptor_header *) &pn_control_intf_desc,
  153. (struct usb_descriptor_header *) &pn_header_desc,
  154. (struct usb_descriptor_header *) &pn_phonet_desc,
  155. (struct usb_descriptor_header *) &pn_union_desc,
  156. (struct usb_descriptor_header *) &pn_data_nop_intf_desc,
  157. (struct usb_descriptor_header *) &pn_data_intf_desc,
  158. (struct usb_descriptor_header *) &pn_hs_sink_desc,
  159. (struct usb_descriptor_header *) &pn_hs_source_desc,
  160. NULL,
  161. };
  162. /*-------------------------------------------------------------------------*/
  163. static int pn_net_open(struct net_device *dev)
  164. {
  165. netif_wake_queue(dev);
  166. return 0;
  167. }
  168. static int pn_net_close(struct net_device *dev)
  169. {
  170. netif_stop_queue(dev);
  171. return 0;
  172. }
  173. static void pn_tx_complete(struct usb_ep *ep, struct usb_request *req)
  174. {
  175. struct f_phonet *fp = ep->driver_data;
  176. struct net_device *dev = fp->dev;
  177. struct sk_buff *skb = req->context;
  178. switch (req->status) {
  179. case 0:
  180. dev->stats.tx_packets++;
  181. dev->stats.tx_bytes += skb->len;
  182. break;
  183. case -ESHUTDOWN: /* disconnected */
  184. case -ECONNRESET: /* disabled */
  185. dev->stats.tx_aborted_errors++;
  186. default:
  187. dev->stats.tx_errors++;
  188. }
  189. dev_kfree_skb_any(skb);
  190. netif_wake_queue(dev);
  191. }
  192. static int pn_net_xmit(struct sk_buff *skb, struct net_device *dev)
  193. {
  194. struct phonet_port *port = netdev_priv(dev);
  195. struct f_phonet *fp;
  196. struct usb_request *req;
  197. unsigned long flags;
  198. if (skb->protocol != htons(ETH_P_PHONET))
  199. goto out;
  200. spin_lock_irqsave(&port->lock, flags);
  201. fp = port->usb;
  202. if (unlikely(!fp)) /* race with carrier loss */
  203. goto out_unlock;
  204. req = fp->in_req;
  205. req->buf = skb->data;
  206. req->length = skb->len;
  207. req->complete = pn_tx_complete;
  208. req->zero = 1;
  209. req->context = skb;
  210. if (unlikely(usb_ep_queue(fp->in_ep, req, GFP_ATOMIC)))
  211. goto out_unlock;
  212. netif_stop_queue(dev);
  213. skb = NULL;
  214. out_unlock:
  215. spin_unlock_irqrestore(&port->lock, flags);
  216. out:
  217. if (unlikely(skb)) {
  218. dev_kfree_skb(skb);
  219. dev->stats.tx_dropped++;
  220. }
  221. return NETDEV_TX_OK;
  222. }
  223. static int pn_net_mtu(struct net_device *dev, int new_mtu)
  224. {
  225. if ((new_mtu < PHONET_MIN_MTU) || (new_mtu > PHONET_MAX_MTU))
  226. return -EINVAL;
  227. dev->mtu = new_mtu;
  228. return 0;
  229. }
  230. static const struct net_device_ops pn_netdev_ops = {
  231. .ndo_open = pn_net_open,
  232. .ndo_stop = pn_net_close,
  233. .ndo_start_xmit = pn_net_xmit,
  234. .ndo_change_mtu = pn_net_mtu,
  235. };
  236. static void pn_net_setup(struct net_device *dev)
  237. {
  238. dev->features = 0;
  239. dev->type = ARPHRD_PHONET;
  240. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  241. dev->mtu = PHONET_DEV_MTU;
  242. dev->hard_header_len = 1;
  243. dev->dev_addr[0] = PN_MEDIA_USB;
  244. dev->addr_len = 1;
  245. dev->tx_queue_len = 1;
  246. dev->netdev_ops = &pn_netdev_ops;
  247. dev->destructor = free_netdev;
  248. dev->header_ops = &phonet_header_ops;
  249. }
  250. /*-------------------------------------------------------------------------*/
  251. /*
  252. * Queue buffer for data from the host
  253. */
  254. static int
  255. pn_rx_submit(struct f_phonet *fp, struct usb_request *req, gfp_t gfp_flags)
  256. {
  257. struct net_device *dev = fp->dev;
  258. struct page *page;
  259. int err;
  260. page = __netdev_alloc_page(dev, gfp_flags);
  261. if (!page)
  262. return -ENOMEM;
  263. req->buf = page_address(page);
  264. req->length = PAGE_SIZE;
  265. req->context = page;
  266. err = usb_ep_queue(fp->out_ep, req, gfp_flags);
  267. if (unlikely(err))
  268. netdev_free_page(dev, page);
  269. return err;
  270. }
  271. static void pn_rx_complete(struct usb_ep *ep, struct usb_request *req)
  272. {
  273. struct f_phonet *fp = ep->driver_data;
  274. struct net_device *dev = fp->dev;
  275. struct page *page = req->context;
  276. struct sk_buff *skb;
  277. unsigned long flags;
  278. int status = req->status;
  279. switch (status) {
  280. case 0:
  281. spin_lock_irqsave(&fp->rx.lock, flags);
  282. skb = fp->rx.skb;
  283. if (!skb)
  284. skb = fp->rx.skb = netdev_alloc_skb(dev, 12);
  285. if (req->actual < req->length) /* Last fragment */
  286. fp->rx.skb = NULL;
  287. spin_unlock_irqrestore(&fp->rx.lock, flags);
  288. if (unlikely(!skb))
  289. break;
  290. if (skb->len == 0) { /* First fragment */
  291. skb->protocol = htons(ETH_P_PHONET);
  292. skb_reset_mac_header(skb);
  293. /* Can't use pskb_pull() on page in IRQ */
  294. memcpy(skb_put(skb, 1), page_address(page), 1);
  295. }
  296. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
  297. skb->len == 0, req->actual);
  298. page = NULL;
  299. if (req->actual < req->length) { /* Last fragment */
  300. skb->dev = dev;
  301. dev->stats.rx_packets++;
  302. dev->stats.rx_bytes += skb->len;
  303. netif_rx(skb);
  304. }
  305. break;
  306. /* Do not resubmit in these cases: */
  307. case -ESHUTDOWN: /* disconnect */
  308. case -ECONNABORTED: /* hw reset */
  309. case -ECONNRESET: /* dequeued (unlink or netif down) */
  310. req = NULL;
  311. break;
  312. /* Do resubmit in these cases: */
  313. case -EOVERFLOW: /* request buffer overflow */
  314. dev->stats.rx_over_errors++;
  315. default:
  316. dev->stats.rx_errors++;
  317. break;
  318. }
  319. if (page)
  320. netdev_free_page(dev, page);
  321. if (req)
  322. pn_rx_submit(fp, req, GFP_ATOMIC);
  323. }
  324. /*-------------------------------------------------------------------------*/
  325. static void __pn_reset(struct usb_function *f)
  326. {
  327. struct f_phonet *fp = func_to_pn(f);
  328. struct net_device *dev = fp->dev;
  329. struct phonet_port *port = netdev_priv(dev);
  330. netif_carrier_off(dev);
  331. port->usb = NULL;
  332. usb_ep_disable(fp->out_ep);
  333. usb_ep_disable(fp->in_ep);
  334. if (fp->rx.skb) {
  335. dev_kfree_skb_irq(fp->rx.skb);
  336. fp->rx.skb = NULL;
  337. }
  338. }
  339. static int pn_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  340. {
  341. struct f_phonet *fp = func_to_pn(f);
  342. struct usb_gadget *gadget = fp->function.config->cdev->gadget;
  343. if (intf == pn_control_intf_desc.bInterfaceNumber)
  344. /* control interface, no altsetting */
  345. return (alt > 0) ? -EINVAL : 0;
  346. if (intf == pn_data_intf_desc.bInterfaceNumber) {
  347. struct net_device *dev = fp->dev;
  348. struct phonet_port *port = netdev_priv(dev);
  349. /* data intf (0: inactive, 1: active) */
  350. if (alt > 1)
  351. return -EINVAL;
  352. spin_lock(&port->lock);
  353. __pn_reset(f);
  354. if (alt == 1) {
  355. struct usb_endpoint_descriptor *out, *in;
  356. int i;
  357. out = ep_choose(gadget,
  358. &pn_hs_sink_desc,
  359. &pn_fs_sink_desc);
  360. in = ep_choose(gadget,
  361. &pn_hs_source_desc,
  362. &pn_fs_source_desc);
  363. usb_ep_enable(fp->out_ep, out);
  364. usb_ep_enable(fp->in_ep, in);
  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. }