102ac6454SAndrew Thompson /* $NetBSD: uplcom.c,v 1.21 2001/11/13 06:24:56 lukem Exp $ */
202ac6454SAndrew Thompson
302ac6454SAndrew Thompson /*-
4*eebd9d53SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
5718cf2ccSPedro F. Giffuni *
602ac6454SAndrew Thompson * Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
702ac6454SAndrew Thompson * All rights reserved.
802ac6454SAndrew Thompson *
902ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without
1002ac6454SAndrew Thompson * modification, are permitted provided that the following conditions
1102ac6454SAndrew Thompson * are met:
1202ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright
1302ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer.
1402ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright
1502ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the
1602ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution.
1702ac6454SAndrew Thompson *
1802ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1902ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2002ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2102ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2202ac6454SAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2302ac6454SAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2402ac6454SAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2502ac6454SAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2602ac6454SAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2702ac6454SAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2802ac6454SAndrew Thompson * SUCH DAMAGE.
2902ac6454SAndrew Thompson */
3002ac6454SAndrew Thompson
3102ac6454SAndrew Thompson /*-
3202ac6454SAndrew Thompson * Copyright (c) 2001 The NetBSD Foundation, Inc.
3302ac6454SAndrew Thompson * All rights reserved.
3402ac6454SAndrew Thompson *
3502ac6454SAndrew Thompson * This code is derived from software contributed to The NetBSD Foundation
3602ac6454SAndrew Thompson * by Ichiro FUKUHARA (ichiro@ichiro.org).
3702ac6454SAndrew Thompson *
3802ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without
3902ac6454SAndrew Thompson * modification, are permitted provided that the following conditions
4002ac6454SAndrew Thompson * are met:
4102ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright
4202ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer.
4302ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright
4402ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the
4502ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution.
4602ac6454SAndrew Thompson *
4702ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
4802ac6454SAndrew Thompson * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
4902ac6454SAndrew Thompson * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5002ac6454SAndrew Thompson * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
5102ac6454SAndrew Thompson * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5202ac6454SAndrew Thompson * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5302ac6454SAndrew Thompson * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5402ac6454SAndrew Thompson * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
5502ac6454SAndrew Thompson * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5602ac6454SAndrew Thompson * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
5702ac6454SAndrew Thompson * POSSIBILITY OF SUCH DAMAGE.
5802ac6454SAndrew Thompson */
5902ac6454SAndrew Thompson
6002ac6454SAndrew Thompson /*
6102ac6454SAndrew Thompson * This driver supports several USB-to-RS232 serial adapters driven by
6202ac6454SAndrew Thompson * Prolific PL-2303, PL-2303X and probably PL-2303HX USB-to-RS232
6302ac6454SAndrew Thompson * bridge chip. The adapters are sold under many different brand
6402ac6454SAndrew Thompson * names.
6502ac6454SAndrew Thompson *
6602ac6454SAndrew Thompson * Datasheets are available at Prolific www site at
6702ac6454SAndrew Thompson * http://www.prolific.com.tw. The datasheets don't contain full
6802ac6454SAndrew Thompson * programming information for the chip.
6902ac6454SAndrew Thompson *
7002ac6454SAndrew Thompson * PL-2303HX is probably programmed the same as PL-2303X.
7102ac6454SAndrew Thompson *
7202ac6454SAndrew Thompson * There are several differences between PL-2303 and PL-2303(H)X.
7302ac6454SAndrew Thompson * PL-2303(H)X can do higher bitrate in bulk mode, has _probably_
7402ac6454SAndrew Thompson * different command for controlling CRTSCTS and needs special
7502ac6454SAndrew Thompson * sequence of commands for initialization which aren't also
7602ac6454SAndrew Thompson * documented in the datasheet.
7702ac6454SAndrew Thompson */
7802ac6454SAndrew Thompson
79ed6d949aSAndrew Thompson #include <sys/stdint.h>
80ed6d949aSAndrew Thompson #include <sys/stddef.h>
81ed6d949aSAndrew Thompson #include <sys/param.h>
82ed6d949aSAndrew Thompson #include <sys/queue.h>
83ed6d949aSAndrew Thompson #include <sys/types.h>
84ed6d949aSAndrew Thompson #include <sys/systm.h>
85ed6d949aSAndrew Thompson #include <sys/kernel.h>
86ed6d949aSAndrew Thompson #include <sys/bus.h>
87ed6d949aSAndrew Thompson #include <sys/module.h>
88ed6d949aSAndrew Thompson #include <sys/lock.h>
89ed6d949aSAndrew Thompson #include <sys/mutex.h>
90ed6d949aSAndrew Thompson #include <sys/condvar.h>
91ed6d949aSAndrew Thompson #include <sys/sysctl.h>
92ed6d949aSAndrew Thompson #include <sys/sx.h>
93ed6d949aSAndrew Thompson #include <sys/unistd.h>
94ed6d949aSAndrew Thompson #include <sys/callout.h>
95ed6d949aSAndrew Thompson #include <sys/malloc.h>
96ed6d949aSAndrew Thompson #include <sys/priv.h>
97ed6d949aSAndrew Thompson
9802ac6454SAndrew Thompson #include <dev/usb/usb.h>
99ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
100ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
10102ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h>
102ed6d949aSAndrew Thompson #include "usbdevs.h"
10302ac6454SAndrew Thompson
10402ac6454SAndrew Thompson #define USB_DEBUG_VAR uplcom_debug
10502ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
10602ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
10702ac6454SAndrew Thompson
10802ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h>
10902ac6454SAndrew Thompson
110b850ecc1SAndrew Thompson #ifdef USB_DEBUG
11102ac6454SAndrew Thompson static int uplcom_debug = 0;
11202ac6454SAndrew Thompson
113f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
114f8d2b1f3SPawel Biernacki "USB uplcom");
115ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RWTUN,
11602ac6454SAndrew Thompson &uplcom_debug, 0, "Debug level");
11702ac6454SAndrew Thompson #endif
11802ac6454SAndrew Thompson
11902ac6454SAndrew Thompson #define UPLCOM_MODVER 1 /* module version */
12002ac6454SAndrew Thompson
12102ac6454SAndrew Thompson #define UPLCOM_CONFIG_INDEX 0
12202ac6454SAndrew Thompson #define UPLCOM_IFACE_INDEX 0
12302ac6454SAndrew Thompson #define UPLCOM_SECOND_IFACE_INDEX 1
12402ac6454SAndrew Thompson
12502ac6454SAndrew Thompson #ifndef UPLCOM_INTR_INTERVAL
12602ac6454SAndrew Thompson #define UPLCOM_INTR_INTERVAL 0 /* default */
12702ac6454SAndrew Thompson #endif
12802ac6454SAndrew Thompson
12902ac6454SAndrew Thompson #define UPLCOM_BULK_BUF_SIZE 1024 /* bytes */
13002ac6454SAndrew Thompson
13102ac6454SAndrew Thompson #define UPLCOM_SET_REQUEST 0x01
132ea0efc37SHans Petter Selasky #define UPLCOM_SET_REQUEST_PL2303HXN 0x80
13302ac6454SAndrew Thompson #define UPLCOM_SET_CRTSCTS 0x41
13402ac6454SAndrew Thompson #define UPLCOM_SET_CRTSCTS_PL2303X 0x61
135ea0efc37SHans Petter Selasky #define UPLCOM_SET_CRTSCTS_PL2303HXN 0xFA
136ea0efc37SHans Petter Selasky #define UPLCOM_CLEAR_CRTSCTS_PL2303HXN 0xFF
137ea0efc37SHans Petter Selasky #define UPLCOM_CRTSCTS_REG_PL2303HXN 0x0A
138ea0efc37SHans Petter Selasky #define UPLCOM_STATUS_REG_PL2303HX 0x8080
13902ac6454SAndrew Thompson #define RSAQ_STATUS_CTS 0x80
14027e4bd81SHans Petter Selasky #define RSAQ_STATUS_OVERRUN_ERROR 0x40
14127e4bd81SHans Petter Selasky #define RSAQ_STATUS_PARITY_ERROR 0x20
14227e4bd81SHans Petter Selasky #define RSAQ_STATUS_FRAME_ERROR 0x10
14327e4bd81SHans Petter Selasky #define RSAQ_STATUS_RING 0x08
14427e4bd81SHans Petter Selasky #define RSAQ_STATUS_BREAK_ERROR 0x04
14502ac6454SAndrew Thompson #define RSAQ_STATUS_DSR 0x02
14602ac6454SAndrew Thompson #define RSAQ_STATUS_DCD 0x01
14702ac6454SAndrew Thompson
14802ac6454SAndrew Thompson #define TYPE_PL2303 0
149bc65068dSGavin Atkinson #define TYPE_PL2303HX 1
15027e4bd81SHans Petter Selasky #define TYPE_PL2303HXD 2
151ea0efc37SHans Petter Selasky #define TYPE_PL2303HXN 3
15227e4bd81SHans Petter Selasky
15327e4bd81SHans Petter Selasky #define UPLCOM_STATE_INDEX 8
15402ac6454SAndrew Thompson
15502ac6454SAndrew Thompson enum {
15602ac6454SAndrew Thompson UPLCOM_BULK_DT_WR,
15702ac6454SAndrew Thompson UPLCOM_BULK_DT_RD,
15802ac6454SAndrew Thompson UPLCOM_INTR_DT_RD,
15902ac6454SAndrew Thompson UPLCOM_N_TRANSFER,
16002ac6454SAndrew Thompson };
16102ac6454SAndrew Thompson
16202ac6454SAndrew Thompson struct uplcom_softc {
163760bc48eSAndrew Thompson struct ucom_super_softc sc_super_ucom;
164760bc48eSAndrew Thompson struct ucom_softc sc_ucom;
16502ac6454SAndrew Thompson
166760bc48eSAndrew Thompson struct usb_xfer *sc_xfer[UPLCOM_N_TRANSFER];
167760bc48eSAndrew Thompson struct usb_device *sc_udev;
168deefe583SAndrew Thompson struct mtx sc_mtx;
16902ac6454SAndrew Thompson
17002ac6454SAndrew Thompson uint16_t sc_line;
17102ac6454SAndrew Thompson
17202ac6454SAndrew Thompson uint8_t sc_lsr; /* local status register */
17302ac6454SAndrew Thompson uint8_t sc_msr; /* uplcom status register */
17402ac6454SAndrew Thompson uint8_t sc_chiptype; /* type of chip */
17502ac6454SAndrew Thompson uint8_t sc_ctrl_iface_no;
17602ac6454SAndrew Thompson uint8_t sc_data_iface_no;
17702ac6454SAndrew Thompson uint8_t sc_iface_index[2];
17802ac6454SAndrew Thompson };
17902ac6454SAndrew Thompson
18002ac6454SAndrew Thompson /* prototypes */
18102ac6454SAndrew Thompson
182e0a69b51SAndrew Thompson static usb_error_t uplcom_reset(struct uplcom_softc *, struct usb_device *);
18302d4a225SDimitry Andric static usb_error_t uplcom_pl2303_do(struct usb_device *, uint8_t, uint8_t,
184bc65068dSGavin Atkinson uint16_t, uint16_t, uint16_t);
185bc65068dSGavin Atkinson static int uplcom_pl2303_init(struct usb_device *, uint8_t);
1865805d178SHans Petter Selasky static void uplcom_free(struct ucom_softc *);
187760bc48eSAndrew Thompson static void uplcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
188760bc48eSAndrew Thompson static void uplcom_cfg_set_rts(struct ucom_softc *, uint8_t);
189760bc48eSAndrew Thompson static void uplcom_cfg_set_break(struct ucom_softc *, uint8_t);
190760bc48eSAndrew Thompson static int uplcom_pre_param(struct ucom_softc *, struct termios *);
191760bc48eSAndrew Thompson static void uplcom_cfg_param(struct ucom_softc *, struct termios *);
192760bc48eSAndrew Thompson static void uplcom_start_read(struct ucom_softc *);
193760bc48eSAndrew Thompson static void uplcom_stop_read(struct ucom_softc *);
194760bc48eSAndrew Thompson static void uplcom_start_write(struct ucom_softc *);
195760bc48eSAndrew Thompson static void uplcom_stop_write(struct ucom_softc *);
196760bc48eSAndrew Thompson static void uplcom_cfg_get_status(struct ucom_softc *, uint8_t *,
19702ac6454SAndrew Thompson uint8_t *);
198655dc9d0SAndrew Thompson static void uplcom_poll(struct ucom_softc *ucom);
19902ac6454SAndrew Thompson
20002ac6454SAndrew Thompson static device_probe_t uplcom_probe;
20102ac6454SAndrew Thompson static device_attach_t uplcom_attach;
20202ac6454SAndrew Thompson static device_detach_t uplcom_detach;
203c01fc06eSHans Petter Selasky static void uplcom_free_softc(struct uplcom_softc *);
20402ac6454SAndrew Thompson
205e0a69b51SAndrew Thompson static usb_callback_t uplcom_intr_callback;
206e0a69b51SAndrew Thompson static usb_callback_t uplcom_write_callback;
207e0a69b51SAndrew Thompson static usb_callback_t uplcom_read_callback;
20802ac6454SAndrew Thompson
209760bc48eSAndrew Thompson static const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = {
21002ac6454SAndrew Thompson [UPLCOM_BULK_DT_WR] = {
21102ac6454SAndrew Thompson .type = UE_BULK,
21202ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY,
21302ac6454SAndrew Thompson .direction = UE_DIR_OUT,
2144eae601eSAndrew Thompson .bufsize = UPLCOM_BULK_BUF_SIZE,
2154eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
2164eae601eSAndrew Thompson .callback = &uplcom_write_callback,
21702ac6454SAndrew Thompson .if_index = 0,
21802ac6454SAndrew Thompson },
21902ac6454SAndrew Thompson
22002ac6454SAndrew Thompson [UPLCOM_BULK_DT_RD] = {
22102ac6454SAndrew Thompson .type = UE_BULK,
22202ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY,
22302ac6454SAndrew Thompson .direction = UE_DIR_IN,
2244eae601eSAndrew Thompson .bufsize = UPLCOM_BULK_BUF_SIZE,
2254eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2264eae601eSAndrew Thompson .callback = &uplcom_read_callback,
22702ac6454SAndrew Thompson .if_index = 0,
22802ac6454SAndrew Thompson },
22902ac6454SAndrew Thompson
23002ac6454SAndrew Thompson [UPLCOM_INTR_DT_RD] = {
23102ac6454SAndrew Thompson .type = UE_INTERRUPT,
23202ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY,
23302ac6454SAndrew Thompson .direction = UE_DIR_IN,
2344eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2354eae601eSAndrew Thompson .bufsize = 0, /* use wMaxPacketSize */
2364eae601eSAndrew Thompson .callback = &uplcom_intr_callback,
23702ac6454SAndrew Thompson .if_index = 1,
23802ac6454SAndrew Thompson },
23902ac6454SAndrew Thompson };
24002ac6454SAndrew Thompson
241a8675caeSAndrew Thompson static struct ucom_callback uplcom_callback = {
242a593f6b8SAndrew Thompson .ucom_cfg_get_status = &uplcom_cfg_get_status,
243a593f6b8SAndrew Thompson .ucom_cfg_set_dtr = &uplcom_cfg_set_dtr,
244a593f6b8SAndrew Thompson .ucom_cfg_set_rts = &uplcom_cfg_set_rts,
245a593f6b8SAndrew Thompson .ucom_cfg_set_break = &uplcom_cfg_set_break,
246a593f6b8SAndrew Thompson .ucom_cfg_param = &uplcom_cfg_param,
247a593f6b8SAndrew Thompson .ucom_pre_param = &uplcom_pre_param,
248a593f6b8SAndrew Thompson .ucom_start_read = &uplcom_start_read,
249a593f6b8SAndrew Thompson .ucom_stop_read = &uplcom_stop_read,
250a593f6b8SAndrew Thompson .ucom_start_write = &uplcom_start_write,
251a593f6b8SAndrew Thompson .ucom_stop_write = &uplcom_stop_write,
252655dc9d0SAndrew Thompson .ucom_poll = &uplcom_poll,
2535805d178SHans Petter Selasky .ucom_free = &uplcom_free,
25402ac6454SAndrew Thompson };
25502ac6454SAndrew Thompson
256bc65068dSGavin Atkinson #define UPLCOM_DEV(v,p) \
257bc65068dSGavin Atkinson { USB_VENDOR(USB_VENDOR_##v), USB_PRODUCT(USB_PRODUCT_##v##_##p) }
25802ac6454SAndrew Thompson
259f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID uplcom_devs[] = {
260f5113df9SGavin Atkinson UPLCOM_DEV(ACERP, S81), /* BenQ S81 phone */
261f5113df9SGavin Atkinson UPLCOM_DEV(ADLINK, ND6530), /* ADLINK ND-6530 USB-Serial */
262f5113df9SGavin Atkinson UPLCOM_DEV(ALCATEL, OT535), /* Alcatel One Touch 535/735 */
263f5113df9SGavin Atkinson UPLCOM_DEV(ALCOR, AU9720), /* Alcor AU9720 USB 2.0-RS232 */
264f5113df9SGavin Atkinson UPLCOM_DEV(ANCHOR, SERIAL), /* Anchor Serial adapter */
265bc65068dSGavin Atkinson UPLCOM_DEV(ATEN, UC232A), /* PLANEX USB-RS232 URS-03 */
26646a5f883SEd Maste UPLCOM_DEV(ATEN, UC232B), /* Prolific USB-RS232 Controller D */
26701a8f075SGavin Atkinson UPLCOM_DEV(BELKIN, F5U257), /* Belkin F5U257 USB to Serial */
268bc65068dSGavin Atkinson UPLCOM_DEV(COREGA, CGUSBRS232R), /* Corega CG-USBRS232R */
269f5113df9SGavin Atkinson UPLCOM_DEV(EPSON, CRESSI_EDY), /* Cressi Edy diving computer */
270c6ac426dSGavin Atkinson UPLCOM_DEV(EPSON, N2ITION3), /* Zeagle N2iTion3 diving computer */
27101a8f075SGavin Atkinson UPLCOM_DEV(ELECOM, UCSGT), /* ELECOM UC-SGT Serial Adapter */
27201a8f075SGavin Atkinson UPLCOM_DEV(ELECOM, UCSGT0), /* ELECOM UC-SGT Serial Adapter */
273bc65068dSGavin Atkinson UPLCOM_DEV(HAL, IMR001), /* HAL Corporation Crossam2+USB */
274f5113df9SGavin Atkinson UPLCOM_DEV(HP, LD220), /* HP LD220 POS Display */
275bc65068dSGavin Atkinson UPLCOM_DEV(IODATA, USBRSAQ), /* I/O DATA USB-RSAQ */
276bc65068dSGavin Atkinson UPLCOM_DEV(IODATA, USBRSAQ5), /* I/O DATA USB-RSAQ5 */
277f5113df9SGavin Atkinson UPLCOM_DEV(ITEGNO, WM1080A), /* iTegno WM1080A GSM/GFPRS modem */
278f5113df9SGavin Atkinson UPLCOM_DEV(ITEGNO, WM2080A), /* iTegno WM2080A CDMA modem */
279f5113df9SGavin Atkinson UPLCOM_DEV(LEADTEK, 9531), /* Leadtek 9531 GPS */
280f5113df9SGavin Atkinson UPLCOM_DEV(MICROSOFT, 700WX), /* Microsoft Palm 700WX */
281bc65068dSGavin Atkinson UPLCOM_DEV(MOBILEACTION, MA620), /* Mobile Action MA-620 Infrared Adapter */
282f5113df9SGavin Atkinson UPLCOM_DEV(NETINDEX, WS002IN), /* Willcom W-S002IN */
283f5113df9SGavin Atkinson UPLCOM_DEV(NOKIA2, CA42), /* Nokia CA-42 cable */
284f5113df9SGavin Atkinson UPLCOM_DEV(OTI, DKU5), /* OTI DKU-5 cable */
285f5113df9SGavin Atkinson UPLCOM_DEV(PANASONIC, TYTP50P6S), /* Panasonic TY-TP50P6-S flat screen */
286f5113df9SGavin Atkinson UPLCOM_DEV(PLX, CA42), /* PLX CA-42 clone cable */
287f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC, ALLTRONIX_GPRS), /* Alltronix ACM003U00 modem */
288f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC, ALDIGA_AL11U), /* AlDiga AL-11U modem */
289f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC, DCU11), /* DCU-11 Phone Cable */
290f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC, HCR331), /* HCR331 Card Reader */
291f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC, MICROMAX_610U), /* Micromax 610U modem */
292d01755edSGavin Atkinson UPLCOM_DEV(PROLIFIC, MOTOROLA), /* Motorola cable */
293bc65068dSGavin Atkinson UPLCOM_DEV(PROLIFIC, PHAROS), /* Prolific Pharos */
294f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC, PL2303), /* Generic adapter */
295ea0efc37SHans Petter Selasky UPLCOM_DEV(PROLIFIC, PL2303GC), /* Generic adapter (PL2303HXN, type GC) */
296ea0efc37SHans Petter Selasky UPLCOM_DEV(PROLIFIC, PL2303GB), /* Generic adapter (PL2303HXN, type GB) */
297ea0efc37SHans Petter Selasky UPLCOM_DEV(PROLIFIC, PL2303GT), /* Generic adapter (PL2303HXN, type GT) */
298ea0efc37SHans Petter Selasky UPLCOM_DEV(PROLIFIC, PL2303GL), /* Generic adapter (PL2303HXN, type GL) */
299ea0efc37SHans Petter Selasky UPLCOM_DEV(PROLIFIC, PL2303GE), /* Generic adapter (PL2303HXN, type GE) */
300ea0efc37SHans Petter Selasky UPLCOM_DEV(PROLIFIC, PL2303GS), /* Generic adapter (PL2303HXN, type GS) */
301bc65068dSGavin Atkinson UPLCOM_DEV(PROLIFIC, RSAQ2), /* I/O DATA USB-RSAQ2 */
302bc65068dSGavin Atkinson UPLCOM_DEV(PROLIFIC, RSAQ3), /* I/O DATA USB-RSAQ3 */
303635c9457SGavin Atkinson UPLCOM_DEV(PROLIFIC, UIC_MSR206), /* UIC MSR206 Card Reader */
304f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC2, PL2303), /* Prolific adapter */
30501a8f075SGavin Atkinson UPLCOM_DEV(RADIOSHACK, USBCABLE), /* Radio Shack USB Adapter */
306bc65068dSGavin Atkinson UPLCOM_DEV(RATOC, REXUSB60), /* RATOC REX-USB60 */
307bc65068dSGavin Atkinson UPLCOM_DEV(SAGEM, USBSERIAL), /* Sagem USB-Serial Controller */
308f5113df9SGavin Atkinson UPLCOM_DEV(SAMSUNG, I330), /* Samsung I330 phone cradle */
309f5113df9SGavin Atkinson UPLCOM_DEV(SANWA, KB_USB2), /* Sanwa KB-USB2 Multimeter cable */
31001a8f075SGavin Atkinson UPLCOM_DEV(SIEMENS3, EF81), /* Siemens EF81 */
31101a8f075SGavin Atkinson UPLCOM_DEV(SIEMENS3, SX1), /* Siemens SX1 */
31201a8f075SGavin Atkinson UPLCOM_DEV(SIEMENS3, X65), /* Siemens X65 */
31301a8f075SGavin Atkinson UPLCOM_DEV(SIEMENS3, X75), /* Siemens X75 */
314bc65068dSGavin Atkinson UPLCOM_DEV(SITECOM, SERIAL), /* Sitecom USB to Serial */
31501a8f075SGavin Atkinson UPLCOM_DEV(SMART, PL2303), /* SMART Technologies USB to Serial */
316f5113df9SGavin Atkinson UPLCOM_DEV(SONY, QN3), /* Sony QN3 phone cable */
317f5113df9SGavin Atkinson UPLCOM_DEV(SONYERICSSON, DATAPILOT), /* Sony Ericsson Datapilot */
318f5113df9SGavin Atkinson UPLCOM_DEV(SONYERICSSON, DCU10), /* Sony Ericsson DCU-10 Cable */
319bc65068dSGavin Atkinson UPLCOM_DEV(SOURCENEXT, KEIKAI8), /* SOURCENEXT KeikaiDenwa 8 */
320bc65068dSGavin Atkinson UPLCOM_DEV(SOURCENEXT, KEIKAI8_CHG), /* SOURCENEXT KeikaiDenwa 8 with charger */
321f5113df9SGavin Atkinson UPLCOM_DEV(SPEEDDRAGON, MS3303H), /* Speed Dragon USB-Serial */
322f5113df9SGavin Atkinson UPLCOM_DEV(SYNTECH, CPT8001C), /* Syntech CPT-8001C Barcode scanner */
323bc65068dSGavin Atkinson UPLCOM_DEV(TDK, UHA6400), /* TDK USB-PHS Adapter UHA6400 */
324f5113df9SGavin Atkinson UPLCOM_DEV(TDK, UPA9664), /* TDK USB-PHS Adapter UPA9664 */
32501a8f075SGavin Atkinson UPLCOM_DEV(TRIPPLITE, U209), /* Tripp-Lite U209-000-R USB to Serial */
326f5113df9SGavin Atkinson UPLCOM_DEV(YCCABLE, PL2303), /* YC Cable USB-Serial */
32702ac6454SAndrew Thompson };
3289e6b5313SAndrew Thompson #undef UPLCOM_DEV
32902ac6454SAndrew Thompson
33002ac6454SAndrew Thompson static device_method_t uplcom_methods[] = {
33102ac6454SAndrew Thompson DEVMETHOD(device_probe, uplcom_probe),
33202ac6454SAndrew Thompson DEVMETHOD(device_attach, uplcom_attach),
33302ac6454SAndrew Thompson DEVMETHOD(device_detach, uplcom_detach),
3345805d178SHans Petter Selasky DEVMETHOD_END
33502ac6454SAndrew Thompson };
33602ac6454SAndrew Thompson
33702ac6454SAndrew Thompson static driver_t uplcom_driver = {
33802ac6454SAndrew Thompson .name = "uplcom",
33902ac6454SAndrew Thompson .methods = uplcom_methods,
34002ac6454SAndrew Thompson .size = sizeof(struct uplcom_softc),
34102ac6454SAndrew Thompson };
34202ac6454SAndrew Thompson
343bc9372d7SJohn Baldwin DRIVER_MODULE(uplcom, uhub, uplcom_driver, NULL, NULL);
34402ac6454SAndrew Thompson MODULE_DEPEND(uplcom, ucom, 1, 1, 1);
34502ac6454SAndrew Thompson MODULE_DEPEND(uplcom, usb, 1, 1, 1);
34602ac6454SAndrew Thompson MODULE_VERSION(uplcom, UPLCOM_MODVER);
347f809f280SWarner Losh USB_PNP_HOST_INFO(uplcom_devs);
34802ac6454SAndrew Thompson
34902ac6454SAndrew Thompson static int
uplcom_probe(device_t dev)35002ac6454SAndrew Thompson uplcom_probe(device_t dev)
35102ac6454SAndrew Thompson {
352760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev);
35302ac6454SAndrew Thompson
35402ac6454SAndrew Thompson DPRINTFN(11, "\n");
35502ac6454SAndrew Thompson
356f29a0724SAndrew Thompson if (uaa->usb_mode != USB_MODE_HOST) {
35702ac6454SAndrew Thompson return (ENXIO);
35802ac6454SAndrew Thompson }
35902ac6454SAndrew Thompson if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
36002ac6454SAndrew Thompson return (ENXIO);
36102ac6454SAndrew Thompson }
36202ac6454SAndrew Thompson if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
36302ac6454SAndrew Thompson return (ENXIO);
36402ac6454SAndrew Thompson }
365a593f6b8SAndrew Thompson return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
36602ac6454SAndrew Thompson }
36702ac6454SAndrew Thompson
36802ac6454SAndrew Thompson static int
uplcom_attach(device_t dev)36902ac6454SAndrew Thompson uplcom_attach(device_t dev)
37002ac6454SAndrew Thompson {
371760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev);
37202ac6454SAndrew Thompson struct uplcom_softc *sc = device_get_softc(dev);
373760bc48eSAndrew Thompson struct usb_interface *iface;
374760bc48eSAndrew Thompson struct usb_interface_descriptor *id;
375bc65068dSGavin Atkinson struct usb_device_descriptor *dd;
37602ac6454SAndrew Thompson int error;
37702ac6454SAndrew Thompson
378ea0efc37SHans Petter Selasky struct usb_device_request req;
379ea0efc37SHans Petter Selasky usb_error_t err;
380ea0efc37SHans Petter Selasky uint8_t buf[4];
381ea0efc37SHans Petter Selasky
38202ac6454SAndrew Thompson DPRINTFN(11, "\n");
38302ac6454SAndrew Thompson
384a593f6b8SAndrew Thompson device_set_usb_desc(dev);
385deefe583SAndrew Thompson mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF);
3865805d178SHans Petter Selasky ucom_ref(&sc->sc_super_ucom);
38702ac6454SAndrew Thompson
38802ac6454SAndrew Thompson DPRINTF("sc = %p\n", sc);
38902ac6454SAndrew Thompson
39002ac6454SAndrew Thompson sc->sc_udev = uaa->device;
39102ac6454SAndrew Thompson
392bc65068dSGavin Atkinson dd = usbd_get_device_descriptor(sc->sc_udev);
39327e4bd81SHans Petter Selasky
39427e4bd81SHans Petter Selasky switch (UGETW(dd->bcdDevice)) {
39527e4bd81SHans Petter Selasky case 0x0300:
39627e4bd81SHans Petter Selasky sc->sc_chiptype = TYPE_PL2303HX;
39727e4bd81SHans Petter Selasky /* or TA, that is HX with external crystal */
39827e4bd81SHans Petter Selasky break;
39927e4bd81SHans Petter Selasky case 0x0400:
40027e4bd81SHans Petter Selasky sc->sc_chiptype = TYPE_PL2303HXD;
40127e4bd81SHans Petter Selasky /* or EA, that is HXD with ESD protection */
40227e4bd81SHans Petter Selasky /* or RA, that has internal voltage level converter that works only up to 1Mbaud (!) */
40327e4bd81SHans Petter Selasky break;
40427e4bd81SHans Petter Selasky case 0x0500:
40527e4bd81SHans Petter Selasky sc->sc_chiptype = TYPE_PL2303HXD;
40627e4bd81SHans Petter Selasky /* in fact it's TB, that is HXD with external crystal */
40727e4bd81SHans Petter Selasky break;
40827e4bd81SHans Petter Selasky default:
40927e4bd81SHans Petter Selasky /* NOTE: I have no info about the bcdDevice for the base PL2303 (up to 1.2Mbaud,
41027e4bd81SHans Petter Selasky only fixed rates) and for PL2303SA (8-pin chip, up to 115200 baud */
41127e4bd81SHans Petter Selasky /* Determine the chip type. This algorithm is taken from Linux. */
412bc65068dSGavin Atkinson if (dd->bDeviceClass == 0x02)
413bc65068dSGavin Atkinson sc->sc_chiptype = TYPE_PL2303;
414bc65068dSGavin Atkinson else if (dd->bMaxPacketSize == 0x40)
415bc65068dSGavin Atkinson sc->sc_chiptype = TYPE_PL2303HX;
416bc65068dSGavin Atkinson else
417bc65068dSGavin Atkinson sc->sc_chiptype = TYPE_PL2303;
41827e4bd81SHans Petter Selasky break;
41927e4bd81SHans Petter Selasky }
420bc65068dSGavin Atkinson
421ea0efc37SHans Petter Selasky /*
422ea0efc37SHans Petter Selasky * The new chip revision PL2303HXN is only compatible with the new
423ea0efc37SHans Petter Selasky * UPLCOM_SET_REQUEST_PL2303HXN command. Issuing the old command
424ea0efc37SHans Petter Selasky * UPLCOM_SET_REQUEST to the new chip raises an error. Thus, PL2303HX
425ea0efc37SHans Petter Selasky * and PL2303HXN can be distinguished by issuing an old-style request
426ea0efc37SHans Petter Selasky * (on a status register) to the new chip and checking the error.
427ea0efc37SHans Petter Selasky */
428ea0efc37SHans Petter Selasky if (sc->sc_chiptype == TYPE_PL2303HX) {
429ea0efc37SHans Petter Selasky req.bmRequestType = UT_READ_VENDOR_DEVICE;
430ea0efc37SHans Petter Selasky req.bRequest = UPLCOM_SET_REQUEST;
431ea0efc37SHans Petter Selasky USETW(req.wValue, UPLCOM_STATUS_REG_PL2303HX);
432ea0efc37SHans Petter Selasky req.wIndex[0] = sc->sc_data_iface_no;
433ea0efc37SHans Petter Selasky req.wIndex[1] = 0;
434ea0efc37SHans Petter Selasky USETW(req.wLength, 1);
435ea0efc37SHans Petter Selasky err = usbd_do_request(sc->sc_udev, NULL, &req, buf);
436ea0efc37SHans Petter Selasky if (err)
437ea0efc37SHans Petter Selasky sc->sc_chiptype = TYPE_PL2303HXN;
438ea0efc37SHans Petter Selasky }
439ea0efc37SHans Petter Selasky
44027e4bd81SHans Petter Selasky switch (sc->sc_chiptype) {
44127e4bd81SHans Petter Selasky case TYPE_PL2303:
44227e4bd81SHans Petter Selasky DPRINTF("chiptype: 2303\n");
44327e4bd81SHans Petter Selasky break;
44427e4bd81SHans Petter Selasky case TYPE_PL2303HX:
44527e4bd81SHans Petter Selasky DPRINTF("chiptype: 2303HX/TA\n");
44627e4bd81SHans Petter Selasky break;
447ea0efc37SHans Petter Selasky case TYPE_PL2303HXN:
448ea0efc37SHans Petter Selasky DPRINTF("chiptype: 2303HXN\n");
449ea0efc37SHans Petter Selasky break;
45027e4bd81SHans Petter Selasky case TYPE_PL2303HXD:
45127e4bd81SHans Petter Selasky DPRINTF("chiptype: 2303HXD/TB/RA/EA\n");
45227e4bd81SHans Petter Selasky break;
45327e4bd81SHans Petter Selasky default:
45427e4bd81SHans Petter Selasky DPRINTF("chiptype: unknown %d\n", sc->sc_chiptype);
45527e4bd81SHans Petter Selasky break;
45627e4bd81SHans Petter Selasky }
45702ac6454SAndrew Thompson
45802ac6454SAndrew Thompson /*
45902ac6454SAndrew Thompson * USB-RSAQ1 has two interface
46002ac6454SAndrew Thompson *
46102ac6454SAndrew Thompson * USB-RSAQ1 | USB-RSAQ2
46202ac6454SAndrew Thompson * -----------------+-----------------
46302ac6454SAndrew Thompson * Interface 0 |Interface 0
46402ac6454SAndrew Thompson * Interrupt(0x81) | Interrupt(0x81)
46502ac6454SAndrew Thompson * -----------------+ BulkIN(0x02)
46602ac6454SAndrew Thompson * Interface 1 | BulkOUT(0x83)
46702ac6454SAndrew Thompson * BulkIN(0x02) |
46802ac6454SAndrew Thompson * BulkOUT(0x83) |
46902ac6454SAndrew Thompson */
47002ac6454SAndrew Thompson
47102ac6454SAndrew Thompson sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
47202ac6454SAndrew Thompson sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
47302ac6454SAndrew Thompson
474a593f6b8SAndrew Thompson iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
47502ac6454SAndrew Thompson if (iface) {
476a593f6b8SAndrew Thompson id = usbd_get_interface_descriptor(iface);
47702ac6454SAndrew Thompson if (id == NULL) {
478767cb2e2SAndrew Thompson device_printf(dev, "no interface descriptor (2)\n");
47902ac6454SAndrew Thompson goto detach;
48002ac6454SAndrew Thompson }
48102ac6454SAndrew Thompson sc->sc_data_iface_no = id->bInterfaceNumber;
48202ac6454SAndrew Thompson sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
483a593f6b8SAndrew Thompson usbd_set_parent_iface(uaa->device,
48402ac6454SAndrew Thompson UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
48502ac6454SAndrew Thompson } else {
48602ac6454SAndrew Thompson sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
48702ac6454SAndrew Thompson sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
48802ac6454SAndrew Thompson }
48902ac6454SAndrew Thompson
490a593f6b8SAndrew Thompson error = usbd_transfer_setup(uaa->device,
49102ac6454SAndrew Thompson sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
492deefe583SAndrew Thompson UPLCOM_N_TRANSFER, sc, &sc->sc_mtx);
49302ac6454SAndrew Thompson if (error) {
49402ac6454SAndrew Thompson DPRINTF("one or more missing USB endpoints, "
495a593f6b8SAndrew Thompson "error=%s\n", usbd_errstr(error));
49602ac6454SAndrew Thompson goto detach;
49702ac6454SAndrew Thompson }
49802ac6454SAndrew Thompson error = uplcom_reset(sc, uaa->device);
49902ac6454SAndrew Thompson if (error) {
50002ac6454SAndrew Thompson device_printf(dev, "reset failed, error=%s\n",
501a593f6b8SAndrew Thompson usbd_errstr(error));
50202ac6454SAndrew Thompson goto detach;
50302ac6454SAndrew Thompson }
5049efb7339SHans Petter Selasky
505ec97e9caSHans Petter Selasky if (sc->sc_chiptype == TYPE_PL2303) {
506ec97e9caSHans Petter Selasky /* HX variants seem to lock up after a clear stall request. */
507deefe583SAndrew Thompson mtx_lock(&sc->sc_mtx);
508ec97e9caSHans Petter Selasky usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
509ec97e9caSHans Petter Selasky usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
510deefe583SAndrew Thompson mtx_unlock(&sc->sc_mtx);
511ec97e9caSHans Petter Selasky } else if (sc->sc_chiptype == TYPE_PL2303HX ||
512ea0efc37SHans Petter Selasky sc->sc_chiptype == TYPE_PL2303HXD) {
51327e4bd81SHans Petter Selasky /* reset upstream data pipes */
5149efb7339SHans Petter Selasky if (uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
5159efb7339SHans Petter Selasky UPLCOM_SET_REQUEST, 8, 0, 0) ||
5169efb7339SHans Petter Selasky uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
5179efb7339SHans Petter Selasky UPLCOM_SET_REQUEST, 9, 0, 0)) {
5189efb7339SHans Petter Selasky goto detach;
5199efb7339SHans Petter Selasky }
520ea0efc37SHans Petter Selasky } else if (sc->sc_chiptype == TYPE_PL2303HXN) {
521ea0efc37SHans Petter Selasky /* reset upstream data pipes */
522ea0efc37SHans Petter Selasky if (uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
523ea0efc37SHans Petter Selasky UPLCOM_SET_REQUEST_PL2303HXN, 0x07, 0x03, 0)) {
524ea0efc37SHans Petter Selasky goto detach;
525ea0efc37SHans Petter Selasky }
5269efb7339SHans Petter Selasky }
52702ac6454SAndrew Thompson
528a593f6b8SAndrew Thompson error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
529deefe583SAndrew Thompson &uplcom_callback, &sc->sc_mtx);
53002ac6454SAndrew Thompson if (error) {
53102ac6454SAndrew Thompson goto detach;
53202ac6454SAndrew Thompson }
53302ac6454SAndrew Thompson /*
53402ac6454SAndrew Thompson * do the initialization during attach so that the system does not
53502ac6454SAndrew Thompson * sleep during open:
53602ac6454SAndrew Thompson */
537bc65068dSGavin Atkinson if (uplcom_pl2303_init(uaa->device, sc->sc_chiptype)) {
538767cb2e2SAndrew Thompson device_printf(dev, "init failed\n");
53902ac6454SAndrew Thompson goto detach;
54002ac6454SAndrew Thompson }
5416416c259SNick Hibma ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
5426416c259SNick Hibma
54302ac6454SAndrew Thompson return (0);
54402ac6454SAndrew Thompson
54502ac6454SAndrew Thompson detach:
54602ac6454SAndrew Thompson uplcom_detach(dev);
54702ac6454SAndrew Thompson return (ENXIO);
54802ac6454SAndrew Thompson }
54902ac6454SAndrew Thompson
55002ac6454SAndrew Thompson static int
uplcom_detach(device_t dev)55102ac6454SAndrew Thompson uplcom_detach(device_t dev)
55202ac6454SAndrew Thompson {
55302ac6454SAndrew Thompson struct uplcom_softc *sc = device_get_softc(dev);
55402ac6454SAndrew Thompson
55502ac6454SAndrew Thompson DPRINTF("sc=%p\n", sc);
55602ac6454SAndrew Thompson
557015bb88fSNick Hibma ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
558a593f6b8SAndrew Thompson usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
55902ac6454SAndrew Thompson
560c01fc06eSHans Petter Selasky device_claim_softc(dev);
561c01fc06eSHans Petter Selasky
562c01fc06eSHans Petter Selasky uplcom_free_softc(sc);
563c01fc06eSHans Petter Selasky
56402ac6454SAndrew Thompson return (0);
56502ac6454SAndrew Thompson }
56602ac6454SAndrew Thompson
5675805d178SHans Petter Selasky UCOM_UNLOAD_DRAIN(uplcom);
5685805d178SHans Petter Selasky
5695805d178SHans Petter Selasky static void
uplcom_free_softc(struct uplcom_softc * sc)570c01fc06eSHans Petter Selasky uplcom_free_softc(struct uplcom_softc *sc)
5715805d178SHans Petter Selasky {
5725805d178SHans Petter Selasky if (ucom_unref(&sc->sc_super_ucom)) {
5735805d178SHans Petter Selasky mtx_destroy(&sc->sc_mtx);
574c01fc06eSHans Petter Selasky device_free_softc(sc);
5755805d178SHans Petter Selasky }
5765805d178SHans Petter Selasky }
5775805d178SHans Petter Selasky
5785805d178SHans Petter Selasky static void
uplcom_free(struct ucom_softc * ucom)5795805d178SHans Petter Selasky uplcom_free(struct ucom_softc *ucom)
5805805d178SHans Petter Selasky {
581c01fc06eSHans Petter Selasky uplcom_free_softc(ucom->sc_parent);
5825805d178SHans Petter Selasky }
5835805d178SHans Petter Selasky
584e0a69b51SAndrew Thompson static usb_error_t
uplcom_reset(struct uplcom_softc * sc,struct usb_device * udev)585760bc48eSAndrew Thompson uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev)
58602ac6454SAndrew Thompson {
587760bc48eSAndrew Thompson struct usb_device_request req;
58802ac6454SAndrew Thompson
589ea0efc37SHans Petter Selasky if (sc->sc_chiptype == TYPE_PL2303HXN) {
590ea0efc37SHans Petter Selasky /* PL2303HXN doesn't need this reset sequence */
591ea0efc37SHans Petter Selasky return (0);
592ea0efc37SHans Petter Selasky }
593ea0efc37SHans Petter Selasky
59402ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
59502ac6454SAndrew Thompson req.bRequest = UPLCOM_SET_REQUEST;
59602ac6454SAndrew Thompson USETW(req.wValue, 0);
59702ac6454SAndrew Thompson req.wIndex[0] = sc->sc_data_iface_no;
59802ac6454SAndrew Thompson req.wIndex[1] = 0;
59902ac6454SAndrew Thompson USETW(req.wLength, 0);
60002ac6454SAndrew Thompson
601a593f6b8SAndrew Thompson return (usbd_do_request(udev, NULL, &req, NULL));
60202ac6454SAndrew Thompson }
60302ac6454SAndrew Thompson
604bc65068dSGavin Atkinson static usb_error_t
uplcom_pl2303_do(struct usb_device * udev,uint8_t req_type,uint8_t request,uint16_t value,uint16_t index,uint16_t length)60502d4a225SDimitry Andric uplcom_pl2303_do(struct usb_device *udev, uint8_t req_type, uint8_t request,
606bc65068dSGavin Atkinson uint16_t value, uint16_t index, uint16_t length)
60702ac6454SAndrew Thompson {
608760bc48eSAndrew Thompson struct usb_device_request req;
609e0a69b51SAndrew Thompson usb_error_t err;
61002ac6454SAndrew Thompson uint8_t buf[4];
61102ac6454SAndrew Thompson
612bc65068dSGavin Atkinson req.bmRequestType = req_type;
613bc65068dSGavin Atkinson req.bRequest = request;
614bc65068dSGavin Atkinson USETW(req.wValue, value);
615bc65068dSGavin Atkinson USETW(req.wIndex, index);
616bc65068dSGavin Atkinson USETW(req.wLength, length);
61702ac6454SAndrew Thompson
618a593f6b8SAndrew Thompson err = usbd_do_request(udev, NULL, &req, buf);
61902ac6454SAndrew Thompson if (err) {
620a593f6b8SAndrew Thompson DPRINTF("error=%s\n", usbd_errstr(err));
621bc65068dSGavin Atkinson return (1);
622bc65068dSGavin Atkinson }
623bc65068dSGavin Atkinson return (0);
624bc65068dSGavin Atkinson }
625bc65068dSGavin Atkinson
626bc65068dSGavin Atkinson static int
uplcom_pl2303_init(struct usb_device * udev,uint8_t chiptype)627bc65068dSGavin Atkinson uplcom_pl2303_init(struct usb_device *udev, uint8_t chiptype)
628bc65068dSGavin Atkinson {
629bc65068dSGavin Atkinson int err;
630bc65068dSGavin Atkinson
631ea0efc37SHans Petter Selasky if (chiptype == TYPE_PL2303HXN) {
632ea0efc37SHans Petter Selasky /* PL2303HXN doesn't need this initialization sequence */
633ea0efc37SHans Petter Selasky return (0);
634ea0efc37SHans Petter Selasky }
635ea0efc37SHans Petter Selasky
636bc65068dSGavin Atkinson if (uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
637bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0)
638bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
639bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
640bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
641bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0)
642bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
643bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
644bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0)
645bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0))
64602ac6454SAndrew Thompson return (EIO);
647bc65068dSGavin Atkinson
64827e4bd81SHans Petter Selasky if (chiptype != TYPE_PL2303)
649bc65068dSGavin Atkinson err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0);
650bc65068dSGavin Atkinson else
651bc65068dSGavin Atkinson err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x24, 0);
652bc65068dSGavin Atkinson if (err)
653bc65068dSGavin Atkinson return (EIO);
654bc65068dSGavin Atkinson
65502ac6454SAndrew Thompson return (0);
65602ac6454SAndrew Thompson }
65702ac6454SAndrew Thompson
65802ac6454SAndrew Thompson static void
uplcom_cfg_set_dtr(struct ucom_softc * ucom,uint8_t onoff)659760bc48eSAndrew Thompson uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
66002ac6454SAndrew Thompson {
66102ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent;
662760bc48eSAndrew Thompson struct usb_device_request req;
66302ac6454SAndrew Thompson
66402ac6454SAndrew Thompson DPRINTF("onoff = %d\n", onoff);
66502ac6454SAndrew Thompson
66602ac6454SAndrew Thompson if (onoff)
66702ac6454SAndrew Thompson sc->sc_line |= UCDC_LINE_DTR;
66802ac6454SAndrew Thompson else
66902ac6454SAndrew Thompson sc->sc_line &= ~UCDC_LINE_DTR;
67002ac6454SAndrew Thompson
67102ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
67202ac6454SAndrew Thompson req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
67302ac6454SAndrew Thompson USETW(req.wValue, sc->sc_line);
67402ac6454SAndrew Thompson req.wIndex[0] = sc->sc_data_iface_no;
67502ac6454SAndrew Thompson req.wIndex[1] = 0;
67602ac6454SAndrew Thompson USETW(req.wLength, 0);
67702ac6454SAndrew Thompson
678a593f6b8SAndrew Thompson ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
67902ac6454SAndrew Thompson &req, NULL, 0, 1000);
68002ac6454SAndrew Thompson }
68102ac6454SAndrew Thompson
68202ac6454SAndrew Thompson static void
uplcom_cfg_set_rts(struct ucom_softc * ucom,uint8_t onoff)683760bc48eSAndrew Thompson uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
68402ac6454SAndrew Thompson {
68502ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent;
686760bc48eSAndrew Thompson struct usb_device_request req;
68702ac6454SAndrew Thompson
68802ac6454SAndrew Thompson DPRINTF("onoff = %d\n", onoff);
68902ac6454SAndrew Thompson
69002ac6454SAndrew Thompson if (onoff)
69102ac6454SAndrew Thompson sc->sc_line |= UCDC_LINE_RTS;
69202ac6454SAndrew Thompson else
69302ac6454SAndrew Thompson sc->sc_line &= ~UCDC_LINE_RTS;
69402ac6454SAndrew Thompson
69502ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
69602ac6454SAndrew Thompson req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
69702ac6454SAndrew Thompson USETW(req.wValue, sc->sc_line);
69802ac6454SAndrew Thompson req.wIndex[0] = sc->sc_data_iface_no;
69902ac6454SAndrew Thompson req.wIndex[1] = 0;
70002ac6454SAndrew Thompson USETW(req.wLength, 0);
70102ac6454SAndrew Thompson
702a593f6b8SAndrew Thompson ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
70302ac6454SAndrew Thompson &req, NULL, 0, 1000);
70402ac6454SAndrew Thompson }
70502ac6454SAndrew Thompson
70602ac6454SAndrew Thompson static void
uplcom_cfg_set_break(struct ucom_softc * ucom,uint8_t onoff)707760bc48eSAndrew Thompson uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
70802ac6454SAndrew Thompson {
70902ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent;
710760bc48eSAndrew Thompson struct usb_device_request req;
71102ac6454SAndrew Thompson uint16_t temp;
71202ac6454SAndrew Thompson
71302ac6454SAndrew Thompson DPRINTF("onoff = %d\n", onoff);
71402ac6454SAndrew Thompson
71502ac6454SAndrew Thompson temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
71602ac6454SAndrew Thompson
71702ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
71802ac6454SAndrew Thompson req.bRequest = UCDC_SEND_BREAK;
71902ac6454SAndrew Thompson USETW(req.wValue, temp);
72002ac6454SAndrew Thompson req.wIndex[0] = sc->sc_data_iface_no;
72102ac6454SAndrew Thompson req.wIndex[1] = 0;
72202ac6454SAndrew Thompson USETW(req.wLength, 0);
72302ac6454SAndrew Thompson
724a593f6b8SAndrew Thompson ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
72502ac6454SAndrew Thompson &req, NULL, 0, 1000);
72602ac6454SAndrew Thompson }
72702ac6454SAndrew Thompson
72802ac6454SAndrew Thompson /*
72927e4bd81SHans Petter Selasky * NOTE: These baud rates are officially supported, they can be written
73027e4bd81SHans Petter Selasky * directly into dwDTERate register.
73127e4bd81SHans Petter Selasky *
73227e4bd81SHans Petter Selasky * Free baudrate setting is not supported by the base PL2303, and on
73327e4bd81SHans Petter Selasky * other models it requires writing a divisor value to dwDTERate instead
73427e4bd81SHans Petter Selasky * of the raw baudrate. The formula for divisor calculation is not published
73527e4bd81SHans Petter Selasky * by the vendor, so it is speculative, though the official product homepage
73627e4bd81SHans Petter Selasky * refers to the Linux module source as a reference implementation.
73702ac6454SAndrew Thompson */
73827e4bd81SHans Petter Selasky static const uint32_t uplcom_rates[] = {
73927e4bd81SHans Petter Selasky /*
74027e4bd81SHans Petter Selasky * Basic 'standard' speed rates, supported by all models
74127e4bd81SHans Petter Selasky * NOTE: 900 and 56000 actually works as well
74227e4bd81SHans Petter Selasky */
74327e4bd81SHans Petter Selasky 75, 150, 300, 600, 900, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
74427e4bd81SHans Petter Selasky 19200, 28800, 38400, 56000, 57600, 115200,
74527e4bd81SHans Petter Selasky /*
74627e4bd81SHans Petter Selasky * Advanced speed rates up to 6Mbs, supported by HX/TA and HXD/TB/EA/RA
74727e4bd81SHans Petter Selasky * NOTE: regardless of the spec, 256000 does not work
74827e4bd81SHans Petter Selasky */
74927e4bd81SHans Petter Selasky 128000, 134400, 161280, 201600, 230400, 268800, 403200, 460800, 614400,
75027e4bd81SHans Petter Selasky 806400, 921600, 1228800, 2457600, 3000000, 6000000,
75127e4bd81SHans Petter Selasky /*
75227e4bd81SHans Petter Selasky * Advanced speed rates up to 12, supported by HXD/TB/EA/RA
75327e4bd81SHans Petter Selasky */
75427e4bd81SHans Petter Selasky 12000000
75502ac6454SAndrew Thompson };
75602ac6454SAndrew Thompson
757432157dcSPedro F. Giffuni #define N_UPLCOM_RATES nitems(uplcom_rates)
75802ac6454SAndrew Thompson
75902ac6454SAndrew Thompson static int
uplcom_baud_supported(unsigned speed)76062d42655SHans Petter Selasky uplcom_baud_supported(unsigned speed)
76127e4bd81SHans Petter Selasky {
76227e4bd81SHans Petter Selasky int i;
76327e4bd81SHans Petter Selasky for (i = 0; i < N_UPLCOM_RATES; i++) {
76427e4bd81SHans Petter Selasky if (uplcom_rates[i] == speed)
76527e4bd81SHans Petter Selasky return 1;
76627e4bd81SHans Petter Selasky }
76727e4bd81SHans Petter Selasky return 0;
76827e4bd81SHans Petter Selasky }
76927e4bd81SHans Petter Selasky
77027e4bd81SHans Petter Selasky static int
uplcom_pre_param(struct ucom_softc * ucom,struct termios * t)771760bc48eSAndrew Thompson uplcom_pre_param(struct ucom_softc *ucom, struct termios *t)
77202ac6454SAndrew Thompson {
773fe0f6362SGavin Atkinson struct uplcom_softc *sc = ucom->sc_parent;
77402ac6454SAndrew Thompson
77502ac6454SAndrew Thompson DPRINTF("\n");
77602ac6454SAndrew Thompson
777fe0f6362SGavin Atkinson /**
778fe0f6362SGavin Atkinson * Check requested baud rate.
779fe0f6362SGavin Atkinson *
780fe0f6362SGavin Atkinson * The PL2303 can only set specific baud rates, up to 1228800 baud.
78127e4bd81SHans Petter Selasky * The PL2303HX can set any baud rate up to 6Mb.
782ea0efc37SHans Petter Selasky * The PL2303HX rev. D and PL2303HXN can set any baud rate up to 12Mb.
783fe0f6362SGavin Atkinson *
784fe0f6362SGavin Atkinson */
78527e4bd81SHans Petter Selasky
78627e4bd81SHans Petter Selasky /* accept raw divisor data, if someone wants to do the math in user domain */
78727e4bd81SHans Petter Selasky if (t->c_ospeed & 0x80000000)
78827e4bd81SHans Petter Selasky return 0;
78927e4bd81SHans Petter Selasky switch (sc->sc_chiptype) {
790ea0efc37SHans Petter Selasky case TYPE_PL2303HXN:
791ea0efc37SHans Petter Selasky if (t->c_ospeed <= 12000000)
792ea0efc37SHans Petter Selasky return (0);
793ea0efc37SHans Petter Selasky break;
79427e4bd81SHans Petter Selasky case TYPE_PL2303HXD:
79527e4bd81SHans Petter Selasky if (t->c_ospeed <= 12000000)
796fe0f6362SGavin Atkinson return (0);
79727e4bd81SHans Petter Selasky break;
79827e4bd81SHans Petter Selasky case TYPE_PL2303HX:
799fe0f6362SGavin Atkinson if (t->c_ospeed <= 6000000)
800fe0f6362SGavin Atkinson return (0);
80127e4bd81SHans Petter Selasky break;
80227e4bd81SHans Petter Selasky default:
80327e4bd81SHans Petter Selasky if (uplcom_baud_supported(t->c_ospeed))
80427e4bd81SHans Petter Selasky return (0);
80527e4bd81SHans Petter Selasky break;
80602ac6454SAndrew Thompson }
80702ac6454SAndrew Thompson
808fe0f6362SGavin Atkinson DPRINTF("uplcom_param: bad baud rate (%d)\n", t->c_ospeed);
809fe0f6362SGavin Atkinson return (EIO);
81002ac6454SAndrew Thompson }
81102ac6454SAndrew Thompson
81262d42655SHans Petter Selasky static unsigned
uplcom_encode_baud_rate_divisor(uint8_t * buf,unsigned baud)81362d42655SHans Petter Selasky uplcom_encode_baud_rate_divisor(uint8_t *buf, unsigned baud)
81427e4bd81SHans Petter Selasky {
81562d42655SHans Petter Selasky unsigned baseline, mantissa, exponent;
81627e4bd81SHans Petter Selasky
81727e4bd81SHans Petter Selasky /* Determine the baud rate divisor. This algorithm is taken from Linux. */
81827e4bd81SHans Petter Selasky /*
81927e4bd81SHans Petter Selasky * Apparently the formula is:
82027e4bd81SHans Petter Selasky * baudrate = baseline / (mantissa * 4^exponent)
82127e4bd81SHans Petter Selasky * where
82227e4bd81SHans Petter Selasky * mantissa = buf[8:0]
82327e4bd81SHans Petter Selasky * exponent = buf[11:9]
82427e4bd81SHans Petter Selasky */
82527e4bd81SHans Petter Selasky if (baud == 0)
82627e4bd81SHans Petter Selasky baud = 1;
82727e4bd81SHans Petter Selasky baseline = 383385600;
82827e4bd81SHans Petter Selasky mantissa = baseline / baud;
82927e4bd81SHans Petter Selasky if (mantissa == 0)
83027e4bd81SHans Petter Selasky mantissa = 1;
83127e4bd81SHans Petter Selasky exponent = 0;
83227e4bd81SHans Petter Selasky while (mantissa >= 512) {
83327e4bd81SHans Petter Selasky if (exponent < 7) {
83427e4bd81SHans Petter Selasky mantissa >>= 2; /* divide by 4 */
83527e4bd81SHans Petter Selasky exponent++;
83627e4bd81SHans Petter Selasky } else {
83727e4bd81SHans Petter Selasky /* Exponent is maxed. Trim mantissa and leave. This gives approx. 45.8 baud */
83827e4bd81SHans Petter Selasky mantissa = 511;
83927e4bd81SHans Petter Selasky break;
84027e4bd81SHans Petter Selasky }
84127e4bd81SHans Petter Selasky }
84227e4bd81SHans Petter Selasky
84327e4bd81SHans Petter Selasky buf[3] = 0x80;
84427e4bd81SHans Petter Selasky buf[2] = 0;
84527e4bd81SHans Petter Selasky buf[1] = exponent << 1 | mantissa >> 8;
84627e4bd81SHans Petter Selasky buf[0] = mantissa & 0xff;
84727e4bd81SHans Petter Selasky
84827e4bd81SHans Petter Selasky /* Calculate and return the exact baud rate. */
84927e4bd81SHans Petter Selasky baud = (baseline / mantissa) >> (exponent << 1);
85027e4bd81SHans Petter Selasky DPRINTF("real baud rate will be %u\n", baud);
85127e4bd81SHans Petter Selasky
85227e4bd81SHans Petter Selasky return baud;
85327e4bd81SHans Petter Selasky }
85402ac6454SAndrew Thompson static void
uplcom_cfg_param(struct ucom_softc * ucom,struct termios * t)855760bc48eSAndrew Thompson uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
85602ac6454SAndrew Thompson {
85702ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent;
858760bc48eSAndrew Thompson struct usb_cdc_line_state ls;
859760bc48eSAndrew Thompson struct usb_device_request req;
86002ac6454SAndrew Thompson
86102ac6454SAndrew Thompson DPRINTF("sc = %p\n", sc);
86202ac6454SAndrew Thompson
863271ae033SHans Petter Selasky memset(&ls, 0, sizeof(ls));
86402ac6454SAndrew Thompson
86527e4bd81SHans Petter Selasky /*
86627e4bd81SHans Petter Selasky * NOTE: If unsupported baud rates are set directly, the PL2303* uses 9600 baud.
86727e4bd81SHans Petter Selasky */
86827e4bd81SHans Petter Selasky if ((t->c_ospeed & 0x80000000) || uplcom_baud_supported(t->c_ospeed))
86902ac6454SAndrew Thompson USETDW(ls.dwDTERate, t->c_ospeed);
87027e4bd81SHans Petter Selasky else
87127e4bd81SHans Petter Selasky t->c_ospeed = uplcom_encode_baud_rate_divisor((uint8_t*)&ls.dwDTERate, t->c_ospeed);
87202ac6454SAndrew Thompson
87302ac6454SAndrew Thompson if (t->c_cflag & CSTOPB) {
87427e4bd81SHans Petter Selasky if ((t->c_cflag & CSIZE) == CS5) {
87527e4bd81SHans Petter Selasky /*
87627e4bd81SHans Petter Selasky * NOTE: Comply with "real" UARTs / RS232:
87727e4bd81SHans Petter Selasky * use 1.5 instead of 2 stop bits with 5 data bits
87827e4bd81SHans Petter Selasky */
87927e4bd81SHans Petter Selasky ls.bCharFormat = UCDC_STOP_BIT_1_5;
88027e4bd81SHans Petter Selasky } else {
88102ac6454SAndrew Thompson ls.bCharFormat = UCDC_STOP_BIT_2;
88227e4bd81SHans Petter Selasky }
88302ac6454SAndrew Thompson } else {
88402ac6454SAndrew Thompson ls.bCharFormat = UCDC_STOP_BIT_1;
88502ac6454SAndrew Thompson }
88602ac6454SAndrew Thompson
88702ac6454SAndrew Thompson if (t->c_cflag & PARENB) {
88802ac6454SAndrew Thompson if (t->c_cflag & PARODD) {
88902ac6454SAndrew Thompson ls.bParityType = UCDC_PARITY_ODD;
89002ac6454SAndrew Thompson } else {
89102ac6454SAndrew Thompson ls.bParityType = UCDC_PARITY_EVEN;
89202ac6454SAndrew Thompson }
89302ac6454SAndrew Thompson } else {
89402ac6454SAndrew Thompson ls.bParityType = UCDC_PARITY_NONE;
89502ac6454SAndrew Thompson }
89602ac6454SAndrew Thompson
89702ac6454SAndrew Thompson switch (t->c_cflag & CSIZE) {
89802ac6454SAndrew Thompson case CS5:
89902ac6454SAndrew Thompson ls.bDataBits = 5;
90002ac6454SAndrew Thompson break;
90102ac6454SAndrew Thompson case CS6:
90202ac6454SAndrew Thompson ls.bDataBits = 6;
90302ac6454SAndrew Thompson break;
90402ac6454SAndrew Thompson case CS7:
90502ac6454SAndrew Thompson ls.bDataBits = 7;
90602ac6454SAndrew Thompson break;
90702ac6454SAndrew Thompson case CS8:
90802ac6454SAndrew Thompson ls.bDataBits = 8;
90902ac6454SAndrew Thompson break;
91002ac6454SAndrew Thompson }
91102ac6454SAndrew Thompson
91227e4bd81SHans Petter Selasky DPRINTF("rate=0x%08x fmt=%d parity=%d bits=%d\n",
91302ac6454SAndrew Thompson UGETDW(ls.dwDTERate), ls.bCharFormat,
91402ac6454SAndrew Thompson ls.bParityType, ls.bDataBits);
91502ac6454SAndrew Thompson
91602ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
91702ac6454SAndrew Thompson req.bRequest = UCDC_SET_LINE_CODING;
91802ac6454SAndrew Thompson USETW(req.wValue, 0);
91902ac6454SAndrew Thompson req.wIndex[0] = sc->sc_data_iface_no;
92002ac6454SAndrew Thompson req.wIndex[1] = 0;
92102ac6454SAndrew Thompson USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
92202ac6454SAndrew Thompson
923a593f6b8SAndrew Thompson ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
92402ac6454SAndrew Thompson &req, &ls, 0, 1000);
92502ac6454SAndrew Thompson
92602ac6454SAndrew Thompson if (t->c_cflag & CRTSCTS) {
92702ac6454SAndrew Thompson DPRINTF("crtscts = on\n");
92802ac6454SAndrew Thompson
92902ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
930ea0efc37SHans Petter Selasky if (sc->sc_chiptype == TYPE_PL2303HXN) {
931ea0efc37SHans Petter Selasky req.bRequest = UPLCOM_SET_REQUEST_PL2303HXN;
932ea0efc37SHans Petter Selasky USETW(req.wValue, UPLCOM_CRTSCTS_REG_PL2303HXN);
933ea0efc37SHans Petter Selasky USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303HXN);
934ea0efc37SHans Petter Selasky } else {
93502ac6454SAndrew Thompson req.bRequest = UPLCOM_SET_REQUEST;
93602ac6454SAndrew Thompson USETW(req.wValue, 0);
93727e4bd81SHans Petter Selasky if (sc->sc_chiptype != TYPE_PL2303)
93802ac6454SAndrew Thompson USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
93902ac6454SAndrew Thompson else
94002ac6454SAndrew Thompson USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
941ea0efc37SHans Petter Selasky }
94202ac6454SAndrew Thompson USETW(req.wLength, 0);
94302ac6454SAndrew Thompson
944a593f6b8SAndrew Thompson ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
94502ac6454SAndrew Thompson &req, NULL, 0, 1000);
94602ac6454SAndrew Thompson } else {
94702ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
948ea0efc37SHans Petter Selasky if (sc->sc_chiptype == TYPE_PL2303HXN) {
949ea0efc37SHans Petter Selasky req.bRequest = UPLCOM_SET_REQUEST_PL2303HXN;
950ea0efc37SHans Petter Selasky USETW(req.wValue, UPLCOM_CRTSCTS_REG_PL2303HXN);
951ea0efc37SHans Petter Selasky USETW(req.wIndex, UPLCOM_CLEAR_CRTSCTS_PL2303HXN);
952ea0efc37SHans Petter Selasky }
953ea0efc37SHans Petter Selasky else {
95402ac6454SAndrew Thompson req.bRequest = UPLCOM_SET_REQUEST;
95502ac6454SAndrew Thompson USETW(req.wValue, 0);
95602ac6454SAndrew Thompson USETW(req.wIndex, 0);
957ea0efc37SHans Petter Selasky }
95802ac6454SAndrew Thompson USETW(req.wLength, 0);
959a593f6b8SAndrew Thompson ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
96002ac6454SAndrew Thompson &req, NULL, 0, 1000);
96102ac6454SAndrew Thompson }
96202ac6454SAndrew Thompson }
96302ac6454SAndrew Thompson
96402ac6454SAndrew Thompson static void
uplcom_start_read(struct ucom_softc * ucom)965760bc48eSAndrew Thompson uplcom_start_read(struct ucom_softc *ucom)
96602ac6454SAndrew Thompson {
96702ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent;
96802ac6454SAndrew Thompson
96902ac6454SAndrew Thompson /* start interrupt endpoint */
970a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
97102ac6454SAndrew Thompson
97202ac6454SAndrew Thompson /* start read endpoint */
973a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
97402ac6454SAndrew Thompson }
97502ac6454SAndrew Thompson
97602ac6454SAndrew Thompson static void
uplcom_stop_read(struct ucom_softc * ucom)977760bc48eSAndrew Thompson uplcom_stop_read(struct ucom_softc *ucom)
97802ac6454SAndrew Thompson {
97902ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent;
98002ac6454SAndrew Thompson
98102ac6454SAndrew Thompson /* stop interrupt endpoint */
982a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
98302ac6454SAndrew Thompson
98402ac6454SAndrew Thompson /* stop read endpoint */
985a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
98602ac6454SAndrew Thompson }
98702ac6454SAndrew Thompson
98802ac6454SAndrew Thompson static void
uplcom_start_write(struct ucom_softc * ucom)989760bc48eSAndrew Thompson uplcom_start_write(struct ucom_softc *ucom)
99002ac6454SAndrew Thompson {
99102ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent;
99202ac6454SAndrew Thompson
993a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
99402ac6454SAndrew Thompson }
99502ac6454SAndrew Thompson
99602ac6454SAndrew Thompson static void
uplcom_stop_write(struct ucom_softc * ucom)997760bc48eSAndrew Thompson uplcom_stop_write(struct ucom_softc *ucom)
99802ac6454SAndrew Thompson {
99902ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent;
100002ac6454SAndrew Thompson
1001a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
100202ac6454SAndrew Thompson }
100302ac6454SAndrew Thompson
100402ac6454SAndrew Thompson static void
uplcom_cfg_get_status(struct ucom_softc * ucom,uint8_t * lsr,uint8_t * msr)1005760bc48eSAndrew Thompson uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
100602ac6454SAndrew Thompson {
100702ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent;
100802ac6454SAndrew Thompson
100902ac6454SAndrew Thompson DPRINTF("\n");
101002ac6454SAndrew Thompson
101102ac6454SAndrew Thompson *lsr = sc->sc_lsr;
101202ac6454SAndrew Thompson *msr = sc->sc_msr;
101302ac6454SAndrew Thompson }
101402ac6454SAndrew Thompson
101502ac6454SAndrew Thompson static void
uplcom_intr_callback(struct usb_xfer * xfer,usb_error_t error)1016ed6d949aSAndrew Thompson uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
101702ac6454SAndrew Thompson {
1018ed6d949aSAndrew Thompson struct uplcom_softc *sc = usbd_xfer_softc(xfer);
1019ed6d949aSAndrew Thompson struct usb_page_cache *pc;
102002ac6454SAndrew Thompson uint8_t buf[9];
1021ed6d949aSAndrew Thompson int actlen;
1022ed6d949aSAndrew Thompson
1023ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
102402ac6454SAndrew Thompson
102502ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) {
102602ac6454SAndrew Thompson case USB_ST_TRANSFERRED:
102702ac6454SAndrew Thompson
1028ed6d949aSAndrew Thompson DPRINTF("actlen = %u\n", actlen);
102902ac6454SAndrew Thompson
1030ed6d949aSAndrew Thompson if (actlen >= 9) {
1031ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0);
1032ed6d949aSAndrew Thompson usbd_copy_out(pc, 0, buf, sizeof(buf));
103302ac6454SAndrew Thompson
103427e4bd81SHans Petter Selasky DPRINTF("status = 0x%02x\n", buf[UPLCOM_STATE_INDEX]);
103502ac6454SAndrew Thompson
103602ac6454SAndrew Thompson sc->sc_lsr = 0;
103702ac6454SAndrew Thompson sc->sc_msr = 0;
103802ac6454SAndrew Thompson
103927e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_CTS) {
104002ac6454SAndrew Thompson sc->sc_msr |= SER_CTS;
104102ac6454SAndrew Thompson }
104227e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_OVERRUN_ERROR) {
104327e4bd81SHans Petter Selasky sc->sc_lsr |= ULSR_OE;
104427e4bd81SHans Petter Selasky }
104527e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_PARITY_ERROR) {
104627e4bd81SHans Petter Selasky sc->sc_lsr |= ULSR_PE;
104727e4bd81SHans Petter Selasky }
104827e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_FRAME_ERROR) {
104927e4bd81SHans Petter Selasky sc->sc_lsr |= ULSR_FE;
105027e4bd81SHans Petter Selasky }
105127e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_RING) {
105227e4bd81SHans Petter Selasky sc->sc_msr |= SER_RI;
105327e4bd81SHans Petter Selasky }
105427e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_BREAK_ERROR) {
105527e4bd81SHans Petter Selasky sc->sc_lsr |= ULSR_BI;
105627e4bd81SHans Petter Selasky }
105727e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_DSR) {
105802ac6454SAndrew Thompson sc->sc_msr |= SER_DSR;
105902ac6454SAndrew Thompson }
106027e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_DCD) {
106102ac6454SAndrew Thompson sc->sc_msr |= SER_DCD;
106202ac6454SAndrew Thompson }
1063a593f6b8SAndrew Thompson ucom_status_change(&sc->sc_ucom);
106402ac6454SAndrew Thompson }
106502ac6454SAndrew Thompson case USB_ST_SETUP:
106602ac6454SAndrew Thompson tr_setup:
1067ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1068a593f6b8SAndrew Thompson usbd_transfer_submit(xfer);
106902ac6454SAndrew Thompson return;
107002ac6454SAndrew Thompson
107102ac6454SAndrew Thompson default: /* Error */
1072ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) {
107302ac6454SAndrew Thompson /* try to clear stall first */
1074ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer);
107502ac6454SAndrew Thompson goto tr_setup;
107602ac6454SAndrew Thompson }
107702ac6454SAndrew Thompson return;
107802ac6454SAndrew Thompson }
107902ac6454SAndrew Thompson }
108002ac6454SAndrew Thompson
108102ac6454SAndrew Thompson static void
uplcom_write_callback(struct usb_xfer * xfer,usb_error_t error)1082ed6d949aSAndrew Thompson uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
108302ac6454SAndrew Thompson {
1084ed6d949aSAndrew Thompson struct uplcom_softc *sc = usbd_xfer_softc(xfer);
1085ed6d949aSAndrew Thompson struct usb_page_cache *pc;
108602ac6454SAndrew Thompson uint32_t actlen;
108702ac6454SAndrew Thompson
108802ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) {
108902ac6454SAndrew Thompson case USB_ST_SETUP:
109002ac6454SAndrew Thompson case USB_ST_TRANSFERRED:
109102ac6454SAndrew Thompson tr_setup:
1092ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0);
1093ed6d949aSAndrew Thompson if (ucom_get_data(&sc->sc_ucom, pc, 0,
109402ac6454SAndrew Thompson UPLCOM_BULK_BUF_SIZE, &actlen)) {
109502ac6454SAndrew Thompson DPRINTF("actlen = %d\n", actlen);
109602ac6454SAndrew Thompson
1097ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, actlen);
1098a593f6b8SAndrew Thompson usbd_transfer_submit(xfer);
109902ac6454SAndrew Thompson }
1100ec97e9caSHans Petter Selasky return;
110102ac6454SAndrew Thompson
110202ac6454SAndrew Thompson default: /* Error */
1103ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) {
110402ac6454SAndrew Thompson /* try to clear stall first */
1105ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer);
110602ac6454SAndrew Thompson goto tr_setup;
110702ac6454SAndrew Thompson }
1108ec97e9caSHans Petter Selasky return;
110902ac6454SAndrew Thompson }
111002ac6454SAndrew Thompson }
111102ac6454SAndrew Thompson
111202ac6454SAndrew Thompson static void
uplcom_read_callback(struct usb_xfer * xfer,usb_error_t error)1113ed6d949aSAndrew Thompson uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
111402ac6454SAndrew Thompson {
1115ed6d949aSAndrew Thompson struct uplcom_softc *sc = usbd_xfer_softc(xfer);
1116ed6d949aSAndrew Thompson struct usb_page_cache *pc;
1117ed6d949aSAndrew Thompson int actlen;
1118ed6d949aSAndrew Thompson
1119ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
112002ac6454SAndrew Thompson
112102ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) {
112202ac6454SAndrew Thompson case USB_ST_TRANSFERRED:
1123ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0);
1124ed6d949aSAndrew Thompson ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
112502ac6454SAndrew Thompson
112602ac6454SAndrew Thompson case USB_ST_SETUP:
112702ac6454SAndrew Thompson tr_setup:
1128ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1129a593f6b8SAndrew Thompson usbd_transfer_submit(xfer);
113002ac6454SAndrew Thompson return;
113102ac6454SAndrew Thompson
113202ac6454SAndrew Thompson default: /* Error */
1133ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) {
113402ac6454SAndrew Thompson /* try to clear stall first */
1135ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer);
113602ac6454SAndrew Thompson goto tr_setup;
113702ac6454SAndrew Thompson }
113802ac6454SAndrew Thompson return;
113902ac6454SAndrew Thompson }
114002ac6454SAndrew Thompson }
1141655dc9d0SAndrew Thompson
1142655dc9d0SAndrew Thompson static void
uplcom_poll(struct ucom_softc * ucom)1143655dc9d0SAndrew Thompson uplcom_poll(struct ucom_softc *ucom)
1144655dc9d0SAndrew Thompson {
1145655dc9d0SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent;
1146655dc9d0SAndrew Thompson usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER);
1147655dc9d0SAndrew Thompson }
1148