1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2012-2019, Intel Corporation. All rights reserved. 4 * Intel Management Engine Interface (Intel MEI) Linux driver 5 */ 6 7 #include <linux/module.h> 8 #include <linux/device.h> 9 #include <linux/kernel.h> 10 #include <linux/sched/signal.h> 11 #include <linux/init.h> 12 #include <linux/errno.h> 13 #include <linux/slab.h> 14 #include <linux/mutex.h> 15 #include <linux/interrupt.h> 16 #include <linux/mei_cl_bus.h> 17 18 #include "mei_dev.h" 19 #include "client.h" 20 21 #define to_mei_cl_driver(d) container_of(d, struct mei_cl_driver, driver) 22 23 /** 24 * __mei_cl_send - internal client send (write) 25 * 26 * @cl: host client 27 * @buf: buffer to send 28 * @length: buffer length 29 * @vtag: virtual tag 30 * @mode: sending mode 31 * 32 * Return: written size bytes or < 0 on error 33 */ 34 ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length, u8 vtag, 35 unsigned int mode) 36 { 37 struct mei_device *bus; 38 struct mei_cl_cb *cb; 39 ssize_t rets; 40 41 if (WARN_ON(!cl || !cl->dev)) 42 return -ENODEV; 43 44 bus = cl->dev; 45 46 mutex_lock(&bus->device_lock); 47 if (bus->dev_state != MEI_DEV_ENABLED && 48 bus->dev_state != MEI_DEV_POWERING_DOWN) { 49 rets = -ENODEV; 50 goto out; 51 } 52 53 if (!mei_cl_is_connected(cl)) { 54 rets = -ENODEV; 55 goto out; 56 } 57 58 /* Check if we have an ME client device */ 59 if (!mei_me_cl_is_active(cl->me_cl)) { 60 rets = -ENOTTY; 61 goto out; 62 } 63 64 if (length > mei_cl_mtu(cl)) { 65 rets = -EFBIG; 66 goto out; 67 } 68 69 while (cl->tx_cb_queued >= bus->tx_queue_limit) { 70 mutex_unlock(&bus->device_lock); 71 rets = wait_event_interruptible(cl->tx_wait, 72 cl->writing_state == MEI_WRITE_COMPLETE || 73 (!mei_cl_is_connected(cl))); 74 mutex_lock(&bus->device_lock); 75 if (rets) { 76 if (signal_pending(current)) 77 rets = -EINTR; 78 goto out; 79 } 80 if (!mei_cl_is_connected(cl)) { 81 rets = -ENODEV; 82 goto out; 83 } 84 } 85 86 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL); 87 if (!cb) { 88 rets = -ENOMEM; 89 goto out; 90 } 91 cb->vtag = vtag; 92 93 cb->internal = !!(mode & MEI_CL_IO_TX_INTERNAL); 94 cb->blocking = !!(mode & MEI_CL_IO_TX_BLOCKING); 95 memcpy(cb->buf.data, buf, length); 96 97 rets = mei_cl_write(cl, cb); 98 99 out: 100 mutex_unlock(&bus->device_lock); 101 102 return rets; 103 } 104 105 /** 106 * __mei_cl_recv - internal client receive (read) 107 * 108 * @cl: host client 109 * @buf: buffer to receive 110 * @length: buffer length 111 * @mode: io mode 112 * @vtag: virtual tag 113 * @timeout: recv timeout, 0 for infinite timeout 114 * 115 * Return: read size in bytes of < 0 on error 116 */ 117 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length, u8 *vtag, 118 unsigned int mode, unsigned long timeout) 119 { 120 struct mei_device *bus; 121 struct mei_cl_cb *cb; 122 size_t r_length; 123 ssize_t rets; 124 bool nonblock = !!(mode & MEI_CL_IO_RX_NONBLOCK); 125 126 if (WARN_ON(!cl || !cl->dev)) 127 return -ENODEV; 128 129 bus = cl->dev; 130 131 mutex_lock(&bus->device_lock); 132 if (bus->dev_state != MEI_DEV_ENABLED && 133 bus->dev_state != MEI_DEV_POWERING_DOWN) { 134 rets = -ENODEV; 135 goto out; 136 } 137 138 cb = mei_cl_read_cb(cl, NULL); 139 if (cb) 140 goto copy; 141 142 rets = mei_cl_read_start(cl, length, NULL); 143 if (rets && rets != -EBUSY) 144 goto out; 145 146 if (nonblock) { 147 rets = -EAGAIN; 148 goto out; 149 } 150 151 /* wait on event only if there is no other waiter */ 152 /* synchronized under device mutex */ 153 if (!waitqueue_active(&cl->rx_wait)) { 154 155 mutex_unlock(&bus->device_lock); 156 157 if (timeout) { 158 rets = wait_event_interruptible_timeout 159 (cl->rx_wait, 160 mei_cl_read_cb(cl, NULL) || 161 (!mei_cl_is_connected(cl)), 162 msecs_to_jiffies(timeout)); 163 if (rets == 0) 164 return -ETIME; 165 if (rets < 0) { 166 if (signal_pending(current)) 167 return -EINTR; 168 return -ERESTARTSYS; 169 } 170 } else { 171 if (wait_event_interruptible 172 (cl->rx_wait, 173 mei_cl_read_cb(cl, NULL) || 174 (!mei_cl_is_connected(cl)))) { 175 if (signal_pending(current)) 176 return -EINTR; 177 return -ERESTARTSYS; 178 } 179 } 180 181 mutex_lock(&bus->device_lock); 182 183 if (!mei_cl_is_connected(cl)) { 184 rets = -ENODEV; 185 goto out; 186 } 187 } 188 189 cb = mei_cl_read_cb(cl, NULL); 190 if (!cb) { 191 rets = 0; 192 goto out; 193 } 194 195 copy: 196 if (cb->status) { 197 rets = cb->status; 198 goto free; 199 } 200 201 r_length = min_t(size_t, length, cb->buf_idx); 202 memcpy(buf, cb->buf.data, r_length); 203 rets = r_length; 204 if (vtag) 205 *vtag = cb->vtag; 206 207 free: 208 mei_cl_del_rd_completed(cl, cb); 209 out: 210 mutex_unlock(&bus->device_lock); 211 212 return rets; 213 } 214 215 /** 216 * mei_cldev_send_vtag - me device send with vtag (write) 217 * 218 * @cldev: me client device 219 * @buf: buffer to send 220 * @length: buffer length 221 * @vtag: virtual tag 222 * 223 * Return: 224 * * written size in bytes 225 * * < 0 on error 226 */ 227 228 ssize_t mei_cldev_send_vtag(struct mei_cl_device *cldev, u8 *buf, size_t length, 229 u8 vtag) 230 { 231 struct mei_cl *cl = cldev->cl; 232 233 return __mei_cl_send(cl, buf, length, vtag, MEI_CL_IO_TX_BLOCKING); 234 } 235 EXPORT_SYMBOL_GPL(mei_cldev_send_vtag); 236 237 /** 238 * mei_cldev_recv_vtag - client receive with vtag (read) 239 * 240 * @cldev: me client device 241 * @buf: buffer to receive 242 * @length: buffer length 243 * @vtag: virtual tag 244 * 245 * Return: 246 * * read size in bytes 247 * * < 0 on error 248 */ 249 250 ssize_t mei_cldev_recv_vtag(struct mei_cl_device *cldev, u8 *buf, size_t length, 251 u8 *vtag) 252 { 253 struct mei_cl *cl = cldev->cl; 254 255 return __mei_cl_recv(cl, buf, length, vtag, 0, 0); 256 } 257 EXPORT_SYMBOL_GPL(mei_cldev_recv_vtag); 258 259 /** 260 * mei_cldev_recv_nonblock_vtag - non block client receive with vtag (read) 261 * 262 * @cldev: me client device 263 * @buf: buffer to receive 264 * @length: buffer length 265 * @vtag: virtual tag 266 * 267 * Return: 268 * * read size in bytes 269 * * -EAGAIN if function will block. 270 * * < 0 on other error 271 */ 272 ssize_t mei_cldev_recv_nonblock_vtag(struct mei_cl_device *cldev, u8 *buf, 273 size_t length, u8 *vtag) 274 { 275 struct mei_cl *cl = cldev->cl; 276 277 return __mei_cl_recv(cl, buf, length, vtag, MEI_CL_IO_RX_NONBLOCK, 0); 278 } 279 EXPORT_SYMBOL_GPL(mei_cldev_recv_nonblock_vtag); 280 281 /** 282 * mei_cldev_send - me device send (write) 283 * 284 * @cldev: me client device 285 * @buf: buffer to send 286 * @length: buffer length 287 * 288 * Return: 289 * * written size in bytes 290 * * < 0 on error 291 */ 292 ssize_t mei_cldev_send(struct mei_cl_device *cldev, u8 *buf, size_t length) 293 { 294 return mei_cldev_send_vtag(cldev, buf, length, 0); 295 } 296 EXPORT_SYMBOL_GPL(mei_cldev_send); 297 298 /** 299 * mei_cldev_recv - client receive (read) 300 * 301 * @cldev: me client device 302 * @buf: buffer to receive 303 * @length: buffer length 304 * 305 * Return: read size in bytes of < 0 on error 306 */ 307 ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length) 308 { 309 return mei_cldev_recv_vtag(cldev, buf, length, NULL); 310 } 311 EXPORT_SYMBOL_GPL(mei_cldev_recv); 312 313 /** 314 * mei_cldev_recv_nonblock - non block client receive (read) 315 * 316 * @cldev: me client device 317 * @buf: buffer to receive 318 * @length: buffer length 319 * 320 * Return: read size in bytes of < 0 on error 321 * -EAGAIN if function will block. 322 */ 323 ssize_t mei_cldev_recv_nonblock(struct mei_cl_device *cldev, u8 *buf, 324 size_t length) 325 { 326 return mei_cldev_recv_nonblock_vtag(cldev, buf, length, NULL); 327 } 328 EXPORT_SYMBOL_GPL(mei_cldev_recv_nonblock); 329 330 /** 331 * mei_cl_bus_rx_work - dispatch rx event for a bus device 332 * 333 * @work: work 334 */ 335 static void mei_cl_bus_rx_work(struct work_struct *work) 336 { 337 struct mei_cl_device *cldev; 338 struct mei_device *bus; 339 340 cldev = container_of(work, struct mei_cl_device, rx_work); 341 342 bus = cldev->bus; 343 344 if (cldev->rx_cb) 345 cldev->rx_cb(cldev); 346 347 mutex_lock(&bus->device_lock); 348 if (mei_cl_is_connected(cldev->cl)) 349 mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL); 350 mutex_unlock(&bus->device_lock); 351 } 352 353 /** 354 * mei_cl_bus_notif_work - dispatch FW notif event for a bus device 355 * 356 * @work: work 357 */ 358 static void mei_cl_bus_notif_work(struct work_struct *work) 359 { 360 struct mei_cl_device *cldev; 361 362 cldev = container_of(work, struct mei_cl_device, notif_work); 363 364 if (cldev->notif_cb) 365 cldev->notif_cb(cldev); 366 } 367 368 /** 369 * mei_cl_bus_notify_event - schedule notify cb on bus client 370 * 371 * @cl: host client 372 * 373 * Return: true if event was scheduled 374 * false if the client is not waiting for event 375 */ 376 bool mei_cl_bus_notify_event(struct mei_cl *cl) 377 { 378 struct mei_cl_device *cldev = cl->cldev; 379 380 if (!cldev || !cldev->notif_cb) 381 return false; 382 383 if (!cl->notify_ev) 384 return false; 385 386 schedule_work(&cldev->notif_work); 387 388 cl->notify_ev = false; 389 390 return true; 391 } 392 393 /** 394 * mei_cl_bus_rx_event - schedule rx event 395 * 396 * @cl: host client 397 * 398 * Return: true if event was scheduled 399 * false if the client is not waiting for event 400 */ 401 bool mei_cl_bus_rx_event(struct mei_cl *cl) 402 { 403 struct mei_cl_device *cldev = cl->cldev; 404 405 if (!cldev || !cldev->rx_cb) 406 return false; 407 408 schedule_work(&cldev->rx_work); 409 410 return true; 411 } 412 413 /** 414 * mei_cldev_register_rx_cb - register Rx event callback 415 * 416 * @cldev: me client devices 417 * @rx_cb: callback function 418 * 419 * Return: 0 on success 420 * -EALREADY if an callback is already registered 421 * <0 on other errors 422 */ 423 int mei_cldev_register_rx_cb(struct mei_cl_device *cldev, mei_cldev_cb_t rx_cb) 424 { 425 struct mei_device *bus = cldev->bus; 426 int ret; 427 428 if (!rx_cb) 429 return -EINVAL; 430 if (cldev->rx_cb) 431 return -EALREADY; 432 433 cldev->rx_cb = rx_cb; 434 INIT_WORK(&cldev->rx_work, mei_cl_bus_rx_work); 435 436 mutex_lock(&bus->device_lock); 437 if (mei_cl_is_connected(cldev->cl)) 438 ret = mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL); 439 else 440 ret = -ENODEV; 441 mutex_unlock(&bus->device_lock); 442 if (ret && ret != -EBUSY) { 443 cancel_work_sync(&cldev->rx_work); 444 cldev->rx_cb = NULL; 445 return ret; 446 } 447 448 return 0; 449 } 450 EXPORT_SYMBOL_GPL(mei_cldev_register_rx_cb); 451 452 /** 453 * mei_cldev_register_notif_cb - register FW notification event callback 454 * 455 * @cldev: me client devices 456 * @notif_cb: callback function 457 * 458 * Return: 0 on success 459 * -EALREADY if an callback is already registered 460 * <0 on other errors 461 */ 462 int mei_cldev_register_notif_cb(struct mei_cl_device *cldev, 463 mei_cldev_cb_t notif_cb) 464 { 465 struct mei_device *bus = cldev->bus; 466 int ret; 467 468 if (!notif_cb) 469 return -EINVAL; 470 471 if (cldev->notif_cb) 472 return -EALREADY; 473 474 cldev->notif_cb = notif_cb; 475 INIT_WORK(&cldev->notif_work, mei_cl_bus_notif_work); 476 477 mutex_lock(&bus->device_lock); 478 ret = mei_cl_notify_request(cldev->cl, NULL, 1); 479 mutex_unlock(&bus->device_lock); 480 if (ret) { 481 cancel_work_sync(&cldev->notif_work); 482 cldev->notif_cb = NULL; 483 return ret; 484 } 485 486 return 0; 487 } 488 EXPORT_SYMBOL_GPL(mei_cldev_register_notif_cb); 489 490 /** 491 * mei_cldev_get_drvdata - driver data getter 492 * 493 * @cldev: mei client device 494 * 495 * Return: driver private data 496 */ 497 void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev) 498 { 499 return dev_get_drvdata(&cldev->dev); 500 } 501 EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata); 502 503 /** 504 * mei_cldev_set_drvdata - driver data setter 505 * 506 * @cldev: mei client device 507 * @data: data to store 508 */ 509 void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data) 510 { 511 dev_set_drvdata(&cldev->dev, data); 512 } 513 EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata); 514 515 /** 516 * mei_cldev_uuid - return uuid of the underlying me client 517 * 518 * @cldev: mei client device 519 * 520 * Return: me client uuid 521 */ 522 const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev) 523 { 524 return mei_me_cl_uuid(cldev->me_cl); 525 } 526 EXPORT_SYMBOL_GPL(mei_cldev_uuid); 527 528 /** 529 * mei_cldev_ver - return protocol version of the underlying me client 530 * 531 * @cldev: mei client device 532 * 533 * Return: me client protocol version 534 */ 535 u8 mei_cldev_ver(const struct mei_cl_device *cldev) 536 { 537 return mei_me_cl_ver(cldev->me_cl); 538 } 539 EXPORT_SYMBOL_GPL(mei_cldev_ver); 540 541 /** 542 * mei_cldev_enabled - check whether the device is enabled 543 * 544 * @cldev: mei client device 545 * 546 * Return: true if me client is initialized and connected 547 */ 548 bool mei_cldev_enabled(struct mei_cl_device *cldev) 549 { 550 return mei_cl_is_connected(cldev->cl); 551 } 552 EXPORT_SYMBOL_GPL(mei_cldev_enabled); 553 554 /** 555 * mei_cl_bus_module_get - acquire module of the underlying 556 * hw driver. 557 * 558 * @cldev: mei client device 559 * 560 * Return: true on success; false if the module was removed. 561 */ 562 static bool mei_cl_bus_module_get(struct mei_cl_device *cldev) 563 { 564 return try_module_get(cldev->bus->dev->driver->owner); 565 } 566 567 /** 568 * mei_cl_bus_module_put - release the underlying hw module. 569 * 570 * @cldev: mei client device 571 */ 572 static void mei_cl_bus_module_put(struct mei_cl_device *cldev) 573 { 574 module_put(cldev->bus->dev->driver->owner); 575 } 576 577 /** 578 * mei_cl_bus_vtag - get bus vtag entry wrapper 579 * The tag for bus client is always first. 580 * 581 * @cl: host client 582 * 583 * Return: bus vtag or NULL 584 */ 585 static inline struct mei_cl_vtag *mei_cl_bus_vtag(struct mei_cl *cl) 586 { 587 return list_first_entry_or_null(&cl->vtag_map, 588 struct mei_cl_vtag, list); 589 } 590 591 /** 592 * mei_cl_bus_vtag_alloc - add bus client entry to vtag map 593 * 594 * @cldev: me client device 595 * 596 * Return: 597 * * 0 on success 598 * * -ENOMEM if memory allocation failed 599 */ 600 static int mei_cl_bus_vtag_alloc(struct mei_cl_device *cldev) 601 { 602 struct mei_cl *cl = cldev->cl; 603 struct mei_cl_vtag *cl_vtag; 604 605 /* 606 * Bail out if the client does not supports vtags 607 * or has already allocated one 608 */ 609 if (mei_cl_vt_support_check(cl) || mei_cl_bus_vtag(cl)) 610 return 0; 611 612 cl_vtag = mei_cl_vtag_alloc(NULL, 0); 613 if (IS_ERR(cl_vtag)) 614 return -ENOMEM; 615 616 list_add_tail(&cl_vtag->list, &cl->vtag_map); 617 618 return 0; 619 } 620 621 /** 622 * mei_cl_bus_vtag_free - remove the bus entry from vtag map 623 * 624 * @cldev: me client device 625 */ 626 static void mei_cl_bus_vtag_free(struct mei_cl_device *cldev) 627 { 628 struct mei_cl *cl = cldev->cl; 629 struct mei_cl_vtag *cl_vtag; 630 631 cl_vtag = mei_cl_bus_vtag(cl); 632 if (!cl_vtag) 633 return; 634 635 list_del(&cl_vtag->list); 636 kfree(cl_vtag); 637 } 638 639 /** 640 * mei_cldev_enable - enable me client device 641 * create connection with me client 642 * 643 * @cldev: me client device 644 * 645 * Return: 0 on success and < 0 on error 646 */ 647 int mei_cldev_enable(struct mei_cl_device *cldev) 648 { 649 struct mei_device *bus = cldev->bus; 650 struct mei_cl *cl; 651 int ret; 652 653 cl = cldev->cl; 654 655 mutex_lock(&bus->device_lock); 656 if (cl->state == MEI_FILE_UNINITIALIZED) { 657 ret = mei_cl_link(cl); 658 if (ret) 659 goto out; 660 /* update pointers */ 661 cl->cldev = cldev; 662 } 663 664 if (mei_cl_is_connected(cl)) { 665 ret = 0; 666 goto out; 667 } 668 669 if (!mei_me_cl_is_active(cldev->me_cl)) { 670 dev_err(&cldev->dev, "me client is not active\n"); 671 ret = -ENOTTY; 672 goto out; 673 } 674 675 ret = mei_cl_bus_vtag_alloc(cldev); 676 if (ret) 677 goto out; 678 679 ret = mei_cl_connect(cl, cldev->me_cl, NULL); 680 if (ret < 0) { 681 dev_err(&cldev->dev, "cannot connect\n"); 682 mei_cl_bus_vtag_free(cldev); 683 } 684 685 out: 686 mutex_unlock(&bus->device_lock); 687 688 return ret; 689 } 690 EXPORT_SYMBOL_GPL(mei_cldev_enable); 691 692 /** 693 * mei_cldev_unregister_callbacks - internal wrapper for unregistering 694 * callbacks. 695 * 696 * @cldev: client device 697 */ 698 static void mei_cldev_unregister_callbacks(struct mei_cl_device *cldev) 699 { 700 if (cldev->rx_cb) { 701 cancel_work_sync(&cldev->rx_work); 702 cldev->rx_cb = NULL; 703 } 704 705 if (cldev->notif_cb) { 706 cancel_work_sync(&cldev->notif_work); 707 cldev->notif_cb = NULL; 708 } 709 } 710 711 /** 712 * mei_cldev_disable - disable me client device 713 * disconnect form the me client 714 * 715 * @cldev: me client device 716 * 717 * Return: 0 on success and < 0 on error 718 */ 719 int mei_cldev_disable(struct mei_cl_device *cldev) 720 { 721 struct mei_device *bus; 722 struct mei_cl *cl; 723 int err; 724 725 if (!cldev) 726 return -ENODEV; 727 728 cl = cldev->cl; 729 730 bus = cldev->bus; 731 732 mei_cldev_unregister_callbacks(cldev); 733 734 mutex_lock(&bus->device_lock); 735 736 mei_cl_bus_vtag_free(cldev); 737 738 if (!mei_cl_is_connected(cl)) { 739 dev_dbg(bus->dev, "Already disconnected\n"); 740 err = 0; 741 goto out; 742 } 743 744 err = mei_cl_disconnect(cl); 745 if (err < 0) 746 dev_err(bus->dev, "Could not disconnect from the ME client\n"); 747 748 out: 749 /* Flush queues and remove any pending read */ 750 mei_cl_flush_queues(cl, NULL); 751 mei_cl_unlink(cl); 752 753 mutex_unlock(&bus->device_lock); 754 return err; 755 } 756 EXPORT_SYMBOL_GPL(mei_cldev_disable); 757 758 /** 759 * mei_cl_device_find - find matching entry in the driver id table 760 * 761 * @cldev: me client device 762 * @cldrv: me client driver 763 * 764 * Return: id on success; NULL if no id is matching 765 */ 766 static const 767 struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev, 768 struct mei_cl_driver *cldrv) 769 { 770 const struct mei_cl_device_id *id; 771 const uuid_le *uuid; 772 u8 version; 773 bool match; 774 775 uuid = mei_me_cl_uuid(cldev->me_cl); 776 version = mei_me_cl_ver(cldev->me_cl); 777 778 id = cldrv->id_table; 779 while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) { 780 if (!uuid_le_cmp(*uuid, id->uuid)) { 781 match = true; 782 783 if (cldev->name[0]) 784 if (strncmp(cldev->name, id->name, 785 sizeof(id->name))) 786 match = false; 787 788 if (id->version != MEI_CL_VERSION_ANY) 789 if (id->version != version) 790 match = false; 791 if (match) 792 return id; 793 } 794 795 id++; 796 } 797 798 return NULL; 799 } 800 801 /** 802 * mei_cl_device_match - device match function 803 * 804 * @dev: device 805 * @drv: driver 806 * 807 * Return: 1 if matching device was found 0 otherwise 808 */ 809 static int mei_cl_device_match(struct device *dev, struct device_driver *drv) 810 { 811 struct mei_cl_device *cldev = to_mei_cl_device(dev); 812 struct mei_cl_driver *cldrv = to_mei_cl_driver(drv); 813 const struct mei_cl_device_id *found_id; 814 815 if (!cldev) 816 return 0; 817 818 if (!cldev->do_match) 819 return 0; 820 821 if (!cldrv || !cldrv->id_table) 822 return 0; 823 824 found_id = mei_cl_device_find(cldev, cldrv); 825 if (found_id) 826 return 1; 827 828 return 0; 829 } 830 831 /** 832 * mei_cl_device_probe - bus probe function 833 * 834 * @dev: device 835 * 836 * Return: 0 on success; < 0 otherwise 837 */ 838 static int mei_cl_device_probe(struct device *dev) 839 { 840 struct mei_cl_device *cldev; 841 struct mei_cl_driver *cldrv; 842 const struct mei_cl_device_id *id; 843 int ret; 844 845 cldev = to_mei_cl_device(dev); 846 cldrv = to_mei_cl_driver(dev->driver); 847 848 if (!cldev) 849 return 0; 850 851 if (!cldrv || !cldrv->probe) 852 return -ENODEV; 853 854 id = mei_cl_device_find(cldev, cldrv); 855 if (!id) 856 return -ENODEV; 857 858 if (!mei_cl_bus_module_get(cldev)) { 859 dev_err(&cldev->dev, "get hw module failed"); 860 return -ENODEV; 861 } 862 863 ret = cldrv->probe(cldev, id); 864 if (ret) { 865 mei_cl_bus_module_put(cldev); 866 return ret; 867 } 868 869 __module_get(THIS_MODULE); 870 return 0; 871 } 872 873 /** 874 * mei_cl_device_remove - remove device from the bus 875 * 876 * @dev: device 877 * 878 * Return: 0 on success; < 0 otherwise 879 */ 880 static int mei_cl_device_remove(struct device *dev) 881 { 882 struct mei_cl_device *cldev = to_mei_cl_device(dev); 883 struct mei_cl_driver *cldrv = to_mei_cl_driver(dev->driver); 884 885 if (cldrv->remove) 886 cldrv->remove(cldev); 887 888 mei_cldev_unregister_callbacks(cldev); 889 890 mei_cl_bus_module_put(cldev); 891 module_put(THIS_MODULE); 892 893 return 0; 894 } 895 896 static ssize_t name_show(struct device *dev, struct device_attribute *a, 897 char *buf) 898 { 899 struct mei_cl_device *cldev = to_mei_cl_device(dev); 900 901 return scnprintf(buf, PAGE_SIZE, "%s", cldev->name); 902 } 903 static DEVICE_ATTR_RO(name); 904 905 static ssize_t uuid_show(struct device *dev, struct device_attribute *a, 906 char *buf) 907 { 908 struct mei_cl_device *cldev = to_mei_cl_device(dev); 909 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); 910 911 return sprintf(buf, "%pUl", uuid); 912 } 913 static DEVICE_ATTR_RO(uuid); 914 915 static ssize_t version_show(struct device *dev, struct device_attribute *a, 916 char *buf) 917 { 918 struct mei_cl_device *cldev = to_mei_cl_device(dev); 919 u8 version = mei_me_cl_ver(cldev->me_cl); 920 921 return sprintf(buf, "%02X", version); 922 } 923 static DEVICE_ATTR_RO(version); 924 925 static ssize_t modalias_show(struct device *dev, struct device_attribute *a, 926 char *buf) 927 { 928 struct mei_cl_device *cldev = to_mei_cl_device(dev); 929 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); 930 u8 version = mei_me_cl_ver(cldev->me_cl); 931 932 return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:%02X:", 933 cldev->name, uuid, version); 934 } 935 static DEVICE_ATTR_RO(modalias); 936 937 static ssize_t max_conn_show(struct device *dev, struct device_attribute *a, 938 char *buf) 939 { 940 struct mei_cl_device *cldev = to_mei_cl_device(dev); 941 u8 maxconn = mei_me_cl_max_conn(cldev->me_cl); 942 943 return sprintf(buf, "%d", maxconn); 944 } 945 static DEVICE_ATTR_RO(max_conn); 946 947 static ssize_t fixed_show(struct device *dev, struct device_attribute *a, 948 char *buf) 949 { 950 struct mei_cl_device *cldev = to_mei_cl_device(dev); 951 u8 fixed = mei_me_cl_fixed(cldev->me_cl); 952 953 return sprintf(buf, "%d", fixed); 954 } 955 static DEVICE_ATTR_RO(fixed); 956 957 static ssize_t vtag_show(struct device *dev, struct device_attribute *a, 958 char *buf) 959 { 960 struct mei_cl_device *cldev = to_mei_cl_device(dev); 961 bool vt = mei_me_cl_vt(cldev->me_cl); 962 963 return sprintf(buf, "%d", vt); 964 } 965 static DEVICE_ATTR_RO(vtag); 966 967 static ssize_t max_len_show(struct device *dev, struct device_attribute *a, 968 char *buf) 969 { 970 struct mei_cl_device *cldev = to_mei_cl_device(dev); 971 u32 maxlen = mei_me_cl_max_len(cldev->me_cl); 972 973 return sprintf(buf, "%u", maxlen); 974 } 975 static DEVICE_ATTR_RO(max_len); 976 977 static struct attribute *mei_cldev_attrs[] = { 978 &dev_attr_name.attr, 979 &dev_attr_uuid.attr, 980 &dev_attr_version.attr, 981 &dev_attr_modalias.attr, 982 &dev_attr_max_conn.attr, 983 &dev_attr_fixed.attr, 984 &dev_attr_vtag.attr, 985 &dev_attr_max_len.attr, 986 NULL, 987 }; 988 ATTRIBUTE_GROUPS(mei_cldev); 989 990 /** 991 * mei_cl_device_uevent - me client bus uevent handler 992 * 993 * @dev: device 994 * @env: uevent kobject 995 * 996 * Return: 0 on success -ENOMEM on when add_uevent_var fails 997 */ 998 static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env) 999 { 1000 struct mei_cl_device *cldev = to_mei_cl_device(dev); 1001 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); 1002 u8 version = mei_me_cl_ver(cldev->me_cl); 1003 1004 if (add_uevent_var(env, "MEI_CL_VERSION=%d", version)) 1005 return -ENOMEM; 1006 1007 if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid)) 1008 return -ENOMEM; 1009 1010 if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name)) 1011 return -ENOMEM; 1012 1013 if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:", 1014 cldev->name, uuid, version)) 1015 return -ENOMEM; 1016 1017 return 0; 1018 } 1019 1020 static struct bus_type mei_cl_bus_type = { 1021 .name = "mei", 1022 .dev_groups = mei_cldev_groups, 1023 .match = mei_cl_device_match, 1024 .probe = mei_cl_device_probe, 1025 .remove = mei_cl_device_remove, 1026 .uevent = mei_cl_device_uevent, 1027 }; 1028 1029 static struct mei_device *mei_dev_bus_get(struct mei_device *bus) 1030 { 1031 if (bus) 1032 get_device(bus->dev); 1033 1034 return bus; 1035 } 1036 1037 static void mei_dev_bus_put(struct mei_device *bus) 1038 { 1039 if (bus) 1040 put_device(bus->dev); 1041 } 1042 1043 static void mei_cl_bus_dev_release(struct device *dev) 1044 { 1045 struct mei_cl_device *cldev = to_mei_cl_device(dev); 1046 1047 if (!cldev) 1048 return; 1049 1050 mei_me_cl_put(cldev->me_cl); 1051 mei_dev_bus_put(cldev->bus); 1052 mei_cl_unlink(cldev->cl); 1053 kfree(cldev->cl); 1054 kfree(cldev); 1055 } 1056 1057 static const struct device_type mei_cl_device_type = { 1058 .release = mei_cl_bus_dev_release, 1059 }; 1060 1061 /** 1062 * mei_cl_bus_set_name - set device name for me client device 1063 * <controller>-<client device> 1064 * Example: 0000:00:16.0-55213584-9a29-4916-badf-0fb7ed682aeb 1065 * 1066 * @cldev: me client device 1067 */ 1068 static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev) 1069 { 1070 dev_set_name(&cldev->dev, "%s-%pUl", 1071 dev_name(cldev->bus->dev), 1072 mei_me_cl_uuid(cldev->me_cl)); 1073 } 1074 1075 /** 1076 * mei_cl_bus_dev_alloc - initialize and allocate mei client device 1077 * 1078 * @bus: mei device 1079 * @me_cl: me client 1080 * 1081 * Return: allocated device structur or NULL on allocation failure 1082 */ 1083 static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus, 1084 struct mei_me_client *me_cl) 1085 { 1086 struct mei_cl_device *cldev; 1087 struct mei_cl *cl; 1088 1089 cldev = kzalloc(sizeof(*cldev), GFP_KERNEL); 1090 if (!cldev) 1091 return NULL; 1092 1093 cl = mei_cl_allocate(bus); 1094 if (!cl) { 1095 kfree(cldev); 1096 return NULL; 1097 } 1098 1099 device_initialize(&cldev->dev); 1100 cldev->dev.parent = bus->dev; 1101 cldev->dev.bus = &mei_cl_bus_type; 1102 cldev->dev.type = &mei_cl_device_type; 1103 cldev->bus = mei_dev_bus_get(bus); 1104 cldev->me_cl = mei_me_cl_get(me_cl); 1105 cldev->cl = cl; 1106 mei_cl_bus_set_name(cldev); 1107 cldev->is_added = 0; 1108 INIT_LIST_HEAD(&cldev->bus_list); 1109 1110 return cldev; 1111 } 1112 1113 /** 1114 * mei_cl_bus_dev_setup - setup me client device 1115 * run fix up routines and set the device name 1116 * 1117 * @bus: mei device 1118 * @cldev: me client device 1119 * 1120 * Return: true if the device is eligible for enumeration 1121 */ 1122 static bool mei_cl_bus_dev_setup(struct mei_device *bus, 1123 struct mei_cl_device *cldev) 1124 { 1125 cldev->do_match = 1; 1126 mei_cl_bus_dev_fixup(cldev); 1127 1128 /* the device name can change during fix up */ 1129 if (cldev->do_match) 1130 mei_cl_bus_set_name(cldev); 1131 1132 return cldev->do_match == 1; 1133 } 1134 1135 /** 1136 * mei_cl_bus_dev_add - add me client devices 1137 * 1138 * @cldev: me client device 1139 * 1140 * Return: 0 on success; < 0 on failre 1141 */ 1142 static int mei_cl_bus_dev_add(struct mei_cl_device *cldev) 1143 { 1144 int ret; 1145 1146 dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n", 1147 mei_me_cl_uuid(cldev->me_cl), 1148 mei_me_cl_ver(cldev->me_cl)); 1149 ret = device_add(&cldev->dev); 1150 if (!ret) 1151 cldev->is_added = 1; 1152 1153 return ret; 1154 } 1155 1156 /** 1157 * mei_cl_bus_dev_stop - stop the driver 1158 * 1159 * @cldev: me client device 1160 */ 1161 static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev) 1162 { 1163 if (cldev->is_added) 1164 device_release_driver(&cldev->dev); 1165 } 1166 1167 /** 1168 * mei_cl_bus_dev_destroy - destroy me client devices object 1169 * 1170 * @cldev: me client device 1171 * 1172 * Locking: called under "dev->cl_bus_lock" lock 1173 */ 1174 static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev) 1175 { 1176 1177 WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock)); 1178 1179 if (!cldev->is_added) 1180 return; 1181 1182 device_del(&cldev->dev); 1183 1184 list_del_init(&cldev->bus_list); 1185 1186 cldev->is_added = 0; 1187 put_device(&cldev->dev); 1188 } 1189 1190 /** 1191 * mei_cl_bus_remove_device - remove a devices form the bus 1192 * 1193 * @cldev: me client device 1194 */ 1195 static void mei_cl_bus_remove_device(struct mei_cl_device *cldev) 1196 { 1197 mei_cl_bus_dev_stop(cldev); 1198 mei_cl_bus_dev_destroy(cldev); 1199 } 1200 1201 /** 1202 * mei_cl_bus_remove_devices - remove all devices form the bus 1203 * 1204 * @bus: mei device 1205 */ 1206 void mei_cl_bus_remove_devices(struct mei_device *bus) 1207 { 1208 struct mei_cl_device *cldev, *next; 1209 1210 mutex_lock(&bus->cl_bus_lock); 1211 list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list) 1212 mei_cl_bus_remove_device(cldev); 1213 mutex_unlock(&bus->cl_bus_lock); 1214 } 1215 1216 1217 /** 1218 * mei_cl_bus_dev_init - allocate and initializes an mei client devices 1219 * based on me client 1220 * 1221 * @bus: mei device 1222 * @me_cl: me client 1223 * 1224 * Locking: called under "dev->cl_bus_lock" lock 1225 */ 1226 static void mei_cl_bus_dev_init(struct mei_device *bus, 1227 struct mei_me_client *me_cl) 1228 { 1229 struct mei_cl_device *cldev; 1230 1231 WARN_ON(!mutex_is_locked(&bus->cl_bus_lock)); 1232 1233 dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl)); 1234 1235 if (me_cl->bus_added) 1236 return; 1237 1238 cldev = mei_cl_bus_dev_alloc(bus, me_cl); 1239 if (!cldev) 1240 return; 1241 1242 me_cl->bus_added = true; 1243 list_add_tail(&cldev->bus_list, &bus->device_list); 1244 1245 } 1246 1247 /** 1248 * mei_cl_bus_rescan - scan me clients list and add create 1249 * devices for eligible clients 1250 * 1251 * @bus: mei device 1252 */ 1253 static void mei_cl_bus_rescan(struct mei_device *bus) 1254 { 1255 struct mei_cl_device *cldev, *n; 1256 struct mei_me_client *me_cl; 1257 1258 mutex_lock(&bus->cl_bus_lock); 1259 1260 down_read(&bus->me_clients_rwsem); 1261 list_for_each_entry(me_cl, &bus->me_clients, list) 1262 mei_cl_bus_dev_init(bus, me_cl); 1263 up_read(&bus->me_clients_rwsem); 1264 1265 list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) { 1266 1267 if (!mei_me_cl_is_active(cldev->me_cl)) { 1268 mei_cl_bus_remove_device(cldev); 1269 continue; 1270 } 1271 1272 if (cldev->is_added) 1273 continue; 1274 1275 if (mei_cl_bus_dev_setup(bus, cldev)) 1276 mei_cl_bus_dev_add(cldev); 1277 else { 1278 list_del_init(&cldev->bus_list); 1279 put_device(&cldev->dev); 1280 } 1281 } 1282 mutex_unlock(&bus->cl_bus_lock); 1283 1284 dev_dbg(bus->dev, "rescan end"); 1285 } 1286 1287 void mei_cl_bus_rescan_work(struct work_struct *work) 1288 { 1289 struct mei_device *bus = 1290 container_of(work, struct mei_device, bus_rescan_work); 1291 1292 mei_cl_bus_rescan(bus); 1293 } 1294 1295 int __mei_cldev_driver_register(struct mei_cl_driver *cldrv, 1296 struct module *owner) 1297 { 1298 int err; 1299 1300 cldrv->driver.name = cldrv->name; 1301 cldrv->driver.owner = owner; 1302 cldrv->driver.bus = &mei_cl_bus_type; 1303 1304 err = driver_register(&cldrv->driver); 1305 if (err) 1306 return err; 1307 1308 pr_debug("mei: driver [%s] registered\n", cldrv->driver.name); 1309 1310 return 0; 1311 } 1312 EXPORT_SYMBOL_GPL(__mei_cldev_driver_register); 1313 1314 void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv) 1315 { 1316 driver_unregister(&cldrv->driver); 1317 1318 pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name); 1319 } 1320 EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister); 1321 1322 1323 int __init mei_cl_bus_init(void) 1324 { 1325 return bus_register(&mei_cl_bus_type); 1326 } 1327 1328 void __exit mei_cl_bus_exit(void) 1329 { 1330 bus_unregister(&mei_cl_bus_type); 1331 } 1332