Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
/*	$NetBSD: ucycom.c,v 1.47.2.1 2020/01/02 09:42:06 martin Exp $	*/

/*
 * Copyright (c) 2005 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Nick Hudson
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * This code is based on the ucom driver.
 */

/*
 * Device driver for Cypress CY7C637xx and CY7C640/1xx series USB to
 * RS232 bridges.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ucycom.c,v 1.47.2.1 2020/01/02 09:42:06 martin Exp $");

#ifdef _KERNEL_OPT
#include "opt_usb.h"
#endif

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/device.h>
#include <sys/sysctl.h>
#include <sys/tty.h>
#include <sys/file.h>
#include <sys/vnode.h>
#include <sys/kauth.h>
#include <sys/lwp.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbhid.h>

#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdevs.h>
#include <dev/usb/uhidev.h>
#include <dev/hid/hid.h>

#include "ioconf.h"

#ifdef UCYCOM_DEBUG
#define DPRINTF(x)	if (ucycomdebug) printf x
#define DPRINTFN(n, x)	if (ucycomdebug > (n)) printf x
int	ucycomdebug = 20;
#else
#define DPRINTF(x)
#define DPRINTFN(n,x)
#endif


#define	UCYCOMCALLUNIT_MASK	TTCALLUNIT_MASK
#define	UCYCOMUNIT_MASK		TTUNIT_MASK
#define	UCYCOMDIALOUT_MASK	TTDIALOUT_MASK

#define	UCYCOMCALLUNIT(x)	TTCALLUNIT(x)
#define	UCYCOMUNIT(x)		TTUNIT(x)
#define	UCYCOMDIALOUT(x)	TTDIALOUT(x)

/* Configuration Byte */
#define UCYCOM_RESET		0x80
#define UCYCOM_PARITY_TYPE_MASK	0x20
#define  UCYCOM_PARITY_ODD	 0x20
#define  UCYCOM_PARITY_EVEN	 0x00
#define UCYCOM_PARITY_MASK	0x10
#define  UCYCOM_PARITY_ON	 0x10
#define  UCYCOM_PARITY_OFF	 0x00
#define UCYCOM_STOP_MASK	0x08
#define  UCYCOM_STOP_BITS_2	 0x08
#define  UCYCOM_STOP_BITS_1	 0x00
#define UCYCOM_DATA_MASK	0x03
#define  UCYCOM_DATA_BITS_8	 0x03
#define  UCYCOM_DATA_BITS_7	 0x02
#define  UCYCOM_DATA_BITS_6	 0x01
#define  UCYCOM_DATA_BITS_5	 0x00

/* Modem (Input) status byte */
#define UCYCOM_RI	0x80
#define UCYCOM_DCD	0x40
#define UCYCOM_DSR	0x20
#define UCYCOM_CTS	0x10
#define UCYCOM_ERROR	0x08
#define UCYCOM_LMASK	0x07

/* Modem (Output) control byte */
#define UCYCOM_DTR	0x20
#define UCYCOM_RTS	0x10
#define UCYCOM_ORESET	0x08

struct ucycom_softc {
	struct uhidev		sc_hdev;

	struct tty		*sc_tty;

	enum {
		UCYCOM_INIT_NONE,
		UCYCOM_INIT_INITED
	} sc_init_state;

	kmutex_t sc_lock;	/* protects refcnt, others */

	/* uhidev parameters */
	size_t			sc_flen; /* feature report length */
	size_t			sc_olen; /* output report length */

	uint8_t			*sc_obuf;
	int			sc_wlen;

	/* settings */
	uint32_t		sc_baud;
	uint8_t			sc_cfg;	/* Data format */
	uint8_t			sc_mcr;	/* Modem control */
	uint8_t			sc_msr;	/* Modem status */
	int			sc_swflags;

	/* flags */
	char			sc_dying;
};

dev_type_open(ucycomopen);
dev_type_close(ucycomclose);
dev_type_read(ucycomread);
dev_type_write(ucycomwrite);
dev_type_ioctl(ucycomioctl);
dev_type_stop(ucycomstop);
dev_type_tty(ucycomtty);
dev_type_poll(ucycompoll);

