xref: /freebsd/sys/dev/usb/serial/uslcom.c (revision e28a4053)
1 /*	$OpenBSD: uslcom.c,v 1.17 2007/11/24 10:52:12 jsg Exp $	*/
2 
3 #include <sys/cdefs.h>
4 __FBSDID("$FreeBSD$");
5 
6 /*
7  * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  */
21 
22 #include <sys/stdint.h>
23 #include <sys/stddef.h>
24 #include <sys/param.h>
25 #include <sys/queue.h>
26 #include <sys/types.h>
27 #include <sys/systm.h>
28 #include <sys/kernel.h>
29 #include <sys/bus.h>
30 #include <sys/linker_set.h>
31 #include <sys/module.h>
32 #include <sys/lock.h>
33 #include <sys/mutex.h>
34 #include <sys/condvar.h>
35 #include <sys/sysctl.h>
36 #include <sys/sx.h>
37 #include <sys/unistd.h>
38 #include <sys/callout.h>
39 #include <sys/malloc.h>
40 #include <sys/priv.h>
41 
42 #include <dev/usb/usb.h>
43 #include <dev/usb/usbdi.h>
44 #include <dev/usb/usbdi_util.h>
45 #include "usbdevs.h"
46 
47 #define	USB_DEBUG_VAR uslcom_debug
48 #include <dev/usb/usb_debug.h>
49 #include <dev/usb/usb_process.h>
50 
51 #include <dev/usb/serial/usb_serial.h>
52 
53 #ifdef USB_DEBUG
54 static int uslcom_debug = 0;
55 
56 SYSCTL_NODE(_hw_usb, OID_AUTO, uslcom, CTLFLAG_RW, 0, "USB uslcom");
57 SYSCTL_INT(_hw_usb_uslcom, OID_AUTO, debug, CTLFLAG_RW,
58     &uslcom_debug, 0, "Debug level");
59 #endif
60 
61 #define	USLCOM_BULK_BUF_SIZE		1024
62 #define	USLCOM_CONFIG_INDEX	0
63 #define	USLCOM_IFACE_INDEX	0
64 
65 #define	USLCOM_SET_DATA_BITS(x)	((x) << 8)
66 
67 #define	USLCOM_WRITE		0x41
68 #define	USLCOM_READ		0xc1
69 
70 #define	USLCOM_UART		0x00
71 #define	USLCOM_BAUD_RATE	0x01
72 #define	USLCOM_DATA		0x03
73 #define	USLCOM_BREAK		0x05
74 #define	USLCOM_CTRL		0x07
75 
76 #define	USLCOM_UART_DISABLE	0x00
77 #define	USLCOM_UART_ENABLE	0x01
78 
79 #define	USLCOM_CTRL_DTR_ON	0x0001
80 #define	USLCOM_CTRL_DTR_SET	0x0100
81 #define	USLCOM_CTRL_RTS_ON	0x0002
82 #define	USLCOM_CTRL_RTS_SET	0x0200
83 #define	USLCOM_CTRL_CTS		0x0010
84 #define	USLCOM_CTRL_DSR		0x0020
85 #define	USLCOM_CTRL_DCD		0x0080
86 
87 #define	USLCOM_BAUD_REF		0x384000
88 
89 #define	USLCOM_STOP_BITS_1	0x00
90 #define	USLCOM_STOP_BITS_2	0x02
91 
92 #define	USLCOM_PARITY_NONE	0x00
93 #define	USLCOM_PARITY_ODD	0x10
94 #define	USLCOM_PARITY_EVEN	0x20
95 
96 #define	USLCOM_PORT_NO		0xFFFF /* XXX think this should be 0 --hps */
97 
98 #define	USLCOM_BREAK_OFF	0x00
99 #define	USLCOM_BREAK_ON		0x01
100 
101 enum {
102 	USLCOM_BULK_DT_WR,
103 	USLCOM_BULK_DT_RD,
104 	USLCOM_N_TRANSFER,
105 };
106 
107 struct uslcom_softc {
108 	struct ucom_super_softc sc_super_ucom;
109 	struct ucom_softc sc_ucom;
110 
111 	struct usb_xfer *sc_xfer[USLCOM_N_TRANSFER];
112 	struct usb_device *sc_udev;
113 	struct mtx sc_mtx;
114 
115 	uint8_t		 sc_msr;
116 	uint8_t		 sc_lsr;
117 };
118 
119 static device_probe_t uslcom_probe;
120 static device_attach_t uslcom_attach;
121 static device_detach_t uslcom_detach;
122 
123 static usb_callback_t uslcom_write_callback;
124 static usb_callback_t uslcom_read_callback;
125 
126 static void uslcom_open(struct ucom_softc *);
127 static void uslcom_close(struct ucom_softc *);
128 static void uslcom_set_dtr(struct ucom_softc *, uint8_t);
129 static void uslcom_set_rts(struct ucom_softc *, uint8_t);
130 static void uslcom_set_break(struct ucom_softc *, uint8_t);
131 static int uslcom_pre_param(struct ucom_softc *, struct termios *);
132 static void uslcom_param(struct ucom_softc *, struct termios *);
133 static void uslcom_get_status(struct ucom_softc *, uint8_t *, uint8_t *);
134 static void uslcom_start_read(struct ucom_softc *);
135 static void uslcom_stop_read(struct ucom_softc *);
136 static void uslcom_start_write(struct ucom_softc *);
137 static void uslcom_stop_write(struct ucom_softc *);
138 static void uslcom_poll(struct ucom_softc *ucom);
139 
140 static const struct usb_config uslcom_config[USLCOM_N_TRANSFER] = {
141 
142 	[USLCOM_BULK_DT_WR] = {
143 		.type = UE_BULK,
144 		.endpoint = UE_ADDR_ANY,
145 		.direction = UE_DIR_OUT,
146 		.bufsize = USLCOM_BULK_BUF_SIZE,
147 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
148 		.callback = &uslcom_write_callback,
149 	},
150 
151 	[USLCOM_BULK_DT_RD] = {
152 		.type = UE_BULK,
153 		.endpoint = UE_ADDR_ANY,
154 		.direction = UE_DIR_IN,
155 		.bufsize = USLCOM_BULK_BUF_SIZE,
156 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
157 		.callback = &uslcom_read_callback,
158 	},
159 };
160 
161 static struct ucom_callback uslcom_callback = {
162 	.ucom_cfg_open = &uslcom_open,
163 	.ucom_cfg_close = &uslcom_close,
164 	.ucom_cfg_get_status = &uslcom_get_status,
165 	.ucom_cfg_set_dtr = &uslcom_set_dtr,
166 	.ucom_cfg_set_rts = &uslcom_set_rts,
167 	.ucom_cfg_set_break = &uslcom_set_break,
168 	.ucom_cfg_param = &uslcom_param,
169 	.ucom_pre_param = &uslcom_pre_param,
170 	.ucom_start_read = &uslcom_start_read,
171 	.ucom_stop_read = &uslcom_stop_read,
172 	.ucom_start_write = &uslcom_start_write,
173 	.ucom_stop_write = &uslcom_stop_write,
174 	.ucom_poll = &uslcom_poll,
175 };
176 
177 static const struct usb_device_id uslcom_devs[] = {
178 #define	USLCOM_DEV(v,p)  { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
179     USLCOM_DEV(BALTECH, CARDREADER),
180     USLCOM_DEV(CLIPSAL, 5500PCU),
181     USLCOM_DEV(DATAAPEX, MULTICOM),
182     USLCOM_DEV(DELL, DW700),
183     USLCOM_DEV(DIGIANSWER, ZIGBEE802154),
184     USLCOM_DEV(DYNASTREAM, ANTDEVBOARD),
185     USLCOM_DEV(DYNASTREAM, ANTDEVBOARD2),
186     USLCOM_DEV(DYNASTREAM, ANT2USB),
187     USLCOM_DEV(ELV, USBI2C),
188     USLCOM_DEV(FOXCONN, PIRELLI_DP_L10),
189     USLCOM_DEV(GEMALTO, PROXPU),
190     USLCOM_DEV(JABLOTRON, PC60B),
191     USLCOM_DEV(MEI, CASHFLOW_SC),
192     USLCOM_DEV(MEI, S2000),
193     USLCOM_DEV(JABLOTRON, PC60B),
194     USLCOM_DEV(OWEN, AC4),
195     USLCOM_DEV(PHILIPS, ACE1001),
196     USLCOM_DEV(PLX, CA42),
197     USLCOM_DEV(SILABS, AEROCOMM),
198     USLCOM_DEV(SILABS, AMBER_AMB2560),
199     USLCOM_DEV(SILABS, ARGUSISP),
200     USLCOM_DEV(SILABS, ARKHAM_DS101_A),
201     USLCOM_DEV(SILABS, ARKHAM_DS101_M),
202     USLCOM_DEV(SILABS, ARYGON_MIFARE),
203     USLCOM_DEV(SILABS, AVIT_USB_TTL),
204     USLCOM_DEV(SILABS, BEI_VCP),
205     USLCOM_DEV(SILABS, BSM7DUSB),
206     USLCOM_DEV(SILABS, BURNSIDE),
207     USLCOM_DEV(SILABS, C2_EDGE_MODEM),
208     USLCOM_DEV(SILABS, CP2102),
209     USLCOM_DEV(SILABS, CP210X_2),
210     USLCOM_DEV(SILABS, CRUMB128),
211     USLCOM_DEV(SILABS, CYGNAL),
212     USLCOM_DEV(SILABS, CYGNAL_DEBUG),
213     USLCOM_DEV(SILABS, CYGNAL_GPS),
214     USLCOM_DEV(SILABS, DEGREE),
215     USLCOM_DEV(SILABS, EMS_C1007),
216     USLCOM_DEV(SILABS, HELICOM),
217     USLCOM_DEV(SILABS, IMS_USB_RS422),
218     USLCOM_DEV(SILABS, INFINITY_MIC),
219     USLCOM_DEV(SILABS, INSYS_MODEM),
220     USLCOM_DEV(SILABS, KYOCERA_GPS),
221     USLCOM_DEV(SILABS, LIPOWSKY_HARP),
222     USLCOM_DEV(SILABS, LIPOWSKY_JTAG),
223     USLCOM_DEV(SILABS, LIPOWSKY_LIN),
224     USLCOM_DEV(SILABS, MC35PU),
225     USLCOM_DEV(SILABS, MJS_TOSLINK),
226     USLCOM_DEV(SILABS, MSD_DASHHAWK),
227     USLCOM_DEV(SILABS, POLOLU),
228     USLCOM_DEV(SILABS, PROCYON_AVS),
229     USLCOM_DEV(SILABS, SB_PARAMOUNT_ME),
230     USLCOM_DEV(SILABS, SUUNTO),
231     USLCOM_DEV(SILABS, TAMSMASTER),
232     USLCOM_DEV(SILABS, TELEGESYS_ETRX2),
233     USLCOM_DEV(SILABS, TRACIENT),
234     USLCOM_DEV(SILABS, TRAQMATE),
235     USLCOM_DEV(SILABS, USBCOUNT50),
236     USLCOM_DEV(SILABS, USBPULSE100),
237     USLCOM_DEV(SILABS, USBSCOPE50),
238     USLCOM_DEV(SILABS, USBWAVE12),
239     USLCOM_DEV(SILABS, VSTABI),
240     USLCOM_DEV(SILABS, WAVIT),
241     USLCOM_DEV(SILABS, WMRBATT),
242     USLCOM_DEV(SILABS, WMRRIGBLASTER),
243     USLCOM_DEV(SILABS, WMRRIGTALK),
244     USLCOM_DEV(SILABS, ZEPHYR_BIO),
245     USLCOM_DEV(SILABS2, DCU11CLONE),
246     USLCOM_DEV(SILABS3, GPRS_MODEM),
247     USLCOM_DEV(SILABS4, 100EU_MODEM),
248     USLCOM_DEV(SYNTECH, CYPHERLAB100),
249     USLCOM_DEV(USI, MC60),
250     USLCOM_DEV(VAISALA, CABLE),
251     USLCOM_DEV(WAVESENSE, JAZZ),
252 #undef USLCOM_DEV
253 };
254 
255 static device_method_t uslcom_methods[] = {
256 	DEVMETHOD(device_probe, uslcom_probe),
257 	DEVMETHOD(device_attach, uslcom_attach),
258 	DEVMETHOD(device_detach, uslcom_detach),
259 	{0, 0}
260 };
261 
262 static devclass_t uslcom_devclass;
263 
264 static driver_t uslcom_driver = {
265 	.name = "uslcom",
266 	.methods = uslcom_methods,
267 	.size = sizeof(struct uslcom_softc),
268 };
269 
270 DRIVER_MODULE(uslcom, uhub, uslcom_driver, uslcom_devclass, NULL, 0);
271 MODULE_DEPEND(uslcom, ucom, 1, 1, 1);
272 MODULE_DEPEND(uslcom, usb, 1, 1, 1);
273 MODULE_VERSION(uslcom, 1);
274 
275 static int
276 uslcom_probe(device_t dev)
277 {
278 	struct usb_attach_arg *uaa = device_get_ivars(dev);
279 
280 	DPRINTFN(11, "\n");
281 
282 	if (uaa->usb_mode != USB_MODE_HOST) {
283 		return (ENXIO);
284 	}
285 	if (uaa->info.bConfigIndex != USLCOM_CONFIG_INDEX) {
286 		return (ENXIO);
287 	}
288 	if (uaa->info.bIfaceIndex != USLCOM_IFACE_INDEX) {
289 		return (ENXIO);
290 	}
291 	return (usbd_lookup_id_by_uaa(uslcom_devs, sizeof(uslcom_devs), uaa));
292 }
293 
294 static int
295 uslcom_attach(device_t dev)
296 {
297 	struct usb_attach_arg *uaa = device_get_ivars(dev);
298 	struct uslcom_softc *sc = device_get_softc(dev);
299 	int error;
300 
301 	DPRINTFN(11, "\n");
302 
303 	device_set_usb_desc(dev);
304 	mtx_init(&sc->sc_mtx, "uslcom", NULL, MTX_DEF);
305 
306 	sc->sc_udev = uaa->device;
307 
308 	error = usbd_transfer_setup(uaa->device,
309 	    &uaa->info.bIfaceIndex, sc->sc_xfer, uslcom_config,
310 	    USLCOM_N_TRANSFER, sc, &sc->sc_mtx);
311 	if (error) {
312 		DPRINTF("one or more missing USB endpoints, "
313 		    "error=%s\n", usbd_errstr(error));
314 		goto detach;
315 	}
316 	/* clear stall at first run */
317 	mtx_lock(&sc->sc_mtx);
318 	usbd_xfer_set_stall(sc->sc_xfer[USLCOM_BULK_DT_WR]);
319 	usbd_xfer_set_stall(sc->sc_xfer[USLCOM_BULK_DT_RD]);
320 	mtx_unlock(&sc->sc_mtx);
321 
322 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
323 	    &uslcom_callback, &sc->sc_mtx);
324 	if (error) {
325 		goto detach;
326 	}
327 	return (0);
328 
329 detach:
330 	uslcom_detach(dev);
331 	return (ENXIO);
332 }
333 
334 static int
335 uslcom_detach(device_t dev)
336 {
337 	struct uslcom_softc *sc = device_get_softc(dev);
338 
339 	DPRINTF("sc=%p\n", sc);
340 
341 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
342 	usbd_transfer_unsetup(sc->sc_xfer, USLCOM_N_TRANSFER);
343 	mtx_destroy(&sc->sc_mtx);
344 
345 	return (0);
346 }
347 
348 static void
349 uslcom_open(struct ucom_softc *ucom)
350 {
351 	struct uslcom_softc *sc = ucom->sc_parent;
352 	struct usb_device_request req;
353 
354 	req.bmRequestType = USLCOM_WRITE;
355 	req.bRequest = USLCOM_UART;
356 	USETW(req.wValue, USLCOM_UART_ENABLE);
357 	USETW(req.wIndex, USLCOM_PORT_NO);
358 	USETW(req.wLength, 0);
359 
360         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
361 	    &req, NULL, 0, 1000)) {
362 		DPRINTF("UART enable failed (ignored)\n");
363 	}
364 }
365 
366 static void
367 uslcom_close(struct ucom_softc *ucom)
368 {
369 	struct uslcom_softc *sc = ucom->sc_parent;
370 	struct usb_device_request req;
371 
372 	req.bmRequestType = USLCOM_WRITE;
373 	req.bRequest = USLCOM_UART;
374 	USETW(req.wValue, USLCOM_UART_DISABLE);
375 	USETW(req.wIndex, USLCOM_PORT_NO);
376 	USETW(req.wLength, 0);
377 
378         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
379 	    &req, NULL, 0, 1000)) {
380 		DPRINTF("UART disable failed (ignored)\n");
381 	}
382 }
383 
384 static void
385 uslcom_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
386 {
387         struct uslcom_softc *sc = ucom->sc_parent;
388 	struct usb_device_request req;
389 	uint16_t ctl;
390 
391         DPRINTF("onoff = %d\n", onoff);
392 
393 	ctl = onoff ? USLCOM_CTRL_DTR_ON : 0;
394 	ctl |= USLCOM_CTRL_DTR_SET;
395 
396 	req.bmRequestType = USLCOM_WRITE;
397 	req.bRequest = USLCOM_CTRL;
398 	USETW(req.wValue, ctl);
399 	USETW(req.wIndex, USLCOM_PORT_NO);
400 	USETW(req.wLength, 0);
401 
402         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
403 	    &req, NULL, 0, 1000)) {
404 		DPRINTF("Setting DTR failed (ignored)\n");
405 	}
406 }
407 
408 static void
409 uslcom_set_rts(struct ucom_softc *ucom, uint8_t onoff)
410 {
411         struct uslcom_softc *sc = ucom->sc_parent;
412 	struct usb_device_request req;
413 	uint16_t ctl;
414 
415         DPRINTF("onoff = %d\n", onoff);
416 
417 	ctl = onoff ? USLCOM_CTRL_RTS_ON : 0;
418 	ctl |= USLCOM_CTRL_RTS_SET;
419 
420 	req.bmRequestType = USLCOM_WRITE;
421 	req.bRequest = USLCOM_CTRL;
422 	USETW(req.wValue, ctl);
423 	USETW(req.wIndex, USLCOM_PORT_NO);
424 	USETW(req.wLength, 0);
425 
426         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
427 	    &req, NULL, 0, 1000)) {
428 		DPRINTF("Setting DTR failed (ignored)\n");
429 	}
430 }
431 
432 static int
433 uslcom_pre_param(struct ucom_softc *ucom, struct termios *t)
434 {
435 	if (t->c_ospeed <= 0 || t->c_ospeed > 921600)
436 		return (EINVAL);
437 	return (0);
438 }
439 
440 static void
441 uslcom_param(struct ucom_softc *ucom, struct termios *t)
442 {
443 	struct uslcom_softc *sc = ucom->sc_parent;
444 	struct usb_device_request req;
445 	uint16_t data;
446 
447 	DPRINTF("\n");
448 
449 	req.bmRequestType = USLCOM_WRITE;
450 	req.bRequest = USLCOM_BAUD_RATE;
451 	USETW(req.wValue, USLCOM_BAUD_REF / t->c_ospeed);
452 	USETW(req.wIndex, USLCOM_PORT_NO);
453 	USETW(req.wLength, 0);
454 
455         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
456 	    &req, NULL, 0, 1000)) {
457 		DPRINTF("Set baudrate failed (ignored)\n");
458 	}
459 
460 	if (t->c_cflag & CSTOPB)
461 		data = USLCOM_STOP_BITS_2;
462 	else
463 		data = USLCOM_STOP_BITS_1;
464 	if (t->c_cflag & PARENB) {
465 		if (t->c_cflag & PARODD)
466 			data |= USLCOM_PARITY_ODD;
467 		else
468 			data |= USLCOM_PARITY_EVEN;
469 	} else
470 		data |= USLCOM_PARITY_NONE;
471 	switch (t->c_cflag & CSIZE) {
472 	case CS5:
473 		data |= USLCOM_SET_DATA_BITS(5);
474 		break;
475 	case CS6:
476 		data |= USLCOM_SET_DATA_BITS(6);
477 		break;
478 	case CS7:
479 		data |= USLCOM_SET_DATA_BITS(7);
480 		break;
481 	case CS8:
482 		data |= USLCOM_SET_DATA_BITS(8);
483 		break;
484 	}
485 
486 	req.bmRequestType = USLCOM_WRITE;
487 	req.bRequest = USLCOM_DATA;
488 	USETW(req.wValue, data);
489 	USETW(req.wIndex, USLCOM_PORT_NO);
490 	USETW(req.wLength, 0);
491 
492         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
493 	    &req, NULL, 0, 1000)) {
494 		DPRINTF("Set format failed (ignored)\n");
495 	}
496 	return;
497 }
498 
499 static void
500 uslcom_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
501 {
502 	struct uslcom_softc *sc = ucom->sc_parent;
503 
504 	DPRINTF("\n");
505 
506 	*lsr = sc->sc_lsr;
507 	*msr = sc->sc_msr;
508 }
509 
510 static void
511 uslcom_set_break(struct ucom_softc *ucom, uint8_t onoff)
512 {
513         struct uslcom_softc *sc = ucom->sc_parent;
514 	struct usb_device_request req;
515 	uint16_t brk = onoff ? USLCOM_BREAK_ON : USLCOM_BREAK_OFF;
516 
517 	req.bmRequestType = USLCOM_WRITE;
518 	req.bRequest = USLCOM_BREAK;
519 	USETW(req.wValue, brk);
520 	USETW(req.wIndex, USLCOM_PORT_NO);
521 	USETW(req.wLength, 0);
522 
523         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
524 	    &req, NULL, 0, 1000)) {
525 		DPRINTF("Set BREAK failed (ignored)\n");
526 	}
527 }
528 
529 static void
530 uslcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
531 {
532 	struct uslcom_softc *sc = usbd_xfer_softc(xfer);
533 	struct usb_page_cache *pc;
534 	uint32_t actlen;
535 
536 	switch (USB_GET_STATE(xfer)) {
537 	case USB_ST_SETUP:
538 	case USB_ST_TRANSFERRED:
539 tr_setup:
540 		pc = usbd_xfer_get_frame(xfer, 0);
541 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
542 		    USLCOM_BULK_BUF_SIZE, &actlen)) {
543 
544 			DPRINTF("actlen = %d\n", actlen);
545 
546 			usbd_xfer_set_frame_len(xfer, 0, actlen);
547 			usbd_transfer_submit(xfer);
548 		}
549 		return;
550 
551 	default:			/* Error */
552 		if (error != USB_ERR_CANCELLED) {
553 			/* try to clear stall first */
554 			usbd_xfer_set_stall(xfer);
555 			goto tr_setup;
556 		}
557 		return;
558 	}
559 }
560 
561 static void
562 uslcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
563 {
564 	struct uslcom_softc *sc = usbd_xfer_softc(xfer);
565 	struct usb_page_cache *pc;
566 	int actlen;
567 
568 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
569 
570 	switch (USB_GET_STATE(xfer)) {
571 	case USB_ST_TRANSFERRED:
572 		pc = usbd_xfer_get_frame(xfer, 0);
573 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
574 
575 	case USB_ST_SETUP:
576 tr_setup:
577 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
578 		usbd_transfer_submit(xfer);
579 		return;
580 
581 	default:			/* Error */
582 		if (error != USB_ERR_CANCELLED) {
583 			/* try to clear stall first */
584 			usbd_xfer_set_stall(xfer);
585 			goto tr_setup;
586 		}
587 		return;
588 	}
589 }
590 
591 static void
592 uslcom_start_read(struct ucom_softc *ucom)
593 {
594 	struct uslcom_softc *sc = ucom->sc_parent;
595 
596 	/* start read endpoint */
597 	usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_RD]);
598 }
599 
600 static void
601 uslcom_stop_read(struct ucom_softc *ucom)
602 {
603 	struct uslcom_softc *sc = ucom->sc_parent;
604 
605 	/* stop read endpoint */
606 	usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_RD]);
607 }
608 
609 static void
610 uslcom_start_write(struct ucom_softc *ucom)
611 {
612 	struct uslcom_softc *sc = ucom->sc_parent;
613 
614 	usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_WR]);
615 }
616 
617 static void
618 uslcom_stop_write(struct ucom_softc *ucom)
619 {
620 	struct uslcom_softc *sc = ucom->sc_parent;
621 
622 	usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_WR]);
623 }
624 
625 static void
626 uslcom_poll(struct ucom_softc *ucom)
627 {
628 	struct uslcom_softc *sc = ucom->sc_parent;
629 	usbd_transfer_poll(sc->sc_xfer, USLCOM_N_TRANSFER);
630 }
631