gl620a.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. /*
  32. * GeneSys GL620USB-A (www.genesyslogic.com.tw)
  33. *
  34. * ... should partially interop with the Win32 driver for this hardware.
  35. * The GeneSys docs imply there's some NDIS issue motivating this framing.
  36. *
  37. * Some info from GeneSys:
  38. * - GL620USB-A is full duplex; GL620USB is only half duplex for bulk.
  39. * (Some cables, like the BAFO-100c, use the half duplex version.)
  40. * - For the full duplex model, the low bit of the version code says
  41. * which side is which ("left/right").
  42. * - For the half duplex type, a control/interrupt handshake settles
  43. * the transfer direction. (That's disabled here, partially coded.)
  44. * A control URB would block until other side writes an interrupt.
  45. *
  46. * Original code from Jiun-Jie Huang <huangjj@genesyslogic.com.tw>
  47. * and merged into "usbnet" by Stanislav Brabec <utx@penguin.cz>.
  48. */
  49. // control msg write command
  50. #define GENELINK_CONNECT_WRITE 0xF0
  51. // interrupt pipe index
  52. #define GENELINK_INTERRUPT_PIPE 0x03
  53. // interrupt read buffer size
  54. #define INTERRUPT_BUFSIZE 0x08
  55. // interrupt pipe interval value
  56. #define GENELINK_INTERRUPT_INTERVAL 0x10
  57. // max transmit packet number per transmit
  58. #define GL_MAX_TRANSMIT_PACKETS 32
  59. // max packet length
  60. #define GL_MAX_PACKET_LEN 1514
  61. // max receive buffer size
  62. #define GL_RCV_BUF_SIZE \
  63. (((GL_MAX_PACKET_LEN + 4) * GL_MAX_TRANSMIT_PACKETS) + 4)
  64. struct gl_packet {
  65. __le32 packet_length;
  66. char packet_data [1];
  67. };
  68. struct gl_header {
  69. __le32 packet_count;
  70. struct gl_packet packets;
  71. };
  72. static int genelink_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  73. {
  74. struct gl_header *header;
  75. struct gl_packet *packet;
  76. struct sk_buff *gl_skb;
  77. u32 size;
  78. u32 count;
  79. header = (struct gl_header *) skb->data;
  80. // get the packet count of the received skb
  81. count = le32_to_cpu(header->packet_count);
  82. if (count > GL_MAX_TRANSMIT_PACKETS) {
  83. dbg("genelink: invalid received packet count %u", count);
  84. return 0;
  85. }
  86. // set the current packet pointer to the first packet
  87. packet = &header->packets;
  88. // decrement the length for the packet count size 4 bytes
  89. skb_pull(skb, 4);
  90. while (count > 1) {
  91. // get the packet length
  92. size = le32_to_cpu(packet->packet_length);
  93. // this may be a broken packet
  94. if (size > GL_MAX_PACKET_LEN) {
  95. dbg("genelink: invalid rx length %d", size);
  96. return 0;
  97. }
  98. // allocate the skb for the individual packet
  99. gl_skb = alloc_skb(size, GFP_ATOMIC);
  100. if (gl_skb) {
  101. // copy the packet data to the new skb
  102. memcpy(skb_put(gl_skb, size),
  103. packet->packet_data, size);
  104. usbnet_skb_return(dev, gl_skb);
  105. }
  106. // advance to the next packet
  107. packet = (struct gl_packet *)&packet->packet_data[size];
  108. count--;
  109. // shift the data pointer to the next gl_packet
  110. skb_pull(skb, size + 4);
  111. }
  112. // skip the packet length field 4 bytes
  113. skb_pull(skb, 4);
  114. if (skb->len > GL_MAX_PACKET_LEN) {
  115. dbg("genelink: invalid rx length %d", skb->len);
  116. return 0;
  117. }
  118. return 1;
  119. }
  120. static struct sk_buff *
  121. genelink_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  122. {
  123. int padlen;
  124. int length = skb->len;
  125. int headroom = skb_headroom(skb);
  126. int tailroom = skb_tailroom(skb);
  127. __le32 *packet_count;
  128. __le32 *packet_len;
  129. // FIXME: magic numbers, bleech
  130. padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
  131. if ((!skb_cloned(skb))
  132. && ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
  133. if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
  134. skb->data = memmove(skb->head + (4 + 4*1),
  135. skb->data, skb->len);
  136. skb_set_tail_pointer(skb, skb->len);
  137. }
  138. } else {
  139. struct sk_buff *skb2;
  140. skb2 = skb_copy_expand(skb, (4 + 4*1) , padlen, flags);
  141. dev_kfree_skb_any(skb);
  142. skb = skb2;
  143. if (!skb)
  144. return NULL;
  145. }
  146. // attach the packet count to the header
  147. packet_count = (__le32 *) skb_push(skb, (4 + 4*1));
  148. packet_len = packet_count + 1;
  149. *packet_count = cpu_to_le32(1);
  150. *packet_len = cpu_to_le32(length);
  151. // add padding byte
  152. if ((skb->len % dev->maxpacket) == 0)
  153. skb_put(skb, 1);
  154. return skb;
  155. }
  156. static int genelink_bind(struct usbnet *dev, struct usb_interface *intf)
  157. {
  158. dev->hard_mtu = GL_RCV_BUF_SIZE;
  159. dev->net->hard_header_len += 4;
  160. dev->in = usb_rcvbulkpipe(dev->udev, dev->driver_info->in);
  161. dev->out = usb_sndbulkpipe(dev->udev, dev->driver_info->out);
  162. return 0;
  163. }
  164. static const struct driver_info genelink_info = {
  165. .description = "Genesys GeneLink",
  166. .flags = FLAG_FRAMING_GL | FLAG_NO_SETINT,
  167. .bind = genelink_bind,
  168. .rx_fixup = genelink_rx_fixup,
  169. .tx_fixup = genelink_tx_fixup,
  170. .in = 1, .out = 2,
  171. #ifdef GENELINK_ACK
  172. .check_connect =genelink_check_connect,
  173. #endif
  174. };
  175. static const struct usb_device_id products [] = {
  176. {
  177. USB_DEVICE(0x05e3, 0x0502), // GL620USB-A
  178. .driver_info = (unsigned long) &genelink_info,
  179. },
  180. /* NOT: USB_DEVICE(0x05e3, 0x0501), // GL620USB
  181. * that's half duplex, not currently supported
  182. */
  183. { }, // END
  184. };
  185. MODULE_DEVICE_TABLE(usb, products);
  186. static struct usb_driver gl620a_driver = {
  187. .name = "gl620a",
  188. .id_table = products,
  189. .probe = usbnet_probe,
  190. .disconnect = usbnet_disconnect,
  191. .suspend = usbnet_suspend,
  192. .resume = usbnet_resume,
  193. };
  194. static int __init usbnet_init(void)
  195. {
  196. return usb_register(&gl620a_driver);
  197. }
  198. module_init(usbnet_init);
  199. static void __exit usbnet_exit(void)
  200. {
  201. usb_deregister(&gl620a_driver);
  202. }
  203. module_exit(usbnet_exit);
  204. MODULE_AUTHOR("Jiun-Jie Huang");
  205. MODULE_DESCRIPTION("GL620-USB-A Host-to-Host Link cables");
  206. MODULE_LICENSE("GPL");