const struct cdevsw ucycom_cdevsw = {
	.d_open = ucycomopen,
	.d_close = ucycomclose,
	.d_read = ucycomread,
	.d_write = ucycomwrite,
	.d_ioctl = ucycomioctl,
	.d_stop = ucycomstop,
	.d_tty = ucycomtty,
	.d_poll = ucycompoll,
	.d_mmap = nommap,
	.d_kqfilter = ttykqfilter,
	.d_discard = nodiscard,
	.d_flag = D_TTY
};

Static int ucycomparam(struct tty *, struct termios *);
Static void ucycomstart(struct tty *);
Static void ucycomwritecb(struct usbd_xfer *, void *, usbd_status);
Static void ucycom_intr(struct uhidev *, void *, u_int);
Static int ucycom_configure(struct ucycom_softc *, uint32_t, uint8_t);
Static void tiocm_to_ucycom(struct ucycom_softc *, u_long, int);
Static int ucycom_to_tiocm(struct ucycom_softc *);
Static void ucycom_set_status(struct ucycom_softc *);
Static void ucycom_dtr(struct ucycom_softc *, int);
#if 0
Static void ucycom_rts(struct ucycom_softc *, int);
#endif
Static void ucycom_cleanup(struct ucycom_softc *);

#ifdef UCYCOM_DEBUG
Static void ucycom_get_cfg(struct ucycom_softc *);
#endif

Static const struct usb_devno ucycom_devs[] = {
	{ USB_VENDOR_CYPRESS, USB_PRODUCT_CYPRESS_USBRS232 },
	{ USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EARTHMATE },
};
#define ucycom_lookup(v, p) usb_lookup(ucycom_devs, v, p)

int	ucycom_match(device_t, cfdata_t, void *);
void	ucycom_attach(device_t, device_t, void *);
int	ucycom_detach(device_t, int);
int	ucycom_activate(device_t, enum devact);

CFATTACH_DECL_NEW(ucycom, sizeof(struct ucycom_softc), ucycom_match,
    ucycom_attach, ucycom_detach, ucycom_activate);

int
ucycom_match(device_t parent, cfdata_t match, void *aux)
{
	struct uhidev_attach_arg *uha = aux;

	return ucycom_lookup(uha->uiaa->uiaa_vendor, uha->uiaa->uiaa_product)
	    != NULL ? UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
}

void
ucycom_attach(device_t parent, device_t self, void *aux)
{
	struct ucycom_softc *sc = device_private(self);
	struct uhidev_attach_arg *uha = aux;
	int size, repid;
	void *desc;

	sc->sc_hdev.sc_dev = self;
	sc->sc_hdev.sc_intr = ucycom_intr;
	sc->sc_hdev.sc_parent = uha->parent;
	sc->sc_hdev.sc_report_id = uha->reportid;
	sc->sc_init_state = UCYCOM_INIT_NONE;

	uhidev_get_report_desc(uha->parent, &desc, &size);
	repid = uha->reportid;
	sc->sc_olen = hid_report_size(desc, size, hid_output, repid);
	sc->sc_flen = hid_report_size(desc, size, hid_feature, repid);

	if (sc->sc_olen != 8 && sc->sc_olen != 32)
		return;
	if (sc->sc_flen != 5)
		return;

	sc->sc_msr = sc->sc_mcr = 0;

	/* set up tty */
	sc->sc_tty = tty_alloc();
	sc->sc_tty->t_sc = sc;
	sc->sc_tty->t_oproc = ucycomstart;
	sc->sc_tty->t_param = ucycomparam;

	tty_attach(sc->sc_tty);

	/* Nothing interesting to report */
	aprint_normal("\n");

	sc->sc_init_state = UCYCOM_INIT_INITED;
}


