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