1 /* $OpenBSD: src/sys/dev/usb/moscom.c,v 1.11 2007/10/11 18:33:14 deraadt Exp $ */
2 /* $DragonFly: src/sys/dev/usbmisc/moscom/moscom.c,v 1.1 2007/11/07 08:57:18 hasso Exp $ */
5 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
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.
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.
20 #include <sys/param.h>
21 #include <sys/systm.h>
22 #include <sys/kernel.h>
25 #include <sys/device.h>
26 #include <sys/types.h>
28 #include <sys/module.h>
30 #include <bus/usb/usb.h>
31 #include <bus/usb/usbdi.h>
32 #include <bus/usb/usbdi_util.h>
34 #include <dev/usbmisc/ucom/ucomvar.h>
36 #define MOSCOMBUFSZ 256
37 #define MOSCOM_CONFIG_NO 0
38 #define MOSCOM_IFACE_NO 0
40 #define MOSCOM_READ 0x0d
41 #define MOSCOM_WRITE 0x0e
42 #define MOSCOM_UART_REG 0x0300
43 #define MOSCOM_VEND_REG 0x0000
45 #define MOSCOM_TXBUF 0x00 /* Write */
46 #define MOSCOM_RXBUF 0x00 /* Read */
47 #define MOSCOM_INT 0x01
48 #define MOSCOM_FIFO 0x02 /* Write */
49 #define MOSCOM_ISR 0x02 /* Read */
50 #define MOSCOM_LCR 0x03
51 #define MOSCOM_MCR 0x04
52 #define MOSCOM_LSR 0x05
53 #define MOSCOM_MSR 0x06
54 #define MOSCOM_SCRATCH 0x07
55 #define MOSCOM_DIV_LO 0x08
56 #define MOSCOM_DIV_HI 0x09
57 #define MOSCOM_EFR 0x0a
58 #define MOSCOM_XON1 0x0b
59 #define MOSCOM_XON2 0x0c
60 #define MOSCOM_XOFF1 0x0d
61 #define MOSCOM_XOFF2 0x0e
63 #define MOSCOM_BAUDLO 0x00
64 #define MOSCOM_BAUDHI 0x01
66 #define MOSCOM_INT_RXEN 0x01
67 #define MOSCOM_INT_TXEN 0x02
68 #define MOSCOM_INT_RSEN 0x04
69 #define MOSCOM_INT_MDMEM 0x08
70 #define MOSCOM_INT_SLEEP 0x10
71 #define MOSCOM_INT_XOFF 0x20
72 #define MOSCOM_INT_RTS 0x40
74 #define MOSCOM_FIFO_EN 0x01
75 #define MOSCOM_FIFO_RXCLR 0x02
76 #define MOSCOM_FIFO_TXCLR 0x04
77 #define MOSCOM_FIFO_DMA_BLK 0x08
78 #define MOSCOM_FIFO_TXLVL_MASK 0x30
79 #define MOSCOM_FIFO_TXLVL_8 0x00
80 #define MOSCOM_FIFO_TXLVL_16 0x10
81 #define MOSCOM_FIFO_TXLVL_32 0x20
82 #define MOSCOM_FIFO_TXLVL_56 0x30
83 #define MOSCOM_FIFO_RXLVL_MASK 0xc0
84 #define MOSCOM_FIFO_RXLVL_8 0x00
85 #define MOSCOM_FIFO_RXLVL_16 0x40
86 #define MOSCOM_FIFO_RXLVL_56 0x80
87 #define MOSCOM_FIFO_RXLVL_80 0xc0
89 #define MOSCOM_ISR_MDM 0x00
90 #define MOSCOM_ISR_NONE 0x01
91 #define MOSCOM_ISR_TX 0x02
92 #define MOSCOM_ISR_RX 0x04
93 #define MOSCOM_ISR_LINE 0x06
94 #define MOSCOM_ISR_RXTIMEOUT 0x0c
95 #define MOSCOM_ISR_RX_XOFF 0x10
96 #define MOSCOM_ISR_RTSCTS 0x20
97 #define MOSCOM_ISR_FIFOEN 0xc0
99 #define MOSCOM_LCR_DBITS(x) (x - 5)
100 #define MOSCOM_LCR_STOP_BITS_1 0x00
101 #define MOSCOM_LCR_STOP_BITS_2 0x04 /* 2 if 6-8 bits/char or 1.5 if 5 */
102 #define MOSCOM_LCR_PARITY_NONE 0x00
103 #define MOSCOM_LCR_PARITY_ODD 0x08
104 #define MOSCOM_LCR_PARITY_EVEN 0x18
105 #define MOSCOM_LCR_BREAK 0x40
106 #define MOSCOM_LCR_DIVLATCH_EN 0x80
108 #define MOSCOM_MCR_DTR 0x01
109 #define MOSCOM_MCR_RTS 0x02
110 #define MOSCOM_MCR_LOOP 0x04
111 #define MOSCOM_MCR_INTEN 0x08
112 #define MOSCOM_MCR_LOOPBACK 0x10
113 #define MOSCOM_MCR_XONANY 0x20
114 #define MOSCOM_MCR_IRDA_EN 0x40
115 #define MOSCOM_MCR_BAUD_DIV4 0x80
117 #define MOSCOM_LSR_RXDATA 0x01
118 #define MOSCOM_LSR_RXOVER 0x02
119 #define MOSCOM_LSR_RXPAR_ERR 0x04
120 #define MOSCOM_LSR_RXFRM_ERR 0x08
121 #define MOSCOM_LSR_RXBREAK 0x10
122 #define MOSCOM_LSR_TXEMPTY 0x20
123 #define MOSCOM_LSR_TXALLEMPTY 0x40
124 #define MOSCOM_LSR_TXFIFO_ERR 0x80
126 #define MOSCOM_MSR_CTS_CHG 0x01
127 #define MOSCOM_MSR_DSR_CHG 0x02
128 #define MOSCOM_MSR_RI_CHG 0x04
129 #define MOSCOM_MSR_CD_CHG 0x08
130 #define MOSCOM_MSR_CTS 0x10
131 #define MOSCOM_MSR_RTS 0x20
132 #define MOSCOM_MSR_RI 0x40
133 #define MOSCOM_MSR_CD 0x80
135 #define MOSCOM_BAUD_REF 115200
137 struct moscom_softc
{
138 struct ucom_softc sc_ucom
;
144 static void moscom_get_status(void *, int, u_char
*, u_char
*);
145 static void moscom_set(void *, int, int, int);
146 static int moscom_param(void *, int, struct termios
*);
147 static int moscom_open(void *, int);
148 static int moscom_cmd(struct moscom_softc
*, int, int);
150 struct ucom_callback moscom_callback
= {
161 static const struct usb_devno moscom_devs
[] = {
162 { USB_DEVICE(0x9710, 0x7703) } /* MosChip MCS7703 serial port */
165 static device_probe_t moscom_match
;
166 static device_attach_t moscom_attach
;
167 static device_detach_t moscom_detach
;
169 static device_method_t moscom_methods
[] = {
170 DEVMETHOD(device_probe
, moscom_match
),
171 DEVMETHOD(device_attach
, moscom_attach
),
172 DEVMETHOD(device_detach
, moscom_detach
),
176 static driver_t moscom_driver
= {
179 sizeof (struct moscom_softc
)
182 DRIVER_MODULE(moscom
, uhub
, moscom_driver
, ucom_devclass
, usbd_driver_load
, 0);
183 MODULE_DEPEND(moscom
, usb
, 1, 1, 1);
184 MODULE_DEPEND(moscom
, ucom
, UCOM_MINVER
, UCOM_PREFVER
, UCOM_MAXVER
);
185 MODULE_VERSION(moscom
, 1);
188 moscom_match(device_t self
)
190 struct usb_attach_arg
*uaa
= device_get_ivars(self
);
192 if (uaa
->iface
!= NULL
)
195 return (usb_lookup(moscom_devs
, uaa
->vendor
, uaa
->product
) != NULL
) ?
196 UMATCH_VENDOR_PRODUCT
: UMATCH_NONE
;
200 moscom_attach(device_t self
)
202 struct moscom_softc
*sc
= device_get_softc(self
);
203 struct usb_attach_arg
*uaa
= device_get_ivars(self
);
204 struct ucom_softc
*ucom
;
205 usb_interface_descriptor_t
*id
;
206 usb_endpoint_descriptor_t
*ed
;
210 bzero(sc
, sizeof (struct moscom_softc
));
213 ucom
->sc_udev
= uaa
->device
;
214 ucom
->sc_iface
= uaa
->iface
;
216 if (usbd_set_config_index(ucom
->sc_udev
, MOSCOM_CONFIG_NO
, 1) != 0) {
217 device_printf(ucom
->sc_dev
, "could not set configuration no\n");
222 /* get the first interface handle */
223 error
= usbd_device2interface_handle(ucom
->sc_udev
, MOSCOM_IFACE_NO
,
226 device_printf(ucom
->sc_dev
, "could not get interface handle\n");
231 id
= usbd_get_interface_descriptor(ucom
->sc_iface
);
233 ucom
->sc_bulkin_no
= ucom
->sc_bulkout_no
= -1;
234 for (i
= 0; i
< id
->bNumEndpoints
; i
++) {
235 ed
= usbd_interface2endpoint_descriptor(ucom
->sc_iface
, i
);
237 device_printf(ucom
->sc_dev
, "no endpoint descriptor "
238 "found for %d\n", i
);
243 if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
&&
244 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
)
245 ucom
->sc_bulkin_no
= ed
->bEndpointAddress
;
246 else if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_OUT
&&
247 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
)
248 ucom
->sc_bulkout_no
= ed
->bEndpointAddress
;
251 if (ucom
->sc_bulkin_no
== -1 || ucom
->sc_bulkout_no
== -1) {
252 device_printf(ucom
->sc_dev
, "missing endpoint\n");
257 ucom
->sc_parent
= sc
;
258 ucom
->sc_portno
= UCOM_UNK_PORTNO
;
259 ucom
->sc_ibufsize
= MOSCOMBUFSZ
;
260 ucom
->sc_obufsize
= MOSCOMBUFSZ
;
261 ucom
->sc_ibufsizepad
= MOSCOMBUFSZ
;
262 ucom
->sc_opkthdrlen
= 0;
263 ucom
->sc_callback
= &moscom_callback
;
265 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH
, ucom
->sc_udev
,
272 moscom_detach(device_t self
)
274 struct moscom_softc
*sc
= device_get_softc(self
);
277 sc
->sc_ucom
.sc_dying
= 1;
278 rv
= ucom_detach(&sc
->sc_ucom
);
279 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH
, sc
->sc_ucom
.sc_udev
,
287 moscom_activate(struct device
*self
, enum devact act
)
289 struct moscom_softc
*sc
= (struct moscom_softc
*)self
;
296 case DVACT_DEACTIVATE
:
297 if (sc
->sc_subdev
!= NULL
)
298 rv
= config_deactivate(sc
->sc_subdev
);
299 sc
->sc_ucom
.sc_dying
= 1;
307 moscom_open(void *vsc
, int portno
)
309 struct moscom_softc
*sc
= vsc
;
310 usb_device_request_t req
;
312 if (sc
->sc_ucom
.sc_dying
)
315 /* Purge FIFOs or odd things happen */
316 if (moscom_cmd(sc
, MOSCOM_FIFO
, 0x00) != 0)
319 if (moscom_cmd(sc
, MOSCOM_FIFO
, MOSCOM_FIFO_EN
|
320 MOSCOM_FIFO_RXCLR
| MOSCOM_FIFO_TXCLR
|
321 MOSCOM_FIFO_DMA_BLK
| MOSCOM_FIFO_RXLVL_MASK
) != 0)
324 /* Magic tell device we're ready for data command */
325 req
.bmRequestType
= UT_WRITE_VENDOR_DEVICE
;
326 req
.bRequest
= MOSCOM_WRITE
;
327 USETW(req
.wValue
, 0x08);
328 USETW(req
.wIndex
, MOSCOM_INT
);
329 USETW(req
.wLength
, 0);
330 if (usbd_do_request(sc
->sc_ucom
.sc_udev
, &req
, NULL
) != 0)
337 moscom_set(void *vsc
, int portno
, int reg
, int onoff
)
339 struct moscom_softc
*sc
= vsc
;
344 val
= onoff
? MOSCOM_MCR_DTR
: 0;
347 val
= onoff
? MOSCOM_MCR_RTS
: 0;
352 val
|= MOSCOM_LCR_BREAK
;
353 moscom_cmd(sc
, MOSCOM_LCR
, val
);
359 moscom_cmd(sc
, MOSCOM_MCR
, val
);
363 moscom_param(void *vsc
, int portno
, struct termios
*t
)
365 struct moscom_softc
*sc
= (struct moscom_softc
*)vsc
;
368 if (t
->c_ospeed
<= 0 || t
->c_ospeed
> 115200)
371 data
= MOSCOM_BAUD_REF
/ t
->c_ospeed
;
373 if (data
== 0 || data
> 0xffff)
376 moscom_cmd(sc
, MOSCOM_LCR
, MOSCOM_LCR_DIVLATCH_EN
);
377 moscom_cmd(sc
, MOSCOM_BAUDLO
, data
& 0xFF);
378 moscom_cmd(sc
, MOSCOM_BAUDHI
, (data
>> 8) & 0xFF);
380 if (ISSET(t
->c_cflag
, CSTOPB
))
381 data
= MOSCOM_LCR_STOP_BITS_2
;
383 data
= MOSCOM_LCR_STOP_BITS_1
;
384 if (ISSET(t
->c_cflag
, PARENB
)) {
385 if (ISSET(t
->c_cflag
, PARODD
))
386 data
|= MOSCOM_LCR_PARITY_ODD
;
388 data
|= MOSCOM_LCR_PARITY_EVEN
;
390 data
|= MOSCOM_LCR_PARITY_NONE
;
391 switch (ISSET(t
->c_cflag
, CSIZE
)) {
393 data
|= MOSCOM_LCR_DBITS(5);
396 data
|= MOSCOM_LCR_DBITS(6);
399 data
|= MOSCOM_LCR_DBITS(7);
402 data
|= MOSCOM_LCR_DBITS(8);
407 moscom_cmd(sc
, MOSCOM_LCR
, sc
->sc_lcr
);
410 /* XXX flow control */
411 if (ISSET(t
->c_cflag
, CRTSCTS
))
412 /* rts/cts flow ctl */
413 } else if (ISSET(t
->c_iflag
, IXON
|IXOFF
)) {
414 /* xon/xoff flow ctl */
416 /* disable flow ctl */
424 moscom_get_status(void *vsc
, int portno
, u_char
*lsr
, u_char
*msr
)
426 struct moscom_softc
*sc
= vsc
;
435 moscom_cmd(struct moscom_softc
*sc
, int reg
, int val
)
437 usb_device_request_t req
;
440 req
.bmRequestType
= UT_WRITE_VENDOR_DEVICE
;
441 req
.bRequest
= MOSCOM_WRITE
;
442 USETW(req
.wValue
, val
+ MOSCOM_UART_REG
);
443 USETW(req
.wIndex
, reg
);
444 USETW(req
.wLength
, 0);
445 err
= usbd_do_request(sc
->sc_ucom
.sc_udev
, &req
, NULL
);