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