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
/* $NetBSD: umcs.c,v 1.13 2019/05/09 02:43:35 mrg Exp $ */
/* $FreeBSD: head/sys/dev/usb/serial/umcs.c 260559 2014-01-12 11:44:28Z hselasky $ */

/*-
 * Copyright (c) 2010 Lev Serebryakov <lev@FreeBSD.org>.
 * All rights reserved.
 *
 * 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 AUTHOR 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 AUTHOR 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 driver supports several multiport USB-to-RS232 serial adapters driven
 * by MosChip mos7820 and mos7840, bridge chips.
 * The adapters are sold under many different brand names.
 *
 * Datasheets are available at MosChip www site at
 * http://www.moschip.com.  The datasheets don't contain full
 * programming information for the chip.
 *
 * It is nornal to have only two enabled ports in devices, based on
 * quad-port mos7840.
 *
 */
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: umcs.c,v 1.13 2019/05/09 02:43:35 mrg Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/atomic.h>
#include <sys/kernel.h>
#include <sys/conf.h>
#include <sys/tty.h>
#include <sys/device.h>
#include <sys/kmem.h>

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

#include <dev/usb/usbdevs.h>
#include <dev/usb/ucomvar.h>

#include "umcs.h"

#if 0
#define	DPRINTF(ARG)	printf ARG
#else
#define	DPRINTF(ARG)
#endif

/*
 * Two-port devices (both with 7820 chip and 7840 chip configured as two-port)
 * have ports 0 and 2, with ports 1 and 3 omitted.
 * So, PHYSICAL port numbers on two-port device will be 0 and 2.
 *
 * We use an array of the following struct, indexed by ucom port index,
 * and include the physical port number in it.
 */
struct umcs7840_softc_oneport {
	device_t sc_port_ucom;		/* ucom subdevice */
	unsigned int sc_port_phys;	/* physical port number */
	uint8_t	sc_port_lcr;		/* local line control register */
	uint8_t	sc_port_mcr;		/* local modem control register */
};

struct umcs7840_softc {
	device_t sc_dev;		/* ourself */
	struct usbd_interface *sc_iface; /* the usb interface */
	struct usbd_device *sc_udev;	/* the usb device */
	struct usbd_pipe *sc_intr_pipe;	/* interrupt pipe */
	uint8_t *sc_intr_buf;		/* buffer for interrupt xfer */
	unsigned int sc_intr_buflen;	/* size of buffer */
	struct usb_task sc_change_task;	/* async status changes */
	volatile uint32_t sc_change_mask;	/* mask of port changes */
	struct umcs7840_softc_oneport sc_ports[UMCS7840_MAX_PORTS];
					/* data for each port */
	uint8_t	sc_numports;		/* number of ports (subunits) */
	bool sc_init_done;		/* special one time init in open */
	bool sc_dying;			/* we have been deactivated */
};

static int umcs7840_get_reg(struct umcs7840_softc *, uint8_t, uint8_t *);
static int umcs7840_set_reg(struct umcs7840_softc *, uint8_t, uint8_t);
static int umcs7840_get_UART_reg(struct umcs7840_softc *, uint8_t, uint8_t, uint8_t *);
static int umcs7840_set_UART_reg(struct umcs7840_softc *, uint8_t, uint8_t, uint8_t );
static int umcs7840_calc_baudrate(uint32_t, uint16_t *, uint8_t *);
static void umcs7840_dtr(struct umcs7840_softc *, int, bool);
static void umcs7840_rts(struct umcs7840_softc *, int, bool);
static void umcs7840_break(struct umcs7840_softc *, int, bool );

static int umcs7840_match(device_t, cfdata_t, void *);
static void umcs7840_attach(device_t, device_t, void *);
static int umcs7840_detach(device_t, int);
static void umcs7840_intr(struct usbd_xfer *, void *, usbd_status);
static void umcs7840_change_task(void *arg);
static void umcs7840_childdet(device_t, device_t);

static void umcs7840_get_status(void *, int, u_char *, u_char *);
static void umcs7840_set(void *, int, int, int);
static int umcs7840_param(void *, int, struct termios *);
static int umcs7840_port_open(void *, int);
static void umcs7840_port_close(void *, int);

