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> |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 24 | |
| 25 | /* datasheet: |
| 26 | http://www.davicom.com.tw/big5/download/Data%20Sheet/DM9601-DS-P01-930914.pdf |
| 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 |
| 47 | |
| 48 | #define DM_MAX_MCAST 64 |
| 49 | #define DM_MCAST_SIZE 8 |
| 50 | #define DM_EEPROM_LEN 256 |
| 51 | #define DM_TX_OVERHEAD 2 /* 2 byte header */ |
| 52 | #define DM_RX_OVERHEAD 7 /* 3 byte header + 4 byte crc tail */ |
| 53 | #define DM_TIMEOUT 1000 |
| 54 | |
| 55 | |
| 56 | static int dm_read(struct usbnet *dev, u8 reg, u16 length, void *data) |
| 57 | { |
Peter Korsgaard | 16d78bc25 | 2008-07-14 09:07:32 +0200 | [diff] [blame^] | 58 | void *buf; |
| 59 | int err = -ENOMEM; |
| 60 | |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 61 | devdbg(dev, "dm_read() reg=0x%02x length=%d", reg, length); |
Peter Korsgaard | 16d78bc25 | 2008-07-14 09:07:32 +0200 | [diff] [blame^] | 62 | |
| 63 | buf = kmalloc(length, GFP_KERNEL); |
| 64 | if (!buf) |
| 65 | goto out; |
| 66 | |
| 67 | err = usb_control_msg(dev->udev, |
| 68 | usb_rcvctrlpipe(dev->udev, 0), |
| 69 | DM_READ_REGS, |
| 70 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 71 | 0, reg, buf, length, USB_CTRL_SET_TIMEOUT); |
| 72 | if (err == length) |
| 73 | memcpy(data, buf, length); |
| 74 | else if (err >= 0) |
| 75 | err = -EINVAL; |
| 76 | kfree(buf); |
| 77 | |
| 78 | out: |
| 79 | return err; |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static int dm_read_reg(struct usbnet *dev, u8 reg, u8 *value) |
| 83 | { |
| 84 | return dm_read(dev, reg, 1, value); |
| 85 | } |
| 86 | |
| 87 | static int dm_write(struct usbnet *dev, u8 reg, u16 length, void *data) |
| 88 | { |
Peter Korsgaard | 16d78bc25 | 2008-07-14 09:07:32 +0200 | [diff] [blame^] | 89 | void *buf = NULL; |
| 90 | int err = -ENOMEM; |
| 91 | |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 92 | devdbg(dev, "dm_write() reg=0x%02x, length=%d", reg, length); |
Peter Korsgaard | 16d78bc25 | 2008-07-14 09:07:32 +0200 | [diff] [blame^] | 93 | |
| 94 | if (data) { |
| 95 | buf = kmalloc(length, GFP_KERNEL); |
| 96 | if (!buf) |
| 97 | goto out; |
| 98 | memcpy(buf, data, length); |
| 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 | { |
| 115 | devdbg(dev, "dm_write_reg() reg=0x%02x, value=0x%02x", reg, value); |
| 116 | return usb_control_msg(dev->udev, |
| 117 | usb_sndctrlpipe(dev->udev, 0), |
| 118 | DM_WRITE_REG, |
| 119 | USB_DIR_OUT | USB_TYPE_VENDOR |USB_RECIP_DEVICE, |
Randy Dunlap | 686149f | 2007-03-16 18:33:13 -0700 | [diff] [blame] | 120 | value, reg, NULL, 0, USB_CTRL_SET_TIMEOUT); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static void dm_write_async_callback(struct urb *urb) |
| 124 | { |
| 125 | struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context; |
| 126 | |
| 127 | if (urb->status < 0) |
Peter Korsgaard | 77b6901 | 2007-11-15 11:01:02 +0100 | [diff] [blame] | 128 | printk(KERN_DEBUG "dm_write_async_callback() failed with %d\n", |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 129 | urb->status); |
| 130 | |
| 131 | kfree(req); |
| 132 | usb_free_urb(urb); |
| 133 | } |
| 134 | |
Peter Korsgaard | ba734f3 | 2007-11-15 11:10:12 +0100 | [diff] [blame] | 135 | static void dm_write_async_helper(struct usbnet *dev, u8 reg, u8 value, |
| 136 | u16 length, void *data) |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 137 | { |
| 138 | struct usb_ctrlrequest *req; |
| 139 | struct urb *urb; |
| 140 | int status; |
| 141 | |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 142 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 143 | if (!urb) { |
Peter Korsgaard | ba734f3 | 2007-11-15 11:10:12 +0100 | [diff] [blame] | 144 | deverr(dev, "Error allocating URB in dm_write_async_helper!"); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 145 | return; |
| 146 | } |
| 147 | |
| 148 | req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC); |
| 149 | if (!req) { |
| 150 | deverr(dev, "Failed to allocate memory for control request"); |
| 151 | usb_free_urb(urb); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE; |
Peter Korsgaard | ba734f3 | 2007-11-15 11:10:12 +0100 | [diff] [blame] | 156 | req->bRequest = length ? DM_WRITE_REGS : DM_WRITE_REG; |
| 157 | req->wValue = cpu_to_le16(value); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 158 | req->wIndex = cpu_to_le16(reg); |
| 159 | req->wLength = cpu_to_le16(length); |
| 160 | |
| 161 | usb_fill_control_urb(urb, dev->udev, |
| 162 | usb_sndctrlpipe(dev->udev, 0), |
| 163 | (void *)req, data, length, |
| 164 | dm_write_async_callback, req); |
| 165 | |
| 166 | status = usb_submit_urb(urb, GFP_ATOMIC); |
| 167 | if (status < 0) { |
| 168 | deverr(dev, "Error submitting the control message: status=%d", |
| 169 | status); |
| 170 | kfree(req); |
| 171 | usb_free_urb(urb); |
| 172 | } |
| 173 | } |
| 174 | |
Peter Korsgaard | ba734f3 | 2007-11-15 11:10:12 +0100 | [diff] [blame] | 175 | static void dm_write_async(struct usbnet *dev, u8 reg, u16 length, void *data) |
| 176 | { |
| 177 | devdbg(dev, "dm_write_async() reg=0x%02x length=%d", reg, length); |
| 178 | |
| 179 | dm_write_async_helper(dev, reg, 0, length, data); |
| 180 | } |
| 181 | |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 182 | static void dm_write_reg_async(struct usbnet *dev, u8 reg, u8 value) |
| 183 | { |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 184 | devdbg(dev, "dm_write_reg_async() reg=0x%02x value=0x%02x", |
| 185 | reg, value); |
| 186 | |
Peter Korsgaard | ba734f3 | 2007-11-15 11:10:12 +0100 | [diff] [blame] | 187 | dm_write_async_helper(dev, reg, value, 0, NULL); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Al Viro | eca1ad8 | 2008-03-16 22:21:54 +0000 | [diff] [blame] | 190 | 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] | 191 | { |
| 192 | int ret, i; |
| 193 | |
| 194 | mutex_lock(&dev->phy_mutex); |
| 195 | |
| 196 | dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg); |
| 197 | dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0xc : 0x4); |
| 198 | |
| 199 | for (i = 0; i < DM_TIMEOUT; i++) { |
| 200 | u8 tmp; |
| 201 | |
| 202 | udelay(1); |
| 203 | ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp); |
| 204 | if (ret < 0) |
| 205 | goto out; |
| 206 | |
| 207 | /* ready */ |
| 208 | if ((tmp & 1) == 0) |
| 209 | break; |
| 210 | } |
| 211 | |
| 212 | if (i == DM_TIMEOUT) { |
| 213 | deverr(dev, "%s read timed out!", phy ? "phy" : "eeprom"); |
| 214 | ret = -EIO; |
| 215 | goto out; |
| 216 | } |
| 217 | |
| 218 | dm_write_reg(dev, DM_SHARED_CTRL, 0x0); |
| 219 | ret = dm_read(dev, DM_SHARED_DATA, 2, value); |
| 220 | |
| 221 | devdbg(dev, "read shared %d 0x%02x returned 0x%04x, %d", |
| 222 | phy, reg, *value, ret); |
| 223 | |
| 224 | out: |
| 225 | mutex_unlock(&dev->phy_mutex); |
| 226 | return ret; |
| 227 | } |
| 228 | |
Al Viro | eca1ad8 | 2008-03-16 22:21:54 +0000 | [diff] [blame] | 229 | 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] | 230 | { |
| 231 | int ret, i; |
| 232 | |
| 233 | mutex_lock(&dev->phy_mutex); |
| 234 | |
| 235 | ret = dm_write(dev, DM_SHARED_DATA, 2, &value); |
| 236 | if (ret < 0) |
| 237 | goto out; |
| 238 | |
| 239 | dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg); |
| 240 | dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0x1c : 0x14); |
| 241 | |
| 242 | for (i = 0; i < DM_TIMEOUT; i++) { |
| 243 | u8 tmp; |
| 244 | |
| 245 | udelay(1); |
| 246 | ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp); |
| 247 | if (ret < 0) |
| 248 | goto out; |
| 249 | |
| 250 | /* ready */ |
| 251 | if ((tmp & 1) == 0) |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | if (i == DM_TIMEOUT) { |
| 256 | deverr(dev, "%s write timed out!", phy ? "phy" : "eeprom"); |
| 257 | ret = -EIO; |
| 258 | goto out; |
| 259 | } |
| 260 | |
| 261 | dm_write_reg(dev, DM_SHARED_CTRL, 0x0); |
| 262 | |
| 263 | out: |
| 264 | mutex_unlock(&dev->phy_mutex); |
| 265 | return ret; |
| 266 | } |
| 267 | |
| 268 | static int dm_read_eeprom_word(struct usbnet *dev, u8 offset, void *value) |
| 269 | { |
| 270 | return dm_read_shared_word(dev, 0, offset, value); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | |
| 275 | static int dm9601_get_eeprom_len(struct net_device *dev) |
| 276 | { |
| 277 | return DM_EEPROM_LEN; |
| 278 | } |
| 279 | |
| 280 | static int dm9601_get_eeprom(struct net_device *net, |
| 281 | struct ethtool_eeprom *eeprom, u8 * data) |
| 282 | { |
| 283 | struct usbnet *dev = netdev_priv(net); |
Al Viro | eca1ad8 | 2008-03-16 22:21:54 +0000 | [diff] [blame] | 284 | __le16 *ebuf = (__le16 *) data; |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 285 | int i; |
| 286 | |
| 287 | /* access is 16bit */ |
| 288 | if ((eeprom->offset % 2) || (eeprom->len % 2)) |
| 289 | return -EINVAL; |
| 290 | |
| 291 | for (i = 0; i < eeprom->len / 2; i++) { |
| 292 | if (dm_read_eeprom_word(dev, eeprom->offset / 2 + i, |
| 293 | &ebuf[i]) < 0) |
| 294 | return -EINVAL; |
| 295 | } |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | static int dm9601_mdio_read(struct net_device *netdev, int phy_id, int loc) |
| 300 | { |
| 301 | struct usbnet *dev = netdev_priv(netdev); |
| 302 | |
Al Viro | eca1ad8 | 2008-03-16 22:21:54 +0000 | [diff] [blame] | 303 | __le16 res; |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 304 | |
| 305 | if (phy_id) { |
| 306 | devdbg(dev, "Only internal phy supported"); |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | dm_read_shared_word(dev, 1, loc, &res); |
| 311 | |
| 312 | devdbg(dev, |
| 313 | "dm9601_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x", |
| 314 | phy_id, loc, le16_to_cpu(res)); |
| 315 | |
| 316 | return le16_to_cpu(res); |
| 317 | } |
| 318 | |
| 319 | static void dm9601_mdio_write(struct net_device *netdev, int phy_id, int loc, |
| 320 | int val) |
| 321 | { |
| 322 | struct usbnet *dev = netdev_priv(netdev); |
Al Viro | eca1ad8 | 2008-03-16 22:21:54 +0000 | [diff] [blame] | 323 | __le16 res = cpu_to_le16(val); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 324 | |
| 325 | if (phy_id) { |
| 326 | devdbg(dev, "Only internal phy supported"); |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | devdbg(dev,"dm9601_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x", |
| 331 | phy_id, loc, val); |
| 332 | |
| 333 | dm_write_shared_word(dev, 1, loc, res); |
| 334 | } |
| 335 | |
| 336 | static void dm9601_get_drvinfo(struct net_device *net, |
| 337 | struct ethtool_drvinfo *info) |
| 338 | { |
| 339 | /* Inherit standard device info */ |
| 340 | usbnet_get_drvinfo(net, info); |
| 341 | info->eedump_len = DM_EEPROM_LEN; |
| 342 | } |
| 343 | |
| 344 | static u32 dm9601_get_link(struct net_device *net) |
| 345 | { |
| 346 | struct usbnet *dev = netdev_priv(net); |
| 347 | |
| 348 | return mii_link_ok(&dev->mii); |
| 349 | } |
| 350 | |
| 351 | static int dm9601_ioctl(struct net_device *net, struct ifreq *rq, int cmd) |
| 352 | { |
| 353 | struct usbnet *dev = netdev_priv(net); |
| 354 | |
| 355 | return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL); |
| 356 | } |
| 357 | |
| 358 | static struct ethtool_ops dm9601_ethtool_ops = { |
| 359 | .get_drvinfo = dm9601_get_drvinfo, |
| 360 | .get_link = dm9601_get_link, |
| 361 | .get_msglevel = usbnet_get_msglevel, |
| 362 | .set_msglevel = usbnet_set_msglevel, |
| 363 | .get_eeprom_len = dm9601_get_eeprom_len, |
| 364 | .get_eeprom = dm9601_get_eeprom, |
| 365 | .get_settings = usbnet_get_settings, |
| 366 | .set_settings = usbnet_set_settings, |
| 367 | .nway_reset = usbnet_nway_reset, |
| 368 | }; |
| 369 | |
| 370 | static void dm9601_set_multicast(struct net_device *net) |
| 371 | { |
| 372 | struct usbnet *dev = netdev_priv(net); |
| 373 | /* We use the 20 byte dev->data for our 8 byte filter buffer |
| 374 | * to avoid allocating memory that is tricky to free later */ |
| 375 | u8 *hashes = (u8 *) & dev->data; |
Peter Korsgaard | 33edded | 2008-03-18 23:16:54 +0100 | [diff] [blame] | 376 | u8 rx_ctl = 0x31; |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 377 | |
| 378 | memset(hashes, 0x00, DM_MCAST_SIZE); |
| 379 | hashes[DM_MCAST_SIZE - 1] |= 0x80; /* broadcast address */ |
| 380 | |
| 381 | if (net->flags & IFF_PROMISC) { |
| 382 | rx_ctl |= 0x02; |
| 383 | } else if (net->flags & IFF_ALLMULTI || net->mc_count > DM_MAX_MCAST) { |
| 384 | rx_ctl |= 0x04; |
| 385 | } else if (net->mc_count) { |
| 386 | struct dev_mc_list *mc_list = net->mc_list; |
| 387 | int i; |
| 388 | |
Peter Korsgaard | 47df976 | 2008-03-28 22:25:29 +0100 | [diff] [blame] | 389 | for (i = 0; i < net->mc_count; i++, mc_list = mc_list->next) { |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 390 | u32 crc = ether_crc(ETH_ALEN, mc_list->dmi_addr) >> 26; |
| 391 | hashes[crc >> 3] |= 1 << (crc & 0x7); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | dm_write_async(dev, DM_MCAST_ADDR, DM_MCAST_SIZE, hashes); |
| 396 | dm_write_reg_async(dev, DM_RX_CTRL, rx_ctl); |
| 397 | } |
| 398 | |
| 399 | static int dm9601_bind(struct usbnet *dev, struct usb_interface *intf) |
| 400 | { |
| 401 | int ret; |
| 402 | |
| 403 | ret = usbnet_get_endpoints(dev, intf); |
| 404 | if (ret) |
| 405 | goto out; |
| 406 | |
| 407 | dev->net->do_ioctl = dm9601_ioctl; |
| 408 | dev->net->set_multicast_list = dm9601_set_multicast; |
| 409 | dev->net->ethtool_ops = &dm9601_ethtool_ops; |
| 410 | dev->net->hard_header_len += DM_TX_OVERHEAD; |
| 411 | dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len; |
Peter Korsgaard | f662fe5 | 2007-10-01 13:36:07 +0200 | [diff] [blame] | 412 | dev->rx_urb_size = dev->net->mtu + ETH_HLEN + DM_RX_OVERHEAD; |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 413 | |
| 414 | dev->mii.dev = dev->net; |
| 415 | dev->mii.mdio_read = dm9601_mdio_read; |
| 416 | dev->mii.mdio_write = dm9601_mdio_write; |
| 417 | dev->mii.phy_id_mask = 0x1f; |
| 418 | dev->mii.reg_num_mask = 0x1f; |
| 419 | |
| 420 | /* reset */ |
Peter Korsgaard | b8f59586 | 2007-06-30 23:07:42 +0200 | [diff] [blame] | 421 | dm_write_reg(dev, DM_NET_CTRL, 1); |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 422 | udelay(20); |
| 423 | |
| 424 | /* read MAC */ |
Peter Korsgaard | b8f59586 | 2007-06-30 23:07:42 +0200 | [diff] [blame] | 425 | if (dm_read(dev, DM_PHY_ADDR, ETH_ALEN, dev->net->dev_addr) < 0) { |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 426 | printk(KERN_ERR "Error reading MAC address\n"); |
| 427 | ret = -ENODEV; |
| 428 | goto out; |
| 429 | } |
| 430 | |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 431 | /* power up phy */ |
| 432 | dm_write_reg(dev, DM_GPR_CTRL, 1); |
| 433 | dm_write_reg(dev, DM_GPR_DATA, 0); |
| 434 | |
| 435 | /* receive broadcast packets */ |
| 436 | dm9601_set_multicast(dev->net); |
| 437 | |
| 438 | dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET); |
| 439 | dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE, |
| 440 | ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP); |
| 441 | mii_nway_restart(&dev->mii); |
| 442 | |
| 443 | out: |
| 444 | return ret; |
| 445 | } |
| 446 | |
| 447 | static int dm9601_rx_fixup(struct usbnet *dev, struct sk_buff *skb) |
| 448 | { |
| 449 | u8 status; |
| 450 | int len; |
| 451 | |
| 452 | /* format: |
| 453 | b0: rx status |
| 454 | b1: packet length (incl crc) low |
| 455 | b2: packet length (incl crc) high |
| 456 | b3..n-4: packet data |
| 457 | bn-3..bn: ethernet crc |
| 458 | */ |
| 459 | |
| 460 | if (unlikely(skb->len < DM_RX_OVERHEAD)) { |
| 461 | dev_err(&dev->udev->dev, "unexpected tiny rx frame\n"); |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | status = skb->data[0]; |
| 466 | len = (skb->data[1] | (skb->data[2] << 8)) - 4; |
| 467 | |
| 468 | if (unlikely(status & 0xbf)) { |
| 469 | if (status & 0x01) dev->stats.rx_fifo_errors++; |
| 470 | if (status & 0x02) dev->stats.rx_crc_errors++; |
| 471 | if (status & 0x04) dev->stats.rx_frame_errors++; |
| 472 | if (status & 0x20) dev->stats.rx_missed_errors++; |
| 473 | if (status & 0x90) dev->stats.rx_length_errors++; |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | skb_pull(skb, 3); |
| 478 | skb_trim(skb, len); |
| 479 | |
| 480 | return 1; |
| 481 | } |
| 482 | |
| 483 | static struct sk_buff *dm9601_tx_fixup(struct usbnet *dev, struct sk_buff *skb, |
| 484 | gfp_t flags) |
| 485 | { |
| 486 | int len; |
| 487 | |
| 488 | /* format: |
| 489 | b0: packet length low |
| 490 | b1: packet length high |
| 491 | b3..n: packet data |
| 492 | */ |
| 493 | |
Peter Korsgaard | 23de559 | 2007-06-27 08:14:50 +0200 | [diff] [blame] | 494 | len = skb->len; |
| 495 | |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 496 | if (skb_headroom(skb) < DM_TX_OVERHEAD) { |
| 497 | struct sk_buff *skb2; |
| 498 | |
| 499 | skb2 = skb_copy_expand(skb, DM_TX_OVERHEAD, 0, flags); |
| 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 | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 508 | /* usbnet adds padding if length is a multiple of packet size |
| 509 | if so, adjust length value in header */ |
Peter Korsgaard | 23de559 | 2007-06-27 08:14:50 +0200 | [diff] [blame] | 510 | if ((skb->len % dev->maxpacket) == 0) |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 511 | len++; |
| 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) { |
| 542 | if (link) { |
| 543 | netif_carrier_on(dev->net); |
| 544 | usbnet_defer_kevent (dev, EVENT_LINK_RESET); |
| 545 | } |
| 546 | else |
| 547 | netif_carrier_off(dev->net); |
| 548 | devdbg(dev, "Link Status is: %d", link); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | static int dm9601_link_reset(struct usbnet *dev) |
| 553 | { |
| 554 | struct ethtool_cmd ecmd; |
| 555 | |
| 556 | mii_check_media(&dev->mii, 1, 1); |
| 557 | mii_ethtool_gset(&dev->mii, &ecmd); |
| 558 | |
| 559 | devdbg(dev, "link_reset() speed: %d duplex: %d", |
| 560 | ecmd.speed, ecmd.duplex); |
| 561 | |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | static const struct driver_info dm9601_info = { |
| 566 | .description = "Davicom DM9601 USB Ethernet", |
| 567 | .flags = FLAG_ETHER, |
| 568 | .bind = dm9601_bind, |
| 569 | .rx_fixup = dm9601_rx_fixup, |
| 570 | .tx_fixup = dm9601_tx_fixup, |
| 571 | .status = dm9601_status, |
| 572 | .link_reset = dm9601_link_reset, |
| 573 | .reset = dm9601_link_reset, |
| 574 | }; |
| 575 | |
| 576 | static const struct usb_device_id products[] = { |
| 577 | { |
YOSHIFUJI Hideaki | 6438ac2 | 2007-03-02 20:33:55 -0500 | [diff] [blame] | 578 | USB_DEVICE(0x07aa, 0x9601), /* Corega FEther USB-TXC */ |
| 579 | .driver_info = (unsigned long)&dm9601_info, |
| 580 | }, |
| 581 | { |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 582 | USB_DEVICE(0x0a46, 0x9601), /* Davicom USB-100 */ |
| 583 | .driver_info = (unsigned long)&dm9601_info, |
| 584 | }, |
Jon Dowland | 6dc477f | 2007-03-11 20:03:03 +0000 | [diff] [blame] | 585 | { |
| 586 | USB_DEVICE(0x0a46, 0x6688), /* ZT6688 USB NIC */ |
| 587 | .driver_info = (unsigned long)&dm9601_info, |
| 588 | }, |
| 589 | { |
| 590 | USB_DEVICE(0x0a46, 0x0268), /* ShanTou ST268 USB NIC */ |
| 591 | .driver_info = (unsigned long)&dm9601_info, |
| 592 | }, |
Peter Korsgaard | a06da75 | 2007-10-30 14:23:47 +0100 | [diff] [blame] | 593 | { |
| 594 | USB_DEVICE(0x0a46, 0x8515), /* ADMtek ADM8515 USB NIC */ |
| 595 | .driver_info = (unsigned long)&dm9601_info, |
| 596 | }, |
Peter Korsgaard | b47b4b2 | 2008-03-18 23:16:53 +0100 | [diff] [blame] | 597 | { |
| 598 | USB_DEVICE(0x0a47, 0x9601), /* Hirose USB-100 */ |
| 599 | .driver_info = (unsigned long)&dm9601_info, |
| 600 | }, |
Peter Korsgaard | d0374f4 | 2007-02-16 17:03:54 +0100 | [diff] [blame] | 601 | {}, // END |
| 602 | }; |
| 603 | |
| 604 | MODULE_DEVICE_TABLE(usb, products); |
| 605 | |
| 606 | static struct usb_driver dm9601_driver = { |
| 607 | .name = "dm9601", |
| 608 | .id_table = products, |
| 609 | .probe = usbnet_probe, |
| 610 | .disconnect = usbnet_disconnect, |
| 611 | .suspend = usbnet_suspend, |
| 612 | .resume = usbnet_resume, |
| 613 | }; |
| 614 | |
| 615 | static int __init dm9601_init(void) |
| 616 | { |
| 617 | return usb_register(&dm9601_driver); |
| 618 | } |
| 619 | |
| 620 | static void __exit dm9601_exit(void) |
| 621 | { |
| 622 | usb_deregister(&dm9601_driver); |
| 623 | } |
| 624 | |
| 625 | module_init(dm9601_init); |
| 626 | module_exit(dm9601_exit); |
| 627 | |
| 628 | MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>"); |
| 629 | MODULE_DESCRIPTION("Davicom DM9601 USB 1.1 ethernet devices"); |
| 630 | MODULE_LICENSE("GPL"); |