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