Nuke the code specific to NetBSD/OpenBSD/FreeBSD at first. I doubt anyone
[dragonfly.git] / sys / dev / usbmisc / uvisor / uvisor.c
1 /*
2  * $NetBSD: uvisor.c,v 1.9 2001/01/23 14:04:14 augustss Exp $
3  * $FreeBSD: src/sys/dev/usb/uvisor.c,v 1.16 2003/11/08 11:23:07 joe Exp $
4  * $DragonFly: src/sys/dev/usbmisc/uvisor/uvisor.c,v 1.11 2007/06/27 12:28:00 hasso Exp $
5  */
6
7 /*
8  * Also already merged from NetBSD:
9  *    $NetBSD: uvisor.c,v 1.12 2001/11/13 06:24:57 lukem Exp $
10  *    $NetBSD: uvisor.c,v 1.13 2002/02/11 15:11:49 augustss Exp $
11  *    $NetBSD: uvisor.c,v 1.14 2002/02/27 23:00:03 augustss Exp $
12  *    $NetBSD: uvisor.c,v 1.15 2002/06/16 15:01:31 augustss Exp $
13  *    $NetBSD: uvisor.c,v 1.16 2002/07/11 21:14:36 augustss Exp $
14  *    $NetBSD: uvisor.c,v 1.17 2002/08/13 11:38:15 augustss Exp $
15  *    $NetBSD: uvisor.c,v 1.18 2003/02/05 00:50:14 augustss Exp $
16  *    $NetBSD: uvisor.c,v 1.19 2003/02/07 18:12:37 augustss Exp $
17  *    $NetBSD: uvisor.c,v 1.20 2003/04/11 01:30:10 simonb Exp $
18  */
19
20 /*
21  * Copyright (c) 2000 The NetBSD Foundation, Inc.
22  * All rights reserved.
23  *
24  * This code is derived from software contributed to The NetBSD Foundation
25  * by Lennart Augustsson (lennart@augustsson.net) at
26  * Carlstedt Research & Technology.
27  *
28  * Redistribution and use in source and binary forms, with or without
29  * modification, are permitted provided that the following conditions
30  * are met:
31  * 1. Redistributions of source code must retain the above copyright
32  *    notice, this list of conditions and the following disclaimer.
33  * 2. Redistributions in binary form must reproduce the above copyright
34  *    notice, this list of conditions and the following disclaimer in the
35  *    documentation and/or other materials provided with the distribution.
36  * 3. All advertising materials mentioning features or use of this software
37  *    must display the following acknowledgement:
38  *        This product includes software developed by the NetBSD
39  *        Foundation, Inc. and its contributors.
40  * 4. Neither the name of The NetBSD Foundation nor the names of its
41  *    contributors may be used to endorse or promote products derived
42  *    from this software without specific prior written permission.
43  *
44  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
45  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
46  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
48  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54  * POSSIBILITY OF SUCH DAMAGE.
55  */
56
57 /*
58  * Handspring Visor (Palmpilot compatible PDA) driver
59  */
60
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
64 #include <sys/bus.h>
65 #include <sys/conf.h>
66 #include <sys/tty.h>
67 #include <sys/sysctl.h>
68
69 #include <bus/usb/usb.h>
70 #include <bus/usb/usbhid.h>
71
72 #include <bus/usb/usbdi.h>
73 #include <bus/usb/usbdi_util.h>
74 #include <bus/usb/usbdevs.h>
75
76 #include "../ucom/ucomvar.h"
77
78 #ifdef USB_DEBUG
79 #define DPRINTF(x)      if (uvisordebug) kprintf x
80 #define DPRINTFN(n,x)   if (uvisordebug>(n)) kprintf x
81 int uvisordebug = 0;
82 SYSCTL_NODE(_hw_usb, OID_AUTO, uvisor, CTLFLAG_RW, 0, "USB uvisor");
83 SYSCTL_INT(_hw_usb_uvisor, OID_AUTO, debug, CTLFLAG_RW,
84            &uvisordebug, 0, "uvisor debug level");
85 #else
86 #define DPRINTF(x)
87 #define DPRINTFN(n,x)
88 #endif
89
90 #define UVISOR_CONFIG_INDEX     0
91 #define UVISOR_IFACE_INDEX      0
92 #define UVISOR_MODVER           1
93
94 /* From the Linux driver */
95 /*
96  * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
97  * are available to be transfered to the host for the specified endpoint.
98  * Currently this is not used, and always returns 0x0001
99  */
100 #define UVISOR_REQUEST_BYTES_AVAILABLE          0x01
101
102 /*
103  * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
104  * is now closing the pipe. An empty packet is sent in response.
105  */
106 #define UVISOR_CLOSE_NOTIFICATION               0x02
107
108 /*
109  * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
110  * get the endpoints used by the connection.
111  */
112 #define UVISOR_GET_CONNECTION_INFORMATION       0x03
113
114
115 /*
116  * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
117  */
118 #define UVISOR_MAX_CONN 8
119 struct uvisor_connection_info {
120         uWord   num_ports;
121         struct {
122                 uByte   port_function_id;
123                 uByte   port;
124         } connections[UVISOR_MAX_CONN];
125 };
126 #define UVISOR_CONNECTION_INFO_SIZE 18
127
128 /* struct uvisor_connection_info.connection[x].port defines: */
129 #define UVISOR_ENDPOINT_1               0x01
130 #define UVISOR_ENDPOINT_2               0x02
131
132 /* struct uvisor_connection_info.connection[x].port_function_id defines: */
133 #define UVISOR_FUNCTION_GENERIC         0x00
134 #define UVISOR_FUNCTION_DEBUGGER        0x01
135 #define UVISOR_FUNCTION_HOTSYNC         0x02
136 #define UVISOR_FUNCTION_CONSOLE         0x03
137 #define UVISOR_FUNCTION_REMOTE_FILE_SYS 0x04
138
139 /*
140  * Unknown PalmOS stuff.
141  */
142 #define UVISOR_GET_PALM_INFORMATION             0x04
143 #define UVISOR_GET_PALM_INFORMATION_LEN         0x14
144
145
146 /*
147  * Crank down UVISORBUFSIZE from 1024 to 64 to avoid a problem where
148  * the Palm device and the USB host controller deadlock. The USB host
149  * controller is expecting an early-end-of-transmission packet with 0
150  * data, and the Palm doesn't send one because it's already
151  * communicated the amount of data it's going to send in a header
152  * (which ucom/uvisor are oblivious to). This is the problem that has
153  * been known on the pilot-link lists as the "[Free]BSD USB problem",
154  * but not understood.
155  */
156 #define UVISORIBUFSIZE 64
157 #define UVISOROBUFSIZE 1024
158
159 struct uvisor_softc {
160         struct ucom_softc       sc_ucom;
161         u_int16_t               sc_flags;
162 };
163
164 Static usbd_status uvisor_init(struct uvisor_softc *);
165
166 Static void uvisor_close(void *, int);
167
168 struct ucom_callback uvisor_callback = {
169         NULL,
170         NULL,
171         NULL,
172         NULL,
173         NULL,
174         uvisor_close,
175         NULL,
176         NULL,
177 };
178
179 Static device_probe_t uvisor_match;
180 Static device_attach_t uvisor_attach;
181 Static device_detach_t uvisor_detach;
182 Static device_method_t uvisor_methods[] = {
183        /* Device interface */
184        DEVMETHOD(device_probe, uvisor_match),
185        DEVMETHOD(device_attach, uvisor_attach),
186        DEVMETHOD(device_detach, uvisor_detach),
187        { 0, 0 }
188  };
189
190
191 Static driver_t uvisor_driver = {
192        "ucom",
193        uvisor_methods,
194        sizeof (struct uvisor_softc)
195 };
196
197 DRIVER_MODULE(uvisor, uhub, uvisor_driver, ucom_devclass, usbd_driver_load, 0);
198 MODULE_DEPEND(uvisor, usb, 1, 1, 1);
199 MODULE_DEPEND(uvisor, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
200 MODULE_VERSION(uvisor, UVISOR_MODVER);
201
202 struct uvisor_type {
203         struct usb_devno        uv_dev;
204         u_int16_t               uv_flags;
205 #define PALM4   0x0001
206 };
207 static const struct uvisor_type uvisor_devs[] = {
208         {{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_VISOR }, 0 },
209         {{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO }, PALM4 },
210         {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M500 }, PALM4 },
211         {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M505 }, PALM4 },
212         {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M515 }, PALM4 },
213         {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_I705 }, PALM4 },
214         {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M125 }, PALM4 },
215         {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M130 }, PALM4 },
216         {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_Z }, PALM4 },
217         {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_T }, PALM4 },
218         {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE }, PALM4 },
219         {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40 }, 0 },
220         {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_41 }, 0 },
221         {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_S360 }, PALM4 },
222         {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_NX60 }, PALM4 },
223 /*      {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25 }, PALM4 },*/
224 };
225 #define uvisor_lookup(v, p) ((const struct uvisor_type *)usb_lookup(uvisor_devs, v, p))
226
227
228 USB_MATCH(uvisor)
229 {
230         USB_MATCH_START(uvisor, uaa);
231
232         if (uaa->iface != NULL)
233                 return (UMATCH_NONE);
234
235         DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n",
236                      uaa->vendor, uaa->product));
237
238         return (uvisor_lookup(uaa->vendor, uaa->product) != NULL ?
239                 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
240 }
241
242 USB_ATTACH(uvisor)
243 {
244         USB_ATTACH_START(uvisor, sc, uaa);
245         usbd_device_handle dev = uaa->device;
246         usbd_interface_handle iface;
247         usb_interface_descriptor_t *id;
248         usb_endpoint_descriptor_t *ed;
249         char *devinfo;
250         const char *devname;
251         int i;
252         usbd_status err;
253         struct ucom_softc *ucom;
254
255         devinfo = kmalloc(1024, M_USBDEV, M_INTWAIT);
256         ucom = &sc->sc_ucom;
257
258         bzero(sc, sizeof (struct uvisor_softc));
259         usbd_devinfo(dev, 0, devinfo);
260
261         ucom->sc_dev = self;
262         device_set_desc_copy(self, devinfo);
263
264         ucom->sc_udev = dev;
265         ucom->sc_iface = uaa->iface;
266
267         devname = USBDEVNAME(ucom->sc_dev);
268         kprintf("%s: %s\n", devname, devinfo);
269
270         DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc));
271
272         /* Move the device into the configured state. */
273         err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1);
274         if (err) {
275                 kprintf("\n%s: failed to set configuration, err=%s\n",
276                        devname, usbd_errstr(err));
277                 goto bad;
278         }
279
280         err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
281         if (err) {
282                 kprintf("\n%s: failed to get interface, err=%s\n",
283                        devname, usbd_errstr(err));
284                 goto bad;
285         }
286
287         kprintf("%s: %s\n", devname, devinfo);
288
289         sc->sc_flags = uvisor_lookup(uaa->vendor, uaa->product)->uv_flags;
290
291         id = usbd_get_interface_descriptor(iface);
292
293         ucom->sc_udev = dev;
294         ucom->sc_iface = iface;
295
296         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
297         for (i = 0; i < id->bNumEndpoints; i++) {
298                 int addr, dir, attr;
299                 ed = usbd_interface2endpoint_descriptor(iface, i);
300                 if (ed == NULL) {
301                         kprintf("%s: could not read endpoint descriptor"
302                                ": %s\n", devname, usbd_errstr(err));
303                         goto bad;
304                 }
305
306                 addr = ed->bEndpointAddress;
307                 dir = UE_GET_DIR(ed->bEndpointAddress);
308                 attr = ed->bmAttributes & UE_XFERTYPE;
309                 if (dir == UE_DIR_IN && attr == UE_BULK)
310                         ucom->sc_bulkin_no = addr;
311                 else if (dir == UE_DIR_OUT && attr == UE_BULK)
312                         ucom->sc_bulkout_no = addr;
313                 else {
314                         kprintf("%s: unexpected endpoint\n", devname);
315                         goto bad;
316                 }
317         }
318         if (ucom->sc_bulkin_no == -1) {
319                 kprintf("%s: Could not find data bulk in\n",
320                        USBDEVNAME(ucom->sc_dev));
321                 goto bad;
322         }
323         if (ucom->sc_bulkout_no == -1) {
324                 kprintf("%s: Could not find data bulk out\n",
325                        USBDEVNAME(ucom->sc_dev));
326                 goto bad;
327         }
328
329         ucom->sc_parent = sc;
330         ucom->sc_portno = UCOM_UNK_PORTNO;
331         /* bulkin, bulkout set above */
332         ucom->sc_ibufsize = UVISORIBUFSIZE;
333         ucom->sc_obufsize = UVISOROBUFSIZE;
334         ucom->sc_ibufsizepad = UVISORIBUFSIZE;
335         ucom->sc_opkthdrlen = 0;
336         ucom->sc_callback = &uvisor_callback;
337
338         err = uvisor_init(sc);
339         if (err) {
340                 kprintf("%s: init failed, %s\n", USBDEVNAME(ucom->sc_dev),
341                        usbd_errstr(err));
342                 goto bad;
343         }
344
345         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
346                            ucom->sc_dev);
347
348         DPRINTF(("uvisor: in=0x%x out=0x%x\n", ucom->sc_bulkin_no, ucom->sc_bulkout_no));
349         ucom_attach(&sc->sc_ucom);
350
351         USB_ATTACH_SUCCESS_RETURN;
352
353 bad:
354         DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
355         ucom->sc_dying = 1;
356         USB_ATTACH_ERROR_RETURN;
357 }
358
359 #if 0
360
361 int
362 uvisor_activate(device_ptr_t self, enum devact act)
363 {
364         struct uvisor_softc *sc = (struct uvisor_softc *)self;
365         int rv = 0;
366
367         switch (act) {
368         case DVACT_ACTIVATE:
369                 return (EOPNOTSUPP);
370                 break;
371
372         case DVACT_DEACTIVATE:
373                 if (sc->sc_subdev != NULL)
374                         rv = config_deactivate(sc->sc_subdev);
375                 sc->sc_dying = 1;
376                 break;
377         }
378         return (rv);
379 }
380
381 #endif
382
383 USB_DETACH(uvisor)
384 {
385         USB_DETACH_START(uvisor, sc);
386         int rv = 0;
387
388         DPRINTF(("uvisor_detach: sc=%p\n", sc));
389         sc->sc_ucom.sc_dying = 1;
390         rv = ucom_detach(&sc->sc_ucom);
391
392         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_ucom.sc_udev,
393                            sc->sc_ucom.sc_dev);
394
395         return (rv);
396 }
397
398 usbd_status
399 uvisor_init(struct uvisor_softc *sc)
400 {
401         usbd_status err;
402         usb_device_request_t req;
403         struct uvisor_connection_info coninfo;
404         int actlen;
405         uWord avail;
406         char buffer[256];
407
408         DPRINTF(("uvisor_init: getting connection info\n"));
409         req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
410         req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
411         USETW(req.wValue, 0);
412         USETW(req.wIndex, 0);
413         USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
414         err = usbd_do_request_flags(sc->sc_ucom.sc_udev, &req, &coninfo,
415                                     USBD_SHORT_XFER_OK, &actlen,
416                                     USBD_DEFAULT_TIMEOUT);
417         if (err)
418                 return (err);
419
420 #ifdef USB_DEBUG
421         {
422                 int i, np;
423                 char *string;
424
425                 np = UGETW(coninfo.num_ports);
426                 kprintf("%s: Number of ports: %d\n", USBDEVNAME(sc->sc_ucom.sc_dev), np);
427                 for (i = 0; i < np; ++i) {
428                         switch (coninfo.connections[i].port_function_id) {
429                         case UVISOR_FUNCTION_GENERIC:
430                                 string = "Generic";
431                                 break;
432                         case UVISOR_FUNCTION_DEBUGGER:
433                                 string = "Debugger";
434                                 break;
435                         case UVISOR_FUNCTION_HOTSYNC:
436                                 string = "HotSync";
437                                 break;
438                         case UVISOR_FUNCTION_REMOTE_FILE_SYS:
439                                 string = "Remote File System";
440                                 break;
441                         default:
442                                 string = "unknown";
443                                 break;
444                         }
445                         kprintf("%s: port %d, is for %s\n",
446                             USBDEVNAME(sc->sc_ucom.sc_dev), coninfo.connections[i].port,
447                             string);
448                 }
449         }
450 #endif
451
452         if (sc->sc_flags & PALM4) {
453                 /* Palm OS 4.0 Hack */
454                 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
455                 req.bRequest = UVISOR_GET_PALM_INFORMATION;
456                 USETW(req.wValue, 0);
457                 USETW(req.wIndex, 0);
458                 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
459                 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, buffer);
460                 if (err)
461                         return (err);
462                 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
463                 req.bRequest = UVISOR_GET_PALM_INFORMATION;
464                 USETW(req.wValue, 0);
465                 USETW(req.wIndex, 0);
466                 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
467                 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, buffer);
468                 if (err)
469                         return (err);
470         }
471
472         DPRINTF(("uvisor_init: getting available bytes\n"));
473         req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
474         req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
475         USETW(req.wValue, 0);
476         USETW(req.wIndex, 5);
477         USETW(req.wLength, sizeof avail);
478         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, &avail);
479         if (err)
480                 return (err);
481         DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
482
483         DPRINTF(("uvisor_init: done\n"));
484         return (err);
485 }
486
487 void
488 uvisor_close(void *addr, int portno)
489 {
490         struct uvisor_softc *sc = addr;
491         usb_device_request_t req;
492         struct uvisor_connection_info coninfo; /* XXX ? */
493         int actlen;
494
495         if (sc->sc_ucom.sc_dying)
496                 return;
497
498         req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
499         req.bRequest = UVISOR_CLOSE_NOTIFICATION;
500         USETW(req.wValue, 0);
501         USETW(req.wIndex, 0);
502         USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
503         (void)usbd_do_request_flags(sc->sc_ucom.sc_udev, &req, &coninfo,
504                                     USBD_SHORT_XFER_OK, &actlen,
505                                     USBD_DEFAULT_TIMEOUT);
506 }