net1080.c 15 KB

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