Linux 5.7.7
[linux/fpc-iii.git] / net / nfc / hci / llc_shdlc.c
blob0eb4ddc056e789219bf09665a591246eac1b88bf
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * shdlc Link Layer Control
5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
6 */
8 #define pr_fmt(fmt) "shdlc: %s: " fmt, __func__
10 #include <linux/types.h>
11 #include <linux/sched.h>
12 #include <linux/wait.h>
13 #include <linux/slab.h>
14 #include <linux/skbuff.h>
16 #include "llc.h"
18 enum shdlc_state {
19 SHDLC_DISCONNECTED = 0,
20 SHDLC_CONNECTING = 1,
21 SHDLC_NEGOTIATING = 2,
22 SHDLC_HALF_CONNECTED = 3,
23 SHDLC_CONNECTED = 4
26 struct llc_shdlc {
27 struct nfc_hci_dev *hdev;
28 xmit_to_drv_t xmit_to_drv;
29 rcv_to_hci_t rcv_to_hci;
31 struct mutex state_mutex;
32 enum shdlc_state state;
33 int hard_fault;
35 wait_queue_head_t *connect_wq;
36 int connect_tries;
37 int connect_result;
38 struct timer_list connect_timer;/* aka T3 in spec 10.6.1 */
40 u8 w; /* window size */
41 bool srej_support;
43 struct timer_list t1_timer; /* send ack timeout */
44 bool t1_active;
46 struct timer_list t2_timer; /* guard/retransmit timeout */
47 bool t2_active;
49 int ns; /* next seq num for send */
50 int nr; /* next expected seq num for receive */
51 int dnr; /* oldest sent unacked seq num */
53 struct sk_buff_head rcv_q;
55 struct sk_buff_head send_q;
56 bool rnr; /* other side is not ready to receive */
58 struct sk_buff_head ack_pending_q;
60 struct work_struct sm_work;
62 int tx_headroom;
63 int tx_tailroom;
65 llc_failure_t llc_failure;
68 #define SHDLC_LLC_HEAD_ROOM 2
70 #define SHDLC_MAX_WINDOW 4
71 #define SHDLC_SREJ_SUPPORT false
73 #define SHDLC_CONTROL_HEAD_MASK 0xe0
74 #define SHDLC_CONTROL_HEAD_I 0x80
75 #define SHDLC_CONTROL_HEAD_I2 0xa0
76 #define SHDLC_CONTROL_HEAD_S 0xc0
77 #define SHDLC_CONTROL_HEAD_U 0xe0
79 #define SHDLC_CONTROL_NS_MASK 0x38
80 #define SHDLC_CONTROL_NR_MASK 0x07
81 #define SHDLC_CONTROL_TYPE_MASK 0x18
83 #define SHDLC_CONTROL_M_MASK 0x1f
85 enum sframe_type {
86 S_FRAME_RR = 0x00,
87 S_FRAME_REJ = 0x01,
88 S_FRAME_RNR = 0x02,
89 S_FRAME_SREJ = 0x03
92 enum uframe_modifier {
93 U_FRAME_UA = 0x06,
94 U_FRAME_RSET = 0x19
97 #define SHDLC_CONNECT_VALUE_MS 5
98 #define SHDLC_T1_VALUE_MS(w) ((5 * w) / 4)
99 #define SHDLC_T2_VALUE_MS 300
101 #define SHDLC_DUMP_SKB(info, skb) \
102 do { \
103 pr_debug("%s:\n", info); \
104 print_hex_dump(KERN_DEBUG, "shdlc: ", DUMP_PREFIX_OFFSET, \
105 16, 1, skb->data, skb->len, 0); \
106 } while (0)
108 /* checks x < y <= z modulo 8 */
109 static bool llc_shdlc_x_lt_y_lteq_z(int x, int y, int z)
111 if (x < z)
112 return ((x < y) && (y <= z)) ? true : false;
113 else
114 return ((y > x) || (y <= z)) ? true : false;
117 /* checks x <= y < z modulo 8 */
118 static bool llc_shdlc_x_lteq_y_lt_z(int x, int y, int z)
120 if (x <= z)
121 return ((x <= y) && (y < z)) ? true : false;
122 else /* x > z -> z+8 > x */
123 return ((y >= x) || (y < z)) ? true : false;
126 static struct sk_buff *llc_shdlc_alloc_skb(struct llc_shdlc *shdlc,
127 int payload_len)
129 struct sk_buff *skb;
131 skb = alloc_skb(shdlc->tx_headroom + SHDLC_LLC_HEAD_ROOM +
132 shdlc->tx_tailroom + payload_len, GFP_KERNEL);
133 if (skb)
134 skb_reserve(skb, shdlc->tx_headroom + SHDLC_LLC_HEAD_ROOM);
136 return skb;
139 /* immediately sends an S frame. */
140 static int llc_shdlc_send_s_frame(struct llc_shdlc *shdlc,
141 enum sframe_type sframe_type, int nr)
143 int r;
144 struct sk_buff *skb;
146 pr_debug("sframe_type=%d nr=%d\n", sframe_type, nr);
148 skb = llc_shdlc_alloc_skb(shdlc, 0);
149 if (skb == NULL)
150 return -ENOMEM;
152 *(u8 *)skb_push(skb, 1) = SHDLC_CONTROL_HEAD_S | (sframe_type << 3) | nr;
154 r = shdlc->xmit_to_drv(shdlc->hdev, skb);
156 kfree_skb(skb);
158 return r;
161 /* immediately sends an U frame. skb may contain optional payload */
162 static int llc_shdlc_send_u_frame(struct llc_shdlc *shdlc,
163 struct sk_buff *skb,
164 enum uframe_modifier uframe_modifier)
166 int r;
168 pr_debug("uframe_modifier=%d\n", uframe_modifier);
170 *(u8 *)skb_push(skb, 1) = SHDLC_CONTROL_HEAD_U | uframe_modifier;
172 r = shdlc->xmit_to_drv(shdlc->hdev, skb);
174 kfree_skb(skb);
176 return r;
180 * Free ack_pending frames until y_nr - 1, and reset t2 according to
181 * the remaining oldest ack_pending frame sent time
183 static void llc_shdlc_reset_t2(struct llc_shdlc *shdlc, int y_nr)
185 struct sk_buff *skb;
186 int dnr = shdlc->dnr; /* MUST initially be < y_nr */
188 pr_debug("release ack pending up to frame %d excluded\n", y_nr);
190 while (dnr != y_nr) {
191 pr_debug("release ack pending frame %d\n", dnr);
193 skb = skb_dequeue(&shdlc->ack_pending_q);
194 kfree_skb(skb);
196 dnr = (dnr + 1) % 8;
199 if (skb_queue_empty(&shdlc->ack_pending_q)) {
200 if (shdlc->t2_active) {
201 del_timer_sync(&shdlc->t2_timer);
202 shdlc->t2_active = false;
204 pr_debug
205 ("All sent frames acked. Stopped T2(retransmit)\n");
207 } else {
208 skb = skb_peek(&shdlc->ack_pending_q);
210 mod_timer(&shdlc->t2_timer, *(unsigned long *)skb->cb +
211 msecs_to_jiffies(SHDLC_T2_VALUE_MS));
212 shdlc->t2_active = true;
214 pr_debug
215 ("Start T2(retransmit) for remaining unacked sent frames\n");
220 * Receive validated frames from lower layer. skb contains HCI payload only.
221 * Handle according to algorithm at spec:10.8.2
223 static void llc_shdlc_rcv_i_frame(struct llc_shdlc *shdlc,
224 struct sk_buff *skb, int ns, int nr)
226 int x_ns = ns;
227 int y_nr = nr;
229 pr_debug("recvd I-frame %d, remote waiting frame %d\n", ns, nr);
231 if (shdlc->state != SHDLC_CONNECTED)
232 goto exit;
234 if (x_ns != shdlc->nr) {
235 llc_shdlc_send_s_frame(shdlc, S_FRAME_REJ, shdlc->nr);
236 goto exit;
239 if (shdlc->t1_active == false) {
240 shdlc->t1_active = true;
241 mod_timer(&shdlc->t1_timer, jiffies +
242 msecs_to_jiffies(SHDLC_T1_VALUE_MS(shdlc->w)));
243 pr_debug("(re)Start T1(send ack)\n");
246 if (skb->len) {
247 shdlc->rcv_to_hci(shdlc->hdev, skb);
248 skb = NULL;
251 shdlc->nr = (shdlc->nr + 1) % 8;
253 if (llc_shdlc_x_lt_y_lteq_z(shdlc->dnr, y_nr, shdlc->ns)) {
254 llc_shdlc_reset_t2(shdlc, y_nr);
256 shdlc->dnr = y_nr;
259 exit:
260 kfree_skb(skb);
263 static void llc_shdlc_rcv_ack(struct llc_shdlc *shdlc, int y_nr)
265 pr_debug("remote acked up to frame %d excluded\n", y_nr);
267 if (llc_shdlc_x_lt_y_lteq_z(shdlc->dnr, y_nr, shdlc->ns)) {
268 llc_shdlc_reset_t2(shdlc, y_nr);
269 shdlc->dnr = y_nr;
273 static void llc_shdlc_requeue_ack_pending(struct llc_shdlc *shdlc)
275 struct sk_buff *skb;
277 pr_debug("ns reset to %d\n", shdlc->dnr);
279 while ((skb = skb_dequeue_tail(&shdlc->ack_pending_q))) {
280 skb_pull(skb, 1); /* remove control field */
281 skb_queue_head(&shdlc->send_q, skb);
283 shdlc->ns = shdlc->dnr;
286 static void llc_shdlc_rcv_rej(struct llc_shdlc *shdlc, int y_nr)
288 struct sk_buff *skb;
290 pr_debug("remote asks retransmission from frame %d\n", y_nr);
292 if (llc_shdlc_x_lteq_y_lt_z(shdlc->dnr, y_nr, shdlc->ns)) {
293 if (shdlc->t2_active) {
294 del_timer_sync(&shdlc->t2_timer);
295 shdlc->t2_active = false;
296 pr_debug("Stopped T2(retransmit)\n");
299 if (shdlc->dnr != y_nr) {
300 while ((shdlc->dnr = ((shdlc->dnr + 1) % 8)) != y_nr) {
301 skb = skb_dequeue(&shdlc->ack_pending_q);
302 kfree_skb(skb);
306 llc_shdlc_requeue_ack_pending(shdlc);
310 /* See spec RR:10.8.3 REJ:10.8.4 */
311 static void llc_shdlc_rcv_s_frame(struct llc_shdlc *shdlc,
312 enum sframe_type s_frame_type, int nr)
314 struct sk_buff *skb;
316 if (shdlc->state != SHDLC_CONNECTED)
317 return;
319 switch (s_frame_type) {
320 case S_FRAME_RR:
321 llc_shdlc_rcv_ack(shdlc, nr);
322 if (shdlc->rnr == true) { /* see SHDLC 10.7.7 */
323 shdlc->rnr = false;
324 if (shdlc->send_q.qlen == 0) {
325 skb = llc_shdlc_alloc_skb(shdlc, 0);
326 if (skb)
327 skb_queue_tail(&shdlc->send_q, skb);
330 break;
331 case S_FRAME_REJ:
332 llc_shdlc_rcv_rej(shdlc, nr);
333 break;
334 case S_FRAME_RNR:
335 llc_shdlc_rcv_ack(shdlc, nr);
336 shdlc->rnr = true;
337 break;
338 default:
339 break;
343 static void llc_shdlc_connect_complete(struct llc_shdlc *shdlc, int r)
345 pr_debug("result=%d\n", r);
347 del_timer_sync(&shdlc->connect_timer);
349 if (r == 0) {
350 shdlc->ns = 0;
351 shdlc->nr = 0;
352 shdlc->dnr = 0;
354 shdlc->state = SHDLC_HALF_CONNECTED;
355 } else {
356 shdlc->state = SHDLC_DISCONNECTED;
359 shdlc->connect_result = r;
361 wake_up(shdlc->connect_wq);
364 static int llc_shdlc_connect_initiate(struct llc_shdlc *shdlc)
366 struct sk_buff *skb;
368 pr_debug("\n");
370 skb = llc_shdlc_alloc_skb(shdlc, 2);
371 if (skb == NULL)
372 return -ENOMEM;
374 skb_put_u8(skb, SHDLC_MAX_WINDOW);
375 skb_put_u8(skb, SHDLC_SREJ_SUPPORT ? 1 : 0);
377 return llc_shdlc_send_u_frame(shdlc, skb, U_FRAME_RSET);
380 static int llc_shdlc_connect_send_ua(struct llc_shdlc *shdlc)
382 struct sk_buff *skb;
384 pr_debug("\n");
386 skb = llc_shdlc_alloc_skb(shdlc, 0);
387 if (skb == NULL)
388 return -ENOMEM;
390 return llc_shdlc_send_u_frame(shdlc, skb, U_FRAME_UA);
393 static void llc_shdlc_rcv_u_frame(struct llc_shdlc *shdlc,
394 struct sk_buff *skb,
395 enum uframe_modifier u_frame_modifier)
397 u8 w = SHDLC_MAX_WINDOW;
398 bool srej_support = SHDLC_SREJ_SUPPORT;
399 int r;
401 pr_debug("u_frame_modifier=%d\n", u_frame_modifier);
403 switch (u_frame_modifier) {
404 case U_FRAME_RSET:
405 switch (shdlc->state) {
406 case SHDLC_NEGOTIATING:
407 case SHDLC_CONNECTING:
409 * We sent RSET, but chip wants to negociate or we
410 * got RSET before we managed to send out our.
412 if (skb->len > 0)
413 w = skb->data[0];
415 if (skb->len > 1)
416 srej_support = skb->data[1] & 0x01 ? true :
417 false;
419 if ((w <= SHDLC_MAX_WINDOW) &&
420 (SHDLC_SREJ_SUPPORT || (srej_support == false))) {
421 shdlc->w = w;
422 shdlc->srej_support = srej_support;
423 r = llc_shdlc_connect_send_ua(shdlc);
424 llc_shdlc_connect_complete(shdlc, r);
426 break;
427 case SHDLC_HALF_CONNECTED:
429 * Chip resent RSET due to its timeout - Ignote it
430 * as we already sent UA.
432 break;
433 case SHDLC_CONNECTED:
435 * Chip wants to reset link. This is unexpected and
436 * unsupported.
438 shdlc->hard_fault = -ECONNRESET;
439 break;
440 default:
441 break;
443 break;
444 case U_FRAME_UA:
445 if ((shdlc->state == SHDLC_CONNECTING &&
446 shdlc->connect_tries > 0) ||
447 (shdlc->state == SHDLC_NEGOTIATING)) {
448 llc_shdlc_connect_complete(shdlc, 0);
449 shdlc->state = SHDLC_CONNECTED;
451 break;
452 default:
453 break;
456 kfree_skb(skb);
459 static void llc_shdlc_handle_rcv_queue(struct llc_shdlc *shdlc)
461 struct sk_buff *skb;
462 u8 control;
463 int nr;
464 int ns;
465 enum sframe_type s_frame_type;
466 enum uframe_modifier u_frame_modifier;
468 if (shdlc->rcv_q.qlen)
469 pr_debug("rcvQlen=%d\n", shdlc->rcv_q.qlen);
471 while ((skb = skb_dequeue(&shdlc->rcv_q)) != NULL) {
472 control = skb->data[0];
473 skb_pull(skb, 1);
474 switch (control & SHDLC_CONTROL_HEAD_MASK) {
475 case SHDLC_CONTROL_HEAD_I:
476 case SHDLC_CONTROL_HEAD_I2:
477 if (shdlc->state == SHDLC_HALF_CONNECTED)
478 shdlc->state = SHDLC_CONNECTED;
480 ns = (control & SHDLC_CONTROL_NS_MASK) >> 3;
481 nr = control & SHDLC_CONTROL_NR_MASK;
482 llc_shdlc_rcv_i_frame(shdlc, skb, ns, nr);
483 break;
484 case SHDLC_CONTROL_HEAD_S:
485 if (shdlc->state == SHDLC_HALF_CONNECTED)
486 shdlc->state = SHDLC_CONNECTED;
488 s_frame_type = (control & SHDLC_CONTROL_TYPE_MASK) >> 3;
489 nr = control & SHDLC_CONTROL_NR_MASK;
490 llc_shdlc_rcv_s_frame(shdlc, s_frame_type, nr);
491 kfree_skb(skb);
492 break;
493 case SHDLC_CONTROL_HEAD_U:
494 u_frame_modifier = control & SHDLC_CONTROL_M_MASK;
495 llc_shdlc_rcv_u_frame(shdlc, skb, u_frame_modifier);
496 break;
497 default:
498 pr_err("UNKNOWN Control=%d\n", control);
499 kfree_skb(skb);
500 break;
505 static int llc_shdlc_w_used(int ns, int dnr)
507 int unack_count;
509 if (dnr <= ns)
510 unack_count = ns - dnr;
511 else
512 unack_count = 8 - dnr + ns;
514 return unack_count;
517 /* Send frames according to algorithm at spec:10.8.1 */
518 static void llc_shdlc_handle_send_queue(struct llc_shdlc *shdlc)
520 struct sk_buff *skb;
521 int r;
522 unsigned long time_sent;
524 if (shdlc->send_q.qlen)
525 pr_debug
526 ("sendQlen=%d ns=%d dnr=%d rnr=%s w_room=%d unackQlen=%d\n",
527 shdlc->send_q.qlen, shdlc->ns, shdlc->dnr,
528 shdlc->rnr == false ? "false" : "true",
529 shdlc->w - llc_shdlc_w_used(shdlc->ns, shdlc->dnr),
530 shdlc->ack_pending_q.qlen);
532 while (shdlc->send_q.qlen && shdlc->ack_pending_q.qlen < shdlc->w &&
533 (shdlc->rnr == false)) {
535 if (shdlc->t1_active) {
536 del_timer_sync(&shdlc->t1_timer);
537 shdlc->t1_active = false;
538 pr_debug("Stopped T1(send ack)\n");
541 skb = skb_dequeue(&shdlc->send_q);
543 *(u8 *)skb_push(skb, 1) = SHDLC_CONTROL_HEAD_I | (shdlc->ns << 3) |
544 shdlc->nr;
546 pr_debug("Sending I-Frame %d, waiting to rcv %d\n", shdlc->ns,
547 shdlc->nr);
548 SHDLC_DUMP_SKB("shdlc frame written", skb);
550 r = shdlc->xmit_to_drv(shdlc->hdev, skb);
551 if (r < 0) {
552 shdlc->hard_fault = r;
553 break;
556 shdlc->ns = (shdlc->ns + 1) % 8;
558 time_sent = jiffies;
559 *(unsigned long *)skb->cb = time_sent;
561 skb_queue_tail(&shdlc->ack_pending_q, skb);
563 if (shdlc->t2_active == false) {
564 shdlc->t2_active = true;
565 mod_timer(&shdlc->t2_timer, time_sent +
566 msecs_to_jiffies(SHDLC_T2_VALUE_MS));
567 pr_debug("Started T2 (retransmit)\n");
572 static void llc_shdlc_connect_timeout(struct timer_list *t)
574 struct llc_shdlc *shdlc = from_timer(shdlc, t, connect_timer);
576 pr_debug("\n");
578 schedule_work(&shdlc->sm_work);
581 static void llc_shdlc_t1_timeout(struct timer_list *t)
583 struct llc_shdlc *shdlc = from_timer(shdlc, t, t1_timer);
585 pr_debug("SoftIRQ: need to send ack\n");
587 schedule_work(&shdlc->sm_work);
590 static void llc_shdlc_t2_timeout(struct timer_list *t)
592 struct llc_shdlc *shdlc = from_timer(shdlc, t, t2_timer);
594 pr_debug("SoftIRQ: need to retransmit\n");
596 schedule_work(&shdlc->sm_work);
599 static void llc_shdlc_sm_work(struct work_struct *work)
601 struct llc_shdlc *shdlc = container_of(work, struct llc_shdlc, sm_work);
602 int r;
604 pr_debug("\n");
606 mutex_lock(&shdlc->state_mutex);
608 switch (shdlc->state) {
609 case SHDLC_DISCONNECTED:
610 skb_queue_purge(&shdlc->rcv_q);
611 skb_queue_purge(&shdlc->send_q);
612 skb_queue_purge(&shdlc->ack_pending_q);
613 break;
614 case SHDLC_CONNECTING:
615 if (shdlc->hard_fault) {
616 llc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
617 break;
620 if (shdlc->connect_tries++ < 5)
621 r = llc_shdlc_connect_initiate(shdlc);
622 else
623 r = -ETIME;
624 if (r < 0) {
625 llc_shdlc_connect_complete(shdlc, r);
626 } else {
627 mod_timer(&shdlc->connect_timer, jiffies +
628 msecs_to_jiffies(SHDLC_CONNECT_VALUE_MS));
630 shdlc->state = SHDLC_NEGOTIATING;
632 break;
633 case SHDLC_NEGOTIATING:
634 if (timer_pending(&shdlc->connect_timer) == 0) {
635 shdlc->state = SHDLC_CONNECTING;
636 schedule_work(&shdlc->sm_work);
639 llc_shdlc_handle_rcv_queue(shdlc);
641 if (shdlc->hard_fault) {
642 llc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
643 break;
645 break;
646 case SHDLC_HALF_CONNECTED:
647 case SHDLC_CONNECTED:
648 llc_shdlc_handle_rcv_queue(shdlc);
649 llc_shdlc_handle_send_queue(shdlc);
651 if (shdlc->t1_active && timer_pending(&shdlc->t1_timer) == 0) {
652 pr_debug
653 ("Handle T1(send ack) elapsed (T1 now inactive)\n");
655 shdlc->t1_active = false;
656 r = llc_shdlc_send_s_frame(shdlc, S_FRAME_RR,
657 shdlc->nr);
658 if (r < 0)
659 shdlc->hard_fault = r;
662 if (shdlc->t2_active && timer_pending(&shdlc->t2_timer) == 0) {
663 pr_debug
664 ("Handle T2(retransmit) elapsed (T2 inactive)\n");
666 shdlc->t2_active = false;
668 llc_shdlc_requeue_ack_pending(shdlc);
669 llc_shdlc_handle_send_queue(shdlc);
672 if (shdlc->hard_fault)
673 shdlc->llc_failure(shdlc->hdev, shdlc->hard_fault);
674 break;
675 default:
676 break;
678 mutex_unlock(&shdlc->state_mutex);
682 * Called from syscall context to establish shdlc link. Sleeps until
683 * link is ready or failure.
685 static int llc_shdlc_connect(struct llc_shdlc *shdlc)
687 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(connect_wq);
689 pr_debug("\n");
691 mutex_lock(&shdlc->state_mutex);
693 shdlc->state = SHDLC_CONNECTING;
694 shdlc->connect_wq = &connect_wq;
695 shdlc->connect_tries = 0;
696 shdlc->connect_result = 1;
698 mutex_unlock(&shdlc->state_mutex);
700 schedule_work(&shdlc->sm_work);
702 wait_event(connect_wq, shdlc->connect_result != 1);
704 return shdlc->connect_result;
707 static void llc_shdlc_disconnect(struct llc_shdlc *shdlc)
709 pr_debug("\n");
711 mutex_lock(&shdlc->state_mutex);
713 shdlc->state = SHDLC_DISCONNECTED;
715 mutex_unlock(&shdlc->state_mutex);
717 schedule_work(&shdlc->sm_work);
721 * Receive an incoming shdlc frame. Frame has already been crc-validated.
722 * skb contains only LLC header and payload.
723 * If skb == NULL, it is a notification that the link below is dead.
725 static void llc_shdlc_recv_frame(struct llc_shdlc *shdlc, struct sk_buff *skb)
727 if (skb == NULL) {
728 pr_err("NULL Frame -> link is dead\n");
729 shdlc->hard_fault = -EREMOTEIO;
730 } else {
731 SHDLC_DUMP_SKB("incoming frame", skb);
732 skb_queue_tail(&shdlc->rcv_q, skb);
735 schedule_work(&shdlc->sm_work);
738 static void *llc_shdlc_init(struct nfc_hci_dev *hdev, xmit_to_drv_t xmit_to_drv,
739 rcv_to_hci_t rcv_to_hci, int tx_headroom,
740 int tx_tailroom, int *rx_headroom, int *rx_tailroom,
741 llc_failure_t llc_failure)
743 struct llc_shdlc *shdlc;
745 *rx_headroom = SHDLC_LLC_HEAD_ROOM;
746 *rx_tailroom = 0;
748 shdlc = kzalloc(sizeof(struct llc_shdlc), GFP_KERNEL);
749 if (shdlc == NULL)
750 return NULL;
752 mutex_init(&shdlc->state_mutex);
753 shdlc->state = SHDLC_DISCONNECTED;
755 timer_setup(&shdlc->connect_timer, llc_shdlc_connect_timeout, 0);
756 timer_setup(&shdlc->t1_timer, llc_shdlc_t1_timeout, 0);
757 timer_setup(&shdlc->t2_timer, llc_shdlc_t2_timeout, 0);
759 shdlc->w = SHDLC_MAX_WINDOW;
760 shdlc->srej_support = SHDLC_SREJ_SUPPORT;
762 skb_queue_head_init(&shdlc->rcv_q);
763 skb_queue_head_init(&shdlc->send_q);
764 skb_queue_head_init(&shdlc->ack_pending_q);
766 INIT_WORK(&shdlc->sm_work, llc_shdlc_sm_work);
768 shdlc->hdev = hdev;
769 shdlc->xmit_to_drv = xmit_to_drv;
770 shdlc->rcv_to_hci = rcv_to_hci;
771 shdlc->tx_headroom = tx_headroom;
772 shdlc->tx_tailroom = tx_tailroom;
773 shdlc->llc_failure = llc_failure;
775 return shdlc;
778 static void llc_shdlc_deinit(struct nfc_llc *llc)
780 struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
782 skb_queue_purge(&shdlc->rcv_q);
783 skb_queue_purge(&shdlc->send_q);
784 skb_queue_purge(&shdlc->ack_pending_q);
786 kfree(shdlc);
789 static int llc_shdlc_start(struct nfc_llc *llc)
791 struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
793 return llc_shdlc_connect(shdlc);
796 static int llc_shdlc_stop(struct nfc_llc *llc)
798 struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
800 llc_shdlc_disconnect(shdlc);
802 return 0;
805 static void llc_shdlc_rcv_from_drv(struct nfc_llc *llc, struct sk_buff *skb)
807 struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
809 llc_shdlc_recv_frame(shdlc, skb);
812 static int llc_shdlc_xmit_from_hci(struct nfc_llc *llc, struct sk_buff *skb)
814 struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
816 skb_queue_tail(&shdlc->send_q, skb);
818 schedule_work(&shdlc->sm_work);
820 return 0;
823 static struct nfc_llc_ops llc_shdlc_ops = {
824 .init = llc_shdlc_init,
825 .deinit = llc_shdlc_deinit,
826 .start = llc_shdlc_start,
827 .stop = llc_shdlc_stop,
828 .rcv_from_drv = llc_shdlc_rcv_from_drv,
829 .xmit_from_hci = llc_shdlc_xmit_from_hci,
832 int nfc_llc_shdlc_register(void)
834 return nfc_llc_register(LLC_SHDLC_NAME, &llc_shdlc_ops);