usbnet.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  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. struct driver_info *info = dev->driver_info;
  494. int temp;
  495. int retval;
  496. DECLARE_WAIT_QUEUE_HEAD_ONSTACK (unlink_wakeup);
  497. DECLARE_WAITQUEUE (wait, current);
  498. netif_stop_queue (net);
  499. if (netif_msg_ifdown (dev))
  500. devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
  501. net->stats.rx_packets, net->stats.tx_packets,
  502. net->stats.rx_errors, net->stats.tx_errors
  503. );
  504. /* allow minidriver to stop correctly (wireless devices to turn off
  505. * radio etc) */
  506. if (info->stop) {
  507. retval = info->stop(dev);
  508. if (retval < 0 && netif_msg_ifdown(dev))
  509. devinfo(dev,
  510. "stop fail (%d) usbnet usb-%s-%s, %s",
  511. retval,
  512. dev->udev->bus->bus_name, dev->udev->devpath,
  513. info->description);
  514. }
  515. if (!(info->flags & FLAG_AVOID_UNLINK_URBS)) {
  516. /* ensure there are no more active urbs */
  517. add_wait_queue(&unlink_wakeup, &wait);
  518. dev->wait = &unlink_wakeup;
  519. temp = unlink_urbs(dev, &dev->txq) +
  520. unlink_urbs(dev, &dev->rxq);
  521. /* maybe wait for deletions to finish. */
  522. while (!skb_queue_empty(&dev->rxq)
  523. && !skb_queue_empty(&dev->txq)
  524. && !skb_queue_empty(&dev->done)) {
  525. msleep(UNLINK_TIMEOUT_MS);
  526. if (netif_msg_ifdown(dev))
  527. devdbg(dev, "waited for %d urb completions",
  528. temp);
  529. }
  530. dev->wait = NULL;
  531. remove_wait_queue(&unlink_wakeup, &wait);
  532. }
  533. usb_kill_urb(dev->interrupt);
  534. /* deferred work (task, timer, softirq) must also stop.
  535. * can't flush_scheduled_work() until we drop rtnl (later),
  536. * else workers could deadlock; so make workers a NOP.
  537. */
  538. dev->flags = 0;
  539. del_timer_sync (&dev->delay);
  540. tasklet_kill (&dev->bh);
  541. usb_autopm_put_interface(dev->intf);
  542. return 0;
  543. }
  544. EXPORT_SYMBOL_GPL(usbnet_stop);
  545. /*-------------------------------------------------------------------------*/
  546. // posts reads, and enables write queuing
  547. // precondition: never called in_interrupt
  548. int usbnet_open (struct net_device *net)
  549. {
  550. struct usbnet *dev = netdev_priv(net);
  551. int retval;
  552. struct driver_info *info = dev->driver_info;
  553. if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
  554. if (netif_msg_ifup (dev))
  555. devinfo (dev,
  556. "resumption fail (%d) usbnet usb-%s-%s, %s",
  557. retval,
  558. dev->udev->bus->bus_name, dev->udev->devpath,
  559. info->description);
  560. goto done_nopm;
  561. }
  562. // put into "known safe" state
  563. if (info->reset && (retval = info->reset (dev)) < 0) {
  564. if (netif_msg_ifup (dev))
  565. devinfo (dev,
  566. "open reset fail (%d) usbnet usb-%s-%s, %s",
  567. retval,
  568. dev->udev->bus->bus_name, dev->udev->devpath,
  569. info->description);
  570. goto done;
  571. }
  572. // insist peer be connected
  573. if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
  574. if (netif_msg_ifup (dev))
  575. devdbg (dev, "can't open; %d", retval);
  576. goto done;
  577. }
  578. /* start any status interrupt transfer */
  579. if (dev->interrupt) {
  580. retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
  581. if (retval < 0) {
  582. if (netif_msg_ifup (dev))
  583. deverr (dev, "intr submit %d", retval);
  584. goto done;
  585. }
  586. }
  587. netif_start_queue (net);
  588. if (netif_msg_ifup (dev)) {
  589. char *framing;
  590. if (dev->driver_info->flags & FLAG_FRAMING_NC)
  591. framing = "NetChip";
  592. else if (dev->driver_info->flags & FLAG_FRAMING_GL)
  593. framing = "GeneSys";
  594. else if (dev->driver_info->flags & FLAG_FRAMING_Z)
  595. framing = "Zaurus";
  596. else if (dev->driver_info->flags & FLAG_FRAMING_RN)
  597. framing = "RNDIS";
  598. else if (dev->driver_info->flags & FLAG_FRAMING_AX)
  599. framing = "ASIX";
  600. else
  601. framing = "simple";
  602. devinfo (dev, "open: enable queueing "
  603. "(rx %d, tx %d) mtu %d %s framing",
  604. (int)RX_QLEN (dev), (int)TX_QLEN (dev), dev->net->mtu,
  605. framing);
  606. }
  607. // delay posting reads until we're fully open
  608. tasklet_schedule (&dev->bh);
  609. return retval;
  610. done:
  611. usb_autopm_put_interface(dev->intf);
  612. done_nopm:
  613. return retval;
  614. }
  615. EXPORT_SYMBOL_GPL(usbnet_open);
  616. /*-------------------------------------------------------------------------*/
  617. /* ethtool methods; minidrivers may need to add some more, but
  618. * they'll probably want to use this base set.
  619. */
  620. int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
  621. {
  622. struct usbnet *dev = netdev_priv(net);
  623. if (!dev->mii.mdio_read)
  624. return -EOPNOTSUPP;
  625. return mii_ethtool_gset(&dev->mii, cmd);
  626. }
  627. EXPORT_SYMBOL_GPL(usbnet_get_settings);
  628. int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
  629. {
  630. struct usbnet *dev = netdev_priv(net);
  631. int retval;
  632. if (!dev->mii.mdio_write)
  633. return -EOPNOTSUPP;
  634. retval = mii_ethtool_sset(&dev->mii, cmd);
  635. /* link speed/duplex might have changed */
  636. if (dev->driver_info->link_reset)
  637. dev->driver_info->link_reset(dev);
  638. return retval;
  639. }
  640. EXPORT_SYMBOL_GPL(usbnet_set_settings);
  641. u32 usbnet_get_link (struct net_device *net)
  642. {
  643. struct usbnet *dev = netdev_priv(net);
  644. /* If a check_connect is defined, return its result */
  645. if (dev->driver_info->check_connect)
  646. return dev->driver_info->check_connect (dev) == 0;
  647. /* if the device has mii operations, use those */
  648. if (dev->mii.mdio_read)
  649. return mii_link_ok(&dev->mii);
  650. /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
  651. return ethtool_op_get_link(net);
  652. }
  653. EXPORT_SYMBOL_GPL(usbnet_get_link);
  654. int usbnet_nway_reset(struct net_device *net)
  655. {
  656. struct usbnet *dev = netdev_priv(net);
  657. if (!dev->mii.mdio_write)
  658. return -EOPNOTSUPP;
  659. return mii_nway_restart(&dev->mii);
  660. }
  661. EXPORT_SYMBOL_GPL(usbnet_nway_reset);
  662. void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
  663. {
  664. struct usbnet *dev = netdev_priv(net);
  665. strncpy (info->driver, dev->driver_name, sizeof info->driver);
  666. strncpy (info->version, DRIVER_VERSION, sizeof info->version);
  667. strncpy (info->fw_version, dev->driver_info->description,
  668. sizeof info->fw_version);
  669. usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
  670. }
  671. EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
  672. u32 usbnet_get_msglevel (struct net_device *net)
  673. {
  674. struct usbnet *dev = netdev_priv(net);
  675. return dev->msg_enable;
  676. }
  677. EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
  678. void usbnet_set_msglevel (struct net_device *net, u32 level)
  679. {
  680. struct usbnet *dev = netdev_priv(net);
  681. dev->msg_enable = level;
  682. }
  683. EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
  684. /* drivers may override default ethtool_ops in their bind() routine */
  685. static struct ethtool_ops usbnet_ethtool_ops = {
  686. .get_settings = usbnet_get_settings,
  687. .set_settings = usbnet_set_settings,
  688. .get_link = usbnet_get_link,
  689. .nway_reset = usbnet_nway_reset,
  690. .get_drvinfo = usbnet_get_drvinfo,
  691. .get_msglevel = usbnet_get_msglevel,
  692. .set_msglevel = usbnet_set_msglevel,
  693. };
  694. /*-------------------------------------------------------------------------*/
  695. /* work that cannot be done in interrupt context uses keventd.
  696. *
  697. * NOTE: with 2.5 we could do more of this using completion callbacks,
  698. * especially now that control transfers can be queued.
  699. */
  700. static void
  701. kevent (struct work_struct *work)
  702. {
  703. struct usbnet *dev =
  704. container_of(work, struct usbnet, kevent);
  705. int status;
  706. /* usb_clear_halt() needs a thread context */
  707. if (test_bit (EVENT_TX_HALT, &dev->flags)) {
  708. unlink_urbs (dev, &dev->txq);
  709. status = usb_clear_halt (dev->udev, dev->out);
  710. if (status < 0
  711. && status != -EPIPE
  712. && status != -ESHUTDOWN) {
  713. if (netif_msg_tx_err (dev))
  714. deverr (dev, "can't clear tx halt, status %d",
  715. status);
  716. } else {
  717. clear_bit (EVENT_TX_HALT, &dev->flags);
  718. if (status != -ESHUTDOWN)
  719. netif_wake_queue (dev->net);
  720. }
  721. }
  722. if (test_bit (EVENT_RX_HALT, &dev->flags)) {
  723. unlink_urbs (dev, &dev->rxq);
  724. status = usb_clear_halt (dev->udev, dev->in);
  725. if (status < 0
  726. && status != -EPIPE
  727. && status != -ESHUTDOWN) {
  728. if (netif_msg_rx_err (dev))
  729. deverr (dev, "can't clear rx halt, status %d",
  730. status);
  731. } else {
  732. clear_bit (EVENT_RX_HALT, &dev->flags);
  733. tasklet_schedule (&dev->bh);
  734. }
  735. }
  736. /* tasklet could resubmit itself forever if memory is tight */
  737. if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
  738. struct urb *urb = NULL;
  739. if (netif_running (dev->net))
  740. urb = usb_alloc_urb (0, GFP_KERNEL);
  741. else
  742. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  743. if (urb != NULL) {
  744. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  745. rx_submit (dev, urb, GFP_KERNEL);
  746. tasklet_schedule (&dev->bh);
  747. }
  748. }
  749. if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
  750. struct driver_info *info = dev->driver_info;
  751. int retval = 0;
  752. clear_bit (EVENT_LINK_RESET, &dev->flags);
  753. if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
  754. devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
  755. retval,
  756. dev->udev->bus->bus_name, dev->udev->devpath,
  757. info->description);
  758. }
  759. }
  760. if (dev->flags)
  761. devdbg (dev, "kevent done, flags = 0x%lx",
  762. dev->flags);
  763. }
  764. /*-------------------------------------------------------------------------*/
  765. static void tx_complete (struct urb *urb)
  766. {
  767. struct sk_buff *skb = (struct sk_buff *) urb->context;
  768. struct skb_data *entry = (struct skb_data *) skb->cb;
  769. struct usbnet *dev = entry->dev;
  770. if (urb->status == 0) {
  771. dev->net->stats.tx_packets++;
  772. dev->net->stats.tx_bytes += entry->length;
  773. } else {
  774. dev->net->stats.tx_errors++;
  775. switch (urb->status) {
  776. case -EPIPE:
  777. usbnet_defer_kevent (dev, EVENT_TX_HALT);
  778. break;
  779. /* software-driven interface shutdown */
  780. case -ECONNRESET: // async unlink
  781. case -ESHUTDOWN: // hardware gone
  782. break;
  783. // like rx, tx gets controller i/o faults during khubd delays
  784. // and so it uses the same throttling mechanism.
  785. case -EPROTO:
  786. case -ETIME:
  787. case -EILSEQ:
  788. if (!timer_pending (&dev->delay)) {
  789. mod_timer (&dev->delay,
  790. jiffies + THROTTLE_JIFFIES);
  791. if (netif_msg_link (dev))
  792. devdbg (dev, "tx throttle %d",
  793. urb->status);
  794. }
  795. netif_stop_queue (dev->net);
  796. break;
  797. default:
  798. if (netif_msg_tx_err (dev))
  799. devdbg (dev, "tx err %d", entry->urb->status);
  800. break;
  801. }
  802. }
  803. urb->dev = NULL;
  804. entry->state = tx_done;
  805. defer_bh(dev, skb, &dev->txq);
  806. }
  807. /*-------------------------------------------------------------------------*/
  808. void usbnet_tx_timeout (struct net_device *net)
  809. {
  810. struct usbnet *dev = netdev_priv(net);
  811. unlink_urbs (dev, &dev->txq);
  812. tasklet_schedule (&dev->bh);
  813. // FIXME: device recovery -- reset?
  814. }
  815. EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
  816. /*-------------------------------------------------------------------------*/
  817. int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
  818. {
  819. struct usbnet *dev = netdev_priv(net);
  820. int length;
  821. int retval = NET_XMIT_SUCCESS;
  822. struct urb *urb = NULL;
  823. struct skb_data *entry;
  824. struct driver_info *info = dev->driver_info;
  825. unsigned long flags;
  826. // some devices want funky USB-level framing, for
  827. // win32 driver (usually) and/or hardware quirks
  828. if (info->tx_fixup) {
  829. skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
  830. if (!skb) {
  831. if (netif_msg_tx_err (dev))
  832. devdbg (dev, "can't tx_fixup skb");
  833. goto drop;
  834. }
  835. }
  836. length = skb->len;
  837. if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
  838. if (netif_msg_tx_err (dev))
  839. devdbg (dev, "no urb");
  840. goto drop;
  841. }
  842. entry = (struct skb_data *) skb->cb;
  843. entry->urb = urb;
  844. entry->dev = dev;
  845. entry->state = tx_start;
  846. entry->length = length;
  847. usb_fill_bulk_urb (urb, dev->udev, dev->out,
  848. skb->data, skb->len, tx_complete, skb);
  849. /* don't assume the hardware handles USB_ZERO_PACKET
  850. * NOTE: strictly conforming cdc-ether devices should expect
  851. * the ZLP here, but ignore the one-byte packet.
  852. */
  853. if ((length % dev->maxpacket) == 0) {
  854. urb->transfer_buffer_length++;
  855. if (skb_tailroom(skb)) {
  856. skb->data[skb->len] = 0;
  857. __skb_put(skb, 1);
  858. }
  859. }
  860. spin_lock_irqsave (&dev->txq.lock, flags);
  861. switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
  862. case -EPIPE:
  863. netif_stop_queue (net);
  864. usbnet_defer_kevent (dev, EVENT_TX_HALT);
  865. break;
  866. default:
  867. if (netif_msg_tx_err (dev))
  868. devdbg (dev, "tx: submit urb err %d", retval);
  869. break;
  870. case 0:
  871. net->trans_start = jiffies;
  872. __skb_queue_tail (&dev->txq, skb);
  873. if (dev->txq.qlen >= TX_QLEN (dev))
  874. netif_stop_queue (net);
  875. }
  876. spin_unlock_irqrestore (&dev->txq.lock, flags);
  877. if (retval) {
  878. if (netif_msg_tx_err (dev))
  879. devdbg (dev, "drop, code %d", retval);
  880. drop:
  881. retval = NET_XMIT_SUCCESS;
  882. dev->net->stats.tx_dropped++;
  883. if (skb)
  884. dev_kfree_skb_any (skb);
  885. usb_free_urb (urb);
  886. } else if (netif_msg_tx_queued (dev)) {
  887. devdbg (dev, "> tx, len %d, type 0x%x",
  888. length, skb->protocol);
  889. }
  890. return retval;
  891. }
  892. EXPORT_SYMBOL_GPL(usbnet_start_xmit);
  893. /*-------------------------------------------------------------------------*/
  894. // tasklet (work deferred from completions, in_irq) or timer
  895. static void usbnet_bh (unsigned long param)
  896. {
  897. struct usbnet *dev = (struct usbnet *) param;
  898. struct sk_buff *skb;
  899. struct skb_data *entry;
  900. while ((skb = skb_dequeue (&dev->done))) {
  901. entry = (struct skb_data *) skb->cb;
  902. switch (entry->state) {
  903. case rx_done:
  904. entry->state = rx_cleanup;
  905. rx_process (dev, skb);
  906. continue;
  907. case tx_done:
  908. case rx_cleanup:
  909. usb_free_urb (entry->urb);
  910. dev_kfree_skb (skb);
  911. continue;
  912. default:
  913. devdbg (dev, "bogus skb state %d", entry->state);
  914. }
  915. }
  916. // waiting for all pending urbs to complete?
  917. if (dev->wait) {
  918. if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
  919. wake_up (dev->wait);
  920. }
  921. // or are we maybe short a few urbs?
  922. } else if (netif_running (dev->net)
  923. && netif_device_present (dev->net)
  924. && !timer_pending (&dev->delay)
  925. && !test_bit (EVENT_RX_HALT, &dev->flags)) {
  926. int temp = dev->rxq.qlen;
  927. int qlen = RX_QLEN (dev);
  928. if (temp < qlen) {
  929. struct urb *urb;
  930. int i;
  931. // don't refill the queue all at once
  932. for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
  933. urb = usb_alloc_urb (0, GFP_ATOMIC);
  934. if (urb != NULL)
  935. rx_submit (dev, urb, GFP_ATOMIC);
  936. }
  937. if (temp != dev->rxq.qlen && netif_msg_link (dev))
  938. devdbg (dev, "rxqlen %d --> %d",
  939. temp, dev->rxq.qlen);
  940. if (dev->rxq.qlen < qlen)
  941. tasklet_schedule (&dev->bh);
  942. }
  943. if (dev->txq.qlen < TX_QLEN (dev))
  944. netif_wake_queue (dev->net);
  945. }
  946. }
  947. /*-------------------------------------------------------------------------
  948. *
  949. * USB Device Driver support
  950. *
  951. *-------------------------------------------------------------------------*/
  952. // precondition: never called in_interrupt
  953. void usbnet_disconnect (struct usb_interface *intf)
  954. {
  955. struct usbnet *dev;
  956. struct usb_device *xdev;
  957. struct net_device *net;
  958. dev = usb_get_intfdata(intf);
  959. usb_set_intfdata(intf, NULL);
  960. if (!dev)
  961. return;
  962. xdev = interface_to_usbdev (intf);
  963. if (netif_msg_probe (dev))
  964. devinfo (dev, "unregister '%s' usb-%s-%s, %s",
  965. intf->dev.driver->name,
  966. xdev->bus->bus_name, xdev->devpath,
  967. dev->driver_info->description);
  968. net = dev->net;
  969. unregister_netdev (net);
  970. /* we don't hold rtnl here ... */
  971. flush_scheduled_work ();
  972. if (dev->driver_info->unbind)
  973. dev->driver_info->unbind (dev, intf);
  974. free_netdev(net);
  975. usb_put_dev (xdev);
  976. }
  977. EXPORT_SYMBOL_GPL(usbnet_disconnect);
  978. static const struct net_device_ops usbnet_netdev_ops = {
  979. .ndo_open = usbnet_open,
  980. .ndo_stop = usbnet_stop,
  981. .ndo_start_xmit = usbnet_start_xmit,
  982. .ndo_tx_timeout = usbnet_tx_timeout,
  983. .ndo_change_mtu = usbnet_change_mtu,
  984. .ndo_set_mac_address = eth_mac_addr,
  985. .ndo_validate_addr = eth_validate_addr,
  986. };
  987. /*-------------------------------------------------------------------------*/
  988. // precondition: never called in_interrupt
  989. int
  990. usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
  991. {
  992. struct usbnet *dev;
  993. struct net_device *net;
  994. struct usb_host_interface *interface;
  995. struct driver_info *info;
  996. struct usb_device *xdev;
  997. int status;
  998. const char *name;
  999. name = udev->dev.driver->name;
  1000. info = (struct driver_info *) prod->driver_info;
  1001. if (!info) {
  1002. dev_dbg (&udev->dev, "blacklisted by %s\n", name);
  1003. return -ENODEV;
  1004. }
  1005. xdev = interface_to_usbdev (udev);
  1006. interface = udev->cur_altsetting;
  1007. usb_get_dev (xdev);
  1008. status = -ENOMEM;
  1009. // set up our own records
  1010. net = alloc_etherdev(sizeof(*dev));
  1011. if (!net) {
  1012. dbg ("can't kmalloc dev");
  1013. goto out;
  1014. }
  1015. dev = netdev_priv(net);
  1016. dev->udev = xdev;
  1017. dev->intf = udev;
  1018. dev->driver_info = info;
  1019. dev->driver_name = name;
  1020. dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
  1021. | NETIF_MSG_PROBE | NETIF_MSG_LINK);
  1022. skb_queue_head_init (&dev->rxq);
  1023. skb_queue_head_init (&dev->txq);
  1024. skb_queue_head_init (&dev->done);
  1025. dev->bh.func = usbnet_bh;
  1026. dev->bh.data = (unsigned long) dev;
  1027. INIT_WORK (&dev->kevent, kevent);
  1028. dev->delay.function = usbnet_bh;
  1029. dev->delay.data = (unsigned long) dev;
  1030. init_timer (&dev->delay);
  1031. mutex_init (&dev->phy_mutex);
  1032. dev->net = net;
  1033. strcpy (net->name, "usb%d");
  1034. memcpy (net->dev_addr, node_id, sizeof node_id);
  1035. /* rx and tx sides can use different message sizes;
  1036. * bind() should set rx_urb_size in that case.
  1037. */
  1038. dev->hard_mtu = net->mtu + net->hard_header_len;
  1039. #if 0
  1040. // dma_supported() is deeply broken on almost all architectures
  1041. // possible with some EHCI controllers
  1042. if (dma_supported (&udev->dev, DMA_BIT_MASK(64)))
  1043. net->features |= NETIF_F_HIGHDMA;
  1044. #endif
  1045. net->netdev_ops = &usbnet_netdev_ops;
  1046. net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
  1047. net->ethtool_ops = &usbnet_ethtool_ops;
  1048. // allow device-specific bind/init procedures
  1049. // NOTE net->name still not usable ...
  1050. if (info->bind) {
  1051. status = info->bind (dev, udev);
  1052. if (status < 0)
  1053. goto out1;
  1054. // heuristic: "usb%d" for links we know are two-host,
  1055. // else "eth%d" when there's reasonable doubt. userspace
  1056. // can rename the link if it knows better.
  1057. if ((dev->driver_info->flags & FLAG_ETHER) != 0
  1058. && (net->dev_addr [0] & 0x02) == 0)
  1059. strcpy (net->name, "eth%d");
  1060. /* WLAN devices should always be named "wlan%d" */
  1061. if ((dev->driver_info->flags & FLAG_WLAN) != 0)
  1062. strcpy(net->name, "wlan%d");
  1063. /* maybe the remote can't receive an Ethernet MTU */
  1064. if (net->mtu > (dev->hard_mtu - net->hard_header_len))
  1065. net->mtu = dev->hard_mtu - net->hard_header_len;
  1066. } else if (!info->in || !info->out)
  1067. status = usbnet_get_endpoints (dev, udev);
  1068. else {
  1069. dev->in = usb_rcvbulkpipe (xdev, info->in);
  1070. dev->out = usb_sndbulkpipe (xdev, info->out);
  1071. if (!(info->flags & FLAG_NO_SETINT))
  1072. status = usb_set_interface (xdev,
  1073. interface->desc.bInterfaceNumber,
  1074. interface->desc.bAlternateSetting);
  1075. else
  1076. status = 0;
  1077. }
  1078. if (status >= 0 && dev->status)
  1079. status = init_status (dev, udev);
  1080. if (status < 0)
  1081. goto out3;
  1082. if (!dev->rx_urb_size)
  1083. dev->rx_urb_size = dev->hard_mtu;
  1084. dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
  1085. SET_NETDEV_DEV(net, &udev->dev);
  1086. status = register_netdev (net);
  1087. if (status)
  1088. goto out3;
  1089. if (netif_msg_probe (dev))
  1090. devinfo (dev, "register '%s' at usb-%s-%s, %s, %pM",
  1091. udev->dev.driver->name,
  1092. xdev->bus->bus_name, xdev->devpath,
  1093. dev->driver_info->description,
  1094. net->dev_addr);
  1095. // ok, it's ready to go.
  1096. usb_set_intfdata (udev, dev);
  1097. // start as if the link is up
  1098. netif_device_attach (net);
  1099. return 0;
  1100. out3:
  1101. if (info->unbind)
  1102. info->unbind (dev, udev);
  1103. out1:
  1104. free_netdev(net);
  1105. out:
  1106. usb_put_dev(xdev);
  1107. return status;
  1108. }
  1109. EXPORT_SYMBOL_GPL(usbnet_probe);
  1110. /*-------------------------------------------------------------------------*/
  1111. /*
  1112. * suspend the whole driver as soon as the first interface is suspended
  1113. * resume only when the last interface is resumed
  1114. */
  1115. int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
  1116. {
  1117. struct usbnet *dev = usb_get_intfdata(intf);
  1118. if (!dev->suspend_count++) {
  1119. /*
  1120. * accelerate emptying of the rx and queues, to avoid
  1121. * having everything error out.
  1122. */
  1123. netif_device_detach (dev->net);
  1124. (void) unlink_urbs (dev, &dev->rxq);
  1125. (void) unlink_urbs (dev, &dev->txq);
  1126. /*
  1127. * reattach so runtime management can use and
  1128. * wake the device
  1129. */
  1130. netif_device_attach (dev->net);
  1131. }
  1132. return 0;
  1133. }
  1134. EXPORT_SYMBOL_GPL(usbnet_suspend);
  1135. int usbnet_resume (struct usb_interface *intf)
  1136. {
  1137. struct usbnet *dev = usb_get_intfdata(intf);
  1138. if (!--dev->suspend_count)
  1139. tasklet_schedule (&dev->bh);
  1140. return 0;
  1141. }
  1142. EXPORT_SYMBOL_GPL(usbnet_resume);
  1143. /*-------------------------------------------------------------------------*/
  1144. static int __init usbnet_init(void)
  1145. {
  1146. /* compiler should optimize this out */
  1147. BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb)
  1148. < sizeof (struct skb_data));
  1149. random_ether_addr(node_id);
  1150. return 0;
  1151. }
  1152. module_init(usbnet_init);
  1153. static void __exit usbnet_exit(void)
  1154. {
  1155. }
  1156. module_exit(usbnet_exit);
  1157. MODULE_AUTHOR("David Brownell");
  1158. MODULE_DESCRIPTION("USB network driver framework");
  1159. MODULE_LICENSE("GPL");