usbnet.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:57k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * USB Host-to-Host Links
  3.  * Copyright (C) 2000-2002 by David Brownell <dbrownell@users.sourceforge.net>
  4.  */
  5. /*
  6.  * This is used for "USB networking", connecting USB hosts as peers.
  7.  *
  8.  * It can be used with USB "network cables", for IP-over-USB communications;
  9.  * Ethernet speeds without the Ethernet.  USB devices (including some PDAs)
  10.  * can support such links directly, replacing device-specific protocols
  11.  * with Internet standard ones.
  12.  *
  13.  * The links can be bridged using the Ethernet bridging (net/bridge)
  14.  * support as appropriate.  Devices currently supported include:
  15.  *
  16.  * - AnchorChip 2720
  17.  * - Belkin, eTEK (interops with Win32 drivers)
  18.  * - EPSON USB clients
  19.  * - GeneSys GL620USB-A
  20.  * - "Linux Devices" (like iPaq and similar SA-1100 based PDAs)
  21.  * - NetChip 1080 (interoperates with NetChip Win32 drivers)
  22.  * - Prolific PL-2301/2302 (replaces "plusb" driver)
  23.  *
  24.  * USB devices can implement their side of this protocol at the cost
  25.  * of two bulk endpoints; it's not restricted to "cable" applications.
  26.  * See the LINUXDEV or EPSON device/client support.
  27.  *
  28.  * 
  29.  * Status:
  30.  *
  31.  * - AN2720 ... not widely available, but reportedly works well
  32.  *
  33.  * - Belkin/eTEK ... no known issues
  34.  *
  35.  * - Both GeneSys and PL-230x use interrupt transfers for driver-to-driver
  36.  *   handshaking; it'd be worth implementing those as "carrier detect".
  37.  *   Prefer generic hooks, not minidriver-specific hacks.
  38.  *
  39.  * - Linux devices ... the www.handhelds.org SA-1100 support works nicely,
  40.  *   but the Sharp Zaurus uses an incompatible protocol (extra checksums).
  41.  *   No reason not to merge the Zaurus protocol here too (got patch? :)
  42.  *
  43.  * - For Netchip, should use keventd to poll via control requests to detect
  44.  *   hardware level "carrier detect". 
  45.  *
  46.  * - PL-230x ... the initialization protocol doesn't seem to match chip data
  47.  *   sheets, sometimes it's not needed and sometimes it hangs.  Prolific has
  48.  *   not responded to repeated support/information requests.
  49.  *
  50.  * Interop with more Win32 drivers may be a good thing.
  51.  *
  52.  * Seems like reporting "peer connected" (carrier present) events may end
  53.  * up going through the netlink event system, not hotplug ... that may be
  54.  * awkward in terms of automatic configuration though.
  55.  *
  56.  * There are reports that bridging gives lower-than-usual throughput.
  57.  *
  58.  * Need smarter hotplug policy scripts ... ones that know how to arrange
  59.  * bridging with "brctl", and can handle static and dynamic ("pump") setups.
  60.  * Use those eventual "peer connected" events, and zeroconf.
  61.  *
  62.  *
  63.  * CHANGELOG:
  64.  *
  65.  * 13-sep-2000 experimental, new
  66.  * 10-oct-2000 usb_device_id table created. 
  67.  * 28-oct-2000 misc fixes; mostly, discard more TTL-mangled rx packets.
  68.  * 01-nov-2000 usb_device_id table and probing api update by
  69.  * Adam J. Richter <adam@yggdrasil.com>.
  70.  * 18-dec-2000 (db) tx watchdog, "net1080" renaming to "usbnet", device_info
  71.  * and prolific support, isolate net1080-specific bits, cleanup.
  72.  * fix unlink_urbs oops in D3 PM resume code path.
  73.  *
  74.  * 02-feb-2001 (db) fix tx skb sharing, packet length, match_flags, ...
  75.  * 08-feb-2001 stubbed in "linuxdev", maybe the SA-1100 folk can use it;
  76.  * AnchorChips 2720 support (from spec) for testing;
  77.  * fix bit-ordering problem with ethernet multicast addr
  78.  * 19-feb-2001  Support for clearing halt conditions. SA1100 UDC support
  79.  * updates. Oleg Drokin (green@iXcelerator.com)
  80.  * 25-mar-2001 More SA-1100 updates, including workaround for ip problem
  81.  * expecting cleared skb->cb and framing change to match latest
  82.  * handhelds.org version (Oleg).  Enable device IDs from the
  83.  * Win32 Belkin driver; other cleanups (db).
  84.  * 16-jul-2001 Bugfixes for uhci oops-on-unplug, Belkin support, various
  85.  * cleanups for problems not yet seen in the field. (db)
  86.  * 17-oct-2001 Handle "Advance USBNET" product, like Belkin/eTEK devices,
  87.  * from Ioannis Mavroukakis <i.mavroukakis@btinternet.com>;
  88.  * rx unlinks somehow weren't async; minor cleanup.
  89.  * 03-nov-2001 Merged GeneSys driver; original code from Jiun-Jie Huang
  90.  * <huangjj@genesyslogic.com.tw>, updated by Stanislav Brabec
  91.  * <utx@penguin.cz>.  Made framing options (NetChip/GeneSys)
  92.  * tie mostly to (sub)driver info.  Workaround some PL-2302
  93.  * chips that seem to reject SET_INTERFACE requests.
  94.  *
  95.  * 06-apr-2002 Added ethtool support, based on a patch from Brad Hards.
  96.  * Level of diagnostics is more configurable; they use device
  97.  * location (usb_device->devpath) instead of address (2.5).
  98.  * For tx_fixup, memflags can't be NOIO.
  99.  * 07-may-2002 Generalize/cleanup keventd support, handling rx stalls (mostly
  100.  * for USB 2.0 TTs) and memory shortages (potential) too. (db)
  101.  * Use "locally assigned" IEEE802 address space. (Brad Hards)
  102.  *
  103.  *-------------------------------------------------------------------------*/
  104. #include <linux/config.h>
  105. #include <linux/module.h>
  106. #include <linux/kmod.h>
  107. #include <linux/sched.h>
  108. #include <linux/init.h>
  109. #include <linux/netdevice.h>
  110. #include <linux/etherdevice.h>
  111. #include <linux/random.h>
  112. #include <linux/ethtool.h>
  113. #include <linux/tqueue.h>
  114. #include <asm/uaccess.h>
  115. #include <asm/unaligned.h>
  116. // #define DEBUG // error path messages, extra info
  117. // #define VERBOSE // more; success messages
  118. // #define REALLY_QUEUE
  119. #if !defined (DEBUG) && defined (CONFIG_USB_DEBUG)
  120. #   define DEBUG
  121. #endif
  122. #include <linux/usb.h>
  123. /* in 2.5 these standard usb ops take mem_flags */
  124. #define ALLOC_URB(n,flags) usb_alloc_urb(n)
  125. #define SUBMIT_URB(u,flags) usb_submit_urb(u)
  126. /* and these got renamed (may move to usb.h) */
  127. #define usb_get_dev usb_inc_dev_use
  128. #define usb_put_dev usb_dec_dev_use
  129. /* minidrivers _could_ be individually configured */
  130. #define CONFIG_USB_AN2720
  131. #define CONFIG_USB_BELKIN
  132. #define CONFIG_USB_EPSON2888
  133. #define CONFIG_USB_GENESYS
  134. #define CONFIG_USB_LINUXDEV
  135. #define CONFIG_USB_NET1080
  136. #define CONFIG_USB_PL2301
  137. #define DRIVER_VERSION "17-Jul-2002"
  138. /*-------------------------------------------------------------------------*/
  139. /*
  140.  * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
  141.  * Several dozen bytes of IPv4 data can fit in two such transactions.
  142.  * One maximum size Ethernet packet takes twenty four of them.
  143.  */
  144. #ifdef REALLY_QUEUE
  145. #define RX_QLEN 4
  146. #define TX_QLEN 4
  147. #else
  148. #define RX_QLEN 1
  149. #define TX_QLEN 1
  150. #endif
  151. // packets are always ethernet inside
  152. // ... except they can be bigger (limit of 64K with NetChip framing)
  153. #define MIN_PACKET sizeof(struct ethhdr)
  154. #define MAX_PACKET 32768
  155. // reawaken network queue this soon after stopping; else watchdog barks
  156. #define TX_TIMEOUT_JIFFIES (5*HZ)
  157. // for vendor-specific control operations
  158. #define CONTROL_TIMEOUT_MS (500) /* msec */
  159. #define CONTROL_TIMEOUT_JIFFIES ((CONTROL_TIMEOUT_MS * HZ)/1000)
  160. // between wakeups
  161. #define UNLINK_TIMEOUT_JIFFIES ((3  /*ms*/ * HZ)/1000)
  162. /*-------------------------------------------------------------------------*/
  163. // list of all devices we manage
  164. static DECLARE_MUTEX (usbnet_mutex);
  165. static LIST_HEAD (usbnet_list);
  166. // randomly generated ethernet address
  167. static u8 node_id [ETH_ALEN];
  168. // state we keep for each device we handle
  169. struct usbnet {
  170. // housekeeping
  171. struct usb_device *udev;
  172. struct driver_info *driver_info;
  173. struct semaphore mutex;
  174. struct list_head dev_list;
  175. wait_queue_head_t *wait;
  176. // protocol/interface state
  177. struct net_device net;
  178. struct net_device_stats stats;
  179. int msg_level;
  180. #ifdef CONFIG_USB_NET1080
  181. u16 packet_id;
  182. #endif
  183. // various kinds of pending driver work
  184. struct sk_buff_head rxq;
  185. struct sk_buff_head txq;
  186. struct sk_buff_head done;
  187. struct tasklet_struct bh;
  188. struct tq_struct kevent;
  189. unsigned long flags;
  190. # define EVENT_TX_HALT 0
  191. # define EVENT_RX_HALT 1
  192. # define EVENT_RX_MEMORY 2
  193. };
  194. // device-specific info used by the driver
  195. struct driver_info {
  196. char *description;
  197. int flags;
  198. #define FLAG_FRAMING_NC 0x0001 /* guard against device dropouts */ 
  199. #define FLAG_FRAMING_GL 0x0002 /* genelink batches packets */
  200. #define FLAG_NO_SETINT 0x0010 /* device can't set_interface() */
  201. /* reset device ... can sleep */
  202. int (*reset)(struct usbnet *);
  203. /* see if peer is connected ... can sleep */
  204. int (*check_connect)(struct usbnet *);
  205. /* fixup rx packet (strip framing) */
  206. int (*rx_fixup)(struct usbnet *dev, struct sk_buff *skb);
  207. /* fixup tx packet (add framing) */
  208. struct sk_buff *(*tx_fixup)(struct usbnet *dev,
  209. struct sk_buff *skb, int flags);
  210. // FIXME -- also an interrupt mechanism
  211. // useful for at least PL2301/2302 and GL620USB-A
  212. /* framework currently "knows" bulk EPs talk packets */
  213. int in; /* rx endpoint */
  214. int out; /* tx endpoint */
  215. int epsize;
  216. };
  217. #define EP_SIZE(usbnet) ((usbnet)->driver_info->epsize)
  218. // we record the state for each of our queued skbs
  219. enum skb_state {
  220. illegal = 0,
  221. tx_start, tx_done,
  222. rx_start, rx_done, rx_cleanup
  223. };
  224. struct skb_data { // skb->cb is one of these
  225. struct urb *urb;
  226. struct usbnet *dev;
  227. enum skb_state state;
  228. size_t length;
  229. };
  230. static const char driver_name [] = "usbnet";
  231. /* use ethtool to change the level for any given device */
  232. static int msg_level = 1;
  233. MODULE_PARM (msg_level, "i");
  234. MODULE_PARM_DESC (msg_level, "Initial message level (default = 1)");
  235. #define mutex_lock(x) down(x)
  236. #define mutex_unlock(x) up(x)
  237. #define RUN_CONTEXT (in_irq () ? "in_irq" 
  238. : (in_interrupt () ? "in_interrupt" : "can sleep"))
  239. /*-------------------------------------------------------------------------*/
  240. #ifdef DEBUG
  241. #define devdbg(usbnet, fmt, arg...) 
  242. printk(KERN_DEBUG "%s: " fmt "n" , (usbnet)->net.name, ## arg)
  243. #else
  244. #define devdbg(usbnet, fmt, arg...) do {} while(0)
  245. #endif
  246. #define devinfo(usbnet, fmt, arg...) 
  247. do { if ((usbnet)->msg_level >= 1) 
  248. printk(KERN_INFO "%s: " fmt "n" , (usbnet)->net.name, ## arg); 
  249. } while (0)
  250. #ifdef CONFIG_USB_AN2720
  251. /*-------------------------------------------------------------------------
  252.  *
  253.  * AnchorChips 2720 driver ... http://www.cypress.com
  254.  *
  255.  * This doesn't seem to have a way to detect whether the peer is
  256.  * connected, or need any reset handshaking.  It's got pretty big
  257.  * internal buffers (handles most of a frame's worth of data).
  258.  * Chip data sheets don't describe any vendor control messages.
  259.  *
  260.  *-------------------------------------------------------------------------*/
  261. static const struct driver_info an2720_info = {
  262. .description = "AnchorChips/Cypress 2720",
  263. // no reset available!
  264. // no check_connect available!
  265. .in = 2, .out = 2, // direction distinguishes these
  266. .epsize =64,
  267. };
  268. #endif /* CONFIG_USB_AN2720 */
  269. #ifdef CONFIG_USB_BELKIN
  270. /*-------------------------------------------------------------------------
  271.  *
  272.  * Belkin F5U104 ... two NetChip 2280 devices + Atmel microcontroller
  273.  *
  274.  * ... also two eTEK designs, including one sold as "Advance USBNET"
  275.  *
  276.  *-------------------------------------------------------------------------*/
  277. static const struct driver_info belkin_info = {
  278. .description = "Belkin, eTEK, or compatible",
  279. .in = 1, .out = 1, // direction distinguishes these
  280. .epsize =64,
  281. };
  282. #endif /* CONFIG_USB_BELKIN */
  283. #ifdef CONFIG_USB_EPSON2888
  284. /*-------------------------------------------------------------------------
  285.  *
  286.  * EPSON USB clients
  287.  *
  288.  * This is the same idea as "linuxdev" (below) except the firmware in the
  289.  * device might not be Tux-powered.  Epson provides reference firmware that
  290.  * implements this interface.  Product developers can reuse or modify that
  291.  * code, such as by using their own product and vendor codes.
  292.  *
  293.  *-------------------------------------------------------------------------*/
  294. static const struct driver_info epson2888_info = {
  295. .description = "Epson USB Device",
  296. .in = 4, .out = 3,
  297. .epsize = 64,
  298. };
  299. #endif /* CONFIG_USB_EPSON2888 */
  300. #ifdef CONFIG_USB_GENESYS
  301. /*-------------------------------------------------------------------------
  302.  *
  303.  * GeneSys GL620USB-A (www.genesyslogic.com.tw)
  304.  *
  305.  * ... should partially interop with the Win32 driver for this hardware
  306.  * The GeneSys docs imply there's some NDIS issue motivating this framing.
  307.  *
  308.  * Some info from GeneSys:
  309.  *  - GL620USB-A is full duplex; GL620USB is only half duplex for bulk.
  310.  *    (Some cables, like the BAFO-100c, use the half duplex version.)
  311.  *  - For the full duplex model, the low bit of the version code says
  312.  *    which side is which ("left/right").
  313.  *  - For the half duplex type, a control/interrupt handshake settles
  314.  *    the transfer direction.  (That's disabled here, partially coded.)
  315.  *    A control URB would block until other side writes an interrupt.
  316.  *
  317.  *-------------------------------------------------------------------------*/
  318. // control msg write command
  319. #define GENELINK_CONNECT_WRITE 0xF0
  320. // interrupt pipe index
  321. #define GENELINK_INTERRUPT_PIPE 0x03
  322. // interrupt read buffer size
  323. #define INTERRUPT_BUFSIZE 0x08
  324. // interrupt pipe interval value
  325. #define GENELINK_INTERRUPT_INTERVAL 0x10
  326. // max transmit packet number per transmit
  327. #define GL_MAX_TRANSMIT_PACKETS 32
  328. // max packet length
  329. #define GL_MAX_PACKET_LEN 1514
  330. // max receive buffer size 
  331. #define GL_RCV_BUF_SIZE
  332. (((GL_MAX_PACKET_LEN + 4) * GL_MAX_TRANSMIT_PACKETS) + 4)
  333. struct gl_packet {
  334. u32 packet_length;
  335. char packet_data [1];
  336. };
  337. struct gl_header {
  338. u32 packet_count;
  339. struct gl_packet packets;
  340. };
  341. #ifdef GENLINK_ACK
  342. // FIXME:  this code is incomplete, not debugged; it doesn't
  343. // handle interrupts correctly.  interrupts should be generic
  344. // code like all other device I/O, anyway.
  345. struct gl_priv { 
  346. struct urb *irq_urb;
  347. char irq_buf [INTERRUPT_BUFSIZE];
  348. };
  349. static inline int gl_control_write (struct usbnet *dev, u8 request, u16 value)
  350. {
  351. int retval;
  352. retval = usb_control_msg (dev->udev,
  353.       usb_sndctrlpipe (dev->udev, 0),
  354.       request,
  355.       USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  356.       value, 
  357.       0, // index
  358.       0, // data buffer
  359.       0, // size
  360.       CONTROL_TIMEOUT_JIFFIES);
  361. return retval;
  362. }
  363. static void gl_interrupt_complete (struct urb *urb)
  364. {
  365. int status = urb->status;
  366. if (status)
  367. dbg ("gl_interrupt_complete fail - %X", status);
  368. else
  369. dbg ("gl_interrupt_complete success...");
  370. }
  371. static int gl_interrupt_read (struct usbnet *dev)
  372. {
  373. struct gl_priv *priv = dev->priv_data;
  374. int retval;
  375. // issue usb interrupt read
  376. if (priv && priv->irq_urb) {
  377. // submit urb
  378. if ((retval = SUBMIT_URB (priv->irq_urb, GFP_KERNEL)) != 0)
  379. dbg ("gl_interrupt_read: submit fail - %X...", retval);
  380. else
  381. dbg ("gl_interrupt_read: submit success...");
  382. }
  383. return 0;
  384. }
  385. // check whether another side is connected
  386. static int genelink_check_connect (struct usbnet *dev)
  387. {
  388. int retval;
  389. dbg ("genelink_check_connect...");
  390. // detect whether another side is connected
  391. if ((retval = gl_control_write (dev, GENELINK_CONNECT_WRITE, 0)) != 0) {
  392. dbg ("%s: genelink_check_connect write fail - %X",
  393. dev->net.name, retval);
  394. return retval;
  395. }
  396. // usb interrupt read to ack another side 
  397. if ((retval = gl_interrupt_read (dev)) != 0) {
  398. dbg ("%s: genelink_check_connect read fail - %X",
  399. dev->net.name, retval);
  400. return retval;
  401. }
  402. dbg ("%s: genelink_check_connect read success", dev->net.name);
  403. return 0;
  404. }
  405. // allocate and initialize the private data for genelink
  406. static int genelink_init (struct usbnet *dev)
  407. {
  408. struct gl_priv *priv;
  409. // allocate the private data structure
  410. if ((priv = kmalloc (sizeof *priv, GFP_KERNEL)) == 0) {
  411. dbg ("%s: cannot allocate private data per device",
  412. dev->net.name);
  413. return -ENOMEM;
  414. }
  415. // allocate irq urb
  416. if ((priv->irq_urb = ALLOC_URB (0, GFP_KERNEL)) == 0) {
  417. dbg ("%s: cannot allocate private irq urb per device",
  418. dev->net.name);
  419. kfree (priv);
  420. return -ENOMEM;
  421. }
  422. // fill irq urb
  423. FILL_INT_URB (priv->irq_urb, dev->udev,
  424. usb_rcvintpipe (dev->udev, GENELINK_INTERRUPT_PIPE),
  425. priv->irq_buf, INTERRUPT_BUFSIZE,
  426. gl_interrupt_complete, 0,
  427. GENELINK_INTERRUPT_INTERVAL);
  428. // set private data pointer
  429. dev->priv_data = priv;
  430. return 0;
  431. }
  432. // release the private data
  433. static int genelink_free (struct usbnet *dev)
  434. {
  435. struct gl_priv *priv = dev->priv_data;
  436. if (!priv) 
  437. return 0;
  438. // FIXME:  can't cancel here; it's synchronous, and
  439. // should have happened earlier in any case (interrupt
  440. // handling needs to be generic)
  441. // cancel irq urb first
  442. usb_unlink_urb (priv->irq_urb);
  443. // free irq urb
  444. usb_free_urb (priv->irq_urb);
  445. // free the private data structure
  446. kfree (priv);
  447. return 0;
  448. }
  449. #endif
  450. static int genelink_rx_fixup (struct usbnet *dev, struct sk_buff *skb)
  451. {
  452. struct gl_header *header;
  453. struct gl_packet *packet;
  454. struct sk_buff *gl_skb;
  455. int status;
  456. u32 size;
  457. header = (struct gl_header *) skb->data;
  458. // get the packet count of the received skb
  459. le32_to_cpus (&header->packet_count);
  460. if ((header->packet_count > GL_MAX_TRANSMIT_PACKETS)
  461. || (header->packet_count < 0)) {
  462. dbg ("genelink: illegal received packet count %d",
  463. header->packet_count);
  464. return 0;
  465. }
  466. // set the current packet pointer to the first packet
  467. packet = &header->packets;
  468. // decrement the length for the packet count size 4 bytes
  469. skb_pull (skb, 4);
  470. while (header->packet_count > 1) {
  471. // get the packet length
  472. size = packet->packet_length;
  473. // this may be a broken packet
  474. if (size > GL_MAX_PACKET_LEN) {
  475. dbg ("genelink: illegal rx length %d", size);
  476. return 0;
  477. }
  478. // allocate the skb for the individual packet
  479. gl_skb = alloc_skb (size, GFP_ATOMIC);
  480. if (gl_skb) {
  481. // copy the packet data to the new skb
  482. memcpy (gl_skb->data, packet->packet_data, size);
  483. // set skb data size
  484. gl_skb->len = size;
  485. gl_skb->dev = &dev->net;
  486. // determine the packet's protocol ID
  487. gl_skb->protocol = eth_type_trans (gl_skb, &dev->net);
  488. // update the status
  489. dev->stats.rx_packets++;
  490. dev->stats.rx_bytes += size;
  491. // notify os of the received packet
  492. status = netif_rx (gl_skb);
  493. }
  494. // advance to the next packet
  495. packet = (struct gl_packet *)
  496. &packet->packet_data [size];
  497. header->packet_count--;
  498. // shift the data pointer to the next gl_packet
  499. skb_pull (skb, size + 4);
  500. }
  501. // skip the packet length field 4 bytes
  502. skb_pull (skb, 4);
  503. if (skb->len > GL_MAX_PACKET_LEN) {
  504. dbg ("genelink: illegal rx length %d", skb->len);
  505. return 0;
  506. }
  507. return 1;
  508. }
  509. static struct sk_buff *
  510. genelink_tx_fixup (struct usbnet *dev, struct sk_buff *skb, int flags)
  511. {
  512. int  padlen;
  513. int length = skb->len;
  514. int headroom = skb_headroom (skb);
  515. int tailroom = skb_tailroom (skb);
  516. u32 *packet_count;
  517. u32 *packet_len;
  518. // FIXME:  magic numbers, bleech
  519. padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
  520. if ((!skb_cloned (skb))
  521. && ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
  522. if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
  523. skb->data = memmove (skb->head + (4 + 4*1),
  524.      skb->data, skb->len);
  525. skb->tail = skb->data + skb->len;
  526. }
  527. } else {
  528. struct sk_buff *skb2;
  529. skb2 = skb_copy_expand (skb, (4 + 4*1) , padlen, flags);
  530. dev_kfree_skb_any (skb);
  531. skb = skb2;
  532. }
  533. // attach the packet count to the header
  534. packet_count = (u32 *) skb_push (skb, (4 + 4*1));
  535. packet_len = packet_count + 1;
  536. // FIXME little endian?
  537. *packet_count = 1;
  538. *packet_len = length;
  539. // add padding byte
  540. if ((skb->len % EP_SIZE (dev)) == 0)
  541. skb_put (skb, 1);
  542. return skb;
  543. }
  544. static const struct driver_info genelink_info = {
  545. .description = "Genesys GeneLink",
  546. .flags = FLAG_FRAMING_GL | FLAG_NO_SETINT,
  547. .rx_fixup = genelink_rx_fixup,
  548. .tx_fixup = genelink_tx_fixup,
  549. .in = 1, .out = 2,
  550. .epsize =64,
  551. #ifdef GENELINK_ACK
  552. .check_connect =genelink_check_connect,
  553. #endif
  554. };
  555. #endif /* CONFIG_USB_GENESYS */
  556. #ifdef CONFIG_USB_LINUXDEV
  557. /*-------------------------------------------------------------------------
  558.  *
  559.  * This could talk to a device that uses Linux, such as a PDA or
  560.  * an embedded system, or in fact to any "smart" device using this
  561.  * particular mapping of USB and Ethernet.
  562.  *
  563.  * Such a Linux host would need a "USB Device Controller" hardware
  564.  * (not "USB Host Controller"), and a network driver talking to that
  565.  * hardware.
  566.  *
  567.  * One example is Intel's SA-1100 chip, which integrates basic USB
  568.  * support (arch/arm/sa1100/usb-eth.c); it's used in the iPaq PDA.
  569.  * And others too, like the Yopy.
  570.  *
  571.  *-------------------------------------------------------------------------*/
  572. static const struct driver_info linuxdev_info = {
  573. .description = "Linux Device",
  574. .in = 2, .out = 1,
  575. .epsize = 64,
  576. };
  577. #endif /* CONFIG_USB_LINUXDEV */
  578. #ifdef CONFIG_USB_NET1080
  579. /*-------------------------------------------------------------------------
  580.  *
  581.  * Netchip 1080 driver ... http://www.netchip.com
  582.  * Used in LapLink cables
  583.  *
  584.  *-------------------------------------------------------------------------*/
  585. /*
  586.  * NetChip framing of ethernet packets, supporting additional error
  587.  * checks for links that may drop bulk packets from inside messages.
  588.  * Odd USB length == always short read for last usb packet.
  589.  * - nc_header
  590.  * - Ethernet header (14 bytes)
  591.  * - payload
  592.  * - (optional padding byte, if needed so length becomes odd)
  593.  * - nc_trailer
  594.  *
  595.  * This framing is to be avoided for non-NetChip devices.
  596.  */
  597. struct nc_header { // packed:
  598. u16 hdr_len; // sizeof nc_header (LE, all)
  599. u16 packet_len; // payload size (including ethhdr)
  600. u16 packet_id; // detects dropped packets
  601. #define MIN_HEADER 6
  602. // all else is optional, and must start with:
  603. // u16 vendorId; // from usb-if
  604. // u16 productId;
  605. } __attribute__((__packed__));
  606. #define PAD_BYTE ((unsigned char)0xAC)
  607. struct nc_trailer {
  608. u16 packet_id;
  609. } __attribute__((__packed__));
  610. // packets may use FLAG_FRAMING_NC and optional pad
  611. #define FRAMED_SIZE(mtu) (sizeof (struct nc_header) 
  612. + sizeof (struct ethhdr) 
  613. + (mtu) 
  614. + 1 
  615. + sizeof (struct nc_trailer))
  616. #define MIN_FRAMED FRAMED_SIZE(0)
  617. /*
  618.  * Zero means no timeout; else, how long a 64 byte bulk packet may be queued
  619.  * before the hardware drops it.  If that's done, the driver will need to
  620.  * frame network packets to guard against the dropped USB packets.  The win32
  621.  * driver sets this for both sides of the link.
  622.  */
  623. #define NC_READ_TTL_MS ((u8)255) // ms
  624. /*
  625.  * We ignore most registers and EEPROM contents.
  626.  */
  627. #define REG_USBCTL ((u8)0x04)
  628. #define REG_TTL ((u8)0x10)
  629. #define REG_STATUS ((u8)0x11)
  630. /*
  631.  * Vendor specific requests to read/write data
  632.  */
  633. #define REQUEST_REGISTER ((u8)0x10)
  634. #define REQUEST_EEPROM ((u8)0x11)
  635. static int
  636. nc_vendor_read (struct usbnet *dev, u8 req, u8 regnum, u16 *retval_ptr)
  637. {
  638. int status = usb_control_msg (dev->udev,
  639. usb_rcvctrlpipe (dev->udev, 0),
  640. req,
  641. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  642. 0, regnum,
  643. retval_ptr, sizeof *retval_ptr,
  644. CONTROL_TIMEOUT_JIFFIES);
  645. if (status > 0)
  646. status = 0;
  647. if (!status)
  648. le16_to_cpus (retval_ptr);
  649. return status;
  650. }
  651. static inline int
  652. nc_register_read (struct usbnet *dev, u8 regnum, u16 *retval_ptr)
  653. {
  654. return nc_vendor_read (dev, REQUEST_REGISTER, regnum, retval_ptr);
  655. }
  656. // no retval ... can become async, usable in_interrupt()
  657. static void
  658. nc_vendor_write (struct usbnet *dev, u8 req, u8 regnum, u16 value)
  659. {
  660. usb_control_msg (dev->udev,
  661. usb_sndctrlpipe (dev->udev, 0),
  662. req,
  663. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  664. value, regnum,
  665. 0, 0, // data is in setup packet
  666. CONTROL_TIMEOUT_JIFFIES);
  667. }
  668. static inline void
  669. nc_register_write (struct usbnet *dev, u8 regnum, u16 value)
  670. {
  671. nc_vendor_write (dev, REQUEST_REGISTER, regnum, value);
  672. }
  673. #if 0
  674. static void nc_dump_registers (struct usbnet *dev)
  675. {
  676. u8 reg;
  677. u16 *vp = kmalloc (sizeof (u16));
  678. if (!vp) {
  679. dbg ("no memory?");
  680. return;
  681. }
  682. dbg ("%s registers:", dev->net.name);
  683. for (reg = 0; reg < 0x20; reg++) {
  684. int retval;
  685. // reading some registers is trouble
  686. if (reg >= 0x08 && reg <= 0xf)
  687. continue;
  688. if (reg >= 0x12 && reg <= 0x1e)
  689. continue;
  690. retval = nc_register_read (dev, reg, vp);
  691. if (retval < 0)
  692. dbg ("%s reg [0x%x] ==> error %d",
  693. dev->net.name, reg, retval);
  694. else
  695. dbg ("%s reg [0x%x] = 0x%x",
  696. dev->net.name, reg, *vp);
  697. }
  698. kfree (vp);
  699. }
  700. #endif
  701. /*-------------------------------------------------------------------------*/
  702. /*
  703.  * Control register
  704.  */
  705. #define USBCTL_WRITABLE_MASK 0x1f0f
  706. // bits 15-13 reserved, r/o
  707. #define USBCTL_ENABLE_LANG (1 << 12)
  708. #define USBCTL_ENABLE_MFGR (1 << 11)
  709. #define USBCTL_ENABLE_PROD (1 << 10)
  710. #define USBCTL_ENABLE_SERIAL (1 << 9)
  711. #define USBCTL_ENABLE_DEFAULTS (1 << 8)
  712. // bits 7-4 reserved, r/o
  713. #define USBCTL_FLUSH_OTHER (1 << 3)
  714. #define USBCTL_FLUSH_THIS (1 << 2)
  715. #define USBCTL_DISCONN_OTHER (1 << 1)
  716. #define USBCTL_DISCONN_THIS (1 << 0)
  717. static inline void nc_dump_usbctl (struct usbnet *dev, u16 usbctl)
  718. {
  719. #ifdef DEBUG
  720. devdbg (dev, "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s;"
  721. " this%s%s;"
  722. " other%s%s; r/o 0x%x",
  723. dev->udev->bus->bus_name, dev->udev->devpath,
  724. usbctl,
  725. (usbctl & USBCTL_ENABLE_LANG) ? " lang" : "",
  726. (usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "",
  727. (usbctl & USBCTL_ENABLE_PROD) ? " prod" : "",
  728. (usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "",
  729. (usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "",
  730. (usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "",
  731. (usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "",
  732. (usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "",
  733. (usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "",
  734. usbctl & ~USBCTL_WRITABLE_MASK
  735. );
  736. #endif
  737. }
  738. /*-------------------------------------------------------------------------*/
  739. /*
  740.  * Status register
  741.  */
  742. #define STATUS_PORT_A (1 << 15)
  743. #define STATUS_CONN_OTHER (1 << 14)
  744. #define STATUS_SUSPEND_OTHER (1 << 13)
  745. #define STATUS_MAILBOX_OTHER (1 << 12)
  746. #define STATUS_PACKETS_OTHER(n) (((n) >> 8) && 0x03)
  747. #define STATUS_CONN_THIS (1 << 6)
  748. #define STATUS_SUSPEND_THIS (1 << 5)
  749. #define STATUS_MAILBOX_THIS (1 << 4)
  750. #define STATUS_PACKETS_THIS(n) (((n) >> 0) && 0x03)
  751. #define STATUS_UNSPEC_MASK 0x0c8c
  752. #define STATUS_NOISE_MASK  ((u16)~(0x0303|STATUS_UNSPEC_MASK))
  753. static inline void nc_dump_status (struct usbnet *dev, u16 status)
  754. {
  755. #ifdef DEBUG
  756. devdbg (dev, "net1080 %s-%s status 0x%x:"
  757. " this (%c) PKT=%d%s%s%s;"
  758. " other PKT=%d%s%s%s; unspec 0x%x",
  759. dev->udev->bus->bus_name, dev->udev->devpath,
  760. status,
  761. // XXX the packet counts don't seem right
  762. // (1 at reset, not 0); maybe UNSPEC too
  763. (status & STATUS_PORT_A) ? 'A' : 'B',
  764. STATUS_PACKETS_THIS (status),
  765. (status & STATUS_CONN_THIS) ? " CON" : "",
  766. (status & STATUS_SUSPEND_THIS) ? " SUS" : "",
  767. (status & STATUS_MAILBOX_THIS) ? " MBOX" : "",
  768. STATUS_PACKETS_OTHER (status),
  769. (status & STATUS_CONN_OTHER) ? " CON" : "",
  770. (status & STATUS_SUSPEND_OTHER) ? " SUS" : "",
  771. (status & STATUS_MAILBOX_OTHER) ? " MBOX" : "",
  772. status & STATUS_UNSPEC_MASK
  773. );
  774. #endif
  775. }
  776. /*-------------------------------------------------------------------------*/
  777. /*
  778.  * TTL register
  779.  */
  780. #define TTL_THIS(ttl) (0x00ff & ttl)
  781. #define TTL_OTHER(ttl) (0x00ff & (ttl >> 8))
  782. #define MK_TTL(this,other) ((u16)(((other)<<8)|(0x00ff&(this))))
  783. static inline void nc_dump_ttl (struct usbnet *dev, u16 ttl)
  784. {
  785. #ifdef DEBUG
  786. devdbg (dev, "net1080 %s-%s ttl 0x%x this = %d, other = %d",
  787. dev->udev->bus->bus_name, dev->udev->devpath,
  788. ttl,
  789. TTL_THIS (ttl),
  790. TTL_OTHER (ttl)
  791. );
  792. #endif
  793. }
  794. /*-------------------------------------------------------------------------*/
  795. static int net1080_reset (struct usbnet *dev)
  796. {
  797. u16 usbctl, status, ttl;
  798. u16 *vp = kmalloc (sizeof (u16), GFP_KERNEL);
  799. int retval;
  800. if (!vp)
  801. return -ENOMEM;
  802. // nc_dump_registers (dev);
  803. if ((retval = nc_register_read (dev, REG_STATUS, vp)) < 0) {
  804. dbg ("can't read %s-%s status: %d",
  805. dev->udev->bus->bus_name, dev->udev->devpath, retval);
  806. goto done;
  807. }
  808. status = *vp;
  809. // nc_dump_status (dev, status);
  810. if ((retval = nc_register_read (dev, REG_USBCTL, vp)) < 0) {
  811. dbg ("can't read USBCTL, %d", retval);
  812. goto done;
  813. }
  814. usbctl = *vp;
  815. // nc_dump_usbctl (dev, usbctl);
  816. nc_register_write (dev, REG_USBCTL,
  817. USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER);
  818. if ((retval = nc_register_read (dev, REG_TTL, vp)) < 0) {
  819. dbg ("can't read TTL, %d", retval);
  820. goto done;
  821. }
  822. ttl = *vp;
  823. // nc_dump_ttl (dev, ttl);
  824. nc_register_write (dev, REG_TTL,
  825. MK_TTL (NC_READ_TTL_MS, TTL_OTHER (ttl)) );
  826. dbg ("%s: assigned TTL, %d ms", dev->net.name, NC_READ_TTL_MS);
  827. if (dev->msg_level >= 2)
  828. devinfo (dev, "port %c, peer %sconnected",
  829. (status & STATUS_PORT_A) ? 'A' : 'B',
  830. (status & STATUS_CONN_OTHER) ? "" : "dis"
  831. );
  832. retval = 0;
  833. done:
  834. kfree (vp);
  835. return retval;
  836. }
  837. static int net1080_check_connect (struct usbnet *dev)
  838. {
  839. int retval;
  840. u16 status;
  841. u16 *vp = kmalloc (sizeof (u16), GFP_KERNEL);
  842. if (!vp)
  843. return -ENOMEM;
  844. retval = nc_register_read (dev, REG_STATUS, vp);
  845. status = *vp;
  846. kfree (vp);
  847. if (retval != 0) {
  848. dbg ("%s net1080_check_conn read - %d", dev->net.name, retval);
  849. return retval;
  850. }
  851. if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER)
  852. return -ENOLINK;
  853. return 0;
  854. }
  855. static int net1080_rx_fixup (struct usbnet *dev, struct sk_buff *skb)
  856. {
  857. struct nc_header *header;
  858. struct nc_trailer *trailer;
  859. if (!(skb->len & 0x01)
  860. || MIN_FRAMED > skb->len
  861. || skb->len > FRAMED_SIZE (dev->net.mtu)) {
  862. dev->stats.rx_frame_errors++;
  863. dbg ("rx framesize %d range %d..%d mtu %d", skb->len,
  864. (int)MIN_FRAMED, (int)FRAMED_SIZE (dev->net.mtu),
  865. dev->net.mtu);
  866. return 0;
  867. }
  868. header = (struct nc_header *) skb->data;
  869. le16_to_cpus (&header->hdr_len);
  870. le16_to_cpus (&header->packet_len);
  871. if (FRAMED_SIZE (header->packet_len) > MAX_PACKET) {
  872. dev->stats.rx_frame_errors++;
  873. dbg ("packet too big, %d", header->packet_len);
  874. return 0;
  875. } else if (header->hdr_len < MIN_HEADER) {
  876. dev->stats.rx_frame_errors++;
  877. dbg ("header too short, %d", header->hdr_len);
  878. return 0;
  879. } else if (header->hdr_len > MIN_HEADER) {
  880. // out of band data for us?
  881. dbg ("header OOB, %d bytes",
  882. header->hdr_len - MIN_HEADER);
  883. // switch (vendor/product ids) { ... }
  884. }
  885. skb_pull (skb, header->hdr_len);
  886. trailer = (struct nc_trailer *)
  887. (skb->data + skb->len - sizeof *trailer);
  888. skb_trim (skb, skb->len - sizeof *trailer);
  889. if ((header->packet_len & 0x01) == 0) {
  890. if (skb->data [header->packet_len] != PAD_BYTE) {
  891. dev->stats.rx_frame_errors++;
  892. dbg ("bad pad");
  893. return 0;
  894. }
  895. skb_trim (skb, skb->len - 1);
  896. }
  897. if (skb->len != header->packet_len) {
  898. dev->stats.rx_frame_errors++;
  899. dbg ("bad packet len %d (expected %d)",
  900. skb->len, header->packet_len);
  901. return 0;
  902. }
  903. if (header->packet_id != get_unaligned (&trailer->packet_id)) {
  904. dev->stats.rx_fifo_errors++;
  905. dbg ("(2+ dropped) rx packet_id mismatch 0x%x 0x%x",
  906. header->packet_id, trailer->packet_id);
  907. return 0;
  908. }
  909. #if 0
  910. devdbg (dev, "frame <rx h %d p %d id %d", header->hdr_len,
  911. header->packet_len, header->packet_id);
  912. #endif
  913. return 1;
  914. }
  915. static struct sk_buff *
  916. net1080_tx_fixup (struct usbnet *dev, struct sk_buff *skb, int flags)
  917. {
  918. int padlen;
  919. struct sk_buff *skb2;
  920. padlen = ((skb->len + sizeof (struct nc_header)
  921. + sizeof (struct nc_trailer)) & 0x01) ? 0 : 1;
  922. if (!skb_cloned (skb)) {
  923. int headroom = skb_headroom (skb);
  924. int tailroom = skb_tailroom (skb);
  925. if ((padlen + sizeof (struct nc_trailer)) <= tailroom
  926.     && sizeof (struct nc_header) <= headroom)
  927. return skb;
  928. if ((sizeof (struct nc_header) + padlen
  929. + sizeof (struct nc_trailer)) <
  930. (headroom + tailroom)) {
  931. skb->data = memmove (skb->head
  932. + sizeof (struct nc_header),
  933.     skb->data, skb->len);
  934. skb->tail = skb->data + skb->len;
  935. return skb;
  936. }
  937. }
  938. skb2 = skb_copy_expand (skb,
  939. sizeof (struct nc_header),
  940. sizeof (struct nc_trailer) + padlen,
  941. flags);
  942. dev_kfree_skb_any (skb);
  943. return skb2;
  944. }
  945. static const struct driver_info net1080_info = {
  946. .description = "NetChip TurboCONNECT",
  947. .flags = FLAG_FRAMING_NC,
  948. .reset = net1080_reset,
  949. .check_connect =net1080_check_connect,
  950. .rx_fixup = net1080_rx_fixup,
  951. .tx_fixup = net1080_tx_fixup,
  952. .in = 1, .out = 1, // direction distinguishes these
  953. .epsize =64,
  954. };
  955. #endif /* CONFIG_USB_NET1080 */
  956. #ifdef CONFIG_USB_PL2301
  957. /*-------------------------------------------------------------------------
  958.  *
  959.  * Prolific PL-2301/PL-2302 driver ... http://www.prolifictech.com
  960.  *
  961.  *-------------------------------------------------------------------------*/
  962. /*
  963.  * Bits 0-4 can be used for software handshaking; they're set from
  964.  * one end, cleared from the other, "read" with the interrupt byte.
  965.  */
  966. #define PL_S_EN (1<<7) /* (feature only) suspend enable */
  967. /* reserved bit -- rx ready (6) ? */
  968. #define PL_TX_READY (1<<5) /* (interrupt only) transmit ready */
  969. #define PL_RESET_OUT (1<<4) /* reset output pipe */
  970. #define PL_RESET_IN (1<<3) /* reset input pipe */
  971. #define PL_TX_C (1<<2) /* transmission complete */
  972. #define PL_TX_REQ (1<<1) /* transmission received */
  973. #define PL_PEER_E (1<<0) /* peer exists */
  974. static inline int
  975. pl_vendor_req (struct usbnet *dev, u8 req, u8 val, u8 index)
  976. {
  977. return usb_control_msg (dev->udev,
  978. usb_rcvctrlpipe (dev->udev, 0),
  979. req,
  980. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  981. val, index,
  982. 0, 0,
  983. CONTROL_TIMEOUT_JIFFIES);
  984. }
  985. static inline int
  986. pl_clear_QuickLink_features (struct usbnet *dev, int val)
  987. {
  988. return pl_vendor_req (dev, 1, (u8) val, 0);
  989. }
  990. static inline int
  991. pl_set_QuickLink_features (struct usbnet *dev, int val)
  992. {
  993. return pl_vendor_req (dev, 3, (u8) val, 0);
  994. }
  995. /*-------------------------------------------------------------------------*/
  996. static int pl_reset (struct usbnet *dev)
  997. {
  998. return pl_set_QuickLink_features (dev,
  999. PL_S_EN|PL_RESET_OUT|PL_RESET_IN|PL_PEER_E);
  1000. }
  1001. static const struct driver_info prolific_info = {
  1002. .description = "Prolific PL-2301/PL-2302",
  1003. .flags = FLAG_NO_SETINT,
  1004. /* some PL-2302 versions seem to fail usb_set_interface() */
  1005. .reset = pl_reset,
  1006. .in = 3, .out = 2,
  1007. .epsize =64,
  1008. };
  1009. #endif /* CONFIG_USB_PL2301 */
  1010. /*-------------------------------------------------------------------------
  1011.  *
  1012.  * Network Device Driver (peer link to "Host Device", from USB host)
  1013.  *
  1014.  *-------------------------------------------------------------------------*/
  1015. static int usbnet_change_mtu (struct net_device *net, int new_mtu)
  1016. {
  1017. struct usbnet *dev = (struct usbnet *) net->priv;
  1018. if (new_mtu <= MIN_PACKET || new_mtu > MAX_PACKET)
  1019. return -EINVAL;
  1020. #ifdef CONFIG_USB_NET1080
  1021. if (((dev->driver_info->flags) & FLAG_FRAMING_NC)) {
  1022. if (FRAMED_SIZE (new_mtu) > MAX_PACKET)
  1023. return -EINVAL;
  1024. }
  1025. #endif
  1026. #ifdef CONFIG_USB_GENESYS
  1027. if (((dev->driver_info->flags) & FLAG_FRAMING_GL)
  1028. && new_mtu > GL_MAX_PACKET_LEN)
  1029. return -EINVAL;
  1030. #endif
  1031. // no second zero-length packet read wanted after mtu-sized packets
  1032. if (((new_mtu + sizeof (struct ethhdr)) % EP_SIZE (dev)) == 0)
  1033. return -EDOM;
  1034. net->mtu = new_mtu;
  1035. return 0;
  1036. }
  1037. /*-------------------------------------------------------------------------*/
  1038. static struct net_device_stats *usbnet_get_stats (struct net_device *net)
  1039. {
  1040. return &((struct usbnet *) net->priv)->stats;
  1041. }
  1042. /*-------------------------------------------------------------------------*/
  1043. /* urb completions are currently in_irq; avoid doing real work then. */
  1044. static void defer_bh (struct usbnet *dev, struct sk_buff *skb)
  1045. {
  1046. struct sk_buff_head *list = skb->list;
  1047. unsigned long flags;
  1048. spin_lock_irqsave (&list->lock, flags);
  1049. __skb_unlink (skb, list);
  1050. spin_unlock (&list->lock);
  1051. spin_lock (&dev->done.lock);
  1052. __skb_queue_tail (&dev->done, skb);
  1053. if (dev->done.qlen == 1)
  1054. tasklet_schedule (&dev->bh);
  1055. spin_unlock_irqrestore (&dev->done.lock, flags);
  1056. }
  1057. /* some work can't be done in tasklets, so we use keventd
  1058.  *
  1059.  * NOTE:  annoying asymmetry:  if it's active, schedule_task() fails,
  1060.  * but tasklet_schedule() doesn't.  hope the failure is rare.
  1061.  */
  1062. static void defer_kevent (struct usbnet *dev, int work)
  1063. {
  1064. set_bit (work, &dev->flags);
  1065. if (!schedule_task (&dev->kevent))
  1066. err ("%s: kevent %d may have been dropped",
  1067. dev->net.name, work);
  1068. else
  1069. dbg ("%s: kevent %d scheduled", dev->net.name, work);
  1070. }
  1071. /*-------------------------------------------------------------------------*/
  1072. static void rx_complete (struct urb *urb);
  1073. static void rx_submit (struct usbnet *dev, struct urb *urb, int flags)
  1074. {
  1075. struct sk_buff *skb;
  1076. struct skb_data *entry;
  1077. int retval = 0;
  1078. unsigned long lockflags;
  1079. size_t size;
  1080. #ifdef CONFIG_USB_NET1080
  1081. if (dev->driver_info->flags & FLAG_FRAMING_NC)
  1082. size = FRAMED_SIZE (dev->net.mtu);
  1083. else
  1084. #endif
  1085. #ifdef CONFIG_USB_GENESYS
  1086. if (dev->driver_info->flags & FLAG_FRAMING_GL)
  1087. size = GL_RCV_BUF_SIZE;
  1088. else
  1089. #endif
  1090. size = (sizeof (struct ethhdr) + dev->net.mtu);
  1091. if ((skb = alloc_skb (size, flags)) == 0) {
  1092. dbg ("no rx skb");
  1093. defer_kevent (dev, EVENT_RX_MEMORY);
  1094. usb_free_urb (urb);
  1095. return;
  1096. }
  1097. entry = (struct skb_data *) skb->cb;
  1098. entry->urb = urb;
  1099. entry->dev = dev;
  1100. entry->state = rx_start;
  1101. entry->length = 0;
  1102. FILL_BULK_URB (urb, dev->udev,
  1103. usb_rcvbulkpipe (dev->udev, dev->driver_info->in),
  1104. skb->data, size, rx_complete, skb);
  1105. urb->transfer_flags |= USB_ASYNC_UNLINK;
  1106. #if 0
  1107. // Idle-but-posted reads with UHCI really chew up
  1108. // PCI bandwidth unless FSBR is disabled
  1109. urb->transfer_flags |= USB_NO_FSBR;
  1110. #endif
  1111. spin_lock_irqsave (&dev->rxq.lock, lockflags);
  1112. if (netif_running (&dev->net)
  1113. && !test_bit (EVENT_RX_HALT, &dev->flags)) {
  1114. switch (retval = SUBMIT_URB (urb, GFP_ATOMIC)){ 
  1115. case -EPIPE:
  1116. defer_kevent (dev, EVENT_RX_HALT);
  1117. break;
  1118. case -ENOMEM:
  1119. defer_kevent (dev, EVENT_RX_MEMORY);
  1120. break;
  1121. default:
  1122. dbg ("%s rx submit, %d", dev->net.name, retval);
  1123. tasklet_schedule (&dev->bh);
  1124. break;
  1125. case 0:
  1126. __skb_queue_tail (&dev->rxq, skb);
  1127. }
  1128. } else {
  1129. dbg ("rx: stopped");
  1130. retval = -ENOLINK;
  1131. }
  1132. spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
  1133. if (retval) {
  1134. dev_kfree_skb_any (skb);
  1135. usb_free_urb (urb);
  1136. }
  1137. }
  1138. /*-------------------------------------------------------------------------*/
  1139. static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
  1140. {
  1141. if (dev->driver_info->rx_fixup
  1142. && !dev->driver_info->rx_fixup (dev, skb))
  1143. goto error;
  1144. // else network stack removes extra byte if we forced a short packet
  1145. if (skb->len) {
  1146. int status;
  1147. // FIXME: eth_copy_and_csum "small" packets to new SKB (small < ~200 bytes) ?
  1148. skb->dev = &dev->net;
  1149. skb->protocol = eth_type_trans (skb, &dev->net);
  1150. dev->stats.rx_packets++;
  1151. dev->stats.rx_bytes += skb->len;
  1152. #ifdef VERBOSE
  1153. devdbg (dev, "< rx, len %d, type 0x%x",
  1154. skb->len + sizeof (struct ethhdr), skb->protocol);
  1155. #endif
  1156. memset (skb->cb, 0, sizeof (struct skb_data));
  1157. status = netif_rx (skb);
  1158. if (status != NET_RX_SUCCESS)
  1159. devdbg (dev, "netif_rx status %d", status);
  1160. } else {
  1161. dbg ("drop");
  1162. error:
  1163. dev->stats.rx_errors++;
  1164. skb_queue_tail (&dev->done, skb);
  1165. }
  1166. }
  1167. /*-------------------------------------------------------------------------*/
  1168. static void rx_complete (struct urb *urb)
  1169. {
  1170. struct sk_buff *skb = (struct sk_buff *) urb->context;
  1171. struct skb_data *entry = (struct skb_data *) skb->cb;
  1172. struct usbnet *dev = entry->dev;
  1173. int urb_status = urb->status;
  1174. skb_put (skb, urb->actual_length);
  1175. entry->state = rx_done;
  1176. entry->urb = 0;
  1177. switch (urb_status) {
  1178.     // success
  1179.     case 0:
  1180. if (MIN_PACKET > skb->len || skb->len > MAX_PACKET) {
  1181. entry->state = rx_cleanup;
  1182. dev->stats.rx_errors++;
  1183. dev->stats.rx_length_errors++;
  1184. dbg ("rx length %d", skb->len);
  1185. }
  1186. break;
  1187.     // stalls need manual reset. this is rare ... except that
  1188.     // when going through USB 2.0 TTs, unplug appears this way.
  1189.     // we avoid the highspeed version of the ETIMEOUT/EILSEQ
  1190.     // storm, recovering as needed.
  1191.     case -EPIPE:
  1192. defer_kevent (dev, EVENT_RX_HALT);
  1193. // FALLTHROUGH
  1194.     // software-driven interface shutdown
  1195.     case -ECONNRESET: // according to API spec
  1196.     case -ECONNABORTED: // some (now fixed?) UHCI bugs
  1197. dbg ("%s rx shutdown, code %d", dev->net.name, urb_status);
  1198. entry->state = rx_cleanup;
  1199. // do urb frees only in the tasklet (UHCI has oopsed ...)
  1200. entry->urb = urb;
  1201. urb = 0;
  1202. break;
  1203.     // data overrun ... flush fifo?
  1204.     case -EOVERFLOW:
  1205. dev->stats.rx_over_errors++;
  1206. // FALLTHROUGH
  1207.     
  1208.     default:
  1209. // on unplug we get ETIMEDOUT (ohci) or EILSEQ (uhci)
  1210. // until khubd sees its interrupt and disconnects us.
  1211. // that can easily be hundreds of passes through here.
  1212. entry->state = rx_cleanup;
  1213. dev->stats.rx_errors++;
  1214. dbg ("%s rx: status %d", dev->net.name, urb_status);
  1215. break;
  1216. }
  1217. defer_bh (dev, skb);
  1218. if (urb) {
  1219. if (netif_running (&dev->net)
  1220. && !test_bit (EVENT_RX_HALT, &dev->flags)) {
  1221. rx_submit (dev, urb, GFP_ATOMIC);
  1222. return;
  1223. }
  1224. usb_free_urb (urb);
  1225. }
  1226. #ifdef VERBOSE
  1227. dbg ("no read resubmitted");
  1228. #endif /* VERBOSE */
  1229. }
  1230. /*-------------------------------------------------------------------------*/
  1231. // unlink pending rx/tx; completion handlers do all other cleanup
  1232. static int unlink_urbs (struct sk_buff_head *q)
  1233. {
  1234. unsigned long flags;
  1235. struct sk_buff *skb, *skbnext;
  1236. int count = 0;
  1237. spin_lock_irqsave (&q->lock, flags);
  1238. for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
  1239. struct skb_data *entry;
  1240. struct urb *urb;
  1241. int retval;
  1242. entry = (struct skb_data *) skb->cb;
  1243. urb = entry->urb;
  1244. skbnext = skb->next;
  1245. // during some PM-driven resume scenarios,
  1246. // these (async) unlinks complete immediately
  1247. retval = usb_unlink_urb (urb);
  1248. if (retval != -EINPROGRESS && retval != 0)
  1249. dbg ("unlink urb err, %d", retval);
  1250. else
  1251. count++;
  1252. }
  1253. spin_unlock_irqrestore (&q->lock, flags);
  1254. return count;
  1255. }
  1256. /*-------------------------------------------------------------------------*/
  1257. // precondition: never called in_interrupt
  1258. static int usbnet_stop (struct net_device *net)
  1259. {
  1260. struct usbnet *dev = (struct usbnet *) net->priv;
  1261. int temp;
  1262. DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup); 
  1263. DECLARE_WAITQUEUE (wait, current);
  1264. mutex_lock (&dev->mutex);
  1265. netif_stop_queue (net);
  1266. if (dev->msg_level >= 2)
  1267. devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
  1268. dev->stats.rx_packets, dev->stats.tx_packets, 
  1269. dev->stats.rx_errors, dev->stats.tx_errors
  1270. );
  1271. // ensure there are no more active urbs
  1272. add_wait_queue (&unlink_wakeup, &wait);
  1273. dev->wait = &unlink_wakeup;
  1274. temp = unlink_urbs (&dev->txq) + unlink_urbs (&dev->rxq);
  1275. // maybe wait for deletions to finish.
  1276. while (skb_queue_len (&dev->rxq)
  1277. && skb_queue_len (&dev->txq)
  1278. && skb_queue_len (&dev->done)) {
  1279. set_current_state (TASK_UNINTERRUPTIBLE);
  1280. schedule_timeout (UNLINK_TIMEOUT_JIFFIES);
  1281. dbg ("waited for %d urb completions", temp);
  1282. }
  1283. dev->wait = 0;
  1284. remove_wait_queue (&unlink_wakeup, &wait); 
  1285. mutex_unlock (&dev->mutex);
  1286. return 0;
  1287. }
  1288. /*-------------------------------------------------------------------------*/
  1289. // posts reads, and enables write queing
  1290. // precondition: never called in_interrupt
  1291. static int usbnet_open (struct net_device *net)
  1292. {
  1293. struct usbnet *dev = (struct usbnet *) net->priv;
  1294. int retval = 0;
  1295. struct driver_info *info = dev->driver_info;
  1296. mutex_lock (&dev->mutex);
  1297. // put into "known safe" state
  1298. if (info->reset && (retval = info->reset (dev)) < 0) {
  1299. devinfo (dev, "open reset fail (%d) usbnet usb-%s-%s, %s",
  1300. retval,
  1301. dev->udev->bus->bus_name, dev->udev->devpath,
  1302. info->description);
  1303. goto done;
  1304. }
  1305. // insist peer be connected
  1306. if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
  1307. devdbg (dev, "can't open; %d", retval);
  1308. goto done;
  1309. }
  1310. netif_start_queue (net);
  1311. if (dev->msg_level >= 2)
  1312. devinfo (dev, "open: enable queueing "
  1313. "(rx %d, tx %d) mtu %d %s framing",
  1314. RX_QLEN, TX_QLEN, dev->net.mtu,
  1315. (info->flags & (FLAG_FRAMING_NC | FLAG_FRAMING_GL))
  1316.     ? ((info->flags & FLAG_FRAMING_NC)
  1317. ? "NetChip"
  1318. : "GeneSys")
  1319.     : "raw"
  1320. );
  1321. // delay posting reads until we're fully open
  1322. tasklet_schedule (&dev->bh);
  1323. done:
  1324. mutex_unlock (&dev->mutex);
  1325. return retval;
  1326. }
  1327. /*-------------------------------------------------------------------------*/
  1328. static int usbnet_ethtool_ioctl (struct net_device *net, void *useraddr)
  1329. {
  1330. struct usbnet *dev = (struct usbnet *) net->priv;
  1331. u32 cmd;
  1332. if (get_user (cmd, (u32 *)useraddr))
  1333. return -EFAULT;
  1334. switch (cmd) {
  1335. case ETHTOOL_GDRVINFO: { /* get driver info */
  1336. struct ethtool_drvinfo info;
  1337. memset (&info, 0, sizeof info);
  1338. info.cmd = ETHTOOL_GDRVINFO;
  1339. strncpy (info.driver, driver_name, sizeof info.driver);
  1340. strncpy (info.version, DRIVER_VERSION, sizeof info.version);
  1341. strncpy (info.fw_version, dev->driver_info->description,
  1342. sizeof info.fw_version);
  1343. usb_make_path (dev->udev, info.bus_info, sizeof info.bus_info);
  1344. if (copy_to_user (useraddr, &info, sizeof (info)))
  1345. return -EFAULT;
  1346. return 0;
  1347. }
  1348. case ETHTOOL_GLINK:  /* get link status */
  1349. if (dev->driver_info->check_connect) {
  1350. struct ethtool_value edata = { ETHTOOL_GLINK };
  1351. edata.data = dev->driver_info->check_connect (dev) == 0;
  1352. if (copy_to_user (useraddr, &edata, sizeof (edata)))
  1353. return -EFAULT;
  1354. return 0;
  1355. }
  1356. break;
  1357. case ETHTOOL_GMSGLVL: { /* get message-level */
  1358. struct ethtool_value edata = {ETHTOOL_GMSGLVL};
  1359. edata.data = dev->msg_level;
  1360. if (copy_to_user (useraddr, &edata, sizeof (edata)))
  1361. return -EFAULT;
  1362. return 0;
  1363. }
  1364. case ETHTOOL_SMSGLVL: { /* set message-level */
  1365. struct ethtool_value edata;
  1366. if (copy_from_user (&edata, useraddr, sizeof (edata)))
  1367. return -EFAULT;
  1368. dev->msg_level = edata.data;
  1369. return 0;
  1370. }
  1371. /* could also map RINGPARAM to RX/TX QLEN */
  1372. }
  1373.         /* Note that the ethtool user space code requires EOPNOTSUPP */
  1374. return -EOPNOTSUPP;
  1375. }
  1376. static int usbnet_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
  1377. {
  1378. switch (cmd) {
  1379. case SIOCETHTOOL:
  1380. return usbnet_ethtool_ioctl (net, (void *)rq->ifr_data);
  1381. default:
  1382. return -EOPNOTSUPP;
  1383. }
  1384. }
  1385. /*-------------------------------------------------------------------------*/
  1386. /* work that cannot be done in interrupt context uses keventd.
  1387.  *
  1388.  * NOTE:  "uhci" and "usb-uhci" may have trouble with this since they don't
  1389.  * queue control transfers to individual devices, and other threads could
  1390.  * trigger control requests concurrently.  hope that's rare.
  1391.  */
  1392. static void
  1393. kevent (void *data)
  1394. {
  1395. struct usbnet *dev = data;
  1396. int status;
  1397. /* usb_clear_halt() needs a thread context */
  1398. if (test_bit (EVENT_TX_HALT, &dev->flags)) {
  1399. unlink_urbs (&dev->txq);
  1400. status = usb_clear_halt (dev->udev,
  1401. usb_sndbulkpipe (dev->udev, dev->driver_info->out));
  1402. if (status < 0)
  1403. err ("%s: can't clear tx halt, status %d",
  1404. dev->net.name, status);
  1405. else {
  1406. clear_bit (EVENT_TX_HALT, &dev->flags);
  1407. netif_wake_queue (&dev->net);
  1408. }
  1409. }
  1410. if (test_bit (EVENT_RX_HALT, &dev->flags)) {
  1411. unlink_urbs (&dev->rxq);
  1412. status = usb_clear_halt (dev->udev,
  1413. usb_rcvbulkpipe (dev->udev, dev->driver_info->in));
  1414. if (status < 0)
  1415. err ("%s: can't clear rx halt, status %d",
  1416. dev->net.name, status);
  1417. else {
  1418. clear_bit (EVENT_RX_HALT, &dev->flags);
  1419. tasklet_schedule (&dev->bh);
  1420. }
  1421. }
  1422. /* tasklet could resubmit itself forever if memory is tight */
  1423. if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
  1424. struct urb *urb = 0;
  1425. if (netif_running (&dev->net))
  1426. urb = ALLOC_URB (0, GFP_KERNEL);
  1427. else
  1428. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  1429. if (urb != 0) {
  1430. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  1431. rx_submit (dev, urb, GFP_KERNEL);
  1432. tasklet_schedule (&dev->bh);
  1433. }
  1434. }
  1435. if (dev->flags)
  1436. dbg ("%s: kevent done, flags = 0x%lx",
  1437. dev->net.name, dev->flags);
  1438. }
  1439. /*-------------------------------------------------------------------------*/
  1440. static void tx_complete (struct urb *urb)
  1441. {
  1442. struct sk_buff *skb = (struct sk_buff *) urb->context;
  1443. struct skb_data *entry = (struct skb_data *) skb->cb;
  1444. struct usbnet *dev = entry->dev;
  1445. if (urb->status == -EPIPE)
  1446. defer_kevent (dev, EVENT_TX_HALT);
  1447. urb->dev = 0;
  1448. entry->state = tx_done;
  1449. defer_bh (dev, skb);
  1450. }
  1451. /*-------------------------------------------------------------------------*/
  1452. static void usbnet_tx_timeout (struct net_device *net)
  1453. {
  1454. struct usbnet *dev = (struct usbnet *) net->priv;
  1455. unlink_urbs (&dev->txq);
  1456. tasklet_schedule (&dev->bh);
  1457. // FIXME: device recovery -- reset?
  1458. }
  1459. /*-------------------------------------------------------------------------*/
  1460. static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
  1461. {
  1462. struct usbnet *dev = (struct usbnet *) net->priv;
  1463. int length = skb->len;
  1464. int retval = NET_XMIT_SUCCESS;
  1465. struct urb *urb = 0;
  1466. struct skb_data *entry;
  1467. struct driver_info *info = dev->driver_info;
  1468. unsigned long flags;
  1469. #ifdef CONFIG_USB_NET1080
  1470. struct nc_header *header = 0;
  1471. struct nc_trailer *trailer = 0;
  1472. #endif /* CONFIG_USB_NET1080 */
  1473. // some devices want funky USB-level framing, for
  1474. // win32 driver (usually) and/or hardware quirks
  1475. if (info->tx_fixup) {
  1476. skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
  1477. if (!skb) {
  1478. dbg ("can't tx_fixup skb");
  1479. goto drop;
  1480. }
  1481. }
  1482. if (!(urb = ALLOC_URB (0, GFP_ATOMIC))) {
  1483. dbg ("no urb");
  1484. goto drop;
  1485. }
  1486. entry = (struct skb_data *) skb->cb;
  1487. entry->urb = urb;
  1488. entry->dev = dev;
  1489. entry->state = tx_start;
  1490. entry->length = length;
  1491. // FIXME: reorganize a bit, so that fixup() fills out NetChip
  1492. // framing too. (Packet ID update needs the spinlock...)
  1493. #ifdef CONFIG_USB_NET1080
  1494. if (info->flags & FLAG_FRAMING_NC) {
  1495. header = (struct nc_header *) skb_push (skb, sizeof *header);
  1496. header->hdr_len = cpu_to_le16 (sizeof (*header));
  1497. header->packet_len = cpu_to_le16 (length);
  1498. if (!((skb->len + sizeof *trailer) & 0x01))
  1499. *skb_put (skb, 1) = PAD_BYTE;
  1500. trailer = (struct nc_trailer *) skb_put (skb, sizeof *trailer);
  1501. } else
  1502. #endif /* CONFIG_USB_NET1080 */
  1503. /* don't assume the hardware handles USB_ZERO_PACKET */
  1504. if ((length % EP_SIZE (dev)) == 0)
  1505. skb->len++;
  1506. FILL_BULK_URB (urb, dev->udev,
  1507. usb_sndbulkpipe (dev->udev, info->out),
  1508. skb->data, skb->len, tx_complete, skb);
  1509. urb->transfer_flags |= USB_ASYNC_UNLINK;
  1510. // FIXME urb->timeout = ... jiffies ... ;
  1511. spin_lock_irqsave (&dev->txq.lock, flags);
  1512. #ifdef CONFIG_USB_NET1080
  1513. if (info->flags & FLAG_FRAMING_NC) {
  1514. header->packet_id = cpu_to_le16 (dev->packet_id++);
  1515. put_unaligned (header->packet_id, &trailer->packet_id);
  1516. #if 0
  1517. devdbg (dev, "frame >tx h %d p %d id %d",
  1518. header->hdr_len, header->packet_len,
  1519. header->packet_id);
  1520. #endif
  1521. }
  1522. #endif /* CONFIG_USB_NET1080 */
  1523. switch ((retval = SUBMIT_URB (urb, GFP_ATOMIC))) {
  1524. case -EPIPE:
  1525. netif_stop_queue (net);
  1526. defer_kevent (dev, EVENT_TX_HALT);
  1527. break;
  1528. default:
  1529. dbg ("%s tx: submit urb err %d", net->name, retval);
  1530. break;
  1531. case 0:
  1532. net->trans_start = jiffies;
  1533. __skb_queue_tail (&dev->txq, skb);
  1534. if (dev->txq.qlen >= TX_QLEN)
  1535. netif_stop_queue (net);
  1536. }
  1537. spin_unlock_irqrestore (&dev->txq.lock, flags);
  1538. if (retval) {
  1539. devdbg (dev, "drop, code %d", retval);
  1540. drop:
  1541. retval = NET_XMIT_DROP;
  1542. dev->stats.tx_dropped++;
  1543. if (skb)
  1544. dev_kfree_skb_any (skb);
  1545. usb_free_urb (urb);
  1546. #ifdef VERBOSE
  1547. } else {
  1548. devdbg (dev, "> tx, len %d, type 0x%x",
  1549. length, skb->protocol);
  1550. #endif
  1551. }
  1552. return retval;
  1553. }
  1554. /*-------------------------------------------------------------------------*/
  1555. // tasklet ... work that avoided running in_irq()
  1556. static void usbnet_bh (unsigned long param)
  1557. {
  1558. struct usbnet *dev = (struct usbnet *) param;
  1559. struct sk_buff *skb;
  1560. struct skb_data *entry;
  1561. while ((skb = skb_dequeue (&dev->done))) {
  1562. entry = (struct skb_data *) skb->cb;
  1563. switch (entry->state) {
  1564.     case rx_done:
  1565. entry->state = rx_cleanup;
  1566. rx_process (dev, skb);
  1567. continue;
  1568.     case tx_done:
  1569. if (entry->urb->status) {
  1570. // can this statistic become more specific?
  1571. dev->stats.tx_errors++;
  1572. dbg ("%s tx: err %d", dev->net.name,
  1573. entry->urb->status);
  1574. } else {
  1575. dev->stats.tx_packets++;
  1576. dev->stats.tx_bytes += entry->length;
  1577. }
  1578. // FALLTHROUGH:
  1579.     case rx_cleanup:
  1580. usb_free_urb (entry->urb);
  1581. dev_kfree_skb (skb);
  1582. continue;
  1583.     default:
  1584. dbg ("%s: bogus skb state %d",
  1585. dev->net.name, entry->state);
  1586. }
  1587. }
  1588. // waiting for all pending urbs to complete?
  1589. if (dev->wait) {
  1590. if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
  1591. wake_up (dev->wait);
  1592. }
  1593. // or are we maybe short a few urbs?
  1594. } else if (netif_running (&dev->net)
  1595. && !test_bit (EVENT_RX_HALT, &dev->flags)) {
  1596. int temp = dev->rxq.qlen;
  1597. if (temp < RX_QLEN) {
  1598. struct urb *urb;
  1599. int i;
  1600. for (i = 0; i < 3 && dev->rxq.qlen < RX_QLEN; i++) {
  1601. if ((urb = ALLOC_URB (0, GFP_ATOMIC)) != 0)
  1602. rx_submit (dev, urb, GFP_ATOMIC);
  1603. }
  1604. if (temp != dev->rxq.qlen)
  1605. devdbg (dev, "rxqlen %d --> %d",
  1606. temp, dev->rxq.qlen);
  1607. if (dev->rxq.qlen < RX_QLEN)
  1608. tasklet_schedule (&dev->bh);
  1609. }
  1610. if (dev->txq.qlen < TX_QLEN)
  1611. netif_wake_queue (&dev->net);
  1612. }
  1613. }
  1614. /*-------------------------------------------------------------------------
  1615.  *
  1616.  * USB Device Driver support
  1617.  *
  1618.  *-------------------------------------------------------------------------*/
  1619.  
  1620. // precondition: never called in_interrupt
  1621. static void usbnet_disconnect (struct usb_device *udev, void *ptr)
  1622. {
  1623. struct usbnet *dev = (struct usbnet *) ptr;
  1624. devinfo (dev, "unregister usbnet usb-%s-%s, %s",
  1625. udev->bus->bus_name, udev->devpath,
  1626. dev->driver_info->description);
  1627. unregister_netdev (&dev->net);
  1628. mutex_lock (&usbnet_mutex);
  1629. mutex_lock (&dev->mutex);
  1630. list_del (&dev->dev_list);
  1631. mutex_unlock (&usbnet_mutex);
  1632. // assuming we used keventd, it must quiesce too
  1633. flush_scheduled_tasks ();
  1634. kfree (dev);
  1635. usb_put_dev (udev);
  1636. }
  1637. /*-------------------------------------------------------------------------*/
  1638. // precondition: never called in_interrupt
  1639. static void *
  1640. usbnet_probe (struct usb_device *udev, unsigned ifnum,
  1641. const struct usb_device_id *prod)
  1642. {
  1643. struct usbnet *dev;
  1644. struct net_device  *net;
  1645. struct usb_interface_descriptor *interface;
  1646. struct driver_info *info;
  1647. int altnum = 0;
  1648. info = (struct driver_info *) prod->driver_info;
  1649. // sanity check; expect dedicated interface/devices for now.
  1650. interface = &udev->actconfig->interface [ifnum].altsetting [altnum];
  1651. if (udev->descriptor.bNumConfigurations != 1
  1652. || udev->config[0].bNumInterfaces != 1
  1653. // || interface->bInterfaceClass != USB_CLASS_VENDOR_SPEC
  1654. ) {
  1655. dbg ("Bogus config info");
  1656. return 0;
  1657. }
  1658. // more sanity (unless the device is broken)
  1659. if (!(info->flags & FLAG_NO_SETINT)) {
  1660. if (usb_set_interface (udev, ifnum, altnum) < 0) {
  1661. err ("set_interface failed");
  1662. return 0;
  1663. }
  1664. }
  1665. // set up our own records
  1666. if (!(dev = kmalloc (sizeof *dev, GFP_KERNEL))) {
  1667. dbg ("can't kmalloc dev");
  1668. return 0;
  1669. }
  1670. memset (dev, 0, sizeof *dev);
  1671. init_MUTEX_LOCKED (&dev->mutex);
  1672. usb_get_dev (udev);
  1673. dev->udev = udev;
  1674. dev->driver_info = info;
  1675. dev->msg_level = msg_level;
  1676. INIT_LIST_HEAD (&dev->dev_list);
  1677. skb_queue_head_init (&dev->rxq);
  1678. skb_queue_head_init (&dev->txq);
  1679. skb_queue_head_init (&dev->done);
  1680. dev->bh.func = usbnet_bh;
  1681. dev->bh.data = (unsigned long) dev;
  1682. INIT_TQUEUE (&dev->kevent, kevent, dev);
  1683. // set up network interface records
  1684. net = &dev->net;
  1685. SET_MODULE_OWNER (net);
  1686. net->priv = dev;
  1687. strcpy (net->name, "usb%d");
  1688. memcpy (net->dev_addr, node_id, sizeof node_id);
  1689. // point-to-point link ... we always use Ethernet headers 
  1690. // supports win32 interop and the bridge driver.
  1691. ether_setup (net);
  1692. net->change_mtu = usbnet_change_mtu;
  1693. net->get_stats = usbnet_get_stats;
  1694. net->hard_start_xmit = usbnet_start_xmit;
  1695. net->open = usbnet_open;
  1696. net->stop = usbnet_stop;
  1697. net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
  1698. net->tx_timeout = usbnet_tx_timeout;
  1699. net->do_ioctl = usbnet_ioctl;
  1700. register_netdev (&dev->net);
  1701. devinfo (dev, "register usbnet usb-%s-%s, %s",
  1702. udev->bus->bus_name, udev->devpath,
  1703. dev->driver_info->description);
  1704. // ok, it's ready to go.
  1705. mutex_lock (&usbnet_mutex);
  1706. list_add (&dev->dev_list, &usbnet_list);
  1707. mutex_unlock (&dev->mutex);
  1708. // start as if the link is up
  1709. netif_device_attach (&dev->net);
  1710. mutex_unlock (&usbnet_mutex);
  1711. return dev;
  1712. }
  1713. /*-------------------------------------------------------------------------*/
  1714. /*
  1715.  * chip vendor names won't normally be on the cables, and
  1716.  * may not be on the device.
  1717.  */
  1718. static const struct usb_device_id products [] = {
  1719. #ifdef CONFIG_USB_AN2720
  1720. {
  1721. USB_DEVICE (0x0547, 0x2720), // AnchorChips defaults
  1722. .driver_info = (unsigned long) &an2720_info,
  1723. }, {
  1724. USB_DEVICE (0x0547, 0x2727), // Xircom PGUNET
  1725. .driver_info = (unsigned long) &an2720_info,
  1726. },
  1727. #endif
  1728. #ifdef CONFIG_USB_BELKIN
  1729. {
  1730. USB_DEVICE (0x050d, 0x0004), // Belkin
  1731. .driver_info = (unsigned long) &belkin_info,
  1732. }, {
  1733. USB_DEVICE (0x056c, 0x8100), // eTEK
  1734. .driver_info = (unsigned long) &belkin_info,
  1735. }, {
  1736. USB_DEVICE (0x0525, 0x9901), // Advance USBNET (eTEK)
  1737. .driver_info = (unsigned long) &belkin_info,
  1738. },
  1739. #endif
  1740. #ifdef CONFIG_USB_EPSON2888
  1741. {
  1742. USB_DEVICE (0x0525, 0x2888), // EPSON USB client
  1743. driver_info: (unsigned long) &epson2888_info,
  1744. },
  1745. #endif
  1746. #ifdef CONFIG_USB_GENESYS
  1747. {
  1748. USB_DEVICE (0x05e3, 0x0502), // GL620USB-A
  1749. .driver_info = (unsigned long) &genelink_info,
  1750. },
  1751. /* NOT: USB_DEVICE (0x05e3, 0x0501), // GL620USB
  1752.  * that's half duplex, not currently supported
  1753.  */
  1754. #endif
  1755. #ifdef CONFIG_USB_LINUXDEV
  1756. /*
  1757.  * for example, this can be a host side talk-to-PDA driver.
  1758.  * this driver is NOT what runs _inside_ a Linux device !!
  1759.  */
  1760. {
  1761. // 1183 = 0x049F, both used as hex values?
  1762. USB_DEVICE (0x049F, 0x505A), // Compaq "Itsy"
  1763. .driver_info = (unsigned long) &linuxdev_info,
  1764. }, {
  1765. USB_DEVICE (0x0E7E, 0x1001), // G.Mate "Yopy"
  1766. .driver_info = (unsigned long) &linuxdev_info,
  1767. },
  1768. // NOTE:  the Sharp Zaurus uses a modified version of
  1769. // this driver, which is not interoperable with this.
  1770. #endif
  1771. #ifdef CONFIG_USB_NET1080
  1772. {
  1773. USB_DEVICE (0x0525, 0x1080), // NetChip ref design
  1774. .driver_info = (unsigned long) &net1080_info,
  1775. }, {
  1776. USB_DEVICE (0x06D0, 0x0622), // Laplink Gold
  1777. .driver_info = (unsigned long) &net1080_info,
  1778. },
  1779. #endif
  1780. #ifdef CONFIG_USB_PL2301
  1781. {
  1782. USB_DEVICE (0x067b, 0x0000), // PL-2301
  1783. .driver_info = (unsigned long) &prolific_info,
  1784. }, {
  1785. USB_DEVICE (0x067b, 0x0001), // PL-2302
  1786. .driver_info = (unsigned long) &prolific_info,
  1787. },
  1788. #endif
  1789. /* KC2190 from www.sepoong.co.kr "InstaNET" */
  1790. { }, // END
  1791. };
  1792. MODULE_DEVICE_TABLE (usb, products);
  1793. static struct usb_driver usbnet_driver = {
  1794. .name = driver_name,
  1795. .id_table = products,
  1796. .probe = usbnet_probe,
  1797. .disconnect = usbnet_disconnect,
  1798. };
  1799. /*-------------------------------------------------------------------------*/
  1800. static int __init usbnet_init (void)
  1801. {
  1802. // compiler should optimize this out
  1803. if (sizeof (((struct sk_buff *)0)->cb) < sizeof (struct skb_data))
  1804. BUG ();
  1805. get_random_bytes (node_id, sizeof node_id);
  1806. node_id [0] &= 0xfe; // clear multicast bit
  1807. node_id [0] |= 0x02;    // set local assignment bit (IEEE802)
  1808.   if (usb_register (&usbnet_driver) < 0)
  1809.   return -1;
  1810. return 0;
  1811. }
  1812. module_init (usbnet_init);
  1813. static void __exit usbnet_exit (void)
  1814. {
  1815.   usb_deregister (&usbnet_driver);
  1816. }
  1817. module_exit (usbnet_exit);
  1818. EXPORT_NO_SYMBOLS;
  1819. MODULE_AUTHOR ("David Brownell <dbrownell@users.sourceforge.net>");
  1820. MODULE_DESCRIPTION ("USB Host-to-Host Link Drivers (numerous vendors)");
  1821. MODULE_LICENSE ("GPL");