Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Intel Management Engine Interface (Intel MEI) Linux driver |
Tomas Winkler | 733ba91 | 2012-02-09 19:25:53 +0200 | [diff] [blame] | 4 | * Copyright (c) 2003-2012, Intel Corporation. |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | */ |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/moduleparam.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/device.h> |
Tomas Winkler | 1f18035 | 2014-09-29 16:31:46 +0300 | [diff] [blame] | 20 | #include <linux/slab.h> |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 21 | #include <linux/fs.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/types.h> |
| 24 | #include <linux/fcntl.h> |
| 25 | #include <linux/aio.h> |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 26 | #include <linux/poll.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/ioctl.h> |
| 29 | #include <linux/cdev.h> |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 30 | #include <linux/sched.h> |
| 31 | #include <linux/uuid.h> |
| 32 | #include <linux/compat.h> |
| 33 | #include <linux/jiffies.h> |
| 34 | #include <linux/interrupt.h> |
| 35 | |
Tomas Winkler | 4f3afe1 | 2012-05-09 16:38:59 +0300 | [diff] [blame] | 36 | #include <linux/mei.h> |
Tomas Winkler | 47a7380 | 2012-12-25 19:06:03 +0200 | [diff] [blame] | 37 | |
| 38 | #include "mei_dev.h" |
Tomas Winkler | 90e0b5f1 | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 39 | #include "client.h" |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 40 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 41 | /** |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 42 | * mei_open - the open function |
| 43 | * |
| 44 | * @inode: pointer to inode structure |
| 45 | * @file: pointer to file structure |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 46 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 47 | * Return: 0 on success, <0 on error |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 48 | */ |
| 49 | static int mei_open(struct inode *inode, struct file *file) |
| 50 | { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 51 | struct mei_device *dev; |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 52 | struct mei_cl *cl; |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 53 | |
Tomas Winkler | 6f37aca | 2011-11-13 09:41:15 +0200 | [diff] [blame] | 54 | int err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 55 | |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 56 | dev = container_of(inode->i_cdev, struct mei_device, cdev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 57 | if (!dev) |
Tomas Winkler | e036cc5 | 2013-09-16 23:44:46 +0300 | [diff] [blame] | 58 | return -ENODEV; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 59 | |
| 60 | mutex_lock(&dev->device_lock); |
Tomas Winkler | e036cc5 | 2013-09-16 23:44:46 +0300 | [diff] [blame] | 61 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 62 | if (dev->dev_state != MEI_DEV_ENABLED) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 63 | dev_dbg(dev->dev, "dev_state != MEI_ENABLED dev_state = %s\n", |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 64 | mei_dev_state_str(dev->dev_state)); |
Tomas Winkler | 03b8d34 | 2015-02-10 10:39:44 +0200 | [diff] [blame] | 65 | err = -ENODEV; |
Tomas Winkler | e036cc5 | 2013-09-16 23:44:46 +0300 | [diff] [blame] | 66 | goto err_unlock; |
Tomas Winkler | 1b81294 | 2012-09-11 00:43:20 +0300 | [diff] [blame] | 67 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 68 | |
Tomas Winkler | 03b8d34 | 2015-02-10 10:39:44 +0200 | [diff] [blame] | 69 | cl = mei_cl_alloc_linked(dev, MEI_HOST_CLIENT_ID_ANY); |
| 70 | if (IS_ERR(cl)) { |
| 71 | err = PTR_ERR(cl); |
Tomas Winkler | e036cc5 | 2013-09-16 23:44:46 +0300 | [diff] [blame] | 72 | goto err_unlock; |
Tomas Winkler | 03b8d34 | 2015-02-10 10:39:44 +0200 | [diff] [blame] | 73 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 74 | |
| 75 | file->private_data = cl; |
Tomas Winkler | e036cc5 | 2013-09-16 23:44:46 +0300 | [diff] [blame] | 76 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 77 | mutex_unlock(&dev->device_lock); |
| 78 | |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 79 | return nonseekable_open(inode, file); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 80 | |
Tomas Winkler | e036cc5 | 2013-09-16 23:44:46 +0300 | [diff] [blame] | 81 | err_unlock: |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 82 | mutex_unlock(&dev->device_lock); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 83 | return err; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * mei_release - the release function |
| 88 | * |
| 89 | * @inode: pointer to inode structure |
| 90 | * @file: pointer to file structure |
| 91 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 92 | * Return: 0 on success, <0 on error |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 93 | */ |
| 94 | static int mei_release(struct inode *inode, struct file *file) |
| 95 | { |
| 96 | struct mei_cl *cl = file->private_data; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 97 | struct mei_device *dev; |
| 98 | int rets = 0; |
| 99 | |
| 100 | if (WARN_ON(!cl || !cl->dev)) |
| 101 | return -ENODEV; |
| 102 | |
| 103 | dev = cl->dev; |
| 104 | |
| 105 | mutex_lock(&dev->device_lock); |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 106 | if (cl == &dev->iamthif_cl) { |
| 107 | rets = mei_amthif_release(dev, file); |
| 108 | goto out; |
| 109 | } |
| 110 | if (cl->state == MEI_FILE_CONNECTED) { |
| 111 | cl->state = MEI_FILE_DISCONNECTING; |
Tomas Winkler | 4692218 | 2014-03-16 14:35:55 +0200 | [diff] [blame] | 112 | cl_dbg(dev, cl, "disconnecting\n"); |
Tomas Winkler | 90e0b5f1 | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 113 | rets = mei_cl_disconnect(cl); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 114 | } |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 115 | mei_cl_flush_queues(cl); |
Tomas Winkler | 4692218 | 2014-03-16 14:35:55 +0200 | [diff] [blame] | 116 | cl_dbg(dev, cl, "removing\n"); |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 117 | |
Tomas Winkler | 90e0b5f1 | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 118 | mei_cl_unlink(cl); |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 119 | |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame^] | 120 | mei_io_cb_free(cl->read_cb); |
| 121 | cl->read_cb = NULL; |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 122 | |
| 123 | file->private_data = NULL; |
| 124 | |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame] | 125 | kfree(cl); |
| 126 | out: |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 127 | mutex_unlock(&dev->device_lock); |
| 128 | return rets; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /** |
| 133 | * mei_read - the read function. |
| 134 | * |
| 135 | * @file: pointer to file structure |
| 136 | * @ubuf: pointer to user buffer |
| 137 | * @length: buffer length |
| 138 | * @offset: data offset in buffer |
| 139 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 140 | * Return: >=0 data length on success , <0 on error |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 141 | */ |
| 142 | static ssize_t mei_read(struct file *file, char __user *ubuf, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 143 | size_t length, loff_t *offset) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 144 | { |
| 145 | struct mei_cl *cl = file->private_data; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 146 | struct mei_cl_cb *cb = NULL; |
| 147 | struct mei_device *dev; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 148 | int rets; |
| 149 | int err; |
| 150 | |
| 151 | |
| 152 | if (WARN_ON(!cl || !cl->dev)) |
| 153 | return -ENODEV; |
| 154 | |
| 155 | dev = cl->dev; |
| 156 | |
Tomas Winkler | dd5de1f | 2013-09-02 03:11:04 +0300 | [diff] [blame] | 157 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 158 | mutex_lock(&dev->device_lock); |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 159 | if (dev->dev_state != MEI_DEV_ENABLED) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 160 | rets = -ENODEV; |
| 161 | goto out; |
| 162 | } |
| 163 | |
Tomas Winkler | dd5de1f | 2013-09-02 03:11:04 +0300 | [diff] [blame] | 164 | if (length == 0) { |
| 165 | rets = 0; |
| 166 | goto out; |
| 167 | } |
| 168 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 169 | if (cl == &dev->iamthif_cl) { |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 170 | rets = mei_amthif_read(dev, file, ubuf, length, offset); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 171 | goto out; |
| 172 | } |
| 173 | |
Tomas Winkler | 3d33ff2 | 2015-02-10 10:39:36 +0200 | [diff] [blame] | 174 | cb = cl->read_cb; |
| 175 | if (cb) { |
Tomas Winkler | 139aacf | 2013-05-29 20:09:30 +0300 | [diff] [blame] | 176 | /* read what left */ |
| 177 | if (cb->buf_idx > *offset) |
| 178 | goto copy_buffer; |
| 179 | /* offset is beyond buf_idx we have no more data return 0 */ |
| 180 | if (cb->buf_idx > 0 && cb->buf_idx <= *offset) { |
| 181 | rets = 0; |
| 182 | goto free; |
| 183 | } |
| 184 | /* Offset needs to be cleaned for contiguous reads*/ |
| 185 | if (cb->buf_idx == 0 && *offset > 0) |
| 186 | *offset = 0; |
| 187 | } else if (*offset > 0) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 188 | *offset = 0; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 189 | } |
| 190 | |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame] | 191 | err = mei_cl_read_start(cl, length, file); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 192 | if (err && err != -EBUSY) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 193 | dev_dbg(dev->dev, |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 194 | "mei start read failure with status = %d\n", err); |
| 195 | rets = err; |
| 196 | goto out; |
| 197 | } |
| 198 | |
| 199 | if (MEI_READ_COMPLETE != cl->reading_state && |
Tomas Winkler | 3d33ff2 | 2015-02-10 10:39:36 +0200 | [diff] [blame] | 200 | !waitqueue_active(&cl->rx_wait)) { |
| 201 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 202 | if (file->f_flags & O_NONBLOCK) { |
| 203 | rets = -EAGAIN; |
| 204 | goto out; |
| 205 | } |
| 206 | |
| 207 | mutex_unlock(&dev->device_lock); |
| 208 | |
| 209 | if (wait_event_interruptible(cl->rx_wait, |
Tomas Winkler | e2b3164 | 2013-09-02 13:29:46 +0300 | [diff] [blame] | 210 | MEI_READ_COMPLETE == cl->reading_state || |
| 211 | mei_cl_is_transitioning(cl))) { |
| 212 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 213 | if (signal_pending(current)) |
| 214 | return -EINTR; |
| 215 | return -ERESTARTSYS; |
| 216 | } |
| 217 | |
| 218 | mutex_lock(&dev->device_lock); |
Tomas Winkler | e2b3164 | 2013-09-02 13:29:46 +0300 | [diff] [blame] | 219 | if (mei_cl_is_transitioning(cl)) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 220 | rets = -EBUSY; |
| 221 | goto out; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | cb = cl->read_cb; |
| 226 | |
| 227 | if (!cb) { |
| 228 | rets = -ENODEV; |
| 229 | goto out; |
| 230 | } |
Tomas Winkler | 3d33ff2 | 2015-02-10 10:39:36 +0200 | [diff] [blame] | 231 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 232 | if (cl->reading_state != MEI_READ_COMPLETE) { |
| 233 | rets = 0; |
| 234 | goto out; |
| 235 | } |
Tomas Winkler | 3d33ff2 | 2015-02-10 10:39:36 +0200 | [diff] [blame] | 236 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 237 | copy_buffer: |
Tomas Winkler | 3d33ff2 | 2015-02-10 10:39:36 +0200 | [diff] [blame] | 238 | /* now copy the data to user space */ |
| 239 | if (cb->status) { |
| 240 | rets = cb->status; |
| 241 | dev_dbg(dev->dev, "read operation failed %d\n", rets); |
| 242 | goto free; |
| 243 | } |
| 244 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 245 | dev_dbg(dev->dev, "buf.size = %d buf.idx= %ld\n", |
Tomas Winkler | 5db7514 | 2015-02-10 10:39:42 +0200 | [diff] [blame] | 246 | cb->buf.size, cb->buf_idx); |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 247 | if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 248 | rets = -EMSGSIZE; |
| 249 | goto free; |
| 250 | } |
| 251 | |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 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); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 255 | |
Tomas Winkler | 5db7514 | 2015-02-10 10:39:42 +0200 | [diff] [blame] | 256 | if (copy_to_user(ubuf, cb->buf.data + *offset, length)) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 257 | dev_dbg(dev->dev, "failed to copy data to userland\n"); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 258 | rets = -EFAULT; |
| 259 | goto free; |
| 260 | } |
| 261 | |
| 262 | rets = length; |
| 263 | *offset += length; |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 264 | if ((unsigned long)*offset < cb->buf_idx) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 265 | goto out; |
| 266 | |
| 267 | free: |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 268 | mei_io_cb_free(cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 269 | cl->read_cb = NULL; |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame^] | 270 | |
| 271 | cl->reading_state = MEI_IDLE; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 272 | out: |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 273 | dev_dbg(dev->dev, "end mei read rets= %d\n", rets); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 274 | mutex_unlock(&dev->device_lock); |
| 275 | return rets; |
| 276 | } |
Tomas Winkler | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 277 | /** |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 278 | * mei_write - the write function. |
| 279 | * |
| 280 | * @file: pointer to file structure |
| 281 | * @ubuf: pointer to user buffer |
| 282 | * @length: buffer length |
| 283 | * @offset: data offset in buffer |
| 284 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 285 | * Return: >=0 data length on success , <0 on error |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 286 | */ |
| 287 | static ssize_t mei_write(struct file *file, const char __user *ubuf, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 288 | size_t length, loff_t *offset) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 289 | { |
| 290 | struct mei_cl *cl = file->private_data; |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 291 | struct mei_me_client *me_cl = NULL; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 292 | struct mei_cl_cb *write_cb = NULL; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 293 | struct mei_device *dev; |
| 294 | unsigned long timeout = 0; |
| 295 | int rets; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 296 | |
| 297 | if (WARN_ON(!cl || !cl->dev)) |
| 298 | return -ENODEV; |
| 299 | |
| 300 | dev = cl->dev; |
| 301 | |
| 302 | mutex_lock(&dev->device_lock); |
| 303 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 304 | if (dev->dev_state != MEI_DEV_ENABLED) { |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 305 | rets = -ENODEV; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 306 | goto out; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 307 | } |
| 308 | |
Tomas Winkler | d880f32 | 2014-08-21 14:29:15 +0300 | [diff] [blame] | 309 | me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id); |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 310 | if (!me_cl) { |
Alexander Usyskin | 7ca96aa | 2014-02-19 17:35:49 +0200 | [diff] [blame] | 311 | rets = -ENOTTY; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 312 | goto out; |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 313 | } |
Tomas Winkler | dd5de1f | 2013-09-02 03:11:04 +0300 | [diff] [blame] | 314 | |
| 315 | if (length == 0) { |
| 316 | rets = 0; |
| 317 | goto out; |
| 318 | } |
| 319 | |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 320 | if (length > me_cl->props.max_msg_length) { |
Tomas Winkler | dd5de1f | 2013-09-02 03:11:04 +0300 | [diff] [blame] | 321 | rets = -EFBIG; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 322 | goto out; |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | if (cl->state != MEI_FILE_CONNECTED) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 326 | dev_err(dev->dev, "host client = %d, is not connected to ME client = %d", |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 327 | cl->host_client_id, cl->me_client_id); |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 328 | rets = -ENODEV; |
| 329 | goto out; |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 330 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 331 | if (cl == &dev->iamthif_cl) { |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 332 | write_cb = mei_amthif_find_read_list_entry(dev, file); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 333 | |
| 334 | if (write_cb) { |
| 335 | timeout = write_cb->read_time + |
Tomas Winkler | 3870c32 | 2012-11-01 21:17:14 +0200 | [diff] [blame] | 336 | mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 337 | |
| 338 | if (time_after(jiffies, timeout) || |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 339 | cl->reading_state == MEI_READ_COMPLETE) { |
| 340 | *offset = 0; |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 341 | mei_io_cb_free(write_cb); |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 342 | write_cb = NULL; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /* free entry used in read */ |
| 348 | if (cl->reading_state == MEI_READ_COMPLETE) { |
| 349 | *offset = 0; |
Tomas Winkler | 90e0b5f1 | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 350 | write_cb = mei_cl_find_read_cb(cl); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 351 | if (write_cb) { |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 352 | mei_io_cb_free(write_cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 353 | write_cb = NULL; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 354 | cl->read_cb = NULL; |
Tomas Winkler | 928fa66 | 2015-02-10 10:39:45 +0200 | [diff] [blame^] | 355 | cl->reading_state = MEI_IDLE; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 356 | } |
Tomas Winkler | d91aaed | 2013-01-08 23:07:18 +0200 | [diff] [blame] | 357 | } else if (cl->reading_state == MEI_IDLE) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 358 | *offset = 0; |
| 359 | |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame] | 360 | write_cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, file); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 361 | if (!write_cb) { |
Tomas Winkler | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 362 | rets = -ENOMEM; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 363 | goto out; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 364 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 365 | |
Tomas Winkler | 5db7514 | 2015-02-10 10:39:42 +0200 | [diff] [blame] | 366 | rets = copy_from_user(write_cb->buf.data, ubuf, length); |
Alexander Usyskin | d8b29ef | 2013-09-02 03:11:02 +0300 | [diff] [blame] | 367 | if (rets) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 368 | dev_dbg(dev->dev, "failed to copy data from userland\n"); |
Alexander Usyskin | d8b29ef | 2013-09-02 03:11:02 +0300 | [diff] [blame] | 369 | rets = -EFAULT; |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 370 | goto out; |
Alexander Usyskin | d8b29ef | 2013-09-02 03:11:02 +0300 | [diff] [blame] | 371 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 372 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 373 | if (cl == &dev->iamthif_cl) { |
Tomas Winkler | 8660172 | 2015-02-10 10:39:40 +0200 | [diff] [blame] | 374 | rets = mei_amthif_write(cl, write_cb); |
Tomas Winkler | ab5c4a5 | 2012-11-01 21:17:18 +0200 | [diff] [blame] | 375 | |
| 376 | if (rets) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 377 | dev_err(dev->dev, |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 378 | "amthif write failed with status = %d\n", rets); |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 379 | goto out; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 380 | } |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 381 | mei_me_cl_put(me_cl); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 382 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 383 | return length; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 384 | } |
| 385 | |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 386 | rets = mei_cl_write(cl, write_cb, false); |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 387 | out: |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 388 | mei_me_cl_put(me_cl); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 389 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 390 | if (rets < 0) |
| 391 | mei_io_cb_free(write_cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 392 | return rets; |
| 393 | } |
| 394 | |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 395 | /** |
| 396 | * mei_ioctl_connect_client - the connect to fw client IOCTL function |
| 397 | * |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 398 | * @file: private data of the file object |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 399 | * @data: IOCTL connect data, input and output parameters |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 400 | * |
| 401 | * Locking: called under "dev->device_lock" lock |
| 402 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 403 | * Return: 0 on success, <0 on failure. |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 404 | */ |
| 405 | static int mei_ioctl_connect_client(struct file *file, |
| 406 | struct mei_connect_client_data *data) |
| 407 | { |
| 408 | struct mei_device *dev; |
| 409 | struct mei_client *client; |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 410 | struct mei_me_client *me_cl; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 411 | struct mei_cl *cl; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 412 | int rets; |
| 413 | |
| 414 | cl = file->private_data; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 415 | dev = cl->dev; |
| 416 | |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 417 | if (dev->dev_state != MEI_DEV_ENABLED) |
| 418 | return -ENODEV; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 419 | |
| 420 | if (cl->state != MEI_FILE_INITIALIZING && |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 421 | cl->state != MEI_FILE_DISCONNECTED) |
| 422 | return -EBUSY; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 423 | |
| 424 | /* find ME client we're trying to connect to */ |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 425 | me_cl = mei_me_cl_by_uuid(dev, &data->in_client_uuid); |
| 426 | if (!me_cl || me_cl->props.fixed_address) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 427 | dev_dbg(dev->dev, "Cannot connect to FW Client UUID = %pUl\n", |
Tomas Winkler | 80fe636 | 2013-05-07 21:12:31 +0300 | [diff] [blame] | 428 | &data->in_client_uuid); |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 429 | return -ENOTTY; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 430 | } |
| 431 | |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 432 | cl->me_client_id = me_cl->client_id; |
Tomas Winkler | d880f32 | 2014-08-21 14:29:15 +0300 | [diff] [blame] | 433 | cl->cl_uuid = me_cl->props.protocol_name; |
Tomas Winkler | 80fe636 | 2013-05-07 21:12:31 +0300 | [diff] [blame] | 434 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 435 | dev_dbg(dev->dev, "Connect to FW Client ID = %d\n", |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 436 | cl->me_client_id); |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 437 | dev_dbg(dev->dev, "FW Client - Protocol Version = %d\n", |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 438 | me_cl->props.protocol_version); |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 439 | dev_dbg(dev->dev, "FW Client - Max Msg Len = %d\n", |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 440 | me_cl->props.max_msg_length); |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 441 | |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 442 | /* if we're connecting to amthif client then we will use the |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 443 | * existing connection |
| 444 | */ |
Tomas Winkler | 1a1aca4 | 2013-01-08 23:07:21 +0200 | [diff] [blame] | 445 | if (uuid_le_cmp(data->in_client_uuid, mei_amthif_guid) == 0) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 446 | dev_dbg(dev->dev, "FW Client is amthi\n"); |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 447 | if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) { |
| 448 | rets = -ENODEV; |
| 449 | goto end; |
| 450 | } |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 451 | mei_cl_unlink(cl); |
| 452 | |
| 453 | kfree(cl); |
| 454 | cl = NULL; |
Tomas Winkler | 22f96a0 | 2013-09-16 23:44:47 +0300 | [diff] [blame] | 455 | dev->iamthif_open_count++; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 456 | file->private_data = &dev->iamthif_cl; |
| 457 | |
| 458 | client = &data->out_client_properties; |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 459 | client->max_msg_length = me_cl->props.max_msg_length; |
| 460 | client->protocol_version = me_cl->props.protocol_version; |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 461 | rets = dev->iamthif_cl.status; |
| 462 | |
| 463 | goto end; |
| 464 | } |
| 465 | |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 466 | /* prepare the output buffer */ |
| 467 | client = &data->out_client_properties; |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 468 | client->max_msg_length = me_cl->props.max_msg_length; |
| 469 | client->protocol_version = me_cl->props.protocol_version; |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 470 | dev_dbg(dev->dev, "Can connect?\n"); |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 471 | |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 472 | rets = mei_cl_connect(cl, file); |
| 473 | |
| 474 | end: |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 475 | mei_me_cl_put(me_cl); |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 476 | return rets; |
| 477 | } |
| 478 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 479 | /** |
| 480 | * mei_ioctl - the IOCTL function |
| 481 | * |
| 482 | * @file: pointer to file structure |
| 483 | * @cmd: ioctl command |
| 484 | * @data: pointer to mei message structure |
| 485 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 486 | * Return: 0 on success , <0 on error |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 487 | */ |
| 488 | static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data) |
| 489 | { |
| 490 | struct mei_device *dev; |
| 491 | struct mei_cl *cl = file->private_data; |
Tomas Winkler | 154eb18 | 2014-08-21 14:29:23 +0300 | [diff] [blame] | 492 | struct mei_connect_client_data connect_data; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 493 | int rets; |
| 494 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 495 | |
| 496 | if (WARN_ON(!cl || !cl->dev)) |
| 497 | return -ENODEV; |
| 498 | |
| 499 | dev = cl->dev; |
| 500 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 501 | dev_dbg(dev->dev, "IOCTL cmd = 0x%x", cmd); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 502 | |
| 503 | mutex_lock(&dev->device_lock); |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 504 | if (dev->dev_state != MEI_DEV_ENABLED) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 505 | rets = -ENODEV; |
| 506 | goto out; |
| 507 | } |
| 508 | |
Tomas Winkler | 4f046e7 | 2014-08-21 14:29:22 +0300 | [diff] [blame] | 509 | switch (cmd) { |
| 510 | case IOCTL_MEI_CONNECT_CLIENT: |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 511 | dev_dbg(dev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n"); |
Tomas Winkler | 154eb18 | 2014-08-21 14:29:23 +0300 | [diff] [blame] | 512 | if (copy_from_user(&connect_data, (char __user *)data, |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 513 | sizeof(struct mei_connect_client_data))) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 514 | dev_dbg(dev->dev, "failed to copy data from userland\n"); |
Tomas Winkler | 4f046e7 | 2014-08-21 14:29:22 +0300 | [diff] [blame] | 515 | rets = -EFAULT; |
| 516 | goto out; |
| 517 | } |
Tomas Winkler | 9f81abda | 2013-01-08 23:07:15 +0200 | [diff] [blame] | 518 | |
Tomas Winkler | 154eb18 | 2014-08-21 14:29:23 +0300 | [diff] [blame] | 519 | rets = mei_ioctl_connect_client(file, &connect_data); |
Tomas Winkler | 4f046e7 | 2014-08-21 14:29:22 +0300 | [diff] [blame] | 520 | if (rets) |
| 521 | goto out; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 522 | |
Tomas Winkler | 4f046e7 | 2014-08-21 14:29:22 +0300 | [diff] [blame] | 523 | /* if all is ok, copying the data back to user. */ |
Tomas Winkler | 154eb18 | 2014-08-21 14:29:23 +0300 | [diff] [blame] | 524 | if (copy_to_user((char __user *)data, &connect_data, |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 525 | sizeof(struct mei_connect_client_data))) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 526 | dev_dbg(dev->dev, "failed to copy data to userland\n"); |
Tomas Winkler | 4f046e7 | 2014-08-21 14:29:22 +0300 | [diff] [blame] | 527 | rets = -EFAULT; |
| 528 | goto out; |
| 529 | } |
| 530 | |
| 531 | break; |
Tomas Winkler | 154eb18 | 2014-08-21 14:29:23 +0300 | [diff] [blame] | 532 | |
Tomas Winkler | 4f046e7 | 2014-08-21 14:29:22 +0300 | [diff] [blame] | 533 | default: |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 534 | dev_err(dev->dev, ": unsupported ioctl %d.\n", cmd); |
Tomas Winkler | 4f046e7 | 2014-08-21 14:29:22 +0300 | [diff] [blame] | 535 | rets = -ENOIOCTLCMD; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | out: |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 539 | mutex_unlock(&dev->device_lock); |
| 540 | return rets; |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * mei_compat_ioctl - the compat IOCTL function |
| 545 | * |
| 546 | * @file: pointer to file structure |
| 547 | * @cmd: ioctl command |
| 548 | * @data: pointer to mei message structure |
| 549 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 550 | * Return: 0 on success , <0 on error |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 551 | */ |
| 552 | #ifdef CONFIG_COMPAT |
| 553 | static long mei_compat_ioctl(struct file *file, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 554 | unsigned int cmd, unsigned long data) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 555 | { |
| 556 | return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data)); |
| 557 | } |
| 558 | #endif |
| 559 | |
| 560 | |
| 561 | /** |
| 562 | * mei_poll - the poll function |
| 563 | * |
| 564 | * @file: pointer to file structure |
| 565 | * @wait: pointer to poll_table structure |
| 566 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 567 | * Return: poll mask |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 568 | */ |
| 569 | static unsigned int mei_poll(struct file *file, poll_table *wait) |
| 570 | { |
| 571 | struct mei_cl *cl = file->private_data; |
| 572 | struct mei_device *dev; |
| 573 | unsigned int mask = 0; |
| 574 | |
| 575 | if (WARN_ON(!cl || !cl->dev)) |
Tomas Winkler | b950ac1 | 2013-07-25 20:15:53 +0300 | [diff] [blame] | 576 | return POLLERR; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 577 | |
| 578 | dev = cl->dev; |
| 579 | |
| 580 | mutex_lock(&dev->device_lock); |
| 581 | |
Tomas Winkler | b950ac1 | 2013-07-25 20:15:53 +0300 | [diff] [blame] | 582 | if (!mei_cl_is_connected(cl)) { |
| 583 | mask = POLLERR; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 584 | goto out; |
| 585 | } |
| 586 | |
| 587 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | b950ac1 | 2013-07-25 20:15:53 +0300 | [diff] [blame] | 588 | |
| 589 | |
| 590 | if (cl == &dev->iamthif_cl) |
| 591 | return mei_amthif_poll(dev, file, wait); |
| 592 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 593 | poll_wait(file, &cl->tx_wait, wait); |
Tomas Winkler | b950ac1 | 2013-07-25 20:15:53 +0300 | [diff] [blame] | 594 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 595 | mutex_lock(&dev->device_lock); |
Tomas Winkler | b950ac1 | 2013-07-25 20:15:53 +0300 | [diff] [blame] | 596 | |
| 597 | if (!mei_cl_is_connected(cl)) { |
| 598 | mask = POLLERR; |
| 599 | goto out; |
| 600 | } |
| 601 | |
Alexander Usyskin | 34ec436 | 2014-04-01 23:50:41 +0300 | [diff] [blame] | 602 | mask |= (POLLIN | POLLRDNORM); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 603 | |
| 604 | out: |
| 605 | mutex_unlock(&dev->device_lock); |
| 606 | return mask; |
| 607 | } |
| 608 | |
Tomas Winkler | 55c4e64 | 2014-11-19 17:01:39 +0200 | [diff] [blame] | 609 | /** |
| 610 | * fw_status_show - mei device attribute show method |
| 611 | * |
| 612 | * @device: device pointer |
| 613 | * @attr: attribute pointer |
| 614 | * @buf: char out buffer |
| 615 | * |
| 616 | * Return: number of the bytes printed into buf or error |
| 617 | */ |
| 618 | static ssize_t fw_status_show(struct device *device, |
| 619 | struct device_attribute *attr, char *buf) |
| 620 | { |
| 621 | struct mei_device *dev = dev_get_drvdata(device); |
| 622 | struct mei_fw_status fw_status; |
| 623 | int err, i; |
| 624 | ssize_t cnt = 0; |
| 625 | |
| 626 | mutex_lock(&dev->device_lock); |
| 627 | err = mei_fw_status(dev, &fw_status); |
| 628 | mutex_unlock(&dev->device_lock); |
| 629 | if (err) { |
| 630 | dev_err(device, "read fw_status error = %d\n", err); |
| 631 | return err; |
| 632 | } |
| 633 | |
| 634 | for (i = 0; i < fw_status.count; i++) |
| 635 | cnt += scnprintf(buf + cnt, PAGE_SIZE - cnt, "%08X\n", |
| 636 | fw_status.status[i]); |
| 637 | return cnt; |
| 638 | } |
| 639 | static DEVICE_ATTR_RO(fw_status); |
| 640 | |
| 641 | static struct attribute *mei_attrs[] = { |
| 642 | &dev_attr_fw_status.attr, |
| 643 | NULL |
| 644 | }; |
| 645 | ATTRIBUTE_GROUPS(mei); |
| 646 | |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 647 | /* |
| 648 | * file operations structure will be used for mei char device. |
| 649 | */ |
| 650 | static const struct file_operations mei_fops = { |
| 651 | .owner = THIS_MODULE, |
| 652 | .read = mei_read, |
| 653 | .unlocked_ioctl = mei_ioctl, |
| 654 | #ifdef CONFIG_COMPAT |
| 655 | .compat_ioctl = mei_compat_ioctl, |
| 656 | #endif |
| 657 | .open = mei_open, |
| 658 | .release = mei_release, |
| 659 | .write = mei_write, |
| 660 | .poll = mei_poll, |
| 661 | .llseek = no_llseek |
| 662 | }; |
| 663 | |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 664 | static struct class *mei_class; |
| 665 | static dev_t mei_devt; |
| 666 | #define MEI_MAX_DEVS MINORMASK |
| 667 | static DEFINE_MUTEX(mei_minor_lock); |
| 668 | static DEFINE_IDR(mei_idr); |
| 669 | |
| 670 | /** |
| 671 | * mei_minor_get - obtain next free device minor number |
| 672 | * |
| 673 | * @dev: device pointer |
| 674 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 675 | * Return: allocated minor, or -ENOSPC if no free minor left |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 676 | */ |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 677 | static int mei_minor_get(struct mei_device *dev) |
Tomas Winkler | 9a123f1 | 2012-08-06 15:23:55 +0300 | [diff] [blame] | 678 | { |
Tomas Winkler | 30e53bb | 2013-04-05 22:10:34 +0300 | [diff] [blame] | 679 | int ret; |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 680 | |
| 681 | mutex_lock(&mei_minor_lock); |
| 682 | ret = idr_alloc(&mei_idr, dev, 0, MEI_MAX_DEVS, GFP_KERNEL); |
| 683 | if (ret >= 0) |
| 684 | dev->minor = ret; |
| 685 | else if (ret == -ENOSPC) |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 686 | dev_err(dev->dev, "too many mei devices\n"); |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 687 | |
| 688 | mutex_unlock(&mei_minor_lock); |
| 689 | return ret; |
| 690 | } |
| 691 | |
| 692 | /** |
| 693 | * mei_minor_free - mark device minor number as free |
| 694 | * |
| 695 | * @dev: device pointer |
| 696 | */ |
| 697 | static void mei_minor_free(struct mei_device *dev) |
| 698 | { |
| 699 | mutex_lock(&mei_minor_lock); |
| 700 | idr_remove(&mei_idr, dev->minor); |
| 701 | mutex_unlock(&mei_minor_lock); |
| 702 | } |
| 703 | |
| 704 | int mei_register(struct mei_device *dev, struct device *parent) |
| 705 | { |
| 706 | struct device *clsdev; /* class device */ |
| 707 | int ret, devno; |
| 708 | |
| 709 | ret = mei_minor_get(dev); |
| 710 | if (ret < 0) |
Tomas Winkler | 30e53bb | 2013-04-05 22:10:34 +0300 | [diff] [blame] | 711 | return ret; |
| 712 | |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 713 | /* Fill in the data structures */ |
| 714 | devno = MKDEV(MAJOR(mei_devt), dev->minor); |
| 715 | cdev_init(&dev->cdev, &mei_fops); |
| 716 | dev->cdev.owner = mei_fops.owner; |
| 717 | |
| 718 | /* Add the device */ |
| 719 | ret = cdev_add(&dev->cdev, devno, 1); |
| 720 | if (ret) { |
| 721 | dev_err(parent, "unable to add device %d:%d\n", |
| 722 | MAJOR(mei_devt), dev->minor); |
| 723 | goto err_dev_add; |
| 724 | } |
| 725 | |
Tomas Winkler | 55c4e64 | 2014-11-19 17:01:39 +0200 | [diff] [blame] | 726 | clsdev = device_create_with_groups(mei_class, parent, devno, |
| 727 | dev, mei_groups, |
| 728 | "mei%d", dev->minor); |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 729 | |
| 730 | if (IS_ERR(clsdev)) { |
| 731 | dev_err(parent, "unable to create device %d:%d\n", |
| 732 | MAJOR(mei_devt), dev->minor); |
| 733 | ret = PTR_ERR(clsdev); |
| 734 | goto err_dev_create; |
| 735 | } |
| 736 | |
| 737 | ret = mei_dbgfs_register(dev, dev_name(clsdev)); |
| 738 | if (ret) { |
| 739 | dev_err(clsdev, "cannot register debugfs ret = %d\n", ret); |
| 740 | goto err_dev_dbgfs; |
| 741 | } |
Tomas Winkler | 30e53bb | 2013-04-05 22:10:34 +0300 | [diff] [blame] | 742 | |
| 743 | return 0; |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 744 | |
| 745 | err_dev_dbgfs: |
| 746 | device_destroy(mei_class, devno); |
| 747 | err_dev_create: |
| 748 | cdev_del(&dev->cdev); |
| 749 | err_dev_add: |
| 750 | mei_minor_free(dev); |
| 751 | return ret; |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 752 | } |
Tomas Winkler | 40e0b67 | 2013-03-27 16:58:30 +0200 | [diff] [blame] | 753 | EXPORT_SYMBOL_GPL(mei_register); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 754 | |
Tomas Winkler | 30e53bb | 2013-04-05 22:10:34 +0300 | [diff] [blame] | 755 | void mei_deregister(struct mei_device *dev) |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 756 | { |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 757 | int devno; |
| 758 | |
| 759 | devno = dev->cdev.dev; |
| 760 | cdev_del(&dev->cdev); |
| 761 | |
Tomas Winkler | 30e53bb | 2013-04-05 22:10:34 +0300 | [diff] [blame] | 762 | mei_dbgfs_deregister(dev); |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 763 | |
| 764 | device_destroy(mei_class, devno); |
| 765 | |
| 766 | mei_minor_free(dev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 767 | } |
Tomas Winkler | 40e0b67 | 2013-03-27 16:58:30 +0200 | [diff] [blame] | 768 | EXPORT_SYMBOL_GPL(mei_deregister); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 769 | |
Samuel Ortiz | cf3baef | 2013-03-27 17:29:57 +0200 | [diff] [blame] | 770 | static int __init mei_init(void) |
| 771 | { |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 772 | int ret; |
| 773 | |
| 774 | mei_class = class_create(THIS_MODULE, "mei"); |
| 775 | if (IS_ERR(mei_class)) { |
| 776 | pr_err("couldn't create class\n"); |
| 777 | ret = PTR_ERR(mei_class); |
| 778 | goto err; |
| 779 | } |
| 780 | |
| 781 | ret = alloc_chrdev_region(&mei_devt, 0, MEI_MAX_DEVS, "mei"); |
| 782 | if (ret < 0) { |
| 783 | pr_err("unable to allocate char dev region\n"); |
| 784 | goto err_class; |
| 785 | } |
| 786 | |
| 787 | ret = mei_cl_bus_init(); |
| 788 | if (ret < 0) { |
| 789 | pr_err("unable to initialize bus\n"); |
| 790 | goto err_chrdev; |
| 791 | } |
| 792 | |
| 793 | return 0; |
| 794 | |
| 795 | err_chrdev: |
| 796 | unregister_chrdev_region(mei_devt, MEI_MAX_DEVS); |
| 797 | err_class: |
| 798 | class_destroy(mei_class); |
| 799 | err: |
| 800 | return ret; |
Samuel Ortiz | cf3baef | 2013-03-27 17:29:57 +0200 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | static void __exit mei_exit(void) |
| 804 | { |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 805 | unregister_chrdev_region(mei_devt, MEI_MAX_DEVS); |
| 806 | class_destroy(mei_class); |
Samuel Ortiz | cf3baef | 2013-03-27 17:29:57 +0200 | [diff] [blame] | 807 | mei_cl_bus_exit(); |
| 808 | } |
| 809 | |
| 810 | module_init(mei_init); |
| 811 | module_exit(mei_exit); |
| 812 | |
Tomas Winkler | 40e0b67 | 2013-03-27 16:58:30 +0200 | [diff] [blame] | 813 | MODULE_AUTHOR("Intel Corporation"); |
| 814 | MODULE_DESCRIPTION("Intel(R) Management Engine Interface"); |
Tomas Winkler | 827eef5 | 2013-02-06 14:06:41 +0200 | [diff] [blame] | 815 | MODULE_LICENSE("GPL v2"); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 816 | |