]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/usb/serial/uplcom.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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 #ifdef 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 static void     uplcom_poll(struct ucom_softc *ucom);
190
191 static device_probe_t uplcom_probe;
192 static device_attach_t uplcom_attach;
193 static device_detach_t uplcom_detach;
194
195 static usb_callback_t uplcom_intr_callback;
196 static usb_callback_t uplcom_write_callback;
197 static usb_callback_t uplcom_read_callback;
198
199 static const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = {
200
201         [UPLCOM_BULK_DT_WR] = {
202                 .type = UE_BULK,
203                 .endpoint = UE_ADDR_ANY,
204                 .direction = UE_DIR_OUT,
205                 .bufsize = UPLCOM_BULK_BUF_SIZE,
206                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
207                 .callback = &uplcom_write_callback,
208                 .if_index = 0,
209         },
210
211         [UPLCOM_BULK_DT_RD] = {
212                 .type = UE_BULK,
213                 .endpoint = UE_ADDR_ANY,
214                 .direction = UE_DIR_IN,
215                 .bufsize = UPLCOM_BULK_BUF_SIZE,
216                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
217                 .callback = &uplcom_read_callback,
218                 .if_index = 0,
219         },
220
221         [UPLCOM_INTR_DT_RD] = {
222                 .type = UE_INTERRUPT,
223                 .endpoint = UE_ADDR_ANY,
224                 .direction = UE_DIR_IN,
225                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
226                 .bufsize = 0,   /* use wMaxPacketSize */
227                 .callback = &uplcom_intr_callback,
228                 .if_index = 1,
229         },
230 };
231
232 static struct ucom_callback uplcom_callback = {
233         .ucom_cfg_get_status = &uplcom_cfg_get_status,
234         .ucom_cfg_set_dtr = &uplcom_cfg_set_dtr,
235         .ucom_cfg_set_rts = &uplcom_cfg_set_rts,
236         .ucom_cfg_set_break = &uplcom_cfg_set_break,
237         .ucom_cfg_param = &uplcom_cfg_param,
238         .ucom_pre_param = &uplcom_pre_param,
239         .ucom_start_read = &uplcom_start_read,
240         .ucom_stop_read = &uplcom_stop_read,
241         .ucom_start_write = &uplcom_start_write,
242         .ucom_stop_write = &uplcom_stop_write,
243         .ucom_poll = &uplcom_poll,
244 };
245
246 #define UPLCOM_DEV(v,p,rl,rh,t)                         \
247   { USB_VENDOR(USB_VENDOR_##v), USB_PRODUCT(USB_PRODUCT_##v##_##p), \
248     USB_DEV_BCD_GTEQ(rl), USB_DEV_BCD_LTEQ(rh), USB_DRIVER_INFO(TYPE_##t) }
249
250 static const struct usb_device_id uplcom_devs[] = {
251         /* Belkin F5U257 */
252         UPLCOM_DEV(BELKIN, F5U257, 0, 0xFFFF, PL2303X),
253         /* I/O DATA USB-RSAQ */
254         UPLCOM_DEV(IODATA, USBRSAQ, 0, 0xFFFF, PL2303),
255         /* I/O DATA USB-RSAQ2 */
256         UPLCOM_DEV(PROLIFIC, RSAQ2, 0, 0xFFFF, PL2303),
257         /* I/O DATA USB-RSAQ3 */
258         UPLCOM_DEV(PROLIFIC, RSAQ3, 0, 0xFFFF, PL2303X),
259         /* PLANEX USB-RS232 URS-03 */
260         UPLCOM_DEV(ATEN, UC232A, 0, 0xFFFF, PL2303),
261         /* TrendNet TU-S9 */
262         UPLCOM_DEV(PROLIFIC, PL2303, 0x0400, 0xFFFF, PL2303X),
263         /* ST Lab USB-SERIAL-4 */
264         UPLCOM_DEV(PROLIFIC, PL2303, 0x0300, 0x03FF, PL2303X),
265         /* IOGEAR/ATEN UC-232A (also ST Lab USB-SERIAL-1) */
266         UPLCOM_DEV(PROLIFIC, PL2303, 0, 0x02FF, PL2303),
267         /* TDK USB-PHS Adapter UHA6400 */
268         UPLCOM_DEV(TDK, UHA6400, 0, 0xFFFF, PL2303),
269         /* RATOC REX-USB60 */
270         UPLCOM_DEV(RATOC, REXUSB60, 0, 0xFFFF, PL2303),
271         /* ELECOM UC-SGT */
272         UPLCOM_DEV(ELECOM, UCSGT, 0, 0xFFFF, PL2303),
273         UPLCOM_DEV(ELECOM, UCSGT0, 0, 0xFFFF, PL2303),
274         /* Sagem USB-Serial Controller */
275         UPLCOM_DEV(SAGEM, USBSERIAL, 0, 0xFFFF, PL2303X),
276         /* Sony Ericsson USB Cable */
277         UPLCOM_DEV(SONYERICSSON, DCU10, 0, 0xFFFF, PL2303),
278         /* SOURCENEXT KeikaiDenwa 8 */
279         UPLCOM_DEV(SOURCENEXT, KEIKAI8, 0, 0xFFFF, PL2303),
280         /* SOURCENEXT KeikaiDenwa 8 with charger */
281         UPLCOM_DEV(SOURCENEXT, KEIKAI8_CHG, 0, 0, PL2303),
282         /* HAL Corporation Crossam2+USB */
283         UPLCOM_DEV(HAL, IMR001, 0, 0xFFFF, PL2303),
284         /* Sitecom USB to Serial */
285         UPLCOM_DEV(SITECOM, SERIAL, 0, 0xFFFF, PL2303),
286         /* Tripp-Lite U209-000-R */
287         UPLCOM_DEV(TRIPPLITE, U209, 0, 0xFFFF, PL2303X),
288         UPLCOM_DEV(RADIOSHACK, USBCABLE, 0, 0xFFFF, PL2303),
289         /* Prolific Pharos */
290         UPLCOM_DEV(PROLIFIC, PHAROS, 0, 0xFFFF, PL2303),
291         /* Willcom W-SIM */
292         UPLCOM_DEV(PROLIFIC2, WSIM, 0, 0xFFFF, PL2303X),
293         /* Mobile Action MA-620 Infrared Adapter */
294         UPLCOM_DEV(MOBILEACTION, MA620, 0, 0xFFFF, PL2303X),
295 };
296 #undef UPLCOM_DEV
297
298 static device_method_t uplcom_methods[] = {
299         DEVMETHOD(device_probe, uplcom_probe),
300         DEVMETHOD(device_attach, uplcom_attach),
301         DEVMETHOD(device_detach, uplcom_detach),
302         {0, 0}
303 };
304
305 static devclass_t uplcom_devclass;
306
307 static driver_t uplcom_driver = {
308         .name = "uplcom",
309         .methods = uplcom_methods,
310         .size = sizeof(struct uplcom_softc),
311 };
312
313 DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0);
314 MODULE_DEPEND(uplcom, ucom, 1, 1, 1);
315 MODULE_DEPEND(uplcom, usb, 1, 1, 1);
316 MODULE_VERSION(uplcom, UPLCOM_MODVER);
317
318 static int
319 uplcom_probe(device_t dev)
320 {
321         struct usb_attach_arg *uaa = device_get_ivars(dev);
322
323         DPRINTFN(11, "\n");
324
325         if (uaa->usb_mode != USB_MODE_HOST) {
326                 return (ENXIO);
327         }
328         if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
329                 return (ENXIO);
330         }
331         if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
332                 return (ENXIO);
333         }
334         return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
335 }
336
337 static int
338 uplcom_attach(device_t dev)
339 {
340         struct usb_attach_arg *uaa = device_get_ivars(dev);
341         struct uplcom_softc *sc = device_get_softc(dev);
342         struct usb_interface *iface;
343         struct usb_interface_descriptor *id;
344         int error;
345
346         DPRINTFN(11, "\n");
347
348         device_set_usb_desc(dev);
349         mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF);
350
351         DPRINTF("sc = %p\n", sc);
352
353         sc->sc_chiptype = USB_GET_DRIVER_INFO(uaa);
354         sc->sc_udev = uaa->device;
355
356         DPRINTF("chiptype: %s\n",
357             (sc->sc_chiptype == TYPE_PL2303X) ?
358             "2303X" : "2303");
359
360         /*
361          * USB-RSAQ1 has two interface
362          *
363          *  USB-RSAQ1       | USB-RSAQ2
364          * -----------------+-----------------
365          * Interface 0      |Interface 0
366          *  Interrupt(0x81) | Interrupt(0x81)
367          * -----------------+ BulkIN(0x02)
368          * Interface 1      | BulkOUT(0x83)
369          *   BulkIN(0x02)   |
370          *   BulkOUT(0x83)  |
371          */
372
373         sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
374         sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
375
376         iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
377         if (iface) {
378                 id = usbd_get_interface_descriptor(iface);
379                 if (id == NULL) {
380                         device_printf(dev, "no interface descriptor (2)\n");
381                         goto detach;
382                 }
383                 sc->sc_data_iface_no = id->bInterfaceNumber;
384                 sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
385                 usbd_set_parent_iface(uaa->device,
386                     UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
387         } else {
388                 sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
389                 sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
390         }
391
392         error = usbd_transfer_setup(uaa->device,
393             sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
394             UPLCOM_N_TRANSFER, sc, &sc->sc_mtx);
395         if (error) {
396                 DPRINTF("one or more missing USB endpoints, "
397                     "error=%s\n", usbd_errstr(error));
398                 goto detach;
399         }
400         error = uplcom_reset(sc, uaa->device);
401         if (error) {
402                 device_printf(dev, "reset failed, error=%s\n",
403                     usbd_errstr(error));
404                 goto detach;
405         }
406         /* clear stall at first run */
407         mtx_lock(&sc->sc_mtx);
408         usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
409         usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
410         mtx_unlock(&sc->sc_mtx);
411
412         error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
413             &uplcom_callback, &sc->sc_mtx);
414         if (error) {
415                 goto detach;
416         }
417         /*
418          * do the initialization during attach so that the system does not
419          * sleep during open:
420          */
421         if (sc->sc_chiptype == TYPE_PL2303X) {
422                 if (uplcom_pl2303x_init(uaa->device)) {
423                         device_printf(dev, "init failed\n");
424                         goto detach;
425                 }
426         }
427         return (0);
428
429 detach:
430         uplcom_detach(dev);
431         return (ENXIO);
432 }
433
434 static int
435 uplcom_detach(device_t dev)
436 {
437         struct uplcom_softc *sc = device_get_softc(dev);
438
439         DPRINTF("sc=%p\n", sc);
440
441         ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
442         usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
443         mtx_destroy(&sc->sc_mtx);
444
445         return (0);
446 }
447
448 static usb_error_t
449 uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev)
450 {
451         struct usb_device_request req;
452
453         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
454         req.bRequest = UPLCOM_SET_REQUEST;
455         USETW(req.wValue, 0);
456         req.wIndex[0] = sc->sc_data_iface_no;
457         req.wIndex[1] = 0;
458         USETW(req.wLength, 0);
459
460         return (usbd_do_request(udev, NULL, &req, NULL));
461 }
462
463 struct pl2303x_init {
464         uint8_t req_type;
465         uint8_t request;
466         uint16_t value;
467         uint16_t index;
468         uint16_t length;
469 };
470
471 static const struct pl2303x_init pl2303x[] = {
472         {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
473         {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0},
474         {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
475         {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1},
476         {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
477         {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0},
478         {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
479         {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1},
480         {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0},
481         {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0},
482         {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0},
483         {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 8, 0, 0},
484         {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 9, 0, 0},
485 };
486
487 #define N_PL2302X_INIT  (sizeof(pl2303x)/sizeof(pl2303x[0]))
488
489 static int
490 uplcom_pl2303x_init(struct usb_device *udev)
491 {
492         struct usb_device_request req;
493         usb_error_t err;
494         uint8_t buf[4];
495         uint8_t i;
496
497         for (i = 0; i != N_PL2302X_INIT; i++) {
498                 req.bmRequestType = pl2303x[i].req_type;
499                 req.bRequest = pl2303x[i].request;
500                 USETW(req.wValue, pl2303x[i].value);
501                 USETW(req.wIndex, pl2303x[i].index);
502                 USETW(req.wLength, pl2303x[i].length);
503
504                 err = usbd_do_request(udev, NULL, &req, buf);
505                 if (err) {
506                         DPRINTF("error=%s\n", usbd_errstr(err));
507                         return (EIO);
508                 }
509         }
510         return (0);
511 }
512
513 static void
514 uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
515 {
516         struct uplcom_softc *sc = ucom->sc_parent;
517         struct usb_device_request req;
518
519         DPRINTF("onoff = %d\n", onoff);
520
521         if (onoff)
522                 sc->sc_line |= UCDC_LINE_DTR;
523         else
524                 sc->sc_line &= ~UCDC_LINE_DTR;
525
526         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
527         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
528         USETW(req.wValue, sc->sc_line);
529         req.wIndex[0] = sc->sc_data_iface_no;
530         req.wIndex[1] = 0;
531         USETW(req.wLength, 0);
532
533         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
534             &req, NULL, 0, 1000);
535 }
536
537 static void
538 uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
539 {
540         struct uplcom_softc *sc = ucom->sc_parent;
541         struct usb_device_request req;
542
543         DPRINTF("onoff = %d\n", onoff);
544
545         if (onoff)
546                 sc->sc_line |= UCDC_LINE_RTS;
547         else
548                 sc->sc_line &= ~UCDC_LINE_RTS;
549
550         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
551         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
552         USETW(req.wValue, sc->sc_line);
553         req.wIndex[0] = sc->sc_data_iface_no;
554         req.wIndex[1] = 0;
555         USETW(req.wLength, 0);
556
557         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
558             &req, NULL, 0, 1000);
559 }
560
561 static void
562 uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
563 {
564         struct uplcom_softc *sc = ucom->sc_parent;
565         struct usb_device_request req;
566         uint16_t temp;
567
568         DPRINTF("onoff = %d\n", onoff);
569
570         temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
571
572         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
573         req.bRequest = UCDC_SEND_BREAK;
574         USETW(req.wValue, temp);
575         req.wIndex[0] = sc->sc_data_iface_no;
576         req.wIndex[1] = 0;
577         USETW(req.wLength, 0);
578
579         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
580             &req, NULL, 0, 1000);
581 }
582
583 static const int32_t uplcom_rates[] = {
584         75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
585         19200, 28800, 38400, 57600, 115200,
586         /*
587          * Higher speeds are probably possible. PL2303X supports up to
588          * 6Mb and can set any rate
589          */
590         230400, 460800, 614400, 921600, 1228800
591 };
592
593 #define N_UPLCOM_RATES  (sizeof(uplcom_rates)/sizeof(uplcom_rates[0]))
594
595 static int
596 uplcom_pre_param(struct ucom_softc *ucom, struct termios *t)
597 {
598         uint8_t i;
599
600         DPRINTF("\n");
601
602         /* check requested baud rate */
603
604         for (i = 0;; i++) {
605
606                 if (i != N_UPLCOM_RATES) {
607                         if (uplcom_rates[i] == t->c_ospeed) {
608                                 break;
609                         }
610                 } else {
611                         DPRINTF("invalid baud rate (%d)\n", t->c_ospeed);
612                         return (EIO);
613                 }
614         }
615
616         return (0);
617 }
618
619 static void
620 uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
621 {
622         struct uplcom_softc *sc = ucom->sc_parent;
623         struct usb_cdc_line_state ls;
624         struct usb_device_request req;
625
626         DPRINTF("sc = %p\n", sc);
627
628         bzero(&ls, sizeof(ls));
629
630         USETDW(ls.dwDTERate, t->c_ospeed);
631
632         if (t->c_cflag & CSTOPB) {
633                 ls.bCharFormat = UCDC_STOP_BIT_2;
634         } else {
635                 ls.bCharFormat = UCDC_STOP_BIT_1;
636         }
637
638         if (t->c_cflag & PARENB) {
639                 if (t->c_cflag & PARODD) {
640                         ls.bParityType = UCDC_PARITY_ODD;
641                 } else {
642                         ls.bParityType = UCDC_PARITY_EVEN;
643                 }
644         } else {
645                 ls.bParityType = UCDC_PARITY_NONE;
646         }
647
648         switch (t->c_cflag & CSIZE) {
649         case CS5:
650                 ls.bDataBits = 5;
651                 break;
652         case CS6:
653                 ls.bDataBits = 6;
654                 break;
655         case CS7:
656                 ls.bDataBits = 7;
657                 break;
658         case CS8:
659                 ls.bDataBits = 8;
660                 break;
661         }
662
663         DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
664             UGETDW(ls.dwDTERate), ls.bCharFormat,
665             ls.bParityType, ls.bDataBits);
666
667         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
668         req.bRequest = UCDC_SET_LINE_CODING;
669         USETW(req.wValue, 0);
670         req.wIndex[0] = sc->sc_data_iface_no;
671         req.wIndex[1] = 0;
672         USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
673
674         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
675             &req, &ls, 0, 1000);
676
677         if (t->c_cflag & CRTSCTS) {
678
679                 DPRINTF("crtscts = on\n");
680
681                 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
682                 req.bRequest = UPLCOM_SET_REQUEST;
683                 USETW(req.wValue, 0);
684                 if (sc->sc_chiptype == TYPE_PL2303X)
685                         USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
686                 else
687                         USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
688                 USETW(req.wLength, 0);
689
690                 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
691                     &req, NULL, 0, 1000);
692         } else {
693                 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
694                 req.bRequest = UPLCOM_SET_REQUEST;
695                 USETW(req.wValue, 0);
696                 USETW(req.wIndex, 0);
697                 USETW(req.wLength, 0);
698                 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
699                     &req, NULL, 0, 1000);
700         }
701 }
702
703 static void
704 uplcom_start_read(struct ucom_softc *ucom)
705 {
706         struct uplcom_softc *sc = ucom->sc_parent;
707
708         /* start interrupt endpoint */
709         usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
710
711         /* start read endpoint */
712         usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
713 }
714
715 static void
716 uplcom_stop_read(struct ucom_softc *ucom)
717 {
718         struct uplcom_softc *sc = ucom->sc_parent;
719
720         /* stop interrupt endpoint */
721         usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
722
723         /* stop read endpoint */
724         usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
725 }
726
727 static void
728 uplcom_start_write(struct ucom_softc *ucom)
729 {
730         struct uplcom_softc *sc = ucom->sc_parent;
731
732         usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
733 }
734
735 static void
736 uplcom_stop_write(struct ucom_softc *ucom)
737 {
738         struct uplcom_softc *sc = ucom->sc_parent;
739
740         usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
741 }
742
743 static void
744 uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
745 {
746         struct uplcom_softc *sc = ucom->sc_parent;
747
748         DPRINTF("\n");
749
750         *lsr = sc->sc_lsr;
751         *msr = sc->sc_msr;
752 }
753
754 static void
755 uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
756 {
757         struct uplcom_softc *sc = usbd_xfer_softc(xfer);
758         struct usb_page_cache *pc;
759         uint8_t buf[9];
760         int actlen;
761
762         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
763
764         switch (USB_GET_STATE(xfer)) {
765         case USB_ST_TRANSFERRED:
766
767                 DPRINTF("actlen = %u\n", actlen);
768
769                 if (actlen >= 9) {
770
771                         pc = usbd_xfer_get_frame(xfer, 0);
772                         usbd_copy_out(pc, 0, buf, sizeof(buf));
773
774                         DPRINTF("status = 0x%02x\n", buf[8]);
775
776                         sc->sc_lsr = 0;
777                         sc->sc_msr = 0;
778
779                         if (buf[8] & RSAQ_STATUS_CTS) {
780                                 sc->sc_msr |= SER_CTS;
781                         }
782                         if (buf[8] & RSAQ_STATUS_DSR) {
783                                 sc->sc_msr |= SER_DSR;
784                         }
785                         if (buf[8] & RSAQ_STATUS_DCD) {
786                                 sc->sc_msr |= SER_DCD;
787                         }
788                         ucom_status_change(&sc->sc_ucom);
789                 }
790         case USB_ST_SETUP:
791 tr_setup:
792                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
793                 usbd_transfer_submit(xfer);
794                 return;
795
796         default:                        /* Error */
797                 if (error != USB_ERR_CANCELLED) {
798                         /* try to clear stall first */
799                         usbd_xfer_set_stall(xfer);
800                         goto tr_setup;
801                 }
802                 return;
803         }
804 }
805
806 static void
807 uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
808 {
809         struct uplcom_softc *sc = usbd_xfer_softc(xfer);
810         struct usb_page_cache *pc;
811         uint32_t actlen;
812
813         switch (USB_GET_STATE(xfer)) {
814         case USB_ST_SETUP:
815         case USB_ST_TRANSFERRED:
816 tr_setup:
817                 pc = usbd_xfer_get_frame(xfer, 0);
818                 if (ucom_get_data(&sc->sc_ucom, pc, 0,
819                     UPLCOM_BULK_BUF_SIZE, &actlen)) {
820
821                         DPRINTF("actlen = %d\n", actlen);
822
823                         usbd_xfer_set_frame_len(xfer, 0, actlen);
824                         usbd_transfer_submit(xfer);
825                 }
826                 return;
827
828         default:                        /* Error */
829                 if (error != USB_ERR_CANCELLED) {
830                         /* try to clear stall first */
831                         usbd_xfer_set_stall(xfer);
832                         goto tr_setup;
833                 }
834                 return;
835         }
836 }
837
838 static void
839 uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
840 {
841         struct uplcom_softc *sc = usbd_xfer_softc(xfer);
842         struct usb_page_cache *pc;
843         int actlen;
844
845         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
846
847         switch (USB_GET_STATE(xfer)) {
848         case USB_ST_TRANSFERRED:
849                 pc = usbd_xfer_get_frame(xfer, 0);
850                 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
851
852         case USB_ST_SETUP:
853 tr_setup:
854                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
855                 usbd_transfer_submit(xfer);
856                 return;
857
858         default:                        /* Error */
859                 if (error != USB_ERR_CANCELLED) {
860                         /* try to clear stall first */
861                         usbd_xfer_set_stall(xfer);
862                         goto tr_setup;
863                 }
864                 return;
865         }
866 }
867
868 static void
869 uplcom_poll(struct ucom_softc *ucom)
870 {
871         struct uplcom_softc *sc = ucom->sc_parent;
872         usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER);
873 }