1 /* $NetBSD: uvisor.c,v 1.9 2001/01/23 14:04:14 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/uvisor.c,v 1.7.2.3 2002/08/27 13:46:28 joe Exp $ */
3 /* $DragonFly: src/sys/dev/usbmisc/uvisor/uvisor.c,v 1.3 2003/08/07 21:17:15 dillon Exp $ */
5 /* This version of uvisor is heavily based upon the version in NetBSD
6 * but is missing the following patches:
8 * 1.10 needed? connect a ucom to each of the uvisor ports
9 * 1.11 needed ucom has an "info" attach message - use it
10 * 1.12 not needed rcsids
11 * 1.13 already merged extra arg to usbd_do_request_flags
12 * 1.14 already merged sony and palm support
13 * 1.15 already merged sony clie
14 * 1.16 already merged trailing whites
18 * Copyright (c) 2000 The NetBSD Foundation, Inc.
19 * All rights reserved.
21 * This code is derived from software contributed to The NetBSD Foundation
22 * by Lennart Augustsson (lennart@augustsson.net) at
23 * Carlstedt Research & Technology.
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
33 * 3. All advertising materials mentioning features or use of this software
34 * must display the following acknowledgement:
35 * This product includes software developed by the NetBSD
36 * Foundation, Inc. and its contributors.
37 * 4. Neither the name of The NetBSD Foundation nor the names of its
38 * contributors may be used to endorse or promote products derived
39 * from this software without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
42 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
43 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
45 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
46 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
47 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
48 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
51 * POSSIBILITY OF SUCH DAMAGE.
55 * Handspring Visor (Palmpilot compatible PDA) driver
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/kernel.h>
61 #if defined(__NetBSD__) || defined(__OpenBSD__)
62 #include <sys/device.h>
63 #elif defined(__FreeBSD__)
68 #include <sys/sysctl.h>
70 #include <bus/usb/usb.h>
71 #include <bus/usb/usbhid.h>
73 #include <bus/usb/usbdi.h>
74 #include <bus/usb/usbdi_util.h>
75 #include <bus/usb/usbdevs.h>
77 #include "../ucom/ucomvar.h"
80 #define DPRINTF(x) if (uvisordebug) printf x
81 #define DPRINTFN(n,x) if (uvisordebug>(n)) printf x
83 SYSCTL_NODE(_hw_usb, OID_AUTO, uvisor, CTLFLAG_RW, 0, "USB uvisor");
84 SYSCTL_INT(_hw_usb_uvisor, OID_AUTO, debug, CTLFLAG_RW,
85 &uvisordebug, 0, "uvisor debug level");
91 #define UVISOR_CONFIG_INDEX 0
92 #define UVISOR_IFACE_INDEX 0
93 #define UVISOR_MODVER 1
95 /* From the Linux driver */
97 * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
98 * are available to be transfered to the host for the specified endpoint.
99 * Currently this is not used, and always returns 0x0001
101 #define UVISOR_REQUEST_BYTES_AVAILABLE 0x01
104 * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
105 * is now closing the pipe. An empty packet is sent in response.
107 #define UVISOR_CLOSE_NOTIFICATION 0x02
110 * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
111 * get the endpoints used by the connection.
113 #define UVISOR_GET_CONNECTION_INFORMATION 0x03
117 * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
119 #define UVISOR_MAX_CONN 8
120 struct uvisor_connection_info {
123 uByte port_function_id;
125 } connections[UVISOR_MAX_CONN];
127 #define UVISOR_CONNECTION_INFO_SIZE 18
129 /* struct uvisor_connection_info.connection[x].port defines: */
130 #define UVISOR_ENDPOINT_1 0x01
131 #define UVISOR_ENDPOINT_2 0x02
133 /* struct uvisor_connection_info.connection[x].port_function_id defines: */
134 #define UVISOR_FUNCTION_GENERIC 0x00
135 #define UVISOR_FUNCTION_DEBUGGER 0x01
136 #define UVISOR_FUNCTION_HOTSYNC 0x02
137 #define UVISOR_FUNCTION_CONSOLE 0x03
138 #define UVISOR_FUNCTION_REMOTE_FILE_SYS 0x04
141 * Unknown PalmOS stuff.
143 #define UVISOR_GET_PALM_INFORMATION 0x04
144 #define UVISOR_GET_PALM_INFORMATION_LEN 0x14
147 #define UVISORIBUFSIZE 1024
148 #define UVISOROBUFSIZE 1024
150 struct uvisor_softc {
151 struct ucom_softc sc_ucom;
155 Static usbd_status uvisor_init(struct uvisor_softc *);
157 Static void uvisor_close(void *, int);
159 struct ucom_callback uvisor_callback = {
170 Static device_probe_t uvisor_match;
171 Static device_attach_t uvisor_attach;
172 Static device_detach_t uvisor_detach;
173 Static device_method_t uvisor_methods[] = {
174 /* Device interface */
175 DEVMETHOD(device_probe, uvisor_match),
176 DEVMETHOD(device_attach, uvisor_attach),
177 DEVMETHOD(device_detach, uvisor_detach),
182 Static driver_t uvisor_driver = {
185 sizeof (struct uvisor_softc)
188 DRIVER_MODULE(uvisor, uhub, uvisor_driver, ucom_devclass, usbd_driver_load, 0);
189 MODULE_DEPEND(uvisor, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
190 MODULE_VERSION(uvisor, UVISOR_MODVER);
193 struct usb_devno uv_dev;
197 static const struct uvisor_type uvisor_devs[] = {
198 {{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_VISOR }, 0 },
199 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M500 }, PALM4 },
200 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M505 }, PALM4 },
201 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M515 }, PALM4 },
202 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M125 }, PALM4 },
203 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40 }, PALM4 },
204 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_41 }, 0 },
205 /* {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25 }, PALM4 },*/
207 #define uvisor_lookup(v, p) ((const struct uvisor_type *)usb_lookup(uvisor_devs, v, p))
212 USB_MATCH_START(uvisor, uaa);
214 if (uaa->iface != NULL)
215 return (UMATCH_NONE);
217 DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n",
218 uaa->vendor, uaa->product));
220 return (uvisor_lookup(uaa->vendor, uaa->product) != NULL ?
221 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
226 USB_ATTACH_START(uvisor, sc, uaa);
227 usbd_device_handle dev = uaa->device;
228 usbd_interface_handle iface;
229 usb_interface_descriptor_t *id;
230 usb_endpoint_descriptor_t *ed;
235 struct ucom_softc *ucom;
237 devinfo = malloc(1024, M_USBDEV, M_WAITOK);
240 bzero(sc, sizeof (struct uvisor_softc));
241 usbd_devinfo(dev, 0, devinfo);
244 device_set_desc_copy(self, devinfo);
247 ucom->sc_iface = uaa->iface;
249 devname = USBDEVNAME(ucom->sc_dev);
250 printf("%s: %s\n", devname, devinfo);
252 DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc));
254 /* Move the device into the configured state. */
255 err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1);
257 printf("\n%s: failed to set configuration, err=%s\n",
258 devname, usbd_errstr(err));
262 err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
264 printf("\n%s: failed to get interface, err=%s\n",
265 devname, usbd_errstr(err));
269 printf("%s: %s\n", devname, devinfo);
271 sc->sc_flags = uvisor_lookup(uaa->vendor, uaa->product)->uv_flags;
273 id = usbd_get_interface_descriptor(iface);
276 ucom->sc_iface = iface;
278 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
279 for (i = 0; i < id->bNumEndpoints; i++) {
281 ed = usbd_interface2endpoint_descriptor(iface, i);
283 printf("%s: could not read endpoint descriptor"
284 ": %s\n", devname, usbd_errstr(err));
288 addr = ed->bEndpointAddress;
289 dir = UE_GET_DIR(ed->bEndpointAddress);
290 attr = ed->bmAttributes & UE_XFERTYPE;
291 if (dir == UE_DIR_IN && attr == UE_BULK)
292 ucom->sc_bulkin_no = addr;
293 else if (dir == UE_DIR_OUT && attr == UE_BULK)
294 ucom->sc_bulkout_no = addr;
296 printf("%s: unexpected endpoint\n", devname);
300 if (ucom->sc_bulkin_no == -1) {
301 printf("%s: Could not find data bulk in\n",
302 USBDEVNAME(ucom->sc_dev));
305 if (ucom->sc_bulkout_no == -1) {
306 printf("%s: Could not find data bulk out\n",
307 USBDEVNAME(ucom->sc_dev));
311 ucom->sc_parent = sc;
312 ucom->sc_portno = UCOM_UNK_PORTNO;
313 /* bulkin, bulkout set above */
314 ucom->sc_ibufsize = UVISORIBUFSIZE;
315 ucom->sc_obufsize = UVISOROBUFSIZE;
316 ucom->sc_ibufsizepad = UVISORIBUFSIZE;
317 ucom->sc_opkthdrlen = 0;
318 ucom->sc_callback = &uvisor_callback;
320 err = uvisor_init(sc);
322 printf("%s: init failed, %s\n", USBDEVNAME(ucom->sc_dev),
327 DPRINTF(("uvisor: in=0x%x out=0x%x\n", ucom->sc_bulkin_no, ucom->sc_bulkout_no));
328 ucom_attach(&sc->sc_ucom);
330 USB_ATTACH_SUCCESS_RETURN;
333 DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
335 USB_ATTACH_ERROR_RETURN;
341 uvisor_activate(device_ptr_t self, enum devact act)
343 struct uvisor_softc *sc = (struct uvisor_softc *)self;
351 case DVACT_DEACTIVATE:
352 if (sc->sc_subdev != NULL)
353 rv = config_deactivate(sc->sc_subdev);
364 USB_DETACH_START(uvisor, sc);
367 DPRINTF(("uvisor_detach: sc=%p\n", sc));
368 sc->sc_ucom.sc_dying = 1;
369 rv = ucom_detach(&sc->sc_ucom);
375 uvisor_init(struct uvisor_softc *sc)
378 usb_device_request_t req;
379 struct uvisor_connection_info coninfo;
384 DPRINTF(("uvisor_init: getting connection info\n"));
385 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
386 req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
387 USETW(req.wValue, 0);
388 USETW(req.wIndex, 0);
389 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
390 err = usbd_do_request_flags(sc->sc_ucom.sc_udev, &req, &coninfo,
391 USBD_SHORT_XFER_OK, &actlen);
400 np = UGETW(coninfo.num_ports);
401 printf("%s: Number of ports: %d\n", USBDEVNAME(sc->sc_ucom.sc_dev), np);
402 for (i = 0; i < np; ++i) {
403 switch (coninfo.connections[i].port_function_id) {
404 case UVISOR_FUNCTION_GENERIC:
407 case UVISOR_FUNCTION_DEBUGGER:
410 case UVISOR_FUNCTION_HOTSYNC:
413 case UVISOR_FUNCTION_REMOTE_FILE_SYS:
414 string = "Remote File System";
420 printf("%s: port %d, is for %s\n",
421 USBDEVNAME(sc->sc_ucom.sc_dev), coninfo.connections[i].port,
427 if (sc->sc_flags & PALM4) {
428 /* Palm OS 4.0 Hack */
429 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
430 req.bRequest = UVISOR_GET_PALM_INFORMATION;
431 USETW(req.wValue, 0);
432 USETW(req.wIndex, 0);
433 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
434 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, buffer);
437 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
438 req.bRequest = UVISOR_GET_PALM_INFORMATION;
439 USETW(req.wValue, 0);
440 USETW(req.wIndex, 0);
441 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
442 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, buffer);
447 DPRINTF(("uvisor_init: getting available bytes\n"));
448 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
449 req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
450 USETW(req.wValue, 0);
451 USETW(req.wIndex, 5);
452 USETW(req.wLength, sizeof avail);
453 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, &avail);
456 DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
458 DPRINTF(("uvisor_init: done\n"));
463 uvisor_close(void *addr, int portno)
465 struct uvisor_softc *sc = addr;
466 usb_device_request_t req;
467 struct uvisor_connection_info coninfo; /* XXX ? */
470 if (sc->sc_ucom.sc_dying)
473 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
474 req.bRequest = UVISOR_CLOSE_NOTIFICATION;
475 USETW(req.wValue, 0);
476 USETW(req.wIndex, 0);
477 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
478 (void)usbd_do_request_flags(sc->sc_ucom.sc_udev, &req, &coninfo,
479 USBD_SHORT_XFER_OK, &actlen);