]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/nfc/hci/shdlc.c
ed8796b78f3993b604629f899af82b04d57cbb11
[mirror_ubuntu-artful-kernel.git] / net / nfc / hci / shdlc.c
1 /*
2 * Copyright (C) 2012 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #define pr_fmt(fmt) "shdlc: %s: " fmt, __func__
21
22 #include <linux/sched.h>
23 #include <linux/export.h>
24 #include <linux/wait.h>
25 #include <linux/slab.h>
26 #include <linux/skbuff.h>
27
28 #include <net/nfc/hci.h>
29 #include <net/nfc/shdlc.h>
30
31 #define SHDLC_LLC_HEAD_ROOM 2
32
33 #define SHDLC_MAX_WINDOW 4
34 #define SHDLC_SREJ_SUPPORT false
35
36 #define SHDLC_CONTROL_HEAD_MASK 0xe0
37 #define SHDLC_CONTROL_HEAD_I 0x80
38 #define SHDLC_CONTROL_HEAD_I2 0xa0
39 #define SHDLC_CONTROL_HEAD_S 0xc0
40 #define SHDLC_CONTROL_HEAD_U 0xe0
41
42 #define SHDLC_CONTROL_NS_MASK 0x38
43 #define SHDLC_CONTROL_NR_MASK 0x07
44 #define SHDLC_CONTROL_TYPE_MASK 0x18
45
46 #define SHDLC_CONTROL_M_MASK 0x1f
47
48 enum sframe_type {
49 S_FRAME_RR = 0x00,
50 S_FRAME_REJ = 0x01,
51 S_FRAME_RNR = 0x02,
52 S_FRAME_SREJ = 0x03
53 };
54
55 enum uframe_modifier {
56 U_FRAME_UA = 0x06,
57 U_FRAME_RSET = 0x19
58 };
59
60 #define SHDLC_CONNECT_VALUE_MS 5
61 #define SHDLC_T1_VALUE_MS(w) ((5 * w) / 4)
62 #define SHDLC_T2_VALUE_MS 300
63
64 #define SHDLC_DUMP_SKB(info, skb) \
65 do { \
66 pr_debug("%s:\n", info); \
67 print_hex_dump(KERN_DEBUG, "shdlc: ", DUMP_PREFIX_OFFSET, \
68 16, 1, skb->data, skb->len, 0); \
69 } while (0)
70
71 /* checks x < y <= z modulo 8 */
72 static bool nfc_shdlc_x_lt_y_lteq_z(int x, int y, int z)
73 {
74 if (x < z)
75 return ((x < y) && (y <= z)) ? true : false;
76 else
77 return ((y > x) || (y <= z)) ? true : false;
78 }
79
80 /* checks x <= y < z modulo 8 */
81 static bool nfc_shdlc_x_lteq_y_lt_z(int x, int y, int z)
82 {
83 if (x <= z)
84 return ((x <= y) && (y < z)) ? true : false;
85 else /* x > z -> z+8 > x */
86 return ((y >= x) || (y < z)) ? true : false;
87 }
88
89 static struct sk_buff *nfc_shdlc_alloc_skb(struct nfc_shdlc *shdlc,
90 int payload_len)
91 {
92 struct sk_buff *skb;
93
94 skb = alloc_skb(shdlc->client_headroom + SHDLC_LLC_HEAD_ROOM +
95 shdlc->client_tailroom + payload_len, GFP_KERNEL);
96 if (skb)
97 skb_reserve(skb, shdlc->client_headroom + SHDLC_LLC_HEAD_ROOM);
98
99 return skb;
100 }
101
102 /* immediately sends an S frame. */
103 static int nfc_shdlc_send_s_frame(struct nfc_shdlc *shdlc,
104 enum sframe_type sframe_type, int nr)
105 {
106 int r;
107 struct sk_buff *skb;
108
109 pr_debug("sframe_type=%d nr=%d\n", sframe_type, nr);
110
111 skb = nfc_shdlc_alloc_skb(shdlc, 0);
112 if (skb == NULL)
113 return -ENOMEM;
114
115 *skb_push(skb, 1) = SHDLC_CONTROL_HEAD_S | (sframe_type << 3) | nr;
116
117 r = shdlc->ops->xmit(shdlc, skb);
118
119 kfree_skb(skb);
120
121 return r;
122 }
123
124 /* immediately sends an U frame. skb may contain optional payload */
125 static int nfc_shdlc_send_u_frame(struct nfc_shdlc *shdlc,
126 struct sk_buff *skb,
127 enum uframe_modifier uframe_modifier)
128 {
129 int r;
130
131 pr_debug("uframe_modifier=%d\n", uframe_modifier);
132
133 *skb_push(skb, 1) = SHDLC_CONTROL_HEAD_U | uframe_modifier;
134
135 r = shdlc->ops->xmit(shdlc, skb);
136
137 kfree_skb(skb);
138
139 return r;
140 }
141
142 /*
143 * Free ack_pending frames until y_nr - 1, and reset t2 according to
144 * the remaining oldest ack_pending frame sent time
145 */
146 static void nfc_shdlc_reset_t2(struct nfc_shdlc *shdlc, int y_nr)
147 {
148 struct sk_buff *skb;
149 int dnr = shdlc->dnr; /* MUST initially be < y_nr */
150
151 pr_debug("release ack pending up to frame %d excluded\n", y_nr);
152
153 while (dnr != y_nr) {
154 pr_debug("release ack pending frame %d\n", dnr);
155
156 skb = skb_dequeue(&shdlc->ack_pending_q);
157 kfree_skb(skb);
158
159 dnr = (dnr + 1) % 8;
160 }
161
162 if (skb_queue_empty(&shdlc->ack_pending_q)) {
163 if (shdlc->t2_active) {
164 del_timer_sync(&shdlc->t2_timer);
165 shdlc->t2_active = false;
166
167 pr_debug
168 ("All sent frames acked. Stopped T2(retransmit)\n");
169 }
170 } else {
171 skb = skb_peek(&shdlc->ack_pending_q);
172
173 mod_timer(&shdlc->t2_timer, *(unsigned long *)skb->cb +
174 msecs_to_jiffies(SHDLC_T2_VALUE_MS));
175 shdlc->t2_active = true;
176
177 pr_debug
178 ("Start T2(retransmit) for remaining unacked sent frames\n");
179 }
180 }
181
182 /*
183 * Receive validated frames from lower layer. skb contains HCI payload only.
184 * Handle according to algorithm at spec:10.8.2
185 */
186 static void nfc_shdlc_rcv_i_frame(struct nfc_shdlc *shdlc,
187 struct sk_buff *skb, int ns, int nr)
188 {
189 int x_ns = ns;
190 int y_nr = nr;
191
192 pr_debug("recvd I-frame %d, remote waiting frame %d\n", ns, nr);
193
194 if (shdlc->state != SHDLC_CONNECTED)
195 goto exit;
196
197 if (x_ns != shdlc->nr) {
198 nfc_shdlc_send_s_frame(shdlc, S_FRAME_REJ, shdlc->nr);
199 goto exit;
200 }
201
202 if (shdlc->t1_active == false) {
203 shdlc->t1_active = true;
204 mod_timer(&shdlc->t1_timer,
205 msecs_to_jiffies(SHDLC_T1_VALUE_MS(shdlc->w)));
206 pr_debug("(re)Start T1(send ack)\n");
207 }
208
209 if (skb->len) {
210 nfc_hci_recv_frame(shdlc->hdev, skb);
211 skb = NULL;
212 }
213
214 shdlc->nr = (shdlc->nr + 1) % 8;
215
216 if (nfc_shdlc_x_lt_y_lteq_z(shdlc->dnr, y_nr, shdlc->ns)) {
217 nfc_shdlc_reset_t2(shdlc, y_nr);
218
219 shdlc->dnr = y_nr;
220 }
221
222 exit:
223 kfree_skb(skb);
224 }
225
226 static void nfc_shdlc_rcv_ack(struct nfc_shdlc *shdlc, int y_nr)
227 {
228 pr_debug("remote acked up to frame %d excluded\n", y_nr);
229
230 if (nfc_shdlc_x_lt_y_lteq_z(shdlc->dnr, y_nr, shdlc->ns)) {
231 nfc_shdlc_reset_t2(shdlc, y_nr);
232 shdlc->dnr = y_nr;
233 }
234 }
235
236 static void nfc_shdlc_requeue_ack_pending(struct nfc_shdlc *shdlc)
237 {
238 struct sk_buff *skb;
239
240 pr_debug("ns reset to %d\n", shdlc->dnr);
241
242 while ((skb = skb_dequeue_tail(&shdlc->ack_pending_q))) {
243 skb_pull(skb, 2); /* remove len+control */
244 skb_trim(skb, skb->len - 2); /* remove crc */
245 skb_queue_head(&shdlc->send_q, skb);
246 }
247 shdlc->ns = shdlc->dnr;
248 }
249
250 static void nfc_shdlc_rcv_rej(struct nfc_shdlc *shdlc, int y_nr)
251 {
252 struct sk_buff *skb;
253
254 pr_debug("remote asks retransmition from frame %d\n", y_nr);
255
256 if (nfc_shdlc_x_lteq_y_lt_z(shdlc->dnr, y_nr, shdlc->ns)) {
257 if (shdlc->t2_active) {
258 del_timer_sync(&shdlc->t2_timer);
259 shdlc->t2_active = false;
260 pr_debug("Stopped T2(retransmit)\n");
261 }
262
263 if (shdlc->dnr != y_nr) {
264 while ((shdlc->dnr = ((shdlc->dnr + 1) % 8)) != y_nr) {
265 skb = skb_dequeue(&shdlc->ack_pending_q);
266 kfree_skb(skb);
267 }
268 }
269
270 nfc_shdlc_requeue_ack_pending(shdlc);
271 }
272 }
273
274 /* See spec RR:10.8.3 REJ:10.8.4 */
275 static void nfc_shdlc_rcv_s_frame(struct nfc_shdlc *shdlc,
276 enum sframe_type s_frame_type, int nr)
277 {
278 struct sk_buff *skb;
279
280 if (shdlc->state != SHDLC_CONNECTED)
281 return;
282
283 switch (s_frame_type) {
284 case S_FRAME_RR:
285 nfc_shdlc_rcv_ack(shdlc, nr);
286 if (shdlc->rnr == true) { /* see SHDLC 10.7.7 */
287 shdlc->rnr = false;
288 if (shdlc->send_q.qlen == 0) {
289 skb = nfc_shdlc_alloc_skb(shdlc, 0);
290 if (skb)
291 skb_queue_tail(&shdlc->send_q, skb);
292 }
293 }
294 break;
295 case S_FRAME_REJ:
296 nfc_shdlc_rcv_rej(shdlc, nr);
297 break;
298 case S_FRAME_RNR:
299 nfc_shdlc_rcv_ack(shdlc, nr);
300 shdlc->rnr = true;
301 break;
302 default:
303 break;
304 }
305 }
306
307 static void nfc_shdlc_connect_complete(struct nfc_shdlc *shdlc, int r)
308 {
309 pr_debug("result=%d\n", r);
310
311 del_timer_sync(&shdlc->connect_timer);
312
313 if (r == 0) {
314 shdlc->ns = 0;
315 shdlc->nr = 0;
316 shdlc->dnr = 0;
317
318 shdlc->state = SHDLC_CONNECTED;
319 } else {
320 shdlc->state = SHDLC_DISCONNECTED;
321 }
322
323 shdlc->connect_result = r;
324
325 wake_up(shdlc->connect_wq);
326 }
327
328 static int nfc_shdlc_connect_initiate(struct nfc_shdlc *shdlc)
329 {
330 struct sk_buff *skb;
331
332 pr_debug("\n");
333
334 skb = nfc_shdlc_alloc_skb(shdlc, 2);
335 if (skb == NULL)
336 return -ENOMEM;
337
338 *skb_put(skb, 1) = SHDLC_MAX_WINDOW;
339 *skb_put(skb, 1) = SHDLC_SREJ_SUPPORT ? 1 : 0;
340
341 return nfc_shdlc_send_u_frame(shdlc, skb, U_FRAME_RSET);
342 }
343
344 static int nfc_shdlc_connect_send_ua(struct nfc_shdlc *shdlc)
345 {
346 struct sk_buff *skb;
347
348 pr_debug("\n");
349
350 skb = nfc_shdlc_alloc_skb(shdlc, 0);
351 if (skb == NULL)
352 return -ENOMEM;
353
354 return nfc_shdlc_send_u_frame(shdlc, skb, U_FRAME_UA);
355 }
356
357 static void nfc_shdlc_rcv_u_frame(struct nfc_shdlc *shdlc,
358 struct sk_buff *skb,
359 enum uframe_modifier u_frame_modifier)
360 {
361 u8 w = SHDLC_MAX_WINDOW;
362 bool srej_support = SHDLC_SREJ_SUPPORT;
363 int r;
364
365 pr_debug("u_frame_modifier=%d\n", u_frame_modifier);
366
367 switch (u_frame_modifier) {
368 case U_FRAME_RSET:
369 if (shdlc->state == SHDLC_NEGOCIATING) {
370 /* we sent RSET, but chip wants to negociate */
371 if (skb->len > 0)
372 w = skb->data[0];
373
374 if (skb->len > 1)
375 srej_support = skb->data[1] & 0x01 ? true :
376 false;
377
378 if ((w <= SHDLC_MAX_WINDOW) &&
379 (SHDLC_SREJ_SUPPORT || (srej_support == false))) {
380 shdlc->w = w;
381 shdlc->srej_support = srej_support;
382 r = nfc_shdlc_connect_send_ua(shdlc);
383 nfc_shdlc_connect_complete(shdlc, r);
384 }
385 } else if (shdlc->state == SHDLC_CONNECTED) {
386 /*
387 * Chip wants to reset link. This is unexpected and
388 * unsupported.
389 */
390 shdlc->hard_fault = -ECONNRESET;
391 }
392 break;
393 case U_FRAME_UA:
394 if ((shdlc->state == SHDLC_CONNECTING &&
395 shdlc->connect_tries > 0) ||
396 (shdlc->state == SHDLC_NEGOCIATING))
397 nfc_shdlc_connect_complete(shdlc, 0);
398 break;
399 default:
400 break;
401 }
402
403 kfree_skb(skb);
404 }
405
406 static void nfc_shdlc_handle_rcv_queue(struct nfc_shdlc *shdlc)
407 {
408 struct sk_buff *skb;
409 u8 control;
410 int nr;
411 int ns;
412 enum sframe_type s_frame_type;
413 enum uframe_modifier u_frame_modifier;
414
415 if (shdlc->rcv_q.qlen)
416 pr_debug("rcvQlen=%d\n", shdlc->rcv_q.qlen);
417
418 while ((skb = skb_dequeue(&shdlc->rcv_q)) != NULL) {
419 control = skb->data[0];
420 skb_pull(skb, 1);
421 switch (control & SHDLC_CONTROL_HEAD_MASK) {
422 case SHDLC_CONTROL_HEAD_I:
423 case SHDLC_CONTROL_HEAD_I2:
424 ns = (control & SHDLC_CONTROL_NS_MASK) >> 3;
425 nr = control & SHDLC_CONTROL_NR_MASK;
426 nfc_shdlc_rcv_i_frame(shdlc, skb, ns, nr);
427 break;
428 case SHDLC_CONTROL_HEAD_S:
429 s_frame_type = (control & SHDLC_CONTROL_TYPE_MASK) >> 3;
430 nr = control & SHDLC_CONTROL_NR_MASK;
431 nfc_shdlc_rcv_s_frame(shdlc, s_frame_type, nr);
432 kfree_skb(skb);
433 break;
434 case SHDLC_CONTROL_HEAD_U:
435 u_frame_modifier = control & SHDLC_CONTROL_M_MASK;
436 nfc_shdlc_rcv_u_frame(shdlc, skb, u_frame_modifier);
437 break;
438 default:
439 pr_err("UNKNOWN Control=%d\n", control);
440 kfree_skb(skb);
441 break;
442 }
443 }
444 }
445
446 static int nfc_shdlc_w_used(int ns, int dnr)
447 {
448 int unack_count;
449
450 if (dnr <= ns)
451 unack_count = ns - dnr;
452 else
453 unack_count = 8 - dnr + ns;
454
455 return unack_count;
456 }
457
458 /* Send frames according to algorithm at spec:10.8.1 */
459 static void nfc_shdlc_handle_send_queue(struct nfc_shdlc *shdlc)
460 {
461 struct sk_buff *skb;
462 int r;
463 unsigned long time_sent;
464
465 if (shdlc->send_q.qlen)
466 pr_debug
467 ("sendQlen=%d ns=%d dnr=%d rnr=%s w_room=%d unackQlen=%d\n",
468 shdlc->send_q.qlen, shdlc->ns, shdlc->dnr,
469 shdlc->rnr == false ? "false" : "true",
470 shdlc->w - nfc_shdlc_w_used(shdlc->ns, shdlc->dnr),
471 shdlc->ack_pending_q.qlen);
472
473 while (shdlc->send_q.qlen && shdlc->ack_pending_q.qlen < shdlc->w &&
474 (shdlc->rnr == false)) {
475
476 if (shdlc->t1_active) {
477 del_timer_sync(&shdlc->t1_timer);
478 shdlc->t1_active = false;
479 pr_debug("Stopped T1(send ack)\n");
480 }
481
482 skb = skb_dequeue(&shdlc->send_q);
483
484 *skb_push(skb, 1) = SHDLC_CONTROL_HEAD_I | (shdlc->ns << 3) |
485 shdlc->nr;
486
487 pr_debug("Sending I-Frame %d, waiting to rcv %d\n", shdlc->ns,
488 shdlc->nr);
489 /* SHDLC_DUMP_SKB("shdlc frame written", skb); */
490
491 r = shdlc->ops->xmit(shdlc, skb);
492 if (r < 0) {
493 shdlc->hard_fault = r;
494 break;
495 }
496
497 shdlc->ns = (shdlc->ns + 1) % 8;
498
499 time_sent = jiffies;
500 *(unsigned long *)skb->cb = time_sent;
501
502 skb_queue_tail(&shdlc->ack_pending_q, skb);
503
504 if (shdlc->t2_active == false) {
505 shdlc->t2_active = true;
506 mod_timer(&shdlc->t2_timer, time_sent +
507 msecs_to_jiffies(SHDLC_T2_VALUE_MS));
508 pr_debug("Started T2 (retransmit)\n");
509 }
510 }
511 }
512
513 static void nfc_shdlc_connect_timeout(unsigned long data)
514 {
515 struct nfc_shdlc *shdlc = (struct nfc_shdlc *)data;
516
517 pr_debug("\n");
518
519 queue_work(system_nrt_wq, &shdlc->sm_work);
520 }
521
522 static void nfc_shdlc_t1_timeout(unsigned long data)
523 {
524 struct nfc_shdlc *shdlc = (struct nfc_shdlc *)data;
525
526 pr_debug("SoftIRQ: need to send ack\n");
527
528 queue_work(system_nrt_wq, &shdlc->sm_work);
529 }
530
531 static void nfc_shdlc_t2_timeout(unsigned long data)
532 {
533 struct nfc_shdlc *shdlc = (struct nfc_shdlc *)data;
534
535 pr_debug("SoftIRQ: need to retransmit\n");
536
537 queue_work(system_nrt_wq, &shdlc->sm_work);
538 }
539
540 static void nfc_shdlc_sm_work(struct work_struct *work)
541 {
542 struct nfc_shdlc *shdlc = container_of(work, struct nfc_shdlc, sm_work);
543 int r;
544
545 pr_debug("\n");
546
547 mutex_lock(&shdlc->state_mutex);
548
549 switch (shdlc->state) {
550 case SHDLC_DISCONNECTED:
551 skb_queue_purge(&shdlc->rcv_q);
552 skb_queue_purge(&shdlc->send_q);
553 skb_queue_purge(&shdlc->ack_pending_q);
554 break;
555 case SHDLC_CONNECTING:
556 if (shdlc->hard_fault) {
557 nfc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
558 break;
559 }
560
561 if (shdlc->connect_tries++ < 5)
562 r = nfc_shdlc_connect_initiate(shdlc);
563 else
564 r = -ETIME;
565 if (r < 0)
566 nfc_shdlc_connect_complete(shdlc, r);
567 else {
568 mod_timer(&shdlc->connect_timer, jiffies +
569 msecs_to_jiffies(SHDLC_CONNECT_VALUE_MS));
570
571 shdlc->state = SHDLC_NEGOCIATING;
572 }
573 break;
574 case SHDLC_NEGOCIATING:
575 if (timer_pending(&shdlc->connect_timer) == 0) {
576 shdlc->state = SHDLC_CONNECTING;
577 queue_work(system_nrt_wq, &shdlc->sm_work);
578 }
579
580 nfc_shdlc_handle_rcv_queue(shdlc);
581
582 if (shdlc->hard_fault) {
583 nfc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
584 break;
585 }
586 break;
587 case SHDLC_CONNECTED:
588 nfc_shdlc_handle_rcv_queue(shdlc);
589 nfc_shdlc_handle_send_queue(shdlc);
590
591 if (shdlc->t1_active && timer_pending(&shdlc->t1_timer) == 0) {
592 pr_debug
593 ("Handle T1(send ack) elapsed (T1 now inactive)\n");
594
595 shdlc->t1_active = false;
596 r = nfc_shdlc_send_s_frame(shdlc, S_FRAME_RR,
597 shdlc->nr);
598 if (r < 0)
599 shdlc->hard_fault = r;
600 }
601
602 if (shdlc->t2_active && timer_pending(&shdlc->t2_timer) == 0) {
603 pr_debug
604 ("Handle T2(retransmit) elapsed (T2 inactive)\n");
605
606 shdlc->t2_active = false;
607
608 nfc_shdlc_requeue_ack_pending(shdlc);
609 nfc_shdlc_handle_send_queue(shdlc);
610 }
611
612 if (shdlc->hard_fault) {
613 nfc_hci_driver_failure(shdlc->hdev, shdlc->hard_fault);
614 }
615 break;
616 default:
617 break;
618 }
619 mutex_unlock(&shdlc->state_mutex);
620 }
621
622 /*
623 * Called from syscall context to establish shdlc link. Sleeps until
624 * link is ready or failure.
625 */
626 static int nfc_shdlc_connect(struct nfc_shdlc *shdlc)
627 {
628 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(connect_wq);
629
630 pr_debug("\n");
631
632 mutex_lock(&shdlc->state_mutex);
633
634 shdlc->state = SHDLC_CONNECTING;
635 shdlc->connect_wq = &connect_wq;
636 shdlc->connect_tries = 0;
637 shdlc->connect_result = 1;
638
639 mutex_unlock(&shdlc->state_mutex);
640
641 queue_work(system_nrt_wq, &shdlc->sm_work);
642
643 wait_event(connect_wq, shdlc->connect_result != 1);
644
645 return shdlc->connect_result;
646 }
647
648 static void nfc_shdlc_disconnect(struct nfc_shdlc *shdlc)
649 {
650 pr_debug("\n");
651
652 mutex_lock(&shdlc->state_mutex);
653
654 shdlc->state = SHDLC_DISCONNECTED;
655
656 mutex_unlock(&shdlc->state_mutex);
657
658 queue_work(system_nrt_wq, &shdlc->sm_work);
659 }
660
661 /*
662 * Receive an incoming shdlc frame. Frame has already been crc-validated.
663 * skb contains only LLC header and payload.
664 * If skb == NULL, it is a notification that the link below is dead.
665 */
666 void nfc_shdlc_recv_frame(struct nfc_shdlc *shdlc, struct sk_buff *skb)
667 {
668 if (skb == NULL) {
669 pr_err("NULL Frame -> link is dead\n");
670 shdlc->hard_fault = -EREMOTEIO;
671 } else {
672 SHDLC_DUMP_SKB("incoming frame", skb);
673 skb_queue_tail(&shdlc->rcv_q, skb);
674 }
675
676 queue_work(system_nrt_wq, &shdlc->sm_work);
677 }
678 EXPORT_SYMBOL(nfc_shdlc_recv_frame);
679
680 static int nfc_shdlc_open(struct nfc_hci_dev *hdev)
681 {
682 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
683 int r;
684
685 pr_debug("\n");
686
687 if (shdlc->ops->open) {
688 r = shdlc->ops->open(shdlc);
689 if (r < 0)
690 return r;
691 }
692
693 r = nfc_shdlc_connect(shdlc);
694 if (r < 0 && shdlc->ops->close)
695 shdlc->ops->close(shdlc);
696
697 return r;
698 }
699
700 static void nfc_shdlc_close(struct nfc_hci_dev *hdev)
701 {
702 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
703
704 pr_debug("\n");
705
706 nfc_shdlc_disconnect(shdlc);
707
708 if (shdlc->ops->close)
709 shdlc->ops->close(shdlc);
710 }
711
712 static int nfc_shdlc_hci_ready(struct nfc_hci_dev *hdev)
713 {
714 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
715 int r = 0;
716
717 pr_debug("\n");
718
719 if (shdlc->ops->hci_ready)
720 r = shdlc->ops->hci_ready(shdlc);
721
722 return r;
723 }
724
725 static int nfc_shdlc_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
726 {
727 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
728
729 SHDLC_DUMP_SKB("queuing HCP packet to shdlc", skb);
730
731 skb_queue_tail(&shdlc->send_q, skb);
732
733 queue_work(system_nrt_wq, &shdlc->sm_work);
734
735 return 0;
736 }
737
738 static int nfc_shdlc_start_poll(struct nfc_hci_dev *hdev,
739 u32 im_protocols, u32 tm_protocols)
740 {
741 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
742
743 pr_debug("\n");
744
745 if (shdlc->ops->start_poll)
746 return shdlc->ops->start_poll(shdlc,
747 im_protocols, tm_protocols);
748
749 return 0;
750 }
751
752 static int nfc_shdlc_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
753 struct nfc_target *target)
754 {
755 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
756
757 if (shdlc->ops->target_from_gate)
758 return shdlc->ops->target_from_gate(shdlc, gate, target);
759
760 return -EPERM;
761 }
762
763 static int nfc_shdlc_complete_target_discovered(struct nfc_hci_dev *hdev,
764 u8 gate,
765 struct nfc_target *target)
766 {
767 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
768
769 pr_debug("\n");
770
771 if (shdlc->ops->complete_target_discovered)
772 return shdlc->ops->complete_target_discovered(shdlc, gate,
773 target);
774
775 return 0;
776 }
777
778 static int nfc_shdlc_data_exchange(struct nfc_hci_dev *hdev,
779 struct nfc_target *target,
780 struct sk_buff *skb,
781 struct sk_buff **res_skb)
782 {
783 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
784
785 if (shdlc->ops->data_exchange)
786 return shdlc->ops->data_exchange(shdlc, target, skb, res_skb);
787
788 return -EPERM;
789 }
790
791 static int nfc_shdlc_check_presence(struct nfc_hci_dev *hdev,
792 struct nfc_target *target)
793 {
794 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
795
796 if (shdlc->ops->check_presence)
797 return shdlc->ops->check_presence(shdlc, target);
798
799 return 0;
800 }
801
802 static struct nfc_hci_ops shdlc_ops = {
803 .open = nfc_shdlc_open,
804 .close = nfc_shdlc_close,
805 .hci_ready = nfc_shdlc_hci_ready,
806 .xmit = nfc_shdlc_xmit,
807 .start_poll = nfc_shdlc_start_poll,
808 .target_from_gate = nfc_shdlc_target_from_gate,
809 .complete_target_discovered = nfc_shdlc_complete_target_discovered,
810 .data_exchange = nfc_shdlc_data_exchange,
811 .check_presence = nfc_shdlc_check_presence,
812 };
813
814 struct nfc_shdlc *nfc_shdlc_allocate(struct nfc_shdlc_ops *ops,
815 struct nfc_hci_init_data *init_data,
816 u32 protocols,
817 int tx_headroom, int tx_tailroom,
818 int max_link_payload, const char *devname)
819 {
820 struct nfc_shdlc *shdlc;
821 int r;
822
823 if (ops->xmit == NULL)
824 return NULL;
825
826 shdlc = kzalloc(sizeof(struct nfc_shdlc), GFP_KERNEL);
827 if (shdlc == NULL)
828 return NULL;
829
830 mutex_init(&shdlc->state_mutex);
831 shdlc->ops = ops;
832 shdlc->state = SHDLC_DISCONNECTED;
833
834 init_timer(&shdlc->connect_timer);
835 shdlc->connect_timer.data = (unsigned long)shdlc;
836 shdlc->connect_timer.function = nfc_shdlc_connect_timeout;
837
838 init_timer(&shdlc->t1_timer);
839 shdlc->t1_timer.data = (unsigned long)shdlc;
840 shdlc->t1_timer.function = nfc_shdlc_t1_timeout;
841
842 init_timer(&shdlc->t2_timer);
843 shdlc->t2_timer.data = (unsigned long)shdlc;
844 shdlc->t2_timer.function = nfc_shdlc_t2_timeout;
845
846 shdlc->w = SHDLC_MAX_WINDOW;
847 shdlc->srej_support = SHDLC_SREJ_SUPPORT;
848
849 skb_queue_head_init(&shdlc->rcv_q);
850 skb_queue_head_init(&shdlc->send_q);
851 skb_queue_head_init(&shdlc->ack_pending_q);
852
853 INIT_WORK(&shdlc->sm_work, nfc_shdlc_sm_work);
854
855 shdlc->client_headroom = tx_headroom;
856 shdlc->client_tailroom = tx_tailroom;
857
858 shdlc->hdev = nfc_hci_allocate_device(&shdlc_ops, init_data, protocols,
859 tx_headroom + SHDLC_LLC_HEAD_ROOM,
860 tx_tailroom,
861 max_link_payload);
862 if (shdlc->hdev == NULL)
863 goto err_allocdev;
864
865 nfc_hci_set_clientdata(shdlc->hdev, shdlc);
866
867 r = nfc_hci_register_device(shdlc->hdev);
868 if (r < 0)
869 goto err_regdev;
870
871 return shdlc;
872
873 err_regdev:
874 nfc_hci_free_device(shdlc->hdev);
875
876 err_allocdev:
877 kfree(shdlc);
878
879 return NULL;
880 }
881 EXPORT_SYMBOL(nfc_shdlc_allocate);
882
883 void nfc_shdlc_free(struct nfc_shdlc *shdlc)
884 {
885 pr_debug("\n");
886
887 nfc_hci_unregister_device(shdlc->hdev);
888 nfc_hci_free_device(shdlc->hdev);
889
890 cancel_work_sync(&shdlc->sm_work);
891
892 skb_queue_purge(&shdlc->rcv_q);
893 skb_queue_purge(&shdlc->send_q);
894 skb_queue_purge(&shdlc->ack_pending_q);
895
896 kfree(shdlc);
897 }
898 EXPORT_SYMBOL(nfc_shdlc_free);
899
900 void nfc_shdlc_set_clientdata(struct nfc_shdlc *shdlc, void *clientdata)
901 {
902 pr_debug("\n");
903
904 shdlc->clientdata = clientdata;
905 }
906 EXPORT_SYMBOL(nfc_shdlc_set_clientdata);
907
908 void *nfc_shdlc_get_clientdata(struct nfc_shdlc *shdlc)
909 {
910 return shdlc->clientdata;
911 }
912 EXPORT_SYMBOL(nfc_shdlc_get_clientdata);
913
914 struct nfc_hci_dev *nfc_shdlc_get_hci_dev(struct nfc_shdlc *shdlc)
915 {
916 return shdlc->hdev;
917 }
918 EXPORT_SYMBOL(nfc_shdlc_get_hci_dev);