struct ucom_methods umcs7840_methods = {
	.ucom_get_status = umcs7840_get_status,
	.ucom_set = umcs7840_set,
	.ucom_param = umcs7840_param,
	.ucom_open = umcs7840_port_open,
	.ucom_close = umcs7840_port_close,
};

static const struct usb_devno umcs7840_devs[] = {
	{ USB_VENDOR_MOSCHIP,		USB_PRODUCT_MOSCHIP_MCS7703 },
	{ USB_VENDOR_MOSCHIP,		USB_PRODUCT_MOSCHIP_MCS7810 },
	{ USB_VENDOR_MOSCHIP,		USB_PRODUCT_MOSCHIP_MCS7820 },
	{ USB_VENDOR_MOSCHIP,		USB_PRODUCT_MOSCHIP_MCS7840 },
	{ USB_VENDOR_ATEN,		USB_PRODUCT_ATEN_UC2324 }
};
#define umcs7840_lookup(v, p) usb_lookup(umcs7840_devs, v, p)

CFATTACH_DECL2_NEW(umcs, sizeof(struct umcs7840_softc), umcs7840_match,
    umcs7840_attach, umcs7840_detach, NULL, NULL,
    umcs7840_childdet);

static inline int
umcs7840_reg_sp(int phyport)
{
	KASSERT(phyport >= 0 && phyport < 4);
	switch (phyport) {
	default:
	case 0:	return MCS7840_DEV_REG_SP1;
	case 1:	return MCS7840_DEV_REG_SP2;
	case 2:	return MCS7840_DEV_REG_SP3;
	case 3:	return MCS7840_DEV_REG_SP4;
	}
}

static inline int
umcs7840_reg_ctrl(int phyport)
{
	KASSERT(phyport >= 0 && phyport < 4);
	switch (phyport) {
	default:
	case 0:	return MCS7840_DEV_REG_CONTROL1;
	case 1:	return MCS7840_DEV_REG_CONTROL2;
	case 2:	return MCS7840_DEV_REG_CONTROL3;
	case 3:	return MCS7840_DEV_REG_CONTROL4;
	}
}

static int
umcs7840_match(device_t dev, cfdata_t match, void *aux)
{
	struct usb_attach_arg *uaa = aux;

	return umcs7840_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
}

