]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/usb/serial/uplcom.c
Copy head to stable/8 as part of 8.0 Release cycle.
[FreeBSD/stable/8.git] / sys / dev / usb / serial / uplcom.c
1 /*      $NetBSD: uplcom.c,v 1.21 2001/11/13 06:24:56 lukem Exp $        */
2
3 #include <sys/cdefs.h>
4 __FBSDID("$FreeBSD$");
5
6 /*-
7  * Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 /*-
33  * Copyright (c) 2001 The NetBSD Foundation, Inc.
34  * All rights reserved.
35  *
36  * This code is derived from software contributed to The NetBSD Foundation
37  * by Ichiro FUKUHARA (ichiro@ichiro.org).
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  *    must display the following acknowledgement:
49  *        This product includes software developed by the NetBSD
50  *        Foundation, Inc. and its contributors.
51  * 4. Neither the name of The NetBSD Foundation nor the names of its
52  *    contributors may be used to endorse or promote products derived
53  *    from this software without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
56  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
57  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
59  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
60  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
61  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
62  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
64  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
65  * POSSIBILITY OF SUCH DAMAGE.
66  */
67
68 /*
69  * This driver supports several USB-to-RS232 serial adapters driven by
70  * Prolific PL-2303, PL-2303X and probably PL-2303HX USB-to-RS232
71  * bridge chip.  The adapters are sold under many different brand
72  * names.
73  *
74  * Datasheets are available at Prolific www site at
75  * http://www.prolific.com.tw.  The datasheets don't contain full
76  * programming information for the chip.
77  *
78  * PL-2303HX is probably programmed the same as PL-2303X.
79  *
80  * There are several differences between PL-2303 and PL-2303(H)X.
81  * PL-2303(H)X can do higher bitrate in bulk mode, has _probably_
82  * different command for controlling CRTSCTS and needs special
83  * sequence of commands for initialization which aren't also
84  * documented in the datasheet.
85  */
86
87 #include <sys/stdint.h>
88 #include <sys/stddef.h>
89 #include <sys/param.h>
90 #include <sys/queue.h>
91 #include <sys/types.h>
92 #include <sys/systm.h>
93 #include <sys/kernel.h>
94 #include <sys/bus.h>
95 #include <sys/linker_set.h>
96 #include <sys/module.h>
97 #include <sys/lock.h>
98 #include <sys/mutex.h>
99 #include <sys/condvar.h>
100 #include <sys/sysctl.h>
101 #include <sys/sx.h>
102 #include <sys/unistd.h>
103 #include <sys/callout.h>
104 #include <sys/malloc.h>
105 #include <sys/priv.h>
106
107 #include <dev/usb/usb.h>
108 #include <dev/usb/usbdi.h>
109 #include <dev/usb/usbdi_util.h>
110 #include <dev/usb/usb_cdc.h>
111 #include "usbdevs.h"
112
113 #define USB_DEBUG_VAR uplcom_debug
114 #include <dev/usb/usb_debug.h>
115 #include <dev/usb/usb_process.h>
116
117 #include <dev/usb/serial/usb_serial.h>
118
119 #if USB_DEBUG
120 static int uplcom_debug = 0;
121
122 SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom");
123 SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RW,
124     &uplcom_debug, 0, "Debug level");
125 #endif
126
127 #define UPLCOM_MODVER                   1       /* module version */
128
129 #define UPLCOM_CONFIG_INDEX             0
130 #define UPLCOM_IFACE_INDEX              0
131 #define UPLCOM_SECOND_IFACE_INDEX       1
132
133 #ifndef UPLCOM_INTR_INTERVAL
134 #define UPLCOM_INTR_INTERVAL            0       /* default */
135 #endif
136
137 #define UPLCOM_BULK_BUF_SIZE 1024       /* bytes */
138
139 #define UPLCOM_SET_REQUEST              0x01
140 #define UPLCOM_SET_CRTSCTS              0x41
141 #define UPLCOM_SET_CRTSCTS_PL2303X      0x61
142 #define RSAQ_STATUS_CTS                 0x80
143 #define RSAQ_STATUS_DSR                 0x02
144 #define RSAQ_STATUS_DCD                 0x01
145
146 #define TYPE_PL2303                     0
147 #define TYPE_PL2303X                    1
148
149 enum {
150         UPLCOM_BULK_DT_WR,
151         UPLCOM_BULK_DT_RD,
152         UPLCOM_INTR_DT_RD,
153         UPLCOM_N_TRANSFER,
154 };
155
156 struct uplcom_softc {
157         struct ucom_super_softc sc_super_ucom;
158         struct ucom_softc sc_ucom;
159
160         struct usb_xfer *sc_xfer[UPLCOM_N_TRANSFER];
161         struct usb_device *sc_udev;
162         struct mtx sc_mtx;
163
164         uint16_t sc_line;
165
166         uint8_t sc_lsr;                 /* local status register */
167         uint8_t sc_msr;                 /* uplcom status register */
168         uint8_t sc_chiptype;            /* type of chip */
169         uint8_t sc_ctrl_iface_no;
170         uint8_t sc_data_iface_no;
171         uint8_t sc_iface_index[2];
172 };
173
174 /* prototypes */
175
176 static usb_error_t uplcom_reset(struct uplcom_softc *, struct usb_device *);
177 static int      uplcom_pl2303x_init(struct usb_device *);
178 static void     uplcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
179 static void     uplcom_cfg_set_rts(struct ucom_softc *, uint8_t);
180 static void     uplcom_cfg_set_break(struct ucom_softc *, uint8_t);
181 static int      uplcom_pre_param(struct ucom_softc *, struct termios *);
182 static void     uplcom_cfg_param(struct ucom_softc *, struct termios *);
183 static void     uplcom_start_read(struct ucom_softc *);
184 static void     uplcom_stop_read(struct ucom_softc *);
185 static void     uplcom_start_write(struct ucom_softc *);
186 static void     uplcom_stop_write(struct ucom_softc *);
187 static void     uplcom_cfg_get_status(struct ucom_softc *, uint8_t *,
188                     uint8_t *);
189
190 static device_probe_t uplcom_probe;
191 static device_attach_t uplcom_attach;
192 static device_detach_t uplcom_detach;
193
194 static usb_callback_t uplcom_intr_callback;
195 static usb_callback_t uplcom_write_callback;
196 static usb_callback_t uplcom_read_callback;
197
198 static const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = {
199
200         [UPLCOM_BULK_DT_WR] = {
201                 .type = UE_BULK,
202                 .endpoint = UE_ADDR_ANY,
203                 .direction = UE_DIR_OUT,
204                 .bufsize = UPLCOM_BULK_BUF_SIZE,
205                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
206                 .callback = &uplcom_write_callback,
207                 .if_index = 0,
208         },
209
210         [UPLCOM_BULK_DT_RD] = {
211                 .type = UE_BULK,
212                 .endpoint = UE_ADDR_ANY,
213                 .direction = UE_DIR_IN,
214                 .bufsize = UPLCOM_BULK_BUF_SIZE,
215                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
216                 .callback = &uplcom_read_callback,
217                 .if_index = 0,
218         },
219
220         [UPLCOM_INTR_DT_RD] = {
221                 .type = UE_INTERRUPT,
222                 .endpoint = UE_ADDR_ANY,
223                 .direction = UE_DIR_IN,
224                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
225                 .bufsize = 0,   /* use wMaxPacketSize */
226                 .callback = &uplcom_intr_callback,
227                 .if_index = 1,
228         },
229 };
230
231 static struct ucom_callback uplcom_callback = {
232         .ucom_cfg_get_status = &uplcom_cfg_get_status,
233         .ucom_cfg_set_dtr = &uplcom_cfg_set_dtr,
234         .ucom_cfg_set_rts = &uplcom_cfg_set_rts,
235         .ucom_cfg_set_break = &uplcom_cfg_set_break,
236         .ucom_cfg_param = &uplcom_cfg_param,
237         .ucom_pre_param = &uplcom_pre_param,
238         .ucom_start_read = &uplcom_start_read,
239         .ucom_stop_read = &uplcom_stop_read,
240         .ucom_start_write = &uplcom_start_write,
241         .ucom_stop_write = &uplcom_stop_write,
242 };
243
244 #define USB_UPL(v,p,rl,rh,t)                            \
245   USB_VENDOR(v), USB_PRODUCT(p), USB_DEV_BCD_GTEQ(rl),  \
246   USB_DEV_BCD_LTEQ(rh), USB_DRIVER_INFO(t)
247
248 static const struct usb_device_id uplcom_devs[] = {
249         /* Belkin F5U257 */
250         {USB_UPL(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U257, 0, 0xFFFF, TYPE_PL2303X)},
251         /* I/O DATA USB-RSAQ */
252         {USB_UPL(USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ, 0, 0xFFFF, TYPE_PL2303)},
253         /* I/O DATA USB-RSAQ2 */
254         {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2, 0, 0xFFFF, TYPE_PL2303)},
255         /* I/O DATA USB-RSAQ3 */
256         {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3, 0, 0xFFFF, TYPE_PL2303X)},
257         /* PLANEX USB-RS232 URS-03 */
258         {USB_UPL(USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A, 0, 0xFFFF, TYPE_PL2303)},
259         /* TrendNet TU-S9 */
260         {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303, 0x0400, 0xFFFF, TYPE_PL2303X)},
261         /* ST Lab USB-SERIAL-4 */
262         {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303, 0x0300, 0x03FF, TYPE_PL2303X)},
263         /* IOGEAR/ATEN UC-232A (also ST Lab USB-SERIAL-1) */
264         {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303, 0, 0x02FF, TYPE_PL2303)},
265         /* TDK USB-PHS Adapter UHA6400 */
266         {USB_UPL(USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400, 0, 0xFFFF, TYPE_PL2303)},
267         /* RATOC REX-USB60 */
268         {USB_UPL(USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60, 0, 0xFFFF, TYPE_PL2303)},
269         /* ELECOM UC-SGT */
270         {USB_UPL(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT, 0, 0xFFFF, TYPE_PL2303)},
271         {USB_UPL(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0, 0, 0xFFFF, TYPE_PL2303)},
272         /* Sagem USB-Serial Controller */
273         {USB_UPL(USB_VENDOR_SAGEM, USB_PRODUCT_SAGEM_USBSERIAL, 0, 0xFFFF, TYPE_PL2303X)},
274         /* Sony Ericsson USB Cable */
275         {USB_UPL(USB_VENDOR_SONYERICSSON, USB_PRODUCT_SONYERICSSON_DCU10, 0, 0xFFFF, TYPE_PL2303)},
276         /* SOURCENEXT KeikaiDenwa 8 */
277         {USB_UPL(USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8, 0, 0xFFFF, TYPE_PL2303)},
278         /* SOURCENEXT KeikaiDenwa 8 with charger */
279         {USB_UPL(USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG, 0, 0, TYPE_PL2303)},
280         /* HAL Corporation Crossam2+USB */
281         {USB_UPL(USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001, 0, 0xFFFF, TYPE_PL2303)},
282         /* Sitecom USB to Serial */
283         {USB_UPL(USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_SERIAL, 0, 0xFFFF, TYPE_PL2303)},
284         /* Tripp-Lite U209-000-R */
285         {USB_UPL(USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209, 0, 0xFFFF, TYPE_PL2303X)},
286         {USB_UPL(USB_VENDOR_RADIOSHACK, USB_PRODUCT_RADIOSHACK_USBCABLE, 0, 0xFFFF, TYPE_PL2303)},
287         /* Prolific Pharos */
288         {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PHAROS, 0, 0xFFFF, TYPE_PL2303)},
289         /* Willcom W-SIM */
290         {USB_UPL(USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_WSIM, 0, 0xFFFF, TYPE_PL2303X)},
291         /* Mobile Action MA-620 Infrared Adapter */
292         {USB_UPL(USB_VENDOR_MOBILEACTION, USB_PRODUCT_MOBILEACTION_MA620, 0, 0xFFFF, TYPE_PL2303X)},
293
294 };
295
296 static device_method_t uplcom_methods[] = {
297         DEVMETHOD(device_probe, uplcom_probe),
298         DEVMETHOD(device_attach, uplcom_attach),
299         DEVMETHOD(device_detach, uplcom_detach),
300         {0, 0}
301 };
302
303 static devclass_t uplcom_devclass;
304
305 static driver_t uplcom_driver = {
306         .name = "uplcom",
307         .methods = uplcom_methods,
308         .size = sizeof(struct uplcom_softc),
309 };
310
311 DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0);
312 MODULE_DEPEND(uplcom, ucom, 1, 1, 1);
313 MODULE_DEPEND(uplcom, usb, 1, 1, 1);
314 MODULE_VERSION(uplcom, UPLCOM_MODVER);
315
316 static int
317 uplcom_probe(device_t dev)
318 {
319         struct usb_attach_arg *uaa = device_get_ivars(dev);
320
321         DPRINTFN(11, "\n");
322
323         if (uaa->usb_mode != USB_MODE_HOST) {
324                 return (ENXIO);
325         }
326         if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
327                 return (ENXIO);
328         }
329         if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
330                 return (ENXIO);
331         }
332         return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
333 }
334
335 static int
336 uplcom_attach(device_t dev)
337 {
338         struct usb_attach_arg *uaa = device_get_ivars(dev);
339         struct uplcom_softc *sc = device_get_softc(dev);
340         struct usb_interface *iface;
341         struct usb_interface_descriptor *id;
342         int error;
343
344         DPRINTFN(11, "\n");
345
346         device_set_usb_desc(dev);
347         mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF);
348
349         DPRINTF("sc = %p\n", sc);
350
351         sc->sc_chiptype = USB_GET_DRIVER_INFO(uaa);
352         sc->sc_udev = uaa->device;
353
354         DPRINTF("chiptype: %s\n",
355             (sc->sc_chiptype == TYPE_PL2303X) ?
356             "2303X" : "2303");
357
358         /*
359          * USB-RSAQ1 has two interface
360          *
361          *  USB-RSAQ1       | USB-RSAQ2
362          * -----------------+-----------------
363          * Interface 0      |Interface 0
364          *  Interrupt(0x81) | Interrupt(0x81)
365          * -----------------+ BulkIN(0x02)
366          * Interface 1      | BulkOUT(0x83)
367          *   BulkIN(0x02)   |
368          *   BulkOUT(0x83)  |
369          */
370
371         sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
372         sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
373
374         iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
375         if (iface) {
376                 id = usbd_get_interface_descriptor(iface);
377                 if (id == NULL) {
378                         device_printf(dev, "no interface descriptor (2)!\n");
379                         goto detach;
380                 }
381                 sc->sc_data_iface_no = id->bInterfaceNumber;
382                 sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
383                 usbd_set_parent_iface(uaa->device,
384                     UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
385         } else {
386                 sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
387                 sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
388         }
389
390         error = usbd_transfer_setup(uaa->device,
391             sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
392             UPLCOM_N_TRANSFER, sc, &sc->sc_mtx);
393         if (error) {
394                 DPRINTF("one or more missing USB endpoints, "
395                     "error=%s\n", usbd_errstr(error));
396                 goto detach;
397         }
398         error = uplcom_reset(sc, uaa->device);
399         if (error) {
400                 device_printf(dev, "reset failed, error=%s\n",
401                     usbd_errstr(error));
402                 goto detach;
403         }
404         /* clear stall at first run */
405         mtx_lock(&sc->sc_mtx);
406         usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
407         usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
408         mtx_unlock(&sc->sc_mtx);
409
410         error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
411             &uplcom_callback, &sc->sc_mtx);
412         if (error) {
413                 goto detach;
414         }
415         /*
416          * do the initialization during attach so that the system does not
417          * sleep during open:
418          */
419         if (sc->sc_chiptype == TYPE_PL2303X) {
420                 if (uplcom_pl2303x_init(uaa->device)) {
421                         device_printf(dev, "init failed!\n");
422                         goto detach;
423                 }
424         }
425         return (0);
426
427 detach:
428         uplcom_detach(dev);
429         return (ENXIO);
430 }
431
432 static int
433 uplcom_detach(device_t dev)
434 {
435         struct uplcom_softc *sc = device_get_softc(dev);
436
437         DPRINTF("sc=%p\n", sc);
438
439         ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
440         usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
441         mtx_destroy(&sc->sc_mtx);
442
443         return (0);
444 }
445
446 static usb_error_t
447 uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev)
448 {
449         struct usb_device_request req;
450
451         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
452         req.bRequest = UPLCOM_SET_REQUEST;
453         USETW(req.wValue, 0);
454         req.wIndex[0] = sc->sc_data_iface_no;
455         req.wIndex[1] = 0;
456         USETW(req.wLength, 0);
457
458         return (usbd_do_request(udev, NULL, &req, NULL));
459 }
460
461 struct pl2303x_init {
462         uint8_t req_type;
463         uint8_t request;
464         uint16_t value;
465         uint16_t index;
466         uint16_t length;
467 };
468
469 static const struct pl2303x_init pl2303x[] = {
470         {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
471         {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0},
472         {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
473         {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1},
474         {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
475         {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0},
476         {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
477         {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1},
478         {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0},
479         {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0},
480         {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0},
481         {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 8, 0, 0},
482         {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 9, 0, 0},
483 };
484
485 #define N_PL2302X_INIT  (sizeof(pl2303x)/sizeof(pl2303x[0]))
486
487 static int
488 uplcom_pl2303x_init(struct usb_device *udev)
489 {
490         struct usb_device_request req;
491         usb_error_t err;
492         uint8_t buf[4];
493         uint8_t i;
494
495         for (i = 0; i != N_PL2302X_INIT; i++) {
496                 req.bmRequestType = pl2303x[i].req_type;
497                 req.bRequest = pl2303x[i].request;
498                 USETW(req.wValue, pl2303x[i].value);
499                 USETW(req.wIndex, pl2303x[i].index);
500                 USETW(req.wLength, pl2303x[i].length);
501
502                 err = usbd_do_request(udev, NULL, &req, buf);
503                 if (err) {
504                         DPRINTF("error=%s\n", usbd_errstr(err));
505                         return (EIO);
506                 }
507         }
508         return (0);
509 }
510
511 static void
512 uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
513 {
514         struct uplcom_softc *sc = ucom->sc_parent;
515         struct usb_device_request req;
516
517         DPRINTF("onoff = %d\n", onoff);
518
519         if (onoff)
520                 sc->sc_line |= UCDC_LINE_DTR;
521         else
522                 sc->sc_line &= ~UCDC_LINE_DTR;
523
524         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
525         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
526         USETW(req.wValue, sc->sc_line);
527         req.wIndex[0] = sc->sc_data_iface_no;
528         req.wIndex[1] = 0;
529         USETW(req.wLength, 0);
530
531         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
532             &req, NULL, 0, 1000);
533 }
534
535 static void
536 uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
537 {
538         struct uplcom_softc *sc = ucom->sc_parent;
539         struct usb_device_request req;
540
541         DPRINTF("onoff = %d\n", onoff);
542
543         if (onoff)
544                 sc->sc_line |= UCDC_LINE_RTS;
545         else
546                 sc->sc_line &= ~UCDC_LINE_RTS;
547
548         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
549         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
550         USETW(req.wValue, sc->sc_line);
551         req.wIndex[0] = sc->sc_data_iface_no;
552         req.wIndex[1] = 0;
553         USETW(req.wLength, 0);
554
555         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
556             &req, NULL, 0, 1000);
557 }
558
559 static void
560 uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
561 {
562         struct uplcom_softc *sc = ucom->sc_parent;
563         struct usb_device_request req;
564         uint16_t temp;
565
566         DPRINTF("onoff = %d\n", onoff);
567
568         temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
569
570         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
571         req.bRequest = UCDC_SEND_BREAK;
572         USETW(req.wValue, temp);
573         req.wIndex[0] = sc->sc_data_iface_no;
574         req.wIndex[1] = 0;
575         USETW(req.wLength, 0);
576
577         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
578             &req, NULL, 0, 1000);
579 }
580
581 static const int32_t uplcom_rates[] = {
582         75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
583         19200, 28800, 38400, 57600, 115200,
584         /*
585          * Higher speeds are probably possible. PL2303X supports up to
586          * 6Mb and can set any rate
587          */
588         230400, 460800, 614400, 921600, 1228800
589 };
590
591 #define N_UPLCOM_RATES  (sizeof(uplcom_rates)/sizeof(uplcom_rates[0]))
592
593 static int
594 uplcom_pre_param(struct ucom_softc *ucom, struct termios *t)
595 {
596         uint8_t i;
597
598         DPRINTF("\n");
599
600         /* check requested baud rate */
601
602         for (i = 0;; i++) {
603
604                 if (i != N_UPLCOM_RATES) {
605                         if (uplcom_rates[i] == t->c_ospeed) {
606                                 break;
607                         }
608                 } else {
609                         DPRINTF("invalid baud rate (%d)\n", t->c_ospeed);
610                         return (EIO);
611                 }
612         }
613
614         return (0);
615 }
616
617 static void
618 uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
619 {
620         struct uplcom_softc *sc = ucom->sc_parent;
621         struct usb_cdc_line_state ls;
622         struct usb_device_request req;
623
624         DPRINTF("sc = %p\n", sc);
625
626         bzero(&ls, sizeof(ls));
627
628         USETDW(ls.dwDTERate, t->c_ospeed);
629
630         if (t->c_cflag & CSTOPB) {
631                 ls.bCharFormat = UCDC_STOP_BIT_2;
632         } else {
633                 ls.bCharFormat = UCDC_STOP_BIT_1;
634         }
635
636         if (t->c_cflag & PARENB) {
637                 if (t->c_cflag & PARODD) {
638                         ls.bParityType = UCDC_PARITY_ODD;
639                 } else {
640                         ls.bParityType = UCDC_PARITY_EVEN;
641                 }
642         } else {
643                 ls.bParityType = UCDC_PARITY_NONE;
644         }
645
646         switch (t->c_cflag & CSIZE) {
647         case CS5:
648                 ls.bDataBits = 5;
649                 break;
650         case CS6:
651                 ls.bDataBits = 6;
652                 break;
653         case CS7:
654                 ls.bDataBits = 7;
655                 break;
656         case CS8:
657                 ls.bDataBits = 8;
658                 break;
659         }
660
661         DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
662             UGETDW(ls.dwDTERate), ls.bCharFormat,
663             ls.bParityType, ls.bDataBits);
664
665         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
666         req.bRequest = UCDC_SET_LINE_CODING;
667         USETW(req.wValue, 0);
668         req.wIndex[0] = sc->sc_data_iface_no;
669         req.wIndex[1] = 0;
670         USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
671
672         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
673             &req, &ls, 0, 1000);
674
675         if (t->c_cflag & CRTSCTS) {
676
677                 DPRINTF("crtscts = on\n");
678
679                 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
680                 req.bRequest = UPLCOM_SET_REQUEST;
681                 USETW(req.wValue, 0);
682                 if (sc->sc_chiptype == TYPE_PL2303X)
683                         USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
684                 else
685                         USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
686                 USETW(req.wLength, 0);
687
688                 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
689                     &req, NULL, 0, 1000);
690         } else {
691                 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
692                 req.bRequest = UPLCOM_SET_REQUEST;
693                 USETW(req.wValue, 0);
694                 USETW(req.wIndex, 0);
695                 USETW(req.wLength, 0);
696                 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
697                     &req, NULL, 0, 1000);
698         }
699 }
700
701 static void
702 uplcom_start_read(struct ucom_softc *ucom)
703 {
704         struct uplcom_softc *sc = ucom->sc_parent;
705
706         /* start interrupt endpoint */
707         usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
708
709         /* start read endpoint */
710         usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
711 }
712
713 static void
714 uplcom_stop_read(struct ucom_softc *ucom)
715 {
716         struct uplcom_softc *sc = ucom->sc_parent;
717
718         /* stop interrupt endpoint */
719         usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
720
721         /* stop read endpoint */
722         usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
723 }
724
725 static void
726 uplcom_start_write(struct ucom_softc *ucom)
727 {
728         struct uplcom_softc *sc = ucom->sc_parent;
729
730         usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
731 }
732
733 static void
734 uplcom_stop_write(struct ucom_softc *ucom)
735 {
736         struct uplcom_softc *sc = ucom->sc_parent;
737
738         usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
739 }
740
741 static void
742 uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
743 {
744         struct uplcom_softc *sc = ucom->sc_parent;
745
746         DPRINTF("\n");
747
748         *lsr = sc->sc_lsr;
749         *msr = sc->sc_msr;
750 }
751
752 static void
753 uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
754 {
755         struct uplcom_softc *sc = usbd_xfer_softc(xfer);
756         struct usb_page_cache *pc;
757         uint8_t buf[9];
758         int actlen;
759
760         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
761
762         switch (USB_GET_STATE(xfer)) {
763         case USB_ST_TRANSFERRED:
764
765                 DPRINTF("actlen = %u\n", actlen);
766
767                 if (actlen >= 9) {
768
769                         pc = usbd_xfer_get_frame(xfer, 0);
770                         usbd_copy_out(pc, 0, buf, sizeof(buf));
771
772                         DPRINTF("status = 0x%02x\n", buf[8]);
773
774                         sc->sc_lsr = 0;
775                         sc->sc_msr = 0;
776
777                         if (buf[8] & RSAQ_STATUS_CTS) {
778                                 sc->sc_msr |= SER_CTS;
779                         }
780                         if (buf[8] & RSAQ_STATUS_DSR) {
781                                 sc->sc_msr |= SER_DSR;
782                         }
783                         if (buf[8] & RSAQ_STATUS_DCD) {
784                                 sc->sc_msr |= SER_DCD;
785                         }
786                         ucom_status_change(&sc->sc_ucom);
787                 }
788         case USB_ST_SETUP:
789 tr_setup:
790                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
791                 usbd_transfer_submit(xfer);
792                 return;
793
794         default:                        /* Error */
795                 if (error != USB_ERR_CANCELLED) {
796                         /* try to clear stall first */
797                         usbd_xfer_set_stall(xfer);
798                         goto tr_setup;
799                 }
800                 return;
801         }
802 }
803
804 static void
805 uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
806 {
807         struct uplcom_softc *sc = usbd_xfer_softc(xfer);
808         struct usb_page_cache *pc;
809         uint32_t actlen;
810
811         switch (USB_GET_STATE(xfer)) {
812         case USB_ST_SETUP:
813         case USB_ST_TRANSFERRED:
814 tr_setup:
815                 pc = usbd_xfer_get_frame(xfer, 0);
816                 if (ucom_get_data(&sc->sc_ucom, pc, 0,
817                     UPLCOM_BULK_BUF_SIZE, &actlen)) {
818
819                         DPRINTF("actlen = %d\n", actlen);
820
821                         usbd_xfer_set_frame_len(xfer, 0, actlen);
822                         usbd_transfer_submit(xfer);
823                 }
824                 return;
825
826         default:                        /* Error */
827                 if (error != USB_ERR_CANCELLED) {
828                         /* try to clear stall first */
829                         usbd_xfer_set_stall(xfer);
830                         goto tr_setup;
831                 }
832                 return;
833         }
834 }
835
836 static void
837 uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
838 {
839         struct uplcom_softc *sc = usbd_xfer_softc(xfer);
840         struct usb_page_cache *pc;
841         int actlen;
842
843         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
844
845         switch (USB_GET_STATE(xfer)) {
846         case USB_ST_TRANSFERRED:
847                 pc = usbd_xfer_get_frame(xfer, 0);
848                 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
849
850         case USB_ST_SETUP:
851 tr_setup:
852                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
853                 usbd_transfer_submit(xfer);
854                 return;
855
856         default:                        /* Error */
857                 if (error != USB_ERR_CANCELLED) {
858                         /* try to clear stall first */
859                         usbd_xfer_set_stall(xfer);
860                         goto tr_setup;
861                 }
862                 return;
863         }
864 }