gl620a.c 10.0 KB

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