static void
umcs7840_attach(device_t parent, device_t self, void *aux)
{
	struct umcs7840_softc *sc = device_private(self);
	struct usb_attach_arg *uaa = aux;
	struct usbd_device *dev = uaa->uaa_device;
	usb_interface_descriptor_t *id;
	usb_endpoint_descriptor_t *ed;
	char *devinfop;
	struct ucom_attach_args ucaa;
	int error, i, intr_addr;
	uint8_t data;

	sc->sc_dev = self;
	sc->sc_udev = uaa->uaa_device;
	sc->sc_dying = false;

	if (usbd_set_config_index(sc->sc_udev, MCS7840_CONFIG_INDEX, 1) != 0) {
		aprint_error(": could not set configuration no\n");
		sc->sc_dying = true;
		return;
	}

	/* get the first interface handle */
	error = usbd_device2interface_handle(sc->sc_udev, MCS7840_IFACE_INDEX,
	    &sc->sc_iface);
	if (error != 0) {
		aprint_error(": could not get interface handle\n");
		sc->sc_dying = true;
		return;
	}

	/*
	 * Get number of ports
	 * Documentation (full datasheet) says, that number of ports is
	 * set as MCS7840_DEV_MODE_SELECT24S bit in MODE R/Only
	 * register. But vendor driver uses these undocumented
	 * register & bit.
	 *
	 * Experiments show, that MODE register can have `0'
	 * (4 ports) bit on 2-port device, so use vendor driver's way.
	 *
	 * Also, see notes in header file for these constants.
	 */
	umcs7840_get_reg(sc, MCS7840_DEV_REG_GPIO, &data);
	if (data & MCS7840_DEV_GPIO_4PORTS) {
		sc->sc_numports = 4;
		/* physical port no are : 0, 1, 2, 3 */
	} else {
		if (uaa->uaa_product == USB_PRODUCT_MOSCHIP_MCS7810)
			sc->sc_numports = 1;
		else {
			sc->sc_numports = 2;
			/* physical port no are : 0 and 2 */
		}
	}
	devinfop = usbd_devinfo_alloc(dev, 0);
	aprint_normal(": %s\n", devinfop);
	usbd_devinfo_free(devinfop);
	aprint_verbose_dev(self, "found %d active ports\n", sc->sc_numports);

	if (!umcs7840_get_reg(sc, MCS7840_DEV_REG_MODE, &data)) {
		aprint_verbose_dev(self, "On-die confguration: RST: active %s, "
		    "HRD: %s, PLL: %s, POR: %s, Ports: %s, EEPROM write %s, "
		    "IrDA is %savailable\n",
		    (data & MCS7840_DEV_MODE_RESET) ? "low" : "high",
		    (data & MCS7840_DEV_MODE_SER_PRSNT) ? "yes" : "no",
		    (data & MCS7840_DEV_MODE_PLLBYPASS) ? "bypassed" : "avail",
		    (data & MCS7840_DEV_MODE_PORBYPASS) ? "bypassed" : "avail",
		    (data & MCS7840_DEV_MODE_SELECT24S) ? "2" : "4",
		    (data & MCS7840_DEV_MODE_EEPROMWR) ? "enabled" : "disabled",
		    (data & MCS7840_DEV_MODE_IRDA) ? "" : "not ");
	}

	/*
	 * Set up the interrupt pipe
	 */
	id = usbd_get_interface_descriptor(sc->sc_iface);
	intr_addr = -1;
	for (i = 0 ; i < id->bNumEndpoints ; i++) {
		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
		if (ed == NULL) continue;
		if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN
		    || UE_GET_XFERTYPE(ed->bmAttributes) != UE_INTERRUPT)
			continue;
		sc->sc_intr_buflen = UGETW(ed->wMaxPacketSize);
		intr_addr = ed->bEndpointAddress;
		break;
	}
	if (intr_addr < 0) {
		aprint_error_dev(self, "interrupt pipe not found\n");
		sc->sc_dying = true;
		return;
	}
	sc->sc_intr_buf = kmem_alloc(sc->sc_intr_buflen, KM_SLEEP);

	error = usbd_open_pipe_intr(sc->sc_iface, intr_addr,
		    USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_intr_buf,
		    sc->sc_intr_buflen, umcs7840_intr, 100);
	if (error) {
		aprint_error_dev(self, "cannot open interrupt pipe "
		    "(addr %d)\n", intr_addr);
		sc->sc_dying = true;
		return;
	}

	usb_init_task(&sc->sc_change_task, umcs7840_change_task, sc,
	    USB_TASKQ_MPSAFE);

	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);

	memset(&ucaa, 0, sizeof(ucaa));
	ucaa.ucaa_ibufsize = 256;
	ucaa.ucaa_obufsize = 256;
	ucaa.ucaa_ibufsizepad = 256;
	ucaa.ucaa_opkthdrlen = 0;
	ucaa.ucaa_device = sc->sc_udev;
	ucaa.ucaa_iface = sc->sc_iface;
	ucaa.ucaa_methods = &umcs7840_methods;
	ucaa.ucaa_arg = sc;

	for (i = 0; i < sc->sc_numports; i++) {
		ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;

		/*
		 * On four port cards, endpoints are 0/1 for first,
		 * 2/3 for second, ...
		 * On two port cards, they are 0/1 for first, 4/5 for second.
		 * On single port, just 0/1 will be used.
		 */
		int phyport = i * (sc->sc_numports == 2 ? 2 : 1);

		ed = usbd_interface2endpoint_descriptor(sc->sc_iface,
			phyport*2);
		if (ed == NULL) {
			aprint_error_dev(self,
			    "no bulk in endpoint found for %d\n", i);
			sc->sc_dying = true;
			return;
		}
		ucaa.ucaa_bulkin = ed->bEndpointAddress;

		ed = usbd_interface2endpoint_descriptor(sc->sc_iface,
			phyport*2 + 1);
		if (ed == NULL) {
			aprint_error_dev(self,
			    "no bulk out endpoint found for %d\n", i);
			return;
		}
		ucaa.ucaa_bulkout = ed->bEndpointAddress;
		ucaa.ucaa_portno = i;
		DPRINTF(("port %d physical port %d bulk-in %d bulk-out %d\n",
		    i, phyport, ucaa.ucaa_bulkin, ucaa.ucaa_bulkout));

		sc->sc_ports[i].sc_port_phys = phyport;
		sc->sc_ports[i].sc_port_ucom =
		    config_found_sm_loc(self, "ucombus", NULL, &ucaa,
					    ucomprint, ucomsubmatch);
	}
}

