mcs7830.c 18 KB

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