mcs7830.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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, write to the Free Software
  40. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  41. */
  42. #include <linux/crc32.h>
  43. #include <linux/etherdevice.h>
  44. #include <linux/ethtool.h>
  45. #include <linux/init.h>
  46. #include <linux/mii.h>
  47. #include <linux/module.h>
  48. #include <linux/netdevice.h>
  49. #include <linux/slab.h>
  50. #include <linux/usb.h>
  51. #include <linux/usb/usbnet.h>
  52. /* requests */
  53. #define MCS7830_RD_BMREQ (USB_DIR_IN | USB_TYPE_VENDOR | \
  54. USB_RECIP_DEVICE)
  55. #define MCS7830_WR_BMREQ (USB_DIR_OUT | USB_TYPE_VENDOR | \
  56. USB_RECIP_DEVICE)
  57. #define MCS7830_RD_BREQ 0x0E
  58. #define MCS7830_WR_BREQ 0x0D
  59. #define MCS7830_CTRL_TIMEOUT 1000
  60. #define MCS7830_MAX_MCAST 64
  61. #define MCS7830_VENDOR_ID 0x9710
  62. #define MCS7832_PRODUCT_ID 0x7832
  63. #define MCS7830_PRODUCT_ID 0x7830
  64. #define MCS7730_PRODUCT_ID 0x7730
  65. #define SITECOM_VENDOR_ID 0x0DF6
  66. #define LN_030_PRODUCT_ID 0x0021
  67. #define MCS7830_MII_ADVERTISE (ADVERTISE_PAUSE_CAP | ADVERTISE_100FULL | \
  68. ADVERTISE_100HALF | ADVERTISE_10FULL | \
  69. ADVERTISE_10HALF | ADVERTISE_CSMA)
  70. /* HIF_REG_XX corresponding index value */
  71. enum {
  72. HIF_REG_MULTICAST_HASH = 0x00,
  73. HIF_REG_PACKET_GAP1 = 0x08,
  74. HIF_REG_PACKET_GAP2 = 0x09,
  75. HIF_REG_PHY_DATA = 0x0a,
  76. HIF_REG_PHY_CMD1 = 0x0c,
  77. HIF_REG_PHY_CMD1_READ = 0x40,
  78. HIF_REG_PHY_CMD1_WRITE = 0x20,
  79. HIF_REG_PHY_CMD1_PHYADDR = 0x01,
  80. HIF_REG_PHY_CMD2 = 0x0d,
  81. HIF_REG_PHY_CMD2_PEND_FLAG_BIT = 0x80,
  82. HIF_REG_PHY_CMD2_READY_FLAG_BIT = 0x40,
  83. HIF_REG_CONFIG = 0x0e,
  84. /* hmm, spec sez: "R/W", "Except bit 3" (likely TXENABLE). */
  85. HIF_REG_CONFIG_CFG = 0x80,
  86. HIF_REG_CONFIG_SPEED100 = 0x40,
  87. HIF_REG_CONFIG_FULLDUPLEX_ENABLE = 0x20,
  88. HIF_REG_CONFIG_RXENABLE = 0x10,
  89. HIF_REG_CONFIG_TXENABLE = 0x08,
  90. HIF_REG_CONFIG_SLEEPMODE = 0x04,
  91. HIF_REG_CONFIG_ALLMULTICAST = 0x02,
  92. HIF_REG_CONFIG_PROMISCUOUS = 0x01,
  93. HIF_REG_ETHERNET_ADDR = 0x0f,
  94. HIF_REG_FRAME_DROP_COUNTER = 0x15, /* 0..ff; reset: 0 */
  95. HIF_REG_PAUSE_THRESHOLD = 0x16,
  96. HIF_REG_PAUSE_THRESHOLD_DEFAULT = 0,
  97. };
  98. /* Trailing status byte in Ethernet Rx frame */
  99. enum {
  100. MCS7830_RX_SHORT_FRAME = 0x01, /* < 64 bytes */
  101. MCS7830_RX_LENGTH_ERROR = 0x02, /* framelen != Ethernet length field */
  102. MCS7830_RX_ALIGNMENT_ERROR = 0x04, /* non-even number of nibbles */
  103. MCS7830_RX_CRC_ERROR = 0x08,
  104. MCS7830_RX_LARGE_FRAME = 0x10, /* > 1518 bytes */
  105. MCS7830_RX_FRAME_CORRECT = 0x20, /* frame is correct */
  106. /* [7:6] reserved */
  107. };
  108. struct mcs7830_data {
  109. u8 multi_filter[8];
  110. u8 config;
  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. struct usb_device *xdev = dev->udev;
  116. int ret;
  117. void *buffer;
  118. buffer = kmalloc(size, GFP_NOIO);
  119. if (buffer == NULL)
  120. return -ENOMEM;
  121. ret = usb_control_msg(xdev, usb_rcvctrlpipe(xdev, 0), MCS7830_RD_BREQ,
  122. MCS7830_RD_BMREQ, 0x0000, index, buffer,
  123. size, MCS7830_CTRL_TIMEOUT);
  124. memcpy(data, buffer, size);
  125. kfree(buffer);
  126. return ret;
  127. }
  128. static int mcs7830_set_reg(struct usbnet *dev, u16 index, u16 size, const void *data)
  129. {
  130. struct usb_device *xdev = dev->udev;
  131. int ret;
  132. void *buffer;
  133. buffer = kmemdup(data, size, GFP_NOIO);
  134. if (buffer == NULL)
  135. return -ENOMEM;
  136. ret = usb_control_msg(xdev, usb_sndctrlpipe(xdev, 0), MCS7830_WR_BREQ,
  137. MCS7830_WR_BMREQ, 0x0000, index, buffer,
  138. size, MCS7830_CTRL_TIMEOUT);
  139. kfree(buffer);
  140. return ret;
  141. }
  142. static void mcs7830_async_cmd_callback(struct urb *urb)
  143. {
  144. struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
  145. int status = urb->status;
  146. if (status < 0)
  147. printk(KERN_DEBUG "%s() failed with %d\n",
  148. __func__, status);
  149. kfree(req);
  150. usb_free_urb(urb);
  151. }
  152. static void mcs7830_set_reg_async(struct usbnet *dev, u16 index, u16 size, void *data)
  153. {
  154. struct usb_ctrlrequest *req;
  155. int ret;
  156. struct urb *urb;
  157. urb = usb_alloc_urb(0, GFP_ATOMIC);
  158. if (!urb) {
  159. dev_dbg(&dev->udev->dev,
  160. "Error allocating URB in write_cmd_async!\n");
  161. return;
  162. }
  163. req = kmalloc(sizeof *req, GFP_ATOMIC);
  164. if (!req) {
  165. dev_err(&dev->udev->dev,
  166. "Failed to allocate memory for control request\n");
  167. goto out;
  168. }
  169. req->bRequestType = MCS7830_WR_BMREQ;
  170. req->bRequest = MCS7830_WR_BREQ;
  171. req->wValue = 0;
  172. req->wIndex = cpu_to_le16(index);
  173. req->wLength = cpu_to_le16(size);
  174. usb_fill_control_urb(urb, dev->udev,
  175. usb_sndctrlpipe(dev->udev, 0),
  176. (void *)req, data, size,
  177. mcs7830_async_cmd_callback, req);
  178. ret = usb_submit_urb(urb, GFP_ATOMIC);
  179. if (ret < 0) {
  180. dev_err(&dev->udev->dev,
  181. "Error submitting the control message: ret=%d\n", ret);
  182. goto out;
  183. }
  184. return;
  185. out:
  186. kfree(req);
  187. usb_free_urb(urb);
  188. }
  189. static int mcs7830_hif_get_mac_address(struct usbnet *dev, unsigned char *addr)
  190. {
  191. int ret = mcs7830_get_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN, addr);
  192. if (ret < 0)
  193. return ret;
  194. return 0;
  195. }
  196. static int mcs7830_hif_set_mac_address(struct usbnet *dev, unsigned char *addr)
  197. {
  198. int ret = mcs7830_set_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN, addr);
  199. if (ret < 0)
  200. return ret;
  201. return 0;
  202. }
  203. static int mcs7830_set_mac_address(struct net_device *netdev, void *p)
  204. {
  205. int ret;
  206. struct usbnet *dev = netdev_priv(netdev);
  207. struct sockaddr *addr = p;
  208. if (netif_running(netdev))
  209. return -EBUSY;
  210. if (!is_valid_ether_addr(addr->sa_data))
  211. return -EINVAL;
  212. ret = mcs7830_hif_set_mac_address(dev, addr->sa_data);
  213. if (ret < 0)
  214. return ret;
  215. /* it worked --> adopt it on netdev side */
  216. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  217. return 0;
  218. }
  219. static int mcs7830_read_phy(struct usbnet *dev, u8 index)
  220. {
  221. int ret;
  222. int i;
  223. __le16 val;
  224. u8 cmd[2] = {
  225. HIF_REG_PHY_CMD1_READ | HIF_REG_PHY_CMD1_PHYADDR,
  226. HIF_REG_PHY_CMD2_PEND_FLAG_BIT | index,
  227. };
  228. mutex_lock(&dev->phy_mutex);
  229. /* write the MII command */
  230. ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
  231. if (ret < 0)
  232. goto out;
  233. /* wait for the data to become valid, should be within < 1ms */
  234. for (i = 0; i < 10; i++) {
  235. ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
  236. if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT))
  237. break;
  238. ret = -EIO;
  239. msleep(1);
  240. }
  241. if (ret < 0)
  242. goto out;
  243. /* read actual register contents */
  244. ret = mcs7830_get_reg(dev, HIF_REG_PHY_DATA, 2, &val);
  245. if (ret < 0)
  246. goto out;
  247. ret = le16_to_cpu(val);
  248. dev_dbg(&dev->udev->dev, "read PHY reg %02x: %04x (%d tries)\n",
  249. index, val, i);
  250. out:
  251. mutex_unlock(&dev->phy_mutex);
  252. return ret;
  253. }
  254. static int mcs7830_write_phy(struct usbnet *dev, u8 index, u16 val)
  255. {
  256. int ret;
  257. int i;
  258. __le16 le_val;
  259. u8 cmd[2] = {
  260. HIF_REG_PHY_CMD1_WRITE | HIF_REG_PHY_CMD1_PHYADDR,
  261. HIF_REG_PHY_CMD2_PEND_FLAG_BIT | (index & 0x1F),
  262. };
  263. mutex_lock(&dev->phy_mutex);
  264. /* write the new register contents */
  265. le_val = cpu_to_le16(val);
  266. ret = mcs7830_set_reg(dev, HIF_REG_PHY_DATA, 2, &le_val);
  267. if (ret < 0)
  268. goto out;
  269. /* write the MII command */
  270. ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
  271. if (ret < 0)
  272. goto out;
  273. /* wait for the command to be accepted by the PHY */
  274. for (i = 0; i < 10; i++) {
  275. ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
  276. if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT))
  277. break;
  278. ret = -EIO;
  279. msleep(1);
  280. }
  281. if (ret < 0)
  282. goto out;
  283. ret = 0;
  284. dev_dbg(&dev->udev->dev, "write PHY reg %02x: %04x (%d tries)\n",
  285. index, val, i);
  286. out:
  287. mutex_unlock(&dev->phy_mutex);
  288. return ret;
  289. }
  290. /*
  291. * This algorithm comes from the original mcs7830 version 1.4 driver,
  292. * not sure if it is needed.
  293. */
  294. static int mcs7830_set_autoneg(struct usbnet *dev, int ptrUserPhyMode)
  295. {
  296. int ret;
  297. /* Enable all media types */
  298. ret = mcs7830_write_phy(dev, MII_ADVERTISE, MCS7830_MII_ADVERTISE);
  299. /* First reset BMCR */
  300. if (!ret)
  301. ret = mcs7830_write_phy(dev, MII_BMCR, 0x0000);
  302. /* Enable Auto Neg */
  303. if (!ret)
  304. ret = mcs7830_write_phy(dev, MII_BMCR, BMCR_ANENABLE);
  305. /* Restart Auto Neg (Keep the Enable Auto Neg Bit Set) */
  306. if (!ret)
  307. ret = mcs7830_write_phy(dev, MII_BMCR,
  308. BMCR_ANENABLE | BMCR_ANRESTART );
  309. return ret;
  310. }
  311. /*
  312. * if we can read register 22, the chip revision is C or higher
  313. */
  314. static int mcs7830_get_rev(struct usbnet *dev)
  315. {
  316. u8 dummy[2];
  317. int ret;
  318. ret = mcs7830_get_reg(dev, HIF_REG_FRAME_DROP_COUNTER, 2, dummy);
  319. if (ret > 0)
  320. return 2; /* Rev C or later */
  321. return 1; /* earlier revision */
  322. }
  323. /*
  324. * On rev. C we need to set the pause threshold
  325. */
  326. static void mcs7830_rev_C_fixup(struct usbnet *dev)
  327. {
  328. u8 pause_threshold = HIF_REG_PAUSE_THRESHOLD_DEFAULT;
  329. int retry;
  330. for (retry = 0; retry < 2; retry++) {
  331. if (mcs7830_get_rev(dev) == 2) {
  332. dev_info(&dev->udev->dev, "applying rev.C fixup\n");
  333. mcs7830_set_reg(dev, HIF_REG_PAUSE_THRESHOLD,
  334. 1, &pause_threshold);
  335. }
  336. msleep(1);
  337. }
  338. }
  339. static int mcs7830_mdio_read(struct net_device *netdev, int phy_id,
  340. int location)
  341. {
  342. struct usbnet *dev = netdev_priv(netdev);
  343. return mcs7830_read_phy(dev, location);
  344. }
  345. static void mcs7830_mdio_write(struct net_device *netdev, int phy_id,
  346. int location, int val)
  347. {
  348. struct usbnet *dev = netdev_priv(netdev);
  349. mcs7830_write_phy(dev, location, val);
  350. }
  351. static int mcs7830_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
  352. {
  353. struct usbnet *dev = netdev_priv(net);
  354. return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
  355. }
  356. static inline struct mcs7830_data *mcs7830_get_data(struct usbnet *dev)
  357. {
  358. return (struct mcs7830_data *)&dev->data;
  359. }
  360. static void mcs7830_hif_update_multicast_hash(struct usbnet *dev)
  361. {
  362. struct mcs7830_data *data = mcs7830_get_data(dev);
  363. mcs7830_set_reg_async(dev, HIF_REG_MULTICAST_HASH,
  364. sizeof data->multi_filter,
  365. data->multi_filter);
  366. }
  367. static void mcs7830_hif_update_config(struct usbnet *dev)
  368. {
  369. /* implementation specific to data->config
  370. (argument needs to be heap-based anyway - USB DMA!) */
  371. struct mcs7830_data *data = mcs7830_get_data(dev);
  372. mcs7830_set_reg_async(dev, HIF_REG_CONFIG, 1, &data->config);
  373. }
  374. static void mcs7830_data_set_multicast(struct net_device *net)
  375. {
  376. struct usbnet *dev = netdev_priv(net);
  377. struct mcs7830_data *data = mcs7830_get_data(dev);
  378. memset(data->multi_filter, 0, sizeof data->multi_filter);
  379. data->config = HIF_REG_CONFIG_TXENABLE;
  380. /* this should not be needed, but it doesn't work otherwise */
  381. data->config |= HIF_REG_CONFIG_ALLMULTICAST;
  382. if (net->flags & IFF_PROMISC) {
  383. data->config |= HIF_REG_CONFIG_PROMISCUOUS;
  384. } else if (net->flags & IFF_ALLMULTI ||
  385. netdev_mc_count(net) > MCS7830_MAX_MCAST) {
  386. data->config |= HIF_REG_CONFIG_ALLMULTICAST;
  387. } else if (netdev_mc_empty(net)) {
  388. /* just broadcast and directed */
  389. } else {
  390. /* We use the 20 byte dev->data
  391. * for our 8 byte filter buffer
  392. * to avoid allocating memory that
  393. * is tricky to free later */
  394. struct netdev_hw_addr *ha;
  395. u32 crc_bits;
  396. /* Build the multicast hash filter. */
  397. netdev_for_each_mc_addr(ha, net) {
  398. crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
  399. data->multi_filter[crc_bits >> 3] |= 1 << (crc_bits & 7);
  400. }
  401. }
  402. }
  403. static int mcs7830_apply_base_config(struct usbnet *dev)
  404. {
  405. int ret;
  406. /* re-configure known MAC (suspend case etc.) */
  407. ret = mcs7830_hif_set_mac_address(dev, dev->net->dev_addr);
  408. if (ret) {
  409. dev_info(&dev->udev->dev, "Cannot set MAC address\n");
  410. goto out;
  411. }
  412. /* Set up PHY */
  413. ret = mcs7830_set_autoneg(dev, 0);
  414. if (ret) {
  415. dev_info(&dev->udev->dev, "Cannot set autoneg\n");
  416. goto out;
  417. }
  418. mcs7830_hif_update_multicast_hash(dev);
  419. mcs7830_hif_update_config(dev);
  420. mcs7830_rev_C_fixup(dev);
  421. ret = 0;
  422. out:
  423. return ret;
  424. }
  425. /* credits go to asix_set_multicast */
  426. static void mcs7830_set_multicast(struct net_device *net)
  427. {
  428. struct usbnet *dev = netdev_priv(net);
  429. mcs7830_data_set_multicast(net);
  430. mcs7830_hif_update_multicast_hash(dev);
  431. mcs7830_hif_update_config(dev);
  432. }
  433. static int mcs7830_get_regs_len(struct net_device *net)
  434. {
  435. struct usbnet *dev = netdev_priv(net);
  436. switch (mcs7830_get_rev(dev)) {
  437. case 1:
  438. return 21;
  439. case 2:
  440. return 32;
  441. }
  442. return 0;
  443. }
  444. static void mcs7830_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *drvinfo)
  445. {
  446. usbnet_get_drvinfo(net, drvinfo);
  447. drvinfo->regdump_len = mcs7830_get_regs_len(net);
  448. }
  449. static void mcs7830_get_regs(struct net_device *net, struct ethtool_regs *regs, void *data)
  450. {
  451. struct usbnet *dev = netdev_priv(net);
  452. regs->version = mcs7830_get_rev(dev);
  453. mcs7830_get_reg(dev, 0, regs->len, data);
  454. }
  455. static const struct ethtool_ops mcs7830_ethtool_ops = {
  456. .get_drvinfo = mcs7830_get_drvinfo,
  457. .get_regs_len = mcs7830_get_regs_len,
  458. .get_regs = mcs7830_get_regs,
  459. /* common usbnet calls */
  460. .get_link = usbnet_get_link,
  461. .get_msglevel = usbnet_get_msglevel,
  462. .set_msglevel = usbnet_set_msglevel,
  463. .get_settings = usbnet_get_settings,
  464. .set_settings = usbnet_set_settings,
  465. .nway_reset = usbnet_nway_reset,
  466. };
  467. static const struct net_device_ops mcs7830_netdev_ops = {
  468. .ndo_open = usbnet_open,
  469. .ndo_stop = usbnet_stop,
  470. .ndo_start_xmit = usbnet_start_xmit,
  471. .ndo_tx_timeout = usbnet_tx_timeout,
  472. .ndo_change_mtu = usbnet_change_mtu,
  473. .ndo_validate_addr = eth_validate_addr,
  474. .ndo_do_ioctl = mcs7830_ioctl,
  475. .ndo_set_multicast_list = mcs7830_set_multicast,
  476. .ndo_set_mac_address = mcs7830_set_mac_address,
  477. };
  478. static int mcs7830_bind(struct usbnet *dev, struct usb_interface *udev)
  479. {
  480. struct net_device *net = dev->net;
  481. int ret;
  482. int retry;
  483. /* Initial startup: Gather MAC address setting from EEPROM */
  484. ret = -EINVAL;
  485. for (retry = 0; retry < 5 && ret; retry++)
  486. ret = mcs7830_hif_get_mac_address(dev, net->dev_addr);
  487. if (ret) {
  488. dev_warn(&dev->udev->dev, "Cannot read MAC address\n");
  489. goto out;
  490. }
  491. mcs7830_data_set_multicast(net);
  492. ret = mcs7830_apply_base_config(dev);
  493. if (ret)
  494. goto out;
  495. net->ethtool_ops = &mcs7830_ethtool_ops;
  496. net->netdev_ops = &mcs7830_netdev_ops;
  497. /* reserve space for the status byte on rx */
  498. dev->rx_urb_size = ETH_FRAME_LEN + 1;
  499. dev->mii.mdio_read = mcs7830_mdio_read;
  500. dev->mii.mdio_write = mcs7830_mdio_write;
  501. dev->mii.dev = net;
  502. dev->mii.phy_id_mask = 0x3f;
  503. dev->mii.reg_num_mask = 0x1f;
  504. dev->mii.phy_id = *((u8 *) net->dev_addr + 1);
  505. ret = usbnet_get_endpoints(dev, udev);
  506. out:
  507. return ret;
  508. }
  509. /* The chip always appends a status byte that we need to strip */
  510. static int mcs7830_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  511. {
  512. u8 status;
  513. if (skb->len == 0) {
  514. dev_err(&dev->udev->dev, "unexpected empty rx frame\n");
  515. return 0;
  516. }
  517. skb_trim(skb, skb->len - 1);
  518. status = skb->data[skb->len];
  519. if (status != MCS7830_RX_FRAME_CORRECT) {
  520. dev_dbg(&dev->udev->dev, "rx fixup status %x\n", status);
  521. /* hmm, perhaps usbnet.c already sees a globally visible
  522. frame error and increments rx_errors on its own already? */
  523. dev->net->stats.rx_errors++;
  524. if (status & (MCS7830_RX_SHORT_FRAME
  525. |MCS7830_RX_LENGTH_ERROR
  526. |MCS7830_RX_LARGE_FRAME))
  527. dev->net->stats.rx_length_errors++;
  528. if (status & MCS7830_RX_ALIGNMENT_ERROR)
  529. dev->net->stats.rx_frame_errors++;
  530. if (status & MCS7830_RX_CRC_ERROR)
  531. dev->net->stats.rx_crc_errors++;
  532. }
  533. return skb->len > 0;
  534. }
  535. static const struct driver_info moschip_info = {
  536. .description = "MOSCHIP 7830/7832/7730 usb-NET adapter",
  537. .bind = mcs7830_bind,
  538. .rx_fixup = mcs7830_rx_fixup,
  539. .flags = FLAG_ETHER,
  540. .in = 1,
  541. .out = 2,
  542. };
  543. static const struct driver_info sitecom_info = {
  544. .description = "Sitecom LN-30 usb-NET adapter",
  545. .bind = mcs7830_bind,
  546. .rx_fixup = mcs7830_rx_fixup,
  547. .flags = FLAG_ETHER,
  548. .in = 1,
  549. .out = 2,
  550. };
  551. static const struct usb_device_id products[] = {
  552. {
  553. USB_DEVICE(MCS7830_VENDOR_ID, MCS7832_PRODUCT_ID),
  554. .driver_info = (unsigned long) &moschip_info,
  555. },
  556. {
  557. USB_DEVICE(MCS7830_VENDOR_ID, MCS7830_PRODUCT_ID),
  558. .driver_info = (unsigned long) &moschip_info,
  559. },
  560. {
  561. USB_DEVICE(MCS7830_VENDOR_ID, MCS7730_PRODUCT_ID),
  562. .driver_info = (unsigned long) &moschip_info,
  563. },
  564. {
  565. USB_DEVICE(SITECOM_VENDOR_ID, LN_030_PRODUCT_ID),
  566. .driver_info = (unsigned long) &sitecom_info,
  567. },
  568. {},
  569. };
  570. MODULE_DEVICE_TABLE(usb, products);
  571. static int mcs7830_reset_resume (struct usb_interface *intf)
  572. {
  573. /* YES, this function is successful enough that ethtool -d
  574. does show same output pre-/post-suspend */
  575. struct usbnet *dev = usb_get_intfdata(intf);
  576. mcs7830_apply_base_config(dev);
  577. usbnet_resume(intf);
  578. return 0;
  579. }
  580. static struct usb_driver mcs7830_driver = {
  581. .name = driver_name,
  582. .id_table = products,
  583. .probe = usbnet_probe,
  584. .disconnect = usbnet_disconnect,
  585. .suspend = usbnet_suspend,
  586. .resume = usbnet_resume,
  587. .reset_resume = mcs7830_reset_resume,
  588. };
  589. static int __init mcs7830_init(void)
  590. {
  591. return usb_register(&mcs7830_driver);
  592. }
  593. module_init(mcs7830_init);
  594. static void __exit mcs7830_exit(void)
  595. {
  596. usb_deregister(&mcs7830_driver);
  597. }
  598. module_exit(mcs7830_exit);
  599. MODULE_DESCRIPTION("USB to network adapter MCS7830)");
  600. MODULE_LICENSE("GPL");