Blame view

drivers/net/usb/gl620a.c 6.48 KB
47ee3051c   David Brownell   [PATCH] USB: usbn...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  /*
   * GeneSys GL620USB-A based links
   * Copyright (C) 2001 by Jiun-Jie Huang <huangjj@genesyslogic.com.tw>
   * Copyright (C) 2001 by Stanislav Brabec <utx@penguin.cz>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
9cb00073d   Jeff Kirsher   usb: Fix FSF addr...
17
   * along with this program; if not, see <http://www.gnu.org/licenses/>.
47ee3051c   David Brownell   [PATCH] USB: usbn...
18
19
20
21
   */
  
  // #define	DEBUG			// error path messages, extra info
  // #define	VERBOSE			// more; success messages
47ee3051c   David Brownell   [PATCH] USB: usbn...
22
  #include <linux/module.h>
47ee3051c   David Brownell   [PATCH] USB: usbn...
23
24
25
26
27
28
  #include <linux/netdevice.h>
  #include <linux/etherdevice.h>
  #include <linux/ethtool.h>
  #include <linux/workqueue.h>
  #include <linux/mii.h>
  #include <linux/usb.h>
3692e94f1   Jussi Kivilinna   Move usbnet.h and...
29
  #include <linux/usb/usbnet.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
30
  #include <linux/gfp.h>
47ee3051c   David Brownell   [PATCH] USB: usbn...
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  
  
  /*
   * GeneSys GL620USB-A (www.genesyslogic.com.tw)
   *
   * ... should partially interop with the Win32 driver for this hardware.
   * The GeneSys docs imply there's some NDIS issue motivating this framing.
   *
   * Some info from GeneSys:
   *  - GL620USB-A is full duplex; GL620USB is only half duplex for bulk.
   *    (Some cables, like the BAFO-100c, use the half duplex version.)
   *  - For the full duplex model, the low bit of the version code says
   *    which side is which ("left/right").
   *  - For the half duplex type, a control/interrupt handshake settles
   *    the transfer direction.  (That's disabled here, partially coded.)
   *    A control URB would block until other side writes an interrupt.
   *
   * Original code from Jiun-Jie Huang <huangjj@genesyslogic.com.tw>
   * and merged into "usbnet" by Stanislav Brabec <utx@penguin.cz>.
   */
  
  // control msg write command
  #define GENELINK_CONNECT_WRITE			0xF0
  // interrupt pipe index
  #define GENELINK_INTERRUPT_PIPE			0x03
  // interrupt read buffer size
  #define INTERRUPT_BUFSIZE			0x08
  // interrupt pipe interval value
  #define GENELINK_INTERRUPT_INTERVAL		0x10
  // max transmit packet number per transmit
  #define GL_MAX_TRANSMIT_PACKETS			32
  // max packet length
  #define GL_MAX_PACKET_LEN			1514
  // max receive buffer size
  #define GL_RCV_BUF_SIZE		\
  	(((GL_MAX_PACKET_LEN + 4) * GL_MAX_TRANSMIT_PACKETS) + 4)
  
  struct gl_packet {
53ebb3b82   Al Viro   [PATCH] trivial u...
69
  	__le32		packet_length;
47ee3051c   David Brownell   [PATCH] USB: usbn...
70
71
72
73
  	char		packet_data [1];
  };
  
  struct gl_header {
53ebb3b82   Al Viro   [PATCH] trivial u...
74
  	__le32			packet_count;
47ee3051c   David Brownell   [PATCH] USB: usbn...
75
76
  	struct gl_packet	packets;
  };
47ee3051c   David Brownell   [PATCH] USB: usbn...
77
78
79
80
81
82
  static int genelink_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  {
  	struct gl_header	*header;
  	struct gl_packet	*packet;
  	struct sk_buff		*gl_skb;
  	u32			size;
53ebb3b82   Al Viro   [PATCH] trivial u...
83
  	u32			count;
47ee3051c   David Brownell   [PATCH] USB: usbn...
84

eb85569fe   Emil Goode   usbnet: remove ge...
85
86
87
  	/* This check is no longer done by usbnet */
  	if (skb->len < dev->net->hard_header_len)
  		return 0;
47ee3051c   David Brownell   [PATCH] USB: usbn...
88
89
90
  	header = (struct gl_header *) skb->data;
  
  	// get the packet count of the received skb
53ebb3b82   Al Viro   [PATCH] trivial u...
91
92
  	count = le32_to_cpu(header->packet_count);
  	if (count > GL_MAX_TRANSMIT_PACKETS) {
49ae25b03   Greg Kroah-Hartman   USB: remove dbg()...
93
94
95
96
  		netdev_dbg(dev->net,
  			   "genelink: invalid received packet count %u
  ",
  			   count);
47ee3051c   David Brownell   [PATCH] USB: usbn...
97
98
99
100
101
102
103
104
  		return 0;
  	}
  
  	// set the current packet pointer to the first packet
  	packet = &header->packets;
  
  	// decrement the length for the packet count size 4 bytes
  	skb_pull(skb, 4);
53ebb3b82   Al Viro   [PATCH] trivial u...
105
  	while (count > 1) {
47ee3051c   David Brownell   [PATCH] USB: usbn...
106
107
108
109
110
  		// get the packet length
  		size = le32_to_cpu(packet->packet_length);
  
  		// this may be a broken packet
  		if (size > GL_MAX_PACKET_LEN) {
49ae25b03   Greg Kroah-Hartman   USB: remove dbg()...
111
112
113
  			netdev_dbg(dev->net, "genelink: invalid rx length %d
  ",
  				   size);
47ee3051c   David Brownell   [PATCH] USB: usbn...
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  			return 0;
  		}
  
  		// allocate the skb for the individual packet
  		gl_skb = alloc_skb(size, GFP_ATOMIC);
  		if (gl_skb) {
  
  			// copy the packet data to the new skb
  			memcpy(skb_put(gl_skb, size),
  					packet->packet_data, size);
  			usbnet_skb_return(dev, gl_skb);
  		}
  
  		// advance to the next packet
53ebb3b82   Al Viro   [PATCH] trivial u...
128
129
  		packet = (struct gl_packet *)&packet->packet_data[size];
  		count--;
47ee3051c   David Brownell   [PATCH] USB: usbn...
130
131
132
133
134
135
136
137
138
  
  		// shift the data pointer to the next gl_packet
  		skb_pull(skb, size + 4);
  	}
  
  	// skip the packet length field 4 bytes
  	skb_pull(skb, 4);
  
  	if (skb->len > GL_MAX_PACKET_LEN) {
49ae25b03   Greg Kroah-Hartman   USB: remove dbg()...
139
140
141
  		netdev_dbg(dev->net, "genelink: invalid rx length %d
  ",
  			   skb->len);
47ee3051c   David Brownell   [PATCH] USB: usbn...
142
143
144
145
146
147
  		return 0;
  	}
  	return 1;
  }
  
  static struct sk_buff *
