xref: /freebsd-src/sys/dev/usb/serial/ucycom.c (revision e738085b94631f90e21a49852538ac95974baf44)
102ac6454SAndrew Thompson #include <sys/cdefs.h>
202ac6454SAndrew Thompson /*-
34d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
4718cf2ccSPedro F. Giffuni  *
5*e738085bSDag-Erling Smørgrav  * Copyright (c) 2004 Dag-Erling Smørgrav
602ac6454SAndrew Thompson  * All rights reserved.
702ac6454SAndrew Thompson  *
802ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
902ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1002ac6454SAndrew Thompson  * are met:
1102ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1202ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer
1302ac6454SAndrew Thompson  *    in this position and unchanged.
1402ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1502ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1602ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1702ac6454SAndrew Thompson  * 3. The name of the author may not be used to endorse or promote products
1802ac6454SAndrew Thompson  *    derived from this software without specific prior written permission.
1902ac6454SAndrew Thompson  *
2002ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2102ac6454SAndrew Thompson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2202ac6454SAndrew Thompson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2302ac6454SAndrew Thompson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2402ac6454SAndrew Thompson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2502ac6454SAndrew Thompson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2602ac6454SAndrew Thompson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2702ac6454SAndrew Thompson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2802ac6454SAndrew Thompson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2902ac6454SAndrew Thompson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3002ac6454SAndrew Thompson  */
3102ac6454SAndrew Thompson 
3202ac6454SAndrew Thompson /*
3302ac6454SAndrew Thompson  * Device driver for Cypress CY7C637xx and CY7C640/1xx series USB to
3402ac6454SAndrew Thompson  * RS232 bridges.
3502ac6454SAndrew Thompson  */
3602ac6454SAndrew Thompson 
37ed6d949aSAndrew Thompson #include <sys/stdint.h>
38ed6d949aSAndrew Thompson #include <sys/stddef.h>
39ed6d949aSAndrew Thompson #include <sys/param.h>
40ed6d949aSAndrew Thompson #include <sys/queue.h>
41ed6d949aSAndrew Thompson #include <sys/types.h>
42ed6d949aSAndrew Thompson #include <sys/systm.h>
43ed6d949aSAndrew Thompson #include <sys/kernel.h>
44ed6d949aSAndrew Thompson #include <sys/bus.h>
45ed6d949aSAndrew Thompson #include <sys/module.h>
46ed6d949aSAndrew Thompson #include <sys/lock.h>
47ed6d949aSAndrew Thompson #include <sys/mutex.h>
48ed6d949aSAndrew Thompson #include <sys/condvar.h>
49ed6d949aSAndrew Thompson #include <sys/sysctl.h>
50ed6d949aSAndrew Thompson #include <sys/sx.h>
51ed6d949aSAndrew Thompson #include <sys/unistd.h>
52ed6d949aSAndrew Thompson #include <sys/callout.h>
53ed6d949aSAndrew Thompson #include <sys/malloc.h>
54ed6d949aSAndrew Thompson #include <sys/priv.h>
55ed6d949aSAndrew Thompson 
56eead9017SVladimir Kondratyev #include <dev/hid/hid.h>
57eead9017SVladimir Kondratyev 
5802ac6454SAndrew Thompson #include <dev/usb/usb.h>
59ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
60ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
6102ac6454SAndrew Thompson #include <dev/usb/usbhid.h>
62ed6d949aSAndrew Thompson #include "usbdevs.h"
6302ac6454SAndrew Thompson 
64a593f6b8SAndrew Thompson #define	USB_DEBUG_VAR usb_debug
6502ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
6602ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
6702ac6454SAndrew Thompson 
6802ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h>
6902ac6454SAndrew Thompson 
7002ac6454SAndrew Thompson #define	UCYCOM_MAX_IOLEN	(1024 + 2)	/* bytes */
7102ac6454SAndrew Thompson 
7202ac6454SAndrew Thompson #define	UCYCOM_IFACE_INDEX	0
7302ac6454SAndrew Thompson 
7402ac6454SAndrew Thompson enum {
7502ac6454SAndrew Thompson 	UCYCOM_CTRL_RD,
7602ac6454SAndrew Thompson 	UCYCOM_INTR_RD,
7702ac6454SAndrew Thompson 	UCYCOM_N_TRANSFER,
7802ac6454SAndrew Thompson };
7902ac6454SAndrew Thompson 
8002ac6454SAndrew Thompson struct ucycom_softc {
81760bc48eSAndrew Thompson 	struct ucom_super_softc sc_super_ucom;
82760bc48eSAndrew Thompson 	struct ucom_softc sc_ucom;
8302ac6454SAndrew Thompson 
84760bc48eSAndrew Thompson 	struct usb_device *sc_udev;
85760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UCYCOM_N_TRANSFER];
86deefe583SAndrew Thompson 	struct mtx sc_mtx;
8702ac6454SAndrew Thompson 
8802ac6454SAndrew Thompson 	uint32_t sc_model;
8902ac6454SAndrew Thompson #define	MODEL_CY7C63743		0x63743
9002ac6454SAndrew Thompson #define	MODEL_CY7C64013		0x64013
9102ac6454SAndrew Thompson 
9202ac6454SAndrew Thompson 	uint16_t sc_flen;		/* feature report length */
9302ac6454SAndrew Thompson 	uint16_t sc_ilen;		/* input report length */
9402ac6454SAndrew Thompson 	uint16_t sc_olen;		/* output report length */
9502ac6454SAndrew Thompson 
9602ac6454SAndrew Thompson 	uint8_t	sc_fid;			/* feature report id */
9702ac6454SAndrew Thompson 	uint8_t	sc_iid;			/* input report id */
9802ac6454SAndrew Thompson 	uint8_t	sc_oid;			/* output report id */
9902ac6454SAndrew Thompson 	uint8_t	sc_cfg;
10002ac6454SAndrew Thompson #define	UCYCOM_CFG_RESET	0x80
10102ac6454SAndrew Thompson #define	UCYCOM_CFG_PARODD	0x20
10202ac6454SAndrew Thompson #define	UCYCOM_CFG_PAREN	0x10
10302ac6454SAndrew Thompson #define	UCYCOM_CFG_STOPB	0x08
10402ac6454SAndrew Thompson #define	UCYCOM_CFG_DATAB	0x03
10502ac6454SAndrew Thompson 	uint8_t	sc_ist;			/* status flags from last input */
10602ac6454SAndrew Thompson 	uint8_t	sc_iface_no;
10702ac6454SAndrew Thompson 	uint8_t	sc_temp_cfg[32];
10802ac6454SAndrew Thompson };
10902ac6454SAndrew Thompson 
11002ac6454SAndrew Thompson /* prototypes */
11102ac6454SAndrew Thompson 
11202ac6454SAndrew Thompson static device_probe_t ucycom_probe;
11302ac6454SAndrew Thompson static device_attach_t ucycom_attach;
11402ac6454SAndrew Thompson static device_detach_t ucycom_detach;
115c01fc06eSHans Petter Selasky static void ucycom_free_softc(struct ucycom_softc *);
11602ac6454SAndrew Thompson 
117e0a69b51SAndrew Thompson static usb_callback_t ucycom_ctrl_write_callback;
118e0a69b51SAndrew Thompson static usb_callback_t ucycom_intr_read_callback;
11902ac6454SAndrew Thompson 
1205805d178SHans Petter Selasky static void	ucycom_free(struct ucom_softc *);
121760bc48eSAndrew Thompson static void	ucycom_cfg_open(struct ucom_softc *);
122760bc48eSAndrew Thompson static void	ucycom_start_read(struct ucom_softc *);
123760bc48eSAndrew Thompson static void	ucycom_stop_read(struct ucom_softc *);
124760bc48eSAndrew Thompson static void	ucycom_start_write(struct ucom_softc *);
125760bc48eSAndrew Thompson static void	ucycom_stop_write(struct ucom_softc *);
12602ac6454SAndrew Thompson static void	ucycom_cfg_write(struct ucycom_softc *, uint32_t, uint8_t);
127760bc48eSAndrew Thompson static int	ucycom_pre_param(struct ucom_softc *, struct termios *);
128760bc48eSAndrew Thompson static void	ucycom_cfg_param(struct ucom_softc *, struct termios *);
129655dc9d0SAndrew Thompson static void	ucycom_poll(struct ucom_softc *ucom);
13002ac6454SAndrew Thompson 
131760bc48eSAndrew Thompson static const struct usb_config ucycom_config[UCYCOM_N_TRANSFER] = {
13202ac6454SAndrew Thompson 	[UCYCOM_CTRL_RD] = {
13302ac6454SAndrew Thompson 		.type = UE_CONTROL,
13402ac6454SAndrew Thompson 		.endpoint = 0x00,	/* Control pipe */
13502ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
136760bc48eSAndrew Thompson 		.bufsize = (sizeof(struct usb_device_request) + UCYCOM_MAX_IOLEN),
1374eae601eSAndrew Thompson 		.callback = &ucycom_ctrl_write_callback,
1384eae601eSAndrew Thompson 		.timeout = 1000,	/* 1 second */
13902ac6454SAndrew Thompson 	},
14002ac6454SAndrew Thompson 
14102ac6454SAndrew Thompson 	[UCYCOM_INTR_RD] = {
14202ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
14302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
14402ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
1454eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
1464eae601eSAndrew Thompson 		.bufsize = UCYCOM_MAX_IOLEN,
1474eae601eSAndrew Thompson 		.callback = &ucycom_intr_read_callback,
14802ac6454SAndrew Thompson 	},
14902ac6454SAndrew Thompson };
15002ac6454SAndrew Thompson 
151760bc48eSAndrew Thompson static const struct ucom_callback ucycom_callback = {
152a593f6b8SAndrew Thompson 	.ucom_cfg_param = &ucycom_cfg_param,
153a593f6b8SAndrew Thompson 	.ucom_cfg_open = &ucycom_cfg_open,
154a593f6b8SAndrew Thompson 	.ucom_pre_param = &ucycom_pre_param,
155a593f6b8SAndrew Thompson 	.ucom_start_read = &ucycom_start_read,
156a593f6b8SAndrew Thompson 	.ucom_stop_read = &ucycom_stop_read,
157a593f6b8SAndrew Thompson 	.ucom_start_write = &ucycom_start_write,
158a593f6b8SAndrew Thompson 	.ucom_stop_write = &ucycom_stop_write,
159655dc9d0SAndrew Thompson 	.ucom_poll = &ucycom_poll,
1605805d178SHans Petter Selasky 	.ucom_free = &ucycom_free,
16102ac6454SAndrew Thompson };
16202ac6454SAndrew Thompson 
16302ac6454SAndrew Thompson static device_method_t ucycom_methods[] = {
16402ac6454SAndrew Thompson 	DEVMETHOD(device_probe, ucycom_probe),
16502ac6454SAndrew Thompson 	DEVMETHOD(device_attach, ucycom_attach),
16602ac6454SAndrew Thompson 	DEVMETHOD(device_detach, ucycom_detach),
1675805d178SHans Petter Selasky 	DEVMETHOD_END
16802ac6454SAndrew Thompson };
16902ac6454SAndrew Thompson 
17002ac6454SAndrew Thompson static driver_t ucycom_driver = {
17102ac6454SAndrew Thompson 	.name = "ucycom",
17202ac6454SAndrew Thompson 	.methods = ucycom_methods,
17302ac6454SAndrew Thompson 	.size = sizeof(struct ucycom_softc),
17402ac6454SAndrew Thompson };
17502ac6454SAndrew Thompson 
17602ac6454SAndrew Thompson /*
17702ac6454SAndrew Thompson  * Supported devices
17802ac6454SAndrew Thompson  */
179f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID ucycom_devs[] = {
18002ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EARTHMATE, MODEL_CY7C64013)},
18102ac6454SAndrew Thompson };
18202ac6454SAndrew Thompson 
183bc9372d7SJohn Baldwin DRIVER_MODULE(ucycom, uhub, ucycom_driver, NULL, NULL);
184f809f280SWarner Losh MODULE_DEPEND(ucycom, ucom, 1, 1, 1);
185f809f280SWarner Losh MODULE_DEPEND(ucycom, usb, 1, 1, 1);
18667de2db2SVladimir Kondratyev MODULE_DEPEND(ucycom, hid, 1, 1, 1);
187f809f280SWarner Losh MODULE_VERSION(ucycom, 1);
188f809f280SWarner Losh USB_PNP_HOST_INFO(ucycom_devs);
189f809f280SWarner Losh 
19002ac6454SAndrew Thompson #define	UCYCOM_DEFAULT_RATE	 4800
19102ac6454SAndrew Thompson #define	UCYCOM_DEFAULT_CFG	 0x03	/* N-8-1 */
19202ac6454SAndrew Thompson 
19302ac6454SAndrew Thompson static int
ucycom_probe(device_t dev)19402ac6454SAndrew Thompson ucycom_probe(device_t dev)
19502ac6454SAndrew Thompson {
196760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
19702ac6454SAndrew Thompson 
198f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
19902ac6454SAndrew Thompson 		return (ENXIO);
20002ac6454SAndrew Thompson 	}
20102ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != 0) {
20202ac6454SAndrew Thompson 		return (ENXIO);
20302ac6454SAndrew Thompson 	}
20402ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != UCYCOM_IFACE_INDEX) {
20502ac6454SAndrew Thompson 		return (ENXIO);
20602ac6454SAndrew Thompson 	}
207a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(ucycom_devs, sizeof(ucycom_devs), uaa));
20802ac6454SAndrew Thompson }
20902ac6454SAndrew Thompson 
21002ac6454SAndrew Thompson static int
ucycom_attach(device_t dev)21102ac6454SAndrew Thompson ucycom_attach(device_t dev)
21202ac6454SAndrew Thompson {
213760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
21402ac6454SAndrew Thompson 	struct ucycom_softc *sc = device_get_softc(dev);
21502ac6454SAndrew Thompson 	void *urd_ptr = NULL;
21602ac6454SAndrew Thompson 	int32_t error;
21702ac6454SAndrew Thompson 	uint16_t urd_len;
21802ac6454SAndrew Thompson 	uint8_t iface_index;
21902ac6454SAndrew Thompson 
22002ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
22102ac6454SAndrew Thompson 
222a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
223deefe583SAndrew Thompson 	mtx_init(&sc->sc_mtx, "ucycom", NULL, MTX_DEF);
2245805d178SHans Petter Selasky 	ucom_ref(&sc->sc_super_ucom);
22502ac6454SAndrew Thompson 
22602ac6454SAndrew Thompson 	DPRINTF("\n");
22702ac6454SAndrew Thompson 
22802ac6454SAndrew Thompson 	/* get chip model */
22902ac6454SAndrew Thompson 	sc->sc_model = USB_GET_DRIVER_INFO(uaa);
23002ac6454SAndrew Thompson 	if (sc->sc_model == 0) {
23102ac6454SAndrew Thompson 		device_printf(dev, "unsupported device\n");
23202ac6454SAndrew Thompson 		goto detach;
23302ac6454SAndrew Thompson 	}
23402ac6454SAndrew Thompson 	device_printf(dev, "Cypress CY7C%X USB to RS232 bridge\n", sc->sc_model);
23502ac6454SAndrew Thompson 
23602ac6454SAndrew Thompson 	/* get report descriptor */
23702ac6454SAndrew Thompson 
238a593f6b8SAndrew Thompson 	error = usbd_req_get_hid_desc(uaa->device, NULL,
23902ac6454SAndrew Thompson 	    &urd_ptr, &urd_len, M_USBDEV,
24002ac6454SAndrew Thompson 	    UCYCOM_IFACE_INDEX);
24102ac6454SAndrew Thompson 
24202ac6454SAndrew Thompson 	if (error) {
24302ac6454SAndrew Thompson 		device_printf(dev, "failed to get report "
24402ac6454SAndrew Thompson 		    "descriptor: %s\n",
245a593f6b8SAndrew Thompson 		    usbd_errstr(error));
24602ac6454SAndrew Thompson 		goto detach;
24702ac6454SAndrew Thompson 	}
24802ac6454SAndrew Thompson 	/* get report sizes */
24902ac6454SAndrew Thompson 
250eead9017SVladimir Kondratyev 	sc->sc_flen = hid_report_size_max(urd_ptr, urd_len, hid_feature, &sc->sc_fid);
251eead9017SVladimir Kondratyev 	sc->sc_ilen = hid_report_size_max(urd_ptr, urd_len, hid_input, &sc->sc_iid);
252eead9017SVladimir Kondratyev 	sc->sc_olen = hid_report_size_max(urd_ptr, urd_len, hid_output, &sc->sc_oid);
25302ac6454SAndrew Thompson 
25402ac6454SAndrew Thompson 	if ((sc->sc_ilen > UCYCOM_MAX_IOLEN) || (sc->sc_ilen < 1) ||
25502ac6454SAndrew Thompson 	    (sc->sc_olen > UCYCOM_MAX_IOLEN) || (sc->sc_olen < 2) ||
25602ac6454SAndrew Thompson 	    (sc->sc_flen > UCYCOM_MAX_IOLEN) || (sc->sc_flen < 5)) {
25702ac6454SAndrew Thompson 		device_printf(dev, "invalid report size i=%d, o=%d, f=%d, max=%d\n",
25802ac6454SAndrew Thompson 		    sc->sc_ilen, sc->sc_olen, sc->sc_flen,
25902ac6454SAndrew Thompson 		    UCYCOM_MAX_IOLEN);
26002ac6454SAndrew Thompson 		goto detach;
26102ac6454SAndrew Thompson 	}
26202ac6454SAndrew Thompson 	sc->sc_iface_no = uaa->info.bIfaceNum;
26302ac6454SAndrew Thompson 
26402ac6454SAndrew Thompson 	iface_index = UCYCOM_IFACE_INDEX;
265a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device, &iface_index,
26602ac6454SAndrew Thompson 	    sc->sc_xfer, ucycom_config, UCYCOM_N_TRANSFER,
267deefe583SAndrew Thompson 	    sc, &sc->sc_mtx);
26802ac6454SAndrew Thompson 	if (error) {
26902ac6454SAndrew Thompson 		device_printf(dev, "allocating USB "
270767cb2e2SAndrew Thompson 		    "transfers failed\n");
27102ac6454SAndrew Thompson 		goto detach;
27202ac6454SAndrew Thompson 	}
273a593f6b8SAndrew Thompson 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
274deefe583SAndrew Thompson 	    &ucycom_callback, &sc->sc_mtx);
27502ac6454SAndrew Thompson 	if (error) {
27602ac6454SAndrew Thompson 		goto detach;
27702ac6454SAndrew Thompson 	}
2786416c259SNick Hibma 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
2796416c259SNick Hibma 
28002ac6454SAndrew Thompson 	if (urd_ptr) {
28102ac6454SAndrew Thompson 		free(urd_ptr, M_USBDEV);
28202ac6454SAndrew Thompson 	}
2836416c259SNick Hibma 
28402ac6454SAndrew Thompson 	return (0);			/* success */
28502ac6454SAndrew Thompson 
28602ac6454SAndrew Thompson detach:
28702ac6454SAndrew Thompson 	if (urd_ptr) {
28802ac6454SAndrew Thompson 		free(urd_ptr, M_USBDEV);
28902ac6454SAndrew Thompson 	}
29002ac6454SAndrew Thompson 	ucycom_detach(dev);
29102ac6454SAndrew Thompson 	return (ENXIO);
29202ac6454SAndrew Thompson }
29302ac6454SAndrew Thompson 
29402ac6454SAndrew Thompson static int
ucycom_detach(device_t dev)29502ac6454SAndrew Thompson ucycom_detach(device_t dev)
29602ac6454SAndrew Thompson {
29702ac6454SAndrew Thompson 	struct ucycom_softc *sc = device_get_softc(dev);
29802ac6454SAndrew Thompson 
299015bb88fSNick Hibma 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
300a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, UCYCOM_N_TRANSFER);
30102ac6454SAndrew Thompson 
302c01fc06eSHans Petter Selasky 	device_claim_softc(dev);
303c01fc06eSHans Petter Selasky 
304c01fc06eSHans Petter Selasky 	ucycom_free_softc(sc);
305c01fc06eSHans Petter Selasky 
30602ac6454SAndrew Thompson 	return (0);
30702ac6454SAndrew Thompson }
30802ac6454SAndrew Thompson 
3095805d178SHans Petter Selasky UCOM_UNLOAD_DRAIN(ucycom);
3105805d178SHans Petter Selasky 
3115805d178SHans Petter Selasky static void
ucycom_free_softc(struct ucycom_softc * sc)312c01fc06eSHans Petter Selasky ucycom_free_softc(struct ucycom_softc *sc)
3135805d178SHans Petter Selasky {
3145805d178SHans Petter Selasky 	if (ucom_unref(&sc->sc_super_ucom)) {
3155805d178SHans Petter Selasky 		mtx_destroy(&sc->sc_mtx);
316c01fc06eSHans Petter Selasky 		device_free_softc(sc);
3175805d178SHans Petter Selasky 	}
3185805d178SHans Petter Selasky }
3195805d178SHans Petter Selasky 
3205805d178SHans Petter Selasky static void
ucycom_free(struct ucom_softc * ucom)3215805d178SHans Petter Selasky ucycom_free(struct ucom_softc *ucom)
3225805d178SHans Petter Selasky {
323c01fc06eSHans Petter Selasky 	ucycom_free_softc(ucom->sc_parent);
3245805d178SHans Petter Selasky }
3255805d178SHans Petter Selasky 
32602ac6454SAndrew Thompson static void
ucycom_cfg_open(struct ucom_softc * ucom)327760bc48eSAndrew Thompson ucycom_cfg_open(struct ucom_softc *ucom)
32802ac6454SAndrew Thompson {
32902ac6454SAndrew Thompson 	struct ucycom_softc *sc = ucom->sc_parent;
33002ac6454SAndrew Thompson 
33102ac6454SAndrew Thompson 	/* set default configuration */
33202ac6454SAndrew Thompson 	ucycom_cfg_write(sc, UCYCOM_DEFAULT_RATE, UCYCOM_DEFAULT_CFG);
33302ac6454SAndrew Thompson }
33402ac6454SAndrew Thompson 
33502ac6454SAndrew Thompson static void
ucycom_start_read(struct ucom_softc * ucom)336760bc48eSAndrew Thompson ucycom_start_read(struct ucom_softc *ucom)
33702ac6454SAndrew Thompson {
33802ac6454SAndrew Thompson 	struct ucycom_softc *sc = ucom->sc_parent;
33902ac6454SAndrew Thompson 
340a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UCYCOM_INTR_RD]);
34102ac6454SAndrew Thompson }
34202ac6454SAndrew Thompson 
34302ac6454SAndrew Thompson static void
ucycom_stop_read(struct ucom_softc * ucom)344760bc48eSAndrew Thompson ucycom_stop_read(struct ucom_softc *ucom)
34502ac6454SAndrew Thompson {
34602ac6454SAndrew Thompson 	struct ucycom_softc *sc = ucom->sc_parent;
34702ac6454SAndrew Thompson 
348a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UCYCOM_INTR_RD]);
34902ac6454SAndrew Thompson }
35002ac6454SAndrew Thompson 
35102ac6454SAndrew Thompson static void
ucycom_start_write(struct ucom_softc * ucom)352760bc48eSAndrew Thompson ucycom_start_write(struct ucom_softc *ucom)
35302ac6454SAndrew Thompson {
35402ac6454SAndrew Thompson 	struct ucycom_softc *sc = ucom->sc_parent;
35502ac6454SAndrew Thompson 
356a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UCYCOM_CTRL_RD]);
35702ac6454SAndrew Thompson }
35802ac6454SAndrew Thompson 
35902ac6454SAndrew Thompson static void
ucycom_stop_write(struct ucom_softc * ucom)360760bc48eSAndrew Thompson ucycom_stop_write(struct ucom_softc *ucom)
36102ac6454SAndrew Thompson {
36202ac6454SAndrew Thompson 	struct ucycom_softc *sc = ucom->sc_parent;
36302ac6454SAndrew Thompson 
364a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UCYCOM_CTRL_RD]);
36502ac6454SAndrew Thompson }
36602ac6454SAndrew Thompson 
36702ac6454SAndrew Thompson static void
ucycom_ctrl_write_callback(struct usb_xfer * xfer,usb_error_t error)368ed6d949aSAndrew Thompson ucycom_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error)
36902ac6454SAndrew Thompson {
370ed6d949aSAndrew Thompson 	struct ucycom_softc *sc = usbd_xfer_softc(xfer);
371760bc48eSAndrew Thompson 	struct usb_device_request req;
372ed6d949aSAndrew Thompson 	struct usb_page_cache *pc0, *pc1;
37302ac6454SAndrew Thompson 	uint8_t data[2];
37402ac6454SAndrew Thompson 	uint8_t offset;
37502ac6454SAndrew Thompson 	uint32_t actlen;
37602ac6454SAndrew Thompson 
377ed6d949aSAndrew Thompson 	pc0 = usbd_xfer_get_frame(xfer, 0);
378ed6d949aSAndrew Thompson 	pc1 = usbd_xfer_get_frame(xfer, 1);
379ed6d949aSAndrew Thompson 
38002ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
38102ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
38202ac6454SAndrew Thompson tr_transferred:
38302ac6454SAndrew Thompson 	case USB_ST_SETUP:
38402ac6454SAndrew Thompson 
38502ac6454SAndrew Thompson 		switch (sc->sc_model) {
38602ac6454SAndrew Thompson 		case MODEL_CY7C63743:
38702ac6454SAndrew Thompson 			offset = 1;
38802ac6454SAndrew Thompson 			break;
38902ac6454SAndrew Thompson 		case MODEL_CY7C64013:
39002ac6454SAndrew Thompson 			offset = 2;
39102ac6454SAndrew Thompson 			break;
39202ac6454SAndrew Thompson 		default:
39302ac6454SAndrew Thompson 			offset = 0;
39402ac6454SAndrew Thompson 			break;
39502ac6454SAndrew Thompson 		}
39602ac6454SAndrew Thompson 
397ed6d949aSAndrew Thompson 		if (ucom_get_data(&sc->sc_ucom, pc1, offset,
39802ac6454SAndrew Thompson 		    sc->sc_olen - offset, &actlen)) {
39902ac6454SAndrew Thompson 			req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
40002ac6454SAndrew Thompson 			req.bRequest = UR_SET_REPORT;
40102ac6454SAndrew Thompson 			USETW2(req.wValue, UHID_OUTPUT_REPORT, sc->sc_oid);
40202ac6454SAndrew Thompson 			req.wIndex[0] = sc->sc_iface_no;
40302ac6454SAndrew Thompson 			req.wIndex[1] = 0;
40402ac6454SAndrew Thompson 			USETW(req.wLength, sc->sc_olen);
40502ac6454SAndrew Thompson 
40602ac6454SAndrew Thompson 			switch (sc->sc_model) {
40702ac6454SAndrew Thompson 			case MODEL_CY7C63743:
40802ac6454SAndrew Thompson 				data[0] = actlen;
40902ac6454SAndrew Thompson 				break;
41002ac6454SAndrew Thompson 			case MODEL_CY7C64013:
41102ac6454SAndrew Thompson 				data[0] = 0;
41202ac6454SAndrew Thompson 				data[1] = actlen;
41302ac6454SAndrew Thompson 				break;
41402ac6454SAndrew Thompson 			default:
41502ac6454SAndrew Thompson 				break;
41602ac6454SAndrew Thompson 			}
41702ac6454SAndrew Thompson 
418ed6d949aSAndrew Thompson 			usbd_copy_in(pc0, 0, &req, sizeof(req));
419ed6d949aSAndrew Thompson 			usbd_copy_in(pc1, 0, data, offset);
42002ac6454SAndrew Thompson 
421ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
422ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 1, sc->sc_olen);
423ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, sc->sc_olen ? 2 : 1);
424a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
42502ac6454SAndrew Thompson 		}
42602ac6454SAndrew Thompson 		return;
42702ac6454SAndrew Thompson 
42802ac6454SAndrew Thompson 	default:			/* Error */
429ed6d949aSAndrew Thompson 		if (error == USB_ERR_CANCELLED) {
43002ac6454SAndrew Thompson 			return;
43102ac6454SAndrew Thompson 		}
43202ac6454SAndrew Thompson 		DPRINTF("error=%s\n",
433ed6d949aSAndrew Thompson 		    usbd_errstr(error));
43402ac6454SAndrew Thompson 		goto tr_transferred;
43502ac6454SAndrew Thompson 	}
43602ac6454SAndrew Thompson }
43702ac6454SAndrew Thompson 
43802ac6454SAndrew Thompson static void
ucycom_cfg_write(struct ucycom_softc * sc,uint32_t baud,uint8_t cfg)43902ac6454SAndrew Thompson ucycom_cfg_write(struct ucycom_softc *sc, uint32_t baud, uint8_t cfg)
44002ac6454SAndrew Thompson {
441760bc48eSAndrew Thompson 	struct usb_device_request req;
44202ac6454SAndrew Thompson 	uint16_t len;
443e0a69b51SAndrew Thompson 	usb_error_t err;
44402ac6454SAndrew Thompson 
44502ac6454SAndrew Thompson 	len = sc->sc_flen;
44602ac6454SAndrew Thompson 	if (len > sizeof(sc->sc_temp_cfg)) {
44702ac6454SAndrew Thompson 		len = sizeof(sc->sc_temp_cfg);
44802ac6454SAndrew Thompson 	}
44902ac6454SAndrew Thompson 	sc->sc_cfg = cfg;
45002ac6454SAndrew Thompson 
45102ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
45202ac6454SAndrew Thompson 	req.bRequest = UR_SET_REPORT;
45302ac6454SAndrew Thompson 	USETW2(req.wValue, UHID_FEATURE_REPORT, sc->sc_fid);
45402ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_iface_no;
45502ac6454SAndrew Thompson 	req.wIndex[1] = 0;
45602ac6454SAndrew Thompson 	USETW(req.wLength, len);
45702ac6454SAndrew Thompson 
45802ac6454SAndrew Thompson 	sc->sc_temp_cfg[0] = (baud & 0xff);
45902ac6454SAndrew Thompson 	sc->sc_temp_cfg[1] = (baud >> 8) & 0xff;
46002ac6454SAndrew Thompson 	sc->sc_temp_cfg[2] = (baud >> 16) & 0xff;
46102ac6454SAndrew Thompson 	sc->sc_temp_cfg[3] = (baud >> 24) & 0xff;
46202ac6454SAndrew Thompson 	sc->sc_temp_cfg[4] = cfg;
46302ac6454SAndrew Thompson 
464a593f6b8SAndrew Thompson 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
46502ac6454SAndrew Thompson 	    &req, sc->sc_temp_cfg, 0, 1000);
46602ac6454SAndrew Thompson 	if (err) {
46702ac6454SAndrew Thompson 		DPRINTFN(0, "device request failed, err=%s "
468a593f6b8SAndrew Thompson 		    "(ignored)\n", usbd_errstr(err));
46902ac6454SAndrew Thompson 	}
47002ac6454SAndrew Thompson }
47102ac6454SAndrew Thompson 
47202ac6454SAndrew Thompson static int
ucycom_pre_param(struct ucom_softc * ucom,struct termios * t)473760bc48eSAndrew Thompson ucycom_pre_param(struct ucom_softc *ucom, struct termios *t)
47402ac6454SAndrew Thompson {
47502ac6454SAndrew Thompson 	switch (t->c_ospeed) {
47602ac6454SAndrew Thompson 		case 600:
47702ac6454SAndrew Thompson 		case 1200:
47802ac6454SAndrew Thompson 		case 2400:
47902ac6454SAndrew Thompson 		case 4800:
48002ac6454SAndrew Thompson 		case 9600:
48102ac6454SAndrew Thompson 		case 19200:
48202ac6454SAndrew Thompson 		case 38400:
48302ac6454SAndrew Thompson 		case 57600:
48402ac6454SAndrew Thompson #if 0
48502ac6454SAndrew Thompson 		/*
48602ac6454SAndrew Thompson 		 * Stock chips only support standard baud rates in the 600 - 57600
48702ac6454SAndrew Thompson 		 * range, but higher rates can be achieved using custom firmware.
48802ac6454SAndrew Thompson 		 */
48902ac6454SAndrew Thompson 		case 115200:
49002ac6454SAndrew Thompson 		case 153600:
49102ac6454SAndrew Thompson 		case 192000:
49202ac6454SAndrew Thompson #endif
49302ac6454SAndrew Thompson 		break;
49402ac6454SAndrew Thompson 	default:
49502ac6454SAndrew Thompson 		return (EINVAL);
49602ac6454SAndrew Thompson 	}
49702ac6454SAndrew Thompson 	return (0);
49802ac6454SAndrew Thompson }
49902ac6454SAndrew Thompson 
50002ac6454SAndrew Thompson static void
ucycom_cfg_param(struct ucom_softc * ucom,struct termios * t)501760bc48eSAndrew Thompson ucycom_cfg_param(struct ucom_softc *ucom, struct termios *t)
50202ac6454SAndrew Thompson {
50302ac6454SAndrew Thompson 	struct ucycom_softc *sc = ucom->sc_parent;
50402ac6454SAndrew Thompson 	uint8_t cfg;
50502ac6454SAndrew Thompson 
50602ac6454SAndrew Thompson 	DPRINTF("\n");
50702ac6454SAndrew Thompson 
50802ac6454SAndrew Thompson 	if (t->c_cflag & CIGNORE) {
50902ac6454SAndrew Thompson 		cfg = sc->sc_cfg;
51002ac6454SAndrew Thompson 	} else {
51102ac6454SAndrew Thompson 		cfg = 0;
51202ac6454SAndrew Thompson 		switch (t->c_cflag & CSIZE) {
51302ac6454SAndrew Thompson 		default:
51402ac6454SAndrew Thompson 		case CS8:
51502ac6454SAndrew Thompson 			++cfg;
51602ac6454SAndrew Thompson 		case CS7:
51702ac6454SAndrew Thompson 			++cfg;
51802ac6454SAndrew Thompson 		case CS6:
51902ac6454SAndrew Thompson 			++cfg;
52002ac6454SAndrew Thompson 		case CS5:
52102ac6454SAndrew Thompson 			break;
52202ac6454SAndrew Thompson 		}
52302ac6454SAndrew Thompson 
52402ac6454SAndrew Thompson 		if (t->c_cflag & CSTOPB)
52502ac6454SAndrew Thompson 			cfg |= UCYCOM_CFG_STOPB;
52602ac6454SAndrew Thompson 		if (t->c_cflag & PARENB)
52702ac6454SAndrew Thompson 			cfg |= UCYCOM_CFG_PAREN;
52802ac6454SAndrew Thompson 		if (t->c_cflag & PARODD)
52902ac6454SAndrew Thompson 			cfg |= UCYCOM_CFG_PARODD;
53002ac6454SAndrew Thompson 	}
53102ac6454SAndrew Thompson 
53202ac6454SAndrew Thompson 	ucycom_cfg_write(sc, t->c_ospeed, cfg);
53302ac6454SAndrew Thompson }
53402ac6454SAndrew Thompson 
53502ac6454SAndrew Thompson static void
ucycom_intr_read_callback(struct usb_xfer * xfer,usb_error_t error)536ed6d949aSAndrew Thompson ucycom_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
53702ac6454SAndrew Thompson {
538ed6d949aSAndrew Thompson 	struct ucycom_softc *sc = usbd_xfer_softc(xfer);
539ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
54002ac6454SAndrew Thompson 	uint8_t buf[2];
54102ac6454SAndrew Thompson 	uint32_t offset;
5426d917491SHans Petter Selasky 	int len;
543ed6d949aSAndrew Thompson 	int actlen;
544ed6d949aSAndrew Thompson 
545ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
546ed6d949aSAndrew Thompson 	pc = usbd_xfer_get_frame(xfer, 0);
54702ac6454SAndrew Thompson 
54802ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
54902ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
55002ac6454SAndrew Thompson 		switch (sc->sc_model) {
55102ac6454SAndrew Thompson 		case MODEL_CY7C63743:
552ed6d949aSAndrew Thompson 			if (actlen < 1) {
55302ac6454SAndrew Thompson 				goto tr_setup;
55402ac6454SAndrew Thompson 			}
555ed6d949aSAndrew Thompson 			usbd_copy_out(pc, 0, buf, 1);
55602ac6454SAndrew Thompson 
55702ac6454SAndrew Thompson 			sc->sc_ist = buf[0] & ~0x07;
55802ac6454SAndrew Thompson 			len = buf[0] & 0x07;
55902ac6454SAndrew Thompson 
560ed6d949aSAndrew Thompson 			actlen--;
56102ac6454SAndrew Thompson 			offset = 1;
56202ac6454SAndrew Thompson 
56302ac6454SAndrew Thompson 			break;
56402ac6454SAndrew Thompson 
56502ac6454SAndrew Thompson 		case MODEL_CY7C64013:
566ed6d949aSAndrew Thompson 			if (actlen < 2) {
56702ac6454SAndrew Thompson 				goto tr_setup;
56802ac6454SAndrew Thompson 			}
569ed6d949aSAndrew Thompson 			usbd_copy_out(pc, 0, buf, 2);
57002ac6454SAndrew Thompson 
57102ac6454SAndrew Thompson 			sc->sc_ist = buf[0] & ~0x07;
57202ac6454SAndrew Thompson 			len = buf[1];
57302ac6454SAndrew Thompson 
574ed6d949aSAndrew Thompson 			actlen -= 2;
57502ac6454SAndrew Thompson 			offset = 2;
57602ac6454SAndrew Thompson 
57702ac6454SAndrew Thompson 			break;
57802ac6454SAndrew Thompson 
57902ac6454SAndrew Thompson 		default:
580767cb2e2SAndrew Thompson 			DPRINTFN(0, "unsupported model number\n");
58102ac6454SAndrew Thompson 			goto tr_setup;
58202ac6454SAndrew Thompson 		}
58302ac6454SAndrew Thompson 
584ed6d949aSAndrew Thompson 		if (len > actlen)
585ed6d949aSAndrew Thompson 			len = actlen;
586ed6d949aSAndrew Thompson 		if (len)
587ed6d949aSAndrew Thompson 			ucom_put_data(&sc->sc_ucom, pc, offset, len);
588ed6d949aSAndrew Thompson 		/* FALLTHROUGH */
58902ac6454SAndrew Thompson 	case USB_ST_SETUP:
59002ac6454SAndrew Thompson tr_setup:
591ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, sc->sc_ilen);
592a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
59302ac6454SAndrew Thompson 		return;
59402ac6454SAndrew Thompson 
59502ac6454SAndrew Thompson 	default:			/* Error */
596ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
59702ac6454SAndrew Thompson 			/* try to clear stall first */
598ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
59902ac6454SAndrew Thompson 			goto tr_setup;
60002ac6454SAndrew Thompson 		}
60102ac6454SAndrew Thompson 		return;
60202ac6454SAndrew Thompson 	}
60302ac6454SAndrew Thompson }
604655dc9d0SAndrew Thompson 
605655dc9d0SAndrew Thompson static void
ucycom_poll(struct ucom_softc * ucom)606655dc9d0SAndrew Thompson ucycom_poll(struct ucom_softc *ucom)
607655dc9d0SAndrew Thompson {
608655dc9d0SAndrew Thompson 	struct ucycom_softc *sc = ucom->sc_parent;
609655dc9d0SAndrew Thompson 	usbd_transfer_poll(sc->sc_xfer, UCYCOM_N_TRANSFER);
610655dc9d0SAndrew Thompson }
611