usbnet.c 43 KB

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