55016f10e   Al Viro   [PATCH] gfp_t: dr...
148
  genelink_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
47ee3051c   David Brownell   [PATCH] USB: usbn...
149
150
151
152
153
  {
  	int 	padlen;
  	int	length = skb->len;
  	int	headroom = skb_headroom(skb);
  	int	tailroom = skb_tailroom(skb);
53ebb3b82   Al Viro   [PATCH] trivial u...
154
155
  	__le32	*packet_count;
  	__le32	*packet_len;
47ee3051c   David Brownell   [PATCH] USB: usbn...
156
157
158
159
160
161
162
163
164
  
  	// FIXME:  magic numbers, bleech
  	padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
  
  	if ((!skb_cloned(skb))
  			&& ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
  		if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
  			skb->data = memmove(skb->head + (4 + 4*1),
  					     skb->data, skb->len);
27a884dc3   Arnaldo Carvalho de Melo   [SK_BUFF]: Conver...
165
  			skb_set_tail_pointer(skb, skb->len);
47ee3051c   David Brownell   [PATCH] USB: usbn...
166
167
168
169
170
171
172
173
174
175
176
  		}
  	} else {
  		struct sk_buff	*skb2;
  		skb2 = skb_copy_expand(skb, (4 + 4*1) , padlen, flags);
  		dev_kfree_skb_any(skb);
  		skb = skb2;
  		if (!skb)
  			return NULL;
  	}
  
  	// attach the packet count to the header
53ebb3b82   Al Viro   [PATCH] trivial u...
177
  	packet_count = (__le32 *) skb_push(skb, (4 + 4*1));
47ee3051c   David Brownell   [PATCH] USB: usbn...
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  	packet_len = packet_count + 1;
  
  	*packet_count = cpu_to_le32(1);
  	*packet_len = cpu_to_le32(length);
  
  	// add padding byte
  	if ((skb->len % dev->maxpacket) == 0)
  		skb_put(skb, 1);
  
  	return skb;
  }
  
  static int genelink_bind(struct usbnet *dev, struct usb_interface *intf)
  {
  	dev->hard_mtu = GL_RCV_BUF_SIZE;
  	dev->net->hard_header_len += 4;
  	dev->in = usb_rcvbulkpipe(dev->udev, dev->driver_info->in);
  	dev->out = usb_sndbulkpipe(dev->udev, dev->driver_info->out);
  	return 0;
  }
  
  static const struct driver_info	genelink_info = {
  	.description =	"Genesys GeneLink",
c261344d3   Arnd Bergmann   usbnet: use eth%d...
201
  	.flags =	FLAG_POINTTOPOINT | FLAG_FRAMING_GL | FLAG_NO_SETINT,
47ee3051c   David Brownell   [PATCH] USB: usbn...
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
  	.bind =		genelink_bind,
  	.rx_fixup =	genelink_rx_fixup,
  	.tx_fixup =	genelink_tx_fixup,
  
  	.in = 1, .out = 2,
  
  #ifdef	GENELINK_ACK
  	.check_connect =genelink_check_connect,
  #endif
  };
  
  static const struct usb_device_id	products [] = {
  
  {
  	USB_DEVICE(0x05e3, 0x0502),	// GL620USB-A
  	.driver_info =	(unsigned long) &genelink_info,
  },
  	/* NOT: USB_DEVICE(0x05e3, 0x0501),	// GL620USB
  	 * that's half duplex, not currently supported
  	 */
  	{ },		// END
  };
  MODULE_DEVICE_TABLE(usb, products);
  
  static struct usb_driver gl620a_driver = {
47ee3051c   David Brownell   [PATCH] USB: usbn...
227
228
229
230
231
232
  	.name =		"gl620a",
  	.id_table =	products,
  	.probe =	usbnet_probe,
  	.disconnect =	usbnet_disconnect,
  	.suspend =	usbnet_suspend,
  	.resume =	usbnet_resume,
e1f12eb6b   Sarah Sharp   USB: Disable hub-...
233
  	.disable_hub_initiated_lpm = 1,
47ee3051c   David Brownell   [PATCH] USB: usbn...
234
  };
d632eb1bf   Greg Kroah-Hartman   USB: convert driv...
235
  module_usb_driver(gl620a_driver);
47ee3051c   David Brownell   [PATCH] USB: usbn...
236
237
238
239
  
  MODULE_AUTHOR("Jiun-Jie Huang");
  MODULE_DESCRIPTION("GL620-USB-A Host-to-Host Link cables");
  MODULE_LICENSE("GPL");