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