usbnet.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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 "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. for (skb = q->next; skb != (struct sk_buff *) 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. skbnext = skb->next;
  445. // during some PM-driven resume scenarios,
  446. // these (async) unlinks complete immediately
  447. retval = usb_unlink_urb (urb);
  448. if (retval != -EINPROGRESS && retval != 0)
  449. devdbg (dev, "unlink urb err, %d", retval);
  450. else
  451. count++;
  452. }
  453. spin_unlock_irqrestore (&q->lock, flags);
  454. return count;
  455. }
  456. // Flush all pending rx urbs
  457. // minidrivers may need to do this when the MTU changes
  458. void usbnet_unlink_rx_urbs(struct usbnet *dev)
  459. {
  460. if (netif_running(dev->net)) {
  461. (void) unlink_urbs (dev, &dev->rxq);
  462. tasklet_schedule(&dev->bh);
  463. }
  464. }
  465. EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
  466. /*-------------------------------------------------------------------------*/
  467. // precondition: never called in_interrupt
  468. static int usbnet_stop (struct net_device *net)
  469. {
  470. struct usbnet *dev = netdev_priv(net);
  471. int temp;
  472. DECLARE_WAIT_QUEUE_HEAD_ONSTACK (unlink_wakeup);
  473. DECLARE_WAITQUEUE (wait, current);
  474. netif_stop_queue (net);
  475. if (netif_msg_ifdown (dev))
  476. devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
  477. dev->stats.rx_packets, dev->stats.tx_packets,
  478. dev->stats.rx_errors, dev->stats.tx_errors
  479. );
  480. // ensure there are no more active urbs
  481. add_wait_queue (&unlink_wakeup, &wait);
  482. dev->wait = &unlink_wakeup;
  483. temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
  484. // maybe wait for deletions to finish.
  485. while (!skb_queue_empty(&dev->rxq)
  486. && !skb_queue_empty(&dev->txq)
  487. && !skb_queue_empty(&dev->done)) {
  488. msleep(UNLINK_TIMEOUT_MS);
  489. if (netif_msg_ifdown (dev))
  490. devdbg (dev, "waited for %d urb completions", temp);
  491. }
  492. dev->wait = NULL;
  493. remove_wait_queue (&unlink_wakeup, &wait);
  494. usb_kill_urb(dev->interrupt);
  495. /* deferred work (task, timer, softirq) must also stop.
  496. * can't flush_scheduled_work() until we drop rtnl (later),
  497. * else workers could deadlock; so make workers a NOP.
  498. */
  499. dev->flags = 0;
  500. del_timer_sync (&dev->delay);
  501. tasklet_kill (&dev->bh);
  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 = 0;
  511. struct driver_info *info = dev->driver_info;
  512. // put into "known safe" state
  513. if (info->reset && (retval = info->reset (dev)) < 0) {
  514. if (netif_msg_ifup (dev))
  515. devinfo (dev,
  516. "open reset fail (%d) usbnet usb-%s-%s, %s",
  517. retval,
  518. dev->udev->bus->bus_name, dev->udev->devpath,
  519. info->description);
  520. goto done;
  521. }
  522. // insist peer be connected
  523. if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
  524. if (netif_msg_ifup (dev))
  525. devdbg (dev, "can't open; %d", retval);
  526. goto done;
  527. }
  528. /* start any status interrupt transfer */
  529. if (dev->interrupt) {
  530. retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
  531. if (retval < 0) {
  532. if (netif_msg_ifup (dev))
  533. deverr (dev, "intr submit %d", retval);
  534. goto done;
  535. }
  536. }
  537. netif_start_queue (net);
  538. if (netif_msg_ifup (dev)) {
  539. char *framing;
  540. if (dev->driver_info->flags & FLAG_FRAMING_NC)
  541. framing = "NetChip";
  542. else if (dev->driver_info->flags & FLAG_FRAMING_GL)
  543. framing = "GeneSys";
  544. else if (dev->driver_info->flags & FLAG_FRAMING_Z)
  545. framing = "Zaurus";
  546. else if (dev->driver_info->flags & FLAG_FRAMING_RN)
  547. framing = "RNDIS";
  548. else if (dev->driver_info->flags & FLAG_FRAMING_AX)
  549. framing = "ASIX";
  550. else
  551. framing = "simple";
  552. devinfo (dev, "open: enable queueing "
  553. "(rx %d, tx %d) mtu %d %s framing",
  554. (int)RX_QLEN (dev), (int)TX_QLEN (dev), dev->net->mtu,
  555. framing);
  556. }
  557. // delay posting reads until we're fully open
  558. tasklet_schedule (&dev->bh);
  559. done:
  560. return retval;
  561. }
  562. /*-------------------------------------------------------------------------*/
  563. /* ethtool methods; minidrivers may need to add some more, but
  564. * they'll probably want to use this base set.
  565. */
  566. #if defined(CONFIG_MII) || defined(CONFIG_MII_MODULE)
  567. #define HAVE_MII
  568. int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
  569. {
  570. struct usbnet *dev = netdev_priv(net);
  571. if (!dev->mii.mdio_read)
  572. return -EOPNOTSUPP;
  573. return mii_ethtool_gset(&dev->mii, cmd);
  574. }
  575. EXPORT_SYMBOL_GPL(usbnet_get_settings);
  576. int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
  577. {
  578. struct usbnet *dev = netdev_priv(net);
  579. int retval;
  580. if (!dev->mii.mdio_write)
  581. return -EOPNOTSUPP;
  582. retval = mii_ethtool_sset(&dev->mii, cmd);
  583. /* link speed/duplex might have changed */
  584. if (dev->driver_info->link_reset)
  585. dev->driver_info->link_reset(dev);
  586. return retval;
  587. }
  588. EXPORT_SYMBOL_GPL(usbnet_set_settings);
  589. u32 usbnet_get_link (struct net_device *net)
  590. {
  591. struct usbnet *dev = netdev_priv(net);
  592. /* If a check_connect is defined, return its result */
  593. if (dev->driver_info->check_connect)
  594. return dev->driver_info->check_connect (dev) == 0;
  595. /* if the device has mii operations, use those */
  596. if (dev->mii.mdio_read)
  597. return mii_link_ok(&dev->mii);
  598. /* Otherwise, say we're up (to avoid breaking scripts) */
  599. return 1;
  600. }
  601. EXPORT_SYMBOL_GPL(usbnet_get_link);
  602. int usbnet_nway_reset(struct net_device *net)
  603. {
  604. struct usbnet *dev = netdev_priv(net);
  605. if (!dev->mii.mdio_write)
  606. return -EOPNOTSUPP;
  607. return mii_nway_restart(&dev->mii);
  608. }
  609. EXPORT_SYMBOL_GPL(usbnet_nway_reset);
  610. #endif /* HAVE_MII */
  611. void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
  612. {
  613. struct usbnet *dev = netdev_priv(net);
  614. strncpy (info->driver, dev->driver_name, sizeof info->driver);
  615. strncpy (info->version, DRIVER_VERSION, sizeof info->version);
  616. strncpy (info->fw_version, dev->driver_info->description,
  617. sizeof info->fw_version);
  618. usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
  619. }
  620. EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
  621. u32 usbnet_get_msglevel (struct net_device *net)
  622. {
  623. struct usbnet *dev = netdev_priv(net);
  624. return dev->msg_enable;
  625. }
  626. EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
  627. void usbnet_set_msglevel (struct net_device *net, u32 level)
  628. {
  629. struct usbnet *dev = netdev_priv(net);
  630. dev->msg_enable = level;
  631. }
  632. EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
  633. /* drivers may override default ethtool_ops in their bind() routine */
  634. static struct ethtool_ops usbnet_ethtool_ops = {
  635. #ifdef HAVE_MII
  636. .get_settings = usbnet_get_settings,
  637. .set_settings = usbnet_set_settings,
  638. .get_link = usbnet_get_link,
  639. .nway_reset = usbnet_nway_reset,
  640. #endif
  641. .get_drvinfo = usbnet_get_drvinfo,
  642. .get_msglevel = usbnet_get_msglevel,
  643. .set_msglevel = usbnet_set_msglevel,
  644. };
  645. /*-------------------------------------------------------------------------*/
  646. /* work that cannot be done in interrupt context uses keventd.
  647. *
  648. * NOTE: with 2.5 we could do more of this using completion callbacks,
  649. * especially now that control transfers can be queued.
  650. */
  651. static void
  652. kevent (struct work_struct *work)
  653. {
  654. struct usbnet *dev =
  655. container_of(work, struct usbnet, kevent);
  656. int status;
  657. /* usb_clear_halt() needs a thread context */
  658. if (test_bit (EVENT_TX_HALT, &dev->flags)) {
  659. unlink_urbs (dev, &dev->txq);
  660. status = usb_clear_halt (dev->udev, dev->out);
  661. if (status < 0
  662. && status != -EPIPE
  663. && status != -ESHUTDOWN) {
  664. if (netif_msg_tx_err (dev))
  665. deverr (dev, "can't clear tx halt, status %d",
  666. status);
  667. } else {
  668. clear_bit (EVENT_TX_HALT, &dev->flags);
  669. if (status != -ESHUTDOWN)
  670. netif_wake_queue (dev->net);
  671. }
  672. }
  673. if (test_bit (EVENT_RX_HALT, &dev->flags)) {
  674. unlink_urbs (dev, &dev->rxq);
  675. status = usb_clear_halt (dev->udev, dev->in);
  676. if (status < 0
  677. && status != -EPIPE
  678. && status != -ESHUTDOWN) {
  679. if (netif_msg_rx_err (dev))
  680. deverr (dev, "can't clear rx halt, status %d",
  681. status);
  682. } else {
  683. clear_bit (EVENT_RX_HALT, &dev->flags);
  684. tasklet_schedule (&dev->bh);
  685. }
  686. }
  687. /* tasklet could resubmit itself forever if memory is tight */
  688. if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
  689. struct urb *urb = NULL;
  690. if (netif_running (dev->net))
  691. urb = usb_alloc_urb (0, GFP_KERNEL);
  692. else
  693. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  694. if (urb != NULL) {
  695. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  696. rx_submit (dev, urb, GFP_KERNEL);
  697. tasklet_schedule (&dev->bh);
  698. }
  699. }
  700. if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
  701. struct driver_info *info = dev->driver_info;
  702. int retval = 0;
  703. clear_bit (EVENT_LINK_RESET, &dev->flags);
  704. if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
  705. devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
  706. retval,
  707. dev->udev->bus->bus_name, dev->udev->devpath,
  708. info->description);
  709. }
  710. }
  711. if (dev->flags)
  712. devdbg (dev, "kevent done, flags = 0x%lx",
  713. dev->flags);
  714. }
  715. /*-------------------------------------------------------------------------*/
  716. static void tx_complete (struct urb *urb)
  717. {
  718. struct sk_buff *skb = (struct sk_buff *) urb->context;
  719. struct skb_data *entry = (struct skb_data *) skb->cb;
  720. struct usbnet *dev = entry->dev;
  721. if (urb->status == 0) {
  722. dev->stats.tx_packets++;
  723. dev->stats.tx_bytes += entry->length;
  724. } else {
  725. dev->stats.tx_errors++;
  726. switch (urb->status) {
  727. case -EPIPE:
  728. usbnet_defer_kevent (dev, EVENT_TX_HALT);
  729. break;
  730. /* software-driven interface shutdown */
  731. case -ECONNRESET: // async unlink
  732. case -ESHUTDOWN: // hardware gone
  733. break;
  734. // like rx, tx gets controller i/o faults during khubd delays
  735. // and so it uses the same throttling mechanism.
  736. case -EPROTO:
  737. case -ETIME:
  738. case -EILSEQ:
  739. if (!timer_pending (&dev->delay)) {
  740. mod_timer (&dev->delay,
  741. jiffies + THROTTLE_JIFFIES);
  742. if (netif_msg_link (dev))
  743. devdbg (dev, "tx throttle %d",
  744. urb->status);
  745. }
  746. netif_stop_queue (dev->net);
  747. break;
  748. default:
  749. if (netif_msg_tx_err (dev))
  750. devdbg (dev, "tx err %d", entry->urb->status);
  751. break;
  752. }
  753. }
  754. urb->dev = NULL;
  755. entry->state = tx_done;
  756. defer_bh(dev, skb, &dev->txq);
  757. }
  758. /*-------------------------------------------------------------------------*/
  759. static void usbnet_tx_timeout (struct net_device *net)
  760. {
  761. struct usbnet *dev = netdev_priv(net);
  762. unlink_urbs (dev, &dev->txq);
  763. tasklet_schedule (&dev->bh);
  764. // FIXME: device recovery -- reset?
  765. }
  766. /*-------------------------------------------------------------------------*/
  767. static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
  768. {
  769. struct usbnet *dev = netdev_priv(net);
  770. int length;
  771. int retval = NET_XMIT_SUCCESS;
  772. struct urb *urb = NULL;
  773. struct skb_data *entry;
  774. struct driver_info *info = dev->driver_info;
  775. unsigned long flags;
  776. // some devices want funky USB-level framing, for
  777. // win32 driver (usually) and/or hardware quirks
  778. if (info->tx_fixup) {
  779. skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
  780. if (!skb) {
  781. if (netif_msg_tx_err (dev))
  782. devdbg (dev, "can't tx_fixup skb");
  783. goto drop;
  784. }
  785. }
  786. length = skb->len;
  787. if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
  788. if (netif_msg_tx_err (dev))
  789. devdbg (dev, "no urb");
  790. goto drop;
  791. }
  792. entry = (struct skb_data *) skb->cb;
  793. entry->urb = urb;
  794. entry->dev = dev;
  795. entry->state = tx_start;
  796. entry->length = length;
  797. usb_fill_bulk_urb (urb, dev->udev, dev->out,
  798. skb->data, skb->len, tx_complete, skb);
  799. /* don't assume the hardware handles USB_ZERO_PACKET
  800. * NOTE: strictly conforming cdc-ether devices should expect
  801. * the ZLP here, but ignore the one-byte packet.
  802. */
  803. if ((length % dev->maxpacket) == 0) {
  804. urb->transfer_buffer_length++;
  805. if (skb_tailroom(skb)) {
  806. skb->data[skb->len] = 0;
  807. __skb_put(skb, 1);
  808. }
  809. }
  810. spin_lock_irqsave (&dev->txq.lock, flags);
  811. switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
  812. case -EPIPE:
  813. netif_stop_queue (net);
  814. usbnet_defer_kevent (dev, EVENT_TX_HALT);
  815. break;
  816. default:
  817. if (netif_msg_tx_err (dev))
  818. devdbg (dev, "tx: submit urb err %d", retval);
  819. break;
  820. case 0:
  821. net->trans_start = jiffies;
  822. __skb_queue_tail (&dev->txq, skb);
  823. if (dev->txq.qlen >= TX_QLEN (dev))
  824. netif_stop_queue (net);
  825. }
  826. spin_unlock_irqrestore (&dev->txq.lock, flags);
  827. if (retval) {
  828. if (netif_msg_tx_err (dev))
  829. devdbg (dev, "drop, code %d", retval);
  830. drop:
  831. retval = NET_XMIT_SUCCESS;
  832. dev->stats.tx_dropped++;
  833. if (skb)
  834. dev_kfree_skb_any (skb);
  835. usb_free_urb (urb);
  836. } else if (netif_msg_tx_queued (dev)) {
  837. devdbg (dev, "> tx, len %d, type 0x%x",
  838. length, skb->protocol);
  839. }
  840. return retval;
  841. }
  842. /*-------------------------------------------------------------------------*/
  843. // tasklet (work deferred from completions, in_irq) or timer
  844. static void usbnet_bh (unsigned long param)
  845. {
  846. struct usbnet *dev = (struct usbnet *) param;
  847. struct sk_buff *skb;
  848. struct skb_data *entry;
  849. while ((skb = skb_dequeue (&dev->done))) {
  850. entry = (struct skb_data *) skb->cb;
  851. switch (entry->state) {
  852. case rx_done:
  853. entry->state = rx_cleanup;
  854. rx_process (dev, skb);
  855. continue;
  856. case tx_done:
  857. case rx_cleanup:
  858. usb_free_urb (entry->urb);
  859. dev_kfree_skb (skb);
  860. continue;
  861. default:
  862. devdbg (dev, "bogus skb state %d", entry->state);
  863. }
  864. }
  865. // waiting for all pending urbs to complete?
  866. if (dev->wait) {
  867. if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
  868. wake_up (dev->wait);
  869. }
  870. // or are we maybe short a few urbs?
  871. } else if (netif_running (dev->net)
  872. && netif_device_present (dev->net)
  873. && !timer_pending (&dev->delay)
  874. && !test_bit (EVENT_RX_HALT, &dev->flags)) {
  875. int temp = dev->rxq.qlen;
  876. int qlen = RX_QLEN (dev);
  877. if (temp < qlen) {
  878. struct urb *urb;
  879. int i;
  880. // don't refill the queue all at once
  881. for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
  882. urb = usb_alloc_urb (0, GFP_ATOMIC);
  883. if (urb != NULL)
  884. rx_submit (dev, urb, GFP_ATOMIC);
  885. }
  886. if (temp != dev->rxq.qlen && netif_msg_link (dev))
  887. devdbg (dev, "rxqlen %d --> %d",
  888. temp, dev->rxq.qlen);
  889. if (dev->rxq.qlen < qlen)
  890. tasklet_schedule (&dev->bh);
  891. }
  892. if (dev->txq.qlen < TX_QLEN (dev))
  893. netif_wake_queue (dev->net);
  894. }
  895. }
  896. /*-------------------------------------------------------------------------
  897. *
  898. * USB Device Driver support
  899. *
  900. *-------------------------------------------------------------------------*/
  901. // precondition: never called in_interrupt
  902. void usbnet_disconnect (struct usb_interface *intf)
  903. {
  904. struct usbnet *dev;
  905. struct usb_device *xdev;
  906. struct net_device *net;
  907. dev = usb_get_intfdata(intf);
  908. usb_set_intfdata(intf, NULL);
  909. if (!dev)
  910. return;
  911. xdev = interface_to_usbdev (intf);
  912. if (netif_msg_probe (dev))
  913. devinfo (dev, "unregister '%s' usb-%s-%s, %s",
  914. intf->dev.driver->name,
  915. xdev->bus->bus_name, xdev->devpath,
  916. dev->driver_info->description);
  917. net = dev->net;
  918. unregister_netdev (net);
  919. /* we don't hold rtnl here ... */
  920. flush_scheduled_work ();
  921. if (dev->driver_info->unbind)
  922. dev->driver_info->unbind (dev, intf);
  923. free_netdev(net);
  924. usb_put_dev (xdev);
  925. }
  926. EXPORT_SYMBOL_GPL(usbnet_disconnect);
  927. /*-------------------------------------------------------------------------*/
  928. // precondition: never called in_interrupt
  929. int
  930. usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
  931. {
  932. struct usbnet *dev;
  933. struct net_device *net;
  934. struct usb_host_interface *interface;
  935. struct driver_info *info;
  936. struct usb_device *xdev;
  937. int status;
  938. const char *name;
  939. name = udev->dev.driver->name;
  940. info = (struct driver_info *) prod->driver_info;
  941. if (!info) {
  942. dev_dbg (&udev->dev, "blacklisted by %s\n", name);
  943. return -ENODEV;
  944. }
  945. xdev = interface_to_usbdev (udev);
  946. interface = udev->cur_altsetting;
  947. usb_get_dev (xdev);
  948. status = -ENOMEM;
  949. // set up our own records
  950. net = alloc_etherdev(sizeof(*dev));
  951. if (!net) {
  952. dbg ("can't kmalloc dev");
  953. goto out;
  954. }
  955. dev = netdev_priv(net);
  956. dev->udev = xdev;
  957. dev->driver_info = info;
  958. dev->driver_name = name;
  959. dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
  960. | NETIF_MSG_PROBE | NETIF_MSG_LINK);
  961. skb_queue_head_init (&dev->rxq);
  962. skb_queue_head_init (&dev->txq);
  963. skb_queue_head_init (&dev->done);
  964. dev->bh.func = usbnet_bh;
  965. dev->bh.data = (unsigned long) dev;
  966. INIT_WORK (&dev->kevent, kevent);
  967. dev->delay.function = usbnet_bh;
  968. dev->delay.data = (unsigned long) dev;
  969. init_timer (&dev->delay);
  970. mutex_init (&dev->phy_mutex);
  971. SET_MODULE_OWNER (net);
  972. dev->net = net;
  973. strcpy (net->name, "usb%d");
  974. memcpy (net->dev_addr, node_id, sizeof node_id);
  975. /* rx and tx sides can use different message sizes;
  976. * bind() should set rx_urb_size in that case.
  977. */
  978. dev->hard_mtu = net->mtu + net->hard_header_len;
  979. #if 0
  980. // dma_supported() is deeply broken on almost all architectures
  981. // possible with some EHCI controllers
  982. if (dma_supported (&udev->dev, DMA_64BIT_MASK))
  983. net->features |= NETIF_F_HIGHDMA;
  984. #endif
  985. net->change_mtu = usbnet_change_mtu;
  986. net->get_stats = usbnet_get_stats;
  987. net->hard_start_xmit = usbnet_start_xmit;
  988. net->open = usbnet_open;
  989. net->stop = usbnet_stop;
  990. net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
  991. net->tx_timeout = usbnet_tx_timeout;
  992. net->ethtool_ops = &usbnet_ethtool_ops;
  993. // allow device-specific bind/init procedures
  994. // NOTE net->name still not usable ...
  995. if (info->bind) {
  996. status = info->bind (dev, udev);
  997. if (status < 0)
  998. goto out1;
  999. // heuristic: "usb%d" for links we know are two-host,
  1000. // else "eth%d" when there's reasonable doubt. userspace
  1001. // can rename the link if it knows better.
  1002. if ((dev->driver_info->flags & FLAG_ETHER) != 0
  1003. && (net->dev_addr [0] & 0x02) == 0)
  1004. strcpy (net->name, "eth%d");
  1005. /* maybe the remote can't receive an Ethernet MTU */
  1006. if (net->mtu > (dev->hard_mtu - net->hard_header_len))
  1007. net->mtu = dev->hard_mtu - net->hard_header_len;
  1008. } else if (!info->in || !info->out)
  1009. status = usbnet_get_endpoints (dev, udev);
  1010. else {
  1011. dev->in = usb_rcvbulkpipe (xdev, info->in);
  1012. dev->out = usb_sndbulkpipe (xdev, info->out);
  1013. if (!(info->flags & FLAG_NO_SETINT))
  1014. status = usb_set_interface (xdev,
  1015. interface->desc.bInterfaceNumber,
  1016. interface->desc.bAlternateSetting);
  1017. else
  1018. status = 0;
  1019. }
  1020. if (status >= 0 && dev->status)
  1021. status = init_status (dev, udev);
  1022. if (status < 0)
  1023. goto out3;
  1024. if (!dev->rx_urb_size)
  1025. dev->rx_urb_size = dev->hard_mtu;
  1026. dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
  1027. SET_NETDEV_DEV(net, &udev->dev);
  1028. status = register_netdev (net);
  1029. if (status)
  1030. goto out3;
  1031. if (netif_msg_probe (dev))
  1032. devinfo (dev, "register '%s' at usb-%s-%s, %s, "
  1033. "%02x:%02x:%02x:%02x:%02x:%02x",
  1034. udev->dev.driver->name,
  1035. xdev->bus->bus_name, xdev->devpath,
  1036. dev->driver_info->description,
  1037. net->dev_addr [0], net->dev_addr [1],
  1038. net->dev_addr [2], net->dev_addr [3],
  1039. net->dev_addr [4], net->dev_addr [5]);
  1040. // ok, it's ready to go.
  1041. usb_set_intfdata (udev, dev);
  1042. // start as if the link is up
  1043. netif_device_attach (net);
  1044. return 0;
  1045. out3:
  1046. if (info->unbind)
  1047. info->unbind (dev, udev);
  1048. out1:
  1049. free_netdev(net);
  1050. out:
  1051. usb_put_dev(xdev);
  1052. return status;
  1053. }
  1054. EXPORT_SYMBOL_GPL(usbnet_probe);
  1055. /*-------------------------------------------------------------------------*/
  1056. /*
  1057. * suspend the whole driver as soon as the first interface is suspended
  1058. * resume only when the last interface is resumed
  1059. */
  1060. int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
  1061. {
  1062. struct usbnet *dev = usb_get_intfdata(intf);
  1063. if (!dev->suspend_count++) {
  1064. /* accelerate emptying of the rx and queues, to avoid
  1065. * having everything error out.
  1066. */
  1067. netif_device_detach (dev->net);
  1068. (void) unlink_urbs (dev, &dev->rxq);
  1069. (void) unlink_urbs (dev, &dev->txq);
  1070. }
  1071. return 0;
  1072. }
  1073. EXPORT_SYMBOL_GPL(usbnet_suspend);
  1074. int usbnet_resume (struct usb_interface *intf)
  1075. {
  1076. struct usbnet *dev = usb_get_intfdata(intf);
  1077. if (!--dev->suspend_count) {
  1078. netif_device_attach (dev->net);
  1079. tasklet_schedule (&dev->bh);
  1080. }
  1081. return 0;
  1082. }
  1083. EXPORT_SYMBOL_GPL(usbnet_resume);
  1084. /*-------------------------------------------------------------------------*/
  1085. static int __init usbnet_init(void)
  1086. {
  1087. /* compiler should optimize this out */
  1088. BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb)
  1089. < sizeof (struct skb_data));
  1090. random_ether_addr(node_id);
  1091. return 0;
  1092. }
  1093. module_init(usbnet_init);
  1094. static void __exit usbnet_exit(void)
  1095. {
  1096. }
  1097. module_exit(usbnet_exit);
  1098. MODULE_AUTHOR("David Brownell");
  1099. MODULE_DESCRIPTION("USB network driver framework");
  1100. MODULE_LICENSE("GPL");