cdc-phonet.c 10 KB

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