Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * GeneSys GL620USB-A based links
0004  * Copyright (C) 2001 by Jiun-Jie Huang <huangjj@genesyslogic.com.tw>
0005  * Copyright (C) 2001 by Stanislav Brabec <utx@penguin.cz>
0006  */
0007 
0008 // #define  DEBUG           // error path messages, extra info
0009 // #define  VERBOSE         // more; success messages
0010 
0011 #include <linux/module.h>
0012 #include <linux/netdevice.h>
0013 #include <linux/etherdevice.h>
0014 #include <linux/ethtool.h>
0015 #include <linux/workqueue.h>
0016 #include <linux/mii.h>
0017 #include <linux/usb.h>
0018 #include <linux/usb/usbnet.h>
0019 #include <linux/gfp.h>
0020 
0021 
0022 /*
0023  * GeneSys GL620USB-A (www.genesyslogic.com.tw)
0024  *
0025  * ... should partially interop with the Win32 driver for this hardware.
0026  * The GeneSys docs imply there's some NDIS issue motivating this framing.
0027  *
0028  * Some info from GeneSys:
0029  *  - GL620USB-A is full duplex; GL620USB is only half duplex for bulk.
0030  *    (Some cables, like the BAFO-100c, use the half duplex version.)
0031  *  - For the full duplex model, the low bit of the version code says
0032  *    which side is which ("left/right").
0033  *  - For the half duplex type, a control/interrupt handshake settles
0034  *    the transfer direction.  (That's disabled here, partially coded.)
0035  *    A control URB would block until other side writes an interrupt.
0036  *
0037  * Original code from Jiun-Jie Huang <huangjj@genesyslogic.com.tw>
0038  * and merged into "usbnet" by Stanislav Brabec <utx@penguin.cz>.
0039  */
0040 
0041 // control msg write command
0042 #define GENELINK_CONNECT_WRITE          0xF0
0043 // interrupt pipe index
0044 #define GENELINK_INTERRUPT_PIPE         0x03
0045 // interrupt read buffer size
0046 #define INTERRUPT_BUFSIZE           0x08
0047 // interrupt pipe interval value
0048 #define GENELINK_INTERRUPT_INTERVAL     0x10
0049 // max transmit packet number per transmit
0050 #define GL_MAX_TRANSMIT_PACKETS         32
0051 // max packet length
0052 #define GL_MAX_PACKET_LEN           1514
0053 // max receive buffer size
0054 #define GL_RCV_BUF_SIZE     \
0055     (((GL_MAX_PACKET_LEN + 4) * GL_MAX_TRANSMIT_PACKETS) + 4)
0056 
0057 struct gl_packet {
0058     __le32      packet_length;
0059     char        packet_data[];
0060 };
0061 
0062 struct gl_header {
0063     __le32          packet_count;
0064     struct gl_packet    packets;
0065 };
0066 
0067 static int genelink_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
0068 {
0069     struct gl_header    *header;
0070     struct gl_packet    *packet;
0071     struct sk_buff      *gl_skb;
0072     u32         size;
0073     u32         count;
0074 
0075     /* This check is no longer done by usbnet */
0076     if (skb->len < dev->net->hard_header_len)
0077         return 0;
0078 
0079     header = (struct gl_header *) skb->data;
0080 
0081     // get the packet count of the received skb
0082     count = le32_to_cpu(header->packet_count);
0083     if (count > GL_MAX_TRANSMIT_PACKETS) {
0084         netdev_dbg(dev->net,
0085                "genelink: invalid received packet count %u\n",
0086                count);
0087         return 0;
0088     }
0089 
0090     // set the current packet pointer to the first packet
0091     packet = &header->packets;
0092 
0093     // decrement the length for the packet count size 4 bytes
0094     skb_pull(skb, 4);
0095 
0096     while (count > 1) {
0097         // get the packet length
0098         size = le32_to_cpu(packet->packet_length);
0099 
0100         // this may be a broken packet
0101         if (size > GL_MAX_PACKET_LEN) {
0102             netdev_dbg(dev->net, "genelink: invalid rx length %d\n",
0103                    size);
0104             return 0;
0105         }
0106 
0107         // allocate the skb for the individual packet
0108         gl_skb = alloc_skb(size, GFP_ATOMIC);
0109         if (gl_skb) {
0110 
0111             // copy the packet data to the new skb
0112             skb_put_data(gl_skb, packet->packet_data, size);
0113             usbnet_skb_return(dev, gl_skb);
0114         }
0115 
0116         // advance to the next packet
0117         packet = (struct gl_packet *)&packet->packet_data[size];
0118         count--;
0119 
0120         // shift the data pointer to the next gl_packet
0121         skb_pull(skb, size + 4);
0122     }
0123 
0124     // skip the packet length field 4 bytes
0125     skb_pull(skb, 4);
0126 
0127     if (skb->len > GL_MAX_PACKET_LEN) {
0128         netdev_dbg(dev->net, "genelink: invalid rx length %d\n",
0129                skb->len);
0130         return 0;
0131     }
0132     return 1;
0133 }
0134 
0135 static struct sk_buff *
0136 genelink_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
0137 {
0138     int     padlen;
0139     int length = skb->len;
0140     int headroom = skb_headroom(skb);
0141     int tailroom = skb_tailroom(skb);
0142     __le32  *packet_count;
0143     __le32  *packet_len;
0144 
0145     // FIXME:  magic numbers, bleech
0146     padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
0147 
0148     if ((!skb_cloned(skb))
0149             && ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
0150         if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
0151             skb->data = memmove(skb->head + (4 + 4*1),
0152                          skb->data, skb->len);
0153             skb_set_tail_pointer(skb, skb->len);
0154         }
0155     } else {
0156         struct sk_buff  *skb2;
0157         skb2 = skb_copy_expand(skb, (4 + 4*1) , padlen, flags);
0158         dev_kfree_skb_any(skb);
0159         skb = skb2;
0160         if (!skb)
0161             return NULL;
0162     }
0163 
0164     // attach the packet count to the header
0165     packet_count = skb_push(skb, (4 + 4 * 1));
0166     packet_len = packet_count + 1;
0167 
0168     *packet_count = cpu_to_le32(1);
0169     *packet_len = cpu_to_le32(length);
0170 
0171     // add padding byte
0172     if ((skb->len % dev->maxpacket) == 0)
0173         skb_put(skb, 1);
0174 
0175     return skb;
0176 }
0177 
0178 static int genelink_bind(struct usbnet *dev, struct usb_interface *intf)
0179 {
0180     dev->hard_mtu = GL_RCV_BUF_SIZE;
0181     dev->net->hard_header_len += 4;
0182     dev->in = usb_rcvbulkpipe(dev->udev, dev->driver_info->in);
0183     dev->out = usb_sndbulkpipe(dev->udev, dev->driver_info->out);
0184     return 0;
0185 }
0186 
0187 static const struct driver_info genelink_info = {
0188     .description =  "Genesys GeneLink",
0189     .flags =    FLAG_POINTTOPOINT | FLAG_FRAMING_GL | FLAG_NO_SETINT,
0190     .bind =     genelink_bind,
0191     .rx_fixup = genelink_rx_fixup,
0192     .tx_fixup = genelink_tx_fixup,
0193 
0194     .in = 1, .out = 2,
0195 
0196 #ifdef  GENELINK_ACK
0197     .check_connect =genelink_check_connect,
0198 #endif
0199 };
0200 
0201 static const struct usb_device_id   products [] = {
0202 
0203 {
0204     USB_DEVICE(0x05e3, 0x0502), // GL620USB-A
0205     .driver_info =  (unsigned long) &genelink_info,
0206 },
0207     /* NOT: USB_DEVICE(0x05e3, 0x0501), // GL620USB
0208      * that's half duplex, not currently supported
0209      */
0210     { },        // END
0211 };
0212 MODULE_DEVICE_TABLE(usb, products);
0213 
0214 static struct usb_driver gl620a_driver = {
0215     .name =     "gl620a",
0216     .id_table = products,
0217     .probe =    usbnet_probe,
0218     .disconnect =   usbnet_disconnect,
0219     .suspend =  usbnet_suspend,
0220     .resume =   usbnet_resume,
0221     .disable_hub_initiated_lpm = 1,
0222 };
0223 
0224 module_usb_driver(gl620a_driver);
0225 
0226 MODULE_AUTHOR("Jiun-Jie Huang");
0227 MODULE_DESCRIPTION("GL620-USB-A Host-to-Host Link cables");
0228 MODULE_LICENSE("GPL");
0229