thermal: Convert to devm_ioremap_resource()
[linux-2.6.git] / drivers / net / usb / dm9601.c
blob3f554c1149f36d2cf3221ee38e4f02507d566ba7
1 /*
2 * Davicom DM9601 USB 1.1 10/100Mbps ethernet devices
4 * Peter Korsgaard <jacmet@sunsite.dk>
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 */
11 //#define DEBUG
13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/stddef.h>
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>
23 #include <linux/usb/usbnet.h>
24 #include <linux/slab.h>
26 /* datasheet:
27 http://ptm2.cc.utu.fi/ftp/network/cards/DM9601/From_NET/DM9601-DS-P01-930914.pdf
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
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
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
57 static int dm_read(struct usbnet *dev, u8 reg, u16 length, void *data)
59 int err;
60 err = usbnet_read_cmd(dev, DM_READ_REGS,
61 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
62 0, reg, data, length);
63 if(err != length && err >= 0)
64 err = -EINVAL;
65 return err;
68 static int dm_read_reg(struct usbnet *dev, u8 reg, u8 *value)
70 return dm_read(dev, reg, 1, value);
73 static int dm_write(struct usbnet *dev, u8 reg, u16 length, void *data)
75 int err;
76 err = usbnet_write_cmd(dev, DM_WRITE_REGS,
77 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
78 0, reg, data, length);
80 if (err >= 0 && err < length)
81 err = -EINVAL;
82 return err;
85 static int dm_write_reg(struct usbnet *dev, u8 reg, u8 value)
87 return usbnet_write_cmd(dev, DM_WRITE_REGS,
88 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
89 value, reg, NULL, 0);
92 static void dm_write_async_helper(struct usbnet *dev, u8 reg, u8 value,
93 u16 length, void *data)
95 usbnet_write_cmd_async(dev, DM_WRITE_REGS,
96 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
97 value, reg, data, length);
100 static void dm_write_async(struct usbnet *dev, u8 reg, u16 length, void *data)
102 netdev_dbg(dev->net, "dm_write_async() reg=0x%02x length=%d\n", reg, length);
104 dm_write_async_helper(dev, reg, 0, length, data);
107 static void dm_write_reg_async(struct usbnet *dev, u8 reg, u8 value)
109 netdev_dbg(dev->net, "dm_write_reg_async() reg=0x%02x value=0x%02x\n",
110 reg, value);
112 dm_write_async_helper(dev, reg, value, 0, NULL);
115 static int dm_read_shared_word(struct usbnet *dev, int phy, u8 reg, __le16 *value)
117 int ret, i;
119 mutex_lock(&dev->phy_mutex);
121 dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg);
122 dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0xc : 0x4);
124 for (i = 0; i < DM_TIMEOUT; i++) {
125 u8 tmp;
127 udelay(1);
128 ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp);
129 if (ret < 0)
130 goto out;
132 /* ready */
133 if ((tmp & 1) == 0)
134 break;
137 if (i == DM_TIMEOUT) {
138 netdev_err(dev->net, "%s read timed out!\n", phy ? "phy" : "eeprom");
139 ret = -EIO;
140 goto out;
143 dm_write_reg(dev, DM_SHARED_CTRL, 0x0);
144 ret = dm_read(dev, DM_SHARED_DATA, 2, value);
146 netdev_dbg(dev->net, "read shared %d 0x%02x returned 0x%04x, %d\n",
147 phy, reg, *value, ret);
149 out:
150 mutex_unlock(&dev->phy_mutex);
151 return ret;
154 static int dm_write_shared_word(struct usbnet *dev, int phy, u8 reg, __le16 value)
156 int ret, i;
158 mutex_lock(&dev->phy_mutex);
160 ret = dm_write(dev, DM_SHARED_DATA, 2, &value);
161 if (ret < 0)
162 goto out;
164 dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg);
165 dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0x1a : 0x12);
167 for (i = 0; i < DM_TIMEOUT; i++) {
168 u8 tmp;
170 udelay(1);
171 ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp);
172 if (ret < 0)
173 goto out;
175 /* ready */
176 if ((tmp & 1) == 0)
177 break;
180 if (i == DM_TIMEOUT) {
181 netdev_err(dev->net, "%s write timed out!\n", phy ? "phy" : "eeprom");
182 ret = -EIO;
183 goto out;
186 dm_write_reg(dev, DM_SHARED_CTRL, 0x0);
188 out:
189 mutex_unlock(&dev->phy_mutex);
190 return ret;
193 static int dm_read_eeprom_word(struct usbnet *dev, u8 offset, void *value)
195 return dm_read_shared_word(dev, 0, offset, value);
200 static int dm9601_get_eeprom_len(struct net_device *dev)
202 return DM_EEPROM_LEN;
205 static int dm9601_get_eeprom(struct net_device *net,
206 struct ethtool_eeprom *eeprom, u8 * data)
208 struct usbnet *dev = netdev_priv(net);
209 __le16 *ebuf = (__le16 *) data;
210 int i;
212 /* access is 16bit */
213 if ((eeprom->offset % 2) || (eeprom->len % 2))
214 return -EINVAL;
216 for (i = 0; i < eeprom->len / 2; i++) {
217 if (dm_read_eeprom_word(dev, eeprom->offset / 2 + i,
218 &ebuf[i]) < 0)
219 return -EINVAL;
221 return 0;
224 static int dm9601_mdio_read(struct net_device *netdev, int phy_id, int loc)
226 struct usbnet *dev = netdev_priv(netdev);
228 __le16 res;
230 if (phy_id) {
231 netdev_dbg(dev->net, "Only internal phy supported\n");
232 return 0;
235 dm_read_shared_word(dev, 1, loc, &res);
237 netdev_dbg(dev->net,
238 "dm9601_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
239 phy_id, loc, le16_to_cpu(res));
241 return le16_to_cpu(res);
244 static void dm9601_mdio_write(struct net_device *netdev, int phy_id, int loc,
245 int val)
247 struct usbnet *dev = netdev_priv(netdev);
248 __le16 res = cpu_to_le16(val);
250 if (phy_id) {
251 netdev_dbg(dev->net, "Only internal phy supported\n");
252 return;
255 netdev_dbg(dev->net, "dm9601_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
256 phy_id, loc, val);
258 dm_write_shared_word(dev, 1, loc, res);
261 static void dm9601_get_drvinfo(struct net_device *net,
262 struct ethtool_drvinfo *info)
264 /* Inherit standard device info */
265 usbnet_get_drvinfo(net, info);
266 info->eedump_len = DM_EEPROM_LEN;
269 static u32 dm9601_get_link(struct net_device *net)
271 struct usbnet *dev = netdev_priv(net);
273 return mii_link_ok(&dev->mii);
276 static int dm9601_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
278 struct usbnet *dev = netdev_priv(net);
280 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
283 static const struct ethtool_ops dm9601_ethtool_ops = {
284 .get_drvinfo = dm9601_get_drvinfo,
285 .get_link = dm9601_get_link,
286 .get_msglevel = usbnet_get_msglevel,
287 .set_msglevel = usbnet_set_msglevel,
288 .get_eeprom_len = dm9601_get_eeprom_len,
289 .get_eeprom = dm9601_get_eeprom,
290 .get_settings = usbnet_get_settings,
291 .set_settings = usbnet_set_settings,
292 .nway_reset = usbnet_nway_reset,
295 static void dm9601_set_multicast(struct net_device *net)
297 struct usbnet *dev = netdev_priv(net);
298 /* We use the 20 byte dev->data for our 8 byte filter buffer
299 * to avoid allocating memory that is tricky to free later */
300 u8 *hashes = (u8 *) & dev->data;
301 u8 rx_ctl = 0x31;
303 memset(hashes, 0x00, DM_MCAST_SIZE);
304 hashes[DM_MCAST_SIZE - 1] |= 0x80; /* broadcast address */
306 if (net->flags & IFF_PROMISC) {
307 rx_ctl |= 0x02;
308 } else if (net->flags & IFF_ALLMULTI ||
309 netdev_mc_count(net) > DM_MAX_MCAST) {
310 rx_ctl |= 0x04;
311 } else if (!netdev_mc_empty(net)) {
312 struct netdev_hw_addr *ha;
314 netdev_for_each_mc_addr(ha, net) {
315 u32 crc = ether_crc(ETH_ALEN, ha->addr) >> 26;
316 hashes[crc >> 3] |= 1 << (crc & 0x7);
320 dm_write_async(dev, DM_MCAST_ADDR, DM_MCAST_SIZE, hashes);
321 dm_write_reg_async(dev, DM_RX_CTRL, rx_ctl);
324 static void __dm9601_set_mac_address(struct usbnet *dev)
326 dm_write_async(dev, DM_PHY_ADDR, ETH_ALEN, dev->net->dev_addr);
329 static int dm9601_set_mac_address(struct net_device *net, void *p)
331 struct sockaddr *addr = p;
332 struct usbnet *dev = netdev_priv(net);
334 if (!is_valid_ether_addr(addr->sa_data)) {
335 dev_err(&net->dev, "not setting invalid mac address %pM\n",
336 addr->sa_data);
337 return -EINVAL;
340 memcpy(net->dev_addr, addr->sa_data, net->addr_len);
341 __dm9601_set_mac_address(dev);
343 return 0;
346 static const struct net_device_ops dm9601_netdev_ops = {
347 .ndo_open = usbnet_open,
348 .ndo_stop = usbnet_stop,
349 .ndo_start_xmit = usbnet_start_xmit,
350 .ndo_tx_timeout = usbnet_tx_timeout,
351 .ndo_change_mtu = usbnet_change_mtu,
352 .ndo_validate_addr = eth_validate_addr,
353 .ndo_do_ioctl = dm9601_ioctl,
354 .ndo_set_rx_mode = dm9601_set_multicast,
355 .ndo_set_mac_address = dm9601_set_mac_address,
358 static int dm9601_bind(struct usbnet *dev, struct usb_interface *intf)
360 int ret;
361 u8 mac[ETH_ALEN];
363 ret = usbnet_get_endpoints(dev, intf);
364 if (ret)
365 goto out;
367 dev->net->netdev_ops = &dm9601_netdev_ops;
368 dev->net->ethtool_ops = &dm9601_ethtool_ops;
369 dev->net->hard_header_len += DM_TX_OVERHEAD;
370 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
371 dev->rx_urb_size = dev->net->mtu + ETH_HLEN + DM_RX_OVERHEAD;
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;
379 /* reset */
380 dm_write_reg(dev, DM_NET_CTRL, 1);
381 udelay(20);
383 /* read MAC */
384 if (dm_read(dev, DM_PHY_ADDR, ETH_ALEN, mac) < 0) {
385 printk(KERN_ERR "Error reading MAC address\n");
386 ret = -ENODEV;
387 goto out;
391 * Overwrite the auto-generated address only with good ones.
393 if (is_valid_ether_addr(mac))
394 memcpy(dev->net->dev_addr, mac, ETH_ALEN);
395 else {
396 printk(KERN_WARNING
397 "dm9601: No valid MAC address in EEPROM, using %pM\n",
398 dev->net->dev_addr);
399 __dm9601_set_mac_address(dev);
402 /* power up phy */
403 dm_write_reg(dev, DM_GPR_CTRL, 1);
404 dm_write_reg(dev, DM_GPR_DATA, 0);
406 /* receive broadcast packets */
407 dm9601_set_multicast(dev->net);
409 dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
410 dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
411 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
412 mii_nway_restart(&dev->mii);
414 out:
415 return ret;
418 static int dm9601_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
420 u8 status;
421 int len;
423 /* format:
424 b1: rx status
425 b2: packet length (incl crc) low
426 b3: packet length (incl crc) high
427 b4..n-4: packet data
428 bn-3..bn: ethernet crc
431 if (unlikely(skb->len < DM_RX_OVERHEAD)) {
432 dev_err(&dev->udev->dev, "unexpected tiny rx frame\n");
433 return 0;
436 status = skb->data[0];
437 len = (skb->data[1] | (skb->data[2] << 8)) - 4;
439 if (unlikely(status & 0xbf)) {
440 if (status & 0x01) dev->net->stats.rx_fifo_errors++;
441 if (status & 0x02) dev->net->stats.rx_crc_errors++;
442 if (status & 0x04) dev->net->stats.rx_frame_errors++;
443 if (status & 0x20) dev->net->stats.rx_missed_errors++;
444 if (status & 0x90) dev->net->stats.rx_length_errors++;
445 return 0;
448 skb_pull(skb, 3);
449 skb_trim(skb, len);
451 return 1;
454 static struct sk_buff *dm9601_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
455 gfp_t flags)
457 int len;
459 /* format:
460 b1: packet length low
461 b2: packet length high
462 b3..n: packet data
465 len = skb->len;
467 if (skb_headroom(skb) < DM_TX_OVERHEAD) {
468 struct sk_buff *skb2;
470 skb2 = skb_copy_expand(skb, DM_TX_OVERHEAD, 0, flags);
471 dev_kfree_skb_any(skb);
472 skb = skb2;
473 if (!skb)
474 return NULL;
477 __skb_push(skb, DM_TX_OVERHEAD);
479 /* usbnet adds padding if length is a multiple of packet size
480 if so, adjust length value in header */
481 if ((skb->len % dev->maxpacket) == 0)
482 len++;
484 skb->data[0] = len;
485 skb->data[1] = len >> 8;
487 return skb;
490 static void dm9601_status(struct usbnet *dev, struct urb *urb)
492 int link;
493 u8 *buf;
495 /* format:
496 b0: net status
497 b1: tx status 1
498 b2: tx status 2
499 b3: rx status
500 b4: rx overflow
501 b5: rx count
502 b6: tx count
503 b7: gpr
506 if (urb->actual_length < 8)
507 return;
509 buf = urb->transfer_buffer;
511 link = !!(buf[0] & 0x40);
512 if (netif_carrier_ok(dev->net) != link) {
513 if (link) {
514 netif_carrier_on(dev->net);
515 usbnet_defer_kevent (dev, EVENT_LINK_RESET);
517 else
518 netif_carrier_off(dev->net);
519 netdev_dbg(dev->net, "Link Status is: %d\n", link);
523 static int dm9601_link_reset(struct usbnet *dev)
525 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
527 mii_check_media(&dev->mii, 1, 1);
528 mii_ethtool_gset(&dev->mii, &ecmd);
530 netdev_dbg(dev->net, "link_reset() speed: %u duplex: %d\n",
531 ethtool_cmd_speed(&ecmd), ecmd.duplex);
533 return 0;
536 static const struct driver_info dm9601_info = {
537 .description = "Davicom DM9601 USB Ethernet",
538 .flags = FLAG_ETHER | FLAG_LINK_INTR,
539 .bind = dm9601_bind,
540 .rx_fixup = dm9601_rx_fixup,
541 .tx_fixup = dm9601_tx_fixup,
542 .status = dm9601_status,
543 .link_reset = dm9601_link_reset,
544 .reset = dm9601_link_reset,
547 static const struct usb_device_id products[] = {
549 USB_DEVICE(0x07aa, 0x9601), /* Corega FEther USB-TXC */
550 .driver_info = (unsigned long)&dm9601_info,
553 USB_DEVICE(0x0a46, 0x9601), /* Davicom USB-100 */
554 .driver_info = (unsigned long)&dm9601_info,
557 USB_DEVICE(0x0a46, 0x6688), /* ZT6688 USB NIC */
558 .driver_info = (unsigned long)&dm9601_info,
561 USB_DEVICE(0x0a46, 0x0268), /* ShanTou ST268 USB NIC */
562 .driver_info = (unsigned long)&dm9601_info,
565 USB_DEVICE(0x0a46, 0x8515), /* ADMtek ADM8515 USB NIC */
566 .driver_info = (unsigned long)&dm9601_info,
569 USB_DEVICE(0x0a47, 0x9601), /* Hirose USB-100 */
570 .driver_info = (unsigned long)&dm9601_info,
573 USB_DEVICE(0x0fe6, 0x8101), /* DM9601 USB to Fast Ethernet Adapter */
574 .driver_info = (unsigned long)&dm9601_info,
577 USB_DEVICE(0x0fe6, 0x9700), /* DM9601 USB to Fast Ethernet Adapter */
578 .driver_info = (unsigned long)&dm9601_info,
581 USB_DEVICE(0x0a46, 0x9000), /* DM9000E */
582 .driver_info = (unsigned long)&dm9601_info,
584 {}, // END
587 MODULE_DEVICE_TABLE(usb, products);
589 static struct usb_driver dm9601_driver = {
590 .name = "dm9601",
591 .id_table = products,
592 .probe = usbnet_probe,
593 .disconnect = usbnet_disconnect,
594 .suspend = usbnet_suspend,
595 .resume = usbnet_resume,
596 .disable_hub_initiated_lpm = 1,
599 module_usb_driver(dm9601_driver);
601 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
602 MODULE_DESCRIPTION("Davicom DM9601 USB 1.1 ethernet devices");
603 MODULE_LICENSE("GPL");