xref: /freebsd/sys/dev/usb/serial/uvisor.c (revision eb6d21b4ca6d668cf89afd99eef7baeafa712197)
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 
99 /*
100  * The following buffer sizes are hardcoded due to the way the Palm
101  * firmware works. It looks like the device is not short terminating
102  * the data transferred.
103  */
104 #define	UVISORIBUFSIZE	       0	/* Use wMaxPacketSize */
105 #define	UVISOROBUFSIZE	       32	/* bytes */
106 #define	UVISOROFRAMES	       32	/* units */
107 
108 /* From the Linux driver */
109 /*
110  * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
111  * are available to be transfered to the host for the specified endpoint.
112  * Currently this is not used, and always returns 0x0001
113  */
114 #define	UVISOR_REQUEST_BYTES_AVAILABLE		0x01
115 
116 /*
117  * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
118  * is now closing the pipe. An empty packet is sent in response.
119  */
120 #define	UVISOR_CLOSE_NOTIFICATION		0x02
121 
122 /*
123  * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
124  * get the endpoints used by the connection.
125  */
126 #define	UVISOR_GET_CONNECTION_INFORMATION	0x03
127 
128 /*
129  * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
130  */
131 #define	UVISOR_MAX_CONN 8
132 struct uvisor_connection_info {
133 	uWord	num_ports;
134 	struct {
135 		uByte	port_function_id;
136 		uByte	port;
137 	} __packed connections[UVISOR_MAX_CONN];
138 } __packed;
139 
140 #define	UVISOR_CONNECTION_INFO_SIZE 18
141 
142 /* struct uvisor_connection_info.connection[x].port defines: */
143 #define	UVISOR_ENDPOINT_1		0x01
144 #define	UVISOR_ENDPOINT_2		0x02
145 
146 /* struct uvisor_connection_info.connection[x].port_function_id defines: */
147 #define	UVISOR_FUNCTION_GENERIC		0x00
148 #define	UVISOR_FUNCTION_DEBUGGER	0x01
149 #define	UVISOR_FUNCTION_HOTSYNC		0x02
150 #define	UVISOR_FUNCTION_CONSOLE		0x03
151 #define	UVISOR_FUNCTION_REMOTE_FILE_SYS	0x04
152 
153 /*
154  * Unknown PalmOS stuff.
155  */
156 #define	UVISOR_GET_PALM_INFORMATION		0x04
157 #define	UVISOR_GET_PALM_INFORMATION_LEN		0x44
158 
159 struct uvisor_palm_connection_info {
160 	uByte	num_ports;
161 	uByte	endpoint_numbers_different;
162 	uWord	reserved1;
163 	struct {
164 		uDWord	port_function_id;
165 		uByte	port;
166 		uByte	end_point_info;
167 		uWord	reserved;
168 	} __packed connections[UVISOR_MAX_CONN];
169 } __packed;
170 
171 enum {
172 	UVISOR_BULK_DT_WR,
173 	UVISOR_BULK_DT_RD,
174 	UVISOR_N_TRANSFER,
175 };
176 
177 struct uvisor_softc {
178 	struct ucom_super_softc sc_super_ucom;
179 	struct ucom_softc sc_ucom;
180 
181 	struct usb_xfer *sc_xfer[UVISOR_N_TRANSFER];
182 	struct usb_device *sc_udev;
183 	struct mtx sc_mtx;
184 
185 	uint16_t sc_flag;
186 #define	UVISOR_FLAG_PALM4       0x0001
187 #define	UVISOR_FLAG_VISOR       0x0002
188 #define	UVISOR_FLAG_PALM35      0x0004
189 #define	UVISOR_FLAG_SEND_NOTIFY 0x0008
190 
191 	uint8_t	sc_iface_no;
192 	uint8_t	sc_iface_index;
193 };
194 
195 /* prototypes */
196 
197 static device_probe_t uvisor_probe;
198 static device_attach_t uvisor_attach;
199 static device_detach_t uvisor_detach;
200 
201 static usb_callback_t uvisor_write_callback;
202 static usb_callback_t uvisor_read_callback;
203 
204 static usb_error_t uvisor_init(struct uvisor_softc *, struct usb_device *,
205 		    struct usb_config *);
206 static void	uvisor_cfg_open(struct ucom_softc *);
207 static void	uvisor_cfg_close(struct ucom_softc *);
208 static void	uvisor_start_read(struct ucom_softc *);
209 static void	uvisor_stop_read(struct ucom_softc *);
210 static void	uvisor_start_write(struct ucom_softc *);
211 static void	uvisor_stop_write(struct ucom_softc *);
212 
213 static const struct usb_config uvisor_config[UVISOR_N_TRANSFER] = {
214 
215 	[UVISOR_BULK_DT_WR] = {
216 		.type = UE_BULK,
217 		.endpoint = UE_ADDR_ANY,
218 		.direction = UE_DIR_OUT,
219 		.bufsize = UVISOROBUFSIZE * UVISOROFRAMES,
220 		.frames = UVISOROFRAMES,
221 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
222 		.callback = &uvisor_write_callback,
223 	},
224 
225 	[UVISOR_BULK_DT_RD] = {
226 		.type = UE_BULK,
227 		.endpoint = UE_ADDR_ANY,
228 		.direction = UE_DIR_IN,
229 		.bufsize = UVISORIBUFSIZE,
230 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
231 		.callback = &uvisor_read_callback,
232 	},
233 };
234 
235 static const struct ucom_callback uvisor_callback = {
236 	.ucom_cfg_open = &uvisor_cfg_open,
237 	.ucom_cfg_close = &uvisor_cfg_close,
238 	.ucom_start_read = &uvisor_start_read,
239 	.ucom_stop_read = &uvisor_stop_read,
240 	.ucom_start_write = &uvisor_start_write,
241 	.ucom_stop_write = &uvisor_stop_write,
242 };
243 
244 static device_method_t uvisor_methods[] = {
245 	DEVMETHOD(device_probe, uvisor_probe),
246 	DEVMETHOD(device_attach, uvisor_attach),
247 	DEVMETHOD(device_detach, uvisor_detach),
248 	{0, 0}
249 };
250 
251 static devclass_t uvisor_devclass;
252 
253 static driver_t uvisor_driver = {
254 	.name = "uvisor",
255 	.methods = uvisor_methods,
256 	.size = sizeof(struct uvisor_softc),
257 };
258 
259 DRIVER_MODULE(uvisor, uhub, uvisor_driver, uvisor_devclass, NULL, 0);
260 MODULE_DEPEND(uvisor, ucom, 1, 1, 1);
261 MODULE_DEPEND(uvisor, usb, 1, 1, 1);
262 
263 static const struct usb_device_id uvisor_devs[] = {
264 	{USB_VPI(USB_VENDOR_ACEECA, USB_PRODUCT_ACEECA_MEZ1000, UVISOR_FLAG_PALM4)},
265 	{USB_VPI(USB_VENDOR_GARMIN, USB_PRODUCT_GARMIN_IQUE_3600, UVISOR_FLAG_PALM4)},
266 	{USB_VPI(USB_VENDOR_FOSSIL, USB_PRODUCT_FOSSIL_WRISTPDA, UVISOR_FLAG_PALM4)},
267 	{USB_VPI(USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_VISOR, UVISOR_FLAG_VISOR)},
268 	{USB_VPI(USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO, UVISOR_FLAG_PALM4)},
269 	{USB_VPI(USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO600, UVISOR_FLAG_PALM4)},
270 	{USB_VPI(USB_VENDOR_PALM, USB_PRODUCT_PALM_M500, UVISOR_FLAG_PALM4)},
271 	{USB_VPI(USB_VENDOR_PALM, USB_PRODUCT_PALM_M505, UVISOR_FLAG_PALM4)},
272 	{USB_VPI(USB_VENDOR_PALM, USB_PRODUCT_PALM_M515, UVISOR_FLAG_PALM4)},
273 	{USB_VPI(USB_VENDOR_PALM, USB_PRODUCT_PALM_I705, UVISOR_FLAG_PALM4)},
274 	{USB_VPI(USB_VENDOR_PALM, USB_PRODUCT_PALM_M125, UVISOR_FLAG_PALM4)},
275 	{USB_VPI(USB_VENDOR_PALM, USB_PRODUCT_PALM_M130, UVISOR_FLAG_PALM4)},
276 	{USB_VPI(USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_Z, UVISOR_FLAG_PALM4)},
277 	{USB_VPI(USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_T, UVISOR_FLAG_PALM4)},
278 	{USB_VPI(USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE, UVISOR_FLAG_PALM4)},
279 	{USB_VPI(USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE31, UVISOR_FLAG_PALM4)},
280 	{USB_VPI(USB_VENDOR_SAMSUNG, USB_PRODUCT_SAMSUNG_I500, UVISOR_FLAG_PALM4)},
281 	{USB_VPI(USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40, 0)},
282 	{USB_VPI(USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_41, 0)},
283 	{USB_VPI(USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_S360, UVISOR_FLAG_PALM4)},
284 	{USB_VPI(USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_NX60, UVISOR_FLAG_PALM4)},
285 	{USB_VPI(USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_35, UVISOR_FLAG_PALM35)},
286 /*  {USB_VPI(USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25, UVISOR_FLAG_PALM4 )}, */
287 	{USB_VPI(USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_TJ37, UVISOR_FLAG_PALM4)},
288 /*  {USB_VPI(USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_TH55, UVISOR_FLAG_PALM4 )}, See PR 80935 */
289 	{USB_VPI(USB_VENDOR_TAPWAVE, USB_PRODUCT_TAPWAVE_ZODIAC, UVISOR_FLAG_PALM4)},
290 };
291 
292 static int
293 uvisor_probe(device_t dev)
294 {
295 	struct usb_attach_arg *uaa = device_get_ivars(dev);
296 
297 	if (uaa->usb_mode != USB_MODE_HOST) {
298 		return (ENXIO);
299 	}
300 	if (uaa->info.bConfigIndex != UVISOR_CONFIG_INDEX) {
301 		return (ENXIO);
302 	}
303 	if (uaa->info.bIfaceIndex != UVISOR_IFACE_INDEX) {
304 		return (ENXIO);
305 	}
306 	return (usbd_lookup_id_by_uaa(uvisor_devs, sizeof(uvisor_devs), uaa));
307 }
308 
309 static int
310 uvisor_attach(device_t dev)
311 {
312 	struct usb_attach_arg *uaa = device_get_ivars(dev);
313 	struct uvisor_softc *sc = device_get_softc(dev);
314 	struct usb_config uvisor_config_copy[UVISOR_N_TRANSFER];
315 	int error;
316 
317 	DPRINTF("sc=%p\n", sc);
318 	bcopy(uvisor_config, uvisor_config_copy,
319 	    sizeof(uvisor_config_copy));
320 	device_set_usb_desc(dev);
321 
322 	mtx_init(&sc->sc_mtx, "uvisor", NULL, MTX_DEF);
323 
324 	sc->sc_udev = uaa->device;
325 
326 	/* configure the device */
327 
328 	sc->sc_flag = USB_GET_DRIVER_INFO(uaa);
329 	sc->sc_iface_no = uaa->info.bIfaceNum;
330 	sc->sc_iface_index = UVISOR_IFACE_INDEX;
331 
332 	error = uvisor_init(sc, uaa->device, uvisor_config_copy);
333 
334 	if (error) {
335 		DPRINTF("init failed, error=%s\n",
336 		    usbd_errstr(error));
337 		goto detach;
338 	}
339 	error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
340 	    sc->sc_xfer, uvisor_config_copy, UVISOR_N_TRANSFER,
341 	    sc, &sc->sc_mtx);
342 	if (error) {
343 		DPRINTF("could not allocate all pipes\n");
344 		goto detach;
345 	}
346 	/* clear stall at first run */
347 	mtx_lock(&sc->sc_mtx);
348 	usbd_xfer_set_stall(sc->sc_xfer[UVISOR_BULK_DT_WR]);
349 	usbd_xfer_set_stall(sc->sc_xfer[UVISOR_BULK_DT_RD]);
350 	mtx_unlock(&sc->sc_mtx);
351 
352 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
353 	    &uvisor_callback, &sc->sc_mtx);
354 	if (error) {
355 		DPRINTF("ucom_attach failed\n");
356 		goto detach;
357 	}
358 	return (0);
359 
360 detach:
361 	uvisor_detach(dev);
362 	return (ENXIO);
363 }
364 
365 static int
366 uvisor_detach(device_t dev)
367 {
368 	struct uvisor_softc *sc = device_get_softc(dev);
369 
370 	DPRINTF("sc=%p\n", sc);
371 
372 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
373 	usbd_transfer_unsetup(sc->sc_xfer, UVISOR_N_TRANSFER);
374 	mtx_destroy(&sc->sc_mtx);
375 
376 	return (0);
377 }
378 
379 static usb_error_t
380 uvisor_init(struct uvisor_softc *sc, struct usb_device *udev, struct usb_config *config)
381 {
382 	usb_error_t err = 0;
383 	struct usb_device_request req;
384 	struct uvisor_connection_info coninfo;
385 	struct uvisor_palm_connection_info pconinfo;
386 	uint16_t actlen;
387 	uint8_t buffer[256];
388 
389 	if (sc->sc_flag & UVISOR_FLAG_VISOR) {
390 		DPRINTF("getting connection info\n");
391 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
392 		req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
393 		USETW(req.wValue, 0);
394 		USETW(req.wIndex, 0);
395 		USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
396 		err = usbd_do_request_flags(udev, NULL,
397 		    &req, &coninfo, USB_SHORT_XFER_OK,
398 		    &actlen, USB_DEFAULT_TIMEOUT);
399 
400 		if (err) {
401 			goto done;
402 		}
403 	}
404 #if USB_DEBUG
405 	if (sc->sc_flag & UVISOR_FLAG_VISOR) {
406 		uint16_t i, np;
407 		const char *desc;
408 
409 		np = UGETW(coninfo.num_ports);
410 		if (np > UVISOR_MAX_CONN) {
411 			np = UVISOR_MAX_CONN;
412 		}
413 		DPRINTF("Number of ports: %d\n", np);
414 
415 		for (i = 0; i < np; ++i) {
416 			switch (coninfo.connections[i].port_function_id) {
417 			case UVISOR_FUNCTION_GENERIC:
418 				desc = "Generic";
419 				break;
420 			case UVISOR_FUNCTION_DEBUGGER:
421 				desc = "Debugger";
422 				break;
423 			case UVISOR_FUNCTION_HOTSYNC:
424 				desc = "HotSync";
425 				break;
426 			case UVISOR_FUNCTION_REMOTE_FILE_SYS:
427 				desc = "Remote File System";
428 				break;
429 			default:
430 				desc = "unknown";
431 				break;
432 			}
433 			DPRINTF("Port %d is for %s\n",
434 			    coninfo.connections[i].port, desc);
435 		}
436 	}
437 #endif
438 
439 	if (sc->sc_flag & UVISOR_FLAG_PALM4) {
440 		uint8_t port;
441 
442 		/* Palm OS 4.0 Hack */
443 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
444 		req.bRequest = UVISOR_GET_PALM_INFORMATION;
445 		USETW(req.wValue, 0);
446 		USETW(req.wIndex, 0);
447 		USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
448 
449 		err = usbd_do_request_flags
450 		    (udev, NULL, &req, &pconinfo, USB_SHORT_XFER_OK,
451 		    &actlen, USB_DEFAULT_TIMEOUT);
452 
453 		if (err) {
454 			goto done;
455 		}
456 		if (actlen < 12) {
457 			DPRINTF("too little data\n");
458 			err = USB_ERR_INVAL;
459 			goto done;
460 		}
461 		if (pconinfo.endpoint_numbers_different) {
462 			port = pconinfo.connections[0].end_point_info;
463 			config[0].endpoint = (port & 0xF);	/* output */
464 			config[1].endpoint = (port >> 4);	/* input */
465 		} else {
466 			port = pconinfo.connections[0].port;
467 			config[0].endpoint = (port & 0xF);	/* output */
468 			config[1].endpoint = (port & 0xF);	/* input */
469 		}
470 #if 0
471 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
472 		req.bRequest = UVISOR_GET_PALM_INFORMATION;
473 		USETW(req.wValue, 0);
474 		USETW(req.wIndex, 0);
475 		USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
476 		err = usbd_do_request(udev, &req, buffer);
477 		if (err) {
478 			goto done;
479 		}
480 #endif
481 	}
482 	if (sc->sc_flag & UVISOR_FLAG_PALM35) {
483 		/* get the config number */
484 		DPRINTF("getting config info\n");
485 		req.bmRequestType = UT_READ;
486 		req.bRequest = UR_GET_CONFIG;
487 		USETW(req.wValue, 0);
488 		USETW(req.wIndex, 0);
489 		USETW(req.wLength, 1);
490 
491 		err = usbd_do_request(udev, NULL, &req, buffer);
492 		if (err) {
493 			goto done;
494 		}
495 		/* get the interface number */
496 		DPRINTF("get the interface number\n");
497 		req.bmRequestType = UT_READ_DEVICE;
498 		req.bRequest = UR_GET_INTERFACE;
499 		USETW(req.wValue, 0);
500 		USETW(req.wIndex, 0);
501 		USETW(req.wLength, 1);
502 		err = usbd_do_request(udev, NULL, &req, buffer);
503 		if (err) {
504 			goto done;
505 		}
506 	}
507 #if 0
508 	uWord wAvail;
509 
510 	DPRINTF("getting available bytes\n");
511 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
512 	req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
513 	USETW(req.wValue, 0);
514 	USETW(req.wIndex, 5);
515 	USETW(req.wLength, sizeof(wAvail));
516 	err = usbd_do_request(udev, NULL, &req, &wAvail);
517 	if (err) {
518 		goto done;
519 	}
520 	DPRINTF("avail=%d\n", UGETW(wAvail));
521 #endif
522 
523 	DPRINTF("done\n");
524 done:
525 	return (err);
526 }
527 
528 static void
529 uvisor_cfg_open(struct ucom_softc *ucom)
530 {
531 	return;
532 }
533 
534 static void
535 uvisor_cfg_close(struct ucom_softc *ucom)
536 {
537 	struct uvisor_softc *sc = ucom->sc_parent;
538 	uint8_t buffer[UVISOR_CONNECTION_INFO_SIZE];
539 	struct usb_device_request req;
540 	usb_error_t err;
541 
542 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;	/* XXX read? */
543 	req.bRequest = UVISOR_CLOSE_NOTIFICATION;
544 	USETW(req.wValue, 0);
545 	USETW(req.wIndex, 0);
546 	USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
547 
548 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
549 	    &req, buffer, 0, 1000);
550 	if (err) {
551 		DPRINTFN(0, "close notification failed, error=%s\n",
552 		    usbd_errstr(err));
553 	}
554 }
555 
556 static void
557 uvisor_start_read(struct ucom_softc *ucom)
558 {
559 	struct uvisor_softc *sc = ucom->sc_parent;
560 
561 	usbd_transfer_start(sc->sc_xfer[UVISOR_BULK_DT_RD]);
562 }
563 
564 static void
565 uvisor_stop_read(struct ucom_softc *ucom)
566 {
567 	struct uvisor_softc *sc = ucom->sc_parent;
568 
569 	usbd_transfer_stop(sc->sc_xfer[UVISOR_BULK_DT_RD]);
570 }
571 
572 static void
573 uvisor_start_write(struct ucom_softc *ucom)
574 {
575 	struct uvisor_softc *sc = ucom->sc_parent;
576 
577 	usbd_transfer_start(sc->sc_xfer[UVISOR_BULK_DT_WR]);
578 }
579 
580 static void
581 uvisor_stop_write(struct ucom_softc *ucom)
582 {
583 	struct uvisor_softc *sc = ucom->sc_parent;
584 
585 	usbd_transfer_stop(sc->sc_xfer[UVISOR_BULK_DT_WR]);
586 }
587 
588 static void
589 uvisor_write_callback(struct usb_xfer *xfer, usb_error_t error)
590 {
591 	struct uvisor_softc *sc = usbd_xfer_softc(xfer);
592 	struct usb_page_cache *pc;
593 	uint32_t actlen;
594 	uint8_t x;
595 
596 	switch (USB_GET_STATE(xfer)) {
597 	case USB_ST_SETUP:
598 	case USB_ST_TRANSFERRED:
599 tr_setup:
600 		for (x = 0; x != UVISOROFRAMES; x++) {
601 
602 			usbd_xfer_set_frame_offset(xfer,
603 			    x * UVISOROBUFSIZE, x);
604 
605 			pc = usbd_xfer_get_frame(xfer, x);
606 			if (ucom_get_data(&sc->sc_ucom, pc, 0,
607 			    UVISOROBUFSIZE, &actlen)) {
608 				usbd_xfer_set_frame_len(xfer, x, actlen);
609 			} else {
610 				break;
611 			}
612 		}
613 		/* check for data */
614 		if (x != 0) {
615 			usbd_xfer_set_frames(xfer, x);
616 			usbd_transfer_submit(xfer);
617 		}
618 		break;
619 
620 	default:			/* Error */
621 		if (error != USB_ERR_CANCELLED) {
622 			/* try to clear stall first */
623 			usbd_xfer_set_stall(xfer);
624 			goto tr_setup;
625 		}
626 		break;
627 	}
628 }
629 
630 static void
631 uvisor_read_callback(struct usb_xfer *xfer, usb_error_t error)
632 {
633 	struct uvisor_softc *sc = usbd_xfer_softc(xfer);
634 	struct usb_page_cache *pc;
635 	int actlen;
636 
637 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
638 
639 	switch (USB_GET_STATE(xfer)) {
640 	case USB_ST_TRANSFERRED:
641 		pc = usbd_xfer_get_frame(xfer, 0);
642 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
643 
644 	case USB_ST_SETUP:
645 tr_setup:
646 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
647 		usbd_transfer_submit(xfer);
648 		return;
649 
650 	default:			/* Error */
651 		if (error != USB_ERR_CANCELLED) {
652 			/* try to clear stall first */
653 			usbd_xfer_set_stall(xfer);
654 			goto tr_setup;
655 		}
656 		return;
657 	}
658 }
659