| 1 | /* $NetBSD: uvscom.c,v 1.30 2016/07/07 06:55:42 msaitoh Exp $ */ |
| 2 | /*- |
| 3 | * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 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 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 25 | * SUCH DAMAGE. |
| 26 | * |
| 27 | * $FreeBSD: src/sys/dev/usb/uvscom.c,v 1.1 2002/03/18 18:23:39 joe Exp $ |
| 28 | */ |
| 29 | |
| 30 | /* |
| 31 | * uvscom: SUNTAC Slipper U VS-10U driver. |
| 32 | * Slipper U is a PC card to USB converter for data communication card |
| 33 | * adapter. It supports DDI Pocket's Air H" C@rd, C@rd H" 64, NTT's P-in, |
| 34 | * P-in m@ater and various data communication card adapters. |
| 35 | */ |
| 36 | |
| 37 | #include <sys/cdefs.h> |
| 38 | __KERNEL_RCSID(0, "$NetBSD: uvscom.c,v 1.30 2016/07/07 06:55:42 msaitoh Exp $" ); |
| 39 | |
| 40 | #include <sys/param.h> |
| 41 | #include <sys/systm.h> |
| 42 | #include <sys/kernel.h> |
| 43 | #include <sys/kmem.h> |
| 44 | #include <sys/fcntl.h> |
| 45 | #include <sys/conf.h> |
| 46 | #include <sys/tty.h> |
| 47 | #include <sys/file.h> |
| 48 | #include <sys/ioctl.h> |
| 49 | #include <sys/device.h> |
| 50 | #include <sys/proc.h> |
| 51 | #include <sys/poll.h> |
| 52 | |
| 53 | #include <dev/usb/usb.h> |
| 54 | #include <dev/usb/usbcdc.h> |
| 55 | |
| 56 | #include <dev/usb/usbdi.h> |
| 57 | #include <dev/usb/usbdi_util.h> |
| 58 | #include <dev/usb/usbdevs.h> |
| 59 | #include <dev/usb/usb_quirks.h> |
| 60 | |
| 61 | #include <dev/usb/ucomvar.h> |
| 62 | |
| 63 | #ifdef UVSCOM_DEBUG |
| 64 | static int uvscomdebug = 1; |
| 65 | |
| 66 | #define DPRINTFN(n, x) do { \ |
| 67 | if (uvscomdebug > (n)) \ |
| 68 | printf x; \ |
| 69 | } while (0) |
| 70 | #else |
| 71 | #define DPRINTFN(n, x) |
| 72 | #endif |
| 73 | #define DPRINTF(x) DPRINTFN(0, x) |
| 74 | |
| 75 | #define UVSCOM_CONFIG_INDEX 0 |
| 76 | #define UVSCOM_IFACE_INDEX 0 |
| 77 | |
| 78 | #define UVSCOM_INTR_INTERVAL 100 /* mS */ |
| 79 | |
| 80 | #define UVSCOM_UNIT_WAIT 5 |
| 81 | |
| 82 | /* Request */ |
| 83 | #define UVSCOM_SET_SPEED 0x10 |
| 84 | #define UVSCOM_LINE_CTL 0x11 |
| 85 | #define UVSCOM_SET_PARAM 0x12 |
| 86 | #define UVSCOM_READ_STATUS 0xd0 |
| 87 | #define UVSCOM_SHUTDOWN 0xe0 |
| 88 | |
| 89 | /* UVSCOM_SET_SPEED parameters */ |
| 90 | #define UVSCOM_SPEED_150BPS 0x00 |
| 91 | #define UVSCOM_SPEED_300BPS 0x01 |
| 92 | #define UVSCOM_SPEED_600BPS 0x02 |
| 93 | #define UVSCOM_SPEED_1200BPS 0x03 |
| 94 | #define UVSCOM_SPEED_2400BPS 0x04 |
| 95 | #define UVSCOM_SPEED_4800BPS 0x05 |
| 96 | #define UVSCOM_SPEED_9600BPS 0x06 |
| 97 | #define UVSCOM_SPEED_19200BPS 0x07 |
| 98 | #define UVSCOM_SPEED_38400BPS 0x08 |
| 99 | #define UVSCOM_SPEED_57600BPS 0x09 |
| 100 | #define UVSCOM_SPEED_115200BPS 0x0a |
| 101 | |
| 102 | /* UVSCOM_LINE_CTL parameters */ |
| 103 | #define UVSCOM_BREAK 0x40 |
| 104 | #define UVSCOM_RTS 0x02 |
| 105 | #define UVSCOM_DTR 0x01 |
| 106 | #define UVSCOM_LINE_INIT 0x08 |
| 107 | |
| 108 | /* UVSCOM_SET_PARAM parameters */ |
| 109 | #define UVSCOM_DATA_MASK 0x03 |
| 110 | #define UVSCOM_DATA_BIT_8 0x03 |
| 111 | #define UVSCOM_DATA_BIT_7 0x02 |
| 112 | #define UVSCOM_DATA_BIT_6 0x01 |
| 113 | #define UVSCOM_DATA_BIT_5 0x00 |
| 114 | |
| 115 | #define UVSCOM_STOP_MASK 0x04 |
| 116 | #define UVSCOM_STOP_BIT_2 0x04 |
| 117 | #define UVSCOM_STOP_BIT_1 0x00 |
| 118 | |
| 119 | #define UVSCOM_PARITY_MASK 0x18 |
| 120 | #define UVSCOM_PARITY_EVEN 0x18 |
| 121 | #if 0 |
| 122 | #define UVSCOM_PARITY_UNK 0x10 |
| 123 | #endif |
| 124 | #define UVSCOM_PARITY_ODD 0x08 |
| 125 | #define UVSCOM_PARITY_NONE 0x00 |
| 126 | |
| 127 | /* Status bits */ |
| 128 | #define UVSCOM_TXRDY 0x04 |
| 129 | #define UVSCOM_RXRDY 0x01 |
| 130 | |
| 131 | #define UVSCOM_DCD 0x08 |
| 132 | #define UVSCOM_NOCARD 0x04 |
| 133 | #define UVSCOM_DSR 0x02 |
| 134 | #define UVSCOM_CTS 0x01 |
| 135 | #define UVSCOM_USTAT_MASK (UVSCOM_NOCARD | UVSCOM_DSR | UVSCOM_CTS) |
| 136 | |
| 137 | struct uvscom_softc { |
| 138 | device_t sc_dev; /* base device */ |
| 139 | struct usbd_device * sc_udev; /* USB device */ |
| 140 | struct usbd_interface * sc_iface; /* interface */ |
| 141 | int sc_iface_number;/* interface number */ |
| 142 | |
| 143 | struct usbd_interface * sc_intr_iface; /* interrupt interface */ |
| 144 | int sc_intr_number; /* interrupt number */ |
| 145 | struct usbd_pipe * sc_intr_pipe; /* interrupt pipe */ |
| 146 | u_char *sc_intr_buf; /* interrupt buffer */ |
| 147 | int sc_isize; |
| 148 | |
| 149 | u_char sc_dtr; /* current DTR state */ |
| 150 | u_char sc_rts; /* current RTS state */ |
| 151 | |
| 152 | u_char sc_lsr; /* Local status register */ |
| 153 | u_char sc_msr; /* uvscom status register */ |
| 154 | |
| 155 | uint16_t sc_lcr; /* Line control */ |
| 156 | u_char sc_usr; /* unit status */ |
| 157 | |
| 158 | device_t sc_subdev; /* ucom device */ |
| 159 | u_char sc_dying; /* disconnecting */ |
| 160 | }; |
| 161 | |
| 162 | /* |
| 163 | * These are the maximum number of bytes transferred per frame. |
| 164 | * The output buffer size cannot be increased due to the size encoding. |
| 165 | */ |
| 166 | #define UVSCOMIBUFSIZE 512 |
| 167 | #define UVSCOMOBUFSIZE 64 |
| 168 | |
| 169 | Static usbd_status uvscom_readstat(struct uvscom_softc *); |
| 170 | Static usbd_status uvscom_shutdown(struct uvscom_softc *); |
| 171 | Static usbd_status uvscom_reset(struct uvscom_softc *); |
| 172 | Static usbd_status uvscom_set_line_coding(struct uvscom_softc *, |
| 173 | uint16_t, uint16_t); |
| 174 | Static usbd_status uvscom_set_line(struct uvscom_softc *, uint16_t); |
| 175 | Static usbd_status uvscom_set_crtscts(struct uvscom_softc *); |
| 176 | Static void uvscom_get_status(void *, int, u_char *, u_char *); |
| 177 | Static void uvscom_dtr(struct uvscom_softc *, int); |
| 178 | Static void uvscom_rts(struct uvscom_softc *, int); |
| 179 | Static void uvscom_break(struct uvscom_softc *, int); |
| 180 | |
| 181 | Static void uvscom_set(void *, int, int, int); |
| 182 | Static void uvscom_intr(struct usbd_xfer *, void *, usbd_status); |
| 183 | Static int uvscom_param(void *, int, struct termios *); |
| 184 | Static int uvscom_open(void *, int); |
| 185 | Static void uvscom_close(void *, int); |
| 186 | |
| 187 | struct ucom_methods uvscom_methods = { |
| 188 | .ucom_get_status = uvscom_get_status, |
| 189 | .ucom_set = uvscom_set, |
| 190 | .ucom_param = uvscom_param, |
| 191 | .ucom_ioctl = NULL, /* TODO */ |
| 192 | .ucom_open = uvscom_open, |
| 193 | .ucom_close = uvscom_close, |
| 194 | .ucom_read = NULL, |
| 195 | .ucom_write = NULL |
| 196 | }; |
| 197 | |
| 198 | static const struct usb_devno uvscom_devs [] = { |
| 199 | /* SUNTAC U-Cable type A4 */ |
| 200 | { USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_AS144L4 }, |
| 201 | /* SUNTAC U-Cable type D2 */ |
| 202 | { USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_DS96L }, |
| 203 | /* SUNTAC U-Cable type P1 */ |
| 204 | { USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_PS64P1 }, |
| 205 | /* SUNTAC Slipper U */ |
| 206 | { USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_VS10U }, |
| 207 | /* SUNTAC Ir-Trinity */ |
| 208 | { USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_IS96U }, |
| 209 | }; |
| 210 | #define uvscom_lookup(v, p) usb_lookup(uvscom_devs, v, p) |
| 211 | |
| 212 | int uvscom_match(device_t, cfdata_t, void *); |
| 213 | void uvscom_attach(device_t, device_t, void *); |
| 214 | void uvscom_childdet(device_t, device_t); |
| 215 | int uvscom_detach(device_t, int); |
| 216 | int uvscom_activate(device_t, enum devact); |
| 217 | extern struct cfdriver uvscom_cd; |
| 218 | CFATTACH_DECL2_NEW(uvscom, sizeof(struct uvscom_softc), uvscom_match, |
| 219 | uvscom_attach, uvscom_detach, uvscom_activate, NULL, uvscom_childdet); |
| 220 | |
| 221 | int |
| 222 | uvscom_match(device_t parent, cfdata_t match, void *aux) |
| 223 | { |
| 224 | struct usb_attach_arg *uaa = aux; |
| 225 | |
| 226 | return uvscom_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ? |
| 227 | UMATCH_VENDOR_PRODUCT : UMATCH_NONE; |
| 228 | } |
| 229 | |
| 230 | void |
| 231 | uvscom_attach(device_t parent, device_t self, void *aux) |
| 232 | { |
| 233 | struct uvscom_softc *sc = device_private(self); |
| 234 | struct usb_attach_arg *uaa = aux; |
| 235 | struct usbd_device *dev = uaa->uaa_device; |
| 236 | usb_config_descriptor_t *cdesc; |
| 237 | usb_interface_descriptor_t *id; |
| 238 | usb_endpoint_descriptor_t *ed; |
| 239 | char *devinfop; |
| 240 | usbd_status err; |
| 241 | int i; |
| 242 | struct ucom_attach_args ucaa; |
| 243 | |
| 244 | aprint_naive("\n" ); |
| 245 | aprint_normal("\n" ); |
| 246 | |
| 247 | devinfop = usbd_devinfo_alloc(dev, 0); |
| 248 | aprint_normal_dev(self, "%s\n" , devinfop); |
| 249 | usbd_devinfo_free(devinfop); |
| 250 | |
| 251 | sc->sc_dev = self; |
| 252 | sc->sc_udev = dev; |
| 253 | |
| 254 | DPRINTF(("uvscom attach: sc = %p\n" , sc)); |
| 255 | |
| 256 | /* initialize endpoints */ |
| 257 | ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1; |
| 258 | sc->sc_intr_number = -1; |
| 259 | sc->sc_intr_pipe = NULL; |
| 260 | |
| 261 | /* Move the device into the configured state. */ |
| 262 | err = usbd_set_config_index(dev, UVSCOM_CONFIG_INDEX, 1); |
| 263 | if (err) { |
| 264 | aprint_error_dev(self, "failed to set configuration, err=%s\n" , |
| 265 | usbd_errstr(err)); |
| 266 | sc->sc_dying = 1; |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | /* get the config descriptor */ |
| 271 | cdesc = usbd_get_config_descriptor(sc->sc_udev); |
| 272 | |
| 273 | if (cdesc == NULL) { |
| 274 | aprint_error_dev(self, |
| 275 | "failed to get configuration descriptor\n" ); |
| 276 | sc->sc_dying = 1; |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | /* get the common interface */ |
| 281 | err = usbd_device2interface_handle(dev, UVSCOM_IFACE_INDEX, |
| 282 | &sc->sc_iface); |
| 283 | if (err) { |
| 284 | aprint_error_dev(self, "failed to get interface, err=%s\n" , |
| 285 | usbd_errstr(err)); |
| 286 | sc->sc_dying = 1; |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | id = usbd_get_interface_descriptor(sc->sc_iface); |
| 291 | sc->sc_iface_number = id->bInterfaceNumber; |
| 292 | |
| 293 | /* Find endpoints */ |
| 294 | for (i = 0; i < id->bNumEndpoints; i++) { |
| 295 | ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); |
| 296 | if (ed == NULL) { |
| 297 | aprint_error_dev(self, |
| 298 | "no endpoint descriptor for %d\n" , i); |
| 299 | sc->sc_dying = 1; |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && |
| 304 | UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { |
| 305 | ucaa.ucaa_bulkin = ed->bEndpointAddress; |
| 306 | } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && |
| 307 | UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { |
| 308 | ucaa.ucaa_bulkout = ed->bEndpointAddress; |
| 309 | } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && |
| 310 | UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { |
| 311 | sc->sc_intr_number = ed->bEndpointAddress; |
| 312 | sc->sc_isize = UGETW(ed->wMaxPacketSize); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | if (ucaa.ucaa_bulkin == -1) { |
| 317 | aprint_error_dev(self, "Could not find data bulk in\n" ); |
| 318 | sc->sc_dying = 1; |
| 319 | return; |
| 320 | } |
| 321 | if (ucaa.ucaa_bulkout == -1) { |
| 322 | aprint_error_dev(self, "Could not find data bulk out\n" ); |
| 323 | sc->sc_dying = 1; |
| 324 | return; |
| 325 | } |
| 326 | if (sc->sc_intr_number == -1) { |
| 327 | aprint_error_dev(self, "Could not find interrupt in\n" ); |
| 328 | sc->sc_dying = 1; |
| 329 | return; |
| 330 | } |
| 331 | |
| 332 | sc->sc_dtr = sc->sc_rts = 0; |
| 333 | sc->sc_lcr = UVSCOM_LINE_INIT; |
| 334 | |
| 335 | ucaa.ucaa_portno = UCOM_UNK_PORTNO; |
| 336 | /* ucaa_bulkin, ucaa_bulkout set above */ |
| 337 | ucaa.ucaa_ibufsize = UVSCOMIBUFSIZE; |
| 338 | ucaa.ucaa_obufsize = UVSCOMOBUFSIZE; |
| 339 | ucaa.ucaa_ibufsizepad = UVSCOMIBUFSIZE; |
| 340 | ucaa.ucaa_opkthdrlen = 0; |
| 341 | ucaa.ucaa_device = dev; |
| 342 | ucaa.ucaa_iface = sc->sc_iface; |
| 343 | ucaa.ucaa_methods = &uvscom_methods; |
| 344 | ucaa.ucaa_arg = sc; |
| 345 | ucaa.ucaa_info = NULL; |
| 346 | |
| 347 | err = uvscom_reset(sc); |
| 348 | |
| 349 | if (err) { |
| 350 | aprint_error_dev(self, "reset failed, %s\n" , usbd_errstr(err)); |
| 351 | sc->sc_dying = 1; |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | DPRINTF(("uvscom: in = 0x%x out = 0x%x intr = 0x%x\n" , |
| 356 | ucaa.ucaa_bulkin, ucaa.ucaa_bulkout, sc->sc_intr_number)); |
| 357 | |
| 358 | usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); |
| 359 | |
| 360 | DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n" , |
| 361 | ucaa.ucaa_bulkin, ucaa.ucaa_bulkout, sc->sc_intr_number )); |
| 362 | sc->sc_subdev = config_found_sm_loc(self, "ucombus" , NULL, &ucaa, |
| 363 | ucomprint, ucomsubmatch); |
| 364 | |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | void |
| 369 | uvscom_childdet(device_t self, device_t child) |
| 370 | { |
| 371 | struct uvscom_softc *sc = device_private(self); |
| 372 | |
| 373 | KASSERT(sc->sc_subdev == child); |
| 374 | sc->sc_subdev = NULL; |
| 375 | } |
| 376 | |
| 377 | int |
| 378 | uvscom_detach(device_t self, int flags) |
| 379 | { |
| 380 | struct uvscom_softc *sc = device_private(self); |
| 381 | int rv = 0; |
| 382 | |
| 383 | DPRINTF(("uvscom_detach: sc = %p\n" , sc)); |
| 384 | |
| 385 | sc->sc_dying = 1; |
| 386 | |
| 387 | if (sc->sc_intr_pipe != NULL) { |
| 388 | usbd_abort_pipe(sc->sc_intr_pipe); |
| 389 | usbd_close_pipe(sc->sc_intr_pipe); |
| 390 | kmem_free(sc->sc_intr_buf, sc->sc_isize); |
| 391 | sc->sc_intr_pipe = NULL; |
| 392 | } |
| 393 | |
| 394 | sc->sc_dying = 1; |
| 395 | if (sc->sc_subdev != NULL) |
| 396 | rv = config_detach(sc->sc_subdev, flags); |
| 397 | |
| 398 | usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); |
| 399 | |
| 400 | return rv; |
| 401 | } |
| 402 | |
| 403 | int |
| 404 | uvscom_activate(device_t self, enum devact act) |
| 405 | { |
| 406 | struct uvscom_softc *sc = device_private(self); |
| 407 | |
| 408 | switch (act) { |
| 409 | case DVACT_DEACTIVATE: |
| 410 | sc->sc_dying = 1; |
| 411 | return 0; |
| 412 | default: |
| 413 | return EOPNOTSUPP; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | Static usbd_status |
| 418 | uvscom_readstat(struct uvscom_softc *sc) |
| 419 | { |
| 420 | usb_device_request_t req; |
| 421 | usbd_status err; |
| 422 | uint16_t r; |
| 423 | |
| 424 | DPRINTF(("%s: send readstat\n" , device_xname(sc->sc_dev))); |
| 425 | |
| 426 | req.bmRequestType = UT_READ_VENDOR_DEVICE; |
| 427 | req.bRequest = UVSCOM_READ_STATUS; |
| 428 | USETW(req.wValue, 0); |
| 429 | USETW(req.wIndex, 0); |
| 430 | USETW(req.wLength, 2); |
| 431 | |
| 432 | err = usbd_do_request(sc->sc_udev, &req, &r); |
| 433 | if (err) { |
| 434 | aprint_error_dev(sc->sc_dev, "uvscom_readstat: %s\n" , |
| 435 | usbd_errstr(err)); |
| 436 | return err; |
| 437 | } |
| 438 | |
| 439 | DPRINTF(("%s: uvscom_readstat: r = %d\n" , |
| 440 | device_xname(sc->sc_dev), r)); |
| 441 | |
| 442 | return USBD_NORMAL_COMPLETION; |
| 443 | } |
| 444 | |
| 445 | Static usbd_status |
| 446 | uvscom_shutdown(struct uvscom_softc *sc) |
| 447 | { |
| 448 | usb_device_request_t req; |
| 449 | usbd_status err; |
| 450 | |
| 451 | DPRINTF(("%s: send shutdown\n" , device_xname(sc->sc_dev))); |
| 452 | |
| 453 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
| 454 | req.bRequest = UVSCOM_SHUTDOWN; |
| 455 | USETW(req.wValue, 0); |
| 456 | USETW(req.wIndex, 0); |
| 457 | USETW(req.wLength, 0); |
| 458 | |
| 459 | err = usbd_do_request(sc->sc_udev, &req, NULL); |
| 460 | if (err) { |
| 461 | aprint_error_dev(sc->sc_dev, "uvscom_shutdown: %s\n" , |
| 462 | usbd_errstr(err)); |
| 463 | return err; |
| 464 | } |
| 465 | |
| 466 | return USBD_NORMAL_COMPLETION; |
| 467 | } |
| 468 | |
| 469 | Static usbd_status |
| 470 | uvscom_reset(struct uvscom_softc *sc) |
| 471 | { |
| 472 | DPRINTF(("%s: uvscom_reset\n" , device_xname(sc->sc_dev))); |
| 473 | |
| 474 | return USBD_NORMAL_COMPLETION; |
| 475 | } |
| 476 | |
| 477 | Static usbd_status |
| 478 | uvscom_set_crtscts(struct uvscom_softc *sc) |
| 479 | { |
| 480 | DPRINTF(("%s: uvscom_set_crtscts\n" , device_xname(sc->sc_dev))); |
| 481 | |
| 482 | return USBD_NORMAL_COMPLETION; |
| 483 | } |
| 484 | |
| 485 | Static usbd_status |
| 486 | uvscom_set_line(struct uvscom_softc *sc, uint16_t line) |
| 487 | { |
| 488 | usb_device_request_t req; |
| 489 | usbd_status err; |
| 490 | |
| 491 | DPRINTF(("%s: uvscom_set_line: %04x\n" , |
| 492 | device_xname(sc->sc_dev), line)); |
| 493 | |
| 494 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
| 495 | req.bRequest = UVSCOM_LINE_CTL; |
| 496 | USETW(req.wValue, line); |
| 497 | USETW(req.wIndex, 0); |
| 498 | USETW(req.wLength, 0); |
| 499 | |
| 500 | err = usbd_do_request(sc->sc_udev, &req, NULL); |
| 501 | if (err) { |
| 502 | aprint_error_dev(sc->sc_dev, "uvscom_set_line: %s\n" , |
| 503 | usbd_errstr(err)); |
| 504 | return err; |
| 505 | } |
| 506 | |
| 507 | return USBD_NORMAL_COMPLETION; |
| 508 | } |
| 509 | |
| 510 | Static usbd_status |
| 511 | uvscom_set_line_coding(struct uvscom_softc *sc, uint16_t lsp, uint16_t ls) |
| 512 | { |
| 513 | usb_device_request_t req; |
| 514 | usbd_status err; |
| 515 | |
| 516 | DPRINTF(("%s: uvscom_set_line_coding: %02x %02x\n" , |
| 517 | device_xname(sc->sc_dev), lsp, ls)); |
| 518 | |
| 519 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
| 520 | req.bRequest = UVSCOM_SET_SPEED; |
| 521 | USETW(req.wValue, lsp); |
| 522 | USETW(req.wIndex, 0); |
| 523 | USETW(req.wLength, 0); |
| 524 | |
| 525 | err = usbd_do_request(sc->sc_udev, &req, NULL); |
| 526 | if (err) { |
| 527 | aprint_error_dev(sc->sc_dev, "uvscom_set_line_coding: %s\n" , |
| 528 | usbd_errstr(err)); |
| 529 | return err; |
| 530 | } |
| 531 | |
| 532 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
| 533 | req.bRequest = UVSCOM_SET_PARAM; |
| 534 | USETW(req.wValue, ls); |
| 535 | USETW(req.wIndex, 0); |
| 536 | USETW(req.wLength, 0); |
| 537 | |
| 538 | err = usbd_do_request(sc->sc_udev, &req, NULL); |
| 539 | if (err) { |
| 540 | aprint_error_dev(sc->sc_dev, "uvscom_set_line_coding: %s\n" , |
| 541 | usbd_errstr(err)); |
| 542 | return err; |
| 543 | } |
| 544 | |
| 545 | return USBD_NORMAL_COMPLETION; |
| 546 | } |
| 547 | |
| 548 | Static void |
| 549 | uvscom_dtr(struct uvscom_softc *sc, int onoff) |
| 550 | { |
| 551 | DPRINTF(("%s: uvscom_dtr: onoff = %d\n" , |
| 552 | device_xname(sc->sc_dev), onoff)); |
| 553 | |
| 554 | if (sc->sc_dtr == onoff) |
| 555 | return; /* no change */ |
| 556 | |
| 557 | sc->sc_dtr = onoff; |
| 558 | |
| 559 | if (onoff) |
| 560 | SET(sc->sc_lcr, UVSCOM_DTR); |
| 561 | else |
| 562 | CLR(sc->sc_lcr, UVSCOM_DTR); |
| 563 | |
| 564 | uvscom_set_line(sc, sc->sc_lcr); |
| 565 | } |
| 566 | |
| 567 | Static void |
| 568 | uvscom_rts(struct uvscom_softc *sc, int onoff) |
| 569 | { |
| 570 | DPRINTF(("%s: uvscom_rts: onoff = %d\n" , |
| 571 | device_xname(sc->sc_dev), onoff)); |
| 572 | |
| 573 | if (sc->sc_rts == onoff) |
| 574 | return; /* no change */ |
| 575 | |
| 576 | sc->sc_rts = onoff; |
| 577 | |
| 578 | if (onoff) |
| 579 | SET(sc->sc_lcr, UVSCOM_RTS); |
| 580 | else |
| 581 | CLR(sc->sc_lcr, UVSCOM_RTS); |
| 582 | |
| 583 | uvscom_set_line(sc, sc->sc_lcr); |
| 584 | } |
| 585 | |
| 586 | Static void |
| 587 | uvscom_break(struct uvscom_softc *sc, int onoff) |
| 588 | { |
| 589 | DPRINTF(("%s: uvscom_break: onoff = %d\n" , |
| 590 | device_xname(sc->sc_dev), onoff)); |
| 591 | |
| 592 | if (onoff) |
| 593 | uvscom_set_line(sc, SET(sc->sc_lcr, UVSCOM_BREAK)); |
| 594 | } |
| 595 | |
| 596 | Static void |
| 597 | uvscom_set(void *addr, int portno, int reg, int onoff) |
| 598 | { |
| 599 | struct uvscom_softc *sc = addr; |
| 600 | |
| 601 | switch (reg) { |
| 602 | case UCOM_SET_DTR: |
| 603 | uvscom_dtr(sc, onoff); |
| 604 | break; |
| 605 | case UCOM_SET_RTS: |
| 606 | uvscom_rts(sc, onoff); |
| 607 | break; |
| 608 | case UCOM_SET_BREAK: |
| 609 | uvscom_break(sc, onoff); |
| 610 | break; |
| 611 | default: |
| 612 | break; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | Static int |
| 617 | uvscom_param(void *addr, int portno, struct termios *t) |
| 618 | { |
| 619 | struct uvscom_softc *sc = addr; |
| 620 | usbd_status err; |
| 621 | uint16_t lsp; |
| 622 | uint16_t ls; |
| 623 | |
| 624 | DPRINTF(("%s: uvscom_param: sc = %p\n" , |
| 625 | device_xname(sc->sc_dev), sc)); |
| 626 | |
| 627 | ls = 0; |
| 628 | |
| 629 | switch (t->c_ospeed) { |
| 630 | case B150: |
| 631 | lsp = UVSCOM_SPEED_150BPS; |
| 632 | break; |
| 633 | case B300: |
| 634 | lsp = UVSCOM_SPEED_300BPS; |
| 635 | break; |
| 636 | case B600: |
| 637 | lsp = UVSCOM_SPEED_600BPS; |
| 638 | break; |
| 639 | case B1200: |
| 640 | lsp = UVSCOM_SPEED_1200BPS; |
| 641 | break; |
| 642 | case B2400: |
| 643 | lsp = UVSCOM_SPEED_2400BPS; |
| 644 | break; |
| 645 | case B4800: |
| 646 | lsp = UVSCOM_SPEED_4800BPS; |
| 647 | break; |
| 648 | case B9600: |
| 649 | lsp = UVSCOM_SPEED_9600BPS; |
| 650 | break; |
| 651 | case B19200: |
| 652 | lsp = UVSCOM_SPEED_19200BPS; |
| 653 | break; |
| 654 | case B38400: |
| 655 | lsp = UVSCOM_SPEED_38400BPS; |
| 656 | break; |
| 657 | case B57600: |
| 658 | lsp = UVSCOM_SPEED_57600BPS; |
| 659 | break; |
| 660 | case B115200: |
| 661 | lsp = UVSCOM_SPEED_115200BPS; |
| 662 | break; |
| 663 | default: |
| 664 | return EIO; |
| 665 | } |
| 666 | |
| 667 | if (ISSET(t->c_cflag, CSTOPB)) |
| 668 | SET(ls, UVSCOM_STOP_BIT_2); |
| 669 | else |
| 670 | SET(ls, UVSCOM_STOP_BIT_1); |
| 671 | |
| 672 | if (ISSET(t->c_cflag, PARENB)) { |
| 673 | if (ISSET(t->c_cflag, PARODD)) |
| 674 | SET(ls, UVSCOM_PARITY_ODD); |
| 675 | else |
| 676 | SET(ls, UVSCOM_PARITY_EVEN); |
| 677 | } else |
| 678 | SET(ls, UVSCOM_PARITY_NONE); |
| 679 | |
| 680 | switch (ISSET(t->c_cflag, CSIZE)) { |
| 681 | case CS5: |
| 682 | SET(ls, UVSCOM_DATA_BIT_5); |
| 683 | break; |
| 684 | case CS6: |
| 685 | SET(ls, UVSCOM_DATA_BIT_6); |
| 686 | break; |
| 687 | case CS7: |
| 688 | SET(ls, UVSCOM_DATA_BIT_7); |
| 689 | break; |
| 690 | case CS8: |
| 691 | SET(ls, UVSCOM_DATA_BIT_8); |
| 692 | break; |
| 693 | default: |
| 694 | return EIO; |
| 695 | } |
| 696 | |
| 697 | err = uvscom_set_line_coding(sc, lsp, ls); |
| 698 | if (err) |
| 699 | return EIO; |
| 700 | |
| 701 | if (ISSET(t->c_cflag, CRTSCTS)) { |
| 702 | err = uvscom_set_crtscts(sc); |
| 703 | if (err) |
| 704 | return EIO; |
| 705 | } |
| 706 | |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | Static int |
| 711 | uvscom_open(void *addr, int portno) |
| 712 | { |
| 713 | struct uvscom_softc *sc = addr; |
| 714 | int err; |
| 715 | int i; |
| 716 | |
| 717 | if (sc->sc_dying) |
| 718 | return EIO; |
| 719 | |
| 720 | DPRINTF(("uvscom_open: sc = %p\n" , sc)); |
| 721 | |
| 722 | if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) { |
| 723 | DPRINTF(("uvscom_open: open interrupt pipe.\n" )); |
| 724 | |
| 725 | sc->sc_usr = 0; /* clear unit status */ |
| 726 | |
| 727 | err = uvscom_readstat(sc); |
| 728 | if (err) { |
| 729 | DPRINTF(("%s: uvscom_open: readstat faild\n" , |
| 730 | device_xname(sc->sc_dev))); |
| 731 | return EIO; |
| 732 | } |
| 733 | |
| 734 | sc->sc_intr_buf = kmem_alloc(sc->sc_isize, KM_SLEEP); |
| 735 | err = usbd_open_pipe_intr(sc->sc_iface, |
| 736 | sc->sc_intr_number, |
| 737 | USBD_SHORT_XFER_OK, |
| 738 | &sc->sc_intr_pipe, |
| 739 | sc, |
| 740 | sc->sc_intr_buf, |
| 741 | sc->sc_isize, |
| 742 | uvscom_intr, |
| 743 | UVSCOM_INTR_INTERVAL); |
| 744 | if (err) { |
| 745 | aprint_error_dev(sc->sc_dev, |
| 746 | "cannot open interrupt pipe (addr %d)\n" , |
| 747 | sc->sc_intr_number); |
| 748 | return EIO; |
| 749 | } |
| 750 | } else { |
| 751 | DPRINTF(("uvscom_open: did not open interrupt pipe.\n" )); |
| 752 | } |
| 753 | |
| 754 | if ((sc->sc_usr & UVSCOM_USTAT_MASK) == 0) { |
| 755 | /* unit is not ready */ |
| 756 | |
| 757 | for (i = UVSCOM_UNIT_WAIT; i > 0; --i) { |
| 758 | tsleep(&err, TTIPRI, "uvsop" , hz); /* XXX */ |
| 759 | if (ISSET(sc->sc_usr, UVSCOM_USTAT_MASK)) |
| 760 | break; |
| 761 | } |
| 762 | if (i == 0) { |
| 763 | DPRINTF(("%s: unit is not ready\n" , |
| 764 | device_xname(sc->sc_dev))); |
| 765 | return EIO; |
| 766 | } |
| 767 | |
| 768 | /* check PC card was inserted */ |
| 769 | if (ISSET(sc->sc_usr, UVSCOM_NOCARD)) { |
| 770 | DPRINTF(("%s: no card\n" , |
| 771 | device_xname(sc->sc_dev))); |
| 772 | return EIO; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | return 0; |
| 777 | } |
| 778 | |
| 779 | Static void |
| 780 | uvscom_close(void *addr, int portno) |
| 781 | { |
| 782 | struct uvscom_softc *sc = addr; |
| 783 | int err; |
| 784 | |
| 785 | if (sc->sc_dying) |
| 786 | return; |
| 787 | |
| 788 | DPRINTF(("uvscom_close: close\n" )); |
| 789 | |
| 790 | uvscom_shutdown(sc); |
| 791 | |
| 792 | if (sc->sc_intr_pipe != NULL) { |
| 793 | err = usbd_abort_pipe(sc->sc_intr_pipe); |
| 794 | if (err) |
| 795 | aprint_error_dev(sc->sc_dev, |
| 796 | "abort interrupt pipe failed: %s\n" , |
| 797 | usbd_errstr(err)); |
| 798 | err = usbd_close_pipe(sc->sc_intr_pipe); |
| 799 | if (err) |
| 800 | aprint_error_dev(sc->sc_dev, |
| 801 | "lose interrupt pipe failed: %s\n" , |
| 802 | usbd_errstr(err)); |
| 803 | kmem_free(sc->sc_intr_buf, sc->sc_isize); |
| 804 | sc->sc_intr_pipe = NULL; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | Static void |
| 809 | uvscom_intr(struct usbd_xfer *xfer, void *priv, |
| 810 | usbd_status status) |
| 811 | { |
| 812 | struct uvscom_softc *sc = priv; |
| 813 | u_char *buf = sc->sc_intr_buf; |
| 814 | u_char pstatus; |
| 815 | |
| 816 | if (sc->sc_dying) |
| 817 | return; |
| 818 | |
| 819 | if (status != USBD_NORMAL_COMPLETION) { |
| 820 | if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) |
| 821 | return; |
| 822 | |
| 823 | aprint_error_dev(sc->sc_dev, |
| 824 | "uvscom_intr: abnormal status: %s\n" , |
| 825 | usbd_errstr(status)); |
| 826 | usbd_clear_endpoint_stall_async(sc->sc_intr_pipe); |
| 827 | return; |
| 828 | } |
| 829 | |
| 830 | DPRINTFN(2, ("%s: uvscom status = %02x %02x\n" , |
| 831 | device_xname(sc->sc_dev), buf[0], buf[1])); |
| 832 | |
| 833 | sc->sc_lsr = sc->sc_msr = 0; |
| 834 | sc->sc_usr = buf[1]; |
| 835 | |
| 836 | pstatus = buf[0]; |
| 837 | if (ISSET(pstatus, UVSCOM_TXRDY)) |
| 838 | SET(sc->sc_lsr, ULSR_TXRDY); |
| 839 | if (ISSET(pstatus, UVSCOM_RXRDY)) |
| 840 | SET(sc->sc_lsr, ULSR_RXRDY); |
| 841 | |
| 842 | pstatus = buf[1]; |
| 843 | if (ISSET(pstatus, UVSCOM_CTS)) |
| 844 | SET(sc->sc_msr, UMSR_CTS); |
| 845 | if (ISSET(pstatus, UVSCOM_DSR)) |
| 846 | SET(sc->sc_msr, UMSR_DSR); |
| 847 | if (ISSET(pstatus, UVSCOM_DCD)) |
| 848 | SET(sc->sc_msr, UMSR_DCD); |
| 849 | |
| 850 | ucom_status_change(device_private(sc->sc_subdev)); |
| 851 | } |
| 852 | |
| 853 | Static void |
| 854 | uvscom_get_status(void *addr, int portno, u_char *lsr, u_char *msr) |
| 855 | { |
| 856 | struct uvscom_softc *sc = addr; |
| 857 | |
| 858 | if (lsr != NULL) |
| 859 | *lsr = sc->sc_lsr; |
| 860 | if (msr != NULL) |
| 861 | *msr = sc->sc_msr; |
| 862 | } |
| 863 | |
| 864 | |