usbnet.c 37 KB

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