cdc-phonet.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * phonet.c -- USB CDC Phonet host driver
  3. *
  4. * Copyright (C) 2008-2009 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/module.h>
  24. #include <linux/gfp.h>
  25. #include <linux/usb.h>
  26. #include <linux/usb/cdc.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/if_arp.h>
  29. #include <linux/if_phonet.h>
  30. #include <linux/phonet.h>
  31. #define PN_MEDIA_USB 0x1B
  32. static const unsigned rxq_size = 17;
  33. struct usbpn_dev {
  34. struct net_device *dev;
  35. struct usb_interface *intf, *data_intf;
  36. struct usb_device *usb;
  37. unsigned int tx_pipe, rx_pipe;
  38. u8 active_setting;
  39. u8 disconnected;
  40. unsigned tx_queue;
  41. spinlock_t tx_lock;
  42. spinlock_t rx_lock;
  43. struct sk_buff *rx_skb;
  44. struct urb *urbs[0];
  45. };
  46. static void tx_complete(struct urb *req);
  47. static void rx_complete(struct urb *req);
  48. /*
  49. * Network device callbacks
  50. */
  51. static netdev_tx_t usbpn_xmit(struct sk_buff *skb, struct net_device *dev)
  52. {
  53. struct usbpn_dev *pnd = netdev_priv(dev);
  54. struct urb *req = NULL;
  55. unsigned long flags;
  56. int err;
  57. if (skb->protocol != htons(ETH_P_PHONET))
  58. goto drop;
  59. req = usb_alloc_urb(0, GFP_ATOMIC);
  60. if (!req)
  61. goto drop;
  62. usb_fill_bulk_urb(req, pnd->usb, pnd->tx_pipe, skb->data, skb->len,
  63. tx_complete, skb);
  64. req->transfer_flags = URB_ZERO_PACKET;
  65. err = usb_submit_urb(req, GFP_ATOMIC);
  66. if (err) {
  67. usb_free_urb(req);
  68. goto drop;
  69. }
  70. spin_lock_irqsave(&pnd->tx_lock, flags);
  71. pnd->tx_queue++;
  72. if (pnd->tx_queue >= dev->tx_queue_len)
  73. netif_stop_queue(dev);
  74. spin_unlock_irqrestore(&pnd->tx_lock, flags);
  75. return NETDEV_TX_OK;
  76. drop:
  77. dev_kfree_skb(skb);
  78. dev->stats.tx_dropped++;
  79. return NETDEV_TX_OK;
  80. }
  81. static void tx_complete(struct urb *req)
  82. {
  83. struct sk_buff *skb = req->context;
  84. struct net_device *dev = skb->dev;
  85. struct usbpn_dev *pnd = netdev_priv(dev);
  86. int status = req->status;
  87. switch (status) {
  88. case 0:
  89. dev->stats.tx_bytes += skb->len;
  90. break;
  91. case -ENOENT:
  92. case -ECONNRESET:
  93. case -ESHUTDOWN:
  94. dev->stats.tx_aborted_errors++;
  95. default:
  96. dev->stats.tx_errors++;
  97. dev_dbg(&dev->dev, "TX error (%d)\n", status);
  98. }
  99. dev->stats.tx_packets++;
  100. spin_lock(&pnd->tx_lock);
  101. pnd->tx_queue--;
  102. netif_wake_queue(dev);
  103. spin_unlock(&pnd->tx_lock);
  104. dev_kfree_skb_any(skb);
  105. usb_free_urb(req);
  106. }
  107. static int rx_submit(struct usbpn_dev *pnd, struct urb *req, gfp_t gfp_flags)
  108. {
  109. struct net_device *dev = pnd->dev;
  110. struct page *page;
  111. int err;
  112. page = __netdev_alloc_page(dev, gfp_flags);
  113. if (!page)
  114. return -ENOMEM;
  115. usb_fill_bulk_urb(req, pnd->usb, pnd->rx_pipe, page_address(page),
  116. PAGE_SIZE, rx_complete, dev);
  117. req->transfer_flags = 0;
  118. err = usb_submit_urb(req, gfp_flags);
  119. if (unlikely(err)) {
  120. dev_dbg(&dev->dev, "RX submit error (%d)\n", err);
  121. netdev_free_page(dev, page);
  122. }
  123. return err;
  124. }
  125. static void rx_complete(struct urb *req)
  126. {
  127. struct net_device *dev = req->context;
  128. struct usbpn_dev *pnd = netdev_priv(dev);
  129. struct page *page = virt_to_page(req->transfer_buffer);
  130. struct sk_buff *skb;
  131. unsigned long flags;
  132. int status = req->status;
  133. switch (status) {
  134. case 0:
  135. spin_lock_irqsave(&pnd->rx_lock, flags);
  136. skb = pnd->rx_skb;
  137. if (!skb) {
  138. skb = pnd->rx_skb = netdev_alloc_skb(dev, 12);
  139. if (likely(skb)) {
  140. /* Can't use pskb_pull() on page in IRQ */
  141. memcpy(skb_put(skb, 1), page_address(page), 1);
  142. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
  143. page, 1, req->actual_length);
  144. page = NULL;
  145. }
  146. } else {
  147. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
  148. page, 0, req->actual_length);
  149. page = NULL;
  150. }
  151. if (req->actual_length < PAGE_SIZE)
  152. pnd->rx_skb = NULL; /* Last fragment */
  153. else
  154. skb = NULL;
  155. spin_unlock_irqrestore(&pnd->rx_lock, flags);
  156. if (skb) {
  157. skb->protocol = htons(ETH_P_PHONET);
  158. skb_reset_mac_header(skb);
  159. __skb_pull(skb, 1);
  160. skb->dev = dev;
  161. dev->stats.rx_packets++;
  162. dev->stats.rx_bytes += skb->len;
  163. netif_rx(skb);
  164. }
  165. goto resubmit;
  166. case -ENOENT:
  167. case -ECONNRESET:
  168. case -ESHUTDOWN:
  169. req = NULL;
  170. break;
  171. case -EOVERFLOW:
  172. dev->stats.rx_over_errors++;
  173. dev_dbg(&dev->dev, "RX overflow\n");
  174. break;
  175. case -EILSEQ:
  176. dev->stats.rx_crc_errors++;
  177. break;
  178. }
  179. dev->stats.rx_errors++;
  180. resubmit:
  181. if (page)
  182. netdev_free_page(dev, page);
  183. if (req)
  184. rx_submit(pnd, req, GFP_ATOMIC);
  185. }
  186. static int usbpn_close(struct net_device *dev);
  187. static int usbpn_open(struct net_device *dev)
  188. {
  189. struct usbpn_dev *pnd = netdev_priv(dev);
  190. int err;
  191. unsigned i;
  192. unsigned num = pnd->data_intf->cur_altsetting->desc.bInterfaceNumber;
  193. err = usb_set_interface(pnd->usb, num, pnd->active_setting);
  194. if (err)
  195. return err;
  196. for (i = 0; i < rxq_size; i++) {
  197. struct urb *req = usb_alloc_urb(0, GFP_KERNEL);
  198. if (!req || rx_submit(pnd, req, GFP_KERNEL)) {
  199. usbpn_close(dev);
  200. return -ENOMEM;
  201. }
  202. pnd->urbs[i] = req;
  203. }
  204. netif_wake_queue(dev);
  205. return 0;
  206. }
  207. static int usbpn_close(struct net_device *dev)
  208. {
  209. struct usbpn_dev *pnd = netdev_priv(dev);
  210. unsigned i;
  211. unsigned num = pnd->data_intf->cur_altsetting->desc.bInterfaceNumber;
  212. netif_stop_queue(dev);
  213. for (i = 0; i < rxq_size; i++) {
  214. struct urb *req = pnd->urbs[i];
  215. if (!req)
  216. continue;
  217. usb_kill_urb(req);
  218. usb_free_urb(req);
  219. pnd->urbs[i] = NULL;
  220. }
  221. return usb_set_interface(pnd->usb, num, !pnd->active_setting);
  222. }
  223. static int usbpn_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  224. {
  225. struct if_phonet_req *req = (struct if_phonet_req *)ifr;
  226. switch (cmd) {
  227. case SIOCPNGAUTOCONF:
  228. req->ifr_phonet_autoconf.device = PN_DEV_PC;
  229. return 0;
  230. }
  231. return -ENOIOCTLCMD;
  232. }
  233. static int usbpn_set_mtu(struct net_device *dev, int new_mtu)
  234. {
  235. if ((new_mtu < PHONET_MIN_MTU) || (new_mtu > PHONET_MAX_MTU))
  236. return -EINVAL;
  237. dev->mtu = new_mtu;
  238. return 0;
  239. }
  240. static const struct net_device_ops usbpn_ops = {
  241. .ndo_open = usbpn_open,
  242. .ndo_stop = usbpn_close,
  243. .ndo_start_xmit = usbpn_xmit,
  244. .ndo_do_ioctl = usbpn_ioctl,
  245. .ndo_change_mtu = usbpn_set_mtu,
  246. };
  247. static void usbpn_setup(struct net_device *dev)
  248. {
  249. dev->features = 0;
  250. dev->netdev_ops = &usbpn_ops,
  251. dev->header_ops = &phonet_header_ops;
  252. dev->type = ARPHRD_PHONET;
  253. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  254. dev->mtu = PHONET_MAX_MTU;
  255. dev->hard_header_len = 1;
  256. dev->dev_addr[0] = PN_MEDIA_USB;
  257. dev->addr_len = 1;
  258. dev->tx_queue_len = 3;
  259. dev->destructor = free_netdev;
  260. }
  261. /*
  262. * USB driver callbacks
  263. */
  264. static struct usb_device_id usbpn_ids[] = {
  265. {
  266. .match_flags = USB_DEVICE_ID_MATCH_VENDOR
  267. | USB_DEVICE_ID_MATCH_INT_CLASS
  268. | USB_DEVICE_ID_MATCH_INT_SUBCLASS,
  269. .idVendor = 0x0421, /* Nokia */
  270. .bInterfaceClass = USB_CLASS_COMM,
  271. .bInterfaceSubClass = 0xFE,
  272. },
  273. { },
  274. };
  275. MODULE_DEVICE_TABLE(usb, usbpn_ids);
  276. static struct usb_driver usbpn_driver;
  277. int usbpn_probe(struct usb_interface *intf, const struct usb_device_id *id)
  278. {
  279. static const char ifname[] = "usbpn%d";
  280. const struct usb_cdc_union_desc *union_header = NULL;
  281. const struct usb_host_interface *data_desc;
  282. struct usb_interface *data_intf;
  283. struct usb_device *usbdev = interface_to_usbdev(intf);
  284. struct net_device *dev;
  285. struct usbpn_dev *pnd;
  286. u8 *data;
  287. int phonet = 0;
  288. int len, err;
  289. data = intf->altsetting->extra;
  290. len = intf->altsetting->extralen;
  291. while (len >= 3) {
  292. u8 dlen = data[0];
  293. if (dlen < 3)
  294. return -EINVAL;
  295. /* bDescriptorType */
  296. if (data[1] == USB_DT_CS_INTERFACE) {
  297. /* bDescriptorSubType */
  298. switch (data[2]) {
  299. case USB_CDC_UNION_TYPE:
  300. if (union_header || dlen < 5)
  301. break;
  302. union_header =
  303. (struct usb_cdc_union_desc *)data;
  304. break;
  305. case 0xAB:
  306. phonet = 1;
  307. break;
  308. }
  309. }
  310. data += dlen;
  311. len -= dlen;
  312. }
  313. if (!union_header || !phonet)
  314. return -EINVAL;
  315. data_intf = usb_ifnum_to_if(usbdev, union_header->bSlaveInterface0);
  316. if (data_intf == NULL)
  317. return -ENODEV;
  318. /* Data interface has one inactive and one active setting */
  319. if (data_intf->num_altsetting != 2)
  320. return -EINVAL;
  321. if (data_intf->altsetting[0].desc.bNumEndpoints == 0 &&
  322. data_intf->altsetting[1].desc.bNumEndpoints == 2)
  323. data_desc = data_intf->altsetting + 1;
  324. else
  325. if (data_intf->altsetting[0].desc.bNumEndpoints == 2 &&
  326. data_intf->altsetting[1].desc.bNumEndpoints == 0)
  327. data_desc = data_intf->altsetting;
  328. else
  329. return -EINVAL;
  330. dev = alloc_netdev(sizeof(*pnd) + sizeof(pnd->urbs[0]) * rxq_size,
  331. ifname, usbpn_setup);
  332. if (!dev)
  333. return -ENOMEM;
  334. pnd = netdev_priv(dev);
  335. SET_NETDEV_DEV(dev, &intf->dev);
  336. pnd->dev = dev;
  337. pnd->usb = usb_get_dev(usbdev);
  338. pnd->intf = intf;
  339. pnd->data_intf = data_intf;
  340. spin_lock_init(&pnd->tx_lock);
  341. spin_lock_init(&pnd->rx_lock);
  342. /* Endpoints */
  343. if (usb_pipein(data_desc->endpoint[0].desc.bEndpointAddress)) {
  344. pnd->rx_pipe = usb_rcvbulkpipe(usbdev,
  345. data_desc->endpoint[0].desc.bEndpointAddress);
  346. pnd->tx_pipe = usb_sndbulkpipe(usbdev,
  347. data_desc->endpoint[1].desc.bEndpointAddress);
  348. } else {
  349. pnd->rx_pipe = usb_rcvbulkpipe(usbdev,
  350. data_desc->endpoint[1].desc.bEndpointAddress);
  351. pnd->tx_pipe = usb_sndbulkpipe(usbdev,
  352. data_desc->endpoint[0].desc.bEndpointAddress);
  353. }
  354. pnd->active_setting = data_desc - data_intf->altsetting;
  355. err = usb_driver_claim_interface(&usbpn_driver, data_intf, pnd);
  356. if (err)
  357. goto out;
  358. /* Force inactive mode until the network device is brought UP */
  359. usb_set_interface(usbdev, union_header->bSlaveInterface0,
  360. !pnd->active_setting);
  361. usb_set_intfdata(intf, pnd);
  362. err = register_netdev(dev);
  363. if (err) {
  364. usb_driver_release_interface(&usbpn_driver, data_intf);
  365. goto out;
  366. }
  367. dev_dbg(&dev->dev, "USB CDC Phonet device found\n");
  368. return 0;
  369. out:
  370. usb_set_intfdata(intf, NULL);
  371. free_netdev(dev);
  372. return err;
  373. }
  374. static void usbpn_disconnect(struct usb_interface *intf)
  375. {
  376. struct usbpn_dev *pnd = usb_get_intfdata(intf);
  377. struct usb_device *usb = pnd->usb;
  378. if (pnd->disconnected)
  379. return;
  380. pnd->disconnected = 1;
  381. usb_driver_release_interface(&usbpn_driver,
  382. (pnd->intf == intf) ? pnd->data_intf : pnd->intf);
  383. unregister_netdev(pnd->dev);
  384. usb_put_dev(usb);
  385. }
  386. static struct usb_driver usbpn_driver = {
  387. .name = "cdc_phonet",
  388. .probe = usbpn_probe,
  389. .disconnect = usbpn_disconnect,
  390. .id_table = usbpn_ids,
  391. };
  392. static int __init usbpn_init(void)
  393. {
  394. return usb_register(&usbpn_driver);
  395. }
  396. static void __exit usbpn_exit(void)
  397. {
  398. usb_deregister(&usbpn_driver);
  399. }
  400. module_init(usbpn_init);
  401. module_exit(usbpn_exit);
  402. MODULE_AUTHOR("Remi Denis-Courmont");
  403. MODULE_DESCRIPTION("USB CDC Phonet host interface");
  404. MODULE_LICENSE("GPL");