1 /* $OpenBSD: uark.c,v 1.1 2006/08/14 08:30:22 jsg Exp $ */ 2 3 /* 4 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 * 18 * $FreeBSD$ 19 */ 20 21 /* 22 * NOTE: all function names beginning like "uark_cfg_" can only 23 * be called from within the config thread function ! 24 */ 25 26 27 #include <sys/stdint.h> 28 #include <sys/stddef.h> 29 #include <sys/param.h> 30 #include <sys/queue.h> 31 #include <sys/types.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/bus.h> 35 #include <sys/module.h> 36 #include <sys/lock.h> 37 #include <sys/mutex.h> 38 #include <sys/condvar.h> 39 #include <sys/sysctl.h> 40 #include <sys/sx.h> 41 #include <sys/unistd.h> 42 #include <sys/callout.h> 43 #include <sys/malloc.h> 44 #include <sys/priv.h> 45 46 #include <dev/usb/usb.h> 47 #include <dev/usb/usbdi.h> 48 #include <dev/usb/usbdi_util.h> 49 #include <dev/usb/usbhid.h> 50 #include "usbdevs.h" 51 52 #define USB_DEBUG_VAR usb_debug 53 #include <dev/usb/usb_debug.h> 54 #include <dev/usb/usb_process.h> 55 56 #include <dev/usb/serial/usb_serial.h> 57 58 #define UARK_BUF_SIZE 1024 /* bytes */ 59 60 #define UARK_SET_DATA_BITS(x) ((x) - 5) 61 62 #define UARK_PARITY_NONE 0x00 63 #define UARK_PARITY_ODD 0x08 64 #define UARK_PARITY_EVEN 0x18 65 66 #define UARK_STOP_BITS_1 0x00 67 #define UARK_STOP_BITS_2 0x04 68 69 #define UARK_BAUD_REF 3000000 70 71 #define UARK_WRITE 0x40 72 #define UARK_READ 0xc0 73 74 #define UARK_REQUEST 0xfe 75 76 #define UARK_CONFIG_INDEX 0 77 #define UARK_IFACE_INDEX 0 78 79 enum { 80 UARK_BULK_DT_WR, 81 UARK_BULK_DT_RD, 82 UARK_N_TRANSFER, 83 }; 84 85 struct uark_softc { 86 struct ucom_super_softc sc_super_ucom; 87 struct ucom_softc sc_ucom; 88 89 struct usb_xfer *sc_xfer[UARK_N_TRANSFER]; 90 struct usb_device *sc_udev; 91 struct mtx sc_mtx; 92 93 uint8_t sc_msr; 94 uint8_t sc_lsr; 95 }; 96 97 /* prototypes */ 98 99 static device_probe_t uark_probe; 100 static device_attach_t uark_attach; 101 static device_detach_t uark_detach; 102 static void uark_free_softc(struct uark_softc *); 103 104 static usb_callback_t uark_bulk_write_callback; 105 static usb_callback_t uark_bulk_read_callback; 106 107 static void uark_free(struct ucom_softc *); 108 static void uark_start_read(struct ucom_softc *); 109 static void uark_stop_read(struct ucom_softc *); 110 static void uark_start_write(struct ucom_softc *); 111 static void uark_stop_write(struct ucom_softc *); 112 static int uark_pre_param(struct ucom_softc *, struct termios *); 113 static void uark_cfg_param(struct ucom_softc *, struct termios *); 114 static void uark_cfg_get_status(struct ucom_softc *, uint8_t *, 115 uint8_t *); 116 static void uark_cfg_set_break(struct ucom_softc *, uint8_t); 117 static void uark_cfg_write(struct uark_softc *, uint16_t, uint16_t); 118 static void uark_poll(struct ucom_softc *ucom); 119 120 static const struct usb_config 121 uark_xfer_config[UARK_N_TRANSFER] = { 122 123 [UARK_BULK_DT_WR] = { 124 .type = UE_BULK, 125 .endpoint = UE_ADDR_ANY, 126 .direction = UE_DIR_OUT, 127 .bufsize = UARK_BUF_SIZE, 128 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 129 .callback = &uark_bulk_write_callback, 130 }, 131 132 [UARK_BULK_DT_RD] = { 133 .type = UE_BULK, 134 .endpoint = UE_ADDR_ANY, 135 .direction = UE_DIR_IN, 136 .bufsize = UARK_BUF_SIZE, 137 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 138 .callback = &uark_bulk_read_callback, 139 }, 140 }; 141 142 static const struct ucom_callback uark_callback = { 143 .ucom_cfg_get_status = &uark_cfg_get_status, 144 .ucom_cfg_set_break = &uark_cfg_set_break, 145 .ucom_cfg_param = &uark_cfg_param, 146 .ucom_pre_param = &uark_pre_param, 147 .ucom_start_read = &uark_start_read, 148 .ucom_stop_read = &uark_stop_read, 149 .ucom_start_write = &uark_start_write, 150 .ucom_stop_write = &uark_stop_write, 151 .ucom_poll = &uark_poll, 152 .ucom_free = &uark_free, 153 }; 154 155 static device_method_t uark_methods[] = { 156 /* Device methods */ 157 DEVMETHOD(device_probe, uark_probe), 158 DEVMETHOD(device_attach, uark_attach), 159 DEVMETHOD(device_detach, uark_detach), 160 DEVMETHOD_END 161 }; 162 163 static devclass_t uark_devclass; 164 165 static driver_t uark_driver = { 166 .name = "uark", 167 .methods = uark_methods, 168 .size = sizeof(struct uark_softc), 169 }; 170 171 static const STRUCT_USB_HOST_ID uark_devs[] = { 172 {USB_VPI(USB_VENDOR_ARKMICRO, USB_PRODUCT_ARKMICRO_ARK3116, 0)}, 173 }; 174 175 DRIVER_MODULE(uark, uhub, uark_driver, uark_devclass, NULL, 0); 176 MODULE_DEPEND(uark, ucom, 1, 1, 1); 177 MODULE_DEPEND(uark, usb, 1, 1, 1); 178 MODULE_VERSION(uark, 1); 179 USB_PNP_HOST_INFO(uark_devs); 180 181 static int 182 uark_probe(device_t dev) 183 { 184 struct usb_attach_arg *uaa = device_get_ivars(dev); 185 186 if (uaa->usb_mode != USB_MODE_HOST) { 187 return (ENXIO); 188 } 189 if (uaa->info.bConfigIndex != 0) { 190 return (ENXIO); 191 } 192 if (uaa->info.bIfaceIndex != UARK_IFACE_INDEX) { 193 return (ENXIO); 194 } 195 return (usbd_lookup_id_by_uaa(uark_devs, sizeof(uark_devs), uaa)); 196 } 197 198 static int 199 uark_attach(device_t dev) 200 { 201 struct usb_attach_arg *uaa = device_get_ivars(dev); 202 struct uark_softc *sc = device_get_softc(dev); 203 int32_t error; 204 uint8_t iface_index; 205 206 device_set_usb_desc(dev); 207 mtx_init(&sc->sc_mtx, "uark", NULL, MTX_DEF); 208 ucom_ref(&sc->sc_super_ucom); 209 210 sc->sc_udev = uaa->device; 211 212 iface_index = UARK_IFACE_INDEX; 213 error = usbd_transfer_setup 214 (uaa->device, &iface_index, sc->sc_xfer, 215 uark_xfer_config, UARK_N_TRANSFER, sc, &sc->sc_mtx); 216 217 if (error) { 218 device_printf(dev, "allocating control USB " 219 "transfers failed\n"); 220 goto detach; 221 } 222 /* clear stall at first run */ 223 mtx_lock(&sc->sc_mtx); 224 usbd_xfer_set_stall(sc->sc_xfer[UARK_BULK_DT_WR]); 225 usbd_xfer_set_stall(sc->sc_xfer[UARK_BULK_DT_RD]); 226 mtx_unlock(&sc->sc_mtx); 227 228 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc, 229 &uark_callback, &sc->sc_mtx); 230 if (error) { 231 DPRINTF("ucom_attach failed\n"); 232 goto detach; 233 } 234 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev); 235 236 return (0); /* success */ 237 238 detach: 239 uark_detach(dev); 240 return (ENXIO); /* failure */ 241 } 242 243 static int 244 uark_detach(device_t dev) 245 { 246 struct uark_softc *sc = device_get_softc(dev); 247 248 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom); 249 usbd_transfer_unsetup(sc->sc_xfer, UARK_N_TRANSFER); 250 251 device_claim_softc(dev); 252 253 uark_free_softc(sc); 254 255 return (0); 256 } 257 258 UCOM_UNLOAD_DRAIN(uark); 259 260 static void 261 uark_free_softc(struct uark_softc *sc) 262 { 263 if (ucom_unref(&sc->sc_super_ucom)) { 264 mtx_destroy(&sc->sc_mtx); 265 device_free_softc(sc); 266 } 267 } 268 269 static void 270 uark_free(struct ucom_softc *ucom) 271 { 272 uark_free_softc(ucom->sc_parent); 273 } 274 275 static void 276 uark_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 277 { 278 struct uark_softc *sc = usbd_xfer_softc(xfer); 279 struct usb_page_cache *pc; 280 uint32_t actlen; 281 282 switch (USB_GET_STATE(xfer)) { 283 case USB_ST_SETUP: 284 case USB_ST_TRANSFERRED: 285 tr_setup: 286 pc = usbd_xfer_get_frame(xfer, 0); 287 if (ucom_get_data(&sc->sc_ucom, pc, 0, 288 UARK_BUF_SIZE, &actlen)) { 289 usbd_xfer_set_frame_len(xfer, 0, actlen); 290 usbd_transfer_submit(xfer); 291 } 292 return; 293 294 default: /* Error */ 295 if (error != USB_ERR_CANCELLED) { 296 /* try to clear stall first */ 297 usbd_xfer_set_stall(xfer); 298 goto tr_setup; 299 } 300 return; 301 302 } 303 } 304 305 static void 306 uark_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 307 { 308 struct uark_softc *sc = usbd_xfer_softc(xfer); 309 struct usb_page_cache *pc; 310 int actlen; 311 312 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 313 314 switch (USB_GET_STATE(xfer)) { 315 case USB_ST_TRANSFERRED: 316 pc = usbd_xfer_get_frame(xfer, 0); 317 ucom_put_data(&sc->sc_ucom, pc, 0, actlen); 318 319 case USB_ST_SETUP: 320 tr_setup: 321 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 322 usbd_transfer_submit(xfer); 323 return; 324 325 default: /* Error */ 326 if (error != USB_ERR_CANCELLED) { 327 /* try to clear stall first */ 328 usbd_xfer_set_stall(xfer); 329 goto tr_setup; 330 } 331 return; 332 } 333 } 334 335 static void 336 uark_start_read(struct ucom_softc *ucom) 337 { 338 struct uark_softc *sc = ucom->sc_parent; 339 340 usbd_transfer_start(sc->sc_xfer[UARK_BULK_DT_RD]); 341 } 342 343 static void 344 uark_stop_read(struct ucom_softc *ucom) 345 { 346 struct uark_softc *sc = ucom->sc_parent; 347 348 usbd_transfer_stop(sc->sc_xfer[UARK_BULK_DT_RD]); 349 } 350 351 static void 352 uark_start_write(struct ucom_softc *ucom) 353 { 354 struct uark_softc *sc = ucom->sc_parent; 355 356 usbd_transfer_start(sc->sc_xfer[UARK_BULK_DT_WR]); 357 } 358 359 static void 360 uark_stop_write(struct ucom_softc *ucom) 361 { 362 struct uark_softc *sc = ucom->sc_parent; 363 364 usbd_transfer_stop(sc->sc_xfer[UARK_BULK_DT_WR]); 365 } 366 367 static int 368 uark_pre_param(struct ucom_softc *ucom, struct termios *t) 369 { 370 if ((t->c_ospeed < 300) || (t->c_ospeed > 115200)) 371 return (EINVAL); 372 return (0); 373 } 374 375 static void 376 uark_cfg_param(struct ucom_softc *ucom, struct termios *t) 377 { 378 struct uark_softc *sc = ucom->sc_parent; 379 uint32_t speed = t->c_ospeed; 380 uint16_t data; 381 382 /* 383 * NOTE: When reverse computing the baud rate from the "data" all 384 * allowed baud rates are within 3% of the initial baud rate. 385 */ 386 data = (UARK_BAUD_REF + (speed / 2)) / speed; 387 388 uark_cfg_write(sc, 3, 0x83); 389 uark_cfg_write(sc, 0, data & 0xFF); 390 uark_cfg_write(sc, 1, data >> 8); 391 uark_cfg_write(sc, 3, 0x03); 392 393 if (t->c_cflag & CSTOPB) 394 data = UARK_STOP_BITS_2; 395 else 396 data = UARK_STOP_BITS_1; 397 398 if (t->c_cflag & PARENB) { 399 if (t->c_cflag & PARODD) 400 data |= UARK_PARITY_ODD; 401 else 402 data |= UARK_PARITY_EVEN; 403 } else 404 data |= UARK_PARITY_NONE; 405 406 switch (t->c_cflag & CSIZE) { 407 case CS5: 408 data |= UARK_SET_DATA_BITS(5); 409 break; 410 case CS6: 411 data |= UARK_SET_DATA_BITS(6); 412 break; 413 case CS7: 414 data |= UARK_SET_DATA_BITS(7); 415 break; 416 default: 417 case CS8: 418 data |= UARK_SET_DATA_BITS(8); 419 break; 420 } 421 uark_cfg_write(sc, 3, 0x00); 422 uark_cfg_write(sc, 3, data); 423 } 424 425 static void 426 uark_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 427 { 428 struct uark_softc *sc = ucom->sc_parent; 429 430 *lsr = sc->sc_lsr; 431 *msr = sc->sc_msr; 432 } 433 434 static void 435 uark_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff) 436 { 437 struct uark_softc *sc = ucom->sc_parent; 438 439 DPRINTF("onoff=%d\n", onoff); 440 441 uark_cfg_write(sc, 4, onoff ? 0x01 : 0x00); 442 } 443 444 static void 445 uark_cfg_write(struct uark_softc *sc, uint16_t index, uint16_t value) 446 { 447 struct usb_device_request req; 448 usb_error_t err; 449 450 req.bmRequestType = UARK_WRITE; 451 req.bRequest = UARK_REQUEST; 452 USETW(req.wValue, value); 453 USETW(req.wIndex, index); 454 USETW(req.wLength, 0); 455 456 err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 457 &req, NULL, 0, 1000); 458 if (err) { 459 DPRINTFN(0, "device request failed, err=%s " 460 "(ignored)\n", usbd_errstr(err)); 461 } 462 } 463 464 static void 465 uark_poll(struct ucom_softc *ucom) 466 { 467 struct uark_softc *sc = ucom->sc_parent; 468 usbd_transfer_poll(sc->sc_xfer, UARK_N_TRANSFER); 469 } 470