Commit 8668fdd6efb3a75e0d58a3287a47fa7e60a68a73
Committed by
John W. Linville
1 parent
a070c8591a
Exists in
smarc-l5.0.0_1.0.0-ga
and in
5 other branches
NFC: Core must test the device polling state inside the device lock
There can ever be only one call to nfc_targets_found() after polling has been engaged. This could be from a target discovered event from the driver, or from an error handler to notify poll will never complete. Signed-off-by: Eric Lapuyade <eric.lapuyade@intel.com> Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
Showing 1 changed file with 7 additions and 2 deletions Inline Diff
net/nfc/core.c
1 | /* | 1 | /* |
2 | * Copyright (C) 2011 Instituto Nokia de Tecnologia | 2 | * Copyright (C) 2011 Instituto Nokia de Tecnologia |
3 | * | 3 | * |
4 | * Authors: | 4 | * Authors: |
5 | * Lauro Ramos Venancio <lauro.venancio@openbossa.org> | 5 | * Lauro Ramos Venancio <lauro.venancio@openbossa.org> |
6 | * Aloisio Almeida Jr <aloisio.almeida@openbossa.org> | 6 | * Aloisio Almeida Jr <aloisio.almeida@openbossa.org> |
7 | * | 7 | * |
8 | * This program is free software; you can redistribute it and/or modify | 8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by | 9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 2 of the License, or | 10 | * the Free Software Foundation; either version 2 of the License, or |
11 | * (at your option) any later version. | 11 | * (at your option) any later version. |
12 | * | 12 | * |
13 | * This program is distributed in the hope that it will be useful, | 13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. | 16 | * GNU General Public License for more details. |
17 | * | 17 | * |
18 | * You should have received a copy of the GNU General Public License | 18 | * You should have received a copy of the GNU General Public License |
19 | * along with this program; if not, write to the | 19 | * along with this program; if not, write to the |
20 | * Free Software Foundation, Inc., | 20 | * Free Software Foundation, Inc., |
21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22 | */ | 22 | */ |
23 | 23 | ||
24 | #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ | 24 | #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ |
25 | 25 | ||
26 | #include <linux/init.h> | 26 | #include <linux/init.h> |
27 | #include <linux/kernel.h> | 27 | #include <linux/kernel.h> |
28 | #include <linux/module.h> | 28 | #include <linux/module.h> |
29 | #include <linux/slab.h> | 29 | #include <linux/slab.h> |
30 | #include <linux/nfc.h> | 30 | #include <linux/nfc.h> |
31 | 31 | ||
32 | #include "nfc.h" | 32 | #include "nfc.h" |
33 | 33 | ||
34 | #define VERSION "0.1" | 34 | #define VERSION "0.1" |
35 | 35 | ||
36 | #define NFC_CHECK_PRES_FREQ_MS 2000 | 36 | #define NFC_CHECK_PRES_FREQ_MS 2000 |
37 | 37 | ||
38 | int nfc_devlist_generation; | 38 | int nfc_devlist_generation; |
39 | DEFINE_MUTEX(nfc_devlist_mutex); | 39 | DEFINE_MUTEX(nfc_devlist_mutex); |
40 | 40 | ||
41 | /** | 41 | /** |
42 | * nfc_dev_up - turn on the NFC device | 42 | * nfc_dev_up - turn on the NFC device |
43 | * | 43 | * |
44 | * @dev: The nfc device to be turned on | 44 | * @dev: The nfc device to be turned on |
45 | * | 45 | * |
46 | * The device remains up until the nfc_dev_down function is called. | 46 | * The device remains up until the nfc_dev_down function is called. |
47 | */ | 47 | */ |
48 | int nfc_dev_up(struct nfc_dev *dev) | 48 | int nfc_dev_up(struct nfc_dev *dev) |
49 | { | 49 | { |
50 | int rc = 0; | 50 | int rc = 0; |
51 | 51 | ||
52 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); | 52 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
53 | 53 | ||
54 | device_lock(&dev->dev); | 54 | device_lock(&dev->dev); |
55 | 55 | ||
56 | if (!device_is_registered(&dev->dev)) { | 56 | if (!device_is_registered(&dev->dev)) { |
57 | rc = -ENODEV; | 57 | rc = -ENODEV; |
58 | goto error; | 58 | goto error; |
59 | } | 59 | } |
60 | 60 | ||
61 | if (dev->dev_up) { | 61 | if (dev->dev_up) { |
62 | rc = -EALREADY; | 62 | rc = -EALREADY; |
63 | goto error; | 63 | goto error; |
64 | } | 64 | } |
65 | 65 | ||
66 | if (dev->ops->dev_up) | 66 | if (dev->ops->dev_up) |
67 | rc = dev->ops->dev_up(dev); | 67 | rc = dev->ops->dev_up(dev); |
68 | 68 | ||
69 | if (!rc) | 69 | if (!rc) |
70 | dev->dev_up = true; | 70 | dev->dev_up = true; |
71 | 71 | ||
72 | error: | 72 | error: |
73 | device_unlock(&dev->dev); | 73 | device_unlock(&dev->dev); |
74 | return rc; | 74 | return rc; |
75 | } | 75 | } |
76 | 76 | ||
77 | /** | 77 | /** |
78 | * nfc_dev_down - turn off the NFC device | 78 | * nfc_dev_down - turn off the NFC device |
79 | * | 79 | * |
80 | * @dev: The nfc device to be turned off | 80 | * @dev: The nfc device to be turned off |
81 | */ | 81 | */ |
82 | int nfc_dev_down(struct nfc_dev *dev) | 82 | int nfc_dev_down(struct nfc_dev *dev) |
83 | { | 83 | { |
84 | int rc = 0; | 84 | int rc = 0; |
85 | 85 | ||
86 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); | 86 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
87 | 87 | ||
88 | device_lock(&dev->dev); | 88 | device_lock(&dev->dev); |
89 | 89 | ||
90 | if (!device_is_registered(&dev->dev)) { | 90 | if (!device_is_registered(&dev->dev)) { |
91 | rc = -ENODEV; | 91 | rc = -ENODEV; |
92 | goto error; | 92 | goto error; |
93 | } | 93 | } |
94 | 94 | ||
95 | if (!dev->dev_up) { | 95 | if (!dev->dev_up) { |
96 | rc = -EALREADY; | 96 | rc = -EALREADY; |
97 | goto error; | 97 | goto error; |
98 | } | 98 | } |
99 | 99 | ||
100 | if (dev->polling || dev->active_target) { | 100 | if (dev->polling || dev->active_target) { |
101 | rc = -EBUSY; | 101 | rc = -EBUSY; |
102 | goto error; | 102 | goto error; |
103 | } | 103 | } |
104 | 104 | ||
105 | if (dev->ops->dev_down) | 105 | if (dev->ops->dev_down) |
106 | dev->ops->dev_down(dev); | 106 | dev->ops->dev_down(dev); |
107 | 107 | ||
108 | dev->dev_up = false; | 108 | dev->dev_up = false; |
109 | 109 | ||
110 | error: | 110 | error: |
111 | device_unlock(&dev->dev); | 111 | device_unlock(&dev->dev); |
112 | return rc; | 112 | return rc; |
113 | } | 113 | } |
114 | 114 | ||
115 | /** | 115 | /** |
116 | * nfc_start_poll - start polling for nfc targets | 116 | * nfc_start_poll - start polling for nfc targets |
117 | * | 117 | * |
118 | * @dev: The nfc device that must start polling | 118 | * @dev: The nfc device that must start polling |
119 | * @protocols: bitset of nfc protocols that must be used for polling | 119 | * @protocols: bitset of nfc protocols that must be used for polling |
120 | * | 120 | * |
121 | * The device remains polling for targets until a target is found or | 121 | * The device remains polling for targets until a target is found or |
122 | * the nfc_stop_poll function is called. | 122 | * the nfc_stop_poll function is called. |
123 | */ | 123 | */ |
124 | int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols) | 124 | int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols) |
125 | { | 125 | { |
126 | int rc; | 126 | int rc; |
127 | 127 | ||
128 | pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n", | 128 | pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n", |
129 | dev_name(&dev->dev), im_protocols, tm_protocols); | 129 | dev_name(&dev->dev), im_protocols, tm_protocols); |
130 | 130 | ||
131 | if (!im_protocols && !tm_protocols) | 131 | if (!im_protocols && !tm_protocols) |
132 | return -EINVAL; | 132 | return -EINVAL; |
133 | 133 | ||
134 | device_lock(&dev->dev); | 134 | device_lock(&dev->dev); |
135 | 135 | ||
136 | if (!device_is_registered(&dev->dev)) { | 136 | if (!device_is_registered(&dev->dev)) { |
137 | rc = -ENODEV; | 137 | rc = -ENODEV; |
138 | goto error; | 138 | goto error; |
139 | } | 139 | } |
140 | 140 | ||
141 | if (dev->polling) { | 141 | if (dev->polling) { |
142 | rc = -EBUSY; | 142 | rc = -EBUSY; |
143 | goto error; | 143 | goto error; |
144 | } | 144 | } |
145 | 145 | ||
146 | rc = dev->ops->start_poll(dev, im_protocols, tm_protocols); | 146 | rc = dev->ops->start_poll(dev, im_protocols, tm_protocols); |
147 | if (!rc) { | 147 | if (!rc) { |
148 | dev->polling = true; | 148 | dev->polling = true; |
149 | dev->rf_mode = NFC_RF_NONE; | 149 | dev->rf_mode = NFC_RF_NONE; |
150 | } | 150 | } |
151 | 151 | ||
152 | error: | 152 | error: |
153 | device_unlock(&dev->dev); | 153 | device_unlock(&dev->dev); |
154 | return rc; | 154 | return rc; |
155 | } | 155 | } |
156 | 156 | ||
157 | /** | 157 | /** |
158 | * nfc_stop_poll - stop polling for nfc targets | 158 | * nfc_stop_poll - stop polling for nfc targets |
159 | * | 159 | * |
160 | * @dev: The nfc device that must stop polling | 160 | * @dev: The nfc device that must stop polling |
161 | */ | 161 | */ |
162 | int nfc_stop_poll(struct nfc_dev *dev) | 162 | int nfc_stop_poll(struct nfc_dev *dev) |
163 | { | 163 | { |
164 | int rc = 0; | 164 | int rc = 0; |
165 | 165 | ||
166 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); | 166 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
167 | 167 | ||
168 | device_lock(&dev->dev); | 168 | device_lock(&dev->dev); |
169 | 169 | ||
170 | if (!device_is_registered(&dev->dev)) { | 170 | if (!device_is_registered(&dev->dev)) { |
171 | rc = -ENODEV; | 171 | rc = -ENODEV; |
172 | goto error; | 172 | goto error; |
173 | } | 173 | } |
174 | 174 | ||
175 | if (!dev->polling) { | 175 | if (!dev->polling) { |
176 | rc = -EINVAL; | 176 | rc = -EINVAL; |
177 | goto error; | 177 | goto error; |
178 | } | 178 | } |
179 | 179 | ||
180 | dev->ops->stop_poll(dev); | 180 | dev->ops->stop_poll(dev); |
181 | dev->polling = false; | 181 | dev->polling = false; |
182 | 182 | ||
183 | error: | 183 | error: |
184 | device_unlock(&dev->dev); | 184 | device_unlock(&dev->dev); |
185 | return rc; | 185 | return rc; |
186 | } | 186 | } |
187 | 187 | ||
188 | static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx) | 188 | static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx) |
189 | { | 189 | { |
190 | int i; | 190 | int i; |
191 | 191 | ||
192 | if (dev->n_targets == 0) | 192 | if (dev->n_targets == 0) |
193 | return NULL; | 193 | return NULL; |
194 | 194 | ||
195 | for (i = 0; i < dev->n_targets ; i++) { | 195 | for (i = 0; i < dev->n_targets ; i++) { |
196 | if (dev->targets[i].idx == target_idx) | 196 | if (dev->targets[i].idx == target_idx) |
197 | return &dev->targets[i]; | 197 | return &dev->targets[i]; |
198 | } | 198 | } |
199 | 199 | ||
200 | return NULL; | 200 | return NULL; |
201 | } | 201 | } |
202 | 202 | ||
203 | int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode) | 203 | int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode) |
204 | { | 204 | { |
205 | int rc = 0; | 205 | int rc = 0; |
206 | u8 *gb; | 206 | u8 *gb; |
207 | size_t gb_len; | 207 | size_t gb_len; |
208 | struct nfc_target *target; | 208 | struct nfc_target *target; |
209 | 209 | ||
210 | pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode); | 210 | pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode); |
211 | 211 | ||
212 | if (!dev->ops->dep_link_up) | 212 | if (!dev->ops->dep_link_up) |
213 | return -EOPNOTSUPP; | 213 | return -EOPNOTSUPP; |
214 | 214 | ||
215 | device_lock(&dev->dev); | 215 | device_lock(&dev->dev); |
216 | 216 | ||
217 | if (!device_is_registered(&dev->dev)) { | 217 | if (!device_is_registered(&dev->dev)) { |
218 | rc = -ENODEV; | 218 | rc = -ENODEV; |
219 | goto error; | 219 | goto error; |
220 | } | 220 | } |
221 | 221 | ||
222 | if (dev->dep_link_up == true) { | 222 | if (dev->dep_link_up == true) { |
223 | rc = -EALREADY; | 223 | rc = -EALREADY; |
224 | goto error; | 224 | goto error; |
225 | } | 225 | } |
226 | 226 | ||
227 | gb = nfc_llcp_general_bytes(dev, &gb_len); | 227 | gb = nfc_llcp_general_bytes(dev, &gb_len); |
228 | if (gb_len > NFC_MAX_GT_LEN) { | 228 | if (gb_len > NFC_MAX_GT_LEN) { |
229 | rc = -EINVAL; | 229 | rc = -EINVAL; |
230 | goto error; | 230 | goto error; |
231 | } | 231 | } |
232 | 232 | ||
233 | target = nfc_find_target(dev, target_index); | 233 | target = nfc_find_target(dev, target_index); |
234 | if (target == NULL) { | 234 | if (target == NULL) { |
235 | rc = -ENOTCONN; | 235 | rc = -ENOTCONN; |
236 | goto error; | 236 | goto error; |
237 | } | 237 | } |
238 | 238 | ||
239 | rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len); | 239 | rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len); |
240 | if (!rc) { | 240 | if (!rc) { |
241 | dev->active_target = target; | 241 | dev->active_target = target; |
242 | dev->rf_mode = NFC_RF_INITIATOR; | 242 | dev->rf_mode = NFC_RF_INITIATOR; |
243 | } | 243 | } |
244 | 244 | ||
245 | error: | 245 | error: |
246 | device_unlock(&dev->dev); | 246 | device_unlock(&dev->dev); |
247 | return rc; | 247 | return rc; |
248 | } | 248 | } |
249 | 249 | ||
250 | int nfc_dep_link_down(struct nfc_dev *dev) | 250 | int nfc_dep_link_down(struct nfc_dev *dev) |
251 | { | 251 | { |
252 | int rc = 0; | 252 | int rc = 0; |
253 | 253 | ||
254 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); | 254 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
255 | 255 | ||
256 | if (!dev->ops->dep_link_down) | 256 | if (!dev->ops->dep_link_down) |
257 | return -EOPNOTSUPP; | 257 | return -EOPNOTSUPP; |
258 | 258 | ||
259 | device_lock(&dev->dev); | 259 | device_lock(&dev->dev); |
260 | 260 | ||
261 | if (!device_is_registered(&dev->dev)) { | 261 | if (!device_is_registered(&dev->dev)) { |
262 | rc = -ENODEV; | 262 | rc = -ENODEV; |
263 | goto error; | 263 | goto error; |
264 | } | 264 | } |
265 | 265 | ||
266 | if (dev->dep_link_up == false) { | 266 | if (dev->dep_link_up == false) { |
267 | rc = -EALREADY; | 267 | rc = -EALREADY; |
268 | goto error; | 268 | goto error; |
269 | } | 269 | } |
270 | 270 | ||
271 | rc = dev->ops->dep_link_down(dev); | 271 | rc = dev->ops->dep_link_down(dev); |
272 | if (!rc) { | 272 | if (!rc) { |
273 | dev->dep_link_up = false; | 273 | dev->dep_link_up = false; |
274 | dev->active_target = NULL; | 274 | dev->active_target = NULL; |
275 | nfc_llcp_mac_is_down(dev); | 275 | nfc_llcp_mac_is_down(dev); |
276 | nfc_genl_dep_link_down_event(dev); | 276 | nfc_genl_dep_link_down_event(dev); |
277 | } | 277 | } |
278 | 278 | ||
279 | error: | 279 | error: |
280 | device_unlock(&dev->dev); | 280 | device_unlock(&dev->dev); |
281 | return rc; | 281 | return rc; |
282 | } | 282 | } |
283 | 283 | ||
284 | int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx, | 284 | int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx, |
285 | u8 comm_mode, u8 rf_mode) | 285 | u8 comm_mode, u8 rf_mode) |
286 | { | 286 | { |
287 | dev->dep_link_up = true; | 287 | dev->dep_link_up = true; |
288 | 288 | ||
289 | nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode); | 289 | nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode); |
290 | 290 | ||
291 | return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode); | 291 | return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode); |
292 | } | 292 | } |
293 | EXPORT_SYMBOL(nfc_dep_link_is_up); | 293 | EXPORT_SYMBOL(nfc_dep_link_is_up); |
294 | 294 | ||
295 | /** | 295 | /** |
296 | * nfc_activate_target - prepare the target for data exchange | 296 | * nfc_activate_target - prepare the target for data exchange |
297 | * | 297 | * |
298 | * @dev: The nfc device that found the target | 298 | * @dev: The nfc device that found the target |
299 | * @target_idx: index of the target that must be activated | 299 | * @target_idx: index of the target that must be activated |
300 | * @protocol: nfc protocol that will be used for data exchange | 300 | * @protocol: nfc protocol that will be used for data exchange |
301 | */ | 301 | */ |
302 | int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol) | 302 | int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol) |
303 | { | 303 | { |
304 | int rc; | 304 | int rc; |
305 | struct nfc_target *target; | 305 | struct nfc_target *target; |
306 | 306 | ||
307 | pr_debug("dev_name=%s target_idx=%u protocol=%u\n", | 307 | pr_debug("dev_name=%s target_idx=%u protocol=%u\n", |
308 | dev_name(&dev->dev), target_idx, protocol); | 308 | dev_name(&dev->dev), target_idx, protocol); |
309 | 309 | ||
310 | device_lock(&dev->dev); | 310 | device_lock(&dev->dev); |
311 | 311 | ||
312 | if (!device_is_registered(&dev->dev)) { | 312 | if (!device_is_registered(&dev->dev)) { |
313 | rc = -ENODEV; | 313 | rc = -ENODEV; |
314 | goto error; | 314 | goto error; |
315 | } | 315 | } |
316 | 316 | ||
317 | if (dev->active_target) { | 317 | if (dev->active_target) { |
318 | rc = -EBUSY; | 318 | rc = -EBUSY; |
319 | goto error; | 319 | goto error; |
320 | } | 320 | } |
321 | 321 | ||
322 | target = nfc_find_target(dev, target_idx); | 322 | target = nfc_find_target(dev, target_idx); |
323 | if (target == NULL) { | 323 | if (target == NULL) { |
324 | rc = -ENOTCONN; | 324 | rc = -ENOTCONN; |
325 | goto error; | 325 | goto error; |
326 | } | 326 | } |
327 | 327 | ||
328 | rc = dev->ops->activate_target(dev, target, protocol); | 328 | rc = dev->ops->activate_target(dev, target, protocol); |
329 | if (!rc) { | 329 | if (!rc) { |
330 | dev->active_target = target; | 330 | dev->active_target = target; |
331 | dev->rf_mode = NFC_RF_INITIATOR; | 331 | dev->rf_mode = NFC_RF_INITIATOR; |
332 | 332 | ||
333 | if (dev->ops->check_presence) | 333 | if (dev->ops->check_presence) |
334 | mod_timer(&dev->check_pres_timer, jiffies + | 334 | mod_timer(&dev->check_pres_timer, jiffies + |
335 | msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); | 335 | msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); |
336 | } | 336 | } |
337 | 337 | ||
338 | error: | 338 | error: |
339 | device_unlock(&dev->dev); | 339 | device_unlock(&dev->dev); |
340 | return rc; | 340 | return rc; |
341 | } | 341 | } |
342 | 342 | ||
343 | /** | 343 | /** |
344 | * nfc_deactivate_target - deactivate a nfc target | 344 | * nfc_deactivate_target - deactivate a nfc target |
345 | * | 345 | * |
346 | * @dev: The nfc device that found the target | 346 | * @dev: The nfc device that found the target |
347 | * @target_idx: index of the target that must be deactivated | 347 | * @target_idx: index of the target that must be deactivated |
348 | */ | 348 | */ |
349 | int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx) | 349 | int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx) |
350 | { | 350 | { |
351 | int rc = 0; | 351 | int rc = 0; |
352 | 352 | ||
353 | pr_debug("dev_name=%s target_idx=%u\n", | 353 | pr_debug("dev_name=%s target_idx=%u\n", |
354 | dev_name(&dev->dev), target_idx); | 354 | dev_name(&dev->dev), target_idx); |
355 | 355 | ||
356 | device_lock(&dev->dev); | 356 | device_lock(&dev->dev); |
357 | 357 | ||
358 | if (!device_is_registered(&dev->dev)) { | 358 | if (!device_is_registered(&dev->dev)) { |
359 | rc = -ENODEV; | 359 | rc = -ENODEV; |
360 | goto error; | 360 | goto error; |
361 | } | 361 | } |
362 | 362 | ||
363 | if (dev->active_target == NULL) { | 363 | if (dev->active_target == NULL) { |
364 | rc = -ENOTCONN; | 364 | rc = -ENOTCONN; |
365 | goto error; | 365 | goto error; |
366 | } | 366 | } |
367 | 367 | ||
368 | if (dev->active_target->idx != target_idx) { | 368 | if (dev->active_target->idx != target_idx) { |
369 | rc = -ENOTCONN; | 369 | rc = -ENOTCONN; |
370 | goto error; | 370 | goto error; |
371 | } | 371 | } |
372 | 372 | ||
373 | if (dev->ops->check_presence) | 373 | if (dev->ops->check_presence) |
374 | del_timer_sync(&dev->check_pres_timer); | 374 | del_timer_sync(&dev->check_pres_timer); |
375 | 375 | ||
376 | dev->ops->deactivate_target(dev, dev->active_target); | 376 | dev->ops->deactivate_target(dev, dev->active_target); |
377 | dev->active_target = NULL; | 377 | dev->active_target = NULL; |
378 | 378 | ||
379 | error: | 379 | error: |
380 | device_unlock(&dev->dev); | 380 | device_unlock(&dev->dev); |
381 | return rc; | 381 | return rc; |
382 | } | 382 | } |
383 | 383 | ||
384 | /** | 384 | /** |
385 | * nfc_data_exchange - transceive data | 385 | * nfc_data_exchange - transceive data |
386 | * | 386 | * |
387 | * @dev: The nfc device that found the target | 387 | * @dev: The nfc device that found the target |
388 | * @target_idx: index of the target | 388 | * @target_idx: index of the target |
389 | * @skb: data to be sent | 389 | * @skb: data to be sent |
390 | * @cb: callback called when the response is received | 390 | * @cb: callback called when the response is received |
391 | * @cb_context: parameter for the callback function | 391 | * @cb_context: parameter for the callback function |
392 | * | 392 | * |
393 | * The user must wait for the callback before calling this function again. | 393 | * The user must wait for the callback before calling this function again. |
394 | */ | 394 | */ |
395 | int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb, | 395 | int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb, |
396 | data_exchange_cb_t cb, void *cb_context) | 396 | data_exchange_cb_t cb, void *cb_context) |
397 | { | 397 | { |
398 | int rc; | 398 | int rc; |
399 | 399 | ||
400 | pr_debug("dev_name=%s target_idx=%u skb->len=%u\n", | 400 | pr_debug("dev_name=%s target_idx=%u skb->len=%u\n", |
401 | dev_name(&dev->dev), target_idx, skb->len); | 401 | dev_name(&dev->dev), target_idx, skb->len); |
402 | 402 | ||
403 | device_lock(&dev->dev); | 403 | device_lock(&dev->dev); |
404 | 404 | ||
405 | if (!device_is_registered(&dev->dev)) { | 405 | if (!device_is_registered(&dev->dev)) { |
406 | rc = -ENODEV; | 406 | rc = -ENODEV; |
407 | kfree_skb(skb); | 407 | kfree_skb(skb); |
408 | goto error; | 408 | goto error; |
409 | } | 409 | } |
410 | 410 | ||
411 | if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) { | 411 | if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) { |
412 | if (dev->active_target->idx != target_idx) { | 412 | if (dev->active_target->idx != target_idx) { |
413 | rc = -EADDRNOTAVAIL; | 413 | rc = -EADDRNOTAVAIL; |
414 | kfree_skb(skb); | 414 | kfree_skb(skb); |
415 | goto error; | 415 | goto error; |
416 | } | 416 | } |
417 | 417 | ||
418 | if (dev->ops->check_presence) | 418 | if (dev->ops->check_presence) |
419 | del_timer_sync(&dev->check_pres_timer); | 419 | del_timer_sync(&dev->check_pres_timer); |
420 | 420 | ||
421 | rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb, | 421 | rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb, |
422 | cb_context); | 422 | cb_context); |
423 | 423 | ||
424 | if (!rc && dev->ops->check_presence) | 424 | if (!rc && dev->ops->check_presence) |
425 | mod_timer(&dev->check_pres_timer, jiffies + | 425 | mod_timer(&dev->check_pres_timer, jiffies + |
426 | msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); | 426 | msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); |
427 | } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) { | 427 | } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) { |
428 | rc = dev->ops->tm_send(dev, skb); | 428 | rc = dev->ops->tm_send(dev, skb); |
429 | } else { | 429 | } else { |
430 | rc = -ENOTCONN; | 430 | rc = -ENOTCONN; |
431 | kfree_skb(skb); | 431 | kfree_skb(skb); |
432 | goto error; | 432 | goto error; |
433 | } | 433 | } |
434 | 434 | ||
435 | 435 | ||
436 | error: | 436 | error: |
437 | device_unlock(&dev->dev); | 437 | device_unlock(&dev->dev); |
438 | return rc; | 438 | return rc; |
439 | } | 439 | } |
440 | 440 | ||
441 | int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len) | 441 | int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len) |
442 | { | 442 | { |
443 | pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len); | 443 | pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len); |
444 | 444 | ||
445 | if (gb_len > NFC_MAX_GT_LEN) | 445 | if (gb_len > NFC_MAX_GT_LEN) |
446 | return -EINVAL; | 446 | return -EINVAL; |
447 | 447 | ||
448 | return nfc_llcp_set_remote_gb(dev, gb, gb_len); | 448 | return nfc_llcp_set_remote_gb(dev, gb, gb_len); |
449 | } | 449 | } |
450 | EXPORT_SYMBOL(nfc_set_remote_general_bytes); | 450 | EXPORT_SYMBOL(nfc_set_remote_general_bytes); |
451 | 451 | ||
452 | u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len) | 452 | u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len) |
453 | { | 453 | { |
454 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); | 454 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
455 | 455 | ||
456 | return nfc_llcp_general_bytes(dev, gb_len); | 456 | return nfc_llcp_general_bytes(dev, gb_len); |
457 | } | 457 | } |
458 | EXPORT_SYMBOL(nfc_get_local_general_bytes); | 458 | EXPORT_SYMBOL(nfc_get_local_general_bytes); |
459 | 459 | ||
460 | int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb) | 460 | int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb) |
461 | { | 461 | { |
462 | /* Only LLCP target mode for now */ | 462 | /* Only LLCP target mode for now */ |
463 | if (dev->dep_link_up == false) { | 463 | if (dev->dep_link_up == false) { |
464 | kfree_skb(skb); | 464 | kfree_skb(skb); |
465 | return -ENOLINK; | 465 | return -ENOLINK; |
466 | } | 466 | } |
467 | 467 | ||
468 | return nfc_llcp_data_received(dev, skb); | 468 | return nfc_llcp_data_received(dev, skb); |
469 | } | 469 | } |
470 | EXPORT_SYMBOL(nfc_tm_data_received); | 470 | EXPORT_SYMBOL(nfc_tm_data_received); |
471 | 471 | ||
472 | int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode, | 472 | int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode, |
473 | u8 *gb, size_t gb_len) | 473 | u8 *gb, size_t gb_len) |
474 | { | 474 | { |
475 | int rc; | 475 | int rc; |
476 | 476 | ||
477 | device_lock(&dev->dev); | 477 | device_lock(&dev->dev); |
478 | 478 | ||
479 | dev->polling = false; | 479 | dev->polling = false; |
480 | 480 | ||
481 | if (gb != NULL) { | 481 | if (gb != NULL) { |
482 | rc = nfc_set_remote_general_bytes(dev, gb, gb_len); | 482 | rc = nfc_set_remote_general_bytes(dev, gb, gb_len); |
483 | if (rc < 0) | 483 | if (rc < 0) |
484 | goto out; | 484 | goto out; |
485 | } | 485 | } |
486 | 486 | ||
487 | dev->rf_mode = NFC_RF_TARGET; | 487 | dev->rf_mode = NFC_RF_TARGET; |
488 | 488 | ||
489 | if (protocol == NFC_PROTO_NFC_DEP_MASK) | 489 | if (protocol == NFC_PROTO_NFC_DEP_MASK) |
490 | nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET); | 490 | nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET); |
491 | 491 | ||
492 | rc = nfc_genl_tm_activated(dev, protocol); | 492 | rc = nfc_genl_tm_activated(dev, protocol); |
493 | 493 | ||
494 | out: | 494 | out: |
495 | device_unlock(&dev->dev); | 495 | device_unlock(&dev->dev); |
496 | 496 | ||
497 | return rc; | 497 | return rc; |
498 | } | 498 | } |
499 | EXPORT_SYMBOL(nfc_tm_activated); | 499 | EXPORT_SYMBOL(nfc_tm_activated); |
500 | 500 | ||
501 | int nfc_tm_deactivated(struct nfc_dev *dev) | 501 | int nfc_tm_deactivated(struct nfc_dev *dev) |
502 | { | 502 | { |
503 | dev->dep_link_up = false; | 503 | dev->dep_link_up = false; |
504 | 504 | ||
505 | return nfc_genl_tm_deactivated(dev); | 505 | return nfc_genl_tm_deactivated(dev); |
506 | } | 506 | } |
507 | EXPORT_SYMBOL(nfc_tm_deactivated); | 507 | EXPORT_SYMBOL(nfc_tm_deactivated); |
508 | 508 | ||
509 | /** | 509 | /** |
510 | * nfc_alloc_send_skb - allocate a skb for data exchange responses | 510 | * nfc_alloc_send_skb - allocate a skb for data exchange responses |
511 | * | 511 | * |
512 | * @size: size to allocate | 512 | * @size: size to allocate |
513 | * @gfp: gfp flags | 513 | * @gfp: gfp flags |
514 | */ | 514 | */ |
515 | struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk, | 515 | struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk, |
516 | unsigned int flags, unsigned int size, | 516 | unsigned int flags, unsigned int size, |
517 | unsigned int *err) | 517 | unsigned int *err) |
518 | { | 518 | { |
519 | struct sk_buff *skb; | 519 | struct sk_buff *skb; |
520 | unsigned int total_size; | 520 | unsigned int total_size; |
521 | 521 | ||
522 | total_size = size + | 522 | total_size = size + |
523 | dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; | 523 | dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; |
524 | 524 | ||
525 | skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err); | 525 | skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err); |
526 | if (skb) | 526 | if (skb) |
527 | skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); | 527 | skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); |
528 | 528 | ||
529 | return skb; | 529 | return skb; |
530 | } | 530 | } |
531 | 531 | ||
532 | /** | 532 | /** |
533 | * nfc_alloc_recv_skb - allocate a skb for data exchange responses | 533 | * nfc_alloc_recv_skb - allocate a skb for data exchange responses |
534 | * | 534 | * |
535 | * @size: size to allocate | 535 | * @size: size to allocate |
536 | * @gfp: gfp flags | 536 | * @gfp: gfp flags |
537 | */ | 537 | */ |
538 | struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp) | 538 | struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp) |
539 | { | 539 | { |
540 | struct sk_buff *skb; | 540 | struct sk_buff *skb; |
541 | unsigned int total_size; | 541 | unsigned int total_size; |
542 | 542 | ||
543 | total_size = size + 1; | 543 | total_size = size + 1; |
544 | skb = alloc_skb(total_size, gfp); | 544 | skb = alloc_skb(total_size, gfp); |
545 | 545 | ||
546 | if (skb) | 546 | if (skb) |
547 | skb_reserve(skb, 1); | 547 | skb_reserve(skb, 1); |
548 | 548 | ||
549 | return skb; | 549 | return skb; |
550 | } | 550 | } |
551 | EXPORT_SYMBOL(nfc_alloc_recv_skb); | 551 | EXPORT_SYMBOL(nfc_alloc_recv_skb); |
552 | 552 | ||
553 | /** | 553 | /** |
554 | * nfc_targets_found - inform that targets were found | 554 | * nfc_targets_found - inform that targets were found |
555 | * | 555 | * |
556 | * @dev: The nfc device that found the targets | 556 | * @dev: The nfc device that found the targets |
557 | * @targets: array of nfc targets found | 557 | * @targets: array of nfc targets found |
558 | * @ntargets: targets array size | 558 | * @ntargets: targets array size |
559 | * | 559 | * |
560 | * The device driver must call this function when one or many nfc targets | 560 | * The device driver must call this function when one or many nfc targets |
561 | * are found. After calling this function, the device driver must stop | 561 | * are found. After calling this function, the device driver must stop |
562 | * polling for targets. | 562 | * polling for targets. |
563 | * IMPORTANT: this function must not be called from an atomic context. | 563 | * IMPORTANT: this function must not be called from an atomic context. |
564 | * In addition, it must also not be called from a context that would prevent | 564 | * In addition, it must also not be called from a context that would prevent |
565 | * the NFC Core to call other nfc ops entry point concurrently. | 565 | * the NFC Core to call other nfc ops entry point concurrently. |
566 | */ | 566 | */ |
567 | int nfc_targets_found(struct nfc_dev *dev, | 567 | int nfc_targets_found(struct nfc_dev *dev, |
568 | struct nfc_target *targets, int n_targets) | 568 | struct nfc_target *targets, int n_targets) |
569 | { | 569 | { |
570 | int i; | 570 | int i; |
571 | 571 | ||
572 | pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets); | 572 | pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets); |
573 | 573 | ||
574 | dev->polling = false; | ||
575 | |||
576 | for (i = 0; i < n_targets; i++) | 574 | for (i = 0; i < n_targets; i++) |
577 | targets[i].idx = dev->target_next_idx++; | 575 | targets[i].idx = dev->target_next_idx++; |
578 | 576 | ||
579 | device_lock(&dev->dev); | 577 | device_lock(&dev->dev); |
578 | |||
579 | if (dev->polling == false) { | ||
580 | device_unlock(&dev->dev); | ||
581 | return 0; | ||
582 | } | ||
583 | |||
584 | dev->polling = false; | ||
580 | 585 | ||
581 | dev->targets_generation++; | 586 | dev->targets_generation++; |
582 | 587 | ||
583 | kfree(dev->targets); | 588 | kfree(dev->targets); |
584 | dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target), | 589 | dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target), |
585 | GFP_ATOMIC); | 590 | GFP_ATOMIC); |
586 | 591 | ||
587 | if (!dev->targets) { | 592 | if (!dev->targets) { |
588 | dev->n_targets = 0; | 593 | dev->n_targets = 0; |
589 | device_unlock(&dev->dev); | 594 | device_unlock(&dev->dev); |
590 | return -ENOMEM; | 595 | return -ENOMEM; |
591 | } | 596 | } |
592 | 597 | ||
593 | dev->n_targets = n_targets; | 598 | dev->n_targets = n_targets; |
594 | device_unlock(&dev->dev); | 599 | device_unlock(&dev->dev); |
595 | 600 | ||
596 | nfc_genl_targets_found(dev); | 601 | nfc_genl_targets_found(dev); |
597 | 602 | ||
598 | return 0; | 603 | return 0; |
599 | } | 604 | } |
600 | EXPORT_SYMBOL(nfc_targets_found); | 605 | EXPORT_SYMBOL(nfc_targets_found); |
601 | 606 | ||
602 | /** | 607 | /** |
603 | * nfc_target_lost - inform that an activated target went out of field | 608 | * nfc_target_lost - inform that an activated target went out of field |
604 | * | 609 | * |
605 | * @dev: The nfc device that had the activated target in field | 610 | * @dev: The nfc device that had the activated target in field |
606 | * @target_idx: the nfc index of the target | 611 | * @target_idx: the nfc index of the target |
607 | * | 612 | * |
608 | * The device driver must call this function when the activated target | 613 | * The device driver must call this function when the activated target |
609 | * goes out of the field. | 614 | * goes out of the field. |
610 | * IMPORTANT: this function must not be called from an atomic context. | 615 | * IMPORTANT: this function must not be called from an atomic context. |
611 | * In addition, it must also not be called from a context that would prevent | 616 | * In addition, it must also not be called from a context that would prevent |
612 | * the NFC Core to call other nfc ops entry point concurrently. | 617 | * the NFC Core to call other nfc ops entry point concurrently. |
613 | */ | 618 | */ |
614 | int nfc_target_lost(struct nfc_dev *dev, u32 target_idx) | 619 | int nfc_target_lost(struct nfc_dev *dev, u32 target_idx) |
615 | { | 620 | { |
616 | struct nfc_target *tg; | 621 | struct nfc_target *tg; |
617 | int i; | 622 | int i; |
618 | 623 | ||
619 | pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx); | 624 | pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx); |
620 | 625 | ||
621 | device_lock(&dev->dev); | 626 | device_lock(&dev->dev); |
622 | 627 | ||
623 | for (i = 0; i < dev->n_targets; i++) { | 628 | for (i = 0; i < dev->n_targets; i++) { |
624 | tg = &dev->targets[i]; | 629 | tg = &dev->targets[i]; |
625 | if (tg->idx == target_idx) | 630 | if (tg->idx == target_idx) |
626 | break; | 631 | break; |
627 | } | 632 | } |
628 | 633 | ||
629 | if (i == dev->n_targets) { | 634 | if (i == dev->n_targets) { |
630 | device_unlock(&dev->dev); | 635 | device_unlock(&dev->dev); |
631 | return -EINVAL; | 636 | return -EINVAL; |
632 | } | 637 | } |
633 | 638 | ||
634 | dev->targets_generation++; | 639 | dev->targets_generation++; |
635 | dev->n_targets--; | 640 | dev->n_targets--; |
636 | dev->active_target = NULL; | 641 | dev->active_target = NULL; |
637 | 642 | ||
638 | if (dev->n_targets) { | 643 | if (dev->n_targets) { |
639 | memcpy(&dev->targets[i], &dev->targets[i + 1], | 644 | memcpy(&dev->targets[i], &dev->targets[i + 1], |
640 | (dev->n_targets - i) * sizeof(struct nfc_target)); | 645 | (dev->n_targets - i) * sizeof(struct nfc_target)); |
641 | } else { | 646 | } else { |
642 | kfree(dev->targets); | 647 | kfree(dev->targets); |
643 | dev->targets = NULL; | 648 | dev->targets = NULL; |
644 | } | 649 | } |
645 | 650 | ||
646 | device_unlock(&dev->dev); | 651 | device_unlock(&dev->dev); |
647 | 652 | ||
648 | nfc_genl_target_lost(dev, target_idx); | 653 | nfc_genl_target_lost(dev, target_idx); |
649 | 654 | ||
650 | return 0; | 655 | return 0; |
651 | } | 656 | } |
652 | EXPORT_SYMBOL(nfc_target_lost); | 657 | EXPORT_SYMBOL(nfc_target_lost); |
653 | 658 | ||
654 | void nfc_driver_failure(struct nfc_dev *dev, int err) | 659 | void nfc_driver_failure(struct nfc_dev *dev, int err) |
655 | { | 660 | { |
656 | /* | 661 | /* |
657 | * TODO: if polling is active, send empty target_found | 662 | * TODO: if polling is active, send empty target_found |
658 | * or else do whatever makes sense to let user space | 663 | * or else do whatever makes sense to let user space |
659 | * know this device needs to be closed and reinitialized. | 664 | * know this device needs to be closed and reinitialized. |
660 | */ | 665 | */ |
661 | } | 666 | } |
662 | EXPORT_SYMBOL(nfc_driver_failure); | 667 | EXPORT_SYMBOL(nfc_driver_failure); |
663 | 668 | ||
664 | static void nfc_release(struct device *d) | 669 | static void nfc_release(struct device *d) |
665 | { | 670 | { |
666 | struct nfc_dev *dev = to_nfc_dev(d); | 671 | struct nfc_dev *dev = to_nfc_dev(d); |
667 | 672 | ||
668 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); | 673 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
669 | 674 | ||
670 | if (dev->ops->check_presence) { | 675 | if (dev->ops->check_presence) { |
671 | del_timer_sync(&dev->check_pres_timer); | 676 | del_timer_sync(&dev->check_pres_timer); |
672 | destroy_workqueue(dev->check_pres_wq); | 677 | destroy_workqueue(dev->check_pres_wq); |
673 | } | 678 | } |
674 | 679 | ||
675 | nfc_genl_data_exit(&dev->genl_data); | 680 | nfc_genl_data_exit(&dev->genl_data); |
676 | kfree(dev->targets); | 681 | kfree(dev->targets); |
677 | kfree(dev); | 682 | kfree(dev); |
678 | } | 683 | } |
679 | 684 | ||
680 | static void nfc_check_pres_work(struct work_struct *work) | 685 | static void nfc_check_pres_work(struct work_struct *work) |
681 | { | 686 | { |
682 | struct nfc_dev *dev = container_of(work, struct nfc_dev, | 687 | struct nfc_dev *dev = container_of(work, struct nfc_dev, |
683 | check_pres_work); | 688 | check_pres_work); |
684 | int rc; | 689 | int rc; |
685 | 690 | ||
686 | device_lock(&dev->dev); | 691 | device_lock(&dev->dev); |
687 | 692 | ||
688 | if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) { | 693 | if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) { |
689 | rc = dev->ops->check_presence(dev, dev->active_target); | 694 | rc = dev->ops->check_presence(dev, dev->active_target); |
690 | if (!rc) { | 695 | if (!rc) { |
691 | mod_timer(&dev->check_pres_timer, jiffies + | 696 | mod_timer(&dev->check_pres_timer, jiffies + |
692 | msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); | 697 | msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); |
693 | } else { | 698 | } else { |
694 | u32 active_target_idx = dev->active_target->idx; | 699 | u32 active_target_idx = dev->active_target->idx; |
695 | device_unlock(&dev->dev); | 700 | device_unlock(&dev->dev); |
696 | nfc_target_lost(dev, active_target_idx); | 701 | nfc_target_lost(dev, active_target_idx); |
697 | return; | 702 | return; |
698 | } | 703 | } |
699 | } | 704 | } |
700 | 705 | ||
701 | device_unlock(&dev->dev); | 706 | device_unlock(&dev->dev); |
702 | } | 707 | } |
703 | 708 | ||
704 | static void nfc_check_pres_timeout(unsigned long data) | 709 | static void nfc_check_pres_timeout(unsigned long data) |
705 | { | 710 | { |
706 | struct nfc_dev *dev = (struct nfc_dev *)data; | 711 | struct nfc_dev *dev = (struct nfc_dev *)data; |
707 | 712 | ||
708 | queue_work(dev->check_pres_wq, &dev->check_pres_work); | 713 | queue_work(dev->check_pres_wq, &dev->check_pres_work); |
709 | } | 714 | } |
710 | 715 | ||
711 | struct class nfc_class = { | 716 | struct class nfc_class = { |
712 | .name = "nfc", | 717 | .name = "nfc", |
713 | .dev_release = nfc_release, | 718 | .dev_release = nfc_release, |
714 | }; | 719 | }; |
715 | EXPORT_SYMBOL(nfc_class); | 720 | EXPORT_SYMBOL(nfc_class); |
716 | 721 | ||
717 | static int match_idx(struct device *d, void *data) | 722 | static int match_idx(struct device *d, void *data) |
718 | { | 723 | { |
719 | struct nfc_dev *dev = to_nfc_dev(d); | 724 | struct nfc_dev *dev = to_nfc_dev(d); |
720 | unsigned int *idx = data; | 725 | unsigned int *idx = data; |
721 | 726 | ||
722 | return dev->idx == *idx; | 727 | return dev->idx == *idx; |
723 | } | 728 | } |
724 | 729 | ||
725 | struct nfc_dev *nfc_get_device(unsigned int idx) | 730 | struct nfc_dev *nfc_get_device(unsigned int idx) |
726 | { | 731 | { |
727 | struct device *d; | 732 | struct device *d; |
728 | 733 | ||
729 | d = class_find_device(&nfc_class, NULL, &idx, match_idx); | 734 | d = class_find_device(&nfc_class, NULL, &idx, match_idx); |
730 | if (!d) | 735 | if (!d) |
731 | return NULL; | 736 | return NULL; |
732 | 737 | ||
733 | return to_nfc_dev(d); | 738 | return to_nfc_dev(d); |
734 | } | 739 | } |
735 | 740 | ||
736 | /** | 741 | /** |
737 | * nfc_allocate_device - allocate a new nfc device | 742 | * nfc_allocate_device - allocate a new nfc device |
738 | * | 743 | * |
739 | * @ops: device operations | 744 | * @ops: device operations |
740 | * @supported_protocols: NFC protocols supported by the device | 745 | * @supported_protocols: NFC protocols supported by the device |
741 | */ | 746 | */ |
742 | struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops, | 747 | struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops, |
743 | u32 supported_protocols, | 748 | u32 supported_protocols, |
744 | int tx_headroom, int tx_tailroom) | 749 | int tx_headroom, int tx_tailroom) |
745 | { | 750 | { |
746 | static atomic_t dev_no = ATOMIC_INIT(0); | 751 | static atomic_t dev_no = ATOMIC_INIT(0); |
747 | struct nfc_dev *dev; | 752 | struct nfc_dev *dev; |
748 | 753 | ||
749 | if (!ops->start_poll || !ops->stop_poll || !ops->activate_target || | 754 | if (!ops->start_poll || !ops->stop_poll || !ops->activate_target || |
750 | !ops->deactivate_target || !ops->im_transceive) | 755 | !ops->deactivate_target || !ops->im_transceive) |
751 | return NULL; | 756 | return NULL; |
752 | 757 | ||
753 | if (!supported_protocols) | 758 | if (!supported_protocols) |
754 | return NULL; | 759 | return NULL; |
755 | 760 | ||
756 | dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL); | 761 | dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL); |
757 | if (!dev) | 762 | if (!dev) |
758 | return NULL; | 763 | return NULL; |
759 | 764 | ||
760 | dev->dev.class = &nfc_class; | 765 | dev->dev.class = &nfc_class; |
761 | dev->idx = atomic_inc_return(&dev_no) - 1; | 766 | dev->idx = atomic_inc_return(&dev_no) - 1; |
762 | dev_set_name(&dev->dev, "nfc%d", dev->idx); | 767 | dev_set_name(&dev->dev, "nfc%d", dev->idx); |
763 | device_initialize(&dev->dev); | 768 | device_initialize(&dev->dev); |
764 | 769 | ||
765 | dev->ops = ops; | 770 | dev->ops = ops; |
766 | dev->supported_protocols = supported_protocols; | 771 | dev->supported_protocols = supported_protocols; |
767 | dev->tx_headroom = tx_headroom; | 772 | dev->tx_headroom = tx_headroom; |
768 | dev->tx_tailroom = tx_tailroom; | 773 | dev->tx_tailroom = tx_tailroom; |
769 | 774 | ||
770 | nfc_genl_data_init(&dev->genl_data); | 775 | nfc_genl_data_init(&dev->genl_data); |
771 | 776 | ||
772 | 777 | ||
773 | /* first generation must not be 0 */ | 778 | /* first generation must not be 0 */ |
774 | dev->targets_generation = 1; | 779 | dev->targets_generation = 1; |
775 | 780 | ||
776 | if (ops->check_presence) { | 781 | if (ops->check_presence) { |
777 | char name[32]; | 782 | char name[32]; |
778 | init_timer(&dev->check_pres_timer); | 783 | init_timer(&dev->check_pres_timer); |
779 | dev->check_pres_timer.data = (unsigned long)dev; | 784 | dev->check_pres_timer.data = (unsigned long)dev; |
780 | dev->check_pres_timer.function = nfc_check_pres_timeout; | 785 | dev->check_pres_timer.function = nfc_check_pres_timeout; |
781 | 786 | ||
782 | INIT_WORK(&dev->check_pres_work, nfc_check_pres_work); | 787 | INIT_WORK(&dev->check_pres_work, nfc_check_pres_work); |
783 | snprintf(name, sizeof(name), "nfc%d_check_pres_wq", dev->idx); | 788 | snprintf(name, sizeof(name), "nfc%d_check_pres_wq", dev->idx); |
784 | dev->check_pres_wq = alloc_workqueue(name, WQ_NON_REENTRANT | | 789 | dev->check_pres_wq = alloc_workqueue(name, WQ_NON_REENTRANT | |
785 | WQ_UNBOUND | | 790 | WQ_UNBOUND | |
786 | WQ_MEM_RECLAIM, 1); | 791 | WQ_MEM_RECLAIM, 1); |
787 | if (dev->check_pres_wq == NULL) { | 792 | if (dev->check_pres_wq == NULL) { |
788 | kfree(dev); | 793 | kfree(dev); |
789 | return NULL; | 794 | return NULL; |
790 | } | 795 | } |
791 | } | 796 | } |
792 | 797 | ||
793 | return dev; | 798 | return dev; |
794 | } | 799 | } |
795 | EXPORT_SYMBOL(nfc_allocate_device); | 800 | EXPORT_SYMBOL(nfc_allocate_device); |
796 | 801 | ||
797 | /** | 802 | /** |
798 | * nfc_register_device - register a nfc device in the nfc subsystem | 803 | * nfc_register_device - register a nfc device in the nfc subsystem |
799 | * | 804 | * |
800 | * @dev: The nfc device to register | 805 | * @dev: The nfc device to register |
801 | */ | 806 | */ |
802 | int nfc_register_device(struct nfc_dev *dev) | 807 | int nfc_register_device(struct nfc_dev *dev) |
803 | { | 808 | { |
804 | int rc; | 809 | int rc; |
805 | 810 | ||
806 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); | 811 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
807 | 812 | ||
808 | mutex_lock(&nfc_devlist_mutex); | 813 | mutex_lock(&nfc_devlist_mutex); |
809 | nfc_devlist_generation++; | 814 | nfc_devlist_generation++; |
810 | rc = device_add(&dev->dev); | 815 | rc = device_add(&dev->dev); |
811 | mutex_unlock(&nfc_devlist_mutex); | 816 | mutex_unlock(&nfc_devlist_mutex); |
812 | 817 | ||
813 | if (rc < 0) | 818 | if (rc < 0) |
814 | return rc; | 819 | return rc; |
815 | 820 | ||
816 | rc = nfc_llcp_register_device(dev); | 821 | rc = nfc_llcp_register_device(dev); |
817 | if (rc) | 822 | if (rc) |
818 | pr_err("Could not register llcp device\n"); | 823 | pr_err("Could not register llcp device\n"); |
819 | 824 | ||
820 | rc = nfc_genl_device_added(dev); | 825 | rc = nfc_genl_device_added(dev); |
821 | if (rc) | 826 | if (rc) |
822 | pr_debug("The userspace won't be notified that the device %s was added\n", | 827 | pr_debug("The userspace won't be notified that the device %s was added\n", |
823 | dev_name(&dev->dev)); | 828 | dev_name(&dev->dev)); |
824 | 829 | ||
825 | return 0; | 830 | return 0; |
826 | } | 831 | } |
827 | EXPORT_SYMBOL(nfc_register_device); | 832 | EXPORT_SYMBOL(nfc_register_device); |
828 | 833 | ||
829 | /** | 834 | /** |
830 | * nfc_unregister_device - unregister a nfc device in the nfc subsystem | 835 | * nfc_unregister_device - unregister a nfc device in the nfc subsystem |
831 | * | 836 | * |
832 | * @dev: The nfc device to unregister | 837 | * @dev: The nfc device to unregister |
833 | */ | 838 | */ |
834 | void nfc_unregister_device(struct nfc_dev *dev) | 839 | void nfc_unregister_device(struct nfc_dev *dev) |
835 | { | 840 | { |
836 | int rc; | 841 | int rc; |
837 | 842 | ||
838 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); | 843 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
839 | 844 | ||
840 | mutex_lock(&nfc_devlist_mutex); | 845 | mutex_lock(&nfc_devlist_mutex); |
841 | nfc_devlist_generation++; | 846 | nfc_devlist_generation++; |
842 | 847 | ||
843 | /* lock to avoid unregistering a device while an operation | 848 | /* lock to avoid unregistering a device while an operation |
844 | is in progress */ | 849 | is in progress */ |
845 | device_lock(&dev->dev); | 850 | device_lock(&dev->dev); |
846 | device_del(&dev->dev); | 851 | device_del(&dev->dev); |
847 | device_unlock(&dev->dev); | 852 | device_unlock(&dev->dev); |
848 | 853 | ||
849 | mutex_unlock(&nfc_devlist_mutex); | 854 | mutex_unlock(&nfc_devlist_mutex); |
850 | 855 | ||
851 | nfc_llcp_unregister_device(dev); | 856 | nfc_llcp_unregister_device(dev); |
852 | 857 | ||
853 | rc = nfc_genl_device_removed(dev); | 858 | rc = nfc_genl_device_removed(dev); |
854 | if (rc) | 859 | if (rc) |
855 | pr_debug("The userspace won't be notified that the device %s was removed\n", | 860 | pr_debug("The userspace won't be notified that the device %s was removed\n", |
856 | dev_name(&dev->dev)); | 861 | dev_name(&dev->dev)); |
857 | 862 | ||
858 | } | 863 | } |
859 | EXPORT_SYMBOL(nfc_unregister_device); | 864 | EXPORT_SYMBOL(nfc_unregister_device); |
860 | 865 | ||
861 | static int __init nfc_init(void) | 866 | static int __init nfc_init(void) |
862 | { | 867 | { |
863 | int rc; | 868 | int rc; |
864 | 869 | ||
865 | pr_info("NFC Core ver %s\n", VERSION); | 870 | pr_info("NFC Core ver %s\n", VERSION); |
866 | 871 | ||
867 | rc = class_register(&nfc_class); | 872 | rc = class_register(&nfc_class); |
868 | if (rc) | 873 | if (rc) |
869 | return rc; | 874 | return rc; |
870 | 875 | ||
871 | rc = nfc_genl_init(); | 876 | rc = nfc_genl_init(); |
872 | if (rc) | 877 | if (rc) |
873 | goto err_genl; | 878 | goto err_genl; |
874 | 879 | ||
875 | /* the first generation must not be 0 */ | 880 | /* the first generation must not be 0 */ |
876 | nfc_devlist_generation = 1; | 881 | nfc_devlist_generation = 1; |
877 | 882 | ||
878 | rc = rawsock_init(); | 883 | rc = rawsock_init(); |
879 | if (rc) | 884 | if (rc) |
880 | goto err_rawsock; | 885 | goto err_rawsock; |
881 | 886 | ||
882 | rc = nfc_llcp_init(); | 887 | rc = nfc_llcp_init(); |
883 | if (rc) | 888 | if (rc) |
884 | goto err_llcp_sock; | 889 | goto err_llcp_sock; |
885 | 890 | ||
886 | rc = af_nfc_init(); | 891 | rc = af_nfc_init(); |
887 | if (rc) | 892 | if (rc) |
888 | goto err_af_nfc; | 893 | goto err_af_nfc; |
889 | 894 | ||
890 | return 0; | 895 | return 0; |
891 | 896 | ||
892 | err_af_nfc: | 897 | err_af_nfc: |
893 | nfc_llcp_exit(); | 898 | nfc_llcp_exit(); |
894 | err_llcp_sock: | 899 | err_llcp_sock: |
895 | rawsock_exit(); | 900 | rawsock_exit(); |
896 | err_rawsock: | 901 | err_rawsock: |
897 | nfc_genl_exit(); | 902 | nfc_genl_exit(); |
898 | err_genl: | 903 | err_genl: |
899 | class_unregister(&nfc_class); | 904 | class_unregister(&nfc_class); |
900 | return rc; | 905 | return rc; |
901 | } | 906 | } |
902 | 907 | ||
903 | static void __exit nfc_exit(void) | 908 | static void __exit nfc_exit(void) |
904 | { | 909 | { |
905 | af_nfc_exit(); | 910 | af_nfc_exit(); |
906 | nfc_llcp_exit(); | 911 | nfc_llcp_exit(); |
907 | rawsock_exit(); | 912 | rawsock_exit(); |
908 | nfc_genl_exit(); | 913 | nfc_genl_exit(); |
909 | class_unregister(&nfc_class); | 914 | class_unregister(&nfc_class); |
910 | } | 915 | } |
911 | 916 | ||
912 | subsys_initcall(nfc_init); | 917 | subsys_initcall(nfc_init); |
913 | module_exit(nfc_exit); | 918 | module_exit(nfc_exit); |
914 | 919 | ||
915 | MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>"); | 920 | MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>"); |
916 | MODULE_DESCRIPTION("NFC Core ver " VERSION); | 921 | MODULE_DESCRIPTION("NFC Core ver " VERSION); |
917 | MODULE_VERSION(VERSION); | 922 | MODULE_VERSION(VERSION); |