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