usbnet.c 57 KB

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