net1080.c 15 KB

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