1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2003-2022, 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/compat.h>
22 #include <linux/jiffies.h>
23 #include <linux/interrupt.h>
25 #include <linux/mei.h>
30 static struct class *mei_class;
31 static dev_t mei_devt;
32 #define MEI_MAX_DEVS MINORMASK
33 static DEFINE_MUTEX(mei_minor_lock);
34 static DEFINE_IDR(mei_idr);
37 * mei_open - the open function
39 * @inode: pointer to inode structure
40 * @file: pointer to file structure
42 * Return: 0 on success, <0 on error
44 static int mei_open(struct inode *inode, struct file *file)
46 struct mei_device *dev;
51 dev = container_of(inode->i_cdev, struct mei_device, cdev);
53 mutex_lock(&dev->device_lock);
55 if (dev->dev_state != MEI_DEV_ENABLED) {
56 dev_dbg(dev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
57 mei_dev_state_str(dev->dev_state));
62 cl = mei_cl_alloc_linked(dev);
69 file->private_data = cl;
71 mutex_unlock(&dev->device_lock);
73 return nonseekable_open(inode, file);
76 mutex_unlock(&dev->device_lock);
81 * mei_cl_vtag_remove_by_fp - remove vtag that corresponds to fp from list
84 * @fp: pointer to file structure
87 static void mei_cl_vtag_remove_by_fp(const struct mei_cl *cl,
88 const struct file *fp)
90 struct mei_cl_vtag *vtag_l, *next;
92 list_for_each_entry_safe(vtag_l, next, &cl->vtag_map, list) {
93 if (vtag_l->fp == fp) {
94 list_del(&vtag_l->list);
102 * mei_release - the release function
104 * @inode: pointer to inode structure
105 * @file: pointer to file structure
107 * Return: 0 on success, <0 on error
109 static int mei_release(struct inode *inode, struct file *file)
111 struct mei_cl *cl = file->private_data;
112 struct mei_device *dev;
115 if (WARN_ON(!cl || !cl->dev))
120 mutex_lock(&dev->device_lock);
122 mei_cl_vtag_remove_by_fp(cl, file);
124 if (!list_empty(&cl->vtag_map)) {
125 cl_dbg(dev, cl, "not the last vtag\n");
126 mei_cl_flush_queues(cl, file);
131 rets = mei_cl_disconnect(cl);
133 * Check again: This is necessary since disconnect releases the lock
134 * and another client can connect in the meantime.
136 if (!list_empty(&cl->vtag_map)) {
137 cl_dbg(dev, cl, "not the last vtag after disconnect\n");
138 mei_cl_flush_queues(cl, file);
142 mei_cl_flush_queues(cl, NULL);
143 cl_dbg(dev, cl, "removing\n");
149 file->private_data = NULL;
151 mutex_unlock(&dev->device_lock);
157 * mei_read - the read function.
159 * @file: pointer to file structure
160 * @ubuf: pointer to user buffer
161 * @length: buffer length
162 * @offset: data offset in buffer
164 * Return: >=0 data length on success , <0 on error
166 static ssize_t mei_read(struct file *file, char __user *ubuf,
167 size_t length, loff_t *offset)
169 struct mei_cl *cl = file->private_data;
170 struct mei_device *dev;
171 struct mei_cl_cb *cb = NULL;
172 bool nonblock = !!(file->f_flags & O_NONBLOCK);
175 if (WARN_ON(!cl || !cl->dev))
181 mutex_lock(&dev->device_lock);
182 if (dev->dev_state != MEI_DEV_ENABLED) {
197 cb = mei_cl_read_cb(cl, file);
204 rets = mei_cl_read_start(cl, length, file);
205 if (rets && rets != -EBUSY) {
206 cl_dbg(dev, cl, "mei start read failure status = %zd\n", rets);
215 mutex_unlock(&dev->device_lock);
216 if (wait_event_interruptible(cl->rx_wait,
217 mei_cl_read_cb(cl, file) ||
218 !mei_cl_is_connected(cl))) {
219 if (signal_pending(current))
223 mutex_lock(&dev->device_lock);
225 if (!mei_cl_is_connected(cl)) {
230 cb = mei_cl_read_cb(cl, file);
237 /* now copy the data to user space */
240 cl_dbg(dev, cl, "read operation failed %zd\n", rets);
244 cl_dbg(dev, cl, "buf.size = %zu buf.idx = %zu offset = %lld\n",
245 cb->buf.size, cb->buf_idx, *offset);
246 if (*offset >= cb->buf_idx) {
251 /* length is being truncated to PAGE_SIZE,
252 * however buf_idx may point beyond that */
253 length = min_t(size_t, length, cb->buf_idx - *offset);
255 if (copy_to_user(ubuf, cb->buf.data + *offset, length)) {
256 dev_dbg(dev->dev, "failed to copy data to userland\n");
263 /* not all data was read, keep the cb */
264 if (*offset < cb->buf_idx)
268 mei_cl_del_rd_completed(cl, cb);
272 cl_dbg(dev, cl, "end mei read rets = %zd\n", rets);
273 mutex_unlock(&dev->device_lock);
278 * mei_cl_vtag_by_fp - obtain the vtag by file pointer
281 * @fp: pointer to file structure
283 * Return: vtag value on success, otherwise 0
285 static u8 mei_cl_vtag_by_fp(const struct mei_cl *cl, const struct file *fp)
287 struct mei_cl_vtag *cl_vtag;
292 list_for_each_entry(cl_vtag, &cl->vtag_map, list)
293 if (cl_vtag->fp == fp)
294 return cl_vtag->vtag;
299 * mei_write - the write function.
301 * @file: pointer to file structure
302 * @ubuf: pointer to user buffer
303 * @length: buffer length
304 * @offset: data offset in buffer
306 * Return: >=0 data length on success , <0 on error
308 static ssize_t mei_write(struct file *file, const char __user *ubuf,
309 size_t length, loff_t *offset)
311 struct mei_cl *cl = file->private_data;
312 struct mei_cl_cb *cb;
313 struct mei_device *dev;
316 if (WARN_ON(!cl || !cl->dev))
321 mutex_lock(&dev->device_lock);
323 if (dev->dev_state != MEI_DEV_ENABLED) {
328 if (!mei_cl_is_connected(cl)) {
329 cl_err(dev, cl, "is not connected");
334 if (!mei_me_cl_is_active(cl->me_cl)) {
339 if (length > mei_cl_mtu(cl)) {
349 while (cl->tx_cb_queued >= dev->tx_queue_limit) {
350 if (file->f_flags & O_NONBLOCK) {
354 mutex_unlock(&dev->device_lock);
355 rets = wait_event_interruptible(cl->tx_wait,
356 cl->writing_state == MEI_WRITE_COMPLETE ||
357 (!mei_cl_is_connected(cl)));
358 mutex_lock(&dev->device_lock);
360 if (signal_pending(current))
364 if (!mei_cl_is_connected(cl)) {
370 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, file);
375 cb->vtag = mei_cl_vtag_by_fp(cl, file);
377 rets = copy_from_user(cb->buf.data, ubuf, length);
379 dev_dbg(dev->dev, "failed to copy data from userland\n");
385 rets = mei_cl_write(cl, cb, MAX_SCHEDULE_TIMEOUT);
387 mutex_unlock(&dev->device_lock);
392 * mei_ioctl_connect_client - the connect to fw client IOCTL function
394 * @file: private data of the file object
395 * @in_client_uuid: requested UUID for connection
396 * @client: IOCTL connect data, output parameters
398 * Locking: called under "dev->device_lock" lock
400 * Return: 0 on success, <0 on failure.
402 static int mei_ioctl_connect_client(struct file *file,
403 const uuid_le *in_client_uuid,
404 struct mei_client *client)
406 struct mei_device *dev;
407 struct mei_me_client *me_cl;
411 cl = file->private_data;
414 if (cl->state != MEI_FILE_INITIALIZING &&
415 cl->state != MEI_FILE_DISCONNECTED)
418 /* find ME client we're trying to connect to */
419 me_cl = mei_me_cl_by_uuid(dev, in_client_uuid);
421 dev_dbg(dev->dev, "Cannot connect to FW Client UUID = %pUl\n",
427 if (me_cl->props.fixed_address) {
428 bool forbidden = dev->override_fixed_address ?
429 !dev->allow_fixed_address : !dev->hbm_f_fa_supported;
431 dev_dbg(dev->dev, "Connection forbidden to FW Client UUID = %pUl\n",
438 dev_dbg(dev->dev, "Connect to FW Client ID = %d\n",
440 dev_dbg(dev->dev, "FW Client - Protocol Version = %d\n",
441 me_cl->props.protocol_version);
442 dev_dbg(dev->dev, "FW Client - Max Msg Len = %d\n",
443 me_cl->props.max_msg_length);
445 /* prepare the output buffer */
446 client->max_msg_length = me_cl->props.max_msg_length;
447 client->protocol_version = me_cl->props.protocol_version;
448 dev_dbg(dev->dev, "Can connect?\n");
450 rets = mei_cl_connect(cl, me_cl, file);
453 mei_me_cl_put(me_cl);
458 * mei_vt_support_check - check if client support vtags
460 * Locking: called under "dev->device_lock" lock
467 * -ENOTTY - no such client
468 * -EOPNOTSUPP - vtags are not supported by client
470 static int mei_vt_support_check(struct mei_device *dev, const uuid_le *uuid)
472 struct mei_me_client *me_cl;
475 if (!dev->hbm_f_vt_supported)
478 me_cl = mei_me_cl_by_uuid(dev, uuid);
480 dev_dbg(dev->dev, "Cannot connect to FW Client UUID = %pUl\n",
484 ret = me_cl->props.vt_supported ? 0 : -EOPNOTSUPP;
485 mei_me_cl_put(me_cl);
491 * mei_ioctl_connect_vtag - connect to fw client with vtag IOCTL function
493 * @file: private data of the file object
494 * @in_client_uuid: requested UUID for connection
495 * @client: IOCTL connect data, output parameters
498 * Locking: called under "dev->device_lock" lock
500 * Return: 0 on success, <0 on failure.
502 static int mei_ioctl_connect_vtag(struct file *file,
503 const uuid_le *in_client_uuid,
504 struct mei_client *client,
507 struct mei_device *dev;
510 struct mei_cl_vtag *cl_vtag;
512 cl = file->private_data;
515 dev_dbg(dev->dev, "FW Client %pUl vtag %d\n", in_client_uuid, vtag);
518 case MEI_FILE_DISCONNECTED:
519 if (mei_cl_vtag_by_fp(cl, file) != vtag) {
520 dev_err(dev->dev, "reconnect with different vtag\n");
524 case MEI_FILE_INITIALIZING:
525 /* malicious connect from another thread may push vtag */
526 if (!IS_ERR(mei_cl_fp_by_vtag(cl, vtag))) {
527 dev_err(dev->dev, "vtag already filled\n");
531 list_for_each_entry(pos, &dev->file_list, link) {
537 /* only search for same UUID */
538 if (uuid_le_cmp(*mei_cl_uuid(pos), *in_client_uuid))
541 /* if tag already exist try another fp */
542 if (!IS_ERR(mei_cl_fp_by_vtag(pos, vtag)))
545 /* replace cl with acquired one */
546 dev_dbg(dev->dev, "replacing with existing cl\n");
549 file->private_data = pos;
554 cl_vtag = mei_cl_vtag_alloc(file, vtag);
558 list_add_tail(&cl_vtag->list, &cl->vtag_map);
564 while (cl->state != MEI_FILE_INITIALIZING &&
565 cl->state != MEI_FILE_DISCONNECTED &&
566 cl->state != MEI_FILE_CONNECTED) {
567 mutex_unlock(&dev->device_lock);
568 wait_event_timeout(cl->wait,
569 (cl->state == MEI_FILE_CONNECTED ||
570 cl->state == MEI_FILE_DISCONNECTED ||
571 cl->state == MEI_FILE_DISCONNECT_REQUIRED ||
572 cl->state == MEI_FILE_DISCONNECT_REPLY),
573 dev->timeouts.cl_connect);
574 mutex_lock(&dev->device_lock);
577 if (!mei_cl_is_connected(cl))
578 return mei_ioctl_connect_client(file, in_client_uuid, client);
580 client->max_msg_length = cl->me_cl->props.max_msg_length;
581 client->protocol_version = cl->me_cl->props.protocol_version;
587 * mei_ioctl_client_notify_request -
588 * propagate event notification request to client
590 * @file: pointer to file structure
591 * @request: 0 - disable, 1 - enable
593 * Return: 0 on success , <0 on error
595 static int mei_ioctl_client_notify_request(const struct file *file, u32 request)
597 struct mei_cl *cl = file->private_data;
599 if (request != MEI_HBM_NOTIFICATION_START &&
600 request != MEI_HBM_NOTIFICATION_STOP)
603 return mei_cl_notify_request(cl, file, (u8)request);
607 * mei_ioctl_client_notify_get - wait for notification request
609 * @file: pointer to file structure
610 * @notify_get: 0 - disable, 1 - enable
612 * Return: 0 on success , <0 on error
614 static int mei_ioctl_client_notify_get(const struct file *file, u32 *notify_get)
616 struct mei_cl *cl = file->private_data;
618 bool block = (file->f_flags & O_NONBLOCK) == 0;
621 rets = mei_cl_notify_get(cl, block, ¬ify_ev);
625 *notify_get = notify_ev ? 1 : 0;
630 * mei_ioctl - the IOCTL function
632 * @file: pointer to file structure
633 * @cmd: ioctl command
634 * @data: pointer to mei message structure
636 * Return: 0 on success , <0 on error
638 static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
640 struct mei_device *dev;
641 struct mei_cl *cl = file->private_data;
642 struct mei_connect_client_data conn;
643 struct mei_connect_client_data_vtag conn_vtag;
644 const uuid_le *cl_uuid;
645 struct mei_client *props;
647 u32 notify_get, notify_req;
651 if (WARN_ON(!cl || !cl->dev))
656 dev_dbg(dev->dev, "IOCTL cmd = 0x%x", cmd);
658 mutex_lock(&dev->device_lock);
659 if (dev->dev_state != MEI_DEV_ENABLED) {
665 case IOCTL_MEI_CONNECT_CLIENT:
666 dev_dbg(dev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
667 if (copy_from_user(&conn, (char __user *)data, sizeof(conn))) {
668 dev_dbg(dev->dev, "failed to copy data from userland\n");
672 cl_uuid = &conn.in_client_uuid;
673 props = &conn.out_client_properties;
676 rets = mei_vt_support_check(dev, cl_uuid);
680 rets = mei_ioctl_connect_vtag(file, cl_uuid, props,
683 rets = mei_ioctl_connect_client(file, cl_uuid, props);
687 /* if all is ok, copying the data back to user. */
688 if (copy_to_user((char __user *)data, &conn, sizeof(conn))) {
689 dev_dbg(dev->dev, "failed to copy data to userland\n");
696 case IOCTL_MEI_CONNECT_CLIENT_VTAG:
697 dev_dbg(dev->dev, "IOCTL_MEI_CONNECT_CLIENT_VTAG\n");
698 if (copy_from_user(&conn_vtag, (char __user *)data,
699 sizeof(conn_vtag))) {
700 dev_dbg(dev->dev, "failed to copy data from userland\n");
705 cl_uuid = &conn_vtag.connect.in_client_uuid;
706 props = &conn_vtag.out_client_properties;
707 vtag = conn_vtag.connect.vtag;
709 rets = mei_vt_support_check(dev, cl_uuid);
710 if (rets == -EOPNOTSUPP)
711 dev_dbg(dev->dev, "FW Client %pUl does not support vtags\n",
717 dev_dbg(dev->dev, "vtag can't be zero\n");
722 rets = mei_ioctl_connect_vtag(file, cl_uuid, props, vtag);
726 /* if all is ok, copying the data back to user. */
727 if (copy_to_user((char __user *)data, &conn_vtag,
728 sizeof(conn_vtag))) {
729 dev_dbg(dev->dev, "failed to copy data to userland\n");
736 case IOCTL_MEI_NOTIFY_SET:
737 dev_dbg(dev->dev, ": IOCTL_MEI_NOTIFY_SET.\n");
738 if (copy_from_user(¬ify_req,
739 (char __user *)data, sizeof(notify_req))) {
740 dev_dbg(dev->dev, "failed to copy data from userland\n");
744 rets = mei_ioctl_client_notify_request(file, notify_req);
747 case IOCTL_MEI_NOTIFY_GET:
748 dev_dbg(dev->dev, ": IOCTL_MEI_NOTIFY_GET.\n");
749 rets = mei_ioctl_client_notify_get(file, ¬ify_get);
753 dev_dbg(dev->dev, "copy connect data to user\n");
754 if (copy_to_user((char __user *)data,
755 ¬ify_get, sizeof(notify_get))) {
756 dev_dbg(dev->dev, "failed to copy data to userland\n");
768 mutex_unlock(&dev->device_lock);
773 * mei_poll - the poll function
775 * @file: pointer to file structure
776 * @wait: pointer to poll_table structure
780 static __poll_t mei_poll(struct file *file, poll_table *wait)
782 __poll_t req_events = poll_requested_events(wait);
783 struct mei_cl *cl = file->private_data;
784 struct mei_device *dev;
788 if (WARN_ON(!cl || !cl->dev))
793 mutex_lock(&dev->device_lock);
795 notify_en = cl->notify_en && (req_events & EPOLLPRI);
797 if (dev->dev_state != MEI_DEV_ENABLED ||
798 !mei_cl_is_connected(cl)) {
804 poll_wait(file, &cl->ev_wait, wait);
809 if (req_events & (EPOLLIN | EPOLLRDNORM)) {
810 poll_wait(file, &cl->rx_wait, wait);
812 if (mei_cl_read_cb(cl, file))
813 mask |= EPOLLIN | EPOLLRDNORM;
815 mei_cl_read_start(cl, mei_cl_mtu(cl), file);
818 if (req_events & (EPOLLOUT | EPOLLWRNORM)) {
819 poll_wait(file, &cl->tx_wait, wait);
820 if (cl->tx_cb_queued < dev->tx_queue_limit)
821 mask |= EPOLLOUT | EPOLLWRNORM;
825 mutex_unlock(&dev->device_lock);
830 * mei_cl_is_write_queued - check if the client has pending writes.
832 * @cl: writing host client
834 * Return: true if client is writing, false otherwise.
836 static bool mei_cl_is_write_queued(struct mei_cl *cl)
838 struct mei_device *dev = cl->dev;
839 struct mei_cl_cb *cb;
841 list_for_each_entry(cb, &dev->write_list, list)
844 list_for_each_entry(cb, &dev->write_waiting_list, list)
851 * mei_fsync - the fsync handler
853 * @fp: pointer to file structure
858 * Return: 0 on success, -ENODEV if client is not connected
860 static int mei_fsync(struct file *fp, loff_t start, loff_t end, int datasync)
862 struct mei_cl *cl = fp->private_data;
863 struct mei_device *dev;
866 if (WARN_ON(!cl || !cl->dev))
871 mutex_lock(&dev->device_lock);
873 if (dev->dev_state != MEI_DEV_ENABLED || !mei_cl_is_connected(cl)) {
878 while (mei_cl_is_write_queued(cl)) {
879 mutex_unlock(&dev->device_lock);
880 rets = wait_event_interruptible(cl->tx_wait,
881 cl->writing_state == MEI_WRITE_COMPLETE ||
882 !mei_cl_is_connected(cl));
883 mutex_lock(&dev->device_lock);
885 if (signal_pending(current))
889 if (!mei_cl_is_connected(cl)) {
896 mutex_unlock(&dev->device_lock);
901 * mei_fasync - asynchronous io support
903 * @fd: file descriptor
904 * @file: pointer to file structure
907 * Return: negative on error,
908 * 0 if it did no changes,
909 * and positive a process was added or deleted
911 static int mei_fasync(int fd, struct file *file, int band)
914 struct mei_cl *cl = file->private_data;
916 if (!mei_cl_is_connected(cl))
919 return fasync_helper(fd, file, band, &cl->ev_async);
923 * trc_show - mei device trc attribute show method
925 * @device: device pointer
926 * @attr: attribute pointer
927 * @buf: char out buffer
929 * Return: number of the bytes printed into buf or error
931 static ssize_t trc_show(struct device *device,
932 struct device_attribute *attr, char *buf)
934 struct mei_device *dev = dev_get_drvdata(device);
938 ret = mei_trc_status(dev, &trc);
941 return sprintf(buf, "%08X\n", trc);
943 static DEVICE_ATTR_RO(trc);
946 * fw_status_show - mei device fw_status attribute show method
948 * @device: device pointer
949 * @attr: attribute pointer
950 * @buf: char out buffer
952 * Return: number of the bytes printed into buf or error
954 static ssize_t fw_status_show(struct device *device,
955 struct device_attribute *attr, char *buf)
957 struct mei_device *dev = dev_get_drvdata(device);
958 struct mei_fw_status fw_status;
962 mutex_lock(&dev->device_lock);
963 err = mei_fw_status(dev, &fw_status);
964 mutex_unlock(&dev->device_lock);
966 dev_err(device, "read fw_status error = %d\n", err);
970 for (i = 0; i < fw_status.count; i++)
971 cnt += scnprintf(buf + cnt, PAGE_SIZE - cnt, "%08X\n",
972 fw_status.status[i]);
975 static DEVICE_ATTR_RO(fw_status);
978 * hbm_ver_show - display HBM protocol version negotiated with FW
980 * @device: device pointer
981 * @attr: attribute pointer
982 * @buf: char out buffer
984 * Return: number of the bytes printed into buf or error
986 static ssize_t hbm_ver_show(struct device *device,
987 struct device_attribute *attr, char *buf)
989 struct mei_device *dev = dev_get_drvdata(device);
990 struct hbm_version ver;
992 mutex_lock(&dev->device_lock);
994 mutex_unlock(&dev->device_lock);
996 return sprintf(buf, "%u.%u\n", ver.major_version, ver.minor_version);
998 static DEVICE_ATTR_RO(hbm_ver);
1001 * hbm_ver_drv_show - display HBM protocol version advertised by driver
1003 * @device: device pointer
1004 * @attr: attribute pointer
1005 * @buf: char out buffer
1007 * Return: number of the bytes printed into buf or error
1009 static ssize_t hbm_ver_drv_show(struct device *device,
1010 struct device_attribute *attr, char *buf)
1012 return sprintf(buf, "%u.%u\n", HBM_MAJOR_VERSION, HBM_MINOR_VERSION);
1014 static DEVICE_ATTR_RO(hbm_ver_drv);
1016 static ssize_t tx_queue_limit_show(struct device *device,
1017 struct device_attribute *attr, char *buf)
1019 struct mei_device *dev = dev_get_drvdata(device);
1022 mutex_lock(&dev->device_lock);
1023 size = dev->tx_queue_limit;
1024 mutex_unlock(&dev->device_lock);
1026 return sysfs_emit(buf, "%u\n", size);
1029 static ssize_t tx_queue_limit_store(struct device *device,
1030 struct device_attribute *attr,
1031 const char *buf, size_t count)
1033 struct mei_device *dev = dev_get_drvdata(device);
1038 err = kstrtouint(buf, 10, &inp);
1041 if (inp > MEI_TX_QUEUE_LIMIT_MAX || inp < MEI_TX_QUEUE_LIMIT_MIN)
1045 mutex_lock(&dev->device_lock);
1046 dev->tx_queue_limit = limit;
1047 mutex_unlock(&dev->device_lock);
1051 static DEVICE_ATTR_RW(tx_queue_limit);
1054 * fw_ver_show - display ME FW version
1056 * @device: device pointer
1057 * @attr: attribute pointer
1058 * @buf: char out buffer
1060 * Return: number of the bytes printed into buf or error
1062 static ssize_t fw_ver_show(struct device *device,
1063 struct device_attribute *attr, char *buf)
1065 struct mei_device *dev = dev_get_drvdata(device);
1066 struct mei_fw_version *ver;
1072 for (i = 0; i < MEI_MAX_FW_VER_BLOCKS; i++)
1073 cnt += scnprintf(buf + cnt, PAGE_SIZE - cnt, "%u:%u.%u.%u.%u\n",
1074 ver[i].platform, ver[i].major, ver[i].minor,
1075 ver[i].hotfix, ver[i].buildno);
1078 static DEVICE_ATTR_RO(fw_ver);
1081 * dev_state_show - display device state
1083 * @device: device pointer
1084 * @attr: attribute pointer
1085 * @buf: char out buffer
1087 * Return: number of the bytes printed into buf or error
1089 static ssize_t dev_state_show(struct device *device,
1090 struct device_attribute *attr, char *buf)
1092 struct mei_device *dev = dev_get_drvdata(device);
1093 enum mei_dev_state dev_state;
1095 mutex_lock(&dev->device_lock);
1096 dev_state = dev->dev_state;
1097 mutex_unlock(&dev->device_lock);
1099 return sprintf(buf, "%s", mei_dev_state_str(dev_state));
1101 static DEVICE_ATTR_RO(dev_state);
1104 * mei_set_devstate: set to new device state and notify sysfs file.
1107 * @state: new device state
1109 void mei_set_devstate(struct mei_device *dev, enum mei_dev_state state)
1111 struct device *clsdev;
1113 if (dev->dev_state == state)
1116 dev->dev_state = state;
1118 clsdev = class_find_device_by_devt(mei_class, dev->cdev.dev);
1120 sysfs_notify(&clsdev->kobj, NULL, "dev_state");
1126 * kind_show - display device kind
1128 * @device: device pointer
1129 * @attr: attribute pointer
1130 * @buf: char out buffer
1132 * Return: number of the bytes printed into buf or error
1134 static ssize_t kind_show(struct device *device,
1135 struct device_attribute *attr, char *buf)
1137 struct mei_device *dev = dev_get_drvdata(device);
1141 ret = sprintf(buf, "%s\n", dev->kind);
1143 ret = sprintf(buf, "%s\n", "mei");
1147 static DEVICE_ATTR_RO(kind);
1149 static struct attribute *mei_attrs[] = {
1150 &dev_attr_fw_status.attr,
1151 &dev_attr_hbm_ver.attr,
1152 &dev_attr_hbm_ver_drv.attr,
1153 &dev_attr_tx_queue_limit.attr,
1154 &dev_attr_fw_ver.attr,
1155 &dev_attr_dev_state.attr,
1157 &dev_attr_kind.attr,
1160 ATTRIBUTE_GROUPS(mei);
1163 * file operations structure will be used for mei char device.
1165 static const struct file_operations mei_fops = {
1166 .owner = THIS_MODULE,
1168 .unlocked_ioctl = mei_ioctl,
1169 .compat_ioctl = compat_ptr_ioctl,
1171 .release = mei_release,
1175 .fasync = mei_fasync,
1180 * mei_minor_get - obtain next free device minor number
1182 * @dev: device pointer
1184 * Return: allocated minor, or -ENOSPC if no free minor left
1186 static int mei_minor_get(struct mei_device *dev)
1190 mutex_lock(&mei_minor_lock);
1191 ret = idr_alloc(&mei_idr, dev, 0, MEI_MAX_DEVS, GFP_KERNEL);
1194 else if (ret == -ENOSPC)
1195 dev_err(dev->dev, "too many mei devices\n");
1197 mutex_unlock(&mei_minor_lock);
1202 * mei_minor_free - mark device minor number as free
1204 * @dev: device pointer
1206 static void mei_minor_free(struct mei_device *dev)
1208 mutex_lock(&mei_minor_lock);
1209 idr_remove(&mei_idr, dev->minor);
1210 mutex_unlock(&mei_minor_lock);
1213 int mei_register(struct mei_device *dev, struct device *parent)
1215 struct device *clsdev; /* class device */
1218 ret = mei_minor_get(dev);
1222 /* Fill in the data structures */
1223 devno = MKDEV(MAJOR(mei_devt), dev->minor);
1224 cdev_init(&dev->cdev, &mei_fops);
1225 dev->cdev.owner = parent->driver->owner;
1227 /* Add the device */
1228 ret = cdev_add(&dev->cdev, devno, 1);
1230 dev_err(parent, "unable to add device %d:%d\n",
1231 MAJOR(mei_devt), dev->minor);
1235 clsdev = device_create_with_groups(mei_class, parent, devno,
1237 "mei%d", dev->minor);
1239 if (IS_ERR(clsdev)) {
1240 dev_err(parent, "unable to create device %d:%d\n",
1241 MAJOR(mei_devt), dev->minor);
1242 ret = PTR_ERR(clsdev);
1243 goto err_dev_create;
1246 mei_dbgfs_register(dev, dev_name(clsdev));
1251 cdev_del(&dev->cdev);
1253 mei_minor_free(dev);
1256 EXPORT_SYMBOL_GPL(mei_register);
1258 void mei_deregister(struct mei_device *dev)
1262 devno = dev->cdev.dev;
1263 cdev_del(&dev->cdev);
1265 mei_dbgfs_deregister(dev);
1267 device_destroy(mei_class, devno);
1269 mei_minor_free(dev);
1271 EXPORT_SYMBOL_GPL(mei_deregister);
1273 static int __init mei_init(void)
1277 mei_class = class_create("mei");
1278 if (IS_ERR(mei_class)) {
1279 pr_err("couldn't create class\n");
1280 ret = PTR_ERR(mei_class);
1284 ret = alloc_chrdev_region(&mei_devt, 0, MEI_MAX_DEVS, "mei");
1286 pr_err("unable to allocate char dev region\n");
1290 ret = mei_cl_bus_init();
1292 pr_err("unable to initialize bus\n");
1299 unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
1301 class_destroy(mei_class);
1306 static void __exit mei_exit(void)
1308 unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
1309 class_destroy(mei_class);
1313 module_init(mei_init);
1314 module_exit(mei_exit);
1316 MODULE_AUTHOR("Intel Corporation");
1317 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1318 MODULE_LICENSE("GPL v2");