uchcom.c revision 1.14 1 /* $NetBSD: uchcom.c,v 1.14 2016/04/23 10:15:32 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.14 2016/04/23 10:15:32 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
95 #define UCHCOM_BASE_UNKNOWN 0
96 #define UCHCOM_BPS_MOD_BASE 20000000
97 #define UCHCOM_BPS_MOD_BASE_OFS 1100
98
99 #define UCHCOM_DTR_MASK 0x20
100 #define UCHCOM_RTS_MASK 0x40
101
102 #define UCHCOM_BRK1_MASK 0x01
103 #define UCHCOM_BRK2_MASK 0x40
104
105 #define UCHCOM_LCR1_MASK 0xAF
106 #define UCHCOM_LCR2_MASK 0x07
107 #define UCHCOM_LCR1_PARENB 0x80
108 #define UCHCOM_LCR2_PAREVEN 0x07
109 #define UCHCOM_LCR2_PARODD 0x06
110 #define UCHCOM_LCR2_PARMARK 0x05
111 #define UCHCOM_LCR2_PARSPACE 0x04
112
113 #define UCHCOM_INTR_STAT1 0x02
114 #define UCHCOM_INTR_STAT2 0x03
115 #define UCHCOM_INTR_LEAST 4
116
117 #define UCHCOMIBUFSIZE 256
118 #define UCHCOMOBUFSIZE 256
119
120 struct uchcom_softc
121 {
122 device_t sc_dev;
123 struct usbd_device * sc_udev;
124 device_t sc_subdev;
125 struct usbd_interface * sc_iface;
126 int sc_dying;
127 /* */
128 int sc_intr_endpoint;
129 int sc_intr_size;
130 struct usbd_pipe * sc_intr_pipe;
131 u_char *sc_intr_buf;
132 /* */
133 uint8_t sc_version;
134 int sc_dtr;
135 int sc_rts;
136 u_char sc_lsr;
137 u_char sc_msr;
138 int sc_lcr1;
139 int sc_lcr2;
140 };
141
142 struct uchcom_endpoints
143 {
144 int ep_bulkin;
145 int ep_bulkout;
146 int ep_intr;
147 int ep_intr_size;
148 };
149
150 struct uchcom_divider
151 {
152 uint8_t dv_prescaler;
153 uint8_t dv_div;
154 uint8_t dv_mod;
155 };
156
157 struct uchcom_divider_record
158 {
159 uint32_t dvr_high;
160 uint32_t dvr_low;
161 uint32_t dvr_base_clock;
162 struct uchcom_divider dvr_divider;
163 };
164
165 static const struct uchcom_divider_record dividers[] =
166 {
167 { 307200, 307200, UCHCOM_BASE_UNKNOWN, { 7, 0xD9, 0 } },
168 { 921600, 921600, UCHCOM_BASE_UNKNOWN, { 7, 0xF3, 0 } },
169 { 2999999, 23530, 6000000, { 3, 0, 0 } },
170 { 23529, 2942, 750000, { 2, 0, 0 } },
171 { 2941, 368, 93750, { 1, 0, 0 } },
172 { 367, 1, 11719, { 0, 0, 0 } },
173 };
174 #define NUM_DIVIDERS (sizeof (dividers) / sizeof (dividers[0]))
175
176 static const struct usb_devno uchcom_devs[] = {
177 { USB_VENDOR_WINCHIPHEAD, USB_PRODUCT_WINCHIPHEAD_CH341SER },
178 { USB_VENDOR_WINCHIPHEAD2, USB_PRODUCT_WINCHIPHEAD2_CH341 },
179 };
180 #define uchcom_lookup(v, p) usb_lookup(uchcom_devs, v, p)
181
182 Static void uchcom_get_status(void *, int, u_char *, u_char *);
183 Static void uchcom_set(void *, int, int, int);
184 Static int uchcom_param(void *, int, struct termios *);
185 Static int uchcom_open(void *, int);
186 Static void uchcom_close(void *, int);
187 Static void uchcom_intr(struct usbd_xfer *, void *,
188 usbd_status);
189
190 static int set_config(struct uchcom_softc *);
191 static int find_ifaces(struct uchcom_softc *, struct usbd_interface **);
192 static int find_endpoints(struct uchcom_softc *,
193 struct uchcom_endpoints *);
194 static void close_intr_pipe(struct uchcom_softc *);
195
196
197 struct ucom_methods uchcom_methods = {
198 .ucom_get_status = uchcom_get_status,
199 .ucom_set = uchcom_set,
200 .ucom_param = uchcom_param,
201 .ucom_ioctl = NULL,
202 .ucom_open = uchcom_open,
203 .ucom_close = uchcom_close,
204 .ucom_read = NULL,
205 .ucom_write = NULL,
206 };
207
208 int uchcom_match(device_t, cfdata_t, void *);
209 void uchcom_attach(device_t, device_t, void *);
210 void uchcom_childdet(device_t, device_t);
211 int uchcom_detach(device_t, int);
212 int uchcom_activate(device_t, enum devact);
213
214 extern struct cfdriver uchcom_cd;
215
216 CFATTACH_DECL2_NEW(uchcom,
217 sizeof(struct uchcom_softc),
218 uchcom_match,
219 uchcom_attach,
220 uchcom_detach,
221 uchcom_activate,
222 NULL,
223 uchcom_childdet);
224
225 /* ----------------------------------------------------------------------
226 * driver entry points
227 */
228
229 int
230 uchcom_match(device_t parent, cfdata_t match, void *aux)
231 {
232 struct usb_attach_arg *uaa = aux;
233
234 return (uchcom_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
235 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
236 }
237
238 void
239 uchcom_attach(device_t parent, device_t self, void *aux)
240 {
241 struct uchcom_softc *sc = device_private(self);
242 struct usb_attach_arg *uaa = aux;
243 struct usbd_device *dev = uaa->uaa_device;
244 char *devinfop;
245 struct uchcom_endpoints endpoints;
246 struct ucom_attach_args ucaa;
247
248 aprint_naive("\n");
249 aprint_normal("\n");
250
251 devinfop = usbd_devinfo_alloc(dev, 0);
252 aprint_normal_dev(self, "%s\n", devinfop);
253 usbd_devinfo_free(devinfop);
254
255 sc->sc_dev = self;
256 sc->sc_udev = dev;
257 sc->sc_dying = 0;
258 sc->sc_dtr = sc->sc_rts = -1;
259 sc->sc_lsr = sc->sc_msr = 0;
260
261 DPRINTF(("\n\nuchcom attach: sc=%p\n", sc));
262
263 if (set_config(sc))
264 goto failed;
265
266 switch (uaa->uaa_release) {
267 case UCHCOM_REV_CH340:
268 aprint_normal_dev(self, "CH340 detected\n");
269 break;
270 default:
271 aprint_normal_dev(self, "CH341 detected\n");
272 break;
273 }
274
275 if (find_ifaces(sc, &sc->sc_iface))
276 goto failed;
277
278 if (find_endpoints(sc, &endpoints))
279 goto failed;
280
281 sc->sc_intr_endpoint = endpoints.ep_intr;
282 sc->sc_intr_size = endpoints.ep_intr_size;
283
284 /* setup ucom layer */
285 ucaa.ucaa_portno = UCOM_UNK_PORTNO;
286 ucaa.ucaa_bulkin = endpoints.ep_bulkin;
287 ucaa.ucaa_bulkout = endpoints.ep_bulkout;
288 ucaa.ucaa_ibufsize = UCHCOMIBUFSIZE;
289 ucaa.ucaa_obufsize = UCHCOMOBUFSIZE;
290 ucaa.ucaa_ibufsizepad = UCHCOMIBUFSIZE;
291 ucaa.ucaa_opkthdrlen = 0;
292 ucaa.ucaa_device = dev;
293 ucaa.ucaa_iface = sc->sc_iface;
294 ucaa.ucaa_methods = &uchcom_methods;
295 ucaa.ucaa_arg = sc;
296 ucaa.ucaa_info = NULL;
297
298 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
299 sc->sc_dev);
300
301 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa,
302 ucomprint, ucomsubmatch);
303
304 return;
305
306 failed:
307 sc->sc_dying = 1;
308 return;
309 }
310
311 void
312 uchcom_childdet(device_t self, device_t child)
313 {
314 struct uchcom_softc *sc = device_private(self);
315
316 KASSERT(sc->sc_subdev == child);
317 sc->sc_subdev = NULL;
318 }
319
320 int
321 uchcom_detach(device_t self, int flags)
322 {
323 struct uchcom_softc *sc = device_private(self);
324 int rv = 0;
325
326 DPRINTF(("uchcom_detach: sc=%p flags=%d\n", sc, flags));
327
328 close_intr_pipe(sc);
329
330 sc->sc_dying = 1;
331
332 if (sc->sc_subdev != NULL)
333 rv = config_detach(sc->sc_subdev, flags);
334
335 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
336 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
574 return 0;
575 }
576
577 static void
578 convert_status(struct uchcom_softc *sc, uint8_t cur)
579 {
580 sc->sc_dtr = !(cur & UCHCOM_DTR_MASK);
581 sc->sc_rts = !(cur & UCHCOM_RTS_MASK);
582
583 cur = ~cur & 0x0F;
584 sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur);
585 }
586
587 static int
588 update_status(struct uchcom_softc *sc)
589 {
590 usbd_status err;
591 uint8_t cur;
592
593 err = get_status(sc, &cur);
594 if (err) {
595 aprint_error_dev(sc->sc_dev,
596 "cannot update status: %s\n", usbd_errstr(err));
597 return EIO;
598 }
599 convert_status(sc, cur);
600
601 return 0;
602 }
603
604
605 static int
606 set_dtrrts(struct uchcom_softc *sc, int dtr, int rts)
607 {
608 usbd_status err;
609 uint8_t val = 0;
610
611 if (dtr) val |= UCHCOM_DTR_MASK;
612 if (rts) val |= UCHCOM_RTS_MASK;
613
614 if (sc->sc_version < UCHCOM_VER_20)
615 err = set_dtrrts_10(sc, ~val);
616 else
617 err = set_dtrrts_20(sc, ~val);
618
619 if (err) {
620 aprint_error_dev(sc->sc_dev, "cannot set DTR/RTS: %s\n",
621 usbd_errstr(err));
622 return EIO;
623 }
624
625 return 0;
626 }
627
628 static int
629 set_break(struct uchcom_softc *sc, int onoff)
630 {
631 usbd_status err;
632 uint8_t brk1, brk2;
633
634 err = read_reg(sc, UCHCOM_REG_BREAK1, &brk1, UCHCOM_REG_BREAK2, &brk2);
635 if (err)
636 return EIO;
637 if (onoff) {
638 /* on - clear bits */
639 brk1 &= ~UCHCOM_BRK1_MASK;
640 brk2 &= ~UCHCOM_BRK2_MASK;
641 } else {
642 /* off - set bits */
643 brk1 |= UCHCOM_BRK1_MASK;
644 brk2 |= UCHCOM_BRK2_MASK;
645 }
646 err = write_reg(sc, UCHCOM_REG_BREAK1, brk1, UCHCOM_REG_BREAK2, brk2);
647 if (err)
648 return EIO;
649
650 return 0;
651 }
652
653 static int
654 calc_divider_settings(struct uchcom_divider *dp, uint32_t rate)
655 {
656 int i;
657 const struct uchcom_divider_record *rp;
658 uint32_t div, rem, mod;
659
660 /* find record */
661 for (i=0; i<NUM_DIVIDERS; i++) {
662 if (dividers[i].dvr_high >= rate &&
663 dividers[i].dvr_low <= rate) {
664 rp = ÷rs[i];
665 goto found;
666 }
667 }
668 return -1;
669
670 found:
671 dp->dv_prescaler = rp->dvr_divider.dv_prescaler;
672 if (rp->dvr_base_clock == UCHCOM_BASE_UNKNOWN)
673 dp->dv_div = rp->dvr_divider.dv_div;
674 else {
675 div = rp->dvr_base_clock / rate;
676 rem = rp->dvr_base_clock % rate;
677 if (div==0 || div>=0xFF)
678 return -1;
679 if ((rem<<1) >= rate)
680 div += 1;
681 dp->dv_div = (uint8_t)-div;
682 }
683
684 mod = UCHCOM_BPS_MOD_BASE/rate + UCHCOM_BPS_MOD_BASE_OFS;
685 mod = mod + mod/2;
686
687 dp->dv_mod = mod / 0x100;
688
689 return 0;
690 }
691
692 static int
693 set_dte_rate(struct uchcom_softc *sc, uint32_t rate)
694 {
695 usbd_status err;
696 struct uchcom_divider dv;
697
698 if (calc_divider_settings(&dv, rate))
699 return EINVAL;
700
701 if ((err = write_reg(sc,
702 UCHCOM_REG_BPS_PRE, dv.dv_prescaler,
703 UCHCOM_REG_BPS_DIV, dv.dv_div)) ||
704 (err = write_reg(sc,
705 UCHCOM_REG_BPS_MOD, dv.dv_mod,
706 UCHCOM_REG_BPS_PAD, 0))) {
707 aprint_error_dev(sc->sc_dev, "cannot set DTE rate: %s\n",
708 usbd_errstr(err));
709 return EIO;
710 }
711
712 return 0;
713 }
714
715 static int
716 set_line_control(struct uchcom_softc *sc, tcflag_t cflag)
717 {
718 usbd_status err;
719 uint8_t lcr1val = 0, lcr2val = 0;
720
721 err = read_reg(sc, UCHCOM_REG_LCR1, &lcr1val, UCHCOM_REG_LCR2, &lcr2val);
722 if (err) {
723 aprint_error_dev(sc->sc_dev, "cannot get LCR: %s\n",
724 usbd_errstr(err));
725 return EIO;
726 }
727
728 lcr1val &= ~UCHCOM_LCR1_MASK;
729 lcr2val &= ~UCHCOM_LCR2_MASK;
730
731 /*
732 * XXX: it is difficult to handle the line control appropriately:
733 * - CS8, !CSTOPB and any parity mode seems ok, but
734 * - the chip doesn't have the function to calculate parity
735 * in !CS8 mode.
736 * - it is unclear that the chip supports CS5,6 mode.
737 * - it is unclear how to handle stop bits.
738 */
739
740 switch (ISSET(cflag, CSIZE)) {
741 case CS5:
742 case CS6:
743 case CS7:
744 return EINVAL;
745 case CS8:
746 break;
747 }
748
749 if (ISSET(cflag, PARENB)) {
750 lcr1val |= UCHCOM_LCR1_PARENB;
751 if (ISSET(cflag, PARODD))
752 lcr2val |= UCHCOM_LCR2_PARODD;
753 else
754 lcr2val |= UCHCOM_LCR2_PAREVEN;
755 }
756
757 err = write_reg(sc, UCHCOM_REG_LCR1, lcr1val, UCHCOM_REG_LCR2, lcr2val);
758 if (err) {
759 aprint_error_dev(sc->sc_dev, "cannot set LCR: %s\n",
760 usbd_errstr(err));
761 return EIO;
762 }
763
764 return 0;
765 }
766
767 static int
768 clear_chip(struct uchcom_softc *sc)
769 {
770 usbd_status err;
771
772 DPRINTF(("%s: clear\n", device_xname(sc->sc_dev)));
773 err = generic_control_out(sc, UCHCOM_REQ_RESET, 0, 0);
774 if (err) {
775 aprint_error_dev(sc->sc_dev, "cannot clear: %s\n",
776 usbd_errstr(err));
777 return EIO;
778 }
779
780 return 0;
781 }
782
783 static int
784 reset_chip(struct uchcom_softc *sc)
785 {
786 usbd_status err;
787 uint8_t lcr1val, lcr2val, pre, div, mod;
788 uint16_t val=0, idx=0;
789
790 err = read_reg(sc, UCHCOM_REG_LCR1, &lcr1val, UCHCOM_REG_LCR2, &lcr2val);
791 if (err)
792 goto failed;
793
794 err = read_reg(sc, UCHCOM_REG_BPS_PRE, &pre, UCHCOM_REG_BPS_DIV, &div);
795 if (err)
796 goto failed;
797
798 err = read_reg(sc, UCHCOM_REG_BPS_MOD, &mod, UCHCOM_REG_BPS_PAD, NULL);
799 if (err)
800 goto failed;
801
802 val |= (uint16_t)(lcr1val&0xF0) << 8;
803 val |= 0x01;
804 val |= (uint16_t)(lcr2val&0x0F) << 8;
805 val |= 0x02;
806 idx |= pre & 0x07;
807 val |= 0x04;
808 idx |= (uint16_t)div << 8;
809 val |= 0x08;
810 idx |= mod & 0xF8;
811 val |= 0x10;
812
813 DPRINTF(("%s: reset v=0x%04X, i=0x%04X\n",
814 device_xname(sc->sc_dev), val, idx));
815
816 err = generic_control_out(sc, UCHCOM_REQ_RESET, val, idx);
817 if (err)
818 goto failed;
819
820 return 0;
821
822 failed:
823 printf("%s: cannot reset: %s\n",
824 device_xname(sc->sc_dev), usbd_errstr(err));
825 return EIO;
826 }
827
828 static int
829 setup_comm(struct uchcom_softc *sc)
830 {
831 int ret;
832
833 ret = update_version(sc);
834 if (ret)
835 return ret;
836
837 ret = clear_chip(sc);
838 if (ret)
839 return ret;
840
841 ret = set_dte_rate(sc, TTYDEF_SPEED);
842 if (ret)
843 return ret;
844
845 ret = set_line_control(sc, CS8);
846 if (ret)
847 return ret;
848
849 ret = update_status(sc);
850 if (ret)
851 return ret;
852
853 ret = reset_chip(sc);
854 if (ret)
855 return ret;
856
857 ret = set_dte_rate(sc, TTYDEF_SPEED); /* XXX */
858 if (ret)
859 return ret;
860
861 sc->sc_dtr = sc->sc_rts = 1;
862 ret = set_dtrrts(sc, sc->sc_dtr, sc->sc_rts);
863 if (ret)
864 return ret;
865
866 return 0;
867 }
868
869 static int
870 setup_intr_pipe(struct uchcom_softc *sc)
871 {
872 usbd_status err;
873
874 if (sc->sc_intr_endpoint != -1 && sc->sc_intr_pipe == NULL) {
875 sc->sc_intr_buf = kmem_alloc(sc->sc_intr_size, KM_SLEEP);
876 err = usbd_open_pipe_intr(sc->sc_iface,
877 sc->sc_intr_endpoint,
878 USBD_SHORT_XFER_OK,
879 &sc->sc_intr_pipe, sc,
880 sc->sc_intr_buf,
881 sc->sc_intr_size,
882 uchcom_intr, USBD_DEFAULT_INTERVAL);
883 if (err) {
884 aprint_error_dev(sc->sc_dev,
885 "cannot open interrupt pipe: %s\n",
886 usbd_errstr(err));
887 return EIO;
888 }
889 }
890 return 0;
891 }
892
893 static void
894 close_intr_pipe(struct uchcom_softc *sc)
895 {
896 usbd_status err;
897
898 if (sc->sc_dying)
899 return;
900
901 if (sc->sc_intr_pipe != NULL) {
902 err = usbd_abort_pipe(sc->sc_intr_pipe);
903 if (err)
904 aprint_error_dev(sc->sc_dev,
905 "abort interrupt pipe failed: %s\n",
906 usbd_errstr(err));
907 err = usbd_close_pipe(sc->sc_intr_pipe);
908 if (err)
909 aprint_error_dev(sc->sc_dev,
910 "close interrupt pipe failed: %s\n",
911 usbd_errstr(err));
912 kmem_free(sc->sc_intr_buf, sc->sc_intr_size);
913 sc->sc_intr_pipe = NULL;
914 }
915 }
916
917
918 /* ----------------------------------------------------------------------
919 * methods for ucom
920 */
921 void
922 uchcom_get_status(void *arg, int portno, u_char *rlsr, u_char *rmsr)
923 {
924 struct uchcom_softc *sc = arg;
925
926 if (sc->sc_dying)
927 return;
928
929 *rlsr = sc->sc_lsr;
930 *rmsr = sc->sc_msr;
931 }
932
933 void
934 uchcom_set(void *arg, int portno, int reg, int onoff)
935 {
936 struct uchcom_softc *sc = arg;
937
938 if (sc->sc_dying)
939 return;
940
941 switch (reg) {
942 case UCOM_SET_DTR:
943 sc->sc_dtr = !!onoff;
944 set_dtrrts(sc, sc->sc_dtr, sc->sc_rts);
945 break;
946 case UCOM_SET_RTS:
947 sc->sc_rts = !!onoff;
948 set_dtrrts(sc, sc->sc_dtr, sc->sc_rts);
949 break;
950 case UCOM_SET_BREAK:
951 set_break(sc, onoff);
952 break;
953 }
954 }
955
956 int
957 uchcom_param(void *arg, int portno, struct termios *t)
958 {
959 struct uchcom_softc *sc = arg;
960 int ret;
961
962 if (sc->sc_dying)
963 return 0;
964
965 ret = set_line_control(sc, t->c_cflag);
966 if (ret)
967 return ret;
968
969 ret = set_dte_rate(sc, t->c_ospeed);
970 if (ret)
971 return ret;
972
973 return 0;
974 }
975
976 int
977 uchcom_open(void *arg, int portno)
978 {
979 int ret;
980 struct uchcom_softc *sc = arg;
981
982 if (sc->sc_dying)
983 return EIO;
984
985 ret = setup_intr_pipe(sc);
986 if (ret)
987 return ret;
988
989 ret = setup_comm(sc);
990 if (ret)
991 return ret;
992
993 return 0;
994 }
995
996 void
997 uchcom_close(void *arg, int portno)
998 {
999 struct uchcom_softc *sc = arg;
1000
1001 if (sc->sc_dying)
1002 return;
1003
1004 close_intr_pipe(sc);
1005 }
1006
1007
1008 /* ----------------------------------------------------------------------
1009 * callback when the modem status is changed.
1010 */
1011 void
1012 uchcom_intr(struct usbd_xfer *xfer, void * priv,
1013 usbd_status status)
1014 {
1015 struct uchcom_softc *sc = priv;
1016 u_char *buf = sc->sc_intr_buf;
1017
1018 if (sc->sc_dying)
1019 return;
1020
1021 if (status != USBD_NORMAL_COMPLETION) {
1022 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1023 return;
1024
1025 DPRINTF(("%s: abnormal status: %s\n",
1026 device_xname(sc->sc_dev), usbd_errstr(status)));
1027 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
1028 return;
1029 }
1030 DPRINTF(("%s: intr: 0x%02X 0x%02X 0x%02X 0x%02X "
1031 "0x%02X 0x%02X 0x%02X 0x%02X\n",
1032 device_xname(sc->sc_dev),
1033 (unsigned)buf[0], (unsigned)buf[1],
1034 (unsigned)buf[2], (unsigned)buf[3],
1035 (unsigned)buf[4], (unsigned)buf[5],
1036 (unsigned)buf[6], (unsigned)buf[7]));
1037
1038 convert_status(sc, buf[UCHCOM_INTR_STAT1]);
1039 ucom_status_change(device_private(sc->sc_subdev));
1040 }
1041