usbnet.c 59 KB

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