Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * USB Networking Links |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 3 | * Copyright (C) 2000-2005 by David Brownell |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Copyright (C) 2002 Pavel Machek <pavel@ucw.cz> |
| 5 | * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com> |
| 6 | * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net> |
| 7 | * Copyright (c) 2002-2003 TiVo Inc. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * This is a generic "USB networking" framework that works with several |
| 26 | * kinds of full and high speed networking devices: |
| 27 | * |
| 28 | * + USB host-to-host "network cables", used for IP-over-USB links. |
| 29 | * These are often used for Laplink style connectivity products. |
| 30 | * - AnchorChip 2720 |
| 31 | * - Belkin, eTEK (interops with Win32 drivers) |
| 32 | * - GeneSys GL620USB-A |
| 33 | * - NetChip 1080 (interoperates with NetChip Win32 drivers) |
| 34 | * - Prolific PL-2301/2302 (replaces "plusb" driver) |
| 35 | * - KC Technology KC2190 |
| 36 | * |
| 37 | * + Smart USB devices can support such links directly, using Internet |
| 38 | * standard protocols instead of proprietary host-to-device links. |
| 39 | * - Linux PDAs like iPaq, Yopy, and Zaurus |
| 40 | * - The BLOB boot loader (for diskless booting) |
| 41 | * - Linux "gadgets", perhaps using PXA-2xx or Net2280 controllers |
| 42 | * - Devices using EPSON's sample USB firmware |
| 43 | * - CDC-Ethernet class devices, such as many cable modems |
| 44 | * |
| 45 | * + Adapters to networks such as Ethernet. |
| 46 | * - AX8817X based USB 2.0 products |
| 47 | * |
| 48 | * Links to these devices can be bridged using Linux Ethernet bridging. |
| 49 | * With minor exceptions, these all use similar USB framing for network |
| 50 | * traffic, but need different protocols for control traffic. |
| 51 | * |
| 52 | * USB devices can implement their side of this protocol at the cost |
| 53 | * of two bulk endpoints; it's not restricted to "cable" applications. |
| 54 | * See the SA1110, Zaurus, or EPSON device/client support in this driver; |
| 55 | * slave/target drivers such as "usb-eth" (on most SA-1100 PDAs) or |
| 56 | * "g_ether" (in the Linux "gadget" framework) implement that behavior |
| 57 | * within devices. |
| 58 | * |
| 59 | * |
| 60 | * CHANGELOG: |
| 61 | * |
| 62 | * 13-sep-2000 experimental, new |
| 63 | * 10-oct-2000 usb_device_id table created. |
| 64 | * 28-oct-2000 misc fixes; mostly, discard more TTL-mangled rx packets. |
| 65 | * 01-nov-2000 usb_device_id table and probing api update by |
| 66 | * Adam J. Richter <adam@yggdrasil.com>. |
| 67 | * 18-dec-2000 (db) tx watchdog, "net1080" renaming to "usbnet", device_info |
| 68 | * and prolific support, isolate net1080-specific bits, cleanup. |
| 69 | * fix unlink_urbs oops in D3 PM resume code path. |
| 70 | * |
| 71 | * 02-feb-2001 (db) fix tx skb sharing, packet length, match_flags, ... |
| 72 | * 08-feb-2001 stubbed in "linuxdev", maybe the SA-1100 folk can use it; |
| 73 | * AnchorChips 2720 support (from spec) for testing; |
| 74 | * fix bit-ordering problem with ethernet multicast addr |
| 75 | * 19-feb-2001 Support for clearing halt conditions. SA1100 UDC support |
| 76 | * updates. Oleg Drokin (green@iXcelerator.com) |
| 77 | * 25-mar-2001 More SA-1100 updates, including workaround for ip problem |
| 78 | * expecting cleared skb->cb and framing change to match latest |
| 79 | * handhelds.org version (Oleg). Enable device IDs from the |
| 80 | * Win32 Belkin driver; other cleanups (db). |
| 81 | * 16-jul-2001 Bugfixes for uhci oops-on-unplug, Belkin support, various |
| 82 | * cleanups for problems not yet seen in the field. (db) |
| 83 | * 17-oct-2001 Handle "Advance USBNET" product, like Belkin/eTEK devices, |
| 84 | * from Ioannis Mavroukakis <i.mavroukakis@btinternet.com>; |
| 85 | * rx unlinks somehow weren't async; minor cleanup. |
| 86 | * 03-nov-2001 Merged GeneSys driver; original code from Jiun-Jie Huang |
| 87 | * <huangjj@genesyslogic.com.tw>, updated by Stanislav Brabec |
| 88 | * <utx@penguin.cz>. Made framing options (NetChip/GeneSys) |
| 89 | * tie mostly to (sub)driver info. Workaround some PL-2302 |
| 90 | * chips that seem to reject SET_INTERFACE requests. |
| 91 | * |
| 92 | * 06-apr-2002 Added ethtool support, based on a patch from Brad Hards. |
| 93 | * Level of diagnostics is more configurable; they use device |
| 94 | * location (usb_device->devpath) instead of address (2.5). |
| 95 | * For tx_fixup, memflags can't be NOIO. |
| 96 | * 07-may-2002 Generalize/cleanup keventd support, handling rx stalls (mostly |
| 97 | * for USB 2.0 TTs) and memory shortages (potential) too. (db) |
| 98 | * Use "locally assigned" IEEE802 address space. (Brad Hards) |
| 99 | * 18-oct-2002 Support for Zaurus (Pavel Machek), related cleanup (db). |
| 100 | * 14-dec-2002 Remove Zaurus-private crc32 code (Pavel); 2.5 oops fix, |
| 101 | * cleanups and stubbed PXA-250 support (db), fix for framing |
| 102 | * issues on Z, net1080, and gl620a (Toby Milne) |
| 103 | * |
| 104 | * 31-mar-2003 Use endpoint descriptors: high speed support, simpler sa1100 |
| 105 | * vs pxa25x, and CDC Ethernet. Throttle down log floods on |
| 106 | * disconnect; other cleanups. (db) Flush net1080 fifos |
| 107 | * after several sequential framing errors. (Johannes Erdfelt) |
| 108 | * 22-aug-2003 AX8817X support (Dave Hollis). |
| 109 | * 14-jun-2004 Trivial patch for AX8817X based Buffalo LUA-U2-KTX in Japan |
| 110 | * (Neil Bortnak) |
| 111 | * 03-nov-2004 Trivial patch for KC2190 (KC-190) chip. (Jonathan McDowell) |
| 112 | * |
| 113 | * 01-feb-2005 AX88772 support (Phil Chang & Dave Hollis) |
| 114 | *-------------------------------------------------------------------------*/ |
| 115 | |
| 116 | // #define DEBUG // error path messages, extra info |
| 117 | // #define VERBOSE // more; success messages |
| 118 | |
| 119 | #include <linux/config.h> |
| 120 | #ifdef CONFIG_USB_DEBUG |
| 121 | # define DEBUG |
| 122 | #endif |
| 123 | #include <linux/module.h> |
| 124 | #include <linux/kmod.h> |
| 125 | #include <linux/sched.h> |
| 126 | #include <linux/init.h> |
| 127 | #include <linux/netdevice.h> |
| 128 | #include <linux/etherdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | #include <linux/ethtool.h> |
| 130 | #include <linux/workqueue.h> |
| 131 | #include <linux/mii.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | #include <linux/usb.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | #include <linux/mm.h> |
| 134 | #include <linux/dma-mapping.h> |
| 135 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 136 | #include <asm/unaligned.h> |
| 137 | |
| 138 | #include "usbnet.h" |
| 139 | |
| 140 | #define DRIVER_VERSION "22-Aug-2005" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | |
| 143 | /*-------------------------------------------------------------------------*/ |
| 144 | |
| 145 | /* |
| 146 | * Nineteen USB 1.1 max size bulk transactions per frame (ms), max. |
| 147 | * Several dozen bytes of IPv4 data can fit in two such transactions. |
| 148 | * One maximum size Ethernet packet takes twenty four of them. |
| 149 | * For high speed, each frame comfortably fits almost 36 max size |
| 150 | * Ethernet packets (so queues should be bigger). |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 151 | * |
| 152 | * REVISIT qlens should be members of 'struct usbnet'; the goal is to |
| 153 | * let the USB host controller be busy for 5msec or more before an irq |
| 154 | * is required, under load. Jumbograms change the equation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | */ |
| 156 | #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4) |
| 157 | #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4) |
| 158 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 159 | /* packets are always ethernet, sometimes wrapped in other framing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | #define MIN_PACKET sizeof(struct ethhdr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
| 162 | // reawaken network queue this soon after stopping; else watchdog barks |
| 163 | #define TX_TIMEOUT_JIFFIES (5*HZ) |
| 164 | |
| 165 | // throttle rx/tx briefly after some faults, so khubd might disconnect() |
| 166 | // us (it polls at HZ/4 usually) before we report too many false errors. |
| 167 | #define THROTTLE_JIFFIES (HZ/8) |
| 168 | |
| 169 | // for vendor-specific control operations |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 170 | #define CONTROL_TIMEOUT_MS USB_CTRL_GET_TIMEOUT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
| 172 | // between wakeups |
| 173 | #define UNLINK_TIMEOUT_MS 3 |
| 174 | |
| 175 | /*-------------------------------------------------------------------------*/ |
| 176 | |
| 177 | // randomly generated ethernet address |
| 178 | static u8 node_id [ETH_ALEN]; |
| 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | static const char driver_name [] = "usbnet"; |
| 181 | |
| 182 | /* use ethtool to change the level for any given device */ |
| 183 | static int msg_level = -1; |
| 184 | module_param (msg_level, int, 0); |
| 185 | MODULE_PARM_DESC (msg_level, "Override default message level"); |
| 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | /*-------------------------------------------------------------------------*/ |
| 188 | |
| 189 | static void usbnet_get_drvinfo (struct net_device *, struct ethtool_drvinfo *); |
| 190 | static u32 usbnet_get_link (struct net_device *); |
| 191 | static u32 usbnet_get_msglevel (struct net_device *); |
| 192 | static void usbnet_set_msglevel (struct net_device *, u32); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 193 | static void defer_kevent (struct usbnet *, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
| 195 | /* mostly for PDA style devices, which are always connected if present */ |
| 196 | static int always_connected (struct usbnet *dev) |
| 197 | { |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | /* handles CDC Ethernet and many other network "bulk data" interfaces */ |
| 202 | static int |
| 203 | get_endpoints (struct usbnet *dev, struct usb_interface *intf) |
| 204 | { |
| 205 | int tmp; |
| 206 | struct usb_host_interface *alt = NULL; |
| 207 | struct usb_host_endpoint *in = NULL, *out = NULL; |
| 208 | struct usb_host_endpoint *status = NULL; |
| 209 | |
| 210 | for (tmp = 0; tmp < intf->num_altsetting; tmp++) { |
| 211 | unsigned ep; |
| 212 | |
| 213 | in = out = status = NULL; |
| 214 | alt = intf->altsetting + tmp; |
| 215 | |
| 216 | /* take the first altsetting with in-bulk + out-bulk; |
| 217 | * remember any status endpoint, just in case; |
| 218 | * ignore other endpoints and altsetttings. |
| 219 | */ |
| 220 | for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) { |
| 221 | struct usb_host_endpoint *e; |
| 222 | int intr = 0; |
| 223 | |
| 224 | e = alt->endpoint + ep; |
| 225 | switch (e->desc.bmAttributes) { |
| 226 | case USB_ENDPOINT_XFER_INT: |
| 227 | if (!(e->desc.bEndpointAddress & USB_DIR_IN)) |
| 228 | continue; |
| 229 | intr = 1; |
| 230 | /* FALLTHROUGH */ |
| 231 | case USB_ENDPOINT_XFER_BULK: |
| 232 | break; |
| 233 | default: |
| 234 | continue; |
| 235 | } |
| 236 | if (e->desc.bEndpointAddress & USB_DIR_IN) { |
| 237 | if (!intr && !in) |
| 238 | in = e; |
| 239 | else if (intr && !status) |
| 240 | status = e; |
| 241 | } else { |
| 242 | if (!out) |
| 243 | out = e; |
| 244 | } |
| 245 | } |
| 246 | if (in && out) |
| 247 | break; |
| 248 | } |
| 249 | if (!alt || !in || !out) |
| 250 | return -EINVAL; |
| 251 | |
| 252 | if (alt->desc.bAlternateSetting != 0 |
| 253 | || !(dev->driver_info->flags & FLAG_NO_SETINT)) { |
| 254 | tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber, |
| 255 | alt->desc.bAlternateSetting); |
| 256 | if (tmp < 0) |
| 257 | return tmp; |
| 258 | } |
| 259 | |
| 260 | dev->in = usb_rcvbulkpipe (dev->udev, |
| 261 | in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); |
| 262 | dev->out = usb_sndbulkpipe (dev->udev, |
| 263 | out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); |
| 264 | dev->status = status; |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static void intr_complete (struct urb *urb, struct pt_regs *regs); |
| 269 | |
| 270 | static int init_status (struct usbnet *dev, struct usb_interface *intf) |
| 271 | { |
| 272 | char *buf = NULL; |
| 273 | unsigned pipe = 0; |
| 274 | unsigned maxp; |
| 275 | unsigned period; |
| 276 | |
| 277 | if (!dev->driver_info->status) |
| 278 | return 0; |
| 279 | |
| 280 | pipe = usb_rcvintpipe (dev->udev, |
| 281 | dev->status->desc.bEndpointAddress |
| 282 | & USB_ENDPOINT_NUMBER_MASK); |
| 283 | maxp = usb_maxpacket (dev->udev, pipe, 0); |
| 284 | |
| 285 | /* avoid 1 msec chatter: min 8 msec poll rate */ |
| 286 | period = max ((int) dev->status->desc.bInterval, |
| 287 | (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3); |
| 288 | |
| 289 | buf = kmalloc (maxp, SLAB_KERNEL); |
| 290 | if (buf) { |
| 291 | dev->interrupt = usb_alloc_urb (0, SLAB_KERNEL); |
| 292 | if (!dev->interrupt) { |
| 293 | kfree (buf); |
| 294 | return -ENOMEM; |
| 295 | } else { |
| 296 | usb_fill_int_urb(dev->interrupt, dev->udev, pipe, |
| 297 | buf, maxp, intr_complete, dev, period); |
| 298 | dev_dbg(&intf->dev, |
| 299 | "status ep%din, %d bytes period %d\n", |
| 300 | usb_pipeendpoint(pipe), maxp, period); |
| 301 | } |
| 302 | } |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static void skb_return (struct usbnet *dev, struct sk_buff *skb) |
| 307 | { |
| 308 | int status; |
| 309 | |
| 310 | skb->dev = dev->net; |
| 311 | skb->protocol = eth_type_trans (skb, dev->net); |
| 312 | dev->stats.rx_packets++; |
| 313 | dev->stats.rx_bytes += skb->len; |
| 314 | |
| 315 | if (netif_msg_rx_status (dev)) |
Al Viro | 5330e92 | 2005-04-26 11:26:53 -0700 | [diff] [blame] | 316 | devdbg (dev, "< rx, len %zu, type 0x%x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | skb->len + sizeof (struct ethhdr), skb->protocol); |
| 318 | memset (skb->cb, 0, sizeof (struct skb_data)); |
| 319 | status = netif_rx (skb); |
| 320 | if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev)) |
| 321 | devdbg (dev, "netif_rx status %d", status); |
| 322 | } |
| 323 | |
| 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | #ifdef CONFIG_USB_AX8817X |
| 326 | /* ASIX AX8817X based USB 2.0 Ethernet Devices */ |
| 327 | |
| 328 | #define HAVE_HARDWARE |
| 329 | #define NEED_MII |
| 330 | |
| 331 | #include <linux/crc32.h> |
| 332 | |
| 333 | #define AX_CMD_SET_SW_MII 0x06 |
| 334 | #define AX_CMD_READ_MII_REG 0x07 |
| 335 | #define AX_CMD_WRITE_MII_REG 0x08 |
| 336 | #define AX_CMD_SET_HW_MII 0x0a |
| 337 | #define AX_CMD_READ_EEPROM 0x0b |
| 338 | #define AX_CMD_WRITE_EEPROM 0x0c |
| 339 | #define AX_CMD_WRITE_ENABLE 0x0d |
| 340 | #define AX_CMD_WRITE_DISABLE 0x0e |
| 341 | #define AX_CMD_WRITE_RX_CTL 0x10 |
| 342 | #define AX_CMD_READ_IPG012 0x11 |
| 343 | #define AX_CMD_WRITE_IPG0 0x12 |
| 344 | #define AX_CMD_WRITE_IPG1 0x13 |
| 345 | #define AX_CMD_WRITE_IPG2 0x14 |
| 346 | #define AX_CMD_WRITE_MULTI_FILTER 0x16 |
| 347 | #define AX_CMD_READ_NODE_ID 0x17 |
| 348 | #define AX_CMD_READ_PHY_ID 0x19 |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 349 | #define AX_CMD_READ_MEDIUM_STATUS 0x1a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | #define AX_CMD_WRITE_MEDIUM_MODE 0x1b |
| 351 | #define AX_CMD_READ_MONITOR_MODE 0x1c |
| 352 | #define AX_CMD_WRITE_MONITOR_MODE 0x1d |
| 353 | #define AX_CMD_WRITE_GPIOS 0x1f |
| 354 | #define AX_CMD_SW_RESET 0x20 |
| 355 | #define AX_CMD_SW_PHY_STATUS 0x21 |
| 356 | #define AX_CMD_SW_PHY_SELECT 0x22 |
| 357 | #define AX88772_CMD_READ_NODE_ID 0x13 |
| 358 | |
| 359 | #define AX_MONITOR_MODE 0x01 |
| 360 | #define AX_MONITOR_LINK 0x02 |
| 361 | #define AX_MONITOR_MAGIC 0x04 |
| 362 | #define AX_MONITOR_HSFS 0x10 |
| 363 | |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 364 | /* AX88172 Medium Status Register values */ |
| 365 | #define AX_MEDIUM_FULL_DUPLEX 0x02 |
| 366 | #define AX_MEDIUM_TX_ABORT_ALLOW 0x04 |
| 367 | #define AX_MEDIUM_FLOW_CONTROL_EN 0x10 |
| 368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | #define AX_MCAST_FILTER_SIZE 8 |
| 370 | #define AX_MAX_MCAST 64 |
| 371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | #define AX_EEPROM_LEN 0x40 |
| 373 | |
| 374 | #define AX_SWRESET_CLEAR 0x00 |
| 375 | #define AX_SWRESET_RR 0x01 |
| 376 | #define AX_SWRESET_RT 0x02 |
| 377 | #define AX_SWRESET_PRTE 0x04 |
| 378 | #define AX_SWRESET_PRL 0x08 |
| 379 | #define AX_SWRESET_BZ 0x10 |
| 380 | #define AX_SWRESET_IPRL 0x20 |
| 381 | #define AX_SWRESET_IPPD 0x40 |
| 382 | |
| 383 | #define AX88772_IPG0_DEFAULT 0x15 |
| 384 | #define AX88772_IPG1_DEFAULT 0x0c |
| 385 | #define AX88772_IPG2_DEFAULT 0x12 |
| 386 | |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 387 | #define AX88772_MEDIUM_FULL_DUPLEX 0x0002 |
| 388 | #define AX88772_MEDIUM_RESERVED 0x0004 |
| 389 | #define AX88772_MEDIUM_RX_FC_ENABLE 0x0010 |
| 390 | #define AX88772_MEDIUM_TX_FC_ENABLE 0x0020 |
| 391 | #define AX88772_MEDIUM_PAUSE_FORMAT 0x0080 |
| 392 | #define AX88772_MEDIUM_RX_ENABLE 0x0100 |
| 393 | #define AX88772_MEDIUM_100MB 0x0200 |
| 394 | #define AX88772_MEDIUM_DEFAULT \ |
| 395 | (AX88772_MEDIUM_FULL_DUPLEX | AX88772_MEDIUM_RX_FC_ENABLE | \ |
| 396 | AX88772_MEDIUM_TX_FC_ENABLE | AX88772_MEDIUM_100MB | \ |
| 397 | AX88772_MEDIUM_RESERVED | AX88772_MEDIUM_RX_ENABLE ) |
| 398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | #define AX_EEPROM_MAGIC 0xdeadbeef |
| 400 | |
| 401 | /* This structure cannot exceed sizeof(unsigned long [5]) AKA 20 bytes */ |
| 402 | struct ax8817x_data { |
| 403 | u8 multi_filter[AX_MCAST_FILTER_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | }; |
| 405 | |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 406 | struct ax88172_int_data { |
| 407 | u16 res1; |
| 408 | u8 link; |
| 409 | u16 res2; |
| 410 | u8 status; |
| 411 | u16 res3; |
| 412 | } __attribute__ ((packed)); |
| 413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | static int ax8817x_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index, |
| 415 | u16 size, void *data) |
| 416 | { |
| 417 | return usb_control_msg( |
| 418 | dev->udev, |
| 419 | usb_rcvctrlpipe(dev->udev, 0), |
| 420 | cmd, |
| 421 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 422 | value, |
| 423 | index, |
| 424 | data, |
| 425 | size, |
| 426 | CONTROL_TIMEOUT_MS); |
| 427 | } |
| 428 | |
| 429 | static int ax8817x_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index, |
| 430 | u16 size, void *data) |
| 431 | { |
| 432 | return usb_control_msg( |
| 433 | dev->udev, |
| 434 | usb_sndctrlpipe(dev->udev, 0), |
| 435 | cmd, |
| 436 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 437 | value, |
| 438 | index, |
| 439 | data, |
| 440 | size, |
| 441 | CONTROL_TIMEOUT_MS); |
| 442 | } |
| 443 | |
| 444 | static void ax8817x_async_cmd_callback(struct urb *urb, struct pt_regs *regs) |
| 445 | { |
| 446 | struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context; |
| 447 | |
| 448 | if (urb->status < 0) |
| 449 | printk(KERN_DEBUG "ax8817x_async_cmd_callback() failed with %d", |
| 450 | urb->status); |
| 451 | |
| 452 | kfree(req); |
| 453 | usb_free_urb(urb); |
| 454 | } |
| 455 | |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 456 | static void ax8817x_status(struct usbnet *dev, struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | { |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 458 | struct ax88172_int_data *event; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | int link; |
| 460 | |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 461 | if (urb->actual_length < 8) |
| 462 | return; |
| 463 | |
| 464 | event = urb->transfer_buffer; |
| 465 | link = event->link & 0x01; |
| 466 | if (netif_carrier_ok(dev->net) != link) { |
| 467 | if (link) { |
| 468 | netif_carrier_on(dev->net); |
| 469 | defer_kevent (dev, EVENT_LINK_RESET ); |
| 470 | } else |
| 471 | netif_carrier_off(dev->net); |
| 472 | devdbg(dev, "ax8817x - Link Status is: %d", link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
| 476 | static void ax8817x_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index, |
| 477 | u16 size, void *data) |
| 478 | { |
| 479 | struct usb_ctrlrequest *req; |
| 480 | int status; |
| 481 | struct urb *urb; |
| 482 | |
| 483 | if ((urb = usb_alloc_urb(0, GFP_ATOMIC)) == NULL) { |
| 484 | devdbg(dev, "Error allocating URB in write_cmd_async!"); |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | if ((req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC)) == NULL) { |
| 489 | deverr(dev, "Failed to allocate memory for control request"); |
| 490 | usb_free_urb(urb); |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE; |
| 495 | req->bRequest = cmd; |
| 496 | req->wValue = cpu_to_le16(value); |
| 497 | req->wIndex = cpu_to_le16(index); |
| 498 | req->wLength = cpu_to_le16(size); |
| 499 | |
| 500 | usb_fill_control_urb(urb, dev->udev, |
| 501 | usb_sndctrlpipe(dev->udev, 0), |
| 502 | (void *)req, data, size, |
| 503 | ax8817x_async_cmd_callback, req); |
| 504 | |
| 505 | if((status = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { |
| 506 | deverr(dev, "Error submitting the control message: status=%d", status); |
| 507 | kfree(req); |
| 508 | usb_free_urb(urb); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | static void ax8817x_set_multicast(struct net_device *net) |
| 513 | { |
| 514 | struct usbnet *dev = netdev_priv(net); |
| 515 | struct ax8817x_data *data = (struct ax8817x_data *)&dev->data; |
| 516 | u8 rx_ctl = 0x8c; |
| 517 | |
| 518 | if (net->flags & IFF_PROMISC) { |
| 519 | rx_ctl |= 0x01; |
| 520 | } else if (net->flags & IFF_ALLMULTI |
| 521 | || net->mc_count > AX_MAX_MCAST) { |
| 522 | rx_ctl |= 0x02; |
| 523 | } else if (net->mc_count == 0) { |
| 524 | /* just broadcast and directed */ |
| 525 | } else { |
| 526 | /* We use the 20 byte dev->data |
| 527 | * for our 8 byte filter buffer |
| 528 | * to avoid allocating memory that |
| 529 | * is tricky to free later */ |
| 530 | struct dev_mc_list *mc_list = net->mc_list; |
| 531 | u32 crc_bits; |
| 532 | int i; |
| 533 | |
| 534 | memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE); |
| 535 | |
| 536 | /* Build the multicast hash filter. */ |
| 537 | for (i = 0; i < net->mc_count; i++) { |
| 538 | crc_bits = |
| 539 | ether_crc(ETH_ALEN, |
| 540 | mc_list->dmi_addr) >> 26; |
| 541 | data->multi_filter[crc_bits >> 3] |= |
| 542 | 1 << (crc_bits & 7); |
| 543 | mc_list = mc_list->next; |
| 544 | } |
| 545 | |
| 546 | ax8817x_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0, |
| 547 | AX_MCAST_FILTER_SIZE, data->multi_filter); |
| 548 | |
| 549 | rx_ctl |= 0x10; |
| 550 | } |
| 551 | |
| 552 | ax8817x_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL); |
| 553 | } |
| 554 | |
| 555 | static int ax8817x_mdio_read(struct net_device *netdev, int phy_id, int loc) |
| 556 | { |
| 557 | struct usbnet *dev = netdev_priv(netdev); |
| 558 | u16 res; |
| 559 | u8 buf[1]; |
| 560 | |
| 561 | ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII, 0, 0, 0, &buf); |
| 562 | ax8817x_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, (u16 *)&res); |
| 563 | ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, &buf); |
| 564 | |
| 565 | return res & 0xffff; |
| 566 | } |
| 567 | |
| 568 | static void ax8817x_mdio_write(struct net_device *netdev, int phy_id, int loc, int val) |
| 569 | { |
| 570 | struct usbnet *dev = netdev_priv(netdev); |
| 571 | u16 res = val; |
| 572 | u8 buf[1]; |
| 573 | |
| 574 | ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII, 0, 0, 0, &buf); |
| 575 | ax8817x_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, (u16 *)&res); |
| 576 | ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, &buf); |
| 577 | } |
| 578 | |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 579 | static int ax88172_link_reset(struct usbnet *dev) |
| 580 | { |
| 581 | u16 lpa; |
| 582 | u8 mode; |
| 583 | |
| 584 | mode = AX_MEDIUM_TX_ABORT_ALLOW | AX_MEDIUM_FLOW_CONTROL_EN; |
| 585 | lpa = ax8817x_mdio_read(dev->net, dev->mii.phy_id, MII_LPA); |
| 586 | if (lpa & LPA_DUPLEX) |
| 587 | mode |= AX_MEDIUM_FULL_DUPLEX; |
| 588 | ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL); |
| 589 | |
| 590 | return 0; |
| 591 | } |
| 592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | static void ax8817x_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo) |
| 594 | { |
| 595 | struct usbnet *dev = netdev_priv(net); |
| 596 | u8 opt; |
| 597 | |
| 598 | if (ax8817x_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) { |
| 599 | wolinfo->supported = 0; |
| 600 | wolinfo->wolopts = 0; |
| 601 | return; |
| 602 | } |
| 603 | wolinfo->supported = WAKE_PHY | WAKE_MAGIC; |
| 604 | wolinfo->wolopts = 0; |
| 605 | if (opt & AX_MONITOR_MODE) { |
| 606 | if (opt & AX_MONITOR_LINK) |
| 607 | wolinfo->wolopts |= WAKE_PHY; |
| 608 | if (opt & AX_MONITOR_MAGIC) |
| 609 | wolinfo->wolopts |= WAKE_MAGIC; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | static int ax8817x_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo) |
| 614 | { |
| 615 | struct usbnet *dev = netdev_priv(net); |
| 616 | u8 opt = 0; |
| 617 | u8 buf[1]; |
| 618 | |
| 619 | if (wolinfo->wolopts & WAKE_PHY) |
| 620 | opt |= AX_MONITOR_LINK; |
| 621 | if (wolinfo->wolopts & WAKE_MAGIC) |
| 622 | opt |= AX_MONITOR_MAGIC; |
| 623 | if (opt != 0) |
| 624 | opt |= AX_MONITOR_MODE; |
| 625 | |
| 626 | if (ax8817x_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE, |
| 627 | opt, 0, 0, &buf) < 0) |
| 628 | return -EINVAL; |
| 629 | |
| 630 | return 0; |
| 631 | } |
| 632 | |
| 633 | static int ax8817x_get_eeprom_len(struct net_device *net) |
| 634 | { |
| 635 | return AX_EEPROM_LEN; |
| 636 | } |
| 637 | |
| 638 | static int ax8817x_get_eeprom(struct net_device *net, |
| 639 | struct ethtool_eeprom *eeprom, u8 *data) |
| 640 | { |
| 641 | struct usbnet *dev = netdev_priv(net); |
| 642 | u16 *ebuf = (u16 *)data; |
| 643 | int i; |
| 644 | |
| 645 | /* Crude hack to ensure that we don't overwrite memory |
| 646 | * if an odd length is supplied |
| 647 | */ |
| 648 | if (eeprom->len % 2) |
| 649 | return -EINVAL; |
| 650 | |
| 651 | eeprom->magic = AX_EEPROM_MAGIC; |
| 652 | |
| 653 | /* ax8817x returns 2 bytes from eeprom on read */ |
| 654 | for (i=0; i < eeprom->len / 2; i++) { |
| 655 | if (ax8817x_read_cmd(dev, AX_CMD_READ_EEPROM, |
| 656 | eeprom->offset + i, 0, 2, &ebuf[i]) < 0) |
| 657 | return -EINVAL; |
| 658 | } |
| 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | static void ax8817x_get_drvinfo (struct net_device *net, |
| 663 | struct ethtool_drvinfo *info) |
| 664 | { |
| 665 | /* Inherit standard device info */ |
| 666 | usbnet_get_drvinfo(net, info); |
| 667 | info->eedump_len = 0x3e; |
| 668 | } |
| 669 | |
| 670 | static int ax8817x_get_settings(struct net_device *net, struct ethtool_cmd *cmd) |
| 671 | { |
| 672 | struct usbnet *dev = netdev_priv(net); |
| 673 | |
| 674 | return mii_ethtool_gset(&dev->mii,cmd); |
| 675 | } |
| 676 | |
| 677 | static int ax8817x_set_settings(struct net_device *net, struct ethtool_cmd *cmd) |
| 678 | { |
| 679 | struct usbnet *dev = netdev_priv(net); |
| 680 | |
| 681 | return mii_ethtool_sset(&dev->mii,cmd); |
| 682 | } |
| 683 | |
| 684 | /* We need to override some ethtool_ops so we require our |
| 685 | own structure so we don't interfere with other usbnet |
| 686 | devices that may be connected at the same time. */ |
| 687 | static struct ethtool_ops ax8817x_ethtool_ops = { |
| 688 | .get_drvinfo = ax8817x_get_drvinfo, |
| 689 | .get_link = ethtool_op_get_link, |
| 690 | .get_msglevel = usbnet_get_msglevel, |
| 691 | .set_msglevel = usbnet_set_msglevel, |
| 692 | .get_wol = ax8817x_get_wol, |
| 693 | .set_wol = ax8817x_set_wol, |
| 694 | .get_eeprom_len = ax8817x_get_eeprom_len, |
| 695 | .get_eeprom = ax8817x_get_eeprom, |
| 696 | .get_settings = ax8817x_get_settings, |
| 697 | .set_settings = ax8817x_set_settings, |
| 698 | }; |
| 699 | |
| 700 | static int ax8817x_bind(struct usbnet *dev, struct usb_interface *intf) |
| 701 | { |
| 702 | int ret = 0; |
| 703 | void *buf; |
| 704 | int i; |
| 705 | unsigned long gpio_bits = dev->driver_info->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
| 707 | get_endpoints(dev,intf); |
| 708 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | buf = kmalloc(ETH_ALEN, GFP_KERNEL); |
| 710 | if(!buf) { |
| 711 | ret = -ENOMEM; |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 712 | goto out1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /* Toggle the GPIOs in a manufacturer/model specific way */ |
| 716 | for (i = 2; i >= 0; i--) { |
| 717 | if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_GPIOS, |
| 718 | (gpio_bits >> (i * 8)) & 0xff, 0, 0, |
| 719 | buf)) < 0) |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 720 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | msleep(5); |
| 722 | } |
| 723 | |
| 724 | if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL, 0x80, 0, 0, buf)) < 0) { |
| 725 | dbg("send AX_CMD_WRITE_RX_CTL failed: %d", ret); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 726 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | /* Get the MAC address */ |
| 730 | memset(buf, 0, ETH_ALEN); |
| 731 | if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, 6, buf)) < 0) { |
| 732 | dbg("read AX_CMD_READ_NODE_ID failed: %d", ret); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 733 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | } |
| 735 | memcpy(dev->net->dev_addr, buf, ETH_ALEN); |
| 736 | |
| 737 | /* Get the PHY id */ |
| 738 | if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf)) < 0) { |
| 739 | dbg("error on read AX_CMD_READ_PHY_ID: %02x", ret); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 740 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | } else if (ret < 2) { |
| 742 | /* this should always return 2 bytes */ |
| 743 | dbg("AX_CMD_READ_PHY_ID returned less than 2 bytes: ret=%02x", ret); |
| 744 | ret = -EIO; |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 745 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | /* Initialize MII structure */ |
| 749 | dev->mii.dev = dev->net; |
| 750 | dev->mii.mdio_read = ax8817x_mdio_read; |
| 751 | dev->mii.mdio_write = ax8817x_mdio_write; |
| 752 | dev->mii.phy_id_mask = 0x3f; |
| 753 | dev->mii.reg_num_mask = 0x1f; |
| 754 | dev->mii.phy_id = *((u8 *)buf + 1); |
| 755 | |
| 756 | dev->net->set_multicast_list = ax8817x_set_multicast; |
| 757 | dev->net->ethtool_ops = &ax8817x_ethtool_ops; |
| 758 | |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 759 | ax8817x_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | ax8817x_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE, |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 761 | ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | mii_nway_restart(&dev->mii); |
| 763 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 764 | if (dev->driver_info->flags & FLAG_FRAMING_AX) { |
| 765 | /* REVISIT: adjust hard_header_len too */ |
| 766 | dev->hard_mtu = 2048; |
| 767 | } |
| 768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | out2: |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 771 | kfree(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | out1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | return ret; |
| 774 | } |
| 775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | static struct ethtool_ops ax88772_ethtool_ops = { |
| 777 | .get_drvinfo = ax8817x_get_drvinfo, |
| 778 | .get_link = ethtool_op_get_link, |
| 779 | .get_msglevel = usbnet_get_msglevel, |
| 780 | .set_msglevel = usbnet_set_msglevel, |
| 781 | .get_wol = ax8817x_get_wol, |
| 782 | .set_wol = ax8817x_set_wol, |
| 783 | .get_eeprom_len = ax8817x_get_eeprom_len, |
| 784 | .get_eeprom = ax8817x_get_eeprom, |
| 785 | .get_settings = ax8817x_get_settings, |
| 786 | .set_settings = ax8817x_set_settings, |
| 787 | }; |
| 788 | |
| 789 | static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf) |
| 790 | { |
| 791 | int ret; |
| 792 | void *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | |
| 794 | get_endpoints(dev,intf); |
| 795 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | buf = kmalloc(6, GFP_KERNEL); |
| 797 | if(!buf) { |
| 798 | dbg ("Cannot allocate memory for buffer"); |
| 799 | ret = -ENOMEM; |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 800 | goto out1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_GPIOS, |
| 804 | 0x00B0, 0, 0, buf)) < 0) |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 805 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | |
| 807 | msleep(5); |
| 808 | if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_PHY_SELECT, 0x0001, 0, 0, buf)) < 0) { |
| 809 | dbg("Select PHY #1 failed: %d", ret); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 810 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | if ((ret = |
| 814 | ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_IPPD, 0, 0, buf)) < 0) { |
| 815 | dbg("Failed to power down internal PHY: %d", ret); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 816 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | msleep(150); |
| 820 | if ((ret = |
| 821 | ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_CLEAR, 0, 0, buf)) < 0) { |
| 822 | dbg("Failed to perform software reset: %d", ret); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 823 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | msleep(150); |
| 827 | if ((ret = |
| 828 | ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_IPRL | AX_SWRESET_PRL, 0, 0, buf)) < 0) { |
| 829 | dbg("Failed to set Internal/External PHY reset control: %d", ret); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 830 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | msleep(150); |
| 834 | if ((ret = |
| 835 | ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL, 0x0000, 0, 0, |
| 836 | buf)) < 0) { |
| 837 | dbg("Failed to reset RX_CTL: %d", ret); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 838 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | /* Get the MAC address */ |
| 842 | memset(buf, 0, ETH_ALEN); |
| 843 | if ((ret = ax8817x_read_cmd(dev, AX88772_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf)) < 0) { |
| 844 | dbg("Failed to read MAC address: %d", ret); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 845 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | } |
| 847 | memcpy(dev->net->dev_addr, buf, ETH_ALEN); |
| 848 | |
| 849 | if ((ret = ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII, 0, 0, 0, buf)) < 0) { |
| 850 | dbg("Enabling software MII failed: %d", ret); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 851 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | if (((ret = |
| 855 | ax8817x_read_cmd(dev, AX_CMD_READ_MII_REG, 0x0010, 2, 2, buf)) < 0) |
| 856 | || (*((u16 *)buf) != 0x003b)) { |
| 857 | dbg("Read PHY register 2 must be 0x3b00: %d", ret); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 858 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /* Initialize MII structure */ |
| 862 | dev->mii.dev = dev->net; |
| 863 | dev->mii.mdio_read = ax8817x_mdio_read; |
| 864 | dev->mii.mdio_write = ax8817x_mdio_write; |
| 865 | dev->mii.phy_id_mask = 0xff; |
| 866 | dev->mii.reg_num_mask = 0xff; |
| 867 | |
| 868 | /* Get the PHY id */ |
| 869 | if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf)) < 0) { |
| 870 | dbg("Error reading PHY ID: %02x", ret); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 871 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | } else if (ret < 2) { |
| 873 | /* this should always return 2 bytes */ |
| 874 | dbg("AX_CMD_READ_PHY_ID returned less than 2 bytes: ret=%02x", |
| 875 | ret); |
| 876 | ret = -EIO; |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 877 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | } |
| 879 | dev->mii.phy_id = *((u8 *)buf + 1); |
| 880 | |
| 881 | if ((ret = |
| 882 | ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_PRL, 0, 0, buf)) < 0) { |
| 883 | dbg("Set external PHY reset pin level: %d", ret); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 884 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | } |
| 886 | msleep(150); |
| 887 | if ((ret = |
| 888 | ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_IPRL | AX_SWRESET_PRL, 0, 0, buf)) < 0) { |
| 889 | dbg("Set Internal/External PHY reset control: %d", ret); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 890 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | } |
| 892 | msleep(150); |
| 893 | |
| 894 | |
| 895 | dev->net->set_multicast_list = ax8817x_set_multicast; |
| 896 | dev->net->ethtool_ops = &ax88772_ethtool_ops; |
| 897 | |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 898 | ax8817x_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | ax8817x_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE, |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 900 | ADVERTISE_ALL | ADVERTISE_CSMA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | mii_nway_restart(&dev->mii); |
| 902 | |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 903 | if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, AX88772_MEDIUM_DEFAULT, 0, 0, buf)) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | dbg("Write medium mode register: %d", ret); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 905 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_IPG0, AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,AX88772_IPG2_DEFAULT, 0, buf)) < 0) { |
| 909 | dbg("Write IPG,IPG1,IPG2 failed: %d", ret); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 910 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | } |
| 912 | if ((ret = |
| 913 | ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, &buf)) < 0) { |
| 914 | dbg("Failed to set hardware MII: %02x", ret); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 915 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | /* Set RX_CTL to default values with 2k buffer, and enable cactus */ |
| 919 | if ((ret = |
| 920 | ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL, 0x0088, 0, 0, |
| 921 | buf)) < 0) { |
| 922 | dbg("Reset RX_CTL failed: %d", ret); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 923 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | kfree(buf); |
| 927 | |
| 928 | return 0; |
| 929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | out2: |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 931 | kfree(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | out1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | return ret; |
| 934 | } |
| 935 | |
| 936 | static int ax88772_rx_fixup(struct usbnet *dev, struct sk_buff *skb) |
| 937 | { |
| 938 | u32 *header; |
| 939 | char *packet; |
| 940 | struct sk_buff *ax_skb; |
| 941 | u16 size; |
| 942 | |
| 943 | header = (u32 *) skb->data; |
| 944 | le32_to_cpus(header); |
| 945 | packet = (char *)(header + 1); |
| 946 | |
| 947 | skb_pull(skb, 4); |
| 948 | |
| 949 | while (skb->len > 0) { |
| 950 | if ((short)(*header & 0x0000ffff) != |
| 951 | ~((short)((*header & 0xffff0000) >> 16))) { |
| 952 | devdbg(dev,"header length data is error"); |
| 953 | } |
| 954 | /* get the packet length */ |
| 955 | size = (u16) (*header & 0x0000ffff); |
| 956 | |
| 957 | if ((skb->len) - ((size + 1) & 0xfffe) == 0) |
| 958 | return 2; |
| 959 | if (size > ETH_FRAME_LEN) { |
| 960 | devdbg(dev,"invalid rx length %d", size); |
| 961 | return 0; |
| 962 | } |
| 963 | ax_skb = skb_clone(skb, GFP_ATOMIC); |
| 964 | if (ax_skb) { |
| 965 | ax_skb->len = size; |
| 966 | ax_skb->data = packet; |
| 967 | ax_skb->tail = packet + size; |
| 968 | skb_return(dev, ax_skb); |
| 969 | } else { |
| 970 | return 0; |
| 971 | } |
| 972 | |
| 973 | skb_pull(skb, (size + 1) & 0xfffe); |
| 974 | |
| 975 | if (skb->len == 0) |
| 976 | break; |
| 977 | |
| 978 | header = (u32 *) skb->data; |
| 979 | le32_to_cpus(header); |
| 980 | packet = (char *)(header + 1); |
| 981 | skb_pull(skb, 4); |
| 982 | } |
| 983 | |
| 984 | if (skb->len < 0) { |
| 985 | devdbg(dev,"invalid rx length %d", skb->len); |
| 986 | return 0; |
| 987 | } |
| 988 | return 1; |
| 989 | } |
| 990 | |
| 991 | static struct sk_buff *ax88772_tx_fixup(struct usbnet *dev, struct sk_buff *skb, |
david-b@pacbell.net | 0d9899f | 2005-07-28 20:46:32 -0700 | [diff] [blame] | 992 | unsigned flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | { |
| 994 | int padlen; |
| 995 | int headroom = skb_headroom(skb); |
| 996 | int tailroom = skb_tailroom(skb); |
| 997 | u32 *packet_len; |
| 998 | u32 *padbytes_ptr; |
| 999 | |
| 1000 | padlen = ((skb->len + 4) % 512) ? 0 : 4; |
| 1001 | |
| 1002 | if ((!skb_cloned(skb)) |
| 1003 | && ((headroom + tailroom) >= (4 + padlen))) { |
| 1004 | if ((headroom < 4) || (tailroom < padlen)) { |
| 1005 | skb->data = memmove(skb->head + 4, skb->data, skb->len); |
| 1006 | skb->tail = skb->data + skb->len; |
| 1007 | } |
| 1008 | } else { |
| 1009 | struct sk_buff *skb2; |
| 1010 | skb2 = skb_copy_expand(skb, 4, padlen, flags); |
| 1011 | dev_kfree_skb_any(skb); |
| 1012 | skb = skb2; |
| 1013 | if (!skb) |
| 1014 | return NULL; |
| 1015 | } |
| 1016 | |
| 1017 | packet_len = (u32 *) skb_push(skb, 4); |
| 1018 | |
| 1019 | packet_len = (u32 *) skb->data; |
| 1020 | *packet_len = (((skb->len - 4) ^ 0x0000ffff) << 16) + (skb->len - 4); |
| 1021 | |
| 1022 | if ((skb->len % 512) == 0) { |
| 1023 | padbytes_ptr = (u32 *) skb->tail; |
| 1024 | *padbytes_ptr = 0xffff0000; |
| 1025 | skb_put(skb, padlen); |
| 1026 | } |
| 1027 | return skb; |
| 1028 | } |
| 1029 | |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 1030 | static int ax88772_link_reset(struct usbnet *dev) |
| 1031 | { |
| 1032 | u16 lpa; |
| 1033 | u16 mode; |
| 1034 | |
| 1035 | mode = AX88772_MEDIUM_DEFAULT; |
| 1036 | lpa = ax8817x_mdio_read(dev->net, dev->mii.phy_id, MII_LPA); |
| 1037 | |
| 1038 | if ((lpa & LPA_DUPLEX) == 0) |
| 1039 | mode &= ~AX88772_MEDIUM_FULL_DUPLEX; |
| 1040 | if ((lpa & LPA_100) == 0) |
| 1041 | mode &= ~AX88772_MEDIUM_100MB; |
| 1042 | ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL); |
| 1043 | |
| 1044 | return 0; |
| 1045 | } |
| 1046 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | static const struct driver_info ax8817x_info = { |
| 1048 | .description = "ASIX AX8817x USB 2.0 Ethernet", |
| 1049 | .bind = ax8817x_bind, |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 1050 | .status = ax8817x_status, |
| 1051 | .link_reset = ax88172_link_reset, |
| 1052 | .reset = ax88172_link_reset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | .flags = FLAG_ETHER, |
| 1054 | .data = 0x00130103, |
| 1055 | }; |
| 1056 | |
| 1057 | static const struct driver_info dlink_dub_e100_info = { |
| 1058 | .description = "DLink DUB-E100 USB Ethernet", |
| 1059 | .bind = ax8817x_bind, |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 1060 | .status = ax8817x_status, |
| 1061 | .link_reset = ax88172_link_reset, |
| 1062 | .reset = ax88172_link_reset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | .flags = FLAG_ETHER, |
| 1064 | .data = 0x009f9d9f, |
| 1065 | }; |
| 1066 | |
| 1067 | static const struct driver_info netgear_fa120_info = { |
| 1068 | .description = "Netgear FA-120 USB Ethernet", |
| 1069 | .bind = ax8817x_bind, |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 1070 | .status = ax8817x_status, |
| 1071 | .link_reset = ax88172_link_reset, |
| 1072 | .reset = ax88172_link_reset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | .flags = FLAG_ETHER, |
| 1074 | .data = 0x00130103, |
| 1075 | }; |
| 1076 | |
| 1077 | static const struct driver_info hawking_uf200_info = { |
| 1078 | .description = "Hawking UF200 USB Ethernet", |
| 1079 | .bind = ax8817x_bind, |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 1080 | .status = ax8817x_status, |
| 1081 | .link_reset = ax88172_link_reset, |
| 1082 | .reset = ax88172_link_reset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | .flags = FLAG_ETHER, |
| 1084 | .data = 0x001f1d1f, |
| 1085 | }; |
| 1086 | |
| 1087 | static const struct driver_info ax88772_info = { |
| 1088 | .description = "ASIX AX88772 USB 2.0 Ethernet", |
| 1089 | .bind = ax88772_bind, |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 1090 | .status = ax8817x_status, |
| 1091 | .link_reset = ax88772_link_reset, |
| 1092 | .reset = ax88772_link_reset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | .flags = FLAG_ETHER | FLAG_FRAMING_AX, |
| 1094 | .rx_fixup = ax88772_rx_fixup, |
| 1095 | .tx_fixup = ax88772_tx_fixup, |
| 1096 | .data = 0x00130103, |
| 1097 | }; |
| 1098 | |
| 1099 | #endif /* CONFIG_USB_AX8817X */ |
| 1100 | |
| 1101 | |
| 1102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | /*------------------------------------------------------------------------- |
| 1104 | * |
| 1105 | * Communications Device Class declarations. |
| 1106 | * Used by CDC Ethernet, and some CDC variants |
| 1107 | * |
| 1108 | *-------------------------------------------------------------------------*/ |
| 1109 | |
| 1110 | #ifdef CONFIG_USB_CDCETHER |
| 1111 | #define NEED_GENERIC_CDC |
| 1112 | #endif |
| 1113 | |
| 1114 | #ifdef CONFIG_USB_ZAURUS |
| 1115 | /* Ethernet variant uses funky framing, broken ethernet addressing */ |
| 1116 | #define NEED_GENERIC_CDC |
| 1117 | #endif |
| 1118 | |
| 1119 | #ifdef CONFIG_USB_RNDIS |
| 1120 | /* ACM variant uses even funkier framing, complex control RPC scheme */ |
| 1121 | #define NEED_GENERIC_CDC |
| 1122 | #endif |
| 1123 | |
| 1124 | |
| 1125 | #ifdef NEED_GENERIC_CDC |
| 1126 | |
| 1127 | #include <linux/usb_cdc.h> |
| 1128 | |
| 1129 | struct cdc_state { |
| 1130 | struct usb_cdc_header_desc *header; |
| 1131 | struct usb_cdc_union_desc *u; |
| 1132 | struct usb_cdc_ether_desc *ether; |
| 1133 | struct usb_interface *control; |
| 1134 | struct usb_interface *data; |
| 1135 | }; |
| 1136 | |
| 1137 | static struct usb_driver usbnet_driver; |
| 1138 | |
| 1139 | /* |
| 1140 | * probes control interface, claims data interface, collects the bulk |
| 1141 | * endpoints, activates data interface (if needed), maybe sets MTU. |
| 1142 | * all pure cdc, except for certain firmware workarounds. |
| 1143 | */ |
| 1144 | static int generic_cdc_bind (struct usbnet *dev, struct usb_interface *intf) |
| 1145 | { |
| 1146 | u8 *buf = intf->cur_altsetting->extra; |
| 1147 | int len = intf->cur_altsetting->extralen; |
| 1148 | struct usb_interface_descriptor *d; |
| 1149 | struct cdc_state *info = (void *) &dev->data; |
| 1150 | int status; |
| 1151 | int rndis; |
| 1152 | |
| 1153 | if (sizeof dev->data < sizeof *info) |
| 1154 | return -EDOM; |
| 1155 | |
| 1156 | /* expect strict spec conformance for the descriptors, but |
| 1157 | * cope with firmware which stores them in the wrong place |
| 1158 | */ |
| 1159 | if (len == 0 && dev->udev->actconfig->extralen) { |
| 1160 | /* Motorola SB4100 (and others: Brad Hards says it's |
| 1161 | * from a Broadcom design) put CDC descriptors here |
| 1162 | */ |
| 1163 | buf = dev->udev->actconfig->extra; |
| 1164 | len = dev->udev->actconfig->extralen; |
| 1165 | if (len) |
| 1166 | dev_dbg (&intf->dev, |
| 1167 | "CDC descriptors on config\n"); |
| 1168 | } |
| 1169 | |
| 1170 | /* this assumes that if there's a non-RNDIS vendor variant |
| 1171 | * of cdc-acm, it'll fail RNDIS requests cleanly. |
| 1172 | */ |
| 1173 | rndis = (intf->cur_altsetting->desc.bInterfaceProtocol == 0xff); |
| 1174 | |
| 1175 | memset (info, 0, sizeof *info); |
| 1176 | info->control = intf; |
| 1177 | while (len > 3) { |
| 1178 | if (buf [1] != USB_DT_CS_INTERFACE) |
| 1179 | goto next_desc; |
| 1180 | |
| 1181 | /* use bDescriptorSubType to identify the CDC descriptors. |
| 1182 | * We expect devices with CDC header and union descriptors. |
| 1183 | * For CDC Ethernet we need the ethernet descriptor. |
| 1184 | * For RNDIS, ignore two (pointless) CDC modem descriptors |
| 1185 | * in favor of a complicated OID-based RPC scheme doing what |
| 1186 | * CDC Ethernet achieves with a simple descriptor. |
| 1187 | */ |
| 1188 | switch (buf [2]) { |
| 1189 | case USB_CDC_HEADER_TYPE: |
| 1190 | if (info->header) { |
| 1191 | dev_dbg (&intf->dev, "extra CDC header\n"); |
| 1192 | goto bad_desc; |
| 1193 | } |
| 1194 | info->header = (void *) buf; |
| 1195 | if (info->header->bLength != sizeof *info->header) { |
| 1196 | dev_dbg (&intf->dev, "CDC header len %u\n", |
| 1197 | info->header->bLength); |
| 1198 | goto bad_desc; |
| 1199 | } |
| 1200 | break; |
| 1201 | case USB_CDC_UNION_TYPE: |
| 1202 | if (info->u) { |
| 1203 | dev_dbg (&intf->dev, "extra CDC union\n"); |
| 1204 | goto bad_desc; |
| 1205 | } |
| 1206 | info->u = (void *) buf; |
| 1207 | if (info->u->bLength != sizeof *info->u) { |
| 1208 | dev_dbg (&intf->dev, "CDC union len %u\n", |
| 1209 | info->u->bLength); |
| 1210 | goto bad_desc; |
| 1211 | } |
| 1212 | |
| 1213 | /* we need a master/control interface (what we're |
| 1214 | * probed with) and a slave/data interface; union |
| 1215 | * descriptors sort this all out. |
| 1216 | */ |
| 1217 | info->control = usb_ifnum_to_if(dev->udev, |
| 1218 | info->u->bMasterInterface0); |
| 1219 | info->data = usb_ifnum_to_if(dev->udev, |
| 1220 | info->u->bSlaveInterface0); |
| 1221 | if (!info->control || !info->data) { |
| 1222 | dev_dbg (&intf->dev, |
| 1223 | "master #%u/%p slave #%u/%p\n", |
| 1224 | info->u->bMasterInterface0, |
| 1225 | info->control, |
| 1226 | info->u->bSlaveInterface0, |
| 1227 | info->data); |
| 1228 | goto bad_desc; |
| 1229 | } |
| 1230 | if (info->control != intf) { |
| 1231 | dev_dbg (&intf->dev, "bogus CDC Union\n"); |
| 1232 | /* Ambit USB Cable Modem (and maybe others) |
| 1233 | * interchanges master and slave interface. |
| 1234 | */ |
| 1235 | if (info->data == intf) { |
| 1236 | info->data = info->control; |
| 1237 | info->control = intf; |
| 1238 | } else |
| 1239 | goto bad_desc; |
| 1240 | } |
| 1241 | |
| 1242 | /* a data interface altsetting does the real i/o */ |
| 1243 | d = &info->data->cur_altsetting->desc; |
| 1244 | if (d->bInterfaceClass != USB_CLASS_CDC_DATA) { |
| 1245 | dev_dbg (&intf->dev, "slave class %u\n", |
| 1246 | d->bInterfaceClass); |
| 1247 | goto bad_desc; |
| 1248 | } |
| 1249 | break; |
| 1250 | case USB_CDC_ETHERNET_TYPE: |
| 1251 | if (info->ether) { |
| 1252 | dev_dbg (&intf->dev, "extra CDC ether\n"); |
| 1253 | goto bad_desc; |
| 1254 | } |
| 1255 | info->ether = (void *) buf; |
| 1256 | if (info->ether->bLength != sizeof *info->ether) { |
| 1257 | dev_dbg (&intf->dev, "CDC ether len %u\n", |
David Brownell | e3bc8b4 | 2005-06-15 08:04:30 -0700 | [diff] [blame] | 1258 | info->ether->bLength); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | goto bad_desc; |
| 1260 | } |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1261 | dev->hard_mtu = le16_to_cpu( |
| 1262 | info->ether->wMaxSegmentSize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | /* because of Zaurus, we may be ignoring the host |
| 1264 | * side link address we were given. |
| 1265 | */ |
| 1266 | break; |
| 1267 | } |
| 1268 | next_desc: |
| 1269 | len -= buf [0]; /* bLength */ |
| 1270 | buf += buf [0]; |
| 1271 | } |
| 1272 | |
| 1273 | if (!info->header || !info->u || (!rndis && !info->ether)) { |
| 1274 | dev_dbg (&intf->dev, "missing cdc %s%s%sdescriptor\n", |
| 1275 | info->header ? "" : "header ", |
| 1276 | info->u ? "" : "union ", |
| 1277 | info->ether ? "" : "ether "); |
| 1278 | goto bad_desc; |
| 1279 | } |
| 1280 | |
| 1281 | /* claim data interface and set it up ... with side effects. |
| 1282 | * network traffic can't flow until an altsetting is enabled. |
| 1283 | */ |
| 1284 | status = usb_driver_claim_interface (&usbnet_driver, info->data, dev); |
| 1285 | if (status < 0) |
| 1286 | return status; |
| 1287 | status = get_endpoints (dev, info->data); |
| 1288 | if (status < 0) { |
| 1289 | /* ensure immediate exit from usbnet_disconnect */ |
| 1290 | usb_set_intfdata(info->data, NULL); |
| 1291 | usb_driver_release_interface (&usbnet_driver, info->data); |
| 1292 | return status; |
| 1293 | } |
| 1294 | |
| 1295 | /* status endpoint: optional for CDC Ethernet, not RNDIS (or ACM) */ |
| 1296 | dev->status = NULL; |
| 1297 | if (info->control->cur_altsetting->desc.bNumEndpoints == 1) { |
| 1298 | struct usb_endpoint_descriptor *desc; |
| 1299 | |
| 1300 | dev->status = &info->control->cur_altsetting->endpoint [0]; |
| 1301 | desc = &dev->status->desc; |
| 1302 | if (desc->bmAttributes != USB_ENDPOINT_XFER_INT |
| 1303 | || !(desc->bEndpointAddress & USB_DIR_IN) |
| 1304 | || (le16_to_cpu(desc->wMaxPacketSize) |
| 1305 | < sizeof (struct usb_cdc_notification)) |
| 1306 | || !desc->bInterval) { |
| 1307 | dev_dbg (&intf->dev, "bad notification endpoint\n"); |
| 1308 | dev->status = NULL; |
| 1309 | } |
| 1310 | } |
| 1311 | if (rndis && !dev->status) { |
| 1312 | dev_dbg (&intf->dev, "missing RNDIS status endpoint\n"); |
| 1313 | usb_set_intfdata(info->data, NULL); |
| 1314 | usb_driver_release_interface (&usbnet_driver, info->data); |
| 1315 | return -ENODEV; |
| 1316 | } |
| 1317 | return 0; |
| 1318 | |
| 1319 | bad_desc: |
| 1320 | dev_info (&dev->udev->dev, "bad CDC descriptors\n"); |
| 1321 | return -ENODEV; |
| 1322 | } |
| 1323 | |
| 1324 | static void cdc_unbind (struct usbnet *dev, struct usb_interface *intf) |
| 1325 | { |
| 1326 | struct cdc_state *info = (void *) &dev->data; |
| 1327 | |
| 1328 | /* disconnect master --> disconnect slave */ |
| 1329 | if (intf == info->control && info->data) { |
| 1330 | /* ensure immediate exit from usbnet_disconnect */ |
| 1331 | usb_set_intfdata(info->data, NULL); |
| 1332 | usb_driver_release_interface (&usbnet_driver, info->data); |
| 1333 | info->data = NULL; |
| 1334 | } |
| 1335 | |
| 1336 | /* and vice versa (just in case) */ |
| 1337 | else if (intf == info->data && info->control) { |
| 1338 | /* ensure immediate exit from usbnet_disconnect */ |
| 1339 | usb_set_intfdata(info->control, NULL); |
| 1340 | usb_driver_release_interface (&usbnet_driver, info->control); |
| 1341 | info->control = NULL; |
| 1342 | } |
| 1343 | } |
| 1344 | |
David Brownell | 80615f8 | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 1345 | #endif /* NEED_GENERIC_CDC */ |
| 1346 | |
| 1347 | |
| 1348 | #ifdef CONFIG_USB_CDCETHER |
| 1349 | #define HAVE_HARDWARE |
| 1350 | |
| 1351 | /*------------------------------------------------------------------------- |
| 1352 | * |
| 1353 | * Communications Device Class, Ethernet Control model |
| 1354 | * |
| 1355 | * Takes two interfaces. The DATA interface is inactive till an altsetting |
| 1356 | * is selected. Configuration data includes class descriptors. |
| 1357 | * |
| 1358 | * This should interop with whatever the 2.4 "CDCEther.c" driver |
| 1359 | * (by Brad Hards) talked with. |
| 1360 | * |
| 1361 | *-------------------------------------------------------------------------*/ |
| 1362 | |
| 1363 | #include <linux/ctype.h> |
| 1364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
| 1366 | static void dumpspeed (struct usbnet *dev, __le32 *speeds) |
| 1367 | { |
| 1368 | if (netif_msg_timer (dev)) |
| 1369 | devinfo (dev, "link speeds: %u kbps up, %u kbps down", |
| 1370 | __le32_to_cpu(speeds[0]) / 1000, |
| 1371 | __le32_to_cpu(speeds[1]) / 1000); |
| 1372 | } |
| 1373 | |
| 1374 | static void cdc_status (struct usbnet *dev, struct urb *urb) |
| 1375 | { |
| 1376 | struct usb_cdc_notification *event; |
| 1377 | |
| 1378 | if (urb->actual_length < sizeof *event) |
| 1379 | return; |
| 1380 | |
| 1381 | /* SPEED_CHANGE can get split into two 8-byte packets */ |
| 1382 | if (test_and_clear_bit (EVENT_STS_SPLIT, &dev->flags)) { |
| 1383 | dumpspeed (dev, (__le32 *) urb->transfer_buffer); |
| 1384 | return; |
| 1385 | } |
| 1386 | |
| 1387 | event = urb->transfer_buffer; |
| 1388 | switch (event->bNotificationType) { |
| 1389 | case USB_CDC_NOTIFY_NETWORK_CONNECTION: |
| 1390 | if (netif_msg_timer (dev)) |
| 1391 | devdbg (dev, "CDC: carrier %s", |
| 1392 | event->wValue ? "on" : "off"); |
| 1393 | if (event->wValue) |
| 1394 | netif_carrier_on(dev->net); |
| 1395 | else |
| 1396 | netif_carrier_off(dev->net); |
| 1397 | break; |
| 1398 | case USB_CDC_NOTIFY_SPEED_CHANGE: /* tx/rx rates */ |
| 1399 | if (netif_msg_timer (dev)) |
| 1400 | devdbg (dev, "CDC: speed change (len %d)", |
| 1401 | urb->actual_length); |
| 1402 | if (urb->actual_length != (sizeof *event + 8)) |
| 1403 | set_bit (EVENT_STS_SPLIT, &dev->flags); |
| 1404 | else |
| 1405 | dumpspeed (dev, (__le32 *) &event[1]); |
| 1406 | break; |
| 1407 | // case USB_CDC_NOTIFY_RESPONSE_AVAILABLE: /* RNDIS; or unsolicited */ |
| 1408 | default: |
| 1409 | deverr (dev, "CDC: unexpected notification %02x!", |
| 1410 | event->bNotificationType); |
| 1411 | break; |
| 1412 | } |
| 1413 | } |
| 1414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | static u8 nibble (unsigned char c) |
| 1416 | { |
| 1417 | if (likely (isdigit (c))) |
| 1418 | return c - '0'; |
| 1419 | c = toupper (c); |
| 1420 | if (likely (isxdigit (c))) |
| 1421 | return 10 + c - 'A'; |
| 1422 | return 0; |
| 1423 | } |
| 1424 | |
| 1425 | static inline int |
| 1426 | get_ethernet_addr (struct usbnet *dev, struct usb_cdc_ether_desc *e) |
| 1427 | { |
| 1428 | int tmp, i; |
| 1429 | unsigned char buf [13]; |
| 1430 | |
| 1431 | tmp = usb_string (dev->udev, e->iMACAddress, buf, sizeof buf); |
| 1432 | if (tmp != 12) { |
| 1433 | dev_dbg (&dev->udev->dev, |
| 1434 | "bad MAC string %d fetch, %d\n", e->iMACAddress, tmp); |
| 1435 | if (tmp >= 0) |
| 1436 | tmp = -EINVAL; |
| 1437 | return tmp; |
| 1438 | } |
| 1439 | for (i = tmp = 0; i < 6; i++, tmp += 2) |
| 1440 | dev->net->dev_addr [i] = |
| 1441 | (nibble (buf [tmp]) << 4) + nibble (buf [tmp + 1]); |
| 1442 | return 0; |
| 1443 | } |
| 1444 | |
| 1445 | static int cdc_bind (struct usbnet *dev, struct usb_interface *intf) |
| 1446 | { |
| 1447 | int status; |
| 1448 | struct cdc_state *info = (void *) &dev->data; |
| 1449 | |
| 1450 | status = generic_cdc_bind (dev, intf); |
| 1451 | if (status < 0) |
| 1452 | return status; |
| 1453 | |
| 1454 | status = get_ethernet_addr (dev, info->ether); |
| 1455 | if (status < 0) { |
| 1456 | usb_set_intfdata(info->data, NULL); |
| 1457 | usb_driver_release_interface (&usbnet_driver, info->data); |
| 1458 | return status; |
| 1459 | } |
| 1460 | |
| 1461 | /* FIXME cdc-ether has some multicast code too, though it complains |
| 1462 | * in routine cases. info->ether describes the multicast support. |
| 1463 | */ |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
| 1467 | static const struct driver_info cdc_info = { |
| 1468 | .description = "CDC Ethernet Device", |
| 1469 | .flags = FLAG_ETHER, |
| 1470 | // .check_connect = cdc_check_connect, |
| 1471 | .bind = cdc_bind, |
| 1472 | .unbind = cdc_unbind, |
| 1473 | .status = cdc_status, |
| 1474 | }; |
| 1475 | |
| 1476 | #endif /* CONFIG_USB_CDCETHER */ |
| 1477 | |
| 1478 | |
| 1479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | #ifdef CONFIG_USB_GENESYS |
| 1481 | #define HAVE_HARDWARE |
| 1482 | |
| 1483 | /*------------------------------------------------------------------------- |
| 1484 | * |
| 1485 | * GeneSys GL620USB-A (www.genesyslogic.com.tw) |
| 1486 | * |
| 1487 | * ... should partially interop with the Win32 driver for this hardware |
| 1488 | * The GeneSys docs imply there's some NDIS issue motivating this framing. |
| 1489 | * |
| 1490 | * Some info from GeneSys: |
| 1491 | * - GL620USB-A is full duplex; GL620USB is only half duplex for bulk. |
| 1492 | * (Some cables, like the BAFO-100c, use the half duplex version.) |
| 1493 | * - For the full duplex model, the low bit of the version code says |
| 1494 | * which side is which ("left/right"). |
| 1495 | * - For the half duplex type, a control/interrupt handshake settles |
| 1496 | * the transfer direction. (That's disabled here, partially coded.) |
| 1497 | * A control URB would block until other side writes an interrupt. |
| 1498 | * |
| 1499 | * Original code from Jiun-Jie Huang <huangjj@genesyslogic.com.tw> |
| 1500 | * and merged into "usbnet" by Stanislav Brabec <utx@penguin.cz>. |
| 1501 | * |
| 1502 | *-------------------------------------------------------------------------*/ |
| 1503 | |
| 1504 | // control msg write command |
| 1505 | #define GENELINK_CONNECT_WRITE 0xF0 |
| 1506 | // interrupt pipe index |
| 1507 | #define GENELINK_INTERRUPT_PIPE 0x03 |
| 1508 | // interrupt read buffer size |
| 1509 | #define INTERRUPT_BUFSIZE 0x08 |
| 1510 | // interrupt pipe interval value |
| 1511 | #define GENELINK_INTERRUPT_INTERVAL 0x10 |
| 1512 | // max transmit packet number per transmit |
| 1513 | #define GL_MAX_TRANSMIT_PACKETS 32 |
| 1514 | // max packet length |
| 1515 | #define GL_MAX_PACKET_LEN 1514 |
| 1516 | // max receive buffer size |
| 1517 | #define GL_RCV_BUF_SIZE \ |
| 1518 | (((GL_MAX_PACKET_LEN + 4) * GL_MAX_TRANSMIT_PACKETS) + 4) |
| 1519 | |
| 1520 | struct gl_packet { |
| 1521 | u32 packet_length; |
| 1522 | char packet_data [1]; |
| 1523 | }; |
| 1524 | |
| 1525 | struct gl_header { |
| 1526 | u32 packet_count; |
| 1527 | struct gl_packet packets; |
| 1528 | }; |
| 1529 | |
| 1530 | #ifdef GENELINK_ACK |
| 1531 | |
| 1532 | // FIXME: this code is incomplete, not debugged; it doesn't |
| 1533 | // handle interrupts correctly. interrupts should be generic |
| 1534 | // code like all other device I/O, anyway. |
| 1535 | |
| 1536 | struct gl_priv { |
| 1537 | struct urb *irq_urb; |
| 1538 | char irq_buf [INTERRUPT_BUFSIZE]; |
| 1539 | }; |
| 1540 | |
| 1541 | static inline int gl_control_write (struct usbnet *dev, u8 request, u16 value) |
| 1542 | { |
| 1543 | int retval; |
| 1544 | |
| 1545 | retval = usb_control_msg (dev->udev, |
| 1546 | usb_sndctrlpipe (dev->udev, 0), |
| 1547 | request, |
| 1548 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 1549 | value, |
| 1550 | 0, // index |
| 1551 | 0, // data buffer |
| 1552 | 0, // size |
| 1553 | CONTROL_TIMEOUT_MS); |
| 1554 | return retval; |
| 1555 | } |
| 1556 | |
| 1557 | static void gl_interrupt_complete (struct urb *urb, struct pt_regs *regs) |
| 1558 | { |
| 1559 | int status = urb->status; |
| 1560 | |
| 1561 | switch (status) { |
| 1562 | case 0: |
| 1563 | /* success */ |
| 1564 | break; |
| 1565 | case -ECONNRESET: |
| 1566 | case -ENOENT: |
| 1567 | case -ESHUTDOWN: |
| 1568 | /* this urb is terminated, clean up */ |
| 1569 | dbg("%s - urb shutting down with status: %d", |
| 1570 | __FUNCTION__, status); |
| 1571 | return; |
| 1572 | default: |
| 1573 | dbg("%s - nonzero urb status received: %d", |
| 1574 | __FUNCTION__, urb->status); |
| 1575 | } |
| 1576 | |
| 1577 | status = usb_submit_urb (urb, GFP_ATOMIC); |
| 1578 | if (status) |
| 1579 | err ("%s - usb_submit_urb failed with result %d", |
| 1580 | __FUNCTION__, status); |
| 1581 | } |
| 1582 | |
| 1583 | static int gl_interrupt_read (struct usbnet *dev) |
| 1584 | { |
| 1585 | struct gl_priv *priv = dev->priv_data; |
| 1586 | int retval; |
| 1587 | |
| 1588 | // issue usb interrupt read |
| 1589 | if (priv && priv->irq_urb) { |
| 1590 | // submit urb |
| 1591 | if ((retval = usb_submit_urb (priv->irq_urb, GFP_KERNEL)) != 0) |
| 1592 | dbg ("gl_interrupt_read: submit fail - %X...", retval); |
| 1593 | else |
| 1594 | dbg ("gl_interrupt_read: submit success..."); |
| 1595 | } |
| 1596 | |
| 1597 | return 0; |
| 1598 | } |
| 1599 | |
| 1600 | // check whether another side is connected |
| 1601 | static int genelink_check_connect (struct usbnet *dev) |
| 1602 | { |
| 1603 | int retval; |
| 1604 | |
| 1605 | dbg ("genelink_check_connect..."); |
| 1606 | |
| 1607 | // detect whether another side is connected |
| 1608 | if ((retval = gl_control_write (dev, GENELINK_CONNECT_WRITE, 0)) != 0) { |
| 1609 | dbg ("%s: genelink_check_connect write fail - %X", |
| 1610 | dev->net->name, retval); |
| 1611 | return retval; |
| 1612 | } |
| 1613 | |
| 1614 | // usb interrupt read to ack another side |
| 1615 | if ((retval = gl_interrupt_read (dev)) != 0) { |
| 1616 | dbg ("%s: genelink_check_connect read fail - %X", |
| 1617 | dev->net->name, retval); |
| 1618 | return retval; |
| 1619 | } |
| 1620 | |
| 1621 | dbg ("%s: genelink_check_connect read success", dev->net->name); |
| 1622 | return 0; |
| 1623 | } |
| 1624 | |
| 1625 | // allocate and initialize the private data for genelink |
| 1626 | static int genelink_init (struct usbnet *dev) |
| 1627 | { |
| 1628 | struct gl_priv *priv; |
| 1629 | |
| 1630 | // allocate the private data structure |
| 1631 | if ((priv = kmalloc (sizeof *priv, GFP_KERNEL)) == 0) { |
| 1632 | dbg ("%s: cannot allocate private data per device", |
| 1633 | dev->net->name); |
| 1634 | return -ENOMEM; |
| 1635 | } |
| 1636 | |
| 1637 | // allocate irq urb |
| 1638 | if ((priv->irq_urb = usb_alloc_urb (0, GFP_KERNEL)) == 0) { |
| 1639 | dbg ("%s: cannot allocate private irq urb per device", |
| 1640 | dev->net->name); |
| 1641 | kfree (priv); |
| 1642 | return -ENOMEM; |
| 1643 | } |
| 1644 | |
| 1645 | // fill irq urb |
| 1646 | usb_fill_int_urb (priv->irq_urb, dev->udev, |
| 1647 | usb_rcvintpipe (dev->udev, GENELINK_INTERRUPT_PIPE), |
| 1648 | priv->irq_buf, INTERRUPT_BUFSIZE, |
| 1649 | gl_interrupt_complete, 0, |
| 1650 | GENELINK_INTERRUPT_INTERVAL); |
| 1651 | |
| 1652 | // set private data pointer |
| 1653 | dev->priv_data = priv; |
| 1654 | |
| 1655 | return 0; |
| 1656 | } |
| 1657 | |
| 1658 | // release the private data |
| 1659 | static int genelink_free (struct usbnet *dev) |
| 1660 | { |
| 1661 | struct gl_priv *priv = dev->priv_data; |
| 1662 | |
| 1663 | if (!priv) |
| 1664 | return 0; |
| 1665 | |
| 1666 | // FIXME: can't cancel here; it's synchronous, and |
| 1667 | // should have happened earlier in any case (interrupt |
| 1668 | // handling needs to be generic) |
| 1669 | |
| 1670 | // cancel irq urb first |
| 1671 | usb_kill_urb (priv->irq_urb); |
| 1672 | |
| 1673 | // free irq urb |
| 1674 | usb_free_urb (priv->irq_urb); |
| 1675 | |
| 1676 | // free the private data structure |
| 1677 | kfree (priv); |
| 1678 | |
| 1679 | return 0; |
| 1680 | } |
| 1681 | |
| 1682 | #endif |
| 1683 | |
| 1684 | static int genelink_rx_fixup (struct usbnet *dev, struct sk_buff *skb) |
| 1685 | { |
| 1686 | struct gl_header *header; |
| 1687 | struct gl_packet *packet; |
| 1688 | struct sk_buff *gl_skb; |
| 1689 | u32 size; |
| 1690 | |
| 1691 | header = (struct gl_header *) skb->data; |
| 1692 | |
| 1693 | // get the packet count of the received skb |
| 1694 | le32_to_cpus (&header->packet_count); |
| 1695 | if ((header->packet_count > GL_MAX_TRANSMIT_PACKETS) |
| 1696 | || (header->packet_count < 0)) { |
| 1697 | dbg ("genelink: invalid received packet count %d", |
| 1698 | header->packet_count); |
| 1699 | return 0; |
| 1700 | } |
| 1701 | |
| 1702 | // set the current packet pointer to the first packet |
| 1703 | packet = &header->packets; |
| 1704 | |
| 1705 | // decrement the length for the packet count size 4 bytes |
| 1706 | skb_pull (skb, 4); |
| 1707 | |
| 1708 | while (header->packet_count > 1) { |
| 1709 | // get the packet length |
| 1710 | size = packet->packet_length; |
| 1711 | |
| 1712 | // this may be a broken packet |
| 1713 | if (size > GL_MAX_PACKET_LEN) { |
| 1714 | dbg ("genelink: invalid rx length %d", size); |
| 1715 | return 0; |
| 1716 | } |
| 1717 | |
| 1718 | // allocate the skb for the individual packet |
| 1719 | gl_skb = alloc_skb (size, GFP_ATOMIC); |
| 1720 | if (gl_skb) { |
| 1721 | |
| 1722 | // copy the packet data to the new skb |
| 1723 | memcpy(skb_put(gl_skb, size), packet->packet_data, size); |
lepton | 2bbfb16 | 2005-08-22 17:06:14 -0700 | [diff] [blame] | 1724 | skb_return (dev, gl_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | // advance to the next packet |
| 1728 | packet = (struct gl_packet *) |
| 1729 | &packet->packet_data [size]; |
| 1730 | header->packet_count--; |
| 1731 | |
| 1732 | // shift the data pointer to the next gl_packet |
| 1733 | skb_pull (skb, size + 4); |
| 1734 | } |
| 1735 | |
| 1736 | // skip the packet length field 4 bytes |
| 1737 | skb_pull (skb, 4); |
| 1738 | |
| 1739 | if (skb->len > GL_MAX_PACKET_LEN) { |
| 1740 | dbg ("genelink: invalid rx length %d", skb->len); |
| 1741 | return 0; |
| 1742 | } |
| 1743 | return 1; |
| 1744 | } |
| 1745 | |
| 1746 | static struct sk_buff * |
david-b@pacbell.net | 0d9899f | 2005-07-28 20:46:32 -0700 | [diff] [blame] | 1747 | genelink_tx_fixup (struct usbnet *dev, struct sk_buff *skb, unsigned flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | { |
| 1749 | int padlen; |
| 1750 | int length = skb->len; |
| 1751 | int headroom = skb_headroom (skb); |
| 1752 | int tailroom = skb_tailroom (skb); |
| 1753 | u32 *packet_count; |
| 1754 | u32 *packet_len; |
| 1755 | |
| 1756 | // FIXME: magic numbers, bleech |
| 1757 | padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1; |
| 1758 | |
| 1759 | if ((!skb_cloned (skb)) |
| 1760 | && ((headroom + tailroom) >= (padlen + (4 + 4*1)))) { |
| 1761 | if ((headroom < (4 + 4*1)) || (tailroom < padlen)) { |
| 1762 | skb->data = memmove (skb->head + (4 + 4*1), |
| 1763 | skb->data, skb->len); |
| 1764 | skb->tail = skb->data + skb->len; |
| 1765 | } |
| 1766 | } else { |
| 1767 | struct sk_buff *skb2; |
| 1768 | skb2 = skb_copy_expand (skb, (4 + 4*1) , padlen, flags); |
| 1769 | dev_kfree_skb_any (skb); |
| 1770 | skb = skb2; |
| 1771 | if (!skb) |
| 1772 | return NULL; |
| 1773 | } |
| 1774 | |
| 1775 | // attach the packet count to the header |
| 1776 | packet_count = (u32 *) skb_push (skb, (4 + 4*1)); |
| 1777 | packet_len = packet_count + 1; |
| 1778 | |
| 1779 | // FIXME little endian? |
| 1780 | *packet_count = 1; |
| 1781 | *packet_len = length; |
| 1782 | |
| 1783 | // add padding byte |
| 1784 | if ((skb->len % dev->maxpacket) == 0) |
| 1785 | skb_put (skb, 1); |
| 1786 | |
| 1787 | return skb; |
| 1788 | } |
| 1789 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1790 | static int genelink_bind (struct usbnet *dev, struct usb_interface *intf) |
| 1791 | { |
| 1792 | dev->hard_mtu = GL_RCV_BUF_SIZE; |
| 1793 | dev->net->hard_header_len += 4; |
| 1794 | return 0; |
| 1795 | } |
| 1796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | static const struct driver_info genelink_info = { |
| 1798 | .description = "Genesys GeneLink", |
| 1799 | .flags = FLAG_FRAMING_GL | FLAG_NO_SETINT, |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1800 | .bind = genelink_bind, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | .rx_fixup = genelink_rx_fixup, |
| 1802 | .tx_fixup = genelink_tx_fixup, |
| 1803 | |
| 1804 | .in = 1, .out = 2, |
| 1805 | |
| 1806 | #ifdef GENELINK_ACK |
| 1807 | .check_connect =genelink_check_connect, |
| 1808 | #endif |
| 1809 | }; |
| 1810 | |
| 1811 | #endif /* CONFIG_USB_GENESYS */ |
| 1812 | |
| 1813 | |
| 1814 | |
| 1815 | #ifdef CONFIG_USB_NET1080 |
| 1816 | #define HAVE_HARDWARE |
| 1817 | |
| 1818 | /*------------------------------------------------------------------------- |
| 1819 | * |
| 1820 | * Netchip 1080 driver ... http://www.netchip.com |
| 1821 | * Used in LapLink cables |
| 1822 | * |
| 1823 | *-------------------------------------------------------------------------*/ |
| 1824 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | #define frame_errors data[1] |
| 1826 | |
| 1827 | /* |
| 1828 | * NetChip framing of ethernet packets, supporting additional error |
| 1829 | * checks for links that may drop bulk packets from inside messages. |
| 1830 | * Odd USB length == always short read for last usb packet. |
| 1831 | * - nc_header |
| 1832 | * - Ethernet header (14 bytes) |
| 1833 | * - payload |
| 1834 | * - (optional padding byte, if needed so length becomes odd) |
| 1835 | * - nc_trailer |
| 1836 | * |
| 1837 | * This framing is to be avoided for non-NetChip devices. |
| 1838 | */ |
| 1839 | |
| 1840 | struct nc_header { // packed: |
| 1841 | __le16 hdr_len; // sizeof nc_header (LE, all) |
| 1842 | __le16 packet_len; // payload size (including ethhdr) |
| 1843 | __le16 packet_id; // detects dropped packets |
| 1844 | #define MIN_HEADER 6 |
| 1845 | |
| 1846 | // all else is optional, and must start with: |
| 1847 | // u16 vendorId; // from usb-if |
| 1848 | // u16 productId; |
| 1849 | } __attribute__((__packed__)); |
| 1850 | |
| 1851 | #define PAD_BYTE ((unsigned char)0xAC) |
| 1852 | |
| 1853 | struct nc_trailer { |
| 1854 | __le16 packet_id; |
| 1855 | } __attribute__((__packed__)); |
| 1856 | |
| 1857 | // packets may use FLAG_FRAMING_NC and optional pad |
| 1858 | #define FRAMED_SIZE(mtu) (sizeof (struct nc_header) \ |
| 1859 | + sizeof (struct ethhdr) \ |
| 1860 | + (mtu) \ |
| 1861 | + 1 \ |
| 1862 | + sizeof (struct nc_trailer)) |
| 1863 | |
| 1864 | #define MIN_FRAMED FRAMED_SIZE(0) |
| 1865 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1866 | /* packets _could_ be up to 64KB... */ |
| 1867 | #define NC_MAX_PACKET 32767 |
| 1868 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | |
| 1870 | /* |
| 1871 | * Zero means no timeout; else, how long a 64 byte bulk packet may be queued |
| 1872 | * before the hardware drops it. If that's done, the driver will need to |
| 1873 | * frame network packets to guard against the dropped USB packets. The win32 |
| 1874 | * driver sets this for both sides of the link. |
| 1875 | */ |
| 1876 | #define NC_READ_TTL_MS ((u8)255) // ms |
| 1877 | |
| 1878 | /* |
| 1879 | * We ignore most registers and EEPROM contents. |
| 1880 | */ |
| 1881 | #define REG_USBCTL ((u8)0x04) |
| 1882 | #define REG_TTL ((u8)0x10) |
| 1883 | #define REG_STATUS ((u8)0x11) |
| 1884 | |
| 1885 | /* |
| 1886 | * Vendor specific requests to read/write data |
| 1887 | */ |
| 1888 | #define REQUEST_REGISTER ((u8)0x10) |
| 1889 | #define REQUEST_EEPROM ((u8)0x11) |
| 1890 | |
| 1891 | static int |
| 1892 | nc_vendor_read (struct usbnet *dev, u8 req, u8 regnum, u16 *retval_ptr) |
| 1893 | { |
| 1894 | int status = usb_control_msg (dev->udev, |
| 1895 | usb_rcvctrlpipe (dev->udev, 0), |
| 1896 | req, |
| 1897 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 1898 | 0, regnum, |
| 1899 | retval_ptr, sizeof *retval_ptr, |
| 1900 | CONTROL_TIMEOUT_MS); |
| 1901 | if (status > 0) |
| 1902 | status = 0; |
| 1903 | if (!status) |
| 1904 | le16_to_cpus (retval_ptr); |
| 1905 | return status; |
| 1906 | } |
| 1907 | |
| 1908 | static inline int |
| 1909 | nc_register_read (struct usbnet *dev, u8 regnum, u16 *retval_ptr) |
| 1910 | { |
| 1911 | return nc_vendor_read (dev, REQUEST_REGISTER, regnum, retval_ptr); |
| 1912 | } |
| 1913 | |
| 1914 | // no retval ... can become async, usable in_interrupt() |
| 1915 | static void |
| 1916 | nc_vendor_write (struct usbnet *dev, u8 req, u8 regnum, u16 value) |
| 1917 | { |
| 1918 | usb_control_msg (dev->udev, |
| 1919 | usb_sndctrlpipe (dev->udev, 0), |
| 1920 | req, |
| 1921 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 1922 | value, regnum, |
| 1923 | NULL, 0, // data is in setup packet |
| 1924 | CONTROL_TIMEOUT_MS); |
| 1925 | } |
| 1926 | |
| 1927 | static inline void |
| 1928 | nc_register_write (struct usbnet *dev, u8 regnum, u16 value) |
| 1929 | { |
| 1930 | nc_vendor_write (dev, REQUEST_REGISTER, regnum, value); |
| 1931 | } |
| 1932 | |
| 1933 | |
| 1934 | #if 0 |
| 1935 | static void nc_dump_registers (struct usbnet *dev) |
| 1936 | { |
| 1937 | u8 reg; |
| 1938 | u16 *vp = kmalloc (sizeof (u16)); |
| 1939 | |
| 1940 | if (!vp) { |
| 1941 | dbg ("no memory?"); |
| 1942 | return; |
| 1943 | } |
| 1944 | |
| 1945 | dbg ("%s registers:", dev->net->name); |
| 1946 | for (reg = 0; reg < 0x20; reg++) { |
| 1947 | int retval; |
| 1948 | |
| 1949 | // reading some registers is trouble |
| 1950 | if (reg >= 0x08 && reg <= 0xf) |
| 1951 | continue; |
| 1952 | if (reg >= 0x12 && reg <= 0x1e) |
| 1953 | continue; |
| 1954 | |
| 1955 | retval = nc_register_read (dev, reg, vp); |
| 1956 | if (retval < 0) |
| 1957 | dbg ("%s reg [0x%x] ==> error %d", |
| 1958 | dev->net->name, reg, retval); |
| 1959 | else |
| 1960 | dbg ("%s reg [0x%x] = 0x%x", |
| 1961 | dev->net->name, reg, *vp); |
| 1962 | } |
| 1963 | kfree (vp); |
| 1964 | } |
| 1965 | #endif |
| 1966 | |
| 1967 | |
| 1968 | /*-------------------------------------------------------------------------*/ |
| 1969 | |
| 1970 | /* |
| 1971 | * Control register |
| 1972 | */ |
| 1973 | |
| 1974 | #define USBCTL_WRITABLE_MASK 0x1f0f |
| 1975 | // bits 15-13 reserved, r/o |
| 1976 | #define USBCTL_ENABLE_LANG (1 << 12) |
| 1977 | #define USBCTL_ENABLE_MFGR (1 << 11) |
| 1978 | #define USBCTL_ENABLE_PROD (1 << 10) |
| 1979 | #define USBCTL_ENABLE_SERIAL (1 << 9) |
| 1980 | #define USBCTL_ENABLE_DEFAULTS (1 << 8) |
| 1981 | // bits 7-4 reserved, r/o |
| 1982 | #define USBCTL_FLUSH_OTHER (1 << 3) |
| 1983 | #define USBCTL_FLUSH_THIS (1 << 2) |
| 1984 | #define USBCTL_DISCONN_OTHER (1 << 1) |
| 1985 | #define USBCTL_DISCONN_THIS (1 << 0) |
| 1986 | |
| 1987 | static inline void nc_dump_usbctl (struct usbnet *dev, u16 usbctl) |
| 1988 | { |
| 1989 | if (!netif_msg_link (dev)) |
| 1990 | return; |
| 1991 | devdbg (dev, "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s;" |
| 1992 | " this%s%s;" |
| 1993 | " other%s%s; r/o 0x%x", |
| 1994 | dev->udev->bus->bus_name, dev->udev->devpath, |
| 1995 | usbctl, |
| 1996 | (usbctl & USBCTL_ENABLE_LANG) ? " lang" : "", |
| 1997 | (usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "", |
| 1998 | (usbctl & USBCTL_ENABLE_PROD) ? " prod" : "", |
| 1999 | (usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "", |
| 2000 | (usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "", |
| 2001 | |
| 2002 | (usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "", |
| 2003 | (usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "", |
| 2004 | (usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "", |
| 2005 | (usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "", |
| 2006 | usbctl & ~USBCTL_WRITABLE_MASK |
| 2007 | ); |
| 2008 | } |
| 2009 | |
| 2010 | /*-------------------------------------------------------------------------*/ |
| 2011 | |
| 2012 | /* |
| 2013 | * Status register |
| 2014 | */ |
| 2015 | |
| 2016 | #define STATUS_PORT_A (1 << 15) |
| 2017 | |
| 2018 | #define STATUS_CONN_OTHER (1 << 14) |
| 2019 | #define STATUS_SUSPEND_OTHER (1 << 13) |
| 2020 | #define STATUS_MAILBOX_OTHER (1 << 12) |
| 2021 | #define STATUS_PACKETS_OTHER(n) (((n) >> 8) && 0x03) |
| 2022 | |
| 2023 | #define STATUS_CONN_THIS (1 << 6) |
| 2024 | #define STATUS_SUSPEND_THIS (1 << 5) |
| 2025 | #define STATUS_MAILBOX_THIS (1 << 4) |
| 2026 | #define STATUS_PACKETS_THIS(n) (((n) >> 0) && 0x03) |
| 2027 | |
| 2028 | #define STATUS_UNSPEC_MASK 0x0c8c |
| 2029 | #define STATUS_NOISE_MASK ((u16)~(0x0303|STATUS_UNSPEC_MASK)) |
| 2030 | |
| 2031 | |
| 2032 | static inline void nc_dump_status (struct usbnet *dev, u16 status) |
| 2033 | { |
| 2034 | if (!netif_msg_link (dev)) |
| 2035 | return; |
| 2036 | devdbg (dev, "net1080 %s-%s status 0x%x:" |
| 2037 | " this (%c) PKT=%d%s%s%s;" |
| 2038 | " other PKT=%d%s%s%s; unspec 0x%x", |
| 2039 | dev->udev->bus->bus_name, dev->udev->devpath, |
| 2040 | status, |
| 2041 | |
| 2042 | // XXX the packet counts don't seem right |
| 2043 | // (1 at reset, not 0); maybe UNSPEC too |
| 2044 | |
| 2045 | (status & STATUS_PORT_A) ? 'A' : 'B', |
| 2046 | STATUS_PACKETS_THIS (status), |
| 2047 | (status & STATUS_CONN_THIS) ? " CON" : "", |
| 2048 | (status & STATUS_SUSPEND_THIS) ? " SUS" : "", |
| 2049 | (status & STATUS_MAILBOX_THIS) ? " MBOX" : "", |
| 2050 | |
| 2051 | STATUS_PACKETS_OTHER (status), |
| 2052 | (status & STATUS_CONN_OTHER) ? " CON" : "", |
| 2053 | (status & STATUS_SUSPEND_OTHER) ? " SUS" : "", |
| 2054 | (status & STATUS_MAILBOX_OTHER) ? " MBOX" : "", |
| 2055 | |
| 2056 | status & STATUS_UNSPEC_MASK |
| 2057 | ); |
| 2058 | } |
| 2059 | |
| 2060 | /*-------------------------------------------------------------------------*/ |
| 2061 | |
| 2062 | /* |
| 2063 | * TTL register |
| 2064 | */ |
| 2065 | |
| 2066 | #define TTL_THIS(ttl) (0x00ff & ttl) |
| 2067 | #define TTL_OTHER(ttl) (0x00ff & (ttl >> 8)) |
| 2068 | #define MK_TTL(this,other) ((u16)(((other)<<8)|(0x00ff&(this)))) |
| 2069 | |
| 2070 | static inline void nc_dump_ttl (struct usbnet *dev, u16 ttl) |
| 2071 | { |
| 2072 | if (netif_msg_link (dev)) |
| 2073 | devdbg (dev, "net1080 %s-%s ttl 0x%x this = %d, other = %d", |
| 2074 | dev->udev->bus->bus_name, dev->udev->devpath, |
| 2075 | ttl, TTL_THIS (ttl), TTL_OTHER (ttl)); |
| 2076 | } |
| 2077 | |
| 2078 | /*-------------------------------------------------------------------------*/ |
| 2079 | |
| 2080 | static int net1080_reset (struct usbnet *dev) |
| 2081 | { |
| 2082 | u16 usbctl, status, ttl; |
| 2083 | u16 *vp = kmalloc (sizeof (u16), GFP_KERNEL); |
| 2084 | int retval; |
| 2085 | |
| 2086 | if (!vp) |
| 2087 | return -ENOMEM; |
| 2088 | |
| 2089 | // nc_dump_registers (dev); |
| 2090 | |
| 2091 | if ((retval = nc_register_read (dev, REG_STATUS, vp)) < 0) { |
| 2092 | dbg ("can't read %s-%s status: %d", |
| 2093 | dev->udev->bus->bus_name, dev->udev->devpath, retval); |
| 2094 | goto done; |
| 2095 | } |
| 2096 | status = *vp; |
| 2097 | nc_dump_status (dev, status); |
| 2098 | |
| 2099 | if ((retval = nc_register_read (dev, REG_USBCTL, vp)) < 0) { |
| 2100 | dbg ("can't read USBCTL, %d", retval); |
| 2101 | goto done; |
| 2102 | } |
| 2103 | usbctl = *vp; |
| 2104 | nc_dump_usbctl (dev, usbctl); |
| 2105 | |
| 2106 | nc_register_write (dev, REG_USBCTL, |
| 2107 | USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER); |
| 2108 | |
| 2109 | if ((retval = nc_register_read (dev, REG_TTL, vp)) < 0) { |
| 2110 | dbg ("can't read TTL, %d", retval); |
| 2111 | goto done; |
| 2112 | } |
| 2113 | ttl = *vp; |
| 2114 | // nc_dump_ttl (dev, ttl); |
| 2115 | |
| 2116 | nc_register_write (dev, REG_TTL, |
| 2117 | MK_TTL (NC_READ_TTL_MS, TTL_OTHER (ttl)) ); |
| 2118 | dbg ("%s: assigned TTL, %d ms", dev->net->name, NC_READ_TTL_MS); |
| 2119 | |
| 2120 | if (netif_msg_link (dev)) |
| 2121 | devinfo (dev, "port %c, peer %sconnected", |
| 2122 | (status & STATUS_PORT_A) ? 'A' : 'B', |
| 2123 | (status & STATUS_CONN_OTHER) ? "" : "dis" |
| 2124 | ); |
| 2125 | retval = 0; |
| 2126 | |
| 2127 | done: |
| 2128 | kfree (vp); |
| 2129 | return retval; |
| 2130 | } |
| 2131 | |
| 2132 | static int net1080_check_connect (struct usbnet *dev) |
| 2133 | { |
| 2134 | int retval; |
| 2135 | u16 status; |
| 2136 | u16 *vp = kmalloc (sizeof (u16), GFP_KERNEL); |
| 2137 | |
| 2138 | if (!vp) |
| 2139 | return -ENOMEM; |
| 2140 | retval = nc_register_read (dev, REG_STATUS, vp); |
| 2141 | status = *vp; |
| 2142 | kfree (vp); |
| 2143 | if (retval != 0) { |
| 2144 | dbg ("%s net1080_check_conn read - %d", dev->net->name, retval); |
| 2145 | return retval; |
| 2146 | } |
| 2147 | if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER) |
| 2148 | return -ENOLINK; |
| 2149 | return 0; |
| 2150 | } |
| 2151 | |
| 2152 | static void nc_flush_complete (struct urb *urb, struct pt_regs *regs) |
| 2153 | { |
| 2154 | kfree (urb->context); |
| 2155 | usb_free_urb(urb); |
| 2156 | } |
| 2157 | |
| 2158 | static void nc_ensure_sync (struct usbnet *dev) |
| 2159 | { |
| 2160 | dev->frame_errors++; |
| 2161 | if (dev->frame_errors > 5) { |
| 2162 | struct urb *urb; |
| 2163 | struct usb_ctrlrequest *req; |
| 2164 | int status; |
| 2165 | |
| 2166 | /* Send a flush */ |
| 2167 | urb = usb_alloc_urb (0, SLAB_ATOMIC); |
| 2168 | if (!urb) |
| 2169 | return; |
| 2170 | |
| 2171 | req = kmalloc (sizeof *req, GFP_ATOMIC); |
| 2172 | if (!req) { |
| 2173 | usb_free_urb (urb); |
| 2174 | return; |
| 2175 | } |
| 2176 | |
| 2177 | req->bRequestType = USB_DIR_OUT |
| 2178 | | USB_TYPE_VENDOR |
| 2179 | | USB_RECIP_DEVICE; |
| 2180 | req->bRequest = REQUEST_REGISTER; |
| 2181 | req->wValue = cpu_to_le16 (USBCTL_FLUSH_THIS |
| 2182 | | USBCTL_FLUSH_OTHER); |
| 2183 | req->wIndex = cpu_to_le16 (REG_USBCTL); |
| 2184 | req->wLength = cpu_to_le16 (0); |
| 2185 | |
| 2186 | /* queue an async control request, we don't need |
| 2187 | * to do anything when it finishes except clean up. |
| 2188 | */ |
| 2189 | usb_fill_control_urb (urb, dev->udev, |
| 2190 | usb_sndctrlpipe (dev->udev, 0), |
| 2191 | (unsigned char *) req, |
| 2192 | NULL, 0, |
| 2193 | nc_flush_complete, req); |
| 2194 | status = usb_submit_urb (urb, GFP_ATOMIC); |
| 2195 | if (status) { |
| 2196 | kfree (req); |
| 2197 | usb_free_urb (urb); |
| 2198 | return; |
| 2199 | } |
| 2200 | |
| 2201 | if (netif_msg_rx_err (dev)) |
| 2202 | devdbg (dev, "flush net1080; too many framing errors"); |
| 2203 | dev->frame_errors = 0; |
| 2204 | } |
| 2205 | } |
| 2206 | |
| 2207 | static int net1080_rx_fixup (struct usbnet *dev, struct sk_buff *skb) |
| 2208 | { |
| 2209 | struct nc_header *header; |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 2210 | struct net_device *net = dev->net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | struct nc_trailer *trailer; |
| 2212 | u16 hdr_len, packet_len; |
| 2213 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 2214 | if (!(skb->len & 0x01)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | dev->stats.rx_frame_errors++; |
| 2216 | dbg ("rx framesize %d range %d..%d mtu %d", skb->len, |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 2217 | net->hard_header_len, dev->hard_mtu, net->mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | nc_ensure_sync (dev); |
| 2219 | return 0; |
| 2220 | } |
| 2221 | |
| 2222 | header = (struct nc_header *) skb->data; |
| 2223 | hdr_len = le16_to_cpup (&header->hdr_len); |
| 2224 | packet_len = le16_to_cpup (&header->packet_len); |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 2225 | if (FRAMED_SIZE (packet_len) > NC_MAX_PACKET) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | dev->stats.rx_frame_errors++; |
| 2227 | dbg ("packet too big, %d", packet_len); |
| 2228 | nc_ensure_sync (dev); |
| 2229 | return 0; |
| 2230 | } else if (hdr_len < MIN_HEADER) { |
| 2231 | dev->stats.rx_frame_errors++; |
| 2232 | dbg ("header too short, %d", hdr_len); |
| 2233 | nc_ensure_sync (dev); |
| 2234 | return 0; |
| 2235 | } else if (hdr_len > MIN_HEADER) { |
| 2236 | // out of band data for us? |
| 2237 | dbg ("header OOB, %d bytes", hdr_len - MIN_HEADER); |
| 2238 | nc_ensure_sync (dev); |
| 2239 | // switch (vendor/product ids) { ... } |
| 2240 | } |
| 2241 | skb_pull (skb, hdr_len); |
| 2242 | |
| 2243 | trailer = (struct nc_trailer *) |
| 2244 | (skb->data + skb->len - sizeof *trailer); |
| 2245 | skb_trim (skb, skb->len - sizeof *trailer); |
| 2246 | |
| 2247 | if ((packet_len & 0x01) == 0) { |
| 2248 | if (skb->data [packet_len] != PAD_BYTE) { |
| 2249 | dev->stats.rx_frame_errors++; |
| 2250 | dbg ("bad pad"); |
| 2251 | return 0; |
| 2252 | } |
| 2253 | skb_trim (skb, skb->len - 1); |
| 2254 | } |
| 2255 | if (skb->len != packet_len) { |
| 2256 | dev->stats.rx_frame_errors++; |
| 2257 | dbg ("bad packet len %d (expected %d)", |
| 2258 | skb->len, packet_len); |
| 2259 | nc_ensure_sync (dev); |
| 2260 | return 0; |
| 2261 | } |
| 2262 | if (header->packet_id != get_unaligned (&trailer->packet_id)) { |
| 2263 | dev->stats.rx_fifo_errors++; |
| 2264 | dbg ("(2+ dropped) rx packet_id mismatch 0x%x 0x%x", |
| 2265 | le16_to_cpu (header->packet_id), |
| 2266 | le16_to_cpu (trailer->packet_id)); |
| 2267 | return 0; |
| 2268 | } |
| 2269 | #if 0 |
| 2270 | devdbg (dev, "frame <rx h %d p %d id %d", header->hdr_len, |
| 2271 | header->packet_len, header->packet_id); |
| 2272 | #endif |
| 2273 | dev->frame_errors = 0; |
| 2274 | return 1; |
| 2275 | } |
| 2276 | |
| 2277 | static struct sk_buff * |
david-b@pacbell.net | 0d9899f | 2005-07-28 20:46:32 -0700 | [diff] [blame] | 2278 | net1080_tx_fixup (struct usbnet *dev, struct sk_buff *skb, unsigned flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | { |
| 2280 | int padlen; |
| 2281 | struct sk_buff *skb2; |
| 2282 | |
| 2283 | padlen = ((skb->len + sizeof (struct nc_header) |
| 2284 | + sizeof (struct nc_trailer)) & 0x01) ? 0 : 1; |
| 2285 | if (!skb_cloned (skb)) { |
| 2286 | int headroom = skb_headroom (skb); |
| 2287 | int tailroom = skb_tailroom (skb); |
| 2288 | |
| 2289 | if ((padlen + sizeof (struct nc_trailer)) <= tailroom |
| 2290 | && sizeof (struct nc_header) <= headroom) |
| 2291 | /* There's enough head and tail room */ |
| 2292 | return skb; |
| 2293 | |
| 2294 | if ((sizeof (struct nc_header) + padlen |
| 2295 | + sizeof (struct nc_trailer)) < |
| 2296 | (headroom + tailroom)) { |
| 2297 | /* There's enough total room, so just readjust */ |
| 2298 | skb->data = memmove (skb->head |
| 2299 | + sizeof (struct nc_header), |
| 2300 | skb->data, skb->len); |
| 2301 | skb->tail = skb->data + skb->len; |
| 2302 | return skb; |
| 2303 | } |
| 2304 | } |
| 2305 | |
| 2306 | /* Create a new skb to use with the correct size */ |
| 2307 | skb2 = skb_copy_expand (skb, |
| 2308 | sizeof (struct nc_header), |
| 2309 | sizeof (struct nc_trailer) + padlen, |
| 2310 | flags); |
| 2311 | dev_kfree_skb_any (skb); |
| 2312 | return skb2; |
| 2313 | } |
| 2314 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 2315 | static int net1080_bind (struct usbnet *dev, struct usb_interface *intf) |
| 2316 | { |
| 2317 | unsigned extra = sizeof (struct nc_header) |
| 2318 | + 1 |
| 2319 | + sizeof (struct nc_trailer); |
| 2320 | |
| 2321 | dev->net->hard_header_len += extra; |
| 2322 | dev->hard_mtu = NC_MAX_PACKET; |
| 2323 | return 0; |
| 2324 | } |
| 2325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2326 | static const struct driver_info net1080_info = { |
| 2327 | .description = "NetChip TurboCONNECT", |
| 2328 | .flags = FLAG_FRAMING_NC, |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 2329 | .bind = net1080_bind, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | .reset = net1080_reset, |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 2331 | .check_connect = net1080_check_connect, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | .rx_fixup = net1080_rx_fixup, |
| 2333 | .tx_fixup = net1080_tx_fixup, |
| 2334 | }; |
| 2335 | |
| 2336 | #endif /* CONFIG_USB_NET1080 */ |
| 2337 | |
| 2338 | |
| 2339 | |
| 2340 | #ifdef CONFIG_USB_PL2301 |
| 2341 | #define HAVE_HARDWARE |
| 2342 | |
| 2343 | /*------------------------------------------------------------------------- |
| 2344 | * |
| 2345 | * Prolific PL-2301/PL-2302 driver ... http://www.prolifictech.com |
| 2346 | * |
| 2347 | * The protocol and handshaking used here should be bug-compatible |
| 2348 | * with the Linux 2.2 "plusb" driver, by Deti Fliegl. |
| 2349 | * |
| 2350 | *-------------------------------------------------------------------------*/ |
| 2351 | |
| 2352 | /* |
| 2353 | * Bits 0-4 can be used for software handshaking; they're set from |
| 2354 | * one end, cleared from the other, "read" with the interrupt byte. |
| 2355 | */ |
| 2356 | #define PL_S_EN (1<<7) /* (feature only) suspend enable */ |
| 2357 | /* reserved bit -- rx ready (6) ? */ |
| 2358 | #define PL_TX_READY (1<<5) /* (interrupt only) transmit ready */ |
| 2359 | #define PL_RESET_OUT (1<<4) /* reset output pipe */ |
| 2360 | #define PL_RESET_IN (1<<3) /* reset input pipe */ |
| 2361 | #define PL_TX_C (1<<2) /* transmission complete */ |
| 2362 | #define PL_TX_REQ (1<<1) /* transmission received */ |
| 2363 | #define PL_PEER_E (1<<0) /* peer exists */ |
| 2364 | |
| 2365 | static inline int |
| 2366 | pl_vendor_req (struct usbnet *dev, u8 req, u8 val, u8 index) |
| 2367 | { |
| 2368 | return usb_control_msg (dev->udev, |
| 2369 | usb_rcvctrlpipe (dev->udev, 0), |
| 2370 | req, |
| 2371 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 2372 | val, index, |
| 2373 | NULL, 0, |
| 2374 | CONTROL_TIMEOUT_MS); |
| 2375 | } |
| 2376 | |
| 2377 | static inline int |
| 2378 | pl_clear_QuickLink_features (struct usbnet *dev, int val) |
| 2379 | { |
| 2380 | return pl_vendor_req (dev, 1, (u8) val, 0); |
| 2381 | } |
| 2382 | |
| 2383 | static inline int |
| 2384 | pl_set_QuickLink_features (struct usbnet *dev, int val) |
| 2385 | { |
| 2386 | return pl_vendor_req (dev, 3, (u8) val, 0); |
| 2387 | } |
| 2388 | |
| 2389 | /*-------------------------------------------------------------------------*/ |
| 2390 | |
| 2391 | static int pl_reset (struct usbnet *dev) |
| 2392 | { |
| 2393 | /* some units seem to need this reset, others reject it utterly. |
| 2394 | * FIXME be more like "naplink" or windows drivers. |
| 2395 | */ |
| 2396 | (void) pl_set_QuickLink_features (dev, |
| 2397 | PL_S_EN|PL_RESET_OUT|PL_RESET_IN|PL_PEER_E); |
| 2398 | return 0; |
| 2399 | } |
| 2400 | |
| 2401 | static const struct driver_info prolific_info = { |
| 2402 | .description = "Prolific PL-2301/PL-2302", |
| 2403 | .flags = FLAG_NO_SETINT, |
| 2404 | /* some PL-2302 versions seem to fail usb_set_interface() */ |
| 2405 | .reset = pl_reset, |
| 2406 | }; |
| 2407 | |
| 2408 | #endif /* CONFIG_USB_PL2301 */ |
| 2409 | |
| 2410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | #ifdef CONFIG_USB_ZAURUS |
| 2412 | #define HAVE_HARDWARE |
| 2413 | |
| 2414 | #include <linux/crc32.h> |
| 2415 | |
| 2416 | /*------------------------------------------------------------------------- |
| 2417 | * |
| 2418 | * Zaurus is also a SA-1110 based PDA, but one using a different driver |
| 2419 | * (and framing) for its USB slave/gadget controller than the case above. |
| 2420 | * |
| 2421 | * For the current version of that driver, the main way that framing is |
| 2422 | * nonstandard (also from perspective of the CDC ethernet model!) is a |
| 2423 | * crc32, added to help detect when some sa1100 usb-to-memory DMA errata |
| 2424 | * haven't been fully worked around. Also, all Zaurii use the same |
| 2425 | * default Ethernet address. |
| 2426 | * |
| 2427 | * PXA based models use the same framing, and also can't implement |
| 2428 | * set_interface properly. |
| 2429 | * |
| 2430 | * All known Zaurii lie about their standards conformance. Most lie by |
| 2431 | * saying they support CDC Ethernet. Some lie and say they support CDC |
| 2432 | * MDLM (as if for access to cell phone modems). Someone, please beat |
David Brownell | f3fae6e | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 2433 | * on Sharp (and other such vendors) for a while with a cluestick. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | * |
| 2435 | *-------------------------------------------------------------------------*/ |
| 2436 | |
| 2437 | static struct sk_buff * |
david-b@pacbell.net | 0d9899f | 2005-07-28 20:46:32 -0700 | [diff] [blame] | 2438 | zaurus_tx_fixup (struct usbnet *dev, struct sk_buff *skb, unsigned flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | { |
| 2440 | int padlen; |
| 2441 | struct sk_buff *skb2; |
| 2442 | |
| 2443 | padlen = 2; |
| 2444 | if (!skb_cloned (skb)) { |
| 2445 | int tailroom = skb_tailroom (skb); |
| 2446 | if ((padlen + 4) <= tailroom) |
| 2447 | goto done; |
| 2448 | } |
| 2449 | skb2 = skb_copy_expand (skb, 0, 4 + padlen, flags); |
| 2450 | dev_kfree_skb_any (skb); |
| 2451 | skb = skb2; |
| 2452 | if (skb) { |
| 2453 | u32 fcs; |
| 2454 | done: |
| 2455 | fcs = crc32_le (~0, skb->data, skb->len); |
| 2456 | fcs = ~fcs; |
| 2457 | |
| 2458 | *skb_put (skb, 1) = fcs & 0xff; |
| 2459 | *skb_put (skb, 1) = (fcs>> 8) & 0xff; |
| 2460 | *skb_put (skb, 1) = (fcs>>16) & 0xff; |
| 2461 | *skb_put (skb, 1) = (fcs>>24) & 0xff; |
| 2462 | } |
| 2463 | return skb; |
| 2464 | } |
| 2465 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 2466 | static int zaurus_bind (struct usbnet *dev, struct usb_interface *intf) |
| 2467 | { |
| 2468 | /* Belcarra's funky framing has other options; mostly |
| 2469 | * TRAILERS (!) with 4 bytes CRC, and maybe 2 pad bytes. |
| 2470 | */ |
| 2471 | dev->net->hard_header_len += 6; |
| 2472 | return generic_cdc_bind(dev, intf); |
| 2473 | } |
| 2474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2475 | static const struct driver_info zaurus_sl5x00_info = { |
| 2476 | .description = "Sharp Zaurus SL-5x00", |
| 2477 | .flags = FLAG_FRAMING_Z, |
| 2478 | .check_connect = always_connected, |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 2479 | .bind = zaurus_bind, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2480 | .unbind = cdc_unbind, |
| 2481 | .tx_fixup = zaurus_tx_fixup, |
| 2482 | }; |
| 2483 | #define ZAURUS_STRONGARM_INFO ((unsigned long)&zaurus_sl5x00_info) |
| 2484 | |
| 2485 | static const struct driver_info zaurus_pxa_info = { |
| 2486 | .description = "Sharp Zaurus, PXA-2xx based", |
| 2487 | .flags = FLAG_FRAMING_Z, |
| 2488 | .check_connect = always_connected, |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 2489 | .bind = zaurus_bind, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | .unbind = cdc_unbind, |
| 2491 | .tx_fixup = zaurus_tx_fixup, |
| 2492 | }; |
| 2493 | #define ZAURUS_PXA_INFO ((unsigned long)&zaurus_pxa_info) |
| 2494 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | static const struct driver_info olympus_mxl_info = { |
| 2496 | .description = "Olympus R1000", |
| 2497 | .flags = FLAG_FRAMING_Z, |
| 2498 | .check_connect = always_connected, |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 2499 | .bind = zaurus_bind, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | .unbind = cdc_unbind, |
| 2501 | .tx_fixup = zaurus_tx_fixup, |
| 2502 | }; |
| 2503 | #define OLYMPUS_MXL_INFO ((unsigned long)&olympus_mxl_info) |
| 2504 | |
David Brownell | f3fae6e | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 2505 | |
| 2506 | /* Some more recent products using Lineo/Belcarra code will wrongly claim |
| 2507 | * CDC MDLM conformance. They aren't conformant: data endpoints live |
| 2508 | * in the control interface, there's no data interface, and it's not used |
| 2509 | * to talk to a cell phone radio. But at least we can detect these two |
| 2510 | * pseudo-classes, rather than growing this product list with entries for |
| 2511 | * each new nonconformant product (sigh). |
| 2512 | */ |
| 2513 | static const u8 safe_guid[16] = { |
| 2514 | 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6, |
| 2515 | 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f, |
| 2516 | }; |
| 2517 | static const u8 blan_guid[16] = { |
| 2518 | 0x74, 0xf0, 0x3d, 0xbd, 0x1e, 0xc1, 0x44, 0x70, |
| 2519 | 0xa3, 0x67, 0x71, 0x34, 0xc9, 0xf5, 0x54, 0x37, |
| 2520 | }; |
| 2521 | |
| 2522 | static int blan_mdlm_bind (struct usbnet *dev, struct usb_interface *intf) |
| 2523 | { |
| 2524 | u8 *buf = intf->cur_altsetting->extra; |
| 2525 | int len = intf->cur_altsetting->extralen; |
| 2526 | struct usb_cdc_mdlm_desc *desc = NULL; |
| 2527 | struct usb_cdc_mdlm_detail_desc *detail = NULL; |
| 2528 | |
| 2529 | while (len > 3) { |
| 2530 | if (buf [1] != USB_DT_CS_INTERFACE) |
| 2531 | goto next_desc; |
| 2532 | |
| 2533 | /* use bDescriptorSubType, and just verify that we get a |
| 2534 | * "BLAN" (or "SAFE") descriptor. |
| 2535 | */ |
| 2536 | switch (buf [2]) { |
| 2537 | case USB_CDC_MDLM_TYPE: |
| 2538 | if (desc) { |
| 2539 | dev_dbg (&intf->dev, "extra MDLM\n"); |
| 2540 | goto bad_desc; |
| 2541 | } |
| 2542 | desc = (void *) buf; |
| 2543 | if (desc->bLength != sizeof *desc) { |
| 2544 | dev_dbg (&intf->dev, "MDLM len %u\n", |
| 2545 | desc->bLength); |
| 2546 | goto bad_desc; |
| 2547 | } |
| 2548 | /* expect bcdVersion 1.0, ignore */ |
| 2549 | if (memcmp(&desc->bGUID, blan_guid, 16) |
David Brownell | f4d340c | 2005-06-03 08:01:35 -0700 | [diff] [blame] | 2550 | && memcmp(&desc->bGUID, safe_guid, 16) ) { |
David Brownell | f3fae6e | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 2551 | /* hey, this one might _really_ be MDLM! */ |
| 2552 | dev_dbg (&intf->dev, "MDLM guid\n"); |
| 2553 | goto bad_desc; |
| 2554 | } |
| 2555 | break; |
| 2556 | case USB_CDC_MDLM_DETAIL_TYPE: |
| 2557 | if (detail) { |
| 2558 | dev_dbg (&intf->dev, "extra MDLM detail\n"); |
| 2559 | goto bad_desc; |
| 2560 | } |
| 2561 | detail = (void *) buf; |
| 2562 | switch (detail->bGuidDescriptorType) { |
| 2563 | case 0: /* "SAFE" */ |
| 2564 | if (detail->bLength != (sizeof *detail + 2)) |
| 2565 | goto bad_detail; |
| 2566 | break; |
| 2567 | case 1: /* "BLAN" */ |
| 2568 | if (detail->bLength != (sizeof *detail + 3)) |
| 2569 | goto bad_detail; |
| 2570 | break; |
| 2571 | default: |
| 2572 | goto bad_detail; |
| 2573 | } |
| 2574 | |
| 2575 | /* assuming we either noticed BLAN already, or will |
| 2576 | * find it soon, there are some data bytes here: |
| 2577 | * - bmNetworkCapabilities (unused) |
| 2578 | * - bmDataCapabilities (bits, see below) |
| 2579 | * - bPad (ignored, for PADAFTER -- BLAN-only) |
| 2580 | * bits are: |
| 2581 | * - 0x01 -- Zaurus framing (add CRC) |
David Brownell | 80615f8 | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 2582 | * - 0x02 -- PADBEFORE (CRC includes some padding) |
| 2583 | * - 0x04 -- PADAFTER (some padding after CRC) |
David Brownell | f3fae6e | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 2584 | * - 0x08 -- "fermat" packet mangling (for hw bugs) |
David Brownell | 80615f8 | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 2585 | * the PADBEFORE appears not to matter; we interop |
| 2586 | * with devices that use it and those that don't. |
David Brownell | f3fae6e | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 2587 | */ |
David Brownell | 80615f8 | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 2588 | if ((detail->bDetailData[1] & ~02) != 0x01) { |
David Brownell | f3fae6e | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 2589 | /* bmDataCapabilites == 0 would be fine too, |
| 2590 | * but framing is minidriver-coupled for now. |
| 2591 | */ |
| 2592 | bad_detail: |
| 2593 | dev_dbg (&intf->dev, |
| 2594 | "bad MDLM detail, %d %d %d\n", |
| 2595 | detail->bLength, |
| 2596 | detail->bDetailData[0], |
| 2597 | detail->bDetailData[2]); |
| 2598 | goto bad_desc; |
| 2599 | } |
| 2600 | break; |
| 2601 | } |
| 2602 | next_desc: |
| 2603 | len -= buf [0]; /* bLength */ |
| 2604 | buf += buf [0]; |
| 2605 | } |
| 2606 | |
| 2607 | if (!desc || !detail) { |
| 2608 | dev_dbg (&intf->dev, "missing cdc mdlm %s%sdescriptor\n", |
| 2609 | desc ? "" : "func ", |
| 2610 | detail ? "" : "detail "); |
| 2611 | goto bad_desc; |
| 2612 | } |
| 2613 | |
| 2614 | /* There's probably a CDC Ethernet descriptor there, but we can't |
| 2615 | * rely on the Ethernet address it provides since not all vendors |
| 2616 | * bother to make it unique. Likewise there's no point in tracking |
| 2617 | * of the CDC event notifications. |
| 2618 | */ |
| 2619 | return get_endpoints (dev, intf); |
| 2620 | |
| 2621 | bad_desc: |
| 2622 | dev_info (&dev->udev->dev, "unsupported MDLM descriptors\n"); |
| 2623 | return -ENODEV; |
| 2624 | } |
| 2625 | |
| 2626 | static const struct driver_info bogus_mdlm_info = { |
| 2627 | .description = "pseudo-MDLM (BLAN) device", |
| 2628 | .flags = FLAG_FRAMING_Z, |
| 2629 | .check_connect = always_connected, |
| 2630 | .tx_fixup = zaurus_tx_fixup, |
| 2631 | .bind = blan_mdlm_bind, |
| 2632 | }; |
| 2633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | #else |
| 2635 | |
| 2636 | /* blacklist all those devices */ |
| 2637 | #define ZAURUS_STRONGARM_INFO 0 |
| 2638 | #define ZAURUS_PXA_INFO 0 |
| 2639 | #define OLYMPUS_MXL_INFO 0 |
| 2640 | |
| 2641 | #endif |
| 2642 | |
| 2643 | |
| 2644 | /*------------------------------------------------------------------------- |
| 2645 | * |
| 2646 | * Network Device Driver (peer link to "Host Device", from USB host) |
| 2647 | * |
| 2648 | *-------------------------------------------------------------------------*/ |
| 2649 | |
| 2650 | static int usbnet_change_mtu (struct net_device *net, int new_mtu) |
| 2651 | { |
| 2652 | struct usbnet *dev = netdev_priv(net); |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 2653 | int ll_mtu = new_mtu + net->hard_header_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2654 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 2655 | if (new_mtu <= 0 || ll_mtu > dev->hard_mtu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2656 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2657 | // no second zero-length packet read wanted after mtu-sized packets |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 2658 | if ((ll_mtu % dev->maxpacket) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | return -EDOM; |
| 2660 | net->mtu = new_mtu; |
| 2661 | return 0; |
| 2662 | } |
| 2663 | |
| 2664 | /*-------------------------------------------------------------------------*/ |
| 2665 | |
| 2666 | static struct net_device_stats *usbnet_get_stats (struct net_device *net) |
| 2667 | { |
| 2668 | struct usbnet *dev = netdev_priv(net); |
| 2669 | return &dev->stats; |
| 2670 | } |
| 2671 | |
| 2672 | /*-------------------------------------------------------------------------*/ |
| 2673 | |
| 2674 | /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from |
| 2675 | * completion callbacks. 2.5 should have fixed those bugs... |
| 2676 | */ |
| 2677 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2678 | static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | unsigned long flags; |
| 2681 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2682 | spin_lock_irqsave(&list->lock, flags); |
| 2683 | __skb_unlink(skb, list); |
| 2684 | spin_unlock(&list->lock); |
| 2685 | spin_lock(&dev->done.lock); |
| 2686 | __skb_queue_tail(&dev->done, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | if (dev->done.qlen == 1) |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2688 | tasklet_schedule(&dev->bh); |
| 2689 | spin_unlock_irqrestore(&dev->done.lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | /* some work can't be done in tasklets, so we use keventd |
| 2693 | * |
| 2694 | * NOTE: annoying asymmetry: if it's active, schedule_work() fails, |
| 2695 | * but tasklet_schedule() doesn't. hope the failure is rare. |
| 2696 | */ |
| 2697 | static void defer_kevent (struct usbnet *dev, int work) |
| 2698 | { |
| 2699 | set_bit (work, &dev->flags); |
| 2700 | if (!schedule_work (&dev->kevent)) |
| 2701 | deverr (dev, "kevent %d may have been dropped", work); |
| 2702 | else |
| 2703 | devdbg (dev, "kevent %d scheduled", work); |
| 2704 | } |
| 2705 | |
| 2706 | /*-------------------------------------------------------------------------*/ |
| 2707 | |
| 2708 | static void rx_complete (struct urb *urb, struct pt_regs *regs); |
| 2709 | |
david-b@pacbell.net | 0d9899f | 2005-07-28 20:46:32 -0700 | [diff] [blame] | 2710 | static void rx_submit (struct usbnet *dev, struct urb *urb, unsigned flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2711 | { |
| 2712 | struct sk_buff *skb; |
| 2713 | struct skb_data *entry; |
| 2714 | int retval = 0; |
| 2715 | unsigned long lockflags; |
| 2716 | size_t size; |
| 2717 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 2718 | size = max(dev->net->hard_header_len + dev->net->mtu, |
| 2719 | (unsigned)ETH_FRAME_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2720 | if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) { |
| 2721 | if (netif_msg_rx_err (dev)) |
| 2722 | devdbg (dev, "no rx skb"); |
| 2723 | defer_kevent (dev, EVENT_RX_MEMORY); |
| 2724 | usb_free_urb (urb); |
| 2725 | return; |
| 2726 | } |
| 2727 | skb_reserve (skb, NET_IP_ALIGN); |
| 2728 | |
| 2729 | entry = (struct skb_data *) skb->cb; |
| 2730 | entry->urb = urb; |
| 2731 | entry->dev = dev; |
| 2732 | entry->state = rx_start; |
| 2733 | entry->length = 0; |
| 2734 | |
| 2735 | usb_fill_bulk_urb (urb, dev->udev, dev->in, |
| 2736 | skb->data, size, rx_complete, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | |
| 2738 | spin_lock_irqsave (&dev->rxq.lock, lockflags); |
| 2739 | |
| 2740 | if (netif_running (dev->net) |
| 2741 | && netif_device_present (dev->net) |
| 2742 | && !test_bit (EVENT_RX_HALT, &dev->flags)) { |
| 2743 | switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){ |
| 2744 | case -EPIPE: |
| 2745 | defer_kevent (dev, EVENT_RX_HALT); |
| 2746 | break; |
| 2747 | case -ENOMEM: |
| 2748 | defer_kevent (dev, EVENT_RX_MEMORY); |
| 2749 | break; |
| 2750 | case -ENODEV: |
| 2751 | if (netif_msg_ifdown (dev)) |
| 2752 | devdbg (dev, "device gone"); |
| 2753 | netif_device_detach (dev->net); |
| 2754 | break; |
| 2755 | default: |
| 2756 | if (netif_msg_rx_err (dev)) |
| 2757 | devdbg (dev, "rx submit, %d", retval); |
| 2758 | tasklet_schedule (&dev->bh); |
| 2759 | break; |
| 2760 | case 0: |
| 2761 | __skb_queue_tail (&dev->rxq, skb); |
| 2762 | } |
| 2763 | } else { |
| 2764 | if (netif_msg_ifdown (dev)) |
| 2765 | devdbg (dev, "rx: stopped"); |
| 2766 | retval = -ENOLINK; |
| 2767 | } |
| 2768 | spin_unlock_irqrestore (&dev->rxq.lock, lockflags); |
| 2769 | if (retval) { |
| 2770 | dev_kfree_skb_any (skb); |
| 2771 | usb_free_urb (urb); |
| 2772 | } |
| 2773 | } |
| 2774 | |
| 2775 | |
| 2776 | /*-------------------------------------------------------------------------*/ |
| 2777 | |
| 2778 | static inline void rx_process (struct usbnet *dev, struct sk_buff *skb) |
| 2779 | { |
| 2780 | if (dev->driver_info->rx_fixup |
| 2781 | && !dev->driver_info->rx_fixup (dev, skb)) |
| 2782 | goto error; |
| 2783 | // else network stack removes extra byte if we forced a short packet |
| 2784 | |
| 2785 | if (skb->len) |
| 2786 | skb_return (dev, skb); |
| 2787 | else { |
| 2788 | if (netif_msg_rx_err (dev)) |
| 2789 | devdbg (dev, "drop"); |
| 2790 | error: |
| 2791 | dev->stats.rx_errors++; |
| 2792 | skb_queue_tail (&dev->done, skb); |
| 2793 | } |
| 2794 | } |
| 2795 | |
| 2796 | /*-------------------------------------------------------------------------*/ |
| 2797 | |
| 2798 | static void rx_complete (struct urb *urb, struct pt_regs *regs) |
| 2799 | { |
| 2800 | struct sk_buff *skb = (struct sk_buff *) urb->context; |
| 2801 | struct skb_data *entry = (struct skb_data *) skb->cb; |
| 2802 | struct usbnet *dev = entry->dev; |
| 2803 | int urb_status = urb->status; |
| 2804 | |
| 2805 | skb_put (skb, urb->actual_length); |
| 2806 | entry->state = rx_done; |
| 2807 | entry->urb = NULL; |
| 2808 | |
| 2809 | switch (urb_status) { |
| 2810 | // success |
| 2811 | case 0: |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 2812 | if (skb->len < dev->net->hard_header_len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | entry->state = rx_cleanup; |
| 2814 | dev->stats.rx_errors++; |
| 2815 | dev->stats.rx_length_errors++; |
| 2816 | if (netif_msg_rx_err (dev)) |
| 2817 | devdbg (dev, "rx length %d", skb->len); |
| 2818 | } |
| 2819 | break; |
| 2820 | |
| 2821 | // stalls need manual reset. this is rare ... except that |
| 2822 | // when going through USB 2.0 TTs, unplug appears this way. |
| 2823 | // we avoid the highspeed version of the ETIMEOUT/EILSEQ |
| 2824 | // storm, recovering as needed. |
| 2825 | case -EPIPE: |
| 2826 | dev->stats.rx_errors++; |
| 2827 | defer_kevent (dev, EVENT_RX_HALT); |
| 2828 | // FALLTHROUGH |
| 2829 | |
| 2830 | // software-driven interface shutdown |
| 2831 | case -ECONNRESET: // async unlink |
| 2832 | case -ESHUTDOWN: // hardware gone |
| 2833 | if (netif_msg_ifdown (dev)) |
| 2834 | devdbg (dev, "rx shutdown, code %d", urb_status); |
| 2835 | goto block; |
| 2836 | |
| 2837 | // we get controller i/o faults during khubd disconnect() delays. |
| 2838 | // throttle down resubmits, to avoid log floods; just temporarily, |
| 2839 | // so we still recover when the fault isn't a khubd delay. |
| 2840 | case -EPROTO: // ehci |
| 2841 | case -ETIMEDOUT: // ohci |
| 2842 | case -EILSEQ: // uhci |
| 2843 | dev->stats.rx_errors++; |
| 2844 | if (!timer_pending (&dev->delay)) { |
| 2845 | mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES); |
| 2846 | if (netif_msg_link (dev)) |
| 2847 | devdbg (dev, "rx throttle %d", urb_status); |
| 2848 | } |
| 2849 | block: |
| 2850 | entry->state = rx_cleanup; |
| 2851 | entry->urb = urb; |
| 2852 | urb = NULL; |
| 2853 | break; |
| 2854 | |
| 2855 | // data overrun ... flush fifo? |
| 2856 | case -EOVERFLOW: |
| 2857 | dev->stats.rx_over_errors++; |
| 2858 | // FALLTHROUGH |
| 2859 | |
| 2860 | default: |
| 2861 | entry->state = rx_cleanup; |
| 2862 | dev->stats.rx_errors++; |
| 2863 | if (netif_msg_rx_err (dev)) |
| 2864 | devdbg (dev, "rx status %d", urb_status); |
| 2865 | break; |
| 2866 | } |
| 2867 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2868 | defer_bh(dev, skb, &dev->rxq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2869 | |
| 2870 | if (urb) { |
| 2871 | if (netif_running (dev->net) |
| 2872 | && !test_bit (EVENT_RX_HALT, &dev->flags)) { |
| 2873 | rx_submit (dev, urb, GFP_ATOMIC); |
| 2874 | return; |
| 2875 | } |
| 2876 | usb_free_urb (urb); |
| 2877 | } |
| 2878 | if (netif_msg_rx_err (dev)) |
| 2879 | devdbg (dev, "no read resubmitted"); |
| 2880 | } |
| 2881 | |
| 2882 | static void intr_complete (struct urb *urb, struct pt_regs *regs) |
| 2883 | { |
| 2884 | struct usbnet *dev = urb->context; |
| 2885 | int status = urb->status; |
| 2886 | |
| 2887 | switch (status) { |
| 2888 | /* success */ |
| 2889 | case 0: |
| 2890 | dev->driver_info->status(dev, urb); |
| 2891 | break; |
| 2892 | |
| 2893 | /* software-driven interface shutdown */ |
| 2894 | case -ENOENT: // urb killed |
| 2895 | case -ESHUTDOWN: // hardware gone |
| 2896 | if (netif_msg_ifdown (dev)) |
| 2897 | devdbg (dev, "intr shutdown, code %d", status); |
| 2898 | return; |
| 2899 | |
| 2900 | /* NOTE: not throttling like RX/TX, since this endpoint |
| 2901 | * already polls infrequently |
| 2902 | */ |
| 2903 | default: |
| 2904 | devdbg (dev, "intr status %d", status); |
| 2905 | break; |
| 2906 | } |
| 2907 | |
| 2908 | if (!netif_running (dev->net)) |
| 2909 | return; |
| 2910 | |
| 2911 | memset(urb->transfer_buffer, 0, urb->transfer_buffer_length); |
| 2912 | status = usb_submit_urb (urb, GFP_ATOMIC); |
| 2913 | if (status != 0 && netif_msg_timer (dev)) |
| 2914 | deverr(dev, "intr resubmit --> %d", status); |
| 2915 | } |
| 2916 | |
| 2917 | /*-------------------------------------------------------------------------*/ |
| 2918 | |
| 2919 | // unlink pending rx/tx; completion handlers do all other cleanup |
| 2920 | |
| 2921 | static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q) |
| 2922 | { |
| 2923 | unsigned long flags; |
| 2924 | struct sk_buff *skb, *skbnext; |
| 2925 | int count = 0; |
| 2926 | |
| 2927 | spin_lock_irqsave (&q->lock, flags); |
| 2928 | for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) { |
| 2929 | struct skb_data *entry; |
| 2930 | struct urb *urb; |
| 2931 | int retval; |
| 2932 | |
| 2933 | entry = (struct skb_data *) skb->cb; |
| 2934 | urb = entry->urb; |
| 2935 | skbnext = skb->next; |
| 2936 | |
| 2937 | // during some PM-driven resume scenarios, |
| 2938 | // these (async) unlinks complete immediately |
| 2939 | retval = usb_unlink_urb (urb); |
| 2940 | if (retval != -EINPROGRESS && retval != 0) |
| 2941 | devdbg (dev, "unlink urb err, %d", retval); |
| 2942 | else |
| 2943 | count++; |
| 2944 | } |
| 2945 | spin_unlock_irqrestore (&q->lock, flags); |
| 2946 | return count; |
| 2947 | } |
| 2948 | |
| 2949 | |
| 2950 | /*-------------------------------------------------------------------------*/ |
| 2951 | |
| 2952 | // precondition: never called in_interrupt |
| 2953 | |
| 2954 | static int usbnet_stop (struct net_device *net) |
| 2955 | { |
| 2956 | struct usbnet *dev = netdev_priv(net); |
| 2957 | int temp; |
| 2958 | DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup); |
| 2959 | DECLARE_WAITQUEUE (wait, current); |
| 2960 | |
| 2961 | netif_stop_queue (net); |
| 2962 | |
| 2963 | if (netif_msg_ifdown (dev)) |
| 2964 | devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld", |
| 2965 | dev->stats.rx_packets, dev->stats.tx_packets, |
| 2966 | dev->stats.rx_errors, dev->stats.tx_errors |
| 2967 | ); |
| 2968 | |
| 2969 | // ensure there are no more active urbs |
| 2970 | add_wait_queue (&unlink_wakeup, &wait); |
| 2971 | dev->wait = &unlink_wakeup; |
| 2972 | temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq); |
| 2973 | |
| 2974 | // maybe wait for deletions to finish. |
David S. Miller | b03efcf | 2005-07-08 14:57:23 -0700 | [diff] [blame] | 2975 | while (!skb_queue_empty(&dev->rxq) && |
| 2976 | !skb_queue_empty(&dev->txq) && |
| 2977 | !skb_queue_empty(&dev->done)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2978 | msleep(UNLINK_TIMEOUT_MS); |
| 2979 | if (netif_msg_ifdown (dev)) |
| 2980 | devdbg (dev, "waited for %d urb completions", temp); |
| 2981 | } |
| 2982 | dev->wait = NULL; |
| 2983 | remove_wait_queue (&unlink_wakeup, &wait); |
| 2984 | |
| 2985 | usb_kill_urb(dev->interrupt); |
| 2986 | |
| 2987 | /* deferred work (task, timer, softirq) must also stop. |
| 2988 | * can't flush_scheduled_work() until we drop rtnl (later), |
| 2989 | * else workers could deadlock; so make workers a NOP. |
| 2990 | */ |
| 2991 | dev->flags = 0; |
| 2992 | del_timer_sync (&dev->delay); |
| 2993 | tasklet_kill (&dev->bh); |
| 2994 | |
| 2995 | return 0; |
| 2996 | } |
| 2997 | |
| 2998 | /*-------------------------------------------------------------------------*/ |
| 2999 | |
| 3000 | // posts reads, and enables write queuing |
| 3001 | |
| 3002 | // precondition: never called in_interrupt |
| 3003 | |
| 3004 | static int usbnet_open (struct net_device *net) |
| 3005 | { |
| 3006 | struct usbnet *dev = netdev_priv(net); |
| 3007 | int retval = 0; |
| 3008 | struct driver_info *info = dev->driver_info; |
| 3009 | |
| 3010 | // put into "known safe" state |
| 3011 | if (info->reset && (retval = info->reset (dev)) < 0) { |
| 3012 | if (netif_msg_ifup (dev)) |
| 3013 | devinfo (dev, |
| 3014 | "open reset fail (%d) usbnet usb-%s-%s, %s", |
| 3015 | retval, |
| 3016 | dev->udev->bus->bus_name, dev->udev->devpath, |
| 3017 | info->description); |
| 3018 | goto done; |
| 3019 | } |
| 3020 | |
| 3021 | // insist peer be connected |
| 3022 | if (info->check_connect && (retval = info->check_connect (dev)) < 0) { |
| 3023 | if (netif_msg_ifup (dev)) |
| 3024 | devdbg (dev, "can't open; %d", retval); |
| 3025 | goto done; |
| 3026 | } |
| 3027 | |
| 3028 | /* start any status interrupt transfer */ |
| 3029 | if (dev->interrupt) { |
| 3030 | retval = usb_submit_urb (dev->interrupt, GFP_KERNEL); |
| 3031 | if (retval < 0) { |
| 3032 | if (netif_msg_ifup (dev)) |
| 3033 | deverr (dev, "intr submit %d", retval); |
| 3034 | goto done; |
| 3035 | } |
| 3036 | } |
| 3037 | |
| 3038 | netif_start_queue (net); |
| 3039 | if (netif_msg_ifup (dev)) { |
| 3040 | char *framing; |
| 3041 | |
| 3042 | if (dev->driver_info->flags & FLAG_FRAMING_NC) |
| 3043 | framing = "NetChip"; |
| 3044 | else if (dev->driver_info->flags & FLAG_FRAMING_GL) |
| 3045 | framing = "GeneSys"; |
| 3046 | else if (dev->driver_info->flags & FLAG_FRAMING_Z) |
| 3047 | framing = "Zaurus"; |
| 3048 | else if (dev->driver_info->flags & FLAG_FRAMING_RN) |
| 3049 | framing = "RNDIS"; |
| 3050 | else if (dev->driver_info->flags & FLAG_FRAMING_AX) |
| 3051 | framing = "ASIX"; |
| 3052 | else |
| 3053 | framing = "simple"; |
| 3054 | |
| 3055 | devinfo (dev, "open: enable queueing " |
| 3056 | "(rx %d, tx %d) mtu %d %s framing", |
| 3057 | RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu, |
| 3058 | framing); |
| 3059 | } |
| 3060 | |
| 3061 | // delay posting reads until we're fully open |
| 3062 | tasklet_schedule (&dev->bh); |
| 3063 | done: |
| 3064 | return retval; |
| 3065 | } |
| 3066 | |
| 3067 | /*-------------------------------------------------------------------------*/ |
| 3068 | |
| 3069 | static void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info) |
| 3070 | { |
| 3071 | struct usbnet *dev = netdev_priv(net); |
| 3072 | |
| 3073 | strncpy (info->driver, driver_name, sizeof info->driver); |
| 3074 | strncpy (info->version, DRIVER_VERSION, sizeof info->version); |
| 3075 | strncpy (info->fw_version, dev->driver_info->description, |
| 3076 | sizeof info->fw_version); |
| 3077 | usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info); |
| 3078 | } |
| 3079 | |
| 3080 | static u32 usbnet_get_link (struct net_device *net) |
| 3081 | { |
| 3082 | struct usbnet *dev = netdev_priv(net); |
| 3083 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 3084 | /* If a check_connect is defined, return its result */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | if (dev->driver_info->check_connect) |
| 3086 | return dev->driver_info->check_connect (dev) == 0; |
| 3087 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 3088 | /* Otherwise, say we're up (to avoid breaking scripts) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | return 1; |
| 3090 | } |
| 3091 | |
| 3092 | static u32 usbnet_get_msglevel (struct net_device *net) |
| 3093 | { |
| 3094 | struct usbnet *dev = netdev_priv(net); |
| 3095 | |
| 3096 | return dev->msg_enable; |
| 3097 | } |
| 3098 | |
| 3099 | static void usbnet_set_msglevel (struct net_device *net, u32 level) |
| 3100 | { |
| 3101 | struct usbnet *dev = netdev_priv(net); |
| 3102 | |
| 3103 | dev->msg_enable = level; |
| 3104 | } |
| 3105 | |
| 3106 | static int usbnet_ioctl (struct net_device *net, struct ifreq *rq, int cmd) |
| 3107 | { |
| 3108 | #ifdef NEED_MII |
| 3109 | { |
| 3110 | struct usbnet *dev = netdev_priv(net); |
| 3111 | |
| 3112 | if (dev->mii.mdio_read != NULL && dev->mii.mdio_write != NULL) |
| 3113 | return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL); |
| 3114 | } |
| 3115 | #endif |
| 3116 | return -EOPNOTSUPP; |
| 3117 | } |
| 3118 | |
| 3119 | /*-------------------------------------------------------------------------*/ |
| 3120 | |
| 3121 | /* work that cannot be done in interrupt context uses keventd. |
| 3122 | * |
| 3123 | * NOTE: with 2.5 we could do more of this using completion callbacks, |
| 3124 | * especially now that control transfers can be queued. |
| 3125 | */ |
| 3126 | static void |
| 3127 | kevent (void *data) |
| 3128 | { |
| 3129 | struct usbnet *dev = data; |
| 3130 | int status; |
| 3131 | |
| 3132 | /* usb_clear_halt() needs a thread context */ |
| 3133 | if (test_bit (EVENT_TX_HALT, &dev->flags)) { |
| 3134 | unlink_urbs (dev, &dev->txq); |
| 3135 | status = usb_clear_halt (dev->udev, dev->out); |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 3136 | if (status < 0 |
| 3137 | && status != -EPIPE |
| 3138 | && status != -ESHUTDOWN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3139 | if (netif_msg_tx_err (dev)) |
| 3140 | deverr (dev, "can't clear tx halt, status %d", |
| 3141 | status); |
| 3142 | } else { |
| 3143 | clear_bit (EVENT_TX_HALT, &dev->flags); |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 3144 | if (status != -ESHUTDOWN) |
| 3145 | netif_wake_queue (dev->net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | } |
| 3147 | } |
| 3148 | if (test_bit (EVENT_RX_HALT, &dev->flags)) { |
| 3149 | unlink_urbs (dev, &dev->rxq); |
| 3150 | status = usb_clear_halt (dev->udev, dev->in); |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 3151 | if (status < 0 |
| 3152 | && status != -EPIPE |
| 3153 | && status != -ESHUTDOWN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3154 | if (netif_msg_rx_err (dev)) |
| 3155 | deverr (dev, "can't clear rx halt, status %d", |
| 3156 | status); |
| 3157 | } else { |
| 3158 | clear_bit (EVENT_RX_HALT, &dev->flags); |
| 3159 | tasklet_schedule (&dev->bh); |
| 3160 | } |
| 3161 | } |
| 3162 | |
| 3163 | /* tasklet could resubmit itself forever if memory is tight */ |
| 3164 | if (test_bit (EVENT_RX_MEMORY, &dev->flags)) { |
| 3165 | struct urb *urb = NULL; |
| 3166 | |
| 3167 | if (netif_running (dev->net)) |
| 3168 | urb = usb_alloc_urb (0, GFP_KERNEL); |
| 3169 | else |
| 3170 | clear_bit (EVENT_RX_MEMORY, &dev->flags); |
| 3171 | if (urb != NULL) { |
| 3172 | clear_bit (EVENT_RX_MEMORY, &dev->flags); |
| 3173 | rx_submit (dev, urb, GFP_KERNEL); |
| 3174 | tasklet_schedule (&dev->bh); |
| 3175 | } |
| 3176 | } |
| 3177 | |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 3178 | if (test_bit (EVENT_LINK_RESET, &dev->flags)) { |
| 3179 | struct driver_info *info = dev->driver_info; |
| 3180 | int retval = 0; |
| 3181 | |
| 3182 | clear_bit (EVENT_LINK_RESET, &dev->flags); |
| 3183 | if(info->link_reset && (retval = info->link_reset(dev)) < 0) { |
| 3184 | devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s", |
| 3185 | retval, |
| 3186 | dev->udev->bus->bus_name, dev->udev->devpath, |
| 3187 | info->description); |
| 3188 | } |
| 3189 | } |
| 3190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | if (dev->flags) |
| 3192 | devdbg (dev, "kevent done, flags = 0x%lx", |
| 3193 | dev->flags); |
| 3194 | } |
| 3195 | |
| 3196 | /*-------------------------------------------------------------------------*/ |
| 3197 | |
| 3198 | static void tx_complete (struct urb *urb, struct pt_regs *regs) |
| 3199 | { |
| 3200 | struct sk_buff *skb = (struct sk_buff *) urb->context; |
| 3201 | struct skb_data *entry = (struct skb_data *) skb->cb; |
| 3202 | struct usbnet *dev = entry->dev; |
| 3203 | |
| 3204 | if (urb->status == 0) { |
| 3205 | dev->stats.tx_packets++; |
| 3206 | dev->stats.tx_bytes += entry->length; |
| 3207 | } else { |
| 3208 | dev->stats.tx_errors++; |
| 3209 | |
| 3210 | switch (urb->status) { |
| 3211 | case -EPIPE: |
| 3212 | defer_kevent (dev, EVENT_TX_HALT); |
| 3213 | break; |
| 3214 | |
| 3215 | /* software-driven interface shutdown */ |
| 3216 | case -ECONNRESET: // async unlink |
| 3217 | case -ESHUTDOWN: // hardware gone |
| 3218 | break; |
| 3219 | |
| 3220 | // like rx, tx gets controller i/o faults during khubd delays |
| 3221 | // and so it uses the same throttling mechanism. |
| 3222 | case -EPROTO: // ehci |
| 3223 | case -ETIMEDOUT: // ohci |
| 3224 | case -EILSEQ: // uhci |
| 3225 | if (!timer_pending (&dev->delay)) { |
| 3226 | mod_timer (&dev->delay, |
| 3227 | jiffies + THROTTLE_JIFFIES); |
| 3228 | if (netif_msg_link (dev)) |
| 3229 | devdbg (dev, "tx throttle %d", |
| 3230 | urb->status); |
| 3231 | } |
| 3232 | netif_stop_queue (dev->net); |
| 3233 | break; |
| 3234 | default: |
| 3235 | if (netif_msg_tx_err (dev)) |
| 3236 | devdbg (dev, "tx err %d", entry->urb->status); |
| 3237 | break; |
| 3238 | } |
| 3239 | } |
| 3240 | |
| 3241 | urb->dev = NULL; |
| 3242 | entry->state = tx_done; |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 3243 | defer_bh(dev, skb, &dev->txq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | } |
| 3245 | |
| 3246 | /*-------------------------------------------------------------------------*/ |
| 3247 | |
| 3248 | static void usbnet_tx_timeout (struct net_device *net) |
| 3249 | { |
| 3250 | struct usbnet *dev = netdev_priv(net); |
| 3251 | |
| 3252 | unlink_urbs (dev, &dev->txq); |
| 3253 | tasklet_schedule (&dev->bh); |
| 3254 | |
| 3255 | // FIXME: device recovery -- reset? |
| 3256 | } |
| 3257 | |
| 3258 | /*-------------------------------------------------------------------------*/ |
| 3259 | |
| 3260 | static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net) |
| 3261 | { |
| 3262 | struct usbnet *dev = netdev_priv(net); |
| 3263 | int length; |
| 3264 | int retval = NET_XMIT_SUCCESS; |
| 3265 | struct urb *urb = NULL; |
| 3266 | struct skb_data *entry; |
| 3267 | struct driver_info *info = dev->driver_info; |
| 3268 | unsigned long flags; |
| 3269 | #ifdef CONFIG_USB_NET1080 |
| 3270 | struct nc_header *header = NULL; |
| 3271 | struct nc_trailer *trailer = NULL; |
| 3272 | #endif /* CONFIG_USB_NET1080 */ |
| 3273 | |
| 3274 | // some devices want funky USB-level framing, for |
| 3275 | // win32 driver (usually) and/or hardware quirks |
| 3276 | if (info->tx_fixup) { |
| 3277 | skb = info->tx_fixup (dev, skb, GFP_ATOMIC); |
| 3278 | if (!skb) { |
| 3279 | if (netif_msg_tx_err (dev)) |
| 3280 | devdbg (dev, "can't tx_fixup skb"); |
| 3281 | goto drop; |
| 3282 | } |
| 3283 | } |
| 3284 | length = skb->len; |
| 3285 | |
| 3286 | if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) { |
| 3287 | if (netif_msg_tx_err (dev)) |
| 3288 | devdbg (dev, "no urb"); |
| 3289 | goto drop; |
| 3290 | } |
| 3291 | |
| 3292 | entry = (struct skb_data *) skb->cb; |
| 3293 | entry->urb = urb; |
| 3294 | entry->dev = dev; |
| 3295 | entry->state = tx_start; |
| 3296 | entry->length = length; |
| 3297 | |
| 3298 | // FIXME: reorganize a bit, so that fixup() fills out NetChip |
| 3299 | // framing too. (Packet ID update needs the spinlock...) |
| 3300 | // [ BETTER: we already own net->xmit_lock, that's enough ] |
| 3301 | |
| 3302 | #ifdef CONFIG_USB_NET1080 |
| 3303 | if (info->flags & FLAG_FRAMING_NC) { |
| 3304 | header = (struct nc_header *) skb_push (skb, sizeof *header); |
| 3305 | header->hdr_len = cpu_to_le16 (sizeof (*header)); |
| 3306 | header->packet_len = cpu_to_le16 (length); |
| 3307 | if (!((skb->len + sizeof *trailer) & 0x01)) |
| 3308 | *skb_put (skb, 1) = PAD_BYTE; |
| 3309 | trailer = (struct nc_trailer *) skb_put (skb, sizeof *trailer); |
| 3310 | } |
| 3311 | #endif /* CONFIG_USB_NET1080 */ |
| 3312 | |
| 3313 | usb_fill_bulk_urb (urb, dev->udev, dev->out, |
| 3314 | skb->data, skb->len, tx_complete, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3315 | |
| 3316 | /* don't assume the hardware handles USB_ZERO_PACKET |
| 3317 | * NOTE: strictly conforming cdc-ether devices should expect |
| 3318 | * the ZLP here, but ignore the one-byte packet. |
| 3319 | * |
| 3320 | * FIXME zero that byte, if it doesn't require a new skb. |
| 3321 | */ |
| 3322 | if ((length % dev->maxpacket) == 0) |
| 3323 | urb->transfer_buffer_length++; |
| 3324 | |
| 3325 | spin_lock_irqsave (&dev->txq.lock, flags); |
| 3326 | |
| 3327 | #ifdef CONFIG_USB_NET1080 |
| 3328 | if (info->flags & FLAG_FRAMING_NC) { |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 3329 | header->packet_id = cpu_to_le16 ((u16)dev->xid++); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3330 | put_unaligned (header->packet_id, &trailer->packet_id); |
| 3331 | #if 0 |
| 3332 | devdbg (dev, "frame >tx h %d p %d id %d", |
| 3333 | header->hdr_len, header->packet_len, |
| 3334 | header->packet_id); |
| 3335 | #endif |
| 3336 | } |
| 3337 | #endif /* CONFIG_USB_NET1080 */ |
| 3338 | |
| 3339 | switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) { |
| 3340 | case -EPIPE: |
| 3341 | netif_stop_queue (net); |
| 3342 | defer_kevent (dev, EVENT_TX_HALT); |
| 3343 | break; |
| 3344 | default: |
| 3345 | if (netif_msg_tx_err (dev)) |
| 3346 | devdbg (dev, "tx: submit urb err %d", retval); |
| 3347 | break; |
| 3348 | case 0: |
| 3349 | net->trans_start = jiffies; |
| 3350 | __skb_queue_tail (&dev->txq, skb); |
| 3351 | if (dev->txq.qlen >= TX_QLEN (dev)) |
| 3352 | netif_stop_queue (net); |
| 3353 | } |
| 3354 | spin_unlock_irqrestore (&dev->txq.lock, flags); |
| 3355 | |
| 3356 | if (retval) { |
| 3357 | if (netif_msg_tx_err (dev)) |
| 3358 | devdbg (dev, "drop, code %d", retval); |
| 3359 | drop: |
| 3360 | retval = NET_XMIT_SUCCESS; |
| 3361 | dev->stats.tx_dropped++; |
| 3362 | if (skb) |
| 3363 | dev_kfree_skb_any (skb); |
| 3364 | usb_free_urb (urb); |
| 3365 | } else if (netif_msg_tx_queued (dev)) { |
| 3366 | devdbg (dev, "> tx, len %d, type 0x%x", |
| 3367 | length, skb->protocol); |
| 3368 | } |
| 3369 | return retval; |
| 3370 | } |
| 3371 | |
| 3372 | |
| 3373 | /*-------------------------------------------------------------------------*/ |
| 3374 | |
| 3375 | // tasklet (work deferred from completions, in_irq) or timer |
| 3376 | |
| 3377 | static void usbnet_bh (unsigned long param) |
| 3378 | { |
| 3379 | struct usbnet *dev = (struct usbnet *) param; |
| 3380 | struct sk_buff *skb; |
| 3381 | struct skb_data *entry; |
| 3382 | |
| 3383 | while ((skb = skb_dequeue (&dev->done))) { |
| 3384 | entry = (struct skb_data *) skb->cb; |
| 3385 | switch (entry->state) { |
| 3386 | case rx_done: |
| 3387 | entry->state = rx_cleanup; |
| 3388 | rx_process (dev, skb); |
| 3389 | continue; |
| 3390 | case tx_done: |
| 3391 | case rx_cleanup: |
| 3392 | usb_free_urb (entry->urb); |
| 3393 | dev_kfree_skb (skb); |
| 3394 | continue; |
| 3395 | default: |
| 3396 | devdbg (dev, "bogus skb state %d", entry->state); |
| 3397 | } |
| 3398 | } |
| 3399 | |
| 3400 | // waiting for all pending urbs to complete? |
| 3401 | if (dev->wait) { |
| 3402 | if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) { |
| 3403 | wake_up (dev->wait); |
| 3404 | } |
| 3405 | |
| 3406 | // or are we maybe short a few urbs? |
| 3407 | } else if (netif_running (dev->net) |
| 3408 | && netif_device_present (dev->net) |
| 3409 | && !timer_pending (&dev->delay) |
| 3410 | && !test_bit (EVENT_RX_HALT, &dev->flags)) { |
| 3411 | int temp = dev->rxq.qlen; |
| 3412 | int qlen = RX_QLEN (dev); |
| 3413 | |
| 3414 | if (temp < qlen) { |
| 3415 | struct urb *urb; |
| 3416 | int i; |
| 3417 | |
| 3418 | // don't refill the queue all at once |
| 3419 | for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) { |
| 3420 | urb = usb_alloc_urb (0, GFP_ATOMIC); |
| 3421 | if (urb != NULL) |
| 3422 | rx_submit (dev, urb, GFP_ATOMIC); |
| 3423 | } |
| 3424 | if (temp != dev->rxq.qlen && netif_msg_link (dev)) |
| 3425 | devdbg (dev, "rxqlen %d --> %d", |
| 3426 | temp, dev->rxq.qlen); |
| 3427 | if (dev->rxq.qlen < qlen) |
| 3428 | tasklet_schedule (&dev->bh); |
| 3429 | } |
| 3430 | if (dev->txq.qlen < TX_QLEN (dev)) |
| 3431 | netif_wake_queue (dev->net); |
| 3432 | } |
| 3433 | } |
| 3434 | |
| 3435 | |
| 3436 | |
| 3437 | /*------------------------------------------------------------------------- |
| 3438 | * |
| 3439 | * USB Device Driver support |
| 3440 | * |
| 3441 | *-------------------------------------------------------------------------*/ |
| 3442 | |
| 3443 | // precondition: never called in_interrupt |
| 3444 | |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame^] | 3445 | void usbnet_disconnect (struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3446 | { |
| 3447 | struct usbnet *dev; |
| 3448 | struct usb_device *xdev; |
| 3449 | struct net_device *net; |
| 3450 | |
| 3451 | dev = usb_get_intfdata(intf); |
| 3452 | usb_set_intfdata(intf, NULL); |
| 3453 | if (!dev) |
| 3454 | return; |
| 3455 | |
| 3456 | xdev = interface_to_usbdev (intf); |
| 3457 | |
| 3458 | if (netif_msg_probe (dev)) |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame^] | 3459 | devinfo (dev, "unregister '%s' usb-%s-%s, %s", |
| 3460 | intf->dev.driver->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3461 | xdev->bus->bus_name, xdev->devpath, |
| 3462 | dev->driver_info->description); |
| 3463 | |
| 3464 | net = dev->net; |
| 3465 | unregister_netdev (net); |
| 3466 | |
| 3467 | /* we don't hold rtnl here ... */ |
| 3468 | flush_scheduled_work (); |
| 3469 | |
| 3470 | if (dev->driver_info->unbind) |
| 3471 | dev->driver_info->unbind (dev, intf); |
| 3472 | |
| 3473 | free_netdev(net); |
| 3474 | usb_put_dev (xdev); |
| 3475 | } |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame^] | 3476 | EXPORT_SYMBOL_GPL(usbnet_disconnect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3477 | |
| 3478 | |
| 3479 | /*-------------------------------------------------------------------------*/ |
| 3480 | |
| 3481 | static struct ethtool_ops usbnet_ethtool_ops; |
| 3482 | |
| 3483 | // precondition: never called in_interrupt |
| 3484 | |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame^] | 3485 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3486 | usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod) |
| 3487 | { |
| 3488 | struct usbnet *dev; |
| 3489 | struct net_device *net; |
| 3490 | struct usb_host_interface *interface; |
| 3491 | struct driver_info *info; |
| 3492 | struct usb_device *xdev; |
| 3493 | int status; |
| 3494 | |
| 3495 | info = (struct driver_info *) prod->driver_info; |
| 3496 | if (!info) { |
| 3497 | dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name); |
| 3498 | return -ENODEV; |
| 3499 | } |
| 3500 | xdev = interface_to_usbdev (udev); |
| 3501 | interface = udev->cur_altsetting; |
| 3502 | |
| 3503 | usb_get_dev (xdev); |
| 3504 | |
| 3505 | status = -ENOMEM; |
| 3506 | |
| 3507 | // set up our own records |
| 3508 | net = alloc_etherdev(sizeof(*dev)); |
| 3509 | if (!net) { |
| 3510 | dbg ("can't kmalloc dev"); |
| 3511 | goto out; |
| 3512 | } |
| 3513 | |
| 3514 | dev = netdev_priv(net); |
| 3515 | dev->udev = xdev; |
| 3516 | dev->driver_info = info; |
| 3517 | dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV |
| 3518 | | NETIF_MSG_PROBE | NETIF_MSG_LINK); |
| 3519 | skb_queue_head_init (&dev->rxq); |
| 3520 | skb_queue_head_init (&dev->txq); |
| 3521 | skb_queue_head_init (&dev->done); |
| 3522 | dev->bh.func = usbnet_bh; |
| 3523 | dev->bh.data = (unsigned long) dev; |
| 3524 | INIT_WORK (&dev->kevent, kevent, dev); |
| 3525 | dev->delay.function = usbnet_bh; |
| 3526 | dev->delay.data = (unsigned long) dev; |
| 3527 | init_timer (&dev->delay); |
| 3528 | |
| 3529 | SET_MODULE_OWNER (net); |
| 3530 | dev->net = net; |
| 3531 | strcpy (net->name, "usb%d"); |
| 3532 | memcpy (net->dev_addr, node_id, sizeof node_id); |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 3533 | dev->hard_mtu = net->mtu + net->hard_header_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3534 | |
| 3535 | #if 0 |
| 3536 | // dma_supported() is deeply broken on almost all architectures |
| 3537 | // possible with some EHCI controllers |
| 3538 | if (dma_supported (&udev->dev, DMA_64BIT_MASK)) |
| 3539 | net->features |= NETIF_F_HIGHDMA; |
| 3540 | #endif |
| 3541 | |
| 3542 | net->change_mtu = usbnet_change_mtu; |
| 3543 | net->get_stats = usbnet_get_stats; |
| 3544 | net->hard_start_xmit = usbnet_start_xmit; |
| 3545 | net->open = usbnet_open; |
| 3546 | net->stop = usbnet_stop; |
| 3547 | net->watchdog_timeo = TX_TIMEOUT_JIFFIES; |
| 3548 | net->tx_timeout = usbnet_tx_timeout; |
| 3549 | net->do_ioctl = usbnet_ioctl; |
| 3550 | net->ethtool_ops = &usbnet_ethtool_ops; |
| 3551 | |
| 3552 | // allow device-specific bind/init procedures |
| 3553 | // NOTE net->name still not usable ... |
| 3554 | if (info->bind) { |
| 3555 | status = info->bind (dev, udev); |
| 3556 | // heuristic: "usb%d" for links we know are two-host, |
| 3557 | // else "eth%d" when there's reasonable doubt. userspace |
| 3558 | // can rename the link if it knows better. |
| 3559 | if ((dev->driver_info->flags & FLAG_ETHER) != 0 |
| 3560 | && (net->dev_addr [0] & 0x02) == 0) |
| 3561 | strcpy (net->name, "eth%d"); |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 3562 | |
| 3563 | /* maybe the remote can't receive an Ethernet MTU */ |
| 3564 | if (net->mtu > (dev->hard_mtu - net->hard_header_len)) |
| 3565 | net->mtu = dev->hard_mtu - net->hard_header_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3566 | } else if (!info->in || info->out) |
| 3567 | status = get_endpoints (dev, udev); |
| 3568 | else { |
| 3569 | dev->in = usb_rcvbulkpipe (xdev, info->in); |
| 3570 | dev->out = usb_sndbulkpipe (xdev, info->out); |
| 3571 | if (!(info->flags & FLAG_NO_SETINT)) |
| 3572 | status = usb_set_interface (xdev, |
| 3573 | interface->desc.bInterfaceNumber, |
| 3574 | interface->desc.bAlternateSetting); |
| 3575 | else |
| 3576 | status = 0; |
| 3577 | |
| 3578 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3579 | if (status == 0 && dev->status) |
| 3580 | status = init_status (dev, udev); |
| 3581 | if (status < 0) |
| 3582 | goto out1; |
| 3583 | |
| 3584 | dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1); |
| 3585 | |
| 3586 | SET_NETDEV_DEV(net, &udev->dev); |
| 3587 | status = register_netdev (net); |
| 3588 | if (status) |
| 3589 | goto out3; |
| 3590 | if (netif_msg_probe (dev)) |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame^] | 3591 | devinfo (dev, "register '%s' at usb-%s-%s, %s, " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3592 | "%02x:%02x:%02x:%02x:%02x:%02x", |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame^] | 3593 | udev->dev.driver->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3594 | xdev->bus->bus_name, xdev->devpath, |
| 3595 | dev->driver_info->description, |
| 3596 | net->dev_addr [0], net->dev_addr [1], |
| 3597 | net->dev_addr [2], net->dev_addr [3], |
| 3598 | net->dev_addr [4], net->dev_addr [5]); |
| 3599 | |
| 3600 | // ok, it's ready to go. |
| 3601 | usb_set_intfdata (udev, dev); |
| 3602 | |
| 3603 | // start as if the link is up |
| 3604 | netif_device_attach (net); |
| 3605 | |
| 3606 | return 0; |
| 3607 | |
| 3608 | out3: |
| 3609 | if (info->unbind) |
| 3610 | info->unbind (dev, udev); |
| 3611 | out1: |
| 3612 | free_netdev(net); |
| 3613 | out: |
| 3614 | usb_put_dev(xdev); |
| 3615 | return status; |
| 3616 | } |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame^] | 3617 | EXPORT_SYMBOL_GPL(usbnet_probe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3618 | |
| 3619 | /*-------------------------------------------------------------------------*/ |
| 3620 | |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame^] | 3621 | /* FIXME these suspend/resume methods assume non-CDC style |
| 3622 | * devices, with only one interface. |
| 3623 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3624 | |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame^] | 3625 | int usbnet_suspend (struct usb_interface *intf, pm_message_t message) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3626 | { |
| 3627 | struct usbnet *dev = usb_get_intfdata(intf); |
| 3628 | |
David Brownell | 27d72e8 | 2005-04-18 17:39:22 -0700 | [diff] [blame] | 3629 | /* accelerate emptying of the rx and queues, to avoid |
| 3630 | * having everything error out. |
| 3631 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3632 | netif_device_detach (dev->net); |
David Brownell | 27d72e8 | 2005-04-18 17:39:22 -0700 | [diff] [blame] | 3633 | (void) unlink_urbs (dev, &dev->rxq); |
| 3634 | (void) unlink_urbs (dev, &dev->txq); |
| 3635 | intf->dev.power.power_state = PMSG_SUSPEND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3636 | return 0; |
| 3637 | } |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame^] | 3638 | EXPORT_SYMBOL_GPL(usbnet_suspend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3639 | |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame^] | 3640 | int usbnet_resume (struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3641 | { |
| 3642 | struct usbnet *dev = usb_get_intfdata(intf); |
| 3643 | |
David Brownell | 27d72e8 | 2005-04-18 17:39:22 -0700 | [diff] [blame] | 3644 | intf->dev.power.power_state = PMSG_ON; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | netif_device_attach (dev->net); |
David Brownell | 27d72e8 | 2005-04-18 17:39:22 -0700 | [diff] [blame] | 3646 | tasklet_schedule (&dev->bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3647 | return 0; |
| 3648 | } |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame^] | 3649 | EXPORT_SYMBOL_GPL(usbnet_resume); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3651 | |
| 3652 | /*-------------------------------------------------------------------------*/ |
| 3653 | |
| 3654 | #ifndef HAVE_HARDWARE |
| 3655 | #error You need to configure some hardware for this driver |
| 3656 | #endif |
| 3657 | |
| 3658 | /* |
| 3659 | * chip vendor names won't normally be on the cables, and |
| 3660 | * may not be on the device. |
| 3661 | */ |
| 3662 | |
| 3663 | static const struct usb_device_id products [] = { |
| 3664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3665 | #ifdef CONFIG_USB_AX8817X |
| 3666 | { |
| 3667 | // Linksys USB200M |
| 3668 | USB_DEVICE (0x077b, 0x2226), |
| 3669 | .driver_info = (unsigned long) &ax8817x_info, |
| 3670 | }, { |
| 3671 | // Netgear FA120 |
| 3672 | USB_DEVICE (0x0846, 0x1040), |
| 3673 | .driver_info = (unsigned long) &netgear_fa120_info, |
| 3674 | }, { |
| 3675 | // DLink DUB-E100 |
| 3676 | USB_DEVICE (0x2001, 0x1a00), |
| 3677 | .driver_info = (unsigned long) &dlink_dub_e100_info, |
| 3678 | }, { |
| 3679 | // Intellinet, ST Lab USB Ethernet |
| 3680 | USB_DEVICE (0x0b95, 0x1720), |
| 3681 | .driver_info = (unsigned long) &ax8817x_info, |
| 3682 | }, { |
| 3683 | // Hawking UF200, TrendNet TU2-ET100 |
| 3684 | USB_DEVICE (0x07b8, 0x420a), |
| 3685 | .driver_info = (unsigned long) &hawking_uf200_info, |
| 3686 | }, { |
| 3687 | // Billionton Systems, USB2AR |
| 3688 | USB_DEVICE (0x08dd, 0x90ff), |
| 3689 | .driver_info = (unsigned long) &ax8817x_info, |
| 3690 | }, { |
| 3691 | // ATEN UC210T |
| 3692 | USB_DEVICE (0x0557, 0x2009), |
| 3693 | .driver_info = (unsigned long) &ax8817x_info, |
| 3694 | }, { |
| 3695 | // Buffalo LUA-U2-KTX |
| 3696 | USB_DEVICE (0x0411, 0x003d), |
| 3697 | .driver_info = (unsigned long) &ax8817x_info, |
| 3698 | }, { |
| 3699 | // Sitecom LN-029 "USB 2.0 10/100 Ethernet adapter" |
| 3700 | USB_DEVICE (0x6189, 0x182d), |
| 3701 | .driver_info = (unsigned long) &ax8817x_info, |
| 3702 | }, { |
| 3703 | // corega FEther USB2-TX |
| 3704 | USB_DEVICE (0x07aa, 0x0017), |
| 3705 | .driver_info = (unsigned long) &ax8817x_info, |
| 3706 | }, { |
| 3707 | // Surecom EP-1427X-2 |
| 3708 | USB_DEVICE (0x1189, 0x0893), |
| 3709 | .driver_info = (unsigned long) &ax8817x_info, |
| 3710 | }, { |
| 3711 | // goodway corp usb gwusb2e |
| 3712 | USB_DEVICE (0x1631, 0x6200), |
| 3713 | .driver_info = (unsigned long) &ax8817x_info, |
| 3714 | }, { |
| 3715 | // ASIX AX88772 10/100 |
| 3716 | USB_DEVICE (0x0b95, 0x7720), |
| 3717 | .driver_info = (unsigned long) &ax88772_info, |
| 3718 | }, |
| 3719 | #endif |
| 3720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3721 | #ifdef CONFIG_USB_GENESYS |
| 3722 | { |
| 3723 | USB_DEVICE (0x05e3, 0x0502), // GL620USB-A |
| 3724 | .driver_info = (unsigned long) &genelink_info, |
| 3725 | }, |
| 3726 | /* NOT: USB_DEVICE (0x05e3, 0x0501), // GL620USB |
| 3727 | * that's half duplex, not currently supported |
| 3728 | */ |
| 3729 | #endif |
| 3730 | |
| 3731 | #ifdef CONFIG_USB_NET1080 |
| 3732 | { |
| 3733 | USB_DEVICE (0x0525, 0x1080), // NetChip ref design |
| 3734 | .driver_info = (unsigned long) &net1080_info, |
| 3735 | }, { |
| 3736 | USB_DEVICE (0x06D0, 0x0622), // Laplink Gold |
| 3737 | .driver_info = (unsigned long) &net1080_info, |
| 3738 | }, |
| 3739 | #endif |
| 3740 | |
| 3741 | #ifdef CONFIG_USB_PL2301 |
| 3742 | { |
| 3743 | USB_DEVICE (0x067b, 0x0000), // PL-2301 |
| 3744 | .driver_info = (unsigned long) &prolific_info, |
| 3745 | }, { |
| 3746 | USB_DEVICE (0x067b, 0x0001), // PL-2302 |
| 3747 | .driver_info = (unsigned long) &prolific_info, |
| 3748 | }, |
| 3749 | #endif |
| 3750 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3751 | #ifdef CONFIG_USB_RNDIS |
| 3752 | { |
| 3753 | /* RNDIS is MSFT's un-official variant of CDC ACM */ |
| 3754 | USB_INTERFACE_INFO (USB_CLASS_COMM, 2 /* ACM */, 0x0ff), |
| 3755 | .driver_info = (unsigned long) &rndis_info, |
| 3756 | }, |
| 3757 | #endif |
| 3758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3759 | #if defined(CONFIG_USB_ZAURUS) || defined(CONFIG_USB_CDCETHER) |
| 3760 | /* |
| 3761 | * SA-1100 based Sharp Zaurus ("collie"), or compatible. |
| 3762 | * Same idea as above, but different framing. |
| 3763 | * |
| 3764 | * PXA-2xx based models are also lying-about-cdc. |
| 3765 | * Some models don't even tell the same lies ... |
| 3766 | * |
| 3767 | * NOTE: OpenZaurus versions with 2.6 kernels won't use these entries, |
| 3768 | * unlike the older ones with 2.4 "embedix" kernels. |
| 3769 | * |
| 3770 | * NOTE: These entries do double-duty, serving as blacklist entries |
| 3771 | * whenever Zaurus support isn't enabled, but CDC Ethernet is. |
| 3772 | */ |
| 3773 | #define ZAURUS_MASTER_INTERFACE \ |
| 3774 | .bInterfaceClass = USB_CLASS_COMM, \ |
| 3775 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \ |
| 3776 | .bInterfaceProtocol = USB_CDC_PROTO_NONE |
| 3777 | { |
| 3778 | .match_flags = USB_DEVICE_ID_MATCH_INT_INFO |
| 3779 | | USB_DEVICE_ID_MATCH_DEVICE, |
| 3780 | .idVendor = 0x04DD, |
| 3781 | .idProduct = 0x8004, |
| 3782 | ZAURUS_MASTER_INTERFACE, |
| 3783 | .driver_info = ZAURUS_STRONGARM_INFO, |
| 3784 | }, { |
| 3785 | .match_flags = USB_DEVICE_ID_MATCH_INT_INFO |
| 3786 | | USB_DEVICE_ID_MATCH_DEVICE, |
| 3787 | .idVendor = 0x04DD, |
| 3788 | .idProduct = 0x8005, /* A-300 */ |
| 3789 | ZAURUS_MASTER_INTERFACE, |
| 3790 | .driver_info = ZAURUS_PXA_INFO, |
| 3791 | }, { |
| 3792 | .match_flags = USB_DEVICE_ID_MATCH_INT_INFO |
| 3793 | | USB_DEVICE_ID_MATCH_DEVICE, |
| 3794 | .idVendor = 0x04DD, |
| 3795 | .idProduct = 0x8006, /* B-500/SL-5600 */ |
| 3796 | ZAURUS_MASTER_INTERFACE, |
| 3797 | .driver_info = ZAURUS_PXA_INFO, |
| 3798 | }, { |
| 3799 | .match_flags = USB_DEVICE_ID_MATCH_INT_INFO |
| 3800 | | USB_DEVICE_ID_MATCH_DEVICE, |
| 3801 | .idVendor = 0x04DD, |
| 3802 | .idProduct = 0x8007, /* C-700 */ |
| 3803 | ZAURUS_MASTER_INTERFACE, |
| 3804 | .driver_info = ZAURUS_PXA_INFO, |
| 3805 | }, { |
| 3806 | .match_flags = USB_DEVICE_ID_MATCH_INT_INFO |
| 3807 | | USB_DEVICE_ID_MATCH_DEVICE, |
| 3808 | .idVendor = 0x04DD, |
| 3809 | .idProduct = 0x9031, /* C-750 C-760 */ |
| 3810 | ZAURUS_MASTER_INTERFACE, |
| 3811 | .driver_info = ZAURUS_PXA_INFO, |
| 3812 | }, { |
| 3813 | .match_flags = USB_DEVICE_ID_MATCH_INT_INFO |
| 3814 | | USB_DEVICE_ID_MATCH_DEVICE, |
| 3815 | .idVendor = 0x04DD, |
| 3816 | .idProduct = 0x9032, /* SL-6000 */ |
| 3817 | ZAURUS_MASTER_INTERFACE, |
| 3818 | .driver_info = ZAURUS_PXA_INFO, |
| 3819 | }, { |
| 3820 | .match_flags = USB_DEVICE_ID_MATCH_INT_INFO |
| 3821 | | USB_DEVICE_ID_MATCH_DEVICE, |
| 3822 | .idVendor = 0x04DD, |
| 3823 | /* reported with some C860 units */ |
| 3824 | .idProduct = 0x9050, /* C-860 */ |
| 3825 | ZAURUS_MASTER_INTERFACE, |
| 3826 | .driver_info = ZAURUS_PXA_INFO, |
David Brownell | a2fe201 | 2005-04-18 17:39:29 -0700 | [diff] [blame] | 3827 | }, |
| 3828 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3829 | #ifdef CONFIG_USB_ZAURUS |
David Brownell | f3fae6e | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 3830 | /* At least some (reports vary) PXA units have very different lies |
| 3831 | * about their standards support: they claim to be cell phones with |
| 3832 | * direct access to their radios. (They don't conform to CDC MDLM.) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3833 | */ |
David Brownell | a2fe201 | 2005-04-18 17:39:29 -0700 | [diff] [blame] | 3834 | { |
David Brownell | f3fae6e | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 3835 | USB_INTERFACE_INFO (USB_CLASS_COMM, USB_CDC_SUBCLASS_MDLM, |
| 3836 | USB_CDC_PROTO_NONE), |
| 3837 | .driver_info = (unsigned long) &bogus_mdlm_info, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3838 | }, |
David Brownell | a2fe201 | 2005-04-18 17:39:29 -0700 | [diff] [blame] | 3839 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3840 | |
| 3841 | /* Olympus has some models with a Zaurus-compatible option. |
| 3842 | * R-1000 uses a FreeScale i.MXL cpu (ARMv4T) |
| 3843 | */ |
| 3844 | { |
| 3845 | .match_flags = USB_DEVICE_ID_MATCH_INT_INFO |
| 3846 | | USB_DEVICE_ID_MATCH_DEVICE, |
| 3847 | .idVendor = 0x07B4, |
| 3848 | .idProduct = 0x0F02, /* R-1000 */ |
| 3849 | ZAURUS_MASTER_INTERFACE, |
| 3850 | .driver_info = OLYMPUS_MXL_INFO, |
| 3851 | }, |
| 3852 | #endif |
| 3853 | |
| 3854 | #ifdef CONFIG_USB_CDCETHER |
| 3855 | { |
| 3856 | /* CDC Ether uses two interfaces, not necessarily consecutive. |
| 3857 | * We match the main interface, ignoring the optional device |
| 3858 | * class so we could handle devices that aren't exclusively |
| 3859 | * CDC ether. |
| 3860 | * |
| 3861 | * NOTE: this match must come AFTER entries working around |
| 3862 | * bugs/quirks in a given product (like Zaurus, above). |
| 3863 | */ |
| 3864 | USB_INTERFACE_INFO (USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET, |
| 3865 | USB_CDC_PROTO_NONE), |
| 3866 | .driver_info = (unsigned long) &cdc_info, |
| 3867 | }, |
| 3868 | #endif |
| 3869 | |
| 3870 | { }, // END |
| 3871 | }; |
| 3872 | MODULE_DEVICE_TABLE (usb, products); |
| 3873 | |
| 3874 | static struct usb_driver usbnet_driver = { |
| 3875 | .owner = THIS_MODULE, |
| 3876 | .name = driver_name, |
| 3877 | .id_table = products, |
| 3878 | .probe = usbnet_probe, |
| 3879 | .disconnect = usbnet_disconnect, |
| 3880 | .suspend = usbnet_suspend, |
| 3881 | .resume = usbnet_resume, |
| 3882 | }; |
| 3883 | |
| 3884 | /* Default ethtool_ops assigned. Devices can override in their bind() routine */ |
| 3885 | static struct ethtool_ops usbnet_ethtool_ops = { |
| 3886 | .get_drvinfo = usbnet_get_drvinfo, |
| 3887 | .get_link = usbnet_get_link, |
| 3888 | .get_msglevel = usbnet_get_msglevel, |
| 3889 | .set_msglevel = usbnet_set_msglevel, |
| 3890 | }; |
| 3891 | |
| 3892 | /*-------------------------------------------------------------------------*/ |
| 3893 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 3894 | static int __init usbnet_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3895 | { |
| 3896 | // compiler should optimize these out |
| 3897 | BUG_ON (sizeof (((struct sk_buff *)0)->cb) |
| 3898 | < sizeof (struct skb_data)); |
| 3899 | #ifdef CONFIG_USB_CDCETHER |
| 3900 | BUG_ON ((sizeof (((struct usbnet *)0)->data) |
| 3901 | < sizeof (struct cdc_state))); |
| 3902 | #endif |
| 3903 | |
| 3904 | random_ether_addr(node_id); |
| 3905 | |
| 3906 | return usb_register(&usbnet_driver); |
| 3907 | } |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 3908 | module_init(usbnet_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3909 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 3910 | static void __exit usbnet_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3911 | { |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 3912 | usb_deregister(&usbnet_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3913 | } |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 3914 | module_exit(usbnet_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3915 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 3916 | MODULE_AUTHOR("David Brownell"); |
| 3917 | MODULE_DESCRIPTION("USB Host-to-Host Link Drivers (numerous vendors)"); |
| 3918 | MODULE_LICENSE("GPL"); |