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