treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / misc / mei / main.c
blobf17297f2943d37fc82a7fce8d77eeebaa1bcf79d
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2003-2018, Intel Corporation. All rights reserved.
4 * Intel Management Engine Interface (Intel MEI) Linux driver
5 */
7 #include <linux/module.h>
8 #include <linux/moduleparam.h>
9 #include <linux/kernel.h>
10 #include <linux/device.h>
11 #include <linux/slab.h>
12 #include <linux/fs.h>
13 #include <linux/errno.h>
14 #include <linux/types.h>
15 #include <linux/fcntl.h>
16 #include <linux/poll.h>
17 #include <linux/init.h>
18 #include <linux/ioctl.h>
19 #include <linux/cdev.h>
20 #include <linux/sched/signal.h>
21 #include <linux/uuid.h>
22 #include <linux/compat.h>
23 #include <linux/jiffies.h>
24 #include <linux/interrupt.h>
26 #include <linux/mei.h>
28 #include "mei_dev.h"
29 #include "client.h"
31 static struct class *mei_class;
32 static dev_t mei_devt;
33 #define MEI_MAX_DEVS MINORMASK
34 static DEFINE_MUTEX(mei_minor_lock);
35 static DEFINE_IDR(mei_idr);
37 /**
38 * mei_open - the open function
40 * @inode: pointer to inode structure
41 * @file: pointer to file structure
43 * Return: 0 on success, <0 on error
45 static int mei_open(struct inode *inode, struct file *file)
47 struct mei_device *dev;
48 struct mei_cl *cl;
50 int err;
52 dev = container_of(inode->i_cdev, struct mei_device, cdev);
53 if (!dev)
54 return -ENODEV;
56 mutex_lock(&dev->device_lock);
58 if (dev->dev_state != MEI_DEV_ENABLED) {
59 dev_dbg(dev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
60 mei_dev_state_str(dev->dev_state));
61 err = -ENODEV;
62 goto err_unlock;
65 cl = mei_cl_alloc_linked(dev);
66 if (IS_ERR(cl)) {
67 err = PTR_ERR(cl);
68 goto err_unlock;
71 cl->fp = file;
72 file->private_data = cl;
74 mutex_unlock(&dev->device_lock);
76 return nonseekable_open(inode, file);
78 err_unlock:
79 mutex_unlock(&dev->device_lock);
80 return err;
83 /**
84 * mei_release - the release function
86 * @inode: pointer to inode structure
87 * @file: pointer to file structure
89 * Return: 0 on success, <0 on error
91 static int mei_release(struct inode *inode, struct file *file)
93 struct mei_cl *cl = file->private_data;
94 struct mei_device *dev;
95 int rets;
97 if (WARN_ON(!cl || !cl->dev))
98 return -ENODEV;
100 dev = cl->dev;
102 mutex_lock(&dev->device_lock);
104 rets = mei_cl_disconnect(cl);
106 mei_cl_flush_queues(cl, file);
107 cl_dbg(dev, cl, "removing\n");
109 mei_cl_unlink(cl);
111 file->private_data = NULL;
113 kfree(cl);
115 mutex_unlock(&dev->device_lock);
116 return rets;
121 * mei_read - the read function.
123 * @file: pointer to file structure
124 * @ubuf: pointer to user buffer
125 * @length: buffer length
126 * @offset: data offset in buffer
128 * Return: >=0 data length on success , <0 on error
130 static ssize_t mei_read(struct file *file, char __user *ubuf,
131 size_t length, loff_t *offset)
133 struct mei_cl *cl = file->private_data;
134 struct mei_device *dev;
135 struct mei_cl_cb *cb = NULL;
136 bool nonblock = !!(file->f_flags & O_NONBLOCK);
137 ssize_t rets;
139 if (WARN_ON(!cl || !cl->dev))
140 return -ENODEV;
142 dev = cl->dev;
145 mutex_lock(&dev->device_lock);
146 if (dev->dev_state != MEI_DEV_ENABLED) {
147 rets = -ENODEV;
148 goto out;
151 if (length == 0) {
152 rets = 0;
153 goto out;
156 if (ubuf == NULL) {
157 rets = -EMSGSIZE;
158 goto out;
161 cb = mei_cl_read_cb(cl, file);
162 if (cb)
163 goto copy_buffer;
165 if (*offset > 0)
166 *offset = 0;
168 rets = mei_cl_read_start(cl, length, file);
169 if (rets && rets != -EBUSY) {
170 cl_dbg(dev, cl, "mei start read failure status = %zd\n", rets);
171 goto out;
174 if (nonblock) {
175 rets = -EAGAIN;
176 goto out;
179 mutex_unlock(&dev->device_lock);
180 if (wait_event_interruptible(cl->rx_wait,
181 !list_empty(&cl->rd_completed) ||
182 !mei_cl_is_connected(cl))) {
183 if (signal_pending(current))
184 return -EINTR;
185 return -ERESTARTSYS;
187 mutex_lock(&dev->device_lock);
189 if (!mei_cl_is_connected(cl)) {
190 rets = -ENODEV;
191 goto out;
194 cb = mei_cl_read_cb(cl, file);
195 if (!cb) {
196 rets = 0;
197 goto out;
200 copy_buffer:
201 /* now copy the data to user space */
202 if (cb->status) {
203 rets = cb->status;
204 cl_dbg(dev, cl, "read operation failed %zd\n", rets);
205 goto free;
208 cl_dbg(dev, cl, "buf.size = %zu buf.idx = %zu offset = %lld\n",
209 cb->buf.size, cb->buf_idx, *offset);
210 if (*offset >= cb->buf_idx) {
211 rets = 0;
212 goto free;
215 /* length is being truncated to PAGE_SIZE,
216 * however buf_idx may point beyond that */
217 length = min_t(size_t, length, cb->buf_idx - *offset);
219 if (copy_to_user(ubuf, cb->buf.data + *offset, length)) {
220 dev_dbg(dev->dev, "failed to copy data to userland\n");
221 rets = -EFAULT;
222 goto free;
225 rets = length;
226 *offset += length;
227 /* not all data was read, keep the cb */
228 if (*offset < cb->buf_idx)
229 goto out;
231 free:
232 mei_io_cb_free(cb);
233 *offset = 0;
235 out:
236 cl_dbg(dev, cl, "end mei read rets = %zd\n", rets);
237 mutex_unlock(&dev->device_lock);
238 return rets;
241 * mei_write - the write function.
243 * @file: pointer to file structure
244 * @ubuf: pointer to user buffer
245 * @length: buffer length
246 * @offset: data offset in buffer
248 * Return: >=0 data length on success , <0 on error
250 static ssize_t mei_write(struct file *file, const char __user *ubuf,
251 size_t length, loff_t *offset)
253 struct mei_cl *cl = file->private_data;
254 struct mei_cl_cb *cb;
255 struct mei_device *dev;
256 ssize_t rets;
258 if (WARN_ON(!cl || !cl->dev))
259 return -ENODEV;
261 dev = cl->dev;
263 mutex_lock(&dev->device_lock);
265 if (dev->dev_state != MEI_DEV_ENABLED) {
266 rets = -ENODEV;
267 goto out;
270 if (!mei_cl_is_connected(cl)) {
271 cl_err(dev, cl, "is not connected");
272 rets = -ENODEV;
273 goto out;
276 if (!mei_me_cl_is_active(cl->me_cl)) {
277 rets = -ENOTTY;
278 goto out;
281 if (length > mei_cl_mtu(cl)) {
282 rets = -EFBIG;
283 goto out;
286 if (length == 0) {
287 rets = 0;
288 goto out;
291 while (cl->tx_cb_queued >= dev->tx_queue_limit) {
292 if (file->f_flags & O_NONBLOCK) {
293 rets = -EAGAIN;
294 goto out;
296 mutex_unlock(&dev->device_lock);
297 rets = wait_event_interruptible(cl->tx_wait,
298 cl->writing_state == MEI_WRITE_COMPLETE ||
299 (!mei_cl_is_connected(cl)));
300 mutex_lock(&dev->device_lock);
301 if (rets) {
302 if (signal_pending(current))
303 rets = -EINTR;
304 goto out;
306 if (!mei_cl_is_connected(cl)) {
307 rets = -ENODEV;
308 goto out;
312 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, file);
313 if (!cb) {
314 rets = -ENOMEM;
315 goto out;
318 rets = copy_from_user(cb->buf.data, ubuf, length);
319 if (rets) {
320 dev_dbg(dev->dev, "failed to copy data from userland\n");
321 rets = -EFAULT;
322 mei_io_cb_free(cb);
323 goto out;
326 rets = mei_cl_write(cl, cb);
327 out:
328 mutex_unlock(&dev->device_lock);
329 return rets;
333 * mei_ioctl_connect_client - the connect to fw client IOCTL function
335 * @file: private data of the file object
336 * @data: IOCTL connect data, input and output parameters
338 * Locking: called under "dev->device_lock" lock
340 * Return: 0 on success, <0 on failure.
342 static int mei_ioctl_connect_client(struct file *file,
343 struct mei_connect_client_data *data)
345 struct mei_device *dev;
346 struct mei_client *client;
347 struct mei_me_client *me_cl;
348 struct mei_cl *cl;
349 int rets;
351 cl = file->private_data;
352 dev = cl->dev;
354 if (dev->dev_state != MEI_DEV_ENABLED)
355 return -ENODEV;
357 if (cl->state != MEI_FILE_INITIALIZING &&
358 cl->state != MEI_FILE_DISCONNECTED)
359 return -EBUSY;
361 /* find ME client we're trying to connect to */
362 me_cl = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
363 if (!me_cl) {
364 dev_dbg(dev->dev, "Cannot connect to FW Client UUID = %pUl\n",
365 &data->in_client_uuid);
366 rets = -ENOTTY;
367 goto end;
370 if (me_cl->props.fixed_address) {
371 bool forbidden = dev->override_fixed_address ?
372 !dev->allow_fixed_address : !dev->hbm_f_fa_supported;
373 if (forbidden) {
374 dev_dbg(dev->dev, "Connection forbidden to FW Client UUID = %pUl\n",
375 &data->in_client_uuid);
376 rets = -ENOTTY;
377 goto end;
381 dev_dbg(dev->dev, "Connect to FW Client ID = %d\n",
382 me_cl->client_id);
383 dev_dbg(dev->dev, "FW Client - Protocol Version = %d\n",
384 me_cl->props.protocol_version);
385 dev_dbg(dev->dev, "FW Client - Max Msg Len = %d\n",
386 me_cl->props.max_msg_length);
388 /* prepare the output buffer */
389 client = &data->out_client_properties;
390 client->max_msg_length = me_cl->props.max_msg_length;
391 client->protocol_version = me_cl->props.protocol_version;
392 dev_dbg(dev->dev, "Can connect?\n");
394 rets = mei_cl_connect(cl, me_cl, file);
396 end:
397 mei_me_cl_put(me_cl);
398 return rets;
402 * mei_ioctl_client_notify_request -
403 * propagate event notification request to client
405 * @file: pointer to file structure
406 * @request: 0 - disable, 1 - enable
408 * Return: 0 on success , <0 on error
410 static int mei_ioctl_client_notify_request(const struct file *file, u32 request)
412 struct mei_cl *cl = file->private_data;
414 if (request != MEI_HBM_NOTIFICATION_START &&
415 request != MEI_HBM_NOTIFICATION_STOP)
416 return -EINVAL;
418 return mei_cl_notify_request(cl, file, (u8)request);
422 * mei_ioctl_client_notify_get - wait for notification request
424 * @file: pointer to file structure
425 * @notify_get: 0 - disable, 1 - enable
427 * Return: 0 on success , <0 on error
429 static int mei_ioctl_client_notify_get(const struct file *file, u32 *notify_get)
431 struct mei_cl *cl = file->private_data;
432 bool notify_ev;
433 bool block = (file->f_flags & O_NONBLOCK) == 0;
434 int rets;
436 rets = mei_cl_notify_get(cl, block, &notify_ev);
437 if (rets)
438 return rets;
440 *notify_get = notify_ev ? 1 : 0;
441 return 0;
445 * mei_ioctl - the IOCTL function
447 * @file: pointer to file structure
448 * @cmd: ioctl command
449 * @data: pointer to mei message structure
451 * Return: 0 on success , <0 on error
453 static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
455 struct mei_device *dev;
456 struct mei_cl *cl = file->private_data;
457 struct mei_connect_client_data connect_data;
458 u32 notify_get, notify_req;
459 int rets;
462 if (WARN_ON(!cl || !cl->dev))
463 return -ENODEV;
465 dev = cl->dev;
467 dev_dbg(dev->dev, "IOCTL cmd = 0x%x", cmd);
469 mutex_lock(&dev->device_lock);
470 if (dev->dev_state != MEI_DEV_ENABLED) {
471 rets = -ENODEV;
472 goto out;
475 switch (cmd) {
476 case IOCTL_MEI_CONNECT_CLIENT:
477 dev_dbg(dev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
478 if (copy_from_user(&connect_data, (char __user *)data,
479 sizeof(struct mei_connect_client_data))) {
480 dev_dbg(dev->dev, "failed to copy data from userland\n");
481 rets = -EFAULT;
482 goto out;
485 rets = mei_ioctl_connect_client(file, &connect_data);
486 if (rets)
487 goto out;
489 /* if all is ok, copying the data back to user. */
490 if (copy_to_user((char __user *)data, &connect_data,
491 sizeof(struct mei_connect_client_data))) {
492 dev_dbg(dev->dev, "failed to copy data to userland\n");
493 rets = -EFAULT;
494 goto out;
497 break;
499 case IOCTL_MEI_NOTIFY_SET:
500 dev_dbg(dev->dev, ": IOCTL_MEI_NOTIFY_SET.\n");
501 if (copy_from_user(&notify_req,
502 (char __user *)data, sizeof(notify_req))) {
503 dev_dbg(dev->dev, "failed to copy data from userland\n");
504 rets = -EFAULT;
505 goto out;
507 rets = mei_ioctl_client_notify_request(file, notify_req);
508 break;
510 case IOCTL_MEI_NOTIFY_GET:
511 dev_dbg(dev->dev, ": IOCTL_MEI_NOTIFY_GET.\n");
512 rets = mei_ioctl_client_notify_get(file, &notify_get);
513 if (rets)
514 goto out;
516 dev_dbg(dev->dev, "copy connect data to user\n");
517 if (copy_to_user((char __user *)data,
518 &notify_get, sizeof(notify_get))) {
519 dev_dbg(dev->dev, "failed to copy data to userland\n");
520 rets = -EFAULT;
521 goto out;
524 break;
526 default:
527 rets = -ENOIOCTLCMD;
530 out:
531 mutex_unlock(&dev->device_lock);
532 return rets;
536 * mei_poll - the poll function
538 * @file: pointer to file structure
539 * @wait: pointer to poll_table structure
541 * Return: poll mask
543 static __poll_t mei_poll(struct file *file, poll_table *wait)
545 __poll_t req_events = poll_requested_events(wait);
546 struct mei_cl *cl = file->private_data;
547 struct mei_device *dev;
548 __poll_t mask = 0;
549 bool notify_en;
551 if (WARN_ON(!cl || !cl->dev))
552 return EPOLLERR;
554 dev = cl->dev;
556 mutex_lock(&dev->device_lock);
558 notify_en = cl->notify_en && (req_events & EPOLLPRI);
560 if (dev->dev_state != MEI_DEV_ENABLED ||
561 !mei_cl_is_connected(cl)) {
562 mask = EPOLLERR;
563 goto out;
566 if (notify_en) {
567 poll_wait(file, &cl->ev_wait, wait);
568 if (cl->notify_ev)
569 mask |= EPOLLPRI;
572 if (req_events & (EPOLLIN | EPOLLRDNORM)) {
573 poll_wait(file, &cl->rx_wait, wait);
575 if (!list_empty(&cl->rd_completed))
576 mask |= EPOLLIN | EPOLLRDNORM;
577 else
578 mei_cl_read_start(cl, mei_cl_mtu(cl), file);
581 if (req_events & (EPOLLOUT | EPOLLWRNORM)) {
582 poll_wait(file, &cl->tx_wait, wait);
583 if (cl->tx_cb_queued < dev->tx_queue_limit)
584 mask |= EPOLLOUT | EPOLLWRNORM;
587 out:
588 mutex_unlock(&dev->device_lock);
589 return mask;
593 * mei_cl_is_write_queued - check if the client has pending writes.
595 * @cl: writing host client
597 * Return: true if client is writing, false otherwise.
599 static bool mei_cl_is_write_queued(struct mei_cl *cl)
601 struct mei_device *dev = cl->dev;
602 struct mei_cl_cb *cb;
604 list_for_each_entry(cb, &dev->write_list, list)
605 if (cb->cl == cl)
606 return true;
607 list_for_each_entry(cb, &dev->write_waiting_list, list)
608 if (cb->cl == cl)
609 return true;
610 return false;
614 * mei_fsync - the fsync handler
616 * @fp: pointer to file structure
617 * @start: unused
618 * @end: unused
619 * @datasync: unused
621 * Return: 0 on success, -ENODEV if client is not connected
623 static int mei_fsync(struct file *fp, loff_t start, loff_t end, int datasync)
625 struct mei_cl *cl = fp->private_data;
626 struct mei_device *dev;
627 int rets;
629 if (WARN_ON(!cl || !cl->dev))
630 return -ENODEV;
632 dev = cl->dev;
634 mutex_lock(&dev->device_lock);
636 if (dev->dev_state != MEI_DEV_ENABLED || !mei_cl_is_connected(cl)) {
637 rets = -ENODEV;
638 goto out;
641 while (mei_cl_is_write_queued(cl)) {
642 mutex_unlock(&dev->device_lock);
643 rets = wait_event_interruptible(cl->tx_wait,
644 cl->writing_state == MEI_WRITE_COMPLETE ||
645 !mei_cl_is_connected(cl));
646 mutex_lock(&dev->device_lock);
647 if (rets) {
648 if (signal_pending(current))
649 rets = -EINTR;
650 goto out;
652 if (!mei_cl_is_connected(cl)) {
653 rets = -ENODEV;
654 goto out;
657 rets = 0;
658 out:
659 mutex_unlock(&dev->device_lock);
660 return rets;
664 * mei_fasync - asynchronous io support
666 * @fd: file descriptor
667 * @file: pointer to file structure
668 * @band: band bitmap
670 * Return: negative on error,
671 * 0 if it did no changes,
672 * and positive a process was added or deleted
674 static int mei_fasync(int fd, struct file *file, int band)
677 struct mei_cl *cl = file->private_data;
679 if (!mei_cl_is_connected(cl))
680 return -ENODEV;
682 return fasync_helper(fd, file, band, &cl->ev_async);
686 * trc_show - mei device trc attribute show method
688 * @device: device pointer
689 * @attr: attribute pointer
690 * @buf: char out buffer
692 * Return: number of the bytes printed into buf or error
694 static ssize_t trc_show(struct device *device,
695 struct device_attribute *attr, char *buf)
697 struct mei_device *dev = dev_get_drvdata(device);
698 u32 trc;
699 int ret;
701 ret = mei_trc_status(dev, &trc);
702 if (ret)
703 return ret;
704 return sprintf(buf, "%08X\n", trc);
706 static DEVICE_ATTR_RO(trc);
709 * fw_status_show - mei device fw_status attribute show method
711 * @device: device pointer
712 * @attr: attribute pointer
713 * @buf: char out buffer
715 * Return: number of the bytes printed into buf or error
717 static ssize_t fw_status_show(struct device *device,
718 struct device_attribute *attr, char *buf)
720 struct mei_device *dev = dev_get_drvdata(device);
721 struct mei_fw_status fw_status;
722 int err, i;
723 ssize_t cnt = 0;
725 mutex_lock(&dev->device_lock);
726 err = mei_fw_status(dev, &fw_status);
727 mutex_unlock(&dev->device_lock);
728 if (err) {
729 dev_err(device, "read fw_status error = %d\n", err);
730 return err;
733 for (i = 0; i < fw_status.count; i++)
734 cnt += scnprintf(buf + cnt, PAGE_SIZE - cnt, "%08X\n",
735 fw_status.status[i]);
736 return cnt;
738 static DEVICE_ATTR_RO(fw_status);
741 * hbm_ver_show - display HBM protocol version negotiated with FW
743 * @device: device pointer
744 * @attr: attribute pointer
745 * @buf: char out buffer
747 * Return: number of the bytes printed into buf or error
749 static ssize_t hbm_ver_show(struct device *device,
750 struct device_attribute *attr, char *buf)
752 struct mei_device *dev = dev_get_drvdata(device);
753 struct hbm_version ver;
755 mutex_lock(&dev->device_lock);
756 ver = dev->version;
757 mutex_unlock(&dev->device_lock);
759 return sprintf(buf, "%u.%u\n", ver.major_version, ver.minor_version);
761 static DEVICE_ATTR_RO(hbm_ver);
764 * hbm_ver_drv_show - display HBM protocol version advertised by driver
766 * @device: device pointer
767 * @attr: attribute pointer
768 * @buf: char out buffer
770 * Return: number of the bytes printed into buf or error
772 static ssize_t hbm_ver_drv_show(struct device *device,
773 struct device_attribute *attr, char *buf)
775 return sprintf(buf, "%u.%u\n", HBM_MAJOR_VERSION, HBM_MINOR_VERSION);
777 static DEVICE_ATTR_RO(hbm_ver_drv);
779 static ssize_t tx_queue_limit_show(struct device *device,
780 struct device_attribute *attr, char *buf)
782 struct mei_device *dev = dev_get_drvdata(device);
783 u8 size = 0;
785 mutex_lock(&dev->device_lock);
786 size = dev->tx_queue_limit;
787 mutex_unlock(&dev->device_lock);
789 return snprintf(buf, PAGE_SIZE, "%u\n", size);
792 static ssize_t tx_queue_limit_store(struct device *device,
793 struct device_attribute *attr,
794 const char *buf, size_t count)
796 struct mei_device *dev = dev_get_drvdata(device);
797 u8 limit;
798 unsigned int inp;
799 int err;
801 err = kstrtouint(buf, 10, &inp);
802 if (err)
803 return err;
804 if (inp > MEI_TX_QUEUE_LIMIT_MAX || inp < MEI_TX_QUEUE_LIMIT_MIN)
805 return -EINVAL;
806 limit = inp;
808 mutex_lock(&dev->device_lock);
809 dev->tx_queue_limit = limit;
810 mutex_unlock(&dev->device_lock);
812 return count;
814 static DEVICE_ATTR_RW(tx_queue_limit);
817 * fw_ver_show - display ME FW version
819 * @device: device pointer
820 * @attr: attribute pointer
821 * @buf: char out buffer
823 * Return: number of the bytes printed into buf or error
825 static ssize_t fw_ver_show(struct device *device,
826 struct device_attribute *attr, char *buf)
828 struct mei_device *dev = dev_get_drvdata(device);
829 struct mei_fw_version *ver;
830 ssize_t cnt = 0;
831 int i;
833 ver = dev->fw_ver;
835 for (i = 0; i < MEI_MAX_FW_VER_BLOCKS; i++)
836 cnt += scnprintf(buf + cnt, PAGE_SIZE - cnt, "%u:%u.%u.%u.%u\n",
837 ver[i].platform, ver[i].major, ver[i].minor,
838 ver[i].hotfix, ver[i].buildno);
839 return cnt;
841 static DEVICE_ATTR_RO(fw_ver);
844 * dev_state_show - display device state
846 * @device: device pointer
847 * @attr: attribute pointer
848 * @buf: char out buffer
850 * Return: number of the bytes printed into buf or error
852 static ssize_t dev_state_show(struct device *device,
853 struct device_attribute *attr, char *buf)
855 struct mei_device *dev = dev_get_drvdata(device);
856 enum mei_dev_state dev_state;
858 mutex_lock(&dev->device_lock);
859 dev_state = dev->dev_state;
860 mutex_unlock(&dev->device_lock);
862 return sprintf(buf, "%s", mei_dev_state_str(dev_state));
864 static DEVICE_ATTR_RO(dev_state);
867 * dev_set_devstate: set to new device state and notify sysfs file.
869 * @dev: mei_device
870 * @state: new device state
872 void mei_set_devstate(struct mei_device *dev, enum mei_dev_state state)
874 struct device *clsdev;
876 if (dev->dev_state == state)
877 return;
879 dev->dev_state = state;
881 clsdev = class_find_device_by_devt(mei_class, dev->cdev.dev);
882 if (clsdev) {
883 sysfs_notify(&clsdev->kobj, NULL, "dev_state");
884 put_device(clsdev);
888 static struct attribute *mei_attrs[] = {
889 &dev_attr_fw_status.attr,
890 &dev_attr_hbm_ver.attr,
891 &dev_attr_hbm_ver_drv.attr,
892 &dev_attr_tx_queue_limit.attr,
893 &dev_attr_fw_ver.attr,
894 &dev_attr_dev_state.attr,
895 &dev_attr_trc.attr,
896 NULL
898 ATTRIBUTE_GROUPS(mei);
901 * file operations structure will be used for mei char device.
903 static const struct file_operations mei_fops = {
904 .owner = THIS_MODULE,
905 .read = mei_read,
906 .unlocked_ioctl = mei_ioctl,
907 .compat_ioctl = compat_ptr_ioctl,
908 .open = mei_open,
909 .release = mei_release,
910 .write = mei_write,
911 .poll = mei_poll,
912 .fsync = mei_fsync,
913 .fasync = mei_fasync,
914 .llseek = no_llseek
918 * mei_minor_get - obtain next free device minor number
920 * @dev: device pointer
922 * Return: allocated minor, or -ENOSPC if no free minor left
924 static int mei_minor_get(struct mei_device *dev)
926 int ret;
928 mutex_lock(&mei_minor_lock);
929 ret = idr_alloc(&mei_idr, dev, 0, MEI_MAX_DEVS, GFP_KERNEL);
930 if (ret >= 0)
931 dev->minor = ret;
932 else if (ret == -ENOSPC)
933 dev_err(dev->dev, "too many mei devices\n");
935 mutex_unlock(&mei_minor_lock);
936 return ret;
940 * mei_minor_free - mark device minor number as free
942 * @dev: device pointer
944 static void mei_minor_free(struct mei_device *dev)
946 mutex_lock(&mei_minor_lock);
947 idr_remove(&mei_idr, dev->minor);
948 mutex_unlock(&mei_minor_lock);
951 int mei_register(struct mei_device *dev, struct device *parent)
953 struct device *clsdev; /* class device */
954 int ret, devno;
956 ret = mei_minor_get(dev);
957 if (ret < 0)
958 return ret;
960 /* Fill in the data structures */
961 devno = MKDEV(MAJOR(mei_devt), dev->minor);
962 cdev_init(&dev->cdev, &mei_fops);
963 dev->cdev.owner = parent->driver->owner;
965 /* Add the device */
966 ret = cdev_add(&dev->cdev, devno, 1);
967 if (ret) {
968 dev_err(parent, "unable to add device %d:%d\n",
969 MAJOR(mei_devt), dev->minor);
970 goto err_dev_add;
973 clsdev = device_create_with_groups(mei_class, parent, devno,
974 dev, mei_groups,
975 "mei%d", dev->minor);
977 if (IS_ERR(clsdev)) {
978 dev_err(parent, "unable to create device %d:%d\n",
979 MAJOR(mei_devt), dev->minor);
980 ret = PTR_ERR(clsdev);
981 goto err_dev_create;
984 mei_dbgfs_register(dev, dev_name(clsdev));
986 return 0;
988 err_dev_create:
989 cdev_del(&dev->cdev);
990 err_dev_add:
991 mei_minor_free(dev);
992 return ret;
994 EXPORT_SYMBOL_GPL(mei_register);
996 void mei_deregister(struct mei_device *dev)
998 int devno;
1000 devno = dev->cdev.dev;
1001 cdev_del(&dev->cdev);
1003 mei_dbgfs_deregister(dev);
1005 device_destroy(mei_class, devno);
1007 mei_minor_free(dev);
1009 EXPORT_SYMBOL_GPL(mei_deregister);
1011 static int __init mei_init(void)
1013 int ret;
1015 mei_class = class_create(THIS_MODULE, "mei");
1016 if (IS_ERR(mei_class)) {
1017 pr_err("couldn't create class\n");
1018 ret = PTR_ERR(mei_class);
1019 goto err;
1022 ret = alloc_chrdev_region(&mei_devt, 0, MEI_MAX_DEVS, "mei");
1023 if (ret < 0) {
1024 pr_err("unable to allocate char dev region\n");
1025 goto err_class;
1028 ret = mei_cl_bus_init();
1029 if (ret < 0) {
1030 pr_err("unable to initialize bus\n");
1031 goto err_chrdev;
1034 return 0;
1036 err_chrdev:
1037 unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
1038 err_class:
1039 class_destroy(mei_class);
1040 err:
1041 return ret;
1044 static void __exit mei_exit(void)
1046 unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
1047 class_destroy(mei_class);
1048 mei_cl_bus_exit();
1051 module_init(mei_init);
1052 module_exit(mei_exit);
1054 MODULE_AUTHOR("Intel Corporation");
1055 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1056 MODULE_LICENSE("GPL v2");