gl620a.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * GeneSys GL620USB-A based links
  3. * Copyright (C) 2001 by Jiun-Jie Huang <huangjj@genesyslogic.com.tw>
  4. * Copyright (C) 2001 by Stanislav Brabec <utx@penguin.cz>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. // #define DEBUG // error path messages, extra info
  21. // #define VERBOSE // more; success messages
  22. #include <linux/module.h>
  23. #include <linux/sched.h>
  24. #include <linux/init.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/ethtool.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/mii.h>
  30. #include <linux/usb.h>
  31. #include "usbnet.h"
  32. /*
  33. * GeneSys GL620USB-A (www.genesyslogic.com.tw)
  34. *
  35. * ... should partially interop with the Win32 driver for this hardware.
  36. * The GeneSys docs imply there's some NDIS issue motivating this framing.
  37. *
  38. * Some info from GeneSys:
  39. * - GL620USB-A is full duplex; GL620USB is only half duplex for bulk.
  40. * (Some cables, like the BAFO-100c, use the half duplex version.)
  41. * - For the full duplex model, the low bit of the version code says
  42. * which side is which ("left/right").
  43. * - For the half duplex type, a control/interrupt handshake settles
  44. * the transfer direction. (That's disabled here, partially coded.)
  45. * A control URB would block until other side writes an interrupt.
  46. *
  47. * Original code from Jiun-Jie Huang <huangjj@genesyslogic.com.tw>
  48. * and merged into "usbnet" by Stanislav Brabec <utx@penguin.cz>.
  49. */
  50. // control msg write command
  51. #define GENELINK_CONNECT_WRITE 0xF0
  52. // interrupt pipe index
  53. #define GENELINK_INTERRUPT_PIPE 0x03
  54. // interrupt read buffer size
  55. #define INTERRUPT_BUFSIZE 0x08
  56. // interrupt pipe interval value
  57. #define GENELINK_INTERRUPT_INTERVAL 0x10
  58. // max transmit packet number per transmit
  59. #define GL_MAX_TRANSMIT_PACKETS 32
  60. // max packet length
  61. #define GL_MAX_PACKET_LEN 1514
  62. // max receive buffer size
  63. #define GL_RCV_BUF_SIZE \
  64. (((GL_MAX_PACKET_LEN + 4) * GL_MAX_TRANSMIT_PACKETS) + 4)
  65. struct gl_packet {
  66. u32 packet_length;
  67. char packet_data [1];
  68. };
  69. struct gl_header {
  70. u32 packet_count;
  71. struct gl_packet packets;
  72. };
  73. #ifdef GENELINK_ACK
  74. // FIXME: this code is incomplete, not debugged; it doesn't
  75. // handle interrupts correctly; it should use the generic
  76. // status IRQ code (which didn't exist back in 2001).
  77. struct gl_priv {
  78. struct urb *irq_urb;
  79. char irq_buf [INTERRUPT_BUFSIZE];
  80. };
  81. static inline int gl_control_write(struct usbnet *dev, u8 request, u16 value)
  82. {
  83. int retval;
  84. retval = usb_control_msg(dev->udev,
  85. usb_sndctrlpipe(dev->udev, 0),
  86. request,
  87. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  88. value,
  89. 0, // index
  90. 0, // data buffer
  91. 0, // size
  92. USB_CTRL_SET_TIMEOUT);
  93. return retval;
  94. }
  95. static void gl_interrupt_complete(struct urb *urb, struct pt_regs *regs)
  96. {
  97. int status = urb->status;
  98. switch (status) {
  99. case 0:
  100. /* success */
  101. break;
  102. case -ECONNRESET:
  103. case -ENOENT:
  104. case -ESHUTDOWN:
  105. /* this urb is terminated, clean up */
  106. dbg("%s - urb shutting down with status: %d",
  107. __FUNCTION__, status);
  108. return;
  109. default:
  110. dbg("%s - nonzero urb status received: %d",
  111. __FUNCTION__, urb->status);
  112. }
  113. status = usb_submit_urb(urb, GFP_ATOMIC);
  114. if (status)
  115. err("%s - usb_submit_urb failed with result %d",
  116. __FUNCTION__, status);
  117. }
  118. static int gl_interrupt_read(struct usbnet *dev)
  119. {
  120. struct gl_priv *priv = dev->priv_data;
  121. int retval;
  122. // issue usb interrupt read
  123. if (priv && priv->irq_urb) {
  124. // submit urb
  125. if ((retval = usb_submit_urb(priv->irq_urb, GFP_KERNEL)) != 0)
  126. dbg("gl_interrupt_read: submit fail - %X...", retval);
  127. else
  128. dbg("gl_interrupt_read: submit success...");
  129. }
  130. return 0;
  131. }
  132. // check whether another side is connected
  133. static int genelink_check_connect(struct usbnet *dev)
  134. {
  135. int retval;
  136. dbg("genelink_check_connect...");
  137. // detect whether another side is connected
  138. if ((retval = gl_control_write(dev, GENELINK_CONNECT_WRITE, 0)) != 0) {
  139. dbg("%s: genelink_check_connect write fail - %X",
  140. dev->net->name, retval);
  141. return retval;
  142. }
  143. // usb interrupt read to ack another side
  144. if ((retval = gl_interrupt_read(dev)) != 0) {
  145. dbg("%s: genelink_check_connect read fail - %X",
  146. dev->net->name, retval);
  147. return retval;
  148. }
  149. dbg("%s: genelink_check_connect read success", dev->net->name);
  150. return 0;
  151. }
  152. // allocate and initialize the private data for genelink
  153. static int genelink_init(struct usbnet *dev)
  154. {
  155. struct gl_priv *priv;
  156. // allocate the private data structure
  157. if ((priv = kmalloc(sizeof *priv, GFP_KERNEL)) == 0) {
  158. dbg("%s: cannot allocate private data per device",
  159. dev->net->name);
  160. return -ENOMEM;
  161. }
  162. // allocate irq urb
  163. if ((priv->irq_urb = usb_alloc_urb(0, GFP_KERNEL)) == 0) {
  164. dbg("%s: cannot allocate private irq urb per device",
  165. dev->net->name);
  166. kfree(priv);
  167. return -ENOMEM;
  168. }
  169. // fill irq urb
  170. usb_fill_int_urb(priv->irq_urb, dev->udev,
  171. usb_rcvintpipe(dev->udev, GENELINK_INTERRUPT_PIPE),
  172. priv->irq_buf, INTERRUPT_BUFSIZE,
  173. gl_interrupt_complete, 0,
  174. GENELINK_INTERRUPT_INTERVAL);
  175. // set private data pointer
  176. dev->priv_data = priv;
  177. return 0;
  178. }
  179. // release the private data
  180. static int genelink_free(struct usbnet *dev)
  181. {
  182. struct gl_priv *priv = dev->priv_data;
  183. if (!priv)
  184. return 0;
  185. // FIXME: can't cancel here; it's synchronous, and
  186. // should have happened earlier in any case (interrupt
  187. // handling needs to be generic)
  188. // cancel irq urb first
  189. usb_kill_urb(priv->irq_urb);
  190. // free irq urb
  191. usb_free_urb(priv->irq_urb);
  192. // free the private data structure
  193. kfree(priv);
  194. return 0;
  195. }
  196. #endif
  197. static int genelink_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  198. {
  199. struct gl_header *header;
  200. struct gl_packet *packet;
  201. struct sk_buff *gl_skb;
  202. u32 size;
  203. header = (struct gl_header *) skb->data;
  204. // get the packet count of the received skb
  205. le32_to_cpus(&header->packet_count);
  206. if ((header->packet_count > GL_MAX_TRANSMIT_PACKETS)
  207. || (header->packet_count < 0)) {
  208. dbg("genelink: invalid received packet count %d",
  209. header->packet_count);
  210. return 0;
  211. }
  212. // set the current packet pointer to the first packet
  213. packet = &header->packets;
  214. // decrement the length for the packet count size 4 bytes
  215. skb_pull(skb, 4);
  216. while (header->packet_count > 1) {
  217. // get the packet length
  218. size = le32_to_cpu(packet->packet_length);
  219. // this may be a broken packet
  220. if (size > GL_MAX_PACKET_LEN) {
  221. dbg("genelink: invalid rx length %d", size);
  222. return 0;
  223. }
  224. // allocate the skb for the individual packet
  225. gl_skb = alloc_skb(size, GFP_ATOMIC);
  226. if (gl_skb) {
  227. // copy the packet data to the new skb
  228. memcpy(skb_put(gl_skb, size),
  229. packet->packet_data, size);
  230. usbnet_skb_return(dev, gl_skb);
  231. }
  232. // advance to the next packet
  233. packet = (struct gl_packet *)
  234. &packet->packet_data [size];
  235. header->packet_count--;
  236. // shift the data pointer to the next gl_packet
  237. skb_pull(skb, size + 4);
  238. }
  239. // skip the packet length field 4 bytes
  240. skb_pull(skb, 4);
  241. if (skb->len > GL_MAX_PACKET_LEN) {
  242. dbg("genelink: invalid rx length %d", skb->len);
  243. return 0;
  244. }
  245. return 1;
  246. }
  247. static struct sk_buff *
  248. genelink_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  249. {
  250. int padlen;
  251. int length = skb->len;
  252. int headroom = skb_headroom(skb);
  253. int tailroom = skb_tailroom(skb);
  254. u32 *packet_count;
  255. u32 *packet_len;
  256. // FIXME: magic numbers, bleech
  257. padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
  258. if ((!skb_cloned(skb))
  259. && ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
  260. if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
  261. skb->data = memmove(skb->head + (4 + 4*1),
  262. skb->data, skb->len);
  263. skb->tail = skb->data + skb->len;
  264. }
  265. } else {
  266. struct sk_buff *skb2;
  267. skb2 = skb_copy_expand(skb, (4 + 4*1) , padlen, flags);
  268. dev_kfree_skb_any(skb);
  269. skb = skb2;
  270. if (!skb)
  271. return NULL;
  272. }
  273. // attach the packet count to the header
  274. packet_count = (u32 *) skb_push(skb, (4 + 4*1));
  275. packet_len = packet_count + 1;
  276. *packet_count = cpu_to_le32(1);
  277. *packet_len = cpu_to_le32(length);
  278. // add padding byte
  279. if ((skb->len % dev->maxpacket) == 0)
  280. skb_put(skb, 1);
  281. return skb;
  282. }
  283. static int genelink_bind(struct usbnet *dev, struct usb_interface *intf)
  284. {
  285. dev->hard_mtu = GL_RCV_BUF_SIZE;
  286. dev->net->hard_header_len += 4;
  287. dev->in = usb_rcvbulkpipe(dev->udev, dev->driver_info->in);
  288. dev->out = usb_sndbulkpipe(dev->udev, dev->driver_info->out);
  289. return 0;
  290. }
  291. static const struct driver_info genelink_info = {
  292. .description = "Genesys GeneLink",
  293. .flags = FLAG_FRAMING_GL | FLAG_NO_SETINT,
  294. .bind = genelink_bind,
  295. .rx_fixup = genelink_rx_fixup,
  296. .tx_fixup = genelink_tx_fixup,
  297. .in = 1, .out = 2,
  298. #ifdef GENELINK_ACK
  299. .check_connect =genelink_check_connect,
  300. #endif
  301. };
  302. static const struct usb_device_id products [] = {
  303. {
  304. USB_DEVICE(0x05e3, 0x0502), // GL620USB-A
  305. .driver_info = (unsigned long) &genelink_info,
  306. },
  307. /* NOT: USB_DEVICE(0x05e3, 0x0501), // GL620USB
  308. * that's half duplex, not currently supported
  309. */
  310. { }, // END
  311. };
  312. MODULE_DEVICE_TABLE(usb, products);
  313. static struct usb_driver gl620a_driver = {
  314. .name = "gl620a",
  315. .id_table = products,
  316. .probe = usbnet_probe,
  317. .disconnect = usbnet_disconnect,
  318. .suspend = usbnet_suspend,
  319. .resume = usbnet_resume,
  320. };
  321. static int __init usbnet_init(void)
  322. {
  323. return usb_register(&gl620a_driver);
  324. }
  325. module_init(usbnet_init);
  326. static void __exit usbnet_exit(void)
  327. {
  328. usb_deregister(&gl620a_driver);
  329. }
  330. module_exit(usbnet_exit);
  331. MODULE_AUTHOR("Jiun-Jie Huang");
  332. MODULE_DESCRIPTION("GL620-USB-A Host-to-Host Link cables");
  333. MODULE_LICENSE("GPL");