This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / usb / net / net1080.c
1 /*
2  * Net1080 based USB host-to-host cables
3  * Copyright (C) 2000-2005 by David Brownell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 // #define      DEBUG                   // error path messages, extra info
21 // #define      VERBOSE                 // more; success messages
22
23 #include <linux/config.h>
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 <asm/unaligned.h>
35
36 #include "usbnet.h"
37
38
39 /*
40  * Netchip 1080 driver ... http://www.netchip.com
41  * (Sept 2004:  End-of-life announcement has been sent.)
42  * Used in (some) LapLink cables
43  */
44
45 #define frame_errors    data[1]
46
47 /*
48  * NetChip framing of ethernet packets, supporting additional error
49  * checks for links that may drop bulk packets from inside messages.
50  * Odd USB length == always short read for last usb packet.
51  *      - nc_header
52  *      - Ethernet header (14 bytes)
53  *      - payload
54  *      - (optional padding byte, if needed so length becomes odd)
55  *      - nc_trailer
56  *
57  * This framing is to be avoided for non-NetChip devices.
58  */
59
60 struct nc_header {              // packed:
61         __le16  hdr_len;                // sizeof nc_header (LE, all)
62         __le16  packet_len;             // payload size (including ethhdr)
63         __le16  packet_id;              // detects dropped packets
64 #define MIN_HEADER      6
65
66         // all else is optional, and must start with:
67         // __le16       vendorId;       // from usb-if
68         // __le16       productId;
69 } __attribute__((__packed__));
70
71 #define PAD_BYTE        ((unsigned char)0xAC)
72
73 struct nc_trailer {
74         __le16  packet_id;
75 } __attribute__((__packed__));
76
77 // packets may use FLAG_FRAMING_NC and optional pad
78 #define FRAMED_SIZE(mtu) (sizeof (struct nc_header) \
79                                 + sizeof (struct ethhdr) \
80                                 + (mtu) \
81                                 + 1 \
82                                 + sizeof (struct nc_trailer))
83
84 #define MIN_FRAMED      FRAMED_SIZE(0)
85
86 /* packets _could_ be up to 64KB... */
87 #define NC_MAX_PACKET   32767
88
89
90 /*
91  * Zero means no timeout; else, how long a 64 byte bulk packet may be queued
92  * before the hardware drops it.  If that's done, the driver will need to
93  * frame network packets to guard against the dropped USB packets.  The win32
94  * driver sets this for both sides of the link.
95  */
96 #define NC_READ_TTL_MS  ((u8)255)       // ms
97
98 /*
99  * We ignore most registers and EEPROM contents.
100  */
101 #define REG_USBCTL      ((u8)0x04)
102 #define REG_TTL         ((u8)0x10)
103 #define REG_STATUS      ((u8)0x11)
104
105 /*
106  * Vendor specific requests to read/write data
107  */
108 #define REQUEST_REGISTER        ((u8)0x10)
109 #define REQUEST_EEPROM          ((u8)0x11)
110
111 static int
112 nc_vendor_read(struct usbnet *dev, u8 req, u8 regnum, u16 *retval_ptr)
113 {
114         int status = usb_control_msg(dev->udev,
115                 usb_rcvctrlpipe(dev->udev, 0),
116                 req,
117                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
118                 0, regnum,
119                 retval_ptr, sizeof *retval_ptr,
120                 USB_CTRL_GET_TIMEOUT);
121         if (status > 0)
122                 status = 0;
123         if (!status)
124                 le16_to_cpus(retval_ptr);
125         return status;
126 }
127
128 static inline int
129 nc_register_read(struct usbnet *dev, u8 regnum, u16 *retval_ptr)
130 {
131         return nc_vendor_read(dev, REQUEST_REGISTER, regnum, retval_ptr);
132 }
133
134 // no retval ... can become async, usable in_interrupt()
135 static void
136 nc_vendor_write(struct usbnet *dev, u8 req, u8 regnum, u16 value)
137 {
138         usb_control_msg(dev->udev,
139                 usb_sndctrlpipe(dev->udev, 0),
140                 req,
141                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
142                 value, regnum,
143                 NULL, 0,                        // data is in setup packet
144                 USB_CTRL_SET_TIMEOUT);
145 }
146
147 static inline void
148 nc_register_write(struct usbnet *dev, u8 regnum, u16 value)
149 {
150         nc_vendor_write(dev, REQUEST_REGISTER, regnum, value);
151 }
152
153
154 #if 0
155 static void nc_dump_registers(struct usbnet *dev)
156 {
157         u8      reg;
158         u16     *vp = kmalloc(sizeof (u16));
159
160         if (!vp) {
161                 dbg("no memory?");
162                 return;
163         }
164
165         dbg("%s registers:", dev->net->name);
166         for (reg = 0; reg < 0x20; reg++) {
167                 int retval;
168
169                 // reading some registers is trouble
170                 if (reg >= 0x08 && reg <= 0xf)
171                         continue;
172                 if (reg >= 0x12 && reg <= 0x1e)
173                         continue;
174
175                 retval = nc_register_read(dev, reg, vp);
176                 if (retval < 0)
177                         dbg("%s reg [0x%x] ==> error %d",
178                                 dev->net->name, reg, retval);
179                 else
180                         dbg("%s reg [0x%x] = 0x%x",
181                                 dev->net->name, reg, *vp);
182         }
183         kfree(vp);
184 }
185 #endif
186
187
188 /*-------------------------------------------------------------------------*/
189
190 /*
191  * Control register
192  */
193
194 #define USBCTL_WRITABLE_MASK    0x1f0f
195 // bits 15-13 reserved, r/o
196 #define USBCTL_ENABLE_LANG      (1 << 12)
197 #define USBCTL_ENABLE_MFGR      (1 << 11)
198 #define USBCTL_ENABLE_PROD      (1 << 10)
199 #define USBCTL_ENABLE_SERIAL    (1 << 9)
200 #define USBCTL_ENABLE_DEFAULTS  (1 << 8)
201 // bits 7-4 reserved, r/o
202 #define USBCTL_FLUSH_OTHER      (1 << 3)
203 #define USBCTL_FLUSH_THIS       (1 << 2)
204 #define USBCTL_DISCONN_OTHER    (1 << 1)
205 #define USBCTL_DISCONN_THIS     (1 << 0)
206
207 static inline void nc_dump_usbctl(struct usbnet *dev, u16 usbctl)
208 {
209         if (!netif_msg_link(dev))
210                 return;
211         devdbg(dev, "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s;"
212                         " this%s%s;"
213                         " other%s%s; r/o 0x%x",
214                 dev->udev->bus->bus_name, dev->udev->devpath,
215                 usbctl,
216                 (usbctl & USBCTL_ENABLE_LANG) ? " lang" : "",
217                 (usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "",
218                 (usbctl & USBCTL_ENABLE_PROD) ? " prod" : "",
219                 (usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "",
220                 (usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "",
221
222                 (usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "",
223                 (usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "",
224                 (usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "",
225                 (usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "",
226                 usbctl & ~USBCTL_WRITABLE_MASK
227                 );
228 }
229
230 /*-------------------------------------------------------------------------*/
231
232 /*
233  * Status register
234  */
235
236 #define STATUS_PORT_A           (1 << 15)
237
238 #define STATUS_CONN_OTHER       (1 << 14)
239 #define STATUS_SUSPEND_OTHER    (1 << 13)
240 #define STATUS_MAILBOX_OTHER    (1 << 12)
241 #define STATUS_PACKETS_OTHER(n) (((n) >> 8) && 0x03)
242
243 #define STATUS_CONN_THIS        (1 << 6)
244 #define STATUS_SUSPEND_THIS     (1 << 5)
245 #define STATUS_MAILBOX_THIS     (1 << 4)
246 #define STATUS_PACKETS_THIS(n)  (((n) >> 0) && 0x03)
247
248 #define STATUS_UNSPEC_MASK      0x0c8c
249 #define STATUS_NOISE_MASK       ((u16)~(0x0303|STATUS_UNSPEC_MASK))
250
251
252 static inline void nc_dump_status(struct usbnet *dev, u16 status)
253 {
254         if (!netif_msg_link(dev))
255                 return;
256         devdbg(dev, "net1080 %s-%s status 0x%x:"
257                         " this (%c) PKT=%d%s%s%s;"
258                         " other PKT=%d%s%s%s; unspec 0x%x",
259                 dev->udev->bus->bus_name, dev->udev->devpath,
260                 status,
261
262                 // XXX the packet counts don't seem right
263                 // (1 at reset, not 0); maybe UNSPEC too
264
265                 (status & STATUS_PORT_A) ? 'A' : 'B',
266                 STATUS_PACKETS_THIS(status),
267                 (status & STATUS_CONN_THIS) ? " CON" : "",
268                 (status & STATUS_SUSPEND_THIS) ? " SUS" : "",
269                 (status & STATUS_MAILBOX_THIS) ? " MBOX" : "",
270
271                 STATUS_PACKETS_OTHER(status),
272                 (status & STATUS_CONN_OTHER) ? " CON" : "",
273                 (status & STATUS_SUSPEND_OTHER) ? " SUS" : "",
274                 (status & STATUS_MAILBOX_OTHER) ? " MBOX" : "",
275
276                 status & STATUS_UNSPEC_MASK
277                 );
278 }
279
280 /*-------------------------------------------------------------------------*/
281
282 /*
283  * TTL register
284  */
285
286 #define TTL_THIS(ttl)   (0x00ff & ttl)
287 #define TTL_OTHER(ttl)  (0x00ff & (ttl >> 8))
288 #define MK_TTL(this,other)      ((u16)(((other)<<8)|(0x00ff&(this))))
289
290 static inline void nc_dump_ttl(struct usbnet *dev, u16 ttl)
291 {
292         if (netif_msg_link(dev))
293                 devdbg(dev, "net1080 %s-%s ttl 0x%x this = %d, other = %d",
294                         dev->udev->bus->bus_name, dev->udev->devpath,
295                         ttl, TTL_THIS(ttl), TTL_OTHER(ttl));
296 }
297
298 /*-------------------------------------------------------------------------*/
299
300 static int net1080_reset(struct usbnet *dev)
301 {
302         u16             usbctl, status, ttl;
303         u16             *vp = kmalloc(sizeof (u16), GFP_KERNEL);
304         int             retval;
305
306         if (!vp)
307                 return -ENOMEM;
308
309         // nc_dump_registers(dev);
310
311         if ((retval = nc_register_read(dev, REG_STATUS, vp)) < 0) {
312                 dbg("can't read %s-%s status: %d",
313                         dev->udev->bus->bus_name, dev->udev->devpath, retval);
314                 goto done;
315         }
316         status = *vp;
317         nc_dump_status(dev, status);
318
319         if ((retval = nc_register_read(dev, REG_USBCTL, vp)) < 0) {
320                 dbg("can't read USBCTL, %d", retval);
321                 goto done;
322         }
323         usbctl = *vp;
324         nc_dump_usbctl(dev, usbctl);
325
326         nc_register_write(dev, REG_USBCTL,
327                         USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER);
328
329         if ((retval = nc_register_read(dev, REG_TTL, vp)) < 0) {
330                 dbg("can't read TTL, %d", retval);
331                 goto done;
332         }
333         ttl = *vp;
334         // nc_dump_ttl(dev, ttl);
335
336         nc_register_write(dev, REG_TTL,
337                         MK_TTL(NC_READ_TTL_MS, TTL_OTHER(ttl)) );
338         dbg("%s: assigned TTL, %d ms", dev->net->name, NC_READ_TTL_MS);
339
340         if (netif_msg_link(dev))
341                 devinfo(dev, "port %c, peer %sconnected",
342                         (status & STATUS_PORT_A) ? 'A' : 'B',
343                         (status & STATUS_CONN_OTHER) ? "" : "dis"
344                         );
345         retval = 0;
346
347 done:
348         kfree(vp);
349         return retval;
350 }
351
352 static int net1080_check_connect(struct usbnet *dev)
353 {
354         int                     retval;
355         u16                     status;
356         u16                     *vp = kmalloc(sizeof (u16), GFP_KERNEL);
357
358         if (!vp)
359                 return -ENOMEM;
360         retval = nc_register_read(dev, REG_STATUS, vp);
361         status = *vp;
362         kfree(vp);
363         if (retval != 0) {
364                 dbg("%s net1080_check_conn read - %d", dev->net->name, retval);
365                 return retval;
366         }
367         if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER)
368                 return -ENOLINK;
369         return 0;
370 }
371
372 static void nc_flush_complete(struct urb *urb, struct pt_regs *regs)
373 {
374         kfree(urb->context);
375         usb_free_urb(urb);
376 }
377
378 static void nc_ensure_sync(struct usbnet *dev)
379 {
380         dev->frame_errors++;
381         if (dev->frame_errors > 5) {
382                 struct urb              *urb;
383                 struct usb_ctrlrequest  *req;
384                 int                     status;
385
386                 /* Send a flush */
387                 urb = usb_alloc_urb(0, SLAB_ATOMIC);
388                 if (!urb)
389                         return;
390
391                 req = kmalloc(sizeof *req, GFP_ATOMIC);
392                 if (!req) {
393                         usb_free_urb(urb);
394                         return;
395                 }
396
397                 req->bRequestType = USB_DIR_OUT
398                         | USB_TYPE_VENDOR
399                         | USB_RECIP_DEVICE;
400                 req->bRequest = REQUEST_REGISTER;
401                 req->wValue = cpu_to_le16(USBCTL_FLUSH_THIS
402                                 | USBCTL_FLUSH_OTHER);
403                 req->wIndex = cpu_to_le16(REG_USBCTL);
404                 req->wLength = cpu_to_le16(0);
405
406                 /* queue an async control request, we don't need
407                  * to do anything when it finishes except clean up.
408                  */
409                 usb_fill_control_urb(urb, dev->udev,
410                         usb_sndctrlpipe(dev->udev, 0),
411                         (unsigned char *) req,
412                         NULL, 0,
413                         nc_flush_complete, req);
414                 status = usb_submit_urb(urb, GFP_ATOMIC);
415                 if (status) {
416                         kfree(req);
417                         usb_free_urb(urb);
418                         return;
419                 }
420
421                 if (netif_msg_rx_err(dev))
422                         devdbg(dev, "flush net1080; too many framing errors");
423                 dev->frame_errors = 0;
424         }
425 }
426
427 static int net1080_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
428 {
429         struct nc_header        *header;
430         struct nc_trailer       *trailer;
431         u16                     hdr_len, packet_len;
432
433         if (!(skb->len & 0x01)) {
434 #ifdef DEBUG
435                 struct net_device       *net = dev->net;
436                 dbg("rx framesize %d range %d..%d mtu %d", skb->len,
437                         net->hard_header_len, dev->hard_mtu, net->mtu);
438 #endif
439                 dev->stats.rx_frame_errors++;
440                 nc_ensure_sync(dev);
441                 return 0;
442         }
443
444         header = (struct nc_header *) skb->data;
445         hdr_len = le16_to_cpup(&header->hdr_len);
446         packet_len = le16_to_cpup(&header->packet_len);
447         if (FRAMED_SIZE(packet_len) > NC_MAX_PACKET) {
448                 dev->stats.rx_frame_errors++;
449                 dbg("packet too big, %d", packet_len);
450                 nc_ensure_sync(dev);
451                 return 0;
452         } else if (hdr_len < MIN_HEADER) {
453                 dev->stats.rx_frame_errors++;
454                 dbg("header too short, %d", hdr_len);
455                 nc_ensure_sync(dev);
456                 return 0;
457         } else if (hdr_len > MIN_HEADER) {
458                 // out of band data for us?
459                 dbg("header OOB, %d bytes", hdr_len - MIN_HEADER);
460                 nc_ensure_sync(dev);
461                 // switch (vendor/product ids) { ... }
462         }
463         skb_pull(skb, hdr_len);
464
465         trailer = (struct nc_trailer *)
466                 (skb->data + skb->len - sizeof *trailer);
467         skb_trim(skb, skb->len - sizeof *trailer);
468
469         if ((packet_len & 0x01) == 0) {
470                 if (skb->data [packet_len] != PAD_BYTE) {
471                         dev->stats.rx_frame_errors++;
472                         dbg("bad pad");
473                         return 0;
474                 }
475                 skb_trim(skb, skb->len - 1);
476         }
477         if (skb->len != packet_len) {
478                 dev->stats.rx_frame_errors++;
479                 dbg("bad packet len %d (expected %d)",
480                         skb->len, packet_len);
481                 nc_ensure_sync(dev);
482                 return 0;
483         }
484         if (header->packet_id != get_unaligned(&trailer->packet_id)) {
485                 dev->stats.rx_fifo_errors++;
486                 dbg("(2+ dropped) rx packet_id mismatch 0x%x 0x%x",
487                         le16_to_cpu(header->packet_id),
488                         le16_to_cpu(trailer->packet_id));
489                 return 0;
490         }
491 #if 0
492         devdbg(dev, "frame <rx h %d p %d id %d", header->hdr_len,
493                 header->packet_len, header->packet_id);
494 #endif
495         dev->frame_errors = 0;
496         return 1;
497 }
498
499 static struct sk_buff *
500 net1080_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
501 {
502         int                     padlen;
503         struct sk_buff          *skb2;
504         struct nc_header        *header = NULL;
505         struct nc_trailer       *trailer = NULL;
506         int                     len = skb->len;
507
508         padlen = ((len + sizeof (struct nc_header)
509                         + sizeof (struct nc_trailer)) & 0x01) ? 0 : 1;
510         if (!skb_cloned(skb)) {
511                 int     headroom = skb_headroom(skb);
512                 int     tailroom = skb_tailroom(skb);
513
514                 if ((padlen + sizeof (struct nc_trailer)) <= tailroom
515                             && sizeof (struct nc_header) <= headroom)
516                         /* There's enough head and tail room */
517                         goto encapsulate;
518
519                 if ((sizeof (struct nc_header) + padlen
520                                         + sizeof (struct nc_trailer)) <
521                                 (headroom + tailroom)) {
522                         /* There's enough total room, so just readjust */
523                         skb->data = memmove(skb->head
524                                                 + sizeof (struct nc_header),
525                                             skb->data, skb->len);
526                         skb->tail = skb->data + len;
527                         goto encapsulate;
528                 }
529         }
530
531         /* Create a new skb to use with the correct size */
532         skb2 = skb_copy_expand(skb,
533                                 sizeof (struct nc_header),
534                                 sizeof (struct nc_trailer) + padlen,
535                                 flags);
536         dev_kfree_skb_any(skb);
537         if (!skb2)
538                 return skb2;
539         skb = skb2;
540
541 encapsulate:
542         /* header first */
543         header = (struct nc_header *) skb_push(skb, sizeof *header);
544         header->hdr_len = cpu_to_le16(sizeof (*header));
545         header->packet_len = cpu_to_le16(len);
546         header->packet_id = cpu_to_le16((u16)dev->xid++);
547
548         /* maybe pad; then trailer */
549         if (!((skb->len + sizeof *trailer) & 0x01))
550                 *skb_put(skb, 1) = PAD_BYTE;
551         trailer = (struct nc_trailer *) skb_put(skb, sizeof *trailer);
552         put_unaligned(header->packet_id, &trailer->packet_id);
553 #if 0
554         devdbg(dev, "frame >tx h %d p %d id %d",
555                 header->hdr_len, header->packet_len,
556                 header->packet_id);
557 #endif
558         return skb;
559 }
560
561 static int net1080_bind(struct usbnet *dev, struct usb_interface *intf)
562 {
563         unsigned        extra = sizeof (struct nc_header)
564                                 + 1
565                                 + sizeof (struct nc_trailer);
566
567         dev->net->hard_header_len += extra;
568         dev->rx_urb_size = dev->net->hard_header_len + dev->net->mtu;
569         dev->hard_mtu = NC_MAX_PACKET;
570         return usbnet_get_endpoints (dev, intf);
571 }
572
573 static const struct driver_info net1080_info = {
574         .description =  "NetChip TurboCONNECT",
575         .flags =        FLAG_FRAMING_NC,
576         .bind =         net1080_bind,
577         .reset =        net1080_reset,
578         .check_connect = net1080_check_connect,
579         .rx_fixup =     net1080_rx_fixup,
580         .tx_fixup =     net1080_tx_fixup,
581 };
582
583 static const struct usb_device_id       products [] = {
584 {
585         USB_DEVICE(0x0525, 0x1080),     // NetChip ref design
586         .driver_info =  (unsigned long) &net1080_info,
587 }, {
588         USB_DEVICE(0x06D0, 0x0622),     // Laplink Gold
589         .driver_info =  (unsigned long) &net1080_info,
590 },
591         { },            // END
592 };
593 MODULE_DEVICE_TABLE(usb, products);
594
595 static struct usb_driver net1080_driver = {
596         .name =         "net1080",
597         .id_table =     products,
598         .probe =        usbnet_probe,
599         .disconnect =   usbnet_disconnect,
600         .suspend =      usbnet_suspend,
601         .resume =       usbnet_resume,
602 };
603
604 static int __init net1080_init(void)
605 {
606         return usb_register(&net1080_driver);
607 }
608 module_init(net1080_init);
609
610 static void __exit net1080_exit(void)
611 {
612         usb_deregister(&net1080_driver);
613 }
614 module_exit(net1080_exit);
615
616 MODULE_AUTHOR("David Brownell");
617 MODULE_DESCRIPTION("NetChip 1080 based USB Host-to-Host Links");
618 MODULE_LICENSE("GPL");