usbnet.c 40 KB

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