xref: /freebsd/sys/dev/usb/serial/uvisor.c (revision 830940567b49bb0c08dfaed40418999e76616909)
1 /*	$NetBSD: uvisor.c,v 1.9 2001/01/23 14:04:14 augustss Exp $	*/
2 /*      $FreeBSD$ */
3 
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 $
14  */
15 
16 /*-
17  * Copyright (c) 2000 The NetBSD Foundation, Inc.
18  * All rights reserved.
19  *
20  * This code is derived from software contributed to The NetBSD Foundation
21  * by Lennart Augustsson (lennart@augustsson.net) at
22  * Carlstedt Research & Technology.
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions
26  * are met:
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.
39  *
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.
51  */
52 
53 /*
54  * Handspring Visor (Palmpilot compatible PDA) driver
55  */
56 
57 #include <sys/stdint.h>
58 #include <sys/stddef.h>
59 #include <sys/param.h>
60 #include <sys/queue.h>
61 #include <sys/types.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
64 #include <sys/bus.h>
65 #include <sys/linker_set.h>
66 #include <sys/module.h>
67 #include <sys/lock.h>
68 #include <sys/mutex.h>
69 #include <sys/condvar.h>
70 #include <sys/sysctl.h>
71 #include <sys/sx.h>
72 #include <sys/unistd.h>
73 #include <sys/callout.h>
74 #include <sys/malloc.h>
75 #include <sys/priv.h>
76 
77 #include <dev/usb/usb.h>
78 #include <dev/usb/usbdi.h>
79 #include <dev/usb/usbdi_util.h>
80 #include "usbdevs.h"
81 
82 #define	USB_DEBUG_VAR uvisor_debug
83 #include <dev/usb/usb_debug.h>
84 #include <dev/usb/usb_process.h>
85 
86 #include <dev/usb/serial/usb_serial.h>
87 
88 #if USB_DEBUG
89 static int uvisor_debug = 0;
90 
91 SYSCTL_NODE(_hw_usb, OID_AUTO, uvisor, CTLFLAG_RW, 0, "USB uvisor");
92 SYSCTL_INT(_hw_usb_uvisor, OID_AUTO, debug, CTLFLAG_RW,
93     &uvisor_debug, 0, "Debug level");
94 #endif
95 
96 #define	UVISOR_CONFIG_INDEX	0
97 #define	UVISOR_IFACE_INDEX	0
98 #define	UVISOR_BUFSIZE       1024	/* bytes */
99 
100 /* From the Linux driver */
101 /*
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
105  */
106 #define	UVISOR_REQUEST_BYTES_AVAILABLE		0x01
107 
108 /*
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.
111  */
112 #define	UVISOR_CLOSE_NOTIFICATION		0x02
113 
114 /*
115  * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
116  * get the endpoints used by the connection.
117  */
118 #define	UVISOR_GET_CONNECTION_INFORMATION	0x03
119 
120 /*
121  * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
122  */
123 #define	UVISOR_MAX_CONN 8
124 struct uvisor_connection_info {
125 	uWord	num_ports;
126 	struct {
127 		uByte	port_function_id;
128 		uByte	port;
129 	} __packed connections[UVISOR_MAX_CONN];
130 } __packed;
131 
132 #define	UVISOR_CONNECTION_INFO_SIZE 18
133 
134 /* struct uvisor_connection_info.connection[x].port defines: */
135 #define	UVISOR_ENDPOINT_1		0x01
136 #define	UVISOR_ENDPOINT_2		0x02
137 
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
144 
145 /*
146  * Unknown PalmOS stuff.
147  */
148 #define	UVISOR_GET_PALM_INFORMATION		0x04
149 #define	UVISOR_GET_PALM_INFORMATION_LEN		0x44
150 
151 struct uvisor_palm_connection_info {
152 	uByte	num_ports;
153 	uByte	endpoint_numbers_different;
154 	uWord	reserved1;
155 	struct {
156 		uDWord	port_function_id;
157 		uByte	port;
158 		uByte	end_point_info;
159 		uWord	reserved;
160 	} __packed connections[UVISOR_MAX_CONN];
161 } __packed;
162 
163 enum {
164 	UVISOR_BULK_DT_WR,
165 	UVISOR_BULK_DT_RD,
166 	UVISOR_N_TRANSFER,
167 };
168 
169 struct uvisor_softc {
170 	struct ucom_super_softc sc_super_ucom;
171 	struct ucom_softc sc_ucom;
172 
173 	struct usb_xfer *sc_xfer[UVISOR_N_TRANSFER];
174 	struct usb_device *sc_udev;
175 	struct mtx sc_mtx;
176 
177 	uint16_t sc_flag;
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
182 
183 	uint8_t	sc_iface_no;
184 	uint8_t	sc_iface_index;
185 };
186 
187 /* prototypes */
188 
189 static device_probe_t uvisor_probe;
190 static device_attach_t uvisor_attach;
191 static device_detach_t uvisor_detach;
192 
193 static usb_callback_t uvisor_write_callback;
194 static usb_callback_t uvisor_read_callback;
195 
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 *);
204 
205 static const struct usb_config uvisor_config[UVISOR_N_TRANSFER] = {
206 
207 	[UVISOR_BULK_DT_WR] = {
208 		.type = UE_BULK,
209 		.endpoint = UE_ADDR_ANY,
210 		.direction = UE_DIR_OUT,
211 		.bufsize = UVISOR_BUFSIZE,	/* bytes */
212 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
213 		.callback = &uvisor_write_callback,
214 	},
215 
216 	[UVISOR_BULK_DT_RD] = {
217 		.type = UE_BULK,
218 		.endpoint = UE_ADDR_ANY,
219 		.direction = UE_DIR_IN,
220 		.bufsize = UVISOR_BUFSIZE,	/* bytes */
221 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
222 		.callback = &uvisor_read_callback,
223 	},
224 };
225 
226 static const struct ucom_callback uvisor_callback = {
227 	.ucom_cfg_open = &uvisor_cfg_open,
228 	.ucom_cfg_close = &uvisor_cfg_close,
229 	.ucom_start_read = &uvisor_start_read,
230 	.ucom_stop_read = &uvisor_stop_read,
231 	.ucom_start_write = &uvisor_start_write,
232 	.ucom_stop_write = &uvisor_stop_write,
233 };
234 
235 static device_method_t uvisor_methods[] = {
236 	DEVMETHOD(device_probe, uvisor_probe),
237 	DEVMETHOD(device_attach, uvisor_attach),
238 	DEVMETHOD(device_detach, uvisor_detach),
239 	{0, 0}
240 };
241 
242 static devclass_t uvisor_devclass;
243 
244 static driver_t uvisor_driver = {
245 	.name = "uvisor",
246 	.methods = uvisor_methods,
247 	.size = sizeof(struct uvisor_softc),
248 };
249 
250 DRIVER_MODULE(uvisor, uhub, uvisor_driver, uvisor_devclass, NULL, 0);
251 MODULE_DEPEND(uvisor, ucom, 1, 1, 1);
252 MODULE_DEPEND(uvisor, usb, 1, 1, 1);
253 
254 static const struct usb_device_id uvisor_devs[] = {
255 	{USB_VPI(USB_VENDOR_ACEECA, USB_PRODUCT_ACEECA_MEZ1000, UVISOR_FLAG_PALM4)},
256 	{USB_VPI(USB_VENDOR_GARMIN, USB_PRODUCT_GARMIN_IQUE_3600, UVISOR_FLAG_PALM4)},
257 	{USB_VPI(USB_VENDOR_FOSSIL, USB_PRODUCT_FOSSIL_WRISTPDA, UVISOR_FLAG_PALM4)},
258 	{USB_VPI(USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_VISOR, UVISOR_FLAG_VISOR)},
259 	{USB_VPI(USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO, UVISOR_FLAG_PALM4)},
260 	{USB_VPI(USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO600, UVISOR_FLAG_PALM4)},
261 	{USB_VPI(USB_VENDOR_PALM, USB_PRODUCT_PALM_M500, UVISOR_FLAG_PALM4)},
262 	{USB_VPI(USB_VENDOR_PALM, USB_PRODUCT_PALM_M505, UVISOR_FLAG_PALM4)},
263 	{USB_VPI(USB_VENDOR_PALM, USB_PRODUCT_PALM_M515, UVISOR_FLAG_PALM4)},
264 	{USB_VPI(USB_VENDOR_PALM, USB_PRODUCT_PALM_I705, UVISOR_FLAG_PALM4)},
265 	{USB_VPI(USB_VENDOR_PALM, USB_PRODUCT_PALM_M125, UVISOR_FLAG_PALM4)},
266 	{USB_VPI(USB_VENDOR_PALM, USB_PRODUCT_PALM_M130, UVISOR_FLAG_PALM4)},
267 	{USB_VPI(USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_Z, UVISOR_FLAG_PALM4)},
268 	{USB_VPI(USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_T, UVISOR_FLAG_PALM4)},
269 	{USB_VPI(USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE, UVISOR_FLAG_PALM4)},
270 	{USB_VPI(USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE31, UVISOR_FLAG_PALM4)},
271 	{USB_VPI(USB_VENDOR_SAMSUNG, USB_PRODUCT_SAMSUNG_I500, UVISOR_FLAG_PALM4)},
272 	{USB_VPI(USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40, 0)},
273 	{USB_VPI(USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_41, UVISOR_FLAG_PALM4)},
274 	{USB_VPI(USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_S360, UVISOR_FLAG_PALM4)},
275 	{USB_VPI(USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_NX60, UVISOR_FLAG_PALM4)},
276 	{USB_VPI(USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_35, UVISOR_FLAG_PALM35)},
277 /*  {USB_VPI(USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25, UVISOR_FLAG_PALM4 )}, */
278 	{USB_VPI(USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_TJ37, UVISOR_FLAG_PALM4)},
279 /*  {USB_VPI(USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_TH55, UVISOR_FLAG_PALM4 )}, See PR 80935 */
280 	{USB_VPI(USB_VENDOR_TAPWAVE, USB_PRODUCT_TAPWAVE_ZODIAC, UVISOR_FLAG_PALM4)},
281 };
282 
283 static int
284 uvisor_probe(device_t dev)
285 {
286 	struct usb_attach_arg *uaa = device_get_ivars(dev);
287 
288 	if (uaa->usb_mode != USB_MODE_HOST) {
289 		return (ENXIO);
290 	}
291 	if (uaa->info.bConfigIndex != UVISOR_CONFIG_INDEX) {
292 		return (ENXIO);
293 	}
294 	if (uaa->info.bIfaceIndex != UVISOR_IFACE_INDEX) {
295 		return (ENXIO);
296 	}
297 	return (usbd_lookup_id_by_uaa(uvisor_devs, sizeof(uvisor_devs), uaa));
298 }
299 
300 static int
301 uvisor_attach(device_t dev)
302 {
303 	struct usb_attach_arg *uaa = device_get_ivars(dev);
304 	struct uvisor_softc *sc = device_get_softc(dev);
305 	struct usb_config uvisor_config_copy[UVISOR_N_TRANSFER];
306 	int error;
307 
308 	DPRINTF("sc=%p\n", sc);
309 	bcopy(uvisor_config, uvisor_config_copy,
310 	    sizeof(uvisor_config_copy));
311 	device_set_usb_desc(dev);
312 
313 	mtx_init(&sc->sc_mtx, "uvisor", NULL, MTX_DEF);
314 
315 	sc->sc_udev = uaa->device;
316 
317 	/* configure the device */
318 
319 	sc->sc_flag = USB_GET_DRIVER_INFO(uaa);
320 	sc->sc_iface_no = uaa->info.bIfaceNum;
321 	sc->sc_iface_index = UVISOR_IFACE_INDEX;
322 
323 	error = uvisor_init(sc, uaa->device, uvisor_config_copy);
324 
325 	if (error) {
326 		DPRINTF("init failed, error=%s\n",
327 		    usbd_errstr(error));
328 		goto detach;
329 	}
330 	error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
331 	    sc->sc_xfer, uvisor_config_copy, UVISOR_N_TRANSFER,
332 	    sc, &sc->sc_mtx);
333 	if (error) {
334 		DPRINTF("could not allocate all pipes\n");
335 		goto detach;
336 	}
337 	/* clear stall at first run */
338 	mtx_lock(&sc->sc_mtx);
339 	usbd_xfer_set_stall(sc->sc_xfer[UVISOR_BULK_DT_WR]);
340 	usbd_xfer_set_stall(sc->sc_xfer[UVISOR_BULK_DT_RD]);
341 	mtx_unlock(&sc->sc_mtx);
342 
343 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
344 	    &uvisor_callback, &sc->sc_mtx);
345 	if (error) {
346 		DPRINTF("ucom_attach failed\n");
347 		goto detach;
348 	}
349 	return (0);
350 
351 detach:
352 	uvisor_detach(dev);
353 	return (ENXIO);
354 }
355 
356 static int
357 uvisor_detach(device_t dev)
358 {
359 	struct uvisor_softc *sc = device_get_softc(dev);
360 
361 	DPRINTF("sc=%p\n", sc);
362 
363 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
364 	usbd_transfer_unsetup(sc->sc_xfer, UVISOR_N_TRANSFER);
365 	mtx_destroy(&sc->sc_mtx);
366 
367 	return (0);
368 }
369 
370 static usb_error_t
371 uvisor_init(struct uvisor_softc *sc, struct usb_device *udev, struct usb_config *config)
372 {
373 	usb_error_t err = 0;
374 	struct usb_device_request req;
375 	struct uvisor_connection_info coninfo;
376 	struct uvisor_palm_connection_info pconinfo;
377 	uint16_t actlen;
378 	uWord wAvail;
379 	uint8_t buffer[256];
380 
381 	if (sc->sc_flag & UVISOR_FLAG_VISOR) {
382 		DPRINTF("getting connection info\n");
383 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
384 		req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
385 		USETW(req.wValue, 0);
386 		USETW(req.wIndex, 0);
387 		USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
388 		err = usbd_do_request_flags(udev, NULL,
389 		    &req, &coninfo, USB_SHORT_XFER_OK,
390 		    &actlen, USB_DEFAULT_TIMEOUT);
391 
392 		if (err) {
393 			goto done;
394 		}
395 	}
396 #if USB_DEBUG
397 	if (sc->sc_flag & UVISOR_FLAG_VISOR) {
398 		uint16_t i, np;
399 		const char *desc;
400 
401 		np = UGETW(coninfo.num_ports);
402 		if (np > UVISOR_MAX_CONN) {
403 			np = UVISOR_MAX_CONN;
404 		}
405 		DPRINTF("Number of ports: %d\n", np);
406 
407 		for (i = 0; i < np; ++i) {
408 			switch (coninfo.connections[i].port_function_id) {
409 			case UVISOR_FUNCTION_GENERIC:
410 				desc = "Generic";
411 				break;
412 			case UVISOR_FUNCTION_DEBUGGER:
413 				desc = "Debugger";
414 				break;
415 			case UVISOR_FUNCTION_HOTSYNC:
416 				desc = "HotSync";
417 				break;
418 			case UVISOR_FUNCTION_REMOTE_FILE_SYS:
419 				desc = "Remote File System";
420 				break;
421 			default:
422 				desc = "unknown";
423 				break;
424 			}
425 			DPRINTF("Port %d is for %s\n",
426 			    coninfo.connections[i].port, desc);
427 		}
428 	}
429 #endif
430 
431 	if (sc->sc_flag & UVISOR_FLAG_PALM4) {
432 		uint8_t port;
433 
434 		/* Palm OS 4.0 Hack */
435 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
436 		req.bRequest = UVISOR_GET_PALM_INFORMATION;
437 		USETW(req.wValue, 0);
438 		USETW(req.wIndex, 0);
439 		USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
440 
441 		err = usbd_do_request_flags
442 		    (udev, NULL, &req, &pconinfo, USB_SHORT_XFER_OK,
443 		    &actlen, USB_DEFAULT_TIMEOUT);
444 
445 		if (err) {
446 			goto done;
447 		}
448 		if (actlen < 12) {
449 			DPRINTF("too little data\n");
450 			err = USB_ERR_INVAL;
451 			goto done;
452 		}
453 		if (pconinfo.endpoint_numbers_different) {
454 			port = pconinfo.connections[0].end_point_info;
455 			config[0].endpoint = (port & 0xF);	/* output */
456 			config[1].endpoint = (port >> 4);	/* input */
457 		} else {
458 			port = pconinfo.connections[0].port;
459 			config[0].endpoint = (port & 0xF);	/* output */
460 			config[1].endpoint = (port & 0xF);	/* input */
461 		}
462 #if 0
463 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
464 		req.bRequest = UVISOR_GET_PALM_INFORMATION;
465 		USETW(req.wValue, 0);
466 		USETW(req.wIndex, 0);
467 		USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
468 		err = usbd_do_request(udev, &req, buffer);
469 		if (err) {
470 			goto done;
471 		}
472 #endif
473 	}
474 	if (sc->sc_flag & UVISOR_FLAG_PALM35) {
475 		/* get the config number */
476 		DPRINTF("getting config info\n");
477 		req.bmRequestType = UT_READ;
478 		req.bRequest = UR_GET_CONFIG;
479 		USETW(req.wValue, 0);
480 		USETW(req.wIndex, 0);
481 		USETW(req.wLength, 1);
482 
483 		err = usbd_do_request(udev, NULL, &req, buffer);
484 		if (err) {
485 			goto done;
486 		}
487 		/* get the interface number */
488 		DPRINTF("get the interface number\n");
489 		req.bmRequestType = UT_READ_DEVICE;
490 		req.bRequest = UR_GET_INTERFACE;
491 		USETW(req.wValue, 0);
492 		USETW(req.wIndex, 0);
493 		USETW(req.wLength, 1);
494 		err = usbd_do_request(udev, NULL, &req, buffer);
495 		if (err) {
496 			goto done;
497 		}
498 	}
499 	DPRINTF("getting available bytes\n");
500 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
501 	req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
502 	USETW(req.wValue, 0);
503 	USETW(req.wIndex, 5);
504 	USETW(req.wLength, sizeof(wAvail));
505 	err = usbd_do_request(udev, NULL, &req, &wAvail);
506 	if (err) {
507 		goto done;
508 	}
509 	DPRINTF("avail=%d\n", UGETW(wAvail));
510 
511 	DPRINTF("done\n");
512 done:
513 	return (err);
514 }
515 
516 static void
517 uvisor_cfg_open(struct ucom_softc *ucom)
518 {
519 	return;
520 }
521 
522 static void
523 uvisor_cfg_close(struct ucom_softc *ucom)
524 {
525 	struct uvisor_softc *sc = ucom->sc_parent;
526 	uint8_t buffer[UVISOR_CONNECTION_INFO_SIZE];
527 	struct usb_device_request req;
528 	usb_error_t err;
529 
530 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;	/* XXX read? */
531 	req.bRequest = UVISOR_CLOSE_NOTIFICATION;
532 	USETW(req.wValue, 0);
533 	USETW(req.wIndex, 0);
534 	USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
535 
536 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
537 	    &req, buffer, 0, 1000);
538 	if (err) {
539 		DPRINTFN(0, "close notification failed, error=%s\n",
540 		    usbd_errstr(err));
541 	}
542 }
543 
544 static void
545 uvisor_start_read(struct ucom_softc *ucom)
546 {
547 	struct uvisor_softc *sc = ucom->sc_parent;
548 
549 	usbd_transfer_start(sc->sc_xfer[UVISOR_BULK_DT_RD]);
550 }
551 
552 static void
553 uvisor_stop_read(struct ucom_softc *ucom)
554 {
555 	struct uvisor_softc *sc = ucom->sc_parent;
556 
557 	usbd_transfer_stop(sc->sc_xfer[UVISOR_BULK_DT_RD]);
558 }
559 
560 static void
561 uvisor_start_write(struct ucom_softc *ucom)
562 {
563 	struct uvisor_softc *sc = ucom->sc_parent;
564 
565 	usbd_transfer_start(sc->sc_xfer[UVISOR_BULK_DT_WR]);
566 }
567 
568 static void
569 uvisor_stop_write(struct ucom_softc *ucom)
570 {
571 	struct uvisor_softc *sc = ucom->sc_parent;
572 
573 	usbd_transfer_stop(sc->sc_xfer[UVISOR_BULK_DT_WR]);
574 }
575 
576 static void
577 uvisor_write_callback(struct usb_xfer *xfer, usb_error_t error)
578 {
579 	struct uvisor_softc *sc = usbd_xfer_softc(xfer);
580 	struct usb_page_cache *pc;
581 	uint32_t actlen;
582 
583 	switch (USB_GET_STATE(xfer)) {
584 	case USB_ST_SETUP:
585 	case USB_ST_TRANSFERRED:
586 tr_setup:
587 		pc = usbd_xfer_get_frame(xfer, 0);
588 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
589 		    UVISOR_BUFSIZE, &actlen)) {
590 
591 			usbd_xfer_set_frame_len(xfer, 0, actlen);
592 			usbd_transfer_submit(xfer);
593 		}
594 		return;
595 
596 	default:			/* Error */
597 		if (error != USB_ERR_CANCELLED) {
598 			/* try to clear stall first */
599 			usbd_xfer_set_stall(xfer);
600 			goto tr_setup;
601 		}
602 		return;
603 	}
604 }
605 
606 static void
607 uvisor_read_callback(struct usb_xfer *xfer, usb_error_t error)
608 {
609 	struct uvisor_softc *sc = usbd_xfer_softc(xfer);
610 	struct usb_page_cache *pc;
611 	int actlen;
612 
613 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
614 
615 	switch (USB_GET_STATE(xfer)) {
616 	case USB_ST_TRANSFERRED:
617 		pc = usbd_xfer_get_frame(xfer, 0);
618 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
619 
620 	case USB_ST_SETUP:
621 tr_setup:
622 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
623 		usbd_transfer_submit(xfer);
624 		return;
625 
626 	default:			/* Error */
627 		if (error != USB_ERR_CANCELLED) {
628 			/* try to clear stall first */
629 			usbd_xfer_set_stall(xfer);
630 			goto tr_setup;
631 		}
632 		return;
633 	}
634 }
635