int
ucycom_detach(device_t self, int flags)
{
	struct ucycom_softc *sc = device_private(self);
	struct tty *tp = sc->sc_tty;
	int maj, mn;
	int s;

	DPRINTF(("ucycom_detach: sc=%p flags=%d tp=%p\n", sc, flags, tp));

	sc->sc_dying = 1;

	s = splusb();
	if (tp != NULL) {
		mutex_spin_enter(&tty_lock);
		CLR(tp->t_state, TS_CARR_ON);
		CLR(tp->t_cflag, CLOCAL | MDMBUF);
		ttyflush(tp, FREAD|FWRITE);
		mutex_spin_exit(&tty_lock);
	}
	/* Wait for processes to go away. */
	usb_detach_waitold(sc->sc_hdev.sc_dev);
	splx(s);

	/* locate the major number */
	maj = cdevsw_lookup_major(&ucycom_cdevsw);

	/* Nuke the vnodes for any open instances. */
	mn = device_unit(self);

	DPRINTFN(2, ("ucycom_detach: maj=%d mn=%d\n", maj, mn));
	vdevgone(maj, mn, mn, VCHR);
	vdevgone(maj, mn | UCYCOMDIALOUT_MASK, mn | UCYCOMDIALOUT_MASK, VCHR);
	vdevgone(maj, mn | UCYCOMCALLUNIT_MASK, mn | UCYCOMCALLUNIT_MASK, VCHR);

	/* Detach and free the tty. */
	if (tp != NULL) {
		DPRINTF(("ucycom_detach: tty_detach %p\n", tp));
		tty_detach(tp);
		tty_free(tp);
		sc->sc_tty = NULL;
	}

	return 0;
}

int
ucycom_activate(device_t self, enum devact act)
{
	struct ucycom_softc *sc = device_private(self);

	DPRINTFN(5,("ucycom_activate: %d\n", act));

	switch (act) {
	case DVACT_DEACTIVATE:
		sc->sc_dying = 1;
		return 0;
	default:
		return EOPNOTSUPP;
	}
}

#if 0
void
ucycom_shutdown(struct ucycom_softc *sc)
{
	struct tty *tp = sc->sc_tty;

	DPRINTF(("ucycom_shutdown\n"));
	/*
	 * Hang up if necessary.  Wait a bit, so the other side has time to
	 * notice even if we immediately open the port again.
	 */
	if (ISSET(tp->t_cflag, HUPCL)) {
		ucycom_dtr(sc, 0);
		(void)tsleep(sc, TTIPRI, ttclos, hz);
	}
}
#endif

int
ucycomopen(dev_t dev, int flag, int mode, struct lwp *l)
{
	struct ucycom_softc *sc =
	    device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
	struct tty *tp;
	int s, err;

	DPRINTF(("ucycomopen: unit=%d\n", UCYCOMUNIT(dev)));
	DPRINTF(("ucycomopen: sc=%p\n", sc));

	if (sc == NULL)
		return ENXIO;
	if (sc->sc_dying)
		return EIO;
	if (sc->sc_init_state != UCYCOM_INIT_INITED)
		return ENXIO;
	if (!device_is_active(sc->sc_hdev.sc_dev))
		return ENXIO;

	tp = sc->sc_tty;

	DPRINTF(("ucycomopen: tp=%p\n", tp));

	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
		return EBUSY;

	s = spltty();

	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
		struct termios t;

		tp->t_dev = dev;

		err = uhidev_open(&sc->sc_hdev);
		if (err) {
			/* Any cleanup? */
			splx(s);
			return err;
		}

		/*
		 * Initialize the termios status to the defaults.  Add in the
		 * sticky bits from TIOCSFLAGS.
		 */
		t.c_ispeed = 0;
		t.c_ospeed = TTYDEF_SPEED;
		t.c_cflag = TTYDEF_CFLAG;
		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
			SET(t.c_cflag, CLOCAL);
		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
			SET(t.c_cflag, CRTSCTS);
		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
			SET(t.c_cflag, MDMBUF);

		tp->t_ospeed = 0;
		(void) ucycomparam(tp, &t);
		tp->t_iflag = TTYDEF_IFLAG;
		tp->t_oflag = TTYDEF_OFLAG;
		tp->t_lflag = TTYDEF_LFLAG;
		ttychars(tp);
		ttsetwater(tp);

		/* Allocate an output report buffer */
		sc->sc_obuf = kmem_alloc(sc->sc_olen, KM_SLEEP);

		DPRINTF(("ucycomopen: sc->sc_obuf=%p\n", sc->sc_obuf));

#if 0
		/* XXX Don't do this as for some reason trying to do an
		 * XXX interrupt out transfer at this point means everything
		 * XXX gets stuck!?!
		 */
		/*
		 * Turn on DTR.  We must always do this, even if carrier is not
		 * present, because otherwise we'd have to use TIOCSDTR
		 * immediately after setting CLOCAL, which applications do not
		 * expect.  We always assert DTR while the device is open
		 * unless explicitly requested to deassert it.
		 */
		ucycom_dtr(sc, 1);
#endif

#if 0
		/* XXX CLR(sc->sc_rx_flags, RX_ANY_BLOCK);*/
		ucycom_hwiflow(sc);
#endif

	}
	splx(s);

	err = ttyopen(tp, UCYCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
	if (err)
		goto bad;

	err = (*tp->t_linesw->l_open)(dev, tp);
	if (err)
		goto bad;

	return 0;

bad:
	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
		/*
		 * We failed to open the device, and nobody else had it opened.
		 * Clean up the state as appropriate.
		 */
		ucycom_cleanup(sc);
	}

	return err;

}