static int
umcs7840_get_reg(struct umcs7840_softc *sc, uint8_t reg, uint8_t *data)
{
	usb_device_request_t req;
	int err;

	req.bmRequestType = UT_READ_VENDOR_DEVICE;
	req.bRequest = MCS7840_RDREQ;
	USETW(req.wValue, 0);
	USETW(req.wIndex, reg);
	USETW(req.wLength, UMCS7840_READ_LENGTH);

	err = usbd_do_request(sc->sc_udev, &req, data);
	if (err)
		aprint_normal_dev(sc->sc_dev,
		    "Reading register %d failed: %s\n", reg, usbd_errstr(err));
	return err;
}

static int
umcs7840_set_reg(struct umcs7840_softc *sc, uint8_t reg, uint8_t data)
{
	usb_device_request_t req;
	int err;

	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
	req.bRequest = MCS7840_WRREQ;
	USETW(req.wValue, data);
	USETW(req.wIndex, reg);
	USETW(req.wLength, 0);

	err = usbd_do_request(sc->sc_udev, &req, 0);
	if (err)
		aprint_normal_dev(sc->sc_dev, "Writing register %d failed: %s\n", reg, usbd_errstr(err));

	return err;
}

static int
umcs7840_get_UART_reg(struct umcs7840_softc *sc, uint8_t portno,
	uint8_t reg, uint8_t *data)
{
	usb_device_request_t req;
	uint16_t wVal;
	int err;

	/* portno is port number */
	wVal = ((uint16_t)(portno + 1)) << 8;

	req.bmRequestType = UT_READ_VENDOR_DEVICE;
	req.bRequest = MCS7840_RDREQ;
	USETW(req.wValue, wVal);
	USETW(req.wIndex, reg);
	USETW(req.wLength, UMCS7840_READ_LENGTH);

	err = usbd_do_request(sc->sc_udev, &req, data);
	if (err)
		aprint_normal_dev(sc->sc_dev, "Reading UART %d register %d failed: %s\n", portno, reg, usbd_errstr(err));
	return err;
}

static int
umcs7840_set_UART_reg(struct umcs7840_softc *sc, uint8_t portno, uint8_t reg, uint8_t data)
{
	usb_device_request_t req;
	int err;
	uint16_t wVal;

	/* portno is the physical port number */
	wVal = ((uint16_t)(portno + 1)) << 8 | data;

	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
	req.bRequest = MCS7840_WRREQ;
	USETW(req.wValue, wVal);
	USETW(req.wIndex, reg);
	USETW(req.wLength, 0);

	err = usbd_do_request(sc->sc_udev, &req, NULL);
	if (err)
		aprint_error_dev(sc->sc_dev,
		    "Writing UART %d register %d failed: %s\n",
		    portno, reg, usbd_errstr(err));
	return err;
}

