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