int
ucycomclose(dev_t dev, int flag, int mode, struct lwp *l)
{
	struct ucycom_softc *sc =
	    device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
	struct tty *tp = sc->sc_tty;

	DPRINTF(("ucycomclose: unit=%d\n", UCYCOMUNIT(dev)));
	if (!ISSET(tp->t_state, TS_ISOPEN))
		return 0;

	(*tp->t_linesw->l_close)(tp, flag);
	ttyclose(tp);

	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
		/*
		 * Although we got a last close, the device may still be in
		 * use; e.g. if this was the dialout node, and there are still
		 * processes waiting for carrier on the non-dialout node.
		 */
		ucycom_cleanup(sc);
	}

	return 0;
}

Static void
ucycomstart(struct tty *tp)
{
	struct ucycom_softc *sc =
	    device_lookup_private(&ucycom_cd, UCYCOMUNIT(tp->t_dev));
	usbd_status err __unused;
	u_char *data;
	int cnt, len, s;

	if (sc->sc_dying)
		return;

	s = spltty();
	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
		DPRINTFN(4,("ucycomstart: no go, state=0x%x\n", tp->t_state));
		goto out;
	}

#if 0
	/* HW FLOW CTL */
	if (sc->sc_tx_stopped)
		goto out;
#endif

	if (ttypull(tp) == 0)
		goto out;

	/* Grab the first contiguous region of buffer space. */
	data = tp->t_outq.c_cf;
	cnt = ndqb(&tp->t_outq, 0);

	if (cnt == 0) {
		DPRINTF(("ucycomstart: cnt == 0\n"));
		goto out;
	}

	SET(tp->t_state, TS_BUSY);

	/*
	 * The 8 byte output report uses byte 0 for control and byte
	 * count.
	 *
	 * The 32 byte output report uses byte 0 for control. Byte 1
	 * is used for byte count.
	 */
	memset(sc->sc_obuf, 0, sc->sc_olen);
	len = cnt;
	switch (sc->sc_olen) {
	case 8:
		if (cnt > sc->sc_olen - 1) {
			DPRINTF(("ucycomstart(8): big buffer %d chars\n", len));
			len = sc->sc_olen - 1;
		}

		memcpy(sc->sc_obuf + 1, data, len);
		sc->sc_obuf[0] = len | sc->sc_mcr;

		DPRINTF(("ucycomstart(8): sc->sc_obuf[0] = %d | %d = %d\n",
		    len, sc->sc_mcr, sc->sc_obuf[0]));
#ifdef UCYCOM_DEBUG
		if (ucycomdebug > 10) {
			uint32_t i;
			uint8_t *d = data;

			DPRINTF(("ucycomstart(8): data ="));
			for (i = 0; i < len; i++)
				DPRINTF((" %02x", d[i]));
			DPRINTF(("\n"));
		}
#endif
		break;

	case 32:
		if (cnt > sc->sc_olen - 2) {
			DPRINTF(("ucycomstart(32): big buffer %d chars\n",
			    len));
			len = sc->sc_olen - 2;
		}

		memcpy(sc->sc_obuf + 2, data, len);
		sc->sc_obuf[0] = sc->sc_mcr;
		sc->sc_obuf[1] = len;
		DPRINTF(("ucycomstart(32): sc->sc_obuf[0] = %d\n"
		    "sc->sc_obuf[1] = %d\n", sc->sc_obuf[0], sc->sc_obuf[1]));
#ifdef UCYCOM_DEBUG
		if (ucycomdebug > 10) {
			uint32_t i;
			uint8_t *d = data;

			DPRINTF(("ucycomstart(32): data ="));
			for (i = 0; i < len; i++)
				DPRINTF((" %02x", d[i]));
			DPRINTF(("\n"));
		}
#endif
		break;

	default:
		DPRINTFN(2,("ucycomstart: unknown output report size (%zd)\n",
		    sc->sc_olen));
		goto out;
	}
	splx(s);
	sc->sc_wlen = len;

