xref: /freebsd/sys/dev/usb/serial/uchcom.c (revision 7661de35d15f582ab33e3bd6b8d909601557e436)
1 /*	$NetBSD: uchcom.c,v 1.1 2007/09/03 17:57:37 tshiozak Exp $	*/
2 
3 /*-
4  * Copyright (c) 2007, Takanori Watanabe
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  * Copyright (c) 2007 The NetBSD Foundation, Inc.
31  * All rights reserved.
32  *
33  * This code is derived from software contributed to The NetBSD Foundation
34  * by Takuya SHIOZAKI (tshiozak@netbsd.org).
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. All advertising materials mentioning features or use of this software
45  *    must display the following acknowledgement:
46  *        This product includes software developed by the NetBSD
47  *        Foundation, Inc. and its contributors.
48  * 4. Neither the name of The NetBSD Foundation nor the names of its
49  *    contributors may be used to endorse or promote products derived
50  *    from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
53  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
54  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
55  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
56  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62  * POSSIBILITY OF SUCH DAMAGE.
63  */
64 
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
67 
68 /*
69  * Driver for WinChipHead CH341/340, the worst USB-serial chip in the
70  * world.
71  */
72 
73 #include <sys/stdint.h>
74 #include <sys/stddef.h>
75 #include <sys/param.h>
76 #include <sys/queue.h>
77 #include <sys/types.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/bus.h>
81 #include <sys/module.h>
82 #include <sys/lock.h>
83 #include <sys/mutex.h>
84 #include <sys/condvar.h>
85 #include <sys/sysctl.h>
86 #include <sys/sx.h>
87 #include <sys/unistd.h>
88 #include <sys/callout.h>
89 #include <sys/malloc.h>
90 #include <sys/priv.h>
91 
92 #include <dev/usb/usb.h>
93 #include <dev/usb/usbdi.h>
94 #include <dev/usb/usbdi_util.h>
95 #include "usbdevs.h"
96 
97 #define	USB_DEBUG_VAR uchcom_debug
98 #include <dev/usb/usb_debug.h>
99 #include <dev/usb/usb_process.h>
100 
101 #include <dev/usb/serial/usb_serial.h>
102 
103 #ifdef USB_DEBUG
104 static int uchcom_debug = 0;
105 
106 static SYSCTL_NODE(_hw_usb, OID_AUTO, uchcom, CTLFLAG_RW, 0, "USB uchcom");
107 SYSCTL_INT(_hw_usb_uchcom, OID_AUTO, debug, CTLFLAG_RW,
108     &uchcom_debug, 0, "uchcom debug level");
109 #endif
110 
111 #define	UCHCOM_IFACE_INDEX	0
112 #define	UCHCOM_CONFIG_INDEX	0
113 
114 #define	UCHCOM_REV_CH340	0x0250
115 #define	UCHCOM_INPUT_BUF_SIZE	8
116 
117 #define	UCHCOM_REQ_GET_VERSION	0x5F
118 #define	UCHCOM_REQ_READ_REG	0x95
119 #define	UCHCOM_REQ_WRITE_REG	0x9A
120 #define	UCHCOM_REQ_RESET	0xA1
121 #define	UCHCOM_REQ_SET_DTRRTS	0xA4
122 
123 #define	UCHCOM_REG_STAT1	0x06
124 #define	UCHCOM_REG_STAT2	0x07
125 #define	UCHCOM_REG_BPS_PRE	0x12
126 #define	UCHCOM_REG_BPS_DIV	0x13
127 #define	UCHCOM_REG_BPS_MOD	0x14
128 #define	UCHCOM_REG_BPS_PAD	0x0F
129 #define	UCHCOM_REG_BREAK1	0x05
130 #define	UCHCOM_REG_BREAK2	0x18
131 #define	UCHCOM_REG_LCR1		0x18
132 #define	UCHCOM_REG_LCR2		0x25
133 
134 #define	UCHCOM_VER_20		0x20
135 
136 #define	UCHCOM_BASE_UNKNOWN	0
137 #define	UCHCOM_BPS_MOD_BASE	20000000
138 #define	UCHCOM_BPS_MOD_BASE_OFS	1100
139 
140 #define	UCHCOM_DTR_MASK		0x20
141 #define	UCHCOM_RTS_MASK		0x40
142 
143 #define	UCHCOM_BRK1_MASK	0x01
144 #define	UCHCOM_BRK2_MASK	0x40
145 
146 #define	UCHCOM_LCR1_MASK	0xAF
147 #define	UCHCOM_LCR2_MASK	0x07
148 #define	UCHCOM_LCR1_PARENB	0x80
149 #define	UCHCOM_LCR2_PAREVEN	0x07
150 #define	UCHCOM_LCR2_PARODD	0x06
151 #define	UCHCOM_LCR2_PARMARK	0x05
152 #define	UCHCOM_LCR2_PARSPACE	0x04
153 
154 #define	UCHCOM_INTR_STAT1	0x02
155 #define	UCHCOM_INTR_STAT2	0x03
156 #define	UCHCOM_INTR_LEAST	4
157 
158 #define	UCHCOM_BULK_BUF_SIZE 1024	/* bytes */
159 
160 enum {
161 	UCHCOM_BULK_DT_WR,
162 	UCHCOM_BULK_DT_RD,
163 	UCHCOM_INTR_DT_RD,
164 	UCHCOM_N_TRANSFER,
165 };
166 
167 struct uchcom_softc {
168 	struct ucom_super_softc sc_super_ucom;
169 	struct ucom_softc sc_ucom;
170 
171 	struct usb_xfer *sc_xfer[UCHCOM_N_TRANSFER];
172 	struct usb_device *sc_udev;
173 	struct mtx sc_mtx;
174 
175 	uint8_t	sc_dtr;			/* local copy */
176 	uint8_t	sc_rts;			/* local copy */
177 	uint8_t	sc_version;
178 	uint8_t	sc_msr;
179 	uint8_t	sc_lsr;			/* local status register */
180 };
181 
182 struct uchcom_divider {
183 	uint8_t	dv_prescaler;
184 	uint8_t	dv_div;
185 	uint8_t	dv_mod;
186 };
187 
188 struct uchcom_divider_record {
189 	uint32_t dvr_high;
190 	uint32_t dvr_low;
191 	uint32_t dvr_base_clock;
192 	struct uchcom_divider dvr_divider;
193 };
194 
195 static const struct uchcom_divider_record dividers[] =
196 {
197 	{307200, 307200, UCHCOM_BASE_UNKNOWN, {7, 0xD9, 0}},
198 	{921600, 921600, UCHCOM_BASE_UNKNOWN, {7, 0xF3, 0}},
199 	{2999999, 23530, 6000000, {3, 0, 0}},
200 	{23529, 2942, 750000, {2, 0, 0}},
201 	{2941, 368, 93750, {1, 0, 0}},
202 	{367, 1, 11719, {0, 0, 0}},
203 };
204 
205 #define	NUM_DIVIDERS	(sizeof (dividers) / sizeof (dividers[0]))
206 
207 static const STRUCT_USB_HOST_ID uchcom_devs[] = {
208 	{USB_VPI(USB_VENDOR_WCH, USB_PRODUCT_WCH_CH341SER, 0)},
209 	{USB_VPI(USB_VENDOR_WCH2, USB_PRODUCT_WCH2_CH341SER, 0)},
210 	{USB_VPI(USB_VENDOR_WCH2, USB_PRODUCT_WCH2_CH341SER_2, 0)},
211 };
212 
213 /* protypes */
214 
215 static void	uchcom_free(struct ucom_softc *);
216 static int	uchcom_pre_param(struct ucom_softc *, struct termios *);
217 static void	uchcom_cfg_get_status(struct ucom_softc *, uint8_t *,
218 		    uint8_t *);
219 static void	uchcom_cfg_open(struct ucom_softc *ucom);
220 static void	uchcom_cfg_param(struct ucom_softc *, struct termios *);
221 static void	uchcom_cfg_set_break(struct ucom_softc *, uint8_t);
222 static void	uchcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
223 static void	uchcom_cfg_set_rts(struct ucom_softc *, uint8_t);
224 static void	uchcom_start_read(struct ucom_softc *);
225 static void	uchcom_start_write(struct ucom_softc *);
226 static void	uchcom_stop_read(struct ucom_softc *);
227 static void	uchcom_stop_write(struct ucom_softc *);
228 static void	uchcom_update_version(struct uchcom_softc *);
229 static void	uchcom_convert_status(struct uchcom_softc *, uint8_t);
230 static void	uchcom_update_status(struct uchcom_softc *);
231 static void	uchcom_set_dtr_rts(struct uchcom_softc *);
232 static int	uchcom_calc_divider_settings(struct uchcom_divider *, uint32_t);
233 static void	uchcom_set_baudrate(struct uchcom_softc *, uint32_t);
234 static void	uchcom_poll(struct ucom_softc *ucom);
235 
236 static device_probe_t uchcom_probe;
237 static device_attach_t uchcom_attach;
238 static device_detach_t uchcom_detach;
239 static void uchcom_free_softc(struct uchcom_softc *);
240 
241 static usb_callback_t uchcom_intr_callback;
242 static usb_callback_t uchcom_write_callback;
243 static usb_callback_t uchcom_read_callback;
244 
245 static const struct usb_config uchcom_config_data[UCHCOM_N_TRANSFER] = {
246 
247 	[UCHCOM_BULK_DT_WR] = {
248 		.type = UE_BULK,
249 		.endpoint = UE_ADDR_ANY,
250 		.direction = UE_DIR_OUT,
251 		.bufsize = UCHCOM_BULK_BUF_SIZE,
252 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
253 		.callback = &uchcom_write_callback,
254 	},
255 
256 	[UCHCOM_BULK_DT_RD] = {
257 		.type = UE_BULK,
258 		.endpoint = UE_ADDR_ANY,
259 		.direction = UE_DIR_IN,
260 		.bufsize = UCHCOM_BULK_BUF_SIZE,
261 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
262 		.callback = &uchcom_read_callback,
263 	},
264 
265 	[UCHCOM_INTR_DT_RD] = {
266 		.type = UE_INTERRUPT,
267 		.endpoint = UE_ADDR_ANY,
268 		.direction = UE_DIR_IN,
269 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
270 		.bufsize = 0,	/* use wMaxPacketSize */
271 		.callback = &uchcom_intr_callback,
272 	},
273 };
274 
275 static struct ucom_callback uchcom_callback = {
276 	.ucom_cfg_get_status = &uchcom_cfg_get_status,
277 	.ucom_cfg_set_dtr = &uchcom_cfg_set_dtr,
278 	.ucom_cfg_set_rts = &uchcom_cfg_set_rts,
279 	.ucom_cfg_set_break = &uchcom_cfg_set_break,
280 	.ucom_cfg_open = &uchcom_cfg_open,
281 	.ucom_cfg_param = &uchcom_cfg_param,
282 	.ucom_pre_param = &uchcom_pre_param,
283 	.ucom_start_read = &uchcom_start_read,
284 	.ucom_stop_read = &uchcom_stop_read,
285 	.ucom_start_write = &uchcom_start_write,
286 	.ucom_stop_write = &uchcom_stop_write,
287 	.ucom_poll = &uchcom_poll,
288 	.ucom_free = &uchcom_free,
289 };
290 
291 /* ----------------------------------------------------------------------
292  * driver entry points
293  */
294 
295 static int
296 uchcom_probe(device_t dev)
297 {
298 	struct usb_attach_arg *uaa = device_get_ivars(dev);
299 
300 	DPRINTFN(11, "\n");
301 
302 	if (uaa->usb_mode != USB_MODE_HOST) {
303 		return (ENXIO);
304 	}
305 	if (uaa->info.bConfigIndex != UCHCOM_CONFIG_INDEX) {
306 		return (ENXIO);
307 	}
308 	if (uaa->info.bIfaceIndex != UCHCOM_IFACE_INDEX) {
309 		return (ENXIO);
310 	}
311 	return (usbd_lookup_id_by_uaa(uchcom_devs, sizeof(uchcom_devs), uaa));
312 }
313 
314 static int
315 uchcom_attach(device_t dev)
316 {
317 	struct uchcom_softc *sc = device_get_softc(dev);
318 	struct usb_attach_arg *uaa = device_get_ivars(dev);
319 	int error;
320 	uint8_t iface_index;
321 
322 	DPRINTFN(11, "\n");
323 
324 	device_set_usb_desc(dev);
325 	mtx_init(&sc->sc_mtx, "uchcom", NULL, MTX_DEF);
326 	ucom_ref(&sc->sc_super_ucom);
327 
328 	sc->sc_udev = uaa->device;
329 
330 	switch (uaa->info.bcdDevice) {
331 	case UCHCOM_REV_CH340:
332 		device_printf(dev, "CH340 detected\n");
333 		break;
334 	default:
335 		device_printf(dev, "CH341 detected\n");
336 		break;
337 	}
338 
339 	iface_index = UCHCOM_IFACE_INDEX;
340 	error = usbd_transfer_setup(uaa->device,
341 	    &iface_index, sc->sc_xfer, uchcom_config_data,
342 	    UCHCOM_N_TRANSFER, sc, &sc->sc_mtx);
343 
344 	if (error) {
345 		DPRINTF("one or more missing USB endpoints, "
346 		    "error=%s\n", usbd_errstr(error));
347 		goto detach;
348 	}
349 
350 	/* clear stall at first run */
351 	mtx_lock(&sc->sc_mtx);
352 	usbd_xfer_set_stall(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
353 	usbd_xfer_set_stall(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
354 	mtx_unlock(&sc->sc_mtx);
355 
356 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
357 	    &uchcom_callback, &sc->sc_mtx);
358 	if (error) {
359 		goto detach;
360 	}
361 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
362 
363 	return (0);
364 
365 detach:
366 	uchcom_detach(dev);
367 	return (ENXIO);
368 }
369 
370 static int
371 uchcom_detach(device_t dev)
372 {
373 	struct uchcom_softc *sc = device_get_softc(dev);
374 
375 	DPRINTFN(11, "\n");
376 
377 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
378 	usbd_transfer_unsetup(sc->sc_xfer, UCHCOM_N_TRANSFER);
379 
380 	device_claim_softc(dev);
381 
382 	uchcom_free_softc(sc);
383 
384 	return (0);
385 }
386 
387 UCOM_UNLOAD_DRAIN(uchcom);
388 
389 static void
390 uchcom_free_softc(struct uchcom_softc *sc)
391 {
392 	if (ucom_unref(&sc->sc_super_ucom)) {
393 		mtx_destroy(&sc->sc_mtx);
394 		device_free_softc(sc);
395 	}
396 }
397 
398 static void
399 uchcom_free(struct ucom_softc *ucom)
400 {
401 	uchcom_free_softc(ucom->sc_parent);
402 }
403 
404 /* ----------------------------------------------------------------------
405  * low level i/o
406  */
407 
408 static void
409 uchcom_ctrl_write(struct uchcom_softc *sc, uint8_t reqno,
410     uint16_t value, uint16_t index)
411 {
412 	struct usb_device_request req;
413 
414 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
415 	req.bRequest = reqno;
416 	USETW(req.wValue, value);
417 	USETW(req.wIndex, index);
418 	USETW(req.wLength, 0);
419 
420 	ucom_cfg_do_request(sc->sc_udev,
421 	    &sc->sc_ucom, &req, NULL, 0, 1000);
422 }
423 
424 static void
425 uchcom_ctrl_read(struct uchcom_softc *sc, uint8_t reqno,
426     uint16_t value, uint16_t index, void *buf, uint16_t buflen)
427 {
428 	struct usb_device_request req;
429 
430 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
431 	req.bRequest = reqno;
432 	USETW(req.wValue, value);
433 	USETW(req.wIndex, index);
434 	USETW(req.wLength, buflen);
435 
436 	ucom_cfg_do_request(sc->sc_udev,
437 	    &sc->sc_ucom, &req, buf, USB_SHORT_XFER_OK, 1000);
438 }
439 
440 static void
441 uchcom_write_reg(struct uchcom_softc *sc,
442     uint8_t reg1, uint8_t val1, uint8_t reg2, uint8_t val2)
443 {
444 	DPRINTF("0x%02X<-0x%02X, 0x%02X<-0x%02X\n",
445 	    (unsigned)reg1, (unsigned)val1,
446 	    (unsigned)reg2, (unsigned)val2);
447 	uchcom_ctrl_write(
448 	    sc, UCHCOM_REQ_WRITE_REG,
449 	    reg1 | ((uint16_t)reg2 << 8), val1 | ((uint16_t)val2 << 8));
450 }
451 
452 static void
453 uchcom_read_reg(struct uchcom_softc *sc,
454     uint8_t reg1, uint8_t *rval1, uint8_t reg2, uint8_t *rval2)
455 {
456 	uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
457 
458 	uchcom_ctrl_read(
459 	    sc, UCHCOM_REQ_READ_REG,
460 	    reg1 | ((uint16_t)reg2 << 8), 0, buf, sizeof(buf));
461 
462 	DPRINTF("0x%02X->0x%02X, 0x%02X->0x%02X\n",
463 	    (unsigned)reg1, (unsigned)buf[0],
464 	    (unsigned)reg2, (unsigned)buf[1]);
465 
466 	if (rval1)
467 		*rval1 = buf[0];
468 	if (rval2)
469 		*rval2 = buf[1];
470 }
471 
472 static void
473 uchcom_get_version(struct uchcom_softc *sc, uint8_t *rver)
474 {
475 	uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
476 
477 	uchcom_ctrl_read(sc, UCHCOM_REQ_GET_VERSION, 0, 0, buf, sizeof(buf));
478 
479 	if (rver)
480 		*rver = buf[0];
481 }
482 
483 static void
484 uchcom_get_status(struct uchcom_softc *sc, uint8_t *rval)
485 {
486 	uchcom_read_reg(sc, UCHCOM_REG_STAT1, rval, UCHCOM_REG_STAT2, NULL);
487 }
488 
489 static void
490 uchcom_set_dtr_rts_10(struct uchcom_softc *sc, uint8_t val)
491 {
492 	uchcom_write_reg(sc, UCHCOM_REG_STAT1, val, UCHCOM_REG_STAT1, val);
493 }
494 
495 static void
496 uchcom_set_dtr_rts_20(struct uchcom_softc *sc, uint8_t val)
497 {
498 	uchcom_ctrl_write(sc, UCHCOM_REQ_SET_DTRRTS, val, 0);
499 }
500 
501 
502 /* ----------------------------------------------------------------------
503  * middle layer
504  */
505 
506 static void
507 uchcom_update_version(struct uchcom_softc *sc)
508 {
509 	uchcom_get_version(sc, &sc->sc_version);
510 }
511 
512 static void
513 uchcom_convert_status(struct uchcom_softc *sc, uint8_t cur)
514 {
515 	sc->sc_dtr = !(cur & UCHCOM_DTR_MASK);
516 	sc->sc_rts = !(cur & UCHCOM_RTS_MASK);
517 
518 	cur = ~cur & 0x0F;
519 	sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur);
520 }
521 
522 static void
523 uchcom_update_status(struct uchcom_softc *sc)
524 {
525 	uint8_t cur;
526 
527 	uchcom_get_status(sc, &cur);
528 	uchcom_convert_status(sc, cur);
529 }
530 
531 
532 static void
533 uchcom_set_dtr_rts(struct uchcom_softc *sc)
534 {
535 	uint8_t val = 0;
536 
537 	if (sc->sc_dtr)
538 		val |= UCHCOM_DTR_MASK;
539 	if (sc->sc_rts)
540 		val |= UCHCOM_RTS_MASK;
541 
542 	if (sc->sc_version < UCHCOM_VER_20)
543 		uchcom_set_dtr_rts_10(sc, ~val);
544 	else
545 		uchcom_set_dtr_rts_20(sc, ~val);
546 }
547 
548 static void
549 uchcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
550 {
551 	struct uchcom_softc *sc = ucom->sc_parent;
552 	uint8_t brk1;
553 	uint8_t brk2;
554 
555 	uchcom_read_reg(sc, UCHCOM_REG_BREAK1, &brk1, UCHCOM_REG_BREAK2, &brk2);
556 	if (onoff) {
557 		/* on - clear bits */
558 		brk1 &= ~UCHCOM_BRK1_MASK;
559 		brk2 &= ~UCHCOM_BRK2_MASK;
560 	} else {
561 		/* off - set bits */
562 		brk1 |= UCHCOM_BRK1_MASK;
563 		brk2 |= UCHCOM_BRK2_MASK;
564 	}
565 	uchcom_write_reg(sc, UCHCOM_REG_BREAK1, brk1, UCHCOM_REG_BREAK2, brk2);
566 }
567 
568 static int
569 uchcom_calc_divider_settings(struct uchcom_divider *dp, uint32_t rate)
570 {
571 	const struct uchcom_divider_record *rp;
572 	uint32_t div;
573 	uint32_t rem;
574 	uint32_t mod;
575 	uint8_t i;
576 
577 	/* find record */
578 	for (i = 0; i != NUM_DIVIDERS; i++) {
579 		if (dividers[i].dvr_high >= rate &&
580 		    dividers[i].dvr_low <= rate) {
581 			rp = &dividers[i];
582 			goto found;
583 		}
584 	}
585 	return (-1);
586 
587 found:
588 	dp->dv_prescaler = rp->dvr_divider.dv_prescaler;
589 	if (rp->dvr_base_clock == UCHCOM_BASE_UNKNOWN)
590 		dp->dv_div = rp->dvr_divider.dv_div;
591 	else {
592 		div = rp->dvr_base_clock / rate;
593 		rem = rp->dvr_base_clock % rate;
594 		if (div == 0 || div >= 0xFF)
595 			return (-1);
596 		if ((rem << 1) >= rate)
597 			div += 1;
598 		dp->dv_div = (uint8_t)-div;
599 	}
600 
601 	mod = (UCHCOM_BPS_MOD_BASE / rate) + UCHCOM_BPS_MOD_BASE_OFS;
602 	mod = mod + (mod / 2);
603 
604 	dp->dv_mod = (mod + 0xFF) / 0x100;
605 
606 	return (0);
607 }
608 
609 static void
610 uchcom_set_baudrate(struct uchcom_softc *sc, uint32_t rate)
611 {
612 	struct uchcom_divider dv;
613 
614 	if (uchcom_calc_divider_settings(&dv, rate))
615 		return;
616 
617 	uchcom_write_reg(sc,
618 	    UCHCOM_REG_BPS_PRE, dv.dv_prescaler,
619 	    UCHCOM_REG_BPS_DIV, dv.dv_div);
620 	uchcom_write_reg(sc,
621 	    UCHCOM_REG_BPS_MOD, dv.dv_mod,
622 	    UCHCOM_REG_BPS_PAD, 0);
623 }
624 
625 /* ----------------------------------------------------------------------
626  * methods for ucom
627  */
628 static void
629 uchcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
630 {
631 	struct uchcom_softc *sc = ucom->sc_parent;
632 
633 	DPRINTF("\n");
634 
635 	*lsr = sc->sc_lsr;
636 	*msr = sc->sc_msr;
637 }
638 
639 static void
640 uchcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
641 {
642 	struct uchcom_softc *sc = ucom->sc_parent;
643 
644 	DPRINTF("onoff = %d\n", onoff);
645 
646 	sc->sc_dtr = onoff;
647 	uchcom_set_dtr_rts(sc);
648 }
649 
650 static void
651 uchcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
652 {
653 	struct uchcom_softc *sc = ucom->sc_parent;
654 
655 	DPRINTF("onoff = %d\n", onoff);
656 
657 	sc->sc_rts = onoff;
658 	uchcom_set_dtr_rts(sc);
659 }
660 
661 static void
662 uchcom_cfg_open(struct ucom_softc *ucom)
663 {
664 	struct uchcom_softc *sc = ucom->sc_parent;
665 
666 	DPRINTF("\n");
667 
668 	uchcom_update_version(sc);
669 	uchcom_update_status(sc);
670 }
671 
672 static int
673 uchcom_pre_param(struct ucom_softc *ucom, struct termios *t)
674 {
675 	struct uchcom_divider dv;
676 
677 	switch (t->c_cflag & CSIZE) {
678 	case CS8:
679 		break;
680 	default:
681 		return (EIO);
682 	}
683 
684 	if (uchcom_calc_divider_settings(&dv, t->c_ospeed)) {
685 		return (EIO);
686 	}
687 	return (0);			/* success */
688 }
689 
690 static void
691 uchcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
692 {
693 	struct uchcom_softc *sc = ucom->sc_parent;
694 
695 	uchcom_get_version(sc, 0);
696 	uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0, 0);
697 	uchcom_set_baudrate(sc, t->c_ospeed);
698 	uchcom_read_reg(sc, 0x18, 0, 0x25, 0);
699 	uchcom_write_reg(sc, 0x18, 0x50, 0x25, 0x00);
700 	uchcom_update_status(sc);
701 	uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0x501f, 0xd90a);
702 	uchcom_set_baudrate(sc, t->c_ospeed);
703 	uchcom_set_dtr_rts(sc);
704 	uchcom_update_status(sc);
705 }
706 
707 static void
708 uchcom_start_read(struct ucom_softc *ucom)
709 {
710 	struct uchcom_softc *sc = ucom->sc_parent;
711 
712 	/* start interrupt endpoint */
713 	usbd_transfer_start(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
714 
715 	/* start read endpoint */
716 	usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
717 }
718 
719 static void
720 uchcom_stop_read(struct ucom_softc *ucom)
721 {
722 	struct uchcom_softc *sc = ucom->sc_parent;
723 
724 	/* stop interrupt endpoint */
725 	usbd_transfer_stop(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
726 
727 	/* stop read endpoint */
728 	usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
729 }
730 
731 static void
732 uchcom_start_write(struct ucom_softc *ucom)
733 {
734 	struct uchcom_softc *sc = ucom->sc_parent;
735 
736 	usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
737 }
738 
739 static void
740 uchcom_stop_write(struct ucom_softc *ucom)
741 {
742 	struct uchcom_softc *sc = ucom->sc_parent;
743 
744 	usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
745 }
746 
747 /* ----------------------------------------------------------------------
748  * callback when the modem status is changed.
749  */
750 static void
751 uchcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
752 {
753 	struct uchcom_softc *sc = usbd_xfer_softc(xfer);
754 	struct usb_page_cache *pc;
755 	uint8_t buf[UCHCOM_INTR_LEAST];
756 	int actlen;
757 
758 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
759 
760 	switch (USB_GET_STATE(xfer)) {
761 	case USB_ST_TRANSFERRED:
762 
763 		DPRINTF("actlen = %u\n", actlen);
764 
765 		if (actlen >= UCHCOM_INTR_LEAST) {
766 			pc = usbd_xfer_get_frame(xfer, 0);
767 			usbd_copy_out(pc, 0, buf, UCHCOM_INTR_LEAST);
768 
769 			DPRINTF("data = 0x%02X 0x%02X 0x%02X 0x%02X\n",
770 			    (unsigned)buf[0], (unsigned)buf[1],
771 			    (unsigned)buf[2], (unsigned)buf[3]);
772 
773 			uchcom_convert_status(sc, buf[UCHCOM_INTR_STAT1]);
774 			ucom_status_change(&sc->sc_ucom);
775 		}
776 	case USB_ST_SETUP:
777 tr_setup:
778 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
779 		usbd_transfer_submit(xfer);
780 		break;
781 
782 	default:			/* Error */
783 		if (error != USB_ERR_CANCELLED) {
784 			/* try to clear stall first */
785 			usbd_xfer_set_stall(xfer);
786 			goto tr_setup;
787 		}
788 		break;
789 	}
790 }
791 
792 static void
793 uchcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
794 {
795 	struct uchcom_softc *sc = usbd_xfer_softc(xfer);
796 	struct usb_page_cache *pc;
797 	uint32_t actlen;
798 
799 	switch (USB_GET_STATE(xfer)) {
800 	case USB_ST_SETUP:
801 	case USB_ST_TRANSFERRED:
802 tr_setup:
803 		pc = usbd_xfer_get_frame(xfer, 0);
804 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
805 		    usbd_xfer_max_len(xfer), &actlen)) {
806 
807 			DPRINTF("actlen = %d\n", actlen);
808 
809 			usbd_xfer_set_frame_len(xfer, 0, actlen);
810 			usbd_transfer_submit(xfer);
811 		}
812 		break;
813 
814 	default:			/* Error */
815 		if (error != USB_ERR_CANCELLED) {
816 			/* try to clear stall first */
817 			usbd_xfer_set_stall(xfer);
818 			goto tr_setup;
819 		}
820 		break;
821 	}
822 }
823 
824 static void
825 uchcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
826 {
827 	struct uchcom_softc *sc = usbd_xfer_softc(xfer);
828 	struct usb_page_cache *pc;
829 	int actlen;
830 
831 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
832 
833 	switch (USB_GET_STATE(xfer)) {
834 	case USB_ST_TRANSFERRED:
835 
836 		if (actlen > 0) {
837 			pc = usbd_xfer_get_frame(xfer, 0);
838 			ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
839 		}
840 
841 	case USB_ST_SETUP:
842 tr_setup:
843 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
844 		usbd_transfer_submit(xfer);
845 		break;
846 
847 	default:			/* Error */
848 		if (error != USB_ERR_CANCELLED) {
849 			/* try to clear stall first */
850 			usbd_xfer_set_stall(xfer);
851 			goto tr_setup;
852 		}
853 		break;
854 	}
855 }
856 
857 static void
858 uchcom_poll(struct ucom_softc *ucom)
859 {
860 	struct uchcom_softc *sc = ucom->sc_parent;
861 	usbd_transfer_poll(sc->sc_xfer, UCHCOM_N_TRANSFER);
862 }
863 
864 static device_method_t uchcom_methods[] = {
865 	/* Device interface */
866 	DEVMETHOD(device_probe, uchcom_probe),
867 	DEVMETHOD(device_attach, uchcom_attach),
868 	DEVMETHOD(device_detach, uchcom_detach),
869 	DEVMETHOD_END
870 };
871 
872 static driver_t uchcom_driver = {
873 	.name = "uchcom",
874 	.methods = uchcom_methods,
875 	.size = sizeof(struct uchcom_softc)
876 };
877 
878 static devclass_t uchcom_devclass;
879 
880 DRIVER_MODULE(uchcom, uhub, uchcom_driver, uchcom_devclass, NULL, 0);
881 MODULE_DEPEND(uchcom, ucom, 1, 1, 1);
882 MODULE_DEPEND(uchcom, usb, 1, 1, 1);
883 MODULE_VERSION(uchcom, 1);
884