xref: /netbsd-src/sys/dev/usb/ucycom.c (revision 267197ec1eebfcb9810ea27a89625b6ddf68e3e7)
1 /*	$NetBSD: ucycom.c,v 1.20 2007/12/11 12:16:34 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 2005 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Nick Hudson
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 /*
39  * This code is based on the ucom driver.
40  */
41 
42 /*
43  * Device driver for Cypress CY7C637xx and CY7C640/1xx series USB to
44  * RS232 bridges.
45  */
46 
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: ucycom.c,v 1.20 2007/12/11 12:16:34 lukem Exp $");
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/conf.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/device.h>
56 #include <sys/sysctl.h>
57 #include <sys/tty.h>
58 #include <sys/file.h>
59 #include <sys/vnode.h>
60 #include <sys/kauth.h>
61 
62 #include <dev/usb/usb.h>
63 #include <dev/usb/usbhid.h>
64 
65 #include <dev/usb/usbdi.h>
66 #include <dev/usb/usbdi_util.h>
67 #include <dev/usb/usbdevs.h>
68 #include <dev/usb/uhidev.h>
69 #include <dev/usb/hid.h>
70 
71 #include "ioconf.h"
72 
73 #ifdef UCYCOM_DEBUG
74 #define DPRINTF(x)	if (ucycomdebug) logprintf x
75 #define DPRINTFN(n, x)	if (ucycomdebug > (n)) logprintf x
76 int	ucycomdebug = 0;
77 #else
78 #define DPRINTF(x)
79 #define DPRINTFN(n,x)
80 #endif
81 
82 
83 #define UCYCOMUNIT_MASK		0x3ffff
84 #define UCYCOMDIALOUT_MASK	0x80000
85 #define UCYCOMCALLUNIT_MASK	0x40000
86 
87 #define UCYCOMUNIT(x)		(minor(x) & UCYCOMUNIT_MASK)
88 #define UCYCOMDIALOUT(x)	(minor(x) & UCYCOMDIALOUT_MASK)
89 #define UCYCOMCALLUNIT(x)	(minor(x) & UCYCOMCALLUNIT_MASK)
90 
91 /* Configuration Byte */
92 #define UCYCOM_RESET		0x80
93 #define UCYCOM_PARITY_TYPE_MASK	0x20
94 #define  UCYCOM_PARITY_ODD	 0x20
95 #define  UCYCOM_PARITY_EVEN	 0x00
96 #define UCYCOM_PARITY_MASK	0x10
97 #define  UCYCOM_PARITY_ON	 0x10
98 #define  UCYCOM_PARITY_OFF	 0x00
99 #define UCYCOM_STOP_MASK	0x08
100 #define  UCYCOM_STOP_BITS_2	 0x08
101 #define  UCYCOM_STOP_BITS_1	 0x00
102 #define UCYCOM_DATA_MASK	0x03
103 #define  UCYCOM_DATA_BITS_8	 0x03
104 #define  UCYCOM_DATA_BITS_7	 0x02
105 #define  UCYCOM_DATA_BITS_6	 0x01
106 #define  UCYCOM_DATA_BITS_5	 0x00
107 
108 /* Modem (Input) status byte */
109 #define UCYCOM_RI	0x80
110 #define UCYCOM_DCD	0x40
111 #define UCYCOM_DSR	0x20
112 #define UCYCOM_CTS	0x10
113 #define UCYCOM_ERROR	0x08
114 #define UCYCOM_LMASK	0x07
115 
116 /* Modem (Output) control byte */
117 #define UCYCOM_DTR	0x20
118 #define UCYCOM_RTS	0x10
119 #define UCYCOM_ORESET	0x08
120 
121 struct ucycom_softc {
122 	struct uhidev		sc_hdev;
123 
124 	struct tty		*sc_tty;
125 
126 	/* uhidev parameters */
127 	size_t			sc_flen; /* feature report length */
128 	size_t			sc_ilen; /* input report length */
129 	size_t			sc_olen; /* output report length */
130 
131 	uint8_t			*sc_obuf;
132 
133 	/* settings */
134 	uint32_t		sc_baud;
135 	uint8_t			sc_cfg;	/* Data format */
136 	uint8_t			sc_mcr;	/* Modem control */
137 	uint8_t			sc_msr;	/* Modem status */
138 	int			sc_swflags;
139 
140 	/* flags */
141 	char			sc_dying;
142 };
143 
144 dev_type_open(ucycomopen);
145 dev_type_close(ucycomclose);
146 dev_type_read(ucycomread);
147 dev_type_write(ucycomwrite);
148 dev_type_ioctl(ucycomioctl);
149 dev_type_stop(ucycomstop);
150 dev_type_tty(ucycomtty);
151 dev_type_poll(ucycompoll);
152 
153 const struct cdevsw ucycom_cdevsw = {
154 	ucycomopen, ucycomclose, ucycomread, ucycomwrite, ucycomioctl,
155 	ucycomstop, ucycomtty, ucycompoll, nommap, ttykqfilter, D_TTY
156 };
157 
158 Static int ucycomparam(struct tty *, struct termios *);
159 Static void ucycomstart(struct tty *);
160 Static void ucycom_intr(struct uhidev *, void *, u_int);
161 Static int ucycom_configure(struct ucycom_softc *, uint32_t, uint8_t);
162 Static void tiocm_to_ucycom(struct ucycom_softc *, u_long, int);
163 Static int ucycom_to_tiocm(struct ucycom_softc *);
164 Static void ucycom_set_status(struct ucycom_softc *);
165 Static void ucycom_dtr(struct ucycom_softc *, int);
166 #if 0
167 Static void ucycom_rts(struct ucycom_softc *, int);
168 #endif
169 Static void ucycom_cleanup(struct ucycom_softc *sc);
170 
171 #ifdef UCYCOM_DEBUG
172 Static void ucycom_get_cfg(struct ucycom_softc *);
173 #endif
174 
175 Static const struct usb_devno ucycom_devs[] = {
176 	{ USB_VENDOR_CYPRESS, USB_PRODUCT_CYPRESS_USBRS232 },
177 	{ USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EARTHMATE },
178 };
179 #define ucycom_lookup(v, p) usb_lookup(ucycom_devs, v, p)
180 
181 USB_DECLARE_DRIVER(ucycom);
182 
183 int
184 ucycom_match(struct device *parent, struct cfdata *match,
185     void *aux)
186 {
187 	struct uhidev_attach_arg *uha = aux;
188 
189 	return (ucycom_lookup(uha->uaa->vendor, uha->uaa->product) != NULL ?
190 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
191 }
192 
193 void
194 ucycom_attach(struct device *parent, struct device *self, void *aux)
195 {
196 	struct ucycom_softc *sc = (struct ucycom_softc *)self;
197 	struct uhidev_attach_arg *uha = aux;
198 	int size, repid;
199 	void *desc;
200 
201 	sc->sc_hdev.sc_intr = ucycom_intr;
202 	sc->sc_hdev.sc_parent = uha->parent;
203 	sc->sc_hdev.sc_report_id = uha->reportid;
204 
205 	uhidev_get_report_desc(uha->parent, &desc, &size);
206 	repid = uha->reportid;
207 	sc->sc_ilen = hid_report_size(desc, size, hid_input, repid);
208 	sc->sc_olen = hid_report_size(desc, size, hid_output, repid);
209 	sc->sc_flen = hid_report_size(desc, size, hid_feature, repid);
210 
211 	sc->sc_msr = sc->sc_mcr = 0;
212 
213 	/* set up tty */
214 	sc->sc_tty = ttymalloc();
215 	sc->sc_tty->t_sc = sc;
216 	sc->sc_tty->t_oproc = ucycomstart;
217 	sc->sc_tty->t_param = ucycomparam;
218 
219 	tty_attach(sc->sc_tty);
220 
221 	/* Nothing interesting to report */
222 	printf("\n");
223 
224 }
225 
226 
227 int
228 ucycom_detach(struct device *self, int flags)
229 {
230 	struct ucycom_softc *sc = (struct ucycom_softc *)self;
231 	struct tty *tp = sc->sc_tty;
232 	int maj, mn;
233 	int s;
234 
235 	DPRINTF(("ucycom_detach: sc=%p flags=%d tp=%p\n", sc, flags, tp));
236 
237 	sc->sc_dying = 1;
238 
239 	s = splusb();
240 	if (tp != NULL) {
241 		mutex_spin_enter(&tty_lock);
242 		CLR(tp->t_state, TS_CARR_ON);
243 		CLR(tp->t_cflag, CLOCAL | MDMBUF);
244 		ttyflush(tp, FREAD|FWRITE);
245 		mutex_spin_exit(&tty_lock);
246 	}
247 	/* Wait for processes to go away. */
248 	usb_detach_wait(USBDEV(sc->sc_hdev.sc_dev));
249 	splx(s);
250 
251 	/* locate the major number */
252 	maj = cdevsw_lookup_major(&ucycom_cdevsw);
253 
254 	/* Nuke the vnodes for any open instances. */
255 	mn = device_unit(self);
256 
257 	DPRINTFN(2, ("ucycom_detach: maj=%d mn=%d\n", maj, mn));
258 	vdevgone(maj, mn, mn, VCHR);
259 	vdevgone(maj, mn | UCYCOMDIALOUT_MASK, mn | UCYCOMDIALOUT_MASK, VCHR);
260 	vdevgone(maj, mn | UCYCOMCALLUNIT_MASK, mn | UCYCOMCALLUNIT_MASK, VCHR);
261 
262 	/* Detach and free the tty. */
263 	if (tp != NULL) {
264 		DPRINTF(("ucycom_detach: tty_detach %p\n", tp));
265 		tty_detach(tp);
266 		ttyfree(tp);
267 		sc->sc_tty = NULL;
268 	}
269 
270 	return 0;
271 }
272 
273 int
274 ucycom_activate(device_ptr_t self, enum devact act)
275 {
276 	struct ucycom_softc *sc = (struct ucycom_softc *)self;
277 
278 	DPRINTFN(5,("ucycom_activate: %d\n", act));
279 
280 	switch (act) {
281 	case DVACT_ACTIVATE:
282 		return (EOPNOTSUPP);
283 
284 	case DVACT_DEACTIVATE:
285 		sc->sc_dying = 1;
286 		break;
287 	}
288 	return (0);
289 }
290 
291 #if 0
292 void
293 ucycom_shutdown(struct ucycom_softc *sc)
294 {
295 	struct tty *tp = sc->sc_tty;
296 
297 	DPRINTF(("ucycom_shutdown\n"));
298 	/*
299 	 * Hang up if necessary.  Wait a bit, so the other side has time to
300 	 * notice even if we immediately open the port again.
301 	 */
302 	if (ISSET(tp->t_cflag, HUPCL)) {
303 		ucycom_dtr(sc, 0);
304 		(void)tsleep(sc, TTIPRI, ttclos, hz);
305 	}
306 }
307 #endif
308 
309 int
310 ucycomopen(dev_t dev, int flag, int mode, struct lwp *l)
311 {
312 	int unit = UCYCOMUNIT(dev);
313 	struct ucycom_softc *sc;
314 	struct tty *tp;
315 	int s, err;
316 
317 	DPRINTF(("ucycomopen: unit=%d\n", unit));
318 
319 	if (unit >= ucycom_cd.cd_ndevs)
320 		return (ENXIO);
321 	sc = ucycom_cd.cd_devs[unit];
322 
323 	DPRINTF(("ucycomopen: sc=%p\n", sc));
324 
325 	if (sc == NULL)
326 		return (ENXIO);
327 
328 	if (sc->sc_dying)
329 		return (EIO);
330 
331 	if (!device_is_active(&sc->sc_hdev.sc_dev))
332 		return (ENXIO);
333 
334 	tp = sc->sc_tty;
335 
336 	DPRINTF(("ucycomopen: tp=%p\n", tp));
337 
338 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
339 		return (EBUSY);
340 
341 	s = spltty();
342 
343 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
344 		struct termios t;
345 
346 		tp->t_dev = dev;
347 
348 		err = uhidev_open(&sc->sc_hdev);
349 		if (err) {
350 			/* Any cleanup? */
351 			splx(s);
352 			return (err);
353 		}
354 
355 		/*
356 		 * Initialize the termios status to the defaults.  Add in the
357 		 * sticky bits from TIOCSFLAGS.
358 		 */
359 		t.c_ispeed = 0;
360 		t.c_ospeed = TTYDEF_SPEED;
361 		t.c_cflag = TTYDEF_CFLAG;
362 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
363 			SET(t.c_cflag, CLOCAL);
364 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
365 			SET(t.c_cflag, CRTSCTS);
366 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
367 			SET(t.c_cflag, MDMBUF);
368 
369 		tp->t_ospeed = 0;
370 		(void) ucycomparam(tp, &t);
371 		tp->t_iflag = TTYDEF_IFLAG;
372 		tp->t_oflag = TTYDEF_OFLAG;
373 		tp->t_lflag = TTYDEF_LFLAG;
374 		ttychars(tp);
375 		ttsetwater(tp);
376 
377 		/* Allocate an output report buffer */
378 		sc->sc_obuf = malloc(sc->sc_olen, M_USBDEV, M_WAITOK);
379 
380 		DPRINTF(("ucycomopen: sc->sc_obuf=%p\n", sc->sc_obuf));
381 
382 #if 0
383 		/* XXX Don't do this as for some reason trying to do an interrupt
384 		 * XXX out transfer at this point means everything gets stuck!?!
385 		 */
386 		/*
387 		 * Turn on DTR.  We must always do this, even if carrier is not
388 		 * present, because otherwise we'd have to use TIOCSDTR
389 		 * immediately after setting CLOCAL, which applications do not
390 		 * expect.  We always assert DTR while the device is open
391 		 * unless explicitly requested to deassert it.
392 		 */
393 		ucycom_dtr(sc, 1);
394 #endif
395 
396 #if 0
397 		/* XXX CLR(sc->sc_rx_flags, RX_ANY_BLOCK);*/
398 		ucycom_hwiflow(sc);
399 #endif
400 
401 	}
402 	splx(s);
403 
404 	err = ttyopen(tp, UCYCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
405 	if (err)
406 		goto bad;
407 
408 	err = (*tp->t_linesw->l_open)(dev, tp);
409 	if (err)
410 		goto bad;
411 
412 	return (0);
413 
414 bad:
415 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
416 		/*
417 		 * We failed to open the device, and nobody else had it opened.
418 		 * Clean up the state as appropriate.
419 		 */
420 		ucycom_cleanup(sc);
421 	}
422 
423 	return (err);
424 
425 }
426 
427 
428 int
429 ucycomclose(dev_t dev, int flag, int mode, struct lwp *l)
430 {
431 	struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(dev)];
432 	struct tty *tp = sc->sc_tty;
433 
434 	DPRINTF(("ucycomclose: unit=%d\n", UCYCOMUNIT(dev)));
435 	if (!ISSET(tp->t_state, TS_ISOPEN))
436 		return (0);
437 
438 	(*tp->t_linesw->l_close)(tp, flag);
439 	ttyclose(tp);
440 
441 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
442 		/*
443 		 * Although we got a last close, the device may still be in
444 		 * use; e.g. if this was the dialout node, and there are still
445 		 * processes waiting for carrier on the non-dialout node.
446 		 */
447 		ucycom_cleanup(sc);
448 	}
449 
450 	return (0);
451 }
452 
453 Static void
454 ucycomstart(struct tty *tp)
455 {
456 	struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(tp->t_dev)];
457 	u_char *data;
458 	int cnt, len, err, s;
459 
460 	if (sc->sc_dying)
461 		return;
462 
463 	s = spltty();
464 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
465 		DPRINTFN(4,("ucycomstart: no go, state=0x%x\n", tp->t_state));
466 		goto out;
467 	}
468 
469 #if 0
470 	/* HW FLOW CTL */
471 	if (sc->sc_tx_stopped)
472 		goto out;
473 #endif
474 
475 	if (ttypull(tp) == 0)
476 		goto out;
477 
478 	/* Grab the first contiguous region of buffer space. */
479 	data = tp->t_outq.c_cf;
480 	cnt = ndqb(&tp->t_outq, 0);
481 
482 	if (cnt == 0) {
483 		DPRINTF(("ucycomstart: cnt == 0\n"));
484 		goto out;
485 	}
486 
487 	SET(tp->t_state, TS_BUSY);
488 
489 	/*
490 	 * The 8 byte output report uses byte 0 for control and byte
491 	 * count.
492 	 *
493 	 * The 32 byte output report uses byte 0 for control. Byte 1
494 	 * is used for byte count.
495 	 */
496 	memset(sc->sc_obuf, 0, sc->sc_olen);
497 	len = cnt;
498 	switch (sc->sc_olen) {
499 	case 8:
500 		if (cnt > sc->sc_olen - 1) {
501 			DPRINTF(("ucycomstart(8): big buffer %d chars\n", len));
502 			len = sc->sc_olen - 1;
503 		}
504 
505 		memcpy(sc->sc_obuf + 1, data, len);
506 		sc->sc_obuf[0] = len | sc->sc_mcr;
507 
508 		DPRINTF(("ucycomstart(8): sc->sc_obuf[0] = %d | %d = %d\n", len, sc->sc_mcr, sc->sc_obuf[0]));
509 #ifdef UCYCOM_DEBUG
510 		if (ucycomdebug > 10) {
511 			u_int32_t i;
512 			u_int8_t *d = data;
513 
514 			DPRINTF(("ucycomstart(8): data ="));
515 			for (i = 0; i < len; i++)
516 				DPRINTF((" %02x", d[i]));
517 			DPRINTF(("\n"));
518 		}
519 #endif
520 		break;
521 
522 	case 32:
523 		if (cnt > sc->sc_olen - 2) {
524 			DPRINTF(("ucycomstart(32): big buffer %d chars\n", len));
525 			len = sc->sc_olen - 2;
526 		}
527 
528 		memcpy(sc->sc_obuf + 2, data, len);
529 		sc->sc_obuf[0] = sc->sc_mcr;
530 		sc->sc_obuf[1] = len;
531 		DPRINTF(("ucycomstart(32): sc->sc_obuf[0] = %d\nsc->sc_obuf[1] = %d\n", sc->sc_obuf[0], sc->sc_obuf[1]));
532 #ifdef UCYCOM_DEBUG
533 		if (ucycomdebug > 10) {
534 			u_int32_t i;
535 			u_int8_t *d = data;
536 
537 			DPRINTF(("ucycomstart(32): data ="));
538 			for (i = 0; i < len; i++)
539 				DPRINTF((" %02x", d[i]));
540 			DPRINTF(("\n"));
541 		}
542 #endif
543 		break;
544 
545 	default:
546         	DPRINTFN(2,("ucycomstart: unknown output report size (%zd)\n",
547 		    sc->sc_olen));
548 		goto out;
549 	}
550 	splx(s);
551 
552 #ifdef UCYCOM_DEBUG
553 	if (ucycomdebug > 5) {
554 		int i;
555 
556 		if (len != 0) {
557 			DPRINTF(("ucycomstart: sc->sc_obuf[0..%zd) =", sc->sc_olen));
558 			for (i = 0; i < sc->sc_olen; i++)
559 				DPRINTF((" %02x", sc->sc_obuf[i]));
560 			DPRINTF(("\n"));
561 		}
562 	}
563 #endif
564 	err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf, sc->sc_olen);
565 
566 	if (err) {
567 		DPRINTF(("ucycomstart: error doing uhidev_write = %d\n", err));
568 	}
569 
570 #ifdef UCYCOM_DEBUG
571 	ucycom_get_cfg(sc);
572 #endif
573 	DPRINTFN(4,("ucycomstart: req %d chars did %d chars\n", cnt, len));
574 
575  	s = spltty();
576 	CLR(tp->t_state, TS_BUSY);
577 	if (ISSET(tp->t_state, TS_FLUSH))
578 		CLR(tp->t_state, TS_FLUSH);
579 	else
580 		ndflush(&tp->t_outq, len);
581 	(*tp->t_linesw->l_start)(tp);
582 
583 out:
584 	splx(s);
585 }
586 
587 Static int
588 ucycomparam(struct tty *tp, struct termios *t)
589 {
590 	struct ucycom_softc *sc = tp->t_sc;
591 	uint32_t baud;
592 	uint8_t cfg;
593 	int err;
594 
595 	if (t->c_ospeed < 0) {
596 		DPRINTF(("ucycomparam: c_ospeed < 0\n"));
597 		return (EINVAL);
598 	}
599 
600 	/* Check requested parameters. */
601 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
602 		return (EINVAL);
603 
604 	/*
605 	 * For the console, always force CLOCAL and !HUPCL, so that the port
606 	 * is always active.
607 	 */
608 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
609 		SET(t->c_cflag, CLOCAL);
610 		CLR(t->c_cflag, HUPCL);
611 	}
612 
613 	/*
614 	 * If there were no changes, don't do anything.  This avoids dropping
615 	 * input and improves performance when all we did was frob things like
616 	 * VMIN and VTIME.
617 	 */
618 	if (tp->t_ospeed == t->c_ospeed &&
619 	    tp->t_cflag == t->c_cflag)
620 		return (0);
621 
622 	/* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */
623 
624 	/* And copy to tty. */
625 	tp->t_ispeed = 0;
626 	tp->t_ospeed = t->c_ospeed;
627 	tp->t_cflag = t->c_cflag;
628 
629 	baud = t->c_ispeed;
630 	DPRINTF(("ucycomparam: baud=%d\n", baud));
631 
632 	if (t->c_cflag & CIGNORE) {
633 		cfg = sc->sc_cfg;
634 	} else {
635 		cfg = 0;
636 		switch (t->c_cflag & CSIZE) {
637 		case CS8:
638 			cfg |= UCYCOM_DATA_BITS_8;
639 			break;
640 		case CS7:
641 			cfg |= UCYCOM_DATA_BITS_7;
642 			break;
643 		case CS6:
644 			cfg |= UCYCOM_DATA_BITS_6;
645 			break;
646 		case CS5:
647 			cfg |= UCYCOM_DATA_BITS_5;
648 			break;
649 		default:
650 			return (EINVAL);
651 		}
652 		cfg |= ISSET(t->c_cflag, CSTOPB) ?
653 		    UCYCOM_STOP_BITS_2 : UCYCOM_STOP_BITS_1;
654 		cfg |= ISSET(t->c_cflag, PARENB) ?
655 		    UCYCOM_PARITY_ON : UCYCOM_PARITY_OFF;
656 		cfg |= ISSET(t->c_cflag, PARODD) ?
657 		    UCYCOM_PARITY_ODD : UCYCOM_PARITY_EVEN;
658 	}
659 
660 	/*
661 	 * Update the tty layer's idea of the carrier bit, in case we changed
662 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
663 	 * explicit request.
664 	 */
665 	DPRINTF(("ucycomparam: l_modem\n"));
666 	(void) (*tp->t_linesw->l_modem)(tp, 1 /* XXX carrier */ );
667 
668 	err = ucycom_configure(sc, baud, cfg);
669 	return (err);
670 }
671 
672 void
673 ucycomstop(struct tty *tp, int flag)
674 {
675 	DPRINTF(("ucycomstop: flag=%d\n", flag));
676 }
677 
678 int
679 ucycomread(dev_t dev, struct uio *uio, int flag)
680 {
681 	struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(dev)];
682 	struct tty *tp = sc->sc_tty;
683 	int err;
684 
685 	DPRINTF(("ucycomread: sc=%p, tp=%p, uio=%p, flag=%d\n", sc, tp, uio, flag));
686 	if (sc->sc_dying)
687 		return (EIO);
688 
689 	err = ((*tp->t_linesw->l_read)(tp, uio, flag));
690 	return (err);
691 }
692 
693 
694 int
695 ucycomwrite(dev_t dev, struct uio *uio, int flag)
696 {
697 	struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(dev)];
698 	struct tty *tp = sc->sc_tty;
699 	int err;
700 
701 	DPRINTF(("ucycomwrite: sc=%p, tp=%p, uio=%p, flag=%d\n", sc, tp, uio, flag));
702 	if (sc->sc_dying)
703 		return (EIO);
704 
705 	err = ((*tp->t_linesw->l_write)(tp, uio, flag));
706 	return (err);
707 }
708 
709 struct tty *
710 ucycomtty(dev_t dev)
711 {
712 	struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(dev)];
713 	struct tty *tp = sc->sc_tty;
714 
715 	DPRINTF(("ucycomtty: sc=%p, tp=%p\n", sc, tp));
716 
717 	return (tp);
718 }
719 
720 int
721 ucycomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
722 {
723 	struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(dev)];
724 	struct tty *tp = sc->sc_tty;
725 	int err;
726 	int s;
727 
728 	if (sc->sc_dying)
729 		return (EIO);
730 
731 	DPRINTF(("ucycomioctl: sc=%p, tp=%p, data=%p\n", sc, tp, data));
732 
733 	err = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
734 	if (err != EPASSTHROUGH)
735 		return (err);
736 
737 	err = ttioctl(tp, cmd, data, flag, l);
738 	if (err != EPASSTHROUGH)
739 		return (err);
740 
741 	err = 0;
742 
743 	DPRINTF(("ucycomioctl: our cmd=0x%08lx\n", cmd));
744 	s = spltty();
745 
746 	switch (cmd) {
747 /*	case TIOCSBRK:
748 		ucycom_break(sc, 1);
749 		break;
750 
751 	case TIOCCBRK:
752 		ucycom_break(sc, 0);
753 		break;
754 */
755 	case TIOCSDTR:
756 		ucycom_dtr(sc, 1);
757 		break;
758 
759 	case TIOCCDTR:
760 		ucycom_dtr(sc, 0);
761 		break;
762 
763 	case TIOCGFLAGS:
764 		*(int *)data = sc->sc_swflags;
765 		break;
766 
767 	case TIOCSFLAGS:
768 		err = kauth_authorize_device_tty(l->l_cred,
769 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
770 		if (err)
771 			break;
772 		sc->sc_swflags = *(int *)data;
773 		break;
774 
775 	case TIOCMSET:
776 	case TIOCMBIS:
777 	case TIOCMBIC:
778 		tiocm_to_ucycom(sc, cmd, *(int *)data);
779 		break;
780 
781 	case TIOCMGET:
782 		*(int *)data = ucycom_to_tiocm(sc);
783 		break;
784 
785 	default:
786 		err = EPASSTHROUGH;
787 		break;
788 	}
789 
790 	splx(s);
791 
792 	return (err);
793 }
794 
795 int
796 ucycompoll(dev_t dev, int events, struct lwp *l)
797 {
798 	struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(dev)];
799 	struct tty *tp = sc->sc_tty;
800 	int err;
801 
802 	DPRINTF(("ucycompoll: sc=%p, tp=%p, events=%d, lwp=%p\n", sc, tp, events, l));
803 
804 	if (sc->sc_dying)
805 		return (EIO);
806 
807 	err = ((*tp->t_linesw->l_poll)(tp, events, l));
808 	return (err);
809 }
810 
811 Static int
812 ucycom_configure(struct ucycom_softc *sc, uint32_t baud, uint8_t cfg)
813 {
814 	uint8_t report[5];
815 	int err;
816 
817 	switch (baud) {
818 	case 600:
819 	case 1200:
820 	case 2400:
821 	case 4800:
822 	case 9600:
823 	case 19200:
824 	case 38400:
825 	case 57600:
826 #if 0
827 	/*
828 	 * Stock chips only support standard baud rates in the 600 - 57600
829 	 * range, but higher rates can be achieved using custom firmware.
830 	 */
831 	case 115200:
832 	case 153600:
833 	case 192000:
834 #endif
835 		break;
836 	default:
837 		return (EINVAL);
838 	}
839 
840 	DPRINTF(("ucycom_configure: setting %d baud, %d-%c-%d (%d)\n", baud,
841 	    5 + (cfg & UCYCOM_DATA_MASK),
842 	    (cfg & UCYCOM_PARITY_MASK) ?
843 		((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N',
844 	    (cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg));
845 
846 	report[0] = baud & 0xff;
847 	report[1] = (baud >> 8) & 0xff;
848 	report[2] = (baud >> 16) & 0xff;
849 	report[3] = (baud >> 24) & 0xff;
850 	report[4] = cfg;
851 	err = uhidev_set_report(&sc->sc_hdev, UHID_FEATURE_REPORT,
852 	    report, sc->sc_flen);
853 	if (err != 0) {
854 		DPRINTF(("%s\n", usbd_errstr(err)));
855 		return EIO;
856 	}
857 	sc->sc_baud = baud;
858 	sc->sc_cfg = cfg;
859 
860 	return 0;
861 }
862 
863 Static void
864 ucycom_intr(struct uhidev *addr, void *ibuf, u_int len)
865 {
866 	struct ucycom_softc *sc = (struct ucycom_softc *)addr;
867 	struct tty *tp = sc->sc_tty;
868 	int (*rint)(int , struct tty *) = tp->t_linesw->l_rint;
869 	uint8_t *cp = ibuf;
870 	int s, n, st, chg;
871 
872 	/* We understand 8 byte and 32 byte input records */
873 	switch (len) {
874 	case 8:
875 		n = cp[0] & UCYCOM_LMASK;
876 		st = cp[0] & ~UCYCOM_LMASK;
877 		cp++;
878 		break;
879 
880 	case 32:
881 		st = cp[0];
882 		n = cp[1];
883 		cp += 2;
884 		break;
885 
886 	default:
887 		DPRINTFN(3,("ucycom_intr: Unknown input report length\n"));
888 		return;
889 	}
890 
891 #ifdef UCYCOM_DEBUG
892 	if (ucycomdebug > 5) {
893 		u_int32_t i;
894 
895 		if (n != 0) {
896 			DPRINTF(("ucycom_intr: ibuf[0..%d) =", n));
897 			for (i = 0; i < n; i++)
898 				DPRINTF((" %02x", cp[i]));
899 			DPRINTF(("\n"));
900 		}
901 	}
902 #endif
903 	s = spltty();
904 
905 	/* Give characters to tty layer. */
906 	while (n-- > 0) {
907 		DPRINTFN(7,("ucycom_intr: char=0x%02x\n", *cp));
908 		if ((*rint)(*cp++, tp) == -1) {
909 			/* XXX what should we do? */
910 			printf("%s: lost a character\n", USBDEVNAME(sc->sc_hdev.sc_dev));
911 			break;
912 		}
913 	}
914 	splx(s);
915 	chg = st ^ sc->sc_msr;
916 	sc->sc_msr = st;
917 	if (ISSET(chg, UCYCOM_DCD))
918 		(*tp->t_linesw->l_modem)(tp,
919 		    ISSET(sc->sc_msr, UCYCOM_DCD));
920 
921 }
922 
923 Static void
924 tiocm_to_ucycom(struct ucycom_softc *sc, u_long how, int ttybits)
925 {
926 	u_char combits;
927 	u_char before = sc->sc_mcr;
928 
929 	combits = 0;
930 	if (ISSET(ttybits, TIOCM_DTR))
931 		SET(combits, UCYCOM_DTR);
932 	if (ISSET(ttybits, TIOCM_RTS))
933 		SET(combits, UCYCOM_RTS);
934 
935 	switch (how) {
936 	case TIOCMBIC:
937 		CLR(sc->sc_mcr, combits);
938 		break;
939 
940 	case TIOCMBIS:
941 		SET(sc->sc_mcr, combits);
942 		break;
943 
944 	case TIOCMSET:
945 		CLR(sc->sc_mcr, UCYCOM_DTR | UCYCOM_RTS);
946 		SET(sc->sc_mcr, combits);
947 		break;
948 	}
949 	if (before ^ sc->sc_mcr) {
950 		DPRINTF(("tiocm_to_ucycom: something has changed\n"));
951 		ucycom_set_status(sc);
952 	}
953 }
954 
955 Static int
956 ucycom_to_tiocm(struct ucycom_softc *sc)
957 {
958 	u_char combits;
959 	int ttybits = 0;
960 
961 	combits = sc->sc_mcr;
962 	if (ISSET(combits, UCYCOM_DTR))
963 		SET(ttybits, TIOCM_DTR);
964 	if (ISSET(combits, UCYCOM_RTS))
965 		SET(ttybits, TIOCM_RTS);
966 
967 	combits = sc->sc_msr;
968 	if (ISSET(combits, UCYCOM_DCD))
969 		SET(ttybits, TIOCM_CD);
970 	if (ISSET(combits, UCYCOM_CTS))
971 		SET(ttybits, TIOCM_CTS);
972 	if (ISSET(combits, UCYCOM_DSR))
973 		SET(ttybits, TIOCM_DSR);
974 	if (ISSET(combits, UCYCOM_RI))
975 		SET(ttybits, TIOCM_RI);
976 
977 	return (ttybits);
978 }
979 
980 Static void
981 ucycom_dtr(struct ucycom_softc *sc, int set)
982 {
983 	uint8_t old;
984 
985 	old = sc->sc_mcr;
986 	if (set)
987 		SET(sc->sc_mcr, UCYCOM_DTR);
988 	else
989 		CLR(sc->sc_mcr, UCYCOM_DTR);
990 
991 	if (old ^ sc->sc_mcr)
992 		ucycom_set_status(sc);
993 }
994 
995 #if 0
996 Static void
997 ucycom_rts(struct ucycom_softc *sc, int set)
998 {
999 	uint8_t old;
1000 
1001 	old = sc->sc_msr;
1002 	if (set)
1003 		SET(sc->sc_mcr, UCYCOM_RTS);
1004 	else
1005 		CLR(sc->sc_mcr, UCYCOM_RTS);
1006 
1007 	if (old ^ sc->sc_mcr)
1008 		ucycom_set_status(sc);
1009 }
1010 #endif
1011 
1012 Static void
1013 ucycom_set_status(struct ucycom_softc *sc)
1014 {
1015 	int err;
1016 
1017 	if (sc->sc_olen != 8 && sc->sc_olen != 32) {
1018 		DPRINTFN(2,("ucycom_set_status: unknown output report size (%zd)\n",
1019 		    sc->sc_olen));
1020 		return;
1021 	}
1022 
1023 	DPRINTF(("ucycom_set_status: %d\n", sc->sc_mcr));
1024 
1025 	memset(sc->sc_obuf, 0, sc->sc_olen);
1026 	sc->sc_obuf[0] = sc->sc_mcr;
1027 
1028 	err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf, sc->sc_olen);
1029 	if (err) {
1030 		DPRINTF(("ucycom_set_status: err=%d\n", err));
1031 	}
1032 }
1033 
1034 #ifdef UCYCOM_DEBUG
1035 Static void
1036 ucycom_get_cfg(struct ucycom_softc *sc)
1037 {
1038 	int err, cfg, baud;
1039 	uint8_t report[5];
1040 
1041 	err = uhidev_get_report(&sc->sc_hdev, UHID_FEATURE_REPORT,
1042 	    report, sc->sc_flen);
1043 	cfg = report[4];
1044 	baud = (report[3] << 24) + (report[2] << 16) + (report[1] << 8) + report[0];
1045 	DPRINTF(("ucycom_configure: device reports %d baud, %d-%c-%d (%d)\n", baud,
1046 	    5 + (cfg & UCYCOM_DATA_MASK),
1047 	    (cfg & UCYCOM_PARITY_MASK) ?
1048 		((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N',
1049 	    (cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg));
1050 }
1051 #endif
1052 
1053 Static void
1054 ucycom_cleanup(struct ucycom_softc *sc)
1055 {
1056 	DPRINTF(("ucycom_cleanup: closing uhidev\n"));
1057 
1058 	if (sc->sc_obuf !=NULL)
1059 		free (sc->sc_obuf, M_USBDEV);
1060 	uhidev_close(&sc->sc_hdev);
1061 }
1062