1 /* $NetBSD: uchcom.c,v 1.1 2007/09/03 17:57:37 tshiozak Exp $ */
4 * Copyright (c) 2007, Takanori Watanabe
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
30 * Copyright (c) 2007 The NetBSD Foundation, Inc.
31 * All rights reserved.
33 * This code is derived from software contributed to The NetBSD Foundation
34 * by Takuya SHIOZAKI (tshiozak@netbsd.org).
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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.
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
69 * Driver for WinChipHead CH341/340, the worst USB-serial chip in the
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>
81 #include <sys/linker_set.h>
82 #include <sys/module.h>
84 #include <sys/mutex.h>
85 #include <sys/condvar.h>
86 #include <sys/sysctl.h>
88 #include <sys/unistd.h>
89 #include <sys/callout.h>
90 #include <sys/malloc.h>
93 #include <dev/usb/usb.h>
94 #include <dev/usb/usbdi.h>
95 #include <dev/usb/usbdi_util.h>
98 #define USB_DEBUG_VAR uchcom_debug
99 #include <dev/usb/usb_debug.h>
100 #include <dev/usb/usb_process.h>
102 #include <dev/usb/serial/usb_serial.h>
105 static int uchcom_debug = 0;
107 SYSCTL_NODE(_hw_usb, OID_AUTO, uchcom, CTLFLAG_RW, 0, "USB uchcom");
108 SYSCTL_INT(_hw_usb_uchcom, OID_AUTO, debug, CTLFLAG_RW,
109 &uchcom_debug, 0, "uchcom debug level");
112 #define UCHCOM_IFACE_INDEX 0
113 #define UCHCOM_CONFIG_INDEX 0
115 #define UCHCOM_REV_CH340 0x0250
116 #define UCHCOM_INPUT_BUF_SIZE 8
118 #define UCHCOM_REQ_GET_VERSION 0x5F
119 #define UCHCOM_REQ_READ_REG 0x95
120 #define UCHCOM_REQ_WRITE_REG 0x9A
121 #define UCHCOM_REQ_RESET 0xA1
122 #define UCHCOM_REQ_SET_DTRRTS 0xA4
124 #define UCHCOM_REG_STAT1 0x06
125 #define UCHCOM_REG_STAT2 0x07
126 #define UCHCOM_REG_BPS_PRE 0x12
127 #define UCHCOM_REG_BPS_DIV 0x13
128 #define UCHCOM_REG_BPS_MOD 0x14
129 #define UCHCOM_REG_BPS_PAD 0x0F
130 #define UCHCOM_REG_BREAK1 0x05
131 #define UCHCOM_REG_BREAK2 0x18
132 #define UCHCOM_REG_LCR1 0x18
133 #define UCHCOM_REG_LCR2 0x25
135 #define UCHCOM_VER_20 0x20
137 #define UCHCOM_BASE_UNKNOWN 0
138 #define UCHCOM_BPS_MOD_BASE 20000000
139 #define UCHCOM_BPS_MOD_BASE_OFS 1100
141 #define UCHCOM_DTR_MASK 0x20
142 #define UCHCOM_RTS_MASK 0x40
144 #define UCHCOM_BRK1_MASK 0x01
145 #define UCHCOM_BRK2_MASK 0x40
147 #define UCHCOM_LCR1_MASK 0xAF
148 #define UCHCOM_LCR2_MASK 0x07
149 #define UCHCOM_LCR1_PARENB 0x80
150 #define UCHCOM_LCR2_PAREVEN 0x07
151 #define UCHCOM_LCR2_PARODD 0x06
152 #define UCHCOM_LCR2_PARMARK 0x05
153 #define UCHCOM_LCR2_PARSPACE 0x04
155 #define UCHCOM_INTR_STAT1 0x02
156 #define UCHCOM_INTR_STAT2 0x03
157 #define UCHCOM_INTR_LEAST 4
159 #define UCHCOM_BULK_BUF_SIZE 1024 /* bytes */
168 struct uchcom_softc {
169 struct ucom_super_softc sc_super_ucom;
170 struct ucom_softc sc_ucom;
172 struct usb_xfer *sc_xfer[UCHCOM_N_TRANSFER];
173 struct usb_device *sc_udev;
176 uint8_t sc_dtr; /* local copy */
177 uint8_t sc_rts; /* local copy */
180 uint8_t sc_lsr; /* local status register */
183 struct uchcom_divider {
184 uint8_t dv_prescaler;
189 struct uchcom_divider_record {
192 uint32_t dvr_base_clock;
193 struct uchcom_divider dvr_divider;
196 static const struct uchcom_divider_record dividers[] =
198 {307200, 307200, UCHCOM_BASE_UNKNOWN, {7, 0xD9, 0}},
199 {921600, 921600, UCHCOM_BASE_UNKNOWN, {7, 0xF3, 0}},
200 {2999999, 23530, 6000000, {3, 0, 0}},
201 {23529, 2942, 750000, {2, 0, 0}},
202 {2941, 368, 93750, {1, 0, 0}},
203 {367, 1, 11719, {0, 0, 0}},
206 #define NUM_DIVIDERS (sizeof (dividers) / sizeof (dividers[0]))
208 static const struct usb_device_id uchcom_devs[] = {
209 {USB_VPI(USB_VENDOR_WCH, USB_PRODUCT_WCH_CH341SER, 0)},
210 {USB_VPI(USB_VENDOR_WCH2, USB_PRODUCT_WCH2_CH341SER, 0)},
215 static int uchcom_pre_param(struct ucom_softc *, struct termios *);
216 static void uchcom_cfg_get_status(struct ucom_softc *, 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);
235 static device_probe_t uchcom_probe;
236 static device_attach_t uchcom_attach;
237 static device_detach_t uchcom_detach;
239 static usb_callback_t uchcom_intr_callback;
240 static usb_callback_t uchcom_write_callback;
241 static usb_callback_t uchcom_read_callback;
243 static const struct usb_config uchcom_config_data[UCHCOM_N_TRANSFER] = {
245 [UCHCOM_BULK_DT_WR] = {
247 .endpoint = UE_ADDR_ANY,
248 .direction = UE_DIR_OUT,
249 .bufsize = UCHCOM_BULK_BUF_SIZE,
250 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
251 .callback = &uchcom_write_callback,
254 [UCHCOM_BULK_DT_RD] = {
256 .endpoint = UE_ADDR_ANY,
257 .direction = UE_DIR_IN,
258 .bufsize = UCHCOM_BULK_BUF_SIZE,
259 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
260 .callback = &uchcom_read_callback,
263 [UCHCOM_INTR_DT_RD] = {
264 .type = UE_INTERRUPT,
265 .endpoint = UE_ADDR_ANY,
266 .direction = UE_DIR_IN,
267 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
268 .bufsize = 0, /* use wMaxPacketSize */
269 .callback = &uchcom_intr_callback,
273 static struct ucom_callback uchcom_callback = {
274 .ucom_cfg_get_status = &uchcom_cfg_get_status,
275 .ucom_cfg_set_dtr = &uchcom_cfg_set_dtr,
276 .ucom_cfg_set_rts = &uchcom_cfg_set_rts,
277 .ucom_cfg_set_break = &uchcom_cfg_set_break,
278 .ucom_cfg_open = &uchcom_cfg_open,
279 .ucom_cfg_param = &uchcom_cfg_param,
280 .ucom_pre_param = &uchcom_pre_param,
281 .ucom_start_read = &uchcom_start_read,
282 .ucom_stop_read = &uchcom_stop_read,
283 .ucom_start_write = &uchcom_start_write,
284 .ucom_stop_write = &uchcom_stop_write,
285 .ucom_poll = &uchcom_poll,
288 /* ----------------------------------------------------------------------
289 * driver entry points
293 uchcom_probe(device_t dev)
295 struct usb_attach_arg *uaa = device_get_ivars(dev);
299 if (uaa->usb_mode != USB_MODE_HOST) {
302 if (uaa->info.bConfigIndex != UCHCOM_CONFIG_INDEX) {
305 if (uaa->info.bIfaceIndex != UCHCOM_IFACE_INDEX) {
308 return (usbd_lookup_id_by_uaa(uchcom_devs, sizeof(uchcom_devs), uaa));
312 uchcom_attach(device_t dev)
314 struct uchcom_softc *sc = device_get_softc(dev);
315 struct usb_attach_arg *uaa = device_get_ivars(dev);
321 device_set_usb_desc(dev);
322 mtx_init(&sc->sc_mtx, "uchcom", NULL, MTX_DEF);
324 sc->sc_udev = uaa->device;
326 switch (uaa->info.bcdDevice) {
327 case UCHCOM_REV_CH340:
328 device_printf(dev, "CH340 detected\n");
331 device_printf(dev, "CH341 detected\n");
335 iface_index = UCHCOM_IFACE_INDEX;
336 error = usbd_transfer_setup(uaa->device,
337 &iface_index, sc->sc_xfer, uchcom_config_data,
338 UCHCOM_N_TRANSFER, sc, &sc->sc_mtx);
341 DPRINTF("one or more missing USB endpoints, "
342 "error=%s\n", usbd_errstr(error));
346 /* clear stall at first run */
347 mtx_lock(&sc->sc_mtx);
348 usbd_xfer_set_stall(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
349 usbd_xfer_set_stall(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
350 mtx_unlock(&sc->sc_mtx);
352 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
353 &uchcom_callback, &sc->sc_mtx);
365 uchcom_detach(device_t dev)
367 struct uchcom_softc *sc = device_get_softc(dev);
371 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
372 usbd_transfer_unsetup(sc->sc_xfer, UCHCOM_N_TRANSFER);
373 mtx_destroy(&sc->sc_mtx);
378 /* ----------------------------------------------------------------------
383 uchcom_ctrl_write(struct uchcom_softc *sc, uint8_t reqno,
384 uint16_t value, uint16_t index)
386 struct usb_device_request req;
388 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
389 req.bRequest = reqno;
390 USETW(req.wValue, value);
391 USETW(req.wIndex, index);
392 USETW(req.wLength, 0);
394 ucom_cfg_do_request(sc->sc_udev,
395 &sc->sc_ucom, &req, NULL, 0, 1000);
399 uchcom_ctrl_read(struct uchcom_softc *sc, uint8_t reqno,
400 uint16_t value, uint16_t index, void *buf, uint16_t buflen)
402 struct usb_device_request req;
404 req.bmRequestType = UT_READ_VENDOR_DEVICE;
405 req.bRequest = reqno;
406 USETW(req.wValue, value);
407 USETW(req.wIndex, index);
408 USETW(req.wLength, buflen);
410 ucom_cfg_do_request(sc->sc_udev,
411 &sc->sc_ucom, &req, buf, USB_SHORT_XFER_OK, 1000);
415 uchcom_write_reg(struct uchcom_softc *sc,
416 uint8_t reg1, uint8_t val1, uint8_t reg2, uint8_t val2)
418 DPRINTF("0x%02X<-0x%02X, 0x%02X<-0x%02X\n",
419 (unsigned)reg1, (unsigned)val1,
420 (unsigned)reg2, (unsigned)val2);
422 sc, UCHCOM_REQ_WRITE_REG,
423 reg1 | ((uint16_t)reg2 << 8), val1 | ((uint16_t)val2 << 8));
427 uchcom_read_reg(struct uchcom_softc *sc,
428 uint8_t reg1, uint8_t *rval1, uint8_t reg2, uint8_t *rval2)
430 uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
433 sc, UCHCOM_REQ_READ_REG,
434 reg1 | ((uint16_t)reg2 << 8), 0, buf, sizeof(buf));
436 DPRINTF("0x%02X->0x%02X, 0x%02X->0x%02X\n",
437 (unsigned)reg1, (unsigned)buf[0],
438 (unsigned)reg2, (unsigned)buf[1]);
447 uchcom_get_version(struct uchcom_softc *sc, uint8_t *rver)
449 uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
451 uchcom_ctrl_read(sc, UCHCOM_REQ_GET_VERSION, 0, 0, buf, sizeof(buf));
458 uchcom_get_status(struct uchcom_softc *sc, uint8_t *rval)
460 uchcom_read_reg(sc, UCHCOM_REG_STAT1, rval, UCHCOM_REG_STAT2, NULL);
464 uchcom_set_dtr_rts_10(struct uchcom_softc *sc, uint8_t val)
466 uchcom_write_reg(sc, UCHCOM_REG_STAT1, val, UCHCOM_REG_STAT1, val);
470 uchcom_set_dtr_rts_20(struct uchcom_softc *sc, uint8_t val)
472 uchcom_ctrl_write(sc, UCHCOM_REQ_SET_DTRRTS, val, 0);
476 /* ----------------------------------------------------------------------
481 uchcom_update_version(struct uchcom_softc *sc)
483 uchcom_get_version(sc, &sc->sc_version);
487 uchcom_convert_status(struct uchcom_softc *sc, uint8_t cur)
489 sc->sc_dtr = !(cur & UCHCOM_DTR_MASK);
490 sc->sc_rts = !(cur & UCHCOM_RTS_MASK);
493 sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur);
497 uchcom_update_status(struct uchcom_softc *sc)
501 uchcom_get_status(sc, &cur);
502 uchcom_convert_status(sc, cur);
507 uchcom_set_dtr_rts(struct uchcom_softc *sc)
512 val |= UCHCOM_DTR_MASK;
514 val |= UCHCOM_RTS_MASK;
516 if (sc->sc_version < UCHCOM_VER_20)
517 uchcom_set_dtr_rts_10(sc, ~val);
519 uchcom_set_dtr_rts_20(sc, ~val);
523 uchcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
525 struct uchcom_softc *sc = ucom->sc_parent;
529 uchcom_read_reg(sc, UCHCOM_REG_BREAK1, &brk1, UCHCOM_REG_BREAK2, &brk2);
531 /* on - clear bits */
532 brk1 &= ~UCHCOM_BRK1_MASK;
533 brk2 &= ~UCHCOM_BRK2_MASK;
536 brk1 |= UCHCOM_BRK1_MASK;
537 brk2 |= UCHCOM_BRK2_MASK;
539 uchcom_write_reg(sc, UCHCOM_REG_BREAK1, brk1, UCHCOM_REG_BREAK2, brk2);
543 uchcom_calc_divider_settings(struct uchcom_divider *dp, uint32_t rate)
545 const struct uchcom_divider_record *rp;
552 for (i = 0; i != NUM_DIVIDERS; i++) {
553 if (dividers[i].dvr_high >= rate &&
554 dividers[i].dvr_low <= rate) {
562 dp->dv_prescaler = rp->dvr_divider.dv_prescaler;
563 if (rp->dvr_base_clock == UCHCOM_BASE_UNKNOWN)
564 dp->dv_div = rp->dvr_divider.dv_div;
566 div = rp->dvr_base_clock / rate;
567 rem = rp->dvr_base_clock % rate;
568 if (div == 0 || div >= 0xFF)
570 if ((rem << 1) >= rate)
572 dp->dv_div = (uint8_t)-div;
575 mod = (UCHCOM_BPS_MOD_BASE / rate) + UCHCOM_BPS_MOD_BASE_OFS;
576 mod = mod + (mod / 2);
578 dp->dv_mod = (mod + 0xFF) / 0x100;
584 uchcom_set_baudrate(struct uchcom_softc *sc, uint32_t rate)
586 struct uchcom_divider dv;
588 if (uchcom_calc_divider_settings(&dv, rate))
592 UCHCOM_REG_BPS_PRE, dv.dv_prescaler,
593 UCHCOM_REG_BPS_DIV, dv.dv_div);
595 UCHCOM_REG_BPS_MOD, dv.dv_mod,
596 UCHCOM_REG_BPS_PAD, 0);
599 /* ----------------------------------------------------------------------
603 uchcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
605 struct uchcom_softc *sc = ucom->sc_parent;
614 uchcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
616 struct uchcom_softc *sc = ucom->sc_parent;
618 DPRINTF("onoff = %d\n", onoff);
621 uchcom_set_dtr_rts(sc);
625 uchcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
627 struct uchcom_softc *sc = ucom->sc_parent;
629 DPRINTF("onoff = %d\n", onoff);
632 uchcom_set_dtr_rts(sc);
636 uchcom_cfg_open(struct ucom_softc *ucom)
638 struct uchcom_softc *sc = ucom->sc_parent;
642 uchcom_update_version(sc);
643 uchcom_update_status(sc);
647 uchcom_pre_param(struct ucom_softc *ucom, struct termios *t)
649 struct uchcom_divider dv;
651 switch (t->c_cflag & CSIZE) {
658 if (uchcom_calc_divider_settings(&dv, t->c_ospeed)) {
661 return (0); /* success */
665 uchcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
667 struct uchcom_softc *sc = ucom->sc_parent;
669 uchcom_get_version(sc, 0);
670 uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0, 0);
671 uchcom_set_baudrate(sc, t->c_ospeed);
672 uchcom_read_reg(sc, 0x18, 0, 0x25, 0);
673 uchcom_write_reg(sc, 0x18, 0x50, 0x25, 0x00);
674 uchcom_update_status(sc);
675 uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0x501f, 0xd90a);
676 uchcom_set_baudrate(sc, t->c_ospeed);
677 uchcom_set_dtr_rts(sc);
678 uchcom_update_status(sc);
682 uchcom_start_read(struct ucom_softc *ucom)
684 struct uchcom_softc *sc = ucom->sc_parent;
686 /* start interrupt endpoint */
687 usbd_transfer_start(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
689 /* start read endpoint */
690 usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
694 uchcom_stop_read(struct ucom_softc *ucom)
696 struct uchcom_softc *sc = ucom->sc_parent;
698 /* stop interrupt endpoint */
699 usbd_transfer_stop(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
701 /* stop read endpoint */
702 usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
706 uchcom_start_write(struct ucom_softc *ucom)
708 struct uchcom_softc *sc = ucom->sc_parent;
710 usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
714 uchcom_stop_write(struct ucom_softc *ucom)
716 struct uchcom_softc *sc = ucom->sc_parent;
718 usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
721 /* ----------------------------------------------------------------------
722 * callback when the modem status is changed.
725 uchcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
727 struct uchcom_softc *sc = usbd_xfer_softc(xfer);
728 struct usb_page_cache *pc;
729 uint8_t buf[UCHCOM_INTR_LEAST];
732 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
734 switch (USB_GET_STATE(xfer)) {
735 case USB_ST_TRANSFERRED:
737 DPRINTF("actlen = %u\n", actlen);
739 if (actlen >= UCHCOM_INTR_LEAST) {
740 pc = usbd_xfer_get_frame(xfer, 0);
741 usbd_copy_out(pc, 0, buf, UCHCOM_INTR_LEAST);
743 DPRINTF("data = 0x%02X 0x%02X 0x%02X 0x%02X\n",
744 (unsigned)buf[0], (unsigned)buf[1],
745 (unsigned)buf[2], (unsigned)buf[3]);
747 uchcom_convert_status(sc, buf[UCHCOM_INTR_STAT1]);
748 ucom_status_change(&sc->sc_ucom);
752 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
753 usbd_transfer_submit(xfer);
757 if (error != USB_ERR_CANCELLED) {
758 /* try to clear stall first */
759 usbd_xfer_set_stall(xfer);
767 uchcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
769 struct uchcom_softc *sc = usbd_xfer_softc(xfer);
770 struct usb_page_cache *pc;
773 switch (USB_GET_STATE(xfer)) {
775 case USB_ST_TRANSFERRED:
777 pc = usbd_xfer_get_frame(xfer, 0);
778 if (ucom_get_data(&sc->sc_ucom, pc, 0,
779 usbd_xfer_max_len(xfer), &actlen)) {
781 DPRINTF("actlen = %d\n", actlen);
783 usbd_xfer_set_frame_len(xfer, 0, actlen);
784 usbd_transfer_submit(xfer);
789 if (error != USB_ERR_CANCELLED) {
790 /* try to clear stall first */
791 usbd_xfer_set_stall(xfer);
799 uchcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
801 struct uchcom_softc *sc = usbd_xfer_softc(xfer);
802 struct usb_page_cache *pc;
805 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
807 switch (USB_GET_STATE(xfer)) {
808 case USB_ST_TRANSFERRED:
811 pc = usbd_xfer_get_frame(xfer, 0);
812 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
817 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
818 usbd_transfer_submit(xfer);
822 if (error != USB_ERR_CANCELLED) {
823 /* try to clear stall first */
824 usbd_xfer_set_stall(xfer);
832 uchcom_poll(struct ucom_softc *ucom)
834 struct uchcom_softc *sc = ucom->sc_parent;
835 usbd_transfer_poll(sc->sc_xfer, UCHCOM_N_TRANSFER);
838 static device_method_t uchcom_methods[] = {
839 /* Device interface */
840 DEVMETHOD(device_probe, uchcom_probe),
841 DEVMETHOD(device_attach, uchcom_attach),
842 DEVMETHOD(device_detach, uchcom_detach),
847 static driver_t uchcom_driver = {
850 sizeof(struct uchcom_softc)
853 static devclass_t uchcom_devclass;
855 DRIVER_MODULE(uchcom, uhub, uchcom_driver, uchcom_devclass, NULL, 0);
856 MODULE_DEPEND(uchcom, ucom, 1, 1, 1);
857 MODULE_DEPEND(uchcom, usb, 1, 1, 1);