xref: /openbsd-src/sys/dev/usb/ucycom.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: ucycom.c,v 1.29 2014/07/12 20:26:33 mpi 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 
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbhid.h>
52 
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usbdi_util.h>
55 #include <dev/usb/usbdevs.h>
56 #include <dev/usb/uhidev.h>
57 #include <dev/usb/hid.h>
58 
59 #include <dev/usb/ucomvar.h>
60 
61 #ifdef UCYCOM_DEBUG
62 #define DPRINTF(x)	if (ucycomdebug) printf x
63 #define DPRINTFN(n, x)	if (ucycomdebug > (n)) printf x
64 int	ucycomdebug = 200;
65 #else
66 #define DPRINTF(x)
67 #define DPRINTFN(n,x)
68 #endif
69 
70 /* Configuration Byte */
71 #define UCYCOM_RESET		0x80
72 #define UCYCOM_PARITY_TYPE_MASK	0x20
73 #define  UCYCOM_PARITY_ODD	 0x20
74 #define  UCYCOM_PARITY_EVEN	 0x00
75 #define UCYCOM_PARITY_MASK	0x10
76 #define  UCYCOM_PARITY_ON	 0x10
77 #define  UCYCOM_PARITY_OFF	 0x00
78 #define UCYCOM_STOP_MASK	0x08
79 #define  UCYCOM_STOP_BITS_2	 0x08
80 #define  UCYCOM_STOP_BITS_1	 0x00
81 #define UCYCOM_DATA_MASK	0x03
82 #define  UCYCOM_DATA_BITS_8	 0x03
83 #define  UCYCOM_DATA_BITS_7	 0x02
84 #define  UCYCOM_DATA_BITS_6	 0x01
85 #define  UCYCOM_DATA_BITS_5	 0x00
86 
87 /* Modem (Input) status byte */
88 #define UCYCOM_RI	0x80
89 #define UCYCOM_DCD	0x40
90 #define UCYCOM_DSR	0x20
91 #define UCYCOM_CTS	0x10
92 #define UCYCOM_ERROR	0x08
93 #define UCYCOM_LMASK	0x07
94 
95 /* Modem (Output) control byte */
96 #define UCYCOM_DTR	0x20
97 #define UCYCOM_RTS	0x10
98 #define UCYCOM_ORESET	0x08
99 
100 struct ucycom_softc {
101 	struct uhidev		 sc_hdev;
102 	struct usbd_device	*sc_udev;
103 
104 	/* uhidev parameters */
105 	size_t			 sc_flen;	/* feature report length */
106 	size_t			 sc_ilen;	/* input report length */
107 	size_t			 sc_olen;	/* output report length */
108 
109 	uint8_t			*sc_obuf;
110 
111 	uint8_t			*sc_ibuf;
112 	uint32_t		 sc_icnt;
113 
114 	/* settings */
115 	uint32_t		 sc_baud;
116 	uint8_t			 sc_cfg;	/* Data format */
117 	uint8_t			 sc_mcr;	/* Modem control */
118 	uint8_t			 sc_msr;	/* Modem status */
119 	uint8_t			 sc_newmsr;	/* from HID intr */
120 	int			 sc_swflags;
121 
122 	struct device		*sc_subdev;
123 };
124 
125 /* Callback routines */
126 void	ucycom_set(void *, int, int, int);
127 int	ucycom_param(void *, int, struct termios *);
128 void	ucycom_get_status(void *, int, u_char *, u_char *);
129 int	ucycom_open(void *, int);
130 void	ucycom_close(void *, int);
131 void	ucycom_write(void *, int, u_char *, u_char *, u_int32_t *);
132 void	ucycom_read(void *, int, u_char **, u_int32_t *);
133 
134 struct ucom_methods ucycom_methods = {
135 	NULL, /* ucycom_get_status, */
136 	ucycom_set,
137 	ucycom_param,
138 	NULL,
139 	ucycom_open,
140 	ucycom_close,
141 	ucycom_read,
142 	ucycom_write,
143 };
144 
145 void ucycom_intr(struct uhidev *, void *, u_int);
146 
147 void ucycom_get_cfg(struct ucycom_softc *);
148 
149 const struct usb_devno ucycom_devs[] = {
150 	{ USB_VENDOR_CYPRESS, USB_PRODUCT_CYPRESS_USBRS232 },
151 	{ USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EMUSB },
152 	{ USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EMLT20 },
153 };
154 
155 int ucycom_match(struct device *, void *, void *);
156 void ucycom_attach(struct device *, struct device *, void *);
157 int ucycom_detach(struct device *, int);
158 
159 struct cfdriver ucycom_cd = {
160 	NULL, "ucycom", DV_DULL
161 };
162 
163 const struct cfattach ucycom_ca = {
164 	sizeof(struct ucycom_softc), ucycom_match, ucycom_attach, ucycom_detach
165 };
166 
167 int
168 ucycom_match(struct device *parent, void *match, void *aux)
169 {
170 	struct uhidev_attach_arg *uha = aux;
171 
172 	if (uha->reportid == UHIDEV_CLAIM_ALLREPORTID)
173 		return (UMATCH_NONE);
174 
175 	return (usb_lookup(ucycom_devs, uha->uaa->vendor, uha->uaa->product) != NULL ?
176 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
177 }
178 
179 void
180 ucycom_attach(struct device *parent, struct device *self, void *aux)
181 {
182 	struct ucycom_softc *sc = (struct ucycom_softc *)self;
183 	struct usb_attach_arg *uaa = aux;
184 	struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa;
185 	struct usbd_device *dev = uha->parent->sc_udev;
186 	struct ucom_attach_args uca;
187 	int size, repid, err;
188 	void *desc;
189 
190 	sc->sc_hdev.sc_intr = ucycom_intr;
191 	sc->sc_hdev.sc_parent = uha->parent;
192 	sc->sc_hdev.sc_report_id = uha->reportid;
193 
194 	uhidev_get_report_desc(uha->parent, &desc, &size);
195 	repid = uha->reportid;
196 	sc->sc_ilen = hid_report_size(desc, size, hid_input, repid);
197 	sc->sc_olen = hid_report_size(desc, size, hid_output, repid);
198 	sc->sc_flen = hid_report_size(desc, size, hid_feature, repid);
199 
200 	DPRINTF(("ucycom_open: olen %d ilen %d flen %d\n", sc->sc_ilen,
201 	    sc->sc_olen, sc->sc_flen));
202 
203 	printf("\n");
204 
205 	sc->sc_udev = dev;
206 
207 	sc->sc_msr = sc->sc_mcr = 0;
208 
209 	err = uhidev_open(&sc->sc_hdev);
210 	if (err) {
211 		DPRINTF(("ucycom_open: uhidev_open %d\n", err));
212 		return;
213 	}
214 
215 	DPRINTF(("ucycom attach: sc %p opipe %p ipipe %p report_id %d\n",
216 	    sc, sc->sc_hdev.sc_parent->sc_opipe, sc->sc_hdev.sc_parent->sc_ipipe,
217 	    uha->reportid));
218 
219 	/* bulkin, bulkout set above */
220 	bzero(&uca, sizeof uca);
221 	uca.bulkin = uca.bulkout = -1;
222 	uca.uhidev = sc->sc_hdev.sc_parent;
223 	uca.ibufsize = sc->sc_ilen - 1;
224 	uca.obufsize = sc->sc_olen - 1;
225 	uca.ibufsizepad = 1;
226 	uca.opkthdrlen = 0;
227 	uca.device = uaa->device;
228 	uca.iface = uaa->iface;
229 	uca.methods = &ucycom_methods;
230 	uca.arg = sc;
231 	uca.info = NULL;
232 
233 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
234 	DPRINTF(("ucycom_attach: complete %p\n", sc->sc_subdev));
235 }
236 
237 void
238 ucycom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
239 {
240 	struct ucycom_softc *sc = addr;
241 
242 	DPRINTF(("ucycom_get_status:\n"));
243 
244 #if 0
245 	if (lsr != NULL)
246 		*lsr = sc->sc_lsr;
247 #endif
248 	if (msr != NULL)
249 		*msr = sc->sc_msr;
250 }
251 
252 int
253 ucycom_open(void *addr, int portno)
254 {
255 	struct ucycom_softc *sc = addr;
256 	struct termios t;
257 	int err;
258 
259 	DPRINTF(("ucycom_open: complete\n"));
260 
261 	if (usbd_is_dying(sc->sc_udev))
262 		return (EIO);
263 
264 	/* Allocate an output report buffer */
265 	sc->sc_obuf = malloc(sc->sc_olen, M_USBDEV, M_WAITOK | M_ZERO);
266 
267 	/* Allocate an input report buffer */
268 	sc->sc_ibuf = malloc(sc->sc_ilen, M_USBDEV, M_WAITOK);
269 
270 	DPRINTF(("ucycom_open: sc->sc_ibuf=%p sc->sc_obuf=%p \n",
271 	    sc->sc_ibuf, sc->sc_obuf));
272 
273 	t.c_ospeed = 9600;
274 	t.c_cflag = CSTOPB | CS8;
275 	(void)ucycom_param(sc, portno, &t);
276 
277 	sc->sc_mcr = UCYCOM_DTR | UCYCOM_RTS;
278 	sc->sc_obuf[0] = sc->sc_mcr;
279 	err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf, sc->sc_olen);
280 	if (err) {
281 		DPRINTF(("ucycom_open: set RTS err=%d\n", err));
282 		return (EIO);
283 	}
284 
285 	return (0);
286 }
287 
288 void
289 ucycom_close(void *addr, int portno)
290 {
291 	struct ucycom_softc *sc = addr;
292 	int s;
293 
294 	if (usbd_is_dying(sc->sc_udev))
295 		return;
296 
297 	s = splusb();
298 	if (sc->sc_obuf != NULL) {
299 		free(sc->sc_obuf, M_USBDEV, 0);
300 		sc->sc_obuf = NULL;
301 	}
302 	if (sc->sc_ibuf != NULL) {
303 		free(sc->sc_ibuf, M_USBDEV, 0);
304 		sc->sc_ibuf = NULL;
305 	}
306 	splx(s);
307 }
308 
309 void
310 ucycom_read(void *addr, int portno, u_char **ptr, u_int32_t *count)
311 {
312 	struct ucycom_softc *sc = addr;
313 
314 	if (sc->sc_newmsr ^ sc->sc_msr) {
315 		DPRINTF(("ucycom_read: msr %d new %d\n",
316 		    sc->sc_msr, sc->sc_newmsr));
317 		sc->sc_msr = sc->sc_newmsr;
318 		ucom_status_change((struct ucom_softc *)sc->sc_subdev);
319 	}
320 
321 	DPRINTF(("ucycom_read: buf %p chars %d\n", sc->sc_ibuf, sc->sc_icnt));
322 	*ptr = sc->sc_ibuf;
323 	*count = sc->sc_icnt;
324 }
325 
326 void
327 ucycom_write(void *addr, int portno, u_char *to, u_char *data, u_int32_t *cnt)
328 {
329 	struct ucycom_softc *sc = addr;
330 	u_int32_t len;
331 #ifdef UCYCOM_DEBUG
332 	u_int32_t want = *cnt;
333 #endif
334 
335 	/*
336 	 * The 8 byte output report uses byte 0 for control and byte
337 	 * count.
338 	 *
339 	 * The 32 byte output report uses byte 0 for control. Byte 1
340 	 * is used for byte count.
341 	 */
342 	len = sc->sc_olen;
343 	memset(to, 0, len);
344 	switch (sc->sc_olen) {
345 	case 8:
346 		to[0] = *cnt | sc->sc_mcr;
347 		memcpy(&to[1], data, *cnt);
348 		DPRINTF(("ucycomstart(8): to[0] = %d | %d = %d\n",
349 		    *cnt, sc->sc_mcr, to[0]));
350 		break;
351 
352 	case 32:
353 		to[0] = sc->sc_mcr;
354 		to[1] = *cnt;
355 		memcpy(&to[2], data, *cnt);
356 		DPRINTF(("ucycomstart(32): to[0] = %d\nto[1] = %d\n",
357 		    to[0], to[1]));
358 		break;
359 	}
360 
361 #ifdef UCYCOM_DEBUG
362 	if (ucycomdebug > 5) {
363 		int i;
364 
365 		if (len != 0) {
366 			DPRINTF(("ucycomstart: to[0..%d) =", len-1));
367 			for (i = 0; i < len; i++)
368 				DPRINTF((" %02x", to[i]));
369 			DPRINTF(("\n"));
370 		}
371 	}
372 #endif
373 	*cnt = len;
374 
375 #if 0
376 	ucycom_get_cfg(sc);
377 #endif
378 	DPRINTFN(4,("ucycomstart: req %d chars did %d chars\n", want, len));
379 }
380 
381 int
382 ucycom_param(void *addr, int portno, struct termios *t)
383 {
384 	struct ucycom_softc *sc = addr;
385 	uint8_t report[5];
386 	uint32_t baud = 0;
387 	uint8_t cfg;
388 	int err;
389 
390 	if (usbd_is_dying(sc->sc_udev))
391 		return (EIO);
392 
393 	switch (t->c_ospeed) {
394 	case 600:
395 	case 1200:
396 	case 2400:
397 	case 4800:
398 	case 9600:
399 	case 19200:
400 	case 38400:
401 	case 57600:
402 #if 0
403 	/*
404 	 * Stock chips only support standard baud rates in the 600 - 57600
405 	 * range, but higher rates can be achieved using custom firmware.
406 	 */
407 	case 115200:
408 	case 153600:
409 	case 192000:
410 #endif
411 		baud = t->c_ospeed;
412 		break;
413 	default:
414 		return (EINVAL);
415 	}
416 
417 	if (t->c_cflag & CIGNORE) {
418 		cfg = sc->sc_cfg;
419 	} else {
420 		cfg = 0;
421 		switch (t->c_cflag & CSIZE) {
422 		case CS8:
423 			cfg |= UCYCOM_DATA_BITS_8;
424 			break;
425 		case CS7:
426 			cfg |= UCYCOM_DATA_BITS_7;
427 			break;
428 		case CS6:
429 			cfg |= UCYCOM_DATA_BITS_6;
430 			break;
431 		case CS5:
432 			cfg |= UCYCOM_DATA_BITS_5;
433 			break;
434 		default:
435 			return (EINVAL);
436 		}
437 		cfg |= ISSET(t->c_cflag, CSTOPB) ?
438 		    UCYCOM_STOP_BITS_2 : UCYCOM_STOP_BITS_1;
439 		cfg |= ISSET(t->c_cflag, PARENB) ?
440 		    UCYCOM_PARITY_ON : UCYCOM_PARITY_OFF;
441 		cfg |= ISSET(t->c_cflag, PARODD) ?
442 		    UCYCOM_PARITY_ODD : UCYCOM_PARITY_EVEN;
443 	}
444 
445 	DPRINTF(("ucycom_param: setting %d baud, %d-%c-%d (%d)\n", baud,
446 	    5 + (cfg & UCYCOM_DATA_MASK),
447 	    (cfg & UCYCOM_PARITY_MASK) ?
448 		((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N',
449 	    (cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg));
450 
451 	report[0] = baud & 0xff;
452 	report[1] = (baud >> 8) & 0xff;
453 	report[2] = (baud >> 16) & 0xff;
454 	report[3] = (baud >> 24) & 0xff;
455 	report[4] = cfg;
456 	err = uhidev_set_report(&sc->sc_hdev, UHID_FEATURE_REPORT,
457 	    sc->sc_hdev.sc_report_id, report, sc->sc_flen);
458 	if (err != 0) {
459 		DPRINTF(("ucycom_param: uhidev_set_report %d %s\n",
460 		    err, usbd_errstr(err)));
461 		return EIO;
462 	}
463 	sc->sc_baud = baud;
464 	return (err);
465 }
466 
467 void
468 ucycom_intr(struct uhidev *addr, void *ibuf, u_int len)
469 {
470 	extern void ucomreadcb(struct usbd_xfer *, void *, usbd_status);
471 	struct ucycom_softc *sc = (struct ucycom_softc *)addr;
472 	uint8_t *cp = ibuf;
473 	int n, st, s;
474 
475 	/* not accepting data anymore.. */
476 	if (sc->sc_ibuf == NULL)
477 		return;
478 
479 	/* We understand 8 byte and 32 byte input records */
480 	switch (len) {
481 	case 8:
482 		n = cp[0] & UCYCOM_LMASK;
483 		st = cp[0] & ~UCYCOM_LMASK;
484 		cp++;
485 		break;
486 
487 	case 32:
488 		st = cp[0];
489 		n = cp[1];
490 		cp += 2;
491 		break;
492 
493 	default:
494 		DPRINTFN(3,("ucycom_intr: Unknown input report length\n"));
495 		return;
496 	}
497 
498 #ifdef UCYCOM_DEBUG
499 	if (ucycomdebug > 5) {
500 		u_int32_t i;
501 
502 		if (n != 0) {
503 			DPRINTF(("ucycom_intr: ibuf[0..%d) =", n));
504 			for (i = 0; i < n; i++)
505 				DPRINTF((" %02x", cp[i]));
506 			DPRINTF(("\n"));
507 		}
508 	}
509 #endif
510 
511 	if (n > 0 || st != sc->sc_msr) {
512 		s = spltty();
513 		sc->sc_newmsr = st;
514 		bcopy(cp, sc->sc_ibuf, n);
515 		sc->sc_icnt = n;
516 		ucomreadcb(addr->sc_parent->sc_ixfer, sc->sc_subdev,
517 		    USBD_NORMAL_COMPLETION);
518 		splx(s);
519 	}
520 }
521 
522 void
523 ucycom_set(void *addr, int portno, int reg, int onoff)
524 {
525 	struct ucycom_softc *sc = addr;
526 	int err;
527 
528 	switch (reg) {
529 	case UCOM_SET_DTR:
530 		if (onoff)
531 			SET(sc->sc_mcr, UCYCOM_DTR);
532 		else
533 			CLR(sc->sc_mcr, UCYCOM_DTR);
534 		break;
535 	case UCOM_SET_RTS:
536 		if (onoff)
537 			SET(sc->sc_mcr, UCYCOM_RTS);
538 		else
539 			CLR(sc->sc_mcr, UCYCOM_RTS);
540 		break;
541 	case UCOM_SET_BREAK:
542 		break;
543 	}
544 
545 	memset(sc->sc_obuf, 0, sc->sc_olen);
546 	sc->sc_obuf[0] = sc->sc_mcr;
547 
548 	err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf, sc->sc_olen);
549 	if (err)
550 		DPRINTF(("ucycom_set_status: err=%d\n", err));
551 }
552 
553 void
554 ucycom_get_cfg(struct ucycom_softc *sc)
555 {
556 	int err, cfg, baud;
557 	uint8_t report[5];
558 
559 	err = uhidev_get_report(&sc->sc_hdev, UHID_FEATURE_REPORT,
560 	    sc->sc_hdev.sc_report_id, report, sc->sc_flen);
561 	cfg = report[4];
562 	baud = (report[3] << 24) + (report[2] << 16) + (report[1] << 8) + report[0];
563 	DPRINTF(("ucycom_configure: device reports %d baud, %d-%c-%d (%d)\n", baud,
564 	    5 + (cfg & UCYCOM_DATA_MASK),
565 	    (cfg & UCYCOM_PARITY_MASK) ?
566 		((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N',
567 	    (cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg));
568 }
569 
570 int
571 ucycom_detach(struct device *self, int flags)
572 {
573 	struct ucycom_softc *sc = (struct ucycom_softc *)self;
574 
575 	DPRINTF(("ucycom_detach: sc=%p flags=%d\n", sc, flags));
576 	if (sc->sc_subdev != NULL) {
577 		config_detach(sc->sc_subdev, flags);
578 		sc->sc_subdev = NULL;
579 	}
580 
581 	if (sc->sc_hdev.sc_state & UHIDEV_OPEN)
582 		uhidev_close(&sc->sc_hdev);
583 
584 	return (0);
585 }
586