usbnet.c 42 KB

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