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