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