usbnet.c 42 KB

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