]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/net/usb/mcs7830.c
Merge branches 'topic/slab/fixes' and 'topic/slub/fixes' into for-linus
[net-next-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         int status = urb->status;
119
120         if (status < 0)
121                 printk(KERN_DEBUG "%s() failed with %d\n",
122                        __func__, status);
123
124         kfree(req);
125         usb_free_urb(urb);
126 }
127
128 static void mcs7830_set_reg_async(struct usbnet *dev, u16 index, u16 size, void *data)
129 {
130         struct usb_ctrlrequest *req;
131         int ret;
132         struct urb *urb;
133
134         urb = usb_alloc_urb(0, GFP_ATOMIC);
135         if (!urb) {
136                 dev_dbg(&dev->udev->dev,
137                         "Error allocating URB in write_cmd_async!\n");
138                 return;
139         }
140
141         req = kmalloc(sizeof *req, GFP_ATOMIC);
142         if (!req) {
143                 dev_err(&dev->udev->dev,
144                         "Failed to allocate memory for control request\n");
145                 goto out;
146         }
147         req->bRequestType = MCS7830_WR_BMREQ;
148         req->bRequest = MCS7830_WR_BREQ;
149         req->wValue = 0;
150         req->wIndex = cpu_to_le16(index);
151         req->wLength = cpu_to_le16(size);
152
153         usb_fill_control_urb(urb, dev->udev,
154                              usb_sndctrlpipe(dev->udev, 0),
155                              (void *)req, data, size,
156                              mcs7830_async_cmd_callback, req);
157
158         ret = usb_submit_urb(urb, GFP_ATOMIC);
159         if (ret < 0) {
160                 dev_err(&dev->udev->dev,
161                         "Error submitting the control message: ret=%d\n", ret);
162                 goto out;
163         }
164         return;
165 out:
166         kfree(req);
167         usb_free_urb(urb);
168 }
169
170 static int mcs7830_get_address(struct usbnet *dev)
171 {
172         int ret;
173         ret = mcs7830_get_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN,
174                                    dev->net->dev_addr);
175         if (ret < 0)
176                 return ret;
177         return 0;
178 }
179
180 static 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         /* write the MII command */
193         ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
194         if (ret < 0)
195                 goto out;
196
197         /* wait for the data to become valid, should be within < 1ms */
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         /* read actual register contents */
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);
215 out:
216         mutex_unlock(&dev->phy_mutex);
217         return ret;
218 }
219
220 static 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         /* write the new register contents */
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         /* write the MII command */
240         ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
241         if (ret < 0)
242                 goto out;
243
244         /* wait for the command to be accepted by the PHY */
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);
258 out:
259         mutex_unlock(&dev->phy_mutex);
260         return ret;
261 }
262
263 /*
264  * This algorithm comes from the original mcs7830 version 1.4 driver,
265  * not sure if it is needed.
266  */
267 static int mcs7830_set_autoneg(struct usbnet *dev, int ptrUserPhyMode)
268 {
269         int ret;
270         /* Enable all media types */
271         ret = mcs7830_write_phy(dev, MII_ADVERTISE, MCS7830_MII_ADVERTISE);
272
273         /* First reset BMCR */
274         if (!ret)
275                 ret = mcs7830_write_phy(dev, MII_BMCR, 0x0000);
276         /* Enable Auto Neg */
277         if (!ret)
278                 ret = mcs7830_write_phy(dev, MII_BMCR, BMCR_ANENABLE);
279         /* Restart Auto Neg (Keep the Enable Auto Neg Bit Set) */
280         if (!ret)
281                 ret = mcs7830_write_phy(dev, MII_BMCR,
282                                 BMCR_ANENABLE | BMCR_ANRESTART  );
283         return ret < 0 ? : 0;
284 }
285
286
287 /*
288  * if we can read register 22, the chip revision is C or higher
289  */
290 static int mcs7830_get_rev(struct usbnet *dev)
291 {
292         u8 dummy[2];
293         int ret;
294         ret = mcs7830_get_reg(dev, HIF_REG_22, 2, dummy);
295         if (ret > 0)
296                 return 2; /* Rev C or later */
297         return 1; /* earlier revision */
298 }
299
300 /*
301  * On rev. C we need to set the pause threshold
302  */
303 static 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
318 static int mcs7830_init_dev(struct usbnet *dev)
319 {
320         int ret;
321         int retry;
322
323         /* Read MAC address from EEPROM */
324         ret = -EINVAL;
325         for (retry = 0; retry < 5 && ret; retry++)
326                 ret = mcs7830_get_address(dev);
327         if (ret) {
328                 dev_warn(&dev->udev->dev, "Cannot read MAC address\n");
329                 goto out;
330         }
331
332         /* Set up PHY */
333         ret = mcs7830_set_autoneg(dev, 0);
334         if (ret) {
335                 dev_info(&dev->udev->dev, "Cannot set autoneg\n");
336                 goto out;
337         }
338
339         mcs7830_rev_C_fixup(dev);
340         ret = 0;
341 out:
342         return ret;
343 }
344
345 static int mcs7830_mdio_read(struct net_device *netdev, int phy_id,
346                              int location)
347 {
348         struct usbnet *dev = netdev_priv(netdev);
349         return mcs7830_read_phy(dev, location);
350 }
351
352 static void mcs7830_mdio_write(struct net_device *netdev, int phy_id,
353                                 int location, int val)
354 {
355         struct usbnet *dev = netdev_priv(netdev);
356         mcs7830_write_phy(dev, location, val);
357 }
358
359 static int mcs7830_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
360 {
361         struct usbnet *dev = netdev_priv(net);
362         return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
363 }
364
365 /* credits go to asix_set_multicast */
366 static void mcs7830_set_multicast(struct net_device *net)
367 {
368         struct usbnet *dev = netdev_priv(net);
369         struct mcs7830_data *data = (struct mcs7830_data *)&dev->data;
370
371         data->config = HIF_REG_CONFIG_TXENABLE;
372
373         /* this should not be needed, but it doesn't work otherwise */
374         data->config |= HIF_REG_CONFIG_ALLMULTICAST;
375
376         if (net->flags & IFF_PROMISC) {
377                 data->config |= HIF_REG_CONFIG_PROMISCIOUS;
378         } else if (net->flags & IFF_ALLMULTI
379                    || net->mc_count > MCS7830_MAX_MCAST) {
380                 data->config |= HIF_REG_CONFIG_ALLMULTICAST;
381         } else if (net->mc_count == 0) {
382                 /* just broadcast and directed */
383         } else {
384                 /* We use the 20 byte dev->data
385                  * for our 8 byte filter buffer
386                  * to avoid allocating memory that
387                  * is tricky to free later */
388                 struct dev_mc_list *mc_list = net->mc_list;
389                 u32 crc_bits;
390                 int i;
391
392                 memset(data->multi_filter, 0, sizeof data->multi_filter);
393
394                 /* Build the multicast hash filter. */
395                 for (i = 0; i < net->mc_count; i++) {
396                         crc_bits = ether_crc(ETH_ALEN, mc_list->dmi_addr) >> 26;
397                         data->multi_filter[crc_bits >> 3] |= 1 << (crc_bits & 7);
398                         mc_list = mc_list->next;
399                 }
400
401                 mcs7830_set_reg_async(dev, HIF_REG_MULTICAST_HASH,
402                                 sizeof data->multi_filter,
403                                 data->multi_filter);
404         }
405
406         mcs7830_set_reg_async(dev, HIF_REG_CONFIG, 1, &data->config);
407 }
408
409 static int mcs7830_get_regs_len(struct net_device *net)
410 {
411         struct usbnet *dev = netdev_priv(net);
412
413         switch (mcs7830_get_rev(dev)) {
414         case 1:
415                 return 21;
416         case 2:
417                 return 32;
418         }
419         return 0;
420 }
421
422 static void mcs7830_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *drvinfo)
423 {
424         usbnet_get_drvinfo(net, drvinfo);
425         drvinfo->regdump_len = mcs7830_get_regs_len(net);
426 }
427
428 static void mcs7830_get_regs(struct net_device *net, struct ethtool_regs *regs, void *data)
429 {
430         struct usbnet *dev = netdev_priv(net);
431
432         regs->version = mcs7830_get_rev(dev);
433         mcs7830_get_reg(dev, 0, regs->len, data);
434 }
435
436 static struct ethtool_ops mcs7830_ethtool_ops = {
437         .get_drvinfo            = mcs7830_get_drvinfo,
438         .get_regs_len           = mcs7830_get_regs_len,
439         .get_regs               = mcs7830_get_regs,
440
441         /* common usbnet calls */
442         .get_link               = usbnet_get_link,
443         .get_msglevel           = usbnet_get_msglevel,
444         .set_msglevel           = usbnet_set_msglevel,
445         .get_settings           = usbnet_get_settings,
446         .set_settings           = usbnet_set_settings,
447         .nway_reset             = usbnet_nway_reset,
448 };
449
450 static int mcs7830_set_mac_address(struct net_device *netdev, void *p)
451 {
452         int ret;
453         struct usbnet *dev = netdev_priv(netdev);
454         struct sockaddr *addr = p;
455
456         if (netif_running(netdev))
457                 return -EBUSY;
458
459         if (!is_valid_ether_addr(addr->sa_data))
460                 return -EINVAL;
461
462         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
463
464         ret = mcs7830_set_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN,
465                         netdev->dev_addr);
466
467         if (ret < 0)
468                 return ret;
469
470         return 0;
471 }
472
473 static int mcs7830_bind(struct usbnet *dev, struct usb_interface *udev)
474 {
475         struct net_device *net = dev->net;
476         int ret;
477
478         ret = mcs7830_init_dev(dev);
479         if (ret)
480                 goto out;
481
482         net->do_ioctl = mcs7830_ioctl;
483         net->ethtool_ops = &mcs7830_ethtool_ops;
484         net->set_multicast_list = mcs7830_set_multicast;
485         mcs7830_set_multicast(net);
486         net->set_mac_address = mcs7830_set_mac_address;
487
488         /* reserve space for the status byte on rx */
489         dev->rx_urb_size = ETH_FRAME_LEN + 1;
490
491         dev->mii.mdio_read = mcs7830_mdio_read;
492         dev->mii.mdio_write = mcs7830_mdio_write;
493         dev->mii.dev = net;
494         dev->mii.phy_id_mask = 0x3f;
495         dev->mii.reg_num_mask = 0x1f;
496         dev->mii.phy_id = *((u8 *) net->dev_addr + 1);
497
498         ret = usbnet_get_endpoints(dev, udev);
499 out:
500         return ret;
501 }
502
503 /* The chip always appends a status bytes that we need to strip */
504 static int mcs7830_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
505 {
506         u8 status;
507
508         if (skb->len == 0) {
509                 dev_err(&dev->udev->dev, "unexpected empty rx frame\n");
510                 return 0;
511         }
512
513         skb_trim(skb, skb->len - 1);
514         status = skb->data[skb->len];
515
516         if (status != 0x20)
517                 dev_dbg(&dev->udev->dev, "rx fixup status %x\n", status);
518
519         return skb->len > 0;
520 }
521
522 static const struct driver_info moschip_info = {
523         .description    = "MOSCHIP 7830/7730 usb-NET adapter",
524         .bind           = mcs7830_bind,
525         .rx_fixup       = mcs7830_rx_fixup,
526         .flags          = FLAG_ETHER,
527         .in             = 1,
528         .out            = 2,
529 };
530
531 static const struct driver_info sitecom_info = {
532         .description    = "Sitecom LN-30 usb-NET adapter",
533         .bind           = mcs7830_bind,
534         .rx_fixup       = mcs7830_rx_fixup,
535         .flags          = FLAG_ETHER,
536         .in             = 1,
537         .out            = 2,
538 };
539
540 static const struct usb_device_id products[] = {
541         {
542                 USB_DEVICE(MCS7830_VENDOR_ID, MCS7830_PRODUCT_ID),
543                 .driver_info = (unsigned long) &moschip_info,
544         },
545         {
546                 USB_DEVICE(MCS7830_VENDOR_ID, MCS7730_PRODUCT_ID),
547                 .driver_info = (unsigned long) &moschip_info,
548         },
549         {
550                 USB_DEVICE(SITECOM_VENDOR_ID, LN_030_PRODUCT_ID),
551                 .driver_info = (unsigned long) &sitecom_info,
552         },
553         {},
554 };
555 MODULE_DEVICE_TABLE(usb, products);
556
557 static struct usb_driver mcs7830_driver = {
558         .name = driver_name,
559         .id_table = products,
560         .probe = usbnet_probe,
561         .disconnect = usbnet_disconnect,
562         .suspend = usbnet_suspend,
563         .resume = usbnet_resume,
564 };
565
566 static int __init mcs7830_init(void)
567 {
568         return usb_register(&mcs7830_driver);
569 }
570 module_init(mcs7830_init);
571
572 static void __exit mcs7830_exit(void)
573 {
574         usb_deregister(&mcs7830_driver);
575 }
576 module_exit(mcs7830_exit);
577
578 MODULE_DESCRIPTION("USB to network adapter MCS7830)");
579 MODULE_LICENSE("GPL");