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. skb_put_data(gl_skb, packet->packet_data, size);
  108. usbnet_skb_return(dev, gl_skb);
  109. }
  110. // advance to the next packet
  111. packet = (struct gl_packet *)&packet->packet_data[size];
  112. count--;
  113. // shift the data pointer to the next gl_packet
  114. skb_pull(skb, size + 4);
  115. }
  116. // skip the packet length field 4 bytes
  117. skb_pull(skb, 4);
  118. if (skb->len > GL_MAX_PACKET_LEN) {
  119. netdev_dbg(dev->net, "genelink: invalid rx length %d\n",
  120. skb->len);
  121. return 0;
  122. }
  123. return 1;
  124. }
  125. static struct sk_buff *
  126. genelink_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  127. {
  128. int padlen;
  129. int length = skb->len;
  130. int headroom = skb_headroom(skb);
  131. int tailroom = skb_tailroom(skb);
  132. __le32 *packet_count;
  133. __le32 *packet_len;
  134. // FIXME: magic numbers, bleech
  135. padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
  136. if ((!skb_cloned(skb))
  137. && ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
  138. if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
  139. skb->data = memmove(skb->head + (4 + 4*1),
  140. skb->data, skb->len);
  141. skb_set_tail_pointer(skb, skb->len);
  142. }
  143. } else {
  144. struct sk_buff *skb2;
  145. skb2 = skb_copy_expand(skb, (4 + 4*1) , padlen, flags);
  146. dev_kfree_skb_any(skb);
  147. skb = skb2;
  148. if (!skb)
  149. return NULL;
  150. }
  151. // attach the packet count to the header
  152. packet_count = skb_push(skb, (4 + 4 * 1));
  153. packet_len = packet_count + 1;
  154. *packet_count = cpu_to_le32(1);
  155. *packet_len = cpu_to_le32(length);
  156. // add padding byte
  157. if ((skb->len % dev->maxpacket) == 0)
  158. skb_put(skb, 1);
  159. return skb;
  160. }
  161. static int genelink_bind(struct usbnet *dev, struct usb_interface *intf)
  162. {
  163. dev->hard_mtu = GL_RCV_BUF_SIZE;
  164. dev->net->hard_header_len += 4;
  165. dev->in = usb_rcvbulkpipe(dev->udev, dev->driver_info->in);
  166. dev->out = usb_sndbulkpipe(dev->udev, dev->driver_info->out);
  167. return 0;
  168. }
  169. static const struct driver_info genelink_info = {
  170. .description = "Genesys GeneLink",
  171. .flags = FLAG_POINTTOPOINT | FLAG_FRAMING_GL | FLAG_NO_SETINT,
  172. .bind = genelink_bind,
  173. .rx_fixup = genelink_rx_fixup,
  174. .tx_fixup = genelink_tx_fixup,
  175. .in = 1, .out = 2,
  176. #ifdef GENELINK_ACK
  177. .check_connect =genelink_check_connect,
  178. #endif
  179. };
  180. static const struct usb_device_id products [] = {
  181. {
  182. USB_DEVICE(0x05e3, 0x0502), // GL620USB-A
  183. .driver_info = (unsigned long) &genelink_info,
  184. },
  185. /* NOT: USB_DEVICE(0x05e3, 0x0501), // GL620USB
  186. * that's half duplex, not currently supported
  187. */
  188. { }, // END
  189. };
  190. MODULE_DEVICE_TABLE(usb, products);
  191. static struct usb_driver gl620a_driver = {
  192. .name = "gl620a",
  193. .id_table = products,
  194. .probe = usbnet_probe,
  195. .disconnect = usbnet_disconnect,
  196. .suspend = usbnet_suspend,
  197. .resume = usbnet_resume,
  198. .disable_hub_initiated_lpm = 1,
  199. };
  200. module_usb_driver(gl620a_driver);
  201. MODULE_AUTHOR("Jiun-Jie Huang");
  202. MODULE_DESCRIPTION("GL620-USB-A Host-to-Host Link cables");
  203. MODULE_LICENSE("GPL");