lc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * WUSB Wire Adapter: WLP interface
  3. * Driver for the Linux Network stack.
  4. *
  5. * Copyright (C) 2005-2006 Intel Corporation
  6. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  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 version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU 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 Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA.
  21. *
  22. *
  23. * FIXME: docs
  24. *
  25. * This implements a very simple network driver for the WLP USB
  26. * device that is associated to a UWB (Ultra Wide Band) host.
  27. *
  28. * This is seen as an interface of a composite device. Once the UWB
  29. * host has an association to another WLP capable device, the
  30. * networking interface (aka WLP) can start to send packets back and
  31. * forth.
  32. *
  33. * Limitations:
  34. *
  35. * - Hand cranked; can't ifup the interface until there is an association
  36. *
  37. * - BW allocation very simplistic [see i1480u_mas_set() and callees].
  38. *
  39. *
  40. * ROADMAP:
  41. *
  42. * ENTRY POINTS (driver model):
  43. *
  44. * i1480u_driver_{exit,init}(): initialization of the driver.
  45. *
  46. * i1480u_probe(): called by the driver code when a device
  47. * matching 'i1480u_id_table' is connected.
  48. *
  49. * This allocs a netdev instance, inits with
  50. * i1480u_add(), then registers_netdev().
  51. * i1480u_init()
  52. * i1480u_add()
  53. *
  54. * i1480u_disconnect(): device has been disconnected/module
  55. * is being removed.
  56. * i1480u_rm()
  57. */
  58. #include <linux/gfp.h>
  59. #include <linux/if_arp.h>
  60. #include <linux/etherdevice.h>
  61. #include "i1480u-wlp.h"
  62. static inline
  63. void i1480u_init(struct i1480u *i1480u)
  64. {
  65. /* nothing so far... doesn't it suck? */
  66. spin_lock_init(&i1480u->lock);
  67. INIT_LIST_HEAD(&i1480u->tx_list);
  68. spin_lock_init(&i1480u->tx_list_lock);
  69. wlp_options_init(&i1480u->options);
  70. edc_init(&i1480u->tx_errors);
  71. edc_init(&i1480u->rx_errors);
  72. #ifdef i1480u_FLOW_CONTROL
  73. edc_init(&i1480u->notif_edc);
  74. #endif
  75. stats_init(&i1480u->lqe_stats);
  76. stats_init(&i1480u->rssi_stats);
  77. wlp_init(&i1480u->wlp);
  78. }
  79. /**
  80. * Fill WLP device information structure
  81. *
  82. * The structure will contain a few character arrays, each ending with a
  83. * null terminated string. Each string has to fit (excluding terminating
  84. * character) into a specified range obtained from the WLP substack.
  85. *
  86. * It is still not clear exactly how this device information should be
  87. * obtained. Until we find out we use the USB device descriptor as backup, some
  88. * information elements have intuitive mappings, other not.
  89. */
  90. static
  91. void i1480u_fill_device_info(struct wlp *wlp, struct wlp_device_info *dev_info)
  92. {
  93. struct i1480u *i1480u = container_of(wlp, struct i1480u, wlp);
  94. struct usb_device *usb_dev = i1480u->usb_dev;
  95. /* Treat device name and model name the same */
  96. if (usb_dev->descriptor.iProduct) {
  97. usb_string(usb_dev, usb_dev->descriptor.iProduct,
  98. dev_info->name, sizeof(dev_info->name));
  99. usb_string(usb_dev, usb_dev->descriptor.iProduct,
  100. dev_info->model_name, sizeof(dev_info->model_name));
  101. }
  102. if (usb_dev->descriptor.iManufacturer)
  103. usb_string(usb_dev, usb_dev->descriptor.iManufacturer,
  104. dev_info->manufacturer,
  105. sizeof(dev_info->manufacturer));
  106. scnprintf(dev_info->model_nr, sizeof(dev_info->model_nr), "%04x",
  107. __le16_to_cpu(usb_dev->descriptor.bcdDevice));
  108. if (usb_dev->descriptor.iSerialNumber)
  109. usb_string(usb_dev, usb_dev->descriptor.iSerialNumber,
  110. dev_info->serial, sizeof(dev_info->serial));
  111. /* FIXME: where should we obtain category? */
  112. dev_info->prim_dev_type.category = cpu_to_le16(WLP_DEV_CAT_OTHER);
  113. /* FIXME: Complete OUI and OUIsubdiv attributes */
  114. }
  115. #ifdef i1480u_FLOW_CONTROL
  116. /**
  117. * Callback for the notification endpoint
  118. *
  119. * This mostly controls the xon/xoff protocol. In case of hard error,
  120. * we stop the queue. If not, we always retry.
  121. */
  122. static
  123. void i1480u_notif_cb(struct urb *urb, struct pt_regs *regs)
  124. {
  125. struct i1480u *i1480u = urb->context;
  126. struct usb_interface *usb_iface = i1480u->usb_iface;
  127. struct device *dev = &usb_iface->dev;
  128. int result;
  129. switch (urb->status) {
  130. case 0: /* Got valid data, do xon/xoff */
  131. switch (i1480u->notif_buffer[0]) {
  132. case 'N':
  133. dev_err(dev, "XOFF STOPPING queue at %lu\n", jiffies);
  134. netif_stop_queue(i1480u->net_dev);
  135. break;
  136. case 'A':
  137. dev_err(dev, "XON STARTING queue at %lu\n", jiffies);
  138. netif_start_queue(i1480u->net_dev);
  139. break;
  140. default:
  141. dev_err(dev, "NEP: unknown data 0x%02hhx\n",
  142. i1480u->notif_buffer[0]);
  143. }
  144. break;
  145. case -ECONNRESET: /* Controlled situation ... */
  146. case -ENOENT: /* we killed the URB... */
  147. dev_err(dev, "NEP: URB reset/noent %d\n", urb->status);
  148. goto error;
  149. case -ESHUTDOWN: /* going away! */
  150. dev_err(dev, "NEP: URB down %d\n", urb->status);
  151. goto error;
  152. default: /* Retry unless it gets ugly */
  153. if (edc_inc(&i1480u->notif_edc, EDC_MAX_ERRORS,
  154. EDC_ERROR_TIMEFRAME)) {
  155. dev_err(dev, "NEP: URB max acceptable errors "
  156. "exceeded; resetting device\n");
  157. goto error_reset;
  158. }
  159. dev_err(dev, "NEP: URB error %d\n", urb->status);
  160. break;
  161. }
  162. result = usb_submit_urb(urb, GFP_ATOMIC);
  163. if (result < 0) {
  164. dev_err(dev, "NEP: Can't resubmit URB: %d; resetting device\n",
  165. result);
  166. goto error_reset;
  167. }
  168. return;
  169. error_reset:
  170. wlp_reset_all(&i1480-wlp);
  171. error:
  172. netif_stop_queue(i1480u->net_dev);
  173. return;
  174. }
  175. #endif
  176. static const struct net_device_ops i1480u_netdev_ops = {
  177. .ndo_open = i1480u_open,
  178. .ndo_stop = i1480u_stop,
  179. .ndo_start_xmit = i1480u_hard_start_xmit,
  180. .ndo_tx_timeout = i1480u_tx_timeout,
  181. .ndo_set_config = i1480u_set_config,
  182. .ndo_change_mtu = i1480u_change_mtu,
  183. };
  184. static
  185. int i1480u_add(struct i1480u *i1480u, struct usb_interface *iface)
  186. {
  187. int result = -ENODEV;
  188. struct wlp *wlp = &i1480u->wlp;
  189. struct usb_device *usb_dev = interface_to_usbdev(iface);
  190. struct net_device *net_dev = i1480u->net_dev;
  191. struct uwb_rc *rc;
  192. struct uwb_dev *uwb_dev;
  193. #ifdef i1480u_FLOW_CONTROL
  194. struct usb_endpoint_descriptor *epd;
  195. #endif
  196. i1480u->usb_dev = usb_get_dev(usb_dev);
  197. i1480u->usb_iface = iface;
  198. rc = uwb_rc_get_by_grandpa(&i1480u->usb_dev->dev);
  199. if (rc == NULL) {
  200. dev_err(&iface->dev, "Cannot get associated UWB Radio "
  201. "Controller\n");
  202. goto out;
  203. }
  204. wlp->xmit_frame = i1480u_xmit_frame;
  205. wlp->fill_device_info = i1480u_fill_device_info;
  206. wlp->stop_queue = i1480u_stop_queue;
  207. wlp->start_queue = i1480u_start_queue;
  208. result = wlp_setup(wlp, rc, net_dev);
  209. if (result < 0) {
  210. dev_err(&iface->dev, "Cannot setup WLP\n");
  211. goto error_wlp_setup;
  212. }
  213. result = 0;
  214. ether_setup(net_dev); /* make it an etherdevice */
  215. uwb_dev = &rc->uwb_dev;
  216. /* FIXME: hookup address change notifications? */
  217. memcpy(net_dev->dev_addr, uwb_dev->mac_addr.data,
  218. sizeof(net_dev->dev_addr));
  219. net_dev->hard_header_len = sizeof(struct untd_hdr_cmp)
  220. + sizeof(struct wlp_tx_hdr)
  221. + WLP_DATA_HLEN
  222. + ETH_HLEN;
  223. net_dev->mtu = 3500;
  224. net_dev->tx_queue_len = 20; /* FIXME: maybe use 1000? */
  225. /* net_dev->flags &= ~IFF_BROADCAST; FIXME: BUG in firmware */
  226. /* FIXME: multicast disabled */
  227. net_dev->flags &= ~IFF_MULTICAST;
  228. net_dev->features &= ~NETIF_F_SG;
  229. net_dev->features &= ~NETIF_F_FRAGLIST;
  230. /* All NETIF_F_*_CSUM disabled */
  231. net_dev->features |= NETIF_F_HIGHDMA;
  232. net_dev->watchdog_timeo = 5*HZ; /* FIXME: a better default? */
  233. net_dev->netdev_ops = &i1480u_netdev_ops;
  234. #ifdef i1480u_FLOW_CONTROL
  235. /* Notification endpoint setup (submitted when we open the device) */
  236. i1480u->notif_urb = usb_alloc_urb(0, GFP_KERNEL);
  237. if (i1480u->notif_urb == NULL) {
  238. dev_err(&iface->dev, "Unable to allocate notification URB\n");
  239. result = -ENOMEM;
  240. goto error_urb_alloc;
  241. }
  242. epd = &iface->cur_altsetting->endpoint[0].desc;
  243. usb_fill_int_urb(i1480u->notif_urb, usb_dev,
  244. usb_rcvintpipe(usb_dev, epd->bEndpointAddress),
  245. i1480u->notif_buffer, sizeof(i1480u->notif_buffer),
  246. i1480u_notif_cb, i1480u, epd->bInterval);
  247. #endif
  248. i1480u->tx_inflight.max = i1480u_TX_INFLIGHT_MAX;
  249. i1480u->tx_inflight.threshold = i1480u_TX_INFLIGHT_THRESHOLD;
  250. i1480u->tx_inflight.restart_ts = jiffies;
  251. usb_set_intfdata(iface, i1480u);
  252. return result;
  253. #ifdef i1480u_FLOW_CONTROL
  254. error_urb_alloc:
  255. #endif
  256. wlp_remove(wlp);
  257. error_wlp_setup:
  258. uwb_rc_put(rc);
  259. out:
  260. usb_put_dev(i1480u->usb_dev);
  261. return result;
  262. }
  263. static void i1480u_rm(struct i1480u *i1480u)
  264. {
  265. struct uwb_rc *rc = i1480u->wlp.rc;
  266. usb_set_intfdata(i1480u->usb_iface, NULL);
  267. #ifdef i1480u_FLOW_CONTROL
  268. usb_kill_urb(i1480u->notif_urb);
  269. usb_free_urb(i1480u->notif_urb);
  270. #endif
  271. wlp_remove(&i1480u->wlp);
  272. uwb_rc_put(rc);
  273. usb_put_dev(i1480u->usb_dev);
  274. }
  275. /** Just setup @net_dev's i1480u private data */
  276. static void i1480u_netdev_setup(struct net_device *net_dev)
  277. {
  278. struct i1480u *i1480u = netdev_priv(net_dev);
  279. /* Initialize @i1480u */
  280. memset(i1480u, 0, sizeof(*i1480u));
  281. i1480u_init(i1480u);
  282. }
  283. /**
  284. * Probe a i1480u interface and register it
  285. *
  286. * @iface: USB interface to link to
  287. * @id: USB class/subclass/protocol id
  288. * @returns: 0 if ok, < 0 errno code on error.
  289. *
  290. * Does basic housekeeping stuff and then allocs a netdev with space
  291. * for the i1480u data. Initializes, registers in i1480u, registers in
  292. * netdev, ready to go.
  293. */
  294. static int i1480u_probe(struct usb_interface *iface,
  295. const struct usb_device_id *id)
  296. {
  297. int result;
  298. struct net_device *net_dev;
  299. struct device *dev = &iface->dev;
  300. struct i1480u *i1480u;
  301. /* Allocate instance [calls i1480u_netdev_setup() on it] */
  302. result = -ENOMEM;
  303. net_dev = alloc_netdev(sizeof(*i1480u), "wlp%d", i1480u_netdev_setup);
  304. if (net_dev == NULL) {
  305. dev_err(dev, "no memory for network device instance\n");
  306. goto error_alloc_netdev;
  307. }
  308. SET_NETDEV_DEV(net_dev, dev);
  309. i1480u = netdev_priv(net_dev);
  310. i1480u->net_dev = net_dev;
  311. result = i1480u_add(i1480u, iface); /* Now setup all the wlp stuff */
  312. if (result < 0) {
  313. dev_err(dev, "cannot add i1480u device: %d\n", result);
  314. goto error_i1480u_add;
  315. }
  316. result = register_netdev(net_dev); /* Okey dokey, bring it up */
  317. if (result < 0) {
  318. dev_err(dev, "cannot register network device: %d\n", result);
  319. goto error_register_netdev;
  320. }
  321. i1480u_sysfs_setup(i1480u);
  322. if (result < 0)
  323. goto error_sysfs_init;
  324. return 0;
  325. error_sysfs_init:
  326. unregister_netdev(net_dev);
  327. error_register_netdev:
  328. i1480u_rm(i1480u);
  329. error_i1480u_add:
  330. free_netdev(net_dev);
  331. error_alloc_netdev:
  332. return result;
  333. }
  334. /**
  335. * Disconect a i1480u from the system.
  336. *
  337. * i1480u_stop() has been called before, so al the rx and tx contexts
  338. * have been taken down already. Make sure the queue is stopped,
  339. * unregister netdev and i1480u, free and kill.
  340. */
  341. static void i1480u_disconnect(struct usb_interface *iface)
  342. {
  343. struct i1480u *i1480u;
  344. struct net_device *net_dev;
  345. i1480u = usb_get_intfdata(iface);
  346. net_dev = i1480u->net_dev;
  347. netif_stop_queue(net_dev);
  348. #ifdef i1480u_FLOW_CONTROL
  349. usb_kill_urb(i1480u->notif_urb);
  350. #endif
  351. i1480u_sysfs_release(i1480u);
  352. unregister_netdev(net_dev);
  353. i1480u_rm(i1480u);
  354. free_netdev(net_dev);
  355. }
  356. static struct usb_device_id i1480u_id_table[] = {
  357. {
  358. .match_flags = USB_DEVICE_ID_MATCH_DEVICE \
  359. | USB_DEVICE_ID_MATCH_DEV_INFO \
  360. | USB_DEVICE_ID_MATCH_INT_INFO,
  361. .idVendor = 0x8086,
  362. .idProduct = 0x0c3b,
  363. .bDeviceClass = 0xef,
  364. .bDeviceSubClass = 0x02,
  365. .bDeviceProtocol = 0x02,
  366. .bInterfaceClass = 0xff,
  367. .bInterfaceSubClass = 0xff,
  368. .bInterfaceProtocol = 0xff,
  369. },
  370. {},
  371. };
  372. MODULE_DEVICE_TABLE(usb, i1480u_id_table);
  373. static struct usb_driver i1480u_driver = {
  374. .name = KBUILD_MODNAME,
  375. .probe = i1480u_probe,
  376. .disconnect = i1480u_disconnect,
  377. .id_table = i1480u_id_table,
  378. };
  379. static int __init i1480u_driver_init(void)
  380. {
  381. return usb_register(&i1480u_driver);
  382. }
  383. module_init(i1480u_driver_init);
  384. static void __exit i1480u_driver_exit(void)
  385. {
  386. usb_deregister(&i1480u_driver);
  387. }
  388. module_exit(i1480u_driver_exit);
  389. MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
  390. MODULE_DESCRIPTION("i1480 Wireless UWB Link WLP networking for USB");
  391. MODULE_LICENSE("GPL");