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 | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 623 | rets = mei_io_cb_alloc_resp_buf(write_cb, dev->iamthif_mtu); |
| 624 | if (rets) |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 625 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 626 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 627 | write_cb->major_file_operations = MEI_IOCTL; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 628 | |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 629 | if (!list_empty(&dev->amthi_cmd_list.list) || |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 630 | dev->iamthif_state != MEI_IAMTHIF_IDLE) { |
| 631 | dev_dbg(&dev->pdev->dev, "amthi_state = %d\n", |
| 632 | (int) dev->iamthif_state); |
| 633 | dev_dbg(&dev->pdev->dev, "add amthi cb to amthi cmd waiting list\n"); |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 634 | list_add_tail(&write_cb->list, &dev->amthi_cmd_list.list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 635 | } else { |
| 636 | dev_dbg(&dev->pdev->dev, "call amthi write\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 637 | rets = mei_amthif_write(dev, write_cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 638 | |
| 639 | if (rets) { |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 640 | dev_err(&dev->pdev->dev, "amthi write failed with status = %d\n", |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 641 | rets); |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 642 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 643 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 644 | } |
| 645 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 646 | return length; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | write_cb->major_file_operations = MEI_WRITE; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 650 | |
| 651 | dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n", |
| 652 | cl->host_client_id, cl->me_client_id); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 653 | rets = mei_flow_ctrl_creds(dev, cl); |
| 654 | if (rets < 0) |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 655 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 656 | |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 657 | if (rets == 0 || dev->mei_host_buffer_is_empty == false) { |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 658 | write_cb->buf_idx = 0; |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 659 | mei_hdr.msg_complete = 0; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 660 | cl->writing_state = MEI_WRITING; |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 661 | goto out; |
| 662 | } |
| 663 | |
| 664 | dev->mei_host_buffer_is_empty = false; |
| 665 | if (length > mei_hbuf_max_data(dev)) { |
| 666 | mei_hdr.length = mei_hbuf_max_data(dev); |
| 667 | mei_hdr.msg_complete = 0; |
| 668 | } else { |
| 669 | mei_hdr.length = length; |
| 670 | mei_hdr.msg_complete = 1; |
| 671 | } |
| 672 | mei_hdr.host_addr = cl->host_client_id; |
| 673 | mei_hdr.me_addr = cl->me_client_id; |
| 674 | mei_hdr.reserved = 0; |
| 675 | dev_dbg(&dev->pdev->dev, "call mei_write_message header=%08x.\n", |
| 676 | *((u32 *) &mei_hdr)); |
| 677 | if (mei_write_message(dev, &mei_hdr, |
| 678 | write_cb->request_buffer.data, mei_hdr.length)) { |
| 679 | rets = -ENODEV; |
| 680 | goto err; |
| 681 | } |
| 682 | cl->writing_state = MEI_WRITING; |
| 683 | write_cb->buf_idx = mei_hdr.length; |
| 684 | |
| 685 | out: |
| 686 | if (mei_hdr.msg_complete) { |
| 687 | if (mei_flow_ctrl_reduce(dev, cl)) { |
| 688 | rets = -ENODEV; |
| 689 | goto err; |
| 690 | } |
| 691 | list_add_tail(&write_cb->list, &dev->write_waiting_list.list); |
| 692 | } else { |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 693 | list_add_tail(&write_cb->list, &dev->write_list.list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 694 | } |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 695 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 696 | mutex_unlock(&dev->device_lock); |
| 697 | return length; |
| 698 | |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 699 | err: |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 700 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 701 | mei_io_cb_free(write_cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 702 | return rets; |
| 703 | } |
| 704 | |
| 705 | |
| 706 | /** |
| 707 | * mei_ioctl - the IOCTL function |
| 708 | * |
| 709 | * @file: pointer to file structure |
| 710 | * @cmd: ioctl command |
| 711 | * @data: pointer to mei message structure |
| 712 | * |
| 713 | * returns 0 on success , <0 on error |
| 714 | */ |
| 715 | static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data) |
| 716 | { |
| 717 | struct mei_device *dev; |
| 718 | struct mei_cl *cl = file->private_data; |
| 719 | struct mei_connect_client_data *connect_data = NULL; |
| 720 | int rets; |
| 721 | |
| 722 | if (cmd != IOCTL_MEI_CONNECT_CLIENT) |
| 723 | return -EINVAL; |
| 724 | |
| 725 | if (WARN_ON(!cl || !cl->dev)) |
| 726 | return -ENODEV; |
| 727 | |
| 728 | dev = cl->dev; |
| 729 | |
| 730 | dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd); |
| 731 | |
| 732 | mutex_lock(&dev->device_lock); |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 733 | if (dev->dev_state != MEI_DEV_ENABLED) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 734 | rets = -ENODEV; |
| 735 | goto out; |
| 736 | } |
| 737 | |
| 738 | dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n"); |
| 739 | |
| 740 | connect_data = kzalloc(sizeof(struct mei_connect_client_data), |
| 741 | GFP_KERNEL); |
| 742 | if (!connect_data) { |
| 743 | rets = -ENOMEM; |
| 744 | goto out; |
| 745 | } |
| 746 | dev_dbg(&dev->pdev->dev, "copy connect data from user\n"); |
| 747 | if (copy_from_user(connect_data, (char __user *)data, |
| 748 | sizeof(struct mei_connect_client_data))) { |
| 749 | dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n"); |
| 750 | rets = -EFAULT; |
| 751 | goto out; |
| 752 | } |
| 753 | rets = mei_ioctl_connect_client(file, connect_data); |
| 754 | |
| 755 | /* if all is ok, copying the data back to user. */ |
| 756 | if (rets) |
| 757 | goto out; |
| 758 | |
| 759 | dev_dbg(&dev->pdev->dev, "copy connect data to user\n"); |
| 760 | if (copy_to_user((char __user *)data, connect_data, |
| 761 | sizeof(struct mei_connect_client_data))) { |
| 762 | dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n"); |
| 763 | rets = -EFAULT; |
| 764 | goto out; |
| 765 | } |
| 766 | |
| 767 | out: |
| 768 | kfree(connect_data); |
| 769 | mutex_unlock(&dev->device_lock); |
| 770 | return rets; |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * mei_compat_ioctl - the compat IOCTL function |
| 775 | * |
| 776 | * @file: pointer to file structure |
| 777 | * @cmd: ioctl command |
| 778 | * @data: pointer to mei message structure |
| 779 | * |
| 780 | * returns 0 on success , <0 on error |
| 781 | */ |
| 782 | #ifdef CONFIG_COMPAT |
| 783 | static long mei_compat_ioctl(struct file *file, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 784 | unsigned int cmd, unsigned long data) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 785 | { |
| 786 | return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data)); |
| 787 | } |
| 788 | #endif |
| 789 | |
| 790 | |
| 791 | /** |
| 792 | * mei_poll - the poll function |
| 793 | * |
| 794 | * @file: pointer to file structure |
| 795 | * @wait: pointer to poll_table structure |
| 796 | * |
| 797 | * returns poll mask |
| 798 | */ |
| 799 | static unsigned int mei_poll(struct file *file, poll_table *wait) |
| 800 | { |
| 801 | struct mei_cl *cl = file->private_data; |
| 802 | struct mei_device *dev; |
| 803 | unsigned int mask = 0; |
| 804 | |
| 805 | if (WARN_ON(!cl || !cl->dev)) |
| 806 | return mask; |
| 807 | |
| 808 | dev = cl->dev; |
| 809 | |
| 810 | mutex_lock(&dev->device_lock); |
| 811 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 812 | if (dev->dev_state != MEI_DEV_ENABLED) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 813 | goto out; |
| 814 | |
| 815 | |
| 816 | if (cl == &dev->iamthif_cl) { |
| 817 | mutex_unlock(&dev->device_lock); |
| 818 | poll_wait(file, &dev->iamthif_cl.wait, wait); |
| 819 | mutex_lock(&dev->device_lock); |
| 820 | if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE && |
| 821 | dev->iamthif_file_object == file) { |
| 822 | mask |= (POLLIN | POLLRDNORM); |
| 823 | dev_dbg(&dev->pdev->dev, "run next amthi cb\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 824 | mei_amthif_run_next_cmd(dev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 825 | } |
| 826 | goto out; |
| 827 | } |
| 828 | |
| 829 | mutex_unlock(&dev->device_lock); |
| 830 | poll_wait(file, &cl->tx_wait, wait); |
| 831 | mutex_lock(&dev->device_lock); |
| 832 | if (MEI_WRITE_COMPLETE == cl->writing_state) |
| 833 | mask |= (POLLIN | POLLRDNORM); |
| 834 | |
| 835 | out: |
| 836 | mutex_unlock(&dev->device_lock); |
| 837 | return mask; |
| 838 | } |
| 839 | |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 840 | /* |
| 841 | * file operations structure will be used for mei char device. |
| 842 | */ |
| 843 | static const struct file_operations mei_fops = { |
| 844 | .owner = THIS_MODULE, |
| 845 | .read = mei_read, |
| 846 | .unlocked_ioctl = mei_ioctl, |
| 847 | #ifdef CONFIG_COMPAT |
| 848 | .compat_ioctl = mei_compat_ioctl, |
| 849 | #endif |
| 850 | .open = mei_open, |
| 851 | .release = mei_release, |
| 852 | .write = mei_write, |
| 853 | .poll = mei_poll, |
| 854 | .llseek = no_llseek |
| 855 | }; |
| 856 | |
| 857 | |
| 858 | /* |
| 859 | * Misc Device Struct |
| 860 | */ |
| 861 | static struct miscdevice mei_misc_device = { |
Tomas Winkler | c38ea24 | 2012-04-02 20:32:39 +0300 | [diff] [blame] | 862 | .name = "mei", |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 863 | .fops = &mei_fops, |
| 864 | .minor = MISC_DYNAMIC_MINOR, |
| 865 | }; |
| 866 | |
| 867 | /** |
Tomas Winkler | 9a123f1 | 2012-08-06 15:23:55 +0300 | [diff] [blame] | 868 | * mei_quirk_probe - probe for devices that doesn't valid ME interface |
| 869 | * @pdev: PCI device structure |
| 870 | * @ent: entry into pci_device_table |
| 871 | * |
| 872 | * returns true if ME Interface is valid, false otherwise |
| 873 | */ |
| 874 | static bool __devinit mei_quirk_probe(struct pci_dev *pdev, |
| 875 | const struct pci_device_id *ent) |
| 876 | { |
| 877 | u32 reg; |
| 878 | if (ent->device == MEI_DEV_ID_PBG_1) { |
| 879 | pci_read_config_dword(pdev, 0x48, ®); |
| 880 | /* make sure that bit 9 is up and bit 10 is down */ |
| 881 | if ((reg & 0x600) == 0x200) { |
| 882 | dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n"); |
| 883 | return false; |
| 884 | } |
| 885 | } |
| 886 | return true; |
| 887 | } |
| 888 | /** |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 889 | * mei_probe - Device Initialization Routine |
| 890 | * |
| 891 | * @pdev: PCI device structure |
| 892 | * @ent: entry in kcs_pci_tbl |
| 893 | * |
| 894 | * returns 0 on success, <0 on failure. |
| 895 | */ |
| 896 | static int __devinit mei_probe(struct pci_dev *pdev, |
| 897 | const struct pci_device_id *ent) |
| 898 | { |
| 899 | struct mei_device *dev; |
| 900 | int err; |
| 901 | |
| 902 | mutex_lock(&mei_mutex); |
Tomas Winkler | 9a123f1 | 2012-08-06 15:23:55 +0300 | [diff] [blame] | 903 | |
| 904 | if (!mei_quirk_probe(pdev, ent)) { |
| 905 | err = -ENODEV; |
| 906 | goto end; |
| 907 | } |
| 908 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 909 | if (mei_pdev) { |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 910 | err = -EEXIST; |
| 911 | goto end; |
| 912 | } |
| 913 | /* enable pci dev */ |
| 914 | err = pci_enable_device(pdev); |
| 915 | if (err) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 916 | dev_err(&pdev->dev, "failed to enable pci device.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 917 | goto end; |
| 918 | } |
| 919 | /* set PCI host mastering */ |
| 920 | pci_set_master(pdev); |
| 921 | /* pci request regions for mei driver */ |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 922 | err = pci_request_regions(pdev, KBUILD_MODNAME); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 923 | if (err) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 924 | dev_err(&pdev->dev, "failed to get pci regions.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 925 | goto disable_device; |
| 926 | } |
| 927 | /* allocates and initializes the mei dev structure */ |
| 928 | dev = mei_device_init(pdev); |
| 929 | if (!dev) { |
| 930 | err = -ENOMEM; |
| 931 | goto release_regions; |
| 932 | } |
| 933 | /* mapping IO device memory */ |
| 934 | dev->mem_addr = pci_iomap(pdev, 0, 0); |
| 935 | if (!dev->mem_addr) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 936 | dev_err(&pdev->dev, "mapping I/O device memory failure.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 937 | err = -ENOMEM; |
| 938 | goto free_device; |
| 939 | } |
| 940 | pci_enable_msi(pdev); |
| 941 | |
| 942 | /* request and enable interrupt */ |
| 943 | if (pci_dev_msi_enabled(pdev)) |
| 944 | err = request_threaded_irq(pdev->irq, |
| 945 | NULL, |
| 946 | mei_interrupt_thread_handler, |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 947 | IRQF_ONESHOT, KBUILD_MODNAME, dev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 948 | else |
| 949 | err = request_threaded_irq(pdev->irq, |
| 950 | mei_interrupt_quick_handler, |
| 951 | mei_interrupt_thread_handler, |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 952 | IRQF_SHARED, KBUILD_MODNAME, dev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 953 | |
| 954 | if (err) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 955 | dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n", |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 956 | pdev->irq); |
Samuel Ortiz | 169dc38 | 2012-06-11 12:18:30 +0300 | [diff] [blame] | 957 | goto disable_msi; |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 958 | } |
| 959 | INIT_DELAYED_WORK(&dev->timer_work, mei_timer); |
| 960 | if (mei_hw_init(dev)) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 961 | dev_err(&pdev->dev, "init hw failure.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 962 | err = -ENODEV; |
| 963 | goto release_irq; |
| 964 | } |
| 965 | |
| 966 | err = misc_register(&mei_misc_device); |
| 967 | if (err) |
| 968 | goto release_irq; |
| 969 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 970 | mei_pdev = pdev; |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 971 | pci_set_drvdata(pdev, dev); |
| 972 | |
| 973 | |
| 974 | schedule_delayed_work(&dev->timer_work, HZ); |
| 975 | |
| 976 | mutex_unlock(&mei_mutex); |
| 977 | |
Tomas Winkler | 2f3d2b4 | 2012-03-19 22:38:13 +0200 | [diff] [blame] | 978 | pr_debug("initialization successful.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 979 | |
| 980 | return 0; |
| 981 | |
| 982 | release_irq: |
| 983 | /* disable interrupts */ |
| 984 | dev->host_hw_state = mei_hcsr_read(dev); |
| 985 | mei_disable_interrupts(dev); |
| 986 | flush_scheduled_work(); |
| 987 | free_irq(pdev->irq, dev); |
Samuel Ortiz | 169dc38 | 2012-06-11 12:18:30 +0300 | [diff] [blame] | 988 | disable_msi: |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 989 | pci_disable_msi(pdev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 990 | pci_iounmap(pdev, dev->mem_addr); |
| 991 | free_device: |
| 992 | kfree(dev); |
| 993 | release_regions: |
| 994 | pci_release_regions(pdev); |
| 995 | disable_device: |
| 996 | pci_disable_device(pdev); |
| 997 | end: |
| 998 | mutex_unlock(&mei_mutex); |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 999 | dev_err(&pdev->dev, "initialization failed.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1000 | return err; |
| 1001 | } |
| 1002 | |
| 1003 | /** |
| 1004 | * mei_remove - Device Removal Routine |
| 1005 | * |
| 1006 | * @pdev: PCI device structure |
| 1007 | * |
| 1008 | * mei_remove is called by the PCI subsystem to alert the driver |
| 1009 | * that it should release a PCI device. |
| 1010 | */ |
| 1011 | static void __devexit mei_remove(struct pci_dev *pdev) |
| 1012 | { |
| 1013 | struct mei_device *dev; |
| 1014 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 1015 | if (mei_pdev != pdev) |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1016 | return; |
| 1017 | |
| 1018 | dev = pci_get_drvdata(pdev); |
| 1019 | if (!dev) |
| 1020 | return; |
| 1021 | |
| 1022 | mutex_lock(&dev->device_lock); |
| 1023 | |
Tomas Winkler | c216fde | 2012-08-16 19:39:43 +0300 | [diff] [blame] | 1024 | cancel_delayed_work(&dev->timer_work); |
| 1025 | |
| 1026 | mei_wd_stop(dev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1027 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 1028 | mei_pdev = NULL; |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1029 | |
| 1030 | if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) { |
| 1031 | dev->iamthif_cl.state = MEI_FILE_DISCONNECTING; |
| 1032 | mei_disconnect_host_client(dev, &dev->iamthif_cl); |
| 1033 | } |
| 1034 | if (dev->wd_cl.state == MEI_FILE_CONNECTED) { |
| 1035 | dev->wd_cl.state = MEI_FILE_DISCONNECTING; |
| 1036 | mei_disconnect_host_client(dev, &dev->wd_cl); |
| 1037 | } |
| 1038 | |
| 1039 | /* Unregistering watchdog device */ |
Tomas Winkler | 70cd533 | 2011-12-22 18:50:50 +0200 | [diff] [blame] | 1040 | mei_watchdog_unregister(dev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1041 | |
| 1042 | /* remove entry if already in list */ |
| 1043 | dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n"); |
| 1044 | mei_remove_client_from_file_list(dev, dev->wd_cl.host_client_id); |
| 1045 | mei_remove_client_from_file_list(dev, dev->iamthif_cl.host_client_id); |
| 1046 | |
| 1047 | dev->iamthif_current_cb = NULL; |
| 1048 | dev->me_clients_num = 0; |
| 1049 | |
| 1050 | mutex_unlock(&dev->device_lock); |
| 1051 | |
| 1052 | flush_scheduled_work(); |
| 1053 | |
| 1054 | /* disable interrupts */ |
| 1055 | mei_disable_interrupts(dev); |
| 1056 | |
| 1057 | free_irq(pdev->irq, dev); |
| 1058 | pci_disable_msi(pdev); |
| 1059 | pci_set_drvdata(pdev, NULL); |
| 1060 | |
| 1061 | if (dev->mem_addr) |
| 1062 | pci_iounmap(pdev, dev->mem_addr); |
| 1063 | |
| 1064 | kfree(dev); |
| 1065 | |
| 1066 | pci_release_regions(pdev); |
| 1067 | pci_disable_device(pdev); |
Tomas Winkler | a44cab4 | 2012-05-29 16:39:11 +0300 | [diff] [blame] | 1068 | |
| 1069 | misc_deregister(&mei_misc_device); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1070 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1071 | #ifdef CONFIG_PM |
| 1072 | static int mei_pci_suspend(struct device *device) |
| 1073 | { |
| 1074 | struct pci_dev *pdev = to_pci_dev(device); |
| 1075 | struct mei_device *dev = pci_get_drvdata(pdev); |
| 1076 | int err; |
| 1077 | |
| 1078 | if (!dev) |
| 1079 | return -ENODEV; |
| 1080 | mutex_lock(&dev->device_lock); |
Tomas Winkler | c216fde | 2012-08-16 19:39:43 +0300 | [diff] [blame] | 1081 | |
| 1082 | cancel_delayed_work(&dev->timer_work); |
| 1083 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1084 | /* Stop watchdog if exists */ |
Tomas Winkler | c216fde | 2012-08-16 19:39:43 +0300 | [diff] [blame] | 1085 | err = mei_wd_stop(dev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1086 | /* Set new mei state */ |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 1087 | if (dev->dev_state == MEI_DEV_ENABLED || |
| 1088 | dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) { |
| 1089 | dev->dev_state = MEI_DEV_POWER_DOWN; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1090 | mei_reset(dev, 0); |
| 1091 | } |
| 1092 | mutex_unlock(&dev->device_lock); |
| 1093 | |
| 1094 | free_irq(pdev->irq, dev); |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 1095 | pci_disable_msi(pdev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1096 | |
| 1097 | return err; |
| 1098 | } |
| 1099 | |
| 1100 | static int mei_pci_resume(struct device *device) |
| 1101 | { |
| 1102 | struct pci_dev *pdev = to_pci_dev(device); |
| 1103 | struct mei_device *dev; |
| 1104 | int err; |
| 1105 | |
| 1106 | dev = pci_get_drvdata(pdev); |
| 1107 | if (!dev) |
| 1108 | return -ENODEV; |
| 1109 | |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 1110 | pci_enable_msi(pdev); |
| 1111 | |
| 1112 | /* request and enable interrupt */ |
| 1113 | if (pci_dev_msi_enabled(pdev)) |
| 1114 | err = request_threaded_irq(pdev->irq, |
| 1115 | NULL, |
| 1116 | mei_interrupt_thread_handler, |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 1117 | IRQF_ONESHOT, KBUILD_MODNAME, dev); |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 1118 | else |
| 1119 | err = request_threaded_irq(pdev->irq, |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1120 | mei_interrupt_quick_handler, |
| 1121 | mei_interrupt_thread_handler, |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 1122 | IRQF_SHARED, KBUILD_MODNAME, dev); |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 1123 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1124 | if (err) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 1125 | dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n", |
| 1126 | pdev->irq); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1127 | return err; |
| 1128 | } |
| 1129 | |
| 1130 | mutex_lock(&dev->device_lock); |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 1131 | dev->dev_state = MEI_DEV_POWER_UP; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1132 | mei_reset(dev, 1); |
| 1133 | mutex_unlock(&dev->device_lock); |
| 1134 | |
Oren Weil | 6d70e93 | 2011-09-07 09:03:14 +0300 | [diff] [blame] | 1135 | /* Start timer if stopped in suspend */ |
| 1136 | schedule_delayed_work(&dev->timer_work, HZ); |
| 1137 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1138 | return err; |
| 1139 | } |
| 1140 | static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume); |
| 1141 | #define MEI_PM_OPS (&mei_pm_ops) |
| 1142 | #else |
Randy Dunlap | 2d99036 | 2011-05-19 08:52:34 -0700 | [diff] [blame] | 1143 | #define MEI_PM_OPS NULL |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1144 | #endif /* CONFIG_PM */ |
| 1145 | /* |
| 1146 | * PCI driver structure |
| 1147 | */ |
| 1148 | static struct pci_driver mei_driver = { |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 1149 | .name = KBUILD_MODNAME, |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1150 | .id_table = mei_pci_tbl, |
| 1151 | .probe = mei_probe, |
| 1152 | .remove = __devexit_p(mei_remove), |
| 1153 | .shutdown = __devexit_p(mei_remove), |
| 1154 | .driver.pm = MEI_PM_OPS, |
| 1155 | }; |
| 1156 | |
Tomas Winkler | 6078188 | 2012-07-19 09:45:32 +0300 | [diff] [blame] | 1157 | module_pci_driver(mei_driver); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1158 | |
| 1159 | MODULE_AUTHOR("Intel Corporation"); |
| 1160 | MODULE_DESCRIPTION("Intel(R) Management Engine Interface"); |
| 1161 | MODULE_LICENSE("GPL v2"); |