static int
umcs7840_set_baudrate(struct umcs7840_softc *sc, uint8_t portno,
	uint32_t rate)
{
	int err;
	uint16_t divisor;
	uint8_t clk;
	uint8_t data;
	uint8_t physport = sc->sc_ports[portno].sc_port_phys;
	int spreg = umcs7840_reg_sp(physport);

	if (umcs7840_calc_baudrate(rate, &divisor, &clk)) {
		DPRINTF(("Port %d bad speed: %d\n", portno, rate));
		return -1;
	}
	if (divisor == 0 || (clk & MCS7840_DEV_SPx_CLOCK_MASK) != clk) {
		DPRINTF(("Port %d bad speed calculation: %d\n", portno,
		    rate));
		return -1;
	}
	DPRINTF(("Port %d set speed: %d (%02x / %d)\n", portno, rate, clk, divisor));

	/* Set clock source for standard BAUD frequences */
	err = umcs7840_get_reg(sc, spreg, &data);
	if (err)
		return err;
	data &= MCS7840_DEV_SPx_CLOCK_MASK;
	data |= clk;
	err = umcs7840_set_reg(sc, spreg, data);
	if (err)
		return err;

	/* Set divider */
	sc->sc_ports[portno].sc_port_lcr |= MCS7840_UART_LCR_DIVISORS;
	err = umcs7840_set_UART_reg(sc, physport, MCS7840_UART_REG_LCR, sc->sc_ports[portno].sc_port_lcr);
	if (err)
		return err;

	err = umcs7840_set_UART_reg(sc, physport, MCS7840_UART_REG_DLL, (uint8_t)(divisor & 0xff));
	if (err)
		return err;
	err = umcs7840_set_UART_reg(sc, physport, MCS7840_UART_REG_DLM, (uint8_t)((divisor >> 8) & 0xff));
	if (err)
		return err;

	/* Turn off access to DLL/DLM registers of UART */
	sc->sc_ports[portno].sc_port_lcr &= ~MCS7840_UART_LCR_DIVISORS;
	err = umcs7840_set_UART_reg(sc, physport, MCS7840_UART_REG_LCR, sc->sc_ports[portno].sc_port_lcr);
	if (err)
		return err;
	return 0;
}

static int
umcs7840_calc_baudrate(uint32_t rate, uint16_t *divisor, uint8_t *clk)
{
	/* Maximum speeds for standard frequences, when PLL is not used */
	static const uint32_t umcs7840_baudrate_divisors[] =
	    {0, 115200, 230400, 403200, 460800, 806400, 921600,
	     1572864, 3145728,};
	static const uint8_t umcs7840_baudrate_divisors_len =
	     __arraycount(umcs7840_baudrate_divisors);
	uint8_t i = 0;

	if (rate > umcs7840_baudrate_divisors[umcs7840_baudrate_divisors_len - 1])
		return -1;

	for (i = 0; i < umcs7840_baudrate_divisors_len - 1
	     && !(rate > umcs7840_baudrate_divisors[i]
	     && rate <= umcs7840_baudrate_divisors[i + 1]); ++i);
	*divisor = umcs7840_baudrate_divisors[i + 1] / rate;
	/* 0x00 .. 0x70 */
	*clk = i << MCS7840_DEV_SPx_CLOCK_SHIFT;
	return 0;
}

static int
umcs7840_detach(device_t self, int flags)
{
	struct umcs7840_softc *sc = device_private(self);
	int rv = 0, i;

	sc->sc_dying = true;

	/* close interrupt pipe */
	if (sc->sc_intr_pipe != NULL) {
		usbd_abort_pipe(sc->sc_intr_pipe);
		usbd_close_pipe(sc->sc_intr_pipe);
		sc->sc_intr_pipe = NULL;
	}
	if (sc->sc_intr_buf != NULL) {
		kmem_free(sc->sc_intr_buf, sc->sc_intr_buflen);
		sc->sc_intr_buf = NULL;
	}
	usb_rem_task_wait(sc->sc_udev, &sc->sc_change_task, USB_TASKQ_DRIVER,
	    NULL);

	/* detach children */
	for (i = 0; i < sc->sc_numports; i++) {
		if (sc->sc_ports[i].sc_port_ucom) {
			rv |= config_detach(sc->sc_ports[i].sc_port_ucom,
			    flags);
			sc->sc_ports[i].sc_port_ucom = NULL;
		}
	}

	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);

	return rv;
}

static void
umcs7840_childdet(device_t self, device_t child)
{
	struct umcs7840_softc *sc = device_private(self);
	int i;

	for (i = 0; i < sc->sc_numports; i++) {
		if (child == sc->sc_ports[i].sc_port_ucom) {
			sc->sc_ports[i].sc_port_ucom = NULL;
			return;
		}
	}
}

