mcs7830.c 18 KB

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