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