usbnet.c 35 KB

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