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