2 * Copyright (c) 2004 Dag-Erling Coïdan Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Device driver for Cypress CY7C637xx and CY7C640/1xx series USB to
35 * Normally, a driver for a USB-to-serial chip would hang off the ucom(4)
36 * driver, but ucom(4) was written under the assumption that all USB-to-
37 * serial chips use bulk pipes for I/O, while the Cypress parts use HID
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/module.h>
49 #include <sys/sysctl.h>
54 #include <dev/usb/usb.h>
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usbdi_util.h>
57 #include <dev/usb/usbhid.h>
58 #include <dev/usb/hid.h>
60 #define UCYCOM_EP_INPUT 0
61 #define UCYCOM_EP_OUTPUT 1
63 #define UCYCOM_MAX_IOLEN 32U
69 unsigned long sc_cintr;
71 unsigned long sc_clost;
72 unsigned long sc_cout;
75 usbd_device_handle sc_usbdev;
76 usbd_interface_handle sc_iface;
77 usbd_pipe_handle sc_pipe;
78 uint8_t sc_iep; /* input endpoint */
79 uint8_t sc_fid; /* feature report id*/
80 uint8_t sc_iid; /* input report id */
81 uint8_t sc_oid; /* output report id */
82 size_t sc_flen; /* feature report length */
83 size_t sc_ilen; /* input report length */
84 size_t sc_olen; /* output report length */
85 uint8_t sc_ibuf[UCYCOM_MAX_IOLEN];
87 /* model and settings */
89 #define MODEL_CY7C63743 0x63743
90 #define MODEL_CY7C64013 0x64013
93 #define UCYCOM_CFG_RESET 0x80
94 #define UCYCOM_CFG_PARODD 0x20
95 #define UCYCOM_CFG_PAREN 0x10
96 #define UCYCOM_CFG_STOPB 0x08
97 #define UCYCOM_CFG_DATAB 0x03
98 uint8_t sc_ist; /* status flags from last input */
99 uint8_t sc_ost; /* status flags for next output */
105 static device_probe_t ucycom_probe;
106 static device_attach_t ucycom_attach;
107 static device_detach_t ucycom_detach;
108 static t_open_t ucycom_open;
109 static t_close_t ucycom_close;
110 static void ucycom_start(struct tty *);
111 static void ucycom_stop(struct tty *, int);
112 static int ucycom_param(struct tty *, struct termios *);
113 static int ucycom_configure(struct ucycom_softc *, uint32_t, uint8_t);
114 static void ucycom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
116 static device_method_t ucycom_methods[] = {
117 DEVMETHOD(device_probe, ucycom_probe),
118 DEVMETHOD(device_attach, ucycom_attach),
119 DEVMETHOD(device_detach, ucycom_detach),
123 static driver_t ucycom_driver = {
126 sizeof(struct ucycom_softc),
129 static devclass_t ucycom_devclass;
131 DRIVER_MODULE(ucycom, uhub, ucycom_driver, ucycom_devclass, usbd_driver_load, 0);
132 MODULE_VERSION(ucycom, 1);
133 MODULE_DEPEND(ucycom, usb, 1, 1, 1);
139 static struct ucycom_device {
143 } ucycom_devices[] = {
144 { USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EARTHMATE, MODEL_CY7C64013 },
148 #define UCYCOM_DEFAULT_RATE 4800
149 #define UCYCOM_DEFAULT_CFG 0x03 /* N-8-1 */
151 /*****************************************************************************
158 ucycom_probe(device_t dev)
160 struct usb_attach_arg *uaa;
161 struct ucycom_device *ud;
163 uaa = device_get_ivars(dev);
164 if (uaa->iface != NULL)
165 return (UMATCH_NONE);
166 for (ud = ucycom_devices; ud->model != 0; ++ud)
167 if (ud->vendor == uaa->vendor && ud->product == uaa->product)
168 return (UMATCH_VENDOR_PRODUCT);
169 return (UMATCH_NONE);
173 ucycom_attach(device_t dev)
175 struct usb_attach_arg *uaa;
176 struct ucycom_softc *sc;
177 struct ucycom_device *ud;
178 usb_endpoint_descriptor_t *ued;
182 /* get arguments and softc */
183 uaa = device_get_ivars(dev);
184 sc = device_get_softc(dev);
185 bzero(sc, sizeof *sc);
187 sc->sc_usbdev = uaa->device;
190 for (ud = ucycom_devices; ud->model != 0; ++ud)
191 if (ud->vendor == uaa->vendor && ud->product == uaa->product)
192 sc->sc_model = ud->model;
193 if (sc->sc_model == 0) {
194 device_printf(dev, "unsupported device\n");
197 device_printf(dev, "Cypress CY7C%X USB to RS232 bridge\n", sc->sc_model);
199 /* select configuration */
200 error = usbd_set_config_index(sc->sc_usbdev, 0, 1 /* verbose */);
202 device_printf(dev, "failed to select configuration: %s\n",
207 /* get first interface handle */
208 error = usbd_device2interface_handle(sc->sc_usbdev, 0, &sc->sc_iface);
210 device_printf(dev, "failed to get interface handle: %s\n",
215 /* get report descriptor */
216 error = usbd_read_report_desc(sc->sc_iface, &urd, &urdlen, M_USBDEV);
218 device_printf(dev, "failed to get report descriptor: %s\n",
223 /* get report sizes */
224 sc->sc_flen = hid_report_size(urd, urdlen, hid_feature, &sc->sc_fid);
225 sc->sc_ilen = hid_report_size(urd, urdlen, hid_input, &sc->sc_iid);
226 sc->sc_olen = hid_report_size(urd, urdlen, hid_output, &sc->sc_oid);
228 if (sc->sc_ilen > UCYCOM_MAX_IOLEN || sc->sc_olen > UCYCOM_MAX_IOLEN) {
229 device_printf(dev, "I/O report size too big (%zu, %zu, %u)\n",
230 sc->sc_ilen, sc->sc_olen, UCYCOM_MAX_IOLEN);
234 /* get and verify input endpoint descriptor */
235 ued = usbd_interface2endpoint_descriptor(sc->sc_iface, UCYCOM_EP_INPUT);
237 device_printf(dev, "failed to get input endpoint descriptor\n");
240 if (UE_GET_DIR(ued->bEndpointAddress) != UE_DIR_IN) {
241 device_printf(dev, "not an input endpoint\n");
244 if ((ued->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
245 device_printf(dev, "not an interrupt endpoint\n");
248 sc->sc_iep = ued->bEndpointAddress;
251 sc->sc_tty = ttyalloc();
252 sc->sc_tty->t_sc = sc;
253 sc->sc_tty->t_oproc = ucycom_start;
254 sc->sc_tty->t_stop = ucycom_stop;
255 sc->sc_tty->t_param = ucycom_param;
256 sc->sc_tty->t_open = ucycom_open;
257 sc->sc_tty->t_close = ucycom_close;
259 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
260 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
261 OID_AUTO, "intr", CTLFLAG_RD, &sc->sc_cintr, 0,
263 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
264 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
265 OID_AUTO, "in", CTLFLAG_RD, &sc->sc_cin, 0,
267 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
268 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
269 OID_AUTO, "lost", CTLFLAG_RD, &sc->sc_clost, 0,
271 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
272 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
273 OID_AUTO, "out", CTLFLAG_RD, &sc->sc_cout, 0,
276 /* create character device node */
277 ttycreate(sc->sc_tty, 0, "y%r", device_get_unit(sc->sc_dev));
283 ucycom_detach(device_t dev)
285 struct ucycom_softc *sc;
287 sc = device_get_softc(dev);
294 /*****************************************************************************
301 ucycom_open(struct tty *tp, struct cdev *cdev)
303 struct ucycom_softc *sc = tp->t_sc;
306 /* set default configuration */
307 ucycom_configure(sc, UCYCOM_DEFAULT_RATE, UCYCOM_DEFAULT_CFG);
309 /* open interrupt pipe */
310 error = usbd_open_pipe_intr(sc->sc_iface, sc->sc_iep, 0,
311 &sc->sc_pipe, sc, sc->sc_ibuf, sc->sc_ilen,
312 ucycom_intr, USBD_DEFAULT_INTERVAL);
314 device_printf(sc->sc_dev, "failed to open interrupt pipe: %s\n",
320 device_printf(sc->sc_dev, "%s bypass l_rint()\n",
321 (sc->sc_tty->t_state & TS_CAN_BYPASS_L_RINT) ?
329 ucycom_close(struct tty *tp)
331 struct ucycom_softc *sc = tp->t_sc;
333 /* stop interrupts and close the interrupt pipe */
334 usbd_abort_pipe(sc->sc_pipe);
335 usbd_close_pipe(sc->sc_pipe);
341 /*****************************************************************************
348 ucycom_start(struct tty *tty)
350 struct ucycom_softc *sc = tty->t_sc;
351 uint8_t report[sc->sc_olen];
354 while (sc->sc_error == 0 && sc->sc_tty->t_outq.c_cc > 0) {
355 switch (sc->sc_model) {
356 case MODEL_CY7C63743:
357 len = q_to_b(&sc->sc_tty->t_outq,
358 report + 1, sc->sc_olen - 1);
363 case MODEL_CY7C64013:
364 len = q_to_b(&sc->sc_tty->t_outq,
365 report + 2, sc->sc_olen - 2);
372 panic("unsupported model (driver error)");
375 while (len < sc->sc_olen)
377 error = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
378 sc->sc_oid, report, sc->sc_olen);
381 device_printf(sc->sc_dev,
382 "failed to set output report: %s\n",
384 sc->sc_error = error;
391 ucycom_stop(struct tty *tty, int flags)
393 struct ucycom_softc *sc;
397 device_printf(sc->sc_dev, "%s()\n", __func__);
401 ucycom_param(struct tty *tty, struct termios *t)
403 struct ucycom_softc *sc;
410 if (t->c_ispeed != t->c_ospeed)
414 if (t->c_cflag & CIGNORE) {
418 switch (t->c_cflag & CSIZE) {
430 if (t->c_cflag & CSTOPB)
431 cfg |= UCYCOM_CFG_STOPB;
432 if (t->c_cflag & PARENB)
433 cfg |= UCYCOM_CFG_PAREN;
434 if (t->c_cflag & PARODD)
435 cfg |= UCYCOM_CFG_PARODD;
438 error = ucycom_configure(sc, baud, cfg);
442 /*****************************************************************************
449 ucycom_configure(struct ucycom_softc *sc, uint32_t baud, uint8_t cfg)
451 uint8_t report[sc->sc_flen];
465 * Stock chips only support standard baud rates in the 600 - 57600
466 * range, but higher rates can be achieved using custom firmware.
478 device_printf(sc->sc_dev, "%d baud, %c-%d-%d\n", baud,
479 (cfg & UCYCOM_CFG_PAREN) ?
480 ((cfg & UCYCOM_CFG_PARODD) ? 'O' : 'E') : 'N',
481 5 + (cfg & UCYCOM_CFG_DATAB),
482 (cfg & UCYCOM_CFG_STOPB) ? 2 : 1);
483 report[0] = baud & 0xff;
484 report[1] = (baud >> 8) & 0xff;
485 report[2] = (baud >> 16) & 0xff;
486 report[3] = (baud >> 24) & 0xff;
488 error = usbd_set_report(sc->sc_iface, UHID_FEATURE_REPORT,
489 sc->sc_fid, report, sc->sc_flen);
491 device_printf(sc->sc_dev, "%s\n", usbd_errstr(error));
500 ucycom_intr(usbd_xfer_handle xfer, usbd_private_handle scp, usbd_status status)
502 struct ucycom_softc *sc = scp;
508 switch (sc->sc_model) {
509 case MODEL_CY7C63743:
510 sc->sc_ist = sc->sc_ibuf[0] & ~0x07;
511 len = sc->sc_ibuf[0] & 0x07;
512 data = sc->sc_ibuf + 1;
514 case MODEL_CY7C64013:
515 sc->sc_ist = sc->sc_ibuf[0] & ~0x07;
516 len = sc->sc_ibuf[1];
517 data = sc->sc_ibuf + 2;
520 panic("unsupported model (driver error)");
524 case USBD_NORMAL_COMPLETION:
531 if (sc->sc_tty->t_state & TS_CAN_BYPASS_L_RINT) {
532 /* XXX flow control! */
533 lost = b_to_q(data, len, &sc->sc_tty->t_rawq);
534 sc->sc_tty->t_rawcc += len - lost;
535 ttwakeup(sc->sc_tty);
537 for (i = 0, lost = len; i < len; ++i, --lost)
538 if (ttyld_rint(sc->sc_tty, data[i]) != 0)
541 sc->sc_cin += len - lost;
542 sc->sc_clost += lost;