gl620a.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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/config.h>
  23. #ifdef CONFIG_USB_DEBUG
  24. # define DEBUG
  25. #endif
  26. #include <linux/module.h>
  27. #include <linux/sched.h>
  28. #include <linux/init.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/ethtool.h>
  32. #include <linux/workqueue.h>
  33. #include <linux/mii.h>
  34. #include <linux/usb.h>
  35. #include "usbnet.h"
  36. /*
  37. * GeneSys GL620USB-A (www.genesyslogic.com.tw)
  38. *
  39. * ... should partially interop with the Win32 driver for this hardware.
  40. * The GeneSys docs imply there's some NDIS issue motivating this framing.
  41. *
  42. * Some info from GeneSys:
  43. * - GL620USB-A is full duplex; GL620USB is only half duplex for bulk.
  44. * (Some cables, like the BAFO-100c, use the half duplex version.)
  45. * - For the full duplex model, the low bit of the version code says
  46. * which side is which ("left/right").
  47. * - For the half duplex type, a control/interrupt handshake settles
  48. * the transfer direction. (That's disabled here, partially coded.)
  49. * A control URB would block until other side writes an interrupt.
  50. *
  51. * Original code from Jiun-Jie Huang <huangjj@genesyslogic.com.tw>
  52. * and merged into "usbnet" by Stanislav Brabec <utx@penguin.cz>.
  53. */
  54. // control msg write command
  55. #define GENELINK_CONNECT_WRITE 0xF0
  56. // interrupt pipe index
  57. #define GENELINK_INTERRUPT_PIPE 0x03
  58. // interrupt read buffer size
  59. #define INTERRUPT_BUFSIZE 0x08
  60. // interrupt pipe interval value
  61. #define GENELINK_INTERRUPT_INTERVAL 0x10
  62. // max transmit packet number per transmit
  63. #define GL_MAX_TRANSMIT_PACKETS 32
  64. // max packet length
  65. #define GL_MAX_PACKET_LEN 1514
  66. // max receive buffer size
  67. #define GL_RCV_BUF_SIZE \
  68. (((GL_MAX_PACKET_LEN + 4) * GL_MAX_TRANSMIT_PACKETS) + 4)
  69. struct gl_packet {
  70. u32 packet_length;
  71. char packet_data [1];
  72. };
  73. struct gl_header {
  74. u32 packet_count;
  75. struct gl_packet packets;
  76. };
  77. #ifdef GENELINK_ACK
  78. // FIXME: this code is incomplete, not debugged; it doesn't
  79. // handle interrupts correctly; it should use the generic
  80. // status IRQ code (which didn't exist back in 2001).
  81. struct gl_priv {
  82. struct urb *irq_urb;
  83. char irq_buf [INTERRUPT_BUFSIZE];
  84. };
  85. static inline int gl_control_write(struct usbnet *dev, u8 request, u16 value)
  86. {
  87. int retval;
  88. retval = usb_control_msg(dev->udev,
  89. usb_sndctrlpipe(dev->udev, 0),
  90. request,
  91. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  92. value,
  93. 0, // index
  94. 0, // data buffer
  95. 0, // size
  96. USB_CTRL_SET_TIMEOUT);
  97. return retval;
  98. }
  99. static void gl_interrupt_complete(struct urb *urb, struct pt_regs *regs)
  100. {
  101. int status = urb->status;
  102. switch (status) {
  103. case 0:
  104. /* success */
  105. break;
  106. case -ECONNRESET:
  107. case -ENOENT:
  108. case -ESHUTDOWN:
  109. /* this urb is terminated, clean up */
  110. dbg("%s - urb shutting down with status: %d",
  111. __FUNCTION__, status);
  112. return;
  113. default:
  114. dbg("%s - nonzero urb status received: %d",
  115. __FUNCTION__, urb->status);
  116. }
  117. status = usb_submit_urb(urb, GFP_ATOMIC);
  118. if (status)
  119. err("%s - usb_submit_urb failed with result %d",
  120. __FUNCTION__, status);
  121. }
  122. static int gl_interrupt_read(struct usbnet *dev)
  123. {
  124. struct gl_priv *priv = dev->priv_data;
  125. int retval;
  126. // issue usb interrupt read
  127. if (priv && priv->irq_urb) {
  128. // submit urb
  129. if ((retval = usb_submit_urb(priv->irq_urb, GFP_KERNEL)) != 0)
  130. dbg("gl_interrupt_read: submit fail - %X...", retval);
  131. else
  132. dbg("gl_interrupt_read: submit success...");
  133. }
  134. return 0;
  135. }
  136. // check whether another side is connected
  137. static int genelink_check_connect(struct usbnet *dev)
  138. {
  139. int retval;
  140. dbg("genelink_check_connect...");
  141. // detect whether another side is connected
  142. if ((retval = gl_control_write(dev, GENELINK_CONNECT_WRITE, 0)) != 0) {
  143. dbg("%s: genelink_check_connect write fail - %X",
  144. dev->net->name, retval);
  145. return retval;
  146. }
  147. // usb interrupt read to ack another side
  148. if ((retval = gl_interrupt_read(dev)) != 0) {
  149. dbg("%s: genelink_check_connect read fail - %X",
  150. dev->net->name, retval);
  151. return retval;
  152. }
  153. dbg("%s: genelink_check_connect read success", dev->net->name);
  154. return 0;
  155. }
  156. // allocate and initialize the private data for genelink
  157. static int genelink_init(struct usbnet *dev)
  158. {
  159. struct gl_priv *priv;
  160. // allocate the private data structure
  161. if ((priv = kmalloc(sizeof *priv, GFP_KERNEL)) == 0) {
  162. dbg("%s: cannot allocate private data per device",
  163. dev->net->name);
  164. return -ENOMEM;
  165. }
  166. // allocate irq urb
  167. if ((priv->irq_urb = usb_alloc_urb(0, GFP_KERNEL)) == 0) {
  168. dbg("%s: cannot allocate private irq urb per device",
  169. dev->net->name);
  170. kfree(priv);
  171. return -ENOMEM;
  172. }
  173. // fill irq urb
  174. usb_fill_int_urb(priv->irq_urb, dev->udev,
  175. usb_rcvintpipe(dev->udev, GENELINK_INTERRUPT_PIPE),
  176. priv->irq_buf, INTERRUPT_BUFSIZE,
  177. gl_interrupt_complete, 0,
  178. GENELINK_INTERRUPT_INTERVAL);
  179. // set private data pointer
  180. dev->priv_data = priv;
  181. return 0;
  182. }
  183. // release the private data
  184. static int genelink_free(struct usbnet *dev)
  185. {
  186. struct gl_priv *priv = dev->priv_data;
  187. if (!priv)
  188. return 0;
  189. // FIXME: can't cancel here; it's synchronous, and
  190. // should have happened earlier in any case (interrupt
  191. // handling needs to be generic)
  192. // cancel irq urb first
  193. usb_kill_urb(priv->irq_urb);
  194. // free irq urb
  195. usb_free_urb(priv->irq_urb);
  196. // free the private data structure
  197. kfree(priv);
  198. return 0;
  199. }
  200. #endif
  201. static int genelink_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  202. {
  203. struct gl_header *header;
  204. struct gl_packet *packet;
  205. struct sk_buff *gl_skb;
  206. u32 size;
  207. header = (struct gl_header *) skb->data;
  208. // get the packet count of the received skb
  209. le32_to_cpus(&header->packet_count);
  210. if ((header->packet_count > GL_MAX_TRANSMIT_PACKETS)
  211. || (header->packet_count < 0)) {
  212. dbg("genelink: invalid received packet count %d",
  213. header->packet_count);
  214. return 0;
  215. }
  216. // set the current packet pointer to the first packet
  217. packet = &header->packets;
  218. // decrement the length for the packet count size 4 bytes
  219. skb_pull(skb, 4);
  220. while (header->packet_count > 1) {
  221. // get the packet length
  222. size = le32_to_cpu(packet->packet_length);
  223. // this may be a broken packet
  224. if (size > GL_MAX_PACKET_LEN) {
  225. dbg("genelink: invalid rx length %d", size);
  226. return 0;
  227. }
  228. // allocate the skb for the individual packet
  229. gl_skb = alloc_skb(size, GFP_ATOMIC);
  230. if (gl_skb) {
  231. // copy the packet data to the new skb
  232. memcpy(skb_put(gl_skb, size),
  233. packet->packet_data, size);
  234. usbnet_skb_return(dev, gl_skb);
  235. }
  236. // advance to the next packet
  237. packet = (struct gl_packet *)
  238. &packet->packet_data [size];
  239. header->packet_count--;
  240. // shift the data pointer to the next gl_packet
  241. skb_pull(skb, size + 4);
  242. }
  243. // skip the packet length field 4 bytes
  244. skb_pull(skb, 4);
  245. if (skb->len > GL_MAX_PACKET_LEN) {
  246. dbg("genelink: invalid rx length %d", skb->len);
  247. return 0;
  248. }
  249. return 1;
  250. }
  251. static struct sk_buff *
  252. genelink_tx_fixup(struct usbnet *dev, struct sk_buff *skb, unsigned flags)
  253. {
  254. int padlen;
  255. int length = skb->len;
  256. int headroom = skb_headroom(skb);
  257. int tailroom = skb_tailroom(skb);
  258. u32 *packet_count;
  259. u32 *packet_len;
  260. // FIXME: magic numbers, bleech
  261. padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
  262. if ((!skb_cloned(skb))
  263. && ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
  264. if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
  265. skb->data = memmove(skb->head + (4 + 4*1),
  266. skb->data, skb->len);
  267. skb->tail = skb->data + skb->len;
  268. }
  269. } else {
  270. struct sk_buff *skb2;
  271. skb2 = skb_copy_expand(skb, (4 + 4*1) , padlen, flags);
  272. dev_kfree_skb_any(skb);
  273. skb = skb2;
  274. if (!skb)
  275. return NULL;
  276. }
  277. // attach the packet count to the header
  278. packet_count = (u32 *) skb_push(skb, (4 + 4*1));
  279. packet_len = packet_count + 1;
  280. *packet_count = cpu_to_le32(1);
  281. *packet_len = cpu_to_le32(length);
  282. // add padding byte
  283. if ((skb->len % dev->maxpacket) == 0)
  284. skb_put(skb, 1);
  285. return skb;
  286. }
  287. static int genelink_bind(struct usbnet *dev, struct usb_interface *intf)
  288. {
  289. dev->hard_mtu = GL_RCV_BUF_SIZE;
  290. dev->net->hard_header_len += 4;
  291. dev->in = usb_rcvbulkpipe(dev->udev, dev->driver_info->in);
  292. dev->out = usb_sndbulkpipe(dev->udev, dev->driver_info->out);
  293. return 0;
  294. }
  295. static const struct driver_info genelink_info = {
  296. .description = "Genesys GeneLink",
  297. .flags = FLAG_FRAMING_GL | FLAG_NO_SETINT,
  298. .bind = genelink_bind,
  299. .rx_fixup = genelink_rx_fixup,
  300. .tx_fixup = genelink_tx_fixup,
  301. .in = 1, .out = 2,
  302. #ifdef GENELINK_ACK
  303. .check_connect =genelink_check_connect,
  304. #endif
  305. };
  306. static const struct usb_device_id products [] = {
  307. {
  308. USB_DEVICE(0x05e3, 0x0502), // GL620USB-A
  309. .driver_info = (unsigned long) &genelink_info,
  310. },
  311. /* NOT: USB_DEVICE(0x05e3, 0x0501), // GL620USB
  312. * that's half duplex, not currently supported
  313. */
  314. { }, // END
  315. };
  316. MODULE_DEVICE_TABLE(usb, products);
  317. static struct usb_driver gl620a_driver = {
  318. .owner = THIS_MODULE,
  319. .name = "gl620a",
  320. .id_table = products,
  321. .probe = usbnet_probe,
  322. .disconnect = usbnet_disconnect,
  323. .suspend = usbnet_suspend,
  324. .resume = usbnet_resume,
  325. };
  326. static int __init usbnet_init(void)
  327. {
  328. return usb_register(&gl620a_driver);
  329. }
  330. module_init(usbnet_init);
  331. static void __exit usbnet_exit(void)
  332. {
  333. usb_deregister(&gl620a_driver);
  334. }
  335. module_exit(usbnet_exit);
  336. MODULE_AUTHOR("Jiun-Jie Huang");
  337. MODULE_DESCRIPTION("GL620-USB-A Host-to-Host Link cables");
  338. MODULE_LICENSE("GPL");