xref: /openbsd/sys/dev/usb/moscom.c (revision 76d0caae)
1 /*	$OpenBSD: moscom.c,v 1.24 2016/09/02 09:14:59 mpi Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/kernel.h>
22 #include <sys/tty.h>
23 #include <sys/device.h>
24 
25 #include <dev/usb/usb.h>
26 #include <dev/usb/usbdi.h>
27 #include <dev/usb/usbdi_util.h>
28 #include <dev/usb/usbdevs.h>
29 
30 #include <dev/usb/ucomvar.h>
31 
32 #define MOSCOMBUFSZ		256
33 #define MOSCOM_IFACE_NO		0
34 
35 #define MOSCOM_READ		0x0d
36 #define MOSCOM_WRITE		0x0e
37 #define MOSCOM_UART_REG		0x0300
38 #define MOSCOM_VEND_REG		0x0000
39 
40 #define MOSCOM_TXBUF		0x00	/* Write */
41 #define MOSCOM_RXBUF		0x00	/* Read */
42 #define MOSCOM_INT		0x01
43 #define MOSCOM_FIFO		0x02	/* Write */
44 #define MOSCOM_ISR		0x02	/* Read */
45 #define MOSCOM_LCR		0x03
46 #define MOSCOM_MCR		0x04
47 #define MOSCOM_LSR		0x05
48 #define MOSCOM_MSR		0x06
49 #define MOSCOM_SCRATCH		0x07
50 #define MOSCOM_DIV_LO		0x08
51 #define MOSCOM_DIV_HI		0x09
52 #define MOSCOM_EFR		0x0a
53 #define	MOSCOM_XON1		0x0b
54 #define MOSCOM_XON2		0x0c
55 #define MOSCOM_XOFF1		0x0d
56 #define MOSCOM_XOFF2		0x0e
57 
58 #define MOSCOM_BAUDLO		0x00
59 #define MOSCOM_BAUDHI		0x01
60 
61 #define MOSCOM_INT_RXEN		0x01
62 #define MOSCOM_INT_TXEN		0x02
63 #define MOSCOM_INT_RSEN		0x04
64 #define MOSCOM_INT_MDMEM	0x08
65 #define MOSCOM_INT_SLEEP	0x10
66 #define MOSCOM_INT_XOFF		0x20
67 #define MOSCOM_INT_RTS		0x40
68 
69 #define MOSCOM_FIFO_EN		0x01
70 #define MOSCOM_FIFO_RXCLR	0x02
71 #define MOSCOM_FIFO_TXCLR	0x04
72 #define MOSCOM_FIFO_DMA_BLK	0x08
73 #define MOSCOM_FIFO_TXLVL_MASK	0x30
74 #define MOSCOM_FIFO_TXLVL_8	0x00
75 #define MOSCOM_FIFO_TXLVL_16	0x10
76 #define MOSCOM_FIFO_TXLVL_32	0x20
77 #define MOSCOM_FIFO_TXLVL_56	0x30
78 #define MOSCOM_FIFO_RXLVL_MASK	0xc0
79 #define MOSCOM_FIFO_RXLVL_8	0x00
80 #define MOSCOM_FIFO_RXLVL_16	0x40
81 #define MOSCOM_FIFO_RXLVL_56	0x80
82 #define MOSCOM_FIFO_RXLVL_80	0xc0
83 
84 #define MOSCOM_ISR_MDM		0x00
85 #define MOSCOM_ISR_NONE		0x01
86 #define MOSCOM_ISR_TX		0x02
87 #define MOSCOM_ISR_RX		0x04
88 #define MOSCOM_ISR_LINE		0x06
89 #define MOSCOM_ISR_RXTIMEOUT	0x0c
90 #define MOSCOM_ISR_RX_XOFF	0x10
91 #define MOSCOM_ISR_RTSCTS	0x20
92 #define MOSCOM_ISR_FIFOEN	0xc0
93 
94 #define MOSCOM_LCR_DBITS(x)	(x - 5)
95 #define MOSCOM_LCR_STOP_BITS_1	0x00
96 #define MOSCOM_LCR_STOP_BITS_2	0x04	/* 2 if 6-8 bits/char or 1.5 if 5 */
97 #define MOSCOM_LCR_PARITY_NONE	0x00
98 #define MOSCOM_LCR_PARITY_ODD	0x08
99 #define MOSCOM_LCR_PARITY_EVEN	0x18
100 #define MOSCOM_LCR_BREAK	0x40
101 #define MOSCOM_LCR_DIVLATCH_EN	0x80
102 
103 #define MOSCOM_MCR_DTR		0x01
104 #define MOSCOM_MCR_RTS		0x02
105 #define MOSCOM_MCR_LOOP		0x04
106 #define MOSCOM_MCR_INTEN	0x08
107 #define MOSCOM_MCR_LOOPBACK	0x10
108 #define MOSCOM_MCR_XONANY	0x20
109 #define MOSCOM_MCR_IRDA_EN	0x40
110 #define MOSCOM_MCR_BAUD_DIV4	0x80
111 
112 #define MOSCOM_LSR_RXDATA	0x01
113 #define MOSCOM_LSR_RXOVER	0x02
114 #define MOSCOM_LSR_RXPAR_ERR	0x04
115 #define MOSCOM_LSR_RXFRM_ERR	0x08
116 #define MOSCOM_LSR_RXBREAK	0x10
117 #define MOSCOM_LSR_TXEMPTY	0x20
118 #define MOSCOM_LSR_TXALLEMPTY	0x40
119 #define MOSCOM_LSR_TXFIFO_ERR	0x80
120 
121 #define MOSCOM_MSR_CTS_CHG	0x01
122 #define MOSCOM_MSR_DSR_CHG	0x02
123 #define MOSCOM_MSR_RI_CHG	0x04
124 #define MOSCOM_MSR_CD_CHG	0x08
125 #define MOSCOM_MSR_CTS		0x10
126 #define MOSCOM_MSR_RTS		0x20
127 #define MOSCOM_MSR_RI		0x40
128 #define MOSCOM_MSR_CD		0x80
129 
130 #define MOSCOM_BAUD_REF		115200
131 
132 struct moscom_softc {
133 	struct device		 sc_dev;
134 	struct usbd_device	*sc_udev;
135 	struct usbd_interface	*sc_iface;
136 	struct device		*sc_subdev;
137 
138 	u_char			 sc_msr;
139 	u_char			 sc_lsr;
140 	u_char			 sc_lcr;
141 };
142 
143 void	moscom_set(void *, int, int, int);
144 int	moscom_param(void *, int, struct termios *);
145 int	moscom_open(void *, int);
146 int	moscom_cmd(struct moscom_softc *, int, int);
147 
148 struct ucom_methods moscom_methods = {
149 	NULL,
150 	moscom_set,
151 	moscom_param,
152 	NULL,
153 	moscom_open,
154 	NULL,
155 	NULL,
156 	NULL,
157 };
158 
159 static const struct usb_devno moscom_devs[] = {
160 	{ USB_VENDOR_MOSCHIP,		USB_PRODUCT_MOSCHIP_MCS7703 }
161 };
162 
163 int moscom_match(struct device *, void *, void *);
164 void moscom_attach(struct device *, struct device *, void *);
165 int moscom_detach(struct device *, int);
166 
167 struct cfdriver moscom_cd = {
168 	NULL, "moscom", DV_DULL
169 };
170 
171 const struct cfattach moscom_ca = {
172 	sizeof(struct moscom_softc), moscom_match, moscom_attach, moscom_detach
173 };
174 
175 int
176 moscom_match(struct device *parent, void *match, void *aux)
177 {
178 	struct usb_attach_arg *uaa = aux;
179 
180 	if (uaa->iface == NULL)
181 		return UMATCH_NONE;
182 
183 	return (usb_lookup(moscom_devs, uaa->vendor, uaa->product) != NULL) ?
184 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
185 }
186 
187 void
188 moscom_attach(struct device *parent, struct device *self, void *aux)
189 {
190 	struct moscom_softc *sc = (struct moscom_softc *)self;
191 	struct usb_attach_arg *uaa = aux;
192 	struct ucom_attach_args uca;
193 	usb_interface_descriptor_t *id;
194 	usb_endpoint_descriptor_t *ed;
195 	usbd_status error;
196 	int i;
197 
198 	bzero(&uca, sizeof(uca));
199 	sc->sc_udev = uaa->device;
200 
201 	/* get the first interface handle */
202 	error = usbd_device2interface_handle(sc->sc_udev, MOSCOM_IFACE_NO,
203 	    &sc->sc_iface);
204 	if (error != 0) {
205 		printf("%s: could not get interface handle\n",
206 		    sc->sc_dev.dv_xname);
207 		usbd_deactivate(sc->sc_udev);
208 		return;
209 	}
210 
211 	id = usbd_get_interface_descriptor(sc->sc_iface);
212 
213 	uca.bulkin = uca.bulkout = -1;
214 	for (i = 0; i < id->bNumEndpoints; i++) {
215 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
216 		if (ed == NULL) {
217 			printf("%s: no endpoint descriptor found for %d\n",
218 			    sc->sc_dev.dv_xname, i);
219 			usbd_deactivate(sc->sc_udev);
220 			return;
221 		}
222 
223 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
224 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
225 			uca.bulkin = ed->bEndpointAddress;
226 		else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
227 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
228 			uca.bulkout = ed->bEndpointAddress;
229 	}
230 
231 	if (uca.bulkin == -1 || uca.bulkout == -1) {
232 		printf("%s: missing endpoint\n", sc->sc_dev.dv_xname);
233 		usbd_deactivate(sc->sc_udev);
234 		return;
235 	}
236 
237 	uca.ibufsize = MOSCOMBUFSZ;
238 	uca.obufsize = MOSCOMBUFSZ;
239 	uca.ibufsizepad = MOSCOMBUFSZ;
240 	uca.opkthdrlen = 0;
241 	uca.device = sc->sc_udev;
242 	uca.iface = sc->sc_iface;
243 	uca.methods = &moscom_methods;
244 	uca.arg = sc;
245 	uca.info = NULL;
246 
247 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
248 }
249 
250 int
251 moscom_detach(struct device *self, int flags)
252 {
253 	struct moscom_softc *sc = (struct moscom_softc *)self;
254 	int rv = 0;
255 
256 	if (sc->sc_subdev != NULL) {
257 		rv = config_detach(sc->sc_subdev, flags);
258 		sc->sc_subdev = NULL;
259 	}
260 
261 	return (rv);
262 }
263 
264 int
265 moscom_open(void *vsc, int portno)
266 {
267 	struct moscom_softc *sc = vsc;
268 	usb_device_request_t req;
269 
270 	if (usbd_is_dying(sc->sc_udev))
271 		return (EIO);
272 
273 	/* Purge FIFOs or odd things happen */
274 	if (moscom_cmd(sc, MOSCOM_FIFO, 0x00) != 0)
275 		return (EIO);
276 
277 	if (moscom_cmd(sc, MOSCOM_FIFO, MOSCOM_FIFO_EN |
278 	    MOSCOM_FIFO_RXCLR | MOSCOM_FIFO_TXCLR |
279 	    MOSCOM_FIFO_DMA_BLK | MOSCOM_FIFO_RXLVL_MASK) != 0)
280 		return (EIO);
281 
282 	/* Magic tell device we're ready for data command */
283 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
284 	req.bRequest = MOSCOM_WRITE;
285 	USETW(req.wValue, 0x08);
286 	USETW(req.wIndex, MOSCOM_INT);
287 	USETW(req.wLength, 0);
288 	if (usbd_do_request(sc->sc_udev, &req, NULL) != 0)
289 		return (EIO);
290 
291 	return (0);
292 }
293 
294 void
295 moscom_set(void *vsc, int portno, int reg, int onoff)
296 {
297 	struct moscom_softc *sc = vsc;
298 	int val;
299 
300 	switch (reg) {
301 	case UCOM_SET_DTR:
302 		val = onoff ? MOSCOM_MCR_DTR : 0;
303 		break;
304 	case UCOM_SET_RTS:
305 		val = onoff ? MOSCOM_MCR_RTS : 0;
306 		break;
307 	case UCOM_SET_BREAK:
308 		val = sc->sc_lcr;
309 		if (onoff)
310 			val |= MOSCOM_LCR_BREAK;
311 		moscom_cmd(sc, MOSCOM_LCR, val);
312 		return;
313 	default:
314 		return;
315 	}
316 
317 	moscom_cmd(sc, MOSCOM_MCR, val);
318 }
319 
320 int
321 moscom_param(void *vsc, int portno, struct termios *t)
322 {
323 	struct moscom_softc *sc = (struct moscom_softc *)vsc;
324 	int data;
325 
326 	if (t->c_ospeed <= 0 || t->c_ospeed > 115200)
327 		return (EINVAL);
328 
329 	data = MOSCOM_BAUD_REF / t->c_ospeed;
330 
331 	if (data == 0 || data > 0xffff)
332 		return (EINVAL);
333 
334 	moscom_cmd(sc, MOSCOM_LCR, MOSCOM_LCR_DIVLATCH_EN);
335 	moscom_cmd(sc, MOSCOM_BAUDLO, data & 0xFF);
336 	moscom_cmd(sc, MOSCOM_BAUDHI, (data >> 8) & 0xFF);
337 
338 	if (ISSET(t->c_cflag, CSTOPB))
339 		data = MOSCOM_LCR_STOP_BITS_2;
340 	else
341 		data = MOSCOM_LCR_STOP_BITS_1;
342 	if (ISSET(t->c_cflag, PARENB)) {
343 		if (ISSET(t->c_cflag, PARODD))
344 			data |= MOSCOM_LCR_PARITY_ODD;
345 		else
346 			data |= MOSCOM_LCR_PARITY_EVEN;
347 	} else
348 		data |= MOSCOM_LCR_PARITY_NONE;
349 	switch (ISSET(t->c_cflag, CSIZE)) {
350 	case CS5:
351 		data |= MOSCOM_LCR_DBITS(5);
352 		break;
353 	case CS6:
354 		data |= MOSCOM_LCR_DBITS(6);
355 		break;
356 	case CS7:
357 		data |= MOSCOM_LCR_DBITS(7);
358 		break;
359 	case CS8:
360 		data |= MOSCOM_LCR_DBITS(8);
361 		break;
362 	}
363 
364 	sc->sc_lcr = data;
365 	moscom_cmd(sc, MOSCOM_LCR, sc->sc_lcr);
366 
367 #if 0
368 	/* XXX flow control */
369 	if (ISSET(t->c_cflag, CRTSCTS))
370 		/*  rts/cts flow ctl */
371 	} else if (ISSET(t->c_iflag, IXON|IXOFF)) {
372 		/*  xon/xoff flow ctl */
373 	} else {
374 		/* disable flow ctl */
375 	}
376 #endif
377 
378 	return (0);
379 }
380 
381 int
382 moscom_cmd(struct moscom_softc *sc, int reg, int val)
383 {
384 	usb_device_request_t req;
385 	usbd_status err;
386 
387 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
388 	req.bRequest = MOSCOM_WRITE;
389 	USETW(req.wValue, val + MOSCOM_UART_REG);
390 	USETW(req.wIndex, reg);
391 	USETW(req.wLength, 0);
392 	err = usbd_do_request(sc->sc_udev, &req, NULL);
393 	if (err)
394 		return (EIO);
395 	else
396 		return (0);
397 }
398