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