mcs7830.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. #include <linux/crc32.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/ethtool.h>
  28. #include <linux/init.h>
  29. #include <linux/mii.h>
  30. #include <linux/module.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/usb.h>
  33. #include "usbnet.h"
  34. /* requests */
  35. #define MCS7830_RD_BMREQ (USB_DIR_IN | USB_TYPE_VENDOR | \
  36. USB_RECIP_DEVICE)
  37. #define MCS7830_WR_BMREQ (USB_DIR_OUT | USB_TYPE_VENDOR | \
  38. USB_RECIP_DEVICE)
  39. #define MCS7830_RD_BREQ 0x0E
  40. #define MCS7830_WR_BREQ 0x0D
  41. #define MCS7830_CTRL_TIMEOUT 1000
  42. #define MCS7830_MAX_MCAST 64
  43. #define MCS7830_VENDOR_ID 0x9710
  44. #define MCS7830_PRODUCT_ID 0x7830
  45. #define MCS7830_MII_ADVERTISE (ADVERTISE_PAUSE_CAP | ADVERTISE_100FULL | \
  46. ADVERTISE_100HALF | ADVERTISE_10FULL | \
  47. ADVERTISE_10HALF | ADVERTISE_CSMA)
  48. /* HIF_REG_XX coressponding index value */
  49. enum {
  50. HIF_REG_MULTICAST_HASH = 0x00,
  51. HIF_REG_PACKET_GAP1 = 0x08,
  52. HIF_REG_PACKET_GAP2 = 0x09,
  53. HIF_REG_PHY_DATA = 0x0a,
  54. HIF_REG_PHY_CMD1 = 0x0c,
  55. HIF_REG_PHY_CMD1_READ = 0x40,
  56. HIF_REG_PHY_CMD1_WRITE = 0x20,
  57. HIF_REG_PHY_CMD1_PHYADDR = 0x01,
  58. HIF_REG_PHY_CMD2 = 0x0d,
  59. HIF_REG_PHY_CMD2_PEND_FLAG_BIT = 0x80,
  60. HIF_REG_PHY_CMD2_READY_FLAG_BIT = 0x40,
  61. HIF_REG_CONFIG = 0x0e,
  62. HIF_REG_CONFIG_CFG = 0x80,
  63. HIF_REG_CONFIG_SPEED100 = 0x40,
  64. HIF_REG_CONFIG_FULLDUPLEX_ENABLE = 0x20,
  65. HIF_REG_CONFIG_RXENABLE = 0x10,
  66. HIF_REG_CONFIG_TXENABLE = 0x08,
  67. HIF_REG_CONFIG_SLEEPMODE = 0x04,
  68. HIF_REG_CONFIG_ALLMULTICAST = 0x02,
  69. HIF_REG_CONFIG_PROMISCIOUS = 0x01,
  70. HIF_REG_ETHERNET_ADDR = 0x0f,
  71. HIF_REG_22 = 0x15,
  72. HIF_REG_PAUSE_THRESHOLD = 0x16,
  73. HIF_REG_PAUSE_THRESHOLD_DEFAULT = 0,
  74. };
  75. struct mcs7830_data {
  76. u8 multi_filter[8];
  77. u8 config;
  78. };
  79. static const char driver_name[] = "MOSCHIP usb-ethernet driver";
  80. static int mcs7830_get_reg(struct usbnet *dev, u16 index, u16 size, void *data)
  81. {
  82. struct usb_device *xdev = dev->udev;
  83. int ret;
  84. ret = usb_control_msg(xdev, usb_rcvctrlpipe(xdev, 0), MCS7830_RD_BREQ,
  85. MCS7830_RD_BMREQ, 0x0000, index, data,
  86. size, msecs_to_jiffies(MCS7830_CTRL_TIMEOUT));
  87. return ret;
  88. }
  89. static int mcs7830_set_reg(struct usbnet *dev, u16 index, u16 size, void *data)
  90. {
  91. struct usb_device *xdev = dev->udev;
  92. int ret;
  93. ret = usb_control_msg(xdev, usb_sndctrlpipe(xdev, 0), MCS7830_WR_BREQ,
  94. MCS7830_WR_BMREQ, 0x0000, index, data,
  95. size, msecs_to_jiffies(MCS7830_CTRL_TIMEOUT));
  96. return ret;
  97. }
  98. static void mcs7830_async_cmd_callback(struct urb *urb)
  99. {
  100. struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
  101. if (urb->status < 0)
  102. printk(KERN_DEBUG "mcs7830_async_cmd_callback() failed with %d",
  103. urb->status);
  104. kfree(req);
  105. usb_free_urb(urb);
  106. }
  107. static void mcs7830_set_reg_async(struct usbnet *dev, u16 index, u16 size, void *data)
  108. {
  109. struct usb_ctrlrequest *req;
  110. int ret;
  111. struct urb *urb;
  112. urb = usb_alloc_urb(0, GFP_ATOMIC);
  113. if (!urb) {
  114. dev_dbg(&dev->udev->dev, "Error allocating URB "
  115. "in write_cmd_async!");
  116. return;
  117. }
  118. req = kmalloc(sizeof *req, GFP_ATOMIC);
  119. if (!req) {
  120. dev_err(&dev->udev->dev, "Failed to allocate memory for "
  121. "control request");
  122. goto out;
  123. }
  124. req->bRequestType = MCS7830_WR_BMREQ;
  125. req->bRequest = MCS7830_WR_BREQ;
  126. req->wValue = 0;
  127. req->wIndex = cpu_to_le16(index);
  128. req->wLength = cpu_to_le16(size);
  129. usb_fill_control_urb(urb, dev->udev,
  130. usb_sndctrlpipe(dev->udev, 0),
  131. (void *)req, data, size,
  132. mcs7830_async_cmd_callback, req);
  133. ret = usb_submit_urb(urb, GFP_ATOMIC);
  134. if (ret < 0) {
  135. dev_err(&dev->udev->dev, "Error submitting the control "
  136. "message: ret=%d", ret);
  137. goto out;
  138. }
  139. return;
  140. out:
  141. kfree(req);
  142. usb_free_urb(urb);
  143. }
  144. static int mcs7830_get_address(struct usbnet *dev)
  145. {
  146. int ret;
  147. ret = mcs7830_get_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN,
  148. dev->net->dev_addr);
  149. if (ret < 0)
  150. return ret;
  151. return 0;
  152. }
  153. static int mcs7830_read_phy(struct usbnet *dev, u8 index)
  154. {
  155. int ret;
  156. int i;
  157. __le16 val;
  158. u8 cmd[2] = {
  159. HIF_REG_PHY_CMD1_READ | HIF_REG_PHY_CMD1_PHYADDR,
  160. HIF_REG_PHY_CMD2_PEND_FLAG_BIT | index,
  161. };
  162. /* write the MII command */
  163. ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
  164. if (ret < 0)
  165. goto out;
  166. /* wait for the data to become valid, should be within < 1ms */
  167. for (i = 0; i < 10; i++) {
  168. ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
  169. if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT))
  170. break;
  171. ret = -EIO;
  172. msleep(1);
  173. }
  174. if (ret < 0)
  175. goto out;
  176. /* read actual register contents */
  177. ret = mcs7830_get_reg(dev, HIF_REG_PHY_DATA, 2, &val);
  178. if (ret < 0)
  179. goto out;
  180. ret = le16_to_cpu(val);
  181. dev_dbg(&dev->udev->dev, "read PHY reg %02x: %04x (%d tries)\n",
  182. index, val, i);
  183. out:
  184. return ret;
  185. }
  186. static int mcs7830_write_phy(struct usbnet *dev, u8 index, u16 val)
  187. {
  188. int ret;
  189. int i;
  190. __le16 le_val;
  191. u8 cmd[2] = {
  192. HIF_REG_PHY_CMD1_WRITE | HIF_REG_PHY_CMD1_PHYADDR,
  193. HIF_REG_PHY_CMD2_PEND_FLAG_BIT | (index & 0x1F),
  194. };
  195. /* write the new register contents */
  196. le_val = cpu_to_le16(val);
  197. ret = mcs7830_set_reg(dev, HIF_REG_PHY_DATA, 2, &le_val);
  198. if (ret < 0)
  199. goto out;
  200. /* write the MII command */
  201. ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
  202. if (ret < 0)
  203. goto out;
  204. /* wait for the command to be accepted by the PHY */
  205. for (i = 0; i < 10; i++) {
  206. ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
  207. if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT))
  208. break;
  209. ret = -EIO;
  210. msleep(1);
  211. }
  212. if (ret < 0)
  213. goto out;
  214. ret = 0;
  215. dev_dbg(&dev->udev->dev, "write PHY reg %02x: %04x (%d tries)\n",
  216. index, val, i);
  217. out:
  218. return ret;
  219. }
  220. /*
  221. * This algorithm comes from the original mcs7830 version 1.4 driver,
  222. * not sure if it is needed.
  223. */
  224. static int mcs7830_set_autoneg(struct usbnet *dev, int ptrUserPhyMode)
  225. {
  226. int ret;
  227. /* Enable all media types */
  228. ret = mcs7830_write_phy(dev, MII_ADVERTISE, MCS7830_MII_ADVERTISE);
  229. /* First reset BMCR */
  230. if (!ret)
  231. ret = mcs7830_write_phy(dev, MII_BMCR, 0x0000);
  232. /* Enable Auto Neg */
  233. if (!ret)
  234. ret = mcs7830_write_phy(dev, MII_BMCR, BMCR_ANENABLE);
  235. /* Restart Auto Neg (Keep the Enable Auto Neg Bit Set) */
  236. if (!ret)
  237. ret = mcs7830_write_phy(dev, MII_BMCR,
  238. BMCR_ANENABLE | BMCR_ANRESTART );
  239. return ret < 0 ? : 0;
  240. }
  241. /*
  242. * if we can read register 22, the chip revision is C or higher
  243. */
  244. static int mcs7830_get_rev(struct usbnet *dev)
  245. {
  246. u8 dummy[2];
  247. int ret;
  248. ret = mcs7830_get_reg(dev, HIF_REG_22, 2, dummy);
  249. if (ret > 0)
  250. return 2; /* Rev C or later */
  251. return 1; /* earlier revision */
  252. }
  253. /*
  254. * On rev. C we need to set the pause threshold
  255. */
  256. static void mcs7830_rev_C_fixup(struct usbnet *dev)
  257. {
  258. u8 pause_threshold = HIF_REG_PAUSE_THRESHOLD_DEFAULT;
  259. int retry;
  260. for (retry = 0; retry < 2; retry++) {
  261. if (mcs7830_get_rev(dev) == 2) {
  262. dev_info(&dev->udev->dev, "applying rev.C fixup\n");
  263. mcs7830_set_reg(dev, HIF_REG_PAUSE_THRESHOLD,
  264. 1, &pause_threshold);
  265. }
  266. msleep(1);
  267. }
  268. }
  269. static int mcs7830_init_dev(struct usbnet *dev)
  270. {
  271. int ret;
  272. int retry;
  273. /* Read MAC address from EEPROM */
  274. ret = -EINVAL;
  275. for (retry = 0; retry < 5 && ret; retry++)
  276. ret = mcs7830_get_address(dev);
  277. if (ret) {
  278. dev_warn(&dev->udev->dev, "Cannot read MAC address\n");
  279. goto out;
  280. }
  281. /* Set up PHY */
  282. ret = mcs7830_set_autoneg(dev, 0);
  283. if (ret) {
  284. dev_info(&dev->udev->dev, "Cannot set autoneg\n");
  285. goto out;
  286. }
  287. mcs7830_rev_C_fixup(dev);
  288. ret = 0;
  289. out:
  290. return ret;
  291. }
  292. static int mcs7830_mdio_read(struct net_device *netdev, int phy_id,
  293. int location)
  294. {
  295. struct usbnet *dev = netdev->priv;
  296. return mcs7830_read_phy(dev, location);
  297. }
  298. static void mcs7830_mdio_write(struct net_device *netdev, int phy_id,
  299. int location, int val)
  300. {
  301. struct usbnet *dev = netdev->priv;
  302. mcs7830_write_phy(dev, location, val);
  303. }
  304. static int mcs7830_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
  305. {
  306. struct usbnet *dev = netdev_priv(net);
  307. return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
  308. }
  309. /* credits go to asix_set_multicast */
  310. static void mcs7830_set_multicast(struct net_device *net)
  311. {
  312. struct usbnet *dev = netdev_priv(net);
  313. struct mcs7830_data *data = (struct mcs7830_data *)&dev->data;
  314. data->config = HIF_REG_CONFIG_TXENABLE;
  315. /* this should not be needed, but it doesn't work otherwise */
  316. data->config |= HIF_REG_CONFIG_ALLMULTICAST;
  317. if (net->flags & IFF_PROMISC) {
  318. data->config |= HIF_REG_CONFIG_PROMISCIOUS;
  319. } else if (net->flags & IFF_ALLMULTI
  320. || net->mc_count > MCS7830_MAX_MCAST) {
  321. data->config |= HIF_REG_CONFIG_ALLMULTICAST;
  322. } else if (net->mc_count == 0) {
  323. /* just broadcast and directed */
  324. } else {
  325. /* We use the 20 byte dev->data
  326. * for our 8 byte filter buffer
  327. * to avoid allocating memory that
  328. * is tricky to free later */
  329. struct dev_mc_list *mc_list = net->mc_list;
  330. u32 crc_bits;
  331. int i;
  332. memset(data->multi_filter, 0, sizeof data->multi_filter);
  333. /* Build the multicast hash filter. */
  334. for (i = 0; i < net->mc_count; i++) {
  335. crc_bits = ether_crc(ETH_ALEN, mc_list->dmi_addr) >> 26;
  336. data->multi_filter[crc_bits >> 3] |= 1 << (crc_bits & 7);
  337. mc_list = mc_list->next;
  338. }
  339. mcs7830_set_reg_async(dev, HIF_REG_MULTICAST_HASH,
  340. sizeof data->multi_filter,
  341. data->multi_filter);
  342. }
  343. mcs7830_set_reg_async(dev, HIF_REG_CONFIG, 1, &data->config);
  344. }
  345. static int mcs7830_get_regs_len(struct net_device *net)
  346. {
  347. struct usbnet *dev = netdev_priv(net);
  348. switch (mcs7830_get_rev(dev)) {
  349. case 1:
  350. return 21;
  351. case 2:
  352. return 32;
  353. }
  354. return 0;
  355. }
  356. static void mcs7830_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *drvinfo)
  357. {
  358. usbnet_get_drvinfo(net, drvinfo);
  359. drvinfo->regdump_len = mcs7830_get_regs_len(net);
  360. }
  361. static void mcs7830_get_regs(struct net_device *net, struct ethtool_regs *regs, void *data)
  362. {
  363. struct usbnet *dev = netdev_priv(net);
  364. regs->version = mcs7830_get_rev(dev);
  365. mcs7830_get_reg(dev, 0, regs->len, data);
  366. }
  367. static struct ethtool_ops mcs7830_ethtool_ops = {
  368. .get_drvinfo = mcs7830_get_drvinfo,
  369. .get_regs_len = mcs7830_get_regs_len,
  370. .get_regs = mcs7830_get_regs,
  371. /* common usbnet calls */
  372. .get_msglevel = usbnet_get_msglevel,
  373. .set_msglevel = usbnet_set_msglevel,
  374. };
  375. static int mcs7830_bind(struct usbnet *dev, struct usb_interface *udev)
  376. {
  377. struct net_device *net = dev->net;
  378. int ret;
  379. ret = mcs7830_init_dev(dev);
  380. if (ret)
  381. goto out;
  382. net->do_ioctl = mcs7830_ioctl;
  383. net->ethtool_ops = &mcs7830_ethtool_ops;
  384. net->set_multicast_list = mcs7830_set_multicast;
  385. mcs7830_set_multicast(net);
  386. /* reserve space for the status byte on rx */
  387. dev->rx_urb_size = ETH_FRAME_LEN + 1;
  388. dev->mii.mdio_read = mcs7830_mdio_read;
  389. dev->mii.mdio_write = mcs7830_mdio_write;
  390. dev->mii.dev = net;
  391. dev->mii.phy_id_mask = 0x3f;
  392. dev->mii.reg_num_mask = 0x1f;
  393. dev->mii.phy_id = *((u8 *) net->dev_addr + 1);
  394. ret = usbnet_get_endpoints(dev, udev);
  395. out:
  396. return ret;
  397. }
  398. /* The chip always appends a status bytes that we need to strip */
  399. static int mcs7830_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  400. {
  401. u8 status;
  402. if (skb->len == 0) {
  403. dev_err(&dev->udev->dev, "unexpected empty rx frame\n");
  404. return 0;
  405. }
  406. skb_trim(skb, skb->len - 1);
  407. status = skb->data[skb->len];
  408. if (status != 0x20)
  409. dev_dbg(&dev->udev->dev, "rx fixup status %x\n", status);
  410. return skb->len > 0;
  411. }
  412. static const struct driver_info moschip_info = {
  413. .description = "MOSCHIP 7830 usb-NET adapter",
  414. .bind = mcs7830_bind,
  415. .rx_fixup = mcs7830_rx_fixup,
  416. .flags = FLAG_ETHER,
  417. .in = 1,
  418. .out = 2,
  419. };
  420. static const struct usb_device_id products[] = {
  421. {
  422. USB_DEVICE(MCS7830_VENDOR_ID, MCS7830_PRODUCT_ID),
  423. .driver_info = (unsigned long) &moschip_info,
  424. },
  425. {},
  426. };
  427. MODULE_DEVICE_TABLE(usb, products);
  428. static struct usb_driver mcs7830_driver = {
  429. .name = driver_name,
  430. .id_table = products,
  431. .probe = usbnet_probe,
  432. .disconnect = usbnet_disconnect,
  433. .suspend = usbnet_suspend,
  434. .resume = usbnet_resume,
  435. };
  436. static int __init mcs7830_init(void)
  437. {
  438. return usb_register(&mcs7830_driver);
  439. }
  440. module_init(mcs7830_init);
  441. static void __exit mcs7830_exit(void)
  442. {
  443. usb_deregister(&mcs7830_driver);
  444. }
  445. module_exit(mcs7830_exit);
  446. MODULE_DESCRIPTION("USB to network adapter MCS7830)");
  447. MODULE_LICENSE("GPL");