xref: /openbsd-src/sys/dev/usb/ucycom.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: ucycom.c,v 1.20 2011/07/03 15:47:17 matthew Exp $	*/
2 /*	$NetBSD: ucycom.c,v 1.3 2005/08/05 07:27:47 skrll Exp $	*/
3 
4 /*
5  * Copyright (c) 2005 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Nick Hudson
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*
33  * This code is based on the ucom driver.
34  */
35 
36 /*
37  * Device driver for Cypress CY7C637xx and CY7C640/1xx series USB to
38  * RS232 bridges.
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/conf.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/device.h>
47 #include <sys/tty.h>
48 #include <sys/file.h>
49 #include <sys/vnode.h>
50 
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbhid.h>
53 
54 #include <dev/usb/usbdi.h>
55 #include <dev/usb/usbdi_util.h>
56 #include <dev/usb/usbdevs.h>
57 #include <dev/usb/uhidev.h>
58 #include <dev/usb/hid.h>
59 
60 #include <dev/usb/ucomvar.h>
61 
62 #ifdef UCYCOM_DEBUG
63 #define DPRINTF(x)	if (ucycomdebug) printf x
64 #define DPRINTFN(n, x)	if (ucycomdebug > (n)) printf x
65 int	ucycomdebug = 200;
66 #else
67 #define DPRINTF(x)
68 #define DPRINTFN(n,x)
69 #endif
70 
71 /* Configuration Byte */
72 #define UCYCOM_RESET		0x80
73 #define UCYCOM_PARITY_TYPE_MASK	0x20
74 #define  UCYCOM_PARITY_ODD	 0x20
75 #define  UCYCOM_PARITY_EVEN	 0x00
76 #define UCYCOM_PARITY_MASK	0x10
77 #define  UCYCOM_PARITY_ON	 0x10
78 #define  UCYCOM_PARITY_OFF	 0x00
79 #define UCYCOM_STOP_MASK	0x08
80 #define  UCYCOM_STOP_BITS_2	 0x08
81 #define  UCYCOM_STOP_BITS_1	 0x00
82 #define UCYCOM_DATA_MASK	0x03
83 #define  UCYCOM_DATA_BITS_8	 0x03
84 #define  UCYCOM_DATA_BITS_7	 0x02
85 #define  UCYCOM_DATA_BITS_6	 0x01
86 #define  UCYCOM_DATA_BITS_5	 0x00
87 
88 /* Modem (Input) status byte */
89 #define UCYCOM_RI	0x80
90 #define UCYCOM_DCD	0x40
91 #define UCYCOM_DSR	0x20
92 #define UCYCOM_CTS	0x10
93 #define UCYCOM_ERROR	0x08
94 #define UCYCOM_LMASK	0x07
95 
96 /* Modem (Output) control byte */
97 #define UCYCOM_DTR	0x20
98 #define UCYCOM_RTS	0x10
99 #define UCYCOM_ORESET	0x08
100 
101 struct ucycom_softc {
102 	struct uhidev		 sc_hdev;
103 	usbd_device_handle	 sc_udev;
104 
105 	/* uhidev parameters */
106 	size_t			 sc_flen;	/* feature report length */
107 	size_t			 sc_ilen;	/* input report length */
108 	size_t			 sc_olen;	/* output report length */
109 
110 	uint8_t			*sc_obuf;
111 
112 	uint8_t			*sc_ibuf;
113 	uint32_t		 sc_icnt;
114 
115 	/* settings */
116 	uint32_t		 sc_baud;
117 	uint8_t			 sc_cfg;	/* Data format */
118 	uint8_t			 sc_mcr;	/* Modem control */
119 	uint8_t			 sc_msr;	/* Modem status */
120 	uint8_t			 sc_newmsr;	/* from HID intr */
121 	int			 sc_swflags;
122 
123 	struct device		*sc_subdev;
124 
125 	/* flags */
126 	u_char			 sc_dying;
127 };
128 
129 /* Callback routines */
130 void	ucycom_set(void *, int, int, int);
131 int	ucycom_param(void *, int, struct termios *);
132 void	ucycom_get_status(void *, int, u_char *, u_char *);
133 int	ucycom_open(void *, int);
134 void	ucycom_close(void *, int);
135 void	ucycom_write(void *, int, u_char *, u_char *, u_int32_t *);
136 void	ucycom_read(void *, int, u_char **, u_int32_t *);
137 
138 struct ucom_methods ucycom_methods = {
139 	NULL, /* ucycom_get_status, */
140 	ucycom_set,
141 	ucycom_param,
142 	NULL,
143 	ucycom_open,
144 	ucycom_close,
145 	ucycom_read,
146 	ucycom_write,
147 };
148 
149 void ucycom_intr(struct uhidev *, void *, u_int);
150 
151 void ucycom_get_cfg(struct ucycom_softc *);
152 
153 const struct usb_devno ucycom_devs[] = {
154 	{ USB_VENDOR_CYPRESS, USB_PRODUCT_CYPRESS_USBRS232 },
155 	{ USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EMUSB },
156 	{ USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EMLT20 },
157 };
158 
159 int ucycom_match(struct device *, void *, void *);
160 void ucycom_attach(struct device *, struct device *, void *);
161 int ucycom_detach(struct device *, int);
162 int ucycom_activate(struct device *, int);
163 
164 struct cfdriver ucycom_cd = {
165 	NULL, "ucycom", DV_DULL
166 };
167 
168 const struct cfattach ucycom_ca = {
169 	sizeof(struct ucycom_softc),
170 	ucycom_match,
171 	ucycom_attach,
172 	ucycom_detach,
173 	ucycom_activate,
174 };
175 
176 int
177 ucycom_match(struct device *parent, void *match, void *aux)
178 {
179 	struct uhidev_attach_arg *uha = aux;
180 
181 	DPRINTF(("ucycom match\n"));
182 	return (usb_lookup(ucycom_devs, uha->uaa->vendor, uha->uaa->product) != NULL ?
183 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
184 }
185 
186 void
187 ucycom_attach(struct device *parent, struct device *self, void *aux)
188 {
189 	struct ucycom_softc *sc = (struct ucycom_softc *)self;
190 	struct usb_attach_arg *uaa = aux;
191 	struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa;
192 	usbd_device_handle dev = uha->parent->sc_udev;
193 	struct ucom_attach_args uca;
194 	int size, repid, err;
195 	void *desc;
196 
197 	sc->sc_hdev.sc_intr = ucycom_intr;
198 	sc->sc_hdev.sc_parent = uha->parent;
199 	sc->sc_hdev.sc_report_id = uha->reportid;
200 
201 	uhidev_get_report_desc(uha->parent, &desc, &size);
202 	repid = uha->reportid;
203 	sc->sc_ilen = hid_report_size(desc, size, hid_input, repid);
204 	sc->sc_olen = hid_report_size(desc, size, hid_output, repid);
205 	sc->sc_flen = hid_report_size(desc, size, hid_feature, repid);
206 
207 	DPRINTF(("ucycom_open: olen %d ilen %d flen %d\n", sc->sc_ilen,
208 	    sc->sc_olen, sc->sc_flen));
209 
210 	printf("\n");
211 
212 	sc->sc_udev = dev;
213 
214 	sc->sc_msr = sc->sc_mcr = 0;
215 
216 	err = uhidev_open(&sc->sc_hdev);
217 	if (err) {
218 		DPRINTF(("ucycom_open: uhidev_open %d\n", err));
219 		return;
220 	}
221 
222 	DPRINTF(("ucycom attach: sc %p opipe %p ipipe %p report_id %d\n",
223 	    sc, sc->sc_hdev.sc_parent->sc_opipe, sc->sc_hdev.sc_parent->sc_ipipe,
224 	    uha->reportid));
225 
226 	/* bulkin, bulkout set above */
227 	bzero(&uca, sizeof uca);
228 	uca.bulkin = uca.bulkout = -1;
229 	uca.uhidev = sc->sc_hdev.sc_parent;
230 	uca.ibufsize = sc->sc_ilen - 1;
231 	uca.obufsize = sc->sc_olen - 1;
232 	uca.ibufsizepad = 1;
233 	uca.opkthdrlen = 0;
234 	uca.device = uaa->device;
235 	uca.iface = uaa->iface;
236 	uca.methods = &ucycom_methods;
237 	uca.arg = sc;
238 	uca.info = NULL;
239 
240 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
241 	DPRINTF(("ucycom_attach: complete %p\n", sc->sc_subdev));
242 }
243 
244 void
245 ucycom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
246 {
247 	struct ucycom_softc *sc = addr;
248 
249 	DPRINTF(("ucycom_get_status:\n"));
250 
251 #if 0
252 	if (lsr != NULL)
253 		*lsr = sc->sc_lsr;
254 #endif
255 	if (msr != NULL)
256 		*msr = sc->sc_msr;
257 }
258 
259 int
260 ucycom_open(void *addr, int portno)
261 {
262 	struct ucycom_softc *sc = addr;
263 	struct termios t;
264 	int err;
265 
266 	DPRINTF(("ucycom_open: complete\n"));
267 
268 	if (sc->sc_dying)
269 		return (EIO);
270 
271 	/* Allocate an output report buffer */
272 	sc->sc_obuf = malloc(sc->sc_olen, M_USBDEV, M_WAITOK | M_ZERO);
273 
274 	/* Allocate an input report buffer */
275 	sc->sc_ibuf = malloc(sc->sc_ilen, M_USBDEV, M_WAITOK);
276 
277 	DPRINTF(("ucycom_open: sc->sc_ibuf=%p sc->sc_obuf=%p \n",
278 	    sc->sc_ibuf, sc->sc_obuf));
279 
280 	t.c_ospeed = 9600;
281 	t.c_cflag = CSTOPB | CS8;
282 	(void)ucycom_param(sc, portno, &t);
283 
284 	sc->sc_mcr = UCYCOM_DTR | UCYCOM_RTS;
285 	sc->sc_obuf[0] = sc->sc_mcr;
286 	err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf, sc->sc_olen);
287 	if (err) {
288 		DPRINTF(("ucycom_open: set RTS err=%d\n", err));
289 		return (EIO);
290 	}
291 
292 	return (0);
293 }
294 
295 void
296 ucycom_close(void *addr, int portno)
297 {
298 	struct ucycom_softc *sc = addr;
299 	int s;
300 
301 	if (sc->sc_dying)
302 		return;
303 
304 	s = splusb();
305 	if (sc->sc_obuf != NULL) {
306 		free(sc->sc_obuf, M_USBDEV);
307 		sc->sc_obuf = NULL;
308 	}
309 	if (sc->sc_ibuf != NULL) {
310 		free(sc->sc_ibuf, M_USBDEV);
311 		sc->sc_ibuf = NULL;
312 	}
313 	splx(s);
314 }
315 
316 void
317 ucycom_read(void *addr, int portno, u_char **ptr, u_int32_t *count)
318 {
319 	struct ucycom_softc *sc = addr;
320 
321 	if (sc->sc_newmsr ^ sc->sc_msr) {
322 		DPRINTF(("ucycom_read: msr %d new %d\n",
323 		    sc->sc_msr, sc->sc_newmsr));
324 		sc->sc_msr = sc->sc_newmsr;
325 		ucom_status_change((struct ucom_softc *)sc->sc_subdev);
326 	}
327 
328 	DPRINTF(("ucycom_read: buf %p chars %d\n", sc->sc_ibuf, sc->sc_icnt));
329 	*ptr = sc->sc_ibuf;
330 	*count = sc->sc_icnt;
331 }
332 
333 void
334 ucycom_write(void *addr, int portno, u_char *to, u_char *data, u_int32_t *cnt)
335 {
336 	struct ucycom_softc *sc = addr;
337 	u_int32_t len;
338 #ifdef UCYCOM_DEBUG
339 	u_int32_t want = *cnt;
340 #endif
341 
342 	/*
343 	 * The 8 byte output report uses byte 0 for control and byte
344 	 * count.
345 	 *
346 	 * The 32 byte output report uses byte 0 for control. Byte 1
347 	 * is used for byte count.
348 	 */
349 	len = sc->sc_olen;
350 	memset(to, 0, len);
351 	switch (sc->sc_olen) {
352 	case 8:
353 		to[0] = *cnt | sc->sc_mcr;
354 		memcpy(&to[1], data, *cnt);
355 		DPRINTF(("ucycomstart(8): to[0] = %d | %d = %d\n",
356 		    *cnt, sc->sc_mcr, to[0]));
357 		break;
358 
359 	case 32:
360 		to[0] = sc->sc_mcr;
361 		to[1] = *cnt;
362 		memcpy(&to[2], data, *cnt);
363 		DPRINTF(("ucycomstart(32): to[0] = %d\nto[1] = %d\n",
364 		    to[0], to[1]));
365 		break;
366 	}
367 
368 #ifdef UCYCOM_DEBUG
369 	if (ucycomdebug > 5) {
370 		int i;
371 
372 		if (len != 0) {
373 			DPRINTF(("ucycomstart: to[0..%d) =", len-1));
374 			for (i = 0; i < len; i++)
375 				DPRINTF((" %02x", to[i]));
376 			DPRINTF(("\n"));
377 		}
378 	}
379 #endif
380 	*cnt = len;
381 
382 #if 0
383 	ucycom_get_cfg(sc);
384 #endif
385 	DPRINTFN(4,("ucycomstart: req %d chars did %d chars\n", want, len));
386 }
387 
388 int
389 ucycom_param(void *addr, int portno, struct termios *t)
390 {
391 	struct ucycom_softc *sc = addr;
392 	uint8_t report[5];
393 	uint32_t baud = 0;
394 	uint8_t cfg;
395 	int err;
396 
397 	if (sc->sc_dying)
398 		return (EIO);
399 
400 	switch (t->c_ospeed) {
401 	case 600:
402 	case 1200:
403 	case 2400:
404 	case 4800:
405 	case 9600:
406 	case 19200:
407 	case 38400:
408 	case 57600:
409 #if 0
410 	/*
411 	 * Stock chips only support standard baud rates in the 600 - 57600
412 	 * range, but higher rates can be achieved using custom firmware.
413 	 */
414 	case 115200:
415 	case 153600:
416 	case 192000:
417 #endif
418 		baud = t->c_ospeed;
419 		break;
420 	default:
421 		return (EINVAL);
422 	}
423 
424 	if (t->c_cflag & CIGNORE) {
425 		cfg = sc->sc_cfg;
426 	} else {
427 		cfg = 0;
428 		switch (t->c_cflag & CSIZE) {
429 		case CS8:
430 			cfg |= UCYCOM_DATA_BITS_8;
431 			break;
432 		case CS7:
433 			cfg |= UCYCOM_DATA_BITS_7;
434 			break;
435 		case CS6:
436 			cfg |= UCYCOM_DATA_BITS_6;
437 			break;
438 		case CS5:
439 			cfg |= UCYCOM_DATA_BITS_5;
440 			break;
441 		default:
442 			return (EINVAL);
443 		}
444 		cfg |= ISSET(t->c_cflag, CSTOPB) ?
445 		    UCYCOM_STOP_BITS_2 : UCYCOM_STOP_BITS_1;
446 		cfg |= ISSET(t->c_cflag, PARENB) ?
447 		    UCYCOM_PARITY_ON : UCYCOM_PARITY_OFF;
448 		cfg |= ISSET(t->c_cflag, PARODD) ?
449 		    UCYCOM_PARITY_ODD : UCYCOM_PARITY_EVEN;
450 	}
451 
452 	DPRINTF(("ucycom_param: setting %d baud, %d-%c-%d (%d)\n", baud,
453 	    5 + (cfg & UCYCOM_DATA_MASK),
454 	    (cfg & UCYCOM_PARITY_MASK) ?
455 		((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N',
456 	    (cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg));
457 
458 	report[0] = baud & 0xff;
459 	report[1] = (baud >> 8) & 0xff;
460 	report[2] = (baud >> 16) & 0xff;
461 	report[3] = (baud >> 24) & 0xff;
462 	report[4] = cfg;
463 	err = uhidev_set_report(&sc->sc_hdev, UHID_FEATURE_REPORT,
464 	    report, sc->sc_flen);
465 	if (err != 0) {
466 		DPRINTF(("ucycom_param: uhidev_set_report %d %s\n",
467 		    err, usbd_errstr(err)));
468 		return EIO;
469 	}
470 	sc->sc_baud = baud;
471 	return (err);
472 }
473 
474 void
475 ucycom_intr(struct uhidev *addr, void *ibuf, u_int len)
476 {
477 	extern void ucomreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status);
478 	struct ucycom_softc *sc = (struct ucycom_softc *)addr;
479 	uint8_t *cp = ibuf;
480 	int n, st, s;
481 
482 	/* not accepting data anymore.. */
483 	if (sc->sc_ibuf == NULL)
484 		return;
485 
486 	/* We understand 8 byte and 32 byte input records */
487 	switch (len) {
488 	case 8:
489 		n = cp[0] & UCYCOM_LMASK;
490 		st = cp[0] & ~UCYCOM_LMASK;
491 		cp++;
492 		break;
493 
494 	case 32:
495 		st = cp[0];
496 		n = cp[1];
497 		cp += 2;
498 		break;
499 
500 	default:
501 		DPRINTFN(3,("ucycom_intr: Unknown input report length\n"));
502 		return;
503 	}
504 
505 #ifdef UCYCOM_DEBUG
506 	if (ucycomdebug > 5) {
507 		u_int32_t i;
508 
509 		if (n != 0) {
510 			DPRINTF(("ucycom_intr: ibuf[0..%d) =", n));
511 			for (i = 0; i < n; i++)
512 				DPRINTF((" %02x", cp[i]));
513 			DPRINTF(("\n"));
514 		}
515 	}
516 #endif
517 
518 	if (n > 0 || st != sc->sc_msr) {
519 		s = spltty();
520 		sc->sc_newmsr = st;
521 		bcopy(cp, sc->sc_ibuf, n);
522 		sc->sc_icnt = n;
523 		ucomreadcb(addr->sc_parent->sc_ixfer, sc->sc_subdev,
524 		    USBD_NORMAL_COMPLETION);
525 		splx(s);
526 	}
527 }
528 
529 void
530 ucycom_set(void *addr, int portno, int reg, int onoff)
531 {
532 	struct ucycom_softc *sc = addr;
533 	int err;
534 
535 	switch (reg) {
536 	case UCOM_SET_DTR:
537 		if (onoff)
538 			SET(sc->sc_mcr, UCYCOM_DTR);
539 		else
540 			CLR(sc->sc_mcr, UCYCOM_DTR);
541 		break;
542 	case UCOM_SET_RTS:
543 		if (onoff)
544 			SET(sc->sc_mcr, UCYCOM_RTS);
545 		else
546 			CLR(sc->sc_mcr, UCYCOM_RTS);
547 		break;
548 	case UCOM_SET_BREAK:
549 		break;
550 	}
551 
552 	memset(sc->sc_obuf, 0, sc->sc_olen);
553 	sc->sc_obuf[0] = sc->sc_mcr;
554 
555 	err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf, sc->sc_olen);
556 	if (err)
557 		DPRINTF(("ucycom_set_status: err=%d\n", err));
558 }
559 
560 void
561 ucycom_get_cfg(struct ucycom_softc *sc)
562 {
563 	int err, cfg, baud;
564 	uint8_t report[5];
565 
566 	err = uhidev_get_report(&sc->sc_hdev, UHID_FEATURE_REPORT,
567 	    report, sc->sc_flen);
568 	cfg = report[4];
569 	baud = (report[3] << 24) + (report[2] << 16) + (report[1] << 8) + report[0];
570 	DPRINTF(("ucycom_configure: device reports %d baud, %d-%c-%d (%d)\n", baud,
571 	    5 + (cfg & UCYCOM_DATA_MASK),
572 	    (cfg & UCYCOM_PARITY_MASK) ?
573 		((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N',
574 	    (cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg));
575 }
576 
577 int
578 ucycom_detach(struct device *self, int flags)
579 {
580 	struct ucycom_softc *sc = (struct ucycom_softc *)self;
581 
582 	DPRINTF(("ucycom_detach: sc=%p flags=%d\n", sc, flags));
583 	if (sc->sc_subdev != NULL) {
584 		config_detach(sc->sc_subdev, flags);
585 		sc->sc_subdev = NULL;
586 	}
587 	return (0);
588 }
589 
590 int
591 ucycom_activate(struct device *self, int act)
592 {
593 	struct ucycom_softc *sc = (struct ucycom_softc *)self;
594 	int rv = 0;
595 
596 	DPRINTFN(5,("ucycom_activate: %d\n", act));
597 
598 	switch (act) {
599 	case DVACT_DEACTIVATE:
600 		if (sc->sc_subdev != NULL)
601 			rv = config_deactivate(sc->sc_subdev);
602 		sc->sc_dying = 1;
603 		break;
604 	}
605 	return (rv);
606 }
607