xref: /freebsd/sys/dev/usb/serial/umct.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 #include <sys/cdefs.h>
2 /*-
3  * SPDX-License-Identifier: BSD-2-Clause
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 	[UMCT_BULK_DT_WR] = {
161 		.type = UE_BULK,
162 		.endpoint = UE_ADDR_ANY,
163 		.direction = UE_DIR_OUT,
164 		.bufsize = 0,	/* use wMaxPacketSize */
165 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
166 		.callback = &umct_write_callback,
167 	},
168 
169 	[UMCT_BULK_DT_RD] = {
170 		.type = UE_INTERRUPT,
171 		.endpoint = UE_ADDR_ANY,
172 		.direction = UE_DIR_IN,
173 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
174 		.bufsize = 0,	/* use wMaxPacketSize */
175 		.callback = &umct_read_callback,
176 		.ep_index = 0,		/* first interrupt endpoint */
177 	},
178 
179 	[UMCT_INTR_DT_RD] = {
180 		.type = UE_INTERRUPT,
181 		.endpoint = UE_ADDR_ANY,
182 		.direction = UE_DIR_IN,
183 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
184 		.bufsize = 0,	/* use wMaxPacketSize */
185 		.callback = &umct_intr_callback,
186 		.ep_index = 1,		/* second interrupt endpoint */
187 	},
188 };
189 
190 static const struct ucom_callback umct_callback = {
191 	.ucom_cfg_get_status = &umct_cfg_get_status,
192 	.ucom_cfg_set_dtr = &umct_cfg_set_dtr,
193 	.ucom_cfg_set_rts = &umct_cfg_set_rts,
194 	.ucom_cfg_set_break = &umct_cfg_set_break,
195 	.ucom_cfg_param = &umct_cfg_param,
196 	.ucom_pre_param = &umct_pre_param,
197 	.ucom_start_read = &umct_start_read,
198 	.ucom_stop_read = &umct_stop_read,
199 	.ucom_start_write = &umct_start_write,
200 	.ucom_stop_write = &umct_stop_write,
201 	.ucom_poll = &umct_poll,
202 	.ucom_free = &umct_free,
203 };
204 
205 static const STRUCT_USB_HOST_ID umct_devs[] = {
206 	{USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232, 0)},
207 	{USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232, 0)},
208 	{USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232, 0)},
209 	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U109, 0)},
210 	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U409, 0)},
211 };
212 
213 static device_method_t umct_methods[] = {
214 	DEVMETHOD(device_probe, umct_probe),
215 	DEVMETHOD(device_attach, umct_attach),
216 	DEVMETHOD(device_detach, umct_detach),
217 	DEVMETHOD_END
218 };
219 
220 static driver_t umct_driver = {
221 	.name = "umct",
222 	.methods = umct_methods,
223 	.size = sizeof(struct umct_softc),
224 };
225 
226 DRIVER_MODULE(umct, uhub, umct_driver, NULL, NULL);
227 MODULE_DEPEND(umct, ucom, 1, 1, 1);
228 MODULE_DEPEND(umct, usb, 1, 1, 1);
229 MODULE_VERSION(umct, 1);
230 USB_PNP_HOST_INFO(umct_devs);
231 
232 static int
233 umct_probe(device_t dev)
234 {
235 	struct usb_attach_arg *uaa = device_get_ivars(dev);
236 
237 	if (uaa->usb_mode != USB_MODE_HOST) {
238 		return (ENXIO);
239 	}
240 	if (uaa->info.bConfigIndex != UMCT_CONFIG_INDEX) {
241 		return (ENXIO);
242 	}
243 	if (uaa->info.bIfaceIndex != UMCT_IFACE_INDEX) {
244 		return (ENXIO);
245 	}
246 	return (usbd_lookup_id_by_uaa(umct_devs, sizeof(umct_devs), uaa));
247 }
248 
249 static int
250 umct_attach(device_t dev)
251 {
252 	struct usb_attach_arg *uaa = device_get_ivars(dev);
253 	struct umct_softc *sc = device_get_softc(dev);
254 	int32_t error;
255 	uint16_t maxp;
256 	uint8_t iface_index;
257 
258 	sc->sc_udev = uaa->device;
259 	sc->sc_unit = device_get_unit(dev);
260 
261 	device_set_usb_desc(dev);
262 	mtx_init(&sc->sc_mtx, "umct", NULL, MTX_DEF);
263 	ucom_ref(&sc->sc_super_ucom);
264 
265 	sc->sc_iface_no = uaa->info.bIfaceNum;
266 
267 	iface_index = UMCT_IFACE_INDEX;
268 	error = usbd_transfer_setup(uaa->device, &iface_index,
269 	    sc->sc_xfer, umct_config, UMCT_N_TRANSFER, sc, &sc->sc_mtx);
270 
271 	if (error) {
272 		device_printf(dev, "allocating USB "
273 		    "transfers failed\n");
274 		goto detach;
275 	}
276 
277 	/*
278 	 * The real bulk-in endpoint is also marked as an interrupt.
279 	 * The only way to differentiate it from the real interrupt
280 	 * endpoint is to look at the wMaxPacketSize field.
281 	 */
282 	maxp = usbd_xfer_max_framelen(sc->sc_xfer[UMCT_BULK_DT_RD]);
283 	if (maxp == 0x2) {
284 		/* guessed wrong - switch around endpoints */
285 
286 		struct usb_xfer *temp = sc->sc_xfer[UMCT_INTR_DT_RD];
287 
288 		sc->sc_xfer[UMCT_INTR_DT_RD] = sc->sc_xfer[UMCT_BULK_DT_RD];
289 		sc->sc_xfer[UMCT_BULK_DT_RD] = temp;
290 		sc->sc_swap_cb = 1;
291 	}
292 
293 	sc->sc_obufsize = usbd_xfer_max_len(sc->sc_xfer[UMCT_BULK_DT_WR]);
294 
295 	if (uaa->info.idProduct == USB_PRODUCT_MCT_SITECOM_USB232) {
296 		if (sc->sc_obufsize > 16) {
297 			sc->sc_obufsize = 16;
298 		}
299 	}
300 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
301 	    &umct_callback, &sc->sc_mtx);
302 	if (error) {
303 		goto detach;
304 	}
305 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
306 
307 	return (0);			/* success */
308 
309 detach:
310 	umct_detach(dev);
311 	return (ENXIO);			/* failure */
312 }
313 
314 static int
315 umct_detach(device_t dev)
316 {
317 	struct umct_softc *sc = device_get_softc(dev);
318 
319 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
320 	usbd_transfer_unsetup(sc->sc_xfer, UMCT_N_TRANSFER);
321 
322 	device_claim_softc(dev);
323 
324 	umct_free_softc(sc);
325 
326 	return (0);
327 }
328 
329 UCOM_UNLOAD_DRAIN(umct);
330 
331 static void
332 umct_free_softc(struct umct_softc *sc)
333 {
334 	if (ucom_unref(&sc->sc_super_ucom)) {
335 		mtx_destroy(&sc->sc_mtx);
336 		device_free_softc(sc);
337 	}
338 }
339 
340 static void
341 umct_free(struct ucom_softc *ucom)
342 {
343 	umct_free_softc(ucom->sc_parent);
344 }
345 
346 static void
347 umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
348     uint16_t len, uint32_t value)
349 {
350 	struct usb_device_request req;
351 	usb_error_t err;
352 	uint8_t temp[4];
353 
354 	if (len > 4)
355 		len = 4;
356 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
357 	req.bRequest = request;
358 	USETW(req.wValue, 0);
359 	req.wIndex[0] = sc->sc_iface_no;
360 	req.wIndex[1] = 0;
361 	USETW(req.wLength, len);
362 	USETDW(temp, value);
363 
364 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
365 	    &req, temp, 0, 1000);
366 	if (err) {
367 		DPRINTFN(0, "device request failed, err=%s "
368 		    "(ignored)\n", usbd_errstr(err));
369 	}
370 	return;
371 }
372 
373 static void
374 umct_intr_callback_sub(struct usb_xfer *xfer, usb_error_t error)
375 {
376 	struct umct_softc *sc = usbd_xfer_softc(xfer);
377 	struct usb_page_cache *pc;
378 	uint8_t buf[2];
379 	int actlen;
380 
381 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
382 
383 	switch (USB_GET_STATE(xfer)) {
384 	case USB_ST_TRANSFERRED:
385 		if (actlen < 2) {
386 			DPRINTF("too short message\n");
387 			goto tr_setup;
388 		}
389 		pc = usbd_xfer_get_frame(xfer, 0);
390 		usbd_copy_out(pc, 0, buf, sizeof(buf));
391 
392 		/*
393 		 * MSR bits need translation from ns16550 to SER_* values.
394 		 * LSR bits are ns16550 in hardware and ucom.
395 		 */
396 		sc->sc_msr = 0;
397 		if (buf[0] & UMCT_MSR_CTS)
398 			sc->sc_msr |= SER_CTS;
399 		if (buf[0] & UMCT_MSR_CD)
400 			sc->sc_msr |= SER_DCD;
401 		if (buf[0] & UMCT_MSR_RI)
402 			sc->sc_msr |= SER_RI;
403 		if (buf[0] & UMCT_MSR_RTS)
404 			sc->sc_msr |= SER_DSR;
405 		sc->sc_lsr = buf[1];
406 
407 		ucom_status_change(&sc->sc_ucom);
408 		/* FALLTHROUGH */
409 	case USB_ST_SETUP:
410 tr_setup:
411 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
412 		usbd_transfer_submit(xfer);
413 		return;
414 
415 	default:			/* Error */
416 		if (error != USB_ERR_CANCELLED) {
417 			/* try to clear stall first */
418 			usbd_xfer_set_stall(xfer);
419 			goto tr_setup;
420 		}
421 		return;
422 	}
423 }
424 
425 static void
426 umct_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
427 {
428 	struct umct_softc *sc = ucom->sc_parent;
429 
430 	*lsr = sc->sc_lsr;
431 	*msr = sc->sc_msr;
432 }
433 
434 static void
435 umct_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
436 {
437 	struct umct_softc *sc = ucom->sc_parent;
438 
439 	if (onoff)
440 		sc->sc_lcr |= 0x40;
441 	else
442 		sc->sc_lcr &= ~0x40;
443 
444 	umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr);
445 }
446 
447 static void
448 umct_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
449 {
450 	struct umct_softc *sc = ucom->sc_parent;
451 
452 	if (onoff)
453 		sc->sc_mcr |= 0x01;
454 	else
455 		sc->sc_mcr &= ~0x01;
456 
457 	umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
458 }
459 
460 static void
461 umct_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
462 {
463 	struct umct_softc *sc = ucom->sc_parent;
464 
465 	if (onoff)
466 		sc->sc_mcr |= 0x02;
467 	else
468 		sc->sc_mcr &= ~0x02;
469 
470 	umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
471 }
472 
473 static uint8_t
474 umct_calc_baud(uint32_t baud)
475 {
476 	switch (baud) {
477 	case B300:
478 		return (0x1);
479 	case B600:
480 		return (0x2);
481 	case B1200:
482 		return (0x3);
483 	case B2400:
484 		return (0x4);
485 	case B4800:
486 		return (0x6);
487 	case B9600:
488 		return (0x8);
489 	case B19200:
490 		return (0x9);
491 	case B38400:
492 		return (0xa);
493 	case B57600:
494 		return (0xb);
495 	case 115200:
496 		return (0xc);
497 	case B0:
498 	default:
499 		break;
500 	}
501 	return (0x0);
502 }
503 
504 static int
505 umct_pre_param(struct ucom_softc *ucom, struct termios *t)
506 {
507 	return (0);			/* we accept anything */
508 }
509 
510 static void
511 umct_cfg_param(struct ucom_softc *ucom, struct termios *t)
512 {
513 	struct umct_softc *sc = ucom->sc_parent;
514 	uint32_t value;
515 
516 	value = umct_calc_baud(t->c_ospeed);
517 	umct_cfg_do_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value);
518 
519 	value = (sc->sc_lcr & 0x40);
520 
521 	switch (t->c_cflag & CSIZE) {
522 	case CS5:
523 		value |= 0x0;
524 		break;
525 	case CS6:
526 		value |= 0x1;
527 		break;
528 	case CS7:
529 		value |= 0x2;
530 		break;
531 	default:
532 	case CS8:
533 		value |= 0x3;
534 		break;
535 	}
536 
537 	value |= (t->c_cflag & CSTOPB) ? 0x4 : 0;
538 	if (t->c_cflag & PARENB) {
539 		value |= 0x8;
540 		value |= (t->c_cflag & PARODD) ? 0x0 : 0x10;
541 	}
542 	/*
543 	 * XXX There doesn't seem to be a way to tell the device
544 	 * to use flow control.
545 	 */
546 
547 	sc->sc_lcr = value;
548 	umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value);
549 }
550 
551 static void
552 umct_start_read(struct ucom_softc *ucom)
553 {
554 	struct umct_softc *sc = ucom->sc_parent;
555 
556 	/* start interrupt endpoint */
557 	usbd_transfer_start(sc->sc_xfer[UMCT_INTR_DT_RD]);
558 
559 	/* start read endpoint */
560 	usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_RD]);
561 }
562 
563 static void
564 umct_stop_read(struct ucom_softc *ucom)
565 {
566 	struct umct_softc *sc = ucom->sc_parent;
567 
568 	/* stop interrupt endpoint */
569 	usbd_transfer_stop(sc->sc_xfer[UMCT_INTR_DT_RD]);
570 
571 	/* stop read endpoint */
572 	usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_RD]);
573 }
574 
575 static void
576 umct_start_write(struct ucom_softc *ucom)
577 {
578 	struct umct_softc *sc = ucom->sc_parent;
579 
580 	usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_WR]);
581 }
582 
583 static void
584 umct_stop_write(struct ucom_softc *ucom)
585 {
586 	struct umct_softc *sc = ucom->sc_parent;
587 
588 	usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_WR]);
589 }
590 
591 static void
592 umct_read_callback(struct usb_xfer *xfer, usb_error_t error)
593 {
594 	struct umct_softc *sc = usbd_xfer_softc(xfer);
595 
596 	if (sc->sc_swap_cb)
597 		umct_intr_callback_sub(xfer, error);
598 	else
599 		umct_read_callback_sub(xfer, error);
600 }
601 
602 static void
603 umct_intr_callback(struct usb_xfer *xfer, usb_error_t error)
604 {
605 	struct umct_softc *sc = usbd_xfer_softc(xfer);
606 
607 	if (sc->sc_swap_cb)
608 		umct_read_callback_sub(xfer, error);
609 	else
610 		umct_intr_callback_sub(xfer, error);
611 }
612 
613 static void
614 umct_write_callback(struct usb_xfer *xfer, usb_error_t error)
615 {
616 	struct umct_softc *sc = usbd_xfer_softc(xfer);
617 	struct usb_page_cache *pc;
618 	uint32_t actlen;
619 
620 	switch (USB_GET_STATE(xfer)) {
621 	case USB_ST_SETUP:
622 	case USB_ST_TRANSFERRED:
623 tr_setup:
624 		pc = usbd_xfer_get_frame(xfer, 0);
625 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
626 		    sc->sc_obufsize, &actlen)) {
627 			usbd_xfer_set_frame_len(xfer, 0, actlen);
628 			usbd_transfer_submit(xfer);
629 		}
630 		return;
631 
632 	default:			/* Error */
633 		if (error != USB_ERR_CANCELLED) {
634 			/* try to clear stall first */
635 			usbd_xfer_set_stall(xfer);
636 			goto tr_setup;
637 		}
638 		return;
639 	}
640 }
641 
642 static void
643 umct_read_callback_sub(struct usb_xfer *xfer, usb_error_t error)
644 {
645 	struct umct_softc *sc = usbd_xfer_softc(xfer);
646 	struct usb_page_cache *pc;
647 	int actlen;
648 
649 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
650 
651 	switch (USB_GET_STATE(xfer)) {
652 	case USB_ST_TRANSFERRED:
653 		pc = usbd_xfer_get_frame(xfer, 0);
654 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
655 
656 	case USB_ST_SETUP:
657 tr_setup:
658 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
659 		usbd_transfer_submit(xfer);
660 		return;
661 
662 	default:			/* Error */
663 		if (error != USB_ERR_CANCELLED) {
664 			/* try to clear stall first */
665 			usbd_xfer_set_stall(xfer);
666 			goto tr_setup;
667 		}
668 		return;
669 	}
670 }
671 
672 static void
673 umct_poll(struct ucom_softc *ucom)
674 {
675 	struct umct_softc *sc = ucom->sc_parent;
676 	usbd_transfer_poll(sc->sc_xfer, UMCT_N_TRANSFER);
677 }
678