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 * TODO: Could it be possible that there are pending
346 * executing commands that are waiting for connect to complete
347 * before they can be carried? As connect is a blocking
348 * operation, it would require that the userspace process can
349 * send commands on the same device from a second thread before
350 * the device is up. I don't think that is possible, is it?
354 shdlc
->connect_result
= r
;
356 wake_up(shdlc
->connect_wq
);
359 static int nfc_shdlc_connect_initiate(struct nfc_shdlc
*shdlc
)
365 skb
= nfc_shdlc_alloc_skb(shdlc
, 2);
369 *skb_put(skb
, 1) = SHDLC_MAX_WINDOW
;
370 *skb_put(skb
, 1) = SHDLC_SREJ_SUPPORT
? 1 : 0;
372 return nfc_shdlc_send_u_frame(shdlc
, skb
, U_FRAME_RSET
);
375 static int nfc_shdlc_connect_send_ua(struct nfc_shdlc
*shdlc
)
381 skb
= nfc_shdlc_alloc_skb(shdlc
, 0);
385 return nfc_shdlc_send_u_frame(shdlc
, skb
, U_FRAME_UA
);
388 static void nfc_shdlc_rcv_u_frame(struct nfc_shdlc
*shdlc
,
390 enum uframe_modifier u_frame_modifier
)
392 u8 w
= SHDLC_MAX_WINDOW
;
393 bool srej_support
= SHDLC_SREJ_SUPPORT
;
396 pr_debug("u_frame_modifier=%d\n", u_frame_modifier
);
398 switch (u_frame_modifier
) {
400 if (shdlc
->state
== SHDLC_NEGOCIATING
) {
401 /* we sent RSET, but chip wants to negociate */
406 srej_support
= skb
->data
[1] & 0x01 ? true :
409 if ((w
<= SHDLC_MAX_WINDOW
) &&
410 (SHDLC_SREJ_SUPPORT
|| (srej_support
== false))) {
412 shdlc
->srej_support
= srej_support
;
413 r
= nfc_shdlc_connect_send_ua(shdlc
);
414 nfc_shdlc_connect_complete(shdlc
, r
);
416 } else if (shdlc
->state
> SHDLC_NEGOCIATING
) {
418 * TODO: Chip wants to reset link
419 * send ua, empty skb lists, reset counters
420 * propagate info to HCI layer
425 if ((shdlc
->state
== SHDLC_CONNECTING
&&
426 shdlc
->connect_tries
> 0) ||
427 (shdlc
->state
== SHDLC_NEGOCIATING
))
428 nfc_shdlc_connect_complete(shdlc
, 0);
437 static void nfc_shdlc_handle_rcv_queue(struct nfc_shdlc
*shdlc
)
443 enum sframe_type s_frame_type
;
444 enum uframe_modifier u_frame_modifier
;
446 if (shdlc
->rcv_q
.qlen
)
447 pr_debug("rcvQlen=%d\n", shdlc
->rcv_q
.qlen
);
449 while ((skb
= skb_dequeue(&shdlc
->rcv_q
)) != NULL
) {
450 control
= skb
->data
[0];
452 switch (control
& SHDLC_CONTROL_HEAD_MASK
) {
453 case SHDLC_CONTROL_HEAD_I
:
454 case SHDLC_CONTROL_HEAD_I2
:
455 ns
= (control
& SHDLC_CONTROL_NS_MASK
) >> 3;
456 nr
= control
& SHDLC_CONTROL_NR_MASK
;
457 nfc_shdlc_rcv_i_frame(shdlc
, skb
, ns
, nr
);
459 case SHDLC_CONTROL_HEAD_S
:
460 s_frame_type
= (control
& SHDLC_CONTROL_TYPE_MASK
) >> 3;
461 nr
= control
& SHDLC_CONTROL_NR_MASK
;
462 nfc_shdlc_rcv_s_frame(shdlc
, s_frame_type
, nr
);
465 case SHDLC_CONTROL_HEAD_U
:
466 u_frame_modifier
= control
& SHDLC_CONTROL_M_MASK
;
467 nfc_shdlc_rcv_u_frame(shdlc
, skb
, u_frame_modifier
);
470 pr_err("UNKNOWN Control=%d\n", control
);
477 static int nfc_shdlc_w_used(int ns
, int dnr
)
482 unack_count
= ns
- dnr
;
484 unack_count
= 8 - dnr
+ ns
;
489 /* Send frames according to algorithm at spec:10.8.1 */
490 static void nfc_shdlc_handle_send_queue(struct nfc_shdlc
*shdlc
)
494 unsigned long time_sent
;
496 if (shdlc
->send_q
.qlen
)
498 ("sendQlen=%d ns=%d dnr=%d rnr=%s w_room=%d unackQlen=%d\n",
499 shdlc
->send_q
.qlen
, shdlc
->ns
, shdlc
->dnr
,
500 shdlc
->rnr
== false ? "false" : "true",
501 shdlc
->w
- nfc_shdlc_w_used(shdlc
->ns
, shdlc
->dnr
),
502 shdlc
->ack_pending_q
.qlen
);
504 while (shdlc
->send_q
.qlen
&& shdlc
->ack_pending_q
.qlen
< shdlc
->w
&&
505 (shdlc
->rnr
== false)) {
507 if (shdlc
->t1_active
) {
508 del_timer_sync(&shdlc
->t1_timer
);
509 shdlc
->t1_active
= false;
510 pr_debug("Stopped T1(send ack)\n");
513 skb
= skb_dequeue(&shdlc
->send_q
);
515 *skb_push(skb
, 1) = SHDLC_CONTROL_HEAD_I
| (shdlc
->ns
<< 3) |
518 pr_debug("Sending I-Frame %d, waiting to rcv %d\n", shdlc
->ns
,
520 /* SHDLC_DUMP_SKB("shdlc frame written", skb); */
522 nfc_shdlc_add_len_crc(skb
);
524 r
= shdlc
->ops
->xmit(shdlc
, skb
);
527 * TODO: Cannot send, shdlc machine is dead, we
528 * must propagate the information up to HCI.
530 shdlc
->hard_fault
= r
;
534 shdlc
->ns
= (shdlc
->ns
+ 1) % 8;
537 *(unsigned long *)skb
->cb
= time_sent
;
539 skb_queue_tail(&shdlc
->ack_pending_q
, skb
);
541 if (shdlc
->t2_active
== false) {
542 shdlc
->t2_active
= true;
543 mod_timer(&shdlc
->t2_timer
, time_sent
+
544 msecs_to_jiffies(SHDLC_T2_VALUE_MS
));
545 pr_debug("Started T2 (retransmit)\n");
550 static void nfc_shdlc_connect_timeout(unsigned long data
)
552 struct nfc_shdlc
*shdlc
= (struct nfc_shdlc
*)data
;
556 queue_work(shdlc
->sm_wq
, &shdlc
->sm_work
);
559 static void nfc_shdlc_t1_timeout(unsigned long data
)
561 struct nfc_shdlc
*shdlc
= (struct nfc_shdlc
*)data
;
563 pr_debug("SoftIRQ: need to send ack\n");
565 queue_work(shdlc
->sm_wq
, &shdlc
->sm_work
);
568 static void nfc_shdlc_t2_timeout(unsigned long data
)
570 struct nfc_shdlc
*shdlc
= (struct nfc_shdlc
*)data
;
572 pr_debug("SoftIRQ: need to retransmit\n");
574 queue_work(shdlc
->sm_wq
, &shdlc
->sm_work
);
577 static void nfc_shdlc_sm_work(struct work_struct
*work
)
579 struct nfc_shdlc
*shdlc
= container_of(work
, struct nfc_shdlc
, sm_work
);
584 mutex_lock(&shdlc
->state_mutex
);
586 switch (shdlc
->state
) {
587 case SHDLC_DISCONNECTED
:
588 skb_queue_purge(&shdlc
->rcv_q
);
589 skb_queue_purge(&shdlc
->send_q
);
590 skb_queue_purge(&shdlc
->ack_pending_q
);
592 case SHDLC_CONNECTING
:
593 if (shdlc
->connect_tries
++ < 5)
594 r
= nfc_shdlc_connect_initiate(shdlc
);
598 nfc_shdlc_connect_complete(shdlc
, r
);
600 mod_timer(&shdlc
->connect_timer
, jiffies
+
601 msecs_to_jiffies(SHDLC_CONNECT_VALUE_MS
));
603 shdlc
->state
= SHDLC_NEGOCIATING
;
606 case SHDLC_NEGOCIATING
:
607 if (timer_pending(&shdlc
->connect_timer
) == 0) {
608 shdlc
->state
= SHDLC_CONNECTING
;
609 queue_work(shdlc
->sm_wq
, &shdlc
->sm_work
);
612 nfc_shdlc_handle_rcv_queue(shdlc
);
614 case SHDLC_CONNECTED
:
615 nfc_shdlc_handle_rcv_queue(shdlc
);
616 nfc_shdlc_handle_send_queue(shdlc
);
618 if (shdlc
->t1_active
&& timer_pending(&shdlc
->t1_timer
) == 0) {
620 ("Handle T1(send ack) elapsed (T1 now inactive)\n");
622 shdlc
->t1_active
= false;
623 r
= nfc_shdlc_send_s_frame(shdlc
, S_FRAME_RR
,
626 shdlc
->hard_fault
= r
;
629 if (shdlc
->t2_active
&& timer_pending(&shdlc
->t2_timer
) == 0) {
631 ("Handle T2(retransmit) elapsed (T2 inactive)\n");
633 shdlc
->t2_active
= false;
635 nfc_shdlc_requeue_ack_pending(shdlc
);
636 nfc_shdlc_handle_send_queue(shdlc
);
639 if (shdlc
->hard_fault
) {
641 * TODO: Handle hard_fault that occured during
642 * this invocation of the shdlc worker
649 mutex_unlock(&shdlc
->state_mutex
);
653 * Called from syscall context to establish shdlc link. Sleeps until
654 * link is ready or failure.
656 static int nfc_shdlc_connect(struct nfc_shdlc
*shdlc
)
658 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(connect_wq
);
662 mutex_lock(&shdlc
->state_mutex
);
664 shdlc
->state
= SHDLC_CONNECTING
;
665 shdlc
->connect_wq
= &connect_wq
;
666 shdlc
->connect_tries
= 0;
667 shdlc
->connect_result
= 1;
669 mutex_unlock(&shdlc
->state_mutex
);
671 queue_work(shdlc
->sm_wq
, &shdlc
->sm_work
);
673 wait_event(connect_wq
, shdlc
->connect_result
!= 1);
675 return shdlc
->connect_result
;
678 static void nfc_shdlc_disconnect(struct nfc_shdlc
*shdlc
)
682 mutex_lock(&shdlc
->state_mutex
);
684 shdlc
->state
= SHDLC_DISCONNECTED
;
686 mutex_unlock(&shdlc
->state_mutex
);
688 queue_work(shdlc
->sm_wq
, &shdlc
->sm_work
);
692 * Receive an incoming shdlc frame. Frame has already been crc-validated.
693 * skb contains only LLC header and payload.
694 * If skb == NULL, it is a notification that the link below is dead.
696 void nfc_shdlc_recv_frame(struct nfc_shdlc
*shdlc
, struct sk_buff
*skb
)
699 pr_err("NULL Frame -> link is dead\n");
700 shdlc
->hard_fault
= -EREMOTEIO
;
702 SHDLC_DUMP_SKB("incoming frame", skb
);
703 skb_queue_tail(&shdlc
->rcv_q
, skb
);
706 queue_work(shdlc
->sm_wq
, &shdlc
->sm_work
);
708 EXPORT_SYMBOL(nfc_shdlc_recv_frame
);
710 static int nfc_shdlc_open(struct nfc_hci_dev
*hdev
)
712 struct nfc_shdlc
*shdlc
= nfc_hci_get_clientdata(hdev
);
717 if (shdlc
->ops
->open
) {
718 r
= shdlc
->ops
->open(shdlc
);
723 r
= nfc_shdlc_connect(shdlc
);
724 if (r
< 0 && shdlc
->ops
->close
)
725 shdlc
->ops
->close(shdlc
);
730 static void nfc_shdlc_close(struct nfc_hci_dev
*hdev
)
732 struct nfc_shdlc
*shdlc
= nfc_hci_get_clientdata(hdev
);
736 nfc_shdlc_disconnect(shdlc
);
738 if (shdlc
->ops
->close
)
739 shdlc
->ops
->close(shdlc
);
742 static int nfc_shdlc_hci_ready(struct nfc_hci_dev
*hdev
)
744 struct nfc_shdlc
*shdlc
= nfc_hci_get_clientdata(hdev
);
749 if (shdlc
->ops
->hci_ready
)
750 r
= shdlc
->ops
->hci_ready(shdlc
);
755 static int nfc_shdlc_xmit(struct nfc_hci_dev
*hdev
, struct sk_buff
*skb
)
757 struct nfc_shdlc
*shdlc
= nfc_hci_get_clientdata(hdev
);
759 SHDLC_DUMP_SKB("queuing HCP packet to shdlc", skb
);
761 skb_queue_tail(&shdlc
->send_q
, skb
);
763 queue_work(shdlc
->sm_wq
, &shdlc
->sm_work
);
768 static int nfc_shdlc_start_poll(struct nfc_hci_dev
*hdev
, u32 protocols
)
770 struct nfc_shdlc
*shdlc
= nfc_hci_get_clientdata(hdev
);
774 if (shdlc
->ops
->start_poll
)
775 return shdlc
->ops
->start_poll(shdlc
, protocols
);
780 static int nfc_shdlc_target_from_gate(struct nfc_hci_dev
*hdev
, u8 gate
,
781 struct nfc_target
*target
)
783 struct nfc_shdlc
*shdlc
= nfc_hci_get_clientdata(hdev
);
785 if (shdlc
->ops
->target_from_gate
)
786 return shdlc
->ops
->target_from_gate(shdlc
, gate
, target
);
791 static int nfc_shdlc_complete_target_discovered(struct nfc_hci_dev
*hdev
,
793 struct nfc_target
*target
)
795 struct nfc_shdlc
*shdlc
= nfc_hci_get_clientdata(hdev
);
799 if (shdlc
->ops
->complete_target_discovered
)
800 return shdlc
->ops
->complete_target_discovered(shdlc
, gate
,
806 static int nfc_shdlc_data_exchange(struct nfc_hci_dev
*hdev
,
807 struct nfc_target
*target
,
809 struct sk_buff
**res_skb
)
811 struct nfc_shdlc
*shdlc
= nfc_hci_get_clientdata(hdev
);
813 if (shdlc
->ops
->data_exchange
)
814 return shdlc
->ops
->data_exchange(shdlc
, target
, skb
, res_skb
);
819 static struct nfc_hci_ops shdlc_ops
= {
820 .open
= nfc_shdlc_open
,
821 .close
= nfc_shdlc_close
,
822 .hci_ready
= nfc_shdlc_hci_ready
,
823 .xmit
= nfc_shdlc_xmit
,
824 .start_poll
= nfc_shdlc_start_poll
,
825 .target_from_gate
= nfc_shdlc_target_from_gate
,
826 .complete_target_discovered
= nfc_shdlc_complete_target_discovered
,
827 .data_exchange
= nfc_shdlc_data_exchange
,
830 struct nfc_shdlc
*nfc_shdlc_allocate(struct nfc_shdlc_ops
*ops
,
831 struct nfc_hci_init_data
*init_data
,
833 int tx_headroom
, int tx_tailroom
,
834 int max_link_payload
, const char *devname
)
836 struct nfc_shdlc
*shdlc
;
840 if (ops
->xmit
== NULL
)
843 shdlc
= kzalloc(sizeof(struct nfc_shdlc
), GFP_KERNEL
);
847 mutex_init(&shdlc
->state_mutex
);
849 shdlc
->state
= SHDLC_DISCONNECTED
;
851 init_timer(&shdlc
->connect_timer
);
852 shdlc
->connect_timer
.data
= (unsigned long)shdlc
;
853 shdlc
->connect_timer
.function
= nfc_shdlc_connect_timeout
;
855 init_timer(&shdlc
->t1_timer
);
856 shdlc
->t1_timer
.data
= (unsigned long)shdlc
;
857 shdlc
->t1_timer
.function
= nfc_shdlc_t1_timeout
;
859 init_timer(&shdlc
->t2_timer
);
860 shdlc
->t2_timer
.data
= (unsigned long)shdlc
;
861 shdlc
->t2_timer
.function
= nfc_shdlc_t2_timeout
;
863 shdlc
->w
= SHDLC_MAX_WINDOW
;
864 shdlc
->srej_support
= SHDLC_SREJ_SUPPORT
;
866 skb_queue_head_init(&shdlc
->rcv_q
);
867 skb_queue_head_init(&shdlc
->send_q
);
868 skb_queue_head_init(&shdlc
->ack_pending_q
);
870 INIT_WORK(&shdlc
->sm_work
, nfc_shdlc_sm_work
);
871 snprintf(name
, sizeof(name
), "%s_shdlc_sm_wq", devname
);
872 shdlc
->sm_wq
= alloc_workqueue(name
, WQ_NON_REENTRANT
| WQ_UNBOUND
|
874 if (shdlc
->sm_wq
== NULL
)
877 shdlc
->client_headroom
= tx_headroom
;
878 shdlc
->client_tailroom
= tx_tailroom
;
880 shdlc
->hdev
= nfc_hci_allocate_device(&shdlc_ops
, init_data
, protocols
,
881 tx_headroom
+ SHDLC_LLC_HEAD_ROOM
,
882 tx_tailroom
+ SHDLC_LLC_TAIL_ROOM
,
884 if (shdlc
->hdev
== NULL
)
887 nfc_hci_set_clientdata(shdlc
->hdev
, shdlc
);
889 r
= nfc_hci_register_device(shdlc
->hdev
);
896 nfc_hci_free_device(shdlc
->hdev
);
899 destroy_workqueue(shdlc
->sm_wq
);
906 EXPORT_SYMBOL(nfc_shdlc_allocate
);
908 void nfc_shdlc_free(struct nfc_shdlc
*shdlc
)
912 /* TODO: Check that this cannot be called while still in use */
914 nfc_hci_unregister_device(shdlc
->hdev
);
915 nfc_hci_free_device(shdlc
->hdev
);
917 destroy_workqueue(shdlc
->sm_wq
);
919 skb_queue_purge(&shdlc
->rcv_q
);
920 skb_queue_purge(&shdlc
->send_q
);
921 skb_queue_purge(&shdlc
->ack_pending_q
);
925 EXPORT_SYMBOL(nfc_shdlc_free
);
927 void nfc_shdlc_set_clientdata(struct nfc_shdlc
*shdlc
, void *clientdata
)
931 shdlc
->clientdata
= clientdata
;
933 EXPORT_SYMBOL(nfc_shdlc_set_clientdata
);
935 void *nfc_shdlc_get_clientdata(struct nfc_shdlc
*shdlc
)
937 return shdlc
->clientdata
;
939 EXPORT_SYMBOL(nfc_shdlc_get_clientdata
);
941 struct nfc_hci_dev
*nfc_shdlc_get_hci_dev(struct nfc_shdlc
*shdlc
)
945 EXPORT_SYMBOL(nfc_shdlc_get_hci_dev
);