Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Davicom DM9601 USB 1.1 10/100Mbps ethernet devices |
| 3 | * |
| 4 | * Peter Korsgaard <jacmet@sunsite.dk> |
| 5 | * |
| 6 | * This file is licensed under the terms of the GNU General Public License |
| 7 | * version 2. This program is licensed "as is" without any warranty of any |
| 8 | * kind, whether express or implied. |
| 9 | */ |
| 10 | |
| 11 | //#define DEBUG |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/sched.h> |
Randy Dunlap | 686149f | 2007-03-16 18:33:13 -0700 | [diff] [blame] | 15 | #include <linux/stddef.h> |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 16 | #include <linux/init.h> |
| 17 | #include <linux/netdevice.h> |
| 18 | #include <linux/etherdevice.h> |
| 19 | #include <linux/ethtool.h> |
| 20 | #include <linux/mii.h> |
| 21 | #include <linux/usb.h> |
| 22 | #include <linux/crc32.h> |
Jussi Kivilinna | 3692e94 | 2008-01-26 00:51:45 +0200 | [diff] [blame] | 23 | #include <linux/usb/usbnet.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 25 | |
| 26 | /* datasheet: |
Wu Fengguang | 98658bc | 2009-01-06 10:56:07 -0800 | [diff] [blame] | 27 | http://ptm2.cc.utu.fi/ftp/network/cards/DM9601/From_NET/DM9601-DS-P01-930914.pdf |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | /* control requests */ |
| 31 | #define DM_READ_REGS 0x00 |
| 32 | #define DM_WRITE_REGS 0x01 |
| 33 | #define DM_READ_MEMS 0x02 |
| 34 | #define DM_WRITE_REG 0x03 |
| 35 | #define DM_WRITE_MEMS 0x05 |
| 36 | #define DM_WRITE_MEM 0x07 |
| 37 | |
| 38 | /* registers */ |
| 39 | #define DM_NET_CTRL 0x00 |
| 40 | #define DM_RX_CTRL 0x05 |
| 41 | #define DM_SHARED_CTRL 0x0b |
| 42 | #define DM_SHARED_ADDR 0x0c |
| 43 | #define DM_SHARED_DATA 0x0d /* low + high */ |
| 44 | #define DM_PHY_ADDR 0x10 /* 6 bytes */ |
| 45 | #define DM_MCAST_ADDR 0x16 /* 8 bytes */ |
| 46 | #define DM_GPR_CTRL 0x1e |
| 47 | #define DM_GPR_DATA 0x1f |
| 48 | |
| 49 | #define DM_MAX_MCAST 64 |
| 50 | #define DM_MCAST_SIZE 8 |
| 51 | #define DM_EEPROM_LEN 256 |
| 52 | #define DM_TX_OVERHEAD 2 /* 2 byte header */ |
| 53 | #define DM_RX_OVERHEAD 7 /* 3 byte header + 4 byte crc tail */ |
| 54 | #define DM_TIMEOUT 1000 |
| 55 | |
| 56 | |
| 57 | static int dm_read(struct usbnet *dev, u8 reg, u16 length, void *data) |
| 58 | { |
Peter Korsgaard | 16d78bc | 2008-07-14 09:07:32 +0200 | [diff] [blame] | 59 | void *buf; |
| 60 | int err = -ENOMEM; |
| 61 | |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 62 | netdev_dbg(dev->net, "dm_read() reg=0x%02x length=%d\n", reg, length); |
Peter Korsgaard | 16d78bc | 2008-07-14 09:07:32 +0200 | [diff] [blame] | 63 | |
| 64 | buf = kmalloc(length, GFP_KERNEL); |
| 65 | if (!buf) |
| 66 | goto out; |
| 67 | |
| 68 | err = usb_control_msg(dev->udev, |
| 69 | usb_rcvctrlpipe(dev->udev, 0), |
| 70 | DM_READ_REGS, |
| 71 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 72 | 0, reg, buf, length, USB_CTRL_SET_TIMEOUT); |
| 73 | if (err == length) |
| 74 | memcpy(data, buf, length); |
| 75 | else if (err >= 0) |
| 76 | err = -EINVAL; |
| 77 | kfree(buf); |
| 78 | |
| 79 | out: |
| 80 | return err; |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static int dm_read_reg(struct usbnet *dev, u8 reg, u8 *value) |
| 84 | { |
| 85 | return dm_read(dev, reg, 1, value); |
| 86 | } |
| 87 | |
| 88 | static int dm_write(struct usbnet *dev, u8 reg, u16 length, void *data) |
| 89 | { |
Peter Korsgaard | 16d78bc | 2008-07-14 09:07:32 +0200 | [diff] [blame] | 90 | void *buf = NULL; |
| 91 | int err = -ENOMEM; |
| 92 | |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 93 | netdev_dbg(dev->net, "dm_write() reg=0x%02x, length=%d\n", reg, length); |
Peter Korsgaard | 16d78bc | 2008-07-14 09:07:32 +0200 | [diff] [blame] | 94 | |
| 95 | if (data) { |
Julia Lawall | 5476b8b | 2010-05-15 11:23:15 +0000 | [diff] [blame] | 96 | buf = kmemdup(data, length, GFP_KERNEL); |
Peter Korsgaard | 16d78bc | 2008-07-14 09:07:32 +0200 | [diff] [blame] | 97 | if (!buf) |
| 98 | goto out; |
Peter Korsgaard | 16d78bc | 2008-07-14 09:07:32 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | err = usb_control_msg(dev->udev, |
| 102 | usb_sndctrlpipe(dev->udev, 0), |
| 103 | DM_WRITE_REGS, |
| 104 | USB_DIR_OUT | USB_TYPE_VENDOR |USB_RECIP_DEVICE, |
| 105 | 0, reg, buf, length, USB_CTRL_SET_TIMEOUT); |
| 106 | kfree(buf); |
| 107 | if (err >= 0 && err < length) |
| 108 | err = -EINVAL; |
| 109 | out: |
| 110 | return err; |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static int dm_write_reg(struct usbnet *dev, u8 reg, u8 value) |
| 114 | { |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 115 | netdev_dbg(dev->net, "dm_write_reg() reg=0x%02x, value=0x%02x\n", |
| 116 | reg, value); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 117 | return usb_control_msg(dev->udev, |
| 118 | usb_sndctrlpipe(dev->udev, 0), |
| 119 | DM_WRITE_REG, |
| 120 | USB_DIR_OUT | USB_TYPE_VENDOR |USB_RECIP_DEVICE, |
Randy Dunlap | 686149f | 2007-03-16 18:33:13 -0700 | [diff] [blame] | 121 | value, reg, NULL, 0, USB_CTRL_SET_TIMEOUT); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static void dm_write_async_callback(struct urb *urb) |
| 125 | { |
| 126 | struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context; |
Oliver Neukum | c94cb31 | 2008-12-18 23:00:59 -0800 | [diff] [blame] | 127 | int status = urb->status; |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 128 | |
Oliver Neukum | c94cb31 | 2008-12-18 23:00:59 -0800 | [diff] [blame] | 129 | if (status < 0) |
Peter Korsgaard | 77b6901 | 2007-11-15 11:01:02 +0100 | [diff] [blame] | 130 | printk(KERN_DEBUG "dm_write_async_callback() failed with %d\n", |
Oliver Neukum | c94cb31 | 2008-12-18 23:00:59 -0800 | [diff] [blame] | 131 | status); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 132 | |
| 133 | kfree(req); |
| 134 | usb_free_urb(urb); |
| 135 | } |
| 136 | |
Peter Korsgaard | ba734f3 | 2007-11-15 11:10:12 +0100 | [diff] [blame] | 137 | static void dm_write_async_helper(struct usbnet *dev, u8 reg, u8 value, |
| 138 | u16 length, void *data) |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 139 | { |
| 140 | struct usb_ctrlrequest *req; |
| 141 | struct urb *urb; |
| 142 | int status; |
| 143 | |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 144 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 145 | if (!urb) { |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 146 | netdev_err(dev->net, "Error allocating URB in dm_write_async_helper!\n"); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 147 | return; |
| 148 | } |
| 149 | |
| 150 | req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC); |
| 151 | if (!req) { |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 152 | netdev_err(dev->net, "Failed to allocate memory for control request\n"); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 153 | usb_free_urb(urb); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE; |
Peter Korsgaard | ba734f3 | 2007-11-15 11:10:12 +0100 | [diff] [blame] | 158 | req->bRequest = length ? DM_WRITE_REGS : DM_WRITE_REG; |
| 159 | req->wValue = cpu_to_le16(value); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 160 | req->wIndex = cpu_to_le16(reg); |
| 161 | req->wLength = cpu_to_le16(length); |
| 162 | |
| 163 | usb_fill_control_urb(urb, dev->udev, |
| 164 | usb_sndctrlpipe(dev->udev, 0), |
| 165 | (void *)req, data, length, |
| 166 | dm_write_async_callback, req); |
| 167 | |
| 168 | status = usb_submit_urb(urb, GFP_ATOMIC); |
| 169 | if (status < 0) { |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 170 | netdev_err(dev->net, "Error submitting the control message: status=%d\n", |
| 171 | status); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 172 | kfree(req); |
| 173 | usb_free_urb(urb); |
| 174 | } |
| 175 | } |
| 176 | |
Peter Korsgaard | ba734f3 | 2007-11-15 11:10:12 +0100 | [diff] [blame] | 177 | static void dm_write_async(struct usbnet *dev, u8 reg, u16 length, void *data) |
| 178 | { |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 179 | netdev_dbg(dev->net, "dm_write_async() reg=0x%02x length=%d\n", reg, length); |
Peter Korsgaard | ba734f3 | 2007-11-15 11:10:12 +0100 | [diff] [blame] | 180 | |
| 181 | dm_write_async_helper(dev, reg, 0, length, data); |
| 182 | } |
| 183 | |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 184 | static void dm_write_reg_async(struct usbnet *dev, u8 reg, u8 value) |
| 185 | { |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 186 | netdev_dbg(dev->net, "dm_write_reg_async() reg=0x%02x value=0x%02x\n", |
| 187 | reg, value); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 188 | |
Peter Korsgaard | ba734f3 | 2007-11-15 11:10:12 +0100 | [diff] [blame] | 189 | dm_write_async_helper(dev, reg, value, 0, NULL); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Al Viro | eca1ad8 | 2008-03-16 22:21:54 +0000 | [diff] [blame] | 192 | static int dm_read_shared_word(struct usbnet *dev, int phy, u8 reg, __le16 *value) |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 193 | { |
| 194 | int ret, i; |
| 195 | |
| 196 | mutex_lock(&dev->phy_mutex); |
| 197 | |
| 198 | dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg); |
| 199 | dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0xc : 0x4); |
| 200 | |
| 201 | for (i = 0; i < DM_TIMEOUT; i++) { |
| 202 | u8 tmp; |
| 203 | |
| 204 | udelay(1); |
| 205 | ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp); |
| 206 | if (ret < 0) |
| 207 | goto out; |
| 208 | |
| 209 | /* ready */ |
| 210 | if ((tmp & 1) == 0) |
| 211 | break; |
| 212 | } |
| 213 | |
| 214 | if (i == DM_TIMEOUT) { |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 215 | netdev_err(dev->net, "%s read timed out!\n", phy ? "phy" : "eeprom"); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 216 | ret = -EIO; |
| 217 | goto out; |
| 218 | } |
| 219 | |
| 220 | dm_write_reg(dev, DM_SHARED_CTRL, 0x0); |
| 221 | ret = dm_read(dev, DM_SHARED_DATA, 2, value); |
| 222 | |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 223 | netdev_dbg(dev->net, "read shared %d 0x%02x returned 0x%04x, %d\n", |
| 224 | phy, reg, *value, ret); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 225 | |
| 226 | out: |
| 227 | mutex_unlock(&dev->phy_mutex); |
| 228 | return ret; |
| 229 | } |
| 230 | |
Al Viro | eca1ad8 | 2008-03-16 22:21:54 +0000 | [diff] [blame] | 231 | static int dm_write_shared_word(struct usbnet *dev, int phy, u8 reg, __le16 value) |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 232 | { |
| 233 | int ret, i; |
| 234 | |
| 235 | mutex_lock(&dev->phy_mutex); |
| 236 | |
| 237 | ret = dm_write(dev, DM_SHARED_DATA, 2, &value); |
| 238 | if (ret < 0) |
| 239 | goto out; |
| 240 | |
| 241 | dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg); |
Peter Korsgaard | e9162ab | 2010-05-03 10:01:26 +0000 | [diff] [blame] | 242 | dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0x1a : 0x12); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 243 | |
| 244 | for (i = 0; i < DM_TIMEOUT; i++) { |
| 245 | u8 tmp; |
| 246 | |
| 247 | udelay(1); |
| 248 | ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp); |
| 249 | if (ret < 0) |
| 250 | goto out; |
| 251 | |
| 252 | /* ready */ |
| 253 | if ((tmp & 1) == 0) |
| 254 | break; |
| 255 | } |
| 256 | |
| 257 | if (i == DM_TIMEOUT) { |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 258 | netdev_err(dev->net, "%s write timed out!\n", phy ? "phy" : "eeprom"); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 259 | ret = -EIO; |
| 260 | goto out; |
| 261 | } |
| 262 | |
| 263 | dm_write_reg(dev, DM_SHARED_CTRL, 0x0); |
| 264 | |
| 265 | out: |
| 266 | mutex_unlock(&dev->phy_mutex); |
| 267 | return ret; |
| 268 | } |
| 269 | |
| 270 | static int dm_read_eeprom_word(struct usbnet *dev, u8 offset, void *value) |
| 271 | { |
| 272 | return dm_read_shared_word(dev, 0, offset, value); |
| 273 | } |
| 274 | |
| 275 | |
| 276 | |
| 277 | static int dm9601_get_eeprom_len(struct net_device *dev) |
| 278 | { |
| 279 | return DM_EEPROM_LEN; |
| 280 | } |
| 281 | |
| 282 | static int dm9601_get_eeprom(struct net_device *net, |
| 283 | struct ethtool_eeprom *eeprom, u8 * data) |
| 284 | { |
| 285 | struct usbnet *dev = netdev_priv(net); |
Al Viro | eca1ad8 | 2008-03-16 22:21:54 +0000 | [diff] [blame] | 286 | __le16 *ebuf = (__le16 *) data; |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 287 | int i; |
| 288 | |
| 289 | /* access is 16bit */ |
| 290 | if ((eeprom->offset % 2) || (eeprom->len % 2)) |
| 291 | return -EINVAL; |
| 292 | |
| 293 | for (i = 0; i < eeprom->len / 2; i++) { |
| 294 | if (dm_read_eeprom_word(dev, eeprom->offset / 2 + i, |
| 295 | &ebuf[i]) < 0) |
| 296 | return -EINVAL; |
| 297 | } |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static int dm9601_mdio_read(struct net_device *netdev, int phy_id, int loc) |
| 302 | { |
| 303 | struct usbnet *dev = netdev_priv(netdev); |
| 304 | |
Al Viro | eca1ad8 | 2008-03-16 22:21:54 +0000 | [diff] [blame] | 305 | __le16 res; |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 306 | |
| 307 | if (phy_id) { |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 308 | netdev_dbg(dev->net, "Only internal phy supported\n"); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | dm_read_shared_word(dev, 1, loc, &res); |
| 313 | |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 314 | netdev_dbg(dev->net, |
| 315 | "dm9601_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n", |
| 316 | phy_id, loc, le16_to_cpu(res)); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 317 | |
| 318 | return le16_to_cpu(res); |
| 319 | } |
| 320 | |
| 321 | static void dm9601_mdio_write(struct net_device *netdev, int phy_id, int loc, |
| 322 | int val) |
| 323 | { |
| 324 | struct usbnet *dev = netdev_priv(netdev); |
Al Viro | eca1ad8 | 2008-03-16 22:21:54 +0000 | [diff] [blame] | 325 | __le16 res = cpu_to_le16(val); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 326 | |
| 327 | if (phy_id) { |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 328 | netdev_dbg(dev->net, "Only internal phy supported\n"); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 329 | return; |
| 330 | } |
| 331 | |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 332 | netdev_dbg(dev->net, "dm9601_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n", |
| 333 | phy_id, loc, val); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 334 | |
| 335 | dm_write_shared_word(dev, 1, loc, res); |
| 336 | } |
| 337 | |
| 338 | static void dm9601_get_drvinfo(struct net_device *net, |
| 339 | struct ethtool_drvinfo *info) |
| 340 | { |
| 341 | /* Inherit standard device info */ |
| 342 | usbnet_get_drvinfo(net, info); |
| 343 | info->eedump_len = DM_EEPROM_LEN; |
| 344 | } |
| 345 | |
| 346 | static u32 dm9601_get_link(struct net_device *net) |
| 347 | { |
| 348 | struct usbnet *dev = netdev_priv(net); |
| 349 | |
| 350 | return mii_link_ok(&dev->mii); |
| 351 | } |
| 352 | |
| 353 | static int dm9601_ioctl(struct net_device *net, struct ifreq *rq, int cmd) |
| 354 | { |
| 355 | struct usbnet *dev = netdev_priv(net); |
| 356 | |
| 357 | return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL); |
| 358 | } |
| 359 | |
Stephen Hemminger | 0fc0b73 | 2009-09-02 01:03:33 -0700 | [diff] [blame] | 360 | static const struct ethtool_ops dm9601_ethtool_ops = { |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 361 | .get_drvinfo = dm9601_get_drvinfo, |
| 362 | .get_link = dm9601_get_link, |
| 363 | .get_msglevel = usbnet_get_msglevel, |
| 364 | .set_msglevel = usbnet_set_msglevel, |
| 365 | .get_eeprom_len = dm9601_get_eeprom_len, |
| 366 | .get_eeprom = dm9601_get_eeprom, |
| 367 | .get_settings = usbnet_get_settings, |
| 368 | .set_settings = usbnet_set_settings, |
| 369 | .nway_reset = usbnet_nway_reset, |
| 370 | }; |
| 371 | |
| 372 | static void dm9601_set_multicast(struct net_device *net) |
| 373 | { |
| 374 | struct usbnet *dev = netdev_priv(net); |
| 375 | /* We use the 20 byte dev->data for our 8 byte filter buffer |
| 376 | * to avoid allocating memory that is tricky to free later */ |
| 377 | u8 *hashes = (u8 *) & dev->data; |
Peter Korsgaard | 33edded | 2008-03-18 23:16:54 +0100 | [diff] [blame] | 378 | u8 rx_ctl = 0x31; |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 379 | |
| 380 | memset(hashes, 0x00, DM_MCAST_SIZE); |
| 381 | hashes[DM_MCAST_SIZE - 1] |= 0x80; /* broadcast address */ |
| 382 | |
| 383 | if (net->flags & IFF_PROMISC) { |
| 384 | rx_ctl |= 0x02; |
Jiri Pirko | 2cc04d2 | 2010-02-05 02:51:24 +0000 | [diff] [blame] | 385 | } else if (net->flags & IFF_ALLMULTI || |
| 386 | netdev_mc_count(net) > DM_MAX_MCAST) { |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 387 | rx_ctl |= 0x04; |
Jiri Pirko | 2cc04d2 | 2010-02-05 02:51:24 +0000 | [diff] [blame] | 388 | } else if (!netdev_mc_empty(net)) { |
Jiri Pirko | 22bedad | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 389 | struct netdev_hw_addr *ha; |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 390 | |
Jiri Pirko | 22bedad | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 391 | netdev_for_each_mc_addr(ha, net) { |
| 392 | u32 crc = ether_crc(ETH_ALEN, ha->addr) >> 26; |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 393 | hashes[crc >> 3] |= 1 << (crc & 0x7); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | dm_write_async(dev, DM_MCAST_ADDR, DM_MCAST_SIZE, hashes); |
| 398 | dm_write_reg_async(dev, DM_RX_CTRL, rx_ctl); |
| 399 | } |
| 400 | |
Wu Fengguang | 4b9f8ec | 2009-01-06 18:52:58 +0000 | [diff] [blame] | 401 | static void __dm9601_set_mac_address(struct usbnet *dev) |
| 402 | { |
| 403 | dm_write_async(dev, DM_PHY_ADDR, ETH_ALEN, dev->net->dev_addr); |
| 404 | } |
| 405 | |
Peter Korsgaard | 753dcfe | 2008-10-24 09:08:27 +0200 | [diff] [blame] | 406 | static int dm9601_set_mac_address(struct net_device *net, void *p) |
| 407 | { |
| 408 | struct sockaddr *addr = p; |
| 409 | struct usbnet *dev = netdev_priv(net); |
| 410 | |
Wu Fengguang | f52deb0 | 2009-01-08 10:47:01 -0800 | [diff] [blame] | 411 | if (!is_valid_ether_addr(addr->sa_data)) { |
| 412 | dev_err(&net->dev, "not setting invalid mac address %pM\n", |
| 413 | addr->sa_data); |
Peter Korsgaard | 753dcfe | 2008-10-24 09:08:27 +0200 | [diff] [blame] | 414 | return -EINVAL; |
Wu Fengguang | f52deb0 | 2009-01-08 10:47:01 -0800 | [diff] [blame] | 415 | } |
Peter Korsgaard | 753dcfe | 2008-10-24 09:08:27 +0200 | [diff] [blame] | 416 | |
| 417 | memcpy(net->dev_addr, addr->sa_data, net->addr_len); |
Wu Fengguang | 4b9f8ec | 2009-01-06 18:52:58 +0000 | [diff] [blame] | 418 | __dm9601_set_mac_address(dev); |
Peter Korsgaard | 753dcfe | 2008-10-24 09:08:27 +0200 | [diff] [blame] | 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
Stephen Hemminger | fe85ff8 | 2009-03-20 19:35:56 +0000 | [diff] [blame] | 423 | static const struct net_device_ops dm9601_netdev_ops = { |
| 424 | .ndo_open = usbnet_open, |
| 425 | .ndo_stop = usbnet_stop, |
| 426 | .ndo_start_xmit = usbnet_start_xmit, |
| 427 | .ndo_tx_timeout = usbnet_tx_timeout, |
| 428 | .ndo_change_mtu = usbnet_change_mtu, |
| 429 | .ndo_validate_addr = eth_validate_addr, |
| 430 | .ndo_do_ioctl = dm9601_ioctl, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 431 | .ndo_set_rx_mode = dm9601_set_multicast, |
Stephen Hemminger | fe85ff8 | 2009-03-20 19:35:56 +0000 | [diff] [blame] | 432 | .ndo_set_mac_address = dm9601_set_mac_address, |
| 433 | }; |
| 434 | |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 435 | static int dm9601_bind(struct usbnet *dev, struct usb_interface *intf) |
| 436 | { |
| 437 | int ret; |
Wu Fengguang | 20f10aa | 2009-01-06 10:55:10 -0800 | [diff] [blame] | 438 | u8 mac[ETH_ALEN]; |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 439 | |
| 440 | ret = usbnet_get_endpoints(dev, intf); |
| 441 | if (ret) |
| 442 | goto out; |
| 443 | |
Stephen Hemminger | fe85ff8 | 2009-03-20 19:35:56 +0000 | [diff] [blame] | 444 | dev->net->netdev_ops = &dm9601_netdev_ops; |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 445 | dev->net->ethtool_ops = &dm9601_ethtool_ops; |
| 446 | dev->net->hard_header_len += DM_TX_OVERHEAD; |
| 447 | dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len; |
Peter Korsgaard | f662fe5 | 2007-10-01 13:36:07 +0200 | [diff] [blame] | 448 | dev->rx_urb_size = dev->net->mtu + ETH_HLEN + DM_RX_OVERHEAD; |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 449 | |
| 450 | dev->mii.dev = dev->net; |
| 451 | dev->mii.mdio_read = dm9601_mdio_read; |
| 452 | dev->mii.mdio_write = dm9601_mdio_write; |
| 453 | dev->mii.phy_id_mask = 0x1f; |
| 454 | dev->mii.reg_num_mask = 0x1f; |
| 455 | |
| 456 | /* reset */ |
Peter Korsgaard | b8f5958 | 2007-06-30 23:07:42 +0200 | [diff] [blame] | 457 | dm_write_reg(dev, DM_NET_CTRL, 1); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 458 | udelay(20); |
| 459 | |
| 460 | /* read MAC */ |
Wu Fengguang | 20f10aa | 2009-01-06 10:55:10 -0800 | [diff] [blame] | 461 | if (dm_read(dev, DM_PHY_ADDR, ETH_ALEN, mac) < 0) { |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 462 | printk(KERN_ERR "Error reading MAC address\n"); |
| 463 | ret = -ENODEV; |
| 464 | goto out; |
| 465 | } |
| 466 | |
Wu Fengguang | 20f10aa | 2009-01-06 10:55:10 -0800 | [diff] [blame] | 467 | /* |
| 468 | * Overwrite the auto-generated address only with good ones. |
| 469 | */ |
| 470 | if (is_valid_ether_addr(mac)) |
| 471 | memcpy(dev->net->dev_addr, mac, ETH_ALEN); |
Wu Fengguang | f52deb0 | 2009-01-08 10:47:01 -0800 | [diff] [blame] | 472 | else { |
| 473 | printk(KERN_WARNING |
| 474 | "dm9601: No valid MAC address in EEPROM, using %pM\n", |
| 475 | dev->net->dev_addr); |
Wu Fengguang | 4b9f8ec | 2009-01-06 18:52:58 +0000 | [diff] [blame] | 476 | __dm9601_set_mac_address(dev); |
Wu Fengguang | f52deb0 | 2009-01-08 10:47:01 -0800 | [diff] [blame] | 477 | } |
Wu Fengguang | 20f10aa | 2009-01-06 10:55:10 -0800 | [diff] [blame] | 478 | |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 479 | /* power up phy */ |
| 480 | dm_write_reg(dev, DM_GPR_CTRL, 1); |
| 481 | dm_write_reg(dev, DM_GPR_DATA, 0); |
| 482 | |
| 483 | /* receive broadcast packets */ |
| 484 | dm9601_set_multicast(dev->net); |
| 485 | |
| 486 | dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET); |
| 487 | dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE, |
| 488 | ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP); |
| 489 | mii_nway_restart(&dev->mii); |
| 490 | |
| 491 | out: |
| 492 | return ret; |
| 493 | } |
| 494 | |
| 495 | static int dm9601_rx_fixup(struct usbnet *dev, struct sk_buff *skb) |
| 496 | { |
| 497 | u8 status; |
| 498 | int len; |
| 499 | |
| 500 | /* format: |
Peter Korsgaard | 61189c7 | 2009-05-13 10:09:53 +0000 | [diff] [blame] | 501 | b1: rx status |
| 502 | b2: packet length (incl crc) low |
| 503 | b3: packet length (incl crc) high |
| 504 | b4..n-4: packet data |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 505 | bn-3..bn: ethernet crc |
| 506 | */ |
| 507 | |
| 508 | if (unlikely(skb->len < DM_RX_OVERHEAD)) { |
| 509 | dev_err(&dev->udev->dev, "unexpected tiny rx frame\n"); |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | status = skb->data[0]; |
| 514 | len = (skb->data[1] | (skb->data[2] << 8)) - 4; |
| 515 | |
| 516 | if (unlikely(status & 0xbf)) { |
Herbert Xu | 9612101 | 2009-06-29 16:50:51 +0000 | [diff] [blame] | 517 | if (status & 0x01) dev->net->stats.rx_fifo_errors++; |
| 518 | if (status & 0x02) dev->net->stats.rx_crc_errors++; |
| 519 | if (status & 0x04) dev->net->stats.rx_frame_errors++; |
| 520 | if (status & 0x20) dev->net->stats.rx_missed_errors++; |
| 521 | if (status & 0x90) dev->net->stats.rx_length_errors++; |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | skb_pull(skb, 3); |
| 526 | skb_trim(skb, len); |
| 527 | |
| 528 | return 1; |
| 529 | } |
| 530 | |
| 531 | static struct sk_buff *dm9601_tx_fixup(struct usbnet *dev, struct sk_buff *skb, |
| 532 | gfp_t flags) |
| 533 | { |
| 534 | int len; |
| 535 | |
| 536 | /* format: |
Peter Korsgaard | 61189c7 | 2009-05-13 10:09:53 +0000 | [diff] [blame] | 537 | b1: packet length low |
| 538 | b2: packet length high |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 539 | b3..n: packet data |
| 540 | */ |
| 541 | |
Peter Korsgaard | 23de559 | 2007-06-27 08:14:50 +0200 | [diff] [blame] | 542 | len = skb->len; |
| 543 | |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 544 | if (skb_headroom(skb) < DM_TX_OVERHEAD) { |
| 545 | struct sk_buff *skb2; |
| 546 | |
| 547 | skb2 = skb_copy_expand(skb, DM_TX_OVERHEAD, 0, flags); |
| 548 | dev_kfree_skb_any(skb); |
| 549 | skb = skb2; |
| 550 | if (!skb) |
| 551 | return NULL; |
| 552 | } |
| 553 | |
| 554 | __skb_push(skb, DM_TX_OVERHEAD); |
| 555 | |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 556 | /* usbnet adds padding if length is a multiple of packet size |
| 557 | if so, adjust length value in header */ |
Peter Korsgaard | 23de559 | 2007-06-27 08:14:50 +0200 | [diff] [blame] | 558 | if ((skb->len % dev->maxpacket) == 0) |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 559 | len++; |
| 560 | |
| 561 | skb->data[0] = len; |
| 562 | skb->data[1] = len >> 8; |
| 563 | |
| 564 | return skb; |
| 565 | } |
| 566 | |
| 567 | static void dm9601_status(struct usbnet *dev, struct urb *urb) |
| 568 | { |
| 569 | int link; |
| 570 | u8 *buf; |
| 571 | |
| 572 | /* format: |
| 573 | b0: net status |
| 574 | b1: tx status 1 |
| 575 | b2: tx status 2 |
| 576 | b3: rx status |
| 577 | b4: rx overflow |
| 578 | b5: rx count |
| 579 | b6: tx count |
| 580 | b7: gpr |
| 581 | */ |
| 582 | |
| 583 | if (urb->actual_length < 8) |
| 584 | return; |
| 585 | |
| 586 | buf = urb->transfer_buffer; |
| 587 | |
| 588 | link = !!(buf[0] & 0x40); |
| 589 | if (netif_carrier_ok(dev->net) != link) { |
| 590 | if (link) { |
| 591 | netif_carrier_on(dev->net); |
| 592 | usbnet_defer_kevent (dev, EVENT_LINK_RESET); |
| 593 | } |
| 594 | else |
| 595 | netif_carrier_off(dev->net); |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 596 | netdev_dbg(dev->net, "Link Status is: %d\n", link); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
| 600 | static int dm9601_link_reset(struct usbnet *dev) |
| 601 | { |
David Decotigny | 8ae6dac | 2011-04-27 18:32:38 +0000 | [diff] [blame] | 602 | struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET }; |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 603 | |
| 604 | mii_check_media(&dev->mii, 1, 1); |
| 605 | mii_ethtool_gset(&dev->mii, &ecmd); |
| 606 | |
David Decotigny | 8ae6dac | 2011-04-27 18:32:38 +0000 | [diff] [blame] | 607 | netdev_dbg(dev->net, "link_reset() speed: %u duplex: %d\n", |
| 608 | ethtool_cmd_speed(&ecmd), ecmd.duplex); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | static const struct driver_info dm9601_info = { |
| 614 | .description = "Davicom DM9601 USB Ethernet", |
Ben Hutchings | 37e8273 | 2009-11-04 15:29:52 +0000 | [diff] [blame] | 615 | .flags = FLAG_ETHER | FLAG_LINK_INTR, |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 616 | .bind = dm9601_bind, |
| 617 | .rx_fixup = dm9601_rx_fixup, |
| 618 | .tx_fixup = dm9601_tx_fixup, |
| 619 | .status = dm9601_status, |
| 620 | .link_reset = dm9601_link_reset, |
| 621 | .reset = dm9601_link_reset, |
| 622 | }; |
| 623 | |
| 624 | static const struct usb_device_id products[] = { |
| 625 | { |
YOSHIFUJI Hideaki | 6438ac2 | 2007-03-02 20:33:55 -0500 | [diff] [blame] | 626 | USB_DEVICE(0x07aa, 0x9601), /* Corega FEther USB-TXC */ |
| 627 | .driver_info = (unsigned long)&dm9601_info, |
| 628 | }, |
| 629 | { |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 630 | USB_DEVICE(0x0a46, 0x9601), /* Davicom USB-100 */ |
| 631 | .driver_info = (unsigned long)&dm9601_info, |
| 632 | }, |
Jon Dowland | 6dc477f | 2007-03-11 20:03:03 +0000 | [diff] [blame] | 633 | { |
| 634 | USB_DEVICE(0x0a46, 0x6688), /* ZT6688 USB NIC */ |
| 635 | .driver_info = (unsigned long)&dm9601_info, |
| 636 | }, |
| 637 | { |
| 638 | USB_DEVICE(0x0a46, 0x0268), /* ShanTou ST268 USB NIC */ |
| 639 | .driver_info = (unsigned long)&dm9601_info, |
| 640 | }, |
Peter Korsgaard | a06da75 | 2007-10-30 14:23:47 +0100 | [diff] [blame] | 641 | { |
| 642 | USB_DEVICE(0x0a46, 0x8515), /* ADMtek ADM8515 USB NIC */ |
| 643 | .driver_info = (unsigned long)&dm9601_info, |
| 644 | }, |
Peter Korsgaard | b47b4b2 | 2008-03-18 23:16:53 +0100 | [diff] [blame] | 645 | { |
| 646 | USB_DEVICE(0x0a47, 0x9601), /* Hirose USB-100 */ |
| 647 | .driver_info = (unsigned long)&dm9601_info, |
| 648 | }, |
Peter Korsgaard | a1a69c8 | 2009-03-03 23:48:16 -0800 | [diff] [blame] | 649 | { |
| 650 | USB_DEVICE(0x0fe6, 0x8101), /* DM9601 USB to Fast Ethernet Adapter */ |
| 651 | .driver_info = (unsigned long)&dm9601_info, |
| 652 | }, |
Janusz Krzysztofik | 06b71b6 | 2009-10-28 05:34:21 +0000 | [diff] [blame] | 653 | { |
Shahar Havivi | 67158ce | 2011-02-22 04:41:11 +0000 | [diff] [blame] | 654 | USB_DEVICE(0x0fe6, 0x9700), /* DM9601 USB to Fast Ethernet Adapter */ |
| 655 | .driver_info = (unsigned long)&dm9601_info, |
| 656 | }, |
| 657 | { |
Janusz Krzysztofik | 06b71b6 | 2009-10-28 05:34:21 +0000 | [diff] [blame] | 658 | USB_DEVICE(0x0a46, 0x9000), /* DM9000E */ |
| 659 | .driver_info = (unsigned long)&dm9601_info, |
| 660 | }, |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 661 | {}, // END |
| 662 | }; |
| 663 | |
| 664 | MODULE_DEVICE_TABLE(usb, products); |
| 665 | |
| 666 | static struct usb_driver dm9601_driver = { |
| 667 | .name = "dm9601", |
| 668 | .id_table = products, |
| 669 | .probe = usbnet_probe, |
| 670 | .disconnect = usbnet_disconnect, |
| 671 | .suspend = usbnet_suspend, |
| 672 | .resume = usbnet_resume, |
| 673 | }; |
| 674 | |
Greg Kroah-Hartman | d632eb1 | 2011-11-18 09:44:20 -0800 | [diff] [blame] | 675 | module_usb_driver(dm9601_driver); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 676 | |
| 677 | MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>"); |
| 678 | MODULE_DESCRIPTION("Davicom DM9601 USB 1.1 ethernet devices"); |
| 679 | MODULE_LICENSE("GPL"); |