]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/usb/serial/uplcom.c
MFC overview:
[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/module.h>
96 #include <sys/lock.h>
97 #include <sys/mutex.h>
98 #include <sys/condvar.h>
99 #include <sys/sysctl.h>
100 #include <sys/sx.h>
101 #include <sys/unistd.h>
102 #include <sys/callout.h>
103 #include <sys/malloc.h>
104 #include <sys/priv.h>
105
106 #include <dev/usb/usb.h>
107 #include <dev/usb/usbdi.h>
108 #include <dev/usb/usbdi_util.h>
109 #include <dev/usb/usb_cdc.h>
110 #include "usbdevs.h"
111
112 #define USB_DEBUG_VAR uplcom_debug
113 #include <dev/usb/usb_debug.h>
114 #include <dev/usb/usb_process.h>
115
116 #include <dev/usb/serial/usb_serial.h>
117
118 #ifdef USB_DEBUG
119 static int uplcom_debug = 0;
120
121 SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom");
122 SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RW,
123     &uplcom_debug, 0, "Debug level");
124 #endif
125
126 #define UPLCOM_MODVER                   1       /* module version */
127
128 #define UPLCOM_CONFIG_INDEX             0
129 #define UPLCOM_IFACE_INDEX              0
130 #define UPLCOM_SECOND_IFACE_INDEX       1
131
132 #ifndef UPLCOM_INTR_INTERVAL
133 #define UPLCOM_INTR_INTERVAL            0       /* default */
134 #endif
135
136 #define UPLCOM_BULK_BUF_SIZE 1024       /* bytes */
137
138 #define UPLCOM_SET_REQUEST              0x01
139 #define UPLCOM_SET_CRTSCTS              0x41
140 #define UPLCOM_SET_CRTSCTS_PL2303X      0x61
141 #define RSAQ_STATUS_CTS                 0x80
142 #define RSAQ_STATUS_DSR                 0x02
143 #define RSAQ_STATUS_DCD                 0x01
144
145 #define TYPE_PL2303                     0
146 #define TYPE_PL2303HX                   1
147
148 enum {
149         UPLCOM_BULK_DT_WR,
150         UPLCOM_BULK_DT_RD,
151         UPLCOM_INTR_DT_RD,
152         UPLCOM_N_TRANSFER,
153 };
154
155 struct uplcom_softc {
156         struct ucom_super_softc sc_super_ucom;
157         struct ucom_softc sc_ucom;
158
159         struct usb_xfer *sc_xfer[UPLCOM_N_TRANSFER];
160         struct usb_device *sc_udev;
161         struct mtx sc_mtx;
162
163         uint16_t sc_line;
164
165         uint8_t sc_lsr;                 /* local status register */
166         uint8_t sc_msr;                 /* uplcom status register */
167         uint8_t sc_chiptype;            /* type of chip */
168         uint8_t sc_ctrl_iface_no;
169         uint8_t sc_data_iface_no;
170         uint8_t sc_iface_index[2];
171 };
172
173 /* prototypes */
174
175 static usb_error_t uplcom_reset(struct uplcom_softc *, struct usb_device *);
176 static usb_error_t uplcom_pl2303_do(struct usb_device *, int8_t, uint8_t,
177                         uint16_t, uint16_t, uint16_t);
178 static int      uplcom_pl2303_init(struct usb_device *, uint8_t);
179 static void     uplcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
180 static void     uplcom_cfg_set_rts(struct ucom_softc *, uint8_t);
181 static void     uplcom_cfg_set_break(struct ucom_softc *, uint8_t);
182 static int      uplcom_pre_param(struct ucom_softc *, struct termios *);
183 static void     uplcom_cfg_param(struct ucom_softc *, struct termios *);
184 static void     uplcom_start_read(struct ucom_softc *);
185 static void     uplcom_stop_read(struct ucom_softc *);
186 static void     uplcom_start_write(struct ucom_softc *);
187 static void     uplcom_stop_write(struct ucom_softc *);
188 static void     uplcom_cfg_get_status(struct ucom_softc *, uint8_t *,
189                     uint8_t *);
190 static void     uplcom_poll(struct ucom_softc *ucom);
191
192 static device_probe_t uplcom_probe;
193 static device_attach_t uplcom_attach;
194 static device_detach_t uplcom_detach;
195
196 static usb_callback_t uplcom_intr_callback;
197 static usb_callback_t uplcom_write_callback;
198 static usb_callback_t uplcom_read_callback;
199
200 static const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = {
201
202         [UPLCOM_BULK_DT_WR] = {
203                 .type = UE_BULK,
204                 .endpoint = UE_ADDR_ANY,
205                 .direction = UE_DIR_OUT,
206                 .bufsize = UPLCOM_BULK_BUF_SIZE,
207                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
208                 .callback = &uplcom_write_callback,
209                 .if_index = 0,
210         },
211
212         [UPLCOM_BULK_DT_RD] = {
213                 .type = UE_BULK,
214                 .endpoint = UE_ADDR_ANY,
215                 .direction = UE_DIR_IN,
216                 .bufsize = UPLCOM_BULK_BUF_SIZE,
217                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
218                 .callback = &uplcom_read_callback,
219                 .if_index = 0,
220         },
221
222         [UPLCOM_INTR_DT_RD] = {
223                 .type = UE_INTERRUPT,
224                 .endpoint = UE_ADDR_ANY,
225                 .direction = UE_DIR_IN,
226                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
227                 .bufsize = 0,   /* use wMaxPacketSize */
228                 .callback = &uplcom_intr_callback,
229                 .if_index = 1,
230         },
231 };
232
233 static struct ucom_callback uplcom_callback = {
234         .ucom_cfg_get_status = &uplcom_cfg_get_status,
235         .ucom_cfg_set_dtr = &uplcom_cfg_set_dtr,
236         .ucom_cfg_set_rts = &uplcom_cfg_set_rts,
237         .ucom_cfg_set_break = &uplcom_cfg_set_break,
238         .ucom_cfg_param = &uplcom_cfg_param,
239         .ucom_pre_param = &uplcom_pre_param,
240         .ucom_start_read = &uplcom_start_read,
241         .ucom_stop_read = &uplcom_stop_read,
242         .ucom_start_write = &uplcom_start_write,
243         .ucom_stop_write = &uplcom_stop_write,
244         .ucom_poll = &uplcom_poll,
245 };
246
247 #define UPLCOM_DEV(v,p)                         \
248   { USB_VENDOR(USB_VENDOR_##v), USB_PRODUCT(USB_PRODUCT_##v##_##p) }
249
250 static const struct usb_device_id uplcom_devs[] = {
251         UPLCOM_DEV(ACERP, S81),                 /* BenQ S81 phone */
252         UPLCOM_DEV(ADLINK, ND6530),             /* ADLINK ND-6530 USB-Serial */
253         UPLCOM_DEV(ALCATEL, OT535),             /* Alcatel One Touch 535/735 */
254         UPLCOM_DEV(ALCOR, AU9720),              /* Alcor AU9720 USB 2.0-RS232 */
255         UPLCOM_DEV(ANCHOR, SERIAL),             /* Anchor Serial adapter */
256         UPLCOM_DEV(ATEN, UC232A),               /* PLANEX USB-RS232 URS-03 */
257         UPLCOM_DEV(BELKIN, F5U257),             /* Belkin F5U257 USB to Serial */
258         UPLCOM_DEV(COREGA, CGUSBRS232R),        /* Corega CG-USBRS232R */
259         UPLCOM_DEV(EPSON, CRESSI_EDY),          /* Cressi Edy diving computer */
260         UPLCOM_DEV(ELECOM, UCSGT),              /* ELECOM UC-SGT Serial Adapter */
261         UPLCOM_DEV(ELECOM, UCSGT0),             /* ELECOM UC-SGT Serial Adapter */
262         UPLCOM_DEV(HAL, IMR001),                /* HAL Corporation Crossam2+USB */
263         UPLCOM_DEV(HP, LD220),                  /* HP LD220 POS Display */
264         UPLCOM_DEV(IODATA, USBRSAQ),            /* I/O DATA USB-RSAQ */
265         UPLCOM_DEV(IODATA, USBRSAQ5),           /* I/O DATA USB-RSAQ5 */
266         UPLCOM_DEV(ITEGNO, WM1080A),            /* iTegno WM1080A GSM/GFPRS modem */
267         UPLCOM_DEV(ITEGNO, WM2080A),            /* iTegno WM2080A CDMA modem */
268         UPLCOM_DEV(LEADTEK, 9531),              /* Leadtek 9531 GPS */
269         UPLCOM_DEV(MICROSOFT, 700WX),           /* Microsoft Palm 700WX */
270         UPLCOM_DEV(MOBILEACTION, MA620),        /* Mobile Action MA-620 Infrared Adapter */
271         UPLCOM_DEV(NETINDEX, WS002IN),          /* Willcom W-S002IN */
272         UPLCOM_DEV(NOKIA2, CA42),               /* Nokia CA-42 cable */
273         UPLCOM_DEV(OTI, DKU5),                  /* OTI DKU-5 cable */
274         UPLCOM_DEV(PANASONIC, TYTP50P6S),       /* Panasonic TY-TP50P6-S flat screen */
275         UPLCOM_DEV(PLX, CA42),                  /* PLX CA-42 clone cable */
276         UPLCOM_DEV(PROLIFIC, ALLTRONIX_GPRS),   /* Alltronix ACM003U00 modem */
277         UPLCOM_DEV(PROLIFIC, ALDIGA_AL11U),     /* AlDiga AL-11U modem */
278         UPLCOM_DEV(PROLIFIC, DCU11),            /* DCU-11 Phone Cable */
279         UPLCOM_DEV(PROLIFIC, HCR331),           /* HCR331 Card Reader */
280         UPLCOM_DEV(PROLIFIC, MICROMAX_610U),    /* Micromax 610U modem */
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         {0, 0}
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
332 static int
333 uplcom_probe(device_t dev)
334 {
335         struct usb_attach_arg *uaa = device_get_ivars(dev);
336
337         DPRINTFN(11, "\n");
338
339         if (uaa->usb_mode != USB_MODE_HOST) {
340                 return (ENXIO);
341         }
342         if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
343                 return (ENXIO);
344         }
345         if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
346                 return (ENXIO);
347         }
348         return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
349 }
350
351 static int
352 uplcom_attach(device_t dev)
353 {
354         struct usb_attach_arg *uaa = device_get_ivars(dev);
355         struct uplcom_softc *sc = device_get_softc(dev);
356         struct usb_interface *iface;
357         struct usb_interface_descriptor *id;
358         struct usb_device_descriptor *dd;
359         int error;
360
361         DPRINTFN(11, "\n");
362
363         device_set_usb_desc(dev);
364         mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF);
365
366         DPRINTF("sc = %p\n", sc);
367
368         sc->sc_udev = uaa->device;
369
370         /* Determine the chip type.  This algorithm is taken from Linux. */
371         dd = usbd_get_device_descriptor(sc->sc_udev);
372         if (dd->bDeviceClass == 0x02)
373                 sc->sc_chiptype = TYPE_PL2303;
374         else if (dd->bMaxPacketSize == 0x40)
375                 sc->sc_chiptype = TYPE_PL2303HX;
376         else
377                 sc->sc_chiptype = TYPE_PL2303;
378
379         DPRINTF("chiptype: %s\n",
380             (sc->sc_chiptype == TYPE_PL2303HX) ?
381             "2303X" : "2303");
382
383         /*
384          * USB-RSAQ1 has two interface
385          *
386          *  USB-RSAQ1       | USB-RSAQ2
387          * -----------------+-----------------
388          * Interface 0      |Interface 0
389          *  Interrupt(0x81) | Interrupt(0x81)
390          * -----------------+ BulkIN(0x02)
391          * Interface 1      | BulkOUT(0x83)
392          *   BulkIN(0x02)   |
393          *   BulkOUT(0x83)  |
394          */
395
396         sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
397         sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
398
399         iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
400         if (iface) {
401                 id = usbd_get_interface_descriptor(iface);
402                 if (id == NULL) {
403                         device_printf(dev, "no interface descriptor (2)\n");
404                         goto detach;
405                 }
406                 sc->sc_data_iface_no = id->bInterfaceNumber;
407                 sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
408                 usbd_set_parent_iface(uaa->device,
409                     UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
410         } else {
411                 sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
412                 sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
413         }
414
415         error = usbd_transfer_setup(uaa->device,
416             sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
417             UPLCOM_N_TRANSFER, sc, &sc->sc_mtx);
418         if (error) {
419                 DPRINTF("one or more missing USB endpoints, "
420                     "error=%s\n", usbd_errstr(error));
421                 goto detach;
422         }
423         error = uplcom_reset(sc, uaa->device);
424         if (error) {
425                 device_printf(dev, "reset failed, error=%s\n",
426                     usbd_errstr(error));
427                 goto detach;
428         }
429         /* clear stall at first run */
430         mtx_lock(&sc->sc_mtx);
431         usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
432         usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
433         mtx_unlock(&sc->sc_mtx);
434
435         error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
436             &uplcom_callback, &sc->sc_mtx);
437         if (error) {
438                 goto detach;
439         }
440         /*
441          * do the initialization during attach so that the system does not
442          * sleep during open:
443          */
444         if (uplcom_pl2303_init(uaa->device, sc->sc_chiptype)) {
445                 device_printf(dev, "init failed\n");
446                 goto detach;
447         }
448         ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
449
450         return (0);
451
452 detach:
453         uplcom_detach(dev);
454         return (ENXIO);
455 }
456
457 static int
458 uplcom_detach(device_t dev)
459 {
460         struct uplcom_softc *sc = device_get_softc(dev);
461
462         DPRINTF("sc=%p\n", sc);
463
464         ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
465         usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
466         mtx_destroy(&sc->sc_mtx);
467
468         return (0);
469 }
470
471 static usb_error_t
472 uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev)
473 {
474         struct usb_device_request req;
475
476         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
477         req.bRequest = UPLCOM_SET_REQUEST;
478         USETW(req.wValue, 0);
479         req.wIndex[0] = sc->sc_data_iface_no;
480         req.wIndex[1] = 0;
481         USETW(req.wLength, 0);
482
483         return (usbd_do_request(udev, NULL, &req, NULL));
484 }
485
486 static usb_error_t
487 uplcom_pl2303_do(struct usb_device *udev, int8_t req_type, uint8_t request,
488     uint16_t value, uint16_t index, uint16_t length)
489 {
490         struct usb_device_request req;
491         usb_error_t err;
492         uint8_t buf[4];
493
494         req.bmRequestType = req_type;
495         req.bRequest = request;
496         USETW(req.wValue, value);
497         USETW(req.wIndex, index);
498         USETW(req.wLength, length);
499
500         err = usbd_do_request(udev, NULL, &req, buf);
501         if (err) {
502                 DPRINTF("error=%s\n", usbd_errstr(err));
503                 return (1);
504         }
505         return (0);
506 }
507
508 static int
509 uplcom_pl2303_init(struct usb_device *udev, uint8_t chiptype)
510 {
511         int err;
512
513         if (uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
514             || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0)
515             || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
516             || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
517             || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
518             || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0)
519             || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
520             || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
521             || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0)
522             || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0))
523                 return (EIO);
524
525         if (chiptype == TYPE_PL2303HX)
526                 err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0);
527         else
528                 err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x24, 0);
529         if (err)
530                 return (EIO);
531         
532         if (uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 8, 0, 0)
533             || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 9, 0, 0))
534                 return (EIO);
535         return (0);
536 }
537
538 static void
539 uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
540 {
541         struct uplcom_softc *sc = ucom->sc_parent;
542         struct usb_device_request req;
543
544         DPRINTF("onoff = %d\n", onoff);
545
546         if (onoff)
547                 sc->sc_line |= UCDC_LINE_DTR;
548         else
549                 sc->sc_line &= ~UCDC_LINE_DTR;
550
551         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
552         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
553         USETW(req.wValue, sc->sc_line);
554         req.wIndex[0] = sc->sc_data_iface_no;
555         req.wIndex[1] = 0;
556         USETW(req.wLength, 0);
557
558         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
559             &req, NULL, 0, 1000);
560 }
561
562 static void
563 uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
564 {
565         struct uplcom_softc *sc = ucom->sc_parent;
566         struct usb_device_request req;
567
568         DPRINTF("onoff = %d\n", onoff);
569
570         if (onoff)
571                 sc->sc_line |= UCDC_LINE_RTS;
572         else
573                 sc->sc_line &= ~UCDC_LINE_RTS;
574
575         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
576         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
577         USETW(req.wValue, sc->sc_line);
578         req.wIndex[0] = sc->sc_data_iface_no;
579         req.wIndex[1] = 0;
580         USETW(req.wLength, 0);
581
582         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
583             &req, NULL, 0, 1000);
584 }
585
586 static void
587 uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
588 {
589         struct uplcom_softc *sc = ucom->sc_parent;
590         struct usb_device_request req;
591         uint16_t temp;
592
593         DPRINTF("onoff = %d\n", onoff);
594
595         temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
596
597         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
598         req.bRequest = UCDC_SEND_BREAK;
599         USETW(req.wValue, temp);
600         req.wIndex[0] = sc->sc_data_iface_no;
601         req.wIndex[1] = 0;
602         USETW(req.wLength, 0);
603
604         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
605             &req, NULL, 0, 1000);
606 }
607
608 static const int32_t uplcom_rates[] = {
609         75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
610         19200, 28800, 38400, 57600, 115200,
611         /*
612          * Higher speeds are probably possible. PL2303X supports up to
613          * 6Mb and can set any rate
614          */
615         230400, 460800, 614400, 921600, 1228800
616 };
617
618 #define N_UPLCOM_RATES  (sizeof(uplcom_rates)/sizeof(uplcom_rates[0]))
619
620 static int
621 uplcom_pre_param(struct ucom_softc *ucom, struct termios *t)
622 {
623         struct uplcom_softc *sc = ucom->sc_parent;
624         uint8_t i;
625
626         DPRINTF("\n");
627
628         /**
629          * Check requested baud rate.
630          *
631          * The PL2303 can only set specific baud rates, up to 1228800 baud.
632          * The PL2303X can set any baud rate up to 6Mb.
633          * The PL2303HX rev. D can set any baud rate up to 12Mb.
634          *
635          * XXX: We currently cannot identify the PL2303HX rev. D, so treat
636          *      it the same as the PL2303X.
637          */
638         if (sc->sc_chiptype != TYPE_PL2303HX) {
639                 for (i = 0; i < N_UPLCOM_RATES; i++) {
640                         if (uplcom_rates[i] == t->c_ospeed)
641                                 return (0);
642                 }
643         } else {
644                 if (t->c_ospeed <= 6000000)
645                         return (0);
646         }
647
648         DPRINTF("uplcom_param: bad baud rate (%d)\n", t->c_ospeed);
649         return (EIO);
650 }
651
652 static void
653 uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
654 {
655         struct uplcom_softc *sc = ucom->sc_parent;
656         struct usb_cdc_line_state ls;
657         struct usb_device_request req;
658
659         DPRINTF("sc = %p\n", sc);
660
661         bzero(&ls, sizeof(ls));
662
663         USETDW(ls.dwDTERate, t->c_ospeed);
664
665         if (t->c_cflag & CSTOPB) {
666                 ls.bCharFormat = UCDC_STOP_BIT_2;
667         } else {
668                 ls.bCharFormat = UCDC_STOP_BIT_1;
669         }
670
671         if (t->c_cflag & PARENB) {
672                 if (t->c_cflag & PARODD) {
673                         ls.bParityType = UCDC_PARITY_ODD;
674                 } else {
675                         ls.bParityType = UCDC_PARITY_EVEN;
676                 }
677         } else {
678                 ls.bParityType = UCDC_PARITY_NONE;
679         }
680
681         switch (t->c_cflag & CSIZE) {
682         case CS5:
683                 ls.bDataBits = 5;
684                 break;
685         case CS6:
686                 ls.bDataBits = 6;
687                 break;
688         case CS7:
689                 ls.bDataBits = 7;
690                 break;
691         case CS8:
692                 ls.bDataBits = 8;
693                 break;
694         }
695
696         DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
697             UGETDW(ls.dwDTERate), ls.bCharFormat,
698             ls.bParityType, ls.bDataBits);
699
700         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
701         req.bRequest = UCDC_SET_LINE_CODING;
702         USETW(req.wValue, 0);
703         req.wIndex[0] = sc->sc_data_iface_no;
704         req.wIndex[1] = 0;
705         USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
706
707         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
708             &req, &ls, 0, 1000);
709
710         if (t->c_cflag & CRTSCTS) {
711
712                 DPRINTF("crtscts = on\n");
713
714                 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
715                 req.bRequest = UPLCOM_SET_REQUEST;
716                 USETW(req.wValue, 0);
717                 if (sc->sc_chiptype == TYPE_PL2303HX)
718                         USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
719                 else
720                         USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
721                 USETW(req.wLength, 0);
722
723                 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
724                     &req, NULL, 0, 1000);
725         } else {
726                 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
727                 req.bRequest = UPLCOM_SET_REQUEST;
728                 USETW(req.wValue, 0);
729                 USETW(req.wIndex, 0);
730                 USETW(req.wLength, 0);
731                 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
732                     &req, NULL, 0, 1000);
733         }
734 }
735
736 static void
737 uplcom_start_read(struct ucom_softc *ucom)
738 {
739         struct uplcom_softc *sc = ucom->sc_parent;
740
741         /* start interrupt endpoint */
742         usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
743
744         /* start read endpoint */
745         usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
746 }
747
748 static void
749 uplcom_stop_read(struct ucom_softc *ucom)
750 {
751         struct uplcom_softc *sc = ucom->sc_parent;
752
753         /* stop interrupt endpoint */
754         usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
755
756         /* stop read endpoint */
757         usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
758 }
759
760 static void
761 uplcom_start_write(struct ucom_softc *ucom)
762 {
763         struct uplcom_softc *sc = ucom->sc_parent;
764
765         usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
766 }
767
768 static void
769 uplcom_stop_write(struct ucom_softc *ucom)
770 {
771         struct uplcom_softc *sc = ucom->sc_parent;
772
773         usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
774 }
775
776 static void
777 uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
778 {
779         struct uplcom_softc *sc = ucom->sc_parent;
780
781         DPRINTF("\n");
782
783         *lsr = sc->sc_lsr;
784         *msr = sc->sc_msr;
785 }
786
787 static void
788 uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
789 {
790         struct uplcom_softc *sc = usbd_xfer_softc(xfer);
791         struct usb_page_cache *pc;
792         uint8_t buf[9];
793         int actlen;
794
795         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
796
797         switch (USB_GET_STATE(xfer)) {
798         case USB_ST_TRANSFERRED:
799
800                 DPRINTF("actlen = %u\n", actlen);
801
802                 if (actlen >= 9) {
803
804                         pc = usbd_xfer_get_frame(xfer, 0);
805                         usbd_copy_out(pc, 0, buf, sizeof(buf));
806
807                         DPRINTF("status = 0x%02x\n", buf[8]);
808
809                         sc->sc_lsr = 0;
810                         sc->sc_msr = 0;
811
812                         if (buf[8] & RSAQ_STATUS_CTS) {
813                                 sc->sc_msr |= SER_CTS;
814                         }
815                         if (buf[8] & RSAQ_STATUS_DSR) {
816                                 sc->sc_msr |= SER_DSR;
817                         }
818                         if (buf[8] & RSAQ_STATUS_DCD) {
819                                 sc->sc_msr |= SER_DCD;
820                         }
821                         ucom_status_change(&sc->sc_ucom);
822                 }
823         case USB_ST_SETUP:
824 tr_setup:
825                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
826                 usbd_transfer_submit(xfer);
827                 return;
828
829         default:                        /* Error */
830                 if (error != USB_ERR_CANCELLED) {
831                         /* try to clear stall first */
832                         usbd_xfer_set_stall(xfer);
833                         goto tr_setup;
834                 }
835                 return;
836         }
837 }
838
839 static void
840 uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
841 {
842         struct uplcom_softc *sc = usbd_xfer_softc(xfer);
843         struct usb_page_cache *pc;
844         uint32_t actlen;
845
846         switch (USB_GET_STATE(xfer)) {
847         case USB_ST_SETUP:
848         case USB_ST_TRANSFERRED:
849 tr_setup:
850                 pc = usbd_xfer_get_frame(xfer, 0);
851                 if (ucom_get_data(&sc->sc_ucom, pc, 0,
852                     UPLCOM_BULK_BUF_SIZE, &actlen)) {
853
854                         DPRINTF("actlen = %d\n", actlen);
855
856                         usbd_xfer_set_frame_len(xfer, 0, actlen);
857                         usbd_transfer_submit(xfer);
858                 }
859                 return;
860
861         default:                        /* Error */
862                 if (error != USB_ERR_CANCELLED) {
863                         /* try to clear stall first */
864                         usbd_xfer_set_stall(xfer);
865                         goto tr_setup;
866                 }
867                 return;
868         }
869 }
870
871 static void
872 uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
873 {
874         struct uplcom_softc *sc = usbd_xfer_softc(xfer);
875         struct usb_page_cache *pc;
876         int actlen;
877
878         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
879
880         switch (USB_GET_STATE(xfer)) {
881         case USB_ST_TRANSFERRED:
882                 pc = usbd_xfer_get_frame(xfer, 0);
883                 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
884
885         case USB_ST_SETUP:
886 tr_setup:
887                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
888                 usbd_transfer_submit(xfer);
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_poll(struct ucom_softc *ucom)
903 {
904         struct uplcom_softc *sc = ucom->sc_parent;
905         usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER);
906 }