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