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