usbnet.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. /*
  2. * USB Networking Links
  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:
  23. *
  24. * + USB host-to-host "network cables", used for IP-over-USB links.
  25. * These are often used for Laplink style connectivity products.
  26. * - AnchorChip 2720
  27. * - Belkin, eTEK (interops with Win32 drivers)
  28. * - GeneSys GL620USB-A
  29. * - NetChip 1080 (interoperates with NetChip Win32 drivers)
  30. * - Prolific PL-2301/2302 (replaces "plusb" driver)
  31. * - KC Technology KC2190
  32. *
  33. * + Smart USB devices can support such links directly, using Internet
  34. * standard protocols instead of proprietary host-to-device links.
  35. * - Linux PDAs like iPaq, Yopy, and Zaurus
  36. * - The BLOB boot loader (for diskless booting)
  37. * - Linux "gadgets", perhaps using PXA-2xx or Net2280 controllers
  38. * - Devices using EPSON's sample USB firmware
  39. * - CDC-Ethernet class devices, such as many cable modems
  40. *
  41. * + Adapters to networks such as Ethernet.
  42. * - AX8817X based USB 2.0 products
  43. *
  44. * Links to these devices can be bridged using Linux Ethernet bridging.
  45. * With minor exceptions, these all use similar USB framing for network
  46. * traffic, but need different protocols for control traffic.
  47. *
  48. * USB devices can implement their side of this protocol at the cost
  49. * of two bulk endpoints; it's not restricted to "cable" applications.
  50. * See the SA1110, Zaurus, or EPSON device/client support in this driver;
  51. * slave/target drivers such as "usb-eth" (on most SA-1100 PDAs) or
  52. * "g_ether" (in the Linux "gadget" framework) implement that behavior
  53. * within devices.
  54. *
  55. *
  56. * CHANGELOG:
  57. *
  58. * 13-sep-2000 experimental, new
  59. * 10-oct-2000 usb_device_id table created.
  60. * 28-oct-2000 misc fixes; mostly, discard more TTL-mangled rx packets.
  61. * 01-nov-2000 usb_device_id table and probing api update by
  62. * Adam J. Richter <adam@yggdrasil.com>.
  63. * 18-dec-2000 (db) tx watchdog, "net1080" renaming to "usbnet", device_info
  64. * and prolific support, isolate net1080-specific bits, cleanup.
  65. * fix unlink_urbs oops in D3 PM resume code path.
  66. *
  67. * 02-feb-2001 (db) fix tx skb sharing, packet length, match_flags, ...
  68. * 08-feb-2001 stubbed in "linuxdev", maybe the SA-1100 folk can use it;
  69. * AnchorChips 2720 support (from spec) for testing;
  70. * fix bit-ordering problem with ethernet multicast addr
  71. * 19-feb-2001 Support for clearing halt conditions. SA1100 UDC support
  72. * updates. Oleg Drokin (green@iXcelerator.com)
  73. * 25-mar-2001 More SA-1100 updates, including workaround for ip problem
  74. * expecting cleared skb->cb and framing change to match latest
  75. * handhelds.org version (Oleg). Enable device IDs from the
  76. * Win32 Belkin driver; other cleanups (db).
  77. * 16-jul-2001 Bugfixes for uhci oops-on-unplug, Belkin support, various
  78. * cleanups for problems not yet seen in the field. (db)
  79. * 17-oct-2001 Handle "Advance USBNET" product, like Belkin/eTEK devices,
  80. * from Ioannis Mavroukakis <i.mavroukakis@btinternet.com>;
  81. * rx unlinks somehow weren't async; minor cleanup.
  82. * 03-nov-2001 Merged GeneSys driver; original code from Jiun-Jie Huang
  83. * <huangjj@genesyslogic.com.tw>, updated by Stanislav Brabec
  84. * <utx@penguin.cz>. Made framing options (NetChip/GeneSys)
  85. * tie mostly to (sub)driver info. Workaround some PL-2302
  86. * chips that seem to reject SET_INTERFACE requests.
  87. *
  88. * 06-apr-2002 Added ethtool support, based on a patch from Brad Hards.
  89. * Level of diagnostics is more configurable; they use device
  90. * location (usb_device->devpath) instead of address (2.5).
  91. * For tx_fixup, memflags can't be NOIO.
  92. * 07-may-2002 Generalize/cleanup keventd support, handling rx stalls (mostly
  93. * for USB 2.0 TTs) and memory shortages (potential) too. (db)
  94. * Use "locally assigned" IEEE802 address space. (Brad Hards)
  95. * 18-oct-2002 Support for Zaurus (Pavel Machek), related cleanup (db).
  96. * 14-dec-2002 Remove Zaurus-private crc32 code (Pavel); 2.5 oops fix,
  97. * cleanups and stubbed PXA-250 support (db), fix for framing
  98. * issues on Z, net1080, and gl620a (Toby Milne)
  99. *
  100. * 31-mar-2003 Use endpoint descriptors: high speed support, simpler sa1100
  101. * vs pxa25x, and CDC Ethernet. Throttle down log floods on
  102. * disconnect; other cleanups. (db) Flush net1080 fifos
  103. * after several sequential framing errors. (Johannes Erdfelt)
  104. * 22-aug-2003 AX8817X support (Dave Hollis).
  105. * 14-jun-2004 Trivial patch for AX8817X based Buffalo LUA-U2-KTX in Japan
  106. * (Neil Bortnak)
  107. * 03-nov-2004 Trivial patch for KC2190 (KC-190) chip. (Jonathan McDowell)
  108. *
  109. * 01-feb-2005 AX88772 support (Phil Chang & Dave Hollis)
  110. *-------------------------------------------------------------------------*/
  111. // #define DEBUG // error path messages, extra info
  112. // #define VERBOSE // more; success messages
  113. #include <linux/config.h>
  114. #ifdef CONFIG_USB_DEBUG
  115. # define DEBUG
  116. #endif
  117. #include <linux/module.h>
  118. #include <linux/sched.h>
  119. #include <linux/init.h>
  120. #include <linux/netdevice.h>
  121. #include <linux/etherdevice.h>
  122. #include <linux/ethtool.h>
  123. #include <linux/workqueue.h>
  124. #include <linux/mii.h>
  125. #include <linux/usb.h>
  126. #include "usbnet.h"
  127. #define DRIVER_VERSION "22-Aug-2005"
  128. /*-------------------------------------------------------------------------*/
  129. /*
  130. * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
  131. * Several dozen bytes of IPv4 data can fit in two such transactions.
  132. * One maximum size Ethernet packet takes twenty four of them.
  133. * For high speed, each frame comfortably fits almost 36 max size
  134. * Ethernet packets (so queues should be bigger).
  135. *
  136. * REVISIT qlens should be members of 'struct usbnet'; the goal is to
  137. * let the USB host controller be busy for 5msec or more before an irq
  138. * is required, under load. Jumbograms change the equation.
  139. */
  140. #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
  141. #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
  142. // reawaken network queue this soon after stopping; else watchdog barks
  143. #define TX_TIMEOUT_JIFFIES (5*HZ)
  144. // throttle rx/tx briefly after some faults, so khubd might disconnect()
  145. // us (it polls at HZ/4 usually) before we report too many false errors.
  146. #define THROTTLE_JIFFIES (HZ/8)
  147. // between wakeups
  148. #define UNLINK_TIMEOUT_MS 3
  149. /*-------------------------------------------------------------------------*/
  150. // randomly generated ethernet address
  151. static u8 node_id [ETH_ALEN];
  152. static const char driver_name [] = "usbnet";
  153. /* use ethtool to change the level for any given device */
  154. static int msg_level = -1;
  155. module_param (msg_level, int, 0);
  156. MODULE_PARM_DESC (msg_level, "Override default message level");
  157. /*-------------------------------------------------------------------------*/
  158. /* handles CDC Ethernet and many other network "bulk data" interfaces */
  159. int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
  160. {
  161. int tmp;
  162. struct usb_host_interface *alt = NULL;
  163. struct usb_host_endpoint *in = NULL, *out = NULL;
  164. struct usb_host_endpoint *status = NULL;
  165. for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
  166. unsigned ep;
  167. in = out = status = NULL;
  168. alt = intf->altsetting + tmp;
  169. /* take the first altsetting with in-bulk + out-bulk;
  170. * remember any status endpoint, just in case;
  171. * ignore other endpoints and altsetttings.
  172. */
  173. for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
  174. struct usb_host_endpoint *e;
  175. int intr = 0;
  176. e = alt->endpoint + ep;
  177. switch (e->desc.bmAttributes) {
  178. case USB_ENDPOINT_XFER_INT:
  179. if (!(e->desc.bEndpointAddress & USB_DIR_IN))
  180. continue;
  181. intr = 1;
  182. /* FALLTHROUGH */
  183. case USB_ENDPOINT_XFER_BULK:
  184. break;
  185. default:
  186. continue;
  187. }
  188. if (e->desc.bEndpointAddress & USB_DIR_IN) {
  189. if (!intr && !in)
  190. in = e;
  191. else if (intr && !status)
  192. status = e;
  193. } else {
  194. if (!out)
  195. out = e;
  196. }
  197. }
  198. if (in && out)
  199. break;
  200. }
  201. if (!alt || !in || !out)
  202. return -EINVAL;
  203. if (alt->desc.bAlternateSetting != 0
  204. || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
  205. tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
  206. alt->desc.bAlternateSetting);
  207. if (tmp < 0)
  208. return tmp;
  209. }
  210. dev->in = usb_rcvbulkpipe (dev->udev,
  211. in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  212. dev->out = usb_sndbulkpipe (dev->udev,
  213. out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  214. dev->status = status;
  215. return 0;
  216. }
  217. EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
  218. static void intr_complete (struct urb *urb, struct pt_regs *regs);
  219. static int init_status (struct usbnet *dev, struct usb_interface *intf)
  220. {
  221. char *buf = NULL;
  222. unsigned pipe = 0;
  223. unsigned maxp;
  224. unsigned period;
  225. if (!dev->driver_info->status)
  226. return 0;
  227. pipe = usb_rcvintpipe (dev->udev,
  228. dev->status->desc.bEndpointAddress
  229. & USB_ENDPOINT_NUMBER_MASK);
  230. maxp = usb_maxpacket (dev->udev, pipe, 0);
  231. /* avoid 1 msec chatter: min 8 msec poll rate */
  232. period = max ((int) dev->status->desc.bInterval,
  233. (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
  234. buf = kmalloc (maxp, SLAB_KERNEL);
  235. if (buf) {
  236. dev->interrupt = usb_alloc_urb (0, SLAB_KERNEL);
  237. if (!dev->interrupt) {
  238. kfree (buf);
  239. return -ENOMEM;
  240. } else {
  241. usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
  242. buf, maxp, intr_complete, dev, period);
  243. dev_dbg(&intf->dev,
  244. "status ep%din, %d bytes period %d\n",
  245. usb_pipeendpoint(pipe), maxp, period);
  246. }
  247. }
  248. return 0;
  249. }
  250. /* Passes this packet up the stack, updating its accounting.
  251. * Some link protocols batch packets, so their rx_fixup paths
  252. * can return clones as well as just modify the original skb.
  253. */
  254. void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
  255. {
  256. int status;
  257. skb->dev = dev->net;
  258. skb->protocol = eth_type_trans (skb, dev->net);
  259. dev->stats.rx_packets++;
  260. dev->stats.rx_bytes += skb->len;
  261. if (netif_msg_rx_status (dev))
  262. devdbg (dev, "< rx, len %zu, type 0x%x",
  263. skb->len + sizeof (struct ethhdr), skb->protocol);
  264. memset (skb->cb, 0, sizeof (struct skb_data));
  265. status = netif_rx (skb);
  266. if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
  267. devdbg (dev, "netif_rx status %d", status);
  268. }
  269. EXPORT_SYMBOL_GPL(usbnet_skb_return);
  270. #ifdef CONFIG_USB_PL2301
  271. #define HAVE_HARDWARE
  272. /*-------------------------------------------------------------------------
  273. *
  274. * Prolific PL-2301/PL-2302 driver ... http://www.prolifictech.com
  275. *
  276. * The protocol and handshaking used here should be bug-compatible
  277. * with the Linux 2.2 "plusb" driver, by Deti Fliegl.
  278. *
  279. *-------------------------------------------------------------------------*/
  280. /*
  281. * Bits 0-4 can be used for software handshaking; they're set from
  282. * one end, cleared from the other, "read" with the interrupt byte.
  283. */
  284. #define PL_S_EN (1<<7) /* (feature only) suspend enable */
  285. /* reserved bit -- rx ready (6) ? */
  286. #define PL_TX_READY (1<<5) /* (interrupt only) transmit ready */
  287. #define PL_RESET_OUT (1<<4) /* reset output pipe */
  288. #define PL_RESET_IN (1<<3) /* reset input pipe */
  289. #define PL_TX_C (1<<2) /* transmission complete */
  290. #define PL_TX_REQ (1<<1) /* transmission received */
  291. #define PL_PEER_E (1<<0) /* peer exists */
  292. static inline int
  293. pl_vendor_req (struct usbnet *dev, u8 req, u8 val, u8 index)
  294. {
  295. return usb_control_msg (dev->udev,
  296. usb_rcvctrlpipe (dev->udev, 0),
  297. req,
  298. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  299. val, index,
  300. NULL, 0,
  301. USB_CTRL_GET_TIMEOUT);
  302. }
  303. static inline int
  304. pl_clear_QuickLink_features (struct usbnet *dev, int val)
  305. {
  306. return pl_vendor_req (dev, 1, (u8) val, 0);
  307. }
  308. static inline int
  309. pl_set_QuickLink_features (struct usbnet *dev, int val)
  310. {
  311. return pl_vendor_req (dev, 3, (u8) val, 0);
  312. }
  313. /*-------------------------------------------------------------------------*/
  314. static int pl_reset (struct usbnet *dev)
  315. {
  316. /* some units seem to need this reset, others reject it utterly.
  317. * FIXME be more like "naplink" or windows drivers.
  318. */
  319. (void) pl_set_QuickLink_features (dev,
  320. PL_S_EN|PL_RESET_OUT|PL_RESET_IN|PL_PEER_E);
  321. return 0;
  322. }
  323. static const struct driver_info prolific_info = {
  324. .description = "Prolific PL-2301/PL-2302",
  325. .flags = FLAG_NO_SETINT,
  326. /* some PL-2302 versions seem to fail usb_set_interface() */
  327. .reset = pl_reset,
  328. };
  329. #endif /* CONFIG_USB_PL2301 */
  330. /*-------------------------------------------------------------------------
  331. *
  332. * Network Device Driver (peer link to "Host Device", from USB host)
  333. *
  334. *-------------------------------------------------------------------------*/
  335. static int usbnet_change_mtu (struct net_device *net, int new_mtu)
  336. {
  337. struct usbnet *dev = netdev_priv(net);
  338. int ll_mtu = new_mtu + net->hard_header_len;
  339. if (new_mtu <= 0 || ll_mtu > dev->hard_mtu)
  340. return -EINVAL;
  341. // no second zero-length packet read wanted after mtu-sized packets
  342. if ((ll_mtu % dev->maxpacket) == 0)
  343. return -EDOM;
  344. net->mtu = new_mtu;
  345. return 0;
  346. }
  347. /*-------------------------------------------------------------------------*/
  348. static struct net_device_stats *usbnet_get_stats (struct net_device *net)
  349. {
  350. struct usbnet *dev = netdev_priv(net);
  351. return &dev->stats;
  352. }
  353. /*-------------------------------------------------------------------------*/
  354. /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
  355. * completion callbacks. 2.5 should have fixed those bugs...
  356. */
  357. static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
  358. {
  359. unsigned long flags;
  360. spin_lock_irqsave(&list->lock, flags);
  361. __skb_unlink(skb, list);
  362. spin_unlock(&list->lock);
  363. spin_lock(&dev->done.lock);
  364. __skb_queue_tail(&dev->done, skb);
  365. if (dev->done.qlen == 1)
  366. tasklet_schedule(&dev->bh);
  367. spin_unlock_irqrestore(&dev->done.lock, flags);
  368. }
  369. /* some work can't be done in tasklets, so we use keventd
  370. *
  371. * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
  372. * but tasklet_schedule() doesn't. hope the failure is rare.
  373. */
  374. void usbnet_defer_kevent (struct usbnet *dev, int work)
  375. {
  376. set_bit (work, &dev->flags);
  377. if (!schedule_work (&dev->kevent))
  378. deverr (dev, "kevent %d may have been dropped", work);
  379. else
  380. devdbg (dev, "kevent %d scheduled", work);
  381. }
  382. EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
  383. /*-------------------------------------------------------------------------*/
  384. static void rx_complete (struct urb *urb, struct pt_regs *regs);
  385. static void rx_submit (struct usbnet *dev, struct urb *urb, unsigned flags)
  386. {
  387. struct sk_buff *skb;
  388. struct skb_data *entry;
  389. int retval = 0;
  390. unsigned long lockflags;
  391. size_t size = dev->rx_urb_size;
  392. if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
  393. if (netif_msg_rx_err (dev))
  394. devdbg (dev, "no rx skb");
  395. usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
  396. usb_free_urb (urb);
  397. return;
  398. }
  399. skb_reserve (skb, NET_IP_ALIGN);
  400. entry = (struct skb_data *) skb->cb;
  401. entry->urb = urb;
  402. entry->dev = dev;
  403. entry->state = rx_start;
  404. entry->length = 0;
  405. usb_fill_bulk_urb (urb, dev->udev, dev->in,
  406. skb->data, size, rx_complete, skb);
  407. spin_lock_irqsave (&dev->rxq.lock, lockflags);
  408. if (netif_running (dev->net)
  409. && netif_device_present (dev->net)
  410. && !test_bit (EVENT_RX_HALT, &dev->flags)) {
  411. switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){
  412. case -EPIPE:
  413. usbnet_defer_kevent (dev, EVENT_RX_HALT);
  414. break;
  415. case -ENOMEM:
  416. usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
  417. break;
  418. case -ENODEV:
  419. if (netif_msg_ifdown (dev))
  420. devdbg (dev, "device gone");
  421. netif_device_detach (dev->net);
  422. break;
  423. default:
  424. if (netif_msg_rx_err (dev))
  425. devdbg (dev, "rx submit, %d", retval);
  426. tasklet_schedule (&dev->bh);
  427. break;
  428. case 0:
  429. __skb_queue_tail (&dev->rxq, skb);
  430. }
  431. } else {
  432. if (netif_msg_ifdown (dev))
  433. devdbg (dev, "rx: stopped");
  434. retval = -ENOLINK;
  435. }
  436. spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
  437. if (retval) {
  438. dev_kfree_skb_any (skb);
  439. usb_free_urb (urb);
  440. }
  441. }
  442. /*-------------------------------------------------------------------------*/
  443. static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
  444. {
  445. if (dev->driver_info->rx_fixup
  446. && !dev->driver_info->rx_fixup (dev, skb))
  447. goto error;
  448. // else network stack removes extra byte if we forced a short packet
  449. if (skb->len)
  450. usbnet_skb_return (dev, skb);
  451. else {
  452. if (netif_msg_rx_err (dev))
  453. devdbg (dev, "drop");
  454. error:
  455. dev->stats.rx_errors++;
  456. skb_queue_tail (&dev->done, skb);
  457. }
  458. }
  459. /*-------------------------------------------------------------------------*/
  460. static void rx_complete (struct urb *urb, struct pt_regs *regs)
  461. {
  462. struct sk_buff *skb = (struct sk_buff *) urb->context;
  463. struct skb_data *entry = (struct skb_data *) skb->cb;
  464. struct usbnet *dev = entry->dev;
  465. int urb_status = urb->status;
  466. skb_put (skb, urb->actual_length);
  467. entry->state = rx_done;
  468. entry->urb = NULL;
  469. switch (urb_status) {
  470. // success
  471. case 0:
  472. if (skb->len < dev->net->hard_header_len) {
  473. entry->state = rx_cleanup;
  474. dev->stats.rx_errors++;
  475. dev->stats.rx_length_errors++;
  476. if (netif_msg_rx_err (dev))
  477. devdbg (dev, "rx length %d", skb->len);
  478. }
  479. break;
  480. // stalls need manual reset. this is rare ... except that
  481. // when going through USB 2.0 TTs, unplug appears this way.
  482. // we avoid the highspeed version of the ETIMEOUT/EILSEQ
  483. // storm, recovering as needed.
  484. case -EPIPE:
  485. dev->stats.rx_errors++;
  486. usbnet_defer_kevent (dev, EVENT_RX_HALT);
  487. // FALLTHROUGH
  488. // software-driven interface shutdown
  489. case -ECONNRESET: // async unlink
  490. case -ESHUTDOWN: // hardware gone
  491. if (netif_msg_ifdown (dev))
  492. devdbg (dev, "rx shutdown, code %d", urb_status);
  493. goto block;
  494. // we get controller i/o faults during khubd disconnect() delays.
  495. // throttle down resubmits, to avoid log floods; just temporarily,
  496. // so we still recover when the fault isn't a khubd delay.
  497. case -EPROTO: // ehci
  498. case -ETIMEDOUT: // ohci
  499. case -EILSEQ: // uhci
  500. dev->stats.rx_errors++;
  501. if (!timer_pending (&dev->delay)) {
  502. mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
  503. if (netif_msg_link (dev))
  504. devdbg (dev, "rx throttle %d", urb_status);
  505. }
  506. block:
  507. entry->state = rx_cleanup;
  508. entry->urb = urb;
  509. urb = NULL;
  510. break;
  511. // data overrun ... flush fifo?
  512. case -EOVERFLOW:
  513. dev->stats.rx_over_errors++;
  514. // FALLTHROUGH
  515. default:
  516. entry->state = rx_cleanup;
  517. dev->stats.rx_errors++;
  518. if (netif_msg_rx_err (dev))
  519. devdbg (dev, "rx status %d", urb_status);
  520. break;
  521. }
  522. defer_bh(dev, skb, &dev->rxq);
  523. if (urb) {
  524. if (netif_running (dev->net)
  525. && !test_bit (EVENT_RX_HALT, &dev->flags)) {
  526. rx_submit (dev, urb, GFP_ATOMIC);
  527. return;
  528. }
  529. usb_free_urb (urb);
  530. }
  531. if (netif_msg_rx_err (dev))
  532. devdbg (dev, "no read resubmitted");
  533. }
  534. static void intr_complete (struct urb *urb, struct pt_regs *regs)
  535. {
  536. struct usbnet *dev = urb->context;
  537. int status = urb->status;
  538. switch (status) {
  539. /* success */
  540. case 0:
  541. dev->driver_info->status(dev, urb);
  542. break;
  543. /* software-driven interface shutdown */
  544. case -ENOENT: // urb killed
  545. case -ESHUTDOWN: // hardware gone
  546. if (netif_msg_ifdown (dev))
  547. devdbg (dev, "intr shutdown, code %d", status);
  548. return;
  549. /* NOTE: not throttling like RX/TX, since this endpoint
  550. * already polls infrequently
  551. */
  552. default:
  553. devdbg (dev, "intr status %d", status);
  554. break;
  555. }
  556. if (!netif_running (dev->net))
  557. return;
  558. memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
  559. status = usb_submit_urb (urb, GFP_ATOMIC);
  560. if (status != 0 && netif_msg_timer (dev))
  561. deverr(dev, "intr resubmit --> %d", status);
  562. }
  563. /*-------------------------------------------------------------------------*/
  564. // unlink pending rx/tx; completion handlers do all other cleanup
  565. static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
  566. {
  567. unsigned long flags;
  568. struct sk_buff *skb, *skbnext;
  569. int count = 0;
  570. spin_lock_irqsave (&q->lock, flags);
  571. for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
  572. struct skb_data *entry;
  573. struct urb *urb;
  574. int retval;
  575. entry = (struct skb_data *) skb->cb;
  576. urb = entry->urb;
  577. skbnext = skb->next;
  578. // during some PM-driven resume scenarios,
  579. // these (async) unlinks complete immediately
  580. retval = usb_unlink_urb (urb);
  581. if (retval != -EINPROGRESS && retval != 0)
  582. devdbg (dev, "unlink urb err, %d", retval);
  583. else
  584. count++;
  585. }
  586. spin_unlock_irqrestore (&q->lock, flags);
  587. return count;
  588. }
  589. /*-------------------------------------------------------------------------*/
  590. // precondition: never called in_interrupt
  591. static int usbnet_stop (struct net_device *net)
  592. {
  593. struct usbnet *dev = netdev_priv(net);
  594. int temp;
  595. DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup);
  596. DECLARE_WAITQUEUE (wait, current);
  597. netif_stop_queue (net);
  598. if (netif_msg_ifdown (dev))
  599. devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
  600. dev->stats.rx_packets, dev->stats.tx_packets,
  601. dev->stats.rx_errors, dev->stats.tx_errors
  602. );
  603. // ensure there are no more active urbs
  604. add_wait_queue (&unlink_wakeup, &wait);
  605. dev->wait = &unlink_wakeup;
  606. temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
  607. // maybe wait for deletions to finish.
  608. while (!skb_queue_empty(&dev->rxq) &&
  609. !skb_queue_empty(&dev->txq) &&
  610. !skb_queue_empty(&dev->done)) {
  611. msleep(UNLINK_TIMEOUT_MS);
  612. if (netif_msg_ifdown (dev))
  613. devdbg (dev, "waited for %d urb completions", temp);
  614. }
  615. dev->wait = NULL;
  616. remove_wait_queue (&unlink_wakeup, &wait);
  617. usb_kill_urb(dev->interrupt);
  618. /* deferred work (task, timer, softirq) must also stop.
  619. * can't flush_scheduled_work() until we drop rtnl (later),
  620. * else workers could deadlock; so make workers a NOP.
  621. */
  622. dev->flags = 0;
  623. del_timer_sync (&dev->delay);
  624. tasklet_kill (&dev->bh);
  625. return 0;
  626. }
  627. /*-------------------------------------------------------------------------*/
  628. // posts reads, and enables write queuing
  629. // precondition: never called in_interrupt
  630. static int usbnet_open (struct net_device *net)
  631. {
  632. struct usbnet *dev = netdev_priv(net);
  633. int retval = 0;
  634. struct driver_info *info = dev->driver_info;
  635. // put into "known safe" state
  636. if (info->reset && (retval = info->reset (dev)) < 0) {
  637. if (netif_msg_ifup (dev))
  638. devinfo (dev,
  639. "open reset fail (%d) usbnet usb-%s-%s, %s",
  640. retval,
  641. dev->udev->bus->bus_name, dev->udev->devpath,
  642. info->description);
  643. goto done;
  644. }
  645. // insist peer be connected
  646. if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
  647. if (netif_msg_ifup (dev))
  648. devdbg (dev, "can't open; %d", retval);
  649. goto done;
  650. }
  651. /* start any status interrupt transfer */
  652. if (dev->interrupt) {
  653. retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
  654. if (retval < 0) {
  655. if (netif_msg_ifup (dev))
  656. deverr (dev, "intr submit %d", retval);
  657. goto done;
  658. }
  659. }
  660. netif_start_queue (net);
  661. if (netif_msg_ifup (dev)) {
  662. char *framing;
  663. if (dev->driver_info->flags & FLAG_FRAMING_NC)
  664. framing = "NetChip";
  665. else if (dev->driver_info->flags & FLAG_FRAMING_GL)
  666. framing = "GeneSys";
  667. else if (dev->driver_info->flags & FLAG_FRAMING_Z)
  668. framing = "Zaurus";
  669. else if (dev->driver_info->flags & FLAG_FRAMING_RN)
  670. framing = "RNDIS";
  671. else if (dev->driver_info->flags & FLAG_FRAMING_AX)
  672. framing = "ASIX";
  673. else
  674. framing = "simple";
  675. devinfo (dev, "open: enable queueing "
  676. "(rx %d, tx %d) mtu %d %s framing",
  677. RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu,
  678. framing);
  679. }
  680. // delay posting reads until we're fully open
  681. tasklet_schedule (&dev->bh);
  682. done:
  683. return retval;
  684. }
  685. /*-------------------------------------------------------------------------*/
  686. /* ethtool methods; minidrivers may need to add some more, but
  687. * they'll probably want to use this base set.
  688. */
  689. void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
  690. {
  691. struct usbnet *dev = netdev_priv(net);
  692. /* REVISIT don't always return "usbnet" */
  693. strncpy (info->driver, driver_name, sizeof info->driver);
  694. strncpy (info->version, DRIVER_VERSION, sizeof info->version);
  695. strncpy (info->fw_version, dev->driver_info->description,
  696. sizeof info->fw_version);
  697. usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
  698. }
  699. EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
  700. static u32 usbnet_get_link (struct net_device *net)
  701. {
  702. struct usbnet *dev = netdev_priv(net);
  703. /* If a check_connect is defined, return its result */
  704. if (dev->driver_info->check_connect)
  705. return dev->driver_info->check_connect (dev) == 0;
  706. /* Otherwise, say we're up (to avoid breaking scripts) */
  707. return 1;
  708. }
  709. u32 usbnet_get_msglevel (struct net_device *net)
  710. {
  711. struct usbnet *dev = netdev_priv(net);
  712. return dev->msg_enable;
  713. }
  714. EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
  715. void usbnet_set_msglevel (struct net_device *net, u32 level)
  716. {
  717. struct usbnet *dev = netdev_priv(net);
  718. dev->msg_enable = level;
  719. }
  720. EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
  721. /* drivers may override default ethtool_ops in their bind() routine */
  722. static struct ethtool_ops usbnet_ethtool_ops = {
  723. .get_drvinfo = usbnet_get_drvinfo,
  724. .get_link = usbnet_get_link,
  725. .get_msglevel = usbnet_get_msglevel,
  726. .set_msglevel = usbnet_set_msglevel,
  727. };
  728. /*-------------------------------------------------------------------------*/
  729. /* work that cannot be done in interrupt context uses keventd.
  730. *
  731. * NOTE: with 2.5 we could do more of this using completion callbacks,
  732. * especially now that control transfers can be queued.
  733. */
  734. static void
  735. kevent (void *data)
  736. {
  737. struct usbnet *dev = data;
  738. int status;
  739. /* usb_clear_halt() needs a thread context */
  740. if (test_bit (EVENT_TX_HALT, &dev->flags)) {
  741. unlink_urbs (dev, &dev->txq);
  742. status = usb_clear_halt (dev->udev, dev->out);
  743. if (status < 0
  744. && status != -EPIPE
  745. && status != -ESHUTDOWN) {
  746. if (netif_msg_tx_err (dev))
  747. deverr (dev, "can't clear tx halt, status %d",
  748. status);
  749. } else {
  750. clear_bit (EVENT_TX_HALT, &dev->flags);
  751. if (status != -ESHUTDOWN)
  752. netif_wake_queue (dev->net);
  753. }
  754. }
  755. if (test_bit (EVENT_RX_HALT, &dev->flags)) {
  756. unlink_urbs (dev, &dev->rxq);
  757. status = usb_clear_halt (dev->udev, dev->in);
  758. if (status < 0
  759. && status != -EPIPE
  760. && status != -ESHUTDOWN) {
  761. if (netif_msg_rx_err (dev))
  762. deverr (dev, "can't clear rx halt, status %d",
  763. status);
  764. } else {
  765. clear_bit (EVENT_RX_HALT, &dev->flags);
  766. tasklet_schedule (&dev->bh);
  767. }
  768. }
  769. /* tasklet could resubmit itself forever if memory is tight */
  770. if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
  771. struct urb *urb = NULL;
  772. if (netif_running (dev->net))
  773. urb = usb_alloc_urb (0, GFP_KERNEL);
  774. else
  775. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  776. if (urb != NULL) {
  777. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  778. rx_submit (dev, urb, GFP_KERNEL);
  779. tasklet_schedule (&dev->bh);
  780. }
  781. }
  782. if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
  783. struct driver_info *info = dev->driver_info;
  784. int retval = 0;
  785. clear_bit (EVENT_LINK_RESET, &dev->flags);
  786. if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
  787. devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
  788. retval,
  789. dev->udev->bus->bus_name, dev->udev->devpath,
  790. info->description);
  791. }
  792. }
  793. if (dev->flags)
  794. devdbg (dev, "kevent done, flags = 0x%lx",
  795. dev->flags);
  796. }
  797. /*-------------------------------------------------------------------------*/
  798. static void tx_complete (struct urb *urb, struct pt_regs *regs)
  799. {
  800. struct sk_buff *skb = (struct sk_buff *) urb->context;
  801. struct skb_data *entry = (struct skb_data *) skb->cb;
  802. struct usbnet *dev = entry->dev;
  803. if (urb->status == 0) {
  804. dev->stats.tx_packets++;
  805. dev->stats.tx_bytes += entry->length;
  806. } else {
  807. dev->stats.tx_errors++;
  808. switch (urb->status) {
  809. case -EPIPE:
  810. usbnet_defer_kevent (dev, EVENT_TX_HALT);
  811. break;
  812. /* software-driven interface shutdown */
  813. case -ECONNRESET: // async unlink
  814. case -ESHUTDOWN: // hardware gone
  815. break;
  816. // like rx, tx gets controller i/o faults during khubd delays
  817. // and so it uses the same throttling mechanism.
  818. case -EPROTO: // ehci
  819. case -ETIMEDOUT: // ohci
  820. case -EILSEQ: // uhci
  821. if (!timer_pending (&dev->delay)) {
  822. mod_timer (&dev->delay,
  823. jiffies + THROTTLE_JIFFIES);
  824. if (netif_msg_link (dev))
  825. devdbg (dev, "tx throttle %d",
  826. urb->status);
  827. }
  828. netif_stop_queue (dev->net);
  829. break;
  830. default:
  831. if (netif_msg_tx_err (dev))
  832. devdbg (dev, "tx err %d", entry->urb->status);
  833. break;
  834. }
  835. }
  836. urb->dev = NULL;
  837. entry->state = tx_done;
  838. defer_bh(dev, skb, &dev->txq);
  839. }
  840. /*-------------------------------------------------------------------------*/
  841. static void usbnet_tx_timeout (struct net_device *net)
  842. {
  843. struct usbnet *dev = netdev_priv(net);
  844. unlink_urbs (dev, &dev->txq);
  845. tasklet_schedule (&dev->bh);
  846. // FIXME: device recovery -- reset?
  847. }
  848. /*-------------------------------------------------------------------------*/
  849. static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
  850. {
  851. struct usbnet *dev = netdev_priv(net);
  852. int length;
  853. int retval = NET_XMIT_SUCCESS;
  854. struct urb *urb = NULL;
  855. struct skb_data *entry;
  856. struct driver_info *info = dev->driver_info;
  857. unsigned long flags;
  858. // some devices want funky USB-level framing, for
  859. // win32 driver (usually) and/or hardware quirks
  860. if (info->tx_fixup) {
  861. skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
  862. if (!skb) {
  863. if (netif_msg_tx_err (dev))
  864. devdbg (dev, "can't tx_fixup skb");
  865. goto drop;
  866. }
  867. }
  868. length = skb->len;
  869. if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
  870. if (netif_msg_tx_err (dev))
  871. devdbg (dev, "no urb");
  872. goto drop;
  873. }
  874. entry = (struct skb_data *) skb->cb;
  875. entry->urb = urb;
  876. entry->dev = dev;
  877. entry->state = tx_start;
  878. entry->length = length;
  879. usb_fill_bulk_urb (urb, dev->udev, dev->out,
  880. skb->data, skb->len, tx_complete, skb);
  881. /* don't assume the hardware handles USB_ZERO_PACKET
  882. * NOTE: strictly conforming cdc-ether devices should expect
  883. * the ZLP here, but ignore the one-byte packet.
  884. *
  885. * FIXME zero that byte, if it doesn't require a new skb.
  886. */
  887. if ((length % dev->maxpacket) == 0)
  888. urb->transfer_buffer_length++;
  889. spin_lock_irqsave (&dev->txq.lock, flags);
  890. switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
  891. case -EPIPE:
  892. netif_stop_queue (net);
  893. usbnet_defer_kevent (dev, EVENT_TX_HALT);
  894. break;
  895. default:
  896. if (netif_msg_tx_err (dev))
  897. devdbg (dev, "tx: submit urb err %d", retval);
  898. break;
  899. case 0:
  900. net->trans_start = jiffies;
  901. __skb_queue_tail (&dev->txq, skb);
  902. if (dev->txq.qlen >= TX_QLEN (dev))
  903. netif_stop_queue (net);
  904. }
  905. spin_unlock_irqrestore (&dev->txq.lock, flags);
  906. if (retval) {
  907. if (netif_msg_tx_err (dev))
  908. devdbg (dev, "drop, code %d", retval);
  909. drop:
  910. retval = NET_XMIT_SUCCESS;
  911. dev->stats.tx_dropped++;
  912. if (skb)
  913. dev_kfree_skb_any (skb);
  914. usb_free_urb (urb);
  915. } else if (netif_msg_tx_queued (dev)) {
  916. devdbg (dev, "> tx, len %d, type 0x%x",
  917. length, skb->protocol);
  918. }
  919. return retval;
  920. }
  921. /*-------------------------------------------------------------------------*/
  922. // tasklet (work deferred from completions, in_irq) or timer
  923. static void usbnet_bh (unsigned long param)
  924. {
  925. struct usbnet *dev = (struct usbnet *) param;
  926. struct sk_buff *skb;
  927. struct skb_data *entry;
  928. while ((skb = skb_dequeue (&dev->done))) {
  929. entry = (struct skb_data *) skb->cb;
  930. switch (entry->state) {
  931. case rx_done:
  932. entry->state = rx_cleanup;
  933. rx_process (dev, skb);
  934. continue;
  935. case tx_done:
  936. case rx_cleanup:
  937. usb_free_urb (entry->urb);
  938. dev_kfree_skb (skb);
  939. continue;
  940. default:
  941. devdbg (dev, "bogus skb state %d", entry->state);
  942. }
  943. }
  944. // waiting for all pending urbs to complete?
  945. if (dev->wait) {
  946. if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
  947. wake_up (dev->wait);
  948. }
  949. // or are we maybe short a few urbs?
  950. } else if (netif_running (dev->net)
  951. && netif_device_present (dev->net)
  952. && !timer_pending (&dev->delay)
  953. && !test_bit (EVENT_RX_HALT, &dev->flags)) {
  954. int temp = dev->rxq.qlen;
  955. int qlen = RX_QLEN (dev);
  956. if (temp < qlen) {
  957. struct urb *urb;
  958. int i;
  959. // don't refill the queue all at once
  960. for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
  961. urb = usb_alloc_urb (0, GFP_ATOMIC);
  962. if (urb != NULL)
  963. rx_submit (dev, urb, GFP_ATOMIC);
  964. }
  965. if (temp != dev->rxq.qlen && netif_msg_link (dev))
  966. devdbg (dev, "rxqlen %d --> %d",
  967. temp, dev->rxq.qlen);
  968. if (dev->rxq.qlen < qlen)
  969. tasklet_schedule (&dev->bh);
  970. }
  971. if (dev->txq.qlen < TX_QLEN (dev))
  972. netif_wake_queue (dev->net);
  973. }
  974. }
  975. /*-------------------------------------------------------------------------
  976. *
  977. * USB Device Driver support
  978. *
  979. *-------------------------------------------------------------------------*/
  980. // precondition: never called in_interrupt
  981. void usbnet_disconnect (struct usb_interface *intf)
  982. {
  983. struct usbnet *dev;
  984. struct usb_device *xdev;
  985. struct net_device *net;
  986. dev = usb_get_intfdata(intf);
  987. usb_set_intfdata(intf, NULL);
  988. if (!dev)
  989. return;
  990. xdev = interface_to_usbdev (intf);
  991. if (netif_msg_probe (dev))
  992. devinfo (dev, "unregister '%s' usb-%s-%s, %s",
  993. intf->dev.driver->name,
  994. xdev->bus->bus_name, xdev->devpath,
  995. dev->driver_info->description);
  996. net = dev->net;
  997. unregister_netdev (net);
  998. /* we don't hold rtnl here ... */
  999. flush_scheduled_work ();
  1000. if (dev->driver_info->unbind)
  1001. dev->driver_info->unbind (dev, intf);
  1002. free_netdev(net);
  1003. usb_put_dev (xdev);
  1004. }
  1005. EXPORT_SYMBOL_GPL(usbnet_disconnect);
  1006. /*-------------------------------------------------------------------------*/
  1007. // precondition: never called in_interrupt
  1008. int
  1009. usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
  1010. {
  1011. struct usbnet *dev;
  1012. struct net_device *net;
  1013. struct usb_host_interface *interface;
  1014. struct driver_info *info;
  1015. struct usb_device *xdev;
  1016. int status;
  1017. info = (struct driver_info *) prod->driver_info;
  1018. if (!info) {
  1019. dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
  1020. return -ENODEV;
  1021. }
  1022. xdev = interface_to_usbdev (udev);
  1023. interface = udev->cur_altsetting;
  1024. usb_get_dev (xdev);
  1025. status = -ENOMEM;
  1026. // set up our own records
  1027. net = alloc_etherdev(sizeof(*dev));
  1028. if (!net) {
  1029. dbg ("can't kmalloc dev");
  1030. goto out;
  1031. }
  1032. dev = netdev_priv(net);
  1033. dev->udev = xdev;
  1034. dev->driver_info = info;
  1035. dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
  1036. | NETIF_MSG_PROBE | NETIF_MSG_LINK);
  1037. skb_queue_head_init (&dev->rxq);
  1038. skb_queue_head_init (&dev->txq);
  1039. skb_queue_head_init (&dev->done);
  1040. dev->bh.func = usbnet_bh;
  1041. dev->bh.data = (unsigned long) dev;
  1042. INIT_WORK (&dev->kevent, kevent, dev);
  1043. dev->delay.function = usbnet_bh;
  1044. dev->delay.data = (unsigned long) dev;
  1045. init_timer (&dev->delay);
  1046. SET_MODULE_OWNER (net);
  1047. dev->net = net;
  1048. strcpy (net->name, "usb%d");
  1049. memcpy (net->dev_addr, node_id, sizeof node_id);
  1050. /* rx and tx sides can use different message sizes;
  1051. * bind() should set rx_urb_size in that case.
  1052. */
  1053. dev->hard_mtu = net->mtu + net->hard_header_len;
  1054. #if 0
  1055. // dma_supported() is deeply broken on almost all architectures
  1056. // possible with some EHCI controllers
  1057. if (dma_supported (&udev->dev, DMA_64BIT_MASK))
  1058. net->features |= NETIF_F_HIGHDMA;
  1059. #endif
  1060. net->change_mtu = usbnet_change_mtu;
  1061. net->get_stats = usbnet_get_stats;
  1062. net->hard_start_xmit = usbnet_start_xmit;
  1063. net->open = usbnet_open;
  1064. net->stop = usbnet_stop;
  1065. net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
  1066. net->tx_timeout = usbnet_tx_timeout;
  1067. net->ethtool_ops = &usbnet_ethtool_ops;
  1068. // allow device-specific bind/init procedures
  1069. // NOTE net->name still not usable ...
  1070. if (info->bind) {
  1071. status = info->bind (dev, udev);
  1072. // heuristic: "usb%d" for links we know are two-host,
  1073. // else "eth%d" when there's reasonable doubt. userspace
  1074. // can rename the link if it knows better.
  1075. if ((dev->driver_info->flags & FLAG_ETHER) != 0
  1076. && (net->dev_addr [0] & 0x02) == 0)
  1077. strcpy (net->name, "eth%d");
  1078. /* maybe the remote can't receive an Ethernet MTU */
  1079. if (net->mtu > (dev->hard_mtu - net->hard_header_len))
  1080. net->mtu = dev->hard_mtu - net->hard_header_len;
  1081. } else if (!info->in || !info->out)
  1082. status = usbnet_get_endpoints (dev, udev);
  1083. else {
  1084. dev->in = usb_rcvbulkpipe (xdev, info->in);
  1085. dev->out = usb_sndbulkpipe (xdev, info->out);
  1086. if (!(info->flags & FLAG_NO_SETINT))
  1087. status = usb_set_interface (xdev,
  1088. interface->desc.bInterfaceNumber,
  1089. interface->desc.bAlternateSetting);
  1090. else
  1091. status = 0;
  1092. }
  1093. if (status == 0 && dev->status)
  1094. status = init_status (dev, udev);
  1095. if (status < 0)
  1096. goto out1;
  1097. if (!dev->rx_urb_size)
  1098. dev->rx_urb_size = dev->hard_mtu;
  1099. dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
  1100. SET_NETDEV_DEV(net, &udev->dev);
  1101. status = register_netdev (net);
  1102. if (status)
  1103. goto out3;
  1104. if (netif_msg_probe (dev))
  1105. devinfo (dev, "register '%s' at usb-%s-%s, %s, "
  1106. "%02x:%02x:%02x:%02x:%02x:%02x",
  1107. udev->dev.driver->name,
  1108. xdev->bus->bus_name, xdev->devpath,
  1109. dev->driver_info->description,
  1110. net->dev_addr [0], net->dev_addr [1],
  1111. net->dev_addr [2], net->dev_addr [3],
  1112. net->dev_addr [4], net->dev_addr [5]);
  1113. // ok, it's ready to go.
  1114. usb_set_intfdata (udev, dev);
  1115. // start as if the link is up
  1116. netif_device_attach (net);
  1117. return 0;
  1118. out3:
  1119. if (info->unbind)
  1120. info->unbind (dev, udev);
  1121. out1:
  1122. free_netdev(net);
  1123. out:
  1124. usb_put_dev(xdev);
  1125. return status;
  1126. }
  1127. EXPORT_SYMBOL_GPL(usbnet_probe);
  1128. /*-------------------------------------------------------------------------*/
  1129. /* FIXME these suspend/resume methods assume non-CDC style
  1130. * devices, with only one interface.
  1131. */
  1132. int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
  1133. {
  1134. struct usbnet *dev = usb_get_intfdata(intf);
  1135. /* accelerate emptying of the rx and queues, to avoid
  1136. * having everything error out.
  1137. */
  1138. netif_device_detach (dev->net);
  1139. (void) unlink_urbs (dev, &dev->rxq);
  1140. (void) unlink_urbs (dev, &dev->txq);
  1141. intf->dev.power.power_state = PMSG_SUSPEND;
  1142. return 0;
  1143. }
  1144. EXPORT_SYMBOL_GPL(usbnet_suspend);
  1145. int usbnet_resume (struct usb_interface *intf)
  1146. {
  1147. struct usbnet *dev = usb_get_intfdata(intf);
  1148. intf->dev.power.power_state = PMSG_ON;
  1149. netif_device_attach (dev->net);
  1150. tasklet_schedule (&dev->bh);
  1151. return 0;
  1152. }
  1153. EXPORT_SYMBOL_GPL(usbnet_resume);
  1154. /*-------------------------------------------------------------------------*/
  1155. #ifndef HAVE_HARDWARE
  1156. #error You need to configure some hardware for this driver
  1157. #endif
  1158. /*
  1159. * chip vendor names won't normally be on the cables, and
  1160. * may not be on the device.
  1161. */
  1162. static const struct usb_device_id products [] = {
  1163. #ifdef CONFIG_USB_PL2301
  1164. {
  1165. USB_DEVICE (0x067b, 0x0000), // PL-2301
  1166. .driver_info = (unsigned long) &prolific_info,
  1167. }, {
  1168. USB_DEVICE (0x067b, 0x0001), // PL-2302
  1169. .driver_info = (unsigned long) &prolific_info,
  1170. },
  1171. #endif
  1172. #ifdef CONFIG_USB_RNDIS
  1173. {
  1174. /* RNDIS is MSFT's un-official variant of CDC ACM */
  1175. USB_INTERFACE_INFO (USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
  1176. .driver_info = (unsigned long) &rndis_info,
  1177. },
  1178. #endif
  1179. { }, // END
  1180. };
  1181. MODULE_DEVICE_TABLE (usb, products);
  1182. static struct usb_driver usbnet_driver = {
  1183. .owner = THIS_MODULE,
  1184. .name = driver_name,
  1185. .id_table = products,
  1186. .probe = usbnet_probe,
  1187. .disconnect = usbnet_disconnect,
  1188. .suspend = usbnet_suspend,
  1189. .resume = usbnet_resume,
  1190. };
  1191. /*-------------------------------------------------------------------------*/
  1192. static int __init usbnet_init(void)
  1193. {
  1194. // compiler should optimize these out
  1195. BUG_ON (sizeof (((struct sk_buff *)0)->cb)
  1196. < sizeof (struct skb_data));
  1197. random_ether_addr(node_id);
  1198. return usb_register(&usbnet_driver);
  1199. }
  1200. module_init(usbnet_init);
  1201. static void __exit usbnet_exit(void)
  1202. {
  1203. usb_deregister(&usbnet_driver);
  1204. }
  1205. module_exit(usbnet_exit);
  1206. MODULE_AUTHOR("David Brownell");
  1207. MODULE_DESCRIPTION("USB Host-to-Host Link Drivers (numerous vendors)");
  1208. MODULE_LICENSE("GPL");