usbnet.c 40 KB

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