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