Deleted Added
full compact
umoscom.c (207077) umoscom.c (212122)
1/* $FreeBSD: head/sys/dev/usb/serial/umoscom.c 207077 2010-04-22 21:31:34Z thompsa $ */
1/* $FreeBSD: head/sys/dev/usb/serial/umoscom.c 212122 2010-09-01 23:47:53Z thompsa $ */
2/* $OpenBSD: umoscom.c,v 1.2 2006/10/26 06:02:43 jsg Exp $ */
3
4/*
5 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include <sys/stdint.h>
21#include <sys/stddef.h>
22#include <sys/param.h>
23#include <sys/queue.h>
24#include <sys/types.h>
25#include <sys/systm.h>
26#include <sys/kernel.h>
27#include <sys/bus.h>
28#include <sys/linker_set.h>
29#include <sys/module.h>
30#include <sys/lock.h>
31#include <sys/mutex.h>
32#include <sys/condvar.h>
33#include <sys/sysctl.h>
34#include <sys/sx.h>
35#include <sys/unistd.h>
36#include <sys/callout.h>
37#include <sys/malloc.h>
38#include <sys/priv.h>
39
40#include <dev/usb/usb.h>
41#include <dev/usb/usbdi.h>
42#include <dev/usb/usbdi_util.h>
43#include "usbdevs.h"
44
45#define USB_DEBUG_VAR umoscom_debug
46#include <dev/usb/usb_debug.h>
47#include <dev/usb/usb_process.h>
48
49#include <dev/usb/serial/usb_serial.h>
50
51#ifdef USB_DEBUG
52static int umoscom_debug = 0;
53
54SYSCTL_NODE(_hw_usb, OID_AUTO, umoscom, CTLFLAG_RW, 0, "USB umoscom");
55SYSCTL_INT(_hw_usb_umoscom, OID_AUTO, debug, CTLFLAG_RW,
56 &umoscom_debug, 0, "Debug level");
57#endif
58
59#define UMOSCOM_BUFSIZE 1024 /* bytes */
60
61#define UMOSCOM_CONFIG_INDEX 0
62#define UMOSCOM_IFACE_INDEX 0
63
64/* interrupt packet */
65#define UMOSCOM_IIR_RLS 0x06
66#define UMOSCOM_IIR_RDA 0x04
67#define UMOSCOM_IIR_CTI 0x0c
68#define UMOSCOM_IIR_THR 0x02
69#define UMOSCOM_IIR_MS 0x00
70
71/* registers */
72#define UMOSCOM_READ 0x0d
73#define UMOSCOM_WRITE 0x0e
74#define UMOSCOM_UART_REG 0x0300
75#define UMOSCOM_VEND_REG 0x0000
76
77#define UMOSCOM_TXBUF 0x00 /* Write */
78#define UMOSCOM_RXBUF 0x00 /* Read */
79#define UMOSCOM_INT 0x01
80#define UMOSCOM_FIFO 0x02 /* Write */
81#define UMOSCOM_ISR 0x02 /* Read */
82#define UMOSCOM_LCR 0x03
83#define UMOSCOM_MCR 0x04
84#define UMOSCOM_LSR 0x05
85#define UMOSCOM_MSR 0x06
86#define UMOSCOM_SCRATCH 0x07
87#define UMOSCOM_DIV_LO 0x08
88#define UMOSCOM_DIV_HI 0x09
89#define UMOSCOM_EFR 0x0a
90#define UMOSCOM_XON1 0x0b
91#define UMOSCOM_XON2 0x0c
92#define UMOSCOM_XOFF1 0x0d
93#define UMOSCOM_XOFF2 0x0e
94
95#define UMOSCOM_BAUDLO 0x00
96#define UMOSCOM_BAUDHI 0x01
97
98#define UMOSCOM_INT_RXEN 0x01
99#define UMOSCOM_INT_TXEN 0x02
100#define UMOSCOM_INT_RSEN 0x04
101#define UMOSCOM_INT_MDMEM 0x08
102#define UMOSCOM_INT_SLEEP 0x10
103#define UMOSCOM_INT_XOFF 0x20
104#define UMOSCOM_INT_RTS 0x40
105
106#define UMOSCOM_FIFO_EN 0x01
107#define UMOSCOM_FIFO_RXCLR 0x02
108#define UMOSCOM_FIFO_TXCLR 0x04
109#define UMOSCOM_FIFO_DMA_BLK 0x08
110#define UMOSCOM_FIFO_TXLVL_MASK 0x30
111#define UMOSCOM_FIFO_TXLVL_8 0x00
112#define UMOSCOM_FIFO_TXLVL_16 0x10
113#define UMOSCOM_FIFO_TXLVL_32 0x20
114#define UMOSCOM_FIFO_TXLVL_56 0x30
115#define UMOSCOM_FIFO_RXLVL_MASK 0xc0
116#define UMOSCOM_FIFO_RXLVL_8 0x00
117#define UMOSCOM_FIFO_RXLVL_16 0x40
118#define UMOSCOM_FIFO_RXLVL_56 0x80
119#define UMOSCOM_FIFO_RXLVL_80 0xc0
120
121#define UMOSCOM_ISR_MDM 0x00
122#define UMOSCOM_ISR_NONE 0x01
123#define UMOSCOM_ISR_TX 0x02
124#define UMOSCOM_ISR_RX 0x04
125#define UMOSCOM_ISR_LINE 0x06
126#define UMOSCOM_ISR_RXTIMEOUT 0x0c
127#define UMOSCOM_ISR_RX_XOFF 0x10
128#define UMOSCOM_ISR_RTSCTS 0x20
129#define UMOSCOM_ISR_FIFOEN 0xc0
130
131#define UMOSCOM_LCR_DBITS(x) ((x) - 5)
132#define UMOSCOM_LCR_STOP_BITS_1 0x00
133#define UMOSCOM_LCR_STOP_BITS_2 0x04 /* 2 if 6-8 bits/char or 1.5 if 5 */
134#define UMOSCOM_LCR_PARITY_NONE 0x00
135#define UMOSCOM_LCR_PARITY_ODD 0x08
136#define UMOSCOM_LCR_PARITY_EVEN 0x18
137#define UMOSCOM_LCR_BREAK 0x40
138#define UMOSCOM_LCR_DIVLATCH_EN 0x80
139
140#define UMOSCOM_MCR_DTR 0x01
141#define UMOSCOM_MCR_RTS 0x02
142#define UMOSCOM_MCR_LOOP 0x04
143#define UMOSCOM_MCR_INTEN 0x08
144#define UMOSCOM_MCR_LOOPBACK 0x10
145#define UMOSCOM_MCR_XONANY 0x20
146#define UMOSCOM_MCR_IRDA_EN 0x40
147#define UMOSCOM_MCR_BAUD_DIV4 0x80
148
149#define UMOSCOM_LSR_RXDATA 0x01
150#define UMOSCOM_LSR_RXOVER 0x02
151#define UMOSCOM_LSR_RXPAR_ERR 0x04
152#define UMOSCOM_LSR_RXFRM_ERR 0x08
153#define UMOSCOM_LSR_RXBREAK 0x10
154#define UMOSCOM_LSR_TXEMPTY 0x20
155#define UMOSCOM_LSR_TXALLEMPTY 0x40
156#define UMOSCOM_LSR_TXFIFO_ERR 0x80
157
158#define UMOSCOM_MSR_CTS_CHG 0x01
159#define UMOSCOM_MSR_DSR_CHG 0x02
160#define UMOSCOM_MSR_RI_CHG 0x04
161#define UMOSCOM_MSR_CD_CHG 0x08
162#define UMOSCOM_MSR_CTS 0x10
163#define UMOSCOM_MSR_RTS 0x20
164#define UMOSCOM_MSR_RI 0x40
165#define UMOSCOM_MSR_CD 0x80
166
167#define UMOSCOM_BAUD_REF 115200
168
169enum {
170 UMOSCOM_BULK_DT_WR,
171 UMOSCOM_BULK_DT_RD,
172 UMOSCOM_INTR_DT_RD,
173 UMOSCOM_N_TRANSFER,
174};
175
176struct umoscom_softc {
177 struct ucom_super_softc sc_super_ucom;
178 struct ucom_softc sc_ucom;
179
180 struct usb_xfer *sc_xfer[UMOSCOM_N_TRANSFER];
181 struct usb_device *sc_udev;
182 struct mtx sc_mtx;
183
184 uint8_t sc_mcr;
185 uint8_t sc_lcr;
186};
187
188/* prototypes */
189
190static device_probe_t umoscom_probe;
191static device_attach_t umoscom_attach;
192static device_detach_t umoscom_detach;
193
194static usb_callback_t umoscom_write_callback;
195static usb_callback_t umoscom_read_callback;
196static usb_callback_t umoscom_intr_callback;
197
198static void umoscom_cfg_open(struct ucom_softc *);
199static void umoscom_cfg_close(struct ucom_softc *);
200static void umoscom_cfg_set_break(struct ucom_softc *, uint8_t);
201static void umoscom_cfg_set_dtr(struct ucom_softc *, uint8_t);
202static void umoscom_cfg_set_rts(struct ucom_softc *, uint8_t);
203static int umoscom_pre_param(struct ucom_softc *, struct termios *);
204static void umoscom_cfg_param(struct ucom_softc *, struct termios *);
205static void umoscom_cfg_get_status(struct ucom_softc *, uint8_t *,
206 uint8_t *);
207static void umoscom_cfg_write(struct umoscom_softc *, uint16_t, uint16_t);
208static uint8_t umoscom_cfg_read(struct umoscom_softc *, uint16_t);
209static void umoscom_start_read(struct ucom_softc *);
210static void umoscom_stop_read(struct ucom_softc *);
211static void umoscom_start_write(struct ucom_softc *);
212static void umoscom_stop_write(struct ucom_softc *);
213static void umoscom_poll(struct ucom_softc *ucom);
214
215static const struct usb_config umoscom_config_data[UMOSCOM_N_TRANSFER] = {
216
217 [UMOSCOM_BULK_DT_WR] = {
218 .type = UE_BULK,
219 .endpoint = UE_ADDR_ANY,
220 .direction = UE_DIR_OUT,
221 .bufsize = UMOSCOM_BUFSIZE,
222 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
223 .callback = &umoscom_write_callback,
224 },
225
226 [UMOSCOM_BULK_DT_RD] = {
227 .type = UE_BULK,
228 .endpoint = UE_ADDR_ANY,
229 .direction = UE_DIR_IN,
230 .bufsize = UMOSCOM_BUFSIZE,
231 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
232 .callback = &umoscom_read_callback,
233 },
234
235 [UMOSCOM_INTR_DT_RD] = {
236 .type = UE_INTERRUPT,
237 .endpoint = UE_ADDR_ANY,
238 .direction = UE_DIR_IN,
239 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
240 .bufsize = 0, /* use wMaxPacketSize */
241 .callback = &umoscom_intr_callback,
242 },
243};
244
245static const struct ucom_callback umoscom_callback = {
246 /* configuration callbacks */
247 .ucom_cfg_get_status = &umoscom_cfg_get_status,
248 .ucom_cfg_set_dtr = &umoscom_cfg_set_dtr,
249 .ucom_cfg_set_rts = &umoscom_cfg_set_rts,
250 .ucom_cfg_set_break = &umoscom_cfg_set_break,
251 .ucom_cfg_param = &umoscom_cfg_param,
252 .ucom_cfg_open = &umoscom_cfg_open,
253 .ucom_cfg_close = &umoscom_cfg_close,
254
255 /* other callbacks */
256 .ucom_pre_param = &umoscom_pre_param,
257 .ucom_start_read = &umoscom_start_read,
258 .ucom_stop_read = &umoscom_stop_read,
259 .ucom_start_write = &umoscom_start_write,
260 .ucom_stop_write = &umoscom_stop_write,
261 .ucom_poll = &umoscom_poll,
262};
263
264static device_method_t umoscom_methods[] = {
265 DEVMETHOD(device_probe, umoscom_probe),
266 DEVMETHOD(device_attach, umoscom_attach),
267 DEVMETHOD(device_detach, umoscom_detach),
268 {0, 0}
269};
270
271static devclass_t umoscom_devclass;
272
273static driver_t umoscom_driver = {
274 .name = "umoscom",
275 .methods = umoscom_methods,
276 .size = sizeof(struct umoscom_softc),
277};
278
279DRIVER_MODULE(umoscom, uhub, umoscom_driver, umoscom_devclass, NULL, 0);
280MODULE_DEPEND(umoscom, ucom, 1, 1, 1);
281MODULE_DEPEND(umoscom, usb, 1, 1, 1);
2/* $OpenBSD: umoscom.c,v 1.2 2006/10/26 06:02:43 jsg Exp $ */
3
4/*
5 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include <sys/stdint.h>
21#include <sys/stddef.h>
22#include <sys/param.h>
23#include <sys/queue.h>
24#include <sys/types.h>
25#include <sys/systm.h>
26#include <sys/kernel.h>
27#include <sys/bus.h>
28#include <sys/linker_set.h>
29#include <sys/module.h>
30#include <sys/lock.h>
31#include <sys/mutex.h>
32#include <sys/condvar.h>
33#include <sys/sysctl.h>
34#include <sys/sx.h>
35#include <sys/unistd.h>
36#include <sys/callout.h>
37#include <sys/malloc.h>
38#include <sys/priv.h>
39
40#include <dev/usb/usb.h>
41#include <dev/usb/usbdi.h>
42#include <dev/usb/usbdi_util.h>
43#include "usbdevs.h"
44
45#define USB_DEBUG_VAR umoscom_debug
46#include <dev/usb/usb_debug.h>
47#include <dev/usb/usb_process.h>
48
49#include <dev/usb/serial/usb_serial.h>
50
51#ifdef USB_DEBUG
52static int umoscom_debug = 0;
53
54SYSCTL_NODE(_hw_usb, OID_AUTO, umoscom, CTLFLAG_RW, 0, "USB umoscom");
55SYSCTL_INT(_hw_usb_umoscom, OID_AUTO, debug, CTLFLAG_RW,
56 &umoscom_debug, 0, "Debug level");
57#endif
58
59#define UMOSCOM_BUFSIZE 1024 /* bytes */
60
61#define UMOSCOM_CONFIG_INDEX 0
62#define UMOSCOM_IFACE_INDEX 0
63
64/* interrupt packet */
65#define UMOSCOM_IIR_RLS 0x06
66#define UMOSCOM_IIR_RDA 0x04
67#define UMOSCOM_IIR_CTI 0x0c
68#define UMOSCOM_IIR_THR 0x02
69#define UMOSCOM_IIR_MS 0x00
70
71/* registers */
72#define UMOSCOM_READ 0x0d
73#define UMOSCOM_WRITE 0x0e
74#define UMOSCOM_UART_REG 0x0300
75#define UMOSCOM_VEND_REG 0x0000
76
77#define UMOSCOM_TXBUF 0x00 /* Write */
78#define UMOSCOM_RXBUF 0x00 /* Read */
79#define UMOSCOM_INT 0x01
80#define UMOSCOM_FIFO 0x02 /* Write */
81#define UMOSCOM_ISR 0x02 /* Read */
82#define UMOSCOM_LCR 0x03
83#define UMOSCOM_MCR 0x04
84#define UMOSCOM_LSR 0x05
85#define UMOSCOM_MSR 0x06
86#define UMOSCOM_SCRATCH 0x07
87#define UMOSCOM_DIV_LO 0x08
88#define UMOSCOM_DIV_HI 0x09
89#define UMOSCOM_EFR 0x0a
90#define UMOSCOM_XON1 0x0b
91#define UMOSCOM_XON2 0x0c
92#define UMOSCOM_XOFF1 0x0d
93#define UMOSCOM_XOFF2 0x0e
94
95#define UMOSCOM_BAUDLO 0x00
96#define UMOSCOM_BAUDHI 0x01
97
98#define UMOSCOM_INT_RXEN 0x01
99#define UMOSCOM_INT_TXEN 0x02
100#define UMOSCOM_INT_RSEN 0x04
101#define UMOSCOM_INT_MDMEM 0x08
102#define UMOSCOM_INT_SLEEP 0x10
103#define UMOSCOM_INT_XOFF 0x20
104#define UMOSCOM_INT_RTS 0x40
105
106#define UMOSCOM_FIFO_EN 0x01
107#define UMOSCOM_FIFO_RXCLR 0x02
108#define UMOSCOM_FIFO_TXCLR 0x04
109#define UMOSCOM_FIFO_DMA_BLK 0x08
110#define UMOSCOM_FIFO_TXLVL_MASK 0x30
111#define UMOSCOM_FIFO_TXLVL_8 0x00
112#define UMOSCOM_FIFO_TXLVL_16 0x10
113#define UMOSCOM_FIFO_TXLVL_32 0x20
114#define UMOSCOM_FIFO_TXLVL_56 0x30
115#define UMOSCOM_FIFO_RXLVL_MASK 0xc0
116#define UMOSCOM_FIFO_RXLVL_8 0x00
117#define UMOSCOM_FIFO_RXLVL_16 0x40
118#define UMOSCOM_FIFO_RXLVL_56 0x80
119#define UMOSCOM_FIFO_RXLVL_80 0xc0
120
121#define UMOSCOM_ISR_MDM 0x00
122#define UMOSCOM_ISR_NONE 0x01
123#define UMOSCOM_ISR_TX 0x02
124#define UMOSCOM_ISR_RX 0x04
125#define UMOSCOM_ISR_LINE 0x06
126#define UMOSCOM_ISR_RXTIMEOUT 0x0c
127#define UMOSCOM_ISR_RX_XOFF 0x10
128#define UMOSCOM_ISR_RTSCTS 0x20
129#define UMOSCOM_ISR_FIFOEN 0xc0
130
131#define UMOSCOM_LCR_DBITS(x) ((x) - 5)
132#define UMOSCOM_LCR_STOP_BITS_1 0x00
133#define UMOSCOM_LCR_STOP_BITS_2 0x04 /* 2 if 6-8 bits/char or 1.5 if 5 */
134#define UMOSCOM_LCR_PARITY_NONE 0x00
135#define UMOSCOM_LCR_PARITY_ODD 0x08
136#define UMOSCOM_LCR_PARITY_EVEN 0x18
137#define UMOSCOM_LCR_BREAK 0x40
138#define UMOSCOM_LCR_DIVLATCH_EN 0x80
139
140#define UMOSCOM_MCR_DTR 0x01
141#define UMOSCOM_MCR_RTS 0x02
142#define UMOSCOM_MCR_LOOP 0x04
143#define UMOSCOM_MCR_INTEN 0x08
144#define UMOSCOM_MCR_LOOPBACK 0x10
145#define UMOSCOM_MCR_XONANY 0x20
146#define UMOSCOM_MCR_IRDA_EN 0x40
147#define UMOSCOM_MCR_BAUD_DIV4 0x80
148
149#define UMOSCOM_LSR_RXDATA 0x01
150#define UMOSCOM_LSR_RXOVER 0x02
151#define UMOSCOM_LSR_RXPAR_ERR 0x04
152#define UMOSCOM_LSR_RXFRM_ERR 0x08
153#define UMOSCOM_LSR_RXBREAK 0x10
154#define UMOSCOM_LSR_TXEMPTY 0x20
155#define UMOSCOM_LSR_TXALLEMPTY 0x40
156#define UMOSCOM_LSR_TXFIFO_ERR 0x80
157
158#define UMOSCOM_MSR_CTS_CHG 0x01
159#define UMOSCOM_MSR_DSR_CHG 0x02
160#define UMOSCOM_MSR_RI_CHG 0x04
161#define UMOSCOM_MSR_CD_CHG 0x08
162#define UMOSCOM_MSR_CTS 0x10
163#define UMOSCOM_MSR_RTS 0x20
164#define UMOSCOM_MSR_RI 0x40
165#define UMOSCOM_MSR_CD 0x80
166
167#define UMOSCOM_BAUD_REF 115200
168
169enum {
170 UMOSCOM_BULK_DT_WR,
171 UMOSCOM_BULK_DT_RD,
172 UMOSCOM_INTR_DT_RD,
173 UMOSCOM_N_TRANSFER,
174};
175
176struct umoscom_softc {
177 struct ucom_super_softc sc_super_ucom;
178 struct ucom_softc sc_ucom;
179
180 struct usb_xfer *sc_xfer[UMOSCOM_N_TRANSFER];
181 struct usb_device *sc_udev;
182 struct mtx sc_mtx;
183
184 uint8_t sc_mcr;
185 uint8_t sc_lcr;
186};
187
188/* prototypes */
189
190static device_probe_t umoscom_probe;
191static device_attach_t umoscom_attach;
192static device_detach_t umoscom_detach;
193
194static usb_callback_t umoscom_write_callback;
195static usb_callback_t umoscom_read_callback;
196static usb_callback_t umoscom_intr_callback;
197
198static void umoscom_cfg_open(struct ucom_softc *);
199static void umoscom_cfg_close(struct ucom_softc *);
200static void umoscom_cfg_set_break(struct ucom_softc *, uint8_t);
201static void umoscom_cfg_set_dtr(struct ucom_softc *, uint8_t);
202static void umoscom_cfg_set_rts(struct ucom_softc *, uint8_t);
203static int umoscom_pre_param(struct ucom_softc *, struct termios *);
204static void umoscom_cfg_param(struct ucom_softc *, struct termios *);
205static void umoscom_cfg_get_status(struct ucom_softc *, uint8_t *,
206 uint8_t *);
207static void umoscom_cfg_write(struct umoscom_softc *, uint16_t, uint16_t);
208static uint8_t umoscom_cfg_read(struct umoscom_softc *, uint16_t);
209static void umoscom_start_read(struct ucom_softc *);
210static void umoscom_stop_read(struct ucom_softc *);
211static void umoscom_start_write(struct ucom_softc *);
212static void umoscom_stop_write(struct ucom_softc *);
213static void umoscom_poll(struct ucom_softc *ucom);
214
215static const struct usb_config umoscom_config_data[UMOSCOM_N_TRANSFER] = {
216
217 [UMOSCOM_BULK_DT_WR] = {
218 .type = UE_BULK,
219 .endpoint = UE_ADDR_ANY,
220 .direction = UE_DIR_OUT,
221 .bufsize = UMOSCOM_BUFSIZE,
222 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
223 .callback = &umoscom_write_callback,
224 },
225
226 [UMOSCOM_BULK_DT_RD] = {
227 .type = UE_BULK,
228 .endpoint = UE_ADDR_ANY,
229 .direction = UE_DIR_IN,
230 .bufsize = UMOSCOM_BUFSIZE,
231 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
232 .callback = &umoscom_read_callback,
233 },
234
235 [UMOSCOM_INTR_DT_RD] = {
236 .type = UE_INTERRUPT,
237 .endpoint = UE_ADDR_ANY,
238 .direction = UE_DIR_IN,
239 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
240 .bufsize = 0, /* use wMaxPacketSize */
241 .callback = &umoscom_intr_callback,
242 },
243};
244
245static const struct ucom_callback umoscom_callback = {
246 /* configuration callbacks */
247 .ucom_cfg_get_status = &umoscom_cfg_get_status,
248 .ucom_cfg_set_dtr = &umoscom_cfg_set_dtr,
249 .ucom_cfg_set_rts = &umoscom_cfg_set_rts,
250 .ucom_cfg_set_break = &umoscom_cfg_set_break,
251 .ucom_cfg_param = &umoscom_cfg_param,
252 .ucom_cfg_open = &umoscom_cfg_open,
253 .ucom_cfg_close = &umoscom_cfg_close,
254
255 /* other callbacks */
256 .ucom_pre_param = &umoscom_pre_param,
257 .ucom_start_read = &umoscom_start_read,
258 .ucom_stop_read = &umoscom_stop_read,
259 .ucom_start_write = &umoscom_start_write,
260 .ucom_stop_write = &umoscom_stop_write,
261 .ucom_poll = &umoscom_poll,
262};
263
264static device_method_t umoscom_methods[] = {
265 DEVMETHOD(device_probe, umoscom_probe),
266 DEVMETHOD(device_attach, umoscom_attach),
267 DEVMETHOD(device_detach, umoscom_detach),
268 {0, 0}
269};
270
271static devclass_t umoscom_devclass;
272
273static driver_t umoscom_driver = {
274 .name = "umoscom",
275 .methods = umoscom_methods,
276 .size = sizeof(struct umoscom_softc),
277};
278
279DRIVER_MODULE(umoscom, uhub, umoscom_driver, umoscom_devclass, NULL, 0);
280MODULE_DEPEND(umoscom, ucom, 1, 1, 1);
281MODULE_DEPEND(umoscom, usb, 1, 1, 1);
282MODULE_VERSION(umoscom, 1);
282
283static const struct usb_device_id umoscom_devs[] = {
284 {USB_VPI(USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7703, 0)}
285};
286
287static int
288umoscom_probe(device_t dev)
289{
290 struct usb_attach_arg *uaa = device_get_ivars(dev);
291
292 if (uaa->usb_mode != USB_MODE_HOST) {
293 return (ENXIO);
294 }
295 if (uaa->info.bConfigIndex != UMOSCOM_CONFIG_INDEX) {
296 return (ENXIO);
297 }
298 if (uaa->info.bIfaceIndex != UMOSCOM_IFACE_INDEX) {
299 return (ENXIO);
300 }
301 return (usbd_lookup_id_by_uaa(umoscom_devs, sizeof(umoscom_devs), uaa));
302}
303
304static int
305umoscom_attach(device_t dev)
306{
307 struct usb_attach_arg *uaa = device_get_ivars(dev);
308 struct umoscom_softc *sc = device_get_softc(dev);
309 int error;
310 uint8_t iface_index;
311
312 sc->sc_udev = uaa->device;
313 sc->sc_mcr = 0x08; /* enable interrupts */
314
315 /* XXX the device doesn't provide any ID string, so set a static one */
316 device_set_desc(dev, "MOSCHIP USB Serial Port Adapter");
317 device_printf(dev, "<MOSCHIP USB Serial Port Adapter>\n");
318
319 mtx_init(&sc->sc_mtx, "umoscom", NULL, MTX_DEF);
320
321 iface_index = UMOSCOM_IFACE_INDEX;
322 error = usbd_transfer_setup(uaa->device, &iface_index,
323 sc->sc_xfer, umoscom_config_data,
324 UMOSCOM_N_TRANSFER, sc, &sc->sc_mtx);
325
326 if (error) {
327 goto detach;
328 }
329 /* clear stall at first run */
330 mtx_lock(&sc->sc_mtx);
331 usbd_xfer_set_stall(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
332 usbd_xfer_set_stall(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
333 mtx_unlock(&sc->sc_mtx);
334
335 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
336 &umoscom_callback, &sc->sc_mtx);
337 if (error) {
338 goto detach;
339 }
340 return (0);
341
342detach:
343 device_printf(dev, "attach error: %s\n", usbd_errstr(error));
344 umoscom_detach(dev);
345 return (ENXIO);
346}
347
348static int
349umoscom_detach(device_t dev)
350{
351 struct umoscom_softc *sc = device_get_softc(dev);
352
353 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
354 usbd_transfer_unsetup(sc->sc_xfer, UMOSCOM_N_TRANSFER);
355 mtx_destroy(&sc->sc_mtx);
356
357 return (0);
358}
359
360static void
361umoscom_cfg_open(struct ucom_softc *ucom)
362{
363 struct umoscom_softc *sc = ucom->sc_parent;
364
365 DPRINTF("\n");
366
367 /* Purge FIFOs or odd things happen */
368 umoscom_cfg_write(sc, UMOSCOM_FIFO, 0x00 | UMOSCOM_UART_REG);
369
370 /* Enable FIFO */
371 umoscom_cfg_write(sc, UMOSCOM_FIFO, UMOSCOM_FIFO_EN |
372 UMOSCOM_FIFO_RXCLR | UMOSCOM_FIFO_TXCLR |
373 UMOSCOM_FIFO_DMA_BLK | UMOSCOM_FIFO_RXLVL_MASK |
374 UMOSCOM_UART_REG);
375
376 /* Enable Interrupt Registers */
377 umoscom_cfg_write(sc, UMOSCOM_INT, 0x0C | UMOSCOM_UART_REG);
378
379 /* Magic */
380 umoscom_cfg_write(sc, 0x01, 0x08);
381
382 /* Magic */
383 umoscom_cfg_write(sc, 0x00, 0x02);
384}
385
386static void
387umoscom_cfg_close(struct ucom_softc *ucom)
388{
389 return;
390}
391
392static void
393umoscom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
394{
395 struct umoscom_softc *sc = ucom->sc_parent;
396 uint16_t val;
397
398 val = sc->sc_lcr;
399 if (onoff)
400 val |= UMOSCOM_LCR_BREAK;
401
402 umoscom_cfg_write(sc, UMOSCOM_LCR, val | UMOSCOM_UART_REG);
403}
404
405static void
406umoscom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
407{
408 struct umoscom_softc *sc = ucom->sc_parent;
409
410 if (onoff)
411 sc->sc_mcr |= UMOSCOM_MCR_DTR;
412 else
413 sc->sc_mcr &= ~UMOSCOM_MCR_DTR;
414
415 umoscom_cfg_write(sc, UMOSCOM_MCR, sc->sc_mcr | UMOSCOM_UART_REG);
416}
417
418static void
419umoscom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
420{
421 struct umoscom_softc *sc = ucom->sc_parent;
422
423 if (onoff)
424 sc->sc_mcr |= UMOSCOM_MCR_RTS;
425 else
426 sc->sc_mcr &= ~UMOSCOM_MCR_RTS;
427
428 umoscom_cfg_write(sc, UMOSCOM_MCR, sc->sc_mcr | UMOSCOM_UART_REG);
429}
430
431static int
432umoscom_pre_param(struct ucom_softc *ucom, struct termios *t)
433{
434 if ((t->c_ospeed <= 1) || (t->c_ospeed > 115200))
435 return (EINVAL);
436
437 return (0);
438}
439
440static void
441umoscom_cfg_param(struct ucom_softc *ucom, struct termios *t)
442{
443 struct umoscom_softc *sc = ucom->sc_parent;
444 uint16_t data;
445
446 DPRINTF("speed=%d\n", t->c_ospeed);
447
448 data = ((uint32_t)UMOSCOM_BAUD_REF) / ((uint32_t)t->c_ospeed);
449
450 if (data == 0) {
451 DPRINTF("invalid baud rate!\n");
452 return;
453 }
454 umoscom_cfg_write(sc, UMOSCOM_LCR,
455 UMOSCOM_LCR_DIVLATCH_EN | UMOSCOM_UART_REG);
456
457 umoscom_cfg_write(sc, UMOSCOM_BAUDLO,
458 (data & 0xFF) | UMOSCOM_UART_REG);
459
460 umoscom_cfg_write(sc, UMOSCOM_BAUDHI,
461 ((data >> 8) & 0xFF) | UMOSCOM_UART_REG);
462
463 if (t->c_cflag & CSTOPB)
464 data = UMOSCOM_LCR_STOP_BITS_2;
465 else
466 data = UMOSCOM_LCR_STOP_BITS_1;
467
468 if (t->c_cflag & PARENB) {
469 if (t->c_cflag & PARODD)
470 data |= UMOSCOM_LCR_PARITY_ODD;
471 else
472 data |= UMOSCOM_LCR_PARITY_EVEN;
473 } else
474 data |= UMOSCOM_LCR_PARITY_NONE;
475
476 switch (t->c_cflag & CSIZE) {
477 case CS5:
478 data |= UMOSCOM_LCR_DBITS(5);
479 break;
480 case CS6:
481 data |= UMOSCOM_LCR_DBITS(6);
482 break;
483 case CS7:
484 data |= UMOSCOM_LCR_DBITS(7);
485 break;
486 case CS8:
487 data |= UMOSCOM_LCR_DBITS(8);
488 break;
489 }
490
491 sc->sc_lcr = data;
492 umoscom_cfg_write(sc, UMOSCOM_LCR, data | UMOSCOM_UART_REG);
493}
494
495static void
496umoscom_cfg_get_status(struct ucom_softc *ucom, uint8_t *p_lsr, uint8_t *p_msr)
497{
498 struct umoscom_softc *sc = ucom->sc_parent;
499 uint8_t lsr;
500 uint8_t msr;
501
502 DPRINTFN(5, "\n");
503
504 /* read status registers */
505
506 lsr = umoscom_cfg_read(sc, UMOSCOM_LSR);
507 msr = umoscom_cfg_read(sc, UMOSCOM_MSR);
508
509 /* translate bits */
510
511 if (msr & UMOSCOM_MSR_CTS)
512 *p_msr |= SER_CTS;
513
514 if (msr & UMOSCOM_MSR_CD)
515 *p_msr |= SER_DCD;
516
517 if (msr & UMOSCOM_MSR_RI)
518 *p_msr |= SER_RI;
519
520 if (msr & UMOSCOM_MSR_RTS)
521 *p_msr |= SER_DSR;
522}
523
524static void
525umoscom_cfg_write(struct umoscom_softc *sc, uint16_t reg, uint16_t val)
526{
527 struct usb_device_request req;
528
529 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
530 req.bRequest = UMOSCOM_WRITE;
531 USETW(req.wValue, val);
532 USETW(req.wIndex, reg);
533 USETW(req.wLength, 0);
534
535 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
536 &req, NULL, 0, 1000);
537}
538
539static uint8_t
540umoscom_cfg_read(struct umoscom_softc *sc, uint16_t reg)
541{
542 struct usb_device_request req;
543 uint8_t val;
544
545 req.bmRequestType = UT_READ_VENDOR_DEVICE;
546 req.bRequest = UMOSCOM_READ;
547 USETW(req.wValue, 0);
548 USETW(req.wIndex, reg);
549 USETW(req.wLength, 1);
550
551 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
552 &req, &val, 0, 1000);
553
554 DPRINTF("reg=0x%04x, val=0x%02x\n", reg, val);
555
556 return (val);
557}
558
559static void
560umoscom_start_read(struct ucom_softc *ucom)
561{
562 struct umoscom_softc *sc = ucom->sc_parent;
563
564#if 0
565 /* start interrupt endpoint */
566 usbd_transfer_start(sc->sc_xfer[UMOSCOM_INTR_DT_RD]);
567#endif
568 /* start read endpoint */
569 usbd_transfer_start(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
570}
571
572static void
573umoscom_stop_read(struct ucom_softc *ucom)
574{
575 struct umoscom_softc *sc = ucom->sc_parent;
576
577 /* stop interrupt transfer */
578 usbd_transfer_stop(sc->sc_xfer[UMOSCOM_INTR_DT_RD]);
579
580 /* stop read endpoint */
581 usbd_transfer_stop(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
582}
583
584static void
585umoscom_start_write(struct ucom_softc *ucom)
586{
587 struct umoscom_softc *sc = ucom->sc_parent;
588
589 usbd_transfer_start(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
590}
591
592static void
593umoscom_stop_write(struct ucom_softc *ucom)
594{
595 struct umoscom_softc *sc = ucom->sc_parent;
596
597 usbd_transfer_stop(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
598}
599
600static void
601umoscom_write_callback(struct usb_xfer *xfer, usb_error_t error)
602{
603 struct umoscom_softc *sc = usbd_xfer_softc(xfer);
604 struct usb_page_cache *pc;
605 uint32_t actlen;
606
607 switch (USB_GET_STATE(xfer)) {
608 case USB_ST_SETUP:
609 case USB_ST_TRANSFERRED:
610tr_setup:
611 DPRINTF("\n");
612
613 pc = usbd_xfer_get_frame(xfer, 0);
614 if (ucom_get_data(&sc->sc_ucom, pc, 0,
615 UMOSCOM_BUFSIZE, &actlen)) {
616
617 usbd_xfer_set_frame_len(xfer, 0, actlen);
618 usbd_transfer_submit(xfer);
619 }
620 return;
621
622 default: /* Error */
623 if (error != USB_ERR_CANCELLED) {
624 DPRINTFN(0, "transfer failed\n");
625 /* try to clear stall first */
626 usbd_xfer_set_stall(xfer);
627 goto tr_setup;
628 }
629 return;
630 }
631}
632
633static void
634umoscom_read_callback(struct usb_xfer *xfer, usb_error_t error)
635{
636 struct umoscom_softc *sc = usbd_xfer_softc(xfer);
637 struct usb_page_cache *pc;
638 int actlen;
639
640 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
641
642 switch (USB_GET_STATE(xfer)) {
643 case USB_ST_TRANSFERRED:
644 DPRINTF("got %d bytes\n", actlen);
645 pc = usbd_xfer_get_frame(xfer, 0);
646 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
647
648 case USB_ST_SETUP:
649tr_setup:
650 DPRINTF("\n");
651
652 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
653 usbd_transfer_submit(xfer);
654 return;
655
656 default: /* Error */
657 if (error != USB_ERR_CANCELLED) {
658 DPRINTFN(0, "transfer failed\n");
659 /* try to clear stall first */
660 usbd_xfer_set_stall(xfer);
661 goto tr_setup;
662 }
663 return;
664 }
665}
666
667static void
668umoscom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
669{
670 struct umoscom_softc *sc = usbd_xfer_softc(xfer);
671 int actlen;
672
673 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
674
675 switch (USB_GET_STATE(xfer)) {
676 case USB_ST_TRANSFERRED:
677 if (actlen < 2) {
678 DPRINTF("too short message\n");
679 goto tr_setup;
680 }
681 ucom_status_change(&sc->sc_ucom);
682
683 case USB_ST_SETUP:
684tr_setup:
685 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
686 usbd_transfer_submit(xfer);
687 return;
688
689 default: /* Error */
690 if (error != USB_ERR_CANCELLED) {
691 DPRINTFN(0, "transfer failed\n");
692 /* try to clear stall first */
693 usbd_xfer_set_stall(xfer);
694 goto tr_setup;
695 }
696 return;
697 }
698}
699
700static void
701umoscom_poll(struct ucom_softc *ucom)
702{
703 struct umoscom_softc *sc = ucom->sc_parent;
704 usbd_transfer_poll(sc->sc_xfer, UMOSCOM_N_TRANSFER);
705}
283
284static const struct usb_device_id umoscom_devs[] = {
285 {USB_VPI(USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7703, 0)}
286};
287
288static int
289umoscom_probe(device_t dev)
290{
291 struct usb_attach_arg *uaa = device_get_ivars(dev);
292
293 if (uaa->usb_mode != USB_MODE_HOST) {
294 return (ENXIO);
295 }
296 if (uaa->info.bConfigIndex != UMOSCOM_CONFIG_INDEX) {
297 return (ENXIO);
298 }
299 if (uaa->info.bIfaceIndex != UMOSCOM_IFACE_INDEX) {
300 return (ENXIO);
301 }
302 return (usbd_lookup_id_by_uaa(umoscom_devs, sizeof(umoscom_devs), uaa));
303}
304
305static int
306umoscom_attach(device_t dev)
307{
308 struct usb_attach_arg *uaa = device_get_ivars(dev);
309 struct umoscom_softc *sc = device_get_softc(dev);
310 int error;
311 uint8_t iface_index;
312
313 sc->sc_udev = uaa->device;
314 sc->sc_mcr = 0x08; /* enable interrupts */
315
316 /* XXX the device doesn't provide any ID string, so set a static one */
317 device_set_desc(dev, "MOSCHIP USB Serial Port Adapter");
318 device_printf(dev, "<MOSCHIP USB Serial Port Adapter>\n");
319
320 mtx_init(&sc->sc_mtx, "umoscom", NULL, MTX_DEF);
321
322 iface_index = UMOSCOM_IFACE_INDEX;
323 error = usbd_transfer_setup(uaa->device, &iface_index,
324 sc->sc_xfer, umoscom_config_data,
325 UMOSCOM_N_TRANSFER, sc, &sc->sc_mtx);
326
327 if (error) {
328 goto detach;
329 }
330 /* clear stall at first run */
331 mtx_lock(&sc->sc_mtx);
332 usbd_xfer_set_stall(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
333 usbd_xfer_set_stall(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
334 mtx_unlock(&sc->sc_mtx);
335
336 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
337 &umoscom_callback, &sc->sc_mtx);
338 if (error) {
339 goto detach;
340 }
341 return (0);
342
343detach:
344 device_printf(dev, "attach error: %s\n", usbd_errstr(error));
345 umoscom_detach(dev);
346 return (ENXIO);
347}
348
349static int
350umoscom_detach(device_t dev)
351{
352 struct umoscom_softc *sc = device_get_softc(dev);
353
354 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
355 usbd_transfer_unsetup(sc->sc_xfer, UMOSCOM_N_TRANSFER);
356 mtx_destroy(&sc->sc_mtx);
357
358 return (0);
359}
360
361static void
362umoscom_cfg_open(struct ucom_softc *ucom)
363{
364 struct umoscom_softc *sc = ucom->sc_parent;
365
366 DPRINTF("\n");
367
368 /* Purge FIFOs or odd things happen */
369 umoscom_cfg_write(sc, UMOSCOM_FIFO, 0x00 | UMOSCOM_UART_REG);
370
371 /* Enable FIFO */
372 umoscom_cfg_write(sc, UMOSCOM_FIFO, UMOSCOM_FIFO_EN |
373 UMOSCOM_FIFO_RXCLR | UMOSCOM_FIFO_TXCLR |
374 UMOSCOM_FIFO_DMA_BLK | UMOSCOM_FIFO_RXLVL_MASK |
375 UMOSCOM_UART_REG);
376
377 /* Enable Interrupt Registers */
378 umoscom_cfg_write(sc, UMOSCOM_INT, 0x0C | UMOSCOM_UART_REG);
379
380 /* Magic */
381 umoscom_cfg_write(sc, 0x01, 0x08);
382
383 /* Magic */
384 umoscom_cfg_write(sc, 0x00, 0x02);
385}
386
387static void
388umoscom_cfg_close(struct ucom_softc *ucom)
389{
390 return;
391}
392
393static void
394umoscom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
395{
396 struct umoscom_softc *sc = ucom->sc_parent;
397 uint16_t val;
398
399 val = sc->sc_lcr;
400 if (onoff)
401 val |= UMOSCOM_LCR_BREAK;
402
403 umoscom_cfg_write(sc, UMOSCOM_LCR, val | UMOSCOM_UART_REG);
404}
405
406static void
407umoscom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
408{
409 struct umoscom_softc *sc = ucom->sc_parent;
410
411 if (onoff)
412 sc->sc_mcr |= UMOSCOM_MCR_DTR;
413 else
414 sc->sc_mcr &= ~UMOSCOM_MCR_DTR;
415
416 umoscom_cfg_write(sc, UMOSCOM_MCR, sc->sc_mcr | UMOSCOM_UART_REG);
417}
418
419static void
420umoscom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
421{
422 struct umoscom_softc *sc = ucom->sc_parent;
423
424 if (onoff)
425 sc->sc_mcr |= UMOSCOM_MCR_RTS;
426 else
427 sc->sc_mcr &= ~UMOSCOM_MCR_RTS;
428
429 umoscom_cfg_write(sc, UMOSCOM_MCR, sc->sc_mcr | UMOSCOM_UART_REG);
430}
431
432static int
433umoscom_pre_param(struct ucom_softc *ucom, struct termios *t)
434{
435 if ((t->c_ospeed <= 1) || (t->c_ospeed > 115200))
436 return (EINVAL);
437
438 return (0);
439}
440
441static void
442umoscom_cfg_param(struct ucom_softc *ucom, struct termios *t)
443{
444 struct umoscom_softc *sc = ucom->sc_parent;
445 uint16_t data;
446
447 DPRINTF("speed=%d\n", t->c_ospeed);
448
449 data = ((uint32_t)UMOSCOM_BAUD_REF) / ((uint32_t)t->c_ospeed);
450
451 if (data == 0) {
452 DPRINTF("invalid baud rate!\n");
453 return;
454 }
455 umoscom_cfg_write(sc, UMOSCOM_LCR,
456 UMOSCOM_LCR_DIVLATCH_EN | UMOSCOM_UART_REG);
457
458 umoscom_cfg_write(sc, UMOSCOM_BAUDLO,
459 (data & 0xFF) | UMOSCOM_UART_REG);
460
461 umoscom_cfg_write(sc, UMOSCOM_BAUDHI,
462 ((data >> 8) & 0xFF) | UMOSCOM_UART_REG);
463
464 if (t->c_cflag & CSTOPB)
465 data = UMOSCOM_LCR_STOP_BITS_2;
466 else
467 data = UMOSCOM_LCR_STOP_BITS_1;
468
469 if (t->c_cflag & PARENB) {
470 if (t->c_cflag & PARODD)
471 data |= UMOSCOM_LCR_PARITY_ODD;
472 else
473 data |= UMOSCOM_LCR_PARITY_EVEN;
474 } else
475 data |= UMOSCOM_LCR_PARITY_NONE;
476
477 switch (t->c_cflag & CSIZE) {
478 case CS5:
479 data |= UMOSCOM_LCR_DBITS(5);
480 break;
481 case CS6:
482 data |= UMOSCOM_LCR_DBITS(6);
483 break;
484 case CS7:
485 data |= UMOSCOM_LCR_DBITS(7);
486 break;
487 case CS8:
488 data |= UMOSCOM_LCR_DBITS(8);
489 break;
490 }
491
492 sc->sc_lcr = data;
493 umoscom_cfg_write(sc, UMOSCOM_LCR, data | UMOSCOM_UART_REG);
494}
495
496static void
497umoscom_cfg_get_status(struct ucom_softc *ucom, uint8_t *p_lsr, uint8_t *p_msr)
498{
499 struct umoscom_softc *sc = ucom->sc_parent;
500 uint8_t lsr;
501 uint8_t msr;
502
503 DPRINTFN(5, "\n");
504
505 /* read status registers */
506
507 lsr = umoscom_cfg_read(sc, UMOSCOM_LSR);
508 msr = umoscom_cfg_read(sc, UMOSCOM_MSR);
509
510 /* translate bits */
511
512 if (msr & UMOSCOM_MSR_CTS)
513 *p_msr |= SER_CTS;
514
515 if (msr & UMOSCOM_MSR_CD)
516 *p_msr |= SER_DCD;
517
518 if (msr & UMOSCOM_MSR_RI)
519 *p_msr |= SER_RI;
520
521 if (msr & UMOSCOM_MSR_RTS)
522 *p_msr |= SER_DSR;
523}
524
525static void
526umoscom_cfg_write(struct umoscom_softc *sc, uint16_t reg, uint16_t val)
527{
528 struct usb_device_request req;
529
530 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
531 req.bRequest = UMOSCOM_WRITE;
532 USETW(req.wValue, val);
533 USETW(req.wIndex, reg);
534 USETW(req.wLength, 0);
535
536 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
537 &req, NULL, 0, 1000);
538}
539
540static uint8_t
541umoscom_cfg_read(struct umoscom_softc *sc, uint16_t reg)
542{
543 struct usb_device_request req;
544 uint8_t val;
545
546 req.bmRequestType = UT_READ_VENDOR_DEVICE;
547 req.bRequest = UMOSCOM_READ;
548 USETW(req.wValue, 0);
549 USETW(req.wIndex, reg);
550 USETW(req.wLength, 1);
551
552 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
553 &req, &val, 0, 1000);
554
555 DPRINTF("reg=0x%04x, val=0x%02x\n", reg, val);
556
557 return (val);
558}
559
560static void
561umoscom_start_read(struct ucom_softc *ucom)
562{
563 struct umoscom_softc *sc = ucom->sc_parent;
564
565#if 0
566 /* start interrupt endpoint */
567 usbd_transfer_start(sc->sc_xfer[UMOSCOM_INTR_DT_RD]);
568#endif
569 /* start read endpoint */
570 usbd_transfer_start(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
571}
572
573static void
574umoscom_stop_read(struct ucom_softc *ucom)
575{
576 struct umoscom_softc *sc = ucom->sc_parent;
577
578 /* stop interrupt transfer */
579 usbd_transfer_stop(sc->sc_xfer[UMOSCOM_INTR_DT_RD]);
580
581 /* stop read endpoint */
582 usbd_transfer_stop(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
583}
584
585static void
586umoscom_start_write(struct ucom_softc *ucom)
587{
588 struct umoscom_softc *sc = ucom->sc_parent;
589
590 usbd_transfer_start(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
591}
592
593static void
594umoscom_stop_write(struct ucom_softc *ucom)
595{
596 struct umoscom_softc *sc = ucom->sc_parent;
597
598 usbd_transfer_stop(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
599}
600
601static void
602umoscom_write_callback(struct usb_xfer *xfer, usb_error_t error)
603{
604 struct umoscom_softc *sc = usbd_xfer_softc(xfer);
605 struct usb_page_cache *pc;
606 uint32_t actlen;
607
608 switch (USB_GET_STATE(xfer)) {
609 case USB_ST_SETUP:
610 case USB_ST_TRANSFERRED:
611tr_setup:
612 DPRINTF("\n");
613
614 pc = usbd_xfer_get_frame(xfer, 0);
615 if (ucom_get_data(&sc->sc_ucom, pc, 0,
616 UMOSCOM_BUFSIZE, &actlen)) {
617
618 usbd_xfer_set_frame_len(xfer, 0, actlen);
619 usbd_transfer_submit(xfer);
620 }
621 return;
622
623 default: /* Error */
624 if (error != USB_ERR_CANCELLED) {
625 DPRINTFN(0, "transfer failed\n");
626 /* try to clear stall first */
627 usbd_xfer_set_stall(xfer);
628 goto tr_setup;
629 }
630 return;
631 }
632}
633
634static void
635umoscom_read_callback(struct usb_xfer *xfer, usb_error_t error)
636{
637 struct umoscom_softc *sc = usbd_xfer_softc(xfer);
638 struct usb_page_cache *pc;
639 int actlen;
640
641 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
642
643 switch (USB_GET_STATE(xfer)) {
644 case USB_ST_TRANSFERRED:
645 DPRINTF("got %d bytes\n", actlen);
646 pc = usbd_xfer_get_frame(xfer, 0);
647 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
648
649 case USB_ST_SETUP:
650tr_setup:
651 DPRINTF("\n");
652
653 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
654 usbd_transfer_submit(xfer);
655 return;
656
657 default: /* Error */
658 if (error != USB_ERR_CANCELLED) {
659 DPRINTFN(0, "transfer failed\n");
660 /* try to clear stall first */
661 usbd_xfer_set_stall(xfer);
662 goto tr_setup;
663 }
664 return;
665 }
666}
667
668static void
669umoscom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
670{
671 struct umoscom_softc *sc = usbd_xfer_softc(xfer);
672 int actlen;
673
674 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
675
676 switch (USB_GET_STATE(xfer)) {
677 case USB_ST_TRANSFERRED:
678 if (actlen < 2) {
679 DPRINTF("too short message\n");
680 goto tr_setup;
681 }
682 ucom_status_change(&sc->sc_ucom);
683
684 case USB_ST_SETUP:
685tr_setup:
686 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
687 usbd_transfer_submit(xfer);
688 return;
689
690 default: /* Error */
691 if (error != USB_ERR_CANCELLED) {
692 DPRINTFN(0, "transfer failed\n");
693 /* try to clear stall first */
694 usbd_xfer_set_stall(xfer);
695 goto tr_setup;
696 }
697 return;
698 }
699}
700
701static void
702umoscom_poll(struct ucom_softc *ucom)
703{
704 struct umoscom_softc *sc = ucom->sc_parent;
705 usbd_transfer_poll(sc->sc_xfer, UMOSCOM_N_TRANSFER);
706}