usbnet.c 40 KB

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