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