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