gl620a.c 6.5 KB

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