usbnet.c 59 KB

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