686f8655c79e1232a09da58936bf8e65198f4485
[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 /*
66  * Driver for WinChipHead CH341/340, the worst USB-serial chip in the
67  * world.
68  */
69
70 #include <sys/stdint.h>
71 #include <sys/param.h>
72 #include <sys/queue.h>
73 #include <sys/types.h>
74 #include <sys/systm.h>
75 #include <sys/kernel.h>
76 #include <sys/bus.h>
77 #include <sys/module.h>
78 #include <sys/lock.h>
79 #include <sys/condvar.h>
80 #include <sys/sysctl.h>
81 #include <sys/unistd.h>
82 #include <sys/callout.h>
83 #include <sys/malloc.h>
84 #include <sys/priv.h>
85
86 #include <bus/u4b/usb.h>
87 #include <bus/u4b/usbdi.h>
88 #include <bus/u4b/usbdi_util.h>
89
90 #include "usbdevs.h"
91
92 #define USB_DEBUG_VAR uchcom_debug
93 #include <bus/u4b/usb_debug.h>
94 #include <bus/u4b/usb_process.h>
95
96 #include <bus/u4b/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_RW,
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 lock sc_lock;
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    (sizeof (dividers) / sizeof (dividers[0]))
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 };
206
207 /* protypes */
208
209 static int      uchcom_pre_param(struct ucom_softc *, struct termios *);
210 static void     uchcom_cfg_get_status(struct ucom_softc *, uint8_t *,
211                     uint8_t *);
212 static void     uchcom_cfg_open(struct ucom_softc *ucom);
213 static void     uchcom_cfg_param(struct ucom_softc *, struct termios *);
214 static void     uchcom_cfg_set_break(struct ucom_softc *, uint8_t);
215 static void     uchcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
216 static void     uchcom_cfg_set_rts(struct ucom_softc *, uint8_t);
217 static void     uchcom_start_read(struct ucom_softc *);
218 static void     uchcom_start_write(struct ucom_softc *);
219 static void     uchcom_stop_read(struct ucom_softc *);
220 static void     uchcom_stop_write(struct ucom_softc *);
221 static void     uchcom_update_version(struct uchcom_softc *);
222 static void     uchcom_convert_status(struct uchcom_softc *, uint8_t);
223 static void     uchcom_update_status(struct uchcom_softc *);
224 static void     uchcom_set_dtr_rts(struct uchcom_softc *);
225 static int      uchcom_calc_divider_settings(struct uchcom_divider *, uint32_t);
226 static void     uchcom_set_baudrate(struct uchcom_softc *, uint32_t);
227 static void     uchcom_poll(struct ucom_softc *ucom);
228
229 static device_probe_t uchcom_probe;
230 static device_attach_t uchcom_attach;
231 static device_detach_t uchcom_detach;
232
233 static usb_callback_t uchcom_intr_callback;
234 static usb_callback_t uchcom_write_callback;
235 static usb_callback_t uchcom_read_callback;
236
237 static const struct usb_config uchcom_config_data[UCHCOM_N_TRANSFER] = {
238
239         [UCHCOM_BULK_DT_WR] = {
240                 .type = UE_BULK,
241                 .endpoint = UE_ADDR_ANY,
242                 .direction = UE_DIR_OUT,
243                 .bufsize = UCHCOM_BULK_BUF_SIZE,
244                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
245                 .callback = &uchcom_write_callback,
246         },
247
248         [UCHCOM_BULK_DT_RD] = {
249                 .type = UE_BULK,
250                 .endpoint = UE_ADDR_ANY,
251                 .direction = UE_DIR_IN,
252                 .bufsize = UCHCOM_BULK_BUF_SIZE,
253                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
254                 .callback = &uchcom_read_callback,
255         },
256
257         [UCHCOM_INTR_DT_RD] = {
258                 .type = UE_INTERRUPT,
259                 .endpoint = UE_ADDR_ANY,
260                 .direction = UE_DIR_IN,
261                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
262                 .bufsize = 0,   /* use wMaxPacketSize */
263                 .callback = &uchcom_intr_callback,
264         },
265 };
266
267 static struct ucom_callback uchcom_callback = {
268         .ucom_cfg_get_status = &uchcom_cfg_get_status,
269         .ucom_cfg_set_dtr = &uchcom_cfg_set_dtr,
270         .ucom_cfg_set_rts = &uchcom_cfg_set_rts,
271         .ucom_cfg_set_break = &uchcom_cfg_set_break,
272         .ucom_cfg_open = &uchcom_cfg_open,
273         .ucom_cfg_param = &uchcom_cfg_param,
274         .ucom_pre_param = &uchcom_pre_param,
275         .ucom_start_read = &uchcom_start_read,
276         .ucom_stop_read = &uchcom_stop_read,
277         .ucom_start_write = &uchcom_start_write,
278         .ucom_stop_write = &uchcom_stop_write,
279         .ucom_poll = &uchcom_poll,
280 };
281
282 /* ----------------------------------------------------------------------
283  * driver entry points
284  */
285
286 static int
287 uchcom_probe(device_t dev)
288 {
289         struct usb_attach_arg *uaa = device_get_ivars(dev);
290
291         DPRINTFN(11, "\n");
292
293         if (uaa->usb_mode != USB_MODE_HOST) {
294                 return (ENXIO);
295         }
296         if (uaa->info.bConfigIndex != UCHCOM_CONFIG_INDEX) {
297                 return (ENXIO);
298         }
299         if (uaa->info.bIfaceIndex != UCHCOM_IFACE_INDEX) {
300                 return (ENXIO);
301         }
302         return (usbd_lookup_id_by_uaa(uchcom_devs, sizeof(uchcom_devs), uaa));
303 }
304
305 static int
306 uchcom_attach(device_t dev)
307 {
308         struct uchcom_softc *sc = device_get_softc(dev);
309         struct usb_attach_arg *uaa = device_get_ivars(dev);
310         int error;
311         uint8_t iface_index;
312
313         DPRINTFN(11, "\n");
314
315         device_set_usb_desc(dev);
316         lockinit(&sc->sc_lock, "uchcom", 0, LK_CANRECURSE);
317
318         sc->sc_udev = uaa->device;
319
320         switch (uaa->info.bcdDevice) {
321         case UCHCOM_REV_CH340:
322                 device_printf(dev, "CH340 detected\n");
323                 break;
324         default:
325                 device_printf(dev, "CH341 detected\n");
326                 break;
327         }
328
329         iface_index = UCHCOM_IFACE_INDEX;
330         error = usbd_transfer_setup(uaa->device,
331             &iface_index, sc->sc_xfer, uchcom_config_data,
332             UCHCOM_N_TRANSFER, sc, &sc->sc_lock);
333
334         if (error) {
335                 DPRINTF("one or more missing USB endpoints, "
336                     "error=%s\n", usbd_errstr(error));
337                 goto detach;
338         }
339
340         /* clear stall at first run */
341         lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
342         usbd_xfer_set_stall(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
343         usbd_xfer_set_stall(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
344         lockmgr(&sc->sc_lock, LK_RELEASE);
345
346         error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
347             &uchcom_callback, &sc->sc_lock);
348         if (error) {
349                 goto detach;
350         }
351         ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
352
353         return (0);
354
355 detach:
356         uchcom_detach(dev);
357         return (ENXIO);
358 }
359
360 static int
361 uchcom_detach(device_t dev)
362 {
363         struct uchcom_softc *sc = device_get_softc(dev);
364
365         DPRINTFN(11, "\n");
366
367         ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
368         usbd_transfer_unsetup(sc->sc_xfer, UCHCOM_N_TRANSFER);
369         lockuninit(&sc->sc_lock);
370
371         return (0);
372 }
373
374 /* ----------------------------------------------------------------------
375  * low level i/o
376  */
377
378 static void
379 uchcom_ctrl_write(struct uchcom_softc *sc, uint8_t reqno,
380     uint16_t value, uint16_t index)
381 {
382         struct usb_device_request req;
383
384         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
385         req.bRequest = reqno;
386         USETW(req.wValue, value);
387         USETW(req.wIndex, index);
388         USETW(req.wLength, 0);
389
390         ucom_cfg_do_request(sc->sc_udev,
391             &sc->sc_ucom, &req, NULL, 0, 1000);
392 }
393
394 static void
395 uchcom_ctrl_read(struct uchcom_softc *sc, uint8_t reqno,
396     uint16_t value, uint16_t index, void *buf, uint16_t buflen)
397 {
398         struct usb_device_request req;
399
400         req.bmRequestType = UT_READ_VENDOR_DEVICE;
401         req.bRequest = reqno;
402         USETW(req.wValue, value);
403         USETW(req.wIndex, index);
404         USETW(req.wLength, buflen);
405
406         ucom_cfg_do_request(sc->sc_udev,
407             &sc->sc_ucom, &req, buf, USB_SHORT_XFER_OK, 1000);
408 }
409
410 static void
411 uchcom_write_reg(struct uchcom_softc *sc,
412     uint8_t reg1, uint8_t val1, uint8_t reg2, uint8_t val2)
413 {
414         DPRINTF("0x%02X<-0x%02X, 0x%02X<-0x%02X\n",
415             (unsigned)reg1, (unsigned)val1,
416             (unsigned)reg2, (unsigned)val2);
417         uchcom_ctrl_write(
418             sc, UCHCOM_REQ_WRITE_REG,
419             reg1 | ((uint16_t)reg2 << 8), val1 | ((uint16_t)val2 << 8));
420 }
421
422 static void
423 uchcom_read_reg(struct uchcom_softc *sc,
424     uint8_t reg1, uint8_t *rval1, uint8_t reg2, uint8_t *rval2)
425 {
426         uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
427
428         uchcom_ctrl_read(
429             sc, UCHCOM_REQ_READ_REG,
430             reg1 | ((uint16_t)reg2 << 8), 0, buf, sizeof(buf));
431
432         DPRINTF("0x%02X->0x%02X, 0x%02X->0x%02X\n",
433             (unsigned)reg1, (unsigned)buf[0],
434             (unsigned)reg2, (unsigned)buf[1]);
435
436         if (rval1)
437                 *rval1 = buf[0];
438         if (rval2)
439                 *rval2 = buf[1];
440 }
441
442 static void
443 uchcom_get_version(struct uchcom_softc *sc, uint8_t *rver)
444 {
445         uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
446
447         uchcom_ctrl_read(sc, UCHCOM_REQ_GET_VERSION, 0, 0, buf, sizeof(buf));
448
449         if (rver)
450                 *rver = buf[0];
451 }
452
453 static void
454 uchcom_get_status(struct uchcom_softc *sc, uint8_t *rval)
455 {
456         uchcom_read_reg(sc, UCHCOM_REG_STAT1, rval, UCHCOM_REG_STAT2, NULL);
457 }
458
459 static void
460 uchcom_set_dtr_rts_10(struct uchcom_softc *sc, uint8_t val)
461 {
462         uchcom_write_reg(sc, UCHCOM_REG_STAT1, val, UCHCOM_REG_STAT1, val);
463 }
464
465 static void
466 uchcom_set_dtr_rts_20(struct uchcom_softc *sc, uint8_t val)
467 {
468         uchcom_ctrl_write(sc, UCHCOM_REQ_SET_DTRRTS, val, 0);
469 }
470
471
472 /* ----------------------------------------------------------------------
473  * middle layer
474  */
475
476 static void
477 uchcom_update_version(struct uchcom_softc *sc)
478 {
479         uchcom_get_version(sc, &sc->sc_version);
480 }
481
482 static void
483 uchcom_convert_status(struct uchcom_softc *sc, uint8_t cur)
484 {
485         sc->sc_dtr = !(cur & UCHCOM_DTR_MASK);
486         sc->sc_rts = !(cur & UCHCOM_RTS_MASK);
487
488         cur = ~cur & 0x0F;
489         sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur);
490 }
491
492 static void
493 uchcom_update_status(struct uchcom_softc *sc)
494 {
495         uint8_t cur;
496
497         uchcom_get_status(sc, &cur);
498         uchcom_convert_status(sc, cur);
499 }
500
501
502 static void
503 uchcom_set_dtr_rts(struct uchcom_softc *sc)
504 {
505         uint8_t val = 0;
506
507         if (sc->sc_dtr)
508                 val |= UCHCOM_DTR_MASK;
509         if (sc->sc_rts)
510                 val |= UCHCOM_RTS_MASK;
511
512         if (sc->sc_version < UCHCOM_VER_20)
513                 uchcom_set_dtr_rts_10(sc, ~val);
514         else
515                 uchcom_set_dtr_rts_20(sc, ~val);
516 }
517
518 static void
519 uchcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
520 {
521         struct uchcom_softc *sc = ucom->sc_parent;
522         uint8_t brk1;
523         uint8_t brk2;
524
525         uchcom_read_reg(sc, UCHCOM_REG_BREAK1, &brk1, UCHCOM_REG_BREAK2, &brk2);
526         if (onoff) {
527                 /* on - clear bits */
528                 brk1 &= ~UCHCOM_BRK1_MASK;
529                 brk2 &= ~UCHCOM_BRK2_MASK;
530         } else {
531                 /* off - set bits */
532                 brk1 |= UCHCOM_BRK1_MASK;
533                 brk2 |= UCHCOM_BRK2_MASK;
534         }
535         uchcom_write_reg(sc, UCHCOM_REG_BREAK1, brk1, UCHCOM_REG_BREAK2, brk2);
536 }
537
538 static int
539 uchcom_calc_divider_settings(struct uchcom_divider *dp, uint32_t rate)
540 {
541         const struct uchcom_divider_record *rp;
542         uint32_t div;
543         uint32_t rem;
544         uint32_t mod;
545         uint8_t i;
546
547         /* find record */
548         for (i = 0; i != NUM_DIVIDERS; i++) {
549                 if (dividers[i].dvr_high >= rate &&
550                     dividers[i].dvr_low <= rate) {
551                         rp = &dividers[i];
552                         goto found;
553                 }
554         }
555         return (-1);
556
557 found:
558         dp->dv_prescaler = rp->dvr_divider.dv_prescaler;
559         if (rp->dvr_base_clock == UCHCOM_BASE_UNKNOWN)
560                 dp->dv_div = rp->dvr_divider.dv_div;
561         else {
562                 div = rp->dvr_base_clock / rate;
563                 rem = rp->dvr_base_clock % rate;
564                 if (div == 0 || div >= 0xFF)
565                         return (-1);
566                 if ((rem << 1) >= rate)
567                         div += 1;
568                 dp->dv_div = (uint8_t)-div;
569         }
570
571         mod = (UCHCOM_BPS_MOD_BASE / rate) + UCHCOM_BPS_MOD_BASE_OFS;
572         mod = mod + (mod / 2);
573
574         dp->dv_mod = (mod + 0xFF) / 0x100;
575
576         return (0);
577 }
578
579 static void
580 uchcom_set_baudrate(struct uchcom_softc *sc, uint32_t rate)
581 {
582         struct uchcom_divider dv;
583
584         if (uchcom_calc_divider_settings(&dv, rate))
585                 return;
586
587         uchcom_write_reg(sc,
588             UCHCOM_REG_BPS_PRE, dv.dv_prescaler,
589             UCHCOM_REG_BPS_DIV, dv.dv_div);
590         uchcom_write_reg(sc,
591             UCHCOM_REG_BPS_MOD, dv.dv_mod,
592             UCHCOM_REG_BPS_PAD, 0);
593 }
594
595 /* ----------------------------------------------------------------------
596  * methods for ucom
597  */
598 static void
599 uchcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
600 {
601         struct uchcom_softc *sc = ucom->sc_parent;
602
603         DPRINTF("\n");
604
605         *lsr = sc->sc_lsr;
606         *msr = sc->sc_msr;
607 }
608
609 static void
610 uchcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
611 {
612         struct uchcom_softc *sc = ucom->sc_parent;
613
614         DPRINTF("onoff = %d\n", onoff);
615
616         sc->sc_dtr = onoff;
617         uchcom_set_dtr_rts(sc);
618 }
619
620 static void
621 uchcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
622 {
623         struct uchcom_softc *sc = ucom->sc_parent;
624
625         DPRINTF("onoff = %d\n", onoff);
626
627         sc->sc_rts = onoff;
628         uchcom_set_dtr_rts(sc);
629 }
630
631 static void
632 uchcom_cfg_open(struct ucom_softc *ucom)
633 {
634         struct uchcom_softc *sc = ucom->sc_parent;
635
636         DPRINTF("\n");
637
638         uchcom_update_version(sc);
639         uchcom_update_status(sc);
640 }
641
642 static int
643 uchcom_pre_param(struct ucom_softc *ucom, struct termios *t)
644 {
645         struct uchcom_divider dv;
646
647         switch (t->c_cflag & CSIZE) {
648         case CS8:
649                 break;
650         default:
651                 return (EIO);
652         }
653
654         if (uchcom_calc_divider_settings(&dv, t->c_ospeed)) {
655                 return (EIO);
656         }
657         return (0);                     /* success */
658 }
659
660 static void
661 uchcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
662 {
663         struct uchcom_softc *sc = ucom->sc_parent;
664
665         uchcom_get_version(sc, 0);
666         uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0, 0);
667         uchcom_set_baudrate(sc, t->c_ospeed);
668         uchcom_read_reg(sc, 0x18, 0, 0x25, 0);
669         uchcom_write_reg(sc, 0x18, 0x50, 0x25, 0x00);
670         uchcom_update_status(sc);
671         uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0x501f, 0xd90a);
672         uchcom_set_baudrate(sc, t->c_ospeed);
673         uchcom_set_dtr_rts(sc);
674         uchcom_update_status(sc);
675 }
676
677 static void
678 uchcom_start_read(struct ucom_softc *ucom)
679 {
680         struct uchcom_softc *sc = ucom->sc_parent;
681
682         /* start interrupt endpoint */
683         usbd_transfer_start(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
684
685         /* start read endpoint */
686         usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
687 }
688
689 static void
690 uchcom_stop_read(struct ucom_softc *ucom)
691 {
692         struct uchcom_softc *sc = ucom->sc_parent;
693
694         /* stop interrupt endpoint */
695         usbd_transfer_stop(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
696
697         /* stop read endpoint */
698         usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
699 }
700
701 static void
702 uchcom_start_write(struct ucom_softc *ucom)
703 {
704         struct uchcom_softc *sc = ucom->sc_parent;
705
706         usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
707 }
708
709 static void
710 uchcom_stop_write(struct ucom_softc *ucom)
711 {
712         struct uchcom_softc *sc = ucom->sc_parent;
713
714         usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
715 }
716
717 /* ----------------------------------------------------------------------
718  * callback when the modem status is changed.
719  */
720 static void
721 uchcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
722 {
723         struct uchcom_softc *sc = usbd_xfer_softc(xfer);
724         struct usb_page_cache *pc;
725         uint8_t buf[UCHCOM_INTR_LEAST];
726         int actlen;
727
728         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
729
730         switch (USB_GET_STATE(xfer)) {
731         case USB_ST_TRANSFERRED:
732
733                 DPRINTF("actlen = %u\n", actlen);
734
735                 if (actlen >= UCHCOM_INTR_LEAST) {
736                         pc = usbd_xfer_get_frame(xfer, 0);
737                         usbd_copy_out(pc, 0, buf, UCHCOM_INTR_LEAST);
738
739                         DPRINTF("data = 0x%02X 0x%02X 0x%02X 0x%02X\n",
740                             (unsigned)buf[0], (unsigned)buf[1],
741                             (unsigned)buf[2], (unsigned)buf[3]);
742
743                         uchcom_convert_status(sc, buf[UCHCOM_INTR_STAT1]);
744                         ucom_status_change(&sc->sc_ucom);
745                 }
746         case USB_ST_SETUP:
747 tr_setup:
748                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
749                 usbd_transfer_submit(xfer);
750                 break;
751
752         default:                        /* Error */
753                 if (error != USB_ERR_CANCELLED) {
754                         /* try to clear stall first */
755                         usbd_xfer_set_stall(xfer);
756                         goto tr_setup;
757                 }
758                 break;
759         }
760 }
761
762 static void
763 uchcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
764 {
765         struct uchcom_softc *sc = usbd_xfer_softc(xfer);
766         struct usb_page_cache *pc;
767         uint32_t actlen;
768
769         switch (USB_GET_STATE(xfer)) {
770         case USB_ST_SETUP:
771         case USB_ST_TRANSFERRED:
772 tr_setup:
773                 pc = usbd_xfer_get_frame(xfer, 0);
774                 if (ucom_get_data(&sc->sc_ucom, pc, 0,
775                     usbd_xfer_max_len(xfer), &actlen)) {
776
777                         DPRINTF("actlen = %d\n", actlen);
778
779                         usbd_xfer_set_frame_len(xfer, 0, actlen);
780                         usbd_transfer_submit(xfer);
781                 }
782                 break;
783
784         default:                        /* Error */
785                 if (error != USB_ERR_CANCELLED) {
786                         /* try to clear stall first */
787                         usbd_xfer_set_stall(xfer);
788                         goto tr_setup;
789                 }
790                 break;
791         }
792 }
793
794 static void
795 uchcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
796 {
797         struct uchcom_softc *sc = usbd_xfer_softc(xfer);
798         struct usb_page_cache *pc;
799         int actlen;
800
801         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
802
803         switch (USB_GET_STATE(xfer)) {
804         case USB_ST_TRANSFERRED:
805
806                 if (actlen > 0) {
807                         pc = usbd_xfer_get_frame(xfer, 0);
808                         ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
809                 }
810
811         case USB_ST_SETUP:
812 tr_setup:
813                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
814                 usbd_transfer_submit(xfer);
815                 break;
816
817         default:                        /* Error */
818                 if (error != USB_ERR_CANCELLED) {
819                         /* try to clear stall first */
820                         usbd_xfer_set_stall(xfer);
821                         goto tr_setup;
822                 }
823                 break;
824         }
825 }
826
827 static void
828 uchcom_poll(struct ucom_softc *ucom)
829 {
830         struct uchcom_softc *sc = ucom->sc_parent;
831         usbd_transfer_poll(sc->sc_xfer, UCHCOM_N_TRANSFER);
832 }
833
834 static device_method_t uchcom_methods[] = {
835         /* Device interface */
836         DEVMETHOD(device_probe, uchcom_probe),
837         DEVMETHOD(device_attach, uchcom_attach),
838         DEVMETHOD(device_detach, uchcom_detach),
839
840         DEVMETHOD_END
841 };
842
843 static driver_t uchcom_driver = {
844         "ucom",
845         uchcom_methods,
846         sizeof(struct uchcom_softc)
847 };
848
849 static devclass_t uchcom_devclass;
850
851 DRIVER_MODULE(uchcom, uhub, uchcom_driver, uchcom_devclass, NULL, 0);
852 MODULE_DEPEND(uchcom, ucom, 1, 1, 1);
853 MODULE_DEPEND(uchcom, usb, 1, 1, 1);
854 MODULE_VERSION(uchcom, 1);