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