]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/usb/serial/uvisor.c
MFC r205031
[FreeBSD/stable/8.git] / sys / dev / usb / serial / uvisor.c
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 #define UVISOR_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
265         UVISOR_DEV(ACEECA, MEZ1000, UVISOR_FLAG_PALM4),
266         UVISOR_DEV(GARMIN, IQUE_3600, UVISOR_FLAG_PALM4),
267         UVISOR_DEV(FOSSIL, WRISTPDA, UVISOR_FLAG_PALM4),
268         UVISOR_DEV(HANDSPRING, VISOR, UVISOR_FLAG_VISOR),
269         UVISOR_DEV(HANDSPRING, TREO, UVISOR_FLAG_PALM4),
270         UVISOR_DEV(HANDSPRING, TREO600, UVISOR_FLAG_PALM4),
271         UVISOR_DEV(PALM, M500, UVISOR_FLAG_PALM4),
272         UVISOR_DEV(PALM, M505, UVISOR_FLAG_PALM4),
273         UVISOR_DEV(PALM, M515, UVISOR_FLAG_PALM4),
274         UVISOR_DEV(PALM, I705, UVISOR_FLAG_PALM4),
275         UVISOR_DEV(PALM, M125, UVISOR_FLAG_PALM4),
276         UVISOR_DEV(PALM, M130, UVISOR_FLAG_PALM4),
277         UVISOR_DEV(PALM, TUNGSTEN_Z, UVISOR_FLAG_PALM4),
278         UVISOR_DEV(PALM, TUNGSTEN_T, UVISOR_FLAG_PALM4),
279         UVISOR_DEV(PALM, ZIRE, UVISOR_FLAG_PALM4),
280         UVISOR_DEV(PALM, ZIRE31, UVISOR_FLAG_PALM4),
281         UVISOR_DEV(SAMSUNG, I500, UVISOR_FLAG_PALM4),
282         UVISOR_DEV(SONY, CLIE_40, 0),
283         UVISOR_DEV(SONY, CLIE_41, 0),
284         UVISOR_DEV(SONY, CLIE_S360, UVISOR_FLAG_PALM4),
285         UVISOR_DEV(SONY, CLIE_NX60, UVISOR_FLAG_PALM4),
286         UVISOR_DEV(SONY, CLIE_35, UVISOR_FLAG_PALM35),
287 /*  UVISOR_DEV(SONY, CLIE_25, UVISOR_FLAG_PALM4 ), */
288         UVISOR_DEV(SONY, CLIE_TJ37, UVISOR_FLAG_PALM4),
289 /*  UVISOR_DEV(SONY, CLIE_TH55, UVISOR_FLAG_PALM4 ), See PR 80935 */
290         UVISOR_DEV(TAPWAVE, ZODIAC, UVISOR_FLAG_PALM4),
291 #undef UVISOR_DEV
292 };
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         bcopy(uvisor_config, uvisor_config_copy,
321             sizeof(uvisor_config_copy));
322         device_set_usb_desc(dev);
323
324         mtx_init(&sc->sc_mtx, "uvisor", NULL, MTX_DEF);
325
326         sc->sc_udev = uaa->device;
327
328         /* configure the device */
329
330         sc->sc_flag = USB_GET_DRIVER_INFO(uaa);
331         sc->sc_iface_no = uaa->info.bIfaceNum;
332         sc->sc_iface_index = UVISOR_IFACE_INDEX;
333
334         error = uvisor_init(sc, uaa->device, uvisor_config_copy);
335
336         if (error) {
337                 DPRINTF("init failed, error=%s\n",
338                     usbd_errstr(error));
339                 goto detach;
340         }
341         error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
342             sc->sc_xfer, uvisor_config_copy, UVISOR_N_TRANSFER,
343             sc, &sc->sc_mtx);
344         if (error) {
345                 DPRINTF("could not allocate all pipes\n");
346                 goto detach;
347         }
348
349         error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
350             &uvisor_callback, &sc->sc_mtx);
351         if (error) {
352                 DPRINTF("ucom_attach failed\n");
353                 goto detach;
354         }
355         return (0);
356
357 detach:
358         uvisor_detach(dev);
359         return (ENXIO);
360 }
361
362 static int
363 uvisor_detach(device_t dev)
364 {
365         struct uvisor_softc *sc = device_get_softc(dev);
366
367         DPRINTF("sc=%p\n", sc);
368
369         ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
370         usbd_transfer_unsetup(sc->sc_xfer, UVISOR_N_TRANSFER);
371         mtx_destroy(&sc->sc_mtx);
372
373         return (0);
374 }
375
376 static usb_error_t
377 uvisor_init(struct uvisor_softc *sc, struct usb_device *udev, struct usb_config *config)
378 {
379         usb_error_t err = 0;
380         struct usb_device_request req;
381         struct uvisor_connection_info coninfo;
382         struct uvisor_palm_connection_info pconinfo;
383         uint16_t actlen;
384         uint8_t buffer[256];
385
386         if (sc->sc_flag & UVISOR_FLAG_VISOR) {
387                 DPRINTF("getting connection info\n");
388                 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
389                 req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
390                 USETW(req.wValue, 0);
391                 USETW(req.wIndex, 0);
392                 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
393                 err = usbd_do_request_flags(udev, NULL,
394                     &req, &coninfo, USB_SHORT_XFER_OK,
395                     &actlen, USB_DEFAULT_TIMEOUT);
396
397                 if (err) {
398                         goto done;
399                 }
400         }
401 #if USB_DEBUG
402         if (sc->sc_flag & UVISOR_FLAG_VISOR) {
403                 uint16_t i, np;
404                 const char *desc;
405
406                 np = UGETW(coninfo.num_ports);
407                 if (np > UVISOR_MAX_CONN) {
408                         np = UVISOR_MAX_CONN;
409                 }
410                 DPRINTF("Number of ports: %d\n", np);
411
412                 for (i = 0; i < np; ++i) {
413                         switch (coninfo.connections[i].port_function_id) {
414                         case UVISOR_FUNCTION_GENERIC:
415                                 desc = "Generic";
416                                 break;
417                         case UVISOR_FUNCTION_DEBUGGER:
418                                 desc = "Debugger";
419                                 break;
420                         case UVISOR_FUNCTION_HOTSYNC:
421                                 desc = "HotSync";
422                                 break;
423                         case UVISOR_FUNCTION_REMOTE_FILE_SYS:
424                                 desc = "Remote File System";
425                                 break;
426                         default:
427                                 desc = "unknown";
428                                 break;
429                         }
430                         DPRINTF("Port %d is for %s\n",
431                             coninfo.connections[i].port, desc);
432                 }
433         }
434 #endif
435
436         if (sc->sc_flag & UVISOR_FLAG_PALM4) {
437                 uint8_t port;
438
439                 /* Palm OS 4.0 Hack */
440                 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
441                 req.bRequest = UVISOR_GET_PALM_INFORMATION;
442                 USETW(req.wValue, 0);
443                 USETW(req.wIndex, 0);
444                 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
445
446                 err = usbd_do_request_flags
447                     (udev, NULL, &req, &pconinfo, USB_SHORT_XFER_OK,
448                     &actlen, USB_DEFAULT_TIMEOUT);
449
450                 if (err) {
451                         goto done;
452                 }
453                 if (actlen < 12) {
454                         DPRINTF("too little data\n");
455                         err = USB_ERR_INVAL;
456                         goto done;
457                 }
458                 if (pconinfo.endpoint_numbers_different) {
459                         port = pconinfo.connections[0].end_point_info;
460                         config[0].endpoint = (port & 0xF);      /* output */
461                         config[1].endpoint = (port >> 4);       /* input */
462                 } else {
463                         port = pconinfo.connections[0].port;
464                         config[0].endpoint = (port & 0xF);      /* output */
465                         config[1].endpoint = (port & 0xF);      /* input */
466                 }
467 #if 0
468                 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
469                 req.bRequest = UVISOR_GET_PALM_INFORMATION;
470                 USETW(req.wValue, 0);
471                 USETW(req.wIndex, 0);
472                 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
473                 err = usbd_do_request(udev, &req, buffer);
474                 if (err) {
475                         goto done;
476                 }
477 #endif
478         }
479         if (sc->sc_flag & UVISOR_FLAG_PALM35) {
480                 /* get the config number */
481                 DPRINTF("getting config info\n");
482                 req.bmRequestType = UT_READ;
483                 req.bRequest = UR_GET_CONFIG;
484                 USETW(req.wValue, 0);
485                 USETW(req.wIndex, 0);
486                 USETW(req.wLength, 1);
487
488                 err = usbd_do_request(udev, NULL, &req, buffer);
489                 if (err) {
490                         goto done;
491                 }
492                 /* get the interface number */
493                 DPRINTF("get the interface number\n");
494                 req.bmRequestType = UT_READ_DEVICE;
495                 req.bRequest = UR_GET_INTERFACE;
496                 USETW(req.wValue, 0);
497                 USETW(req.wIndex, 0);
498                 USETW(req.wLength, 1);
499                 err = usbd_do_request(udev, NULL, &req, buffer);
500                 if (err) {
501                         goto done;
502                 }
503         }
504 #if 0
505         uWord wAvail;
506
507         DPRINTF("getting available bytes\n");
508         req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
509         req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
510         USETW(req.wValue, 0);
511         USETW(req.wIndex, 5);
512         USETW(req.wLength, sizeof(wAvail));
513         err = usbd_do_request(udev, NULL, &req, &wAvail);
514         if (err) {
515                 goto done;
516         }
517         DPRINTF("avail=%d\n", UGETW(wAvail));
518 #endif
519
520         DPRINTF("done\n");
521 done:
522         return (err);
523 }
524
525 static void
526 uvisor_cfg_open(struct ucom_softc *ucom)
527 {
528         return;
529 }
530
531 static void
532 uvisor_cfg_close(struct ucom_softc *ucom)
533 {
534         struct uvisor_softc *sc = ucom->sc_parent;
535         uint8_t buffer[UVISOR_CONNECTION_INFO_SIZE];
536         struct usb_device_request req;
537         usb_error_t err;
538
539         req.bmRequestType = UT_READ_VENDOR_ENDPOINT;    /* XXX read? */
540         req.bRequest = UVISOR_CLOSE_NOTIFICATION;
541         USETW(req.wValue, 0);
542         USETW(req.wIndex, 0);
543         USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
544
545         err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
546             &req, buffer, 0, 1000);
547         if (err) {
548                 DPRINTFN(0, "close notification failed, error=%s\n",
549                     usbd_errstr(err));
550         }
551 }
552
553 static void
554 uvisor_start_read(struct ucom_softc *ucom)
555 {
556         struct uvisor_softc *sc = ucom->sc_parent;
557
558         usbd_transfer_start(sc->sc_xfer[UVISOR_BULK_DT_RD]);
559 }
560
561 static void
562 uvisor_stop_read(struct ucom_softc *ucom)
563 {
564         struct uvisor_softc *sc = ucom->sc_parent;
565
566         usbd_transfer_stop(sc->sc_xfer[UVISOR_BULK_DT_RD]);
567 }
568
569 static void
570 uvisor_start_write(struct ucom_softc *ucom)
571 {
572         struct uvisor_softc *sc = ucom->sc_parent;
573
574         usbd_transfer_start(sc->sc_xfer[UVISOR_BULK_DT_WR]);
575 }
576
577 static void
578 uvisor_stop_write(struct ucom_softc *ucom)
579 {
580         struct uvisor_softc *sc = ucom->sc_parent;
581
582         usbd_transfer_stop(sc->sc_xfer[UVISOR_BULK_DT_WR]);
583 }
584
585 static void
586 uvisor_write_callback(struct usb_xfer *xfer, usb_error_t error)
587 {
588         struct uvisor_softc *sc = usbd_xfer_softc(xfer);
589         struct usb_page_cache *pc;
590         uint32_t actlen;
591         uint8_t x;
592
593         switch (USB_GET_STATE(xfer)) {
594         case USB_ST_SETUP:
595         case USB_ST_TRANSFERRED:
596 tr_setup:
597                 for (x = 0; x != UVISOROFRAMES; x++) {
598
599                         usbd_xfer_set_frame_offset(xfer, 
600                             x * UVISOROBUFSIZE, x);
601
602                         pc = usbd_xfer_get_frame(xfer, x);
603                         if (ucom_get_data(&sc->sc_ucom, pc, 0,
604                             UVISOROBUFSIZE, &actlen)) {
605                                 usbd_xfer_set_frame_len(xfer, x, actlen);
606                         } else {
607                                 break;
608                         }
609                 }
610                 /* check for data */
611                 if (x != 0) {
612                         usbd_xfer_set_frames(xfer, x);
613                         usbd_transfer_submit(xfer);
614                 }
615                 break;
616
617         default:                        /* Error */
618                 if (error != USB_ERR_CANCELLED) {
619                         /* try to clear stall first */
620                         usbd_xfer_set_stall(xfer);
621                         goto tr_setup;
622                 }
623                 break;
624         }
625 }
626
627 static void
628 uvisor_read_callback(struct usb_xfer *xfer, usb_error_t error)
629 {
630         struct uvisor_softc *sc = usbd_xfer_softc(xfer);
631         struct usb_page_cache *pc;
632         int actlen;
633
634         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
635
636         switch (USB_GET_STATE(xfer)) {
637         case USB_ST_TRANSFERRED:
638                 pc = usbd_xfer_get_frame(xfer, 0);
639                 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
640
641         case USB_ST_SETUP:
642 tr_setup:
643                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
644                 usbd_transfer_submit(xfer);
645                 return;
646
647         default:                        /* Error */
648                 if (error != USB_ERR_CANCELLED) {
649                         /* try to clear stall first */
650                         usbd_xfer_set_stall(xfer);
651                         goto tr_setup;
652                 }
653                 return;
654         }
655 }