Tomas Winkler | 9fff042 | 2019-03-12 00:10:41 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 2 | /* |
Tomas Winkler | 1e55b60 | 2019-03-12 00:10:44 +0200 | [diff] [blame] | 3 | * Copyright (c) 2003-2018, Intel Corporation. All rights reserved. |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 4 | * Intel Management Engine Interface (Intel MEI) Linux driver |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 5 | */ |
Tomas Winkler | 9fff042 | 2019-03-12 00:10:41 +0200 | [diff] [blame] | 6 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 7 | #include <linux/module.h> |
| 8 | #include <linux/moduleparam.h> |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/device.h> |
Tomas Winkler | 1f18035 | 2014-09-29 16:31:46 +0300 | [diff] [blame] | 11 | #include <linux/slab.h> |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 12 | #include <linux/fs.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/fcntl.h> |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 16 | #include <linux/poll.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/ioctl.h> |
| 19 | #include <linux/cdev.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 20 | #include <linux/sched/signal.h> |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 21 | #include <linux/uuid.h> |
| 22 | #include <linux/compat.h> |
| 23 | #include <linux/jiffies.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | |
Tomas Winkler | 4f3afe1 | 2012-05-09 16:38:59 +0300 | [diff] [blame] | 26 | #include <linux/mei.h> |
Tomas Winkler | 47a7380 | 2012-12-25 19:06:03 +0200 | [diff] [blame] | 27 | |
| 28 | #include "mei_dev.h" |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 29 | #include "client.h" |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 30 | |
Alexander Usyskin | 43b8a7e | 2019-04-22 09:51:07 +0300 | [diff] [blame] | 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); |
| 36 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 37 | /** |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 38 | * mei_open - the open function |
| 39 | * |
| 40 | * @inode: pointer to inode structure |
| 41 | * @file: pointer to file structure |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 42 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 43 | * Return: 0 on success, <0 on error |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 44 | */ |
| 45 | static int mei_open(struct inode *inode, struct file *file) |
| 46 | { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 47 | struct mei_device *dev; |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 48 | struct mei_cl *cl; |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 49 | |
Tomas Winkler | 6f37aca | 2011-11-13 09:41:15 +0200 | [diff] [blame] | 50 | int err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 51 | |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 52 | dev = container_of(inode->i_cdev, struct mei_device, cdev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 53 | if (!dev) |
Tomas Winkler | e036cc5 | 2013-09-16 23:44:46 +0300 | [diff] [blame] | 54 | return -ENODEV; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 55 | |
| 56 | mutex_lock(&dev->device_lock); |
Tomas Winkler | e036cc5 | 2013-09-16 23:44:46 +0300 | [diff] [blame] | 57 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 58 | if (dev->dev_state != MEI_DEV_ENABLED) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 59 | dev_dbg(dev->dev, "dev_state != MEI_ENABLED dev_state = %s\n", |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 60 | mei_dev_state_str(dev->dev_state)); |
Tomas Winkler | 03b8d34 | 2015-02-10 10:39:44 +0200 | [diff] [blame] | 61 | err = -ENODEV; |
Tomas Winkler | e036cc5 | 2013-09-16 23:44:46 +0300 | [diff] [blame] | 62 | goto err_unlock; |
Tomas Winkler | 1b81294 | 2012-09-11 00:43:20 +0300 | [diff] [blame] | 63 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 64 | |
Alexander Usyskin | 7851e00 | 2016-02-07 23:35:40 +0200 | [diff] [blame] | 65 | cl = mei_cl_alloc_linked(dev); |
Tomas Winkler | 03b8d34 | 2015-02-10 10:39:44 +0200 | [diff] [blame] | 66 | if (IS_ERR(cl)) { |
| 67 | err = PTR_ERR(cl); |
Tomas Winkler | e036cc5 | 2013-09-16 23:44:46 +0300 | [diff] [blame] | 68 | goto err_unlock; |
Tomas Winkler | 03b8d34 | 2015-02-10 10:39:44 +0200 | [diff] [blame] | 69 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 70 | |
Alexander Usyskin | 97d549b | 2016-06-16 17:58:57 +0300 | [diff] [blame] | 71 | cl->fp = file; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 72 | file->private_data = cl; |
Tomas Winkler | e036cc5 | 2013-09-16 23:44:46 +0300 | [diff] [blame] | 73 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 74 | mutex_unlock(&dev->device_lock); |
| 75 | |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 76 | return nonseekable_open(inode, file); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 77 | |
Tomas Winkler | e036cc5 | 2013-09-16 23:44:46 +0300 | [diff] [blame] | 78 | err_unlock: |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 79 | mutex_unlock(&dev->device_lock); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 80 | return err; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * mei_release - the release function |
| 85 | * |
| 86 | * @inode: pointer to inode structure |
| 87 | * @file: pointer to file structure |
| 88 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 89 | * Return: 0 on success, <0 on error |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 90 | */ |
| 91 | static int mei_release(struct inode *inode, struct file *file) |
| 92 | { |
| 93 | struct mei_cl *cl = file->private_data; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 94 | struct mei_device *dev; |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 95 | int rets; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 96 | |
| 97 | if (WARN_ON(!cl || !cl->dev)) |
| 98 | return -ENODEV; |
| 99 | |
| 100 | dev = cl->dev; |
| 101 | |
| 102 | mutex_lock(&dev->device_lock); |
Alexander Usyskin | 394a77d | 2017-03-20 15:04:03 +0200 | [diff] [blame] | 103 | |
Tomas Winkler | 3c66618 | 2015-05-04 09:43:52 +0300 | [diff] [blame] | 104 | rets = mei_cl_disconnect(cl); |
| 105 | |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 106 | mei_cl_flush_queues(cl, file); |
Tomas Winkler | 4692218 | 2014-03-16 14:35:55 +0200 | [diff] [blame] | 107 | cl_dbg(dev, cl, "removing\n"); |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 108 | |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 109 | mei_cl_unlink(cl); |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 110 | |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 111 | file->private_data = NULL; |
| 112 | |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 113 | kfree(cl); |
Alexander Usyskin | 394a77d | 2017-03-20 15:04:03 +0200 | [diff] [blame] | 114 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 115 | mutex_unlock(&dev->device_lock); |
| 116 | return rets; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /** |
| 121 | * mei_read - the read function. |
| 122 | * |
| 123 | * @file: pointer to file structure |
| 124 | * @ubuf: pointer to user buffer |
| 125 | * @length: buffer length |
| 126 | * @offset: data offset in buffer |
| 127 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 128 | * Return: >=0 data length on success , <0 on error |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 129 | */ |
| 130 | static ssize_t mei_read(struct file *file, char __user *ubuf, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 131 | size_t length, loff_t *offset) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 132 | { |
| 133 | struct mei_cl *cl = file->private_data; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 134 | struct mei_device *dev; |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 135 | struct mei_cl_cb *cb = NULL; |
Alexander Usyskin | ff1586a | 2016-07-26 01:06:06 +0300 | [diff] [blame] | 136 | bool nonblock = !!(file->f_flags & O_NONBLOCK); |
Tomas Winkler | 5151e2b | 2018-07-12 17:10:10 +0300 | [diff] [blame] | 137 | ssize_t rets; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 138 | |
| 139 | if (WARN_ON(!cl || !cl->dev)) |
| 140 | return -ENODEV; |
| 141 | |
| 142 | dev = cl->dev; |
| 143 | |
Tomas Winkler | dd5de1f | 2013-09-02 03:11:04 +0300 | [diff] [blame] | 144 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 145 | mutex_lock(&dev->device_lock); |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 146 | if (dev->dev_state != MEI_DEV_ENABLED) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 147 | rets = -ENODEV; |
| 148 | goto out; |
| 149 | } |
| 150 | |
Tomas Winkler | dd5de1f | 2013-09-02 03:11:04 +0300 | [diff] [blame] | 151 | if (length == 0) { |
| 152 | rets = 0; |
| 153 | goto out; |
| 154 | } |
| 155 | |
Alexander Usyskin | 8b2458f | 2016-01-07 14:46:37 +0200 | [diff] [blame] | 156 | if (ubuf == NULL) { |
| 157 | rets = -EMSGSIZE; |
| 158 | goto out; |
| 159 | } |
| 160 | |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 161 | cb = mei_cl_read_cb(cl, file); |
Alexander Usyskin | 8b2458f | 2016-01-07 14:46:37 +0200 | [diff] [blame] | 162 | if (cb) |
| 163 | goto copy_buffer; |
| 164 | |
| 165 | if (*offset > 0) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 166 | *offset = 0; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 167 | |
Alexander Usyskin | ff1586a | 2016-07-26 01:06:06 +0300 | [diff] [blame] | 168 | rets = mei_cl_read_start(cl, length, file); |
| 169 | if (rets && rets != -EBUSY) { |
Tomas Winkler | 5151e2b | 2018-07-12 17:10:10 +0300 | [diff] [blame] | 170 | cl_dbg(dev, cl, "mei start read failure status = %zd\n", rets); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 171 | goto out; |
| 172 | } |
| 173 | |
Alexander Usyskin | ff1586a | 2016-07-26 01:06:06 +0300 | [diff] [blame] | 174 | if (nonblock) { |
| 175 | rets = -EAGAIN; |
| 176 | goto out; |
| 177 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 178 | |
Alexander Usyskin | cb97fbb | 2017-02-08 00:41:45 +0200 | [diff] [blame] | 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; |
| 186 | } |
| 187 | mutex_lock(&dev->device_lock); |
| 188 | |
| 189 | if (!mei_cl_is_connected(cl)) { |
| 190 | rets = -ENODEV; |
Alexander Usyskin | ff1586a | 2016-07-26 01:06:06 +0300 | [diff] [blame] | 191 | goto out; |
| 192 | } |
| 193 | |
Alexander Usyskin | cb97fbb | 2017-02-08 00:41:45 +0200 | [diff] [blame] | 194 | cb = mei_cl_read_cb(cl, file); |
| 195 | if (!cb) { |
Alexander Usyskin | cb97fbb | 2017-02-08 00:41:45 +0200 | [diff] [blame] | 196 | rets = 0; |
| 197 | goto out; |
| 198 | } |
Tomas Winkler | 3d33ff2 | 2015-02-10 10:39:36 +0200 | [diff] [blame] | 199 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 200 | copy_buffer: |
Tomas Winkler | 3d33ff2 | 2015-02-10 10:39:36 +0200 | [diff] [blame] | 201 | /* now copy the data to user space */ |
| 202 | if (cb->status) { |
| 203 | rets = cb->status; |
Tomas Winkler | 5151e2b | 2018-07-12 17:10:10 +0300 | [diff] [blame] | 204 | cl_dbg(dev, cl, "read operation failed %zd\n", rets); |
Tomas Winkler | 3d33ff2 | 2015-02-10 10:39:36 +0200 | [diff] [blame] | 205 | goto free; |
| 206 | } |
| 207 | |
Alexander Usyskin | 35bf769 | 2016-02-17 18:27:34 +0200 | [diff] [blame] | 208 | cl_dbg(dev, cl, "buf.size = %zu buf.idx = %zu offset = %lld\n", |
Alexander Usyskin | 8b2458f | 2016-01-07 14:46:37 +0200 | [diff] [blame] | 209 | cb->buf.size, cb->buf_idx, *offset); |
| 210 | if (*offset >= cb->buf_idx) { |
| 211 | rets = 0; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 212 | goto free; |
| 213 | } |
| 214 | |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 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); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 218 | |
Tomas Winkler | 5db7514 | 2015-02-10 10:39:42 +0200 | [diff] [blame] | 219 | if (copy_to_user(ubuf, cb->buf.data + *offset, length)) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 220 | dev_dbg(dev->dev, "failed to copy data to userland\n"); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 221 | rets = -EFAULT; |
| 222 | goto free; |
| 223 | } |
| 224 | |
| 225 | rets = length; |
| 226 | *offset += length; |
Tomas Winkler | f862b6b | 2016-02-07 23:35:19 +0200 | [diff] [blame] | 227 | /* not all data was read, keep the cb */ |
| 228 | if (*offset < cb->buf_idx) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 229 | goto out; |
| 230 | |
| 231 | free: |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 232 | mei_io_cb_free(cb); |
Alexander Usyskin | 8b2458f | 2016-01-07 14:46:37 +0200 | [diff] [blame] | 233 | *offset = 0; |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame] | 234 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 235 | out: |
Tomas Winkler | 5151e2b | 2018-07-12 17:10:10 +0300 | [diff] [blame] | 236 | cl_dbg(dev, cl, "end mei read rets = %zd\n", rets); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 237 | mutex_unlock(&dev->device_lock); |
| 238 | return rets; |
| 239 | } |
Tomas Winkler | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 240 | /** |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 241 | * mei_write - the write function. |
| 242 | * |
| 243 | * @file: pointer to file structure |
| 244 | * @ubuf: pointer to user buffer |
| 245 | * @length: buffer length |
| 246 | * @offset: data offset in buffer |
| 247 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 248 | * Return: >=0 data length on success , <0 on error |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 249 | */ |
| 250 | static ssize_t mei_write(struct file *file, const char __user *ubuf, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 251 | size_t length, loff_t *offset) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 252 | { |
| 253 | struct mei_cl *cl = file->private_data; |
Alexander Usyskin | 6cbb097 | 2016-02-10 23:57:26 +0200 | [diff] [blame] | 254 | struct mei_cl_cb *cb; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 255 | struct mei_device *dev; |
Tomas Winkler | 5151e2b | 2018-07-12 17:10:10 +0300 | [diff] [blame] | 256 | ssize_t rets; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 257 | |
| 258 | if (WARN_ON(!cl || !cl->dev)) |
| 259 | return -ENODEV; |
| 260 | |
| 261 | dev = cl->dev; |
| 262 | |
| 263 | mutex_lock(&dev->device_lock); |
| 264 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 265 | if (dev->dev_state != MEI_DEV_ENABLED) { |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 266 | rets = -ENODEV; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 267 | goto out; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 268 | } |
| 269 | |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 270 | if (!mei_cl_is_connected(cl)) { |
| 271 | cl_err(dev, cl, "is not connected"); |
| 272 | rets = -ENODEV; |
| 273 | goto out; |
| 274 | } |
| 275 | |
| 276 | if (!mei_me_cl_is_active(cl->me_cl)) { |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 277 | rets = -ENOTTY; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 278 | goto out; |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 279 | } |
Tomas Winkler | dd5de1f | 2013-09-02 03:11:04 +0300 | [diff] [blame] | 280 | |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 281 | if (length > mei_cl_mtu(cl)) { |
| 282 | rets = -EFBIG; |
| 283 | goto out; |
| 284 | } |
| 285 | |
Tomas Winkler | dd5de1f | 2013-09-02 03:11:04 +0300 | [diff] [blame] | 286 | if (length == 0) { |
| 287 | rets = 0; |
| 288 | goto out; |
| 289 | } |
| 290 | |
Alexander Usyskin | af336ca | 2018-02-25 20:07:05 +0200 | [diff] [blame] | 291 | while (cl->tx_cb_queued >= dev->tx_queue_limit) { |
| 292 | if (file->f_flags & O_NONBLOCK) { |
| 293 | rets = -EAGAIN; |
| 294 | goto out; |
| 295 | } |
| 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; |
| 305 | } |
| 306 | if (!mei_cl_is_connected(cl)) { |
| 307 | rets = -ENODEV; |
| 308 | goto out; |
| 309 | } |
| 310 | } |
| 311 | |
Alexander Usyskin | 6cbb097 | 2016-02-10 23:57:26 +0200 | [diff] [blame] | 312 | cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, file); |
| 313 | if (!cb) { |
Tomas Winkler | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 314 | rets = -ENOMEM; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 315 | goto out; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 316 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 317 | |
Alexander Usyskin | 6cbb097 | 2016-02-10 23:57:26 +0200 | [diff] [blame] | 318 | rets = copy_from_user(cb->buf.data, ubuf, length); |
Alexander Usyskin | d8b29ef | 2013-09-02 03:11:02 +0300 | [diff] [blame] | 319 | if (rets) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 320 | dev_dbg(dev->dev, "failed to copy data from userland\n"); |
Alexander Usyskin | d8b29ef | 2013-09-02 03:11:02 +0300 | [diff] [blame] | 321 | rets = -EFAULT; |
Alexander Usyskin | 6cbb097 | 2016-02-10 23:57:26 +0200 | [diff] [blame] | 322 | mei_io_cb_free(cb); |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 323 | goto out; |
Alexander Usyskin | d8b29ef | 2013-09-02 03:11:02 +0300 | [diff] [blame] | 324 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 325 | |
Alexander Usyskin | e0cb6b2 | 2016-11-08 18:26:08 +0200 | [diff] [blame] | 326 | rets = mei_cl_write(cl, cb); |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 327 | out: |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 328 | mutex_unlock(&dev->device_lock); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 329 | return rets; |
| 330 | } |
| 331 | |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 332 | /** |
| 333 | * mei_ioctl_connect_client - the connect to fw client IOCTL function |
| 334 | * |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 335 | * @file: private data of the file object |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 336 | * @data: IOCTL connect data, input and output parameters |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 337 | * |
| 338 | * Locking: called under "dev->device_lock" lock |
| 339 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 340 | * Return: 0 on success, <0 on failure. |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 341 | */ |
| 342 | static int mei_ioctl_connect_client(struct file *file, |
| 343 | struct mei_connect_client_data *data) |
| 344 | { |
| 345 | struct mei_device *dev; |
| 346 | struct mei_client *client; |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 347 | struct mei_me_client *me_cl; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 348 | struct mei_cl *cl; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 349 | int rets; |
| 350 | |
| 351 | cl = file->private_data; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 352 | dev = cl->dev; |
| 353 | |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 354 | if (dev->dev_state != MEI_DEV_ENABLED) |
| 355 | return -ENODEV; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 356 | |
| 357 | if (cl->state != MEI_FILE_INITIALIZING && |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 358 | cl->state != MEI_FILE_DISCONNECTED) |
| 359 | return -EBUSY; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 360 | |
| 361 | /* find ME client we're trying to connect to */ |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 362 | me_cl = mei_me_cl_by_uuid(dev, &data->in_client_uuid); |
Alexander Usyskin | f4e0624 | 2016-02-07 23:35:38 +0200 | [diff] [blame] | 363 | if (!me_cl) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 364 | dev_dbg(dev->dev, "Cannot connect to FW Client UUID = %pUl\n", |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 365 | &data->in_client_uuid); |
Alexander Usyskin | f4e0624 | 2016-02-07 23:35:38 +0200 | [diff] [blame] | 366 | rets = -ENOTTY; |
| 367 | goto end; |
| 368 | } |
| 369 | |
| 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; |
| 378 | } |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 379 | } |
| 380 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 381 | dev_dbg(dev->dev, "Connect to FW Client ID = %d\n", |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 382 | me_cl->client_id); |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 383 | dev_dbg(dev->dev, "FW Client - Protocol Version = %d\n", |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 384 | me_cl->props.protocol_version); |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 385 | dev_dbg(dev->dev, "FW Client - Max Msg Len = %d\n", |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 386 | me_cl->props.max_msg_length); |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 387 | |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 388 | /* prepare the output buffer */ |
| 389 | client = &data->out_client_properties; |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 390 | client->max_msg_length = me_cl->props.max_msg_length; |
| 391 | client->protocol_version = me_cl->props.protocol_version; |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 392 | dev_dbg(dev->dev, "Can connect?\n"); |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 393 | |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 394 | rets = mei_cl_connect(cl, me_cl, file); |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 395 | |
| 396 | end: |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 397 | mei_me_cl_put(me_cl); |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 398 | return rets; |
| 399 | } |
| 400 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 401 | /** |
Tomas Winkler | 3c7c846 | 2015-07-26 09:54:20 +0300 | [diff] [blame] | 402 | * mei_ioctl_client_notify_request - |
| 403 | * propagate event notification request to client |
| 404 | * |
| 405 | * @file: pointer to file structure |
| 406 | * @request: 0 - disable, 1 - enable |
| 407 | * |
| 408 | * Return: 0 on success , <0 on error |
| 409 | */ |
Tomas Winkler | f23e2cc | 2016-02-07 23:35:23 +0200 | [diff] [blame] | 410 | static int mei_ioctl_client_notify_request(const struct file *file, u32 request) |
Tomas Winkler | 3c7c846 | 2015-07-26 09:54:20 +0300 | [diff] [blame] | 411 | { |
| 412 | struct mei_cl *cl = file->private_data; |
| 413 | |
Alexander Usyskin | 7326fff | 2016-01-17 12:25:01 +0200 | [diff] [blame] | 414 | if (request != MEI_HBM_NOTIFICATION_START && |
| 415 | request != MEI_HBM_NOTIFICATION_STOP) |
| 416 | return -EINVAL; |
| 417 | |
| 418 | return mei_cl_notify_request(cl, file, (u8)request); |
Tomas Winkler | 3c7c846 | 2015-07-26 09:54:20 +0300 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /** |
| 422 | * mei_ioctl_client_notify_get - wait for notification request |
| 423 | * |
| 424 | * @file: pointer to file structure |
| 425 | * @notify_get: 0 - disable, 1 - enable |
| 426 | * |
| 427 | * Return: 0 on success , <0 on error |
| 428 | */ |
Tomas Winkler | f23e2cc | 2016-02-07 23:35:23 +0200 | [diff] [blame] | 429 | static int mei_ioctl_client_notify_get(const struct file *file, u32 *notify_get) |
Tomas Winkler | 3c7c846 | 2015-07-26 09:54:20 +0300 | [diff] [blame] | 430 | { |
| 431 | struct mei_cl *cl = file->private_data; |
| 432 | bool notify_ev; |
| 433 | bool block = (file->f_flags & O_NONBLOCK) == 0; |
| 434 | int rets; |
| 435 | |
| 436 | rets = mei_cl_notify_get(cl, block, ¬ify_ev); |
| 437 | if (rets) |
| 438 | return rets; |
| 439 | |
| 440 | *notify_get = notify_ev ? 1 : 0; |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | /** |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 445 | * mei_ioctl - the IOCTL function |
| 446 | * |
| 447 | * @file: pointer to file structure |
| 448 | * @cmd: ioctl command |
| 449 | * @data: pointer to mei message structure |
| 450 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 451 | * Return: 0 on success , <0 on error |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 452 | */ |
| 453 | static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data) |
| 454 | { |
| 455 | struct mei_device *dev; |
| 456 | struct mei_cl *cl = file->private_data; |
Tomas Winkler | 154eb18 | 2014-08-21 14:29:23 +0300 | [diff] [blame] | 457 | struct mei_connect_client_data connect_data; |
Tomas Winkler | 3c7c846 | 2015-07-26 09:54:20 +0300 | [diff] [blame] | 458 | u32 notify_get, notify_req; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 459 | int rets; |
| 460 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 461 | |
| 462 | if (WARN_ON(!cl || !cl->dev)) |
| 463 | return -ENODEV; |
| 464 | |
| 465 | dev = cl->dev; |
| 466 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 467 | dev_dbg(dev->dev, "IOCTL cmd = 0x%x", cmd); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 468 | |
| 469 | mutex_lock(&dev->device_lock); |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 470 | if (dev->dev_state != MEI_DEV_ENABLED) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 471 | rets = -ENODEV; |
| 472 | goto out; |
| 473 | } |
| 474 | |
Tomas Winkler | 4f046e7 | 2014-08-21 14:29:22 +0300 | [diff] [blame] | 475 | switch (cmd) { |
| 476 | case IOCTL_MEI_CONNECT_CLIENT: |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 477 | dev_dbg(dev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n"); |
Tomas Winkler | 154eb18 | 2014-08-21 14:29:23 +0300 | [diff] [blame] | 478 | if (copy_from_user(&connect_data, (char __user *)data, |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 479 | sizeof(struct mei_connect_client_data))) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 480 | dev_dbg(dev->dev, "failed to copy data from userland\n"); |
Tomas Winkler | 4f046e7 | 2014-08-21 14:29:22 +0300 | [diff] [blame] | 481 | rets = -EFAULT; |
| 482 | goto out; |
| 483 | } |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 484 | |
Tomas Winkler | 154eb18 | 2014-08-21 14:29:23 +0300 | [diff] [blame] | 485 | rets = mei_ioctl_connect_client(file, &connect_data); |
Tomas Winkler | 4f046e7 | 2014-08-21 14:29:22 +0300 | [diff] [blame] | 486 | if (rets) |
| 487 | goto out; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 488 | |
Tomas Winkler | 4f046e7 | 2014-08-21 14:29:22 +0300 | [diff] [blame] | 489 | /* if all is ok, copying the data back to user. */ |
Tomas Winkler | 154eb18 | 2014-08-21 14:29:23 +0300 | [diff] [blame] | 490 | if (copy_to_user((char __user *)data, &connect_data, |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 491 | sizeof(struct mei_connect_client_data))) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 492 | dev_dbg(dev->dev, "failed to copy data to userland\n"); |
Tomas Winkler | 4f046e7 | 2014-08-21 14:29:22 +0300 | [diff] [blame] | 493 | rets = -EFAULT; |
| 494 | goto out; |
| 495 | } |
| 496 | |
| 497 | break; |
Tomas Winkler | 154eb18 | 2014-08-21 14:29:23 +0300 | [diff] [blame] | 498 | |
Tomas Winkler | 3c7c846 | 2015-07-26 09:54:20 +0300 | [diff] [blame] | 499 | case IOCTL_MEI_NOTIFY_SET: |
| 500 | dev_dbg(dev->dev, ": IOCTL_MEI_NOTIFY_SET.\n"); |
| 501 | if (copy_from_user(¬ify_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; |
| 506 | } |
| 507 | rets = mei_ioctl_client_notify_request(file, notify_req); |
| 508 | break; |
| 509 | |
| 510 | case IOCTL_MEI_NOTIFY_GET: |
| 511 | dev_dbg(dev->dev, ": IOCTL_MEI_NOTIFY_GET.\n"); |
| 512 | rets = mei_ioctl_client_notify_get(file, ¬ify_get); |
| 513 | if (rets) |
| 514 | goto out; |
| 515 | |
| 516 | dev_dbg(dev->dev, "copy connect data to user\n"); |
| 517 | if (copy_to_user((char __user *)data, |
| 518 | ¬ify_get, sizeof(notify_get))) { |
| 519 | dev_dbg(dev->dev, "failed to copy data to userland\n"); |
| 520 | rets = -EFAULT; |
| 521 | goto out; |
| 522 | |
| 523 | } |
| 524 | break; |
| 525 | |
Tomas Winkler | 4f046e7 | 2014-08-21 14:29:22 +0300 | [diff] [blame] | 526 | default: |
Tomas Winkler | 4f046e7 | 2014-08-21 14:29:22 +0300 | [diff] [blame] | 527 | rets = -ENOIOCTLCMD; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | out: |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 531 | mutex_unlock(&dev->device_lock); |
| 532 | return rets; |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * mei_compat_ioctl - the compat IOCTL function |
| 537 | * |
| 538 | * @file: pointer to file structure |
| 539 | * @cmd: ioctl command |
| 540 | * @data: pointer to mei message structure |
| 541 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 542 | * Return: 0 on success , <0 on error |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 543 | */ |
| 544 | #ifdef CONFIG_COMPAT |
| 545 | static long mei_compat_ioctl(struct file *file, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 546 | unsigned int cmd, unsigned long data) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 547 | { |
| 548 | return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data)); |
| 549 | } |
| 550 | #endif |
| 551 | |
| 552 | |
| 553 | /** |
| 554 | * mei_poll - the poll function |
| 555 | * |
| 556 | * @file: pointer to file structure |
| 557 | * @wait: pointer to poll_table structure |
| 558 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 559 | * Return: poll mask |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 560 | */ |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 561 | static __poll_t mei_poll(struct file *file, poll_table *wait) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 562 | { |
Al Viro | 0169943 | 2017-07-03 03:14:15 -0400 | [diff] [blame] | 563 | __poll_t req_events = poll_requested_events(wait); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 564 | struct mei_cl *cl = file->private_data; |
| 565 | struct mei_device *dev; |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 566 | __poll_t mask = 0; |
Tomas Winkler | 2c84c29 | 2015-07-26 09:54:21 +0300 | [diff] [blame] | 567 | bool notify_en; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 568 | |
| 569 | if (WARN_ON(!cl || !cl->dev)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 570 | return EPOLLERR; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 571 | |
| 572 | dev = cl->dev; |
| 573 | |
| 574 | mutex_lock(&dev->device_lock); |
| 575 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 576 | notify_en = cl->notify_en && (req_events & EPOLLPRI); |
Tomas Winkler | f3de9b6 | 2015-03-27 00:27:58 +0200 | [diff] [blame] | 577 | |
| 578 | if (dev->dev_state != MEI_DEV_ENABLED || |
| 579 | !mei_cl_is_connected(cl)) { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 580 | mask = EPOLLERR; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 581 | goto out; |
| 582 | } |
| 583 | |
Tomas Winkler | 2c84c29 | 2015-07-26 09:54:21 +0300 | [diff] [blame] | 584 | if (notify_en) { |
| 585 | poll_wait(file, &cl->ev_wait, wait); |
| 586 | if (cl->notify_ev) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 587 | mask |= EPOLLPRI; |
Tomas Winkler | 2c84c29 | 2015-07-26 09:54:21 +0300 | [diff] [blame] | 588 | } |
| 589 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 590 | if (req_events & (EPOLLIN | EPOLLRDNORM)) { |
Tomas Winkler | 1d9013f | 2015-03-27 00:27:57 +0200 | [diff] [blame] | 591 | poll_wait(file, &cl->rx_wait, wait); |
| 592 | |
| 593 | if (!list_empty(&cl->rd_completed)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 594 | mask |= EPOLLIN | EPOLLRDNORM; |
Tomas Winkler | 1d9013f | 2015-03-27 00:27:57 +0200 | [diff] [blame] | 595 | else |
Tomas Winkler | 3030dc0 | 2016-07-26 01:06:05 +0300 | [diff] [blame] | 596 | mei_cl_read_start(cl, mei_cl_mtu(cl), file); |
Tomas Winkler | 1d9013f | 2015-03-27 00:27:57 +0200 | [diff] [blame] | 597 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 598 | |
Tomas Winkler | 03b2cbb | 2018-09-28 23:27:48 +0300 | [diff] [blame] | 599 | if (req_events & (EPOLLOUT | EPOLLWRNORM)) { |
Alexander Usyskin | af336ca | 2018-02-25 20:07:05 +0200 | [diff] [blame] | 600 | poll_wait(file, &cl->tx_wait, wait); |
| 601 | if (cl->tx_cb_queued < dev->tx_queue_limit) |
Tomas Winkler | 03b2cbb | 2018-09-28 23:27:48 +0300 | [diff] [blame] | 602 | mask |= EPOLLOUT | EPOLLWRNORM; |
Alexander Usyskin | af336ca | 2018-02-25 20:07:05 +0200 | [diff] [blame] | 603 | } |
| 604 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 605 | out: |
| 606 | mutex_unlock(&dev->device_lock); |
| 607 | return mask; |
| 608 | } |
| 609 | |
Tomas Winkler | 55c4e64 | 2014-11-19 17:01:39 +0200 | [diff] [blame] | 610 | /** |
Alexander Usyskin | 58cde1a | 2017-03-20 15:04:06 +0200 | [diff] [blame] | 611 | * mei_cl_is_write_queued - check if the client has pending writes. |
| 612 | * |
| 613 | * @cl: writing host client |
| 614 | * |
| 615 | * Return: true if client is writing, false otherwise. |
| 616 | */ |
| 617 | static bool mei_cl_is_write_queued(struct mei_cl *cl) |
| 618 | { |
| 619 | struct mei_device *dev = cl->dev; |
| 620 | struct mei_cl_cb *cb; |
| 621 | |
| 622 | list_for_each_entry(cb, &dev->write_list, list) |
| 623 | if (cb->cl == cl) |
| 624 | return true; |
| 625 | list_for_each_entry(cb, &dev->write_waiting_list, list) |
| 626 | if (cb->cl == cl) |
| 627 | return true; |
| 628 | return false; |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * mei_fsync - the fsync handler |
| 633 | * |
| 634 | * @fp: pointer to file structure |
| 635 | * @start: unused |
| 636 | * @end: unused |
| 637 | * @datasync: unused |
| 638 | * |
| 639 | * Return: 0 on success, -ENODEV if client is not connected |
| 640 | */ |
| 641 | static int mei_fsync(struct file *fp, loff_t start, loff_t end, int datasync) |
| 642 | { |
| 643 | struct mei_cl *cl = fp->private_data; |
| 644 | struct mei_device *dev; |
| 645 | int rets; |
| 646 | |
| 647 | if (WARN_ON(!cl || !cl->dev)) |
| 648 | return -ENODEV; |
| 649 | |
| 650 | dev = cl->dev; |
| 651 | |
| 652 | mutex_lock(&dev->device_lock); |
| 653 | |
| 654 | if (dev->dev_state != MEI_DEV_ENABLED || !mei_cl_is_connected(cl)) { |
| 655 | rets = -ENODEV; |
| 656 | goto out; |
| 657 | } |
| 658 | |
| 659 | while (mei_cl_is_write_queued(cl)) { |
| 660 | mutex_unlock(&dev->device_lock); |
| 661 | rets = wait_event_interruptible(cl->tx_wait, |
| 662 | cl->writing_state == MEI_WRITE_COMPLETE || |
| 663 | !mei_cl_is_connected(cl)); |
| 664 | mutex_lock(&dev->device_lock); |
| 665 | if (rets) { |
| 666 | if (signal_pending(current)) |
| 667 | rets = -EINTR; |
| 668 | goto out; |
| 669 | } |
| 670 | if (!mei_cl_is_connected(cl)) { |
| 671 | rets = -ENODEV; |
| 672 | goto out; |
| 673 | } |
| 674 | } |
| 675 | rets = 0; |
| 676 | out: |
| 677 | mutex_unlock(&dev->device_lock); |
| 678 | return rets; |
| 679 | } |
| 680 | |
| 681 | /** |
Tomas Winkler | 237092b | 2015-07-26 09:54:22 +0300 | [diff] [blame] | 682 | * mei_fasync - asynchronous io support |
| 683 | * |
| 684 | * @fd: file descriptor |
| 685 | * @file: pointer to file structure |
| 686 | * @band: band bitmap |
| 687 | * |
Tomas Winkler | ed6dc53 | 2016-01-07 14:46:38 +0200 | [diff] [blame] | 688 | * Return: negative on error, |
| 689 | * 0 if it did no changes, |
| 690 | * and positive a process was added or deleted |
Tomas Winkler | 237092b | 2015-07-26 09:54:22 +0300 | [diff] [blame] | 691 | */ |
| 692 | static int mei_fasync(int fd, struct file *file, int band) |
| 693 | { |
| 694 | |
| 695 | struct mei_cl *cl = file->private_data; |
| 696 | |
| 697 | if (!mei_cl_is_connected(cl)) |
Tomas Winkler | ed6dc53 | 2016-01-07 14:46:38 +0200 | [diff] [blame] | 698 | return -ENODEV; |
Tomas Winkler | 237092b | 2015-07-26 09:54:22 +0300 | [diff] [blame] | 699 | |
| 700 | return fasync_helper(fd, file, band, &cl->ev_async); |
| 701 | } |
| 702 | |
| 703 | /** |
Alexander Usyskin | 88d1bec | 2016-10-30 01:42:18 +0200 | [diff] [blame] | 704 | * fw_status_show - mei device fw_status attribute show method |
Tomas Winkler | 55c4e64 | 2014-11-19 17:01:39 +0200 | [diff] [blame] | 705 | * |
| 706 | * @device: device pointer |
| 707 | * @attr: attribute pointer |
| 708 | * @buf: char out buffer |
| 709 | * |
| 710 | * Return: number of the bytes printed into buf or error |
| 711 | */ |
| 712 | static ssize_t fw_status_show(struct device *device, |
| 713 | struct device_attribute *attr, char *buf) |
| 714 | { |
| 715 | struct mei_device *dev = dev_get_drvdata(device); |
| 716 | struct mei_fw_status fw_status; |
| 717 | int err, i; |
| 718 | ssize_t cnt = 0; |
| 719 | |
| 720 | mutex_lock(&dev->device_lock); |
| 721 | err = mei_fw_status(dev, &fw_status); |
| 722 | mutex_unlock(&dev->device_lock); |
| 723 | if (err) { |
| 724 | dev_err(device, "read fw_status error = %d\n", err); |
| 725 | return err; |
| 726 | } |
| 727 | |
| 728 | for (i = 0; i < fw_status.count; i++) |
| 729 | cnt += scnprintf(buf + cnt, PAGE_SIZE - cnt, "%08X\n", |
| 730 | fw_status.status[i]); |
| 731 | return cnt; |
| 732 | } |
| 733 | static DEVICE_ATTR_RO(fw_status); |
| 734 | |
Alexander Usyskin | 88d1bec | 2016-10-30 01:42:18 +0200 | [diff] [blame] | 735 | /** |
| 736 | * hbm_ver_show - display HBM protocol version negotiated with FW |
| 737 | * |
| 738 | * @device: device pointer |
| 739 | * @attr: attribute pointer |
| 740 | * @buf: char out buffer |
| 741 | * |
| 742 | * Return: number of the bytes printed into buf or error |
| 743 | */ |
| 744 | static ssize_t hbm_ver_show(struct device *device, |
| 745 | struct device_attribute *attr, char *buf) |
| 746 | { |
| 747 | struct mei_device *dev = dev_get_drvdata(device); |
| 748 | struct hbm_version ver; |
| 749 | |
| 750 | mutex_lock(&dev->device_lock); |
| 751 | ver = dev->version; |
| 752 | mutex_unlock(&dev->device_lock); |
| 753 | |
| 754 | return sprintf(buf, "%u.%u\n", ver.major_version, ver.minor_version); |
| 755 | } |
| 756 | static DEVICE_ATTR_RO(hbm_ver); |
| 757 | |
| 758 | /** |
| 759 | * hbm_ver_drv_show - display HBM protocol version advertised by driver |
| 760 | * |
| 761 | * @device: device pointer |
| 762 | * @attr: attribute pointer |
| 763 | * @buf: char out buffer |
| 764 | * |
| 765 | * Return: number of the bytes printed into buf or error |
| 766 | */ |
| 767 | static ssize_t hbm_ver_drv_show(struct device *device, |
| 768 | struct device_attribute *attr, char *buf) |
| 769 | { |
| 770 | return sprintf(buf, "%u.%u\n", HBM_MAJOR_VERSION, HBM_MINOR_VERSION); |
| 771 | } |
| 772 | static DEVICE_ATTR_RO(hbm_ver_drv); |
| 773 | |
Alexander Usyskin | af336ca | 2018-02-25 20:07:05 +0200 | [diff] [blame] | 774 | static ssize_t tx_queue_limit_show(struct device *device, |
| 775 | struct device_attribute *attr, char *buf) |
| 776 | { |
| 777 | struct mei_device *dev = dev_get_drvdata(device); |
| 778 | u8 size = 0; |
| 779 | |
| 780 | mutex_lock(&dev->device_lock); |
| 781 | size = dev->tx_queue_limit; |
| 782 | mutex_unlock(&dev->device_lock); |
| 783 | |
| 784 | return snprintf(buf, PAGE_SIZE, "%u\n", size); |
| 785 | } |
| 786 | |
| 787 | static ssize_t tx_queue_limit_store(struct device *device, |
| 788 | struct device_attribute *attr, |
| 789 | const char *buf, size_t count) |
| 790 | { |
| 791 | struct mei_device *dev = dev_get_drvdata(device); |
| 792 | u8 limit; |
| 793 | unsigned int inp; |
| 794 | int err; |
| 795 | |
| 796 | err = kstrtouint(buf, 10, &inp); |
| 797 | if (err) |
| 798 | return err; |
| 799 | if (inp > MEI_TX_QUEUE_LIMIT_MAX || inp < MEI_TX_QUEUE_LIMIT_MIN) |
| 800 | return -EINVAL; |
| 801 | limit = inp; |
| 802 | |
| 803 | mutex_lock(&dev->device_lock); |
| 804 | dev->tx_queue_limit = limit; |
| 805 | mutex_unlock(&dev->device_lock); |
| 806 | |
| 807 | return count; |
| 808 | } |
| 809 | static DEVICE_ATTR_RW(tx_queue_limit); |
| 810 | |
Alexander Usyskin | 3cfaeb3 | 2018-06-25 00:11:41 +0300 | [diff] [blame] | 811 | /** |
| 812 | * fw_ver_show - display ME FW version |
| 813 | * |
| 814 | * @device: device pointer |
| 815 | * @attr: attribute pointer |
| 816 | * @buf: char out buffer |
| 817 | * |
| 818 | * Return: number of the bytes printed into buf or error |
| 819 | */ |
| 820 | static ssize_t fw_ver_show(struct device *device, |
| 821 | struct device_attribute *attr, char *buf) |
| 822 | { |
| 823 | struct mei_device *dev = dev_get_drvdata(device); |
| 824 | struct mei_fw_version *ver; |
| 825 | ssize_t cnt = 0; |
| 826 | int i; |
| 827 | |
| 828 | ver = dev->fw_ver; |
| 829 | |
| 830 | for (i = 0; i < MEI_MAX_FW_VER_BLOCKS; i++) |
| 831 | cnt += scnprintf(buf + cnt, PAGE_SIZE - cnt, "%u:%u.%u.%u.%u\n", |
| 832 | ver[i].platform, ver[i].major, ver[i].minor, |
| 833 | ver[i].hotfix, ver[i].buildno); |
| 834 | return cnt; |
| 835 | } |
| 836 | static DEVICE_ATTR_RO(fw_ver); |
| 837 | |
Alexander Usyskin | 43b8a7e | 2019-04-22 09:51:07 +0300 | [diff] [blame] | 838 | /** |
| 839 | * dev_state_show - display device state |
| 840 | * |
| 841 | * @device: device pointer |
| 842 | * @attr: attribute pointer |
| 843 | * @buf: char out buffer |
| 844 | * |
| 845 | * Return: number of the bytes printed into buf or error |
| 846 | */ |
| 847 | static ssize_t dev_state_show(struct device *device, |
| 848 | struct device_attribute *attr, char *buf) |
| 849 | { |
| 850 | struct mei_device *dev = dev_get_drvdata(device); |
| 851 | enum mei_dev_state dev_state; |
| 852 | |
| 853 | mutex_lock(&dev->device_lock); |
| 854 | dev_state = dev->dev_state; |
| 855 | mutex_unlock(&dev->device_lock); |
| 856 | |
| 857 | return sprintf(buf, "%s", mei_dev_state_str(dev_state)); |
| 858 | } |
| 859 | static DEVICE_ATTR_RO(dev_state); |
| 860 | |
Alexander Usyskin | 43b8a7e | 2019-04-22 09:51:07 +0300 | [diff] [blame] | 861 | /** |
| 862 | * dev_set_devstate: set to new device state and notify sysfs file. |
| 863 | * |
| 864 | * @dev: mei_device |
| 865 | * @state: new device state |
| 866 | */ |
| 867 | void mei_set_devstate(struct mei_device *dev, enum mei_dev_state state) |
| 868 | { |
| 869 | struct device *clsdev; |
| 870 | |
| 871 | if (dev->dev_state == state) |
| 872 | return; |
| 873 | |
| 874 | dev->dev_state = state; |
| 875 | |
Suzuki K Poulose | 4495dfd | 2019-07-23 23:18:35 +0100 | [diff] [blame] | 876 | clsdev = class_find_device_by_devt(mei_class, dev->cdev.dev); |
Alexander Usyskin | 43b8a7e | 2019-04-22 09:51:07 +0300 | [diff] [blame] | 877 | if (clsdev) { |
| 878 | sysfs_notify(&clsdev->kobj, NULL, "dev_state"); |
| 879 | put_device(clsdev); |
| 880 | } |
| 881 | } |
| 882 | |
Tomas Winkler | 55c4e64 | 2014-11-19 17:01:39 +0200 | [diff] [blame] | 883 | static struct attribute *mei_attrs[] = { |
| 884 | &dev_attr_fw_status.attr, |
Alexander Usyskin | 88d1bec | 2016-10-30 01:42:18 +0200 | [diff] [blame] | 885 | &dev_attr_hbm_ver.attr, |
| 886 | &dev_attr_hbm_ver_drv.attr, |
Alexander Usyskin | af336ca | 2018-02-25 20:07:05 +0200 | [diff] [blame] | 887 | &dev_attr_tx_queue_limit.attr, |
Alexander Usyskin | 3cfaeb3 | 2018-06-25 00:11:41 +0300 | [diff] [blame] | 888 | &dev_attr_fw_ver.attr, |
Alexander Usyskin | 43b8a7e | 2019-04-22 09:51:07 +0300 | [diff] [blame] | 889 | &dev_attr_dev_state.attr, |
Tomas Winkler | 55c4e64 | 2014-11-19 17:01:39 +0200 | [diff] [blame] | 890 | NULL |
| 891 | }; |
| 892 | ATTRIBUTE_GROUPS(mei); |
| 893 | |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 894 | /* |
| 895 | * file operations structure will be used for mei char device. |
| 896 | */ |
| 897 | static const struct file_operations mei_fops = { |
| 898 | .owner = THIS_MODULE, |
| 899 | .read = mei_read, |
| 900 | .unlocked_ioctl = mei_ioctl, |
| 901 | #ifdef CONFIG_COMPAT |
| 902 | .compat_ioctl = mei_compat_ioctl, |
| 903 | #endif |
| 904 | .open = mei_open, |
| 905 | .release = mei_release, |
| 906 | .write = mei_write, |
| 907 | .poll = mei_poll, |
Alexander Usyskin | 58cde1a | 2017-03-20 15:04:06 +0200 | [diff] [blame] | 908 | .fsync = mei_fsync, |
Tomas Winkler | 237092b | 2015-07-26 09:54:22 +0300 | [diff] [blame] | 909 | .fasync = mei_fasync, |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 910 | .llseek = no_llseek |
| 911 | }; |
| 912 | |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 913 | /** |
| 914 | * mei_minor_get - obtain next free device minor number |
| 915 | * |
| 916 | * @dev: device pointer |
| 917 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 918 | * Return: allocated minor, or -ENOSPC if no free minor left |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 919 | */ |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 920 | static int mei_minor_get(struct mei_device *dev) |
Tomas Winkler | 9a123f1 | 2012-08-06 15:23:55 +0300 | [diff] [blame] | 921 | { |
Tomas Winkler | 30e53bb | 2013-04-05 22:10:34 +0300 | [diff] [blame] | 922 | int ret; |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 923 | |
| 924 | mutex_lock(&mei_minor_lock); |
| 925 | ret = idr_alloc(&mei_idr, dev, 0, MEI_MAX_DEVS, GFP_KERNEL); |
| 926 | if (ret >= 0) |
| 927 | dev->minor = ret; |
| 928 | else if (ret == -ENOSPC) |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 929 | dev_err(dev->dev, "too many mei devices\n"); |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 930 | |
| 931 | mutex_unlock(&mei_minor_lock); |
| 932 | return ret; |
| 933 | } |
| 934 | |
| 935 | /** |
| 936 | * mei_minor_free - mark device minor number as free |
| 937 | * |
| 938 | * @dev: device pointer |
| 939 | */ |
| 940 | static void mei_minor_free(struct mei_device *dev) |
| 941 | { |
| 942 | mutex_lock(&mei_minor_lock); |
| 943 | idr_remove(&mei_idr, dev->minor); |
| 944 | mutex_unlock(&mei_minor_lock); |
| 945 | } |
| 946 | |
| 947 | int mei_register(struct mei_device *dev, struct device *parent) |
| 948 | { |
| 949 | struct device *clsdev; /* class device */ |
| 950 | int ret, devno; |
| 951 | |
| 952 | ret = mei_minor_get(dev); |
| 953 | if (ret < 0) |
Tomas Winkler | 30e53bb | 2013-04-05 22:10:34 +0300 | [diff] [blame] | 954 | return ret; |
| 955 | |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 956 | /* Fill in the data structures */ |
| 957 | devno = MKDEV(MAJOR(mei_devt), dev->minor); |
| 958 | cdev_init(&dev->cdev, &mei_fops); |
Tomas Winkler | 154322f | 2015-06-18 11:41:03 +0300 | [diff] [blame] | 959 | dev->cdev.owner = parent->driver->owner; |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 960 | |
| 961 | /* Add the device */ |
| 962 | ret = cdev_add(&dev->cdev, devno, 1); |
| 963 | if (ret) { |
| 964 | dev_err(parent, "unable to add device %d:%d\n", |
| 965 | MAJOR(mei_devt), dev->minor); |
| 966 | goto err_dev_add; |
| 967 | } |
| 968 | |
Tomas Winkler | 55c4e64 | 2014-11-19 17:01:39 +0200 | [diff] [blame] | 969 | clsdev = device_create_with_groups(mei_class, parent, devno, |
| 970 | dev, mei_groups, |
| 971 | "mei%d", dev->minor); |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 972 | |
| 973 | if (IS_ERR(clsdev)) { |
| 974 | dev_err(parent, "unable to create device %d:%d\n", |
| 975 | MAJOR(mei_devt), dev->minor); |
| 976 | ret = PTR_ERR(clsdev); |
| 977 | goto err_dev_create; |
| 978 | } |
| 979 | |
Greg Kroah-Hartman | 5666d89 | 2019-06-11 20:38:16 +0200 | [diff] [blame] | 980 | mei_dbgfs_register(dev, dev_name(clsdev)); |
Tomas Winkler | 30e53bb | 2013-04-05 22:10:34 +0300 | [diff] [blame] | 981 | |
| 982 | return 0; |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 983 | |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 984 | err_dev_create: |
| 985 | cdev_del(&dev->cdev); |
| 986 | err_dev_add: |
| 987 | mei_minor_free(dev); |
| 988 | return ret; |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 989 | } |
Tomas Winkler | 40e0b67 | 2013-03-27 16:58:30 +0200 | [diff] [blame] | 990 | EXPORT_SYMBOL_GPL(mei_register); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 991 | |
Tomas Winkler | 30e53bb | 2013-04-05 22:10:34 +0300 | [diff] [blame] | 992 | void mei_deregister(struct mei_device *dev) |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 993 | { |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 994 | int devno; |
| 995 | |
| 996 | devno = dev->cdev.dev; |
| 997 | cdev_del(&dev->cdev); |
| 998 | |
Tomas Winkler | 30e53bb | 2013-04-05 22:10:34 +0300 | [diff] [blame] | 999 | mei_dbgfs_deregister(dev); |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 1000 | |
| 1001 | device_destroy(mei_class, devno); |
| 1002 | |
| 1003 | mei_minor_free(dev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1004 | } |
Tomas Winkler | 40e0b67 | 2013-03-27 16:58:30 +0200 | [diff] [blame] | 1005 | EXPORT_SYMBOL_GPL(mei_deregister); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1006 | |
Samuel Ortiz | cf3baef | 2013-03-27 17:29:57 +0200 | [diff] [blame] | 1007 | static int __init mei_init(void) |
| 1008 | { |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 1009 | int ret; |
| 1010 | |
| 1011 | mei_class = class_create(THIS_MODULE, "mei"); |
| 1012 | if (IS_ERR(mei_class)) { |
| 1013 | pr_err("couldn't create class\n"); |
| 1014 | ret = PTR_ERR(mei_class); |
| 1015 | goto err; |
| 1016 | } |
| 1017 | |
| 1018 | ret = alloc_chrdev_region(&mei_devt, 0, MEI_MAX_DEVS, "mei"); |
| 1019 | if (ret < 0) { |
| 1020 | pr_err("unable to allocate char dev region\n"); |
| 1021 | goto err_class; |
| 1022 | } |
| 1023 | |
| 1024 | ret = mei_cl_bus_init(); |
| 1025 | if (ret < 0) { |
| 1026 | pr_err("unable to initialize bus\n"); |
| 1027 | goto err_chrdev; |
| 1028 | } |
| 1029 | |
| 1030 | return 0; |
| 1031 | |
| 1032 | err_chrdev: |
| 1033 | unregister_chrdev_region(mei_devt, MEI_MAX_DEVS); |
| 1034 | err_class: |
| 1035 | class_destroy(mei_class); |
| 1036 | err: |
| 1037 | return ret; |
Samuel Ortiz | cf3baef | 2013-03-27 17:29:57 +0200 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | static void __exit mei_exit(void) |
| 1041 | { |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 1042 | unregister_chrdev_region(mei_devt, MEI_MAX_DEVS); |
| 1043 | class_destroy(mei_class); |
Samuel Ortiz | cf3baef | 2013-03-27 17:29:57 +0200 | [diff] [blame] | 1044 | mei_cl_bus_exit(); |
| 1045 | } |
| 1046 | |
| 1047 | module_init(mei_init); |
| 1048 | module_exit(mei_exit); |
| 1049 | |
Tomas Winkler | 40e0b67 | 2013-03-27 16:58:30 +0200 | [diff] [blame] | 1050 | MODULE_AUTHOR("Intel Corporation"); |
| 1051 | MODULE_DESCRIPTION("Intel(R) Management Engine Interface"); |
Tomas Winkler | 827eef5 | 2013-02-06 14:06:41 +0200 | [diff] [blame] | 1052 | MODULE_LICENSE("GPL v2"); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1053 | |