net1080.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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/sched.h>
  23. #include <linux/init.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/ethtool.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/mii.h>
  29. #include <linux/usb.h>
  30. #include <asm/unaligned.h>
  31. #include "usbnet.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. } __attribute__((__packed__));
  59. #define PAD_BYTE ((unsigned char)0xAC)
  60. struct nc_trailer {
  61. __le16 packet_id;
  62. } __attribute__((__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. if (!netif_msg_link(dev))
  175. return;
  176. devdbg(dev, "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s;"
  177. " this%s%s;"
  178. " other%s%s; r/o 0x%x",
  179. dev->udev->bus->bus_name, dev->udev->devpath,
  180. usbctl,
  181. (usbctl & USBCTL_ENABLE_LANG) ? " lang" : "",
  182. (usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "",
  183. (usbctl & USBCTL_ENABLE_PROD) ? " prod" : "",
  184. (usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "",
  185. (usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "",
  186. (usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "",
  187. (usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "",
  188. (usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "",
  189. (usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "",
  190. usbctl & ~USBCTL_WRITABLE_MASK
  191. );
  192. }
  193. /*-------------------------------------------------------------------------*/
  194. /*
  195. * Status register
  196. */
  197. #define STATUS_PORT_A (1 << 15)
  198. #define STATUS_CONN_OTHER (1 << 14)
  199. #define STATUS_SUSPEND_OTHER (1 << 13)
  200. #define STATUS_MAILBOX_OTHER (1 << 12)
  201. #define STATUS_PACKETS_OTHER(n) (((n) >> 8) && 0x03)
  202. #define STATUS_CONN_THIS (1 << 6)
  203. #define STATUS_SUSPEND_THIS (1 << 5)
  204. #define STATUS_MAILBOX_THIS (1 << 4)
  205. #define STATUS_PACKETS_THIS(n) (((n) >> 0) && 0x03)
  206. #define STATUS_UNSPEC_MASK 0x0c8c
  207. #define STATUS_NOISE_MASK ((u16)~(0x0303|STATUS_UNSPEC_MASK))
  208. static inline void nc_dump_status(struct usbnet *dev, u16 status)
  209. {
  210. if (!netif_msg_link(dev))
  211. return;
  212. devdbg(dev, "net1080 %s-%s status 0x%x:"
  213. " this (%c) PKT=%d%s%s%s;"
  214. " other PKT=%d%s%s%s; unspec 0x%x",
  215. dev->udev->bus->bus_name, dev->udev->devpath,
  216. status,
  217. // XXX the packet counts don't seem right
  218. // (1 at reset, not 0); maybe UNSPEC too
  219. (status & STATUS_PORT_A) ? 'A' : 'B',
  220. STATUS_PACKETS_THIS(status),
  221. (status & STATUS_CONN_THIS) ? " CON" : "",
  222. (status & STATUS_SUSPEND_THIS) ? " SUS" : "",
  223. (status & STATUS_MAILBOX_THIS) ? " MBOX" : "",
  224. STATUS_PACKETS_OTHER(status),
  225. (status & STATUS_CONN_OTHER) ? " CON" : "",
  226. (status & STATUS_SUSPEND_OTHER) ? " SUS" : "",
  227. (status & STATUS_MAILBOX_OTHER) ? " MBOX" : "",
  228. status & STATUS_UNSPEC_MASK
  229. );
  230. }
  231. /*-------------------------------------------------------------------------*/
  232. /*
  233. * TTL register
  234. */
  235. #define TTL_THIS(ttl) (0x00ff & ttl)
  236. #define TTL_OTHER(ttl) (0x00ff & (ttl >> 8))
  237. #define MK_TTL(this,other) ((u16)(((other)<<8)|(0x00ff&(this))))
  238. static inline void nc_dump_ttl(struct usbnet *dev, u16 ttl)
  239. {
  240. if (netif_msg_link(dev))
  241. devdbg(dev, "net1080 %s-%s ttl 0x%x this = %d, other = %d",
  242. dev->udev->bus->bus_name, dev->udev->devpath,
  243. ttl, TTL_THIS(ttl), TTL_OTHER(ttl));
  244. }
  245. /*-------------------------------------------------------------------------*/
  246. static int net1080_reset(struct usbnet *dev)
  247. {
  248. u16 usbctl, status, ttl;
  249. u16 *vp = kmalloc(sizeof (u16), GFP_KERNEL);
  250. int retval;
  251. if (!vp)
  252. return -ENOMEM;
  253. // nc_dump_registers(dev);
  254. if ((retval = nc_register_read(dev, REG_STATUS, vp)) < 0) {
  255. dbg("can't read %s-%s status: %d",
  256. dev->udev->bus->bus_name, dev->udev->devpath, retval);
  257. goto done;
  258. }
  259. status = *vp;
  260. nc_dump_status(dev, status);
  261. if ((retval = nc_register_read(dev, REG_USBCTL, vp)) < 0) {
  262. dbg("can't read USBCTL, %d", retval);
  263. goto done;
  264. }
  265. usbctl = *vp;
  266. nc_dump_usbctl(dev, usbctl);
  267. nc_register_write(dev, REG_USBCTL,
  268. USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER);
  269. if ((retval = nc_register_read(dev, REG_TTL, vp)) < 0) {
  270. dbg("can't read TTL, %d", retval);
  271. goto done;
  272. }
  273. ttl = *vp;
  274. // nc_dump_ttl(dev, ttl);
  275. nc_register_write(dev, REG_TTL,
  276. MK_TTL(NC_READ_TTL_MS, TTL_OTHER(ttl)) );
  277. dbg("%s: assigned TTL, %d ms", dev->net->name, NC_READ_TTL_MS);
  278. if (netif_msg_link(dev))
  279. devinfo(dev, "port %c, peer %sconnected",
  280. (status & STATUS_PORT_A) ? 'A' : 'B',
  281. (status & STATUS_CONN_OTHER) ? "" : "dis"
  282. );
  283. retval = 0;
  284. done:
  285. kfree(vp);
  286. return retval;
  287. }
  288. static int net1080_check_connect(struct usbnet *dev)
  289. {
  290. int retval;
  291. u16 status;
  292. u16 *vp = kmalloc(sizeof (u16), GFP_KERNEL);
  293. if (!vp)
  294. return -ENOMEM;
  295. retval = nc_register_read(dev, REG_STATUS, vp);
  296. status = *vp;
  297. kfree(vp);
  298. if (retval != 0) {
  299. dbg("%s net1080_check_conn read - %d", dev->net->name, retval);
  300. return retval;
  301. }
  302. if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER)
  303. return -ENOLINK;
  304. return 0;
  305. }
  306. static void nc_flush_complete(struct urb *urb, struct pt_regs *regs)
  307. {
  308. kfree(urb->context);
  309. usb_free_urb(urb);
  310. }
  311. static void nc_ensure_sync(struct usbnet *dev)
  312. {
  313. dev->frame_errors++;
  314. if (dev->frame_errors > 5) {
  315. struct urb *urb;
  316. struct usb_ctrlrequest *req;
  317. int status;
  318. /* Send a flush */
  319. urb = usb_alloc_urb(0, SLAB_ATOMIC);
  320. if (!urb)
  321. return;
  322. req = kmalloc(sizeof *req, GFP_ATOMIC);
  323. if (!req) {
  324. usb_free_urb(urb);
  325. return;
  326. }
  327. req->bRequestType = USB_DIR_OUT
  328. | USB_TYPE_VENDOR
  329. | USB_RECIP_DEVICE;
  330. req->bRequest = REQUEST_REGISTER;
  331. req->wValue = cpu_to_le16(USBCTL_FLUSH_THIS
  332. | USBCTL_FLUSH_OTHER);
  333. req->wIndex = cpu_to_le16(REG_USBCTL);
  334. req->wLength = cpu_to_le16(0);
  335. /* queue an async control request, we don't need
  336. * to do anything when it finishes except clean up.
  337. */
  338. usb_fill_control_urb(urb, dev->udev,
  339. usb_sndctrlpipe(dev->udev, 0),
  340. (unsigned char *) req,
  341. NULL, 0,
  342. nc_flush_complete, req);
  343. status = usb_submit_urb(urb, GFP_ATOMIC);
  344. if (status) {
  345. kfree(req);
  346. usb_free_urb(urb);
  347. return;
  348. }
  349. if (netif_msg_rx_err(dev))
  350. devdbg(dev, "flush net1080; too many framing errors");
  351. dev->frame_errors = 0;
  352. }
  353. }
  354. static int net1080_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  355. {
  356. struct nc_header *header;
  357. struct nc_trailer *trailer;
  358. u16 hdr_len, packet_len;
  359. if (!(skb->len & 0x01)) {
  360. #ifdef DEBUG
  361. struct net_device *net = dev->net;
  362. dbg("rx framesize %d range %d..%d mtu %d", skb->len,
  363. net->hard_header_len, dev->hard_mtu, net->mtu);
  364. #endif
  365. dev->stats.rx_frame_errors++;
  366. nc_ensure_sync(dev);
  367. return 0;
  368. }
  369. header = (struct nc_header *) skb->data;
  370. hdr_len = le16_to_cpup(&header->hdr_len);
  371. packet_len = le16_to_cpup(&header->packet_len);
  372. if (FRAMED_SIZE(packet_len) > NC_MAX_PACKET) {
  373. dev->stats.rx_frame_errors++;
  374. dbg("packet too big, %d", packet_len);
  375. nc_ensure_sync(dev);
  376. return 0;
  377. } else if (hdr_len < MIN_HEADER) {
  378. dev->stats.rx_frame_errors++;
  379. dbg("header too short, %d", hdr_len);
  380. nc_ensure_sync(dev);
  381. return 0;
  382. } else if (hdr_len > MIN_HEADER) {
  383. // out of band data for us?
  384. dbg("header OOB, %d bytes", hdr_len - MIN_HEADER);
  385. nc_ensure_sync(dev);
  386. // switch (vendor/product ids) { ... }
  387. }
  388. skb_pull(skb, hdr_len);
  389. trailer = (struct nc_trailer *)
  390. (skb->data + skb->len - sizeof *trailer);
  391. skb_trim(skb, skb->len - sizeof *trailer);
  392. if ((packet_len & 0x01) == 0) {
  393. if (skb->data [packet_len] != PAD_BYTE) {
  394. dev->stats.rx_frame_errors++;
  395. dbg("bad pad");
  396. return 0;
  397. }
  398. skb_trim(skb, skb->len - 1);
  399. }
  400. if (skb->len != packet_len) {
  401. dev->stats.rx_frame_errors++;
  402. dbg("bad packet len %d (expected %d)",
  403. skb->len, packet_len);
  404. nc_ensure_sync(dev);
  405. return 0;
  406. }
  407. if (header->packet_id != get_unaligned(&trailer->packet_id)) {
  408. dev->stats.rx_fifo_errors++;
  409. dbg("(2+ dropped) rx packet_id mismatch 0x%x 0x%x",
  410. le16_to_cpu(header->packet_id),
  411. le16_to_cpu(trailer->packet_id));
  412. return 0;
  413. }
  414. #if 0
  415. devdbg(dev, "frame <rx h %d p %d id %d", header->hdr_len,
  416. header->packet_len, header->packet_id);
  417. #endif
  418. dev->frame_errors = 0;
  419. return 1;
  420. }
  421. static struct sk_buff *
  422. net1080_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  423. {
  424. struct sk_buff *skb2;
  425. struct nc_header *header = NULL;
  426. struct nc_trailer *trailer = NULL;
  427. int padlen = sizeof (struct nc_trailer);
  428. int len = skb->len;
  429. if (!((len + padlen + sizeof (struct nc_header)) & 0x01))
  430. padlen++;
  431. if (!skb_cloned(skb)) {
  432. int headroom = skb_headroom(skb);
  433. int tailroom = skb_tailroom(skb);
  434. if (padlen <= tailroom &&
  435. sizeof(struct nc_header) <= headroom)
  436. /* There's enough head and tail room */
  437. goto encapsulate;
  438. if ((sizeof (struct nc_header) + padlen) <
  439. (headroom + tailroom)) {
  440. /* There's enough total room, so just readjust */
  441. skb->data = memmove(skb->head
  442. + sizeof (struct nc_header),
  443. skb->data, skb->len);
  444. skb->tail = skb->data + len;
  445. goto encapsulate;
  446. }
  447. }
  448. /* Create a new skb to use with the correct size */
  449. skb2 = skb_copy_expand(skb,
  450. sizeof (struct nc_header),
  451. padlen,
  452. flags);
  453. dev_kfree_skb_any(skb);
  454. if (!skb2)
  455. return skb2;
  456. skb = skb2;
  457. encapsulate:
  458. /* header first */
  459. header = (struct nc_header *) skb_push(skb, sizeof *header);
  460. header->hdr_len = cpu_to_le16(sizeof (*header));
  461. header->packet_len = cpu_to_le16(len);
  462. header->packet_id = cpu_to_le16((u16)dev->xid++);
  463. /* maybe pad; then trailer */
  464. if (!((skb->len + sizeof *trailer) & 0x01))
  465. *skb_put(skb, 1) = PAD_BYTE;
  466. trailer = (struct nc_trailer *) skb_put(skb, sizeof *trailer);
  467. put_unaligned(header->packet_id, &trailer->packet_id);
  468. #if 0
  469. devdbg(dev, "frame >tx h %d p %d id %d",
  470. header->hdr_len, header->packet_len,
  471. header->packet_id);
  472. #endif
  473. return skb;
  474. }
  475. static int net1080_bind(struct usbnet *dev, struct usb_interface *intf)
  476. {
  477. unsigned extra = sizeof (struct nc_header)
  478. + 1
  479. + sizeof (struct nc_trailer);
  480. dev->net->hard_header_len += extra;
  481. dev->rx_urb_size = dev->net->hard_header_len + dev->net->mtu;
  482. dev->hard_mtu = NC_MAX_PACKET;
  483. return usbnet_get_endpoints (dev, intf);
  484. }
  485. static const struct driver_info net1080_info = {
  486. .description = "NetChip TurboCONNECT",
  487. .flags = FLAG_FRAMING_NC,
  488. .bind = net1080_bind,
  489. .reset = net1080_reset,
  490. .check_connect = net1080_check_connect,
  491. .rx_fixup = net1080_rx_fixup,
  492. .tx_fixup = net1080_tx_fixup,
  493. };
  494. static const struct usb_device_id products [] = {
  495. {
  496. USB_DEVICE(0x0525, 0x1080), // NetChip ref design
  497. .driver_info = (unsigned long) &net1080_info,
  498. }, {
  499. USB_DEVICE(0x06D0, 0x0622), // Laplink Gold
  500. .driver_info = (unsigned long) &net1080_info,
  501. },
  502. { }, // END
  503. };
  504. MODULE_DEVICE_TABLE(usb, products);
  505. static struct usb_driver net1080_driver = {
  506. .name = "net1080",
  507. .id_table = products,
  508. .probe = usbnet_probe,
  509. .disconnect = usbnet_disconnect,
  510. .suspend = usbnet_suspend,
  511. .resume = usbnet_resume,
  512. };
  513. static int __init net1080_init(void)
  514. {
  515. return usb_register(&net1080_driver);
  516. }
  517. module_init(net1080_init);
  518. static void __exit net1080_exit(void)
  519. {
  520. usb_deregister(&net1080_driver);
  521. }
  522. module_exit(net1080_exit);
  523. MODULE_AUTHOR("David Brownell");
  524. MODULE_DESCRIPTION("NetChip 1080 based USB Host-to-Host Links");
  525. MODULE_LICENSE("GPL");