cx82310_eth.c 9.4 KB

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