f_phonet.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. /*-------------------------------------------------------------------------*/
  34. struct phonet_port {
  35. struct f_phonet *usb;
  36. spinlock_t lock;
  37. };
  38. struct f_phonet {
  39. struct usb_function function;
  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 = 2;
  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(512),
  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 0;
  213. }
  214. static int pn_net_mtu(struct net_device *dev, int new_mtu)
  215. {
  216. struct phonet_port *port = netdev_priv(dev);
  217. unsigned long flags;
  218. int err = -EBUSY;
  219. if ((new_mtu < PHONET_MIN_MTU) || (new_mtu > PHONET_MAX_MTU))
  220. return -EINVAL;
  221. spin_lock_irqsave(&port->lock, flags);
  222. if (!netif_carrier_ok(dev)) {
  223. dev->mtu = new_mtu;
  224. err = 0;
  225. }
  226. spin_unlock_irqrestore(&port->lock, flags);
  227. return err;
  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 sk_buff *skb;
  257. const size_t size = fp->dev->mtu;
  258. int err;
  259. skb = alloc_skb(size, gfp_flags);
  260. if (!skb)
  261. return -ENOMEM;
  262. req->buf = skb->data;
  263. req->length = size;
  264. req->context = skb;
  265. err = usb_ep_queue(fp->out_ep, req, gfp_flags);
  266. if (unlikely(err))
  267. dev_kfree_skb_any(skb);
  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 sk_buff *skb = req->context;
  275. int status = req->status;
  276. switch (status) {
  277. case 0:
  278. if (unlikely(!netif_running(dev)))
  279. break;
  280. if (unlikely(req->actual < 1))
  281. break;
  282. skb_put(skb, req->actual);
  283. skb->protocol = htons(ETH_P_PHONET);
  284. skb_reset_mac_header(skb);
  285. __skb_pull(skb, 1);
  286. skb->dev = dev;
  287. dev->stats.rx_packets++;
  288. dev->stats.rx_bytes += skb->len;
  289. netif_rx(skb);
  290. skb = NULL;
  291. break;
  292. /* Do not resubmit in these cases: */
  293. case -ESHUTDOWN: /* disconnect */
  294. case -ECONNABORTED: /* hw reset */
  295. case -ECONNRESET: /* dequeued (unlink or netif down) */
  296. req = NULL;
  297. break;
  298. /* Do resubmit in these cases: */
  299. case -EOVERFLOW: /* request buffer overflow */
  300. dev->stats.rx_over_errors++;
  301. default:
  302. dev->stats.rx_errors++;
  303. break;
  304. }
  305. if (skb)
  306. dev_kfree_skb_any(skb);
  307. if (req)
  308. pn_rx_submit(fp, req, GFP_ATOMIC);
  309. }
  310. /*-------------------------------------------------------------------------*/
  311. static void __pn_reset(struct usb_function *f)
  312. {
  313. struct f_phonet *fp = func_to_pn(f);
  314. struct net_device *dev = fp->dev;
  315. struct phonet_port *port = netdev_priv(dev);
  316. netif_carrier_off(dev);
  317. port->usb = NULL;
  318. usb_ep_disable(fp->out_ep);
  319. usb_ep_disable(fp->in_ep);
  320. }
  321. static int pn_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  322. {
  323. struct f_phonet *fp = func_to_pn(f);
  324. struct usb_gadget *gadget = fp->function.config->cdev->gadget;
  325. if (intf == pn_control_intf_desc.bInterfaceNumber)
  326. /* control interface, no altsetting */
  327. return (alt > 0) ? -EINVAL : 0;
  328. if (intf == pn_data_intf_desc.bInterfaceNumber) {
  329. struct net_device *dev = fp->dev;
  330. struct phonet_port *port = netdev_priv(dev);
  331. /* data intf (0: inactive, 1: active) */
  332. if (alt > 1)
  333. return -EINVAL;
  334. spin_lock(&port->lock);
  335. __pn_reset(f);
  336. if (alt == 1) {
  337. struct usb_endpoint_descriptor *out, *in;
  338. int i;
  339. out = ep_choose(gadget,
  340. &pn_hs_sink_desc,
  341. &pn_fs_sink_desc);
  342. in = ep_choose(gadget,
  343. &pn_hs_source_desc,
  344. &pn_fs_source_desc);
  345. usb_ep_enable(fp->out_ep, out);
  346. usb_ep_enable(fp->in_ep, in);
  347. port->usb = fp;
  348. fp->out_ep->driver_data = fp;
  349. fp->in_ep->driver_data = fp;
  350. netif_carrier_on(dev);
  351. for (i = 0; i < phonet_rxq_size; i++)
  352. pn_rx_submit(fp, fp->out_reqv[i], GFP_ATOMIC);
  353. }
  354. spin_unlock(&port->lock);
  355. return 0;
  356. }
  357. return -EINVAL;
  358. }
  359. static int pn_get_alt(struct usb_function *f, unsigned intf)
  360. {
  361. struct f_phonet *fp = func_to_pn(f);
  362. if (intf == pn_control_intf_desc.bInterfaceNumber)
  363. return 0;
  364. if (intf == pn_data_intf_desc.bInterfaceNumber) {
  365. struct phonet_port *port = netdev_priv(fp->dev);
  366. u8 alt;
  367. spin_lock(&port->lock);
  368. alt = port->usb != NULL;
  369. spin_unlock(&port->lock);
  370. return alt;
  371. }
  372. return -EINVAL;
  373. }
  374. static void pn_disconnect(struct usb_function *f)
  375. {
  376. struct f_phonet *fp = func_to_pn(f);
  377. struct phonet_port *port = netdev_priv(fp->dev);
  378. unsigned long flags;
  379. /* remain disabled until set_alt */
  380. spin_lock_irqsave(&port->lock, flags);
  381. __pn_reset(f);
  382. spin_unlock_irqrestore(&port->lock, flags);
  383. }
  384. /*-------------------------------------------------------------------------*/
  385. static __init
  386. int pn_bind(struct usb_configuration *c, struct usb_function *f)
  387. {
  388. struct usb_composite_dev *cdev = c->cdev;
  389. struct usb_gadget *gadget = cdev->gadget;
  390. struct f_phonet *fp = func_to_pn(f);
  391. struct usb_ep *ep;
  392. int status, i;
  393. /* Reserve interface IDs */
  394. status = usb_interface_id(c, f);
  395. if (status < 0)
  396. goto err;
  397. pn_control_intf_desc.bInterfaceNumber = status;
  398. pn_union_desc.bMasterInterface0 = status;
  399. status = usb_interface_id(c, f);
  400. if (status < 0)
  401. goto err;
  402. pn_data_nop_intf_desc.bInterfaceNumber = status;
  403. pn_data_intf_desc.bInterfaceNumber = status;
  404. pn_union_desc.bSlaveInterface0 = status;
  405. /* Reserve endpoints */
  406. status = -ENODEV;
  407. ep = usb_ep_autoconfig(gadget, &pn_fs_sink_desc);
  408. if (!ep)
  409. goto err;
  410. fp->out_ep = ep;
  411. ep->driver_data = fp; /* Claim */
  412. ep = usb_ep_autoconfig(gadget, &pn_fs_source_desc);
  413. if (!ep)
  414. goto err;
  415. fp->in_ep = ep;
  416. ep->driver_data = fp; /* Claim */
  417. pn_hs_sink_desc.bEndpointAddress =
  418. pn_fs_sink_desc.bEndpointAddress;
  419. pn_hs_source_desc.bEndpointAddress =
  420. pn_fs_source_desc.bEndpointAddress;
  421. /* Do not try to bind Phonet twice... */
  422. fp->function.descriptors = fs_pn_function;
  423. fp->function.hs_descriptors = hs_pn_function;
  424. /* Incoming USB requests */
  425. status = -ENOMEM;
  426. for (i = 0; i < phonet_rxq_size; i++) {
  427. struct usb_request *req;
  428. req = usb_ep_alloc_request(fp->out_ep, GFP_KERNEL);
  429. if (!req)
  430. goto err;
  431. req->complete = pn_rx_complete;
  432. fp->out_reqv[i] = req;
  433. }
  434. /* Outgoing USB requests */
  435. fp->in_req = usb_ep_alloc_request(fp->in_ep, GFP_KERNEL);
  436. if (!fp->in_req)
  437. goto err;
  438. INFO(cdev, "USB CDC Phonet function\n");
  439. INFO(cdev, "using %s, OUT %s, IN %s\n", cdev->gadget->name,
  440. fp->out_ep->name, fp->in_ep->name);
  441. return 0;
  442. err:
  443. if (fp->out_ep)
  444. fp->out_ep->driver_data = NULL;
  445. if (fp->in_ep)
  446. fp->in_ep->driver_data = NULL;
  447. ERROR(cdev, "USB CDC Phonet: cannot autoconfigure\n");
  448. return status;
  449. }
  450. static void
  451. pn_unbind(struct usb_configuration *c, struct usb_function *f)
  452. {
  453. struct f_phonet *fp = func_to_pn(f);
  454. int i;
  455. /* We are already disconnected */
  456. if (fp->in_req)
  457. usb_ep_free_request(fp->in_ep, fp->in_req);
  458. for (i = 0; i < phonet_rxq_size; i++)
  459. if (fp->out_reqv[i])
  460. usb_ep_free_request(fp->out_ep, fp->out_reqv[i]);
  461. kfree(fp);
  462. }
  463. /*-------------------------------------------------------------------------*/
  464. static struct net_device *dev;
  465. int __init phonet_bind_config(struct usb_configuration *c)
  466. {
  467. struct f_phonet *fp;
  468. int err, size;
  469. size = sizeof(*fp) + (phonet_rxq_size * sizeof(struct usb_request *));
  470. fp = kzalloc(size, GFP_KERNEL);
  471. if (!fp)
  472. return -ENOMEM;
  473. fp->dev = dev;
  474. fp->function.name = "phonet";
  475. fp->function.bind = pn_bind;
  476. fp->function.unbind = pn_unbind;
  477. fp->function.set_alt = pn_set_alt;
  478. fp->function.get_alt = pn_get_alt;
  479. fp->function.disable = pn_disconnect;
  480. err = usb_add_function(c, &fp->function);
  481. if (err)
  482. kfree(fp);
  483. return err;
  484. }
  485. int __init gphonet_setup(struct usb_gadget *gadget)
  486. {
  487. struct phonet_port *port;
  488. int err;
  489. /* Create net device */
  490. BUG_ON(dev);
  491. dev = alloc_netdev(sizeof(*port), "upnlink%d", pn_net_setup);
  492. if (!dev)
  493. return -ENOMEM;
  494. port = netdev_priv(dev);
  495. spin_lock_init(&port->lock);
  496. netif_carrier_off(dev);
  497. SET_NETDEV_DEV(dev, &gadget->dev);
  498. err = register_netdev(dev);
  499. if (err)
  500. free_netdev(dev);
  501. return err;
  502. }
  503. void gphonet_cleanup(void)
  504. {
  505. unregister_netdev(dev);
  506. }