Deleted Added
sdiff udiff text old ( 187176 ) new ( 187259 )
full compact
1/* $NetBSD: uplcom.c,v 1.21 2001/11/13 06:24:56 lukem Exp $ */
2
3#include <sys/cdefs.h>
4__FBSDID("$FreeBSD: head/sys/dev/usb2/serial/uplcom2.c 187176 2009-01-13 19:03:47Z thompsa $");
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 <dev/usb2/include/usb2_devid.h>
88#include <dev/usb2/include/usb2_standard.h>
89#include <dev/usb2/include/usb2_mfunc.h>
90#include <dev/usb2/include/usb2_error.h>
91#include <dev/usb2/include/usb2_cdc.h>
92
93#define USB_DEBUG_VAR uplcom_debug
94
95#include <dev/usb2/core/usb2_core.h>
96#include <dev/usb2/core/usb2_debug.h>
97#include <dev/usb2/core/usb2_process.h>
98#include <dev/usb2/core/usb2_request.h>
99#include <dev/usb2/core/usb2_lookup.h>
100#include <dev/usb2/core/usb2_util.h>
101#include <dev/usb2/core/usb2_busdma.h>
102
103#include <dev/usb2/serial/usb2_serial.h>
104
105#if USB_DEBUG
106static int uplcom_debug = 0;
107
108SYSCTL_NODE(_hw_usb2, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom");
109SYSCTL_INT(_hw_usb2_uplcom, OID_AUTO, debug, CTLFLAG_RW,
110 &uplcom_debug, 0, "Debug level");
111#endif
112
113#define UPLCOM_MODVER 1 /* module version */
114
115#define UPLCOM_CONFIG_INDEX 0
116#define UPLCOM_IFACE_INDEX 0
117#define UPLCOM_SECOND_IFACE_INDEX 1
118
119#ifndef UPLCOM_INTR_INTERVAL
120#define UPLCOM_INTR_INTERVAL 0 /* default */
121#endif
122
123#define UPLCOM_BULK_BUF_SIZE 1024 /* bytes */
124#define UPLCOM_N_TRANSFER 6
125
126#define UPLCOM_SET_REQUEST 0x01
127#define UPLCOM_SET_CRTSCTS 0x41
128#define UPLCOM_SET_CRTSCTS_PL2303X 0x61
129#define RSAQ_STATUS_CTS 0x80
130#define RSAQ_STATUS_DSR 0x02
131#define RSAQ_STATUS_DCD 0x01
132
133#define TYPE_PL2303 0
134#define TYPE_PL2303X 1
135
136struct uplcom_softc {
137 struct usb2_com_super_softc sc_super_ucom;
138 struct usb2_com_softc sc_ucom;
139
140 struct usb2_xfer *sc_xfer[UPLCOM_N_TRANSFER];
141 struct usb2_device *sc_udev;
142
143 uint16_t sc_line;
144
145 uint8_t sc_flag;
146#define UPLCOM_FLAG_INTR_STALL 0x01
147#define UPLCOM_FLAG_READ_STALL 0x02
148#define UPLCOM_FLAG_WRITE_STALL 0x04
149
150 uint8_t sc_lsr; /* local status register */
151 uint8_t sc_msr; /* uplcom status register */
152 uint8_t sc_chiptype; /* type of chip */
153 uint8_t sc_ctrl_iface_no;
154 uint8_t sc_data_iface_no;
155 uint8_t sc_iface_index[2];
156};
157
158/* prototypes */
159
160static usb2_error_t uplcom_reset(struct uplcom_softc *, struct usb2_device *);
161static int uplcom_pl2303x_init(struct usb2_device *);
162static void uplcom_cfg_set_dtr(struct usb2_com_softc *, uint8_t);
163static void uplcom_cfg_set_rts(struct usb2_com_softc *, uint8_t);
164static void uplcom_cfg_set_break(struct usb2_com_softc *, uint8_t);
165static int uplcom_pre_param(struct usb2_com_softc *, struct termios *);
166static void uplcom_cfg_param(struct usb2_com_softc *, struct termios *);
167static void uplcom_start_read(struct usb2_com_softc *);
168static void uplcom_stop_read(struct usb2_com_softc *);
169static void uplcom_start_write(struct usb2_com_softc *);
170static void uplcom_stop_write(struct usb2_com_softc *);
171static void uplcom_cfg_get_status(struct usb2_com_softc *, uint8_t *,
172 uint8_t *);
173static void uplcom_cfg_do_request(struct uplcom_softc *,
174 struct usb2_device_request *, void *);
175
176static device_probe_t uplcom_probe;
177static device_attach_t uplcom_attach;
178static device_detach_t uplcom_detach;
179
180static usb2_callback_t uplcom_intr_callback;
181static usb2_callback_t uplcom_intr_clear_stall_callback;
182static usb2_callback_t uplcom_write_callback;
183static usb2_callback_t uplcom_write_clear_stall_callback;
184static usb2_callback_t uplcom_read_callback;
185static usb2_callback_t uplcom_read_clear_stall_callback;
186
187static const struct usb2_config uplcom_config_data[UPLCOM_N_TRANSFER] = {
188
189 [0] = {
190 .type = UE_BULK,
191 .endpoint = UE_ADDR_ANY,
192 .direction = UE_DIR_OUT,
193 .mh.bufsize = UPLCOM_BULK_BUF_SIZE,
194 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
195 .mh.callback = &uplcom_write_callback,
196 .if_index = 0,
197 },
198
199 [1] = {
200 .type = UE_BULK,
201 .endpoint = UE_ADDR_ANY,
202 .direction = UE_DIR_IN,
203 .mh.bufsize = UPLCOM_BULK_BUF_SIZE,
204 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
205 .mh.callback = &uplcom_read_callback,
206 .if_index = 0,
207 },
208
209 [2] = {
210 .type = UE_CONTROL,
211 .endpoint = 0x00, /* Control pipe */
212 .direction = UE_DIR_ANY,
213 .mh.bufsize = sizeof(struct usb2_device_request),
214 .mh.callback = &uplcom_write_clear_stall_callback,
215 .mh.timeout = 1000, /* 1 second */
216 .mh.interval = 50, /* 50ms */
217 .if_index = 0,
218 },
219
220 [3] = {
221 .type = UE_CONTROL,
222 .endpoint = 0x00, /* Control pipe */
223 .direction = UE_DIR_ANY,
224 .mh.bufsize = sizeof(struct usb2_device_request),
225 .mh.callback = &uplcom_read_clear_stall_callback,
226 .mh.timeout = 1000, /* 1 second */
227 .mh.interval = 50, /* 50ms */
228 .if_index = 0,
229 },
230
231 [4] = {
232 .type = UE_INTERRUPT,
233 .endpoint = UE_ADDR_ANY,
234 .direction = UE_DIR_IN,
235 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
236 .mh.bufsize = 0, /* use wMaxPacketSize */
237 .mh.callback = &uplcom_intr_callback,
238 .if_index = 1,
239 },
240
241 [5] = {
242 .type = UE_CONTROL,
243 .endpoint = 0x00, /* Control pipe */
244 .direction = UE_DIR_ANY,
245 .mh.bufsize = sizeof(struct usb2_device_request),
246 .mh.callback = &uplcom_intr_clear_stall_callback,
247 .mh.timeout = 1000, /* 1 second */
248 .mh.interval = 50, /* 50ms */
249 .if_index = 1,
250 },
251};
252
253struct usb2_com_callback uplcom_callback = {
254 .usb2_com_cfg_get_status = &uplcom_cfg_get_status,
255 .usb2_com_cfg_set_dtr = &uplcom_cfg_set_dtr,
256 .usb2_com_cfg_set_rts = &uplcom_cfg_set_rts,
257 .usb2_com_cfg_set_break = &uplcom_cfg_set_break,
258 .usb2_com_cfg_param = &uplcom_cfg_param,
259 .usb2_com_pre_param = &uplcom_pre_param,
260 .usb2_com_start_read = &uplcom_start_read,
261 .usb2_com_stop_read = &uplcom_stop_read,
262 .usb2_com_start_write = &uplcom_start_write,
263 .usb2_com_stop_write = &uplcom_stop_write,
264};
265
266#define USB_UPL(v,p,rl,rh,t) \
267 USB_VENDOR(v), USB_PRODUCT(p), USB_DEV_BCD_GTEQ(rl), \
268 USB_DEV_BCD_LTEQ(rh), USB_DRIVER_INFO(t)
269
270static const struct usb2_device_id uplcom_devs[] = {
271 /* Belkin F5U257 */
272 {USB_UPL(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U257, 0, 0xFFFF, TYPE_PL2303X)},
273 /* I/O DATA USB-RSAQ */
274 {USB_UPL(USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ, 0, 0xFFFF, TYPE_PL2303)},
275 /* I/O DATA USB-RSAQ2 */
276 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2, 0, 0xFFFF, TYPE_PL2303)},
277 /* I/O DATA USB-RSAQ3 */
278 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3, 0, 0xFFFF, TYPE_PL2303X)},
279 /* PLANEX USB-RS232 URS-03 */
280 {USB_UPL(USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A, 0, 0xFFFF, TYPE_PL2303)},
281 /* TrendNet TU-S9 */
282 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303, 0x0400, 0xFFFF, TYPE_PL2303X)},
283 /* ST Lab USB-SERIAL-4 */
284 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303, 0x0300, 0x03FF, TYPE_PL2303X)},
285 /* IOGEAR/ATEN UC-232A (also ST Lab USB-SERIAL-1) */
286 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303, 0, 0x02FF, TYPE_PL2303)},
287 /* TDK USB-PHS Adapter UHA6400 */
288 {USB_UPL(USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400, 0, 0xFFFF, TYPE_PL2303)},
289 /* RATOC REX-USB60 */
290 {USB_UPL(USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60, 0, 0xFFFF, TYPE_PL2303)},
291 /* ELECOM UC-SGT */
292 {USB_UPL(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT, 0, 0xFFFF, TYPE_PL2303)},
293 {USB_UPL(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0, 0, 0xFFFF, TYPE_PL2303)},
294 /* Sagem USB-Serial Controller */
295 {USB_UPL(USB_VENDOR_SAGEM, USB_PRODUCT_SAGEM_USBSERIAL, 0, 0xFFFF, TYPE_PL2303X)},
296 /* Sony Ericsson USB Cable */
297 {USB_UPL(USB_VENDOR_SONYERICSSON, USB_PRODUCT_SONYERICSSON_DCU10, 0, 0xFFFF, TYPE_PL2303)},
298 /* SOURCENEXT KeikaiDenwa 8 */
299 {USB_UPL(USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8, 0, 0xFFFF, TYPE_PL2303)},
300 /* SOURCENEXT KeikaiDenwa 8 with charger */
301 {USB_UPL(USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG, 0, 0, TYPE_PL2303)},
302 /* HAL Corporation Crossam2+USB */
303 {USB_UPL(USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001, 0, 0xFFFF, TYPE_PL2303)},
304 /* Sitecom USB to Serial */
305 {USB_UPL(USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_SERIAL, 0, 0xFFFF, TYPE_PL2303)},
306 /* Tripp-Lite U209-000-R */
307 {USB_UPL(USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209, 0, 0xFFFF, TYPE_PL2303X)},
308 {USB_UPL(USB_VENDOR_RADIOSHACK, USB_PRODUCT_RADIOSHACK_USBCABLE, 0, 0xFFFF, TYPE_PL2303)},
309 /* Prolific Pharos */
310 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PHAROS, 0, 0xFFFF, TYPE_PL2303)},
311 /* Willcom W-SIM */
312 {USB_UPL(USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_WSIM, 0, 0xFFFF, TYPE_PL2303X)},
313};
314
315static device_method_t uplcom_methods[] = {
316 DEVMETHOD(device_probe, uplcom_probe),
317 DEVMETHOD(device_attach, uplcom_attach),
318 DEVMETHOD(device_detach, uplcom_detach),
319 {0, 0}
320};
321
322static devclass_t uplcom_devclass;
323
324static driver_t uplcom_driver = {
325 .name = "uplcom",
326 .methods = uplcom_methods,
327 .size = sizeof(struct uplcom_softc),
328};
329
330DRIVER_MODULE(uplcom, ushub, uplcom_driver, uplcom_devclass, NULL, 0);
331MODULE_DEPEND(uplcom, usb2_serial, 1, 1, 1);
332MODULE_DEPEND(uplcom, usb2_core, 1, 1, 1);
333MODULE_VERSION(uplcom, UPLCOM_MODVER);
334
335static int
336uplcom_probe(device_t dev)
337{
338 struct usb2_attach_arg *uaa = device_get_ivars(dev);
339
340 DPRINTFN(11, "\n");
341
342 if (uaa->usb2_mode != USB_MODE_HOST) {
343 return (ENXIO);
344 }
345 if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
346 return (ENXIO);
347 }
348 if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
349 return (ENXIO);
350 }
351 return (usb2_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
352}
353
354static int
355uplcom_attach(device_t dev)
356{
357 struct usb2_attach_arg *uaa = device_get_ivars(dev);
358 struct uplcom_softc *sc = device_get_softc(dev);
359 struct usb2_interface *iface;
360 struct usb2_interface_descriptor *id;
361 int error;
362
363 DPRINTFN(11, "\n");
364
365 if (sc == NULL) {
366 return (ENOMEM);
367 }
368 device_set_usb2_desc(dev);
369
370 DPRINTF("sc = %p\n", sc);
371
372 sc->sc_chiptype = USB_GET_DRIVER_INFO(uaa);
373 sc->sc_udev = uaa->device;
374
375 DPRINTF("chiptype: %s\n",
376 (sc->sc_chiptype == TYPE_PL2303X) ?
377 "2303X" : "2303");
378
379 /*
380 * USB-RSAQ1 has two interface
381 *
382 * USB-RSAQ1 | USB-RSAQ2
383 * -----------------+-----------------
384 * Interface 0 |Interface 0
385 * Interrupt(0x81) | Interrupt(0x81)
386 * -----------------+ BulkIN(0x02)
387 * Interface 1 | BulkOUT(0x83)
388 * BulkIN(0x02) |
389 * BulkOUT(0x83) |
390 */
391
392 sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
393 sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
394
395 iface = usb2_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
396 if (iface) {
397 id = usb2_get_interface_descriptor(iface);
398 if (id == NULL) {
399 device_printf(dev, "no interface descriptor (2)!\n");
400 goto detach;
401 }
402 sc->sc_data_iface_no = id->bInterfaceNumber;
403 sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
404 usb2_set_parent_iface(uaa->device,
405 UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
406 } else {
407 sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
408 sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
409 }
410
411 error = usb2_transfer_setup(uaa->device,
412 sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
413 UPLCOM_N_TRANSFER, sc, &Giant);
414 if (error) {
415 DPRINTF("one or more missing USB endpoints, "
416 "error=%s\n", usb2_errstr(error));
417 goto detach;
418 }
419 error = uplcom_reset(sc, uaa->device);
420 if (error) {
421 device_printf(dev, "reset failed, error=%s\n",
422 usb2_errstr(error));
423 goto detach;
424 }
425 /* clear stall at first run */
426 sc->sc_flag |= (UPLCOM_FLAG_READ_STALL |
427 UPLCOM_FLAG_WRITE_STALL);
428
429 error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
430 &uplcom_callback, &Giant);
431 if (error) {
432 goto detach;
433 }
434 /*
435 * do the initialization during attach so that the system does not
436 * sleep during open:
437 */
438 if (sc->sc_chiptype == TYPE_PL2303X) {
439 if (uplcom_pl2303x_init(uaa->device)) {
440 device_printf(dev, "init failed!\n");
441 goto detach;
442 }
443 }
444 return (0);
445
446detach:
447 uplcom_detach(dev);
448 return (ENXIO);
449}
450
451static int
452uplcom_detach(device_t dev)
453{
454 struct uplcom_softc *sc = device_get_softc(dev);
455
456 DPRINTF("sc=%p\n", sc);
457
458 usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
459
460 usb2_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
461
462 return (0);
463}
464
465static usb2_error_t
466uplcom_reset(struct uplcom_softc *sc, struct usb2_device *udev)
467{
468 struct usb2_device_request req;
469
470 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
471 req.bRequest = UPLCOM_SET_REQUEST;
472 USETW(req.wValue, 0);
473 req.wIndex[0] = sc->sc_data_iface_no;
474 req.wIndex[1] = 0;
475 USETW(req.wLength, 0);
476
477 return (usb2_do_request(udev, &Giant, &req, NULL));
478}
479
480struct pl2303x_init {
481 uint8_t req_type;
482 uint8_t request;
483 uint16_t value;
484 uint16_t index;
485 uint16_t length;
486};
487
488static const struct pl2303x_init pl2303x[] = {
489 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
490 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0},
491 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
492 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1},
493 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
494 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0},
495 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
496 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1},
497 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0},
498 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0},
499 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0},
500 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 8, 0, 0},
501 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 9, 0, 0},
502};
503
504#define N_PL2302X_INIT (sizeof(pl2303x)/sizeof(pl2303x[0]))
505
506static int
507uplcom_pl2303x_init(struct usb2_device *udev)
508{
509 struct usb2_device_request req;
510 usb2_error_t err;
511 uint8_t buf[4];
512 uint8_t i;
513
514 for (i = 0; i != N_PL2302X_INIT; i++) {
515 req.bmRequestType = pl2303x[i].req_type;
516 req.bRequest = pl2303x[i].request;
517 USETW(req.wValue, pl2303x[i].value);
518 USETW(req.wIndex, pl2303x[i].index);
519 USETW(req.wLength, pl2303x[i].length);
520
521 err = usb2_do_request(udev, &Giant, &req, buf);
522 if (err) {
523 DPRINTF("error=%s\n", usb2_errstr(err));
524 return (EIO);
525 }
526 }
527 return (0);
528}
529
530static void
531uplcom_cfg_set_dtr(struct usb2_com_softc *ucom, uint8_t onoff)
532{
533 struct uplcom_softc *sc = ucom->sc_parent;
534 struct usb2_device_request req;
535
536 DPRINTF("onoff = %d\n", onoff);
537
538 if (onoff)
539 sc->sc_line |= UCDC_LINE_DTR;
540 else
541 sc->sc_line &= ~UCDC_LINE_DTR;
542
543 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
544 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
545 USETW(req.wValue, sc->sc_line);
546 req.wIndex[0] = sc->sc_data_iface_no;
547 req.wIndex[1] = 0;
548 USETW(req.wLength, 0);
549
550 uplcom_cfg_do_request(sc, &req, NULL);
551}
552
553static void
554uplcom_cfg_set_rts(struct usb2_com_softc *ucom, uint8_t onoff)
555{
556 struct uplcom_softc *sc = ucom->sc_parent;
557 struct usb2_device_request req;
558
559 DPRINTF("onoff = %d\n", onoff);
560
561 if (onoff)
562 sc->sc_line |= UCDC_LINE_RTS;
563 else
564 sc->sc_line &= ~UCDC_LINE_RTS;
565
566 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
567 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
568 USETW(req.wValue, sc->sc_line);
569 req.wIndex[0] = sc->sc_data_iface_no;
570 req.wIndex[1] = 0;
571 USETW(req.wLength, 0);
572
573 uplcom_cfg_do_request(sc, &req, NULL);
574}
575
576static void
577uplcom_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff)
578{
579 struct uplcom_softc *sc = ucom->sc_parent;
580 struct usb2_device_request req;
581 uint16_t temp;
582
583 DPRINTF("onoff = %d\n", onoff);
584
585 temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
586
587 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
588 req.bRequest = UCDC_SEND_BREAK;
589 USETW(req.wValue, temp);
590 req.wIndex[0] = sc->sc_data_iface_no;
591 req.wIndex[1] = 0;
592 USETW(req.wLength, 0);
593
594 uplcom_cfg_do_request(sc, &req, NULL);
595}
596
597static const int32_t uplcom_rates[] = {
598 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
599 19200, 28800, 38400, 57600, 115200,
600 /*
601 * Higher speeds are probably possible. PL2303X supports up to
602 * 6Mb and can set any rate
603 */
604 230400, 460800, 614400, 921600, 1228800
605};
606
607#define N_UPLCOM_RATES (sizeof(uplcom_rates)/sizeof(uplcom_rates[0]))
608
609static int
610uplcom_pre_param(struct usb2_com_softc *ucom, struct termios *t)
611{
612 uint8_t i;
613
614 DPRINTF("\n");
615
616 /* check requested baud rate */
617
618 for (i = 0;; i++) {
619
620 if (i != N_UPLCOM_RATES) {
621 if (uplcom_rates[i] == t->c_ospeed) {
622 break;
623 }
624 } else {
625 DPRINTF("invalid baud rate (%d)\n", t->c_ospeed);
626 return (EIO);
627 }
628 }
629
630 return (0);
631}
632
633static void
634uplcom_cfg_param(struct usb2_com_softc *ucom, struct termios *t)
635{
636 struct uplcom_softc *sc = ucom->sc_parent;
637 struct usb2_cdc_line_state ls;
638 struct usb2_device_request req;
639
640 DPRINTF("sc = %p\n", sc);
641
642 bzero(&ls, sizeof(ls));
643
644 USETDW(ls.dwDTERate, t->c_ospeed);
645
646 if (t->c_cflag & CSTOPB) {
647 ls.bCharFormat = UCDC_STOP_BIT_2;
648 } else {
649 ls.bCharFormat = UCDC_STOP_BIT_1;
650 }
651
652 if (t->c_cflag & PARENB) {
653 if (t->c_cflag & PARODD) {
654 ls.bParityType = UCDC_PARITY_ODD;
655 } else {
656 ls.bParityType = UCDC_PARITY_EVEN;
657 }
658 } else {
659 ls.bParityType = UCDC_PARITY_NONE;
660 }
661
662 switch (t->c_cflag & CSIZE) {
663 case CS5:
664 ls.bDataBits = 5;
665 break;
666 case CS6:
667 ls.bDataBits = 6;
668 break;
669 case CS7:
670 ls.bDataBits = 7;
671 break;
672 case CS8:
673 ls.bDataBits = 8;
674 break;
675 }
676
677 DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
678 UGETDW(ls.dwDTERate), ls.bCharFormat,
679 ls.bParityType, ls.bDataBits);
680
681 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
682 req.bRequest = UCDC_SET_LINE_CODING;
683 USETW(req.wValue, 0);
684 req.wIndex[0] = sc->sc_data_iface_no;
685 req.wIndex[1] = 0;
686 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
687
688 uplcom_cfg_do_request(sc, &req, &ls);
689
690 if (t->c_cflag & CRTSCTS) {
691
692 DPRINTF("crtscts = on\n");
693
694 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
695 req.bRequest = UPLCOM_SET_REQUEST;
696 USETW(req.wValue, 0);
697 if (sc->sc_chiptype == TYPE_PL2303X)
698 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
699 else
700 USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
701 USETW(req.wLength, 0);
702
703 uplcom_cfg_do_request(sc, &req, NULL);
704 } else {
705 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
706 req.bRequest = UPLCOM_SET_REQUEST;
707 USETW(req.wValue, 0);
708 USETW(req.wIndex, 0);
709 USETW(req.wLength, 0);
710 uplcom_cfg_do_request(sc, &req, NULL);
711 }
712}
713
714static void
715uplcom_start_read(struct usb2_com_softc *ucom)
716{
717 struct uplcom_softc *sc = ucom->sc_parent;
718
719 /* start interrupt endpoint */
720 usb2_transfer_start(sc->sc_xfer[4]);
721
722 /* start read endpoint */
723 usb2_transfer_start(sc->sc_xfer[1]);
724}
725
726static void
727uplcom_stop_read(struct usb2_com_softc *ucom)
728{
729 struct uplcom_softc *sc = ucom->sc_parent;
730
731 /* stop interrupt endpoint */
732 usb2_transfer_stop(sc->sc_xfer[4]);
733
734 /* stop read endpoint */
735 usb2_transfer_stop(sc->sc_xfer[3]);
736 usb2_transfer_stop(sc->sc_xfer[1]);
737}
738
739static void
740uplcom_start_write(struct usb2_com_softc *ucom)
741{
742 struct uplcom_softc *sc = ucom->sc_parent;
743
744 usb2_transfer_start(sc->sc_xfer[0]);
745}
746
747static void
748uplcom_stop_write(struct usb2_com_softc *ucom)
749{
750 struct uplcom_softc *sc = ucom->sc_parent;
751
752 usb2_transfer_stop(sc->sc_xfer[2]);
753 usb2_transfer_stop(sc->sc_xfer[0]);
754}
755
756static void
757uplcom_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr)
758{
759 struct uplcom_softc *sc = ucom->sc_parent;
760
761 DPRINTF("\n");
762
763 *lsr = sc->sc_lsr;
764 *msr = sc->sc_msr;
765}
766
767static void
768uplcom_intr_callback(struct usb2_xfer *xfer)
769{
770 struct uplcom_softc *sc = xfer->priv_sc;
771 uint8_t buf[9];
772
773 switch (USB_GET_STATE(xfer)) {
774 case USB_ST_TRANSFERRED:
775
776 DPRINTF("actlen = %u\n", xfer->actlen);
777
778 if (xfer->actlen >= 9) {
779
780 usb2_copy_out(xfer->frbuffers, 0, buf, sizeof(buf));
781
782 DPRINTF("status = 0x%02x\n", buf[8]);
783
784 sc->sc_lsr = 0;
785 sc->sc_msr = 0;
786
787 if (buf[8] & RSAQ_STATUS_CTS) {
788 sc->sc_msr |= SER_CTS;
789 }
790 if (buf[8] & RSAQ_STATUS_DSR) {
791 sc->sc_msr |= SER_DSR;
792 }
793 if (buf[8] & RSAQ_STATUS_DCD) {
794 sc->sc_msr |= SER_DCD;
795 }
796 usb2_com_status_change(&sc->sc_ucom);
797 }
798 case USB_ST_SETUP:
799 if (sc->sc_flag & UPLCOM_FLAG_INTR_STALL) {
800 usb2_transfer_start(sc->sc_xfer[5]);
801 } else {
802 xfer->frlengths[0] = xfer->max_data_length;
803 usb2_start_hardware(xfer);
804 }
805 return;
806
807 default: /* Error */
808 if (xfer->error != USB_ERR_CANCELLED) {
809 sc->sc_flag |= UPLCOM_FLAG_INTR_STALL;
810 usb2_transfer_start(sc->sc_xfer[5]);
811 }
812 return;
813
814 }
815}
816
817static void
818uplcom_intr_clear_stall_callback(struct usb2_xfer *xfer)
819{
820 struct uplcom_softc *sc = xfer->priv_sc;
821 struct usb2_xfer *xfer_other = sc->sc_xfer[4];
822
823 if (usb2_clear_stall_callback(xfer, xfer_other)) {
824 DPRINTF("stall cleared\n");
825 sc->sc_flag &= ~UPLCOM_FLAG_INTR_STALL;
826 usb2_transfer_start(xfer_other);
827 }
828}
829
830static void
831uplcom_write_callback(struct usb2_xfer *xfer)
832{
833 struct uplcom_softc *sc = xfer->priv_sc;
834 uint32_t actlen;
835
836 switch (USB_GET_STATE(xfer)) {
837 case USB_ST_SETUP:
838 case USB_ST_TRANSFERRED:
839 if (sc->sc_flag & UPLCOM_FLAG_WRITE_STALL) {
840 usb2_transfer_start(sc->sc_xfer[2]);
841 return;
842 }
843 if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0,
844 UPLCOM_BULK_BUF_SIZE, &actlen)) {
845
846 DPRINTF("actlen = %d\n", actlen);
847
848 xfer->frlengths[0] = actlen;
849 usb2_start_hardware(xfer);
850 }
851 return;
852
853 default: /* Error */
854 if (xfer->error != USB_ERR_CANCELLED) {
855 sc->sc_flag |= UPLCOM_FLAG_WRITE_STALL;
856 usb2_transfer_start(sc->sc_xfer[2]);
857 }
858 return;
859
860 }
861}
862
863static void
864uplcom_write_clear_stall_callback(struct usb2_xfer *xfer)
865{
866 struct uplcom_softc *sc = xfer->priv_sc;
867 struct usb2_xfer *xfer_other = sc->sc_xfer[0];
868
869 if (usb2_clear_stall_callback(xfer, xfer_other)) {
870 DPRINTF("stall cleared\n");
871 sc->sc_flag &= ~UPLCOM_FLAG_WRITE_STALL;
872 usb2_transfer_start(xfer_other);
873 }
874}
875
876static void
877uplcom_read_callback(struct usb2_xfer *xfer)
878{
879 struct uplcom_softc *sc = xfer->priv_sc;
880
881 switch (USB_GET_STATE(xfer)) {
882 case USB_ST_TRANSFERRED:
883 usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers, 0, xfer->actlen);
884
885 case USB_ST_SETUP:
886 if (sc->sc_flag & UPLCOM_FLAG_READ_STALL) {
887 usb2_transfer_start(sc->sc_xfer[3]);
888 } else {
889 xfer->frlengths[0] = xfer->max_data_length;
890 usb2_start_hardware(xfer);
891 }
892 return;
893
894 default: /* Error */
895 if (xfer->error != USB_ERR_CANCELLED) {
896 sc->sc_flag |= UPLCOM_FLAG_READ_STALL;
897 usb2_transfer_start(sc->sc_xfer[3]);
898 }
899 return;
900
901 }
902}
903
904static void
905uplcom_read_clear_stall_callback(struct usb2_xfer *xfer)
906{
907 struct uplcom_softc *sc = xfer->priv_sc;
908 struct usb2_xfer *xfer_other = sc->sc_xfer[1];
909
910 if (usb2_clear_stall_callback(xfer, xfer_other)) {
911 DPRINTF("stall cleared\n");
912 sc->sc_flag &= ~UPLCOM_FLAG_READ_STALL;
913 usb2_transfer_start(xfer_other);
914 }
915}
916
917static void
918uplcom_cfg_do_request(struct uplcom_softc *sc, struct usb2_device_request *req,
919 void *data)
920{
921 uint16_t length;
922 usb2_error_t err;
923
924 if (usb2_com_cfg_is_gone(&sc->sc_ucom)) {
925 goto error;
926 }
927 err = usb2_do_request_flags(sc->sc_udev, &Giant, req,
928 data, 0, NULL, 1000);
929
930 if (err) {
931
932 DPRINTFN(0, "device request failed, err=%s "
933 "(ignored)\n", usb2_errstr(err));
934
935error:
936 length = UGETW(req->wLength);
937
938 if ((req->bmRequestType & UT_READ) && length) {
939 bzero(data, length);
940 }
941 }
942}