Commit | Line | Data |
---|---|---|
3e256b8f LRV |
1 | /* |
2 | * Copyright (C) 2011 Instituto Nokia de Tecnologia | |
3 | * | |
4 | * Authors: | |
5 | * Lauro Ramos Venancio <lauro.venancio@openbossa.org> | |
6 | * Aloisio Almeida Jr <aloisio.almeida@openbossa.org> | |
7 | * | |
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 | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the | |
20 | * Free Software Foundation, Inc., | |
21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
22 | */ | |
23 | ||
24 | #include <linux/init.h> | |
25 | #include <linux/kernel.h> | |
26 | #include <linux/module.h> | |
27 | #include <linux/slab.h> | |
28 | ||
29 | #include "nfc.h" | |
30 | ||
31 | #define VERSION "0.1" | |
32 | ||
33 | int nfc_devlist_generation; | |
34 | DEFINE_MUTEX(nfc_devlist_mutex); | |
35 | ||
36 | int nfc_printk(const char *level, const char *format, ...) | |
37 | { | |
38 | struct va_format vaf; | |
39 | va_list args; | |
40 | int r; | |
41 | ||
42 | va_start(args, format); | |
43 | ||
44 | vaf.fmt = format; | |
45 | vaf.va = &args; | |
46 | ||
47 | r = printk("%sNFC: %pV\n", level, &vaf); | |
48 | ||
49 | va_end(args); | |
50 | ||
51 | return r; | |
52 | } | |
53 | EXPORT_SYMBOL(nfc_printk); | |
54 | ||
55 | /** | |
56 | * nfc_start_poll - start polling for nfc targets | |
57 | * | |
58 | * @dev: The nfc device that must start polling | |
59 | * @protocols: bitset of nfc protocols that must be used for polling | |
60 | * | |
61 | * The device remains polling for targets until a target is found or | |
62 | * the nfc_stop_poll function is called. | |
63 | */ | |
64 | int nfc_start_poll(struct nfc_dev *dev, u32 protocols) | |
65 | { | |
66 | int rc; | |
67 | ||
68 | nfc_dbg("dev_name=%s protocols=0x%x", dev_name(&dev->dev), protocols); | |
69 | ||
70 | if (!protocols) | |
71 | return -EINVAL; | |
72 | ||
73 | device_lock(&dev->dev); | |
74 | ||
75 | if (!device_is_registered(&dev->dev)) { | |
76 | rc = -ENODEV; | |
77 | goto error; | |
78 | } | |
79 | ||
80 | if (dev->polling) { | |
81 | rc = -EBUSY; | |
82 | goto error; | |
83 | } | |
84 | ||
85 | rc = dev->ops->start_poll(dev, protocols); | |
86 | if (!rc) | |
87 | dev->polling = true; | |
88 | ||
89 | error: | |
90 | device_unlock(&dev->dev); | |
91 | return rc; | |
92 | } | |
93 | ||
94 | /** | |
95 | * nfc_stop_poll - stop polling for nfc targets | |
96 | * | |
97 | * @dev: The nfc device that must stop polling | |
98 | */ | |
99 | int nfc_stop_poll(struct nfc_dev *dev) | |
100 | { | |
101 | int rc = 0; | |
102 | ||
103 | nfc_dbg("dev_name=%s", dev_name(&dev->dev)); | |
104 | ||
105 | device_lock(&dev->dev); | |
106 | ||
107 | if (!device_is_registered(&dev->dev)) { | |
108 | rc = -ENODEV; | |
109 | goto error; | |
110 | } | |
111 | ||
112 | if (!dev->polling) { | |
113 | rc = -EINVAL; | |
114 | goto error; | |
115 | } | |
116 | ||
117 | dev->ops->stop_poll(dev); | |
118 | dev->polling = false; | |
119 | ||
120 | error: | |
121 | device_unlock(&dev->dev); | |
122 | return rc; | |
123 | } | |
124 | ||
125 | /** | |
126 | * nfc_activate_target - prepare the target for data exchange | |
127 | * | |
128 | * @dev: The nfc device that found the target | |
129 | * @target_idx: index of the target that must be activated | |
130 | * @protocol: nfc protocol that will be used for data exchange | |
131 | */ | |
132 | int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol) | |
133 | { | |
134 | int rc; | |
135 | ||
136 | nfc_dbg("dev_name=%s target_idx=%u protocol=%u", dev_name(&dev->dev), | |
137 | target_idx, protocol); | |
138 | ||
139 | device_lock(&dev->dev); | |
140 | ||
141 | if (!device_is_registered(&dev->dev)) { | |
142 | rc = -ENODEV; | |
143 | goto error; | |
144 | } | |
145 | ||
146 | rc = dev->ops->activate_target(dev, target_idx, protocol); | |
147 | ||
148 | error: | |
149 | device_unlock(&dev->dev); | |
150 | return rc; | |
151 | } | |
152 | ||
153 | /** | |
154 | * nfc_deactivate_target - deactivate a nfc target | |
155 | * | |
156 | * @dev: The nfc device that found the target | |
157 | * @target_idx: index of the target that must be deactivated | |
158 | */ | |
159 | int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx) | |
160 | { | |
161 | int rc = 0; | |
162 | ||
163 | nfc_dbg("dev_name=%s target_idx=%u", dev_name(&dev->dev), target_idx); | |
164 | ||
165 | device_lock(&dev->dev); | |
166 | ||
167 | if (!device_is_registered(&dev->dev)) { | |
168 | rc = -ENODEV; | |
169 | goto error; | |
170 | } | |
171 | ||
172 | dev->ops->deactivate_target(dev, target_idx); | |
173 | ||
174 | error: | |
175 | device_unlock(&dev->dev); | |
176 | return rc; | |
177 | } | |
178 | ||
179 | /** | |
180 | * nfc_data_exchange - transceive data | |
181 | * | |
182 | * @dev: The nfc device that found the target | |
183 | * @target_idx: index of the target | |
184 | * @skb: data to be sent | |
185 | * @cb: callback called when the response is received | |
186 | * @cb_context: parameter for the callback function | |
187 | * | |
188 | * The user must wait for the callback before calling this function again. | |
189 | */ | |
190 | int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, | |
191 | struct sk_buff *skb, | |
192 | data_exchange_cb_t cb, | |
193 | void *cb_context) | |
194 | { | |
195 | int rc; | |
196 | ||
197 | nfc_dbg("dev_name=%s target_idx=%u skb->len=%u", dev_name(&dev->dev), | |
198 | target_idx, skb->len); | |
199 | ||
200 | device_lock(&dev->dev); | |
201 | ||
202 | if (!device_is_registered(&dev->dev)) { | |
203 | rc = -ENODEV; | |
204 | kfree_skb(skb); | |
205 | goto error; | |
206 | } | |
207 | ||
208 | rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context); | |
209 | ||
210 | error: | |
211 | device_unlock(&dev->dev); | |
212 | return rc; | |
213 | } | |
214 | ||
215 | /** | |
216 | * nfc_alloc_skb - allocate a skb for data exchange responses | |
217 | * | |
218 | * @size: size to allocate | |
219 | * @gfp: gfp flags | |
220 | */ | |
221 | struct sk_buff *nfc_alloc_skb(unsigned int size, gfp_t gfp) | |
222 | { | |
223 | struct sk_buff *skb; | |
224 | unsigned int total_size; | |
225 | ||
226 | total_size = size + 1; | |
227 | skb = alloc_skb(total_size, gfp); | |
228 | ||
229 | if (skb) | |
230 | skb_reserve(skb, 1); | |
231 | ||
232 | return skb; | |
233 | } | |
234 | EXPORT_SYMBOL(nfc_alloc_skb); | |
235 | ||
4d12b8b1 LRV |
236 | /** |
237 | * nfc_targets_found - inform that targets were found | |
238 | * | |
239 | * @dev: The nfc device that found the targets | |
240 | * @targets: array of nfc targets found | |
241 | * @ntargets: targets array size | |
242 | * | |
243 | * The device driver must call this function when one or many nfc targets | |
244 | * are found. After calling this function, the device driver must stop | |
245 | * polling for targets. | |
246 | */ | |
247 | int nfc_targets_found(struct nfc_dev *dev, struct nfc_target *targets, | |
248 | int n_targets) | |
249 | { | |
250 | int i; | |
251 | ||
252 | nfc_dbg("dev_name=%s n_targets=%d", dev_name(&dev->dev), n_targets); | |
253 | ||
254 | dev->polling = false; | |
255 | ||
256 | for (i = 0; i < n_targets; i++) | |
257 | targets[i].idx = dev->target_idx++; | |
258 | ||
259 | spin_lock_bh(&dev->targets_lock); | |
260 | ||
261 | dev->targets_generation++; | |
262 | ||
263 | kfree(dev->targets); | |
264 | dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target), | |
265 | GFP_ATOMIC); | |
266 | ||
267 | if (!dev->targets) { | |
268 | dev->n_targets = 0; | |
269 | spin_unlock_bh(&dev->targets_lock); | |
270 | return -ENOMEM; | |
271 | } | |
272 | ||
273 | dev->n_targets = n_targets; | |
274 | spin_unlock_bh(&dev->targets_lock); | |
275 | ||
276 | nfc_genl_targets_found(dev); | |
277 | ||
278 | return 0; | |
279 | } | |
280 | EXPORT_SYMBOL(nfc_targets_found); | |
281 | ||
3e256b8f LRV |
282 | static void nfc_release(struct device *d) |
283 | { | |
284 | struct nfc_dev *dev = to_nfc_dev(d); | |
285 | ||
286 | nfc_dbg("dev_name=%s", dev_name(&dev->dev)); | |
287 | ||
4d12b8b1 LRV |
288 | nfc_genl_data_exit(&dev->genl_data); |
289 | kfree(dev->targets); | |
3e256b8f LRV |
290 | kfree(dev); |
291 | } | |
292 | ||
293 | struct class nfc_class = { | |
294 | .name = "nfc", | |
295 | .dev_release = nfc_release, | |
296 | }; | |
297 | EXPORT_SYMBOL(nfc_class); | |
298 | ||
299 | static int match_idx(struct device *d, void *data) | |
300 | { | |
301 | struct nfc_dev *dev = to_nfc_dev(d); | |
302 | unsigned *idx = data; | |
303 | ||
304 | return dev->idx == *idx; | |
305 | } | |
306 | ||
307 | struct nfc_dev *nfc_get_device(unsigned idx) | |
308 | { | |
309 | struct device *d; | |
310 | ||
311 | d = class_find_device(&nfc_class, NULL, &idx, match_idx); | |
312 | if (!d) | |
313 | return NULL; | |
314 | ||
315 | return to_nfc_dev(d); | |
316 | } | |
317 | ||
318 | /** | |
319 | * nfc_allocate_device - allocate a new nfc device | |
320 | * | |
321 | * @ops: device operations | |
322 | * @supported_protocols: NFC protocols supported by the device | |
323 | */ | |
324 | struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops, | |
325 | u32 supported_protocols) | |
326 | { | |
327 | static atomic_t dev_no = ATOMIC_INIT(0); | |
328 | struct nfc_dev *dev; | |
329 | ||
330 | if (!ops->start_poll || !ops->stop_poll || !ops->activate_target || | |
331 | !ops->deactivate_target || !ops->data_exchange) | |
332 | return NULL; | |
333 | ||
334 | if (!supported_protocols) | |
335 | return NULL; | |
336 | ||
337 | dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL); | |
338 | if (!dev) | |
339 | return NULL; | |
340 | ||
341 | dev->dev.class = &nfc_class; | |
342 | dev->idx = atomic_inc_return(&dev_no) - 1; | |
343 | dev_set_name(&dev->dev, "nfc%d", dev->idx); | |
344 | device_initialize(&dev->dev); | |
345 | ||
346 | dev->ops = ops; | |
347 | dev->supported_protocols = supported_protocols; | |
348 | ||
4d12b8b1 LRV |
349 | spin_lock_init(&dev->targets_lock); |
350 | nfc_genl_data_init(&dev->genl_data); | |
351 | ||
352 | /* first generation must not be 0 */ | |
353 | dev->targets_generation = 1; | |
354 | ||
3e256b8f LRV |
355 | return dev; |
356 | } | |
357 | EXPORT_SYMBOL(nfc_allocate_device); | |
358 | ||
359 | /** | |
360 | * nfc_register_device - register a nfc device in the nfc subsystem | |
361 | * | |
362 | * @dev: The nfc device to register | |
363 | */ | |
364 | int nfc_register_device(struct nfc_dev *dev) | |
365 | { | |
366 | int rc; | |
367 | ||
368 | nfc_dbg("dev_name=%s", dev_name(&dev->dev)); | |
369 | ||
370 | mutex_lock(&nfc_devlist_mutex); | |
371 | nfc_devlist_generation++; | |
372 | rc = device_add(&dev->dev); | |
373 | mutex_unlock(&nfc_devlist_mutex); | |
374 | ||
4d12b8b1 LRV |
375 | if (rc < 0) |
376 | return rc; | |
377 | ||
378 | rc = nfc_genl_device_added(dev); | |
379 | if (rc) | |
380 | nfc_dbg("The userspace won't be notified that the device %s was" | |
381 | " added", dev_name(&dev->dev)); | |
382 | ||
383 | ||
384 | return 0; | |
3e256b8f LRV |
385 | } |
386 | EXPORT_SYMBOL(nfc_register_device); | |
387 | ||
388 | /** | |
389 | * nfc_unregister_device - unregister a nfc device in the nfc subsystem | |
390 | * | |
391 | * @dev: The nfc device to unregister | |
392 | */ | |
393 | void nfc_unregister_device(struct nfc_dev *dev) | |
394 | { | |
4d12b8b1 LRV |
395 | int rc; |
396 | ||
3e256b8f LRV |
397 | nfc_dbg("dev_name=%s", dev_name(&dev->dev)); |
398 | ||
399 | mutex_lock(&nfc_devlist_mutex); | |
400 | nfc_devlist_generation++; | |
401 | ||
402 | /* lock to avoid unregistering a device while an operation | |
403 | is in progress */ | |
404 | device_lock(&dev->dev); | |
405 | device_del(&dev->dev); | |
406 | device_unlock(&dev->dev); | |
407 | ||
408 | mutex_unlock(&nfc_devlist_mutex); | |
4d12b8b1 LRV |
409 | |
410 | rc = nfc_genl_device_removed(dev); | |
411 | if (rc) | |
412 | nfc_dbg("The userspace won't be notified that the device %s" | |
413 | " was removed", dev_name(&dev->dev)); | |
414 | ||
3e256b8f LRV |
415 | } |
416 | EXPORT_SYMBOL(nfc_unregister_device); | |
417 | ||
418 | static int __init nfc_init(void) | |
419 | { | |
4d12b8b1 LRV |
420 | int rc; |
421 | ||
3e256b8f LRV |
422 | nfc_info("NFC Core ver %s", VERSION); |
423 | ||
4d12b8b1 LRV |
424 | rc = class_register(&nfc_class); |
425 | if (rc) | |
426 | return rc; | |
427 | ||
428 | rc = nfc_genl_init(); | |
429 | if (rc) | |
430 | goto err_genl; | |
431 | ||
432 | /* the first generation must not be 0 */ | |
433 | nfc_devlist_generation = 1; | |
434 | ||
c7fe3b52 AAJ |
435 | rc = af_nfc_init(); |
436 | if (rc) | |
437 | goto err_af_nfc; | |
438 | ||
4d12b8b1 LRV |
439 | return 0; |
440 | ||
c7fe3b52 AAJ |
441 | err_af_nfc: |
442 | nfc_genl_exit(); | |
4d12b8b1 LRV |
443 | err_genl: |
444 | class_unregister(&nfc_class); | |
445 | return rc; | |
3e256b8f LRV |
446 | } |
447 | ||
448 | static void __exit nfc_exit(void) | |
449 | { | |
c7fe3b52 | 450 | af_nfc_exit(); |
4d12b8b1 | 451 | nfc_genl_exit(); |
3e256b8f LRV |
452 | class_unregister(&nfc_class); |
453 | } | |
454 | ||
455 | subsys_initcall(nfc_init); | |
456 | module_exit(nfc_exit); | |
457 | ||
458 | MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>"); | |
459 | MODULE_DESCRIPTION("NFC Core ver " VERSION); | |
460 | MODULE_VERSION(VERSION); | |
461 | MODULE_LICENSE("GPL"); |