blob: 3569da1135c140f65a7b1c56430ca064594b8a5a [file] [log] [blame]
David Brownell47ee3052005-08-31 09:53:42 -07001/*
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
Jeff Kirsher9cb00072013-12-06 06:28:46 -080017 * along with this program; if not, see <http://www.gnu.org/licenses/>.
David Brownell47ee3052005-08-31 09:53:42 -070018 */
19
20// #define DEBUG // error path messages, extra info
21// #define VERBOSE // more; success messages
22
David Brownell47ee3052005-08-31 09:53:42 -070023#include <linux/module.h>
David Brownell47ee3052005-08-31 09:53:42 -070024#include <linux/init.h>
25#include <linux/netdevice.h>
26#include <linux/etherdevice.h>
27#include <linux/ethtool.h>
28#include <linux/workqueue.h>
29#include <linux/mii.h>
30#include <linux/usb.h>
Jussi Kivilinna3692e942008-01-26 00:51:45 +020031#include <linux/usb/usbnet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/gfp.h>
David Brownell47ee3052005-08-31 09:53:42 -070033
34
35/*
36 * GeneSys GL620USB-A (www.genesyslogic.com.tw)
37 *
38 * ... should partially interop with the Win32 driver for this hardware.
39 * The GeneSys docs imply there's some NDIS issue motivating this framing.
40 *
41 * Some info from GeneSys:
42 * - GL620USB-A is full duplex; GL620USB is only half duplex for bulk.
43 * (Some cables, like the BAFO-100c, use the half duplex version.)
44 * - For the full duplex model, the low bit of the version code says
45 * which side is which ("left/right").
46 * - For the half duplex type, a control/interrupt handshake settles
47 * the transfer direction. (That's disabled here, partially coded.)
48 * A control URB would block until other side writes an interrupt.
49 *
50 * Original code from Jiun-Jie Huang <huangjj@genesyslogic.com.tw>
51 * and merged into "usbnet" by Stanislav Brabec <utx@penguin.cz>.
52 */
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
70struct gl_packet {
Al Viro53ebb3b2007-02-09 16:39:55 +000071 __le32 packet_length;
David Brownell47ee3052005-08-31 09:53:42 -070072 char packet_data [1];
73};
74
75struct gl_header {
Al Viro53ebb3b2007-02-09 16:39:55 +000076 __le32 packet_count;
David Brownell47ee3052005-08-31 09:53:42 -070077 struct gl_packet packets;
78};
79
David Brownell47ee3052005-08-31 09:53:42 -070080static int genelink_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
81{
82 struct gl_header *header;
83 struct gl_packet *packet;
84 struct sk_buff *gl_skb;
85 u32 size;
Al Viro53ebb3b2007-02-09 16:39:55 +000086 u32 count;
David Brownell47ee3052005-08-31 09:53:42 -070087
88 header = (struct gl_header *) skb->data;
89
90 // get the packet count of the received skb
Al Viro53ebb3b2007-02-09 16:39:55 +000091 count = le32_to_cpu(header->packet_count);
92 if (count > GL_MAX_TRANSMIT_PACKETS) {
Greg Kroah-Hartman49ae25b2012-09-19 09:46:14 +000093 netdev_dbg(dev->net,
94 "genelink: invalid received packet count %u\n",
95 count);
David Brownell47ee3052005-08-31 09:53:42 -070096 return 0;
97 }
98
99 // set the current packet pointer to the first packet
100 packet = &header->packets;
101
102 // decrement the length for the packet count size 4 bytes
103 skb_pull(skb, 4);
104
Al Viro53ebb3b2007-02-09 16:39:55 +0000105 while (count > 1) {
David Brownell47ee3052005-08-31 09:53:42 -0700106 // get the packet length
107 size = le32_to_cpu(packet->packet_length);
108
109 // this may be a broken packet
110 if (size > GL_MAX_PACKET_LEN) {
Greg Kroah-Hartman49ae25b2012-09-19 09:46:14 +0000111 netdev_dbg(dev->net, "genelink: invalid rx length %d\n",
112 size);
David Brownell47ee3052005-08-31 09:53:42 -0700113 return 0;
114 }
115
116 // allocate the skb for the individual packet
117 gl_skb = alloc_skb(size, GFP_ATOMIC);
118 if (gl_skb) {
119
120 // copy the packet data to the new skb
121 memcpy(skb_put(gl_skb, size),
122 packet->packet_data, size);
123 usbnet_skb_return(dev, gl_skb);
124 }
125
126 // advance to the next packet
Al Viro53ebb3b2007-02-09 16:39:55 +0000127 packet = (struct gl_packet *)&packet->packet_data[size];
128 count--;
David Brownell47ee3052005-08-31 09:53:42 -0700129
130 // shift the data pointer to the next gl_packet
131 skb_pull(skb, size + 4);
132 }
133
134 // skip the packet length field 4 bytes
135 skb_pull(skb, 4);
136
137 if (skb->len > GL_MAX_PACKET_LEN) {
Greg Kroah-Hartman49ae25b2012-09-19 09:46:14 +0000138 netdev_dbg(dev->net, "genelink: invalid rx length %d\n",
139 skb->len);
David Brownell47ee3052005-08-31 09:53:42 -0700140 return 0;
141 }
142 return 1;
143}
144
145static struct sk_buff *
Al Viro55016f12005-10-21 03:21:58 -0400146genelink_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
David Brownell47ee3052005-08-31 09:53:42 -0700147{
148 int padlen;
149 int length = skb->len;
150 int headroom = skb_headroom(skb);
151 int tailroom = skb_tailroom(skb);
Al Viro53ebb3b2007-02-09 16:39:55 +0000152 __le32 *packet_count;
153 __le32 *packet_len;
David Brownell47ee3052005-08-31 09:53:42 -0700154
155 // FIXME: magic numbers, bleech
156 padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
157
158 if ((!skb_cloned(skb))
159 && ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
160 if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
161 skb->data = memmove(skb->head + (4 + 4*1),
162 skb->data, skb->len);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700163 skb_set_tail_pointer(skb, skb->len);
David Brownell47ee3052005-08-31 09:53:42 -0700164 }
165 } else {
166 struct sk_buff *skb2;
167 skb2 = skb_copy_expand(skb, (4 + 4*1) , padlen, flags);
168 dev_kfree_skb_any(skb);
169 skb = skb2;
170 if (!skb)
171 return NULL;
172 }
173
174 // attach the packet count to the header
Al Viro53ebb3b2007-02-09 16:39:55 +0000175 packet_count = (__le32 *) skb_push(skb, (4 + 4*1));
David Brownell47ee3052005-08-31 09:53:42 -0700176 packet_len = packet_count + 1;
177
178 *packet_count = cpu_to_le32(1);
179 *packet_len = cpu_to_le32(length);
180
181 // add padding byte
182 if ((skb->len % dev->maxpacket) == 0)
183 skb_put(skb, 1);
184
185 return skb;
186}
187
188static int genelink_bind(struct usbnet *dev, struct usb_interface *intf)
189{
190 dev->hard_mtu = GL_RCV_BUF_SIZE;
191 dev->net->hard_header_len += 4;
192 dev->in = usb_rcvbulkpipe(dev->udev, dev->driver_info->in);
193 dev->out = usb_sndbulkpipe(dev->udev, dev->driver_info->out);
194 return 0;
195}
196
197static const struct driver_info genelink_info = {
198 .description = "Genesys GeneLink",
Arnd Bergmannc2613442011-04-01 20:12:02 -0700199 .flags = FLAG_POINTTOPOINT | FLAG_FRAMING_GL | FLAG_NO_SETINT,
David Brownell47ee3052005-08-31 09:53:42 -0700200 .bind = genelink_bind,
201 .rx_fixup = genelink_rx_fixup,
202 .tx_fixup = genelink_tx_fixup,
203
204 .in = 1, .out = 2,
205
206#ifdef GENELINK_ACK
207 .check_connect =genelink_check_connect,
208#endif
209};
210
211static const struct usb_device_id products [] = {
212
213{
214 USB_DEVICE(0x05e3, 0x0502), // GL620USB-A
215 .driver_info = (unsigned long) &genelink_info,
216},
217 /* NOT: USB_DEVICE(0x05e3, 0x0501), // GL620USB
218 * that's half duplex, not currently supported
219 */
220 { }, // END
221};
222MODULE_DEVICE_TABLE(usb, products);
223
224static struct usb_driver gl620a_driver = {
David Brownell47ee3052005-08-31 09:53:42 -0700225 .name = "gl620a",
226 .id_table = products,
227 .probe = usbnet_probe,
228 .disconnect = usbnet_disconnect,
229 .suspend = usbnet_suspend,
230 .resume = usbnet_resume,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -0700231 .disable_hub_initiated_lpm = 1,
David Brownell47ee3052005-08-31 09:53:42 -0700232};
233
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -0800234module_usb_driver(gl620a_driver);
David Brownell47ee3052005-08-31 09:53:42 -0700235
236MODULE_AUTHOR("Jiun-Jie Huang");
237MODULE_DESCRIPTION("GL620-USB-A Host-to-Host Link cables");
238MODULE_LICENSE("GPL");
239