361bc37d2db1953aa9aba7d61553c4838a826686
[deliverable/linux.git] / net / nfc / digital_core.c
1 /*
2 * NFC Digital Protocol stack
3 * Copyright (c) 2013, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 */
15
16 #define pr_fmt(fmt) "digital: %s: " fmt, __func__
17
18 #include <linux/module.h>
19
20 #include "digital.h"
21
22 #define DIGITAL_PROTO_NFCA_RF_TECH \
23 (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK | NFC_PROTO_NFC_DEP_MASK)
24
25 #define DIGITAL_PROTO_NFCB_RF_TECH NFC_PROTO_ISO14443_B_MASK
26
27 #define DIGITAL_PROTO_NFCF_RF_TECH \
28 (NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK)
29
30 #define DIGITAL_PROTO_ISO15693_RF_TECH NFC_PROTO_ISO15693_MASK
31
32 struct digital_cmd {
33 struct list_head queue;
34
35 u8 type;
36 u8 pending;
37
38 u16 timeout;
39 struct sk_buff *req;
40 struct sk_buff *resp;
41 struct digital_tg_mdaa_params *mdaa_params;
42
43 nfc_digital_cmd_complete_t cmd_cb;
44 void *cb_context;
45 };
46
47 struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
48 unsigned int len)
49 {
50 struct sk_buff *skb;
51
52 skb = alloc_skb(len + ddev->tx_headroom + ddev->tx_tailroom,
53 GFP_KERNEL);
54 if (skb)
55 skb_reserve(skb, ddev->tx_headroom);
56
57 return skb;
58 }
59
60 void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init,
61 u8 bitwise_inv, u8 msb_first)
62 {
63 u16 crc;
64
65 crc = crc_func(init, skb->data, skb->len);
66
67 if (bitwise_inv)
68 crc = ~crc;
69
70 if (msb_first)
71 crc = __fswab16(crc);
72
73 *skb_put(skb, 1) = crc & 0xFF;
74 *skb_put(skb, 1) = (crc >> 8) & 0xFF;
75 }
76
77 int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func,
78 u16 crc_init, u8 bitwise_inv, u8 msb_first)
79 {
80 int rc;
81 u16 crc;
82
83 if (skb->len <= 2)
84 return -EIO;
85
86 crc = crc_func(crc_init, skb->data, skb->len - 2);
87
88 if (bitwise_inv)
89 crc = ~crc;
90
91 if (msb_first)
92 crc = __swab16(crc);
93
94 rc = (skb->data[skb->len - 2] - (crc & 0xFF)) +
95 (skb->data[skb->len - 1] - ((crc >> 8) & 0xFF));
96
97 if (rc)
98 return -EIO;
99
100 skb_trim(skb, skb->len - 2);
101
102 return 0;
103 }
104
105 static inline void digital_switch_rf(struct nfc_digital_dev *ddev, bool on)
106 {
107 ddev->ops->switch_rf(ddev, on);
108 }
109
110 static inline void digital_abort_cmd(struct nfc_digital_dev *ddev)
111 {
112 ddev->ops->abort_cmd(ddev);
113 }
114
115 static void digital_wq_cmd_complete(struct work_struct *work)
116 {
117 struct digital_cmd *cmd;
118 struct nfc_digital_dev *ddev = container_of(work,
119 struct nfc_digital_dev,
120 cmd_complete_work);
121
122 mutex_lock(&ddev->cmd_lock);
123
124 cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
125 queue);
126 if (!cmd) {
127 mutex_unlock(&ddev->cmd_lock);
128 return;
129 }
130
131 list_del(&cmd->queue);
132
133 mutex_unlock(&ddev->cmd_lock);
134
135 if (!IS_ERR(cmd->resp))
136 print_hex_dump_debug("DIGITAL RX: ", DUMP_PREFIX_NONE, 16, 1,
137 cmd->resp->data, cmd->resp->len, false);
138
139 cmd->cmd_cb(ddev, cmd->cb_context, cmd->resp);
140
141 kfree(cmd->mdaa_params);
142 kfree(cmd);
143
144 schedule_work(&ddev->cmd_work);
145 }
146
147 static void digital_send_cmd_complete(struct nfc_digital_dev *ddev,
148 void *arg, struct sk_buff *resp)
149 {
150 struct digital_cmd *cmd = arg;
151
152 cmd->resp = resp;
153
154 schedule_work(&ddev->cmd_complete_work);
155 }
156
157 static void digital_wq_cmd(struct work_struct *work)
158 {
159 int rc;
160 struct digital_cmd *cmd;
161 struct digital_tg_mdaa_params *params;
162 struct nfc_digital_dev *ddev = container_of(work,
163 struct nfc_digital_dev,
164 cmd_work);
165
166 mutex_lock(&ddev->cmd_lock);
167
168 cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
169 queue);
170 if (!cmd || cmd->pending) {
171 mutex_unlock(&ddev->cmd_lock);
172 return;
173 }
174
175 mutex_unlock(&ddev->cmd_lock);
176
177 if (cmd->req)
178 print_hex_dump_debug("DIGITAL TX: ", DUMP_PREFIX_NONE, 16, 1,
179 cmd->req->data, cmd->req->len, false);
180
181 switch (cmd->type) {
182 case DIGITAL_CMD_IN_SEND:
183 rc = ddev->ops->in_send_cmd(ddev, cmd->req, cmd->timeout,
184 digital_send_cmd_complete, cmd);
185 break;
186
187 case DIGITAL_CMD_TG_SEND:
188 rc = ddev->ops->tg_send_cmd(ddev, cmd->req, cmd->timeout,
189 digital_send_cmd_complete, cmd);
190 break;
191
192 case DIGITAL_CMD_TG_LISTEN:
193 rc = ddev->ops->tg_listen(ddev, cmd->timeout,
194 digital_send_cmd_complete, cmd);
195 break;
196
197 case DIGITAL_CMD_TG_LISTEN_MDAA:
198 params = cmd->mdaa_params;
199
200 rc = ddev->ops->tg_listen_mdaa(ddev, params, cmd->timeout,
201 digital_send_cmd_complete, cmd);
202 break;
203
204 default:
205 pr_err("Unknown cmd type %d\n", cmd->type);
206 return;
207 }
208
209 if (!rc)
210 return;
211
212 pr_err("in_send_command returned err %d\n", rc);
213
214 mutex_lock(&ddev->cmd_lock);
215 list_del(&cmd->queue);
216 mutex_unlock(&ddev->cmd_lock);
217
218 kfree_skb(cmd->req);
219 kfree(cmd->mdaa_params);
220 kfree(cmd);
221
222 schedule_work(&ddev->cmd_work);
223 }
224
225 int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
226 struct sk_buff *skb, struct digital_tg_mdaa_params *params,
227 u16 timeout, nfc_digital_cmd_complete_t cmd_cb,
228 void *cb_context)
229 {
230 struct digital_cmd *cmd;
231
232 cmd = kzalloc(sizeof(struct digital_cmd), GFP_KERNEL);
233 if (!cmd)
234 return -ENOMEM;
235
236 cmd->type = cmd_type;
237 cmd->timeout = timeout;
238 cmd->req = skb;
239 cmd->mdaa_params = params;
240 cmd->cmd_cb = cmd_cb;
241 cmd->cb_context = cb_context;
242 INIT_LIST_HEAD(&cmd->queue);
243
244 mutex_lock(&ddev->cmd_lock);
245 list_add_tail(&cmd->queue, &ddev->cmd_queue);
246 mutex_unlock(&ddev->cmd_lock);
247
248 schedule_work(&ddev->cmd_work);
249
250 return 0;
251 }
252
253 int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
254 {
255 int rc;
256
257 rc = ddev->ops->in_configure_hw(ddev, type, param);
258 if (rc)
259 pr_err("in_configure_hw failed: %d\n", rc);
260
261 return rc;
262 }
263
264 int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
265 {
266 int rc;
267
268 rc = ddev->ops->tg_configure_hw(ddev, type, param);
269 if (rc)
270 pr_err("tg_configure_hw failed: %d\n", rc);
271
272 return rc;
273 }
274
275 static int digital_tg_listen_mdaa(struct nfc_digital_dev *ddev, u8 rf_tech)
276 {
277 struct digital_tg_mdaa_params *params;
278
279 params = kzalloc(sizeof(struct digital_tg_mdaa_params), GFP_KERNEL);
280 if (!params)
281 return -ENOMEM;
282
283 params->sens_res = DIGITAL_SENS_RES_NFC_DEP;
284 get_random_bytes(params->nfcid1, sizeof(params->nfcid1));
285 params->sel_res = DIGITAL_SEL_RES_NFC_DEP;
286
287 params->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
288 params->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
289 get_random_bytes(params->nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2);
290 params->sc = DIGITAL_SENSF_FELICA_SC;
291
292 return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MDAA, NULL, params,
293 500, digital_tg_recv_atr_req, NULL);
294 }
295
296 int digital_target_found(struct nfc_digital_dev *ddev,
297 struct nfc_target *target, u8 protocol)
298 {
299 int rc;
300 u8 framing;
301 u8 rf_tech;
302 u8 poll_tech_count;
303 int (*check_crc)(struct sk_buff *skb);
304 void (*add_crc)(struct sk_buff *skb);
305
306 rf_tech = ddev->poll_techs[ddev->poll_tech_index].rf_tech;
307
308 switch (protocol) {
309 case NFC_PROTO_JEWEL:
310 framing = NFC_DIGITAL_FRAMING_NFCA_T1T;
311 check_crc = digital_skb_check_crc_b;
312 add_crc = digital_skb_add_crc_b;
313 break;
314
315 case NFC_PROTO_MIFARE:
316 framing = NFC_DIGITAL_FRAMING_NFCA_T2T;
317 check_crc = digital_skb_check_crc_a;
318 add_crc = digital_skb_add_crc_a;
319 break;
320
321 case NFC_PROTO_FELICA:
322 framing = NFC_DIGITAL_FRAMING_NFCF_T3T;
323 check_crc = digital_skb_check_crc_f;
324 add_crc = digital_skb_add_crc_f;
325 break;
326
327 case NFC_PROTO_NFC_DEP:
328 if (rf_tech == NFC_DIGITAL_RF_TECH_106A) {
329 framing = NFC_DIGITAL_FRAMING_NFCA_NFC_DEP;
330 check_crc = digital_skb_check_crc_a;
331 add_crc = digital_skb_add_crc_a;
332 } else {
333 framing = NFC_DIGITAL_FRAMING_NFCF_NFC_DEP;
334 check_crc = digital_skb_check_crc_f;
335 add_crc = digital_skb_add_crc_f;
336 }
337 break;
338
339 case NFC_PROTO_ISO15693:
340 framing = NFC_DIGITAL_FRAMING_ISO15693_T5T;
341 check_crc = digital_skb_check_crc_b;
342 add_crc = digital_skb_add_crc_b;
343 break;
344
345 case NFC_PROTO_ISO14443:
346 framing = NFC_DIGITAL_FRAMING_NFCA_T4T;
347 check_crc = digital_skb_check_crc_a;
348 add_crc = digital_skb_add_crc_a;
349 break;
350
351 case NFC_PROTO_ISO14443_B:
352 framing = NFC_DIGITAL_FRAMING_NFCB_T4T;
353 check_crc = digital_skb_check_crc_b;
354 add_crc = digital_skb_add_crc_b;
355 break;
356
357 default:
358 pr_err("Invalid protocol %d\n", protocol);
359 return -EINVAL;
360 }
361
362 pr_debug("rf_tech=%d, protocol=%d\n", rf_tech, protocol);
363
364 ddev->curr_rf_tech = rf_tech;
365
366 if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
367 ddev->skb_add_crc = digital_skb_add_crc_none;
368 ddev->skb_check_crc = digital_skb_check_crc_none;
369 } else {
370 ddev->skb_add_crc = add_crc;
371 ddev->skb_check_crc = check_crc;
372 }
373
374 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, framing);
375 if (rc)
376 return rc;
377
378 target->supported_protocols = (1 << protocol);
379
380 poll_tech_count = ddev->poll_tech_count;
381 ddev->poll_tech_count = 0;
382
383 rc = nfc_targets_found(ddev->nfc_dev, target, 1);
384 if (rc) {
385 ddev->poll_tech_count = poll_tech_count;
386 return rc;
387 }
388
389 return 0;
390 }
391
392 void digital_poll_next_tech(struct nfc_digital_dev *ddev)
393 {
394 u8 rand_mod;
395
396 digital_switch_rf(ddev, 0);
397
398 mutex_lock(&ddev->poll_lock);
399
400 if (!ddev->poll_tech_count) {
401 mutex_unlock(&ddev->poll_lock);
402 return;
403 }
404
405 get_random_bytes(&rand_mod, sizeof(rand_mod));
406 ddev->poll_tech_index = rand_mod % ddev->poll_tech_count;
407
408 mutex_unlock(&ddev->poll_lock);
409
410 schedule_work(&ddev->poll_work);
411 }
412
413 static void digital_wq_poll(struct work_struct *work)
414 {
415 int rc;
416 struct digital_poll_tech *poll_tech;
417 struct nfc_digital_dev *ddev = container_of(work,
418 struct nfc_digital_dev,
419 poll_work);
420 mutex_lock(&ddev->poll_lock);
421
422 if (!ddev->poll_tech_count) {
423 mutex_unlock(&ddev->poll_lock);
424 return;
425 }
426
427 poll_tech = &ddev->poll_techs[ddev->poll_tech_index];
428
429 mutex_unlock(&ddev->poll_lock);
430
431 rc = poll_tech->poll_func(ddev, poll_tech->rf_tech);
432 if (rc)
433 digital_poll_next_tech(ddev);
434 }
435
436 static void digital_add_poll_tech(struct nfc_digital_dev *ddev, u8 rf_tech,
437 digital_poll_t poll_func)
438 {
439 struct digital_poll_tech *poll_tech;
440
441 if (ddev->poll_tech_count >= NFC_DIGITAL_POLL_MODE_COUNT_MAX)
442 return;
443
444 poll_tech = &ddev->poll_techs[ddev->poll_tech_count++];
445
446 poll_tech->rf_tech = rf_tech;
447 poll_tech->poll_func = poll_func;
448 }
449
450 /**
451 * start_poll operation
452 *
453 * For every supported protocol, the corresponding polling function is added
454 * to the table of polling technologies (ddev->poll_techs[]) using
455 * digital_add_poll_tech().
456 * When a polling function fails (by timeout or protocol error) the next one is
457 * schedule by digital_poll_next_tech() on the poll workqueue (ddev->poll_work).
458 */
459 static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
460 __u32 tm_protocols)
461 {
462 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
463 u32 matching_im_protocols, matching_tm_protocols;
464
465 pr_debug("protocols: im 0x%x, tm 0x%x, supported 0x%x\n", im_protocols,
466 tm_protocols, ddev->protocols);
467
468 matching_im_protocols = ddev->protocols & im_protocols;
469 matching_tm_protocols = ddev->protocols & tm_protocols;
470
471 if (!matching_im_protocols && !matching_tm_protocols) {
472 pr_err("Unknown protocol\n");
473 return -EINVAL;
474 }
475
476 if (ddev->poll_tech_count) {
477 pr_err("Already polling\n");
478 return -EBUSY;
479 }
480
481 if (ddev->curr_protocol) {
482 pr_err("A target is already active\n");
483 return -EBUSY;
484 }
485
486 ddev->poll_tech_count = 0;
487 ddev->poll_tech_index = 0;
488
489 if (matching_im_protocols & DIGITAL_PROTO_NFCA_RF_TECH)
490 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
491 digital_in_send_sens_req);
492
493 if (matching_im_protocols & DIGITAL_PROTO_NFCB_RF_TECH)
494 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106B,
495 digital_in_send_sensb_req);
496
497 if (matching_im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) {
498 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
499 digital_in_send_sensf_req);
500
501 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
502 digital_in_send_sensf_req);
503 }
504
505 if (matching_im_protocols & DIGITAL_PROTO_ISO15693_RF_TECH)
506 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_ISO15693,
507 digital_in_send_iso15693_inv_req);
508
509 if (matching_tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
510 if (ddev->ops->tg_listen_mdaa) {
511 digital_add_poll_tech(ddev, 0,
512 digital_tg_listen_mdaa);
513 } else {
514 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
515 digital_tg_listen_nfca);
516
517 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
518 digital_tg_listen_nfcf);
519
520 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
521 digital_tg_listen_nfcf);
522 }
523 }
524
525 if (!ddev->poll_tech_count) {
526 pr_err("Unsupported protocols: im=0x%x, tm=0x%x\n",
527 matching_im_protocols, matching_tm_protocols);
528 return -EINVAL;
529 }
530
531 schedule_work(&ddev->poll_work);
532
533 return 0;
534 }
535
536 static void digital_stop_poll(struct nfc_dev *nfc_dev)
537 {
538 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
539
540 mutex_lock(&ddev->poll_lock);
541
542 if (!ddev->poll_tech_count) {
543 pr_err("Polling operation was not running\n");
544 mutex_unlock(&ddev->poll_lock);
545 return;
546 }
547
548 ddev->poll_tech_count = 0;
549
550 mutex_unlock(&ddev->poll_lock);
551
552 cancel_work_sync(&ddev->poll_work);
553
554 digital_abort_cmd(ddev);
555 }
556
557 static int digital_dev_up(struct nfc_dev *nfc_dev)
558 {
559 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
560
561 digital_switch_rf(ddev, 1);
562
563 return 0;
564 }
565
566 static int digital_dev_down(struct nfc_dev *nfc_dev)
567 {
568 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
569
570 digital_switch_rf(ddev, 0);
571
572 return 0;
573 }
574
575 static int digital_dep_link_up(struct nfc_dev *nfc_dev,
576 struct nfc_target *target,
577 __u8 comm_mode, __u8 *gb, size_t gb_len)
578 {
579 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
580 int rc;
581
582 rc = digital_in_send_atr_req(ddev, target, comm_mode, gb, gb_len);
583
584 if (!rc)
585 ddev->curr_protocol = NFC_PROTO_NFC_DEP;
586
587 return rc;
588 }
589
590 static int digital_dep_link_down(struct nfc_dev *nfc_dev)
591 {
592 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
593
594 ddev->curr_protocol = 0;
595
596 return 0;
597 }
598
599 static int digital_activate_target(struct nfc_dev *nfc_dev,
600 struct nfc_target *target, __u32 protocol)
601 {
602 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
603
604 if (ddev->poll_tech_count) {
605 pr_err("Can't activate a target while polling\n");
606 return -EBUSY;
607 }
608
609 if (ddev->curr_protocol) {
610 pr_err("A target is already active\n");
611 return -EBUSY;
612 }
613
614 ddev->curr_protocol = protocol;
615
616 return 0;
617 }
618
619 static void digital_deactivate_target(struct nfc_dev *nfc_dev,
620 struct nfc_target *target)
621 {
622 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
623
624 if (!ddev->curr_protocol) {
625 pr_err("No active target\n");
626 return;
627 }
628
629 ddev->curr_protocol = 0;
630 }
631
632 static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb)
633 {
634 struct nfc_digital_dev *ddev = nfc_get_drvdata(dev);
635
636 return digital_tg_send_dep_res(ddev, skb);
637 }
638
639 static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg,
640 struct sk_buff *resp)
641 {
642 struct digital_data_exch *data_exch = arg;
643 int rc;
644
645 if (IS_ERR(resp)) {
646 rc = PTR_ERR(resp);
647 resp = NULL;
648 goto done;
649 }
650
651 if (ddev->curr_protocol == NFC_PROTO_MIFARE) {
652 rc = digital_in_recv_mifare_res(resp);
653 /* crc check is done in digital_in_recv_mifare_res() */
654 goto done;
655 }
656
657 if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
658 (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
659 rc = digital_in_iso_dep_pull_sod(ddev, resp);
660 if (rc)
661 goto done;
662 }
663
664 rc = ddev->skb_check_crc(resp);
665
666 done:
667 if (rc) {
668 kfree_skb(resp);
669 resp = NULL;
670 }
671
672 data_exch->cb(data_exch->cb_context, resp, rc);
673
674 kfree(data_exch);
675 }
676
677 static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
678 struct sk_buff *skb, data_exchange_cb_t cb,
679 void *cb_context)
680 {
681 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
682 struct digital_data_exch *data_exch;
683 int rc;
684
685 data_exch = kzalloc(sizeof(struct digital_data_exch), GFP_KERNEL);
686 if (!data_exch) {
687 pr_err("Failed to allocate data_exch struct\n");
688 return -ENOMEM;
689 }
690
691 data_exch->cb = cb;
692 data_exch->cb_context = cb_context;
693
694 if (ddev->curr_protocol == NFC_PROTO_NFC_DEP) {
695 rc = digital_in_send_dep_req(ddev, target, skb, data_exch);
696 goto exit;
697 }
698
699 if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
700 (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
701 rc = digital_in_iso_dep_push_sod(ddev, skb);
702 if (rc)
703 goto exit;
704 }
705
706 ddev->skb_add_crc(skb);
707
708 rc = digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete,
709 data_exch);
710
711 exit:
712 if (rc)
713 kfree(data_exch);
714
715 return rc;
716 }
717
718 static struct nfc_ops digital_nfc_ops = {
719 .dev_up = digital_dev_up,
720 .dev_down = digital_dev_down,
721 .start_poll = digital_start_poll,
722 .stop_poll = digital_stop_poll,
723 .dep_link_up = digital_dep_link_up,
724 .dep_link_down = digital_dep_link_down,
725 .activate_target = digital_activate_target,
726 .deactivate_target = digital_deactivate_target,
727 .tm_send = digital_tg_send,
728 .im_transceive = digital_in_send,
729 };
730
731 struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops,
732 __u32 supported_protocols,
733 __u32 driver_capabilities,
734 int tx_headroom, int tx_tailroom)
735 {
736 struct nfc_digital_dev *ddev;
737
738 if (!ops->in_configure_hw || !ops->in_send_cmd || !ops->tg_listen ||
739 !ops->tg_configure_hw || !ops->tg_send_cmd || !ops->abort_cmd ||
740 !ops->switch_rf)
741 return NULL;
742
743 ddev = kzalloc(sizeof(struct nfc_digital_dev), GFP_KERNEL);
744 if (!ddev)
745 return NULL;
746
747 ddev->driver_capabilities = driver_capabilities;
748 ddev->ops = ops;
749
750 mutex_init(&ddev->cmd_lock);
751 INIT_LIST_HEAD(&ddev->cmd_queue);
752
753 INIT_WORK(&ddev->cmd_work, digital_wq_cmd);
754 INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete);
755
756 mutex_init(&ddev->poll_lock);
757 INIT_WORK(&ddev->poll_work, digital_wq_poll);
758
759 if (supported_protocols & NFC_PROTO_JEWEL_MASK)
760 ddev->protocols |= NFC_PROTO_JEWEL_MASK;
761 if (supported_protocols & NFC_PROTO_MIFARE_MASK)
762 ddev->protocols |= NFC_PROTO_MIFARE_MASK;
763 if (supported_protocols & NFC_PROTO_FELICA_MASK)
764 ddev->protocols |= NFC_PROTO_FELICA_MASK;
765 if (supported_protocols & NFC_PROTO_NFC_DEP_MASK)
766 ddev->protocols |= NFC_PROTO_NFC_DEP_MASK;
767 if (supported_protocols & NFC_PROTO_ISO15693_MASK)
768 ddev->protocols |= NFC_PROTO_ISO15693_MASK;
769 if (supported_protocols & NFC_PROTO_ISO14443_MASK)
770 ddev->protocols |= NFC_PROTO_ISO14443_MASK;
771 if (supported_protocols & NFC_PROTO_ISO14443_B_MASK)
772 ddev->protocols |= NFC_PROTO_ISO14443_B_MASK;
773
774 ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
775 ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
776
777 ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols,
778 ddev->tx_headroom,
779 ddev->tx_tailroom);
780 if (!ddev->nfc_dev) {
781 pr_err("nfc_allocate_device failed\n");
782 goto free_dev;
783 }
784
785 nfc_set_drvdata(ddev->nfc_dev, ddev);
786
787 return ddev;
788
789 free_dev:
790 kfree(ddev);
791
792 return NULL;
793 }
794 EXPORT_SYMBOL(nfc_digital_allocate_device);
795
796 void nfc_digital_free_device(struct nfc_digital_dev *ddev)
797 {
798 nfc_free_device(ddev->nfc_dev);
799 kfree(ddev);
800 }
801 EXPORT_SYMBOL(nfc_digital_free_device);
802
803 int nfc_digital_register_device(struct nfc_digital_dev *ddev)
804 {
805 return nfc_register_device(ddev->nfc_dev);
806 }
807 EXPORT_SYMBOL(nfc_digital_register_device);
808
809 void nfc_digital_unregister_device(struct nfc_digital_dev *ddev)
810 {
811 struct digital_cmd *cmd, *n;
812
813 nfc_unregister_device(ddev->nfc_dev);
814
815 mutex_lock(&ddev->poll_lock);
816 ddev->poll_tech_count = 0;
817 mutex_unlock(&ddev->poll_lock);
818
819 cancel_work_sync(&ddev->poll_work);
820 cancel_work_sync(&ddev->cmd_work);
821 cancel_work_sync(&ddev->cmd_complete_work);
822
823 list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) {
824 list_del(&cmd->queue);
825 kfree(cmd->mdaa_params);
826 kfree(cmd);
827 }
828 }
829 EXPORT_SYMBOL(nfc_digital_unregister_device);
830
831 MODULE_LICENSE("GPL");
This page took 0.072336 seconds and 4 git commands to generate.