3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2013, Intel Corporation.
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.
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
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/device.h>
22 #include <linux/slab.h>
23 #include <linux/uuid.h>
25 #include <linux/mei_cl_bus.h>
30 #define MEI_UUID_NFC_INFO UUID_LE(0xd2de1625, 0x382d, 0x417d, \
31 0x48, 0xa4, 0xef, 0xab, 0xba, 0x8a, 0x12, 0x06)
33 #define MEI_UUID_ANY NULL_UUID_LE
36 * number_of_connections - determine whether an client be on the bus
37 * according number of connections
38 * We support only clients:
39 * 1. with single connection
40 * 2. and fixed clients (max_number_of_connections == 0)
42 * @cldev: me clients device
44 static void number_of_connections(struct mei_cl_device *cldev)
46 dev_dbg(&cldev->dev, "running hook %s on %pUl\n",
47 __func__, mei_me_cl_uuid(cldev->me_cl));
49 if (cldev->me_cl->props.max_number_of_connections > 1)
54 * blacklist - blacklist a client from the bus
56 * @cldev: me clients device
58 static void blacklist(struct mei_cl_device *cldev)
60 dev_dbg(&cldev->dev, "running hook %s on %pUl\n",
61 __func__, mei_me_cl_uuid(cldev->me_cl));
75 struct mei_nfc_reply {
86 struct mei_nfc_if_version {
87 u8 radio_version_sw[3];
89 u8 radio_version_hw[3];
96 struct mei_nfc_connect {
101 struct mei_nfc_connect_resp {
110 struct mei_nfc_hci_hdr {
118 #define MEI_NFC_CMD_MAINTENANCE 0x00
119 #define MEI_NFC_CMD_HCI_SEND 0x01
120 #define MEI_NFC_CMD_HCI_RECV 0x02
122 #define MEI_NFC_SUBCMD_CONNECT 0x00
123 #define MEI_NFC_SUBCMD_IF_VERSION 0x01
125 #define MEI_NFC_HEADER_SIZE 10
128 * struct mei_nfc_dev - NFC mei device
130 * @me_cl: NFC me client
131 * @cl: NFC host client
132 * @cl_info: NFC info host client
133 * @init_work: perform connection to the info client
134 * @fw_ivn: NFC Interface Version Number
135 * @vendor_id: NFC manufacturer ID
136 * @radio_type: NFC radio type
137 * @bus_name: bus name
141 struct mei_me_client *me_cl;
143 struct mei_cl *cl_info;
144 struct work_struct init_work;
148 const char *bus_name;
151 /* UUIDs for NFC F/W clients */
152 const uuid_le mei_nfc_guid = UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50,
153 0x94, 0xd4, 0x50, 0x26,
154 0x67, 0x23, 0x77, 0x5c);
156 static const uuid_le mei_nfc_info_guid = MEI_UUID_NFC_INFO;
159 #define MEI_NFC_VENDOR_INSIDE 0x00
160 #define MEI_NFC_VENDOR_NXP 0x01
163 #define MEI_NFC_VENDOR_INSIDE_UREAD 0x00
164 #define MEI_NFC_VENDOR_NXP_PN544 0x01
166 static void mei_nfc_free(struct mei_nfc_dev *ndev)
172 list_del(&ndev->cl->device_link);
173 mei_cl_unlink(ndev->cl);
178 list_del(&ndev->cl_info->device_link);
179 mei_cl_unlink(ndev->cl_info);
180 kfree(ndev->cl_info);
183 mei_me_cl_put(ndev->me_cl);
188 * mei_nfc_if_version - get NFC interface version
190 * @cl: host client (nfc info)
191 * @ver: NFC interface version to be filled in
193 * Return: 0 on success; < 0 otherwise
195 static int mei_nfc_if_version(struct mei_cl *cl,
196 struct mei_nfc_if_version *ver)
198 struct mei_device *bus;
199 struct mei_nfc_cmd cmd = {
200 .command = MEI_NFC_CMD_MAINTENANCE,
202 .sub_command = MEI_NFC_SUBCMD_IF_VERSION,
204 struct mei_nfc_reply *reply = NULL;
205 size_t if_version_length;
210 WARN_ON(mutex_is_locked(&bus->device_lock));
212 ret = __mei_cl_send(cl, (u8 *)&cmd, sizeof(struct mei_nfc_cmd), 1);
214 dev_err(bus->dev, "Could not send IF version cmd\n");
218 /* to be sure on the stack we alloc memory */
219 if_version_length = sizeof(struct mei_nfc_reply) +
220 sizeof(struct mei_nfc_if_version);
222 reply = kzalloc(if_version_length, GFP_KERNEL);
227 bytes_recv = __mei_cl_recv(cl, (u8 *)reply, if_version_length);
228 if (bytes_recv < 0 || bytes_recv < sizeof(struct mei_nfc_reply)) {
229 dev_err(bus->dev, "Could not read IF version\n");
234 memcpy(ver, reply->data, sizeof(struct mei_nfc_if_version));
236 dev_info(bus->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n",
237 ver->fw_ivn, ver->vendor_id, ver->radio_type);
245 * mei_nfc_radio_name - derive nfc radio name from the interface version
247 * @ver: NFC radio version
249 * Return: radio name string
251 static const char *mei_nfc_radio_name(struct mei_nfc_if_version *ver)
254 if (ver->vendor_id == MEI_NFC_VENDOR_INSIDE) {
255 if (ver->radio_type == MEI_NFC_VENDOR_INSIDE_UREAD)
259 if (ver->vendor_id == MEI_NFC_VENDOR_NXP) {
260 if (ver->radio_type == MEI_NFC_VENDOR_NXP_PN544)
267 static void mei_nfc_init(struct work_struct *work)
269 struct mei_device *bus;
270 struct mei_cl_device *cldev;
271 struct mei_nfc_dev *ndev;
272 struct mei_cl *cl_info;
273 struct mei_me_client *me_cl_info;
274 struct mei_nfc_if_version version;
276 ndev = container_of(work, struct mei_nfc_dev, init_work);
278 cl_info = ndev->cl_info;
281 mutex_lock(&bus->device_lock);
283 /* check for valid client id */
284 me_cl_info = mei_me_cl_by_uuid(bus, &mei_nfc_info_guid);
286 mutex_unlock(&bus->device_lock);
287 dev_info(bus->dev, "nfc: failed to find the info client\n");
291 if (mei_cl_connect(cl_info, me_cl_info, NULL) < 0) {
292 mei_me_cl_put(me_cl_info);
293 mutex_unlock(&bus->device_lock);
294 dev_err(bus->dev, "Could not connect to the NFC INFO ME client");
298 mei_me_cl_put(me_cl_info);
299 mutex_unlock(&bus->device_lock);
301 if (mei_nfc_if_version(cl_info, &version) < 0) {
302 dev_err(bus->dev, "Could not get the NFC interface version");
307 ndev->fw_ivn = version.fw_ivn;
308 ndev->vendor_id = version.vendor_id;
309 ndev->radio_type = version.radio_type;
311 dev_info(bus->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n",
312 ndev->fw_ivn, ndev->vendor_id, ndev->radio_type);
314 mutex_lock(&bus->device_lock);
316 if (mei_cl_disconnect(cl_info) < 0) {
317 mutex_unlock(&bus->device_lock);
318 dev_err(bus->dev, "Could not disconnect the NFC INFO ME client");
323 mutex_unlock(&bus->device_lock);
325 ndev->bus_name = mei_nfc_radio_name(&version);
327 if (!ndev->bus_name) {
328 dev_err(bus->dev, "Could not build the bus ID name\n");
332 cldev = mei_cl_add_device(bus, ndev->me_cl, ndev->cl,
335 dev_err(bus->dev, "Could not add the NFC device to the MEI bus\n");
340 cldev->priv_data = ndev;
346 mutex_lock(&bus->device_lock);
348 mutex_unlock(&bus->device_lock);
353 int mei_nfc_host_init(struct mei_device *bus, struct mei_me_client *me_cl)
355 struct mei_nfc_dev *ndev;
356 struct mei_cl *cl_info, *cl;
360 /* in case of internal reset bail out
361 * as the device is already setup
363 cl = mei_cl_bus_find_cl_by_uuid(bus, mei_nfc_guid);
367 ndev = kzalloc(sizeof(struct mei_nfc_dev), GFP_KERNEL);
373 ndev->me_cl = mei_me_cl_get(me_cl);
379 cl_info = mei_cl_alloc_linked(bus, MEI_HOST_CLIENT_ID_ANY);
380 if (IS_ERR(cl_info)) {
381 ret = PTR_ERR(cl_info);
385 list_add_tail(&cl_info->device_link, &bus->device_list);
387 ndev->cl_info = cl_info;
389 cl = mei_cl_alloc_linked(bus, MEI_HOST_CLIENT_ID_ANY);
395 list_add_tail(&cl->device_link, &bus->device_list);
399 INIT_WORK(&ndev->init_work, mei_nfc_init);
400 schedule_work(&ndev->init_work);
410 void mei_nfc_host_exit(struct mei_device *bus)
412 struct mei_nfc_dev *ndev;
414 struct mei_cl_device *cldev;
416 cl = mei_cl_bus_find_cl_by_uuid(bus, mei_nfc_guid);
424 ndev = (struct mei_nfc_dev *)cldev->priv_data;
426 cancel_work_sync(&ndev->init_work);
428 cldev->priv_data = NULL;
430 /* Need to remove the device here
431 * since mei_nfc_free will unlink the clients
433 mei_cl_remove_device(cldev);
435 mutex_lock(&bus->device_lock);
437 mutex_unlock(&bus->device_lock);
440 #define MEI_FIXUP(_uuid, _hook) { _uuid, _hook }
442 static struct mei_fixup {
445 void (*hook)(struct mei_cl_device *cldev);
447 MEI_FIXUP(MEI_UUID_ANY, number_of_connections),
448 MEI_FIXUP(MEI_UUID_NFC_INFO, blacklist),
452 * mei_cl_dev_fixup - run fixup handlers
454 * @cldev: me client device
456 void mei_cl_dev_fixup(struct mei_cl_device *cldev)
459 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
462 for (i = 0; i < ARRAY_SIZE(mei_fixups); i++) {
465 if (uuid_le_cmp(f->uuid, MEI_UUID_ANY) == 0 ||
466 uuid_le_cmp(f->uuid, *uuid) == 0)