net1080.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Net1080 based USB host-to-host cables
  4. * Copyright (C) 2000-2005 by David Brownell
  5. */
  6. // #define DEBUG // error path messages, extra info
  7. // #define VERBOSE // more; success messages
  8. #include <linux/module.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/etherdevice.h>
  11. #include <linux/ethtool.h>
  12. #include <linux/workqueue.h>
  13. #include <linux/mii.h>
  14. #include <linux/usb.h>
  15. #include <linux/usb/usbnet.h>
  16. #include <linux/slab.h>
  17. #include <asm/unaligned.h>
  18. /*
  19. * Netchip 1080 driver ... http://www.netchip.com
  20. * (Sept 2004: End-of-life announcement has been sent.)
  21. * Used in (some) LapLink cables
  22. */
  23. #define frame_errors data[1]
  24. /*
  25. * NetChip framing of ethernet packets, supporting additional error
  26. * checks for links that may drop bulk packets from inside messages.
  27. * Odd USB length == always short read for last usb packet.
  28. * - nc_header
  29. * - Ethernet header (14 bytes)
  30. * - payload
  31. * - (optional padding byte, if needed so length becomes odd)
  32. * - nc_trailer
  33. *
  34. * This framing is to be avoided for non-NetChip devices.
  35. */
  36. struct nc_header { // packed:
  37. __le16 hdr_len; // sizeof nc_header (LE, all)
  38. __le16 packet_len; // payload size (including ethhdr)
  39. __le16 packet_id; // detects dropped packets
  40. #define MIN_HEADER 6
  41. // all else is optional, and must start with:
  42. // __le16 vendorId; // from usb-if
  43. // __le16 productId;
  44. } __packed;
  45. #define PAD_BYTE ((unsigned char)0xAC)
  46. struct nc_trailer {
  47. __le16 packet_id;
  48. } __packed;
  49. // packets may use FLAG_FRAMING_NC and optional pad
  50. #define FRAMED_SIZE(mtu) (sizeof (struct nc_header) \
  51. + sizeof (struct ethhdr) \
  52. + (mtu) \
  53. + 1 \
  54. + sizeof (struct nc_trailer))
  55. #define MIN_FRAMED FRAMED_SIZE(0)
  56. /* packets _could_ be up to 64KB... */
  57. #define NC_MAX_PACKET 32767
  58. /*
  59. * Zero means no timeout; else, how long a 64 byte bulk packet may be queued
  60. * before the hardware drops it. If that's done, the driver will need to
  61. * frame network packets to guard against the dropped USB packets. The win32
  62. * driver sets this for both sides of the link.
  63. */
  64. #define NC_READ_TTL_MS ((u8)255) // ms
  65. /*
  66. * We ignore most registers and EEPROM contents.
  67. */
  68. #define REG_USBCTL ((u8)0x04)
  69. #define REG_TTL ((u8)0x10)
  70. #define REG_STATUS ((u8)0x11)
  71. /*
  72. * Vendor specific requests to read/write data
  73. */
  74. #define REQUEST_REGISTER ((u8)0x10)
  75. #define REQUEST_EEPROM ((u8)0x11)
  76. static int
  77. nc_vendor_read(struct usbnet *dev, u8 req, u8 regnum, u16 *retval_ptr)
  78. {
  79. int status = usbnet_read_cmd(dev, req,
  80. USB_DIR_IN | USB_TYPE_VENDOR |
  81. USB_RECIP_DEVICE,
  82. 0, regnum, retval_ptr,
  83. sizeof *retval_ptr);
  84. if (status > 0)
  85. status = 0;
  86. if (!status)
  87. le16_to_cpus(retval_ptr);
  88. return status;
  89. }
  90. static inline int
  91. nc_register_read(struct usbnet *dev, u8 regnum, u16 *retval_ptr)
  92. {
  93. return nc_vendor_read(dev, REQUEST_REGISTER, regnum, retval_ptr);
  94. }
  95. // no retval ... can become async, usable in_interrupt()
  96. static void
  97. nc_vendor_write(struct usbnet *dev, u8 req, u8 regnum, u16 value)
  98. {
  99. usbnet_write_cmd(dev, req,
  100. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  101. value, regnum, NULL, 0);
  102. }
  103. static inline void
  104. nc_register_write(struct usbnet *dev, u8 regnum, u16 value)
  105. {
  106. nc_vendor_write(dev, REQUEST_REGISTER, regnum, value);
  107. }
  108. #if 0
  109. static void nc_dump_registers(struct usbnet *dev)
  110. {
  111. u8 reg;
  112. u16 *vp = kmalloc(sizeof (u16));
  113. if (!vp)
  114. return;
  115. netdev_dbg(dev->net, "registers:\n");
  116. for (reg = 0; reg < 0x20; reg++) {
  117. int retval;
  118. // reading some registers is trouble
  119. if (reg >= 0x08 && reg <= 0xf)
  120. continue;
  121. if (reg >= 0x12 && reg <= 0x1e)
  122. continue;
  123. retval = nc_register_read(dev, reg, vp);
  124. if (retval < 0)
  125. netdev_dbg(dev->net, "reg [0x%x] ==> error %d\n",
  126. reg, retval);
  127. else
  128. netdev_dbg(dev->net, "reg [0x%x] = 0x%x\n", reg, *vp);
  129. }
  130. kfree(vp);
  131. }
  132. #endif
  133. /*-------------------------------------------------------------------------*/
  134. /*
  135. * Control register
  136. */
  137. #define USBCTL_WRITABLE_MASK 0x1f0f
  138. // bits 15-13 reserved, r/o
  139. #define USBCTL_ENABLE_LANG (1 << 12)
  140. #define USBCTL_ENABLE_MFGR (1 << 11)
  141. #define USBCTL_ENABLE_PROD (1 << 10)
  142. #define USBCTL_ENABLE_SERIAL (1 << 9)
  143. #define USBCTL_ENABLE_DEFAULTS (1 << 8)
  144. // bits 7-4 reserved, r/o
  145. #define USBCTL_FLUSH_OTHER (1 << 3)
  146. #define USBCTL_FLUSH_THIS (1 << 2)
  147. #define USBCTL_DISCONN_OTHER (1 << 1)
  148. #define USBCTL_DISCONN_THIS (1 << 0)
  149. static inline void nc_dump_usbctl(struct usbnet *dev, u16 usbctl)
  150. {
  151. netif_dbg(dev, link, dev->net,
  152. "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s; this%s%s; other%s%s; r/o 0x%x\n",
  153. dev->udev->bus->bus_name, dev->udev->devpath,
  154. usbctl,
  155. (usbctl & USBCTL_ENABLE_LANG) ? " lang" : "",
  156. (usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "",
  157. (usbctl & USBCTL_ENABLE_PROD) ? " prod" : "",
  158. (usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "",
  159. (usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "",
  160. (usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "",
  161. (usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "",
  162. (usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "",
  163. (usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "",
  164. usbctl & ~USBCTL_WRITABLE_MASK);
  165. }
  166. /*-------------------------------------------------------------------------*/
  167. /*
  168. * Status register
  169. */
  170. #define STATUS_PORT_A (1 << 15)
  171. #define STATUS_CONN_OTHER (1 << 14)
  172. #define STATUS_SUSPEND_OTHER (1 << 13)
  173. #define STATUS_MAILBOX_OTHER (1 << 12)
  174. #define STATUS_PACKETS_OTHER(n) (((n) >> 8) & 0x03)
  175. #define STATUS_CONN_THIS (1 << 6)
  176. #define STATUS_SUSPEND_THIS (1 << 5)
  177. #define STATUS_MAILBOX_THIS (1 << 4)
  178. #define STATUS_PACKETS_THIS(n) (((n) >> 0) & 0x03)
  179. #define STATUS_UNSPEC_MASK 0x0c8c
  180. #define STATUS_NOISE_MASK ((u16)~(0x0303|STATUS_UNSPEC_MASK))
  181. static inline void nc_dump_status(struct usbnet *dev, u16 status)
  182. {
  183. netif_dbg(dev, link, dev->net,
  184. "net1080 %s-%s status 0x%x: this (%c) PKT=%d%s%s%s; other PKT=%d%s%s%s; unspec 0x%x\n",
  185. dev->udev->bus->bus_name, dev->udev->devpath,
  186. status,
  187. // XXX the packet counts don't seem right
  188. // (1 at reset, not 0); maybe UNSPEC too
  189. (status & STATUS_PORT_A) ? 'A' : 'B',
  190. STATUS_PACKETS_THIS(status),
  191. (status & STATUS_CONN_THIS) ? " CON" : "",
  192. (status & STATUS_SUSPEND_THIS) ? " SUS" : "",
  193. (status & STATUS_MAILBOX_THIS) ? " MBOX" : "",
  194. STATUS_PACKETS_OTHER(status),
  195. (status & STATUS_CONN_OTHER) ? " CON" : "",
  196. (status & STATUS_SUSPEND_OTHER) ? " SUS" : "",
  197. (status & STATUS_MAILBOX_OTHER) ? " MBOX" : "",
  198. status & STATUS_UNSPEC_MASK);
  199. }
  200. /*-------------------------------------------------------------------------*/
  201. /*
  202. * TTL register
  203. */
  204. #define TTL_OTHER(ttl) (0x00ff & (ttl >> 8))
  205. #define MK_TTL(this,other) ((u16)(((other)<<8)|(0x00ff&(this))))
  206. /*-------------------------------------------------------------------------*/
  207. static int net1080_reset(struct usbnet *dev)
  208. {
  209. u16 usbctl, status, ttl;
  210. u16 vp;
  211. int retval;
  212. // nc_dump_registers(dev);
  213. if ((retval = nc_register_read(dev, REG_STATUS, &vp)) < 0) {
  214. netdev_dbg(dev->net, "can't read %s-%s status: %d\n",
  215. dev->udev->bus->bus_name, dev->udev->devpath, retval);
  216. goto done;
  217. }
  218. status = vp;
  219. nc_dump_status(dev, status);
  220. if ((retval = nc_register_read(dev, REG_USBCTL, &vp)) < 0) {
  221. netdev_dbg(dev->net, "can't read USBCTL, %d\n", retval);
  222. goto done;
  223. }
  224. usbctl = vp;
  225. nc_dump_usbctl(dev, usbctl);
  226. nc_register_write(dev, REG_USBCTL,
  227. USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER);
  228. if ((retval = nc_register_read(dev, REG_TTL, &vp)) < 0) {
  229. netdev_dbg(dev->net, "can't read TTL, %d\n", retval);
  230. goto done;
  231. }
  232. ttl = vp;
  233. nc_register_write(dev, REG_TTL,
  234. MK_TTL(NC_READ_TTL_MS, TTL_OTHER(ttl)) );
  235. netdev_dbg(dev->net, "assigned TTL, %d ms\n", NC_READ_TTL_MS);
  236. netif_info(dev, link, dev->net, "port %c, peer %sconnected\n",
  237. (status & STATUS_PORT_A) ? 'A' : 'B',
  238. (status & STATUS_CONN_OTHER) ? "" : "dis");
  239. retval = 0;
  240. done:
  241. return retval;
  242. }
  243. static int net1080_check_connect(struct usbnet *dev)
  244. {
  245. int retval;
  246. u16 status;
  247. u16 vp;
  248. retval = nc_register_read(dev, REG_STATUS, &vp);
  249. status = vp;
  250. if (retval != 0) {
  251. netdev_dbg(dev->net, "net1080_check_conn read - %d\n", retval);
  252. return retval;
  253. }
  254. if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER)
  255. return -ENOLINK;
  256. return 0;
  257. }
  258. static void nc_ensure_sync(struct usbnet *dev)
  259. {
  260. if (++dev->frame_errors <= 5)
  261. return;
  262. if (usbnet_write_cmd_async(dev, REQUEST_REGISTER,
  263. USB_DIR_OUT | USB_TYPE_VENDOR |
  264. USB_RECIP_DEVICE,
  265. USBCTL_FLUSH_THIS |
  266. USBCTL_FLUSH_OTHER,
  267. REG_USBCTL, NULL, 0))
  268. return;
  269. netif_dbg(dev, rx_err, dev->net,
  270. "flush net1080; too many framing errors\n");
  271. dev->frame_errors = 0;
  272. }
  273. static int net1080_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  274. {
  275. struct nc_header *header;
  276. struct nc_trailer *trailer;
  277. u16 hdr_len, packet_len;
  278. /* This check is no longer done by usbnet */
  279. if (skb->len < dev->net->hard_header_len)
  280. return 0;
  281. if (!(skb->len & 0x01)) {
  282. netdev_dbg(dev->net, "rx framesize %d range %d..%d mtu %d\n",
  283. skb->len, dev->net->hard_header_len, dev->hard_mtu,
  284. dev->net->mtu);
  285. dev->net->stats.rx_frame_errors++;
  286. nc_ensure_sync(dev);
  287. return 0;
  288. }
  289. header = (struct nc_header *) skb->data;
  290. hdr_len = le16_to_cpup(&header->hdr_len);
  291. packet_len = le16_to_cpup(&header->packet_len);
  292. if (FRAMED_SIZE(packet_len) > NC_MAX_PACKET) {
  293. dev->net->stats.rx_frame_errors++;
  294. netdev_dbg(dev->net, "packet too big, %d\n", packet_len);
  295. nc_ensure_sync(dev);
  296. return 0;
  297. } else if (hdr_len < MIN_HEADER) {
  298. dev->net->stats.rx_frame_errors++;
  299. netdev_dbg(dev->net, "header too short, %d\n", hdr_len);
  300. nc_ensure_sync(dev);
  301. return 0;
  302. } else if (hdr_len > MIN_HEADER) {
  303. // out of band data for us?
  304. netdev_dbg(dev->net, "header OOB, %d bytes\n", hdr_len - MIN_HEADER);
  305. nc_ensure_sync(dev);
  306. // switch (vendor/product ids) { ... }
  307. }
  308. skb_pull(skb, hdr_len);
  309. trailer = (struct nc_trailer *)
  310. (skb->data + skb->len - sizeof *trailer);
  311. skb_trim(skb, skb->len - sizeof *trailer);
  312. if ((packet_len & 0x01) == 0) {
  313. if (skb->data [packet_len] != PAD_BYTE) {
  314. dev->net->stats.rx_frame_errors++;
  315. netdev_dbg(dev->net, "bad pad\n");
  316. return 0;
  317. }
  318. skb_trim(skb, skb->len - 1);
  319. }
  320. if (skb->len != packet_len) {
  321. dev->net->stats.rx_frame_errors++;
  322. netdev_dbg(dev->net, "bad packet len %d (expected %d)\n",
  323. skb->len, packet_len);
  324. nc_ensure_sync(dev);
  325. return 0;
  326. }
  327. if (header->packet_id != get_unaligned(&trailer->packet_id)) {
  328. dev->net->stats.rx_fifo_errors++;
  329. netdev_dbg(dev->net, "(2+ dropped) rx packet_id mismatch 0x%x 0x%x\n",
  330. le16_to_cpu(header->packet_id),
  331. le16_to_cpu(trailer->packet_id));
  332. return 0;
  333. }
  334. #if 0
  335. netdev_dbg(dev->net, "frame <rx h %d p %d id %d\n", header->hdr_len,
  336. header->packet_len, header->packet_id);
  337. #endif
  338. dev->frame_errors = 0;
  339. return 1;
  340. }
  341. static struct sk_buff *
  342. net1080_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  343. {
  344. struct sk_buff *skb2;
  345. struct nc_header *header = NULL;
  346. struct nc_trailer *trailer = NULL;
  347. int padlen = sizeof (struct nc_trailer);
  348. int len = skb->len;
  349. if (!((len + padlen + sizeof (struct nc_header)) & 0x01))
  350. padlen++;
  351. if (!skb_cloned(skb)) {
  352. int headroom = skb_headroom(skb);
  353. int tailroom = skb_tailroom(skb);
  354. if (padlen <= tailroom &&
  355. sizeof(struct nc_header) <= headroom)
  356. /* There's enough head and tail room */
  357. goto encapsulate;
  358. if ((sizeof (struct nc_header) + padlen) <
  359. (headroom + tailroom)) {
  360. /* There's enough total room, so just readjust */
  361. skb->data = memmove(skb->head
  362. + sizeof (struct nc_header),
  363. skb->data, skb->len);
  364. skb_set_tail_pointer(skb, len);
  365. goto encapsulate;
  366. }
  367. }
  368. /* Create a new skb to use with the correct size */
  369. skb2 = skb_copy_expand(skb,
  370. sizeof (struct nc_header),
  371. padlen,
  372. flags);
  373. dev_kfree_skb_any(skb);
  374. if (!skb2)
  375. return skb2;
  376. skb = skb2;
  377. encapsulate:
  378. /* header first */
  379. header = skb_push(skb, sizeof *header);
  380. header->hdr_len = cpu_to_le16(sizeof (*header));
  381. header->packet_len = cpu_to_le16(len);
  382. header->packet_id = cpu_to_le16((u16)dev->xid++);
  383. /* maybe pad; then trailer */
  384. if (!((skb->len + sizeof *trailer) & 0x01))
  385. skb_put_u8(skb, PAD_BYTE);
  386. trailer = skb_put(skb, sizeof *trailer);
  387. put_unaligned(header->packet_id, &trailer->packet_id);
  388. #if 0
  389. netdev_dbg(dev->net, "frame >tx h %d p %d id %d\n",
  390. header->hdr_len, header->packet_len,
  391. header->packet_id);
  392. #endif
  393. return skb;
  394. }
  395. static int net1080_bind(struct usbnet *dev, struct usb_interface *intf)
  396. {
  397. unsigned extra = sizeof (struct nc_header)
  398. + 1
  399. + sizeof (struct nc_trailer);
  400. dev->net->hard_header_len += extra;
  401. dev->rx_urb_size = dev->net->hard_header_len + dev->net->mtu;
  402. dev->hard_mtu = NC_MAX_PACKET;
  403. return usbnet_get_endpoints (dev, intf);
  404. }
  405. static const struct driver_info net1080_info = {
  406. .description = "NetChip TurboCONNECT",
  407. .flags = FLAG_POINTTOPOINT | FLAG_FRAMING_NC,
  408. .bind = net1080_bind,
  409. .reset = net1080_reset,
  410. .check_connect = net1080_check_connect,
  411. .rx_fixup = net1080_rx_fixup,
  412. .tx_fixup = net1080_tx_fixup,
  413. };
  414. static const struct usb_device_id products [] = {
  415. {
  416. USB_DEVICE(0x0525, 0x1080), // NetChip ref design
  417. .driver_info = (unsigned long) &net1080_info,
  418. }, {
  419. USB_DEVICE(0x06D0, 0x0622), // Laplink Gold
  420. .driver_info = (unsigned long) &net1080_info,
  421. },
  422. { }, // END
  423. };
  424. MODULE_DEVICE_TABLE(usb, products);
  425. static struct usb_driver net1080_driver = {
  426. .name = "net1080",
  427. .id_table = products,
  428. .probe = usbnet_probe,
  429. .disconnect = usbnet_disconnect,
  430. .suspend = usbnet_suspend,
  431. .resume = usbnet_resume,
  432. .disable_hub_initiated_lpm = 1,
  433. };
  434. module_usb_driver(net1080_driver);
  435. MODULE_AUTHOR("David Brownell");
  436. MODULE_DESCRIPTION("NetChip 1080 based USB Host-to-Host Links");
  437. MODULE_LICENSE("GPL");