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