Home | History | Annotate | Line # | Download | only in usb
uchcom.c revision 1.13.6.8
      1  1.13.6.8     skrll /*	$NetBSD: uchcom.c,v 1.13.6.8 2016/04/16 13:22:00 skrll Exp $	*/
      2       1.1  tshiozak 
      3       1.1  tshiozak /*
      4       1.1  tshiozak  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5       1.1  tshiozak  * All rights reserved.
      6       1.1  tshiozak  *
      7       1.1  tshiozak  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  tshiozak  * by Takuya SHIOZAKI (tshiozak (at) netbsd.org).
      9       1.1  tshiozak  *
     10       1.1  tshiozak  * Redistribution and use in source and binary forms, with or without
     11       1.1  tshiozak  * modification, are permitted provided that the following conditions
     12       1.1  tshiozak  * are met:
     13       1.1  tshiozak  * 1. Redistributions of source code must retain the above copyright
     14       1.1  tshiozak  *    notice, this list of conditions and the following disclaimer.
     15       1.1  tshiozak  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  tshiozak  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  tshiozak  *    documentation and/or other materials provided with the distribution.
     18       1.1  tshiozak  *
     19       1.1  tshiozak  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1  tshiozak  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1  tshiozak  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1  tshiozak  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1  tshiozak  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1  tshiozak  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1  tshiozak  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1  tshiozak  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1  tshiozak  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1  tshiozak  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1  tshiozak  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1  tshiozak  */
     31       1.1  tshiozak 
     32       1.1  tshiozak #include <sys/cdefs.h>
     33  1.13.6.8     skrll __KERNEL_RCSID(0, "$NetBSD: uchcom.c,v 1.13.6.8 2016/04/16 13:22:00 skrll Exp $");
     34       1.1  tshiozak 
     35       1.1  tshiozak /*
     36       1.1  tshiozak  * driver for WinChipHead CH341/340, the worst USB-serial chip in the world.
     37       1.1  tshiozak  */
     38       1.1  tshiozak 
     39       1.1  tshiozak #include <sys/param.h>
     40       1.1  tshiozak #include <sys/systm.h>
     41       1.1  tshiozak #include <sys/kernel.h>
     42  1.13.6.1     skrll #include <sys/kmem.h>
     43       1.1  tshiozak #include <sys/ioctl.h>
     44       1.1  tshiozak #include <sys/conf.h>
     45       1.1  tshiozak #include <sys/tty.h>
     46       1.1  tshiozak #include <sys/file.h>
     47       1.1  tshiozak #include <sys/select.h>
     48       1.1  tshiozak #include <sys/proc.h>
     49       1.1  tshiozak #include <sys/device.h>
     50       1.1  tshiozak #include <sys/poll.h>
     51       1.1  tshiozak 
     52       1.1  tshiozak #include <dev/usb/usb.h>
     53       1.1  tshiozak #include <dev/usb/usbcdc.h>
     54       1.1  tshiozak 
     55       1.1  tshiozak #include <dev/usb/usbdi.h>
     56       1.1  tshiozak #include <dev/usb/usbdi_util.h>
     57       1.1  tshiozak #include <dev/usb/usbdevs.h>
     58       1.1  tshiozak #include <dev/usb/usb_quirks.h>
     59       1.1  tshiozak 
     60       1.1  tshiozak #include <dev/usb/ucomvar.h>
     61       1.1  tshiozak 
     62       1.1  tshiozak #ifdef UCHCOM_DEBUG
     63      1.10    dyoung #define DPRINTFN(n, x)  if (uchcomdebug > (n)) printf x
     64       1.1  tshiozak int	uchcomdebug = 0;
     65       1.1  tshiozak #else
     66       1.1  tshiozak #define DPRINTFN(n, x)
     67       1.1  tshiozak #endif
     68       1.1  tshiozak #define DPRINTF(x) DPRINTFN(0, x)
     69       1.1  tshiozak 
     70       1.1  tshiozak #define	UCHCOM_IFACE_INDEX	0
     71       1.1  tshiozak #define	UCHCOM_CONFIG_INDEX	0
     72       1.1  tshiozak 
     73       1.1  tshiozak #define UCHCOM_REV_CH340	0x0250
     74       1.1  tshiozak #define UCHCOM_INPUT_BUF_SIZE	8
     75       1.1  tshiozak 
     76       1.1  tshiozak #define UCHCOM_REQ_GET_VERSION	0x5F
     77       1.1  tshiozak #define UCHCOM_REQ_READ_REG	0x95
     78       1.1  tshiozak #define UCHCOM_REQ_WRITE_REG	0x9A
     79       1.1  tshiozak #define UCHCOM_REQ_RESET	0xA1
     80       1.1  tshiozak #define UCHCOM_REQ_SET_DTRRTS	0xA4
     81       1.1  tshiozak 
     82       1.1  tshiozak #define UCHCOM_REG_STAT1	0x06
     83       1.1  tshiozak #define UCHCOM_REG_STAT2	0x07
     84       1.1  tshiozak #define UCHCOM_REG_BPS_PRE	0x12
     85       1.1  tshiozak #define UCHCOM_REG_BPS_DIV	0x13
     86       1.1  tshiozak #define UCHCOM_REG_BPS_MOD	0x14
     87       1.1  tshiozak #define UCHCOM_REG_BPS_PAD	0x0F
     88       1.1  tshiozak #define UCHCOM_REG_BREAK1	0x05
     89       1.1  tshiozak #define UCHCOM_REG_BREAK2	0x18
     90       1.1  tshiozak #define UCHCOM_REG_LCR1		0x18
     91       1.1  tshiozak #define UCHCOM_REG_LCR2		0x25
     92       1.1  tshiozak 
     93       1.1  tshiozak #define UCHCOM_VER_20		0x20
     94       1.1  tshiozak 
     95       1.1  tshiozak #define UCHCOM_BASE_UNKNOWN	0
     96       1.1  tshiozak #define UCHCOM_BPS_MOD_BASE	20000000
     97       1.1  tshiozak #define UCHCOM_BPS_MOD_BASE_OFS	1100
     98       1.1  tshiozak 
     99       1.1  tshiozak #define UCHCOM_DTR_MASK		0x20
    100       1.1  tshiozak #define UCHCOM_RTS_MASK		0x40
    101       1.1  tshiozak 
    102       1.1  tshiozak #define UCHCOM_BRK1_MASK	0x01
    103       1.1  tshiozak #define UCHCOM_BRK2_MASK	0x40
    104       1.1  tshiozak 
    105       1.1  tshiozak #define UCHCOM_LCR1_MASK	0xAF
    106       1.1  tshiozak #define UCHCOM_LCR2_MASK	0x07
    107       1.1  tshiozak #define UCHCOM_LCR1_PARENB	0x80
    108       1.1  tshiozak #define UCHCOM_LCR2_PAREVEN	0x07
    109       1.1  tshiozak #define UCHCOM_LCR2_PARODD	0x06
    110       1.1  tshiozak #define UCHCOM_LCR2_PARMARK	0x05
    111       1.1  tshiozak #define UCHCOM_LCR2_PARSPACE	0x04
    112       1.1  tshiozak 
    113       1.1  tshiozak #define UCHCOM_INTR_STAT1	0x02
    114       1.1  tshiozak #define UCHCOM_INTR_STAT2	0x03
    115       1.1  tshiozak #define UCHCOM_INTR_LEAST	4
    116       1.1  tshiozak 
    117       1.1  tshiozak #define UCHCOMIBUFSIZE 256
    118       1.1  tshiozak #define UCHCOMOBUFSIZE 256
    119       1.1  tshiozak 
    120       1.1  tshiozak struct uchcom_softc
    121       1.1  tshiozak {
    122      1.10    dyoung 	device_t		sc_dev;
    123  1.13.6.2     skrll 	struct usbd_device *	sc_udev;
    124      1.10    dyoung 	device_t		sc_subdev;
    125  1.13.6.2     skrll 	struct usbd_interface *	sc_iface;
    126       1.1  tshiozak 	int			sc_dying;
    127       1.1  tshiozak 	/* */
    128       1.1  tshiozak 	int			sc_intr_endpoint;
    129       1.1  tshiozak 	int			sc_intr_size;
    130  1.13.6.2     skrll 	struct usbd_pipe *	sc_intr_pipe;
    131       1.1  tshiozak 	u_char			*sc_intr_buf;
    132       1.1  tshiozak 	/* */
    133       1.1  tshiozak 	uint8_t			sc_version;
    134       1.1  tshiozak 	int			sc_dtr;
    135       1.1  tshiozak 	int			sc_rts;
    136       1.1  tshiozak 	u_char			sc_lsr;
    137       1.1  tshiozak 	u_char			sc_msr;
    138       1.1  tshiozak 	int			sc_lcr1;
    139       1.1  tshiozak 	int			sc_lcr2;
    140       1.1  tshiozak };
    141       1.1  tshiozak 
    142       1.1  tshiozak struct uchcom_endpoints
    143       1.1  tshiozak {
    144       1.1  tshiozak 	int		ep_bulkin;
    145       1.1  tshiozak 	int		ep_bulkout;
    146       1.1  tshiozak 	int		ep_intr;
    147       1.1  tshiozak 	int		ep_intr_size;
    148       1.1  tshiozak };
    149       1.1  tshiozak 
    150       1.1  tshiozak struct uchcom_divider
    151       1.1  tshiozak {
    152       1.1  tshiozak 	uint8_t		dv_prescaler;
    153       1.1  tshiozak 	uint8_t		dv_div;
    154       1.1  tshiozak 	uint8_t		dv_mod;
    155       1.1  tshiozak };
    156       1.1  tshiozak 
    157       1.1  tshiozak struct uchcom_divider_record
    158       1.1  tshiozak {
    159       1.1  tshiozak 	uint32_t		dvr_high;
    160       1.1  tshiozak 	uint32_t		dvr_low;
    161       1.1  tshiozak 	uint32_t		dvr_base_clock;
    162       1.1  tshiozak 	struct uchcom_divider	dvr_divider;
    163       1.1  tshiozak };
    164       1.1  tshiozak 
    165       1.1  tshiozak static const struct uchcom_divider_record dividers[] =
    166       1.1  tshiozak {
    167       1.1  tshiozak 	{  307200, 307200, UCHCOM_BASE_UNKNOWN, { 7, 0xD9, 0 } },
    168       1.1  tshiozak 	{  921600, 921600, UCHCOM_BASE_UNKNOWN, { 7, 0xF3, 0 } },
    169       1.1  tshiozak 	{ 2999999,  23530,             6000000, { 3,    0, 0 } },
    170       1.1  tshiozak 	{   23529,   2942,              750000, { 2,    0, 0 } },
    171       1.1  tshiozak 	{    2941,    368,               93750, { 1,    0, 0 } },
    172       1.1  tshiozak 	{     367,      1,               11719, { 0,    0, 0 } },
    173       1.1  tshiozak };
    174       1.1  tshiozak #define NUM_DIVIDERS	(sizeof (dividers) / sizeof (dividers[0]))
    175       1.1  tshiozak 
    176       1.1  tshiozak static const struct usb_devno uchcom_devs[] = {
    177       1.1  tshiozak 	{ USB_VENDOR_WINCHIPHEAD, USB_PRODUCT_WINCHIPHEAD_CH341SER },
    178       1.7      haad 	{ USB_VENDOR_WINCHIPHEAD2, USB_PRODUCT_WINCHIPHEAD2_CH341 },
    179       1.1  tshiozak };
    180       1.1  tshiozak #define uchcom_lookup(v, p)	usb_lookup(uchcom_devs, v, p)
    181       1.1  tshiozak 
    182       1.1  tshiozak Static void	uchcom_get_status(void *, int, u_char *, u_char *);
    183       1.1  tshiozak Static void	uchcom_set(void *, int, int, int);
    184       1.1  tshiozak Static int	uchcom_param(void *, int, struct termios *);
    185       1.1  tshiozak Static int	uchcom_open(void *, int);
    186       1.1  tshiozak Static void	uchcom_close(void *, int);
    187  1.13.6.2     skrll Static void	uchcom_intr(struct usbd_xfer *, void *,
    188       1.1  tshiozak 			    usbd_status);
    189       1.1  tshiozak 
    190       1.1  tshiozak static int	set_config(struct uchcom_softc *);
    191  1.13.6.7     skrll static int	find_ifaces(struct uchcom_softc *, struct usbd_interface **);
    192       1.1  tshiozak static int	find_endpoints(struct uchcom_softc *,
    193       1.1  tshiozak 			       struct uchcom_endpoints *);
    194       1.1  tshiozak static void	close_intr_pipe(struct uchcom_softc *);
    195       1.1  tshiozak 
    196       1.1  tshiozak 
    197       1.1  tshiozak struct	ucom_methods uchcom_methods = {
    198       1.1  tshiozak 	.ucom_get_status	= uchcom_get_status,
    199       1.1  tshiozak 	.ucom_set		= uchcom_set,
    200       1.1  tshiozak 	.ucom_param		= uchcom_param,
    201       1.1  tshiozak 	.ucom_ioctl		= NULL,
    202       1.1  tshiozak 	.ucom_open		= uchcom_open,
    203       1.1  tshiozak 	.ucom_close		= uchcom_close,
    204       1.1  tshiozak 	.ucom_read		= NULL,
    205       1.1  tshiozak 	.ucom_write		= NULL,
    206       1.1  tshiozak };
    207       1.1  tshiozak 
    208       1.6      cube int uchcom_match(device_t, cfdata_t, void *);
    209       1.2    dyoung void uchcom_attach(device_t, device_t, void *);
    210       1.2    dyoung void uchcom_childdet(device_t, device_t);
    211       1.2    dyoung int uchcom_detach(device_t, int);
    212       1.2    dyoung int uchcom_activate(device_t, enum devact);
    213       1.2    dyoung 
    214       1.2    dyoung extern struct cfdriver uchcom_cd;
    215       1.2    dyoung 
    216       1.6      cube CFATTACH_DECL2_NEW(uchcom,
    217       1.2    dyoung     sizeof(struct uchcom_softc),
    218       1.2    dyoung     uchcom_match,
    219       1.2    dyoung     uchcom_attach,
    220       1.2    dyoung     uchcom_detach,
    221       1.2    dyoung     uchcom_activate,
    222       1.2    dyoung     NULL,
    223       1.2    dyoung     uchcom_childdet);
    224       1.1  tshiozak 
    225       1.1  tshiozak /* ----------------------------------------------------------------------
    226       1.1  tshiozak  * driver entry points
    227       1.1  tshiozak  */
    228       1.1  tshiozak 
    229  1.13.6.5     skrll int
    230      1.10    dyoung uchcom_match(device_t parent, cfdata_t match, void *aux)
    231       1.1  tshiozak {
    232      1.10    dyoung 	struct usb_attach_arg *uaa = aux;
    233       1.1  tshiozak 
    234  1.13.6.3     skrll 	return (uchcom_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    235       1.1  tshiozak 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    236       1.1  tshiozak }
    237       1.1  tshiozak 
    238  1.13.6.5     skrll void
    239      1.10    dyoung uchcom_attach(device_t parent, device_t self, void *aux)
    240       1.1  tshiozak {
    241      1.10    dyoung 	struct uchcom_softc *sc = device_private(self);
    242      1.10    dyoung 	struct usb_attach_arg *uaa = aux;
    243  1.13.6.4     skrll 	struct usbd_device *dev = uaa->uaa_device;
    244       1.1  tshiozak 	char *devinfop;
    245       1.1  tshiozak 	struct uchcom_endpoints endpoints;
    246  1.13.6.8     skrll 	struct ucom_attach_args ucaa;
    247       1.1  tshiozak 
    248       1.8    plunky 	aprint_naive("\n");
    249       1.8    plunky 	aprint_normal("\n");
    250       1.8    plunky 
    251       1.1  tshiozak 	devinfop = usbd_devinfo_alloc(dev, 0);
    252       1.6      cube 	aprint_normal_dev(self, "%s\n", devinfop);
    253       1.1  tshiozak 	usbd_devinfo_free(devinfop);
    254       1.1  tshiozak 
    255       1.6      cube 	sc->sc_dev = self;
    256       1.1  tshiozak         sc->sc_udev = dev;
    257       1.1  tshiozak 	sc->sc_dying = 0;
    258       1.1  tshiozak 	sc->sc_dtr = sc->sc_rts = -1;
    259       1.1  tshiozak 	sc->sc_lsr = sc->sc_msr = 0;
    260       1.1  tshiozak 
    261       1.1  tshiozak 	DPRINTF(("\n\nuchcom attach: sc=%p\n", sc));
    262       1.1  tshiozak 
    263       1.1  tshiozak 	if (set_config(sc))
    264       1.1  tshiozak 		goto failed;
    265       1.1  tshiozak 
    266  1.13.6.3     skrll 	switch (uaa->uaa_release) {
    267       1.1  tshiozak 	case UCHCOM_REV_CH340:
    268       1.6      cube 		aprint_normal_dev(self, "CH340 detected\n");
    269       1.1  tshiozak 		break;
    270       1.1  tshiozak 	default:
    271       1.6      cube 		aprint_normal_dev(self, "CH341 detected\n");
    272       1.1  tshiozak 		break;
    273       1.1  tshiozak 	}
    274       1.1  tshiozak 
    275       1.1  tshiozak 	if (find_ifaces(sc, &sc->sc_iface))
    276       1.1  tshiozak 		goto failed;
    277       1.1  tshiozak 
    278       1.1  tshiozak 	if (find_endpoints(sc, &endpoints))
    279       1.1  tshiozak 		goto failed;
    280       1.1  tshiozak 
    281       1.1  tshiozak 	sc->sc_intr_endpoint = endpoints.ep_intr;
    282       1.1  tshiozak 	sc->sc_intr_size = endpoints.ep_intr_size;
    283       1.1  tshiozak 
    284       1.1  tshiozak 	/* setup ucom layer */
    285  1.13.6.8     skrll 	ucaa.ucaa_portno = UCOM_UNK_PORTNO;
    286  1.13.6.8     skrll 	ucaa.ucaa_bulkin = endpoints.ep_bulkin;
    287  1.13.6.8     skrll 	ucaa.ucaa_bulkout = endpoints.ep_bulkout;
    288  1.13.6.8     skrll 	ucaa.ucaa_ibufsize = UCHCOMIBUFSIZE;
    289  1.13.6.8     skrll 	ucaa.ucaa_obufsize = UCHCOMOBUFSIZE;
    290  1.13.6.8     skrll 	ucaa.ucaa_ibufsizepad = UCHCOMIBUFSIZE;
    291  1.13.6.8     skrll 	ucaa.ucaa_opkthdrlen = 0;
    292  1.13.6.8     skrll 	ucaa.ucaa_device = dev;
    293  1.13.6.8     skrll 	ucaa.ucaa_iface = sc->sc_iface;
    294  1.13.6.8     skrll 	ucaa.ucaa_methods = &uchcom_methods;
    295  1.13.6.8     skrll 	ucaa.ucaa_arg = sc;
    296  1.13.6.8     skrll 	ucaa.ucaa_info = NULL;
    297       1.1  tshiozak 
    298       1.1  tshiozak 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    299      1.10    dyoung 			   sc->sc_dev);
    300       1.1  tshiozak 
    301  1.13.6.8     skrll 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa,
    302       1.1  tshiozak 					    ucomprint, ucomsubmatch);
    303       1.1  tshiozak 
    304      1.10    dyoung 	return;
    305       1.1  tshiozak 
    306       1.1  tshiozak failed:
    307       1.1  tshiozak 	sc->sc_dying = 1;
    308      1.10    dyoung 	return;
    309       1.1  tshiozak }
    310       1.1  tshiozak 
    311       1.2    dyoung void
    312       1.2    dyoung uchcom_childdet(device_t self, device_t child)
    313       1.2    dyoung {
    314       1.2    dyoung 	struct uchcom_softc *sc = device_private(self);
    315       1.2    dyoung 
    316       1.2    dyoung 	KASSERT(sc->sc_subdev == child);
    317       1.2    dyoung 	sc->sc_subdev = NULL;
    318       1.2    dyoung }
    319       1.2    dyoung 
    320  1.13.6.5     skrll int
    321      1.10    dyoung uchcom_detach(device_t self, int flags)
    322       1.1  tshiozak {
    323      1.10    dyoung 	struct uchcom_softc *sc = device_private(self);
    324       1.1  tshiozak 	int rv = 0;
    325       1.1  tshiozak 
    326       1.1  tshiozak 	DPRINTF(("uchcom_detach: sc=%p flags=%d\n", sc, flags));
    327       1.1  tshiozak 
    328       1.1  tshiozak 	close_intr_pipe(sc);
    329       1.1  tshiozak 
    330       1.1  tshiozak 	sc->sc_dying = 1;
    331       1.1  tshiozak 
    332       1.2    dyoung 	if (sc->sc_subdev != NULL)
    333       1.1  tshiozak 		rv = config_detach(sc->sc_subdev, flags);
    334       1.1  tshiozak 
    335       1.1  tshiozak 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    336      1.10    dyoung 			   sc->sc_dev);
    337       1.1  tshiozak 
    338       1.1  tshiozak 	return rv;
    339       1.1  tshiozak }
    340       1.1  tshiozak 
    341       1.1  tshiozak int
    342       1.6      cube uchcom_activate(device_t self, enum devact act)
    343       1.1  tshiozak {
    344       1.6      cube 	struct uchcom_softc *sc = device_private(self);
    345       1.1  tshiozak 
    346       1.1  tshiozak 	switch (act) {
    347       1.1  tshiozak 	case DVACT_DEACTIVATE:
    348       1.1  tshiozak 		close_intr_pipe(sc);
    349       1.1  tshiozak 		sc->sc_dying = 1;
    350       1.9    dyoung 		return 0;
    351       1.9    dyoung 	default:
    352       1.9    dyoung 		return EOPNOTSUPP;
    353       1.1  tshiozak 	}
    354       1.1  tshiozak }
    355       1.1  tshiozak 
    356       1.1  tshiozak static int
    357       1.1  tshiozak set_config(struct uchcom_softc *sc)
    358       1.1  tshiozak {
    359       1.1  tshiozak 	usbd_status err;
    360       1.1  tshiozak 
    361       1.1  tshiozak 	err = usbd_set_config_index(sc->sc_udev, UCHCOM_CONFIG_INDEX, 1);
    362       1.1  tshiozak 	if (err) {
    363       1.6      cube 		aprint_error_dev(sc->sc_dev,
    364       1.6      cube 		    "failed to set configuration: %s\n", usbd_errstr(err));
    365       1.1  tshiozak 		return -1;
    366       1.1  tshiozak 	}
    367       1.1  tshiozak 
    368       1.1  tshiozak 	return 0;
    369       1.1  tshiozak }
    370       1.1  tshiozak 
    371       1.1  tshiozak static int
    372  1.13.6.7     skrll find_ifaces(struct uchcom_softc *sc, struct usbd_interface **riface)
    373       1.1  tshiozak {
    374       1.1  tshiozak 	usbd_status err;
    375       1.1  tshiozak 
    376       1.1  tshiozak 	err = usbd_device2interface_handle(sc->sc_udev, UCHCOM_IFACE_INDEX,
    377       1.1  tshiozak 					   riface);
    378       1.1  tshiozak 	if (err) {
    379       1.6      cube 		aprint_error("\n%s: failed to get interface: %s\n",
    380      1.10    dyoung 			device_xname(sc->sc_dev), usbd_errstr(err));
    381       1.1  tshiozak 		return -1;
    382       1.1  tshiozak 	}
    383       1.1  tshiozak 
    384       1.1  tshiozak 	return 0;
    385       1.1  tshiozak }
    386       1.1  tshiozak 
    387       1.1  tshiozak static int
    388       1.1  tshiozak find_endpoints(struct uchcom_softc *sc, struct uchcom_endpoints *endpoints)
    389       1.1  tshiozak {
    390       1.1  tshiozak 	int i, bin=-1, bout=-1, intr=-1, isize=0;
    391       1.1  tshiozak 	usb_interface_descriptor_t *id;
    392       1.1  tshiozak 	usb_endpoint_descriptor_t *ed;
    393       1.1  tshiozak 
    394       1.1  tshiozak 	id = usbd_get_interface_descriptor(sc->sc_iface);
    395       1.1  tshiozak 
    396       1.1  tshiozak 	for (i = 0; i < id->bNumEndpoints; i++) {
    397       1.1  tshiozak 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    398       1.1  tshiozak 		if (ed == NULL) {
    399       1.6      cube 			aprint_error_dev(sc->sc_dev,
    400       1.6      cube 			    "no endpoint descriptor for %d\n", i);
    401       1.1  tshiozak 			return -1;
    402       1.1  tshiozak 		}
    403       1.1  tshiozak 
    404       1.1  tshiozak 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    405       1.1  tshiozak 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    406       1.1  tshiozak 			intr = ed->bEndpointAddress;
    407       1.1  tshiozak 			isize = UGETW(ed->wMaxPacketSize);
    408       1.1  tshiozak 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    409       1.1  tshiozak 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    410       1.1  tshiozak 			bin = ed->bEndpointAddress;
    411       1.1  tshiozak 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    412       1.1  tshiozak 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    413       1.1  tshiozak 			bout = ed->bEndpointAddress;
    414       1.1  tshiozak 		}
    415       1.1  tshiozak 	}
    416       1.1  tshiozak 
    417       1.1  tshiozak 	if (intr == -1 || bin == -1 || bout == -1) {
    418       1.1  tshiozak 		if (intr == -1) {
    419       1.6      cube 			aprint_error_dev(sc->sc_dev,
    420       1.6      cube 			    "no interrupt end point\n");
    421       1.1  tshiozak 		}
    422       1.1  tshiozak 		if (bin == -1) {
    423       1.6      cube 			aprint_error_dev(sc->sc_dev,
    424       1.6      cube 			    "no data bulk in end point\n");
    425       1.1  tshiozak 		}
    426       1.1  tshiozak 		if (bout == -1) {
    427       1.6      cube 			aprint_error_dev(sc->sc_dev,
    428       1.6      cube 			    "no data bulk out end point\n");
    429       1.1  tshiozak 		}
    430       1.1  tshiozak 		return -1;
    431       1.1  tshiozak 	}
    432       1.1  tshiozak 	if (isize < UCHCOM_INTR_LEAST) {
    433       1.6      cube 		aprint_error_dev(sc->sc_dev, "intr pipe is too short\n");
    434       1.1  tshiozak 		return -1;
    435       1.1  tshiozak 	}
    436       1.1  tshiozak 
    437       1.1  tshiozak 	DPRINTF(("%s: bulkin=%d, bulkout=%d, intr=%d, isize=%d\n",
    438      1.10    dyoung 		 device_xname(sc->sc_dev), bin, bout, intr, isize));
    439       1.1  tshiozak 
    440       1.1  tshiozak 	endpoints->ep_intr = intr;
    441       1.1  tshiozak 	endpoints->ep_intr_size = isize;
    442       1.1  tshiozak 	endpoints->ep_bulkin = bin;
    443       1.1  tshiozak 	endpoints->ep_bulkout = bout;
    444       1.1  tshiozak 
    445       1.1  tshiozak 	return 0;
    446       1.1  tshiozak }
    447       1.1  tshiozak 
    448       1.1  tshiozak 
    449       1.1  tshiozak /* ----------------------------------------------------------------------
    450       1.1  tshiozak  * low level i/o
    451       1.1  tshiozak  */
    452       1.1  tshiozak 
    453       1.1  tshiozak static __inline usbd_status
    454       1.1  tshiozak generic_control_out(struct uchcom_softc *sc, uint8_t reqno,
    455       1.1  tshiozak 		    uint16_t value, uint16_t index)
    456       1.1  tshiozak {
    457       1.1  tshiozak 	usb_device_request_t req;
    458       1.1  tshiozak 
    459       1.1  tshiozak 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    460       1.1  tshiozak 	req.bRequest = reqno;
    461       1.1  tshiozak 	USETW(req.wValue, value);
    462       1.1  tshiozak 	USETW(req.wIndex, index);
    463       1.1  tshiozak 	USETW(req.wLength, 0);
    464       1.1  tshiozak 
    465       1.1  tshiozak 	return usbd_do_request(sc->sc_udev, &req, 0);
    466       1.1  tshiozak }
    467       1.1  tshiozak 
    468       1.1  tshiozak static __inline usbd_status
    469       1.1  tshiozak generic_control_in(struct uchcom_softc *sc, uint8_t reqno,
    470       1.1  tshiozak 		   uint16_t value, uint16_t index, void *buf, int buflen,
    471       1.1  tshiozak 		   int *actlen)
    472       1.1  tshiozak {
    473       1.1  tshiozak 	usb_device_request_t req;
    474       1.1  tshiozak 
    475       1.1  tshiozak 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    476       1.1  tshiozak 	req.bRequest = reqno;
    477       1.1  tshiozak 	USETW(req.wValue, value);
    478       1.1  tshiozak 	USETW(req.wIndex, index);
    479       1.1  tshiozak 	USETW(req.wLength, (uint16_t)buflen);
    480       1.1  tshiozak 
    481       1.1  tshiozak 	return usbd_do_request_flags(sc->sc_udev, &req, buf,
    482       1.1  tshiozak 				     USBD_SHORT_XFER_OK, actlen,
    483       1.1  tshiozak 				     USBD_DEFAULT_TIMEOUT);
    484       1.1  tshiozak }
    485       1.1  tshiozak 
    486       1.1  tshiozak static __inline usbd_status
    487       1.1  tshiozak write_reg(struct uchcom_softc *sc,
    488       1.1  tshiozak 	  uint8_t reg1, uint8_t val1, uint8_t reg2, uint8_t val2)
    489       1.1  tshiozak {
    490       1.1  tshiozak 	DPRINTF(("uchcom: write reg 0x%02X<-0x%02X, 0x%02X<-0x%02X\n",
    491       1.1  tshiozak 		 (unsigned)reg1, (unsigned)val1,
    492       1.1  tshiozak 		 (unsigned)reg2, (unsigned)val2));
    493       1.1  tshiozak 	return generic_control_out(
    494       1.1  tshiozak 		sc, UCHCOM_REQ_WRITE_REG,
    495       1.1  tshiozak 		reg1|((uint16_t)reg2<<8), val1|((uint16_t)val2<<8));
    496       1.1  tshiozak }
    497       1.1  tshiozak 
    498       1.1  tshiozak static __inline usbd_status
    499       1.1  tshiozak read_reg(struct uchcom_softc *sc,
    500       1.1  tshiozak 	 uint8_t reg1, uint8_t *rval1, uint8_t reg2, uint8_t *rval2)
    501       1.1  tshiozak {
    502       1.1  tshiozak 	uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
    503       1.1  tshiozak 	usbd_status err;
    504       1.1  tshiozak 	int actin;
    505       1.1  tshiozak 
    506       1.1  tshiozak 	err = generic_control_in(
    507       1.1  tshiozak 		sc, UCHCOM_REQ_READ_REG,
    508  1.13.6.6     skrll 		reg1|((uint16_t)reg2<<8), 0, buf, sizeof(buf), &actin);
    509       1.1  tshiozak 	if (err)
    510       1.1  tshiozak 		return err;
    511       1.1  tshiozak 
    512       1.1  tshiozak 	DPRINTF(("uchcom: read reg 0x%02X->0x%02X, 0x%02X->0x%02X\n",
    513       1.1  tshiozak 		 (unsigned)reg1, (unsigned)buf[0],
    514       1.1  tshiozak 		 (unsigned)reg2, (unsigned)buf[1]));
    515       1.1  tshiozak 
    516       1.1  tshiozak 	if (rval1) *rval1 = buf[0];
    517       1.1  tshiozak 	if (rval2) *rval2 = buf[1];
    518       1.1  tshiozak 
    519       1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
    520       1.1  tshiozak }
    521       1.1  tshiozak 
    522       1.1  tshiozak static __inline usbd_status
    523       1.1  tshiozak get_version(struct uchcom_softc *sc, uint8_t *rver)
    524       1.1  tshiozak {
    525       1.1  tshiozak 	uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
    526       1.1  tshiozak 	usbd_status err;
    527       1.1  tshiozak 	int actin;
    528       1.1  tshiozak 
    529       1.1  tshiozak 	err = generic_control_in(
    530  1.13.6.6     skrll 		sc, UCHCOM_REQ_GET_VERSION, 0, 0, buf, sizeof(buf), &actin);
    531       1.1  tshiozak 	if (err)
    532       1.1  tshiozak 		return err;
    533       1.1  tshiozak 
    534       1.1  tshiozak 	if (rver) *rver = buf[0];
    535       1.1  tshiozak 
    536       1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
    537       1.1  tshiozak }
    538       1.1  tshiozak 
    539       1.1  tshiozak static __inline usbd_status
    540       1.1  tshiozak get_status(struct uchcom_softc *sc, uint8_t *rval)
    541       1.1  tshiozak {
    542       1.1  tshiozak 	return read_reg(sc, UCHCOM_REG_STAT1, rval, UCHCOM_REG_STAT2, NULL);
    543       1.1  tshiozak }
    544       1.1  tshiozak 
    545       1.1  tshiozak static __inline usbd_status
    546       1.1  tshiozak set_dtrrts_10(struct uchcom_softc *sc, uint8_t val)
    547       1.1  tshiozak {
    548       1.1  tshiozak 	return write_reg(sc, UCHCOM_REG_STAT1, val, UCHCOM_REG_STAT1, val);
    549       1.1  tshiozak }
    550       1.1  tshiozak 
    551       1.1  tshiozak static __inline usbd_status
    552       1.1  tshiozak set_dtrrts_20(struct uchcom_softc *sc, uint8_t val)
    553       1.1  tshiozak {
    554       1.1  tshiozak 	return generic_control_out(sc, UCHCOM_REQ_SET_DTRRTS, val, 0);
    555       1.1  tshiozak }
    556       1.1  tshiozak 
    557       1.1  tshiozak 
    558       1.1  tshiozak /* ----------------------------------------------------------------------
    559       1.1  tshiozak  * middle layer
    560       1.1  tshiozak  */
    561       1.1  tshiozak 
    562       1.1  tshiozak static int
    563       1.1  tshiozak update_version(struct uchcom_softc *sc)
    564       1.1  tshiozak {
    565       1.1  tshiozak 	usbd_status err;
    566       1.1  tshiozak 
    567       1.1  tshiozak 	err = get_version(sc, &sc->sc_version);
    568       1.1  tshiozak 	if (err) {
    569       1.6      cube 		aprint_error_dev(sc->sc_dev, "cannot get version: %s\n",
    570       1.6      cube 		    usbd_errstr(err));
    571       1.1  tshiozak 		return EIO;
    572       1.1  tshiozak 	}
    573       1.1  tshiozak 
    574       1.1  tshiozak 	return 0;
    575       1.1  tshiozak }
    576       1.1  tshiozak 
    577       1.1  tshiozak static void
    578       1.1  tshiozak convert_status(struct uchcom_softc *sc, uint8_t cur)
    579       1.1  tshiozak {
    580       1.1  tshiozak 	sc->sc_dtr = !(cur & UCHCOM_DTR_MASK);
    581       1.1  tshiozak 	sc->sc_rts = !(cur & UCHCOM_RTS_MASK);
    582       1.1  tshiozak 
    583       1.1  tshiozak 	cur = ~cur & 0x0F;
    584       1.1  tshiozak 	sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur);
    585       1.1  tshiozak }
    586       1.1  tshiozak 
    587       1.1  tshiozak static int
    588       1.1  tshiozak update_status(struct uchcom_softc *sc)
    589       1.1  tshiozak {
    590       1.1  tshiozak 	usbd_status err;
    591       1.1  tshiozak 	uint8_t cur;
    592       1.1  tshiozak 
    593       1.1  tshiozak 	err = get_status(sc, &cur);
    594       1.1  tshiozak 	if (err) {
    595       1.6      cube 		aprint_error_dev(sc->sc_dev,
    596       1.6      cube 		    "cannot update status: %s\n", usbd_errstr(err));
    597       1.1  tshiozak 		return EIO;
    598       1.1  tshiozak 	}
    599       1.1  tshiozak 	convert_status(sc, cur);
    600       1.1  tshiozak 
    601       1.1  tshiozak 	return 0;
    602       1.1  tshiozak }
    603       1.1  tshiozak 
    604       1.1  tshiozak 
    605       1.1  tshiozak static int
    606       1.1  tshiozak set_dtrrts(struct uchcom_softc *sc, int dtr, int rts)
    607       1.1  tshiozak {
    608       1.1  tshiozak 	usbd_status err;
    609       1.1  tshiozak 	uint8_t val = 0;
    610       1.1  tshiozak 
    611       1.1  tshiozak 	if (dtr) val |= UCHCOM_DTR_MASK;
    612       1.1  tshiozak 	if (rts) val |= UCHCOM_RTS_MASK;
    613       1.1  tshiozak 
    614       1.1  tshiozak 	if (sc->sc_version < UCHCOM_VER_20)
    615       1.1  tshiozak 		err = set_dtrrts_10(sc, ~val);
    616       1.1  tshiozak 	else
    617       1.1  tshiozak 		err = set_dtrrts_20(sc, ~val);
    618       1.1  tshiozak 
    619       1.1  tshiozak 	if (err) {
    620       1.6      cube 		aprint_error_dev(sc->sc_dev, "cannot set DTR/RTS: %s\n",
    621       1.6      cube 		    usbd_errstr(err));
    622       1.1  tshiozak 		return EIO;
    623       1.1  tshiozak 	}
    624       1.1  tshiozak 
    625       1.1  tshiozak 	return 0;
    626       1.1  tshiozak }
    627       1.1  tshiozak 
    628       1.1  tshiozak static int
    629       1.1  tshiozak set_break(struct uchcom_softc *sc, int onoff)
    630       1.1  tshiozak {
    631       1.1  tshiozak 	usbd_status err;
    632       1.1  tshiozak 	uint8_t brk1, brk2;
    633       1.1  tshiozak 
    634       1.1  tshiozak 	err = read_reg(sc, UCHCOM_REG_BREAK1, &brk1, UCHCOM_REG_BREAK2, &brk2);
    635       1.1  tshiozak 	if (err)
    636       1.1  tshiozak 		return EIO;
    637       1.1  tshiozak 	if (onoff) {
    638       1.1  tshiozak 		/* on - clear bits */
    639       1.1  tshiozak 		brk1 &= ~UCHCOM_BRK1_MASK;
    640       1.1  tshiozak 		brk2 &= ~UCHCOM_BRK2_MASK;
    641       1.1  tshiozak 	} else {
    642       1.1  tshiozak 		/* off - set bits */
    643       1.1  tshiozak 		brk1 |= UCHCOM_BRK1_MASK;
    644       1.1  tshiozak 		brk2 |= UCHCOM_BRK2_MASK;
    645       1.1  tshiozak 	}
    646       1.1  tshiozak 	err = write_reg(sc, UCHCOM_REG_BREAK1, brk1, UCHCOM_REG_BREAK2, brk2);
    647       1.1  tshiozak 	if (err)
    648       1.1  tshiozak 		return EIO;
    649       1.1  tshiozak 
    650       1.1  tshiozak 	return 0;
    651       1.1  tshiozak }
    652       1.1  tshiozak 
    653       1.1  tshiozak static int
    654       1.1  tshiozak calc_divider_settings(struct uchcom_divider *dp, uint32_t rate)
    655       1.1  tshiozak {
    656       1.1  tshiozak 	int i;
    657       1.1  tshiozak 	const struct uchcom_divider_record *rp;
    658       1.1  tshiozak 	uint32_t div, rem, mod;
    659       1.1  tshiozak 
    660       1.1  tshiozak 	/* find record */
    661       1.1  tshiozak 	for (i=0; i<NUM_DIVIDERS; i++) {
    662       1.1  tshiozak 		if (dividers[i].dvr_high >= rate &&
    663       1.1  tshiozak 		    dividers[i].dvr_low <= rate) {
    664       1.1  tshiozak 			rp = &dividers[i];
    665       1.1  tshiozak 			goto found;
    666       1.1  tshiozak 		}
    667       1.1  tshiozak 	}
    668       1.1  tshiozak 	return -1;
    669       1.1  tshiozak 
    670       1.1  tshiozak found:
    671       1.1  tshiozak 	dp->dv_prescaler = rp->dvr_divider.dv_prescaler;
    672       1.1  tshiozak 	if (rp->dvr_base_clock == UCHCOM_BASE_UNKNOWN)
    673       1.1  tshiozak 		dp->dv_div = rp->dvr_divider.dv_div;
    674       1.1  tshiozak 	else {
    675       1.1  tshiozak 		div = rp->dvr_base_clock / rate;
    676       1.1  tshiozak 		rem = rp->dvr_base_clock % rate;
    677       1.1  tshiozak 		if (div==0 || div>=0xFF)
    678       1.1  tshiozak 			return -1;
    679       1.1  tshiozak 		if ((rem<<1) >= rate)
    680       1.1  tshiozak 			div += 1;
    681       1.1  tshiozak 		dp->dv_div = (uint8_t)-div;
    682       1.1  tshiozak 	}
    683       1.1  tshiozak 
    684       1.1  tshiozak 	mod = UCHCOM_BPS_MOD_BASE/rate + UCHCOM_BPS_MOD_BASE_OFS;
    685       1.1  tshiozak 	mod = mod + mod/2;
    686       1.1  tshiozak 
    687       1.1  tshiozak 	dp->dv_mod = mod / 0x100;
    688       1.1  tshiozak 
    689       1.1  tshiozak 	return 0;
    690       1.1  tshiozak }
    691       1.1  tshiozak 
    692       1.1  tshiozak static int
    693       1.1  tshiozak set_dte_rate(struct uchcom_softc *sc, uint32_t rate)
    694       1.1  tshiozak {
    695       1.1  tshiozak 	usbd_status err;
    696       1.1  tshiozak 	struct uchcom_divider dv;
    697       1.1  tshiozak 
    698       1.1  tshiozak 	if (calc_divider_settings(&dv, rate))
    699       1.1  tshiozak 		return EINVAL;
    700       1.1  tshiozak 
    701       1.1  tshiozak 	if ((err = write_reg(sc,
    702       1.1  tshiozak 			     UCHCOM_REG_BPS_PRE, dv.dv_prescaler,
    703       1.1  tshiozak 			     UCHCOM_REG_BPS_DIV, dv.dv_div)) ||
    704       1.1  tshiozak 	    (err = write_reg(sc,
    705       1.1  tshiozak 			     UCHCOM_REG_BPS_MOD, dv.dv_mod,
    706       1.1  tshiozak 			     UCHCOM_REG_BPS_PAD, 0))) {
    707       1.6      cube 		aprint_error_dev(sc->sc_dev, "cannot set DTE rate: %s\n",
    708       1.6      cube 		    usbd_errstr(err));
    709       1.1  tshiozak 		return EIO;
    710       1.1  tshiozak 	}
    711       1.1  tshiozak 
    712       1.1  tshiozak 	return 0;
    713       1.1  tshiozak }
    714       1.1  tshiozak 
    715       1.1  tshiozak static int
    716       1.1  tshiozak set_line_control(struct uchcom_softc *sc, tcflag_t cflag)
    717       1.1  tshiozak {
    718       1.1  tshiozak 	usbd_status err;
    719       1.4        ad 	uint8_t lcr1val = 0, lcr2val = 0;
    720       1.1  tshiozak 
    721       1.4        ad 	err = read_reg(sc, UCHCOM_REG_LCR1, &lcr1val, UCHCOM_REG_LCR2, &lcr2val);
    722       1.1  tshiozak 	if (err) {
    723       1.6      cube 		aprint_error_dev(sc->sc_dev, "cannot get LCR: %s\n",
    724       1.6      cube 		    usbd_errstr(err));
    725       1.1  tshiozak 		return EIO;
    726       1.1  tshiozak 	}
    727       1.1  tshiozak 
    728       1.4        ad 	lcr1val &= ~UCHCOM_LCR1_MASK;
    729       1.4        ad 	lcr2val &= ~UCHCOM_LCR2_MASK;
    730       1.1  tshiozak 
    731       1.1  tshiozak 	/*
    732       1.1  tshiozak 	 * XXX: it is difficult to handle the line control appropriately:
    733       1.1  tshiozak 	 *   - CS8, !CSTOPB and any parity mode seems ok, but
    734       1.1  tshiozak 	 *   - the chip doesn't have the function to calculate parity
    735       1.1  tshiozak 	 *     in !CS8 mode.
    736       1.1  tshiozak 	 *   - it is unclear that the chip supports CS5,6 mode.
    737       1.1  tshiozak 	 *   - it is unclear how to handle stop bits.
    738       1.1  tshiozak 	 */
    739       1.1  tshiozak 
    740       1.1  tshiozak 	switch (ISSET(cflag, CSIZE)) {
    741       1.1  tshiozak 	case CS5:
    742       1.1  tshiozak 	case CS6:
    743       1.1  tshiozak 	case CS7:
    744       1.1  tshiozak 		return EINVAL;
    745       1.1  tshiozak 	case CS8:
    746       1.1  tshiozak 		break;
    747       1.1  tshiozak 	}
    748       1.1  tshiozak 
    749       1.1  tshiozak 	if (ISSET(cflag, PARENB)) {
    750       1.4        ad 		lcr1val |= UCHCOM_LCR1_PARENB;
    751       1.1  tshiozak 		if (ISSET(cflag, PARODD))
    752       1.4        ad 			lcr2val |= UCHCOM_LCR2_PARODD;
    753       1.1  tshiozak 		else
    754       1.4        ad 			lcr2val |= UCHCOM_LCR2_PAREVEN;
    755       1.1  tshiozak 	}
    756       1.1  tshiozak 
    757       1.4        ad 	err = write_reg(sc, UCHCOM_REG_LCR1, lcr1val, UCHCOM_REG_LCR2, lcr2val);
    758       1.1  tshiozak 	if (err) {
    759       1.6      cube 		aprint_error_dev(sc->sc_dev, "cannot set LCR: %s\n",
    760       1.6      cube 		    usbd_errstr(err));
    761       1.1  tshiozak 		return EIO;
    762       1.1  tshiozak 	}
    763       1.1  tshiozak 
    764       1.1  tshiozak 	return 0;
    765       1.1  tshiozak }
    766       1.1  tshiozak 
    767       1.1  tshiozak static int
    768       1.1  tshiozak clear_chip(struct uchcom_softc *sc)
    769       1.1  tshiozak {
    770       1.1  tshiozak 	usbd_status err;
    771       1.1  tshiozak 
    772      1.10    dyoung 	DPRINTF(("%s: clear\n", device_xname(sc->sc_dev)));
    773       1.1  tshiozak 	err = generic_control_out(sc, UCHCOM_REQ_RESET, 0, 0);
    774       1.1  tshiozak 	if (err) {
    775       1.6      cube 		aprint_error_dev(sc->sc_dev, "cannot clear: %s\n",
    776       1.6      cube 		    usbd_errstr(err));
    777       1.1  tshiozak 		return EIO;
    778       1.1  tshiozak 	}
    779       1.1  tshiozak 
    780       1.1  tshiozak 	return 0;
    781       1.1  tshiozak }
    782       1.1  tshiozak 
    783       1.1  tshiozak static int
    784       1.1  tshiozak reset_chip(struct uchcom_softc *sc)
    785       1.1  tshiozak {
    786       1.1  tshiozak 	usbd_status err;
    787       1.4        ad 	uint8_t lcr1val, lcr2val, pre, div, mod;
    788       1.1  tshiozak 	uint16_t val=0, idx=0;
    789       1.1  tshiozak 
    790       1.4        ad 	err = read_reg(sc, UCHCOM_REG_LCR1, &lcr1val, UCHCOM_REG_LCR2, &lcr2val);
    791       1.1  tshiozak 	if (err)
    792       1.1  tshiozak 		goto failed;
    793       1.1  tshiozak 
    794       1.1  tshiozak 	err = read_reg(sc, UCHCOM_REG_BPS_PRE, &pre, UCHCOM_REG_BPS_DIV, &div);
    795       1.1  tshiozak 	if (err)
    796       1.1  tshiozak 		goto failed;
    797       1.1  tshiozak 
    798       1.1  tshiozak 	err = read_reg(sc, UCHCOM_REG_BPS_MOD, &mod, UCHCOM_REG_BPS_PAD, NULL);
    799       1.1  tshiozak 	if (err)
    800       1.1  tshiozak 		goto failed;
    801       1.1  tshiozak 
    802       1.4        ad 	val |= (uint16_t)(lcr1val&0xF0) << 8;
    803       1.1  tshiozak 	val |= 0x01;
    804       1.4        ad 	val |= (uint16_t)(lcr2val&0x0F) << 8;
    805       1.1  tshiozak 	val |= 0x02;
    806       1.1  tshiozak 	idx |= pre & 0x07;
    807       1.1  tshiozak 	val |= 0x04;
    808       1.1  tshiozak 	idx |= (uint16_t)div << 8;
    809       1.1  tshiozak 	val |= 0x08;
    810       1.1  tshiozak 	idx |= mod & 0xF8;
    811       1.1  tshiozak 	val |= 0x10;
    812       1.1  tshiozak 
    813       1.1  tshiozak 	DPRINTF(("%s: reset v=0x%04X, i=0x%04X\n",
    814      1.10    dyoung 		 device_xname(sc->sc_dev), val, idx));
    815       1.1  tshiozak 
    816       1.1  tshiozak 	err = generic_control_out(sc, UCHCOM_REQ_RESET, val, idx);
    817       1.1  tshiozak 	if (err)
    818       1.1  tshiozak 		goto failed;
    819       1.1  tshiozak 
    820       1.1  tshiozak 	return 0;
    821       1.1  tshiozak 
    822       1.1  tshiozak failed:
    823       1.1  tshiozak 	printf("%s: cannot reset: %s\n",
    824      1.10    dyoung 	       device_xname(sc->sc_dev), usbd_errstr(err));
    825       1.1  tshiozak 	return EIO;
    826       1.1  tshiozak }
    827       1.1  tshiozak 
    828       1.1  tshiozak static int
    829       1.1  tshiozak setup_comm(struct uchcom_softc *sc)
    830       1.1  tshiozak {
    831       1.1  tshiozak 	int ret;
    832       1.1  tshiozak 
    833       1.1  tshiozak 	ret = update_version(sc);
    834       1.1  tshiozak 	if (ret)
    835       1.1  tshiozak 		return ret;
    836       1.1  tshiozak 
    837       1.1  tshiozak 	ret = clear_chip(sc);
    838       1.1  tshiozak 	if (ret)
    839       1.1  tshiozak 		return ret;
    840       1.1  tshiozak 
    841       1.1  tshiozak 	ret = set_dte_rate(sc, TTYDEF_SPEED);
    842       1.1  tshiozak 	if (ret)
    843       1.1  tshiozak 		return ret;
    844       1.1  tshiozak 
    845       1.1  tshiozak 	ret = set_line_control(sc, CS8);
    846       1.1  tshiozak 	if (ret)
    847       1.1  tshiozak 		return ret;
    848       1.1  tshiozak 
    849       1.1  tshiozak 	ret = update_status(sc);
    850       1.1  tshiozak 	if (ret)
    851       1.1  tshiozak 		return ret;
    852       1.1  tshiozak 
    853       1.1  tshiozak 	ret = reset_chip(sc);
    854       1.1  tshiozak 	if (ret)
    855       1.1  tshiozak 		return ret;
    856       1.1  tshiozak 
    857       1.1  tshiozak 	ret = set_dte_rate(sc, TTYDEF_SPEED); /* XXX */
    858       1.1  tshiozak 	if (ret)
    859       1.1  tshiozak 		return ret;
    860       1.1  tshiozak 
    861       1.1  tshiozak 	sc->sc_dtr = sc->sc_rts = 1;
    862       1.1  tshiozak 	ret = set_dtrrts(sc, sc->sc_dtr, sc->sc_rts);
    863       1.1  tshiozak 	if (ret)
    864       1.1  tshiozak 		return ret;
    865       1.1  tshiozak 
    866       1.1  tshiozak 	return 0;
    867       1.1  tshiozak }
    868       1.1  tshiozak 
    869       1.1  tshiozak static int
    870       1.1  tshiozak setup_intr_pipe(struct uchcom_softc *sc)
    871       1.1  tshiozak {
    872       1.1  tshiozak 	usbd_status err;
    873       1.1  tshiozak 
    874       1.1  tshiozak 	if (sc->sc_intr_endpoint != -1 && sc->sc_intr_pipe == NULL) {
    875  1.13.6.1     skrll 		sc->sc_intr_buf = kmem_alloc(sc->sc_intr_size, KM_SLEEP);
    876       1.1  tshiozak 		err = usbd_open_pipe_intr(sc->sc_iface,
    877       1.1  tshiozak 					  sc->sc_intr_endpoint,
    878       1.1  tshiozak 					  USBD_SHORT_XFER_OK,
    879       1.1  tshiozak 					  &sc->sc_intr_pipe, sc,
    880       1.1  tshiozak 					  sc->sc_intr_buf,
    881       1.1  tshiozak 					  sc->sc_intr_size,
    882       1.1  tshiozak 					  uchcom_intr, USBD_DEFAULT_INTERVAL);
    883       1.1  tshiozak 		if (err) {
    884       1.6      cube 			aprint_error_dev(sc->sc_dev,
    885       1.6      cube 			    "cannot open interrupt pipe: %s\n",
    886       1.6      cube 			    usbd_errstr(err));
    887       1.1  tshiozak 			return EIO;
    888       1.1  tshiozak 		}
    889       1.1  tshiozak 	}
    890       1.1  tshiozak 	return 0;
    891       1.1  tshiozak }
    892       1.1  tshiozak 
    893       1.1  tshiozak static void
    894       1.1  tshiozak close_intr_pipe(struct uchcom_softc *sc)
    895       1.1  tshiozak {
    896       1.1  tshiozak 	usbd_status err;
    897       1.1  tshiozak 
    898       1.1  tshiozak 	if (sc->sc_dying)
    899       1.1  tshiozak 		return;
    900       1.1  tshiozak 
    901       1.1  tshiozak 	if (sc->sc_intr_pipe != NULL) {
    902       1.1  tshiozak 		err = usbd_abort_pipe(sc->sc_intr_pipe);
    903       1.1  tshiozak 		if (err)
    904       1.6      cube 			aprint_error_dev(sc->sc_dev,
    905       1.6      cube 			    "abort interrupt pipe failed: %s\n",
    906       1.6      cube 			    usbd_errstr(err));
    907       1.1  tshiozak 		err = usbd_close_pipe(sc->sc_intr_pipe);
    908       1.1  tshiozak 		if (err)
    909       1.6      cube 			aprint_error_dev(sc->sc_dev,
    910       1.6      cube 			    "close interrupt pipe failed: %s\n",
    911       1.6      cube 			    usbd_errstr(err));
    912  1.13.6.1     skrll 		kmem_free(sc->sc_intr_buf, sc->sc_intr_size);
    913       1.1  tshiozak 		sc->sc_intr_pipe = NULL;
    914       1.1  tshiozak 	}
    915       1.1  tshiozak }
    916       1.1  tshiozak 
    917       1.1  tshiozak 
    918       1.1  tshiozak /* ----------------------------------------------------------------------
    919       1.1  tshiozak  * methods for ucom
    920       1.1  tshiozak  */
    921       1.1  tshiozak void
    922       1.1  tshiozak uchcom_get_status(void *arg, int portno, u_char *rlsr, u_char *rmsr)
    923       1.1  tshiozak {
    924       1.1  tshiozak 	struct uchcom_softc *sc = arg;
    925       1.1  tshiozak 
    926       1.1  tshiozak 	if (sc->sc_dying)
    927       1.1  tshiozak 		return;
    928       1.1  tshiozak 
    929       1.1  tshiozak 	*rlsr = sc->sc_lsr;
    930       1.1  tshiozak 	*rmsr = sc->sc_msr;
    931       1.1  tshiozak }
    932       1.1  tshiozak 
    933       1.1  tshiozak void
    934       1.1  tshiozak uchcom_set(void *arg, int portno, int reg, int onoff)
    935       1.1  tshiozak {
    936       1.1  tshiozak 	struct uchcom_softc *sc = arg;
    937       1.1  tshiozak 
    938       1.1  tshiozak 	if (sc->sc_dying)
    939       1.1  tshiozak 		return;
    940       1.1  tshiozak 
    941       1.1  tshiozak 	switch (reg) {
    942       1.1  tshiozak 	case UCOM_SET_DTR:
    943       1.1  tshiozak 		sc->sc_dtr = !!onoff;
    944       1.1  tshiozak 		set_dtrrts(sc, sc->sc_dtr, sc->sc_rts);
    945       1.1  tshiozak 		break;
    946       1.1  tshiozak 	case UCOM_SET_RTS:
    947       1.1  tshiozak 		sc->sc_rts = !!onoff;
    948       1.1  tshiozak 		set_dtrrts(sc, sc->sc_dtr, sc->sc_rts);
    949       1.1  tshiozak 		break;
    950       1.1  tshiozak 	case UCOM_SET_BREAK:
    951       1.1  tshiozak 		set_break(sc, onoff);
    952       1.1  tshiozak 		break;
    953       1.1  tshiozak 	}
    954       1.1  tshiozak }
    955       1.1  tshiozak 
    956       1.1  tshiozak int
    957       1.1  tshiozak uchcom_param(void *arg, int portno, struct termios *t)
    958       1.1  tshiozak {
    959       1.1  tshiozak 	struct uchcom_softc *sc = arg;
    960       1.1  tshiozak 	int ret;
    961       1.1  tshiozak 
    962       1.1  tshiozak 	if (sc->sc_dying)
    963       1.1  tshiozak 		return 0;
    964       1.1  tshiozak 
    965       1.1  tshiozak 	ret = set_line_control(sc, t->c_cflag);
    966       1.1  tshiozak 	if (ret)
    967       1.1  tshiozak 		return ret;
    968       1.1  tshiozak 
    969       1.1  tshiozak 	ret = set_dte_rate(sc, t->c_ospeed);
    970       1.1  tshiozak 	if (ret)
    971       1.1  tshiozak 		return ret;
    972       1.1  tshiozak 
    973       1.1  tshiozak 	return 0;
    974       1.1  tshiozak }
    975       1.1  tshiozak 
    976       1.1  tshiozak int
    977       1.1  tshiozak uchcom_open(void *arg, int portno)
    978       1.1  tshiozak {
    979       1.1  tshiozak 	int ret;
    980       1.1  tshiozak 	struct uchcom_softc *sc = arg;
    981       1.1  tshiozak 
    982       1.1  tshiozak 	if (sc->sc_dying)
    983       1.1  tshiozak 		return EIO;
    984       1.1  tshiozak 
    985       1.1  tshiozak 	ret = setup_intr_pipe(sc);
    986       1.1  tshiozak 	if (ret)
    987       1.1  tshiozak 		return ret;
    988       1.1  tshiozak 
    989       1.1  tshiozak 	ret = setup_comm(sc);
    990       1.1  tshiozak 	if (ret)
    991       1.1  tshiozak 		return ret;
    992       1.1  tshiozak 
    993       1.1  tshiozak 	return 0;
    994       1.1  tshiozak }
    995       1.1  tshiozak 
    996       1.1  tshiozak void
    997       1.1  tshiozak uchcom_close(void *arg, int portno)
    998       1.1  tshiozak {
    999       1.1  tshiozak 	struct uchcom_softc *sc = arg;
   1000       1.1  tshiozak 
   1001       1.1  tshiozak 	if (sc->sc_dying)
   1002       1.1  tshiozak 		return;
   1003       1.1  tshiozak 
   1004       1.1  tshiozak 	close_intr_pipe(sc);
   1005       1.1  tshiozak }
   1006       1.1  tshiozak 
   1007       1.1  tshiozak 
   1008       1.1  tshiozak /* ----------------------------------------------------------------------
   1009       1.1  tshiozak  * callback when the modem status is changed.
   1010       1.1  tshiozak  */
   1011       1.1  tshiozak void
   1012  1.13.6.2     skrll uchcom_intr(struct usbd_xfer *xfer, void * priv,
   1013       1.1  tshiozak 	    usbd_status status)
   1014       1.1  tshiozak {
   1015       1.1  tshiozak 	struct uchcom_softc *sc = priv;
   1016       1.1  tshiozak 	u_char *buf = sc->sc_intr_buf;
   1017       1.1  tshiozak 
   1018       1.1  tshiozak 	if (sc->sc_dying)
   1019       1.1  tshiozak 		return;
   1020       1.1  tshiozak 
   1021       1.1  tshiozak 	if (status != USBD_NORMAL_COMPLETION) {
   1022       1.1  tshiozak 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1023       1.1  tshiozak 			return;
   1024       1.1  tshiozak 
   1025       1.1  tshiozak 		DPRINTF(("%s: abnormal status: %s\n",
   1026      1.10    dyoung 			 device_xname(sc->sc_dev), usbd_errstr(status)));
   1027       1.1  tshiozak 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
   1028       1.1  tshiozak 		return;
   1029       1.1  tshiozak 	}
   1030       1.1  tshiozak 	DPRINTF(("%s: intr: 0x%02X 0x%02X 0x%02X 0x%02X "
   1031       1.1  tshiozak 		 "0x%02X 0x%02X 0x%02X 0x%02X\n",
   1032      1.10    dyoung 		 device_xname(sc->sc_dev),
   1033       1.1  tshiozak 		 (unsigned)buf[0], (unsigned)buf[1],
   1034       1.1  tshiozak 		 (unsigned)buf[2], (unsigned)buf[3],
   1035       1.1  tshiozak 		 (unsigned)buf[4], (unsigned)buf[5],
   1036       1.1  tshiozak 		 (unsigned)buf[6], (unsigned)buf[7]));
   1037       1.1  tshiozak 
   1038       1.1  tshiozak 	convert_status(sc, buf[UCHCOM_INTR_STAT1]);
   1039       1.6      cube 	ucom_status_change(device_private(sc->sc_subdev));
   1040       1.1  tshiozak }
   1041