1 /*- 2 * Copyright (c) 2017 Microsoft Corp. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * 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 ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/kernel.h> 32 #include <sys/conf.h> 33 #include <sys/uio.h> 34 #include <sys/bus.h> 35 #include <sys/malloc.h> 36 #include <sys/mbuf.h> 37 #include <sys/module.h> 38 #include <sys/lock.h> 39 #include <sys/taskqueue.h> 40 #include <sys/selinfo.h> 41 #include <sys/sysctl.h> 42 #include <sys/poll.h> 43 #include <sys/proc.h> 44 #include <sys/queue.h> 45 #include <sys/syscallsubr.h> 46 #include <sys/sysproto.h> 47 #include <sys/systm.h> 48 #include <sys/mutex.h> 49 50 #include <sys/kbio.h> 51 #include <dev/kbd/kbdreg.h> 52 #include <dev/kbd/kbdtables.h> 53 54 #include <dev/hyperv/include/hyperv.h> 55 #include <dev/hyperv/utilities/hv_utilreg.h> 56 #include <dev/hyperv/utilities/vmbus_icreg.h> 57 #include <dev/hyperv/utilities/vmbus_icvar.h> 58 #include <dev/hyperv/include/vmbus_xact.h> 59 60 #include "dev/hyperv/input/hv_kbdc.h" 61 #include "vmbus_if.h" 62 63 #define HV_KBD_VER_MAJOR (1) 64 #define HV_KBD_VER_MINOR (0) 65 66 #define HV_KBD_VER (HV_KBD_VER_MINOR | (HV_KBD_VER_MAJOR) << 16) 67 68 #define HV_KBD_PROTO_ACCEPTED (1) 69 70 #define HV_BUFF_SIZE (4*PAGE_SIZE) 71 #define HV_KBD_RINGBUFF_SEND_SZ (10*PAGE_SIZE) 72 #define HV_KBD_RINGBUFF_RECV_SZ (10*PAGE_SIZE) 73 74 enum hv_kbd_msg_type_t { 75 HV_KBD_PROTO_REQUEST = 1, 76 HV_KBD_PROTO_RESPONSE = 2, 77 HV_KBD_PROTO_EVENT = 3, 78 HV_KBD_PROTO_LED_INDICATORS = 4, 79 }; 80 81 typedef struct hv_kbd_msg_hdr_t { 82 uint32_t type; 83 } hv_kbd_msg_hdr; 84 85 typedef struct hv_kbd_msg_t { 86 hv_kbd_msg_hdr hdr; 87 char data[]; 88 } hv_kbd_msg; 89 90 typedef struct hv_kbd_proto_req_t { 91 hv_kbd_msg_hdr hdr; 92 uint32_t ver; 93 } hv_kbd_proto_req; 94 95 typedef struct hv_kbd_proto_resp_t { 96 hv_kbd_msg_hdr hdr; 97 uint32_t status; 98 } hv_kbd_proto_resp; 99 100 #define HV_KBD_PROTO_REQ_SZ (sizeof(hv_kbd_proto_req)) 101 #define HV_KBD_PROTO_RESP_SZ (sizeof(hv_kbd_proto_resp)) 102 103 /** 104 * the struct in win host: 105 * typedef struct _HK_MESSAGE_KEYSTROKE 106 * { 107 * HK_MESSAGE_HEADER Header; 108 * UINT16 MakeCode; 109 * UINT32 IsUnicode:1; 110 * UINT32 IsBreak:1; 111 * UINT32 IsE0:1; 112 * UINT32 IsE1:1; 113 * UINT32 Reserved:28; 114 * } HK_MESSAGE_KEYSTROKE 115 */ 116 typedef struct hv_kbd_keystroke_t { 117 hv_kbd_msg_hdr hdr; 118 keystroke ks; 119 } hv_kbd_keystroke; 120 121 static const struct vmbus_ic_desc vmbus_kbd_descs[] = { 122 { 123 .ic_guid = { .hv_guid = { 124 0x6d, 0xad, 0x12, 0xf9, 0x17, 0x2b, 0xea, 0x48, 125 0xbd, 0x65, 0xf9, 0x27, 0xa6, 0x1c, 0x76, 0x84} }, 126 .ic_desc = "Hyper-V KBD" 127 }, 128 VMBUS_IC_DESC_END 129 }; 130 131 static int hv_kbd_attach(device_t dev); 132 static int hv_kbd_detach(device_t dev); 133 134 /** 135 * return 1 if producer is ready 136 */ 137 int 138 hv_kbd_prod_is_ready(hv_kbd_sc *sc) 139 { 140 int ret; 141 mtx_lock(&sc->ks_mtx); 142 ret = !STAILQ_EMPTY(&sc->ks_queue); 143 mtx_unlock(&sc->ks_mtx); 144 return (ret); 145 } 146 147 int 148 hv_kbd_produce_ks(hv_kbd_sc *sc, const keystroke *ks) 149 { 150 int ret = 0; 151 keystroke_info *ksi; 152 mtx_lock(&sc->ks_mtx); 153 if (LIST_EMPTY(&sc->ks_free_list)) { 154 DEBUG_HVSC(sc, "NO buffer!\n"); 155 ret = 1; 156 } else { 157 ksi = LIST_FIRST(&sc->ks_free_list); 158 LIST_REMOVE(ksi, link); 159 ksi->ks = *ks; 160 STAILQ_INSERT_TAIL(&sc->ks_queue, ksi, slink); 161 } 162 mtx_unlock(&sc->ks_mtx); 163 return (ret); 164 } 165 166 /** 167 * return 0 if successfully get the 1st item of queue without removing it 168 */ 169 int 170 hv_kbd_fetch_top(hv_kbd_sc *sc, keystroke *result) 171 { 172 int ret = 0; 173 keystroke_info *ksi = NULL; 174 mtx_lock(&sc->ks_mtx); 175 if (STAILQ_EMPTY(&sc->ks_queue)) { 176 DEBUG_HVSC(sc, "Empty queue!\n"); 177 ret = 1; 178 } else { 179 ksi = STAILQ_FIRST(&sc->ks_queue); 180 *result = ksi->ks; 181 } 182 mtx_unlock(&sc->ks_mtx); 183 return (ret); 184 } 185 186 /** 187 * return 0 if successfully removing the top item 188 */ 189 int 190 hv_kbd_remove_top(hv_kbd_sc *sc) 191 { 192 int ret = 0; 193 keystroke_info *ksi = NULL; 194 mtx_lock(&sc->ks_mtx); 195 if (STAILQ_EMPTY(&sc->ks_queue)) { 196 DEBUG_HVSC(sc, "Empty queue!\n"); 197 ret = 1; 198 } else { 199 ksi = STAILQ_FIRST(&sc->ks_queue); 200 STAILQ_REMOVE_HEAD(&sc->ks_queue, slink); 201 LIST_INSERT_HEAD(&sc->ks_free_list, ksi, link); 202 } 203 mtx_unlock(&sc->ks_mtx); 204 return (ret); 205 } 206 207 /** 208 * return 0 if successfully modify the 1st item of queue 209 */ 210 int 211 hv_kbd_modify_top(hv_kbd_sc *sc, keystroke *top) 212 { 213 int ret = 0; 214 keystroke_info *ksi = NULL; 215 mtx_lock(&sc->ks_mtx); 216 if (STAILQ_EMPTY(&sc->ks_queue)) { 217 DEBUG_HVSC(sc, "Empty queue!\n"); 218 ret = 1; 219 } else { 220 ksi = STAILQ_FIRST(&sc->ks_queue); 221 ksi->ks = *top; 222 } 223 mtx_unlock(&sc->ks_mtx); 224 return (ret); 225 } 226 227 static int 228 hv_kbd_probe(device_t dev) 229 { 230 device_t bus = device_get_parent(dev); 231 const struct vmbus_ic_desc *d; 232 233 if (resource_disabled(device_get_name(dev), 0)) 234 return (ENXIO); 235 236 for (d = vmbus_kbd_descs; d->ic_desc != NULL; ++d) { 237 if (VMBUS_PROBE_GUID(bus, dev, &d->ic_guid) == 0) { 238 device_set_desc(dev, d->ic_desc); 239 return (BUS_PROBE_DEFAULT); 240 } 241 } 242 return (ENXIO); 243 } 244 245 static void 246 hv_kbd_on_response(hv_kbd_sc *sc, struct vmbus_chanpkt_hdr *pkt) 247 { 248 struct vmbus_xact_ctx *xact = sc->hs_xact_ctx; 249 if (xact != NULL) { 250 DEBUG_HVSC(sc, "hvkbd is ready\n"); 251 vmbus_xact_ctx_wakeup(xact, VMBUS_CHANPKT_CONST_DATA(pkt), 252 VMBUS_CHANPKT_DATALEN(pkt)); 253 } 254 } 255 256 static void 257 hv_kbd_on_received(hv_kbd_sc *sc, struct vmbus_chanpkt_hdr *pkt) 258 { 259 260 const hv_kbd_msg *msg = VMBUS_CHANPKT_CONST_DATA(pkt); 261 const hv_kbd_proto_resp *resp = 262 VMBUS_CHANPKT_CONST_DATA(pkt); 263 const hv_kbd_keystroke *keystroke = 264 VMBUS_CHANPKT_CONST_DATA(pkt); 265 uint32_t msg_len = VMBUS_CHANPKT_DATALEN(pkt); 266 enum hv_kbd_msg_type_t msg_type; 267 uint32_t info; 268 uint16_t scan_code; 269 270 if (msg_len <= sizeof(hv_kbd_msg)) { 271 device_printf(sc->dev, "Illegal packet\n"); 272 return; 273 } 274 msg_type = msg->hdr.type; 275 switch (msg_type) { 276 case HV_KBD_PROTO_RESPONSE: 277 hv_kbd_on_response(sc, pkt); 278 DEBUG_HVSC(sc, "keyboard resp: 0x%x\n", 279 resp->status); 280 break; 281 case HV_KBD_PROTO_EVENT: 282 info = keystroke->ks.info; 283 scan_code = keystroke->ks.makecode; 284 DEBUG_HVSC(sc, "keystroke info: 0x%x, scan: 0x%x\n", 285 info, scan_code); 286 hv_kbd_produce_ks(sc, &keystroke->ks); 287 hv_kbd_intr(sc); 288 default: 289 break; 290 } 291 } 292 293 void 294 hv_kbd_read_channel(struct vmbus_channel *channel, void *context) 295 { 296 uint8_t *buf; 297 uint32_t buflen = 0; 298 int ret = 0; 299 300 hv_kbd_sc *sc = (hv_kbd_sc*)context; 301 channel = vmbus_get_channel(sc->dev); 302 buf = sc->buf; 303 buflen = sc->buflen; 304 for (;;) { 305 struct vmbus_chanpkt_hdr *pkt = (struct vmbus_chanpkt_hdr *)buf; 306 uint32_t rxed = buflen; 307 308 ret = vmbus_chan_recv_pkt(channel, pkt, &rxed); 309 if (__predict_false(ret == ENOBUFS)) { 310 buflen = sc->buflen * 2; 311 while (buflen < rxed) 312 buflen *= 2; 313 buf = malloc(buflen, M_DEVBUF, M_WAITOK | M_ZERO); 314 device_printf(sc->dev, "expand recvbuf %d -> %d\n", 315 sc->buflen, buflen); 316 free(sc->buf, M_DEVBUF); 317 sc->buf = buf; 318 sc->buflen = buflen; 319 continue; 320 } else if (__predict_false(ret == EAGAIN)) { 321 /* No more channel packets; done! */ 322 break; 323 } 324 KASSERT(!ret, ("vmbus_chan_recv_pkt failed: %d", ret)); 325 326 DEBUG_HVSC(sc, "event: 0x%x\n", pkt->cph_type); 327 switch (pkt->cph_type) { 328 case VMBUS_CHANPKT_TYPE_COMP: 329 case VMBUS_CHANPKT_TYPE_RXBUF: 330 device_printf(sc->dev, "unhandled event: %d\n", 331 pkt->cph_type); 332 break; 333 case VMBUS_CHANPKT_TYPE_INBAND: 334 hv_kbd_on_received(sc, pkt); 335 break; 336 default: 337 device_printf(sc->dev, "unknown event: %d\n", 338 pkt->cph_type); 339 break; 340 } 341 } 342 } 343 344 static int 345 hv_kbd_connect_vsp(hv_kbd_sc *sc) 346 { 347 int ret; 348 size_t resplen; 349 struct vmbus_xact *xact; 350 hv_kbd_proto_req *req; 351 const hv_kbd_proto_resp *resp; 352 353 xact = vmbus_xact_get(sc->hs_xact_ctx, sizeof(*req)); 354 if (xact == NULL) { 355 device_printf(sc->dev, "no xact for kbd init"); 356 return (ENODEV); 357 } 358 req = vmbus_xact_req_data(xact); 359 req->hdr.type = HV_KBD_PROTO_REQUEST; 360 req->ver = HV_KBD_VER; 361 362 vmbus_xact_activate(xact); 363 ret = vmbus_chan_send(sc->hs_chan, 364 VMBUS_CHANPKT_TYPE_INBAND, 365 VMBUS_CHANPKT_FLAG_RC, 366 req, sizeof(hv_kbd_proto_req), 367 (uint64_t)(uintptr_t)xact); 368 if (ret) { 369 device_printf(sc->dev, "fail to send\n"); 370 vmbus_xact_deactivate(xact); 371 return (ret); 372 } 373 resp = vmbus_chan_xact_wait(sc->hs_chan, xact, &resplen, true); 374 if (resplen < HV_KBD_PROTO_RESP_SZ) { 375 device_printf(sc->dev, "hv_kbd init communicate failed\n"); 376 ret = ENODEV; 377 goto clean; 378 } 379 380 if (!(resp->status & HV_KBD_PROTO_ACCEPTED)) { 381 device_printf(sc->dev, "hv_kbd protocol request failed\n"); 382 ret = ENODEV; 383 } 384 clean: 385 vmbus_xact_put(xact); 386 DEBUG_HVSC(sc, "finish connect vsp\n"); 387 return (ret); 388 } 389 390 static int 391 hv_kbd_attach1(device_t dev, vmbus_chan_callback_t cb) 392 { 393 int ret; 394 hv_kbd_sc *sc; 395 396 sc = device_get_softc(dev); 397 sc->buflen = HV_BUFF_SIZE; 398 sc->buf = malloc(sc->buflen, M_DEVBUF, M_WAITOK | M_ZERO); 399 vmbus_chan_set_readbatch(sc->hs_chan, false); 400 ret = vmbus_chan_open( 401 sc->hs_chan, 402 HV_KBD_RINGBUFF_SEND_SZ, 403 HV_KBD_RINGBUFF_RECV_SZ, 404 NULL, 0, 405 cb, 406 sc); 407 if (ret != 0) { 408 free(sc->buf, M_DEVBUF); 409 } 410 return (ret); 411 } 412 413 static int 414 hv_kbd_detach1(device_t dev) 415 { 416 hv_kbd_sc *sc = device_get_softc(dev); 417 vmbus_chan_close(vmbus_get_channel(dev)); 418 free(sc->buf, M_DEVBUF); 419 return (0); 420 } 421 422 static void 423 hv_kbd_init(hv_kbd_sc *sc) 424 { 425 const int max_list = 16; 426 int i; 427 keystroke_info *ksi; 428 429 mtx_init(&sc->ks_mtx, "hv_kbdc mutex", NULL, MTX_DEF); 430 LIST_INIT(&sc->ks_free_list); 431 STAILQ_INIT(&sc->ks_queue); 432 for (i = 0; i < max_list; i++) { 433 ksi = malloc(sizeof(keystroke_info), 434 M_DEVBUF, M_WAITOK|M_ZERO); 435 LIST_INSERT_HEAD(&sc->ks_free_list, ksi, link); 436 } 437 } 438 439 static void 440 hv_kbd_fini(hv_kbd_sc *sc) 441 { 442 keystroke_info *ksi; 443 while (!LIST_EMPTY(&sc->ks_free_list)) { 444 ksi = LIST_FIRST(&sc->ks_free_list); 445 LIST_REMOVE(ksi, link); 446 free(ksi, M_DEVBUF); 447 } 448 while (!STAILQ_EMPTY(&sc->ks_queue)) { 449 ksi = STAILQ_FIRST(&sc->ks_queue); 450 STAILQ_REMOVE_HEAD(&sc->ks_queue, slink); 451 free(ksi, M_DEVBUF); 452 } 453 mtx_destroy(&sc->ks_mtx); 454 } 455 456 static void 457 hv_kbd_sysctl(device_t dev) 458 { 459 struct sysctl_oid_list *child; 460 struct sysctl_ctx_list *ctx; 461 hv_kbd_sc *sc; 462 463 sc = device_get_softc(dev); 464 ctx = device_get_sysctl_ctx(dev); 465 child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev)); 466 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "debug", CTLFLAG_RW, 467 &sc->debug, 0, "debug hyperv keyboard"); 468 } 469 470 static int 471 hv_kbd_attach(device_t dev) 472 { 473 int error = 0; 474 hv_kbd_sc *sc; 475 476 sc = device_get_softc(dev); 477 sc->hs_chan = vmbus_get_channel(dev); 478 sc->dev = dev; 479 hv_kbd_init(sc); 480 sc->hs_xact_ctx = vmbus_xact_ctx_create(bus_get_dma_tag(dev), 481 HV_KBD_PROTO_REQ_SZ, HV_KBD_PROTO_RESP_SZ, 0); 482 if (sc->hs_xact_ctx == NULL) { 483 error = ENOMEM; 484 goto failed; 485 } 486 487 error = hv_kbd_attach1(dev, hv_kbd_read_channel); 488 if (error) 489 goto failed; 490 error = hv_kbd_connect_vsp(sc); 491 if (error) 492 goto failed; 493 494 error = hv_kbd_drv_attach(dev); 495 if (error) 496 goto failed; 497 hv_kbd_sysctl(dev); 498 return (0); 499 failed: 500 hv_kbd_detach(dev); 501 return (error); 502 } 503 504 static int 505 hv_kbd_detach(device_t dev) 506 { 507 int ret; 508 hv_kbd_sc *sc = device_get_softc(dev); 509 hv_kbd_fini(sc); 510 if (sc->hs_xact_ctx != NULL) 511 vmbus_xact_ctx_destroy(sc->hs_xact_ctx); 512 ret = hv_kbd_detach1(dev); 513 if (!ret) 514 device_printf(dev, "Fail to detach\n"); 515 return hv_kbd_drv_detach(dev); 516 } 517 518 static device_method_t kbd_methods[] = { 519 /* Device interface */ 520 DEVMETHOD(device_probe, hv_kbd_probe), 521 DEVMETHOD(device_attach, hv_kbd_attach), 522 DEVMETHOD(device_detach, hv_kbd_detach), 523 { 0, 0 } 524 }; 525 526 static driver_t kbd_driver = {HVKBD_DRIVER_NAME , kbd_methods, sizeof(hv_kbd_sc)}; 527 528 static devclass_t kbd_devclass; 529 530 DRIVER_MODULE(hv_kbd, vmbus, kbd_driver, kbd_devclass, hvkbd_driver_load, NULL); 531 MODULE_VERSION(hv_kbd, 1); 532 MODULE_DEPEND(hv_kbd, vmbus, 1, 1, 1); 533