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