usbnet.c 40 KB

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