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 $ */
4 /* This version of uvisor is heavily based upon the version in NetBSD
5 * but is missing the following patches:
7 * 1.10 needed? connect a ucom to each of the uvisor ports
8 * 1.11 needed ucom has an "info" attach message - use it
9 * 1.12 not needed rcsids
10 * 1.13 already merged extra arg to usbd_do_request_flags
11 * 1.14 already merged sony and palm support
12 * 1.15 already merged sony clie
13 * 1.16 already merged trailing whites
17 * Copyright (c) 2000 The NetBSD Foundation, Inc.
18 * All rights reserved.
20 * This code is derived from software contributed to The NetBSD Foundation
21 * by Lennart Augustsson (lennart@augustsson.net) at
22 * Carlstedt Research & Technology.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by the NetBSD
35 * Foundation, Inc. and its contributors.
36 * 4. Neither the name of The NetBSD Foundation nor the names of its
37 * contributors may be used to endorse or promote products derived
38 * from this software without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
41 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
42 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
44 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50 * POSSIBILITY OF SUCH DAMAGE.
54 * Handspring Visor (Palmpilot compatible PDA) driver
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/kernel.h>
60 #if defined(__NetBSD__) || defined(__OpenBSD__)
61 #include <sys/device.h>
62 #elif defined(__FreeBSD__)
67 #include <sys/sysctl.h>
69 #include <dev/usb/usb.h>
70 #include <dev/usb/usbhid.h>
72 #include <dev/usb/usbdi.h>
73 #include <dev/usb/usbdi_util.h>
74 #include <dev/usb/usbdevs.h>
76 #include <dev/usb/ucomvar.h>
79 #define DPRINTF(x) if (uvisordebug) printf x
80 #define DPRINTFN(n,x) if (uvisordebug>(n)) printf x
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");
90 #define UVISOR_CONFIG_INDEX 0
91 #define UVISOR_IFACE_INDEX 0
92 #define UVISOR_MODVER 1
94 /* From the Linux driver */
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
100 #define UVISOR_REQUEST_BYTES_AVAILABLE 0x01
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.
106 #define UVISOR_CLOSE_NOTIFICATION 0x02
109 * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
110 * get the endpoints used by the connection.
112 #define UVISOR_GET_CONNECTION_INFORMATION 0x03
116 * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
118 #define UVISOR_MAX_CONN 8
119 struct uvisor_connection_info {
122 uByte port_function_id;
124 } connections[UVISOR_MAX_CONN];
126 #define UVISOR_CONNECTION_INFO_SIZE 18
128 /* struct uvisor_connection_info.connection[x].port defines: */
129 #define UVISOR_ENDPOINT_1 0x01
130 #define UVISOR_ENDPOINT_2 0x02
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
140 * Unknown PalmOS stuff.
142 #define UVISOR_GET_PALM_INFORMATION 0x04
143 #define UVISOR_GET_PALM_INFORMATION_LEN 0x14
146 #define UVISORIBUFSIZE 1024
147 #define UVISOROBUFSIZE 1024
149 struct uvisor_softc {
150 struct ucom_softc sc_ucom;
154 Static usbd_status uvisor_init(struct uvisor_softc *);
156 Static void uvisor_close(void *, int);
158 struct ucom_callback uvisor_callback = {
169 Static device_probe_t uvisor_match;
170 Static device_attach_t uvisor_attach;
171 Static device_detach_t uvisor_detach;
172 Static device_method_t uvisor_methods[] = {
173 /* Device interface */
174 DEVMETHOD(device_probe, uvisor_match),
175 DEVMETHOD(device_attach, uvisor_attach),
176 DEVMETHOD(device_detach, uvisor_detach),
181 Static driver_t uvisor_driver = {
184 sizeof (struct uvisor_softc)
187 DRIVER_MODULE(uvisor, uhub, uvisor_driver, ucom_devclass, usbd_driver_load, 0);
188 MODULE_DEPEND(uvisor, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
189 MODULE_VERSION(uvisor, UVISOR_MODVER);
192 struct usb_devno uv_dev;
196 static const struct uvisor_type uvisor_devs[] = {
197 {{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_VISOR }, 0 },
198 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M500 }, PALM4 },
199 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M505 }, PALM4 },
200 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M515 }, PALM4 },
201 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M125 }, PALM4 },
202 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40 }, PALM4 },
203 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_41 }, 0 },
204 /* {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25 }, PALM4 },*/
206 #define uvisor_lookup(v, p) ((const struct uvisor_type *)usb_lookup(uvisor_devs, v, p))
211 USB_MATCH_START(uvisor, uaa);
213 if (uaa->iface != NULL)
214 return (UMATCH_NONE);
216 DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n",
217 uaa->vendor, uaa->product));
219 return (uvisor_lookup(uaa->vendor, uaa->product) != NULL ?
220 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
225 USB_ATTACH_START(uvisor, sc, uaa);
226 usbd_device_handle dev = uaa->device;
227 usbd_interface_handle iface;
228 usb_interface_descriptor_t *id;
229 usb_endpoint_descriptor_t *ed;
234 struct ucom_softc *ucom;
236 devinfo = malloc(1024, M_USBDEV, M_WAITOK);
239 bzero(sc, sizeof (struct uvisor_softc));
240 usbd_devinfo(dev, 0, devinfo);
243 device_set_desc_copy(self, devinfo);
246 ucom->sc_iface = uaa->iface;
248 devname = USBDEVNAME(ucom->sc_dev);
249 printf("%s: %s\n", devname, devinfo);
251 DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc));
253 /* Move the device into the configured state. */
254 err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1);
256 printf("\n%s: failed to set configuration, err=%s\n",
257 devname, usbd_errstr(err));
261 err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
263 printf("\n%s: failed to get interface, err=%s\n",
264 devname, usbd_errstr(err));
268 printf("%s: %s\n", devname, devinfo);
270 sc->sc_flags = uvisor_lookup(uaa->vendor, uaa->product)->uv_flags;
272 id = usbd_get_interface_descriptor(iface);
275 ucom->sc_iface = iface;
277 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
278 for (i = 0; i < id->bNumEndpoints; i++) {
280 ed = usbd_interface2endpoint_descriptor(iface, i);
282 printf("%s: could not read endpoint descriptor"
283 ": %s\n", devname, usbd_errstr(err));
287 addr = ed->bEndpointAddress;
288 dir = UE_GET_DIR(ed->bEndpointAddress);
289 attr = ed->bmAttributes & UE_XFERTYPE;
290 if (dir == UE_DIR_IN && attr == UE_BULK)
291 ucom->sc_bulkin_no = addr;
292 else if (dir == UE_DIR_OUT && attr == UE_BULK)
293 ucom->sc_bulkout_no = addr;
295 printf("%s: unexpected endpoint\n", devname);
299 if (ucom->sc_bulkin_no == -1) {
300 printf("%s: Could not find data bulk in\n",
301 USBDEVNAME(ucom->sc_dev));
304 if (ucom->sc_bulkout_no == -1) {
305 printf("%s: Could not find data bulk out\n",
306 USBDEVNAME(ucom->sc_dev));
310 ucom->sc_parent = sc;
311 ucom->sc_portno = UCOM_UNK_PORTNO;
312 /* bulkin, bulkout set above */
313 ucom->sc_ibufsize = UVISORIBUFSIZE;
314 ucom->sc_obufsize = UVISOROBUFSIZE;
315 ucom->sc_ibufsizepad = UVISORIBUFSIZE;
316 ucom->sc_opkthdrlen = 0;
317 ucom->sc_callback = &uvisor_callback;
319 err = uvisor_init(sc);
321 printf("%s: init failed, %s\n", USBDEVNAME(ucom->sc_dev),
326 DPRINTF(("uvisor: in=0x%x out=0x%x\n", ucom->sc_bulkin_no, ucom->sc_bulkout_no));
327 ucom_attach(&sc->sc_ucom);
329 USB_ATTACH_SUCCESS_RETURN;
332 DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
334 USB_ATTACH_ERROR_RETURN;
340 uvisor_activate(device_ptr_t self, enum devact act)
342 struct uvisor_softc *sc = (struct uvisor_softc *)self;
350 case DVACT_DEACTIVATE:
351 if (sc->sc_subdev != NULL)
352 rv = config_deactivate(sc->sc_subdev);
363 USB_DETACH_START(uvisor, sc);
366 DPRINTF(("uvisor_detach: sc=%p\n", sc));
367 sc->sc_ucom.sc_dying = 1;
368 rv = ucom_detach(&sc->sc_ucom);
374 uvisor_init(struct uvisor_softc *sc)
377 usb_device_request_t req;
378 struct uvisor_connection_info coninfo;
383 DPRINTF(("uvisor_init: getting connection info\n"));
384 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
385 req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
386 USETW(req.wValue, 0);
387 USETW(req.wIndex, 0);
388 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
389 err = usbd_do_request_flags(sc->sc_ucom.sc_udev, &req, &coninfo,
390 USBD_SHORT_XFER_OK, &actlen);
399 np = UGETW(coninfo.num_ports);
400 printf("%s: Number of ports: %d\n", USBDEVNAME(sc->sc_ucom.sc_dev), np);
401 for (i = 0; i < np; ++i) {
402 switch (coninfo.connections[i].port_function_id) {
403 case UVISOR_FUNCTION_GENERIC:
406 case UVISOR_FUNCTION_DEBUGGER:
409 case UVISOR_FUNCTION_HOTSYNC:
412 case UVISOR_FUNCTION_REMOTE_FILE_SYS:
413 string = "Remote File System";
419 printf("%s: port %d, is for %s\n",
420 USBDEVNAME(sc->sc_ucom.sc_dev), coninfo.connections[i].port,
426 if (sc->sc_flags & PALM4) {
427 /* Palm OS 4.0 Hack */
428 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
429 req.bRequest = UVISOR_GET_PALM_INFORMATION;
430 USETW(req.wValue, 0);
431 USETW(req.wIndex, 0);
432 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
433 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, buffer);
436 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
437 req.bRequest = UVISOR_GET_PALM_INFORMATION;
438 USETW(req.wValue, 0);
439 USETW(req.wIndex, 0);
440 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
441 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, buffer);
446 DPRINTF(("uvisor_init: getting available bytes\n"));
447 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
448 req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
449 USETW(req.wValue, 0);
450 USETW(req.wIndex, 5);
451 USETW(req.wLength, sizeof avail);
452 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, &avail);
455 DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
457 DPRINTF(("uvisor_init: done\n"));
462 uvisor_close(void *addr, int portno)
464 struct uvisor_softc *sc = addr;
465 usb_device_request_t req;
466 struct uvisor_connection_info coninfo; /* XXX ? */
469 if (sc->sc_ucom.sc_dying)
472 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
473 req.bRequest = UVISOR_CLOSE_NOTIFICATION;
474 USETW(req.wValue, 0);
475 USETW(req.wIndex, 0);
476 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
477 (void)usbd_do_request_flags(sc->sc_ucom.sc_udev, &req, &coninfo,
478 USBD_SHORT_XFER_OK, &actlen);