blob: 81ccdfaecc58bc9953a3c6d2d67a5419d31877e1 [file] [log] [blame]
Peter Korsgaardd0374f42007-02-16 17:03:54 +01001/*
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 Dunlap686149f2007-03-16 18:33:13 -070015#include <linux/stddef.h>
Peter Korsgaardd0374f42007-02-16 17:03:54 +010016#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 Kivilinna3692e942008-01-26 00:51:45 +020023#include <linux/usb/usbnet.h>
Peter Korsgaardd0374f42007-02-16 17:03:54 +010024
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
56static int dm_read(struct usbnet *dev, u8 reg, u16 length, void *data)
57{
58 devdbg(dev, "dm_read() reg=0x%02x length=%d", reg, length);
59 return usb_control_msg(dev->udev,
60 usb_rcvctrlpipe(dev->udev, 0),
61 DM_READ_REGS,
62 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
63 0, reg, data, length, USB_CTRL_SET_TIMEOUT);
64}
65
66static int dm_read_reg(struct usbnet *dev, u8 reg, u8 *value)
67{
68 return dm_read(dev, reg, 1, value);
69}
70
71static int dm_write(struct usbnet *dev, u8 reg, u16 length, void *data)
72{
73 devdbg(dev, "dm_write() reg=0x%02x, length=%d", reg, length);
74 return usb_control_msg(dev->udev,
75 usb_sndctrlpipe(dev->udev, 0),
76 DM_WRITE_REGS,
77 USB_DIR_OUT | USB_TYPE_VENDOR |USB_RECIP_DEVICE,
78 0, reg, data, length, USB_CTRL_SET_TIMEOUT);
79}
80
81static int dm_write_reg(struct usbnet *dev, u8 reg, u8 value)
82{
83 devdbg(dev, "dm_write_reg() reg=0x%02x, value=0x%02x", reg, value);
84 return usb_control_msg(dev->udev,
85 usb_sndctrlpipe(dev->udev, 0),
86 DM_WRITE_REG,
87 USB_DIR_OUT | USB_TYPE_VENDOR |USB_RECIP_DEVICE,
Randy Dunlap686149f2007-03-16 18:33:13 -070088 value, reg, NULL, 0, USB_CTRL_SET_TIMEOUT);
Peter Korsgaardd0374f42007-02-16 17:03:54 +010089}
90
91static void dm_write_async_callback(struct urb *urb)
92{
93 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
94
95 if (urb->status < 0)
Peter Korsgaard77b69012007-11-15 11:01:02 +010096 printk(KERN_DEBUG "dm_write_async_callback() failed with %d\n",
Peter Korsgaardd0374f42007-02-16 17:03:54 +010097 urb->status);
98
99 kfree(req);
100 usb_free_urb(urb);
101}
102
Peter Korsgaardba734f32007-11-15 11:10:12 +0100103static void dm_write_async_helper(struct usbnet *dev, u8 reg, u8 value,
104 u16 length, void *data)
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100105{
106 struct usb_ctrlrequest *req;
107 struct urb *urb;
108 int status;
109
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100110 urb = usb_alloc_urb(0, GFP_ATOMIC);
111 if (!urb) {
Peter Korsgaardba734f32007-11-15 11:10:12 +0100112 deverr(dev, "Error allocating URB in dm_write_async_helper!");
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100113 return;
114 }
115
116 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
117 if (!req) {
118 deverr(dev, "Failed to allocate memory for control request");
119 usb_free_urb(urb);
120 return;
121 }
122
123 req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
Peter Korsgaardba734f32007-11-15 11:10:12 +0100124 req->bRequest = length ? DM_WRITE_REGS : DM_WRITE_REG;
125 req->wValue = cpu_to_le16(value);
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100126 req->wIndex = cpu_to_le16(reg);
127 req->wLength = cpu_to_le16(length);
128
129 usb_fill_control_urb(urb, dev->udev,
130 usb_sndctrlpipe(dev->udev, 0),
131 (void *)req, data, length,
132 dm_write_async_callback, req);
133
134 status = usb_submit_urb(urb, GFP_ATOMIC);
135 if (status < 0) {
136 deverr(dev, "Error submitting the control message: status=%d",
137 status);
138 kfree(req);
139 usb_free_urb(urb);
140 }
141}
142
Peter Korsgaardba734f32007-11-15 11:10:12 +0100143static void dm_write_async(struct usbnet *dev, u8 reg, u16 length, void *data)
144{
145 devdbg(dev, "dm_write_async() reg=0x%02x length=%d", reg, length);
146
147 dm_write_async_helper(dev, reg, 0, length, data);
148}
149
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100150static void dm_write_reg_async(struct usbnet *dev, u8 reg, u8 value)
151{
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100152 devdbg(dev, "dm_write_reg_async() reg=0x%02x value=0x%02x",
153 reg, value);
154
Peter Korsgaardba734f32007-11-15 11:10:12 +0100155 dm_write_async_helper(dev, reg, value, 0, NULL);
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100156}
157
Al Viroeca1ad82008-03-16 22:21:54 +0000158static int dm_read_shared_word(struct usbnet *dev, int phy, u8 reg, __le16 *value)
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100159{
160 int ret, i;
161
162 mutex_lock(&dev->phy_mutex);
163
164 dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg);
165 dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0xc : 0x4);
166
167 for (i = 0; i < DM_TIMEOUT; i++) {
168 u8 tmp;
169
170 udelay(1);
171 ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp);
172 if (ret < 0)
173 goto out;
174
175 /* ready */
176 if ((tmp & 1) == 0)
177 break;
178 }
179
180 if (i == DM_TIMEOUT) {
181 deverr(dev, "%s read timed out!", phy ? "phy" : "eeprom");
182 ret = -EIO;
183 goto out;
184 }
185
186 dm_write_reg(dev, DM_SHARED_CTRL, 0x0);
187 ret = dm_read(dev, DM_SHARED_DATA, 2, value);
188
189 devdbg(dev, "read shared %d 0x%02x returned 0x%04x, %d",
190 phy, reg, *value, ret);
191
192 out:
193 mutex_unlock(&dev->phy_mutex);
194 return ret;
195}
196
Al Viroeca1ad82008-03-16 22:21:54 +0000197static int dm_write_shared_word(struct usbnet *dev, int phy, u8 reg, __le16 value)
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100198{
199 int ret, i;
200
201 mutex_lock(&dev->phy_mutex);
202
203 ret = dm_write(dev, DM_SHARED_DATA, 2, &value);
204 if (ret < 0)
205 goto out;
206
207 dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg);
208 dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0x1c : 0x14);
209
210 for (i = 0; i < DM_TIMEOUT; i++) {
211 u8 tmp;
212
213 udelay(1);
214 ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp);
215 if (ret < 0)
216 goto out;
217
218 /* ready */
219 if ((tmp & 1) == 0)
220 break;
221 }
222
223 if (i == DM_TIMEOUT) {
224 deverr(dev, "%s write timed out!", phy ? "phy" : "eeprom");
225 ret = -EIO;
226 goto out;
227 }
228
229 dm_write_reg(dev, DM_SHARED_CTRL, 0x0);
230
231out:
232 mutex_unlock(&dev->phy_mutex);
233 return ret;
234}
235
236static int dm_read_eeprom_word(struct usbnet *dev, u8 offset, void *value)
237{
238 return dm_read_shared_word(dev, 0, offset, value);
239}
240
241
242
243static int dm9601_get_eeprom_len(struct net_device *dev)
244{
245 return DM_EEPROM_LEN;
246}
247
248static int dm9601_get_eeprom(struct net_device *net,
249 struct ethtool_eeprom *eeprom, u8 * data)
250{
251 struct usbnet *dev = netdev_priv(net);
Al Viroeca1ad82008-03-16 22:21:54 +0000252 __le16 *ebuf = (__le16 *) data;
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100253 int i;
254
255 /* access is 16bit */
256 if ((eeprom->offset % 2) || (eeprom->len % 2))
257 return -EINVAL;
258
259 for (i = 0; i < eeprom->len / 2; i++) {
260 if (dm_read_eeprom_word(dev, eeprom->offset / 2 + i,
261 &ebuf[i]) < 0)
262 return -EINVAL;
263 }
264 return 0;
265}
266
267static int dm9601_mdio_read(struct net_device *netdev, int phy_id, int loc)
268{
269 struct usbnet *dev = netdev_priv(netdev);
270
Al Viroeca1ad82008-03-16 22:21:54 +0000271 __le16 res;
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100272
273 if (phy_id) {
274 devdbg(dev, "Only internal phy supported");
275 return 0;
276 }
277
278 dm_read_shared_word(dev, 1, loc, &res);
279
280 devdbg(dev,
281 "dm9601_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x",
282 phy_id, loc, le16_to_cpu(res));
283
284 return le16_to_cpu(res);
285}
286
287static void dm9601_mdio_write(struct net_device *netdev, int phy_id, int loc,
288 int val)
289{
290 struct usbnet *dev = netdev_priv(netdev);
Al Viroeca1ad82008-03-16 22:21:54 +0000291 __le16 res = cpu_to_le16(val);
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100292
293 if (phy_id) {
294 devdbg(dev, "Only internal phy supported");
295 return;
296 }
297
298 devdbg(dev,"dm9601_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x",
299 phy_id, loc, val);
300
301 dm_write_shared_word(dev, 1, loc, res);
302}
303
304static void dm9601_get_drvinfo(struct net_device *net,
305 struct ethtool_drvinfo *info)
306{
307 /* Inherit standard device info */
308 usbnet_get_drvinfo(net, info);
309 info->eedump_len = DM_EEPROM_LEN;
310}
311
312static u32 dm9601_get_link(struct net_device *net)
313{
314 struct usbnet *dev = netdev_priv(net);
315
316 return mii_link_ok(&dev->mii);
317}
318
319static int dm9601_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
320{
321 struct usbnet *dev = netdev_priv(net);
322
323 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
324}
325
326static struct ethtool_ops dm9601_ethtool_ops = {
327 .get_drvinfo = dm9601_get_drvinfo,
328 .get_link = dm9601_get_link,
329 .get_msglevel = usbnet_get_msglevel,
330 .set_msglevel = usbnet_set_msglevel,
331 .get_eeprom_len = dm9601_get_eeprom_len,
332 .get_eeprom = dm9601_get_eeprom,
333 .get_settings = usbnet_get_settings,
334 .set_settings = usbnet_set_settings,
335 .nway_reset = usbnet_nway_reset,
336};
337
338static void dm9601_set_multicast(struct net_device *net)
339{
340 struct usbnet *dev = netdev_priv(net);
341 /* We use the 20 byte dev->data for our 8 byte filter buffer
342 * to avoid allocating memory that is tricky to free later */
343 u8 *hashes = (u8 *) & dev->data;
344 u8 rx_ctl = 0x01;
345
346 memset(hashes, 0x00, DM_MCAST_SIZE);
347 hashes[DM_MCAST_SIZE - 1] |= 0x80; /* broadcast address */
348
349 if (net->flags & IFF_PROMISC) {
350 rx_ctl |= 0x02;
351 } else if (net->flags & IFF_ALLMULTI || net->mc_count > DM_MAX_MCAST) {
352 rx_ctl |= 0x04;
353 } else if (net->mc_count) {
354 struct dev_mc_list *mc_list = net->mc_list;
355 int i;
356
357 for (i = 0; i < net->mc_count; i++) {
358 u32 crc = ether_crc(ETH_ALEN, mc_list->dmi_addr) >> 26;
359 hashes[crc >> 3] |= 1 << (crc & 0x7);
360 }
361 }
362
363 dm_write_async(dev, DM_MCAST_ADDR, DM_MCAST_SIZE, hashes);
364 dm_write_reg_async(dev, DM_RX_CTRL, rx_ctl);
365}
366
367static int dm9601_bind(struct usbnet *dev, struct usb_interface *intf)
368{
369 int ret;
370
371 ret = usbnet_get_endpoints(dev, intf);
372 if (ret)
373 goto out;
374
375 dev->net->do_ioctl = dm9601_ioctl;
376 dev->net->set_multicast_list = dm9601_set_multicast;
377 dev->net->ethtool_ops = &dm9601_ethtool_ops;
378 dev->net->hard_header_len += DM_TX_OVERHEAD;
379 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
Peter Korsgaardf662fe52007-10-01 13:36:07 +0200380 dev->rx_urb_size = dev->net->mtu + ETH_HLEN + DM_RX_OVERHEAD;
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100381
382 dev->mii.dev = dev->net;
383 dev->mii.mdio_read = dm9601_mdio_read;
384 dev->mii.mdio_write = dm9601_mdio_write;
385 dev->mii.phy_id_mask = 0x1f;
386 dev->mii.reg_num_mask = 0x1f;
387
388 /* reset */
Peter Korsgaardb8f595862007-06-30 23:07:42 +0200389 dm_write_reg(dev, DM_NET_CTRL, 1);
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100390 udelay(20);
391
392 /* read MAC */
Peter Korsgaardb8f595862007-06-30 23:07:42 +0200393 if (dm_read(dev, DM_PHY_ADDR, ETH_ALEN, dev->net->dev_addr) < 0) {
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100394 printk(KERN_ERR "Error reading MAC address\n");
395 ret = -ENODEV;
396 goto out;
397 }
398
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100399 /* power up phy */
400 dm_write_reg(dev, DM_GPR_CTRL, 1);
401 dm_write_reg(dev, DM_GPR_DATA, 0);
402
403 /* receive broadcast packets */
404 dm9601_set_multicast(dev->net);
405
406 dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
407 dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
408 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
409 mii_nway_restart(&dev->mii);
410
411out:
412 return ret;
413}
414
415static int dm9601_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
416{
417 u8 status;
418 int len;
419
420 /* format:
421 b0: rx status
422 b1: packet length (incl crc) low
423 b2: packet length (incl crc) high
424 b3..n-4: packet data
425 bn-3..bn: ethernet crc
426 */
427
428 if (unlikely(skb->len < DM_RX_OVERHEAD)) {
429 dev_err(&dev->udev->dev, "unexpected tiny rx frame\n");
430 return 0;
431 }
432
433 status = skb->data[0];
434 len = (skb->data[1] | (skb->data[2] << 8)) - 4;
435
436 if (unlikely(status & 0xbf)) {
437 if (status & 0x01) dev->stats.rx_fifo_errors++;
438 if (status & 0x02) dev->stats.rx_crc_errors++;
439 if (status & 0x04) dev->stats.rx_frame_errors++;
440 if (status & 0x20) dev->stats.rx_missed_errors++;
441 if (status & 0x90) dev->stats.rx_length_errors++;
442 return 0;
443 }
444
445 skb_pull(skb, 3);
446 skb_trim(skb, len);
447
448 return 1;
449}
450
451static struct sk_buff *dm9601_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
452 gfp_t flags)
453{
454 int len;
455
456 /* format:
457 b0: packet length low
458 b1: packet length high
459 b3..n: packet data
460 */
461
Peter Korsgaard23de5592007-06-27 08:14:50 +0200462 len = skb->len;
463
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100464 if (skb_headroom(skb) < DM_TX_OVERHEAD) {
465 struct sk_buff *skb2;
466
467 skb2 = skb_copy_expand(skb, DM_TX_OVERHEAD, 0, flags);
468 dev_kfree_skb_any(skb);
469 skb = skb2;
470 if (!skb)
471 return NULL;
472 }
473
474 __skb_push(skb, DM_TX_OVERHEAD);
475
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100476 /* usbnet adds padding if length is a multiple of packet size
477 if so, adjust length value in header */
Peter Korsgaard23de5592007-06-27 08:14:50 +0200478 if ((skb->len % dev->maxpacket) == 0)
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100479 len++;
480
481 skb->data[0] = len;
482 skb->data[1] = len >> 8;
483
484 return skb;
485}
486
487static void dm9601_status(struct usbnet *dev, struct urb *urb)
488{
489 int link;
490 u8 *buf;
491
492 /* format:
493 b0: net status
494 b1: tx status 1
495 b2: tx status 2
496 b3: rx status
497 b4: rx overflow
498 b5: rx count
499 b6: tx count
500 b7: gpr
501 */
502
503 if (urb->actual_length < 8)
504 return;
505
506 buf = urb->transfer_buffer;
507
508 link = !!(buf[0] & 0x40);
509 if (netif_carrier_ok(dev->net) != link) {
510 if (link) {
511 netif_carrier_on(dev->net);
512 usbnet_defer_kevent (dev, EVENT_LINK_RESET);
513 }
514 else
515 netif_carrier_off(dev->net);
516 devdbg(dev, "Link Status is: %d", link);
517 }
518}
519
520static int dm9601_link_reset(struct usbnet *dev)
521{
522 struct ethtool_cmd ecmd;
523
524 mii_check_media(&dev->mii, 1, 1);
525 mii_ethtool_gset(&dev->mii, &ecmd);
526
527 devdbg(dev, "link_reset() speed: %d duplex: %d",
528 ecmd.speed, ecmd.duplex);
529
530 return 0;
531}
532
533static const struct driver_info dm9601_info = {
534 .description = "Davicom DM9601 USB Ethernet",
535 .flags = FLAG_ETHER,
536 .bind = dm9601_bind,
537 .rx_fixup = dm9601_rx_fixup,
538 .tx_fixup = dm9601_tx_fixup,
539 .status = dm9601_status,
540 .link_reset = dm9601_link_reset,
541 .reset = dm9601_link_reset,
542};
543
544static const struct usb_device_id products[] = {
545 {
YOSHIFUJI Hideaki6438ac22007-03-02 20:33:55 -0500546 USB_DEVICE(0x07aa, 0x9601), /* Corega FEther USB-TXC */
547 .driver_info = (unsigned long)&dm9601_info,
548 },
549 {
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100550 USB_DEVICE(0x0a46, 0x9601), /* Davicom USB-100 */
551 .driver_info = (unsigned long)&dm9601_info,
552 },
Jon Dowland6dc477f2007-03-11 20:03:03 +0000553 {
554 USB_DEVICE(0x0a46, 0x6688), /* ZT6688 USB NIC */
555 .driver_info = (unsigned long)&dm9601_info,
556 },
557 {
558 USB_DEVICE(0x0a46, 0x0268), /* ShanTou ST268 USB NIC */
559 .driver_info = (unsigned long)&dm9601_info,
560 },
Peter Korsgaarda06da752007-10-30 14:23:47 +0100561 {
562 USB_DEVICE(0x0a46, 0x8515), /* ADMtek ADM8515 USB NIC */
563 .driver_info = (unsigned long)&dm9601_info,
564 },
Peter Korsgaardd0374f42007-02-16 17:03:54 +0100565 {}, // END
566};
567
568MODULE_DEVICE_TABLE(usb, products);
569
570static struct usb_driver dm9601_driver = {
571 .name = "dm9601",
572 .id_table = products,
573 .probe = usbnet_probe,
574 .disconnect = usbnet_disconnect,
575 .suspend = usbnet_suspend,
576 .resume = usbnet_resume,
577};
578
579static int __init dm9601_init(void)
580{
581 return usb_register(&dm9601_driver);
582}
583
584static void __exit dm9601_exit(void)
585{
586 usb_deregister(&dm9601_driver);
587}
588
589module_init(dm9601_init);
590module_exit(dm9601_exit);
591
592MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
593MODULE_DESCRIPTION("Davicom DM9601 USB 1.1 ethernet devices");
594MODULE_LICENSE("GPL");