/* * Copyright (c) 2008 Masato YAMANISHI * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include #include #include #include #include #include #include #include #include #ifdef TRCOM_DEBUG #define DPRINTFN(n, x) do { if (trcomdebug > (n)) logprintf x; } while (0) int trcomdebug = 0; #else #define DPRINTFN(n, x) #endif #define DPRINTF(x) DPRINTFN(0, x) #define TRCOMBUFSZ 64 #define TRCOM_CONFIG_NO 0 #define TRCOM_IFACE_NO 0 #define TRCOM_CTRL 0x40 #define TRCOM_OPEN_CLOSE 0x03 #define TRCOM_UART_DISABLE 0x00 #define TRCOM_UART_ENABLE 0x01 #define TRCOM_BREAK_OFF 0x00 #define TRCOM_BREAK_ON 0x01 struct trcom_softc { USBBASEDEVICE sc_dev; usbd_device_handle sc_udev; usbd_interface_handle sc_iface; device_ptr_t sc_subdev; u_char sc_msr; u_char sc_lsr; u_char sc_dying; }; Static void trcom_get_status(void *, int portno, u_char *lsr, u_char *msr); Static void trcom_set(void *, int, int, int); Static int trcom_param(void *, int, struct termios *); Static int trcom_open(void *sc, int portno); Static void trcom_close(void *, int); /* Static void trcom_break(void *sc, int portno, int onoff); */ struct ucom_methods trcom_methods = { trcom_get_status, trcom_set, trcom_param, NULL, trcom_open, trcom_close, NULL, NULL, }; static const struct usb_devno trcom_devs[] = { { USB_VENDOR_NEC, 0xFFFD }, /* 78K0 board TORAGI-BIOS */ { USB_VENDOR_NEC, 0x01CD } /* TK-78F0730(COM Sample) */ }; USB_DECLARE_DRIVER(trcom); USB_MATCH(trcom) { USB_MATCH_START(trcom, uaa); return (usb_lookup(trcom_devs, uaa->vendor, uaa->product) != NULL) ? UMATCH_VENDOR_PRODUCT : UMATCH_NONE; } USB_ATTACH(trcom) { USB_ATTACH_START(trcom, sc, uaa); struct ucom_attach_args uca; usb_interface_descriptor_t *id; usb_endpoint_descriptor_t *ed; usbd_status error; char *devinfop; int i; bzero(&uca, sizeof(uca)); sc->sc_udev = uaa->device; devinfop = usbd_devinfo_alloc(uaa->device, 0); USB_ATTACH_SETUP; printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop); usbd_devinfo_free(devinfop); if (usbd_set_config_index(sc->sc_udev, TRCOM_CONFIG_NO, 1) != 0) { printf("%s: could not set configuration no\n", USBDEVNAME(sc->sc_dev)); sc->sc_dying = 1; USB_ATTACH_ERROR_RETURN; } /* get the first interface handle */ error = usbd_device2interface_handle(sc->sc_udev, TRCOM_IFACE_NO, &sc->sc_iface); if (error != 0) { printf("%s: could not get interface handle\n", USBDEVNAME(sc->sc_dev)); sc->sc_dying = 1; USB_ATTACH_ERROR_RETURN; } id = usbd_get_interface_descriptor(sc->sc_iface); uca.bulkin = uca.bulkout = -1; for (i = 0; i < id->bNumEndpoints; i++) { ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); if (ed == NULL) { printf("%s: no endpoint descriptor found for %d\n", USBDEVNAME(sc->sc_dev), i); sc->sc_dying = 1; USB_ATTACH_ERROR_RETURN; } if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) uca.bulkin = ed->bEndpointAddress; else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) uca.bulkout = ed->bEndpointAddress; } if (uca.bulkin == -1 || uca.bulkout == -1) { printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev)); sc->sc_dying = 1; USB_ATTACH_ERROR_RETURN; } uca.ibufsize = TRCOMBUFSZ; uca.obufsize = TRCOMBUFSZ; uca.ibufsizepad = TRCOMBUFSZ; uca.opkthdrlen = 0; uca.device = sc->sc_udev; uca.iface = sc->sc_iface; uca.methods = &trcom_methods; uca.arg = sc; uca.info = NULL; usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, USBDEV(sc->sc_dev)); sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca, ucomprint, ucomsubmatch); USB_ATTACH_SUCCESS_RETURN; } USB_DETACH(trcom) { USB_DETACH_START(trcom, sc); int rv = 0; sc->sc_dying = 1; if (sc->sc_subdev != NULL) { rv = config_detach(sc->sc_subdev, flags); sc->sc_subdev = NULL; } usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, USBDEV(sc->sc_dev)); return (rv); } int trcom_activate(device_ptr_t self, enum devact act) { struct trcom_softc *sc = (struct trcom_softc *)self; int rv = 0; switch (act) { case DVACT_ACTIVATE: break; case DVACT_DEACTIVATE: if (sc->sc_subdev != NULL) rv = config_deactivate(sc->sc_subdev); sc->sc_dying = 1; break; } return (rv); } Static int trcom_open(void *vsc, int portno) { unsigned char data[2] = { TRCOM_OPEN_CLOSE, TRCOM_UART_ENABLE }; struct trcom_softc *sc = vsc; usb_device_request_t req; usbd_status err; if (sc->sc_dying) return (EIO); req.bmRequestType = TRCOM_CTRL; req.bRequest = 0x0b; USETW(req.wValue, 0x00); USETW(req.wIndex, portno); USETW(req.wLength, 2); err = usbd_do_request(sc->sc_udev, &req, (void*)data); if (err) return (EIO); return (0); } Static void trcom_close(void *vsc, int portno) { unsigned char data[2] = { TRCOM_OPEN_CLOSE, TRCOM_UART_DISABLE }; struct trcom_softc *sc = vsc; usb_device_request_t req; if (sc->sc_dying) return; req.bmRequestType = TRCOM_CTRL; req.bRequest = 0x0b; USETW(req.wValue, 0x00); USETW(req.wIndex, portno); USETW(req.wLength, 2); usbd_do_request(sc->sc_udev, &req, (void*)data); } Static void trcom_set(void *vsc, int portno, int reg, int onoff) { #if 0 unsigned char data[2] = { 0x01, 0x03 }; /* DTR_RTS */ struct trcom_softc *sc = vsc; usb_device_request_t req; switch (reg) { case UCOM_SET_DTR: case UCOM_SET_RTS: break; case UCOM_SET_BREAK: trcom_break(sc, portno, onoff); return; default: return; } req.bmRequestType = TRCOM_CTRL; req.bRequest = 0x0b; USETW(req.wValue, 0x00); USETW(req.wIndex, portno); USETW(req.wLength, 2); usbd_do_request(sc->sc_udev, &req, (void*)data); #endif } Static int trcom_param(void *vsc, int portno, struct termios *t) { #if 0 /* XXX flow control */ if (ISSET(t->c_cflag, CRTSCTS)) /* rts/cts flow ctl */ } else if (ISSET(t->c_iflag, IXON|IXOFF)) { /* xon/xoff flow ctl */ } else { /* disable flow ctl */ } #endif return (0); } void trcom_get_status(void *vsc, int portno, u_char *lsr, u_char *msr) { struct trcom_softc *sc = vsc; if (msr != NULL) *msr = sc->sc_msr; if (lsr != NULL) *lsr = sc->sc_lsr; } #if 0 void trcom_break(void *vsc, int portno, int onoff) { unsigned char data[3] = { 0x00, 0x04, 0x01 } ; struct trcom_softc *sc = vsc; usb_device_request_t req; data[2] = onoff ? TRCOM_BREAK_ON : TRCOM_BREAK_OFF; req.bmRequestType = TRCOM_CTRL; req.bRequest = 0x0b; USETW(req.wValue, 0x00); USETW(req.wIndex, portno); USETW(req.wLength, 3); usbd_do_request(sc->sc_udev, &req, (void*)data); } #endif /* EOF */