static void
umcs7840_get_status(void *self, int portno, u_char *lsr, u_char *msr)
{
	struct umcs7840_softc *sc = self;
	uint8_t pn = sc->sc_ports[portno].sc_port_phys;
	uint8_t	hw_lsr = 0;	/* local line status register */
	uint8_t	hw_msr = 0;	/* local modem status register */

	if (sc->sc_dying)
		return;

	/* Read LSR & MSR */
	umcs7840_get_UART_reg(sc, pn, MCS7840_UART_REG_LSR, &hw_lsr);
	umcs7840_get_UART_reg(sc, pn, MCS7840_UART_REG_MSR, &hw_msr);

	*lsr = hw_lsr;
	*msr = hw_msr;
}

static void
umcs7840_set(void *self, int portno, int reg, int onoff)
{
	struct umcs7840_softc *sc = self;

	if (sc->sc_dying)
		return;

	switch (reg) {
	case UCOM_SET_DTR:
		umcs7840_dtr(sc, portno, onoff);
		break;
	case UCOM_SET_RTS:
		umcs7840_rts(sc, portno, onoff);
		break;
	case UCOM_SET_BREAK:
		umcs7840_break(sc, portno, onoff);
		break;
	default:
		break;
	}
}

static int
umcs7840_param(void *self, int portno, struct termios *t)
{
	struct umcs7840_softc *sc = self;
	int pn = sc->sc_ports[portno].sc_port_phys;
	uint8_t lcr = sc->sc_ports[portno].sc_port_lcr;
	uint8_t mcr = sc->sc_ports[portno].sc_port_mcr;

	if (sc->sc_dying)
		return EIO;

	if (t->c_cflag & CSTOPB) {
		lcr |= MCS7840_UART_LCR_STOPB2;
	} else {
		lcr |= MCS7840_UART_LCR_STOPB1;
	}

	lcr &= ~MCS7840_UART_LCR_PARITYMASK;
	if (t->c_cflag & PARENB) {
		lcr |= MCS7840_UART_LCR_PARITYON;
		if (t->c_cflag & PARODD) {
			lcr = MCS7840_UART_LCR_PARITYODD;
		} else {
			lcr = MCS7840_UART_LCR_PARITYEVEN;
		}
	} else {
		lcr &= ~MCS7840_UART_LCR_PARITYON;
	}

	lcr &= ~MCS7840_UART_LCR_DATALENMASK;
	switch (t->c_cflag & CSIZE) {
	case CS5:
		lcr |= MCS7840_UART_LCR_DATALEN5;
		break;
	case CS6:
		lcr |= MCS7840_UART_LCR_DATALEN6;
		break;
	case CS7:
		lcr |= MCS7840_UART_LCR_DATALEN7;
		break;
	case CS8:
		lcr |= MCS7840_UART_LCR_DATALEN8;
		break;
	}

	if (t->c_cflag & CRTSCTS)
		mcr |= MCS7840_UART_MCR_CTSRTS;
	else
		mcr &= ~MCS7840_UART_MCR_CTSRTS;

	if (t->c_cflag & CLOCAL)
		mcr &= ~MCS7840_UART_MCR_DTRDSR;
	else
		mcr |= MCS7840_UART_MCR_DTRDSR;

	sc->sc_ports[portno].sc_port_lcr = lcr;
	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
	    sc->sc_ports[pn].sc_port_lcr);

	sc->sc_ports[portno].sc_port_mcr = mcr;
	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
	    sc->sc_ports[pn].sc_port_mcr);

	if (umcs7840_set_baudrate(sc, portno, t->c_ospeed))
		return EIO;

	return 0;
}

static void
umcs7840_dtr(struct umcs7840_softc *sc, int portno, bool onoff)
{
	int pn = sc->sc_ports[portno].sc_port_phys;
	uint8_t mcr = sc->sc_ports[portno].sc_port_mcr;

	if (onoff)
		mcr |= MCS7840_UART_MCR_DTR;
	else
		mcr &= ~MCS7840_UART_MCR_DTR;

	sc->sc_ports[portno].sc_port_mcr = mcr;
	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
	    sc->sc_ports[pn].sc_port_mcr);
}

