xref: /openbmc/linux/drivers/net/usb/gl620a.c (revision 643d1f7f)
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 
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/ethtool.h>
29 #include <linux/workqueue.h>
30 #include <linux/mii.h>
31 #include <linux/usb.h>
32 #include <linux/usb/usbnet.h>
33 
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 
70 struct gl_packet {
71 	__le32		packet_length;
72 	char		packet_data [1];
73 };
74 
75 struct gl_header {
76 	__le32			packet_count;
77 	struct gl_packet	packets;
78 };
79 
80 static 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;
86 	u32			count;
87 
88 	header = (struct gl_header *) skb->data;
89 
90 	// get the packet count of the received skb
91 	count = le32_to_cpu(header->packet_count);
92 	if (count > GL_MAX_TRANSMIT_PACKETS) {
93 		dbg("genelink: invalid received packet count %u", count);
94 		return 0;
95 	}
96 
97 	// set the current packet pointer to the first packet
98 	packet = &header->packets;
99 
100 	// decrement the length for the packet count size 4 bytes
101 	skb_pull(skb, 4);
102 
103 	while (count > 1) {
104 		// get the packet length
105 		size = le32_to_cpu(packet->packet_length);
106 
107 		// this may be a broken packet
108 		if (size > GL_MAX_PACKET_LEN) {
109 			dbg("genelink: invalid rx length %d", size);
110 			return 0;
111 		}
112 
113 		// allocate the skb for the individual packet
114 		gl_skb = alloc_skb(size, GFP_ATOMIC);
115 		if (gl_skb) {
116 
117 			// copy the packet data to the new skb
118 			memcpy(skb_put(gl_skb, size),
119 					packet->packet_data, size);
120 			usbnet_skb_return(dev, gl_skb);
121 		}
122 
123 		// advance to the next packet
124 		packet = (struct gl_packet *)&packet->packet_data[size];
125 		count--;
126 
127 		// shift the data pointer to the next gl_packet
128 		skb_pull(skb, size + 4);
129 	}
130 
131 	// skip the packet length field 4 bytes
132 	skb_pull(skb, 4);
133 
134 	if (skb->len > GL_MAX_PACKET_LEN) {
135 		dbg("genelink: invalid rx length %d", skb->len);
136 		return 0;
137 	}
138 	return 1;
139 }
140 
141 static struct sk_buff *
142 genelink_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
143 {
144 	int 	padlen;
145 	int	length = skb->len;
146 	int	headroom = skb_headroom(skb);
147 	int	tailroom = skb_tailroom(skb);
148 	__le32	*packet_count;
149 	__le32	*packet_len;
150 
151 	// FIXME:  magic numbers, bleech
152 	padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
153 
154 	if ((!skb_cloned(skb))
155 			&& ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
156 		if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
157 			skb->data = memmove(skb->head + (4 + 4*1),
158 					     skb->data, skb->len);
159 			skb_set_tail_pointer(skb, skb->len);
160 		}
161 	} else {
162 		struct sk_buff	*skb2;
163 		skb2 = skb_copy_expand(skb, (4 + 4*1) , padlen, flags);
164 		dev_kfree_skb_any(skb);
165 		skb = skb2;
166 		if (!skb)
167 			return NULL;
168 	}
169 
170 	// attach the packet count to the header
171 	packet_count = (__le32 *) skb_push(skb, (4 + 4*1));
172 	packet_len = packet_count + 1;
173 
174 	*packet_count = cpu_to_le32(1);
175 	*packet_len = cpu_to_le32(length);
176 
177 	// add padding byte
178 	if ((skb->len % dev->maxpacket) == 0)
179 		skb_put(skb, 1);
180 
181 	return skb;
182 }
183 
184 static int genelink_bind(struct usbnet *dev, struct usb_interface *intf)
185 {
186 	dev->hard_mtu = GL_RCV_BUF_SIZE;
187 	dev->net->hard_header_len += 4;
188 	dev->in = usb_rcvbulkpipe(dev->udev, dev->driver_info->in);
189 	dev->out = usb_sndbulkpipe(dev->udev, dev->driver_info->out);
190 	return 0;
191 }
192 
193 static const struct driver_info	genelink_info = {
194 	.description =	"Genesys GeneLink",
195 	.flags =	FLAG_FRAMING_GL | FLAG_NO_SETINT,
196 	.bind =		genelink_bind,
197 	.rx_fixup =	genelink_rx_fixup,
198 	.tx_fixup =	genelink_tx_fixup,
199 
200 	.in = 1, .out = 2,
201 
202 #ifdef	GENELINK_ACK
203 	.check_connect =genelink_check_connect,
204 #endif
205 };
206 
207 static const struct usb_device_id	products [] = {
208 
209 {
210 	USB_DEVICE(0x05e3, 0x0502),	// GL620USB-A
211 	.driver_info =	(unsigned long) &genelink_info,
212 },
213 	/* NOT: USB_DEVICE(0x05e3, 0x0501),	// GL620USB
214 	 * that's half duplex, not currently supported
215 	 */
216 	{ },		// END
217 };
218 MODULE_DEVICE_TABLE(usb, products);
219 
220 static struct usb_driver gl620a_driver = {
221 	.name =		"gl620a",
222 	.id_table =	products,
223 	.probe =	usbnet_probe,
224 	.disconnect =	usbnet_disconnect,
225 	.suspend =	usbnet_suspend,
226 	.resume =	usbnet_resume,
227 };
228 
229 static int __init usbnet_init(void)
230 {
231  	return usb_register(&gl620a_driver);
232 }
233 module_init(usbnet_init);
234 
235 static void __exit usbnet_exit(void)
236 {
237  	usb_deregister(&gl620a_driver);
238 }
239 module_exit(usbnet_exit);
240 
241 MODULE_AUTHOR("Jiun-Jie Huang");
242 MODULE_DESCRIPTION("GL620-USB-A Host-to-Host Link cables");
243 MODULE_LICENSE("GPL");
244 
245