usbnet.c 40 KB

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