ch9200.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * USB 10M/100M ethernet adapter
  3. *
  4. * This file is licensed under the terms of the GNU General Public License
  5. * version 2. This program is licensed "as is" without any warranty of any
  6. * kind, whether express or implied
  7. *
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/sched.h>
  12. #include <linux/stddef.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 <linux/usb/usbnet.h>
  21. #include <linux/slab.h>
  22. #define CH9200_VID 0x1A86
  23. #define CH9200_PID_E092 0xE092
  24. #define CTRL_TIMEOUT_MS 1000
  25. #define CONTROL_TIMEOUT_MS 1000
  26. #define REQUEST_READ 0x0E
  27. #define REQUEST_WRITE 0x0F
  28. /* Address space:
  29. * 00-63 : MII
  30. * 64-128: MAC
  31. *
  32. * Note: all accesses must be 16-bit
  33. */
  34. #define MAC_REG_CTRL 64
  35. #define MAC_REG_STATUS 66
  36. #define MAC_REG_INTERRUPT_MASK 68
  37. #define MAC_REG_PHY_COMMAND 70
  38. #define MAC_REG_PHY_DATA 72
  39. #define MAC_REG_STATION_L 74
  40. #define MAC_REG_STATION_M 76
  41. #define MAC_REG_STATION_H 78
  42. #define MAC_REG_HASH_L 80
  43. #define MAC_REG_HASH_M1 82
  44. #define MAC_REG_HASH_M2 84
  45. #define MAC_REG_HASH_H 86
  46. #define MAC_REG_THRESHOLD 88
  47. #define MAC_REG_FIFO_DEPTH 90
  48. #define MAC_REG_PAUSE 92
  49. #define MAC_REG_FLOW_CONTROL 94
  50. /* Control register bits
  51. *
  52. * Note: bits 13 and 15 are reserved
  53. */
  54. #define LOOPBACK (0x01 << 14)
  55. #define BASE100X (0x01 << 12)
  56. #define MBPS_10 (0x01 << 11)
  57. #define DUPLEX_MODE (0x01 << 10)
  58. #define PAUSE_FRAME (0x01 << 9)
  59. #define PROMISCUOUS (0x01 << 8)
  60. #define MULTICAST (0x01 << 7)
  61. #define BROADCAST (0x01 << 6)
  62. #define HASH (0x01 << 5)
  63. #define APPEND_PAD (0x01 << 4)
  64. #define APPEND_CRC (0x01 << 3)
  65. #define TRANSMITTER_ACTION (0x01 << 2)
  66. #define RECEIVER_ACTION (0x01 << 1)
  67. #define DMA_ACTION (0x01 << 0)
  68. /* Status register bits
  69. *
  70. * Note: bits 7-15 are reserved
  71. */
  72. #define ALIGNMENT (0x01 << 6)
  73. #define FIFO_OVER_RUN (0x01 << 5)
  74. #define FIFO_UNDER_RUN (0x01 << 4)
  75. #define RX_ERROR (0x01 << 3)
  76. #define RX_COMPLETE (0x01 << 2)
  77. #define TX_ERROR (0x01 << 1)
  78. #define TX_COMPLETE (0x01 << 0)
  79. /* FIFO depth register bits
  80. *
  81. * Note: bits 6 and 14 are reserved
  82. */
  83. #define ETH_TXBD (0x01 << 15)
  84. #define ETN_TX_FIFO_DEPTH (0x01 << 8)
  85. #define ETH_RXBD (0x01 << 7)
  86. #define ETH_RX_FIFO_DEPTH (0x01 << 0)
  87. static int control_read(struct usbnet *dev,
  88. unsigned char request, unsigned short value,
  89. unsigned short index, void *data, unsigned short size,
  90. int timeout)
  91. {
  92. unsigned char *buf = NULL;
  93. unsigned char request_type;
  94. int err = 0;
  95. if (request == REQUEST_READ)
  96. request_type = (USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER);
  97. else
  98. request_type = (USB_DIR_IN | USB_TYPE_VENDOR |
  99. USB_RECIP_DEVICE);
  100. netdev_dbg(dev->net, "Control_read() index=0x%02x size=%d\n",
  101. index, size);
  102. buf = kmalloc(size, GFP_KERNEL);
  103. if (!buf) {
  104. err = -ENOMEM;
  105. goto err_out;
  106. }
  107. err = usb_control_msg(dev->udev,
  108. usb_rcvctrlpipe(dev->udev, 0),
  109. request, request_type, value, index, buf, size,
  110. timeout);
  111. if (err == size)
  112. memcpy(data, buf, size);
  113. else if (err >= 0)
  114. err = -EINVAL;
  115. kfree(buf);
  116. return err;
  117. err_out:
  118. return err;
  119. }
  120. static int control_write(struct usbnet *dev, unsigned char request,
  121. unsigned short value, unsigned short index,
  122. void *data, unsigned short size, int timeout)
  123. {
  124. unsigned char *buf = NULL;
  125. unsigned char request_type;
  126. int err = 0;
  127. if (request == REQUEST_WRITE)
  128. request_type = (USB_DIR_OUT | USB_TYPE_VENDOR |
  129. USB_RECIP_OTHER);
  130. else
  131. request_type = (USB_DIR_OUT | USB_TYPE_VENDOR |
  132. USB_RECIP_DEVICE);
  133. netdev_dbg(dev->net, "Control_write() index=0x%02x size=%d\n",
  134. index, size);
  135. if (data) {
  136. buf = kmemdup(data, size, GFP_KERNEL);
  137. if (!buf) {
  138. err = -ENOMEM;
  139. goto err_out;
  140. }
  141. }
  142. err = usb_control_msg(dev->udev,
  143. usb_sndctrlpipe(dev->udev, 0),
  144. request, request_type, value, index, buf, size,
  145. timeout);
  146. if (err >= 0 && err < size)
  147. err = -EINVAL;
  148. kfree(buf);
  149. return 0;
  150. err_out:
  151. return err;
  152. }
  153. static int ch9200_mdio_read(struct net_device *netdev, int phy_id, int loc)
  154. {
  155. struct usbnet *dev = netdev_priv(netdev);
  156. unsigned char buff[2];
  157. netdev_dbg(netdev, "ch9200_mdio_read phy_id:%02x loc:%02x\n",
  158. phy_id, loc);
  159. if (phy_id != 0)
  160. return -ENODEV;
  161. control_read(dev, REQUEST_READ, 0, loc * 2, buff, 0x02,
  162. CONTROL_TIMEOUT_MS);
  163. return (buff[0] | buff[1] << 8);
  164. }
  165. static void ch9200_mdio_write(struct net_device *netdev,
  166. int phy_id, int loc, int val)
  167. {
  168. struct usbnet *dev = netdev_priv(netdev);
  169. unsigned char buff[2];
  170. netdev_dbg(netdev, "ch9200_mdio_write() phy_id=%02x loc:%02x\n",
  171. phy_id, loc);
  172. if (phy_id != 0)
  173. return;
  174. buff[0] = (unsigned char)val;
  175. buff[1] = (unsigned char)(val >> 8);
  176. control_write(dev, REQUEST_WRITE, 0, loc * 2, buff, 0x02,
  177. CONTROL_TIMEOUT_MS);
  178. }
  179. static int ch9200_link_reset(struct usbnet *dev)
  180. {
  181. struct ethtool_cmd ecmd;
  182. mii_check_media(&dev->mii, 1, 1);
  183. mii_ethtool_gset(&dev->mii, &ecmd);
  184. netdev_dbg(dev->net, "link_reset() speed:%d duplex:%d\n",
  185. ecmd.speed, ecmd.duplex);
  186. return 0;
  187. }
  188. static void ch9200_status(struct usbnet *dev, struct urb *urb)
  189. {
  190. int link;
  191. unsigned char *buf;
  192. if (urb->actual_length < 16)
  193. return;
  194. buf = urb->transfer_buffer;
  195. link = !!(buf[0] & 0x01);
  196. if (link) {
  197. netif_carrier_on(dev->net);
  198. usbnet_defer_kevent(dev, EVENT_LINK_RESET);
  199. } else {
  200. netif_carrier_off(dev->net);
  201. }
  202. }
  203. static struct sk_buff *ch9200_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
  204. gfp_t flags)
  205. {
  206. int i = 0;
  207. int len = 0;
  208. int tx_overhead = 0;
  209. tx_overhead = 0x40;
  210. len = skb->len;
  211. if (skb_cow_head(skb, tx_overhead)) {
  212. dev_kfree_skb_any(skb);
  213. return NULL;
  214. }
  215. __skb_push(skb, tx_overhead);
  216. /* usbnet adds padding if length is a multiple of packet size
  217. * if so, adjust length value in header
  218. */
  219. if ((skb->len % dev->maxpacket) == 0)
  220. len++;
  221. skb->data[0] = len;
  222. skb->data[1] = len >> 8;
  223. skb->data[2] = 0x00;
  224. skb->data[3] = 0x80;
  225. for (i = 4; i < 48; i++)
  226. skb->data[i] = 0x00;
  227. skb->data[48] = len;
  228. skb->data[49] = len >> 8;
  229. skb->data[50] = 0x00;
  230. skb->data[51] = 0x80;
  231. for (i = 52; i < 64; i++)
  232. skb->data[i] = 0x00;
  233. return skb;
  234. }
  235. static int ch9200_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  236. {
  237. int len = 0;
  238. int rx_overhead = 0;
  239. rx_overhead = 64;
  240. if (unlikely(skb->len < rx_overhead)) {
  241. dev_err(&dev->udev->dev, "unexpected tiny rx frame\n");
  242. return 0;
  243. }
  244. len = (skb->data[skb->len - 16] | skb->data[skb->len - 15] << 8);
  245. skb_trim(skb, len);
  246. return 1;
  247. }
  248. static int get_mac_address(struct usbnet *dev, unsigned char *data)
  249. {
  250. int err = 0;
  251. unsigned char mac_addr[0x06];
  252. int rd_mac_len = 0;
  253. netdev_dbg(dev->net, "get_mac_address:\n\tusbnet VID:%0x PID:%0x\n",
  254. le16_to_cpu(dev->udev->descriptor.idVendor),
  255. le16_to_cpu(dev->udev->descriptor.idProduct));
  256. memset(mac_addr, 0, sizeof(mac_addr));
  257. rd_mac_len = control_read(dev, REQUEST_READ, 0,
  258. MAC_REG_STATION_L, mac_addr, 0x02,
  259. CONTROL_TIMEOUT_MS);
  260. rd_mac_len += control_read(dev, REQUEST_READ, 0, MAC_REG_STATION_M,
  261. mac_addr + 2, 0x02, CONTROL_TIMEOUT_MS);
  262. rd_mac_len += control_read(dev, REQUEST_READ, 0, MAC_REG_STATION_H,
  263. mac_addr + 4, 0x02, CONTROL_TIMEOUT_MS);
  264. if (rd_mac_len != ETH_ALEN)
  265. err = -EINVAL;
  266. data[0] = mac_addr[5];
  267. data[1] = mac_addr[4];
  268. data[2] = mac_addr[3];
  269. data[3] = mac_addr[2];
  270. data[4] = mac_addr[1];
  271. data[5] = mac_addr[0];
  272. return err;
  273. }
  274. static int ch9200_bind(struct usbnet *dev, struct usb_interface *intf)
  275. {
  276. int retval = 0;
  277. unsigned char data[2];
  278. retval = usbnet_get_endpoints(dev, intf);
  279. if (retval)
  280. return retval;
  281. dev->mii.dev = dev->net;
  282. dev->mii.mdio_read = ch9200_mdio_read;
  283. dev->mii.mdio_write = ch9200_mdio_write;
  284. dev->mii.reg_num_mask = 0x1f;
  285. dev->mii.phy_id_mask = 0x1f;
  286. dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
  287. dev->rx_urb_size = 24 * 64 + 16;
  288. mii_nway_restart(&dev->mii);
  289. data[0] = 0x01;
  290. data[1] = 0x0F;
  291. retval = control_write(dev, REQUEST_WRITE, 0, MAC_REG_THRESHOLD, data,
  292. 0x02, CONTROL_TIMEOUT_MS);
  293. data[0] = 0xA0;
  294. data[1] = 0x90;
  295. retval = control_write(dev, REQUEST_WRITE, 0, MAC_REG_FIFO_DEPTH, data,
  296. 0x02, CONTROL_TIMEOUT_MS);
  297. data[0] = 0x30;
  298. data[1] = 0x00;
  299. retval = control_write(dev, REQUEST_WRITE, 0, MAC_REG_PAUSE, data,
  300. 0x02, CONTROL_TIMEOUT_MS);
  301. data[0] = 0x17;
  302. data[1] = 0xD8;
  303. retval = control_write(dev, REQUEST_WRITE, 0, MAC_REG_FLOW_CONTROL,
  304. data, 0x02, CONTROL_TIMEOUT_MS);
  305. /* Undocumented register */
  306. data[0] = 0x01;
  307. data[1] = 0x00;
  308. retval = control_write(dev, REQUEST_WRITE, 0, 254, data, 0x02,
  309. CONTROL_TIMEOUT_MS);
  310. data[0] = 0x5F;
  311. data[1] = 0x0D;
  312. retval = control_write(dev, REQUEST_WRITE, 0, MAC_REG_CTRL, data, 0x02,
  313. CONTROL_TIMEOUT_MS);
  314. retval = get_mac_address(dev, dev->net->dev_addr);
  315. return retval;
  316. }
  317. static const struct driver_info ch9200_info = {
  318. .description = "CH9200 USB to Network Adaptor",
  319. .flags = FLAG_ETHER,
  320. .bind = ch9200_bind,
  321. .rx_fixup = ch9200_rx_fixup,
  322. .tx_fixup = ch9200_tx_fixup,
  323. .status = ch9200_status,
  324. .link_reset = ch9200_link_reset,
  325. .reset = ch9200_link_reset,
  326. };
  327. static const struct usb_device_id ch9200_products[] = {
  328. {
  329. USB_DEVICE(0x1A86, 0xE092),
  330. .driver_info = (unsigned long)&ch9200_info,
  331. },
  332. {},
  333. };
  334. MODULE_DEVICE_TABLE(usb, ch9200_products);
  335. static struct usb_driver ch9200_driver = {
  336. .name = "ch9200",
  337. .id_table = ch9200_products,
  338. .probe = usbnet_probe,
  339. .disconnect = usbnet_disconnect,
  340. .suspend = usbnet_suspend,
  341. .resume = usbnet_resume,
  342. };
  343. module_usb_driver(ch9200_driver);
  344. MODULE_DESCRIPTION("QinHeng CH9200 USB Network device");
  345. MODULE_LICENSE("GPL");