usbnet.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  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, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /*
  21. * This is a generic "USB networking" framework that works with several
  22. * kinds of full and high speed networking devices: host-to-host cables,
  23. * smart usb peripherals, and actual Ethernet adapters.
  24. *
  25. * These devices usually differ in terms of control protocols (if they
  26. * even have one!) and sometimes they define new framing to wrap or batch
  27. * Ethernet packets. Otherwise, they talk to USB pretty much the same,
  28. * so interface (un)binding, endpoint I/O queues, fault handling, and other
  29. * issues can usefully be addressed by this framework.
  30. */
  31. // #define DEBUG // error path messages, extra info
  32. // #define VERBOSE // more; success messages
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/etherdevice.h>
  37. #include <linux/ctype.h>
  38. #include <linux/ethtool.h>
  39. #include <linux/workqueue.h>
  40. #include <linux/mii.h>
  41. #include <linux/usb.h>
  42. #include <linux/usb/usbnet.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. * REVISIT qlens should be members of 'struct usbnet'; the goal is to
  56. * let the USB host controller be busy for 5msec or more before an irq
  57. * is required, under load. Jumbograms change the equation.
  58. */
  59. #define RX_MAX_QUEUE_MEMORY (60 * 1518)
  60. #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
  61. (RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4)
  62. #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
  63. (RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4)
  64. // reawaken network queue this soon after stopping; else watchdog barks
  65. #define TX_TIMEOUT_JIFFIES (5*HZ)
  66. // throttle rx/tx briefly after some faults, so khubd might disconnect()
  67. // us (it polls at HZ/4 usually) before we report too many false errors.
  68. #define THROTTLE_JIFFIES (HZ/8)
  69. // between wakeups
  70. #define UNLINK_TIMEOUT_MS 3
  71. /*-------------------------------------------------------------------------*/
  72. // randomly generated ethernet address
  73. static u8 node_id [ETH_ALEN];
  74. static const char driver_name [] = "usbnet";
  75. struct workqueue_struct *usbnet_wq;
  76. static DECLARE_WAIT_QUEUE_HEAD(unlink_wakeup);
  77. /* use ethtool to change the level for any given device */
  78. static int msg_level = -1;
  79. module_param (msg_level, int, 0);
  80. MODULE_PARM_DESC (msg_level, "Override default message level");
  81. /*-------------------------------------------------------------------------*/
  82. /* handles CDC Ethernet and many other network "bulk data" interfaces */
  83. int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
  84. {
  85. int tmp;
  86. struct usb_host_interface *alt = NULL;
  87. struct usb_host_endpoint *in = NULL, *out = NULL;
  88. struct usb_host_endpoint *status = NULL;
  89. for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
  90. unsigned ep;
  91. in = out = status = NULL;
  92. alt = intf->altsetting + tmp;
  93. /* take the first altsetting with in-bulk + out-bulk;
  94. * remember any status endpoint, just in case;
  95. * ignore other endpoints and altsettings.
  96. */
  97. for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
  98. struct usb_host_endpoint *e;
  99. int intr = 0;
  100. e = alt->endpoint + ep;
  101. switch (e->desc.bmAttributes) {
  102. case USB_ENDPOINT_XFER_INT:
  103. if (!usb_endpoint_dir_in(&e->desc))
  104. continue;
  105. intr = 1;
  106. /* FALLTHROUGH */
  107. case USB_ENDPOINT_XFER_BULK:
  108. break;
  109. default:
  110. continue;
  111. }
  112. if (usb_endpoint_dir_in(&e->desc)) {
  113. if (!intr && !in)
  114. in = e;
  115. else if (intr && !status)
  116. status = e;
  117. } else {
  118. if (!out)
  119. out = e;
  120. }
  121. }
  122. if (in && out)
  123. break;
  124. }
  125. if (!alt || !in || !out)
  126. return -EINVAL;
  127. if (alt->desc.bAlternateSetting != 0 ||
  128. !(dev->driver_info->flags & FLAG_NO_SETINT)) {
  129. tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
  130. alt->desc.bAlternateSetting);
  131. if (tmp < 0)
  132. return tmp;
  133. }
  134. dev->in = usb_rcvbulkpipe (dev->udev,
  135. in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  136. dev->out = usb_sndbulkpipe (dev->udev,
  137. out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  138. dev->status = status;
  139. return 0;
  140. }
  141. EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
  142. int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
  143. {
  144. int tmp, i;
  145. unsigned char buf [13];
  146. tmp = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
  147. if (tmp != 12) {
  148. dev_dbg(&dev->udev->dev,
  149. "bad MAC string %d fetch, %d\n", iMACAddress, tmp);
  150. if (tmp >= 0)
  151. tmp = -EINVAL;
  152. return tmp;
  153. }
  154. for (i = tmp = 0; i < 6; i++, tmp += 2)
  155. dev->net->dev_addr [i] =
  156. (hex_to_bin(buf[tmp]) << 4) + hex_to_bin(buf[tmp + 1]);
  157. return 0;
  158. }
  159. EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
  160. static void intr_complete (struct urb *urb);
  161. static int init_status (struct usbnet *dev, struct usb_interface *intf)
  162. {
  163. char *buf = NULL;
  164. unsigned pipe = 0;
  165. unsigned maxp;
  166. unsigned period;
  167. if (!dev->driver_info->status)
  168. return 0;
  169. pipe = usb_rcvintpipe (dev->udev,
  170. dev->status->desc.bEndpointAddress
  171. & USB_ENDPOINT_NUMBER_MASK);
  172. maxp = usb_maxpacket (dev->udev, pipe, 0);
  173. /* avoid 1 msec chatter: min 8 msec poll rate */
  174. period = max ((int) dev->status->desc.bInterval,
  175. (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
  176. buf = kmalloc (maxp, GFP_KERNEL);
  177. if (buf) {
  178. dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
  179. if (!dev->interrupt) {
  180. kfree (buf);
  181. return -ENOMEM;
  182. } else {
  183. usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
  184. buf, maxp, intr_complete, dev, period);
  185. dev->interrupt->transfer_flags |= URB_FREE_BUFFER;
  186. dev_dbg(&intf->dev,
  187. "status ep%din, %d bytes period %d\n",
  188. usb_pipeendpoint(pipe), maxp, period);
  189. }
  190. }
  191. return 0;
  192. }
  193. /* Passes this packet up the stack, updating its accounting.
  194. * Some link protocols batch packets, so their rx_fixup paths
  195. * can return clones as well as just modify the original skb.
  196. */
  197. void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
  198. {
  199. int status;
  200. if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
  201. skb_queue_tail(&dev->rxq_pause, skb);
  202. return;
  203. }
  204. if (!skb->protocol)
  205. skb->protocol = eth_type_trans(skb, dev->net);
  206. dev->net->stats.rx_packets++;
  207. dev->net->stats.rx_bytes += skb->len;
  208. netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
  209. skb->len + sizeof (struct ethhdr), skb->protocol);
  210. memset (skb->cb, 0, sizeof (struct skb_data));
  211. if (skb_defer_rx_timestamp(skb))
  212. return;
  213. status = netif_rx_ni(skb);
  214. if (status != NET_RX_SUCCESS)
  215. netif_dbg(dev, rx_err, dev->net,
  216. "netif_rx status %d\n", status);
  217. }
  218. EXPORT_SYMBOL_GPL(usbnet_skb_return);
  219. /*-------------------------------------------------------------------------
  220. *
  221. * Network Device Driver (peer link to "Host Device", from USB host)
  222. *
  223. *-------------------------------------------------------------------------*/
  224. int usbnet_change_mtu (struct net_device *net, int new_mtu)
  225. {
  226. struct usbnet *dev = netdev_priv(net);
  227. int ll_mtu = new_mtu + net->hard_header_len;
  228. int old_hard_mtu = dev->hard_mtu;
  229. int old_rx_urb_size = dev->rx_urb_size;
  230. if (new_mtu <= 0)
  231. return -EINVAL;
  232. // no second zero-length packet read wanted after mtu-sized packets
  233. if ((ll_mtu % dev->maxpacket) == 0)
  234. return -EDOM;
  235. net->mtu = new_mtu;
  236. dev->hard_mtu = net->mtu + net->hard_header_len;
  237. if (dev->rx_urb_size == old_hard_mtu) {
  238. dev->rx_urb_size = dev->hard_mtu;
  239. if (dev->rx_urb_size > old_rx_urb_size)
  240. usbnet_unlink_rx_urbs(dev);
  241. }
  242. return 0;
  243. }
  244. EXPORT_SYMBOL_GPL(usbnet_change_mtu);
  245. /* The caller must hold list->lock */
  246. static void __usbnet_queue_skb(struct sk_buff_head *list,
  247. struct sk_buff *newsk, enum skb_state state)
  248. {
  249. struct skb_data *entry = (struct skb_data *) newsk->cb;
  250. __skb_queue_tail(list, newsk);
  251. entry->state = state;
  252. }
  253. /*-------------------------------------------------------------------------*/
  254. /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
  255. * completion callbacks. 2.5 should have fixed those bugs...
  256. */
  257. static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
  258. struct sk_buff_head *list, enum skb_state state)
  259. {
  260. unsigned long flags;
  261. enum skb_state old_state;
  262. struct skb_data *entry = (struct skb_data *) skb->cb;
  263. spin_lock_irqsave(&list->lock, flags);
  264. old_state = entry->state;
  265. entry->state = state;
  266. __skb_unlink(skb, list);
  267. spin_unlock(&list->lock);
  268. spin_lock(&dev->done.lock);
  269. __skb_queue_tail(&dev->done, skb);
  270. if (dev->done.qlen == 1)
  271. queue_work(usbnet_wq, &dev->bh_w);
  272. spin_unlock_irqrestore(&dev->done.lock, flags);
  273. return old_state;
  274. }
  275. /* some work can't be done in tasklets, so we use keventd
  276. *
  277. * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
  278. * but tasklet_schedule() doesn't. hope the failure is rare.
  279. */
  280. void usbnet_defer_kevent (struct usbnet *dev, int work)
  281. {
  282. set_bit (work, &dev->flags);
  283. if (!schedule_work (&dev->kevent)) {
  284. if (net_ratelimit())
  285. netdev_err(dev->net, "kevent %d may have been dropped\n", work);
  286. } else {
  287. netdev_dbg(dev->net, "kevent %d scheduled\n", work);
  288. }
  289. }
  290. EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
  291. /*-------------------------------------------------------------------------*/
  292. static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
  293. {
  294. struct sk_buff *skb;
  295. struct skb_data *entry;
  296. usb_complete_t complete_fn;
  297. int retval = 0;
  298. unsigned long lockflags;
  299. size_t size = dev->rx_urb_size;
  300. skb = __netdev_alloc_skb_ip_align(dev->net, size, flags);
  301. if (!skb) {
  302. netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
  303. usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
  304. usb_free_urb (urb);
  305. return -ENOMEM;
  306. }
  307. entry = (struct skb_data *) skb->cb;
  308. entry->urb = urb;
  309. entry->dev = dev;
  310. entry->length = 0;
  311. if (dev->driver_info->rx_complete)
  312. complete_fn = dev->driver_info->rx_complete;
  313. else
  314. complete_fn = rx_complete;
  315. usb_fill_bulk_urb (urb, dev->udev, dev->in,
  316. skb->data, size, complete_fn, skb);
  317. spin_lock_irqsave (&dev->rxq.lock, lockflags);
  318. if (netif_running (dev->net) &&
  319. netif_device_present (dev->net) &&
  320. !test_bit (EVENT_RX_HALT, &dev->flags) &&
  321. !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
  322. switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
  323. case -EPIPE:
  324. usbnet_defer_kevent (dev, EVENT_RX_HALT);
  325. break;
  326. case -ENOMEM:
  327. usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
  328. break;
  329. case -ENODEV:
  330. netif_dbg(dev, ifdown, dev->net, "device gone\n");
  331. netif_device_detach (dev->net);
  332. break;
  333. case -EHOSTUNREACH:
  334. retval = -ENOLINK;
  335. break;
  336. default:
  337. netif_dbg(dev, rx_err, dev->net,
  338. "rx submit, %d\n", retval);
  339. queue_work(usbnet_wq, &dev->bh_w);
  340. break;
  341. case 0:
  342. usb_mark_last_busy(dev->udev);
  343. __usbnet_queue_skb(&dev->rxq, skb, rx_start);
  344. }
  345. } else {
  346. netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
  347. retval = -ENOLINK;
  348. }
  349. spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
  350. if (retval) {
  351. dev_kfree_skb_any (skb);
  352. usb_free_urb (urb);
  353. }
  354. return retval;
  355. }
  356. /*-------------------------------------------------------------------------*/
  357. static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
  358. {
  359. if (dev->driver_info->rx_fixup &&
  360. !dev->driver_info->rx_fixup (dev, skb)) {
  361. /* With RX_ASSEMBLE, rx_fixup() must update counters */
  362. if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE))
  363. dev->net->stats.rx_errors++;
  364. goto done;
  365. }
  366. // else network stack removes extra byte if we forced a short packet
  367. /* all data was already cloned from skb inside the driver */
  368. if (dev->driver_info->flags & FLAG_MULTI_PACKET)
  369. goto done;
  370. if (skb->len < ETH_HLEN) {
  371. dev->net->stats.rx_errors++;
  372. dev->net->stats.rx_length_errors++;
  373. netif_dbg(dev, rx_err, dev->net, "rx length %d\n", skb->len);
  374. } else {
  375. usbnet_skb_return(dev, skb);
  376. return;
  377. }
  378. done:
  379. skb_queue_tail(&dev->done, skb);
  380. }
  381. /*-------------------------------------------------------------------------*/
  382. void rx_complete(struct urb *urb)
  383. {
  384. struct sk_buff *skb = (struct sk_buff *) urb->context;
  385. struct skb_data *entry = (struct skb_data *) skb->cb;
  386. struct usbnet *dev = entry->dev;
  387. int urb_status = urb->status;
  388. enum skb_state state;
  389. skb_put (skb, urb->actual_length);
  390. state = rx_done;
  391. entry->urb = NULL;
  392. switch (urb_status) {
  393. /* success */
  394. case 0:
  395. break;
  396. /* stalls need manual reset. this is rare ... except that
  397. * when going through USB 2.0 TTs, unplug appears this way.
  398. * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
  399. * storm, recovering as needed.
  400. */
  401. case -EPIPE:
  402. dev->net->stats.rx_errors++;
  403. usbnet_defer_kevent (dev, EVENT_RX_HALT);
  404. // FALLTHROUGH
  405. /* software-driven interface shutdown */
  406. case -ECONNRESET: /* async unlink */
  407. case -ESHUTDOWN: /* hardware gone */
  408. netif_dbg(dev, ifdown, dev->net,
  409. "rx shutdown, code %d\n", urb_status);
  410. goto block;
  411. /* we get controller i/o faults during khubd disconnect() delays.
  412. * throttle down resubmits, to avoid log floods; just temporarily,
  413. * so we still recover when the fault isn't a khubd delay.
  414. */
  415. case -EPROTO:
  416. case -ETIME:
  417. case -EILSEQ:
  418. dev->net->stats.rx_errors++;
  419. if (!timer_pending (&dev->delay)) {
  420. mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
  421. netif_dbg(dev, link, dev->net,
  422. "rx throttle %d\n", urb_status);
  423. }
  424. block:
  425. state = rx_cleanup;
  426. entry->urb = urb;
  427. urb = NULL;
  428. break;
  429. /* data overrun ... flush fifo? */
  430. case -EOVERFLOW:
  431. dev->net->stats.rx_over_errors++;
  432. // FALLTHROUGH
  433. default:
  434. state = rx_cleanup;
  435. dev->net->stats.rx_errors++;
  436. netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
  437. break;
  438. }
  439. state = defer_bh(dev, skb, &dev->rxq, state);
  440. if (urb) {
  441. if (netif_running (dev->net) &&
  442. !test_bit (EVENT_RX_HALT, &dev->flags) &&
  443. state != unlink_start) {
  444. rx_submit (dev, urb, GFP_ATOMIC);
  445. usb_mark_last_busy(dev->udev);
  446. return;
  447. }
  448. usb_free_urb (urb);
  449. }
  450. netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
  451. }
  452. EXPORT_SYMBOL_GPL(rx_complete);
  453. static void intr_complete (struct urb *urb)
  454. {
  455. struct usbnet *dev = urb->context;
  456. int status = urb->status;
  457. switch (status) {
  458. /* success */
  459. case 0:
  460. dev->driver_info->status(dev, urb);
  461. break;
  462. /* software-driven interface shutdown */
  463. case -ENOENT: /* urb killed */
  464. case -ESHUTDOWN: /* hardware gone */
  465. netif_dbg(dev, ifdown, dev->net,
  466. "intr shutdown, code %d\n", status);
  467. return;
  468. /* NOTE: not throttling like RX/TX, since this endpoint
  469. * already polls infrequently
  470. */
  471. default:
  472. netdev_dbg(dev->net, "intr status %d\n", status);
  473. break;
  474. }
  475. if (!netif_running (dev->net))
  476. return;
  477. memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
  478. status = usb_submit_urb (urb, GFP_ATOMIC);
  479. if (status != 0)
  480. netif_err(dev, timer, dev->net,
  481. "intr resubmit --> %d\n", status);
  482. }
  483. /*-------------------------------------------------------------------------*/
  484. void usbnet_pause_rx(struct usbnet *dev)
  485. {
  486. set_bit(EVENT_RX_PAUSED, &dev->flags);
  487. netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
  488. }
  489. EXPORT_SYMBOL_GPL(usbnet_pause_rx);
  490. void usbnet_resume_rx(struct usbnet *dev)
  491. {
  492. struct sk_buff *skb;
  493. int num = 0;
  494. clear_bit(EVENT_RX_PAUSED, &dev->flags);
  495. while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
  496. usbnet_skb_return(dev, skb);
  497. num++;
  498. }
  499. queue_work(usbnet_wq, &dev->bh_w);
  500. netif_dbg(dev, rx_status, dev->net,
  501. "paused rx queue disabled, %d skbs requeued\n", num);
  502. }
  503. EXPORT_SYMBOL_GPL(usbnet_resume_rx);
  504. void usbnet_purge_paused_rxq(struct usbnet *dev)
  505. {
  506. skb_queue_purge(&dev->rxq_pause);
  507. }
  508. EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
  509. /*-------------------------------------------------------------------------*/
  510. // unlink pending rx/tx; completion handlers do all other cleanup
  511. static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
  512. {
  513. unsigned long flags;
  514. struct sk_buff *skb;
  515. int count = 0;
  516. spin_lock_irqsave (&q->lock, flags);
  517. while (!skb_queue_empty(q)) {
  518. struct skb_data *entry;
  519. struct urb *urb;
  520. int retval;
  521. skb_queue_walk(q, skb) {
  522. entry = (struct skb_data *) skb->cb;
  523. if (entry->state != unlink_start)
  524. goto found;
  525. }
  526. break;
  527. found:
  528. entry->state = unlink_start;
  529. urb = entry->urb;
  530. /*
  531. * Get reference count of the URB to avoid it to be
  532. * freed during usb_unlink_urb, which may trigger
  533. * use-after-free problem inside usb_unlink_urb since
  534. * usb_unlink_urb is always racing with .complete
  535. * handler(include defer_bh).
  536. */
  537. usb_get_urb(urb);
  538. spin_unlock_irqrestore(&q->lock, flags);
  539. // during some PM-driven resume scenarios,
  540. // these (async) unlinks complete immediately
  541. retval = usb_unlink_urb (urb);
  542. if (retval != -EINPROGRESS && retval != 0)
  543. netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
  544. else
  545. count++;
  546. usb_put_urb(urb);
  547. spin_lock_irqsave(&q->lock, flags);
  548. }
  549. spin_unlock_irqrestore (&q->lock, flags);
  550. return count;
  551. }
  552. // Flush all pending rx urbs
  553. // minidrivers may need to do this when the MTU changes
  554. void usbnet_unlink_rx_urbs(struct usbnet *dev)
  555. {
  556. if (netif_running(dev->net)) {
  557. (void) unlink_urbs (dev, &dev->rxq);
  558. queue_work(usbnet_wq, &dev->bh_w);
  559. }
  560. }
  561. EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
  562. /*-------------------------------------------------------------------------*/
  563. // precondition: never called in_interrupt
  564. void usbnet_terminate_urbs(struct usbnet *dev)
  565. {
  566. DECLARE_WAITQUEUE(wait, current);
  567. int temp;
  568. /* ensure there are no more active urbs */
  569. add_wait_queue(&unlink_wakeup, &wait);
  570. set_current_state(TASK_UNINTERRUPTIBLE);
  571. dev->wait = &unlink_wakeup;
  572. temp = unlink_urbs(dev, &dev->txq) +
  573. unlink_urbs(dev, &dev->rxq);
  574. /* maybe wait for deletions to finish. */
  575. while (!skb_queue_empty(&dev->rxq)
  576. && !skb_queue_empty(&dev->txq)
  577. && !skb_queue_empty(&dev->done)) {
  578. schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
  579. set_current_state(TASK_UNINTERRUPTIBLE);
  580. netif_dbg(dev, ifdown, dev->net,
  581. "waited for %d urb completions\n", temp);
  582. }
  583. set_current_state(TASK_RUNNING);
  584. dev->wait = NULL;
  585. remove_wait_queue(&unlink_wakeup, &wait);
  586. }
  587. EXPORT_SYMBOL_GPL(usbnet_terminate_urbs);
  588. int usbnet_stop (struct net_device *net)
  589. {
  590. struct usbnet *dev = netdev_priv(net);
  591. struct driver_info *info = dev->driver_info;
  592. int retval;
  593. clear_bit(EVENT_DEV_OPEN, &dev->flags);
  594. netif_stop_queue (net);
  595. netif_info(dev, ifdown, dev->net,
  596. "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
  597. net->stats.rx_packets, net->stats.tx_packets,
  598. net->stats.rx_errors, net->stats.tx_errors);
  599. /* allow minidriver to stop correctly (wireless devices to turn off
  600. * radio etc) */
  601. if (info->stop) {
  602. retval = info->stop(dev);
  603. if (retval < 0)
  604. netif_info(dev, ifdown, dev->net,
  605. "stop fail (%d) usbnet usb-%s-%s, %s\n",
  606. retval,
  607. dev->udev->bus->bus_name, dev->udev->devpath,
  608. info->description);
  609. }
  610. if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
  611. usbnet_terminate_urbs(dev);
  612. usb_kill_urb(dev->interrupt);
  613. usbnet_purge_paused_rxq(dev);
  614. /* deferred work (task, timer, softirq) must also stop.
  615. * can't flush_scheduled_work() until we drop rtnl (later),
  616. * else workers could deadlock; so make workers a NOP.
  617. */
  618. dev->flags = 0;
  619. del_timer_sync (&dev->delay);
  620. cancel_work_sync(&dev->bh_w);
  621. if (info->manage_power)
  622. info->manage_power(dev, 0);
  623. else
  624. usb_autopm_put_interface(dev->intf);
  625. return 0;
  626. }
  627. EXPORT_SYMBOL_GPL(usbnet_stop);
  628. /*-------------------------------------------------------------------------*/
  629. // posts reads, and enables write queuing
  630. // precondition: never called in_interrupt
  631. int usbnet_open (struct net_device *net)
  632. {
  633. struct usbnet *dev = netdev_priv(net);
  634. int retval;
  635. struct driver_info *info = dev->driver_info;
  636. if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
  637. netif_info(dev, ifup, dev->net,
  638. "resumption fail (%d) usbnet usb-%s-%s, %s\n",
  639. retval,
  640. dev->udev->bus->bus_name,
  641. dev->udev->devpath,
  642. info->description);
  643. goto done_nopm;
  644. }
  645. // put into "known safe" state
  646. if (info->reset && (retval = info->reset (dev)) < 0) {
  647. netif_info(dev, ifup, dev->net,
  648. "open reset fail (%d) usbnet usb-%s-%s, %s\n",
  649. retval,
  650. dev->udev->bus->bus_name,
  651. dev->udev->devpath,
  652. info->description);
  653. goto done;
  654. }
  655. // insist peer be connected
  656. if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
  657. netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
  658. goto done;
  659. }
  660. /* start any status interrupt transfer */
  661. if (dev->interrupt) {
  662. retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
  663. if (retval < 0) {
  664. netif_err(dev, ifup, dev->net,
  665. "intr submit %d\n", retval);
  666. goto done;
  667. }
  668. }
  669. set_bit(EVENT_DEV_OPEN, &dev->flags);
  670. netif_start_queue (net);
  671. netif_info(dev, ifup, dev->net,
  672. "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
  673. (int)RX_QLEN(dev), (int)TX_QLEN(dev),
  674. dev->net->mtu,
  675. (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
  676. (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
  677. (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
  678. (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
  679. (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
  680. "simple");
  681. // delay posting reads until we're fully open
  682. queue_work(usbnet_wq, &dev->bh_w);
  683. if (info->manage_power) {
  684. retval = info->manage_power(dev, 1);
  685. if (retval < 0)
  686. goto done;
  687. usb_autopm_put_interface(dev->intf);
  688. }
  689. return retval;
  690. done:
  691. usb_autopm_put_interface(dev->intf);
  692. done_nopm:
  693. return retval;
  694. }
  695. EXPORT_SYMBOL_GPL(usbnet_open);
  696. /*-------------------------------------------------------------------------*/
  697. /* ethtool methods; minidrivers may need to add some more, but
  698. * they'll probably want to use this base set.
  699. */
  700. int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
  701. {
  702. struct usbnet *dev = netdev_priv(net);
  703. if (!dev->mii.mdio_read)
  704. return -EOPNOTSUPP;
  705. return mii_ethtool_gset(&dev->mii, cmd);
  706. }
  707. EXPORT_SYMBOL_GPL(usbnet_get_settings);
  708. int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
  709. {
  710. struct usbnet *dev = netdev_priv(net);
  711. int retval;
  712. if (!dev->mii.mdio_write)
  713. return -EOPNOTSUPP;
  714. retval = mii_ethtool_sset(&dev->mii, cmd);
  715. /* link speed/duplex might have changed */
  716. if (dev->driver_info->link_reset)
  717. dev->driver_info->link_reset(dev);
  718. return retval;
  719. }
  720. EXPORT_SYMBOL_GPL(usbnet_set_settings);
  721. u32 usbnet_get_link (struct net_device *net)
  722. {
  723. struct usbnet *dev = netdev_priv(net);
  724. /* If a check_connect is defined, return its result */
  725. if (dev->driver_info->check_connect)
  726. return dev->driver_info->check_connect (dev) == 0;
  727. /* if the device has mii operations, use those */
  728. if (dev->mii.mdio_read)
  729. return mii_link_ok(&dev->mii);
  730. /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
  731. return ethtool_op_get_link(net);
  732. }
  733. EXPORT_SYMBOL_GPL(usbnet_get_link);
  734. int usbnet_nway_reset(struct net_device *net)
  735. {
  736. struct usbnet *dev = netdev_priv(net);
  737. if (!dev->mii.mdio_write)
  738. return -EOPNOTSUPP;
  739. return mii_nway_restart(&dev->mii);
  740. }
  741. EXPORT_SYMBOL_GPL(usbnet_nway_reset);
  742. void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
  743. {
  744. struct usbnet *dev = netdev_priv(net);
  745. strncpy (info->driver, dev->driver_name, sizeof info->driver);
  746. strncpy (info->version, DRIVER_VERSION, sizeof info->version);
  747. strncpy (info->fw_version, dev->driver_info->description,
  748. sizeof info->fw_version);
  749. usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
  750. }
  751. EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
  752. u32 usbnet_get_msglevel (struct net_device *net)
  753. {
  754. struct usbnet *dev = netdev_priv(net);
  755. return dev->msg_enable;
  756. }
  757. EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
  758. void usbnet_set_msglevel (struct net_device *net, u32 level)
  759. {
  760. struct usbnet *dev = netdev_priv(net);
  761. dev->msg_enable = level;
  762. }
  763. EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
  764. /* drivers may override default ethtool_ops in their bind() routine */
  765. static const struct ethtool_ops usbnet_ethtool_ops = {
  766. .get_settings = usbnet_get_settings,
  767. .set_settings = usbnet_set_settings,
  768. .get_link = usbnet_get_link,
  769. .nway_reset = usbnet_nway_reset,
  770. .get_drvinfo = usbnet_get_drvinfo,
  771. .get_msglevel = usbnet_get_msglevel,
  772. .set_msglevel = usbnet_set_msglevel,
  773. };
  774. /*-------------------------------------------------------------------------*/
  775. /* work that cannot be done in interrupt context uses keventd.
  776. *
  777. * NOTE: with 2.5 we could do more of this using completion callbacks,
  778. * especially now that control transfers can be queued.
  779. */
  780. static void
  781. kevent (struct work_struct *work)
  782. {
  783. struct usbnet *dev =
  784. container_of(work, struct usbnet, kevent);
  785. int status;
  786. /* usb_clear_halt() needs a thread context */
  787. if (test_bit (EVENT_TX_HALT, &dev->flags)) {
  788. unlink_urbs (dev, &dev->txq);
  789. status = usb_autopm_get_interface(dev->intf);
  790. if (status < 0)
  791. goto fail_pipe;
  792. status = usb_clear_halt (dev->udev, dev->out);
  793. usb_autopm_put_interface(dev->intf);
  794. if (status < 0 &&
  795. status != -EPIPE &&
  796. status != -ESHUTDOWN) {
  797. if (netif_msg_tx_err (dev))
  798. fail_pipe:
  799. netdev_err(dev->net, "can't clear tx halt, status %d\n",
  800. status);
  801. } else {
  802. clear_bit (EVENT_TX_HALT, &dev->flags);
  803. if (status != -ESHUTDOWN)
  804. netif_wake_queue (dev->net);
  805. }
  806. }
  807. if (test_bit (EVENT_RX_HALT, &dev->flags)) {
  808. unlink_urbs (dev, &dev->rxq);
  809. status = usb_autopm_get_interface(dev->intf);
  810. if (status < 0)
  811. goto fail_halt;
  812. status = usb_clear_halt (dev->udev, dev->in);
  813. usb_autopm_put_interface(dev->intf);
  814. if (status < 0 &&
  815. status != -EPIPE &&
  816. status != -ESHUTDOWN) {
  817. if (netif_msg_rx_err (dev))
  818. fail_halt:
  819. netdev_err(dev->net, "can't clear rx halt, status %d\n",
  820. status);
  821. } else {
  822. clear_bit (EVENT_RX_HALT, &dev->flags);
  823. queue_work(usbnet_wq, &dev->bh_w);
  824. }
  825. }
  826. /* tasklet could resubmit itself forever if memory is tight */
  827. if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
  828. struct urb *urb = NULL;
  829. int resched = 1;
  830. if (netif_running (dev->net))
  831. urb = usb_alloc_urb (0, GFP_KERNEL);
  832. else
  833. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  834. if (urb != NULL) {
  835. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  836. status = usb_autopm_get_interface(dev->intf);
  837. if (status < 0) {
  838. usb_free_urb(urb);
  839. goto fail_lowmem;
  840. }
  841. if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
  842. resched = 0;
  843. usb_autopm_put_interface(dev->intf);
  844. fail_lowmem:
  845. if (resched)
  846. queue_work(usbnet_wq, &dev->bh_w);
  847. }
  848. }
  849. if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
  850. struct driver_info *info = dev->driver_info;
  851. int retval = 0;
  852. clear_bit (EVENT_LINK_RESET, &dev->flags);
  853. status = usb_autopm_get_interface(dev->intf);
  854. if (status < 0)
  855. goto skip_reset;
  856. if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
  857. usb_autopm_put_interface(dev->intf);
  858. skip_reset:
  859. netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
  860. retval,
  861. dev->udev->bus->bus_name,
  862. dev->udev->devpath,
  863. info->description);
  864. } else {
  865. usb_autopm_put_interface(dev->intf);
  866. }
  867. }
  868. if (dev->flags)
  869. netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
  870. }
  871. /*-------------------------------------------------------------------------*/
  872. static void tx_complete (struct urb *urb)
  873. {
  874. struct sk_buff *skb = (struct sk_buff *) urb->context;
  875. struct skb_data *entry = (struct skb_data *) skb->cb;
  876. struct usbnet *dev = entry->dev;
  877. if (urb->status == 0) {
  878. if (!(dev->driver_info->flags & FLAG_MULTI_PACKET))
  879. dev->net->stats.tx_packets++;
  880. dev->net->stats.tx_bytes += entry->length;
  881. } else {
  882. dev->net->stats.tx_errors++;
  883. switch (urb->status) {
  884. case -EPIPE:
  885. usbnet_defer_kevent (dev, EVENT_TX_HALT);
  886. break;
  887. /* software-driven interface shutdown */
  888. case -ECONNRESET: // async unlink
  889. case -ESHUTDOWN: // hardware gone
  890. break;
  891. // like rx, tx gets controller i/o faults during khubd delays
  892. // and so it uses the same throttling mechanism.
  893. case -EPROTO:
  894. case -ETIME:
  895. case -EILSEQ:
  896. usb_mark_last_busy(dev->udev);
  897. if (!timer_pending (&dev->delay)) {
  898. mod_timer (&dev->delay,
  899. jiffies + THROTTLE_JIFFIES);
  900. netif_dbg(dev, link, dev->net,
  901. "tx throttle %d\n", urb->status);
  902. }
  903. netif_stop_queue (dev->net);
  904. break;
  905. default:
  906. netif_dbg(dev, tx_err, dev->net,
  907. "tx err %d\n", entry->urb->status);
  908. break;
  909. }
  910. }
  911. usb_autopm_put_interface_async(dev->intf);
  912. (void) defer_bh(dev, skb, &dev->txq, tx_done);
  913. }
  914. /*-------------------------------------------------------------------------*/
  915. void usbnet_tx_timeout (struct net_device *net)
  916. {
  917. struct usbnet *dev = netdev_priv(net);
  918. unlink_urbs (dev, &dev->txq);
  919. queue_work(usbnet_wq, &dev->bh_w);
  920. // FIXME: device recovery -- reset?
  921. }
  922. EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
  923. /*-------------------------------------------------------------------------*/
  924. netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
  925. struct net_device *net)
  926. {
  927. struct usbnet *dev = netdev_priv(net);
  928. int length;
  929. struct urb *urb = NULL;
  930. struct skb_data *entry;
  931. struct driver_info *info = dev->driver_info;
  932. unsigned long flags;
  933. int retval;
  934. if (skb)
  935. skb_tx_timestamp(skb);
  936. // some devices want funky USB-level framing, for
  937. // win32 driver (usually) and/or hardware quirks
  938. if (info->tx_fixup) {
  939. skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
  940. if (!skb) {
  941. if (netif_msg_tx_err(dev)) {
  942. netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
  943. goto drop;
  944. } else {
  945. /* cdc_ncm collected packet; waits for more */
  946. goto not_drop;
  947. }
  948. }
  949. }
  950. length = skb->len;
  951. if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
  952. netif_dbg(dev, tx_err, dev->net, "no urb\n");
  953. goto drop;
  954. }
  955. entry = (struct skb_data *) skb->cb;
  956. entry->urb = urb;
  957. entry->dev = dev;
  958. entry->length = length;
  959. usb_fill_bulk_urb (urb, dev->udev, dev->out,
  960. skb->data, skb->len, tx_complete, skb);
  961. /* don't assume the hardware handles USB_ZERO_PACKET
  962. * NOTE: strictly conforming cdc-ether devices should expect
  963. * the ZLP here, but ignore the one-byte packet.
  964. * NOTE2: CDC NCM specification is different from CDC ECM when
  965. * handling ZLP/short packets, so cdc_ncm driver will make short
  966. * packet itself if needed.
  967. */
  968. if (length % dev->maxpacket == 0) {
  969. if (!(info->flags & FLAG_SEND_ZLP)) {
  970. if (!(info->flags & FLAG_MULTI_PACKET)) {
  971. urb->transfer_buffer_length++;
  972. if (skb_tailroom(skb)) {
  973. skb->data[skb->len] = 0;
  974. __skb_put(skb, 1);
  975. }
  976. }
  977. } else
  978. urb->transfer_flags |= URB_ZERO_PACKET;
  979. }
  980. spin_lock_irqsave(&dev->txq.lock, flags);
  981. retval = usb_autopm_get_interface_async(dev->intf);
  982. if (retval < 0) {
  983. spin_unlock_irqrestore(&dev->txq.lock, flags);
  984. goto drop;
  985. }
  986. #ifdef CONFIG_PM
  987. /* if this triggers the device is still a sleep */
  988. if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
  989. /* transmission will be done in resume */
  990. usb_anchor_urb(urb, &dev->deferred);
  991. /* no use to process more packets */
  992. netif_stop_queue(net);
  993. usb_put_urb(urb);
  994. spin_unlock_irqrestore(&dev->txq.lock, flags);
  995. netdev_dbg(dev->net, "Delaying transmission for resumption\n");
  996. goto deferred;
  997. }
  998. #endif
  999. switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
  1000. case -EPIPE:
  1001. netif_stop_queue (net);
  1002. usbnet_defer_kevent (dev, EVENT_TX_HALT);
  1003. usb_autopm_put_interface_async(dev->intf);
  1004. break;
  1005. default:
  1006. usb_autopm_put_interface_async(dev->intf);
  1007. netif_dbg(dev, tx_err, dev->net,
  1008. "tx: submit urb err %d\n", retval);
  1009. break;
  1010. case 0:
  1011. net->trans_start = jiffies;
  1012. __usbnet_queue_skb(&dev->txq, skb, tx_start);
  1013. if (dev->txq.qlen >= TX_QLEN (dev))
  1014. netif_stop_queue (net);
  1015. }
  1016. spin_unlock_irqrestore (&dev->txq.lock, flags);
  1017. if (retval) {
  1018. netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
  1019. drop:
  1020. dev->net->stats.tx_dropped++;
  1021. not_drop:
  1022. if (skb)
  1023. dev_kfree_skb_any (skb);
  1024. usb_free_urb (urb);
  1025. } else
  1026. netif_dbg(dev, tx_queued, dev->net,
  1027. "> tx, len %d, type 0x%x\n", length, skb->protocol);
  1028. #ifdef CONFIG_PM
  1029. deferred:
  1030. #endif
  1031. return NETDEV_TX_OK;
  1032. }
  1033. EXPORT_SYMBOL_GPL(usbnet_start_xmit);
  1034. /*-------------------------------------------------------------------------*/
  1035. // tasklet (work deferred from completions, in_irq) or timer
  1036. static void usbnet_bh (unsigned long param)
  1037. {
  1038. struct usbnet *dev = (struct usbnet *) param;
  1039. struct sk_buff *skb;
  1040. struct skb_data *entry;
  1041. while ((skb = skb_dequeue (&dev->done))) {
  1042. entry = (struct skb_data *) skb->cb;
  1043. switch (entry->state) {
  1044. case rx_done:
  1045. entry->state = rx_cleanup;
  1046. rx_process (dev, skb);
  1047. continue;
  1048. case tx_done:
  1049. case rx_cleanup:
  1050. usb_free_urb (entry->urb);
  1051. dev_kfree_skb (skb);
  1052. continue;
  1053. default:
  1054. netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
  1055. }
  1056. }
  1057. // waiting for all pending urbs to complete?
  1058. if (dev->wait) {
  1059. if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
  1060. wake_up(&unlink_wakeup);
  1061. }
  1062. // or are we maybe short a few urbs?
  1063. } else if (netif_running (dev->net) &&
  1064. netif_device_present (dev->net) &&
  1065. !timer_pending (&dev->delay) &&
  1066. !test_bit (EVENT_RX_HALT, &dev->flags)) {
  1067. int temp = dev->rxq.qlen;
  1068. int qlen = RX_QLEN (dev);
  1069. if (temp < qlen) {
  1070. struct urb *urb;
  1071. int i;
  1072. // don't refill the queue all at once
  1073. for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
  1074. urb = usb_alloc_urb (0, GFP_ATOMIC);
  1075. if (urb != NULL) {
  1076. if (rx_submit (dev, urb, GFP_ATOMIC) ==
  1077. -ENOLINK)
  1078. return;
  1079. }
  1080. }
  1081. if (temp != dev->rxq.qlen)
  1082. netif_dbg(dev, link, dev->net,
  1083. "rxqlen %d --> %d\n",
  1084. temp, dev->rxq.qlen);
  1085. if (dev->rxq.qlen < qlen)
  1086. queue_work(usbnet_wq, &dev->bh_w);
  1087. }
  1088. if (dev->txq.qlen < TX_QLEN (dev))
  1089. netif_wake_queue (dev->net);
  1090. }
  1091. }
  1092. static void usbnet_bh_w(struct work_struct *work)
  1093. {
  1094. struct usbnet *dev =
  1095. container_of(work, struct usbnet, bh_w);
  1096. unsigned long param = (unsigned long)dev;
  1097. usbnet_bh(param);
  1098. }
  1099. /*-------------------------------------------------------------------------
  1100. *
  1101. * USB Device Driver support
  1102. *
  1103. *-------------------------------------------------------------------------*/
  1104. // precondition: never called in_interrupt
  1105. void usbnet_disconnect (struct usb_interface *intf)
  1106. {
  1107. struct usbnet *dev;
  1108. struct usb_device *xdev;
  1109. struct net_device *net;
  1110. dev = usb_get_intfdata(intf);
  1111. usb_set_intfdata(intf, NULL);
  1112. if (!dev)
  1113. return;
  1114. xdev = interface_to_usbdev (intf);
  1115. netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
  1116. intf->dev.driver->name,
  1117. xdev->bus->bus_name, xdev->devpath,
  1118. dev->driver_info->description);
  1119. net = dev->net;
  1120. unregister_netdev (net);
  1121. cancel_work_sync(&dev->kevent);
  1122. usb_scuttle_anchored_urbs(&dev->deferred);
  1123. if (dev->driver_info->unbind)
  1124. dev->driver_info->unbind (dev, intf);
  1125. usb_kill_urb(dev->interrupt);
  1126. usb_free_urb(dev->interrupt);
  1127. free_netdev(net);
  1128. usb_put_dev (xdev);
  1129. }
  1130. EXPORT_SYMBOL_GPL(usbnet_disconnect);
  1131. static const struct net_device_ops usbnet_netdev_ops = {
  1132. .ndo_open = usbnet_open,
  1133. .ndo_stop = usbnet_stop,
  1134. .ndo_start_xmit = usbnet_start_xmit,
  1135. .ndo_tx_timeout = usbnet_tx_timeout,
  1136. .ndo_change_mtu = usbnet_change_mtu,
  1137. .ndo_set_mac_address = eth_mac_addr,
  1138. .ndo_validate_addr = eth_validate_addr,
  1139. };
  1140. /*-------------------------------------------------------------------------*/
  1141. // precondition: never called in_interrupt
  1142. static struct device_type wlan_type = {
  1143. .name = "wlan",
  1144. };
  1145. static struct device_type wwan_type = {
  1146. .name = "wwan",
  1147. };
  1148. int
  1149. usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
  1150. {
  1151. struct usbnet *dev;
  1152. struct net_device *net;
  1153. struct usb_host_interface *interface;
  1154. struct driver_info *info;
  1155. struct usb_device *xdev;
  1156. int status;
  1157. const char *name;
  1158. struct usb_driver *driver = to_usb_driver(udev->dev.driver);
  1159. /* usbnet already took usb runtime pm, so have to enable the feature
  1160. * for usb interface, otherwise usb_autopm_get_interface may return
  1161. * failure if USB_SUSPEND(RUNTIME_PM) is enabled.
  1162. */
  1163. if (!driver->supports_autosuspend) {
  1164. driver->supports_autosuspend = 1;
  1165. pm_runtime_enable(&udev->dev);
  1166. }
  1167. name = udev->dev.driver->name;
  1168. info = (struct driver_info *) prod->driver_info;
  1169. if (!info) {
  1170. dev_dbg (&udev->dev, "blacklisted by %s\n", name);
  1171. return -ENODEV;
  1172. }
  1173. xdev = interface_to_usbdev (udev);
  1174. interface = udev->cur_altsetting;
  1175. usb_get_dev (xdev);
  1176. status = -ENOMEM;
  1177. // set up our own records
  1178. net = alloc_etherdev(sizeof(*dev));
  1179. if (!net)
  1180. goto out;
  1181. /* netdev_printk() needs this so do it as early as possible */
  1182. SET_NETDEV_DEV(net, &udev->dev);
  1183. dev = netdev_priv(net);
  1184. dev->udev = xdev;
  1185. dev->intf = udev;
  1186. dev->driver_info = info;
  1187. dev->driver_name = name;
  1188. dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
  1189. | NETIF_MSG_PROBE | NETIF_MSG_LINK);
  1190. skb_queue_head_init (&dev->rxq);
  1191. skb_queue_head_init (&dev->txq);
  1192. skb_queue_head_init (&dev->done);
  1193. skb_queue_head_init(&dev->rxq_pause);
  1194. INIT_WORK(&dev->bh_w, usbnet_bh_w);
  1195. INIT_WORK (&dev->kevent, kevent);
  1196. init_usb_anchor(&dev->deferred);
  1197. dev->delay.function = usbnet_bh;
  1198. dev->delay.data = (unsigned long) dev;
  1199. init_timer (&dev->delay);
  1200. mutex_init (&dev->phy_mutex);
  1201. dev->net = net;
  1202. strcpy (net->name, "usb%d");
  1203. memcpy (net->dev_addr, node_id, sizeof node_id);
  1204. /* rx and tx sides can use different message sizes;
  1205. * bind() should set rx_urb_size in that case.
  1206. */
  1207. dev->hard_mtu = net->mtu + net->hard_header_len;
  1208. #if 0
  1209. // dma_supported() is deeply broken on almost all architectures
  1210. // possible with some EHCI controllers
  1211. if (dma_supported (&udev->dev, DMA_BIT_MASK(64)))
  1212. net->features |= NETIF_F_HIGHDMA;
  1213. #endif
  1214. net->netdev_ops = &usbnet_netdev_ops;
  1215. net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
  1216. net->ethtool_ops = &usbnet_ethtool_ops;
  1217. // allow device-specific bind/init procedures
  1218. // NOTE net->name still not usable ...
  1219. if (info->bind) {
  1220. status = info->bind (dev, udev);
  1221. if (status < 0)
  1222. goto out1;
  1223. // heuristic: "usb%d" for links we know are two-host,
  1224. // else "eth%d" when there's reasonable doubt. userspace
  1225. // can rename the link if it knows better.
  1226. if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
  1227. ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
  1228. (net->dev_addr [0] & 0x02) == 0))
  1229. strcpy (net->name, "eth%d");
  1230. /* WLAN devices should always be named "wlan%d" */
  1231. if ((dev->driver_info->flags & FLAG_WLAN) != 0)
  1232. strcpy(net->name, "wlan%d");
  1233. /* WWAN devices should always be named "wwan%d" */
  1234. if ((dev->driver_info->flags & FLAG_WWAN) != 0)
  1235. strcpy(net->name, "wwan%d");
  1236. /* maybe the remote can't receive an Ethernet MTU */
  1237. if (net->mtu > (dev->hard_mtu - net->hard_header_len))
  1238. net->mtu = dev->hard_mtu - net->hard_header_len;
  1239. } else if (!info->in || !info->out)
  1240. status = usbnet_get_endpoints (dev, udev);
  1241. else {
  1242. dev->in = usb_rcvbulkpipe (xdev, info->in);
  1243. dev->out = usb_sndbulkpipe (xdev, info->out);
  1244. if (!(info->flags & FLAG_NO_SETINT))
  1245. status = usb_set_interface (xdev,
  1246. interface->desc.bInterfaceNumber,
  1247. interface->desc.bAlternateSetting);
  1248. else
  1249. status = 0;
  1250. }
  1251. if (status >= 0 && dev->status)
  1252. status = init_status (dev, udev);
  1253. if (status < 0)
  1254. goto out3;
  1255. if (!dev->rx_urb_size)
  1256. dev->rx_urb_size = dev->hard_mtu;
  1257. dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
  1258. if ((dev->driver_info->flags & FLAG_WLAN) != 0)
  1259. SET_NETDEV_DEVTYPE(net, &wlan_type);
  1260. if ((dev->driver_info->flags & FLAG_WWAN) != 0)
  1261. SET_NETDEV_DEVTYPE(net, &wwan_type);
  1262. status = register_netdev (net);
  1263. if (status)
  1264. goto out4;
  1265. netif_info(dev, probe, dev->net,
  1266. "register '%s' at usb-%s-%s, %s, %pM\n",
  1267. udev->dev.driver->name,
  1268. xdev->bus->bus_name, xdev->devpath,
  1269. dev->driver_info->description,
  1270. net->dev_addr);
  1271. // ok, it's ready to go.
  1272. usb_set_intfdata (udev, dev);
  1273. netif_device_attach (net);
  1274. if (dev->driver_info->flags & FLAG_LINK_INTR)
  1275. netif_carrier_off(net);
  1276. return 0;
  1277. out4:
  1278. usb_free_urb(dev->interrupt);
  1279. out3:
  1280. if (info->unbind)
  1281. info->unbind (dev, udev);
  1282. out1:
  1283. /* subdrivers must undo all they did in bind() if they
  1284. * fail it, but we may fail later and a deferred kevent
  1285. * may trigger an error resubmitting itself and, worse,
  1286. * schedule a timer. So we kill it all just in case.
  1287. */
  1288. cancel_work_sync(&dev->kevent);
  1289. del_timer_sync(&dev->delay);
  1290. free_netdev(net);
  1291. out:
  1292. usb_put_dev(xdev);
  1293. return status;
  1294. }
  1295. EXPORT_SYMBOL_GPL(usbnet_probe);
  1296. /*-------------------------------------------------------------------------*/
  1297. /*
  1298. * suspend the whole driver as soon as the first interface is suspended
  1299. * resume only when the last interface is resumed
  1300. */
  1301. int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
  1302. {
  1303. struct usbnet *dev = usb_get_intfdata(intf);
  1304. if (!dev->suspend_count++) {
  1305. spin_lock_irq(&dev->txq.lock);
  1306. /* don't autosuspend while transmitting */
  1307. if (dev->txq.qlen && PMSG_IS_AUTO(message)) {
  1308. dev->suspend_count--;
  1309. spin_unlock_irq(&dev->txq.lock);
  1310. return -EBUSY;
  1311. } else {
  1312. set_bit(EVENT_DEV_ASLEEP, &dev->flags);
  1313. spin_unlock_irq(&dev->txq.lock);
  1314. }
  1315. /*
  1316. * accelerate emptying of the rx and queues, to avoid
  1317. * having everything error out.
  1318. */
  1319. netif_device_detach (dev->net);
  1320. usbnet_terminate_urbs(dev);
  1321. usb_kill_urb(dev->interrupt);
  1322. /*
  1323. * reattach so runtime management can use and
  1324. * wake the device
  1325. */
  1326. netif_device_attach (dev->net);
  1327. }
  1328. return 0;
  1329. }
  1330. EXPORT_SYMBOL_GPL(usbnet_suspend);
  1331. int usbnet_resume (struct usb_interface *intf)
  1332. {
  1333. struct usbnet *dev = usb_get_intfdata(intf);
  1334. struct sk_buff *skb;
  1335. struct urb *res;
  1336. int retval;
  1337. if (!--dev->suspend_count) {
  1338. /* resume interrupt URBs */
  1339. if (dev->interrupt && test_bit(EVENT_DEV_OPEN, &dev->flags))
  1340. usb_submit_urb(dev->interrupt, GFP_NOIO);
  1341. spin_lock_irq(&dev->txq.lock);
  1342. while ((res = usb_get_from_anchor(&dev->deferred))) {
  1343. skb = (struct sk_buff *)res->context;
  1344. retval = usb_submit_urb(res, GFP_ATOMIC);
  1345. if (retval < 0) {
  1346. dev_kfree_skb_any(skb);
  1347. usb_free_urb(res);
  1348. usb_autopm_put_interface_async(dev->intf);
  1349. } else {
  1350. dev->net->trans_start = jiffies;
  1351. __skb_queue_tail(&dev->txq, skb);
  1352. }
  1353. }
  1354. smp_mb();
  1355. clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
  1356. spin_unlock_irq(&dev->txq.lock);
  1357. if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
  1358. if (!(dev->txq.qlen >= TX_QLEN(dev)))
  1359. netif_tx_wake_all_queues(dev->net);
  1360. queue_work(usbnet_wq, &dev->bh_w);
  1361. }
  1362. }
  1363. return 0;
  1364. }
  1365. EXPORT_SYMBOL_GPL(usbnet_resume);
  1366. /*-------------------------------------------------------------------------*/
  1367. static int __init usbnet_init(void)
  1368. {
  1369. /* Compiler should optimize this out. */
  1370. BUILD_BUG_ON(
  1371. FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data));
  1372. random_ether_addr(node_id);
  1373. usbnet_wq = create_singlethread_workqueue("usbnet");
  1374. if (!usbnet_wq) {
  1375. pr_err("%s: Unable to create workqueue:usbnet\n", __func__);
  1376. return -ENOMEM;
  1377. }
  1378. return 0;
  1379. }
  1380. module_init(usbnet_init);
  1381. static void __exit usbnet_exit(void)
  1382. {
  1383. destroy_workqueue(usbnet_wq);
  1384. }
  1385. module_exit(usbnet_exit);
  1386. MODULE_AUTHOR("David Brownell");
  1387. MODULE_DESCRIPTION("USB network driver framework");
  1388. MODULE_LICENSE("GPL");