xref: /freebsd/sys/dev/usb/serial/ucycom.c (revision ca48e43ba9ee73a07cdbad8365117793b01273bb)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2004 Dag-Erling Smørgrav
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /*
32  * Device driver for Cypress CY7C637xx and CY7C640/1xx series USB to
33  * RS232 bridges.
34  */
35 
36 #include <sys/stdint.h>
37 #include <sys/stddef.h>
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/types.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/bus.h>
44 #include <sys/module.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/condvar.h>
48 #include <sys/sysctl.h>
49 #include <sys/sx.h>
50 #include <sys/unistd.h>
51 #include <sys/callout.h>
52 #include <sys/malloc.h>
53 #include <sys/priv.h>
54 
55 #include <dev/hid/hid.h>
56 
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
60 #include <dev/usb/usbhid.h>
61 #include "usbdevs.h"
62 
63 #define	USB_DEBUG_VAR usb_debug
64 #include <dev/usb/usb_debug.h>
65 #include <dev/usb/usb_process.h>
66 
67 #include <dev/usb/serial/usb_serial.h>
68 
69 #define	UCYCOM_MAX_IOLEN	(1024 + 2)	/* bytes */
70 
71 #define	UCYCOM_IFACE_INDEX	0
72 
73 enum {
74 	UCYCOM_CTRL_RD,
75 	UCYCOM_INTR_RD,
76 	UCYCOM_N_TRANSFER,
77 };
78 
79 struct ucycom_softc {
80 	struct ucom_super_softc sc_super_ucom;
81 	struct ucom_softc sc_ucom;
82 
83 	struct usb_device *sc_udev;
84 	struct usb_xfer *sc_xfer[UCYCOM_N_TRANSFER];
85 	struct mtx sc_mtx;
86 
87 	uint32_t sc_model;
88 #define	MODEL_CY7C63743		0x63743
89 #define	MODEL_CY7C64013		0x64013
90 
91 	uint16_t sc_flen;		/* feature report length */
92 	uint16_t sc_ilen;		/* input report length */
93 	uint16_t sc_olen;		/* output report length */
94 
95 	uint8_t	sc_fid;			/* feature report id */
96 	uint8_t	sc_iid;			/* input report id */
97 	uint8_t	sc_oid;			/* output report id */
98 	uint8_t	sc_cfg;
99 #define	UCYCOM_CFG_RESET	0x80
100 #define	UCYCOM_CFG_PARODD	0x20
101 #define	UCYCOM_CFG_PAREN	0x10
102 #define	UCYCOM_CFG_STOPB	0x08
103 #define	UCYCOM_CFG_DATAB	0x03
104 	uint8_t	sc_ist;			/* status flags from last input */
105 	uint8_t	sc_iface_no;
106 	uint8_t	sc_temp_cfg[32];
107 };
108 
109 /* prototypes */
110 
111 static device_probe_t ucycom_probe;
112 static device_attach_t ucycom_attach;
113 static device_detach_t ucycom_detach;
114 static void ucycom_free_softc(struct ucycom_softc *);
115 
116 static usb_callback_t ucycom_ctrl_write_callback;
117 static usb_callback_t ucycom_intr_read_callback;
118 
119 static void	ucycom_free(struct ucom_softc *);
120 static void	ucycom_cfg_open(struct ucom_softc *);
121 static void	ucycom_start_read(struct ucom_softc *);
122 static void	ucycom_stop_read(struct ucom_softc *);
123 static void	ucycom_start_write(struct ucom_softc *);
124 static void	ucycom_stop_write(struct ucom_softc *);
125 static void	ucycom_cfg_write(struct ucycom_softc *, uint32_t, uint8_t);
126 static int	ucycom_pre_param(struct ucom_softc *, struct termios *);
127 static void	ucycom_cfg_param(struct ucom_softc *, struct termios *);
128 static void	ucycom_poll(struct ucom_softc *ucom);
129 
130 static const struct usb_config ucycom_config[UCYCOM_N_TRANSFER] = {
131 	[UCYCOM_CTRL_RD] = {
132 		.type = UE_CONTROL,
133 		.endpoint = 0x00,	/* Control pipe */
134 		.direction = UE_DIR_ANY,
135 		.bufsize = (sizeof(struct usb_device_request) + UCYCOM_MAX_IOLEN),
136 		.callback = &ucycom_ctrl_write_callback,
137 		.timeout = 1000,	/* 1 second */
138 	},
139 
140 	[UCYCOM_INTR_RD] = {
141 		.type = UE_INTERRUPT,
142 		.endpoint = UE_ADDR_ANY,
143 		.direction = UE_DIR_IN,
144 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
145 		.bufsize = UCYCOM_MAX_IOLEN,
146 		.callback = &ucycom_intr_read_callback,
147 	},
148 };
149 
150 static const struct ucom_callback ucycom_callback = {
151 	.ucom_cfg_param = &ucycom_cfg_param,
152 	.ucom_cfg_open = &ucycom_cfg_open,
153 	.ucom_pre_param = &ucycom_pre_param,
154 	.ucom_start_read = &ucycom_start_read,
155 	.ucom_stop_read = &ucycom_stop_read,
156 	.ucom_start_write = &ucycom_start_write,
157 	.ucom_stop_write = &ucycom_stop_write,
158 	.ucom_poll = &ucycom_poll,
159 	.ucom_free = &ucycom_free,
160 };
161 
162 static device_method_t ucycom_methods[] = {
163 	DEVMETHOD(device_probe, ucycom_probe),
164 	DEVMETHOD(device_attach, ucycom_attach),
165 	DEVMETHOD(device_detach, ucycom_detach),
166 	DEVMETHOD_END
167 };
168 
169 static driver_t ucycom_driver = {
170 	.name = "ucycom",
171 	.methods = ucycom_methods,
172 	.size = sizeof(struct ucycom_softc),
173 };
174 
175 /*
176  * Supported devices
177  */
178 static const STRUCT_USB_HOST_ID ucycom_devs[] = {
179 	{USB_VPI(USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EARTHMATE, MODEL_CY7C64013)},
180 };
181 
182 DRIVER_MODULE(ucycom, uhub, ucycom_driver, NULL, NULL);
183 MODULE_DEPEND(ucycom, ucom, 1, 1, 1);
184 MODULE_DEPEND(ucycom, usb, 1, 1, 1);
185 MODULE_DEPEND(ucycom, hid, 1, 1, 1);
186 MODULE_VERSION(ucycom, 1);
187 USB_PNP_HOST_INFO(ucycom_devs);
188 
189 #define	UCYCOM_DEFAULT_RATE	 4800
190 #define	UCYCOM_DEFAULT_CFG	 0x03	/* N-8-1 */
191 
192 static int
ucycom_probe(device_t dev)193 ucycom_probe(device_t dev)
194 {
195 	struct usb_attach_arg *uaa = device_get_ivars(dev);
196 
197 	if (uaa->usb_mode != USB_MODE_HOST) {
198 		return (ENXIO);
199 	}
200 	if (uaa->info.bConfigIndex != 0) {
201 		return (ENXIO);
202 	}
203 	if (uaa->info.bIfaceIndex != UCYCOM_IFACE_INDEX) {
204 		return (ENXIO);
205 	}
206 	return (usbd_lookup_id_by_uaa(ucycom_devs, sizeof(ucycom_devs), uaa));
207 }
208 
209 static int
ucycom_attach(device_t dev)210 ucycom_attach(device_t dev)
211 {
212 	struct usb_attach_arg *uaa = device_get_ivars(dev);
213 	struct ucycom_softc *sc = device_get_softc(dev);
214 	void *urd_ptr = NULL;
215 	int32_t error;
216 	uint16_t urd_len;
217 	uint8_t iface_index;
218 
219 	sc->sc_udev = uaa->device;
220 
221 	device_set_usb_desc(dev);
222 	mtx_init(&sc->sc_mtx, "ucycom", NULL, MTX_DEF);
223 	ucom_ref(&sc->sc_super_ucom);
224 
225 	DPRINTF("\n");
226 
227 	/* get chip model */
228 	sc->sc_model = USB_GET_DRIVER_INFO(uaa);
229 	if (sc->sc_model == 0) {
230 		device_printf(dev, "unsupported device\n");
231 		goto detach;
232 	}
233 	device_printf(dev, "Cypress CY7C%X USB to RS232 bridge\n", sc->sc_model);
234 
235 	/* get report descriptor */
236 
237 	error = usbd_req_get_hid_desc(uaa->device, NULL,
238 	    &urd_ptr, &urd_len, M_USBDEV,
239 	    UCYCOM_IFACE_INDEX);
240 
241 	if (error) {
242 		device_printf(dev, "failed to get report "
243 		    "descriptor: %s\n",
244 		    usbd_errstr(error));
245 		goto detach;
246 	}
247 	/* get report sizes */
248 
249 	sc->sc_flen = hid_report_size_max(urd_ptr, urd_len, hid_feature, &sc->sc_fid);
250 	sc->sc_ilen = hid_report_size_max(urd_ptr, urd_len, hid_input, &sc->sc_iid);
251 	sc->sc_olen = hid_report_size_max(urd_ptr, urd_len, hid_output, &sc->sc_oid);
252 
253 	if ((sc->sc_ilen > UCYCOM_MAX_IOLEN) || (sc->sc_ilen < 1) ||
254 	    (sc->sc_olen > UCYCOM_MAX_IOLEN) || (sc->sc_olen < 2) ||
255 	    (sc->sc_flen > UCYCOM_MAX_IOLEN) || (sc->sc_flen < 5)) {
256 		device_printf(dev, "invalid report size i=%d, o=%d, f=%d, max=%d\n",
257 		    sc->sc_ilen, sc->sc_olen, sc->sc_flen,
258 		    UCYCOM_MAX_IOLEN);
259 		goto detach;
260 	}
261 	sc->sc_iface_no = uaa->info.bIfaceNum;
262 
263 	iface_index = UCYCOM_IFACE_INDEX;
264 	error = usbd_transfer_setup(uaa->device, &iface_index,
265 	    sc->sc_xfer, ucycom_config, UCYCOM_N_TRANSFER,
266 	    sc, &sc->sc_mtx);
267 	if (error) {
268 		device_printf(dev, "allocating USB "
269 		    "transfers failed\n");
270 		goto detach;
271 	}
272 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
273 	    &ucycom_callback, &sc->sc_mtx);
274 	if (error) {
275 		goto detach;
276 	}
277 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
278 
279 	if (urd_ptr) {
280 		free(urd_ptr, M_USBDEV);
281 	}
282 
283 	return (0);			/* success */
284 
285 detach:
286 	if (urd_ptr) {
287 		free(urd_ptr, M_USBDEV);
288 	}
289 	ucycom_detach(dev);
290 	return (ENXIO);
291 }
292 
293 static int
ucycom_detach(device_t dev)294 ucycom_detach(device_t dev)
295 {
296 	struct ucycom_softc *sc = device_get_softc(dev);
297 
298 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
299 	usbd_transfer_unsetup(sc->sc_xfer, UCYCOM_N_TRANSFER);
300 
301 	device_claim_softc(dev);
302 
303 	ucycom_free_softc(sc);
304 
305 	return (0);
306 }
307 
308 UCOM_UNLOAD_DRAIN(ucycom);
309 
310 static void
ucycom_free_softc(struct ucycom_softc * sc)311 ucycom_free_softc(struct ucycom_softc *sc)
312 {
313 	if (ucom_unref(&sc->sc_super_ucom)) {
314 		mtx_destroy(&sc->sc_mtx);
315 		device_free_softc(sc);
316 	}
317 }
318 
319 static void
ucycom_free(struct ucom_softc * ucom)320 ucycom_free(struct ucom_softc *ucom)
321 {
322 	ucycom_free_softc(ucom->sc_parent);
323 }
324 
325 static void
ucycom_cfg_open(struct ucom_softc * ucom)326 ucycom_cfg_open(struct ucom_softc *ucom)
327 {
328 	struct ucycom_softc *sc = ucom->sc_parent;
329 
330 	/* set default configuration */
331 	ucycom_cfg_write(sc, UCYCOM_DEFAULT_RATE, UCYCOM_DEFAULT_CFG);
332 }
333 
334 static void
ucycom_start_read(struct ucom_softc * ucom)335 ucycom_start_read(struct ucom_softc *ucom)
336 {
337 	struct ucycom_softc *sc = ucom->sc_parent;
338 
339 	usbd_transfer_start(sc->sc_xfer[UCYCOM_INTR_RD]);
340 }
341 
342 static void
ucycom_stop_read(struct ucom_softc * ucom)343 ucycom_stop_read(struct ucom_softc *ucom)
344 {
345 	struct ucycom_softc *sc = ucom->sc_parent;
346 
347 	usbd_transfer_stop(sc->sc_xfer[UCYCOM_INTR_RD]);
348 }
349 
350 static void
ucycom_start_write(struct ucom_softc * ucom)351 ucycom_start_write(struct ucom_softc *ucom)
352 {
353 	struct ucycom_softc *sc = ucom->sc_parent;
354 
355 	usbd_transfer_start(sc->sc_xfer[UCYCOM_CTRL_RD]);
356 }
357 
358 static void
ucycom_stop_write(struct ucom_softc * ucom)359 ucycom_stop_write(struct ucom_softc *ucom)
360 {
361 	struct ucycom_softc *sc = ucom->sc_parent;
362 
363 	usbd_transfer_stop(sc->sc_xfer[UCYCOM_CTRL_RD]);
364 }
365 
366 static void
ucycom_ctrl_write_callback(struct usb_xfer * xfer,usb_error_t error)367 ucycom_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error)
368 {
369 	struct ucycom_softc *sc = usbd_xfer_softc(xfer);
370 	struct usb_device_request req;
371 	struct usb_page_cache *pc0, *pc1;
372 	uint8_t data[2];
373 	uint8_t offset;
374 	uint32_t actlen;
375 
376 	pc0 = usbd_xfer_get_frame(xfer, 0);
377 	pc1 = usbd_xfer_get_frame(xfer, 1);
378 
379 	switch (USB_GET_STATE(xfer)) {
380 	case USB_ST_TRANSFERRED:
381 tr_transferred:
382 	case USB_ST_SETUP:
383 
384 		switch (sc->sc_model) {
385 		case MODEL_CY7C63743:
386 			offset = 1;
387 			break;
388 		case MODEL_CY7C64013:
389 			offset = 2;
390 			break;
391 		default:
392 			offset = 0;
393 			break;
394 		}
395 
396 		if (ucom_get_data(&sc->sc_ucom, pc1, offset,
397 		    sc->sc_olen - offset, &actlen)) {
398 			req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
399 			req.bRequest = UR_SET_REPORT;
400 			USETW2(req.wValue, UHID_OUTPUT_REPORT, sc->sc_oid);
401 			req.wIndex[0] = sc->sc_iface_no;
402 			req.wIndex[1] = 0;
403 			USETW(req.wLength, sc->sc_olen);
404 
405 			switch (sc->sc_model) {
406 			case MODEL_CY7C63743:
407 				data[0] = actlen;
408 				break;
409 			case MODEL_CY7C64013:
410 				data[0] = 0;
411 				data[1] = actlen;
412 				break;
413 			default:
414 				break;
415 			}
416 
417 			usbd_copy_in(pc0, 0, &req, sizeof(req));
418 			usbd_copy_in(pc1, 0, data, offset);
419 
420 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
421 			usbd_xfer_set_frame_len(xfer, 1, sc->sc_olen);
422 			usbd_xfer_set_frames(xfer, sc->sc_olen ? 2 : 1);
423 			usbd_transfer_submit(xfer);
424 		}
425 		return;
426 
427 	default:			/* Error */
428 		if (error == USB_ERR_CANCELLED) {
429 			return;
430 		}
431 		DPRINTF("error=%s\n",
432 		    usbd_errstr(error));
433 		goto tr_transferred;
434 	}
435 }
436 
437 static void
ucycom_cfg_write(struct ucycom_softc * sc,uint32_t baud,uint8_t cfg)438 ucycom_cfg_write(struct ucycom_softc *sc, uint32_t baud, uint8_t cfg)
439 {
440 	struct usb_device_request req;
441 	uint16_t len;
442 	usb_error_t err;
443 
444 	len = sc->sc_flen;
445 	if (len > sizeof(sc->sc_temp_cfg)) {
446 		len = sizeof(sc->sc_temp_cfg);
447 	}
448 	sc->sc_cfg = cfg;
449 
450 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
451 	req.bRequest = UR_SET_REPORT;
452 	USETW2(req.wValue, UHID_FEATURE_REPORT, sc->sc_fid);
453 	req.wIndex[0] = sc->sc_iface_no;
454 	req.wIndex[1] = 0;
455 	USETW(req.wLength, len);
456 
457 	sc->sc_temp_cfg[0] = (baud & 0xff);
458 	sc->sc_temp_cfg[1] = (baud >> 8) & 0xff;
459 	sc->sc_temp_cfg[2] = (baud >> 16) & 0xff;
460 	sc->sc_temp_cfg[3] = (baud >> 24) & 0xff;
461 	sc->sc_temp_cfg[4] = cfg;
462 
463 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
464 	    &req, sc->sc_temp_cfg, 0, 1000);
465 	if (err) {
466 		DPRINTFN(0, "device request failed, err=%s "
467 		    "(ignored)\n", usbd_errstr(err));
468 	}
469 }
470 
471 static int
ucycom_pre_param(struct ucom_softc * ucom,struct termios * t)472 ucycom_pre_param(struct ucom_softc *ucom, struct termios *t)
473 {
474 	switch (t->c_ospeed) {
475 		case 600:
476 		case 1200:
477 		case 2400:
478 		case 4800:
479 		case 9600:
480 		case 19200:
481 		case 38400:
482 		case 57600:
483 #if 0
484 		/*
485 		 * Stock chips only support standard baud rates in the 600 - 57600
486 		 * range, but higher rates can be achieved using custom firmware.
487 		 */
488 		case 115200:
489 		case 153600:
490 		case 192000:
491 #endif
492 		break;
493 	default:
494 		return (EINVAL);
495 	}
496 	return (0);
497 }
498 
499 static void
ucycom_cfg_param(struct ucom_softc * ucom,struct termios * t)500 ucycom_cfg_param(struct ucom_softc *ucom, struct termios *t)
501 {
502 	struct ucycom_softc *sc = ucom->sc_parent;
503 	uint8_t cfg;
504 
505 	DPRINTF("\n");
506 
507 	if (t->c_cflag & CIGNORE) {
508 		cfg = sc->sc_cfg;
509 	} else {
510 		cfg = 0;
511 		switch (t->c_cflag & CSIZE) {
512 		default:
513 		case CS8:
514 			++cfg;
515 		case CS7:
516 			++cfg;
517 		case CS6:
518 			++cfg;
519 		case CS5:
520 			break;
521 		}
522 
523 		if (t->c_cflag & CSTOPB)
524 			cfg |= UCYCOM_CFG_STOPB;
525 		if (t->c_cflag & PARENB)
526 			cfg |= UCYCOM_CFG_PAREN;
527 		if (t->c_cflag & PARODD)
528 			cfg |= UCYCOM_CFG_PARODD;
529 	}
530 
531 	ucycom_cfg_write(sc, t->c_ospeed, cfg);
532 }
533 
534 static void
ucycom_intr_read_callback(struct usb_xfer * xfer,usb_error_t error)535 ucycom_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
536 {
537 	struct ucycom_softc *sc = usbd_xfer_softc(xfer);
538 	struct usb_page_cache *pc;
539 	uint8_t buf[2];
540 	uint32_t offset;
541 	int len;
542 	int actlen;
543 
544 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
545 	pc = usbd_xfer_get_frame(xfer, 0);
546 
547 	switch (USB_GET_STATE(xfer)) {
548 	case USB_ST_TRANSFERRED:
549 		switch (sc->sc_model) {
550 		case MODEL_CY7C63743:
551 			if (actlen < 1) {
552 				goto tr_setup;
553 			}
554 			usbd_copy_out(pc, 0, buf, 1);
555 
556 			sc->sc_ist = buf[0] & ~0x07;
557 			len = buf[0] & 0x07;
558 
559 			actlen--;
560 			offset = 1;
561 
562 			break;
563 
564 		case MODEL_CY7C64013:
565 			if (actlen < 2) {
566 				goto tr_setup;
567 			}
568 			usbd_copy_out(pc, 0, buf, 2);
569 
570 			sc->sc_ist = buf[0] & ~0x07;
571 			len = buf[1];
572 
573 			actlen -= 2;
574 			offset = 2;
575 
576 			break;
577 
578 		default:
579 			DPRINTFN(0, "unsupported model number\n");
580 			goto tr_setup;
581 		}
582 
583 		if (len > actlen)
584 			len = actlen;
585 		if (len)
586 			ucom_put_data(&sc->sc_ucom, pc, offset, len);
587 		/* FALLTHROUGH */
588 	case USB_ST_SETUP:
589 tr_setup:
590 		usbd_xfer_set_frame_len(xfer, 0, sc->sc_ilen);
591 		usbd_transfer_submit(xfer);
592 		return;
593 
594 	default:			/* Error */
595 		if (error != USB_ERR_CANCELLED) {
596 			/* try to clear stall first */
597 			usbd_xfer_set_stall(xfer);
598 			goto tr_setup;
599 		}
600 		return;
601 	}
602 }
603 
604 static void
ucycom_poll(struct ucom_softc * ucom)605 ucycom_poll(struct ucom_softc *ucom)
606 {
607 	struct ucycom_softc *sc = ucom->sc_parent;
608 	usbd_transfer_poll(sc->sc_xfer, UCYCOM_N_TRANSFER);
609 }
610