#ifdef UCYCOM_DEBUG
	if (ucycomdebug > 5) {
		int i;

		if (len != 0) {
			DPRINTF(("ucycomstart: sc->sc_obuf[0..%zd) =",
			    sc->sc_olen));
			for (i = 0; i < sc->sc_olen; i++)
				DPRINTF((" %02x", sc->sc_obuf[i]));
			DPRINTF(("\n"));
		}
	}
#endif
	DPRINTFN(4,("ucycomstart: %d chars\n", len));
	usbd_setup_xfer(sc->sc_hdev.sc_parent->sc_oxfer, sc, sc->sc_obuf,
	    sc->sc_olen, 0, USBD_NO_TIMEOUT, ucycomwritecb);

	/* What can we do on error? */
	err = usbd_transfer(sc->sc_hdev.sc_parent->sc_oxfer);

#ifdef UCYCOM_DEBUG
	if (err != USBD_IN_PROGRESS)
		DPRINTF(("ucycomstart: err=%s\n", usbd_errstr(err)));
#endif
	return;

out:
	splx(s);
}

Static void
ucycomwritecb(struct usbd_xfer *xfer, void *p, usbd_status status)
{
	struct ucycom_softc *sc = (struct ucycom_softc *)p;
	struct tty *tp = sc->sc_tty;
	usbd_status stat;
	int len, s;

	if (status == USBD_CANCELLED || sc->sc_dying)
		goto error;

	if (status) {
		DPRINTF(("ucycomwritecb: status=%d\n", status));
		usbd_clear_endpoint_stall(sc->sc_hdev.sc_parent->sc_opipe);
		/* XXX we should restart after some delay. */
		goto error;
	}

	usbd_get_xfer_status(xfer, NULL, NULL, &len, &stat);

	if (status != USBD_NORMAL_COMPLETION) {
		DPRINTFN(4,("ucycomwritecb: status = %d\n", status));
		goto error;
	}

	DPRINTFN(4,("ucycomwritecb: did %d/%d chars\n", sc->sc_wlen, len));

	s = spltty();
	CLR(tp->t_state, TS_BUSY);
	if (ISSET(tp->t_state, TS_FLUSH))
		CLR(tp->t_state, TS_FLUSH);
	else
		ndflush(&tp->t_outq, sc->sc_wlen);
	(*tp->t_linesw->l_start)(tp);
	splx(s);
	return;

error:
	s = spltty();
	CLR(tp->t_state, TS_BUSY);
	splx(s);
}

