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