usbnet.c 35 KB

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