Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Intel Management Engine Interface (Intel MEI) Linux driver |
| 4 | * Copyright (c) 2003-2013, Intel Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/kernel.h> |
Samuel Ortiz | 36eda94 | 2013-04-11 03:03:31 +0200 | [diff] [blame] | 18 | #include <linux/sched.h> |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 19 | #include <linux/module.h> |
| 20 | #include <linux/moduleparam.h> |
| 21 | #include <linux/device.h> |
Tomas Winkler | 1f18035 | 2014-09-29 16:31:46 +0300 | [diff] [blame] | 22 | #include <linux/slab.h> |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame^] | 23 | #include <linux/uuid.h> |
Tomas Winkler | 1f18035 | 2014-09-29 16:31:46 +0300 | [diff] [blame] | 24 | |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 25 | #include <linux/mei_cl_bus.h> |
| 26 | |
| 27 | #include "mei_dev.h" |
| 28 | #include "client.h" |
| 29 | |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame^] | 30 | #define MEI_UUID_ANY NULL_UUID_LE |
| 31 | |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 32 | struct mei_nfc_cmd { |
| 33 | u8 command; |
| 34 | u8 status; |
| 35 | u16 req_id; |
| 36 | u32 reserved; |
| 37 | u16 data_size; |
| 38 | u8 sub_command; |
| 39 | u8 data[]; |
| 40 | } __packed; |
| 41 | |
| 42 | struct mei_nfc_reply { |
| 43 | u8 command; |
| 44 | u8 status; |
| 45 | u16 req_id; |
| 46 | u32 reserved; |
| 47 | u16 data_size; |
| 48 | u8 sub_command; |
| 49 | u8 reply_status; |
| 50 | u8 data[]; |
| 51 | } __packed; |
| 52 | |
| 53 | struct mei_nfc_if_version { |
| 54 | u8 radio_version_sw[3]; |
| 55 | u8 reserved[3]; |
| 56 | u8 radio_version_hw[3]; |
| 57 | u8 i2c_addr; |
| 58 | u8 fw_ivn; |
| 59 | u8 vendor_id; |
| 60 | u8 radio_type; |
| 61 | } __packed; |
| 62 | |
| 63 | struct mei_nfc_connect { |
| 64 | u8 fw_ivn; |
| 65 | u8 vendor_id; |
| 66 | } __packed; |
| 67 | |
| 68 | struct mei_nfc_connect_resp { |
| 69 | u8 fw_ivn; |
| 70 | u8 vendor_id; |
| 71 | u16 me_major; |
| 72 | u16 me_minor; |
| 73 | u16 me_hotfix; |
| 74 | u16 me_build; |
| 75 | } __packed; |
| 76 | |
| 77 | struct mei_nfc_hci_hdr { |
| 78 | u8 cmd; |
| 79 | u8 status; |
| 80 | u16 req_id; |
| 81 | u32 reserved; |
| 82 | u16 data_size; |
| 83 | } __packed; |
| 84 | |
| 85 | #define MEI_NFC_CMD_MAINTENANCE 0x00 |
| 86 | #define MEI_NFC_CMD_HCI_SEND 0x01 |
| 87 | #define MEI_NFC_CMD_HCI_RECV 0x02 |
| 88 | |
| 89 | #define MEI_NFC_SUBCMD_CONNECT 0x00 |
| 90 | #define MEI_NFC_SUBCMD_IF_VERSION 0x01 |
| 91 | |
| 92 | #define MEI_NFC_HEADER_SIZE 10 |
| 93 | |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 94 | /** |
| 95 | * struct mei_nfc_dev - NFC mei device |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 96 | * |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 97 | * @me_cl: NFC me client |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 98 | * @cl: NFC host client |
| 99 | * @cl_info: NFC info host client |
| 100 | * @init_work: perform connection to the info client |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 101 | * @fw_ivn: NFC Interface Version Number |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 102 | * @vendor_id: NFC manufacturer ID |
| 103 | * @radio_type: NFC radio type |
Alexander Usyskin | ce23139 | 2014-09-29 16:31:50 +0300 | [diff] [blame] | 104 | * @bus_name: bus name |
| 105 | * |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 106 | */ |
| 107 | struct mei_nfc_dev { |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 108 | struct mei_me_client *me_cl; |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 109 | struct mei_cl *cl; |
| 110 | struct mei_cl *cl_info; |
| 111 | struct work_struct init_work; |
| 112 | u8 fw_ivn; |
| 113 | u8 vendor_id; |
| 114 | u8 radio_type; |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame] | 115 | char *bus_name; |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 116 | }; |
| 117 | |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 118 | /* UUIDs for NFC F/W clients */ |
| 119 | const uuid_le mei_nfc_guid = UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50, |
| 120 | 0x94, 0xd4, 0x50, 0x26, |
| 121 | 0x67, 0x23, 0x77, 0x5c); |
| 122 | |
| 123 | static const uuid_le mei_nfc_info_guid = UUID_LE(0xd2de1625, 0x382d, 0x417d, |
| 124 | 0x48, 0xa4, 0xef, 0xab, |
| 125 | 0xba, 0x8a, 0x12, 0x06); |
| 126 | |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame] | 127 | /* Vendors */ |
| 128 | #define MEI_NFC_VENDOR_INSIDE 0x00 |
| 129 | #define MEI_NFC_VENDOR_NXP 0x01 |
| 130 | |
| 131 | /* Radio types */ |
| 132 | #define MEI_NFC_VENDOR_INSIDE_UREAD 0x00 |
| 133 | #define MEI_NFC_VENDOR_NXP_PN544 0x01 |
| 134 | |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 135 | static void mei_nfc_free(struct mei_nfc_dev *ndev) |
| 136 | { |
Tomas Winkler | a176c24 | 2014-11-05 18:18:52 +0200 | [diff] [blame] | 137 | if (!ndev) |
| 138 | return; |
| 139 | |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 140 | if (ndev->cl) { |
| 141 | list_del(&ndev->cl->device_link); |
| 142 | mei_cl_unlink(ndev->cl); |
| 143 | kfree(ndev->cl); |
| 144 | } |
| 145 | |
| 146 | if (ndev->cl_info) { |
| 147 | list_del(&ndev->cl_info->device_link); |
| 148 | mei_cl_unlink(ndev->cl_info); |
| 149 | kfree(ndev->cl_info); |
| 150 | } |
Tomas Winkler | 2753ff5 | 2013-06-10 10:10:26 +0300 | [diff] [blame] | 151 | |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 152 | mei_me_cl_put(ndev->me_cl); |
Tomas Winkler | a176c24 | 2014-11-05 18:18:52 +0200 | [diff] [blame] | 153 | kfree(ndev); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame] | 156 | static int mei_nfc_build_bus_name(struct mei_nfc_dev *ndev) |
| 157 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 158 | struct mei_device *bus; |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame] | 159 | |
| 160 | if (!ndev->cl) |
| 161 | return -ENODEV; |
| 162 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 163 | bus = ndev->cl->dev; |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame] | 164 | |
| 165 | switch (ndev->vendor_id) { |
| 166 | case MEI_NFC_VENDOR_INSIDE: |
| 167 | switch (ndev->radio_type) { |
| 168 | case MEI_NFC_VENDOR_INSIDE_UREAD: |
| 169 | ndev->bus_name = "microread"; |
| 170 | return 0; |
| 171 | |
| 172 | default: |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 173 | dev_err(bus->dev, "Unknown radio type 0x%x\n", |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame] | 174 | ndev->radio_type); |
| 175 | |
| 176 | return -EINVAL; |
| 177 | } |
| 178 | |
| 179 | case MEI_NFC_VENDOR_NXP: |
| 180 | switch (ndev->radio_type) { |
| 181 | case MEI_NFC_VENDOR_NXP_PN544: |
| 182 | ndev->bus_name = "pn544"; |
| 183 | return 0; |
| 184 | default: |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 185 | dev_err(bus->dev, "Unknown radio type 0x%x\n", |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame] | 186 | ndev->radio_type); |
| 187 | |
| 188 | return -EINVAL; |
| 189 | } |
| 190 | |
| 191 | default: |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 192 | dev_err(bus->dev, "Unknown vendor ID 0x%x\n", |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame] | 193 | ndev->vendor_id); |
| 194 | |
| 195 | return -EINVAL; |
| 196 | } |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 201 | static int mei_nfc_if_version(struct mei_nfc_dev *ndev) |
| 202 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 203 | struct mei_device *bus; |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 204 | struct mei_cl *cl; |
| 205 | |
| 206 | struct mei_nfc_cmd cmd; |
| 207 | struct mei_nfc_reply *reply = NULL; |
| 208 | struct mei_nfc_if_version *version; |
| 209 | size_t if_version_length; |
| 210 | int bytes_recv, ret; |
| 211 | |
| 212 | cl = ndev->cl_info; |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 213 | bus = cl->dev; |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 214 | |
| 215 | memset(&cmd, 0, sizeof(struct mei_nfc_cmd)); |
| 216 | cmd.command = MEI_NFC_CMD_MAINTENANCE; |
| 217 | cmd.data_size = 1; |
| 218 | cmd.sub_command = MEI_NFC_SUBCMD_IF_VERSION; |
| 219 | |
Tomas Winkler | be9b720 | 2015-05-07 15:54:04 +0300 | [diff] [blame] | 220 | ret = __mei_cl_send(cl, (u8 *)&cmd, sizeof(struct mei_nfc_cmd), 1); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 221 | if (ret < 0) { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 222 | dev_err(bus->dev, "Could not send IF version cmd\n"); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 223 | return ret; |
| 224 | } |
| 225 | |
| 226 | /* to be sure on the stack we alloc memory */ |
| 227 | if_version_length = sizeof(struct mei_nfc_reply) + |
| 228 | sizeof(struct mei_nfc_if_version); |
| 229 | |
| 230 | reply = kzalloc(if_version_length, GFP_KERNEL); |
| 231 | if (!reply) |
| 232 | return -ENOMEM; |
| 233 | |
| 234 | bytes_recv = __mei_cl_recv(cl, (u8 *)reply, if_version_length); |
| 235 | if (bytes_recv < 0 || bytes_recv < sizeof(struct mei_nfc_reply)) { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 236 | dev_err(bus->dev, "Could not read IF version\n"); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 237 | ret = -EIO; |
| 238 | goto err; |
| 239 | } |
| 240 | |
| 241 | version = (struct mei_nfc_if_version *)reply->data; |
| 242 | |
| 243 | ndev->fw_ivn = version->fw_ivn; |
| 244 | ndev->vendor_id = version->vendor_id; |
| 245 | ndev->radio_type = version->radio_type; |
| 246 | |
| 247 | err: |
| 248 | kfree(reply); |
| 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | static void mei_nfc_init(struct work_struct *work) |
| 253 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 254 | struct mei_device *bus; |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame] | 255 | struct mei_cl_device *cldev; |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 256 | struct mei_nfc_dev *ndev; |
| 257 | struct mei_cl *cl_info; |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 258 | struct mei_me_client *me_cl_info; |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 259 | |
| 260 | ndev = container_of(work, struct mei_nfc_dev, init_work); |
| 261 | |
| 262 | cl_info = ndev->cl_info; |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 263 | bus = cl_info->dev; |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 264 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 265 | mutex_lock(&bus->device_lock); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 266 | |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 267 | /* check for valid client id */ |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 268 | me_cl_info = mei_me_cl_by_uuid(bus, &mei_nfc_info_guid); |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 269 | if (!me_cl_info) { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 270 | mutex_unlock(&bus->device_lock); |
| 271 | dev_info(bus->dev, "nfc: failed to find the info client\n"); |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 272 | goto err; |
| 273 | } |
| 274 | |
| 275 | if (mei_cl_connect(cl_info, me_cl_info, NULL) < 0) { |
| 276 | mei_me_cl_put(me_cl_info); |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 277 | mutex_unlock(&bus->device_lock); |
| 278 | dev_err(bus->dev, "Could not connect to the NFC INFO ME client"); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 279 | |
| 280 | goto err; |
| 281 | } |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 282 | mei_me_cl_put(me_cl_info); |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 283 | mutex_unlock(&bus->device_lock); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 284 | |
| 285 | if (mei_nfc_if_version(ndev) < 0) { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 286 | dev_err(bus->dev, "Could not get the NFC interface version"); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 287 | |
| 288 | goto err; |
| 289 | } |
| 290 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 291 | dev_info(bus->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n", |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 292 | ndev->fw_ivn, ndev->vendor_id, ndev->radio_type); |
| 293 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 294 | mutex_lock(&bus->device_lock); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 295 | |
| 296 | if (mei_cl_disconnect(cl_info) < 0) { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 297 | mutex_unlock(&bus->device_lock); |
| 298 | dev_err(bus->dev, "Could not disconnect the NFC INFO ME client"); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 299 | |
| 300 | goto err; |
| 301 | } |
| 302 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 303 | mutex_unlock(&bus->device_lock); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 304 | |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame] | 305 | if (mei_nfc_build_bus_name(ndev) < 0) { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 306 | dev_err(bus->dev, "Could not build the bus ID name\n"); |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame] | 307 | return; |
| 308 | } |
| 309 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 310 | cldev = mei_cl_add_device(bus, ndev->me_cl, ndev->cl, |
Tomas Winkler | be9b720 | 2015-05-07 15:54:04 +0300 | [diff] [blame] | 311 | ndev->bus_name); |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame] | 312 | if (!cldev) { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 313 | dev_err(bus->dev, "Could not add the NFC device to the MEI bus\n"); |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame] | 314 | |
| 315 | goto err; |
| 316 | } |
| 317 | |
| 318 | cldev->priv_data = ndev; |
| 319 | |
| 320 | |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 321 | return; |
| 322 | |
| 323 | err: |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 324 | mutex_lock(&bus->device_lock); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 325 | mei_nfc_free(ndev); |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 326 | mutex_unlock(&bus->device_lock); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 327 | |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 331 | int mei_nfc_host_init(struct mei_device *bus, struct mei_me_client *me_cl) |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 332 | { |
Tomas Winkler | a176c24 | 2014-11-05 18:18:52 +0200 | [diff] [blame] | 333 | struct mei_nfc_dev *ndev; |
Tomas Winkler | 03b8d34 | 2015-02-10 10:39:44 +0200 | [diff] [blame] | 334 | struct mei_cl *cl_info, *cl; |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 335 | int ret; |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 336 | |
Tomas Winkler | a176c24 | 2014-11-05 18:18:52 +0200 | [diff] [blame] | 337 | |
| 338 | /* in case of internal reset bail out |
| 339 | * as the device is already setup |
| 340 | */ |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 341 | cl = mei_cl_bus_find_cl_by_uuid(bus, mei_nfc_guid); |
Tomas Winkler | a176c24 | 2014-11-05 18:18:52 +0200 | [diff] [blame] | 342 | if (cl) |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 343 | return 0; |
| 344 | |
Tomas Winkler | a176c24 | 2014-11-05 18:18:52 +0200 | [diff] [blame] | 345 | ndev = kzalloc(sizeof(struct mei_nfc_dev), GFP_KERNEL); |
| 346 | if (!ndev) { |
| 347 | ret = -ENOMEM; |
| 348 | goto err; |
| 349 | } |
| 350 | |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 351 | ndev->me_cl = mei_me_cl_get(me_cl); |
| 352 | if (!ndev->me_cl) { |
| 353 | ret = -ENODEV; |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 354 | goto err; |
| 355 | } |
| 356 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 357 | cl_info = mei_cl_alloc_linked(bus, MEI_HOST_CLIENT_ID_ANY); |
Tomas Winkler | 03b8d34 | 2015-02-10 10:39:44 +0200 | [diff] [blame] | 358 | if (IS_ERR(cl_info)) { |
| 359 | ret = PTR_ERR(cl_info); |
| 360 | goto err; |
| 361 | } |
| 362 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 363 | list_add_tail(&cl_info->device_link, &bus->device_list); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 364 | |
Tomas Winkler | 03b8d34 | 2015-02-10 10:39:44 +0200 | [diff] [blame] | 365 | ndev->cl_info = cl_info; |
| 366 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 367 | cl = mei_cl_alloc_linked(bus, MEI_HOST_CLIENT_ID_ANY); |
Tomas Winkler | 03b8d34 | 2015-02-10 10:39:44 +0200 | [diff] [blame] | 368 | if (IS_ERR(cl)) { |
| 369 | ret = PTR_ERR(cl); |
| 370 | goto err; |
| 371 | } |
| 372 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 373 | list_add_tail(&cl->device_link, &bus->device_list); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 374 | |
Tomas Winkler | 03b8d34 | 2015-02-10 10:39:44 +0200 | [diff] [blame] | 375 | ndev->cl = cl; |
| 376 | |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 377 | INIT_WORK(&ndev->init_work, mei_nfc_init); |
| 378 | schedule_work(&ndev->init_work); |
| 379 | |
| 380 | return 0; |
| 381 | |
| 382 | err: |
| 383 | mei_nfc_free(ndev); |
| 384 | |
| 385 | return ret; |
| 386 | } |
| 387 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 388 | void mei_nfc_host_exit(struct mei_device *bus) |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 389 | { |
Tomas Winkler | a176c24 | 2014-11-05 18:18:52 +0200 | [diff] [blame] | 390 | struct mei_nfc_dev *ndev; |
| 391 | struct mei_cl *cl; |
| 392 | struct mei_cl_device *cldev; |
Tomas Winkler | 92db155 | 2014-09-29 16:31:37 +0300 | [diff] [blame] | 393 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 394 | cl = mei_cl_bus_find_cl_by_uuid(bus, mei_nfc_guid); |
Tomas Winkler | a176c24 | 2014-11-05 18:18:52 +0200 | [diff] [blame] | 395 | if (!cl) |
| 396 | return; |
| 397 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 398 | cldev = cl->cldev; |
Tomas Winkler | a176c24 | 2014-11-05 18:18:52 +0200 | [diff] [blame] | 399 | if (!cldev) |
| 400 | return; |
| 401 | |
| 402 | ndev = (struct mei_nfc_dev *)cldev->priv_data; |
| 403 | if (ndev) |
| 404 | cancel_work_sync(&ndev->init_work); |
| 405 | |
| 406 | cldev->priv_data = NULL; |
| 407 | |
Tomas Winkler | a176c24 | 2014-11-05 18:18:52 +0200 | [diff] [blame] | 408 | /* Need to remove the device here |
| 409 | * since mei_nfc_free will unlink the clients |
| 410 | */ |
| 411 | mei_cl_remove_device(cldev); |
Tomas Winkler | 4f27395 | 2015-07-08 00:22:03 +0300 | [diff] [blame] | 412 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 413 | mutex_lock(&bus->device_lock); |
Tomas Winkler | a176c24 | 2014-11-05 18:18:52 +0200 | [diff] [blame] | 414 | mei_nfc_free(ndev); |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 415 | mutex_unlock(&bus->device_lock); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 416 | } |
Tomas Winkler | 4870569 | 2014-02-17 15:13:19 +0200 | [diff] [blame] | 417 | |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame^] | 418 | #define MEI_FIXUP(_uuid, _hook) { _uuid, _hook } |
| 419 | |
| 420 | static struct mei_fixup { |
| 421 | |
| 422 | const uuid_le uuid; |
| 423 | void (*hook)(struct mei_cl_device *cldev); |
| 424 | } mei_fixups[] = {}; |
| 425 | |
| 426 | /** |
| 427 | * mei_cl_dev_fixup - run fixup handlers |
| 428 | * |
| 429 | * @cldev: me client device |
| 430 | */ |
| 431 | void mei_cl_dev_fixup(struct mei_cl_device *cldev) |
| 432 | { |
| 433 | struct mei_fixup *f; |
| 434 | const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); |
| 435 | int i; |
| 436 | |
| 437 | for (i = 0; i < ARRAY_SIZE(mei_fixups); i++) { |
| 438 | |
| 439 | f = &mei_fixups[i]; |
| 440 | if (uuid_le_cmp(f->uuid, MEI_UUID_ANY) == 0 || |
| 441 | uuid_le_cmp(f->uuid, *uuid) == 0) |
| 442 | f->hook(cldev); |
| 443 | } |
| 444 | } |
Tomas Winkler | 4870569 | 2014-02-17 15:13:19 +0200 | [diff] [blame] | 445 | |