NFC: mei_phy: move all nfc logic from mei driver to nfc
[firefly-linux-kernel-4.4.55.git] / drivers / nfc / mei_phy.c
1 /*
2  * MEI Library for mei bus nfc device access
3  *
4  * Copyright (C) 2013  Intel Corporation. All rights reserved.
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 that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/nfc.h>
24
25 #include "mei_phy.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
90 #define MEI_NFC_MAX_READ (MEI_NFC_HEADER_SIZE + MEI_NFC_MAX_HCI_PAYLOAD)
91
92 #define MEI_DUMP_SKB_IN(info, skb)                              \
93 do {                                                            \
94         pr_debug("%s:\n", info);                                \
95         print_hex_dump_debug("mei in : ", DUMP_PREFIX_OFFSET,   \
96                         16, 1, (skb)->data, (skb)->len, false); \
97 } while (0)
98
99 #define MEI_DUMP_SKB_OUT(info, skb)                             \
100 do {                                                            \
101         pr_debug("%s:\n", info);                                \
102         print_hex_dump_debug("mei out: ", DUMP_PREFIX_OFFSET,   \
103                         16, 1, (skb)->data, (skb)->len, false); \
104 } while (0)
105
106
107 static int mei_nfc_if_version(struct nfc_mei_phy *phy)
108 {
109
110         struct mei_nfc_cmd cmd;
111         struct mei_nfc_reply *reply = NULL;
112         struct mei_nfc_if_version *version;
113         size_t if_version_length;
114         int bytes_recv, r;
115
116         pr_info("%s\n", __func__);
117
118         memset(&cmd, 0, sizeof(struct mei_nfc_cmd));
119         cmd.command = MEI_NFC_CMD_MAINTENANCE;
120         cmd.data_size = 1;
121         cmd.sub_command = MEI_NFC_SUBCMD_IF_VERSION;
122
123         r = mei_cl_send(phy->device, (u8 *)&cmd, sizeof(struct mei_nfc_cmd));
124         if (r < 0) {
125                 pr_err("Could not send IF version cmd\n");
126                 return r;
127         }
128
129         /* to be sure on the stack we alloc memory */
130         if_version_length = sizeof(struct mei_nfc_reply) +
131                 sizeof(struct mei_nfc_if_version);
132
133         reply = kzalloc(if_version_length, GFP_KERNEL);
134         if (!reply)
135                 return -ENOMEM;
136
137         bytes_recv = mei_cl_recv(phy->device, (u8 *)reply, if_version_length);
138         if (bytes_recv < 0 || bytes_recv < sizeof(struct mei_nfc_reply)) {
139                 pr_err("Could not read IF version\n");
140                 r = -EIO;
141                 goto err;
142         }
143
144         version = (struct mei_nfc_if_version *)reply->data;
145
146         phy->fw_ivn = version->fw_ivn;
147         phy->vendor_id = version->vendor_id;
148         phy->radio_type = version->radio_type;
149
150 err:
151         kfree(reply);
152         return r;
153 }
154
155 static int mei_nfc_connect(struct nfc_mei_phy *phy)
156 {
157         struct mei_nfc_cmd *cmd, *reply;
158         struct mei_nfc_connect *connect;
159         struct mei_nfc_connect_resp *connect_resp;
160         size_t connect_length, connect_resp_length;
161         int bytes_recv, r;
162
163         pr_info("%s\n", __func__);
164
165         connect_length = sizeof(struct mei_nfc_cmd) +
166                         sizeof(struct mei_nfc_connect);
167
168         connect_resp_length = sizeof(struct mei_nfc_cmd) +
169                         sizeof(struct mei_nfc_connect_resp);
170
171         cmd = kzalloc(connect_length, GFP_KERNEL);
172         if (!cmd)
173                 return -ENOMEM;
174         connect = (struct mei_nfc_connect *)cmd->data;
175
176         reply = kzalloc(connect_resp_length, GFP_KERNEL);
177         if (!reply) {
178                 kfree(cmd);
179                 return -ENOMEM;
180         }
181
182         connect_resp = (struct mei_nfc_connect_resp *)reply->data;
183
184         cmd->command = MEI_NFC_CMD_MAINTENANCE;
185         cmd->data_size = 3;
186         cmd->sub_command = MEI_NFC_SUBCMD_CONNECT;
187         connect->fw_ivn = phy->fw_ivn;
188         connect->vendor_id = phy->vendor_id;
189
190         r = mei_cl_send(phy->device, (u8 *)cmd, connect_length);
191         if (r < 0) {
192                 pr_err("Could not send connect cmd %d\n", r);
193                 goto err;
194         }
195
196         bytes_recv = mei_cl_recv(phy->device, (u8 *)reply, connect_resp_length);
197         if (bytes_recv < 0) {
198                 r = bytes_recv;
199                 pr_err("Could not read connect response %d\n", r);
200                 goto err;
201         }
202
203         pr_info("IVN 0x%x Vendor ID 0x%x\n",
204                  connect_resp->fw_ivn, connect_resp->vendor_id);
205
206         pr_info("ME FW %d.%d.%d.%d\n",
207                 connect_resp->me_major, connect_resp->me_minor,
208                 connect_resp->me_hotfix, connect_resp->me_build);
209
210         r = 0;
211
212 err:
213         kfree(reply);
214         kfree(cmd);
215
216         return r;
217 }
218
219 static int mei_nfc_send(struct nfc_mei_phy *phy, u8 *buf, size_t length)
220 {
221         struct mei_nfc_hci_hdr *hdr;
222         u8 *mei_buf;
223         int err;
224
225         err = -ENOMEM;
226         mei_buf = kzalloc(length + MEI_NFC_HEADER_SIZE, GFP_KERNEL);
227         if (!mei_buf)
228                 goto out;
229
230         hdr = (struct mei_nfc_hci_hdr *) mei_buf;
231         hdr->cmd = MEI_NFC_CMD_HCI_SEND;
232         hdr->status = 0;
233         hdr->req_id = phy->req_id;
234         hdr->reserved = 0;
235         hdr->data_size = length;
236
237         memcpy(mei_buf + MEI_NFC_HEADER_SIZE, buf, length);
238         err = mei_cl_send(phy->device, mei_buf, length + MEI_NFC_HEADER_SIZE);
239         if (err < 0)
240                 goto out;
241
242         if (!wait_event_interruptible_timeout(phy->send_wq,
243                                 phy->recv_req_id == phy->req_id, HZ)) {
244                 pr_err("NFC MEI command timeout\n");
245                 err = -ETIME;
246         } else {
247                 phy->req_id++;
248         }
249 out:
250         kfree(mei_buf);
251         return err;
252 }
253
254 /*
255  * Writing a frame must not return the number of written bytes.
256  * It must return either zero for success, or <0 for error.
257  * In addition, it must not alter the skb
258  */
259 static int nfc_mei_phy_write(void *phy_id, struct sk_buff *skb)
260 {
261         struct nfc_mei_phy *phy = phy_id;
262         int r;
263
264         MEI_DUMP_SKB_OUT("mei frame sent", skb);
265
266         r = mei_nfc_send(phy, skb->data, skb->len);
267         if (r > 0)
268                 r = 0;
269
270         return r;
271 }
272
273 static int mei_nfc_recv(struct nfc_mei_phy *phy, u8 *buf, size_t length)
274 {
275         struct mei_nfc_hci_hdr *hci_hdr;
276         int received_length;
277
278         received_length = mei_cl_recv(phy->device, buf, length);
279         if (received_length < 0)
280                 return received_length;
281
282         hci_hdr = (struct mei_nfc_hci_hdr *) buf;
283
284         if (hci_hdr->cmd == MEI_NFC_CMD_HCI_SEND) {
285                 phy->recv_req_id = hci_hdr->req_id;
286                 wake_up(&phy->send_wq);
287
288                 return 0;
289         }
290
291         return received_length;
292 }
293
294
295 static void nfc_mei_event_cb(struct mei_cl_device *device, u32 events,
296                              void *context)
297 {
298         struct nfc_mei_phy *phy = context;
299
300         if (phy->hard_fault != 0)
301                 return;
302
303         if (events & BIT(MEI_CL_EVENT_RX)) {
304                 struct sk_buff *skb;
305                 int reply_size;
306
307                 skb = alloc_skb(MEI_NFC_MAX_READ, GFP_KERNEL);
308                 if (!skb)
309                         return;
310
311                 reply_size = mei_nfc_recv(phy, skb->data, MEI_NFC_MAX_READ);
312                 if (reply_size < MEI_NFC_HEADER_SIZE) {
313                         kfree_skb(skb);
314                         return;
315                 }
316
317                 skb_put(skb, reply_size);
318                 skb_pull(skb, MEI_NFC_HEADER_SIZE);
319
320                 MEI_DUMP_SKB_IN("mei frame read", skb);
321
322                 nfc_hci_recv_frame(phy->hdev, skb);
323         }
324 }
325
326 static int nfc_mei_phy_enable(void *phy_id)
327 {
328         int r;
329         struct nfc_mei_phy *phy = phy_id;
330
331         pr_info("%s\n", __func__);
332
333         if (phy->powered == 1)
334                 return 0;
335
336         r = mei_cl_enable_device(phy->device);
337         if (r < 0) {
338                 pr_err("Could not enable device %d\n", r);
339                 return r;
340         }
341
342         r = mei_nfc_if_version(phy);
343         if (r < 0) {
344                 pr_err("Could not enable device %d\n", r);
345                 goto err;
346         }
347
348         r = mei_nfc_connect(phy);
349         if (r < 0) {
350                 pr_err("Could not connect to device %d\n", r);
351                 goto err;
352         }
353
354         r = mei_cl_register_event_cb(phy->device, nfc_mei_event_cb, phy);
355         if (r) {
356                 pr_err("Event cb registration failed %d\n", r);
357                 goto err;
358         }
359
360         phy->powered = 1;
361
362         return 0;
363
364 err:
365         phy->powered = 0;
366         mei_cl_disable_device(phy->device);
367         return r;
368 }
369
370 static void nfc_mei_phy_disable(void *phy_id)
371 {
372         struct nfc_mei_phy *phy = phy_id;
373
374         pr_info("%s\n", __func__);
375
376         mei_cl_disable_device(phy->device);
377
378         phy->powered = 0;
379 }
380
381 struct nfc_phy_ops mei_phy_ops = {
382         .write = nfc_mei_phy_write,
383         .enable = nfc_mei_phy_enable,
384         .disable = nfc_mei_phy_disable,
385 };
386 EXPORT_SYMBOL_GPL(mei_phy_ops);
387
388 struct nfc_mei_phy *nfc_mei_phy_alloc(struct mei_cl_device *device)
389 {
390         struct nfc_mei_phy *phy;
391
392         phy = kzalloc(sizeof(struct nfc_mei_phy), GFP_KERNEL);
393         if (!phy)
394                 return NULL;
395
396         phy->device = device;
397         init_waitqueue_head(&phy->send_wq);
398         mei_cl_set_drvdata(device, phy);
399
400         return phy;
401 }
402 EXPORT_SYMBOL_GPL(nfc_mei_phy_alloc);
403
404 void nfc_mei_phy_free(struct nfc_mei_phy *phy)
405 {
406         mei_cl_disable_device(phy->device);
407         kfree(phy);
408 }
409 EXPORT_SYMBOL_GPL(nfc_mei_phy_free);
410
411 MODULE_LICENSE("GPL");
412 MODULE_DESCRIPTION("mei bus NFC device interface");