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