Static int
ucycomparam(struct tty *tp, struct termios *t)
{
	struct ucycom_softc *sc = tp->t_sc;
	uint32_t baud;
	uint8_t cfg;
	int err;

	if (t->c_ospeed < 0) {
		DPRINTF(("ucycomparam: c_ospeed < 0\n"));
		return EINVAL;
	}

	/* Check requested parameters. */
	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
		return EINVAL;

	/*
	 * For the console, always force CLOCAL and !HUPCL, so that the port
	 * is always active.
	 */
	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
		SET(t->c_cflag, CLOCAL);
		CLR(t->c_cflag, HUPCL);
	}

	/*
	 * If there were no changes, don't do anything.  This avoids dropping
	 * input and improves performance when all we did was frob things like
	 * VMIN and VTIME.
	 */
	if (tp->t_ospeed == t->c_ospeed &&
	    tp->t_cflag == t->c_cflag)
		return 0;

	/* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */

	/* And copy to tty. */
	tp->t_ispeed = 0;
	tp->t_ospeed = t->c_ospeed;
	tp->t_cflag = t->c_cflag;

	baud = t->c_ispeed;
	DPRINTF(("ucycomparam: baud=%d\n", baud));

	if (t->c_cflag & CIGNORE) {
		cfg = sc->sc_cfg;
	} else {
		cfg = 0;
		switch (t->c_cflag & CSIZE) {
		case CS8:
			cfg |= UCYCOM_DATA_BITS_8;
			break;
		case CS7:
			cfg |= UCYCOM_DATA_BITS_7;
			break;
		case CS6:
			cfg |= UCYCOM_DATA_BITS_6;
			break;
		case CS5:
			cfg |= UCYCOM_DATA_BITS_5;
			break;
		default:
			return EINVAL;
		}
		cfg |= ISSET(t->c_cflag, CSTOPB) ?
		    UCYCOM_STOP_BITS_2 : UCYCOM_STOP_BITS_1;
		cfg |= ISSET(t->c_cflag, PARENB) ?
		    UCYCOM_PARITY_ON : UCYCOM_PARITY_OFF;
		cfg |= ISSET(t->c_cflag, PARODD) ?
		    UCYCOM_PARITY_ODD : UCYCOM_PARITY_EVEN;
	}

	/*
	 * Update the tty layer's idea of the carrier bit, in case we changed
	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
	 * explicit request.
	 */
	DPRINTF(("ucycomparam: l_modem\n"));
	(void) (*tp->t_linesw->l_modem)(tp, 1 /* XXX carrier */ );

	err = ucycom_configure(sc, baud, cfg);
	return err;
}

void
ucycomstop(struct tty *tp, int flag)
{
	DPRINTF(("ucycomstop: flag=%d\n", flag));
}

int
ucycomread(dev_t dev, struct uio *uio, int flag)
{
	struct ucycom_softc *sc =
	    device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
	struct tty *tp = sc->sc_tty;
	int err;

	DPRINTF(("ucycomread: sc=%p, tp=%p, uio=%p, flag=%d\n", sc, tp, uio,
	    flag));
	if (sc->sc_dying)
		return EIO;

	err = ((*tp->t_linesw->l_read)(tp, uio, flag));
	return err;
}


int
ucycomwrite(dev_t dev, struct uio *uio, int flag)
{
	struct ucycom_softc *sc =
	    device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
	struct tty *tp = sc->sc_tty;
	int err;

	DPRINTF(("ucycomwrite: sc=%p, tp=%p, uio=%p, flag=%d\n", sc, tp, uio,
	    flag));
	if (sc->sc_dying)
		return EIO;

	err = ((*tp->t_linesw->l_write)(tp, uio, flag));
	return err;
}

struct tty *
ucycomtty(dev_t dev)
{
	struct ucycom_softc *sc =
	    device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
	struct tty *tp = sc->sc_tty;

	DPRINTF(("ucycomtty: sc=%p, tp=%p\n", sc, tp));

	return tp;
}

int
ucycomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
	struct ucycom_softc *sc =
	    device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
	struct tty *tp = sc->sc_tty;
	int err;
	int s;

	if (sc->sc_dying)
		return EIO;

	DPRINTF(("ucycomioctl: sc=%p, tp=%p, data=%p\n", sc, tp, data));

	err = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
	if (err != EPASSTHROUGH)
		return err;

	err = ttioctl(tp, cmd, data, flag, l);
	if (err != EPASSTHROUGH)
		return err;

	err = 0;

	DPRINTF(("ucycomioctl: our cmd=0x%08lx\n", cmd));
	s = spltty();

	switch (cmd) {
/*	case TIOCSBRK:
		ucycom_break(sc, 1);
		break;

	case TIOCCBRK:
		ucycom_break(sc, 0);
		break;
*/
	case TIOCSDTR:
		ucycom_dtr(sc, 1);
		break;

	case TIOCCDTR:
		ucycom_dtr(sc, 0);
		break;

	case TIOCGFLAGS:
		*(int *)data = sc->sc_swflags;
		break;

	case TIOCSFLAGS:
		err = kauth_authorize_device_tty(l->l_cred,
		    KAUTH_DEVICE_TTY_PRIVSET, tp);
		if (err)
			break;
		sc->sc_swflags = *(int *)data;
		break;

	case TIOCMSET:
	case TIOCMBIS:
	case TIOCMBIC:
		tiocm_to_ucycom(sc, cmd, *(int *)data);
		break;

	case TIOCMGET:
		*(int *)data = ucycom_to_tiocm(sc);
		break;

	default:
		err = EPASSTHROUGH;
		break;
	}

	splx(s);

	return err;
}

