Merge branch 'master' into for_paulus
[linux-drm-fsl-dcu.git] / drivers / usb / net / gl620a.c
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/sched.h>
26 #include <linux/init.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/ethtool.h>
30 #include <linux/workqueue.h>
31 #include <linux/mii.h>
32 #include <linux/usb.h>
33
34 #include "usbnet.h"
35
36
37 /*
38  * GeneSys GL620USB-A (www.genesyslogic.com.tw)
39  *
40  * ... should partially interop with the Win32 driver for this hardware.
41  * The GeneSys docs imply there's some NDIS issue motivating this framing.
42  *
43  * Some info from GeneSys:
44  *  - GL620USB-A is full duplex; GL620USB is only half duplex for bulk.
45  *    (Some cables, like the BAFO-100c, use the half duplex version.)
46  *  - For the full duplex model, the low bit of the version code says
47  *    which side is which ("left/right").
48  *  - For the half duplex type, a control/interrupt handshake settles
49  *    the transfer direction.  (That's disabled here, partially coded.)
50  *    A control URB would block until other side writes an interrupt.
51  *
52  * Original code from Jiun-Jie Huang <huangjj@genesyslogic.com.tw>
53  * and merged into "usbnet" by Stanislav Brabec <utx@penguin.cz>.
54  */
55
56 // control msg write command
57 #define GENELINK_CONNECT_WRITE                  0xF0
58 // interrupt pipe index
59 #define GENELINK_INTERRUPT_PIPE                 0x03
60 // interrupt read buffer size
61 #define INTERRUPT_BUFSIZE                       0x08
62 // interrupt pipe interval value
63 #define GENELINK_INTERRUPT_INTERVAL             0x10
64 // max transmit packet number per transmit
65 #define GL_MAX_TRANSMIT_PACKETS                 32
66 // max packet length
67 #define GL_MAX_PACKET_LEN                       1514
68 // max receive buffer size
69 #define GL_RCV_BUF_SIZE         \
70         (((GL_MAX_PACKET_LEN + 4) * GL_MAX_TRANSMIT_PACKETS) + 4)
71
72 struct gl_packet {
73         __le32          packet_length;
74         char            packet_data [1];
75 };
76
77 struct gl_header {
78         __le32                  packet_count;
79         struct gl_packet        packets;
80 };
81
82 static int genelink_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
83 {
84         struct gl_header        *header;
85         struct gl_packet        *packet;
86         struct sk_buff          *gl_skb;
87         u32                     size;
88         u32                     count;
89
90         header = (struct gl_header *) skb->data;
91
92         // get the packet count of the received skb
93         count = le32_to_cpu(header->packet_count);
94         if (count > GL_MAX_TRANSMIT_PACKETS) {
95                 dbg("genelink: invalid received packet count %u", count);
96                 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
105         while (count > 1) {
106                 // 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) {
111                         dbg("genelink: invalid rx length %d", size);
112                         return 0;
113                 }
114
115                 // allocate the skb for the individual packet
116                 gl_skb = alloc_skb(size, GFP_ATOMIC);
117                 if (gl_skb) {
118
119                         // copy the packet data to the new skb
120                         memcpy(skb_put(gl_skb, size),
121                                         packet->packet_data, size);
122                         usbnet_skb_return(dev, gl_skb);
123                 }
124
125                 // advance to the next packet
126                 packet = (struct gl_packet *)&packet->packet_data[size];
127                 count--;
128
129                 // shift the data pointer to the next gl_packet
130                 skb_pull(skb, size + 4);
131         }
132
133         // skip the packet length field 4 bytes
134         skb_pull(skb, 4);
135
136         if (skb->len > GL_MAX_PACKET_LEN) {
137                 dbg("genelink: invalid rx length %d", skb->len);
138                 return 0;
139         }
140         return 1;
141 }
142
143 static struct sk_buff *
144 genelink_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
145 {
146         int     padlen;
147         int     length = skb->len;
148         int     headroom = skb_headroom(skb);
149         int     tailroom = skb_tailroom(skb);
150         __le32  *packet_count;
151         __le32  *packet_len;
152
153         // FIXME:  magic numbers, bleech
154         padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
155
156         if ((!skb_cloned(skb))
157                         && ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
158                 if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
159                         skb->data = memmove(skb->head + (4 + 4*1),
160                                              skb->data, skb->len);
161                         skb->tail = skb->data + skb->len;
162                 }
163         } else {
164                 struct sk_buff  *skb2;
165                 skb2 = skb_copy_expand(skb, (4 + 4*1) , padlen, flags);
166                 dev_kfree_skb_any(skb);
167                 skb = skb2;
168                 if (!skb)
169                         return NULL;
170         }
171
172         // attach the packet count to the header
173         packet_count = (__le32 *) skb_push(skb, (4 + 4*1));
174         packet_len = packet_count + 1;
175
176         *packet_count = cpu_to_le32(1);
177         *packet_len = cpu_to_le32(length);
178
179         // add padding byte
180         if ((skb->len % dev->maxpacket) == 0)
181                 skb_put(skb, 1);
182
183         return skb;
184 }
185
186 static int genelink_bind(struct usbnet *dev, struct usb_interface *intf)
187 {
188         dev->hard_mtu = GL_RCV_BUF_SIZE;
189         dev->net->hard_header_len += 4;
190         dev->in = usb_rcvbulkpipe(dev->udev, dev->driver_info->in);
191         dev->out = usb_sndbulkpipe(dev->udev, dev->driver_info->out);
192         return 0;
193 }
194
195 static const struct driver_info genelink_info = {
196         .description =  "Genesys GeneLink",
197         .flags =        FLAG_FRAMING_GL | FLAG_NO_SETINT,
198         .bind =         genelink_bind,
199         .rx_fixup =     genelink_rx_fixup,
200         .tx_fixup =     genelink_tx_fixup,
201
202         .in = 1, .out = 2,
203
204 #ifdef  GENELINK_ACK
205         .check_connect =genelink_check_connect,
206 #endif
207 };
208
209 static const struct usb_device_id       products [] = {
210
211 {
212         USB_DEVICE(0x05e3, 0x0502),     // GL620USB-A
213         .driver_info =  (unsigned long) &genelink_info,
214 },
215         /* NOT: USB_DEVICE(0x05e3, 0x0501),     // GL620USB
216          * that's half duplex, not currently supported
217          */
218         { },            // END
219 };
220 MODULE_DEVICE_TABLE(usb, products);
221
222 static struct usb_driver gl620a_driver = {
223         .name =         "gl620a",
224         .id_table =     products,
225         .probe =        usbnet_probe,
226         .disconnect =   usbnet_disconnect,
227         .suspend =      usbnet_suspend,
228         .resume =       usbnet_resume,
229 };
230
231 static int __init usbnet_init(void)
232 {
233         return usb_register(&gl620a_driver);
234 }
235 module_init(usbnet_init);
236
237 static void __exit usbnet_exit(void)
238 {
239         usb_deregister(&gl620a_driver);
240 }
241 module_exit(usbnet_exit);
242
243 MODULE_AUTHOR("Jiun-Jie Huang");
244 MODULE_DESCRIPTION("GL620-USB-A Host-to-Host Link cables");
245 MODULE_LICENSE("GPL");
246