1 /* $NetBSD: uvisor.c,v 1.9 2001/01/23 14:04:14 augustss Exp $ */
4 /* Also already merged from NetBSD:
5 * $NetBSD: uvisor.c,v 1.12 2001/11/13 06:24:57 lukem Exp $
6 * $NetBSD: uvisor.c,v 1.13 2002/02/11 15:11:49 augustss Exp $
7 * $NetBSD: uvisor.c,v 1.14 2002/02/27 23:00:03 augustss Exp $
8 * $NetBSD: uvisor.c,v 1.15 2002/06/16 15:01:31 augustss Exp $
9 * $NetBSD: uvisor.c,v 1.16 2002/07/11 21:14:36 augustss Exp $
10 * $NetBSD: uvisor.c,v 1.17 2002/08/13 11:38:15 augustss Exp $
11 * $NetBSD: uvisor.c,v 1.18 2003/02/05 00:50:14 augustss Exp $
12 * $NetBSD: uvisor.c,v 1.19 2003/02/07 18:12:37 augustss Exp $
13 * $NetBSD: uvisor.c,v 1.20 2003/04/11 01:30:10 simonb Exp $
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.
33 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
34 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
35 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
36 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
37 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
38 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
39 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
40 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
41 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43 * POSSIBILITY OF SUCH DAMAGE.
47 * Handspring Visor (Palmpilot compatible PDA) driver
50 #include <sys/stdint.h>
51 #include <sys/stddef.h>
52 #include <sys/param.h>
53 #include <sys/queue.h>
54 #include <sys/types.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
58 #include <sys/module.h>
60 #include <sys/mutex.h>
61 #include <sys/condvar.h>
62 #include <sys/sysctl.h>
64 #include <sys/unistd.h>
65 #include <sys/callout.h>
66 #include <sys/malloc.h>
69 #include <dev/usb/usb.h>
70 #include <dev/usb/usbdi.h>
71 #include <dev/usb/usbdi_util.h>
74 #define USB_DEBUG_VAR uvisor_debug
75 #include <dev/usb/usb_debug.h>
76 #include <dev/usb/usb_process.h>
78 #include <dev/usb/serial/usb_serial.h>
81 static int uvisor_debug = 0;
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 &uvisor_debug, 0, "Debug level");
88 #define UVISOR_CONFIG_INDEX 0
89 #define UVISOR_IFACE_INDEX 0
92 * The following buffer sizes are hardcoded due to the way the Palm
93 * firmware works. It looks like the device is not short terminating
94 * the data transferred.
96 #define UVISORIBUFSIZE 0 /* Use wMaxPacketSize */
97 #define UVISOROBUFSIZE 32 /* bytes */
98 #define UVISOROFRAMES 32 /* units */
100 /* From the Linux driver */
102 * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
103 * are available to be transfered to the host for the specified endpoint.
104 * Currently this is not used, and always returns 0x0001
106 #define UVISOR_REQUEST_BYTES_AVAILABLE 0x01
109 * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
110 * is now closing the pipe. An empty packet is sent in response.
112 #define UVISOR_CLOSE_NOTIFICATION 0x02
115 * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
116 * get the endpoints used by the connection.
118 #define UVISOR_GET_CONNECTION_INFORMATION 0x03
121 * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
123 #define UVISOR_MAX_CONN 8
124 struct uvisor_connection_info {
127 uByte port_function_id;
129 } __packed connections[UVISOR_MAX_CONN];
132 #define UVISOR_CONNECTION_INFO_SIZE 18
134 /* struct uvisor_connection_info.connection[x].port defines: */
135 #define UVISOR_ENDPOINT_1 0x01
136 #define UVISOR_ENDPOINT_2 0x02
138 /* struct uvisor_connection_info.connection[x].port_function_id defines: */
139 #define UVISOR_FUNCTION_GENERIC 0x00
140 #define UVISOR_FUNCTION_DEBUGGER 0x01
141 #define UVISOR_FUNCTION_HOTSYNC 0x02
142 #define UVISOR_FUNCTION_CONSOLE 0x03
143 #define UVISOR_FUNCTION_REMOTE_FILE_SYS 0x04
146 * Unknown PalmOS stuff.
148 #define UVISOR_GET_PALM_INFORMATION 0x04
149 #define UVISOR_GET_PALM_INFORMATION_LEN 0x44
151 struct uvisor_palm_connection_info {
153 uByte endpoint_numbers_different;
156 uDWord port_function_id;
158 uByte end_point_info;
160 } __packed connections[UVISOR_MAX_CONN];
169 struct uvisor_softc {
170 struct ucom_super_softc sc_super_ucom;
171 struct ucom_softc sc_ucom;
173 struct usb_xfer *sc_xfer[UVISOR_N_TRANSFER];
174 struct usb_device *sc_udev;
178 #define UVISOR_FLAG_PALM4 0x0001
179 #define UVISOR_FLAG_VISOR 0x0002
180 #define UVISOR_FLAG_PALM35 0x0004
181 #define UVISOR_FLAG_SEND_NOTIFY 0x0008
184 uint8_t sc_iface_index;
189 static device_probe_t uvisor_probe;
190 static device_attach_t uvisor_attach;
191 static device_detach_t uvisor_detach;
193 static usb_callback_t uvisor_write_callback;
194 static usb_callback_t uvisor_read_callback;
196 static usb_error_t uvisor_init(struct uvisor_softc *, struct usb_device *,
197 struct usb_config *);
198 static void uvisor_cfg_open(struct ucom_softc *);
199 static void uvisor_cfg_close(struct ucom_softc *);
200 static void uvisor_start_read(struct ucom_softc *);
201 static void uvisor_stop_read(struct ucom_softc *);
202 static void uvisor_start_write(struct ucom_softc *);
203 static void uvisor_stop_write(struct ucom_softc *);
205 static const struct usb_config uvisor_config[UVISOR_N_TRANSFER] = {
207 [UVISOR_BULK_DT_WR] = {
209 .endpoint = UE_ADDR_ANY,
210 .direction = UE_DIR_OUT,
211 .bufsize = UVISOROBUFSIZE * UVISOROFRAMES,
212 .frames = UVISOROFRAMES,
213 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
214 .callback = &uvisor_write_callback,
217 [UVISOR_BULK_DT_RD] = {
219 .endpoint = UE_ADDR_ANY,
220 .direction = UE_DIR_IN,
221 .bufsize = UVISORIBUFSIZE,
222 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
223 .callback = &uvisor_read_callback,
227 static const struct ucom_callback uvisor_callback = {
228 .ucom_cfg_open = &uvisor_cfg_open,
229 .ucom_cfg_close = &uvisor_cfg_close,
230 .ucom_start_read = &uvisor_start_read,
231 .ucom_stop_read = &uvisor_stop_read,
232 .ucom_start_write = &uvisor_start_write,
233 .ucom_stop_write = &uvisor_stop_write,
236 static device_method_t uvisor_methods[] = {
237 DEVMETHOD(device_probe, uvisor_probe),
238 DEVMETHOD(device_attach, uvisor_attach),
239 DEVMETHOD(device_detach, uvisor_detach),
243 static devclass_t uvisor_devclass;
245 static driver_t uvisor_driver = {
247 .methods = uvisor_methods,
248 .size = sizeof(struct uvisor_softc),
251 DRIVER_MODULE(uvisor, uhub, uvisor_driver, uvisor_devclass, NULL, 0);
252 MODULE_DEPEND(uvisor, ucom, 1, 1, 1);
253 MODULE_DEPEND(uvisor, usb, 1, 1, 1);
254 MODULE_VERSION(uvisor, 1);
256 static const STRUCT_USB_HOST_ID uvisor_devs[] = {
257 #define UVISOR_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
258 UVISOR_DEV(ACEECA, MEZ1000, UVISOR_FLAG_PALM4),
259 UVISOR_DEV(ALPHASMART, DANA_SYNC, UVISOR_FLAG_PALM4),
260 UVISOR_DEV(GARMIN, IQUE_3600, UVISOR_FLAG_PALM4),
261 UVISOR_DEV(FOSSIL, WRISTPDA, UVISOR_FLAG_PALM4),
262 UVISOR_DEV(HANDSPRING, VISOR, UVISOR_FLAG_VISOR),
263 UVISOR_DEV(HANDSPRING, TREO, UVISOR_FLAG_PALM4),
264 UVISOR_DEV(HANDSPRING, TREO600, UVISOR_FLAG_PALM4),
265 UVISOR_DEV(PALM, M500, UVISOR_FLAG_PALM4),
266 UVISOR_DEV(PALM, M505, UVISOR_FLAG_PALM4),
267 UVISOR_DEV(PALM, M515, UVISOR_FLAG_PALM4),
268 UVISOR_DEV(PALM, I705, UVISOR_FLAG_PALM4),
269 UVISOR_DEV(PALM, M125, UVISOR_FLAG_PALM4),
270 UVISOR_DEV(PALM, M130, UVISOR_FLAG_PALM4),
271 UVISOR_DEV(PALM, TUNGSTEN_Z, UVISOR_FLAG_PALM4),
272 UVISOR_DEV(PALM, TUNGSTEN_T, UVISOR_FLAG_PALM4),
273 UVISOR_DEV(PALM, ZIRE, UVISOR_FLAG_PALM4),
274 UVISOR_DEV(PALM, ZIRE31, UVISOR_FLAG_PALM4),
275 UVISOR_DEV(SAMSUNG, I500, UVISOR_FLAG_PALM4),
276 UVISOR_DEV(SONY, CLIE_40, 0),
277 UVISOR_DEV(SONY, CLIE_41, 0),
278 UVISOR_DEV(SONY, CLIE_S360, UVISOR_FLAG_PALM4),
279 UVISOR_DEV(SONY, CLIE_NX60, UVISOR_FLAG_PALM4),
280 UVISOR_DEV(SONY, CLIE_35, UVISOR_FLAG_PALM35),
281 /* UVISOR_DEV(SONY, CLIE_25, UVISOR_FLAG_PALM4 ), */
282 UVISOR_DEV(SONY, CLIE_TJ37, UVISOR_FLAG_PALM4),
283 /* UVISOR_DEV(SONY, CLIE_TH55, UVISOR_FLAG_PALM4 ), See PR 80935 */
284 UVISOR_DEV(TAPWAVE, ZODIAC, UVISOR_FLAG_PALM4),
289 uvisor_probe(device_t dev)
291 struct usb_attach_arg *uaa = device_get_ivars(dev);
293 if (uaa->usb_mode != USB_MODE_HOST) {
296 if (uaa->info.bConfigIndex != UVISOR_CONFIG_INDEX) {
299 if (uaa->info.bIfaceIndex != UVISOR_IFACE_INDEX) {
302 return (usbd_lookup_id_by_uaa(uvisor_devs, sizeof(uvisor_devs), uaa));
306 uvisor_attach(device_t dev)
308 struct usb_attach_arg *uaa = device_get_ivars(dev);
309 struct uvisor_softc *sc = device_get_softc(dev);
310 struct usb_config uvisor_config_copy[UVISOR_N_TRANSFER];
313 DPRINTF("sc=%p\n", sc);
314 bcopy(uvisor_config, uvisor_config_copy,
315 sizeof(uvisor_config_copy));
316 device_set_usb_desc(dev);
318 mtx_init(&sc->sc_mtx, "uvisor", NULL, MTX_DEF);
320 sc->sc_udev = uaa->device;
322 /* configure the device */
324 sc->sc_flag = USB_GET_DRIVER_INFO(uaa);
325 sc->sc_iface_no = uaa->info.bIfaceNum;
326 sc->sc_iface_index = UVISOR_IFACE_INDEX;
328 error = uvisor_init(sc, uaa->device, uvisor_config_copy);
331 DPRINTF("init failed, error=%s\n",
335 error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
336 sc->sc_xfer, uvisor_config_copy, UVISOR_N_TRANSFER,
339 DPRINTF("could not allocate all pipes\n");
343 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
344 &uvisor_callback, &sc->sc_mtx);
346 DPRINTF("ucom_attach failed\n");
349 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
359 uvisor_detach(device_t dev)
361 struct uvisor_softc *sc = device_get_softc(dev);
363 DPRINTF("sc=%p\n", sc);
365 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
366 usbd_transfer_unsetup(sc->sc_xfer, UVISOR_N_TRANSFER);
367 mtx_destroy(&sc->sc_mtx);
373 uvisor_init(struct uvisor_softc *sc, struct usb_device *udev, struct usb_config *config)
376 struct usb_device_request req;
377 struct uvisor_connection_info coninfo;
378 struct uvisor_palm_connection_info pconinfo;
382 if (sc->sc_flag & UVISOR_FLAG_VISOR) {
383 DPRINTF("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(udev, NULL,
390 &req, &coninfo, USB_SHORT_XFER_OK,
391 &actlen, USB_DEFAULT_TIMEOUT);
398 if (sc->sc_flag & UVISOR_FLAG_VISOR) {
402 np = UGETW(coninfo.num_ports);
403 if (np > UVISOR_MAX_CONN) {
404 np = UVISOR_MAX_CONN;
406 DPRINTF("Number of ports: %d\n", np);
408 for (i = 0; i < np; ++i) {
409 switch (coninfo.connections[i].port_function_id) {
410 case UVISOR_FUNCTION_GENERIC:
413 case UVISOR_FUNCTION_DEBUGGER:
416 case UVISOR_FUNCTION_HOTSYNC:
419 case UVISOR_FUNCTION_REMOTE_FILE_SYS:
420 desc = "Remote File System";
426 DPRINTF("Port %d is for %s\n",
427 coninfo.connections[i].port, desc);
432 if (sc->sc_flag & UVISOR_FLAG_PALM4) {
435 /* Palm OS 4.0 Hack */
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);
442 err = usbd_do_request_flags
443 (udev, NULL, &req, &pconinfo, USB_SHORT_XFER_OK,
444 &actlen, USB_DEFAULT_TIMEOUT);
450 DPRINTF("too little data\n");
454 if (pconinfo.endpoint_numbers_different) {
455 port = pconinfo.connections[0].end_point_info;
456 config[0].endpoint = (port & 0xF); /* output */
457 config[1].endpoint = (port >> 4); /* input */
459 port = pconinfo.connections[0].port;
460 config[0].endpoint = (port & 0xF); /* output */
461 config[1].endpoint = (port & 0xF); /* input */
464 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
465 req.bRequest = UVISOR_GET_PALM_INFORMATION;
466 USETW(req.wValue, 0);
467 USETW(req.wIndex, 0);
468 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
469 err = usbd_do_request(udev, &req, buffer);
475 if (sc->sc_flag & UVISOR_FLAG_PALM35) {
476 /* get the config number */
477 DPRINTF("getting config info\n");
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);
484 err = usbd_do_request(udev, NULL, &req, buffer);
488 /* get the interface number */
489 DPRINTF("get the interface number\n");
490 req.bmRequestType = UT_READ_DEVICE;
491 req.bRequest = UR_GET_INTERFACE;
492 USETW(req.wValue, 0);
493 USETW(req.wIndex, 0);
494 USETW(req.wLength, 1);
495 err = usbd_do_request(udev, NULL, &req, buffer);
503 DPRINTF("getting available bytes\n");
504 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
505 req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
506 USETW(req.wValue, 0);
507 USETW(req.wIndex, 5);
508 USETW(req.wLength, sizeof(wAvail));
509 err = usbd_do_request(udev, NULL, &req, &wAvail);
513 DPRINTF("avail=%d\n", UGETW(wAvail));
522 uvisor_cfg_open(struct ucom_softc *ucom)
528 uvisor_cfg_close(struct ucom_softc *ucom)
530 struct uvisor_softc *sc = ucom->sc_parent;
531 uint8_t buffer[UVISOR_CONNECTION_INFO_SIZE];
532 struct usb_device_request req;
535 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
536 req.bRequest = UVISOR_CLOSE_NOTIFICATION;
537 USETW(req.wValue, 0);
538 USETW(req.wIndex, 0);
539 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
541 err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
542 &req, buffer, 0, 1000);
544 DPRINTFN(0, "close notification failed, error=%s\n",
550 uvisor_start_read(struct ucom_softc *ucom)
552 struct uvisor_softc *sc = ucom->sc_parent;
554 usbd_transfer_start(sc->sc_xfer[UVISOR_BULK_DT_RD]);
558 uvisor_stop_read(struct ucom_softc *ucom)
560 struct uvisor_softc *sc = ucom->sc_parent;
562 usbd_transfer_stop(sc->sc_xfer[UVISOR_BULK_DT_RD]);
566 uvisor_start_write(struct ucom_softc *ucom)
568 struct uvisor_softc *sc = ucom->sc_parent;
570 usbd_transfer_start(sc->sc_xfer[UVISOR_BULK_DT_WR]);
574 uvisor_stop_write(struct ucom_softc *ucom)
576 struct uvisor_softc *sc = ucom->sc_parent;
578 usbd_transfer_stop(sc->sc_xfer[UVISOR_BULK_DT_WR]);
582 uvisor_write_callback(struct usb_xfer *xfer, usb_error_t error)
584 struct uvisor_softc *sc = usbd_xfer_softc(xfer);
585 struct usb_page_cache *pc;
589 switch (USB_GET_STATE(xfer)) {
591 case USB_ST_TRANSFERRED:
593 for (x = 0; x != UVISOROFRAMES; x++) {
595 usbd_xfer_set_frame_offset(xfer,
596 x * UVISOROBUFSIZE, x);
598 pc = usbd_xfer_get_frame(xfer, x);
599 if (ucom_get_data(&sc->sc_ucom, pc, 0,
600 UVISOROBUFSIZE, &actlen)) {
601 usbd_xfer_set_frame_len(xfer, x, actlen);
608 usbd_xfer_set_frames(xfer, x);
609 usbd_transfer_submit(xfer);
614 if (error != USB_ERR_CANCELLED) {
615 /* try to clear stall first */
616 usbd_xfer_set_stall(xfer);
624 uvisor_read_callback(struct usb_xfer *xfer, usb_error_t error)
626 struct uvisor_softc *sc = usbd_xfer_softc(xfer);
627 struct usb_page_cache *pc;
630 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
632 switch (USB_GET_STATE(xfer)) {
633 case USB_ST_TRANSFERRED:
634 pc = usbd_xfer_get_frame(xfer, 0);
635 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
639 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
640 usbd_transfer_submit(xfer);
644 if (error != USB_ERR_CANCELLED) {
645 /* try to clear stall first */
646 usbd_xfer_set_stall(xfer);