uchcom.c revision 1.16 1 /* $NetBSD: uchcom.c,v 1.16 2016/11/25 12:56:29 skrll Exp $ */
2
3 /*
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Takuya SHIOZAKI (tshiozak (at) netbsd.org).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: uchcom.c,v 1.16 2016/11/25 12:56:29 skrll Exp $");
34
35 #ifdef _KERNEL_OPT
36 #include "opt_usb.h"
37 #endif
38
39 /*
40 * driver for WinChipHead CH341/340, the worst USB-serial chip in the world.
41 */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/kmem.h>
47 #include <sys/ioctl.h>
48 #include <sys/conf.h>
49 #include <sys/tty.h>
50 #include <sys/file.h>
51 #include <sys/select.h>
52 #include <sys/proc.h>
53 #include <sys/device.h>
54 #include <sys/poll.h>
55
56 #include <dev/usb/usb.h>
57 #include <dev/usb/usbcdc.h>
58
59 #include <dev/usb/usbdi.h>
60 #include <dev/usb/usbdi_util.h>
61 #include <dev/usb/usbdevs.h>
62 #include <dev/usb/usb_quirks.h>
63
64 #include <dev/usb/ucomvar.h>
65
66 #ifdef UCHCOM_DEBUG
67 #define DPRINTFN(n, x) if (uchcomdebug > (n)) printf x
68 int uchcomdebug = 0;
69 #else
70 #define DPRINTFN(n, x)
71 #endif
72 #define DPRINTF(x) DPRINTFN(0, x)
73
74 #define UCHCOM_IFACE_INDEX 0
75 #define UCHCOM_CONFIG_INDEX 0
76
77 #define UCHCOM_REV_CH340 0x0250
78 #define UCHCOM_INPUT_BUF_SIZE 8
79
80 #define UCHCOM_REQ_GET_VERSION 0x5F
81 #define UCHCOM_REQ_READ_REG 0x95
82 #define UCHCOM_REQ_WRITE_REG 0x9A
83 #define UCHCOM_REQ_RESET 0xA1
84 #define UCHCOM_REQ_SET_DTRRTS 0xA4
85
86 #define UCHCOM_REG_STAT1 0x06
87 #define UCHCOM_REG_STAT2 0x07
88 #define UCHCOM_REG_BPS_PRE 0x12
89 #define UCHCOM_REG_BPS_DIV 0x13
90 #define UCHCOM_REG_BPS_MOD 0x14
91 #define UCHCOM_REG_BPS_PAD 0x0F
92 #define UCHCOM_REG_BREAK1 0x05
93 #define UCHCOM_REG_BREAK2 0x18
94 #define UCHCOM_REG_LCR1 0x18
95 #define UCHCOM_REG_LCR2 0x25
96
97 #define UCHCOM_VER_20 0x20
98
99 #define UCHCOM_BASE_UNKNOWN 0
100 #define UCHCOM_BPS_MOD_BASE 20000000
101 #define UCHCOM_BPS_MOD_BASE_OFS 1100
102
103 #define UCHCOM_DTR_MASK 0x20
104 #define UCHCOM_RTS_MASK 0x40
105
106 #define UCHCOM_BRK1_MASK 0x01
107 #define UCHCOM_BRK2_MASK 0x40
108
109 #define UCHCOM_LCR1_MASK 0xAF
110 #define UCHCOM_LCR2_MASK 0x07
111 #define UCHCOM_LCR1_PARENB 0x80
112 #define UCHCOM_LCR2_PAREVEN 0x07
113 #define UCHCOM_LCR2_PARODD 0x06
114 #define UCHCOM_LCR2_PARMARK 0x05
115 #define UCHCOM_LCR2_PARSPACE 0x04
116
117 #define UCHCOM_INTR_STAT1 0x02
118 #define UCHCOM_INTR_STAT2 0x03
119 #define UCHCOM_INTR_LEAST 4
120
121 #define UCHCOMIBUFSIZE 256
122 #define UCHCOMOBUFSIZE 256
123
124 struct uchcom_softc
125 {
126 device_t sc_dev;
127 struct usbd_device * sc_udev;
128 device_t sc_subdev;
129 struct usbd_interface * sc_iface;
130 int sc_dying;
131 /* */
132 int sc_intr_endpoint;
133 int sc_intr_size;
134 struct usbd_pipe * sc_intr_pipe;
135 u_char *sc_intr_buf;
136 /* */
137 uint8_t sc_version;
138 int sc_dtr;
139 int sc_rts;
140 u_char sc_lsr;
141 u_char sc_msr;
142 int sc_lcr1;
143 int sc_lcr2;
144 };
145
146 struct uchcom_endpoints
147 {
148 int ep_bulkin;
149 int ep_bulkout;
150 int ep_intr;
151 int ep_intr_size;
152 };
153
154 struct uchcom_divider
155 {
156 uint8_t dv_prescaler;
157 uint8_t dv_div;
158 uint8_t dv_mod;
159 };
160
161 struct uchcom_divider_record
162 {
163 uint32_t dvr_high;
164 uint32_t dvr_low;
165 uint32_t dvr_base_clock;
166 struct uchcom_divider dvr_divider;
167 };
168
169 static const struct uchcom_divider_record dividers[] =
170 {
171 { 307200, 307200, UCHCOM_BASE_UNKNOWN, { 7, 0xD9, 0 } },
172 { 921600, 921600, UCHCOM_BASE_UNKNOWN, { 7, 0xF3, 0 } },
173 { 2999999, 23530, 6000000, { 3, 0, 0 } },
174 { 23529, 2942, 750000, { 2, 0, 0 } },
175 { 2941, 368, 93750, { 1, 0, 0 } },
176 { 367, 1, 11719, { 0, 0, 0 } },
177 };
178 #define NUM_DIVIDERS (sizeof (dividers) / sizeof (dividers[0]))
179
180 static const struct usb_devno uchcom_devs[] = {
181 { USB_VENDOR_WINCHIPHEAD, USB_PRODUCT_WINCHIPHEAD_CH341SER },
182 { USB_VENDOR_WINCHIPHEAD2, USB_PRODUCT_WINCHIPHEAD2_CH341 },
183 };
184 #define uchcom_lookup(v, p) usb_lookup(uchcom_devs, v, p)
185
186 Static void uchcom_get_status(void *, int, u_char *, u_char *);
187 Static void uchcom_set(void *, int, int, int);
188 Static int uchcom_param(void *, int, struct termios *);
189 Static int uchcom_open(void *, int);
190 Static void uchcom_close(void *, int);
191 Static void uchcom_intr(struct usbd_xfer *, void *,
192 usbd_status);
193
194 static int set_config(struct uchcom_softc *);
195 static int find_ifaces(struct uchcom_softc *, struct usbd_interface **);
196 static int find_endpoints(struct uchcom_softc *,
197 struct uchcom_endpoints *);
198 static void close_intr_pipe(struct uchcom_softc *);
199
200
201 struct ucom_methods uchcom_methods = {
202 .ucom_get_status = uchcom_get_status,
203 .ucom_set = uchcom_set,
204 .ucom_param = uchcom_param,
205 .ucom_ioctl = NULL,
206 .ucom_open = uchcom_open,
207 .ucom_close = uchcom_close,
208 .ucom_read = NULL,
209 .ucom_write = NULL,
210 };
211
212 int uchcom_match(device_t, cfdata_t, void *);
213 void uchcom_attach(device_t, device_t, void *);
214 void uchcom_childdet(device_t, device_t);
215 int uchcom_detach(device_t, int);
216 int uchcom_activate(device_t, enum devact);
217
218 extern struct cfdriver uchcom_cd;
219
220 CFATTACH_DECL2_NEW(uchcom,
221 sizeof(struct uchcom_softc),
222 uchcom_match,
223 uchcom_attach,
224 uchcom_detach,
225 uchcom_activate,
226 NULL,
227 uchcom_childdet);
228
229 /* ----------------------------------------------------------------------
230 * driver entry points
231 */
232
233 int
234 uchcom_match(device_t parent, cfdata_t match, void *aux)
235 {
236 struct usb_attach_arg *uaa = aux;
237
238 return (uchcom_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
239 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
240 }
241
242 void
243 uchcom_attach(device_t parent, device_t self, void *aux)
244 {
245 struct uchcom_softc *sc = device_private(self);
246 struct usb_attach_arg *uaa = aux;
247 struct usbd_device *dev = uaa->uaa_device;
248 char *devinfop;
249 struct uchcom_endpoints endpoints;
250 struct ucom_attach_args ucaa;
251
252 aprint_naive("\n");
253 aprint_normal("\n");
254
255 devinfop = usbd_devinfo_alloc(dev, 0);
256 aprint_normal_dev(self, "%s\n", devinfop);
257 usbd_devinfo_free(devinfop);
258
259 sc->sc_dev = self;
260 sc->sc_udev = dev;
261 sc->sc_dying = 0;
262 sc->sc_dtr = sc->sc_rts = -1;
263 sc->sc_lsr = sc->sc_msr = 0;
264
265 DPRINTF(("\n\nuchcom attach: sc=%p\n", sc));
266
267 if (set_config(sc))
268 goto failed;
269
270 switch (uaa->uaa_release) {
271 case UCHCOM_REV_CH340:
272 aprint_normal_dev(self, "CH340 detected\n");
273 break;
274 default:
275 aprint_normal_dev(self, "CH341 detected\n");
276 break;
277 }
278
279 if (find_ifaces(sc, &sc->sc_iface))
280 goto failed;
281
282 if (find_endpoints(sc, &endpoints))
283 goto failed;
284
285 sc->sc_intr_endpoint = endpoints.ep_intr;
286 sc->sc_intr_size = endpoints.ep_intr_size;
287
288 /* setup ucom layer */
289 ucaa.ucaa_portno = UCOM_UNK_PORTNO;
290 ucaa.ucaa_bulkin = endpoints.ep_bulkin;
291 ucaa.ucaa_bulkout = endpoints.ep_bulkout;
292 ucaa.ucaa_ibufsize = UCHCOMIBUFSIZE;
293 ucaa.ucaa_obufsize = UCHCOMOBUFSIZE;
294 ucaa.ucaa_ibufsizepad = UCHCOMIBUFSIZE;
295 ucaa.ucaa_opkthdrlen = 0;
296 ucaa.ucaa_device = dev;
297 ucaa.ucaa_iface = sc->sc_iface;
298 ucaa.ucaa_methods = &uchcom_methods;
299 ucaa.ucaa_arg = sc;
300 ucaa.ucaa_info = NULL;
301
302 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
303
304 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa,
305 ucomprint, ucomsubmatch);
306
307 return;
308
309 failed:
310 sc->sc_dying = 1;
311 return;
312 }
313
314 void
315 uchcom_childdet(device_t self, device_t child)
316 {
317 struct uchcom_softc *sc = device_private(self);
318
319 KASSERT(sc->sc_subdev == child);
320 sc->sc_subdev = NULL;
321 }
322
323 int
324 uchcom_detach(device_t self, int flags)
325 {
326 struct uchcom_softc *sc = device_private(self);
327 int rv = 0;
328
329 DPRINTF(("uchcom_detach: sc=%p flags=%d\n", sc, flags));
330
331 close_intr_pipe(sc);
332
333 sc->sc_dying = 1;
334
335 if (sc->sc_subdev != NULL)
336 rv = config_detach(sc->sc_subdev, flags);
337
338 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
339
340 return rv;
341 }
342
343 int
344 uchcom_activate(device_t self, enum devact act)
345 {
346 struct uchcom_softc *sc = device_private(self);
347
348 switch (act) {
349 case DVACT_DEACTIVATE:
350 close_intr_pipe(sc);
351 sc->sc_dying = 1;
352 return 0;
353 default:
354 return EOPNOTSUPP;
355 }
356 }
357
358 static int
359 set_config(struct uchcom_softc *sc)
360 {
361 usbd_status err;
362
363 err = usbd_set_config_index(sc->sc_udev, UCHCOM_CONFIG_INDEX, 1);
364 if (err) {
365 aprint_error_dev(sc->sc_dev,
366 "failed to set configuration: %s\n", usbd_errstr(err));
367 return -1;
368 }
369
370 return 0;
371 }
372
373 static int
374 find_ifaces(struct uchcom_softc *sc, struct usbd_interface **riface)
375 {
376 usbd_status err;
377
378 err = usbd_device2interface_handle(sc->sc_udev, UCHCOM_IFACE_INDEX,
379 riface);
380 if (err) {
381 aprint_error("\n%s: failed to get interface: %s\n",
382 device_xname(sc->sc_dev), usbd_errstr(err));
383 return -1;
384 }
385
386 return 0;
387 }
388
389 static int
390 find_endpoints(struct uchcom_softc *sc, struct uchcom_endpoints *endpoints)
391 {
392 int i, bin=-1, bout=-1, intr=-1, isize=0;
393 usb_interface_descriptor_t *id;
394 usb_endpoint_descriptor_t *ed;
395
396 id = usbd_get_interface_descriptor(sc->sc_iface);
397
398 for (i = 0; i < id->bNumEndpoints; i++) {
399 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
400 if (ed == NULL) {
401 aprint_error_dev(sc->sc_dev,
402 "no endpoint descriptor for %d\n", i);
403 return -1;
404 }
405
406 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
407 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
408 intr = ed->bEndpointAddress;
409 isize = UGETW(ed->wMaxPacketSize);
410 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
411 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
412 bin = ed->bEndpointAddress;
413 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
414 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
415 bout = ed->bEndpointAddress;
416 }
417 }
418
419 if (intr == -1 || bin == -1 || bout == -1) {
420 if (intr == -1) {
421 aprint_error_dev(sc->sc_dev,
422 "no interrupt end point\n");
423 }
424 if (bin == -1) {
425 aprint_error_dev(sc->sc_dev,
426 "no data bulk in end point\n");
427 }
428 if (bout == -1) {
429 aprint_error_dev(sc->sc_dev,
430 "no data bulk out end point\n");
431 }
432 return -1;
433 }
434 if (isize < UCHCOM_INTR_LEAST) {
435 aprint_error_dev(sc->sc_dev, "intr pipe is too short\n");
436 return -1;
437 }
438
439 DPRINTF(("%s: bulkin=%d, bulkout=%d, intr=%d, isize=%d\n",
440 device_xname(sc->sc_dev), bin, bout, intr, isize));
441
442 endpoints->ep_intr = intr;
443 endpoints->ep_intr_size = isize;
444 endpoints->ep_bulkin = bin;
445 endpoints->ep_bulkout = bout;
446
447 return 0;
448 }
449
450
451 /* ----------------------------------------------------------------------
452 * low level i/o
453 */
454
455 static __inline usbd_status
456 generic_control_out(struct uchcom_softc *sc, uint8_t reqno,
457 uint16_t value, uint16_t index)
458 {
459 usb_device_request_t req;
460
461 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
462 req.bRequest = reqno;
463 USETW(req.wValue, value);
464 USETW(req.wIndex, index);
465 USETW(req.wLength, 0);
466
467 return usbd_do_request(sc->sc_udev, &req, 0);
468 }
469
470 static __inline usbd_status
471 generic_control_in(struct uchcom_softc *sc, uint8_t reqno,
472 uint16_t value, uint16_t index, void *buf, int buflen,
473 int *actlen)
474 {
475 usb_device_request_t req;
476
477 req.bmRequestType = UT_READ_VENDOR_DEVICE;
478 req.bRequest = reqno;
479 USETW(req.wValue, value);
480 USETW(req.wIndex, index);
481 USETW(req.wLength, (uint16_t)buflen);
482
483 return usbd_do_request_flags(sc->sc_udev, &req, buf,
484 USBD_SHORT_XFER_OK, actlen,
485 USBD_DEFAULT_TIMEOUT);
486 }
487
488 static __inline usbd_status
489 write_reg(struct uchcom_softc *sc,
490 uint8_t reg1, uint8_t val1, uint8_t reg2, uint8_t val2)
491 {
492 DPRINTF(("uchcom: write reg 0x%02X<-0x%02X, 0x%02X<-0x%02X\n",
493 (unsigned)reg1, (unsigned)val1,
494 (unsigned)reg2, (unsigned)val2));
495 return generic_control_out(
496 sc, UCHCOM_REQ_WRITE_REG,
497 reg1|((uint16_t)reg2<<8), val1|((uint16_t)val2<<8));
498 }
499
500 static __inline usbd_status
501 read_reg(struct uchcom_softc *sc,
502 uint8_t reg1, uint8_t *rval1, uint8_t reg2, uint8_t *rval2)
503 {
504 uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
505 usbd_status err;
506 int actin;
507
508 err = generic_control_in(
509 sc, UCHCOM_REQ_READ_REG,
510 reg1|((uint16_t)reg2<<8), 0, buf, sizeof(buf), &actin);
511 if (err)
512 return err;
513
514 DPRINTF(("uchcom: read reg 0x%02X->0x%02X, 0x%02X->0x%02X\n",
515 (unsigned)reg1, (unsigned)buf[0],
516 (unsigned)reg2, (unsigned)buf[1]));
517
518 if (rval1) *rval1 = buf[0];
519 if (rval2) *rval2 = buf[1];
520
521 return USBD_NORMAL_COMPLETION;
522 }
523
524 static __inline usbd_status
525 get_version(struct uchcom_softc *sc, uint8_t *rver)
526 {
527 uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
528 usbd_status err;
529 int actin;
530
531 err = generic_control_in(
532 sc, UCHCOM_REQ_GET_VERSION, 0, 0, buf, sizeof(buf), &actin);
533 if (err)
534 return err;
535
536 if (rver) *rver = buf[0];
537
538 return USBD_NORMAL_COMPLETION;
539 }
540
541 static __inline usbd_status
542 get_status(struct uchcom_softc *sc, uint8_t *rval)
543 {
544 return read_reg(sc, UCHCOM_REG_STAT1, rval, UCHCOM_REG_STAT2, NULL);
545 }
546
547 static __inline usbd_status
548 set_dtrrts_10(struct uchcom_softc *sc, uint8_t val)
549 {
550 return write_reg(sc, UCHCOM_REG_STAT1, val, UCHCOM_REG_STAT1, val);
551 }
552
553 static __inline usbd_status
554 set_dtrrts_20(struct uchcom_softc *sc, uint8_t val)
555 {
556 return generic_control_out(sc, UCHCOM_REQ_SET_DTRRTS, val, 0);
557 }
558
559
560 /* ----------------------------------------------------------------------
561 * middle layer
562 */
563
564 static int
565 update_version(struct uchcom_softc *sc)
566 {
567 usbd_status err;
568
569 err = get_version(sc, &sc->sc_version);
570 if (err) {
571 aprint_error_dev(sc->sc_dev, "cannot get version: %s\n",
572 usbd_errstr(err));
573 return EIO;
574 }
575
576 return 0;
577 }
578
579 static void
580 convert_status(struct uchcom_softc *sc, uint8_t cur)
581 {
582 sc->sc_dtr = !(cur & UCHCOM_DTR_MASK);
583 sc->sc_rts = !(cur & UCHCOM_RTS_MASK);
584
585 cur = ~cur & 0x0F;
586 sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur);
587 }
588
589 static int
590 update_status(struct uchcom_softc *sc)
591 {
592 usbd_status err;
593 uint8_t cur;
594
595 err = get_status(sc, &cur);
596 if (err) {
597 aprint_error_dev(sc->sc_dev,
598 "cannot update status: %s\n", usbd_errstr(err));
599 return EIO;
600 }
601 convert_status(sc, cur);
602
603 return 0;
604 }
605
606
607 static int
608 set_dtrrts(struct uchcom_softc *sc, int dtr, int rts)
609 {
610 usbd_status err;
611 uint8_t val = 0;
612
613 if (dtr) val |= UCHCOM_DTR_MASK;
614 if (rts) val |= UCHCOM_RTS_MASK;
615
616 if (sc->sc_version < UCHCOM_VER_20)
617 err = set_dtrrts_10(sc, ~val);
618 else
619 err = set_dtrrts_20(sc, ~val);
620
621 if (err) {
622 aprint_error_dev(sc->sc_dev, "cannot set DTR/RTS: %s\n",
623 usbd_errstr(err));
624 return EIO;
625 }
626
627 return 0;
628 }
629
630 static int
631 set_break(struct uchcom_softc *sc, int onoff)
632 {
633 usbd_status err;
634 uint8_t brk1, brk2;
635
636 err = read_reg(sc, UCHCOM_REG_BREAK1, &brk1, UCHCOM_REG_BREAK2, &brk2);
637 if (err)
638 return EIO;
639 if (onoff) {
640 /* on - clear bits */
641 brk1 &= ~UCHCOM_BRK1_MASK;
642 brk2 &= ~UCHCOM_BRK2_MASK;
643 } else {
644 /* off - set bits */
645 brk1 |= UCHCOM_BRK1_MASK;
646 brk2 |= UCHCOM_BRK2_MASK;
647 }
648 err = write_reg(sc, UCHCOM_REG_BREAK1, brk1, UCHCOM_REG_BREAK2, brk2);
649 if (err)
650 return EIO;
651
652 return 0;
653 }
654
655 static int
656 calc_divider_settings(struct uchcom_divider *dp, uint32_t rate)
657 {
658 int i;
659 const struct uchcom_divider_record *rp;
660 uint32_t div, rem, mod;
661
662 /* find record */
663 for (i=0; i<NUM_DIVIDERS; i++) {
664 if (dividers[i].dvr_high >= rate &&
665 dividers[i].dvr_low <= rate) {
666 rp = ÷rs[i];
667 goto found;
668 }
669 }
670 return -1;
671
672 found:
673 dp->dv_prescaler = rp->dvr_divider.dv_prescaler;
674 if (rp->dvr_base_clock == UCHCOM_BASE_UNKNOWN)
675 dp->dv_div = rp->dvr_divider.dv_div;
676 else {
677 div = rp->dvr_base_clock / rate;
678 rem = rp->dvr_base_clock % rate;
679 if (div==0 || div>=0xFF)
680 return -1;
681 if ((rem<<1) >= rate)
682 div += 1;
683 dp->dv_div = (uint8_t)-div;
684 }
685
686 mod = UCHCOM_BPS_MOD_BASE/rate + UCHCOM_BPS_MOD_BASE_OFS;
687 mod = mod + mod/2;
688
689 dp->dv_mod = mod / 0x100;
690
691 return 0;
692 }
693
694 static int
695 set_dte_rate(struct uchcom_softc *sc, uint32_t rate)
696 {
697 usbd_status err;
698 struct uchcom_divider dv;
699
700 if (calc_divider_settings(&dv, rate))
701 return EINVAL;
702
703 if ((err = write_reg(sc,
704 UCHCOM_REG_BPS_PRE, dv.dv_prescaler,
705 UCHCOM_REG_BPS_DIV, dv.dv_div)) ||
706 (err = write_reg(sc,
707 UCHCOM_REG_BPS_MOD, dv.dv_mod,
708 UCHCOM_REG_BPS_PAD, 0))) {
709 aprint_error_dev(sc->sc_dev, "cannot set DTE rate: %s\n",
710 usbd_errstr(err));
711 return EIO;
712 }
713
714 return 0;
715 }
716
717 static int
718 set_line_control(struct uchcom_softc *sc, tcflag_t cflag)
719 {
720 usbd_status err;
721 uint8_t lcr1val = 0, lcr2val = 0;
722
723 err = read_reg(sc, UCHCOM_REG_LCR1, &lcr1val, UCHCOM_REG_LCR2, &lcr2val);
724 if (err) {
725 aprint_error_dev(sc->sc_dev, "cannot get LCR: %s\n",
726 usbd_errstr(err));
727 return EIO;
728 }
729
730 lcr1val &= ~UCHCOM_LCR1_MASK;
731 lcr2val &= ~UCHCOM_LCR2_MASK;
732
733 /*
734 * XXX: it is difficult to handle the line control appropriately:
735 * - CS8, !CSTOPB and any parity mode seems ok, but
736 * - the chip doesn't have the function to calculate parity
737 * in !CS8 mode.
738 * - it is unclear that the chip supports CS5,6 mode.
739 * - it is unclear how to handle stop bits.
740 */
741
742 switch (ISSET(cflag, CSIZE)) {
743 case CS5:
744 case CS6:
745 case CS7:
746 return EINVAL;
747 case CS8:
748 break;
749 }
750
751 if (ISSET(cflag, PARENB)) {
752 lcr1val |= UCHCOM_LCR1_PARENB;
753 if (ISSET(cflag, PARODD))
754 lcr2val |= UCHCOM_LCR2_PARODD;
755 else
756 lcr2val |= UCHCOM_LCR2_PAREVEN;
757 }
758
759 err = write_reg(sc, UCHCOM_REG_LCR1, lcr1val, UCHCOM_REG_LCR2, lcr2val);
760 if (err) {
761 aprint_error_dev(sc->sc_dev, "cannot set LCR: %s\n",
762 usbd_errstr(err));
763 return EIO;
764 }
765
766 return 0;
767 }
768
769 static int
770 clear_chip(struct uchcom_softc *sc)
771 {
772 usbd_status err;
773
774 DPRINTF(("%s: clear\n", device_xname(sc->sc_dev)));
775 err = generic_control_out(sc, UCHCOM_REQ_RESET, 0, 0);
776 if (err) {
777 aprint_error_dev(sc->sc_dev, "cannot clear: %s\n",
778 usbd_errstr(err));
779 return EIO;
780 }
781
782 return 0;
783 }
784
785 static int
786 reset_chip(struct uchcom_softc *sc)
787 {
788 usbd_status err;
789 uint8_t lcr1val, lcr2val, pre, div, mod;
790 uint16_t val=0, idx=0;
791
792 err = read_reg(sc, UCHCOM_REG_LCR1, &lcr1val, UCHCOM_REG_LCR2, &lcr2val);
793 if (err)
794 goto failed;
795
796 err = read_reg(sc, UCHCOM_REG_BPS_PRE, &pre, UCHCOM_REG_BPS_DIV, &div);
797 if (err)
798 goto failed;
799
800 err = read_reg(sc, UCHCOM_REG_BPS_MOD, &mod, UCHCOM_REG_BPS_PAD, NULL);
801 if (err)
802 goto failed;
803
804 val |= (uint16_t)(lcr1val&0xF0) << 8;
805 val |= 0x01;
806 val |= (uint16_t)(lcr2val&0x0F) << 8;
807 val |= 0x02;
808 idx |= pre & 0x07;
809 val |= 0x04;
810 idx |= (uint16_t)div << 8;
811 val |= 0x08;
812 idx |= mod & 0xF8;
813 val |= 0x10;
814
815 DPRINTF(("%s: reset v=0x%04X, i=0x%04X\n",
816 device_xname(sc->sc_dev), val, idx));
817
818 err = generic_control_out(sc, UCHCOM_REQ_RESET, val, idx);
819 if (err)
820 goto failed;
821
822 return 0;
823
824 failed:
825 printf("%s: cannot reset: %s\n",
826 device_xname(sc->sc_dev), usbd_errstr(err));
827 return EIO;
828 }
829
830 static int
831 setup_comm(struct uchcom_softc *sc)
832 {
833 int ret;
834
835 ret = update_version(sc);
836 if (ret)
837 return ret;
838
839 ret = clear_chip(sc);
840 if (ret)
841 return ret;
842
843 ret = set_dte_rate(sc, TTYDEF_SPEED);
844 if (ret)
845 return ret;
846
847 ret = set_line_control(sc, CS8);
848 if (ret)
849 return ret;
850
851 ret = update_status(sc);
852 if (ret)
853 return ret;
854
855 ret = reset_chip(sc);
856 if (ret)
857 return ret;
858
859 ret = set_dte_rate(sc, TTYDEF_SPEED); /* XXX */
860 if (ret)
861 return ret;
862
863 sc->sc_dtr = sc->sc_rts = 1;
864 ret = set_dtrrts(sc, sc->sc_dtr, sc->sc_rts);
865 if (ret)
866 return ret;
867
868 return 0;
869 }
870
871 static int
872 setup_intr_pipe(struct uchcom_softc *sc)
873 {
874 usbd_status err;
875
876 if (sc->sc_intr_endpoint != -1 && sc->sc_intr_pipe == NULL) {
877 sc->sc_intr_buf = kmem_alloc(sc->sc_intr_size, KM_SLEEP);
878 err = usbd_open_pipe_intr(sc->sc_iface,
879 sc->sc_intr_endpoint,
880 USBD_SHORT_XFER_OK,
881 &sc->sc_intr_pipe, sc,
882 sc->sc_intr_buf,
883 sc->sc_intr_size,
884 uchcom_intr, USBD_DEFAULT_INTERVAL);
885 if (err) {
886 aprint_error_dev(sc->sc_dev,
887 "cannot open interrupt pipe: %s\n",
888 usbd_errstr(err));
889 return EIO;
890 }
891 }
892 return 0;
893 }
894
895 static void
896 close_intr_pipe(struct uchcom_softc *sc)
897 {
898 usbd_status err;
899
900 if (sc->sc_dying)
901 return;
902
903 if (sc->sc_intr_pipe != NULL) {
904 err = usbd_abort_pipe(sc->sc_intr_pipe);
905 if (err)
906 aprint_error_dev(sc->sc_dev,
907 "abort interrupt pipe failed: %s\n",
908 usbd_errstr(err));
909 err = usbd_close_pipe(sc->sc_intr_pipe);
910 if (err)
911 aprint_error_dev(sc->sc_dev,
912 "close interrupt pipe failed: %s\n",
913 usbd_errstr(err));
914 kmem_free(sc->sc_intr_buf, sc->sc_intr_size);
915 sc->sc_intr_pipe = NULL;
916 }
917 }
918
919
920 /* ----------------------------------------------------------------------
921 * methods for ucom
922 */
923 void
924 uchcom_get_status(void *arg, int portno, u_char *rlsr, u_char *rmsr)
925 {
926 struct uchcom_softc *sc = arg;
927
928 if (sc->sc_dying)
929 return;
930
931 *rlsr = sc->sc_lsr;
932 *rmsr = sc->sc_msr;
933 }
934
935 void
936 uchcom_set(void *arg, int portno, int reg, int onoff)
937 {
938 struct uchcom_softc *sc = arg;
939
940 if (sc->sc_dying)
941 return;
942
943 switch (reg) {
944 case UCOM_SET_DTR:
945 sc->sc_dtr = !!onoff;
946 set_dtrrts(sc, sc->sc_dtr, sc->sc_rts);
947 break;
948 case UCOM_SET_RTS:
949 sc->sc_rts = !!onoff;
950 set_dtrrts(sc, sc->sc_dtr, sc->sc_rts);
951 break;
952 case UCOM_SET_BREAK:
953 set_break(sc, onoff);
954 break;
955 }
956 }
957
958 int
959 uchcom_param(void *arg, int portno, struct termios *t)
960 {
961 struct uchcom_softc *sc = arg;
962 int ret;
963
964 if (sc->sc_dying)
965 return 0;
966
967 ret = set_line_control(sc, t->c_cflag);
968 if (ret)
969 return ret;
970
971 ret = set_dte_rate(sc, t->c_ospeed);
972 if (ret)
973 return ret;
974
975 return 0;
976 }
977
978 int
979 uchcom_open(void *arg, int portno)
980 {
981 int ret;
982 struct uchcom_softc *sc = arg;
983
984 if (sc->sc_dying)
985 return EIO;
986
987 ret = setup_intr_pipe(sc);
988 if (ret)
989 return ret;
990
991 ret = setup_comm(sc);
992 if (ret)
993 return ret;
994
995 return 0;
996 }
997
998 void
999 uchcom_close(void *arg, int portno)
1000 {
1001 struct uchcom_softc *sc = arg;
1002
1003 if (sc->sc_dying)
1004 return;
1005
1006 close_intr_pipe(sc);
1007 }
1008
1009
1010 /* ----------------------------------------------------------------------
1011 * callback when the modem status is changed.
1012 */
1013 void
1014 uchcom_intr(struct usbd_xfer *xfer, void * priv,
1015 usbd_status status)
1016 {
1017 struct uchcom_softc *sc = priv;
1018 u_char *buf = sc->sc_intr_buf;
1019
1020 if (sc->sc_dying)
1021 return;
1022
1023 if (status != USBD_NORMAL_COMPLETION) {
1024 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1025 return;
1026
1027 DPRINTF(("%s: abnormal status: %s\n",
1028 device_xname(sc->sc_dev), usbd_errstr(status)));
1029 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
1030 return;
1031 }
1032 DPRINTF(("%s: intr: 0x%02X 0x%02X 0x%02X 0x%02X "
1033 "0x%02X 0x%02X 0x%02X 0x%02X\n",
1034 device_xname(sc->sc_dev),
1035 (unsigned)buf[0], (unsigned)buf[1],
1036 (unsigned)buf[2], (unsigned)buf[3],
1037 (unsigned)buf[4], (unsigned)buf[5],
1038 (unsigned)buf[6], (unsigned)buf[7]));
1039
1040 convert_status(sc, buf[UCHCOM_INTR_STAT1]);
1041 ucom_status_change(device_private(sc->sc_subdev));
1042 }
1043