usbnet.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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/config.h>
  34. #include <linux/module.h>
  35. #include <linux/sched.h>
  36. #include <linux/init.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/etherdevice.h>
  39. #include <linux/ethtool.h>
  40. #include <linux/workqueue.h>
  41. #include <linux/mii.h>
  42. #include <linux/usb.h>
  43. #include "usbnet.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_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
  58. #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
  59. // reawaken network queue this soon after stopping; else watchdog barks
  60. #define TX_TIMEOUT_JIFFIES (5*HZ)
  61. // throttle rx/tx briefly after some faults, so khubd might disconnect()
  62. // us (it polls at HZ/4 usually) before we report too many false errors.
  63. #define THROTTLE_JIFFIES (HZ/8)
  64. // between wakeups
  65. #define UNLINK_TIMEOUT_MS 3
  66. /*-------------------------------------------------------------------------*/
  67. // randomly generated ethernet address
  68. static u8 node_id [ETH_ALEN];
  69. static const char driver_name [] = "usbnet";
  70. /* use ethtool to change the level for any given device */
  71. static int msg_level = -1;
  72. module_param (msg_level, int, 0);
  73. MODULE_PARM_DESC (msg_level, "Override default message level");
  74. /*-------------------------------------------------------------------------*/
  75. /* handles CDC Ethernet and many other network "bulk data" interfaces */
  76. int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
  77. {
  78. int tmp;
  79. struct usb_host_interface *alt = NULL;
  80. struct usb_host_endpoint *in = NULL, *out = NULL;
  81. struct usb_host_endpoint *status = NULL;
  82. for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
  83. unsigned ep;
  84. in = out = status = NULL;
  85. alt = intf->altsetting + tmp;
  86. /* take the first altsetting with in-bulk + out-bulk;
  87. * remember any status endpoint, just in case;
  88. * ignore other endpoints and altsetttings.
  89. */
  90. for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
  91. struct usb_host_endpoint *e;
  92. int intr = 0;
  93. e = alt->endpoint + ep;
  94. switch (e->desc.bmAttributes) {
  95. case USB_ENDPOINT_XFER_INT:
  96. if (!(e->desc.bEndpointAddress & USB_DIR_IN))
  97. continue;
  98. intr = 1;
  99. /* FALLTHROUGH */
  100. case USB_ENDPOINT_XFER_BULK:
  101. break;
  102. default:
  103. continue;
  104. }
  105. if (e->desc.bEndpointAddress & USB_DIR_IN) {
  106. if (!intr && !in)
  107. in = e;
  108. else if (intr && !status)
  109. status = e;
  110. } else {
  111. if (!out)
  112. out = e;
  113. }
  114. }
  115. if (in && out)
  116. break;
  117. }
  118. if (!alt || !in || !out)
  119. return -EINVAL;
  120. if (alt->desc.bAlternateSetting != 0
  121. || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
  122. tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
  123. alt->desc.bAlternateSetting);
  124. if (tmp < 0)
  125. return tmp;
  126. }
  127. dev->in = usb_rcvbulkpipe (dev->udev,
  128. in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  129. dev->out = usb_sndbulkpipe (dev->udev,
  130. out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  131. dev->status = status;
  132. return 0;
  133. }
  134. EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
  135. static void intr_complete (struct urb *urb, struct pt_regs *regs);
  136. static int init_status (struct usbnet *dev, struct usb_interface *intf)
  137. {
  138. char *buf = NULL;
  139. unsigned pipe = 0;
  140. unsigned maxp;
  141. unsigned period;
  142. if (!dev->driver_info->status)
  143. return 0;
  144. pipe = usb_rcvintpipe (dev->udev,
  145. dev->status->desc.bEndpointAddress
  146. & USB_ENDPOINT_NUMBER_MASK);
  147. maxp = usb_maxpacket (dev->udev, pipe, 0);
  148. /* avoid 1 msec chatter: min 8 msec poll rate */
  149. period = max ((int) dev->status->desc.bInterval,
  150. (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
  151. buf = kmalloc (maxp, SLAB_KERNEL);
  152. if (buf) {
  153. dev->interrupt = usb_alloc_urb (0, SLAB_KERNEL);
  154. if (!dev->interrupt) {
  155. kfree (buf);
  156. return -ENOMEM;
  157. } else {
  158. usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
  159. buf, maxp, intr_complete, dev, period);
  160. dev_dbg(&intf->dev,
  161. "status ep%din, %d bytes period %d\n",
  162. usb_pipeendpoint(pipe), maxp, period);
  163. }
  164. }
  165. return 0;
  166. }
  167. /* Passes this packet up the stack, updating its accounting.
  168. * Some link protocols batch packets, so their rx_fixup paths
  169. * can return clones as well as just modify the original skb.
  170. */
  171. void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
  172. {
  173. int status;
  174. skb->dev = dev->net;
  175. skb->protocol = eth_type_trans (skb, dev->net);
  176. dev->stats.rx_packets++;
  177. dev->stats.rx_bytes += skb->len;
  178. if (netif_msg_rx_status (dev))
  179. devdbg (dev, "< rx, len %zu, type 0x%x",
  180. skb->len + sizeof (struct ethhdr), skb->protocol);
  181. memset (skb->cb, 0, sizeof (struct skb_data));
  182. status = netif_rx (skb);
  183. if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
  184. devdbg (dev, "netif_rx status %d", status);
  185. }
  186. EXPORT_SYMBOL_GPL(usbnet_skb_return);
  187. /*-------------------------------------------------------------------------
  188. *
  189. * Network Device Driver (peer link to "Host Device", from USB host)
  190. *
  191. *-------------------------------------------------------------------------*/
  192. static int usbnet_change_mtu (struct net_device *net, int new_mtu)
  193. {
  194. struct usbnet *dev = netdev_priv(net);
  195. int ll_mtu = new_mtu + net->hard_header_len;
  196. if (new_mtu <= 0 || ll_mtu > dev->hard_mtu)
  197. return -EINVAL;
  198. // no second zero-length packet read wanted after mtu-sized packets
  199. if ((ll_mtu % dev->maxpacket) == 0)
  200. return -EDOM;
  201. net->mtu = new_mtu;
  202. return 0;
  203. }
  204. /*-------------------------------------------------------------------------*/
  205. static struct net_device_stats *usbnet_get_stats (struct net_device *net)
  206. {
  207. struct usbnet *dev = netdev_priv(net);
  208. return &dev->stats;
  209. }
  210. /*-------------------------------------------------------------------------*/
  211. /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
  212. * completion callbacks. 2.5 should have fixed those bugs...
  213. */
  214. static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
  215. {
  216. unsigned long flags;
  217. spin_lock_irqsave(&list->lock, flags);
  218. __skb_unlink(skb, list);
  219. spin_unlock(&list->lock);
  220. spin_lock(&dev->done.lock);
  221. __skb_queue_tail(&dev->done, skb);
  222. if (dev->done.qlen == 1)
  223. tasklet_schedule(&dev->bh);
  224. spin_unlock_irqrestore(&dev->done.lock, flags);
  225. }
  226. /* some work can't be done in tasklets, so we use keventd
  227. *
  228. * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
  229. * but tasklet_schedule() doesn't. hope the failure is rare.
  230. */
  231. void usbnet_defer_kevent (struct usbnet *dev, int work)
  232. {
  233. set_bit (work, &dev->flags);
  234. if (!schedule_work (&dev->kevent))
  235. deverr (dev, "kevent %d may have been dropped", work);
  236. else
  237. devdbg (dev, "kevent %d scheduled", work);
  238. }
  239. EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
  240. /*-------------------------------------------------------------------------*/
  241. static void rx_complete (struct urb *urb, struct pt_regs *regs);
  242. static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
  243. {
  244. struct sk_buff *skb;
  245. struct skb_data *entry;
  246. int retval = 0;
  247. unsigned long lockflags;
  248. size_t size = dev->rx_urb_size;
  249. if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
  250. if (netif_msg_rx_err (dev))
  251. devdbg (dev, "no rx skb");
  252. usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
  253. usb_free_urb (urb);
  254. return;
  255. }
  256. skb_reserve (skb, NET_IP_ALIGN);
  257. entry = (struct skb_data *) skb->cb;
  258. entry->urb = urb;
  259. entry->dev = dev;
  260. entry->state = rx_start;
  261. entry->length = 0;
  262. usb_fill_bulk_urb (urb, dev->udev, dev->in,
  263. skb->data, size, rx_complete, skb);
  264. spin_lock_irqsave (&dev->rxq.lock, lockflags);
  265. if (netif_running (dev->net)
  266. && netif_device_present (dev->net)
  267. && !test_bit (EVENT_RX_HALT, &dev->flags)) {
  268. switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){
  269. case -EPIPE:
  270. usbnet_defer_kevent (dev, EVENT_RX_HALT);
  271. break;
  272. case -ENOMEM:
  273. usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
  274. break;
  275. case -ENODEV:
  276. if (netif_msg_ifdown (dev))
  277. devdbg (dev, "device gone");
  278. netif_device_detach (dev->net);
  279. break;
  280. default:
  281. if (netif_msg_rx_err (dev))
  282. devdbg (dev, "rx submit, %d", retval);
  283. tasklet_schedule (&dev->bh);
  284. break;
  285. case 0:
  286. __skb_queue_tail (&dev->rxq, skb);
  287. }
  288. } else {
  289. if (netif_msg_ifdown (dev))
  290. devdbg (dev, "rx: stopped");
  291. retval = -ENOLINK;
  292. }
  293. spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
  294. if (retval) {
  295. dev_kfree_skb_any (skb);
  296. usb_free_urb (urb);
  297. }
  298. }
  299. /*-------------------------------------------------------------------------*/
  300. static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
  301. {
  302. if (dev->driver_info->rx_fixup
  303. && !dev->driver_info->rx_fixup (dev, skb))
  304. goto error;
  305. // else network stack removes extra byte if we forced a short packet
  306. if (skb->len)
  307. usbnet_skb_return (dev, skb);
  308. else {
  309. if (netif_msg_rx_err (dev))
  310. devdbg (dev, "drop");
  311. error:
  312. dev->stats.rx_errors++;
  313. skb_queue_tail (&dev->done, skb);
  314. }
  315. }
  316. /*-------------------------------------------------------------------------*/
  317. static void rx_complete (struct urb *urb, struct pt_regs *regs)
  318. {
  319. struct sk_buff *skb = (struct sk_buff *) urb->context;
  320. struct skb_data *entry = (struct skb_data *) skb->cb;
  321. struct usbnet *dev = entry->dev;
  322. int urb_status = urb->status;
  323. skb_put (skb, urb->actual_length);
  324. entry->state = rx_done;
  325. entry->urb = NULL;
  326. switch (urb_status) {
  327. // success
  328. case 0:
  329. if (skb->len < dev->net->hard_header_len) {
  330. entry->state = rx_cleanup;
  331. dev->stats.rx_errors++;
  332. dev->stats.rx_length_errors++;
  333. if (netif_msg_rx_err (dev))
  334. devdbg (dev, "rx length %d", skb->len);
  335. }
  336. break;
  337. // stalls need manual reset. this is rare ... except that
  338. // when going through USB 2.0 TTs, unplug appears this way.
  339. // we avoid the highspeed version of the ETIMEOUT/EILSEQ
  340. // storm, recovering as needed.
  341. case -EPIPE:
  342. dev->stats.rx_errors++;
  343. usbnet_defer_kevent (dev, EVENT_RX_HALT);
  344. // FALLTHROUGH
  345. // software-driven interface shutdown
  346. case -ECONNRESET: // async unlink
  347. case -ESHUTDOWN: // hardware gone
  348. if (netif_msg_ifdown (dev))
  349. devdbg (dev, "rx shutdown, code %d", urb_status);
  350. goto block;
  351. // we get controller i/o faults during khubd disconnect() delays.
  352. // throttle down resubmits, to avoid log floods; just temporarily,
  353. // so we still recover when the fault isn't a khubd delay.
  354. case -EPROTO: // ehci
  355. case -ETIMEDOUT: // ohci
  356. case -EILSEQ: // uhci
  357. dev->stats.rx_errors++;
  358. if (!timer_pending (&dev->delay)) {
  359. mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
  360. if (netif_msg_link (dev))
  361. devdbg (dev, "rx throttle %d", urb_status);
  362. }
  363. block:
  364. entry->state = rx_cleanup;
  365. entry->urb = urb;
  366. urb = NULL;
  367. break;
  368. // data overrun ... flush fifo?
  369. case -EOVERFLOW:
  370. dev->stats.rx_over_errors++;
  371. // FALLTHROUGH
  372. default:
  373. entry->state = rx_cleanup;
  374. dev->stats.rx_errors++;
  375. if (netif_msg_rx_err (dev))
  376. devdbg (dev, "rx status %d", urb_status);
  377. break;
  378. }
  379. defer_bh(dev, skb, &dev->rxq);
  380. if (urb) {
  381. if (netif_running (dev->net)
  382. && !test_bit (EVENT_RX_HALT, &dev->flags)) {
  383. rx_submit (dev, urb, GFP_ATOMIC);
  384. return;
  385. }
  386. usb_free_urb (urb);
  387. }
  388. if (netif_msg_rx_err (dev))
  389. devdbg (dev, "no read resubmitted");
  390. }
  391. static void intr_complete (struct urb *urb, struct pt_regs *regs)
  392. {
  393. struct usbnet *dev = urb->context;
  394. int status = urb->status;
  395. switch (status) {
  396. /* success */
  397. case 0:
  398. dev->driver_info->status(dev, urb);
  399. break;
  400. /* software-driven interface shutdown */
  401. case -ENOENT: // urb killed
  402. case -ESHUTDOWN: // hardware gone
  403. if (netif_msg_ifdown (dev))
  404. devdbg (dev, "intr shutdown, code %d", status);
  405. return;
  406. /* NOTE: not throttling like RX/TX, since this endpoint
  407. * already polls infrequently
  408. */
  409. default:
  410. devdbg (dev, "intr status %d", status);
  411. break;
  412. }
  413. if (!netif_running (dev->net))
  414. return;
  415. memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
  416. status = usb_submit_urb (urb, GFP_ATOMIC);
  417. if (status != 0 && netif_msg_timer (dev))
  418. deverr(dev, "intr resubmit --> %d", status);
  419. }
  420. /*-------------------------------------------------------------------------*/
  421. // unlink pending rx/tx; completion handlers do all other cleanup
  422. static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
  423. {
  424. unsigned long flags;
  425. struct sk_buff *skb, *skbnext;
  426. int count = 0;
  427. spin_lock_irqsave (&q->lock, flags);
  428. for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
  429. struct skb_data *entry;
  430. struct urb *urb;
  431. int retval;
  432. entry = (struct skb_data *) skb->cb;
  433. urb = entry->urb;
  434. skbnext = skb->next;
  435. // during some PM-driven resume scenarios,
  436. // these (async) unlinks complete immediately
  437. retval = usb_unlink_urb (urb);
  438. if (retval != -EINPROGRESS && retval != 0)
  439. devdbg (dev, "unlink urb err, %d", retval);
  440. else
  441. count++;
  442. }
  443. spin_unlock_irqrestore (&q->lock, flags);
  444. return count;
  445. }
  446. /*-------------------------------------------------------------------------*/
  447. // precondition: never called in_interrupt
  448. static int usbnet_stop (struct net_device *net)
  449. {
  450. struct usbnet *dev = netdev_priv(net);
  451. int temp;
  452. DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup);
  453. DECLARE_WAITQUEUE (wait, current);
  454. netif_stop_queue (net);
  455. if (netif_msg_ifdown (dev))
  456. devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
  457. dev->stats.rx_packets, dev->stats.tx_packets,
  458. dev->stats.rx_errors, dev->stats.tx_errors
  459. );
  460. // ensure there are no more active urbs
  461. add_wait_queue (&unlink_wakeup, &wait);
  462. dev->wait = &unlink_wakeup;
  463. temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
  464. // maybe wait for deletions to finish.
  465. while (!skb_queue_empty(&dev->rxq) &&
  466. !skb_queue_empty(&dev->txq) &&
  467. !skb_queue_empty(&dev->done)) {
  468. msleep(UNLINK_TIMEOUT_MS);
  469. if (netif_msg_ifdown (dev))
  470. devdbg (dev, "waited for %d urb completions", temp);
  471. }
  472. dev->wait = NULL;
  473. remove_wait_queue (&unlink_wakeup, &wait);
  474. usb_kill_urb(dev->interrupt);
  475. /* deferred work (task, timer, softirq) must also stop.
  476. * can't flush_scheduled_work() until we drop rtnl (later),
  477. * else workers could deadlock; so make workers a NOP.
  478. */
  479. dev->flags = 0;
  480. del_timer_sync (&dev->delay);
  481. tasklet_kill (&dev->bh);
  482. return 0;
  483. }
  484. /*-------------------------------------------------------------------------*/
  485. // posts reads, and enables write queuing
  486. // precondition: never called in_interrupt
  487. static int usbnet_open (struct net_device *net)
  488. {
  489. struct usbnet *dev = netdev_priv(net);
  490. int retval = 0;
  491. struct driver_info *info = dev->driver_info;
  492. // put into "known safe" state
  493. if (info->reset && (retval = info->reset (dev)) < 0) {
  494. if (netif_msg_ifup (dev))
  495. devinfo (dev,
  496. "open reset fail (%d) usbnet usb-%s-%s, %s",
  497. retval,
  498. dev->udev->bus->bus_name, dev->udev->devpath,
  499. info->description);
  500. goto done;
  501. }
  502. // insist peer be connected
  503. if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
  504. if (netif_msg_ifup (dev))
  505. devdbg (dev, "can't open; %d", retval);
  506. goto done;
  507. }
  508. /* start any status interrupt transfer */
  509. if (dev->interrupt) {
  510. retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
  511. if (retval < 0) {
  512. if (netif_msg_ifup (dev))
  513. deverr (dev, "intr submit %d", retval);
  514. goto done;
  515. }
  516. }
  517. netif_start_queue (net);
  518. if (netif_msg_ifup (dev)) {
  519. char *framing;
  520. if (dev->driver_info->flags & FLAG_FRAMING_NC)
  521. framing = "NetChip";
  522. else if (dev->driver_info->flags & FLAG_FRAMING_GL)
  523. framing = "GeneSys";
  524. else if (dev->driver_info->flags & FLAG_FRAMING_Z)
  525. framing = "Zaurus";
  526. else if (dev->driver_info->flags & FLAG_FRAMING_RN)
  527. framing = "RNDIS";
  528. else if (dev->driver_info->flags & FLAG_FRAMING_AX)
  529. framing = "ASIX";
  530. else
  531. framing = "simple";
  532. devinfo (dev, "open: enable queueing "
  533. "(rx %d, tx %d) mtu %d %s framing",
  534. RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu,
  535. framing);
  536. }
  537. // delay posting reads until we're fully open
  538. tasklet_schedule (&dev->bh);
  539. done:
  540. return retval;
  541. }
  542. /*-------------------------------------------------------------------------*/
  543. /* ethtool methods; minidrivers may need to add some more, but
  544. * they'll probably want to use this base set.
  545. */
  546. void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
  547. {
  548. struct usbnet *dev = netdev_priv(net);
  549. /* REVISIT don't always return "usbnet" */
  550. strncpy (info->driver, driver_name, sizeof info->driver);
  551. strncpy (info->version, DRIVER_VERSION, sizeof info->version);
  552. strncpy (info->fw_version, dev->driver_info->description,
  553. sizeof info->fw_version);
  554. usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
  555. }
  556. EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
  557. static u32 usbnet_get_link (struct net_device *net)
  558. {
  559. struct usbnet *dev = netdev_priv(net);
  560. /* If a check_connect is defined, return its result */
  561. if (dev->driver_info->check_connect)
  562. return dev->driver_info->check_connect (dev) == 0;
  563. /* Otherwise, say we're up (to avoid breaking scripts) */
  564. return 1;
  565. }
  566. u32 usbnet_get_msglevel (struct net_device *net)
  567. {
  568. struct usbnet *dev = netdev_priv(net);
  569. return dev->msg_enable;
  570. }
  571. EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
  572. void usbnet_set_msglevel (struct net_device *net, u32 level)
  573. {
  574. struct usbnet *dev = netdev_priv(net);
  575. dev->msg_enable = level;
  576. }
  577. EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
  578. /* drivers may override default ethtool_ops in their bind() routine */
  579. static struct ethtool_ops usbnet_ethtool_ops = {
  580. .get_drvinfo = usbnet_get_drvinfo,
  581. .get_link = usbnet_get_link,
  582. .get_msglevel = usbnet_get_msglevel,
  583. .set_msglevel = usbnet_set_msglevel,
  584. };
  585. /*-------------------------------------------------------------------------*/
  586. /* work that cannot be done in interrupt context uses keventd.
  587. *
  588. * NOTE: with 2.5 we could do more of this using completion callbacks,
  589. * especially now that control transfers can be queued.
  590. */
  591. static void
  592. kevent (void *data)
  593. {
  594. struct usbnet *dev = data;
  595. int status;
  596. /* usb_clear_halt() needs a thread context */
  597. if (test_bit (EVENT_TX_HALT, &dev->flags)) {
  598. unlink_urbs (dev, &dev->txq);
  599. status = usb_clear_halt (dev->udev, dev->out);
  600. if (status < 0
  601. && status != -EPIPE
  602. && status != -ESHUTDOWN) {
  603. if (netif_msg_tx_err (dev))
  604. deverr (dev, "can't clear tx halt, status %d",
  605. status);
  606. } else {
  607. clear_bit (EVENT_TX_HALT, &dev->flags);
  608. if (status != -ESHUTDOWN)
  609. netif_wake_queue (dev->net);
  610. }
  611. }
  612. if (test_bit (EVENT_RX_HALT, &dev->flags)) {
  613. unlink_urbs (dev, &dev->rxq);
  614. status = usb_clear_halt (dev->udev, dev->in);
  615. if (status < 0
  616. && status != -EPIPE
  617. && status != -ESHUTDOWN) {
  618. if (netif_msg_rx_err (dev))
  619. deverr (dev, "can't clear rx halt, status %d",
  620. status);
  621. } else {
  622. clear_bit (EVENT_RX_HALT, &dev->flags);
  623. tasklet_schedule (&dev->bh);
  624. }
  625. }
  626. /* tasklet could resubmit itself forever if memory is tight */
  627. if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
  628. struct urb *urb = NULL;
  629. if (netif_running (dev->net))
  630. urb = usb_alloc_urb (0, GFP_KERNEL);
  631. else
  632. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  633. if (urb != NULL) {
  634. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  635. rx_submit (dev, urb, GFP_KERNEL);
  636. tasklet_schedule (&dev->bh);
  637. }
  638. }
  639. if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
  640. struct driver_info *info = dev->driver_info;
  641. int retval = 0;
  642. clear_bit (EVENT_LINK_RESET, &dev->flags);
  643. if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
  644. devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
  645. retval,
  646. dev->udev->bus->bus_name, dev->udev->devpath,
  647. info->description);
  648. }
  649. }
  650. if (dev->flags)
  651. devdbg (dev, "kevent done, flags = 0x%lx",
  652. dev->flags);
  653. }
  654. /*-------------------------------------------------------------------------*/
  655. static void tx_complete (struct urb *urb, struct pt_regs *regs)
  656. {
  657. struct sk_buff *skb = (struct sk_buff *) urb->context;
  658. struct skb_data *entry = (struct skb_data *) skb->cb;
  659. struct usbnet *dev = entry->dev;
  660. if (urb->status == 0) {
  661. dev->stats.tx_packets++;
  662. dev->stats.tx_bytes += entry->length;
  663. } else {
  664. dev->stats.tx_errors++;
  665. switch (urb->status) {
  666. case -EPIPE:
  667. usbnet_defer_kevent (dev, EVENT_TX_HALT);
  668. break;
  669. /* software-driven interface shutdown */
  670. case -ECONNRESET: // async unlink
  671. case -ESHUTDOWN: // hardware gone
  672. break;
  673. // like rx, tx gets controller i/o faults during khubd delays
  674. // and so it uses the same throttling mechanism.
  675. case -EPROTO: // ehci
  676. case -ETIMEDOUT: // ohci
  677. case -EILSEQ: // uhci
  678. if (!timer_pending (&dev->delay)) {
  679. mod_timer (&dev->delay,
  680. jiffies + THROTTLE_JIFFIES);
  681. if (netif_msg_link (dev))
  682. devdbg (dev, "tx throttle %d",
  683. urb->status);
  684. }
  685. netif_stop_queue (dev->net);
  686. break;
  687. default:
  688. if (netif_msg_tx_err (dev))
  689. devdbg (dev, "tx err %d", entry->urb->status);
  690. break;
  691. }
  692. }
  693. urb->dev = NULL;
  694. entry->state = tx_done;
  695. defer_bh(dev, skb, &dev->txq);
  696. }
  697. /*-------------------------------------------------------------------------*/
  698. static void usbnet_tx_timeout (struct net_device *net)
  699. {
  700. struct usbnet *dev = netdev_priv(net);
  701. unlink_urbs (dev, &dev->txq);
  702. tasklet_schedule (&dev->bh);
  703. // FIXME: device recovery -- reset?
  704. }
  705. /*-------------------------------------------------------------------------*/
  706. static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
  707. {
  708. struct usbnet *dev = netdev_priv(net);
  709. int length;
  710. int retval = NET_XMIT_SUCCESS;
  711. struct urb *urb = NULL;
  712. struct skb_data *entry;
  713. struct driver_info *info = dev->driver_info;
  714. unsigned long flags;
  715. // some devices want funky USB-level framing, for
  716. // win32 driver (usually) and/or hardware quirks
  717. if (info->tx_fixup) {
  718. skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
  719. if (!skb) {
  720. if (netif_msg_tx_err (dev))
  721. devdbg (dev, "can't tx_fixup skb");
  722. goto drop;
  723. }
  724. }
  725. length = skb->len;
  726. if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
  727. if (netif_msg_tx_err (dev))
  728. devdbg (dev, "no urb");
  729. goto drop;
  730. }
  731. entry = (struct skb_data *) skb->cb;
  732. entry->urb = urb;
  733. entry->dev = dev;
  734. entry->state = tx_start;
  735. entry->length = length;
  736. usb_fill_bulk_urb (urb, dev->udev, dev->out,
  737. skb->data, skb->len, tx_complete, skb);
  738. /* don't assume the hardware handles USB_ZERO_PACKET
  739. * NOTE: strictly conforming cdc-ether devices should expect
  740. * the ZLP here, but ignore the one-byte packet.
  741. *
  742. * FIXME zero that byte, if it doesn't require a new skb.
  743. */
  744. if ((length % dev->maxpacket) == 0)
  745. urb->transfer_buffer_length++;
  746. spin_lock_irqsave (&dev->txq.lock, flags);
  747. switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
  748. case -EPIPE:
  749. netif_stop_queue (net);
  750. usbnet_defer_kevent (dev, EVENT_TX_HALT);
  751. break;
  752. default:
  753. if (netif_msg_tx_err (dev))
  754. devdbg (dev, "tx: submit urb err %d", retval);
  755. break;
  756. case 0:
  757. net->trans_start = jiffies;
  758. __skb_queue_tail (&dev->txq, skb);
  759. if (dev->txq.qlen >= TX_QLEN (dev))
  760. netif_stop_queue (net);
  761. }
  762. spin_unlock_irqrestore (&dev->txq.lock, flags);
  763. if (retval) {
  764. if (netif_msg_tx_err (dev))
  765. devdbg (dev, "drop, code %d", retval);
  766. drop:
  767. retval = NET_XMIT_SUCCESS;
  768. dev->stats.tx_dropped++;
  769. if (skb)
  770. dev_kfree_skb_any (skb);
  771. usb_free_urb (urb);
  772. } else if (netif_msg_tx_queued (dev)) {
  773. devdbg (dev, "> tx, len %d, type 0x%x",
  774. length, skb->protocol);
  775. }
  776. return retval;
  777. }
  778. /*-------------------------------------------------------------------------*/
  779. // tasklet (work deferred from completions, in_irq) or timer
  780. static void usbnet_bh (unsigned long param)
  781. {
  782. struct usbnet *dev = (struct usbnet *) param;
  783. struct sk_buff *skb;
  784. struct skb_data *entry;
  785. while ((skb = skb_dequeue (&dev->done))) {
  786. entry = (struct skb_data *) skb->cb;
  787. switch (entry->state) {
  788. case rx_done:
  789. entry->state = rx_cleanup;
  790. rx_process (dev, skb);
  791. continue;
  792. case tx_done:
  793. case rx_cleanup:
  794. usb_free_urb (entry->urb);
  795. dev_kfree_skb (skb);
  796. continue;
  797. default:
  798. devdbg (dev, "bogus skb state %d", entry->state);
  799. }
  800. }
  801. // waiting for all pending urbs to complete?
  802. if (dev->wait) {
  803. if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
  804. wake_up (dev->wait);
  805. }
  806. // or are we maybe short a few urbs?
  807. } else if (netif_running (dev->net)
  808. && netif_device_present (dev->net)
  809. && !timer_pending (&dev->delay)
  810. && !test_bit (EVENT_RX_HALT, &dev->flags)) {
  811. int temp = dev->rxq.qlen;
  812. int qlen = RX_QLEN (dev);
  813. if (temp < qlen) {
  814. struct urb *urb;
  815. int i;
  816. // don't refill the queue all at once
  817. for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
  818. urb = usb_alloc_urb (0, GFP_ATOMIC);
  819. if (urb != NULL)
  820. rx_submit (dev, urb, GFP_ATOMIC);
  821. }
  822. if (temp != dev->rxq.qlen && netif_msg_link (dev))
  823. devdbg (dev, "rxqlen %d --> %d",
  824. temp, dev->rxq.qlen);
  825. if (dev->rxq.qlen < qlen)
  826. tasklet_schedule (&dev->bh);
  827. }
  828. if (dev->txq.qlen < TX_QLEN (dev))
  829. netif_wake_queue (dev->net);
  830. }
  831. }
  832. /*-------------------------------------------------------------------------
  833. *
  834. * USB Device Driver support
  835. *
  836. *-------------------------------------------------------------------------*/
  837. // precondition: never called in_interrupt
  838. void usbnet_disconnect (struct usb_interface *intf)
  839. {
  840. struct usbnet *dev;
  841. struct usb_device *xdev;
  842. struct net_device *net;
  843. dev = usb_get_intfdata(intf);
  844. usb_set_intfdata(intf, NULL);
  845. if (!dev)
  846. return;
  847. xdev = interface_to_usbdev (intf);
  848. if (netif_msg_probe (dev))
  849. devinfo (dev, "unregister '%s' usb-%s-%s, %s",
  850. intf->dev.driver->name,
  851. xdev->bus->bus_name, xdev->devpath,
  852. dev->driver_info->description);
  853. net = dev->net;
  854. unregister_netdev (net);
  855. /* we don't hold rtnl here ... */
  856. flush_scheduled_work ();
  857. if (dev->driver_info->unbind)
  858. dev->driver_info->unbind (dev, intf);
  859. free_netdev(net);
  860. usb_put_dev (xdev);
  861. }
  862. EXPORT_SYMBOL_GPL(usbnet_disconnect);
  863. /*-------------------------------------------------------------------------*/
  864. // precondition: never called in_interrupt
  865. int
  866. usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
  867. {
  868. struct usbnet *dev;
  869. struct net_device *net;
  870. struct usb_host_interface *interface;
  871. struct driver_info *info;
  872. struct usb_device *xdev;
  873. int status;
  874. info = (struct driver_info *) prod->driver_info;
  875. if (!info) {
  876. dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
  877. return -ENODEV;
  878. }
  879. xdev = interface_to_usbdev (udev);
  880. interface = udev->cur_altsetting;
  881. usb_get_dev (xdev);
  882. status = -ENOMEM;
  883. // set up our own records
  884. net = alloc_etherdev(sizeof(*dev));
  885. if (!net) {
  886. dbg ("can't kmalloc dev");
  887. goto out;
  888. }
  889. dev = netdev_priv(net);
  890. dev->udev = xdev;
  891. dev->driver_info = info;
  892. dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
  893. | NETIF_MSG_PROBE | NETIF_MSG_LINK);
  894. skb_queue_head_init (&dev->rxq);
  895. skb_queue_head_init (&dev->txq);
  896. skb_queue_head_init (&dev->done);
  897. dev->bh.func = usbnet_bh;
  898. dev->bh.data = (unsigned long) dev;
  899. INIT_WORK (&dev->kevent, kevent, dev);
  900. dev->delay.function = usbnet_bh;
  901. dev->delay.data = (unsigned long) dev;
  902. init_timer (&dev->delay);
  903. SET_MODULE_OWNER (net);
  904. dev->net = net;
  905. strcpy (net->name, "usb%d");
  906. memcpy (net->dev_addr, node_id, sizeof node_id);
  907. /* rx and tx sides can use different message sizes;
  908. * bind() should set rx_urb_size in that case.
  909. */
  910. dev->hard_mtu = net->mtu + net->hard_header_len;
  911. #if 0
  912. // dma_supported() is deeply broken on almost all architectures
  913. // possible with some EHCI controllers
  914. if (dma_supported (&udev->dev, DMA_64BIT_MASK))
  915. net->features |= NETIF_F_HIGHDMA;
  916. #endif
  917. net->change_mtu = usbnet_change_mtu;
  918. net->get_stats = usbnet_get_stats;
  919. net->hard_start_xmit = usbnet_start_xmit;
  920. net->open = usbnet_open;
  921. net->stop = usbnet_stop;
  922. net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
  923. net->tx_timeout = usbnet_tx_timeout;
  924. net->ethtool_ops = &usbnet_ethtool_ops;
  925. // allow device-specific bind/init procedures
  926. // NOTE net->name still not usable ...
  927. if (info->bind) {
  928. status = info->bind (dev, udev);
  929. // heuristic: "usb%d" for links we know are two-host,
  930. // else "eth%d" when there's reasonable doubt. userspace
  931. // can rename the link if it knows better.
  932. if ((dev->driver_info->flags & FLAG_ETHER) != 0
  933. && (net->dev_addr [0] & 0x02) == 0)
  934. strcpy (net->name, "eth%d");
  935. /* maybe the remote can't receive an Ethernet MTU */
  936. if (net->mtu > (dev->hard_mtu - net->hard_header_len))
  937. net->mtu = dev->hard_mtu - net->hard_header_len;
  938. } else if (!info->in || !info->out)
  939. status = usbnet_get_endpoints (dev, udev);
  940. else {
  941. dev->in = usb_rcvbulkpipe (xdev, info->in);
  942. dev->out = usb_sndbulkpipe (xdev, info->out);
  943. if (!(info->flags & FLAG_NO_SETINT))
  944. status = usb_set_interface (xdev,
  945. interface->desc.bInterfaceNumber,
  946. interface->desc.bAlternateSetting);
  947. else
  948. status = 0;
  949. }
  950. if (status == 0 && dev->status)
  951. status = init_status (dev, udev);
  952. if (status < 0)
  953. goto out1;
  954. if (!dev->rx_urb_size)
  955. dev->rx_urb_size = dev->hard_mtu;
  956. dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
  957. SET_NETDEV_DEV(net, &udev->dev);
  958. status = register_netdev (net);
  959. if (status)
  960. goto out3;
  961. if (netif_msg_probe (dev))
  962. devinfo (dev, "register '%s' at usb-%s-%s, %s, "
  963. "%02x:%02x:%02x:%02x:%02x:%02x",
  964. udev->dev.driver->name,
  965. xdev->bus->bus_name, xdev->devpath,
  966. dev->driver_info->description,
  967. net->dev_addr [0], net->dev_addr [1],
  968. net->dev_addr [2], net->dev_addr [3],
  969. net->dev_addr [4], net->dev_addr [5]);
  970. // ok, it's ready to go.
  971. usb_set_intfdata (udev, dev);
  972. // start as if the link is up
  973. netif_device_attach (net);
  974. return 0;
  975. out3:
  976. if (info->unbind)
  977. info->unbind (dev, udev);
  978. out1:
  979. free_netdev(net);
  980. out:
  981. usb_put_dev(xdev);
  982. return status;
  983. }
  984. EXPORT_SYMBOL_GPL(usbnet_probe);
  985. /*-------------------------------------------------------------------------*/
  986. /* FIXME these suspend/resume methods assume non-CDC style
  987. * devices, with only one interface.
  988. */
  989. int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
  990. {
  991. struct usbnet *dev = usb_get_intfdata(intf);
  992. /* accelerate emptying of the rx and queues, to avoid
  993. * having everything error out.
  994. */
  995. netif_device_detach (dev->net);
  996. (void) unlink_urbs (dev, &dev->rxq);
  997. (void) unlink_urbs (dev, &dev->txq);
  998. return 0;
  999. }
  1000. EXPORT_SYMBOL_GPL(usbnet_suspend);
  1001. int usbnet_resume (struct usb_interface *intf)
  1002. {
  1003. struct usbnet *dev = usb_get_intfdata(intf);
  1004. netif_device_attach (dev->net);
  1005. tasklet_schedule (&dev->bh);
  1006. return 0;
  1007. }
  1008. EXPORT_SYMBOL_GPL(usbnet_resume);
  1009. /*-------------------------------------------------------------------------*/
  1010. static int __init usbnet_init(void)
  1011. {
  1012. /* compiler should optimize this out */
  1013. BUG_ON (sizeof (((struct sk_buff *)0)->cb)
  1014. < sizeof (struct skb_data));
  1015. random_ether_addr(node_id);
  1016. return 0;
  1017. }
  1018. module_init(usbnet_init);
  1019. static void __exit usbnet_exit(void)
  1020. {
  1021. }
  1022. module_exit(usbnet_exit);
  1023. MODULE_AUTHOR("David Brownell");
  1024. MODULE_DESCRIPTION("USB network driver framework");
  1025. MODULE_LICENSE("GPL");