Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs-2.6
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / net / usb / mcs7830.c
blobc3d119f997f5d69e725b8c25346db8e9a6125989
1 /*
2 * MosChips MCS7830 based USB 2.0 Ethernet Devices
4 * based on usbnet.c, asix.c and the vendor provided mcs7830 driver
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.
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.
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.
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
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>
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
44 #define MCS7830_CTRL_TIMEOUT 1000
45 #define MCS7830_MAX_MCAST 64
47 #define MCS7830_VENDOR_ID 0x9710
48 #define MCS7830_PRODUCT_ID 0x7830
50 #define MCS7830_MII_ADVERTISE (ADVERTISE_PAUSE_CAP | ADVERTISE_100FULL | \
51 ADVERTISE_100HALF | ADVERTISE_10FULL | \
52 ADVERTISE_10HALF | ADVERTISE_CSMA)
54 /* HIF_REG_XX coressponding index value */
55 enum {
56 HIF_REG_MULTICAST_HASH = 0x00,
57 HIF_REG_PACKET_GAP1 = 0x08,
58 HIF_REG_PACKET_GAP2 = 0x09,
59 HIF_REG_PHY_DATA = 0x0a,
60 HIF_REG_PHY_CMD1 = 0x0c,
61 HIF_REG_PHY_CMD1_READ = 0x40,
62 HIF_REG_PHY_CMD1_WRITE = 0x20,
63 HIF_REG_PHY_CMD1_PHYADDR = 0x01,
64 HIF_REG_PHY_CMD2 = 0x0d,
65 HIF_REG_PHY_CMD2_PEND_FLAG_BIT = 0x80,
66 HIF_REG_PHY_CMD2_READY_FLAG_BIT = 0x40,
67 HIF_REG_CONFIG = 0x0e,
68 HIF_REG_CONFIG_CFG = 0x80,
69 HIF_REG_CONFIG_SPEED100 = 0x40,
70 HIF_REG_CONFIG_FULLDUPLEX_ENABLE = 0x20,
71 HIF_REG_CONFIG_RXENABLE = 0x10,
72 HIF_REG_CONFIG_TXENABLE = 0x08,
73 HIF_REG_CONFIG_SLEEPMODE = 0x04,
74 HIF_REG_CONFIG_ALLMULTICAST = 0x02,
75 HIF_REG_CONFIG_PROMISCIOUS = 0x01,
76 HIF_REG_ETHERNET_ADDR = 0x0f,
77 HIF_REG_22 = 0x15,
78 HIF_REG_PAUSE_THRESHOLD = 0x16,
79 HIF_REG_PAUSE_THRESHOLD_DEFAULT = 0,
82 struct mcs7830_data {
83 u8 multi_filter[8];
84 u8 config;
87 static const char driver_name[] = "MOSCHIP usb-ethernet driver";
89 static int mcs7830_get_reg(struct usbnet *dev, u16 index, u16 size, void *data)
91 struct usb_device *xdev = dev->udev;
92 int ret;
94 ret = usb_control_msg(xdev, usb_rcvctrlpipe(xdev, 0), MCS7830_RD_BREQ,
95 MCS7830_RD_BMREQ, 0x0000, index, data,
96 size, MCS7830_CTRL_TIMEOUT);
97 return ret;
100 static int mcs7830_set_reg(struct usbnet *dev, u16 index, u16 size, void *data)
102 struct usb_device *xdev = dev->udev;
103 int ret;
105 ret = usb_control_msg(xdev, usb_sndctrlpipe(xdev, 0), MCS7830_WR_BREQ,
106 MCS7830_WR_BMREQ, 0x0000, index, data,
107 size, MCS7830_CTRL_TIMEOUT);
108 return ret;
111 static void mcs7830_async_cmd_callback(struct urb *urb)
113 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
115 if (urb->status < 0)
116 printk(KERN_DEBUG "%s() failed with %d\n",
117 __FUNCTION__, urb->status);
119 kfree(req);
120 usb_free_urb(urb);
123 static void mcs7830_set_reg_async(struct usbnet *dev, u16 index, u16 size, void *data)
125 struct usb_ctrlrequest *req;
126 int ret;
127 struct urb *urb;
129 urb = usb_alloc_urb(0, GFP_ATOMIC);
130 if (!urb) {
131 dev_dbg(&dev->udev->dev,
132 "Error allocating URB in write_cmd_async!\n");
133 return;
136 req = kmalloc(sizeof *req, GFP_ATOMIC);
137 if (!req) {
138 dev_err(&dev->udev->dev,
139 "Failed to allocate memory for control request\n");
140 goto out;
142 req->bRequestType = MCS7830_WR_BMREQ;
143 req->bRequest = MCS7830_WR_BREQ;
144 req->wValue = 0;
145 req->wIndex = cpu_to_le16(index);
146 req->wLength = cpu_to_le16(size);
148 usb_fill_control_urb(urb, dev->udev,
149 usb_sndctrlpipe(dev->udev, 0),
150 (void *)req, data, size,
151 mcs7830_async_cmd_callback, req);
153 ret = usb_submit_urb(urb, GFP_ATOMIC);
154 if (ret < 0) {
155 dev_err(&dev->udev->dev,
156 "Error submitting the control message: ret=%d\n", ret);
157 goto out;
159 return;
160 out:
161 kfree(req);
162 usb_free_urb(urb);
165 static int mcs7830_get_address(struct usbnet *dev)
167 int ret;
168 ret = mcs7830_get_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN,
169 dev->net->dev_addr);
170 if (ret < 0)
171 return ret;
172 return 0;
175 static int mcs7830_read_phy(struct usbnet *dev, u8 index)
177 int ret;
178 int i;
179 __le16 val;
181 u8 cmd[2] = {
182 HIF_REG_PHY_CMD1_READ | HIF_REG_PHY_CMD1_PHYADDR,
183 HIF_REG_PHY_CMD2_PEND_FLAG_BIT | index,
186 mutex_lock(&dev->phy_mutex);
187 /* write the MII command */
188 ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
189 if (ret < 0)
190 goto out;
192 /* wait for the data to become valid, should be within < 1ms */
193 for (i = 0; i < 10; i++) {
194 ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
195 if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT))
196 break;
197 ret = -EIO;
198 msleep(1);
200 if (ret < 0)
201 goto out;
203 /* read actual register contents */
204 ret = mcs7830_get_reg(dev, HIF_REG_PHY_DATA, 2, &val);
205 if (ret < 0)
206 goto out;
207 ret = le16_to_cpu(val);
208 dev_dbg(&dev->udev->dev, "read PHY reg %02x: %04x (%d tries)\n",
209 index, val, i);
210 out:
211 mutex_unlock(&dev->phy_mutex);
212 return ret;
215 static int mcs7830_write_phy(struct usbnet *dev, u8 index, u16 val)
217 int ret;
218 int i;
219 __le16 le_val;
221 u8 cmd[2] = {
222 HIF_REG_PHY_CMD1_WRITE | HIF_REG_PHY_CMD1_PHYADDR,
223 HIF_REG_PHY_CMD2_PEND_FLAG_BIT | (index & 0x1F),
226 mutex_lock(&dev->phy_mutex);
228 /* write the new register contents */
229 le_val = cpu_to_le16(val);
230 ret = mcs7830_set_reg(dev, HIF_REG_PHY_DATA, 2, &le_val);
231 if (ret < 0)
232 goto out;
234 /* write the MII command */
235 ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
236 if (ret < 0)
237 goto out;
239 /* wait for the command to be accepted by the PHY */
240 for (i = 0; i < 10; i++) {
241 ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
242 if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT))
243 break;
244 ret = -EIO;
245 msleep(1);
247 if (ret < 0)
248 goto out;
250 ret = 0;
251 dev_dbg(&dev->udev->dev, "write PHY reg %02x: %04x (%d tries)\n",
252 index, val, i);
253 out:
254 mutex_unlock(&dev->phy_mutex);
255 return ret;
259 * This algorithm comes from the original mcs7830 version 1.4 driver,
260 * not sure if it is needed.
262 static int mcs7830_set_autoneg(struct usbnet *dev, int ptrUserPhyMode)
264 int ret;
265 /* Enable all media types */
266 ret = mcs7830_write_phy(dev, MII_ADVERTISE, MCS7830_MII_ADVERTISE);
268 /* First reset BMCR */
269 if (!ret)
270 ret = mcs7830_write_phy(dev, MII_BMCR, 0x0000);
271 /* Enable Auto Neg */
272 if (!ret)
273 ret = mcs7830_write_phy(dev, MII_BMCR, BMCR_ANENABLE);
274 /* Restart Auto Neg (Keep the Enable Auto Neg Bit Set) */
275 if (!ret)
276 ret = mcs7830_write_phy(dev, MII_BMCR,
277 BMCR_ANENABLE | BMCR_ANRESTART );
278 return ret < 0 ? : 0;
283 * if we can read register 22, the chip revision is C or higher
285 static int mcs7830_get_rev(struct usbnet *dev)
287 u8 dummy[2];
288 int ret;
289 ret = mcs7830_get_reg(dev, HIF_REG_22, 2, dummy);
290 if (ret > 0)
291 return 2; /* Rev C or later */
292 return 1; /* earlier revision */
296 * On rev. C we need to set the pause threshold
298 static void mcs7830_rev_C_fixup(struct usbnet *dev)
300 u8 pause_threshold = HIF_REG_PAUSE_THRESHOLD_DEFAULT;
301 int retry;
303 for (retry = 0; retry < 2; retry++) {
304 if (mcs7830_get_rev(dev) == 2) {
305 dev_info(&dev->udev->dev, "applying rev.C fixup\n");
306 mcs7830_set_reg(dev, HIF_REG_PAUSE_THRESHOLD,
307 1, &pause_threshold);
309 msleep(1);
313 static int mcs7830_init_dev(struct usbnet *dev)
315 int ret;
316 int retry;
318 /* Read MAC address from EEPROM */
319 ret = -EINVAL;
320 for (retry = 0; retry < 5 && ret; retry++)
321 ret = mcs7830_get_address(dev);
322 if (ret) {
323 dev_warn(&dev->udev->dev, "Cannot read MAC address\n");
324 goto out;
327 /* Set up PHY */
328 ret = mcs7830_set_autoneg(dev, 0);
329 if (ret) {
330 dev_info(&dev->udev->dev, "Cannot set autoneg\n");
331 goto out;
334 mcs7830_rev_C_fixup(dev);
335 ret = 0;
336 out:
337 return ret;
340 static int mcs7830_mdio_read(struct net_device *netdev, int phy_id,
341 int location)
343 struct usbnet *dev = netdev->priv;
344 return mcs7830_read_phy(dev, location);
347 static void mcs7830_mdio_write(struct net_device *netdev, int phy_id,
348 int location, int val)
350 struct usbnet *dev = netdev->priv;
351 mcs7830_write_phy(dev, location, val);
354 static int mcs7830_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
356 struct usbnet *dev = netdev_priv(net);
357 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
360 /* credits go to asix_set_multicast */
361 static void mcs7830_set_multicast(struct net_device *net)
363 struct usbnet *dev = netdev_priv(net);
364 struct mcs7830_data *data = (struct mcs7830_data *)&dev->data;
366 data->config = HIF_REG_CONFIG_TXENABLE;
368 /* this should not be needed, but it doesn't work otherwise */
369 data->config |= HIF_REG_CONFIG_ALLMULTICAST;
371 if (net->flags & IFF_PROMISC) {
372 data->config |= HIF_REG_CONFIG_PROMISCIOUS;
373 } else if (net->flags & IFF_ALLMULTI
374 || net->mc_count > MCS7830_MAX_MCAST) {
375 data->config |= HIF_REG_CONFIG_ALLMULTICAST;
376 } else if (net->mc_count == 0) {
377 /* just broadcast and directed */
378 } else {
379 /* We use the 20 byte dev->data
380 * for our 8 byte filter buffer
381 * to avoid allocating memory that
382 * is tricky to free later */
383 struct dev_mc_list *mc_list = net->mc_list;
384 u32 crc_bits;
385 int i;
387 memset(data->multi_filter, 0, sizeof data->multi_filter);
389 /* Build the multicast hash filter. */
390 for (i = 0; i < net->mc_count; i++) {
391 crc_bits = ether_crc(ETH_ALEN, mc_list->dmi_addr) >> 26;
392 data->multi_filter[crc_bits >> 3] |= 1 << (crc_bits & 7);
393 mc_list = mc_list->next;
396 mcs7830_set_reg_async(dev, HIF_REG_MULTICAST_HASH,
397 sizeof data->multi_filter,
398 data->multi_filter);
401 mcs7830_set_reg_async(dev, HIF_REG_CONFIG, 1, &data->config);
404 static int mcs7830_get_regs_len(struct net_device *net)
406 struct usbnet *dev = netdev_priv(net);
408 switch (mcs7830_get_rev(dev)) {
409 case 1:
410 return 21;
411 case 2:
412 return 32;
414 return 0;
417 static void mcs7830_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *drvinfo)
419 usbnet_get_drvinfo(net, drvinfo);
420 drvinfo->regdump_len = mcs7830_get_regs_len(net);
423 static void mcs7830_get_regs(struct net_device *net, struct ethtool_regs *regs, void *data)
425 struct usbnet *dev = netdev_priv(net);
427 regs->version = mcs7830_get_rev(dev);
428 mcs7830_get_reg(dev, 0, regs->len, data);
431 static struct ethtool_ops mcs7830_ethtool_ops = {
432 .get_drvinfo = mcs7830_get_drvinfo,
433 .get_regs_len = mcs7830_get_regs_len,
434 .get_regs = mcs7830_get_regs,
436 /* common usbnet calls */
437 .get_link = usbnet_get_link,
438 .get_msglevel = usbnet_get_msglevel,
439 .set_msglevel = usbnet_set_msglevel,
440 .get_settings = usbnet_get_settings,
441 .set_settings = usbnet_set_settings,
442 .nway_reset = usbnet_nway_reset,
445 static int mcs7830_bind(struct usbnet *dev, struct usb_interface *udev)
447 struct net_device *net = dev->net;
448 int ret;
450 ret = mcs7830_init_dev(dev);
451 if (ret)
452 goto out;
454 net->do_ioctl = mcs7830_ioctl;
455 net->ethtool_ops = &mcs7830_ethtool_ops;
456 net->set_multicast_list = mcs7830_set_multicast;
457 mcs7830_set_multicast(net);
459 /* reserve space for the status byte on rx */
460 dev->rx_urb_size = ETH_FRAME_LEN + 1;
462 dev->mii.mdio_read = mcs7830_mdio_read;
463 dev->mii.mdio_write = mcs7830_mdio_write;
464 dev->mii.dev = net;
465 dev->mii.phy_id_mask = 0x3f;
466 dev->mii.reg_num_mask = 0x1f;
467 dev->mii.phy_id = *((u8 *) net->dev_addr + 1);
469 ret = usbnet_get_endpoints(dev, udev);
470 out:
471 return ret;
474 /* The chip always appends a status bytes that we need to strip */
475 static int mcs7830_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
477 u8 status;
479 if (skb->len == 0) {
480 dev_err(&dev->udev->dev, "unexpected empty rx frame\n");
481 return 0;
484 skb_trim(skb, skb->len - 1);
485 status = skb->data[skb->len];
487 if (status != 0x20)
488 dev_dbg(&dev->udev->dev, "rx fixup status %x\n", status);
490 return skb->len > 0;
493 static const struct driver_info moschip_info = {
494 .description = "MOSCHIP 7830 usb-NET adapter",
495 .bind = mcs7830_bind,
496 .rx_fixup = mcs7830_rx_fixup,
497 .flags = FLAG_ETHER,
498 .in = 1,
499 .out = 2,
502 static const struct usb_device_id products[] = {
504 USB_DEVICE(MCS7830_VENDOR_ID, MCS7830_PRODUCT_ID),
505 .driver_info = (unsigned long) &moschip_info,
509 MODULE_DEVICE_TABLE(usb, products);
511 static struct usb_driver mcs7830_driver = {
512 .name = driver_name,
513 .id_table = products,
514 .probe = usbnet_probe,
515 .disconnect = usbnet_disconnect,
516 .suspend = usbnet_suspend,
517 .resume = usbnet_resume,
520 static int __init mcs7830_init(void)
522 return usb_register(&mcs7830_driver);
524 module_init(mcs7830_init);
526 static void __exit mcs7830_exit(void)
528 usb_deregister(&mcs7830_driver);
530 module_exit(mcs7830_exit);
532 MODULE_DESCRIPTION("USB to network adapter MCS7830)");
533 MODULE_LICENSE("GPL");