Commit | Line | Data |
---|---|---|
ab841160 OW |
1 | /* |
2 | * | |
3 | * Intel Management Engine Interface (Intel MEI) Linux driver | |
733ba91c | 4 | * Copyright (c) 2003-2012, Intel Corporation. |
ab841160 OW |
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 | */ | |
ab841160 OW |
16 | #include <linux/module.h> |
17 | #include <linux/moduleparam.h> | |
18 | #include <linux/kernel.h> | |
19 | #include <linux/device.h> | |
1f180359 | 20 | #include <linux/slab.h> |
ab841160 OW |
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> | |
ab841160 OW |
26 | #include <linux/poll.h> |
27 | #include <linux/init.h> | |
28 | #include <linux/ioctl.h> | |
29 | #include <linux/cdev.h> | |
ab841160 OW |
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 | ||
4f3afe1d | 36 | #include <linux/mei.h> |
47a73801 TW |
37 | |
38 | #include "mei_dev.h" | |
90e0b5f1 | 39 | #include "client.h" |
ab841160 | 40 | |
ab841160 OW |
41 | /** |
42 | * mei_open - the open function | |
43 | * | |
44 | * @inode: pointer to inode structure | |
45 | * @file: pointer to file structure | |
83ce0741 | 46 | * |
a8605ea2 | 47 | * Return: 0 on success, <0 on error |
ab841160 OW |
48 | */ |
49 | static int mei_open(struct inode *inode, struct file *file) | |
50 | { | |
ab841160 | 51 | struct mei_device *dev; |
f3d8e878 | 52 | struct mei_cl *cl; |
2703d4b2 | 53 | |
6f37aca8 | 54 | int err; |
ab841160 | 55 | |
f3d8e878 | 56 | dev = container_of(inode->i_cdev, struct mei_device, cdev); |
5b881e3c | 57 | if (!dev) |
e036cc57 | 58 | return -ENODEV; |
ab841160 OW |
59 | |
60 | mutex_lock(&dev->device_lock); | |
e036cc57 | 61 | |
b210d750 | 62 | if (dev->dev_state != MEI_DEV_ENABLED) { |
2bf94cab | 63 | dev_dbg(dev->dev, "dev_state != MEI_ENABLED dev_state = %s\n", |
b210d750 | 64 | mei_dev_state_str(dev->dev_state)); |
03b8d341 | 65 | err = -ENODEV; |
e036cc57 | 66 | goto err_unlock; |
1b812947 | 67 | } |
ab841160 | 68 | |
03b8d341 TW |
69 | cl = mei_cl_alloc_linked(dev, MEI_HOST_CLIENT_ID_ANY); |
70 | if (IS_ERR(cl)) { | |
71 | err = PTR_ERR(cl); | |
e036cc57 | 72 | goto err_unlock; |
03b8d341 | 73 | } |
ab841160 OW |
74 | |
75 | file->private_data = cl; | |
e036cc57 | 76 | |
ab841160 OW |
77 | mutex_unlock(&dev->device_lock); |
78 | ||
5b881e3c | 79 | return nonseekable_open(inode, file); |
ab841160 | 80 | |
e036cc57 | 81 | err_unlock: |
ab841160 | 82 | mutex_unlock(&dev->device_lock); |
ab841160 OW |
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 | * | |
a8605ea2 | 92 | * Return: 0 on success, <0 on error |
ab841160 OW |
93 | */ |
94 | static int mei_release(struct inode *inode, struct file *file) | |
95 | { | |
96 | struct mei_cl *cl = file->private_data; | |
ab841160 OW |
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); | |
a562d5c2 TW |
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; | |
46922186 | 112 | cl_dbg(dev, cl, "disconnecting\n"); |
90e0b5f1 | 113 | rets = mei_cl_disconnect(cl); |
a562d5c2 TW |
114 | } |
115 | mei_cl_flush_queues(cl); | |
46922186 | 116 | cl_dbg(dev, cl, "removing\n"); |
a562d5c2 | 117 | |
90e0b5f1 | 118 | mei_cl_unlink(cl); |
a562d5c2 | 119 | |
928fa666 TW |
120 | mei_io_cb_free(cl->read_cb); |
121 | cl->read_cb = NULL; | |
ab841160 | 122 | |
a562d5c2 | 123 | file->private_data = NULL; |
ab841160 | 124 | |
a562d5c2 TW |
125 | kfree(cl); |
126 | out: | |
ab841160 OW |
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 | * | |
a8605ea2 | 140 | * Return: >=0 data length on success , <0 on error |
ab841160 OW |
141 | */ |
142 | static ssize_t mei_read(struct file *file, char __user *ubuf, | |
441ab50f | 143 | size_t length, loff_t *offset) |
ab841160 OW |
144 | { |
145 | struct mei_cl *cl = file->private_data; | |
ab841160 OW |
146 | struct mei_cl_cb *cb = NULL; |
147 | struct mei_device *dev; | |
ab841160 OW |
148 | int rets; |
149 | int err; | |
150 | ||
151 | ||
152 | if (WARN_ON(!cl || !cl->dev)) | |
153 | return -ENODEV; | |
154 | ||
155 | dev = cl->dev; | |
156 | ||
dd5de1f1 | 157 | |
ab841160 | 158 | mutex_lock(&dev->device_lock); |
b210d750 | 159 | if (dev->dev_state != MEI_DEV_ENABLED) { |
ab841160 OW |
160 | rets = -ENODEV; |
161 | goto out; | |
162 | } | |
163 | ||
dd5de1f1 TW |
164 | if (length == 0) { |
165 | rets = 0; | |
166 | goto out; | |
167 | } | |
168 | ||
ab841160 | 169 | if (cl == &dev->iamthif_cl) { |
19838fb8 | 170 | rets = mei_amthif_read(dev, file, ubuf, length, offset); |
ab841160 OW |
171 | goto out; |
172 | } | |
173 | ||
3d33ff24 TW |
174 | cb = cl->read_cb; |
175 | if (cb) { | |
139aacf7 TW |
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) { | |
ab841160 | 188 | *offset = 0; |
ab841160 OW |
189 | } |
190 | ||
bca67d68 | 191 | err = mei_cl_read_start(cl, length, file); |
ab841160 | 192 | if (err && err != -EBUSY) { |
2bf94cab | 193 | dev_dbg(dev->dev, |
ab841160 OW |
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 && | |
3d33ff24 TW |
200 | !waitqueue_active(&cl->rx_wait)) { |
201 | ||
ab841160 OW |
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, | |
e2b31644 TW |
210 | MEI_READ_COMPLETE == cl->reading_state || |
211 | mei_cl_is_transitioning(cl))) { | |
212 | ||
ab841160 OW |
213 | if (signal_pending(current)) |
214 | return -EINTR; | |
215 | return -ERESTARTSYS; | |
216 | } | |
217 | ||
218 | mutex_lock(&dev->device_lock); | |
e2b31644 | 219 | if (mei_cl_is_transitioning(cl)) { |
ab841160 OW |
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 | } | |
3d33ff24 | 231 | |
ab841160 OW |
232 | if (cl->reading_state != MEI_READ_COMPLETE) { |
233 | rets = 0; | |
234 | goto out; | |
235 | } | |
3d33ff24 | 236 | |
ab841160 | 237 | copy_buffer: |
3d33ff24 TW |
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 | ||
2bf94cab | 245 | dev_dbg(dev->dev, "buf.size = %d buf.idx= %ld\n", |
5db7514d | 246 | cb->buf.size, cb->buf_idx); |
ebb108ef | 247 | if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) { |
ab841160 OW |
248 | rets = -EMSGSIZE; |
249 | goto free; | |
250 | } | |
251 | ||
ebb108ef TW |
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); | |
ab841160 | 255 | |
5db7514d | 256 | if (copy_to_user(ubuf, cb->buf.data + *offset, length)) { |
2bf94cab | 257 | dev_dbg(dev->dev, "failed to copy data to userland\n"); |
ab841160 OW |
258 | rets = -EFAULT; |
259 | goto free; | |
260 | } | |
261 | ||
262 | rets = length; | |
263 | *offset += length; | |
ebb108ef | 264 | if ((unsigned long)*offset < cb->buf_idx) |
ab841160 OW |
265 | goto out; |
266 | ||
267 | free: | |
601a1efa | 268 | mei_io_cb_free(cb); |
ab841160 | 269 | cl->read_cb = NULL; |
928fa666 TW |
270 | |
271 | cl->reading_state = MEI_IDLE; | |
ab841160 | 272 | out: |
2bf94cab | 273 | dev_dbg(dev->dev, "end mei read rets= %d\n", rets); |
ab841160 OW |
274 | mutex_unlock(&dev->device_lock); |
275 | return rets; | |
276 | } | |
ab841160 OW |
277 | /** |
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 | * | |
a8605ea2 | 285 | * Return: >=0 data length on success , <0 on error |
ab841160 OW |
286 | */ |
287 | static ssize_t mei_write(struct file *file, const char __user *ubuf, | |
441ab50f | 288 | size_t length, loff_t *offset) |
ab841160 OW |
289 | { |
290 | struct mei_cl *cl = file->private_data; | |
79563db9 | 291 | struct mei_me_client *me_cl = NULL; |
ab841160 | 292 | struct mei_cl_cb *write_cb = NULL; |
ab841160 OW |
293 | struct mei_device *dev; |
294 | unsigned long timeout = 0; | |
295 | int rets; | |
ab841160 OW |
296 | |
297 | if (WARN_ON(!cl || !cl->dev)) | |
298 | return -ENODEV; | |
299 | ||
300 | dev = cl->dev; | |
301 | ||
302 | mutex_lock(&dev->device_lock); | |
303 | ||
b210d750 | 304 | if (dev->dev_state != MEI_DEV_ENABLED) { |
75f0ee15 | 305 | rets = -ENODEV; |
4234a6de | 306 | goto out; |
ab841160 OW |
307 | } |
308 | ||
d880f329 | 309 | me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id); |
d320832f | 310 | if (!me_cl) { |
7ca96aa2 | 311 | rets = -ENOTTY; |
4234a6de | 312 | goto out; |
75f0ee15 | 313 | } |
dd5de1f1 TW |
314 | |
315 | if (length == 0) { | |
316 | rets = 0; | |
317 | goto out; | |
318 | } | |
319 | ||
d320832f | 320 | if (length > me_cl->props.max_msg_length) { |
dd5de1f1 | 321 | rets = -EFBIG; |
4234a6de | 322 | goto out; |
75f0ee15 TW |
323 | } |
324 | ||
325 | if (cl->state != MEI_FILE_CONNECTED) { | |
2bf94cab | 326 | dev_err(dev->dev, "host client = %d, is not connected to ME client = %d", |
75f0ee15 | 327 | cl->host_client_id, cl->me_client_id); |
4234a6de TW |
328 | rets = -ENODEV; |
329 | goto out; | |
75f0ee15 | 330 | } |
ab841160 | 331 | if (cl == &dev->iamthif_cl) { |
19838fb8 | 332 | write_cb = mei_amthif_find_read_list_entry(dev, file); |
ab841160 OW |
333 | |
334 | if (write_cb) { | |
335 | timeout = write_cb->read_time + | |
3870c320 | 336 | mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER); |
ab841160 OW |
337 | |
338 | if (time_after(jiffies, timeout) || | |
75f0ee15 TW |
339 | cl->reading_state == MEI_READ_COMPLETE) { |
340 | *offset = 0; | |
601a1efa | 341 | mei_io_cb_free(write_cb); |
75f0ee15 | 342 | write_cb = NULL; |
ab841160 OW |
343 | } |
344 | } | |
345 | } | |
346 | ||
347 | /* free entry used in read */ | |
348 | if (cl->reading_state == MEI_READ_COMPLETE) { | |
349 | *offset = 0; | |
90e0b5f1 | 350 | write_cb = mei_cl_find_read_cb(cl); |
ab841160 | 351 | if (write_cb) { |
601a1efa | 352 | mei_io_cb_free(write_cb); |
ab841160 | 353 | write_cb = NULL; |
ab841160 | 354 | cl->read_cb = NULL; |
928fa666 | 355 | cl->reading_state = MEI_IDLE; |
ab841160 | 356 | } |
d91aaed3 | 357 | } else if (cl->reading_state == MEI_IDLE) |
ab841160 OW |
358 | *offset = 0; |
359 | ||
bca67d68 | 360 | write_cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, file); |
ab841160 | 361 | if (!write_cb) { |
33d28c92 | 362 | rets = -ENOMEM; |
4234a6de | 363 | goto out; |
ab841160 | 364 | } |
ab841160 | 365 | |
5db7514d | 366 | rets = copy_from_user(write_cb->buf.data, ubuf, length); |
d8b29efa | 367 | if (rets) { |
2bf94cab | 368 | dev_dbg(dev->dev, "failed to copy data from userland\n"); |
d8b29efa | 369 | rets = -EFAULT; |
4234a6de | 370 | goto out; |
d8b29efa | 371 | } |
ab841160 | 372 | |
ab841160 | 373 | if (cl == &dev->iamthif_cl) { |
8660172e | 374 | rets = mei_amthif_write(cl, write_cb); |
ab841160 | 375 | |
ab5c4a56 | 376 | if (rets) { |
2bf94cab | 377 | dev_err(dev->dev, |
1a1aca42 | 378 | "amthif write failed with status = %d\n", rets); |
4234a6de | 379 | goto out; |
ab841160 | 380 | } |
79563db9 | 381 | mei_me_cl_put(me_cl); |
ab841160 | 382 | mutex_unlock(&dev->device_lock); |
75f0ee15 | 383 | return length; |
ab841160 OW |
384 | } |
385 | ||
4234a6de | 386 | rets = mei_cl_write(cl, write_cb, false); |
b0d0cf77 | 387 | out: |
79563db9 | 388 | mei_me_cl_put(me_cl); |
ab841160 | 389 | mutex_unlock(&dev->device_lock); |
4234a6de TW |
390 | if (rets < 0) |
391 | mei_io_cb_free(write_cb); | |
ab841160 OW |
392 | return rets; |
393 | } | |
394 | ||
9f81abda TW |
395 | /** |
396 | * mei_ioctl_connect_client - the connect to fw client IOCTL function | |
397 | * | |
9f81abda | 398 | * @file: private data of the file object |
a8605ea2 | 399 | * @data: IOCTL connect data, input and output parameters |
9f81abda TW |
400 | * |
401 | * Locking: called under "dev->device_lock" lock | |
402 | * | |
a8605ea2 | 403 | * Return: 0 on success, <0 on failure. |
9f81abda TW |
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; | |
d320832f | 410 | struct mei_me_client *me_cl; |
9f81abda | 411 | struct mei_cl *cl; |
9f81abda TW |
412 | int rets; |
413 | ||
414 | cl = file->private_data; | |
9f81abda TW |
415 | dev = cl->dev; |
416 | ||
79563db9 TW |
417 | if (dev->dev_state != MEI_DEV_ENABLED) |
418 | return -ENODEV; | |
9f81abda TW |
419 | |
420 | if (cl->state != MEI_FILE_INITIALIZING && | |
79563db9 TW |
421 | cl->state != MEI_FILE_DISCONNECTED) |
422 | return -EBUSY; | |
9f81abda TW |
423 | |
424 | /* find ME client we're trying to connect to */ | |
d320832f TW |
425 | me_cl = mei_me_cl_by_uuid(dev, &data->in_client_uuid); |
426 | if (!me_cl || me_cl->props.fixed_address) { | |
2bf94cab | 427 | dev_dbg(dev->dev, "Cannot connect to FW Client UUID = %pUl\n", |
80fe6361 | 428 | &data->in_client_uuid); |
79563db9 | 429 | return -ENOTTY; |
9f81abda TW |
430 | } |
431 | ||
d320832f | 432 | cl->me_client_id = me_cl->client_id; |
d880f329 | 433 | cl->cl_uuid = me_cl->props.protocol_name; |
80fe6361 | 434 | |
2bf94cab | 435 | dev_dbg(dev->dev, "Connect to FW Client ID = %d\n", |
9f81abda | 436 | cl->me_client_id); |
2bf94cab | 437 | dev_dbg(dev->dev, "FW Client - Protocol Version = %d\n", |
d320832f | 438 | me_cl->props.protocol_version); |
2bf94cab | 439 | dev_dbg(dev->dev, "FW Client - Max Msg Len = %d\n", |
d320832f | 440 | me_cl->props.max_msg_length); |
9f81abda | 441 | |
1a1aca42 | 442 | /* if we're connecting to amthif client then we will use the |
9f81abda TW |
443 | * existing connection |
444 | */ | |
1a1aca42 | 445 | if (uuid_le_cmp(data->in_client_uuid, mei_amthif_guid) == 0) { |
2bf94cab | 446 | dev_dbg(dev->dev, "FW Client is amthi\n"); |
9f81abda TW |
447 | if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) { |
448 | rets = -ENODEV; | |
449 | goto end; | |
450 | } | |
9f81abda TW |
451 | mei_cl_unlink(cl); |
452 | ||
453 | kfree(cl); | |
454 | cl = NULL; | |
22f96a0e | 455 | dev->iamthif_open_count++; |
9f81abda TW |
456 | file->private_data = &dev->iamthif_cl; |
457 | ||
458 | client = &data->out_client_properties; | |
d320832f TW |
459 | client->max_msg_length = me_cl->props.max_msg_length; |
460 | client->protocol_version = me_cl->props.protocol_version; | |
9f81abda TW |
461 | rets = dev->iamthif_cl.status; |
462 | ||
463 | goto end; | |
464 | } | |
465 | ||
9f81abda TW |
466 | /* prepare the output buffer */ |
467 | client = &data->out_client_properties; | |
d320832f TW |
468 | client->max_msg_length = me_cl->props.max_msg_length; |
469 | client->protocol_version = me_cl->props.protocol_version; | |
2bf94cab | 470 | dev_dbg(dev->dev, "Can connect?\n"); |
9f81abda | 471 | |
9f81abda TW |
472 | rets = mei_cl_connect(cl, file); |
473 | ||
474 | end: | |
79563db9 | 475 | mei_me_cl_put(me_cl); |
9f81abda TW |
476 | return rets; |
477 | } | |
478 | ||
ab841160 OW |
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 | * | |
a8605ea2 | 486 | * Return: 0 on success , <0 on error |
ab841160 OW |
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; | |
154eb18f | 492 | struct mei_connect_client_data connect_data; |
ab841160 OW |
493 | int rets; |
494 | ||
ab841160 OW |
495 | |
496 | if (WARN_ON(!cl || !cl->dev)) | |
497 | return -ENODEV; | |
498 | ||
499 | dev = cl->dev; | |
500 | ||
2bf94cab | 501 | dev_dbg(dev->dev, "IOCTL cmd = 0x%x", cmd); |
ab841160 OW |
502 | |
503 | mutex_lock(&dev->device_lock); | |
b210d750 | 504 | if (dev->dev_state != MEI_DEV_ENABLED) { |
ab841160 OW |
505 | rets = -ENODEV; |
506 | goto out; | |
507 | } | |
508 | ||
4f046e7b TW |
509 | switch (cmd) { |
510 | case IOCTL_MEI_CONNECT_CLIENT: | |
2bf94cab | 511 | dev_dbg(dev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n"); |
154eb18f | 512 | if (copy_from_user(&connect_data, (char __user *)data, |
4f046e7b | 513 | sizeof(struct mei_connect_client_data))) { |
2bf94cab | 514 | dev_dbg(dev->dev, "failed to copy data from userland\n"); |
4f046e7b TW |
515 | rets = -EFAULT; |
516 | goto out; | |
517 | } | |
ab841160 | 518 | |
154eb18f | 519 | rets = mei_ioctl_connect_client(file, &connect_data); |
4f046e7b TW |
520 | if (rets) |
521 | goto out; | |
ab841160 | 522 | |
4f046e7b | 523 | /* if all is ok, copying the data back to user. */ |
154eb18f | 524 | if (copy_to_user((char __user *)data, &connect_data, |
ab841160 | 525 | sizeof(struct mei_connect_client_data))) { |
2bf94cab | 526 | dev_dbg(dev->dev, "failed to copy data to userland\n"); |
4f046e7b TW |
527 | rets = -EFAULT; |
528 | goto out; | |
529 | } | |
530 | ||
531 | break; | |
154eb18f | 532 | |
4f046e7b | 533 | default: |
2bf94cab | 534 | dev_err(dev->dev, ": unsupported ioctl %d.\n", cmd); |
4f046e7b | 535 | rets = -ENOIOCTLCMD; |
ab841160 OW |
536 | } |
537 | ||
538 | out: | |
ab841160 OW |
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 | * | |
a8605ea2 | 550 | * Return: 0 on success , <0 on error |
ab841160 OW |
551 | */ |
552 | #ifdef CONFIG_COMPAT | |
553 | static long mei_compat_ioctl(struct file *file, | |
441ab50f | 554 | unsigned int cmd, unsigned long data) |
ab841160 OW |
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 | * | |
a8605ea2 | 567 | * Return: poll mask |
ab841160 OW |
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)) | |
b950ac1d | 576 | return POLLERR; |
ab841160 OW |
577 | |
578 | dev = cl->dev; | |
579 | ||
580 | mutex_lock(&dev->device_lock); | |
581 | ||
b950ac1d TW |
582 | if (!mei_cl_is_connected(cl)) { |
583 | mask = POLLERR; | |
ab841160 OW |
584 | goto out; |
585 | } | |
586 | ||
587 | mutex_unlock(&dev->device_lock); | |
b950ac1d TW |
588 | |
589 | ||
590 | if (cl == &dev->iamthif_cl) | |
591 | return mei_amthif_poll(dev, file, wait); | |
592 | ||
ab841160 | 593 | poll_wait(file, &cl->tx_wait, wait); |
b950ac1d | 594 | |
ab841160 | 595 | mutex_lock(&dev->device_lock); |
b950ac1d TW |
596 | |
597 | if (!mei_cl_is_connected(cl)) { | |
598 | mask = POLLERR; | |
599 | goto out; | |
600 | } | |
601 | ||
34ec4366 | 602 | mask |= (POLLIN | POLLRDNORM); |
ab841160 OW |
603 | |
604 | out: | |
605 | mutex_unlock(&dev->device_lock); | |
606 | return mask; | |
607 | } | |
608 | ||
55c4e640 TW |
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 | ||
5b881e3c OW |
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 | ||
f3d8e878 AU |
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 | * | |
a8605ea2 | 675 | * Return: allocated minor, or -ENOSPC if no free minor left |
5b881e3c | 676 | */ |
f3d8e878 AU |
677 | static int mei_minor_get(struct mei_device *dev) |
678 | { | |
679 | int ret; | |
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) | |
2bf94cab | 686 | dev_err(dev->dev, "too many mei devices\n"); |
5b881e3c | 687 | |
f3d8e878 AU |
688 | mutex_unlock(&mei_minor_lock); |
689 | return ret; | |
690 | } | |
30e53bb8 | 691 | |
f3d8e878 AU |
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) | |
9a123f19 | 698 | { |
f3d8e878 AU |
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) | |
30e53bb8 TW |
711 | return ret; |
712 | ||
f3d8e878 AU |
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 | ||
55c4e640 TW |
726 | clsdev = device_create_with_groups(mei_class, parent, devno, |
727 | dev, mei_groups, | |
728 | "mei%d", dev->minor); | |
f3d8e878 AU |
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 | } | |
30e53bb8 TW |
742 | |
743 | return 0; | |
f3d8e878 AU |
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; | |
5b881e3c | 752 | } |
40e0b67b | 753 | EXPORT_SYMBOL_GPL(mei_register); |
5b881e3c | 754 | |
30e53bb8 | 755 | void mei_deregister(struct mei_device *dev) |
5b881e3c | 756 | { |
f3d8e878 AU |
757 | int devno; |
758 | ||
759 | devno = dev->cdev.dev; | |
760 | cdev_del(&dev->cdev); | |
761 | ||
30e53bb8 | 762 | mei_dbgfs_deregister(dev); |
f3d8e878 AU |
763 | |
764 | device_destroy(mei_class, devno); | |
765 | ||
766 | mei_minor_free(dev); | |
5b881e3c | 767 | } |
40e0b67b | 768 | EXPORT_SYMBOL_GPL(mei_deregister); |
ab841160 | 769 | |
cf3baefb SO |
770 | static int __init mei_init(void) |
771 | { | |
f3d8e878 AU |
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; | |
cf3baefb SO |
801 | } |
802 | ||
803 | static void __exit mei_exit(void) | |
804 | { | |
f3d8e878 AU |
805 | unregister_chrdev_region(mei_devt, MEI_MAX_DEVS); |
806 | class_destroy(mei_class); | |
cf3baefb SO |
807 | mei_cl_bus_exit(); |
808 | } | |
809 | ||
810 | module_init(mei_init); | |
811 | module_exit(mei_exit); | |
812 | ||
40e0b67b TW |
813 | MODULE_AUTHOR("Intel Corporation"); |
814 | MODULE_DESCRIPTION("Intel(R) Management Engine Interface"); | |
827eef51 | 815 | MODULE_LICENSE("GPL v2"); |
ab841160 | 816 |