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