xref: /freebsd-src/sys/dev/usb/serial/uvisor.c (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
102ac6454SAndrew Thompson /*	$NetBSD: uvisor.c,v 1.9 2001/01/23 14:04:14 augustss Exp $	*/
202ac6454SAndrew Thompson 
302ac6454SAndrew Thompson /* Also already merged from NetBSD:
402ac6454SAndrew Thompson  *	$NetBSD: uvisor.c,v 1.12 2001/11/13 06:24:57 lukem Exp $
502ac6454SAndrew Thompson  *	$NetBSD: uvisor.c,v 1.13 2002/02/11 15:11:49 augustss Exp $
602ac6454SAndrew Thompson  *	$NetBSD: uvisor.c,v 1.14 2002/02/27 23:00:03 augustss Exp $
702ac6454SAndrew Thompson  *	$NetBSD: uvisor.c,v 1.15 2002/06/16 15:01:31 augustss Exp $
802ac6454SAndrew Thompson  *	$NetBSD: uvisor.c,v 1.16 2002/07/11 21:14:36 augustss Exp $
902ac6454SAndrew Thompson  *	$NetBSD: uvisor.c,v 1.17 2002/08/13 11:38:15 augustss Exp $
1002ac6454SAndrew Thompson  *	$NetBSD: uvisor.c,v 1.18 2003/02/05 00:50:14 augustss Exp $
1102ac6454SAndrew Thompson  *	$NetBSD: uvisor.c,v 1.19 2003/02/07 18:12:37 augustss Exp $
1202ac6454SAndrew Thompson  *	$NetBSD: uvisor.c,v 1.20 2003/04/11 01:30:10 simonb Exp $
1302ac6454SAndrew Thompson  */
1402ac6454SAndrew Thompson 
1502ac6454SAndrew Thompson /*-
16*b61a5730SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
17718cf2ccSPedro F. Giffuni  *
1802ac6454SAndrew Thompson  * Copyright (c) 2000 The NetBSD Foundation, Inc.
1902ac6454SAndrew Thompson  * All rights reserved.
2002ac6454SAndrew Thompson  *
2102ac6454SAndrew Thompson  * This code is derived from software contributed to The NetBSD Foundation
2202ac6454SAndrew Thompson  * by Lennart Augustsson (lennart@augustsson.net) at
2302ac6454SAndrew Thompson  * Carlstedt Research & Technology.
2402ac6454SAndrew Thompson  *
2502ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
2602ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
2702ac6454SAndrew Thompson  * are met:
2802ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
2902ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
3002ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
3102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
3202ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
3302ac6454SAndrew Thompson  *
3402ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
3502ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
3602ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3702ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3802ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3902ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
4002ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
4102ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
4202ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4302ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
4402ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
4502ac6454SAndrew Thompson  */
4602ac6454SAndrew Thompson 
4702ac6454SAndrew Thompson /*
4802ac6454SAndrew Thompson  * Handspring Visor (Palmpilot compatible PDA) driver
4902ac6454SAndrew Thompson  */
5002ac6454SAndrew Thompson 
51ed6d949aSAndrew Thompson #include <sys/stdint.h>
52ed6d949aSAndrew Thompson #include <sys/stddef.h>
53ed6d949aSAndrew Thompson #include <sys/param.h>
54ed6d949aSAndrew Thompson #include <sys/queue.h>
55ed6d949aSAndrew Thompson #include <sys/types.h>
56ed6d949aSAndrew Thompson #include <sys/systm.h>
57ed6d949aSAndrew Thompson #include <sys/kernel.h>
58ed6d949aSAndrew Thompson #include <sys/bus.h>
59ed6d949aSAndrew Thompson #include <sys/module.h>
60ed6d949aSAndrew Thompson #include <sys/lock.h>
61ed6d949aSAndrew Thompson #include <sys/mutex.h>
62ed6d949aSAndrew Thompson #include <sys/condvar.h>
63ed6d949aSAndrew Thompson #include <sys/sysctl.h>
64ed6d949aSAndrew Thompson #include <sys/sx.h>
65ed6d949aSAndrew Thompson #include <sys/unistd.h>
66ed6d949aSAndrew Thompson #include <sys/callout.h>
67ed6d949aSAndrew Thompson #include <sys/malloc.h>
68ed6d949aSAndrew Thompson #include <sys/priv.h>
69ed6d949aSAndrew Thompson 
7002ac6454SAndrew Thompson #include <dev/usb/usb.h>
71ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
72ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
73ed6d949aSAndrew Thompson #include "usbdevs.h"
7402ac6454SAndrew Thompson 
7502ac6454SAndrew Thompson #define	USB_DEBUG_VAR uvisor_debug
7602ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
7702ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
7802ac6454SAndrew Thompson 
7902ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h>
8002ac6454SAndrew Thompson 
81b850ecc1SAndrew Thompson #ifdef USB_DEBUG
8202ac6454SAndrew Thompson static int uvisor_debug = 0;
8302ac6454SAndrew Thompson 
84f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, uvisor, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
85f8d2b1f3SPawel Biernacki     "USB uvisor");
86ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_uvisor, OID_AUTO, debug, CTLFLAG_RWTUN,
8702ac6454SAndrew Thompson     &uvisor_debug, 0, "Debug level");
8802ac6454SAndrew Thompson #endif
8902ac6454SAndrew Thompson 
9002ac6454SAndrew Thompson #define	UVISOR_CONFIG_INDEX	0
9102ac6454SAndrew Thompson #define	UVISOR_IFACE_INDEX	0
9240675d1aSAlfred Perlstein 
9340675d1aSAlfred Perlstein /*
9440675d1aSAlfred Perlstein  * The following buffer sizes are hardcoded due to the way the Palm
9540675d1aSAlfred Perlstein  * firmware works. It looks like the device is not short terminating
9640675d1aSAlfred Perlstein  * the data transferred.
9740675d1aSAlfred Perlstein  */
9840675d1aSAlfred Perlstein #define	UVISORIBUFSIZE	       0	/* Use wMaxPacketSize */
9940675d1aSAlfred Perlstein #define	UVISOROBUFSIZE	       32	/* bytes */
10040675d1aSAlfred Perlstein #define	UVISOROFRAMES	       32	/* units */
10102ac6454SAndrew Thompson 
10202ac6454SAndrew Thompson /* From the Linux driver */
10302ac6454SAndrew Thompson /*
10402ac6454SAndrew Thompson  * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
10520733245SPedro F. Giffuni  * are available to be transferred to the host for the specified endpoint.
10602ac6454SAndrew Thompson  * Currently this is not used, and always returns 0x0001
10702ac6454SAndrew Thompson  */
10802ac6454SAndrew Thompson #define	UVISOR_REQUEST_BYTES_AVAILABLE		0x01
10902ac6454SAndrew Thompson 
11002ac6454SAndrew Thompson /*
11102ac6454SAndrew Thompson  * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
11202ac6454SAndrew Thompson  * is now closing the pipe. An empty packet is sent in response.
11302ac6454SAndrew Thompson  */
11402ac6454SAndrew Thompson #define	UVISOR_CLOSE_NOTIFICATION		0x02
11502ac6454SAndrew Thompson 
11602ac6454SAndrew Thompson /*
11702ac6454SAndrew Thompson  * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
11802ac6454SAndrew Thompson  * get the endpoints used by the connection.
11902ac6454SAndrew Thompson  */
12002ac6454SAndrew Thompson #define	UVISOR_GET_CONNECTION_INFORMATION	0x03
12102ac6454SAndrew Thompson 
12202ac6454SAndrew Thompson /*
12302ac6454SAndrew Thompson  * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
12402ac6454SAndrew Thompson  */
12502ac6454SAndrew Thompson #define	UVISOR_MAX_CONN 8
12602ac6454SAndrew Thompson struct uvisor_connection_info {
12702ac6454SAndrew Thompson 	uWord	num_ports;
12802ac6454SAndrew Thompson 	struct {
12902ac6454SAndrew Thompson 		uByte	port_function_id;
13002ac6454SAndrew Thompson 		uByte	port;
13102ac6454SAndrew Thompson 	} __packed connections[UVISOR_MAX_CONN];
13202ac6454SAndrew Thompson } __packed;
13302ac6454SAndrew Thompson 
13402ac6454SAndrew Thompson #define	UVISOR_CONNECTION_INFO_SIZE 18
13502ac6454SAndrew Thompson 
13602ac6454SAndrew Thompson /* struct uvisor_connection_info.connection[x].port defines: */
13702ac6454SAndrew Thompson #define	UVISOR_ENDPOINT_1		0x01
13802ac6454SAndrew Thompson #define	UVISOR_ENDPOINT_2		0x02
13902ac6454SAndrew Thompson 
14002ac6454SAndrew Thompson /* struct uvisor_connection_info.connection[x].port_function_id defines: */
14102ac6454SAndrew Thompson #define	UVISOR_FUNCTION_GENERIC		0x00
14202ac6454SAndrew Thompson #define	UVISOR_FUNCTION_DEBUGGER	0x01
14302ac6454SAndrew Thompson #define	UVISOR_FUNCTION_HOTSYNC		0x02
14402ac6454SAndrew Thompson #define	UVISOR_FUNCTION_CONSOLE		0x03
14502ac6454SAndrew Thompson #define	UVISOR_FUNCTION_REMOTE_FILE_SYS	0x04
14602ac6454SAndrew Thompson 
14702ac6454SAndrew Thompson /*
14802ac6454SAndrew Thompson  * Unknown PalmOS stuff.
14902ac6454SAndrew Thompson  */
15002ac6454SAndrew Thompson #define	UVISOR_GET_PALM_INFORMATION		0x04
15102ac6454SAndrew Thompson #define	UVISOR_GET_PALM_INFORMATION_LEN		0x44
15202ac6454SAndrew Thompson 
15302ac6454SAndrew Thompson struct uvisor_palm_connection_info {
15402ac6454SAndrew Thompson 	uByte	num_ports;
15502ac6454SAndrew Thompson 	uByte	endpoint_numbers_different;
15602ac6454SAndrew Thompson 	uWord	reserved1;
15702ac6454SAndrew Thompson 	struct {
15802ac6454SAndrew Thompson 		uDWord	port_function_id;
15902ac6454SAndrew Thompson 		uByte	port;
16002ac6454SAndrew Thompson 		uByte	end_point_info;
16102ac6454SAndrew Thompson 		uWord	reserved;
16202ac6454SAndrew Thompson 	} __packed connections[UVISOR_MAX_CONN];
16302ac6454SAndrew Thompson } __packed;
16402ac6454SAndrew Thompson 
16502ac6454SAndrew Thompson enum {
16602ac6454SAndrew Thompson 	UVISOR_BULK_DT_WR,
16702ac6454SAndrew Thompson 	UVISOR_BULK_DT_RD,
16802ac6454SAndrew Thompson 	UVISOR_N_TRANSFER,
16902ac6454SAndrew Thompson };
17002ac6454SAndrew Thompson 
17102ac6454SAndrew Thompson struct uvisor_softc {
172760bc48eSAndrew Thompson 	struct ucom_super_softc sc_super_ucom;
173760bc48eSAndrew Thompson 	struct ucom_softc sc_ucom;
17402ac6454SAndrew Thompson 
175760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UVISOR_N_TRANSFER];
176760bc48eSAndrew Thompson 	struct usb_device *sc_udev;
177deefe583SAndrew Thompson 	struct mtx sc_mtx;
17802ac6454SAndrew Thompson 
17902ac6454SAndrew Thompson 	uint16_t sc_flag;
18002ac6454SAndrew Thompson #define	UVISOR_FLAG_PALM4       0x0001
18102ac6454SAndrew Thompson #define	UVISOR_FLAG_VISOR       0x0002
18202ac6454SAndrew Thompson #define	UVISOR_FLAG_PALM35      0x0004
18302ac6454SAndrew Thompson #define	UVISOR_FLAG_SEND_NOTIFY 0x0008
18402ac6454SAndrew Thompson 
18502ac6454SAndrew Thompson 	uint8_t	sc_iface_no;
18602ac6454SAndrew Thompson 	uint8_t	sc_iface_index;
18702ac6454SAndrew Thompson };
18802ac6454SAndrew Thompson 
18902ac6454SAndrew Thompson /* prototypes */
19002ac6454SAndrew Thompson 
19102ac6454SAndrew Thompson static device_probe_t uvisor_probe;
19202ac6454SAndrew Thompson static device_attach_t uvisor_attach;
19302ac6454SAndrew Thompson static device_detach_t uvisor_detach;
194c01fc06eSHans Petter Selasky static void uvisor_free_softc(struct uvisor_softc *);
19502ac6454SAndrew Thompson 
196e0a69b51SAndrew Thompson static usb_callback_t uvisor_write_callback;
197e0a69b51SAndrew Thompson static usb_callback_t uvisor_read_callback;
19802ac6454SAndrew Thompson 
199e0a69b51SAndrew Thompson static usb_error_t uvisor_init(struct uvisor_softc *, struct usb_device *,
200760bc48eSAndrew Thompson 		    struct usb_config *);
2015805d178SHans Petter Selasky static void	uvisor_free(struct ucom_softc *);
202760bc48eSAndrew Thompson static void	uvisor_cfg_open(struct ucom_softc *);
203760bc48eSAndrew Thompson static void	uvisor_cfg_close(struct ucom_softc *);
204760bc48eSAndrew Thompson static void	uvisor_start_read(struct ucom_softc *);
205760bc48eSAndrew Thompson static void	uvisor_stop_read(struct ucom_softc *);
206760bc48eSAndrew Thompson static void	uvisor_start_write(struct ucom_softc *);
207760bc48eSAndrew Thompson static void	uvisor_stop_write(struct ucom_softc *);
20802ac6454SAndrew Thompson 
209760bc48eSAndrew Thompson static const struct usb_config uvisor_config[UVISOR_N_TRANSFER] = {
21002ac6454SAndrew Thompson 	[UVISOR_BULK_DT_WR] = {
21102ac6454SAndrew Thompson 		.type = UE_BULK,
21202ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
21302ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
21440675d1aSAlfred Perlstein 		.bufsize = UVISOROBUFSIZE * UVISOROFRAMES,
21540675d1aSAlfred Perlstein 		.frames = UVISOROFRAMES,
2164eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
2174eae601eSAndrew Thompson 		.callback = &uvisor_write_callback,
21802ac6454SAndrew Thompson 	},
21902ac6454SAndrew Thompson 
22002ac6454SAndrew Thompson 	[UVISOR_BULK_DT_RD] = {
22102ac6454SAndrew Thompson 		.type = UE_BULK,
22202ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
22302ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
22440675d1aSAlfred Perlstein 		.bufsize = UVISORIBUFSIZE,
2254eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2264eae601eSAndrew Thompson 		.callback = &uvisor_read_callback,
22702ac6454SAndrew Thompson 	},
22802ac6454SAndrew Thompson };
22902ac6454SAndrew Thompson 
230760bc48eSAndrew Thompson static const struct ucom_callback uvisor_callback = {
231a593f6b8SAndrew Thompson 	.ucom_cfg_open = &uvisor_cfg_open,
232a593f6b8SAndrew Thompson 	.ucom_cfg_close = &uvisor_cfg_close,
233a593f6b8SAndrew Thompson 	.ucom_start_read = &uvisor_start_read,
234a593f6b8SAndrew Thompson 	.ucom_stop_read = &uvisor_stop_read,
235a593f6b8SAndrew Thompson 	.ucom_start_write = &uvisor_start_write,
236a593f6b8SAndrew Thompson 	.ucom_stop_write = &uvisor_stop_write,
2375805d178SHans Petter Selasky 	.ucom_free = &uvisor_free,
23802ac6454SAndrew Thompson };
23902ac6454SAndrew Thompson 
24002ac6454SAndrew Thompson static device_method_t uvisor_methods[] = {
24102ac6454SAndrew Thompson 	DEVMETHOD(device_probe, uvisor_probe),
24202ac6454SAndrew Thompson 	DEVMETHOD(device_attach, uvisor_attach),
24302ac6454SAndrew Thompson 	DEVMETHOD(device_detach, uvisor_detach),
2445805d178SHans Petter Selasky 	DEVMETHOD_END
24502ac6454SAndrew Thompson };
24602ac6454SAndrew Thompson 
24702ac6454SAndrew Thompson static driver_t uvisor_driver = {
24802ac6454SAndrew Thompson 	.name = "uvisor",
24902ac6454SAndrew Thompson 	.methods = uvisor_methods,
25002ac6454SAndrew Thompson 	.size = sizeof(struct uvisor_softc),
25102ac6454SAndrew Thompson };
25202ac6454SAndrew Thompson 
253f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID uvisor_devs[] = {
2549e6b5313SAndrew Thompson #define	UVISOR_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
2559e6b5313SAndrew Thompson 	UVISOR_DEV(ACEECA, MEZ1000, UVISOR_FLAG_PALM4),
2563e4af777SAndrew Thompson 	UVISOR_DEV(ALPHASMART, DANA_SYNC, UVISOR_FLAG_PALM4),
2579e6b5313SAndrew Thompson 	UVISOR_DEV(GARMIN, IQUE_3600, UVISOR_FLAG_PALM4),
2589e6b5313SAndrew Thompson 	UVISOR_DEV(FOSSIL, WRISTPDA, UVISOR_FLAG_PALM4),
2599e6b5313SAndrew Thompson 	UVISOR_DEV(HANDSPRING, VISOR, UVISOR_FLAG_VISOR),
2609e6b5313SAndrew Thompson 	UVISOR_DEV(HANDSPRING, TREO, UVISOR_FLAG_PALM4),
2619e6b5313SAndrew Thompson 	UVISOR_DEV(HANDSPRING, TREO600, UVISOR_FLAG_PALM4),
2629e6b5313SAndrew Thompson 	UVISOR_DEV(PALM, M500, UVISOR_FLAG_PALM4),
2639e6b5313SAndrew Thompson 	UVISOR_DEV(PALM, M505, UVISOR_FLAG_PALM4),
2649e6b5313SAndrew Thompson 	UVISOR_DEV(PALM, M515, UVISOR_FLAG_PALM4),
2659e6b5313SAndrew Thompson 	UVISOR_DEV(PALM, I705, UVISOR_FLAG_PALM4),
2669e6b5313SAndrew Thompson 	UVISOR_DEV(PALM, M125, UVISOR_FLAG_PALM4),
2679e6b5313SAndrew Thompson 	UVISOR_DEV(PALM, M130, UVISOR_FLAG_PALM4),
2689e6b5313SAndrew Thompson 	UVISOR_DEV(PALM, TUNGSTEN_Z, UVISOR_FLAG_PALM4),
2699e6b5313SAndrew Thompson 	UVISOR_DEV(PALM, TUNGSTEN_T, UVISOR_FLAG_PALM4),
2709e6b5313SAndrew Thompson 	UVISOR_DEV(PALM, ZIRE, UVISOR_FLAG_PALM4),
2719e6b5313SAndrew Thompson 	UVISOR_DEV(PALM, ZIRE31, UVISOR_FLAG_PALM4),
2729e6b5313SAndrew Thompson 	UVISOR_DEV(SAMSUNG, I500, UVISOR_FLAG_PALM4),
2739e6b5313SAndrew Thompson 	UVISOR_DEV(SONY, CLIE_40, 0),
2749e6b5313SAndrew Thompson 	UVISOR_DEV(SONY, CLIE_41, 0),
2759e6b5313SAndrew Thompson 	UVISOR_DEV(SONY, CLIE_S360, UVISOR_FLAG_PALM4),
2769e6b5313SAndrew Thompson 	UVISOR_DEV(SONY, CLIE_NX60, UVISOR_FLAG_PALM4),
2779e6b5313SAndrew Thompson 	UVISOR_DEV(SONY, CLIE_35, UVISOR_FLAG_PALM35),
2789e6b5313SAndrew Thompson /*  UVISOR_DEV(SONY, CLIE_25, UVISOR_FLAG_PALM4 ), */
2799e6b5313SAndrew Thompson 	UVISOR_DEV(SONY, CLIE_TJ37, UVISOR_FLAG_PALM4),
2809e6b5313SAndrew Thompson /*  UVISOR_DEV(SONY, CLIE_TH55, UVISOR_FLAG_PALM4 ), See PR 80935 */
2819e6b5313SAndrew Thompson 	UVISOR_DEV(TAPWAVE, ZODIAC, UVISOR_FLAG_PALM4),
2829e6b5313SAndrew Thompson #undef UVISOR_DEV
28302ac6454SAndrew Thompson };
28402ac6454SAndrew Thompson 
285bc9372d7SJohn Baldwin DRIVER_MODULE(uvisor, uhub, uvisor_driver, NULL, NULL);
286f809f280SWarner Losh MODULE_DEPEND(uvisor, ucom, 1, 1, 1);
287f809f280SWarner Losh MODULE_DEPEND(uvisor, usb, 1, 1, 1);
288f809f280SWarner Losh MODULE_VERSION(uvisor, 1);
289f809f280SWarner Losh USB_PNP_HOST_INFO(uvisor_devs);
290f809f280SWarner Losh 
29102ac6454SAndrew Thompson static int
uvisor_probe(device_t dev)29202ac6454SAndrew Thompson uvisor_probe(device_t dev)
29302ac6454SAndrew Thompson {
294760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
29502ac6454SAndrew Thompson 
296f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
29702ac6454SAndrew Thompson 		return (ENXIO);
29802ac6454SAndrew Thompson 	}
29902ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != UVISOR_CONFIG_INDEX) {
30002ac6454SAndrew Thompson 		return (ENXIO);
30102ac6454SAndrew Thompson 	}
30202ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != UVISOR_IFACE_INDEX) {
30302ac6454SAndrew Thompson 		return (ENXIO);
30402ac6454SAndrew Thompson 	}
305a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(uvisor_devs, sizeof(uvisor_devs), uaa));
30602ac6454SAndrew Thompson }
30702ac6454SAndrew Thompson 
30802ac6454SAndrew Thompson static int
uvisor_attach(device_t dev)30902ac6454SAndrew Thompson uvisor_attach(device_t dev)
31002ac6454SAndrew Thompson {
311760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
31202ac6454SAndrew Thompson 	struct uvisor_softc *sc = device_get_softc(dev);
313760bc48eSAndrew Thompson 	struct usb_config uvisor_config_copy[UVISOR_N_TRANSFER];
31402ac6454SAndrew Thompson 	int error;
31502ac6454SAndrew Thompson 
31602ac6454SAndrew Thompson 	DPRINTF("sc=%p\n", sc);
317271ae033SHans Petter Selasky 	memcpy(uvisor_config_copy, uvisor_config,
31802ac6454SAndrew Thompson 	    sizeof(uvisor_config_copy));
319271ae033SHans Petter Selasky 
320a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
32102ac6454SAndrew Thompson 
322deefe583SAndrew Thompson 	mtx_init(&sc->sc_mtx, "uvisor", NULL, MTX_DEF);
3235805d178SHans Petter Selasky 	ucom_ref(&sc->sc_super_ucom);
324deefe583SAndrew Thompson 
32502ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
32602ac6454SAndrew Thompson 
32702ac6454SAndrew Thompson 	/* configure the device */
32802ac6454SAndrew Thompson 
32902ac6454SAndrew Thompson 	sc->sc_flag = USB_GET_DRIVER_INFO(uaa);
33002ac6454SAndrew Thompson 	sc->sc_iface_no = uaa->info.bIfaceNum;
33102ac6454SAndrew Thompson 	sc->sc_iface_index = UVISOR_IFACE_INDEX;
33202ac6454SAndrew Thompson 
33302ac6454SAndrew Thompson 	error = uvisor_init(sc, uaa->device, uvisor_config_copy);
33402ac6454SAndrew Thompson 
33502ac6454SAndrew Thompson 	if (error) {
33602ac6454SAndrew Thompson 		DPRINTF("init failed, error=%s\n",
337a593f6b8SAndrew Thompson 		    usbd_errstr(error));
33802ac6454SAndrew Thompson 		goto detach;
33902ac6454SAndrew Thompson 	}
340a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
34102ac6454SAndrew Thompson 	    sc->sc_xfer, uvisor_config_copy, UVISOR_N_TRANSFER,
342deefe583SAndrew Thompson 	    sc, &sc->sc_mtx);
34302ac6454SAndrew Thompson 	if (error) {
34402ac6454SAndrew Thompson 		DPRINTF("could not allocate all pipes\n");
34502ac6454SAndrew Thompson 		goto detach;
34602ac6454SAndrew Thompson 	}
34702ac6454SAndrew Thompson 
348a593f6b8SAndrew Thompson 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
349deefe583SAndrew Thompson 	    &uvisor_callback, &sc->sc_mtx);
35002ac6454SAndrew Thompson 	if (error) {
351a593f6b8SAndrew Thompson 		DPRINTF("ucom_attach failed\n");
35202ac6454SAndrew Thompson 		goto detach;
35302ac6454SAndrew Thompson 	}
3546416c259SNick Hibma 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
3556416c259SNick Hibma 
35602ac6454SAndrew Thompson 	return (0);
35702ac6454SAndrew Thompson 
35802ac6454SAndrew Thompson detach:
35902ac6454SAndrew Thompson 	uvisor_detach(dev);
36002ac6454SAndrew Thompson 	return (ENXIO);
36102ac6454SAndrew Thompson }
36202ac6454SAndrew Thompson 
36302ac6454SAndrew Thompson static int
uvisor_detach(device_t dev)36402ac6454SAndrew Thompson uvisor_detach(device_t dev)
36502ac6454SAndrew Thompson {
36602ac6454SAndrew Thompson 	struct uvisor_softc *sc = device_get_softc(dev);
36702ac6454SAndrew Thompson 
36802ac6454SAndrew Thompson 	DPRINTF("sc=%p\n", sc);
36902ac6454SAndrew Thompson 
370015bb88fSNick Hibma 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
371a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, UVISOR_N_TRANSFER);
37202ac6454SAndrew Thompson 
373c01fc06eSHans Petter Selasky 	device_claim_softc(dev);
374c01fc06eSHans Petter Selasky 
375c01fc06eSHans Petter Selasky 	uvisor_free_softc(sc);
376c01fc06eSHans Petter Selasky 
37702ac6454SAndrew Thompson 	return (0);
37802ac6454SAndrew Thompson }
37902ac6454SAndrew Thompson 
3805805d178SHans Petter Selasky UCOM_UNLOAD_DRAIN(uvisor);
3815805d178SHans Petter Selasky 
3825805d178SHans Petter Selasky static void
uvisor_free_softc(struct uvisor_softc * sc)383c01fc06eSHans Petter Selasky uvisor_free_softc(struct uvisor_softc *sc)
3845805d178SHans Petter Selasky {
3855805d178SHans Petter Selasky 	if (ucom_unref(&sc->sc_super_ucom)) {
3865805d178SHans Petter Selasky 		mtx_destroy(&sc->sc_mtx);
387c01fc06eSHans Petter Selasky 		device_free_softc(sc);
3885805d178SHans Petter Selasky 	}
3895805d178SHans Petter Selasky }
3905805d178SHans Petter Selasky 
3915805d178SHans Petter Selasky static void
uvisor_free(struct ucom_softc * ucom)3925805d178SHans Petter Selasky uvisor_free(struct ucom_softc *ucom)
3935805d178SHans Petter Selasky {
394c01fc06eSHans Petter Selasky 	uvisor_free_softc(ucom->sc_parent);
3955805d178SHans Petter Selasky }
3965805d178SHans Petter Selasky 
397e0a69b51SAndrew Thompson static usb_error_t
uvisor_init(struct uvisor_softc * sc,struct usb_device * udev,struct usb_config * config)398760bc48eSAndrew Thompson uvisor_init(struct uvisor_softc *sc, struct usb_device *udev, struct usb_config *config)
39902ac6454SAndrew Thompson {
400e0a69b51SAndrew Thompson 	usb_error_t err = 0;
401760bc48eSAndrew Thompson 	struct usb_device_request req;
40202ac6454SAndrew Thompson 	struct uvisor_connection_info coninfo;
40302ac6454SAndrew Thompson 	struct uvisor_palm_connection_info pconinfo;
40402ac6454SAndrew Thompson 	uint16_t actlen;
40502ac6454SAndrew Thompson 	uint8_t buffer[256];
40602ac6454SAndrew Thompson 
40702ac6454SAndrew Thompson 	if (sc->sc_flag & UVISOR_FLAG_VISOR) {
40802ac6454SAndrew Thompson 		DPRINTF("getting connection info\n");
40902ac6454SAndrew Thompson 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
41002ac6454SAndrew Thompson 		req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
41102ac6454SAndrew Thompson 		USETW(req.wValue, 0);
41202ac6454SAndrew Thompson 		USETW(req.wIndex, 0);
41302ac6454SAndrew Thompson 		USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
414a593f6b8SAndrew Thompson 		err = usbd_do_request_flags(udev, NULL,
415296ade60SAndrew Thompson 		    &req, &coninfo, USB_SHORT_XFER_OK,
41602ac6454SAndrew Thompson 		    &actlen, USB_DEFAULT_TIMEOUT);
41702ac6454SAndrew Thompson 
41802ac6454SAndrew Thompson 		if (err) {
41902ac6454SAndrew Thompson 			goto done;
42002ac6454SAndrew Thompson 		}
42102ac6454SAndrew Thompson 	}
422b850ecc1SAndrew Thompson #ifdef USB_DEBUG
42302ac6454SAndrew Thompson 	if (sc->sc_flag & UVISOR_FLAG_VISOR) {
42402ac6454SAndrew Thompson 		uint16_t i, np;
42502ac6454SAndrew Thompson 		const char *desc;
42602ac6454SAndrew Thompson 
42702ac6454SAndrew Thompson 		np = UGETW(coninfo.num_ports);
42802ac6454SAndrew Thompson 		if (np > UVISOR_MAX_CONN) {
42902ac6454SAndrew Thompson 			np = UVISOR_MAX_CONN;
43002ac6454SAndrew Thompson 		}
43102ac6454SAndrew Thompson 		DPRINTF("Number of ports: %d\n", np);
43202ac6454SAndrew Thompson 
43302ac6454SAndrew Thompson 		for (i = 0; i < np; ++i) {
43402ac6454SAndrew Thompson 			switch (coninfo.connections[i].port_function_id) {
43502ac6454SAndrew Thompson 			case UVISOR_FUNCTION_GENERIC:
43602ac6454SAndrew Thompson 				desc = "Generic";
43702ac6454SAndrew Thompson 				break;
43802ac6454SAndrew Thompson 			case UVISOR_FUNCTION_DEBUGGER:
43902ac6454SAndrew Thompson 				desc = "Debugger";
44002ac6454SAndrew Thompson 				break;
44102ac6454SAndrew Thompson 			case UVISOR_FUNCTION_HOTSYNC:
44202ac6454SAndrew Thompson 				desc = "HotSync";
44302ac6454SAndrew Thompson 				break;
44402ac6454SAndrew Thompson 			case UVISOR_FUNCTION_REMOTE_FILE_SYS:
44502ac6454SAndrew Thompson 				desc = "Remote File System";
44602ac6454SAndrew Thompson 				break;
44702ac6454SAndrew Thompson 			default:
44802ac6454SAndrew Thompson 				desc = "unknown";
44902ac6454SAndrew Thompson 				break;
45002ac6454SAndrew Thompson 			}
45102ac6454SAndrew Thompson 			DPRINTF("Port %d is for %s\n",
45202ac6454SAndrew Thompson 			    coninfo.connections[i].port, desc);
45302ac6454SAndrew Thompson 		}
45402ac6454SAndrew Thompson 	}
45502ac6454SAndrew Thompson #endif
45602ac6454SAndrew Thompson 
45702ac6454SAndrew Thompson 	if (sc->sc_flag & UVISOR_FLAG_PALM4) {
45802ac6454SAndrew Thompson 		uint8_t port;
45902ac6454SAndrew Thompson 
46002ac6454SAndrew Thompson 		/* Palm OS 4.0 Hack */
46102ac6454SAndrew Thompson 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
46202ac6454SAndrew Thompson 		req.bRequest = UVISOR_GET_PALM_INFORMATION;
46302ac6454SAndrew Thompson 		USETW(req.wValue, 0);
46402ac6454SAndrew Thompson 		USETW(req.wIndex, 0);
46502ac6454SAndrew Thompson 		USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
46602ac6454SAndrew Thompson 
467a593f6b8SAndrew Thompson 		err = usbd_do_request_flags
468296ade60SAndrew Thompson 		    (udev, NULL, &req, &pconinfo, USB_SHORT_XFER_OK,
46902ac6454SAndrew Thompson 		    &actlen, USB_DEFAULT_TIMEOUT);
47002ac6454SAndrew Thompson 
47102ac6454SAndrew Thompson 		if (err) {
47202ac6454SAndrew Thompson 			goto done;
47302ac6454SAndrew Thompson 		}
47402ac6454SAndrew Thompson 		if (actlen < 12) {
47502ac6454SAndrew Thompson 			DPRINTF("too little data\n");
47602ac6454SAndrew Thompson 			err = USB_ERR_INVAL;
47702ac6454SAndrew Thompson 			goto done;
47802ac6454SAndrew Thompson 		}
47902ac6454SAndrew Thompson 		if (pconinfo.endpoint_numbers_different) {
48002ac6454SAndrew Thompson 			port = pconinfo.connections[0].end_point_info;
48102ac6454SAndrew Thompson 			config[0].endpoint = (port & 0xF);	/* output */
48202ac6454SAndrew Thompson 			config[1].endpoint = (port >> 4);	/* input */
48302ac6454SAndrew Thompson 		} else {
48402ac6454SAndrew Thompson 			port = pconinfo.connections[0].port;
48502ac6454SAndrew Thompson 			config[0].endpoint = (port & 0xF);	/* output */
48602ac6454SAndrew Thompson 			config[1].endpoint = (port & 0xF);	/* input */
48702ac6454SAndrew Thompson 		}
48802ac6454SAndrew Thompson #if 0
48902ac6454SAndrew Thompson 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
49002ac6454SAndrew Thompson 		req.bRequest = UVISOR_GET_PALM_INFORMATION;
49102ac6454SAndrew Thompson 		USETW(req.wValue, 0);
49202ac6454SAndrew Thompson 		USETW(req.wIndex, 0);
49302ac6454SAndrew Thompson 		USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
494a593f6b8SAndrew Thompson 		err = usbd_do_request(udev, &req, buffer);
49502ac6454SAndrew Thompson 		if (err) {
49602ac6454SAndrew Thompson 			goto done;
49702ac6454SAndrew Thompson 		}
49802ac6454SAndrew Thompson #endif
49902ac6454SAndrew Thompson 	}
50002ac6454SAndrew Thompson 	if (sc->sc_flag & UVISOR_FLAG_PALM35) {
50102ac6454SAndrew Thompson 		/* get the config number */
50202ac6454SAndrew Thompson 		DPRINTF("getting config info\n");
50302ac6454SAndrew Thompson 		req.bmRequestType = UT_READ;
50402ac6454SAndrew Thompson 		req.bRequest = UR_GET_CONFIG;
50502ac6454SAndrew Thompson 		USETW(req.wValue, 0);
50602ac6454SAndrew Thompson 		USETW(req.wIndex, 0);
50702ac6454SAndrew Thompson 		USETW(req.wLength, 1);
50802ac6454SAndrew Thompson 
509a593f6b8SAndrew Thompson 		err = usbd_do_request(udev, NULL, &req, buffer);
51002ac6454SAndrew Thompson 		if (err) {
51102ac6454SAndrew Thompson 			goto done;
51202ac6454SAndrew Thompson 		}
51302ac6454SAndrew Thompson 		/* get the interface number */
51402ac6454SAndrew Thompson 		DPRINTF("get the interface number\n");
51502ac6454SAndrew Thompson 		req.bmRequestType = UT_READ_DEVICE;
51602ac6454SAndrew Thompson 		req.bRequest = UR_GET_INTERFACE;
51702ac6454SAndrew Thompson 		USETW(req.wValue, 0);
51802ac6454SAndrew Thompson 		USETW(req.wIndex, 0);
51902ac6454SAndrew Thompson 		USETW(req.wLength, 1);
520a593f6b8SAndrew Thompson 		err = usbd_do_request(udev, NULL, &req, buffer);
52102ac6454SAndrew Thompson 		if (err) {
52202ac6454SAndrew Thompson 			goto done;
52302ac6454SAndrew Thompson 		}
52402ac6454SAndrew Thompson 	}
52540675d1aSAlfred Perlstein #if 0
52640675d1aSAlfred Perlstein 	uWord wAvail;
52740675d1aSAlfred Perlstein 
52802ac6454SAndrew Thompson 	DPRINTF("getting available bytes\n");
52902ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
53002ac6454SAndrew Thompson 	req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
53102ac6454SAndrew Thompson 	USETW(req.wValue, 0);
53202ac6454SAndrew Thompson 	USETW(req.wIndex, 5);
53302ac6454SAndrew Thompson 	USETW(req.wLength, sizeof(wAvail));
534a593f6b8SAndrew Thompson 	err = usbd_do_request(udev, NULL, &req, &wAvail);
53502ac6454SAndrew Thompson 	if (err) {
53602ac6454SAndrew Thompson 		goto done;
53702ac6454SAndrew Thompson 	}
53802ac6454SAndrew Thompson 	DPRINTF("avail=%d\n", UGETW(wAvail));
53940675d1aSAlfred Perlstein #endif
54002ac6454SAndrew Thompson 
54102ac6454SAndrew Thompson 	DPRINTF("done\n");
54202ac6454SAndrew Thompson done:
54302ac6454SAndrew Thompson 	return (err);
54402ac6454SAndrew Thompson }
54502ac6454SAndrew Thompson 
54602ac6454SAndrew Thompson static void
uvisor_cfg_open(struct ucom_softc * ucom)547760bc48eSAndrew Thompson uvisor_cfg_open(struct ucom_softc *ucom)
54802ac6454SAndrew Thompson {
54902ac6454SAndrew Thompson 	return;
55002ac6454SAndrew Thompson }
55102ac6454SAndrew Thompson 
55202ac6454SAndrew Thompson static void
uvisor_cfg_close(struct ucom_softc * ucom)553760bc48eSAndrew Thompson uvisor_cfg_close(struct ucom_softc *ucom)
55402ac6454SAndrew Thompson {
55502ac6454SAndrew Thompson 	struct uvisor_softc *sc = ucom->sc_parent;
55602ac6454SAndrew Thompson 	uint8_t buffer[UVISOR_CONNECTION_INFO_SIZE];
557760bc48eSAndrew Thompson 	struct usb_device_request req;
558e0a69b51SAndrew Thompson 	usb_error_t err;
55902ac6454SAndrew Thompson 
56002ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;	/* XXX read? */
56102ac6454SAndrew Thompson 	req.bRequest = UVISOR_CLOSE_NOTIFICATION;
56202ac6454SAndrew Thompson 	USETW(req.wValue, 0);
56302ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
56402ac6454SAndrew Thompson 	USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
56502ac6454SAndrew Thompson 
566a593f6b8SAndrew Thompson 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
56702ac6454SAndrew Thompson 	    &req, buffer, 0, 1000);
56802ac6454SAndrew Thompson 	if (err) {
56902ac6454SAndrew Thompson 		DPRINTFN(0, "close notification failed, error=%s\n",
570a593f6b8SAndrew Thompson 		    usbd_errstr(err));
57102ac6454SAndrew Thompson 	}
57202ac6454SAndrew Thompson }
57302ac6454SAndrew Thompson 
57402ac6454SAndrew Thompson static void
uvisor_start_read(struct ucom_softc * ucom)575760bc48eSAndrew Thompson uvisor_start_read(struct ucom_softc *ucom)
57602ac6454SAndrew Thompson {
57702ac6454SAndrew Thompson 	struct uvisor_softc *sc = ucom->sc_parent;
57802ac6454SAndrew Thompson 
579a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UVISOR_BULK_DT_RD]);
58002ac6454SAndrew Thompson }
58102ac6454SAndrew Thompson 
58202ac6454SAndrew Thompson static void
uvisor_stop_read(struct ucom_softc * ucom)583760bc48eSAndrew Thompson uvisor_stop_read(struct ucom_softc *ucom)
58402ac6454SAndrew Thompson {
58502ac6454SAndrew Thompson 	struct uvisor_softc *sc = ucom->sc_parent;
58602ac6454SAndrew Thompson 
587a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UVISOR_BULK_DT_RD]);
58802ac6454SAndrew Thompson }
58902ac6454SAndrew Thompson 
59002ac6454SAndrew Thompson static void
uvisor_start_write(struct ucom_softc * ucom)591760bc48eSAndrew Thompson uvisor_start_write(struct ucom_softc *ucom)
59202ac6454SAndrew Thompson {
59302ac6454SAndrew Thompson 	struct uvisor_softc *sc = ucom->sc_parent;
59402ac6454SAndrew Thompson 
595a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UVISOR_BULK_DT_WR]);
59602ac6454SAndrew Thompson }
59702ac6454SAndrew Thompson 
59802ac6454SAndrew Thompson static void
uvisor_stop_write(struct ucom_softc * ucom)599760bc48eSAndrew Thompson uvisor_stop_write(struct ucom_softc *ucom)
60002ac6454SAndrew Thompson {
60102ac6454SAndrew Thompson 	struct uvisor_softc *sc = ucom->sc_parent;
60202ac6454SAndrew Thompson 
603a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UVISOR_BULK_DT_WR]);
60402ac6454SAndrew Thompson }
60502ac6454SAndrew Thompson 
60602ac6454SAndrew Thompson static void
uvisor_write_callback(struct usb_xfer * xfer,usb_error_t error)607ed6d949aSAndrew Thompson uvisor_write_callback(struct usb_xfer *xfer, usb_error_t error)
60802ac6454SAndrew Thompson {
609ed6d949aSAndrew Thompson 	struct uvisor_softc *sc = usbd_xfer_softc(xfer);
610ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
61102ac6454SAndrew Thompson 	uint32_t actlen;
61240675d1aSAlfred Perlstein 	uint8_t x;
61302ac6454SAndrew Thompson 
61402ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
61502ac6454SAndrew Thompson 	case USB_ST_SETUP:
61602ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
61702ac6454SAndrew Thompson tr_setup:
61840675d1aSAlfred Perlstein 		for (x = 0; x != UVISOROFRAMES; x++) {
61940675d1aSAlfred Perlstein 			usbd_xfer_set_frame_offset(xfer,
62040675d1aSAlfred Perlstein 			    x * UVISOROBUFSIZE, x);
62140675d1aSAlfred Perlstein 
62240675d1aSAlfred Perlstein 			pc = usbd_xfer_get_frame(xfer, x);
62340675d1aSAlfred Perlstein 			if (ucom_get_data(&sc->sc_ucom, pc, 0,
62440675d1aSAlfred Perlstein 			    UVISOROBUFSIZE, &actlen)) {
62540675d1aSAlfred Perlstein 				usbd_xfer_set_frame_len(xfer, x, actlen);
62640675d1aSAlfred Perlstein 			} else {
62740675d1aSAlfred Perlstein 				break;
62840675d1aSAlfred Perlstein 			}
62940675d1aSAlfred Perlstein 		}
63040675d1aSAlfred Perlstein 		/* check for data */
63140675d1aSAlfred Perlstein 		if (x != 0) {
63240675d1aSAlfred Perlstein 			usbd_xfer_set_frames(xfer, x);
633a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
63402ac6454SAndrew Thompson 		}
63540675d1aSAlfred Perlstein 		break;
63602ac6454SAndrew Thompson 
63702ac6454SAndrew Thompson 	default:			/* Error */
638ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
63902ac6454SAndrew Thompson 			/* try to clear stall first */
640ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
64102ac6454SAndrew Thompson 			goto tr_setup;
64202ac6454SAndrew Thompson 		}
64340675d1aSAlfred Perlstein 		break;
64402ac6454SAndrew Thompson 	}
64502ac6454SAndrew Thompson }
64602ac6454SAndrew Thompson 
64702ac6454SAndrew Thompson static void
uvisor_read_callback(struct usb_xfer * xfer,usb_error_t error)648ed6d949aSAndrew Thompson uvisor_read_callback(struct usb_xfer *xfer, usb_error_t error)
64902ac6454SAndrew Thompson {
650ed6d949aSAndrew Thompson 	struct uvisor_softc *sc = usbd_xfer_softc(xfer);
651ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
652ed6d949aSAndrew Thompson 	int actlen;
653ed6d949aSAndrew Thompson 
654ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
65502ac6454SAndrew Thompson 
65602ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
65702ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
658ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
659ed6d949aSAndrew Thompson 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
66002ac6454SAndrew Thompson 
66102ac6454SAndrew Thompson 	case USB_ST_SETUP:
66202ac6454SAndrew Thompson tr_setup:
663ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
664a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
66502ac6454SAndrew Thompson 		return;
66602ac6454SAndrew Thompson 
66702ac6454SAndrew Thompson 	default:			/* Error */
668ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
66902ac6454SAndrew Thompson 			/* try to clear stall first */
670ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
67102ac6454SAndrew Thompson 			goto tr_setup;
67202ac6454SAndrew Thompson 		}
67302ac6454SAndrew Thompson 		return;
67402ac6454SAndrew Thompson 	}
67502ac6454SAndrew Thompson }
676