cx82310_eth.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Driver for USB ethernet port of Conexant CX82310-based ADSL routers
  3. * Copyright (C) 2010 by Ondrej Zary
  4. * some parts inspired by the cxacru driver
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/ethtool.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/mii.h>
  26. #include <linux/usb.h>
  27. #include <linux/usb/usbnet.h>
  28. enum cx82310_cmd {
  29. CMD_START = 0x84, /* no effect? */
  30. CMD_STOP = 0x85, /* no effect? */
  31. CMD_GET_STATUS = 0x90, /* returns nothing? */
  32. CMD_GET_MAC_ADDR = 0x91, /* read MAC address */
  33. CMD_GET_LINK_STATUS = 0x92, /* not useful, link is always up */
  34. CMD_ETHERNET_MODE = 0x99, /* unknown, needed during init */
  35. };
  36. enum cx82310_status {
  37. STATUS_UNDEFINED,
  38. STATUS_SUCCESS,
  39. STATUS_ERROR,
  40. STATUS_UNSUPPORTED,
  41. STATUS_UNIMPLEMENTED,
  42. STATUS_PARAMETER_ERROR,
  43. STATUS_DBG_LOOPBACK,
  44. };
  45. #define CMD_PACKET_SIZE 64
  46. /* first command after power on can take around 8 seconds */
  47. #define CMD_TIMEOUT 15000
  48. #define CMD_REPLY_RETRY 5
  49. #define CX82310_MTU 1514
  50. #define CMD_EP 0x01
  51. /*
  52. * execute control command
  53. * - optionally send some data (command parameters)
  54. * - optionally wait for the reply
  55. * - optionally read some data from the reply
  56. */
  57. static int cx82310_cmd(struct usbnet *dev, enum cx82310_cmd cmd, bool reply,
  58. u8 *wdata, int wlen, u8 *rdata, int rlen)
  59. {
  60. int actual_len, retries, ret;
  61. struct usb_device *udev = dev->udev;
  62. u8 *buf = kzalloc(CMD_PACKET_SIZE, GFP_KERNEL);
  63. if (!buf)
  64. return -ENOMEM;
  65. /* create command packet */
  66. buf[0] = cmd;
  67. if (wdata)
  68. memcpy(buf + 4, wdata, min_t(int, wlen, CMD_PACKET_SIZE - 4));
  69. /* send command packet */
  70. ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, CMD_EP), buf,
  71. CMD_PACKET_SIZE, &actual_len, CMD_TIMEOUT);
  72. if (ret < 0) {
  73. dev_err(&dev->udev->dev, "send command %#x: error %d\n",
  74. cmd, ret);
  75. goto end;
  76. }
  77. if (reply) {
  78. /* wait for reply, retry if it's empty */
  79. for (retries = 0; retries < CMD_REPLY_RETRY; retries++) {
  80. ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, CMD_EP),
  81. buf, CMD_PACKET_SIZE, &actual_len,
  82. CMD_TIMEOUT);
  83. if (ret < 0) {
  84. dev_err(&dev->udev->dev,
  85. "reply receive error %d\n", ret);
  86. goto end;
  87. }
  88. if (actual_len > 0)
  89. break;
  90. }
  91. if (actual_len == 0) {
  92. dev_err(&dev->udev->dev, "no reply to command %#x\n",
  93. cmd);
  94. ret = -EIO;
  95. goto end;
  96. }
  97. if (buf[0] != cmd) {
  98. dev_err(&dev->udev->dev,
  99. "got reply to command %#x, expected: %#x\n",
  100. buf[0], cmd);
  101. ret = -EIO;
  102. goto end;
  103. }
  104. if (buf[1] != STATUS_SUCCESS) {
  105. dev_err(&dev->udev->dev, "command %#x failed: %#x\n",
  106. cmd, buf[1]);
  107. ret = -EIO;
  108. goto end;
  109. }
  110. if (rdata)
  111. memcpy(rdata, buf + 4,
  112. min_t(int, rlen, CMD_PACKET_SIZE - 4));
  113. }
  114. end:
  115. kfree(buf);
  116. return ret;
  117. }
  118. #define partial_len data[0] /* length of partial packet data */
  119. #define partial_rem data[1] /* remaining (missing) data length */
  120. #define partial_data data[2] /* partial packet data */
  121. static int cx82310_bind(struct usbnet *dev, struct usb_interface *intf)
  122. {
  123. int ret;
  124. char buf[15];
  125. struct usb_device *udev = dev->udev;
  126. /* avoid ADSL modems - continue only if iProduct is "USB NET CARD" */
  127. if (usb_string(udev, udev->descriptor.iProduct, buf, sizeof(buf)) > 0
  128. && strcmp(buf, "USB NET CARD")) {
  129. dev_info(&udev->dev, "ignoring: probably an ADSL modem\n");
  130. return -ENODEV;
  131. }
  132. ret = usbnet_get_endpoints(dev, intf);
  133. if (ret)
  134. return ret;
  135. /*
  136. * this must not include ethernet header as the device can send partial
  137. * packets with no header (and sometimes even empty URBs)
  138. */
  139. dev->net->hard_header_len = 0;
  140. /* we can send at most 1514 bytes of data (+ 2-byte header) per URB */
  141. dev->hard_mtu = CX82310_MTU + 2;
  142. /* we can receive URBs up to 4KB from the device */
  143. dev->rx_urb_size = 4096;
  144. dev->partial_data = (unsigned long) kmalloc(dev->hard_mtu, GFP_KERNEL);
  145. if (!dev->partial_data)
  146. return -ENOMEM;
  147. /* enable ethernet mode (?) */
  148. ret = cx82310_cmd(dev, CMD_ETHERNET_MODE, true, "\x01", 1, NULL, 0);
  149. if (ret) {
  150. dev_err(&udev->dev, "unable to enable ethernet mode: %d\n",
  151. ret);
  152. goto err;
  153. }
  154. /* get the MAC address */
  155. ret = cx82310_cmd(dev, CMD_GET_MAC_ADDR, true, NULL, 0,
  156. dev->net->dev_addr, ETH_ALEN);
  157. if (ret) {
  158. dev_err(&udev->dev, "unable to read MAC address: %d\n", ret);
  159. goto err;
  160. }
  161. /* start (does not seem to have any effect?) */
  162. ret = cx82310_cmd(dev, CMD_START, false, NULL, 0, NULL, 0);
  163. if (ret)
  164. goto err;
  165. return 0;
  166. err:
  167. kfree((void *)dev->partial_data);
  168. return ret;
  169. }
  170. static void cx82310_unbind(struct usbnet *dev, struct usb_interface *intf)
  171. {
  172. kfree((void *)dev->partial_data);
  173. }
  174. /*
  175. * RX is NOT easy - we can receive multiple packets per skb, each having 2-byte
  176. * packet length at the beginning.
  177. * The last packet might be incomplete (when it crosses the 4KB URB size),
  178. * continuing in the next skb (without any headers).
  179. * If a packet has odd length, there is one extra byte at the end (before next
  180. * packet or at the end of the URB).
  181. */
  182. static int cx82310_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  183. {
  184. int len;
  185. struct sk_buff *skb2;
  186. /*
  187. * If the last skb ended with an incomplete packet, this skb contains
  188. * end of that packet at the beginning.
  189. */
  190. if (dev->partial_rem) {
  191. len = dev->partial_len + dev->partial_rem;
  192. skb2 = alloc_skb(len, GFP_ATOMIC);
  193. if (!skb2)
  194. return 0;
  195. skb_put(skb2, len);
  196. memcpy(skb2->data, (void *)dev->partial_data,
  197. dev->partial_len);
  198. memcpy(skb2->data + dev->partial_len, skb->data,
  199. dev->partial_rem);
  200. usbnet_skb_return(dev, skb2);
  201. skb_pull(skb, (dev->partial_rem + 1) & ~1);
  202. dev->partial_rem = 0;
  203. if (skb->len < 2)
  204. return 1;
  205. }
  206. /* a skb can contain multiple packets */
  207. while (skb->len > 1) {
  208. /* first two bytes are packet length */
  209. len = skb->data[0] | (skb->data[1] << 8);
  210. skb_pull(skb, 2);
  211. /* if last packet in the skb, let usbnet to process it */
  212. if (len == skb->len || len + 1 == skb->len) {
  213. skb_trim(skb, len);
  214. break;
  215. }
  216. if (len > CX82310_MTU) {
  217. dev_err(&dev->udev->dev, "RX packet too long: %d B\n",
  218. len);
  219. return 0;
  220. }
  221. /* incomplete packet, save it for the next skb */
  222. if (len > skb->len) {
  223. dev->partial_len = skb->len;
  224. dev->partial_rem = len - skb->len;
  225. memcpy((void *)dev->partial_data, skb->data,
  226. dev->partial_len);
  227. skb_pull(skb, skb->len);
  228. break;
  229. }
  230. skb2 = alloc_skb(len, GFP_ATOMIC);
  231. if (!skb2)
  232. return 0;
  233. skb_put(skb2, len);
  234. memcpy(skb2->data, skb->data, len);
  235. /* process the packet */
  236. usbnet_skb_return(dev, skb2);
  237. skb_pull(skb, (len + 1) & ~1);
  238. }
  239. /* let usbnet process the last packet */
  240. return 1;
  241. }
  242. /* TX is easy, just add 2 bytes of length at the beginning */
  243. static struct sk_buff *cx82310_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
  244. gfp_t flags)
  245. {
  246. int len = skb->len;
  247. if (skb_headroom(skb) < 2) {
  248. struct sk_buff *skb2 = skb_copy_expand(skb, 2, 0, flags);
  249. dev_kfree_skb_any(skb);
  250. skb = skb2;
  251. if (!skb)
  252. return NULL;
  253. }
  254. skb_push(skb, 2);
  255. skb->data[0] = len;
  256. skb->data[1] = len >> 8;
  257. return skb;
  258. }
  259. static const struct driver_info cx82310_info = {
  260. .description = "Conexant CX82310 USB ethernet",
  261. .flags = FLAG_ETHER,
  262. .bind = cx82310_bind,
  263. .unbind = cx82310_unbind,
  264. .rx_fixup = cx82310_rx_fixup,
  265. .tx_fixup = cx82310_tx_fixup,
  266. };
  267. static const struct usb_device_id products[] = {
  268. {
  269. USB_DEVICE_AND_INTERFACE_INFO(0x0572, 0xcb01, 0xff, 0, 0),
  270. .driver_info = (unsigned long) &cx82310_info
  271. },
  272. { },
  273. };
  274. MODULE_DEVICE_TABLE(usb, products);
  275. static struct usb_driver cx82310_driver = {
  276. .name = "cx82310_eth",
  277. .id_table = products,
  278. .probe = usbnet_probe,
  279. .disconnect = usbnet_disconnect,
  280. .suspend = usbnet_suspend,
  281. .resume = usbnet_resume,
  282. .disable_hub_initiated_lpm = 1,
  283. };
  284. module_usb_driver(cx82310_driver);
  285. MODULE_AUTHOR("Ondrej Zary");
  286. MODULE_DESCRIPTION("Conexant CX82310-based ADSL router USB ethernet driver");
  287. MODULE_LICENSE("GPL");