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 $
7 * Also already merged from NetBSD:
8 * $NetBSD: uvisor.c,v 1.12 2001/11/13 06:24:57 lukem Exp $
9 * $NetBSD: uvisor.c,v 1.13 2002/02/11 15:11:49 augustss Exp $
10 * $NetBSD: uvisor.c,v 1.14 2002/02/27 23:00:03 augustss Exp $
11 * $NetBSD: uvisor.c,v 1.15 2002/06/16 15:01:31 augustss Exp $
12 * $NetBSD: uvisor.c,v 1.16 2002/07/11 21:14:36 augustss Exp $
13 * $NetBSD: uvisor.c,v 1.17 2002/08/13 11:38:15 augustss Exp $
14 * $NetBSD: uvisor.c,v 1.18 2003/02/05 00:50:14 augustss Exp $
15 * $NetBSD: uvisor.c,v 1.19 2003/02/07 18:12:37 augustss Exp $
16 * $NetBSD: uvisor.c,v 1.20 2003/04/11 01:30:10 simonb Exp $
20 * Copyright (c) 2000 The NetBSD Foundation, Inc.
21 * All rights reserved.
23 * This code is derived from software contributed to The NetBSD Foundation
24 * by Lennart Augustsson (lennart@augustsson.net) at
25 * Carlstedt Research & Technology.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the NetBSD
38 * Foundation, Inc. and its contributors.
39 * 4. Neither the name of The NetBSD Foundation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
44 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
45 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
46 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
47 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
49 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
51 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
53 * POSSIBILITY OF SUCH DAMAGE.
57 * Handspring Visor (Palmpilot compatible PDA) driver
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
66 #include <sys/sysctl.h>
68 #include <bus/usb/usb.h>
69 #include <bus/usb/usbhid.h>
71 #include <bus/usb/usbdi.h>
72 #include <bus/usb/usbdi_util.h>
74 #include "../ucom/ucomvar.h"
77 #define DPRINTF(x) if (uvisordebug) kprintf x
78 #define DPRINTFN(n,x) if (uvisordebug>(n)) kprintf x
80 SYSCTL_NODE(_hw_usb, OID_AUTO, uvisor, CTLFLAG_RW, 0, "USB uvisor");
81 SYSCTL_INT(_hw_usb_uvisor, OID_AUTO, debug, CTLFLAG_RW,
82 &uvisordebug, 0, "uvisor debug level");
88 #define UVISOR_CONFIG_INDEX 0
89 #define UVISOR_IFACE_INDEX 0
90 #define UVISOR_MODVER 1
92 /* From the Linux driver */
94 * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
95 * are available to be transfered to the host for the specified endpoint.
96 * Currently this is not used, and always returns 0x0001
98 #define UVISOR_REQUEST_BYTES_AVAILABLE 0x01
101 * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
102 * is now closing the pipe. An empty packet is sent in response.
104 #define UVISOR_CLOSE_NOTIFICATION 0x02
107 * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
108 * get the endpoints used by the connection.
110 #define UVISOR_GET_CONNECTION_INFORMATION 0x03
114 * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
116 #define UVISOR_MAX_CONN 8
117 struct uvisor_connection_info {
120 uByte port_function_id;
122 } connections[UVISOR_MAX_CONN];
124 #define UVISOR_CONNECTION_INFO_SIZE 18
126 /* struct uvisor_connection_info.connection[x].port defines: */
127 #define UVISOR_ENDPOINT_1 0x01
128 #define UVISOR_ENDPOINT_2 0x02
130 /* struct uvisor_connection_info.connection[x].port_function_id defines: */
131 #define UVISOR_FUNCTION_GENERIC 0x00
132 #define UVISOR_FUNCTION_DEBUGGER 0x01
133 #define UVISOR_FUNCTION_HOTSYNC 0x02
134 #define UVISOR_FUNCTION_CONSOLE 0x03
135 #define UVISOR_FUNCTION_REMOTE_FILE_SYS 0x04
138 * Unknown PalmOS stuff.
140 #define UVISOR_GET_PALM_INFORMATION 0x04
141 #define UVISOR_GET_PALM_INFORMATION_LEN 0x14
145 * Crank down UVISORBUFSIZE from 1024 to 64 to avoid a problem where
146 * the Palm device and the USB host controller deadlock. The USB host
147 * controller is expecting an early-end-of-transmission packet with 0
148 * data, and the Palm doesn't send one because it's already
149 * communicated the amount of data it's going to send in a header
150 * (which ucom/uvisor are oblivious to). This is the problem that has
151 * been known on the pilot-link lists as the "[Free]BSD USB problem",
152 * but not understood.
154 #define UVISORIBUFSIZE 64
155 #define UVISOROBUFSIZE 1024
157 struct uvisor_softc {
158 struct ucom_softc sc_ucom;
162 static usbd_status uvisor_init(struct uvisor_softc *);
164 static void uvisor_close(void *, int);
166 struct ucom_callback uvisor_callback = {
177 static device_probe_t uvisor_match;
178 static device_attach_t uvisor_attach;
179 static device_detach_t uvisor_detach;
180 static device_method_t uvisor_methods[] = {
181 /* Device interface */
182 DEVMETHOD(device_probe, uvisor_match),
183 DEVMETHOD(device_attach, uvisor_attach),
184 DEVMETHOD(device_detach, uvisor_detach),
189 static driver_t uvisor_driver = {
192 sizeof (struct uvisor_softc)
195 DRIVER_MODULE(uvisor, uhub, uvisor_driver, ucom_devclass, usbd_driver_load, NULL);
196 MODULE_DEPEND(uvisor, usb, 1, 1, 1);
197 MODULE_DEPEND(uvisor, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
198 MODULE_VERSION(uvisor, UVISOR_MODVER);
201 struct usb_devno uv_dev;
205 #define PALM35 0x0004
207 static const struct uvisor_type uvisor_devs[] = {
208 {{ USB_DEVICE(0x054c, 0x0066) }, 0 }, /* Sony Clie v4.0 */
209 {{ USB_DEVICE(0x054c, 0x0095) }, PALM4 }, /* Sony Clie s360 */
210 {{ USB_DEVICE(0x054c, 0x009a) }, 0 }, /* Sony Clie v4.1 */
211 {{ USB_DEVICE(0x054c, 0x00da) }, PALM4 }, /* Sony Clie nx60 */
212 {{ USB_DEVICE(0x082d, 0x0100) }, 0 }, /* Handspring Visor */
213 {{ USB_DEVICE(0x082d, 0x0200) }, PALM4 }, /* Handspring Treo */
214 {{ USB_DEVICE(0x0830, 0x0001) }, PALM4 }, /* Palm m500 */
215 {{ USB_DEVICE(0x0830, 0x0002) }, PALM4 }, /* Palm m505 */
216 {{ USB_DEVICE(0x0830, 0x0003) }, PALM4 }, /* Palm m515 */
217 {{ USB_DEVICE(0x0830, 0x0020) }, PALM4 }, /* Palm i705 */
218 {{ USB_DEVICE(0x0830, 0x0031) }, PALM4 }, /* Palm Tungsten Z */
219 {{ USB_DEVICE(0x0830, 0x0040) }, PALM4 }, /* Palm m125 */
220 {{ USB_DEVICE(0x0830, 0x0050) }, PALM4 }, /* Palm m130 */
221 {{ USB_DEVICE(0x0830, 0x0060) }, PALM4 }, /* Palm Tungsten T */
222 {{ USB_DEVICE(0x0830, 0x0070) }, PALM4 }, /* Palm Palm Zire */
223 {{ USB_DEVICE(0x4766, 0x0001) }, PALM4 }, /* Aceeca MEZ1000 RDA */
224 {{ USB_DEVICE(0x091e, 0x0004) }, PALM4 }, /* Garmin iQue 3600 */
225 {{ USB_DEVICE(0x0e67, 0x0002) }, PALM4 }, /* Fossil, Wrist PDA */
226 {{ USB_DEVICE(0x082d, 0x0300) }, PALM4 }, /* Handspring Treo 600 */
227 {{ USB_DEVICE(0x0830, 0x0060) }, PALM4 }, /* Palm Tungsten T */
228 {{ USB_DEVICE(0x0830, 0x0061) }, PALM4 }, /* Palm Zire 31 */
229 {{ USB_DEVICE(0x04e8, 0x6601) }, PALM4 }, /* Samsung I500 Palm USB */
230 {{ USB_DEVICE(0x054c, 0x0038) }, PALM35 }, /* Sony Clie v3.5 */
231 {{ USB_DEVICE(0x054c, 0x0169) }, PALM4 }, /* Sony Clie tj37 */
232 {{ USB_DEVICE(0x12ef, 0x0100) }, PALM4 }, /* Tapwave Zodiac */
234 #define uvisor_lookup(v, p) ((const struct uvisor_type *)usb_lookup(uvisor_devs, v, p))
237 uvisor_match(device_t self)
239 struct usb_attach_arg *uaa = device_get_ivars(self);
241 if (uaa->iface != NULL)
242 return (UMATCH_NONE);
244 DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n",
245 uaa->vendor, uaa->product));
247 return (uvisor_lookup(uaa->vendor, uaa->product) != NULL ?
248 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
252 uvisor_attach(device_t self)
254 struct uvisor_softc *sc = device_get_softc(self);
255 struct usb_attach_arg *uaa = device_get_ivars(self);
256 usbd_device_handle dev = uaa->device;
257 usbd_interface_handle iface;
258 usb_interface_descriptor_t *id;
259 usb_endpoint_descriptor_t *ed;
263 struct ucom_softc *ucom;
267 bzero(sc, sizeof (struct uvisor_softc));
271 ucom->sc_iface = uaa->iface;
273 devname = device_get_nameunit(ucom->sc_dev);
275 DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc));
277 /* Move the device into the configured state. */
278 err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1);
280 kprintf("\n%s: failed to set configuration, err=%s\n",
281 devname, usbd_errstr(err));
285 err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
287 kprintf("\n%s: failed to get interface, err=%s\n",
288 devname, usbd_errstr(err));
292 sc->sc_flags = uvisor_lookup(uaa->vendor, uaa->product)->uv_flags;
294 id = usbd_get_interface_descriptor(iface);
297 ucom->sc_iface = iface;
299 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
300 for (i = 0; i < id->bNumEndpoints; i++) {
302 ed = usbd_interface2endpoint_descriptor(iface, i);
304 kprintf("%s: could not read endpoint descriptor"
305 ": %s\n", devname, usbd_errstr(err));
309 addr = ed->bEndpointAddress;
310 dir = UE_GET_DIR(ed->bEndpointAddress);
311 attr = ed->bmAttributes & UE_XFERTYPE;
312 if (dir == UE_DIR_IN && attr == UE_BULK)
313 ucom->sc_bulkin_no = addr;
314 else if (dir == UE_DIR_OUT && attr == UE_BULK)
315 ucom->sc_bulkout_no = addr;
317 kprintf("%s: unexpected endpoint\n", devname);
321 if (ucom->sc_bulkin_no == -1) {
322 kprintf("%s: Could not find data bulk in\n",
323 device_get_nameunit(ucom->sc_dev));
326 if (ucom->sc_bulkout_no == -1) {
327 kprintf("%s: Could not find data bulk out\n",
328 device_get_nameunit(ucom->sc_dev));
332 ucom->sc_parent = sc;
333 ucom->sc_portno = UCOM_UNK_PORTNO;
334 /* bulkin, bulkout set above */
335 ucom->sc_ibufsize = UVISORIBUFSIZE;
336 ucom->sc_obufsize = UVISOROBUFSIZE;
337 ucom->sc_ibufsizepad = UVISORIBUFSIZE;
338 ucom->sc_opkthdrlen = 0;
339 ucom->sc_callback = &uvisor_callback;
341 err = uvisor_init(sc);
343 kprintf("%s: init failed, %s\n", device_get_nameunit(ucom->sc_dev),
348 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
351 DPRINTF(("uvisor: in=0x%x out=0x%x\n", ucom->sc_bulkin_no, ucom->sc_bulkout_no));
352 ucom_attach(&sc->sc_ucom);
357 DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
365 uvisor_activate(device_t self, enum devact act)
367 struct uvisor_softc *sc = (struct uvisor_softc *)self;
375 case DVACT_DEACTIVATE:
376 if (sc->sc_subdev != NULL)
377 rv = config_deactivate(sc->sc_subdev);
387 uvisor_detach(device_t self)
389 struct uvisor_softc *sc = device_get_softc(self);
392 DPRINTF(("uvisor_detach: sc=%p\n", sc));
393 sc->sc_ucom.sc_dying = 1;
394 rv = ucom_detach(&sc->sc_ucom);
396 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_ucom.sc_udev,
403 uvisor_init(struct uvisor_softc *sc)
406 usb_device_request_t req;
407 struct uvisor_connection_info coninfo;
412 DPRINTF(("uvisor_init: getting connection info\n"));
413 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
414 req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
415 USETW(req.wValue, 0);
416 USETW(req.wIndex, 0);
417 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
418 err = usbd_do_request_flags(sc->sc_ucom.sc_udev, &req, &coninfo,
419 USBD_SHORT_XFER_OK, &actlen,
420 USBD_DEFAULT_TIMEOUT);
429 np = UGETW(coninfo.num_ports);
430 kprintf("%s: Number of ports: %d\n", device_get_nameunit(sc->sc_ucom.sc_dev), np);
431 for (i = 0; i < np; ++i) {
432 switch (coninfo.connections[i].port_function_id) {
433 case UVISOR_FUNCTION_GENERIC:
436 case UVISOR_FUNCTION_DEBUGGER:
439 case UVISOR_FUNCTION_HOTSYNC:
442 case UVISOR_FUNCTION_REMOTE_FILE_SYS:
443 string = "Remote File System";
449 kprintf("%s: port %d, is for %s\n",
450 device_get_nameunit(sc->sc_ucom.sc_dev), coninfo.connections[i].port,
456 if (sc->sc_flags & PALM4) {
457 /* Palm OS 4.0 Hack */
458 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
459 req.bRequest = UVISOR_GET_PALM_INFORMATION;
460 USETW(req.wValue, 0);
461 USETW(req.wIndex, 0);
462 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
463 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, buffer);
466 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
467 req.bRequest = UVISOR_GET_PALM_INFORMATION;
468 USETW(req.wValue, 0);
469 USETW(req.wIndex, 0);
470 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
471 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, buffer);
476 if (sc->sc_flags & PALM35) {
477 /* get the config number */
478 req.bmRequestType = UT_READ;
479 req.bRequest = UR_GET_CONFIG;
480 USETW(req.wValue, 0);
481 USETW(req.wIndex, 0);
482 USETW(req.wLength, 1);
483 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, buffer);
487 /* get the interface number */
488 req.bmRequestType = UT_READ_DEVICE;
489 req.bRequest = UR_GET_INTERFACE;
490 USETW(req.wValue, 0);
491 USETW(req.wIndex, 0);
492 USETW(req.wLength, 1);
493 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, buffer);
498 DPRINTF(("uvisor_init: getting available bytes\n"));
499 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
500 req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
501 USETW(req.wValue, 0);
502 USETW(req.wIndex, 5);
503 USETW(req.wLength, sizeof avail);
504 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, &avail);
507 DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
509 DPRINTF(("uvisor_init: done\n"));
514 uvisor_close(void *addr, int portno)
516 struct uvisor_softc *sc = addr;
517 usb_device_request_t req;
518 struct uvisor_connection_info coninfo; /* XXX ? */
521 if (sc->sc_ucom.sc_dying)
524 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
525 req.bRequest = UVISOR_CLOSE_NOTIFICATION;
526 USETW(req.wValue, 0);
527 USETW(req.wIndex, 0);
528 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
529 (void)usbd_do_request_flags(sc->sc_ucom.sc_udev, &req, &coninfo,
530 USBD_SHORT_XFER_OK, &actlen,
531 USBD_DEFAULT_TIMEOUT);