5 * Copyright (c) 2004 Dag-Erling Coïdan Smørgrav
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer
13 * in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * Device driver for Cypress CY7C637xx and CY7C640/1xx series USB to
37 #include <sys/stdint.h>
38 #include <sys/stddef.h>
39 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <sys/types.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
45 #include <sys/module.h>
47 #include <sys/mutex.h>
48 #include <sys/condvar.h>
49 #include <sys/sysctl.h>
51 #include <sys/unistd.h>
52 #include <sys/callout.h>
53 #include <sys/malloc.h>
56 #include <dev/usb/usb.h>
57 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usbdi_util.h>
59 #include <dev/usb/usbhid.h>
62 #define USB_DEBUG_VAR usb_debug
63 #include <dev/usb/usb_debug.h>
64 #include <dev/usb/usb_process.h>
66 #include <dev/usb/serial/usb_serial.h>
68 #define UCYCOM_MAX_IOLEN (1024 + 2) /* bytes */
70 #define UCYCOM_IFACE_INDEX 0
79 struct ucom_super_softc sc_super_ucom;
80 struct ucom_softc sc_ucom;
82 struct usb_device *sc_udev;
83 struct usb_xfer *sc_xfer[UCYCOM_N_TRANSFER];
87 #define MODEL_CY7C63743 0x63743
88 #define MODEL_CY7C64013 0x64013
90 uint16_t sc_flen; /* feature report length */
91 uint16_t sc_ilen; /* input report length */
92 uint16_t sc_olen; /* output report length */
94 uint8_t sc_fid; /* feature report id */
95 uint8_t sc_iid; /* input report id */
96 uint8_t sc_oid; /* output report id */
98 #define UCYCOM_CFG_RESET 0x80
99 #define UCYCOM_CFG_PARODD 0x20
100 #define UCYCOM_CFG_PAREN 0x10
101 #define UCYCOM_CFG_STOPB 0x08
102 #define UCYCOM_CFG_DATAB 0x03
103 uint8_t sc_ist; /* status flags from last input */
105 uint8_t sc_temp_cfg[32];
110 static device_probe_t ucycom_probe;
111 static device_attach_t ucycom_attach;
112 static device_detach_t ucycom_detach;
113 static void ucycom_free_softc(struct ucycom_softc *);
115 static usb_callback_t ucycom_ctrl_write_callback;
116 static usb_callback_t ucycom_intr_read_callback;
118 static void ucycom_free(struct ucom_softc *);
119 static void ucycom_cfg_open(struct ucom_softc *);
120 static void ucycom_start_read(struct ucom_softc *);
121 static void ucycom_stop_read(struct ucom_softc *);
122 static void ucycom_start_write(struct ucom_softc *);
123 static void ucycom_stop_write(struct ucom_softc *);
124 static void ucycom_cfg_write(struct ucycom_softc *, uint32_t, uint8_t);
125 static int ucycom_pre_param(struct ucom_softc *, struct termios *);
126 static void ucycom_cfg_param(struct ucom_softc *, struct termios *);
127 static void ucycom_poll(struct ucom_softc *ucom);
129 static const struct usb_config ucycom_config[UCYCOM_N_TRANSFER] = {
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 */
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,
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,
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),
169 static devclass_t ucycom_devclass;
171 static driver_t ucycom_driver = {
173 .methods = ucycom_methods,
174 .size = sizeof(struct ucycom_softc),
180 static const STRUCT_USB_HOST_ID ucycom_devs[] = {
181 {USB_VPI(USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EARTHMATE, MODEL_CY7C64013)},
184 DRIVER_MODULE(ucycom, uhub, ucycom_driver, ucycom_devclass, NULL, 0);
185 MODULE_DEPEND(ucycom, ucom, 1, 1, 1);
186 MODULE_DEPEND(ucycom, usb, 1, 1, 1);
187 MODULE_VERSION(ucycom, 1);
188 USB_PNP_HOST_INFO(ucycom_devs);
190 #define UCYCOM_DEFAULT_RATE 4800
191 #define UCYCOM_DEFAULT_CFG 0x03 /* N-8-1 */
194 ucycom_probe(device_t dev)
196 struct usb_attach_arg *uaa = device_get_ivars(dev);
198 if (uaa->usb_mode != USB_MODE_HOST) {
201 if (uaa->info.bConfigIndex != 0) {
204 if (uaa->info.bIfaceIndex != UCYCOM_IFACE_INDEX) {
207 return (usbd_lookup_id_by_uaa(ucycom_devs, sizeof(ucycom_devs), uaa));
211 ucycom_attach(device_t dev)
213 struct usb_attach_arg *uaa = device_get_ivars(dev);
214 struct ucycom_softc *sc = device_get_softc(dev);
215 void *urd_ptr = NULL;
220 sc->sc_udev = uaa->device;
222 device_set_usb_desc(dev);
223 mtx_init(&sc->sc_mtx, "ucycom", NULL, MTX_DEF);
224 ucom_ref(&sc->sc_super_ucom);
229 sc->sc_model = USB_GET_DRIVER_INFO(uaa);
230 if (sc->sc_model == 0) {
231 device_printf(dev, "unsupported device\n");
234 device_printf(dev, "Cypress CY7C%X USB to RS232 bridge\n", sc->sc_model);
236 /* get report descriptor */
238 error = usbd_req_get_hid_desc(uaa->device, NULL,
239 &urd_ptr, &urd_len, M_USBDEV,
243 device_printf(dev, "failed to get report "
248 /* get report sizes */
250 sc->sc_flen = hid_report_size(urd_ptr, urd_len, hid_feature, &sc->sc_fid);
251 sc->sc_ilen = hid_report_size(urd_ptr, urd_len, hid_input, &sc->sc_iid);
252 sc->sc_olen = hid_report_size(urd_ptr, urd_len, hid_output, &sc->sc_oid);
254 if ((sc->sc_ilen > UCYCOM_MAX_IOLEN) || (sc->sc_ilen < 1) ||
255 (sc->sc_olen > UCYCOM_MAX_IOLEN) || (sc->sc_olen < 2) ||
256 (sc->sc_flen > UCYCOM_MAX_IOLEN) || (sc->sc_flen < 5)) {
257 device_printf(dev, "invalid report size i=%d, o=%d, f=%d, max=%d\n",
258 sc->sc_ilen, sc->sc_olen, sc->sc_flen,
262 sc->sc_iface_no = uaa->info.bIfaceNum;
264 iface_index = UCYCOM_IFACE_INDEX;
265 error = usbd_transfer_setup(uaa->device, &iface_index,
266 sc->sc_xfer, ucycom_config, UCYCOM_N_TRANSFER,
269 device_printf(dev, "allocating USB "
270 "transfers failed\n");
273 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
274 &ucycom_callback, &sc->sc_mtx);
278 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
281 free(urd_ptr, M_USBDEV);
284 return (0); /* success */
288 free(urd_ptr, M_USBDEV);
295 ucycom_detach(device_t dev)
297 struct ucycom_softc *sc = device_get_softc(dev);
299 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
300 usbd_transfer_unsetup(sc->sc_xfer, UCYCOM_N_TRANSFER);
302 device_claim_softc(dev);
304 ucycom_free_softc(sc);
309 UCOM_UNLOAD_DRAIN(ucycom);
312 ucycom_free_softc(struct ucycom_softc *sc)
314 if (ucom_unref(&sc->sc_super_ucom)) {
315 mtx_destroy(&sc->sc_mtx);
316 device_free_softc(sc);
321 ucycom_free(struct ucom_softc *ucom)
323 ucycom_free_softc(ucom->sc_parent);
327 ucycom_cfg_open(struct ucom_softc *ucom)
329 struct ucycom_softc *sc = ucom->sc_parent;
331 /* set default configuration */
332 ucycom_cfg_write(sc, UCYCOM_DEFAULT_RATE, UCYCOM_DEFAULT_CFG);
336 ucycom_start_read(struct ucom_softc *ucom)
338 struct ucycom_softc *sc = ucom->sc_parent;
340 usbd_transfer_start(sc->sc_xfer[UCYCOM_INTR_RD]);
344 ucycom_stop_read(struct ucom_softc *ucom)
346 struct ucycom_softc *sc = ucom->sc_parent;
348 usbd_transfer_stop(sc->sc_xfer[UCYCOM_INTR_RD]);
352 ucycom_start_write(struct ucom_softc *ucom)
354 struct ucycom_softc *sc = ucom->sc_parent;
356 usbd_transfer_start(sc->sc_xfer[UCYCOM_CTRL_RD]);
360 ucycom_stop_write(struct ucom_softc *ucom)
362 struct ucycom_softc *sc = ucom->sc_parent;
364 usbd_transfer_stop(sc->sc_xfer[UCYCOM_CTRL_RD]);
368 ucycom_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error)
370 struct ucycom_softc *sc = usbd_xfer_softc(xfer);
371 struct usb_device_request req;
372 struct usb_page_cache *pc0, *pc1;
377 pc0 = usbd_xfer_get_frame(xfer, 0);
378 pc1 = usbd_xfer_get_frame(xfer, 1);
380 switch (USB_GET_STATE(xfer)) {
381 case USB_ST_TRANSFERRED:
385 switch (sc->sc_model) {
386 case MODEL_CY7C63743:
389 case MODEL_CY7C64013:
397 if (ucom_get_data(&sc->sc_ucom, pc1, offset,
398 sc->sc_olen - offset, &actlen)) {
400 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
401 req.bRequest = UR_SET_REPORT;
402 USETW2(req.wValue, UHID_OUTPUT_REPORT, sc->sc_oid);
403 req.wIndex[0] = sc->sc_iface_no;
405 USETW(req.wLength, sc->sc_olen);
407 switch (sc->sc_model) {
408 case MODEL_CY7C63743:
411 case MODEL_CY7C64013:
419 usbd_copy_in(pc0, 0, &req, sizeof(req));
420 usbd_copy_in(pc1, 0, data, offset);
422 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
423 usbd_xfer_set_frame_len(xfer, 1, sc->sc_olen);
424 usbd_xfer_set_frames(xfer, sc->sc_olen ? 2 : 1);
425 usbd_transfer_submit(xfer);
430 if (error == USB_ERR_CANCELLED) {
433 DPRINTF("error=%s\n",
440 ucycom_cfg_write(struct ucycom_softc *sc, uint32_t baud, uint8_t cfg)
442 struct usb_device_request req;
447 if (len > sizeof(sc->sc_temp_cfg)) {
448 len = sizeof(sc->sc_temp_cfg);
452 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
453 req.bRequest = UR_SET_REPORT;
454 USETW2(req.wValue, UHID_FEATURE_REPORT, sc->sc_fid);
455 req.wIndex[0] = sc->sc_iface_no;
457 USETW(req.wLength, len);
459 sc->sc_temp_cfg[0] = (baud & 0xff);
460 sc->sc_temp_cfg[1] = (baud >> 8) & 0xff;
461 sc->sc_temp_cfg[2] = (baud >> 16) & 0xff;
462 sc->sc_temp_cfg[3] = (baud >> 24) & 0xff;
463 sc->sc_temp_cfg[4] = cfg;
465 err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
466 &req, sc->sc_temp_cfg, 0, 1000);
468 DPRINTFN(0, "device request failed, err=%s "
469 "(ignored)\n", usbd_errstr(err));
474 ucycom_pre_param(struct ucom_softc *ucom, struct termios *t)
476 switch (t->c_ospeed) {
487 * Stock chips only support standard baud rates in the 600 - 57600
488 * range, but higher rates can be achieved using custom firmware.
502 ucycom_cfg_param(struct ucom_softc *ucom, struct termios *t)
504 struct ucycom_softc *sc = ucom->sc_parent;
509 if (t->c_cflag & CIGNORE) {
513 switch (t->c_cflag & CSIZE) {
525 if (t->c_cflag & CSTOPB)
526 cfg |= UCYCOM_CFG_STOPB;
527 if (t->c_cflag & PARENB)
528 cfg |= UCYCOM_CFG_PAREN;
529 if (t->c_cflag & PARODD)
530 cfg |= UCYCOM_CFG_PARODD;
533 ucycom_cfg_write(sc, t->c_ospeed, cfg);
537 ucycom_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
539 struct ucycom_softc *sc = usbd_xfer_softc(xfer);
540 struct usb_page_cache *pc;
546 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
547 pc = usbd_xfer_get_frame(xfer, 0);
549 switch (USB_GET_STATE(xfer)) {
550 case USB_ST_TRANSFERRED:
551 switch (sc->sc_model) {
552 case MODEL_CY7C63743:
556 usbd_copy_out(pc, 0, buf, 1);
558 sc->sc_ist = buf[0] & ~0x07;
566 case MODEL_CY7C64013:
570 usbd_copy_out(pc, 0, buf, 2);
572 sc->sc_ist = buf[0] & ~0x07;
581 DPRINTFN(0, "unsupported model number\n");
588 ucom_put_data(&sc->sc_ucom, pc, offset, len);
592 usbd_xfer_set_frame_len(xfer, 0, sc->sc_ilen);
593 usbd_transfer_submit(xfer);
597 if (error != USB_ERR_CANCELLED) {
598 /* try to clear stall first */
599 usbd_xfer_set_stall(xfer);
608 ucycom_poll(struct ucom_softc *ucom)
610 struct ucycom_softc *sc = ucom->sc_parent;
611 usbd_transfer_poll(sc->sc_xfer, UCYCOM_N_TRANSFER);