usbnet.c 43 KB

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