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