lc.c 12 KB

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