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