Deleted Added
full compact
uftdi.c (214761) uftdi.c (214843)
1/* $NetBSD: uftdi.c,v 1.13 2002/09/23 05:51:23 simonb Exp $ */
2
3/*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
1/* $NetBSD: uftdi.c,v 1.13 2002/09/23 05:51:23 simonb Exp $ */
2
3/*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/dev/usb/serial/uftdi.c 214761 2010-11-03 21:50:49Z n_hibma $");
33__FBSDID("$FreeBSD: head/sys/dev/usb/serial/uftdi.c 214843 2010-11-05 19:12:48Z n_hibma $");
34
35/*
36 * NOTE: all function names beginning like "uftdi_cfg_" can only
37 * be called from within the config thread function !
38 */
39
40/*
41 * FTDI FT8U100AX serial adapter driver
42 */
43
44#include <sys/stdint.h>
45#include <sys/stddef.h>
46#include <sys/param.h>
47#include <sys/queue.h>
48#include <sys/types.h>
49#include <sys/systm.h>
50#include <sys/kernel.h>
51#include <sys/bus.h>
52#include <sys/linker_set.h>
53#include <sys/module.h>
54#include <sys/lock.h>
55#include <sys/mutex.h>
56#include <sys/condvar.h>
57#include <sys/sysctl.h>
58#include <sys/sx.h>
59#include <sys/unistd.h>
60#include <sys/callout.h>
61#include <sys/malloc.h>
62#include <sys/priv.h>
63
64#include <dev/usb/usb.h>
65#include <dev/usb/usbdi.h>
66#include <dev/usb/usbdi_util.h>
67#include "usbdevs.h"
68
69#define USB_DEBUG_VAR uftdi_debug
70#include <dev/usb/usb_debug.h>
71#include <dev/usb/usb_process.h>
72
73#include <dev/usb/serial/usb_serial.h>
74#include <dev/usb/serial/uftdi_reg.h>
75
76#ifdef USB_DEBUG
77static int uftdi_debug = 0;
78
79SYSCTL_NODE(_hw_usb, OID_AUTO, uftdi, CTLFLAG_RW, 0, "USB uftdi");
80SYSCTL_INT(_hw_usb_uftdi, OID_AUTO, debug, CTLFLAG_RW,
81 &uftdi_debug, 0, "Debug level");
82#endif
83
84#define UFTDI_CONFIG_INDEX 0
85#define UFTDI_IFACE_INDEX 0
86
87#define UFTDI_OBUFSIZE 64 /* bytes, cannot be increased due to
88 * do size encoding */
89
90enum {
91 UFTDI_BULK_DT_WR,
92 UFTDI_BULK_DT_RD,
93 UFTDI_N_TRANSFER,
94};
95
96struct uftdi_softc {
97 struct ucom_super_softc sc_super_ucom;
98 struct ucom_softc sc_ucom;
99
100 struct usb_device *sc_udev;
101 struct usb_xfer *sc_xfer[UFTDI_N_TRANSFER];
102 device_t sc_dev;
103 struct mtx sc_mtx;
104
105 uint32_t sc_unit;
106 enum uftdi_type sc_type;
107
108 uint16_t sc_last_lcr;
109
110 uint8_t sc_iface_index;
111 uint8_t sc_hdrlen;
112 uint8_t sc_msr;
113 uint8_t sc_lsr;
114
115 uint8_t sc_name[16];
116};
117
118struct uftdi_param_config {
119 uint16_t rate;
120 uint16_t lcr;
121 uint8_t v_start;
122 uint8_t v_stop;
123 uint8_t v_flow;
124};
125
126/* prototypes */
127
128static device_probe_t uftdi_probe;
129static device_attach_t uftdi_attach;
130static device_detach_t uftdi_detach;
131
132static usb_callback_t uftdi_write_callback;
133static usb_callback_t uftdi_read_callback;
134
135static void uftdi_cfg_open(struct ucom_softc *);
136static void uftdi_cfg_set_dtr(struct ucom_softc *, uint8_t);
137static void uftdi_cfg_set_rts(struct ucom_softc *, uint8_t);
138static void uftdi_cfg_set_break(struct ucom_softc *, uint8_t);
139static int uftdi_set_parm_soft(struct termios *,
140 struct uftdi_param_config *, uint8_t);
141static int uftdi_pre_param(struct ucom_softc *, struct termios *);
142static void uftdi_cfg_param(struct ucom_softc *, struct termios *);
143static void uftdi_cfg_get_status(struct ucom_softc *, uint8_t *,
144 uint8_t *);
145static void uftdi_start_read(struct ucom_softc *);
146static void uftdi_stop_read(struct ucom_softc *);
147static void uftdi_start_write(struct ucom_softc *);
148static void uftdi_stop_write(struct ucom_softc *);
149static uint8_t uftdi_8u232am_getrate(uint32_t, uint16_t *);
150static void uftdi_poll(struct ucom_softc *ucom);
151
152static const struct usb_config uftdi_config[UFTDI_N_TRANSFER] = {
153
154 [UFTDI_BULK_DT_WR] = {
155 .type = UE_BULK,
156 .endpoint = UE_ADDR_ANY,
157 .direction = UE_DIR_OUT,
158 .bufsize = UFTDI_OBUFSIZE,
159 .flags = {.pipe_bof = 1,},
160 .callback = &uftdi_write_callback,
161 },
162
163 [UFTDI_BULK_DT_RD] = {
164 .type = UE_BULK,
165 .endpoint = UE_ADDR_ANY,
166 .direction = UE_DIR_IN,
167 .bufsize = 0, /* use wMaxPacketSize */
168 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
169 .callback = &uftdi_read_callback,
170 },
171};
172
173static const struct ucom_callback uftdi_callback = {
174 .ucom_cfg_get_status = &uftdi_cfg_get_status,
175 .ucom_cfg_set_dtr = &uftdi_cfg_set_dtr,
176 .ucom_cfg_set_rts = &uftdi_cfg_set_rts,
177 .ucom_cfg_set_break = &uftdi_cfg_set_break,
178 .ucom_cfg_param = &uftdi_cfg_param,
179 .ucom_cfg_open = &uftdi_cfg_open,
180 .ucom_pre_param = &uftdi_pre_param,
181 .ucom_start_read = &uftdi_start_read,
182 .ucom_stop_read = &uftdi_stop_read,
183 .ucom_start_write = &uftdi_start_write,
184 .ucom_stop_write = &uftdi_stop_write,
185 .ucom_poll = &uftdi_poll,
186};
187
188static device_method_t uftdi_methods[] = {
189 /* Device interface */
190 DEVMETHOD(device_probe, uftdi_probe),
191 DEVMETHOD(device_attach, uftdi_attach),
192 DEVMETHOD(device_detach, uftdi_detach),
193
194 {0, 0}
195};
196
197static devclass_t uftdi_devclass;
198
199static driver_t uftdi_driver = {
200 .name = "uftdi",
201 .methods = uftdi_methods,
202 .size = sizeof(struct uftdi_softc),
203};
204
205DRIVER_MODULE(uftdi, uhub, uftdi_driver, uftdi_devclass, NULL, 0);
206MODULE_DEPEND(uftdi, ucom, 1, 1, 1);
207MODULE_DEPEND(uftdi, usb, 1, 1, 1);
208MODULE_VERSION(uftdi, 1);
209
210static struct usb_device_id uftdi_devs[] = {
211#define UFTDI_DEV(v,p,t) \
212 { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, UFTDI_TYPE_##t) }
213 UFTDI_DEV(ATMEL, STK541, 8U232AM),
214 UFTDI_DEV(DRESDENELEKTRONIK, SENSORTERMINALBOARD, 8U232AM),
215 UFTDI_DEV(DRESDENELEKTRONIK, WIRELESSHANDHELDTERMINAL, 8U232AM),
216 UFTDI_DEV(FTDI, GAMMASCOUT, 8U232AM),
217 UFTDI_DEV(FTDI, SERIAL_8U100AX, SIO),
218 UFTDI_DEV(FTDI, SERIAL_2232C, 8U232AM),
219 UFTDI_DEV(FTDI, SERIAL_2232D, 8U232AM),
220 UFTDI_DEV(FTDI, SERIAL_4232H, 8U232AM),
221 UFTDI_DEV(FTDI, SERIAL_8U232AM, 8U232AM),
222 UFTDI_DEV(FTDI, SERIAL_8U232AM4, 8U232AM),
223 UFTDI_DEV(FTDI, SEMC_DSS20, 8U232AM),
224 UFTDI_DEV(FTDI, CFA_631, 8U232AM),
225 UFTDI_DEV(FTDI, CFA_632, 8U232AM),
226 UFTDI_DEV(FTDI, CFA_633, 8U232AM),
227 UFTDI_DEV(FTDI, CFA_634, 8U232AM),
228 UFTDI_DEV(FTDI, CFA_635, 8U232AM),
229 UFTDI_DEV(FTDI, USB_UIRT, 8U232AM),
230 UFTDI_DEV(FTDI, USBSERIAL, 8U232AM),
231 UFTDI_DEV(FTDI, KBS, 8U232AM),
232 UFTDI_DEV(FTDI, MX2_3, 8U232AM),
233 UFTDI_DEV(FTDI, MX4_5, 8U232AM),
234 UFTDI_DEV(FTDI, LK202, 8U232AM),
235 UFTDI_DEV(FTDI, LK204, 8U232AM),
236 UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13M, 8U232AM),
237 UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13S, 8U232AM),
238 UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13U, 8U232AM),
239 UFTDI_DEV(FTDI, EISCOU, 8U232AM),
240 UFTDI_DEV(FTDI, UOPTBR, 8U232AM),
241 UFTDI_DEV(FTDI, EMCU2D, 8U232AM),
242 UFTDI_DEV(FTDI, PCMSFU, 8U232AM),
243 UFTDI_DEV(FTDI, EMCU2H, 8U232AM),
244 UFTDI_DEV(FTDI, MAXSTREAM, 8U232AM),
245 UFTDI_DEV(FTDI, CTI_USB_NANO_485, 8U232AM),
246 UFTDI_DEV(FTDI, CTI_USB_MINI_485, 8U232AM),
247 UFTDI_DEV(SIIG2, US2308, 8U232AM),
248 UFTDI_DEV(INTREPIDCS, VALUECAN, 8U232AM),
249 UFTDI_DEV(INTREPIDCS, NEOVI, 8U232AM),
250 UFTDI_DEV(BBELECTRONICS, USOTL4, 8U232AM),
251 UFTDI_DEV(MATRIXORBITAL, MOUA, 8U232AM),
252 UFTDI_DEV(MARVELL, SHEEVAPLUG, 8U232AM),
253 UFTDI_DEV(MELCO, PCOPRS1, 8U232AM),
254 UFTDI_DEV(RATOC, REXUSB60F, 8U232AM),
255#undef UFTDI_DEV
256};
257
258static int
259uftdi_probe(device_t dev)
260{
261 struct usb_attach_arg *uaa = device_get_ivars(dev);
262
263 if (uaa->usb_mode != USB_MODE_HOST) {
264 return (ENXIO);
265 }
266 if (uaa->info.bConfigIndex != UFTDI_CONFIG_INDEX) {
267 return (ENXIO);
268 }
269 /* attach to all present interfaces */
270
271 return (usbd_lookup_id_by_uaa(uftdi_devs, sizeof(uftdi_devs), uaa));
272}
273
274static int
275uftdi_attach(device_t dev)
276{
277 struct usb_attach_arg *uaa = device_get_ivars(dev);
278 struct uftdi_softc *sc = device_get_softc(dev);
279 int error;
280
281 sc->sc_udev = uaa->device;
282 sc->sc_dev = dev;
283 sc->sc_unit = device_get_unit(dev);
284
285 device_set_usb_desc(dev);
286 mtx_init(&sc->sc_mtx, "uftdi", NULL, MTX_DEF);
287
288 snprintf(sc->sc_name, sizeof(sc->sc_name),
289 "%s", device_get_nameunit(dev));
290
291 DPRINTF("\n");
292
293 sc->sc_iface_index = uaa->info.bIfaceIndex;
294 sc->sc_type = USB_GET_DRIVER_INFO(uaa);
295
296 switch (sc->sc_type) {
297 case UFTDI_TYPE_SIO:
298 sc->sc_hdrlen = 1;
299 break;
300 case UFTDI_TYPE_8U232AM:
301 default:
302 sc->sc_hdrlen = 0;
303 break;
304 }
305
306 error = usbd_transfer_setup(uaa->device,
307 &sc->sc_iface_index, sc->sc_xfer, uftdi_config,
308 UFTDI_N_TRANSFER, sc, &sc->sc_mtx);
309
310 if (error) {
311 device_printf(dev, "allocating USB "
312 "transfers failed\n");
313 goto detach;
314 }
315 sc->sc_ucom.sc_portno = FTDI_PIT_SIOA + uaa->info.bIfaceNum;
316
317 /* clear stall at first run */
318 mtx_lock(&sc->sc_mtx);
319 usbd_xfer_set_stall(sc->sc_xfer[UFTDI_BULK_DT_WR]);
320 usbd_xfer_set_stall(sc->sc_xfer[UFTDI_BULK_DT_RD]);
321 mtx_unlock(&sc->sc_mtx);
322
323 /* set a valid "lcr" value */
324
325 sc->sc_last_lcr =
326 (FTDI_SIO_SET_DATA_STOP_BITS_2 |
327 FTDI_SIO_SET_DATA_PARITY_NONE |
328 FTDI_SIO_SET_DATA_BITS(8));
329
330 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
331 &uftdi_callback, &sc->sc_mtx);
332 if (error) {
333 goto detach;
334 }
34
35/*
36 * NOTE: all function names beginning like "uftdi_cfg_" can only
37 * be called from within the config thread function !
38 */
39
40/*
41 * FTDI FT8U100AX serial adapter driver
42 */
43
44#include <sys/stdint.h>
45#include <sys/stddef.h>
46#include <sys/param.h>
47#include <sys/queue.h>
48#include <sys/types.h>
49#include <sys/systm.h>
50#include <sys/kernel.h>
51#include <sys/bus.h>
52#include <sys/linker_set.h>
53#include <sys/module.h>
54#include <sys/lock.h>
55#include <sys/mutex.h>
56#include <sys/condvar.h>
57#include <sys/sysctl.h>
58#include <sys/sx.h>
59#include <sys/unistd.h>
60#include <sys/callout.h>
61#include <sys/malloc.h>
62#include <sys/priv.h>
63
64#include <dev/usb/usb.h>
65#include <dev/usb/usbdi.h>
66#include <dev/usb/usbdi_util.h>
67#include "usbdevs.h"
68
69#define USB_DEBUG_VAR uftdi_debug
70#include <dev/usb/usb_debug.h>
71#include <dev/usb/usb_process.h>
72
73#include <dev/usb/serial/usb_serial.h>
74#include <dev/usb/serial/uftdi_reg.h>
75
76#ifdef USB_DEBUG
77static int uftdi_debug = 0;
78
79SYSCTL_NODE(_hw_usb, OID_AUTO, uftdi, CTLFLAG_RW, 0, "USB uftdi");
80SYSCTL_INT(_hw_usb_uftdi, OID_AUTO, debug, CTLFLAG_RW,
81 &uftdi_debug, 0, "Debug level");
82#endif
83
84#define UFTDI_CONFIG_INDEX 0
85#define UFTDI_IFACE_INDEX 0
86
87#define UFTDI_OBUFSIZE 64 /* bytes, cannot be increased due to
88 * do size encoding */
89
90enum {
91 UFTDI_BULK_DT_WR,
92 UFTDI_BULK_DT_RD,
93 UFTDI_N_TRANSFER,
94};
95
96struct uftdi_softc {
97 struct ucom_super_softc sc_super_ucom;
98 struct ucom_softc sc_ucom;
99
100 struct usb_device *sc_udev;
101 struct usb_xfer *sc_xfer[UFTDI_N_TRANSFER];
102 device_t sc_dev;
103 struct mtx sc_mtx;
104
105 uint32_t sc_unit;
106 enum uftdi_type sc_type;
107
108 uint16_t sc_last_lcr;
109
110 uint8_t sc_iface_index;
111 uint8_t sc_hdrlen;
112 uint8_t sc_msr;
113 uint8_t sc_lsr;
114
115 uint8_t sc_name[16];
116};
117
118struct uftdi_param_config {
119 uint16_t rate;
120 uint16_t lcr;
121 uint8_t v_start;
122 uint8_t v_stop;
123 uint8_t v_flow;
124};
125
126/* prototypes */
127
128static device_probe_t uftdi_probe;
129static device_attach_t uftdi_attach;
130static device_detach_t uftdi_detach;
131
132static usb_callback_t uftdi_write_callback;
133static usb_callback_t uftdi_read_callback;
134
135static void uftdi_cfg_open(struct ucom_softc *);
136static void uftdi_cfg_set_dtr(struct ucom_softc *, uint8_t);
137static void uftdi_cfg_set_rts(struct ucom_softc *, uint8_t);
138static void uftdi_cfg_set_break(struct ucom_softc *, uint8_t);
139static int uftdi_set_parm_soft(struct termios *,
140 struct uftdi_param_config *, uint8_t);
141static int uftdi_pre_param(struct ucom_softc *, struct termios *);
142static void uftdi_cfg_param(struct ucom_softc *, struct termios *);
143static void uftdi_cfg_get_status(struct ucom_softc *, uint8_t *,
144 uint8_t *);
145static void uftdi_start_read(struct ucom_softc *);
146static void uftdi_stop_read(struct ucom_softc *);
147static void uftdi_start_write(struct ucom_softc *);
148static void uftdi_stop_write(struct ucom_softc *);
149static uint8_t uftdi_8u232am_getrate(uint32_t, uint16_t *);
150static void uftdi_poll(struct ucom_softc *ucom);
151
152static const struct usb_config uftdi_config[UFTDI_N_TRANSFER] = {
153
154 [UFTDI_BULK_DT_WR] = {
155 .type = UE_BULK,
156 .endpoint = UE_ADDR_ANY,
157 .direction = UE_DIR_OUT,
158 .bufsize = UFTDI_OBUFSIZE,
159 .flags = {.pipe_bof = 1,},
160 .callback = &uftdi_write_callback,
161 },
162
163 [UFTDI_BULK_DT_RD] = {
164 .type = UE_BULK,
165 .endpoint = UE_ADDR_ANY,
166 .direction = UE_DIR_IN,
167 .bufsize = 0, /* use wMaxPacketSize */
168 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
169 .callback = &uftdi_read_callback,
170 },
171};
172
173static const struct ucom_callback uftdi_callback = {
174 .ucom_cfg_get_status = &uftdi_cfg_get_status,
175 .ucom_cfg_set_dtr = &uftdi_cfg_set_dtr,
176 .ucom_cfg_set_rts = &uftdi_cfg_set_rts,
177 .ucom_cfg_set_break = &uftdi_cfg_set_break,
178 .ucom_cfg_param = &uftdi_cfg_param,
179 .ucom_cfg_open = &uftdi_cfg_open,
180 .ucom_pre_param = &uftdi_pre_param,
181 .ucom_start_read = &uftdi_start_read,
182 .ucom_stop_read = &uftdi_stop_read,
183 .ucom_start_write = &uftdi_start_write,
184 .ucom_stop_write = &uftdi_stop_write,
185 .ucom_poll = &uftdi_poll,
186};
187
188static device_method_t uftdi_methods[] = {
189 /* Device interface */
190 DEVMETHOD(device_probe, uftdi_probe),
191 DEVMETHOD(device_attach, uftdi_attach),
192 DEVMETHOD(device_detach, uftdi_detach),
193
194 {0, 0}
195};
196
197static devclass_t uftdi_devclass;
198
199static driver_t uftdi_driver = {
200 .name = "uftdi",
201 .methods = uftdi_methods,
202 .size = sizeof(struct uftdi_softc),
203};
204
205DRIVER_MODULE(uftdi, uhub, uftdi_driver, uftdi_devclass, NULL, 0);
206MODULE_DEPEND(uftdi, ucom, 1, 1, 1);
207MODULE_DEPEND(uftdi, usb, 1, 1, 1);
208MODULE_VERSION(uftdi, 1);
209
210static struct usb_device_id uftdi_devs[] = {
211#define UFTDI_DEV(v,p,t) \
212 { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, UFTDI_TYPE_##t) }
213 UFTDI_DEV(ATMEL, STK541, 8U232AM),
214 UFTDI_DEV(DRESDENELEKTRONIK, SENSORTERMINALBOARD, 8U232AM),
215 UFTDI_DEV(DRESDENELEKTRONIK, WIRELESSHANDHELDTERMINAL, 8U232AM),
216 UFTDI_DEV(FTDI, GAMMASCOUT, 8U232AM),
217 UFTDI_DEV(FTDI, SERIAL_8U100AX, SIO),
218 UFTDI_DEV(FTDI, SERIAL_2232C, 8U232AM),
219 UFTDI_DEV(FTDI, SERIAL_2232D, 8U232AM),
220 UFTDI_DEV(FTDI, SERIAL_4232H, 8U232AM),
221 UFTDI_DEV(FTDI, SERIAL_8U232AM, 8U232AM),
222 UFTDI_DEV(FTDI, SERIAL_8U232AM4, 8U232AM),
223 UFTDI_DEV(FTDI, SEMC_DSS20, 8U232AM),
224 UFTDI_DEV(FTDI, CFA_631, 8U232AM),
225 UFTDI_DEV(FTDI, CFA_632, 8U232AM),
226 UFTDI_DEV(FTDI, CFA_633, 8U232AM),
227 UFTDI_DEV(FTDI, CFA_634, 8U232AM),
228 UFTDI_DEV(FTDI, CFA_635, 8U232AM),
229 UFTDI_DEV(FTDI, USB_UIRT, 8U232AM),
230 UFTDI_DEV(FTDI, USBSERIAL, 8U232AM),
231 UFTDI_DEV(FTDI, KBS, 8U232AM),
232 UFTDI_DEV(FTDI, MX2_3, 8U232AM),
233 UFTDI_DEV(FTDI, MX4_5, 8U232AM),
234 UFTDI_DEV(FTDI, LK202, 8U232AM),
235 UFTDI_DEV(FTDI, LK204, 8U232AM),
236 UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13M, 8U232AM),
237 UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13S, 8U232AM),
238 UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13U, 8U232AM),
239 UFTDI_DEV(FTDI, EISCOU, 8U232AM),
240 UFTDI_DEV(FTDI, UOPTBR, 8U232AM),
241 UFTDI_DEV(FTDI, EMCU2D, 8U232AM),
242 UFTDI_DEV(FTDI, PCMSFU, 8U232AM),
243 UFTDI_DEV(FTDI, EMCU2H, 8U232AM),
244 UFTDI_DEV(FTDI, MAXSTREAM, 8U232AM),
245 UFTDI_DEV(FTDI, CTI_USB_NANO_485, 8U232AM),
246 UFTDI_DEV(FTDI, CTI_USB_MINI_485, 8U232AM),
247 UFTDI_DEV(SIIG2, US2308, 8U232AM),
248 UFTDI_DEV(INTREPIDCS, VALUECAN, 8U232AM),
249 UFTDI_DEV(INTREPIDCS, NEOVI, 8U232AM),
250 UFTDI_DEV(BBELECTRONICS, USOTL4, 8U232AM),
251 UFTDI_DEV(MATRIXORBITAL, MOUA, 8U232AM),
252 UFTDI_DEV(MARVELL, SHEEVAPLUG, 8U232AM),
253 UFTDI_DEV(MELCO, PCOPRS1, 8U232AM),
254 UFTDI_DEV(RATOC, REXUSB60F, 8U232AM),
255#undef UFTDI_DEV
256};
257
258static int
259uftdi_probe(device_t dev)
260{
261 struct usb_attach_arg *uaa = device_get_ivars(dev);
262
263 if (uaa->usb_mode != USB_MODE_HOST) {
264 return (ENXIO);
265 }
266 if (uaa->info.bConfigIndex != UFTDI_CONFIG_INDEX) {
267 return (ENXIO);
268 }
269 /* attach to all present interfaces */
270
271 return (usbd_lookup_id_by_uaa(uftdi_devs, sizeof(uftdi_devs), uaa));
272}
273
274static int
275uftdi_attach(device_t dev)
276{
277 struct usb_attach_arg *uaa = device_get_ivars(dev);
278 struct uftdi_softc *sc = device_get_softc(dev);
279 int error;
280
281 sc->sc_udev = uaa->device;
282 sc->sc_dev = dev;
283 sc->sc_unit = device_get_unit(dev);
284
285 device_set_usb_desc(dev);
286 mtx_init(&sc->sc_mtx, "uftdi", NULL, MTX_DEF);
287
288 snprintf(sc->sc_name, sizeof(sc->sc_name),
289 "%s", device_get_nameunit(dev));
290
291 DPRINTF("\n");
292
293 sc->sc_iface_index = uaa->info.bIfaceIndex;
294 sc->sc_type = USB_GET_DRIVER_INFO(uaa);
295
296 switch (sc->sc_type) {
297 case UFTDI_TYPE_SIO:
298 sc->sc_hdrlen = 1;
299 break;
300 case UFTDI_TYPE_8U232AM:
301 default:
302 sc->sc_hdrlen = 0;
303 break;
304 }
305
306 error = usbd_transfer_setup(uaa->device,
307 &sc->sc_iface_index, sc->sc_xfer, uftdi_config,
308 UFTDI_N_TRANSFER, sc, &sc->sc_mtx);
309
310 if (error) {
311 device_printf(dev, "allocating USB "
312 "transfers failed\n");
313 goto detach;
314 }
315 sc->sc_ucom.sc_portno = FTDI_PIT_SIOA + uaa->info.bIfaceNum;
316
317 /* clear stall at first run */
318 mtx_lock(&sc->sc_mtx);
319 usbd_xfer_set_stall(sc->sc_xfer[UFTDI_BULK_DT_WR]);
320 usbd_xfer_set_stall(sc->sc_xfer[UFTDI_BULK_DT_RD]);
321 mtx_unlock(&sc->sc_mtx);
322
323 /* set a valid "lcr" value */
324
325 sc->sc_last_lcr =
326 (FTDI_SIO_SET_DATA_STOP_BITS_2 |
327 FTDI_SIO_SET_DATA_PARITY_NONE |
328 FTDI_SIO_SET_DATA_BITS(8));
329
330 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
331 &uftdi_callback, &sc->sc_mtx);
332 if (error) {
333 goto detach;
334 }
335 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
336
335 return (0); /* success */
336
337detach:
338 uftdi_detach(dev);
339 return (ENXIO);
340}
341
342static int
343uftdi_detach(device_t dev)
344{
345 struct uftdi_softc *sc = device_get_softc(dev);
346
347 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
348 usbd_transfer_unsetup(sc->sc_xfer, UFTDI_N_TRANSFER);
349 mtx_destroy(&sc->sc_mtx);
350
351 return (0);
352}
353
354static void
355uftdi_cfg_open(struct ucom_softc *ucom)
356{
357 struct uftdi_softc *sc = ucom->sc_parent;
358 uint16_t wIndex = ucom->sc_portno;
359 struct usb_device_request req;
360
361 DPRINTF("");
362
363 /* perform a full reset on the device */
364
365 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
366 req.bRequest = FTDI_SIO_RESET;
367 USETW(req.wValue, FTDI_SIO_RESET_SIO);
368 USETW(req.wIndex, wIndex);
369 USETW(req.wLength, 0);
370 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
371 &req, NULL, 0, 1000);
372
373 /* turn on RTS/CTS flow control */
374
375 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
376 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
377 USETW(req.wValue, 0);
378 USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, wIndex);
379 USETW(req.wLength, 0);
380 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
381 &req, NULL, 0, 1000);
382
383 /*
384 * NOTE: with the new UCOM layer there will always be a
385 * "uftdi_cfg_param()" call after "open()", so there is no need for
386 * "open()" to configure anything
387 */
388}
389
390static void
391uftdi_write_callback(struct usb_xfer *xfer, usb_error_t error)
392{
393 struct uftdi_softc *sc = usbd_xfer_softc(xfer);
394 struct usb_page_cache *pc;
395 uint32_t actlen;
396 uint8_t buf[1];
397
398 switch (USB_GET_STATE(xfer)) {
399 case USB_ST_SETUP:
400 case USB_ST_TRANSFERRED:
401tr_setup:
402 pc = usbd_xfer_get_frame(xfer, 0);
403 if (ucom_get_data(&sc->sc_ucom, pc,
404 sc->sc_hdrlen, UFTDI_OBUFSIZE - sc->sc_hdrlen,
405 &actlen)) {
406
407 if (sc->sc_hdrlen > 0) {
408 buf[0] =
409 FTDI_OUT_TAG(actlen, sc->sc_ucom.sc_portno);
410 usbd_copy_in(pc, 0, buf, 1);
411 }
412 usbd_xfer_set_frame_len(xfer, 0, actlen + sc->sc_hdrlen);
413 usbd_transfer_submit(xfer);
414 }
415 return;
416
417 default: /* Error */
418 if (error != USB_ERR_CANCELLED) {
419 /* try to clear stall first */
420 usbd_xfer_set_stall(xfer);
421 goto tr_setup;
422 }
423 return;
424 }
425}
426
427static void
428uftdi_read_callback(struct usb_xfer *xfer, usb_error_t error)
429{
430 struct uftdi_softc *sc = usbd_xfer_softc(xfer);
431 struct usb_page_cache *pc;
432 uint8_t buf[2];
433 uint8_t ftdi_msr;
434 uint8_t msr;
435 uint8_t lsr;
436 int actlen;
437
438 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
439
440 switch (USB_GET_STATE(xfer)) {
441 case USB_ST_TRANSFERRED:
442
443 if (actlen < 2) {
444 goto tr_setup;
445 }
446 pc = usbd_xfer_get_frame(xfer, 0);
447 usbd_copy_out(pc, 0, buf, 2);
448
449 ftdi_msr = FTDI_GET_MSR(buf);
450 lsr = FTDI_GET_LSR(buf);
451
452 msr = 0;
453 if (ftdi_msr & FTDI_SIO_CTS_MASK)
454 msr |= SER_CTS;
455 if (ftdi_msr & FTDI_SIO_DSR_MASK)
456 msr |= SER_DSR;
457 if (ftdi_msr & FTDI_SIO_RI_MASK)
458 msr |= SER_RI;
459 if (ftdi_msr & FTDI_SIO_RLSD_MASK)
460 msr |= SER_DCD;
461
462 if ((sc->sc_msr != msr) ||
463 ((sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK))) {
464 DPRINTF("status change msr=0x%02x (0x%02x) "
465 "lsr=0x%02x (0x%02x)\n", msr, sc->sc_msr,
466 lsr, sc->sc_lsr);
467
468 sc->sc_msr = msr;
469 sc->sc_lsr = lsr;
470
471 ucom_status_change(&sc->sc_ucom);
472 }
473 actlen -= 2;
474
475 if (actlen > 0) {
476 ucom_put_data(&sc->sc_ucom, pc, 2, actlen);
477 }
478 case USB_ST_SETUP:
479tr_setup:
480 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
481 usbd_transfer_submit(xfer);
482 return;
483
484 default: /* Error */
485 if (error != USB_ERR_CANCELLED) {
486 /* try to clear stall first */
487 usbd_xfer_set_stall(xfer);
488 goto tr_setup;
489 }
490 return;
491 }
492}
493
494static void
495uftdi_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
496{
497 struct uftdi_softc *sc = ucom->sc_parent;
498 uint16_t wIndex = ucom->sc_portno;
499 uint16_t wValue;
500 struct usb_device_request req;
501
502 wValue = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
503
504 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
505 req.bRequest = FTDI_SIO_MODEM_CTRL;
506 USETW(req.wValue, wValue);
507 USETW(req.wIndex, wIndex);
508 USETW(req.wLength, 0);
509 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
510 &req, NULL, 0, 1000);
511}
512
513static void
514uftdi_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
515{
516 struct uftdi_softc *sc = ucom->sc_parent;
517 uint16_t wIndex = ucom->sc_portno;
518 uint16_t wValue;
519 struct usb_device_request req;
520
521 wValue = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
522
523 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
524 req.bRequest = FTDI_SIO_MODEM_CTRL;
525 USETW(req.wValue, wValue);
526 USETW(req.wIndex, wIndex);
527 USETW(req.wLength, 0);
528 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
529 &req, NULL, 0, 1000);
530}
531
532static void
533uftdi_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
534{
535 struct uftdi_softc *sc = ucom->sc_parent;
536 uint16_t wIndex = ucom->sc_portno;
537 uint16_t wValue;
538 struct usb_device_request req;
539
540 if (onoff) {
541 sc->sc_last_lcr |= FTDI_SIO_SET_BREAK;
542 } else {
543 sc->sc_last_lcr &= ~FTDI_SIO_SET_BREAK;
544 }
545
546 wValue = sc->sc_last_lcr;
547
548 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
549 req.bRequest = FTDI_SIO_SET_DATA;
550 USETW(req.wValue, wValue);
551 USETW(req.wIndex, wIndex);
552 USETW(req.wLength, 0);
553 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
554 &req, NULL, 0, 1000);
555}
556
557static int
558uftdi_set_parm_soft(struct termios *t,
559 struct uftdi_param_config *cfg, uint8_t type)
560{
561 bzero(cfg, sizeof(*cfg));
562
563 switch (type) {
564 case UFTDI_TYPE_SIO:
565 switch (t->c_ospeed) {
566 case 300:
567 cfg->rate = ftdi_sio_b300;
568 break;
569 case 600:
570 cfg->rate = ftdi_sio_b600;
571 break;
572 case 1200:
573 cfg->rate = ftdi_sio_b1200;
574 break;
575 case 2400:
576 cfg->rate = ftdi_sio_b2400;
577 break;
578 case 4800:
579 cfg->rate = ftdi_sio_b4800;
580 break;
581 case 9600:
582 cfg->rate = ftdi_sio_b9600;
583 break;
584 case 19200:
585 cfg->rate = ftdi_sio_b19200;
586 break;
587 case 38400:
588 cfg->rate = ftdi_sio_b38400;
589 break;
590 case 57600:
591 cfg->rate = ftdi_sio_b57600;
592 break;
593 case 115200:
594 cfg->rate = ftdi_sio_b115200;
595 break;
596 default:
597 return (EINVAL);
598 }
599 break;
600
601 case UFTDI_TYPE_8U232AM:
602 if (uftdi_8u232am_getrate(t->c_ospeed, &cfg->rate)) {
603 return (EINVAL);
604 }
605 break;
606 }
607
608 if (t->c_cflag & CSTOPB)
609 cfg->lcr = FTDI_SIO_SET_DATA_STOP_BITS_2;
610 else
611 cfg->lcr = FTDI_SIO_SET_DATA_STOP_BITS_1;
612
613 if (t->c_cflag & PARENB) {
614 if (t->c_cflag & PARODD) {
615 cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_ODD;
616 } else {
617 cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_EVEN;
618 }
619 } else {
620 cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_NONE;
621 }
622
623 switch (t->c_cflag & CSIZE) {
624 case CS5:
625 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(5);
626 break;
627
628 case CS6:
629 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(6);
630 break;
631
632 case CS7:
633 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(7);
634 break;
635
636 case CS8:
637 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(8);
638 break;
639 }
640
641 if (t->c_cflag & CRTSCTS) {
642 cfg->v_flow = FTDI_SIO_RTS_CTS_HS;
643 } else if (t->c_iflag & (IXON | IXOFF)) {
644 cfg->v_flow = FTDI_SIO_XON_XOFF_HS;
645 cfg->v_start = t->c_cc[VSTART];
646 cfg->v_stop = t->c_cc[VSTOP];
647 } else {
648 cfg->v_flow = FTDI_SIO_DISABLE_FLOW_CTRL;
649 }
650
651 return (0);
652}
653
654static int
655uftdi_pre_param(struct ucom_softc *ucom, struct termios *t)
656{
657 struct uftdi_softc *sc = ucom->sc_parent;
658 struct uftdi_param_config cfg;
659
660 DPRINTF("\n");
661
662 return (uftdi_set_parm_soft(t, &cfg, sc->sc_type));
663}
664
665static void
666uftdi_cfg_param(struct ucom_softc *ucom, struct termios *t)
667{
668 struct uftdi_softc *sc = ucom->sc_parent;
669 uint16_t wIndex = ucom->sc_portno;
670 struct uftdi_param_config cfg;
671 struct usb_device_request req;
672
673 if (uftdi_set_parm_soft(t, &cfg, sc->sc_type)) {
674 /* should not happen */
675 return;
676 }
677 sc->sc_last_lcr = cfg.lcr;
678
679 DPRINTF("\n");
680
681 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
682 req.bRequest = FTDI_SIO_SET_BAUD_RATE;
683 USETW(req.wValue, cfg.rate);
684 USETW(req.wIndex, wIndex);
685 USETW(req.wLength, 0);
686 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
687 &req, NULL, 0, 1000);
688
689 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
690 req.bRequest = FTDI_SIO_SET_DATA;
691 USETW(req.wValue, cfg.lcr);
692 USETW(req.wIndex, wIndex);
693 USETW(req.wLength, 0);
694 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
695 &req, NULL, 0, 1000);
696
697 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
698 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
699 USETW2(req.wValue, cfg.v_stop, cfg.v_start);
700 USETW2(req.wIndex, cfg.v_flow, wIndex);
701 USETW(req.wLength, 0);
702 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
703 &req, NULL, 0, 1000);
704}
705
706static void
707uftdi_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
708{
709 struct uftdi_softc *sc = ucom->sc_parent;
710
711 DPRINTF("msr=0x%02x lsr=0x%02x\n",
712 sc->sc_msr, sc->sc_lsr);
713
714 *msr = sc->sc_msr;
715 *lsr = sc->sc_lsr;
716}
717
718static void
719uftdi_start_read(struct ucom_softc *ucom)
720{
721 struct uftdi_softc *sc = ucom->sc_parent;
722
723 usbd_transfer_start(sc->sc_xfer[UFTDI_BULK_DT_RD]);
724}
725
726static void
727uftdi_stop_read(struct ucom_softc *ucom)
728{
729 struct uftdi_softc *sc = ucom->sc_parent;
730
731 usbd_transfer_stop(sc->sc_xfer[UFTDI_BULK_DT_RD]);
732}
733
734static void
735uftdi_start_write(struct ucom_softc *ucom)
736{
737 struct uftdi_softc *sc = ucom->sc_parent;
738
739 usbd_transfer_start(sc->sc_xfer[UFTDI_BULK_DT_WR]);
740}
741
742static void
743uftdi_stop_write(struct ucom_softc *ucom)
744{
745 struct uftdi_softc *sc = ucom->sc_parent;
746
747 usbd_transfer_stop(sc->sc_xfer[UFTDI_BULK_DT_WR]);
748}
749
750/*------------------------------------------------------------------------*
751 * uftdi_8u232am_getrate
752 *
753 * Return values:
754 * 0: Success
755 * Else: Failure
756 *------------------------------------------------------------------------*/
757static uint8_t
758uftdi_8u232am_getrate(uint32_t speed, uint16_t *rate)
759{
760 /* Table of the nearest even powers-of-2 for values 0..15. */
761 static const uint8_t roundoff[16] = {
762 0, 2, 2, 4, 4, 4, 8, 8,
763 8, 8, 8, 8, 16, 16, 16, 16,
764 };
765 uint32_t d;
766 uint32_t freq;
767 uint16_t result;
768
769 if ((speed < 178) || (speed > ((3000000 * 100) / 97)))
770 return (1); /* prevent numerical overflow */
771
772 /* Special cases for 2M and 3M. */
773 if ((speed >= ((3000000 * 100) / 103)) &&
774 (speed <= ((3000000 * 100) / 97))) {
775 result = 0;
776 goto done;
777 }
778 if ((speed >= ((2000000 * 100) / 103)) &&
779 (speed <= ((2000000 * 100) / 97))) {
780 result = 1;
781 goto done;
782 }
783 d = (FTDI_8U232AM_FREQ << 4) / speed;
784 d = (d & ~15) + roundoff[d & 15];
785
786 if (d < FTDI_8U232AM_MIN_DIV)
787 d = FTDI_8U232AM_MIN_DIV;
788 else if (d > FTDI_8U232AM_MAX_DIV)
789 d = FTDI_8U232AM_MAX_DIV;
790
791 /*
792 * Calculate the frequency needed for "d" to exactly divide down to
793 * our target "speed", and check that the actual frequency is within
794 * 3% of this.
795 */
796 freq = (speed * d);
797 if ((freq < ((FTDI_8U232AM_FREQ * 1600ULL) / 103)) ||
798 (freq > ((FTDI_8U232AM_FREQ * 1600ULL) / 97)))
799 return (1);
800
801 /*
802 * Pack the divisor into the resultant value. The lower 14-bits
803 * hold the integral part, while the upper 2 bits encode the
804 * fractional component: either 0, 0.5, 0.25, or 0.125.
805 */
806 result = (d >> 4);
807 if (d & 8)
808 result |= 0x4000;
809 else if (d & 4)
810 result |= 0x8000;
811 else if (d & 2)
812 result |= 0xc000;
813
814done:
815 *rate = result;
816 return (0);
817}
818
819static void
820uftdi_poll(struct ucom_softc *ucom)
821{
822 struct uftdi_softc *sc = ucom->sc_parent;
823 usbd_transfer_poll(sc->sc_xfer, UFTDI_N_TRANSFER);
824}
337 return (0); /* success */
338
339detach:
340 uftdi_detach(dev);
341 return (ENXIO);
342}
343
344static int
345uftdi_detach(device_t dev)
346{
347 struct uftdi_softc *sc = device_get_softc(dev);
348
349 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
350 usbd_transfer_unsetup(sc->sc_xfer, UFTDI_N_TRANSFER);
351 mtx_destroy(&sc->sc_mtx);
352
353 return (0);
354}
355
356static void
357uftdi_cfg_open(struct ucom_softc *ucom)
358{
359 struct uftdi_softc *sc = ucom->sc_parent;
360 uint16_t wIndex = ucom->sc_portno;
361 struct usb_device_request req;
362
363 DPRINTF("");
364
365 /* perform a full reset on the device */
366
367 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
368 req.bRequest = FTDI_SIO_RESET;
369 USETW(req.wValue, FTDI_SIO_RESET_SIO);
370 USETW(req.wIndex, wIndex);
371 USETW(req.wLength, 0);
372 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
373 &req, NULL, 0, 1000);
374
375 /* turn on RTS/CTS flow control */
376
377 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
378 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
379 USETW(req.wValue, 0);
380 USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, wIndex);
381 USETW(req.wLength, 0);
382 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
383 &req, NULL, 0, 1000);
384
385 /*
386 * NOTE: with the new UCOM layer there will always be a
387 * "uftdi_cfg_param()" call after "open()", so there is no need for
388 * "open()" to configure anything
389 */
390}
391
392static void
393uftdi_write_callback(struct usb_xfer *xfer, usb_error_t error)
394{
395 struct uftdi_softc *sc = usbd_xfer_softc(xfer);
396 struct usb_page_cache *pc;
397 uint32_t actlen;
398 uint8_t buf[1];
399
400 switch (USB_GET_STATE(xfer)) {
401 case USB_ST_SETUP:
402 case USB_ST_TRANSFERRED:
403tr_setup:
404 pc = usbd_xfer_get_frame(xfer, 0);
405 if (ucom_get_data(&sc->sc_ucom, pc,
406 sc->sc_hdrlen, UFTDI_OBUFSIZE - sc->sc_hdrlen,
407 &actlen)) {
408
409 if (sc->sc_hdrlen > 0) {
410 buf[0] =
411 FTDI_OUT_TAG(actlen, sc->sc_ucom.sc_portno);
412 usbd_copy_in(pc, 0, buf, 1);
413 }
414 usbd_xfer_set_frame_len(xfer, 0, actlen + sc->sc_hdrlen);
415 usbd_transfer_submit(xfer);
416 }
417 return;
418
419 default: /* Error */
420 if (error != USB_ERR_CANCELLED) {
421 /* try to clear stall first */
422 usbd_xfer_set_stall(xfer);
423 goto tr_setup;
424 }
425 return;
426 }
427}
428
429static void
430uftdi_read_callback(struct usb_xfer *xfer, usb_error_t error)
431{
432 struct uftdi_softc *sc = usbd_xfer_softc(xfer);
433 struct usb_page_cache *pc;
434 uint8_t buf[2];
435 uint8_t ftdi_msr;
436 uint8_t msr;
437 uint8_t lsr;
438 int actlen;
439
440 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
441
442 switch (USB_GET_STATE(xfer)) {
443 case USB_ST_TRANSFERRED:
444
445 if (actlen < 2) {
446 goto tr_setup;
447 }
448 pc = usbd_xfer_get_frame(xfer, 0);
449 usbd_copy_out(pc, 0, buf, 2);
450
451 ftdi_msr = FTDI_GET_MSR(buf);
452 lsr = FTDI_GET_LSR(buf);
453
454 msr = 0;
455 if (ftdi_msr & FTDI_SIO_CTS_MASK)
456 msr |= SER_CTS;
457 if (ftdi_msr & FTDI_SIO_DSR_MASK)
458 msr |= SER_DSR;
459 if (ftdi_msr & FTDI_SIO_RI_MASK)
460 msr |= SER_RI;
461 if (ftdi_msr & FTDI_SIO_RLSD_MASK)
462 msr |= SER_DCD;
463
464 if ((sc->sc_msr != msr) ||
465 ((sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK))) {
466 DPRINTF("status change msr=0x%02x (0x%02x) "
467 "lsr=0x%02x (0x%02x)\n", msr, sc->sc_msr,
468 lsr, sc->sc_lsr);
469
470 sc->sc_msr = msr;
471 sc->sc_lsr = lsr;
472
473 ucom_status_change(&sc->sc_ucom);
474 }
475 actlen -= 2;
476
477 if (actlen > 0) {
478 ucom_put_data(&sc->sc_ucom, pc, 2, actlen);
479 }
480 case USB_ST_SETUP:
481tr_setup:
482 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
483 usbd_transfer_submit(xfer);
484 return;
485
486 default: /* Error */
487 if (error != USB_ERR_CANCELLED) {
488 /* try to clear stall first */
489 usbd_xfer_set_stall(xfer);
490 goto tr_setup;
491 }
492 return;
493 }
494}
495
496static void
497uftdi_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
498{
499 struct uftdi_softc *sc = ucom->sc_parent;
500 uint16_t wIndex = ucom->sc_portno;
501 uint16_t wValue;
502 struct usb_device_request req;
503
504 wValue = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
505
506 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
507 req.bRequest = FTDI_SIO_MODEM_CTRL;
508 USETW(req.wValue, wValue);
509 USETW(req.wIndex, wIndex);
510 USETW(req.wLength, 0);
511 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
512 &req, NULL, 0, 1000);
513}
514
515static void
516uftdi_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
517{
518 struct uftdi_softc *sc = ucom->sc_parent;
519 uint16_t wIndex = ucom->sc_portno;
520 uint16_t wValue;
521 struct usb_device_request req;
522
523 wValue = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
524
525 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
526 req.bRequest = FTDI_SIO_MODEM_CTRL;
527 USETW(req.wValue, wValue);
528 USETW(req.wIndex, wIndex);
529 USETW(req.wLength, 0);
530 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
531 &req, NULL, 0, 1000);
532}
533
534static void
535uftdi_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
536{
537 struct uftdi_softc *sc = ucom->sc_parent;
538 uint16_t wIndex = ucom->sc_portno;
539 uint16_t wValue;
540 struct usb_device_request req;
541
542 if (onoff) {
543 sc->sc_last_lcr |= FTDI_SIO_SET_BREAK;
544 } else {
545 sc->sc_last_lcr &= ~FTDI_SIO_SET_BREAK;
546 }
547
548 wValue = sc->sc_last_lcr;
549
550 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
551 req.bRequest = FTDI_SIO_SET_DATA;
552 USETW(req.wValue, wValue);
553 USETW(req.wIndex, wIndex);
554 USETW(req.wLength, 0);
555 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
556 &req, NULL, 0, 1000);
557}
558
559static int
560uftdi_set_parm_soft(struct termios *t,
561 struct uftdi_param_config *cfg, uint8_t type)
562{
563 bzero(cfg, sizeof(*cfg));
564
565 switch (type) {
566 case UFTDI_TYPE_SIO:
567 switch (t->c_ospeed) {
568 case 300:
569 cfg->rate = ftdi_sio_b300;
570 break;
571 case 600:
572 cfg->rate = ftdi_sio_b600;
573 break;
574 case 1200:
575 cfg->rate = ftdi_sio_b1200;
576 break;
577 case 2400:
578 cfg->rate = ftdi_sio_b2400;
579 break;
580 case 4800:
581 cfg->rate = ftdi_sio_b4800;
582 break;
583 case 9600:
584 cfg->rate = ftdi_sio_b9600;
585 break;
586 case 19200:
587 cfg->rate = ftdi_sio_b19200;
588 break;
589 case 38400:
590 cfg->rate = ftdi_sio_b38400;
591 break;
592 case 57600:
593 cfg->rate = ftdi_sio_b57600;
594 break;
595 case 115200:
596 cfg->rate = ftdi_sio_b115200;
597 break;
598 default:
599 return (EINVAL);
600 }
601 break;
602
603 case UFTDI_TYPE_8U232AM:
604 if (uftdi_8u232am_getrate(t->c_ospeed, &cfg->rate)) {
605 return (EINVAL);
606 }
607 break;
608 }
609
610 if (t->c_cflag & CSTOPB)
611 cfg->lcr = FTDI_SIO_SET_DATA_STOP_BITS_2;
612 else
613 cfg->lcr = FTDI_SIO_SET_DATA_STOP_BITS_1;
614
615 if (t->c_cflag & PARENB) {
616 if (t->c_cflag & PARODD) {
617 cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_ODD;
618 } else {
619 cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_EVEN;
620 }
621 } else {
622 cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_NONE;
623 }
624
625 switch (t->c_cflag & CSIZE) {
626 case CS5:
627 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(5);
628 break;
629
630 case CS6:
631 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(6);
632 break;
633
634 case CS7:
635 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(7);
636 break;
637
638 case CS8:
639 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(8);
640 break;
641 }
642
643 if (t->c_cflag & CRTSCTS) {
644 cfg->v_flow = FTDI_SIO_RTS_CTS_HS;
645 } else if (t->c_iflag & (IXON | IXOFF)) {
646 cfg->v_flow = FTDI_SIO_XON_XOFF_HS;
647 cfg->v_start = t->c_cc[VSTART];
648 cfg->v_stop = t->c_cc[VSTOP];
649 } else {
650 cfg->v_flow = FTDI_SIO_DISABLE_FLOW_CTRL;
651 }
652
653 return (0);
654}
655
656static int
657uftdi_pre_param(struct ucom_softc *ucom, struct termios *t)
658{
659 struct uftdi_softc *sc = ucom->sc_parent;
660 struct uftdi_param_config cfg;
661
662 DPRINTF("\n");
663
664 return (uftdi_set_parm_soft(t, &cfg, sc->sc_type));
665}
666
667static void
668uftdi_cfg_param(struct ucom_softc *ucom, struct termios *t)
669{
670 struct uftdi_softc *sc = ucom->sc_parent;
671 uint16_t wIndex = ucom->sc_portno;
672 struct uftdi_param_config cfg;
673 struct usb_device_request req;
674
675 if (uftdi_set_parm_soft(t, &cfg, sc->sc_type)) {
676 /* should not happen */
677 return;
678 }
679 sc->sc_last_lcr = cfg.lcr;
680
681 DPRINTF("\n");
682
683 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
684 req.bRequest = FTDI_SIO_SET_BAUD_RATE;
685 USETW(req.wValue, cfg.rate);
686 USETW(req.wIndex, wIndex);
687 USETW(req.wLength, 0);
688 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
689 &req, NULL, 0, 1000);
690
691 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
692 req.bRequest = FTDI_SIO_SET_DATA;
693 USETW(req.wValue, cfg.lcr);
694 USETW(req.wIndex, wIndex);
695 USETW(req.wLength, 0);
696 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
697 &req, NULL, 0, 1000);
698
699 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
700 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
701 USETW2(req.wValue, cfg.v_stop, cfg.v_start);
702 USETW2(req.wIndex, cfg.v_flow, wIndex);
703 USETW(req.wLength, 0);
704 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
705 &req, NULL, 0, 1000);
706}
707
708static void
709uftdi_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
710{
711 struct uftdi_softc *sc = ucom->sc_parent;
712
713 DPRINTF("msr=0x%02x lsr=0x%02x\n",
714 sc->sc_msr, sc->sc_lsr);
715
716 *msr = sc->sc_msr;
717 *lsr = sc->sc_lsr;
718}
719
720static void
721uftdi_start_read(struct ucom_softc *ucom)
722{
723 struct uftdi_softc *sc = ucom->sc_parent;
724
725 usbd_transfer_start(sc->sc_xfer[UFTDI_BULK_DT_RD]);
726}
727
728static void
729uftdi_stop_read(struct ucom_softc *ucom)
730{
731 struct uftdi_softc *sc = ucom->sc_parent;
732
733 usbd_transfer_stop(sc->sc_xfer[UFTDI_BULK_DT_RD]);
734}
735
736static void
737uftdi_start_write(struct ucom_softc *ucom)
738{
739 struct uftdi_softc *sc = ucom->sc_parent;
740
741 usbd_transfer_start(sc->sc_xfer[UFTDI_BULK_DT_WR]);
742}
743
744static void
745uftdi_stop_write(struct ucom_softc *ucom)
746{
747 struct uftdi_softc *sc = ucom->sc_parent;
748
749 usbd_transfer_stop(sc->sc_xfer[UFTDI_BULK_DT_WR]);
750}
751
752/*------------------------------------------------------------------------*
753 * uftdi_8u232am_getrate
754 *
755 * Return values:
756 * 0: Success
757 * Else: Failure
758 *------------------------------------------------------------------------*/
759static uint8_t
760uftdi_8u232am_getrate(uint32_t speed, uint16_t *rate)
761{
762 /* Table of the nearest even powers-of-2 for values 0..15. */
763 static const uint8_t roundoff[16] = {
764 0, 2, 2, 4, 4, 4, 8, 8,
765 8, 8, 8, 8, 16, 16, 16, 16,
766 };
767 uint32_t d;
768 uint32_t freq;
769 uint16_t result;
770
771 if ((speed < 178) || (speed > ((3000000 * 100) / 97)))
772 return (1); /* prevent numerical overflow */
773
774 /* Special cases for 2M and 3M. */
775 if ((speed >= ((3000000 * 100) / 103)) &&
776 (speed <= ((3000000 * 100) / 97))) {
777 result = 0;
778 goto done;
779 }
780 if ((speed >= ((2000000 * 100) / 103)) &&
781 (speed <= ((2000000 * 100) / 97))) {
782 result = 1;
783 goto done;
784 }
785 d = (FTDI_8U232AM_FREQ << 4) / speed;
786 d = (d & ~15) + roundoff[d & 15];
787
788 if (d < FTDI_8U232AM_MIN_DIV)
789 d = FTDI_8U232AM_MIN_DIV;
790 else if (d > FTDI_8U232AM_MAX_DIV)
791 d = FTDI_8U232AM_MAX_DIV;
792
793 /*
794 * Calculate the frequency needed for "d" to exactly divide down to
795 * our target "speed", and check that the actual frequency is within
796 * 3% of this.
797 */
798 freq = (speed * d);
799 if ((freq < ((FTDI_8U232AM_FREQ * 1600ULL) / 103)) ||
800 (freq > ((FTDI_8U232AM_FREQ * 1600ULL) / 97)))
801 return (1);
802
803 /*
804 * Pack the divisor into the resultant value. The lower 14-bits
805 * hold the integral part, while the upper 2 bits encode the
806 * fractional component: either 0, 0.5, 0.25, or 0.125.
807 */
808 result = (d >> 4);
809 if (d & 8)
810 result |= 0x4000;
811 else if (d & 4)
812 result |= 0x8000;
813 else if (d & 2)
814 result |= 0xc000;
815
816done:
817 *rate = result;
818 return (0);
819}
820
821static void
822uftdi_poll(struct ucom_softc *ucom)
823{
824 struct uftdi_softc *sc = ucom->sc_parent;
825 usbd_transfer_poll(sc->sc_xfer, UFTDI_N_TRANSFER);
826}