2 * Copyright (C) 2012 Intel Corporation. All rights reserved.
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.
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.
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.
20 #define pr_fmt(fmt) "shdlc: %s: " fmt, __func__
22 #include <linux/sched.h>
23 #include <linux/export.h>
24 #include <linux/wait.h>
25 #include <linux/crc-ccitt.h>
26 #include <linux/slab.h>
27 #include <linux/skbuff.h>
29 #include <net/nfc/hci.h>
30 #include <net/nfc/shdlc.h>
32 #define SHDLC_LLC_HEAD_ROOM 2
33 #define SHDLC_LLC_TAIL_ROOM 2
35 #define SHDLC_MAX_WINDOW 4
36 #define SHDLC_SREJ_SUPPORT false
38 #define SHDLC_CONTROL_HEAD_MASK 0xe0
39 #define SHDLC_CONTROL_HEAD_I 0x80
40 #define SHDLC_CONTROL_HEAD_I2 0xa0
41 #define SHDLC_CONTROL_HEAD_S 0xc0
42 #define SHDLC_CONTROL_HEAD_U 0xe0
44 #define SHDLC_CONTROL_NS_MASK 0x38
45 #define SHDLC_CONTROL_NR_MASK 0x07
46 #define SHDLC_CONTROL_TYPE_MASK 0x18
48 #define SHDLC_CONTROL_M_MASK 0x1f
57 enum uframe_modifier {
62 #define SHDLC_CONNECT_VALUE_MS 5
63 #define SHDLC_T1_VALUE_MS(w) ((5 * w) / 4)
64 #define SHDLC_T2_VALUE_MS 300
66 #define SHDLC_DUMP_SKB(info, skb) \
68 pr_debug("%s:\n", info); \
69 print_hex_dump(KERN_DEBUG, "shdlc: ", DUMP_PREFIX_OFFSET, \
70 16, 1, skb->data, skb->len, 0); \
73 /* checks x < y <= z modulo 8 */
74 static bool nfc_shdlc_x_lt_y_lteq_z(int x, int y, int z)
77 return ((x < y) && (y <= z)) ? true : false;
79 return ((y > x) || (y <= z)) ? true : false;
82 /* checks x <= y < z modulo 8 */
83 static bool nfc_shdlc_x_lteq_y_lt_z(int x, int y, int z)
86 return ((x <= y) && (y < z)) ? true : false;
87 else /* x > z -> z+8 > x */
88 return ((y >= x) || (y < z)) ? true : false;
91 static struct sk_buff *nfc_shdlc_alloc_skb(struct nfc_shdlc *shdlc,
96 skb = alloc_skb(shdlc->client_headroom + SHDLC_LLC_HEAD_ROOM +
97 shdlc->client_tailroom + SHDLC_LLC_TAIL_ROOM +
98 payload_len, GFP_KERNEL);
100 skb_reserve(skb, shdlc->client_headroom + SHDLC_LLC_HEAD_ROOM);
105 static void nfc_shdlc_add_len_crc(struct sk_buff *skb)
111 *skb_push(skb, 1) = len;
113 crc = crc_ccitt(0xffff, skb->data, skb->len);
115 *skb_put(skb, 1) = crc & 0xff;
116 *skb_put(skb, 1) = crc >> 8;
119 /* immediately sends an S frame. */
120 static int nfc_shdlc_send_s_frame(struct nfc_shdlc *shdlc,
121 enum sframe_type sframe_type, int nr)
126 pr_debug("sframe_type=%d nr=%d\n", sframe_type, nr);
128 skb = nfc_shdlc_alloc_skb(shdlc, 0);
132 *skb_push(skb, 1) = SHDLC_CONTROL_HEAD_S | (sframe_type << 3) | nr;
134 nfc_shdlc_add_len_crc(skb);
136 r = shdlc->ops->xmit(shdlc, skb);
143 /* immediately sends an U frame. skb may contain optional payload */
144 static int nfc_shdlc_send_u_frame(struct nfc_shdlc *shdlc,
146 enum uframe_modifier uframe_modifier)
150 pr_debug("uframe_modifier=%d\n", uframe_modifier);
152 *skb_push(skb, 1) = SHDLC_CONTROL_HEAD_U | uframe_modifier;
154 nfc_shdlc_add_len_crc(skb);
156 r = shdlc->ops->xmit(shdlc, skb);
164 * Free ack_pending frames until y_nr - 1, and reset t2 according to
165 * the remaining oldest ack_pending frame sent time
167 static void nfc_shdlc_reset_t2(struct nfc_shdlc *shdlc, int y_nr)
170 int dnr = shdlc->dnr; /* MUST initially be < y_nr */
172 pr_debug("release ack pending up to frame %d excluded\n", y_nr);
174 while (dnr != y_nr) {
175 pr_debug("release ack pending frame %d\n", dnr);
177 skb = skb_dequeue(&shdlc->ack_pending_q);
183 if (skb_queue_empty(&shdlc->ack_pending_q)) {
184 if (shdlc->t2_active) {
185 del_timer_sync(&shdlc->t2_timer);
186 shdlc->t2_active = false;
189 ("All sent frames acked. Stopped T2(retransmit)\n");
192 skb = skb_peek(&shdlc->ack_pending_q);
194 mod_timer(&shdlc->t2_timer, *(unsigned long *)skb->cb +
195 msecs_to_jiffies(SHDLC_T2_VALUE_MS));
196 shdlc->t2_active = true;
199 ("Start T2(retransmit) for remaining unacked sent frames\n");
204 * Receive validated frames from lower layer. skb contains HCI payload only.
205 * Handle according to algorithm at spec:10.8.2
207 static void nfc_shdlc_rcv_i_frame(struct nfc_shdlc *shdlc,
208 struct sk_buff *skb, int ns, int nr)
213 pr_debug("recvd I-frame %d, remote waiting frame %d\n", ns, nr);
215 if (shdlc->state != SHDLC_CONNECTED)
218 if (x_ns != shdlc->nr) {
219 nfc_shdlc_send_s_frame(shdlc, S_FRAME_REJ, shdlc->nr);
223 if (shdlc->t1_active == false) {
224 shdlc->t1_active = true;
225 mod_timer(&shdlc->t1_timer,
226 msecs_to_jiffies(SHDLC_T1_VALUE_MS(shdlc->w)));
227 pr_debug("(re)Start T1(send ack)\n");
231 nfc_hci_recv_frame(shdlc->hdev, skb);
235 shdlc->nr = (shdlc->nr + 1) % 8;
237 if (nfc_shdlc_x_lt_y_lteq_z(shdlc->dnr, y_nr, shdlc->ns)) {
238 nfc_shdlc_reset_t2(shdlc, y_nr);
248 static void nfc_shdlc_rcv_ack(struct nfc_shdlc *shdlc, int y_nr)
250 pr_debug("remote acked up to frame %d excluded\n", y_nr);
252 if (nfc_shdlc_x_lt_y_lteq_z(shdlc->dnr, y_nr, shdlc->ns)) {
253 nfc_shdlc_reset_t2(shdlc, y_nr);
258 static void nfc_shdlc_requeue_ack_pending(struct nfc_shdlc *shdlc)
262 pr_debug("ns reset to %d\n", shdlc->dnr);
264 while ((skb = skb_dequeue_tail(&shdlc->ack_pending_q))) {
265 skb_pull(skb, 2); /* remove len+control */
266 skb_trim(skb, skb->len - 2); /* remove crc */
267 skb_queue_head(&shdlc->send_q, skb);
269 shdlc->ns = shdlc->dnr;
272 static void nfc_shdlc_rcv_rej(struct nfc_shdlc *shdlc, int y_nr)
276 pr_debug("remote asks retransmition from frame %d\n", y_nr);
278 if (nfc_shdlc_x_lteq_y_lt_z(shdlc->dnr, y_nr, shdlc->ns)) {
279 if (shdlc->t2_active) {
280 del_timer_sync(&shdlc->t2_timer);
281 shdlc->t2_active = false;
282 pr_debug("Stopped T2(retransmit)\n");
285 if (shdlc->dnr != y_nr) {
286 while ((shdlc->dnr = ((shdlc->dnr + 1) % 8)) != y_nr) {
287 skb = skb_dequeue(&shdlc->ack_pending_q);
292 nfc_shdlc_requeue_ack_pending(shdlc);
296 /* See spec RR:10.8.3 REJ:10.8.4 */
297 static void nfc_shdlc_rcv_s_frame(struct nfc_shdlc *shdlc,
298 enum sframe_type s_frame_type, int nr)
302 if (shdlc->state != SHDLC_CONNECTED)
305 switch (s_frame_type) {
307 nfc_shdlc_rcv_ack(shdlc, nr);
308 if (shdlc->rnr == true) { /* see SHDLC 10.7.7 */
310 if (shdlc->send_q.qlen == 0) {
311 skb = nfc_shdlc_alloc_skb(shdlc, 0);
313 skb_queue_tail(&shdlc->send_q, skb);
318 nfc_shdlc_rcv_rej(shdlc, nr);
321 nfc_shdlc_rcv_ack(shdlc, nr);
329 static void nfc_shdlc_connect_complete(struct nfc_shdlc *shdlc, int r)
331 pr_debug("result=%d\n", r);
333 del_timer_sync(&shdlc->connect_timer);
340 shdlc->state = SHDLC_CONNECTED;
342 shdlc->state = SHDLC_DISCONNECTED;
345 shdlc->connect_result = r;
347 wake_up(shdlc->connect_wq);
350 static int nfc_shdlc_connect_initiate(struct nfc_shdlc *shdlc)
356 skb = nfc_shdlc_alloc_skb(shdlc, 2);
360 *skb_put(skb, 1) = SHDLC_MAX_WINDOW;
361 *skb_put(skb, 1) = SHDLC_SREJ_SUPPORT ? 1 : 0;
363 return nfc_shdlc_send_u_frame(shdlc, skb, U_FRAME_RSET);
366 static int nfc_shdlc_connect_send_ua(struct nfc_shdlc *shdlc)
372 skb = nfc_shdlc_alloc_skb(shdlc, 0);
376 return nfc_shdlc_send_u_frame(shdlc, skb, U_FRAME_UA);
379 static void nfc_shdlc_rcv_u_frame(struct nfc_shdlc *shdlc,
381 enum uframe_modifier u_frame_modifier)
383 u8 w = SHDLC_MAX_WINDOW;
384 bool srej_support = SHDLC_SREJ_SUPPORT;
387 pr_debug("u_frame_modifier=%d\n", u_frame_modifier);
389 switch (u_frame_modifier) {
391 if (shdlc->state == SHDLC_NEGOCIATING) {
392 /* we sent RSET, but chip wants to negociate */
397 srej_support = skb->data[1] & 0x01 ? true :
400 if ((w <= SHDLC_MAX_WINDOW) &&
401 (SHDLC_SREJ_SUPPORT || (srej_support == false))) {
403 shdlc->srej_support = srej_support;
404 r = nfc_shdlc_connect_send_ua(shdlc);
405 nfc_shdlc_connect_complete(shdlc, r);
407 } else if (shdlc->state == SHDLC_CONNECTED) {
409 * Chip wants to reset link. This is unexpected and
412 shdlc->hard_fault = -ECONNRESET;
416 if ((shdlc->state == SHDLC_CONNECTING &&
417 shdlc->connect_tries > 0) ||
418 (shdlc->state == SHDLC_NEGOCIATING))
419 nfc_shdlc_connect_complete(shdlc, 0);
428 static void nfc_shdlc_handle_rcv_queue(struct nfc_shdlc *shdlc)
434 enum sframe_type s_frame_type;
435 enum uframe_modifier u_frame_modifier;
437 if (shdlc->rcv_q.qlen)
438 pr_debug("rcvQlen=%d\n", shdlc->rcv_q.qlen);
440 while ((skb = skb_dequeue(&shdlc->rcv_q)) != NULL) {
441 control = skb->data[0];
443 switch (control & SHDLC_CONTROL_HEAD_MASK) {
444 case SHDLC_CONTROL_HEAD_I:
445 case SHDLC_CONTROL_HEAD_I2:
446 ns = (control & SHDLC_CONTROL_NS_MASK) >> 3;
447 nr = control & SHDLC_CONTROL_NR_MASK;
448 nfc_shdlc_rcv_i_frame(shdlc, skb, ns, nr);
450 case SHDLC_CONTROL_HEAD_S:
451 s_frame_type = (control & SHDLC_CONTROL_TYPE_MASK) >> 3;
452 nr = control & SHDLC_CONTROL_NR_MASK;
453 nfc_shdlc_rcv_s_frame(shdlc, s_frame_type, nr);
456 case SHDLC_CONTROL_HEAD_U:
457 u_frame_modifier = control & SHDLC_CONTROL_M_MASK;
458 nfc_shdlc_rcv_u_frame(shdlc, skb, u_frame_modifier);
461 pr_err("UNKNOWN Control=%d\n", control);
468 static int nfc_shdlc_w_used(int ns, int dnr)
473 unack_count = ns - dnr;
475 unack_count = 8 - dnr + ns;
480 /* Send frames according to algorithm at spec:10.8.1 */
481 static void nfc_shdlc_handle_send_queue(struct nfc_shdlc *shdlc)
485 unsigned long time_sent;
487 if (shdlc->send_q.qlen)
489 ("sendQlen=%d ns=%d dnr=%d rnr=%s w_room=%d unackQlen=%d\n",
490 shdlc->send_q.qlen, shdlc->ns, shdlc->dnr,
491 shdlc->rnr == false ? "false" : "true",
492 shdlc->w - nfc_shdlc_w_used(shdlc->ns, shdlc->dnr),
493 shdlc->ack_pending_q.qlen);
495 while (shdlc->send_q.qlen && shdlc->ack_pending_q.qlen < shdlc->w &&
496 (shdlc->rnr == false)) {
498 if (shdlc->t1_active) {
499 del_timer_sync(&shdlc->t1_timer);
500 shdlc->t1_active = false;
501 pr_debug("Stopped T1(send ack)\n");
504 skb = skb_dequeue(&shdlc->send_q);
506 *skb_push(skb, 1) = SHDLC_CONTROL_HEAD_I | (shdlc->ns << 3) |
509 pr_debug("Sending I-Frame %d, waiting to rcv %d\n", shdlc->ns,
511 /* SHDLC_DUMP_SKB("shdlc frame written", skb); */
513 nfc_shdlc_add_len_crc(skb);
515 r = shdlc->ops->xmit(shdlc, skb);
517 shdlc->hard_fault = r;
521 shdlc->ns = (shdlc->ns + 1) % 8;
524 *(unsigned long *)skb->cb = time_sent;
526 skb_queue_tail(&shdlc->ack_pending_q, skb);
528 if (shdlc->t2_active == false) {
529 shdlc->t2_active = true;
530 mod_timer(&shdlc->t2_timer, time_sent +
531 msecs_to_jiffies(SHDLC_T2_VALUE_MS));
532 pr_debug("Started T2 (retransmit)\n");
537 static void nfc_shdlc_connect_timeout(unsigned long data)
539 struct nfc_shdlc *shdlc = (struct nfc_shdlc *)data;
543 queue_work(shdlc->sm_wq, &shdlc->sm_work);
546 static void nfc_shdlc_t1_timeout(unsigned long data)
548 struct nfc_shdlc *shdlc = (struct nfc_shdlc *)data;
550 pr_debug("SoftIRQ: need to send ack\n");
552 queue_work(shdlc->sm_wq, &shdlc->sm_work);
555 static void nfc_shdlc_t2_timeout(unsigned long data)
557 struct nfc_shdlc *shdlc = (struct nfc_shdlc *)data;
559 pr_debug("SoftIRQ: need to retransmit\n");
561 queue_work(shdlc->sm_wq, &shdlc->sm_work);
564 static void nfc_shdlc_sm_work(struct work_struct *work)
566 struct nfc_shdlc *shdlc = container_of(work, struct nfc_shdlc, sm_work);
571 mutex_lock(&shdlc->state_mutex);
573 switch (shdlc->state) {
574 case SHDLC_DISCONNECTED:
575 skb_queue_purge(&shdlc->rcv_q);
576 skb_queue_purge(&shdlc->send_q);
577 skb_queue_purge(&shdlc->ack_pending_q);
579 case SHDLC_CONNECTING:
580 if (shdlc->hard_fault) {
581 nfc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
585 if (shdlc->connect_tries++ < 5)
586 r = nfc_shdlc_connect_initiate(shdlc);
590 nfc_shdlc_connect_complete(shdlc, r);
592 mod_timer(&shdlc->connect_timer, jiffies +
593 msecs_to_jiffies(SHDLC_CONNECT_VALUE_MS));
595 shdlc->state = SHDLC_NEGOCIATING;
598 case SHDLC_NEGOCIATING:
599 if (timer_pending(&shdlc->connect_timer) == 0) {
600 shdlc->state = SHDLC_CONNECTING;
601 queue_work(shdlc->sm_wq, &shdlc->sm_work);
604 nfc_shdlc_handle_rcv_queue(shdlc);
606 if (shdlc->hard_fault) {
607 nfc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
611 case SHDLC_CONNECTED:
612 nfc_shdlc_handle_rcv_queue(shdlc);
613 nfc_shdlc_handle_send_queue(shdlc);
615 if (shdlc->t1_active && timer_pending(&shdlc->t1_timer) == 0) {
617 ("Handle T1(send ack) elapsed (T1 now inactive)\n");
619 shdlc->t1_active = false;
620 r = nfc_shdlc_send_s_frame(shdlc, S_FRAME_RR,
623 shdlc->hard_fault = r;
626 if (shdlc->t2_active && timer_pending(&shdlc->t2_timer) == 0) {
628 ("Handle T2(retransmit) elapsed (T2 inactive)\n");
630 shdlc->t2_active = false;
632 nfc_shdlc_requeue_ack_pending(shdlc);
633 nfc_shdlc_handle_send_queue(shdlc);
636 if (shdlc->hard_fault) {
637 nfc_hci_driver_failure(shdlc->hdev, shdlc->hard_fault);
643 mutex_unlock(&shdlc->state_mutex);
647 * Called from syscall context to establish shdlc link. Sleeps until
648 * link is ready or failure.
650 static int nfc_shdlc_connect(struct nfc_shdlc *shdlc)
652 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(connect_wq);
656 mutex_lock(&shdlc->state_mutex);
658 shdlc->state = SHDLC_CONNECTING;
659 shdlc->connect_wq = &connect_wq;
660 shdlc->connect_tries = 0;
661 shdlc->connect_result = 1;
663 mutex_unlock(&shdlc->state_mutex);
665 queue_work(shdlc->sm_wq, &shdlc->sm_work);
667 wait_event(connect_wq, shdlc->connect_result != 1);
669 return shdlc->connect_result;
672 static void nfc_shdlc_disconnect(struct nfc_shdlc *shdlc)
676 mutex_lock(&shdlc->state_mutex);
678 shdlc->state = SHDLC_DISCONNECTED;
680 mutex_unlock(&shdlc->state_mutex);
682 queue_work(shdlc->sm_wq, &shdlc->sm_work);
686 * Receive an incoming shdlc frame. Frame has already been crc-validated.
687 * skb contains only LLC header and payload.
688 * If skb == NULL, it is a notification that the link below is dead.
690 void nfc_shdlc_recv_frame(struct nfc_shdlc *shdlc, struct sk_buff *skb)
693 pr_err("NULL Frame -> link is dead\n");
694 shdlc->hard_fault = -EREMOTEIO;
696 SHDLC_DUMP_SKB("incoming frame", skb);
697 skb_queue_tail(&shdlc->rcv_q, skb);
700 queue_work(shdlc->sm_wq, &shdlc->sm_work);
702 EXPORT_SYMBOL(nfc_shdlc_recv_frame);
704 static int nfc_shdlc_open(struct nfc_hci_dev *hdev)
706 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
711 if (shdlc->ops->open) {
712 r = shdlc->ops->open(shdlc);
717 r = nfc_shdlc_connect(shdlc);
718 if (r < 0 && shdlc->ops->close)
719 shdlc->ops->close(shdlc);
724 static void nfc_shdlc_close(struct nfc_hci_dev *hdev)
726 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
730 nfc_shdlc_disconnect(shdlc);
732 if (shdlc->ops->close)
733 shdlc->ops->close(shdlc);
736 static int nfc_shdlc_hci_ready(struct nfc_hci_dev *hdev)
738 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
743 if (shdlc->ops->hci_ready)
744 r = shdlc->ops->hci_ready(shdlc);
749 static int nfc_shdlc_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
751 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
753 SHDLC_DUMP_SKB("queuing HCP packet to shdlc", skb);
755 skb_queue_tail(&shdlc->send_q, skb);
757 queue_work(shdlc->sm_wq, &shdlc->sm_work);
762 static int nfc_shdlc_start_poll(struct nfc_hci_dev *hdev,
763 u32 im_protocols, u32 tm_protocols)
765 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
769 if (shdlc->ops->start_poll)
770 return shdlc->ops->start_poll(shdlc,
771 im_protocols, tm_protocols);
776 static int nfc_shdlc_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
777 struct nfc_target *target)
779 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
781 if (shdlc->ops->target_from_gate)
782 return shdlc->ops->target_from_gate(shdlc, gate, target);
787 static int nfc_shdlc_complete_target_discovered(struct nfc_hci_dev *hdev,
789 struct nfc_target *target)
791 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
795 if (shdlc->ops->complete_target_discovered)
796 return shdlc->ops->complete_target_discovered(shdlc, gate,
802 static int nfc_shdlc_data_exchange(struct nfc_hci_dev *hdev,
803 struct nfc_target *target,
805 struct sk_buff **res_skb)
807 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
809 if (shdlc->ops->data_exchange)
810 return shdlc->ops->data_exchange(shdlc, target, skb, res_skb);
815 static int nfc_shdlc_check_presence(struct nfc_hci_dev *hdev,
816 struct nfc_target *target)
818 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
820 if (shdlc->ops->check_presence)
821 return shdlc->ops->check_presence(shdlc, target);
826 static struct nfc_hci_ops shdlc_ops = {
827 .open = nfc_shdlc_open,
828 .close = nfc_shdlc_close,
829 .hci_ready = nfc_shdlc_hci_ready,
830 .xmit = nfc_shdlc_xmit,
831 .start_poll = nfc_shdlc_start_poll,
832 .target_from_gate = nfc_shdlc_target_from_gate,
833 .complete_target_discovered = nfc_shdlc_complete_target_discovered,
834 .data_exchange = nfc_shdlc_data_exchange,
835 .check_presence = nfc_shdlc_check_presence,
838 struct nfc_shdlc *nfc_shdlc_allocate(struct nfc_shdlc_ops *ops,
839 struct nfc_hci_init_data *init_data,
841 int tx_headroom, int tx_tailroom,
842 int max_link_payload, const char *devname)
844 struct nfc_shdlc *shdlc;
848 if (ops->xmit == NULL)
851 shdlc = kzalloc(sizeof(struct nfc_shdlc), GFP_KERNEL);
855 mutex_init(&shdlc->state_mutex);
857 shdlc->state = SHDLC_DISCONNECTED;
859 init_timer(&shdlc->connect_timer);
860 shdlc->connect_timer.data = (unsigned long)shdlc;
861 shdlc->connect_timer.function = nfc_shdlc_connect_timeout;
863 init_timer(&shdlc->t1_timer);
864 shdlc->t1_timer.data = (unsigned long)shdlc;
865 shdlc->t1_timer.function = nfc_shdlc_t1_timeout;
867 init_timer(&shdlc->t2_timer);
868 shdlc->t2_timer.data = (unsigned long)shdlc;
869 shdlc->t2_timer.function = nfc_shdlc_t2_timeout;
871 shdlc->w = SHDLC_MAX_WINDOW;
872 shdlc->srej_support = SHDLC_SREJ_SUPPORT;
874 skb_queue_head_init(&shdlc->rcv_q);
875 skb_queue_head_init(&shdlc->send_q);
876 skb_queue_head_init(&shdlc->ack_pending_q);
878 INIT_WORK(&shdlc->sm_work, nfc_shdlc_sm_work);
879 snprintf(name, sizeof(name), "%s_shdlc_sm_wq", devname);
880 shdlc->sm_wq = alloc_workqueue(name, WQ_NON_REENTRANT | WQ_UNBOUND |
882 if (shdlc->sm_wq == NULL)
885 shdlc->client_headroom = tx_headroom;
886 shdlc->client_tailroom = tx_tailroom;
888 shdlc->hdev = nfc_hci_allocate_device(&shdlc_ops, init_data, protocols,
889 tx_headroom + SHDLC_LLC_HEAD_ROOM,
890 tx_tailroom + SHDLC_LLC_TAIL_ROOM,
892 if (shdlc->hdev == NULL)
895 nfc_hci_set_clientdata(shdlc->hdev, shdlc);
897 r = nfc_hci_register_device(shdlc->hdev);
904 nfc_hci_free_device(shdlc->hdev);
907 destroy_workqueue(shdlc->sm_wq);
914 EXPORT_SYMBOL(nfc_shdlc_allocate);
916 void nfc_shdlc_free(struct nfc_shdlc *shdlc)
920 nfc_hci_unregister_device(shdlc->hdev);
921 nfc_hci_free_device(shdlc->hdev);
923 destroy_workqueue(shdlc->sm_wq);
925 skb_queue_purge(&shdlc->rcv_q);
926 skb_queue_purge(&shdlc->send_q);
927 skb_queue_purge(&shdlc->ack_pending_q);
931 EXPORT_SYMBOL(nfc_shdlc_free);
933 void nfc_shdlc_set_clientdata(struct nfc_shdlc *shdlc, void *clientdata)
937 shdlc->clientdata = clientdata;
939 EXPORT_SYMBOL(nfc_shdlc_set_clientdata);
941 void *nfc_shdlc_get_clientdata(struct nfc_shdlc *shdlc)
943 return shdlc->clientdata;
945 EXPORT_SYMBOL(nfc_shdlc_get_clientdata);
947 struct nfc_hci_dev *nfc_shdlc_get_hci_dev(struct nfc_shdlc *shdlc)
951 EXPORT_SYMBOL(nfc_shdlc_get_hci_dev);