xref: /freebsd/sys/dev/usb/serial/umct.c (revision 396c556d77189a5c474d35cec6f44a762e310b7d)
1 #include <sys/cdefs.h>
2 __FBSDID("$FreeBSD$");
3 
4 /*-
5  * Copyright (c) 2003 Scott Long
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 
31 /*
32  * Driver for the MCT (Magic Control Technology) USB-RS232 Converter.
33  * Based on the superb documentation from the linux mct_u232 driver by
34  * Wolfgang Grandeggar <wolfgang@cec.ch>.
35  * This device smells a lot like the Belkin F5U103, except that it has
36  * suffered some mild brain-damage.  This driver is based off of the ubsa.c
37  * driver from Alexander Kabaev <kan@FreeBSD.org>.  Merging the two together
38  * might be useful, though the subtle differences might lead to lots of
39  * #ifdef's.
40  */
41 
42 /*
43  * NOTE: all function names beginning like "umct_cfg_" can only
44  * be called from within the config thread function !
45  */
46 
47 #include <sys/stdint.h>
48 #include <sys/stddef.h>
49 #include <sys/param.h>
50 #include <sys/queue.h>
51 #include <sys/types.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/bus.h>
55 #include <sys/module.h>
56 #include <sys/lock.h>
57 #include <sys/mutex.h>
58 #include <sys/condvar.h>
59 #include <sys/sysctl.h>
60 #include <sys/sx.h>
61 #include <sys/unistd.h>
62 #include <sys/callout.h>
63 #include <sys/malloc.h>
64 #include <sys/priv.h>
65 
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbdi.h>
68 #include <dev/usb/usbdi_util.h>
69 #include "usbdevs.h"
70 
71 #define	USB_DEBUG_VAR usb_debug
72 #include <dev/usb/usb_debug.h>
73 #include <dev/usb/usb_process.h>
74 
75 #include <dev/usb/serial/usb_serial.h>
76 
77 /* The UMCT advertises the standard 8250 UART registers */
78 #define	UMCT_GET_MSR		2	/* Get Modem Status Register */
79 #define	UMCT_GET_MSR_SIZE	1
80 #define	UMCT_GET_LCR		6	/* Get Line Control Register */
81 #define	UMCT_GET_LCR_SIZE	1
82 #define	UMCT_SET_BAUD		5	/* Set the Baud Rate Divisor */
83 #define	UMCT_SET_BAUD_SIZE	4
84 #define	UMCT_SET_LCR		7	/* Set Line Control Register */
85 #define	UMCT_SET_LCR_SIZE	1
86 #define	UMCT_SET_MCR		10	/* Set Modem Control Register */
87 #define	UMCT_SET_MCR_SIZE	1
88 
89 #define	UMCT_MSR_CTS_CHG	0x01
90 #define	UMCT_MSR_DSR_CHG	0x02
91 #define	UMCT_MSR_RI_CHG		0x04
92 #define	UMCT_MSR_CD_CHG		0x08
93 #define	UMCT_MSR_CTS		0x10
94 #define	UMCT_MSR_RTS		0x20
95 #define	UMCT_MSR_RI		0x40
96 #define	UMCT_MSR_CD		0x80
97 
98 #define	UMCT_INTR_INTERVAL	100
99 #define	UMCT_IFACE_INDEX	0
100 #define	UMCT_CONFIG_INDEX	0
101 
102 enum {
103 	UMCT_BULK_DT_WR,
104 	UMCT_BULK_DT_RD,
105 	UMCT_INTR_DT_RD,
106 	UMCT_N_TRANSFER,
107 };
108 
109 struct umct_softc {
110 	struct ucom_super_softc sc_super_ucom;
111 	struct ucom_softc sc_ucom;
112 
113 	struct usb_device *sc_udev;
114 	struct usb_xfer *sc_xfer[UMCT_N_TRANSFER];
115 	struct mtx sc_mtx;
116 
117 	uint32_t sc_unit;
118 
119 	uint16_t sc_obufsize;
120 
121 	uint8_t	sc_lsr;
122 	uint8_t	sc_msr;
123 	uint8_t	sc_lcr;
124 	uint8_t	sc_mcr;
125 	uint8_t	sc_iface_no;
126 	uint8_t sc_swap_cb;
127 };
128 
129 /* prototypes */
130 
131 static device_probe_t umct_probe;
132 static device_attach_t umct_attach;
133 static device_detach_t umct_detach;
134 static void umct_free_softc(struct umct_softc *);
135 
136 static usb_callback_t umct_intr_callback;
137 static usb_callback_t umct_intr_callback_sub;
138 static usb_callback_t umct_read_callback;
139 static usb_callback_t umct_read_callback_sub;
140 static usb_callback_t umct_write_callback;
141 
142 static void	umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
143 		    uint16_t len, uint32_t value);
144 static void	umct_free(struct ucom_softc *);
145 static void	umct_cfg_get_status(struct ucom_softc *, uint8_t *,
146 		    uint8_t *);
147 static void	umct_cfg_set_break(struct ucom_softc *, uint8_t);
148 static void	umct_cfg_set_dtr(struct ucom_softc *, uint8_t);
149 static void	umct_cfg_set_rts(struct ucom_softc *, uint8_t);
150 static uint8_t	umct_calc_baud(uint32_t);
151 static int	umct_pre_param(struct ucom_softc *, struct termios *);
152 static void	umct_cfg_param(struct ucom_softc *, struct termios *);
153 static void	umct_start_read(struct ucom_softc *);
154 static void	umct_stop_read(struct ucom_softc *);
155 static void	umct_start_write(struct ucom_softc *);
156 static void	umct_stop_write(struct ucom_softc *);
157 static void	umct_poll(struct ucom_softc *ucom);
158 
159 static const struct usb_config umct_config[UMCT_N_TRANSFER] = {
160 
161 	[UMCT_BULK_DT_WR] = {
162 		.type = UE_BULK,
163 		.endpoint = UE_ADDR_ANY,
164 		.direction = UE_DIR_OUT,
165 		.bufsize = 0,	/* use wMaxPacketSize */
166 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
167 		.callback = &umct_write_callback,
168 	},
169 
170 	[UMCT_BULK_DT_RD] = {
171 		.type = UE_INTERRUPT,
172 		.endpoint = UE_ADDR_ANY,
173 		.direction = UE_DIR_IN,
174 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
175 		.bufsize = 0,	/* use wMaxPacketSize */
176 		.callback = &umct_read_callback,
177 		.ep_index = 0,		/* first interrupt endpoint */
178 	},
179 
180 	[UMCT_INTR_DT_RD] = {
181 		.type = UE_INTERRUPT,
182 		.endpoint = UE_ADDR_ANY,
183 		.direction = UE_DIR_IN,
184 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
185 		.bufsize = 0,	/* use wMaxPacketSize */
186 		.callback = &umct_intr_callback,
187 		.ep_index = 1,		/* second interrupt endpoint */
188 	},
189 };
190 
191 static const struct ucom_callback umct_callback = {
192 	.ucom_cfg_get_status = &umct_cfg_get_status,
193 	.ucom_cfg_set_dtr = &umct_cfg_set_dtr,
194 	.ucom_cfg_set_rts = &umct_cfg_set_rts,
195 	.ucom_cfg_set_break = &umct_cfg_set_break,
196 	.ucom_cfg_param = &umct_cfg_param,
197 	.ucom_pre_param = &umct_pre_param,
198 	.ucom_start_read = &umct_start_read,
199 	.ucom_stop_read = &umct_stop_read,
200 	.ucom_start_write = &umct_start_write,
201 	.ucom_stop_write = &umct_stop_write,
202 	.ucom_poll = &umct_poll,
203 	.ucom_free = &umct_free,
204 };
205 
206 static const STRUCT_USB_HOST_ID umct_devs[] = {
207 	{USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232, 0)},
208 	{USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232, 0)},
209 	{USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232, 0)},
210 	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U109, 0)},
211 	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U409, 0)},
212 };
213 
214 static device_method_t umct_methods[] = {
215 	DEVMETHOD(device_probe, umct_probe),
216 	DEVMETHOD(device_attach, umct_attach),
217 	DEVMETHOD(device_detach, umct_detach),
218 	DEVMETHOD_END
219 };
220 
221 static devclass_t umct_devclass;
222 
223 static driver_t umct_driver = {
224 	.name = "umct",
225 	.methods = umct_methods,
226 	.size = sizeof(struct umct_softc),
227 };
228 
229 DRIVER_MODULE(umct, uhub, umct_driver, umct_devclass, NULL, 0);
230 MODULE_DEPEND(umct, ucom, 1, 1, 1);
231 MODULE_DEPEND(umct, usb, 1, 1, 1);
232 MODULE_VERSION(umct, 1);
233 USB_PNP_HOST_INFO(umct_devs);
234 
235 static int
236 umct_probe(device_t dev)
237 {
238 	struct usb_attach_arg *uaa = device_get_ivars(dev);
239 
240 	if (uaa->usb_mode != USB_MODE_HOST) {
241 		return (ENXIO);
242 	}
243 	if (uaa->info.bConfigIndex != UMCT_CONFIG_INDEX) {
244 		return (ENXIO);
245 	}
246 	if (uaa->info.bIfaceIndex != UMCT_IFACE_INDEX) {
247 		return (ENXIO);
248 	}
249 	return (usbd_lookup_id_by_uaa(umct_devs, sizeof(umct_devs), uaa));
250 }
251 
252 static int
253 umct_attach(device_t dev)
254 {
255 	struct usb_attach_arg *uaa = device_get_ivars(dev);
256 	struct umct_softc *sc = device_get_softc(dev);
257 	int32_t error;
258 	uint16_t maxp;
259 	uint8_t iface_index;
260 
261 	sc->sc_udev = uaa->device;
262 	sc->sc_unit = device_get_unit(dev);
263 
264 	device_set_usb_desc(dev);
265 	mtx_init(&sc->sc_mtx, "umct", NULL, MTX_DEF);
266 	ucom_ref(&sc->sc_super_ucom);
267 
268 	sc->sc_iface_no = uaa->info.bIfaceNum;
269 
270 	iface_index = UMCT_IFACE_INDEX;
271 	error = usbd_transfer_setup(uaa->device, &iface_index,
272 	    sc->sc_xfer, umct_config, UMCT_N_TRANSFER, sc, &sc->sc_mtx);
273 
274 	if (error) {
275 		device_printf(dev, "allocating USB "
276 		    "transfers failed\n");
277 		goto detach;
278 	}
279 
280 	/*
281 	 * The real bulk-in endpoint is also marked as an interrupt.
282 	 * The only way to differentiate it from the real interrupt
283 	 * endpoint is to look at the wMaxPacketSize field.
284 	 */
285 	maxp = usbd_xfer_max_framelen(sc->sc_xfer[UMCT_BULK_DT_RD]);
286 	if (maxp == 0x2) {
287 
288 		/* guessed wrong - switch around endpoints */
289 
290 		struct usb_xfer *temp = sc->sc_xfer[UMCT_INTR_DT_RD];
291 
292 		sc->sc_xfer[UMCT_INTR_DT_RD] = sc->sc_xfer[UMCT_BULK_DT_RD];
293 		sc->sc_xfer[UMCT_BULK_DT_RD] = temp;
294 		sc->sc_swap_cb = 1;
295 	}
296 
297 	sc->sc_obufsize = usbd_xfer_max_len(sc->sc_xfer[UMCT_BULK_DT_WR]);
298 
299 	if (uaa->info.idProduct == USB_PRODUCT_MCT_SITECOM_USB232) {
300 		if (sc->sc_obufsize > 16) {
301 			sc->sc_obufsize = 16;
302 		}
303 	}
304 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
305 	    &umct_callback, &sc->sc_mtx);
306 	if (error) {
307 		goto detach;
308 	}
309 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
310 
311 	return (0);			/* success */
312 
313 detach:
314 	umct_detach(dev);
315 	return (ENXIO);			/* failure */
316 }
317 
318 static int
319 umct_detach(device_t dev)
320 {
321 	struct umct_softc *sc = device_get_softc(dev);
322 
323 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
324 	usbd_transfer_unsetup(sc->sc_xfer, UMCT_N_TRANSFER);
325 
326 	device_claim_softc(dev);
327 
328 	umct_free_softc(sc);
329 
330 	return (0);
331 }
332 
333 UCOM_UNLOAD_DRAIN(umct);
334 
335 static void
336 umct_free_softc(struct umct_softc *sc)
337 {
338 	if (ucom_unref(&sc->sc_super_ucom)) {
339 		mtx_destroy(&sc->sc_mtx);
340 		device_free_softc(sc);
341 	}
342 }
343 
344 static void
345 umct_free(struct ucom_softc *ucom)
346 {
347 	umct_free_softc(ucom->sc_parent);
348 }
349 
350 static void
351 umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
352     uint16_t len, uint32_t value)
353 {
354 	struct usb_device_request req;
355 	usb_error_t err;
356 	uint8_t temp[4];
357 
358 	if (len > 4)
359 		len = 4;
360 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
361 	req.bRequest = request;
362 	USETW(req.wValue, 0);
363 	req.wIndex[0] = sc->sc_iface_no;
364 	req.wIndex[1] = 0;
365 	USETW(req.wLength, len);
366 	USETDW(temp, value);
367 
368 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
369 	    &req, temp, 0, 1000);
370 	if (err) {
371 		DPRINTFN(0, "device request failed, err=%s "
372 		    "(ignored)\n", usbd_errstr(err));
373 	}
374 	return;
375 }
376 
377 static void
378 umct_intr_callback_sub(struct usb_xfer *xfer, usb_error_t error)
379 {
380 	struct umct_softc *sc = usbd_xfer_softc(xfer);
381 	struct usb_page_cache *pc;
382 	uint8_t buf[2];
383 	int actlen;
384 
385 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
386 
387 	switch (USB_GET_STATE(xfer)) {
388 	case USB_ST_TRANSFERRED:
389 		if (actlen < 2) {
390 			DPRINTF("too short message\n");
391 			goto tr_setup;
392 		}
393 		pc = usbd_xfer_get_frame(xfer, 0);
394 		usbd_copy_out(pc, 0, buf, sizeof(buf));
395 
396 		/*
397 		 * MSR bits need translation from ns16550 to SER_* values.
398 		 * LSR bits are ns16550 in hardware and ucom.
399 		 */
400 		sc->sc_msr = 0;
401 		if (buf[0] & UMCT_MSR_CTS)
402 			sc->sc_msr |= SER_CTS;
403 		if (buf[0] & UMCT_MSR_CD)
404 			sc->sc_msr |= SER_DCD;
405 		if (buf[0] & UMCT_MSR_RI)
406 			sc->sc_msr |= SER_RI;
407 		if (buf[0] & UMCT_MSR_RTS)
408 			sc->sc_msr |= SER_DSR;
409 		sc->sc_lsr = buf[1];
410 
411 		ucom_status_change(&sc->sc_ucom);
412 		/* FALLTHROUGH */
413 	case USB_ST_SETUP:
414 tr_setup:
415 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
416 		usbd_transfer_submit(xfer);
417 		return;
418 
419 	default:			/* Error */
420 		if (error != USB_ERR_CANCELLED) {
421 			/* try to clear stall first */
422 			usbd_xfer_set_stall(xfer);
423 			goto tr_setup;
424 		}
425 		return;
426 	}
427 }
428 
429 static void
430 umct_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
431 {
432 	struct umct_softc *sc = ucom->sc_parent;
433 
434 	*lsr = sc->sc_lsr;
435 	*msr = sc->sc_msr;
436 }
437 
438 static void
439 umct_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
440 {
441 	struct umct_softc *sc = ucom->sc_parent;
442 
443 	if (onoff)
444 		sc->sc_lcr |= 0x40;
445 	else
446 		sc->sc_lcr &= ~0x40;
447 
448 	umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr);
449 }
450 
451 static void
452 umct_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
453 {
454 	struct umct_softc *sc = ucom->sc_parent;
455 
456 	if (onoff)
457 		sc->sc_mcr |= 0x01;
458 	else
459 		sc->sc_mcr &= ~0x01;
460 
461 	umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
462 }
463 
464 static void
465 umct_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
466 {
467 	struct umct_softc *sc = ucom->sc_parent;
468 
469 	if (onoff)
470 		sc->sc_mcr |= 0x02;
471 	else
472 		sc->sc_mcr &= ~0x02;
473 
474 	umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
475 }
476 
477 static uint8_t
478 umct_calc_baud(uint32_t baud)
479 {
480 	switch (baud) {
481 		case B300:return (0x1);
482 	case B600:
483 		return (0x2);
484 	case B1200:
485 		return (0x3);
486 	case B2400:
487 		return (0x4);
488 	case B4800:
489 		return (0x6);
490 	case B9600:
491 		return (0x8);
492 	case B19200:
493 		return (0x9);
494 	case B38400:
495 		return (0xa);
496 	case B57600:
497 		return (0xb);
498 	case 115200:
499 		return (0xc);
500 	case B0:
501 	default:
502 		break;
503 	}
504 	return (0x0);
505 }
506 
507 static int
508 umct_pre_param(struct ucom_softc *ucom, struct termios *t)
509 {
510 	return (0);			/* we accept anything */
511 }
512 
513 static void
514 umct_cfg_param(struct ucom_softc *ucom, struct termios *t)
515 {
516 	struct umct_softc *sc = ucom->sc_parent;
517 	uint32_t value;
518 
519 	value = umct_calc_baud(t->c_ospeed);
520 	umct_cfg_do_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value);
521 
522 	value = (sc->sc_lcr & 0x40);
523 
524 	switch (t->c_cflag & CSIZE) {
525 	case CS5:
526 		value |= 0x0;
527 		break;
528 	case CS6:
529 		value |= 0x1;
530 		break;
531 	case CS7:
532 		value |= 0x2;
533 		break;
534 	default:
535 	case CS8:
536 		value |= 0x3;
537 		break;
538 	}
539 
540 	value |= (t->c_cflag & CSTOPB) ? 0x4 : 0;
541 	if (t->c_cflag & PARENB) {
542 		value |= 0x8;
543 		value |= (t->c_cflag & PARODD) ? 0x0 : 0x10;
544 	}
545 	/*
546 	 * XXX There doesn't seem to be a way to tell the device
547 	 * to use flow control.
548 	 */
549 
550 	sc->sc_lcr = value;
551 	umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value);
552 }
553 
554 static void
555 umct_start_read(struct ucom_softc *ucom)
556 {
557 	struct umct_softc *sc = ucom->sc_parent;
558 
559 	/* start interrupt endpoint */
560 	usbd_transfer_start(sc->sc_xfer[UMCT_INTR_DT_RD]);
561 
562 	/* start read endpoint */
563 	usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_RD]);
564 }
565 
566 static void
567 umct_stop_read(struct ucom_softc *ucom)
568 {
569 	struct umct_softc *sc = ucom->sc_parent;
570 
571 	/* stop interrupt endpoint */
572 	usbd_transfer_stop(sc->sc_xfer[UMCT_INTR_DT_RD]);
573 
574 	/* stop read endpoint */
575 	usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_RD]);
576 }
577 
578 static void
579 umct_start_write(struct ucom_softc *ucom)
580 {
581 	struct umct_softc *sc = ucom->sc_parent;
582 
583 	usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_WR]);
584 }
585 
586 static void
587 umct_stop_write(struct ucom_softc *ucom)
588 {
589 	struct umct_softc *sc = ucom->sc_parent;
590 
591 	usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_WR]);
592 }
593 
594 static void
595 umct_read_callback(struct usb_xfer *xfer, usb_error_t error)
596 {
597 	struct umct_softc *sc = usbd_xfer_softc(xfer);
598 
599 	if (sc->sc_swap_cb)
600 		umct_intr_callback_sub(xfer, error);
601 	else
602 		umct_read_callback_sub(xfer, error);
603 }
604 
605 static void
606 umct_intr_callback(struct usb_xfer *xfer, usb_error_t error)
607 {
608 	struct umct_softc *sc = usbd_xfer_softc(xfer);
609 
610 	if (sc->sc_swap_cb)
611 		umct_read_callback_sub(xfer, error);
612 	else
613 		umct_intr_callback_sub(xfer, error);
614 }
615 
616 static void
617 umct_write_callback(struct usb_xfer *xfer, usb_error_t error)
618 {
619 	struct umct_softc *sc = usbd_xfer_softc(xfer);
620 	struct usb_page_cache *pc;
621 	uint32_t actlen;
622 
623 	switch (USB_GET_STATE(xfer)) {
624 	case USB_ST_SETUP:
625 	case USB_ST_TRANSFERRED:
626 tr_setup:
627 		pc = usbd_xfer_get_frame(xfer, 0);
628 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
629 		    sc->sc_obufsize, &actlen)) {
630 
631 			usbd_xfer_set_frame_len(xfer, 0, actlen);
632 			usbd_transfer_submit(xfer);
633 		}
634 		return;
635 
636 	default:			/* Error */
637 		if (error != USB_ERR_CANCELLED) {
638 			/* try to clear stall first */
639 			usbd_xfer_set_stall(xfer);
640 			goto tr_setup;
641 		}
642 		return;
643 	}
644 }
645 
646 static void
647 umct_read_callback_sub(struct usb_xfer *xfer, usb_error_t error)
648 {
649 	struct umct_softc *sc = usbd_xfer_softc(xfer);
650 	struct usb_page_cache *pc;
651 	int actlen;
652 
653 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
654 
655 	switch (USB_GET_STATE(xfer)) {
656 	case USB_ST_TRANSFERRED:
657 		pc = usbd_xfer_get_frame(xfer, 0);
658 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
659 
660 	case USB_ST_SETUP:
661 tr_setup:
662 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
663 		usbd_transfer_submit(xfer);
664 		return;
665 
666 	default:			/* Error */
667 		if (error != USB_ERR_CANCELLED) {
668 			/* try to clear stall first */
669 			usbd_xfer_set_stall(xfer);
670 			goto tr_setup;
671 		}
672 		return;
673 	}
674 }
675 
676 static void
677 umct_poll(struct ucom_softc *ucom)
678 {
679 	struct umct_softc *sc = ucom->sc_parent;
680 	usbd_transfer_poll(sc->sc_xfer, UMCT_N_TRANSFER);
681 }
682