1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2003-2020, Intel Corporation. All rights reserved.
4 * Intel Management Engine Interface (Intel MEI) Linux driver
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>
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>
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);
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;
52 dev = container_of(inode->i_cdev, struct mei_device, cdev);
54 mutex_lock(&dev->device_lock);
56 if (dev->dev_state != MEI_DEV_ENABLED) {
57 dev_dbg(dev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
58 mei_dev_state_str(dev->dev_state));
63 cl = mei_cl_alloc_linked(dev);
70 file->private_data = cl;
72 mutex_unlock(&dev->device_lock);
74 return nonseekable_open(inode, file);
77 mutex_unlock(&dev->device_lock);
82 * mei_cl_vtag_remove_by_fp - remove vtag that corresponds to fp from list
85 * @fp: pointer to file structure
88 static void mei_cl_vtag_remove_by_fp(const struct mei_cl *cl,
89 const struct file *fp)
91 struct mei_cl_vtag *vtag_l, *next;
93 list_for_each_entry_safe(vtag_l, next, &cl->vtag_map, list) {
94 if (vtag_l->fp == fp) {
95 list_del(&vtag_l->list);
103 * mei_release - the release function
105 * @inode: pointer to inode structure
106 * @file: pointer to file structure
108 * Return: 0 on success, <0 on error
110 static int mei_release(struct inode *inode, struct file *file)
112 struct mei_cl *cl = file->private_data;
113 struct mei_device *dev;
116 if (WARN_ON(!cl || !cl->dev))
121 mutex_lock(&dev->device_lock);
123 mei_cl_vtag_remove_by_fp(cl, file);
125 if (!list_empty(&cl->vtag_map)) {
126 cl_dbg(dev, cl, "not the last vtag\n");
127 mei_cl_flush_queues(cl, file);
132 rets = mei_cl_disconnect(cl);
134 * Check again: This is necessary since disconnect releases the lock
135 * and another client can connect in the meantime.
137 if (!list_empty(&cl->vtag_map)) {
138 cl_dbg(dev, cl, "not the last vtag after disconnect\n");
139 mei_cl_flush_queues(cl, file);
143 mei_cl_flush_queues(cl, NULL);
144 cl_dbg(dev, cl, "removing\n");
150 file->private_data = NULL;
152 mutex_unlock(&dev->device_lock);
158 * mei_read - the read function.
160 * @file: pointer to file structure
161 * @ubuf: pointer to user buffer
162 * @length: buffer length
163 * @offset: data offset in buffer
165 * Return: >=0 data length on success , <0 on error
167 static ssize_t mei_read(struct file *file, char __user *ubuf,
168 size_t length, loff_t *offset)
170 struct mei_cl *cl = file->private_data;
171 struct mei_device *dev;
172 struct mei_cl_cb *cb = NULL;
173 bool nonblock = !!(file->f_flags & O_NONBLOCK);
176 if (WARN_ON(!cl || !cl->dev))
182 mutex_lock(&dev->device_lock);
183 if (dev->dev_state != MEI_DEV_ENABLED) {
198 cb = mei_cl_read_cb(cl, file);
205 rets = mei_cl_read_start(cl, length, file);
206 if (rets && rets != -EBUSY) {
207 cl_dbg(dev, cl, "mei start read failure status = %zd\n", rets);
216 mutex_unlock(&dev->device_lock);
217 if (wait_event_interruptible(cl->rx_wait,
218 mei_cl_read_cb(cl, file) ||
219 !mei_cl_is_connected(cl))) {
220 if (signal_pending(current))
224 mutex_lock(&dev->device_lock);
226 if (!mei_cl_is_connected(cl)) {
231 cb = mei_cl_read_cb(cl, file);
238 /* now copy the data to user space */
241 cl_dbg(dev, cl, "read operation failed %zd\n", rets);
245 cl_dbg(dev, cl, "buf.size = %zu buf.idx = %zu offset = %lld\n",
246 cb->buf.size, cb->buf_idx, *offset);
247 if (*offset >= cb->buf_idx) {
252 /* length is being truncated to PAGE_SIZE,
253 * however buf_idx may point beyond that */
254 length = min_t(size_t, length, cb->buf_idx - *offset);
256 if (copy_to_user(ubuf, cb->buf.data + *offset, length)) {
257 dev_dbg(dev->dev, "failed to copy data to userland\n");
264 /* not all data was read, keep the cb */
265 if (*offset < cb->buf_idx)
269 mei_cl_del_rd_completed(cl, cb);
273 cl_dbg(dev, cl, "end mei read rets = %zd\n", rets);
274 mutex_unlock(&dev->device_lock);
279 * mei_cl_vtag_by_fp - obtain the vtag by file pointer
282 * @fp: pointer to file structure
284 * Return: vtag value on success, otherwise 0
286 static u8 mei_cl_vtag_by_fp(const struct mei_cl *cl, const struct file *fp)
288 struct mei_cl_vtag *cl_vtag;
293 list_for_each_entry(cl_vtag, &cl->vtag_map, list)
294 if (cl_vtag->fp == fp)
295 return cl_vtag->vtag;
300 * mei_write - the write function.
302 * @file: pointer to file structure
303 * @ubuf: pointer to user buffer
304 * @length: buffer length
305 * @offset: data offset in buffer
307 * Return: >=0 data length on success , <0 on error
309 static ssize_t mei_write(struct file *file, const char __user *ubuf,
310 size_t length, loff_t *offset)
312 struct mei_cl *cl = file->private_data;
313 struct mei_cl_cb *cb;
314 struct mei_device *dev;
317 if (WARN_ON(!cl || !cl->dev))
322 mutex_lock(&dev->device_lock);
324 if (dev->dev_state != MEI_DEV_ENABLED) {
329 if (!mei_cl_is_connected(cl)) {
330 cl_err(dev, cl, "is not connected");
335 if (!mei_me_cl_is_active(cl->me_cl)) {
340 if (length > mei_cl_mtu(cl)) {
350 while (cl->tx_cb_queued >= dev->tx_queue_limit) {
351 if (file->f_flags & O_NONBLOCK) {
355 mutex_unlock(&dev->device_lock);
356 rets = wait_event_interruptible(cl->tx_wait,
357 cl->writing_state == MEI_WRITE_COMPLETE ||
358 (!mei_cl_is_connected(cl)));
359 mutex_lock(&dev->device_lock);
361 if (signal_pending(current))
365 if (!mei_cl_is_connected(cl)) {
371 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, file);
376 cb->vtag = mei_cl_vtag_by_fp(cl, file);
378 rets = copy_from_user(cb->buf.data, ubuf, length);
380 dev_dbg(dev->dev, "failed to copy data from userland\n");
386 rets = mei_cl_write(cl, cb);
388 mutex_unlock(&dev->device_lock);
393 * mei_ioctl_connect_client - the connect to fw client IOCTL function
395 * @file: private data of the file object
396 * @in_client_uuid: requested UUID for connection
397 * @client: IOCTL connect data, output parameters
399 * Locking: called under "dev->device_lock" lock
401 * Return: 0 on success, <0 on failure.
403 static int mei_ioctl_connect_client(struct file *file,
404 const uuid_le *in_client_uuid,
405 struct mei_client *client)
407 struct mei_device *dev;
408 struct mei_me_client *me_cl;
412 cl = file->private_data;
415 if (cl->state != MEI_FILE_INITIALIZING &&
416 cl->state != MEI_FILE_DISCONNECTED)
419 /* find ME client we're trying to connect to */
420 me_cl = mei_me_cl_by_uuid(dev, in_client_uuid);
422 dev_dbg(dev->dev, "Cannot connect to FW Client UUID = %pUl\n",
428 if (me_cl->props.fixed_address) {
429 bool forbidden = dev->override_fixed_address ?
430 !dev->allow_fixed_address : !dev->hbm_f_fa_supported;
432 dev_dbg(dev->dev, "Connection forbidden to FW Client UUID = %pUl\n",
439 dev_dbg(dev->dev, "Connect to FW Client ID = %d\n",
441 dev_dbg(dev->dev, "FW Client - Protocol Version = %d\n",
442 me_cl->props.protocol_version);
443 dev_dbg(dev->dev, "FW Client - Max Msg Len = %d\n",
444 me_cl->props.max_msg_length);
446 /* prepare the output buffer */
447 client->max_msg_length = me_cl->props.max_msg_length;
448 client->protocol_version = me_cl->props.protocol_version;
449 dev_dbg(dev->dev, "Can connect?\n");
451 rets = mei_cl_connect(cl, me_cl, file);
454 mei_me_cl_put(me_cl);
459 * mei_vt_support_check - check if client support vtags
461 * Locking: called under "dev->device_lock" lock
468 * -ENOTTY - no such client
469 * -EOPNOTSUPP - vtags are not supported by client
471 static int mei_vt_support_check(struct mei_device *dev, const uuid_le *uuid)
473 struct mei_me_client *me_cl;
476 if (!dev->hbm_f_vt_supported)
479 me_cl = mei_me_cl_by_uuid(dev, uuid);
481 dev_dbg(dev->dev, "Cannot connect to FW Client UUID = %pUl\n",
485 ret = me_cl->props.vt_supported ? 0 : -EOPNOTSUPP;
486 mei_me_cl_put(me_cl);
492 * mei_ioctl_connect_vtag - connect to fw client with vtag IOCTL function
494 * @file: private data of the file object
495 * @in_client_uuid: requested UUID for connection
496 * @client: IOCTL connect data, output parameters
499 * Locking: called under "dev->device_lock" lock
501 * Return: 0 on success, <0 on failure.
503 static int mei_ioctl_connect_vtag(struct file *file,
504 const uuid_le *in_client_uuid,
505 struct mei_client *client,
508 struct mei_device *dev;
511 struct mei_cl_vtag *cl_vtag;
513 cl = file->private_data;
516 dev_dbg(dev->dev, "FW Client %pUl vtag %d\n", in_client_uuid, vtag);
519 case MEI_FILE_DISCONNECTED:
520 if (mei_cl_vtag_by_fp(cl, file) != vtag) {
521 dev_err(dev->dev, "reconnect with different vtag\n");
525 case MEI_FILE_INITIALIZING:
526 /* malicious connect from another thread may push vtag */
527 if (!IS_ERR(mei_cl_fp_by_vtag(cl, vtag))) {
528 dev_err(dev->dev, "vtag already filled\n");
532 list_for_each_entry(pos, &dev->file_list, link) {
538 /* only search for same UUID */
539 if (uuid_le_cmp(*mei_cl_uuid(pos), *in_client_uuid))
542 /* if tag already exist try another fp */
543 if (!IS_ERR(mei_cl_fp_by_vtag(pos, vtag)))
546 /* replace cl with acquired one */
547 dev_dbg(dev->dev, "replacing with existing cl\n");
550 file->private_data = pos;
555 cl_vtag = mei_cl_vtag_alloc(file, vtag);
559 list_add_tail(&cl_vtag->list, &cl->vtag_map);
565 while (cl->state != MEI_FILE_INITIALIZING &&
566 cl->state != MEI_FILE_DISCONNECTED &&
567 cl->state != MEI_FILE_CONNECTED) {
568 mutex_unlock(&dev->device_lock);
569 wait_event_timeout(cl->wait,
570 (cl->state == MEI_FILE_CONNECTED ||
571 cl->state == MEI_FILE_DISCONNECTED ||
572 cl->state == MEI_FILE_DISCONNECT_REQUIRED ||
573 cl->state == MEI_FILE_DISCONNECT_REPLY),
574 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
575 mutex_lock(&dev->device_lock);
578 if (!mei_cl_is_connected(cl))
579 return mei_ioctl_connect_client(file, in_client_uuid, client);
581 client->max_msg_length = cl->me_cl->props.max_msg_length;
582 client->protocol_version = cl->me_cl->props.protocol_version;
588 * mei_ioctl_client_notify_request -
589 * propagate event notification request to client
591 * @file: pointer to file structure
592 * @request: 0 - disable, 1 - enable
594 * Return: 0 on success , <0 on error
596 static int mei_ioctl_client_notify_request(const struct file *file, u32 request)
598 struct mei_cl *cl = file->private_data;
600 if (request != MEI_HBM_NOTIFICATION_START &&
601 request != MEI_HBM_NOTIFICATION_STOP)
604 return mei_cl_notify_request(cl, file, (u8)request);
608 * mei_ioctl_client_notify_get - wait for notification request
610 * @file: pointer to file structure
611 * @notify_get: 0 - disable, 1 - enable
613 * Return: 0 on success , <0 on error
615 static int mei_ioctl_client_notify_get(const struct file *file, u32 *notify_get)
617 struct mei_cl *cl = file->private_data;
619 bool block = (file->f_flags & O_NONBLOCK) == 0;
622 rets = mei_cl_notify_get(cl, block, ¬ify_ev);
626 *notify_get = notify_ev ? 1 : 0;
631 * mei_ioctl - the IOCTL function
633 * @file: pointer to file structure
634 * @cmd: ioctl command
635 * @data: pointer to mei message structure
637 * Return: 0 on success , <0 on error
639 static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
641 struct mei_device *dev;
642 struct mei_cl *cl = file->private_data;
643 struct mei_connect_client_data conn;
644 struct mei_connect_client_data_vtag conn_vtag;
645 const uuid_le *cl_uuid;
646 struct mei_client *props;
648 u32 notify_get, notify_req;
652 if (WARN_ON(!cl || !cl->dev))
657 dev_dbg(dev->dev, "IOCTL cmd = 0x%x", cmd);
659 mutex_lock(&dev->device_lock);
660 if (dev->dev_state != MEI_DEV_ENABLED) {
666 case IOCTL_MEI_CONNECT_CLIENT:
667 dev_dbg(dev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
668 if (copy_from_user(&conn, (char __user *)data, sizeof(conn))) {
669 dev_dbg(dev->dev, "failed to copy data from userland\n");
673 cl_uuid = &conn.in_client_uuid;
674 props = &conn.out_client_properties;
677 rets = mei_vt_support_check(dev, cl_uuid);
681 rets = mei_ioctl_connect_vtag(file, cl_uuid, props,
684 rets = mei_ioctl_connect_client(file, cl_uuid, props);
688 /* if all is ok, copying the data back to user. */
689 if (copy_to_user((char __user *)data, &conn, sizeof(conn))) {
690 dev_dbg(dev->dev, "failed to copy data to userland\n");
697 case IOCTL_MEI_CONNECT_CLIENT_VTAG:
698 dev_dbg(dev->dev, "IOCTL_MEI_CONNECT_CLIENT_VTAG\n");
699 if (copy_from_user(&conn_vtag, (char __user *)data,
700 sizeof(conn_vtag))) {
701 dev_dbg(dev->dev, "failed to copy data from userland\n");
706 cl_uuid = &conn_vtag.connect.in_client_uuid;
707 props = &conn_vtag.out_client_properties;
708 vtag = conn_vtag.connect.vtag;
710 rets = mei_vt_support_check(dev, cl_uuid);
711 if (rets == -EOPNOTSUPP)
712 dev_dbg(dev->dev, "FW Client %pUl does not support vtags\n",
718 dev_dbg(dev->dev, "vtag can't be zero\n");
723 rets = mei_ioctl_connect_vtag(file, cl_uuid, props, vtag);
727 /* if all is ok, copying the data back to user. */
728 if (copy_to_user((char __user *)data, &conn_vtag,
729 sizeof(conn_vtag))) {
730 dev_dbg(dev->dev, "failed to copy data to userland\n");
737 case IOCTL_MEI_NOTIFY_SET:
738 dev_dbg(dev->dev, ": IOCTL_MEI_NOTIFY_SET.\n");
739 if (copy_from_user(¬ify_req,
740 (char __user *)data, sizeof(notify_req))) {
741 dev_dbg(dev->dev, "failed to copy data from userland\n");
745 rets = mei_ioctl_client_notify_request(file, notify_req);
748 case IOCTL_MEI_NOTIFY_GET:
749 dev_dbg(dev->dev, ": IOCTL_MEI_NOTIFY_GET.\n");
750 rets = mei_ioctl_client_notify_get(file, ¬ify_get);
754 dev_dbg(dev->dev, "copy connect data to user\n");
755 if (copy_to_user((char __user *)data,
756 ¬ify_get, sizeof(notify_get))) {
757 dev_dbg(dev->dev, "failed to copy data to userland\n");
769 mutex_unlock(&dev->device_lock);
774 * mei_poll - the poll function
776 * @file: pointer to file structure
777 * @wait: pointer to poll_table structure
781 static __poll_t mei_poll(struct file *file, poll_table *wait)
783 __poll_t req_events = poll_requested_events(wait);
784 struct mei_cl *cl = file->private_data;
785 struct mei_device *dev;
789 if (WARN_ON(!cl || !cl->dev))
794 mutex_lock(&dev->device_lock);
796 notify_en = cl->notify_en && (req_events & EPOLLPRI);
798 if (dev->dev_state != MEI_DEV_ENABLED ||
799 !mei_cl_is_connected(cl)) {
805 poll_wait(file, &cl->ev_wait, wait);
810 if (req_events & (EPOLLIN | EPOLLRDNORM)) {
811 poll_wait(file, &cl->rx_wait, wait);
813 if (mei_cl_read_cb(cl, file))
814 mask |= EPOLLIN | EPOLLRDNORM;
816 mei_cl_read_start(cl, mei_cl_mtu(cl), file);
819 if (req_events & (EPOLLOUT | EPOLLWRNORM)) {
820 poll_wait(file, &cl->tx_wait, wait);
821 if (cl->tx_cb_queued < dev->tx_queue_limit)
822 mask |= EPOLLOUT | EPOLLWRNORM;
826 mutex_unlock(&dev->device_lock);
831 * mei_cl_is_write_queued - check if the client has pending writes.
833 * @cl: writing host client
835 * Return: true if client is writing, false otherwise.
837 static bool mei_cl_is_write_queued(struct mei_cl *cl)
839 struct mei_device *dev = cl->dev;
840 struct mei_cl_cb *cb;
842 list_for_each_entry(cb, &dev->write_list, list)
845 list_for_each_entry(cb, &dev->write_waiting_list, list)
852 * mei_fsync - the fsync handler
854 * @fp: pointer to file structure
859 * Return: 0 on success, -ENODEV if client is not connected
861 static int mei_fsync(struct file *fp, loff_t start, loff_t end, int datasync)
863 struct mei_cl *cl = fp->private_data;
864 struct mei_device *dev;
867 if (WARN_ON(!cl || !cl->dev))
872 mutex_lock(&dev->device_lock);
874 if (dev->dev_state != MEI_DEV_ENABLED || !mei_cl_is_connected(cl)) {
879 while (mei_cl_is_write_queued(cl)) {
880 mutex_unlock(&dev->device_lock);
881 rets = wait_event_interruptible(cl->tx_wait,
882 cl->writing_state == MEI_WRITE_COMPLETE ||
883 !mei_cl_is_connected(cl));
884 mutex_lock(&dev->device_lock);
886 if (signal_pending(current))
890 if (!mei_cl_is_connected(cl)) {
897 mutex_unlock(&dev->device_lock);
902 * mei_fasync - asynchronous io support
904 * @fd: file descriptor
905 * @file: pointer to file structure
908 * Return: negative on error,
909 * 0 if it did no changes,
910 * and positive a process was added or deleted
912 static int mei_fasync(int fd, struct file *file, int band)
915 struct mei_cl *cl = file->private_data;
917 if (!mei_cl_is_connected(cl))
920 return fasync_helper(fd, file, band, &cl->ev_async);
924 * trc_show - mei device trc attribute show method
926 * @device: device pointer
927 * @attr: attribute pointer
928 * @buf: char out buffer
930 * Return: number of the bytes printed into buf or error
932 static ssize_t trc_show(struct device *device,
933 struct device_attribute *attr, char *buf)
935 struct mei_device *dev = dev_get_drvdata(device);
939 ret = mei_trc_status(dev, &trc);
942 return sprintf(buf, "%08X\n", trc);
944 static DEVICE_ATTR_RO(trc);
947 * fw_status_show - mei device fw_status attribute show method
949 * @device: device pointer
950 * @attr: attribute pointer
951 * @buf: char out buffer
953 * Return: number of the bytes printed into buf or error
955 static ssize_t fw_status_show(struct device *device,
956 struct device_attribute *attr, char *buf)
958 struct mei_device *dev = dev_get_drvdata(device);
959 struct mei_fw_status fw_status;
963 mutex_lock(&dev->device_lock);
964 err = mei_fw_status(dev, &fw_status);
965 mutex_unlock(&dev->device_lock);
967 dev_err(device, "read fw_status error = %d\n", err);
971 for (i = 0; i < fw_status.count; i++)
972 cnt += scnprintf(buf + cnt, PAGE_SIZE - cnt, "%08X\n",
973 fw_status.status[i]);
976 static DEVICE_ATTR_RO(fw_status);
979 * hbm_ver_show - display HBM protocol version negotiated with FW
981 * @device: device pointer
982 * @attr: attribute pointer
983 * @buf: char out buffer
985 * Return: number of the bytes printed into buf or error
987 static ssize_t hbm_ver_show(struct device *device,
988 struct device_attribute *attr, char *buf)
990 struct mei_device *dev = dev_get_drvdata(device);
991 struct hbm_version ver;
993 mutex_lock(&dev->device_lock);
995 mutex_unlock(&dev->device_lock);
997 return sprintf(buf, "%u.%u\n", ver.major_version, ver.minor_version);
999 static DEVICE_ATTR_RO(hbm_ver);
1002 * hbm_ver_drv_show - display HBM protocol version advertised by driver
1004 * @device: device pointer
1005 * @attr: attribute pointer
1006 * @buf: char out buffer
1008 * Return: number of the bytes printed into buf or error
1010 static ssize_t hbm_ver_drv_show(struct device *device,
1011 struct device_attribute *attr, char *buf)
1013 return sprintf(buf, "%u.%u\n", HBM_MAJOR_VERSION, HBM_MINOR_VERSION);
1015 static DEVICE_ATTR_RO(hbm_ver_drv);
1017 static ssize_t tx_queue_limit_show(struct device *device,
1018 struct device_attribute *attr, char *buf)
1020 struct mei_device *dev = dev_get_drvdata(device);
1023 mutex_lock(&dev->device_lock);
1024 size = dev->tx_queue_limit;
1025 mutex_unlock(&dev->device_lock);
1027 return sysfs_emit(buf, "%u\n", size);
1030 static ssize_t tx_queue_limit_store(struct device *device,
1031 struct device_attribute *attr,
1032 const char *buf, size_t count)
1034 struct mei_device *dev = dev_get_drvdata(device);
1039 err = kstrtouint(buf, 10, &inp);
1042 if (inp > MEI_TX_QUEUE_LIMIT_MAX || inp < MEI_TX_QUEUE_LIMIT_MIN)
1046 mutex_lock(&dev->device_lock);
1047 dev->tx_queue_limit = limit;
1048 mutex_unlock(&dev->device_lock);
1052 static DEVICE_ATTR_RW(tx_queue_limit);
1055 * fw_ver_show - display ME FW version
1057 * @device: device pointer
1058 * @attr: attribute pointer
1059 * @buf: char out buffer
1061 * Return: number of the bytes printed into buf or error
1063 static ssize_t fw_ver_show(struct device *device,
1064 struct device_attribute *attr, char *buf)
1066 struct mei_device *dev = dev_get_drvdata(device);
1067 struct mei_fw_version *ver;
1073 for (i = 0; i < MEI_MAX_FW_VER_BLOCKS; i++)
1074 cnt += scnprintf(buf + cnt, PAGE_SIZE - cnt, "%u:%u.%u.%u.%u\n",
1075 ver[i].platform, ver[i].major, ver[i].minor,
1076 ver[i].hotfix, ver[i].buildno);
1079 static DEVICE_ATTR_RO(fw_ver);
1082 * dev_state_show - display device state
1084 * @device: device pointer
1085 * @attr: attribute pointer
1086 * @buf: char out buffer
1088 * Return: number of the bytes printed into buf or error
1090 static ssize_t dev_state_show(struct device *device,
1091 struct device_attribute *attr, char *buf)
1093 struct mei_device *dev = dev_get_drvdata(device);
1094 enum mei_dev_state dev_state;
1096 mutex_lock(&dev->device_lock);
1097 dev_state = dev->dev_state;
1098 mutex_unlock(&dev->device_lock);
1100 return sprintf(buf, "%s", mei_dev_state_str(dev_state));
1102 static DEVICE_ATTR_RO(dev_state);
1105 * mei_set_devstate: set to new device state and notify sysfs file.
1108 * @state: new device state
1110 void mei_set_devstate(struct mei_device *dev, enum mei_dev_state state)
1112 struct device *clsdev;
1114 if (dev->dev_state == state)
1117 dev->dev_state = state;
1119 clsdev = class_find_device_by_devt(mei_class, dev->cdev.dev);
1121 sysfs_notify(&clsdev->kobj, NULL, "dev_state");
1127 * kind_show - display device kind
1129 * @device: device pointer
1130 * @attr: attribute pointer
1131 * @buf: char out buffer
1133 * Return: number of the bytes printed into buf or error
1135 static ssize_t kind_show(struct device *device,
1136 struct device_attribute *attr, char *buf)
1138 struct mei_device *dev = dev_get_drvdata(device);
1142 ret = sprintf(buf, "%s\n", dev->kind);
1144 ret = sprintf(buf, "%s\n", "mei");
1148 static DEVICE_ATTR_RO(kind);
1150 static struct attribute *mei_attrs[] = {
1151 &dev_attr_fw_status.attr,
1152 &dev_attr_hbm_ver.attr,
1153 &dev_attr_hbm_ver_drv.attr,
1154 &dev_attr_tx_queue_limit.attr,
1155 &dev_attr_fw_ver.attr,
1156 &dev_attr_dev_state.attr,
1158 &dev_attr_kind.attr,
1161 ATTRIBUTE_GROUPS(mei);
1164 * file operations structure will be used for mei char device.
1166 static const struct file_operations mei_fops = {
1167 .owner = THIS_MODULE,
1169 .unlocked_ioctl = mei_ioctl,
1170 .compat_ioctl = compat_ptr_ioctl,
1172 .release = mei_release,
1176 .fasync = mei_fasync,
1181 * mei_minor_get - obtain next free device minor number
1183 * @dev: device pointer
1185 * Return: allocated minor, or -ENOSPC if no free minor left
1187 static int mei_minor_get(struct mei_device *dev)
1191 mutex_lock(&mei_minor_lock);
1192 ret = idr_alloc(&mei_idr, dev, 0, MEI_MAX_DEVS, GFP_KERNEL);
1195 else if (ret == -ENOSPC)
1196 dev_err(dev->dev, "too many mei devices\n");
1198 mutex_unlock(&mei_minor_lock);
1203 * mei_minor_free - mark device minor number as free
1205 * @dev: device pointer
1207 static void mei_minor_free(struct mei_device *dev)
1209 mutex_lock(&mei_minor_lock);
1210 idr_remove(&mei_idr, dev->minor);
1211 mutex_unlock(&mei_minor_lock);
1214 int mei_register(struct mei_device *dev, struct device *parent)
1216 struct device *clsdev; /* class device */
1219 ret = mei_minor_get(dev);
1223 /* Fill in the data structures */
1224 devno = MKDEV(MAJOR(mei_devt), dev->minor);
1225 cdev_init(&dev->cdev, &mei_fops);
1226 dev->cdev.owner = parent->driver->owner;
1228 /* Add the device */
1229 ret = cdev_add(&dev->cdev, devno, 1);
1231 dev_err(parent, "unable to add device %d:%d\n",
1232 MAJOR(mei_devt), dev->minor);
1236 clsdev = device_create_with_groups(mei_class, parent, devno,
1238 "mei%d", dev->minor);
1240 if (IS_ERR(clsdev)) {
1241 dev_err(parent, "unable to create device %d:%d\n",
1242 MAJOR(mei_devt), dev->minor);
1243 ret = PTR_ERR(clsdev);
1244 goto err_dev_create;
1247 mei_dbgfs_register(dev, dev_name(clsdev));
1252 cdev_del(&dev->cdev);
1254 mei_minor_free(dev);
1257 EXPORT_SYMBOL_GPL(mei_register);
1259 void mei_deregister(struct mei_device *dev)
1263 devno = dev->cdev.dev;
1264 cdev_del(&dev->cdev);
1266 mei_dbgfs_deregister(dev);
1268 device_destroy(mei_class, devno);
1270 mei_minor_free(dev);
1272 EXPORT_SYMBOL_GPL(mei_deregister);
1274 static int __init mei_init(void)
1278 mei_class = class_create(THIS_MODULE, "mei");
1279 if (IS_ERR(mei_class)) {
1280 pr_err("couldn't create class\n");
1281 ret = PTR_ERR(mei_class);
1285 ret = alloc_chrdev_region(&mei_devt, 0, MEI_MAX_DEVS, "mei");
1287 pr_err("unable to allocate char dev region\n");
1291 ret = mei_cl_bus_init();
1293 pr_err("unable to initialize bus\n");
1300 unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
1302 class_destroy(mei_class);
1307 static void __exit mei_exit(void)
1309 unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
1310 class_destroy(mei_class);
1314 module_init(mei_init);
1315 module_exit(mei_exit);
1317 MODULE_AUTHOR("Intel Corporation");
1318 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1319 MODULE_LICENSE("GPL v2");