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