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