usbnet.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188
  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. if (net_ratelimit())
  394. netdev_err(dev->net, "kevent %d may have been dropped\n", work);
  395. } else {
  396. netdev_dbg(dev->net, "kevent %d scheduled\n", work);
  397. }
  398. }
  399. EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
  400. /*-------------------------------------------------------------------------*/
  401. static void rx_complete (struct urb *urb);
  402. static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
  403. {
  404. struct sk_buff *skb;
  405. struct skb_data *entry;
  406. int retval = 0;
  407. unsigned long lockflags;
  408. size_t size = dev->rx_urb_size;
  409. /* prevent rx skb allocation when error ratio is high */
  410. if (test_bit(EVENT_RX_KILL, &dev->flags)) {
  411. usb_free_urb(urb);
  412. return -ENOLINK;
  413. }
  414. skb = __netdev_alloc_skb_ip_align(dev->net, size, flags);
  415. if (!skb) {
  416. netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
  417. usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
  418. usb_free_urb (urb);
  419. return -ENOMEM;
  420. }
  421. entry = (struct skb_data *) skb->cb;
  422. entry->urb = urb;
  423. entry->dev = dev;
  424. entry->length = 0;
  425. usb_fill_bulk_urb (urb, dev->udev, dev->in,
  426. skb->data, size, rx_complete, skb);
  427. spin_lock_irqsave (&dev->rxq.lock, lockflags);
  428. if (netif_running (dev->net) &&
  429. netif_device_present (dev->net) &&
  430. !test_bit (EVENT_RX_HALT, &dev->flags) &&
  431. !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
  432. switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
  433. case -EPIPE:
  434. usbnet_defer_kevent (dev, EVENT_RX_HALT);
  435. break;
  436. case -ENOMEM:
  437. usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
  438. break;
  439. case -ENODEV:
  440. netif_dbg(dev, ifdown, dev->net, "device gone\n");
  441. netif_device_detach (dev->net);
  442. break;
  443. case -EHOSTUNREACH:
  444. retval = -ENOLINK;
  445. break;
  446. default:
  447. netif_dbg(dev, rx_err, dev->net,
  448. "rx submit, %d\n", retval);
  449. tasklet_schedule (&dev->bh);
  450. break;
  451. case 0:
  452. __usbnet_queue_skb(&dev->rxq, skb, rx_start);
  453. }
  454. } else {
  455. netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
  456. retval = -ENOLINK;
  457. }
  458. spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
  459. if (retval) {
  460. dev_kfree_skb_any (skb);
  461. usb_free_urb (urb);
  462. }
  463. return retval;
  464. }
  465. /*-------------------------------------------------------------------------*/
  466. static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
  467. {
  468. if (dev->driver_info->rx_fixup &&
  469. !dev->driver_info->rx_fixup (dev, skb)) {
  470. /* With RX_ASSEMBLE, rx_fixup() must update counters */
  471. if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE))
  472. dev->net->stats.rx_errors++;
  473. goto done;
  474. }
  475. // else network stack removes extra byte if we forced a short packet
  476. /* all data was already cloned from skb inside the driver */
  477. if (dev->driver_info->flags & FLAG_MULTI_PACKET)
  478. goto done;
  479. if (skb->len < ETH_HLEN) {
  480. dev->net->stats.rx_errors++;
  481. dev->net->stats.rx_length_errors++;
  482. netif_dbg(dev, rx_err, dev->net, "rx length %d\n", skb->len);
  483. } else {
  484. usbnet_skb_return(dev, skb);
  485. return;
  486. }
  487. done:
  488. skb_queue_tail(&dev->done, skb);
  489. }
  490. /*-------------------------------------------------------------------------*/
  491. static void rx_complete (struct urb *urb)
  492. {
  493. struct sk_buff *skb = (struct sk_buff *) urb->context;
  494. struct skb_data *entry = (struct skb_data *) skb->cb;
  495. struct usbnet *dev = entry->dev;
  496. int urb_status = urb->status;
  497. enum skb_state state;
  498. skb_put (skb, urb->actual_length);
  499. state = rx_done;
  500. entry->urb = NULL;
  501. switch (urb_status) {
  502. /* success */
  503. case 0:
  504. break;
  505. /* stalls need manual reset. this is rare ... except that
  506. * when going through USB 2.0 TTs, unplug appears this way.
  507. * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
  508. * storm, recovering as needed.
  509. */
  510. case -EPIPE:
  511. dev->net->stats.rx_errors++;
  512. usbnet_defer_kevent (dev, EVENT_RX_HALT);
  513. // FALLTHROUGH
  514. /* software-driven interface shutdown */
  515. case -ECONNRESET: /* async unlink */
  516. case -ESHUTDOWN: /* hardware gone */
  517. netif_dbg(dev, ifdown, dev->net,
  518. "rx shutdown, code %d\n", urb_status);
  519. goto block;
  520. /* we get controller i/o faults during hub_wq disconnect() delays.
  521. * throttle down resubmits, to avoid log floods; just temporarily,
  522. * so we still recover when the fault isn't a hub_wq delay.
  523. */
  524. case -EPROTO:
  525. case -ETIME:
  526. case -EILSEQ:
  527. dev->net->stats.rx_errors++;
  528. if (!timer_pending (&dev->delay)) {
  529. mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
  530. netif_dbg(dev, link, dev->net,
  531. "rx throttle %d\n", urb_status);
  532. }
  533. block:
  534. state = rx_cleanup;
  535. entry->urb = urb;
  536. urb = NULL;
  537. break;
  538. /* data overrun ... flush fifo? */
  539. case -EOVERFLOW:
  540. dev->net->stats.rx_over_errors++;
  541. // FALLTHROUGH
  542. default:
  543. state = rx_cleanup;
  544. dev->net->stats.rx_errors++;
  545. netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
  546. break;
  547. }
  548. /* stop rx if packet error rate is high */
  549. if (++dev->pkt_cnt > 30) {
  550. dev->pkt_cnt = 0;
  551. dev->pkt_err = 0;
  552. } else {
  553. if (state == rx_cleanup)
  554. dev->pkt_err++;
  555. if (dev->pkt_err > 20)
  556. set_bit(EVENT_RX_KILL, &dev->flags);
  557. }
  558. state = defer_bh(dev, skb, &dev->rxq, state);
  559. if (urb) {
  560. if (netif_running (dev->net) &&
  561. !test_bit (EVENT_RX_HALT, &dev->flags) &&
  562. state != unlink_start) {
  563. rx_submit (dev, urb, GFP_ATOMIC);
  564. usb_mark_last_busy(dev->udev);
  565. return;
  566. }
  567. usb_free_urb (urb);
  568. }
  569. netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
  570. }
  571. /*-------------------------------------------------------------------------*/
  572. void usbnet_pause_rx(struct usbnet *dev)
  573. {
  574. set_bit(EVENT_RX_PAUSED, &dev->flags);
  575. netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
  576. }
  577. EXPORT_SYMBOL_GPL(usbnet_pause_rx);
  578. void usbnet_resume_rx(struct usbnet *dev)
  579. {
  580. struct sk_buff *skb;
  581. int num = 0;
  582. clear_bit(EVENT_RX_PAUSED, &dev->flags);
  583. while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
  584. usbnet_skb_return(dev, skb);
  585. num++;
  586. }
  587. tasklet_schedule(&dev->bh);
  588. netif_dbg(dev, rx_status, dev->net,
  589. "paused rx queue disabled, %d skbs requeued\n", num);
  590. }
  591. EXPORT_SYMBOL_GPL(usbnet_resume_rx);
  592. void usbnet_purge_paused_rxq(struct usbnet *dev)
  593. {
  594. skb_queue_purge(&dev->rxq_pause);
  595. }
  596. EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
  597. /*-------------------------------------------------------------------------*/
  598. // unlink pending rx/tx; completion handlers do all other cleanup
  599. static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
  600. {
  601. unsigned long flags;
  602. struct sk_buff *skb;
  603. int count = 0;
  604. spin_lock_irqsave (&q->lock, flags);
  605. while (!skb_queue_empty(q)) {
  606. struct skb_data *entry;
  607. struct urb *urb;
  608. int retval;
  609. skb_queue_walk(q, skb) {
  610. entry = (struct skb_data *) skb->cb;
  611. if (entry->state != unlink_start)
  612. goto found;
  613. }
  614. break;
  615. found:
  616. entry->state = unlink_start;
  617. urb = entry->urb;
  618. /*
  619. * Get reference count of the URB to avoid it to be
  620. * freed during usb_unlink_urb, which may trigger
  621. * use-after-free problem inside usb_unlink_urb since
  622. * usb_unlink_urb is always racing with .complete
  623. * handler(include defer_bh).
  624. */
  625. usb_get_urb(urb);
  626. spin_unlock_irqrestore(&q->lock, flags);
  627. // during some PM-driven resume scenarios,
  628. // these (async) unlinks complete immediately
  629. retval = usb_unlink_urb (urb);
  630. if (retval != -EINPROGRESS && retval != 0)
  631. netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
  632. else
  633. count++;
  634. usb_put_urb(urb);
  635. spin_lock_irqsave(&q->lock, flags);
  636. }
  637. spin_unlock_irqrestore (&q->lock, flags);
  638. return count;
  639. }
  640. // Flush all pending rx urbs
  641. // minidrivers may need to do this when the MTU changes
  642. void usbnet_unlink_rx_urbs(struct usbnet *dev)
  643. {
  644. if (netif_running(dev->net)) {
  645. (void) unlink_urbs (dev, &dev->rxq);
  646. tasklet_schedule(&dev->bh);
  647. }
  648. }
  649. EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
  650. /*-------------------------------------------------------------------------*/
  651. static void wait_skb_queue_empty(struct sk_buff_head *q)
  652. {
  653. unsigned long flags;
  654. spin_lock_irqsave(&q->lock, flags);
  655. while (!skb_queue_empty(q)) {
  656. spin_unlock_irqrestore(&q->lock, flags);
  657. schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
  658. set_current_state(TASK_UNINTERRUPTIBLE);
  659. spin_lock_irqsave(&q->lock, flags);
  660. }
  661. spin_unlock_irqrestore(&q->lock, flags);
  662. }
  663. // precondition: never called in_interrupt
  664. static void usbnet_terminate_urbs(struct usbnet *dev)
  665. {
  666. DECLARE_WAITQUEUE(wait, current);
  667. int temp;
  668. /* ensure there are no more active urbs */
  669. add_wait_queue(&dev->wait, &wait);
  670. set_current_state(TASK_UNINTERRUPTIBLE);
  671. temp = unlink_urbs(dev, &dev->txq) +
  672. unlink_urbs(dev, &dev->rxq);
  673. /* maybe wait for deletions to finish. */
  674. wait_skb_queue_empty(&dev->rxq);
  675. wait_skb_queue_empty(&dev->txq);
  676. wait_skb_queue_empty(&dev->done);
  677. netif_dbg(dev, ifdown, dev->net,
  678. "waited for %d urb completions\n", temp);
  679. set_current_state(TASK_RUNNING);
  680. remove_wait_queue(&dev->wait, &wait);
  681. }
  682. int usbnet_stop (struct net_device *net)
  683. {
  684. struct usbnet *dev = netdev_priv(net);
  685. struct driver_info *info = dev->driver_info;
  686. int retval, pm, mpn;
  687. clear_bit(EVENT_DEV_OPEN, &dev->flags);
  688. netif_stop_queue (net);
  689. netif_info(dev, ifdown, dev->net,
  690. "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
  691. net->stats.rx_packets, net->stats.tx_packets,
  692. net->stats.rx_errors, net->stats.tx_errors);
  693. /* to not race resume */
  694. pm = usb_autopm_get_interface(dev->intf);
  695. /* allow minidriver to stop correctly (wireless devices to turn off
  696. * radio etc) */
  697. if (info->stop) {
  698. retval = info->stop(dev);
  699. if (retval < 0)
  700. netif_info(dev, ifdown, dev->net,
  701. "stop fail (%d) usbnet usb-%s-%s, %s\n",
  702. retval,
  703. dev->udev->bus->bus_name, dev->udev->devpath,
  704. info->description);
  705. }
  706. if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
  707. usbnet_terminate_urbs(dev);
  708. usbnet_status_stop(dev);
  709. usbnet_purge_paused_rxq(dev);
  710. mpn = !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
  711. /* deferred work (task, timer, softirq) must also stop.
  712. * can't flush_scheduled_work() until we drop rtnl (later),
  713. * else workers could deadlock; so make workers a NOP.
  714. */
  715. dev->flags = 0;
  716. del_timer_sync (&dev->delay);
  717. tasklet_kill (&dev->bh);
  718. if (!pm)
  719. usb_autopm_put_interface(dev->intf);
  720. if (info->manage_power && mpn)
  721. info->manage_power(dev, 0);
  722. else
  723. usb_autopm_put_interface(dev->intf);
  724. return 0;
  725. }
  726. EXPORT_SYMBOL_GPL(usbnet_stop);
  727. /*-------------------------------------------------------------------------*/
  728. // posts reads, and enables write queuing
  729. // precondition: never called in_interrupt
  730. int usbnet_open (struct net_device *net)
  731. {
  732. struct usbnet *dev = netdev_priv(net);
  733. int retval;
  734. struct driver_info *info = dev->driver_info;
  735. if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
  736. netif_info(dev, ifup, dev->net,
  737. "resumption fail (%d) usbnet usb-%s-%s, %s\n",
  738. retval,
  739. dev->udev->bus->bus_name,
  740. dev->udev->devpath,
  741. info->description);
  742. goto done_nopm;
  743. }
  744. // put into "known safe" state
  745. if (info->reset && (retval = info->reset (dev)) < 0) {
  746. netif_info(dev, ifup, dev->net,
  747. "open reset fail (%d) usbnet usb-%s-%s, %s\n",
  748. retval,
  749. dev->udev->bus->bus_name,
  750. dev->udev->devpath,
  751. info->description);
  752. goto done;
  753. }
  754. /* hard_mtu or rx_urb_size may change in reset() */
  755. usbnet_update_max_qlen(dev);
  756. // insist peer be connected
  757. if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
  758. netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
  759. goto done;
  760. }
  761. /* start any status interrupt transfer */
  762. if (dev->interrupt) {
  763. retval = usbnet_status_start(dev, GFP_KERNEL);
  764. if (retval < 0) {
  765. netif_err(dev, ifup, dev->net,
  766. "intr submit %d\n", retval);
  767. goto done;
  768. }
  769. }
  770. set_bit(EVENT_DEV_OPEN, &dev->flags);
  771. netif_start_queue (net);
  772. netif_info(dev, ifup, dev->net,
  773. "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
  774. (int)RX_QLEN(dev), (int)TX_QLEN(dev),
  775. dev->net->mtu,
  776. (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
  777. (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
  778. (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
  779. (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
  780. (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
  781. "simple");
  782. /* reset rx error state */
  783. dev->pkt_cnt = 0;
  784. dev->pkt_err = 0;
  785. clear_bit(EVENT_RX_KILL, &dev->flags);
  786. // delay posting reads until we're fully open
  787. tasklet_schedule (&dev->bh);
  788. if (info->manage_power) {
  789. retval = info->manage_power(dev, 1);
  790. if (retval < 0) {
  791. retval = 0;
  792. set_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
  793. } else {
  794. usb_autopm_put_interface(dev->intf);
  795. }
  796. }
  797. return retval;
  798. done:
  799. usb_autopm_put_interface(dev->intf);
  800. done_nopm:
  801. return retval;
  802. }
  803. EXPORT_SYMBOL_GPL(usbnet_open);
  804. /*-------------------------------------------------------------------------*/
  805. /* ethtool methods; minidrivers may need to add some more, but
  806. * they'll probably want to use this base set.
  807. */
  808. int usbnet_get_link_ksettings(struct net_device *net,
  809. struct ethtool_link_ksettings *cmd)
  810. {
  811. struct usbnet *dev = netdev_priv(net);
  812. if (!dev->mii.mdio_read)
  813. return -EOPNOTSUPP;
  814. return mii_ethtool_get_link_ksettings(&dev->mii, cmd);
  815. }
  816. EXPORT_SYMBOL_GPL(usbnet_get_link_ksettings);
  817. int usbnet_set_link_ksettings(struct net_device *net,
  818. const struct ethtool_link_ksettings *cmd)
  819. {
  820. struct usbnet *dev = netdev_priv(net);
  821. int retval;
  822. if (!dev->mii.mdio_write)
  823. return -EOPNOTSUPP;
  824. retval = mii_ethtool_set_link_ksettings(&dev->mii, cmd);
  825. /* link speed/duplex might have changed */
  826. if (dev->driver_info->link_reset)
  827. dev->driver_info->link_reset(dev);
  828. /* hard_mtu or rx_urb_size may change in link_reset() */
  829. usbnet_update_max_qlen(dev);
  830. return retval;
  831. }
  832. EXPORT_SYMBOL_GPL(usbnet_set_link_ksettings);
  833. void usbnet_get_stats64(struct net_device *net, struct rtnl_link_stats64 *stats)
  834. {
  835. struct usbnet *dev = netdev_priv(net);
  836. unsigned int start;
  837. int cpu;
  838. netdev_stats_to_stats64(stats, &net->stats);
  839. for_each_possible_cpu(cpu) {
  840. struct pcpu_sw_netstats *stats64;
  841. u64 rx_packets, rx_bytes;
  842. u64 tx_packets, tx_bytes;
  843. stats64 = per_cpu_ptr(dev->stats64, cpu);
  844. do {
  845. start = u64_stats_fetch_begin_irq(&stats64->syncp);
  846. rx_packets = stats64->rx_packets;
  847. rx_bytes = stats64->rx_bytes;
  848. tx_packets = stats64->tx_packets;
  849. tx_bytes = stats64->tx_bytes;
  850. } while (u64_stats_fetch_retry_irq(&stats64->syncp, start));
  851. stats->rx_packets += rx_packets;
  852. stats->rx_bytes += rx_bytes;
  853. stats->tx_packets += tx_packets;
  854. stats->tx_bytes += tx_bytes;
  855. }
  856. }
  857. EXPORT_SYMBOL_GPL(usbnet_get_stats64);
  858. u32 usbnet_get_link (struct net_device *net)
  859. {
  860. struct usbnet *dev = netdev_priv(net);
  861. /* If a check_connect is defined, return its result */
  862. if (dev->driver_info->check_connect)
  863. return dev->driver_info->check_connect (dev) == 0;
  864. /* if the device has mii operations, use those */
  865. if (dev->mii.mdio_read)
  866. return mii_link_ok(&dev->mii);
  867. /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
  868. return ethtool_op_get_link(net);
  869. }
  870. EXPORT_SYMBOL_GPL(usbnet_get_link);
  871. int usbnet_nway_reset(struct net_device *net)
  872. {
  873. struct usbnet *dev = netdev_priv(net);
  874. if (!dev->mii.mdio_write)
  875. return -EOPNOTSUPP;
  876. return mii_nway_restart(&dev->mii);
  877. }
  878. EXPORT_SYMBOL_GPL(usbnet_nway_reset);
  879. void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
  880. {
  881. struct usbnet *dev = netdev_priv(net);
  882. strlcpy (info->driver, dev->driver_name, sizeof info->driver);
  883. strlcpy (info->version, DRIVER_VERSION, sizeof info->version);
  884. strlcpy (info->fw_version, dev->driver_info->description,
  885. sizeof info->fw_version);
  886. usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
  887. }
  888. EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
  889. u32 usbnet_get_msglevel (struct net_device *net)
  890. {
  891. struct usbnet *dev = netdev_priv(net);
  892. return dev->msg_enable;
  893. }
  894. EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
  895. void usbnet_set_msglevel (struct net_device *net, u32 level)
  896. {
  897. struct usbnet *dev = netdev_priv(net);
  898. dev->msg_enable = level;
  899. }
  900. EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
  901. /* drivers may override default ethtool_ops in their bind() routine */
  902. static const struct ethtool_ops usbnet_ethtool_ops = {
  903. .get_link = usbnet_get_link,
  904. .nway_reset = usbnet_nway_reset,
  905. .get_drvinfo = usbnet_get_drvinfo,
  906. .get_msglevel = usbnet_get_msglevel,
  907. .set_msglevel = usbnet_set_msglevel,
  908. .get_ts_info = ethtool_op_get_ts_info,
  909. .get_link_ksettings = usbnet_get_link_ksettings,
  910. .set_link_ksettings = usbnet_set_link_ksettings,
  911. };
  912. /*-------------------------------------------------------------------------*/
  913. static void __handle_link_change(struct usbnet *dev)
  914. {
  915. if (!test_bit(EVENT_DEV_OPEN, &dev->flags))
  916. return;
  917. if (!netif_carrier_ok(dev->net)) {
  918. /* kill URBs for reading packets to save bus bandwidth */
  919. unlink_urbs(dev, &dev->rxq);
  920. /*
  921. * tx_timeout will unlink URBs for sending packets and
  922. * tx queue is stopped by netcore after link becomes off
  923. */
  924. } else {
  925. /* submitting URBs for reading packets */
  926. tasklet_schedule(&dev->bh);
  927. }
  928. /* hard_mtu or rx_urb_size may change during link change */
  929. usbnet_update_max_qlen(dev);
  930. clear_bit(EVENT_LINK_CHANGE, &dev->flags);
  931. }
  932. static void usbnet_set_rx_mode(struct net_device *net)
  933. {
  934. struct usbnet *dev = netdev_priv(net);
  935. usbnet_defer_kevent(dev, EVENT_SET_RX_MODE);
  936. }
  937. static void __handle_set_rx_mode(struct usbnet *dev)
  938. {
  939. if (dev->driver_info->set_rx_mode)
  940. (dev->driver_info->set_rx_mode)(dev);
  941. clear_bit(EVENT_SET_RX_MODE, &dev->flags);
  942. }
  943. /* work that cannot be done in interrupt context uses keventd.
  944. *
  945. * NOTE: with 2.5 we could do more of this using completion callbacks,
  946. * especially now that control transfers can be queued.
  947. */
  948. static void
  949. usbnet_deferred_kevent (struct work_struct *work)
  950. {
  951. struct usbnet *dev =
  952. container_of(work, struct usbnet, kevent);
  953. int status;
  954. /* usb_clear_halt() needs a thread context */
  955. if (test_bit (EVENT_TX_HALT, &dev->flags)) {
  956. unlink_urbs (dev, &dev->txq);
  957. status = usb_autopm_get_interface(dev->intf);
  958. if (status < 0)
  959. goto fail_pipe;
  960. status = usb_clear_halt (dev->udev, dev->out);
  961. usb_autopm_put_interface(dev->intf);
  962. if (status < 0 &&
  963. status != -EPIPE &&
  964. status != -ESHUTDOWN) {
  965. if (netif_msg_tx_err (dev))
  966. fail_pipe:
  967. netdev_err(dev->net, "can't clear tx halt, status %d\n",
  968. status);
  969. } else {
  970. clear_bit (EVENT_TX_HALT, &dev->flags);
  971. if (status != -ESHUTDOWN)
  972. netif_wake_queue (dev->net);
  973. }
  974. }
  975. if (test_bit (EVENT_RX_HALT, &dev->flags)) {
  976. unlink_urbs (dev, &dev->rxq);
  977. status = usb_autopm_get_interface(dev->intf);
  978. if (status < 0)
  979. goto fail_halt;
  980. status = usb_clear_halt (dev->udev, dev->in);
  981. usb_autopm_put_interface(dev->intf);
  982. if (status < 0 &&
  983. status != -EPIPE &&
  984. status != -ESHUTDOWN) {
  985. if (netif_msg_rx_err (dev))
  986. fail_halt:
  987. netdev_err(dev->net, "can't clear rx halt, status %d\n",
  988. status);
  989. } else {
  990. clear_bit (EVENT_RX_HALT, &dev->flags);
  991. tasklet_schedule (&dev->bh);
  992. }
  993. }
  994. /* tasklet could resubmit itself forever if memory is tight */
  995. if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
  996. struct urb *urb = NULL;
  997. int resched = 1;
  998. if (netif_running (dev->net))
  999. urb = usb_alloc_urb (0, GFP_KERNEL);
  1000. else
  1001. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  1002. if (urb != NULL) {
  1003. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  1004. status = usb_autopm_get_interface(dev->intf);
  1005. if (status < 0) {
  1006. usb_free_urb(urb);
  1007. goto fail_lowmem;
  1008. }
  1009. if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
  1010. resched = 0;
  1011. usb_autopm_put_interface(dev->intf);
  1012. fail_lowmem:
  1013. if (resched)
  1014. tasklet_schedule (&dev->bh);
  1015. }
  1016. }
  1017. if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
  1018. struct driver_info *info = dev->driver_info;
  1019. int retval = 0;
  1020. clear_bit (EVENT_LINK_RESET, &dev->flags);
  1021. status = usb_autopm_get_interface(dev->intf);
  1022. if (status < 0)
  1023. goto skip_reset;
  1024. if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
  1025. usb_autopm_put_interface(dev->intf);
  1026. skip_reset:
  1027. netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
  1028. retval,
  1029. dev->udev->bus->bus_name,
  1030. dev->udev->devpath,
  1031. info->description);
  1032. } else {
  1033. usb_autopm_put_interface(dev->intf);
  1034. }
  1035. /* handle link change from link resetting */
  1036. __handle_link_change(dev);
  1037. }
  1038. if (test_bit (EVENT_LINK_CHANGE, &dev->flags))
  1039. __handle_link_change(dev);
  1040. if (test_bit (EVENT_SET_RX_MODE, &dev->flags))
  1041. __handle_set_rx_mode(dev);
  1042. if (dev->flags)
  1043. netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
  1044. }
  1045. /*-------------------------------------------------------------------------*/
  1046. static void tx_complete (struct urb *urb)
  1047. {
  1048. struct sk_buff *skb = (struct sk_buff *) urb->context;
  1049. struct skb_data *entry = (struct skb_data *) skb->cb;
  1050. struct usbnet *dev = entry->dev;
  1051. if (urb->status == 0) {
  1052. struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->stats64);
  1053. u64_stats_update_begin(&stats64->syncp);
  1054. stats64->tx_packets += entry->packets;
  1055. stats64->tx_bytes += entry->length;
  1056. u64_stats_update_end(&stats64->syncp);
  1057. } else {
  1058. dev->net->stats.tx_errors++;
  1059. switch (urb->status) {
  1060. case -EPIPE:
  1061. usbnet_defer_kevent (dev, EVENT_TX_HALT);
  1062. break;
  1063. /* software-driven interface shutdown */
  1064. case -ECONNRESET: // async unlink
  1065. case -ESHUTDOWN: // hardware gone
  1066. break;
  1067. /* like rx, tx gets controller i/o faults during hub_wq
  1068. * delays and so it uses the same throttling mechanism.
  1069. */
  1070. case -EPROTO:
  1071. case -ETIME:
  1072. case -EILSEQ:
  1073. usb_mark_last_busy(dev->udev);
  1074. if (!timer_pending (&dev->delay)) {
  1075. mod_timer (&dev->delay,
  1076. jiffies + THROTTLE_JIFFIES);
  1077. netif_dbg(dev, link, dev->net,
  1078. "tx throttle %d\n", urb->status);
  1079. }
  1080. netif_stop_queue (dev->net);
  1081. break;
  1082. default:
  1083. netif_dbg(dev, tx_err, dev->net,
  1084. "tx err %d\n", entry->urb->status);
  1085. break;
  1086. }
  1087. }
  1088. usb_autopm_put_interface_async(dev->intf);
  1089. (void) defer_bh(dev, skb, &dev->txq, tx_done);
  1090. }
  1091. /*-------------------------------------------------------------------------*/
  1092. void usbnet_tx_timeout (struct net_device *net)
  1093. {
  1094. struct usbnet *dev = netdev_priv(net);
  1095. unlink_urbs (dev, &dev->txq);
  1096. tasklet_schedule (&dev->bh);
  1097. /* this needs to be handled individually because the generic layer
  1098. * doesn't know what is sufficient and could not restore private
  1099. * information if a remedy of an unconditional reset were used.
  1100. */
  1101. if (dev->driver_info->recover)
  1102. (dev->driver_info->recover)(dev);
  1103. }
  1104. EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
  1105. /*-------------------------------------------------------------------------*/
  1106. static int build_dma_sg(const struct sk_buff *skb, struct urb *urb)
  1107. {
  1108. unsigned num_sgs, total_len = 0;
  1109. int i, s = 0;
  1110. num_sgs = skb_shinfo(skb)->nr_frags + 1;
  1111. if (num_sgs == 1)
  1112. return 0;
  1113. /* reserve one for zero packet */
  1114. urb->sg = kmalloc((num_sgs + 1) * sizeof(struct scatterlist),
  1115. GFP_ATOMIC);
  1116. if (!urb->sg)
  1117. return -ENOMEM;
  1118. urb->num_sgs = num_sgs;
  1119. sg_init_table(urb->sg, urb->num_sgs + 1);
  1120. sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb));
  1121. total_len += skb_headlen(skb);
  1122. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1123. struct skb_frag_struct *f = &skb_shinfo(skb)->frags[i];
  1124. total_len += skb_frag_size(f);
  1125. sg_set_page(&urb->sg[i + s], f->page.p, f->size,
  1126. f->page_offset);
  1127. }
  1128. urb->transfer_buffer_length = total_len;
  1129. return 1;
  1130. }
  1131. netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
  1132. struct net_device *net)
  1133. {
  1134. struct usbnet *dev = netdev_priv(net);
  1135. unsigned int length;
  1136. struct urb *urb = NULL;
  1137. struct skb_data *entry;
  1138. struct driver_info *info = dev->driver_info;
  1139. unsigned long flags;
  1140. int retval;
  1141. if (skb)
  1142. skb_tx_timestamp(skb);
  1143. // some devices want funky USB-level framing, for
  1144. // win32 driver (usually) and/or hardware quirks
  1145. if (info->tx_fixup) {
  1146. skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
  1147. if (!skb) {
  1148. /* packet collected; minidriver waiting for more */
  1149. if (info->flags & FLAG_MULTI_PACKET)
  1150. goto not_drop;
  1151. netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
  1152. goto drop;
  1153. }
  1154. }
  1155. if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
  1156. netif_dbg(dev, tx_err, dev->net, "no urb\n");
  1157. goto drop;
  1158. }
  1159. entry = (struct skb_data *) skb->cb;
  1160. entry->urb = urb;
  1161. entry->dev = dev;
  1162. usb_fill_bulk_urb (urb, dev->udev, dev->out,
  1163. skb->data, skb->len, tx_complete, skb);
  1164. if (dev->can_dma_sg) {
  1165. if (build_dma_sg(skb, urb) < 0)
  1166. goto drop;
  1167. }
  1168. length = urb->transfer_buffer_length;
  1169. /* don't assume the hardware handles USB_ZERO_PACKET
  1170. * NOTE: strictly conforming cdc-ether devices should expect
  1171. * the ZLP here, but ignore the one-byte packet.
  1172. * NOTE2: CDC NCM specification is different from CDC ECM when
  1173. * handling ZLP/short packets, so cdc_ncm driver will make short
  1174. * packet itself if needed.
  1175. */
  1176. if (length % dev->maxpacket == 0) {
  1177. if (!(info->flags & FLAG_SEND_ZLP)) {
  1178. if (!(info->flags & FLAG_MULTI_PACKET)) {
  1179. length++;
  1180. if (skb_tailroom(skb) && !urb->num_sgs) {
  1181. skb->data[skb->len] = 0;
  1182. __skb_put(skb, 1);
  1183. } else if (urb->num_sgs)
  1184. sg_set_buf(&urb->sg[urb->num_sgs++],
  1185. dev->padding_pkt, 1);
  1186. }
  1187. } else
  1188. urb->transfer_flags |= URB_ZERO_PACKET;
  1189. }
  1190. urb->transfer_buffer_length = length;
  1191. if (info->flags & FLAG_MULTI_PACKET) {
  1192. /* Driver has set number of packets and a length delta.
  1193. * Calculate the complete length and ensure that it's
  1194. * positive.
  1195. */
  1196. entry->length += length;
  1197. if (WARN_ON_ONCE(entry->length <= 0))
  1198. entry->length = length;
  1199. } else {
  1200. usbnet_set_skb_tx_stats(skb, 1, length);
  1201. }
  1202. spin_lock_irqsave(&dev->txq.lock, flags);
  1203. retval = usb_autopm_get_interface_async(dev->intf);
  1204. if (retval < 0) {
  1205. spin_unlock_irqrestore(&dev->txq.lock, flags);
  1206. goto drop;
  1207. }
  1208. #ifdef CONFIG_PM
  1209. /* if this triggers the device is still a sleep */
  1210. if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
  1211. /* transmission will be done in resume */
  1212. usb_anchor_urb(urb, &dev->deferred);
  1213. /* no use to process more packets */
  1214. netif_stop_queue(net);
  1215. usb_put_urb(urb);
  1216. spin_unlock_irqrestore(&dev->txq.lock, flags);
  1217. netdev_dbg(dev->net, "Delaying transmission for resumption\n");
  1218. goto deferred;
  1219. }
  1220. #endif
  1221. switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
  1222. case -EPIPE:
  1223. netif_stop_queue (net);
  1224. usbnet_defer_kevent (dev, EVENT_TX_HALT);
  1225. usb_autopm_put_interface_async(dev->intf);
  1226. break;
  1227. default:
  1228. usb_autopm_put_interface_async(dev->intf);
  1229. netif_dbg(dev, tx_err, dev->net,
  1230. "tx: submit urb err %d\n", retval);
  1231. break;
  1232. case 0:
  1233. netif_trans_update(net);
  1234. __usbnet_queue_skb(&dev->txq, skb, tx_start);
  1235. if (dev->txq.qlen >= TX_QLEN (dev))
  1236. netif_stop_queue (net);
  1237. }
  1238. spin_unlock_irqrestore (&dev->txq.lock, flags);
  1239. if (retval) {
  1240. netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
  1241. drop:
  1242. dev->net->stats.tx_dropped++;
  1243. not_drop:
  1244. if (skb)
  1245. dev_kfree_skb_any (skb);
  1246. if (urb) {
  1247. kfree(urb->sg);
  1248. usb_free_urb(urb);
  1249. }
  1250. } else
  1251. netif_dbg(dev, tx_queued, dev->net,
  1252. "> tx, len %u, type 0x%x\n", length, skb->protocol);
  1253. #ifdef CONFIG_PM
  1254. deferred:
  1255. #endif
  1256. return NETDEV_TX_OK;
  1257. }
  1258. EXPORT_SYMBOL_GPL(usbnet_start_xmit);
  1259. static int rx_alloc_submit(struct usbnet *dev, gfp_t flags)
  1260. {
  1261. struct urb *urb;
  1262. int i;
  1263. int ret = 0;
  1264. /* don't refill the queue all at once */
  1265. for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) {
  1266. urb = usb_alloc_urb(0, flags);
  1267. if (urb != NULL) {
  1268. ret = rx_submit(dev, urb, flags);
  1269. if (ret)
  1270. goto err;
  1271. } else {
  1272. ret = -ENOMEM;
  1273. goto err;
  1274. }
  1275. }
  1276. err:
  1277. return ret;
  1278. }
  1279. /*-------------------------------------------------------------------------*/
  1280. // tasklet (work deferred from completions, in_irq) or timer
  1281. static void usbnet_bh (unsigned long param)
  1282. {
  1283. struct usbnet *dev = (struct usbnet *) param;
  1284. struct sk_buff *skb;
  1285. struct skb_data *entry;
  1286. while ((skb = skb_dequeue (&dev->done))) {
  1287. entry = (struct skb_data *) skb->cb;
  1288. switch (entry->state) {
  1289. case rx_done:
  1290. entry->state = rx_cleanup;
  1291. rx_process (dev, skb);
  1292. continue;
  1293. case tx_done:
  1294. kfree(entry->urb->sg);
  1295. case rx_cleanup:
  1296. usb_free_urb (entry->urb);
  1297. dev_kfree_skb (skb);
  1298. continue;
  1299. default:
  1300. netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
  1301. }
  1302. }
  1303. /* restart RX again after disabling due to high error rate */
  1304. clear_bit(EVENT_RX_KILL, &dev->flags);
  1305. /* waiting for all pending urbs to complete?
  1306. * only then can we forgo submitting anew
  1307. */
  1308. if (waitqueue_active(&dev->wait)) {
  1309. if (dev->txq.qlen + dev->rxq.qlen + dev->done.qlen == 0)
  1310. wake_up_all(&dev->wait);
  1311. // or are we maybe short a few urbs?
  1312. } else if (netif_running (dev->net) &&
  1313. netif_device_present (dev->net) &&
  1314. netif_carrier_ok(dev->net) &&
  1315. !timer_pending(&dev->delay) &&
  1316. !test_bit(EVENT_RX_PAUSED, &dev->flags) &&
  1317. !test_bit(EVENT_RX_HALT, &dev->flags)) {
  1318. int temp = dev->rxq.qlen;
  1319. if (temp < RX_QLEN(dev)) {
  1320. if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK)
  1321. return;
  1322. if (temp != dev->rxq.qlen)
  1323. netif_dbg(dev, link, dev->net,
  1324. "rxqlen %d --> %d\n",
  1325. temp, dev->rxq.qlen);
  1326. if (dev->rxq.qlen < RX_QLEN(dev))
  1327. tasklet_schedule (&dev->bh);
  1328. }
  1329. if (dev->txq.qlen < TX_QLEN (dev))
  1330. netif_wake_queue (dev->net);
  1331. }
  1332. }
  1333. /*-------------------------------------------------------------------------
  1334. *
  1335. * USB Device Driver support
  1336. *
  1337. *-------------------------------------------------------------------------*/
  1338. // precondition: never called in_interrupt
  1339. void usbnet_disconnect (struct usb_interface *intf)
  1340. {
  1341. struct usbnet *dev;
  1342. struct usb_device *xdev;
  1343. struct net_device *net;
  1344. dev = usb_get_intfdata(intf);
  1345. usb_set_intfdata(intf, NULL);
  1346. if (!dev)
  1347. return;
  1348. xdev = interface_to_usbdev (intf);
  1349. netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
  1350. intf->dev.driver->name,
  1351. xdev->bus->bus_name, xdev->devpath,
  1352. dev->driver_info->description);
  1353. net = dev->net;
  1354. unregister_netdev (net);
  1355. cancel_work_sync(&dev->kevent);
  1356. usb_scuttle_anchored_urbs(&dev->deferred);
  1357. if (dev->driver_info->unbind)
  1358. dev->driver_info->unbind (dev, intf);
  1359. usb_kill_urb(dev->interrupt);
  1360. usb_free_urb(dev->interrupt);
  1361. kfree(dev->padding_pkt);
  1362. free_percpu(dev->stats64);
  1363. free_netdev(net);
  1364. }
  1365. EXPORT_SYMBOL_GPL(usbnet_disconnect);
  1366. static const struct net_device_ops usbnet_netdev_ops = {
  1367. .ndo_open = usbnet_open,
  1368. .ndo_stop = usbnet_stop,
  1369. .ndo_start_xmit = usbnet_start_xmit,
  1370. .ndo_tx_timeout = usbnet_tx_timeout,
  1371. .ndo_set_rx_mode = usbnet_set_rx_mode,
  1372. .ndo_change_mtu = usbnet_change_mtu,
  1373. .ndo_get_stats64 = usbnet_get_stats64,
  1374. .ndo_set_mac_address = eth_mac_addr,
  1375. .ndo_validate_addr = eth_validate_addr,
  1376. };
  1377. /*-------------------------------------------------------------------------*/
  1378. // precondition: never called in_interrupt
  1379. static struct device_type wlan_type = {
  1380. .name = "wlan",
  1381. };
  1382. static struct device_type wwan_type = {
  1383. .name = "wwan",
  1384. };
  1385. int
  1386. usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
  1387. {
  1388. struct usbnet *dev;
  1389. struct net_device *net;
  1390. struct usb_host_interface *interface;
  1391. struct driver_info *info;
  1392. struct usb_device *xdev;
  1393. int status;
  1394. const char *name;
  1395. struct usb_driver *driver = to_usb_driver(udev->dev.driver);
  1396. /* usbnet already took usb runtime pm, so have to enable the feature
  1397. * for usb interface, otherwise usb_autopm_get_interface may return
  1398. * failure if RUNTIME_PM is enabled.
  1399. */
  1400. if (!driver->supports_autosuspend) {
  1401. driver->supports_autosuspend = 1;
  1402. pm_runtime_enable(&udev->dev);
  1403. }
  1404. name = udev->dev.driver->name;
  1405. info = (struct driver_info *) prod->driver_info;
  1406. if (!info) {
  1407. dev_dbg (&udev->dev, "blacklisted by %s\n", name);
  1408. return -ENODEV;
  1409. }
  1410. xdev = interface_to_usbdev (udev);
  1411. interface = udev->cur_altsetting;
  1412. status = -ENOMEM;
  1413. // set up our own records
  1414. net = alloc_etherdev(sizeof(*dev));
  1415. if (!net)
  1416. goto out;
  1417. /* netdev_printk() needs this so do it as early as possible */
  1418. SET_NETDEV_DEV(net, &udev->dev);
  1419. dev = netdev_priv(net);
  1420. dev->udev = xdev;
  1421. dev->intf = udev;
  1422. dev->driver_info = info;
  1423. dev->driver_name = name;
  1424. dev->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1425. if (!dev->stats64)
  1426. goto out0;
  1427. dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
  1428. | NETIF_MSG_PROBE | NETIF_MSG_LINK);
  1429. init_waitqueue_head(&dev->wait);
  1430. skb_queue_head_init (&dev->rxq);
  1431. skb_queue_head_init (&dev->txq);
  1432. skb_queue_head_init (&dev->done);
  1433. skb_queue_head_init(&dev->rxq_pause);
  1434. dev->bh.func = usbnet_bh;
  1435. dev->bh.data = (unsigned long) dev;
  1436. INIT_WORK (&dev->kevent, usbnet_deferred_kevent);
  1437. init_usb_anchor(&dev->deferred);
  1438. dev->delay.function = usbnet_bh;
  1439. dev->delay.data = (unsigned long) dev;
  1440. init_timer (&dev->delay);
  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");