gl620a.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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/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 <linux/usb/usbnet.h>
  31. #include <linux/gfp.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. __le32 packet_length;
  67. char packet_data [1];
  68. };
  69. struct gl_header {
  70. __le32 packet_count;
  71. struct gl_packet packets;
  72. };
  73. static int genelink_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  74. {
  75. struct gl_header *header;
  76. struct gl_packet *packet;
  77. struct sk_buff *gl_skb;
  78. u32 size;
  79. u32 count;
  80. header = (struct gl_header *) skb->data;
  81. // get the packet count of the received skb
  82. count = le32_to_cpu(header->packet_count);
  83. if (count > GL_MAX_TRANSMIT_PACKETS) {
  84. dbg("genelink: invalid received packet count %u", count);
  85. return 0;
  86. }
  87. // set the current packet pointer to the first packet
  88. packet = &header->packets;
  89. // decrement the length for the packet count size 4 bytes
  90. skb_pull(skb, 4);
  91. while (count > 1) {
  92. // get the packet length
  93. size = le32_to_cpu(packet->packet_length);
  94. // this may be a broken packet
  95. if (size > GL_MAX_PACKET_LEN) {
  96. dbg("genelink: invalid rx length %d", size);
  97. return 0;
  98. }
  99. // allocate the skb for the individual packet
  100. gl_skb = alloc_skb(size, GFP_ATOMIC);
  101. if (gl_skb) {
  102. // copy the packet data to the new skb
  103. memcpy(skb_put(gl_skb, size),
  104. packet->packet_data, size);
  105. usbnet_skb_return(dev, gl_skb);
  106. }
  107. // advance to the next packet
  108. packet = (struct gl_packet *)&packet->packet_data[size];
  109. count--;
  110. // shift the data pointer to the next gl_packet
  111. skb_pull(skb, size + 4);
  112. }
  113. // skip the packet length field 4 bytes
  114. skb_pull(skb, 4);
  115. if (skb->len > GL_MAX_PACKET_LEN) {
  116. dbg("genelink: invalid rx length %d", skb->len);
  117. return 0;
  118. }
  119. return 1;
  120. }
  121. static struct sk_buff *
  122. genelink_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  123. {
  124. int padlen;
  125. int length = skb->len;
  126. int headroom = skb_headroom(skb);
  127. int tailroom = skb_tailroom(skb);
  128. __le32 *packet_count;
  129. __le32 *packet_len;
  130. // FIXME: magic numbers, bleech
  131. padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
  132. if ((!skb_cloned(skb))
  133. && ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
  134. if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
  135. skb->data = memmove(skb->head + (4 + 4*1),
  136. skb->data, skb->len);
  137. skb_set_tail_pointer(skb, skb->len);
  138. }
  139. } else {
  140. struct sk_buff *skb2;
  141. skb2 = skb_copy_expand(skb, (4 + 4*1) , padlen, flags);
  142. dev_kfree_skb_any(skb);
  143. skb = skb2;
  144. if (!skb)
  145. return NULL;
  146. }
  147. // attach the packet count to the header
  148. packet_count = (__le32 *) skb_push(skb, (4 + 4*1));
  149. packet_len = packet_count + 1;
  150. *packet_count = cpu_to_le32(1);
  151. *packet_len = cpu_to_le32(length);
  152. // add padding byte
  153. if ((skb->len % dev->maxpacket) == 0)
  154. skb_put(skb, 1);
  155. return skb;
  156. }
  157. static int genelink_bind(struct usbnet *dev, struct usb_interface *intf)
  158. {
  159. dev->hard_mtu = GL_RCV_BUF_SIZE;
  160. dev->net->hard_header_len += 4;
  161. dev->in = usb_rcvbulkpipe(dev->udev, dev->driver_info->in);
  162. dev->out = usb_sndbulkpipe(dev->udev, dev->driver_info->out);
  163. return 0;
  164. }
  165. static const struct driver_info genelink_info = {
  166. .description = "Genesys GeneLink",
  167. .flags = FLAG_POINTTOPOINT | FLAG_FRAMING_GL | FLAG_NO_SETINT,
  168. .bind = genelink_bind,
  169. .rx_fixup = genelink_rx_fixup,
  170. .tx_fixup = genelink_tx_fixup,
  171. .in = 1, .out = 2,
  172. #ifdef GENELINK_ACK
  173. .check_connect =genelink_check_connect,
  174. #endif
  175. };
  176. static const struct usb_device_id products [] = {
  177. {
  178. USB_DEVICE(0x05e3, 0x0502), // GL620USB-A
  179. .driver_info = (unsigned long) &genelink_info,
  180. },
  181. /* NOT: USB_DEVICE(0x05e3, 0x0501), // GL620USB
  182. * that's half duplex, not currently supported
  183. */
  184. { }, // END
  185. };
  186. MODULE_DEVICE_TABLE(usb, products);
  187. static struct usb_driver gl620a_driver = {
  188. .name = "gl620a",
  189. .id_table = products,
  190. .probe = usbnet_probe,
  191. .disconnect = usbnet_disconnect,
  192. .suspend = usbnet_suspend,
  193. .resume = usbnet_resume,
  194. };
  195. static int __init usbnet_init(void)
  196. {
  197. return usb_register(&gl620a_driver);
  198. }
  199. module_init(usbnet_init);
  200. static void __exit usbnet_exit(void)
  201. {
  202. usb_deregister(&gl620a_driver);
  203. }
  204. module_exit(usbnet_exit);
  205. MODULE_AUTHOR("Jiun-Jie Huang");
  206. MODULE_DESCRIPTION("GL620-USB-A Host-to-Host Link cables");
  207. MODULE_LICENSE("GPL");