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