usbnet.c 40 KB

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