usbnet.c 36 KB

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