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