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