aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/usb/dm9601.c
diff options
context:
space:
mode:
authorJeff Garzik <jeff@garzik.org>2007-05-09 21:31:55 -0400
committerJeff Garzik <jeff@garzik.org>2007-05-09 21:31:55 -0400
commit5b2fc499917e5897a13add780e181b4cef197072 (patch)
tree1a1ba52c0c2a7ce9843875cdd713d75d37c4ea1b /drivers/net/usb/dm9601.c
parent3cb7396b7b26585b1ab7c1a8ca554ec103da5d37 (diff)
Move USB network drivers to drivers/net/usb.
It is preferable to group drivers by usage (net, scsi, ATA, ...) than by bus. When reviewing drivers, the [PCI|USB|PCMCIA|...] maintainer is probably less qualified on networking issues than a networking maintainer. Also, from a practical standpoint, chips often appear on multiple buses, which is why we do not put drivers into drivers/pci/net. Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de> Signed-off-by: Jeff Garzik <jeff@garzik.org>
Diffstat (limited to 'drivers/net/usb/dm9601.c')
-rw-r--r--drivers/net/usb/dm9601.c619
1 files changed, 619 insertions, 0 deletions
diff --git a/drivers/net/usb/dm9601.c b/drivers/net/usb/dm9601.c
new file mode 100644
index 000000000000..a67638601477
--- /dev/null
+++ b/drivers/net/usb/dm9601.c
@@ -0,0 +1,619 @@
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>
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
24#include "usbnet.h"
25
26/* datasheet:
27 http://www.davicom.com.tw/big5/download/Data%20Sheet/DM9601-DS-P01-930914.pdf
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
48
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
55
56
57static int dm_read(struct usbnet *dev, u8 reg, u16 length, void *data)
58{
59 devdbg(dev, "dm_read() reg=0x%02x length=%d", reg, length);
60 return usb_control_msg(dev->udev,
61 usb_rcvctrlpipe(dev->udev, 0),
62 DM_READ_REGS,
63 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
64 0, reg, data, length, USB_CTRL_SET_TIMEOUT);
65}
66
67static int dm_read_reg(struct usbnet *dev, u8 reg, u8 *value)
68{
69 return dm_read(dev, reg, 1, value);
70}
71
72static int dm_write(struct usbnet *dev, u8 reg, u16 length, void *data)
73{
74 devdbg(dev, "dm_write() reg=0x%02x, length=%d", reg, length);
75 return usb_control_msg(dev->udev,
76 usb_sndctrlpipe(dev->udev, 0),
77 DM_WRITE_REGS,
78 USB_DIR_OUT | USB_TYPE_VENDOR |USB_RECIP_DEVICE,
79 0, reg, data, length, USB_CTRL_SET_TIMEOUT);
80}
81
82static int dm_write_reg(struct usbnet *dev, u8 reg, u8 value)
83{
84 devdbg(dev, "dm_write_reg() reg=0x%02x, value=0x%02x", reg, value);
85 return usb_control_msg(dev->udev,
86 usb_sndctrlpipe(dev->udev, 0),
87 DM_WRITE_REG,
88 USB_DIR_OUT | USB_TYPE_VENDOR |USB_RECIP_DEVICE,
89 value, reg, NULL, 0, USB_CTRL_SET_TIMEOUT);
90}
91
92static void dm_write_async_callback(struct urb *urb)
93{
94 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
95
96 if (urb->status < 0)
97 printk(KERN_DEBUG "dm_write_async_callback() failed with %d",
98 urb->status);
99
100 kfree(req);
101 usb_free_urb(urb);
102}
103
104static void dm_write_async(struct usbnet *dev, u8 reg, u16 length, void *data)
105{
106 struct usb_ctrlrequest *req;
107 struct urb *urb;
108 int status;
109
110 devdbg(dev, "dm_write_async() reg=0x%02x length=%d", reg, length);
111
112 urb = usb_alloc_urb(0, GFP_ATOMIC);
113 if (!urb) {
114 deverr(dev, "Error allocating URB in dm_write_async!");
115 return;
116 }
117
118 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
119 if (!req) {
120 deverr(dev, "Failed to allocate memory for control request");
121 usb_free_urb(urb);
122 return;
123 }
124
125 req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
126 req->bRequest = DM_WRITE_REGS;
127 req->wValue = 0;
128 req->wIndex = cpu_to_le16(reg);
129 req->wLength = cpu_to_le16(length);
130
131 usb_fill_control_urb(urb, dev->udev,
132 usb_sndctrlpipe(dev->udev, 0),
133 (void *)req, data, length,
134 dm_write_async_callback, req);
135
136 status = usb_submit_urb(urb, GFP_ATOMIC);
137 if (status < 0) {
138 deverr(dev, "Error submitting the control message: status=%d",
139 status);
140 kfree(req);
141 usb_free_urb(urb);
142 }
143}
144
145static void dm_write_reg_async(struct usbnet *dev, u8 reg, u8 value)
146{
147 struct usb_ctrlrequest *req;
148 struct urb *urb;
149 int status;
150
151 devdbg(dev, "dm_write_reg_async() reg=0x%02x value=0x%02x",
152 reg, value);
153
154 urb = usb_alloc_urb(0, GFP_ATOMIC);
155 if (!urb) {
156 deverr(dev, "Error allocating URB in dm_write_async!");
157 return;
158 }
159
160 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
161 if (!req) {
162 deverr(dev, "Failed to allocate memory for control request");
163 usb_free_urb(urb);
164 return;
165 }
166
167 req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
168 req->bRequest = DM_WRITE_REG;
169 req->wValue = cpu_to_le16(value);
170 req->wIndex = cpu_to_le16(reg);
171 req->wLength = 0;
172
173 usb_fill_control_urb(urb, dev->udev,
174 usb_sndctrlpipe(dev->udev, 0),
175 (void *)req, NULL, 0, dm_write_async_callback, req);
176
177 status = usb_submit_urb(urb, GFP_ATOMIC);
178 if (status < 0) {
179 deverr(dev, "Error submitting the control message: status=%d",
180 status);
181 kfree(req);
182 usb_free_urb(urb);
183 }
184}
185
186static int dm_read_shared_word(struct usbnet *dev, int phy, u8 reg, u16 *value)
187{
188 int ret, i;
189
190 mutex_lock(&dev->phy_mutex);
191
192 dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg);
193 dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0xc : 0x4);
194
195 for (i = 0; i < DM_TIMEOUT; i++) {
196 u8 tmp;
197
198 udelay(1);
199 ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp);
200 if (ret < 0)
201 goto out;
202
203 /* ready */
204 if ((tmp & 1) == 0)
205 break;
206 }
207
208 if (i == DM_TIMEOUT) {
209 deverr(dev, "%s read timed out!", phy ? "phy" : "eeprom");
210 ret = -EIO;
211 goto out;
212 }
213
214 dm_write_reg(dev, DM_SHARED_CTRL, 0x0);
215 ret = dm_read(dev, DM_SHARED_DATA, 2, value);
216
217 devdbg(dev, "read shared %d 0x%02x returned 0x%04x, %d",
218 phy, reg, *value, ret);
219
220 out:
221 mutex_unlock(&dev->phy_mutex);
222 return ret;
223}
224
225static int dm_write_shared_word(struct usbnet *dev, int phy, u8 reg, u16 value)
226{
227 int ret, i;
228
229 mutex_lock(&dev->phy_mutex);
230
231 ret = dm_write(dev, DM_SHARED_DATA, 2, &value);
232 if (ret < 0)
233 goto out;
234
235 dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg);
236 dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0x1c : 0x14);
237
238 for (i = 0; i < DM_TIMEOUT; i++) {
239 u8 tmp;
240
241 udelay(1);
242 ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp);
243 if (ret < 0)
244 goto out;
245
246 /* ready */
247 if ((tmp & 1) == 0)
248 break;
249 }
250
251 if (i == DM_TIMEOUT) {
252 deverr(dev, "%s write timed out!", phy ? "phy" : "eeprom");
253 ret = -EIO;
254 goto out;
255 }
256
257 dm_write_reg(dev, DM_SHARED_CTRL, 0x0);
258
259out:
260 mutex_unlock(&dev->phy_mutex);
261 return ret;
262}
263
264static int dm_read_eeprom_word(struct usbnet *dev, u8 offset, void *value)
265{
266 return dm_read_shared_word(dev, 0, offset, value);
267}
268
269
270
271static int dm9601_get_eeprom_len(struct net_device *dev)
272{
273 return DM_EEPROM_LEN;
274}
275
276static int dm9601_get_eeprom(struct net_device *net,
277 struct ethtool_eeprom *eeprom, u8 * data)
278{
279 struct usbnet *dev = netdev_priv(net);
280 u16 *ebuf = (u16 *) data;
281 int i;
282
283 /* access is 16bit */
284 if ((eeprom->offset % 2) || (eeprom->len % 2))
285 return -EINVAL;
286
287 for (i = 0; i < eeprom->len / 2; i++) {
288 if (dm_read_eeprom_word(dev, eeprom->offset / 2 + i,
289 &ebuf[i]) < 0)
290 return -EINVAL;
291 }
292 return 0;
293}
294
295static int dm9601_mdio_read(struct net_device *netdev, int phy_id, int loc)
296{
297 struct usbnet *dev = netdev_priv(netdev);
298
299 u16 res;
300
301 if (phy_id) {
302 devdbg(dev, "Only internal phy supported");
303 return 0;
304 }
305
306 dm_read_shared_word(dev, 1, loc, &res);
307
308 devdbg(dev,
309 "dm9601_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x",
310 phy_id, loc, le16_to_cpu(res));
311
312 return le16_to_cpu(res);
313}
314
315static void dm9601_mdio_write(struct net_device *netdev, int phy_id, int loc,
316 int val)
317{
318 struct usbnet *dev = netdev_priv(netdev);
319 u16 res = cpu_to_le16(val);
320
321 if (phy_id) {
322 devdbg(dev, "Only internal phy supported");
323 return;
324 }
325
326 devdbg(dev,"dm9601_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x",
327 phy_id, loc, val);
328
329 dm_write_shared_word(dev, 1, loc, res);
330}
331
332static void dm9601_get_drvinfo(struct net_device *net,
333 struct ethtool_drvinfo *info)
334{
335 /* Inherit standard device info */
336 usbnet_get_drvinfo(net, info);
337 info->eedump_len = DM_EEPROM_LEN;
338}
339
340static u32 dm9601_get_link(struct net_device *net)
341{
342 struct usbnet *dev = netdev_priv(net);
343
344 return mii_link_ok(&dev->mii);
345}
346
347static int dm9601_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
348{
349 struct usbnet *dev = netdev_priv(net);
350
351 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
352}
353
354static struct ethtool_ops dm9601_ethtool_ops = {
355 .get_drvinfo = dm9601_get_drvinfo,
356 .get_link = dm9601_get_link,
357 .get_msglevel = usbnet_get_msglevel,
358 .set_msglevel = usbnet_set_msglevel,
359 .get_eeprom_len = dm9601_get_eeprom_len,
360 .get_eeprom = dm9601_get_eeprom,
361 .get_settings = usbnet_get_settings,
362 .set_settings = usbnet_set_settings,
363 .nway_reset = usbnet_nway_reset,
364};
365
366static void dm9601_set_multicast(struct net_device *net)
367{
368 struct usbnet *dev = netdev_priv(net);
369 /* We use the 20 byte dev->data for our 8 byte filter buffer
370 * to avoid allocating memory that is tricky to free later */
371 u8 *hashes = (u8 *) & dev->data;
372 u8 rx_ctl = 0x01;
373
374 memset(hashes, 0x00, DM_MCAST_SIZE);
375 hashes[DM_MCAST_SIZE - 1] |= 0x80; /* broadcast address */
376
377 if (net->flags & IFF_PROMISC) {
378 rx_ctl |= 0x02;
379 } else if (net->flags & IFF_ALLMULTI || net->mc_count > DM_MAX_MCAST) {
380 rx_ctl |= 0x04;
381 } else if (net->mc_count) {
382 struct dev_mc_list *mc_list = net->mc_list;
383 int i;
384
385 for (i = 0; i < net->mc_count; i++) {
386 u32 crc = ether_crc(ETH_ALEN, mc_list->dmi_addr) >> 26;
387 hashes[crc >> 3] |= 1 << (crc & 0x7);
388 }
389 }
390
391 dm_write_async(dev, DM_MCAST_ADDR, DM_MCAST_SIZE, hashes);
392 dm_write_reg_async(dev, DM_RX_CTRL, rx_ctl);
393}
394
395static int dm9601_bind(struct usbnet *dev, struct usb_interface *intf)
396{
397 int ret;
398
399 ret = usbnet_get_endpoints(dev, intf);
400 if (ret)
401 goto out;
402
403 dev->net->do_ioctl = dm9601_ioctl;
404 dev->net->set_multicast_list = dm9601_set_multicast;
405 dev->net->ethtool_ops = &dm9601_ethtool_ops;
406 dev->net->hard_header_len += DM_TX_OVERHEAD;
407 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
408 dev->rx_urb_size = dev->net->mtu + DM_RX_OVERHEAD;
409
410 dev->mii.dev = dev->net;
411 dev->mii.mdio_read = dm9601_mdio_read;
412 dev->mii.mdio_write = dm9601_mdio_write;
413 dev->mii.phy_id_mask = 0x1f;
414 dev->mii.reg_num_mask = 0x1f;
415
416 /* reset */
417 ret = dm_write_reg(dev, DM_NET_CTRL, 1);
418 udelay(20);
419
420 /* read MAC */
421 ret = dm_read(dev, DM_PHY_ADDR, ETH_ALEN, dev->net->dev_addr);
422 if (ret < 0) {
423 printk(KERN_ERR "Error reading MAC address\n");
424 ret = -ENODEV;
425 goto out;
426 }
427
428
429 /* power up phy */
430 dm_write_reg(dev, DM_GPR_CTRL, 1);
431 dm_write_reg(dev, DM_GPR_DATA, 0);
432
433 /* receive broadcast packets */
434 dm9601_set_multicast(dev->net);
435
436 dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
437 dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
438 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
439 mii_nway_restart(&dev->mii);
440
441out:
442 return ret;
443}
444
445static int dm9601_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
446{
447 u8 status;
448 int len;
449
450 /* format:
451 b0: rx status
452 b1: packet length (incl crc) low
453 b2: packet length (incl crc) high
454 b3..n-4: packet data
455 bn-3..bn: ethernet crc
456 */
457
458 if (unlikely(skb->len < DM_RX_OVERHEAD)) {
459 dev_err(&dev->udev->dev, "unexpected tiny rx frame\n");
460 return 0;
461 }
462
463 status = skb->data[0];
464 len = (skb->data[1] | (skb->data[2] << 8)) - 4;
465
466 if (unlikely(status & 0xbf)) {
467 if (status & 0x01) dev->stats.rx_fifo_errors++;
468 if (status & 0x02) dev->stats.rx_crc_errors++;
469 if (status & 0x04) dev->stats.rx_frame_errors++;
470 if (status & 0x20) dev->stats.rx_missed_errors++;
471 if (status & 0x90) dev->stats.rx_length_errors++;
472 return 0;
473 }
474
475 skb_pull(skb, 3);
476 skb_trim(skb, len);
477
478 return 1;
479}
480
481static struct sk_buff *dm9601_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
482 gfp_t flags)
483{
484 int len;
485
486 /* format:
487 b0: packet length low
488 b1: packet length high
489 b3..n: packet data
490 */
491
492 if (skb_headroom(skb) < DM_TX_OVERHEAD) {
493 struct sk_buff *skb2;
494
495 skb2 = skb_copy_expand(skb, DM_TX_OVERHEAD, 0, flags);
496 dev_kfree_skb_any(skb);
497 skb = skb2;
498 if (!skb)
499 return NULL;
500 }
501
502 __skb_push(skb, DM_TX_OVERHEAD);
503
504 len = skb->len;
505 /* usbnet adds padding if length is a multiple of packet size
506 if so, adjust length value in header */
507 if ((len % dev->maxpacket) == 0)
508 len++;
509
510 skb->data[0] = len;
511 skb->data[1] = len >> 8;
512
513 return skb;
514}
515
516static void dm9601_status(struct usbnet *dev, struct urb *urb)
517{
518 int link;
519 u8 *buf;
520
521 /* format:
522 b0: net status
523 b1: tx status 1
524 b2: tx status 2
525 b3: rx status
526 b4: rx overflow
527 b5: rx count
528 b6: tx count
529 b7: gpr
530 */
531
532 if (urb->actual_length < 8)
533 return;
534
535 buf = urb->transfer_buffer;
536
537 link = !!(buf[0] & 0x40);
538 if (netif_carrier_ok(dev->net) != link) {
539 if (link) {
540 netif_carrier_on(dev->net);
541 usbnet_defer_kevent (dev, EVENT_LINK_RESET);
542 }
543 else
544 netif_carrier_off(dev->net);
545 devdbg(dev, "Link Status is: %d", link);
546 }
547}
548
549static int dm9601_link_reset(struct usbnet *dev)
550{
551 struct ethtool_cmd ecmd;
552
553 mii_check_media(&dev->mii, 1, 1);
554 mii_ethtool_gset(&dev->mii, &ecmd);
555
556 devdbg(dev, "link_reset() speed: %d duplex: %d",
557 ecmd.speed, ecmd.duplex);
558
559 return 0;
560}
561
562static const struct driver_info dm9601_info = {
563 .description = "Davicom DM9601 USB Ethernet",
564 .flags = FLAG_ETHER,
565 .bind = dm9601_bind,
566 .rx_fixup = dm9601_rx_fixup,
567 .tx_fixup = dm9601_tx_fixup,
568 .status = dm9601_status,
569 .link_reset = dm9601_link_reset,
570 .reset = dm9601_link_reset,
571};
572
573static const struct usb_device_id products[] = {
574 {
575 USB_DEVICE(0x07aa, 0x9601), /* Corega FEther USB-TXC */
576 .driver_info = (unsigned long)&dm9601_info,
577 },
578 {
579 USB_DEVICE(0x0a46, 0x9601), /* Davicom USB-100 */
580 .driver_info = (unsigned long)&dm9601_info,
581 },
582 {
583 USB_DEVICE(0x0a46, 0x6688), /* ZT6688 USB NIC */
584 .driver_info = (unsigned long)&dm9601_info,
585 },
586 {
587 USB_DEVICE(0x0a46, 0x0268), /* ShanTou ST268 USB NIC */
588 .driver_info = (unsigned long)&dm9601_info,
589 },
590 {}, // END
591};
592
593MODULE_DEVICE_TABLE(usb, products);
594
595static struct usb_driver dm9601_driver = {
596 .name = "dm9601",
597 .id_table = products,
598 .probe = usbnet_probe,
599 .disconnect = usbnet_disconnect,
600 .suspend = usbnet_suspend,
601 .resume = usbnet_resume,
602};
603
604static int __init dm9601_init(void)
605{
606 return usb_register(&dm9601_driver);
607}
608
609static void __exit dm9601_exit(void)
610{
611 usb_deregister(&dm9601_driver);
612}
613
614module_init(dm9601_init);
615module_exit(dm9601_exit);
616
617MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
618MODULE_DESCRIPTION("Davicom DM9601 USB 1.1 ethernet devices");
619MODULE_LICENSE("GPL");