static void
umcs7840_rts(struct umcs7840_softc *sc, int portno, bool onoff)
{
	int pn = sc->sc_ports[portno].sc_port_phys;
	uint8_t mcr = sc->sc_ports[portno].sc_port_mcr;

	if (onoff)
		mcr |= MCS7840_UART_MCR_RTS;
	else
		mcr &= ~MCS7840_UART_MCR_RTS;

	sc->sc_ports[portno].sc_port_mcr = mcr;
	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
	    sc->sc_ports[pn].sc_port_mcr);
}

static void
umcs7840_break(struct umcs7840_softc *sc, int portno, bool onoff)
{
	int pn = sc->sc_ports[portno].sc_port_phys;
	uint8_t lcr = sc->sc_ports[portno].sc_port_lcr;

	if (onoff)
		lcr |= MCS7840_UART_LCR_BREAK;
	else
		lcr &= ~MCS7840_UART_LCR_BREAK;

	sc->sc_ports[portno].sc_port_lcr = lcr;
	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
	    sc->sc_ports[pn].sc_port_lcr);
}

static int
umcs7840_port_open(void *self, int portno)
{
	struct umcs7840_softc *sc = self;
	int pn = sc->sc_ports[portno].sc_port_phys;
	int spreg = umcs7840_reg_sp(pn);
	int ctrlreg = umcs7840_reg_ctrl(pn);
	uint8_t data;

	if (sc->sc_dying)
		return EIO;

	/* If it very first open, finish global configuration */
	if (!sc->sc_init_done) {
		if (umcs7840_get_reg(sc, MCS7840_DEV_REG_CONTROL1, &data))
			return EIO;
		data |= MCS7840_DEV_CONTROL1_DRIVER_DONE;
		if (umcs7840_set_reg(sc, MCS7840_DEV_REG_CONTROL1, data))
			return EIO;
		sc->sc_init_done = 1;
	}

	/* Toggle reset bit on-off */
	if (umcs7840_get_reg(sc, spreg, &data))
		return EIO;
	data |= MCS7840_DEV_SPx_UART_RESET;
	if (umcs7840_set_reg(sc, spreg, data))
		return EIO;
	data &= ~MCS7840_DEV_SPx_UART_RESET;
	if (umcs7840_set_reg(sc, spreg, data))
		return EIO;

	/* Set RS-232 mode */
	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_SCRATCHPAD,
	    MCS7840_UART_SCRATCHPAD_RS232))
		return EIO;

	/* Disable RX on time of initialization */
	if (umcs7840_get_reg(sc, ctrlreg, &data))
		return EIO;
	data |= MCS7840_DEV_CONTROLx_RX_DISABLE;
	if (umcs7840_set_reg(sc, ctrlreg, data))
		return EIO;

	/* Disable all interrupts */
	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_IER, 0))
		return EIO;

	/* Reset FIFO -- documented */
	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_FCR, 0))
		return EIO;
	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_FCR,
	    MCS7840_UART_FCR_ENABLE | MCS7840_UART_FCR_FLUSHRHR |
	    MCS7840_UART_FCR_FLUSHTHR | MCS7840_UART_FCR_RTL_1_14))
		return EIO;

	/* Set 8 bit, no parity, 1 stop bit -- documented */
	sc->sc_ports[pn].sc_port_lcr =
	    MCS7840_UART_LCR_DATALEN8 | MCS7840_UART_LCR_STOPB1;
	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
	    sc->sc_ports[pn].sc_port_lcr))
		return EIO;

	/*
	 * Enable DTR/RTS on modem control, enable modem interrupts --
	 * documented
	 */
	sc->sc_ports[pn].sc_port_mcr = MCS7840_UART_MCR_DTR
	    | MCS7840_UART_MCR_RTS | MCS7840_UART_MCR_IE;
	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
	    sc->sc_ports[pn].sc_port_mcr))
		return EIO;

	/* Clearing Bulkin and Bulkout FIFO */
	if (umcs7840_get_reg(sc, spreg, &data))
		return EIO;
	data |= MCS7840_DEV_SPx_RESET_OUT_FIFO | MCS7840_DEV_SPx_RESET_IN_FIFO;
	if (umcs7840_set_reg(sc, spreg, data))
		return EIO;
	data &= ~(MCS7840_DEV_SPx_RESET_OUT_FIFO
	    | MCS7840_DEV_SPx_RESET_IN_FIFO);
	if (umcs7840_set_reg(sc, spreg, data))
		return EIO;

	/* Set speed 9600 */
	if (umcs7840_set_baudrate(sc, portno, 9600))
		return EIO;


	/* Finally enable all interrupts -- documented */
	/*
	 * Copied from vendor driver, I don't know why we should read LCR
	 * here
	 */
	if (umcs7840_get_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
	    &sc->sc_ports[pn].sc_port_lcr))
		return EIO;
	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_IER,
	    MCS7840_UART_IER_RXSTAT | MCS7840_UART_IER_MODEM))
		return EIO;

	/* Enable RX */
	if (umcs7840_get_reg(sc, ctrlreg, &data))
		return EIO;
	data &= ~MCS7840_DEV_CONTROLx_RX_DISABLE;
	if (umcs7840_set_reg(sc, ctrlreg, data))
		return EIO;
	return 0;
}

