usbnet.c 34 KB

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