]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/serial/uplcom.c
MFV r333789: libpcap 1.9.0 (pre-release)
[FreeBSD/FreeBSD.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  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-2-Clause-NetBSD
8  *
9  * Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 /*-
35  * Copyright (c) 2001 The NetBSD Foundation, Inc.
36  * All rights reserved.
37  *
38  * This code is derived from software contributed to The NetBSD Foundation
39  * by Ichiro FUKUHARA (ichiro@ichiro.org).
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
51  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
52  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
54  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60  * POSSIBILITY OF SUCH DAMAGE.
61  */
62
63 /*
64  * This driver supports several USB-to-RS232 serial adapters driven by
65  * Prolific PL-2303, PL-2303X and probably PL-2303HX USB-to-RS232
66  * bridge chip.  The adapters are sold under many different brand
67  * names.
68  *
69  * Datasheets are available at Prolific www site at
70  * http://www.prolific.com.tw.  The datasheets don't contain full
71  * programming information for the chip.
72  *
73  * PL-2303HX is probably programmed the same as PL-2303X.
74  *
75  * There are several differences between PL-2303 and PL-2303(H)X.
76  * PL-2303(H)X can do higher bitrate in bulk mode, has _probably_
77  * different command for controlling CRTSCTS and needs special
78  * sequence of commands for initialization which aren't also
79  * documented in the datasheet.
80  */
81
82 #include <sys/stdint.h>
83 #include <sys/stddef.h>
84 #include <sys/param.h>
85 #include <sys/queue.h>
86 #include <sys/types.h>
87 #include <sys/systm.h>
88 #include <sys/kernel.h>
89 #include <sys/bus.h>
90 #include <sys/module.h>
91 #include <sys/lock.h>
92 #include <sys/mutex.h>
93 #include <sys/condvar.h>
94 #include <sys/sysctl.h>
95 #include <sys/sx.h>
96 #include <sys/unistd.h>
97 #include <sys/callout.h>
98 #include <sys/malloc.h>
99 #include <sys/priv.h>
100
101 #include <dev/usb/usb.h>
102 #include <dev/usb/usbdi.h>
103 #include <dev/usb/usbdi_util.h>
104 #include <dev/usb/usb_cdc.h>
105 #include "usbdevs.h"
106
107 #define USB_DEBUG_VAR uplcom_debug
108 #include <dev/usb/usb_debug.h>
109 #include <dev/usb/usb_process.h>
110
111 #include <dev/usb/serial/usb_serial.h>
112
113 #ifdef USB_DEBUG
114 static int uplcom_debug = 0;
115
116 static SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom");
117 SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RWTUN,
118     &uplcom_debug, 0, "Debug level");
119 #endif
120
121 #define UPLCOM_MODVER                   1       /* module version */
122
123 #define UPLCOM_CONFIG_INDEX             0
124 #define UPLCOM_IFACE_INDEX              0
125 #define UPLCOM_SECOND_IFACE_INDEX       1
126
127 #ifndef UPLCOM_INTR_INTERVAL
128 #define UPLCOM_INTR_INTERVAL            0       /* default */
129 #endif
130
131 #define UPLCOM_BULK_BUF_SIZE 1024       /* bytes */
132
133 #define UPLCOM_SET_REQUEST              0x01
134 #define UPLCOM_SET_CRTSCTS              0x41
135 #define UPLCOM_SET_CRTSCTS_PL2303X      0x61
136 #define RSAQ_STATUS_CTS                 0x80
137 #define RSAQ_STATUS_DSR                 0x02
138 #define RSAQ_STATUS_DCD                 0x01
139
140 #define TYPE_PL2303                     0
141 #define TYPE_PL2303HX                   1
142
143 enum {
144         UPLCOM_BULK_DT_WR,
145         UPLCOM_BULK_DT_RD,
146         UPLCOM_INTR_DT_RD,
147         UPLCOM_N_TRANSFER,
148 };
149
150 struct uplcom_softc {
151         struct ucom_super_softc sc_super_ucom;
152         struct ucom_softc sc_ucom;
153
154         struct usb_xfer *sc_xfer[UPLCOM_N_TRANSFER];
155         struct usb_device *sc_udev;
156         struct mtx sc_mtx;
157
158         uint16_t sc_line;
159
160         uint8_t sc_lsr;                 /* local status register */
161         uint8_t sc_msr;                 /* uplcom status register */
162         uint8_t sc_chiptype;            /* type of chip */
163         uint8_t sc_ctrl_iface_no;
164         uint8_t sc_data_iface_no;
165         uint8_t sc_iface_index[2];
166 };
167
168 /* prototypes */
169
170 static usb_error_t uplcom_reset(struct uplcom_softc *, struct usb_device *);
171 static usb_error_t uplcom_pl2303_do(struct usb_device *, uint8_t, uint8_t,
172                         uint16_t, uint16_t, uint16_t);
173 static int      uplcom_pl2303_init(struct usb_device *, uint8_t);
174 static void     uplcom_free(struct ucom_softc *);
175 static void     uplcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
176 static void     uplcom_cfg_set_rts(struct ucom_softc *, uint8_t);
177 static void     uplcom_cfg_set_break(struct ucom_softc *, uint8_t);
178 static int      uplcom_pre_param(struct ucom_softc *, struct termios *);
179 static void     uplcom_cfg_param(struct ucom_softc *, struct termios *);
180 static void     uplcom_start_read(struct ucom_softc *);
181 static void     uplcom_stop_read(struct ucom_softc *);
182 static void     uplcom_start_write(struct ucom_softc *);
183 static void     uplcom_stop_write(struct ucom_softc *);
184 static void     uplcom_cfg_get_status(struct ucom_softc *, uint8_t *,
185                     uint8_t *);
186 static void     uplcom_poll(struct ucom_softc *ucom);
187
188 static device_probe_t uplcom_probe;
189 static device_attach_t uplcom_attach;
190 static device_detach_t uplcom_detach;
191 static void uplcom_free_softc(struct uplcom_softc *);
192
193 static usb_callback_t uplcom_intr_callback;
194 static usb_callback_t uplcom_write_callback;
195 static usb_callback_t uplcom_read_callback;
196
197 static const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = {
198
199         [UPLCOM_BULK_DT_WR] = {
200                 .type = UE_BULK,
201                 .endpoint = UE_ADDR_ANY,
202                 .direction = UE_DIR_OUT,
203                 .bufsize = UPLCOM_BULK_BUF_SIZE,
204                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
205                 .callback = &uplcom_write_callback,
206                 .if_index = 0,
207         },
208
209         [UPLCOM_BULK_DT_RD] = {
210                 .type = UE_BULK,
211                 .endpoint = UE_ADDR_ANY,
212                 .direction = UE_DIR_IN,
213                 .bufsize = UPLCOM_BULK_BUF_SIZE,
214                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
215                 .callback = &uplcom_read_callback,
216                 .if_index = 0,
217         },
218
219         [UPLCOM_INTR_DT_RD] = {
220                 .type = UE_INTERRUPT,
221                 .endpoint = UE_ADDR_ANY,
222                 .direction = UE_DIR_IN,
223                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
224                 .bufsize = 0,   /* use wMaxPacketSize */
225                 .callback = &uplcom_intr_callback,
226                 .if_index = 1,
227         },
228 };
229
230 static struct ucom_callback uplcom_callback = {
231         .ucom_cfg_get_status = &uplcom_cfg_get_status,
232         .ucom_cfg_set_dtr = &uplcom_cfg_set_dtr,
233         .ucom_cfg_set_rts = &uplcom_cfg_set_rts,
234         .ucom_cfg_set_break = &uplcom_cfg_set_break,
235         .ucom_cfg_param = &uplcom_cfg_param,
236         .ucom_pre_param = &uplcom_pre_param,
237         .ucom_start_read = &uplcom_start_read,
238         .ucom_stop_read = &uplcom_stop_read,
239         .ucom_start_write = &uplcom_start_write,
240         .ucom_stop_write = &uplcom_stop_write,
241         .ucom_poll = &uplcom_poll,
242         .ucom_free = &uplcom_free,
243 };
244
245 #define UPLCOM_DEV(v,p)                         \
246   { USB_VENDOR(USB_VENDOR_##v), USB_PRODUCT(USB_PRODUCT_##v##_##p) }
247
248 static const STRUCT_USB_HOST_ID uplcom_devs[] = {
249         UPLCOM_DEV(ACERP, S81),                 /* BenQ S81 phone */
250         UPLCOM_DEV(ADLINK, ND6530),             /* ADLINK ND-6530 USB-Serial */
251         UPLCOM_DEV(ALCATEL, OT535),             /* Alcatel One Touch 535/735 */
252         UPLCOM_DEV(ALCOR, AU9720),              /* Alcor AU9720 USB 2.0-RS232 */
253         UPLCOM_DEV(ANCHOR, SERIAL),             /* Anchor Serial adapter */
254         UPLCOM_DEV(ATEN, UC232A),               /* PLANEX USB-RS232 URS-03 */
255         UPLCOM_DEV(BELKIN, F5U257),             /* Belkin F5U257 USB to Serial */
256         UPLCOM_DEV(COREGA, CGUSBRS232R),        /* Corega CG-USBRS232R */
257         UPLCOM_DEV(EPSON, CRESSI_EDY),          /* Cressi Edy diving computer */
258         UPLCOM_DEV(EPSON, N2ITION3),            /* Zeagle N2iTion3 diving computer */
259         UPLCOM_DEV(ELECOM, UCSGT),              /* ELECOM UC-SGT Serial Adapter */
260         UPLCOM_DEV(ELECOM, UCSGT0),             /* ELECOM UC-SGT Serial Adapter */
261         UPLCOM_DEV(HAL, IMR001),                /* HAL Corporation Crossam2+USB */
262         UPLCOM_DEV(HP, LD220),                  /* HP LD220 POS Display */
263         UPLCOM_DEV(IODATA, USBRSAQ),            /* I/O DATA USB-RSAQ */
264         UPLCOM_DEV(IODATA, USBRSAQ5),           /* I/O DATA USB-RSAQ5 */
265         UPLCOM_DEV(ITEGNO, WM1080A),            /* iTegno WM1080A GSM/GFPRS modem */
266         UPLCOM_DEV(ITEGNO, WM2080A),            /* iTegno WM2080A CDMA modem */
267         UPLCOM_DEV(LEADTEK, 9531),              /* Leadtek 9531 GPS */
268         UPLCOM_DEV(MICROSOFT, 700WX),           /* Microsoft Palm 700WX */
269         UPLCOM_DEV(MOBILEACTION, MA620),        /* Mobile Action MA-620 Infrared Adapter */
270         UPLCOM_DEV(NETINDEX, WS002IN),          /* Willcom W-S002IN */
271         UPLCOM_DEV(NOKIA2, CA42),               /* Nokia CA-42 cable */
272         UPLCOM_DEV(OTI, DKU5),                  /* OTI DKU-5 cable */
273         UPLCOM_DEV(PANASONIC, TYTP50P6S),       /* Panasonic TY-TP50P6-S flat screen */
274         UPLCOM_DEV(PLX, CA42),                  /* PLX CA-42 clone cable */
275         UPLCOM_DEV(PROLIFIC, ALLTRONIX_GPRS),   /* Alltronix ACM003U00 modem */
276         UPLCOM_DEV(PROLIFIC, ALDIGA_AL11U),     /* AlDiga AL-11U modem */
277         UPLCOM_DEV(PROLIFIC, DCU11),            /* DCU-11 Phone Cable */
278         UPLCOM_DEV(PROLIFIC, HCR331),           /* HCR331 Card Reader */
279         UPLCOM_DEV(PROLIFIC, MICROMAX_610U),    /* Micromax 610U modem */
280         UPLCOM_DEV(PROLIFIC, MOTOROLA),         /* Motorola cable */
281         UPLCOM_DEV(PROLIFIC, PHAROS),           /* Prolific Pharos */
282         UPLCOM_DEV(PROLIFIC, PL2303),           /* Generic adapter */
283         UPLCOM_DEV(PROLIFIC, RSAQ2),            /* I/O DATA USB-RSAQ2 */
284         UPLCOM_DEV(PROLIFIC, RSAQ3),            /* I/O DATA USB-RSAQ3 */
285         UPLCOM_DEV(PROLIFIC, UIC_MSR206),       /* UIC MSR206 Card Reader */
286         UPLCOM_DEV(PROLIFIC2, PL2303),          /* Prolific adapter */
287         UPLCOM_DEV(RADIOSHACK, USBCABLE),       /* Radio Shack USB Adapter */
288         UPLCOM_DEV(RATOC, REXUSB60),            /* RATOC REX-USB60 */
289         UPLCOM_DEV(SAGEM, USBSERIAL),           /* Sagem USB-Serial Controller */
290         UPLCOM_DEV(SAMSUNG, I330),              /* Samsung I330 phone cradle */
291         UPLCOM_DEV(SANWA, KB_USB2),             /* Sanwa KB-USB2 Multimeter cable */
292         UPLCOM_DEV(SIEMENS3, EF81),             /* Siemens EF81 */
293         UPLCOM_DEV(SIEMENS3, SX1),              /* Siemens SX1 */
294         UPLCOM_DEV(SIEMENS3, X65),              /* Siemens X65 */
295         UPLCOM_DEV(SIEMENS3, X75),              /* Siemens X75 */
296         UPLCOM_DEV(SITECOM, SERIAL),            /* Sitecom USB to Serial */
297         UPLCOM_DEV(SMART, PL2303),              /* SMART Technologies USB to Serial */
298         UPLCOM_DEV(SONY, QN3),                  /* Sony QN3 phone cable */
299         UPLCOM_DEV(SONYERICSSON, DATAPILOT),    /* Sony Ericsson Datapilot */
300         UPLCOM_DEV(SONYERICSSON, DCU10),        /* Sony Ericsson DCU-10 Cable */
301         UPLCOM_DEV(SOURCENEXT, KEIKAI8),        /* SOURCENEXT KeikaiDenwa 8 */
302         UPLCOM_DEV(SOURCENEXT, KEIKAI8_CHG),    /* SOURCENEXT KeikaiDenwa 8 with charger */
303         UPLCOM_DEV(SPEEDDRAGON, MS3303H),       /* Speed Dragon USB-Serial */
304         UPLCOM_DEV(SYNTECH, CPT8001C),          /* Syntech CPT-8001C Barcode scanner */
305         UPLCOM_DEV(TDK, UHA6400),               /* TDK USB-PHS Adapter UHA6400 */
306         UPLCOM_DEV(TDK, UPA9664),               /* TDK USB-PHS Adapter UPA9664 */
307         UPLCOM_DEV(TRIPPLITE, U209),            /* Tripp-Lite U209-000-R USB to Serial */
308         UPLCOM_DEV(YCCABLE, PL2303),            /* YC Cable USB-Serial */
309 };
310 #undef UPLCOM_DEV
311
312 static device_method_t uplcom_methods[] = {
313         DEVMETHOD(device_probe, uplcom_probe),
314         DEVMETHOD(device_attach, uplcom_attach),
315         DEVMETHOD(device_detach, uplcom_detach),
316         DEVMETHOD_END
317 };
318
319 static devclass_t uplcom_devclass;
320
321 static driver_t uplcom_driver = {
322         .name = "uplcom",
323         .methods = uplcom_methods,
324         .size = sizeof(struct uplcom_softc),
325 };
326
327 DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0);
328 MODULE_DEPEND(uplcom, ucom, 1, 1, 1);
329 MODULE_DEPEND(uplcom, usb, 1, 1, 1);
330 MODULE_VERSION(uplcom, UPLCOM_MODVER);
331 USB_PNP_HOST_INFO(uplcom_devs);
332
333 static int
334 uplcom_probe(device_t dev)
335 {
336         struct usb_attach_arg *uaa = device_get_ivars(dev);
337
338         DPRINTFN(11, "\n");
339
340         if (uaa->usb_mode != USB_MODE_HOST) {
341                 return (ENXIO);
342         }
343         if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
344                 return (ENXIO);
345         }
346         if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
347                 return (ENXIO);
348         }
349         return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
350 }
351
352 static int
353 uplcom_attach(device_t dev)
354 {
355         struct usb_attach_arg *uaa = device_get_ivars(dev);
356         struct uplcom_softc *sc = device_get_softc(dev);
357         struct usb_interface *iface;
358         struct usb_interface_descriptor *id;
359         struct usb_device_descriptor *dd;
360         int error;
361
362         DPRINTFN(11, "\n");
363
364         device_set_usb_desc(dev);
365         mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF);
366         ucom_ref(&sc->sc_super_ucom);
367
368         DPRINTF("sc = %p\n", sc);
369
370         sc->sc_udev = uaa->device;
371
372         /* Determine the chip type.  This algorithm is taken from Linux. */
373         dd = usbd_get_device_descriptor(sc->sc_udev);
374         if (dd->bDeviceClass == 0x02)
375                 sc->sc_chiptype = TYPE_PL2303;
376         else if (dd->bMaxPacketSize == 0x40)
377                 sc->sc_chiptype = TYPE_PL2303HX;
378         else
379                 sc->sc_chiptype = TYPE_PL2303;
380
381         DPRINTF("chiptype: %s\n",
382             (sc->sc_chiptype == TYPE_PL2303HX) ?
383             "2303X" : "2303");
384
385         /*
386          * USB-RSAQ1 has two interface
387          *
388          *  USB-RSAQ1       | USB-RSAQ2
389          * -----------------+-----------------
390          * Interface 0      |Interface 0
391          *  Interrupt(0x81) | Interrupt(0x81)
392          * -----------------+ BulkIN(0x02)
393          * Interface 1      | BulkOUT(0x83)
394          *   BulkIN(0x02)   |
395          *   BulkOUT(0x83)  |
396          */
397
398         sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
399         sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
400
401         iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
402         if (iface) {
403                 id = usbd_get_interface_descriptor(iface);
404                 if (id == NULL) {
405                         device_printf(dev, "no interface descriptor (2)\n");
406                         goto detach;
407                 }
408                 sc->sc_data_iface_no = id->bInterfaceNumber;
409                 sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
410                 usbd_set_parent_iface(uaa->device,
411                     UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
412         } else {
413                 sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
414                 sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
415         }
416
417         error = usbd_transfer_setup(uaa->device,
418             sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
419             UPLCOM_N_TRANSFER, sc, &sc->sc_mtx);
420         if (error) {
421                 DPRINTF("one or more missing USB endpoints, "
422                     "error=%s\n", usbd_errstr(error));
423                 goto detach;
424         }
425         error = uplcom_reset(sc, uaa->device);
426         if (error) {
427                 device_printf(dev, "reset failed, error=%s\n",
428                     usbd_errstr(error));
429                 goto detach;
430         }
431
432         if (sc->sc_chiptype != TYPE_PL2303HX) {
433                 /* HX variants seem to lock up after a clear stall request. */
434                 mtx_lock(&sc->sc_mtx);
435                 usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
436                 usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
437                 mtx_unlock(&sc->sc_mtx);
438         } else {
439                 if (uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
440                     UPLCOM_SET_REQUEST, 8, 0, 0) ||
441                     uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
442                     UPLCOM_SET_REQUEST, 9, 0, 0)) {
443                         goto detach;
444                 }
445         }
446
447         error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
448             &uplcom_callback, &sc->sc_mtx);
449         if (error) {
450                 goto detach;
451         }
452         /*
453          * do the initialization during attach so that the system does not
454          * sleep during open:
455          */
456         if (uplcom_pl2303_init(uaa->device, sc->sc_chiptype)) {
457                 device_printf(dev, "init failed\n");
458                 goto detach;
459         }
460         ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
461
462         return (0);
463
464 detach:
465         uplcom_detach(dev);
466         return (ENXIO);
467 }
468
469 static int
470 uplcom_detach(device_t dev)
471 {
472         struct uplcom_softc *sc = device_get_softc(dev);
473
474         DPRINTF("sc=%p\n", sc);
475
476         ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
477         usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
478
479         device_claim_softc(dev);
480
481         uplcom_free_softc(sc);
482
483         return (0);
484 }
485
486 UCOM_UNLOAD_DRAIN(uplcom);
487
488 static void
489 uplcom_free_softc(struct uplcom_softc *sc)
490 {
491         if (ucom_unref(&sc->sc_super_ucom)) {
492                 mtx_destroy(&sc->sc_mtx);
493                 device_free_softc(sc);
494         }
495 }
496
497 static void
498 uplcom_free(struct ucom_softc *ucom)
499 {
500         uplcom_free_softc(ucom->sc_parent);
501 }
502
503 static usb_error_t
504 uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev)
505 {
506         struct usb_device_request req;
507
508         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
509         req.bRequest = UPLCOM_SET_REQUEST;
510         USETW(req.wValue, 0);
511         req.wIndex[0] = sc->sc_data_iface_no;
512         req.wIndex[1] = 0;
513         USETW(req.wLength, 0);
514
515         return (usbd_do_request(udev, NULL, &req, NULL));
516 }
517
518 static usb_error_t
519 uplcom_pl2303_do(struct usb_device *udev, uint8_t req_type, uint8_t request,
520     uint16_t value, uint16_t index, uint16_t length)
521 {
522         struct usb_device_request req;
523         usb_error_t err;
524         uint8_t buf[4];
525
526         req.bmRequestType = req_type;
527         req.bRequest = request;
528         USETW(req.wValue, value);
529         USETW(req.wIndex, index);
530         USETW(req.wLength, length);
531
532         err = usbd_do_request(udev, NULL, &req, buf);
533         if (err) {
534                 DPRINTF("error=%s\n", usbd_errstr(err));
535                 return (1);
536         }
537         return (0);
538 }
539
540 static int
541 uplcom_pl2303_init(struct usb_device *udev, uint8_t chiptype)
542 {
543         int err;
544
545         if (uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
546             || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0)
547             || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
548             || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
549             || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
550             || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0)
551             || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
552             || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
553             || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0)
554             || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0))
555                 return (EIO);
556
557         if (chiptype == TYPE_PL2303HX)
558                 err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0);
559         else
560                 err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x24, 0);
561         if (err)
562                 return (EIO);
563         
564         return (0);
565 }
566
567 static void
568 uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
569 {
570         struct uplcom_softc *sc = ucom->sc_parent;
571         struct usb_device_request req;
572
573         DPRINTF("onoff = %d\n", onoff);
574
575         if (onoff)
576                 sc->sc_line |= UCDC_LINE_DTR;
577         else
578                 sc->sc_line &= ~UCDC_LINE_DTR;
579
580         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
581         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
582         USETW(req.wValue, sc->sc_line);
583         req.wIndex[0] = sc->sc_data_iface_no;
584         req.wIndex[1] = 0;
585         USETW(req.wLength, 0);
586
587         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
588             &req, NULL, 0, 1000);
589 }
590
591 static void
592 uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
593 {
594         struct uplcom_softc *sc = ucom->sc_parent;
595         struct usb_device_request req;
596
597         DPRINTF("onoff = %d\n", onoff);
598
599         if (onoff)
600                 sc->sc_line |= UCDC_LINE_RTS;
601         else
602                 sc->sc_line &= ~UCDC_LINE_RTS;
603
604         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
605         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
606         USETW(req.wValue, sc->sc_line);
607         req.wIndex[0] = sc->sc_data_iface_no;
608         req.wIndex[1] = 0;
609         USETW(req.wLength, 0);
610
611         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
612             &req, NULL, 0, 1000);
613 }
614
615 static void
616 uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
617 {
618         struct uplcom_softc *sc = ucom->sc_parent;
619         struct usb_device_request req;
620         uint16_t temp;
621
622         DPRINTF("onoff = %d\n", onoff);
623
624         temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
625
626         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
627         req.bRequest = UCDC_SEND_BREAK;
628         USETW(req.wValue, temp);
629         req.wIndex[0] = sc->sc_data_iface_no;
630         req.wIndex[1] = 0;
631         USETW(req.wLength, 0);
632
633         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
634             &req, NULL, 0, 1000);
635 }
636
637 static const uint32_t uplcom_rates[] = {
638         75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
639         19200, 28800, 38400, 57600, 115200,
640         /*
641          * Higher speeds are probably possible. PL2303X supports up to
642          * 6Mb and can set any rate
643          */
644         230400, 460800, 614400, 921600, 1228800
645 };
646
647 #define N_UPLCOM_RATES  nitems(uplcom_rates)
648
649 static int
650 uplcom_pre_param(struct ucom_softc *ucom, struct termios *t)
651 {
652         struct uplcom_softc *sc = ucom->sc_parent;
653         uint8_t i;
654
655         DPRINTF("\n");
656
657         /**
658          * Check requested baud rate.
659          *
660          * The PL2303 can only set specific baud rates, up to 1228800 baud.
661          * The PL2303X can set any baud rate up to 6Mb.
662          * The PL2303HX rev. D can set any baud rate up to 12Mb.
663          *
664          * XXX: We currently cannot identify the PL2303HX rev. D, so treat
665          *      it the same as the PL2303X.
666          */
667         if (sc->sc_chiptype != TYPE_PL2303HX) {
668                 for (i = 0; i < N_UPLCOM_RATES; i++) {
669                         if (uplcom_rates[i] == t->c_ospeed)
670                                 return (0);
671                 }
672         } else {
673                 if (t->c_ospeed <= 6000000)
674                         return (0);
675         }
676
677         DPRINTF("uplcom_param: bad baud rate (%d)\n", t->c_ospeed);
678         return (EIO);
679 }
680
681 static void
682 uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
683 {
684         struct uplcom_softc *sc = ucom->sc_parent;
685         struct usb_cdc_line_state ls;
686         struct usb_device_request req;
687
688         DPRINTF("sc = %p\n", sc);
689
690         memset(&ls, 0, sizeof(ls));
691
692         USETDW(ls.dwDTERate, t->c_ospeed);
693
694         if (t->c_cflag & CSTOPB) {
695                 ls.bCharFormat = UCDC_STOP_BIT_2;
696         } else {
697                 ls.bCharFormat = UCDC_STOP_BIT_1;
698         }
699
700         if (t->c_cflag & PARENB) {
701                 if (t->c_cflag & PARODD) {
702                         ls.bParityType = UCDC_PARITY_ODD;
703                 } else {
704                         ls.bParityType = UCDC_PARITY_EVEN;
705                 }
706         } else {
707                 ls.bParityType = UCDC_PARITY_NONE;
708         }
709
710         switch (t->c_cflag & CSIZE) {
711         case CS5:
712                 ls.bDataBits = 5;
713                 break;
714         case CS6:
715                 ls.bDataBits = 6;
716                 break;
717         case CS7:
718                 ls.bDataBits = 7;
719                 break;
720         case CS8:
721                 ls.bDataBits = 8;
722                 break;
723         }
724
725         DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
726             UGETDW(ls.dwDTERate), ls.bCharFormat,
727             ls.bParityType, ls.bDataBits);
728
729         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
730         req.bRequest = UCDC_SET_LINE_CODING;
731         USETW(req.wValue, 0);
732         req.wIndex[0] = sc->sc_data_iface_no;
733         req.wIndex[1] = 0;
734         USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
735
736         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
737             &req, &ls, 0, 1000);
738
739         if (t->c_cflag & CRTSCTS) {
740
741                 DPRINTF("crtscts = on\n");
742
743                 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
744                 req.bRequest = UPLCOM_SET_REQUEST;
745                 USETW(req.wValue, 0);
746                 if (sc->sc_chiptype == TYPE_PL2303HX)
747                         USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
748                 else
749                         USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
750                 USETW(req.wLength, 0);
751
752                 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
753                     &req, NULL, 0, 1000);
754         } else {
755                 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
756                 req.bRequest = UPLCOM_SET_REQUEST;
757                 USETW(req.wValue, 0);
758                 USETW(req.wIndex, 0);
759                 USETW(req.wLength, 0);
760                 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
761                     &req, NULL, 0, 1000);
762         }
763 }
764
765 static void
766 uplcom_start_read(struct ucom_softc *ucom)
767 {
768         struct uplcom_softc *sc = ucom->sc_parent;
769
770         /* start interrupt endpoint */
771         usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
772
773         /* start read endpoint */
774         usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
775 }
776
777 static void
778 uplcom_stop_read(struct ucom_softc *ucom)
779 {
780         struct uplcom_softc *sc = ucom->sc_parent;
781
782         /* stop interrupt endpoint */
783         usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
784
785         /* stop read endpoint */
786         usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
787 }
788
789 static void
790 uplcom_start_write(struct ucom_softc *ucom)
791 {
792         struct uplcom_softc *sc = ucom->sc_parent;
793
794         usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
795 }
796
797 static void
798 uplcom_stop_write(struct ucom_softc *ucom)
799 {
800         struct uplcom_softc *sc = ucom->sc_parent;
801
802         usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
803 }
804
805 static void
806 uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
807 {
808         struct uplcom_softc *sc = ucom->sc_parent;
809
810         DPRINTF("\n");
811
812         /* XXX Note: sc_lsr is always zero */
813         *lsr = sc->sc_lsr;
814         *msr = sc->sc_msr;
815 }
816
817 static void
818 uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
819 {
820         struct uplcom_softc *sc = usbd_xfer_softc(xfer);
821         struct usb_page_cache *pc;
822         uint8_t buf[9];
823         int actlen;
824
825         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
826
827         switch (USB_GET_STATE(xfer)) {
828         case USB_ST_TRANSFERRED:
829
830                 DPRINTF("actlen = %u\n", actlen);
831
832                 if (actlen >= 9) {
833
834                         pc = usbd_xfer_get_frame(xfer, 0);
835                         usbd_copy_out(pc, 0, buf, sizeof(buf));
836
837                         DPRINTF("status = 0x%02x\n", buf[8]);
838
839                         sc->sc_lsr = 0;
840                         sc->sc_msr = 0;
841
842                         if (buf[8] & RSAQ_STATUS_CTS) {
843                                 sc->sc_msr |= SER_CTS;
844                         }
845                         if (buf[8] & RSAQ_STATUS_DSR) {
846                                 sc->sc_msr |= SER_DSR;
847                         }
848                         if (buf[8] & RSAQ_STATUS_DCD) {
849                                 sc->sc_msr |= SER_DCD;
850                         }
851                         ucom_status_change(&sc->sc_ucom);
852                 }
853         case USB_ST_SETUP:
854 tr_setup:
855                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
856                 usbd_transfer_submit(xfer);
857                 return;
858
859         default:                        /* Error */
860                 if (error != USB_ERR_CANCELLED) {
861                         /* try to clear stall first */
862                         usbd_xfer_set_stall(xfer);
863                         goto tr_setup;
864                 }
865                 return;
866         }
867 }
868
869 static void
870 uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
871 {
872         struct uplcom_softc *sc = usbd_xfer_softc(xfer);
873         struct usb_page_cache *pc;
874         uint32_t actlen;
875
876         switch (USB_GET_STATE(xfer)) {
877         case USB_ST_SETUP:
878         case USB_ST_TRANSFERRED:
879 tr_setup:
880                 pc = usbd_xfer_get_frame(xfer, 0);
881                 if (ucom_get_data(&sc->sc_ucom, pc, 0,
882                     UPLCOM_BULK_BUF_SIZE, &actlen)) {
883
884                         DPRINTF("actlen = %d\n", actlen);
885
886                         usbd_xfer_set_frame_len(xfer, 0, actlen);
887                         usbd_transfer_submit(xfer);
888                 }
889                 return;
890
891         default:                        /* Error */
892                 if (error != USB_ERR_CANCELLED) {
893                         /* try to clear stall first */
894                         usbd_xfer_set_stall(xfer);
895                         goto tr_setup;
896                 }
897                 return;
898         }
899 }
900
901 static void
902 uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
903 {
904         struct uplcom_softc *sc = usbd_xfer_softc(xfer);
905         struct usb_page_cache *pc;
906         int actlen;
907
908         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
909
910         switch (USB_GET_STATE(xfer)) {
911         case USB_ST_TRANSFERRED:
912                 pc = usbd_xfer_get_frame(xfer, 0);
913                 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
914
915         case USB_ST_SETUP:
916 tr_setup:
917                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
918                 usbd_transfer_submit(xfer);
919                 return;
920
921         default:                        /* Error */
922                 if (error != USB_ERR_CANCELLED) {
923                         /* try to clear stall first */
924                         usbd_xfer_set_stall(xfer);
925                         goto tr_setup;
926                 }
927                 return;
928         }
929 }
930
931 static void
932 uplcom_poll(struct ucom_softc *ucom)
933 {
934         struct uplcom_softc *sc = ucom->sc_parent;
935         usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER);
936 }