xref: /freebsd/sys/dev/usb/serial/uark.c (revision 95ee2897)
102ac6454SAndrew Thompson /*	$OpenBSD: uark.c,v 1.1 2006/08/14 08:30:22 jsg Exp $	*/
202ac6454SAndrew Thompson 
302ac6454SAndrew Thompson /*
402ac6454SAndrew Thompson  * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
502ac6454SAndrew Thompson  *
602ac6454SAndrew Thompson  * Permission to use, copy, modify, and distribute this software for any
702ac6454SAndrew Thompson  * purpose with or without fee is hereby granted, provided that the above
802ac6454SAndrew Thompson  * copyright notice and this permission notice appear in all copies.
902ac6454SAndrew Thompson  *
1002ac6454SAndrew Thompson  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1102ac6454SAndrew Thompson  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1202ac6454SAndrew Thompson  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1302ac6454SAndrew Thompson  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1402ac6454SAndrew Thompson  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1502ac6454SAndrew Thompson  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1602ac6454SAndrew Thompson  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1702ac6454SAndrew Thompson  */
1802ac6454SAndrew Thompson 
1902ac6454SAndrew Thompson /*
2002ac6454SAndrew Thompson  * NOTE: all function names beginning like "uark_cfg_" can only
2102ac6454SAndrew Thompson  * be called from within the config thread function !
2202ac6454SAndrew Thompson  */
2302ac6454SAndrew Thompson 
24ed6d949aSAndrew Thompson #include <sys/stdint.h>
25ed6d949aSAndrew Thompson #include <sys/stddef.h>
26ed6d949aSAndrew Thompson #include <sys/param.h>
27ed6d949aSAndrew Thompson #include <sys/queue.h>
28ed6d949aSAndrew Thompson #include <sys/types.h>
29ed6d949aSAndrew Thompson #include <sys/systm.h>
30ed6d949aSAndrew Thompson #include <sys/kernel.h>
31ed6d949aSAndrew Thompson #include <sys/bus.h>
32ed6d949aSAndrew Thompson #include <sys/module.h>
33ed6d949aSAndrew Thompson #include <sys/lock.h>
34ed6d949aSAndrew Thompson #include <sys/mutex.h>
35ed6d949aSAndrew Thompson #include <sys/condvar.h>
36ed6d949aSAndrew Thompson #include <sys/sysctl.h>
37ed6d949aSAndrew Thompson #include <sys/sx.h>
38ed6d949aSAndrew Thompson #include <sys/unistd.h>
39ed6d949aSAndrew Thompson #include <sys/callout.h>
40ed6d949aSAndrew Thompson #include <sys/malloc.h>
41ed6d949aSAndrew Thompson #include <sys/priv.h>
42ed6d949aSAndrew Thompson 
4302ac6454SAndrew Thompson #include <dev/usb/usb.h>
44ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
45ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
46ed6d949aSAndrew Thompson #include <dev/usb/usbhid.h>
47ed6d949aSAndrew Thompson #include "usbdevs.h"
4802ac6454SAndrew Thompson 
49a593f6b8SAndrew Thompson #define	USB_DEBUG_VAR usb_debug
5002ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
5102ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
5202ac6454SAndrew Thompson 
5302ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h>
5402ac6454SAndrew Thompson 
5502ac6454SAndrew Thompson #define	UARK_BUF_SIZE		1024	/* bytes */
5602ac6454SAndrew Thompson 
5702ac6454SAndrew Thompson #define	UARK_SET_DATA_BITS(x)	((x) - 5)
5802ac6454SAndrew Thompson 
5902ac6454SAndrew Thompson #define	UARK_PARITY_NONE	0x00
6002ac6454SAndrew Thompson #define	UARK_PARITY_ODD		0x08
6102ac6454SAndrew Thompson #define	UARK_PARITY_EVEN	0x18
6202ac6454SAndrew Thompson 
6302ac6454SAndrew Thompson #define	UARK_STOP_BITS_1	0x00
6402ac6454SAndrew Thompson #define	UARK_STOP_BITS_2	0x04
6502ac6454SAndrew Thompson 
6602ac6454SAndrew Thompson #define	UARK_BAUD_REF		3000000
6702ac6454SAndrew Thompson 
6802ac6454SAndrew Thompson #define	UARK_WRITE		0x40
6902ac6454SAndrew Thompson #define	UARK_READ		0xc0
7002ac6454SAndrew Thompson 
7102ac6454SAndrew Thompson #define	UARK_REQUEST		0xfe
7202ac6454SAndrew Thompson 
7302ac6454SAndrew Thompson #define	UARK_CONFIG_INDEX	0
7402ac6454SAndrew Thompson #define	UARK_IFACE_INDEX	0
7502ac6454SAndrew Thompson 
7602ac6454SAndrew Thompson enum {
7702ac6454SAndrew Thompson 	UARK_BULK_DT_WR,
7802ac6454SAndrew Thompson 	UARK_BULK_DT_RD,
7902ac6454SAndrew Thompson 	UARK_N_TRANSFER,
8002ac6454SAndrew Thompson };
8102ac6454SAndrew Thompson 
8202ac6454SAndrew Thompson struct uark_softc {
83760bc48eSAndrew Thompson 	struct ucom_super_softc sc_super_ucom;
84760bc48eSAndrew Thompson 	struct ucom_softc sc_ucom;
8502ac6454SAndrew Thompson 
86760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UARK_N_TRANSFER];
87760bc48eSAndrew Thompson 	struct usb_device *sc_udev;
88deefe583SAndrew Thompson 	struct mtx sc_mtx;
8902ac6454SAndrew Thompson 
9002ac6454SAndrew Thompson 	uint8_t	sc_msr;
9102ac6454SAndrew Thompson 	uint8_t	sc_lsr;
9202ac6454SAndrew Thompson };
9302ac6454SAndrew Thompson 
9402ac6454SAndrew Thompson /* prototypes */
9502ac6454SAndrew Thompson 
9602ac6454SAndrew Thompson static device_probe_t uark_probe;
9702ac6454SAndrew Thompson static device_attach_t uark_attach;
9802ac6454SAndrew Thompson static device_detach_t uark_detach;
99c01fc06eSHans Petter Selasky static void uark_free_softc(struct uark_softc *);
10002ac6454SAndrew Thompson 
101e0a69b51SAndrew Thompson static usb_callback_t uark_bulk_write_callback;
102e0a69b51SAndrew Thompson static usb_callback_t uark_bulk_read_callback;
10302ac6454SAndrew Thompson 
1045805d178SHans Petter Selasky static void	uark_free(struct ucom_softc *);
105760bc48eSAndrew Thompson static void	uark_start_read(struct ucom_softc *);
106760bc48eSAndrew Thompson static void	uark_stop_read(struct ucom_softc *);
107760bc48eSAndrew Thompson static void	uark_start_write(struct ucom_softc *);
108760bc48eSAndrew Thompson static void	uark_stop_write(struct ucom_softc *);
109760bc48eSAndrew Thompson static int	uark_pre_param(struct ucom_softc *, struct termios *);
110760bc48eSAndrew Thompson static void	uark_cfg_param(struct ucom_softc *, struct termios *);
111760bc48eSAndrew Thompson static void	uark_cfg_get_status(struct ucom_softc *, uint8_t *,
11202ac6454SAndrew Thompson 		    uint8_t *);
113760bc48eSAndrew Thompson static void	uark_cfg_set_break(struct ucom_softc *, uint8_t);
11402ac6454SAndrew Thompson static void	uark_cfg_write(struct uark_softc *, uint16_t, uint16_t);
115655dc9d0SAndrew Thompson static void	uark_poll(struct ucom_softc *ucom);
11602ac6454SAndrew Thompson 
117760bc48eSAndrew Thompson static const struct usb_config
11802ac6454SAndrew Thompson 	uark_xfer_config[UARK_N_TRANSFER] = {
11902ac6454SAndrew Thompson 	[UARK_BULK_DT_WR] = {
12002ac6454SAndrew Thompson 		.type = UE_BULK,
12102ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
12202ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
1234eae601eSAndrew Thompson 		.bufsize = UARK_BUF_SIZE,
1244eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
1254eae601eSAndrew Thompson 		.callback = &uark_bulk_write_callback,
12602ac6454SAndrew Thompson 	},
12702ac6454SAndrew Thompson 
12802ac6454SAndrew Thompson 	[UARK_BULK_DT_RD] = {
12902ac6454SAndrew Thompson 		.type = UE_BULK,
13002ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
13102ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
1324eae601eSAndrew Thompson 		.bufsize = UARK_BUF_SIZE,
1334eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
1344eae601eSAndrew Thompson 		.callback = &uark_bulk_read_callback,
13502ac6454SAndrew Thompson 	},
13602ac6454SAndrew Thompson };
13702ac6454SAndrew Thompson 
138760bc48eSAndrew Thompson static const struct ucom_callback uark_callback = {
139a593f6b8SAndrew Thompson 	.ucom_cfg_get_status = &uark_cfg_get_status,
140a593f6b8SAndrew Thompson 	.ucom_cfg_set_break = &uark_cfg_set_break,
141a593f6b8SAndrew Thompson 	.ucom_cfg_param = &uark_cfg_param,
142a593f6b8SAndrew Thompson 	.ucom_pre_param = &uark_pre_param,
143a593f6b8SAndrew Thompson 	.ucom_start_read = &uark_start_read,
144a593f6b8SAndrew Thompson 	.ucom_stop_read = &uark_stop_read,
145a593f6b8SAndrew Thompson 	.ucom_start_write = &uark_start_write,
146a593f6b8SAndrew Thompson 	.ucom_stop_write = &uark_stop_write,
147655dc9d0SAndrew Thompson 	.ucom_poll = &uark_poll,
1485805d178SHans Petter Selasky 	.ucom_free = &uark_free,
14902ac6454SAndrew Thompson };
15002ac6454SAndrew Thompson 
15102ac6454SAndrew Thompson static device_method_t uark_methods[] = {
15202ac6454SAndrew Thompson 	/* Device methods */
15302ac6454SAndrew Thompson 	DEVMETHOD(device_probe, uark_probe),
15402ac6454SAndrew Thompson 	DEVMETHOD(device_attach, uark_attach),
15502ac6454SAndrew Thompson 	DEVMETHOD(device_detach, uark_detach),
1565805d178SHans Petter Selasky 	DEVMETHOD_END
15702ac6454SAndrew Thompson };
15802ac6454SAndrew Thompson 
15902ac6454SAndrew Thompson static driver_t uark_driver = {
16002ac6454SAndrew Thompson 	.name = "uark",
16102ac6454SAndrew Thompson 	.methods = uark_methods,
16202ac6454SAndrew Thompson 	.size = sizeof(struct uark_softc),
16302ac6454SAndrew Thompson };
16402ac6454SAndrew Thompson 
165f809f280SWarner Losh static const STRUCT_USB_HOST_ID uark_devs[] = {
166f809f280SWarner Losh 	{USB_VPI(USB_VENDOR_ARKMICRO, USB_PRODUCT_ARKMICRO_ARK3116, 0)},
167f809f280SWarner Losh };
168f809f280SWarner Losh 
169bc9372d7SJohn Baldwin DRIVER_MODULE(uark, uhub, uark_driver, NULL, NULL);
17002ac6454SAndrew Thompson MODULE_DEPEND(uark, ucom, 1, 1, 1);
17102ac6454SAndrew Thompson MODULE_DEPEND(uark, usb, 1, 1, 1);
172910cb8feSAndrew Thompson MODULE_VERSION(uark, 1);
173f809f280SWarner Losh USB_PNP_HOST_INFO(uark_devs);
17402ac6454SAndrew Thompson 
17502ac6454SAndrew Thompson static int
uark_probe(device_t dev)17602ac6454SAndrew Thompson uark_probe(device_t dev)
17702ac6454SAndrew Thompson {
178760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
17902ac6454SAndrew Thompson 
180f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
18102ac6454SAndrew Thompson 		return (ENXIO);
18202ac6454SAndrew Thompson 	}
18302ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != 0) {
18402ac6454SAndrew Thompson 		return (ENXIO);
18502ac6454SAndrew Thompson 	}
18602ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != UARK_IFACE_INDEX) {
18702ac6454SAndrew Thompson 		return (ENXIO);
18802ac6454SAndrew Thompson 	}
189a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(uark_devs, sizeof(uark_devs), uaa));
19002ac6454SAndrew Thompson }
19102ac6454SAndrew Thompson 
19202ac6454SAndrew Thompson static int
uark_attach(device_t dev)19302ac6454SAndrew Thompson uark_attach(device_t dev)
19402ac6454SAndrew Thompson {
195760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
19602ac6454SAndrew Thompson 	struct uark_softc *sc = device_get_softc(dev);
19702ac6454SAndrew Thompson 	int32_t error;
19802ac6454SAndrew Thompson 	uint8_t iface_index;
19902ac6454SAndrew Thompson 
200a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
201deefe583SAndrew Thompson 	mtx_init(&sc->sc_mtx, "uark", NULL, MTX_DEF);
2025805d178SHans Petter Selasky 	ucom_ref(&sc->sc_super_ucom);
20302ac6454SAndrew Thompson 
20402ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
20502ac6454SAndrew Thompson 
20602ac6454SAndrew Thompson 	iface_index = UARK_IFACE_INDEX;
207a593f6b8SAndrew Thompson 	error = usbd_transfer_setup
20802ac6454SAndrew Thompson 	    (uaa->device, &iface_index, sc->sc_xfer,
209deefe583SAndrew Thompson 	    uark_xfer_config, UARK_N_TRANSFER, sc, &sc->sc_mtx);
21002ac6454SAndrew Thompson 
21102ac6454SAndrew Thompson 	if (error) {
21202ac6454SAndrew Thompson 		device_printf(dev, "allocating control USB "
213767cb2e2SAndrew Thompson 		    "transfers failed\n");
21402ac6454SAndrew Thompson 		goto detach;
21502ac6454SAndrew Thompson 	}
21602ac6454SAndrew Thompson 	/* clear stall at first run */
217deefe583SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
218ec97e9caSHans Petter Selasky 	usbd_xfer_set_stall(sc->sc_xfer[UARK_BULK_DT_WR]);
219ec97e9caSHans Petter Selasky 	usbd_xfer_set_stall(sc->sc_xfer[UARK_BULK_DT_RD]);
220deefe583SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
22102ac6454SAndrew Thompson 
222a593f6b8SAndrew Thompson 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
223deefe583SAndrew Thompson 	    &uark_callback, &sc->sc_mtx);
22402ac6454SAndrew Thompson 	if (error) {
225a593f6b8SAndrew Thompson 		DPRINTF("ucom_attach failed\n");
22602ac6454SAndrew Thompson 		goto detach;
22702ac6454SAndrew Thompson 	}
2286416c259SNick Hibma 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
2296416c259SNick Hibma 
23002ac6454SAndrew Thompson 	return (0);			/* success */
23102ac6454SAndrew Thompson 
23202ac6454SAndrew Thompson detach:
23302ac6454SAndrew Thompson 	uark_detach(dev);
23402ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
23502ac6454SAndrew Thompson }
23602ac6454SAndrew Thompson 
23702ac6454SAndrew Thompson static int
uark_detach(device_t dev)23802ac6454SAndrew Thompson uark_detach(device_t dev)
23902ac6454SAndrew Thompson {
24002ac6454SAndrew Thompson 	struct uark_softc *sc = device_get_softc(dev);
24102ac6454SAndrew Thompson 
242015bb88fSNick Hibma 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
243a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, UARK_N_TRANSFER);
24402ac6454SAndrew Thompson 
245c01fc06eSHans Petter Selasky 	device_claim_softc(dev);
246c01fc06eSHans Petter Selasky 
247c01fc06eSHans Petter Selasky 	uark_free_softc(sc);
248c01fc06eSHans Petter Selasky 
24902ac6454SAndrew Thompson 	return (0);
25002ac6454SAndrew Thompson }
25102ac6454SAndrew Thompson 
2525805d178SHans Petter Selasky UCOM_UNLOAD_DRAIN(uark);
2535805d178SHans Petter Selasky 
2545805d178SHans Petter Selasky static void
uark_free_softc(struct uark_softc * sc)255c01fc06eSHans Petter Selasky uark_free_softc(struct uark_softc *sc)
2565805d178SHans Petter Selasky {
2575805d178SHans Petter Selasky 	if (ucom_unref(&sc->sc_super_ucom)) {
2585805d178SHans Petter Selasky 		mtx_destroy(&sc->sc_mtx);
259c01fc06eSHans Petter Selasky 		device_free_softc(sc);
2605805d178SHans Petter Selasky 	}
2615805d178SHans Petter Selasky }
2625805d178SHans Petter Selasky 
2635805d178SHans Petter Selasky static void
uark_free(struct ucom_softc * ucom)2645805d178SHans Petter Selasky uark_free(struct ucom_softc *ucom)
2655805d178SHans Petter Selasky {
266c01fc06eSHans Petter Selasky 	uark_free_softc(ucom->sc_parent);
2675805d178SHans Petter Selasky }
2685805d178SHans Petter Selasky 
26902ac6454SAndrew Thompson static void
uark_bulk_write_callback(struct usb_xfer * xfer,usb_error_t error)270ed6d949aSAndrew Thompson uark_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
27102ac6454SAndrew Thompson {
272ed6d949aSAndrew Thompson 	struct uark_softc *sc = usbd_xfer_softc(xfer);
273ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
27402ac6454SAndrew Thompson 	uint32_t actlen;
27502ac6454SAndrew Thompson 
27602ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
27702ac6454SAndrew Thompson 	case USB_ST_SETUP:
27802ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
27902ac6454SAndrew Thompson tr_setup:
280ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
281ed6d949aSAndrew Thompson 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
28202ac6454SAndrew Thompson 		    UARK_BUF_SIZE, &actlen)) {
283ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, actlen);
284a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
28502ac6454SAndrew Thompson 		}
286ec97e9caSHans Petter Selasky 		return;
28702ac6454SAndrew Thompson 
28802ac6454SAndrew Thompson 	default:			/* Error */
289ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
29002ac6454SAndrew Thompson 			/* try to clear stall first */
291ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
29202ac6454SAndrew Thompson 			goto tr_setup;
29302ac6454SAndrew Thompson 		}
294ec97e9caSHans Petter Selasky 		return;
29502ac6454SAndrew Thompson 	}
29602ac6454SAndrew Thompson }
29702ac6454SAndrew Thompson 
29802ac6454SAndrew Thompson static void
uark_bulk_read_callback(struct usb_xfer * xfer,usb_error_t error)299ed6d949aSAndrew Thompson uark_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
30002ac6454SAndrew Thompson {
301ed6d949aSAndrew Thompson 	struct uark_softc *sc = usbd_xfer_softc(xfer);
302ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
303ed6d949aSAndrew Thompson 	int actlen;
304ed6d949aSAndrew Thompson 
305ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
30602ac6454SAndrew Thompson 
30702ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
30802ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
309ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
310ed6d949aSAndrew Thompson 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
31102ac6454SAndrew Thompson 
31202ac6454SAndrew Thompson 	case USB_ST_SETUP:
31302ac6454SAndrew Thompson tr_setup:
314ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
315a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
31602ac6454SAndrew Thompson 		return;
31702ac6454SAndrew Thompson 
31802ac6454SAndrew Thompson 	default:			/* Error */
319ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
32002ac6454SAndrew Thompson 			/* try to clear stall first */
321ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
32202ac6454SAndrew Thompson 			goto tr_setup;
32302ac6454SAndrew Thompson 		}
32402ac6454SAndrew Thompson 		return;
32502ac6454SAndrew Thompson 	}
32602ac6454SAndrew Thompson }
32702ac6454SAndrew Thompson 
32802ac6454SAndrew Thompson static void
uark_start_read(struct ucom_softc * ucom)329760bc48eSAndrew Thompson uark_start_read(struct ucom_softc *ucom)
33002ac6454SAndrew Thompson {
33102ac6454SAndrew Thompson 	struct uark_softc *sc = ucom->sc_parent;
33202ac6454SAndrew Thompson 
333a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UARK_BULK_DT_RD]);
33402ac6454SAndrew Thompson }
33502ac6454SAndrew Thompson 
33602ac6454SAndrew Thompson static void
uark_stop_read(struct ucom_softc * ucom)337760bc48eSAndrew Thompson uark_stop_read(struct ucom_softc *ucom)
33802ac6454SAndrew Thompson {
33902ac6454SAndrew Thompson 	struct uark_softc *sc = ucom->sc_parent;
34002ac6454SAndrew Thompson 
341a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UARK_BULK_DT_RD]);
34202ac6454SAndrew Thompson }
34302ac6454SAndrew Thompson 
34402ac6454SAndrew Thompson static void
uark_start_write(struct ucom_softc * ucom)345760bc48eSAndrew Thompson uark_start_write(struct ucom_softc *ucom)
34602ac6454SAndrew Thompson {
34702ac6454SAndrew Thompson 	struct uark_softc *sc = ucom->sc_parent;
34802ac6454SAndrew Thompson 
349a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UARK_BULK_DT_WR]);
35002ac6454SAndrew Thompson }
35102ac6454SAndrew Thompson 
35202ac6454SAndrew Thompson static void
uark_stop_write(struct ucom_softc * ucom)353760bc48eSAndrew Thompson uark_stop_write(struct ucom_softc *ucom)
35402ac6454SAndrew Thompson {
35502ac6454SAndrew Thompson 	struct uark_softc *sc = ucom->sc_parent;
35602ac6454SAndrew Thompson 
357a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UARK_BULK_DT_WR]);
35802ac6454SAndrew Thompson }
35902ac6454SAndrew Thompson 
36002ac6454SAndrew Thompson static int
uark_pre_param(struct ucom_softc * ucom,struct termios * t)361760bc48eSAndrew Thompson uark_pre_param(struct ucom_softc *ucom, struct termios *t)
36202ac6454SAndrew Thompson {
36302ac6454SAndrew Thompson 	if ((t->c_ospeed < 300) || (t->c_ospeed > 115200))
36402ac6454SAndrew Thompson 		return (EINVAL);
36502ac6454SAndrew Thompson 	return (0);
36602ac6454SAndrew Thompson }
36702ac6454SAndrew Thompson 
36802ac6454SAndrew Thompson static void
uark_cfg_param(struct ucom_softc * ucom,struct termios * t)369760bc48eSAndrew Thompson uark_cfg_param(struct ucom_softc *ucom, struct termios *t)
37002ac6454SAndrew Thompson {
37102ac6454SAndrew Thompson 	struct uark_softc *sc = ucom->sc_parent;
37202ac6454SAndrew Thompson 	uint32_t speed = t->c_ospeed;
37302ac6454SAndrew Thompson 	uint16_t data;
37402ac6454SAndrew Thompson 
37502ac6454SAndrew Thompson 	/*
37602ac6454SAndrew Thompson 	 * NOTE: When reverse computing the baud rate from the "data" all
37702ac6454SAndrew Thompson 	 * allowed baud rates are within 3% of the initial baud rate.
37802ac6454SAndrew Thompson 	 */
37902ac6454SAndrew Thompson 	data = (UARK_BAUD_REF + (speed / 2)) / speed;
38002ac6454SAndrew Thompson 
38102ac6454SAndrew Thompson 	uark_cfg_write(sc, 3, 0x83);
38202ac6454SAndrew Thompson 	uark_cfg_write(sc, 0, data & 0xFF);
38302ac6454SAndrew Thompson 	uark_cfg_write(sc, 1, data >> 8);
38402ac6454SAndrew Thompson 	uark_cfg_write(sc, 3, 0x03);
38502ac6454SAndrew Thompson 
38602ac6454SAndrew Thompson 	if (t->c_cflag & CSTOPB)
38702ac6454SAndrew Thompson 		data = UARK_STOP_BITS_2;
38802ac6454SAndrew Thompson 	else
38902ac6454SAndrew Thompson 		data = UARK_STOP_BITS_1;
39002ac6454SAndrew Thompson 
39102ac6454SAndrew Thompson 	if (t->c_cflag & PARENB) {
39202ac6454SAndrew Thompson 		if (t->c_cflag & PARODD)
39302ac6454SAndrew Thompson 			data |= UARK_PARITY_ODD;
39402ac6454SAndrew Thompson 		else
39502ac6454SAndrew Thompson 			data |= UARK_PARITY_EVEN;
39602ac6454SAndrew Thompson 	} else
39702ac6454SAndrew Thompson 		data |= UARK_PARITY_NONE;
39802ac6454SAndrew Thompson 
39902ac6454SAndrew Thompson 	switch (t->c_cflag & CSIZE) {
40002ac6454SAndrew Thompson 	case CS5:
40102ac6454SAndrew Thompson 		data |= UARK_SET_DATA_BITS(5);
40202ac6454SAndrew Thompson 		break;
40302ac6454SAndrew Thompson 	case CS6:
40402ac6454SAndrew Thompson 		data |= UARK_SET_DATA_BITS(6);
40502ac6454SAndrew Thompson 		break;
40602ac6454SAndrew Thompson 	case CS7:
40702ac6454SAndrew Thompson 		data |= UARK_SET_DATA_BITS(7);
40802ac6454SAndrew Thompson 		break;
40902ac6454SAndrew Thompson 	default:
41002ac6454SAndrew Thompson 	case CS8:
41102ac6454SAndrew Thompson 		data |= UARK_SET_DATA_BITS(8);
41202ac6454SAndrew Thompson 		break;
41302ac6454SAndrew Thompson 	}
41402ac6454SAndrew Thompson 	uark_cfg_write(sc, 3, 0x00);
41502ac6454SAndrew Thompson 	uark_cfg_write(sc, 3, data);
41602ac6454SAndrew Thompson }
41702ac6454SAndrew Thompson 
41802ac6454SAndrew Thompson static void
uark_cfg_get_status(struct ucom_softc * ucom,uint8_t * lsr,uint8_t * msr)419760bc48eSAndrew Thompson uark_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
42002ac6454SAndrew Thompson {
42102ac6454SAndrew Thompson 	struct uark_softc *sc = ucom->sc_parent;
42202ac6454SAndrew Thompson 
423fe67c846SIan Lepore 	/* XXX Note: sc_lsr is always zero */
42402ac6454SAndrew Thompson 	*lsr = sc->sc_lsr;
42502ac6454SAndrew Thompson 	*msr = sc->sc_msr;
42602ac6454SAndrew Thompson }
42702ac6454SAndrew Thompson 
42802ac6454SAndrew Thompson static void
uark_cfg_set_break(struct ucom_softc * ucom,uint8_t onoff)429760bc48eSAndrew Thompson uark_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
43002ac6454SAndrew Thompson {
43102ac6454SAndrew Thompson 	struct uark_softc *sc = ucom->sc_parent;
43202ac6454SAndrew Thompson 
43302ac6454SAndrew Thompson 	DPRINTF("onoff=%d\n", onoff);
43402ac6454SAndrew Thompson 
43502ac6454SAndrew Thompson 	uark_cfg_write(sc, 4, onoff ? 0x01 : 0x00);
43602ac6454SAndrew Thompson }
43702ac6454SAndrew Thompson 
43802ac6454SAndrew Thompson static void
uark_cfg_write(struct uark_softc * sc,uint16_t index,uint16_t value)43902ac6454SAndrew Thompson uark_cfg_write(struct uark_softc *sc, uint16_t index, uint16_t value)
44002ac6454SAndrew Thompson {
441760bc48eSAndrew Thompson 	struct usb_device_request req;
442e0a69b51SAndrew Thompson 	usb_error_t err;
44302ac6454SAndrew Thompson 
44402ac6454SAndrew Thompson 	req.bmRequestType = UARK_WRITE;
44502ac6454SAndrew Thompson 	req.bRequest = UARK_REQUEST;
44602ac6454SAndrew Thompson 	USETW(req.wValue, value);
44702ac6454SAndrew Thompson 	USETW(req.wIndex, index);
44802ac6454SAndrew Thompson 	USETW(req.wLength, 0);
44902ac6454SAndrew Thompson 
450a593f6b8SAndrew Thompson 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
45102ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
45202ac6454SAndrew Thompson 	if (err) {
45302ac6454SAndrew Thompson 		DPRINTFN(0, "device request failed, err=%s "
454a593f6b8SAndrew Thompson 		    "(ignored)\n", usbd_errstr(err));
45502ac6454SAndrew Thompson 	}
45602ac6454SAndrew Thompson }
457655dc9d0SAndrew Thompson 
458655dc9d0SAndrew Thompson static void
uark_poll(struct ucom_softc * ucom)459655dc9d0SAndrew Thompson uark_poll(struct ucom_softc *ucom)
460655dc9d0SAndrew Thompson {
461655dc9d0SAndrew Thompson 	struct uark_softc *sc = ucom->sc_parent;
462655dc9d0SAndrew Thompson 	usbd_transfer_poll(sc->sc_xfer, UARK_N_TRANSFER);
463655dc9d0SAndrew Thompson }
464