xref: /freebsd/sys/dev/usb/serial/uslcom.c (revision a3cf0ef5a295c885c895fabfd56470c0d1db322d)
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 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
328 
329 	return (0);
330 
331 detach:
332 	uslcom_detach(dev);
333 	return (ENXIO);
334 }
335 
336 static int
337 uslcom_detach(device_t dev)
338 {
339 	struct uslcom_softc *sc = device_get_softc(dev);
340 
341 	DPRINTF("sc=%p\n", sc);
342 
343 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
344 	usbd_transfer_unsetup(sc->sc_xfer, USLCOM_N_TRANSFER);
345 	mtx_destroy(&sc->sc_mtx);
346 
347 	return (0);
348 }
349 
350 static void
351 uslcom_open(struct ucom_softc *ucom)
352 {
353 	struct uslcom_softc *sc = ucom->sc_parent;
354 	struct usb_device_request req;
355 
356 	req.bmRequestType = USLCOM_WRITE;
357 	req.bRequest = USLCOM_UART;
358 	USETW(req.wValue, USLCOM_UART_ENABLE);
359 	USETW(req.wIndex, USLCOM_PORT_NO);
360 	USETW(req.wLength, 0);
361 
362         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
363 	    &req, NULL, 0, 1000)) {
364 		DPRINTF("UART enable failed (ignored)\n");
365 	}
366 }
367 
368 static void
369 uslcom_close(struct ucom_softc *ucom)
370 {
371 	struct uslcom_softc *sc = ucom->sc_parent;
372 	struct usb_device_request req;
373 
374 	req.bmRequestType = USLCOM_WRITE;
375 	req.bRequest = USLCOM_UART;
376 	USETW(req.wValue, USLCOM_UART_DISABLE);
377 	USETW(req.wIndex, USLCOM_PORT_NO);
378 	USETW(req.wLength, 0);
379 
380         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
381 	    &req, NULL, 0, 1000)) {
382 		DPRINTF("UART disable failed (ignored)\n");
383 	}
384 }
385 
386 static void
387 uslcom_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
388 {
389         struct uslcom_softc *sc = ucom->sc_parent;
390 	struct usb_device_request req;
391 	uint16_t ctl;
392 
393         DPRINTF("onoff = %d\n", onoff);
394 
395 	ctl = onoff ? USLCOM_CTRL_DTR_ON : 0;
396 	ctl |= USLCOM_CTRL_DTR_SET;
397 
398 	req.bmRequestType = USLCOM_WRITE;
399 	req.bRequest = USLCOM_CTRL;
400 	USETW(req.wValue, ctl);
401 	USETW(req.wIndex, USLCOM_PORT_NO);
402 	USETW(req.wLength, 0);
403 
404         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
405 	    &req, NULL, 0, 1000)) {
406 		DPRINTF("Setting DTR failed (ignored)\n");
407 	}
408 }
409 
410 static void
411 uslcom_set_rts(struct ucom_softc *ucom, uint8_t onoff)
412 {
413         struct uslcom_softc *sc = ucom->sc_parent;
414 	struct usb_device_request req;
415 	uint16_t ctl;
416 
417         DPRINTF("onoff = %d\n", onoff);
418 
419 	ctl = onoff ? USLCOM_CTRL_RTS_ON : 0;
420 	ctl |= USLCOM_CTRL_RTS_SET;
421 
422 	req.bmRequestType = USLCOM_WRITE;
423 	req.bRequest = USLCOM_CTRL;
424 	USETW(req.wValue, ctl);
425 	USETW(req.wIndex, USLCOM_PORT_NO);
426 	USETW(req.wLength, 0);
427 
428         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
429 	    &req, NULL, 0, 1000)) {
430 		DPRINTF("Setting DTR failed (ignored)\n");
431 	}
432 }
433 
434 static int
435 uslcom_pre_param(struct ucom_softc *ucom, struct termios *t)
436 {
437 	if (t->c_ospeed <= 0 || t->c_ospeed > 921600)
438 		return (EINVAL);
439 	return (0);
440 }
441 
442 static void
443 uslcom_param(struct ucom_softc *ucom, struct termios *t)
444 {
445 	struct uslcom_softc *sc = ucom->sc_parent;
446 	struct usb_device_request req;
447 	uint16_t data;
448 
449 	DPRINTF("\n");
450 
451 	req.bmRequestType = USLCOM_WRITE;
452 	req.bRequest = USLCOM_BAUD_RATE;
453 	USETW(req.wValue, USLCOM_BAUD_REF / t->c_ospeed);
454 	USETW(req.wIndex, USLCOM_PORT_NO);
455 	USETW(req.wLength, 0);
456 
457         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
458 	    &req, NULL, 0, 1000)) {
459 		DPRINTF("Set baudrate failed (ignored)\n");
460 	}
461 
462 	if (t->c_cflag & CSTOPB)
463 		data = USLCOM_STOP_BITS_2;
464 	else
465 		data = USLCOM_STOP_BITS_1;
466 	if (t->c_cflag & PARENB) {
467 		if (t->c_cflag & PARODD)
468 			data |= USLCOM_PARITY_ODD;
469 		else
470 			data |= USLCOM_PARITY_EVEN;
471 	} else
472 		data |= USLCOM_PARITY_NONE;
473 	switch (t->c_cflag & CSIZE) {
474 	case CS5:
475 		data |= USLCOM_SET_DATA_BITS(5);
476 		break;
477 	case CS6:
478 		data |= USLCOM_SET_DATA_BITS(6);
479 		break;
480 	case CS7:
481 		data |= USLCOM_SET_DATA_BITS(7);
482 		break;
483 	case CS8:
484 		data |= USLCOM_SET_DATA_BITS(8);
485 		break;
486 	}
487 
488 	req.bmRequestType = USLCOM_WRITE;
489 	req.bRequest = USLCOM_DATA;
490 	USETW(req.wValue, data);
491 	USETW(req.wIndex, USLCOM_PORT_NO);
492 	USETW(req.wLength, 0);
493 
494         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
495 	    &req, NULL, 0, 1000)) {
496 		DPRINTF("Set format failed (ignored)\n");
497 	}
498 	return;
499 }
500 
501 static void
502 uslcom_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
503 {
504 	struct uslcom_softc *sc = ucom->sc_parent;
505 
506 	DPRINTF("\n");
507 
508 	*lsr = sc->sc_lsr;
509 	*msr = sc->sc_msr;
510 }
511 
512 static void
513 uslcom_set_break(struct ucom_softc *ucom, uint8_t onoff)
514 {
515         struct uslcom_softc *sc = ucom->sc_parent;
516 	struct usb_device_request req;
517 	uint16_t brk = onoff ? USLCOM_BREAK_ON : USLCOM_BREAK_OFF;
518 
519 	req.bmRequestType = USLCOM_WRITE;
520 	req.bRequest = USLCOM_BREAK;
521 	USETW(req.wValue, brk);
522 	USETW(req.wIndex, USLCOM_PORT_NO);
523 	USETW(req.wLength, 0);
524 
525         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
526 	    &req, NULL, 0, 1000)) {
527 		DPRINTF("Set BREAK failed (ignored)\n");
528 	}
529 }
530 
531 static void
532 uslcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
533 {
534 	struct uslcom_softc *sc = usbd_xfer_softc(xfer);
535 	struct usb_page_cache *pc;
536 	uint32_t actlen;
537 
538 	switch (USB_GET_STATE(xfer)) {
539 	case USB_ST_SETUP:
540 	case USB_ST_TRANSFERRED:
541 tr_setup:
542 		pc = usbd_xfer_get_frame(xfer, 0);
543 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
544 		    USLCOM_BULK_BUF_SIZE, &actlen)) {
545 
546 			DPRINTF("actlen = %d\n", actlen);
547 
548 			usbd_xfer_set_frame_len(xfer, 0, actlen);
549 			usbd_transfer_submit(xfer);
550 		}
551 		return;
552 
553 	default:			/* Error */
554 		if (error != USB_ERR_CANCELLED) {
555 			/* try to clear stall first */
556 			usbd_xfer_set_stall(xfer);
557 			goto tr_setup;
558 		}
559 		return;
560 	}
561 }
562 
563 static void
564 uslcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
565 {
566 	struct uslcom_softc *sc = usbd_xfer_softc(xfer);
567 	struct usb_page_cache *pc;
568 	int actlen;
569 
570 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
571 
572 	switch (USB_GET_STATE(xfer)) {
573 	case USB_ST_TRANSFERRED:
574 		pc = usbd_xfer_get_frame(xfer, 0);
575 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
576 
577 	case USB_ST_SETUP:
578 tr_setup:
579 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
580 		usbd_transfer_submit(xfer);
581 		return;
582 
583 	default:			/* Error */
584 		if (error != USB_ERR_CANCELLED) {
585 			/* try to clear stall first */
586 			usbd_xfer_set_stall(xfer);
587 			goto tr_setup;
588 		}
589 		return;
590 	}
591 }
592 
593 static void
594 uslcom_start_read(struct ucom_softc *ucom)
595 {
596 	struct uslcom_softc *sc = ucom->sc_parent;
597 
598 	/* start read endpoint */
599 	usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_RD]);
600 }
601 
602 static void
603 uslcom_stop_read(struct ucom_softc *ucom)
604 {
605 	struct uslcom_softc *sc = ucom->sc_parent;
606 
607 	/* stop read endpoint */
608 	usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_RD]);
609 }
610 
611 static void
612 uslcom_start_write(struct ucom_softc *ucom)
613 {
614 	struct uslcom_softc *sc = ucom->sc_parent;
615 
616 	usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_WR]);
617 }
618 
619 static void
620 uslcom_stop_write(struct ucom_softc *ucom)
621 {
622 	struct uslcom_softc *sc = ucom->sc_parent;
623 
624 	usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_WR]);
625 }
626 
627 static void
628 uslcom_poll(struct ucom_softc *ucom)
629 {
630 	struct uslcom_softc *sc = ucom->sc_parent;
631 	usbd_transfer_poll(sc->sc_xfer, USLCOM_N_TRANSFER);
632 }
633