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