lc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 <linux/uwb/debug.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
  177. int i1480u_add(struct i1480u *i1480u, struct usb_interface *iface)
  178. {
  179. int result = -ENODEV;
  180. struct wlp *wlp = &i1480u->wlp;
  181. struct usb_device *usb_dev = interface_to_usbdev(iface);
  182. struct net_device *net_dev = i1480u->net_dev;
  183. struct uwb_rc *rc;
  184. struct uwb_dev *uwb_dev;
  185. #ifdef i1480u_FLOW_CONTROL
  186. struct usb_endpoint_descriptor *epd;
  187. #endif
  188. i1480u->usb_dev = usb_get_dev(usb_dev);
  189. i1480u->usb_iface = iface;
  190. rc = uwb_rc_get_by_grandpa(&i1480u->usb_dev->dev);
  191. if (rc == NULL) {
  192. dev_err(&iface->dev, "Cannot get associated UWB Radio "
  193. "Controller\n");
  194. goto out;
  195. }
  196. wlp->xmit_frame = i1480u_xmit_frame;
  197. wlp->fill_device_info = i1480u_fill_device_info;
  198. wlp->stop_queue = i1480u_stop_queue;
  199. wlp->start_queue = i1480u_start_queue;
  200. result = wlp_setup(wlp, rc, net_dev);
  201. if (result < 0) {
  202. dev_err(&iface->dev, "Cannot setup WLP\n");
  203. goto error_wlp_setup;
  204. }
  205. result = 0;
  206. ether_setup(net_dev); /* make it an etherdevice */
  207. uwb_dev = &rc->uwb_dev;
  208. /* FIXME: hookup address change notifications? */
  209. memcpy(net_dev->dev_addr, uwb_dev->mac_addr.data,
  210. sizeof(net_dev->dev_addr));
  211. net_dev->hard_header_len = sizeof(struct untd_hdr_cmp)
  212. + sizeof(struct wlp_tx_hdr)
  213. + WLP_DATA_HLEN
  214. + ETH_HLEN;
  215. net_dev->mtu = 3500;
  216. net_dev->tx_queue_len = 20; /* FIXME: maybe use 1000? */
  217. /* net_dev->flags &= ~IFF_BROADCAST; FIXME: BUG in firmware */
  218. /* FIXME: multicast disabled */
  219. net_dev->flags &= ~IFF_MULTICAST;
  220. net_dev->features &= ~NETIF_F_SG;
  221. net_dev->features &= ~NETIF_F_FRAGLIST;
  222. /* All NETIF_F_*_CSUM disabled */
  223. net_dev->features |= NETIF_F_HIGHDMA;
  224. net_dev->watchdog_timeo = 5*HZ; /* FIXME: a better default? */
  225. net_dev->open = i1480u_open;
  226. net_dev->stop = i1480u_stop;
  227. net_dev->hard_start_xmit = i1480u_hard_start_xmit;
  228. net_dev->tx_timeout = i1480u_tx_timeout;
  229. net_dev->get_stats = i1480u_get_stats;
  230. net_dev->set_config = i1480u_set_config;
  231. net_dev->change_mtu = i1480u_change_mtu;
  232. #ifdef i1480u_FLOW_CONTROL
  233. /* Notification endpoint setup (submitted when we open the device) */
  234. i1480u->notif_urb = usb_alloc_urb(0, GFP_KERNEL);
  235. if (i1480u->notif_urb == NULL) {
  236. dev_err(&iface->dev, "Unable to allocate notification URB\n");
  237. result = -ENOMEM;
  238. goto error_urb_alloc;
  239. }
  240. epd = &iface->cur_altsetting->endpoint[0].desc;
  241. usb_fill_int_urb(i1480u->notif_urb, usb_dev,
  242. usb_rcvintpipe(usb_dev, epd->bEndpointAddress),
  243. i1480u->notif_buffer, sizeof(i1480u->notif_buffer),
  244. i1480u_notif_cb, i1480u, epd->bInterval);
  245. #endif
  246. i1480u->tx_inflight.max = i1480u_TX_INFLIGHT_MAX;
  247. i1480u->tx_inflight.threshold = i1480u_TX_INFLIGHT_THRESHOLD;
  248. i1480u->tx_inflight.restart_ts = jiffies;
  249. usb_set_intfdata(iface, i1480u);
  250. return result;
  251. #ifdef i1480u_FLOW_CONTROL
  252. error_urb_alloc:
  253. #endif
  254. wlp_remove(wlp);
  255. error_wlp_setup:
  256. uwb_rc_put(rc);
  257. out:
  258. usb_put_dev(i1480u->usb_dev);
  259. return result;
  260. }
  261. static void i1480u_rm(struct i1480u *i1480u)
  262. {
  263. struct uwb_rc *rc = i1480u->wlp.rc;
  264. usb_set_intfdata(i1480u->usb_iface, NULL);
  265. #ifdef i1480u_FLOW_CONTROL
  266. usb_kill_urb(i1480u->notif_urb);
  267. usb_free_urb(i1480u->notif_urb);
  268. #endif
  269. wlp_remove(&i1480u->wlp);
  270. uwb_rc_put(rc);
  271. usb_put_dev(i1480u->usb_dev);
  272. }
  273. /** Just setup @net_dev's i1480u private data */
  274. static void i1480u_netdev_setup(struct net_device *net_dev)
  275. {
  276. struct i1480u *i1480u = netdev_priv(net_dev);
  277. /* Initialize @i1480u */
  278. memset(i1480u, 0, sizeof(*i1480u));
  279. i1480u_init(i1480u);
  280. }
  281. /**
  282. * Probe a i1480u interface and register it
  283. *
  284. * @iface: USB interface to link to
  285. * @id: USB class/subclass/protocol id
  286. * @returns: 0 if ok, < 0 errno code on error.
  287. *
  288. * Does basic housekeeping stuff and then allocs a netdev with space
  289. * for the i1480u data. Initializes, registers in i1480u, registers in
  290. * netdev, ready to go.
  291. */
  292. static int i1480u_probe(struct usb_interface *iface,
  293. const struct usb_device_id *id)
  294. {
  295. int result;
  296. struct net_device *net_dev;
  297. struct device *dev = &iface->dev;
  298. struct i1480u *i1480u;
  299. /* Allocate instance [calls i1480u_netdev_setup() on it] */
  300. result = -ENOMEM;
  301. net_dev = alloc_netdev(sizeof(*i1480u), "wlp%d", i1480u_netdev_setup);
  302. if (net_dev == NULL) {
  303. dev_err(dev, "no memory for network device instance\n");
  304. goto error_alloc_netdev;
  305. }
  306. SET_NETDEV_DEV(net_dev, dev);
  307. i1480u = netdev_priv(net_dev);
  308. i1480u->net_dev = net_dev;
  309. result = i1480u_add(i1480u, iface); /* Now setup all the wlp stuff */
  310. if (result < 0) {
  311. dev_err(dev, "cannot add i1480u device: %d\n", result);
  312. goto error_i1480u_add;
  313. }
  314. result = register_netdev(net_dev); /* Okey dokey, bring it up */
  315. if (result < 0) {
  316. dev_err(dev, "cannot register network device: %d\n", result);
  317. goto error_register_netdev;
  318. }
  319. i1480u_sysfs_setup(i1480u);
  320. if (result < 0)
  321. goto error_sysfs_init;
  322. return 0;
  323. error_sysfs_init:
  324. unregister_netdev(net_dev);
  325. error_register_netdev:
  326. i1480u_rm(i1480u);
  327. error_i1480u_add:
  328. free_netdev(net_dev);
  329. error_alloc_netdev:
  330. return result;
  331. }
  332. /**
  333. * Disconect a i1480u from the system.
  334. *
  335. * i1480u_stop() has been called before, so al the rx and tx contexts
  336. * have been taken down already. Make sure the queue is stopped,
  337. * unregister netdev and i1480u, free and kill.
  338. */
  339. static void i1480u_disconnect(struct usb_interface *iface)
  340. {
  341. struct i1480u *i1480u;
  342. struct net_device *net_dev;
  343. i1480u = usb_get_intfdata(iface);
  344. net_dev = i1480u->net_dev;
  345. netif_stop_queue(net_dev);
  346. #ifdef i1480u_FLOW_CONTROL
  347. usb_kill_urb(i1480u->notif_urb);
  348. #endif
  349. i1480u_sysfs_release(i1480u);
  350. unregister_netdev(net_dev);
  351. i1480u_rm(i1480u);
  352. free_netdev(net_dev);
  353. }
  354. static struct usb_device_id i1480u_id_table[] = {
  355. {
  356. .match_flags = USB_DEVICE_ID_MATCH_DEVICE \
  357. | USB_DEVICE_ID_MATCH_DEV_INFO \
  358. | USB_DEVICE_ID_MATCH_INT_INFO,
  359. .idVendor = 0x8086,
  360. .idProduct = 0x0c3b,
  361. .bDeviceClass = 0xef,
  362. .bDeviceSubClass = 0x02,
  363. .bDeviceProtocol = 0x02,
  364. .bInterfaceClass = 0xff,
  365. .bInterfaceSubClass = 0xff,
  366. .bInterfaceProtocol = 0xff,
  367. },
  368. {},
  369. };
  370. MODULE_DEVICE_TABLE(usb, i1480u_id_table);
  371. static struct usb_driver i1480u_driver = {
  372. .name = KBUILD_MODNAME,
  373. .probe = i1480u_probe,
  374. .disconnect = i1480u_disconnect,
  375. .id_table = i1480u_id_table,
  376. };
  377. static int __init i1480u_driver_init(void)
  378. {
  379. return usb_register(&i1480u_driver);
  380. }
  381. module_init(i1480u_driver_init);
  382. static void __exit i1480u_driver_exit(void)
  383. {
  384. usb_deregister(&i1480u_driver);
  385. }
  386. module_exit(i1480u_driver_exit);
  387. MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
  388. MODULE_DESCRIPTION("i1480 Wireless UWB Link WLP networking for USB");
  389. MODULE_LICENSE("GPL");