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 | 5b6dca2 | 2015-07-23 15:08:44 +0300 | [diff] [blame] | 30 | #define MEI_UUID_NFC_INFO UUID_LE(0xd2de1625, 0x382d, 0x417d, \ |
| 31 | 0x48, 0xa4, 0xef, 0xab, 0xba, 0x8a, 0x12, 0x06) |
| 32 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 33 | static const uuid_le mei_nfc_info_guid = MEI_UUID_NFC_INFO; |
| 34 | |
| 35 | #define MEI_UUID_NFC_HCI UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50, \ |
| 36 | 0x94, 0xd4, 0x50, 0x26, 0x67, 0x23, 0x77, 0x5c) |
| 37 | |
Tomas Winkler | e97cdb3 | 2016-01-08 00:49:24 +0200 | [diff] [blame] | 38 | #define MEI_UUID_WD UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, \ |
| 39 | 0x89, 0x9D, 0xA9, 0x15, 0x14, 0xCB, 0x32, 0xAB) |
| 40 | |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 41 | #define MEI_UUID_ANY NULL_UUID_LE |
| 42 | |
Tomas Winkler | 5b6dca2 | 2015-07-23 15:08:44 +0300 | [diff] [blame] | 43 | /** |
Tomas Winkler | dd070a1 | 2015-07-23 15:08:45 +0300 | [diff] [blame] | 44 | * number_of_connections - determine whether an client be on the bus |
| 45 | * according number of connections |
| 46 | * We support only clients: |
| 47 | * 1. with single connection |
| 48 | * 2. and fixed clients (max_number_of_connections == 0) |
| 49 | * |
| 50 | * @cldev: me clients device |
| 51 | */ |
| 52 | static void number_of_connections(struct mei_cl_device *cldev) |
| 53 | { |
Tomas Winkler | 2bcfdc2 | 2016-01-07 14:46:34 +0200 | [diff] [blame] | 54 | dev_dbg(&cldev->dev, "running hook %s\n", __func__); |
Tomas Winkler | dd070a1 | 2015-07-23 15:08:45 +0300 | [diff] [blame] | 55 | |
| 56 | if (cldev->me_cl->props.max_number_of_connections > 1) |
| 57 | cldev->do_match = 0; |
| 58 | } |
| 59 | |
| 60 | /** |
Tomas Winkler | 5b6dca2 | 2015-07-23 15:08:44 +0300 | [diff] [blame] | 61 | * blacklist - blacklist a client from the bus |
| 62 | * |
| 63 | * @cldev: me clients device |
| 64 | */ |
| 65 | static void blacklist(struct mei_cl_device *cldev) |
| 66 | { |
Tomas Winkler | 2bcfdc2 | 2016-01-07 14:46:34 +0200 | [diff] [blame] | 67 | dev_dbg(&cldev->dev, "running hook %s\n", __func__); |
| 68 | |
Tomas Winkler | 5b6dca2 | 2015-07-23 15:08:44 +0300 | [diff] [blame] | 69 | cldev->do_match = 0; |
| 70 | } |
| 71 | |
Tomas Winkler | e97cdb3 | 2016-01-08 00:49:24 +0200 | [diff] [blame] | 72 | /** |
| 73 | * mei_wd - wd client on the bus, change protocol version |
| 74 | * as the API has changed. |
| 75 | * |
| 76 | * @cldev: me clients device |
| 77 | */ |
| 78 | #if IS_ENABLED(CONFIG_INTEL_MEI_ME) |
| 79 | #include <linux/pci.h> |
| 80 | #include "hw-me-regs.h" |
| 81 | static void mei_wd(struct mei_cl_device *cldev) |
| 82 | { |
| 83 | struct pci_dev *pdev = to_pci_dev(cldev->dev.parent); |
| 84 | |
| 85 | dev_dbg(&cldev->dev, "running hook %s\n", __func__); |
| 86 | if (pdev->device == MEI_DEV_ID_WPT_LP || |
| 87 | pdev->device == MEI_DEV_ID_SPT || |
| 88 | pdev->device == MEI_DEV_ID_SPT_H) |
| 89 | cldev->me_cl->props.protocol_version = 0x2; |
| 90 | |
| 91 | cldev->do_match = 1; |
| 92 | } |
| 93 | #else |
| 94 | static inline void mei_wd(struct mei_cl_device *cldev) {} |
| 95 | #endif /* CONFIG_INTEL_MEI_ME */ |
| 96 | |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 97 | struct mei_nfc_cmd { |
| 98 | u8 command; |
| 99 | u8 status; |
| 100 | u16 req_id; |
| 101 | u32 reserved; |
| 102 | u16 data_size; |
| 103 | u8 sub_command; |
| 104 | u8 data[]; |
| 105 | } __packed; |
| 106 | |
| 107 | struct mei_nfc_reply { |
| 108 | u8 command; |
| 109 | u8 status; |
| 110 | u16 req_id; |
| 111 | u32 reserved; |
| 112 | u16 data_size; |
| 113 | u8 sub_command; |
| 114 | u8 reply_status; |
| 115 | u8 data[]; |
| 116 | } __packed; |
| 117 | |
| 118 | struct mei_nfc_if_version { |
| 119 | u8 radio_version_sw[3]; |
| 120 | u8 reserved[3]; |
| 121 | u8 radio_version_hw[3]; |
| 122 | u8 i2c_addr; |
| 123 | u8 fw_ivn; |
| 124 | u8 vendor_id; |
| 125 | u8 radio_type; |
| 126 | } __packed; |
| 127 | |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 128 | |
| 129 | #define MEI_NFC_CMD_MAINTENANCE 0x00 |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 130 | #define MEI_NFC_SUBCMD_IF_VERSION 0x01 |
| 131 | |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame] | 132 | /* Vendors */ |
| 133 | #define MEI_NFC_VENDOR_INSIDE 0x00 |
| 134 | #define MEI_NFC_VENDOR_NXP 0x01 |
| 135 | |
| 136 | /* Radio types */ |
| 137 | #define MEI_NFC_VENDOR_INSIDE_UREAD 0x00 |
| 138 | #define MEI_NFC_VENDOR_NXP_PN544 0x01 |
| 139 | |
Tomas Winkler | b39910c | 2015-07-23 15:08:46 +0300 | [diff] [blame] | 140 | /** |
| 141 | * mei_nfc_if_version - get NFC interface version |
| 142 | * |
| 143 | * @cl: host client (nfc info) |
| 144 | * @ver: NFC interface version to be filled in |
| 145 | * |
| 146 | * Return: 0 on success; < 0 otherwise |
| 147 | */ |
| 148 | static int mei_nfc_if_version(struct mei_cl *cl, |
| 149 | struct mei_nfc_if_version *ver) |
Samuel Ortiz | 91a6b95 | 2013-04-11 03:03:30 +0200 | [diff] [blame] | 150 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 151 | struct mei_device *bus; |
Tomas Winkler | b39910c | 2015-07-23 15:08:46 +0300 | [diff] [blame] | 152 | struct mei_nfc_cmd cmd = { |
| 153 | .command = MEI_NFC_CMD_MAINTENANCE, |
| 154 | .data_size = 1, |
| 155 | .sub_command = MEI_NFC_SUBCMD_IF_VERSION, |
| 156 | }; |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 157 | struct mei_nfc_reply *reply = NULL; |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 158 | size_t if_version_length; |
| 159 | int bytes_recv, ret; |
| 160 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 161 | bus = cl->dev; |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 162 | |
Tomas Winkler | b39910c | 2015-07-23 15:08:46 +0300 | [diff] [blame] | 163 | WARN_ON(mutex_is_locked(&bus->device_lock)); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 164 | |
Tomas Winkler | be9b720 | 2015-05-07 15:54:04 +0300 | [diff] [blame] | 165 | 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] | 166 | if (ret < 0) { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 167 | dev_err(bus->dev, "Could not send IF version cmd\n"); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | /* to be sure on the stack we alloc memory */ |
| 172 | if_version_length = sizeof(struct mei_nfc_reply) + |
| 173 | sizeof(struct mei_nfc_if_version); |
| 174 | |
| 175 | reply = kzalloc(if_version_length, GFP_KERNEL); |
| 176 | if (!reply) |
| 177 | return -ENOMEM; |
| 178 | |
Tomas Winkler | b39910c | 2015-07-23 15:08:46 +0300 | [diff] [blame] | 179 | ret = 0; |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 180 | bytes_recv = __mei_cl_recv(cl, (u8 *)reply, if_version_length); |
| 181 | if (bytes_recv < 0 || bytes_recv < sizeof(struct mei_nfc_reply)) { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 182 | dev_err(bus->dev, "Could not read IF version\n"); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 183 | ret = -EIO; |
| 184 | goto err; |
| 185 | } |
| 186 | |
Tomas Winkler | b39910c | 2015-07-23 15:08:46 +0300 | [diff] [blame] | 187 | memcpy(ver, reply->data, sizeof(struct mei_nfc_if_version)); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 188 | |
Tomas Winkler | b39910c | 2015-07-23 15:08:46 +0300 | [diff] [blame] | 189 | dev_info(bus->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n", |
| 190 | ver->fw_ivn, ver->vendor_id, ver->radio_type); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 191 | |
| 192 | err: |
| 193 | kfree(reply); |
| 194 | return ret; |
| 195 | } |
| 196 | |
Tomas Winkler | b39910c | 2015-07-23 15:08:46 +0300 | [diff] [blame] | 197 | /** |
| 198 | * mei_nfc_radio_name - derive nfc radio name from the interface version |
| 199 | * |
| 200 | * @ver: NFC radio version |
| 201 | * |
| 202 | * Return: radio name string |
| 203 | */ |
| 204 | static const char *mei_nfc_radio_name(struct mei_nfc_if_version *ver) |
| 205 | { |
| 206 | |
| 207 | if (ver->vendor_id == MEI_NFC_VENDOR_INSIDE) { |
| 208 | if (ver->radio_type == MEI_NFC_VENDOR_INSIDE_UREAD) |
| 209 | return "microread"; |
| 210 | } |
| 211 | |
| 212 | if (ver->vendor_id == MEI_NFC_VENDOR_NXP) { |
| 213 | if (ver->radio_type == MEI_NFC_VENDOR_NXP_PN544) |
| 214 | return "pn544"; |
| 215 | } |
| 216 | |
| 217 | return NULL; |
| 218 | } |
| 219 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 220 | /** |
| 221 | * mei_nfc - The nfc fixup function. The function retrieves nfc radio |
| 222 | * name and set is as device attribute so we can load |
| 223 | * the proper device driver for it |
| 224 | * |
| 225 | * @cldev: me client device (nfc) |
| 226 | */ |
| 227 | static void mei_nfc(struct mei_cl_device *cldev) |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 228 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 229 | struct mei_device *bus; |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 230 | struct mei_cl *cl; |
| 231 | struct mei_me_client *me_cl = NULL; |
| 232 | struct mei_nfc_if_version ver; |
| 233 | const char *radio_name = NULL; |
Tomas Winkler | d320832 | 2014-08-24 12:08:55 +0300 | [diff] [blame] | 234 | int ret; |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 235 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 236 | bus = cldev->bus; |
Tomas Winkler | a176c24 | 2014-11-05 18:18:52 +0200 | [diff] [blame] | 237 | |
Tomas Winkler | 2bcfdc2 | 2016-01-07 14:46:34 +0200 | [diff] [blame] | 238 | dev_dbg(&cldev->dev, "running hook %s\n", __func__); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 239 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 240 | mutex_lock(&bus->device_lock); |
| 241 | /* we need to connect to INFO GUID */ |
Alexander Usyskin | 7851e00 | 2016-02-07 23:35:40 +0200 | [diff] [blame^] | 242 | cl = mei_cl_alloc_linked(bus); |
Tomas Winkler | 03b8d34 | 2015-02-10 10:39:44 +0200 | [diff] [blame] | 243 | if (IS_ERR(cl)) { |
| 244 | ret = PTR_ERR(cl); |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 245 | cl = NULL; |
| 246 | dev_err(bus->dev, "nfc hook alloc failed %d\n", ret); |
| 247 | goto out; |
Tomas Winkler | 03b8d34 | 2015-02-10 10:39:44 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 250 | me_cl = mei_me_cl_by_uuid(bus, &mei_nfc_info_guid); |
| 251 | if (!me_cl) { |
| 252 | ret = -ENOTTY; |
| 253 | dev_err(bus->dev, "Cannot find nfc info %d\n", ret); |
| 254 | goto out; |
| 255 | } |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 256 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 257 | ret = mei_cl_connect(cl, me_cl, NULL); |
| 258 | if (ret < 0) { |
| 259 | dev_err(&cldev->dev, "Can't connect to the NFC INFO ME ret = %d\n", |
| 260 | ret); |
| 261 | goto out; |
| 262 | } |
Tomas Winkler | 03b8d34 | 2015-02-10 10:39:44 +0200 | [diff] [blame] | 263 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 264 | mutex_unlock(&bus->device_lock); |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 265 | |
| 266 | ret = mei_nfc_if_version(cl, &ver); |
| 267 | if (ret) |
| 268 | goto disconnect; |
| 269 | |
| 270 | radio_name = mei_nfc_radio_name(&ver); |
| 271 | |
| 272 | if (!radio_name) { |
| 273 | ret = -ENOENT; |
| 274 | dev_err(&cldev->dev, "Can't get the NFC interface version ret = %d\n", |
| 275 | ret); |
| 276 | goto disconnect; |
| 277 | } |
| 278 | |
| 279 | dev_dbg(bus->dev, "nfc radio %s\n", radio_name); |
| 280 | strlcpy(cldev->name, radio_name, sizeof(cldev->name)); |
| 281 | |
| 282 | disconnect: |
| 283 | mutex_lock(&bus->device_lock); |
| 284 | if (mei_cl_disconnect(cl) < 0) |
| 285 | dev_err(bus->dev, "Can't disconnect the NFC INFO ME\n"); |
| 286 | |
| 287 | mei_cl_flush_queues(cl, NULL); |
| 288 | |
| 289 | out: |
| 290 | mei_cl_unlink(cl); |
| 291 | mutex_unlock(&bus->device_lock); |
| 292 | mei_me_cl_put(me_cl); |
| 293 | kfree(cl); |
| 294 | |
| 295 | if (ret) |
| 296 | cldev->do_match = 0; |
| 297 | |
| 298 | dev_dbg(bus->dev, "end of fixup match = %d\n", cldev->do_match); |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 299 | } |
Tomas Winkler | 4870569 | 2014-02-17 15:13:19 +0200 | [diff] [blame] | 300 | |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 301 | #define MEI_FIXUP(_uuid, _hook) { _uuid, _hook } |
| 302 | |
| 303 | static struct mei_fixup { |
| 304 | |
| 305 | const uuid_le uuid; |
| 306 | void (*hook)(struct mei_cl_device *cldev); |
Tomas Winkler | 5b6dca2 | 2015-07-23 15:08:44 +0300 | [diff] [blame] | 307 | } mei_fixups[] = { |
Tomas Winkler | dd070a1 | 2015-07-23 15:08:45 +0300 | [diff] [blame] | 308 | MEI_FIXUP(MEI_UUID_ANY, number_of_connections), |
Tomas Winkler | 5b6dca2 | 2015-07-23 15:08:44 +0300 | [diff] [blame] | 309 | MEI_FIXUP(MEI_UUID_NFC_INFO, blacklist), |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 310 | MEI_FIXUP(MEI_UUID_NFC_HCI, mei_nfc), |
Tomas Winkler | e97cdb3 | 2016-01-08 00:49:24 +0200 | [diff] [blame] | 311 | MEI_FIXUP(MEI_UUID_WD, mei_wd), |
Tomas Winkler | 5b6dca2 | 2015-07-23 15:08:44 +0300 | [diff] [blame] | 312 | }; |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 313 | |
| 314 | /** |
Tomas Winkler | ae48d74 | 2015-09-10 10:18:06 +0300 | [diff] [blame] | 315 | * mei_cldev_fixup - run fixup handlers |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 316 | * |
| 317 | * @cldev: me client device |
| 318 | */ |
Tomas Winkler | ae48d74 | 2015-09-10 10:18:06 +0300 | [diff] [blame] | 319 | void mei_cl_bus_dev_fixup(struct mei_cl_device *cldev) |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 320 | { |
| 321 | struct mei_fixup *f; |
| 322 | const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); |
| 323 | int i; |
| 324 | |
| 325 | for (i = 0; i < ARRAY_SIZE(mei_fixups); i++) { |
| 326 | |
| 327 | f = &mei_fixups[i]; |
| 328 | if (uuid_le_cmp(f->uuid, MEI_UUID_ANY) == 0 || |
| 329 | uuid_le_cmp(f->uuid, *uuid) == 0) |
| 330 | f->hook(cldev); |
| 331 | } |
| 332 | } |
Tomas Winkler | 4870569 | 2014-02-17 15:13:19 +0200 | [diff] [blame] | 333 | |