Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[platform/adaptation/renesas_rcar/renesas_kernel.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, write to the
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/nfc.h>
26
27 #include "mei_phy.h"
28
29 struct mei_nfc_hdr {
30         u8 cmd;
31         u8 status;
32         u16 req_id;
33         u32 reserved;
34         u16 data_size;
35 } __packed;
36
37 #define MEI_NFC_MAX_READ (MEI_NFC_HEADER_SIZE + MEI_NFC_MAX_HCI_PAYLOAD)
38
39 #define MEI_DUMP_SKB_IN(info, skb)                              \
40 do {                                                            \
41         pr_debug("%s:\n", info);                                \
42         print_hex_dump_debug("mei in : ", DUMP_PREFIX_OFFSET,   \
43                         16, 1, (skb)->data, (skb)->len, false); \
44 } while (0)
45
46 #define MEI_DUMP_SKB_OUT(info, skb)                             \
47 do {                                                            \
48         pr_debug("%s:\n", info);                                \
49         print_hex_dump_debug("mei out: ", DUMP_PREFIX_OFFSET,   \
50                        16, 1, (skb)->data, (skb)->len, false);  \
51 } while (0)
52
53 int nfc_mei_phy_enable(void *phy_id)
54 {
55         int r;
56         struct nfc_mei_phy *phy = phy_id;
57
58         pr_info("%s\n", __func__);
59
60         if (phy->powered == 1)
61                 return 0;
62
63         r = mei_cl_enable_device(phy->device);
64         if (r < 0) {
65                 pr_err("Could not enable device\n");
66                 return r;
67         }
68
69         r = mei_cl_register_event_cb(phy->device, nfc_mei_event_cb, phy);
70         if (r) {
71                 pr_err("Event cb registration failed\n");
72                 mei_cl_disable_device(phy->device);
73                 phy->powered = 0;
74
75                 return r;
76         }
77
78         phy->powered = 1;
79
80         return 0;
81 }
82 EXPORT_SYMBOL_GPL(nfc_mei_phy_enable);
83
84 void nfc_mei_phy_disable(void *phy_id)
85 {
86         struct nfc_mei_phy *phy = phy_id;
87
88         pr_info("%s\n", __func__);
89
90         mei_cl_disable_device(phy->device);
91
92         phy->powered = 0;
93 }
94 EXPORT_SYMBOL_GPL(nfc_mei_phy_disable);
95
96 /*
97  * Writing a frame must not return the number of written bytes.
98  * It must return either zero for success, or <0 for error.
99  * In addition, it must not alter the skb
100  */
101 static int nfc_mei_phy_write(void *phy_id, struct sk_buff *skb)
102 {
103         struct nfc_mei_phy *phy = phy_id;
104         int r;
105
106         MEI_DUMP_SKB_OUT("mei frame sent", skb);
107
108         r = mei_cl_send(phy->device, skb->data, skb->len);
109         if (r > 0)
110                 r = 0;
111
112         return r;
113 }
114
115 void nfc_mei_event_cb(struct mei_cl_device *device, u32 events, void *context)
116 {
117         struct nfc_mei_phy *phy = context;
118
119         if (phy->hard_fault != 0)
120                 return;
121
122         if (events & BIT(MEI_CL_EVENT_RX)) {
123                 struct sk_buff *skb;
124                 int reply_size;
125
126                 skb = alloc_skb(MEI_NFC_MAX_READ, GFP_KERNEL);
127                 if (!skb)
128                         return;
129
130                 reply_size = mei_cl_recv(device, skb->data, MEI_NFC_MAX_READ);
131                 if (reply_size < MEI_NFC_HEADER_SIZE) {
132                         kfree(skb);
133                         return;
134                 }
135
136                 skb_put(skb, reply_size);
137                 skb_pull(skb, MEI_NFC_HEADER_SIZE);
138
139                 MEI_DUMP_SKB_IN("mei frame read", skb);
140
141                 nfc_hci_recv_frame(phy->hdev, skb);
142         }
143 }
144 EXPORT_SYMBOL_GPL(nfc_mei_event_cb);
145
146 struct nfc_phy_ops mei_phy_ops = {
147         .write = nfc_mei_phy_write,
148         .enable = nfc_mei_phy_enable,
149         .disable = nfc_mei_phy_disable,
150 };
151 EXPORT_SYMBOL_GPL(mei_phy_ops);
152
153 struct nfc_mei_phy *nfc_mei_phy_alloc(struct mei_cl_device *device)
154 {
155         struct nfc_mei_phy *phy;
156
157         phy = kzalloc(sizeof(struct nfc_mei_phy), GFP_KERNEL);
158         if (!phy)
159                 return NULL;
160
161         phy->device = device;
162         mei_cl_set_drvdata(device, phy);
163
164         return phy;
165 }
166 EXPORT_SYMBOL_GPL(nfc_mei_phy_alloc);
167
168 void nfc_mei_phy_free(struct nfc_mei_phy *phy)
169 {
170         kfree(phy);
171 }
172 EXPORT_SYMBOL_GPL(nfc_mei_phy_free);
173
174 MODULE_LICENSE("GPL");
175 MODULE_DESCRIPTION("mei bus NFC device interface");