Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-drm-fsl-dcu.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, see <http://www.gnu.org/licenses/>.
17  */
18
19 // #define      DEBUG                   // error path messages, extra info
20 // #define      VERBOSE                 // more; success messages
21
22 #include <linux/module.h>
23 #include <linux/init.h>
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>
30 #include <linux/usb/usbnet.h>
31 #include <linux/slab.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 } __packed;
67
68 #define PAD_BYTE        ((unsigned char)0xAC)
69
70 struct nc_trailer {
71         __le16  packet_id;
72 } __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 = usbnet_read_cmd(dev, req,
112                                      USB_DIR_IN | USB_TYPE_VENDOR |
113                                      USB_RECIP_DEVICE,
114                                      0, regnum, retval_ptr,
115                                      sizeof *retval_ptr);
116         if (status > 0)
117                 status = 0;
118         if (!status)
119                 le16_to_cpus(retval_ptr);
120         return status;
121 }
122
123 static inline int
124 nc_register_read(struct usbnet *dev, u8 regnum, u16 *retval_ptr)
125 {
126         return nc_vendor_read(dev, REQUEST_REGISTER, regnum, retval_ptr);
127 }
128
129 // no retval ... can become async, usable in_interrupt()
130 static void
131 nc_vendor_write(struct usbnet *dev, u8 req, u8 regnum, u16 value)
132 {
133         usbnet_write_cmd(dev, req,
134                          USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
135                          value, regnum, NULL, 0);
136 }
137
138 static inline void
139 nc_register_write(struct usbnet *dev, u8 regnum, u16 value)
140 {
141         nc_vendor_write(dev, REQUEST_REGISTER, regnum, value);
142 }
143
144
145 #if 0
146 static void nc_dump_registers(struct usbnet *dev)
147 {
148         u8      reg;
149         u16     *vp = kmalloc(sizeof (u16));
150
151         if (!vp)
152                 return;
153
154         netdev_dbg(dev->net, "registers:\n");
155         for (reg = 0; reg < 0x20; reg++) {
156                 int retval;
157
158                 // reading some registers is trouble
159                 if (reg >= 0x08 && reg <= 0xf)
160                         continue;
161                 if (reg >= 0x12 && reg <= 0x1e)
162                         continue;
163
164                 retval = nc_register_read(dev, reg, vp);
165                 if (retval < 0)
166                         netdev_dbg(dev->net, "reg [0x%x] ==> error %d\n",
167                                    reg, retval);
168                 else
169                         netdev_dbg(dev->net, "reg [0x%x] = 0x%x\n", reg, *vp);
170         }
171         kfree(vp);
172 }
173 #endif
174
175
176 /*-------------------------------------------------------------------------*/
177
178 /*
179  * Control register
180  */
181
182 #define USBCTL_WRITABLE_MASK    0x1f0f
183 // bits 15-13 reserved, r/o
184 #define USBCTL_ENABLE_LANG      (1 << 12)
185 #define USBCTL_ENABLE_MFGR      (1 << 11)
186 #define USBCTL_ENABLE_PROD      (1 << 10)
187 #define USBCTL_ENABLE_SERIAL    (1 << 9)
188 #define USBCTL_ENABLE_DEFAULTS  (1 << 8)
189 // bits 7-4 reserved, r/o
190 #define USBCTL_FLUSH_OTHER      (1 << 3)
191 #define USBCTL_FLUSH_THIS       (1 << 2)
192 #define USBCTL_DISCONN_OTHER    (1 << 1)
193 #define USBCTL_DISCONN_THIS     (1 << 0)
194
195 static inline void nc_dump_usbctl(struct usbnet *dev, u16 usbctl)
196 {
197         netif_dbg(dev, link, dev->net,
198                   "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s; this%s%s; other%s%s; r/o 0x%x\n",
199                   dev->udev->bus->bus_name, dev->udev->devpath,
200                   usbctl,
201                   (usbctl & USBCTL_ENABLE_LANG) ? " lang" : "",
202                   (usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "",
203                   (usbctl & USBCTL_ENABLE_PROD) ? " prod" : "",
204                   (usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "",
205                   (usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "",
206
207                   (usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "",
208                   (usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "",
209
210                   (usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "",
211                   (usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "",
212
213                   usbctl & ~USBCTL_WRITABLE_MASK);
214 }
215
216 /*-------------------------------------------------------------------------*/
217
218 /*
219  * Status register
220  */
221
222 #define STATUS_PORT_A           (1 << 15)
223
224 #define STATUS_CONN_OTHER       (1 << 14)
225 #define STATUS_SUSPEND_OTHER    (1 << 13)
226 #define STATUS_MAILBOX_OTHER    (1 << 12)
227 #define STATUS_PACKETS_OTHER(n) (((n) >> 8) & 0x03)
228
229 #define STATUS_CONN_THIS        (1 << 6)
230 #define STATUS_SUSPEND_THIS     (1 << 5)
231 #define STATUS_MAILBOX_THIS     (1 << 4)
232 #define STATUS_PACKETS_THIS(n)  (((n) >> 0) & 0x03)
233
234 #define STATUS_UNSPEC_MASK      0x0c8c
235 #define STATUS_NOISE_MASK       ((u16)~(0x0303|STATUS_UNSPEC_MASK))
236
237
238 static inline void nc_dump_status(struct usbnet *dev, u16 status)
239 {
240         netif_dbg(dev, link, dev->net,
241                   "net1080 %s-%s status 0x%x: this (%c) PKT=%d%s%s%s; other PKT=%d%s%s%s; unspec 0x%x\n",
242                   dev->udev->bus->bus_name, dev->udev->devpath,
243                   status,
244
245                   // XXX the packet counts don't seem right
246                   // (1 at reset, not 0); maybe UNSPEC too
247
248                   (status & STATUS_PORT_A) ? 'A' : 'B',
249                   STATUS_PACKETS_THIS(status),
250                   (status & STATUS_CONN_THIS) ? " CON" : "",
251                   (status & STATUS_SUSPEND_THIS) ? " SUS" : "",
252                   (status & STATUS_MAILBOX_THIS) ? " MBOX" : "",
253
254                   STATUS_PACKETS_OTHER(status),
255                   (status & STATUS_CONN_OTHER) ? " CON" : "",
256                   (status & STATUS_SUSPEND_OTHER) ? " SUS" : "",
257                   (status & STATUS_MAILBOX_OTHER) ? " MBOX" : "",
258
259                   status & STATUS_UNSPEC_MASK);
260 }
261
262 /*-------------------------------------------------------------------------*/
263
264 /*
265  * TTL register
266  */
267
268 #define TTL_THIS(ttl)   (0x00ff & ttl)
269 #define TTL_OTHER(ttl)  (0x00ff & (ttl >> 8))
270 #define MK_TTL(this,other)      ((u16)(((other)<<8)|(0x00ff&(this))))
271
272 static inline void nc_dump_ttl(struct usbnet *dev, u16 ttl)
273 {
274         netif_dbg(dev, link, dev->net, "net1080 %s-%s ttl 0x%x this = %d, other = %d\n",
275                   dev->udev->bus->bus_name, dev->udev->devpath,
276                   ttl, TTL_THIS(ttl), TTL_OTHER(ttl));
277 }
278
279 /*-------------------------------------------------------------------------*/
280
281 static int net1080_reset(struct usbnet *dev)
282 {
283         u16             usbctl, status, ttl;
284         u16             vp;
285         int             retval;
286
287         // nc_dump_registers(dev);
288
289         if ((retval = nc_register_read(dev, REG_STATUS, &vp)) < 0) {
290                 netdev_dbg(dev->net, "can't read %s-%s status: %d\n",
291                            dev->udev->bus->bus_name, dev->udev->devpath, retval);
292                 goto done;
293         }
294         status = vp;
295         nc_dump_status(dev, status);
296
297         if ((retval = nc_register_read(dev, REG_USBCTL, &vp)) < 0) {
298                 netdev_dbg(dev->net, "can't read USBCTL, %d\n", retval);
299                 goto done;
300         }
301         usbctl = vp;
302         nc_dump_usbctl(dev, usbctl);
303
304         nc_register_write(dev, REG_USBCTL,
305                         USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER);
306
307         if ((retval = nc_register_read(dev, REG_TTL, &vp)) < 0) {
308                 netdev_dbg(dev->net, "can't read TTL, %d\n", retval);
309                 goto done;
310         }
311         ttl = vp;
312         // nc_dump_ttl(dev, ttl);
313
314         nc_register_write(dev, REG_TTL,
315                         MK_TTL(NC_READ_TTL_MS, TTL_OTHER(ttl)) );
316         netdev_dbg(dev->net, "assigned TTL, %d ms\n", NC_READ_TTL_MS);
317
318         netif_info(dev, link, dev->net, "port %c, peer %sconnected\n",
319                    (status & STATUS_PORT_A) ? 'A' : 'B',
320                    (status & STATUS_CONN_OTHER) ? "" : "dis");
321         retval = 0;
322
323 done:
324         return retval;
325 }
326
327 static int net1080_check_connect(struct usbnet *dev)
328 {
329         int                     retval;
330         u16                     status;
331         u16                     vp;
332
333         retval = nc_register_read(dev, REG_STATUS, &vp);
334         status = vp;
335         if (retval != 0) {
336                 netdev_dbg(dev->net, "net1080_check_conn read - %d\n", retval);
337                 return retval;
338         }
339         if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER)
340                 return -ENOLINK;
341         return 0;
342 }
343
344 static void nc_ensure_sync(struct usbnet *dev)
345 {
346         if (++dev->frame_errors <= 5)
347                 return;
348
349         if (usbnet_write_cmd_async(dev, REQUEST_REGISTER,
350                                         USB_DIR_OUT | USB_TYPE_VENDOR |
351                                         USB_RECIP_DEVICE,
352                                         USBCTL_FLUSH_THIS |
353                                         USBCTL_FLUSH_OTHER,
354                                         REG_USBCTL, NULL, 0))
355                 return;
356
357         netif_dbg(dev, rx_err, dev->net,
358                   "flush net1080; too many framing errors\n");
359         dev->frame_errors = 0;
360 }
361
362 static int net1080_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
363 {
364         struct nc_header        *header;
365         struct nc_trailer       *trailer;
366         u16                     hdr_len, packet_len;
367
368         if (!(skb->len & 0x01)) {
369                 netdev_dbg(dev->net, "rx framesize %d range %d..%d mtu %d\n",
370                            skb->len, dev->net->hard_header_len, dev->hard_mtu,
371                            dev->net->mtu);
372                 dev->net->stats.rx_frame_errors++;
373                 nc_ensure_sync(dev);
374                 return 0;
375         }
376
377         header = (struct nc_header *) skb->data;
378         hdr_len = le16_to_cpup(&header->hdr_len);
379         packet_len = le16_to_cpup(&header->packet_len);
380         if (FRAMED_SIZE(packet_len) > NC_MAX_PACKET) {
381                 dev->net->stats.rx_frame_errors++;
382                 netdev_dbg(dev->net, "packet too big, %d\n", packet_len);
383                 nc_ensure_sync(dev);
384                 return 0;
385         } else if (hdr_len < MIN_HEADER) {
386                 dev->net->stats.rx_frame_errors++;
387                 netdev_dbg(dev->net, "header too short, %d\n", hdr_len);
388                 nc_ensure_sync(dev);
389                 return 0;
390         } else if (hdr_len > MIN_HEADER) {
391                 // out of band data for us?
392                 netdev_dbg(dev->net, "header OOB, %d bytes\n", hdr_len - MIN_HEADER);
393                 nc_ensure_sync(dev);
394                 // switch (vendor/product ids) { ... }
395         }
396         skb_pull(skb, hdr_len);
397
398         trailer = (struct nc_trailer *)
399                 (skb->data + skb->len - sizeof *trailer);
400         skb_trim(skb, skb->len - sizeof *trailer);
401
402         if ((packet_len & 0x01) == 0) {
403                 if (skb->data [packet_len] != PAD_BYTE) {
404                         dev->net->stats.rx_frame_errors++;
405                         netdev_dbg(dev->net, "bad pad\n");
406                         return 0;
407                 }
408                 skb_trim(skb, skb->len - 1);
409         }
410         if (skb->len != packet_len) {
411                 dev->net->stats.rx_frame_errors++;
412                 netdev_dbg(dev->net, "bad packet len %d (expected %d)\n",
413                            skb->len, packet_len);
414                 nc_ensure_sync(dev);
415                 return 0;
416         }
417         if (header->packet_id != get_unaligned(&trailer->packet_id)) {
418                 dev->net->stats.rx_fifo_errors++;
419                 netdev_dbg(dev->net, "(2+ dropped) rx packet_id mismatch 0x%x 0x%x\n",
420                            le16_to_cpu(header->packet_id),
421                            le16_to_cpu(trailer->packet_id));
422                 return 0;
423         }
424 #if 0
425         netdev_dbg(dev->net, "frame <rx h %d p %d id %d\n", header->hdr_len,
426                    header->packet_len, header->packet_id);
427 #endif
428         dev->frame_errors = 0;
429         return 1;
430 }
431
432 static struct sk_buff *
433 net1080_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
434 {
435         struct sk_buff          *skb2;
436         struct nc_header        *header = NULL;
437         struct nc_trailer       *trailer = NULL;
438         int                     padlen = sizeof (struct nc_trailer);
439         int                     len = skb->len;
440
441         if (!((len + padlen + sizeof (struct nc_header)) & 0x01))
442                 padlen++;
443         if (!skb_cloned(skb)) {
444                 int     headroom = skb_headroom(skb);
445                 int     tailroom = skb_tailroom(skb);
446
447                 if (padlen <= tailroom &&
448                     sizeof(struct nc_header) <= headroom)
449                         /* There's enough head and tail room */
450                         goto encapsulate;
451
452                 if ((sizeof (struct nc_header) + padlen) <
453                                 (headroom + tailroom)) {
454                         /* There's enough total room, so just readjust */
455                         skb->data = memmove(skb->head
456                                                 + sizeof (struct nc_header),
457                                             skb->data, skb->len);
458                         skb_set_tail_pointer(skb, len);
459                         goto encapsulate;
460                 }
461         }
462
463         /* Create a new skb to use with the correct size */
464         skb2 = skb_copy_expand(skb,
465                                 sizeof (struct nc_header),
466                                 padlen,
467                                 flags);
468         dev_kfree_skb_any(skb);
469         if (!skb2)
470                 return skb2;
471         skb = skb2;
472
473 encapsulate:
474         /* header first */
475         header = (struct nc_header *) skb_push(skb, sizeof *header);
476         header->hdr_len = cpu_to_le16(sizeof (*header));
477         header->packet_len = cpu_to_le16(len);
478         header->packet_id = cpu_to_le16((u16)dev->xid++);
479
480         /* maybe pad; then trailer */
481         if (!((skb->len + sizeof *trailer) & 0x01))
482                 *skb_put(skb, 1) = PAD_BYTE;
483         trailer = (struct nc_trailer *) skb_put(skb, sizeof *trailer);
484         put_unaligned(header->packet_id, &trailer->packet_id);
485 #if 0
486         netdev_dbg(dev->net, "frame >tx h %d p %d id %d\n",
487                    header->hdr_len, header->packet_len,
488                    header->packet_id);
489 #endif
490         return skb;
491 }
492
493 static int net1080_bind(struct usbnet *dev, struct usb_interface *intf)
494 {
495         unsigned        extra = sizeof (struct nc_header)
496                                 + 1
497                                 + sizeof (struct nc_trailer);
498
499         dev->net->hard_header_len += extra;
500         dev->rx_urb_size = dev->net->hard_header_len + dev->net->mtu;
501         dev->hard_mtu = NC_MAX_PACKET;
502         return usbnet_get_endpoints (dev, intf);
503 }
504
505 static const struct driver_info net1080_info = {
506         .description =  "NetChip TurboCONNECT",
507         .flags =        FLAG_POINTTOPOINT | FLAG_FRAMING_NC,
508         .bind =         net1080_bind,
509         .reset =        net1080_reset,
510         .check_connect = net1080_check_connect,
511         .rx_fixup =     net1080_rx_fixup,
512         .tx_fixup =     net1080_tx_fixup,
513 };
514
515 static const struct usb_device_id       products [] = {
516 {
517         USB_DEVICE(0x0525, 0x1080),     // NetChip ref design
518         .driver_info =  (unsigned long) &net1080_info,
519 }, {
520         USB_DEVICE(0x06D0, 0x0622),     // Laplink Gold
521         .driver_info =  (unsigned long) &net1080_info,
522 },
523         { },            // END
524 };
525 MODULE_DEVICE_TABLE(usb, products);
526
527 static struct usb_driver net1080_driver = {
528         .name =         "net1080",
529         .id_table =     products,
530         .probe =        usbnet_probe,
531         .disconnect =   usbnet_disconnect,
532         .suspend =      usbnet_suspend,
533         .resume =       usbnet_resume,
534         .disable_hub_initiated_lpm = 1,
535 };
536
537 module_usb_driver(net1080_driver);
538
539 MODULE_AUTHOR("David Brownell");
540 MODULE_DESCRIPTION("NetChip 1080 based USB Host-to-Host Links");
541 MODULE_LICENSE("GPL");