dm9601.c 14 KB

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