1 /* $NetBSD: ubsa_common.c,v 1.3 2008/04/28 20:23:59 martin Exp $ */
3 * Copyright (c) 2002, Alexander Kabaev <kan.FreeBSD.org>.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Copyright (c) 2001 The NetBSD Foundation, Inc.
29 * All rights reserved.
31 * This code is derived from software contributed to The NetBSD Foundation
32 * by Ichiro FUKUHARA (ichiro@ichiro.org).
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
43 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
44 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
45 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
46 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
47 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
49 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
51 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
53 * POSSIBILITY OF SUCH DAMAGE.
56 #include <sys/cdefs.h>
57 __KERNEL_RCSID(0, "$NetBSD: ubsa_common.c,v 1.3 2008/04/28 20:23:59 martin Exp $");
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/malloc.h>
66 #include <sys/ioccom.h>
67 #include <sys/fcntl.h>
71 #if __FreeBSD_version >= 500014
72 #include <sys/selinfo.h>
74 #include <sys/select.h>
77 #include <sys/device.h>
79 #include <sys/sysctl.h>
82 #include <dev/usb/usb.h>
83 #include <dev/usb/usbdi.h>
84 #include <dev/usb/usbdi_util.h>
85 #include <dev/usb/usbdivar.h>
87 #include <dev/usb/usbcdc.h>
88 #include <dev/usb/usbdevs.h>
89 #include <dev/usb/usb_quirks.h>
90 #include <dev/usb/ucomvar.h>
91 #include <dev/usb/ubsavar.h>
95 #define DPRINTFN(n, x) do { \
96 if (ubsadebug > (n)) \
100 #define DPRINTFN(n, x)
102 #define DPRINTF(x) DPRINTFN(0, x)
105 ubsa_request(struct ubsa_softc
*sc
, int portno
, u_int8_t request
, u_int16_t value
)
107 usb_device_request_t req
;
111 req
.bmRequestType
= UT_WRITE_CLASS_INTERFACE
;
113 req
.bmRequestType
= UT_WRITE_VENDOR_DEVICE
;
115 if (portno
> UBSA_MAXCONN
) {
116 printf("%s: ubsa_request: invalid port(%d)#\n",
117 USBDEVNAME(sc
->sc_dev
), portno
);
121 req
.bRequest
= request
;
122 USETW(req
.wValue
, value
);
123 USETW(req
.wIndex
, sc
->sc_iface_number
[portno
]);
124 USETW(req
.wLength
, 0);
126 err
= usbd_do_request(sc
->sc_udev
, &req
, 0);
128 printf("%s: ubsa_request: %s\n",
129 USBDEVNAME(sc
->sc_dev
), usbd_errstr(err
));
134 ubsa_dtr(struct ubsa_softc
*sc
, int portno
, int onoff
)
137 DPRINTF(("ubsa_dtr: onoff = %d\n", onoff
));
139 if (sc
->sc_dtr
== onoff
)
143 ubsa_request(sc
, portno
, UBSA_SET_DTR
, onoff
? 1 : 0);
147 ubsa_rts(struct ubsa_softc
*sc
, int portno
, int onoff
)
150 DPRINTF(("ubsa_rts: onoff = %d\n", onoff
));
152 if (sc
->sc_rts
== onoff
)
156 ubsa_request(sc
, portno
, UBSA_SET_RTS
, onoff
? 1 : 0);
160 ubsa_quadumts_dtr(struct ubsa_softc
*sc
, int portno
, int onoff
)
163 DPRINTF(("ubsa_dtr: onoff = %d\n", onoff
));
165 if (sc
->sc_dtr
== onoff
)
169 ubsa_request(sc
, portno
, UBSA_QUADUMTS_SET_PIN
,
170 (sc
->sc_rts
? 2 : 0)+(sc
->sc_dtr
? 1 : 0));
174 ubsa_quadumts_rts(struct ubsa_softc
*sc
, int portno
, int onoff
)
177 DPRINTF(("ubsa_rts: onoff = %d\n", onoff
));
179 if (sc
->sc_rts
== onoff
)
183 ubsa_request(sc
, portno
, UBSA_QUADUMTS_SET_PIN
,
184 (sc
->sc_rts
? 2 : 0)+(sc
->sc_dtr
? 1 : 0));
188 ubsa_break(struct ubsa_softc
*sc
, int portno
, int onoff
)
190 DPRINTF(("ubsa_rts: onoff = %d\n", onoff
));
192 ubsa_request(sc
, portno
, UBSA_SET_BREAK
, onoff
? 1 : 0);
196 ubsa_set(void *addr
, int portno
, int reg
, int onoff
)
198 struct ubsa_softc
*sc
;
204 ubsa_quadumts_dtr(sc
, portno
, onoff
);
206 ubsa_dtr(sc
, portno
, onoff
);
210 ubsa_quadumts_rts(sc
, portno
, onoff
);
212 ubsa_rts(sc
, portno
, onoff
);
215 if (!sc
->sc_quadumts
)
216 ubsa_break(sc
, portno
, onoff
);
224 ubsa_baudrate(struct ubsa_softc
*sc
, int portno
, speed_t speed
)
228 DPRINTF(("ubsa_baudrate: speed = %d\n", speed
));
244 value
= B230400
/ speed
;
247 printf("%s: ubsa_param: unsupported baudrate, "
248 "forcing default of 9600\n",
249 USBDEVNAME(sc
->sc_dev
));
250 value
= B230400
/ B9600
;
255 ubsa_flow(sc
, portno
, 0, 0);
256 ubsa_dtr(sc
, portno
, 0);
257 ubsa_rts(sc
, portno
, 0);
259 ubsa_request(sc
, portno
, UBSA_SET_BAUDRATE
, value
);
263 ubsa_parity(struct ubsa_softc
*sc
, int portno
, tcflag_t cflag
)
267 DPRINTF(("ubsa_parity: cflag = 0x%x\n", cflag
));
270 value
= (cflag
& PARODD
) ? UBSA_PARITY_ODD
: UBSA_PARITY_EVEN
;
272 value
= UBSA_PARITY_NONE
;
274 ubsa_request(sc
, portno
, UBSA_SET_PARITY
, value
);
278 ubsa_databits(struct ubsa_softc
*sc
, int portno
, tcflag_t cflag
)
282 DPRINTF(("ubsa_databits: cflag = 0x%x\n", cflag
));
284 switch (cflag
& CSIZE
) {
285 case CS5
: value
= 0; break;
286 case CS6
: value
= 1; break;
287 case CS7
: value
= 2; break;
288 case CS8
: value
= 3; break;
290 printf("%s: ubsa_param: unsupported databits requested, "
291 "forcing default of 8\n",
292 USBDEVNAME(sc
->sc_dev
));
296 ubsa_request(sc
, portno
, UBSA_SET_DATA_BITS
, value
);
300 ubsa_stopbits(struct ubsa_softc
*sc
, int portno
, tcflag_t cflag
)
304 DPRINTF(("ubsa_stopbits: cflag = 0x%x\n", cflag
));
306 value
= (cflag
& CSTOPB
) ? 1 : 0;
308 ubsa_request(sc
, portno
, UBSA_SET_STOP_BITS
, value
);
312 ubsa_flow(struct ubsa_softc
*sc
, int portno
, tcflag_t cflag
, tcflag_t iflag
)
316 DPRINTF(("ubsa_flow: cflag = 0x%x, iflag = 0x%x\n", cflag
, iflag
));
320 value
|= UBSA_FLOW_OCTS
| UBSA_FLOW_IRTS
;
321 if (iflag
& (IXON
|IXOFF
))
322 value
|= UBSA_FLOW_OXON
| UBSA_FLOW_IXON
;
324 ubsa_request(sc
, portno
, UBSA_SET_FLOW_CTRL
, value
);
328 ubsa_param(void *addr
, int portno
, struct termios
*ti
)
330 struct ubsa_softc
*sc
= addr
;
332 DPRINTF(("ubsa_param: sc = %p\n", sc
));
334 if (!sc
->sc_quadumts
) {
335 ubsa_baudrate(sc
, portno
, ti
->c_ospeed
);
336 ubsa_parity(sc
, portno
, ti
->c_cflag
);
337 ubsa_databits(sc
, portno
, ti
->c_cflag
);
338 ubsa_stopbits(sc
, portno
, ti
->c_cflag
);
339 ubsa_flow(sc
, portno
, ti
->c_cflag
, ti
->c_iflag
);
346 ubsa_open(void *addr
, int portno
)
348 struct ubsa_softc
*sc
= addr
;
354 if (sc
->sc_intr_number
!= -1 && sc
->sc_intr_pipe
== NULL
) {
355 sc
->sc_intr_buf
= malloc(sc
->sc_isize
, M_USBDEV
, M_WAITOK
);
356 /* XXX only iface# = 0 has intr line */
357 /* XXX E220 specific? need to check */
358 err
= usbd_open_pipe_intr(sc
->sc_iface
[0],
368 printf("%s: cannot open interrupt pipe (addr %d)\n",
369 USBDEVNAME(sc
->sc_dev
),
379 ubsa_close(void *addr
, int portno
)
381 struct ubsa_softc
*sc
= addr
;
387 DPRINTF(("ubsa_close: close\n"));
389 if (sc
->sc_intr_pipe
!= NULL
) {
390 err
= usbd_abort_pipe(sc
->sc_intr_pipe
);
392 printf("%s: abort interrupt pipe failed: %s\n",
393 USBDEVNAME(sc
->sc_dev
),
395 err
= usbd_close_pipe(sc
->sc_intr_pipe
);
397 printf("%s: close interrupt pipe failed: %s\n",
398 USBDEVNAME(sc
->sc_dev
),
400 free(sc
->sc_intr_buf
, M_USBDEV
);
401 sc
->sc_intr_pipe
= NULL
;
406 ubsa_intr(usbd_xfer_handle xfer
, usbd_private_handle priv
,
409 struct ubsa_softc
*sc
= priv
;
413 buf
= sc
->sc_intr_buf
;
417 if (status
!= USBD_NORMAL_COMPLETION
) {
418 if (status
== USBD_NOT_STARTED
|| status
== USBD_CANCELLED
)
421 DPRINTF(("%s: ubsa_intr: abnormal status: %s\n",
422 USBDEVNAME(sc
->sc_dev
), usbd_errstr(status
)));
423 usbd_clear_endpoint_stall_async(sc
->sc_intr_pipe
);
427 /* incidentally, Belkin adapter status bits match UART 16550 bits */
431 DPRINTF(("%s: ubsa lsr = 0x%02x, msr = 0x%02x\n",
432 USBDEVNAME(sc
->sc_dev
), sc
->sc_lsr
, sc
->sc_msr
));
434 for (i
= 0; i
< sc
->sc_numif
; i
++) {
435 ucom_status_change(device_private(sc
->sc_subdevs
[i
]));
440 ubsa_get_status(void *addr
, int portno
, u_char
*lsr
, u_char
*msr
)
442 struct ubsa_softc
*sc
= addr
;
444 DPRINTF(("ubsa_get_status\n"));