xref: /linux/drivers/net/usb/dm9601.c (revision b889fcf63cb62e7fdb7816565e28f44dbe4a76a5)
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 #include <linux/usb/usbnet.h>
24 #include <linux/slab.h>
25 
26 /* datasheet:
27  http://ptm2.cc.utu.fi/ftp/network/cards/DM9601/From_NET/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 
57 static int dm_read(struct usbnet *dev, u8 reg, u16 length, void *data)
58 {
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;
66 }
67 
68 static int dm_read_reg(struct usbnet *dev, u8 reg, u8 *value)
69 {
70 	return dm_read(dev, reg, 1, value);
71 }
72 
73 static int dm_write(struct usbnet *dev, u8 reg, u16 length, void *data)
74 {
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);
79 
80 	if (err >= 0 && err < length)
81 		err = -EINVAL;
82 	return err;
83 }
84 
85 static int dm_write_reg(struct usbnet *dev, u8 reg, u8 value)
86 {
87 	return usbnet_write_cmd(dev, DM_WRITE_REGS,
88 				USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
89 				value, reg, NULL, 0);
90 }
91 
92 static void dm_write_async_helper(struct usbnet *dev, u8 reg, u8 value,
93 				  u16 length, void *data)
94 {
95 	usbnet_write_cmd_async(dev, DM_WRITE_REGS,
96 			       USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
97 			       value, reg, data, length);
98 }
99 
100 static void dm_write_async(struct usbnet *dev, u8 reg, u16 length, void *data)
101 {
102 	netdev_dbg(dev->net, "dm_write_async() reg=0x%02x length=%d\n", reg, length);
103 
104 	dm_write_async_helper(dev, reg, 0, length, data);
105 }
106 
107 static void dm_write_reg_async(struct usbnet *dev, u8 reg, u8 value)
108 {
109 	netdev_dbg(dev->net, "dm_write_reg_async() reg=0x%02x value=0x%02x\n",
110 		   reg, value);
111 
112 	dm_write_async_helper(dev, reg, value, 0, NULL);
113 }
114 
115 static int dm_read_shared_word(struct usbnet *dev, int phy, u8 reg, __le16 *value)
116 {
117 	int ret, i;
118 
119 	mutex_lock(&dev->phy_mutex);
120 
121 	dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg);
122 	dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0xc : 0x4);
123 
124 	for (i = 0; i < DM_TIMEOUT; i++) {
125 		u8 tmp;
126 
127 		udelay(1);
128 		ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp);
129 		if (ret < 0)
130 			goto out;
131 
132 		/* ready */
133 		if ((tmp & 1) == 0)
134 			break;
135 	}
136 
137 	if (i == DM_TIMEOUT) {
138 		netdev_err(dev->net, "%s read timed out!\n", phy ? "phy" : "eeprom");
139 		ret = -EIO;
140 		goto out;
141 	}
142 
143 	dm_write_reg(dev, DM_SHARED_CTRL, 0x0);
144 	ret = dm_read(dev, DM_SHARED_DATA, 2, value);
145 
146 	netdev_dbg(dev->net, "read shared %d 0x%02x returned 0x%04x, %d\n",
147 		   phy, reg, *value, ret);
148 
149  out:
150 	mutex_unlock(&dev->phy_mutex);
151 	return ret;
152 }
153 
154 static int dm_write_shared_word(struct usbnet *dev, int phy, u8 reg, __le16 value)
155 {
156 	int ret, i;
157 
158 	mutex_lock(&dev->phy_mutex);
159 
160 	ret = dm_write(dev, DM_SHARED_DATA, 2, &value);
161 	if (ret < 0)
162 		goto out;
163 
164 	dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg);
165 	dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0x1a : 0x12);
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 		netdev_err(dev->net, "%s write timed out!\n", phy ? "phy" : "eeprom");
182 		ret = -EIO;
183 		goto out;
184 	}
185 
186 	dm_write_reg(dev, DM_SHARED_CTRL, 0x0);
187 
188 out:
189 	mutex_unlock(&dev->phy_mutex);
190 	return ret;
191 }
192 
193 static int dm_read_eeprom_word(struct usbnet *dev, u8 offset, void *value)
194 {
195 	return dm_read_shared_word(dev, 0, offset, value);
196 }
197 
198 
199 
200 static int dm9601_get_eeprom_len(struct net_device *dev)
201 {
202 	return DM_EEPROM_LEN;
203 }
204 
205 static int dm9601_get_eeprom(struct net_device *net,
206 			     struct ethtool_eeprom *eeprom, u8 * data)
207 {
208 	struct usbnet *dev = netdev_priv(net);
209 	__le16 *ebuf = (__le16 *) data;
210 	int i;
211 
212 	/* access is 16bit */
213 	if ((eeprom->offset % 2) || (eeprom->len % 2))
214 		return -EINVAL;
215 
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;
220 	}
221 	return 0;
222 }
223 
224 static int dm9601_mdio_read(struct net_device *netdev, int phy_id, int loc)
225 {
226 	struct usbnet *dev = netdev_priv(netdev);
227 
228 	__le16 res;
229 
230 	if (phy_id) {
231 		netdev_dbg(dev->net, "Only internal phy supported\n");
232 		return 0;
233 	}
234 
235 	dm_read_shared_word(dev, 1, loc, &res);
236 
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));
240 
241 	return le16_to_cpu(res);
242 }
243 
244 static void dm9601_mdio_write(struct net_device *netdev, int phy_id, int loc,
245 			      int val)
246 {
247 	struct usbnet *dev = netdev_priv(netdev);
248 	__le16 res = cpu_to_le16(val);
249 
250 	if (phy_id) {
251 		netdev_dbg(dev->net, "Only internal phy supported\n");
252 		return;
253 	}
254 
255 	netdev_dbg(dev->net, "dm9601_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
256 		   phy_id, loc, val);
257 
258 	dm_write_shared_word(dev, 1, loc, res);
259 }
260 
261 static void dm9601_get_drvinfo(struct net_device *net,
262 			       struct ethtool_drvinfo *info)
263 {
264 	/* Inherit standard device info */
265 	usbnet_get_drvinfo(net, info);
266 	info->eedump_len = DM_EEPROM_LEN;
267 }
268 
269 static u32 dm9601_get_link(struct net_device *net)
270 {
271 	struct usbnet *dev = netdev_priv(net);
272 
273 	return mii_link_ok(&dev->mii);
274 }
275 
276 static int dm9601_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
277 {
278 	struct usbnet *dev = netdev_priv(net);
279 
280 	return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
281 }
282 
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,
293 };
294 
295 static void dm9601_set_multicast(struct net_device *net)
296 {
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;
302 
303 	memset(hashes, 0x00, DM_MCAST_SIZE);
304 	hashes[DM_MCAST_SIZE - 1] |= 0x80;	/* broadcast address */
305 
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;
313 
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);
317 		}
318 	}
319 
320 	dm_write_async(dev, DM_MCAST_ADDR, DM_MCAST_SIZE, hashes);
321 	dm_write_reg_async(dev, DM_RX_CTRL, rx_ctl);
322 }
323 
324 static void __dm9601_set_mac_address(struct usbnet *dev)
325 {
326 	dm_write_async(dev, DM_PHY_ADDR, ETH_ALEN, dev->net->dev_addr);
327 }
328 
329 static int dm9601_set_mac_address(struct net_device *net, void *p)
330 {
331 	struct sockaddr *addr = p;
332 	struct usbnet *dev = netdev_priv(net);
333 
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;
338 	}
339 
340 	memcpy(net->dev_addr, addr->sa_data, net->addr_len);
341 	__dm9601_set_mac_address(dev);
342 
343 	return 0;
344 }
345 
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,
356 };
357 
358 static int dm9601_bind(struct usbnet *dev, struct usb_interface *intf)
359 {
360 	int ret;
361 	u8 mac[ETH_ALEN];
362 
363 	ret = usbnet_get_endpoints(dev, intf);
364 	if (ret)
365 		goto out;
366 
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;
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 */
380 	dm_write_reg(dev, DM_NET_CTRL, 1);
381 	udelay(20);
382 
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;
388 	}
389 
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);
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);
400 	}
401 
402 	/* power up phy */
403 	dm_write_reg(dev, DM_GPR_CTRL, 1);
404 	dm_write_reg(dev, DM_GPR_DATA, 0);
405 
406 	/* receive broadcast packets */
407 	dm9601_set_multicast(dev->net);
408 
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);
413 
414 out:
415 	return ret;
416 }
417 
418 static int dm9601_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
419 {
420 	u8 status;
421 	int len;
422 
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
429 	 */
430 
431 	if (unlikely(skb->len < DM_RX_OVERHEAD)) {
432 		dev_err(&dev->udev->dev, "unexpected tiny rx frame\n");
433 		return 0;
434 	}
435 
436 	status = skb->data[0];
437 	len = (skb->data[1] | (skb->data[2] << 8)) - 4;
438 
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;
446 	}
447 
448 	skb_pull(skb, 3);
449 	skb_trim(skb, len);
450 
451 	return 1;
452 }
453 
454 static struct sk_buff *dm9601_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
455 				       gfp_t flags)
456 {
457 	int len;
458 
459 	/* format:
460 	   b1: packet length low
461 	   b2: packet length high
462 	   b3..n: packet data
463 	*/
464 
465 	len = skb->len;
466 
467 	if (skb_headroom(skb) < DM_TX_OVERHEAD) {
468 		struct sk_buff *skb2;
469 
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;
475 	}
476 
477 	__skb_push(skb, DM_TX_OVERHEAD);
478 
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++;
483 
484 	skb->data[0] = len;
485 	skb->data[1] = len >> 8;
486 
487 	return skb;
488 }
489 
490 static void dm9601_status(struct usbnet *dev, struct urb *urb)
491 {
492 	int link;
493 	u8 *buf;
494 
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
504 	*/
505 
506 	if (urb->actual_length < 8)
507 		return;
508 
509 	buf = urb->transfer_buffer;
510 
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);
516 		}
517 		else
518 			netif_carrier_off(dev->net);
519 		netdev_dbg(dev->net, "Link Status is: %d\n", link);
520 	}
521 }
522 
523 static int dm9601_link_reset(struct usbnet *dev)
524 {
525 	struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
526 
527 	mii_check_media(&dev->mii, 1, 1);
528 	mii_ethtool_gset(&dev->mii, &ecmd);
529 
530 	netdev_dbg(dev->net, "link_reset() speed: %u duplex: %d\n",
531 		   ethtool_cmd_speed(&ecmd), ecmd.duplex);
532 
533 	return 0;
534 }
535 
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,
545 };
546 
547 static const struct usb_device_id products[] = {
548 	{
549 	 USB_DEVICE(0x07aa, 0x9601),	/* Corega FEther USB-TXC */
550 	 .driver_info = (unsigned long)&dm9601_info,
551 	 },
552 	{
553 	 USB_DEVICE(0x0a46, 0x9601),	/* Davicom USB-100 */
554 	 .driver_info = (unsigned long)&dm9601_info,
555 	 },
556 	{
557 	 USB_DEVICE(0x0a46, 0x6688),	/* ZT6688 USB NIC */
558 	 .driver_info = (unsigned long)&dm9601_info,
559 	 },
560 	{
561 	 USB_DEVICE(0x0a46, 0x0268),	/* ShanTou ST268 USB NIC */
562 	 .driver_info = (unsigned long)&dm9601_info,
563 	 },
564 	{
565 	 USB_DEVICE(0x0a46, 0x8515),	/* ADMtek ADM8515 USB NIC */
566 	 .driver_info = (unsigned long)&dm9601_info,
567 	 },
568 	{
569 	USB_DEVICE(0x0a47, 0x9601),	/* Hirose USB-100 */
570 	.driver_info = (unsigned long)&dm9601_info,
571 	 },
572 	{
573 	USB_DEVICE(0x0fe6, 0x8101),	/* DM9601 USB to Fast Ethernet Adapter */
574 	.driver_info = (unsigned long)&dm9601_info,
575 	 },
576 	{
577 	 USB_DEVICE(0x0fe6, 0x9700),	/* DM9601 USB to Fast Ethernet Adapter */
578 	 .driver_info = (unsigned long)&dm9601_info,
579 	 },
580 	{
581 	 USB_DEVICE(0x0a46, 0x9000),	/* DM9000E */
582 	 .driver_info = (unsigned long)&dm9601_info,
583 	 },
584 	{},			// END
585 };
586 
587 MODULE_DEVICE_TABLE(usb, products);
588 
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,
597 };
598 
599 module_usb_driver(dm9601_driver);
600 
601 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
602 MODULE_DESCRIPTION("Davicom DM9601 USB 1.1 ethernet devices");
603 MODULE_LICENSE("GPL");
604