Linux 4.2.1
[linux/fpc-iii.git] / drivers / misc / mei / nfc.c
blob290ef3037437816114e6d373d4889c6ee2855c1b
1 /*
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
13 * more details.
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>
24 #include <linux/mei_cl_bus.h>
26 #include "mei_dev.h"
27 #include "client.h"
29 struct mei_nfc_cmd {
30 u8 command;
31 u8 status;
32 u16 req_id;
33 u32 reserved;
34 u16 data_size;
35 u8 sub_command;
36 u8 data[];
37 } __packed;
39 struct mei_nfc_reply {
40 u8 command;
41 u8 status;
42 u16 req_id;
43 u32 reserved;
44 u16 data_size;
45 u8 sub_command;
46 u8 reply_status;
47 u8 data[];
48 } __packed;
50 struct mei_nfc_if_version {
51 u8 radio_version_sw[3];
52 u8 reserved[3];
53 u8 radio_version_hw[3];
54 u8 i2c_addr;
55 u8 fw_ivn;
56 u8 vendor_id;
57 u8 radio_type;
58 } __packed;
60 struct mei_nfc_connect {
61 u8 fw_ivn;
62 u8 vendor_id;
63 } __packed;
65 struct mei_nfc_connect_resp {
66 u8 fw_ivn;
67 u8 vendor_id;
68 u16 me_major;
69 u16 me_minor;
70 u16 me_hotfix;
71 u16 me_build;
72 } __packed;
74 struct mei_nfc_hci_hdr {
75 u8 cmd;
76 u8 status;
77 u16 req_id;
78 u32 reserved;
79 u16 data_size;
80 } __packed;
82 #define MEI_NFC_CMD_MAINTENANCE 0x00
83 #define MEI_NFC_CMD_HCI_SEND 0x01
84 #define MEI_NFC_CMD_HCI_RECV 0x02
86 #define MEI_NFC_SUBCMD_CONNECT 0x00
87 #define MEI_NFC_SUBCMD_IF_VERSION 0x01
89 #define MEI_NFC_HEADER_SIZE 10
91 /**
92 * struct mei_nfc_dev - NFC mei device
94 * @me_cl: NFC me client
95 * @cl: NFC host client
96 * @cl_info: NFC info host client
97 * @init_work: perform connection to the info client
98 * @fw_ivn: NFC Interface Version Number
99 * @vendor_id: NFC manufacturer ID
100 * @radio_type: NFC radio type
101 * @bus_name: bus name
104 struct mei_nfc_dev {
105 struct mei_me_client *me_cl;
106 struct mei_cl *cl;
107 struct mei_cl *cl_info;
108 struct work_struct init_work;
109 u8 fw_ivn;
110 u8 vendor_id;
111 u8 radio_type;
112 char *bus_name;
115 /* UUIDs for NFC F/W clients */
116 const uuid_le mei_nfc_guid = UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50,
117 0x94, 0xd4, 0x50, 0x26,
118 0x67, 0x23, 0x77, 0x5c);
120 static const uuid_le mei_nfc_info_guid = UUID_LE(0xd2de1625, 0x382d, 0x417d,
121 0x48, 0xa4, 0xef, 0xab,
122 0xba, 0x8a, 0x12, 0x06);
124 /* Vendors */
125 #define MEI_NFC_VENDOR_INSIDE 0x00
126 #define MEI_NFC_VENDOR_NXP 0x01
128 /* Radio types */
129 #define MEI_NFC_VENDOR_INSIDE_UREAD 0x00
130 #define MEI_NFC_VENDOR_NXP_PN544 0x01
132 static void mei_nfc_free(struct mei_nfc_dev *ndev)
134 if (!ndev)
135 return;
137 if (ndev->cl) {
138 list_del(&ndev->cl->device_link);
139 mei_cl_unlink(ndev->cl);
140 kfree(ndev->cl);
143 if (ndev->cl_info) {
144 list_del(&ndev->cl_info->device_link);
145 mei_cl_unlink(ndev->cl_info);
146 kfree(ndev->cl_info);
149 mei_me_cl_put(ndev->me_cl);
150 kfree(ndev);
153 static int mei_nfc_build_bus_name(struct mei_nfc_dev *ndev)
155 struct mei_device *dev;
157 if (!ndev->cl)
158 return -ENODEV;
160 dev = ndev->cl->dev;
162 switch (ndev->vendor_id) {
163 case MEI_NFC_VENDOR_INSIDE:
164 switch (ndev->radio_type) {
165 case MEI_NFC_VENDOR_INSIDE_UREAD:
166 ndev->bus_name = "microread";
167 return 0;
169 default:
170 dev_err(dev->dev, "Unknown radio type 0x%x\n",
171 ndev->radio_type);
173 return -EINVAL;
176 case MEI_NFC_VENDOR_NXP:
177 switch (ndev->radio_type) {
178 case MEI_NFC_VENDOR_NXP_PN544:
179 ndev->bus_name = "pn544";
180 return 0;
181 default:
182 dev_err(dev->dev, "Unknown radio type 0x%x\n",
183 ndev->radio_type);
185 return -EINVAL;
188 default:
189 dev_err(dev->dev, "Unknown vendor ID 0x%x\n",
190 ndev->vendor_id);
192 return -EINVAL;
195 return 0;
198 static int mei_nfc_if_version(struct mei_nfc_dev *ndev)
200 struct mei_device *dev;
201 struct mei_cl *cl;
203 struct mei_nfc_cmd cmd;
204 struct mei_nfc_reply *reply = NULL;
205 struct mei_nfc_if_version *version;
206 size_t if_version_length;
207 int bytes_recv, ret;
209 cl = ndev->cl_info;
210 dev = cl->dev;
212 memset(&cmd, 0, sizeof(struct mei_nfc_cmd));
213 cmd.command = MEI_NFC_CMD_MAINTENANCE;
214 cmd.data_size = 1;
215 cmd.sub_command = MEI_NFC_SUBCMD_IF_VERSION;
217 ret = __mei_cl_send(cl, (u8 *)&cmd, sizeof(struct mei_nfc_cmd), 1);
218 if (ret < 0) {
219 dev_err(dev->dev, "Could not send IF version cmd\n");
220 return ret;
223 /* to be sure on the stack we alloc memory */
224 if_version_length = sizeof(struct mei_nfc_reply) +
225 sizeof(struct mei_nfc_if_version);
227 reply = kzalloc(if_version_length, GFP_KERNEL);
228 if (!reply)
229 return -ENOMEM;
231 bytes_recv = __mei_cl_recv(cl, (u8 *)reply, if_version_length);
232 if (bytes_recv < 0 || bytes_recv < sizeof(struct mei_nfc_reply)) {
233 dev_err(dev->dev, "Could not read IF version\n");
234 ret = -EIO;
235 goto err;
238 version = (struct mei_nfc_if_version *)reply->data;
240 ndev->fw_ivn = version->fw_ivn;
241 ndev->vendor_id = version->vendor_id;
242 ndev->radio_type = version->radio_type;
244 err:
245 kfree(reply);
246 return ret;
249 static void mei_nfc_init(struct work_struct *work)
251 struct mei_device *dev;
252 struct mei_cl_device *cldev;
253 struct mei_nfc_dev *ndev;
254 struct mei_cl *cl_info;
255 struct mei_me_client *me_cl_info;
257 ndev = container_of(work, struct mei_nfc_dev, init_work);
259 cl_info = ndev->cl_info;
260 dev = cl_info->dev;
262 mutex_lock(&dev->device_lock);
264 /* check for valid client id */
265 me_cl_info = mei_me_cl_by_uuid(dev, &mei_nfc_info_guid);
266 if (!me_cl_info) {
267 mutex_unlock(&dev->device_lock);
268 dev_info(dev->dev, "nfc: failed to find the info client\n");
269 goto err;
272 if (mei_cl_connect(cl_info, me_cl_info, NULL) < 0) {
273 mei_me_cl_put(me_cl_info);
274 mutex_unlock(&dev->device_lock);
275 dev_err(dev->dev, "Could not connect to the NFC INFO ME client");
277 goto err;
279 mei_me_cl_put(me_cl_info);
280 mutex_unlock(&dev->device_lock);
282 if (mei_nfc_if_version(ndev) < 0) {
283 dev_err(dev->dev, "Could not get the NFC interface version");
285 goto err;
288 dev_info(dev->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n",
289 ndev->fw_ivn, ndev->vendor_id, ndev->radio_type);
291 mutex_lock(&dev->device_lock);
293 if (mei_cl_disconnect(cl_info) < 0) {
294 mutex_unlock(&dev->device_lock);
295 dev_err(dev->dev, "Could not disconnect the NFC INFO ME client");
297 goto err;
300 mutex_unlock(&dev->device_lock);
302 if (mei_nfc_build_bus_name(ndev) < 0) {
303 dev_err(dev->dev, "Could not build the bus ID name\n");
304 return;
307 cldev = mei_cl_add_device(dev, ndev->me_cl, ndev->cl,
308 ndev->bus_name);
309 if (!cldev) {
310 dev_err(dev->dev, "Could not add the NFC device to the MEI bus\n");
312 goto err;
315 cldev->priv_data = ndev;
318 return;
320 err:
321 mutex_lock(&dev->device_lock);
322 mei_nfc_free(ndev);
323 mutex_unlock(&dev->device_lock);
328 int mei_nfc_host_init(struct mei_device *dev, struct mei_me_client *me_cl)
330 struct mei_nfc_dev *ndev;
331 struct mei_cl *cl_info, *cl;
332 int ret;
335 /* in case of internal reset bail out
336 * as the device is already setup
338 cl = mei_cl_bus_find_cl_by_uuid(dev, mei_nfc_guid);
339 if (cl)
340 return 0;
342 ndev = kzalloc(sizeof(struct mei_nfc_dev), GFP_KERNEL);
343 if (!ndev) {
344 ret = -ENOMEM;
345 goto err;
348 ndev->me_cl = mei_me_cl_get(me_cl);
349 if (!ndev->me_cl) {
350 ret = -ENODEV;
351 goto err;
354 cl_info = mei_cl_alloc_linked(dev, MEI_HOST_CLIENT_ID_ANY);
355 if (IS_ERR(cl_info)) {
356 ret = PTR_ERR(cl_info);
357 goto err;
360 list_add_tail(&cl_info->device_link, &dev->device_list);
362 ndev->cl_info = cl_info;
364 cl = mei_cl_alloc_linked(dev, MEI_HOST_CLIENT_ID_ANY);
365 if (IS_ERR(cl)) {
366 ret = PTR_ERR(cl);
367 goto err;
370 list_add_tail(&cl->device_link, &dev->device_list);
372 ndev->cl = cl;
374 INIT_WORK(&ndev->init_work, mei_nfc_init);
375 schedule_work(&ndev->init_work);
377 return 0;
379 err:
380 mei_nfc_free(ndev);
382 return ret;
385 void mei_nfc_host_exit(struct mei_device *dev)
387 struct mei_nfc_dev *ndev;
388 struct mei_cl *cl;
389 struct mei_cl_device *cldev;
391 cl = mei_cl_bus_find_cl_by_uuid(dev, mei_nfc_guid);
392 if (!cl)
393 return;
395 cldev = cl->device;
396 if (!cldev)
397 return;
399 ndev = (struct mei_nfc_dev *)cldev->priv_data;
400 if (ndev)
401 cancel_work_sync(&ndev->init_work);
403 cldev->priv_data = NULL;
405 /* Need to remove the device here
406 * since mei_nfc_free will unlink the clients
408 mei_cl_remove_device(cldev);
410 mutex_lock(&dev->device_lock);
411 mei_nfc_free(ndev);
412 mutex_unlock(&dev->device_lock);