usbnet.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191
  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. if (test_bit(EVENT_NO_IP_ALIGN, &dev->flags))
  415. skb = __netdev_alloc_skb(dev->net, size, flags);
  416. else
  417. skb = __netdev_alloc_skb_ip_align(dev->net, size, flags);
  418. if (!skb) {
  419. netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
  420. usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
  421. usb_free_urb (urb);
  422. return -ENOMEM;
  423. }
  424. entry = (struct skb_data *) skb->cb;
  425. entry->urb = urb;
  426. entry->dev = dev;
  427. entry->length = 0;
  428. usb_fill_bulk_urb (urb, dev->udev, dev->in,
  429. skb->data, size, rx_complete, skb);
  430. spin_lock_irqsave (&dev->rxq.lock, lockflags);
  431. if (netif_running (dev->net) &&
  432. netif_device_present (dev->net) &&
  433. !test_bit (EVENT_RX_HALT, &dev->flags) &&
  434. !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
  435. switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
  436. case -EPIPE:
  437. usbnet_defer_kevent (dev, EVENT_RX_HALT);
  438. break;
  439. case -ENOMEM:
  440. usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
  441. break;
  442. case -ENODEV:
  443. netif_dbg(dev, ifdown, dev->net, "device gone\n");
  444. netif_device_detach (dev->net);
  445. break;
  446. case -EHOSTUNREACH:
  447. retval = -ENOLINK;
  448. break;
  449. default:
  450. netif_dbg(dev, rx_err, dev->net,
  451. "rx submit, %d\n", retval);
  452. tasklet_schedule (&dev->bh);
  453. break;
  454. case 0:
  455. __usbnet_queue_skb(&dev->rxq, skb, rx_start);
  456. }
  457. } else {
  458. netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
  459. retval = -ENOLINK;
  460. }
  461. spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
  462. if (retval) {
  463. dev_kfree_skb_any (skb);
  464. usb_free_urb (urb);
  465. }
  466. return retval;
  467. }
  468. /*-------------------------------------------------------------------------*/
  469. static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
  470. {
  471. if (dev->driver_info->rx_fixup &&
  472. !dev->driver_info->rx_fixup (dev, skb)) {
  473. /* With RX_ASSEMBLE, rx_fixup() must update counters */
  474. if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE))
  475. dev->net->stats.rx_errors++;
  476. goto done;
  477. }
  478. // else network stack removes extra byte if we forced a short packet
  479. /* all data was already cloned from skb inside the driver */
  480. if (dev->driver_info->flags & FLAG_MULTI_PACKET)
  481. goto done;
  482. if (skb->len < ETH_HLEN) {
  483. dev->net->stats.rx_errors++;
  484. dev->net->stats.rx_length_errors++;
  485. netif_dbg(dev, rx_err, dev->net, "rx length %d\n", skb->len);
  486. } else {
  487. usbnet_skb_return(dev, skb);
  488. return;
  489. }
  490. done:
  491. skb_queue_tail(&dev->done, skb);
  492. }
  493. /*-------------------------------------------------------------------------*/
  494. static void rx_complete (struct urb *urb)
  495. {
  496. struct sk_buff *skb = (struct sk_buff *) urb->context;
  497. struct skb_data *entry = (struct skb_data *) skb->cb;
  498. struct usbnet *dev = entry->dev;
  499. int urb_status = urb->status;
  500. enum skb_state state;
  501. skb_put (skb, urb->actual_length);
  502. state = rx_done;
  503. entry->urb = NULL;
  504. switch (urb_status) {
  505. /* success */
  506. case 0:
  507. break;
  508. /* stalls need manual reset. this is rare ... except that
  509. * when going through USB 2.0 TTs, unplug appears this way.
  510. * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
  511. * storm, recovering as needed.
  512. */
  513. case -EPIPE:
  514. dev->net->stats.rx_errors++;
  515. usbnet_defer_kevent (dev, EVENT_RX_HALT);
  516. // FALLTHROUGH
  517. /* software-driven interface shutdown */
  518. case -ECONNRESET: /* async unlink */
  519. case -ESHUTDOWN: /* hardware gone */
  520. netif_dbg(dev, ifdown, dev->net,
  521. "rx shutdown, code %d\n", urb_status);
  522. goto block;
  523. /* we get controller i/o faults during hub_wq disconnect() delays.
  524. * throttle down resubmits, to avoid log floods; just temporarily,
  525. * so we still recover when the fault isn't a hub_wq delay.
  526. */
  527. case -EPROTO:
  528. case -ETIME:
  529. case -EILSEQ:
  530. dev->net->stats.rx_errors++;
  531. if (!timer_pending (&dev->delay)) {
  532. mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
  533. netif_dbg(dev, link, dev->net,
  534. "rx throttle %d\n", urb_status);
  535. }
  536. block:
  537. state = rx_cleanup;
  538. entry->urb = urb;
  539. urb = NULL;
  540. break;
  541. /* data overrun ... flush fifo? */
  542. case -EOVERFLOW:
  543. dev->net->stats.rx_over_errors++;
  544. // FALLTHROUGH
  545. default:
  546. state = rx_cleanup;
  547. dev->net->stats.rx_errors++;
  548. netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
  549. break;
  550. }
  551. /* stop rx if packet error rate is high */
  552. if (++dev->pkt_cnt > 30) {
  553. dev->pkt_cnt = 0;
  554. dev->pkt_err = 0;
  555. } else {
  556. if (state == rx_cleanup)
  557. dev->pkt_err++;
  558. if (dev->pkt_err > 20)
  559. set_bit(EVENT_RX_KILL, &dev->flags);
  560. }
  561. state = defer_bh(dev, skb, &dev->rxq, state);
  562. if (urb) {
  563. if (netif_running (dev->net) &&
  564. !test_bit (EVENT_RX_HALT, &dev->flags) &&
  565. state != unlink_start) {
  566. rx_submit (dev, urb, GFP_ATOMIC);
  567. usb_mark_last_busy(dev->udev);
  568. return;
  569. }
  570. usb_free_urb (urb);
  571. }
  572. netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
  573. }
  574. /*-------------------------------------------------------------------------*/
  575. void usbnet_pause_rx(struct usbnet *dev)
  576. {
  577. set_bit(EVENT_RX_PAUSED, &dev->flags);
  578. netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
  579. }
  580. EXPORT_SYMBOL_GPL(usbnet_pause_rx);
  581. void usbnet_resume_rx(struct usbnet *dev)
  582. {
  583. struct sk_buff *skb;
  584. int num = 0;
  585. clear_bit(EVENT_RX_PAUSED, &dev->flags);
  586. while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
  587. usbnet_skb_return(dev, skb);
  588. num++;
  589. }
  590. tasklet_schedule(&dev->bh);
  591. netif_dbg(dev, rx_status, dev->net,
  592. "paused rx queue disabled, %d skbs requeued\n", num);
  593. }
  594. EXPORT_SYMBOL_GPL(usbnet_resume_rx);
  595. void usbnet_purge_paused_rxq(struct usbnet *dev)
  596. {
  597. skb_queue_purge(&dev->rxq_pause);
  598. }
  599. EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
  600. /*-------------------------------------------------------------------------*/
  601. // unlink pending rx/tx; completion handlers do all other cleanup
  602. static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
  603. {
  604. unsigned long flags;
  605. struct sk_buff *skb;
  606. int count = 0;
  607. spin_lock_irqsave (&q->lock, flags);
  608. while (!skb_queue_empty(q)) {
  609. struct skb_data *entry;
  610. struct urb *urb;
  611. int retval;
  612. skb_queue_walk(q, skb) {
  613. entry = (struct skb_data *) skb->cb;
  614. if (entry->state != unlink_start)
  615. goto found;
  616. }
  617. break;
  618. found:
  619. entry->state = unlink_start;
  620. urb = entry->urb;
  621. /*
  622. * Get reference count of the URB to avoid it to be
  623. * freed during usb_unlink_urb, which may trigger
  624. * use-after-free problem inside usb_unlink_urb since
  625. * usb_unlink_urb is always racing with .complete
  626. * handler(include defer_bh).
  627. */
  628. usb_get_urb(urb);
  629. spin_unlock_irqrestore(&q->lock, flags);
  630. // during some PM-driven resume scenarios,
  631. // these (async) unlinks complete immediately
  632. retval = usb_unlink_urb (urb);
  633. if (retval != -EINPROGRESS && retval != 0)
  634. netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
  635. else
  636. count++;
  637. usb_put_urb(urb);
  638. spin_lock_irqsave(&q->lock, flags);
  639. }
  640. spin_unlock_irqrestore (&q->lock, flags);
  641. return count;
  642. }
  643. // Flush all pending rx urbs
  644. // minidrivers may need to do this when the MTU changes
  645. void usbnet_unlink_rx_urbs(struct usbnet *dev)
  646. {
  647. if (netif_running(dev->net)) {
  648. (void) unlink_urbs (dev, &dev->rxq);
  649. tasklet_schedule(&dev->bh);
  650. }
  651. }
  652. EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
  653. /*-------------------------------------------------------------------------*/
  654. static void wait_skb_queue_empty(struct sk_buff_head *q)
  655. {
  656. unsigned long flags;
  657. spin_lock_irqsave(&q->lock, flags);
  658. while (!skb_queue_empty(q)) {
  659. spin_unlock_irqrestore(&q->lock, flags);
  660. schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
  661. set_current_state(TASK_UNINTERRUPTIBLE);
  662. spin_lock_irqsave(&q->lock, flags);
  663. }
  664. spin_unlock_irqrestore(&q->lock, flags);
  665. }
  666. // precondition: never called in_interrupt
  667. static void usbnet_terminate_urbs(struct usbnet *dev)
  668. {
  669. DECLARE_WAITQUEUE(wait, current);
  670. int temp;
  671. /* ensure there are no more active urbs */
  672. add_wait_queue(&dev->wait, &wait);
  673. set_current_state(TASK_UNINTERRUPTIBLE);
  674. temp = unlink_urbs(dev, &dev->txq) +
  675. unlink_urbs(dev, &dev->rxq);
  676. /* maybe wait for deletions to finish. */
  677. wait_skb_queue_empty(&dev->rxq);
  678. wait_skb_queue_empty(&dev->txq);
  679. wait_skb_queue_empty(&dev->done);
  680. netif_dbg(dev, ifdown, dev->net,
  681. "waited for %d urb completions\n", temp);
  682. set_current_state(TASK_RUNNING);
  683. remove_wait_queue(&dev->wait, &wait);
  684. }
  685. int usbnet_stop (struct net_device *net)
  686. {
  687. struct usbnet *dev = netdev_priv(net);
  688. struct driver_info *info = dev->driver_info;
  689. int retval, pm, mpn;
  690. clear_bit(EVENT_DEV_OPEN, &dev->flags);
  691. netif_stop_queue (net);
  692. netif_info(dev, ifdown, dev->net,
  693. "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
  694. net->stats.rx_packets, net->stats.tx_packets,
  695. net->stats.rx_errors, net->stats.tx_errors);
  696. /* to not race resume */
  697. pm = usb_autopm_get_interface(dev->intf);
  698. /* allow minidriver to stop correctly (wireless devices to turn off
  699. * radio etc) */
  700. if (info->stop) {
  701. retval = info->stop(dev);
  702. if (retval < 0)
  703. netif_info(dev, ifdown, dev->net,
  704. "stop fail (%d) usbnet usb-%s-%s, %s\n",
  705. retval,
  706. dev->udev->bus->bus_name, dev->udev->devpath,
  707. info->description);
  708. }
  709. if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
  710. usbnet_terminate_urbs(dev);
  711. usbnet_status_stop(dev);
  712. usbnet_purge_paused_rxq(dev);
  713. mpn = !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
  714. /* deferred work (task, timer, softirq) must also stop.
  715. * can't flush_scheduled_work() until we drop rtnl (later),
  716. * else workers could deadlock; so make workers a NOP.
  717. */
  718. dev->flags = 0;
  719. del_timer_sync (&dev->delay);
  720. tasklet_kill (&dev->bh);
  721. if (!pm)
  722. usb_autopm_put_interface(dev->intf);
  723. if (info->manage_power && mpn)
  724. info->manage_power(dev, 0);
  725. else
  726. usb_autopm_put_interface(dev->intf);
  727. return 0;
  728. }
  729. EXPORT_SYMBOL_GPL(usbnet_stop);
  730. /*-------------------------------------------------------------------------*/
  731. // posts reads, and enables write queuing
  732. // precondition: never called in_interrupt
  733. int usbnet_open (struct net_device *net)
  734. {
  735. struct usbnet *dev = netdev_priv(net);
  736. int retval;
  737. struct driver_info *info = dev->driver_info;
  738. if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
  739. netif_info(dev, ifup, dev->net,
  740. "resumption fail (%d) usbnet usb-%s-%s, %s\n",
  741. retval,
  742. dev->udev->bus->bus_name,
  743. dev->udev->devpath,
  744. info->description);
  745. goto done_nopm;
  746. }
  747. // put into "known safe" state
  748. if (info->reset && (retval = info->reset (dev)) < 0) {
  749. netif_info(dev, ifup, dev->net,
  750. "open reset fail (%d) usbnet usb-%s-%s, %s\n",
  751. retval,
  752. dev->udev->bus->bus_name,
  753. dev->udev->devpath,
  754. info->description);
  755. goto done;
  756. }
  757. /* hard_mtu or rx_urb_size may change in reset() */
  758. usbnet_update_max_qlen(dev);
  759. // insist peer be connected
  760. if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
  761. netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
  762. goto done;
  763. }
  764. /* start any status interrupt transfer */
  765. if (dev->interrupt) {
  766. retval = usbnet_status_start(dev, GFP_KERNEL);
  767. if (retval < 0) {
  768. netif_err(dev, ifup, dev->net,
  769. "intr submit %d\n", retval);
  770. goto done;
  771. }
  772. }
  773. set_bit(EVENT_DEV_OPEN, &dev->flags);
  774. netif_start_queue (net);
  775. netif_info(dev, ifup, dev->net,
  776. "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
  777. (int)RX_QLEN(dev), (int)TX_QLEN(dev),
  778. dev->net->mtu,
  779. (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
  780. (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
  781. (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
  782. (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
  783. (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
  784. "simple");
  785. /* reset rx error state */
  786. dev->pkt_cnt = 0;
  787. dev->pkt_err = 0;
  788. clear_bit(EVENT_RX_KILL, &dev->flags);
  789. // delay posting reads until we're fully open
  790. tasklet_schedule (&dev->bh);
  791. if (info->manage_power) {
  792. retval = info->manage_power(dev, 1);
  793. if (retval < 0) {
  794. retval = 0;
  795. set_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
  796. } else {
  797. usb_autopm_put_interface(dev->intf);
  798. }
  799. }
  800. return retval;
  801. done:
  802. usb_autopm_put_interface(dev->intf);
  803. done_nopm:
  804. return retval;
  805. }
  806. EXPORT_SYMBOL_GPL(usbnet_open);
  807. /*-------------------------------------------------------------------------*/
  808. /* ethtool methods; minidrivers may need to add some more, but
  809. * they'll probably want to use this base set.
  810. */
  811. int usbnet_get_link_ksettings(struct net_device *net,
  812. struct ethtool_link_ksettings *cmd)
  813. {
  814. struct usbnet *dev = netdev_priv(net);
  815. if (!dev->mii.mdio_read)
  816. return -EOPNOTSUPP;
  817. mii_ethtool_get_link_ksettings(&dev->mii, cmd);
  818. return 0;
  819. }
  820. EXPORT_SYMBOL_GPL(usbnet_get_link_ksettings);
  821. int usbnet_set_link_ksettings(struct net_device *net,
  822. const struct ethtool_link_ksettings *cmd)
  823. {
  824. struct usbnet *dev = netdev_priv(net);
  825. int retval;
  826. if (!dev->mii.mdio_write)
  827. return -EOPNOTSUPP;
  828. retval = mii_ethtool_set_link_ksettings(&dev->mii, cmd);
  829. /* link speed/duplex might have changed */
  830. if (dev->driver_info->link_reset)
  831. dev->driver_info->link_reset(dev);
  832. /* hard_mtu or rx_urb_size may change in link_reset() */
  833. usbnet_update_max_qlen(dev);
  834. return retval;
  835. }
  836. EXPORT_SYMBOL_GPL(usbnet_set_link_ksettings);
  837. void usbnet_get_stats64(struct net_device *net, struct rtnl_link_stats64 *stats)
  838. {
  839. struct usbnet *dev = netdev_priv(net);
  840. unsigned int start;
  841. int cpu;
  842. netdev_stats_to_stats64(stats, &net->stats);
  843. for_each_possible_cpu(cpu) {
  844. struct pcpu_sw_netstats *stats64;
  845. u64 rx_packets, rx_bytes;
  846. u64 tx_packets, tx_bytes;
  847. stats64 = per_cpu_ptr(dev->stats64, cpu);
  848. do {
  849. start = u64_stats_fetch_begin_irq(&stats64->syncp);
  850. rx_packets = stats64->rx_packets;
  851. rx_bytes = stats64->rx_bytes;
  852. tx_packets = stats64->tx_packets;
  853. tx_bytes = stats64->tx_bytes;
  854. } while (u64_stats_fetch_retry_irq(&stats64->syncp, start));
  855. stats->rx_packets += rx_packets;
  856. stats->rx_bytes += rx_bytes;
  857. stats->tx_packets += tx_packets;
  858. stats->tx_bytes += tx_bytes;
  859. }
  860. }
  861. EXPORT_SYMBOL_GPL(usbnet_get_stats64);
  862. u32 usbnet_get_link (struct net_device *net)
  863. {
  864. struct usbnet *dev = netdev_priv(net);
  865. /* If a check_connect is defined, return its result */
  866. if (dev->driver_info->check_connect)
  867. return dev->driver_info->check_connect (dev) == 0;
  868. /* if the device has mii operations, use those */
  869. if (dev->mii.mdio_read)
  870. return mii_link_ok(&dev->mii);
  871. /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
  872. return ethtool_op_get_link(net);
  873. }
  874. EXPORT_SYMBOL_GPL(usbnet_get_link);
  875. int usbnet_nway_reset(struct net_device *net)
  876. {
  877. struct usbnet *dev = netdev_priv(net);
  878. if (!dev->mii.mdio_write)
  879. return -EOPNOTSUPP;
  880. return mii_nway_restart(&dev->mii);
  881. }
  882. EXPORT_SYMBOL_GPL(usbnet_nway_reset);
  883. void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
  884. {
  885. struct usbnet *dev = netdev_priv(net);
  886. strlcpy (info->driver, dev->driver_name, sizeof info->driver);
  887. strlcpy (info->version, DRIVER_VERSION, sizeof info->version);
  888. strlcpy (info->fw_version, dev->driver_info->description,
  889. sizeof info->fw_version);
  890. usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
  891. }
  892. EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
  893. u32 usbnet_get_msglevel (struct net_device *net)
  894. {
  895. struct usbnet *dev = netdev_priv(net);
  896. return dev->msg_enable;
  897. }
  898. EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
  899. void usbnet_set_msglevel (struct net_device *net, u32 level)
  900. {
  901. struct usbnet *dev = netdev_priv(net);
  902. dev->msg_enable = level;
  903. }
  904. EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
  905. /* drivers may override default ethtool_ops in their bind() routine */
  906. static const struct ethtool_ops usbnet_ethtool_ops = {
  907. .get_link = usbnet_get_link,
  908. .nway_reset = usbnet_nway_reset,
  909. .get_drvinfo = usbnet_get_drvinfo,
  910. .get_msglevel = usbnet_get_msglevel,
  911. .set_msglevel = usbnet_set_msglevel,
  912. .get_ts_info = ethtool_op_get_ts_info,
  913. .get_link_ksettings = usbnet_get_link_ksettings,
  914. .set_link_ksettings = usbnet_set_link_ksettings,
  915. };
  916. /*-------------------------------------------------------------------------*/
  917. static void __handle_link_change(struct usbnet *dev)
  918. {
  919. if (!test_bit(EVENT_DEV_OPEN, &dev->flags))
  920. return;
  921. if (!netif_carrier_ok(dev->net)) {
  922. /* kill URBs for reading packets to save bus bandwidth */
  923. unlink_urbs(dev, &dev->rxq);
  924. /*
  925. * tx_timeout will unlink URBs for sending packets and
  926. * tx queue is stopped by netcore after link becomes off
  927. */
  928. } else {
  929. /* submitting URBs for reading packets */
  930. tasklet_schedule(&dev->bh);
  931. }
  932. /* hard_mtu or rx_urb_size may change during link change */
  933. usbnet_update_max_qlen(dev);
  934. clear_bit(EVENT_LINK_CHANGE, &dev->flags);
  935. }
  936. static void usbnet_set_rx_mode(struct net_device *net)
  937. {
  938. struct usbnet *dev = netdev_priv(net);
  939. usbnet_defer_kevent(dev, EVENT_SET_RX_MODE);
  940. }
  941. static void __handle_set_rx_mode(struct usbnet *dev)
  942. {
  943. if (dev->driver_info->set_rx_mode)
  944. (dev->driver_info->set_rx_mode)(dev);
  945. clear_bit(EVENT_SET_RX_MODE, &dev->flags);
  946. }
  947. /* work that cannot be done in interrupt context uses keventd.
  948. *
  949. * NOTE: with 2.5 we could do more of this using completion callbacks,
  950. * especially now that control transfers can be queued.
  951. */
  952. static void
  953. usbnet_deferred_kevent (struct work_struct *work)
  954. {
  955. struct usbnet *dev =
  956. container_of(work, struct usbnet, kevent);
  957. int status;
  958. /* usb_clear_halt() needs a thread context */
  959. if (test_bit (EVENT_TX_HALT, &dev->flags)) {
  960. unlink_urbs (dev, &dev->txq);
  961. status = usb_autopm_get_interface(dev->intf);
  962. if (status < 0)
  963. goto fail_pipe;
  964. status = usb_clear_halt (dev->udev, dev->out);
  965. usb_autopm_put_interface(dev->intf);
  966. if (status < 0 &&
  967. status != -EPIPE &&
  968. status != -ESHUTDOWN) {
  969. if (netif_msg_tx_err (dev))
  970. fail_pipe:
  971. netdev_err(dev->net, "can't clear tx halt, status %d\n",
  972. status);
  973. } else {
  974. clear_bit (EVENT_TX_HALT, &dev->flags);
  975. if (status != -ESHUTDOWN)
  976. netif_wake_queue (dev->net);
  977. }
  978. }
  979. if (test_bit (EVENT_RX_HALT, &dev->flags)) {
  980. unlink_urbs (dev, &dev->rxq);
  981. status = usb_autopm_get_interface(dev->intf);
  982. if (status < 0)
  983. goto fail_halt;
  984. status = usb_clear_halt (dev->udev, dev->in);
  985. usb_autopm_put_interface(dev->intf);
  986. if (status < 0 &&
  987. status != -EPIPE &&
  988. status != -ESHUTDOWN) {
  989. if (netif_msg_rx_err (dev))
  990. fail_halt:
  991. netdev_err(dev->net, "can't clear rx halt, status %d\n",
  992. status);
  993. } else {
  994. clear_bit (EVENT_RX_HALT, &dev->flags);
  995. tasklet_schedule (&dev->bh);
  996. }
  997. }
  998. /* tasklet could resubmit itself forever if memory is tight */
  999. if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
  1000. struct urb *urb = NULL;
  1001. int resched = 1;
  1002. if (netif_running (dev->net))
  1003. urb = usb_alloc_urb (0, GFP_KERNEL);
  1004. else
  1005. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  1006. if (urb != NULL) {
  1007. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  1008. status = usb_autopm_get_interface(dev->intf);
  1009. if (status < 0) {
  1010. usb_free_urb(urb);
  1011. goto fail_lowmem;
  1012. }
  1013. if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
  1014. resched = 0;
  1015. usb_autopm_put_interface(dev->intf);
  1016. fail_lowmem:
  1017. if (resched)
  1018. tasklet_schedule (&dev->bh);
  1019. }
  1020. }
  1021. if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
  1022. struct driver_info *info = dev->driver_info;
  1023. int retval = 0;
  1024. clear_bit (EVENT_LINK_RESET, &dev->flags);
  1025. status = usb_autopm_get_interface(dev->intf);
  1026. if (status < 0)
  1027. goto skip_reset;
  1028. if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
  1029. usb_autopm_put_interface(dev->intf);
  1030. skip_reset:
  1031. netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
  1032. retval,
  1033. dev->udev->bus->bus_name,
  1034. dev->udev->devpath,
  1035. info->description);
  1036. } else {
  1037. usb_autopm_put_interface(dev->intf);
  1038. }
  1039. /* handle link change from link resetting */
  1040. __handle_link_change(dev);
  1041. }
  1042. if (test_bit (EVENT_LINK_CHANGE, &dev->flags))
  1043. __handle_link_change(dev);
  1044. if (test_bit (EVENT_SET_RX_MODE, &dev->flags))
  1045. __handle_set_rx_mode(dev);
  1046. if (dev->flags)
  1047. netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
  1048. }
  1049. /*-------------------------------------------------------------------------*/
  1050. static void tx_complete (struct urb *urb)
  1051. {
  1052. struct sk_buff *skb = (struct sk_buff *) urb->context;
  1053. struct skb_data *entry = (struct skb_data *) skb->cb;
  1054. struct usbnet *dev = entry->dev;
  1055. if (urb->status == 0) {
  1056. struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->stats64);
  1057. u64_stats_update_begin(&stats64->syncp);
  1058. stats64->tx_packets += entry->packets;
  1059. stats64->tx_bytes += entry->length;
  1060. u64_stats_update_end(&stats64->syncp);
  1061. } else {
  1062. dev->net->stats.tx_errors++;
  1063. switch (urb->status) {
  1064. case -EPIPE:
  1065. usbnet_defer_kevent (dev, EVENT_TX_HALT);
  1066. break;
  1067. /* software-driven interface shutdown */
  1068. case -ECONNRESET: // async unlink
  1069. case -ESHUTDOWN: // hardware gone
  1070. break;
  1071. /* like rx, tx gets controller i/o faults during hub_wq
  1072. * delays and so it uses the same throttling mechanism.
  1073. */
  1074. case -EPROTO:
  1075. case -ETIME:
  1076. case -EILSEQ:
  1077. usb_mark_last_busy(dev->udev);
  1078. if (!timer_pending (&dev->delay)) {
  1079. mod_timer (&dev->delay,
  1080. jiffies + THROTTLE_JIFFIES);
  1081. netif_dbg(dev, link, dev->net,
  1082. "tx throttle %d\n", urb->status);
  1083. }
  1084. netif_stop_queue (dev->net);
  1085. break;
  1086. default:
  1087. netif_dbg(dev, tx_err, dev->net,
  1088. "tx err %d\n", entry->urb->status);
  1089. break;
  1090. }
  1091. }
  1092. usb_autopm_put_interface_async(dev->intf);
  1093. (void) defer_bh(dev, skb, &dev->txq, tx_done);
  1094. }
  1095. /*-------------------------------------------------------------------------*/
  1096. void usbnet_tx_timeout (struct net_device *net)
  1097. {
  1098. struct usbnet *dev = netdev_priv(net);
  1099. unlink_urbs (dev, &dev->txq);
  1100. tasklet_schedule (&dev->bh);
  1101. /* this needs to be handled individually because the generic layer
  1102. * doesn't know what is sufficient and could not restore private
  1103. * information if a remedy of an unconditional reset were used.
  1104. */
  1105. if (dev->driver_info->recover)
  1106. (dev->driver_info->recover)(dev);
  1107. }
  1108. EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
  1109. /*-------------------------------------------------------------------------*/
  1110. static int build_dma_sg(const struct sk_buff *skb, struct urb *urb)
  1111. {
  1112. unsigned num_sgs, total_len = 0;
  1113. int i, s = 0;
  1114. num_sgs = skb_shinfo(skb)->nr_frags + 1;
  1115. if (num_sgs == 1)
  1116. return 0;
  1117. /* reserve one for zero packet */
  1118. urb->sg = kmalloc((num_sgs + 1) * sizeof(struct scatterlist),
  1119. GFP_ATOMIC);
  1120. if (!urb->sg)
  1121. return -ENOMEM;
  1122. urb->num_sgs = num_sgs;
  1123. sg_init_table(urb->sg, urb->num_sgs + 1);
  1124. sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb));
  1125. total_len += skb_headlen(skb);
  1126. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1127. struct skb_frag_struct *f = &skb_shinfo(skb)->frags[i];
  1128. total_len += skb_frag_size(f);
  1129. sg_set_page(&urb->sg[i + s], f->page.p, f->size,
  1130. f->page_offset);
  1131. }
  1132. urb->transfer_buffer_length = total_len;
  1133. return 1;
  1134. }
  1135. netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
  1136. struct net_device *net)
  1137. {
  1138. struct usbnet *dev = netdev_priv(net);
  1139. unsigned int length;
  1140. struct urb *urb = NULL;
  1141. struct skb_data *entry;
  1142. struct driver_info *info = dev->driver_info;
  1143. unsigned long flags;
  1144. int retval;
  1145. if (skb)
  1146. skb_tx_timestamp(skb);
  1147. // some devices want funky USB-level framing, for
  1148. // win32 driver (usually) and/or hardware quirks
  1149. if (info->tx_fixup) {
  1150. skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
  1151. if (!skb) {
  1152. /* packet collected; minidriver waiting for more */
  1153. if (info->flags & FLAG_MULTI_PACKET)
  1154. goto not_drop;
  1155. netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
  1156. goto drop;
  1157. }
  1158. }
  1159. if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
  1160. netif_dbg(dev, tx_err, dev->net, "no urb\n");
  1161. goto drop;
  1162. }
  1163. entry = (struct skb_data *) skb->cb;
  1164. entry->urb = urb;
  1165. entry->dev = dev;
  1166. usb_fill_bulk_urb (urb, dev->udev, dev->out,
  1167. skb->data, skb->len, tx_complete, skb);
  1168. if (dev->can_dma_sg) {
  1169. if (build_dma_sg(skb, urb) < 0)
  1170. goto drop;
  1171. }
  1172. length = urb->transfer_buffer_length;
  1173. /* don't assume the hardware handles USB_ZERO_PACKET
  1174. * NOTE: strictly conforming cdc-ether devices should expect
  1175. * the ZLP here, but ignore the one-byte packet.
  1176. * NOTE2: CDC NCM specification is different from CDC ECM when
  1177. * handling ZLP/short packets, so cdc_ncm driver will make short
  1178. * packet itself if needed.
  1179. */
  1180. if (length % dev->maxpacket == 0) {
  1181. if (!(info->flags & FLAG_SEND_ZLP)) {
  1182. if (!(info->flags & FLAG_MULTI_PACKET)) {
  1183. length++;
  1184. if (skb_tailroom(skb) && !urb->num_sgs) {
  1185. skb->data[skb->len] = 0;
  1186. __skb_put(skb, 1);
  1187. } else if (urb->num_sgs)
  1188. sg_set_buf(&urb->sg[urb->num_sgs++],
  1189. dev->padding_pkt, 1);
  1190. }
  1191. } else
  1192. urb->transfer_flags |= URB_ZERO_PACKET;
  1193. }
  1194. urb->transfer_buffer_length = length;
  1195. if (info->flags & FLAG_MULTI_PACKET) {
  1196. /* Driver has set number of packets and a length delta.
  1197. * Calculate the complete length and ensure that it's
  1198. * positive.
  1199. */
  1200. entry->length += length;
  1201. if (WARN_ON_ONCE(entry->length <= 0))
  1202. entry->length = length;
  1203. } else {
  1204. usbnet_set_skb_tx_stats(skb, 1, length);
  1205. }
  1206. spin_lock_irqsave(&dev->txq.lock, flags);
  1207. retval = usb_autopm_get_interface_async(dev->intf);
  1208. if (retval < 0) {
  1209. spin_unlock_irqrestore(&dev->txq.lock, flags);
  1210. goto drop;
  1211. }
  1212. #ifdef CONFIG_PM
  1213. /* if this triggers the device is still a sleep */
  1214. if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
  1215. /* transmission will be done in resume */
  1216. usb_anchor_urb(urb, &dev->deferred);
  1217. /* no use to process more packets */
  1218. netif_stop_queue(net);
  1219. usb_put_urb(urb);
  1220. spin_unlock_irqrestore(&dev->txq.lock, flags);
  1221. netdev_dbg(dev->net, "Delaying transmission for resumption\n");
  1222. goto deferred;
  1223. }
  1224. #endif
  1225. switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
  1226. case -EPIPE:
  1227. netif_stop_queue (net);
  1228. usbnet_defer_kevent (dev, EVENT_TX_HALT);
  1229. usb_autopm_put_interface_async(dev->intf);
  1230. break;
  1231. default:
  1232. usb_autopm_put_interface_async(dev->intf);
  1233. netif_dbg(dev, tx_err, dev->net,
  1234. "tx: submit urb err %d\n", retval);
  1235. break;
  1236. case 0:
  1237. netif_trans_update(net);
  1238. __usbnet_queue_skb(&dev->txq, skb, tx_start);
  1239. if (dev->txq.qlen >= TX_QLEN (dev))
  1240. netif_stop_queue (net);
  1241. }
  1242. spin_unlock_irqrestore (&dev->txq.lock, flags);
  1243. if (retval) {
  1244. netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
  1245. drop:
  1246. dev->net->stats.tx_dropped++;
  1247. not_drop:
  1248. if (skb)
  1249. dev_kfree_skb_any (skb);
  1250. if (urb) {
  1251. kfree(urb->sg);
  1252. usb_free_urb(urb);
  1253. }
  1254. } else
  1255. netif_dbg(dev, tx_queued, dev->net,
  1256. "> tx, len %u, type 0x%x\n", length, skb->protocol);
  1257. #ifdef CONFIG_PM
  1258. deferred:
  1259. #endif
  1260. return NETDEV_TX_OK;
  1261. }
  1262. EXPORT_SYMBOL_GPL(usbnet_start_xmit);
  1263. static int rx_alloc_submit(struct usbnet *dev, gfp_t flags)
  1264. {
  1265. struct urb *urb;
  1266. int i;
  1267. int ret = 0;
  1268. /* don't refill the queue all at once */
  1269. for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) {
  1270. urb = usb_alloc_urb(0, flags);
  1271. if (urb != NULL) {
  1272. ret = rx_submit(dev, urb, flags);
  1273. if (ret)
  1274. goto err;
  1275. } else {
  1276. ret = -ENOMEM;
  1277. goto err;
  1278. }
  1279. }
  1280. err:
  1281. return ret;
  1282. }
  1283. /*-------------------------------------------------------------------------*/
  1284. // tasklet (work deferred from completions, in_irq) or timer
  1285. static void usbnet_bh (struct timer_list *t)
  1286. {
  1287. struct usbnet *dev = from_timer(dev, t, delay);
  1288. struct sk_buff *skb;
  1289. struct skb_data *entry;
  1290. while ((skb = skb_dequeue (&dev->done))) {
  1291. entry = (struct skb_data *) skb->cb;
  1292. switch (entry->state) {
  1293. case rx_done:
  1294. entry->state = rx_cleanup;
  1295. rx_process (dev, skb);
  1296. continue;
  1297. case tx_done:
  1298. kfree(entry->urb->sg);
  1299. case rx_cleanup:
  1300. usb_free_urb (entry->urb);
  1301. dev_kfree_skb (skb);
  1302. continue;
  1303. default:
  1304. netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
  1305. }
  1306. }
  1307. /* restart RX again after disabling due to high error rate */
  1308. clear_bit(EVENT_RX_KILL, &dev->flags);
  1309. /* waiting for all pending urbs to complete?
  1310. * only then can we forgo submitting anew
  1311. */
  1312. if (waitqueue_active(&dev->wait)) {
  1313. if (dev->txq.qlen + dev->rxq.qlen + dev->done.qlen == 0)
  1314. wake_up_all(&dev->wait);
  1315. // or are we maybe short a few urbs?
  1316. } else if (netif_running (dev->net) &&
  1317. netif_device_present (dev->net) &&
  1318. netif_carrier_ok(dev->net) &&
  1319. !timer_pending(&dev->delay) &&
  1320. !test_bit(EVENT_RX_PAUSED, &dev->flags) &&
  1321. !test_bit(EVENT_RX_HALT, &dev->flags)) {
  1322. int temp = dev->rxq.qlen;
  1323. if (temp < RX_QLEN(dev)) {
  1324. if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK)
  1325. return;
  1326. if (temp != dev->rxq.qlen)
  1327. netif_dbg(dev, link, dev->net,
  1328. "rxqlen %d --> %d\n",
  1329. temp, dev->rxq.qlen);
  1330. if (dev->rxq.qlen < RX_QLEN(dev))
  1331. tasklet_schedule (&dev->bh);
  1332. }
  1333. if (dev->txq.qlen < TX_QLEN (dev))
  1334. netif_wake_queue (dev->net);
  1335. }
  1336. }
  1337. /*-------------------------------------------------------------------------
  1338. *
  1339. * USB Device Driver support
  1340. *
  1341. *-------------------------------------------------------------------------*/
  1342. // precondition: never called in_interrupt
  1343. void usbnet_disconnect (struct usb_interface *intf)
  1344. {
  1345. struct usbnet *dev;
  1346. struct usb_device *xdev;
  1347. struct net_device *net;
  1348. dev = usb_get_intfdata(intf);
  1349. usb_set_intfdata(intf, NULL);
  1350. if (!dev)
  1351. return;
  1352. xdev = interface_to_usbdev (intf);
  1353. netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
  1354. intf->dev.driver->name,
  1355. xdev->bus->bus_name, xdev->devpath,
  1356. dev->driver_info->description);
  1357. net = dev->net;
  1358. unregister_netdev (net);
  1359. cancel_work_sync(&dev->kevent);
  1360. usb_scuttle_anchored_urbs(&dev->deferred);
  1361. if (dev->driver_info->unbind)
  1362. dev->driver_info->unbind (dev, intf);
  1363. usb_kill_urb(dev->interrupt);
  1364. usb_free_urb(dev->interrupt);
  1365. kfree(dev->padding_pkt);
  1366. free_percpu(dev->stats64);
  1367. free_netdev(net);
  1368. }
  1369. EXPORT_SYMBOL_GPL(usbnet_disconnect);
  1370. static const struct net_device_ops usbnet_netdev_ops = {
  1371. .ndo_open = usbnet_open,
  1372. .ndo_stop = usbnet_stop,
  1373. .ndo_start_xmit = usbnet_start_xmit,
  1374. .ndo_tx_timeout = usbnet_tx_timeout,
  1375. .ndo_set_rx_mode = usbnet_set_rx_mode,
  1376. .ndo_change_mtu = usbnet_change_mtu,
  1377. .ndo_get_stats64 = usbnet_get_stats64,
  1378. .ndo_set_mac_address = eth_mac_addr,
  1379. .ndo_validate_addr = eth_validate_addr,
  1380. };
  1381. /*-------------------------------------------------------------------------*/
  1382. // precondition: never called in_interrupt
  1383. static struct device_type wlan_type = {
  1384. .name = "wlan",
  1385. };
  1386. static struct device_type wwan_type = {
  1387. .name = "wwan",
  1388. };
  1389. int
  1390. usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
  1391. {
  1392. struct usbnet *dev;
  1393. struct net_device *net;
  1394. struct usb_host_interface *interface;
  1395. struct driver_info *info;
  1396. struct usb_device *xdev;
  1397. int status;
  1398. const char *name;
  1399. struct usb_driver *driver = to_usb_driver(udev->dev.driver);
  1400. /* usbnet already took usb runtime pm, so have to enable the feature
  1401. * for usb interface, otherwise usb_autopm_get_interface may return
  1402. * failure if RUNTIME_PM is enabled.
  1403. */
  1404. if (!driver->supports_autosuspend) {
  1405. driver->supports_autosuspend = 1;
  1406. pm_runtime_enable(&udev->dev);
  1407. }
  1408. name = udev->dev.driver->name;
  1409. info = (struct driver_info *) prod->driver_info;
  1410. if (!info) {
  1411. dev_dbg (&udev->dev, "blacklisted by %s\n", name);
  1412. return -ENODEV;
  1413. }
  1414. xdev = interface_to_usbdev (udev);
  1415. interface = udev->cur_altsetting;
  1416. status = -ENOMEM;
  1417. // set up our own records
  1418. net = alloc_etherdev(sizeof(*dev));
  1419. if (!net)
  1420. goto out;
  1421. /* netdev_printk() needs this so do it as early as possible */
  1422. SET_NETDEV_DEV(net, &udev->dev);
  1423. dev = netdev_priv(net);
  1424. dev->udev = xdev;
  1425. dev->intf = udev;
  1426. dev->driver_info = info;
  1427. dev->driver_name = name;
  1428. dev->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1429. if (!dev->stats64)
  1430. goto out0;
  1431. dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
  1432. | NETIF_MSG_PROBE | NETIF_MSG_LINK);
  1433. init_waitqueue_head(&dev->wait);
  1434. skb_queue_head_init (&dev->rxq);
  1435. skb_queue_head_init (&dev->txq);
  1436. skb_queue_head_init (&dev->done);
  1437. skb_queue_head_init(&dev->rxq_pause);
  1438. dev->bh.func = (void (*)(unsigned long))usbnet_bh;
  1439. dev->bh.data = (unsigned long)&dev->delay;
  1440. INIT_WORK (&dev->kevent, usbnet_deferred_kevent);
  1441. init_usb_anchor(&dev->deferred);
  1442. timer_setup(&dev->delay, usbnet_bh, 0);
  1443. mutex_init (&dev->phy_mutex);
  1444. mutex_init(&dev->interrupt_mutex);
  1445. dev->interrupt_count = 0;
  1446. dev->net = net;
  1447. strcpy (net->name, "usb%d");
  1448. memcpy (net->dev_addr, node_id, sizeof node_id);
  1449. /* rx and tx sides can use different message sizes;
  1450. * bind() should set rx_urb_size in that case.
  1451. */
  1452. dev->hard_mtu = net->mtu + net->hard_header_len;
  1453. net->min_mtu = 0;
  1454. net->max_mtu = ETH_MAX_MTU;
  1455. net->netdev_ops = &usbnet_netdev_ops;
  1456. net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
  1457. net->ethtool_ops = &usbnet_ethtool_ops;
  1458. // allow device-specific bind/init procedures
  1459. // NOTE net->name still not usable ...
  1460. if (info->bind) {
  1461. status = info->bind (dev, udev);
  1462. if (status < 0)
  1463. goto out1;
  1464. // heuristic: "usb%d" for links we know are two-host,
  1465. // else "eth%d" when there's reasonable doubt. userspace
  1466. // can rename the link if it knows better.
  1467. if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
  1468. ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
  1469. (net->dev_addr [0] & 0x02) == 0))
  1470. strcpy (net->name, "eth%d");
  1471. /* WLAN devices should always be named "wlan%d" */
  1472. if ((dev->driver_info->flags & FLAG_WLAN) != 0)
  1473. strcpy(net->name, "wlan%d");
  1474. /* WWAN devices should always be named "wwan%d" */
  1475. if ((dev->driver_info->flags & FLAG_WWAN) != 0)
  1476. strcpy(net->name, "wwan%d");
  1477. /* devices that cannot do ARP */
  1478. if ((dev->driver_info->flags & FLAG_NOARP) != 0)
  1479. net->flags |= IFF_NOARP;
  1480. /* maybe the remote can't receive an Ethernet MTU */
  1481. if (net->mtu > (dev->hard_mtu - net->hard_header_len))
  1482. net->mtu = dev->hard_mtu - net->hard_header_len;
  1483. } else if (!info->in || !info->out)
  1484. status = usbnet_get_endpoints (dev, udev);
  1485. else {
  1486. dev->in = usb_rcvbulkpipe (xdev, info->in);
  1487. dev->out = usb_sndbulkpipe (xdev, info->out);
  1488. if (!(info->flags & FLAG_NO_SETINT))
  1489. status = usb_set_interface (xdev,
  1490. interface->desc.bInterfaceNumber,
  1491. interface->desc.bAlternateSetting);
  1492. else
  1493. status = 0;
  1494. }
  1495. if (status >= 0 && dev->status)
  1496. status = init_status (dev, udev);
  1497. if (status < 0)
  1498. goto out3;
  1499. if (!dev->rx_urb_size)
  1500. dev->rx_urb_size = dev->hard_mtu;
  1501. dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
  1502. /* let userspace know we have a random address */
  1503. if (ether_addr_equal(net->dev_addr, node_id))
  1504. net->addr_assign_type = NET_ADDR_RANDOM;
  1505. if ((dev->driver_info->flags & FLAG_WLAN) != 0)
  1506. SET_NETDEV_DEVTYPE(net, &wlan_type);
  1507. if ((dev->driver_info->flags & FLAG_WWAN) != 0)
  1508. SET_NETDEV_DEVTYPE(net, &wwan_type);
  1509. /* initialize max rx_qlen and tx_qlen */
  1510. usbnet_update_max_qlen(dev);
  1511. if (dev->can_dma_sg && !(info->flags & FLAG_SEND_ZLP) &&
  1512. !(info->flags & FLAG_MULTI_PACKET)) {
  1513. dev->padding_pkt = kzalloc(1, GFP_KERNEL);
  1514. if (!dev->padding_pkt) {
  1515. status = -ENOMEM;
  1516. goto out4;
  1517. }
  1518. }
  1519. status = register_netdev (net);
  1520. if (status)
  1521. goto out5;
  1522. netif_info(dev, probe, dev->net,
  1523. "register '%s' at usb-%s-%s, %s, %pM\n",
  1524. udev->dev.driver->name,
  1525. xdev->bus->bus_name, xdev->devpath,
  1526. dev->driver_info->description,
  1527. net->dev_addr);
  1528. // ok, it's ready to go.
  1529. usb_set_intfdata (udev, dev);
  1530. netif_device_attach (net);
  1531. if (dev->driver_info->flags & FLAG_LINK_INTR)
  1532. usbnet_link_change(dev, 0, 0);
  1533. return 0;
  1534. out5:
  1535. kfree(dev->padding_pkt);
  1536. out4:
  1537. usb_free_urb(dev->interrupt);
  1538. out3:
  1539. if (info->unbind)
  1540. info->unbind (dev, udev);
  1541. out1:
  1542. /* subdrivers must undo all they did in bind() if they
  1543. * fail it, but we may fail later and a deferred kevent
  1544. * may trigger an error resubmitting itself and, worse,
  1545. * schedule a timer. So we kill it all just in case.
  1546. */
  1547. cancel_work_sync(&dev->kevent);
  1548. del_timer_sync(&dev->delay);
  1549. free_percpu(dev->stats64);
  1550. out0:
  1551. free_netdev(net);
  1552. out:
  1553. return status;
  1554. }
  1555. EXPORT_SYMBOL_GPL(usbnet_probe);
  1556. /*-------------------------------------------------------------------------*/
  1557. /*
  1558. * suspend the whole driver as soon as the first interface is suspended
  1559. * resume only when the last interface is resumed
  1560. */
  1561. int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
  1562. {
  1563. struct usbnet *dev = usb_get_intfdata(intf);
  1564. if (!dev->suspend_count++) {
  1565. spin_lock_irq(&dev->txq.lock);
  1566. /* don't autosuspend while transmitting */
  1567. if (dev->txq.qlen && PMSG_IS_AUTO(message)) {
  1568. dev->suspend_count--;
  1569. spin_unlock_irq(&dev->txq.lock);
  1570. return -EBUSY;
  1571. } else {
  1572. set_bit(EVENT_DEV_ASLEEP, &dev->flags);
  1573. spin_unlock_irq(&dev->txq.lock);
  1574. }
  1575. /*
  1576. * accelerate emptying of the rx and queues, to avoid
  1577. * having everything error out.
  1578. */
  1579. netif_device_detach (dev->net);
  1580. usbnet_terminate_urbs(dev);
  1581. __usbnet_status_stop_force(dev);
  1582. /*
  1583. * reattach so runtime management can use and
  1584. * wake the device
  1585. */
  1586. netif_device_attach (dev->net);
  1587. }
  1588. return 0;
  1589. }
  1590. EXPORT_SYMBOL_GPL(usbnet_suspend);
  1591. int usbnet_resume (struct usb_interface *intf)
  1592. {
  1593. struct usbnet *dev = usb_get_intfdata(intf);
  1594. struct sk_buff *skb;
  1595. struct urb *res;
  1596. int retval;
  1597. if (!--dev->suspend_count) {
  1598. /* resume interrupt URB if it was previously submitted */
  1599. __usbnet_status_start_force(dev, GFP_NOIO);
  1600. spin_lock_irq(&dev->txq.lock);
  1601. while ((res = usb_get_from_anchor(&dev->deferred))) {
  1602. skb = (struct sk_buff *)res->context;
  1603. retval = usb_submit_urb(res, GFP_ATOMIC);
  1604. if (retval < 0) {
  1605. dev_kfree_skb_any(skb);
  1606. kfree(res->sg);
  1607. usb_free_urb(res);
  1608. usb_autopm_put_interface_async(dev->intf);
  1609. } else {
  1610. netif_trans_update(dev->net);
  1611. __skb_queue_tail(&dev->txq, skb);
  1612. }
  1613. }
  1614. smp_mb();
  1615. clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
  1616. spin_unlock_irq(&dev->txq.lock);
  1617. if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
  1618. /* handle remote wakeup ASAP
  1619. * we cannot race against stop
  1620. */
  1621. if (netif_device_present(dev->net) &&
  1622. !timer_pending(&dev->delay) &&
  1623. !test_bit(EVENT_RX_HALT, &dev->flags))
  1624. rx_alloc_submit(dev, GFP_NOIO);
  1625. if (!(dev->txq.qlen >= TX_QLEN(dev)))
  1626. netif_tx_wake_all_queues(dev->net);
  1627. tasklet_schedule (&dev->bh);
  1628. }
  1629. }
  1630. if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags))
  1631. usb_autopm_get_interface_no_resume(intf);
  1632. return 0;
  1633. }
  1634. EXPORT_SYMBOL_GPL(usbnet_resume);
  1635. /*
  1636. * Either a subdriver implements manage_power, then it is assumed to always
  1637. * be ready to be suspended or it reports the readiness to be suspended
  1638. * explicitly
  1639. */
  1640. void usbnet_device_suggests_idle(struct usbnet *dev)
  1641. {
  1642. if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) {
  1643. dev->intf->needs_remote_wakeup = 1;
  1644. usb_autopm_put_interface_async(dev->intf);
  1645. }
  1646. }
  1647. EXPORT_SYMBOL(usbnet_device_suggests_idle);
  1648. /*
  1649. * For devices that can do without special commands
  1650. */
  1651. int usbnet_manage_power(struct usbnet *dev, int on)
  1652. {
  1653. dev->intf->needs_remote_wakeup = on;
  1654. return 0;
  1655. }
  1656. EXPORT_SYMBOL(usbnet_manage_power);
  1657. void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset)
  1658. {
  1659. /* update link after link is reseted */
  1660. if (link && !need_reset)
  1661. netif_carrier_on(dev->net);
  1662. else
  1663. netif_carrier_off(dev->net);
  1664. if (need_reset && link)
  1665. usbnet_defer_kevent(dev, EVENT_LINK_RESET);
  1666. else
  1667. usbnet_defer_kevent(dev, EVENT_LINK_CHANGE);
  1668. }
  1669. EXPORT_SYMBOL(usbnet_link_change);
  1670. /*-------------------------------------------------------------------------*/
  1671. static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
  1672. u16 value, u16 index, void *data, u16 size)
  1673. {
  1674. void *buf = NULL;
  1675. int err = -ENOMEM;
  1676. netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x"
  1677. " value=0x%04x index=0x%04x size=%d\n",
  1678. cmd, reqtype, value, index, size);
  1679. if (size) {
  1680. buf = kmalloc(size, GFP_KERNEL);
  1681. if (!buf)
  1682. goto out;
  1683. }
  1684. err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
  1685. cmd, reqtype, value, index, buf, size,
  1686. USB_CTRL_GET_TIMEOUT);
  1687. if (err > 0 && err <= size) {
  1688. if (data)
  1689. memcpy(data, buf, err);
  1690. else
  1691. netdev_dbg(dev->net,
  1692. "Huh? Data requested but thrown away.\n");
  1693. }
  1694. kfree(buf);
  1695. out:
  1696. return err;
  1697. }
  1698. static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
  1699. u16 value, u16 index, const void *data,
  1700. u16 size)
  1701. {
  1702. void *buf = NULL;
  1703. int err = -ENOMEM;
  1704. netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
  1705. " value=0x%04x index=0x%04x size=%d\n",
  1706. cmd, reqtype, value, index, size);
  1707. if (data) {
  1708. buf = kmemdup(data, size, GFP_KERNEL);
  1709. if (!buf)
  1710. goto out;
  1711. } else {
  1712. if (size) {
  1713. WARN_ON_ONCE(1);
  1714. err = -EINVAL;
  1715. goto out;
  1716. }
  1717. }
  1718. err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
  1719. cmd, reqtype, value, index, buf, size,
  1720. USB_CTRL_SET_TIMEOUT);
  1721. kfree(buf);
  1722. out:
  1723. return err;
  1724. }
  1725. /*
  1726. * The function can't be called inside suspend/resume callback,
  1727. * otherwise deadlock will be caused.
  1728. */
  1729. int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
  1730. u16 value, u16 index, void *data, u16 size)
  1731. {
  1732. int ret;
  1733. if (usb_autopm_get_interface(dev->intf) < 0)
  1734. return -ENODEV;
  1735. ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index,
  1736. data, size);
  1737. usb_autopm_put_interface(dev->intf);
  1738. return ret;
  1739. }
  1740. EXPORT_SYMBOL_GPL(usbnet_read_cmd);
  1741. /*
  1742. * The function can't be called inside suspend/resume callback,
  1743. * otherwise deadlock will be caused.
  1744. */
  1745. int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
  1746. u16 value, u16 index, const void *data, u16 size)
  1747. {
  1748. int ret;
  1749. if (usb_autopm_get_interface(dev->intf) < 0)
  1750. return -ENODEV;
  1751. ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index,
  1752. data, size);
  1753. usb_autopm_put_interface(dev->intf);
  1754. return ret;
  1755. }
  1756. EXPORT_SYMBOL_GPL(usbnet_write_cmd);
  1757. /*
  1758. * The function can be called inside suspend/resume callback safely
  1759. * and should only be called by suspend/resume callback generally.
  1760. */
  1761. int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
  1762. u16 value, u16 index, void *data, u16 size)
  1763. {
  1764. return __usbnet_read_cmd(dev, cmd, reqtype, value, index,
  1765. data, size);
  1766. }
  1767. EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm);
  1768. /*
  1769. * The function can be called inside suspend/resume callback safely
  1770. * and should only be called by suspend/resume callback generally.
  1771. */
  1772. int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
  1773. u16 value, u16 index, const void *data,
  1774. u16 size)
  1775. {
  1776. return __usbnet_write_cmd(dev, cmd, reqtype, value, index,
  1777. data, size);
  1778. }
  1779. EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm);
  1780. static void usbnet_async_cmd_cb(struct urb *urb)
  1781. {
  1782. struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
  1783. int status = urb->status;
  1784. if (status < 0)
  1785. dev_dbg(&urb->dev->dev, "%s failed with %d",
  1786. __func__, status);
  1787. kfree(req);
  1788. usb_free_urb(urb);
  1789. }
  1790. /*
  1791. * The caller must make sure that device can't be put into suspend
  1792. * state until the control URB completes.
  1793. */
  1794. int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype,
  1795. u16 value, u16 index, const void *data, u16 size)
  1796. {
  1797. struct usb_ctrlrequest *req = NULL;
  1798. struct urb *urb;
  1799. int err = -ENOMEM;
  1800. void *buf = NULL;
  1801. netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
  1802. " value=0x%04x index=0x%04x size=%d\n",
  1803. cmd, reqtype, value, index, size);
  1804. urb = usb_alloc_urb(0, GFP_ATOMIC);
  1805. if (!urb)
  1806. goto fail;
  1807. if (data) {
  1808. buf = kmemdup(data, size, GFP_ATOMIC);
  1809. if (!buf) {
  1810. netdev_err(dev->net, "Error allocating buffer"
  1811. " in %s!\n", __func__);
  1812. goto fail_free;
  1813. }
  1814. }
  1815. req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
  1816. if (!req)
  1817. goto fail_free_buf;
  1818. req->bRequestType = reqtype;
  1819. req->bRequest = cmd;
  1820. req->wValue = cpu_to_le16(value);
  1821. req->wIndex = cpu_to_le16(index);
  1822. req->wLength = cpu_to_le16(size);
  1823. usb_fill_control_urb(urb, dev->udev,
  1824. usb_sndctrlpipe(dev->udev, 0),
  1825. (void *)req, buf, size,
  1826. usbnet_async_cmd_cb, req);
  1827. urb->transfer_flags |= URB_FREE_BUFFER;
  1828. err = usb_submit_urb(urb, GFP_ATOMIC);
  1829. if (err < 0) {
  1830. netdev_err(dev->net, "Error submitting the control"
  1831. " message: status=%d\n", err);
  1832. goto fail_free;
  1833. }
  1834. return 0;
  1835. fail_free_buf:
  1836. kfree(buf);
  1837. fail_free:
  1838. kfree(req);
  1839. usb_free_urb(urb);
  1840. fail:
  1841. return err;
  1842. }
  1843. EXPORT_SYMBOL_GPL(usbnet_write_cmd_async);
  1844. /*-------------------------------------------------------------------------*/
  1845. static int __init usbnet_init(void)
  1846. {
  1847. /* Compiler should optimize this out. */
  1848. BUILD_BUG_ON(
  1849. FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data));
  1850. eth_random_addr(node_id);
  1851. return 0;
  1852. }
  1853. module_init(usbnet_init);
  1854. static void __exit usbnet_exit(void)
  1855. {
  1856. }
  1857. module_exit(usbnet_exit);
  1858. MODULE_AUTHOR("David Brownell");
  1859. MODULE_DESCRIPTION("USB network driver framework");
  1860. MODULE_LICENSE("GPL");