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