blob: 09bfa6a4dfbc17ecd40bc1fd37f23a8e88b75afe [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Arnd Bergmann2a36d702006-10-09 00:08:00 +02002/*
Andreas Mohrbbccc162010-12-20 10:38:47 -08003 * MOSCHIP MCS7830 based (7730/7830/7832) USB 2.0 Ethernet Devices
Arnd Bergmann2a36d702006-10-09 00:08:00 +02004 *
5 * based on usbnet.c, asix.c and the vendor provided mcs7830 driver
6 *
Andreas Mohr76802852010-01-31 02:58:19 +00007 * Copyright (C) 2010 Andreas Mohr <andi@lisas.de>
Arnd Bergmann2a36d702006-10-09 00:08:00 +02008 * Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de>
9 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
10 * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
11 * Copyright (c) 2002-2003 TiVo Inc.
12 *
Andreas Mohr76802852010-01-31 02:58:19 +000013 * Definitions gathered from MOSCHIP, Data Sheet_7830DA.pdf (thanks!).
14 *
Andreas Mohrbbccc162010-12-20 10:38:47 -080015 * 2010-12-19: add 7832 USB PID ("functionality same as MCS7830"),
16 * per active notification by manufacturer
17 *
Andreas Mohr76802852010-01-31 02:58:19 +000018 * TODO:
Andreas Mohr76802852010-01-31 02:58:19 +000019 * - support HIF_REG_CONFIG_SLEEPMODE/HIF_REG_CONFIG_TXENABLE (via autopm?)
20 * - implement ethtool_ops get_pauseparam/set_pauseparam
21 * via HIF_REG_PAUSE_THRESHOLD (>= revision C only!)
22 * - implement get_eeprom/[set_eeprom]
23 * - switch PHY on/off on ifup/ifdown (perhaps in usbnet.c, via MII)
24 * - mcs7830_get_regs() handling is weird: for rev 2 we return 32 regs,
25 * can access only ~ 24, remaining user buffer is uninitialized garbage
26 * - anything else?
Arnd Bergmann2a36d702006-10-09 00:08:00 +020027 */
28
29#include <linux/crc32.h>
30#include <linux/etherdevice.h>
31#include <linux/ethtool.h>
Arnd Bergmann2a36d702006-10-09 00:08:00 +020032#include <linux/mii.h>
33#include <linux/module.h>
34#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Arnd Bergmann2a36d702006-10-09 00:08:00 +020036#include <linux/usb.h>
Jussi Kivilinna3692e942008-01-26 00:51:45 +020037#include <linux/usb/usbnet.h>
Arnd Bergmann2a36d702006-10-09 00:08:00 +020038
39/* requests */
40#define MCS7830_RD_BMREQ (USB_DIR_IN | USB_TYPE_VENDOR | \
41 USB_RECIP_DEVICE)
42#define MCS7830_WR_BMREQ (USB_DIR_OUT | USB_TYPE_VENDOR | \
43 USB_RECIP_DEVICE)
44#define MCS7830_RD_BREQ 0x0E
45#define MCS7830_WR_BREQ 0x0D
46
47#define MCS7830_CTRL_TIMEOUT 1000
48#define MCS7830_MAX_MCAST 64
49
50#define MCS7830_VENDOR_ID 0x9710
Andreas Mohrbbccc162010-12-20 10:38:47 -080051#define MCS7832_PRODUCT_ID 0x7832
Arnd Bergmann2a36d702006-10-09 00:08:00 +020052#define MCS7830_PRODUCT_ID 0x7830
Arnd Bergmann8382cc12008-08-23 22:02:23 +020053#define MCS7730_PRODUCT_ID 0x7730
54
55#define SITECOM_VENDOR_ID 0x0DF6
56#define LN_030_PRODUCT_ID 0x0021
Arnd Bergmann2a36d702006-10-09 00:08:00 +020057
58#define MCS7830_MII_ADVERTISE (ADVERTISE_PAUSE_CAP | ADVERTISE_100FULL | \
59 ADVERTISE_100HALF | ADVERTISE_10FULL | \
60 ADVERTISE_10HALF | ADVERTISE_CSMA)
61
Andreas Mohr00e49912010-01-31 02:58:26 +000062/* HIF_REG_XX corresponding index value */
Arnd Bergmann2a36d702006-10-09 00:08:00 +020063enum {
64 HIF_REG_MULTICAST_HASH = 0x00,
65 HIF_REG_PACKET_GAP1 = 0x08,
66 HIF_REG_PACKET_GAP2 = 0x09,
67 HIF_REG_PHY_DATA = 0x0a,
68 HIF_REG_PHY_CMD1 = 0x0c,
69 HIF_REG_PHY_CMD1_READ = 0x40,
70 HIF_REG_PHY_CMD1_WRITE = 0x20,
71 HIF_REG_PHY_CMD1_PHYADDR = 0x01,
72 HIF_REG_PHY_CMD2 = 0x0d,
73 HIF_REG_PHY_CMD2_PEND_FLAG_BIT = 0x80,
74 HIF_REG_PHY_CMD2_READY_FLAG_BIT = 0x40,
75 HIF_REG_CONFIG = 0x0e,
Andreas Mohrc7746512010-01-31 02:58:33 +000076 /* hmm, spec sez: "R/W", "Except bit 3" (likely TXENABLE). */
Arnd Bergmann2a36d702006-10-09 00:08:00 +020077 HIF_REG_CONFIG_CFG = 0x80,
78 HIF_REG_CONFIG_SPEED100 = 0x40,
79 HIF_REG_CONFIG_FULLDUPLEX_ENABLE = 0x20,
80 HIF_REG_CONFIG_RXENABLE = 0x10,
81 HIF_REG_CONFIG_TXENABLE = 0x08,
82 HIF_REG_CONFIG_SLEEPMODE = 0x04,
83 HIF_REG_CONFIG_ALLMULTICAST = 0x02,
Andreas Mohr00e49912010-01-31 02:58:26 +000084 HIF_REG_CONFIG_PROMISCUOUS = 0x01,
Arnd Bergmann2a36d702006-10-09 00:08:00 +020085 HIF_REG_ETHERNET_ADDR = 0x0f,
Andreas Mohrc7746512010-01-31 02:58:33 +000086 HIF_REG_FRAME_DROP_COUNTER = 0x15, /* 0..ff; reset: 0 */
Arnd Bergmann2a36d702006-10-09 00:08:00 +020087 HIF_REG_PAUSE_THRESHOLD = 0x16,
88 HIF_REG_PAUSE_THRESHOLD_DEFAULT = 0,
89};
90
Andreas Mohr76802852010-01-31 02:58:19 +000091/* Trailing status byte in Ethernet Rx frame */
92enum {
93 MCS7830_RX_SHORT_FRAME = 0x01, /* < 64 bytes */
94 MCS7830_RX_LENGTH_ERROR = 0x02, /* framelen != Ethernet length field */
95 MCS7830_RX_ALIGNMENT_ERROR = 0x04, /* non-even number of nibbles */
96 MCS7830_RX_CRC_ERROR = 0x08,
97 MCS7830_RX_LARGE_FRAME = 0x10, /* > 1518 bytes */
98 MCS7830_RX_FRAME_CORRECT = 0x20, /* frame is correct */
99 /* [7:6] reserved */
100};
101
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200102struct mcs7830_data {
103 u8 multi_filter[8];
104 u8 config;
105};
106
107static const char driver_name[] = "MOSCHIP usb-ethernet driver";
108
109static int mcs7830_get_reg(struct usbnet *dev, u16 index, u16 size, void *data)
110{
Ming Lei9c06a2b2012-10-24 19:46:59 +0000111 return usbnet_read_cmd(dev, MCS7830_RD_BREQ, MCS7830_RD_BMREQ,
112 0x0000, index, data, size);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200113}
114
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000115static int mcs7830_set_reg(struct usbnet *dev, u16 index, u16 size, const void *data)
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200116{
Ming Lei9c06a2b2012-10-24 19:46:59 +0000117 return usbnet_write_cmd(dev, MCS7830_WR_BREQ, MCS7830_WR_BMREQ,
118 0x0000, index, data, size);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200119}
120
121static void mcs7830_set_reg_async(struct usbnet *dev, u16 index, u16 size, void *data)
122{
Ming Lei9c06a2b2012-10-24 19:46:59 +0000123 usbnet_write_cmd_async(dev, MCS7830_WR_BREQ, MCS7830_WR_BMREQ,
124 0x0000, index, data, size);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200125}
126
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000127static int mcs7830_hif_get_mac_address(struct usbnet *dev, unsigned char *addr)
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200128{
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000129 int ret = mcs7830_get_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN, addr);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200130 if (ret < 0)
131 return ret;
132 return 0;
133}
134
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000135static int mcs7830_hif_set_mac_address(struct usbnet *dev, unsigned char *addr)
136{
137 int ret = mcs7830_set_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN, addr);
138
139 if (ret < 0)
140 return ret;
141 return 0;
142}
143
144static int mcs7830_set_mac_address(struct net_device *netdev, void *p)
145{
146 int ret;
147 struct usbnet *dev = netdev_priv(netdev);
148 struct sockaddr *addr = p;
149
150 if (netif_running(netdev))
151 return -EBUSY;
152
153 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka15b8f4c2012-02-21 02:07:50 +0000154 return -EADDRNOTAVAIL;
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000155
156 ret = mcs7830_hif_set_mac_address(dev, addr->sa_data);
157
158 if (ret < 0)
159 return ret;
160
161 /* it worked --> adopt it on netdev side */
162 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
163
164 return 0;
165}
166
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200167static int mcs7830_read_phy(struct usbnet *dev, u8 index)
168{
169 int ret;
170 int i;
171 __le16 val;
172
173 u8 cmd[2] = {
174 HIF_REG_PHY_CMD1_READ | HIF_REG_PHY_CMD1_PHYADDR,
175 HIF_REG_PHY_CMD2_PEND_FLAG_BIT | index,
176 };
177
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200178 mutex_lock(&dev->phy_mutex);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200179 /* write the MII command */
180 ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
181 if (ret < 0)
182 goto out;
183
184 /* wait for the data to become valid, should be within < 1ms */
185 for (i = 0; i < 10; i++) {
186 ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
187 if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT))
188 break;
189 ret = -EIO;
190 msleep(1);
191 }
192 if (ret < 0)
193 goto out;
194
195 /* read actual register contents */
196 ret = mcs7830_get_reg(dev, HIF_REG_PHY_DATA, 2, &val);
197 if (ret < 0)
198 goto out;
199 ret = le16_to_cpu(val);
200 dev_dbg(&dev->udev->dev, "read PHY reg %02x: %04x (%d tries)\n",
201 index, val, i);
202out:
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200203 mutex_unlock(&dev->phy_mutex);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200204 return ret;
205}
206
207static int mcs7830_write_phy(struct usbnet *dev, u8 index, u16 val)
208{
209 int ret;
210 int i;
211 __le16 le_val;
212
213 u8 cmd[2] = {
214 HIF_REG_PHY_CMD1_WRITE | HIF_REG_PHY_CMD1_PHYADDR,
215 HIF_REG_PHY_CMD2_PEND_FLAG_BIT | (index & 0x1F),
216 };
217
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200218 mutex_lock(&dev->phy_mutex);
219
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200220 /* write the new register contents */
221 le_val = cpu_to_le16(val);
222 ret = mcs7830_set_reg(dev, HIF_REG_PHY_DATA, 2, &le_val);
223 if (ret < 0)
224 goto out;
225
226 /* write the MII command */
227 ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
228 if (ret < 0)
229 goto out;
230
231 /* wait for the command to be accepted by the PHY */
232 for (i = 0; i < 10; i++) {
233 ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
234 if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT))
235 break;
236 ret = -EIO;
237 msleep(1);
238 }
239 if (ret < 0)
240 goto out;
241
242 ret = 0;
243 dev_dbg(&dev->udev->dev, "write PHY reg %02x: %04x (%d tries)\n",
244 index, val, i);
245out:
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200246 mutex_unlock(&dev->phy_mutex);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200247 return ret;
248}
249
250/*
251 * This algorithm comes from the original mcs7830 version 1.4 driver,
252 * not sure if it is needed.
253 */
254static int mcs7830_set_autoneg(struct usbnet *dev, int ptrUserPhyMode)
255{
256 int ret;
257 /* Enable all media types */
258 ret = mcs7830_write_phy(dev, MII_ADVERTISE, MCS7830_MII_ADVERTISE);
259
260 /* First reset BMCR */
261 if (!ret)
262 ret = mcs7830_write_phy(dev, MII_BMCR, 0x0000);
263 /* Enable Auto Neg */
264 if (!ret)
265 ret = mcs7830_write_phy(dev, MII_BMCR, BMCR_ANENABLE);
266 /* Restart Auto Neg (Keep the Enable Auto Neg Bit Set) */
267 if (!ret)
268 ret = mcs7830_write_phy(dev, MII_BMCR,
269 BMCR_ANENABLE | BMCR_ANRESTART );
Dan Carpenter0e214ad2010-12-17 03:25:43 +0000270 return ret;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200271}
272
273
274/*
275 * if we can read register 22, the chip revision is C or higher
276 */
277static int mcs7830_get_rev(struct usbnet *dev)
278{
279 u8 dummy[2];
280 int ret;
Andreas Mohrc7746512010-01-31 02:58:33 +0000281 ret = mcs7830_get_reg(dev, HIF_REG_FRAME_DROP_COUNTER, 2, dummy);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200282 if (ret > 0)
283 return 2; /* Rev C or later */
284 return 1; /* earlier revision */
285}
286
287/*
288 * On rev. C we need to set the pause threshold
289 */
290static void mcs7830_rev_C_fixup(struct usbnet *dev)
291{
292 u8 pause_threshold = HIF_REG_PAUSE_THRESHOLD_DEFAULT;
293 int retry;
294
295 for (retry = 0; retry < 2; retry++) {
296 if (mcs7830_get_rev(dev) == 2) {
297 dev_info(&dev->udev->dev, "applying rev.C fixup\n");
298 mcs7830_set_reg(dev, HIF_REG_PAUSE_THRESHOLD,
299 1, &pause_threshold);
300 }
301 msleep(1);
302 }
303}
304
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200305static int mcs7830_mdio_read(struct net_device *netdev, int phy_id,
306 int location)
307{
Wang Chen8f15ea42008-11-12 23:38:36 -0800308 struct usbnet *dev = netdev_priv(netdev);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200309 return mcs7830_read_phy(dev, location);
310}
311
312static void mcs7830_mdio_write(struct net_device *netdev, int phy_id,
313 int location, int val)
314{
Wang Chen8f15ea42008-11-12 23:38:36 -0800315 struct usbnet *dev = netdev_priv(netdev);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200316 mcs7830_write_phy(dev, location, val);
317}
318
319static int mcs7830_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
320{
321 struct usbnet *dev = netdev_priv(net);
322 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
323}
324
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000325static inline struct mcs7830_data *mcs7830_get_data(struct usbnet *dev)
326{
327 return (struct mcs7830_data *)&dev->data;
328}
329
330static void mcs7830_hif_update_multicast_hash(struct usbnet *dev)
331{
332 struct mcs7830_data *data = mcs7830_get_data(dev);
333 mcs7830_set_reg_async(dev, HIF_REG_MULTICAST_HASH,
334 sizeof data->multi_filter,
335 data->multi_filter);
336}
337
338static void mcs7830_hif_update_config(struct usbnet *dev)
339{
340 /* implementation specific to data->config
341 (argument needs to be heap-based anyway - USB DMA!) */
342 struct mcs7830_data *data = mcs7830_get_data(dev);
343 mcs7830_set_reg_async(dev, HIF_REG_CONFIG, 1, &data->config);
344}
345
346static void mcs7830_data_set_multicast(struct net_device *net)
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200347{
348 struct usbnet *dev = netdev_priv(net);
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000349 struct mcs7830_data *data = mcs7830_get_data(dev);
350
351 memset(data->multi_filter, 0, sizeof data->multi_filter);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200352
353 data->config = HIF_REG_CONFIG_TXENABLE;
354
355 /* this should not be needed, but it doesn't work otherwise */
356 data->config |= HIF_REG_CONFIG_ALLMULTICAST;
357
358 if (net->flags & IFF_PROMISC) {
Andreas Mohr00e49912010-01-31 02:58:26 +0000359 data->config |= HIF_REG_CONFIG_PROMISCUOUS;
Joe Perches8e95a202009-12-03 07:58:21 +0000360 } else if (net->flags & IFF_ALLMULTI ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000361 netdev_mc_count(net) > MCS7830_MAX_MCAST) {
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200362 data->config |= HIF_REG_CONFIG_ALLMULTICAST;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000363 } else if (netdev_mc_empty(net)) {
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200364 /* just broadcast and directed */
365 } else {
366 /* We use the 20 byte dev->data
367 * for our 8 byte filter buffer
368 * to avoid allocating memory that
369 * is tricky to free later */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000370 struct netdev_hw_addr *ha;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200371 u32 crc_bits;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200372
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200373 /* Build the multicast hash filter. */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000374 netdev_for_each_mc_addr(ha, net) {
375 crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200376 data->multi_filter[crc_bits >> 3] |= 1 << (crc_bits & 7);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200377 }
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000378 }
379}
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200380
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000381static int mcs7830_apply_base_config(struct usbnet *dev)
382{
383 int ret;
384
385 /* re-configure known MAC (suspend case etc.) */
386 ret = mcs7830_hif_set_mac_address(dev, dev->net->dev_addr);
387 if (ret) {
388 dev_info(&dev->udev->dev, "Cannot set MAC address\n");
389 goto out;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200390 }
391
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000392 /* Set up PHY */
393 ret = mcs7830_set_autoneg(dev, 0);
394 if (ret) {
395 dev_info(&dev->udev->dev, "Cannot set autoneg\n");
396 goto out;
397 }
398
399 mcs7830_hif_update_multicast_hash(dev);
400 mcs7830_hif_update_config(dev);
401
402 mcs7830_rev_C_fixup(dev);
403 ret = 0;
404out:
405 return ret;
406}
407
408/* credits go to asix_set_multicast */
409static void mcs7830_set_multicast(struct net_device *net)
410{
411 struct usbnet *dev = netdev_priv(net);
412
413 mcs7830_data_set_multicast(net);
414
415 mcs7830_hif_update_multicast_hash(dev);
416 mcs7830_hif_update_config(dev);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200417}
418
419static int mcs7830_get_regs_len(struct net_device *net)
420{
421 struct usbnet *dev = netdev_priv(net);
422
423 switch (mcs7830_get_rev(dev)) {
424 case 1:
425 return 21;
426 case 2:
427 return 32;
428 }
429 return 0;
430}
431
432static void mcs7830_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *drvinfo)
433{
434 usbnet_get_drvinfo(net, drvinfo);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200435}
436
437static void mcs7830_get_regs(struct net_device *net, struct ethtool_regs *regs, void *data)
438{
439 struct usbnet *dev = netdev_priv(net);
440
441 regs->version = mcs7830_get_rev(dev);
442 mcs7830_get_reg(dev, 0, regs->len, data);
443}
444
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700445static const struct ethtool_ops mcs7830_ethtool_ops = {
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200446 .get_drvinfo = mcs7830_get_drvinfo,
447 .get_regs_len = mcs7830_get_regs_len,
448 .get_regs = mcs7830_get_regs,
449
450 /* common usbnet calls */
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200451 .get_link = usbnet_get_link,
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200452 .get_msglevel = usbnet_get_msglevel,
453 .set_msglevel = usbnet_set_msglevel,
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200454 .nway_reset = usbnet_nway_reset,
Philippe Reynes17400f72017-03-16 23:18:52 +0100455 .get_link_ksettings = usbnet_get_link_ksettings,
456 .set_link_ksettings = usbnet_set_link_ksettings,
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200457};
458
Stephen Hemmingere12c4f82009-03-20 19:35:57 +0000459static const struct net_device_ops mcs7830_netdev_ops = {
460 .ndo_open = usbnet_open,
461 .ndo_stop = usbnet_stop,
462 .ndo_start_xmit = usbnet_start_xmit,
463 .ndo_tx_timeout = usbnet_tx_timeout,
464 .ndo_change_mtu = usbnet_change_mtu,
Greg Ungererc8b5d122017-04-03 15:50:03 +1000465 .ndo_get_stats64 = usbnet_get_stats64,
Stephen Hemmingere12c4f82009-03-20 19:35:57 +0000466 .ndo_validate_addr = eth_validate_addr,
467 .ndo_do_ioctl = mcs7830_ioctl,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000468 .ndo_set_rx_mode = mcs7830_set_multicast,
Andreas Mohr00e49912010-01-31 02:58:26 +0000469 .ndo_set_mac_address = mcs7830_set_mac_address,
Stephen Hemmingere12c4f82009-03-20 19:35:57 +0000470};
471
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200472static int mcs7830_bind(struct usbnet *dev, struct usb_interface *udev)
473{
474 struct net_device *net = dev->net;
475 int ret;
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000476 int retry;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200477
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000478 /* Initial startup: Gather MAC address setting from EEPROM */
479 ret = -EINVAL;
480 for (retry = 0; retry < 5 && ret; retry++)
481 ret = mcs7830_hif_get_mac_address(dev, net->dev_addr);
482 if (ret) {
483 dev_warn(&dev->udev->dev, "Cannot read MAC address\n");
484 goto out;
485 }
486
487 mcs7830_data_set_multicast(net);
488
489 ret = mcs7830_apply_base_config(dev);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200490 if (ret)
491 goto out;
492
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200493 net->ethtool_ops = &mcs7830_ethtool_ops;
Stephen Hemmingere12c4f82009-03-20 19:35:57 +0000494 net->netdev_ops = &mcs7830_netdev_ops;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200495
496 /* reserve space for the status byte on rx */
497 dev->rx_urb_size = ETH_FRAME_LEN + 1;
498
499 dev->mii.mdio_read = mcs7830_mdio_read;
500 dev->mii.mdio_write = mcs7830_mdio_write;
501 dev->mii.dev = net;
502 dev->mii.phy_id_mask = 0x3f;
503 dev->mii.reg_num_mask = 0x1f;
504 dev->mii.phy_id = *((u8 *) net->dev_addr + 1);
505
506 ret = usbnet_get_endpoints(dev, udev);
507out:
508 return ret;
509}
510
Andreas Mohr00e49912010-01-31 02:58:26 +0000511/* The chip always appends a status byte that we need to strip */
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200512static int mcs7830_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
513{
514 u8 status;
515
Emil Goodeeb855692014-02-13 17:50:19 +0100516 /* This check is no longer done by usbnet */
517 if (skb->len < dev->net->hard_header_len) {
518 dev_err(&dev->udev->dev, "unexpected tiny rx frame\n");
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200519 return 0;
520 }
521
522 skb_trim(skb, skb->len - 1);
523 status = skb->data[skb->len];
524
Andreas Mohr76802852010-01-31 02:58:19 +0000525 if (status != MCS7830_RX_FRAME_CORRECT) {
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200526 dev_dbg(&dev->udev->dev, "rx fixup status %x\n", status);
527
Andreas Mohr76802852010-01-31 02:58:19 +0000528 /* hmm, perhaps usbnet.c already sees a globally visible
529 frame error and increments rx_errors on its own already? */
530 dev->net->stats.rx_errors++;
531
532 if (status & (MCS7830_RX_SHORT_FRAME
533 |MCS7830_RX_LENGTH_ERROR
534 |MCS7830_RX_LARGE_FRAME))
535 dev->net->stats.rx_length_errors++;
536 if (status & MCS7830_RX_ALIGNMENT_ERROR)
537 dev->net->stats.rx_frame_errors++;
538 if (status & MCS7830_RX_CRC_ERROR)
539 dev->net->stats.rx_crc_errors++;
540 }
541
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200542 return skb->len > 0;
543}
544
Ondrej Zaryb1ff4f92012-06-01 10:29:08 +0000545static void mcs7830_status(struct usbnet *dev, struct urb *urb)
546{
547 u8 *buf = urb->transfer_buffer;
Ondrej Zarydabdaf02012-10-11 22:51:41 +0000548 bool link, link_changed;
Ondrej Zaryb1ff4f92012-06-01 10:29:08 +0000549
550 if (urb->actual_length < 16)
551 return;
552
Octavian Purdila8d88bbf2013-12-23 19:06:31 +0200553 link = !(buf[1] == 0x20);
Ondrej Zarydabdaf02012-10-11 22:51:41 +0000554 link_changed = netif_carrier_ok(dev->net) != link;
555 if (link_changed) {
Octavian Purdila8d88bbf2013-12-23 19:06:31 +0200556 usbnet_link_change(dev, link, 0);
557 netdev_dbg(dev->net, "Link Status is: %d\n", link);
558 }
Ondrej Zaryb1ff4f92012-06-01 10:29:08 +0000559}
560
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200561static const struct driver_info moschip_info = {
Andreas Mohrbbccc162010-12-20 10:38:47 -0800562 .description = "MOSCHIP 7830/7832/7730 usb-NET adapter",
Arnd Bergmann8382cc12008-08-23 22:02:23 +0200563 .bind = mcs7830_bind,
564 .rx_fixup = mcs7830_rx_fixup,
Ondrej Zaryb1ff4f92012-06-01 10:29:08 +0000565 .flags = FLAG_ETHER | FLAG_LINK_INTR,
566 .status = mcs7830_status,
Arnd Bergmann8382cc12008-08-23 22:02:23 +0200567 .in = 1,
568 .out = 2,
569};
570
571static const struct driver_info sitecom_info = {
572 .description = "Sitecom LN-30 usb-NET adapter",
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200573 .bind = mcs7830_bind,
574 .rx_fixup = mcs7830_rx_fixup,
Ondrej Zaryb1ff4f92012-06-01 10:29:08 +0000575 .flags = FLAG_ETHER | FLAG_LINK_INTR,
576 .status = mcs7830_status,
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200577 .in = 1,
578 .out = 2,
579};
580
581static const struct usb_device_id products[] = {
582 {
Andreas Mohrbbccc162010-12-20 10:38:47 -0800583 USB_DEVICE(MCS7830_VENDOR_ID, MCS7832_PRODUCT_ID),
584 .driver_info = (unsigned long) &moschip_info,
585 },
586 {
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200587 USB_DEVICE(MCS7830_VENDOR_ID, MCS7830_PRODUCT_ID),
588 .driver_info = (unsigned long) &moschip_info,
589 },
Arnd Bergmann8382cc12008-08-23 22:02:23 +0200590 {
591 USB_DEVICE(MCS7830_VENDOR_ID, MCS7730_PRODUCT_ID),
592 .driver_info = (unsigned long) &moschip_info,
593 },
594 {
595 USB_DEVICE(SITECOM_VENDOR_ID, LN_030_PRODUCT_ID),
596 .driver_info = (unsigned long) &sitecom_info,
597 },
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200598 {},
599};
600MODULE_DEVICE_TABLE(usb, products);
601
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000602static int mcs7830_reset_resume (struct usb_interface *intf)
603{
604 /* YES, this function is successful enough that ethtool -d
605 does show same output pre-/post-suspend */
606
607 struct usbnet *dev = usb_get_intfdata(intf);
608
609 mcs7830_apply_base_config(dev);
610
611 usbnet_resume(intf);
612
613 return 0;
614}
615
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200616static struct usb_driver mcs7830_driver = {
617 .name = driver_name,
618 .id_table = products,
619 .probe = usbnet_probe,
620 .disconnect = usbnet_disconnect,
621 .suspend = usbnet_suspend,
622 .resume = usbnet_resume,
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000623 .reset_resume = mcs7830_reset_resume,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -0700624 .disable_hub_initiated_lpm = 1,
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200625};
626
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -0800627module_usb_driver(mcs7830_driver);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200628
629MODULE_DESCRIPTION("USB to network adapter MCS7830)");
630MODULE_LICENSE("GPL");