usbnet.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. /*
  2. * USB Network driver infrastructure
  3. * Copyright (C) 2000-2005 by David Brownell
  4. * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /*
  21. * This is a generic "USB networking" framework that works with several
  22. * kinds of full and high speed networking devices: host-to-host cables,
  23. * smart usb peripherals, and actual Ethernet adapters.
  24. *
  25. * These devices usually differ in terms of control protocols (if they
  26. * even have one!) and sometimes they define new framing to wrap or batch
  27. * Ethernet packets. Otherwise, they talk to USB pretty much the same,
  28. * so interface (un)binding, endpoint I/O queues, fault handling, and other
  29. * issues can usefully be addressed by this framework.
  30. */
  31. // #define DEBUG // error path messages, extra info
  32. // #define VERBOSE // more; success messages
  33. #include <linux/config.h>
  34. #ifdef CONFIG_USB_DEBUG
  35. # define DEBUG
  36. #endif
  37. #include <linux/module.h>
  38. #include <linux/sched.h>
  39. #include <linux/init.h>
  40. #include <linux/netdevice.h>
  41. #include <linux/etherdevice.h>
  42. #include <linux/ethtool.h>
  43. #include <linux/workqueue.h>
  44. #include <linux/mii.h>
  45. #include <linux/usb.h>
  46. #include "usbnet.h"
  47. #define DRIVER_VERSION "22-Aug-2005"
  48. /*-------------------------------------------------------------------------*/
  49. /*
  50. * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
  51. * Several dozen bytes of IPv4 data can fit in two such transactions.
  52. * One maximum size Ethernet packet takes twenty four of them.
  53. * For high speed, each frame comfortably fits almost 36 max size
  54. * Ethernet packets (so queues should be bigger).
  55. *
  56. * REVISIT qlens should be members of 'struct usbnet'; the goal is to
  57. * let the USB host controller be busy for 5msec or more before an irq
  58. * is required, under load. Jumbograms change the equation.
  59. */
  60. #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
  61. #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
  62. // reawaken network queue this soon after stopping; else watchdog barks
  63. #define TX_TIMEOUT_JIFFIES (5*HZ)
  64. // throttle rx/tx briefly after some faults, so khubd might disconnect()
  65. // us (it polls at HZ/4 usually) before we report too many false errors.
  66. #define THROTTLE_JIFFIES (HZ/8)
  67. // between wakeups
  68. #define UNLINK_TIMEOUT_MS 3
  69. /*-------------------------------------------------------------------------*/
  70. // randomly generated ethernet address
  71. static u8 node_id [ETH_ALEN];
  72. static const char driver_name [] = "usbnet";
  73. /* use ethtool to change the level for any given device */
  74. static int msg_level = -1;
  75. module_param (msg_level, int, 0);
  76. MODULE_PARM_DESC (msg_level, "Override default message level");
  77. /*-------------------------------------------------------------------------*/
  78. /* handles CDC Ethernet and many other network "bulk data" interfaces */
  79. int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
  80. {
  81. int tmp;
  82. struct usb_host_interface *alt = NULL;
  83. struct usb_host_endpoint *in = NULL, *out = NULL;
  84. struct usb_host_endpoint *status = NULL;
  85. for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
  86. unsigned ep;
  87. in = out = status = NULL;
  88. alt = intf->altsetting + tmp;
  89. /* take the first altsetting with in-bulk + out-bulk;
  90. * remember any status endpoint, just in case;
  91. * ignore other endpoints and altsetttings.
  92. */
  93. for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
  94. struct usb_host_endpoint *e;
  95. int intr = 0;
  96. e = alt->endpoint + ep;
  97. switch (e->desc.bmAttributes) {
  98. case USB_ENDPOINT_XFER_INT:
  99. if (!(e->desc.bEndpointAddress & USB_DIR_IN))
  100. continue;
  101. intr = 1;
  102. /* FALLTHROUGH */
  103. case USB_ENDPOINT_XFER_BULK:
  104. break;
  105. default:
  106. continue;
  107. }
  108. if (e->desc.bEndpointAddress & USB_DIR_IN) {
  109. if (!intr && !in)
  110. in = e;
  111. else if (intr && !status)
  112. status = e;
  113. } else {
  114. if (!out)
  115. out = e;
  116. }
  117. }
  118. if (in && out)
  119. break;
  120. }
  121. if (!alt || !in || !out)
  122. return -EINVAL;
  123. if (alt->desc.bAlternateSetting != 0
  124. || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
  125. tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
  126. alt->desc.bAlternateSetting);
  127. if (tmp < 0)
  128. return tmp;
  129. }
  130. dev->in = usb_rcvbulkpipe (dev->udev,
  131. in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  132. dev->out = usb_sndbulkpipe (dev->udev,
  133. out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  134. dev->status = status;
  135. return 0;
  136. }
  137. EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
  138. static void intr_complete (struct urb *urb, struct pt_regs *regs);
  139. static int init_status (struct usbnet *dev, struct usb_interface *intf)
  140. {
  141. char *buf = NULL;
  142. unsigned pipe = 0;
  143. unsigned maxp;
  144. unsigned period;
  145. if (!dev->driver_info->status)
  146. return 0;
  147. pipe = usb_rcvintpipe (dev->udev,
  148. dev->status->desc.bEndpointAddress
  149. & USB_ENDPOINT_NUMBER_MASK);
  150. maxp = usb_maxpacket (dev->udev, pipe, 0);
  151. /* avoid 1 msec chatter: min 8 msec poll rate */
  152. period = max ((int) dev->status->desc.bInterval,
  153. (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
  154. buf = kmalloc (maxp, SLAB_KERNEL);
  155. if (buf) {
  156. dev->interrupt = usb_alloc_urb (0, SLAB_KERNEL);
  157. if (!dev->interrupt) {
  158. kfree (buf);
  159. return -ENOMEM;
  160. } else {
  161. usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
  162. buf, maxp, intr_complete, dev, period);
  163. dev_dbg(&intf->dev,
  164. "status ep%din, %d bytes period %d\n",
  165. usb_pipeendpoint(pipe), maxp, period);
  166. }
  167. }
  168. return 0;
  169. }
  170. /* Passes this packet up the stack, updating its accounting.
  171. * Some link protocols batch packets, so their rx_fixup paths
  172. * can return clones as well as just modify the original skb.
  173. */
  174. void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
  175. {
  176. int status;
  177. skb->dev = dev->net;
  178. skb->protocol = eth_type_trans (skb, dev->net);
  179. dev->stats.rx_packets++;
  180. dev->stats.rx_bytes += skb->len;
  181. if (netif_msg_rx_status (dev))
  182. devdbg (dev, "< rx, len %zu, type 0x%x",
  183. skb->len + sizeof (struct ethhdr), skb->protocol);
  184. memset (skb->cb, 0, sizeof (struct skb_data));
  185. status = netif_rx (skb);
  186. if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
  187. devdbg (dev, "netif_rx status %d", status);
  188. }
  189. EXPORT_SYMBOL_GPL(usbnet_skb_return);
  190. /*-------------------------------------------------------------------------
  191. *
  192. * Network Device Driver (peer link to "Host Device", from USB host)
  193. *
  194. *-------------------------------------------------------------------------*/
  195. static int usbnet_change_mtu (struct net_device *net, int new_mtu)
  196. {
  197. struct usbnet *dev = netdev_priv(net);
  198. int ll_mtu = new_mtu + net->hard_header_len;
  199. if (new_mtu <= 0 || ll_mtu > dev->hard_mtu)
  200. return -EINVAL;
  201. // no second zero-length packet read wanted after mtu-sized packets
  202. if ((ll_mtu % dev->maxpacket) == 0)
  203. return -EDOM;
  204. net->mtu = new_mtu;
  205. return 0;
  206. }
  207. /*-------------------------------------------------------------------------*/
  208. static struct net_device_stats *usbnet_get_stats (struct net_device *net)
  209. {
  210. struct usbnet *dev = netdev_priv(net);
  211. return &dev->stats;
  212. }
  213. /*-------------------------------------------------------------------------*/
  214. /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
  215. * completion callbacks. 2.5 should have fixed those bugs...
  216. */
  217. static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
  218. {
  219. unsigned long flags;
  220. spin_lock_irqsave(&list->lock, flags);
  221. __skb_unlink(skb, list);
  222. spin_unlock(&list->lock);
  223. spin_lock(&dev->done.lock);
  224. __skb_queue_tail(&dev->done, skb);
  225. if (dev->done.qlen == 1)
  226. tasklet_schedule(&dev->bh);
  227. spin_unlock_irqrestore(&dev->done.lock, flags);
  228. }
  229. /* some work can't be done in tasklets, so we use keventd
  230. *
  231. * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
  232. * but tasklet_schedule() doesn't. hope the failure is rare.
  233. */
  234. void usbnet_defer_kevent (struct usbnet *dev, int work)
  235. {
  236. set_bit (work, &dev->flags);
  237. if (!schedule_work (&dev->kevent))
  238. deverr (dev, "kevent %d may have been dropped", work);
  239. else
  240. devdbg (dev, "kevent %d scheduled", work);
  241. }
  242. EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
  243. /*-------------------------------------------------------------------------*/
  244. static void rx_complete (struct urb *urb, struct pt_regs *regs);
  245. static void rx_submit (struct usbnet *dev, struct urb *urb, unsigned flags)
  246. {
  247. struct sk_buff *skb;
  248. struct skb_data *entry;
  249. int retval = 0;
  250. unsigned long lockflags;
  251. size_t size = dev->rx_urb_size;
  252. if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
  253. if (netif_msg_rx_err (dev))
  254. devdbg (dev, "no rx skb");
  255. usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
  256. usb_free_urb (urb);
  257. return;
  258. }
  259. skb_reserve (skb, NET_IP_ALIGN);
  260. entry = (struct skb_data *) skb->cb;
  261. entry->urb = urb;
  262. entry->dev = dev;
  263. entry->state = rx_start;
  264. entry->length = 0;
  265. usb_fill_bulk_urb (urb, dev->udev, dev->in,
  266. skb->data, size, rx_complete, skb);
  267. spin_lock_irqsave (&dev->rxq.lock, lockflags);
  268. if (netif_running (dev->net)
  269. && netif_device_present (dev->net)
  270. && !test_bit (EVENT_RX_HALT, &dev->flags)) {
  271. switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){
  272. case -EPIPE:
  273. usbnet_defer_kevent (dev, EVENT_RX_HALT);
  274. break;
  275. case -ENOMEM:
  276. usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
  277. break;
  278. case -ENODEV:
  279. if (netif_msg_ifdown (dev))
  280. devdbg (dev, "device gone");
  281. netif_device_detach (dev->net);
  282. break;
  283. default:
  284. if (netif_msg_rx_err (dev))
  285. devdbg (dev, "rx submit, %d", retval);
  286. tasklet_schedule (&dev->bh);
  287. break;
  288. case 0:
  289. __skb_queue_tail (&dev->rxq, skb);
  290. }
  291. } else {
  292. if (netif_msg_ifdown (dev))
  293. devdbg (dev, "rx: stopped");
  294. retval = -ENOLINK;
  295. }
  296. spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
  297. if (retval) {
  298. dev_kfree_skb_any (skb);
  299. usb_free_urb (urb);
  300. }
  301. }
  302. /*-------------------------------------------------------------------------*/
  303. static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
  304. {
  305. if (dev->driver_info->rx_fixup
  306. && !dev->driver_info->rx_fixup (dev, skb))
  307. goto error;
  308. // else network stack removes extra byte if we forced a short packet
  309. if (skb->len)
  310. usbnet_skb_return (dev, skb);
  311. else {
  312. if (netif_msg_rx_err (dev))
  313. devdbg (dev, "drop");
  314. error:
  315. dev->stats.rx_errors++;
  316. skb_queue_tail (&dev->done, skb);
  317. }
  318. }
  319. /*-------------------------------------------------------------------------*/
  320. static void rx_complete (struct urb *urb, struct pt_regs *regs)
  321. {
  322. struct sk_buff *skb = (struct sk_buff *) urb->context;
  323. struct skb_data *entry = (struct skb_data *) skb->cb;
  324. struct usbnet *dev = entry->dev;
  325. int urb_status = urb->status;
  326. skb_put (skb, urb->actual_length);
  327. entry->state = rx_done;
  328. entry->urb = NULL;
  329. switch (urb_status) {
  330. // success
  331. case 0:
  332. if (skb->len < dev->net->hard_header_len) {
  333. entry->state = rx_cleanup;
  334. dev->stats.rx_errors++;
  335. dev->stats.rx_length_errors++;
  336. if (netif_msg_rx_err (dev))
  337. devdbg (dev, "rx length %d", skb->len);
  338. }
  339. break;
  340. // stalls need manual reset. this is rare ... except that
  341. // when going through USB 2.0 TTs, unplug appears this way.
  342. // we avoid the highspeed version of the ETIMEOUT/EILSEQ
  343. // storm, recovering as needed.
  344. case -EPIPE:
  345. dev->stats.rx_errors++;
  346. usbnet_defer_kevent (dev, EVENT_RX_HALT);
  347. // FALLTHROUGH
  348. // software-driven interface shutdown
  349. case -ECONNRESET: // async unlink
  350. case -ESHUTDOWN: // hardware gone
  351. if (netif_msg_ifdown (dev))
  352. devdbg (dev, "rx shutdown, code %d", urb_status);
  353. goto block;
  354. // we get controller i/o faults during khubd disconnect() delays.
  355. // throttle down resubmits, to avoid log floods; just temporarily,
  356. // so we still recover when the fault isn't a khubd delay.
  357. case -EPROTO: // ehci
  358. case -ETIMEDOUT: // ohci
  359. case -EILSEQ: // uhci
  360. dev->stats.rx_errors++;
  361. if (!timer_pending (&dev->delay)) {
  362. mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
  363. if (netif_msg_link (dev))
  364. devdbg (dev, "rx throttle %d", urb_status);
  365. }
  366. block:
  367. entry->state = rx_cleanup;
  368. entry->urb = urb;
  369. urb = NULL;
  370. break;
  371. // data overrun ... flush fifo?
  372. case -EOVERFLOW:
  373. dev->stats.rx_over_errors++;
  374. // FALLTHROUGH
  375. default:
  376. entry->state = rx_cleanup;
  377. dev->stats.rx_errors++;
  378. if (netif_msg_rx_err (dev))
  379. devdbg (dev, "rx status %d", urb_status);
  380. break;
  381. }
  382. defer_bh(dev, skb, &dev->rxq);
  383. if (urb) {
  384. if (netif_running (dev->net)
  385. && !test_bit (EVENT_RX_HALT, &dev->flags)) {
  386. rx_submit (dev, urb, GFP_ATOMIC);
  387. return;
  388. }
  389. usb_free_urb (urb);
  390. }
  391. if (netif_msg_rx_err (dev))
  392. devdbg (dev, "no read resubmitted");
  393. }
  394. static void intr_complete (struct urb *urb, struct pt_regs *regs)
  395. {
  396. struct usbnet *dev = urb->context;
  397. int status = urb->status;
  398. switch (status) {
  399. /* success */
  400. case 0:
  401. dev->driver_info->status(dev, urb);
  402. break;
  403. /* software-driven interface shutdown */
  404. case -ENOENT: // urb killed
  405. case -ESHUTDOWN: // hardware gone
  406. if (netif_msg_ifdown (dev))
  407. devdbg (dev, "intr shutdown, code %d", status);
  408. return;
  409. /* NOTE: not throttling like RX/TX, since this endpoint
  410. * already polls infrequently
  411. */
  412. default:
  413. devdbg (dev, "intr status %d", status);
  414. break;
  415. }
  416. if (!netif_running (dev->net))
  417. return;
  418. memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
  419. status = usb_submit_urb (urb, GFP_ATOMIC);
  420. if (status != 0 && netif_msg_timer (dev))
  421. deverr(dev, "intr resubmit --> %d", status);
  422. }
  423. /*-------------------------------------------------------------------------*/
  424. // unlink pending rx/tx; completion handlers do all other cleanup
  425. static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
  426. {
  427. unsigned long flags;
  428. struct sk_buff *skb, *skbnext;
  429. int count = 0;
  430. spin_lock_irqsave (&q->lock, flags);
  431. for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
  432. struct skb_data *entry;
  433. struct urb *urb;
  434. int retval;
  435. entry = (struct skb_data *) skb->cb;
  436. urb = entry->urb;
  437. skbnext = skb->next;
  438. // during some PM-driven resume scenarios,
  439. // these (async) unlinks complete immediately
  440. retval = usb_unlink_urb (urb);
  441. if (retval != -EINPROGRESS && retval != 0)
  442. devdbg (dev, "unlink urb err, %d", retval);
  443. else
  444. count++;
  445. }
  446. spin_unlock_irqrestore (&q->lock, flags);
  447. return count;
  448. }
  449. /*-------------------------------------------------------------------------*/
  450. // precondition: never called in_interrupt
  451. static int usbnet_stop (struct net_device *net)
  452. {
  453. struct usbnet *dev = netdev_priv(net);
  454. int temp;
  455. DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup);
  456. DECLARE_WAITQUEUE (wait, current);
  457. netif_stop_queue (net);
  458. if (netif_msg_ifdown (dev))
  459. devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
  460. dev->stats.rx_packets, dev->stats.tx_packets,
  461. dev->stats.rx_errors, dev->stats.tx_errors
  462. );
  463. // ensure there are no more active urbs
  464. add_wait_queue (&unlink_wakeup, &wait);
  465. dev->wait = &unlink_wakeup;
  466. temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
  467. // maybe wait for deletions to finish.
  468. while (!skb_queue_empty(&dev->rxq) &&
  469. !skb_queue_empty(&dev->txq) &&
  470. !skb_queue_empty(&dev->done)) {
  471. msleep(UNLINK_TIMEOUT_MS);
  472. if (netif_msg_ifdown (dev))
  473. devdbg (dev, "waited for %d urb completions", temp);
  474. }
  475. dev->wait = NULL;
  476. remove_wait_queue (&unlink_wakeup, &wait);
  477. usb_kill_urb(dev->interrupt);
  478. /* deferred work (task, timer, softirq) must also stop.
  479. * can't flush_scheduled_work() until we drop rtnl (later),
  480. * else workers could deadlock; so make workers a NOP.
  481. */
  482. dev->flags = 0;
  483. del_timer_sync (&dev->delay);
  484. tasklet_kill (&dev->bh);
  485. return 0;
  486. }
  487. /*-------------------------------------------------------------------------*/
  488. // posts reads, and enables write queuing
  489. // precondition: never called in_interrupt
  490. static int usbnet_open (struct net_device *net)
  491. {
  492. struct usbnet *dev = netdev_priv(net);
  493. int retval = 0;
  494. struct driver_info *info = dev->driver_info;
  495. // put into "known safe" state
  496. if (info->reset && (retval = info->reset (dev)) < 0) {
  497. if (netif_msg_ifup (dev))
  498. devinfo (dev,
  499. "open reset fail (%d) usbnet usb-%s-%s, %s",
  500. retval,
  501. dev->udev->bus->bus_name, dev->udev->devpath,
  502. info->description);
  503. goto done;
  504. }
  505. // insist peer be connected
  506. if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
  507. if (netif_msg_ifup (dev))
  508. devdbg (dev, "can't open; %d", retval);
  509. goto done;
  510. }
  511. /* start any status interrupt transfer */
  512. if (dev->interrupt) {
  513. retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
  514. if (retval < 0) {
  515. if (netif_msg_ifup (dev))
  516. deverr (dev, "intr submit %d", retval);
  517. goto done;
  518. }
  519. }
  520. netif_start_queue (net);
  521. if (netif_msg_ifup (dev)) {
  522. char *framing;
  523. if (dev->driver_info->flags & FLAG_FRAMING_NC)
  524. framing = "NetChip";
  525. else if (dev->driver_info->flags & FLAG_FRAMING_GL)
  526. framing = "GeneSys";
  527. else if (dev->driver_info->flags & FLAG_FRAMING_Z)
  528. framing = "Zaurus";
  529. else if (dev->driver_info->flags & FLAG_FRAMING_RN)
  530. framing = "RNDIS";
  531. else if (dev->driver_info->flags & FLAG_FRAMING_AX)
  532. framing = "ASIX";
  533. else
  534. framing = "simple";
  535. devinfo (dev, "open: enable queueing "
  536. "(rx %d, tx %d) mtu %d %s framing",
  537. RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu,
  538. framing);
  539. }
  540. // delay posting reads until we're fully open
  541. tasklet_schedule (&dev->bh);
  542. done:
  543. return retval;
  544. }
  545. /*-------------------------------------------------------------------------*/
  546. /* ethtool methods; minidrivers may need to add some more, but
  547. * they'll probably want to use this base set.
  548. */
  549. void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
  550. {
  551. struct usbnet *dev = netdev_priv(net);
  552. /* REVISIT don't always return "usbnet" */
  553. strncpy (info->driver, driver_name, sizeof info->driver);
  554. strncpy (info->version, DRIVER_VERSION, sizeof info->version);
  555. strncpy (info->fw_version, dev->driver_info->description,
  556. sizeof info->fw_version);
  557. usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
  558. }
  559. EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
  560. static u32 usbnet_get_link (struct net_device *net)
  561. {
  562. struct usbnet *dev = netdev_priv(net);
  563. /* If a check_connect is defined, return its result */
  564. if (dev->driver_info->check_connect)
  565. return dev->driver_info->check_connect (dev) == 0;
  566. /* Otherwise, say we're up (to avoid breaking scripts) */
  567. return 1;
  568. }
  569. u32 usbnet_get_msglevel (struct net_device *net)
  570. {
  571. struct usbnet *dev = netdev_priv(net);
  572. return dev->msg_enable;
  573. }
  574. EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
  575. void usbnet_set_msglevel (struct net_device *net, u32 level)
  576. {
  577. struct usbnet *dev = netdev_priv(net);
  578. dev->msg_enable = level;
  579. }
  580. EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
  581. /* drivers may override default ethtool_ops in their bind() routine */
  582. static struct ethtool_ops usbnet_ethtool_ops = {
  583. .get_drvinfo = usbnet_get_drvinfo,
  584. .get_link = usbnet_get_link,
  585. .get_msglevel = usbnet_get_msglevel,
  586. .set_msglevel = usbnet_set_msglevel,
  587. };
  588. /*-------------------------------------------------------------------------*/
  589. /* work that cannot be done in interrupt context uses keventd.
  590. *
  591. * NOTE: with 2.5 we could do more of this using completion callbacks,
  592. * especially now that control transfers can be queued.
  593. */
  594. static void
  595. kevent (void *data)
  596. {
  597. struct usbnet *dev = data;
  598. int status;
  599. /* usb_clear_halt() needs a thread context */
  600. if (test_bit (EVENT_TX_HALT, &dev->flags)) {
  601. unlink_urbs (dev, &dev->txq);
  602. status = usb_clear_halt (dev->udev, dev->out);
  603. if (status < 0
  604. && status != -EPIPE
  605. && status != -ESHUTDOWN) {
  606. if (netif_msg_tx_err (dev))
  607. deverr (dev, "can't clear tx halt, status %d",
  608. status);
  609. } else {
  610. clear_bit (EVENT_TX_HALT, &dev->flags);
  611. if (status != -ESHUTDOWN)
  612. netif_wake_queue (dev->net);
  613. }
  614. }
  615. if (test_bit (EVENT_RX_HALT, &dev->flags)) {
  616. unlink_urbs (dev, &dev->rxq);
  617. status = usb_clear_halt (dev->udev, dev->in);
  618. if (status < 0
  619. && status != -EPIPE
  620. && status != -ESHUTDOWN) {
  621. if (netif_msg_rx_err (dev))
  622. deverr (dev, "can't clear rx halt, status %d",
  623. status);
  624. } else {
  625. clear_bit (EVENT_RX_HALT, &dev->flags);
  626. tasklet_schedule (&dev->bh);
  627. }
  628. }
  629. /* tasklet could resubmit itself forever if memory is tight */
  630. if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
  631. struct urb *urb = NULL;
  632. if (netif_running (dev->net))
  633. urb = usb_alloc_urb (0, GFP_KERNEL);
  634. else
  635. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  636. if (urb != NULL) {
  637. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  638. rx_submit (dev, urb, GFP_KERNEL);
  639. tasklet_schedule (&dev->bh);
  640. }
  641. }
  642. if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
  643. struct driver_info *info = dev->driver_info;
  644. int retval = 0;
  645. clear_bit (EVENT_LINK_RESET, &dev->flags);
  646. if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
  647. devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
  648. retval,
  649. dev->udev->bus->bus_name, dev->udev->devpath,
  650. info->description);
  651. }
  652. }
  653. if (dev->flags)
  654. devdbg (dev, "kevent done, flags = 0x%lx",
  655. dev->flags);
  656. }
  657. /*-------------------------------------------------------------------------*/
  658. static void tx_complete (struct urb *urb, struct pt_regs *regs)
  659. {
  660. struct sk_buff *skb = (struct sk_buff *) urb->context;
  661. struct skb_data *entry = (struct skb_data *) skb->cb;
  662. struct usbnet *dev = entry->dev;
  663. if (urb->status == 0) {
  664. dev->stats.tx_packets++;
  665. dev->stats.tx_bytes += entry->length;
  666. } else {
  667. dev->stats.tx_errors++;
  668. switch (urb->status) {
  669. case -EPIPE:
  670. usbnet_defer_kevent (dev, EVENT_TX_HALT);
  671. break;
  672. /* software-driven interface shutdown */
  673. case -ECONNRESET: // async unlink
  674. case -ESHUTDOWN: // hardware gone
  675. break;
  676. // like rx, tx gets controller i/o faults during khubd delays
  677. // and so it uses the same throttling mechanism.
  678. case -EPROTO: // ehci
  679. case -ETIMEDOUT: // ohci
  680. case -EILSEQ: // uhci
  681. if (!timer_pending (&dev->delay)) {
  682. mod_timer (&dev->delay,
  683. jiffies + THROTTLE_JIFFIES);
  684. if (netif_msg_link (dev))
  685. devdbg (dev, "tx throttle %d",
  686. urb->status);
  687. }
  688. netif_stop_queue (dev->net);
  689. break;
  690. default:
  691. if (netif_msg_tx_err (dev))
  692. devdbg (dev, "tx err %d", entry->urb->status);
  693. break;
  694. }
  695. }
  696. urb->dev = NULL;
  697. entry->state = tx_done;
  698. defer_bh(dev, skb, &dev->txq);
  699. }
  700. /*-------------------------------------------------------------------------*/
  701. static void usbnet_tx_timeout (struct net_device *net)
  702. {
  703. struct usbnet *dev = netdev_priv(net);
  704. unlink_urbs (dev, &dev->txq);
  705. tasklet_schedule (&dev->bh);
  706. // FIXME: device recovery -- reset?
  707. }
  708. /*-------------------------------------------------------------------------*/
  709. static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
  710. {
  711. struct usbnet *dev = netdev_priv(net);
  712. int length;
  713. int retval = NET_XMIT_SUCCESS;
  714. struct urb *urb = NULL;
  715. struct skb_data *entry;
  716. struct driver_info *info = dev->driver_info;
  717. unsigned long flags;
  718. // some devices want funky USB-level framing, for
  719. // win32 driver (usually) and/or hardware quirks
  720. if (info->tx_fixup) {
  721. skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
  722. if (!skb) {
  723. if (netif_msg_tx_err (dev))
  724. devdbg (dev, "can't tx_fixup skb");
  725. goto drop;
  726. }
  727. }
  728. length = skb->len;
  729. if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
  730. if (netif_msg_tx_err (dev))
  731. devdbg (dev, "no urb");
  732. goto drop;
  733. }
  734. entry = (struct skb_data *) skb->cb;
  735. entry->urb = urb;
  736. entry->dev = dev;
  737. entry->state = tx_start;
  738. entry->length = length;
  739. usb_fill_bulk_urb (urb, dev->udev, dev->out,
  740. skb->data, skb->len, tx_complete, skb);
  741. /* don't assume the hardware handles USB_ZERO_PACKET
  742. * NOTE: strictly conforming cdc-ether devices should expect
  743. * the ZLP here, but ignore the one-byte packet.
  744. *
  745. * FIXME zero that byte, if it doesn't require a new skb.
  746. */
  747. if ((length % dev->maxpacket) == 0)
  748. urb->transfer_buffer_length++;
  749. spin_lock_irqsave (&dev->txq.lock, flags);
  750. switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
  751. case -EPIPE:
  752. netif_stop_queue (net);
  753. usbnet_defer_kevent (dev, EVENT_TX_HALT);
  754. break;
  755. default:
  756. if (netif_msg_tx_err (dev))
  757. devdbg (dev, "tx: submit urb err %d", retval);
  758. break;
  759. case 0:
  760. net->trans_start = jiffies;
  761. __skb_queue_tail (&dev->txq, skb);
  762. if (dev->txq.qlen >= TX_QLEN (dev))
  763. netif_stop_queue (net);
  764. }
  765. spin_unlock_irqrestore (&dev->txq.lock, flags);
  766. if (retval) {
  767. if (netif_msg_tx_err (dev))
  768. devdbg (dev, "drop, code %d", retval);
  769. drop:
  770. retval = NET_XMIT_SUCCESS;
  771. dev->stats.tx_dropped++;
  772. if (skb)
  773. dev_kfree_skb_any (skb);
  774. usb_free_urb (urb);
  775. } else if (netif_msg_tx_queued (dev)) {
  776. devdbg (dev, "> tx, len %d, type 0x%x",
  777. length, skb->protocol);
  778. }
  779. return retval;
  780. }
  781. /*-------------------------------------------------------------------------*/
  782. // tasklet (work deferred from completions, in_irq) or timer
  783. static void usbnet_bh (unsigned long param)
  784. {
  785. struct usbnet *dev = (struct usbnet *) param;
  786. struct sk_buff *skb;
  787. struct skb_data *entry;
  788. while ((skb = skb_dequeue (&dev->done))) {
  789. entry = (struct skb_data *) skb->cb;
  790. switch (entry->state) {
  791. case rx_done:
  792. entry->state = rx_cleanup;
  793. rx_process (dev, skb);
  794. continue;
  795. case tx_done:
  796. case rx_cleanup:
  797. usb_free_urb (entry->urb);
  798. dev_kfree_skb (skb);
  799. continue;
  800. default:
  801. devdbg (dev, "bogus skb state %d", entry->state);
  802. }
  803. }
  804. // waiting for all pending urbs to complete?
  805. if (dev->wait) {
  806. if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
  807. wake_up (dev->wait);
  808. }
  809. // or are we maybe short a few urbs?
  810. } else if (netif_running (dev->net)
  811. && netif_device_present (dev->net)
  812. && !timer_pending (&dev->delay)
  813. && !test_bit (EVENT_RX_HALT, &dev->flags)) {
  814. int temp = dev->rxq.qlen;
  815. int qlen = RX_QLEN (dev);
  816. if (temp < qlen) {
  817. struct urb *urb;
  818. int i;
  819. // don't refill the queue all at once
  820. for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
  821. urb = usb_alloc_urb (0, GFP_ATOMIC);
  822. if (urb != NULL)
  823. rx_submit (dev, urb, GFP_ATOMIC);
  824. }
  825. if (temp != dev->rxq.qlen && netif_msg_link (dev))
  826. devdbg (dev, "rxqlen %d --> %d",
  827. temp, dev->rxq.qlen);
  828. if (dev->rxq.qlen < qlen)
  829. tasklet_schedule (&dev->bh);
  830. }
  831. if (dev->txq.qlen < TX_QLEN (dev))
  832. netif_wake_queue (dev->net);
  833. }
  834. }
  835. /*-------------------------------------------------------------------------
  836. *
  837. * USB Device Driver support
  838. *
  839. *-------------------------------------------------------------------------*/
  840. // precondition: never called in_interrupt
  841. void usbnet_disconnect (struct usb_interface *intf)
  842. {
  843. struct usbnet *dev;
  844. struct usb_device *xdev;
  845. struct net_device *net;
  846. dev = usb_get_intfdata(intf);
  847. usb_set_intfdata(intf, NULL);
  848. if (!dev)
  849. return;
  850. xdev = interface_to_usbdev (intf);
  851. if (netif_msg_probe (dev))
  852. devinfo (dev, "unregister '%s' usb-%s-%s, %s",
  853. intf->dev.driver->name,
  854. xdev->bus->bus_name, xdev->devpath,
  855. dev->driver_info->description);
  856. net = dev->net;
  857. unregister_netdev (net);
  858. /* we don't hold rtnl here ... */
  859. flush_scheduled_work ();
  860. if (dev->driver_info->unbind)
  861. dev->driver_info->unbind (dev, intf);
  862. free_netdev(net);
  863. usb_put_dev (xdev);
  864. }
  865. EXPORT_SYMBOL_GPL(usbnet_disconnect);
  866. /*-------------------------------------------------------------------------*/
  867. // precondition: never called in_interrupt
  868. int
  869. usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
  870. {
  871. struct usbnet *dev;
  872. struct net_device *net;
  873. struct usb_host_interface *interface;
  874. struct driver_info *info;
  875. struct usb_device *xdev;
  876. int status;
  877. info = (struct driver_info *) prod->driver_info;
  878. if (!info) {
  879. dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
  880. return -ENODEV;
  881. }
  882. xdev = interface_to_usbdev (udev);
  883. interface = udev->cur_altsetting;
  884. usb_get_dev (xdev);
  885. status = -ENOMEM;
  886. // set up our own records
  887. net = alloc_etherdev(sizeof(*dev));
  888. if (!net) {
  889. dbg ("can't kmalloc dev");
  890. goto out;
  891. }
  892. dev = netdev_priv(net);
  893. dev->udev = xdev;
  894. dev->driver_info = info;
  895. dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
  896. | NETIF_MSG_PROBE | NETIF_MSG_LINK);
  897. skb_queue_head_init (&dev->rxq);
  898. skb_queue_head_init (&dev->txq);
  899. skb_queue_head_init (&dev->done);
  900. dev->bh.func = usbnet_bh;
  901. dev->bh.data = (unsigned long) dev;
  902. INIT_WORK (&dev->kevent, kevent, dev);
  903. dev->delay.function = usbnet_bh;
  904. dev->delay.data = (unsigned long) dev;
  905. init_timer (&dev->delay);
  906. SET_MODULE_OWNER (net);
  907. dev->net = net;
  908. strcpy (net->name, "usb%d");
  909. memcpy (net->dev_addr, node_id, sizeof node_id);
  910. /* rx and tx sides can use different message sizes;
  911. * bind() should set rx_urb_size in that case.
  912. */
  913. dev->hard_mtu = net->mtu + net->hard_header_len;
  914. #if 0
  915. // dma_supported() is deeply broken on almost all architectures
  916. // possible with some EHCI controllers
  917. if (dma_supported (&udev->dev, DMA_64BIT_MASK))
  918. net->features |= NETIF_F_HIGHDMA;
  919. #endif
  920. net->change_mtu = usbnet_change_mtu;
  921. net->get_stats = usbnet_get_stats;
  922. net->hard_start_xmit = usbnet_start_xmit;
  923. net->open = usbnet_open;
  924. net->stop = usbnet_stop;
  925. net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
  926. net->tx_timeout = usbnet_tx_timeout;
  927. net->ethtool_ops = &usbnet_ethtool_ops;
  928. // allow device-specific bind/init procedures
  929. // NOTE net->name still not usable ...
  930. if (info->bind) {
  931. status = info->bind (dev, udev);
  932. // heuristic: "usb%d" for links we know are two-host,
  933. // else "eth%d" when there's reasonable doubt. userspace
  934. // can rename the link if it knows better.
  935. if ((dev->driver_info->flags & FLAG_ETHER) != 0
  936. && (net->dev_addr [0] & 0x02) == 0)
  937. strcpy (net->name, "eth%d");
  938. /* maybe the remote can't receive an Ethernet MTU */
  939. if (net->mtu > (dev->hard_mtu - net->hard_header_len))
  940. net->mtu = dev->hard_mtu - net->hard_header_len;
  941. } else if (!info->in || !info->out)
  942. status = usbnet_get_endpoints (dev, udev);
  943. else {
  944. dev->in = usb_rcvbulkpipe (xdev, info->in);
  945. dev->out = usb_sndbulkpipe (xdev, info->out);
  946. if (!(info->flags & FLAG_NO_SETINT))
  947. status = usb_set_interface (xdev,
  948. interface->desc.bInterfaceNumber,
  949. interface->desc.bAlternateSetting);
  950. else
  951. status = 0;
  952. }
  953. if (status == 0 && dev->status)
  954. status = init_status (dev, udev);
  955. if (status < 0)
  956. goto out1;
  957. if (!dev->rx_urb_size)
  958. dev->rx_urb_size = dev->hard_mtu;
  959. dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
  960. SET_NETDEV_DEV(net, &udev->dev);
  961. status = register_netdev (net);
  962. if (status)
  963. goto out3;
  964. if (netif_msg_probe (dev))
  965. devinfo (dev, "register '%s' at usb-%s-%s, %s, "
  966. "%02x:%02x:%02x:%02x:%02x:%02x",
  967. udev->dev.driver->name,
  968. xdev->bus->bus_name, xdev->devpath,
  969. dev->driver_info->description,
  970. net->dev_addr [0], net->dev_addr [1],
  971. net->dev_addr [2], net->dev_addr [3],
  972. net->dev_addr [4], net->dev_addr [5]);
  973. // ok, it's ready to go.
  974. usb_set_intfdata (udev, dev);
  975. // start as if the link is up
  976. netif_device_attach (net);
  977. return 0;
  978. out3:
  979. if (info->unbind)
  980. info->unbind (dev, udev);
  981. out1:
  982. free_netdev(net);
  983. out:
  984. usb_put_dev(xdev);
  985. return status;
  986. }
  987. EXPORT_SYMBOL_GPL(usbnet_probe);
  988. /*-------------------------------------------------------------------------*/
  989. /* FIXME these suspend/resume methods assume non-CDC style
  990. * devices, with only one interface.
  991. */
  992. int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
  993. {
  994. struct usbnet *dev = usb_get_intfdata(intf);
  995. /* accelerate emptying of the rx and queues, to avoid
  996. * having everything error out.
  997. */
  998. netif_device_detach (dev->net);
  999. (void) unlink_urbs (dev, &dev->rxq);
  1000. (void) unlink_urbs (dev, &dev->txq);
  1001. intf->dev.power.power_state = PMSG_SUSPEND;
  1002. return 0;
  1003. }
  1004. EXPORT_SYMBOL_GPL(usbnet_suspend);
  1005. int usbnet_resume (struct usb_interface *intf)
  1006. {
  1007. struct usbnet *dev = usb_get_intfdata(intf);
  1008. intf->dev.power.power_state = PMSG_ON;
  1009. netif_device_attach (dev->net);
  1010. tasklet_schedule (&dev->bh);
  1011. return 0;
  1012. }
  1013. EXPORT_SYMBOL_GPL(usbnet_resume);
  1014. /*-------------------------------------------------------------------------*/
  1015. static int __init usbnet_init(void)
  1016. {
  1017. /* compiler should optimize this out */
  1018. BUG_ON (sizeof (((struct sk_buff *)0)->cb)
  1019. < sizeof (struct skb_data));
  1020. random_ether_addr(node_id);
  1021. return 0;
  1022. }
  1023. module_init(usbnet_init);
  1024. static void __exit usbnet_exit(void)
  1025. {
  1026. }
  1027. module_exit(usbnet_exit);
  1028. MODULE_AUTHOR("David Brownell");
  1029. MODULE_DESCRIPTION("USB network driver framework");
  1030. MODULE_LICENSE("GPL");