xref: /freebsd/sys/dev/usb/serial/uvisor.c (revision 6ba2210ee039f2f12878c217bcf058e9c8b26b29)
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  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
18  *
19  * Copyright (c) 2000 The NetBSD Foundation, Inc.
20  * All rights reserved.
21  *
22  * This code is derived from software contributed to The NetBSD Foundation
23  * by Lennart Augustsson (lennart@augustsson.net) at
24  * Carlstedt Research & Technology.
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions
28  * are met:
29  * 1. Redistributions of source code must retain the above copyright
30  *    notice, this list of conditions and the following disclaimer.
31  * 2. Redistributions in binary form must reproduce the above copyright
32  *    notice, this list of conditions and the following disclaimer in the
33  *    documentation and/or other materials provided with the distribution.
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
36  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
37  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
40  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
42  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
43  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45  * POSSIBILITY OF SUCH DAMAGE.
46  */
47 
48 /*
49  * Handspring Visor (Palmpilot compatible PDA) driver
50  */
51 
52 #include <sys/stdint.h>
53 #include <sys/stddef.h>
54 #include <sys/param.h>
55 #include <sys/queue.h>
56 #include <sys/types.h>
57 #include <sys/systm.h>
58 #include <sys/kernel.h>
59 #include <sys/bus.h>
60 #include <sys/module.h>
61 #include <sys/lock.h>
62 #include <sys/mutex.h>
63 #include <sys/condvar.h>
64 #include <sys/sysctl.h>
65 #include <sys/sx.h>
66 #include <sys/unistd.h>
67 #include <sys/callout.h>
68 #include <sys/malloc.h>
69 #include <sys/priv.h>
70 
71 #include <dev/usb/usb.h>
72 #include <dev/usb/usbdi.h>
73 #include <dev/usb/usbdi_util.h>
74 #include "usbdevs.h"
75 
76 #define	USB_DEBUG_VAR uvisor_debug
77 #include <dev/usb/usb_debug.h>
78 #include <dev/usb/usb_process.h>
79 
80 #include <dev/usb/serial/usb_serial.h>
81 
82 #ifdef USB_DEBUG
83 static int uvisor_debug = 0;
84 
85 static SYSCTL_NODE(_hw_usb, OID_AUTO, uvisor, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
86     "USB uvisor");
87 SYSCTL_INT(_hw_usb_uvisor, OID_AUTO, debug, CTLFLAG_RWTUN,
88     &uvisor_debug, 0, "Debug level");
89 #endif
90 
91 #define	UVISOR_CONFIG_INDEX	0
92 #define	UVISOR_IFACE_INDEX	0
93 
94 /*
95  * The following buffer sizes are hardcoded due to the way the Palm
96  * firmware works. It looks like the device is not short terminating
97  * the data transferred.
98  */
99 #define	UVISORIBUFSIZE	       0	/* Use wMaxPacketSize */
100 #define	UVISOROBUFSIZE	       32	/* bytes */
101 #define	UVISOROFRAMES	       32	/* units */
102 
103 /* From the Linux driver */
104 /*
105  * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
106  * are available to be transferred to the host for the specified endpoint.
107  * Currently this is not used, and always returns 0x0001
108  */
109 #define	UVISOR_REQUEST_BYTES_AVAILABLE		0x01
110 
111 /*
112  * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
113  * is now closing the pipe. An empty packet is sent in response.
114  */
115 #define	UVISOR_CLOSE_NOTIFICATION		0x02
116 
117 /*
118  * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
119  * get the endpoints used by the connection.
120  */
121 #define	UVISOR_GET_CONNECTION_INFORMATION	0x03
122 
123 /*
124  * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
125  */
126 #define	UVISOR_MAX_CONN 8
127 struct uvisor_connection_info {
128 	uWord	num_ports;
129 	struct {
130 		uByte	port_function_id;
131 		uByte	port;
132 	} __packed connections[UVISOR_MAX_CONN];
133 } __packed;
134 
135 #define	UVISOR_CONNECTION_INFO_SIZE 18
136 
137 /* struct uvisor_connection_info.connection[x].port defines: */
138 #define	UVISOR_ENDPOINT_1		0x01
139 #define	UVISOR_ENDPOINT_2		0x02
140 
141 /* struct uvisor_connection_info.connection[x].port_function_id defines: */
142 #define	UVISOR_FUNCTION_GENERIC		0x00
143 #define	UVISOR_FUNCTION_DEBUGGER	0x01
144 #define	UVISOR_FUNCTION_HOTSYNC		0x02
145 #define	UVISOR_FUNCTION_CONSOLE		0x03
146 #define	UVISOR_FUNCTION_REMOTE_FILE_SYS	0x04
147 
148 /*
149  * Unknown PalmOS stuff.
150  */
151 #define	UVISOR_GET_PALM_INFORMATION		0x04
152 #define	UVISOR_GET_PALM_INFORMATION_LEN		0x44
153 
154 struct uvisor_palm_connection_info {
155 	uByte	num_ports;
156 	uByte	endpoint_numbers_different;
157 	uWord	reserved1;
158 	struct {
159 		uDWord	port_function_id;
160 		uByte	port;
161 		uByte	end_point_info;
162 		uWord	reserved;
163 	} __packed connections[UVISOR_MAX_CONN];
164 } __packed;
165 
166 enum {
167 	UVISOR_BULK_DT_WR,
168 	UVISOR_BULK_DT_RD,
169 	UVISOR_N_TRANSFER,
170 };
171 
172 struct uvisor_softc {
173 	struct ucom_super_softc sc_super_ucom;
174 	struct ucom_softc sc_ucom;
175 
176 	struct usb_xfer *sc_xfer[UVISOR_N_TRANSFER];
177 	struct usb_device *sc_udev;
178 	struct mtx sc_mtx;
179 
180 	uint16_t sc_flag;
181 #define	UVISOR_FLAG_PALM4       0x0001
182 #define	UVISOR_FLAG_VISOR       0x0002
183 #define	UVISOR_FLAG_PALM35      0x0004
184 #define	UVISOR_FLAG_SEND_NOTIFY 0x0008
185 
186 	uint8_t	sc_iface_no;
187 	uint8_t	sc_iface_index;
188 };
189 
190 /* prototypes */
191 
192 static device_probe_t uvisor_probe;
193 static device_attach_t uvisor_attach;
194 static device_detach_t uvisor_detach;
195 static void uvisor_free_softc(struct uvisor_softc *);
196 
197 static usb_callback_t uvisor_write_callback;
198 static usb_callback_t uvisor_read_callback;
199 
200 static usb_error_t uvisor_init(struct uvisor_softc *, struct usb_device *,
201 		    struct usb_config *);
202 static void	uvisor_free(struct ucom_softc *);
203 static void	uvisor_cfg_open(struct ucom_softc *);
204 static void	uvisor_cfg_close(struct ucom_softc *);
205 static void	uvisor_start_read(struct ucom_softc *);
206 static void	uvisor_stop_read(struct ucom_softc *);
207 static void	uvisor_start_write(struct ucom_softc *);
208 static void	uvisor_stop_write(struct ucom_softc *);
209 
210 static const struct usb_config uvisor_config[UVISOR_N_TRANSFER] = {
211 	[UVISOR_BULK_DT_WR] = {
212 		.type = UE_BULK,
213 		.endpoint = UE_ADDR_ANY,
214 		.direction = UE_DIR_OUT,
215 		.bufsize = UVISOROBUFSIZE * UVISOROFRAMES,
216 		.frames = UVISOROFRAMES,
217 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
218 		.callback = &uvisor_write_callback,
219 	},
220 
221 	[UVISOR_BULK_DT_RD] = {
222 		.type = UE_BULK,
223 		.endpoint = UE_ADDR_ANY,
224 		.direction = UE_DIR_IN,
225 		.bufsize = UVISORIBUFSIZE,
226 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
227 		.callback = &uvisor_read_callback,
228 	},
229 };
230 
231 static const struct ucom_callback uvisor_callback = {
232 	.ucom_cfg_open = &uvisor_cfg_open,
233 	.ucom_cfg_close = &uvisor_cfg_close,
234 	.ucom_start_read = &uvisor_start_read,
235 	.ucom_stop_read = &uvisor_stop_read,
236 	.ucom_start_write = &uvisor_start_write,
237 	.ucom_stop_write = &uvisor_stop_write,
238 	.ucom_free = &uvisor_free,
239 };
240 
241 static device_method_t uvisor_methods[] = {
242 	DEVMETHOD(device_probe, uvisor_probe),
243 	DEVMETHOD(device_attach, uvisor_attach),
244 	DEVMETHOD(device_detach, uvisor_detach),
245 	DEVMETHOD_END
246 };
247 
248 static devclass_t uvisor_devclass;
249 
250 static driver_t uvisor_driver = {
251 	.name = "uvisor",
252 	.methods = uvisor_methods,
253 	.size = sizeof(struct uvisor_softc),
254 };
255 
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),
285 #undef UVISOR_DEV
286 };
287 
288 DRIVER_MODULE(uvisor, uhub, uvisor_driver, uvisor_devclass, NULL, 0);
289 MODULE_DEPEND(uvisor, ucom, 1, 1, 1);
290 MODULE_DEPEND(uvisor, usb, 1, 1, 1);
291 MODULE_VERSION(uvisor, 1);
292 USB_PNP_HOST_INFO(uvisor_devs);
293 
294 static int
295 uvisor_probe(device_t dev)
296 {
297 	struct usb_attach_arg *uaa = device_get_ivars(dev);
298 
299 	if (uaa->usb_mode != USB_MODE_HOST) {
300 		return (ENXIO);
301 	}
302 	if (uaa->info.bConfigIndex != UVISOR_CONFIG_INDEX) {
303 		return (ENXIO);
304 	}
305 	if (uaa->info.bIfaceIndex != UVISOR_IFACE_INDEX) {
306 		return (ENXIO);
307 	}
308 	return (usbd_lookup_id_by_uaa(uvisor_devs, sizeof(uvisor_devs), uaa));
309 }
310 
311 static int
312 uvisor_attach(device_t dev)
313 {
314 	struct usb_attach_arg *uaa = device_get_ivars(dev);
315 	struct uvisor_softc *sc = device_get_softc(dev);
316 	struct usb_config uvisor_config_copy[UVISOR_N_TRANSFER];
317 	int error;
318 
319 	DPRINTF("sc=%p\n", sc);
320 	memcpy(uvisor_config_copy, uvisor_config,
321 	    sizeof(uvisor_config_copy));
322 
323 	device_set_usb_desc(dev);
324 
325 	mtx_init(&sc->sc_mtx, "uvisor", NULL, MTX_DEF);
326 	ucom_ref(&sc->sc_super_ucom);
327 
328 	sc->sc_udev = uaa->device;
329 
330 	/* configure the device */
331 
332 	sc->sc_flag = USB_GET_DRIVER_INFO(uaa);
333 	sc->sc_iface_no = uaa->info.bIfaceNum;
334 	sc->sc_iface_index = UVISOR_IFACE_INDEX;
335 
336 	error = uvisor_init(sc, uaa->device, uvisor_config_copy);
337 
338 	if (error) {
339 		DPRINTF("init failed, error=%s\n",
340 		    usbd_errstr(error));
341 		goto detach;
342 	}
343 	error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
344 	    sc->sc_xfer, uvisor_config_copy, UVISOR_N_TRANSFER,
345 	    sc, &sc->sc_mtx);
346 	if (error) {
347 		DPRINTF("could not allocate all pipes\n");
348 		goto detach;
349 	}
350 
351 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
352 	    &uvisor_callback, &sc->sc_mtx);
353 	if (error) {
354 		DPRINTF("ucom_attach failed\n");
355 		goto detach;
356 	}
357 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
358 
359 	return (0);
360 
361 detach:
362 	uvisor_detach(dev);
363 	return (ENXIO);
364 }
365 
366 static int
367 uvisor_detach(device_t dev)
368 {
369 	struct uvisor_softc *sc = device_get_softc(dev);
370 
371 	DPRINTF("sc=%p\n", sc);
372 
373 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
374 	usbd_transfer_unsetup(sc->sc_xfer, UVISOR_N_TRANSFER);
375 
376 	device_claim_softc(dev);
377 
378 	uvisor_free_softc(sc);
379 
380 	return (0);
381 }
382 
383 UCOM_UNLOAD_DRAIN(uvisor);
384 
385 static void
386 uvisor_free_softc(struct uvisor_softc *sc)
387 {
388 	if (ucom_unref(&sc->sc_super_ucom)) {
389 		mtx_destroy(&sc->sc_mtx);
390 		device_free_softc(sc);
391 	}
392 }
393 
394 static void
395 uvisor_free(struct ucom_softc *ucom)
396 {
397 	uvisor_free_softc(ucom->sc_parent);
398 }
399 
400 static usb_error_t
401 uvisor_init(struct uvisor_softc *sc, struct usb_device *udev, struct usb_config *config)
402 {
403 	usb_error_t err = 0;
404 	struct usb_device_request req;
405 	struct uvisor_connection_info coninfo;
406 	struct uvisor_palm_connection_info pconinfo;
407 	uint16_t actlen;
408 	uint8_t buffer[256];
409 
410 	if (sc->sc_flag & UVISOR_FLAG_VISOR) {
411 		DPRINTF("getting connection info\n");
412 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
413 		req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
414 		USETW(req.wValue, 0);
415 		USETW(req.wIndex, 0);
416 		USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
417 		err = usbd_do_request_flags(udev, NULL,
418 		    &req, &coninfo, USB_SHORT_XFER_OK,
419 		    &actlen, USB_DEFAULT_TIMEOUT);
420 
421 		if (err) {
422 			goto done;
423 		}
424 	}
425 #ifdef USB_DEBUG
426 	if (sc->sc_flag & UVISOR_FLAG_VISOR) {
427 		uint16_t i, np;
428 		const char *desc;
429 
430 		np = UGETW(coninfo.num_ports);
431 		if (np > UVISOR_MAX_CONN) {
432 			np = UVISOR_MAX_CONN;
433 		}
434 		DPRINTF("Number of ports: %d\n", np);
435 
436 		for (i = 0; i < np; ++i) {
437 			switch (coninfo.connections[i].port_function_id) {
438 			case UVISOR_FUNCTION_GENERIC:
439 				desc = "Generic";
440 				break;
441 			case UVISOR_FUNCTION_DEBUGGER:
442 				desc = "Debugger";
443 				break;
444 			case UVISOR_FUNCTION_HOTSYNC:
445 				desc = "HotSync";
446 				break;
447 			case UVISOR_FUNCTION_REMOTE_FILE_SYS:
448 				desc = "Remote File System";
449 				break;
450 			default:
451 				desc = "unknown";
452 				break;
453 			}
454 			DPRINTF("Port %d is for %s\n",
455 			    coninfo.connections[i].port, desc);
456 		}
457 	}
458 #endif
459 
460 	if (sc->sc_flag & UVISOR_FLAG_PALM4) {
461 		uint8_t port;
462 
463 		/* Palm OS 4.0 Hack */
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 
470 		err = usbd_do_request_flags
471 		    (udev, NULL, &req, &pconinfo, USB_SHORT_XFER_OK,
472 		    &actlen, USB_DEFAULT_TIMEOUT);
473 
474 		if (err) {
475 			goto done;
476 		}
477 		if (actlen < 12) {
478 			DPRINTF("too little data\n");
479 			err = USB_ERR_INVAL;
480 			goto done;
481 		}
482 		if (pconinfo.endpoint_numbers_different) {
483 			port = pconinfo.connections[0].end_point_info;
484 			config[0].endpoint = (port & 0xF);	/* output */
485 			config[1].endpoint = (port >> 4);	/* input */
486 		} else {
487 			port = pconinfo.connections[0].port;
488 			config[0].endpoint = (port & 0xF);	/* output */
489 			config[1].endpoint = (port & 0xF);	/* input */
490 		}
491 #if 0
492 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
493 		req.bRequest = UVISOR_GET_PALM_INFORMATION;
494 		USETW(req.wValue, 0);
495 		USETW(req.wIndex, 0);
496 		USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
497 		err = usbd_do_request(udev, &req, buffer);
498 		if (err) {
499 			goto done;
500 		}
501 #endif
502 	}
503 	if (sc->sc_flag & UVISOR_FLAG_PALM35) {
504 		/* get the config number */
505 		DPRINTF("getting config info\n");
506 		req.bmRequestType = UT_READ;
507 		req.bRequest = UR_GET_CONFIG;
508 		USETW(req.wValue, 0);
509 		USETW(req.wIndex, 0);
510 		USETW(req.wLength, 1);
511 
512 		err = usbd_do_request(udev, NULL, &req, buffer);
513 		if (err) {
514 			goto done;
515 		}
516 		/* get the interface number */
517 		DPRINTF("get the interface number\n");
518 		req.bmRequestType = UT_READ_DEVICE;
519 		req.bRequest = UR_GET_INTERFACE;
520 		USETW(req.wValue, 0);
521 		USETW(req.wIndex, 0);
522 		USETW(req.wLength, 1);
523 		err = usbd_do_request(udev, NULL, &req, buffer);
524 		if (err) {
525 			goto done;
526 		}
527 	}
528 #if 0
529 	uWord wAvail;
530 
531 	DPRINTF("getting available bytes\n");
532 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
533 	req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
534 	USETW(req.wValue, 0);
535 	USETW(req.wIndex, 5);
536 	USETW(req.wLength, sizeof(wAvail));
537 	err = usbd_do_request(udev, NULL, &req, &wAvail);
538 	if (err) {
539 		goto done;
540 	}
541 	DPRINTF("avail=%d\n", UGETW(wAvail));
542 #endif
543 
544 	DPRINTF("done\n");
545 done:
546 	return (err);
547 }
548 
549 static void
550 uvisor_cfg_open(struct ucom_softc *ucom)
551 {
552 	return;
553 }
554 
555 static void
556 uvisor_cfg_close(struct ucom_softc *ucom)
557 {
558 	struct uvisor_softc *sc = ucom->sc_parent;
559 	uint8_t buffer[UVISOR_CONNECTION_INFO_SIZE];
560 	struct usb_device_request req;
561 	usb_error_t err;
562 
563 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;	/* XXX read? */
564 	req.bRequest = UVISOR_CLOSE_NOTIFICATION;
565 	USETW(req.wValue, 0);
566 	USETW(req.wIndex, 0);
567 	USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
568 
569 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
570 	    &req, buffer, 0, 1000);
571 	if (err) {
572 		DPRINTFN(0, "close notification failed, error=%s\n",
573 		    usbd_errstr(err));
574 	}
575 }
576 
577 static void
578 uvisor_start_read(struct ucom_softc *ucom)
579 {
580 	struct uvisor_softc *sc = ucom->sc_parent;
581 
582 	usbd_transfer_start(sc->sc_xfer[UVISOR_BULK_DT_RD]);
583 }
584 
585 static void
586 uvisor_stop_read(struct ucom_softc *ucom)
587 {
588 	struct uvisor_softc *sc = ucom->sc_parent;
589 
590 	usbd_transfer_stop(sc->sc_xfer[UVISOR_BULK_DT_RD]);
591 }
592 
593 static void
594 uvisor_start_write(struct ucom_softc *ucom)
595 {
596 	struct uvisor_softc *sc = ucom->sc_parent;
597 
598 	usbd_transfer_start(sc->sc_xfer[UVISOR_BULK_DT_WR]);
599 }
600 
601 static void
602 uvisor_stop_write(struct ucom_softc *ucom)
603 {
604 	struct uvisor_softc *sc = ucom->sc_parent;
605 
606 	usbd_transfer_stop(sc->sc_xfer[UVISOR_BULK_DT_WR]);
607 }
608 
609 static void
610 uvisor_write_callback(struct usb_xfer *xfer, usb_error_t error)
611 {
612 	struct uvisor_softc *sc = usbd_xfer_softc(xfer);
613 	struct usb_page_cache *pc;
614 	uint32_t actlen;
615 	uint8_t x;
616 
617 	switch (USB_GET_STATE(xfer)) {
618 	case USB_ST_SETUP:
619 	case USB_ST_TRANSFERRED:
620 tr_setup:
621 		for (x = 0; x != UVISOROFRAMES; x++) {
622 			usbd_xfer_set_frame_offset(xfer,
623 			    x * UVISOROBUFSIZE, x);
624 
625 			pc = usbd_xfer_get_frame(xfer, x);
626 			if (ucom_get_data(&sc->sc_ucom, pc, 0,
627 			    UVISOROBUFSIZE, &actlen)) {
628 				usbd_xfer_set_frame_len(xfer, x, actlen);
629 			} else {
630 				break;
631 			}
632 		}
633 		/* check for data */
634 		if (x != 0) {
635 			usbd_xfer_set_frames(xfer, x);
636 			usbd_transfer_submit(xfer);
637 		}
638 		break;
639 
640 	default:			/* Error */
641 		if (error != USB_ERR_CANCELLED) {
642 			/* try to clear stall first */
643 			usbd_xfer_set_stall(xfer);
644 			goto tr_setup;
645 		}
646 		break;
647 	}
648 }
649 
650 static void
651 uvisor_read_callback(struct usb_xfer *xfer, usb_error_t error)
652 {
653 	struct uvisor_softc *sc = usbd_xfer_softc(xfer);
654 	struct usb_page_cache *pc;
655 	int actlen;
656 
657 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
658 
659 	switch (USB_GET_STATE(xfer)) {
660 	case USB_ST_TRANSFERRED:
661 		pc = usbd_xfer_get_frame(xfer, 0);
662 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
663 
664 	case USB_ST_SETUP:
665 tr_setup:
666 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
667 		usbd_transfer_submit(xfer);
668 		return;
669 
670 	default:			/* Error */
671 		if (error != USB_ERR_CANCELLED) {
672 			/* try to clear stall first */
673 			usbd_xfer_set_stall(xfer);
674 			goto tr_setup;
675 		}
676 		return;
677 	}
678 }
679