Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Low Level Transport (NDLC) Driver for STMicroelectronics NFC Chip |
| 3 | * |
| 4 | * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/sched.h> |
| 20 | #include <net/nfc/nci_core.h> |
| 21 | |
| 22 | #include "ndlc.h" |
| 23 | #include "st21nfcb.h" |
| 24 | |
| 25 | #define NDLC_TIMER_T1 100 |
| 26 | #define NDLC_TIMER_T1_WAIT 400 |
| 27 | #define NDLC_TIMER_T2 1200 |
| 28 | |
| 29 | #define PCB_TYPE_DATAFRAME 0x80 |
| 30 | #define PCB_TYPE_SUPERVISOR 0xc0 |
| 31 | #define PCB_TYPE_MASK PCB_TYPE_SUPERVISOR |
| 32 | |
| 33 | #define PCB_SYNC_ACK 0x20 |
| 34 | #define PCB_SYNC_NACK 0x10 |
| 35 | #define PCB_SYNC_WAIT 0x30 |
| 36 | #define PCB_SYNC_NOINFO 0x00 |
| 37 | #define PCB_SYNC_MASK PCB_SYNC_WAIT |
| 38 | |
| 39 | #define PCB_DATAFRAME_RETRANSMIT_YES 0x00 |
| 40 | #define PCB_DATAFRAME_RETRANSMIT_NO 0x04 |
| 41 | #define PCB_DATAFRAME_RETRANSMIT_MASK PCB_DATAFRAME_RETRANSMIT_NO |
| 42 | |
| 43 | #define PCB_SUPERVISOR_RETRANSMIT_YES 0x00 |
| 44 | #define PCB_SUPERVISOR_RETRANSMIT_NO 0x02 |
| 45 | #define PCB_SUPERVISOR_RETRANSMIT_MASK PCB_SUPERVISOR_RETRANSMIT_NO |
| 46 | |
| 47 | #define PCB_FRAME_CRC_INFO_PRESENT 0x08 |
| 48 | #define PCB_FRAME_CRC_INFO_NOTPRESENT 0x00 |
| 49 | #define PCB_FRAME_CRC_INFO_MASK PCB_FRAME_CRC_INFO_PRESENT |
| 50 | |
| 51 | #define NDLC_DUMP_SKB(info, skb) \ |
| 52 | do { \ |
| 53 | pr_debug("%s:\n", info); \ |
| 54 | print_hex_dump(KERN_DEBUG, "ndlc: ", DUMP_PREFIX_OFFSET, \ |
| 55 | 16, 1, skb->data, skb->len, 0); \ |
| 56 | } while (0) |
| 57 | |
| 58 | int ndlc_open(struct llt_ndlc *ndlc) |
| 59 | { |
| 60 | /* toggle reset pin */ |
| 61 | ndlc->ops->enable(ndlc->phy_id); |
| 62 | return 0; |
| 63 | } |
| 64 | EXPORT_SYMBOL(ndlc_open); |
| 65 | |
| 66 | void ndlc_close(struct llt_ndlc *ndlc) |
| 67 | { |
| 68 | /* toggle reset pin */ |
| 69 | ndlc->ops->disable(ndlc->phy_id); |
| 70 | } |
| 71 | EXPORT_SYMBOL(ndlc_close); |
| 72 | |
| 73 | int ndlc_send(struct llt_ndlc *ndlc, struct sk_buff *skb) |
| 74 | { |
| 75 | /* add ndlc header */ |
| 76 | u8 pcb = PCB_TYPE_DATAFRAME | PCB_DATAFRAME_RETRANSMIT_NO | |
| 77 | PCB_FRAME_CRC_INFO_NOTPRESENT; |
| 78 | |
| 79 | *skb_push(skb, 1) = pcb; |
| 80 | skb_queue_tail(&ndlc->send_q, skb); |
| 81 | |
| 82 | schedule_work(&ndlc->sm_work); |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | EXPORT_SYMBOL(ndlc_send); |
| 87 | |
| 88 | static void llt_ndlc_send_queue(struct llt_ndlc *ndlc) |
| 89 | { |
| 90 | struct sk_buff *skb; |
| 91 | int r; |
| 92 | unsigned long time_sent; |
| 93 | |
| 94 | if (ndlc->send_q.qlen) |
| 95 | pr_debug("sendQlen=%d unackQlen=%d\n", |
| 96 | ndlc->send_q.qlen, ndlc->ack_pending_q.qlen); |
| 97 | |
| 98 | while (ndlc->send_q.qlen) { |
| 99 | skb = skb_dequeue(&ndlc->send_q); |
| 100 | NDLC_DUMP_SKB("ndlc frame written", skb); |
| 101 | r = ndlc->ops->write(ndlc->phy_id, skb); |
| 102 | if (r < 0) { |
| 103 | ndlc->hard_fault = r; |
| 104 | break; |
| 105 | } |
| 106 | time_sent = jiffies; |
| 107 | *(unsigned long *)skb->cb = time_sent; |
| 108 | |
| 109 | skb_queue_tail(&ndlc->ack_pending_q, skb); |
| 110 | |
| 111 | /* start timer t1 for ndlc aknowledge */ |
| 112 | ndlc->t1_active = true; |
| 113 | mod_timer(&ndlc->t1_timer, time_sent + |
| 114 | msecs_to_jiffies(NDLC_TIMER_T1)); |
Christophe Ricard | fa0daa0 | 2014-09-13 10:28:48 +0200 | [diff] [blame] | 115 | /* start timer t2 for chip availability */ |
| 116 | ndlc->t2_active = true; |
| 117 | mod_timer(&ndlc->t2_timer, time_sent + |
| 118 | msecs_to_jiffies(NDLC_TIMER_T2)); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | static void llt_ndlc_requeue_data_pending(struct llt_ndlc *ndlc) |
| 123 | { |
| 124 | struct sk_buff *skb; |
| 125 | u8 pcb; |
| 126 | |
| 127 | while ((skb = skb_dequeue_tail(&ndlc->ack_pending_q))) { |
| 128 | pcb = skb->data[0]; |
| 129 | switch (pcb & PCB_TYPE_MASK) { |
| 130 | case PCB_TYPE_SUPERVISOR: |
| 131 | skb->data[0] = (pcb & ~PCB_SUPERVISOR_RETRANSMIT_MASK) | |
| 132 | PCB_SUPERVISOR_RETRANSMIT_YES; |
| 133 | break; |
| 134 | case PCB_TYPE_DATAFRAME: |
| 135 | skb->data[0] = (pcb & ~PCB_DATAFRAME_RETRANSMIT_MASK) | |
| 136 | PCB_DATAFRAME_RETRANSMIT_YES; |
| 137 | break; |
| 138 | default: |
| 139 | pr_err("UNKNOWN Packet Control Byte=%d\n", pcb); |
| 140 | kfree_skb(skb); |
Christophe Ricard | 2084246 | 2015-01-25 23:33:23 +0100 | [diff] [blame] | 141 | continue; |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 142 | } |
| 143 | skb_queue_head(&ndlc->send_q, skb); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static void llt_ndlc_rcv_queue(struct llt_ndlc *ndlc) |
| 148 | { |
| 149 | struct sk_buff *skb; |
| 150 | u8 pcb; |
| 151 | unsigned long time_sent; |
| 152 | |
| 153 | if (ndlc->rcv_q.qlen) |
| 154 | pr_debug("rcvQlen=%d\n", ndlc->rcv_q.qlen); |
| 155 | |
| 156 | while ((skb = skb_dequeue(&ndlc->rcv_q)) != NULL) { |
| 157 | pcb = skb->data[0]; |
| 158 | skb_pull(skb, 1); |
| 159 | if ((pcb & PCB_TYPE_MASK) == PCB_TYPE_SUPERVISOR) { |
| 160 | switch (pcb & PCB_SYNC_MASK) { |
| 161 | case PCB_SYNC_ACK: |
| 162 | del_timer_sync(&ndlc->t1_timer); |
| 163 | del_timer_sync(&ndlc->t2_timer); |
| 164 | ndlc->t2_active = false; |
| 165 | ndlc->t1_active = false; |
| 166 | break; |
| 167 | case PCB_SYNC_NACK: |
| 168 | llt_ndlc_requeue_data_pending(ndlc); |
| 169 | llt_ndlc_send_queue(ndlc); |
| 170 | /* start timer t1 for ndlc aknowledge */ |
| 171 | time_sent = jiffies; |
| 172 | ndlc->t1_active = true; |
| 173 | mod_timer(&ndlc->t1_timer, time_sent + |
| 174 | msecs_to_jiffies(NDLC_TIMER_T1)); |
| 175 | break; |
| 176 | case PCB_SYNC_WAIT: |
| 177 | time_sent = jiffies; |
| 178 | ndlc->t1_active = true; |
| 179 | mod_timer(&ndlc->t1_timer, time_sent + |
| 180 | msecs_to_jiffies(NDLC_TIMER_T1_WAIT)); |
| 181 | break; |
| 182 | default: |
| 183 | pr_err("UNKNOWN Packet Control Byte=%d\n", pcb); |
| 184 | kfree_skb(skb); |
| 185 | break; |
| 186 | } |
| 187 | } else { |
| 188 | nci_recv_frame(ndlc->ndev, skb); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | static void llt_ndlc_sm_work(struct work_struct *work) |
| 194 | { |
| 195 | struct llt_ndlc *ndlc = container_of(work, struct llt_ndlc, sm_work); |
| 196 | |
| 197 | llt_ndlc_send_queue(ndlc); |
| 198 | llt_ndlc_rcv_queue(ndlc); |
| 199 | |
| 200 | if (ndlc->t1_active && timer_pending(&ndlc->t1_timer) == 0) { |
| 201 | pr_debug |
| 202 | ("Handle T1(recv SUPERVISOR) elapsed (T1 now inactive)\n"); |
| 203 | ndlc->t1_active = false; |
| 204 | |
| 205 | llt_ndlc_requeue_data_pending(ndlc); |
| 206 | llt_ndlc_send_queue(ndlc); |
| 207 | } |
| 208 | |
| 209 | if (ndlc->t2_active && timer_pending(&ndlc->t2_timer) == 0) { |
| 210 | pr_debug("Handle T2(recv DATA) elapsed (T2 now inactive)\n"); |
| 211 | ndlc->t2_active = false; |
| 212 | ndlc->t1_active = false; |
| 213 | del_timer_sync(&ndlc->t1_timer); |
Christophe Ricard | fa0daa0 | 2014-09-13 10:28:48 +0200 | [diff] [blame] | 214 | del_timer_sync(&ndlc->t2_timer); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 215 | ndlc_close(ndlc); |
| 216 | ndlc->hard_fault = -EREMOTEIO; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | void ndlc_recv(struct llt_ndlc *ndlc, struct sk_buff *skb) |
| 221 | { |
| 222 | if (skb == NULL) { |
| 223 | pr_err("NULL Frame -> link is dead\n"); |
| 224 | ndlc->hard_fault = -EREMOTEIO; |
| 225 | ndlc_close(ndlc); |
| 226 | } else { |
| 227 | NDLC_DUMP_SKB("incoming frame", skb); |
| 228 | skb_queue_tail(&ndlc->rcv_q, skb); |
| 229 | } |
| 230 | |
| 231 | schedule_work(&ndlc->sm_work); |
| 232 | } |
| 233 | EXPORT_SYMBOL(ndlc_recv); |
| 234 | |
| 235 | static void ndlc_t1_timeout(unsigned long data) |
| 236 | { |
| 237 | struct llt_ndlc *ndlc = (struct llt_ndlc *)data; |
| 238 | |
| 239 | pr_debug("\n"); |
| 240 | |
| 241 | schedule_work(&ndlc->sm_work); |
| 242 | } |
| 243 | |
| 244 | static void ndlc_t2_timeout(unsigned long data) |
| 245 | { |
| 246 | struct llt_ndlc *ndlc = (struct llt_ndlc *)data; |
| 247 | |
| 248 | pr_debug("\n"); |
| 249 | |
| 250 | schedule_work(&ndlc->sm_work); |
| 251 | } |
| 252 | |
| 253 | int ndlc_probe(void *phy_id, struct nfc_phy_ops *phy_ops, struct device *dev, |
| 254 | int phy_headroom, int phy_tailroom, struct llt_ndlc **ndlc_id) |
| 255 | { |
| 256 | struct llt_ndlc *ndlc; |
| 257 | |
| 258 | ndlc = devm_kzalloc(dev, sizeof(struct llt_ndlc), GFP_KERNEL); |
| 259 | if (!ndlc) { |
| 260 | nfc_err(dev, "Cannot allocate memory for ndlc.\n"); |
| 261 | return -ENOMEM; |
| 262 | } |
| 263 | ndlc->ops = phy_ops; |
| 264 | ndlc->phy_id = phy_id; |
| 265 | ndlc->dev = dev; |
| 266 | |
| 267 | *ndlc_id = ndlc; |
| 268 | |
Christophe Ricard | ace9183 | 2014-11-13 00:30:40 +0100 | [diff] [blame] | 269 | /* initialize timers */ |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 270 | init_timer(&ndlc->t1_timer); |
| 271 | ndlc->t1_timer.data = (unsigned long)ndlc; |
| 272 | ndlc->t1_timer.function = ndlc_t1_timeout; |
| 273 | |
| 274 | init_timer(&ndlc->t2_timer); |
| 275 | ndlc->t2_timer.data = (unsigned long)ndlc; |
| 276 | ndlc->t2_timer.function = ndlc_t2_timeout; |
| 277 | |
| 278 | skb_queue_head_init(&ndlc->rcv_q); |
| 279 | skb_queue_head_init(&ndlc->send_q); |
| 280 | skb_queue_head_init(&ndlc->ack_pending_q); |
| 281 | |
| 282 | INIT_WORK(&ndlc->sm_work, llt_ndlc_sm_work); |
| 283 | |
| 284 | return st21nfcb_nci_probe(ndlc, phy_headroom, phy_tailroom); |
| 285 | } |
| 286 | EXPORT_SYMBOL(ndlc_probe); |
| 287 | |
| 288 | void ndlc_remove(struct llt_ndlc *ndlc) |
| 289 | { |
| 290 | /* cancel timers */ |
| 291 | del_timer_sync(&ndlc->t1_timer); |
| 292 | del_timer_sync(&ndlc->t2_timer); |
| 293 | ndlc->t2_active = false; |
| 294 | ndlc->t1_active = false; |
| 295 | |
| 296 | skb_queue_purge(&ndlc->rcv_q); |
| 297 | skb_queue_purge(&ndlc->send_q); |
| 298 | |
| 299 | st21nfcb_nci_remove(ndlc->ndev); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 300 | } |
| 301 | EXPORT_SYMBOL(ndlc_remove); |