cx82310_eth.c 9.0 KB

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