]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/net/usb/mcs7830.c
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6.git] / drivers / net / usb / mcs7830.c
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  *
6  * Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de>
7  * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
8  * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
9  * Copyright (c) 2002-2003 TiVo Inc.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26 #include <linux/crc32.h>
27 #include <linux/etherdevice.h>
28 #include <linux/ethtool.h>
29 #include <linux/init.h>
30 #include <linux/mii.h>
31 #include <linux/module.h>
32 #include <linux/netdevice.h>
33 #include <linux/usb.h>
34 #include <linux/usb/usbnet.h>
35
36 /* requests */
37 #define MCS7830_RD_BMREQ        (USB_DIR_IN  | USB_TYPE_VENDOR | \
38                                  USB_RECIP_DEVICE)
39 #define MCS7830_WR_BMREQ        (USB_DIR_OUT | USB_TYPE_VENDOR | \
40                                  USB_RECIP_DEVICE)
41 #define MCS7830_RD_BREQ         0x0E
42 #define MCS7830_WR_BREQ         0x0D
43
44 #define MCS7830_CTRL_TIMEOUT    1000
45 #define MCS7830_MAX_MCAST       64
46
47 #define MCS7830_VENDOR_ID       0x9710
48 #define MCS7830_PRODUCT_ID      0x7830
49 #define MCS7730_PRODUCT_ID      0x7730
50
51 #define SITECOM_VENDOR_ID       0x0DF6
52 #define LN_030_PRODUCT_ID       0x0021
53
54 #define MCS7830_MII_ADVERTISE   (ADVERTISE_PAUSE_CAP | ADVERTISE_100FULL | \
55                                  ADVERTISE_100HALF | ADVERTISE_10FULL | \
56                                  ADVERTISE_10HALF | ADVERTISE_CSMA)
57
58 /* HIF_REG_XX coressponding index value */
59 enum {
60         HIF_REG_MULTICAST_HASH                  = 0x00,
61         HIF_REG_PACKET_GAP1                     = 0x08,
62         HIF_REG_PACKET_GAP2                     = 0x09,
63         HIF_REG_PHY_DATA                        = 0x0a,
64         HIF_REG_PHY_CMD1                        = 0x0c,
65            HIF_REG_PHY_CMD1_READ                = 0x40,
66            HIF_REG_PHY_CMD1_WRITE               = 0x20,
67            HIF_REG_PHY_CMD1_PHYADDR             = 0x01,
68         HIF_REG_PHY_CMD2                        = 0x0d,
69            HIF_REG_PHY_CMD2_PEND_FLAG_BIT       = 0x80,
70            HIF_REG_PHY_CMD2_READY_FLAG_BIT      = 0x40,
71         HIF_REG_CONFIG                          = 0x0e,
72            HIF_REG_CONFIG_CFG                   = 0x80,
73            HIF_REG_CONFIG_SPEED100              = 0x40,
74            HIF_REG_CONFIG_FULLDUPLEX_ENABLE     = 0x20,
75            HIF_REG_CONFIG_RXENABLE              = 0x10,
76            HIF_REG_CONFIG_TXENABLE              = 0x08,
77            HIF_REG_CONFIG_SLEEPMODE             = 0x04,
78            HIF_REG_CONFIG_ALLMULTICAST          = 0x02,
79            HIF_REG_CONFIG_PROMISCIOUS           = 0x01,
80         HIF_REG_ETHERNET_ADDR                   = 0x0f,
81         HIF_REG_22                              = 0x15,
82         HIF_REG_PAUSE_THRESHOLD                 = 0x16,
83            HIF_REG_PAUSE_THRESHOLD_DEFAULT      = 0,
84 };
85
86 struct mcs7830_data {
87         u8 multi_filter[8];
88         u8 config;
89 };
90
91 static const char driver_name[] = "MOSCHIP usb-ethernet driver";
92
93 static int mcs7830_get_reg(struct usbnet *dev, u16 index, u16 size, void *data)
94 {
95         struct usb_device *xdev = dev->udev;
96         int ret;
97
98         ret = usb_control_msg(xdev, usb_rcvctrlpipe(xdev, 0), MCS7830_RD_BREQ,
99                               MCS7830_RD_BMREQ, 0x0000, index, data,
100                               size, MCS7830_CTRL_TIMEOUT);
101         return ret;
102 }
103
104 static int mcs7830_set_reg(struct usbnet *dev, u16 index, u16 size, void *data)
105 {
106         struct usb_device *xdev = dev->udev;
107         int ret;
108
109         ret = usb_control_msg(xdev, usb_sndctrlpipe(xdev, 0), MCS7830_WR_BREQ,
110                               MCS7830_WR_BMREQ, 0x0000, index, data,
111                               size, MCS7830_CTRL_TIMEOUT);
112         return ret;
113 }
114
115 static void mcs7830_async_cmd_callback(struct urb *urb)
116 {
117         struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
118
119         if (urb->status < 0)
120                 printk(KERN_DEBUG "%s() failed with %d\n",
121                        __func__, urb->status);
122
123         kfree(req);
124         usb_free_urb(urb);
125 }
126
127 static void mcs7830_set_reg_async(struct usbnet *dev, u16 index, u16 size, void *data)
128 {
129         struct usb_ctrlrequest *req;
130         int ret;
131         struct urb *urb;
132
133         urb = usb_alloc_urb(0, GFP_ATOMIC);
134         if (!urb) {
135                 dev_dbg(&dev->udev->dev,
136                         "Error allocating URB in write_cmd_async!\n");
137                 return;
138         }
139
140         req = kmalloc(sizeof *req, GFP_ATOMIC);
141         if (!req) {
142                 dev_err(&dev->udev->dev,
143                         "Failed to allocate memory for control request\n");
144                 goto out;
145         }
146         req->bRequestType = MCS7830_WR_BMREQ;
147         req->bRequest = MCS7830_WR_BREQ;
148         req->wValue = 0;
149         req->wIndex = cpu_to_le16(index);
150         req->wLength = cpu_to_le16(size);
151
152         usb_fill_control_urb(urb, dev->udev,
153                              usb_sndctrlpipe(dev->udev, 0),
154                              (void *)req, data, size,
155                              mcs7830_async_cmd_callback, req);
156
157         ret = usb_submit_urb(urb, GFP_ATOMIC);
158         if (ret < 0) {
159                 dev_err(&dev->udev->dev,
160                         "Error submitting the control message: ret=%d\n", ret);
161                 goto out;
162         }
163         return;
164 out:
165         kfree(req);
166         usb_free_urb(urb);
167 }
168
169 static int mcs7830_get_address(struct usbnet *dev)
170 {
171         int ret;
172         ret = mcs7830_get_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN,
173                                    dev->net->dev_addr);
174         if (ret < 0)
175                 return ret;
176         return 0;
177 }
178
179 static int mcs7830_read_phy(struct usbnet *dev, u8 index)
180 {
181         int ret;
182         int i;
183         __le16 val;
184
185         u8 cmd[2] = {
186                 HIF_REG_PHY_CMD1_READ | HIF_REG_PHY_CMD1_PHYADDR,
187                 HIF_REG_PHY_CMD2_PEND_FLAG_BIT | index,
188         };
189
190         mutex_lock(&dev->phy_mutex);
191         /* write the MII command */
192         ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
193         if (ret < 0)
194                 goto out;
195
196         /* wait for the data to become valid, should be within < 1ms */
197         for (i = 0; i < 10; i++) {
198                 ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
199                 if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT))
200                         break;
201                 ret = -EIO;
202                 msleep(1);
203         }
204         if (ret < 0)
205                 goto out;
206
207         /* read actual register contents */
208         ret = mcs7830_get_reg(dev, HIF_REG_PHY_DATA, 2, &val);
209         if (ret < 0)
210                 goto out;
211         ret = le16_to_cpu(val);
212         dev_dbg(&dev->udev->dev, "read PHY reg %02x: %04x (%d tries)\n",
213                 index, val, i);
214 out:
215         mutex_unlock(&dev->phy_mutex);
216         return ret;
217 }
218
219 static int mcs7830_write_phy(struct usbnet *dev, u8 index, u16 val)
220 {
221         int ret;
222         int i;
223         __le16 le_val;
224
225         u8 cmd[2] = {
226                 HIF_REG_PHY_CMD1_WRITE | HIF_REG_PHY_CMD1_PHYADDR,
227                 HIF_REG_PHY_CMD2_PEND_FLAG_BIT | (index & 0x1F),
228         };
229
230         mutex_lock(&dev->phy_mutex);
231
232         /* write the new register contents */
233         le_val = cpu_to_le16(val);
234         ret = mcs7830_set_reg(dev, HIF_REG_PHY_DATA, 2, &le_val);
235         if (ret < 0)
236                 goto out;
237
238         /* write the MII command */
239         ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
240         if (ret < 0)
241                 goto out;
242
243         /* wait for the command to be accepted by the PHY */
244         for (i = 0; i < 10; i++) {
245                 ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
246                 if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT))
247                         break;
248                 ret = -EIO;
249                 msleep(1);
250         }
251         if (ret < 0)
252                 goto out;
253
254         ret = 0;
255         dev_dbg(&dev->udev->dev, "write PHY reg %02x: %04x (%d tries)\n",
256                 index, val, i);
257 out:
258         mutex_unlock(&dev->phy_mutex);
259         return ret;
260 }
261
262 /*
263  * This algorithm comes from the original mcs7830 version 1.4 driver,
264  * not sure if it is needed.
265  */
266 static int mcs7830_set_autoneg(struct usbnet *dev, int ptrUserPhyMode)
267 {
268         int ret;
269         /* Enable all media types */
270         ret = mcs7830_write_phy(dev, MII_ADVERTISE, MCS7830_MII_ADVERTISE);
271
272         /* First reset BMCR */
273         if (!ret)
274                 ret = mcs7830_write_phy(dev, MII_BMCR, 0x0000);
275         /* Enable Auto Neg */
276         if (!ret)
277                 ret = mcs7830_write_phy(dev, MII_BMCR, BMCR_ANENABLE);
278         /* Restart Auto Neg (Keep the Enable Auto Neg Bit Set) */
279         if (!ret)
280                 ret = mcs7830_write_phy(dev, MII_BMCR,
281                                 BMCR_ANENABLE | BMCR_ANRESTART  );
282         return ret < 0 ? : 0;
283 }
284
285
286 /*
287  * if we can read register 22, the chip revision is C or higher
288  */
289 static int mcs7830_get_rev(struct usbnet *dev)
290 {
291         u8 dummy[2];
292         int ret;
293         ret = mcs7830_get_reg(dev, HIF_REG_22, 2, dummy);
294         if (ret > 0)
295                 return 2; /* Rev C or later */
296         return 1; /* earlier revision */
297 }
298
299 /*
300  * On rev. C we need to set the pause threshold
301  */
302 static void mcs7830_rev_C_fixup(struct usbnet *dev)
303 {
304         u8 pause_threshold = HIF_REG_PAUSE_THRESHOLD_DEFAULT;
305         int retry;
306
307         for (retry = 0; retry < 2; retry++) {
308                 if (mcs7830_get_rev(dev) == 2) {
309                         dev_info(&dev->udev->dev, "applying rev.C fixup\n");
310                         mcs7830_set_reg(dev, HIF_REG_PAUSE_THRESHOLD,
311                                         1, &pause_threshold);
312                 }
313                 msleep(1);
314         }
315 }
316
317 static int mcs7830_init_dev(struct usbnet *dev)
318 {
319         int ret;
320         int retry;
321
322         /* Read MAC address from EEPROM */
323         ret = -EINVAL;
324         for (retry = 0; retry < 5 && ret; retry++)
325                 ret = mcs7830_get_address(dev);
326         if (ret) {
327                 dev_warn(&dev->udev->dev, "Cannot read MAC address\n");
328                 goto out;
329         }
330
331         /* Set up PHY */
332         ret = mcs7830_set_autoneg(dev, 0);
333         if (ret) {
334                 dev_info(&dev->udev->dev, "Cannot set autoneg\n");
335                 goto out;
336         }
337
338         mcs7830_rev_C_fixup(dev);
339         ret = 0;
340 out:
341         return ret;
342 }
343
344 static int mcs7830_mdio_read(struct net_device *netdev, int phy_id,
345                              int location)
346 {
347         struct usbnet *dev = netdev_priv(netdev);
348         return mcs7830_read_phy(dev, location);
349 }
350
351 static void mcs7830_mdio_write(struct net_device *netdev, int phy_id,
352                                 int location, int val)
353 {
354         struct usbnet *dev = netdev_priv(netdev);
355         mcs7830_write_phy(dev, location, val);
356 }
357
358 static int mcs7830_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
359 {
360         struct usbnet *dev = netdev_priv(net);
361         return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
362 }
363
364 /* credits go to asix_set_multicast */
365 static void mcs7830_set_multicast(struct net_device *net)
366 {
367         struct usbnet *dev = netdev_priv(net);
368         struct mcs7830_data *data = (struct mcs7830_data *)&dev->data;
369
370         data->config = HIF_REG_CONFIG_TXENABLE;
371
372         /* this should not be needed, but it doesn't work otherwise */
373         data->config |= HIF_REG_CONFIG_ALLMULTICAST;
374
375         if (net->flags & IFF_PROMISC) {
376                 data->config |= HIF_REG_CONFIG_PROMISCIOUS;
377         } else if (net->flags & IFF_ALLMULTI
378                    || net->mc_count > MCS7830_MAX_MCAST) {
379                 data->config |= HIF_REG_CONFIG_ALLMULTICAST;
380         } else if (net->mc_count == 0) {
381                 /* just broadcast and directed */
382         } else {
383                 /* We use the 20 byte dev->data
384                  * for our 8 byte filter buffer
385                  * to avoid allocating memory that
386                  * is tricky to free later */
387                 struct dev_mc_list *mc_list = net->mc_list;
388                 u32 crc_bits;
389                 int i;
390
391                 memset(data->multi_filter, 0, sizeof data->multi_filter);
392
393                 /* Build the multicast hash filter. */
394                 for (i = 0; i < net->mc_count; i++) {
395                         crc_bits = ether_crc(ETH_ALEN, mc_list->dmi_addr) >> 26;
396                         data->multi_filter[crc_bits >> 3] |= 1 << (crc_bits & 7);
397                         mc_list = mc_list->next;
398                 }
399
400                 mcs7830_set_reg_async(dev, HIF_REG_MULTICAST_HASH,
401                                 sizeof data->multi_filter,
402                                 data->multi_filter);
403         }
404
405         mcs7830_set_reg_async(dev, HIF_REG_CONFIG, 1, &data->config);
406 }
407
408 static int mcs7830_get_regs_len(struct net_device *net)
409 {
410         struct usbnet *dev = netdev_priv(net);
411
412         switch (mcs7830_get_rev(dev)) {
413         case 1:
414                 return 21;
415         case 2:
416                 return 32;
417         }
418         return 0;
419 }
420
421 static void mcs7830_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *drvinfo)
422 {
423         usbnet_get_drvinfo(net, drvinfo);
424         drvinfo->regdump_len = mcs7830_get_regs_len(net);
425 }
426
427 static void mcs7830_get_regs(struct net_device *net, struct ethtool_regs *regs, void *data)
428 {
429         struct usbnet *dev = netdev_priv(net);
430
431         regs->version = mcs7830_get_rev(dev);
432         mcs7830_get_reg(dev, 0, regs->len, data);
433 }
434
435 static struct ethtool_ops mcs7830_ethtool_ops = {
436         .get_drvinfo            = mcs7830_get_drvinfo,
437         .get_regs_len           = mcs7830_get_regs_len,
438         .get_regs               = mcs7830_get_regs,
439
440         /* common usbnet calls */
441         .get_link               = usbnet_get_link,
442         .get_msglevel           = usbnet_get_msglevel,
443         .set_msglevel           = usbnet_set_msglevel,
444         .get_settings           = usbnet_get_settings,
445         .set_settings           = usbnet_set_settings,
446         .nway_reset             = usbnet_nway_reset,
447 };
448
449 static int mcs7830_set_mac_address(struct net_device *netdev, void *p)
450 {
451         int ret;
452         struct usbnet *dev = netdev_priv(netdev);
453         struct sockaddr *addr = p;
454
455         if (netif_running(netdev))
456                 return -EBUSY;
457
458         if (!is_valid_ether_addr(addr->sa_data))
459                 return -EINVAL;
460
461         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
462
463         ret = mcs7830_set_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN,
464                         netdev->dev_addr);
465
466         if (ret < 0)
467                 return ret;
468
469         return 0;
470 }
471
472 static int mcs7830_bind(struct usbnet *dev, struct usb_interface *udev)
473 {
474         struct net_device *net = dev->net;
475         int ret;
476
477         ret = mcs7830_init_dev(dev);
478         if (ret)
479                 goto out;
480
481         net->do_ioctl = mcs7830_ioctl;
482         net->ethtool_ops = &mcs7830_ethtool_ops;
483         net->set_multicast_list = mcs7830_set_multicast;
484         mcs7830_set_multicast(net);
485         net->set_mac_address = mcs7830_set_mac_address;
486
487         /* reserve space for the status byte on rx */
488         dev->rx_urb_size = ETH_FRAME_LEN + 1;
489
490         dev->mii.mdio_read = mcs7830_mdio_read;
491         dev->mii.mdio_write = mcs7830_mdio_write;
492         dev->mii.dev = net;
493         dev->mii.phy_id_mask = 0x3f;
494         dev->mii.reg_num_mask = 0x1f;
495         dev->mii.phy_id = *((u8 *) net->dev_addr + 1);
496
497         ret = usbnet_get_endpoints(dev, udev);
498 out:
499         return ret;
500 }
501
502 /* The chip always appends a status bytes that we need to strip */
503 static int mcs7830_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
504 {
505         u8 status;
506
507         if (skb->len == 0) {
508                 dev_err(&dev->udev->dev, "unexpected empty rx frame\n");
509                 return 0;
510         }
511
512         skb_trim(skb, skb->len - 1);
513         status = skb->data[skb->len];
514
515         if (status != 0x20)
516                 dev_dbg(&dev->udev->dev, "rx fixup status %x\n", status);
517
518         return skb->len > 0;
519 }
520
521 static const struct driver_info moschip_info = {
522         .description    = "MOSCHIP 7830/7730 usb-NET adapter",
523         .bind           = mcs7830_bind,
524         .rx_fixup       = mcs7830_rx_fixup,
525         .flags          = FLAG_ETHER,
526         .in             = 1,
527         .out            = 2,
528 };
529
530 static const struct driver_info sitecom_info = {
531         .description    = "Sitecom LN-30 usb-NET adapter",
532         .bind           = mcs7830_bind,
533         .rx_fixup       = mcs7830_rx_fixup,
534         .flags          = FLAG_ETHER,
535         .in             = 1,
536         .out            = 2,
537 };
538
539 static const struct usb_device_id products[] = {
540         {
541                 USB_DEVICE(MCS7830_VENDOR_ID, MCS7830_PRODUCT_ID),
542                 .driver_info = (unsigned long) &moschip_info,
543         },
544         {
545                 USB_DEVICE(MCS7830_VENDOR_ID, MCS7730_PRODUCT_ID),
546                 .driver_info = (unsigned long) &moschip_info,
547         },
548         {
549                 USB_DEVICE(SITECOM_VENDOR_ID, LN_030_PRODUCT_ID),
550                 .driver_info = (unsigned long) &sitecom_info,
551         },
552         {},
553 };
554 MODULE_DEVICE_TABLE(usb, products);
555
556 static struct usb_driver mcs7830_driver = {
557         .name = driver_name,
558         .id_table = products,
559         .probe = usbnet_probe,
560         .disconnect = usbnet_disconnect,
561         .suspend = usbnet_suspend,
562         .resume = usbnet_resume,
563 };
564
565 static int __init mcs7830_init(void)
566 {
567         return usb_register(&mcs7830_driver);
568 }
569 module_init(mcs7830_init);
570
571 static void __exit mcs7830_exit(void)
572 {
573         usb_deregister(&mcs7830_driver);
574 }
575 module_exit(mcs7830_exit);
576
577 MODULE_DESCRIPTION("USB to network adapter MCS7830)");
578 MODULE_LICENSE("GPL");