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