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