Simplify the code a lot - don't try to be too clever and handle chips with
[dragonfly.git] / sys / dev / usbmisc / uftdi / uftdi.c
CommitLineData
62ade751 1/*
1550dfd9 2 * $NetBSD: uftdi.c,v 1.13 2002/09/23 05:51:23 simonb Exp $
fdc9ec00 3 * $FreeBSD: src/sys/dev/usb/uftdi.c,v 1.37 2007/06/22 05:53:05 imp Exp $
048c1f54 4 * $DragonFly: src/sys/dev/usbmisc/uftdi/uftdi.c,v 1.20 2007/10/12 11:15:53 hasso Exp $
62ade751 5 */
984263bc 6
fdc9ec00 7/*-
984263bc
MD
8 * Copyright (c) 2000 The NetBSD Foundation, Inc.
9 * All rights reserved.
10 *
11 * This code is derived from software contributed to The NetBSD Foundation
12 * by Lennart Augustsson (lennart@augustsson.net).
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the NetBSD
25 * Foundation, Inc. and its contributors.
26 * 4. Neither the name of The NetBSD Foundation nor the names of its
27 * contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
41 */
42
43/*
44 * FTDI FT8U100AX serial adapter driver
45 */
46
984263bc
MD
47#include <sys/cdefs.h>
48
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/kernel.h>
52#include <sys/malloc.h>
fdc9ec00 53#include <sys/module.h>
984263bc
MD
54#include <sys/bus.h>
55#include <sys/ioccom.h>
56#include <sys/fcntl.h>
57#include <sys/conf.h>
58#include <sys/tty.h>
59#include <sys/file.h>
60
984263bc 61#include <sys/select.h>
984263bc
MD
62
63#include <sys/sysctl.h>
64
1f2de5d4
MD
65#include <bus/usb/usb.h>
66#include <bus/usb/usbhid.h>
984263bc 67
1f2de5d4
MD
68#include <bus/usb/usbdi.h>
69#include <bus/usb/usbdi_util.h>
70#include <bus/usb/usbdevs.h>
984263bc 71
1f2de5d4 72#include "../ucom/ucomvar.h"
984263bc 73
1f2de5d4 74#include "uftdireg.h"
984263bc
MD
75
76#ifdef USB_DEBUG
77static int uftdidebug = 0;
78SYSCTL_NODE(_hw_usb, OID_AUTO, uftdi, CTLFLAG_RW, 0, "USB uftdi");
79SYSCTL_INT(_hw_usb_uftdi, OID_AUTO, debug, CTLFLAG_RW,
80 &uftdidebug, 0, "uftdi debug level");
81#define DPRINTF(x) do { \
82 if (uftdidebug) \
fc1ef497 83 kprintf x; \
984263bc
MD
84 } while (0)
85
86#define DPRINTFN(n, x) do { \
87 if (uftdidebug > (n)) \
fc1ef497 88 kprintf x; \
984263bc
MD
89 } while (0)
90
91#else
92#define DPRINTF(x)
93#define DPRINTFN(n,x)
94#endif
95
96#define UFTDI_CONFIG_INDEX 0
97#define UFTDI_IFACE_INDEX 0
98
984263bc
MD
99/*
100 * These are the maximum number of bytes transferred per frame.
101 * The output buffer size cannot be increased due to the size encoding.
102 */
103#define UFTDIIBUFSIZE 64
104#define UFTDIOBUFSIZE 64
105
106struct uftdi_softc {
107 struct ucom_softc sc_ucom;
984263bc 108 usbd_interface_handle sc_iface; /* interface */
984263bc
MD
109 enum uftdi_type sc_type;
110 u_int sc_hdrlen;
984263bc
MD
111 u_char sc_msr;
112 u_char sc_lsr;
984263bc
MD
113 u_int last_lcr;
114};
115
6ed427ca
HT
116static void uftdi_get_status(void *, int portno, u_char *lsr, u_char *msr);
117static void uftdi_set(void *, int, int, int);
118static int uftdi_param(void *, int, struct termios *);
119static int uftdi_open(void *sc, int portno);
120static void uftdi_read(void *sc, int portno, u_char **ptr,u_int32_t *count);
121static void uftdi_write(void *sc, int portno, u_char *to, u_char *from,
984263bc 122 u_int32_t *count);
6ed427ca 123static void uftdi_break(void *sc, int portno, int onoff);
21f337aa 124static int uftdi_8u232am_getrate(speed_t speed, int *rate);
984263bc
MD
125
126struct ucom_callback uftdi_callback = {
127 uftdi_get_status,
128 uftdi_set,
129 uftdi_param,
130 NULL,
131 uftdi_open,
132 NULL,
133 uftdi_read,
134 uftdi_write,
135};
136
81787541 137static const struct usb_devno uftdi_devs[] = {
a707bf94
HT
138 { USB_VENDOR_BBELECTR, USB_PRODUCT_BBELECTR_232USB9M },
139 { USB_VENDOR_BBELECTR, USB_PRODUCT_BBELECTR_485USB9F2W },
140 { USB_VENDOR_BBELECTR, USB_PRODUCT_BBELECTR_485USB9F4W },
141 { USB_VENDOR_BBELECTR, USB_PRODUCT_BBELECTR_USO9ML2 },
142 { USB_VENDOR_BBELECTR, USB_PRODUCT_BBELECTR_USO9ML2DR },
143 { USB_VENDOR_BBELECTR, USB_PRODUCT_BBELECTR_USO9ML2DR2 },
144 { USB_VENDOR_BBELECTR, USB_PRODUCT_BBELECTR_USOPTL4 },
145 { USB_VENDOR_BBELECTR, USB_PRODUCT_BBELECTR_USOPTL4DR },
146 { USB_VENDOR_BBELECTR, USB_PRODUCT_BBELECTR_USOPTL4DR2 },
147 { USB_VENDOR_BBELECTR, USB_PRODUCT_BBELECTR_USOTL4 },
148 { USB_VENDOR_BBELECTR, USB_PRODUCT_BBELECTR_USPTL4 },
149 { USB_VENDOR_BBELECTR, USB_PRODUCT_BBELECTR_USTL4 },
150 { USB_VENDOR_EVOLUTION, USB_PRODUCT_EVOLUTION_ER1 },
151 { USB_VENDOR_EVOLUTION, USB_PRODUCT_EVOLUTION_RCM4_1 },
152 { USB_VENDOR_EVOLUTION, USB_PRODUCT_EVOLUTION_RCM4_2 },
153 { USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_SAMBA },
154 { USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_TWIST },
155 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ACCESSO },
156 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ACG_HFDUAL },
157 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ACTROBOTS },
158 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ACTZWAVE },
159 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_AMC232 },
160 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ARTEMIS },
161 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ASK_RDR4X7_1 },
162 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ASK_RDR4X7_2 },
163 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ASK_RDR4X7_3 },
164 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ASK_RDR4X7_4 },
165 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ASK_RDR4X7_5 },
166 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ASK_RDR4X7_6 },
167 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ASK_RDR4X7_7 },
168 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ASK_RDR4X7_8 },
169 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ATK16 },
170 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ATK16C },
171 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ATK16HR },
172 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ATK16HRC },
173 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CANUSB },
174 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CCS_ICDU20 },
175 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CCS_ICDU40 },
176 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CCS_MACHX },
177 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_547 },
178 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_631 },
a707bf94 179 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_632 },
81787541
HT
180 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_633 },
181 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_634 },
182 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_635 },
a707bf94
HT
183 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_640 },
184 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_642 },
185 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CHAMELEON },
186 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_DMX4ALL },
187 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ECLO_1WIRE },
188 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ECO_PRO },
81787541 189 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_EISCOU },
a707bf94
HT
190 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ELV_ALC8500 },
191 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ELV_FHZ1000PC },
192 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ELV_UM100 },
193 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ELV_UO100 },
194 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ELV_UR100 },
81787541 195 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_EMCU2D },
81787541 196 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_EMCU2H },
a707bf94
HT
197 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_FT2232 },
198 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_FT232_1 },
199 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_FT232_2 },
200 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_FT232_3 },
201 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_FT232_4 },
202 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_FT232_5 },
203 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_FT232_6 },
204 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_FT8U100AX },
205 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_GAMMASCOUT },
206 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_GUDE_1 },
207 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_GUDE_2 },
208 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_GUDE_3 },
209 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_GUDE_4 },
210 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_GUDE_5 },
211 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_GUDE_6 },
212 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_GUDE_7 },
213 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_GUDE_8 },
214 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_GUDE_9 },
215 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_GUDE_A },
216 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_GUDE_B },
217 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_IBS_1 },
218 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_IBS_APP70 },
219 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_IBS_PCMCIA },
220 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_IBS_PEDO },
221 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_IBS_PICPRO },
222 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_IBS_PK1 },
223 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_IBS_RS232MON },
224 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_IBS_US485 },
225 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_IPLUS },
226 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LINX_1 },
227 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LINX_2 },
228 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LINX_3 },
229 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LINX_MASTER2 },
230 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LINX_SDMUSBQSS },
231 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LOCOBUFFER },
232 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MATRIX_1 },
233 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MATRIX_2 },
234 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MATRIX_3 },
235 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MATRIX_LK202 },
236 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MATRIX_LK204 },
237 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MATRIX_MX2_3 },
238 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MATRIX_MX4_5 },
239 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MAXS_PKGU },
240 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_DB9 },
241 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_IC },
242 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_KW },
243 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_RS232 },
244 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y6 },
245 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y8 },
246 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y9 },
247 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_YS },
248 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MJS_SIRIUS_PC },
249 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_PCDJ_DAC2 },
250 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_PCMSFU },
251 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_PYRAMID },
252 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SEMC_DSS20 },
253 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SEMC_DSS20 },
254 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_TACTRIX_OP_13M },
255 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_TACTRIX_OP_13S },
256 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_TACTRIX_OP_13U },
257 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_TERATRONIK_D2XX },
258 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_TERATRONIK_VCP },
259 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_THORLABS },
260 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_TIRA1 },
261 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_TNCX },
262 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_UOPTBR },
263 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_USBUIRT },
264 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_VNHC },
265 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_WESTREX_777 },
266 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_WESTREX_8900F },
267 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_XSENS_1 },
268 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_XSENS_2 },
269 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_XSENS_3 },
270 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_XSENS_4 },
271 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_XSENS_5 },
272 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_XSENS_6 },
273 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_XSENS_7 },
274 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_XSENS_8 },
275 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_YEI_SC31 },
276 { USB_VENDOR_ICOM, USB_PRODUCT_ICOM_ID1 },
277 { USB_VENDOR_ICOM, USB_PRODUCT_ICOM_RP2000VR },
278 { USB_VENDOR_ICOM, USB_PRODUCT_ICOM_RP2000VT },
279 { USB_VENDOR_ICOM, USB_PRODUCT_ICOM_RP2C1 },
280 { USB_VENDOR_ICOM, USB_PRODUCT_ICOM_RP2C2 },
281 { USB_VENDOR_ICOM, USB_PRODUCT_ICOM_RP2D },
282 { USB_VENDOR_ICOM, USB_PRODUCT_ICOM_RP2VR },
283 { USB_VENDOR_ICOM, USB_PRODUCT_ICOM_RP2VT },
284 { USB_VENDOR_ICOM, USB_PRODUCT_ICOM_RP4000VR },
285 { USB_VENDOR_ICOM, USB_PRODUCT_ICOM_RP4000VT },
286 { USB_VENDOR_IDTECH, USB_PRODUCT_IDTECH_SERIAL },
287 { USB_VENDOR_INTERBIO, USB_PRODUCT_INTERBIO_IOBOARD },
288 { USB_VENDOR_INTERBIO, USB_PRODUCT_INTERBIO_MINIIOBOARD },
81787541
HT
289 { USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_VALUECAN },
290 { USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_NEOVI },
a707bf94
HT
291 { USB_VENDOR_KOBIL, USB_PRODUCT_KOBIL_B1 },
292 { USB_VENDOR_KOBIL, USB_PRODUCT_KOBIL_KAAN },
81787541 293 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_PCOPRS1 },
a707bf94
HT
294 { USB_VENDOR_MOBILITY, USB_PRODUCT_MOBILITY_ED200H },
295 { USB_VENDOR_OCT, USB_PRODUCT_OCT_US101 },
296 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_AD4USB },
297 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_AP485_1 },
298 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_AP485_2 },
299 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_DRAK5 },
300 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_DRAK6 },
301 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_GOLIATH_MSR },
302 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_GOLIATH_MUX },
303 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_IRAMP },
304 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_LEC },
305 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_MUC },
306 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_QUIDO101 },
307 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_QUIDO216 },
308 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_QUIDO22 },
309 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_QUIDO303 },
310 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_QUIDO332 },
311 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_QUIDO44 },
312 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_QUIDO603 },
313 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_QUIDO88 },
314 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_SB232 },
315 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_SB422_1 },
316 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_SB422_2 },
317 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_SB485C },
318 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_SB485S },
319 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_SB485_1 },
320 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_SB485_2 },
321 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_SERIAL },
322 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_SIMUKEY },
323 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_STAVOVY },
324 { USB_VENDOR_PAPOUCH, USB_PRODUCT_PAPOUCH_TMU },
325 { USB_VENDOR_POSIFLEX, USB_PRODUCT_POSIFLEX_PP7000_1 },
326 { USB_VENDOR_POSIFLEX, USB_PRODUCT_POSIFLEX_PP7000_2 },
327 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2101 },
328 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2102 },
329 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2103 },
330 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2104 },
331 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2106 },
332 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2201_1 },
333 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2201_2 },
334 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2202_1 },
335 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2202_2 },
336 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2203_1 },
337 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2203_2 },
338 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2401_1 },
339 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2401_2 },
340 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2401_3 },
341 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2401_4 },
342 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2402_1 },
343 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2402_2 },
344 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2402_3 },
345 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2402_4 },
346 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2403_1 },
347 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2403_2 },
348 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2403_3 },
349 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2403_4 },
350 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2801_1 },
351 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2801_2 },
352 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2801_3 },
353 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2801_4 },
354 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2801_5 },
355 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2801_6 },
356 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2801_7 },
357 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2801_8 },
358 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2802_1 },
359 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2802_2 },
360 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2802_3 },
361 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2802_4 },
362 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2802_5 },
363 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2802_6 },
364 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2802_7 },
365 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2802_8 },
366 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2803_1 },
367 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2803_2 },
368 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2803_3 },
369 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2803_4 },
370 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2803_5 },
371 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2803_6 },
372 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2803_7 },
373 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_2803_8 },
374 { USB_VENDOR_TESTO, USB_PRODUCT_TESTO_174 },
375 { USB_VENDOR_TESTO, USB_PRODUCT_TESTO_175 },
376 { USB_VENDOR_TESTO, USB_PRODUCT_TESTO_330 },
377 { USB_VENDOR_TESTO, USB_PRODUCT_TESTO_435 },
378 { USB_VENDOR_TESTO, USB_PRODUCT_TESTO_556 },
379 { USB_VENDOR_TESTO, USB_PRODUCT_TESTO_580 },
380 { USB_VENDOR_TESTO, USB_PRODUCT_TESTO_845 },
381 { USB_VENDOR_TESTO, USB_PRODUCT_TESTO_SERIAL_1 },
382 { USB_VENDOR_TESTO, USB_PRODUCT_TESTO_SERIAL_2 },
383 { USB_VENDOR_TESTO, USB_PRODUCT_TESTO_SERVICE },
384 { USB_VENDOR_THURLBY, USB_PRODUCT_THURLBY_QL355P },
81787541
HT
385 { 0, 0 }
386};
387
e785a5d9
HT
388static int
389uftdi_match(device_t self)
984263bc 390{
e785a5d9 391 struct usb_attach_arg *uaa = device_get_ivars(self);
984263bc 392
048c1f54 393 if (uaa->iface == NULL)
984263bc 394 return (UMATCH_NONE);
81787541 395
048c1f54 396 if (usb_lookup(uftdi_devs, uaa->vendor, uaa->product) != NULL)
81787541 397 return (UMATCH_VENDOR_IFACESUBCLASS);
048c1f54
HT
398
399 return (UMATCH_NONE);
984263bc
MD
400}
401
e785a5d9
HT
402static int
403uftdi_attach(device_t self)
984263bc 404{
e785a5d9
HT
405 struct uftdi_softc *sc = device_get_softc(self);
406 struct usb_attach_arg *uaa = device_get_ivars(self);
984263bc
MD
407 usbd_device_handle dev = uaa->device;
408 usbd_interface_handle iface;
409 usb_interface_descriptor_t *id;
410 usb_endpoint_descriptor_t *ed;
411 char *devinfo;
984263bc 412 int i;
984263bc
MD
413 struct ucom_softc *ucom = &sc->sc_ucom;
414 DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc));
efda3bd0 415 devinfo = kmalloc(1024, M_USBDEV, M_INTWAIT);
984263bc
MD
416
417 ucom->sc_dev = self;
418 ucom->sc_udev = dev;
419
fdc9ec00
HT
420 usbd_devinfo(dev, 0, devinfo);
421 device_printf(ucom->sc_dev, "%s\n", devinfo);
422 kfree(devinfo, M_USBDEV);
984263bc 423
048c1f54 424 iface = uaa->iface;
984263bc
MD
425 id = usbd_get_interface_descriptor(iface);
426 ucom->sc_iface = iface;
fdc9ec00 427
81787541
HT
428 if (uaa->release < 0x0200) {
429 sc->sc_type = UFTDI_TYPE_SIO;
430 sc->sc_hdrlen = 1;
431 } else {
432 sc->sc_type = UFTDI_TYPE_8U232AM;
433 sc->sc_hdrlen = 0;
984263bc 434 }
1550dfd9 435
984263bc 436 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
1550dfd9 437
984263bc
MD
438 for (i = 0; i < id->bNumEndpoints; i++) {
439 int addr, dir, attr;
440 ed = usbd_interface2endpoint_descriptor(iface, i);
441 if (ed == NULL) {
fdc9ec00
HT
442 device_printf(ucom->sc_dev,
443 "could not read endpoint descriptor\n");
984263bc
MD
444 goto bad;
445 }
1550dfd9 446
984263bc
MD
447 addr = ed->bEndpointAddress;
448 dir = UE_GET_DIR(ed->bEndpointAddress);
449 attr = ed->bmAttributes & UE_XFERTYPE;
450 if (dir == UE_DIR_IN && attr == UE_BULK)
fdc9ec00 451 ucom->sc_bulkin_no = addr;
984263bc 452 else if (dir == UE_DIR_OUT && attr == UE_BULK)
fdc9ec00 453 ucom->sc_bulkout_no = addr;
984263bc 454 else {
fdc9ec00
HT
455 device_printf(ucom->sc_dev, "unexpected endpoint\n");
456 goto bad;
984263bc
MD
457 }
458 }
459 if (ucom->sc_bulkin_no == -1) {
fdc9ec00 460 device_printf(ucom->sc_dev, "Could not find data bulk in\n");
984263bc
MD
461 goto bad;
462 }
463 if (ucom->sc_bulkout_no == -1) {
fdc9ec00 464 device_printf(ucom->sc_dev, "Could not find data bulk out\n");
984263bc
MD
465 goto bad;
466 }
fdc9ec00 467 ucom->sc_parent = sc;
048c1f54 468 ucom->sc_portno = FTDI_PIT_SIOA + id->bInterfaceNumber;
984263bc
MD
469 /* bulkin, bulkout set above */
470
471 ucom->sc_ibufsize = UFTDIIBUFSIZE;
472 ucom->sc_obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
473 ucom->sc_ibufsizepad = UFTDIIBUFSIZE;
474 ucom->sc_opkthdrlen = sc->sc_hdrlen;
475
476
477 ucom->sc_callback = &uftdi_callback;
478#if 0
479 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
fdc9ec00 480 ucom->sc_dev);
984263bc
MD
481#endif
482 DPRINTF(("uftdi: in=0x%x out=0x%x\n", ucom->sc_bulkin_no, ucom->sc_bulkout_no));
483 ucom_attach(&sc->sc_ucom);
e785a5d9 484 return 0;
984263bc
MD
485
486bad:
487 DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
488 ucom->sc_dying = 1;
e785a5d9 489 return ENXIO;
984263bc
MD
490}
491#if 0
492int
6ed427ca 493uftdi_activate(device_t self, enum devact act)
984263bc
MD
494{
495 struct uftdi_softc *sc = (struct uftdi_softc *)self;
496 int rv = 0;
497
498 switch (act) {
499 case DVACT_ACTIVATE:
500 return (EOPNOTSUPP);
984263bc
MD
501
502 case DVACT_DEACTIVATE:
503 if (sc->sc_subdev != NULL)
504 rv = config_deactivate(sc->sc_subdev);
62ade751 505 sc->sc_ucom.sc_dying = 1;
984263bc
MD
506 break;
507 }
508 return (rv);
509}
510#endif
fdc9ec00 511
e785a5d9
HT
512static int
513uftdi_detach(device_t self)
984263bc 514{
e785a5d9 515 struct uftdi_softc *sc = device_get_softc(self);
1550dfd9 516
984263bc
MD
517 int rv = 0;
518
519 DPRINTF(("uftdi_detach: sc=%p\n", sc));
62ade751 520 sc->sc_ucom.sc_dying = 1;
984263bc
MD
521 rv = ucom_detach(&sc->sc_ucom);
522
523 return rv;
524}
fdc9ec00 525
6ed427ca 526static int
984263bc
MD
527uftdi_open(void *vsc, int portno)
528{
529 struct uftdi_softc *sc = vsc;
62ade751 530 struct ucom_softc *ucom = &sc->sc_ucom;
984263bc
MD
531 usb_device_request_t req;
532 usbd_status err;
533 struct termios t;
534
535 DPRINTF(("uftdi_open: sc=%p\n", sc));
536
62ade751 537 if (ucom->sc_dying)
984263bc
MD
538 return (EIO);
539
540 /* Perform a full reset on the device */
541 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
542 req.bRequest = FTDI_SIO_RESET;
543 USETW(req.wValue, FTDI_SIO_RESET_SIO);
544 USETW(req.wIndex, portno);
545 USETW(req.wLength, 0);
546 err = usbd_do_request(ucom->sc_udev, &req, NULL);
547 if (err)
548 return (EIO);
549
550 /* Set 9600 baud, 2 stop bits, no parity, 8 bits */
551 t.c_ospeed = 9600;
552 t.c_cflag = CSTOPB | CS8;
553 (void)uftdi_param(sc, portno, &t);
554
555 /* Turn on RTS/CTS flow control */
556 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
557 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
558 USETW(req.wValue, 0);
559 USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
560 USETW(req.wLength, 0);
561 err = usbd_do_request(ucom->sc_udev, &req, NULL);
562 if (err)
563 return (EIO);
564
565 return (0);
566}
567
6ed427ca 568static void
984263bc
MD
569uftdi_read(void *vsc, int portno, u_char **ptr, u_int32_t *count)
570{
571 struct uftdi_softc *sc = vsc;
572 u_char msr, lsr;
573
574 DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
575 *count));
576
577 msr = FTDI_GET_MSR(*ptr);
578 lsr = FTDI_GET_LSR(*ptr);
579
580#ifdef USB_DEBUG
581 if (*count != 2)
582 DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
583 "0x%02x\n", sc, portno, *count, (*ptr)[2]));
584#endif
585
586 if (sc->sc_msr != msr ||
587 (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
588 DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
589 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
590 lsr, sc->sc_lsr));
591 sc->sc_msr = msr;
592 sc->sc_lsr = lsr;
593 ucom_status_change(&sc->sc_ucom);
594 }
595
596 /* Pick up status and adjust data part. */
597 *ptr += 2;
598 *count -= 2;
599}
600
6ed427ca 601static void
984263bc
MD
602uftdi_write(void *vsc, int portno, u_char *to, u_char *from, u_int32_t *count)
603{
604 struct uftdi_softc *sc = vsc;
605
606 DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
607 vsc, portno, *count, from[0]));
608
609 /* Make length tag and copy data */
610 if (sc->sc_hdrlen > 0)
611 *to = FTDI_OUT_TAG(*count, portno);
612
613 memcpy(to + sc->sc_hdrlen, from, *count);
614 *count += sc->sc_hdrlen;
615}
616
6ed427ca 617static void
984263bc
MD
618uftdi_set(void *vsc, int portno, int reg, int onoff)
619{
620 struct uftdi_softc *sc = vsc;
621 struct ucom_softc *ucom = vsc;
622 usb_device_request_t req;
623 int ctl;
624
625 DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
626 reg, onoff));
627
628 switch (reg) {
629 case UCOM_SET_DTR:
630 ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
631 break;
632 case UCOM_SET_RTS:
633 ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
634 break;
635 case UCOM_SET_BREAK:
636 uftdi_break(sc, portno, onoff);
637 return;
638 default:
639 return;
640 }
641 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
642 req.bRequest = FTDI_SIO_MODEM_CTRL;
643 USETW(req.wValue, ctl);
644 USETW(req.wIndex, portno);
645 USETW(req.wLength, 0);
646 DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
647 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
648 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
649 (void)usbd_do_request(ucom->sc_udev, &req, NULL);
650}
651
6ed427ca 652static int
984263bc
MD
653uftdi_param(void *vsc, int portno, struct termios *t)
654{
655 struct uftdi_softc *sc = vsc;
62ade751 656 struct ucom_softc *ucom = &sc->sc_ucom;
984263bc
MD
657 usb_device_request_t req;
658 usbd_status err;
659 int rate=0, data, flow;
660
661 DPRINTF(("uftdi_param: sc=%p\n", sc));
662
62ade751 663 if (ucom->sc_dying)
984263bc
MD
664 return (EIO);
665
666 switch (sc->sc_type) {
667 case UFTDI_TYPE_SIO:
668 switch (t->c_ospeed) {
669 case 300: rate = ftdi_sio_b300; break;
670 case 600: rate = ftdi_sio_b600; break;
671 case 1200: rate = ftdi_sio_b1200; break;
672 case 2400: rate = ftdi_sio_b2400; break;
673 case 4800: rate = ftdi_sio_b4800; break;
674 case 9600: rate = ftdi_sio_b9600; break;
675 case 19200: rate = ftdi_sio_b19200; break;
676 case 38400: rate = ftdi_sio_b38400; break;
677 case 57600: rate = ftdi_sio_b57600; break;
678 case 115200: rate = ftdi_sio_b115200; break;
679 default:
680 return (EINVAL);
681 }
682 break;
683
684 case UFTDI_TYPE_8U232AM:
21f337aa 685 if (uftdi_8u232am_getrate(t->c_ospeed, &rate) == -1)
984263bc 686 return (EINVAL);
984263bc
MD
687 break;
688 }
689 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
690 req.bRequest = FTDI_SIO_SET_BAUD_RATE;
691 USETW(req.wValue, rate);
692 USETW(req.wIndex, portno);
693 USETW(req.wLength, 0);
694 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
695 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
696 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
697 err = usbd_do_request(ucom->sc_udev, &req, NULL);
698 if (err)
699 return (EIO);
700
701 if (ISSET(t->c_cflag, CSTOPB))
702 data = FTDI_SIO_SET_DATA_STOP_BITS_2;
703 else
704 data = FTDI_SIO_SET_DATA_STOP_BITS_1;
705 if (ISSET(t->c_cflag, PARENB)) {
706 if (ISSET(t->c_cflag, PARODD))
707 data |= FTDI_SIO_SET_DATA_PARITY_ODD;
708 else
709 data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
710 } else
711 data |= FTDI_SIO_SET_DATA_PARITY_NONE;
712 switch (ISSET(t->c_cflag, CSIZE)) {
713 case CS5:
714 data |= FTDI_SIO_SET_DATA_BITS(5);
715 break;
716 case CS6:
717 data |= FTDI_SIO_SET_DATA_BITS(6);
718 break;
719 case CS7:
720 data |= FTDI_SIO_SET_DATA_BITS(7);
721 break;
722 case CS8:
723 data |= FTDI_SIO_SET_DATA_BITS(8);
724 break;
725 }
726 sc->last_lcr = data;
727
728 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
729 req.bRequest = FTDI_SIO_SET_DATA;
730 USETW(req.wValue, data);
731 USETW(req.wIndex, portno);
732 USETW(req.wLength, 0);
733 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
734 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
735 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
736 err = usbd_do_request(ucom->sc_udev, &req, NULL);
737 if (err)
738 return (EIO);
739
740 if (ISSET(t->c_cflag, CRTSCTS)) {
741 flow = FTDI_SIO_RTS_CTS_HS;
742 USETW(req.wValue, 0);
743 } else if (ISSET(t->c_iflag, IXON|IXOFF)) {
744 flow = FTDI_SIO_XON_XOFF_HS;
745 USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
746 } else {
747 flow = FTDI_SIO_DISABLE_FLOW_CTRL;
748 USETW(req.wValue, 0);
749 }
750 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
751 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
752 USETW2(req.wIndex, flow, portno);
753 USETW(req.wLength, 0);
754 err = usbd_do_request(ucom->sc_udev, &req, NULL);
755 if (err)
756 return (EIO);
757
758 return (0);
759}
760
761void
762uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
763{
764 struct uftdi_softc *sc = vsc;
765
766 DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
767 sc->sc_msr, sc->sc_lsr));
768
769 if (msr != NULL)
770 *msr = sc->sc_msr;
771 if (lsr != NULL)
772 *lsr = sc->sc_lsr;
773}
774
775void
776uftdi_break(void *vsc, int portno, int onoff)
777{
778 struct uftdi_softc *sc = vsc;
779 struct ucom_softc *ucom = vsc;
780
781 usb_device_request_t req;
782 int data;
783
784 DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
785 onoff));
786
787 if (onoff) {
788 data = sc->last_lcr | FTDI_SIO_SET_BREAK;
789 } else {
790 data = sc->last_lcr;
791 }
792
793 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
794 req.bRequest = FTDI_SIO_SET_DATA;
795 USETW(req.wValue, data);
796 USETW(req.wIndex, portno);
797 USETW(req.wLength, 0);
798 (void)usbd_do_request(ucom->sc_udev, &req, NULL);
799}
800
21f337aa
HT
801static int
802uftdi_8u232am_getrate(speed_t speed, int *rate)
803{
804 /* Table of the nearest even powers-of-2 for values 0..15. */
805 static const unsigned char roundoff[16] = {
806 0, 2, 2, 4, 4, 4, 8, 8,
807 8, 8, 8, 8, 16, 16, 16, 16,
808 };
809
810 unsigned int d, freq;
811 int result;
812
813 if (speed <= 0)
814 return (-1);
815
816 /* Special cases for 2M and 3M. */
817 if (speed >= 3000000 * 100 / 103 &&
818 speed <= 3000000 * 100 / 97) {
819 result = 0;
820 goto done;
821 }
822 if (speed >= 2000000 * 100 / 103 &&
823 speed <= 2000000 * 100 / 97) {
824 result = 1;
825 goto done;
826 }
827
828 d = (FTDI_8U232AM_FREQ << 4) / speed;
829 d = (d & ~15) + roundoff[d & 15];
830
831 if (d < FTDI_8U232AM_MIN_DIV)
832 d = FTDI_8U232AM_MIN_DIV;
833 else if (d > FTDI_8U232AM_MAX_DIV)
834 d = FTDI_8U232AM_MAX_DIV;
835
836 /*
837 * Calculate the frequency needed for d to exactly divide down
838 * to our target speed, and check that the actual frequency is
839 * within 3% of this.
840 */
841 freq = speed * d;
842 if (freq < (quad_t)(FTDI_8U232AM_FREQ << 4) * 100 / 103 ||
843 freq > (quad_t)(FTDI_8U232AM_FREQ << 4) * 100 / 97)
844 return (-1);
845
846 /*
847 * Pack the divisor into the resultant value. The lower
848 * 14-bits hold the integral part, while the upper 2 bits
849 * encode the fractional component: either 0, 0.5, 0.25, or
850 * 0.125.
851 */
852 result = d >> 4;
853 if (d & 8)
854 result |= 0x4000;
855 else if (d & 4)
856 result |= 0x8000;
857 else if (d & 2)
858 result |= 0xc000;
859
860done:
861 *rate = result;
862 return (0);
863}
6ed427ca 864static device_method_t uftdi_methods[] = {
984263bc
MD
865 /* Device interface */
866 DEVMETHOD(device_probe, uftdi_match),
867 DEVMETHOD(device_attach, uftdi_attach),
868 DEVMETHOD(device_detach, uftdi_detach),
869
870 { 0, 0 }
871};
872
6ed427ca 873static driver_t uftdi_driver = {
984263bc
MD
874 "ucom",
875 uftdi_methods,
876 sizeof (struct uftdi_softc)
877};
878
879DRIVER_MODULE(uftdi, uhub, uftdi_driver, ucom_devclass, usbd_driver_load, 0);
880MODULE_DEPEND(uftdi, usb, 1, 1, 1);
881MODULE_DEPEND(uftdi, ucom,UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);