Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Intel Management Engine Interface (Intel MEI) Linux driver |
Tomas Winkler | 733ba91 | 2012-02-09 19:25:53 +0200 | [diff] [blame] | 4 | * Copyright (c) 2003-2012, Intel Corporation. |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | */ |
| 16 | |
Tomas Winkler | 2f3d2b4 | 2012-03-19 22:38:13 +0200 | [diff] [blame] | 17 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 18 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 19 | #include <linux/module.h> |
| 20 | #include <linux/moduleparam.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/device.h> |
| 23 | #include <linux/fs.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/types.h> |
| 26 | #include <linux/fcntl.h> |
| 27 | #include <linux/aio.h> |
| 28 | #include <linux/pci.h> |
| 29 | #include <linux/poll.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/ioctl.h> |
| 32 | #include <linux/cdev.h> |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 33 | #include <linux/sched.h> |
| 34 | #include <linux/uuid.h> |
| 35 | #include <linux/compat.h> |
| 36 | #include <linux/jiffies.h> |
| 37 | #include <linux/interrupt.h> |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 38 | #include <linux/miscdevice.h> |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 39 | |
| 40 | #include "mei_dev.h" |
Tomas Winkler | 4f3afe1 | 2012-05-09 16:38:59 +0300 | [diff] [blame] | 41 | #include <linux/mei.h> |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 42 | #include "interface.h" |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 43 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 44 | /* AMT device is a singleton on the platform */ |
| 45 | static struct pci_dev *mei_pdev; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 46 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 47 | /* mei_pci_tbl - PCI Device ID Table */ |
| 48 | static DEFINE_PCI_DEVICE_TABLE(mei_pci_tbl) = { |
| 49 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82946GZ)}, |
| 50 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G35)}, |
| 51 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82Q965)}, |
| 52 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G965)}, |
| 53 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GM965)}, |
| 54 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GME965)}, |
| 55 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q35)}, |
| 56 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82G33)}, |
| 57 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q33)}, |
| 58 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82X38)}, |
| 59 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_3200)}, |
| 60 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_6)}, |
| 61 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_7)}, |
| 62 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_8)}, |
| 63 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_9)}, |
| 64 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_10)}, |
| 65 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_1)}, |
| 66 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_2)}, |
| 67 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_3)}, |
| 68 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_4)}, |
| 69 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_1)}, |
| 70 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_2)}, |
| 71 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_3)}, |
| 72 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_4)}, |
| 73 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_1)}, |
| 74 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_2)}, |
| 75 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_CPT_1)}, |
| 76 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PBG_1)}, |
| 77 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_1)}, |
| 78 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_2)}, |
| 79 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_3)}, |
Tomas Winkler | 9af5142 | 2012-08-29 01:15:50 +0300 | [diff] [blame] | 80 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT)}, |
| 81 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT_LP)}, |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 82 | |
| 83 | /* required last entry */ |
| 84 | {0, } |
| 85 | }; |
| 86 | |
| 87 | MODULE_DEVICE_TABLE(pci, mei_pci_tbl); |
| 88 | |
| 89 | static DEFINE_MUTEX(mei_mutex); |
| 90 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * mei_clear_list - removes all callbacks associated with file |
| 94 | * from mei_cb_list |
| 95 | * |
| 96 | * @dev: device structure. |
| 97 | * @file: file structure |
| 98 | * @mei_cb_list: callbacks list |
| 99 | * |
| 100 | * mei_clear_list is called to clear resources associated with file |
| 101 | * when application calls close function or Ctrl-C was pressed |
| 102 | * |
| 103 | * returns true if callback removed from the list, false otherwise |
| 104 | */ |
| 105 | static bool mei_clear_list(struct mei_device *dev, |
Tomas Winkler | 15ea191 | 2012-11-01 21:17:16 +0200 | [diff] [blame] | 106 | const struct file *file, struct list_head *mei_cb_list) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 107 | { |
| 108 | struct mei_cl_cb *cb_pos = NULL; |
| 109 | struct mei_cl_cb *cb_next = NULL; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 110 | bool removed = false; |
| 111 | |
| 112 | /* list all list member */ |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 113 | list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, list) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 114 | /* check if list member associated with a file */ |
Tomas Winkler | 15ea191 | 2012-11-01 21:17:16 +0200 | [diff] [blame] | 115 | if (file == cb_pos->file_object) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 116 | /* remove member from the list */ |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 117 | list_del(&cb_pos->list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 118 | /* check if cb equal to current iamthif cb */ |
| 119 | if (dev->iamthif_current_cb == cb_pos) { |
| 120 | dev->iamthif_current_cb = NULL; |
| 121 | /* send flow control to iamthif client */ |
| 122 | mei_send_flow_control(dev, &dev->iamthif_cl); |
| 123 | } |
| 124 | /* free all allocated buffers */ |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 125 | mei_io_cb_free(cb_pos); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 126 | cb_pos = NULL; |
| 127 | removed = true; |
| 128 | } |
| 129 | } |
| 130 | return removed; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * mei_clear_lists - removes all callbacks associated with file |
| 135 | * |
| 136 | * @dev: device structure |
| 137 | * @file: file structure |
| 138 | * |
| 139 | * mei_clear_lists is called to clear resources associated with file |
| 140 | * when application calls close function or Ctrl-C was pressed |
| 141 | * |
| 142 | * returns true if callback removed from the list, false otherwise |
| 143 | */ |
| 144 | static bool mei_clear_lists(struct mei_device *dev, struct file *file) |
| 145 | { |
| 146 | bool removed = false; |
| 147 | |
| 148 | /* remove callbacks associated with a file */ |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 149 | mei_clear_list(dev, file, &dev->amthi_cmd_list.list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 150 | if (mei_clear_list(dev, file, |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 151 | &dev->amthi_read_complete_list.list)) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 152 | removed = true; |
| 153 | |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 154 | mei_clear_list(dev, file, &dev->ctrl_rd_list.list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 155 | |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 156 | if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list)) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 157 | removed = true; |
| 158 | |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 159 | if (mei_clear_list(dev, file, &dev->write_waiting_list.list)) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 160 | removed = true; |
| 161 | |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 162 | if (mei_clear_list(dev, file, &dev->write_list.list)) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 163 | removed = true; |
| 164 | |
| 165 | /* check if iamthif_current_cb not NULL */ |
| 166 | if (dev->iamthif_current_cb && !removed) { |
| 167 | /* check file and iamthif current cb association */ |
| 168 | if (dev->iamthif_current_cb->file_object == file) { |
| 169 | /* remove cb */ |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 170 | mei_io_cb_free(dev->iamthif_current_cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 171 | dev->iamthif_current_cb = NULL; |
| 172 | removed = true; |
| 173 | } |
| 174 | } |
| 175 | return removed; |
| 176 | } |
| 177 | /** |
| 178 | * find_read_list_entry - find read list entry |
| 179 | * |
| 180 | * @dev: device structure |
| 181 | * @file: pointer to file structure |
| 182 | * |
| 183 | * returns cb on success, NULL on error |
| 184 | */ |
| 185 | static struct mei_cl_cb *find_read_list_entry( |
| 186 | struct mei_device *dev, |
| 187 | struct mei_cl *cl) |
| 188 | { |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 189 | struct mei_cl_cb *pos = NULL; |
| 190 | struct mei_cl_cb *next = NULL; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 191 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 192 | dev_dbg(&dev->pdev->dev, "remove read_list CB\n"); |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 193 | list_for_each_entry_safe(pos, next, &dev->read_list.list, list) { |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 194 | struct mei_cl *cl_temp; |
| 195 | cl_temp = (struct mei_cl *)pos->file_private; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 196 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 197 | if (mei_cl_cmp_id(cl, cl_temp)) |
| 198 | return pos; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 199 | } |
| 200 | return NULL; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * mei_open - the open function |
| 205 | * |
| 206 | * @inode: pointer to inode structure |
| 207 | * @file: pointer to file structure |
| 208 | * |
| 209 | * returns 0 on success, <0 on error |
| 210 | */ |
| 211 | static int mei_open(struct inode *inode, struct file *file) |
| 212 | { |
| 213 | struct mei_cl *cl; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 214 | struct mei_device *dev; |
Tomas Winkler | 6f37aca | 2011-11-13 09:41:15 +0200 | [diff] [blame] | 215 | unsigned long cl_id; |
| 216 | int err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 217 | |
| 218 | err = -ENODEV; |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 219 | if (!mei_pdev) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 220 | goto out; |
| 221 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 222 | dev = pci_get_drvdata(mei_pdev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 223 | if (!dev) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 224 | goto out; |
| 225 | |
| 226 | mutex_lock(&dev->device_lock); |
| 227 | err = -ENOMEM; |
Tomas Winkler | c95efb7 | 2011-05-25 17:28:21 +0300 | [diff] [blame] | 228 | cl = mei_cl_allocate(dev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 229 | if (!cl) |
Alexey Khoroshilov | 303dfbf | 2011-08-31 00:41:14 +0400 | [diff] [blame] | 230 | goto out_unlock; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 231 | |
| 232 | err = -ENODEV; |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 233 | if (dev->dev_state != MEI_DEV_ENABLED) { |
| 234 | dev_dbg(&dev->pdev->dev, "dev_state != MEI_ENABLED dev_state = %s\n", |
| 235 | mei_dev_state_str(dev->dev_state)); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 236 | goto out_unlock; |
| 237 | } |
| 238 | err = -EMFILE; |
Tomas Winkler | 1b81294 | 2012-09-11 00:43:20 +0300 | [diff] [blame] | 239 | if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) { |
| 240 | dev_err(&dev->pdev->dev, "open_handle_count exceded %d", |
| 241 | MEI_MAX_OPEN_HANDLE_COUNT); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 242 | goto out_unlock; |
Tomas Winkler | 1b81294 | 2012-09-11 00:43:20 +0300 | [diff] [blame] | 243 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 244 | |
Tomas Winkler | 6f37aca | 2011-11-13 09:41:15 +0200 | [diff] [blame] | 245 | cl_id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX); |
Tomas Winkler | 1b81294 | 2012-09-11 00:43:20 +0300 | [diff] [blame] | 246 | if (cl_id >= MEI_CLIENTS_MAX) { |
| 247 | dev_err(&dev->pdev->dev, "client_id exceded %d", |
| 248 | MEI_CLIENTS_MAX) ; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 249 | goto out_unlock; |
Tomas Winkler | 1b81294 | 2012-09-11 00:43:20 +0300 | [diff] [blame] | 250 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 251 | |
Tomas Winkler | 6f37aca | 2011-11-13 09:41:15 +0200 | [diff] [blame] | 252 | cl->host_client_id = cl_id; |
| 253 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 254 | dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id); |
| 255 | |
| 256 | dev->open_handle_count++; |
Tomas Winkler | 6f37aca | 2011-11-13 09:41:15 +0200 | [diff] [blame] | 257 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 258 | list_add_tail(&cl->link, &dev->file_list); |
| 259 | |
| 260 | set_bit(cl->host_client_id, dev->host_clients_map); |
| 261 | cl->state = MEI_FILE_INITIALIZING; |
| 262 | cl->sm_state = 0; |
| 263 | |
| 264 | file->private_data = cl; |
| 265 | mutex_unlock(&dev->device_lock); |
| 266 | |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 267 | return nonseekable_open(inode, file); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 268 | |
| 269 | out_unlock: |
| 270 | mutex_unlock(&dev->device_lock); |
| 271 | kfree(cl); |
| 272 | out: |
| 273 | return err; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * mei_release - the release function |
| 278 | * |
| 279 | * @inode: pointer to inode structure |
| 280 | * @file: pointer to file structure |
| 281 | * |
| 282 | * returns 0 on success, <0 on error |
| 283 | */ |
| 284 | static int mei_release(struct inode *inode, struct file *file) |
| 285 | { |
| 286 | struct mei_cl *cl = file->private_data; |
| 287 | struct mei_cl_cb *cb; |
| 288 | struct mei_device *dev; |
| 289 | int rets = 0; |
| 290 | |
| 291 | if (WARN_ON(!cl || !cl->dev)) |
| 292 | return -ENODEV; |
| 293 | |
| 294 | dev = cl->dev; |
| 295 | |
| 296 | mutex_lock(&dev->device_lock); |
| 297 | if (cl != &dev->iamthif_cl) { |
| 298 | if (cl->state == MEI_FILE_CONNECTED) { |
| 299 | cl->state = MEI_FILE_DISCONNECTING; |
| 300 | dev_dbg(&dev->pdev->dev, |
| 301 | "disconnecting client host client = %d, " |
| 302 | "ME client = %d\n", |
| 303 | cl->host_client_id, |
| 304 | cl->me_client_id); |
| 305 | rets = mei_disconnect_host_client(dev, cl); |
| 306 | } |
Tomas Winkler | 0288c7c | 2011-06-06 10:44:34 +0300 | [diff] [blame] | 307 | mei_cl_flush_queues(cl); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 308 | dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n", |
| 309 | cl->host_client_id, |
| 310 | cl->me_client_id); |
| 311 | |
| 312 | if (dev->open_handle_count > 0) { |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 313 | clear_bit(cl->host_client_id, dev->host_clients_map); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 314 | dev->open_handle_count--; |
| 315 | } |
| 316 | mei_remove_client_from_file_list(dev, cl->host_client_id); |
| 317 | |
| 318 | /* free read cb */ |
| 319 | cb = NULL; |
| 320 | if (cl->read_cb) { |
| 321 | cb = find_read_list_entry(dev, cl); |
| 322 | /* Remove entry from read list */ |
| 323 | if (cb) |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 324 | list_del(&cb->list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 325 | |
| 326 | cb = cl->read_cb; |
| 327 | cl->read_cb = NULL; |
| 328 | } |
| 329 | |
| 330 | file->private_data = NULL; |
| 331 | |
| 332 | if (cb) { |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 333 | mei_io_cb_free(cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 334 | cb = NULL; |
| 335 | } |
| 336 | |
| 337 | kfree(cl); |
| 338 | } else { |
| 339 | if (dev->open_handle_count > 0) |
| 340 | dev->open_handle_count--; |
| 341 | |
| 342 | if (dev->iamthif_file_object == file && |
| 343 | dev->iamthif_state != MEI_IAMTHIF_IDLE) { |
| 344 | |
| 345 | dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n", |
| 346 | dev->iamthif_state); |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 347 | dev->iamthif_canceled = true; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 348 | if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) { |
| 349 | dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 350 | mei_amthif_run_next_cmd(dev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
| 354 | if (mei_clear_lists(dev, file)) |
| 355 | dev->iamthif_state = MEI_IAMTHIF_IDLE; |
| 356 | |
| 357 | } |
| 358 | mutex_unlock(&dev->device_lock); |
| 359 | return rets; |
| 360 | } |
| 361 | |
| 362 | |
| 363 | /** |
| 364 | * mei_read - the read function. |
| 365 | * |
| 366 | * @file: pointer to file structure |
| 367 | * @ubuf: pointer to user buffer |
| 368 | * @length: buffer length |
| 369 | * @offset: data offset in buffer |
| 370 | * |
| 371 | * returns >=0 data length on success , <0 on error |
| 372 | */ |
| 373 | static ssize_t mei_read(struct file *file, char __user *ubuf, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 374 | size_t length, loff_t *offset) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 375 | { |
| 376 | struct mei_cl *cl = file->private_data; |
| 377 | struct mei_cl_cb *cb_pos = NULL; |
| 378 | struct mei_cl_cb *cb = NULL; |
| 379 | struct mei_device *dev; |
| 380 | int i; |
| 381 | int rets; |
| 382 | int err; |
| 383 | |
| 384 | |
| 385 | if (WARN_ON(!cl || !cl->dev)) |
| 386 | return -ENODEV; |
| 387 | |
| 388 | dev = cl->dev; |
| 389 | |
| 390 | mutex_lock(&dev->device_lock); |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 391 | if (dev->dev_state != MEI_DEV_ENABLED) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 392 | rets = -ENODEV; |
| 393 | goto out; |
| 394 | } |
| 395 | |
| 396 | if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) { |
| 397 | /* Do not allow to read watchdog client */ |
Tomas Winkler | 07b509b | 2012-07-23 14:05:39 +0300 | [diff] [blame] | 398 | i = mei_me_cl_by_uuid(dev, &mei_wd_guid); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 399 | if (i >= 0) { |
| 400 | struct mei_me_client *me_client = &dev->me_clients[i]; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 401 | if (cl->me_client_id == me_client->client_id) { |
| 402 | rets = -EBADF; |
| 403 | goto out; |
| 404 | } |
| 405 | } |
| 406 | } else { |
| 407 | cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT; |
| 408 | } |
| 409 | |
| 410 | if (cl == &dev->iamthif_cl) { |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 411 | rets = mei_amthif_read(dev, file, ubuf, length, offset); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 412 | goto out; |
| 413 | } |
| 414 | |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 415 | if (cl->read_cb && cl->read_cb->buf_idx > *offset) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 416 | cb = cl->read_cb; |
| 417 | goto copy_buffer; |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 418 | } else if (cl->read_cb && cl->read_cb->buf_idx > 0 && |
| 419 | cl->read_cb->buf_idx <= *offset) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 420 | cb = cl->read_cb; |
| 421 | rets = 0; |
| 422 | goto free; |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 423 | } else if ((!cl->read_cb || !cl->read_cb->buf_idx) && *offset > 0) { |
Justin P. Mattock | 5f9092f3 | 2012-03-12 07:18:09 -0700 | [diff] [blame] | 424 | /*Offset needs to be cleaned for contiguous reads*/ |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 425 | *offset = 0; |
| 426 | rets = 0; |
| 427 | goto out; |
| 428 | } |
| 429 | |
| 430 | err = mei_start_read(dev, cl); |
| 431 | if (err && err != -EBUSY) { |
| 432 | dev_dbg(&dev->pdev->dev, |
| 433 | "mei start read failure with status = %d\n", err); |
| 434 | rets = err; |
| 435 | goto out; |
| 436 | } |
| 437 | |
| 438 | if (MEI_READ_COMPLETE != cl->reading_state && |
| 439 | !waitqueue_active(&cl->rx_wait)) { |
| 440 | if (file->f_flags & O_NONBLOCK) { |
| 441 | rets = -EAGAIN; |
| 442 | goto out; |
| 443 | } |
| 444 | |
| 445 | mutex_unlock(&dev->device_lock); |
| 446 | |
| 447 | if (wait_event_interruptible(cl->rx_wait, |
| 448 | (MEI_READ_COMPLETE == cl->reading_state || |
| 449 | MEI_FILE_INITIALIZING == cl->state || |
| 450 | MEI_FILE_DISCONNECTED == cl->state || |
| 451 | MEI_FILE_DISCONNECTING == cl->state))) { |
| 452 | if (signal_pending(current)) |
| 453 | return -EINTR; |
| 454 | return -ERESTARTSYS; |
| 455 | } |
| 456 | |
| 457 | mutex_lock(&dev->device_lock); |
| 458 | if (MEI_FILE_INITIALIZING == cl->state || |
| 459 | MEI_FILE_DISCONNECTED == cl->state || |
| 460 | MEI_FILE_DISCONNECTING == cl->state) { |
| 461 | rets = -EBUSY; |
| 462 | goto out; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | cb = cl->read_cb; |
| 467 | |
| 468 | if (!cb) { |
| 469 | rets = -ENODEV; |
| 470 | goto out; |
| 471 | } |
| 472 | if (cl->reading_state != MEI_READ_COMPLETE) { |
| 473 | rets = 0; |
| 474 | goto out; |
| 475 | } |
| 476 | /* now copy the data to user space */ |
| 477 | copy_buffer: |
| 478 | dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n", |
| 479 | cb->response_buffer.size); |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 480 | dev_dbg(&dev->pdev->dev, "cb->buf_idx - %lu\n", cb->buf_idx); |
| 481 | if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 482 | rets = -EMSGSIZE; |
| 483 | goto free; |
| 484 | } |
| 485 | |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 486 | /* length is being truncated to PAGE_SIZE, |
| 487 | * however buf_idx may point beyond that */ |
| 488 | length = min_t(size_t, length, cb->buf_idx - *offset); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 489 | |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 490 | if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 491 | rets = -EFAULT; |
| 492 | goto free; |
| 493 | } |
| 494 | |
| 495 | rets = length; |
| 496 | *offset += length; |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 497 | if ((unsigned long)*offset < cb->buf_idx) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 498 | goto out; |
| 499 | |
| 500 | free: |
| 501 | cb_pos = find_read_list_entry(dev, cl); |
| 502 | /* Remove entry from read list */ |
| 503 | if (cb_pos) |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 504 | list_del(&cb_pos->list); |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 505 | mei_io_cb_free(cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 506 | cl->reading_state = MEI_IDLE; |
| 507 | cl->read_cb = NULL; |
| 508 | cl->read_pending = 0; |
| 509 | out: |
| 510 | dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets); |
| 511 | mutex_unlock(&dev->device_lock); |
| 512 | return rets; |
| 513 | } |
Tomas Winkler | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 514 | /** |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 515 | * mei_write - the write function. |
| 516 | * |
| 517 | * @file: pointer to file structure |
| 518 | * @ubuf: pointer to user buffer |
| 519 | * @length: buffer length |
| 520 | * @offset: data offset in buffer |
| 521 | * |
| 522 | * returns >=0 data length on success , <0 on error |
| 523 | */ |
| 524 | static ssize_t mei_write(struct file *file, const char __user *ubuf, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 525 | size_t length, loff_t *offset) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 526 | { |
| 527 | struct mei_cl *cl = file->private_data; |
| 528 | struct mei_cl_cb *write_cb = NULL; |
| 529 | struct mei_msg_hdr mei_hdr; |
| 530 | struct mei_device *dev; |
| 531 | unsigned long timeout = 0; |
| 532 | int rets; |
| 533 | int i; |
| 534 | |
| 535 | if (WARN_ON(!cl || !cl->dev)) |
| 536 | return -ENODEV; |
| 537 | |
| 538 | dev = cl->dev; |
| 539 | |
| 540 | mutex_lock(&dev->device_lock); |
| 541 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 542 | if (dev->dev_state != MEI_DEV_ENABLED) { |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 543 | rets = -ENODEV; |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 544 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 545 | } |
| 546 | |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 547 | i = mei_me_cl_by_id(dev, cl->me_client_id); |
| 548 | if (i < 0) { |
| 549 | rets = -ENODEV; |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 550 | goto err; |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 551 | } |
| 552 | if (length > dev->me_clients[i].props.max_msg_length || length <= 0) { |
| 553 | rets = -EMSGSIZE; |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 554 | goto err; |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | if (cl->state != MEI_FILE_CONNECTED) { |
| 558 | rets = -ENODEV; |
| 559 | dev_err(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d", |
| 560 | cl->host_client_id, cl->me_client_id); |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 561 | goto err; |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 562 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 563 | if (cl == &dev->iamthif_cl) { |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 564 | write_cb = mei_amthif_find_read_list_entry(dev, file); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 565 | |
| 566 | if (write_cb) { |
| 567 | timeout = write_cb->read_time + |
Tomas Winkler | 3870c32 | 2012-11-01 21:17:14 +0200 | [diff] [blame] | 568 | mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 569 | |
| 570 | if (time_after(jiffies, timeout) || |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 571 | cl->reading_state == MEI_READ_COMPLETE) { |
| 572 | *offset = 0; |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 573 | list_del(&write_cb->list); |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 574 | mei_io_cb_free(write_cb); |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 575 | write_cb = NULL; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | /* free entry used in read */ |
| 581 | if (cl->reading_state == MEI_READ_COMPLETE) { |
| 582 | *offset = 0; |
| 583 | write_cb = find_read_list_entry(dev, cl); |
| 584 | if (write_cb) { |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 585 | list_del(&write_cb->list); |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 586 | mei_io_cb_free(write_cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 587 | write_cb = NULL; |
| 588 | cl->reading_state = MEI_IDLE; |
| 589 | cl->read_cb = NULL; |
| 590 | cl->read_pending = 0; |
| 591 | } |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 592 | } else if (cl->reading_state == MEI_IDLE && !cl->read_pending) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 593 | *offset = 0; |
| 594 | |
| 595 | |
Tomas Winkler | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 596 | write_cb = mei_io_cb_init(cl, file); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 597 | if (!write_cb) { |
Tomas Winkler | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 598 | dev_err(&dev->pdev->dev, "write cb allocation failed\n"); |
| 599 | rets = -ENOMEM; |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 600 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 601 | } |
Tomas Winkler | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 602 | rets = mei_io_cb_alloc_req_buf(write_cb, length); |
| 603 | if (rets) |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 604 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 605 | |
Tomas Winkler | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 606 | dev_dbg(&dev->pdev->dev, "cb request size = %zd\n", length); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 607 | |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 608 | rets = copy_from_user(write_cb->request_buffer.data, ubuf, length); |
| 609 | if (rets) |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 610 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 611 | |
| 612 | cl->sm_state = 0; |
| 613 | if (length == 4 && |
| 614 | ((memcmp(mei_wd_state_independence_msg[0], |
| 615 | write_cb->request_buffer.data, 4) == 0) || |
| 616 | (memcmp(mei_wd_state_independence_msg[1], |
| 617 | write_cb->request_buffer.data, 4) == 0) || |
| 618 | (memcmp(mei_wd_state_independence_msg[2], |
| 619 | write_cb->request_buffer.data, 4) == 0))) |
| 620 | cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT; |
| 621 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 622 | if (cl == &dev->iamthif_cl) { |
Tomas Winkler | ab5c4a5 | 2012-11-01 21:17:18 +0200 | [diff] [blame^] | 623 | rets = mei_amthif_write(dev, write_cb); |
| 624 | |
| 625 | if (rets) { |
| 626 | dev_err(&dev->pdev->dev, |
| 627 | "amthi write failed with status = %d\n", rets); |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 628 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 629 | } |
| 630 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 631 | return length; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | write_cb->major_file_operations = MEI_WRITE; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 635 | |
| 636 | dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n", |
| 637 | cl->host_client_id, cl->me_client_id); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 638 | rets = mei_flow_ctrl_creds(dev, cl); |
| 639 | if (rets < 0) |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 640 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 641 | |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 642 | if (rets == 0 || dev->mei_host_buffer_is_empty == false) { |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 643 | write_cb->buf_idx = 0; |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 644 | mei_hdr.msg_complete = 0; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 645 | cl->writing_state = MEI_WRITING; |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 646 | goto out; |
| 647 | } |
| 648 | |
| 649 | dev->mei_host_buffer_is_empty = false; |
| 650 | if (length > mei_hbuf_max_data(dev)) { |
| 651 | mei_hdr.length = mei_hbuf_max_data(dev); |
| 652 | mei_hdr.msg_complete = 0; |
| 653 | } else { |
| 654 | mei_hdr.length = length; |
| 655 | mei_hdr.msg_complete = 1; |
| 656 | } |
| 657 | mei_hdr.host_addr = cl->host_client_id; |
| 658 | mei_hdr.me_addr = cl->me_client_id; |
| 659 | mei_hdr.reserved = 0; |
| 660 | dev_dbg(&dev->pdev->dev, "call mei_write_message header=%08x.\n", |
| 661 | *((u32 *) &mei_hdr)); |
| 662 | if (mei_write_message(dev, &mei_hdr, |
| 663 | write_cb->request_buffer.data, mei_hdr.length)) { |
| 664 | rets = -ENODEV; |
| 665 | goto err; |
| 666 | } |
| 667 | cl->writing_state = MEI_WRITING; |
| 668 | write_cb->buf_idx = mei_hdr.length; |
| 669 | |
| 670 | out: |
| 671 | if (mei_hdr.msg_complete) { |
| 672 | if (mei_flow_ctrl_reduce(dev, cl)) { |
| 673 | rets = -ENODEV; |
| 674 | goto err; |
| 675 | } |
| 676 | list_add_tail(&write_cb->list, &dev->write_waiting_list.list); |
| 677 | } else { |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 678 | list_add_tail(&write_cb->list, &dev->write_list.list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 679 | } |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 680 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 681 | mutex_unlock(&dev->device_lock); |
| 682 | return length; |
| 683 | |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 684 | err: |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 685 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 686 | mei_io_cb_free(write_cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 687 | return rets; |
| 688 | } |
| 689 | |
| 690 | |
| 691 | /** |
| 692 | * mei_ioctl - the IOCTL function |
| 693 | * |
| 694 | * @file: pointer to file structure |
| 695 | * @cmd: ioctl command |
| 696 | * @data: pointer to mei message structure |
| 697 | * |
| 698 | * returns 0 on success , <0 on error |
| 699 | */ |
| 700 | static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data) |
| 701 | { |
| 702 | struct mei_device *dev; |
| 703 | struct mei_cl *cl = file->private_data; |
| 704 | struct mei_connect_client_data *connect_data = NULL; |
| 705 | int rets; |
| 706 | |
| 707 | if (cmd != IOCTL_MEI_CONNECT_CLIENT) |
| 708 | return -EINVAL; |
| 709 | |
| 710 | if (WARN_ON(!cl || !cl->dev)) |
| 711 | return -ENODEV; |
| 712 | |
| 713 | dev = cl->dev; |
| 714 | |
| 715 | dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd); |
| 716 | |
| 717 | mutex_lock(&dev->device_lock); |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 718 | if (dev->dev_state != MEI_DEV_ENABLED) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 719 | rets = -ENODEV; |
| 720 | goto out; |
| 721 | } |
| 722 | |
| 723 | dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n"); |
| 724 | |
| 725 | connect_data = kzalloc(sizeof(struct mei_connect_client_data), |
| 726 | GFP_KERNEL); |
| 727 | if (!connect_data) { |
| 728 | rets = -ENOMEM; |
| 729 | goto out; |
| 730 | } |
| 731 | dev_dbg(&dev->pdev->dev, "copy connect data from user\n"); |
| 732 | if (copy_from_user(connect_data, (char __user *)data, |
| 733 | sizeof(struct mei_connect_client_data))) { |
| 734 | dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n"); |
| 735 | rets = -EFAULT; |
| 736 | goto out; |
| 737 | } |
| 738 | rets = mei_ioctl_connect_client(file, connect_data); |
| 739 | |
| 740 | /* if all is ok, copying the data back to user. */ |
| 741 | if (rets) |
| 742 | goto out; |
| 743 | |
| 744 | dev_dbg(&dev->pdev->dev, "copy connect data to user\n"); |
| 745 | if (copy_to_user((char __user *)data, connect_data, |
| 746 | sizeof(struct mei_connect_client_data))) { |
| 747 | dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n"); |
| 748 | rets = -EFAULT; |
| 749 | goto out; |
| 750 | } |
| 751 | |
| 752 | out: |
| 753 | kfree(connect_data); |
| 754 | mutex_unlock(&dev->device_lock); |
| 755 | return rets; |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * mei_compat_ioctl - the compat IOCTL function |
| 760 | * |
| 761 | * @file: pointer to file structure |
| 762 | * @cmd: ioctl command |
| 763 | * @data: pointer to mei message structure |
| 764 | * |
| 765 | * returns 0 on success , <0 on error |
| 766 | */ |
| 767 | #ifdef CONFIG_COMPAT |
| 768 | static long mei_compat_ioctl(struct file *file, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 769 | unsigned int cmd, unsigned long data) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 770 | { |
| 771 | return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data)); |
| 772 | } |
| 773 | #endif |
| 774 | |
| 775 | |
| 776 | /** |
| 777 | * mei_poll - the poll function |
| 778 | * |
| 779 | * @file: pointer to file structure |
| 780 | * @wait: pointer to poll_table structure |
| 781 | * |
| 782 | * returns poll mask |
| 783 | */ |
| 784 | static unsigned int mei_poll(struct file *file, poll_table *wait) |
| 785 | { |
| 786 | struct mei_cl *cl = file->private_data; |
| 787 | struct mei_device *dev; |
| 788 | unsigned int mask = 0; |
| 789 | |
| 790 | if (WARN_ON(!cl || !cl->dev)) |
| 791 | return mask; |
| 792 | |
| 793 | dev = cl->dev; |
| 794 | |
| 795 | mutex_lock(&dev->device_lock); |
| 796 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 797 | if (dev->dev_state != MEI_DEV_ENABLED) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 798 | goto out; |
| 799 | |
| 800 | |
| 801 | if (cl == &dev->iamthif_cl) { |
| 802 | mutex_unlock(&dev->device_lock); |
| 803 | poll_wait(file, &dev->iamthif_cl.wait, wait); |
| 804 | mutex_lock(&dev->device_lock); |
| 805 | if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE && |
| 806 | dev->iamthif_file_object == file) { |
| 807 | mask |= (POLLIN | POLLRDNORM); |
| 808 | dev_dbg(&dev->pdev->dev, "run next amthi cb\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 809 | mei_amthif_run_next_cmd(dev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 810 | } |
| 811 | goto out; |
| 812 | } |
| 813 | |
| 814 | mutex_unlock(&dev->device_lock); |
| 815 | poll_wait(file, &cl->tx_wait, wait); |
| 816 | mutex_lock(&dev->device_lock); |
| 817 | if (MEI_WRITE_COMPLETE == cl->writing_state) |
| 818 | mask |= (POLLIN | POLLRDNORM); |
| 819 | |
| 820 | out: |
| 821 | mutex_unlock(&dev->device_lock); |
| 822 | return mask; |
| 823 | } |
| 824 | |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 825 | /* |
| 826 | * file operations structure will be used for mei char device. |
| 827 | */ |
| 828 | static const struct file_operations mei_fops = { |
| 829 | .owner = THIS_MODULE, |
| 830 | .read = mei_read, |
| 831 | .unlocked_ioctl = mei_ioctl, |
| 832 | #ifdef CONFIG_COMPAT |
| 833 | .compat_ioctl = mei_compat_ioctl, |
| 834 | #endif |
| 835 | .open = mei_open, |
| 836 | .release = mei_release, |
| 837 | .write = mei_write, |
| 838 | .poll = mei_poll, |
| 839 | .llseek = no_llseek |
| 840 | }; |
| 841 | |
| 842 | |
| 843 | /* |
| 844 | * Misc Device Struct |
| 845 | */ |
| 846 | static struct miscdevice mei_misc_device = { |
Tomas Winkler | c38ea24 | 2012-04-02 20:32:39 +0300 | [diff] [blame] | 847 | .name = "mei", |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 848 | .fops = &mei_fops, |
| 849 | .minor = MISC_DYNAMIC_MINOR, |
| 850 | }; |
| 851 | |
| 852 | /** |
Tomas Winkler | 9a123f1 | 2012-08-06 15:23:55 +0300 | [diff] [blame] | 853 | * mei_quirk_probe - probe for devices that doesn't valid ME interface |
| 854 | * @pdev: PCI device structure |
| 855 | * @ent: entry into pci_device_table |
| 856 | * |
| 857 | * returns true if ME Interface is valid, false otherwise |
| 858 | */ |
| 859 | static bool __devinit mei_quirk_probe(struct pci_dev *pdev, |
| 860 | const struct pci_device_id *ent) |
| 861 | { |
| 862 | u32 reg; |
| 863 | if (ent->device == MEI_DEV_ID_PBG_1) { |
| 864 | pci_read_config_dword(pdev, 0x48, ®); |
| 865 | /* make sure that bit 9 is up and bit 10 is down */ |
| 866 | if ((reg & 0x600) == 0x200) { |
| 867 | dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n"); |
| 868 | return false; |
| 869 | } |
| 870 | } |
| 871 | return true; |
| 872 | } |
| 873 | /** |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 874 | * mei_probe - Device Initialization Routine |
| 875 | * |
| 876 | * @pdev: PCI device structure |
| 877 | * @ent: entry in kcs_pci_tbl |
| 878 | * |
| 879 | * returns 0 on success, <0 on failure. |
| 880 | */ |
| 881 | static int __devinit mei_probe(struct pci_dev *pdev, |
| 882 | const struct pci_device_id *ent) |
| 883 | { |
| 884 | struct mei_device *dev; |
| 885 | int err; |
| 886 | |
| 887 | mutex_lock(&mei_mutex); |
Tomas Winkler | 9a123f1 | 2012-08-06 15:23:55 +0300 | [diff] [blame] | 888 | |
| 889 | if (!mei_quirk_probe(pdev, ent)) { |
| 890 | err = -ENODEV; |
| 891 | goto end; |
| 892 | } |
| 893 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 894 | if (mei_pdev) { |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 895 | err = -EEXIST; |
| 896 | goto end; |
| 897 | } |
| 898 | /* enable pci dev */ |
| 899 | err = pci_enable_device(pdev); |
| 900 | if (err) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 901 | dev_err(&pdev->dev, "failed to enable pci device.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 902 | goto end; |
| 903 | } |
| 904 | /* set PCI host mastering */ |
| 905 | pci_set_master(pdev); |
| 906 | /* pci request regions for mei driver */ |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 907 | err = pci_request_regions(pdev, KBUILD_MODNAME); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 908 | if (err) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 909 | dev_err(&pdev->dev, "failed to get pci regions.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 910 | goto disable_device; |
| 911 | } |
| 912 | /* allocates and initializes the mei dev structure */ |
| 913 | dev = mei_device_init(pdev); |
| 914 | if (!dev) { |
| 915 | err = -ENOMEM; |
| 916 | goto release_regions; |
| 917 | } |
| 918 | /* mapping IO device memory */ |
| 919 | dev->mem_addr = pci_iomap(pdev, 0, 0); |
| 920 | if (!dev->mem_addr) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 921 | dev_err(&pdev->dev, "mapping I/O device memory failure.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 922 | err = -ENOMEM; |
| 923 | goto free_device; |
| 924 | } |
| 925 | pci_enable_msi(pdev); |
| 926 | |
| 927 | /* request and enable interrupt */ |
| 928 | if (pci_dev_msi_enabled(pdev)) |
| 929 | err = request_threaded_irq(pdev->irq, |
| 930 | NULL, |
| 931 | mei_interrupt_thread_handler, |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 932 | IRQF_ONESHOT, KBUILD_MODNAME, dev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 933 | else |
| 934 | err = request_threaded_irq(pdev->irq, |
| 935 | mei_interrupt_quick_handler, |
| 936 | mei_interrupt_thread_handler, |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 937 | IRQF_SHARED, KBUILD_MODNAME, dev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 938 | |
| 939 | if (err) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 940 | dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n", |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 941 | pdev->irq); |
Samuel Ortiz | 169dc38 | 2012-06-11 12:18:30 +0300 | [diff] [blame] | 942 | goto disable_msi; |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 943 | } |
| 944 | INIT_DELAYED_WORK(&dev->timer_work, mei_timer); |
| 945 | if (mei_hw_init(dev)) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 946 | dev_err(&pdev->dev, "init hw failure.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 947 | err = -ENODEV; |
| 948 | goto release_irq; |
| 949 | } |
| 950 | |
| 951 | err = misc_register(&mei_misc_device); |
| 952 | if (err) |
| 953 | goto release_irq; |
| 954 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 955 | mei_pdev = pdev; |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 956 | pci_set_drvdata(pdev, dev); |
| 957 | |
| 958 | |
| 959 | schedule_delayed_work(&dev->timer_work, HZ); |
| 960 | |
| 961 | mutex_unlock(&mei_mutex); |
| 962 | |
Tomas Winkler | 2f3d2b4 | 2012-03-19 22:38:13 +0200 | [diff] [blame] | 963 | pr_debug("initialization successful.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 964 | |
| 965 | return 0; |
| 966 | |
| 967 | release_irq: |
| 968 | /* disable interrupts */ |
| 969 | dev->host_hw_state = mei_hcsr_read(dev); |
| 970 | mei_disable_interrupts(dev); |
| 971 | flush_scheduled_work(); |
| 972 | free_irq(pdev->irq, dev); |
Samuel Ortiz | 169dc38 | 2012-06-11 12:18:30 +0300 | [diff] [blame] | 973 | disable_msi: |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 974 | pci_disable_msi(pdev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 975 | pci_iounmap(pdev, dev->mem_addr); |
| 976 | free_device: |
| 977 | kfree(dev); |
| 978 | release_regions: |
| 979 | pci_release_regions(pdev); |
| 980 | disable_device: |
| 981 | pci_disable_device(pdev); |
| 982 | end: |
| 983 | mutex_unlock(&mei_mutex); |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 984 | dev_err(&pdev->dev, "initialization failed.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 985 | return err; |
| 986 | } |
| 987 | |
| 988 | /** |
| 989 | * mei_remove - Device Removal Routine |
| 990 | * |
| 991 | * @pdev: PCI device structure |
| 992 | * |
| 993 | * mei_remove is called by the PCI subsystem to alert the driver |
| 994 | * that it should release a PCI device. |
| 995 | */ |
| 996 | static void __devexit mei_remove(struct pci_dev *pdev) |
| 997 | { |
| 998 | struct mei_device *dev; |
| 999 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 1000 | if (mei_pdev != pdev) |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1001 | return; |
| 1002 | |
| 1003 | dev = pci_get_drvdata(pdev); |
| 1004 | if (!dev) |
| 1005 | return; |
| 1006 | |
| 1007 | mutex_lock(&dev->device_lock); |
| 1008 | |
Tomas Winkler | c216fde | 2012-08-16 19:39:43 +0300 | [diff] [blame] | 1009 | cancel_delayed_work(&dev->timer_work); |
| 1010 | |
| 1011 | mei_wd_stop(dev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1012 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 1013 | mei_pdev = NULL; |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1014 | |
| 1015 | if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) { |
| 1016 | dev->iamthif_cl.state = MEI_FILE_DISCONNECTING; |
| 1017 | mei_disconnect_host_client(dev, &dev->iamthif_cl); |
| 1018 | } |
| 1019 | if (dev->wd_cl.state == MEI_FILE_CONNECTED) { |
| 1020 | dev->wd_cl.state = MEI_FILE_DISCONNECTING; |
| 1021 | mei_disconnect_host_client(dev, &dev->wd_cl); |
| 1022 | } |
| 1023 | |
| 1024 | /* Unregistering watchdog device */ |
Tomas Winkler | 70cd533 | 2011-12-22 18:50:50 +0200 | [diff] [blame] | 1025 | mei_watchdog_unregister(dev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1026 | |
| 1027 | /* remove entry if already in list */ |
| 1028 | dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n"); |
| 1029 | mei_remove_client_from_file_list(dev, dev->wd_cl.host_client_id); |
| 1030 | mei_remove_client_from_file_list(dev, dev->iamthif_cl.host_client_id); |
| 1031 | |
| 1032 | dev->iamthif_current_cb = NULL; |
| 1033 | dev->me_clients_num = 0; |
| 1034 | |
| 1035 | mutex_unlock(&dev->device_lock); |
| 1036 | |
| 1037 | flush_scheduled_work(); |
| 1038 | |
| 1039 | /* disable interrupts */ |
| 1040 | mei_disable_interrupts(dev); |
| 1041 | |
| 1042 | free_irq(pdev->irq, dev); |
| 1043 | pci_disable_msi(pdev); |
| 1044 | pci_set_drvdata(pdev, NULL); |
| 1045 | |
| 1046 | if (dev->mem_addr) |
| 1047 | pci_iounmap(pdev, dev->mem_addr); |
| 1048 | |
| 1049 | kfree(dev); |
| 1050 | |
| 1051 | pci_release_regions(pdev); |
| 1052 | pci_disable_device(pdev); |
Tomas Winkler | a44cab4 | 2012-05-29 16:39:11 +0300 | [diff] [blame] | 1053 | |
| 1054 | misc_deregister(&mei_misc_device); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1055 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1056 | #ifdef CONFIG_PM |
| 1057 | static int mei_pci_suspend(struct device *device) |
| 1058 | { |
| 1059 | struct pci_dev *pdev = to_pci_dev(device); |
| 1060 | struct mei_device *dev = pci_get_drvdata(pdev); |
| 1061 | int err; |
| 1062 | |
| 1063 | if (!dev) |
| 1064 | return -ENODEV; |
| 1065 | mutex_lock(&dev->device_lock); |
Tomas Winkler | c216fde | 2012-08-16 19:39:43 +0300 | [diff] [blame] | 1066 | |
| 1067 | cancel_delayed_work(&dev->timer_work); |
| 1068 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1069 | /* Stop watchdog if exists */ |
Tomas Winkler | c216fde | 2012-08-16 19:39:43 +0300 | [diff] [blame] | 1070 | err = mei_wd_stop(dev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1071 | /* Set new mei state */ |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 1072 | if (dev->dev_state == MEI_DEV_ENABLED || |
| 1073 | dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) { |
| 1074 | dev->dev_state = MEI_DEV_POWER_DOWN; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1075 | mei_reset(dev, 0); |
| 1076 | } |
| 1077 | mutex_unlock(&dev->device_lock); |
| 1078 | |
| 1079 | free_irq(pdev->irq, dev); |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 1080 | pci_disable_msi(pdev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1081 | |
| 1082 | return err; |
| 1083 | } |
| 1084 | |
| 1085 | static int mei_pci_resume(struct device *device) |
| 1086 | { |
| 1087 | struct pci_dev *pdev = to_pci_dev(device); |
| 1088 | struct mei_device *dev; |
| 1089 | int err; |
| 1090 | |
| 1091 | dev = pci_get_drvdata(pdev); |
| 1092 | if (!dev) |
| 1093 | return -ENODEV; |
| 1094 | |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 1095 | pci_enable_msi(pdev); |
| 1096 | |
| 1097 | /* request and enable interrupt */ |
| 1098 | if (pci_dev_msi_enabled(pdev)) |
| 1099 | err = request_threaded_irq(pdev->irq, |
| 1100 | NULL, |
| 1101 | mei_interrupt_thread_handler, |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 1102 | IRQF_ONESHOT, KBUILD_MODNAME, dev); |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 1103 | else |
| 1104 | err = request_threaded_irq(pdev->irq, |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1105 | mei_interrupt_quick_handler, |
| 1106 | mei_interrupt_thread_handler, |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 1107 | IRQF_SHARED, KBUILD_MODNAME, dev); |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 1108 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1109 | if (err) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 1110 | dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n", |
| 1111 | pdev->irq); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1112 | return err; |
| 1113 | } |
| 1114 | |
| 1115 | mutex_lock(&dev->device_lock); |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 1116 | dev->dev_state = MEI_DEV_POWER_UP; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1117 | mei_reset(dev, 1); |
| 1118 | mutex_unlock(&dev->device_lock); |
| 1119 | |
Oren Weil | 6d70e93 | 2011-09-07 09:03:14 +0300 | [diff] [blame] | 1120 | /* Start timer if stopped in suspend */ |
| 1121 | schedule_delayed_work(&dev->timer_work, HZ); |
| 1122 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1123 | return err; |
| 1124 | } |
| 1125 | static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume); |
| 1126 | #define MEI_PM_OPS (&mei_pm_ops) |
| 1127 | #else |
Randy Dunlap | 2d99036 | 2011-05-19 08:52:34 -0700 | [diff] [blame] | 1128 | #define MEI_PM_OPS NULL |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1129 | #endif /* CONFIG_PM */ |
| 1130 | /* |
| 1131 | * PCI driver structure |
| 1132 | */ |
| 1133 | static struct pci_driver mei_driver = { |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 1134 | .name = KBUILD_MODNAME, |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1135 | .id_table = mei_pci_tbl, |
| 1136 | .probe = mei_probe, |
| 1137 | .remove = __devexit_p(mei_remove), |
| 1138 | .shutdown = __devexit_p(mei_remove), |
| 1139 | .driver.pm = MEI_PM_OPS, |
| 1140 | }; |
| 1141 | |
Tomas Winkler | 6078188 | 2012-07-19 09:45:32 +0300 | [diff] [blame] | 1142 | module_pci_driver(mei_driver); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1143 | |
| 1144 | MODULE_AUTHOR("Intel Corporation"); |
| 1145 | MODULE_DESCRIPTION("Intel(R) Management Engine Interface"); |
| 1146 | MODULE_LICENSE("GPL v2"); |