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