static void
umcs7840_port_close(void *self, int portno)
{
	struct umcs7840_softc *sc = self;
	int pn = sc->sc_ports[portno].sc_port_phys;
	int ctrlreg = umcs7840_reg_ctrl(pn);
	uint8_t data;

	if (sc->sc_dying)
		return;

	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR, 0);
	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_IER, 0);

	/* Disable RX */
	if (umcs7840_get_reg(sc, ctrlreg, &data))
		return;
	data |= MCS7840_DEV_CONTROLx_RX_DISABLE;
	if (umcs7840_set_reg(sc, ctrlreg, data))
		return;
}

static void
umcs7840_intr(struct usbd_xfer *xfer, void *priv,
    usbd_status status)
{
	struct umcs7840_softc *sc = priv;
	u_char *buf = sc->sc_intr_buf;
	int actlen;
	int subunit;

	if (sc->sc_dying)
		return;

	if (status == USBD_NOT_STARTED || status == USBD_CANCELLED
	    || status == USBD_IOERROR)
		return;

	if (status != USBD_NORMAL_COMPLETION) {
		aprint_error_dev(sc->sc_dev,
		    "umcs7840_intr: abnormal status: %s\n",
		    usbd_errstr(status));
		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
		return;
	}

	usbd_get_xfer_status(xfer, NULL, NULL, &actlen, NULL);
	if (actlen == 5 || actlen == 13) {
		uint32_t change_mask = 0;
		/* Check status of all ports */
		for (subunit = 0; subunit < sc->sc_numports; subunit++) {
			uint8_t pn = sc->sc_ports[subunit].sc_port_phys;
			if (buf[pn] & MCS7840_UART_ISR_NOPENDING)
				continue;
			DPRINTF(("Port %d has pending interrupt: %02x "
			    "(FIFO: %02x)\n", pn,
			    buf[pn] & MCS7840_UART_ISR_INTMASK,
			    buf[pn] & (~MCS7840_UART_ISR_INTMASK)));
			switch (buf[pn] & MCS7840_UART_ISR_INTMASK) {
			case MCS7840_UART_ISR_RXERR:
			case MCS7840_UART_ISR_RXHASDATA:
			case MCS7840_UART_ISR_RXTIMEOUT:
			case MCS7840_UART_ISR_MSCHANGE:
				change_mask |= (1U << subunit);
				break;
			default:
				/* Do nothing */
				break;
			}
		}

		if (change_mask != 0) {
			atomic_or_32(&sc->sc_change_mask, change_mask);
			usb_add_task(sc->sc_udev, &sc->sc_change_task,
			    USB_TASKQ_DRIVER);
		}
	} else {
		aprint_error_dev(sc->sc_dev,
		   "Invalid interrupt data length %d", actlen);
	}
}

static void
umcs7840_change_task(void *arg)
{
	struct umcs7840_softc *sc = arg;
	uint32_t change_mask;
	int i;

	change_mask = atomic_swap_32(&sc->sc_change_mask, 0);
	for (i = 0; i < sc->sc_numports; i++) {
		if (ISSET(change_mask, (1U << i)))
			ucom_status_change(device_private(
			    sc->sc_ports[i].sc_port_ucom));
	}
}