5 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
7 * Copyright (c) 2004 Dag-Erling Coïdan Smørgrav
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
35 * Device driver for Cypress CY7C637xx and CY7C640/1xx series USB to
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>
47 #include <sys/module.h>
49 #include <sys/mutex.h>
50 #include <sys/condvar.h>
51 #include <sys/sysctl.h>
53 #include <sys/unistd.h>
54 #include <sys/callout.h>
55 #include <sys/malloc.h>
58 #include <dev/hid/hid.h>
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>
66 #define USB_DEBUG_VAR usb_debug
67 #include <dev/usb/usb_debug.h>
68 #include <dev/usb/usb_process.h>
70 #include <dev/usb/serial/usb_serial.h>
72 #define UCYCOM_MAX_IOLEN (1024 + 2) /* bytes */
74 #define UCYCOM_IFACE_INDEX 0
83 struct ucom_super_softc sc_super_ucom;
84 struct ucom_softc sc_ucom;
86 struct usb_device *sc_udev;
87 struct usb_xfer *sc_xfer[UCYCOM_N_TRANSFER];
91 #define MODEL_CY7C63743 0x63743
92 #define MODEL_CY7C64013 0x64013
94 uint16_t sc_flen; /* feature report length */
95 uint16_t sc_ilen; /* input report length */
96 uint16_t sc_olen; /* output report length */
98 uint8_t sc_fid; /* feature report id */
99 uint8_t sc_iid; /* input report id */
100 uint8_t sc_oid; /* output report id */
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 */
109 uint8_t sc_temp_cfg[32];
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 *);
119 static usb_callback_t ucycom_ctrl_write_callback;
120 static usb_callback_t ucycom_intr_read_callback;
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);
133 static const struct usb_config ucycom_config[UCYCOM_N_TRANSFER] = {
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 */
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,
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,
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),
172 static devclass_t ucycom_devclass;
174 static driver_t ucycom_driver = {
176 .methods = ucycom_methods,
177 .size = sizeof(struct ucycom_softc),
183 static const STRUCT_USB_HOST_ID ucycom_devs[] = {
184 {USB_VPI(USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EARTHMATE, MODEL_CY7C64013)},
187 DRIVER_MODULE(ucycom, uhub, ucycom_driver, ucycom_devclass, NULL, 0);
188 MODULE_DEPEND(ucycom, ucom, 1, 1, 1);
189 MODULE_DEPEND(ucycom, usb, 1, 1, 1);
190 MODULE_DEPEND(ucycom, hid, 1, 1, 1);
191 MODULE_VERSION(ucycom, 1);
192 USB_PNP_HOST_INFO(ucycom_devs);
194 #define UCYCOM_DEFAULT_RATE 4800
195 #define UCYCOM_DEFAULT_CFG 0x03 /* N-8-1 */
198 ucycom_probe(device_t dev)
200 struct usb_attach_arg *uaa = device_get_ivars(dev);
202 if (uaa->usb_mode != USB_MODE_HOST) {
205 if (uaa->info.bConfigIndex != 0) {
208 if (uaa->info.bIfaceIndex != UCYCOM_IFACE_INDEX) {
211 return (usbd_lookup_id_by_uaa(ucycom_devs, sizeof(ucycom_devs), uaa));
215 ucycom_attach(device_t dev)
217 struct usb_attach_arg *uaa = device_get_ivars(dev);
218 struct ucycom_softc *sc = device_get_softc(dev);
219 void *urd_ptr = NULL;
224 sc->sc_udev = uaa->device;
226 device_set_usb_desc(dev);
227 mtx_init(&sc->sc_mtx, "ucycom", NULL, MTX_DEF);
228 ucom_ref(&sc->sc_super_ucom);
233 sc->sc_model = USB_GET_DRIVER_INFO(uaa);
234 if (sc->sc_model == 0) {
235 device_printf(dev, "unsupported device\n");
238 device_printf(dev, "Cypress CY7C%X USB to RS232 bridge\n", sc->sc_model);
240 /* get report descriptor */
242 error = usbd_req_get_hid_desc(uaa->device, NULL,
243 &urd_ptr, &urd_len, M_USBDEV,
247 device_printf(dev, "failed to get report "
252 /* get report sizes */
254 sc->sc_flen = hid_report_size_max(urd_ptr, urd_len, hid_feature, &sc->sc_fid);
255 sc->sc_ilen = hid_report_size_max(urd_ptr, urd_len, hid_input, &sc->sc_iid);
256 sc->sc_olen = hid_report_size_max(urd_ptr, urd_len, hid_output, &sc->sc_oid);
258 if ((sc->sc_ilen > UCYCOM_MAX_IOLEN) || (sc->sc_ilen < 1) ||
259 (sc->sc_olen > UCYCOM_MAX_IOLEN) || (sc->sc_olen < 2) ||
260 (sc->sc_flen > UCYCOM_MAX_IOLEN) || (sc->sc_flen < 5)) {
261 device_printf(dev, "invalid report size i=%d, o=%d, f=%d, max=%d\n",
262 sc->sc_ilen, sc->sc_olen, sc->sc_flen,
266 sc->sc_iface_no = uaa->info.bIfaceNum;
268 iface_index = UCYCOM_IFACE_INDEX;
269 error = usbd_transfer_setup(uaa->device, &iface_index,
270 sc->sc_xfer, ucycom_config, UCYCOM_N_TRANSFER,
273 device_printf(dev, "allocating USB "
274 "transfers failed\n");
277 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
278 &ucycom_callback, &sc->sc_mtx);
282 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
285 free(urd_ptr, M_USBDEV);
288 return (0); /* success */
292 free(urd_ptr, M_USBDEV);
299 ucycom_detach(device_t dev)
301 struct ucycom_softc *sc = device_get_softc(dev);
303 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
304 usbd_transfer_unsetup(sc->sc_xfer, UCYCOM_N_TRANSFER);
306 device_claim_softc(dev);
308 ucycom_free_softc(sc);
313 UCOM_UNLOAD_DRAIN(ucycom);
316 ucycom_free_softc(struct ucycom_softc *sc)
318 if (ucom_unref(&sc->sc_super_ucom)) {
319 mtx_destroy(&sc->sc_mtx);
320 device_free_softc(sc);
325 ucycom_free(struct ucom_softc *ucom)
327 ucycom_free_softc(ucom->sc_parent);
331 ucycom_cfg_open(struct ucom_softc *ucom)
333 struct ucycom_softc *sc = ucom->sc_parent;
335 /* set default configuration */
336 ucycom_cfg_write(sc, UCYCOM_DEFAULT_RATE, UCYCOM_DEFAULT_CFG);
340 ucycom_start_read(struct ucom_softc *ucom)
342 struct ucycom_softc *sc = ucom->sc_parent;
344 usbd_transfer_start(sc->sc_xfer[UCYCOM_INTR_RD]);
348 ucycom_stop_read(struct ucom_softc *ucom)
350 struct ucycom_softc *sc = ucom->sc_parent;
352 usbd_transfer_stop(sc->sc_xfer[UCYCOM_INTR_RD]);
356 ucycom_start_write(struct ucom_softc *ucom)
358 struct ucycom_softc *sc = ucom->sc_parent;
360 usbd_transfer_start(sc->sc_xfer[UCYCOM_CTRL_RD]);
364 ucycom_stop_write(struct ucom_softc *ucom)
366 struct ucycom_softc *sc = ucom->sc_parent;
368 usbd_transfer_stop(sc->sc_xfer[UCYCOM_CTRL_RD]);
372 ucycom_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error)
374 struct ucycom_softc *sc = usbd_xfer_softc(xfer);
375 struct usb_device_request req;
376 struct usb_page_cache *pc0, *pc1;
381 pc0 = usbd_xfer_get_frame(xfer, 0);
382 pc1 = usbd_xfer_get_frame(xfer, 1);
384 switch (USB_GET_STATE(xfer)) {
385 case USB_ST_TRANSFERRED:
389 switch (sc->sc_model) {
390 case MODEL_CY7C63743:
393 case MODEL_CY7C64013:
401 if (ucom_get_data(&sc->sc_ucom, pc1, offset,
402 sc->sc_olen - offset, &actlen)) {
403 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
404 req.bRequest = UR_SET_REPORT;
405 USETW2(req.wValue, UHID_OUTPUT_REPORT, sc->sc_oid);
406 req.wIndex[0] = sc->sc_iface_no;
408 USETW(req.wLength, sc->sc_olen);
410 switch (sc->sc_model) {
411 case MODEL_CY7C63743:
414 case MODEL_CY7C64013:
422 usbd_copy_in(pc0, 0, &req, sizeof(req));
423 usbd_copy_in(pc1, 0, data, offset);
425 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
426 usbd_xfer_set_frame_len(xfer, 1, sc->sc_olen);
427 usbd_xfer_set_frames(xfer, sc->sc_olen ? 2 : 1);
428 usbd_transfer_submit(xfer);
433 if (error == USB_ERR_CANCELLED) {
436 DPRINTF("error=%s\n",
443 ucycom_cfg_write(struct ucycom_softc *sc, uint32_t baud, uint8_t cfg)
445 struct usb_device_request req;
450 if (len > sizeof(sc->sc_temp_cfg)) {
451 len = sizeof(sc->sc_temp_cfg);
455 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
456 req.bRequest = UR_SET_REPORT;
457 USETW2(req.wValue, UHID_FEATURE_REPORT, sc->sc_fid);
458 req.wIndex[0] = sc->sc_iface_no;
460 USETW(req.wLength, len);
462 sc->sc_temp_cfg[0] = (baud & 0xff);
463 sc->sc_temp_cfg[1] = (baud >> 8) & 0xff;
464 sc->sc_temp_cfg[2] = (baud >> 16) & 0xff;
465 sc->sc_temp_cfg[3] = (baud >> 24) & 0xff;
466 sc->sc_temp_cfg[4] = cfg;
468 err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
469 &req, sc->sc_temp_cfg, 0, 1000);
471 DPRINTFN(0, "device request failed, err=%s "
472 "(ignored)\n", usbd_errstr(err));
477 ucycom_pre_param(struct ucom_softc *ucom, struct termios *t)
479 switch (t->c_ospeed) {
490 * Stock chips only support standard baud rates in the 600 - 57600
491 * range, but higher rates can be achieved using custom firmware.
505 ucycom_cfg_param(struct ucom_softc *ucom, struct termios *t)
507 struct ucycom_softc *sc = ucom->sc_parent;
512 if (t->c_cflag & CIGNORE) {
516 switch (t->c_cflag & CSIZE) {
528 if (t->c_cflag & CSTOPB)
529 cfg |= UCYCOM_CFG_STOPB;
530 if (t->c_cflag & PARENB)
531 cfg |= UCYCOM_CFG_PAREN;
532 if (t->c_cflag & PARODD)
533 cfg |= UCYCOM_CFG_PARODD;
536 ucycom_cfg_write(sc, t->c_ospeed, cfg);
540 ucycom_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
542 struct ucycom_softc *sc = usbd_xfer_softc(xfer);
543 struct usb_page_cache *pc;
549 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
550 pc = usbd_xfer_get_frame(xfer, 0);
552 switch (USB_GET_STATE(xfer)) {
553 case USB_ST_TRANSFERRED:
554 switch (sc->sc_model) {
555 case MODEL_CY7C63743:
559 usbd_copy_out(pc, 0, buf, 1);
561 sc->sc_ist = buf[0] & ~0x07;
569 case MODEL_CY7C64013:
573 usbd_copy_out(pc, 0, buf, 2);
575 sc->sc_ist = buf[0] & ~0x07;
584 DPRINTFN(0, "unsupported model number\n");
591 ucom_put_data(&sc->sc_ucom, pc, offset, len);
595 usbd_xfer_set_frame_len(xfer, 0, sc->sc_ilen);
596 usbd_transfer_submit(xfer);
600 if (error != USB_ERR_CANCELLED) {
601 /* try to clear stall first */
602 usbd_xfer_set_stall(xfer);
610 ucycom_poll(struct ucom_softc *ucom)
612 struct ucycom_softc *sc = ucom->sc_parent;
613 usbd_transfer_poll(sc->sc_xfer, UCYCOM_N_TRANSFER);