int
ucycompoll(dev_t dev, int events, struct lwp *l)
{
	struct ucycom_softc *sc =
	    device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
	struct tty *tp = sc->sc_tty;
	int err;

	DPRINTF(("ucycompoll: sc=%p, tp=%p, events=%d, lwp=%p\n", sc, tp,
	    events, l));

	if (sc->sc_dying)
		return EIO;

	err = ((*tp->t_linesw->l_poll)(tp, events, l));
	return err;
}

Static int
ucycom_configure(struct ucycom_softc *sc, uint32_t baud, uint8_t cfg)
{
	uint8_t report[5];
	int err;

	switch (baud) {
	case 600:
	case 1200:
	case 2400:
	case 4800:
	case 9600:
	case 19200:
	case 38400:
	case 57600:
#if 0
	/*
	 * Stock chips only support standard baud rates in the 600 - 57600
	 * range, but higher rates can be achieved using custom firmware.
	 */
	case 115200:
	case 153600:
	case 192000:
#endif
		break;
	default:
		return EINVAL;
	}

	DPRINTF(("ucycom_configure: setting %d baud, %d-%c-%d (%d)\n", baud,
	    5 + (cfg & UCYCOM_DATA_MASK),
	    (cfg & UCYCOM_PARITY_MASK) ?
		((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N',
	    (cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg));

	report[0] = baud & 0xff;
	report[1] = (baud >> 8) & 0xff;
	report[2] = (baud >> 16) & 0xff;
	report[3] = (baud >> 24) & 0xff;
	report[4] = cfg;
	err = uhidev_set_report(&sc->sc_hdev, UHID_FEATURE_REPORT,
	    report, sc->sc_flen);
	if (err != 0) {
		DPRINTF(("%s\n", usbd_errstr(err)));
		return EIO;
	}
	sc->sc_baud = baud;
	sc->sc_cfg = cfg;

#ifdef UCYCOM_DEBUG
	ucycom_get_cfg(sc);
#endif

	return 0;
}

Static void
ucycom_intr(struct uhidev *addr, void *ibuf, u_int len)
{
	struct ucycom_softc *sc = (struct ucycom_softc *)addr;
	struct tty *tp = sc->sc_tty;
	int (*rint)(int , struct tty *) = tp->t_linesw->l_rint;
	uint8_t *cp = ibuf;
	int s, n, st, chg;

	/* We understand 8 byte and 32 byte input records */
	switch (len) {
	case 8:
		n = cp[0] & UCYCOM_LMASK;
		st = cp[0] & ~UCYCOM_LMASK;
		cp++;
		break;

	case 32:
		st = cp[0];
		n = cp[1];
		cp += 2;
		break;

	default:
		DPRINTFN(3,("ucycom_intr: Unknown input report length\n"));
		return;
	}

#ifdef UCYCOM_DEBUG
	if (ucycomdebug > 5) {
		uint32_t i;

		if (n != 0) {
			DPRINTF(("ucycom_intr: ibuf[0..%d) =", n));
			for (i = 0; i < n; i++)
				DPRINTF((" %02x", cp[i]));
			DPRINTF(("\n"));
		}
	}
#endif

	/* Give characters to tty layer. */
	s = spltty();
	while (n-- > 0) {
		DPRINTFN(7,("ucycom_intr: char=0x%02x\n", *cp));
		if ((*rint)(*cp++, tp) == -1) {
			/* XXX what should we do? */
			aprint_error_dev(sc->sc_hdev.sc_dev,
			    "lost a character\n");
			break;
		}
	}
	splx(s);
	chg = st ^ sc->sc_msr;
	sc->sc_msr = st;
	if (ISSET(chg, UCYCOM_DCD))
		(*tp->t_linesw->l_modem)(tp,
		    ISSET(sc->sc_msr, UCYCOM_DCD));

}

Static void
tiocm_to_ucycom(struct ucycom_softc *sc, u_long how, int ttybits)
{
	u_char combits;
	u_char before = sc->sc_mcr;

	combits = 0;
	if (ISSET(ttybits, TIOCM_DTR))
		SET(combits, UCYCOM_DTR);
	if (ISSET(ttybits, TIOCM_RTS))
		SET(combits, UCYCOM_RTS);

	switch (how) {
	case TIOCMBIC:
		CLR(sc->sc_mcr, combits);
		break;

	case TIOCMBIS:
		SET(sc->sc_mcr, combits);
		break;

	case TIOCMSET:
		CLR(sc->sc_mcr, UCYCOM_DTR | UCYCOM_RTS);
		SET(sc->sc_mcr, combits);
		break;
	}
	if (before ^ sc->sc_mcr) {
		DPRINTF(("tiocm_to_ucycom: something has changed\n"));
		ucycom_set_status(sc);
	}
}

Static int
ucycom_to_tiocm(struct ucycom_softc *sc)
{
	u_char combits;
	int ttybits = 0;

	combits = sc->sc_mcr;
	if (ISSET(combits, UCYCOM_DTR))
		SET(ttybits, TIOCM_DTR);
	if (ISSET(combits, UCYCOM_RTS))
		SET(ttybits, TIOCM_RTS);

	combits = sc->sc_msr;
	if (ISSET(combits, UCYCOM_DCD))
		SET(ttybits, TIOCM_CD);
	if (ISSET(combits, UCYCOM_CTS))
		SET(ttybits, TIOCM_CTS);
	if (ISSET(combits, UCYCOM_DSR))
		SET(ttybits, TIOCM_DSR);
	if (ISSET(combits, UCYCOM_RI))
		SET(ttybits, TIOCM_RI);

	return ttybits;
}

Static void
ucycom_dtr(struct ucycom_softc *sc, int set)
{
	uint8_t old;

	old = sc->sc_mcr;
	if (set)
		SET(sc->sc_mcr, UCYCOM_DTR);
	else
		CLR(sc->sc_mcr, UCYCOM_DTR);

	if (old ^ sc->sc_mcr)
		ucycom_set_status(sc);
}

#if 0
Static void
ucycom_rts(struct ucycom_softc *sc, int set)
{
	uint8_t old;

	old = sc->sc_msr;
	if (set)
		SET(sc->sc_mcr, UCYCOM_RTS);
	else
		CLR(sc->sc_mcr, UCYCOM_RTS);

	if (old ^ sc->sc_mcr)
		ucycom_set_status(sc);
}
#endif

Static void
ucycom_set_status(struct ucycom_softc *sc)
{
	int err;

	if (sc->sc_olen != 8 && sc->sc_olen != 32) {
		DPRINTFN(2,("ucycom_set_status: unknown output report "
		    "size (%zd)\n", sc->sc_olen));
		return;
	}

	DPRINTF(("ucycom_set_status: %d\n", sc->sc_mcr));

	memset(sc->sc_obuf, 0, sc->sc_olen);
	sc->sc_obuf[0] = sc->sc_mcr;

	err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf, sc->sc_olen);
	if (err) {
		DPRINTF(("ucycom_set_status: err=%d\n", err));
	}
}

#ifdef UCYCOM_DEBUG
Static void
ucycom_get_cfg(struct ucycom_softc *sc)
{
	int err, cfg, baud;
	uint8_t report[5];

	err = uhidev_get_report(&sc->sc_hdev, UHID_FEATURE_REPORT,
	    report, sc->sc_flen);
	if (err) {
		DPRINTF(("%s: failed\n", __func__));
		return;
	}
	cfg = report[4];
	baud = (report[3] << 24) + (report[2] << 16) + (report[1] << 8) +
	    report[0];
	DPRINTF(("ucycom_get_cfg: device reports %d baud, %d-%c-%d (%d)\n",
	    baud, 5 + (cfg & UCYCOM_DATA_MASK),
	    (cfg & UCYCOM_PARITY_MASK) ?
		((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N',
	    (cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg));
}
#endif

Static void
ucycom_cleanup(struct ucycom_softc *sc)
{
	uint8_t	*obuf;

	DPRINTF(("ucycom_cleanup: closing uhidev\n"));

	obuf = sc->sc_obuf;
	sc->sc_obuf = NULL;
	uhidev_close(&sc->sc_hdev);

	if (obuf != NULL)
		kmem_free(obuf, sc->sc_olen);
}