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