gl620a.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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, see <http://www.gnu.org/licenses/>.
  18. */
  19. // #define DEBUG // error path messages, extra info
  20. // #define VERBOSE // more; success messages
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/ethtool.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/mii.h>
  28. #include <linux/usb.h>
  29. #include <linux/usb/usbnet.h>
  30. #include <linux/gfp.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. netdev_dbg(dev->net,
  84. "genelink: invalid received packet count %u\n",
  85. count);
  86. return 0;
  87. }
  88. // set the current packet pointer to the first packet
  89. packet = &header->packets;
  90. // decrement the length for the packet count size 4 bytes
  91. skb_pull(skb, 4);
  92. while (count > 1) {
  93. // get the packet length
  94. size = le32_to_cpu(packet->packet_length);
  95. // this may be a broken packet
  96. if (size > GL_MAX_PACKET_LEN) {
  97. netdev_dbg(dev->net, "genelink: invalid rx length %d\n",
  98. size);
  99. return 0;
  100. }
  101. // allocate the skb for the individual packet
  102. gl_skb = alloc_skb(size, GFP_ATOMIC);
  103. if (gl_skb) {
  104. // copy the packet data to the new skb
  105. memcpy(skb_put(gl_skb, size),
  106. packet->packet_data, size);
  107. usbnet_skb_return(dev, gl_skb);
  108. }
  109. // advance to the next packet
  110. packet = (struct gl_packet *)&packet->packet_data[size];
  111. count--;
  112. // shift the data pointer to the next gl_packet
  113. skb_pull(skb, size + 4);
  114. }
  115. // skip the packet length field 4 bytes
  116. skb_pull(skb, 4);
  117. if (skb->len > GL_MAX_PACKET_LEN) {
  118. netdev_dbg(dev->net, "genelink: invalid rx length %d\n",
  119. skb->len);
  120. return 0;
  121. }
  122. return 1;
  123. }
  124. static struct sk_buff *
  125. genelink_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  126. {
  127. int padlen;
  128. int length = skb->len;
  129. int headroom = skb_headroom(skb);
  130. int tailroom = skb_tailroom(skb);
  131. __le32 *packet_count;
  132. __le32 *packet_len;
  133. // FIXME: magic numbers, bleech
  134. padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
  135. if ((!skb_cloned(skb))
  136. && ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
  137. if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
  138. skb->data = memmove(skb->head + (4 + 4*1),
  139. skb->data, skb->len);
  140. skb_set_tail_pointer(skb, skb->len);
  141. }
  142. } else {
  143. struct sk_buff *skb2;
  144. skb2 = skb_copy_expand(skb, (4 + 4*1) , padlen, flags);
  145. dev_kfree_skb_any(skb);
  146. skb = skb2;
  147. if (!skb)
  148. return NULL;
  149. }
  150. // attach the packet count to the header
  151. packet_count = (__le32 *) skb_push(skb, (4 + 4*1));
  152. packet_len = packet_count + 1;
  153. *packet_count = cpu_to_le32(1);
  154. *packet_len = cpu_to_le32(length);
  155. // add padding byte
  156. if ((skb->len % dev->maxpacket) == 0)
  157. skb_put(skb, 1);
  158. return skb;
  159. }
  160. static int genelink_bind(struct usbnet *dev, struct usb_interface *intf)
  161. {
  162. dev->hard_mtu = GL_RCV_BUF_SIZE;
  163. dev->net->hard_header_len += 4;
  164. dev->in = usb_rcvbulkpipe(dev->udev, dev->driver_info->in);
  165. dev->out = usb_sndbulkpipe(dev->udev, dev->driver_info->out);
  166. return 0;
  167. }
  168. static const struct driver_info genelink_info = {
  169. .description = "Genesys GeneLink",
  170. .flags = FLAG_POINTTOPOINT | FLAG_FRAMING_GL | FLAG_NO_SETINT,
  171. .bind = genelink_bind,
  172. .rx_fixup = genelink_rx_fixup,
  173. .tx_fixup = genelink_tx_fixup,
  174. .in = 1, .out = 2,
  175. #ifdef GENELINK_ACK
  176. .check_connect =genelink_check_connect,
  177. #endif
  178. };
  179. static const struct usb_device_id products [] = {
  180. {
  181. USB_DEVICE(0x05e3, 0x0502), // GL620USB-A
  182. .driver_info = (unsigned long) &genelink_info,
  183. },
  184. /* NOT: USB_DEVICE(0x05e3, 0x0501), // GL620USB
  185. * that's half duplex, not currently supported
  186. */
  187. { }, // END
  188. };
  189. MODULE_DEVICE_TABLE(usb, products);
  190. static struct usb_driver gl620a_driver = {
  191. .name = "gl620a",
  192. .id_table = products,
  193. .probe = usbnet_probe,
  194. .disconnect = usbnet_disconnect,
  195. .suspend = usbnet_suspend,
  196. .resume = usbnet_resume,
  197. .disable_hub_initiated_lpm = 1,
  198. };
  199. module_usb_driver(gl620a_driver);
  200. MODULE_AUTHOR("Jiun-Jie Huang");
  201. MODULE_DESCRIPTION("GL620-USB-A Host-to-Host Link cables");
  202. MODULE_LICENSE("GPL");