usbnet.c 32 KB

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