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