dm9601.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*
  2. * Davicom DM9601 USB 1.1 10/100Mbps ethernet devices
  3. *
  4. * Peter Korsgaard <jacmet@sunsite.dk>
  5. *
  6. * This file is licensed under the terms of the GNU General Public License
  7. * version 2. This program is licensed "as is" without any warranty of any
  8. * kind, whether express or implied.
  9. */
  10. //#define DEBUG
  11. #include <linux/module.h>
  12. #include <linux/sched.h>
  13. #include <linux/stddef.h>
  14. #include <linux/init.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/ethtool.h>
  18. #include <linux/mii.h>
  19. #include <linux/usb.h>
  20. #include <linux/crc32.h>
  21. #include <linux/usb/usbnet.h>
  22. #include <linux/slab.h>
  23. /* datasheet:
  24. http://ptm2.cc.utu.fi/ftp/network/cards/DM9601/From_NET/DM9601-DS-P01-930914.pdf
  25. */
  26. /* control requests */
  27. #define DM_READ_REGS 0x00
  28. #define DM_WRITE_REGS 0x01
  29. #define DM_READ_MEMS 0x02
  30. #define DM_WRITE_REG 0x03
  31. #define DM_WRITE_MEMS 0x05
  32. #define DM_WRITE_MEM 0x07
  33. /* registers */
  34. #define DM_NET_CTRL 0x00
  35. #define DM_RX_CTRL 0x05
  36. #define DM_SHARED_CTRL 0x0b
  37. #define DM_SHARED_ADDR 0x0c
  38. #define DM_SHARED_DATA 0x0d /* low + high */
  39. #define DM_PHY_ADDR 0x10 /* 6 bytes */
  40. #define DM_MCAST_ADDR 0x16 /* 8 bytes */
  41. #define DM_GPR_CTRL 0x1e
  42. #define DM_GPR_DATA 0x1f
  43. #define DM_MAX_MCAST 64
  44. #define DM_MCAST_SIZE 8
  45. #define DM_EEPROM_LEN 256
  46. #define DM_TX_OVERHEAD 2 /* 2 byte header */
  47. #define DM_RX_OVERHEAD 7 /* 3 byte header + 4 byte crc tail */
  48. #define DM_TIMEOUT 1000
  49. static int dm_read(struct usbnet *dev, u8 reg, u16 length, void *data)
  50. {
  51. int err;
  52. err = usbnet_read_cmd(dev, DM_READ_REGS,
  53. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  54. 0, reg, data, length);
  55. if(err != length && err >= 0)
  56. err = -EINVAL;
  57. return err;
  58. }
  59. static int dm_read_reg(struct usbnet *dev, u8 reg, u8 *value)
  60. {
  61. return dm_read(dev, reg, 1, value);
  62. }
  63. static int dm_write(struct usbnet *dev, u8 reg, u16 length, void *data)
  64. {
  65. int err;
  66. err = usbnet_write_cmd(dev, DM_WRITE_REGS,
  67. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  68. 0, reg, data, length);
  69. if (err >= 0 && err < length)
  70. err = -EINVAL;
  71. return err;
  72. }
  73. static int dm_write_reg(struct usbnet *dev, u8 reg, u8 value)
  74. {
  75. return usbnet_write_cmd(dev, DM_WRITE_REGS,
  76. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  77. value, reg, NULL, 0);
  78. }
  79. static void dm_write_async_helper(struct usbnet *dev, u8 reg, u8 value,
  80. u16 length, void *data)
  81. {
  82. usbnet_write_cmd_async(dev, DM_WRITE_REGS,
  83. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  84. value, reg, data, length);
  85. }
  86. static void dm_write_async(struct usbnet *dev, u8 reg, u16 length, void *data)
  87. {
  88. netdev_dbg(dev->net, "dm_write_async() reg=0x%02x length=%d\n", reg, length);
  89. dm_write_async_helper(dev, reg, 0, length, data);
  90. }
  91. static void dm_write_reg_async(struct usbnet *dev, u8 reg, u8 value)
  92. {
  93. netdev_dbg(dev->net, "dm_write_reg_async() reg=0x%02x value=0x%02x\n",
  94. reg, value);
  95. dm_write_async_helper(dev, reg, value, 0, NULL);
  96. }
  97. static int dm_read_shared_word(struct usbnet *dev, int phy, u8 reg, __le16 *value)
  98. {
  99. int ret, i;
  100. mutex_lock(&dev->phy_mutex);
  101. dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg);
  102. dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0xc : 0x4);
  103. for (i = 0; i < DM_TIMEOUT; i++) {
  104. u8 tmp;
  105. udelay(1);
  106. ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp);
  107. if (ret < 0)
  108. goto out;
  109. /* ready */
  110. if ((tmp & 1) == 0)
  111. break;
  112. }
  113. if (i == DM_TIMEOUT) {
  114. netdev_err(dev->net, "%s read timed out!\n", phy ? "phy" : "eeprom");
  115. ret = -EIO;
  116. goto out;
  117. }
  118. dm_write_reg(dev, DM_SHARED_CTRL, 0x0);
  119. ret = dm_read(dev, DM_SHARED_DATA, 2, value);
  120. netdev_dbg(dev->net, "read shared %d 0x%02x returned 0x%04x, %d\n",
  121. phy, reg, *value, ret);
  122. out:
  123. mutex_unlock(&dev->phy_mutex);
  124. return ret;
  125. }
  126. static int dm_write_shared_word(struct usbnet *dev, int phy, u8 reg, __le16 value)
  127. {
  128. int ret, i;
  129. mutex_lock(&dev->phy_mutex);
  130. ret = dm_write(dev, DM_SHARED_DATA, 2, &value);
  131. if (ret < 0)
  132. goto out;
  133. dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg);
  134. dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0x1a : 0x12);
  135. for (i = 0; i < DM_TIMEOUT; i++) {
  136. u8 tmp;
  137. udelay(1);
  138. ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp);
  139. if (ret < 0)
  140. goto out;
  141. /* ready */
  142. if ((tmp & 1) == 0)
  143. break;
  144. }
  145. if (i == DM_TIMEOUT) {
  146. netdev_err(dev->net, "%s write timed out!\n", phy ? "phy" : "eeprom");
  147. ret = -EIO;
  148. goto out;
  149. }
  150. dm_write_reg(dev, DM_SHARED_CTRL, 0x0);
  151. out:
  152. mutex_unlock(&dev->phy_mutex);
  153. return ret;
  154. }
  155. static int dm_read_eeprom_word(struct usbnet *dev, u8 offset, void *value)
  156. {
  157. return dm_read_shared_word(dev, 0, offset, value);
  158. }
  159. static int dm9601_get_eeprom_len(struct net_device *dev)
  160. {
  161. return DM_EEPROM_LEN;
  162. }
  163. static int dm9601_get_eeprom(struct net_device *net,
  164. struct ethtool_eeprom *eeprom, u8 * data)
  165. {
  166. struct usbnet *dev = netdev_priv(net);
  167. __le16 *ebuf = (__le16 *) data;
  168. int i;
  169. /* access is 16bit */
  170. if ((eeprom->offset % 2) || (eeprom->len % 2))
  171. return -EINVAL;
  172. for (i = 0; i < eeprom->len / 2; i++) {
  173. if (dm_read_eeprom_word(dev, eeprom->offset / 2 + i,
  174. &ebuf[i]) < 0)
  175. return -EINVAL;
  176. }
  177. return 0;
  178. }
  179. static int dm9601_mdio_read(struct net_device *netdev, int phy_id, int loc)
  180. {
  181. struct usbnet *dev = netdev_priv(netdev);
  182. __le16 res;
  183. if (phy_id) {
  184. netdev_dbg(dev->net, "Only internal phy supported\n");
  185. return 0;
  186. }
  187. dm_read_shared_word(dev, 1, loc, &res);
  188. netdev_dbg(dev->net,
  189. "dm9601_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
  190. phy_id, loc, le16_to_cpu(res));
  191. return le16_to_cpu(res);
  192. }
  193. static void dm9601_mdio_write(struct net_device *netdev, int phy_id, int loc,
  194. int val)
  195. {
  196. struct usbnet *dev = netdev_priv(netdev);
  197. __le16 res = cpu_to_le16(val);
  198. if (phy_id) {
  199. netdev_dbg(dev->net, "Only internal phy supported\n");
  200. return;
  201. }
  202. netdev_dbg(dev->net, "dm9601_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
  203. phy_id, loc, val);
  204. dm_write_shared_word(dev, 1, loc, res);
  205. }
  206. static void dm9601_get_drvinfo(struct net_device *net,
  207. struct ethtool_drvinfo *info)
  208. {
  209. /* Inherit standard device info */
  210. usbnet_get_drvinfo(net, info);
  211. info->eedump_len = DM_EEPROM_LEN;
  212. }
  213. static u32 dm9601_get_link(struct net_device *net)
  214. {
  215. struct usbnet *dev = netdev_priv(net);
  216. return mii_link_ok(&dev->mii);
  217. }
  218. static int dm9601_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
  219. {
  220. struct usbnet *dev = netdev_priv(net);
  221. return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
  222. }
  223. static const struct ethtool_ops dm9601_ethtool_ops = {
  224. .get_drvinfo = dm9601_get_drvinfo,
  225. .get_link = dm9601_get_link,
  226. .get_msglevel = usbnet_get_msglevel,
  227. .set_msglevel = usbnet_set_msglevel,
  228. .get_eeprom_len = dm9601_get_eeprom_len,
  229. .get_eeprom = dm9601_get_eeprom,
  230. .get_settings = usbnet_get_settings,
  231. .set_settings = usbnet_set_settings,
  232. .nway_reset = usbnet_nway_reset,
  233. };
  234. static void dm9601_set_multicast(struct net_device *net)
  235. {
  236. struct usbnet *dev = netdev_priv(net);
  237. /* We use the 20 byte dev->data for our 8 byte filter buffer
  238. * to avoid allocating memory that is tricky to free later */
  239. u8 *hashes = (u8 *) & dev->data;
  240. u8 rx_ctl = 0x31;
  241. memset(hashes, 0x00, DM_MCAST_SIZE);
  242. hashes[DM_MCAST_SIZE - 1] |= 0x80; /* broadcast address */
  243. if (net->flags & IFF_PROMISC) {
  244. rx_ctl |= 0x02;
  245. } else if (net->flags & IFF_ALLMULTI ||
  246. netdev_mc_count(net) > DM_MAX_MCAST) {
  247. rx_ctl |= 0x04;
  248. } else if (!netdev_mc_empty(net)) {
  249. struct netdev_hw_addr *ha;
  250. netdev_for_each_mc_addr(ha, net) {
  251. u32 crc = ether_crc(ETH_ALEN, ha->addr) >> 26;
  252. hashes[crc >> 3] |= 1 << (crc & 0x7);
  253. }
  254. }
  255. dm_write_async(dev, DM_MCAST_ADDR, DM_MCAST_SIZE, hashes);
  256. dm_write_reg_async(dev, DM_RX_CTRL, rx_ctl);
  257. }
  258. static void __dm9601_set_mac_address(struct usbnet *dev)
  259. {
  260. dm_write_async(dev, DM_PHY_ADDR, ETH_ALEN, dev->net->dev_addr);
  261. }
  262. static int dm9601_set_mac_address(struct net_device *net, void *p)
  263. {
  264. struct sockaddr *addr = p;
  265. struct usbnet *dev = netdev_priv(net);
  266. if (!is_valid_ether_addr(addr->sa_data)) {
  267. dev_err(&net->dev, "not setting invalid mac address %pM\n",
  268. addr->sa_data);
  269. return -EINVAL;
  270. }
  271. memcpy(net->dev_addr, addr->sa_data, net->addr_len);
  272. __dm9601_set_mac_address(dev);
  273. return 0;
  274. }
  275. static const struct net_device_ops dm9601_netdev_ops = {
  276. .ndo_open = usbnet_open,
  277. .ndo_stop = usbnet_stop,
  278. .ndo_start_xmit = usbnet_start_xmit,
  279. .ndo_tx_timeout = usbnet_tx_timeout,
  280. .ndo_change_mtu = usbnet_change_mtu,
  281. .ndo_validate_addr = eth_validate_addr,
  282. .ndo_do_ioctl = dm9601_ioctl,
  283. .ndo_set_rx_mode = dm9601_set_multicast,
  284. .ndo_set_mac_address = dm9601_set_mac_address,
  285. };
  286. static int dm9601_bind(struct usbnet *dev, struct usb_interface *intf)
  287. {
  288. int ret;
  289. u8 mac[ETH_ALEN];
  290. ret = usbnet_get_endpoints(dev, intf);
  291. if (ret)
  292. goto out;
  293. dev->net->netdev_ops = &dm9601_netdev_ops;
  294. dev->net->ethtool_ops = &dm9601_ethtool_ops;
  295. dev->net->hard_header_len += DM_TX_OVERHEAD;
  296. dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
  297. dev->rx_urb_size = dev->net->mtu + ETH_HLEN + DM_RX_OVERHEAD;
  298. dev->mii.dev = dev->net;
  299. dev->mii.mdio_read = dm9601_mdio_read;
  300. dev->mii.mdio_write = dm9601_mdio_write;
  301. dev->mii.phy_id_mask = 0x1f;
  302. dev->mii.reg_num_mask = 0x1f;
  303. /* reset */
  304. dm_write_reg(dev, DM_NET_CTRL, 1);
  305. udelay(20);
  306. /* read MAC */
  307. if (dm_read(dev, DM_PHY_ADDR, ETH_ALEN, mac) < 0) {
  308. printk(KERN_ERR "Error reading MAC address\n");
  309. ret = -ENODEV;
  310. goto out;
  311. }
  312. /*
  313. * Overwrite the auto-generated address only with good ones.
  314. */
  315. if (is_valid_ether_addr(mac))
  316. memcpy(dev->net->dev_addr, mac, ETH_ALEN);
  317. else {
  318. printk(KERN_WARNING
  319. "dm9601: No valid MAC address in EEPROM, using %pM\n",
  320. dev->net->dev_addr);
  321. __dm9601_set_mac_address(dev);
  322. }
  323. /* power up phy */
  324. dm_write_reg(dev, DM_GPR_CTRL, 1);
  325. dm_write_reg(dev, DM_GPR_DATA, 0);
  326. /* receive broadcast packets */
  327. dm9601_set_multicast(dev->net);
  328. dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
  329. dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  330. ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
  331. mii_nway_restart(&dev->mii);
  332. out:
  333. return ret;
  334. }
  335. static int dm9601_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  336. {
  337. u8 status;
  338. int len;
  339. /* format:
  340. b1: rx status
  341. b2: packet length (incl crc) low
  342. b3: packet length (incl crc) high
  343. b4..n-4: packet data
  344. bn-3..bn: ethernet crc
  345. */
  346. if (unlikely(skb->len < DM_RX_OVERHEAD)) {
  347. dev_err(&dev->udev->dev, "unexpected tiny rx frame\n");
  348. return 0;
  349. }
  350. status = skb->data[0];
  351. len = (skb->data[1] | (skb->data[2] << 8)) - 4;
  352. if (unlikely(status & 0xbf)) {
  353. if (status & 0x01) dev->net->stats.rx_fifo_errors++;
  354. if (status & 0x02) dev->net->stats.rx_crc_errors++;
  355. if (status & 0x04) dev->net->stats.rx_frame_errors++;
  356. if (status & 0x20) dev->net->stats.rx_missed_errors++;
  357. if (status & 0x90) dev->net->stats.rx_length_errors++;
  358. return 0;
  359. }
  360. skb_pull(skb, 3);
  361. skb_trim(skb, len);
  362. return 1;
  363. }
  364. static struct sk_buff *dm9601_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
  365. gfp_t flags)
  366. {
  367. int len;
  368. /* format:
  369. b1: packet length low
  370. b2: packet length high
  371. b3..n: packet data
  372. */
  373. len = skb->len;
  374. if (skb_headroom(skb) < DM_TX_OVERHEAD) {
  375. struct sk_buff *skb2;
  376. skb2 = skb_copy_expand(skb, DM_TX_OVERHEAD, 0, flags);
  377. dev_kfree_skb_any(skb);
  378. skb = skb2;
  379. if (!skb)
  380. return NULL;
  381. }
  382. __skb_push(skb, DM_TX_OVERHEAD);
  383. /* usbnet adds padding if length is a multiple of packet size
  384. if so, adjust length value in header */
  385. if ((skb->len % dev->maxpacket) == 0)
  386. len++;
  387. skb->data[0] = len;
  388. skb->data[1] = len >> 8;
  389. return skb;
  390. }
  391. static void dm9601_status(struct usbnet *dev, struct urb *urb)
  392. {
  393. int link;
  394. u8 *buf;
  395. /* format:
  396. b0: net status
  397. b1: tx status 1
  398. b2: tx status 2
  399. b3: rx status
  400. b4: rx overflow
  401. b5: rx count
  402. b6: tx count
  403. b7: gpr
  404. */
  405. if (urb->actual_length < 8)
  406. return;
  407. buf = urb->transfer_buffer;
  408. link = !!(buf[0] & 0x40);
  409. if (netif_carrier_ok(dev->net) != link) {
  410. if (link) {
  411. netif_carrier_on(dev->net);
  412. usbnet_defer_kevent (dev, EVENT_LINK_RESET);
  413. }
  414. else
  415. netif_carrier_off(dev->net);
  416. netdev_dbg(dev->net, "Link Status is: %d\n", link);
  417. }
  418. }
  419. static int dm9601_link_reset(struct usbnet *dev)
  420. {
  421. struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
  422. mii_check_media(&dev->mii, 1, 1);
  423. mii_ethtool_gset(&dev->mii, &ecmd);
  424. netdev_dbg(dev->net, "link_reset() speed: %u duplex: %d\n",
  425. ethtool_cmd_speed(&ecmd), ecmd.duplex);
  426. return 0;
  427. }
  428. static const struct driver_info dm9601_info = {
  429. .description = "Davicom DM9601 USB Ethernet",
  430. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  431. .bind = dm9601_bind,
  432. .rx_fixup = dm9601_rx_fixup,
  433. .tx_fixup = dm9601_tx_fixup,
  434. .status = dm9601_status,
  435. .link_reset = dm9601_link_reset,
  436. .reset = dm9601_link_reset,
  437. };
  438. static const struct usb_device_id products[] = {
  439. {
  440. USB_DEVICE(0x07aa, 0x9601), /* Corega FEther USB-TXC */
  441. .driver_info = (unsigned long)&dm9601_info,
  442. },
  443. {
  444. USB_DEVICE(0x0a46, 0x9601), /* Davicom USB-100 */
  445. .driver_info = (unsigned long)&dm9601_info,
  446. },
  447. {
  448. USB_DEVICE(0x0a46, 0x6688), /* ZT6688 USB NIC */
  449. .driver_info = (unsigned long)&dm9601_info,
  450. },
  451. {
  452. USB_DEVICE(0x0a46, 0x0268), /* ShanTou ST268 USB NIC */
  453. .driver_info = (unsigned long)&dm9601_info,
  454. },
  455. {
  456. USB_DEVICE(0x0a46, 0x8515), /* ADMtek ADM8515 USB NIC */
  457. .driver_info = (unsigned long)&dm9601_info,
  458. },
  459. {
  460. USB_DEVICE(0x0a47, 0x9601), /* Hirose USB-100 */
  461. .driver_info = (unsigned long)&dm9601_info,
  462. },
  463. {
  464. USB_DEVICE(0x0fe6, 0x8101), /* DM9601 USB to Fast Ethernet Adapter */
  465. .driver_info = (unsigned long)&dm9601_info,
  466. },
  467. {
  468. USB_DEVICE(0x0fe6, 0x9700), /* DM9601 USB to Fast Ethernet Adapter */
  469. .driver_info = (unsigned long)&dm9601_info,
  470. },
  471. {
  472. USB_DEVICE(0x0a46, 0x9000), /* DM9000E */
  473. .driver_info = (unsigned long)&dm9601_info,
  474. },
  475. {}, // END
  476. };
  477. MODULE_DEVICE_TABLE(usb, products);
  478. static struct usb_driver dm9601_driver = {
  479. .name = "dm9601",
  480. .id_table = products,
  481. .probe = usbnet_probe,
  482. .disconnect = usbnet_disconnect,
  483. .suspend = usbnet_suspend,
  484. .resume = usbnet_resume,
  485. .disable_hub_initiated_lpm = 1,
  486. };
  487. module_usb_driver(dm9601_driver);
  488. MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
  489. MODULE_DESCRIPTION("Davicom DM9601 USB 1.1 ethernet devices");
  490. MODULE_LICENSE("GPL");