xref: /linux/drivers/net/usb/mcs7830.c (revision d39d0ed196aa1685bb24771e92f78633c66ac9cb)
1 /*
2  * MOSCHIP MCS7830 based USB 2.0 Ethernet Devices
3  *
4  * based on usbnet.c, asix.c and the vendor provided mcs7830 driver
5  *
6  * Copyright (C) 2010 Andreas Mohr <andi@lisas.de>
7  * Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de>
8  * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
9  * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
10  * Copyright (c) 2002-2003 TiVo Inc.
11  *
12  * Definitions gathered from MOSCHIP, Data Sheet_7830DA.pdf (thanks!).
13  *
14  * TODO:
15  * - support HIF_REG_CONFIG_SLEEPMODE/HIF_REG_CONFIG_TXENABLE (via autopm?)
16  * - implement ethtool_ops get_pauseparam/set_pauseparam
17  *   via HIF_REG_PAUSE_THRESHOLD (>= revision C only!)
18  * - implement get_eeprom/[set_eeprom]
19  * - switch PHY on/off on ifup/ifdown (perhaps in usbnet.c, via MII)
20  * - mcs7830_get_regs() handling is weird: for rev 2 we return 32 regs,
21  *   can access only ~ 24, remaining user buffer is uninitialized garbage
22  * - anything else?
23  *
24  *
25  * This program is free software; you can redistribute it and/or modify
26  * it under the terms of the GNU General Public License as published by
27  * the Free Software Foundation; either version 2 of the License, or
28  * (at your option) any later version.
29  *
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with this program; if not, write to the Free Software
37  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
38  */
39 
40 #include <linux/crc32.h>
41 #include <linux/etherdevice.h>
42 #include <linux/ethtool.h>
43 #include <linux/init.h>
44 #include <linux/mii.h>
45 #include <linux/module.h>
46 #include <linux/netdevice.h>
47 #include <linux/slab.h>
48 #include <linux/usb.h>
49 #include <linux/usb/usbnet.h>
50 
51 /* requests */
52 #define MCS7830_RD_BMREQ	(USB_DIR_IN  | USB_TYPE_VENDOR | \
53 				 USB_RECIP_DEVICE)
54 #define MCS7830_WR_BMREQ	(USB_DIR_OUT | USB_TYPE_VENDOR | \
55 				 USB_RECIP_DEVICE)
56 #define MCS7830_RD_BREQ		0x0E
57 #define MCS7830_WR_BREQ		0x0D
58 
59 #define MCS7830_CTRL_TIMEOUT	1000
60 #define MCS7830_MAX_MCAST	64
61 
62 #define MCS7830_VENDOR_ID	0x9710
63 #define MCS7830_PRODUCT_ID	0x7830
64 #define MCS7730_PRODUCT_ID	0x7730
65 
66 #define SITECOM_VENDOR_ID	0x0DF6
67 #define LN_030_PRODUCT_ID	0x0021
68 
69 #define MCS7830_MII_ADVERTISE	(ADVERTISE_PAUSE_CAP | ADVERTISE_100FULL | \
70 				 ADVERTISE_100HALF | ADVERTISE_10FULL | \
71 				 ADVERTISE_10HALF | ADVERTISE_CSMA)
72 
73 /* HIF_REG_XX corresponding index value */
74 enum {
75 	HIF_REG_MULTICAST_HASH			= 0x00,
76 	HIF_REG_PACKET_GAP1			= 0x08,
77 	HIF_REG_PACKET_GAP2			= 0x09,
78 	HIF_REG_PHY_DATA			= 0x0a,
79 	HIF_REG_PHY_CMD1			= 0x0c,
80 	   HIF_REG_PHY_CMD1_READ		= 0x40,
81 	   HIF_REG_PHY_CMD1_WRITE		= 0x20,
82 	   HIF_REG_PHY_CMD1_PHYADDR		= 0x01,
83 	HIF_REG_PHY_CMD2			= 0x0d,
84 	   HIF_REG_PHY_CMD2_PEND_FLAG_BIT	= 0x80,
85 	   HIF_REG_PHY_CMD2_READY_FLAG_BIT	= 0x40,
86 	HIF_REG_CONFIG				= 0x0e,
87 	/* hmm, spec sez: "R/W", "Except bit 3" (likely TXENABLE). */
88 	   HIF_REG_CONFIG_CFG			= 0x80,
89 	   HIF_REG_CONFIG_SPEED100		= 0x40,
90 	   HIF_REG_CONFIG_FULLDUPLEX_ENABLE	= 0x20,
91 	   HIF_REG_CONFIG_RXENABLE		= 0x10,
92 	   HIF_REG_CONFIG_TXENABLE		= 0x08,
93 	   HIF_REG_CONFIG_SLEEPMODE		= 0x04,
94 	   HIF_REG_CONFIG_ALLMULTICAST		= 0x02,
95 	   HIF_REG_CONFIG_PROMISCUOUS		= 0x01,
96 	HIF_REG_ETHERNET_ADDR			= 0x0f,
97 	HIF_REG_FRAME_DROP_COUNTER		= 0x15, /* 0..ff; reset: 0 */
98 	HIF_REG_PAUSE_THRESHOLD			= 0x16,
99 	   HIF_REG_PAUSE_THRESHOLD_DEFAULT	= 0,
100 };
101 
102 /* Trailing status byte in Ethernet Rx frame */
103 enum {
104 	MCS7830_RX_SHORT_FRAME		= 0x01, /* < 64 bytes */
105 	MCS7830_RX_LENGTH_ERROR		= 0x02, /* framelen != Ethernet length field */
106 	MCS7830_RX_ALIGNMENT_ERROR	= 0x04, /* non-even number of nibbles */
107 	MCS7830_RX_CRC_ERROR		= 0x08,
108 	MCS7830_RX_LARGE_FRAME		= 0x10, /* > 1518 bytes */
109 	MCS7830_RX_FRAME_CORRECT	= 0x20, /* frame is correct */
110 	/* [7:6] reserved */
111 };
112 
113 struct mcs7830_data {
114 	u8 multi_filter[8];
115 	u8 config;
116 };
117 
118 static const char driver_name[] = "MOSCHIP usb-ethernet driver";
119 
120 static int mcs7830_get_reg(struct usbnet *dev, u16 index, u16 size, void *data)
121 {
122 	struct usb_device *xdev = dev->udev;
123 	int ret;
124 	void *buffer;
125 
126 	buffer = kmalloc(size, GFP_NOIO);
127 	if (buffer == NULL)
128 		return -ENOMEM;
129 
130 	ret = usb_control_msg(xdev, usb_rcvctrlpipe(xdev, 0), MCS7830_RD_BREQ,
131 			      MCS7830_RD_BMREQ, 0x0000, index, buffer,
132 			      size, MCS7830_CTRL_TIMEOUT);
133 	memcpy(data, buffer, size);
134 	kfree(buffer);
135 
136 	return ret;
137 }
138 
139 static int mcs7830_set_reg(struct usbnet *dev, u16 index, u16 size, const void *data)
140 {
141 	struct usb_device *xdev = dev->udev;
142 	int ret;
143 	void *buffer;
144 
145 	buffer = kmemdup(data, size, GFP_NOIO);
146 	if (buffer == NULL)
147 		return -ENOMEM;
148 
149 	ret = usb_control_msg(xdev, usb_sndctrlpipe(xdev, 0), MCS7830_WR_BREQ,
150 			      MCS7830_WR_BMREQ, 0x0000, index, buffer,
151 			      size, MCS7830_CTRL_TIMEOUT);
152 	kfree(buffer);
153 	return ret;
154 }
155 
156 static void mcs7830_async_cmd_callback(struct urb *urb)
157 {
158 	struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
159 	int status = urb->status;
160 
161 	if (status < 0)
162 		printk(KERN_DEBUG "%s() failed with %d\n",
163 		       __func__, status);
164 
165 	kfree(req);
166 	usb_free_urb(urb);
167 }
168 
169 static void mcs7830_set_reg_async(struct usbnet *dev, u16 index, u16 size, void *data)
170 {
171 	struct usb_ctrlrequest *req;
172 	int ret;
173 	struct urb *urb;
174 
175 	urb = usb_alloc_urb(0, GFP_ATOMIC);
176 	if (!urb) {
177 		dev_dbg(&dev->udev->dev,
178 			"Error allocating URB in write_cmd_async!\n");
179 		return;
180 	}
181 
182 	req = kmalloc(sizeof *req, GFP_ATOMIC);
183 	if (!req) {
184 		dev_err(&dev->udev->dev,
185 			"Failed to allocate memory for control request\n");
186 		goto out;
187 	}
188 	req->bRequestType = MCS7830_WR_BMREQ;
189 	req->bRequest = MCS7830_WR_BREQ;
190 	req->wValue = 0;
191 	req->wIndex = cpu_to_le16(index);
192 	req->wLength = cpu_to_le16(size);
193 
194 	usb_fill_control_urb(urb, dev->udev,
195 			     usb_sndctrlpipe(dev->udev, 0),
196 			     (void *)req, data, size,
197 			     mcs7830_async_cmd_callback, req);
198 
199 	ret = usb_submit_urb(urb, GFP_ATOMIC);
200 	if (ret < 0) {
201 		dev_err(&dev->udev->dev,
202 			"Error submitting the control message: ret=%d\n", ret);
203 		goto out;
204 	}
205 	return;
206 out:
207 	kfree(req);
208 	usb_free_urb(urb);
209 }
210 
211 static int mcs7830_hif_get_mac_address(struct usbnet *dev, unsigned char *addr)
212 {
213 	int ret = mcs7830_get_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN, addr);
214 	if (ret < 0)
215 		return ret;
216 	return 0;
217 }
218 
219 static int mcs7830_hif_set_mac_address(struct usbnet *dev, unsigned char *addr)
220 {
221 	int ret = mcs7830_set_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN, addr);
222 
223 	if (ret < 0)
224 		return ret;
225 	return 0;
226 }
227 
228 static int mcs7830_set_mac_address(struct net_device *netdev, void *p)
229 {
230 	int ret;
231 	struct usbnet *dev = netdev_priv(netdev);
232 	struct sockaddr *addr = p;
233 
234 	if (netif_running(netdev))
235 		return -EBUSY;
236 
237 	if (!is_valid_ether_addr(addr->sa_data))
238 		return -EINVAL;
239 
240 	ret = mcs7830_hif_set_mac_address(dev, addr->sa_data);
241 
242 	if (ret < 0)
243 		return ret;
244 
245 	/* it worked --> adopt it on netdev side */
246 	memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
247 
248 	return 0;
249 }
250 
251 static int mcs7830_read_phy(struct usbnet *dev, u8 index)
252 {
253 	int ret;
254 	int i;
255 	__le16 val;
256 
257 	u8 cmd[2] = {
258 		HIF_REG_PHY_CMD1_READ | HIF_REG_PHY_CMD1_PHYADDR,
259 		HIF_REG_PHY_CMD2_PEND_FLAG_BIT | index,
260 	};
261 
262 	mutex_lock(&dev->phy_mutex);
263 	/* write the MII command */
264 	ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
265 	if (ret < 0)
266 		goto out;
267 
268 	/* wait for the data to become valid, should be within < 1ms */
269 	for (i = 0; i < 10; i++) {
270 		ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
271 		if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT))
272 			break;
273 		ret = -EIO;
274 		msleep(1);
275 	}
276 	if (ret < 0)
277 		goto out;
278 
279 	/* read actual register contents */
280 	ret = mcs7830_get_reg(dev, HIF_REG_PHY_DATA, 2, &val);
281 	if (ret < 0)
282 		goto out;
283 	ret = le16_to_cpu(val);
284 	dev_dbg(&dev->udev->dev, "read PHY reg %02x: %04x (%d tries)\n",
285 		index, val, i);
286 out:
287 	mutex_unlock(&dev->phy_mutex);
288 	return ret;
289 }
290 
291 static int mcs7830_write_phy(struct usbnet *dev, u8 index, u16 val)
292 {
293 	int ret;
294 	int i;
295 	__le16 le_val;
296 
297 	u8 cmd[2] = {
298 		HIF_REG_PHY_CMD1_WRITE | HIF_REG_PHY_CMD1_PHYADDR,
299 		HIF_REG_PHY_CMD2_PEND_FLAG_BIT | (index & 0x1F),
300 	};
301 
302 	mutex_lock(&dev->phy_mutex);
303 
304 	/* write the new register contents */
305 	le_val = cpu_to_le16(val);
306 	ret = mcs7830_set_reg(dev, HIF_REG_PHY_DATA, 2, &le_val);
307 	if (ret < 0)
308 		goto out;
309 
310 	/* write the MII command */
311 	ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
312 	if (ret < 0)
313 		goto out;
314 
315 	/* wait for the command to be accepted by the PHY */
316 	for (i = 0; i < 10; i++) {
317 		ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
318 		if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT))
319 			break;
320 		ret = -EIO;
321 		msleep(1);
322 	}
323 	if (ret < 0)
324 		goto out;
325 
326 	ret = 0;
327 	dev_dbg(&dev->udev->dev, "write PHY reg %02x: %04x (%d tries)\n",
328 		index, val, i);
329 out:
330 	mutex_unlock(&dev->phy_mutex);
331 	return ret;
332 }
333 
334 /*
335  * This algorithm comes from the original mcs7830 version 1.4 driver,
336  * not sure if it is needed.
337  */
338 static int mcs7830_set_autoneg(struct usbnet *dev, int ptrUserPhyMode)
339 {
340 	int ret;
341 	/* Enable all media types */
342 	ret = mcs7830_write_phy(dev, MII_ADVERTISE, MCS7830_MII_ADVERTISE);
343 
344 	/* First reset BMCR */
345 	if (!ret)
346 		ret = mcs7830_write_phy(dev, MII_BMCR, 0x0000);
347 	/* Enable Auto Neg */
348 	if (!ret)
349 		ret = mcs7830_write_phy(dev, MII_BMCR, BMCR_ANENABLE);
350 	/* Restart Auto Neg (Keep the Enable Auto Neg Bit Set) */
351 	if (!ret)
352 		ret = mcs7830_write_phy(dev, MII_BMCR,
353 				BMCR_ANENABLE | BMCR_ANRESTART	);
354 	return ret < 0 ? : 0;
355 }
356 
357 
358 /*
359  * if we can read register 22, the chip revision is C or higher
360  */
361 static int mcs7830_get_rev(struct usbnet *dev)
362 {
363 	u8 dummy[2];
364 	int ret;
365 	ret = mcs7830_get_reg(dev, HIF_REG_FRAME_DROP_COUNTER, 2, dummy);
366 	if (ret > 0)
367 		return 2; /* Rev C or later */
368 	return 1; /* earlier revision */
369 }
370 
371 /*
372  * On rev. C we need to set the pause threshold
373  */
374 static void mcs7830_rev_C_fixup(struct usbnet *dev)
375 {
376 	u8 pause_threshold = HIF_REG_PAUSE_THRESHOLD_DEFAULT;
377 	int retry;
378 
379 	for (retry = 0; retry < 2; retry++) {
380 		if (mcs7830_get_rev(dev) == 2) {
381 			dev_info(&dev->udev->dev, "applying rev.C fixup\n");
382 			mcs7830_set_reg(dev, HIF_REG_PAUSE_THRESHOLD,
383 					1, &pause_threshold);
384 		}
385 		msleep(1);
386 	}
387 }
388 
389 static int mcs7830_mdio_read(struct net_device *netdev, int phy_id,
390 			     int location)
391 {
392 	struct usbnet *dev = netdev_priv(netdev);
393 	return mcs7830_read_phy(dev, location);
394 }
395 
396 static void mcs7830_mdio_write(struct net_device *netdev, int phy_id,
397 				int location, int val)
398 {
399 	struct usbnet *dev = netdev_priv(netdev);
400 	mcs7830_write_phy(dev, location, val);
401 }
402 
403 static int mcs7830_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
404 {
405 	struct usbnet *dev = netdev_priv(net);
406 	return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
407 }
408 
409 static inline struct mcs7830_data *mcs7830_get_data(struct usbnet *dev)
410 {
411 	return (struct mcs7830_data *)&dev->data;
412 }
413 
414 static void mcs7830_hif_update_multicast_hash(struct usbnet *dev)
415 {
416 	struct mcs7830_data *data = mcs7830_get_data(dev);
417 	mcs7830_set_reg_async(dev, HIF_REG_MULTICAST_HASH,
418 				sizeof data->multi_filter,
419 				data->multi_filter);
420 }
421 
422 static void mcs7830_hif_update_config(struct usbnet *dev)
423 {
424 	/* implementation specific to data->config
425            (argument needs to be heap-based anyway - USB DMA!) */
426 	struct mcs7830_data *data = mcs7830_get_data(dev);
427 	mcs7830_set_reg_async(dev, HIF_REG_CONFIG, 1, &data->config);
428 }
429 
430 static void mcs7830_data_set_multicast(struct net_device *net)
431 {
432 	struct usbnet *dev = netdev_priv(net);
433 	struct mcs7830_data *data = mcs7830_get_data(dev);
434 
435 	memset(data->multi_filter, 0, sizeof data->multi_filter);
436 
437 	data->config = HIF_REG_CONFIG_TXENABLE;
438 
439 	/* this should not be needed, but it doesn't work otherwise */
440 	data->config |= HIF_REG_CONFIG_ALLMULTICAST;
441 
442 	if (net->flags & IFF_PROMISC) {
443 		data->config |= HIF_REG_CONFIG_PROMISCUOUS;
444 	} else if (net->flags & IFF_ALLMULTI ||
445 		   netdev_mc_count(net) > MCS7830_MAX_MCAST) {
446 		data->config |= HIF_REG_CONFIG_ALLMULTICAST;
447 	} else if (netdev_mc_empty(net)) {
448 		/* just broadcast and directed */
449 	} else {
450 		/* We use the 20 byte dev->data
451 		 * for our 8 byte filter buffer
452 		 * to avoid allocating memory that
453 		 * is tricky to free later */
454 		struct netdev_hw_addr *ha;
455 		u32 crc_bits;
456 
457 		/* Build the multicast hash filter. */
458 		netdev_for_each_mc_addr(ha, net) {
459 			crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
460 			data->multi_filter[crc_bits >> 3] |= 1 << (crc_bits & 7);
461 		}
462 	}
463 }
464 
465 static int mcs7830_apply_base_config(struct usbnet *dev)
466 {
467 	int ret;
468 
469 	/* re-configure known MAC (suspend case etc.) */
470 	ret = mcs7830_hif_set_mac_address(dev, dev->net->dev_addr);
471 	if (ret) {
472 		dev_info(&dev->udev->dev, "Cannot set MAC address\n");
473 		goto out;
474 	}
475 
476 	/* Set up PHY */
477 	ret = mcs7830_set_autoneg(dev, 0);
478 	if (ret) {
479 		dev_info(&dev->udev->dev, "Cannot set autoneg\n");
480 		goto out;
481 	}
482 
483 	mcs7830_hif_update_multicast_hash(dev);
484 	mcs7830_hif_update_config(dev);
485 
486 	mcs7830_rev_C_fixup(dev);
487 	ret = 0;
488 out:
489 	return ret;
490 }
491 
492 /* credits go to asix_set_multicast */
493 static void mcs7830_set_multicast(struct net_device *net)
494 {
495 	struct usbnet *dev = netdev_priv(net);
496 
497 	mcs7830_data_set_multicast(net);
498 
499 	mcs7830_hif_update_multicast_hash(dev);
500 	mcs7830_hif_update_config(dev);
501 }
502 
503 static int mcs7830_get_regs_len(struct net_device *net)
504 {
505 	struct usbnet *dev = netdev_priv(net);
506 
507 	switch (mcs7830_get_rev(dev)) {
508 	case 1:
509 		return 21;
510 	case 2:
511 		return 32;
512 	}
513 	return 0;
514 }
515 
516 static void mcs7830_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *drvinfo)
517 {
518 	usbnet_get_drvinfo(net, drvinfo);
519 	drvinfo->regdump_len = mcs7830_get_regs_len(net);
520 }
521 
522 static void mcs7830_get_regs(struct net_device *net, struct ethtool_regs *regs, void *data)
523 {
524 	struct usbnet *dev = netdev_priv(net);
525 
526 	regs->version = mcs7830_get_rev(dev);
527 	mcs7830_get_reg(dev, 0, regs->len, data);
528 }
529 
530 static const struct ethtool_ops mcs7830_ethtool_ops = {
531 	.get_drvinfo		= mcs7830_get_drvinfo,
532 	.get_regs_len		= mcs7830_get_regs_len,
533 	.get_regs		= mcs7830_get_regs,
534 
535 	/* common usbnet calls */
536 	.get_link		= usbnet_get_link,
537 	.get_msglevel		= usbnet_get_msglevel,
538 	.set_msglevel		= usbnet_set_msglevel,
539 	.get_settings		= usbnet_get_settings,
540 	.set_settings		= usbnet_set_settings,
541 	.nway_reset		= usbnet_nway_reset,
542 };
543 
544 static const struct net_device_ops mcs7830_netdev_ops = {
545 	.ndo_open		= usbnet_open,
546 	.ndo_stop		= usbnet_stop,
547 	.ndo_start_xmit		= usbnet_start_xmit,
548 	.ndo_tx_timeout		= usbnet_tx_timeout,
549 	.ndo_change_mtu		= usbnet_change_mtu,
550 	.ndo_validate_addr	= eth_validate_addr,
551 	.ndo_do_ioctl 		= mcs7830_ioctl,
552 	.ndo_set_multicast_list = mcs7830_set_multicast,
553 	.ndo_set_mac_address	= mcs7830_set_mac_address,
554 };
555 
556 static int mcs7830_bind(struct usbnet *dev, struct usb_interface *udev)
557 {
558 	struct net_device *net = dev->net;
559 	int ret;
560 	int retry;
561 
562 	/* Initial startup: Gather MAC address setting from EEPROM */
563 	ret = -EINVAL;
564 	for (retry = 0; retry < 5 && ret; retry++)
565 		ret = mcs7830_hif_get_mac_address(dev, net->dev_addr);
566 	if (ret) {
567 		dev_warn(&dev->udev->dev, "Cannot read MAC address\n");
568 		goto out;
569 	}
570 
571 	mcs7830_data_set_multicast(net);
572 
573 	ret = mcs7830_apply_base_config(dev);
574 	if (ret)
575 		goto out;
576 
577 	net->ethtool_ops = &mcs7830_ethtool_ops;
578 	net->netdev_ops = &mcs7830_netdev_ops;
579 
580 	/* reserve space for the status byte on rx */
581 	dev->rx_urb_size = ETH_FRAME_LEN + 1;
582 
583 	dev->mii.mdio_read = mcs7830_mdio_read;
584 	dev->mii.mdio_write = mcs7830_mdio_write;
585 	dev->mii.dev = net;
586 	dev->mii.phy_id_mask = 0x3f;
587 	dev->mii.reg_num_mask = 0x1f;
588 	dev->mii.phy_id = *((u8 *) net->dev_addr + 1);
589 
590 	ret = usbnet_get_endpoints(dev, udev);
591 out:
592 	return ret;
593 }
594 
595 /* The chip always appends a status byte that we need to strip */
596 static int mcs7830_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
597 {
598 	u8 status;
599 
600 	if (skb->len == 0) {
601 		dev_err(&dev->udev->dev, "unexpected empty rx frame\n");
602 		return 0;
603 	}
604 
605 	skb_trim(skb, skb->len - 1);
606 	status = skb->data[skb->len];
607 
608 	if (status != MCS7830_RX_FRAME_CORRECT) {
609 		dev_dbg(&dev->udev->dev, "rx fixup status %x\n", status);
610 
611 		/* hmm, perhaps usbnet.c already sees a globally visible
612 		   frame error and increments rx_errors on its own already? */
613 		dev->net->stats.rx_errors++;
614 
615 		if (status &	(MCS7830_RX_SHORT_FRAME
616 				|MCS7830_RX_LENGTH_ERROR
617 				|MCS7830_RX_LARGE_FRAME))
618 			dev->net->stats.rx_length_errors++;
619 		if (status & MCS7830_RX_ALIGNMENT_ERROR)
620 			dev->net->stats.rx_frame_errors++;
621 		if (status & MCS7830_RX_CRC_ERROR)
622 			dev->net->stats.rx_crc_errors++;
623 	}
624 
625 	return skb->len > 0;
626 }
627 
628 static const struct driver_info moschip_info = {
629 	.description	= "MOSCHIP 7830/7730 usb-NET adapter",
630 	.bind		= mcs7830_bind,
631 	.rx_fixup	= mcs7830_rx_fixup,
632 	.flags		= FLAG_ETHER,
633 	.in		= 1,
634 	.out		= 2,
635 };
636 
637 static const struct driver_info sitecom_info = {
638 	.description    = "Sitecom LN-30 usb-NET adapter",
639 	.bind		= mcs7830_bind,
640 	.rx_fixup	= mcs7830_rx_fixup,
641 	.flags		= FLAG_ETHER,
642 	.in		= 1,
643 	.out		= 2,
644 };
645 
646 static const struct usb_device_id products[] = {
647 	{
648 		USB_DEVICE(MCS7830_VENDOR_ID, MCS7830_PRODUCT_ID),
649 		.driver_info = (unsigned long) &moschip_info,
650 	},
651 	{
652 		USB_DEVICE(MCS7830_VENDOR_ID, MCS7730_PRODUCT_ID),
653 		.driver_info = (unsigned long) &moschip_info,
654 	},
655 	{
656 		USB_DEVICE(SITECOM_VENDOR_ID, LN_030_PRODUCT_ID),
657 		.driver_info = (unsigned long) &sitecom_info,
658 	},
659 	{},
660 };
661 MODULE_DEVICE_TABLE(usb, products);
662 
663 static int mcs7830_reset_resume (struct usb_interface *intf)
664 {
665  	/* YES, this function is successful enough that ethtool -d
666            does show same output pre-/post-suspend */
667 
668 	struct usbnet		*dev = usb_get_intfdata(intf);
669 
670 	mcs7830_apply_base_config(dev);
671 
672 	usbnet_resume(intf);
673 
674 	return 0;
675 }
676 
677 static struct usb_driver mcs7830_driver = {
678 	.name = driver_name,
679 	.id_table = products,
680 	.probe = usbnet_probe,
681 	.disconnect = usbnet_disconnect,
682 	.suspend = usbnet_suspend,
683 	.resume = usbnet_resume,
684 	.reset_resume = mcs7830_reset_resume,
685 };
686 
687 static int __init mcs7830_init(void)
688 {
689 	return usb_register(&mcs7830_driver);
690 }
691 module_init(mcs7830_init);
692 
693 static void __exit mcs7830_exit(void)
694 {
695 	usb_deregister(&mcs7830_driver);
696 }
697 module_exit(mcs7830_exit);
698 
699 MODULE_DESCRIPTION("USB to network adapter MCS7830)");
700 MODULE_LICENSE("GPL");
701