1 /* $NetBSD: uchcom.c,v 1.1 2007/09/03 17:57:37 tshiozak Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-2-Clause-NetBSD
6 * Copyright (c) 2007, Takanori Watanabe
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Copyright (c) 2007 The NetBSD Foundation, Inc.
33 * All rights reserved.
35 * This code is derived from software contributed to The NetBSD Foundation
36 * by Takuya SHIOZAKI (tshiozak@netbsd.org).
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
47 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
48 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
49 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
50 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
51 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57 * POSSIBILITY OF SUCH DAMAGE.
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
64 * Driver for WinChipHead CH341/340, the worst USB-serial chip in the
68 #include <sys/stdint.h>
69 #include <sys/stddef.h>
70 #include <sys/param.h>
71 #include <sys/queue.h>
72 #include <sys/types.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
76 #include <sys/module.h>
78 #include <sys/mutex.h>
79 #include <sys/condvar.h>
80 #include <sys/sysctl.h>
82 #include <sys/unistd.h>
83 #include <sys/callout.h>
84 #include <sys/malloc.h>
87 #include <dev/usb/usb.h>
88 #include <dev/usb/usbdi.h>
89 #include <dev/usb/usbdi_util.h>
92 #define USB_DEBUG_VAR uchcom_debug
93 #include <dev/usb/usb_debug.h>
94 #include <dev/usb/usb_process.h>
96 #include <dev/usb/serial/usb_serial.h>
99 static int uchcom_debug = 0;
101 static SYSCTL_NODE(_hw_usb, OID_AUTO, uchcom, CTLFLAG_RW, 0, "USB uchcom");
102 SYSCTL_INT(_hw_usb_uchcom, OID_AUTO, debug, CTLFLAG_RWTUN,
103 &uchcom_debug, 0, "uchcom debug level");
106 #define UCHCOM_IFACE_INDEX 0
107 #define UCHCOM_CONFIG_INDEX 0
109 #define UCHCOM_REV_CH340 0x0250
110 #define UCHCOM_INPUT_BUF_SIZE 8
112 #define UCHCOM_REQ_GET_VERSION 0x5F
113 #define UCHCOM_REQ_READ_REG 0x95
114 #define UCHCOM_REQ_WRITE_REG 0x9A
115 #define UCHCOM_REQ_RESET 0xA1
116 #define UCHCOM_REQ_SET_DTRRTS 0xA4
118 #define UCHCOM_REG_STAT1 0x06
119 #define UCHCOM_REG_STAT2 0x07
120 #define UCHCOM_REG_BPS_PRE 0x12
121 #define UCHCOM_REG_BPS_DIV 0x13
122 #define UCHCOM_REG_BPS_MOD 0x14
123 #define UCHCOM_REG_BPS_PAD 0x0F
124 #define UCHCOM_REG_BREAK1 0x05
125 #define UCHCOM_REG_BREAK2 0x18
126 #define UCHCOM_REG_LCR1 0x18
127 #define UCHCOM_REG_LCR2 0x25
129 #define UCHCOM_VER_20 0x20
131 #define UCHCOM_BASE_UNKNOWN 0
132 #define UCHCOM_BPS_MOD_BASE 20000000
133 #define UCHCOM_BPS_MOD_BASE_OFS 1100
135 #define UCHCOM_DTR_MASK 0x20
136 #define UCHCOM_RTS_MASK 0x40
138 #define UCHCOM_BRK1_MASK 0x01
139 #define UCHCOM_BRK2_MASK 0x40
141 #define UCHCOM_LCR1_MASK 0xAF
142 #define UCHCOM_LCR2_MASK 0x07
143 #define UCHCOM_LCR1_PARENB 0x80
144 #define UCHCOM_LCR2_PAREVEN 0x07
145 #define UCHCOM_LCR2_PARODD 0x06
146 #define UCHCOM_LCR2_PARMARK 0x05
147 #define UCHCOM_LCR2_PARSPACE 0x04
149 #define UCHCOM_INTR_STAT1 0x02
150 #define UCHCOM_INTR_STAT2 0x03
151 #define UCHCOM_INTR_LEAST 4
153 #define UCHCOM_BULK_BUF_SIZE 1024 /* bytes */
162 struct uchcom_softc {
163 struct ucom_super_softc sc_super_ucom;
164 struct ucom_softc sc_ucom;
166 struct usb_xfer *sc_xfer[UCHCOM_N_TRANSFER];
167 struct usb_device *sc_udev;
170 uint8_t sc_dtr; /* local copy */
171 uint8_t sc_rts; /* local copy */
174 uint8_t sc_lsr; /* local status register */
177 struct uchcom_divider {
178 uint8_t dv_prescaler;
183 struct uchcom_divider_record {
186 uint32_t dvr_base_clock;
187 struct uchcom_divider dvr_divider;
190 static const struct uchcom_divider_record dividers[] =
192 {307200, 307200, UCHCOM_BASE_UNKNOWN, {7, 0xD9, 0}},
193 {921600, 921600, UCHCOM_BASE_UNKNOWN, {7, 0xF3, 0}},
194 {2999999, 23530, 6000000, {3, 0, 0}},
195 {23529, 2942, 750000, {2, 0, 0}},
196 {2941, 368, 93750, {1, 0, 0}},
197 {367, 1, 11719, {0, 0, 0}},
200 #define NUM_DIVIDERS nitems(dividers)
202 static const STRUCT_USB_HOST_ID uchcom_devs[] = {
203 {USB_VPI(USB_VENDOR_WCH, USB_PRODUCT_WCH_CH341SER, 0)},
204 {USB_VPI(USB_VENDOR_WCH2, USB_PRODUCT_WCH2_CH341SER, 0)},
205 {USB_VPI(USB_VENDOR_WCH2, USB_PRODUCT_WCH2_CH341SER_2, 0)},
210 static void uchcom_free(struct ucom_softc *);
211 static int uchcom_pre_param(struct ucom_softc *, struct termios *);
212 static void uchcom_cfg_get_status(struct ucom_softc *, uint8_t *,
214 static void uchcom_cfg_open(struct ucom_softc *ucom);
215 static void uchcom_cfg_param(struct ucom_softc *, struct termios *);
216 static void uchcom_cfg_set_break(struct ucom_softc *, uint8_t);
217 static void uchcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
218 static void uchcom_cfg_set_rts(struct ucom_softc *, uint8_t);
219 static void uchcom_start_read(struct ucom_softc *);
220 static void uchcom_start_write(struct ucom_softc *);
221 static void uchcom_stop_read(struct ucom_softc *);
222 static void uchcom_stop_write(struct ucom_softc *);
223 static void uchcom_update_version(struct uchcom_softc *);
224 static void uchcom_convert_status(struct uchcom_softc *, uint8_t);
225 static void uchcom_update_status(struct uchcom_softc *);
226 static void uchcom_set_dtr_rts(struct uchcom_softc *);
227 static int uchcom_calc_divider_settings(struct uchcom_divider *, uint32_t);
228 static void uchcom_set_baudrate(struct uchcom_softc *, uint32_t);
229 static void uchcom_poll(struct ucom_softc *ucom);
231 static device_probe_t uchcom_probe;
232 static device_attach_t uchcom_attach;
233 static device_detach_t uchcom_detach;
234 static void uchcom_free_softc(struct uchcom_softc *);
236 static usb_callback_t uchcom_intr_callback;
237 static usb_callback_t uchcom_write_callback;
238 static usb_callback_t uchcom_read_callback;
240 static const struct usb_config uchcom_config_data[UCHCOM_N_TRANSFER] = {
242 [UCHCOM_BULK_DT_WR] = {
244 .endpoint = UE_ADDR_ANY,
245 .direction = UE_DIR_OUT,
246 .bufsize = UCHCOM_BULK_BUF_SIZE,
247 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
248 .callback = &uchcom_write_callback,
251 [UCHCOM_BULK_DT_RD] = {
253 .endpoint = UE_ADDR_ANY,
254 .direction = UE_DIR_IN,
255 .bufsize = UCHCOM_BULK_BUF_SIZE,
256 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
257 .callback = &uchcom_read_callback,
260 [UCHCOM_INTR_DT_RD] = {
261 .type = UE_INTERRUPT,
262 .endpoint = UE_ADDR_ANY,
263 .direction = UE_DIR_IN,
264 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
265 .bufsize = 0, /* use wMaxPacketSize */
266 .callback = &uchcom_intr_callback,
270 static struct ucom_callback uchcom_callback = {
271 .ucom_cfg_get_status = &uchcom_cfg_get_status,
272 .ucom_cfg_set_dtr = &uchcom_cfg_set_dtr,
273 .ucom_cfg_set_rts = &uchcom_cfg_set_rts,
274 .ucom_cfg_set_break = &uchcom_cfg_set_break,
275 .ucom_cfg_open = &uchcom_cfg_open,
276 .ucom_cfg_param = &uchcom_cfg_param,
277 .ucom_pre_param = &uchcom_pre_param,
278 .ucom_start_read = &uchcom_start_read,
279 .ucom_stop_read = &uchcom_stop_read,
280 .ucom_start_write = &uchcom_start_write,
281 .ucom_stop_write = &uchcom_stop_write,
282 .ucom_poll = &uchcom_poll,
283 .ucom_free = &uchcom_free,
286 /* ----------------------------------------------------------------------
287 * driver entry points
291 uchcom_probe(device_t dev)
293 struct usb_attach_arg *uaa = device_get_ivars(dev);
297 if (uaa->usb_mode != USB_MODE_HOST) {
300 if (uaa->info.bConfigIndex != UCHCOM_CONFIG_INDEX) {
303 if (uaa->info.bIfaceIndex != UCHCOM_IFACE_INDEX) {
306 return (usbd_lookup_id_by_uaa(uchcom_devs, sizeof(uchcom_devs), uaa));
310 uchcom_attach(device_t dev)
312 struct uchcom_softc *sc = device_get_softc(dev);
313 struct usb_attach_arg *uaa = device_get_ivars(dev);
319 device_set_usb_desc(dev);
320 mtx_init(&sc->sc_mtx, "uchcom", NULL, MTX_DEF);
321 ucom_ref(&sc->sc_super_ucom);
323 sc->sc_udev = uaa->device;
325 switch (uaa->info.idProduct) {
326 case USB_PRODUCT_WCH2_CH341SER:
327 device_printf(dev, "CH340 detected\n");
329 case USB_PRODUCT_WCH2_CH341SER_2:
330 device_printf(dev, "CH341 detected\n");
333 device_printf(dev, "New CH340/CH341 product 0x%04x detected\n",
334 uaa->info.idProduct);
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);
344 DPRINTF("one or more missing USB endpoints, "
345 "error=%s\n", usbd_errstr(error));
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);
355 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
356 &uchcom_callback, &sc->sc_mtx);
360 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
370 uchcom_detach(device_t dev)
372 struct uchcom_softc *sc = device_get_softc(dev);
376 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
377 usbd_transfer_unsetup(sc->sc_xfer, UCHCOM_N_TRANSFER);
379 device_claim_softc(dev);
381 uchcom_free_softc(sc);
386 UCOM_UNLOAD_DRAIN(uchcom);
389 uchcom_free_softc(struct uchcom_softc *sc)
391 if (ucom_unref(&sc->sc_super_ucom)) {
392 mtx_destroy(&sc->sc_mtx);
393 device_free_softc(sc);
398 uchcom_free(struct ucom_softc *ucom)
400 uchcom_free_softc(ucom->sc_parent);
403 /* ----------------------------------------------------------------------
408 uchcom_ctrl_write(struct uchcom_softc *sc, uint8_t reqno,
409 uint16_t value, uint16_t index)
411 struct usb_device_request req;
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);
419 DPRINTF("WR REQ 0x%02X VAL 0x%04X IDX 0x%04X\n",
420 reqno, value, index);
421 ucom_cfg_do_request(sc->sc_udev,
422 &sc->sc_ucom, &req, NULL, 0, 1000);
426 uchcom_ctrl_read(struct uchcom_softc *sc, uint8_t reqno,
427 uint16_t value, uint16_t index, void *buf, uint16_t buflen)
429 struct usb_device_request req;
431 req.bmRequestType = UT_READ_VENDOR_DEVICE;
432 req.bRequest = reqno;
433 USETW(req.wValue, value);
434 USETW(req.wIndex, index);
435 USETW(req.wLength, buflen);
437 DPRINTF("RD REQ 0x%02X VAL 0x%04X IDX 0x%04X LEN %d\n",
438 reqno, value, index, buflen);
439 ucom_cfg_do_request(sc->sc_udev,
440 &sc->sc_ucom, &req, buf, USB_SHORT_XFER_OK, 1000);
444 uchcom_write_reg(struct uchcom_softc *sc,
445 uint8_t reg1, uint8_t val1, uint8_t reg2, uint8_t val2)
447 DPRINTF("0x%02X<-0x%02X, 0x%02X<-0x%02X\n",
448 (unsigned)reg1, (unsigned)val1,
449 (unsigned)reg2, (unsigned)val2);
451 sc, UCHCOM_REQ_WRITE_REG,
452 reg1 | ((uint16_t)reg2 << 8), val1 | ((uint16_t)val2 << 8));
456 uchcom_read_reg(struct uchcom_softc *sc,
457 uint8_t reg1, uint8_t *rval1, uint8_t reg2, uint8_t *rval2)
459 uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
462 sc, UCHCOM_REQ_READ_REG,
463 reg1 | ((uint16_t)reg2 << 8), 0, buf, sizeof(buf));
465 DPRINTF("0x%02X->0x%02X, 0x%02X->0x%02X\n",
466 (unsigned)reg1, (unsigned)buf[0],
467 (unsigned)reg2, (unsigned)buf[1]);
476 uchcom_get_version(struct uchcom_softc *sc, uint8_t *rver)
478 uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
480 uchcom_ctrl_read(sc, UCHCOM_REQ_GET_VERSION, 0, 0, buf, sizeof(buf));
487 uchcom_get_status(struct uchcom_softc *sc, uint8_t *rval)
489 uchcom_read_reg(sc, UCHCOM_REG_STAT1, rval, UCHCOM_REG_STAT2, NULL);
493 uchcom_set_dtr_rts_10(struct uchcom_softc *sc, uint8_t val)
495 uchcom_write_reg(sc, UCHCOM_REG_STAT1, val, UCHCOM_REG_STAT1, val);
499 uchcom_set_dtr_rts_20(struct uchcom_softc *sc, uint8_t val)
501 uchcom_ctrl_write(sc, UCHCOM_REQ_SET_DTRRTS, val, 0);
505 /* ----------------------------------------------------------------------
510 uchcom_update_version(struct uchcom_softc *sc)
512 uchcom_get_version(sc, &sc->sc_version);
513 DPRINTF("Chip version: 0x%02x\n", sc->sc_version);
517 uchcom_convert_status(struct uchcom_softc *sc, uint8_t cur)
519 sc->sc_dtr = !(cur & UCHCOM_DTR_MASK);
520 sc->sc_rts = !(cur & UCHCOM_RTS_MASK);
523 sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur);
527 uchcom_update_status(struct uchcom_softc *sc)
531 uchcom_get_status(sc, &cur);
532 uchcom_convert_status(sc, cur);
537 uchcom_set_dtr_rts(struct uchcom_softc *sc)
542 val |= UCHCOM_DTR_MASK;
544 val |= UCHCOM_RTS_MASK;
546 if (sc->sc_version < UCHCOM_VER_20)
547 uchcom_set_dtr_rts_10(sc, ~val);
549 uchcom_set_dtr_rts_20(sc, ~val);
553 uchcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
555 struct uchcom_softc *sc = ucom->sc_parent;
559 uchcom_read_reg(sc, UCHCOM_REG_BREAK1, &brk1, UCHCOM_REG_BREAK2, &brk2);
561 /* on - clear bits */
562 brk1 &= ~UCHCOM_BRK1_MASK;
563 brk2 &= ~UCHCOM_BRK2_MASK;
566 brk1 |= UCHCOM_BRK1_MASK;
567 brk2 |= UCHCOM_BRK2_MASK;
569 uchcom_write_reg(sc, UCHCOM_REG_BREAK1, brk1, UCHCOM_REG_BREAK2, brk2);
573 uchcom_calc_divider_settings(struct uchcom_divider *dp, uint32_t rate)
575 const struct uchcom_divider_record *rp;
582 for (i = 0; i != NUM_DIVIDERS; i++) {
583 if (dividers[i].dvr_high >= rate &&
584 dividers[i].dvr_low <= rate) {
592 dp->dv_prescaler = rp->dvr_divider.dv_prescaler;
593 if (rp->dvr_base_clock == UCHCOM_BASE_UNKNOWN)
594 dp->dv_div = rp->dvr_divider.dv_div;
596 div = rp->dvr_base_clock / rate;
597 rem = rp->dvr_base_clock % rate;
598 if (div == 0 || div >= 0xFF)
600 if ((rem << 1) >= rate)
602 dp->dv_div = (uint8_t)-div;
605 mod = (UCHCOM_BPS_MOD_BASE / rate) + UCHCOM_BPS_MOD_BASE_OFS;
606 mod = mod + (mod / 2);
608 dp->dv_mod = (mod + 0xFF) / 0x100;
614 uchcom_set_baudrate(struct uchcom_softc *sc, uint32_t rate)
616 struct uchcom_divider dv;
618 if (uchcom_calc_divider_settings(&dv, rate))
622 * According to linux code we need to set bit 7 of UCHCOM_REG_BPS_PRE,
623 * otherwise the chip will buffer data.
626 UCHCOM_REG_BPS_PRE, dv.dv_prescaler | 0x80,
627 UCHCOM_REG_BPS_DIV, dv.dv_div);
629 UCHCOM_REG_BPS_MOD, dv.dv_mod,
630 UCHCOM_REG_BPS_PAD, 0);
633 /* ----------------------------------------------------------------------
637 uchcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
639 struct uchcom_softc *sc = ucom->sc_parent;
643 /* XXX Note: sc_lsr is always zero */
649 uchcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
651 struct uchcom_softc *sc = ucom->sc_parent;
653 DPRINTF("onoff = %d\n", onoff);
656 uchcom_set_dtr_rts(sc);
660 uchcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
662 struct uchcom_softc *sc = ucom->sc_parent;
664 DPRINTF("onoff = %d\n", onoff);
667 uchcom_set_dtr_rts(sc);
671 uchcom_cfg_open(struct ucom_softc *ucom)
673 struct uchcom_softc *sc = ucom->sc_parent;
677 uchcom_update_version(sc);
678 uchcom_update_status(sc);
682 uchcom_pre_param(struct ucom_softc *ucom, struct termios *t)
684 struct uchcom_divider dv;
686 switch (t->c_cflag & CSIZE) {
692 if ((t->c_cflag & CSTOPB) != 0)
694 if ((t->c_cflag & PARENB) != 0)
697 if (uchcom_calc_divider_settings(&dv, t->c_ospeed)) {
700 return (0); /* success */
704 uchcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
706 struct uchcom_softc *sc = ucom->sc_parent;
708 uchcom_get_version(sc, 0);
709 uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0, 0);
710 uchcom_set_baudrate(sc, t->c_ospeed);
711 uchcom_read_reg(sc, 0x18, 0, 0x25, 0);
712 uchcom_write_reg(sc, 0x18, 0x50, 0x25, 0x00);
713 uchcom_update_status(sc);
714 uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0x501f, 0xd90a);
715 uchcom_set_baudrate(sc, t->c_ospeed);
716 uchcom_set_dtr_rts(sc);
717 uchcom_update_status(sc);
721 uchcom_start_read(struct ucom_softc *ucom)
723 struct uchcom_softc *sc = ucom->sc_parent;
725 /* start interrupt endpoint */
726 usbd_transfer_start(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
728 /* start read endpoint */
729 usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
733 uchcom_stop_read(struct ucom_softc *ucom)
735 struct uchcom_softc *sc = ucom->sc_parent;
737 /* stop interrupt endpoint */
738 usbd_transfer_stop(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
740 /* stop read endpoint */
741 usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
745 uchcom_start_write(struct ucom_softc *ucom)
747 struct uchcom_softc *sc = ucom->sc_parent;
749 usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
753 uchcom_stop_write(struct ucom_softc *ucom)
755 struct uchcom_softc *sc = ucom->sc_parent;
757 usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
760 /* ----------------------------------------------------------------------
761 * callback when the modem status is changed.
764 uchcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
766 struct uchcom_softc *sc = usbd_xfer_softc(xfer);
767 struct usb_page_cache *pc;
768 uint8_t buf[UCHCOM_INTR_LEAST];
771 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
773 switch (USB_GET_STATE(xfer)) {
774 case USB_ST_TRANSFERRED:
776 DPRINTF("actlen = %u\n", actlen);
778 if (actlen >= UCHCOM_INTR_LEAST) {
779 pc = usbd_xfer_get_frame(xfer, 0);
780 usbd_copy_out(pc, 0, buf, UCHCOM_INTR_LEAST);
782 DPRINTF("data = 0x%02X 0x%02X 0x%02X 0x%02X\n",
783 (unsigned)buf[0], (unsigned)buf[1],
784 (unsigned)buf[2], (unsigned)buf[3]);
786 uchcom_convert_status(sc, buf[UCHCOM_INTR_STAT1]);
787 ucom_status_change(&sc->sc_ucom);
791 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
792 usbd_transfer_submit(xfer);
796 if (error != USB_ERR_CANCELLED) {
797 /* try to clear stall first */
798 usbd_xfer_set_stall(xfer);
806 uchcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
808 struct uchcom_softc *sc = usbd_xfer_softc(xfer);
809 struct usb_page_cache *pc;
812 switch (USB_GET_STATE(xfer)) {
814 case USB_ST_TRANSFERRED:
816 pc = usbd_xfer_get_frame(xfer, 0);
817 if (ucom_get_data(&sc->sc_ucom, pc, 0,
818 usbd_xfer_max_len(xfer), &actlen)) {
820 DPRINTF("actlen = %d\n", actlen);
822 usbd_xfer_set_frame_len(xfer, 0, actlen);
823 usbd_transfer_submit(xfer);
828 if (error != USB_ERR_CANCELLED) {
829 /* try to clear stall first */
830 usbd_xfer_set_stall(xfer);
838 uchcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
840 struct uchcom_softc *sc = usbd_xfer_softc(xfer);
841 struct usb_page_cache *pc;
844 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
846 switch (USB_GET_STATE(xfer)) {
847 case USB_ST_TRANSFERRED:
850 pc = usbd_xfer_get_frame(xfer, 0);
851 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
856 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
857 usbd_transfer_submit(xfer);
861 if (error != USB_ERR_CANCELLED) {
862 /* try to clear stall first */
863 usbd_xfer_set_stall(xfer);
871 uchcom_poll(struct ucom_softc *ucom)
873 struct uchcom_softc *sc = ucom->sc_parent;
874 usbd_transfer_poll(sc->sc_xfer, UCHCOM_N_TRANSFER);
877 static device_method_t uchcom_methods[] = {
878 /* Device interface */
879 DEVMETHOD(device_probe, uchcom_probe),
880 DEVMETHOD(device_attach, uchcom_attach),
881 DEVMETHOD(device_detach, uchcom_detach),
885 static driver_t uchcom_driver = {
887 .methods = uchcom_methods,
888 .size = sizeof(struct uchcom_softc)
891 static devclass_t uchcom_devclass;
893 DRIVER_MODULE(uchcom, uhub, uchcom_driver, uchcom_devclass, NULL, 0);
894 MODULE_DEPEND(uchcom, ucom, 1, 1, 1);
895 MODULE_DEPEND(uchcom, usb, 1, 1, 1);
896 MODULE_VERSION(uchcom, 1);
897 USB_PNP_HOST_INFO(uchcom_devs);