usbnet.c 42 KB

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