uslcom.c revision 238804
1/*	$OpenBSD: uslcom.c,v 1.17 2007/11/24 10:52:12 jsg Exp $	*/
2
3#include <sys/cdefs.h>
4__FBSDID("$FreeBSD: head/sys/dev/usb/serial/uslcom.c 238804 2012-07-26 12:18:23Z gavin $");
5
6/*
7 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */
21
22#include <sys/stdint.h>
23#include <sys/stddef.h>
24#include <sys/param.h>
25#include <sys/queue.h>
26#include <sys/types.h>
27#include <sys/systm.h>
28#include <sys/kernel.h>
29#include <sys/bus.h>
30#include <sys/module.h>
31#include <sys/lock.h>
32#include <sys/mutex.h>
33#include <sys/condvar.h>
34#include <sys/sysctl.h>
35#include <sys/sx.h>
36#include <sys/unistd.h>
37#include <sys/callout.h>
38#include <sys/malloc.h>
39#include <sys/priv.h>
40
41#include <dev/usb/usb.h>
42#include <dev/usb/usbdi.h>
43#include <dev/usb/usbdi_util.h>
44#include <dev/usb/usb_ioctl.h>
45#include "usbdevs.h"
46
47#define	USB_DEBUG_VAR uslcom_debug
48#include <dev/usb/usb_debug.h>
49#include <dev/usb/usb_process.h>
50
51#include <dev/usb/serial/usb_serial.h>
52
53#ifdef USB_DEBUG
54static int uslcom_debug = 0;
55
56static SYSCTL_NODE(_hw_usb, OID_AUTO, uslcom, CTLFLAG_RW, 0, "USB uslcom");
57SYSCTL_INT(_hw_usb_uslcom, OID_AUTO, debug, CTLFLAG_RW,
58    &uslcom_debug, 0, "Debug level");
59#endif
60
61#define	USLCOM_BULK_BUF_SIZE		1024
62#define	USLCOM_CONFIG_INDEX	0
63#define	USLCOM_IFACE_INDEX	0
64
65#define	USLCOM_SET_DATA_BITS(x)	((x) << 8)
66
67/* Request types */
68#define	USLCOM_WRITE		0x41
69#define	USLCOM_READ		0xc1
70
71/* Request codes */
72#define	USLCOM_UART		0x00
73#define	USLCOM_SET_BAUD_DIV	0x01
74#define	USLCOM_DATA		0x03
75#define	USLCOM_BREAK		0x05
76#define	USLCOM_CTRL		0x07
77#define	USLCOM_RCTRL            0x08
78#define	USLCOM_SET_FLOWCTRL     0x13
79#define	USLCOM_SET_BAUD_RATE	0x1e
80#define	USLCOM_VENDOR_SPECIFIC	0xff
81
82/* USLCOM_UART values */
83#define	USLCOM_UART_DISABLE	0x00
84#define	USLCOM_UART_ENABLE	0x01
85
86/* USLCOM_CTRL/USLCOM_RCTRL values */
87#define	USLCOM_CTRL_DTR_ON	0x0001
88#define	USLCOM_CTRL_DTR_SET	0x0100
89#define	USLCOM_CTRL_RTS_ON	0x0002
90#define	USLCOM_CTRL_RTS_SET	0x0200
91#define	USLCOM_CTRL_CTS		0x0010
92#define	USLCOM_CTRL_DSR		0x0020
93#define	USLCOM_CTRL_RI          0x0040
94#define	USLCOM_CTRL_DCD		0x0080
95
96/* USLCOM_SET_BAUD_DIV values */
97#define	USLCOM_BAUD_REF		3686400 /* 3.6864 MHz */
98
99/* USLCOM_DATA values */
100#define	USLCOM_STOP_BITS_1	0x00
101#define	USLCOM_STOP_BITS_2	0x02
102#define	USLCOM_PARITY_NONE	0x00
103#define	USLCOM_PARITY_ODD	0x10
104#define	USLCOM_PARITY_EVEN	0x20
105
106#define	USLCOM_PORT_NO		0x0000
107
108/* USLCOM_BREAK values */
109#define	USLCOM_BREAK_OFF	0x00
110#define	USLCOM_BREAK_ON		0x01
111
112/* USLCOM_SET_FLOWCTRL values - 1st word */
113#define	USLCOM_FLOW_DTR_ON      0x00000001 /* DTR static active */
114#define	USLCOM_FLOW_CTS_HS      0x00000008 /* CTS handshake */
115/* USLCOM_SET_FLOWCTRL values - 2nd word */
116#define	USLCOM_FLOW_RTS_ON      0x00000040 /* RTS static active */
117#define	USLCOM_FLOW_RTS_HS      0x00000080 /* RTS handshake */
118
119/* USLCOM_VENDOR_SPECIFIC values */
120#define	USLCOM_WRITE_LATCH	0x37E1
121#define	USLCOM_READ_LATCH	0x00C2
122
123enum {
124	USLCOM_BULK_DT_WR,
125	USLCOM_BULK_DT_RD,
126	USLCOM_CTRL_DT_RD,
127	USLCOM_N_TRANSFER,
128};
129
130struct uslcom_softc {
131	struct ucom_super_softc sc_super_ucom;
132	struct ucom_softc sc_ucom;
133	struct usb_callout sc_watchdog;
134
135	struct usb_xfer *sc_xfer[USLCOM_N_TRANSFER];
136	struct usb_device *sc_udev;
137	struct mtx sc_mtx;
138
139	uint8_t		 sc_msr;
140	uint8_t		 sc_lsr;
141};
142
143static device_probe_t uslcom_probe;
144static device_attach_t uslcom_attach;
145static device_detach_t uslcom_detach;
146
147static usb_callback_t uslcom_write_callback;
148static usb_callback_t uslcom_read_callback;
149static usb_callback_t uslcom_control_callback;
150
151static void uslcom_open(struct ucom_softc *);
152static void uslcom_close(struct ucom_softc *);
153static void uslcom_set_dtr(struct ucom_softc *, uint8_t);
154static void uslcom_set_rts(struct ucom_softc *, uint8_t);
155static void uslcom_set_break(struct ucom_softc *, uint8_t);
156static int uslcom_ioctl(struct ucom_softc *, uint32_t, caddr_t, int,
157		struct thread *);
158static int uslcom_pre_param(struct ucom_softc *, struct termios *);
159static void uslcom_param(struct ucom_softc *, struct termios *);
160static void uslcom_get_status(struct ucom_softc *, uint8_t *, uint8_t *);
161static void uslcom_start_read(struct ucom_softc *);
162static void uslcom_stop_read(struct ucom_softc *);
163static void uslcom_start_write(struct ucom_softc *);
164static void uslcom_stop_write(struct ucom_softc *);
165static void uslcom_poll(struct ucom_softc *ucom);
166
167static const struct usb_config uslcom_config[USLCOM_N_TRANSFER] = {
168
169	[USLCOM_BULK_DT_WR] = {
170		.type = UE_BULK,
171		.endpoint = UE_ADDR_ANY,
172		.direction = UE_DIR_OUT,
173		.bufsize = USLCOM_BULK_BUF_SIZE,
174               .flags = {.pipe_bof = 1,},
175		.callback = &uslcom_write_callback,
176	},
177
178	[USLCOM_BULK_DT_RD] = {
179		.type = UE_BULK,
180		.endpoint = UE_ADDR_ANY,
181		.direction = UE_DIR_IN,
182		.bufsize = USLCOM_BULK_BUF_SIZE,
183		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
184		.callback = &uslcom_read_callback,
185	},
186	[USLCOM_CTRL_DT_RD] = {
187		.type = UE_CONTROL,
188		.endpoint = 0x00,
189		.direction = UE_DIR_ANY,
190		.bufsize = sizeof(struct usb_device_request) + 8,
191		.flags = {.pipe_bof = 1,},
192		.callback = &uslcom_control_callback,
193		.timeout = 1000,	/* 1 second timeout */
194	},
195};
196
197static struct ucom_callback uslcom_callback = {
198	.ucom_cfg_open = &uslcom_open,
199	.ucom_cfg_close = &uslcom_close,
200	.ucom_cfg_get_status = &uslcom_get_status,
201	.ucom_cfg_set_dtr = &uslcom_set_dtr,
202	.ucom_cfg_set_rts = &uslcom_set_rts,
203	.ucom_cfg_set_break = &uslcom_set_break,
204	.ucom_ioctl = &uslcom_ioctl,
205	.ucom_cfg_param = &uslcom_param,
206	.ucom_pre_param = &uslcom_pre_param,
207	.ucom_start_read = &uslcom_start_read,
208	.ucom_stop_read = &uslcom_stop_read,
209	.ucom_start_write = &uslcom_start_write,
210	.ucom_stop_write = &uslcom_stop_write,
211	.ucom_poll = &uslcom_poll,
212};
213
214static const STRUCT_USB_HOST_ID uslcom_devs[] = {
215#define	USLCOM_DEV(v,p)  { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
216    USLCOM_DEV(BALTECH, CARDREADER),
217    USLCOM_DEV(CLIPSAL, 5000CT2),
218    USLCOM_DEV(CLIPSAL, 5500PACA),
219    USLCOM_DEV(CLIPSAL, 5500PCU),
220    USLCOM_DEV(CLIPSAL, 560884),
221    USLCOM_DEV(CLIPSAL, 5800PC),
222    USLCOM_DEV(CLIPSAL, C5000CT2),
223    USLCOM_DEV(CLIPSAL, L51xx),
224    USLCOM_DEV(DATAAPEX, MULTICOM),
225    USLCOM_DEV(DELL, DW700),
226    USLCOM_DEV(DIGIANSWER, ZIGBEE802154),
227    USLCOM_DEV(DYNASTREAM, ANTDEVBOARD),
228    USLCOM_DEV(DYNASTREAM, ANTDEVBOARD2),
229    USLCOM_DEV(DYNASTREAM, ANT2USB),
230    USLCOM_DEV(ELV, USBI2C),
231    USLCOM_DEV(FESTO, CMSP),
232    USLCOM_DEV(FESTO, CPX_USB),
233    USLCOM_DEV(FOXCONN, PIRELLI_DP_L10),
234    USLCOM_DEV(FOXCONN, TCOM_TC_300),
235    USLCOM_DEV(GEMALTO, PROXPU),
236    USLCOM_DEV(JABLOTRON, PC60B),
237    USLCOM_DEV(KAMSTRUP, OPTICALEYE),
238    USLCOM_DEV(KAMSTRUP, MBUS_250D),
239    USLCOM_DEV(LINKINSTRUMENTS, MSO19),
240    USLCOM_DEV(LINKINSTRUMENTS, MSO28),
241    USLCOM_DEV(LINKINSTRUMENTS, MSO28_2),
242    USLCOM_DEV(MEI, CASHFLOW_SC),
243    USLCOM_DEV(MEI, S2000),
244    USLCOM_DEV(OWEN, AC4),
245    USLCOM_DEV(PHILIPS, ACE1001),
246    USLCOM_DEV(PLX, CA42),
247    USLCOM_DEV(RENESAS, RX610),
248    USLCOM_DEV(SILABS, AC_SERV_CAN),
249    USLCOM_DEV(SILABS, AC_SERV_CIS),
250    USLCOM_DEV(SILABS, AC_SERV_IBUS),
251    USLCOM_DEV(SILABS, AC_SERV_OBD),
252    USLCOM_DEV(SILABS, AEROCOMM),
253    USLCOM_DEV(SILABS, AMBER_AMB2560),
254    USLCOM_DEV(SILABS, ARGUSISP),
255    USLCOM_DEV(SILABS, ARKHAM_DS101_A),
256    USLCOM_DEV(SILABS, ARKHAM_DS101_M),
257    USLCOM_DEV(SILABS, ARYGON_MIFARE),
258    USLCOM_DEV(SILABS, AVIT_USB_TTL),
259    USLCOM_DEV(SILABS, B_G_H3000),
260    USLCOM_DEV(SILABS, BALLUFF_RFID),
261    USLCOM_DEV(SILABS, BEI_VCP),
262    USLCOM_DEV(SILABS, BSM7DUSB),
263    USLCOM_DEV(SILABS, BURNSIDE),
264    USLCOM_DEV(SILABS, C2_EDGE_MODEM),
265    USLCOM_DEV(SILABS, CP2102),
266    USLCOM_DEV(SILABS, CP210X_2),
267    USLCOM_DEV(SILABS, CP210X_3),
268    USLCOM_DEV(SILABS, CP210X_4),
269    USLCOM_DEV(SILABS, CRUMB128),
270    USLCOM_DEV(SILABS, CYGNAL),
271    USLCOM_DEV(SILABS, CYGNAL_DEBUG),
272    USLCOM_DEV(SILABS, CYGNAL_GPS),
273    USLCOM_DEV(SILABS, DEGREE),
274    USLCOM_DEV(SILABS, DEKTEK_DTAPLUS),
275    USLCOM_DEV(SILABS, EMS_C1007),
276    USLCOM_DEV(SILABS, HAMLINKUSB),
277    USLCOM_DEV(SILABS, HELICOM),
278    USLCOM_DEV(SILABS, IMS_USB_RS422),
279    USLCOM_DEV(SILABS, INFINITY_MIC),
280    USLCOM_DEV(SILABS, INSYS_MODEM),
281    USLCOM_DEV(SILABS, IRZ_SG10),
282    USLCOM_DEV(SILABS, KYOCERA_GPS),
283    USLCOM_DEV(SILABS, LIPOWSKY_HARP),
284    USLCOM_DEV(SILABS, LIPOWSKY_JTAG),
285    USLCOM_DEV(SILABS, LIPOWSKY_LIN),
286    USLCOM_DEV(SILABS, MC35PU),
287    USLCOM_DEV(SILABS, MJS_TOSLINK),
288    USLCOM_DEV(SILABS, MSD_DASHHAWK),
289    USLCOM_DEV(SILABS, MULTIPLEX_RC),
290    USLCOM_DEV(SILABS, OPTRIS_MSPRO),
291    USLCOM_DEV(SILABS, POLOLU),
292    USLCOM_DEV(SILABS, PROCYON_AVS),
293    USLCOM_DEV(SILABS, SB_PARAMOUNT_ME),
294    USLCOM_DEV(SILABS, SUUNTO),
295    USLCOM_DEV(SILABS, TAMSMASTER),
296    USLCOM_DEV(SILABS, TELEGESIS_ETRX2),
297    USLCOM_DEV(SILABS, TRACIENT),
298    USLCOM_DEV(SILABS, TRAQMATE),
299    USLCOM_DEV(SILABS, USBCOUNT50),
300    USLCOM_DEV(SILABS, USBPULSE100),
301    USLCOM_DEV(SILABS, USBSCOPE50),
302    USLCOM_DEV(SILABS, USBWAVE12),
303    USLCOM_DEV(SILABS, VSTABI),
304    USLCOM_DEV(SILABS, WAVIT),
305    USLCOM_DEV(SILABS, WMRBATT),
306    USLCOM_DEV(SILABS, WMRRIGBLASTER),
307    USLCOM_DEV(SILABS, WMRRIGTALK),
308    USLCOM_DEV(SILABS, ZEPHYR_BIO),
309    USLCOM_DEV(SILABS2, DCU11CLONE),
310    USLCOM_DEV(SILABS3, GPRS_MODEM),
311    USLCOM_DEV(SILABS4, 100EU_MODEM),
312    USLCOM_DEV(SYNTECH, CYPHERLAB100),
313    USLCOM_DEV(USI, MC60),
314    USLCOM_DEV(VAISALA, CABLE),
315    USLCOM_DEV(WAGO, SERVICECABLE),
316    USLCOM_DEV(WAVESENSE, JAZZ),
317    USLCOM_DEV(WIENERPLEINBAUS, PL512),
318    USLCOM_DEV(WIENERPLEINBAUS, RCM),
319    USLCOM_DEV(WIENERPLEINBAUS, MPOD),
320    USLCOM_DEV(WIENERPLEINBAUS, CML),
321#undef USLCOM_DEV
322};
323
324static device_method_t uslcom_methods[] = {
325	DEVMETHOD(device_probe, uslcom_probe),
326	DEVMETHOD(device_attach, uslcom_attach),
327	DEVMETHOD(device_detach, uslcom_detach),
328	{0, 0}
329};
330
331static devclass_t uslcom_devclass;
332
333static driver_t uslcom_driver = {
334	.name = "uslcom",
335	.methods = uslcom_methods,
336	.size = sizeof(struct uslcom_softc),
337};
338
339DRIVER_MODULE(uslcom, uhub, uslcom_driver, uslcom_devclass, NULL, 0);
340MODULE_DEPEND(uslcom, ucom, 1, 1, 1);
341MODULE_DEPEND(uslcom, usb, 1, 1, 1);
342MODULE_VERSION(uslcom, 1);
343
344static void
345uslcom_watchdog(void *arg)
346{
347	struct uslcom_softc *sc = arg;
348
349	mtx_assert(&sc->sc_mtx, MA_OWNED);
350
351	usbd_transfer_start(sc->sc_xfer[USLCOM_CTRL_DT_RD]);
352
353	usb_callout_reset(&sc->sc_watchdog,
354	    hz / 4, &uslcom_watchdog, sc);
355}
356
357static int
358uslcom_probe(device_t dev)
359{
360	struct usb_attach_arg *uaa = device_get_ivars(dev);
361
362	DPRINTFN(11, "\n");
363
364	if (uaa->usb_mode != USB_MODE_HOST) {
365		return (ENXIO);
366	}
367	if (uaa->info.bConfigIndex != USLCOM_CONFIG_INDEX) {
368		return (ENXIO);
369	}
370	if (uaa->info.bIfaceIndex != USLCOM_IFACE_INDEX) {
371		return (ENXIO);
372	}
373	return (usbd_lookup_id_by_uaa(uslcom_devs, sizeof(uslcom_devs), uaa));
374}
375
376static int
377uslcom_attach(device_t dev)
378{
379	struct usb_attach_arg *uaa = device_get_ivars(dev);
380	struct uslcom_softc *sc = device_get_softc(dev);
381	int error;
382
383	DPRINTFN(11, "\n");
384
385	device_set_usb_desc(dev);
386	mtx_init(&sc->sc_mtx, "uslcom", NULL, MTX_DEF);
387	usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
388
389	sc->sc_udev = uaa->device;
390
391	error = usbd_transfer_setup(uaa->device,
392	    &uaa->info.bIfaceIndex, sc->sc_xfer, uslcom_config,
393	    USLCOM_N_TRANSFER, sc, &sc->sc_mtx);
394	if (error) {
395		DPRINTF("one or more missing USB endpoints, "
396		    "error=%s\n", usbd_errstr(error));
397		goto detach;
398	}
399	/* clear stall at first run */
400	mtx_lock(&sc->sc_mtx);
401	usbd_xfer_set_stall(sc->sc_xfer[USLCOM_BULK_DT_WR]);
402	usbd_xfer_set_stall(sc->sc_xfer[USLCOM_BULK_DT_RD]);
403	mtx_unlock(&sc->sc_mtx);
404
405	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
406	    &uslcom_callback, &sc->sc_mtx);
407	if (error) {
408		goto detach;
409	}
410	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
411
412	return (0);
413
414detach:
415	uslcom_detach(dev);
416	return (ENXIO);
417}
418
419static int
420uslcom_detach(device_t dev)
421{
422	struct uslcom_softc *sc = device_get_softc(dev);
423
424	DPRINTF("sc=%p\n", sc);
425
426	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
427	usbd_transfer_unsetup(sc->sc_xfer, USLCOM_N_TRANSFER);
428
429	usb_callout_drain(&sc->sc_watchdog);
430	mtx_destroy(&sc->sc_mtx);
431
432	return (0);
433}
434
435static void
436uslcom_open(struct ucom_softc *ucom)
437{
438	struct uslcom_softc *sc = ucom->sc_parent;
439	struct usb_device_request req;
440
441	req.bmRequestType = USLCOM_WRITE;
442	req.bRequest = USLCOM_UART;
443	USETW(req.wValue, USLCOM_UART_ENABLE);
444	USETW(req.wIndex, USLCOM_PORT_NO);
445	USETW(req.wLength, 0);
446
447        if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
448	    &req, NULL, 0, 1000)) {
449		DPRINTF("UART enable failed (ignored)\n");
450	}
451
452	/* start polling status */
453	uslcom_watchdog(sc);
454}
455
456static void
457uslcom_close(struct ucom_softc *ucom)
458{
459	struct uslcom_softc *sc = ucom->sc_parent;
460	struct usb_device_request req;
461
462	/* stop polling status */
463	usb_callout_stop(&sc->sc_watchdog);
464
465	req.bmRequestType = USLCOM_WRITE;
466	req.bRequest = USLCOM_UART;
467	USETW(req.wValue, USLCOM_UART_DISABLE);
468	USETW(req.wIndex, USLCOM_PORT_NO);
469	USETW(req.wLength, 0);
470
471	if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
472	    &req, NULL, 0, 1000)) {
473		DPRINTF("UART disable failed (ignored)\n");
474	}
475}
476
477static void
478uslcom_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
479{
480        struct uslcom_softc *sc = ucom->sc_parent;
481	struct usb_device_request req;
482	uint16_t ctl;
483
484        DPRINTF("onoff = %d\n", onoff);
485
486	ctl = onoff ? USLCOM_CTRL_DTR_ON : 0;
487	ctl |= USLCOM_CTRL_DTR_SET;
488
489	req.bmRequestType = USLCOM_WRITE;
490	req.bRequest = USLCOM_CTRL;
491	USETW(req.wValue, ctl);
492	USETW(req.wIndex, USLCOM_PORT_NO);
493	USETW(req.wLength, 0);
494
495        if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
496	    &req, NULL, 0, 1000)) {
497		DPRINTF("Setting DTR failed (ignored)\n");
498	}
499}
500
501static void
502uslcom_set_rts(struct ucom_softc *ucom, uint8_t onoff)
503{
504        struct uslcom_softc *sc = ucom->sc_parent;
505	struct usb_device_request req;
506	uint16_t ctl;
507
508        DPRINTF("onoff = %d\n", onoff);
509
510	ctl = onoff ? USLCOM_CTRL_RTS_ON : 0;
511	ctl |= USLCOM_CTRL_RTS_SET;
512
513	req.bmRequestType = USLCOM_WRITE;
514	req.bRequest = USLCOM_CTRL;
515	USETW(req.wValue, ctl);
516	USETW(req.wIndex, USLCOM_PORT_NO);
517	USETW(req.wLength, 0);
518
519        if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
520	    &req, NULL, 0, 1000)) {
521		DPRINTF("Setting DTR failed (ignored)\n");
522	}
523}
524
525static int
526uslcom_pre_param(struct ucom_softc *ucom, struct termios *t)
527{
528	if (t->c_ospeed <= 0 || t->c_ospeed > 921600)
529		return (EINVAL);
530	return (0);
531}
532
533static void
534uslcom_param(struct ucom_softc *ucom, struct termios *t)
535{
536	struct uslcom_softc *sc = ucom->sc_parent;
537	struct usb_device_request req;
538	uint32_t baudrate, flowctrl[4];
539	uint16_t data;
540
541	DPRINTF("\n");
542
543	baudrate = t->c_ospeed;
544	req.bmRequestType = USLCOM_WRITE;
545	req.bRequest = USLCOM_SET_BAUD_RATE;
546	USETW(req.wValue, 0);
547	USETW(req.wIndex, USLCOM_PORT_NO);
548	USETW(req.wLength, sizeof(baudrate));
549
550	if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
551	    &req, &baudrate, 0, 1000)) {
552		DPRINTF("Set baudrate failed (ignored)\n");
553	}
554
555	if (t->c_cflag & CSTOPB)
556		data = USLCOM_STOP_BITS_2;
557	else
558		data = USLCOM_STOP_BITS_1;
559	if (t->c_cflag & PARENB) {
560		if (t->c_cflag & PARODD)
561			data |= USLCOM_PARITY_ODD;
562		else
563			data |= USLCOM_PARITY_EVEN;
564	} else
565		data |= USLCOM_PARITY_NONE;
566	switch (t->c_cflag & CSIZE) {
567	case CS5:
568		data |= USLCOM_SET_DATA_BITS(5);
569		break;
570	case CS6:
571		data |= USLCOM_SET_DATA_BITS(6);
572		break;
573	case CS7:
574		data |= USLCOM_SET_DATA_BITS(7);
575		break;
576	case CS8:
577		data |= USLCOM_SET_DATA_BITS(8);
578		break;
579	}
580
581	req.bmRequestType = USLCOM_WRITE;
582	req.bRequest = USLCOM_DATA;
583	USETW(req.wValue, data);
584	USETW(req.wIndex, USLCOM_PORT_NO);
585	USETW(req.wLength, 0);
586
587        if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
588	    &req, NULL, 0, 1000)) {
589		DPRINTF("Set format failed (ignored)\n");
590	}
591
592	if (t->c_cflag & CRTSCTS) {
593		flowctrl[0] = htole32(USLCOM_FLOW_DTR_ON | USLCOM_FLOW_CTS_HS);
594		flowctrl[1] = htole32(USLCOM_FLOW_RTS_HS);
595		flowctrl[2] = 0;
596		flowctrl[3] = 0;
597	} else {
598		flowctrl[0] = htole32(USLCOM_FLOW_DTR_ON);
599		flowctrl[1] = htole32(USLCOM_FLOW_RTS_ON);
600		flowctrl[2] = 0;
601		flowctrl[3] = 0;
602	}
603	req.bmRequestType = USLCOM_WRITE;
604	req.bRequest = USLCOM_SET_FLOWCTRL;
605	USETW(req.wValue, 0);
606	USETW(req.wIndex, USLCOM_PORT_NO);
607	USETW(req.wLength, sizeof(flowctrl));
608
609	if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
610	    &req, flowctrl, 0, 1000)) {
611		DPRINTF("Set flowcontrol failed (ignored)\n");
612	}
613}
614
615static void
616uslcom_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
617{
618	struct uslcom_softc *sc = ucom->sc_parent;
619
620	DPRINTF("\n");
621
622	*lsr = sc->sc_lsr;
623	*msr = sc->sc_msr;
624}
625
626static void
627uslcom_set_break(struct ucom_softc *ucom, uint8_t onoff)
628{
629        struct uslcom_softc *sc = ucom->sc_parent;
630	struct usb_device_request req;
631	uint16_t brk = onoff ? USLCOM_BREAK_ON : USLCOM_BREAK_OFF;
632
633	req.bmRequestType = USLCOM_WRITE;
634	req.bRequest = USLCOM_BREAK;
635	USETW(req.wValue, brk);
636	USETW(req.wIndex, USLCOM_PORT_NO);
637	USETW(req.wLength, 0);
638
639        if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
640	    &req, NULL, 0, 1000)) {
641		DPRINTF("Set BREAK failed (ignored)\n");
642	}
643}
644
645static int
646uslcom_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data,
647    int flag, struct thread *td)
648{
649	struct uslcom_softc *sc = ucom->sc_parent;
650	struct usb_device_request req;
651	int error = 0;
652	uint8_t latch;
653
654	DPRINTF("cmd=0x%08x\n", cmd);
655
656	switch (cmd) {
657	case USB_GET_GPIO:
658		req.bmRequestType = USLCOM_READ;
659		req.bRequest = USLCOM_VENDOR_SPECIFIC;
660		USETW(req.wValue, USLCOM_READ_LATCH);
661		USETW(req.wIndex, 0);
662		USETW(req.wLength, sizeof(latch));
663
664		if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
665		    &req, &latch, 0, 1000)) {
666			DPRINTF("Get LATCH failed\n");
667			error = EIO;
668		}
669		*(int *)data = latch;
670		break;
671
672	case USB_SET_GPIO:
673		req.bmRequestType = USLCOM_WRITE;
674		req.bRequest = USLCOM_VENDOR_SPECIFIC;
675		USETW(req.wValue, USLCOM_WRITE_LATCH);
676		USETW(req.wIndex, (*(int *)data));
677		USETW(req.wLength, 0);
678
679		if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
680		    &req, NULL, 0, 1000)) {
681			DPRINTF("Set LATCH failed\n");
682			error = EIO;
683		}
684		break;
685
686	default:
687		DPRINTF("Unknown IOCTL\n");
688		error = ENOIOCTL;
689		break;
690	}
691	return (error);
692}
693
694static void
695uslcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
696{
697	struct uslcom_softc *sc = usbd_xfer_softc(xfer);
698	struct usb_page_cache *pc;
699	uint32_t actlen;
700
701	switch (USB_GET_STATE(xfer)) {
702	case USB_ST_SETUP:
703	case USB_ST_TRANSFERRED:
704tr_setup:
705		pc = usbd_xfer_get_frame(xfer, 0);
706		if (ucom_get_data(&sc->sc_ucom, pc, 0,
707		    USLCOM_BULK_BUF_SIZE, &actlen)) {
708
709			DPRINTF("actlen = %d\n", actlen);
710
711			usbd_xfer_set_frame_len(xfer, 0, actlen);
712			usbd_transfer_submit(xfer);
713		}
714		return;
715
716	default:			/* Error */
717		if (error != USB_ERR_CANCELLED) {
718			/* try to clear stall first */
719			usbd_xfer_set_stall(xfer);
720			goto tr_setup;
721		}
722		return;
723	}
724}
725
726static void
727uslcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
728{
729	struct uslcom_softc *sc = usbd_xfer_softc(xfer);
730	struct usb_page_cache *pc;
731	int actlen;
732
733	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
734
735	switch (USB_GET_STATE(xfer)) {
736	case USB_ST_TRANSFERRED:
737		pc = usbd_xfer_get_frame(xfer, 0);
738		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
739
740	case USB_ST_SETUP:
741tr_setup:
742		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
743		usbd_transfer_submit(xfer);
744		return;
745
746	default:			/* Error */
747		if (error != USB_ERR_CANCELLED) {
748			/* try to clear stall first */
749			usbd_xfer_set_stall(xfer);
750			goto tr_setup;
751		}
752		return;
753	}
754}
755
756static void
757uslcom_control_callback(struct usb_xfer *xfer, usb_error_t error)
758{
759	struct uslcom_softc *sc = usbd_xfer_softc(xfer);
760	struct usb_page_cache *pc;
761	struct usb_device_request req;
762	uint8_t msr = 0;
763	uint8_t buf;
764
765	switch (USB_GET_STATE(xfer)) {
766	case USB_ST_TRANSFERRED:
767		pc = usbd_xfer_get_frame(xfer, 1);
768		usbd_copy_out(pc, 0, &buf, sizeof(buf));
769		if (buf & USLCOM_CTRL_CTS)
770			msr |= SER_CTS;
771		if (buf & USLCOM_CTRL_DSR)
772			msr |= SER_DSR;
773		if (buf & USLCOM_CTRL_RI)
774			msr |= SER_RI;
775		if (buf & USLCOM_CTRL_DCD)
776			msr |= SER_DCD;
777
778		if (msr != sc->sc_msr) {
779			DPRINTF("status change msr=0x%02x "
780			    "(was 0x%02x)\n", msr, sc->sc_msr);
781			sc->sc_msr = msr;
782			ucom_status_change(&sc->sc_ucom);
783		}
784		break;
785
786	case USB_ST_SETUP:
787		req.bmRequestType = USLCOM_READ;
788		req.bRequest = USLCOM_RCTRL;
789		USETW(req.wValue, 0);
790		USETW(req.wIndex, USLCOM_PORT_NO);
791		USETW(req.wLength, sizeof(buf));
792
793		usbd_xfer_set_frames(xfer, 2);
794		usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
795		usbd_xfer_set_frame_len(xfer, 1, sizeof(buf));
796
797		pc = usbd_xfer_get_frame(xfer, 0);
798		usbd_copy_in(pc, 0, &req, sizeof(req));
799		usbd_transfer_submit(xfer);
800		break;
801
802	default:		/* error */
803		if (error != USB_ERR_CANCELLED)
804			DPRINTF("error=%s\n", usbd_errstr(error));
805		break;
806	}
807}
808
809static void
810uslcom_start_read(struct ucom_softc *ucom)
811{
812	struct uslcom_softc *sc = ucom->sc_parent;
813
814	/* start read endpoint */
815	usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_RD]);
816}
817
818static void
819uslcom_stop_read(struct ucom_softc *ucom)
820{
821	struct uslcom_softc *sc = ucom->sc_parent;
822
823	/* stop read endpoint */
824	usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_RD]);
825}
826
827static void
828uslcom_start_write(struct ucom_softc *ucom)
829{
830	struct uslcom_softc *sc = ucom->sc_parent;
831
832	usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_WR]);
833}
834
835static void
836uslcom_stop_write(struct ucom_softc *ucom)
837{
838	struct uslcom_softc *sc = ucom->sc_parent;
839
840	usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_WR]);
841}
842
843static void
844uslcom_poll(struct ucom_softc *ucom)
845{
846	struct uslcom_softc *sc = ucom->sc_parent;
847	usbd_transfer_poll(sc->sc_xfer, USLCOM_N_TRANSFER);
848}
849