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> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/moduleparam.h> |
| 20 | #include <linux/device.h> |
| 21 | #include <linux/pci.h> |
| 22 | #include <linux/mei_cl_bus.h> |
| 23 | |
| 24 | #include "mei_dev.h" |
| 25 | #include "client.h" |
| 26 | |
| 27 | struct mei_nfc_cmd { |
| 28 | u8 command; |
| 29 | u8 status; |
| 30 | u16 req_id; |
| 31 | u32 reserved; |
| 32 | u16 data_size; |
| 33 | u8 sub_command; |
| 34 | u8 data[]; |
| 35 | } __packed; |
| 36 | |
| 37 | struct mei_nfc_reply { |
| 38 | u8 command; |
| 39 | u8 status; |
| 40 | u16 req_id; |
| 41 | u32 reserved; |
| 42 | u16 data_size; |
| 43 | u8 sub_command; |
| 44 | u8 reply_status; |
| 45 | u8 data[]; |
| 46 | } __packed; |
| 47 | |
| 48 | struct mei_nfc_if_version { |
| 49 | u8 radio_version_sw[3]; |
| 50 | u8 reserved[3]; |
| 51 | u8 radio_version_hw[3]; |
| 52 | u8 i2c_addr; |
| 53 | u8 fw_ivn; |
| 54 | u8 vendor_id; |
| 55 | u8 radio_type; |
| 56 | } __packed; |
| 57 | |
| 58 | struct mei_nfc_connect { |
| 59 | u8 fw_ivn; |
| 60 | u8 vendor_id; |
| 61 | } __packed; |
| 62 | |
| 63 | struct mei_nfc_connect_resp { |
| 64 | u8 fw_ivn; |
| 65 | u8 vendor_id; |
| 66 | u16 me_major; |
| 67 | u16 me_minor; |
| 68 | u16 me_hotfix; |
| 69 | u16 me_build; |
| 70 | } __packed; |
| 71 | |
| 72 | struct mei_nfc_hci_hdr { |
| 73 | u8 cmd; |
| 74 | u8 status; |
| 75 | u16 req_id; |
| 76 | u32 reserved; |
| 77 | u16 data_size; |
| 78 | } __packed; |
| 79 | |
| 80 | #define MEI_NFC_CMD_MAINTENANCE 0x00 |
| 81 | #define MEI_NFC_CMD_HCI_SEND 0x01 |
| 82 | #define MEI_NFC_CMD_HCI_RECV 0x02 |
| 83 | |
| 84 | #define MEI_NFC_SUBCMD_CONNECT 0x00 |
| 85 | #define MEI_NFC_SUBCMD_IF_VERSION 0x01 |
| 86 | |
| 87 | #define MEI_NFC_HEADER_SIZE 10 |
| 88 | |
| 89 | /** mei_nfc_dev - NFC mei device |
| 90 | * |
| 91 | * @cl: NFC host client |
| 92 | * @cl_info: NFC info host client |
| 93 | * @init_work: perform connection to the info client |
| 94 | * @fw_ivn: NFC Intervace Version Number |
| 95 | * @vendor_id: NFC manufacturer ID |
| 96 | * @radio_type: NFC radio type |
| 97 | */ |
| 98 | struct mei_nfc_dev { |
| 99 | struct mei_cl *cl; |
| 100 | struct mei_cl *cl_info; |
| 101 | struct work_struct init_work; |
| 102 | u8 fw_ivn; |
| 103 | u8 vendor_id; |
| 104 | u8 radio_type; |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame^] | 105 | char *bus_name; |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | static struct mei_nfc_dev nfc_dev; |
| 109 | |
| 110 | /* UUIDs for NFC F/W clients */ |
| 111 | const uuid_le mei_nfc_guid = UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50, |
| 112 | 0x94, 0xd4, 0x50, 0x26, |
| 113 | 0x67, 0x23, 0x77, 0x5c); |
| 114 | |
| 115 | static const uuid_le mei_nfc_info_guid = UUID_LE(0xd2de1625, 0x382d, 0x417d, |
| 116 | 0x48, 0xa4, 0xef, 0xab, |
| 117 | 0xba, 0x8a, 0x12, 0x06); |
| 118 | |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame^] | 119 | /* Vendors */ |
| 120 | #define MEI_NFC_VENDOR_INSIDE 0x00 |
| 121 | #define MEI_NFC_VENDOR_NXP 0x01 |
| 122 | |
| 123 | /* Radio types */ |
| 124 | #define MEI_NFC_VENDOR_INSIDE_UREAD 0x00 |
| 125 | #define MEI_NFC_VENDOR_NXP_PN544 0x01 |
| 126 | |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 127 | static void mei_nfc_free(struct mei_nfc_dev *ndev) |
| 128 | { |
| 129 | if (ndev->cl) { |
| 130 | list_del(&ndev->cl->device_link); |
| 131 | mei_cl_unlink(ndev->cl); |
| 132 | kfree(ndev->cl); |
| 133 | } |
| 134 | |
| 135 | if (ndev->cl_info) { |
| 136 | list_del(&ndev->cl_info->device_link); |
| 137 | mei_cl_unlink(ndev->cl_info); |
| 138 | kfree(ndev->cl_info); |
| 139 | } |
| 140 | } |
| 141 | |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame^] | 142 | static int mei_nfc_build_bus_name(struct mei_nfc_dev *ndev) |
| 143 | { |
| 144 | struct mei_device *dev; |
| 145 | |
| 146 | if (!ndev->cl) |
| 147 | return -ENODEV; |
| 148 | |
| 149 | dev = ndev->cl->dev; |
| 150 | |
| 151 | switch (ndev->vendor_id) { |
| 152 | case MEI_NFC_VENDOR_INSIDE: |
| 153 | switch (ndev->radio_type) { |
| 154 | case MEI_NFC_VENDOR_INSIDE_UREAD: |
| 155 | ndev->bus_name = "microread"; |
| 156 | return 0; |
| 157 | |
| 158 | default: |
| 159 | dev_err(&dev->pdev->dev, "Unknow radio type 0x%x\n", |
| 160 | ndev->radio_type); |
| 161 | |
| 162 | return -EINVAL; |
| 163 | } |
| 164 | |
| 165 | case MEI_NFC_VENDOR_NXP: |
| 166 | switch (ndev->radio_type) { |
| 167 | case MEI_NFC_VENDOR_NXP_PN544: |
| 168 | ndev->bus_name = "pn544"; |
| 169 | return 0; |
| 170 | default: |
| 171 | dev_err(&dev->pdev->dev, "Unknow radio type 0x%x\n", |
| 172 | ndev->radio_type); |
| 173 | |
| 174 | return -EINVAL; |
| 175 | } |
| 176 | |
| 177 | default: |
| 178 | dev_err(&dev->pdev->dev, "Unknow vendor ID 0x%x\n", |
| 179 | ndev->vendor_id); |
| 180 | |
| 181 | return -EINVAL; |
| 182 | } |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 187 | static int mei_nfc_if_version(struct mei_nfc_dev *ndev) |
| 188 | { |
| 189 | struct mei_device *dev; |
| 190 | struct mei_cl *cl; |
| 191 | |
| 192 | struct mei_nfc_cmd cmd; |
| 193 | struct mei_nfc_reply *reply = NULL; |
| 194 | struct mei_nfc_if_version *version; |
| 195 | size_t if_version_length; |
| 196 | int bytes_recv, ret; |
| 197 | |
| 198 | cl = ndev->cl_info; |
| 199 | dev = cl->dev; |
| 200 | |
| 201 | memset(&cmd, 0, sizeof(struct mei_nfc_cmd)); |
| 202 | cmd.command = MEI_NFC_CMD_MAINTENANCE; |
| 203 | cmd.data_size = 1; |
| 204 | cmd.sub_command = MEI_NFC_SUBCMD_IF_VERSION; |
| 205 | |
| 206 | ret = __mei_cl_send(cl, (u8 *)&cmd, sizeof(struct mei_nfc_cmd)); |
| 207 | if (ret < 0) { |
| 208 | dev_err(&dev->pdev->dev, "Could not send IF version cmd\n"); |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | /* to be sure on the stack we alloc memory */ |
| 213 | if_version_length = sizeof(struct mei_nfc_reply) + |
| 214 | sizeof(struct mei_nfc_if_version); |
| 215 | |
| 216 | reply = kzalloc(if_version_length, GFP_KERNEL); |
| 217 | if (!reply) |
| 218 | return -ENOMEM; |
| 219 | |
| 220 | bytes_recv = __mei_cl_recv(cl, (u8 *)reply, if_version_length); |
| 221 | if (bytes_recv < 0 || bytes_recv < sizeof(struct mei_nfc_reply)) { |
| 222 | dev_err(&dev->pdev->dev, "Could not read IF version\n"); |
| 223 | ret = -EIO; |
| 224 | goto err; |
| 225 | } |
| 226 | |
| 227 | version = (struct mei_nfc_if_version *)reply->data; |
| 228 | |
| 229 | ndev->fw_ivn = version->fw_ivn; |
| 230 | ndev->vendor_id = version->vendor_id; |
| 231 | ndev->radio_type = version->radio_type; |
| 232 | |
| 233 | err: |
| 234 | kfree(reply); |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | static void mei_nfc_init(struct work_struct *work) |
| 239 | { |
| 240 | struct mei_device *dev; |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame^] | 241 | struct mei_cl_device *cldev; |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 242 | struct mei_nfc_dev *ndev; |
| 243 | struct mei_cl *cl_info; |
| 244 | |
| 245 | ndev = container_of(work, struct mei_nfc_dev, init_work); |
| 246 | |
| 247 | cl_info = ndev->cl_info; |
| 248 | dev = cl_info->dev; |
| 249 | |
| 250 | mutex_lock(&dev->device_lock); |
| 251 | |
| 252 | if (mei_cl_connect(cl_info, NULL) < 0) { |
| 253 | mutex_unlock(&dev->device_lock); |
| 254 | dev_err(&dev->pdev->dev, |
| 255 | "Could not connect to the NFC INFO ME client"); |
| 256 | |
| 257 | goto err; |
| 258 | } |
| 259 | |
| 260 | mutex_unlock(&dev->device_lock); |
| 261 | |
| 262 | if (mei_nfc_if_version(ndev) < 0) { |
| 263 | dev_err(&dev->pdev->dev, "Could not get the NFC interfave version"); |
| 264 | |
| 265 | goto err; |
| 266 | } |
| 267 | |
| 268 | dev_info(&dev->pdev->dev, |
| 269 | "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n", |
| 270 | ndev->fw_ivn, ndev->vendor_id, ndev->radio_type); |
| 271 | |
| 272 | mutex_lock(&dev->device_lock); |
| 273 | |
| 274 | if (mei_cl_disconnect(cl_info) < 0) { |
| 275 | mutex_unlock(&dev->device_lock); |
| 276 | dev_err(&dev->pdev->dev, |
| 277 | "Could not disconnect the NFC INFO ME client"); |
| 278 | |
| 279 | goto err; |
| 280 | } |
| 281 | |
| 282 | mutex_unlock(&dev->device_lock); |
| 283 | |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame^] | 284 | if (mei_nfc_build_bus_name(ndev) < 0) { |
| 285 | dev_err(&dev->pdev->dev, |
| 286 | "Could not build the bus ID name\n"); |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | cldev = mei_cl_add_device(dev, mei_nfc_guid, ndev->bus_name, NULL); |
| 291 | if (!cldev) { |
| 292 | dev_err(&dev->pdev->dev, |
| 293 | "Could not add the NFC device to the MEI bus\n"); |
| 294 | |
| 295 | goto err; |
| 296 | } |
| 297 | |
| 298 | cldev->priv_data = ndev; |
| 299 | |
| 300 | |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 301 | return; |
| 302 | |
| 303 | err: |
| 304 | mei_nfc_free(ndev); |
| 305 | |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | |
| 310 | int mei_nfc_host_init(struct mei_device *dev) |
| 311 | { |
| 312 | struct mei_nfc_dev *ndev = &nfc_dev; |
| 313 | struct mei_cl *cl_info, *cl = NULL; |
| 314 | int i, ret; |
| 315 | |
| 316 | /* already initialzed */ |
| 317 | if (ndev->cl_info) |
| 318 | return 0; |
| 319 | |
| 320 | cl_info = mei_cl_allocate(dev); |
| 321 | cl = mei_cl_allocate(dev); |
| 322 | |
| 323 | if (!cl || !cl_info) { |
| 324 | ret = -ENOMEM; |
| 325 | goto err; |
| 326 | } |
| 327 | |
| 328 | /* check for valid client id */ |
| 329 | i = mei_me_cl_by_uuid(dev, &mei_nfc_info_guid); |
| 330 | if (i < 0) { |
| 331 | dev_info(&dev->pdev->dev, "nfc: failed to find the client\n"); |
| 332 | ret = -ENOENT; |
| 333 | goto err; |
| 334 | } |
| 335 | |
| 336 | cl_info->me_client_id = dev->me_clients[i].client_id; |
| 337 | |
| 338 | ret = mei_cl_link(cl_info, MEI_HOST_CLIENT_ID_ANY); |
| 339 | if (ret) |
| 340 | goto err; |
| 341 | |
| 342 | cl_info->device_uuid = mei_nfc_info_guid; |
| 343 | |
| 344 | list_add_tail(&cl_info->device_link, &dev->device_list); |
| 345 | |
| 346 | /* check for valid client id */ |
| 347 | i = mei_me_cl_by_uuid(dev, &mei_nfc_guid); |
| 348 | if (i < 0) { |
| 349 | dev_info(&dev->pdev->dev, "nfc: failed to find the client\n"); |
| 350 | ret = -ENOENT; |
| 351 | goto err; |
| 352 | } |
| 353 | |
| 354 | cl->me_client_id = dev->me_clients[i].client_id; |
| 355 | |
| 356 | ret = mei_cl_link(cl, MEI_HOST_CLIENT_ID_ANY); |
| 357 | if (ret) |
| 358 | goto err; |
| 359 | |
| 360 | cl->device_uuid = mei_nfc_guid; |
| 361 | |
| 362 | list_add_tail(&cl->device_link, &dev->device_list); |
| 363 | |
| 364 | ndev->cl_info = cl_info; |
| 365 | ndev->cl = cl; |
| 366 | |
| 367 | INIT_WORK(&ndev->init_work, mei_nfc_init); |
| 368 | schedule_work(&ndev->init_work); |
| 369 | |
| 370 | return 0; |
| 371 | |
| 372 | err: |
| 373 | mei_nfc_free(ndev); |
| 374 | |
| 375 | return ret; |
| 376 | } |
| 377 | |
| 378 | void mei_nfc_host_exit(void) |
| 379 | { |
| 380 | struct mei_nfc_dev *ndev = &nfc_dev; |
| 381 | |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame^] | 382 | if (ndev->cl && ndev->cl->device) |
| 383 | mei_cl_remove_device(ndev->cl->device); |
| 384 | |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 385 | mei_nfc_free(ndev); |
| 386 | } |