usbnet.c 41 KB

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