Merge branch 'vendor/BMAKE'
[dragonfly.git] / sys / bus / u4b / serial / uchcom.c
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$");
67
68 /*
69  * Driver for WinChipHead CH341/340, the worst USB-serial chip in the
70  * world.
71  */
72
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>
80 #include <sys/bus.h>
81 #include <sys/module.h>
82 #include <sys/lock.h>
83 #include <sys/mutex.h>
84 #include <sys/condvar.h>
85 #include <sys/sysctl.h>
86 #include <sys/sx.h>
87 #include <sys/unistd.h>
88 #include <sys/callout.h>
89 #include <sys/malloc.h>
90 #include <sys/priv.h>
91
92 #include <dev/usb/usb.h>
93 #include <dev/usb/usbdi.h>
94 #include <dev/usb/usbdi_util.h>
95 #include "usbdevs.h"
96
97 #define USB_DEBUG_VAR uchcom_debug
98 #include <dev/usb/usb_debug.h>
99 #include <dev/usb/usb_process.h>
100
101 #include <dev/usb/serial/usb_serial.h>
102
103 #ifdef USB_DEBUG
104 static int uchcom_debug = 0;
105
106 static SYSCTL_NODE(_hw_usb, OID_AUTO, uchcom, CTLFLAG_RW, 0, "USB uchcom");
107 SYSCTL_INT(_hw_usb_uchcom, OID_AUTO, debug, CTLFLAG_RW,
108     &uchcom_debug, 0, "uchcom debug level");
109 #endif
110
111 #define UCHCOM_IFACE_INDEX      0
112 #define UCHCOM_CONFIG_INDEX     0
113
114 #define UCHCOM_REV_CH340        0x0250
115 #define UCHCOM_INPUT_BUF_SIZE   8
116
117 #define UCHCOM_REQ_GET_VERSION  0x5F
118 #define UCHCOM_REQ_READ_REG     0x95
119 #define UCHCOM_REQ_WRITE_REG    0x9A
120 #define UCHCOM_REQ_RESET        0xA1
121 #define UCHCOM_REQ_SET_DTRRTS   0xA4
122
123 #define UCHCOM_REG_STAT1        0x06
124 #define UCHCOM_REG_STAT2        0x07
125 #define UCHCOM_REG_BPS_PRE      0x12
126 #define UCHCOM_REG_BPS_DIV      0x13
127 #define UCHCOM_REG_BPS_MOD      0x14
128 #define UCHCOM_REG_BPS_PAD      0x0F
129 #define UCHCOM_REG_BREAK1       0x05
130 #define UCHCOM_REG_BREAK2       0x18
131 #define UCHCOM_REG_LCR1         0x18
132 #define UCHCOM_REG_LCR2         0x25
133
134 #define UCHCOM_VER_20           0x20
135
136 #define UCHCOM_BASE_UNKNOWN     0
137 #define UCHCOM_BPS_MOD_BASE     20000000
138 #define UCHCOM_BPS_MOD_BASE_OFS 1100
139
140 #define UCHCOM_DTR_MASK         0x20
141 #define UCHCOM_RTS_MASK         0x40
142
143 #define UCHCOM_BRK1_MASK        0x01
144 #define UCHCOM_BRK2_MASK        0x40
145
146 #define UCHCOM_LCR1_MASK        0xAF
147 #define UCHCOM_LCR2_MASK        0x07
148 #define UCHCOM_LCR1_PARENB      0x80
149 #define UCHCOM_LCR2_PAREVEN     0x07
150 #define UCHCOM_LCR2_PARODD      0x06
151 #define UCHCOM_LCR2_PARMARK     0x05
152 #define UCHCOM_LCR2_PARSPACE    0x04
153
154 #define UCHCOM_INTR_STAT1       0x02
155 #define UCHCOM_INTR_STAT2       0x03
156 #define UCHCOM_INTR_LEAST       4
157
158 #define UCHCOM_BULK_BUF_SIZE 1024       /* bytes */
159
160 enum {
161         UCHCOM_BULK_DT_WR,
162         UCHCOM_BULK_DT_RD,
163         UCHCOM_INTR_DT_RD,
164         UCHCOM_N_TRANSFER,
165 };
166
167 struct uchcom_softc {
168         struct ucom_super_softc sc_super_ucom;
169         struct ucom_softc sc_ucom;
170
171         struct usb_xfer *sc_xfer[UCHCOM_N_TRANSFER];
172         struct usb_device *sc_udev;
173         struct mtx sc_mtx;
174
175         uint8_t sc_dtr;                 /* local copy */
176         uint8_t sc_rts;                 /* local copy */
177         uint8_t sc_version;
178         uint8_t sc_msr;
179         uint8_t sc_lsr;                 /* local status register */
180 };
181
182 struct uchcom_divider {
183         uint8_t dv_prescaler;
184         uint8_t dv_div;
185         uint8_t dv_mod;
186 };
187
188 struct uchcom_divider_record {
189         uint32_t dvr_high;
190         uint32_t dvr_low;
191         uint32_t dvr_base_clock;
192         struct uchcom_divider dvr_divider;
193 };
194
195 static const struct uchcom_divider_record dividers[] =
196 {
197         {307200, 307200, UCHCOM_BASE_UNKNOWN, {7, 0xD9, 0}},
198         {921600, 921600, UCHCOM_BASE_UNKNOWN, {7, 0xF3, 0}},
199         {2999999, 23530, 6000000, {3, 0, 0}},
200         {23529, 2942, 750000, {2, 0, 0}},
201         {2941, 368, 93750, {1, 0, 0}},
202         {367, 1, 11719, {0, 0, 0}},
203 };
204
205 #define NUM_DIVIDERS    (sizeof (dividers) / sizeof (dividers[0]))
206
207 static const STRUCT_USB_HOST_ID uchcom_devs[] = {
208         {USB_VPI(USB_VENDOR_WCH, USB_PRODUCT_WCH_CH341SER, 0)},
209         {USB_VPI(USB_VENDOR_WCH2, USB_PRODUCT_WCH2_CH341SER, 0)},
210 };
211
212 /* protypes */
213
214 static int      uchcom_pre_param(struct ucom_softc *, struct termios *);
215 static void     uchcom_cfg_get_status(struct ucom_softc *, uint8_t *,
216                     uint8_t *);
217 static void     uchcom_cfg_open(struct ucom_softc *ucom);
218 static void     uchcom_cfg_param(struct ucom_softc *, struct termios *);
219 static void     uchcom_cfg_set_break(struct ucom_softc *, uint8_t);
220 static void     uchcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
221 static void     uchcom_cfg_set_rts(struct ucom_softc *, uint8_t);
222 static void     uchcom_start_read(struct ucom_softc *);
223 static void     uchcom_start_write(struct ucom_softc *);
224 static void     uchcom_stop_read(struct ucom_softc *);
225 static void     uchcom_stop_write(struct ucom_softc *);
226 static void     uchcom_update_version(struct uchcom_softc *);
227 static void     uchcom_convert_status(struct uchcom_softc *, uint8_t);
228 static void     uchcom_update_status(struct uchcom_softc *);
229 static void     uchcom_set_dtr_rts(struct uchcom_softc *);
230 static int      uchcom_calc_divider_settings(struct uchcom_divider *, uint32_t);
231 static void     uchcom_set_baudrate(struct uchcom_softc *, uint32_t);
232 static void     uchcom_poll(struct ucom_softc *ucom);
233
234 static device_probe_t uchcom_probe;
235 static device_attach_t uchcom_attach;
236 static device_detach_t uchcom_detach;
237
238 static usb_callback_t uchcom_intr_callback;
239 static usb_callback_t uchcom_write_callback;
240 static usb_callback_t uchcom_read_callback;
241
242 static const struct usb_config uchcom_config_data[UCHCOM_N_TRANSFER] = {
243
244         [UCHCOM_BULK_DT_WR] = {
245                 .type = UE_BULK,
246                 .endpoint = UE_ADDR_ANY,
247                 .direction = UE_DIR_OUT,
248                 .bufsize = UCHCOM_BULK_BUF_SIZE,
249                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
250                 .callback = &uchcom_write_callback,
251         },
252
253         [UCHCOM_BULK_DT_RD] = {
254                 .type = UE_BULK,
255                 .endpoint = UE_ADDR_ANY,
256                 .direction = UE_DIR_IN,
257                 .bufsize = UCHCOM_BULK_BUF_SIZE,
258                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
259                 .callback = &uchcom_read_callback,
260         },
261
262         [UCHCOM_INTR_DT_RD] = {
263                 .type = UE_INTERRUPT,
264                 .endpoint = UE_ADDR_ANY,
265                 .direction = UE_DIR_IN,
266                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
267                 .bufsize = 0,   /* use wMaxPacketSize */
268                 .callback = &uchcom_intr_callback,
269         },
270 };
271
272 static struct ucom_callback uchcom_callback = {
273         .ucom_cfg_get_status = &uchcom_cfg_get_status,
274         .ucom_cfg_set_dtr = &uchcom_cfg_set_dtr,
275         .ucom_cfg_set_rts = &uchcom_cfg_set_rts,
276         .ucom_cfg_set_break = &uchcom_cfg_set_break,
277         .ucom_cfg_open = &uchcom_cfg_open,
278         .ucom_cfg_param = &uchcom_cfg_param,
279         .ucom_pre_param = &uchcom_pre_param,
280         .ucom_start_read = &uchcom_start_read,
281         .ucom_stop_read = &uchcom_stop_read,
282         .ucom_start_write = &uchcom_start_write,
283         .ucom_stop_write = &uchcom_stop_write,
284         .ucom_poll = &uchcom_poll,
285 };
286
287 /* ----------------------------------------------------------------------
288  * driver entry points
289  */
290
291 static int
292 uchcom_probe(device_t dev)
293 {
294         struct usb_attach_arg *uaa = device_get_ivars(dev);
295
296         DPRINTFN(11, "\n");
297
298         if (uaa->usb_mode != USB_MODE_HOST) {
299                 return (ENXIO);
300         }
301         if (uaa->info.bConfigIndex != UCHCOM_CONFIG_INDEX) {
302                 return (ENXIO);
303         }
304         if (uaa->info.bIfaceIndex != UCHCOM_IFACE_INDEX) {
305                 return (ENXIO);
306         }
307         return (usbd_lookup_id_by_uaa(uchcom_devs, sizeof(uchcom_devs), uaa));
308 }
309
310 static int
311 uchcom_attach(device_t dev)
312 {
313         struct uchcom_softc *sc = device_get_softc(dev);
314         struct usb_attach_arg *uaa = device_get_ivars(dev);
315         int error;
316         uint8_t iface_index;
317
318         DPRINTFN(11, "\n");
319
320         device_set_usb_desc(dev);
321         mtx_init(&sc->sc_mtx, "uchcom", NULL, MTX_DEF);
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         mtx_destroy(&sc->sc_mtx);
375
376         return (0);
377 }
378
379 /* ----------------------------------------------------------------------
380  * low level i/o
381  */
382
383 static void
384 uchcom_ctrl_write(struct uchcom_softc *sc, uint8_t reqno,
385     uint16_t value, uint16_t index)
386 {
387         struct usb_device_request req;
388
389         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
390         req.bRequest = reqno;
391         USETW(req.wValue, value);
392         USETW(req.wIndex, index);
393         USETW(req.wLength, 0);
394
395         ucom_cfg_do_request(sc->sc_udev,
396             &sc->sc_ucom, &req, NULL, 0, 1000);
397 }
398
399 static void
400 uchcom_ctrl_read(struct uchcom_softc *sc, uint8_t reqno,
401     uint16_t value, uint16_t index, void *buf, uint16_t buflen)
402 {
403         struct usb_device_request req;
404
405         req.bmRequestType = UT_READ_VENDOR_DEVICE;
406         req.bRequest = reqno;
407         USETW(req.wValue, value);
408         USETW(req.wIndex, index);
409         USETW(req.wLength, buflen);
410
411         ucom_cfg_do_request(sc->sc_udev,
412             &sc->sc_ucom, &req, buf, USB_SHORT_XFER_OK, 1000);
413 }
414
415 static void
416 uchcom_write_reg(struct uchcom_softc *sc,
417     uint8_t reg1, uint8_t val1, uint8_t reg2, uint8_t val2)
418 {
419         DPRINTF("0x%02X<-0x%02X, 0x%02X<-0x%02X\n",
420             (unsigned)reg1, (unsigned)val1,
421             (unsigned)reg2, (unsigned)val2);
422         uchcom_ctrl_write(
423             sc, UCHCOM_REQ_WRITE_REG,
424             reg1 | ((uint16_t)reg2 << 8), val1 | ((uint16_t)val2 << 8));
425 }
426
427 static void
428 uchcom_read_reg(struct uchcom_softc *sc,
429     uint8_t reg1, uint8_t *rval1, uint8_t reg2, uint8_t *rval2)
430 {
431         uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
432
433         uchcom_ctrl_read(
434             sc, UCHCOM_REQ_READ_REG,
435             reg1 | ((uint16_t)reg2 << 8), 0, buf, sizeof(buf));
436
437         DPRINTF("0x%02X->0x%02X, 0x%02X->0x%02X\n",
438             (unsigned)reg1, (unsigned)buf[0],
439             (unsigned)reg2, (unsigned)buf[1]);
440
441         if (rval1)
442                 *rval1 = buf[0];
443         if (rval2)
444                 *rval2 = buf[1];
445 }
446
447 static void
448 uchcom_get_version(struct uchcom_softc *sc, uint8_t *rver)
449 {
450         uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
451
452         uchcom_ctrl_read(sc, UCHCOM_REQ_GET_VERSION, 0, 0, buf, sizeof(buf));
453
454         if (rver)
455                 *rver = buf[0];
456 }
457
458 static void
459 uchcom_get_status(struct uchcom_softc *sc, uint8_t *rval)
460 {
461         uchcom_read_reg(sc, UCHCOM_REG_STAT1, rval, UCHCOM_REG_STAT2, NULL);
462 }
463
464 static void
465 uchcom_set_dtr_rts_10(struct uchcom_softc *sc, uint8_t val)
466 {
467         uchcom_write_reg(sc, UCHCOM_REG_STAT1, val, UCHCOM_REG_STAT1, val);
468 }
469
470 static void
471 uchcom_set_dtr_rts_20(struct uchcom_softc *sc, uint8_t val)
472 {
473         uchcom_ctrl_write(sc, UCHCOM_REQ_SET_DTRRTS, val, 0);
474 }
475
476
477 /* ----------------------------------------------------------------------
478  * middle layer
479  */
480
481 static void
482 uchcom_update_version(struct uchcom_softc *sc)
483 {
484         uchcom_get_version(sc, &sc->sc_version);
485 }
486
487 static void
488 uchcom_convert_status(struct uchcom_softc *sc, uint8_t cur)
489 {
490         sc->sc_dtr = !(cur & UCHCOM_DTR_MASK);
491         sc->sc_rts = !(cur & UCHCOM_RTS_MASK);
492
493         cur = ~cur & 0x0F;
494         sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur);
495 }
496
497 static void
498 uchcom_update_status(struct uchcom_softc *sc)
499 {
500         uint8_t cur;
501
502         uchcom_get_status(sc, &cur);
503         uchcom_convert_status(sc, cur);
504 }
505
506
507 static void
508 uchcom_set_dtr_rts(struct uchcom_softc *sc)
509 {
510         uint8_t val = 0;
511
512         if (sc->sc_dtr)
513                 val |= UCHCOM_DTR_MASK;
514         if (sc->sc_rts)
515                 val |= UCHCOM_RTS_MASK;
516
517         if (sc->sc_version < UCHCOM_VER_20)
518                 uchcom_set_dtr_rts_10(sc, ~val);
519         else
520                 uchcom_set_dtr_rts_20(sc, ~val);
521 }
522
523 static void
524 uchcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
525 {
526         struct uchcom_softc *sc = ucom->sc_parent;
527         uint8_t brk1;
528         uint8_t brk2;
529
530         uchcom_read_reg(sc, UCHCOM_REG_BREAK1, &brk1, UCHCOM_REG_BREAK2, &brk2);
531         if (onoff) {
532                 /* on - clear bits */
533                 brk1 &= ~UCHCOM_BRK1_MASK;
534                 brk2 &= ~UCHCOM_BRK2_MASK;
535         } else {
536                 /* off - set bits */
537                 brk1 |= UCHCOM_BRK1_MASK;
538                 brk2 |= UCHCOM_BRK2_MASK;
539         }
540         uchcom_write_reg(sc, UCHCOM_REG_BREAK1, brk1, UCHCOM_REG_BREAK2, brk2);
541 }
542
543 static int
544 uchcom_calc_divider_settings(struct uchcom_divider *dp, uint32_t rate)
545 {
546         const struct uchcom_divider_record *rp;
547         uint32_t div;
548         uint32_t rem;
549         uint32_t mod;
550         uint8_t i;
551
552         /* find record */
553         for (i = 0; i != NUM_DIVIDERS; i++) {
554                 if (dividers[i].dvr_high >= rate &&
555                     dividers[i].dvr_low <= rate) {
556                         rp = &dividers[i];
557                         goto found;
558                 }
559         }
560         return (-1);
561
562 found:
563         dp->dv_prescaler = rp->dvr_divider.dv_prescaler;
564         if (rp->dvr_base_clock == UCHCOM_BASE_UNKNOWN)
565                 dp->dv_div = rp->dvr_divider.dv_div;
566         else {
567                 div = rp->dvr_base_clock / rate;
568                 rem = rp->dvr_base_clock % rate;
569                 if (div == 0 || div >= 0xFF)
570                         return (-1);
571                 if ((rem << 1) >= rate)
572                         div += 1;
573                 dp->dv_div = (uint8_t)-div;
574         }
575
576         mod = (UCHCOM_BPS_MOD_BASE / rate) + UCHCOM_BPS_MOD_BASE_OFS;
577         mod = mod + (mod / 2);
578
579         dp->dv_mod = (mod + 0xFF) / 0x100;
580
581         return (0);
582 }
583
584 static void
585 uchcom_set_baudrate(struct uchcom_softc *sc, uint32_t rate)
586 {
587         struct uchcom_divider dv;
588
589         if (uchcom_calc_divider_settings(&dv, rate))
590                 return;
591
592         uchcom_write_reg(sc,
593             UCHCOM_REG_BPS_PRE, dv.dv_prescaler,
594             UCHCOM_REG_BPS_DIV, dv.dv_div);
595         uchcom_write_reg(sc,
596             UCHCOM_REG_BPS_MOD, dv.dv_mod,
597             UCHCOM_REG_BPS_PAD, 0);
598 }
599
600 /* ----------------------------------------------------------------------
601  * methods for ucom
602  */
603 static void
604 uchcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
605 {
606         struct uchcom_softc *sc = ucom->sc_parent;
607
608         DPRINTF("\n");
609
610         *lsr = sc->sc_lsr;
611         *msr = sc->sc_msr;
612 }
613
614 static void
615 uchcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
616 {
617         struct uchcom_softc *sc = ucom->sc_parent;
618
619         DPRINTF("onoff = %d\n", onoff);
620
621         sc->sc_dtr = onoff;
622         uchcom_set_dtr_rts(sc);
623 }
624
625 static void
626 uchcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
627 {
628         struct uchcom_softc *sc = ucom->sc_parent;
629
630         DPRINTF("onoff = %d\n", onoff);
631
632         sc->sc_rts = onoff;
633         uchcom_set_dtr_rts(sc);
634 }
635
636 static void
637 uchcom_cfg_open(struct ucom_softc *ucom)
638 {
639         struct uchcom_softc *sc = ucom->sc_parent;
640
641         DPRINTF("\n");
642
643         uchcom_update_version(sc);
644         uchcom_update_status(sc);
645 }
646
647 static int
648 uchcom_pre_param(struct ucom_softc *ucom, struct termios *t)
649 {
650         struct uchcom_divider dv;
651
652         switch (t->c_cflag & CSIZE) {
653         case CS8:
654                 break;
655         default:
656                 return (EIO);
657         }
658
659         if (uchcom_calc_divider_settings(&dv, t->c_ospeed)) {
660                 return (EIO);
661         }
662         return (0);                     /* success */
663 }
664
665 static void
666 uchcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
667 {
668         struct uchcom_softc *sc = ucom->sc_parent;
669
670         uchcom_get_version(sc, 0);
671         uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0, 0);
672         uchcom_set_baudrate(sc, t->c_ospeed);
673         uchcom_read_reg(sc, 0x18, 0, 0x25, 0);
674         uchcom_write_reg(sc, 0x18, 0x50, 0x25, 0x00);
675         uchcom_update_status(sc);
676         uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0x501f, 0xd90a);
677         uchcom_set_baudrate(sc, t->c_ospeed);
678         uchcom_set_dtr_rts(sc);
679         uchcom_update_status(sc);
680 }
681
682 static void
683 uchcom_start_read(struct ucom_softc *ucom)
684 {
685         struct uchcom_softc *sc = ucom->sc_parent;
686
687         /* start interrupt endpoint */
688         usbd_transfer_start(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
689
690         /* start read endpoint */
691         usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
692 }
693
694 static void
695 uchcom_stop_read(struct ucom_softc *ucom)
696 {
697         struct uchcom_softc *sc = ucom->sc_parent;
698
699         /* stop interrupt endpoint */
700         usbd_transfer_stop(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
701
702         /* stop read endpoint */
703         usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
704 }
705
706 static void
707 uchcom_start_write(struct ucom_softc *ucom)
708 {
709         struct uchcom_softc *sc = ucom->sc_parent;
710
711         usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
712 }
713
714 static void
715 uchcom_stop_write(struct ucom_softc *ucom)
716 {
717         struct uchcom_softc *sc = ucom->sc_parent;
718
719         usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
720 }
721
722 /* ----------------------------------------------------------------------
723  * callback when the modem status is changed.
724  */
725 static void
726 uchcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
727 {
728         struct uchcom_softc *sc = usbd_xfer_softc(xfer);
729         struct usb_page_cache *pc;
730         uint8_t buf[UCHCOM_INTR_LEAST];
731         int actlen;
732
733         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
734
735         switch (USB_GET_STATE(xfer)) {
736         case USB_ST_TRANSFERRED:
737
738                 DPRINTF("actlen = %u\n", actlen);
739
740                 if (actlen >= UCHCOM_INTR_LEAST) {
741                         pc = usbd_xfer_get_frame(xfer, 0);
742                         usbd_copy_out(pc, 0, buf, UCHCOM_INTR_LEAST);
743
744                         DPRINTF("data = 0x%02X 0x%02X 0x%02X 0x%02X\n",
745                             (unsigned)buf[0], (unsigned)buf[1],
746                             (unsigned)buf[2], (unsigned)buf[3]);
747
748                         uchcom_convert_status(sc, buf[UCHCOM_INTR_STAT1]);
749                         ucom_status_change(&sc->sc_ucom);
750                 }
751         case USB_ST_SETUP:
752 tr_setup:
753                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
754                 usbd_transfer_submit(xfer);
755                 break;
756
757         default:                        /* Error */
758                 if (error != USB_ERR_CANCELLED) {
759                         /* try to clear stall first */
760                         usbd_xfer_set_stall(xfer);
761                         goto tr_setup;
762                 }
763                 break;
764         }
765 }
766
767 static void
768 uchcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
769 {
770         struct uchcom_softc *sc = usbd_xfer_softc(xfer);
771         struct usb_page_cache *pc;
772         uint32_t actlen;
773
774         switch (USB_GET_STATE(xfer)) {
775         case USB_ST_SETUP:
776         case USB_ST_TRANSFERRED:
777 tr_setup:
778                 pc = usbd_xfer_get_frame(xfer, 0);
779                 if (ucom_get_data(&sc->sc_ucom, pc, 0,
780                     usbd_xfer_max_len(xfer), &actlen)) {
781
782                         DPRINTF("actlen = %d\n", actlen);
783
784                         usbd_xfer_set_frame_len(xfer, 0, actlen);
785                         usbd_transfer_submit(xfer);
786                 }
787                 break;
788
789         default:                        /* Error */
790                 if (error != USB_ERR_CANCELLED) {
791                         /* try to clear stall first */
792                         usbd_xfer_set_stall(xfer);
793                         goto tr_setup;
794                 }
795                 break;
796         }
797 }
798
799 static void
800 uchcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
801 {
802         struct uchcom_softc *sc = usbd_xfer_softc(xfer);
803         struct usb_page_cache *pc;
804         int actlen;
805
806         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
807
808         switch (USB_GET_STATE(xfer)) {
809         case USB_ST_TRANSFERRED:
810
811                 if (actlen > 0) {
812                         pc = usbd_xfer_get_frame(xfer, 0);
813                         ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
814                 }
815
816         case USB_ST_SETUP:
817 tr_setup:
818                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
819                 usbd_transfer_submit(xfer);
820                 break;
821
822         default:                        /* Error */
823                 if (error != USB_ERR_CANCELLED) {
824                         /* try to clear stall first */
825                         usbd_xfer_set_stall(xfer);
826                         goto tr_setup;
827                 }
828                 break;
829         }
830 }
831
832 static void
833 uchcom_poll(struct ucom_softc *ucom)
834 {
835         struct uchcom_softc *sc = ucom->sc_parent;
836         usbd_transfer_poll(sc->sc_xfer, UCHCOM_N_TRANSFER);
837 }
838
839 static device_method_t uchcom_methods[] = {
840         /* Device interface */
841         DEVMETHOD(device_probe, uchcom_probe),
842         DEVMETHOD(device_attach, uchcom_attach),
843         DEVMETHOD(device_detach, uchcom_detach),
844
845         {0, 0}
846 };
847
848 static driver_t uchcom_driver = {
849         "ucom",
850         uchcom_methods,
851         sizeof(struct uchcom_softc)
852 };
853
854 static devclass_t uchcom_devclass;
855
856 DRIVER_MODULE(uchcom, uhub, uchcom_driver, uchcom_devclass, NULL, 0);
857 MODULE_DEPEND(uchcom, ucom, 1, 1, 1);
858 MODULE_DEPEND(uchcom, usb, 1, 1, 1);
859 MODULE_VERSION(uchcom, 1);