1 /* $NetBSD: uplcom.c,v 1.69 2009/11/12 19:58:27 dyoung Exp $ */
3 * Copyright (c) 2001 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Ichiro FUKUHARA (ichiro@ichiro.org).
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
32 * General information: http://www.prolific.com.tw/fr_pl2303.htm
33 * http://www.hitachi-hitec.com/jyouhou/prolific/2303.pdf
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: uplcom.c,v 1.69 2009/11/12 19:58:27 dyoung Exp $");
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/ioctl.h>
47 #include <sys/select.h>
49 #include <sys/device.h>
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbcdc.h>
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usbdi_util.h>
57 #include <dev/usb/usbdevs.h>
58 #include <dev/usb/usb_quirks.h>
60 #include <dev/usb/ucomvar.h>
63 #define DPRINTFN(n, x) if (uplcomdebug > (n)) logprintf x
66 #define DPRINTFN(n, x)
68 #define DPRINTF(x) DPRINTFN(0, x)
70 #define UPLCOM_CONFIG_INDEX 0
71 #define UPLCOM_IFACE_INDEX 0
72 #define UPLCOM_SECOND_IFACE_INDEX 1
74 #define UPLCOM_SET_REQUEST 0x01
75 #define UPLCOM_SET_CRTSCTS_0 0x41
76 #define UPLCOM_SET_CRTSCTS_HX 0x61
78 #define UPLCOM_N_SERIAL_CTS 0x80
81 UPLCOM_TYPE_0
, /* we use this for all non-HX variants */
86 USBBASEDEVICE sc_dev
; /* base device */
87 usbd_device_handle sc_udev
; /* USB device */
88 usbd_interface_handle sc_iface
; /* interface */
89 int sc_iface_number
; /* interface number */
91 usbd_interface_handle sc_intr_iface
; /* interrupt interface */
92 int sc_intr_number
; /* interrupt number */
93 usbd_pipe_handle sc_intr_pipe
; /* interrupt pipe */
94 u_char
*sc_intr_buf
; /* interrupt buffer */
97 usb_cdc_line_state_t sc_line_state
; /* current line state */
98 int sc_dtr
; /* current DTR state */
99 int sc_rts
; /* current RTS state */
101 device_ptr_t sc_subdev
; /* ucom device */
103 u_char sc_dying
; /* disconnecting */
105 u_char sc_lsr
; /* Local status register */
106 u_char sc_msr
; /* uplcom status register */
108 enum pl2303_type sc_type
; /* PL2303 chip type */
112 * These are the maximum number of bytes transferred per frame.
113 * The output buffer size cannot be increased due to the size encoding.
115 #define UPLCOMIBUFSIZE 256
116 #define UPLCOMOBUFSIZE 256
118 Static usbd_status
uplcom_reset(struct uplcom_softc
*);
119 Static usbd_status
uplcom_set_line_coding(struct uplcom_softc
*sc
,
120 usb_cdc_line_state_t
*state
);
121 Static usbd_status
uplcom_set_crtscts(struct uplcom_softc
*);
122 Static
void uplcom_intr(usbd_xfer_handle
, usbd_private_handle
, usbd_status
);
124 Static
void uplcom_set(void *, int, int, int);
125 Static
void uplcom_dtr(struct uplcom_softc
*, int);
126 Static
void uplcom_rts(struct uplcom_softc
*, int);
127 Static
void uplcom_break(struct uplcom_softc
*, int);
128 Static
void uplcom_set_line_state(struct uplcom_softc
*);
129 Static
void uplcom_get_status(void *, int portno
, u_char
*lsr
, u_char
*msr
);
131 Static
int uplcom_ioctl(void *, int, u_long
, void *, int, usb_proc_ptr
);
133 Static
int uplcom_param(void *, int, struct termios
*);
134 Static
int uplcom_open(void *, int);
135 Static
void uplcom_close(void *, int);
136 Static usbd_status
uplcom_vendor_control_write(usbd_device_handle
, u_int16_t
, u_int16_t
);
138 struct ucom_methods uplcom_methods
= {
142 NULL
, /* uplcom_ioctl, TODO */
149 static const struct usb_devno uplcom_devs
[] = {
150 /* I/O DATA USB-RSAQ2 */
151 { USB_VENDOR_PROLIFIC
, USB_PRODUCT_PROLIFIC_RSAQ2
},
152 /* I/O DATA USB-RSAQ3 */
153 { USB_VENDOR_PROLIFIC
, USB_PRODUCT_PROLIFIC_RSAQ3
},
154 /* I/O DATA USB-RSAQ */
155 { USB_VENDOR_IODATA
, USB_PRODUCT_IODATA_USBRSAQ
},
156 /* I/O DATA USB-RSAQ5 */
157 { USB_VENDOR_IODATA
, USB_PRODUCT_IODATA_USBRSAQ5
},
158 /* PLANEX USB-RS232 URS-03 */
159 { USB_VENDOR_ATEN
, USB_PRODUCT_ATEN_UC232A
},
161 { USB_VENDOR_PROLIFIC
, USB_PRODUCT_PROLIFIC_PL2303
},
162 /* SMART Technologies USB to serial */
163 { USB_VENDOR_PROLIFIC2
, USB_PRODUCT_PROLIFIC2_PL2303
},
164 /* IOGEAR/ATENTRIPPLITE */
165 { USB_VENDOR_TRIPPLITE
, USB_PRODUCT_TRIPPLITE_U209
},
167 { USB_VENDOR_ELECOM
, USB_PRODUCT_ELECOM_UCSGT
},
169 { USB_VENDOR_ELECOM
, USB_PRODUCT_ELECOM_UCSGT0
},
170 /* Panasonic 50" Touch Panel */
171 { USB_VENDOR_PANASONIC
, USB_PRODUCT_PANASONIC_TYTP50P6S
},
172 /* RATOC REX-USB60 */
173 { USB_VENDOR_RATOC
, USB_PRODUCT_RATOC_REXUSB60
},
174 /* TDK USB-PHS Adapter UHA6400 */
175 { USB_VENDOR_TDK
, USB_PRODUCT_TDK_UHA6400
},
176 /* TDK USB-PDC Adapter UPA9664 */
177 { USB_VENDOR_TDK
, USB_PRODUCT_TDK_UPA9664
},
178 /* Sony Ericsson USB Cable */
179 { USB_VENDOR_SUSTEEN
, USB_PRODUCT_SUSTEEN_DCU10
},
180 /* SOURCENEXT KeikaiDenwa 8 */
181 { USB_VENDOR_SOURCENEXT
, USB_PRODUCT_SOURCENEXT_KEIKAI8
},
182 /* SOURCENEXT KeikaiDenwa 8 with charger */
183 { USB_VENDOR_SOURCENEXT
, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG
},
184 /* HAL Corporation Crossam2+USB */
185 { USB_VENDOR_HAL
, USB_PRODUCT_HAL_IMR001
},
186 /* Sitecom USB to serial cable */
187 { USB_VENDOR_SITECOM
, USB_PRODUCT_SITECOM_CN104
},
188 /* Pharos USB GPS - Microsoft version */
189 { USB_VENDOR_PROLIFIC
, USB_PRODUCT_PROLIFIC_PL2303X
},
190 /* Willcom WS002IN (DD) */
191 { USB_VENDOR_NETINDEX
, USB_PRODUCT_NETINDEX_WS002IN
},
192 /* COREGA CG-USBRS232R */
193 { USB_VENDOR_COREGA
, USB_PRODUCT_COREGA_CGUSBRS232R
},
195 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
197 int uplcom_match(device_t
, cfdata_t
, void *);
198 void uplcom_attach(device_t
, device_t
, void *);
199 void uplcom_childdet(device_t
, device_t
);
200 int uplcom_detach(device_t
, int);
201 int uplcom_activate(device_t
, enum devact
);
202 extern struct cfdriver uplcom_cd
;
203 CFATTACH_DECL2_NEW(uplcom
, sizeof(struct uplcom_softc
), uplcom_match
,
204 uplcom_attach
, uplcom_detach
, uplcom_activate
, NULL
, uplcom_childdet
);
208 USB_MATCH_START(uplcom
, uaa
);
210 return (uplcom_lookup(uaa
->vendor
, uaa
->product
) != NULL
?
211 UMATCH_VENDOR_PRODUCT
: UMATCH_NONE
);
216 USB_ATTACH_START(uplcom
, sc
, uaa
);
217 usbd_device_handle dev
= uaa
->device
;
218 usb_device_descriptor_t
*ddesc
;
219 usb_config_descriptor_t
*cdesc
;
220 usb_interface_descriptor_t
*id
;
221 usb_endpoint_descriptor_t
*ed
;
223 const char *devname
= device_xname(self
);
226 struct ucom_attach_args uca
;
233 devinfop
= usbd_devinfo_alloc(dev
, 0);
234 aprint_normal_dev(self
, "%s\n", devinfop
);
235 usbd_devinfo_free(devinfop
);
239 DPRINTF(("\n\nuplcom attach: sc=%p\n", sc
));
241 /* initialize endpoints */
242 uca
.bulkin
= uca
.bulkout
= -1;
243 sc
->sc_intr_number
= -1;
244 sc
->sc_intr_pipe
= NULL
;
246 /* Move the device into the configured state. */
247 err
= usbd_set_config_index(dev
, UPLCOM_CONFIG_INDEX
, 1);
249 aprint_error("\n%s: failed to set configuration, err=%s\n",
250 devname
, usbd_errstr(err
));
252 USB_ATTACH_ERROR_RETURN
;
255 /* determine chip type */
256 ddesc
= usbd_get_device_descriptor(dev
);
257 if (ddesc
->bDeviceClass
!= UDCLASS_COMM
&&
258 ddesc
->bMaxPacketSize
== 0x40)
259 sc
->sc_type
= UPLCOM_TYPE_HX
;
262 /* print the chip type */
263 if (sc
->sc_type
== UPLCOM_TYPE_HX
) {
264 DPRINTF(("uplcom_attach: chiptype HX\n"));
266 DPRINTF(("uplcom_attach: chiptype 0\n"));
270 /* Move the device into the configured state. */
271 err
= usbd_set_config_index(dev
, UPLCOM_CONFIG_INDEX
, 1);
273 aprint_error_dev(self
, "failed to set configuration: %s\n",
276 USB_ATTACH_ERROR_RETURN
;
279 /* get the config descriptor */
280 cdesc
= usbd_get_config_descriptor(sc
->sc_udev
);
283 aprint_error_dev(self
,
284 "failed to get configuration descriptor\n");
286 USB_ATTACH_ERROR_RETURN
;
289 /* get the (first/common) interface */
290 err
= usbd_device2interface_handle(dev
, UPLCOM_IFACE_INDEX
,
293 aprint_error("\n%s: failed to get interface, err=%s\n",
294 devname
, usbd_errstr(err
));
296 USB_ATTACH_ERROR_RETURN
;
299 /* Find the interrupt endpoints */
301 id
= usbd_get_interface_descriptor(sc
->sc_iface
);
302 sc
->sc_iface_number
= id
->bInterfaceNumber
;
304 for (i
= 0; i
< id
->bNumEndpoints
; i
++) {
305 ed
= usbd_interface2endpoint_descriptor(sc
->sc_iface
, i
);
307 aprint_error_dev(self
,
308 "no endpoint descriptor for %d\n", i
);
310 USB_ATTACH_ERROR_RETURN
;
313 if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
&&
314 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_INTERRUPT
) {
315 sc
->sc_intr_number
= ed
->bEndpointAddress
;
316 sc
->sc_isize
= UGETW(ed
->wMaxPacketSize
);
320 if (sc
->sc_intr_number
== -1) {
321 aprint_error_dev(self
, "Could not find interrupt in\n");
323 USB_ATTACH_ERROR_RETURN
;
326 /* keep interface for interrupt */
327 sc
->sc_intr_iface
= sc
->sc_iface
;
330 * USB-RSAQ1 has two interface
332 * USB-RSAQ1 | USB-RSAQ2
333 * -----------------+-----------------
334 * Interface 0 |Interface 0
335 * Interrupt(0x81) | Interrupt(0x81)
336 * -----------------+ BulkIN(0x02)
337 * Interface 1 | BulkOUT(0x83)
341 if (cdesc
->bNumInterface
== 2) {
342 err
= usbd_device2interface_handle(dev
,
343 UPLCOM_SECOND_IFACE_INDEX
, &sc
->sc_iface
);
345 aprint_error("\n%s: failed to get second interface, err=%s\n",
346 devname
, usbd_errstr(err
));
348 USB_ATTACH_ERROR_RETURN
;
352 /* Find the bulk{in,out} endpoints */
354 id
= usbd_get_interface_descriptor(sc
->sc_iface
);
355 sc
->sc_iface_number
= id
->bInterfaceNumber
;
357 for (i
= 0; i
< id
->bNumEndpoints
; i
++) {
358 ed
= usbd_interface2endpoint_descriptor(sc
->sc_iface
, i
);
360 aprint_error_dev(self
,
361 "no endpoint descriptor for %d\n", i
);
363 USB_ATTACH_ERROR_RETURN
;
366 if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
&&
367 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
) {
368 uca
.bulkin
= ed
->bEndpointAddress
;
369 } else if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_OUT
&&
370 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
) {
371 uca
.bulkout
= ed
->bEndpointAddress
;
375 if (uca
.bulkin
== -1) {
376 aprint_error_dev(self
, "Could not find data bulk in\n");
378 USB_ATTACH_ERROR_RETURN
;
381 if (uca
.bulkout
== -1) {
382 aprint_error_dev(self
, "Could not find data bulk out\n");
384 USB_ATTACH_ERROR_RETURN
;
387 sc
->sc_dtr
= sc
->sc_rts
= -1;
388 uca
.portno
= UCOM_UNK_PORTNO
;
389 /* bulkin, bulkout set above */
390 uca
.ibufsize
= UPLCOMIBUFSIZE
;
391 uca
.obufsize
= UPLCOMOBUFSIZE
;
392 uca
.ibufsizepad
= UPLCOMIBUFSIZE
;
395 uca
.iface
= sc
->sc_iface
;
396 uca
.methods
= &uplcom_methods
;
400 err
= uplcom_reset(sc
);
403 aprint_error_dev(self
, "reset failed, %s\n", usbd_errstr(err
));
405 USB_ATTACH_ERROR_RETURN
;
408 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH
, sc
->sc_udev
,
411 DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
412 uca
.bulkin
, uca
.bulkout
, sc
->sc_intr_number
));
413 sc
->sc_subdev
= config_found_sm_loc(self
, "ucombus", NULL
, &uca
,
414 ucomprint
, ucomsubmatch
);
416 USB_ATTACH_SUCCESS_RETURN
;
420 uplcom_childdet(device_t self
, device_t child
)
422 struct uplcom_softc
*sc
= device_private(self
);
424 KASSERT(sc
->sc_subdev
== child
);
425 sc
->sc_subdev
= NULL
;
430 USB_DETACH_START(uplcom
, sc
);
433 DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc
, flags
));
435 if (sc
->sc_intr_pipe
!= NULL
) {
436 usbd_abort_pipe(sc
->sc_intr_pipe
);
437 usbd_close_pipe(sc
->sc_intr_pipe
);
438 free(sc
->sc_intr_buf
, M_USBDEV
);
439 sc
->sc_intr_pipe
= NULL
;
443 if (sc
->sc_subdev
!= NULL
)
444 rv
= config_detach(sc
->sc_subdev
, flags
);
446 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH
, sc
->sc_udev
,
453 uplcom_activate(device_t self
, enum devact act
)
455 struct uplcom_softc
*sc
= device_private(self
);
458 case DVACT_DEACTIVATE
:
467 uplcom_reset(struct uplcom_softc
*sc
)
469 usb_device_request_t req
;
472 req
.bmRequestType
= UT_WRITE_VENDOR_DEVICE
;
473 req
.bRequest
= UPLCOM_SET_REQUEST
;
474 USETW(req
.wValue
, 0);
475 USETW(req
.wIndex
, sc
->sc_iface_number
);
476 USETW(req
.wLength
, 0);
478 err
= usbd_do_request(sc
->sc_udev
, &req
, 0);
485 struct pl2303x_init
{
493 static const struct pl2303x_init pl2303x
[] = {
494 { UT_READ_VENDOR_DEVICE
, UPLCOM_SET_REQUEST
, 0x8484, 0, 0 },
495 { UT_WRITE_VENDOR_DEVICE
, UPLCOM_SET_REQUEST
, 0x0404, 0, 0 },
496 { UT_READ_VENDOR_DEVICE
, UPLCOM_SET_REQUEST
, 0x8484, 0, 0 },
497 { UT_READ_VENDOR_DEVICE
, UPLCOM_SET_REQUEST
, 0x8383, 0, 0 },
498 { UT_READ_VENDOR_DEVICE
, UPLCOM_SET_REQUEST
, 0x8484, 0, 0 },
499 { UT_WRITE_VENDOR_DEVICE
, UPLCOM_SET_REQUEST
, 0x0404, 1, 0 },
500 { UT_READ_VENDOR_DEVICE
, UPLCOM_SET_REQUEST
, 0x8484, 0, 0 },
501 { UT_READ_VENDOR_DEVICE
, UPLCOM_SET_REQUEST
, 0x8383, 0, 0 },
502 { UT_WRITE_VENDOR_DEVICE
, UPLCOM_SET_REQUEST
, 0, 1, 0 },
503 { UT_WRITE_VENDOR_DEVICE
, UPLCOM_SET_REQUEST
, 1, 0, 0 },
504 { UT_WRITE_VENDOR_DEVICE
, UPLCOM_SET_REQUEST
, 2, 0x44, 0 }
506 #define N_PL2302X_INIT (sizeof(pl2303x)/sizeof(pl2303x[0]))
509 uplcom_pl2303x_init(struct uplcom_softc
*sc
)
511 usb_device_request_t req
;
515 for (i
= 0; i
< N_PL2302X_INIT
; i
++) {
516 req
.bmRequestType
= pl2303x
[i
].req_type
;
517 req
.bRequest
= pl2303x
[i
].request
;
518 USETW(req
.wValue
, pl2303x
[i
].value
);
519 USETW(req
.wIndex
, pl2303x
[i
].index
);
520 USETW(req
.wLength
, pl2303x
[i
].length
);
522 err
= usbd_do_request(sc
->sc_udev
, &req
, 0);
524 aprint_error_dev(sc
->sc_dev
,
525 "uplcom_pl2303x_init failed: %s\n",
535 uplcom_set_line_state(struct uplcom_softc
*sc
)
537 usb_device_request_t req
;
540 /* make sure we have initialized state for sc_dtr and sc_rts */
541 if (sc
->sc_dtr
== -1)
543 if (sc
->sc_rts
== -1)
546 ls
= (sc
->sc_dtr
? UCDC_LINE_DTR
: 0) |
547 (sc
->sc_rts
? UCDC_LINE_RTS
: 0);
549 req
.bmRequestType
= UT_WRITE_CLASS_INTERFACE
;
550 req
.bRequest
= UCDC_SET_CONTROL_LINE_STATE
;
551 USETW(req
.wValue
, ls
);
552 USETW(req
.wIndex
, sc
->sc_iface_number
);
553 USETW(req
.wLength
, 0);
555 (void)usbd_do_request(sc
->sc_udev
, &req
, 0);
559 uplcom_set(void *addr
, int portno
, int reg
, int onoff
)
561 struct uplcom_softc
*sc
= addr
;
565 uplcom_dtr(sc
, onoff
);
568 uplcom_rts(sc
, onoff
);
571 uplcom_break(sc
, onoff
);
579 uplcom_dtr(struct uplcom_softc
*sc
, int onoff
)
582 DPRINTF(("uplcom_dtr: onoff=%d\n", onoff
));
584 if (sc
->sc_dtr
!= -1 && !sc
->sc_dtr
== !onoff
)
587 sc
->sc_dtr
= !!onoff
;
589 uplcom_set_line_state(sc
);
593 uplcom_rts(struct uplcom_softc
*sc
, int onoff
)
595 DPRINTF(("uplcom_rts: onoff=%d\n", onoff
));
597 if (sc
->sc_rts
!= -1 && !sc
->sc_rts
== !onoff
)
600 sc
->sc_rts
= !!onoff
;
602 uplcom_set_line_state(sc
);
606 uplcom_break(struct uplcom_softc
*sc
, int onoff
)
608 usb_device_request_t req
;
610 DPRINTF(("uplcom_break: onoff=%d\n", onoff
));
612 req
.bmRequestType
= UT_WRITE_CLASS_INTERFACE
;
613 req
.bRequest
= UCDC_SEND_BREAK
;
614 USETW(req
.wValue
, onoff
? UCDC_BREAK_ON
: UCDC_BREAK_OFF
);
615 USETW(req
.wIndex
, sc
->sc_iface_number
);
616 USETW(req
.wLength
, 0);
618 (void)usbd_do_request(sc
->sc_udev
, &req
, 0);
622 uplcom_set_crtscts(struct uplcom_softc
*sc
)
624 usb_device_request_t req
;
627 DPRINTF(("uplcom_set_crtscts: on\n"));
629 req
.bmRequestType
= UT_WRITE_VENDOR_DEVICE
;
630 req
.bRequest
= UPLCOM_SET_REQUEST
;
631 USETW(req
.wValue
, 0);
632 if (sc
->sc_type
== UPLCOM_TYPE_HX
)
633 USETW(req
.wIndex
, UPLCOM_SET_CRTSCTS_HX
);
635 USETW(req
.wIndex
, UPLCOM_SET_CRTSCTS_0
);
636 USETW(req
.wLength
, 0);
638 err
= usbd_do_request(sc
->sc_udev
, &req
, 0);
640 DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
645 return (USBD_NORMAL_COMPLETION
);
649 uplcom_set_line_coding(struct uplcom_softc
*sc
, usb_cdc_line_state_t
*state
)
651 usb_device_request_t req
;
654 DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
655 UGETDW(state
->dwDTERate
), state
->bCharFormat
,
656 state
->bParityType
, state
->bDataBits
));
658 if (memcmp(state
, &sc
->sc_line_state
, UCDC_LINE_STATE_LENGTH
) == 0) {
659 DPRINTF(("uplcom_set_line_coding: already set\n"));
660 return (USBD_NORMAL_COMPLETION
);
663 req
.bmRequestType
= UT_WRITE_CLASS_INTERFACE
;
664 req
.bRequest
= UCDC_SET_LINE_CODING
;
665 USETW(req
.wValue
, 0);
666 USETW(req
.wIndex
, sc
->sc_iface_number
);
667 USETW(req
.wLength
, UCDC_LINE_STATE_LENGTH
);
669 err
= usbd_do_request(sc
->sc_udev
, &req
, state
);
671 DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
676 sc
->sc_line_state
= *state
;
678 return (USBD_NORMAL_COMPLETION
);
682 uplcom_param(void *addr
, int portno
, struct termios
*t
)
684 struct uplcom_softc
*sc
= addr
;
686 usb_cdc_line_state_t ls
;
688 DPRINTF(("uplcom_param: sc=%p\n", sc
));
690 USETDW(ls
.dwDTERate
, t
->c_ospeed
);
691 if (ISSET(t
->c_cflag
, CSTOPB
))
692 ls
.bCharFormat
= UCDC_STOP_BIT_2
;
694 ls
.bCharFormat
= UCDC_STOP_BIT_1
;
695 if (ISSET(t
->c_cflag
, PARENB
)) {
696 if (ISSET(t
->c_cflag
, PARODD
))
697 ls
.bParityType
= UCDC_PARITY_ODD
;
699 ls
.bParityType
= UCDC_PARITY_EVEN
;
701 ls
.bParityType
= UCDC_PARITY_NONE
;
702 switch (ISSET(t
->c_cflag
, CSIZE
)) {
717 err
= uplcom_set_line_coding(sc
, &ls
);
719 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err
)));
723 if (ISSET(t
->c_cflag
, CRTSCTS
))
724 uplcom_set_crtscts(sc
);
726 if (sc
->sc_rts
== -1 || sc
->sc_dtr
== -1)
727 uplcom_set_line_state(sc
);
730 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err
)));
738 uplcom_vendor_control_write(usbd_device_handle dev
, u_int16_t value
, u_int16_t index
)
740 usb_device_request_t req
;
743 req
.bmRequestType
= UT_WRITE_VENDOR_DEVICE
;
744 req
.bRequest
= UPLCOM_SET_REQUEST
;
745 USETW(req
.wValue
, value
);
746 USETW(req
.wIndex
, index
);
747 USETW(req
.wLength
, 0);
749 err
= usbd_do_request(dev
, &req
, NULL
);
752 DPRINTF(("uplcom_open: vendor write failed, err=%s (%d)\n",
753 usbd_errstr(err
), err
));
760 uplcom_open(void *addr
, int portno
)
762 struct uplcom_softc
*sc
= addr
;
768 DPRINTF(("uplcom_open: sc=%p\n", sc
));
770 /* Some unknown device frobbing. */
771 if (sc
->sc_type
== UPLCOM_TYPE_HX
)
772 uplcom_vendor_control_write(sc
->sc_udev
, 2, 0x44);
774 uplcom_vendor_control_write(sc
->sc_udev
, 2, 0x24);
776 if (sc
->sc_intr_number
!= -1 && sc
->sc_intr_pipe
== NULL
) {
777 sc
->sc_intr_buf
= malloc(sc
->sc_isize
, M_USBDEV
, M_WAITOK
);
778 err
= usbd_open_pipe_intr(sc
->sc_intr_iface
, sc
->sc_intr_number
,
779 USBD_SHORT_XFER_OK
, &sc
->sc_intr_pipe
, sc
,
780 sc
->sc_intr_buf
, sc
->sc_isize
,
781 uplcom_intr
, USBD_DEFAULT_INTERVAL
);
783 DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
784 USBDEVNAME(sc
->sc_dev
), sc
->sc_intr_number
));
789 if (sc
->sc_type
== UPLCOM_TYPE_HX
)
790 return (uplcom_pl2303x_init(sc
));
796 uplcom_close(void *addr
, int portno
)
798 struct uplcom_softc
*sc
= addr
;
804 DPRINTF(("uplcom_close: close\n"));
806 if (sc
->sc_intr_pipe
!= NULL
) {
807 err
= usbd_abort_pipe(sc
->sc_intr_pipe
);
809 printf("%s: abort interrupt pipe failed: %s\n",
810 USBDEVNAME(sc
->sc_dev
), usbd_errstr(err
));
811 err
= usbd_close_pipe(sc
->sc_intr_pipe
);
813 printf("%s: close interrupt pipe failed: %s\n",
814 USBDEVNAME(sc
->sc_dev
), usbd_errstr(err
));
815 free(sc
->sc_intr_buf
, M_USBDEV
);
816 sc
->sc_intr_pipe
= NULL
;
821 uplcom_intr(usbd_xfer_handle xfer
, usbd_private_handle priv
,
824 struct uplcom_softc
*sc
= priv
;
825 u_char
*buf
= sc
->sc_intr_buf
;
831 if (status
!= USBD_NORMAL_COMPLETION
) {
832 if (status
== USBD_NOT_STARTED
|| status
== USBD_CANCELLED
)
835 DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc
->sc_dev
),
836 usbd_errstr(status
)));
837 usbd_clear_endpoint_stall_async(sc
->sc_intr_pipe
);
841 DPRINTF(("%s: uplcom status = %02x\n", USBDEVNAME(sc
->sc_dev
), buf
[8]));
843 sc
->sc_lsr
= sc
->sc_msr
= 0;
845 if (ISSET(pstatus
, UPLCOM_N_SERIAL_CTS
))
846 sc
->sc_msr
|= UMSR_CTS
;
847 if (ISSET(pstatus
, UCDC_N_SERIAL_RI
))
848 sc
->sc_msr
|= UMSR_RI
;
849 if (ISSET(pstatus
, UCDC_N_SERIAL_DSR
))
850 sc
->sc_msr
|= UMSR_DSR
;
851 if (ISSET(pstatus
, UCDC_N_SERIAL_DCD
))
852 sc
->sc_msr
|= UMSR_DCD
;
853 ucom_status_change(device_private(sc
->sc_subdev
));
857 uplcom_get_status(void *addr
, int portno
, u_char
*lsr
, u_char
*msr
)
859 struct uplcom_softc
*sc
= addr
;
861 DPRINTF(("uplcom_get_status:\n"));
871 uplcom_ioctl(void *addr
, int portno
, u_long cmd
, void *data
, int flag
,
874 struct uplcom_softc
*sc
= addr
;
880 DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd
));
886 case USB_GET_CM_OVER_DATA
:
887 case USB_SET_CM_OVER_DATA
:
891 DPRINTF(("uplcom_ioctl: unknown\n"));