1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42#include <linux/crc32.h>
43#include <linux/etherdevice.h>
44#include <linux/ethtool.h>
45#include <linux/mii.h>
46#include <linux/module.h>
47#include <linux/netdevice.h>
48#include <linux/slab.h>
49#include <linux/usb.h>
50#include <linux/usb/usbnet.h>
51
52
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 MCS7832_PRODUCT_ID 0x7832
65#define MCS7830_PRODUCT_ID 0x7830
66#define MCS7730_PRODUCT_ID 0x7730
67
68#define SITECOM_VENDOR_ID 0x0DF6
69#define LN_030_PRODUCT_ID 0x0021
70
71#define MCS7830_MII_ADVERTISE (ADVERTISE_PAUSE_CAP | ADVERTISE_100FULL | \
72 ADVERTISE_100HALF | ADVERTISE_10FULL | \
73 ADVERTISE_10HALF | ADVERTISE_CSMA)
74
75
76enum {
77 HIF_REG_MULTICAST_HASH = 0x00,
78 HIF_REG_PACKET_GAP1 = 0x08,
79 HIF_REG_PACKET_GAP2 = 0x09,
80 HIF_REG_PHY_DATA = 0x0a,
81 HIF_REG_PHY_CMD1 = 0x0c,
82 HIF_REG_PHY_CMD1_READ = 0x40,
83 HIF_REG_PHY_CMD1_WRITE = 0x20,
84 HIF_REG_PHY_CMD1_PHYADDR = 0x01,
85 HIF_REG_PHY_CMD2 = 0x0d,
86 HIF_REG_PHY_CMD2_PEND_FLAG_BIT = 0x80,
87 HIF_REG_PHY_CMD2_READY_FLAG_BIT = 0x40,
88 HIF_REG_CONFIG = 0x0e,
89
90 HIF_REG_CONFIG_CFG = 0x80,
91 HIF_REG_CONFIG_SPEED100 = 0x40,
92 HIF_REG_CONFIG_FULLDUPLEX_ENABLE = 0x20,
93 HIF_REG_CONFIG_RXENABLE = 0x10,
94 HIF_REG_CONFIG_TXENABLE = 0x08,
95 HIF_REG_CONFIG_SLEEPMODE = 0x04,
96 HIF_REG_CONFIG_ALLMULTICAST = 0x02,
97 HIF_REG_CONFIG_PROMISCUOUS = 0x01,
98 HIF_REG_ETHERNET_ADDR = 0x0f,
99 HIF_REG_FRAME_DROP_COUNTER = 0x15,
100 HIF_REG_PAUSE_THRESHOLD = 0x16,
101 HIF_REG_PAUSE_THRESHOLD_DEFAULT = 0,
102};
103
104
105enum {
106 MCS7830_RX_SHORT_FRAME = 0x01,
107 MCS7830_RX_LENGTH_ERROR = 0x02,
108 MCS7830_RX_ALIGNMENT_ERROR = 0x04,
109 MCS7830_RX_CRC_ERROR = 0x08,
110 MCS7830_RX_LARGE_FRAME = 0x10,
111 MCS7830_RX_FRAME_CORRECT = 0x20,
112
113};
114
115struct mcs7830_data {
116 u8 multi_filter[8];
117 u8 config;
118};
119
120static const char driver_name[] = "MOSCHIP usb-ethernet driver";
121
122static int mcs7830_get_reg(struct usbnet *dev, u16 index, u16 size, void *data)
123{
124 return usbnet_read_cmd(dev, MCS7830_RD_BREQ, MCS7830_RD_BMREQ,
125 0x0000, index, data, size);
126}
127
128static int mcs7830_set_reg(struct usbnet *dev, u16 index, u16 size, const void *data)
129{
130 return usbnet_write_cmd(dev, MCS7830_WR_BREQ, MCS7830_WR_BMREQ,
131 0x0000, index, data, size);
132}
133
134static void mcs7830_set_reg_async(struct usbnet *dev, u16 index, u16 size, void *data)
135{
136 usbnet_write_cmd_async(dev, MCS7830_WR_BREQ, MCS7830_WR_BMREQ,
137 0x0000, index, data, size);
138}
139
140static int mcs7830_hif_get_mac_address(struct usbnet *dev, unsigned char *addr)
141{
142 int ret = mcs7830_get_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN, addr);
143 if (ret < 0)
144 return ret;
145 return 0;
146}
147
148static int mcs7830_hif_set_mac_address(struct usbnet *dev, unsigned char *addr)
149{
150 int ret = mcs7830_set_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN, addr);
151
152 if (ret < 0)
153 return ret;
154 return 0;
155}
156
157static int mcs7830_set_mac_address(struct net_device *netdev, void *p)
158{
159 int ret;
160 struct usbnet *dev = netdev_priv(netdev);
161 struct sockaddr *addr = p;
162
163 if (netif_running(netdev))
164 return -EBUSY;
165
166 if (!is_valid_ether_addr(addr->sa_data))
167 return -EADDRNOTAVAIL;
168
169 ret = mcs7830_hif_set_mac_address(dev, addr->sa_data);
170
171 if (ret < 0)
172 return ret;
173
174
175 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
176
177 return 0;
178}
179
180static int mcs7830_read_phy(struct usbnet *dev, u8 index)
181{
182 int ret;
183 int i;
184 __le16 val;
185
186 u8 cmd[2] = {
187 HIF_REG_PHY_CMD1_READ | HIF_REG_PHY_CMD1_PHYADDR,
188 HIF_REG_PHY_CMD2_PEND_FLAG_BIT | index,
189 };
190
191 mutex_lock(&dev->phy_mutex);
192
193 ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
194 if (ret < 0)
195 goto out;
196
197
198 for (i = 0; i < 10; i++) {
199 ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
200 if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT))
201 break;
202 ret = -EIO;
203 msleep(1);
204 }
205 if (ret < 0)
206 goto out;
207
208
209 ret = mcs7830_get_reg(dev, HIF_REG_PHY_DATA, 2, &val);
210 if (ret < 0)
211 goto out;
212 ret = le16_to_cpu(val);
213 dev_dbg(&dev->udev->dev, "read PHY reg %02x: %04x (%d tries)\n",
214 index, val, i);
215out:
216 mutex_unlock(&dev->phy_mutex);
217 return ret;
218}
219
220static int mcs7830_write_phy(struct usbnet *dev, u8 index, u16 val)
221{
222 int ret;
223 int i;
224 __le16 le_val;
225
226 u8 cmd[2] = {
227 HIF_REG_PHY_CMD1_WRITE | HIF_REG_PHY_CMD1_PHYADDR,
228 HIF_REG_PHY_CMD2_PEND_FLAG_BIT | (index & 0x1F),
229 };
230
231 mutex_lock(&dev->phy_mutex);
232
233
234 le_val = cpu_to_le16(val);
235 ret = mcs7830_set_reg(dev, HIF_REG_PHY_DATA, 2, &le_val);
236 if (ret < 0)
237 goto out;
238
239
240 ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
241 if (ret < 0)
242 goto out;
243
244
245 for (i = 0; i < 10; i++) {
246 ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
247 if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT))
248 break;
249 ret = -EIO;
250 msleep(1);
251 }
252 if (ret < 0)
253 goto out;
254
255 ret = 0;
256 dev_dbg(&dev->udev->dev, "write PHY reg %02x: %04x (%d tries)\n",
257 index, val, i);
258out:
259 mutex_unlock(&dev->phy_mutex);
260 return ret;
261}
262
263
264
265
266
267static int mcs7830_set_autoneg(struct usbnet *dev, int ptrUserPhyMode)
268{
269 int ret;
270
271 ret = mcs7830_write_phy(dev, MII_ADVERTISE, MCS7830_MII_ADVERTISE);
272
273
274 if (!ret)
275 ret = mcs7830_write_phy(dev, MII_BMCR, 0x0000);
276
277 if (!ret)
278 ret = mcs7830_write_phy(dev, MII_BMCR, BMCR_ANENABLE);
279
280 if (!ret)
281 ret = mcs7830_write_phy(dev, MII_BMCR,
282 BMCR_ANENABLE | BMCR_ANRESTART );
283 return ret;
284}
285
286
287
288
289
290static int mcs7830_get_rev(struct usbnet *dev)
291{
292 u8 dummy[2];
293 int ret;
294 ret = mcs7830_get_reg(dev, HIF_REG_FRAME_DROP_COUNTER, 2, dummy);
295 if (ret > 0)
296 return 2;
297 return 1;
298}
299
300
301
302
303static void mcs7830_rev_C_fixup(struct usbnet *dev)
304{
305 u8 pause_threshold = HIF_REG_PAUSE_THRESHOLD_DEFAULT;
306 int retry;
307
308 for (retry = 0; retry < 2; retry++) {
309 if (mcs7830_get_rev(dev) == 2) {
310 dev_info(&dev->udev->dev, "applying rev.C fixup\n");
311 mcs7830_set_reg(dev, HIF_REG_PAUSE_THRESHOLD,
312 1, &pause_threshold);
313 }
314 msleep(1);
315 }
316}
317
318static int mcs7830_mdio_read(struct net_device *netdev, int phy_id,
319 int location)
320{
321 struct usbnet *dev = netdev_priv(netdev);
322 return mcs7830_read_phy(dev, location);
323}
324
325static void mcs7830_mdio_write(struct net_device *netdev, int phy_id,
326 int location, int val)
327{
328 struct usbnet *dev = netdev_priv(netdev);
329 mcs7830_write_phy(dev, location, val);
330}
331
332static int mcs7830_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
333{
334 struct usbnet *dev = netdev_priv(net);
335 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
336}
337
338static inline struct mcs7830_data *mcs7830_get_data(struct usbnet *dev)
339{
340 return (struct mcs7830_data *)&dev->data;
341}
342
343static void mcs7830_hif_update_multicast_hash(struct usbnet *dev)
344{
345 struct mcs7830_data *data = mcs7830_get_data(dev);
346 mcs7830_set_reg_async(dev, HIF_REG_MULTICAST_HASH,
347 sizeof data->multi_filter,
348 data->multi_filter);
349}
350
351static void mcs7830_hif_update_config(struct usbnet *dev)
352{
353
354
355 struct mcs7830_data *data = mcs7830_get_data(dev);
356 mcs7830_set_reg_async(dev, HIF_REG_CONFIG, 1, &data->config);
357}
358
359static void mcs7830_data_set_multicast(struct net_device *net)
360{
361 struct usbnet *dev = netdev_priv(net);
362 struct mcs7830_data *data = mcs7830_get_data(dev);
363
364 memset(data->multi_filter, 0, sizeof data->multi_filter);
365
366 data->config = HIF_REG_CONFIG_TXENABLE;
367
368
369 data->config |= HIF_REG_CONFIG_ALLMULTICAST;
370
371 if (net->flags & IFF_PROMISC) {
372 data->config |= HIF_REG_CONFIG_PROMISCUOUS;
373 } else if (net->flags & IFF_ALLMULTI ||
374 netdev_mc_count(net) > MCS7830_MAX_MCAST) {
375 data->config |= HIF_REG_CONFIG_ALLMULTICAST;
376 } else if (netdev_mc_empty(net)) {
377
378 } else {
379
380
381
382
383 struct netdev_hw_addr *ha;
384 u32 crc_bits;
385
386
387 netdev_for_each_mc_addr(ha, net) {
388 crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
389 data->multi_filter[crc_bits >> 3] |= 1 << (crc_bits & 7);
390 }
391 }
392}
393
394static int mcs7830_apply_base_config(struct usbnet *dev)
395{
396 int ret;
397
398
399 ret = mcs7830_hif_set_mac_address(dev, dev->net->dev_addr);
400 if (ret) {
401 dev_info(&dev->udev->dev, "Cannot set MAC address\n");
402 goto out;
403 }
404
405
406 ret = mcs7830_set_autoneg(dev, 0);
407 if (ret) {
408 dev_info(&dev->udev->dev, "Cannot set autoneg\n");
409 goto out;
410 }
411
412 mcs7830_hif_update_multicast_hash(dev);
413 mcs7830_hif_update_config(dev);
414
415 mcs7830_rev_C_fixup(dev);
416 ret = 0;
417out:
418 return ret;
419}
420
421
422static void mcs7830_set_multicast(struct net_device *net)
423{
424 struct usbnet *dev = netdev_priv(net);
425
426 mcs7830_data_set_multicast(net);
427
428 mcs7830_hif_update_multicast_hash(dev);
429 mcs7830_hif_update_config(dev);
430}
431
432static int mcs7830_get_regs_len(struct net_device *net)
433{
434 struct usbnet *dev = netdev_priv(net);
435
436 switch (mcs7830_get_rev(dev)) {
437 case 1:
438 return 21;
439 case 2:
440 return 32;
441 }
442 return 0;
443}
444
445static void mcs7830_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *drvinfo)
446{
447 usbnet_get_drvinfo(net, drvinfo);
448}
449
450static void mcs7830_get_regs(struct net_device *net, struct ethtool_regs *regs, void *data)
451{
452 struct usbnet *dev = netdev_priv(net);
453
454 regs->version = mcs7830_get_rev(dev);
455 mcs7830_get_reg(dev, 0, regs->len, data);
456}
457
458static const struct ethtool_ops mcs7830_ethtool_ops = {
459 .get_drvinfo = mcs7830_get_drvinfo,
460 .get_regs_len = mcs7830_get_regs_len,
461 .get_regs = mcs7830_get_regs,
462
463
464 .get_link = usbnet_get_link,
465 .get_msglevel = usbnet_get_msglevel,
466 .set_msglevel = usbnet_set_msglevel,
467 .get_settings = usbnet_get_settings,
468 .set_settings = usbnet_set_settings,
469 .nway_reset = usbnet_nway_reset,
470};
471
472static const struct net_device_ops mcs7830_netdev_ops = {
473 .ndo_open = usbnet_open,
474 .ndo_stop = usbnet_stop,
475 .ndo_start_xmit = usbnet_start_xmit,
476 .ndo_tx_timeout = usbnet_tx_timeout,
477 .ndo_change_mtu = usbnet_change_mtu,
478 .ndo_validate_addr = eth_validate_addr,
479 .ndo_do_ioctl = mcs7830_ioctl,
480 .ndo_set_rx_mode = mcs7830_set_multicast,
481 .ndo_set_mac_address = mcs7830_set_mac_address,
482};
483
484static int mcs7830_bind(struct usbnet *dev, struct usb_interface *udev)
485{
486 struct net_device *net = dev->net;
487 int ret;
488 int retry;
489
490
491 ret = -EINVAL;
492 for (retry = 0; retry < 5 && ret; retry++)
493 ret = mcs7830_hif_get_mac_address(dev, net->dev_addr);
494 if (ret) {
495 dev_warn(&dev->udev->dev, "Cannot read MAC address\n");
496 goto out;
497 }
498
499 mcs7830_data_set_multicast(net);
500
501 ret = mcs7830_apply_base_config(dev);
502 if (ret)
503 goto out;
504
505 net->ethtool_ops = &mcs7830_ethtool_ops;
506 net->netdev_ops = &mcs7830_netdev_ops;
507
508
509 dev->rx_urb_size = ETH_FRAME_LEN + 1;
510
511 dev->mii.mdio_read = mcs7830_mdio_read;
512 dev->mii.mdio_write = mcs7830_mdio_write;
513 dev->mii.dev = net;
514 dev->mii.phy_id_mask = 0x3f;
515 dev->mii.reg_num_mask = 0x1f;
516 dev->mii.phy_id = *((u8 *) net->dev_addr + 1);
517
518 ret = usbnet_get_endpoints(dev, udev);
519out:
520 return ret;
521}
522
523
524static int mcs7830_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
525{
526 u8 status;
527
528
529 if (skb->len < dev->net->hard_header_len) {
530 dev_err(&dev->udev->dev, "unexpected tiny rx frame\n");
531 return 0;
532 }
533
534 skb_trim(skb, skb->len - 1);
535 status = skb->data[skb->len];
536
537 if (status != MCS7830_RX_FRAME_CORRECT) {
538 dev_dbg(&dev->udev->dev, "rx fixup status %x\n", status);
539
540
541
542 dev->net->stats.rx_errors++;
543
544 if (status & (MCS7830_RX_SHORT_FRAME
545 |MCS7830_RX_LENGTH_ERROR
546 |MCS7830_RX_LARGE_FRAME))
547 dev->net->stats.rx_length_errors++;
548 if (status & MCS7830_RX_ALIGNMENT_ERROR)
549 dev->net->stats.rx_frame_errors++;
550 if (status & MCS7830_RX_CRC_ERROR)
551 dev->net->stats.rx_crc_errors++;
552 }
553
554 return skb->len > 0;
555}
556
557static void mcs7830_status(struct usbnet *dev, struct urb *urb)
558{
559 u8 *buf = urb->transfer_buffer;
560 bool link, link_changed;
561
562 if (urb->actual_length < 16)
563 return;
564
565 link = !(buf[1] == 0x20);
566 link_changed = netif_carrier_ok(dev->net) != link;
567 if (link_changed) {
568 usbnet_link_change(dev, link, 0);
569 netdev_dbg(dev->net, "Link Status is: %d\n", link);
570 }
571}
572
573static const struct driver_info moschip_info = {
574 .description = "MOSCHIP 7830/7832/7730 usb-NET adapter",
575 .bind = mcs7830_bind,
576 .rx_fixup = mcs7830_rx_fixup,
577 .flags = FLAG_ETHER | FLAG_LINK_INTR,
578 .status = mcs7830_status,
579 .in = 1,
580 .out = 2,
581};
582
583static const struct driver_info sitecom_info = {
584 .description = "Sitecom LN-30 usb-NET adapter",
585 .bind = mcs7830_bind,
586 .rx_fixup = mcs7830_rx_fixup,
587 .flags = FLAG_ETHER | FLAG_LINK_INTR,
588 .status = mcs7830_status,
589 .in = 1,
590 .out = 2,
591};
592
593static const struct usb_device_id products[] = {
594 {
595 USB_DEVICE(MCS7830_VENDOR_ID, MCS7832_PRODUCT_ID),
596 .driver_info = (unsigned long) &moschip_info,
597 },
598 {
599 USB_DEVICE(MCS7830_VENDOR_ID, MCS7830_PRODUCT_ID),
600 .driver_info = (unsigned long) &moschip_info,
601 },
602 {
603 USB_DEVICE(MCS7830_VENDOR_ID, MCS7730_PRODUCT_ID),
604 .driver_info = (unsigned long) &moschip_info,
605 },
606 {
607 USB_DEVICE(SITECOM_VENDOR_ID, LN_030_PRODUCT_ID),
608 .driver_info = (unsigned long) &sitecom_info,
609 },
610 {},
611};
612MODULE_DEVICE_TABLE(usb, products);
613
614static int mcs7830_reset_resume (struct usb_interface *intf)
615{
616
617
618
619 struct usbnet *dev = usb_get_intfdata(intf);
620
621 mcs7830_apply_base_config(dev);
622
623 usbnet_resume(intf);
624
625 return 0;
626}
627
628static struct usb_driver mcs7830_driver = {
629 .name = driver_name,
630 .id_table = products,
631 .probe = usbnet_probe,
632 .disconnect = usbnet_disconnect,
633 .suspend = usbnet_suspend,
634 .resume = usbnet_resume,
635 .reset_resume = mcs7830_reset_resume,
636 .disable_hub_initiated_lpm = 1,
637};
638
639module_usb_driver(mcs7830_driver);
640
641MODULE_DESCRIPTION("USB to network adapter MCS7830)");
642MODULE_LICENSE("GPL");
643