mcs7830.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*
  2. * MOSCHIP MCS7830 based (7730/7830/7832) USB 2.0 Ethernet Devices
  3. *
  4. * based on usbnet.c, asix.c and the vendor provided mcs7830 driver
  5. *
  6. * Copyright (C) 2010 Andreas Mohr <andi@lisas.de>
  7. * Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de>
  8. * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
  9. * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
  10. * Copyright (c) 2002-2003 TiVo Inc.
  11. *
  12. * Definitions gathered from MOSCHIP, Data Sheet_7830DA.pdf (thanks!).
  13. *
  14. * 2010-12-19: add 7832 USB PID ("functionality same as MCS7830"),
  15. * per active notification by manufacturer
  16. *
  17. * TODO:
  18. * - support HIF_REG_CONFIG_SLEEPMODE/HIF_REG_CONFIG_TXENABLE (via autopm?)
  19. * - implement ethtool_ops get_pauseparam/set_pauseparam
  20. * via HIF_REG_PAUSE_THRESHOLD (>= revision C only!)
  21. * - implement get_eeprom/[set_eeprom]
  22. * - switch PHY on/off on ifup/ifdown (perhaps in usbnet.c, via MII)
  23. * - mcs7830_get_regs() handling is weird: for rev 2 we return 32 regs,
  24. * can access only ~ 24, remaining user buffer is uninitialized garbage
  25. * - anything else?
  26. *
  27. *
  28. * This program is free software; you can redistribute it and/or modify
  29. * it under the terms of the GNU General Public License as published by
  30. * the Free Software Foundation; either version 2 of the License, or
  31. * (at your option) any later version.
  32. *
  33. * This program is distributed in the hope that it will be useful,
  34. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36. * GNU General Public License for more details.
  37. *
  38. * You should have received a copy of the GNU General Public License
  39. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  40. */
  41. #include <linux/crc32.h>
  42. #include <linux/etherdevice.h>
  43. #include <linux/ethtool.h>
  44. #include <linux/init.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. /* requests */
  52. #define MCS7830_RD_BMREQ (USB_DIR_IN | USB_TYPE_VENDOR | \
  53. USB_RECIP_DEVICE)
  54. #define MCS7830_WR_BMREQ (USB_DIR_OUT | USB_TYPE_VENDOR | \
  55. USB_RECIP_DEVICE)
  56. #define MCS7830_RD_BREQ 0x0E
  57. #define MCS7830_WR_BREQ 0x0D
  58. #define MCS7830_CTRL_TIMEOUT 1000
  59. #define MCS7830_MAX_MCAST 64
  60. #define MCS7830_VENDOR_ID 0x9710
  61. #define MCS7832_PRODUCT_ID 0x7832
  62. #define MCS7830_PRODUCT_ID 0x7830
  63. #define MCS7730_PRODUCT_ID 0x7730
  64. #define SITECOM_VENDOR_ID 0x0DF6
  65. #define LN_030_PRODUCT_ID 0x0021
  66. #define MCS7830_MII_ADVERTISE (ADVERTISE_PAUSE_CAP | ADVERTISE_100FULL | \
  67. ADVERTISE_100HALF | ADVERTISE_10FULL | \
  68. ADVERTISE_10HALF | ADVERTISE_CSMA)
  69. /* HIF_REG_XX corresponding index value */
  70. enum {
  71. HIF_REG_MULTICAST_HASH = 0x00,
  72. HIF_REG_PACKET_GAP1 = 0x08,
  73. HIF_REG_PACKET_GAP2 = 0x09,
  74. HIF_REG_PHY_DATA = 0x0a,
  75. HIF_REG_PHY_CMD1 = 0x0c,
  76. HIF_REG_PHY_CMD1_READ = 0x40,
  77. HIF_REG_PHY_CMD1_WRITE = 0x20,
  78. HIF_REG_PHY_CMD1_PHYADDR = 0x01,
  79. HIF_REG_PHY_CMD2 = 0x0d,
  80. HIF_REG_PHY_CMD2_PEND_FLAG_BIT = 0x80,
  81. HIF_REG_PHY_CMD2_READY_FLAG_BIT = 0x40,
  82. HIF_REG_CONFIG = 0x0e,
  83. /* hmm, spec sez: "R/W", "Except bit 3" (likely TXENABLE). */
  84. HIF_REG_CONFIG_CFG = 0x80,
  85. HIF_REG_CONFIG_SPEED100 = 0x40,
  86. HIF_REG_CONFIG_FULLDUPLEX_ENABLE = 0x20,
  87. HIF_REG_CONFIG_RXENABLE = 0x10,
  88. HIF_REG_CONFIG_TXENABLE = 0x08,
  89. HIF_REG_CONFIG_SLEEPMODE = 0x04,
  90. HIF_REG_CONFIG_ALLMULTICAST = 0x02,
  91. HIF_REG_CONFIG_PROMISCUOUS = 0x01,
  92. HIF_REG_ETHERNET_ADDR = 0x0f,
  93. HIF_REG_FRAME_DROP_COUNTER = 0x15, /* 0..ff; reset: 0 */
  94. HIF_REG_PAUSE_THRESHOLD = 0x16,
  95. HIF_REG_PAUSE_THRESHOLD_DEFAULT = 0,
  96. };
  97. /* Trailing status byte in Ethernet Rx frame */
  98. enum {
  99. MCS7830_RX_SHORT_FRAME = 0x01, /* < 64 bytes */
  100. MCS7830_RX_LENGTH_ERROR = 0x02, /* framelen != Ethernet length field */
  101. MCS7830_RX_ALIGNMENT_ERROR = 0x04, /* non-even number of nibbles */
  102. MCS7830_RX_CRC_ERROR = 0x08,
  103. MCS7830_RX_LARGE_FRAME = 0x10, /* > 1518 bytes */
  104. MCS7830_RX_FRAME_CORRECT = 0x20, /* frame is correct */
  105. /* [7:6] reserved */
  106. };
  107. struct mcs7830_data {
  108. u8 multi_filter[8];
  109. u8 config;
  110. u8 link_counter;
  111. };
  112. static const char driver_name[] = "MOSCHIP usb-ethernet driver";
  113. static int mcs7830_get_reg(struct usbnet *dev, u16 index, u16 size, void *data)
  114. {
  115. return usbnet_read_cmd(dev, MCS7830_RD_BREQ, MCS7830_RD_BMREQ,
  116. 0x0000, index, data, size);
  117. }
  118. static int mcs7830_set_reg(struct usbnet *dev, u16 index, u16 size, const void *data)
  119. {
  120. return usbnet_write_cmd(dev, MCS7830_WR_BREQ, MCS7830_WR_BMREQ,
  121. 0x0000, index, data, size);
  122. }
  123. static void mcs7830_set_reg_async(struct usbnet *dev, u16 index, u16 size, void *data)
  124. {
  125. usbnet_write_cmd_async(dev, MCS7830_WR_BREQ, MCS7830_WR_BMREQ,
  126. 0x0000, index, data, size);
  127. }
  128. static int mcs7830_hif_get_mac_address(struct usbnet *dev, unsigned char *addr)
  129. {
  130. int ret = mcs7830_get_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN, addr);
  131. if (ret < 0)
  132. return ret;
  133. return 0;
  134. }
  135. static int mcs7830_hif_set_mac_address(struct usbnet *dev, unsigned char *addr)
  136. {
  137. int ret = mcs7830_set_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN, addr);
  138. if (ret < 0)
  139. return ret;
  140. return 0;
  141. }
  142. static int mcs7830_set_mac_address(struct net_device *netdev, void *p)
  143. {
  144. int ret;
  145. struct usbnet *dev = netdev_priv(netdev);
  146. struct sockaddr *addr = p;
  147. if (netif_running(netdev))
  148. return -EBUSY;
  149. if (!is_valid_ether_addr(addr->sa_data))
  150. return -EADDRNOTAVAIL;
  151. ret = mcs7830_hif_set_mac_address(dev, addr->sa_data);
  152. if (ret < 0)
  153. return ret;
  154. /* it worked --> adopt it on netdev side */
  155. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  156. return 0;
  157. }
  158. static int mcs7830_read_phy(struct usbnet *dev, u8 index)
  159. {
  160. int ret;
  161. int i;
  162. __le16 val;
  163. u8 cmd[2] = {
  164. HIF_REG_PHY_CMD1_READ | HIF_REG_PHY_CMD1_PHYADDR,
  165. HIF_REG_PHY_CMD2_PEND_FLAG_BIT | index,
  166. };
  167. mutex_lock(&dev->phy_mutex);
  168. /* write the MII command */
  169. ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
  170. if (ret < 0)
  171. goto out;
  172. /* wait for the data to become valid, should be within < 1ms */
  173. for (i = 0; i < 10; i++) {
  174. ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
  175. if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT))
  176. break;
  177. ret = -EIO;
  178. msleep(1);
  179. }
  180. if (ret < 0)
  181. goto out;
  182. /* read actual register contents */
  183. ret = mcs7830_get_reg(dev, HIF_REG_PHY_DATA, 2, &val);
  184. if (ret < 0)
  185. goto out;
  186. ret = le16_to_cpu(val);
  187. dev_dbg(&dev->udev->dev, "read PHY reg %02x: %04x (%d tries)\n",
  188. index, val, i);
  189. out:
  190. mutex_unlock(&dev->phy_mutex);
  191. return ret;
  192. }
  193. static int mcs7830_write_phy(struct usbnet *dev, u8 index, u16 val)
  194. {
  195. int ret;
  196. int i;
  197. __le16 le_val;
  198. u8 cmd[2] = {
  199. HIF_REG_PHY_CMD1_WRITE | HIF_REG_PHY_CMD1_PHYADDR,
  200. HIF_REG_PHY_CMD2_PEND_FLAG_BIT | (index & 0x1F),
  201. };
  202. mutex_lock(&dev->phy_mutex);
  203. /* write the new register contents */
  204. le_val = cpu_to_le16(val);
  205. ret = mcs7830_set_reg(dev, HIF_REG_PHY_DATA, 2, &le_val);
  206. if (ret < 0)
  207. goto out;
  208. /* write the MII command */
  209. ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
  210. if (ret < 0)
  211. goto out;
  212. /* wait for the command to be accepted by the PHY */
  213. for (i = 0; i < 10; i++) {
  214. ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
  215. if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT))
  216. break;
  217. ret = -EIO;
  218. msleep(1);
  219. }
  220. if (ret < 0)
  221. goto out;
  222. ret = 0;
  223. dev_dbg(&dev->udev->dev, "write PHY reg %02x: %04x (%d tries)\n",
  224. index, val, i);
  225. out:
  226. mutex_unlock(&dev->phy_mutex);
  227. return ret;
  228. }
  229. /*
  230. * This algorithm comes from the original mcs7830 version 1.4 driver,
  231. * not sure if it is needed.
  232. */
  233. static int mcs7830_set_autoneg(struct usbnet *dev, int ptrUserPhyMode)
  234. {
  235. int ret;
  236. /* Enable all media types */
  237. ret = mcs7830_write_phy(dev, MII_ADVERTISE, MCS7830_MII_ADVERTISE);
  238. /* First reset BMCR */
  239. if (!ret)
  240. ret = mcs7830_write_phy(dev, MII_BMCR, 0x0000);
  241. /* Enable Auto Neg */
  242. if (!ret)
  243. ret = mcs7830_write_phy(dev, MII_BMCR, BMCR_ANENABLE);
  244. /* Restart Auto Neg (Keep the Enable Auto Neg Bit Set) */
  245. if (!ret)
  246. ret = mcs7830_write_phy(dev, MII_BMCR,
  247. BMCR_ANENABLE | BMCR_ANRESTART );
  248. return ret;
  249. }
  250. /*
  251. * if we can read register 22, the chip revision is C or higher
  252. */
  253. static int mcs7830_get_rev(struct usbnet *dev)
  254. {
  255. u8 dummy[2];
  256. int ret;
  257. ret = mcs7830_get_reg(dev, HIF_REG_FRAME_DROP_COUNTER, 2, dummy);
  258. if (ret > 0)
  259. return 2; /* Rev C or later */
  260. return 1; /* earlier revision */
  261. }
  262. /*
  263. * On rev. C we need to set the pause threshold
  264. */
  265. static void mcs7830_rev_C_fixup(struct usbnet *dev)
  266. {
  267. u8 pause_threshold = HIF_REG_PAUSE_THRESHOLD_DEFAULT;
  268. int retry;
  269. for (retry = 0; retry < 2; retry++) {
  270. if (mcs7830_get_rev(dev) == 2) {
  271. dev_info(&dev->udev->dev, "applying rev.C fixup\n");
  272. mcs7830_set_reg(dev, HIF_REG_PAUSE_THRESHOLD,
  273. 1, &pause_threshold);
  274. }
  275. msleep(1);
  276. }
  277. }
  278. static int mcs7830_mdio_read(struct net_device *netdev, int phy_id,
  279. int location)
  280. {
  281. struct usbnet *dev = netdev_priv(netdev);
  282. return mcs7830_read_phy(dev, location);
  283. }
  284. static void mcs7830_mdio_write(struct net_device *netdev, int phy_id,
  285. int location, int val)
  286. {
  287. struct usbnet *dev = netdev_priv(netdev);
  288. mcs7830_write_phy(dev, location, val);
  289. }
  290. static int mcs7830_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
  291. {
  292. struct usbnet *dev = netdev_priv(net);
  293. return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
  294. }
  295. static inline struct mcs7830_data *mcs7830_get_data(struct usbnet *dev)
  296. {
  297. return (struct mcs7830_data *)&dev->data;
  298. }
  299. static void mcs7830_hif_update_multicast_hash(struct usbnet *dev)
  300. {
  301. struct mcs7830_data *data = mcs7830_get_data(dev);
  302. mcs7830_set_reg_async(dev, HIF_REG_MULTICAST_HASH,
  303. sizeof data->multi_filter,
  304. data->multi_filter);
  305. }
  306. static void mcs7830_hif_update_config(struct usbnet *dev)
  307. {
  308. /* implementation specific to data->config
  309. (argument needs to be heap-based anyway - USB DMA!) */
  310. struct mcs7830_data *data = mcs7830_get_data(dev);
  311. mcs7830_set_reg_async(dev, HIF_REG_CONFIG, 1, &data->config);
  312. }
  313. static void mcs7830_data_set_multicast(struct net_device *net)
  314. {
  315. struct usbnet *dev = netdev_priv(net);
  316. struct mcs7830_data *data = mcs7830_get_data(dev);
  317. memset(data->multi_filter, 0, sizeof data->multi_filter);
  318. data->config = HIF_REG_CONFIG_TXENABLE;
  319. /* this should not be needed, but it doesn't work otherwise */
  320. data->config |= HIF_REG_CONFIG_ALLMULTICAST;
  321. if (net->flags & IFF_PROMISC) {
  322. data->config |= HIF_REG_CONFIG_PROMISCUOUS;
  323. } else if (net->flags & IFF_ALLMULTI ||
  324. netdev_mc_count(net) > MCS7830_MAX_MCAST) {
  325. data->config |= HIF_REG_CONFIG_ALLMULTICAST;
  326. } else if (netdev_mc_empty(net)) {
  327. /* just broadcast and directed */
  328. } else {
  329. /* We use the 20 byte dev->data
  330. * for our 8 byte filter buffer
  331. * to avoid allocating memory that
  332. * is tricky to free later */
  333. struct netdev_hw_addr *ha;
  334. u32 crc_bits;
  335. /* Build the multicast hash filter. */
  336. netdev_for_each_mc_addr(ha, net) {
  337. crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
  338. data->multi_filter[crc_bits >> 3] |= 1 << (crc_bits & 7);
  339. }
  340. }
  341. }
  342. static int mcs7830_apply_base_config(struct usbnet *dev)
  343. {
  344. int ret;
  345. /* re-configure known MAC (suspend case etc.) */
  346. ret = mcs7830_hif_set_mac_address(dev, dev->net->dev_addr);
  347. if (ret) {
  348. dev_info(&dev->udev->dev, "Cannot set MAC address\n");
  349. goto out;
  350. }
  351. /* Set up PHY */
  352. ret = mcs7830_set_autoneg(dev, 0);
  353. if (ret) {
  354. dev_info(&dev->udev->dev, "Cannot set autoneg\n");
  355. goto out;
  356. }
  357. mcs7830_hif_update_multicast_hash(dev);
  358. mcs7830_hif_update_config(dev);
  359. mcs7830_rev_C_fixup(dev);
  360. ret = 0;
  361. out:
  362. return ret;
  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. mcs7830_data_set_multicast(net);
  369. mcs7830_hif_update_multicast_hash(dev);
  370. mcs7830_hif_update_config(dev);
  371. }
  372. static int mcs7830_get_regs_len(struct net_device *net)
  373. {
  374. struct usbnet *dev = netdev_priv(net);
  375. switch (mcs7830_get_rev(dev)) {
  376. case 1:
  377. return 21;
  378. case 2:
  379. return 32;
  380. }
  381. return 0;
  382. }
  383. static void mcs7830_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *drvinfo)
  384. {
  385. usbnet_get_drvinfo(net, drvinfo);
  386. drvinfo->regdump_len = mcs7830_get_regs_len(net);
  387. }
  388. static void mcs7830_get_regs(struct net_device *net, struct ethtool_regs *regs, void *data)
  389. {
  390. struct usbnet *dev = netdev_priv(net);
  391. regs->version = mcs7830_get_rev(dev);
  392. mcs7830_get_reg(dev, 0, regs->len, data);
  393. }
  394. static const struct ethtool_ops mcs7830_ethtool_ops = {
  395. .get_drvinfo = mcs7830_get_drvinfo,
  396. .get_regs_len = mcs7830_get_regs_len,
  397. .get_regs = mcs7830_get_regs,
  398. /* common usbnet calls */
  399. .get_link = usbnet_get_link,
  400. .get_msglevel = usbnet_get_msglevel,
  401. .set_msglevel = usbnet_set_msglevel,
  402. .get_settings = usbnet_get_settings,
  403. .set_settings = usbnet_set_settings,
  404. .nway_reset = usbnet_nway_reset,
  405. };
  406. static const struct net_device_ops mcs7830_netdev_ops = {
  407. .ndo_open = usbnet_open,
  408. .ndo_stop = usbnet_stop,
  409. .ndo_start_xmit = usbnet_start_xmit,
  410. .ndo_tx_timeout = usbnet_tx_timeout,
  411. .ndo_change_mtu = usbnet_change_mtu,
  412. .ndo_validate_addr = eth_validate_addr,
  413. .ndo_do_ioctl = mcs7830_ioctl,
  414. .ndo_set_rx_mode = mcs7830_set_multicast,
  415. .ndo_set_mac_address = mcs7830_set_mac_address,
  416. };
  417. static int mcs7830_bind(struct usbnet *dev, struct usb_interface *udev)
  418. {
  419. struct net_device *net = dev->net;
  420. int ret;
  421. int retry;
  422. /* Initial startup: Gather MAC address setting from EEPROM */
  423. ret = -EINVAL;
  424. for (retry = 0; retry < 5 && ret; retry++)
  425. ret = mcs7830_hif_get_mac_address(dev, net->dev_addr);
  426. if (ret) {
  427. dev_warn(&dev->udev->dev, "Cannot read MAC address\n");
  428. goto out;
  429. }
  430. mcs7830_data_set_multicast(net);
  431. ret = mcs7830_apply_base_config(dev);
  432. if (ret)
  433. goto out;
  434. net->ethtool_ops = &mcs7830_ethtool_ops;
  435. net->netdev_ops = &mcs7830_netdev_ops;
  436. /* reserve space for the status byte on rx */
  437. dev->rx_urb_size = ETH_FRAME_LEN + 1;
  438. dev->mii.mdio_read = mcs7830_mdio_read;
  439. dev->mii.mdio_write = mcs7830_mdio_write;
  440. dev->mii.dev = net;
  441. dev->mii.phy_id_mask = 0x3f;
  442. dev->mii.reg_num_mask = 0x1f;
  443. dev->mii.phy_id = *((u8 *) net->dev_addr + 1);
  444. ret = usbnet_get_endpoints(dev, udev);
  445. out:
  446. return ret;
  447. }
  448. /* The chip always appends a status byte that we need to strip */
  449. static int mcs7830_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  450. {
  451. u8 status;
  452. if (skb->len == 0) {
  453. dev_err(&dev->udev->dev, "unexpected empty rx frame\n");
  454. return 0;
  455. }
  456. skb_trim(skb, skb->len - 1);
  457. status = skb->data[skb->len];
  458. if (status != MCS7830_RX_FRAME_CORRECT) {
  459. dev_dbg(&dev->udev->dev, "rx fixup status %x\n", status);
  460. /* hmm, perhaps usbnet.c already sees a globally visible
  461. frame error and increments rx_errors on its own already? */
  462. dev->net->stats.rx_errors++;
  463. if (status & (MCS7830_RX_SHORT_FRAME
  464. |MCS7830_RX_LENGTH_ERROR
  465. |MCS7830_RX_LARGE_FRAME))
  466. dev->net->stats.rx_length_errors++;
  467. if (status & MCS7830_RX_ALIGNMENT_ERROR)
  468. dev->net->stats.rx_frame_errors++;
  469. if (status & MCS7830_RX_CRC_ERROR)
  470. dev->net->stats.rx_crc_errors++;
  471. }
  472. return skb->len > 0;
  473. }
  474. static void mcs7830_status(struct usbnet *dev, struct urb *urb)
  475. {
  476. u8 *buf = urb->transfer_buffer;
  477. bool link, link_changed;
  478. struct mcs7830_data *data = mcs7830_get_data(dev);
  479. if (urb->actual_length < 16)
  480. return;
  481. link = !(buf[1] & 0x20);
  482. link_changed = netif_carrier_ok(dev->net) != link;
  483. if (link_changed) {
  484. data->link_counter++;
  485. /*
  486. track link state 20 times to guard against erroneous
  487. link state changes reported sometimes by the chip
  488. */
  489. if (data->link_counter > 20) {
  490. data->link_counter = 0;
  491. usbnet_link_change(dev, link, 0);
  492. netdev_dbg(dev->net, "Link Status is: %d\n", link);
  493. }
  494. } else
  495. data->link_counter = 0;
  496. }
  497. static const struct driver_info moschip_info = {
  498. .description = "MOSCHIP 7830/7832/7730 usb-NET adapter",
  499. .bind = mcs7830_bind,
  500. .rx_fixup = mcs7830_rx_fixup,
  501. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  502. .status = mcs7830_status,
  503. .in = 1,
  504. .out = 2,
  505. };
  506. static const struct driver_info sitecom_info = {
  507. .description = "Sitecom LN-30 usb-NET adapter",
  508. .bind = mcs7830_bind,
  509. .rx_fixup = mcs7830_rx_fixup,
  510. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  511. .status = mcs7830_status,
  512. .in = 1,
  513. .out = 2,
  514. };
  515. static const struct usb_device_id products[] = {
  516. {
  517. USB_DEVICE(MCS7830_VENDOR_ID, MCS7832_PRODUCT_ID),
  518. .driver_info = (unsigned long) &moschip_info,
  519. },
  520. {
  521. USB_DEVICE(MCS7830_VENDOR_ID, MCS7830_PRODUCT_ID),
  522. .driver_info = (unsigned long) &moschip_info,
  523. },
  524. {
  525. USB_DEVICE(MCS7830_VENDOR_ID, MCS7730_PRODUCT_ID),
  526. .driver_info = (unsigned long) &moschip_info,
  527. },
  528. {
  529. USB_DEVICE(SITECOM_VENDOR_ID, LN_030_PRODUCT_ID),
  530. .driver_info = (unsigned long) &sitecom_info,
  531. },
  532. {},
  533. };
  534. MODULE_DEVICE_TABLE(usb, products);
  535. static int mcs7830_reset_resume (struct usb_interface *intf)
  536. {
  537. /* YES, this function is successful enough that ethtool -d
  538. does show same output pre-/post-suspend */
  539. struct usbnet *dev = usb_get_intfdata(intf);
  540. mcs7830_apply_base_config(dev);
  541. usbnet_resume(intf);
  542. return 0;
  543. }
  544. static struct usb_driver mcs7830_driver = {
  545. .name = driver_name,
  546. .id_table = products,
  547. .probe = usbnet_probe,
  548. .disconnect = usbnet_disconnect,
  549. .suspend = usbnet_suspend,
  550. .resume = usbnet_resume,
  551. .reset_resume = mcs7830_reset_resume,
  552. .disable_hub_initiated_lpm = 1,
  553. };
  554. module_usb_driver(mcs7830_driver);
  555. MODULE_DESCRIPTION("USB to network adapter MCS7830)");
  556. MODULE_LICENSE("GPL");