David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 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 |
Jeff Kirsher | 9cb0007 | 2013-12-06 06:28:46 -0800 | [diff] [blame] | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | // #define DEBUG // error path messages, extra info |
| 21 | // #define VERBOSE // more; success messages |
| 22 | |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 23 | #include <linux/module.h> |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 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> |
Jussi Kivilinna | 3692e94 | 2008-01-26 00:51:45 +0200 | [diff] [blame] | 30 | #include <linux/usb/usbnet.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/gfp.h> |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 32 | |
| 33 | |
| 34 | /* |
| 35 | * GeneSys GL620USB-A (www.genesyslogic.com.tw) |
| 36 | * |
| 37 | * ... should partially interop with the Win32 driver for this hardware. |
| 38 | * The GeneSys docs imply there's some NDIS issue motivating this framing. |
| 39 | * |
| 40 | * Some info from GeneSys: |
| 41 | * - GL620USB-A is full duplex; GL620USB is only half duplex for bulk. |
| 42 | * (Some cables, like the BAFO-100c, use the half duplex version.) |
| 43 | * - For the full duplex model, the low bit of the version code says |
| 44 | * which side is which ("left/right"). |
| 45 | * - For the half duplex type, a control/interrupt handshake settles |
| 46 | * the transfer direction. (That's disabled here, partially coded.) |
| 47 | * A control URB would block until other side writes an interrupt. |
| 48 | * |
| 49 | * Original code from Jiun-Jie Huang <huangjj@genesyslogic.com.tw> |
| 50 | * and merged into "usbnet" by Stanislav Brabec <utx@penguin.cz>. |
| 51 | */ |
| 52 | |
| 53 | // control msg write command |
| 54 | #define GENELINK_CONNECT_WRITE 0xF0 |
| 55 | // interrupt pipe index |
| 56 | #define GENELINK_INTERRUPT_PIPE 0x03 |
| 57 | // interrupt read buffer size |
| 58 | #define INTERRUPT_BUFSIZE 0x08 |
| 59 | // interrupt pipe interval value |
| 60 | #define GENELINK_INTERRUPT_INTERVAL 0x10 |
| 61 | // max transmit packet number per transmit |
| 62 | #define GL_MAX_TRANSMIT_PACKETS 32 |
| 63 | // max packet length |
| 64 | #define GL_MAX_PACKET_LEN 1514 |
| 65 | // max receive buffer size |
| 66 | #define GL_RCV_BUF_SIZE \ |
| 67 | (((GL_MAX_PACKET_LEN + 4) * GL_MAX_TRANSMIT_PACKETS) + 4) |
| 68 | |
| 69 | struct gl_packet { |
Al Viro | 53ebb3b | 2007-02-09 16:39:55 +0000 | [diff] [blame] | 70 | __le32 packet_length; |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 71 | char packet_data [1]; |
| 72 | }; |
| 73 | |
| 74 | struct gl_header { |
Al Viro | 53ebb3b | 2007-02-09 16:39:55 +0000 | [diff] [blame] | 75 | __le32 packet_count; |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 76 | struct gl_packet packets; |
| 77 | }; |
| 78 | |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 79 | static 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; |
Al Viro | 53ebb3b | 2007-02-09 16:39:55 +0000 | [diff] [blame] | 85 | u32 count; |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 86 | |
Emil Goode | eb85569 | 2014-02-13 17:50:19 +0100 | [diff] [blame] | 87 | /* This check is no longer done by usbnet */ |
| 88 | if (skb->len < dev->net->hard_header_len) |
| 89 | return 0; |
| 90 | |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 91 | header = (struct gl_header *) skb->data; |
| 92 | |
| 93 | // get the packet count of the received skb |
Al Viro | 53ebb3b | 2007-02-09 16:39:55 +0000 | [diff] [blame] | 94 | count = le32_to_cpu(header->packet_count); |
| 95 | if (count > GL_MAX_TRANSMIT_PACKETS) { |
Greg Kroah-Hartman | 49ae25b | 2012-09-19 09:46:14 +0000 | [diff] [blame] | 96 | netdev_dbg(dev->net, |
| 97 | "genelink: invalid received packet count %u\n", |
| 98 | count); |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | // set the current packet pointer to the first packet |
| 103 | packet = &header->packets; |
| 104 | |
| 105 | // decrement the length for the packet count size 4 bytes |
| 106 | skb_pull(skb, 4); |
| 107 | |
Al Viro | 53ebb3b | 2007-02-09 16:39:55 +0000 | [diff] [blame] | 108 | while (count > 1) { |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 109 | // get the packet length |
| 110 | size = le32_to_cpu(packet->packet_length); |
| 111 | |
| 112 | // this may be a broken packet |
| 113 | if (size > GL_MAX_PACKET_LEN) { |
Greg Kroah-Hartman | 49ae25b | 2012-09-19 09:46:14 +0000 | [diff] [blame] | 114 | netdev_dbg(dev->net, "genelink: invalid rx length %d\n", |
| 115 | size); |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | // allocate the skb for the individual packet |
| 120 | gl_skb = alloc_skb(size, GFP_ATOMIC); |
| 121 | if (gl_skb) { |
| 122 | |
| 123 | // copy the packet data to the new skb |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 124 | skb_put_data(gl_skb, packet->packet_data, size); |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 125 | usbnet_skb_return(dev, gl_skb); |
| 126 | } |
| 127 | |
| 128 | // advance to the next packet |
Al Viro | 53ebb3b | 2007-02-09 16:39:55 +0000 | [diff] [blame] | 129 | packet = (struct gl_packet *)&packet->packet_data[size]; |
| 130 | count--; |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 131 | |
| 132 | // shift the data pointer to the next gl_packet |
| 133 | skb_pull(skb, size + 4); |
| 134 | } |
| 135 | |
| 136 | // skip the packet length field 4 bytes |
| 137 | skb_pull(skb, 4); |
| 138 | |
| 139 | if (skb->len > GL_MAX_PACKET_LEN) { |
Greg Kroah-Hartman | 49ae25b | 2012-09-19 09:46:14 +0000 | [diff] [blame] | 140 | netdev_dbg(dev->net, "genelink: invalid rx length %d\n", |
| 141 | skb->len); |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | return 1; |
| 145 | } |
| 146 | |
| 147 | static struct sk_buff * |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 148 | genelink_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags) |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 149 | { |
| 150 | int padlen; |
| 151 | int length = skb->len; |
| 152 | int headroom = skb_headroom(skb); |
| 153 | int tailroom = skb_tailroom(skb); |
Al Viro | 53ebb3b | 2007-02-09 16:39:55 +0000 | [diff] [blame] | 154 | __le32 *packet_count; |
| 155 | __le32 *packet_len; |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 156 | |
| 157 | // FIXME: magic numbers, bleech |
| 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); |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 165 | skb_set_tail_pointer(skb, skb->len); |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 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 | // attach the packet count to the header |
Johannes Berg | d58ff35 | 2017-06-16 14:29:23 +0200 | [diff] [blame] | 177 | packet_count = skb_push(skb, (4 + 4 * 1)); |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 178 | packet_len = packet_count + 1; |
| 179 | |
| 180 | *packet_count = cpu_to_le32(1); |
| 181 | *packet_len = cpu_to_le32(length); |
| 182 | |
| 183 | // add padding byte |
| 184 | if ((skb->len % dev->maxpacket) == 0) |
| 185 | skb_put(skb, 1); |
| 186 | |
| 187 | return skb; |
| 188 | } |
| 189 | |
| 190 | static 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 | |
| 199 | static const struct driver_info genelink_info = { |
| 200 | .description = "Genesys GeneLink", |
Arnd Bergmann | c261344 | 2011-04-01 20:12:02 -0700 | [diff] [blame] | 201 | .flags = FLAG_POINTTOPOINT | FLAG_FRAMING_GL | FLAG_NO_SETINT, |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 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 | |
| 213 | static const struct usb_device_id products [] = { |
| 214 | |
| 215 | { |
| 216 | USB_DEVICE(0x05e3, 0x0502), // GL620USB-A |
| 217 | .driver_info = (unsigned long) &genelink_info, |
| 218 | }, |
| 219 | /* NOT: USB_DEVICE(0x05e3, 0x0501), // GL620USB |
| 220 | * that's half duplex, not currently supported |
| 221 | */ |
| 222 | { }, // END |
| 223 | }; |
| 224 | MODULE_DEVICE_TABLE(usb, products); |
| 225 | |
| 226 | static struct usb_driver gl620a_driver = { |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 227 | .name = "gl620a", |
| 228 | .id_table = products, |
| 229 | .probe = usbnet_probe, |
| 230 | .disconnect = usbnet_disconnect, |
| 231 | .suspend = usbnet_suspend, |
| 232 | .resume = usbnet_resume, |
Sarah Sharp | e1f12eb | 2012-04-23 10:08:51 -0700 | [diff] [blame] | 233 | .disable_hub_initiated_lpm = 1, |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 234 | }; |
| 235 | |
Greg Kroah-Hartman | d632eb1 | 2011-11-18 09:44:20 -0800 | [diff] [blame] | 236 | module_usb_driver(gl620a_driver); |
David Brownell | 47ee305 | 2005-08-31 09:53:42 -0700 | [diff] [blame] | 237 | |
| 238 | MODULE_AUTHOR("Jiun-Jie Huang"); |
| 239 | MODULE_DESCRIPTION("GL620-USB-A Host-to-Host Link cables"); |
| 240 | MODULE_LICENSE("GPL"); |
| 241 | |