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