xref: /freebsd/sys/dev/usb/serial/uslcom.c (revision 9a14aa017b21c292740c00ee098195cd46642730)
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 <dev/usb/usb_ioctl.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 static 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 /* Request types */
68 #define	USLCOM_WRITE		0x41
69 #define	USLCOM_READ		0xc1
70 
71 /* Request codes */
72 #define	USLCOM_UART		0x00
73 #define	USLCOM_BAUD_RATE	0x01
74 #define	USLCOM_DATA		0x03
75 #define	USLCOM_BREAK		0x05
76 #define	USLCOM_CTRL		0x07
77 #define	USLCOM_RCTRL            0x08
78 #define	USLCOM_SET_FLOWCTRL     0x13
79 #define	USLCOM_VENDOR_SPECIFIC	0xff
80 
81 /* USLCOM_UART values */
82 #define	USLCOM_UART_DISABLE	0x00
83 #define	USLCOM_UART_ENABLE	0x01
84 
85 /* USLCOM_CTRL/USLCOM_RCTRL values */
86 #define	USLCOM_CTRL_DTR_ON	0x0001
87 #define	USLCOM_CTRL_DTR_SET	0x0100
88 #define	USLCOM_CTRL_RTS_ON	0x0002
89 #define	USLCOM_CTRL_RTS_SET	0x0200
90 #define	USLCOM_CTRL_CTS		0x0010
91 #define	USLCOM_CTRL_DSR		0x0020
92 #define	USLCOM_CTRL_RI          0x0040
93 #define	USLCOM_CTRL_DCD		0x0080
94 
95 /* USLCOM_BAUD_RATE values */
96 #define	USLCOM_BAUD_REF		0x384000
97 
98 /* USLCOM_DATA values */
99 #define	USLCOM_STOP_BITS_1	0x00
100 #define	USLCOM_STOP_BITS_2	0x02
101 #define	USLCOM_PARITY_NONE	0x00
102 #define	USLCOM_PARITY_ODD	0x10
103 #define	USLCOM_PARITY_EVEN	0x20
104 
105 #define	USLCOM_PORT_NO		0x0000
106 
107 /* USLCOM_BREAK values */
108 #define	USLCOM_BREAK_OFF	0x00
109 #define	USLCOM_BREAK_ON		0x01
110 
111 /* USLCOM_SET_FLOWCTRL values - 1st word */
112 #define	USLCOM_FLOW_DTR_ON      0x00000001 /* DTR static active */
113 #define	USLCOM_FLOW_CTS_HS      0x00000008 /* CTS handshake */
114 /* USLCOM_SET_FLOWCTRL values - 2nd word */
115 #define	USLCOM_FLOW_RTS_ON      0x00000040 /* RTS static active */
116 #define	USLCOM_FLOW_RTS_HS      0x00000080 /* RTS handshake */
117 
118 /* USLCOM_VENDOR_SPECIFIC values */
119 #define	USLCOM_WRITE_LATCH	0x37E1
120 #define	USLCOM_READ_LATCH	0x00C2
121 
122 enum {
123 	USLCOM_BULK_DT_WR,
124 	USLCOM_BULK_DT_RD,
125 	USLCOM_CTRL_DT_RD,
126 	USLCOM_N_TRANSFER,
127 };
128 
129 struct uslcom_softc {
130 	struct ucom_super_softc sc_super_ucom;
131 	struct ucom_softc sc_ucom;
132 	struct usb_callout sc_watchdog;
133 
134 	struct usb_xfer *sc_xfer[USLCOM_N_TRANSFER];
135 	struct usb_device *sc_udev;
136 	struct mtx sc_mtx;
137 
138 	uint8_t		 sc_msr;
139 	uint8_t		 sc_lsr;
140 };
141 
142 static device_probe_t uslcom_probe;
143 static device_attach_t uslcom_attach;
144 static device_detach_t uslcom_detach;
145 
146 static usb_callback_t uslcom_write_callback;
147 static usb_callback_t uslcom_read_callback;
148 static usb_callback_t uslcom_control_callback;
149 
150 static void uslcom_open(struct ucom_softc *);
151 static void uslcom_close(struct ucom_softc *);
152 static void uslcom_set_dtr(struct ucom_softc *, uint8_t);
153 static void uslcom_set_rts(struct ucom_softc *, uint8_t);
154 static void uslcom_set_break(struct ucom_softc *, uint8_t);
155 static int uslcom_ioctl(struct ucom_softc *, uint32_t, caddr_t, int,
156 		struct thread *);
157 static int uslcom_pre_param(struct ucom_softc *, struct termios *);
158 static void uslcom_param(struct ucom_softc *, struct termios *);
159 static void uslcom_get_status(struct ucom_softc *, uint8_t *, uint8_t *);
160 static void uslcom_start_read(struct ucom_softc *);
161 static void uslcom_stop_read(struct ucom_softc *);
162 static void uslcom_start_write(struct ucom_softc *);
163 static void uslcom_stop_write(struct ucom_softc *);
164 static void uslcom_poll(struct ucom_softc *ucom);
165 
166 static const struct usb_config uslcom_config[USLCOM_N_TRANSFER] = {
167 
168 	[USLCOM_BULK_DT_WR] = {
169 		.type = UE_BULK,
170 		.endpoint = UE_ADDR_ANY,
171 		.direction = UE_DIR_OUT,
172 		.bufsize = USLCOM_BULK_BUF_SIZE,
173                .flags = {.pipe_bof = 1,},
174 		.callback = &uslcom_write_callback,
175 	},
176 
177 	[USLCOM_BULK_DT_RD] = {
178 		.type = UE_BULK,
179 		.endpoint = UE_ADDR_ANY,
180 		.direction = UE_DIR_IN,
181 		.bufsize = USLCOM_BULK_BUF_SIZE,
182 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
183 		.callback = &uslcom_read_callback,
184 	},
185 	[USLCOM_CTRL_DT_RD] = {
186 		.type = UE_CONTROL,
187 		.endpoint = 0x00,
188 		.direction = UE_DIR_ANY,
189 		.bufsize = sizeof(struct usb_device_request) + 8,
190 		.flags = {.pipe_bof = 1,},
191 		.callback = &uslcom_control_callback,
192 		.timeout = 1000,	/* 1 second timeout */
193 	},
194 };
195 
196 static struct ucom_callback uslcom_callback = {
197 	.ucom_cfg_open = &uslcom_open,
198 	.ucom_cfg_close = &uslcom_close,
199 	.ucom_cfg_get_status = &uslcom_get_status,
200 	.ucom_cfg_set_dtr = &uslcom_set_dtr,
201 	.ucom_cfg_set_rts = &uslcom_set_rts,
202 	.ucom_cfg_set_break = &uslcom_set_break,
203 	.ucom_ioctl = &uslcom_ioctl,
204 	.ucom_cfg_param = &uslcom_param,
205 	.ucom_pre_param = &uslcom_pre_param,
206 	.ucom_start_read = &uslcom_start_read,
207 	.ucom_stop_read = &uslcom_stop_read,
208 	.ucom_start_write = &uslcom_start_write,
209 	.ucom_stop_write = &uslcom_stop_write,
210 	.ucom_poll = &uslcom_poll,
211 };
212 
213 static const STRUCT_USB_HOST_ID uslcom_devs[] = {
214 #define	USLCOM_DEV(v,p)  { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
215     USLCOM_DEV(BALTECH, CARDREADER),
216     USLCOM_DEV(CLIPSAL, 5500PCU),
217     USLCOM_DEV(DATAAPEX, MULTICOM),
218     USLCOM_DEV(DELL, DW700),
219     USLCOM_DEV(DIGIANSWER, ZIGBEE802154),
220     USLCOM_DEV(DYNASTREAM, ANTDEVBOARD),
221     USLCOM_DEV(DYNASTREAM, ANTDEVBOARD2),
222     USLCOM_DEV(DYNASTREAM, ANT2USB),
223     USLCOM_DEV(ELV, USBI2C),
224     USLCOM_DEV(FOXCONN, PIRELLI_DP_L10),
225     USLCOM_DEV(FOXCONN, TCOM_TC_300),
226     USLCOM_DEV(GEMALTO, PROXPU),
227     USLCOM_DEV(JABLOTRON, PC60B),
228     USLCOM_DEV(MEI, CASHFLOW_SC),
229     USLCOM_DEV(MEI, S2000),
230     USLCOM_DEV(JABLOTRON, PC60B),
231     USLCOM_DEV(OWEN, AC4),
232     USLCOM_DEV(PHILIPS, ACE1001),
233     USLCOM_DEV(PLX, CA42),
234     USLCOM_DEV(RENESAS, RX610),
235     USLCOM_DEV(SILABS, AEROCOMM),
236     USLCOM_DEV(SILABS, AMBER_AMB2560),
237     USLCOM_DEV(SILABS, ARGUSISP),
238     USLCOM_DEV(SILABS, ARKHAM_DS101_A),
239     USLCOM_DEV(SILABS, ARKHAM_DS101_M),
240     USLCOM_DEV(SILABS, ARYGON_MIFARE),
241     USLCOM_DEV(SILABS, AVIT_USB_TTL),
242     USLCOM_DEV(SILABS, B_G_H3000),
243     USLCOM_DEV(SILABS, BALLUFF_RFID),
244     USLCOM_DEV(SILABS, BEI_VCP),
245     USLCOM_DEV(SILABS, BSM7DUSB),
246     USLCOM_DEV(SILABS, BURNSIDE),
247     USLCOM_DEV(SILABS, C2_EDGE_MODEM),
248     USLCOM_DEV(SILABS, CP2102),
249     USLCOM_DEV(SILABS, CP210X_2),
250     USLCOM_DEV(SILABS, CRUMB128),
251     USLCOM_DEV(SILABS, CYGNAL),
252     USLCOM_DEV(SILABS, CYGNAL_DEBUG),
253     USLCOM_DEV(SILABS, CYGNAL_GPS),
254     USLCOM_DEV(SILABS, DEGREE),
255     USLCOM_DEV(SILABS, EMS_C1007),
256     USLCOM_DEV(SILABS, HELICOM),
257     USLCOM_DEV(SILABS, IMS_USB_RS422),
258     USLCOM_DEV(SILABS, INFINITY_MIC),
259     USLCOM_DEV(SILABS, INSYS_MODEM),
260     USLCOM_DEV(SILABS, KYOCERA_GPS),
261     USLCOM_DEV(SILABS, LIPOWSKY_HARP),
262     USLCOM_DEV(SILABS, LIPOWSKY_JTAG),
263     USLCOM_DEV(SILABS, LIPOWSKY_LIN),
264     USLCOM_DEV(SILABS, MC35PU),
265     USLCOM_DEV(SILABS, MJS_TOSLINK),
266     USLCOM_DEV(SILABS, MSD_DASHHAWK),
267     USLCOM_DEV(SILABS, POLOLU),
268     USLCOM_DEV(SILABS, PROCYON_AVS),
269     USLCOM_DEV(SILABS, SB_PARAMOUNT_ME),
270     USLCOM_DEV(SILABS, SUUNTO),
271     USLCOM_DEV(SILABS, TAMSMASTER),
272     USLCOM_DEV(SILABS, TELEGESYS_ETRX2),
273     USLCOM_DEV(SILABS, TRACIENT),
274     USLCOM_DEV(SILABS, TRAQMATE),
275     USLCOM_DEV(SILABS, USBCOUNT50),
276     USLCOM_DEV(SILABS, USBPULSE100),
277     USLCOM_DEV(SILABS, USBSCOPE50),
278     USLCOM_DEV(SILABS, USBWAVE12),
279     USLCOM_DEV(SILABS, VSTABI),
280     USLCOM_DEV(SILABS, WAVIT),
281     USLCOM_DEV(SILABS, WMRBATT),
282     USLCOM_DEV(SILABS, WMRRIGBLASTER),
283     USLCOM_DEV(SILABS, WMRRIGTALK),
284     USLCOM_DEV(SILABS, ZEPHYR_BIO),
285     USLCOM_DEV(SILABS2, DCU11CLONE),
286     USLCOM_DEV(SILABS3, GPRS_MODEM),
287     USLCOM_DEV(SILABS4, 100EU_MODEM),
288     USLCOM_DEV(SYNTECH, CYPHERLAB100),
289     USLCOM_DEV(USI, MC60),
290     USLCOM_DEV(VAISALA, CABLE),
291     USLCOM_DEV(WAGO, SERVICECABLE),
292     USLCOM_DEV(WAVESENSE, JAZZ),
293     USLCOM_DEV(WIENERPLEINBAUS, PL512),
294     USLCOM_DEV(WIENERPLEINBAUS, RCM),
295     USLCOM_DEV(WIENERPLEINBAUS, MPOD),
296     USLCOM_DEV(WIENERPLEINBAUS, CML),
297 #undef USLCOM_DEV
298 };
299 
300 static device_method_t uslcom_methods[] = {
301 	DEVMETHOD(device_probe, uslcom_probe),
302 	DEVMETHOD(device_attach, uslcom_attach),
303 	DEVMETHOD(device_detach, uslcom_detach),
304 	{0, 0}
305 };
306 
307 static devclass_t uslcom_devclass;
308 
309 static driver_t uslcom_driver = {
310 	.name = "uslcom",
311 	.methods = uslcom_methods,
312 	.size = sizeof(struct uslcom_softc),
313 };
314 
315 DRIVER_MODULE(uslcom, uhub, uslcom_driver, uslcom_devclass, NULL, 0);
316 MODULE_DEPEND(uslcom, ucom, 1, 1, 1);
317 MODULE_DEPEND(uslcom, usb, 1, 1, 1);
318 MODULE_VERSION(uslcom, 1);
319 
320 static void
321 uslcom_watchdog(void *arg)
322 {
323 	struct uslcom_softc *sc = arg;
324 
325 	mtx_assert(&sc->sc_mtx, MA_OWNED);
326 
327 	usbd_transfer_start(sc->sc_xfer[USLCOM_CTRL_DT_RD]);
328 
329 	usb_callout_reset(&sc->sc_watchdog,
330 	    hz / 4, &uslcom_watchdog, sc);
331 }
332 
333 static int
334 uslcom_probe(device_t dev)
335 {
336 	struct usb_attach_arg *uaa = device_get_ivars(dev);
337 
338 	DPRINTFN(11, "\n");
339 
340 	if (uaa->usb_mode != USB_MODE_HOST) {
341 		return (ENXIO);
342 	}
343 	if (uaa->info.bConfigIndex != USLCOM_CONFIG_INDEX) {
344 		return (ENXIO);
345 	}
346 	if (uaa->info.bIfaceIndex != USLCOM_IFACE_INDEX) {
347 		return (ENXIO);
348 	}
349 	return (usbd_lookup_id_by_uaa(uslcom_devs, sizeof(uslcom_devs), uaa));
350 }
351 
352 static int
353 uslcom_attach(device_t dev)
354 {
355 	struct usb_attach_arg *uaa = device_get_ivars(dev);
356 	struct uslcom_softc *sc = device_get_softc(dev);
357 	int error;
358 
359 	DPRINTFN(11, "\n");
360 
361 	device_set_usb_desc(dev);
362 	mtx_init(&sc->sc_mtx, "uslcom", NULL, MTX_DEF);
363 	usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
364 
365 	sc->sc_udev = uaa->device;
366 
367 	error = usbd_transfer_setup(uaa->device,
368 	    &uaa->info.bIfaceIndex, sc->sc_xfer, uslcom_config,
369 	    USLCOM_N_TRANSFER, sc, &sc->sc_mtx);
370 	if (error) {
371 		DPRINTF("one or more missing USB endpoints, "
372 		    "error=%s\n", usbd_errstr(error));
373 		goto detach;
374 	}
375 	/* clear stall at first run */
376 	mtx_lock(&sc->sc_mtx);
377 	usbd_xfer_set_stall(sc->sc_xfer[USLCOM_BULK_DT_WR]);
378 	usbd_xfer_set_stall(sc->sc_xfer[USLCOM_BULK_DT_RD]);
379 	mtx_unlock(&sc->sc_mtx);
380 
381 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
382 	    &uslcom_callback, &sc->sc_mtx);
383 	if (error) {
384 		goto detach;
385 	}
386 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
387 
388 	return (0);
389 
390 detach:
391 	uslcom_detach(dev);
392 	return (ENXIO);
393 }
394 
395 static int
396 uslcom_detach(device_t dev)
397 {
398 	struct uslcom_softc *sc = device_get_softc(dev);
399 
400 	DPRINTF("sc=%p\n", sc);
401 
402 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
403 	usbd_transfer_unsetup(sc->sc_xfer, USLCOM_N_TRANSFER);
404 
405 	usb_callout_drain(&sc->sc_watchdog);
406 	mtx_destroy(&sc->sc_mtx);
407 
408 	return (0);
409 }
410 
411 static void
412 uslcom_open(struct ucom_softc *ucom)
413 {
414 	struct uslcom_softc *sc = ucom->sc_parent;
415 	struct usb_device_request req;
416 
417 	req.bmRequestType = USLCOM_WRITE;
418 	req.bRequest = USLCOM_UART;
419 	USETW(req.wValue, USLCOM_UART_ENABLE);
420 	USETW(req.wIndex, USLCOM_PORT_NO);
421 	USETW(req.wLength, 0);
422 
423         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
424 	    &req, NULL, 0, 1000)) {
425 		DPRINTF("UART enable failed (ignored)\n");
426 	}
427 
428 	/* start polling status */
429 	uslcom_watchdog(sc);
430 }
431 
432 static void
433 uslcom_close(struct ucom_softc *ucom)
434 {
435 	struct uslcom_softc *sc = ucom->sc_parent;
436 	struct usb_device_request req;
437 
438 	/* stop polling status */
439 	usb_callout_stop(&sc->sc_watchdog);
440 
441 	req.bmRequestType = USLCOM_WRITE;
442 	req.bRequest = USLCOM_UART;
443 	USETW(req.wValue, USLCOM_UART_DISABLE);
444 	USETW(req.wIndex, USLCOM_PORT_NO);
445 	USETW(req.wLength, 0);
446 
447 	if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
448 	    &req, NULL, 0, 1000)) {
449 		DPRINTF("UART disable failed (ignored)\n");
450 	}
451 }
452 
453 static void
454 uslcom_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
455 {
456         struct uslcom_softc *sc = ucom->sc_parent;
457 	struct usb_device_request req;
458 	uint16_t ctl;
459 
460         DPRINTF("onoff = %d\n", onoff);
461 
462 	ctl = onoff ? USLCOM_CTRL_DTR_ON : 0;
463 	ctl |= USLCOM_CTRL_DTR_SET;
464 
465 	req.bmRequestType = USLCOM_WRITE;
466 	req.bRequest = USLCOM_CTRL;
467 	USETW(req.wValue, ctl);
468 	USETW(req.wIndex, USLCOM_PORT_NO);
469 	USETW(req.wLength, 0);
470 
471         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
472 	    &req, NULL, 0, 1000)) {
473 		DPRINTF("Setting DTR failed (ignored)\n");
474 	}
475 }
476 
477 static void
478 uslcom_set_rts(struct ucom_softc *ucom, uint8_t onoff)
479 {
480         struct uslcom_softc *sc = ucom->sc_parent;
481 	struct usb_device_request req;
482 	uint16_t ctl;
483 
484         DPRINTF("onoff = %d\n", onoff);
485 
486 	ctl = onoff ? USLCOM_CTRL_RTS_ON : 0;
487 	ctl |= USLCOM_CTRL_RTS_SET;
488 
489 	req.bmRequestType = USLCOM_WRITE;
490 	req.bRequest = USLCOM_CTRL;
491 	USETW(req.wValue, ctl);
492 	USETW(req.wIndex, USLCOM_PORT_NO);
493 	USETW(req.wLength, 0);
494 
495         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
496 	    &req, NULL, 0, 1000)) {
497 		DPRINTF("Setting DTR failed (ignored)\n");
498 	}
499 }
500 
501 static int
502 uslcom_pre_param(struct ucom_softc *ucom, struct termios *t)
503 {
504 	if (t->c_ospeed <= 0 || t->c_ospeed > 921600)
505 		return (EINVAL);
506 	return (0);
507 }
508 
509 static void
510 uslcom_param(struct ucom_softc *ucom, struct termios *t)
511 {
512 	struct uslcom_softc *sc = ucom->sc_parent;
513 	struct usb_device_request req;
514 	uint32_t flowctrl[4];
515 	uint16_t data;
516 
517 	DPRINTF("\n");
518 
519 	req.bmRequestType = USLCOM_WRITE;
520 	req.bRequest = USLCOM_BAUD_RATE;
521 	USETW(req.wValue, USLCOM_BAUD_REF / t->c_ospeed);
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 baudrate failed (ignored)\n");
528 	}
529 
530 	if (t->c_cflag & CSTOPB)
531 		data = USLCOM_STOP_BITS_2;
532 	else
533 		data = USLCOM_STOP_BITS_1;
534 	if (t->c_cflag & PARENB) {
535 		if (t->c_cflag & PARODD)
536 			data |= USLCOM_PARITY_ODD;
537 		else
538 			data |= USLCOM_PARITY_EVEN;
539 	} else
540 		data |= USLCOM_PARITY_NONE;
541 	switch (t->c_cflag & CSIZE) {
542 	case CS5:
543 		data |= USLCOM_SET_DATA_BITS(5);
544 		break;
545 	case CS6:
546 		data |= USLCOM_SET_DATA_BITS(6);
547 		break;
548 	case CS7:
549 		data |= USLCOM_SET_DATA_BITS(7);
550 		break;
551 	case CS8:
552 		data |= USLCOM_SET_DATA_BITS(8);
553 		break;
554 	}
555 
556 	req.bmRequestType = USLCOM_WRITE;
557 	req.bRequest = USLCOM_DATA;
558 	USETW(req.wValue, data);
559 	USETW(req.wIndex, USLCOM_PORT_NO);
560 	USETW(req.wLength, 0);
561 
562         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
563 	    &req, NULL, 0, 1000)) {
564 		DPRINTF("Set format failed (ignored)\n");
565 	}
566 
567 	if (t->c_cflag & CRTSCTS) {
568 		flowctrl[0] = htole32(USLCOM_FLOW_DTR_ON | USLCOM_FLOW_CTS_HS);
569 		flowctrl[1] = htole32(USLCOM_FLOW_RTS_HS);
570 		flowctrl[2] = 0;
571 		flowctrl[3] = 0;
572 	} else {
573 		flowctrl[0] = htole32(USLCOM_FLOW_DTR_ON);
574 		flowctrl[1] = htole32(USLCOM_FLOW_RTS_ON);
575 		flowctrl[2] = 0;
576 		flowctrl[3] = 0;
577 	}
578 	req.bmRequestType = USLCOM_WRITE;
579 	req.bRequest = USLCOM_SET_FLOWCTRL;
580 	USETW(req.wValue, 0);
581 	USETW(req.wIndex, USLCOM_PORT_NO);
582 	USETW(req.wLength, sizeof(flowctrl));
583 
584 	if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
585 	    &req, flowctrl, 0, 1000)) {
586 		DPRINTF("Set flowcontrol failed (ignored)\n");
587 	}
588 }
589 
590 static void
591 uslcom_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
592 {
593 	struct uslcom_softc *sc = ucom->sc_parent;
594 
595 	DPRINTF("\n");
596 
597 	*lsr = sc->sc_lsr;
598 	*msr = sc->sc_msr;
599 }
600 
601 static void
602 uslcom_set_break(struct ucom_softc *ucom, uint8_t onoff)
603 {
604         struct uslcom_softc *sc = ucom->sc_parent;
605 	struct usb_device_request req;
606 	uint16_t brk = onoff ? USLCOM_BREAK_ON : USLCOM_BREAK_OFF;
607 
608 	req.bmRequestType = USLCOM_WRITE;
609 	req.bRequest = USLCOM_BREAK;
610 	USETW(req.wValue, brk);
611 	USETW(req.wIndex, USLCOM_PORT_NO);
612 	USETW(req.wLength, 0);
613 
614         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
615 	    &req, NULL, 0, 1000)) {
616 		DPRINTF("Set BREAK failed (ignored)\n");
617 	}
618 }
619 
620 static int
621 uslcom_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data,
622     int flag, struct thread *td)
623 {
624 	struct uslcom_softc *sc = ucom->sc_parent;
625 	struct usb_device_request req;
626 	int error = 0;
627 	uint8_t latch;
628 
629 	DPRINTF("cmd=0x%08x\n", cmd);
630 
631 	switch (cmd) {
632 	case USB_GET_GPIO:
633 		req.bmRequestType = USLCOM_READ;
634 		req.bRequest = USLCOM_VENDOR_SPECIFIC;
635 		USETW(req.wValue, USLCOM_READ_LATCH);
636 		USETW(req.wIndex, 0);
637 		USETW(req.wLength, sizeof(latch));
638 
639 		if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
640 		    &req, &latch, 0, 1000)) {
641 			DPRINTF("Get LATCH failed\n");
642 			error = EIO;
643 		}
644 		*(int *)data = latch;
645 		break;
646 
647 	case USB_SET_GPIO:
648 		req.bmRequestType = USLCOM_WRITE;
649 		req.bRequest = USLCOM_VENDOR_SPECIFIC;
650 		USETW(req.wValue, USLCOM_WRITE_LATCH);
651 		USETW(req.wIndex, (*(int *)data));
652 		USETW(req.wLength, 0);
653 
654 		if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
655 		    &req, NULL, 0, 1000)) {
656 			DPRINTF("Set LATCH failed\n");
657 			error = EIO;
658 		}
659 		break;
660 
661 	default:
662 		DPRINTF("Unknown IOCTL\n");
663 		error = ENOIOCTL;
664 		break;
665 	}
666 	return (error);
667 }
668 
669 static void
670 uslcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
671 {
672 	struct uslcom_softc *sc = usbd_xfer_softc(xfer);
673 	struct usb_page_cache *pc;
674 	uint32_t actlen;
675 
676 	switch (USB_GET_STATE(xfer)) {
677 	case USB_ST_SETUP:
678 	case USB_ST_TRANSFERRED:
679 tr_setup:
680 		pc = usbd_xfer_get_frame(xfer, 0);
681 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
682 		    USLCOM_BULK_BUF_SIZE, &actlen)) {
683 
684 			DPRINTF("actlen = %d\n", actlen);
685 
686 			usbd_xfer_set_frame_len(xfer, 0, actlen);
687 			usbd_transfer_submit(xfer);
688 		}
689 		return;
690 
691 	default:			/* Error */
692 		if (error != USB_ERR_CANCELLED) {
693 			/* try to clear stall first */
694 			usbd_xfer_set_stall(xfer);
695 			goto tr_setup;
696 		}
697 		return;
698 	}
699 }
700 
701 static void
702 uslcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
703 {
704 	struct uslcom_softc *sc = usbd_xfer_softc(xfer);
705 	struct usb_page_cache *pc;
706 	int actlen;
707 
708 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
709 
710 	switch (USB_GET_STATE(xfer)) {
711 	case USB_ST_TRANSFERRED:
712 		pc = usbd_xfer_get_frame(xfer, 0);
713 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
714 
715 	case USB_ST_SETUP:
716 tr_setup:
717 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
718 		usbd_transfer_submit(xfer);
719 		return;
720 
721 	default:			/* Error */
722 		if (error != USB_ERR_CANCELLED) {
723 			/* try to clear stall first */
724 			usbd_xfer_set_stall(xfer);
725 			goto tr_setup;
726 		}
727 		return;
728 	}
729 }
730 
731 static void
732 uslcom_control_callback(struct usb_xfer *xfer, usb_error_t error)
733 {
734 	struct uslcom_softc *sc = usbd_xfer_softc(xfer);
735 	struct usb_page_cache *pc;
736 	struct usb_device_request req;
737 	uint8_t msr = 0;
738 	uint8_t buf;
739 
740 	switch (USB_GET_STATE(xfer)) {
741 	case USB_ST_TRANSFERRED:
742 		pc = usbd_xfer_get_frame(xfer, 1);
743 		usbd_copy_out(pc, 0, &buf, sizeof(buf));
744 		if (buf & USLCOM_CTRL_CTS)
745 			msr |= SER_CTS;
746 		if (buf & USLCOM_CTRL_DSR)
747 			msr |= SER_DSR;
748 		if (buf & USLCOM_CTRL_RI)
749 			msr |= SER_RI;
750 		if (buf & USLCOM_CTRL_DCD)
751 			msr |= SER_DCD;
752 
753 		if (msr != sc->sc_msr) {
754 			DPRINTF("status change msr=0x%02x "
755 			    "(was 0x%02x)\n", msr, sc->sc_msr);
756 			sc->sc_msr = msr;
757 			ucom_status_change(&sc->sc_ucom);
758 		}
759 		break;
760 
761 	case USB_ST_SETUP:
762 		req.bmRequestType = USLCOM_READ;
763 		req.bRequest = USLCOM_RCTRL;
764 		USETW(req.wValue, 0);
765 		USETW(req.wIndex, USLCOM_PORT_NO);
766 		USETW(req.wLength, sizeof(buf));
767 
768 		usbd_xfer_set_frames(xfer, 2);
769 		usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
770 		usbd_xfer_set_frame_len(xfer, 1, sizeof(buf));
771 
772 		pc = usbd_xfer_get_frame(xfer, 0);
773 		usbd_copy_in(pc, 0, &req, sizeof(req));
774 		usbd_transfer_submit(xfer);
775 		break;
776 
777 	default:		/* error */
778 		if (error != USB_ERR_CANCELLED)
779 			DPRINTF("error=%s\n", usbd_errstr(error));
780 		break;
781 	}
782 }
783 
784 static void
785 uslcom_start_read(struct ucom_softc *ucom)
786 {
787 	struct uslcom_softc *sc = ucom->sc_parent;
788 
789 	/* start read endpoint */
790 	usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_RD]);
791 }
792 
793 static void
794 uslcom_stop_read(struct ucom_softc *ucom)
795 {
796 	struct uslcom_softc *sc = ucom->sc_parent;
797 
798 	/* stop read endpoint */
799 	usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_RD]);
800 }
801 
802 static void
803 uslcom_start_write(struct ucom_softc *ucom)
804 {
805 	struct uslcom_softc *sc = ucom->sc_parent;
806 
807 	usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_WR]);
808 }
809 
810 static void
811 uslcom_stop_write(struct ucom_softc *ucom)
812 {
813 	struct uslcom_softc *sc = ucom->sc_parent;
814 
815 	usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_WR]);
816 }
817 
818 static void
819 uslcom_poll(struct ucom_softc *ucom)
820 {
821 	struct uslcom_softc *sc = ucom->sc_parent;
822 	usbd_transfer_poll(sc->sc_xfer, USLCOM_N_TRANSFER);
823 }
824