xref: /freebsd/sys/dev/usb/serial/uchcom.c (revision b7d683e608ac894e75b2de7eac5eaa083650de54)
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 };
211 
212 /* protypes */
213 
214 static void	uchcom_free(struct ucom_softc *);
215 static int	uchcom_pre_param(struct ucom_softc *, struct termios *);
216 static void	uchcom_cfg_get_status(struct ucom_softc *, uint8_t *,
217 		    uint8_t *);
218 static void	uchcom_cfg_open(struct ucom_softc *ucom);
219 static void	uchcom_cfg_param(struct ucom_softc *, struct termios *);
220 static void	uchcom_cfg_set_break(struct ucom_softc *, uint8_t);
221 static void	uchcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
222 static void	uchcom_cfg_set_rts(struct ucom_softc *, uint8_t);
223 static void	uchcom_start_read(struct ucom_softc *);
224 static void	uchcom_start_write(struct ucom_softc *);
225 static void	uchcom_stop_read(struct ucom_softc *);
226 static void	uchcom_stop_write(struct ucom_softc *);
227 static void	uchcom_update_version(struct uchcom_softc *);
228 static void	uchcom_convert_status(struct uchcom_softc *, uint8_t);
229 static void	uchcom_update_status(struct uchcom_softc *);
230 static void	uchcom_set_dtr_rts(struct uchcom_softc *);
231 static int	uchcom_calc_divider_settings(struct uchcom_divider *, uint32_t);
232 static void	uchcom_set_baudrate(struct uchcom_softc *, uint32_t);
233 static void	uchcom_poll(struct ucom_softc *ucom);
234 
235 static device_probe_t uchcom_probe;
236 static device_attach_t uchcom_attach;
237 static device_detach_t uchcom_detach;
238 static void uchcom_free_softc(struct uchcom_softc *);
239 
240 static usb_callback_t uchcom_intr_callback;
241 static usb_callback_t uchcom_write_callback;
242 static usb_callback_t uchcom_read_callback;
243 
244 static const struct usb_config uchcom_config_data[UCHCOM_N_TRANSFER] = {
245 
246 	[UCHCOM_BULK_DT_WR] = {
247 		.type = UE_BULK,
248 		.endpoint = UE_ADDR_ANY,
249 		.direction = UE_DIR_OUT,
250 		.bufsize = UCHCOM_BULK_BUF_SIZE,
251 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
252 		.callback = &uchcom_write_callback,
253 	},
254 
255 	[UCHCOM_BULK_DT_RD] = {
256 		.type = UE_BULK,
257 		.endpoint = UE_ADDR_ANY,
258 		.direction = UE_DIR_IN,
259 		.bufsize = UCHCOM_BULK_BUF_SIZE,
260 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
261 		.callback = &uchcom_read_callback,
262 	},
263 
264 	[UCHCOM_INTR_DT_RD] = {
265 		.type = UE_INTERRUPT,
266 		.endpoint = UE_ADDR_ANY,
267 		.direction = UE_DIR_IN,
268 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
269 		.bufsize = 0,	/* use wMaxPacketSize */
270 		.callback = &uchcom_intr_callback,
271 	},
272 };
273 
274 static struct ucom_callback uchcom_callback = {
275 	.ucom_cfg_get_status = &uchcom_cfg_get_status,
276 	.ucom_cfg_set_dtr = &uchcom_cfg_set_dtr,
277 	.ucom_cfg_set_rts = &uchcom_cfg_set_rts,
278 	.ucom_cfg_set_break = &uchcom_cfg_set_break,
279 	.ucom_cfg_open = &uchcom_cfg_open,
280 	.ucom_cfg_param = &uchcom_cfg_param,
281 	.ucom_pre_param = &uchcom_pre_param,
282 	.ucom_start_read = &uchcom_start_read,
283 	.ucom_stop_read = &uchcom_stop_read,
284 	.ucom_start_write = &uchcom_start_write,
285 	.ucom_stop_write = &uchcom_stop_write,
286 	.ucom_poll = &uchcom_poll,
287 	.ucom_free = &uchcom_free,
288 };
289 
290 /* ----------------------------------------------------------------------
291  * driver entry points
292  */
293 
294 static int
295 uchcom_probe(device_t dev)
296 {
297 	struct usb_attach_arg *uaa = device_get_ivars(dev);
298 
299 	DPRINTFN(11, "\n");
300 
301 	if (uaa->usb_mode != USB_MODE_HOST) {
302 		return (ENXIO);
303 	}
304 	if (uaa->info.bConfigIndex != UCHCOM_CONFIG_INDEX) {
305 		return (ENXIO);
306 	}
307 	if (uaa->info.bIfaceIndex != UCHCOM_IFACE_INDEX) {
308 		return (ENXIO);
309 	}
310 	return (usbd_lookup_id_by_uaa(uchcom_devs, sizeof(uchcom_devs), uaa));
311 }
312 
313 static int
314 uchcom_attach(device_t dev)
315 {
316 	struct uchcom_softc *sc = device_get_softc(dev);
317 	struct usb_attach_arg *uaa = device_get_ivars(dev);
318 	int error;
319 	uint8_t iface_index;
320 
321 	DPRINTFN(11, "\n");
322 
323 	device_set_usb_desc(dev);
324 	mtx_init(&sc->sc_mtx, "uchcom", NULL, MTX_DEF);
325 	ucom_ref(&sc->sc_super_ucom);
326 
327 	sc->sc_udev = uaa->device;
328 
329 	switch (uaa->info.bcdDevice) {
330 	case UCHCOM_REV_CH340:
331 		device_printf(dev, "CH340 detected\n");
332 		break;
333 	default:
334 		device_printf(dev, "CH341 detected\n");
335 		break;
336 	}
337 
338 	iface_index = UCHCOM_IFACE_INDEX;
339 	error = usbd_transfer_setup(uaa->device,
340 	    &iface_index, sc->sc_xfer, uchcom_config_data,
341 	    UCHCOM_N_TRANSFER, sc, &sc->sc_mtx);
342 
343 	if (error) {
344 		DPRINTF("one or more missing USB endpoints, "
345 		    "error=%s\n", usbd_errstr(error));
346 		goto detach;
347 	}
348 
349 	/* clear stall at first run */
350 	mtx_lock(&sc->sc_mtx);
351 	usbd_xfer_set_stall(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
352 	usbd_xfer_set_stall(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
353 	mtx_unlock(&sc->sc_mtx);
354 
355 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
356 	    &uchcom_callback, &sc->sc_mtx);
357 	if (error) {
358 		goto detach;
359 	}
360 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
361 
362 	return (0);
363 
364 detach:
365 	uchcom_detach(dev);
366 	return (ENXIO);
367 }
368 
369 static int
370 uchcom_detach(device_t dev)
371 {
372 	struct uchcom_softc *sc = device_get_softc(dev);
373 
374 	DPRINTFN(11, "\n");
375 
376 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
377 	usbd_transfer_unsetup(sc->sc_xfer, UCHCOM_N_TRANSFER);
378 
379 	device_claim_softc(dev);
380 
381 	uchcom_free_softc(sc);
382 
383 	return (0);
384 }
385 
386 UCOM_UNLOAD_DRAIN(uchcom);
387 
388 static void
389 uchcom_free_softc(struct uchcom_softc *sc)
390 {
391 	if (ucom_unref(&sc->sc_super_ucom)) {
392 		mtx_destroy(&sc->sc_mtx);
393 		device_free_softc(sc);
394 	}
395 }
396 
397 static void
398 uchcom_free(struct ucom_softc *ucom)
399 {
400 	uchcom_free_softc(ucom->sc_parent);
401 }
402 
403 /* ----------------------------------------------------------------------
404  * low level i/o
405  */
406 
407 static void
408 uchcom_ctrl_write(struct uchcom_softc *sc, uint8_t reqno,
409     uint16_t value, uint16_t index)
410 {
411 	struct usb_device_request req;
412 
413 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
414 	req.bRequest = reqno;
415 	USETW(req.wValue, value);
416 	USETW(req.wIndex, index);
417 	USETW(req.wLength, 0);
418 
419 	ucom_cfg_do_request(sc->sc_udev,
420 	    &sc->sc_ucom, &req, NULL, 0, 1000);
421 }
422 
423 static void
424 uchcom_ctrl_read(struct uchcom_softc *sc, uint8_t reqno,
425     uint16_t value, uint16_t index, void *buf, uint16_t buflen)
426 {
427 	struct usb_device_request req;
428 
429 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
430 	req.bRequest = reqno;
431 	USETW(req.wValue, value);
432 	USETW(req.wIndex, index);
433 	USETW(req.wLength, buflen);
434 
435 	ucom_cfg_do_request(sc->sc_udev,
436 	    &sc->sc_ucom, &req, buf, USB_SHORT_XFER_OK, 1000);
437 }
438 
439 static void
440 uchcom_write_reg(struct uchcom_softc *sc,
441     uint8_t reg1, uint8_t val1, uint8_t reg2, uint8_t val2)
442 {
443 	DPRINTF("0x%02X<-0x%02X, 0x%02X<-0x%02X\n",
444 	    (unsigned)reg1, (unsigned)val1,
445 	    (unsigned)reg2, (unsigned)val2);
446 	uchcom_ctrl_write(
447 	    sc, UCHCOM_REQ_WRITE_REG,
448 	    reg1 | ((uint16_t)reg2 << 8), val1 | ((uint16_t)val2 << 8));
449 }
450 
451 static void
452 uchcom_read_reg(struct uchcom_softc *sc,
453     uint8_t reg1, uint8_t *rval1, uint8_t reg2, uint8_t *rval2)
454 {
455 	uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
456 
457 	uchcom_ctrl_read(
458 	    sc, UCHCOM_REQ_READ_REG,
459 	    reg1 | ((uint16_t)reg2 << 8), 0, buf, sizeof(buf));
460 
461 	DPRINTF("0x%02X->0x%02X, 0x%02X->0x%02X\n",
462 	    (unsigned)reg1, (unsigned)buf[0],
463 	    (unsigned)reg2, (unsigned)buf[1]);
464 
465 	if (rval1)
466 		*rval1 = buf[0];
467 	if (rval2)
468 		*rval2 = buf[1];
469 }
470 
471 static void
472 uchcom_get_version(struct uchcom_softc *sc, uint8_t *rver)
473 {
474 	uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
475 
476 	uchcom_ctrl_read(sc, UCHCOM_REQ_GET_VERSION, 0, 0, buf, sizeof(buf));
477 
478 	if (rver)
479 		*rver = buf[0];
480 }
481 
482 static void
483 uchcom_get_status(struct uchcom_softc *sc, uint8_t *rval)
484 {
485 	uchcom_read_reg(sc, UCHCOM_REG_STAT1, rval, UCHCOM_REG_STAT2, NULL);
486 }
487 
488 static void
489 uchcom_set_dtr_rts_10(struct uchcom_softc *sc, uint8_t val)
490 {
491 	uchcom_write_reg(sc, UCHCOM_REG_STAT1, val, UCHCOM_REG_STAT1, val);
492 }
493 
494 static void
495 uchcom_set_dtr_rts_20(struct uchcom_softc *sc, uint8_t val)
496 {
497 	uchcom_ctrl_write(sc, UCHCOM_REQ_SET_DTRRTS, val, 0);
498 }
499 
500 
501 /* ----------------------------------------------------------------------
502  * middle layer
503  */
504 
505 static void
506 uchcom_update_version(struct uchcom_softc *sc)
507 {
508 	uchcom_get_version(sc, &sc->sc_version);
509 }
510 
511 static void
512 uchcom_convert_status(struct uchcom_softc *sc, uint8_t cur)
513 {
514 	sc->sc_dtr = !(cur & UCHCOM_DTR_MASK);
515 	sc->sc_rts = !(cur & UCHCOM_RTS_MASK);
516 
517 	cur = ~cur & 0x0F;
518 	sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur);
519 }
520 
521 static void
522 uchcom_update_status(struct uchcom_softc *sc)
523 {
524 	uint8_t cur;
525 
526 	uchcom_get_status(sc, &cur);
527 	uchcom_convert_status(sc, cur);
528 }
529 
530 
531 static void
532 uchcom_set_dtr_rts(struct uchcom_softc *sc)
533 {
534 	uint8_t val = 0;
535 
536 	if (sc->sc_dtr)
537 		val |= UCHCOM_DTR_MASK;
538 	if (sc->sc_rts)
539 		val |= UCHCOM_RTS_MASK;
540 
541 	if (sc->sc_version < UCHCOM_VER_20)
542 		uchcom_set_dtr_rts_10(sc, ~val);
543 	else
544 		uchcom_set_dtr_rts_20(sc, ~val);
545 }
546 
547 static void
548 uchcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
549 {
550 	struct uchcom_softc *sc = ucom->sc_parent;
551 	uint8_t brk1;
552 	uint8_t brk2;
553 
554 	uchcom_read_reg(sc, UCHCOM_REG_BREAK1, &brk1, UCHCOM_REG_BREAK2, &brk2);
555 	if (onoff) {
556 		/* on - clear bits */
557 		brk1 &= ~UCHCOM_BRK1_MASK;
558 		brk2 &= ~UCHCOM_BRK2_MASK;
559 	} else {
560 		/* off - set bits */
561 		brk1 |= UCHCOM_BRK1_MASK;
562 		brk2 |= UCHCOM_BRK2_MASK;
563 	}
564 	uchcom_write_reg(sc, UCHCOM_REG_BREAK1, brk1, UCHCOM_REG_BREAK2, brk2);
565 }
566 
567 static int
568 uchcom_calc_divider_settings(struct uchcom_divider *dp, uint32_t rate)
569 {
570 	const struct uchcom_divider_record *rp;
571 	uint32_t div;
572 	uint32_t rem;
573 	uint32_t mod;
574 	uint8_t i;
575 
576 	/* find record */
577 	for (i = 0; i != NUM_DIVIDERS; i++) {
578 		if (dividers[i].dvr_high >= rate &&
579 		    dividers[i].dvr_low <= rate) {
580 			rp = &dividers[i];
581 			goto found;
582 		}
583 	}
584 	return (-1);
585 
586 found:
587 	dp->dv_prescaler = rp->dvr_divider.dv_prescaler;
588 	if (rp->dvr_base_clock == UCHCOM_BASE_UNKNOWN)
589 		dp->dv_div = rp->dvr_divider.dv_div;
590 	else {
591 		div = rp->dvr_base_clock / rate;
592 		rem = rp->dvr_base_clock % rate;
593 		if (div == 0 || div >= 0xFF)
594 			return (-1);
595 		if ((rem << 1) >= rate)
596 			div += 1;
597 		dp->dv_div = (uint8_t)-div;
598 	}
599 
600 	mod = (UCHCOM_BPS_MOD_BASE / rate) + UCHCOM_BPS_MOD_BASE_OFS;
601 	mod = mod + (mod / 2);
602 
603 	dp->dv_mod = (mod + 0xFF) / 0x100;
604 
605 	return (0);
606 }
607 
608 static void
609 uchcom_set_baudrate(struct uchcom_softc *sc, uint32_t rate)
610 {
611 	struct uchcom_divider dv;
612 
613 	if (uchcom_calc_divider_settings(&dv, rate))
614 		return;
615 
616 	uchcom_write_reg(sc,
617 	    UCHCOM_REG_BPS_PRE, dv.dv_prescaler,
618 	    UCHCOM_REG_BPS_DIV, dv.dv_div);
619 	uchcom_write_reg(sc,
620 	    UCHCOM_REG_BPS_MOD, dv.dv_mod,
621 	    UCHCOM_REG_BPS_PAD, 0);
622 }
623 
624 /* ----------------------------------------------------------------------
625  * methods for ucom
626  */
627 static void
628 uchcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
629 {
630 	struct uchcom_softc *sc = ucom->sc_parent;
631 
632 	DPRINTF("\n");
633 
634 	*lsr = sc->sc_lsr;
635 	*msr = sc->sc_msr;
636 }
637 
638 static void
639 uchcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
640 {
641 	struct uchcom_softc *sc = ucom->sc_parent;
642 
643 	DPRINTF("onoff = %d\n", onoff);
644 
645 	sc->sc_dtr = onoff;
646 	uchcom_set_dtr_rts(sc);
647 }
648 
649 static void
650 uchcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
651 {
652 	struct uchcom_softc *sc = ucom->sc_parent;
653 
654 	DPRINTF("onoff = %d\n", onoff);
655 
656 	sc->sc_rts = onoff;
657 	uchcom_set_dtr_rts(sc);
658 }
659 
660 static void
661 uchcom_cfg_open(struct ucom_softc *ucom)
662 {
663 	struct uchcom_softc *sc = ucom->sc_parent;
664 
665 	DPRINTF("\n");
666 
667 	uchcom_update_version(sc);
668 	uchcom_update_status(sc);
669 }
670 
671 static int
672 uchcom_pre_param(struct ucom_softc *ucom, struct termios *t)
673 {
674 	struct uchcom_divider dv;
675 
676 	switch (t->c_cflag & CSIZE) {
677 	case CS8:
678 		break;
679 	default:
680 		return (EIO);
681 	}
682 
683 	if (uchcom_calc_divider_settings(&dv, t->c_ospeed)) {
684 		return (EIO);
685 	}
686 	return (0);			/* success */
687 }
688 
689 static void
690 uchcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
691 {
692 	struct uchcom_softc *sc = ucom->sc_parent;
693 
694 	uchcom_get_version(sc, 0);
695 	uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0, 0);
696 	uchcom_set_baudrate(sc, t->c_ospeed);
697 	uchcom_read_reg(sc, 0x18, 0, 0x25, 0);
698 	uchcom_write_reg(sc, 0x18, 0x50, 0x25, 0x00);
699 	uchcom_update_status(sc);
700 	uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0x501f, 0xd90a);
701 	uchcom_set_baudrate(sc, t->c_ospeed);
702 	uchcom_set_dtr_rts(sc);
703 	uchcom_update_status(sc);
704 }
705 
706 static void
707 uchcom_start_read(struct ucom_softc *ucom)
708 {
709 	struct uchcom_softc *sc = ucom->sc_parent;
710 
711 	/* start interrupt endpoint */
712 	usbd_transfer_start(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
713 
714 	/* start read endpoint */
715 	usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
716 }
717 
718 static void
719 uchcom_stop_read(struct ucom_softc *ucom)
720 {
721 	struct uchcom_softc *sc = ucom->sc_parent;
722 
723 	/* stop interrupt endpoint */
724 	usbd_transfer_stop(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
725 
726 	/* stop read endpoint */
727 	usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
728 }
729 
730 static void
731 uchcom_start_write(struct ucom_softc *ucom)
732 {
733 	struct uchcom_softc *sc = ucom->sc_parent;
734 
735 	usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
736 }
737 
738 static void
739 uchcom_stop_write(struct ucom_softc *ucom)
740 {
741 	struct uchcom_softc *sc = ucom->sc_parent;
742 
743 	usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
744 }
745 
746 /* ----------------------------------------------------------------------
747  * callback when the modem status is changed.
748  */
749 static void
750 uchcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
751 {
752 	struct uchcom_softc *sc = usbd_xfer_softc(xfer);
753 	struct usb_page_cache *pc;
754 	uint8_t buf[UCHCOM_INTR_LEAST];
755 	int actlen;
756 
757 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
758 
759 	switch (USB_GET_STATE(xfer)) {
760 	case USB_ST_TRANSFERRED:
761 
762 		DPRINTF("actlen = %u\n", actlen);
763 
764 		if (actlen >= UCHCOM_INTR_LEAST) {
765 			pc = usbd_xfer_get_frame(xfer, 0);
766 			usbd_copy_out(pc, 0, buf, UCHCOM_INTR_LEAST);
767 
768 			DPRINTF("data = 0x%02X 0x%02X 0x%02X 0x%02X\n",
769 			    (unsigned)buf[0], (unsigned)buf[1],
770 			    (unsigned)buf[2], (unsigned)buf[3]);
771 
772 			uchcom_convert_status(sc, buf[UCHCOM_INTR_STAT1]);
773 			ucom_status_change(&sc->sc_ucom);
774 		}
775 	case USB_ST_SETUP:
776 tr_setup:
777 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
778 		usbd_transfer_submit(xfer);
779 		break;
780 
781 	default:			/* Error */
782 		if (error != USB_ERR_CANCELLED) {
783 			/* try to clear stall first */
784 			usbd_xfer_set_stall(xfer);
785 			goto tr_setup;
786 		}
787 		break;
788 	}
789 }
790 
791 static void
792 uchcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
793 {
794 	struct uchcom_softc *sc = usbd_xfer_softc(xfer);
795 	struct usb_page_cache *pc;
796 	uint32_t actlen;
797 
798 	switch (USB_GET_STATE(xfer)) {
799 	case USB_ST_SETUP:
800 	case USB_ST_TRANSFERRED:
801 tr_setup:
802 		pc = usbd_xfer_get_frame(xfer, 0);
803 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
804 		    usbd_xfer_max_len(xfer), &actlen)) {
805 
806 			DPRINTF("actlen = %d\n", actlen);
807 
808 			usbd_xfer_set_frame_len(xfer, 0, actlen);
809 			usbd_transfer_submit(xfer);
810 		}
811 		break;
812 
813 	default:			/* Error */
814 		if (error != USB_ERR_CANCELLED) {
815 			/* try to clear stall first */
816 			usbd_xfer_set_stall(xfer);
817 			goto tr_setup;
818 		}
819 		break;
820 	}
821 }
822 
823 static void
824 uchcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
825 {
826 	struct uchcom_softc *sc = usbd_xfer_softc(xfer);
827 	struct usb_page_cache *pc;
828 	int actlen;
829 
830 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
831 
832 	switch (USB_GET_STATE(xfer)) {
833 	case USB_ST_TRANSFERRED:
834 
835 		if (actlen > 0) {
836 			pc = usbd_xfer_get_frame(xfer, 0);
837 			ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
838 		}
839 
840 	case USB_ST_SETUP:
841 tr_setup:
842 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
843 		usbd_transfer_submit(xfer);
844 		break;
845 
846 	default:			/* Error */
847 		if (error != USB_ERR_CANCELLED) {
848 			/* try to clear stall first */
849 			usbd_xfer_set_stall(xfer);
850 			goto tr_setup;
851 		}
852 		break;
853 	}
854 }
855 
856 static void
857 uchcom_poll(struct ucom_softc *ucom)
858 {
859 	struct uchcom_softc *sc = ucom->sc_parent;
860 	usbd_transfer_poll(sc->sc_xfer, UCHCOM_N_TRANSFER);
861 }
862 
863 static device_method_t uchcom_methods[] = {
864 	/* Device interface */
865 	DEVMETHOD(device_probe, uchcom_probe),
866 	DEVMETHOD(device_attach, uchcom_attach),
867 	DEVMETHOD(device_detach, uchcom_detach),
868 	DEVMETHOD_END
869 };
870 
871 static driver_t uchcom_driver = {
872 	.name = "ucom",
873 	.methods = uchcom_methods,
874 	.size = sizeof(struct uchcom_softc)
875 };
876 
877 static devclass_t uchcom_devclass;
878 
879 DRIVER_MODULE(uchcom, uhub, uchcom_driver, uchcom_devclass, NULL, 0);
880 MODULE_DEPEND(uchcom, ucom, 1, 1, 1);
881 MODULE_DEPEND(uchcom, usb, 1, 1, 1);
882 MODULE_VERSION(uchcom, 1);
883