uchcom.c revision 186730
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: head/sys/dev/usb2/serial/uchcom2.c 186730 2009-01-04 00:12:01Z alfred $");
67
68/*
69 * driver for WinChipHead CH341/340, the worst USB-serial chip in the world.
70 */
71
72#include <dev/usb2/include/usb2_devid.h>
73#include <dev/usb2/include/usb2_standard.h>
74#include <dev/usb2/include/usb2_mfunc.h>
75#include <dev/usb2/include/usb2_error.h>
76#include <dev/usb2/include/usb2_cdc.h>
77#include <dev/usb2/include/usb2_ioctl.h>
78
79#define	USB_DEBUG_VAR uchcom_debug
80
81#include <dev/usb2/core/usb2_core.h>
82#include <dev/usb2/core/usb2_debug.h>
83#include <dev/usb2/core/usb2_process.h>
84#include <dev/usb2/core/usb2_config_td.h>
85#include <dev/usb2/core/usb2_request.h>
86#include <dev/usb2/core/usb2_lookup.h>
87#include <dev/usb2/core/usb2_util.h>
88#include <dev/usb2/core/usb2_busdma.h>
89
90#include <dev/usb2/serial/usb2_serial.h>
91
92#if USB_DEBUG
93static int uchcom_debug = 0;
94
95SYSCTL_NODE(_hw_usb2, OID_AUTO, uchcom, CTLFLAG_RW, 0, "USB uchcom");
96SYSCTL_INT(_hw_usb2_uchcom, OID_AUTO, debug, CTLFLAG_RW,
97    &uchcom_debug, 0, "uchcom debug level");
98#endif
99
100#define	UCHCOM_IFACE_INDEX	0
101#define	UCHCOM_CONFIG_INDEX	0
102
103#define	UCHCOM_REV_CH340	0x0250
104#define	UCHCOM_INPUT_BUF_SIZE	8
105
106#define	UCHCOM_REQ_GET_VERSION	0x5F
107#define	UCHCOM_REQ_READ_REG	0x95
108#define	UCHCOM_REQ_WRITE_REG	0x9A
109#define	UCHCOM_REQ_RESET	0xA1
110#define	UCHCOM_REQ_SET_DTRRTS	0xA4
111
112#define	UCHCOM_REG_STAT1	0x06
113#define	UCHCOM_REG_STAT2	0x07
114#define	UCHCOM_REG_BPS_PRE	0x12
115#define	UCHCOM_REG_BPS_DIV	0x13
116#define	UCHCOM_REG_BPS_MOD	0x14
117#define	UCHCOM_REG_BPS_PAD	0x0F
118#define	UCHCOM_REG_BREAK1	0x05
119#define	UCHCOM_REG_BREAK2	0x18
120#define	UCHCOM_REG_LCR1		0x18
121#define	UCHCOM_REG_LCR2		0x25
122
123#define	UCHCOM_VER_20		0x20
124
125#define	UCHCOM_BASE_UNKNOWN	0
126#define	UCHCOM_BPS_MOD_BASE	20000000
127#define	UCHCOM_BPS_MOD_BASE_OFS	1100
128
129#define	UCHCOM_DTR_MASK		0x20
130#define	UCHCOM_RTS_MASK		0x40
131
132#define	UCHCOM_BRK1_MASK	0x01
133#define	UCHCOM_BRK2_MASK	0x40
134
135#define	UCHCOM_LCR1_MASK	0xAF
136#define	UCHCOM_LCR2_MASK	0x07
137#define	UCHCOM_LCR1_PARENB	0x80
138#define	UCHCOM_LCR2_PAREVEN	0x07
139#define	UCHCOM_LCR2_PARODD	0x06
140#define	UCHCOM_LCR2_PARMARK	0x05
141#define	UCHCOM_LCR2_PARSPACE	0x04
142
143#define	UCHCOM_INTR_STAT1	0x02
144#define	UCHCOM_INTR_STAT2	0x03
145#define	UCHCOM_INTR_LEAST	4
146
147#define	UCHCOM_BULK_BUF_SIZE 1024	/* bytes */
148#define	UCHCOM_N_TRANSFER 6		/* units */
149
150struct uchcom_softc {
151	struct usb2_com_super_softc sc_super_ucom;
152	struct usb2_com_softc sc_ucom;
153
154	struct usb2_xfer *sc_xfer[UCHCOM_N_TRANSFER];
155	struct usb2_device *sc_udev;
156
157	uint8_t	sc_dtr;			/* local copy */
158	uint8_t	sc_rts;			/* local copy */
159	uint8_t	sc_version;
160	uint8_t	sc_msr;
161	uint8_t	sc_lsr;			/* local status register */
162	uint8_t	sc_flag;
163#define	UCHCOM_FLAG_INTR_STALL  0x01
164#define	UCHCOM_FLAG_READ_STALL  0x02
165#define	UCHCOM_FLAG_WRITE_STALL 0x04
166};
167
168struct uchcom_divider {
169	uint8_t	dv_prescaler;
170	uint8_t	dv_div;
171	uint8_t	dv_mod;
172};
173
174struct uchcom_divider_record {
175	uint32_t dvr_high;
176	uint32_t dvr_low;
177	uint32_t dvr_base_clock;
178	struct uchcom_divider dvr_divider;
179};
180
181static const struct uchcom_divider_record dividers[] =
182{
183	{307200, 307200, UCHCOM_BASE_UNKNOWN, {7, 0xD9, 0}},
184	{921600, 921600, UCHCOM_BASE_UNKNOWN, {7, 0xF3, 0}},
185	{2999999, 23530, 6000000, {3, 0, 0}},
186	{23529, 2942, 750000, {2, 0, 0}},
187	{2941, 368, 93750, {1, 0, 0}},
188	{367, 1, 11719, {0, 0, 0}},
189};
190
191#define	NUM_DIVIDERS	(sizeof (dividers) / sizeof (dividers[0]))
192
193static const struct usb2_device_id uchcom_devs[] = {
194	{USB_VPI(USB_VENDOR_WCH, USB_PRODUCT_WCH_CH341SER, 0)},
195};
196
197/* protypes */
198
199static int	uchcom_pre_param(struct usb2_com_softc *, struct termios *);
200static void	uchcom_cfg_get_status(struct usb2_com_softc *, uint8_t *,
201		    uint8_t *);
202static void	uchcom_cfg_param(struct usb2_com_softc *, struct termios *);
203static void	uchcom_cfg_set_break(struct usb2_com_softc *, uint8_t);
204static void	uchcom_cfg_set_dtr(struct usb2_com_softc *, uint8_t);
205static void	uchcom_cfg_set_rts(struct usb2_com_softc *, uint8_t);
206static void	uchcom_start_read(struct usb2_com_softc *);
207static void	uchcom_start_write(struct usb2_com_softc *);
208static void	uchcom_stop_read(struct usb2_com_softc *);
209static void	uchcom_stop_write(struct usb2_com_softc *);
210static void	uchcom_update_version(struct uchcom_softc *);
211static void	uchcom_convert_status(struct uchcom_softc *, uint8_t);
212static void	uchcom_update_status(struct uchcom_softc *);
213static void	uchcom_set_dtrrts(struct uchcom_softc *);
214static int	uchcom_calc_divider_settings(struct uchcom_divider *, uint32_t);
215static void	uchcom_set_dte_rate(struct uchcom_softc *, uint32_t);
216static void	uchcom_set_line_control(struct uchcom_softc *, tcflag_t);
217static void	uchcom_clear_chip(struct uchcom_softc *);
218static void	uchcom_reset_chip(struct uchcom_softc *);
219
220static device_probe_t uchcom_probe;
221static device_attach_t uchcom_attach;
222static device_detach_t uchcom_detach;
223
224static usb2_callback_t uchcom_intr_callback;
225static usb2_callback_t uchcom_intr_clear_stall_callback;
226static usb2_callback_t uchcom_write_callback;
227static usb2_callback_t uchcom_write_clear_stall_callback;
228static usb2_callback_t uchcom_read_callback;
229static usb2_callback_t uchcom_read_clear_stall_callback;
230
231static const struct usb2_config uchcom_config_data[UCHCOM_N_TRANSFER] = {
232
233	[0] = {
234		.type = UE_BULK,
235		.endpoint = UE_ADDR_ANY,
236		.direction = UE_DIR_OUT,
237		.mh.bufsize = UCHCOM_BULK_BUF_SIZE,
238		.mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
239		.mh.callback = &uchcom_write_callback,
240	},
241
242	[1] = {
243		.type = UE_BULK,
244		.endpoint = UE_ADDR_ANY,
245		.direction = UE_DIR_IN,
246		.mh.bufsize = UCHCOM_BULK_BUF_SIZE,
247		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
248		.mh.callback = &uchcom_read_callback,
249	},
250
251	[2] = {
252		.type = UE_CONTROL,
253		.endpoint = 0x00,	/* Control pipe */
254		.direction = UE_DIR_ANY,
255		.mh.bufsize = sizeof(struct usb2_device_request),
256		.mh.callback = &uchcom_write_clear_stall_callback,
257		.mh.timeout = 1000,	/* 1 second */
258		.mh.interval = 50,	/* 50ms */
259	},
260
261	[3] = {
262		.type = UE_CONTROL,
263		.endpoint = 0x00,	/* Control pipe */
264		.direction = UE_DIR_ANY,
265		.mh.bufsize = sizeof(struct usb2_device_request),
266		.mh.callback = &uchcom_read_clear_stall_callback,
267		.mh.timeout = 1000,	/* 1 second */
268		.mh.interval = 50,	/* 50ms */
269	},
270
271	[4] = {
272		.type = UE_INTERRUPT,
273		.endpoint = UE_ADDR_ANY,
274		.direction = UE_DIR_IN,
275		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
276		.mh.bufsize = 0,	/* use wMaxPacketSize */
277		.mh.callback = &uchcom_intr_callback,
278	},
279
280	[5] = {
281		.type = UE_CONTROL,
282		.endpoint = 0x00,	/* Control pipe */
283		.direction = UE_DIR_ANY,
284		.mh.bufsize = sizeof(struct usb2_device_request),
285		.mh.callback = &uchcom_intr_clear_stall_callback,
286		.mh.timeout = 1000,	/* 1 second */
287		.mh.interval = 50,	/* 50ms */
288	},
289};
290
291struct usb2_com_callback uchcom_callback = {
292	.usb2_com_cfg_get_status = &uchcom_cfg_get_status,
293	.usb2_com_cfg_set_dtr = &uchcom_cfg_set_dtr,
294	.usb2_com_cfg_set_rts = &uchcom_cfg_set_rts,
295	.usb2_com_cfg_set_break = &uchcom_cfg_set_break,
296	.usb2_com_cfg_param = &uchcom_cfg_param,
297	.usb2_com_pre_param = &uchcom_pre_param,
298	.usb2_com_start_read = &uchcom_start_read,
299	.usb2_com_stop_read = &uchcom_stop_read,
300	.usb2_com_start_write = &uchcom_start_write,
301	.usb2_com_stop_write = &uchcom_stop_write,
302};
303
304/* ----------------------------------------------------------------------
305 * driver entry points
306 */
307
308static int
309uchcom_probe(device_t dev)
310{
311	struct usb2_attach_arg *uaa = device_get_ivars(dev);
312
313	DPRINTFN(11, "\n");
314
315	if (uaa->usb2_mode != USB_MODE_HOST) {
316		return (ENXIO);
317	}
318	if (uaa->info.bConfigIndex != UCHCOM_CONFIG_INDEX) {
319		return (ENXIO);
320	}
321	if (uaa->info.bIfaceIndex != UCHCOM_IFACE_INDEX) {
322		return (ENXIO);
323	}
324	return (usb2_lookup_id_by_uaa(uchcom_devs, sizeof(uchcom_devs), uaa));
325}
326
327static int
328uchcom_attach(device_t dev)
329{
330	struct uchcom_softc *sc = device_get_softc(dev);
331	struct usb2_attach_arg *uaa = device_get_ivars(dev);
332	int error;
333	uint8_t iface_index;
334
335	DPRINTFN(11, "\n");
336
337	if (sc == NULL) {
338		return (ENOMEM);
339	}
340	device_set_usb2_desc(dev);
341
342	sc->sc_udev = uaa->device;
343
344	switch (uaa->info.bcdDevice) {
345	case UCHCOM_REV_CH340:
346		device_printf(dev, "CH340 detected\n");
347		break;
348	default:
349		device_printf(dev, "CH341 detected\n");
350		break;
351	}
352
353	iface_index = UCHCOM_IFACE_INDEX;
354	error = usb2_transfer_setup(uaa->device,
355	    &iface_index, sc->sc_xfer, uchcom_config_data,
356	    UCHCOM_N_TRANSFER, sc, &Giant);
357
358	if (error) {
359		DPRINTF("one or more missing USB endpoints, "
360		    "error=%s\n", usb2_errstr(error));
361		goto detach;
362	}
363	/*
364	 * Do the initialization during attach so that the system does not
365	 * sleep during open:
366	 */
367	uchcom_update_version(sc);
368	uchcom_clear_chip(sc);
369	uchcom_reset_chip(sc);
370	uchcom_update_status(sc);
371
372	sc->sc_dtr = 1;
373	sc->sc_rts = 1;
374
375	/* clear stall at first run */
376	sc->sc_flag |= (UCHCOM_FLAG_READ_STALL |
377	    UCHCOM_FLAG_WRITE_STALL);
378
379	error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
380	    &uchcom_callback, &Giant);
381	if (error) {
382		goto detach;
383	}
384	return (0);
385
386detach:
387	uchcom_detach(dev);
388	return (ENXIO);
389}
390
391static int
392uchcom_detach(device_t dev)
393{
394	struct uchcom_softc *sc = device_get_softc(dev);
395
396	DPRINTFN(11, "\n");
397
398	usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
399
400	usb2_transfer_unsetup(sc->sc_xfer, UCHCOM_N_TRANSFER);
401
402	return (0);
403}
404
405/* ----------------------------------------------------------------------
406 * low level i/o
407 */
408
409static void
410uchcom_do_request(struct uchcom_softc *sc,
411    struct usb2_device_request *req, void *data)
412{
413	uint16_t length;
414	uint16_t actlen;
415	usb2_error_t err;
416
417	length = UGETW(req->wLength);
418	actlen = 0;
419
420	if (usb2_com_cfg_is_gone(&sc->sc_ucom)) {
421		goto done;
422	}
423	err = usb2_do_request_flags(sc->sc_udev, &Giant, req,
424	    data, USB_SHORT_XFER_OK, &actlen, 1000);
425
426	if (err) {
427		DPRINTFN(0, "device request failed, err=%s "
428		    "(ignored)\n", usb2_errstr(err));
429	}
430done:
431	if (length != actlen) {
432		if (req->bmRequestType & UT_READ) {
433			bzero(USB_ADD_BYTES(data, actlen), length - actlen);
434		}
435	}
436}
437
438static void
439uchcom_ctrl_write(struct uchcom_softc *sc, uint8_t reqno,
440    uint16_t value, uint16_t index)
441{
442	struct usb2_device_request req;
443
444	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
445	req.bRequest = reqno;
446	USETW(req.wValue, value);
447	USETW(req.wIndex, index);
448	USETW(req.wLength, 0);
449
450	uchcom_do_request(sc, &req, NULL);
451}
452
453static void
454uchcom_ctrl_read(struct uchcom_softc *sc, uint8_t reqno,
455    uint16_t value, uint16_t index, void *buf, uint16_t buflen)
456{
457	struct usb2_device_request req;
458
459	req.bmRequestType = UT_READ_VENDOR_DEVICE;
460	req.bRequest = reqno;
461	USETW(req.wValue, value);
462	USETW(req.wIndex, index);
463	USETW(req.wLength, buflen);
464
465	uchcom_do_request(sc, &req, buf);
466}
467
468static void
469uchcom_write_reg(struct uchcom_softc *sc,
470    uint8_t reg1, uint8_t val1, uint8_t reg2, uint8_t val2)
471{
472	DPRINTF("0x%02X<-0x%02X, 0x%02X<-0x%02X\n",
473	    (unsigned)reg1, (unsigned)val1,
474	    (unsigned)reg2, (unsigned)val2);
475	uchcom_ctrl_write(
476	    sc, UCHCOM_REQ_WRITE_REG,
477	    reg1 | ((uint16_t)reg2 << 8), val1 | ((uint16_t)val2 << 8));
478}
479
480static void
481uchcom_read_reg(struct uchcom_softc *sc,
482    uint8_t reg1, uint8_t *rval1, uint8_t reg2, uint8_t *rval2)
483{
484	uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
485
486	uchcom_ctrl_read(
487	    sc, UCHCOM_REQ_READ_REG,
488	    reg1 | ((uint16_t)reg2 << 8), 0, buf, sizeof(buf));
489
490	DPRINTF("0x%02X->0x%02X, 0x%02X->0x%02X\n",
491	    (unsigned)reg1, (unsigned)buf[0],
492	    (unsigned)reg2, (unsigned)buf[1]);
493
494	if (rval1)
495		*rval1 = buf[0];
496	if (rval2)
497		*rval2 = buf[1];
498}
499
500static void
501uchcom_get_version(struct uchcom_softc *sc, uint8_t *rver)
502{
503	uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
504
505	uchcom_ctrl_read(
506	    sc, UCHCOM_REQ_GET_VERSION, 0, 0, buf, sizeof(buf));
507
508	if (rver)
509		*rver = buf[0];
510}
511
512static void
513uchcom_get_status(struct uchcom_softc *sc, uint8_t *rval)
514{
515	uchcom_read_reg(sc, UCHCOM_REG_STAT1, rval, UCHCOM_REG_STAT2, NULL);
516}
517
518static void
519uchcom_set_dtrrts_10(struct uchcom_softc *sc, uint8_t val)
520{
521	uchcom_write_reg(sc, UCHCOM_REG_STAT1, val, UCHCOM_REG_STAT1, val);
522}
523
524static void
525uchcom_set_dtrrts_20(struct uchcom_softc *sc, uint8_t val)
526{
527	uchcom_ctrl_write(sc, UCHCOM_REQ_SET_DTRRTS, val, 0);
528}
529
530
531/* ----------------------------------------------------------------------
532 * middle layer
533 */
534
535static void
536uchcom_update_version(struct uchcom_softc *sc)
537{
538	uchcom_get_version(sc, &sc->sc_version);
539}
540
541static void
542uchcom_convert_status(struct uchcom_softc *sc, uint8_t cur)
543{
544	sc->sc_dtr = !(cur & UCHCOM_DTR_MASK);
545	sc->sc_rts = !(cur & UCHCOM_RTS_MASK);
546
547	cur = ~cur & 0x0F;
548	sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur);
549}
550
551static void
552uchcom_update_status(struct uchcom_softc *sc)
553{
554	uint8_t cur;
555
556	uchcom_get_status(sc, &cur);
557	uchcom_convert_status(sc, cur);
558}
559
560
561static void
562uchcom_set_dtrrts(struct uchcom_softc *sc)
563{
564	uint8_t val = 0;
565
566	if (sc->sc_dtr)
567		val |= UCHCOM_DTR_MASK;
568	if (sc->sc_rts)
569		val |= UCHCOM_RTS_MASK;
570
571	if (sc->sc_version < UCHCOM_VER_20)
572		uchcom_set_dtrrts_10(sc, ~val);
573	else
574		uchcom_set_dtrrts_20(sc, ~val);
575}
576
577static void
578uchcom_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff)
579{
580	struct uchcom_softc *sc = ucom->sc_parent;
581	uint8_t brk1;
582	uint8_t brk2;
583
584	uchcom_read_reg(sc, UCHCOM_REG_BREAK1, &brk1, UCHCOM_REG_BREAK2, &brk2);
585	if (onoff) {
586		/* on - clear bits */
587		brk1 &= ~UCHCOM_BRK1_MASK;
588		brk2 &= ~UCHCOM_BRK2_MASK;
589	} else {
590		/* off - set bits */
591		brk1 |= UCHCOM_BRK1_MASK;
592		brk2 |= UCHCOM_BRK2_MASK;
593	}
594	uchcom_write_reg(sc, UCHCOM_REG_BREAK1, brk1, UCHCOM_REG_BREAK2, brk2);
595}
596
597static int
598uchcom_calc_divider_settings(struct uchcom_divider *dp, uint32_t rate)
599{
600	const struct uchcom_divider_record *rp;
601	uint32_t div;
602	uint32_t rem;
603	uint32_t mod;
604	uint8_t i;
605
606	/* find record */
607	for (i = 0; i != NUM_DIVIDERS; i++) {
608		if (dividers[i].dvr_high >= rate &&
609		    dividers[i].dvr_low <= rate) {
610			rp = &dividers[i];
611			goto found;
612		}
613	}
614	return (-1);
615
616found:
617	dp->dv_prescaler = rp->dvr_divider.dv_prescaler;
618	if (rp->dvr_base_clock == UCHCOM_BASE_UNKNOWN)
619		dp->dv_div = rp->dvr_divider.dv_div;
620	else {
621		div = rp->dvr_base_clock / rate;
622		rem = rp->dvr_base_clock % rate;
623		if (div == 0 || div >= 0xFF)
624			return (-1);
625		if ((rem << 1) >= rate)
626			div += 1;
627		dp->dv_div = (uint8_t)-div;
628	}
629
630	mod = UCHCOM_BPS_MOD_BASE / rate + UCHCOM_BPS_MOD_BASE_OFS;
631	mod = mod + mod / 2;
632
633	dp->dv_mod = mod / 0x100;
634
635	return (0);
636}
637
638static void
639uchcom_set_dte_rate(struct uchcom_softc *sc, uint32_t rate)
640{
641	struct uchcom_divider dv;
642
643	if (uchcom_calc_divider_settings(&dv, rate))
644		return;
645
646	uchcom_write_reg(sc,
647	    UCHCOM_REG_BPS_PRE, dv.dv_prescaler,
648	    UCHCOM_REG_BPS_DIV, dv.dv_div);
649	uchcom_write_reg(sc,
650	    UCHCOM_REG_BPS_MOD, dv.dv_mod,
651	    UCHCOM_REG_BPS_PAD, 0);
652}
653
654static void
655uchcom_set_line_control(struct uchcom_softc *sc, tcflag_t cflag)
656{
657	uint8_t lcr1 = 0;
658	uint8_t lcr2 = 0;
659
660	uchcom_read_reg(sc, UCHCOM_REG_LCR1, &lcr1, UCHCOM_REG_LCR2, &lcr2);
661
662	lcr1 &= ~UCHCOM_LCR1_MASK;
663	lcr2 &= ~UCHCOM_LCR2_MASK;
664
665	/*
666	 * XXX: it is difficult to handle the line control appropriately:
667	 *   - CS8, !CSTOPB and any parity mode seems ok, but
668	 *   - the chip doesn't have the function to calculate parity
669	 *     in !CS8 mode.
670	 *   - it is unclear that the chip supports CS5,6 mode.
671	 *   - it is unclear how to handle stop bits.
672	 */
673
674	if (cflag & PARENB) {
675		lcr1 |= UCHCOM_LCR1_PARENB;
676		if (cflag & PARODD)
677			lcr2 |= UCHCOM_LCR2_PARODD;
678		else
679			lcr2 |= UCHCOM_LCR2_PAREVEN;
680	}
681	uchcom_write_reg(sc, UCHCOM_REG_LCR1, lcr1, UCHCOM_REG_LCR2, lcr2);
682}
683
684static void
685uchcom_clear_chip(struct uchcom_softc *sc)
686{
687	DPRINTF("\n");
688	uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0, 0);
689}
690
691static void
692uchcom_reset_chip(struct uchcom_softc *sc)
693{
694	uint16_t val;
695	uint16_t idx;
696	uint8_t lcr1;
697	uint8_t lcr2;
698	uint8_t pre;
699	uint8_t div;
700	uint8_t mod;
701
702	uchcom_read_reg(sc, UCHCOM_REG_LCR1, &lcr1, UCHCOM_REG_LCR2, &lcr2);
703	uchcom_read_reg(sc, UCHCOM_REG_BPS_PRE, &pre, UCHCOM_REG_BPS_DIV, &div);
704	uchcom_read_reg(sc, UCHCOM_REG_BPS_MOD, &mod, UCHCOM_REG_BPS_PAD, NULL);
705
706	val = 0;
707	idx = 0;
708	val |= (uint16_t)(lcr1 & 0xF0) << 8;
709	val |= 0x01;
710	val |= (uint16_t)(lcr2 & 0x0F) << 8;
711	val |= 0x02;
712	idx |= pre & 0x07;
713	val |= 0x04;
714	idx |= (uint16_t)div << 8;
715	val |= 0x08;
716	idx |= mod & 0xF8;
717	val |= 0x10;
718
719	DPRINTF("reset v=0x%04X, i=0x%04X\n", val, idx);
720
721	uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, val, idx);
722}
723
724/* ----------------------------------------------------------------------
725 * methods for ucom
726 */
727static void
728uchcom_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr)
729{
730	struct uchcom_softc *sc = ucom->sc_parent;
731
732	DPRINTF("\n");
733
734	*lsr = sc->sc_lsr;
735	*msr = sc->sc_msr;
736}
737
738static void
739uchcom_cfg_set_dtr(struct usb2_com_softc *ucom, uint8_t onoff)
740{
741	struct uchcom_softc *sc = ucom->sc_parent;
742
743	DPRINTF("onoff = %d\n", onoff);
744
745	sc->sc_dtr = onoff;
746	uchcom_set_dtrrts(sc);
747}
748
749static void
750uchcom_cfg_set_rts(struct usb2_com_softc *ucom, uint8_t onoff)
751{
752	struct uchcom_softc *sc = ucom->sc_parent;
753
754	DPRINTF("onoff = %d\n", onoff);
755
756	sc->sc_rts = onoff;
757	uchcom_set_dtrrts(sc);
758}
759
760static int
761uchcom_pre_param(struct usb2_com_softc *ucom, struct termios *t)
762{
763	struct uchcom_divider dv;
764
765	switch (t->c_cflag & CSIZE) {
766	case CS5:
767	case CS6:
768	case CS7:
769		return (EIO);
770	default:
771		break;
772	}
773
774	if (uchcom_calc_divider_settings(&dv, t->c_ospeed)) {
775		return (EIO);
776	}
777	return (0);			/* success */
778}
779
780static void
781uchcom_cfg_param(struct usb2_com_softc *ucom, struct termios *t)
782{
783	struct uchcom_softc *sc = ucom->sc_parent;
784
785	uchcom_set_line_control(sc, t->c_cflag);
786	uchcom_set_dte_rate(sc, t->c_ospeed);
787}
788
789static void
790uchcom_start_read(struct usb2_com_softc *ucom)
791{
792	struct uchcom_softc *sc = ucom->sc_parent;
793
794	/* start interrupt endpoint */
795	usb2_transfer_start(sc->sc_xfer[4]);
796
797	/* start read endpoint */
798	usb2_transfer_start(sc->sc_xfer[1]);
799}
800
801static void
802uchcom_stop_read(struct usb2_com_softc *ucom)
803{
804	struct uchcom_softc *sc = ucom->sc_parent;
805
806	/* stop interrupt endpoint */
807	usb2_transfer_stop(sc->sc_xfer[4]);
808
809	/* stop read endpoint */
810	usb2_transfer_stop(sc->sc_xfer[3]);
811	usb2_transfer_stop(sc->sc_xfer[1]);
812}
813
814static void
815uchcom_start_write(struct usb2_com_softc *ucom)
816{
817	struct uchcom_softc *sc = ucom->sc_parent;
818
819	usb2_transfer_start(sc->sc_xfer[0]);
820}
821
822static void
823uchcom_stop_write(struct usb2_com_softc *ucom)
824{
825	struct uchcom_softc *sc = ucom->sc_parent;
826
827	usb2_transfer_stop(sc->sc_xfer[2]);
828	usb2_transfer_stop(sc->sc_xfer[0]);
829}
830
831/* ----------------------------------------------------------------------
832 * callback when the modem status is changed.
833 */
834static void
835uchcom_intr_callback(struct usb2_xfer *xfer)
836{
837	struct uchcom_softc *sc = xfer->priv_sc;
838	uint8_t buf[UCHCOM_INTR_LEAST];
839
840	switch (USB_GET_STATE(xfer)) {
841	case USB_ST_TRANSFERRED:
842
843		DPRINTF("actlen = %u\n", xfer->actlen);
844
845		if (xfer->actlen >= UCHCOM_INTR_LEAST) {
846			usb2_copy_out(xfer->frbuffers, 0, buf,
847			    UCHCOM_INTR_LEAST);
848
849			DPRINTF("data = 0x%02X 0x%02X 0x%02X 0x%02X\n",
850			    (unsigned)buf[0], (unsigned)buf[1],
851			    (unsigned)buf[2], (unsigned)buf[3]);
852
853			uchcom_convert_status(sc, buf[UCHCOM_INTR_STAT1]);
854			usb2_com_status_change(&sc->sc_ucom);
855		}
856	case USB_ST_SETUP:
857		if (sc->sc_flag & UCHCOM_FLAG_INTR_STALL) {
858			usb2_transfer_start(sc->sc_xfer[5]);
859		} else {
860			xfer->frlengths[0] = xfer->max_data_length;
861			usb2_start_hardware(xfer);
862		}
863		break;
864
865	default:			/* Error */
866		if (xfer->error != USB_ERR_CANCELLED) {
867			sc->sc_flag |= UCHCOM_FLAG_INTR_STALL;
868			usb2_transfer_start(sc->sc_xfer[5]);
869		}
870		break;
871	}
872}
873
874static void
875uchcom_intr_clear_stall_callback(struct usb2_xfer *xfer)
876{
877	struct uchcom_softc *sc = xfer->priv_sc;
878	struct usb2_xfer *xfer_other = sc->sc_xfer[4];
879
880	if (usb2_clear_stall_callback(xfer, xfer_other)) {
881		DPRINTF("stall cleared\n");
882		sc->sc_flag &= ~UCHCOM_FLAG_INTR_STALL;
883		usb2_transfer_start(xfer_other);
884	}
885}
886
887static void
888uchcom_write_callback(struct usb2_xfer *xfer)
889{
890	struct uchcom_softc *sc = xfer->priv_sc;
891	uint32_t actlen;
892
893	switch (USB_GET_STATE(xfer)) {
894	case USB_ST_SETUP:
895	case USB_ST_TRANSFERRED:
896		if (sc->sc_flag & UCHCOM_FLAG_WRITE_STALL) {
897			usb2_transfer_start(sc->sc_xfer[2]);
898			return;
899		}
900		if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0,
901		    UCHCOM_BULK_BUF_SIZE, &actlen)) {
902
903			DPRINTF("actlen = %d\n", actlen);
904
905			xfer->frlengths[0] = actlen;
906			usb2_start_hardware(xfer);
907		}
908		return;
909
910	default:			/* Error */
911		if (xfer->error != USB_ERR_CANCELLED) {
912			sc->sc_flag |= UCHCOM_FLAG_WRITE_STALL;
913			usb2_transfer_start(sc->sc_xfer[2]);
914		}
915		return;
916
917	}
918}
919
920static void
921uchcom_write_clear_stall_callback(struct usb2_xfer *xfer)
922{
923	struct uchcom_softc *sc = xfer->priv_sc;
924	struct usb2_xfer *xfer_other = sc->sc_xfer[0];
925
926	if (usb2_clear_stall_callback(xfer, xfer_other)) {
927		DPRINTF("stall cleared\n");
928		sc->sc_flag &= ~UCHCOM_FLAG_WRITE_STALL;
929		usb2_transfer_start(xfer_other);
930	}
931}
932
933static void
934uchcom_read_callback(struct usb2_xfer *xfer)
935{
936	struct uchcom_softc *sc = xfer->priv_sc;
937
938	switch (USB_GET_STATE(xfer)) {
939	case USB_ST_TRANSFERRED:
940		usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers, 0, xfer->actlen);
941
942	case USB_ST_SETUP:
943		if (sc->sc_flag & UCHCOM_FLAG_READ_STALL) {
944			usb2_transfer_start(sc->sc_xfer[3]);
945		} else {
946			xfer->frlengths[0] = xfer->max_data_length;
947			usb2_start_hardware(xfer);
948		}
949		return;
950
951	default:			/* Error */
952		if (xfer->error != USB_ERR_CANCELLED) {
953			sc->sc_flag |= UCHCOM_FLAG_READ_STALL;
954			usb2_transfer_start(sc->sc_xfer[3]);
955		}
956		return;
957
958	}
959}
960
961static void
962uchcom_read_clear_stall_callback(struct usb2_xfer *xfer)
963{
964	struct uchcom_softc *sc = xfer->priv_sc;
965	struct usb2_xfer *xfer_other = sc->sc_xfer[1];
966
967	if (usb2_clear_stall_callback(xfer, xfer_other)) {
968		DPRINTF("stall cleared\n");
969		sc->sc_flag &= ~UCHCOM_FLAG_READ_STALL;
970		usb2_transfer_start(xfer_other);
971	}
972}
973
974static device_method_t uchcom_methods[] = {
975	/* Device interface */
976	DEVMETHOD(device_probe, uchcom_probe),
977	DEVMETHOD(device_attach, uchcom_attach),
978	DEVMETHOD(device_detach, uchcom_detach),
979
980	{0, 0}
981};
982
983static driver_t uchcom_driver = {
984	"ucom",
985	uchcom_methods,
986	sizeof(struct uchcom_softc)
987};
988
989static devclass_t uchcom_devclass;
990
991DRIVER_MODULE(uchcom, ushub, uchcom_driver, uchcom_devclass, NULL, 0);
992MODULE_DEPEND(uchcom, usb2_serial, 1, 1, 1);
993MODULE_DEPEND(uchcom, usb2_core, 1, 1, 1);
994