usbnet.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * USB Network driver infrastructure
  4. * Copyright (C) 2000-2005 by David Brownell
  5. * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
  6. */
  7. /*
  8. * This is a generic "USB networking" framework that works with several
  9. * kinds of full and high speed networking devices: host-to-host cables,
  10. * smart usb peripherals, and actual Ethernet adapters.
  11. *
  12. * These devices usually differ in terms of control protocols (if they
  13. * even have one!) and sometimes they define new framing to wrap or batch
  14. * Ethernet packets. Otherwise, they talk to USB pretty much the same,
  15. * so interface (un)binding, endpoint I/O queues, fault handling, and other
  16. * issues can usefully be addressed by this framework.
  17. */
  18. // #define DEBUG // error path messages, extra info
  19. // #define VERBOSE // more; success messages
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/ctype.h>
  25. #include <linux/ethtool.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/mii.h>
  28. #include <linux/usb.h>
  29. #include <linux/usb/usbnet.h>
  30. #include <linux/slab.h>
  31. #include <linux/kernel.h>
  32. #include <linux/pm_runtime.h>
  33. #define DRIVER_VERSION "22-Aug-2005"
  34. /*-------------------------------------------------------------------------*/
  35. /*
  36. * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
  37. * Several dozen bytes of IPv4 data can fit in two such transactions.
  38. * One maximum size Ethernet packet takes twenty four of them.
  39. * For high speed, each frame comfortably fits almost 36 max size
  40. * Ethernet packets (so queues should be bigger).
  41. *
  42. * The goal is to let the USB host controller be busy for 5msec or
  43. * more before an irq is required, under load. Jumbograms change
  44. * the equation.
  45. */
  46. #define MAX_QUEUE_MEMORY (60 * 1518)
  47. #define RX_QLEN(dev) ((dev)->rx_qlen)
  48. #define TX_QLEN(dev) ((dev)->tx_qlen)
  49. // reawaken network queue this soon after stopping; else watchdog barks
  50. #define TX_TIMEOUT_JIFFIES (5*HZ)
  51. /* throttle rx/tx briefly after some faults, so hub_wq might disconnect()
  52. * us (it polls at HZ/4 usually) before we report too many false errors.
  53. */
  54. #define THROTTLE_JIFFIES (HZ/8)
  55. // between wakeups
  56. #define UNLINK_TIMEOUT_MS 3
  57. /*-------------------------------------------------------------------------*/
  58. // randomly generated ethernet address
  59. static u8 node_id [ETH_ALEN];
  60. /* use ethtool to change the level for any given device */
  61. static int msg_level = -1;
  62. module_param (msg_level, int, 0);
  63. MODULE_PARM_DESC (msg_level, "Override default message level");
  64. /*-------------------------------------------------------------------------*/
  65. /* handles CDC Ethernet and many other network "bulk data" interfaces */
  66. int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
  67. {
  68. int tmp;
  69. struct usb_host_interface *alt = NULL;
  70. struct usb_host_endpoint *in = NULL, *out = NULL;
  71. struct usb_host_endpoint *status = NULL;
  72. for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
  73. unsigned ep;
  74. in = out = status = NULL;
  75. alt = intf->altsetting + tmp;
  76. /* take the first altsetting with in-bulk + out-bulk;
  77. * remember any status endpoint, just in case;
  78. * ignore other endpoints and altsettings.
  79. */
  80. for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
  81. struct usb_host_endpoint *e;
  82. int intr = 0;
  83. e = alt->endpoint + ep;
  84. /* ignore endpoints which cannot transfer data */
  85. if (!usb_endpoint_maxp(&e->desc))
  86. continue;
  87. switch (e->desc.bmAttributes) {
  88. case USB_ENDPOINT_XFER_INT:
  89. if (!usb_endpoint_dir_in(&e->desc))
  90. continue;
  91. intr = 1;
  92. /* FALLTHROUGH */
  93. case USB_ENDPOINT_XFER_BULK:
  94. break;
  95. default:
  96. continue;
  97. }
  98. if (usb_endpoint_dir_in(&e->desc)) {
  99. if (!intr && !in)
  100. in = e;
  101. else if (intr && !status)
  102. status = e;
  103. } else {
  104. if (!out)
  105. out = e;
  106. }
  107. }
  108. if (in && out)
  109. break;
  110. }
  111. if (!alt || !in || !out)
  112. return -EINVAL;
  113. if (alt->desc.bAlternateSetting != 0 ||
  114. !(dev->driver_info->flags & FLAG_NO_SETINT)) {
  115. tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
  116. alt->desc.bAlternateSetting);
  117. if (tmp < 0)
  118. return tmp;
  119. }
  120. dev->in = usb_rcvbulkpipe (dev->udev,
  121. in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  122. dev->out = usb_sndbulkpipe (dev->udev,
  123. out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  124. dev->status = status;
  125. return 0;
  126. }
  127. EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
  128. int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
  129. {
  130. int tmp = -1, ret;
  131. unsigned char buf [13];
  132. ret = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
  133. if (ret == 12)
  134. tmp = hex2bin(dev->net->dev_addr, buf, 6);
  135. if (tmp < 0) {
  136. dev_dbg(&dev->udev->dev,
  137. "bad MAC string %d fetch, %d\n", iMACAddress, tmp);
  138. if (ret >= 0)
  139. ret = -EINVAL;
  140. return ret;
  141. }
  142. return 0;
  143. }
  144. EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
  145. static void intr_complete (struct urb *urb)
  146. {
  147. struct usbnet *dev = urb->context;
  148. int status = urb->status;
  149. switch (status) {
  150. /* success */
  151. case 0:
  152. dev->driver_info->status(dev, urb);
  153. break;
  154. /* software-driven interface shutdown */
  155. case -ENOENT: /* urb killed */
  156. case -ESHUTDOWN: /* hardware gone */
  157. netif_dbg(dev, ifdown, dev->net,
  158. "intr shutdown, code %d\n", status);
  159. return;
  160. /* NOTE: not throttling like RX/TX, since this endpoint
  161. * already polls infrequently
  162. */
  163. default:
  164. netdev_dbg(dev->net, "intr status %d\n", status);
  165. break;
  166. }
  167. status = usb_submit_urb (urb, GFP_ATOMIC);
  168. if (status != 0)
  169. netif_err(dev, timer, dev->net,
  170. "intr resubmit --> %d\n", status);
  171. }
  172. static int init_status (struct usbnet *dev, struct usb_interface *intf)
  173. {
  174. char *buf = NULL;
  175. unsigned pipe = 0;
  176. unsigned maxp;
  177. unsigned period;
  178. if (!dev->driver_info->status)
  179. return 0;
  180. pipe = usb_rcvintpipe (dev->udev,
  181. dev->status->desc.bEndpointAddress
  182. & USB_ENDPOINT_NUMBER_MASK);
  183. maxp = usb_maxpacket (dev->udev, pipe, 0);
  184. /* avoid 1 msec chatter: min 8 msec poll rate */
  185. period = max ((int) dev->status->desc.bInterval,
  186. (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
  187. buf = kmalloc (maxp, GFP_KERNEL);
  188. if (buf) {
  189. dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
  190. if (!dev->interrupt) {
  191. kfree (buf);
  192. return -ENOMEM;
  193. } else {
  194. usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
  195. buf, maxp, intr_complete, dev, period);
  196. dev->interrupt->transfer_flags |= URB_FREE_BUFFER;
  197. dev_dbg(&intf->dev,
  198. "status ep%din, %d bytes period %d\n",
  199. usb_pipeendpoint(pipe), maxp, period);
  200. }
  201. }
  202. return 0;
  203. }
  204. /* Submit the interrupt URB if not previously submitted, increasing refcount */
  205. int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags)
  206. {
  207. int ret = 0;
  208. WARN_ON_ONCE(dev->interrupt == NULL);
  209. if (dev->interrupt) {
  210. mutex_lock(&dev->interrupt_mutex);
  211. if (++dev->interrupt_count == 1)
  212. ret = usb_submit_urb(dev->interrupt, mem_flags);
  213. dev_dbg(&dev->udev->dev, "incremented interrupt URB count to %d\n",
  214. dev->interrupt_count);
  215. mutex_unlock(&dev->interrupt_mutex);
  216. }
  217. return ret;
  218. }
  219. EXPORT_SYMBOL_GPL(usbnet_status_start);
  220. /* For resume; submit interrupt URB if previously submitted */
  221. static int __usbnet_status_start_force(struct usbnet *dev, gfp_t mem_flags)
  222. {
  223. int ret = 0;
  224. mutex_lock(&dev->interrupt_mutex);
  225. if (dev->interrupt_count) {
  226. ret = usb_submit_urb(dev->interrupt, mem_flags);
  227. dev_dbg(&dev->udev->dev,
  228. "submitted interrupt URB for resume\n");
  229. }
  230. mutex_unlock(&dev->interrupt_mutex);
  231. return ret;
  232. }
  233. /* Kill the interrupt URB if all submitters want it killed */
  234. void usbnet_status_stop(struct usbnet *dev)
  235. {
  236. if (dev->interrupt) {
  237. mutex_lock(&dev->interrupt_mutex);
  238. WARN_ON(dev->interrupt_count == 0);
  239. if (dev->interrupt_count && --dev->interrupt_count == 0)
  240. usb_kill_urb(dev->interrupt);
  241. dev_dbg(&dev->udev->dev,
  242. "decremented interrupt URB count to %d\n",
  243. dev->interrupt_count);
  244. mutex_unlock(&dev->interrupt_mutex);
  245. }
  246. }
  247. EXPORT_SYMBOL_GPL(usbnet_status_stop);
  248. /* For suspend; always kill interrupt URB */
  249. static void __usbnet_status_stop_force(struct usbnet *dev)
  250. {
  251. if (dev->interrupt) {
  252. mutex_lock(&dev->interrupt_mutex);
  253. usb_kill_urb(dev->interrupt);
  254. dev_dbg(&dev->udev->dev, "killed interrupt URB for suspend\n");
  255. mutex_unlock(&dev->interrupt_mutex);
  256. }
  257. }
  258. /* Passes this packet up the stack, updating its accounting.
  259. * Some link protocols batch packets, so their rx_fixup paths
  260. * can return clones as well as just modify the original skb.
  261. */
  262. void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
  263. {
  264. struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->stats64);
  265. unsigned long flags;
  266. int status;
  267. if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
  268. skb_queue_tail(&dev->rxq_pause, skb);
  269. return;
  270. }
  271. /* only update if unset to allow minidriver rx_fixup override */
  272. if (skb->protocol == 0)
  273. skb->protocol = eth_type_trans (skb, dev->net);
  274. flags = u64_stats_update_begin_irqsave(&stats64->syncp);
  275. stats64->rx_packets++;
  276. stats64->rx_bytes += skb->len;
  277. u64_stats_update_end_irqrestore(&stats64->syncp, flags);
  278. netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
  279. skb->len + sizeof (struct ethhdr), skb->protocol);
  280. memset (skb->cb, 0, sizeof (struct skb_data));
  281. if (skb_defer_rx_timestamp(skb))
  282. return;
  283. status = netif_rx (skb);
  284. if (status != NET_RX_SUCCESS)
  285. netif_dbg(dev, rx_err, dev->net,
  286. "netif_rx status %d\n", status);
  287. }
  288. EXPORT_SYMBOL_GPL(usbnet_skb_return);
  289. /* must be called if hard_mtu or rx_urb_size changed */
  290. void usbnet_update_max_qlen(struct usbnet *dev)
  291. {
  292. enum usb_device_speed speed = dev->udev->speed;
  293. if (!dev->rx_urb_size || !dev->hard_mtu)
  294. goto insanity;
  295. switch (speed) {
  296. case USB_SPEED_HIGH:
  297. dev->rx_qlen = MAX_QUEUE_MEMORY / dev->rx_urb_size;
  298. dev->tx_qlen = MAX_QUEUE_MEMORY / dev->hard_mtu;
  299. break;
  300. case USB_SPEED_SUPER:
  301. case USB_SPEED_SUPER_PLUS:
  302. /*
  303. * Not take default 5ms qlen for super speed HC to
  304. * save memory, and iperf tests show 2.5ms qlen can
  305. * work well
  306. */
  307. dev->rx_qlen = 5 * MAX_QUEUE_MEMORY / dev->rx_urb_size;
  308. dev->tx_qlen = 5 * MAX_QUEUE_MEMORY / dev->hard_mtu;
  309. break;
  310. default:
  311. insanity:
  312. dev->rx_qlen = dev->tx_qlen = 4;
  313. }
  314. }
  315. EXPORT_SYMBOL_GPL(usbnet_update_max_qlen);
  316. /*-------------------------------------------------------------------------
  317. *
  318. * Network Device Driver (peer link to "Host Device", from USB host)
  319. *
  320. *-------------------------------------------------------------------------*/
  321. int usbnet_change_mtu (struct net_device *net, int new_mtu)
  322. {
  323. struct usbnet *dev = netdev_priv(net);
  324. int ll_mtu = new_mtu + net->hard_header_len;
  325. int old_hard_mtu = dev->hard_mtu;
  326. int old_rx_urb_size = dev->rx_urb_size;
  327. // no second zero-length packet read wanted after mtu-sized packets
  328. if ((ll_mtu % dev->maxpacket) == 0)
  329. return -EDOM;
  330. net->mtu = new_mtu;
  331. dev->hard_mtu = net->mtu + net->hard_header_len;
  332. if (dev->rx_urb_size == old_hard_mtu) {
  333. dev->rx_urb_size = dev->hard_mtu;
  334. if (dev->rx_urb_size > old_rx_urb_size) {
  335. usbnet_pause_rx(dev);
  336. usbnet_unlink_rx_urbs(dev);
  337. usbnet_resume_rx(dev);
  338. }
  339. }
  340. /* max qlen depend on hard_mtu and rx_urb_size */
  341. usbnet_update_max_qlen(dev);
  342. return 0;
  343. }
  344. EXPORT_SYMBOL_GPL(usbnet_change_mtu);
  345. /* The caller must hold list->lock */
  346. static void __usbnet_queue_skb(struct sk_buff_head *list,
  347. struct sk_buff *newsk, enum skb_state state)
  348. {
  349. struct skb_data *entry = (struct skb_data *) newsk->cb;
  350. __skb_queue_tail(list, newsk);
  351. entry->state = state;
  352. }
  353. /*-------------------------------------------------------------------------*/
  354. /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
  355. * completion callbacks. 2.5 should have fixed those bugs...
  356. */
  357. static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
  358. struct sk_buff_head *list, enum skb_state state)
  359. {
  360. unsigned long flags;
  361. enum skb_state old_state;
  362. struct skb_data *entry = (struct skb_data *) skb->cb;
  363. spin_lock_irqsave(&list->lock, flags);
  364. old_state = entry->state;
  365. entry->state = state;
  366. __skb_unlink(skb, list);
  367. /* defer_bh() is never called with list == &dev->done.
  368. * spin_lock_nested() tells lockdep that it is OK to take
  369. * dev->done.lock here with list->lock held.
  370. */
  371. spin_lock_nested(&dev->done.lock, SINGLE_DEPTH_NESTING);
  372. __skb_queue_tail(&dev->done, skb);
  373. if (dev->done.qlen == 1)
  374. tasklet_schedule(&dev->bh);
  375. spin_unlock(&dev->done.lock);
  376. spin_unlock_irqrestore(&list->lock, flags);
  377. return old_state;
  378. }
  379. /* some work can't be done in tasklets, so we use keventd
  380. *
  381. * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
  382. * but tasklet_schedule() doesn't. hope the failure is rare.
  383. */
  384. void usbnet_defer_kevent (struct usbnet *dev, int work)
  385. {
  386. set_bit (work, &dev->flags);
  387. if (!schedule_work (&dev->kevent))
  388. netdev_dbg(dev->net, "kevent %d may have been dropped\n", work);
  389. else
  390. netdev_dbg(dev->net, "kevent %d scheduled\n", work);
  391. }
  392. EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
  393. /*-------------------------------------------------------------------------*/
  394. static void rx_complete (struct urb *urb);
  395. static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
  396. {
  397. struct sk_buff *skb;
  398. struct skb_data *entry;
  399. int retval = 0;
  400. unsigned long lockflags;
  401. size_t size = dev->rx_urb_size;
  402. /* prevent rx skb allocation when error ratio is high */
  403. if (test_bit(EVENT_RX_KILL, &dev->flags)) {
  404. usb_free_urb(urb);
  405. return -ENOLINK;
  406. }
  407. if (test_bit(EVENT_NO_IP_ALIGN, &dev->flags))
  408. skb = __netdev_alloc_skb(dev->net, size, flags);
  409. else
  410. skb = __netdev_alloc_skb_ip_align(dev->net, size, flags);
  411. if (!skb) {
  412. netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
  413. usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
  414. usb_free_urb (urb);
  415. return -ENOMEM;
  416. }
  417. entry = (struct skb_data *) skb->cb;
  418. entry->urb = urb;
  419. entry->dev = dev;
  420. entry->length = 0;
  421. usb_fill_bulk_urb (urb, dev->udev, dev->in,
  422. skb->data, size, rx_complete, skb);
  423. spin_lock_irqsave (&dev->rxq.lock, lockflags);
  424. if (netif_running (dev->net) &&
  425. netif_device_present (dev->net) &&
  426. test_bit(EVENT_DEV_OPEN, &dev->flags) &&
  427. !test_bit (EVENT_RX_HALT, &dev->flags) &&
  428. !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
  429. switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
  430. case -EPIPE:
  431. usbnet_defer_kevent (dev, EVENT_RX_HALT);
  432. break;
  433. case -ENOMEM:
  434. usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
  435. break;
  436. case -ENODEV:
  437. netif_dbg(dev, ifdown, dev->net, "device gone\n");
  438. netif_device_detach (dev->net);
  439. break;
  440. case -EHOSTUNREACH:
  441. retval = -ENOLINK;
  442. break;
  443. default:
  444. netif_dbg(dev, rx_err, dev->net,
  445. "rx submit, %d\n", retval);
  446. tasklet_schedule (&dev->bh);
  447. break;
  448. case 0:
  449. __usbnet_queue_skb(&dev->rxq, skb, rx_start);
  450. }
  451. } else {
  452. netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
  453. retval = -ENOLINK;
  454. }
  455. spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
  456. if (retval) {
  457. dev_kfree_skb_any (skb);
  458. usb_free_urb (urb);
  459. }
  460. return retval;
  461. }
  462. /*-------------------------------------------------------------------------*/
  463. static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
  464. {
  465. if (dev->driver_info->rx_fixup &&
  466. !dev->driver_info->rx_fixup (dev, skb)) {
  467. /* With RX_ASSEMBLE, rx_fixup() must update counters */
  468. if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE))
  469. dev->net->stats.rx_errors++;
  470. goto done;
  471. }
  472. // else network stack removes extra byte if we forced a short packet
  473. /* all data was already cloned from skb inside the driver */
  474. if (dev->driver_info->flags & FLAG_MULTI_PACKET)
  475. goto done;
  476. if (skb->len < ETH_HLEN) {
  477. dev->net->stats.rx_errors++;
  478. dev->net->stats.rx_length_errors++;
  479. netif_dbg(dev, rx_err, dev->net, "rx length %d\n", skb->len);
  480. } else {
  481. usbnet_skb_return(dev, skb);
  482. return;
  483. }
  484. done:
  485. skb_queue_tail(&dev->done, skb);
  486. }
  487. /*-------------------------------------------------------------------------*/
  488. static void rx_complete (struct urb *urb)
  489. {
  490. struct sk_buff *skb = (struct sk_buff *) urb->context;
  491. struct skb_data *entry = (struct skb_data *) skb->cb;
  492. struct usbnet *dev = entry->dev;
  493. int urb_status = urb->status;
  494. enum skb_state state;
  495. skb_put (skb, urb->actual_length);
  496. state = rx_done;
  497. entry->urb = NULL;
  498. switch (urb_status) {
  499. /* success */
  500. case 0:
  501. break;
  502. /* stalls need manual reset. this is rare ... except that
  503. * when going through USB 2.0 TTs, unplug appears this way.
  504. * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
  505. * storm, recovering as needed.
  506. */
  507. case -EPIPE:
  508. dev->net->stats.rx_errors++;
  509. usbnet_defer_kevent (dev, EVENT_RX_HALT);
  510. // FALLTHROUGH
  511. /* software-driven interface shutdown */
  512. case -ECONNRESET: /* async unlink */
  513. case -ESHUTDOWN: /* hardware gone */
  514. netif_dbg(dev, ifdown, dev->net,
  515. "rx shutdown, code %d\n", urb_status);
  516. goto block;
  517. /* we get controller i/o faults during hub_wq disconnect() delays.
  518. * throttle down resubmits, to avoid log floods; just temporarily,
  519. * so we still recover when the fault isn't a hub_wq delay.
  520. */
  521. case -EPROTO:
  522. case -ETIME:
  523. case -EILSEQ:
  524. dev->net->stats.rx_errors++;
  525. if (!timer_pending (&dev->delay)) {
  526. mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
  527. netif_dbg(dev, link, dev->net,
  528. "rx throttle %d\n", urb_status);
  529. }
  530. block:
  531. state = rx_cleanup;
  532. entry->urb = urb;
  533. urb = NULL;
  534. break;
  535. /* data overrun ... flush fifo? */
  536. case -EOVERFLOW:
  537. dev->net->stats.rx_over_errors++;
  538. // FALLTHROUGH
  539. default:
  540. state = rx_cleanup;
  541. dev->net->stats.rx_errors++;
  542. netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
  543. break;
  544. }
  545. /* stop rx if packet error rate is high */
  546. if (++dev->pkt_cnt > 30) {
  547. dev->pkt_cnt = 0;
  548. dev->pkt_err = 0;
  549. } else {
  550. if (state == rx_cleanup)
  551. dev->pkt_err++;
  552. if (dev->pkt_err > 20)
  553. set_bit(EVENT_RX_KILL, &dev->flags);
  554. }
  555. state = defer_bh(dev, skb, &dev->rxq, state);
  556. if (urb) {
  557. if (netif_running (dev->net) &&
  558. !test_bit (EVENT_RX_HALT, &dev->flags) &&
  559. state != unlink_start) {
  560. rx_submit (dev, urb, GFP_ATOMIC);
  561. usb_mark_last_busy(dev->udev);
  562. return;
  563. }
  564. usb_free_urb (urb);
  565. }
  566. netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
  567. }
  568. /*-------------------------------------------------------------------------*/
  569. void usbnet_pause_rx(struct usbnet *dev)
  570. {
  571. set_bit(EVENT_RX_PAUSED, &dev->flags);
  572. netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
  573. }
  574. EXPORT_SYMBOL_GPL(usbnet_pause_rx);
  575. void usbnet_resume_rx(struct usbnet *dev)
  576. {
  577. struct sk_buff *skb;
  578. int num = 0;
  579. clear_bit(EVENT_RX_PAUSED, &dev->flags);
  580. while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
  581. usbnet_skb_return(dev, skb);
  582. num++;
  583. }
  584. tasklet_schedule(&dev->bh);
  585. netif_dbg(dev, rx_status, dev->net,
  586. "paused rx queue disabled, %d skbs requeued\n", num);
  587. }
  588. EXPORT_SYMBOL_GPL(usbnet_resume_rx);
  589. void usbnet_purge_paused_rxq(struct usbnet *dev)
  590. {
  591. skb_queue_purge(&dev->rxq_pause);
  592. }
  593. EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
  594. /*-------------------------------------------------------------------------*/
  595. // unlink pending rx/tx; completion handlers do all other cleanup
  596. static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
  597. {
  598. unsigned long flags;
  599. struct sk_buff *skb;
  600. int count = 0;
  601. spin_lock_irqsave (&q->lock, flags);
  602. while (!skb_queue_empty(q)) {
  603. struct skb_data *entry;
  604. struct urb *urb;
  605. int retval;
  606. skb_queue_walk(q, skb) {
  607. entry = (struct skb_data *) skb->cb;
  608. if (entry->state != unlink_start)
  609. goto found;
  610. }
  611. break;
  612. found:
  613. entry->state = unlink_start;
  614. urb = entry->urb;
  615. /*
  616. * Get reference count of the URB to avoid it to be
  617. * freed during usb_unlink_urb, which may trigger
  618. * use-after-free problem inside usb_unlink_urb since
  619. * usb_unlink_urb is always racing with .complete
  620. * handler(include defer_bh).
  621. */
  622. usb_get_urb(urb);
  623. spin_unlock_irqrestore(&q->lock, flags);
  624. // during some PM-driven resume scenarios,
  625. // these (async) unlinks complete immediately
  626. retval = usb_unlink_urb (urb);
  627. if (retval != -EINPROGRESS && retval != 0)
  628. netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
  629. else
  630. count++;
  631. usb_put_urb(urb);
  632. spin_lock_irqsave(&q->lock, flags);
  633. }
  634. spin_unlock_irqrestore (&q->lock, flags);
  635. return count;
  636. }
  637. // Flush all pending rx urbs
  638. // minidrivers may need to do this when the MTU changes
  639. void usbnet_unlink_rx_urbs(struct usbnet *dev)
  640. {
  641. if (netif_running(dev->net)) {
  642. (void) unlink_urbs (dev, &dev->rxq);
  643. tasklet_schedule(&dev->bh);
  644. }
  645. }
  646. EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
  647. /*-------------------------------------------------------------------------*/
  648. static void wait_skb_queue_empty(struct sk_buff_head *q)
  649. {
  650. unsigned long flags;
  651. spin_lock_irqsave(&q->lock, flags);
  652. while (!skb_queue_empty(q)) {
  653. spin_unlock_irqrestore(&q->lock, flags);
  654. schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
  655. set_current_state(TASK_UNINTERRUPTIBLE);
  656. spin_lock_irqsave(&q->lock, flags);
  657. }
  658. spin_unlock_irqrestore(&q->lock, flags);
  659. }
  660. // precondition: never called in_interrupt
  661. static void usbnet_terminate_urbs(struct usbnet *dev)
  662. {
  663. DECLARE_WAITQUEUE(wait, current);
  664. int temp;
  665. /* ensure there are no more active urbs */
  666. add_wait_queue(&dev->wait, &wait);
  667. set_current_state(TASK_UNINTERRUPTIBLE);
  668. temp = unlink_urbs(dev, &dev->txq) +
  669. unlink_urbs(dev, &dev->rxq);
  670. /* maybe wait for deletions to finish. */
  671. wait_skb_queue_empty(&dev->rxq);
  672. wait_skb_queue_empty(&dev->txq);
  673. wait_skb_queue_empty(&dev->done);
  674. netif_dbg(dev, ifdown, dev->net,
  675. "waited for %d urb completions\n", temp);
  676. set_current_state(TASK_RUNNING);
  677. remove_wait_queue(&dev->wait, &wait);
  678. }
  679. int usbnet_stop (struct net_device *net)
  680. {
  681. struct usbnet *dev = netdev_priv(net);
  682. const struct driver_info *info = dev->driver_info;
  683. int retval, pm, mpn;
  684. clear_bit(EVENT_DEV_OPEN, &dev->flags);
  685. netif_stop_queue (net);
  686. netif_info(dev, ifdown, dev->net,
  687. "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
  688. net->stats.rx_packets, net->stats.tx_packets,
  689. net->stats.rx_errors, net->stats.tx_errors);
  690. /* to not race resume */
  691. pm = usb_autopm_get_interface(dev->intf);
  692. /* allow minidriver to stop correctly (wireless devices to turn off
  693. * radio etc) */
  694. if (info->stop) {
  695. retval = info->stop(dev);
  696. if (retval < 0)
  697. netif_info(dev, ifdown, dev->net,
  698. "stop fail (%d) usbnet usb-%s-%s, %s\n",
  699. retval,
  700. dev->udev->bus->bus_name, dev->udev->devpath,
  701. info->description);
  702. }
  703. if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
  704. usbnet_terminate_urbs(dev);
  705. usbnet_status_stop(dev);
  706. usbnet_purge_paused_rxq(dev);
  707. mpn = !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
  708. /* deferred work (task, timer, softirq) must also stop.
  709. * can't flush_scheduled_work() until we drop rtnl (later),
  710. * else workers could deadlock; so make workers a NOP.
  711. */
  712. dev->flags = 0;
  713. del_timer_sync (&dev->delay);
  714. tasklet_kill (&dev->bh);
  715. if (!pm)
  716. usb_autopm_put_interface(dev->intf);
  717. if (info->manage_power && mpn)
  718. info->manage_power(dev, 0);
  719. else
  720. usb_autopm_put_interface(dev->intf);
  721. return 0;
  722. }
  723. EXPORT_SYMBOL_GPL(usbnet_stop);
  724. /*-------------------------------------------------------------------------*/
  725. // posts reads, and enables write queuing
  726. // precondition: never called in_interrupt
  727. int usbnet_open (struct net_device *net)
  728. {
  729. struct usbnet *dev = netdev_priv(net);
  730. int retval;
  731. const struct driver_info *info = dev->driver_info;
  732. if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
  733. netif_info(dev, ifup, dev->net,
  734. "resumption fail (%d) usbnet usb-%s-%s, %s\n",
  735. retval,
  736. dev->udev->bus->bus_name,
  737. dev->udev->devpath,
  738. info->description);
  739. goto done_nopm;
  740. }
  741. // put into "known safe" state
  742. if (info->reset && (retval = info->reset (dev)) < 0) {
  743. netif_info(dev, ifup, dev->net,
  744. "open reset fail (%d) usbnet usb-%s-%s, %s\n",
  745. retval,
  746. dev->udev->bus->bus_name,
  747. dev->udev->devpath,
  748. info->description);
  749. goto done;
  750. }
  751. /* hard_mtu or rx_urb_size may change in reset() */
  752. usbnet_update_max_qlen(dev);
  753. // insist peer be connected
  754. if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
  755. netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
  756. goto done;
  757. }
  758. /* start any status interrupt transfer */
  759. if (dev->interrupt) {
  760. retval = usbnet_status_start(dev, GFP_KERNEL);
  761. if (retval < 0) {
  762. netif_err(dev, ifup, dev->net,
  763. "intr submit %d\n", retval);
  764. goto done;
  765. }
  766. }
  767. set_bit(EVENT_DEV_OPEN, &dev->flags);
  768. netif_start_queue (net);
  769. netif_info(dev, ifup, dev->net,
  770. "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
  771. (int)RX_QLEN(dev), (int)TX_QLEN(dev),
  772. dev->net->mtu,
  773. (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
  774. (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
  775. (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
  776. (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
  777. (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
  778. "simple");
  779. /* reset rx error state */
  780. dev->pkt_cnt = 0;
  781. dev->pkt_err = 0;
  782. clear_bit(EVENT_RX_KILL, &dev->flags);
  783. // delay posting reads until we're fully open
  784. tasklet_schedule (&dev->bh);
  785. if (info->manage_power) {
  786. retval = info->manage_power(dev, 1);
  787. if (retval < 0) {
  788. retval = 0;
  789. set_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
  790. } else {
  791. usb_autopm_put_interface(dev->intf);
  792. }
  793. }
  794. return retval;
  795. done:
  796. usb_autopm_put_interface(dev->intf);
  797. done_nopm:
  798. return retval;
  799. }
  800. EXPORT_SYMBOL_GPL(usbnet_open);
  801. /*-------------------------------------------------------------------------*/
  802. /* ethtool methods; minidrivers may need to add some more, but
  803. * they'll probably want to use this base set.
  804. */
  805. int usbnet_get_link_ksettings(struct net_device *net,
  806. struct ethtool_link_ksettings *cmd)
  807. {
  808. struct usbnet *dev = netdev_priv(net);
  809. if (!dev->mii.mdio_read)
  810. return -EOPNOTSUPP;
  811. mii_ethtool_get_link_ksettings(&dev->mii, cmd);
  812. return 0;
  813. }
  814. EXPORT_SYMBOL_GPL(usbnet_get_link_ksettings);
  815. int usbnet_set_link_ksettings(struct net_device *net,
  816. const struct ethtool_link_ksettings *cmd)
  817. {
  818. struct usbnet *dev = netdev_priv(net);
  819. int retval;
  820. if (!dev->mii.mdio_write)
  821. return -EOPNOTSUPP;
  822. retval = mii_ethtool_set_link_ksettings(&dev->mii, cmd);
  823. /* link speed/duplex might have changed */
  824. if (dev->driver_info->link_reset)
  825. dev->driver_info->link_reset(dev);
  826. /* hard_mtu or rx_urb_size may change in link_reset() */
  827. usbnet_update_max_qlen(dev);
  828. return retval;
  829. }
  830. EXPORT_SYMBOL_GPL(usbnet_set_link_ksettings);
  831. void usbnet_get_stats64(struct net_device *net, struct rtnl_link_stats64 *stats)
  832. {
  833. struct usbnet *dev = netdev_priv(net);
  834. unsigned int start;
  835. int cpu;
  836. netdev_stats_to_stats64(stats, &net->stats);
  837. for_each_possible_cpu(cpu) {
  838. struct pcpu_sw_netstats *stats64;
  839. u64 rx_packets, rx_bytes;
  840. u64 tx_packets, tx_bytes;
  841. stats64 = per_cpu_ptr(dev->stats64, cpu);
  842. do {
  843. start = u64_stats_fetch_begin_irq(&stats64->syncp);
  844. rx_packets = stats64->rx_packets;
  845. rx_bytes = stats64->rx_bytes;
  846. tx_packets = stats64->tx_packets;
  847. tx_bytes = stats64->tx_bytes;
  848. } while (u64_stats_fetch_retry_irq(&stats64->syncp, start));
  849. stats->rx_packets += rx_packets;
  850. stats->rx_bytes += rx_bytes;
  851. stats->tx_packets += tx_packets;
  852. stats->tx_bytes += tx_bytes;
  853. }
  854. }
  855. EXPORT_SYMBOL_GPL(usbnet_get_stats64);
  856. u32 usbnet_get_link (struct net_device *net)
  857. {
  858. struct usbnet *dev = netdev_priv(net);
  859. /* If a check_connect is defined, return its result */
  860. if (dev->driver_info->check_connect)
  861. return dev->driver_info->check_connect (dev) == 0;
  862. /* if the device has mii operations, use those */
  863. if (dev->mii.mdio_read)
  864. return mii_link_ok(&dev->mii);
  865. /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
  866. return ethtool_op_get_link(net);
  867. }
  868. EXPORT_SYMBOL_GPL(usbnet_get_link);
  869. int usbnet_nway_reset(struct net_device *net)
  870. {
  871. struct usbnet *dev = netdev_priv(net);
  872. if (!dev->mii.mdio_write)
  873. return -EOPNOTSUPP;
  874. return mii_nway_restart(&dev->mii);
  875. }
  876. EXPORT_SYMBOL_GPL(usbnet_nway_reset);
  877. void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
  878. {
  879. struct usbnet *dev = netdev_priv(net);
  880. strlcpy (info->driver, dev->driver_name, sizeof info->driver);
  881. strlcpy (info->version, DRIVER_VERSION, sizeof info->version);
  882. strlcpy (info->fw_version, dev->driver_info->description,
  883. sizeof info->fw_version);
  884. usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
  885. }
  886. EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
  887. u32 usbnet_get_msglevel (struct net_device *net)
  888. {
  889. struct usbnet *dev = netdev_priv(net);
  890. return dev->msg_enable;
  891. }
  892. EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
  893. void usbnet_set_msglevel (struct net_device *net, u32 level)
  894. {
  895. struct usbnet *dev = netdev_priv(net);
  896. dev->msg_enable = level;
  897. }
  898. EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
  899. /* drivers may override default ethtool_ops in their bind() routine */
  900. static const struct ethtool_ops usbnet_ethtool_ops = {
  901. .get_link = usbnet_get_link,
  902. .nway_reset = usbnet_nway_reset,
  903. .get_drvinfo = usbnet_get_drvinfo,
  904. .get_msglevel = usbnet_get_msglevel,
  905. .set_msglevel = usbnet_set_msglevel,
  906. .get_ts_info = ethtool_op_get_ts_info,
  907. .get_link_ksettings = usbnet_get_link_ksettings,
  908. .set_link_ksettings = usbnet_set_link_ksettings,
  909. };
  910. /*-------------------------------------------------------------------------*/
  911. static void __handle_link_change(struct usbnet *dev)
  912. {
  913. if (!test_bit(EVENT_DEV_OPEN, &dev->flags))
  914. return;
  915. if (!netif_carrier_ok(dev->net)) {
  916. /* kill URBs for reading packets to save bus bandwidth */
  917. unlink_urbs(dev, &dev->rxq);
  918. /*
  919. * tx_timeout will unlink URBs for sending packets and
  920. * tx queue is stopped by netcore after link becomes off
  921. */
  922. } else {
  923. /* submitting URBs for reading packets */
  924. tasklet_schedule(&dev->bh);
  925. }
  926. /* hard_mtu or rx_urb_size may change during link change */
  927. usbnet_update_max_qlen(dev);
  928. clear_bit(EVENT_LINK_CHANGE, &dev->flags);
  929. }
  930. static void usbnet_set_rx_mode(struct net_device *net)
  931. {
  932. struct usbnet *dev = netdev_priv(net);
  933. usbnet_defer_kevent(dev, EVENT_SET_RX_MODE);
  934. }
  935. static void __handle_set_rx_mode(struct usbnet *dev)
  936. {
  937. if (dev->driver_info->set_rx_mode)
  938. (dev->driver_info->set_rx_mode)(dev);
  939. clear_bit(EVENT_SET_RX_MODE, &dev->flags);
  940. }
  941. /* work that cannot be done in interrupt context uses keventd.
  942. *
  943. * NOTE: with 2.5 we could do more of this using completion callbacks,
  944. * especially now that control transfers can be queued.
  945. */
  946. static void
  947. usbnet_deferred_kevent (struct work_struct *work)
  948. {
  949. struct usbnet *dev =
  950. container_of(work, struct usbnet, kevent);
  951. int status;
  952. /* usb_clear_halt() needs a thread context */
  953. if (test_bit (EVENT_TX_HALT, &dev->flags)) {
  954. unlink_urbs (dev, &dev->txq);
  955. status = usb_autopm_get_interface(dev->intf);
  956. if (status < 0)
  957. goto fail_pipe;
  958. status = usb_clear_halt (dev->udev, dev->out);
  959. usb_autopm_put_interface(dev->intf);
  960. if (status < 0 &&
  961. status != -EPIPE &&
  962. status != -ESHUTDOWN) {
  963. if (netif_msg_tx_err (dev))
  964. fail_pipe:
  965. netdev_err(dev->net, "can't clear tx halt, status %d\n",
  966. status);
  967. } else {
  968. clear_bit (EVENT_TX_HALT, &dev->flags);
  969. if (status != -ESHUTDOWN)
  970. netif_wake_queue (dev->net);
  971. }
  972. }
  973. if (test_bit (EVENT_RX_HALT, &dev->flags)) {
  974. unlink_urbs (dev, &dev->rxq);
  975. status = usb_autopm_get_interface(dev->intf);
  976. if (status < 0)
  977. goto fail_halt;
  978. status = usb_clear_halt (dev->udev, dev->in);
  979. usb_autopm_put_interface(dev->intf);
  980. if (status < 0 &&
  981. status != -EPIPE &&
  982. status != -ESHUTDOWN) {
  983. if (netif_msg_rx_err (dev))
  984. fail_halt:
  985. netdev_err(dev->net, "can't clear rx halt, status %d\n",
  986. status);
  987. } else {
  988. clear_bit (EVENT_RX_HALT, &dev->flags);
  989. tasklet_schedule (&dev->bh);
  990. }
  991. }
  992. /* tasklet could resubmit itself forever if memory is tight */
  993. if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
  994. struct urb *urb = NULL;
  995. int resched = 1;
  996. if (netif_running (dev->net))
  997. urb = usb_alloc_urb (0, GFP_KERNEL);
  998. else
  999. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  1000. if (urb != NULL) {
  1001. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  1002. status = usb_autopm_get_interface(dev->intf);
  1003. if (status < 0) {
  1004. usb_free_urb(urb);
  1005. goto fail_lowmem;
  1006. }
  1007. if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
  1008. resched = 0;
  1009. usb_autopm_put_interface(dev->intf);
  1010. fail_lowmem:
  1011. if (resched)
  1012. tasklet_schedule (&dev->bh);
  1013. }
  1014. }
  1015. if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
  1016. const struct driver_info *info = dev->driver_info;
  1017. int retval = 0;
  1018. clear_bit (EVENT_LINK_RESET, &dev->flags);
  1019. status = usb_autopm_get_interface(dev->intf);
  1020. if (status < 0)
  1021. goto skip_reset;
  1022. if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
  1023. usb_autopm_put_interface(dev->intf);
  1024. skip_reset:
  1025. netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
  1026. retval,
  1027. dev->udev->bus->bus_name,
  1028. dev->udev->devpath,
  1029. info->description);
  1030. } else {
  1031. usb_autopm_put_interface(dev->intf);
  1032. }
  1033. /* handle link change from link resetting */
  1034. __handle_link_change(dev);
  1035. }
  1036. if (test_bit (EVENT_LINK_CHANGE, &dev->flags))
  1037. __handle_link_change(dev);
  1038. if (test_bit (EVENT_SET_RX_MODE, &dev->flags))
  1039. __handle_set_rx_mode(dev);
  1040. if (dev->flags)
  1041. netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
  1042. }
  1043. /*-------------------------------------------------------------------------*/
  1044. static void tx_complete (struct urb *urb)
  1045. {
  1046. struct sk_buff *skb = (struct sk_buff *) urb->context;
  1047. struct skb_data *entry = (struct skb_data *) skb->cb;
  1048. struct usbnet *dev = entry->dev;
  1049. if (urb->status == 0) {
  1050. struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->stats64);
  1051. unsigned long flags;
  1052. flags = u64_stats_update_begin_irqsave(&stats64->syncp);
  1053. stats64->tx_packets += entry->packets;
  1054. stats64->tx_bytes += entry->length;
  1055. u64_stats_update_end_irqrestore(&stats64->syncp, flags);
  1056. } else {
  1057. dev->net->stats.tx_errors++;
  1058. switch (urb->status) {
  1059. case -EPIPE:
  1060. usbnet_defer_kevent (dev, EVENT_TX_HALT);
  1061. break;
  1062. /* software-driven interface shutdown */
  1063. case -ECONNRESET: // async unlink
  1064. case -ESHUTDOWN: // hardware gone
  1065. break;
  1066. /* like rx, tx gets controller i/o faults during hub_wq
  1067. * delays and so it uses the same throttling mechanism.
  1068. */
  1069. case -EPROTO:
  1070. case -ETIME:
  1071. case -EILSEQ:
  1072. usb_mark_last_busy(dev->udev);
  1073. if (!timer_pending (&dev->delay)) {
  1074. mod_timer (&dev->delay,
  1075. jiffies + THROTTLE_JIFFIES);
  1076. netif_dbg(dev, link, dev->net,
  1077. "tx throttle %d\n", urb->status);
  1078. }
  1079. netif_stop_queue (dev->net);
  1080. break;
  1081. default:
  1082. netif_dbg(dev, tx_err, dev->net,
  1083. "tx err %d\n", entry->urb->status);
  1084. break;
  1085. }
  1086. }
  1087. usb_autopm_put_interface_async(dev->intf);
  1088. (void) defer_bh(dev, skb, &dev->txq, tx_done);
  1089. }
  1090. /*-------------------------------------------------------------------------*/
  1091. void usbnet_tx_timeout (struct net_device *net)
  1092. {
  1093. struct usbnet *dev = netdev_priv(net);
  1094. unlink_urbs (dev, &dev->txq);
  1095. tasklet_schedule (&dev->bh);
  1096. /* this needs to be handled individually because the generic layer
  1097. * doesn't know what is sufficient and could not restore private
  1098. * information if a remedy of an unconditional reset were used.
  1099. */
  1100. if (dev->driver_info->recover)
  1101. (dev->driver_info->recover)(dev);
  1102. }
  1103. EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
  1104. /*-------------------------------------------------------------------------*/
  1105. static int build_dma_sg(const struct sk_buff *skb, struct urb *urb)
  1106. {
  1107. unsigned num_sgs, total_len = 0;
  1108. int i, s = 0;
  1109. num_sgs = skb_shinfo(skb)->nr_frags + 1;
  1110. if (num_sgs == 1)
  1111. return 0;
  1112. /* reserve one for zero packet */
  1113. urb->sg = kmalloc_array(num_sgs + 1, sizeof(struct scatterlist),
  1114. GFP_ATOMIC);
  1115. if (!urb->sg)
  1116. return -ENOMEM;
  1117. urb->num_sgs = num_sgs;
  1118. sg_init_table(urb->sg, urb->num_sgs + 1);
  1119. sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb));
  1120. total_len += skb_headlen(skb);
  1121. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1122. skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  1123. total_len += skb_frag_size(f);
  1124. sg_set_page(&urb->sg[i + s], skb_frag_page(f), skb_frag_size(f),
  1125. skb_frag_off(f));
  1126. }
  1127. urb->transfer_buffer_length = total_len;
  1128. return 1;
  1129. }
  1130. netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
  1131. struct net_device *net)
  1132. {
  1133. struct usbnet *dev = netdev_priv(net);
  1134. unsigned int length;
  1135. struct urb *urb = NULL;
  1136. struct skb_data *entry;
  1137. const struct driver_info *info = dev->driver_info;
  1138. unsigned long flags;
  1139. int retval;
  1140. if (skb)
  1141. skb_tx_timestamp(skb);
  1142. // some devices want funky USB-level framing, for
  1143. // win32 driver (usually) and/or hardware quirks
  1144. if (info->tx_fixup) {
  1145. skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
  1146. if (!skb) {
  1147. /* packet collected; minidriver waiting for more */
  1148. if (info->flags & FLAG_MULTI_PACKET)
  1149. goto not_drop;
  1150. netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
  1151. goto drop;
  1152. }
  1153. }
  1154. if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
  1155. netif_dbg(dev, tx_err, dev->net, "no urb\n");
  1156. goto drop;
  1157. }
  1158. entry = (struct skb_data *) skb->cb;
  1159. entry->urb = urb;
  1160. entry->dev = dev;
  1161. usb_fill_bulk_urb (urb, dev->udev, dev->out,
  1162. skb->data, skb->len, tx_complete, skb);
  1163. if (dev->can_dma_sg) {
  1164. if (build_dma_sg(skb, urb) < 0)
  1165. goto drop;
  1166. }
  1167. length = urb->transfer_buffer_length;
  1168. /* don't assume the hardware handles USB_ZERO_PACKET
  1169. * NOTE: strictly conforming cdc-ether devices should expect
  1170. * the ZLP here, but ignore the one-byte packet.
  1171. * NOTE2: CDC NCM specification is different from CDC ECM when
  1172. * handling ZLP/short packets, so cdc_ncm driver will make short
  1173. * packet itself if needed.
  1174. */
  1175. if (length % dev->maxpacket == 0) {
  1176. if (!(info->flags & FLAG_SEND_ZLP)) {
  1177. if (!(info->flags & FLAG_MULTI_PACKET)) {
  1178. length++;
  1179. if (skb_tailroom(skb) && !urb->num_sgs) {
  1180. skb->data[skb->len] = 0;
  1181. __skb_put(skb, 1);
  1182. } else if (urb->num_sgs)
  1183. sg_set_buf(&urb->sg[urb->num_sgs++],
  1184. dev->padding_pkt, 1);
  1185. }
  1186. } else
  1187. urb->transfer_flags |= URB_ZERO_PACKET;
  1188. }
  1189. urb->transfer_buffer_length = length;
  1190. if (info->flags & FLAG_MULTI_PACKET) {
  1191. /* Driver has set number of packets and a length delta.
  1192. * Calculate the complete length and ensure that it's
  1193. * positive.
  1194. */
  1195. entry->length += length;
  1196. if (WARN_ON_ONCE(entry->length <= 0))
  1197. entry->length = length;
  1198. } else {
  1199. usbnet_set_skb_tx_stats(skb, 1, length);
  1200. }
  1201. spin_lock_irqsave(&dev->txq.lock, flags);
  1202. retval = usb_autopm_get_interface_async(dev->intf);
  1203. if (retval < 0) {
  1204. spin_unlock_irqrestore(&dev->txq.lock, flags);
  1205. goto drop;
  1206. }
  1207. if (netif_queue_stopped(net)) {
  1208. usb_autopm_put_interface_async(dev->intf);
  1209. spin_unlock_irqrestore(&dev->txq.lock, flags);
  1210. goto drop;
  1211. }
  1212. #ifdef CONFIG_PM
  1213. /* if this triggers the device is still a sleep */
  1214. if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
  1215. /* transmission will be done in resume */
  1216. usb_anchor_urb(urb, &dev->deferred);
  1217. /* no use to process more packets */
  1218. netif_stop_queue(net);
  1219. usb_put_urb(urb);
  1220. spin_unlock_irqrestore(&dev->txq.lock, flags);
  1221. netdev_dbg(dev->net, "Delaying transmission for resumption\n");
  1222. goto deferred;
  1223. }
  1224. #endif
  1225. switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
  1226. case -EPIPE:
  1227. netif_stop_queue (net);
  1228. usbnet_defer_kevent (dev, EVENT_TX_HALT);
  1229. usb_autopm_put_interface_async(dev->intf);
  1230. break;
  1231. default:
  1232. usb_autopm_put_interface_async(dev->intf);
  1233. netif_dbg(dev, tx_err, dev->net,
  1234. "tx: submit urb err %d\n", retval);
  1235. break;
  1236. case 0:
  1237. netif_trans_update(net);
  1238. __usbnet_queue_skb(&dev->txq, skb, tx_start);
  1239. if (dev->txq.qlen >= TX_QLEN (dev))
  1240. netif_stop_queue (net);
  1241. }
  1242. spin_unlock_irqrestore (&dev->txq.lock, flags);
  1243. if (retval) {
  1244. netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
  1245. drop:
  1246. dev->net->stats.tx_dropped++;
  1247. not_drop:
  1248. if (skb)
  1249. dev_kfree_skb_any (skb);
  1250. if (urb) {
  1251. kfree(urb->sg);
  1252. usb_free_urb(urb);
  1253. }
  1254. } else
  1255. netif_dbg(dev, tx_queued, dev->net,
  1256. "> tx, len %u, type 0x%x\n", length, skb->protocol);
  1257. #ifdef CONFIG_PM
  1258. deferred:
  1259. #endif
  1260. return NETDEV_TX_OK;
  1261. }
  1262. EXPORT_SYMBOL_GPL(usbnet_start_xmit);
  1263. static int rx_alloc_submit(struct usbnet *dev, gfp_t flags)
  1264. {
  1265. struct urb *urb;
  1266. int i;
  1267. int ret = 0;
  1268. /* don't refill the queue all at once */
  1269. for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) {
  1270. urb = usb_alloc_urb(0, flags);
  1271. if (urb != NULL) {
  1272. ret = rx_submit(dev, urb, flags);
  1273. if (ret)
  1274. goto err;
  1275. } else {
  1276. ret = -ENOMEM;
  1277. goto err;
  1278. }
  1279. }
  1280. err:
  1281. return ret;
  1282. }
  1283. /*-------------------------------------------------------------------------*/
  1284. // tasklet (work deferred from completions, in_irq) or timer
  1285. static void usbnet_bh (struct timer_list *t)
  1286. {
  1287. struct usbnet *dev = from_timer(dev, t, delay);
  1288. struct sk_buff *skb;
  1289. struct skb_data *entry;
  1290. while ((skb = skb_dequeue (&dev->done))) {
  1291. entry = (struct skb_data *) skb->cb;
  1292. switch (entry->state) {
  1293. case rx_done:
  1294. entry->state = rx_cleanup;
  1295. rx_process (dev, skb);
  1296. continue;
  1297. case tx_done:
  1298. kfree(entry->urb->sg);
  1299. /* fall through */
  1300. case rx_cleanup:
  1301. usb_free_urb (entry->urb);
  1302. dev_kfree_skb (skb);
  1303. continue;
  1304. default:
  1305. netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
  1306. }
  1307. }
  1308. /* restart RX again after disabling due to high error rate */
  1309. clear_bit(EVENT_RX_KILL, &dev->flags);
  1310. /* waiting for all pending urbs to complete?
  1311. * only then can we forgo submitting anew
  1312. */
  1313. if (waitqueue_active(&dev->wait)) {
  1314. if (dev->txq.qlen + dev->rxq.qlen + dev->done.qlen == 0)
  1315. wake_up_all(&dev->wait);
  1316. // or are we maybe short a few urbs?
  1317. } else if (netif_running (dev->net) &&
  1318. netif_device_present (dev->net) &&
  1319. netif_carrier_ok(dev->net) &&
  1320. !timer_pending(&dev->delay) &&
  1321. !test_bit(EVENT_RX_PAUSED, &dev->flags) &&
  1322. !test_bit(EVENT_RX_HALT, &dev->flags)) {
  1323. int temp = dev->rxq.qlen;
  1324. if (temp < RX_QLEN(dev)) {
  1325. if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK)
  1326. return;
  1327. if (temp != dev->rxq.qlen)
  1328. netif_dbg(dev, link, dev->net,
  1329. "rxqlen %d --> %d\n",
  1330. temp, dev->rxq.qlen);
  1331. if (dev->rxq.qlen < RX_QLEN(dev))
  1332. tasklet_schedule (&dev->bh);
  1333. }
  1334. if (dev->txq.qlen < TX_QLEN (dev))
  1335. netif_wake_queue (dev->net);
  1336. }
  1337. }
  1338. /*-------------------------------------------------------------------------
  1339. *
  1340. * USB Device Driver support
  1341. *
  1342. *-------------------------------------------------------------------------*/
  1343. // precondition: never called in_interrupt
  1344. void usbnet_disconnect (struct usb_interface *intf)
  1345. {
  1346. struct usbnet *dev;
  1347. struct usb_device *xdev;
  1348. struct net_device *net;
  1349. dev = usb_get_intfdata(intf);
  1350. usb_set_intfdata(intf, NULL);
  1351. if (!dev)
  1352. return;
  1353. xdev = interface_to_usbdev (intf);
  1354. netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
  1355. intf->dev.driver->name,
  1356. xdev->bus->bus_name, xdev->devpath,
  1357. dev->driver_info->description);
  1358. net = dev->net;
  1359. unregister_netdev (net);
  1360. cancel_work_sync(&dev->kevent);
  1361. usb_scuttle_anchored_urbs(&dev->deferred);
  1362. if (dev->driver_info->unbind)
  1363. dev->driver_info->unbind (dev, intf);
  1364. usb_kill_urb(dev->interrupt);
  1365. usb_free_urb(dev->interrupt);
  1366. kfree(dev->padding_pkt);
  1367. free_percpu(dev->stats64);
  1368. free_netdev(net);
  1369. }
  1370. EXPORT_SYMBOL_GPL(usbnet_disconnect);
  1371. static const struct net_device_ops usbnet_netdev_ops = {
  1372. .ndo_open = usbnet_open,
  1373. .ndo_stop = usbnet_stop,
  1374. .ndo_start_xmit = usbnet_start_xmit,
  1375. .ndo_tx_timeout = usbnet_tx_timeout,
  1376. .ndo_set_rx_mode = usbnet_set_rx_mode,
  1377. .ndo_change_mtu = usbnet_change_mtu,
  1378. .ndo_get_stats64 = usbnet_get_stats64,
  1379. .ndo_set_mac_address = eth_mac_addr,
  1380. .ndo_validate_addr = eth_validate_addr,
  1381. };
  1382. /*-------------------------------------------------------------------------*/
  1383. // precondition: never called in_interrupt
  1384. static struct device_type wlan_type = {
  1385. .name = "wlan",
  1386. };
  1387. static struct device_type wwan_type = {
  1388. .name = "wwan",
  1389. };
  1390. int
  1391. usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
  1392. {
  1393. struct usbnet *dev;
  1394. struct net_device *net;
  1395. struct usb_host_interface *interface;
  1396. const struct driver_info *info;
  1397. struct usb_device *xdev;
  1398. int status;
  1399. const char *name;
  1400. struct usb_driver *driver = to_usb_driver(udev->dev.driver);
  1401. /* usbnet already took usb runtime pm, so have to enable the feature
  1402. * for usb interface, otherwise usb_autopm_get_interface may return
  1403. * failure if RUNTIME_PM is enabled.
  1404. */
  1405. if (!driver->supports_autosuspend) {
  1406. driver->supports_autosuspend = 1;
  1407. pm_runtime_enable(&udev->dev);
  1408. }
  1409. name = udev->dev.driver->name;
  1410. info = (const struct driver_info *) prod->driver_info;
  1411. if (!info) {
  1412. dev_dbg (&udev->dev, "blacklisted by %s\n", name);
  1413. return -ENODEV;
  1414. }
  1415. xdev = interface_to_usbdev (udev);
  1416. interface = udev->cur_altsetting;
  1417. status = -ENOMEM;
  1418. // set up our own records
  1419. net = alloc_etherdev(sizeof(*dev));
  1420. if (!net)
  1421. goto out;
  1422. /* netdev_printk() needs this so do it as early as possible */
  1423. SET_NETDEV_DEV(net, &udev->dev);
  1424. dev = netdev_priv(net);
  1425. dev->udev = xdev;
  1426. dev->intf = udev;
  1427. dev->driver_info = info;
  1428. dev->driver_name = name;
  1429. dev->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1430. if (!dev->stats64)
  1431. goto out0;
  1432. dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
  1433. | NETIF_MSG_PROBE | NETIF_MSG_LINK);
  1434. init_waitqueue_head(&dev->wait);
  1435. skb_queue_head_init (&dev->rxq);
  1436. skb_queue_head_init (&dev->txq);
  1437. skb_queue_head_init (&dev->done);
  1438. skb_queue_head_init(&dev->rxq_pause);
  1439. dev->bh.func = (void (*)(unsigned long))usbnet_bh;
  1440. dev->bh.data = (unsigned long)&dev->delay;
  1441. INIT_WORK (&dev->kevent, usbnet_deferred_kevent);
  1442. init_usb_anchor(&dev->deferred);
  1443. timer_setup(&dev->delay, usbnet_bh, 0);
  1444. mutex_init (&dev->phy_mutex);
  1445. mutex_init(&dev->interrupt_mutex);
  1446. dev->interrupt_count = 0;
  1447. dev->net = net;
  1448. strcpy (net->name, "usb%d");
  1449. memcpy (net->dev_addr, node_id, sizeof node_id);
  1450. /* rx and tx sides can use different message sizes;
  1451. * bind() should set rx_urb_size in that case.
  1452. */
  1453. dev->hard_mtu = net->mtu + net->hard_header_len;
  1454. net->min_mtu = 0;
  1455. net->max_mtu = ETH_MAX_MTU;
  1456. net->netdev_ops = &usbnet_netdev_ops;
  1457. net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
  1458. net->ethtool_ops = &usbnet_ethtool_ops;
  1459. // allow device-specific bind/init procedures
  1460. // NOTE net->name still not usable ...
  1461. if (info->bind) {
  1462. status = info->bind (dev, udev);
  1463. if (status < 0)
  1464. goto out1;
  1465. // heuristic: "usb%d" for links we know are two-host,
  1466. // else "eth%d" when there's reasonable doubt. userspace
  1467. // can rename the link if it knows better.
  1468. if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
  1469. ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
  1470. (net->dev_addr [0] & 0x02) == 0))
  1471. strcpy (net->name, "eth%d");
  1472. /* WLAN devices should always be named "wlan%d" */
  1473. if ((dev->driver_info->flags & FLAG_WLAN) != 0)
  1474. strcpy(net->name, "wlan%d");
  1475. /* WWAN devices should always be named "wwan%d" */
  1476. if ((dev->driver_info->flags & FLAG_WWAN) != 0)
  1477. strcpy(net->name, "wwan%d");
  1478. /* devices that cannot do ARP */
  1479. if ((dev->driver_info->flags & FLAG_NOARP) != 0)
  1480. net->flags |= IFF_NOARP;
  1481. /* maybe the remote can't receive an Ethernet MTU */
  1482. if (net->mtu > (dev->hard_mtu - net->hard_header_len))
  1483. net->mtu = dev->hard_mtu - net->hard_header_len;
  1484. } else if (!info->in || !info->out)
  1485. status = usbnet_get_endpoints (dev, udev);
  1486. else {
  1487. dev->in = usb_rcvbulkpipe (xdev, info->in);
  1488. dev->out = usb_sndbulkpipe (xdev, info->out);
  1489. if (!(info->flags & FLAG_NO_SETINT))
  1490. status = usb_set_interface (xdev,
  1491. interface->desc.bInterfaceNumber,
  1492. interface->desc.bAlternateSetting);
  1493. else
  1494. status = 0;
  1495. }
  1496. if (status >= 0 && dev->status)
  1497. status = init_status (dev, udev);
  1498. if (status < 0)
  1499. goto out3;
  1500. if (!dev->rx_urb_size)
  1501. dev->rx_urb_size = dev->hard_mtu;
  1502. dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
  1503. /* let userspace know we have a random address */
  1504. if (ether_addr_equal(net->dev_addr, node_id))
  1505. net->addr_assign_type = NET_ADDR_RANDOM;
  1506. if ((dev->driver_info->flags & FLAG_WLAN) != 0)
  1507. SET_NETDEV_DEVTYPE(net, &wlan_type);
  1508. if ((dev->driver_info->flags & FLAG_WWAN) != 0)
  1509. SET_NETDEV_DEVTYPE(net, &wwan_type);
  1510. /* initialize max rx_qlen and tx_qlen */
  1511. usbnet_update_max_qlen(dev);
  1512. if (dev->can_dma_sg && !(info->flags & FLAG_SEND_ZLP) &&
  1513. !(info->flags & FLAG_MULTI_PACKET)) {
  1514. dev->padding_pkt = kzalloc(1, GFP_KERNEL);
  1515. if (!dev->padding_pkt) {
  1516. status = -ENOMEM;
  1517. goto out4;
  1518. }
  1519. }
  1520. status = register_netdev (net);
  1521. if (status)
  1522. goto out5;
  1523. netif_info(dev, probe, dev->net,
  1524. "register '%s' at usb-%s-%s, %s, %pM\n",
  1525. udev->dev.driver->name,
  1526. xdev->bus->bus_name, xdev->devpath,
  1527. dev->driver_info->description,
  1528. net->dev_addr);
  1529. // ok, it's ready to go.
  1530. usb_set_intfdata (udev, dev);
  1531. netif_device_attach (net);
  1532. if (dev->driver_info->flags & FLAG_LINK_INTR)
  1533. usbnet_link_change(dev, 0, 0);
  1534. return 0;
  1535. out5:
  1536. kfree(dev->padding_pkt);
  1537. out4:
  1538. usb_free_urb(dev->interrupt);
  1539. out3:
  1540. if (info->unbind)
  1541. info->unbind (dev, udev);
  1542. out1:
  1543. /* subdrivers must undo all they did in bind() if they
  1544. * fail it, but we may fail later and a deferred kevent
  1545. * may trigger an error resubmitting itself and, worse,
  1546. * schedule a timer. So we kill it all just in case.
  1547. */
  1548. cancel_work_sync(&dev->kevent);
  1549. del_timer_sync(&dev->delay);
  1550. free_percpu(dev->stats64);
  1551. out0:
  1552. free_netdev(net);
  1553. out:
  1554. return status;
  1555. }
  1556. EXPORT_SYMBOL_GPL(usbnet_probe);
  1557. /*-------------------------------------------------------------------------*/
  1558. /*
  1559. * suspend the whole driver as soon as the first interface is suspended
  1560. * resume only when the last interface is resumed
  1561. */
  1562. int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
  1563. {
  1564. struct usbnet *dev = usb_get_intfdata(intf);
  1565. if (!dev->suspend_count++) {
  1566. spin_lock_irq(&dev->txq.lock);
  1567. /* don't autosuspend while transmitting */
  1568. if (dev->txq.qlen && PMSG_IS_AUTO(message)) {
  1569. dev->suspend_count--;
  1570. spin_unlock_irq(&dev->txq.lock);
  1571. return -EBUSY;
  1572. } else {
  1573. set_bit(EVENT_DEV_ASLEEP, &dev->flags);
  1574. spin_unlock_irq(&dev->txq.lock);
  1575. }
  1576. /*
  1577. * accelerate emptying of the rx and queues, to avoid
  1578. * having everything error out.
  1579. */
  1580. netif_device_detach (dev->net);
  1581. usbnet_terminate_urbs(dev);
  1582. __usbnet_status_stop_force(dev);
  1583. /*
  1584. * reattach so runtime management can use and
  1585. * wake the device
  1586. */
  1587. netif_device_attach (dev->net);
  1588. }
  1589. return 0;
  1590. }
  1591. EXPORT_SYMBOL_GPL(usbnet_suspend);
  1592. int usbnet_resume (struct usb_interface *intf)
  1593. {
  1594. struct usbnet *dev = usb_get_intfdata(intf);
  1595. struct sk_buff *skb;
  1596. struct urb *res;
  1597. int retval;
  1598. if (!--dev->suspend_count) {
  1599. /* resume interrupt URB if it was previously submitted */
  1600. __usbnet_status_start_force(dev, GFP_NOIO);
  1601. spin_lock_irq(&dev->txq.lock);
  1602. while ((res = usb_get_from_anchor(&dev->deferred))) {
  1603. skb = (struct sk_buff *)res->context;
  1604. retval = usb_submit_urb(res, GFP_ATOMIC);
  1605. if (retval < 0) {
  1606. dev_kfree_skb_any(skb);
  1607. kfree(res->sg);
  1608. usb_free_urb(res);
  1609. usb_autopm_put_interface_async(dev->intf);
  1610. } else {
  1611. netif_trans_update(dev->net);
  1612. __skb_queue_tail(&dev->txq, skb);
  1613. }
  1614. }
  1615. smp_mb();
  1616. clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
  1617. spin_unlock_irq(&dev->txq.lock);
  1618. if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
  1619. /* handle remote wakeup ASAP
  1620. * we cannot race against stop
  1621. */
  1622. if (netif_device_present(dev->net) &&
  1623. !timer_pending(&dev->delay) &&
  1624. !test_bit(EVENT_RX_HALT, &dev->flags))
  1625. rx_alloc_submit(dev, GFP_NOIO);
  1626. if (!(dev->txq.qlen >= TX_QLEN(dev)))
  1627. netif_tx_wake_all_queues(dev->net);
  1628. tasklet_schedule (&dev->bh);
  1629. }
  1630. }
  1631. if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags))
  1632. usb_autopm_get_interface_no_resume(intf);
  1633. return 0;
  1634. }
  1635. EXPORT_SYMBOL_GPL(usbnet_resume);
  1636. /*
  1637. * Either a subdriver implements manage_power, then it is assumed to always
  1638. * be ready to be suspended or it reports the readiness to be suspended
  1639. * explicitly
  1640. */
  1641. void usbnet_device_suggests_idle(struct usbnet *dev)
  1642. {
  1643. if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) {
  1644. dev->intf->needs_remote_wakeup = 1;
  1645. usb_autopm_put_interface_async(dev->intf);
  1646. }
  1647. }
  1648. EXPORT_SYMBOL(usbnet_device_suggests_idle);
  1649. /*
  1650. * For devices that can do without special commands
  1651. */
  1652. int usbnet_manage_power(struct usbnet *dev, int on)
  1653. {
  1654. dev->intf->needs_remote_wakeup = on;
  1655. return 0;
  1656. }
  1657. EXPORT_SYMBOL(usbnet_manage_power);
  1658. void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset)
  1659. {
  1660. /* update link after link is reseted */
  1661. if (link && !need_reset)
  1662. netif_carrier_on(dev->net);
  1663. else
  1664. netif_carrier_off(dev->net);
  1665. if (need_reset && link)
  1666. usbnet_defer_kevent(dev, EVENT_LINK_RESET);
  1667. else
  1668. usbnet_defer_kevent(dev, EVENT_LINK_CHANGE);
  1669. }
  1670. EXPORT_SYMBOL(usbnet_link_change);
  1671. /*-------------------------------------------------------------------------*/
  1672. static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
  1673. u16 value, u16 index, void *data, u16 size)
  1674. {
  1675. void *buf = NULL;
  1676. int err = -ENOMEM;
  1677. netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x"
  1678. " value=0x%04x index=0x%04x size=%d\n",
  1679. cmd, reqtype, value, index, size);
  1680. if (size) {
  1681. buf = kmalloc(size, GFP_KERNEL);
  1682. if (!buf)
  1683. goto out;
  1684. }
  1685. err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
  1686. cmd, reqtype, value, index, buf, size,
  1687. USB_CTRL_GET_TIMEOUT);
  1688. if (err > 0 && err <= size) {
  1689. if (data)
  1690. memcpy(data, buf, err);
  1691. else
  1692. netdev_dbg(dev->net,
  1693. "Huh? Data requested but thrown away.\n");
  1694. }
  1695. kfree(buf);
  1696. out:
  1697. return err;
  1698. }
  1699. static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
  1700. u16 value, u16 index, const void *data,
  1701. u16 size)
  1702. {
  1703. void *buf = NULL;
  1704. int err = -ENOMEM;
  1705. netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
  1706. " value=0x%04x index=0x%04x size=%d\n",
  1707. cmd, reqtype, value, index, size);
  1708. if (data) {
  1709. buf = kmemdup(data, size, GFP_KERNEL);
  1710. if (!buf)
  1711. goto out;
  1712. } else {
  1713. if (size) {
  1714. WARN_ON_ONCE(1);
  1715. err = -EINVAL;
  1716. goto out;
  1717. }
  1718. }
  1719. err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
  1720. cmd, reqtype, value, index, buf, size,
  1721. USB_CTRL_SET_TIMEOUT);
  1722. kfree(buf);
  1723. out:
  1724. return err;
  1725. }
  1726. /*
  1727. * The function can't be called inside suspend/resume callback,
  1728. * otherwise deadlock will be caused.
  1729. */
  1730. int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
  1731. u16 value, u16 index, void *data, u16 size)
  1732. {
  1733. int ret;
  1734. if (usb_autopm_get_interface(dev->intf) < 0)
  1735. return -ENODEV;
  1736. ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index,
  1737. data, size);
  1738. usb_autopm_put_interface(dev->intf);
  1739. return ret;
  1740. }
  1741. EXPORT_SYMBOL_GPL(usbnet_read_cmd);
  1742. /*
  1743. * The function can't be called inside suspend/resume callback,
  1744. * otherwise deadlock will be caused.
  1745. */
  1746. int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
  1747. u16 value, u16 index, const void *data, u16 size)
  1748. {
  1749. int ret;
  1750. if (usb_autopm_get_interface(dev->intf) < 0)
  1751. return -ENODEV;
  1752. ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index,
  1753. data, size);
  1754. usb_autopm_put_interface(dev->intf);
  1755. return ret;
  1756. }
  1757. EXPORT_SYMBOL_GPL(usbnet_write_cmd);
  1758. /*
  1759. * The function can be called inside suspend/resume callback safely
  1760. * and should only be called by suspend/resume callback generally.
  1761. */
  1762. int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
  1763. u16 value, u16 index, void *data, u16 size)
  1764. {
  1765. return __usbnet_read_cmd(dev, cmd, reqtype, value, index,
  1766. data, size);
  1767. }
  1768. EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm);
  1769. /*
  1770. * The function can be called inside suspend/resume callback safely
  1771. * and should only be called by suspend/resume callback generally.
  1772. */
  1773. int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
  1774. u16 value, u16 index, const void *data,
  1775. u16 size)
  1776. {
  1777. return __usbnet_write_cmd(dev, cmd, reqtype, value, index,
  1778. data, size);
  1779. }
  1780. EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm);
  1781. static void usbnet_async_cmd_cb(struct urb *urb)
  1782. {
  1783. struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
  1784. int status = urb->status;
  1785. if (status < 0)
  1786. dev_dbg(&urb->dev->dev, "%s failed with %d",
  1787. __func__, status);
  1788. kfree(req);
  1789. usb_free_urb(urb);
  1790. }
  1791. /*
  1792. * The caller must make sure that device can't be put into suspend
  1793. * state until the control URB completes.
  1794. */
  1795. int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype,
  1796. u16 value, u16 index, const void *data, u16 size)
  1797. {
  1798. struct usb_ctrlrequest *req = NULL;
  1799. struct urb *urb;
  1800. int err = -ENOMEM;
  1801. void *buf = NULL;
  1802. netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
  1803. " value=0x%04x index=0x%04x size=%d\n",
  1804. cmd, reqtype, value, index, size);
  1805. urb = usb_alloc_urb(0, GFP_ATOMIC);
  1806. if (!urb)
  1807. goto fail;
  1808. if (data) {
  1809. buf = kmemdup(data, size, GFP_ATOMIC);
  1810. if (!buf) {
  1811. netdev_err(dev->net, "Error allocating buffer"
  1812. " in %s!\n", __func__);
  1813. goto fail_free;
  1814. }
  1815. }
  1816. req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
  1817. if (!req)
  1818. goto fail_free_buf;
  1819. req->bRequestType = reqtype;
  1820. req->bRequest = cmd;
  1821. req->wValue = cpu_to_le16(value);
  1822. req->wIndex = cpu_to_le16(index);
  1823. req->wLength = cpu_to_le16(size);
  1824. usb_fill_control_urb(urb, dev->udev,
  1825. usb_sndctrlpipe(dev->udev, 0),
  1826. (void *)req, buf, size,
  1827. usbnet_async_cmd_cb, req);
  1828. urb->transfer_flags |= URB_FREE_BUFFER;
  1829. err = usb_submit_urb(urb, GFP_ATOMIC);
  1830. if (err < 0) {
  1831. netdev_err(dev->net, "Error submitting the control"
  1832. " message: status=%d\n", err);
  1833. goto fail_free;
  1834. }
  1835. return 0;
  1836. fail_free_buf:
  1837. kfree(buf);
  1838. fail_free:
  1839. kfree(req);
  1840. usb_free_urb(urb);
  1841. fail:
  1842. return err;
  1843. }
  1844. EXPORT_SYMBOL_GPL(usbnet_write_cmd_async);
  1845. /*-------------------------------------------------------------------------*/
  1846. static int __init usbnet_init(void)
  1847. {
  1848. /* Compiler should optimize this out. */
  1849. BUILD_BUG_ON(
  1850. FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data));
  1851. eth_random_addr(node_id);
  1852. return 0;
  1853. }
  1854. module_init(usbnet_init);
  1855. static void __exit usbnet_exit(void)
  1856. {
  1857. }
  1858. module_exit(usbnet_exit);
  1859. MODULE_AUTHOR("David Brownell");
  1860. MODULE_DESCRIPTION("USB network driver framework");
  1861. MODULE_LICENSE("GPL");