1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/module.h>
24#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
26#include <linux/ethtool.h>
27#include <linux/workqueue.h>
28#include <linux/mii.h>
29#include <linux/usb.h>
30#include <linux/usb/usbnet.h>
31#include <linux/gfp.h>
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54#define GENELINK_CONNECT_WRITE 0xF0
55
56#define GENELINK_INTERRUPT_PIPE 0x03
57
58#define INTERRUPT_BUFSIZE 0x08
59
60#define GENELINK_INTERRUPT_INTERVAL 0x10
61
62#define GL_MAX_TRANSMIT_PACKETS 32
63
64#define GL_MAX_PACKET_LEN 1514
65
66#define GL_RCV_BUF_SIZE \
67 (((GL_MAX_PACKET_LEN + 4) * GL_MAX_TRANSMIT_PACKETS) + 4)
68
69struct gl_packet {
70 __le32 packet_length;
71 char packet_data [1];
72};
73
74struct gl_header {
75 __le32 packet_count;
76 struct gl_packet packets;
77};
78
79static int genelink_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
80{
81 struct gl_header *header;
82 struct gl_packet *packet;
83 struct sk_buff *gl_skb;
84 u32 size;
85 u32 count;
86
87
88 if (skb->len < dev->net->hard_header_len)
89 return 0;
90
91 header = (struct gl_header *) skb->data;
92
93
94 count = le32_to_cpu(header->packet_count);
95 if (count > GL_MAX_TRANSMIT_PACKETS) {
96 netdev_dbg(dev->net,
97 "genelink: invalid received packet count %u\n",
98 count);
99 return 0;
100 }
101
102
103 packet = &header->packets;
104
105
106 skb_pull(skb, 4);
107
108 while (count > 1) {
109
110 size = le32_to_cpu(packet->packet_length);
111
112
113 if (size > GL_MAX_PACKET_LEN) {
114 netdev_dbg(dev->net, "genelink: invalid rx length %d\n",
115 size);
116 return 0;
117 }
118
119
120 gl_skb = alloc_skb(size, GFP_ATOMIC);
121 if (gl_skb) {
122
123
124 skb_put_data(gl_skb, packet->packet_data, size);
125 usbnet_skb_return(dev, gl_skb);
126 }
127
128
129 packet = (struct gl_packet *)&packet->packet_data[size];
130 count--;
131
132
133 skb_pull(skb, size + 4);
134 }
135
136
137 skb_pull(skb, 4);
138
139 if (skb->len > GL_MAX_PACKET_LEN) {
140 netdev_dbg(dev->net, "genelink: invalid rx length %d\n",
141 skb->len);
142 return 0;
143 }
144 return 1;
145}
146
147static struct sk_buff *
148genelink_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
149{
150 int padlen;
151 int length = skb->len;
152 int headroom = skb_headroom(skb);
153 int tailroom = skb_tailroom(skb);
154 __le32 *packet_count;
155 __le32 *packet_len;
156
157
158 padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
159
160 if ((!skb_cloned(skb))
161 && ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
162 if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
163 skb->data = memmove(skb->head + (4 + 4*1),
164 skb->data, skb->len);
165 skb_set_tail_pointer(skb, skb->len);
166 }
167 } else {
168 struct sk_buff *skb2;
169 skb2 = skb_copy_expand(skb, (4 + 4*1) , padlen, flags);
170 dev_kfree_skb_any(skb);
171 skb = skb2;
172 if (!skb)
173 return NULL;
174 }
175
176
177 packet_count = skb_push(skb, (4 + 4 * 1));
178 packet_len = packet_count + 1;
179
180 *packet_count = cpu_to_le32(1);
181 *packet_len = cpu_to_le32(length);
182
183
184 if ((skb->len % dev->maxpacket) == 0)
185 skb_put(skb, 1);
186
187 return skb;
188}
189
190static int genelink_bind(struct usbnet *dev, struct usb_interface *intf)
191{
192 dev->hard_mtu = GL_RCV_BUF_SIZE;
193 dev->net->hard_header_len += 4;
194 dev->in = usb_rcvbulkpipe(dev->udev, dev->driver_info->in);
195 dev->out = usb_sndbulkpipe(dev->udev, dev->driver_info->out);
196 return 0;
197}
198
199static const struct driver_info genelink_info = {
200 .description = "Genesys GeneLink",
201 .flags = FLAG_POINTTOPOINT | FLAG_FRAMING_GL | FLAG_NO_SETINT,
202 .bind = genelink_bind,
203 .rx_fixup = genelink_rx_fixup,
204 .tx_fixup = genelink_tx_fixup,
205
206 .in = 1, .out = 2,
207
208#ifdef GENELINK_ACK
209 .check_connect =genelink_check_connect,
210#endif
211};
212
213static const struct usb_device_id products [] = {
214
215{
216 USB_DEVICE(0x05e3, 0x0502),
217 .driver_info = (unsigned long) &genelink_info,
218},
219
220
221
222 { },
223};
224MODULE_DEVICE_TABLE(usb, products);
225
226static struct usb_driver gl620a_driver = {
227 .name = "gl620a",
228 .id_table = products,
229 .probe = usbnet_probe,
230 .disconnect = usbnet_disconnect,
231 .suspend = usbnet_suspend,
232 .resume = usbnet_resume,
233 .disable_hub_initiated_lpm = 1,
234};
235
236module_usb_driver(gl620a_driver);
237
238MODULE_AUTHOR("Jiun-Jie Huang");
239MODULE_DESCRIPTION("GL620-USB-A Host-to-Host Link cables");
240MODULE_LICENSE("GPL");
241
242