dm9601.c 14 KB

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