]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/misc/mei/main.c
mei: debugfs: allow hbm features list dump in earlier stages
[mirror_ubuntu-artful-kernel.git] / drivers / misc / mei / main.c
CommitLineData
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>
ab841160
OW
25#include <linux/poll.h>
26#include <linux/init.h>
27#include <linux/ioctl.h>
28#include <linux/cdev.h>
ab841160
OW
29#include <linux/sched.h>
30#include <linux/uuid.h>
31#include <linux/compat.h>
32#include <linux/jiffies.h>
33#include <linux/interrupt.h>
34
4f3afe1d 35#include <linux/mei.h>
47a73801
TW
36
37#include "mei_dev.h"
90e0b5f1 38#include "client.h"
ab841160 39
ab841160
OW
40/**
41 * mei_open - the open function
42 *
43 * @inode: pointer to inode structure
44 * @file: pointer to file structure
83ce0741 45 *
a8605ea2 46 * Return: 0 on success, <0 on error
ab841160
OW
47 */
48static int mei_open(struct inode *inode, struct file *file)
49{
ab841160 50 struct mei_device *dev;
f3d8e878 51 struct mei_cl *cl;
2703d4b2 52
6f37aca8 53 int err;
ab841160 54
f3d8e878 55 dev = container_of(inode->i_cdev, struct mei_device, cdev);
5b881e3c 56 if (!dev)
e036cc57 57 return -ENODEV;
ab841160
OW
58
59 mutex_lock(&dev->device_lock);
e036cc57 60
b210d750 61 if (dev->dev_state != MEI_DEV_ENABLED) {
2bf94cab 62 dev_dbg(dev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
b210d750 63 mei_dev_state_str(dev->dev_state));
03b8d341 64 err = -ENODEV;
e036cc57 65 goto err_unlock;
1b812947 66 }
ab841160 67
03b8d341
TW
68 cl = mei_cl_alloc_linked(dev, MEI_HOST_CLIENT_ID_ANY);
69 if (IS_ERR(cl)) {
70 err = PTR_ERR(cl);
e036cc57 71 goto err_unlock;
03b8d341 72 }
ab841160
OW
73
74 file->private_data = cl;
e036cc57 75
ab841160
OW
76 mutex_unlock(&dev->device_lock);
77
5b881e3c 78 return nonseekable_open(inode, file);
ab841160 79
e036cc57 80err_unlock:
ab841160 81 mutex_unlock(&dev->device_lock);
ab841160
OW
82 return err;
83}
84
85/**
86 * mei_release - the release function
87 *
88 * @inode: pointer to inode structure
89 * @file: pointer to file structure
90 *
a8605ea2 91 * Return: 0 on success, <0 on error
ab841160
OW
92 */
93static int mei_release(struct inode *inode, struct file *file)
94{
95 struct mei_cl *cl = file->private_data;
ab841160 96 struct mei_device *dev;
3c666182 97 int rets;
ab841160
OW
98
99 if (WARN_ON(!cl || !cl->dev))
100 return -ENODEV;
101
102 dev = cl->dev;
103
104 mutex_lock(&dev->device_lock);
a562d5c2
TW
105 if (cl == &dev->iamthif_cl) {
106 rets = mei_amthif_release(dev, file);
107 goto out;
108 }
3c666182
TW
109 rets = mei_cl_disconnect(cl);
110
a9bed610 111 mei_cl_flush_queues(cl, file);
46922186 112 cl_dbg(dev, cl, "removing\n");
a562d5c2 113
90e0b5f1 114 mei_cl_unlink(cl);
a562d5c2 115
a562d5c2 116 file->private_data = NULL;
ab841160 117
a562d5c2
TW
118 kfree(cl);
119out:
ab841160
OW
120 mutex_unlock(&dev->device_lock);
121 return rets;
122}
123
124
125/**
126 * mei_read - the read function.
127 *
128 * @file: pointer to file structure
129 * @ubuf: pointer to user buffer
130 * @length: buffer length
131 * @offset: data offset in buffer
132 *
a8605ea2 133 * Return: >=0 data length on success , <0 on error
ab841160
OW
134 */
135static ssize_t mei_read(struct file *file, char __user *ubuf,
441ab50f 136 size_t length, loff_t *offset)
ab841160
OW
137{
138 struct mei_cl *cl = file->private_data;
ab841160 139 struct mei_device *dev;
a9bed610 140 struct mei_cl_cb *cb = NULL;
ab841160
OW
141 int rets;
142 int err;
143
144
145 if (WARN_ON(!cl || !cl->dev))
146 return -ENODEV;
147
148 dev = cl->dev;
149
dd5de1f1 150
ab841160 151 mutex_lock(&dev->device_lock);
b210d750 152 if (dev->dev_state != MEI_DEV_ENABLED) {
ab841160
OW
153 rets = -ENODEV;
154 goto out;
155 }
156
dd5de1f1
TW
157 if (length == 0) {
158 rets = 0;
159 goto out;
160 }
161
8b2458f4
AU
162 if (ubuf == NULL) {
163 rets = -EMSGSIZE;
164 goto out;
165 }
166
ab841160 167 if (cl == &dev->iamthif_cl) {
19838fb8 168 rets = mei_amthif_read(dev, file, ubuf, length, offset);
ab841160
OW
169 goto out;
170 }
171
a9bed610 172 cb = mei_cl_read_cb(cl, file);
8b2458f4
AU
173 if (cb)
174 goto copy_buffer;
175
176 if (*offset > 0)
ab841160 177 *offset = 0;
ab841160 178
bca67d68 179 err = mei_cl_read_start(cl, length, file);
ab841160 180 if (err && err != -EBUSY) {
292f82c8 181 cl_dbg(dev, cl, "mei start read failure status = %d\n", err);
ab841160
OW
182 rets = err;
183 goto out;
184 }
185
a9bed610 186 if (list_empty(&cl->rd_completed) && !waitqueue_active(&cl->rx_wait)) {
ab841160
OW
187 if (file->f_flags & O_NONBLOCK) {
188 rets = -EAGAIN;
189 goto out;
190 }
191
192 mutex_unlock(&dev->device_lock);
193
194 if (wait_event_interruptible(cl->rx_wait,
a9bed610 195 (!list_empty(&cl->rd_completed)) ||
6a84d63d 196 (!mei_cl_is_connected(cl)))) {
e2b31644 197
ab841160
OW
198 if (signal_pending(current))
199 return -EINTR;
200 return -ERESTARTSYS;
201 }
202
203 mutex_lock(&dev->device_lock);
6a84d63d 204 if (!mei_cl_is_connected(cl)) {
ab841160
OW
205 rets = -EBUSY;
206 goto out;
207 }
208 }
209
a9bed610 210 cb = mei_cl_read_cb(cl, file);
ab841160 211 if (!cb) {
eeabfcf5
AU
212 if (mei_cl_is_fixed_address(cl) && dev->allow_fixed_address) {
213 cb = mei_cl_read_cb(cl, NULL);
214 if (cb)
215 goto copy_buffer;
216 }
ab841160
OW
217 rets = 0;
218 goto out;
219 }
3d33ff24 220
ab841160 221copy_buffer:
3d33ff24
TW
222 /* now copy the data to user space */
223 if (cb->status) {
224 rets = cb->status;
292f82c8 225 cl_dbg(dev, cl, "read operation failed %d\n", rets);
3d33ff24
TW
226 goto free;
227 }
228
8b2458f4
AU
229 cl_dbg(dev, cl, "buf.size = %d buf.idx = %ld offset = %lld\n",
230 cb->buf.size, cb->buf_idx, *offset);
231 if (*offset >= cb->buf_idx) {
232 rets = 0;
ab841160
OW
233 goto free;
234 }
235
ebb108ef
TW
236 /* length is being truncated to PAGE_SIZE,
237 * however buf_idx may point beyond that */
238 length = min_t(size_t, length, cb->buf_idx - *offset);
ab841160 239
5db7514d 240 if (copy_to_user(ubuf, cb->buf.data + *offset, length)) {
2bf94cab 241 dev_dbg(dev->dev, "failed to copy data to userland\n");
ab841160
OW
242 rets = -EFAULT;
243 goto free;
244 }
245
246 rets = length;
247 *offset += length;
ebb108ef 248 if ((unsigned long)*offset < cb->buf_idx)
ab841160
OW
249 goto out;
250
251free:
601a1efa 252 mei_io_cb_free(cb);
8b2458f4 253 *offset = 0;
928fa666 254
ab841160 255out:
292f82c8 256 cl_dbg(dev, cl, "end mei read rets = %d\n", rets);
ab841160
OW
257 mutex_unlock(&dev->device_lock);
258 return rets;
259}
ab841160
OW
260/**
261 * mei_write - the write function.
262 *
263 * @file: pointer to file structure
264 * @ubuf: pointer to user buffer
265 * @length: buffer length
266 * @offset: data offset in buffer
267 *
a8605ea2 268 * Return: >=0 data length on success , <0 on error
ab841160
OW
269 */
270static ssize_t mei_write(struct file *file, const char __user *ubuf,
441ab50f 271 size_t length, loff_t *offset)
ab841160
OW
272{
273 struct mei_cl *cl = file->private_data;
274 struct mei_cl_cb *write_cb = NULL;
ab841160
OW
275 struct mei_device *dev;
276 unsigned long timeout = 0;
277 int rets;
ab841160
OW
278
279 if (WARN_ON(!cl || !cl->dev))
280 return -ENODEV;
281
282 dev = cl->dev;
283
284 mutex_lock(&dev->device_lock);
285
b210d750 286 if (dev->dev_state != MEI_DEV_ENABLED) {
75f0ee15 287 rets = -ENODEV;
4234a6de 288 goto out;
ab841160
OW
289 }
290
d49ed64a
AU
291 if (!mei_cl_is_connected(cl)) {
292 cl_err(dev, cl, "is not connected");
293 rets = -ENODEV;
4234a6de 294 goto out;
75f0ee15 295 }
dd5de1f1 296
d49ed64a
AU
297 if (!mei_me_cl_is_active(cl->me_cl)) {
298 rets = -ENOTTY;
dd5de1f1
TW
299 goto out;
300 }
301
d49ed64a 302 if (length > mei_cl_mtu(cl)) {
dd5de1f1 303 rets = -EFBIG;
4234a6de 304 goto out;
75f0ee15
TW
305 }
306
d49ed64a
AU
307 if (length == 0) {
308 rets = 0;
4234a6de 309 goto out;
75f0ee15 310 }
d49ed64a 311
ab841160 312 if (cl == &dev->iamthif_cl) {
19838fb8 313 write_cb = mei_amthif_find_read_list_entry(dev, file);
ab841160
OW
314
315 if (write_cb) {
316 timeout = write_cb->read_time +
3870c320 317 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
ab841160 318
a9bed610 319 if (time_after(jiffies, timeout)) {
75f0ee15 320 *offset = 0;
601a1efa 321 mei_io_cb_free(write_cb);
75f0ee15 322 write_cb = NULL;
ab841160
OW
323 }
324 }
325 }
326
a9bed610 327 *offset = 0;
bca67d68 328 write_cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, file);
ab841160 329 if (!write_cb) {
33d28c92 330 rets = -ENOMEM;
4234a6de 331 goto out;
ab841160 332 }
ab841160 333
5db7514d 334 rets = copy_from_user(write_cb->buf.data, ubuf, length);
d8b29efa 335 if (rets) {
2bf94cab 336 dev_dbg(dev->dev, "failed to copy data from userland\n");
d8b29efa 337 rets = -EFAULT;
4234a6de 338 goto out;
d8b29efa 339 }
ab841160 340
ab841160 341 if (cl == &dev->iamthif_cl) {
8660172e 342 rets = mei_amthif_write(cl, write_cb);
ab841160 343
ab5c4a56 344 if (rets) {
2bf94cab 345 dev_err(dev->dev,
1a1aca42 346 "amthif write failed with status = %d\n", rets);
4234a6de 347 goto out;
ab841160
OW
348 }
349 mutex_unlock(&dev->device_lock);
75f0ee15 350 return length;
ab841160
OW
351 }
352
4234a6de 353 rets = mei_cl_write(cl, write_cb, false);
b0d0cf77 354out:
ab841160 355 mutex_unlock(&dev->device_lock);
4234a6de
TW
356 if (rets < 0)
357 mei_io_cb_free(write_cb);
ab841160
OW
358 return rets;
359}
360
9f81abda
TW
361/**
362 * mei_ioctl_connect_client - the connect to fw client IOCTL function
363 *
9f81abda 364 * @file: private data of the file object
a8605ea2 365 * @data: IOCTL connect data, input and output parameters
9f81abda
TW
366 *
367 * Locking: called under "dev->device_lock" lock
368 *
a8605ea2 369 * Return: 0 on success, <0 on failure.
9f81abda
TW
370 */
371static int mei_ioctl_connect_client(struct file *file,
372 struct mei_connect_client_data *data)
373{
374 struct mei_device *dev;
375 struct mei_client *client;
d320832f 376 struct mei_me_client *me_cl;
9f81abda 377 struct mei_cl *cl;
9f81abda
TW
378 int rets;
379
380 cl = file->private_data;
9f81abda
TW
381 dev = cl->dev;
382
79563db9
TW
383 if (dev->dev_state != MEI_DEV_ENABLED)
384 return -ENODEV;
9f81abda
TW
385
386 if (cl->state != MEI_FILE_INITIALIZING &&
79563db9
TW
387 cl->state != MEI_FILE_DISCONNECTED)
388 return -EBUSY;
9f81abda
TW
389
390 /* find ME client we're trying to connect to */
d320832f 391 me_cl = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
eeabfcf5
AU
392 if (!me_cl ||
393 (me_cl->props.fixed_address && !dev->allow_fixed_address)) {
2bf94cab 394 dev_dbg(dev->dev, "Cannot connect to FW Client UUID = %pUl\n",
d49ed64a
AU
395 &data->in_client_uuid);
396 mei_me_cl_put(me_cl);
79563db9 397 return -ENOTTY;
9f81abda
TW
398 }
399
2bf94cab 400 dev_dbg(dev->dev, "Connect to FW Client ID = %d\n",
d49ed64a 401 me_cl->client_id);
2bf94cab 402 dev_dbg(dev->dev, "FW Client - Protocol Version = %d\n",
d320832f 403 me_cl->props.protocol_version);
2bf94cab 404 dev_dbg(dev->dev, "FW Client - Max Msg Len = %d\n",
d320832f 405 me_cl->props.max_msg_length);
9f81abda 406
1a1aca42 407 /* if we're connecting to amthif client then we will use the
9f81abda
TW
408 * existing connection
409 */
1a1aca42 410 if (uuid_le_cmp(data->in_client_uuid, mei_amthif_guid) == 0) {
2bf94cab 411 dev_dbg(dev->dev, "FW Client is amthi\n");
f3de9b63 412 if (!mei_cl_is_connected(&dev->iamthif_cl)) {
9f81abda
TW
413 rets = -ENODEV;
414 goto end;
415 }
9f81abda
TW
416 mei_cl_unlink(cl);
417
418 kfree(cl);
419 cl = NULL;
22f96a0e 420 dev->iamthif_open_count++;
9f81abda
TW
421 file->private_data = &dev->iamthif_cl;
422
423 client = &data->out_client_properties;
d320832f
TW
424 client->max_msg_length = me_cl->props.max_msg_length;
425 client->protocol_version = me_cl->props.protocol_version;
9f81abda
TW
426 rets = dev->iamthif_cl.status;
427
428 goto end;
429 }
430
9f81abda
TW
431 /* prepare the output buffer */
432 client = &data->out_client_properties;
d320832f
TW
433 client->max_msg_length = me_cl->props.max_msg_length;
434 client->protocol_version = me_cl->props.protocol_version;
2bf94cab 435 dev_dbg(dev->dev, "Can connect?\n");
9f81abda 436
d49ed64a 437 rets = mei_cl_connect(cl, me_cl, file);
9f81abda
TW
438
439end:
79563db9 440 mei_me_cl_put(me_cl);
9f81abda
TW
441 return rets;
442}
443
3c7c8468
TW
444/**
445 * mei_ioctl_client_notify_request -
446 * propagate event notification request to client
447 *
448 * @file: pointer to file structure
449 * @request: 0 - disable, 1 - enable
450 *
451 * Return: 0 on success , <0 on error
452 */
453static int mei_ioctl_client_notify_request(struct file *file, u32 request)
454{
455 struct mei_cl *cl = file->private_data;
456
457 return mei_cl_notify_request(cl, file, request);
458}
459
460/**
461 * mei_ioctl_client_notify_get - wait for notification request
462 *
463 * @file: pointer to file structure
464 * @notify_get: 0 - disable, 1 - enable
465 *
466 * Return: 0 on success , <0 on error
467 */
468static int mei_ioctl_client_notify_get(struct file *file, u32 *notify_get)
469{
470 struct mei_cl *cl = file->private_data;
471 bool notify_ev;
472 bool block = (file->f_flags & O_NONBLOCK) == 0;
473 int rets;
474
475 rets = mei_cl_notify_get(cl, block, &notify_ev);
476 if (rets)
477 return rets;
478
479 *notify_get = notify_ev ? 1 : 0;
480 return 0;
481}
482
ab841160
OW
483/**
484 * mei_ioctl - the IOCTL function
485 *
486 * @file: pointer to file structure
487 * @cmd: ioctl command
488 * @data: pointer to mei message structure
489 *
a8605ea2 490 * Return: 0 on success , <0 on error
ab841160
OW
491 */
492static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
493{
494 struct mei_device *dev;
495 struct mei_cl *cl = file->private_data;
154eb18f 496 struct mei_connect_client_data connect_data;
3c7c8468 497 u32 notify_get, notify_req;
ab841160
OW
498 int rets;
499
ab841160
OW
500
501 if (WARN_ON(!cl || !cl->dev))
502 return -ENODEV;
503
504 dev = cl->dev;
505
2bf94cab 506 dev_dbg(dev->dev, "IOCTL cmd = 0x%x", cmd);
ab841160
OW
507
508 mutex_lock(&dev->device_lock);
b210d750 509 if (dev->dev_state != MEI_DEV_ENABLED) {
ab841160
OW
510 rets = -ENODEV;
511 goto out;
512 }
513
4f046e7b
TW
514 switch (cmd) {
515 case IOCTL_MEI_CONNECT_CLIENT:
2bf94cab 516 dev_dbg(dev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
154eb18f 517 if (copy_from_user(&connect_data, (char __user *)data,
4f046e7b 518 sizeof(struct mei_connect_client_data))) {
2bf94cab 519 dev_dbg(dev->dev, "failed to copy data from userland\n");
4f046e7b
TW
520 rets = -EFAULT;
521 goto out;
522 }
ab841160 523
154eb18f 524 rets = mei_ioctl_connect_client(file, &connect_data);
4f046e7b
TW
525 if (rets)
526 goto out;
ab841160 527
4f046e7b 528 /* if all is ok, copying the data back to user. */
154eb18f 529 if (copy_to_user((char __user *)data, &connect_data,
ab841160 530 sizeof(struct mei_connect_client_data))) {
2bf94cab 531 dev_dbg(dev->dev, "failed to copy data to userland\n");
4f046e7b
TW
532 rets = -EFAULT;
533 goto out;
534 }
535
536 break;
154eb18f 537
3c7c8468
TW
538 case IOCTL_MEI_NOTIFY_SET:
539 dev_dbg(dev->dev, ": IOCTL_MEI_NOTIFY_SET.\n");
540 if (copy_from_user(&notify_req,
541 (char __user *)data, sizeof(notify_req))) {
542 dev_dbg(dev->dev, "failed to copy data from userland\n");
543 rets = -EFAULT;
544 goto out;
545 }
546 rets = mei_ioctl_client_notify_request(file, notify_req);
547 break;
548
549 case IOCTL_MEI_NOTIFY_GET:
550 dev_dbg(dev->dev, ": IOCTL_MEI_NOTIFY_GET.\n");
551 rets = mei_ioctl_client_notify_get(file, &notify_get);
552 if (rets)
553 goto out;
554
555 dev_dbg(dev->dev, "copy connect data to user\n");
556 if (copy_to_user((char __user *)data,
557 &notify_get, sizeof(notify_get))) {
558 dev_dbg(dev->dev, "failed to copy data to userland\n");
559 rets = -EFAULT;
560 goto out;
561
562 }
563 break;
564
4f046e7b 565 default:
2bf94cab 566 dev_err(dev->dev, ": unsupported ioctl %d.\n", cmd);
4f046e7b 567 rets = -ENOIOCTLCMD;
ab841160
OW
568 }
569
570out:
ab841160
OW
571 mutex_unlock(&dev->device_lock);
572 return rets;
573}
574
575/**
576 * mei_compat_ioctl - the compat IOCTL function
577 *
578 * @file: pointer to file structure
579 * @cmd: ioctl command
580 * @data: pointer to mei message structure
581 *
a8605ea2 582 * Return: 0 on success , <0 on error
ab841160
OW
583 */
584#ifdef CONFIG_COMPAT
585static long mei_compat_ioctl(struct file *file,
441ab50f 586 unsigned int cmd, unsigned long data)
ab841160
OW
587{
588 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
589}
590#endif
591
592
593/**
594 * mei_poll - the poll function
595 *
596 * @file: pointer to file structure
597 * @wait: pointer to poll_table structure
598 *
a8605ea2 599 * Return: poll mask
ab841160
OW
600 */
601static unsigned int mei_poll(struct file *file, poll_table *wait)
602{
1d9013f0 603 unsigned long req_events = poll_requested_events(wait);
ab841160
OW
604 struct mei_cl *cl = file->private_data;
605 struct mei_device *dev;
606 unsigned int mask = 0;
2c84c297 607 bool notify_en;
ab841160
OW
608
609 if (WARN_ON(!cl || !cl->dev))
b950ac1d 610 return POLLERR;
ab841160
OW
611
612 dev = cl->dev;
613
614 mutex_lock(&dev->device_lock);
615
2c84c297 616 notify_en = cl->notify_en && (req_events & POLLPRI);
f3de9b63
TW
617
618 if (dev->dev_state != MEI_DEV_ENABLED ||
619 !mei_cl_is_connected(cl)) {
b950ac1d 620 mask = POLLERR;
ab841160
OW
621 goto out;
622 }
623
1d9013f0
TW
624 if (cl == &dev->iamthif_cl) {
625 mask = mei_amthif_poll(dev, file, wait);
b950ac1d
TW
626 goto out;
627 }
628
2c84c297
TW
629 if (notify_en) {
630 poll_wait(file, &cl->ev_wait, wait);
631 if (cl->notify_ev)
632 mask |= POLLPRI;
633 }
634
1d9013f0
TW
635 if (req_events & (POLLIN | POLLRDNORM)) {
636 poll_wait(file, &cl->rx_wait, wait);
637
638 if (!list_empty(&cl->rd_completed))
639 mask |= POLLIN | POLLRDNORM;
640 else
641 mei_cl_read_start(cl, 0, file);
642 }
ab841160
OW
643
644out:
645 mutex_unlock(&dev->device_lock);
646 return mask;
647}
648
237092bf
TW
649/**
650 * mei_fasync - asynchronous io support
651 *
652 * @fd: file descriptor
653 * @file: pointer to file structure
654 * @band: band bitmap
655 *
ed6dc538
TW
656 * Return: negative on error,
657 * 0 if it did no changes,
658 * and positive a process was added or deleted
237092bf
TW
659 */
660static int mei_fasync(int fd, struct file *file, int band)
661{
662
663 struct mei_cl *cl = file->private_data;
664
665 if (!mei_cl_is_connected(cl))
ed6dc538 666 return -ENODEV;
237092bf
TW
667
668 return fasync_helper(fd, file, band, &cl->ev_async);
669}
670
55c4e640
TW
671/**
672 * fw_status_show - mei device attribute show method
673 *
674 * @device: device pointer
675 * @attr: attribute pointer
676 * @buf: char out buffer
677 *
678 * Return: number of the bytes printed into buf or error
679 */
680static ssize_t fw_status_show(struct device *device,
681 struct device_attribute *attr, char *buf)
682{
683 struct mei_device *dev = dev_get_drvdata(device);
684 struct mei_fw_status fw_status;
685 int err, i;
686 ssize_t cnt = 0;
687
688 mutex_lock(&dev->device_lock);
689 err = mei_fw_status(dev, &fw_status);
690 mutex_unlock(&dev->device_lock);
691 if (err) {
692 dev_err(device, "read fw_status error = %d\n", err);
693 return err;
694 }
695
696 for (i = 0; i < fw_status.count; i++)
697 cnt += scnprintf(buf + cnt, PAGE_SIZE - cnt, "%08X\n",
698 fw_status.status[i]);
699 return cnt;
700}
701static DEVICE_ATTR_RO(fw_status);
702
703static struct attribute *mei_attrs[] = {
704 &dev_attr_fw_status.attr,
705 NULL
706};
707ATTRIBUTE_GROUPS(mei);
708
5b881e3c
OW
709/*
710 * file operations structure will be used for mei char device.
711 */
712static const struct file_operations mei_fops = {
713 .owner = THIS_MODULE,
714 .read = mei_read,
715 .unlocked_ioctl = mei_ioctl,
716#ifdef CONFIG_COMPAT
717 .compat_ioctl = mei_compat_ioctl,
718#endif
719 .open = mei_open,
720 .release = mei_release,
721 .write = mei_write,
722 .poll = mei_poll,
237092bf 723 .fasync = mei_fasync,
5b881e3c
OW
724 .llseek = no_llseek
725};
726
f3d8e878
AU
727static struct class *mei_class;
728static dev_t mei_devt;
729#define MEI_MAX_DEVS MINORMASK
730static DEFINE_MUTEX(mei_minor_lock);
731static DEFINE_IDR(mei_idr);
732
733/**
734 * mei_minor_get - obtain next free device minor number
735 *
736 * @dev: device pointer
737 *
a8605ea2 738 * Return: allocated minor, or -ENOSPC if no free minor left
5b881e3c 739 */
f3d8e878
AU
740static int mei_minor_get(struct mei_device *dev)
741{
742 int ret;
743
744 mutex_lock(&mei_minor_lock);
745 ret = idr_alloc(&mei_idr, dev, 0, MEI_MAX_DEVS, GFP_KERNEL);
746 if (ret >= 0)
747 dev->minor = ret;
748 else if (ret == -ENOSPC)
2bf94cab 749 dev_err(dev->dev, "too many mei devices\n");
5b881e3c 750
f3d8e878
AU
751 mutex_unlock(&mei_minor_lock);
752 return ret;
753}
30e53bb8 754
f3d8e878
AU
755/**
756 * mei_minor_free - mark device minor number as free
757 *
758 * @dev: device pointer
759 */
760static void mei_minor_free(struct mei_device *dev)
9a123f19 761{
f3d8e878
AU
762 mutex_lock(&mei_minor_lock);
763 idr_remove(&mei_idr, dev->minor);
764 mutex_unlock(&mei_minor_lock);
765}
766
767int mei_register(struct mei_device *dev, struct device *parent)
768{
769 struct device *clsdev; /* class device */
770 int ret, devno;
771
772 ret = mei_minor_get(dev);
773 if (ret < 0)
30e53bb8
TW
774 return ret;
775
f3d8e878
AU
776 /* Fill in the data structures */
777 devno = MKDEV(MAJOR(mei_devt), dev->minor);
778 cdev_init(&dev->cdev, &mei_fops);
154322f4 779 dev->cdev.owner = parent->driver->owner;
f3d8e878
AU
780
781 /* Add the device */
782 ret = cdev_add(&dev->cdev, devno, 1);
783 if (ret) {
784 dev_err(parent, "unable to add device %d:%d\n",
785 MAJOR(mei_devt), dev->minor);
786 goto err_dev_add;
787 }
788
55c4e640
TW
789 clsdev = device_create_with_groups(mei_class, parent, devno,
790 dev, mei_groups,
791 "mei%d", dev->minor);
f3d8e878
AU
792
793 if (IS_ERR(clsdev)) {
794 dev_err(parent, "unable to create device %d:%d\n",
795 MAJOR(mei_devt), dev->minor);
796 ret = PTR_ERR(clsdev);
797 goto err_dev_create;
798 }
799
800 ret = mei_dbgfs_register(dev, dev_name(clsdev));
801 if (ret) {
802 dev_err(clsdev, "cannot register debugfs ret = %d\n", ret);
803 goto err_dev_dbgfs;
804 }
30e53bb8
TW
805
806 return 0;
f3d8e878
AU
807
808err_dev_dbgfs:
809 device_destroy(mei_class, devno);
810err_dev_create:
811 cdev_del(&dev->cdev);
812err_dev_add:
813 mei_minor_free(dev);
814 return ret;
5b881e3c 815}
40e0b67b 816EXPORT_SYMBOL_GPL(mei_register);
5b881e3c 817
30e53bb8 818void mei_deregister(struct mei_device *dev)
5b881e3c 819{
f3d8e878
AU
820 int devno;
821
822 devno = dev->cdev.dev;
823 cdev_del(&dev->cdev);
824
30e53bb8 825 mei_dbgfs_deregister(dev);
f3d8e878
AU
826
827 device_destroy(mei_class, devno);
828
829 mei_minor_free(dev);
5b881e3c 830}
40e0b67b 831EXPORT_SYMBOL_GPL(mei_deregister);
ab841160 832
cf3baefb
SO
833static int __init mei_init(void)
834{
f3d8e878
AU
835 int ret;
836
837 mei_class = class_create(THIS_MODULE, "mei");
838 if (IS_ERR(mei_class)) {
839 pr_err("couldn't create class\n");
840 ret = PTR_ERR(mei_class);
841 goto err;
842 }
843
844 ret = alloc_chrdev_region(&mei_devt, 0, MEI_MAX_DEVS, "mei");
845 if (ret < 0) {
846 pr_err("unable to allocate char dev region\n");
847 goto err_class;
848 }
849
850 ret = mei_cl_bus_init();
851 if (ret < 0) {
852 pr_err("unable to initialize bus\n");
853 goto err_chrdev;
854 }
855
856 return 0;
857
858err_chrdev:
859 unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
860err_class:
861 class_destroy(mei_class);
862err:
863 return ret;
cf3baefb
SO
864}
865
866static void __exit mei_exit(void)
867{
f3d8e878
AU
868 unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
869 class_destroy(mei_class);
cf3baefb
SO
870 mei_cl_bus_exit();
871}
872
873module_init(mei_init);
874module_exit(mei_exit);
875
40e0b67b
TW
876MODULE_AUTHOR("Intel Corporation");
877MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
827eef51 878MODULE_LICENSE("GPL v2");
ab841160 879