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