blob: 9f24e3f871e1ff67cdff9e9e4327cca53da3e867 [file] [log] [blame]
Arnd Bergmann2a36d702006-10-09 00:08:00 +02001/*
Andreas Mohr00e49912010-01-31 02:58:26 +00002 * MOSCHIP MCS7830 based USB 2.0 Ethernet Devices
Arnd Bergmann2a36d702006-10-09 00:08:00 +02003 *
4 * based on usbnet.c, asix.c and the vendor provided mcs7830 driver
5 *
Andreas Mohr76802852010-01-31 02:58:19 +00006 * Copyright (C) 2010 Andreas Mohr <andi@lisas.de>
Arnd Bergmann2a36d702006-10-09 00:08:00 +02007 * 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 *
Andreas Mohr76802852010-01-31 02:58:19 +000012 * Definitions gathered from MOSCHIP, Data Sheet_7830DA.pdf (thanks!).
13 *
14 * TODO:
Andreas Mohr76802852010-01-31 02:58:19 +000015 * - 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 *
Arnd Bergmann2a36d702006-10-09 00:08:00 +020025 * 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Arnd Bergmann2a36d702006-10-09 00:08:00 +020048#include <linux/usb.h>
Jussi Kivilinna3692e942008-01-26 00:51:45 +020049#include <linux/usb/usbnet.h>
Arnd Bergmann2a36d702006-10-09 00:08:00 +020050
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
Arnd Bergmann8382cc12008-08-23 22:02:23 +020064#define MCS7730_PRODUCT_ID 0x7730
65
66#define SITECOM_VENDOR_ID 0x0DF6
67#define LN_030_PRODUCT_ID 0x0021
Arnd Bergmann2a36d702006-10-09 00:08:00 +020068
69#define MCS7830_MII_ADVERTISE (ADVERTISE_PAUSE_CAP | ADVERTISE_100FULL | \
70 ADVERTISE_100HALF | ADVERTISE_10FULL | \
71 ADVERTISE_10HALF | ADVERTISE_CSMA)
72
Andreas Mohr00e49912010-01-31 02:58:26 +000073/* HIF_REG_XX corresponding index value */
Arnd Bergmann2a36d702006-10-09 00:08:00 +020074enum {
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,
Andreas Mohrc7746512010-01-31 02:58:33 +000087 /* hmm, spec sez: "R/W", "Except bit 3" (likely TXENABLE). */
Arnd Bergmann2a36d702006-10-09 00:08:00 +020088 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,
Andreas Mohr00e49912010-01-31 02:58:26 +000095 HIF_REG_CONFIG_PROMISCUOUS = 0x01,
Arnd Bergmann2a36d702006-10-09 00:08:00 +020096 HIF_REG_ETHERNET_ADDR = 0x0f,
Andreas Mohrc7746512010-01-31 02:58:33 +000097 HIF_REG_FRAME_DROP_COUNTER = 0x15, /* 0..ff; reset: 0 */
Arnd Bergmann2a36d702006-10-09 00:08:00 +020098 HIF_REG_PAUSE_THRESHOLD = 0x16,
99 HIF_REG_PAUSE_THRESHOLD_DEFAULT = 0,
100};
101
Andreas Mohr76802852010-01-31 02:58:19 +0000102/* Trailing status byte in Ethernet Rx frame */
103enum {
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
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200113struct mcs7830_data {
114 u8 multi_filter[8];
115 u8 config;
116};
117
118static const char driver_name[] = "MOSCHIP usb-ethernet driver";
119
120static int mcs7830_get_reg(struct usbnet *dev, u16 index, u16 size, void *data)
121{
122 struct usb_device *xdev = dev->udev;
123 int ret;
Christian Eggers6d317482009-01-21 12:56:24 -0800124 void *buffer;
125
126 buffer = kmalloc(size, GFP_NOIO);
127 if (buffer == NULL)
128 return -ENOMEM;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200129
130 ret = usb_control_msg(xdev, usb_rcvctrlpipe(xdev, 0), MCS7830_RD_BREQ,
Christian Eggers6d317482009-01-21 12:56:24 -0800131 MCS7830_RD_BMREQ, 0x0000, index, buffer,
Russ Dill1d39da32008-01-07 23:15:41 -0800132 size, MCS7830_CTRL_TIMEOUT);
Christian Eggers6d317482009-01-21 12:56:24 -0800133 memcpy(data, buffer, size);
134 kfree(buffer);
135
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200136 return ret;
137}
138
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000139static int mcs7830_set_reg(struct usbnet *dev, u16 index, u16 size, const void *data)
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200140{
141 struct usb_device *xdev = dev->udev;
142 int ret;
Christian Eggers6d317482009-01-21 12:56:24 -0800143 void *buffer;
144
145 buffer = kmalloc(size, GFP_NOIO);
146 if (buffer == NULL)
147 return -ENOMEM;
148
149 memcpy(buffer, data, size);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200150
151 ret = usb_control_msg(xdev, usb_sndctrlpipe(xdev, 0), MCS7830_WR_BREQ,
Christian Eggers6d317482009-01-21 12:56:24 -0800152 MCS7830_WR_BMREQ, 0x0000, index, buffer,
Russ Dill1d39da32008-01-07 23:15:41 -0800153 size, MCS7830_CTRL_TIMEOUT);
Christian Eggers6d317482009-01-21 12:56:24 -0800154 kfree(buffer);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200155 return ret;
156}
157
158static void mcs7830_async_cmd_callback(struct urb *urb)
159{
160 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800161 int status = urb->status;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200162
Oliver Neukumc94cb312008-12-18 23:00:59 -0800163 if (status < 0)
Joe Perches898eb712007-10-18 03:06:30 -0700164 printk(KERN_DEBUG "%s() failed with %d\n",
Oliver Neukumc94cb312008-12-18 23:00:59 -0800165 __func__, status);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200166
167 kfree(req);
168 usb_free_urb(urb);
169}
170
171static void mcs7830_set_reg_async(struct usbnet *dev, u16 index, u16 size, void *data)
172{
173 struct usb_ctrlrequest *req;
174 int ret;
175 struct urb *urb;
176
177 urb = usb_alloc_urb(0, GFP_ATOMIC);
178 if (!urb) {
Joe Perches898eb712007-10-18 03:06:30 -0700179 dev_dbg(&dev->udev->dev,
180 "Error allocating URB in write_cmd_async!\n");
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200181 return;
182 }
183
184 req = kmalloc(sizeof *req, GFP_ATOMIC);
185 if (!req) {
Joe Perches898eb712007-10-18 03:06:30 -0700186 dev_err(&dev->udev->dev,
187 "Failed to allocate memory for control request\n");
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200188 goto out;
189 }
190 req->bRequestType = MCS7830_WR_BMREQ;
191 req->bRequest = MCS7830_WR_BREQ;
192 req->wValue = 0;
193 req->wIndex = cpu_to_le16(index);
194 req->wLength = cpu_to_le16(size);
195
196 usb_fill_control_urb(urb, dev->udev,
197 usb_sndctrlpipe(dev->udev, 0),
198 (void *)req, data, size,
199 mcs7830_async_cmd_callback, req);
200
201 ret = usb_submit_urb(urb, GFP_ATOMIC);
202 if (ret < 0) {
Joe Perches898eb712007-10-18 03:06:30 -0700203 dev_err(&dev->udev->dev,
204 "Error submitting the control message: ret=%d\n", ret);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200205 goto out;
206 }
207 return;
208out:
209 kfree(req);
210 usb_free_urb(urb);
211}
212
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000213static int mcs7830_hif_get_mac_address(struct usbnet *dev, unsigned char *addr)
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200214{
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000215 int ret = mcs7830_get_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN, addr);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200216 if (ret < 0)
217 return ret;
218 return 0;
219}
220
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000221static int mcs7830_hif_set_mac_address(struct usbnet *dev, unsigned char *addr)
222{
223 int ret = mcs7830_set_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN, addr);
224
225 if (ret < 0)
226 return ret;
227 return 0;
228}
229
230static int mcs7830_set_mac_address(struct net_device *netdev, void *p)
231{
232 int ret;
233 struct usbnet *dev = netdev_priv(netdev);
234 struct sockaddr *addr = p;
235
236 if (netif_running(netdev))
237 return -EBUSY;
238
239 if (!is_valid_ether_addr(addr->sa_data))
240 return -EINVAL;
241
242 ret = mcs7830_hif_set_mac_address(dev, addr->sa_data);
243
244 if (ret < 0)
245 return ret;
246
247 /* it worked --> adopt it on netdev side */
248 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
249
250 return 0;
251}
252
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200253static int mcs7830_read_phy(struct usbnet *dev, u8 index)
254{
255 int ret;
256 int i;
257 __le16 val;
258
259 u8 cmd[2] = {
260 HIF_REG_PHY_CMD1_READ | HIF_REG_PHY_CMD1_PHYADDR,
261 HIF_REG_PHY_CMD2_PEND_FLAG_BIT | index,
262 };
263
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200264 mutex_lock(&dev->phy_mutex);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200265 /* write the MII command */
266 ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
267 if (ret < 0)
268 goto out;
269
270 /* wait for the data to become valid, should be within < 1ms */
271 for (i = 0; i < 10; i++) {
272 ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
273 if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT))
274 break;
275 ret = -EIO;
276 msleep(1);
277 }
278 if (ret < 0)
279 goto out;
280
281 /* read actual register contents */
282 ret = mcs7830_get_reg(dev, HIF_REG_PHY_DATA, 2, &val);
283 if (ret < 0)
284 goto out;
285 ret = le16_to_cpu(val);
286 dev_dbg(&dev->udev->dev, "read PHY reg %02x: %04x (%d tries)\n",
287 index, val, i);
288out:
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200289 mutex_unlock(&dev->phy_mutex);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200290 return ret;
291}
292
293static int mcs7830_write_phy(struct usbnet *dev, u8 index, u16 val)
294{
295 int ret;
296 int i;
297 __le16 le_val;
298
299 u8 cmd[2] = {
300 HIF_REG_PHY_CMD1_WRITE | HIF_REG_PHY_CMD1_PHYADDR,
301 HIF_REG_PHY_CMD2_PEND_FLAG_BIT | (index & 0x1F),
302 };
303
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200304 mutex_lock(&dev->phy_mutex);
305
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200306 /* write the new register contents */
307 le_val = cpu_to_le16(val);
308 ret = mcs7830_set_reg(dev, HIF_REG_PHY_DATA, 2, &le_val);
309 if (ret < 0)
310 goto out;
311
312 /* write the MII command */
313 ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
314 if (ret < 0)
315 goto out;
316
317 /* wait for the command to be accepted by the PHY */
318 for (i = 0; i < 10; i++) {
319 ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
320 if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT))
321 break;
322 ret = -EIO;
323 msleep(1);
324 }
325 if (ret < 0)
326 goto out;
327
328 ret = 0;
329 dev_dbg(&dev->udev->dev, "write PHY reg %02x: %04x (%d tries)\n",
330 index, val, i);
331out:
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200332 mutex_unlock(&dev->phy_mutex);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200333 return ret;
334}
335
336/*
337 * This algorithm comes from the original mcs7830 version 1.4 driver,
338 * not sure if it is needed.
339 */
340static int mcs7830_set_autoneg(struct usbnet *dev, int ptrUserPhyMode)
341{
342 int ret;
343 /* Enable all media types */
344 ret = mcs7830_write_phy(dev, MII_ADVERTISE, MCS7830_MII_ADVERTISE);
345
346 /* First reset BMCR */
347 if (!ret)
348 ret = mcs7830_write_phy(dev, MII_BMCR, 0x0000);
349 /* Enable Auto Neg */
350 if (!ret)
351 ret = mcs7830_write_phy(dev, MII_BMCR, BMCR_ANENABLE);
352 /* Restart Auto Neg (Keep the Enable Auto Neg Bit Set) */
353 if (!ret)
354 ret = mcs7830_write_phy(dev, MII_BMCR,
355 BMCR_ANENABLE | BMCR_ANRESTART );
356 return ret < 0 ? : 0;
357}
358
359
360/*
361 * if we can read register 22, the chip revision is C or higher
362 */
363static int mcs7830_get_rev(struct usbnet *dev)
364{
365 u8 dummy[2];
366 int ret;
Andreas Mohrc7746512010-01-31 02:58:33 +0000367 ret = mcs7830_get_reg(dev, HIF_REG_FRAME_DROP_COUNTER, 2, dummy);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200368 if (ret > 0)
369 return 2; /* Rev C or later */
370 return 1; /* earlier revision */
371}
372
373/*
374 * On rev. C we need to set the pause threshold
375 */
376static void mcs7830_rev_C_fixup(struct usbnet *dev)
377{
378 u8 pause_threshold = HIF_REG_PAUSE_THRESHOLD_DEFAULT;
379 int retry;
380
381 for (retry = 0; retry < 2; retry++) {
382 if (mcs7830_get_rev(dev) == 2) {
383 dev_info(&dev->udev->dev, "applying rev.C fixup\n");
384 mcs7830_set_reg(dev, HIF_REG_PAUSE_THRESHOLD,
385 1, &pause_threshold);
386 }
387 msleep(1);
388 }
389}
390
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200391static int mcs7830_mdio_read(struct net_device *netdev, int phy_id,
392 int location)
393{
Wang Chen8f15ea42008-11-12 23:38:36 -0800394 struct usbnet *dev = netdev_priv(netdev);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200395 return mcs7830_read_phy(dev, location);
396}
397
398static void mcs7830_mdio_write(struct net_device *netdev, int phy_id,
399 int location, int val)
400{
Wang Chen8f15ea42008-11-12 23:38:36 -0800401 struct usbnet *dev = netdev_priv(netdev);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200402 mcs7830_write_phy(dev, location, val);
403}
404
405static int mcs7830_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
406{
407 struct usbnet *dev = netdev_priv(net);
408 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
409}
410
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000411static inline struct mcs7830_data *mcs7830_get_data(struct usbnet *dev)
412{
413 return (struct mcs7830_data *)&dev->data;
414}
415
416static void mcs7830_hif_update_multicast_hash(struct usbnet *dev)
417{
418 struct mcs7830_data *data = mcs7830_get_data(dev);
419 mcs7830_set_reg_async(dev, HIF_REG_MULTICAST_HASH,
420 sizeof data->multi_filter,
421 data->multi_filter);
422}
423
424static void mcs7830_hif_update_config(struct usbnet *dev)
425{
426 /* implementation specific to data->config
427 (argument needs to be heap-based anyway - USB DMA!) */
428 struct mcs7830_data *data = mcs7830_get_data(dev);
429 mcs7830_set_reg_async(dev, HIF_REG_CONFIG, 1, &data->config);
430}
431
432static void mcs7830_data_set_multicast(struct net_device *net)
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200433{
434 struct usbnet *dev = netdev_priv(net);
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000435 struct mcs7830_data *data = mcs7830_get_data(dev);
436
437 memset(data->multi_filter, 0, sizeof data->multi_filter);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200438
439 data->config = HIF_REG_CONFIG_TXENABLE;
440
441 /* this should not be needed, but it doesn't work otherwise */
442 data->config |= HIF_REG_CONFIG_ALLMULTICAST;
443
444 if (net->flags & IFF_PROMISC) {
Andreas Mohr00e49912010-01-31 02:58:26 +0000445 data->config |= HIF_REG_CONFIG_PROMISCUOUS;
Joe Perches8e95a202009-12-03 07:58:21 +0000446 } else if (net->flags & IFF_ALLMULTI ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000447 netdev_mc_count(net) > MCS7830_MAX_MCAST) {
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200448 data->config |= HIF_REG_CONFIG_ALLMULTICAST;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000449 } else if (netdev_mc_empty(net)) {
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200450 /* just broadcast and directed */
451 } else {
452 /* We use the 20 byte dev->data
453 * for our 8 byte filter buffer
454 * to avoid allocating memory that
455 * is tricky to free later */
Jiri Pirkoa92635d2010-02-18 04:02:26 +0000456 struct dev_mc_list *mc_list;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200457 u32 crc_bits;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200458
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200459 /* Build the multicast hash filter. */
Jiri Pirkoa92635d2010-02-18 04:02:26 +0000460 netdev_for_each_mc_addr(mc_list, net) {
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200461 crc_bits = ether_crc(ETH_ALEN, mc_list->dmi_addr) >> 26;
462 data->multi_filter[crc_bits >> 3] |= 1 << (crc_bits & 7);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200463 }
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000464 }
465}
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200466
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000467static int mcs7830_apply_base_config(struct usbnet *dev)
468{
469 int ret;
470
471 /* re-configure known MAC (suspend case etc.) */
472 ret = mcs7830_hif_set_mac_address(dev, dev->net->dev_addr);
473 if (ret) {
474 dev_info(&dev->udev->dev, "Cannot set MAC address\n");
475 goto out;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200476 }
477
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000478 /* Set up PHY */
479 ret = mcs7830_set_autoneg(dev, 0);
480 if (ret) {
481 dev_info(&dev->udev->dev, "Cannot set autoneg\n");
482 goto out;
483 }
484
485 mcs7830_hif_update_multicast_hash(dev);
486 mcs7830_hif_update_config(dev);
487
488 mcs7830_rev_C_fixup(dev);
489 ret = 0;
490out:
491 return ret;
492}
493
494/* credits go to asix_set_multicast */
495static void mcs7830_set_multicast(struct net_device *net)
496{
497 struct usbnet *dev = netdev_priv(net);
498
499 mcs7830_data_set_multicast(net);
500
501 mcs7830_hif_update_multicast_hash(dev);
502 mcs7830_hif_update_config(dev);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200503}
504
505static int mcs7830_get_regs_len(struct net_device *net)
506{
507 struct usbnet *dev = netdev_priv(net);
508
509 switch (mcs7830_get_rev(dev)) {
510 case 1:
511 return 21;
512 case 2:
513 return 32;
514 }
515 return 0;
516}
517
518static void mcs7830_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *drvinfo)
519{
520 usbnet_get_drvinfo(net, drvinfo);
521 drvinfo->regdump_len = mcs7830_get_regs_len(net);
522}
523
524static void mcs7830_get_regs(struct net_device *net, struct ethtool_regs *regs, void *data)
525{
526 struct usbnet *dev = netdev_priv(net);
527
528 regs->version = mcs7830_get_rev(dev);
529 mcs7830_get_reg(dev, 0, regs->len, data);
530}
531
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700532static const struct ethtool_ops mcs7830_ethtool_ops = {
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200533 .get_drvinfo = mcs7830_get_drvinfo,
534 .get_regs_len = mcs7830_get_regs_len,
535 .get_regs = mcs7830_get_regs,
536
537 /* common usbnet calls */
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200538 .get_link = usbnet_get_link,
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200539 .get_msglevel = usbnet_get_msglevel,
540 .set_msglevel = usbnet_set_msglevel,
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200541 .get_settings = usbnet_get_settings,
542 .set_settings = usbnet_set_settings,
543 .nway_reset = usbnet_nway_reset,
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200544};
545
Stephen Hemmingere12c4f82009-03-20 19:35:57 +0000546static const struct net_device_ops mcs7830_netdev_ops = {
547 .ndo_open = usbnet_open,
548 .ndo_stop = usbnet_stop,
549 .ndo_start_xmit = usbnet_start_xmit,
550 .ndo_tx_timeout = usbnet_tx_timeout,
551 .ndo_change_mtu = usbnet_change_mtu,
552 .ndo_validate_addr = eth_validate_addr,
553 .ndo_do_ioctl = mcs7830_ioctl,
554 .ndo_set_multicast_list = mcs7830_set_multicast,
Andreas Mohr00e49912010-01-31 02:58:26 +0000555 .ndo_set_mac_address = mcs7830_set_mac_address,
Stephen Hemmingere12c4f82009-03-20 19:35:57 +0000556};
557
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200558static int mcs7830_bind(struct usbnet *dev, struct usb_interface *udev)
559{
560 struct net_device *net = dev->net;
561 int ret;
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000562 int retry;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200563
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000564 /* Initial startup: Gather MAC address setting from EEPROM */
565 ret = -EINVAL;
566 for (retry = 0; retry < 5 && ret; retry++)
567 ret = mcs7830_hif_get_mac_address(dev, net->dev_addr);
568 if (ret) {
569 dev_warn(&dev->udev->dev, "Cannot read MAC address\n");
570 goto out;
571 }
572
573 mcs7830_data_set_multicast(net);
574
575 ret = mcs7830_apply_base_config(dev);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200576 if (ret)
577 goto out;
578
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200579 net->ethtool_ops = &mcs7830_ethtool_ops;
Stephen Hemmingere12c4f82009-03-20 19:35:57 +0000580 net->netdev_ops = &mcs7830_netdev_ops;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200581
582 /* reserve space for the status byte on rx */
583 dev->rx_urb_size = ETH_FRAME_LEN + 1;
584
585 dev->mii.mdio_read = mcs7830_mdio_read;
586 dev->mii.mdio_write = mcs7830_mdio_write;
587 dev->mii.dev = net;
588 dev->mii.phy_id_mask = 0x3f;
589 dev->mii.reg_num_mask = 0x1f;
590 dev->mii.phy_id = *((u8 *) net->dev_addr + 1);
591
592 ret = usbnet_get_endpoints(dev, udev);
593out:
594 return ret;
595}
596
Andreas Mohr00e49912010-01-31 02:58:26 +0000597/* The chip always appends a status byte that we need to strip */
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200598static int mcs7830_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
599{
600 u8 status;
601
602 if (skb->len == 0) {
603 dev_err(&dev->udev->dev, "unexpected empty rx frame\n");
604 return 0;
605 }
606
607 skb_trim(skb, skb->len - 1);
608 status = skb->data[skb->len];
609
Andreas Mohr76802852010-01-31 02:58:19 +0000610 if (status != MCS7830_RX_FRAME_CORRECT) {
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200611 dev_dbg(&dev->udev->dev, "rx fixup status %x\n", status);
612
Andreas Mohr76802852010-01-31 02:58:19 +0000613 /* hmm, perhaps usbnet.c already sees a globally visible
614 frame error and increments rx_errors on its own already? */
615 dev->net->stats.rx_errors++;
616
617 if (status & (MCS7830_RX_SHORT_FRAME
618 |MCS7830_RX_LENGTH_ERROR
619 |MCS7830_RX_LARGE_FRAME))
620 dev->net->stats.rx_length_errors++;
621 if (status & MCS7830_RX_ALIGNMENT_ERROR)
622 dev->net->stats.rx_frame_errors++;
623 if (status & MCS7830_RX_CRC_ERROR)
624 dev->net->stats.rx_crc_errors++;
625 }
626
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200627 return skb->len > 0;
628}
629
630static const struct driver_info moschip_info = {
Arnd Bergmann8382cc12008-08-23 22:02:23 +0200631 .description = "MOSCHIP 7830/7730 usb-NET adapter",
632 .bind = mcs7830_bind,
633 .rx_fixup = mcs7830_rx_fixup,
634 .flags = FLAG_ETHER,
635 .in = 1,
636 .out = 2,
637};
638
639static const struct driver_info sitecom_info = {
640 .description = "Sitecom LN-30 usb-NET adapter",
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200641 .bind = mcs7830_bind,
642 .rx_fixup = mcs7830_rx_fixup,
643 .flags = FLAG_ETHER,
644 .in = 1,
645 .out = 2,
646};
647
648static const struct usb_device_id products[] = {
649 {
650 USB_DEVICE(MCS7830_VENDOR_ID, MCS7830_PRODUCT_ID),
651 .driver_info = (unsigned long) &moschip_info,
652 },
Arnd Bergmann8382cc12008-08-23 22:02:23 +0200653 {
654 USB_DEVICE(MCS7830_VENDOR_ID, MCS7730_PRODUCT_ID),
655 .driver_info = (unsigned long) &moschip_info,
656 },
657 {
658 USB_DEVICE(SITECOM_VENDOR_ID, LN_030_PRODUCT_ID),
659 .driver_info = (unsigned long) &sitecom_info,
660 },
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200661 {},
662};
663MODULE_DEVICE_TABLE(usb, products);
664
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000665static int mcs7830_reset_resume (struct usb_interface *intf)
666{
667 /* YES, this function is successful enough that ethtool -d
668 does show same output pre-/post-suspend */
669
670 struct usbnet *dev = usb_get_intfdata(intf);
671
672 mcs7830_apply_base_config(dev);
673
674 usbnet_resume(intf);
675
676 return 0;
677}
678
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200679static struct usb_driver mcs7830_driver = {
680 .name = driver_name,
681 .id_table = products,
682 .probe = usbnet_probe,
683 .disconnect = usbnet_disconnect,
684 .suspend = usbnet_suspend,
685 .resume = usbnet_resume,
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000686 .reset_resume = mcs7830_reset_resume,
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200687};
688
689static int __init mcs7830_init(void)
690{
691 return usb_register(&mcs7830_driver);
692}
693module_init(mcs7830_init);
694
695static void __exit mcs7830_exit(void)
696{
697 usb_deregister(&mcs7830_driver);
698}
699module_exit(mcs7830_exit);
700
701MODULE_DESCRIPTION("USB to network adapter MCS7830)");
702MODULE_LICENSE("GPL");