usbnet.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267
  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/usb/cdc.h>
  43. #include <linux/slab.h>
  44. #include <linux/kernel.h>
  45. #include <linux/pm_runtime.h>
  46. #define DRIVER_VERSION "22-Aug-2005"
  47. /*-------------------------------------------------------------------------*/
  48. /*
  49. * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
  50. * Several dozen bytes of IPv4 data can fit in two such transactions.
  51. * One maximum size Ethernet packet takes twenty four of them.
  52. * For high speed, each frame comfortably fits almost 36 max size
  53. * Ethernet packets (so queues should be bigger).
  54. *
  55. * The goal is to let the USB host controller be busy for 5msec or
  56. * more before an irq is required, under load. Jumbograms change
  57. * the equation.
  58. */
  59. #define MAX_QUEUE_MEMORY (60 * 1518)
  60. #define RX_QLEN(dev) ((dev)->rx_qlen)
  61. #define TX_QLEN(dev) ((dev)->tx_qlen)
  62. // reawaken network queue this soon after stopping; else watchdog barks
  63. #define TX_TIMEOUT_JIFFIES (5*HZ)
  64. /* throttle rx/tx briefly after some faults, so hub_wq might disconnect()
  65. * us (it polls at HZ/4 usually) before we report too many false errors.
  66. */
  67. #define THROTTLE_JIFFIES (HZ/8)
  68. // between wakeups
  69. #define UNLINK_TIMEOUT_MS 3
  70. /*-------------------------------------------------------------------------*/
  71. // randomly generated ethernet address
  72. static u8 node_id [ETH_ALEN];
  73. static const char driver_name [] = "usbnet";
  74. /* use ethtool to change the level for any given device */
  75. static int msg_level = -1;
  76. module_param (msg_level, int, 0);
  77. MODULE_PARM_DESC (msg_level, "Override default message level");
  78. /*-------------------------------------------------------------------------*/
  79. /* handles CDC Ethernet and many other network "bulk data" interfaces */
  80. int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
  81. {
  82. int tmp;
  83. struct usb_host_interface *alt = NULL;
  84. struct usb_host_endpoint *in = NULL, *out = NULL;
  85. struct usb_host_endpoint *status = NULL;
  86. for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
  87. unsigned ep;
  88. in = out = status = NULL;
  89. alt = intf->altsetting + tmp;
  90. /* take the first altsetting with in-bulk + out-bulk;
  91. * remember any status endpoint, just in case;
  92. * ignore other endpoints and altsettings.
  93. */
  94. for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
  95. struct usb_host_endpoint *e;
  96. int intr = 0;
  97. e = alt->endpoint + ep;
  98. switch (e->desc.bmAttributes) {
  99. case USB_ENDPOINT_XFER_INT:
  100. if (!usb_endpoint_dir_in(&e->desc))
  101. continue;
  102. intr = 1;
  103. /* FALLTHROUGH */
  104. case USB_ENDPOINT_XFER_BULK:
  105. break;
  106. default:
  107. continue;
  108. }
  109. if (usb_endpoint_dir_in(&e->desc)) {
  110. if (!intr && !in)
  111. in = e;
  112. else if (intr && !status)
  113. status = e;
  114. } else {
  115. if (!out)
  116. out = e;
  117. }
  118. }
  119. if (in && out)
  120. break;
  121. }
  122. if (!alt || !in || !out)
  123. return -EINVAL;
  124. if (alt->desc.bAlternateSetting != 0 ||
  125. !(dev->driver_info->flags & FLAG_NO_SETINT)) {
  126. tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
  127. alt->desc.bAlternateSetting);
  128. if (tmp < 0)
  129. return tmp;
  130. }
  131. dev->in = usb_rcvbulkpipe (dev->udev,
  132. in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  133. dev->out = usb_sndbulkpipe (dev->udev,
  134. out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  135. dev->status = status;
  136. return 0;
  137. }
  138. EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
  139. int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
  140. {
  141. int tmp = -1, ret;
  142. unsigned char buf [13];
  143. ret = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
  144. if (ret == 12)
  145. tmp = hex2bin(dev->net->dev_addr, buf, 6);
  146. if (tmp < 0) {
  147. dev_dbg(&dev->udev->dev,
  148. "bad MAC string %d fetch, %d\n", iMACAddress, tmp);
  149. if (ret >= 0)
  150. ret = -EINVAL;
  151. return ret;
  152. }
  153. return 0;
  154. }
  155. EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
  156. static void intr_complete (struct urb *urb)
  157. {
  158. struct usbnet *dev = urb->context;
  159. int status = urb->status;
  160. switch (status) {
  161. /* success */
  162. case 0:
  163. dev->driver_info->status(dev, urb);
  164. break;
  165. /* software-driven interface shutdown */
  166. case -ENOENT: /* urb killed */
  167. case -ESHUTDOWN: /* hardware gone */
  168. netif_dbg(dev, ifdown, dev->net,
  169. "intr shutdown, code %d\n", status);
  170. return;
  171. /* NOTE: not throttling like RX/TX, since this endpoint
  172. * already polls infrequently
  173. */
  174. default:
  175. netdev_dbg(dev->net, "intr status %d\n", status);
  176. break;
  177. }
  178. status = usb_submit_urb (urb, GFP_ATOMIC);
  179. if (status != 0)
  180. netif_err(dev, timer, dev->net,
  181. "intr resubmit --> %d\n", status);
  182. }
  183. static int init_status (struct usbnet *dev, struct usb_interface *intf)
  184. {
  185. char *buf = NULL;
  186. unsigned pipe = 0;
  187. unsigned maxp;
  188. unsigned period;
  189. if (!dev->driver_info->status)
  190. return 0;
  191. pipe = usb_rcvintpipe (dev->udev,
  192. dev->status->desc.bEndpointAddress
  193. & USB_ENDPOINT_NUMBER_MASK);
  194. maxp = usb_maxpacket (dev->udev, pipe, 0);
  195. /* avoid 1 msec chatter: min 8 msec poll rate */
  196. period = max ((int) dev->status->desc.bInterval,
  197. (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
  198. buf = kmalloc (maxp, GFP_KERNEL);
  199. if (buf) {
  200. dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
  201. if (!dev->interrupt) {
  202. kfree (buf);
  203. return -ENOMEM;
  204. } else {
  205. usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
  206. buf, maxp, intr_complete, dev, period);
  207. dev->interrupt->transfer_flags |= URB_FREE_BUFFER;
  208. dev_dbg(&intf->dev,
  209. "status ep%din, %d bytes period %d\n",
  210. usb_pipeendpoint(pipe), maxp, period);
  211. }
  212. }
  213. return 0;
  214. }
  215. /* Submit the interrupt URB if not previously submitted, increasing refcount */
  216. int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags)
  217. {
  218. int ret = 0;
  219. WARN_ON_ONCE(dev->interrupt == NULL);
  220. if (dev->interrupt) {
  221. mutex_lock(&dev->interrupt_mutex);
  222. if (++dev->interrupt_count == 1)
  223. ret = usb_submit_urb(dev->interrupt, mem_flags);
  224. dev_dbg(&dev->udev->dev, "incremented interrupt URB count to %d\n",
  225. dev->interrupt_count);
  226. mutex_unlock(&dev->interrupt_mutex);
  227. }
  228. return ret;
  229. }
  230. EXPORT_SYMBOL_GPL(usbnet_status_start);
  231. /* For resume; submit interrupt URB if previously submitted */
  232. static int __usbnet_status_start_force(struct usbnet *dev, gfp_t mem_flags)
  233. {
  234. int ret = 0;
  235. mutex_lock(&dev->interrupt_mutex);
  236. if (dev->interrupt_count) {
  237. ret = usb_submit_urb(dev->interrupt, mem_flags);
  238. dev_dbg(&dev->udev->dev,
  239. "submitted interrupt URB for resume\n");
  240. }
  241. mutex_unlock(&dev->interrupt_mutex);
  242. return ret;
  243. }
  244. /* Kill the interrupt URB if all submitters want it killed */
  245. void usbnet_status_stop(struct usbnet *dev)
  246. {
  247. if (dev->interrupt) {
  248. mutex_lock(&dev->interrupt_mutex);
  249. WARN_ON(dev->interrupt_count == 0);
  250. if (dev->interrupt_count && --dev->interrupt_count == 0)
  251. usb_kill_urb(dev->interrupt);
  252. dev_dbg(&dev->udev->dev,
  253. "decremented interrupt URB count to %d\n",
  254. dev->interrupt_count);
  255. mutex_unlock(&dev->interrupt_mutex);
  256. }
  257. }
  258. EXPORT_SYMBOL_GPL(usbnet_status_stop);
  259. /* For suspend; always kill interrupt URB */
  260. static void __usbnet_status_stop_force(struct usbnet *dev)
  261. {
  262. if (dev->interrupt) {
  263. mutex_lock(&dev->interrupt_mutex);
  264. usb_kill_urb(dev->interrupt);
  265. dev_dbg(&dev->udev->dev, "killed interrupt URB for suspend\n");
  266. mutex_unlock(&dev->interrupt_mutex);
  267. }
  268. }
  269. /* Passes this packet up the stack, updating its accounting.
  270. * Some link protocols batch packets, so their rx_fixup paths
  271. * can return clones as well as just modify the original skb.
  272. */
  273. void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
  274. {
  275. int status;
  276. if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
  277. skb_queue_tail(&dev->rxq_pause, skb);
  278. return;
  279. }
  280. /* only update if unset to allow minidriver rx_fixup override */
  281. if (skb->protocol == 0)
  282. skb->protocol = eth_type_trans (skb, dev->net);
  283. dev->net->stats.rx_packets++;
  284. dev->net->stats.rx_bytes += skb->len;
  285. netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
  286. skb->len + sizeof (struct ethhdr), skb->protocol);
  287. memset (skb->cb, 0, sizeof (struct skb_data));
  288. if (skb_defer_rx_timestamp(skb))
  289. return;
  290. status = netif_rx (skb);
  291. if (status != NET_RX_SUCCESS)
  292. netif_dbg(dev, rx_err, dev->net,
  293. "netif_rx status %d\n", status);
  294. }
  295. EXPORT_SYMBOL_GPL(usbnet_skb_return);
  296. /* must be called if hard_mtu or rx_urb_size changed */
  297. void usbnet_update_max_qlen(struct usbnet *dev)
  298. {
  299. enum usb_device_speed speed = dev->udev->speed;
  300. switch (speed) {
  301. case USB_SPEED_HIGH:
  302. dev->rx_qlen = MAX_QUEUE_MEMORY / dev->rx_urb_size;
  303. dev->tx_qlen = MAX_QUEUE_MEMORY / dev->hard_mtu;
  304. break;
  305. case USB_SPEED_SUPER:
  306. /*
  307. * Not take default 5ms qlen for super speed HC to
  308. * save memory, and iperf tests show 2.5ms qlen can
  309. * work well
  310. */
  311. dev->rx_qlen = 5 * MAX_QUEUE_MEMORY / dev->rx_urb_size;
  312. dev->tx_qlen = 5 * MAX_QUEUE_MEMORY / dev->hard_mtu;
  313. break;
  314. default:
  315. dev->rx_qlen = dev->tx_qlen = 4;
  316. }
  317. }
  318. EXPORT_SYMBOL_GPL(usbnet_update_max_qlen);
  319. /*-------------------------------------------------------------------------
  320. *
  321. * Network Device Driver (peer link to "Host Device", from USB host)
  322. *
  323. *-------------------------------------------------------------------------*/
  324. int usbnet_change_mtu (struct net_device *net, int new_mtu)
  325. {
  326. struct usbnet *dev = netdev_priv(net);
  327. int ll_mtu = new_mtu + net->hard_header_len;
  328. int old_hard_mtu = dev->hard_mtu;
  329. int old_rx_urb_size = dev->rx_urb_size;
  330. if (new_mtu <= 0)
  331. return -EINVAL;
  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_unlink_rx_urbs(dev);
  341. }
  342. /* max qlen depend on hard_mtu and rx_urb_size */
  343. usbnet_update_max_qlen(dev);
  344. return 0;
  345. }
  346. EXPORT_SYMBOL_GPL(usbnet_change_mtu);
  347. /* The caller must hold list->lock */
  348. static void __usbnet_queue_skb(struct sk_buff_head *list,
  349. struct sk_buff *newsk, enum skb_state state)
  350. {
  351. struct skb_data *entry = (struct skb_data *) newsk->cb;
  352. __skb_queue_tail(list, newsk);
  353. entry->state = state;
  354. }
  355. /*-------------------------------------------------------------------------*/
  356. /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
  357. * completion callbacks. 2.5 should have fixed those bugs...
  358. */
  359. static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
  360. struct sk_buff_head *list, enum skb_state state)
  361. {
  362. unsigned long flags;
  363. enum skb_state old_state;
  364. struct skb_data *entry = (struct skb_data *) skb->cb;
  365. spin_lock_irqsave(&list->lock, flags);
  366. old_state = entry->state;
  367. entry->state = state;
  368. __skb_unlink(skb, list);
  369. /* defer_bh() is never called with list == &dev->done.
  370. * spin_lock_nested() tells lockdep that it is OK to take
  371. * dev->done.lock here with list->lock held.
  372. */
  373. spin_lock_nested(&dev->done.lock, SINGLE_DEPTH_NESTING);
  374. __skb_queue_tail(&dev->done, skb);
  375. if (dev->done.qlen == 1)
  376. tasklet_schedule(&dev->bh);
  377. spin_unlock(&dev->done.lock);
  378. spin_unlock_irqrestore(&list->lock, flags);
  379. return old_state;
  380. }
  381. /* some work can't be done in tasklets, so we use keventd
  382. *
  383. * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
  384. * but tasklet_schedule() doesn't. hope the failure is rare.
  385. */
  386. void usbnet_defer_kevent (struct usbnet *dev, int work)
  387. {
  388. set_bit (work, &dev->flags);
  389. if (!schedule_work (&dev->kevent)) {
  390. if (net_ratelimit())
  391. netdev_err(dev->net, "kevent %d may have been dropped\n", work);
  392. } else {
  393. netdev_dbg(dev->net, "kevent %d scheduled\n", work);
  394. }
  395. }
  396. EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
  397. /*-------------------------------------------------------------------------*/
  398. static void rx_complete (struct urb *urb);
  399. static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
  400. {
  401. struct sk_buff *skb;
  402. struct skb_data *entry;
  403. int retval = 0;
  404. unsigned long lockflags;
  405. size_t size = dev->rx_urb_size;
  406. /* prevent rx skb allocation when error ratio is high */
  407. if (test_bit(EVENT_RX_KILL, &dev->flags)) {
  408. usb_free_urb(urb);
  409. return -ENOLINK;
  410. }
  411. skb = __netdev_alloc_skb_ip_align(dev->net, size, flags);
  412. if (!skb) {
  413. netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
  414. usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
  415. usb_free_urb (urb);
  416. return -ENOMEM;
  417. }
  418. entry = (struct skb_data *) skb->cb;
  419. entry->urb = urb;
  420. entry->dev = dev;
  421. entry->length = 0;
  422. usb_fill_bulk_urb (urb, dev->udev, dev->in,
  423. skb->data, size, rx_complete, skb);
  424. spin_lock_irqsave (&dev->rxq.lock, lockflags);
  425. if (netif_running (dev->net) &&
  426. netif_device_present (dev->net) &&
  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. 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. 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_settings (struct net_device *net, struct ethtool_cmd *cmd)
  806. {
  807. struct usbnet *dev = netdev_priv(net);
  808. if (!dev->mii.mdio_read)
  809. return -EOPNOTSUPP;
  810. return mii_ethtool_gset(&dev->mii, cmd);
  811. }
  812. EXPORT_SYMBOL_GPL(usbnet_get_settings);
  813. int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
  814. {
  815. struct usbnet *dev = netdev_priv(net);
  816. int retval;
  817. if (!dev->mii.mdio_write)
  818. return -EOPNOTSUPP;
  819. retval = mii_ethtool_sset(&dev->mii, cmd);
  820. /* link speed/duplex might have changed */
  821. if (dev->driver_info->link_reset)
  822. dev->driver_info->link_reset(dev);
  823. /* hard_mtu or rx_urb_size may change in link_reset() */
  824. usbnet_update_max_qlen(dev);
  825. return retval;
  826. }
  827. EXPORT_SYMBOL_GPL(usbnet_set_settings);
  828. u32 usbnet_get_link (struct net_device *net)
  829. {
  830. struct usbnet *dev = netdev_priv(net);
  831. /* If a check_connect is defined, return its result */
  832. if (dev->driver_info->check_connect)
  833. return dev->driver_info->check_connect (dev) == 0;
  834. /* if the device has mii operations, use those */
  835. if (dev->mii.mdio_read)
  836. return mii_link_ok(&dev->mii);
  837. /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
  838. return ethtool_op_get_link(net);
  839. }
  840. EXPORT_SYMBOL_GPL(usbnet_get_link);
  841. int usbnet_nway_reset(struct net_device *net)
  842. {
  843. struct usbnet *dev = netdev_priv(net);
  844. if (!dev->mii.mdio_write)
  845. return -EOPNOTSUPP;
  846. return mii_nway_restart(&dev->mii);
  847. }
  848. EXPORT_SYMBOL_GPL(usbnet_nway_reset);
  849. void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
  850. {
  851. struct usbnet *dev = netdev_priv(net);
  852. strlcpy (info->driver, dev->driver_name, sizeof info->driver);
  853. strlcpy (info->version, DRIVER_VERSION, sizeof info->version);
  854. strlcpy (info->fw_version, dev->driver_info->description,
  855. sizeof info->fw_version);
  856. usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
  857. }
  858. EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
  859. u32 usbnet_get_msglevel (struct net_device *net)
  860. {
  861. struct usbnet *dev = netdev_priv(net);
  862. return dev->msg_enable;
  863. }
  864. EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
  865. void usbnet_set_msglevel (struct net_device *net, u32 level)
  866. {
  867. struct usbnet *dev = netdev_priv(net);
  868. dev->msg_enable = level;
  869. }
  870. EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
  871. /* drivers may override default ethtool_ops in their bind() routine */
  872. static const struct ethtool_ops usbnet_ethtool_ops = {
  873. .get_settings = usbnet_get_settings,
  874. .set_settings = usbnet_set_settings,
  875. .get_link = usbnet_get_link,
  876. .nway_reset = usbnet_nway_reset,
  877. .get_drvinfo = usbnet_get_drvinfo,
  878. .get_msglevel = usbnet_get_msglevel,
  879. .set_msglevel = usbnet_set_msglevel,
  880. .get_ts_info = ethtool_op_get_ts_info,
  881. };
  882. /*-------------------------------------------------------------------------*/
  883. static void __handle_link_change(struct usbnet *dev)
  884. {
  885. if (!test_bit(EVENT_DEV_OPEN, &dev->flags))
  886. return;
  887. if (!netif_carrier_ok(dev->net)) {
  888. /* kill URBs for reading packets to save bus bandwidth */
  889. unlink_urbs(dev, &dev->rxq);
  890. /*
  891. * tx_timeout will unlink URBs for sending packets and
  892. * tx queue is stopped by netcore after link becomes off
  893. */
  894. } else {
  895. /* submitting URBs for reading packets */
  896. tasklet_schedule(&dev->bh);
  897. }
  898. /* hard_mtu or rx_urb_size may change during link change */
  899. usbnet_update_max_qlen(dev);
  900. clear_bit(EVENT_LINK_CHANGE, &dev->flags);
  901. }
  902. static void usbnet_set_rx_mode(struct net_device *net)
  903. {
  904. struct usbnet *dev = netdev_priv(net);
  905. usbnet_defer_kevent(dev, EVENT_SET_RX_MODE);
  906. }
  907. static void __handle_set_rx_mode(struct usbnet *dev)
  908. {
  909. if (dev->driver_info->set_rx_mode)
  910. (dev->driver_info->set_rx_mode)(dev);
  911. clear_bit(EVENT_SET_RX_MODE, &dev->flags);
  912. }
  913. /* work that cannot be done in interrupt context uses keventd.
  914. *
  915. * NOTE: with 2.5 we could do more of this using completion callbacks,
  916. * especially now that control transfers can be queued.
  917. */
  918. static void
  919. usbnet_deferred_kevent (struct work_struct *work)
  920. {
  921. struct usbnet *dev =
  922. container_of(work, struct usbnet, kevent);
  923. int status;
  924. /* usb_clear_halt() needs a thread context */
  925. if (test_bit (EVENT_TX_HALT, &dev->flags)) {
  926. unlink_urbs (dev, &dev->txq);
  927. status = usb_autopm_get_interface(dev->intf);
  928. if (status < 0)
  929. goto fail_pipe;
  930. status = usb_clear_halt (dev->udev, dev->out);
  931. usb_autopm_put_interface(dev->intf);
  932. if (status < 0 &&
  933. status != -EPIPE &&
  934. status != -ESHUTDOWN) {
  935. if (netif_msg_tx_err (dev))
  936. fail_pipe:
  937. netdev_err(dev->net, "can't clear tx halt, status %d\n",
  938. status);
  939. } else {
  940. clear_bit (EVENT_TX_HALT, &dev->flags);
  941. if (status != -ESHUTDOWN)
  942. netif_wake_queue (dev->net);
  943. }
  944. }
  945. if (test_bit (EVENT_RX_HALT, &dev->flags)) {
  946. unlink_urbs (dev, &dev->rxq);
  947. status = usb_autopm_get_interface(dev->intf);
  948. if (status < 0)
  949. goto fail_halt;
  950. status = usb_clear_halt (dev->udev, dev->in);
  951. usb_autopm_put_interface(dev->intf);
  952. if (status < 0 &&
  953. status != -EPIPE &&
  954. status != -ESHUTDOWN) {
  955. if (netif_msg_rx_err (dev))
  956. fail_halt:
  957. netdev_err(dev->net, "can't clear rx halt, status %d\n",
  958. status);
  959. } else {
  960. clear_bit (EVENT_RX_HALT, &dev->flags);
  961. tasklet_schedule (&dev->bh);
  962. }
  963. }
  964. /* tasklet could resubmit itself forever if memory is tight */
  965. if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
  966. struct urb *urb = NULL;
  967. int resched = 1;
  968. if (netif_running (dev->net))
  969. urb = usb_alloc_urb (0, GFP_KERNEL);
  970. else
  971. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  972. if (urb != NULL) {
  973. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  974. status = usb_autopm_get_interface(dev->intf);
  975. if (status < 0) {
  976. usb_free_urb(urb);
  977. goto fail_lowmem;
  978. }
  979. if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
  980. resched = 0;
  981. usb_autopm_put_interface(dev->intf);
  982. fail_lowmem:
  983. if (resched)
  984. tasklet_schedule (&dev->bh);
  985. }
  986. }
  987. if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
  988. struct driver_info *info = dev->driver_info;
  989. int retval = 0;
  990. clear_bit (EVENT_LINK_RESET, &dev->flags);
  991. status = usb_autopm_get_interface(dev->intf);
  992. if (status < 0)
  993. goto skip_reset;
  994. if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
  995. usb_autopm_put_interface(dev->intf);
  996. skip_reset:
  997. netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
  998. retval,
  999. dev->udev->bus->bus_name,
  1000. dev->udev->devpath,
  1001. info->description);
  1002. } else {
  1003. usb_autopm_put_interface(dev->intf);
  1004. }
  1005. /* handle link change from link resetting */
  1006. __handle_link_change(dev);
  1007. }
  1008. if (test_bit (EVENT_LINK_CHANGE, &dev->flags))
  1009. __handle_link_change(dev);
  1010. if (test_bit (EVENT_SET_RX_MODE, &dev->flags))
  1011. __handle_set_rx_mode(dev);
  1012. if (dev->flags)
  1013. netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
  1014. }
  1015. /*-------------------------------------------------------------------------*/
  1016. static void tx_complete (struct urb *urb)
  1017. {
  1018. struct sk_buff *skb = (struct sk_buff *) urb->context;
  1019. struct skb_data *entry = (struct skb_data *) skb->cb;
  1020. struct usbnet *dev = entry->dev;
  1021. if (urb->status == 0) {
  1022. dev->net->stats.tx_packets += entry->packets;
  1023. dev->net->stats.tx_bytes += entry->length;
  1024. } else {
  1025. dev->net->stats.tx_errors++;
  1026. switch (urb->status) {
  1027. case -EPIPE:
  1028. usbnet_defer_kevent (dev, EVENT_TX_HALT);
  1029. break;
  1030. /* software-driven interface shutdown */
  1031. case -ECONNRESET: // async unlink
  1032. case -ESHUTDOWN: // hardware gone
  1033. break;
  1034. /* like rx, tx gets controller i/o faults during hub_wq
  1035. * delays and so it uses the same throttling mechanism.
  1036. */
  1037. case -EPROTO:
  1038. case -ETIME:
  1039. case -EILSEQ:
  1040. usb_mark_last_busy(dev->udev);
  1041. if (!timer_pending (&dev->delay)) {
  1042. mod_timer (&dev->delay,
  1043. jiffies + THROTTLE_JIFFIES);
  1044. netif_dbg(dev, link, dev->net,
  1045. "tx throttle %d\n", urb->status);
  1046. }
  1047. netif_stop_queue (dev->net);
  1048. break;
  1049. default:
  1050. netif_dbg(dev, tx_err, dev->net,
  1051. "tx err %d\n", entry->urb->status);
  1052. break;
  1053. }
  1054. }
  1055. usb_autopm_put_interface_async(dev->intf);
  1056. (void) defer_bh(dev, skb, &dev->txq, tx_done);
  1057. }
  1058. /*-------------------------------------------------------------------------*/
  1059. void usbnet_tx_timeout (struct net_device *net)
  1060. {
  1061. struct usbnet *dev = netdev_priv(net);
  1062. unlink_urbs (dev, &dev->txq);
  1063. tasklet_schedule (&dev->bh);
  1064. /* this needs to be handled individually because the generic layer
  1065. * doesn't know what is sufficient and could not restore private
  1066. * information if a remedy of an unconditional reset were used.
  1067. */
  1068. if (dev->driver_info->recover)
  1069. (dev->driver_info->recover)(dev);
  1070. }
  1071. EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
  1072. /*-------------------------------------------------------------------------*/
  1073. static int build_dma_sg(const struct sk_buff *skb, struct urb *urb)
  1074. {
  1075. unsigned num_sgs, total_len = 0;
  1076. int i, s = 0;
  1077. num_sgs = skb_shinfo(skb)->nr_frags + 1;
  1078. if (num_sgs == 1)
  1079. return 0;
  1080. /* reserve one for zero packet */
  1081. urb->sg = kmalloc((num_sgs + 1) * sizeof(struct scatterlist),
  1082. GFP_ATOMIC);
  1083. if (!urb->sg)
  1084. return -ENOMEM;
  1085. urb->num_sgs = num_sgs;
  1086. sg_init_table(urb->sg, urb->num_sgs + 1);
  1087. sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb));
  1088. total_len += skb_headlen(skb);
  1089. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1090. struct skb_frag_struct *f = &skb_shinfo(skb)->frags[i];
  1091. total_len += skb_frag_size(f);
  1092. sg_set_page(&urb->sg[i + s], f->page.p, f->size,
  1093. f->page_offset);
  1094. }
  1095. urb->transfer_buffer_length = total_len;
  1096. return 1;
  1097. }
  1098. netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
  1099. struct net_device *net)
  1100. {
  1101. struct usbnet *dev = netdev_priv(net);
  1102. unsigned int length;
  1103. struct urb *urb = NULL;
  1104. struct skb_data *entry;
  1105. struct driver_info *info = dev->driver_info;
  1106. unsigned long flags;
  1107. int retval;
  1108. if (skb)
  1109. skb_tx_timestamp(skb);
  1110. // some devices want funky USB-level framing, for
  1111. // win32 driver (usually) and/or hardware quirks
  1112. if (info->tx_fixup) {
  1113. skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
  1114. if (!skb) {
  1115. /* packet collected; minidriver waiting for more */
  1116. if (info->flags & FLAG_MULTI_PACKET)
  1117. goto not_drop;
  1118. netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
  1119. goto drop;
  1120. }
  1121. }
  1122. if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
  1123. netif_dbg(dev, tx_err, dev->net, "no urb\n");
  1124. goto drop;
  1125. }
  1126. entry = (struct skb_data *) skb->cb;
  1127. entry->urb = urb;
  1128. entry->dev = dev;
  1129. usb_fill_bulk_urb (urb, dev->udev, dev->out,
  1130. skb->data, skb->len, tx_complete, skb);
  1131. if (dev->can_dma_sg) {
  1132. if (build_dma_sg(skb, urb) < 0)
  1133. goto drop;
  1134. }
  1135. length = urb->transfer_buffer_length;
  1136. /* don't assume the hardware handles USB_ZERO_PACKET
  1137. * NOTE: strictly conforming cdc-ether devices should expect
  1138. * the ZLP here, but ignore the one-byte packet.
  1139. * NOTE2: CDC NCM specification is different from CDC ECM when
  1140. * handling ZLP/short packets, so cdc_ncm driver will make short
  1141. * packet itself if needed.
  1142. */
  1143. if (length % dev->maxpacket == 0) {
  1144. if (!(info->flags & FLAG_SEND_ZLP)) {
  1145. if (!(info->flags & FLAG_MULTI_PACKET)) {
  1146. length++;
  1147. if (skb_tailroom(skb) && !urb->num_sgs) {
  1148. skb->data[skb->len] = 0;
  1149. __skb_put(skb, 1);
  1150. } else if (urb->num_sgs)
  1151. sg_set_buf(&urb->sg[urb->num_sgs++],
  1152. dev->padding_pkt, 1);
  1153. }
  1154. } else
  1155. urb->transfer_flags |= URB_ZERO_PACKET;
  1156. }
  1157. urb->transfer_buffer_length = length;
  1158. if (info->flags & FLAG_MULTI_PACKET) {
  1159. /* Driver has set number of packets and a length delta.
  1160. * Calculate the complete length and ensure that it's
  1161. * positive.
  1162. */
  1163. entry->length += length;
  1164. if (WARN_ON_ONCE(entry->length <= 0))
  1165. entry->length = length;
  1166. } else {
  1167. usbnet_set_skb_tx_stats(skb, 1, length);
  1168. }
  1169. spin_lock_irqsave(&dev->txq.lock, flags);
  1170. retval = usb_autopm_get_interface_async(dev->intf);
  1171. if (retval < 0) {
  1172. spin_unlock_irqrestore(&dev->txq.lock, flags);
  1173. goto drop;
  1174. }
  1175. #ifdef CONFIG_PM
  1176. /* if this triggers the device is still a sleep */
  1177. if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
  1178. /* transmission will be done in resume */
  1179. usb_anchor_urb(urb, &dev->deferred);
  1180. /* no use to process more packets */
  1181. netif_stop_queue(net);
  1182. usb_put_urb(urb);
  1183. spin_unlock_irqrestore(&dev->txq.lock, flags);
  1184. netdev_dbg(dev->net, "Delaying transmission for resumption\n");
  1185. goto deferred;
  1186. }
  1187. #endif
  1188. switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
  1189. case -EPIPE:
  1190. netif_stop_queue (net);
  1191. usbnet_defer_kevent (dev, EVENT_TX_HALT);
  1192. usb_autopm_put_interface_async(dev->intf);
  1193. break;
  1194. default:
  1195. usb_autopm_put_interface_async(dev->intf);
  1196. netif_dbg(dev, tx_err, dev->net,
  1197. "tx: submit urb err %d\n", retval);
  1198. break;
  1199. case 0:
  1200. net->trans_start = jiffies;
  1201. __usbnet_queue_skb(&dev->txq, skb, tx_start);
  1202. if (dev->txq.qlen >= TX_QLEN (dev))
  1203. netif_stop_queue (net);
  1204. }
  1205. spin_unlock_irqrestore (&dev->txq.lock, flags);
  1206. if (retval) {
  1207. netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
  1208. drop:
  1209. dev->net->stats.tx_dropped++;
  1210. not_drop:
  1211. if (skb)
  1212. dev_kfree_skb_any (skb);
  1213. if (urb) {
  1214. kfree(urb->sg);
  1215. usb_free_urb(urb);
  1216. }
  1217. } else
  1218. netif_dbg(dev, tx_queued, dev->net,
  1219. "> tx, len %u, type 0x%x\n", length, skb->protocol);
  1220. #ifdef CONFIG_PM
  1221. deferred:
  1222. #endif
  1223. return NETDEV_TX_OK;
  1224. }
  1225. EXPORT_SYMBOL_GPL(usbnet_start_xmit);
  1226. static int rx_alloc_submit(struct usbnet *dev, gfp_t flags)
  1227. {
  1228. struct urb *urb;
  1229. int i;
  1230. int ret = 0;
  1231. /* don't refill the queue all at once */
  1232. for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) {
  1233. urb = usb_alloc_urb(0, flags);
  1234. if (urb != NULL) {
  1235. ret = rx_submit(dev, urb, flags);
  1236. if (ret)
  1237. goto err;
  1238. } else {
  1239. ret = -ENOMEM;
  1240. goto err;
  1241. }
  1242. }
  1243. err:
  1244. return ret;
  1245. }
  1246. /*-------------------------------------------------------------------------*/
  1247. // tasklet (work deferred from completions, in_irq) or timer
  1248. static void usbnet_bh (unsigned long param)
  1249. {
  1250. struct usbnet *dev = (struct usbnet *) param;
  1251. struct sk_buff *skb;
  1252. struct skb_data *entry;
  1253. while ((skb = skb_dequeue (&dev->done))) {
  1254. entry = (struct skb_data *) skb->cb;
  1255. switch (entry->state) {
  1256. case rx_done:
  1257. entry->state = rx_cleanup;
  1258. rx_process (dev, skb);
  1259. continue;
  1260. case tx_done:
  1261. kfree(entry->urb->sg);
  1262. case rx_cleanup:
  1263. usb_free_urb (entry->urb);
  1264. dev_kfree_skb (skb);
  1265. continue;
  1266. default:
  1267. netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
  1268. }
  1269. }
  1270. /* restart RX again after disabling due to high error rate */
  1271. clear_bit(EVENT_RX_KILL, &dev->flags);
  1272. /* waiting for all pending urbs to complete?
  1273. * only then can we forgo submitting anew
  1274. */
  1275. if (waitqueue_active(&dev->wait)) {
  1276. if (dev->txq.qlen + dev->rxq.qlen + dev->done.qlen == 0)
  1277. wake_up_all(&dev->wait);
  1278. // or are we maybe short a few urbs?
  1279. } else if (netif_running (dev->net) &&
  1280. netif_device_present (dev->net) &&
  1281. netif_carrier_ok(dev->net) &&
  1282. !timer_pending (&dev->delay) &&
  1283. !test_bit (EVENT_RX_HALT, &dev->flags)) {
  1284. int temp = dev->rxq.qlen;
  1285. if (temp < RX_QLEN(dev)) {
  1286. if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK)
  1287. return;
  1288. if (temp != dev->rxq.qlen)
  1289. netif_dbg(dev, link, dev->net,
  1290. "rxqlen %d --> %d\n",
  1291. temp, dev->rxq.qlen);
  1292. if (dev->rxq.qlen < RX_QLEN(dev))
  1293. tasklet_schedule (&dev->bh);
  1294. }
  1295. if (dev->txq.qlen < TX_QLEN (dev))
  1296. netif_wake_queue (dev->net);
  1297. }
  1298. }
  1299. /*-------------------------------------------------------------------------
  1300. *
  1301. * USB Device Driver support
  1302. *
  1303. *-------------------------------------------------------------------------*/
  1304. // precondition: never called in_interrupt
  1305. void usbnet_disconnect (struct usb_interface *intf)
  1306. {
  1307. struct usbnet *dev;
  1308. struct usb_device *xdev;
  1309. struct net_device *net;
  1310. dev = usb_get_intfdata(intf);
  1311. usb_set_intfdata(intf, NULL);
  1312. if (!dev)
  1313. return;
  1314. xdev = interface_to_usbdev (intf);
  1315. netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
  1316. intf->dev.driver->name,
  1317. xdev->bus->bus_name, xdev->devpath,
  1318. dev->driver_info->description);
  1319. net = dev->net;
  1320. unregister_netdev (net);
  1321. cancel_work_sync(&dev->kevent);
  1322. usb_scuttle_anchored_urbs(&dev->deferred);
  1323. if (dev->driver_info->unbind)
  1324. dev->driver_info->unbind (dev, intf);
  1325. usb_kill_urb(dev->interrupt);
  1326. usb_free_urb(dev->interrupt);
  1327. kfree(dev->padding_pkt);
  1328. free_netdev(net);
  1329. }
  1330. EXPORT_SYMBOL_GPL(usbnet_disconnect);
  1331. static const struct net_device_ops usbnet_netdev_ops = {
  1332. .ndo_open = usbnet_open,
  1333. .ndo_stop = usbnet_stop,
  1334. .ndo_start_xmit = usbnet_start_xmit,
  1335. .ndo_tx_timeout = usbnet_tx_timeout,
  1336. .ndo_set_rx_mode = usbnet_set_rx_mode,
  1337. .ndo_change_mtu = usbnet_change_mtu,
  1338. .ndo_set_mac_address = eth_mac_addr,
  1339. .ndo_validate_addr = eth_validate_addr,
  1340. };
  1341. /*-------------------------------------------------------------------------*/
  1342. // precondition: never called in_interrupt
  1343. static struct device_type wlan_type = {
  1344. .name = "wlan",
  1345. };
  1346. static struct device_type wwan_type = {
  1347. .name = "wwan",
  1348. };
  1349. int
  1350. usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
  1351. {
  1352. struct usbnet *dev;
  1353. struct net_device *net;
  1354. struct usb_host_interface *interface;
  1355. struct driver_info *info;
  1356. struct usb_device *xdev;
  1357. int status;
  1358. const char *name;
  1359. struct usb_driver *driver = to_usb_driver(udev->dev.driver);
  1360. /* usbnet already took usb runtime pm, so have to enable the feature
  1361. * for usb interface, otherwise usb_autopm_get_interface may return
  1362. * failure if RUNTIME_PM is enabled.
  1363. */
  1364. if (!driver->supports_autosuspend) {
  1365. driver->supports_autosuspend = 1;
  1366. pm_runtime_enable(&udev->dev);
  1367. }
  1368. name = udev->dev.driver->name;
  1369. info = (struct driver_info *) prod->driver_info;
  1370. if (!info) {
  1371. dev_dbg (&udev->dev, "blacklisted by %s\n", name);
  1372. return -ENODEV;
  1373. }
  1374. xdev = interface_to_usbdev (udev);
  1375. interface = udev->cur_altsetting;
  1376. status = -ENOMEM;
  1377. // set up our own records
  1378. net = alloc_etherdev(sizeof(*dev));
  1379. if (!net)
  1380. goto out;
  1381. /* netdev_printk() needs this so do it as early as possible */
  1382. SET_NETDEV_DEV(net, &udev->dev);
  1383. dev = netdev_priv(net);
  1384. dev->udev = xdev;
  1385. dev->intf = udev;
  1386. dev->driver_info = info;
  1387. dev->driver_name = name;
  1388. dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
  1389. | NETIF_MSG_PROBE | NETIF_MSG_LINK);
  1390. init_waitqueue_head(&dev->wait);
  1391. skb_queue_head_init (&dev->rxq);
  1392. skb_queue_head_init (&dev->txq);
  1393. skb_queue_head_init (&dev->done);
  1394. skb_queue_head_init(&dev->rxq_pause);
  1395. dev->bh.func = usbnet_bh;
  1396. dev->bh.data = (unsigned long) dev;
  1397. INIT_WORK (&dev->kevent, usbnet_deferred_kevent);
  1398. init_usb_anchor(&dev->deferred);
  1399. dev->delay.function = usbnet_bh;
  1400. dev->delay.data = (unsigned long) dev;
  1401. init_timer (&dev->delay);
  1402. mutex_init (&dev->phy_mutex);
  1403. mutex_init(&dev->interrupt_mutex);
  1404. dev->interrupt_count = 0;
  1405. dev->net = net;
  1406. strcpy (net->name, "usb%d");
  1407. memcpy (net->dev_addr, node_id, sizeof node_id);
  1408. /* rx and tx sides can use different message sizes;
  1409. * bind() should set rx_urb_size in that case.
  1410. */
  1411. dev->hard_mtu = net->mtu + net->hard_header_len;
  1412. net->netdev_ops = &usbnet_netdev_ops;
  1413. net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
  1414. net->ethtool_ops = &usbnet_ethtool_ops;
  1415. // allow device-specific bind/init procedures
  1416. // NOTE net->name still not usable ...
  1417. if (info->bind) {
  1418. status = info->bind (dev, udev);
  1419. if (status < 0)
  1420. goto out1;
  1421. // heuristic: "usb%d" for links we know are two-host,
  1422. // else "eth%d" when there's reasonable doubt. userspace
  1423. // can rename the link if it knows better.
  1424. if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
  1425. ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
  1426. (net->dev_addr [0] & 0x02) == 0))
  1427. strcpy (net->name, "eth%d");
  1428. /* WLAN devices should always be named "wlan%d" */
  1429. if ((dev->driver_info->flags & FLAG_WLAN) != 0)
  1430. strcpy(net->name, "wlan%d");
  1431. /* WWAN devices should always be named "wwan%d" */
  1432. if ((dev->driver_info->flags & FLAG_WWAN) != 0)
  1433. strcpy(net->name, "wwan%d");
  1434. /* devices that cannot do ARP */
  1435. if ((dev->driver_info->flags & FLAG_NOARP) != 0)
  1436. net->flags |= IFF_NOARP;
  1437. /* maybe the remote can't receive an Ethernet MTU */
  1438. if (net->mtu > (dev->hard_mtu - net->hard_header_len))
  1439. net->mtu = dev->hard_mtu - net->hard_header_len;
  1440. } else if (!info->in || !info->out)
  1441. status = usbnet_get_endpoints (dev, udev);
  1442. else {
  1443. dev->in = usb_rcvbulkpipe (xdev, info->in);
  1444. dev->out = usb_sndbulkpipe (xdev, info->out);
  1445. if (!(info->flags & FLAG_NO_SETINT))
  1446. status = usb_set_interface (xdev,
  1447. interface->desc.bInterfaceNumber,
  1448. interface->desc.bAlternateSetting);
  1449. else
  1450. status = 0;
  1451. }
  1452. if (status >= 0 && dev->status)
  1453. status = init_status (dev, udev);
  1454. if (status < 0)
  1455. goto out3;
  1456. if (!dev->rx_urb_size)
  1457. dev->rx_urb_size = dev->hard_mtu;
  1458. dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
  1459. /* let userspace know we have a random address */
  1460. if (ether_addr_equal(net->dev_addr, node_id))
  1461. net->addr_assign_type = NET_ADDR_RANDOM;
  1462. if ((dev->driver_info->flags & FLAG_WLAN) != 0)
  1463. SET_NETDEV_DEVTYPE(net, &wlan_type);
  1464. if ((dev->driver_info->flags & FLAG_WWAN) != 0)
  1465. SET_NETDEV_DEVTYPE(net, &wwan_type);
  1466. /* initialize max rx_qlen and tx_qlen */
  1467. usbnet_update_max_qlen(dev);
  1468. if (dev->can_dma_sg && !(info->flags & FLAG_SEND_ZLP) &&
  1469. !(info->flags & FLAG_MULTI_PACKET)) {
  1470. dev->padding_pkt = kzalloc(1, GFP_KERNEL);
  1471. if (!dev->padding_pkt) {
  1472. status = -ENOMEM;
  1473. goto out4;
  1474. }
  1475. }
  1476. status = register_netdev (net);
  1477. if (status)
  1478. goto out5;
  1479. netif_info(dev, probe, dev->net,
  1480. "register '%s' at usb-%s-%s, %s, %pM\n",
  1481. udev->dev.driver->name,
  1482. xdev->bus->bus_name, xdev->devpath,
  1483. dev->driver_info->description,
  1484. net->dev_addr);
  1485. // ok, it's ready to go.
  1486. usb_set_intfdata (udev, dev);
  1487. netif_device_attach (net);
  1488. if (dev->driver_info->flags & FLAG_LINK_INTR)
  1489. usbnet_link_change(dev, 0, 0);
  1490. return 0;
  1491. out5:
  1492. kfree(dev->padding_pkt);
  1493. out4:
  1494. usb_free_urb(dev->interrupt);
  1495. out3:
  1496. if (info->unbind)
  1497. info->unbind (dev, udev);
  1498. out1:
  1499. /* subdrivers must undo all they did in bind() if they
  1500. * fail it, but we may fail later and a deferred kevent
  1501. * may trigger an error resubmitting itself and, worse,
  1502. * schedule a timer. So we kill it all just in case.
  1503. */
  1504. cancel_work_sync(&dev->kevent);
  1505. del_timer_sync(&dev->delay);
  1506. free_netdev(net);
  1507. out:
  1508. return status;
  1509. }
  1510. EXPORT_SYMBOL_GPL(usbnet_probe);
  1511. /*-------------------------------------------------------------------------*/
  1512. /*
  1513. * suspend the whole driver as soon as the first interface is suspended
  1514. * resume only when the last interface is resumed
  1515. */
  1516. int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
  1517. {
  1518. struct usbnet *dev = usb_get_intfdata(intf);
  1519. if (!dev->suspend_count++) {
  1520. spin_lock_irq(&dev->txq.lock);
  1521. /* don't autosuspend while transmitting */
  1522. if (dev->txq.qlen && PMSG_IS_AUTO(message)) {
  1523. dev->suspend_count--;
  1524. spin_unlock_irq(&dev->txq.lock);
  1525. return -EBUSY;
  1526. } else {
  1527. set_bit(EVENT_DEV_ASLEEP, &dev->flags);
  1528. spin_unlock_irq(&dev->txq.lock);
  1529. }
  1530. /*
  1531. * accelerate emptying of the rx and queues, to avoid
  1532. * having everything error out.
  1533. */
  1534. netif_device_detach (dev->net);
  1535. usbnet_terminate_urbs(dev);
  1536. __usbnet_status_stop_force(dev);
  1537. /*
  1538. * reattach so runtime management can use and
  1539. * wake the device
  1540. */
  1541. netif_device_attach (dev->net);
  1542. }
  1543. return 0;
  1544. }
  1545. EXPORT_SYMBOL_GPL(usbnet_suspend);
  1546. int usbnet_resume (struct usb_interface *intf)
  1547. {
  1548. struct usbnet *dev = usb_get_intfdata(intf);
  1549. struct sk_buff *skb;
  1550. struct urb *res;
  1551. int retval;
  1552. if (!--dev->suspend_count) {
  1553. /* resume interrupt URB if it was previously submitted */
  1554. __usbnet_status_start_force(dev, GFP_NOIO);
  1555. spin_lock_irq(&dev->txq.lock);
  1556. while ((res = usb_get_from_anchor(&dev->deferred))) {
  1557. skb = (struct sk_buff *)res->context;
  1558. retval = usb_submit_urb(res, GFP_ATOMIC);
  1559. if (retval < 0) {
  1560. dev_kfree_skb_any(skb);
  1561. kfree(res->sg);
  1562. usb_free_urb(res);
  1563. usb_autopm_put_interface_async(dev->intf);
  1564. } else {
  1565. dev->net->trans_start = jiffies;
  1566. __skb_queue_tail(&dev->txq, skb);
  1567. }
  1568. }
  1569. smp_mb();
  1570. clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
  1571. spin_unlock_irq(&dev->txq.lock);
  1572. if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
  1573. /* handle remote wakeup ASAP
  1574. * we cannot race against stop
  1575. */
  1576. if (netif_device_present(dev->net) &&
  1577. !timer_pending(&dev->delay) &&
  1578. !test_bit(EVENT_RX_HALT, &dev->flags))
  1579. rx_alloc_submit(dev, GFP_NOIO);
  1580. if (!(dev->txq.qlen >= TX_QLEN(dev)))
  1581. netif_tx_wake_all_queues(dev->net);
  1582. tasklet_schedule (&dev->bh);
  1583. }
  1584. }
  1585. if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags))
  1586. usb_autopm_get_interface_no_resume(intf);
  1587. return 0;
  1588. }
  1589. EXPORT_SYMBOL_GPL(usbnet_resume);
  1590. /*
  1591. * Either a subdriver implements manage_power, then it is assumed to always
  1592. * be ready to be suspended or it reports the readiness to be suspended
  1593. * explicitly
  1594. */
  1595. void usbnet_device_suggests_idle(struct usbnet *dev)
  1596. {
  1597. if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) {
  1598. dev->intf->needs_remote_wakeup = 1;
  1599. usb_autopm_put_interface_async(dev->intf);
  1600. }
  1601. }
  1602. EXPORT_SYMBOL(usbnet_device_suggests_idle);
  1603. /*
  1604. * For devices that can do without special commands
  1605. */
  1606. int usbnet_manage_power(struct usbnet *dev, int on)
  1607. {
  1608. dev->intf->needs_remote_wakeup = on;
  1609. return 0;
  1610. }
  1611. EXPORT_SYMBOL(usbnet_manage_power);
  1612. void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset)
  1613. {
  1614. /* update link after link is reseted */
  1615. if (link && !need_reset)
  1616. netif_carrier_on(dev->net);
  1617. else
  1618. netif_carrier_off(dev->net);
  1619. if (need_reset && link)
  1620. usbnet_defer_kevent(dev, EVENT_LINK_RESET);
  1621. else
  1622. usbnet_defer_kevent(dev, EVENT_LINK_CHANGE);
  1623. }
  1624. EXPORT_SYMBOL(usbnet_link_change);
  1625. /*-------------------------------------------------------------------------*/
  1626. static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
  1627. u16 value, u16 index, void *data, u16 size)
  1628. {
  1629. void *buf = NULL;
  1630. int err = -ENOMEM;
  1631. netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x"
  1632. " value=0x%04x index=0x%04x size=%d\n",
  1633. cmd, reqtype, value, index, size);
  1634. if (data) {
  1635. buf = kmalloc(size, GFP_KERNEL);
  1636. if (!buf)
  1637. goto out;
  1638. }
  1639. err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
  1640. cmd, reqtype, value, index, buf, size,
  1641. USB_CTRL_GET_TIMEOUT);
  1642. if (err > 0 && err <= size)
  1643. memcpy(data, buf, err);
  1644. kfree(buf);
  1645. out:
  1646. return err;
  1647. }
  1648. static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
  1649. u16 value, u16 index, const void *data,
  1650. u16 size)
  1651. {
  1652. void *buf = NULL;
  1653. int err = -ENOMEM;
  1654. netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
  1655. " value=0x%04x index=0x%04x size=%d\n",
  1656. cmd, reqtype, value, index, size);
  1657. if (data) {
  1658. buf = kmemdup(data, size, GFP_KERNEL);
  1659. if (!buf)
  1660. goto out;
  1661. }
  1662. err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
  1663. cmd, reqtype, value, index, buf, size,
  1664. USB_CTRL_SET_TIMEOUT);
  1665. kfree(buf);
  1666. out:
  1667. return err;
  1668. }
  1669. int cdc_parse_cdc_header(struct usb_cdc_parsed_header *hdr,
  1670. struct usb_interface *intf,
  1671. u8 *buffer,
  1672. int buflen)
  1673. {
  1674. /* duplicates are ignored */
  1675. struct usb_cdc_union_desc *union_header = NULL;
  1676. /* duplicates are not tolerated */
  1677. struct usb_cdc_header_desc *header = NULL;
  1678. struct usb_cdc_ether_desc *ether = NULL;
  1679. struct usb_cdc_mdlm_detail_desc *detail = NULL;
  1680. struct usb_cdc_mdlm_desc *desc = NULL;
  1681. unsigned int elength;
  1682. int cnt = 0;
  1683. memset(hdr, 0x00, sizeof(struct usb_cdc_parsed_header));
  1684. hdr->phonet_magic_present = false;
  1685. while (buflen > 0) {
  1686. elength = buffer[0];
  1687. if (!elength) {
  1688. dev_err(&intf->dev, "skipping garbage byte\n");
  1689. elength = 1;
  1690. goto next_desc;
  1691. }
  1692. if (buffer[1] != USB_DT_CS_INTERFACE) {
  1693. dev_err(&intf->dev, "skipping garbage\n");
  1694. goto next_desc;
  1695. }
  1696. switch (buffer[2]) {
  1697. case USB_CDC_UNION_TYPE: /* we've found it */
  1698. if (elength < sizeof(struct usb_cdc_union_desc))
  1699. goto next_desc;
  1700. if (union_header) {
  1701. dev_err(&intf->dev, "More than one union descriptor, skipping ...\n");
  1702. goto next_desc;
  1703. }
  1704. union_header = (struct usb_cdc_union_desc *)buffer;
  1705. break;
  1706. case USB_CDC_COUNTRY_TYPE:
  1707. if (elength < sizeof(struct usb_cdc_country_functional_desc))
  1708. goto next_desc;
  1709. hdr->usb_cdc_country_functional_desc =
  1710. (struct usb_cdc_country_functional_desc *)buffer;
  1711. break;
  1712. case USB_CDC_HEADER_TYPE:
  1713. if (elength != sizeof(struct usb_cdc_header_desc))
  1714. goto next_desc;
  1715. if (header)
  1716. return -EINVAL;
  1717. header = (struct usb_cdc_header_desc *)buffer;
  1718. break;
  1719. case USB_CDC_ACM_TYPE:
  1720. if (elength < sizeof(struct usb_cdc_acm_descriptor))
  1721. goto next_desc;
  1722. hdr->usb_cdc_acm_descriptor =
  1723. (struct usb_cdc_acm_descriptor *)buffer;
  1724. break;
  1725. case USB_CDC_ETHERNET_TYPE:
  1726. if (elength != sizeof(struct usb_cdc_ether_desc))
  1727. goto next_desc;
  1728. if (ether)
  1729. return -EINVAL;
  1730. ether = (struct usb_cdc_ether_desc *)buffer;
  1731. break;
  1732. case USB_CDC_CALL_MANAGEMENT_TYPE:
  1733. if (elength < sizeof(struct usb_cdc_call_mgmt_descriptor))
  1734. goto next_desc;
  1735. hdr->usb_cdc_call_mgmt_descriptor =
  1736. (struct usb_cdc_call_mgmt_descriptor *)buffer;
  1737. break;
  1738. case USB_CDC_DMM_TYPE:
  1739. if (elength < sizeof(struct usb_cdc_dmm_desc))
  1740. goto next_desc;
  1741. hdr->usb_cdc_dmm_desc =
  1742. (struct usb_cdc_dmm_desc *)buffer;
  1743. break;
  1744. case USB_CDC_MDLM_TYPE:
  1745. if (elength < sizeof(struct usb_cdc_mdlm_desc *))
  1746. goto next_desc;
  1747. if (desc)
  1748. return -EINVAL;
  1749. desc = (struct usb_cdc_mdlm_desc *)buffer;
  1750. break;
  1751. case USB_CDC_MDLM_DETAIL_TYPE:
  1752. if (elength < sizeof(struct usb_cdc_mdlm_detail_desc *))
  1753. goto next_desc;
  1754. if (detail)
  1755. return -EINVAL;
  1756. detail = (struct usb_cdc_mdlm_detail_desc *)buffer;
  1757. break;
  1758. case USB_CDC_NCM_TYPE:
  1759. if (elength < sizeof(struct usb_cdc_ncm_desc))
  1760. goto next_desc;
  1761. hdr->usb_cdc_ncm_desc = (struct usb_cdc_ncm_desc *)buffer;
  1762. break;
  1763. case USB_CDC_MBIM_TYPE:
  1764. if (elength < sizeof(struct usb_cdc_mbim_desc))
  1765. goto next_desc;
  1766. hdr->usb_cdc_mbim_desc = (struct usb_cdc_mbim_desc *)buffer;
  1767. break;
  1768. case USB_CDC_MBIM_EXTENDED_TYPE:
  1769. if (elength < sizeof(struct usb_cdc_mbim_extended_desc))
  1770. break;
  1771. hdr->usb_cdc_mbim_extended_desc =
  1772. (struct usb_cdc_mbim_extended_desc *)buffer;
  1773. break;
  1774. case CDC_PHONET_MAGIC_NUMBER:
  1775. hdr->phonet_magic_present = true;
  1776. break;
  1777. default:
  1778. /*
  1779. * there are LOTS more CDC descriptors that
  1780. * could legitimately be found here.
  1781. */
  1782. dev_dbg(&intf->dev, "Ignoring descriptor: type %02x, length %ud\n",
  1783. buffer[2], elength);
  1784. goto next_desc;
  1785. }
  1786. cnt++;
  1787. next_desc:
  1788. buflen -= elength;
  1789. buffer += elength;
  1790. }
  1791. hdr->usb_cdc_union_desc = union_header;
  1792. hdr->usb_cdc_header_desc = header;
  1793. hdr->usb_cdc_mdlm_detail_desc = detail;
  1794. hdr->usb_cdc_mdlm_desc = desc;
  1795. hdr->usb_cdc_ether_desc = ether;
  1796. return cnt;
  1797. }
  1798. EXPORT_SYMBOL(cdc_parse_cdc_header);
  1799. /*
  1800. * The function can't be called inside suspend/resume callback,
  1801. * otherwise deadlock will be caused.
  1802. */
  1803. int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
  1804. u16 value, u16 index, void *data, u16 size)
  1805. {
  1806. int ret;
  1807. if (usb_autopm_get_interface(dev->intf) < 0)
  1808. return -ENODEV;
  1809. ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index,
  1810. data, size);
  1811. usb_autopm_put_interface(dev->intf);
  1812. return ret;
  1813. }
  1814. EXPORT_SYMBOL_GPL(usbnet_read_cmd);
  1815. /*
  1816. * The function can't be called inside suspend/resume callback,
  1817. * otherwise deadlock will be caused.
  1818. */
  1819. int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
  1820. u16 value, u16 index, const void *data, u16 size)
  1821. {
  1822. int ret;
  1823. if (usb_autopm_get_interface(dev->intf) < 0)
  1824. return -ENODEV;
  1825. ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index,
  1826. data, size);
  1827. usb_autopm_put_interface(dev->intf);
  1828. return ret;
  1829. }
  1830. EXPORT_SYMBOL_GPL(usbnet_write_cmd);
  1831. /*
  1832. * The function can be called inside suspend/resume callback safely
  1833. * and should only be called by suspend/resume callback generally.
  1834. */
  1835. int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
  1836. u16 value, u16 index, void *data, u16 size)
  1837. {
  1838. return __usbnet_read_cmd(dev, cmd, reqtype, value, index,
  1839. data, size);
  1840. }
  1841. EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm);
  1842. /*
  1843. * The function can be called inside suspend/resume callback safely
  1844. * and should only be called by suspend/resume callback generally.
  1845. */
  1846. int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
  1847. u16 value, u16 index, const void *data,
  1848. u16 size)
  1849. {
  1850. return __usbnet_write_cmd(dev, cmd, reqtype, value, index,
  1851. data, size);
  1852. }
  1853. EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm);
  1854. static void usbnet_async_cmd_cb(struct urb *urb)
  1855. {
  1856. struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
  1857. int status = urb->status;
  1858. if (status < 0)
  1859. dev_dbg(&urb->dev->dev, "%s failed with %d",
  1860. __func__, status);
  1861. kfree(req);
  1862. usb_free_urb(urb);
  1863. }
  1864. /*
  1865. * The caller must make sure that device can't be put into suspend
  1866. * state until the control URB completes.
  1867. */
  1868. int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype,
  1869. u16 value, u16 index, const void *data, u16 size)
  1870. {
  1871. struct usb_ctrlrequest *req = NULL;
  1872. struct urb *urb;
  1873. int err = -ENOMEM;
  1874. void *buf = NULL;
  1875. netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
  1876. " value=0x%04x index=0x%04x size=%d\n",
  1877. cmd, reqtype, value, index, size);
  1878. urb = usb_alloc_urb(0, GFP_ATOMIC);
  1879. if (!urb) {
  1880. netdev_err(dev->net, "Error allocating URB in"
  1881. " %s!\n", __func__);
  1882. goto fail;
  1883. }
  1884. if (data) {
  1885. buf = kmemdup(data, size, GFP_ATOMIC);
  1886. if (!buf) {
  1887. netdev_err(dev->net, "Error allocating buffer"
  1888. " in %s!\n", __func__);
  1889. goto fail_free;
  1890. }
  1891. }
  1892. req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
  1893. if (!req)
  1894. goto fail_free_buf;
  1895. req->bRequestType = reqtype;
  1896. req->bRequest = cmd;
  1897. req->wValue = cpu_to_le16(value);
  1898. req->wIndex = cpu_to_le16(index);
  1899. req->wLength = cpu_to_le16(size);
  1900. usb_fill_control_urb(urb, dev->udev,
  1901. usb_sndctrlpipe(dev->udev, 0),
  1902. (void *)req, buf, size,
  1903. usbnet_async_cmd_cb, req);
  1904. urb->transfer_flags |= URB_FREE_BUFFER;
  1905. err = usb_submit_urb(urb, GFP_ATOMIC);
  1906. if (err < 0) {
  1907. netdev_err(dev->net, "Error submitting the control"
  1908. " message: status=%d\n", err);
  1909. goto fail_free;
  1910. }
  1911. return 0;
  1912. fail_free_buf:
  1913. kfree(buf);
  1914. fail_free:
  1915. kfree(req);
  1916. usb_free_urb(urb);
  1917. fail:
  1918. return err;
  1919. }
  1920. EXPORT_SYMBOL_GPL(usbnet_write_cmd_async);
  1921. /*-------------------------------------------------------------------------*/
  1922. static int __init usbnet_init(void)
  1923. {
  1924. /* Compiler should optimize this out. */
  1925. BUILD_BUG_ON(
  1926. FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data));
  1927. eth_random_addr(node_id);
  1928. return 0;
  1929. }
  1930. module_init(usbnet_init);
  1931. static void __exit usbnet_exit(void)
  1932. {
  1933. }
  1934. module_exit(usbnet_exit);
  1935. MODULE_AUTHOR("David Brownell");
  1936. MODULE_DESCRIPTION("USB network driver framework");
  1937. MODULE_LICENSE("GPL");