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