Merge remote-tracking branches 'regulator/fix/88pm800', 'regulator/fix/max8973',...
[linux-drm-fsl-dcu.git] / drivers / misc / mei / nfc.c
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/sched.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/device.h>
22 #include <linux/slab.h>
23
24 #include <linux/mei_cl_bus.h>
25
26 #include "mei_dev.h"
27 #include "client.h"
28
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;
38
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;
49
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;
59
60 struct mei_nfc_connect {
61         u8 fw_ivn;
62         u8 vendor_id;
63 } __packed;
64
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;
73
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;
81
82 #define MEI_NFC_CMD_MAINTENANCE 0x00
83 #define MEI_NFC_CMD_HCI_SEND 0x01
84 #define MEI_NFC_CMD_HCI_RECV 0x02
85
86 #define MEI_NFC_SUBCMD_CONNECT    0x00
87 #define MEI_NFC_SUBCMD_IF_VERSION 0x01
88
89 #define MEI_NFC_HEADER_SIZE 10
90
91 /**
92  * struct mei_nfc_dev - NFC mei device
93  *
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
102  *
103  */
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;
113 };
114
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);
119
120 static const uuid_le mei_nfc_info_guid = UUID_LE(0xd2de1625, 0x382d, 0x417d,
121                                         0x48, 0xa4, 0xef, 0xab,
122                                         0xba, 0x8a, 0x12, 0x06);
123
124 /* Vendors */
125 #define MEI_NFC_VENDOR_INSIDE 0x00
126 #define MEI_NFC_VENDOR_NXP    0x01
127
128 /* Radio types */
129 #define MEI_NFC_VENDOR_INSIDE_UREAD 0x00
130 #define MEI_NFC_VENDOR_NXP_PN544    0x01
131
132 static void mei_nfc_free(struct mei_nfc_dev *ndev)
133 {
134         if (!ndev)
135                 return;
136
137         if (ndev->cl) {
138                 list_del(&ndev->cl->device_link);
139                 mei_cl_unlink(ndev->cl);
140                 kfree(ndev->cl);
141         }
142
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);
147         }
148
149         mei_me_cl_put(ndev->me_cl);
150         kfree(ndev);
151 }
152
153 static int mei_nfc_build_bus_name(struct mei_nfc_dev *ndev)
154 {
155         struct mei_device *dev;
156
157         if (!ndev->cl)
158                 return -ENODEV;
159
160         dev = ndev->cl->dev;
161
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;
168
169                 default:
170                         dev_err(dev->dev, "Unknown radio type 0x%x\n",
171                                 ndev->radio_type);
172
173                         return -EINVAL;
174                 }
175
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);
184
185                         return -EINVAL;
186                 }
187
188         default:
189                 dev_err(dev->dev, "Unknown vendor ID 0x%x\n",
190                         ndev->vendor_id);
191
192                 return -EINVAL;
193         }
194
195         return 0;
196 }
197
198 static int mei_nfc_if_version(struct mei_nfc_dev *ndev)
199 {
200         struct mei_device *dev;
201         struct mei_cl *cl;
202
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;
208
209         cl = ndev->cl_info;
210         dev = cl->dev;
211
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;
216
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;
221         }
222
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);
226
227         reply = kzalloc(if_version_length, GFP_KERNEL);
228         if (!reply)
229                 return -ENOMEM;
230
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;
236         }
237
238         version = (struct mei_nfc_if_version *)reply->data;
239
240         ndev->fw_ivn = version->fw_ivn;
241         ndev->vendor_id = version->vendor_id;
242         ndev->radio_type = version->radio_type;
243
244 err:
245         kfree(reply);
246         return ret;
247 }
248
249 static void mei_nfc_init(struct work_struct *work)
250 {
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;
256
257         ndev = container_of(work, struct mei_nfc_dev, init_work);
258
259         cl_info = ndev->cl_info;
260         dev = cl_info->dev;
261
262         mutex_lock(&dev->device_lock);
263
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;
270         }
271
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");
276
277                 goto err;
278         }
279         mei_me_cl_put(me_cl_info);
280         mutex_unlock(&dev->device_lock);
281
282         if (mei_nfc_if_version(ndev) < 0) {
283                 dev_err(dev->dev, "Could not get the NFC interface version");
284
285                 goto err;
286         }
287
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);
290
291         mutex_lock(&dev->device_lock);
292
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");
296
297                 goto err;
298         }
299
300         mutex_unlock(&dev->device_lock);
301
302         if (mei_nfc_build_bus_name(ndev) < 0) {
303                 dev_err(dev->dev, "Could not build the bus ID name\n");
304                 return;
305         }
306
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");
311
312                 goto err;
313         }
314
315         cldev->priv_data = ndev;
316
317
318         return;
319
320 err:
321         mutex_lock(&dev->device_lock);
322         mei_nfc_free(ndev);
323         mutex_unlock(&dev->device_lock);
324
325 }
326
327
328 int mei_nfc_host_init(struct mei_device *dev, struct mei_me_client *me_cl)
329 {
330         struct mei_nfc_dev *ndev;
331         struct mei_cl *cl_info, *cl;
332         int ret;
333
334
335         /* in case of internal reset bail out
336          * as the device is already setup
337          */
338         cl = mei_cl_bus_find_cl_by_uuid(dev, mei_nfc_guid);
339         if (cl)
340                 return 0;
341
342         ndev = kzalloc(sizeof(struct mei_nfc_dev), GFP_KERNEL);
343         if (!ndev) {
344                 ret = -ENOMEM;
345                 goto err;
346         }
347
348         ndev->me_cl = mei_me_cl_get(me_cl);
349         if (!ndev->me_cl) {
350                 ret = -ENODEV;
351                 goto err;
352         }
353
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;
358         }
359
360         list_add_tail(&cl_info->device_link, &dev->device_list);
361
362         ndev->cl_info = cl_info;
363
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;
368         }
369
370         list_add_tail(&cl->device_link, &dev->device_list);
371
372         ndev->cl = cl;
373
374         INIT_WORK(&ndev->init_work, mei_nfc_init);
375         schedule_work(&ndev->init_work);
376
377         return 0;
378
379 err:
380         mei_nfc_free(ndev);
381
382         return ret;
383 }
384
385 void mei_nfc_host_exit(struct mei_device *dev)
386 {
387         struct mei_nfc_dev *ndev;
388         struct mei_cl *cl;
389         struct mei_cl_device *cldev;
390
391         cl = mei_cl_bus_find_cl_by_uuid(dev, mei_nfc_guid);
392         if (!cl)
393                 return;
394
395         cldev = cl->device;
396         if (!cldev)
397                 return;
398
399         ndev = (struct mei_nfc_dev *)cldev->priv_data;
400         if (ndev)
401                 cancel_work_sync(&ndev->init_work);
402
403         cldev->priv_data = NULL;
404
405         /* Need to remove the device here
406          * since mei_nfc_free will unlink the clients
407          */
408         mei_cl_remove_device(cldev);
409
410         mutex_lock(&dev->device_lock);
411         mei_nfc_free(ndev);
412         mutex_unlock(&dev->device_lock);
413 }
414
415