ucycom.c revision 239180
1184610Salfred#include <sys/cdefs.h>
2184610Salfred__FBSDID("$FreeBSD: head/sys/dev/usb/serial/ucycom.c 239180 2012-08-10 15:29:41Z hselasky $");
3184610Salfred
4184610Salfred/*-
5230132Suqs * Copyright (c) 2004 Dag-Erling Co��dan Sm��rgrav
6184610Salfred * All rights reserved.
7184610Salfred *
8184610Salfred * Redistribution and use in source and binary forms, with or without
9184610Salfred * modification, are permitted provided that the following conditions
10184610Salfred * are met:
11184610Salfred * 1. Redistributions of source code must retain the above copyright
12184610Salfred *    notice, this list of conditions and the following disclaimer
13184610Salfred *    in this position and unchanged.
14184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
15184610Salfred *    notice, this list of conditions and the following disclaimer in the
16184610Salfred *    documentation and/or other materials provided with the distribution.
17184610Salfred * 3. The name of the author may not be used to endorse or promote products
18184610Salfred *    derived from this software without specific prior written permission.
19184610Salfred *
20184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21184610Salfred * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22184610Salfred * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23184610Salfred * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24184610Salfred * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25184610Salfred * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26184610Salfred * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27184610Salfred * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28184610Salfred * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29184610Salfred * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30184610Salfred */
31184610Salfred
32184610Salfred/*
33184610Salfred * Device driver for Cypress CY7C637xx and CY7C640/1xx series USB to
34184610Salfred * RS232 bridges.
35184610Salfred */
36184610Salfred
37194677Sthompsa#include <sys/stdint.h>
38194677Sthompsa#include <sys/stddef.h>
39194677Sthompsa#include <sys/param.h>
40194677Sthompsa#include <sys/queue.h>
41194677Sthompsa#include <sys/types.h>
42194677Sthompsa#include <sys/systm.h>
43194677Sthompsa#include <sys/kernel.h>
44194677Sthompsa#include <sys/bus.h>
45194677Sthompsa#include <sys/module.h>
46194677Sthompsa#include <sys/lock.h>
47194677Sthompsa#include <sys/mutex.h>
48194677Sthompsa#include <sys/condvar.h>
49194677Sthompsa#include <sys/sysctl.h>
50194677Sthompsa#include <sys/sx.h>
51194677Sthompsa#include <sys/unistd.h>
52194677Sthompsa#include <sys/callout.h>
53194677Sthompsa#include <sys/malloc.h>
54194677Sthompsa#include <sys/priv.h>
55194677Sthompsa
56188942Sthompsa#include <dev/usb/usb.h>
57194677Sthompsa#include <dev/usb/usbdi.h>
58194677Sthompsa#include <dev/usb/usbdi_util.h>
59188942Sthompsa#include <dev/usb/usbhid.h>
60194677Sthompsa#include "usbdevs.h"
61184610Salfred
62194228Sthompsa#define	USB_DEBUG_VAR usb_debug
63188942Sthompsa#include <dev/usb/usb_debug.h>
64188942Sthompsa#include <dev/usb/usb_process.h>
65184610Salfred
66188942Sthompsa#include <dev/usb/serial/usb_serial.h>
67184610Salfred
68184610Salfred#define	UCYCOM_MAX_IOLEN	(1024 + 2)	/* bytes */
69184610Salfred
70184610Salfred#define	UCYCOM_IFACE_INDEX	0
71184610Salfred
72187259Sthompsaenum {
73187259Sthompsa	UCYCOM_CTRL_RD,
74187259Sthompsa	UCYCOM_INTR_RD,
75188413Sthompsa	UCYCOM_N_TRANSFER,
76187259Sthompsa};
77187259Sthompsa
78184610Salfredstruct ucycom_softc {
79192984Sthompsa	struct ucom_super_softc sc_super_ucom;
80192984Sthompsa	struct ucom_softc sc_ucom;
81184610Salfred
82192984Sthompsa	struct usb_device *sc_udev;
83192984Sthompsa	struct usb_xfer *sc_xfer[UCYCOM_N_TRANSFER];
84189265Sthompsa	struct mtx sc_mtx;
85184610Salfred
86184610Salfred	uint32_t sc_model;
87184610Salfred#define	MODEL_CY7C63743		0x63743
88184610Salfred#define	MODEL_CY7C64013		0x64013
89184610Salfred
90184610Salfred	uint16_t sc_flen;		/* feature report length */
91184610Salfred	uint16_t sc_ilen;		/* input report length */
92184610Salfred	uint16_t sc_olen;		/* output report length */
93184610Salfred
94184610Salfred	uint8_t	sc_fid;			/* feature report id */
95184610Salfred	uint8_t	sc_iid;			/* input report id */
96184610Salfred	uint8_t	sc_oid;			/* output report id */
97184610Salfred	uint8_t	sc_cfg;
98184610Salfred#define	UCYCOM_CFG_RESET	0x80
99184610Salfred#define	UCYCOM_CFG_PARODD	0x20
100184610Salfred#define	UCYCOM_CFG_PAREN	0x10
101184610Salfred#define	UCYCOM_CFG_STOPB	0x08
102184610Salfred#define	UCYCOM_CFG_DATAB	0x03
103184610Salfred	uint8_t	sc_ist;			/* status flags from last input */
104184610Salfred	uint8_t	sc_name[16];
105184610Salfred	uint8_t	sc_iface_no;
106184610Salfred	uint8_t	sc_temp_cfg[32];
107184610Salfred};
108184610Salfred
109184610Salfred/* prototypes */
110184610Salfred
111184610Salfredstatic device_probe_t ucycom_probe;
112184610Salfredstatic device_attach_t ucycom_attach;
113184610Salfredstatic device_detach_t ucycom_detach;
114239180Shselaskystatic device_free_softc_t ucycom_free_softc;
115184610Salfred
116193045Sthompsastatic usb_callback_t ucycom_ctrl_write_callback;
117193045Sthompsastatic usb_callback_t ucycom_intr_read_callback;
118184610Salfred
119239180Shselaskystatic void	ucycom_free(struct ucom_softc *);
120192984Sthompsastatic void	ucycom_cfg_open(struct ucom_softc *);
121192984Sthompsastatic void	ucycom_start_read(struct ucom_softc *);
122192984Sthompsastatic void	ucycom_stop_read(struct ucom_softc *);
123192984Sthompsastatic void	ucycom_start_write(struct ucom_softc *);
124192984Sthompsastatic void	ucycom_stop_write(struct ucom_softc *);
125185948Sthompsastatic void	ucycom_cfg_write(struct ucycom_softc *, uint32_t, uint8_t);
126192984Sthompsastatic int	ucycom_pre_param(struct ucom_softc *, struct termios *);
127192984Sthompsastatic void	ucycom_cfg_param(struct ucom_softc *, struct termios *);
128197570Sthompsastatic void	ucycom_poll(struct ucom_softc *ucom);
129184610Salfred
130192984Sthompsastatic const struct usb_config ucycom_config[UCYCOM_N_TRANSFER] = {
131184610Salfred
132187259Sthompsa	[UCYCOM_CTRL_RD] = {
133184610Salfred		.type = UE_CONTROL,
134184610Salfred		.endpoint = 0x00,	/* Control pipe */
135184610Salfred		.direction = UE_DIR_ANY,
136192984Sthompsa		.bufsize = (sizeof(struct usb_device_request) + UCYCOM_MAX_IOLEN),
137190734Sthompsa		.callback = &ucycom_ctrl_write_callback,
138190734Sthompsa		.timeout = 1000,	/* 1 second */
139184610Salfred	},
140184610Salfred
141187259Sthompsa	[UCYCOM_INTR_RD] = {
142184610Salfred		.type = UE_INTERRUPT,
143184610Salfred		.endpoint = UE_ADDR_ANY,
144184610Salfred		.direction = UE_DIR_IN,
145190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
146190734Sthompsa		.bufsize = UCYCOM_MAX_IOLEN,
147190734Sthompsa		.callback = &ucycom_intr_read_callback,
148184610Salfred	},
149184610Salfred};
150184610Salfred
151192984Sthompsastatic const struct ucom_callback ucycom_callback = {
152194228Sthompsa	.ucom_cfg_param = &ucycom_cfg_param,
153194228Sthompsa	.ucom_cfg_open = &ucycom_cfg_open,
154194228Sthompsa	.ucom_pre_param = &ucycom_pre_param,
155194228Sthompsa	.ucom_start_read = &ucycom_start_read,
156194228Sthompsa	.ucom_stop_read = &ucycom_stop_read,
157194228Sthompsa	.ucom_start_write = &ucycom_start_write,
158194228Sthompsa	.ucom_stop_write = &ucycom_stop_write,
159197570Sthompsa	.ucom_poll = &ucycom_poll,
160239180Shselasky	.ucom_free = &ucycom_free,
161184610Salfred};
162184610Salfred
163184610Salfredstatic device_method_t ucycom_methods[] = {
164184610Salfred	DEVMETHOD(device_probe, ucycom_probe),
165184610Salfred	DEVMETHOD(device_attach, ucycom_attach),
166184610Salfred	DEVMETHOD(device_detach, ucycom_detach),
167239180Shselasky	DEVMETHOD(device_free_softc, ucycom_free_softc),
168239180Shselasky	DEVMETHOD_END
169184610Salfred};
170184610Salfred
171184610Salfredstatic devclass_t ucycom_devclass;
172184610Salfred
173184610Salfredstatic driver_t ucycom_driver = {
174184610Salfred	.name = "ucycom",
175184610Salfred	.methods = ucycom_methods,
176184610Salfred	.size = sizeof(struct ucycom_softc),
177184610Salfred};
178184610Salfred
179189275SthompsaDRIVER_MODULE(ucycom, uhub, ucycom_driver, ucycom_devclass, NULL, 0);
180188942SthompsaMODULE_DEPEND(ucycom, ucom, 1, 1, 1);
181188942SthompsaMODULE_DEPEND(ucycom, usb, 1, 1, 1);
182212122SthompsaMODULE_VERSION(ucycom, 1);
183184610Salfred
184184610Salfred/*
185184610Salfred * Supported devices
186184610Salfred */
187223486Shselaskystatic const STRUCT_USB_HOST_ID ucycom_devs[] = {
188184610Salfred	{USB_VPI(USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EARTHMATE, MODEL_CY7C64013)},
189184610Salfred};
190184610Salfred
191184610Salfred#define	UCYCOM_DEFAULT_RATE	 4800
192184610Salfred#define	UCYCOM_DEFAULT_CFG	 0x03	/* N-8-1 */
193184610Salfred
194184610Salfredstatic int
195184610Salfreducycom_probe(device_t dev)
196184610Salfred{
197192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
198184610Salfred
199192499Sthompsa	if (uaa->usb_mode != USB_MODE_HOST) {
200184610Salfred		return (ENXIO);
201184610Salfred	}
202184610Salfred	if (uaa->info.bConfigIndex != 0) {
203184610Salfred		return (ENXIO);
204184610Salfred	}
205184610Salfred	if (uaa->info.bIfaceIndex != UCYCOM_IFACE_INDEX) {
206184610Salfred		return (ENXIO);
207184610Salfred	}
208194228Sthompsa	return (usbd_lookup_id_by_uaa(ucycom_devs, sizeof(ucycom_devs), uaa));
209184610Salfred}
210184610Salfred
211184610Salfredstatic int
212184610Salfreducycom_attach(device_t dev)
213184610Salfred{
214192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
215184610Salfred	struct ucycom_softc *sc = device_get_softc(dev);
216184610Salfred	void *urd_ptr = NULL;
217184610Salfred	int32_t error;
218184610Salfred	uint16_t urd_len;
219184610Salfred	uint8_t iface_index;
220184610Salfred
221184610Salfred	sc->sc_udev = uaa->device;
222184610Salfred
223194228Sthompsa	device_set_usb_desc(dev);
224189265Sthompsa	mtx_init(&sc->sc_mtx, "ucycom", NULL, MTX_DEF);
225239180Shselasky	ucom_ref(&sc->sc_super_ucom);
226184610Salfred
227184610Salfred	snprintf(sc->sc_name, sizeof(sc->sc_name),
228184610Salfred	    "%s", device_get_nameunit(dev));
229184610Salfred
230184610Salfred	DPRINTF("\n");
231184610Salfred
232184610Salfred	/* get chip model */
233184610Salfred	sc->sc_model = USB_GET_DRIVER_INFO(uaa);
234184610Salfred	if (sc->sc_model == 0) {
235184610Salfred		device_printf(dev, "unsupported device\n");
236184610Salfred		goto detach;
237184610Salfred	}
238184610Salfred	device_printf(dev, "Cypress CY7C%X USB to RS232 bridge\n", sc->sc_model);
239184610Salfred
240184610Salfred	/* get report descriptor */
241184610Salfred
242194228Sthompsa	error = usbd_req_get_hid_desc(uaa->device, NULL,
243184610Salfred	    &urd_ptr, &urd_len, M_USBDEV,
244184610Salfred	    UCYCOM_IFACE_INDEX);
245184610Salfred
246184610Salfred	if (error) {
247184610Salfred		device_printf(dev, "failed to get report "
248184610Salfred		    "descriptor: %s\n",
249194228Sthompsa		    usbd_errstr(error));
250184610Salfred		goto detach;
251184610Salfred	}
252184610Salfred	/* get report sizes */
253184610Salfred
254184610Salfred	sc->sc_flen = hid_report_size(urd_ptr, urd_len, hid_feature, &sc->sc_fid);
255184610Salfred	sc->sc_ilen = hid_report_size(urd_ptr, urd_len, hid_input, &sc->sc_iid);
256184610Salfred	sc->sc_olen = hid_report_size(urd_ptr, urd_len, hid_output, &sc->sc_oid);
257184610Salfred
258184610Salfred	if ((sc->sc_ilen > UCYCOM_MAX_IOLEN) || (sc->sc_ilen < 1) ||
259184610Salfred	    (sc->sc_olen > UCYCOM_MAX_IOLEN) || (sc->sc_olen < 2) ||
260184610Salfred	    (sc->sc_flen > UCYCOM_MAX_IOLEN) || (sc->sc_flen < 5)) {
261184610Salfred		device_printf(dev, "invalid report size i=%d, o=%d, f=%d, max=%d\n",
262184610Salfred		    sc->sc_ilen, sc->sc_olen, sc->sc_flen,
263184610Salfred		    UCYCOM_MAX_IOLEN);
264184610Salfred		goto detach;
265184610Salfred	}
266184610Salfred	sc->sc_iface_no = uaa->info.bIfaceNum;
267184610Salfred
268184610Salfred	iface_index = UCYCOM_IFACE_INDEX;
269194228Sthompsa	error = usbd_transfer_setup(uaa->device, &iface_index,
270187259Sthompsa	    sc->sc_xfer, ucycom_config, UCYCOM_N_TRANSFER,
271189265Sthompsa	    sc, &sc->sc_mtx);
272184610Salfred	if (error) {
273184610Salfred		device_printf(dev, "allocating USB "
274199816Sthompsa		    "transfers failed\n");
275184610Salfred		goto detach;
276184610Salfred	}
277194228Sthompsa	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
278189265Sthompsa	    &ucycom_callback, &sc->sc_mtx);
279184610Salfred	if (error) {
280184610Salfred		goto detach;
281184610Salfred	}
282214843Sn_hibma	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
283214843Sn_hibma
284184610Salfred	if (urd_ptr) {
285184610Salfred		free(urd_ptr, M_USBDEV);
286184610Salfred	}
287214843Sn_hibma
288184610Salfred	return (0);			/* success */
289184610Salfred
290184610Salfreddetach:
291184610Salfred	if (urd_ptr) {
292184610Salfred		free(urd_ptr, M_USBDEV);
293184610Salfred	}
294184610Salfred	ucycom_detach(dev);
295184610Salfred	return (ENXIO);
296184610Salfred}
297184610Salfred
298184610Salfredstatic int
299184610Salfreducycom_detach(device_t dev)
300184610Salfred{
301184610Salfred	struct ucycom_softc *sc = device_get_softc(dev);
302184610Salfred
303214761Sn_hibma	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
304194228Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, UCYCOM_N_TRANSFER);
305184610Salfred
306184610Salfred	return (0);
307184610Salfred}
308184610Salfred
309239180ShselaskyUCOM_UNLOAD_DRAIN(ucycom);
310239180Shselasky
311184610Salfredstatic void
312239180Shselaskyucycom_free_softc(device_t dev, void *arg)
313239180Shselasky{
314239180Shselasky	struct ucycom_softc *sc = arg;
315239180Shselasky
316239180Shselasky	if (ucom_unref(&sc->sc_super_ucom)) {
317239180Shselasky		if (mtx_initialized(&sc->sc_mtx))
318239180Shselasky			mtx_destroy(&sc->sc_mtx);
319239180Shselasky		device_free_softc(dev, sc);
320239180Shselasky	}
321239180Shselasky}
322239180Shselasky
323239180Shselaskystatic void
324239180Shselaskyucycom_free(struct ucom_softc *ucom)
325239180Shselasky{
326239180Shselasky	ucycom_free_softc(NULL, ucom->sc_parent);
327239180Shselasky}
328239180Shselasky
329239180Shselaskystatic void
330192984Sthompsaucycom_cfg_open(struct ucom_softc *ucom)
331184610Salfred{
332184610Salfred	struct ucycom_softc *sc = ucom->sc_parent;
333184610Salfred
334184610Salfred	/* set default configuration */
335184610Salfred	ucycom_cfg_write(sc, UCYCOM_DEFAULT_RATE, UCYCOM_DEFAULT_CFG);
336184610Salfred}
337184610Salfred
338184610Salfredstatic void
339192984Sthompsaucycom_start_read(struct ucom_softc *ucom)
340184610Salfred{
341184610Salfred	struct ucycom_softc *sc = ucom->sc_parent;
342184610Salfred
343194228Sthompsa	usbd_transfer_start(sc->sc_xfer[UCYCOM_INTR_RD]);
344184610Salfred}
345184610Salfred
346184610Salfredstatic void
347192984Sthompsaucycom_stop_read(struct ucom_softc *ucom)
348184610Salfred{
349184610Salfred	struct ucycom_softc *sc = ucom->sc_parent;
350184610Salfred
351194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[UCYCOM_INTR_RD]);
352184610Salfred}
353184610Salfred
354184610Salfredstatic void
355192984Sthompsaucycom_start_write(struct ucom_softc *ucom)
356184610Salfred{
357184610Salfred	struct ucycom_softc *sc = ucom->sc_parent;
358184610Salfred
359194228Sthompsa	usbd_transfer_start(sc->sc_xfer[UCYCOM_CTRL_RD]);
360184610Salfred}
361184610Salfred
362184610Salfredstatic void
363192984Sthompsaucycom_stop_write(struct ucom_softc *ucom)
364184610Salfred{
365184610Salfred	struct ucycom_softc *sc = ucom->sc_parent;
366184610Salfred
367194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[UCYCOM_CTRL_RD]);
368184610Salfred}
369184610Salfred
370184610Salfredstatic void
371194677Sthompsaucycom_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error)
372184610Salfred{
373194677Sthompsa	struct ucycom_softc *sc = usbd_xfer_softc(xfer);
374192984Sthompsa	struct usb_device_request req;
375194677Sthompsa	struct usb_page_cache *pc0, *pc1;
376184610Salfred	uint8_t data[2];
377184610Salfred	uint8_t offset;
378184610Salfred	uint32_t actlen;
379184610Salfred
380194677Sthompsa	pc0 = usbd_xfer_get_frame(xfer, 0);
381194677Sthompsa	pc1 = usbd_xfer_get_frame(xfer, 1);
382194677Sthompsa
383184610Salfred	switch (USB_GET_STATE(xfer)) {
384184610Salfred	case USB_ST_TRANSFERRED:
385184610Salfredtr_transferred:
386184610Salfred	case USB_ST_SETUP:
387184610Salfred
388184610Salfred		switch (sc->sc_model) {
389184610Salfred		case MODEL_CY7C63743:
390184610Salfred			offset = 1;
391184610Salfred			break;
392184610Salfred		case MODEL_CY7C64013:
393184610Salfred			offset = 2;
394184610Salfred			break;
395184610Salfred		default:
396184610Salfred			offset = 0;
397184610Salfred			break;
398184610Salfred		}
399184610Salfred
400194677Sthompsa		if (ucom_get_data(&sc->sc_ucom, pc1, offset,
401184610Salfred		    sc->sc_olen - offset, &actlen)) {
402184610Salfred
403184610Salfred			req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
404184610Salfred			req.bRequest = UR_SET_REPORT;
405184610Salfred			USETW2(req.wValue, UHID_OUTPUT_REPORT, sc->sc_oid);
406184610Salfred			req.wIndex[0] = sc->sc_iface_no;
407184610Salfred			req.wIndex[1] = 0;
408184610Salfred			USETW(req.wLength, sc->sc_olen);
409184610Salfred
410184610Salfred			switch (sc->sc_model) {
411184610Salfred			case MODEL_CY7C63743:
412184610Salfred				data[0] = actlen;
413184610Salfred				break;
414184610Salfred			case MODEL_CY7C64013:
415184610Salfred				data[0] = 0;
416184610Salfred				data[1] = actlen;
417184610Salfred				break;
418184610Salfred			default:
419184610Salfred				break;
420184610Salfred			}
421184610Salfred
422194677Sthompsa			usbd_copy_in(pc0, 0, &req, sizeof(req));
423194677Sthompsa			usbd_copy_in(pc1, 0, data, offset);
424184610Salfred
425194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
426194677Sthompsa			usbd_xfer_set_frame_len(xfer, 1, sc->sc_olen);
427194677Sthompsa			usbd_xfer_set_frames(xfer, sc->sc_olen ? 2 : 1);
428194228Sthompsa			usbd_transfer_submit(xfer);
429184610Salfred		}
430184610Salfred		return;
431184610Salfred
432184610Salfred	default:			/* Error */
433194677Sthompsa		if (error == USB_ERR_CANCELLED) {
434184610Salfred			return;
435184610Salfred		}
436184610Salfred		DPRINTF("error=%s\n",
437194677Sthompsa		    usbd_errstr(error));
438184610Salfred		goto tr_transferred;
439184610Salfred	}
440184610Salfred}
441184610Salfred
442184610Salfredstatic void
443184610Salfreducycom_cfg_write(struct ucycom_softc *sc, uint32_t baud, uint8_t cfg)
444184610Salfred{
445192984Sthompsa	struct usb_device_request req;
446184610Salfred	uint16_t len;
447193045Sthompsa	usb_error_t err;
448184610Salfred
449184610Salfred	len = sc->sc_flen;
450184610Salfred	if (len > sizeof(sc->sc_temp_cfg)) {
451184610Salfred		len = sizeof(sc->sc_temp_cfg);
452184610Salfred	}
453184610Salfred	sc->sc_cfg = cfg;
454184610Salfred
455184610Salfred	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
456184610Salfred	req.bRequest = UR_SET_REPORT;
457184610Salfred	USETW2(req.wValue, UHID_FEATURE_REPORT, sc->sc_fid);
458184610Salfred	req.wIndex[0] = sc->sc_iface_no;
459184610Salfred	req.wIndex[1] = 0;
460184610Salfred	USETW(req.wLength, len);
461184610Salfred
462184610Salfred	sc->sc_temp_cfg[0] = (baud & 0xff);
463184610Salfred	sc->sc_temp_cfg[1] = (baud >> 8) & 0xff;
464184610Salfred	sc->sc_temp_cfg[2] = (baud >> 16) & 0xff;
465184610Salfred	sc->sc_temp_cfg[3] = (baud >> 24) & 0xff;
466184610Salfred	sc->sc_temp_cfg[4] = cfg;
467184610Salfred
468194228Sthompsa	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
469188413Sthompsa	    &req, sc->sc_temp_cfg, 0, 1000);
470184610Salfred	if (err) {
471184610Salfred		DPRINTFN(0, "device request failed, err=%s "
472194228Sthompsa		    "(ignored)\n", usbd_errstr(err));
473184610Salfred	}
474184610Salfred}
475184610Salfred
476184610Salfredstatic int
477192984Sthompsaucycom_pre_param(struct ucom_softc *ucom, struct termios *t)
478184610Salfred{
479184610Salfred	switch (t->c_ospeed) {
480184610Salfred		case 600:
481184610Salfred		case 1200:
482184610Salfred		case 2400:
483184610Salfred		case 4800:
484184610Salfred		case 9600:
485184610Salfred		case 19200:
486184610Salfred		case 38400:
487184610Salfred		case 57600:
488184610Salfred#if 0
489184610Salfred		/*
490184610Salfred		 * Stock chips only support standard baud rates in the 600 - 57600
491184610Salfred		 * range, but higher rates can be achieved using custom firmware.
492184610Salfred		 */
493184610Salfred		case 115200:
494184610Salfred		case 153600:
495184610Salfred		case 192000:
496184610Salfred#endif
497184610Salfred		break;
498184610Salfred	default:
499184610Salfred		return (EINVAL);
500184610Salfred	}
501184610Salfred	return (0);
502184610Salfred}
503184610Salfred
504184610Salfredstatic void
505192984Sthompsaucycom_cfg_param(struct ucom_softc *ucom, struct termios *t)
506184610Salfred{
507184610Salfred	struct ucycom_softc *sc = ucom->sc_parent;
508184610Salfred	uint8_t cfg;
509184610Salfred
510184610Salfred	DPRINTF("\n");
511184610Salfred
512184610Salfred	if (t->c_cflag & CIGNORE) {
513184610Salfred		cfg = sc->sc_cfg;
514184610Salfred	} else {
515184610Salfred		cfg = 0;
516184610Salfred		switch (t->c_cflag & CSIZE) {
517184610Salfred		default:
518184610Salfred		case CS8:
519184610Salfred			++cfg;
520184610Salfred		case CS7:
521184610Salfred			++cfg;
522184610Salfred		case CS6:
523184610Salfred			++cfg;
524184610Salfred		case CS5:
525184610Salfred			break;
526184610Salfred		}
527184610Salfred
528184610Salfred		if (t->c_cflag & CSTOPB)
529184610Salfred			cfg |= UCYCOM_CFG_STOPB;
530184610Salfred		if (t->c_cflag & PARENB)
531184610Salfred			cfg |= UCYCOM_CFG_PAREN;
532184610Salfred		if (t->c_cflag & PARODD)
533184610Salfred			cfg |= UCYCOM_CFG_PARODD;
534184610Salfred	}
535184610Salfred
536184610Salfred	ucycom_cfg_write(sc, t->c_ospeed, cfg);
537184610Salfred}
538184610Salfred
539184610Salfredstatic void
540194677Sthompsaucycom_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
541184610Salfred{
542194677Sthompsa	struct ucycom_softc *sc = usbd_xfer_softc(xfer);
543194677Sthompsa	struct usb_page_cache *pc;
544184610Salfred	uint8_t buf[2];
545184610Salfred	uint32_t offset;
546233774Shselasky	int len;
547194677Sthompsa	int actlen;
548184610Salfred
549194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
550194677Sthompsa	pc = usbd_xfer_get_frame(xfer, 0);
551194677Sthompsa
552184610Salfred	switch (USB_GET_STATE(xfer)) {
553184610Salfred	case USB_ST_TRANSFERRED:
554184610Salfred		switch (sc->sc_model) {
555184610Salfred		case MODEL_CY7C63743:
556194677Sthompsa			if (actlen < 1) {
557184610Salfred				goto tr_setup;
558184610Salfred			}
559194677Sthompsa			usbd_copy_out(pc, 0, buf, 1);
560184610Salfred
561184610Salfred			sc->sc_ist = buf[0] & ~0x07;
562184610Salfred			len = buf[0] & 0x07;
563184610Salfred
564194677Sthompsa			actlen--;
565184610Salfred			offset = 1;
566184610Salfred
567184610Salfred			break;
568184610Salfred
569184610Salfred		case MODEL_CY7C64013:
570194677Sthompsa			if (actlen < 2) {
571184610Salfred				goto tr_setup;
572184610Salfred			}
573194677Sthompsa			usbd_copy_out(pc, 0, buf, 2);
574184610Salfred
575184610Salfred			sc->sc_ist = buf[0] & ~0x07;
576184610Salfred			len = buf[1];
577184610Salfred
578194677Sthompsa			actlen -= 2;
579184610Salfred			offset = 2;
580184610Salfred
581184610Salfred			break;
582184610Salfred
583184610Salfred		default:
584199816Sthompsa			DPRINTFN(0, "unsupported model number\n");
585184610Salfred			goto tr_setup;
586184610Salfred		}
587184610Salfred
588194677Sthompsa		if (len > actlen)
589194677Sthompsa			len = actlen;
590194677Sthompsa		if (len)
591194677Sthompsa			ucom_put_data(&sc->sc_ucom, pc, offset, len);
592194677Sthompsa		/* FALLTHROUGH */
593184610Salfred	case USB_ST_SETUP:
594184610Salfredtr_setup:
595194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, sc->sc_ilen);
596194228Sthompsa		usbd_transfer_submit(xfer);
597184610Salfred		return;
598184610Salfred
599184610Salfred	default:			/* Error */
600194677Sthompsa		if (error != USB_ERR_CANCELLED) {
601188413Sthompsa			/* try to clear stall first */
602194677Sthompsa			usbd_xfer_set_stall(xfer);
603188413Sthompsa			goto tr_setup;
604184610Salfred		}
605184610Salfred		return;
606184610Salfred
607184610Salfred	}
608184610Salfred}
609197570Sthompsa
610197570Sthompsastatic void
611197570Sthompsaucycom_poll(struct ucom_softc *ucom)
612197570Sthompsa{
613197570Sthompsa	struct ucycom_softc *sc = ucom->sc_parent;
614197570Sthompsa	usbd_transfer_poll(sc->sc_xfer, UCYCOM_N_TRANSFER);
615197570Sthompsa}
616