Remove inclusion of <sys/cdefs.h> from kernel .c files.
[dragonfly.git] / sys / dev / usbmisc / uftdi / uftdi.c
1 /*
2  * $NetBSD: uftdi.c,v 1.13 2002/09/23 05:51:23 simonb Exp $
3  * $FreeBSD: src/sys/dev/usb/uftdi.c,v 1.37 2007/06/22 05:53:05 imp Exp $
4  */
5
6 /*-
7  * Copyright (c) 2000 The NetBSD Foundation, Inc.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Lennart Augustsson (lennart@augustsson.net).
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *        This product includes software developed by the NetBSD
24  *        Foundation, Inc. and its contributors.
25  * 4. Neither the name of The NetBSD Foundation nor the names of its
26  *    contributors may be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41
42 /*
43  * FTDI FT8U100AX serial adapter driver
44  */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/module.h>
51 #include <sys/bus.h>
52 #include <sys/fcntl.h>
53 #include <sys/conf.h>
54 #include <sys/tty.h>
55 #include <sys/file.h>
56
57 #include <sys/select.h>
58
59 #include <sys/sysctl.h>
60
61 #include <bus/usb/usb.h>
62 #include <bus/usb/usbhid.h>
63
64 #include <bus/usb/usbdi.h>
65 #include <bus/usb/usbdi_util.h>
66
67 #include "../ucom/ucomvar.h"
68
69 #include "uftdireg.h"
70
71 #ifdef USB_DEBUG
72 static int uftdidebug = 0;
73 SYSCTL_NODE(_hw_usb, OID_AUTO, uftdi, CTLFLAG_RW, 0, "USB uftdi");
74 SYSCTL_INT(_hw_usb_uftdi, OID_AUTO, debug, CTLFLAG_RW,
75            &uftdidebug, 0, "uftdi debug level");
76 #define DPRINTF(x)      do { \
77                                 if (uftdidebug) \
78                                         kprintf x; \
79                         } while (0)
80
81 #define DPRINTFN(n, x)  do { \
82                                 if (uftdidebug > (n)) \
83                                         kprintf x; \
84                         } while (0)
85
86 #else
87 #define DPRINTF(x)
88 #define DPRINTFN(n,x)
89 #endif
90
91 #define UFTDI_CONFIG_INDEX      0
92 #define UFTDI_IFACE_INDEX       0
93
94 /*
95  * These are the maximum number of bytes transferred per frame.
96  * The output buffer size cannot be increased due to the size encoding.
97  */
98 #define UFTDIIBUFSIZE 64
99 #define UFTDIOBUFSIZE 64
100
101 struct uftdi_softc {
102         struct ucom_softc       sc_ucom;
103         usbd_interface_handle   sc_iface;       /* interface */
104         enum uftdi_type         sc_type;
105         u_int                   sc_hdrlen;
106         u_char                  sc_msr;
107         u_char                  sc_lsr;
108         u_int                   last_lcr;
109 };
110
111 static void     uftdi_get_status(void *, int portno, u_char *lsr, u_char *msr);
112 static void     uftdi_set(void *, int, int, int);
113 static int      uftdi_param(void *, int, struct termios *);
114 static int      uftdi_open(void *sc, int portno);
115 static void     uftdi_read(void *sc, int portno, u_char **ptr,u_int32_t *count);
116 static void     uftdi_write(void *sc, int portno, u_char *to, u_char *from,
117                             u_int32_t *count);
118 static void     uftdi_break(void *sc, int portno, int onoff);
119 static int      uftdi_8u232am_getrate(speed_t speed, int *rate);
120
121 struct ucom_callback uftdi_callback = {
122         uftdi_get_status,
123         uftdi_set,
124         uftdi_param,
125         NULL,
126         uftdi_open,
127         NULL,
128         uftdi_read,
129         uftdi_write,
130 };
131
132 static const struct usb_devno uftdi_devs[] = {
133         /* FTDI chips defaults */
134         { USB_DEVICE(0x0403, 0x0232) }, /* FT232 serial converter */
135         { USB_DEVICE(0x0403, 0x6001) }, /* FT232 serial converter */
136         { USB_DEVICE(0x0403, 0x6006) }, /* FT232 serial converter */
137         { USB_DEVICE(0x0403, 0x6007) }, /* FT232 serial converter */
138         { USB_DEVICE(0x0403, 0x6008) }, /* FT232 serial converter */
139         { USB_DEVICE(0x0403, 0x6009) }, /* FT232 serial converter */
140         { USB_DEVICE(0x0403, 0x6010) }, /* FT2232 dual port serial converter */
141         { USB_DEVICE(0x0403, 0x8372) }, /* FTDI 8U100AX USB hub controller */
142
143         /* RR-CirKits products */
144         { USB_DEVICE(0x0403, 0xc7d0) }, /* LocoBuffer USB */
145
146         /* DMX4ALL products */
147         { USB_DEVICE(0x0403, 0xc850) }, /* DMX interface */
148
149         /* ASK products */
150         { USB_DEVICE(0x0403, 0xc990) }, /* RDR 4X7 series card reader */
151         { USB_DEVICE(0x0403, 0xc991) }, /* RDR 4X7 series card reader */
152         { USB_DEVICE(0x0403, 0xc992) }, /* RDR 4X7 series card reader */
153         { USB_DEVICE(0x0403, 0xc993) }, /* RDR 4X7 series card reader */
154         { USB_DEVICE(0x0403, 0xc994) }, /* RDR 4X7 series card reader */
155         { USB_DEVICE(0x0403, 0xc995) }, /* RDR 4X7 series card reader */
156         { USB_DEVICE(0x0403, 0xc996) }, /* RDR 4X7 series card reader */
157         { USB_DEVICE(0x0403, 0xc997) }, /* RDR 4X7 series card reader */
158
159         /* MJS products */
160         { USB_DEVICE(0x0403, 0xca81) }, /* Sirius To PC Interface */
161
162         /* Starting Point Systems products */
163         { USB_DEVICE(0x0403, 0xcaa0) }, /* µChameleon */
164
165         /* Tactrix products */
166         { USB_DEVICE(0x0403, 0xcc48) }, /* OpenPort 1.3 Mitsubishi */
167         { USB_DEVICE(0x0403, 0xcc49) }, /* OpenPort 1.3 Subaru */
168         { USB_DEVICE(0x0403, 0xcc4a) }, /* OpenPort 1.3 Universal */
169
170         /* Plus GSM products */
171         { USB_DEVICE(0x0403, 0xd070) }, /* Plus GSM iPlus */
172
173         /* Xsens Technologies BV products */
174         { USB_DEVICE(0x0403, 0xd388) }, /* Serial interface */
175         { USB_DEVICE(0x0403, 0xd389) }, /* Serial interface */
176         { USB_DEVICE(0x0403, 0xd38a) }, /* Serial interface */
177         { USB_DEVICE(0x0403, 0xd38b) }, /* Serial interface */
178         { USB_DEVICE(0x0403, 0xd38c) }, /* Serial interface */
179         { USB_DEVICE(0x0403, 0xd38d) }, /* Serial interface */
180         { USB_DEVICE(0x0403, 0xd38e) }, /* Serial interface */
181         { USB_DEVICE(0x0403, 0xd38f) }, /* Serial interface */
182
183         /* Eurami Group products */
184         { USB_DEVICE(0x0403, 0xd678) }, /* Gamma Scout Online */
185
186         /* Westrex International products */
187         { USB_DEVICE(0x0403, 0xdc00) }, /* Model 777 */
188         { USB_DEVICE(0x0403, 0xdc01) }, /* Model 8900F */
189
190         /* ACG Identification GmbH products */
191         { USB_DEVICE(0x0403, 0xdd20) }, /* HF Dual ISO Reader (RFID) */
192
193         /* Artemis products */
194         { USB_DEVICE(0x0403, 0xdf28) }, /* CCD camera */
195
196         /* ATIK Instruments products */
197         { USB_DEVICE(0x0403, 0xdf30) }, /* ATK-16 Grayscale Camera */
198         { USB_DEVICE(0x0403, 0xdf31) }, /* ATK-16HR Grayscale Camera */
199         { USB_DEVICE(0x0403, 0xdf32) }, /* ATK-16C Colour Camera */
200         { USB_DEVICE(0x0403, 0xdf33) }, /* ATK-16HRC Colour Camera */
201
202         /* Yost Engineering, Inc. products */
203         { USB_DEVICE(0x0403, 0xe050) }, /* ServoCenter3.1 USB */
204
205         /* EVER Sp. products */
206         { USB_DEVICE(0x0403, 0xe520) }, /* Eco Pro UPS */
207
208         /* Active Robots products */
209         { USB_DEVICE(0x0403, 0xe548) }, /* Active Robots comms board */
210
211         /* Pyramid Computer GmbH products */
212         { USB_DEVICE(0x0403, 0xe6c8) }, /* Pyramid Appliance Display */
213
214         /* Gude Analog- und Digitalsysteme GmbH products */
215         { USB_DEVICE(0x0403, 0xe808) }, /* USB to serial */
216         { USB_DEVICE(0x0403, 0xe809) }, /* USB to serial */
217         { USB_DEVICE(0x0403, 0xe80a) }, /* USB to serial */
218         { USB_DEVICE(0x0403, 0xe80b) }, /* USB to serial */
219         { USB_DEVICE(0x0403, 0xe80c) }, /* USB to serial */
220         { USB_DEVICE(0x0403, 0xe80d) }, /* USB to serial */
221         { USB_DEVICE(0x0403, 0xe80e) }, /* USB to serial */
222         { USB_DEVICE(0x0403, 0xe80f) }, /* USB to serial */
223         { USB_DEVICE(0x0403, 0xe888) }, /* Expert ISDN Control USB */
224         { USB_DEVICE(0x0403, 0xe889) }, /* USB-RS232 OptoBridge */
225         { USB_DEVICE(0x0403, 0xe88a) }, /* Expert mouseCLOCK USB II */
226         { USB_DEVICE(0x0403, 0xe88b) }, /* Precision Clock MSF USB */
227         { USB_DEVICE(0x0403, 0xe88c) }, /* Expert mouseCLOCK USB II HBG */
228         { USB_DEVICE(0x0403, 0xe88d) }, /* USB to serial */
229         { USB_DEVICE(0x0403, 0xe88e) }, /* USB to serial */
230         { USB_DEVICE(0x0403, 0xe88f) }, /* USB to serial */
231
232         /* Eclo, Lda. products */
233         { USB_DEVICE(0x0403, 0xea90) }, /* COM to 1-Wire USB adaptor */
234
235         /* Coastal ChipWorks products */
236         { USB_DEVICE(0x0403, 0xebe0) }, /* TNC-X USB to packet-radio adapter */
237
238         /* Teratronik products */
239         { USB_DEVICE(0x0403, 0xec88) }, /* Teratronik VCP device */
240
241         /* MaxStream products */
242         { USB_DEVICE(0x0403, 0xee18) }, /* PKG-U RF modem */
243
244         /* microHAM products */
245         { USB_DEVICE(0x0403, 0xeee8) }, /* USB-KW interface */
246         { USB_DEVICE(0x0403, 0xeee9) }, /* USB-YS interface */
247         { USB_DEVICE(0x0403, 0xeeea) }, /* USB-Y6 interface */
248         { USB_DEVICE(0x0403, 0xeeeb) }, /* USB-Y8 interface */
249         { USB_DEVICE(0x0403, 0xeeec) }, /* USB-IC interface */
250         { USB_DEVICE(0x0403, 0xeeed) }, /* USB-DB9 interface */
251         { USB_DEVICE(0x0403, 0xeeee) }, /* USB-RS232 interface */
252         { USB_DEVICE(0x0403, 0xeeef) }, /* USB-Y9 interface */
253
254         /* ELV products */
255         { USB_DEVICE(0x0403, 0xf06e) }, /* ALC 8500 Expert */
256         { USB_DEVICE(0x0403, 0xf06f) }, /* FHZ 1000 PC */
257
258         /* Perle Systems products */
259         { USB_DEVICE(0x0403, 0xf0c0) }, /* UltraPort USB */
260
261         /* ACT Solutions products */
262         { USB_DEVICE(0x0403, 0xf2d0) }, /* HomePro ZWave */
263
264         /* 4n-galaxy.de products */
265         { USB_DEVICE(0x0403, 0xf3c0) }, /* Galaxy USB to serial */
266         { USB_DEVICE(0x0403, 0xf3c1) }, /* Galaxy USB to serial */
267
268         /* Linx Technologies products */
269         { USB_DEVICE(0x0403, 0xf448) }, /* Linx SDM-USB-QS-S */
270         { USB_DEVICE(0x0403, 0xf449) }, /* Linx Master Development 2.0 */
271         { USB_DEVICE(0x0403, 0xf44a) }, /* Linx USB to serial */
272         { USB_DEVICE(0x0403, 0xf44b) }, /* Linx USB to serial */
273         { USB_DEVICE(0x0403, 0xf44c) }, /* Linx USB to serial */
274
275         /* Suunto Oy products */
276         { USB_DEVICE(0x0403, 0xf680) }, /* Suunto Sports instrument */
277
278         /* USB-UIRT */
279         { USB_DEVICE(0x0403, 0xf850) }, /* USB-UIRT */
280
281         /* CCS Inc. products */
282         { USB_DEVICE(0x0403, 0xf9d0) }, /* ICD-U20 */
283         { USB_DEVICE(0x0403, 0xf9d1) }, /* ICD-U40 */
284         { USB_DEVICE(0x0403, 0xf9d2) }, /* MACH-X */
285
286         /* Matrix Orbital LCD displays */
287         { USB_DEVICE(0x0403, 0xfa00) }, /* USB Serial */
288         { USB_DEVICE(0x0403, 0xfa01) }, /* MX2 or MX3 LCD */
289         { USB_DEVICE(0x0403, 0xfa02) }, /* MX4 or MX5 LCD */
290         { USB_DEVICE(0x0403, 0xfa03) }, /* LK202-24 LCD */
291         { USB_DEVICE(0x0403, 0xfa04) }, /* LK204-24 LCD */
292         { USB_DEVICE(0x0403, 0xfa05) }, /* USB Serial */
293         { USB_DEVICE(0x0403, 0xfa06) }, /* USB Serial */
294
295         /* Home Electronics products */
296         { USB_DEVICE(0x0403, 0xfa78) }, /* Tira-1 */
297
298         /* PCDJ products */
299         { USB_DEVICE(0x0403, 0xfa88) }, /* DAC-2 */
300
301         /* Inside.fr products */
302         { USB_DEVICE(0x0403, 0xfad0) }, /* Accesso contactless reader */
303
304         /* Thorlabs GmbH products */
305         { USB_DEVICE(0x0403, 0xfaf0) }, /* Motors controller */
306
307         /* ELV products */
308         { USB_DEVICE(0x0403, 0xfb58) }, /* UR 100 */
309         { USB_DEVICE(0x0403, 0xfb5a) }, /* UM 100 */
310         { USB_DEVICE(0x0403, 0xfb5b) }, /* UO 100 */
311
312         /* Crystalfontz products */
313         { USB_DEVICE(0x0403, 0xfc08) }, /* CFA-632 LCD */
314         { USB_DEVICE(0x0403, 0xfc09) }, /* CFA-634 LCD */
315         { USB_DEVICE(0x0403, 0xfc0a) }, /* CFA-547 LCD */
316         { USB_DEVICE(0x0403, 0xfc0b) }, /* CFA-633 LCD */
317         { USB_DEVICE(0x0403, 0xfc0c) }, /* CFA-631 LCD */
318         { USB_DEVICE(0x0403, 0xfc0d) }, /* CFA-635 LCD */
319         { USB_DEVICE(0x0403, 0xfc0e) }, /* CFA-640 LCD */
320         { USB_DEVICE(0x0403, 0xfc0f) }, /* CFA-642 LCD */
321
322         /* IRTrans GmbH products */
323         { USB_DEVICE(0x0403, 0xfc60) }, /* Irtrans device */
324
325         /* Sony Ericsson products */
326         { USB_DEVICE(0x0403, 0xfc82) }, /* DSS-20 SyncStation */
327
328         /* RM Michaelides Software & Elektronik GmbH products */
329         { USB_DEVICE(0x0403, 0xfd60) }, /* CANview USB */
330
331         /* Video Networks Limited / Homechoice products */
332         { USB_DEVICE(0x0403, 0xfe38) }, /* Homechoice broadband modem */
333
334         /* AlphaMicro Components products */
335         { USB_DEVICE(0x0403, 0xff00) }, /* AMC-232USB01 */
336
337         /* Thought Technology Ltd. products */
338         { USB_DEVICE(0x0403, 0xff20) }, /* TT-USB */
339
340         /* IBS elektronik products */
341         { USB_DEVICE(0x0403, 0xff38) }, /* US485 interface */
342         { USB_DEVICE(0x0403, 0xff39) }, /* PIC-Programmer */
343         { USB_DEVICE(0x0403, 0xff3a) }, /* PCMCIA SRAM-cards reader */
344         { USB_DEVICE(0x0403, 0xff3b) }, /* Particel counter PK1 */
345         { USB_DEVICE(0x0403, 0xff3c) }, /* RS232 - Monitor */
346         { USB_DEVICE(0x0403, 0xff3d) }, /* APP 70 dust monitoring */
347         { USB_DEVICE(0x0403, 0xff3e) }, /* PEDO-Modem */
348         { USB_DEVICE(0x0403, 0xff3f) }, /* Future device */
349
350         /* Lawicel products */
351         { USB_DEVICE(0x0403, 0xffa8) }, /* CANUSB device */
352
353         /* Melco, Inc products */
354         { USB_DEVICE(0x0411, 0x00b3) }, /* PC-OP-RS1 RemoteStation */
355
356         /* B&B Electronics products */
357         { USB_DEVICE(0x0856, 0xac01) }, /* USOTL4 */
358         { USB_DEVICE(0x0856, 0xac02) }, /* USTL4 */
359         { USB_DEVICE(0x0856, 0xac03) }, /* USO9ML2 */
360         { USB_DEVICE(0x0856, 0xac11) }, /* USOPTL4 */
361         { USB_DEVICE(0x0856, 0xac12) }, /* USPTL4 */
362         { USB_DEVICE(0x0856, 0xac16) }, /* USO9ML2DR-2 */
363         { USB_DEVICE(0x0856, 0xac17) }, /* USO9ML2DR */
364         { USB_DEVICE(0x0856, 0xac18) }, /* USOPTL4DR-2 */
365         { USB_DEVICE(0x0856, 0xac19) }, /* USOPTL4DR */
366         { USB_DEVICE(0x0856, 0xac25) }, /* 485USB9F-2W */
367         { USB_DEVICE(0x0856, 0xac26) }, /* 485USB9F-4W */
368         { USB_DEVICE(0x0856, 0xac27) }, /* 232USB9M */
369
370         /* Interpid Control Systems products */
371         { USB_DEVICE(0x093c, 0x0601) }, /* ValueCAN */
372         { USB_DEVICE(0x093c, 0x0701) }, /* NeoVI Blue */
373         
374         /* ID TECH products */
375         { USB_DEVICE(0x0acd, 0x0300) }, /* USB to serial adapter */
376
377         /* Omnidirectional Control Technology products */
378         { USB_DEVICE(0x0b39, 0x0421) }, /* USB to serial */
379
380         /* Icom, Inc. products */
381         { USB_DEVICE(0x0c26, 0x0004) }, /* ID-1 */
382         { USB_DEVICE(0x0c26, 0x0009) }, /* ID-RP2C service 1 */
383         { USB_DEVICE(0x0c26, 0x000a) }, /* ID-RP2C service 2 */
384         { USB_DEVICE(0x0c26, 0x000b) }, /* ID-RP2D */
385         { USB_DEVICE(0x0c26, 0x000c) }, /* ID-RP2V service T */
386         { USB_DEVICE(0x0c26, 0x000d) }, /* ID-RP2V service R */
387         { USB_DEVICE(0x0c26, 0x0011) }, /* ID-RP4000V service R */
388         { USB_DEVICE(0x0c26, 0x0011) }, /* ID-RP4000V service T */
389         { USB_DEVICE(0x0c26, 0x0012) }, /* ID-RP2000V service T */
390         { USB_DEVICE(0x0c26, 0x0013) }, /* ID-RP2000V service R */
391
392         /* Sealevel products */
393         { USB_DEVICE(0x0c52, 0X2811) }, /* SeaLINK+8/232 (2801) Port 1 */
394         { USB_DEVICE(0x0c52, 0X2812) }, /* SeaLINK+8/485 (2802) Port 1 */
395         { USB_DEVICE(0x0c52, 0X2813) }, /* SeaLINK+8 (2803) Port 1 */
396         { USB_DEVICE(0x0c52, 0X2821) }, /* SeaLINK+8/232 (2801) Port 2 */
397         { USB_DEVICE(0x0c52, 0X2822) }, /* SeaLINK+8/485 (2802) Port 2 */
398         { USB_DEVICE(0x0c52, 0X2823) }, /* SeaLINK+8 (2803) Port 2 */
399         { USB_DEVICE(0x0c52, 0X2831) }, /* SeaLINK+8/232 (2801) Port 3 */
400         { USB_DEVICE(0x0c52, 0X2832) }, /* SeaLINK+8/485 (2802) Port 3 */
401         { USB_DEVICE(0x0c52, 0X2833) }, /* SeaLINK+8 (2803) Port 3 */
402         { USB_DEVICE(0x0c52, 0X2841) }, /* SeaLINK+8/232 (2801) Port 4 */
403         { USB_DEVICE(0x0c52, 0X2842) }, /* SeaLINK+8/485 (2802) Port 4 */
404         { USB_DEVICE(0x0c52, 0X2843) }, /* SeaLINK+8 (2803) Port 4 */
405         { USB_DEVICE(0x0c52, 0X2851) }, /* SeaLINK+8/232 (2801) Port 5 */
406         { USB_DEVICE(0x0c52, 0X2852) }, /* SeaLINK+8/485 (2802) Port 5 */
407         { USB_DEVICE(0x0c52, 0X2853) }, /* SeaLINK+8 (2803) Port 5 */
408         { USB_DEVICE(0x0c52, 0X2861) }, /* SeaLINK+8/232 (2801) Port 6 */
409         { USB_DEVICE(0x0c52, 0X2862) }, /* SeaLINK+8/485 (2802) Port 6 */
410         { USB_DEVICE(0x0c52, 0X2863) }, /* SeaLINK+8 (2803) Port 6 */
411         { USB_DEVICE(0x0c52, 0X2871) }, /* SeaLINK+8/232 (2801) Port 7 */
412         { USB_DEVICE(0x0c52, 0X2872) }, /* SeaLINK+8/485 (2802) Port 7 */
413         { USB_DEVICE(0x0c52, 0X2873) }, /* SeaLINK+8 (2803) Port 7 */
414         { USB_DEVICE(0x0c52, 0X2881) }, /* SeaLINK+8/232 (2801) Port 8 */
415         { USB_DEVICE(0x0c52, 0X2882) }, /* SeaLINK+8/485 (2802) Port 8 */
416         { USB_DEVICE(0x0c52, 0X2883) }, /* SeaLINK+8 (2803) Port 8 */
417         { USB_DEVICE(0x0c52, 0x2101) }, /* SeaLINK+232 (2101/2105) */
418         { USB_DEVICE(0x0c52, 0x2102) }, /* SeaLINK+485 (2102) */
419         { USB_DEVICE(0x0c52, 0x2103) }, /* SeaLINK+232I (2103) */
420         { USB_DEVICE(0x0c52, 0x2104) }, /* SeaLINK+485I (2104) */
421         { USB_DEVICE(0x0c52, 0x2211) }, /* SeaPORT+2/232 (2201) Port 1 */
422         { USB_DEVICE(0x0c52, 0x2212) }, /* SeaPORT+2/485 (2202) Port 1 */
423         { USB_DEVICE(0x0c52, 0x2213) }, /* SeaPORT+2 (2203) Port 1 */
424         { USB_DEVICE(0x0c52, 0x2221) }, /* SeaPORT+2/232 (2201) Port 2 */
425         { USB_DEVICE(0x0c52, 0x2222) }, /* SeaPORT+2/485 (2202) Port 2 */
426         { USB_DEVICE(0x0c52, 0x2223) }, /* SeaPORT+2 (2203) Port 2 */
427         { USB_DEVICE(0x0c52, 0x2411) }, /* SeaPORT+4/232 (2401) Port 1 */
428         { USB_DEVICE(0x0c52, 0x2412) }, /* SeaPORT+4/485 (2402) Port 1 */
429         { USB_DEVICE(0x0c52, 0x2413) }, /* SeaPORT+4 (2403) Port 1 */
430         { USB_DEVICE(0x0c52, 0x2421) }, /* SeaPORT+4/232 (2401) Port 2 */
431         { USB_DEVICE(0x0c52, 0x2422) }, /* SeaPORT+4/485 (2402) Port 2 */
432         { USB_DEVICE(0x0c52, 0x2423) }, /* SeaPORT+4 (2403) Port 2 */
433         { USB_DEVICE(0x0c52, 0x2431) }, /* SeaPORT+4/232 (2401) Port 3 */
434         { USB_DEVICE(0x0c52, 0x2432) }, /* SeaPORT+4/485 (2402) Port 3 */
435         { USB_DEVICE(0x0c52, 0x2433) }, /* SeaPORT+4 (2403) Port 3 */
436         { USB_DEVICE(0x0c52, 0x2441) }, /* SeaPORT+4/232 (2401) Port 4 */
437         { USB_DEVICE(0x0c52, 0x2442) }, /* SeaPORT+4/485 (2402) Port 4 */
438         { USB_DEVICE(0x0c52, 0x2443) }, /* SeaPORT+4 (2403) Port 4 */
439         { USB_DEVICE(0x0c52, 0x9020) }, /* SeaLINK+422 (2106) */
440
441         /* Posiflex Technologies products */
442         { USB_DEVICE(0x0d3a, 0x0300) }, /* PP7000 series printer */
443         { USB_DEVICE(0x0d3a, 0x0400) }, /* PP7000 series printer */
444
445         /* Kobil Systems products */
446         { USB_DEVICE(0x0d46, 0x2020) }, /* Konverter for B1 */
447         { USB_DEVICE(0x0d46, 0x2021) }, /* Konverter for KAAN */
448
449         /* Falcom Wireless Communications products */
450         { USB_DEVICE(0x0f94, 0x0001) }, /* Twist USB GPRS modem */
451         { USB_DEVICE(0x0f94, 0x0005) }, /* Samba USB GPRS modem */
452
453         /* Thurlby Thandar Instruments products */
454         { USB_DEVICE(0x103e, 0x03e8) }, /* QL355P power supply */
455
456         /* InterBiometrics products */
457         { USB_DEVICE(0x1209, 0x1002) }, /* IO Board */
458         { USB_DEVICE(0x1209, 0x1006) }, /* Mini IO Board */
459
460         /* Testo AG products */
461         { USB_DEVICE(0x128d, 0x0001) }, /* 175/177 USB interface */
462         { USB_DEVICE(0x128d, 0x0002) }, /* 330 USB interface */
463         { USB_DEVICE(0x128d, 0x0003) }, /* 435/635/735 USB interface */
464         { USB_DEVICE(0x128d, 0x0004) }, /* 845 USB interface */
465         { USB_DEVICE(0x128d, 0x0005) }, /* Service adapter */
466         { USB_DEVICE(0x128d, 0x0006) }, /* 580 USB interface */
467         { USB_DEVICE(0x128d, 0x0007) }, /* 174 USB interface */
468         { USB_DEVICE(0x128d, 0x0009) }, /* 556/560 USB interface */
469         { USB_DEVICE(0x128d, 0x000a) }, /* USB adapter */
470         { USB_DEVICE(0x128d, 0xf001) }, /* USB to serial converter */
471
472         /* Mobility products */
473         { USB_DEVICE(0x1342, 0x0202) }, /* EasiDock 200 serial port */
474
475         /* Papouch s.r.o. products */
476         { USB_DEVICE(0x5050, 0x0100) }, /* SB485 USB-485/422 Converter */
477         { USB_DEVICE(0x5050, 0x0101) }, /* AP485 USB-RS485 Converter */
478         { USB_DEVICE(0x5050, 0x0102) }, /* SB422 USB-RS422 Converter */
479         { USB_DEVICE(0x5050, 0x0103) }, /* SB485 USB-485/422 Converter */
480         { USB_DEVICE(0x5050, 0x0104) }, /* AP485 USB-RS485 Converter */
481         { USB_DEVICE(0x5050, 0x0105) }, /* SB422 USB-RS422 Converter */
482         { USB_DEVICE(0x5050, 0x0106) }, /* SB485S USB-485/422 Converter */
483         { USB_DEVICE(0x5050, 0x0107) }, /* SB485C USB-485/422 Converter */
484         { USB_DEVICE(0x5050, 0x0200) }, /* USB Device */
485         { USB_DEVICE(0x5050, 0x0300) }, /* LEC USB Converter */
486         { USB_DEVICE(0x5050, 0x0301) }, /* SB232 USB-RS232 Converter */
487         { USB_DEVICE(0x5050, 0x0400) }, /* TMU Thermometer */
488         { USB_DEVICE(0x5050, 0x0500) }, /* IRAmp Duplex */
489         { USB_DEVICE(0x5050, 0x0700) }, /* DRAK5 */
490         { USB_DEVICE(0x5050, 0x0800) }, /* QUIDO USB 8/8 */
491         { USB_DEVICE(0x5050, 0x0900) }, /* QUIDO USB 4/4 */
492         { USB_DEVICE(0x5050, 0x0A00) }, /* QUIDO USB 2/2 */
493         { USB_DEVICE(0x5050, 0x0B00) }, /* QUIDO USB 10/1 */
494         { USB_DEVICE(0x5050, 0x0C00) }, /* QUIDO USB 30/3 */
495         { USB_DEVICE(0x5050, 0x0D00) }, /* QUIDO USB 60(100)/3 */
496         { USB_DEVICE(0x5050, 0x0E00) }, /* QUIDO USB 2/16 */
497         { USB_DEVICE(0x5050, 0x0F00) }, /* QUIDO USB 3/32 */
498         { USB_DEVICE(0x5050, 0x1000) }, /* DRAK6 USB */
499         { USB_DEVICE(0x5050, 0x8000) }, /* UPS-USB Stavovy Adapter */
500         { USB_DEVICE(0x5050, 0x8001) }, /* MU Controller */
501         { USB_DEVICE(0x5050, 0x8002) }, /* SimuKey */
502         { USB_DEVICE(0x5050, 0x8003) }, /* AD4USB */
503         { USB_DEVICE(0x5050, 0x8004) }, /* GOLIATH MUX */
504         { USB_DEVICE(0x5050, 0x8005) }, /* GOLIATH MSR */
505
506         /* Evolution Robotics, Inc. products */
507         { USB_DEVICE(0xdeee, 0x0300) }, /* ER1 Control Module */
508         { USB_DEVICE(0xdeee, 0x0302) }, /* RCM4 interface */
509         { USB_DEVICE(0xdeee, 0x0303) }, /* RCM4 interface */
510 };
511
512 static int
513 uftdi_match(device_t self)
514 {
515         struct usb_attach_arg *uaa = device_get_ivars(self);
516
517         if (uaa->iface == NULL)
518                 return (UMATCH_NONE);
519
520         if (usb_lookup(uftdi_devs, uaa->vendor, uaa->product) != NULL)
521                 return (UMATCH_VENDOR_IFACESUBCLASS);
522
523         return (UMATCH_NONE);
524 }
525
526 static int
527 uftdi_attach(device_t self)
528 {
529         struct uftdi_softc *sc = device_get_softc(self);
530         struct usb_attach_arg *uaa = device_get_ivars(self);
531         usbd_device_handle dev = uaa->device;
532         usbd_interface_handle iface;
533         usb_interface_descriptor_t *id;
534         usb_endpoint_descriptor_t *ed;
535         int i;
536         struct ucom_softc *ucom = &sc->sc_ucom;
537         DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc));
538
539         ucom->sc_dev = self;
540         ucom->sc_udev = dev;
541
542         iface = uaa->iface;
543         id = usbd_get_interface_descriptor(iface);
544         ucom->sc_iface = iface;
545
546         if (uaa->release < 0x0200) {
547                 sc->sc_type = UFTDI_TYPE_SIO;
548                 sc->sc_hdrlen = 1;
549         } else {
550                 sc->sc_type = UFTDI_TYPE_8U232AM;
551                 sc->sc_hdrlen = 0;
552         }
553
554         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
555
556         for (i = 0; i < id->bNumEndpoints; i++) {
557                 int addr, dir, attr;
558                 ed = usbd_interface2endpoint_descriptor(iface, i);
559                 if (ed == NULL) {
560                         device_printf(ucom->sc_dev,
561                             "could not read endpoint descriptor\n");
562                         goto bad;
563                 }
564
565                 addr = ed->bEndpointAddress;
566                 dir = UE_GET_DIR(ed->bEndpointAddress);
567                 attr = ed->bmAttributes & UE_XFERTYPE;
568                 if (dir == UE_DIR_IN && attr == UE_BULK)
569                         ucom->sc_bulkin_no = addr;
570                 else if (dir == UE_DIR_OUT && attr == UE_BULK)
571                         ucom->sc_bulkout_no = addr;
572                 else {
573                         device_printf(ucom->sc_dev, "unexpected endpoint\n");
574                         goto bad;
575                 }
576         }
577         if (ucom->sc_bulkin_no == -1) {
578                 device_printf(ucom->sc_dev, "Could not find data bulk in\n");
579                 goto bad;
580         }
581         if (ucom->sc_bulkout_no == -1) {
582                 device_printf(ucom->sc_dev, "Could not find data bulk out\n");
583                 goto bad;
584         }
585         ucom->sc_parent  = sc;
586         ucom->sc_portno = FTDI_PIT_SIOA + id->bInterfaceNumber;
587         /* bulkin, bulkout set above */
588
589         ucom->sc_ibufsize = UFTDIIBUFSIZE;
590         ucom->sc_obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
591         ucom->sc_ibufsizepad = UFTDIIBUFSIZE;
592         ucom->sc_opkthdrlen = sc->sc_hdrlen;
593
594
595         ucom->sc_callback = &uftdi_callback;
596 #if 0
597         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
598           ucom->sc_dev);
599 #endif
600         DPRINTF(("uftdi: in=0x%x out=0x%x\n", ucom->sc_bulkin_no, ucom->sc_bulkout_no));
601         ucom_attach(&sc->sc_ucom);
602         return 0;
603
604 bad:
605         DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
606         ucom->sc_dying = 1;
607         return ENXIO;
608 }
609 #if 0
610 int
611 uftdi_activate(device_t self, enum devact act)
612 {
613         struct uftdi_softc *sc = (struct uftdi_softc *)self;
614         int rv = 0;
615
616         switch (act) {
617         case DVACT_ACTIVATE:
618                 return (EOPNOTSUPP);
619
620         case DVACT_DEACTIVATE:
621                 if (sc->sc_subdev != NULL)
622                         rv = config_deactivate(sc->sc_subdev);
623                 sc->sc_ucom.sc_dying = 1;
624                 break;
625         }
626         return (rv);
627 }
628 #endif
629
630 static int
631 uftdi_detach(device_t self)
632 {
633         struct uftdi_softc *sc = device_get_softc(self);
634
635         int rv = 0;
636
637         DPRINTF(("uftdi_detach: sc=%p\n", sc));
638         sc->sc_ucom.sc_dying = 1;
639         rv = ucom_detach(&sc->sc_ucom);
640
641         return rv;
642 }
643
644 static int
645 uftdi_open(void *vsc, int portno)
646 {
647         struct uftdi_softc *sc = vsc;
648         struct ucom_softc *ucom = &sc->sc_ucom;
649         usb_device_request_t req;
650         usbd_status err;
651         struct termios t;
652
653         DPRINTF(("uftdi_open: sc=%p\n", sc));
654
655         if (ucom->sc_dying)
656                 return (EIO);
657
658         /* Perform a full reset on the device */
659         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
660         req.bRequest = FTDI_SIO_RESET;
661         USETW(req.wValue, FTDI_SIO_RESET_SIO);
662         USETW(req.wIndex, portno);
663         USETW(req.wLength, 0);
664         err = usbd_do_request(ucom->sc_udev, &req, NULL);
665         if (err)
666                 return (EIO);
667
668         /* Set 9600 baud, 2 stop bits, no parity, 8 bits */
669         t.c_ospeed = 9600;
670         t.c_cflag = CSTOPB | CS8;
671         (void)uftdi_param(sc, portno, &t);
672
673         /* Turn on RTS/CTS flow control */
674         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
675         req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
676         USETW(req.wValue, 0);
677         USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
678         USETW(req.wLength, 0);
679         err = usbd_do_request(ucom->sc_udev, &req, NULL);
680         if (err)
681                 return (EIO);
682
683         return (0);
684 }
685
686 static void
687 uftdi_read(void *vsc, int portno, u_char **ptr, u_int32_t *count)
688 {
689         struct uftdi_softc *sc = vsc;
690         u_char msr, lsr;
691
692         DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
693                      *count));
694
695         msr = FTDI_GET_MSR(*ptr);
696         lsr = FTDI_GET_LSR(*ptr);
697
698 #ifdef USB_DEBUG
699         if (*count != 2)
700                 DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
701                             "0x%02x\n", sc, portno, *count, (*ptr)[2]));
702 #endif
703
704         if (sc->sc_msr != msr ||
705             (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
706                 DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
707                          "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
708                          lsr, sc->sc_lsr));
709                 sc->sc_msr = msr;
710                 sc->sc_lsr = lsr;
711                 ucom_status_change(&sc->sc_ucom);
712         }
713
714         /* Pick up status and adjust data part. */
715         *ptr += 2;
716         *count -= 2;
717 }
718
719 static void
720 uftdi_write(void *vsc, int portno, u_char *to, u_char *from, u_int32_t *count)
721 {
722         struct uftdi_softc *sc = vsc;
723
724         DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
725                      vsc, portno, *count, from[0]));
726
727         /* Make length tag and copy data */
728         if (sc->sc_hdrlen > 0)
729                 *to = FTDI_OUT_TAG(*count, portno);
730
731         memcpy(to + sc->sc_hdrlen, from, *count);
732         *count += sc->sc_hdrlen;
733 }
734
735 static void
736 uftdi_set(void *vsc, int portno, int reg, int onoff)
737 {
738         struct uftdi_softc *sc = vsc;
739         struct ucom_softc *ucom = vsc;
740         usb_device_request_t req;
741         int ctl;
742
743         DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
744                  reg, onoff));
745
746         switch (reg) {
747         case UCOM_SET_DTR:
748                 ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
749                 break;
750         case UCOM_SET_RTS:
751                 ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
752                 break;
753         case UCOM_SET_BREAK:
754                 uftdi_break(sc, portno, onoff);
755                 return;
756         default:
757                 return;
758         }
759         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
760         req.bRequest = FTDI_SIO_MODEM_CTRL;
761         USETW(req.wValue, ctl);
762         USETW(req.wIndex, portno);
763         USETW(req.wLength, 0);
764         DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
765                     "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
766                     UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
767         (void)usbd_do_request(ucom->sc_udev, &req, NULL);
768 }
769
770 static int
771 uftdi_param(void *vsc, int portno, struct termios *t)
772 {
773         struct uftdi_softc *sc = vsc;
774         struct ucom_softc *ucom = &sc->sc_ucom;
775         usb_device_request_t req;
776         usbd_status err;
777         int rate=0, data, flow;
778
779         DPRINTF(("uftdi_param: sc=%p\n", sc));
780
781         if (ucom->sc_dying)
782                 return (EIO);
783
784         switch (sc->sc_type) {
785         case UFTDI_TYPE_SIO:
786                 switch (t->c_ospeed) {
787                 case 300: rate = ftdi_sio_b300; break;
788                 case 600: rate = ftdi_sio_b600; break;
789                 case 1200: rate = ftdi_sio_b1200; break;
790                 case 2400: rate = ftdi_sio_b2400; break;
791                 case 4800: rate = ftdi_sio_b4800; break;
792                 case 9600: rate = ftdi_sio_b9600; break;
793                 case 19200: rate = ftdi_sio_b19200; break;
794                 case 38400: rate = ftdi_sio_b38400; break;
795                 case 57600: rate = ftdi_sio_b57600; break;
796                 case 115200: rate = ftdi_sio_b115200; break;
797                 default:
798                         return (EINVAL);
799                 }
800                 break;
801
802         case UFTDI_TYPE_8U232AM:
803                 if (uftdi_8u232am_getrate(t->c_ospeed, &rate) == -1)
804                         return (EINVAL);
805                 break;
806         }
807         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
808         req.bRequest = FTDI_SIO_SET_BAUD_RATE;
809         USETW(req.wValue, rate);
810         USETW(req.wIndex, portno);
811         USETW(req.wLength, 0);
812         DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
813                     "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
814                     UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
815         err = usbd_do_request(ucom->sc_udev, &req, NULL);
816         if (err)
817                 return (EIO);
818
819         if (ISSET(t->c_cflag, CSTOPB))
820                 data = FTDI_SIO_SET_DATA_STOP_BITS_2;
821         else
822                 data = FTDI_SIO_SET_DATA_STOP_BITS_1;
823         if (ISSET(t->c_cflag, PARENB)) {
824                 if (ISSET(t->c_cflag, PARODD))
825                         data |= FTDI_SIO_SET_DATA_PARITY_ODD;
826                 else
827                         data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
828         } else
829                 data |= FTDI_SIO_SET_DATA_PARITY_NONE;
830         switch (ISSET(t->c_cflag, CSIZE)) {
831         case CS5:
832                 data |= FTDI_SIO_SET_DATA_BITS(5);
833                 break;
834         case CS6:
835                 data |= FTDI_SIO_SET_DATA_BITS(6);
836                 break;
837         case CS7:
838                 data |= FTDI_SIO_SET_DATA_BITS(7);
839                 break;
840         case CS8:
841                 data |= FTDI_SIO_SET_DATA_BITS(8);
842                 break;
843         }
844         sc->last_lcr = data;
845
846         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
847         req.bRequest = FTDI_SIO_SET_DATA;
848         USETW(req.wValue, data);
849         USETW(req.wIndex, portno);
850         USETW(req.wLength, 0);
851         DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
852                     "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
853                     UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
854         err = usbd_do_request(ucom->sc_udev, &req, NULL);
855         if (err)
856                 return (EIO);
857
858         if (ISSET(t->c_cflag, CRTSCTS)) {
859                 flow = FTDI_SIO_RTS_CTS_HS;
860                 USETW(req.wValue, 0);
861         } else if (ISSET(t->c_iflag, IXON|IXOFF)) {
862                 flow = FTDI_SIO_XON_XOFF_HS;
863                 USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
864         } else {
865                 flow = FTDI_SIO_DISABLE_FLOW_CTRL;
866                 USETW(req.wValue, 0);
867         }
868         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
869         req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
870         USETW2(req.wIndex, flow, portno);
871         USETW(req.wLength, 0);
872         err = usbd_do_request(ucom->sc_udev, &req, NULL);
873         if (err)
874                 return (EIO);
875
876         return (0);
877 }
878
879 void
880 uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
881 {
882         struct uftdi_softc *sc = vsc;
883
884         DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
885                  sc->sc_msr, sc->sc_lsr));
886
887         if (msr != NULL)
888                 *msr = sc->sc_msr;
889         if (lsr != NULL)
890                 *lsr = sc->sc_lsr;
891 }
892
893 void
894 uftdi_break(void *vsc, int portno, int onoff)
895 {
896         struct uftdi_softc *sc = vsc;
897         struct ucom_softc *ucom = vsc;
898
899         usb_device_request_t req;
900         int data;
901
902         DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
903                   onoff));
904
905         if (onoff) {
906                 data = sc->last_lcr | FTDI_SIO_SET_BREAK;
907         } else {
908                 data = sc->last_lcr;
909         }
910
911         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
912         req.bRequest = FTDI_SIO_SET_DATA;
913         USETW(req.wValue, data);
914         USETW(req.wIndex, portno);
915         USETW(req.wLength, 0);
916         (void)usbd_do_request(ucom->sc_udev, &req, NULL);
917 }
918
919 static int
920 uftdi_8u232am_getrate(speed_t speed, int *rate)
921 {
922         /* Table of the nearest even powers-of-2 for values 0..15. */
923         static const unsigned char roundoff[16] = {
924                 0, 2, 2, 4,  4,  4,  8,  8,
925                 8, 8, 8, 8, 16, 16, 16, 16,
926         };
927
928         unsigned int d, freq;
929         int result;
930
931         if (speed <= 0)
932                 return (-1);
933
934         /* Special cases for 2M and 3M. */
935         if (speed >= 3000000 * 100 / 103 &&
936             speed <= 3000000 * 100 / 97) {
937                 result = 0;
938                 goto done;
939         }
940         if (speed >= 2000000 * 100 / 103 &&
941             speed <= 2000000 * 100 / 97) {
942                 result = 1;
943                 goto done;
944         }
945
946         d = (FTDI_8U232AM_FREQ << 4) / speed;
947         d = (d & ~15) + roundoff[d & 15];
948
949         if (d < FTDI_8U232AM_MIN_DIV)
950                 d = FTDI_8U232AM_MIN_DIV;
951         else if (d > FTDI_8U232AM_MAX_DIV)
952                 d = FTDI_8U232AM_MAX_DIV;
953
954         /* 
955          * Calculate the frequency needed for d to exactly divide down
956          * to our target speed, and check that the actual frequency is
957          * within 3% of this.
958          */
959         freq = speed * d;
960         if (freq < (quad_t)(FTDI_8U232AM_FREQ << 4) * 100 / 103 ||
961             freq > (quad_t)(FTDI_8U232AM_FREQ << 4) * 100 / 97)
962                 return (-1);
963
964         /* 
965          * Pack the divisor into the resultant value.  The lower
966          * 14-bits hold the integral part, while the upper 2 bits
967          * encode the fractional component: either 0, 0.5, 0.25, or
968          * 0.125.
969          */
970         result = d >> 4;
971         if (d & 8)
972                 result |= 0x4000;
973         else if (d & 4)
974                 result |= 0x8000;
975         else if (d & 2)
976                 result |= 0xc000;
977
978 done:
979         *rate = result;
980         return (0);
981 }
982 static device_method_t uftdi_methods[] = {
983         /* Device interface */
984         DEVMETHOD(device_probe, uftdi_match),
985         DEVMETHOD(device_attach, uftdi_attach),
986         DEVMETHOD(device_detach, uftdi_detach),
987
988         { 0, 0 }
989 };
990
991 static driver_t uftdi_driver = {
992         "ucom",
993         uftdi_methods,
994         sizeof (struct uftdi_softc)
995 };
996
997 DRIVER_MODULE(uftdi, uhub, uftdi_driver, ucom_devclass, usbd_driver_load, 0);
998 MODULE_DEPEND(uftdi, usb, 1, 1, 1);
999 MODULE_DEPEND(uftdi, ucom,UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);