f_phonet.c 15 KB

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