Merge git://git.kernel.org/pub/scm/linux/kernel/git/sam/kbuild
[linux-2.6] / drivers / usb / net / zaurus.c
1 /*
2  * Copyright (C) 2002 Pavel Machek <pavel@ucw.cz>
3  * Copyright (C) 2002-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/ethtool.h>
29 #include <linux/workqueue.h>
30 #include <linux/mii.h>
31 #include <linux/crc32.h>
32 #include <linux/usb.h>
33 #include <linux/usb/cdc.h>
34
35 #include "usbnet.h"
36
37
38 /*
39  * All known Zaurii lie about their standards conformance.  At least
40  * the earliest SA-1100 models lie by saying they support CDC Ethernet.
41  * Some later models (especially PXA-25x and PXA-27x based ones) lie
42  * and say they support CDC MDLM (for access to cell phone modems).
43  *
44  * There are non-Zaurus products that use these same protocols too.
45  *
46  * The annoying thing is that at the same time Sharp was developing
47  * that annoying standards-breaking software, the Linux community had
48  * a simple "CDC Subset" working reliably on the same SA-1100 hardware.
49  * That is, the same functionality but not violating standards.
50  *
51  * The CDC Ethernet nonconformance points are troublesome to hosts
52  * with a true CDC Ethernet implementation:
53  *   - Framing appends a CRC, which the spec says drivers "must not" do;
54  *   - Transfers data in altsetting zero, instead of altsetting 1;
55  *   - All these peripherals use the same ethernet address.
56  *
57  * The CDC MDLM nonconformance is less immediately troublesome, since all
58  * MDLM implementations are quasi-proprietary anyway.
59  */
60
61 static struct sk_buff *
62 zaurus_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
63 {
64         int                     padlen;
65         struct sk_buff          *skb2;
66
67         padlen = 2;
68         if (!skb_cloned(skb)) {
69                 int     tailroom = skb_tailroom(skb);
70                 if ((padlen + 4) <= tailroom)
71                         goto done;
72         }
73         skb2 = skb_copy_expand(skb, 0, 4 + padlen, flags);
74         dev_kfree_skb_any(skb);
75         skb = skb2;
76         if (skb) {
77                 u32             fcs;
78 done:
79                 fcs = crc32_le(~0, skb->data, skb->len);
80                 fcs = ~fcs;
81
82                 *skb_put (skb, 1) = fcs       & 0xff;
83                 *skb_put (skb, 1) = (fcs>> 8) & 0xff;
84                 *skb_put (skb, 1) = (fcs>>16) & 0xff;
85                 *skb_put (skb, 1) = (fcs>>24) & 0xff;
86         }
87         return skb;
88 }
89
90 static int zaurus_bind(struct usbnet *dev, struct usb_interface *intf)
91 {
92         /* Belcarra's funky framing has other options; mostly
93          * TRAILERS (!) with 4 bytes CRC, and maybe 2 pad bytes.
94          */
95         dev->net->hard_header_len += 6;
96         dev->rx_urb_size = dev->net->hard_header_len + dev->net->mtu;
97         return usbnet_generic_cdc_bind(dev, intf);
98 }
99
100 /* PDA style devices are always connected if present */
101 static int always_connected (struct usbnet *dev)
102 {
103         return 0;
104 }
105
106 static const struct driver_info zaurus_sl5x00_info = {
107         .description =  "Sharp Zaurus SL-5x00",
108         .flags =        FLAG_FRAMING_Z,
109         .check_connect = always_connected,
110         .bind =         zaurus_bind,
111         .unbind =       usbnet_cdc_unbind,
112         .tx_fixup =     zaurus_tx_fixup,
113 };
114 #define ZAURUS_STRONGARM_INFO   ((unsigned long)&zaurus_sl5x00_info)
115
116 static const struct driver_info zaurus_pxa_info = {
117         .description =  "Sharp Zaurus, PXA-2xx based",
118         .flags =        FLAG_FRAMING_Z,
119         .check_connect = always_connected,
120         .bind =         zaurus_bind,
121         .unbind =       usbnet_cdc_unbind,
122         .tx_fixup =     zaurus_tx_fixup,
123 };
124 #define ZAURUS_PXA_INFO         ((unsigned long)&zaurus_pxa_info)
125
126 static const struct driver_info olympus_mxl_info = {
127         .description =  "Olympus R1000",
128         .flags =        FLAG_FRAMING_Z,
129         .check_connect = always_connected,
130         .bind =         zaurus_bind,
131         .unbind =       usbnet_cdc_unbind,
132         .tx_fixup =     zaurus_tx_fixup,
133 };
134 #define OLYMPUS_MXL_INFO        ((unsigned long)&olympus_mxl_info)
135
136
137 /* Some more recent products using Lineo/Belcarra code will wrongly claim
138  * CDC MDLM conformance.  They aren't conformant:  data endpoints live
139  * in the control interface, there's no data interface, and it's not used
140  * to talk to a cell phone radio.  But at least we can detect these two
141  * pseudo-classes, rather than growing this product list with entries for
142  * each new nonconformant product (sigh).
143  */
144 static const u8 safe_guid[16] = {
145         0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
146         0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
147 };
148 static const u8 blan_guid[16] = {
149         0x74, 0xf0, 0x3d, 0xbd, 0x1e, 0xc1, 0x44, 0x70,
150         0xa3, 0x67, 0x71, 0x34, 0xc9, 0xf5, 0x54, 0x37,
151 };
152
153 static int blan_mdlm_bind(struct usbnet *dev, struct usb_interface *intf)
154 {
155         u8                              *buf = intf->cur_altsetting->extra;
156         int                             len = intf->cur_altsetting->extralen;
157         struct usb_cdc_mdlm_desc        *desc = NULL;
158         struct usb_cdc_mdlm_detail_desc *detail = NULL;
159
160         while (len > 3) {
161                 if (buf [1] != USB_DT_CS_INTERFACE)
162                         goto next_desc;
163
164                 /* use bDescriptorSubType, and just verify that we get a
165                  * "BLAN" (or "SAFE") descriptor.
166                  */
167                 switch (buf [2]) {
168                 case USB_CDC_MDLM_TYPE:
169                         if (desc) {
170                                 dev_dbg(&intf->dev, "extra MDLM\n");
171                                 goto bad_desc;
172                         }
173                         desc = (void *) buf;
174                         if (desc->bLength != sizeof *desc) {
175                                 dev_dbg(&intf->dev, "MDLM len %u\n",
176                                         desc->bLength);
177                                 goto bad_desc;
178                         }
179                         /* expect bcdVersion 1.0, ignore */
180                         if (memcmp(&desc->bGUID, blan_guid, 16)
181                                     && memcmp(&desc->bGUID, safe_guid, 16) ) {
182                                 /* hey, this one might _really_ be MDLM! */
183                                 dev_dbg(&intf->dev, "MDLM guid\n");
184                                 goto bad_desc;
185                         }
186                         break;
187                 case USB_CDC_MDLM_DETAIL_TYPE:
188                         if (detail) {
189                                 dev_dbg(&intf->dev, "extra MDLM detail\n");
190                                 goto bad_desc;
191                         }
192                         detail = (void *) buf;
193                         switch (detail->bGuidDescriptorType) {
194                         case 0:                 /* "SAFE" */
195                                 if (detail->bLength != (sizeof *detail + 2))
196                                         goto bad_detail;
197                                 break;
198                         case 1:                 /* "BLAN" */
199                                 if (detail->bLength != (sizeof *detail + 3))
200                                         goto bad_detail;
201                                 break;
202                         default:
203                                 goto bad_detail;
204                         }
205
206                         /* assuming we either noticed BLAN already, or will
207                          * find it soon, there are some data bytes here:
208                          *  - bmNetworkCapabilities (unused)
209                          *  - bmDataCapabilities (bits, see below)
210                          *  - bPad (ignored, for PADAFTER -- BLAN-only)
211                          * bits are:
212                          *  - 0x01 -- Zaurus framing (add CRC)
213                          *  - 0x02 -- PADBEFORE (CRC includes some padding)
214                          *  - 0x04 -- PADAFTER (some padding after CRC)
215                          *  - 0x08 -- "fermat" packet mangling (for hw bugs)
216                          * the PADBEFORE appears not to matter; we interop
217                          * with devices that use it and those that don't.
218                          */
219                         if ((detail->bDetailData[1] & ~0x02) != 0x01) {
220                                 /* bmDataCapabilities == 0 would be fine too,
221                                  * but framing is minidriver-coupled for now.
222                                  */
223 bad_detail:
224                                 dev_dbg(&intf->dev,
225                                                 "bad MDLM detail, %d %d %d\n",
226                                                 detail->bLength,
227                                                 detail->bDetailData[0],
228                                                 detail->bDetailData[2]);
229                                 goto bad_desc;
230                         }
231
232                         /* same extra framing as for non-BLAN mode */
233                         dev->net->hard_header_len += 6;
234                         dev->rx_urb_size = dev->net->hard_header_len
235                                         + dev->net->mtu;
236                         break;
237                 }
238 next_desc:
239                 len -= buf [0]; /* bLength */
240                 buf += buf [0];
241         }
242
243         if (!desc || !detail) {
244                 dev_dbg(&intf->dev, "missing cdc mdlm %s%sdescriptor\n",
245                         desc ? "" : "func ",
246                         detail ? "" : "detail ");
247                 goto bad_desc;
248         }
249
250         /* There's probably a CDC Ethernet descriptor there, but we can't
251          * rely on the Ethernet address it provides since not all vendors
252          * bother to make it unique.  Likewise there's no point in tracking
253          * of the CDC event notifications.
254          */
255         return usbnet_get_endpoints(dev, intf);
256
257 bad_desc:
258         dev_info(&dev->udev->dev, "unsupported MDLM descriptors\n");
259         return -ENODEV;
260 }
261
262 static const struct driver_info bogus_mdlm_info = {
263         .description =  "pseudo-MDLM (BLAN) device",
264         .flags =        FLAG_FRAMING_Z,
265         .check_connect = always_connected,
266         .tx_fixup =     zaurus_tx_fixup,
267         .bind =         blan_mdlm_bind,
268 };
269
270 static const struct usb_device_id       products [] = {
271 #define ZAURUS_MASTER_INTERFACE \
272         .bInterfaceClass        = USB_CLASS_COMM, \
273         .bInterfaceSubClass     = USB_CDC_SUBCLASS_ETHERNET, \
274         .bInterfaceProtocol     = USB_CDC_PROTO_NONE
275
276 /* SA-1100 based Sharp Zaurus ("collie"), or compatible. */
277 {
278         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
279                           | USB_DEVICE_ID_MATCH_DEVICE,
280         .idVendor               = 0x04DD,
281         .idProduct              = 0x8004,
282         ZAURUS_MASTER_INTERFACE,
283         .driver_info = ZAURUS_STRONGARM_INFO,
284 },
285
286 /* PXA-2xx based models are also lying-about-cdc.  If you add any
287  * more devices that claim to be CDC Ethernet, make sure they get
288  * added to the blacklist in cdc_ether too.
289  *
290  * NOTE:  OpenZaurus versions with 2.6 kernels won't use these entries,
291  * unlike the older ones with 2.4 "embedix" kernels.
292  */
293 {
294         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
295                           | USB_DEVICE_ID_MATCH_DEVICE,
296         .idVendor               = 0x04DD,
297         .idProduct              = 0x8005,       /* A-300 */
298         ZAURUS_MASTER_INTERFACE,
299         .driver_info = ZAURUS_PXA_INFO,
300 }, {
301         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
302                           | USB_DEVICE_ID_MATCH_DEVICE,
303         .idVendor               = 0x04DD,
304         .idProduct              = 0x8006,       /* B-500/SL-5600 */
305         ZAURUS_MASTER_INTERFACE,
306         .driver_info = ZAURUS_PXA_INFO,
307 }, {
308         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
309                   | USB_DEVICE_ID_MATCH_DEVICE,
310         .idVendor               = 0x04DD,
311         .idProduct              = 0x8007,       /* C-700 */
312         ZAURUS_MASTER_INTERFACE,
313         .driver_info = ZAURUS_PXA_INFO,
314 }, {
315         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
316                  | USB_DEVICE_ID_MATCH_DEVICE,
317         .idVendor               = 0x04DD,
318         .idProduct              = 0x9031,       /* C-750 C-760 */
319         ZAURUS_MASTER_INTERFACE,
320         .driver_info = ZAURUS_PXA_INFO,
321 }, {
322         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
323                  | USB_DEVICE_ID_MATCH_DEVICE,
324         .idVendor               = 0x04DD,
325         .idProduct              = 0x9032,       /* SL-6000 */
326         ZAURUS_MASTER_INTERFACE,
327         .driver_info = ZAURUS_PXA_INFO,
328 }, {
329         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
330                  | USB_DEVICE_ID_MATCH_DEVICE,
331         .idVendor               = 0x04DD,
332         /* reported with some C860 units */
333         .idProduct              = 0x9050,       /* C-860 */
334         ZAURUS_MASTER_INTERFACE,
335         .driver_info = ZAURUS_PXA_INFO,
336 },
337
338
339 /* At least some of the newest PXA units have very different lies about
340  * their standards support:  they claim to be cell phones offering
341  * direct access to their radios!  (No, they don't conform to CDC MDLM.)
342  */
343 {
344         USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_MDLM,
345                         USB_CDC_PROTO_NONE),
346         .driver_info = (unsigned long) &bogus_mdlm_info,
347 },
348
349 /* Olympus has some models with a Zaurus-compatible option.
350  * R-1000 uses a FreeScale i.MXL cpu (ARMv4T)
351  */
352 {
353         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
354                  | USB_DEVICE_ID_MATCH_DEVICE,
355         .idVendor               = 0x07B4,
356         .idProduct              = 0x0F02,       /* R-1000 */
357         ZAURUS_MASTER_INTERFACE,
358         .driver_info = OLYMPUS_MXL_INFO,
359 },
360         { },            // END
361 };
362 MODULE_DEVICE_TABLE(usb, products);
363
364 static struct usb_driver zaurus_driver = {
365         .name =         "zaurus",
366         .id_table =     products,
367         .probe =        usbnet_probe,
368         .disconnect =   usbnet_disconnect,
369         .suspend =      usbnet_suspend,
370         .resume =       usbnet_resume,
371 };
372
373 static int __init zaurus_init(void)
374 {
375         return usb_register(&zaurus_driver);
376 }
377 module_init(zaurus_init);
378
379 static void __exit zaurus_exit(void)
380 {
381         usb_deregister(&zaurus_driver);
382 }
383 module_exit(zaurus_exit);
384
385 MODULE_AUTHOR("Pavel Machek, David Brownell");
386 MODULE_DESCRIPTION("Sharp Zaurus PDA, and compatible products");
387 MODULE_LICENSE("GPL");