]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/serial/uchcom.c
Import Intel Processor Trace decoder library from
[FreeBSD/FreeBSD.git] / sys / dev / usb / serial / uchcom.c
1 /*      $NetBSD: uchcom.c,v 1.1 2007/09/03 17:57:37 tshiozak Exp $      */
2
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-2-Clause-NetBSD
5  *
6  * Copyright (c) 2007, Takanori Watanabe
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  */
30
31 /*
32  * Copyright (c) 2007 The NetBSD Foundation, Inc.
33  * All rights reserved.
34  *
35  * This code is derived from software contributed to The NetBSD Foundation
36  * by Takuya SHIOZAKI (tshiozak@netbsd.org).
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
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.
46  *
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.
58  */
59
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
62
63 /*
64  * Driver for WinChipHead CH341/340, the worst USB-serial chip in the
65  * world.
66  */
67
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>
75 #include <sys/bus.h>
76 #include <sys/module.h>
77 #include <sys/lock.h>
78 #include <sys/mutex.h>
79 #include <sys/condvar.h>
80 #include <sys/sysctl.h>
81 #include <sys/sx.h>
82 #include <sys/unistd.h>
83 #include <sys/callout.h>
84 #include <sys/malloc.h>
85 #include <sys/priv.h>
86
87 #include <dev/usb/usb.h>
88 #include <dev/usb/usbdi.h>
89 #include <dev/usb/usbdi_util.h>
90 #include "usbdevs.h"
91
92 #define USB_DEBUG_VAR uchcom_debug
93 #include <dev/usb/usb_debug.h>
94 #include <dev/usb/usb_process.h>
95
96 #include <dev/usb/serial/usb_serial.h>
97
98 #ifdef USB_DEBUG
99 static int uchcom_debug = 0;
100
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");
104 #endif
105
106 #define UCHCOM_IFACE_INDEX      0
107 #define UCHCOM_CONFIG_INDEX     0
108
109 #define UCHCOM_REV_CH340        0x0250
110 #define UCHCOM_INPUT_BUF_SIZE   8
111
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
117
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
128
129 #define UCHCOM_VER_20           0x20
130
131 #define UCHCOM_BASE_UNKNOWN     0
132 #define UCHCOM_BPS_MOD_BASE     20000000
133 #define UCHCOM_BPS_MOD_BASE_OFS 1100
134
135 #define UCHCOM_DTR_MASK         0x20
136 #define UCHCOM_RTS_MASK         0x40
137
138 #define UCHCOM_BRK1_MASK        0x01
139 #define UCHCOM_BRK2_MASK        0x40
140
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
148
149 #define UCHCOM_INTR_STAT1       0x02
150 #define UCHCOM_INTR_STAT2       0x03
151 #define UCHCOM_INTR_LEAST       4
152
153 #define UCHCOM_BULK_BUF_SIZE 1024       /* bytes */
154
155 enum {
156         UCHCOM_BULK_DT_WR,
157         UCHCOM_BULK_DT_RD,
158         UCHCOM_INTR_DT_RD,
159         UCHCOM_N_TRANSFER,
160 };
161
162 struct uchcom_softc {
163         struct ucom_super_softc sc_super_ucom;
164         struct ucom_softc sc_ucom;
165
166         struct usb_xfer *sc_xfer[UCHCOM_N_TRANSFER];
167         struct usb_device *sc_udev;
168         struct mtx sc_mtx;
169
170         uint8_t sc_dtr;                 /* local copy */
171         uint8_t sc_rts;                 /* local copy */
172         uint8_t sc_version;
173         uint8_t sc_msr;
174         uint8_t sc_lsr;                 /* local status register */
175 };
176
177 struct uchcom_divider {
178         uint8_t dv_prescaler;
179         uint8_t dv_div;
180         uint8_t dv_mod;
181 };
182
183 struct uchcom_divider_record {
184         uint32_t dvr_high;
185         uint32_t dvr_low;
186         uint32_t dvr_base_clock;
187         struct uchcom_divider dvr_divider;
188 };
189
190 static const struct uchcom_divider_record dividers[] =
191 {
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}},
198 };
199
200 #define NUM_DIVIDERS    nitems(dividers)
201
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)},
206 };
207
208 /* protypes */
209
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 *,
213                     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);
230
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 *);
235
236 static usb_callback_t uchcom_intr_callback;
237 static usb_callback_t uchcom_write_callback;
238 static usb_callback_t uchcom_read_callback;
239
240 static const struct usb_config uchcom_config_data[UCHCOM_N_TRANSFER] = {
241
242         [UCHCOM_BULK_DT_WR] = {
243                 .type = UE_BULK,
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,
249         },
250
251         [UCHCOM_BULK_DT_RD] = {
252                 .type = UE_BULK,
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,
258         },
259
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,
267         },
268 };
269
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,
284 };
285
286 /* ----------------------------------------------------------------------
287  * driver entry points
288  */
289
290 static int
291 uchcom_probe(device_t dev)
292 {
293         struct usb_attach_arg *uaa = device_get_ivars(dev);
294
295         DPRINTFN(11, "\n");
296
297         if (uaa->usb_mode != USB_MODE_HOST) {
298                 return (ENXIO);
299         }
300         if (uaa->info.bConfigIndex != UCHCOM_CONFIG_INDEX) {
301                 return (ENXIO);
302         }
303         if (uaa->info.bIfaceIndex != UCHCOM_IFACE_INDEX) {
304                 return (ENXIO);
305         }
306         return (usbd_lookup_id_by_uaa(uchcom_devs, sizeof(uchcom_devs), uaa));
307 }
308
309 static int
310 uchcom_attach(device_t dev)
311 {
312         struct uchcom_softc *sc = device_get_softc(dev);
313         struct usb_attach_arg *uaa = device_get_ivars(dev);
314         int error;
315         uint8_t iface_index;
316
317         DPRINTFN(11, "\n");
318
319         device_set_usb_desc(dev);
320         mtx_init(&sc->sc_mtx, "uchcom", NULL, MTX_DEF);
321         ucom_ref(&sc->sc_super_ucom);
322
323         sc->sc_udev = uaa->device;
324
325         switch (uaa->info.bcdDevice) {
326         case UCHCOM_REV_CH340:
327                 device_printf(dev, "CH340 detected\n");
328                 break;
329         default:
330                 device_printf(dev, "CH341 detected\n");
331                 break;
332         }
333
334         iface_index = UCHCOM_IFACE_INDEX;
335         error = usbd_transfer_setup(uaa->device,
336             &iface_index, sc->sc_xfer, uchcom_config_data,
337             UCHCOM_N_TRANSFER, sc, &sc->sc_mtx);
338
339         if (error) {
340                 DPRINTF("one or more missing USB endpoints, "
341                     "error=%s\n", usbd_errstr(error));
342                 goto detach;
343         }
344
345         /* clear stall at first run */
346         mtx_lock(&sc->sc_mtx);
347         usbd_xfer_set_stall(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
348         usbd_xfer_set_stall(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
349         mtx_unlock(&sc->sc_mtx);
350
351         error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
352             &uchcom_callback, &sc->sc_mtx);
353         if (error) {
354                 goto detach;
355         }
356         ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
357
358         return (0);
359
360 detach:
361         uchcom_detach(dev);
362         return (ENXIO);
363 }
364
365 static int
366 uchcom_detach(device_t dev)
367 {
368         struct uchcom_softc *sc = device_get_softc(dev);
369
370         DPRINTFN(11, "\n");
371
372         ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
373         usbd_transfer_unsetup(sc->sc_xfer, UCHCOM_N_TRANSFER);
374
375         device_claim_softc(dev);
376
377         uchcom_free_softc(sc);
378
379         return (0);
380 }
381
382 UCOM_UNLOAD_DRAIN(uchcom);
383
384 static void
385 uchcom_free_softc(struct uchcom_softc *sc)
386 {
387         if (ucom_unref(&sc->sc_super_ucom)) {
388                 mtx_destroy(&sc->sc_mtx);
389                 device_free_softc(sc);
390         }
391 }
392
393 static void
394 uchcom_free(struct ucom_softc *ucom)
395 {
396         uchcom_free_softc(ucom->sc_parent);
397 }
398
399 /* ----------------------------------------------------------------------
400  * low level i/o
401  */
402
403 static void
404 uchcom_ctrl_write(struct uchcom_softc *sc, uint8_t reqno,
405     uint16_t value, uint16_t index)
406 {
407         struct usb_device_request req;
408
409         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
410         req.bRequest = reqno;
411         USETW(req.wValue, value);
412         USETW(req.wIndex, index);
413         USETW(req.wLength, 0);
414
415         ucom_cfg_do_request(sc->sc_udev,
416             &sc->sc_ucom, &req, NULL, 0, 1000);
417 }
418
419 static void
420 uchcom_ctrl_read(struct uchcom_softc *sc, uint8_t reqno,
421     uint16_t value, uint16_t index, void *buf, uint16_t buflen)
422 {
423         struct usb_device_request req;
424
425         req.bmRequestType = UT_READ_VENDOR_DEVICE;
426         req.bRequest = reqno;
427         USETW(req.wValue, value);
428         USETW(req.wIndex, index);
429         USETW(req.wLength, buflen);
430
431         ucom_cfg_do_request(sc->sc_udev,
432             &sc->sc_ucom, &req, buf, USB_SHORT_XFER_OK, 1000);
433 }
434
435 static void
436 uchcom_write_reg(struct uchcom_softc *sc,
437     uint8_t reg1, uint8_t val1, uint8_t reg2, uint8_t val2)
438 {
439         DPRINTF("0x%02X<-0x%02X, 0x%02X<-0x%02X\n",
440             (unsigned)reg1, (unsigned)val1,
441             (unsigned)reg2, (unsigned)val2);
442         uchcom_ctrl_write(
443             sc, UCHCOM_REQ_WRITE_REG,
444             reg1 | ((uint16_t)reg2 << 8), val1 | ((uint16_t)val2 << 8));
445 }
446
447 static void
448 uchcom_read_reg(struct uchcom_softc *sc,
449     uint8_t reg1, uint8_t *rval1, uint8_t reg2, uint8_t *rval2)
450 {
451         uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
452
453         uchcom_ctrl_read(
454             sc, UCHCOM_REQ_READ_REG,
455             reg1 | ((uint16_t)reg2 << 8), 0, buf, sizeof(buf));
456
457         DPRINTF("0x%02X->0x%02X, 0x%02X->0x%02X\n",
458             (unsigned)reg1, (unsigned)buf[0],
459             (unsigned)reg2, (unsigned)buf[1]);
460
461         if (rval1)
462                 *rval1 = buf[0];
463         if (rval2)
464                 *rval2 = buf[1];
465 }
466
467 static void
468 uchcom_get_version(struct uchcom_softc *sc, uint8_t *rver)
469 {
470         uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
471
472         uchcom_ctrl_read(sc, UCHCOM_REQ_GET_VERSION, 0, 0, buf, sizeof(buf));
473
474         if (rver)
475                 *rver = buf[0];
476 }
477
478 static void
479 uchcom_get_status(struct uchcom_softc *sc, uint8_t *rval)
480 {
481         uchcom_read_reg(sc, UCHCOM_REG_STAT1, rval, UCHCOM_REG_STAT2, NULL);
482 }
483
484 static void
485 uchcom_set_dtr_rts_10(struct uchcom_softc *sc, uint8_t val)
486 {
487         uchcom_write_reg(sc, UCHCOM_REG_STAT1, val, UCHCOM_REG_STAT1, val);
488 }
489
490 static void
491 uchcom_set_dtr_rts_20(struct uchcom_softc *sc, uint8_t val)
492 {
493         uchcom_ctrl_write(sc, UCHCOM_REQ_SET_DTRRTS, val, 0);
494 }
495
496
497 /* ----------------------------------------------------------------------
498  * middle layer
499  */
500
501 static void
502 uchcom_update_version(struct uchcom_softc *sc)
503 {
504         uchcom_get_version(sc, &sc->sc_version);
505 }
506
507 static void
508 uchcom_convert_status(struct uchcom_softc *sc, uint8_t cur)
509 {
510         sc->sc_dtr = !(cur & UCHCOM_DTR_MASK);
511         sc->sc_rts = !(cur & UCHCOM_RTS_MASK);
512
513         cur = ~cur & 0x0F;
514         sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur);
515 }
516
517 static void
518 uchcom_update_status(struct uchcom_softc *sc)
519 {
520         uint8_t cur;
521
522         uchcom_get_status(sc, &cur);
523         uchcom_convert_status(sc, cur);
524 }
525
526
527 static void
528 uchcom_set_dtr_rts(struct uchcom_softc *sc)
529 {
530         uint8_t val = 0;
531
532         if (sc->sc_dtr)
533                 val |= UCHCOM_DTR_MASK;
534         if (sc->sc_rts)
535                 val |= UCHCOM_RTS_MASK;
536
537         if (sc->sc_version < UCHCOM_VER_20)
538                 uchcom_set_dtr_rts_10(sc, ~val);
539         else
540                 uchcom_set_dtr_rts_20(sc, ~val);
541 }
542
543 static void
544 uchcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
545 {
546         struct uchcom_softc *sc = ucom->sc_parent;
547         uint8_t brk1;
548         uint8_t brk2;
549
550         uchcom_read_reg(sc, UCHCOM_REG_BREAK1, &brk1, UCHCOM_REG_BREAK2, &brk2);
551         if (onoff) {
552                 /* on - clear bits */
553                 brk1 &= ~UCHCOM_BRK1_MASK;
554                 brk2 &= ~UCHCOM_BRK2_MASK;
555         } else {
556                 /* off - set bits */
557                 brk1 |= UCHCOM_BRK1_MASK;
558                 brk2 |= UCHCOM_BRK2_MASK;
559         }
560         uchcom_write_reg(sc, UCHCOM_REG_BREAK1, brk1, UCHCOM_REG_BREAK2, brk2);
561 }
562
563 static int
564 uchcom_calc_divider_settings(struct uchcom_divider *dp, uint32_t rate)
565 {
566         const struct uchcom_divider_record *rp;
567         uint32_t div;
568         uint32_t rem;
569         uint32_t mod;
570         uint8_t i;
571
572         /* find record */
573         for (i = 0; i != NUM_DIVIDERS; i++) {
574                 if (dividers[i].dvr_high >= rate &&
575                     dividers[i].dvr_low <= rate) {
576                         rp = &dividers[i];
577                         goto found;
578                 }
579         }
580         return (-1);
581
582 found:
583         dp->dv_prescaler = rp->dvr_divider.dv_prescaler;
584         if (rp->dvr_base_clock == UCHCOM_BASE_UNKNOWN)
585                 dp->dv_div = rp->dvr_divider.dv_div;
586         else {
587                 div = rp->dvr_base_clock / rate;
588                 rem = rp->dvr_base_clock % rate;
589                 if (div == 0 || div >= 0xFF)
590                         return (-1);
591                 if ((rem << 1) >= rate)
592                         div += 1;
593                 dp->dv_div = (uint8_t)-div;
594         }
595
596         mod = (UCHCOM_BPS_MOD_BASE / rate) + UCHCOM_BPS_MOD_BASE_OFS;
597         mod = mod + (mod / 2);
598
599         dp->dv_mod = (mod + 0xFF) / 0x100;
600
601         return (0);
602 }
603
604 static void
605 uchcom_set_baudrate(struct uchcom_softc *sc, uint32_t rate)
606 {
607         struct uchcom_divider dv;
608
609         if (uchcom_calc_divider_settings(&dv, rate))
610                 return;
611
612         uchcom_write_reg(sc,
613             UCHCOM_REG_BPS_PRE, dv.dv_prescaler,
614             UCHCOM_REG_BPS_DIV, dv.dv_div);
615         uchcom_write_reg(sc,
616             UCHCOM_REG_BPS_MOD, dv.dv_mod,
617             UCHCOM_REG_BPS_PAD, 0);
618 }
619
620 /* ----------------------------------------------------------------------
621  * methods for ucom
622  */
623 static void
624 uchcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
625 {
626         struct uchcom_softc *sc = ucom->sc_parent;
627
628         DPRINTF("\n");
629
630         /* XXX Note: sc_lsr is always zero */
631         *lsr = sc->sc_lsr;
632         *msr = sc->sc_msr;
633 }
634
635 static void
636 uchcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
637 {
638         struct uchcom_softc *sc = ucom->sc_parent;
639
640         DPRINTF("onoff = %d\n", onoff);
641
642         sc->sc_dtr = onoff;
643         uchcom_set_dtr_rts(sc);
644 }
645
646 static void
647 uchcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
648 {
649         struct uchcom_softc *sc = ucom->sc_parent;
650
651         DPRINTF("onoff = %d\n", onoff);
652
653         sc->sc_rts = onoff;
654         uchcom_set_dtr_rts(sc);
655 }
656
657 static void
658 uchcom_cfg_open(struct ucom_softc *ucom)
659 {
660         struct uchcom_softc *sc = ucom->sc_parent;
661
662         DPRINTF("\n");
663
664         uchcom_update_version(sc);
665         uchcom_update_status(sc);
666 }
667
668 static int
669 uchcom_pre_param(struct ucom_softc *ucom, struct termios *t)
670 {
671         struct uchcom_divider dv;
672
673         switch (t->c_cflag & CSIZE) {
674         case CS8:
675                 break;
676         default:
677                 return (EIO);
678         }
679
680         if (uchcom_calc_divider_settings(&dv, t->c_ospeed)) {
681                 return (EIO);
682         }
683         return (0);                     /* success */
684 }
685
686 static void
687 uchcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
688 {
689         struct uchcom_softc *sc = ucom->sc_parent;
690
691         uchcom_get_version(sc, 0);
692         uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0, 0);
693         uchcom_set_baudrate(sc, t->c_ospeed);
694         uchcom_read_reg(sc, 0x18, 0, 0x25, 0);
695         uchcom_write_reg(sc, 0x18, 0x50, 0x25, 0x00);
696         uchcom_update_status(sc);
697         uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0x501f, 0xd90a);
698         uchcom_set_baudrate(sc, t->c_ospeed);
699         uchcom_set_dtr_rts(sc);
700         uchcom_update_status(sc);
701 }
702
703 static void
704 uchcom_start_read(struct ucom_softc *ucom)
705 {
706         struct uchcom_softc *sc = ucom->sc_parent;
707
708         /* start interrupt endpoint */
709         usbd_transfer_start(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
710
711         /* start read endpoint */
712         usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
713 }
714
715 static void
716 uchcom_stop_read(struct ucom_softc *ucom)
717 {
718         struct uchcom_softc *sc = ucom->sc_parent;
719
720         /* stop interrupt endpoint */
721         usbd_transfer_stop(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
722
723         /* stop read endpoint */
724         usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
725 }
726
727 static void
728 uchcom_start_write(struct ucom_softc *ucom)
729 {
730         struct uchcom_softc *sc = ucom->sc_parent;
731
732         usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
733 }
734
735 static void
736 uchcom_stop_write(struct ucom_softc *ucom)
737 {
738         struct uchcom_softc *sc = ucom->sc_parent;
739
740         usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
741 }
742
743 /* ----------------------------------------------------------------------
744  * callback when the modem status is changed.
745  */
746 static void
747 uchcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
748 {
749         struct uchcom_softc *sc = usbd_xfer_softc(xfer);
750         struct usb_page_cache *pc;
751         uint8_t buf[UCHCOM_INTR_LEAST];
752         int actlen;
753
754         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
755
756         switch (USB_GET_STATE(xfer)) {
757         case USB_ST_TRANSFERRED:
758
759                 DPRINTF("actlen = %u\n", actlen);
760
761                 if (actlen >= UCHCOM_INTR_LEAST) {
762                         pc = usbd_xfer_get_frame(xfer, 0);
763                         usbd_copy_out(pc, 0, buf, UCHCOM_INTR_LEAST);
764
765                         DPRINTF("data = 0x%02X 0x%02X 0x%02X 0x%02X\n",
766                             (unsigned)buf[0], (unsigned)buf[1],
767                             (unsigned)buf[2], (unsigned)buf[3]);
768
769                         uchcom_convert_status(sc, buf[UCHCOM_INTR_STAT1]);
770                         ucom_status_change(&sc->sc_ucom);
771                 }
772         case USB_ST_SETUP:
773 tr_setup:
774                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
775                 usbd_transfer_submit(xfer);
776                 break;
777
778         default:                        /* Error */
779                 if (error != USB_ERR_CANCELLED) {
780                         /* try to clear stall first */
781                         usbd_xfer_set_stall(xfer);
782                         goto tr_setup;
783                 }
784                 break;
785         }
786 }
787
788 static void
789 uchcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
790 {
791         struct uchcom_softc *sc = usbd_xfer_softc(xfer);
792         struct usb_page_cache *pc;
793         uint32_t actlen;
794
795         switch (USB_GET_STATE(xfer)) {
796         case USB_ST_SETUP:
797         case USB_ST_TRANSFERRED:
798 tr_setup:
799                 pc = usbd_xfer_get_frame(xfer, 0);
800                 if (ucom_get_data(&sc->sc_ucom, pc, 0,
801                     usbd_xfer_max_len(xfer), &actlen)) {
802
803                         DPRINTF("actlen = %d\n", actlen);
804
805                         usbd_xfer_set_frame_len(xfer, 0, actlen);
806                         usbd_transfer_submit(xfer);
807                 }
808                 break;
809
810         default:                        /* Error */
811                 if (error != USB_ERR_CANCELLED) {
812                         /* try to clear stall first */
813                         usbd_xfer_set_stall(xfer);
814                         goto tr_setup;
815                 }
816                 break;
817         }
818 }
819
820 static void
821 uchcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
822 {
823         struct uchcom_softc *sc = usbd_xfer_softc(xfer);
824         struct usb_page_cache *pc;
825         int actlen;
826
827         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
828
829         switch (USB_GET_STATE(xfer)) {
830         case USB_ST_TRANSFERRED:
831
832                 if (actlen > 0) {
833                         pc = usbd_xfer_get_frame(xfer, 0);
834                         ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
835                 }
836
837         case USB_ST_SETUP:
838 tr_setup:
839                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
840                 usbd_transfer_submit(xfer);
841                 break;
842
843         default:                        /* Error */
844                 if (error != USB_ERR_CANCELLED) {
845                         /* try to clear stall first */
846                         usbd_xfer_set_stall(xfer);
847                         goto tr_setup;
848                 }
849                 break;
850         }
851 }
852
853 static void
854 uchcom_poll(struct ucom_softc *ucom)
855 {
856         struct uchcom_softc *sc = ucom->sc_parent;
857         usbd_transfer_poll(sc->sc_xfer, UCHCOM_N_TRANSFER);
858 }
859
860 static device_method_t uchcom_methods[] = {
861         /* Device interface */
862         DEVMETHOD(device_probe, uchcom_probe),
863         DEVMETHOD(device_attach, uchcom_attach),
864         DEVMETHOD(device_detach, uchcom_detach),
865         DEVMETHOD_END
866 };
867
868 static driver_t uchcom_driver = {
869         .name = "uchcom",
870         .methods = uchcom_methods,
871         .size = sizeof(struct uchcom_softc)
872 };
873
874 static devclass_t uchcom_devclass;
875
876 DRIVER_MODULE(uchcom, uhub, uchcom_driver, uchcom_devclass, NULL, 0);
877 MODULE_DEPEND(uchcom, ucom, 1, 1, 1);
878 MODULE_DEPEND(uchcom, usb, 1, 1, 1);
879 MODULE_VERSION(uchcom, 1);
880 USB_PNP_HOST_INFO(uchcom_devs);