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