usbip: stub: stop printing kernel pointer addresses in messages
[pandora-kernel.git] / drivers / staging / mei / main.c
1 /*
2  *
3  * Intel Management Engine Interface (Intel MEI) Linux driver
4  * Copyright (c) 2003-2011, 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
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/kernel.h>
20 #include <linux/device.h>
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>
26 #include <linux/pci.h>
27 #include <linux/poll.h>
28 #include <linux/init.h>
29 #include <linux/ioctl.h>
30 #include <linux/cdev.h>
31 #include <linux/sched.h>
32 #include <linux/uuid.h>
33 #include <linux/compat.h>
34 #include <linux/jiffies.h>
35 #include <linux/interrupt.h>
36
37 #include "mei_dev.h"
38 #include "mei.h"
39 #include "interface.h"
40 #include "mei_version.h"
41
42
43 #define MEI_READ_TIMEOUT 45
44 #define MEI_DRIVER_NAME "mei"
45 #define MEI_DEV_NAME "mei"
46
47 /*
48  *  mei driver strings
49  */
50 static char mei_driver_name[] = MEI_DRIVER_NAME;
51 static const char mei_driver_string[] = "Intel(R) Management Engine Interface";
52 static const char mei_driver_version[] = MEI_DRIVER_VERSION;
53
54 /* mei char device for registration */
55 static struct cdev mei_cdev;
56
57 /* major number for device */
58 static int mei_major;
59 /* The device pointer */
60 /* Currently this driver works as long as there is only a single AMT device. */
61 struct pci_dev *mei_device;
62
63 static struct class *mei_class;
64
65
66 /* mei_pci_tbl - PCI Device ID Table */
67 static DEFINE_PCI_DEVICE_TABLE(mei_pci_tbl) = {
68         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82946GZ)},
69         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G35)},
70         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82Q965)},
71         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G965)},
72         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GM965)},
73         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GME965)},
74         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q35)},
75         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82G33)},
76         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q33)},
77         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82X38)},
78         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_3200)},
79         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_6)},
80         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_7)},
81         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_8)},
82         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_9)},
83         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_10)},
84         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_1)},
85         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_2)},
86         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_3)},
87         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_4)},
88         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_1)},
89         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_2)},
90         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_3)},
91         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_4)},
92         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_1)},
93         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_2)},
94         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_CPT_1)},
95         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PBG_1)},
96         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_1)},
97         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_2)},
98         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_3)},
99
100         /* required last entry */
101         {0, }
102 };
103
104 MODULE_DEVICE_TABLE(pci, mei_pci_tbl);
105
106 static DEFINE_MUTEX(mei_mutex);
107
108 /**
109  * mei_quirk_probe - probe for devices that doesn't valid ME interface
110  * @pdev: PCI device structure
111  * @ent: entry into pci_device_table
112  *
113  * returns true if ME Interface is valid, false otherwise
114  */
115 static bool __devinit mei_quirk_probe(struct pci_dev *pdev,
116                                 const struct pci_device_id *ent)
117 {
118         u32 reg;
119         if (ent->device == MEI_DEV_ID_PBG_1) {
120                 pci_read_config_dword(pdev, 0x48, &reg);
121                 /* make sure that bit 9 is up and bit 10 is down */
122                 if ((reg & 0x600) == 0x200) {
123                         dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
124                         return false;
125                 }
126         }
127         return true;
128 }
129 /**
130  * mei_probe - Device Initialization Routine
131  *
132  * @pdev: PCI device structure
133  * @ent: entry in kcs_pci_tbl
134  *
135  * returns 0 on success, <0 on failure.
136  */
137 static int __devinit mei_probe(struct pci_dev *pdev,
138                                 const struct pci_device_id *ent)
139 {
140         struct mei_device *dev;
141         int err;
142
143         mutex_lock(&mei_mutex);
144
145         if (!mei_quirk_probe(pdev, ent)) {
146                 err = -ENODEV;
147                 goto end;
148         }
149
150         if (mei_device) {
151                 err = -EEXIST;
152                 goto end;
153         }
154         /* enable pci dev */
155         err = pci_enable_device(pdev);
156         if (err) {
157                 printk(KERN_ERR "mei: Failed to enable pci device.\n");
158                 goto end;
159         }
160         /* set PCI host mastering  */
161         pci_set_master(pdev);
162         /* pci request regions for mei driver */
163         err = pci_request_regions(pdev, mei_driver_name);
164         if (err) {
165                 printk(KERN_ERR "mei: Failed to get pci regions.\n");
166                 goto disable_device;
167         }
168         /* allocates and initializes the mei dev structure */
169         dev = mei_device_init(pdev);
170         if (!dev) {
171                 err = -ENOMEM;
172                 goto release_regions;
173         }
174         /* mapping  IO device memory */
175         dev->mem_addr = pci_iomap(pdev, 0, 0);
176         if (!dev->mem_addr) {
177                 printk(KERN_ERR "mei: mapping I/O device memory failure.\n");
178                 err = -ENOMEM;
179                 goto free_device;
180         }
181         pci_enable_msi(pdev);
182
183          /* request and enable interrupt */
184         if (pci_dev_msi_enabled(pdev))
185                 err = request_threaded_irq(pdev->irq,
186                         NULL,
187                         mei_interrupt_thread_handler,
188                         0, mei_driver_name, dev);
189         else
190                 err = request_threaded_irq(pdev->irq,
191                         mei_interrupt_quick_handler,
192                         mei_interrupt_thread_handler,
193                         IRQF_SHARED, mei_driver_name, dev);
194
195         if (err) {
196                 printk(KERN_ERR "mei: request_threaded_irq failure. irq = %d\n",
197                        pdev->irq);
198                 goto unmap_memory;
199         }
200         INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
201         if (mei_hw_init(dev)) {
202                 printk(KERN_ERR "mei: Init hw failure.\n");
203                 err = -ENODEV;
204                 goto release_irq;
205         }
206         mei_device = pdev;
207         pci_set_drvdata(pdev, dev);
208         schedule_delayed_work(&dev->timer_work, HZ);
209
210         mutex_unlock(&mei_mutex);
211
212         pr_debug("mei: Driver initialization successful.\n");
213
214         return 0;
215
216 release_irq:
217         /* disable interrupts */
218         dev->host_hw_state = mei_hcsr_read(dev);
219         mei_disable_interrupts(dev);
220         flush_scheduled_work();
221         free_irq(pdev->irq, dev);
222         pci_disable_msi(pdev);
223 unmap_memory:
224         pci_iounmap(pdev, dev->mem_addr);
225 free_device:
226         kfree(dev);
227 release_regions:
228         pci_release_regions(pdev);
229 disable_device:
230         pci_disable_device(pdev);
231 end:
232         mutex_unlock(&mei_mutex);
233         printk(KERN_ERR "mei: Driver initialization failed.\n");
234         return err;
235 }
236
237 /**
238  * mei_remove - Device Removal Routine
239  *
240  * @pdev: PCI device structure
241  *
242  * mei_remove is called by the PCI subsystem to alert the driver
243  * that it should release a PCI device.
244  */
245 static void __devexit mei_remove(struct pci_dev *pdev)
246 {
247         struct mei_device *dev;
248
249         if (mei_device != pdev)
250                 return;
251
252         dev = pci_get_drvdata(pdev);
253         if (!dev)
254                 return;
255
256         mutex_lock(&dev->device_lock);
257
258         mei_wd_stop(dev, false);
259
260         mei_device = NULL;
261
262         if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) {
263                 dev->iamthif_cl.state = MEI_FILE_DISCONNECTING;
264                 mei_disconnect_host_client(dev, &dev->iamthif_cl);
265         }
266         if (dev->wd_cl.state == MEI_FILE_CONNECTED) {
267                 dev->wd_cl.state = MEI_FILE_DISCONNECTING;
268                 mei_disconnect_host_client(dev, &dev->wd_cl);
269         }
270
271         /* Unregistering watchdog device */
272         if (dev->wd_interface_reg)
273                 watchdog_unregister_device(&amt_wd_dev);
274
275         /* remove entry if already in list */
276         dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
277         mei_remove_client_from_file_list(dev, dev->wd_cl.host_client_id);
278         mei_remove_client_from_file_list(dev, dev->iamthif_cl.host_client_id);
279
280         dev->iamthif_current_cb = NULL;
281         dev->me_clients_num = 0;
282
283         mutex_unlock(&dev->device_lock);
284
285         flush_scheduled_work();
286
287         /* disable interrupts */
288         mei_disable_interrupts(dev);
289
290         free_irq(pdev->irq, dev);
291         pci_disable_msi(pdev);
292         pci_set_drvdata(pdev, NULL);
293
294         if (dev->mem_addr)
295                 pci_iounmap(pdev, dev->mem_addr);
296
297         kfree(dev);
298
299         pci_release_regions(pdev);
300         pci_disable_device(pdev);
301 }
302
303 /**
304  * mei_clear_list - removes all callbacks associated with file
305  *              from mei_cb_list
306  *
307  * @dev: device structure.
308  * @file: file structure
309  * @mei_cb_list: callbacks list
310  *
311  * mei_clear_list is called to clear resources associated with file
312  * when application calls close function or Ctrl-C was pressed
313  *
314  * returns true if callback removed from the list, false otherwise
315  */
316 static bool mei_clear_list(struct mei_device *dev,
317                 struct file *file, struct list_head *mei_cb_list)
318 {
319         struct mei_cl_cb *cb_pos = NULL;
320         struct mei_cl_cb *cb_next = NULL;
321         struct file *file_temp;
322         bool removed = false;
323
324         /* list all list member */
325         list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, cb_list) {
326                 file_temp = (struct file *)cb_pos->file_object;
327                 /* check if list member associated with a file */
328                 if (file_temp == file) {
329                         /* remove member from the list */
330                         list_del(&cb_pos->cb_list);
331                         /* check if cb equal to current iamthif cb */
332                         if (dev->iamthif_current_cb == cb_pos) {
333                                 dev->iamthif_current_cb = NULL;
334                                 /* send flow control to iamthif client */
335                                 mei_send_flow_control(dev, &dev->iamthif_cl);
336                         }
337                         /* free all allocated buffers */
338                         mei_free_cb_private(cb_pos);
339                         cb_pos = NULL;
340                         removed = true;
341                 }
342         }
343         return removed;
344 }
345
346 /**
347  * mei_clear_lists - removes all callbacks associated with file
348  *
349  * @dev: device structure
350  * @file: file structure
351  *
352  * mei_clear_lists is called to clear resources associated with file
353  * when application calls close function or Ctrl-C was pressed
354  *
355  * returns true if callback removed from the list, false otherwise
356  */
357 static bool mei_clear_lists(struct mei_device *dev, struct file *file)
358 {
359         bool removed = false;
360
361         /* remove callbacks associated with a file */
362         mei_clear_list(dev, file, &dev->amthi_cmd_list.mei_cb.cb_list);
363         if (mei_clear_list(dev, file,
364                             &dev->amthi_read_complete_list.mei_cb.cb_list))
365                 removed = true;
366
367         mei_clear_list(dev, file, &dev->ctrl_rd_list.mei_cb.cb_list);
368
369         if (mei_clear_list(dev, file, &dev->ctrl_wr_list.mei_cb.cb_list))
370                 removed = true;
371
372         if (mei_clear_list(dev, file, &dev->write_waiting_list.mei_cb.cb_list))
373                 removed = true;
374
375         if (mei_clear_list(dev, file, &dev->write_list.mei_cb.cb_list))
376                 removed = true;
377
378         /* check if iamthif_current_cb not NULL */
379         if (dev->iamthif_current_cb && !removed) {
380                 /* check file and iamthif current cb association */
381                 if (dev->iamthif_current_cb->file_object == file) {
382                         /* remove cb */
383                         mei_free_cb_private(dev->iamthif_current_cb);
384                         dev->iamthif_current_cb = NULL;
385                         removed = true;
386                 }
387         }
388         return removed;
389 }
390 /**
391  * find_read_list_entry - find read list entry
392  *
393  * @dev: device structure
394  * @file: pointer to file structure
395  *
396  * returns cb on success, NULL on error
397  */
398 static struct mei_cl_cb *find_read_list_entry(
399                 struct mei_device *dev,
400                 struct mei_cl *cl)
401 {
402         struct mei_cl_cb *cb_pos = NULL;
403         struct mei_cl_cb *cb_next = NULL;
404
405         if (!dev->read_list.status &&
406             !list_empty(&dev->read_list.mei_cb.cb_list)) {
407
408                 dev_dbg(&dev->pdev->dev, "remove read_list CB\n");
409                 list_for_each_entry_safe(cb_pos, cb_next,
410                                 &dev->read_list.mei_cb.cb_list, cb_list) {
411                         struct mei_cl *cl_temp;
412                         cl_temp = (struct mei_cl *)cb_pos->file_private;
413
414                         if (mei_cl_cmp_id(cl, cl_temp))
415                                 return cb_pos;
416                 }
417         }
418         return NULL;
419 }
420
421 /**
422  * mei_open - the open function
423  *
424  * @inode: pointer to inode structure
425  * @file: pointer to file structure
426  *
427  * returns 0 on success, <0 on error
428  */
429 static int mei_open(struct inode *inode, struct file *file)
430 {
431         struct mei_cl *cl;
432         int if_num = iminor(inode), err;
433         struct mei_device *dev;
434
435         err = -ENODEV;
436         if (!mei_device)
437                 goto out;
438
439         dev = pci_get_drvdata(mei_device);
440         if (if_num != MEI_MINOR_NUMBER || !dev)
441                 goto out;
442
443         mutex_lock(&dev->device_lock);
444         err = -ENOMEM;
445         cl = mei_cl_allocate(dev);
446         if (!cl)
447                 goto out_unlock;
448
449         err = -ENODEV;
450         if (dev->mei_state != MEI_ENABLED) {
451                 dev_dbg(&dev->pdev->dev, "mei_state != MEI_ENABLED  mei_state= %d\n",
452                     dev->mei_state);
453                 goto out_unlock;
454         }
455         err = -EMFILE;
456         if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT)
457                 goto out_unlock;
458
459         cl->host_client_id = find_first_zero_bit(dev->host_clients_map,
460                                                         MEI_CLIENTS_MAX);
461         if (cl->host_client_id > MEI_CLIENTS_MAX)
462                 goto out_unlock;
463
464         dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id);
465
466         dev->open_handle_count++;
467         list_add_tail(&cl->link, &dev->file_list);
468
469         set_bit(cl->host_client_id, dev->host_clients_map);
470         cl->state = MEI_FILE_INITIALIZING;
471         cl->sm_state = 0;
472
473         file->private_data = cl;
474         mutex_unlock(&dev->device_lock);
475
476         return 0;
477
478 out_unlock:
479         mutex_unlock(&dev->device_lock);
480         kfree(cl);
481 out:
482         return err;
483 }
484
485 /**
486  * mei_release - the release function
487  *
488  * @inode: pointer to inode structure
489  * @file: pointer to file structure
490  *
491  * returns 0 on success, <0 on error
492  */
493 static int mei_release(struct inode *inode, struct file *file)
494 {
495         struct mei_cl *cl = file->private_data;
496         struct mei_cl_cb *cb;
497         struct mei_device *dev;
498         int rets = 0;
499
500         if (WARN_ON(!cl || !cl->dev))
501                 return -ENODEV;
502
503         dev = cl->dev;
504
505         mutex_lock(&dev->device_lock);
506         if (cl != &dev->iamthif_cl) {
507                 if (cl->state == MEI_FILE_CONNECTED) {
508                         cl->state = MEI_FILE_DISCONNECTING;
509                         dev_dbg(&dev->pdev->dev,
510                                 "disconnecting client host client = %d, "
511                             "ME client = %d\n",
512                             cl->host_client_id,
513                             cl->me_client_id);
514                         rets = mei_disconnect_host_client(dev, cl);
515                 }
516                 mei_cl_flush_queues(cl);
517                 dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
518                     cl->host_client_id,
519                     cl->me_client_id);
520
521                 if (dev->open_handle_count > 0) {
522                         clear_bit(cl->host_client_id,
523                                   dev->host_clients_map);
524                         dev->open_handle_count--;
525                 }
526                 mei_remove_client_from_file_list(dev, cl->host_client_id);
527
528                 /* free read cb */
529                 cb = NULL;
530                 if (cl->read_cb) {
531                         cb = find_read_list_entry(dev, cl);
532                         /* Remove entry from read list */
533                         if (cb)
534                                 list_del(&cb->cb_list);
535
536                         cb = cl->read_cb;
537                         cl->read_cb = NULL;
538                 }
539
540                 file->private_data = NULL;
541
542                 if (cb) {
543                         mei_free_cb_private(cb);
544                         cb = NULL;
545                 }
546
547                 kfree(cl);
548         } else {
549                 if (dev->open_handle_count > 0)
550                         dev->open_handle_count--;
551
552                 if (dev->iamthif_file_object == file &&
553                     dev->iamthif_state != MEI_IAMTHIF_IDLE) {
554
555                         dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n",
556                             dev->iamthif_state);
557                         dev->iamthif_canceled = true;
558                         if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
559                                 dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n");
560                                 mei_run_next_iamthif_cmd(dev);
561                         }
562                 }
563
564                 if (mei_clear_lists(dev, file))
565                         dev->iamthif_state = MEI_IAMTHIF_IDLE;
566
567         }
568         mutex_unlock(&dev->device_lock);
569         return rets;
570 }
571
572
573 /**
574  * mei_read - the read function.
575  *
576  * @file: pointer to file structure
577  * @ubuf: pointer to user buffer
578  * @length: buffer length
579  * @offset: data offset in buffer
580  *
581  * returns >=0 data length on success , <0 on error
582  */
583 static ssize_t mei_read(struct file *file, char __user *ubuf,
584                          size_t length, loff_t *offset)
585 {
586         struct mei_cl *cl = file->private_data;
587         struct mei_cl_cb *cb_pos = NULL;
588         struct mei_cl_cb *cb = NULL;
589         struct mei_device *dev;
590         int i;
591         int rets;
592         int err;
593
594
595         if (WARN_ON(!cl || !cl->dev))
596                 return -ENODEV;
597
598         dev = cl->dev;
599
600         mutex_lock(&dev->device_lock);
601         if (dev->mei_state != MEI_ENABLED) {
602                 rets = -ENODEV;
603                 goto out;
604         }
605
606         if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) {
607                 /* Do not allow to read watchdog client */
608                 i = mei_find_me_client_index(dev, mei_wd_guid);
609                 if (i >= 0) {
610                         struct mei_me_client *me_client = &dev->me_clients[i];
611
612                         if (cl->me_client_id == me_client->client_id) {
613                                 rets = -EBADF;
614                                 goto out;
615                         }
616                 }
617         } else {
618                 cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
619         }
620
621         if (cl == &dev->iamthif_cl) {
622                 rets = amthi_read(dev, file, ubuf, length, offset);
623                 goto out;
624         }
625
626         if (cl->read_cb && cl->read_cb->information > *offset) {
627                 cb = cl->read_cb;
628                 goto copy_buffer;
629         } else if (cl->read_cb && cl->read_cb->information > 0 &&
630                    cl->read_cb->information <= *offset) {
631                 cb = cl->read_cb;
632                 rets = 0;
633                 goto free;
634         } else if ((!cl->read_cb || !cl->read_cb->information) &&
635                     *offset > 0) {
636                 /*Offset needs to be cleaned for contingous reads*/
637                 *offset = 0;
638                 rets = 0;
639                 goto out;
640         }
641
642         err = mei_start_read(dev, cl);
643         if (err && err != -EBUSY) {
644                 dev_dbg(&dev->pdev->dev,
645                         "mei start read failure with status = %d\n", err);
646                 rets = err;
647                 goto out;
648         }
649
650         if (MEI_READ_COMPLETE != cl->reading_state &&
651                         !waitqueue_active(&cl->rx_wait)) {
652                 if (file->f_flags & O_NONBLOCK) {
653                         rets = -EAGAIN;
654                         goto out;
655                 }
656
657                 mutex_unlock(&dev->device_lock);
658
659                 if (wait_event_interruptible(cl->rx_wait,
660                         (MEI_READ_COMPLETE == cl->reading_state ||
661                          MEI_FILE_INITIALIZING == cl->state ||
662                          MEI_FILE_DISCONNECTED == cl->state ||
663                          MEI_FILE_DISCONNECTING == cl->state))) {
664                         if (signal_pending(current))
665                                 return -EINTR;
666                         return -ERESTARTSYS;
667                 }
668
669                 mutex_lock(&dev->device_lock);
670                 if (MEI_FILE_INITIALIZING == cl->state ||
671                     MEI_FILE_DISCONNECTED == cl->state ||
672                     MEI_FILE_DISCONNECTING == cl->state) {
673                         rets = -EBUSY;
674                         goto out;
675                 }
676         }
677
678         cb = cl->read_cb;
679
680         if (!cb) {
681                 rets = -ENODEV;
682                 goto out;
683         }
684         if (cl->reading_state != MEI_READ_COMPLETE) {
685                 rets = 0;
686                 goto out;
687         }
688         /* now copy the data to user space */
689 copy_buffer:
690         dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n",
691             cb->response_buffer.size);
692         dev_dbg(&dev->pdev->dev, "cb->information - %lu\n",
693             cb->information);
694         if (length == 0 || ubuf == NULL || *offset > cb->information) {
695                 rets = -EMSGSIZE;
696                 goto free;
697         }
698
699         /* length is being turncated to PAGE_SIZE, however, */
700         /* information size may be longer */
701         length = min_t(size_t, length, (cb->information - *offset));
702
703         if (copy_to_user(ubuf,
704                          cb->response_buffer.data + *offset,
705                          length)) {
706                 rets = -EFAULT;
707                 goto free;
708         }
709
710         rets = length;
711         *offset += length;
712         if ((unsigned long)*offset < cb->information)
713                 goto out;
714
715 free:
716         cb_pos = find_read_list_entry(dev, cl);
717         /* Remove entry from read list */
718         if (cb_pos)
719                 list_del(&cb_pos->cb_list);
720         mei_free_cb_private(cb);
721         cl->reading_state = MEI_IDLE;
722         cl->read_cb = NULL;
723         cl->read_pending = 0;
724 out:
725         dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
726         mutex_unlock(&dev->device_lock);
727         return rets;
728 }
729
730 /**
731  * mei_write - the write function.
732  *
733  * @file: pointer to file structure
734  * @ubuf: pointer to user buffer
735  * @length: buffer length
736  * @offset: data offset in buffer
737  *
738  * returns >=0 data length on success , <0 on error
739  */
740 static ssize_t mei_write(struct file *file, const char __user *ubuf,
741                           size_t length, loff_t *offset)
742 {
743         struct mei_cl *cl = file->private_data;
744         struct mei_cl_cb *write_cb = NULL;
745         struct mei_msg_hdr mei_hdr;
746         struct mei_device *dev;
747         unsigned long timeout = 0;
748         int rets;
749         int i;
750
751         if (WARN_ON(!cl || !cl->dev))
752                 return -ENODEV;
753
754         dev = cl->dev;
755
756         mutex_lock(&dev->device_lock);
757
758         if (dev->mei_state != MEI_ENABLED) {
759                 mutex_unlock(&dev->device_lock);
760                 return -ENODEV;
761         }
762
763         if (cl == &dev->iamthif_cl) {
764                 write_cb = find_amthi_read_list_entry(dev, file);
765
766                 if (write_cb) {
767                         timeout = write_cb->read_time +
768                                         msecs_to_jiffies(IAMTHIF_READ_TIMER);
769
770                         if (time_after(jiffies, timeout) ||
771                                  cl->reading_state == MEI_READ_COMPLETE) {
772                                         *offset = 0;
773                                         list_del(&write_cb->cb_list);
774                                         mei_free_cb_private(write_cb);
775                                         write_cb = NULL;
776                         }
777                 }
778         }
779
780         /* free entry used in read */
781         if (cl->reading_state == MEI_READ_COMPLETE) {
782                 *offset = 0;
783                 write_cb = find_read_list_entry(dev, cl);
784                 if (write_cb) {
785                         list_del(&write_cb->cb_list);
786                         mei_free_cb_private(write_cb);
787                         write_cb = NULL;
788                         cl->reading_state = MEI_IDLE;
789                         cl->read_cb = NULL;
790                         cl->read_pending = 0;
791                 }
792         } else if (cl->reading_state == MEI_IDLE &&
793                    !cl->read_pending)
794                 *offset = 0;
795
796
797         write_cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
798         if (!write_cb) {
799                 mutex_unlock(&dev->device_lock);
800                 return -ENOMEM;
801         }
802
803         write_cb->file_object = file;
804         write_cb->file_private = cl;
805         write_cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
806         rets = -ENOMEM;
807         if (!write_cb->request_buffer.data)
808                 goto unlock_dev;
809
810         dev_dbg(&dev->pdev->dev, "length =%d\n", (int) length);
811
812         rets = -EFAULT;
813         if (copy_from_user(write_cb->request_buffer.data, ubuf, length))
814                 goto unlock_dev;
815
816         cl->sm_state = 0;
817         if (length == 4 &&
818             ((memcmp(mei_wd_state_independence_msg[0],
819                                  write_cb->request_buffer.data, 4) == 0) ||
820              (memcmp(mei_wd_state_independence_msg[1],
821                                  write_cb->request_buffer.data, 4) == 0) ||
822              (memcmp(mei_wd_state_independence_msg[2],
823                                  write_cb->request_buffer.data, 4) == 0)))
824                 cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
825
826         INIT_LIST_HEAD(&write_cb->cb_list);
827         if (cl == &dev->iamthif_cl) {
828                 write_cb->response_buffer.data =
829                     kmalloc(dev->iamthif_mtu, GFP_KERNEL);
830                 if (!write_cb->response_buffer.data) {
831                         rets = -ENOMEM;
832                         goto unlock_dev;
833                 }
834                 if (dev->mei_state != MEI_ENABLED) {
835                         rets = -ENODEV;
836                         goto unlock_dev;
837                 }
838                 for (i = 0; i < dev->me_clients_num; i++) {
839                         if (dev->me_clients[i].client_id ==
840                                 dev->iamthif_cl.me_client_id)
841                                 break;
842                 }
843
844                 if (WARN_ON(dev->me_clients[i].client_id != cl->me_client_id)) {
845                         rets = -ENODEV;
846                         goto unlock_dev;
847                 }
848                 if (i == dev->me_clients_num ||
849                     (dev->me_clients[i].client_id !=
850                       dev->iamthif_cl.me_client_id)) {
851                         rets = -ENODEV;
852                         goto unlock_dev;
853                 } else if (length > dev->me_clients[i].props.max_msg_length ||
854                            length <= 0) {
855                         rets = -EMSGSIZE;
856                         goto unlock_dev;
857                 }
858
859                 write_cb->response_buffer.size = dev->iamthif_mtu;
860                 write_cb->major_file_operations = MEI_IOCTL;
861                 write_cb->information = 0;
862                 write_cb->request_buffer.size = length;
863                 if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
864                         rets = -ENODEV;
865                         goto unlock_dev;
866                 }
867
868                 if (!list_empty(&dev->amthi_cmd_list.mei_cb.cb_list) ||
869                                 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
870                         dev_dbg(&dev->pdev->dev, "amthi_state = %d\n",
871                                         (int) dev->iamthif_state);
872                         dev_dbg(&dev->pdev->dev, "add amthi cb to amthi cmd waiting list\n");
873                         list_add_tail(&write_cb->cb_list,
874                                         &dev->amthi_cmd_list.mei_cb.cb_list);
875                         rets = length;
876                 } else {
877                         dev_dbg(&dev->pdev->dev, "call amthi write\n");
878                         rets = amthi_write(dev, write_cb);
879
880                         if (rets) {
881                                 dev_dbg(&dev->pdev->dev, "amthi write failed with status = %d\n",
882                                     rets);
883                                 goto unlock_dev;
884                         }
885                         rets = length;
886                 }
887                 mutex_unlock(&dev->device_lock);
888                 return rets;
889         }
890
891         write_cb->major_file_operations = MEI_WRITE;
892         /* make sure information is zero before we start */
893
894         write_cb->information = 0;
895         write_cb->request_buffer.size = length;
896
897         dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n",
898             cl->host_client_id, cl->me_client_id);
899         if (cl->state != MEI_FILE_CONNECTED) {
900                 rets = -ENODEV;
901                 dev_dbg(&dev->pdev->dev, "host client = %d,  is not connected to ME client = %d",
902                     cl->host_client_id,
903                     cl->me_client_id);
904                 goto unlock_dev;
905         }
906         for (i = 0; i < dev->me_clients_num; i++) {
907                 if (dev->me_clients[i].client_id ==
908                     cl->me_client_id)
909                         break;
910         }
911         if (WARN_ON(dev->me_clients[i].client_id != cl->me_client_id)) {
912                 rets = -ENODEV;
913                 goto unlock_dev;
914         }
915         if (i == dev->me_clients_num) {
916                 rets = -ENODEV;
917                 goto unlock_dev;
918         }
919         if (length > dev->me_clients[i].props.max_msg_length || length <= 0) {
920                 rets = -EINVAL;
921                 goto unlock_dev;
922         }
923         write_cb->file_private = cl;
924
925         rets = mei_flow_ctrl_creds(dev, cl);
926         if (rets < 0)
927                 goto unlock_dev;
928
929         if (rets && dev->mei_host_buffer_is_empty) {
930                 rets = 0;
931                 dev->mei_host_buffer_is_empty = false;
932                 if (length > ((((dev->host_hw_state & H_CBD) >> 24) *
933                         sizeof(u32)) - sizeof(struct mei_msg_hdr))) {
934
935                         mei_hdr.length =
936                                 (((dev->host_hw_state & H_CBD) >> 24) *
937                                 sizeof(u32)) -
938                                 sizeof(struct mei_msg_hdr);
939                         mei_hdr.msg_complete = 0;
940                 } else {
941                         mei_hdr.length = length;
942                         mei_hdr.msg_complete = 1;
943                 }
944                 mei_hdr.host_addr = cl->host_client_id;
945                 mei_hdr.me_addr = cl->me_client_id;
946                 mei_hdr.reserved = 0;
947                 dev_dbg(&dev->pdev->dev, "call mei_write_message header=%08x.\n",
948                     *((u32 *) &mei_hdr));
949                 if (!mei_write_message(dev, &mei_hdr,
950                         (unsigned char *) (write_cb->request_buffer.data),
951                         mei_hdr.length)) {
952                         rets = -ENODEV;
953                         goto unlock_dev;
954                 }
955                 cl->writing_state = MEI_WRITING;
956                 write_cb->information = mei_hdr.length;
957                 if (mei_hdr.msg_complete) {
958                         if (mei_flow_ctrl_reduce(dev, cl)) {
959                                 rets = -ENODEV;
960                                 goto unlock_dev;
961                         }
962                         list_add_tail(&write_cb->cb_list,
963                                       &dev->write_waiting_list.mei_cb.cb_list);
964                 } else {
965                         list_add_tail(&write_cb->cb_list,
966                                       &dev->write_list.mei_cb.cb_list);
967                 }
968
969         } else {
970
971                 write_cb->information = 0;
972                 cl->writing_state = MEI_WRITING;
973                 list_add_tail(&write_cb->cb_list,
974                               &dev->write_list.mei_cb.cb_list);
975         }
976         mutex_unlock(&dev->device_lock);
977         return length;
978
979 unlock_dev:
980         mutex_unlock(&dev->device_lock);
981         mei_free_cb_private(write_cb);
982         return rets;
983 }
984
985
986 /**
987  * mei_ioctl - the IOCTL function
988  *
989  * @file: pointer to file structure
990  * @cmd: ioctl command
991  * @data: pointer to mei message structure
992  *
993  * returns 0 on success , <0 on error
994  */
995 static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
996 {
997         struct mei_device *dev;
998         struct mei_cl *cl = file->private_data;
999         struct mei_connect_client_data *connect_data = NULL;
1000         int rets;
1001
1002         if (cmd != IOCTL_MEI_CONNECT_CLIENT)
1003                 return -EINVAL;
1004
1005         if (WARN_ON(!cl || !cl->dev))
1006                 return -ENODEV;
1007
1008         dev = cl->dev;
1009
1010         dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
1011
1012         mutex_lock(&dev->device_lock);
1013         if (dev->mei_state != MEI_ENABLED) {
1014                 rets = -ENODEV;
1015                 goto out;
1016         }
1017
1018         dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
1019
1020         connect_data = kzalloc(sizeof(struct mei_connect_client_data),
1021                                                         GFP_KERNEL);
1022         if (!connect_data) {
1023                 rets = -ENOMEM;
1024                 goto out;
1025         }
1026         dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
1027         if (copy_from_user(connect_data, (char __user *)data,
1028                                 sizeof(struct mei_connect_client_data))) {
1029                 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
1030                 rets = -EFAULT;
1031                 goto out;
1032         }
1033         rets = mei_ioctl_connect_client(file, connect_data);
1034
1035         /* if all is ok, copying the data back to user. */
1036         if (rets)
1037                 goto out;
1038
1039         dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
1040         if (copy_to_user((char __user *)data, connect_data,
1041                                 sizeof(struct mei_connect_client_data))) {
1042                 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
1043                 rets = -EFAULT;
1044                 goto out;
1045         }
1046
1047 out:
1048         kfree(connect_data);
1049         mutex_unlock(&dev->device_lock);
1050         return rets;
1051 }
1052
1053 /**
1054  * mei_compat_ioctl - the compat IOCTL function
1055  *
1056  * @file: pointer to file structure
1057  * @cmd: ioctl command
1058  * @data: pointer to mei message structure
1059  *
1060  * returns 0 on success , <0 on error
1061  */
1062 #ifdef CONFIG_COMPAT
1063 static long mei_compat_ioctl(struct file *file,
1064                       unsigned int cmd, unsigned long data)
1065 {
1066         return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
1067 }
1068 #endif
1069
1070
1071 /**
1072  * mei_poll - the poll function
1073  *
1074  * @file: pointer to file structure
1075  * @wait: pointer to poll_table structure
1076  *
1077  * returns poll mask
1078  */
1079 static unsigned int mei_poll(struct file *file, poll_table *wait)
1080 {
1081         struct mei_cl *cl = file->private_data;
1082         struct mei_device *dev;
1083         unsigned int mask = 0;
1084
1085         if (WARN_ON(!cl || !cl->dev))
1086                 return mask;
1087
1088         dev = cl->dev;
1089
1090         mutex_lock(&dev->device_lock);
1091
1092         if (dev->mei_state != MEI_ENABLED)
1093                 goto out;
1094
1095
1096         if (cl == &dev->iamthif_cl) {
1097                 mutex_unlock(&dev->device_lock);
1098                 poll_wait(file, &dev->iamthif_cl.wait, wait);
1099                 mutex_lock(&dev->device_lock);
1100                 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
1101                         dev->iamthif_file_object == file) {
1102                         mask |= (POLLIN | POLLRDNORM);
1103                         dev_dbg(&dev->pdev->dev, "run next amthi cb\n");
1104                         mei_run_next_iamthif_cmd(dev);
1105                 }
1106                 goto out;
1107         }
1108
1109         mutex_unlock(&dev->device_lock);
1110         poll_wait(file, &cl->tx_wait, wait);
1111         mutex_lock(&dev->device_lock);
1112         if (MEI_WRITE_COMPLETE == cl->writing_state)
1113                 mask |= (POLLIN | POLLRDNORM);
1114
1115 out:
1116         mutex_unlock(&dev->device_lock);
1117         return mask;
1118 }
1119
1120 #ifdef CONFIG_PM
1121 static int mei_pci_suspend(struct device *device)
1122 {
1123         struct pci_dev *pdev = to_pci_dev(device);
1124         struct mei_device *dev = pci_get_drvdata(pdev);
1125         int err;
1126
1127         if (!dev)
1128                 return -ENODEV;
1129         mutex_lock(&dev->device_lock);
1130         /* Stop watchdog if exists */
1131         err = mei_wd_stop(dev, true);
1132         /* Set new mei state */
1133         if (dev->mei_state == MEI_ENABLED ||
1134             dev->mei_state == MEI_RECOVERING_FROM_RESET) {
1135                 dev->mei_state = MEI_POWER_DOWN;
1136                 mei_reset(dev, 0);
1137         }
1138         mutex_unlock(&dev->device_lock);
1139
1140         free_irq(pdev->irq, dev);
1141         pci_disable_msi(pdev);
1142
1143         return err;
1144 }
1145
1146 static int mei_pci_resume(struct device *device)
1147 {
1148         struct pci_dev *pdev = to_pci_dev(device);
1149         struct mei_device *dev;
1150         int err;
1151
1152         dev = pci_get_drvdata(pdev);
1153         if (!dev)
1154                 return -ENODEV;
1155
1156         pci_enable_msi(pdev);
1157
1158         /* request and enable interrupt */
1159         if (pci_dev_msi_enabled(pdev))
1160                 err = request_threaded_irq(pdev->irq,
1161                         NULL,
1162                         mei_interrupt_thread_handler,
1163                         0, mei_driver_name, dev);
1164         else
1165                 err = request_threaded_irq(pdev->irq,
1166                         mei_interrupt_quick_handler,
1167                         mei_interrupt_thread_handler,
1168                         IRQF_SHARED, mei_driver_name, dev);
1169
1170         if (err) {
1171                 printk(KERN_ERR "mei: Request_irq failure. irq = %d\n",
1172                        pdev->irq);
1173                 return err;
1174         }
1175
1176         mutex_lock(&dev->device_lock);
1177         dev->mei_state = MEI_POWER_UP;
1178         mei_reset(dev, 1);
1179         mutex_unlock(&dev->device_lock);
1180
1181         /* Start timer if stopped in suspend */
1182         schedule_delayed_work(&dev->timer_work, HZ);
1183
1184         return err;
1185 }
1186 static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
1187 #define MEI_PM_OPS      (&mei_pm_ops)
1188 #else
1189 #define MEI_PM_OPS      NULL
1190 #endif /* CONFIG_PM */
1191 /*
1192  *  PCI driver structure
1193  */
1194 static struct pci_driver mei_driver = {
1195         .name = mei_driver_name,
1196         .id_table = mei_pci_tbl,
1197         .probe = mei_probe,
1198         .remove = __devexit_p(mei_remove),
1199         .shutdown = __devexit_p(mei_remove),
1200         .driver.pm = MEI_PM_OPS,
1201 };
1202
1203 /*
1204  * file operations structure will be used for mei char device.
1205  */
1206 static const struct file_operations mei_fops = {
1207         .owner = THIS_MODULE,
1208         .read = mei_read,
1209         .unlocked_ioctl = mei_ioctl,
1210 #ifdef CONFIG_COMPAT
1211         .compat_ioctl = mei_compat_ioctl,
1212 #endif
1213         .open = mei_open,
1214         .release = mei_release,
1215         .write = mei_write,
1216         .poll = mei_poll,
1217 };
1218
1219 /**
1220  * mei_registration_cdev - sets up the cdev structure for mei device.
1221  *
1222  * @dev: char device struct
1223  * @hminor: minor number for registration char device
1224  * @fops: file operations structure
1225  *
1226  * returns 0 on success, <0 on failure.
1227  */
1228 static int mei_registration_cdev(struct cdev *dev, int hminor,
1229                                   const struct file_operations *fops)
1230 {
1231         int ret, devno = MKDEV(mei_major, hminor);
1232
1233         cdev_init(dev, fops);
1234         dev->owner = THIS_MODULE;
1235         ret = cdev_add(dev, devno, 1);
1236         /* Fail gracefully if need be */
1237         if (ret)
1238                 printk(KERN_ERR "mei: Error %d registering mei device %d\n",
1239                        ret, hminor);
1240         return ret;
1241 }
1242
1243 /**
1244  * mei_register_cdev - registers mei char device
1245  *
1246  * returns 0 on success, <0 on failure.
1247  */
1248 static int mei_register_cdev(void)
1249 {
1250         int ret;
1251         dev_t dev;
1252
1253         /* registration of char devices */
1254         ret = alloc_chrdev_region(&dev, MEI_MINORS_BASE, MEI_MINORS_COUNT,
1255                                   MEI_DRIVER_NAME);
1256         if (ret) {
1257                 printk(KERN_ERR "mei: Error allocating char device region.\n");
1258                 return ret;
1259         }
1260
1261         mei_major = MAJOR(dev);
1262
1263         ret = mei_registration_cdev(&mei_cdev, MEI_MINOR_NUMBER,
1264                                      &mei_fops);
1265         if (ret)
1266                 unregister_chrdev_region(MKDEV(mei_major, MEI_MINORS_BASE),
1267                                          MEI_MINORS_COUNT);
1268
1269         return ret;
1270 }
1271
1272 /**
1273  * mei_unregister_cdev - unregisters mei char device
1274  */
1275 static void mei_unregister_cdev(void)
1276 {
1277         cdev_del(&mei_cdev);
1278         unregister_chrdev_region(MKDEV(mei_major, MEI_MINORS_BASE),
1279                                  MEI_MINORS_COUNT);
1280 }
1281
1282 /**
1283  * mei_sysfs_device_create - adds device entry to sysfs
1284  *
1285  * returns 0 on success, <0 on failure.
1286  */
1287 static int mei_sysfs_device_create(void)
1288 {
1289         struct class *class;
1290         void *tmphdev;
1291         int err;
1292
1293         class = class_create(THIS_MODULE, MEI_DRIVER_NAME);
1294         if (IS_ERR(class)) {
1295                 err = PTR_ERR(class);
1296                 printk(KERN_ERR "mei: Error creating mei class.\n");
1297                 goto err_out;
1298         }
1299
1300         tmphdev = device_create(class, NULL, mei_cdev.dev, NULL,
1301                                         MEI_DEV_NAME);
1302         if (IS_ERR(tmphdev)) {
1303                 err = PTR_ERR(tmphdev);
1304                 goto err_destroy;
1305         }
1306
1307         mei_class = class;
1308         return 0;
1309
1310 err_destroy:
1311         class_destroy(class);
1312 err_out:
1313         return err;
1314 }
1315
1316 /**
1317  * mei_sysfs_device_remove - unregisters the device entry on sysfs
1318  */
1319 static void mei_sysfs_device_remove(void)
1320 {
1321         if (IS_ERR_OR_NULL(mei_class))
1322                 return;
1323
1324         device_destroy(mei_class, mei_cdev.dev);
1325         class_destroy(mei_class);
1326 }
1327
1328 /**
1329  * mei_init_module - Driver Registration Routine
1330  *
1331  * mei_init_module is the first routine called when the driver is
1332  * loaded. All it does is to register with the PCI subsystem.
1333  *
1334  * returns 0 on success, <0 on failure.
1335  */
1336 static int __init mei_init_module(void)
1337 {
1338         int ret;
1339
1340         pr_debug("mei: %s - version %s\n",
1341                 mei_driver_string, mei_driver_version);
1342         /* init pci module */
1343         ret = pci_register_driver(&mei_driver);
1344         if (ret < 0) {
1345                 printk(KERN_ERR "mei: Error registering driver.\n");
1346                 goto end;
1347         }
1348
1349         ret = mei_register_cdev();
1350         if (ret)
1351                 goto unregister_pci;
1352
1353         ret = mei_sysfs_device_create();
1354         if (ret)
1355                 goto unregister_cdev;
1356
1357         return ret;
1358
1359 unregister_cdev:
1360         mei_unregister_cdev();
1361 unregister_pci:
1362         pci_unregister_driver(&mei_driver);
1363 end:
1364         return ret;
1365 }
1366
1367 module_init(mei_init_module);
1368
1369 /**
1370  * mei_exit_module - Driver Exit Cleanup Routine
1371  *
1372  * mei_exit_module is called just before the driver is removed
1373  * from memory.
1374  */
1375 static void __exit mei_exit_module(void)
1376 {
1377         mei_sysfs_device_remove();
1378         mei_unregister_cdev();
1379         pci_unregister_driver(&mei_driver);
1380
1381         pr_debug("mei: Driver unloaded successfully.\n");
1382 }
1383
1384 module_exit(mei_exit_module);
1385
1386
1387 MODULE_AUTHOR("Intel Corporation");
1388 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1389 MODULE_LICENSE("GPL v2");
1390 MODULE_VERSION(MEI_DRIVER_VERSION);