1 /* 2 * HCI based Driver for Inside Secure microread NFC Chip 3 * 4 * Copyright (C) 2013 Intel Corporation. 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, write to the 17 * Free Software Foundation, Inc., 18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19 */ 20 21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 22 23 #include <linux/module.h> 24 #include <linux/delay.h> 25 #include <linux/slab.h> 26 #include <linux/crc-ccitt.h> 27 28 #include <linux/nfc.h> 29 #include <net/nfc/nfc.h> 30 #include <net/nfc/hci.h> 31 #include <net/nfc/llc.h> 32 33 #include "microread.h" 34 35 /* Proprietary gates, events, commands and registers */ 36 /* Admin */ 37 #define MICROREAD_GATE_ID_ADM NFC_HCI_ADMIN_GATE 38 #define MICROREAD_GATE_ID_MGT 0x01 39 #define MICROREAD_GATE_ID_OS 0x02 40 #define MICROREAD_GATE_ID_TESTRF 0x03 41 #define MICROREAD_GATE_ID_LOOPBACK NFC_HCI_LOOPBACK_GATE 42 #define MICROREAD_GATE_ID_IDT NFC_HCI_ID_MGMT_GATE 43 #define MICROREAD_GATE_ID_LMS NFC_HCI_LINK_MGMT_GATE 44 45 /* Reader */ 46 #define MICROREAD_GATE_ID_MREAD_GEN 0x10 47 #define MICROREAD_GATE_ID_MREAD_ISO_B NFC_HCI_RF_READER_B_GATE 48 #define MICROREAD_GATE_ID_MREAD_NFC_T1 0x12 49 #define MICROREAD_GATE_ID_MREAD_ISO_A NFC_HCI_RF_READER_A_GATE 50 #define MICROREAD_GATE_ID_MREAD_NFC_T3 0x14 51 #define MICROREAD_GATE_ID_MREAD_ISO_15_3 0x15 52 #define MICROREAD_GATE_ID_MREAD_ISO_15_2 0x16 53 #define MICROREAD_GATE_ID_MREAD_ISO_B_3 0x17 54 #define MICROREAD_GATE_ID_MREAD_BPRIME 0x18 55 #define MICROREAD_GATE_ID_MREAD_ISO_A_3 0x19 56 57 /* Card */ 58 #define MICROREAD_GATE_ID_MCARD_GEN 0x20 59 #define MICROREAD_GATE_ID_MCARD_ISO_B 0x21 60 #define MICROREAD_GATE_ID_MCARD_BPRIME 0x22 61 #define MICROREAD_GATE_ID_MCARD_ISO_A 0x23 62 #define MICROREAD_GATE_ID_MCARD_NFC_T3 0x24 63 #define MICROREAD_GATE_ID_MCARD_ISO_15_3 0x25 64 #define MICROREAD_GATE_ID_MCARD_ISO_15_2 0x26 65 #define MICROREAD_GATE_ID_MCARD_ISO_B_2 0x27 66 #define MICROREAD_GATE_ID_MCARD_ISO_CUSTOM 0x28 67 #define MICROREAD_GATE_ID_SECURE_ELEMENT 0x2F 68 69 /* P2P */ 70 #define MICROREAD_GATE_ID_P2P_GEN 0x30 71 #define MICROREAD_GATE_ID_P2P_TARGET 0x31 72 #define MICROREAD_PAR_P2P_TARGET_MODE 0x01 73 #define MICROREAD_PAR_P2P_TARGET_GT 0x04 74 #define MICROREAD_GATE_ID_P2P_INITIATOR 0x32 75 #define MICROREAD_PAR_P2P_INITIATOR_GI 0x01 76 #define MICROREAD_PAR_P2P_INITIATOR_GT 0x03 77 78 /* Those pipes are created/opened by default in the chip */ 79 #define MICROREAD_PIPE_ID_LMS 0x00 80 #define MICROREAD_PIPE_ID_ADMIN 0x01 81 #define MICROREAD_PIPE_ID_MGT 0x02 82 #define MICROREAD_PIPE_ID_OS 0x03 83 #define MICROREAD_PIPE_ID_HDS_LOOPBACK 0x04 84 #define MICROREAD_PIPE_ID_HDS_IDT 0x05 85 #define MICROREAD_PIPE_ID_HDS_MCARD_ISO_B 0x08 86 #define MICROREAD_PIPE_ID_HDS_MCARD_ISO_BPRIME 0x09 87 #define MICROREAD_PIPE_ID_HDS_MCARD_ISO_A 0x0A 88 #define MICROREAD_PIPE_ID_HDS_MCARD_ISO_15_3 0x0B 89 #define MICROREAD_PIPE_ID_HDS_MCARD_ISO_15_2 0x0C 90 #define MICROREAD_PIPE_ID_HDS_MCARD_NFC_T3 0x0D 91 #define MICROREAD_PIPE_ID_HDS_MCARD_ISO_B_2 0x0E 92 #define MICROREAD_PIPE_ID_HDS_MCARD_CUSTOM 0x0F 93 #define MICROREAD_PIPE_ID_HDS_MREAD_ISO_B 0x10 94 #define MICROREAD_PIPE_ID_HDS_MREAD_NFC_T1 0x11 95 #define MICROREAD_PIPE_ID_HDS_MREAD_ISO_A 0x12 96 #define MICROREAD_PIPE_ID_HDS_MREAD_ISO_15_3 0x13 97 #define MICROREAD_PIPE_ID_HDS_MREAD_ISO_15_2 0x14 98 #define MICROREAD_PIPE_ID_HDS_MREAD_NFC_T3 0x15 99 #define MICROREAD_PIPE_ID_HDS_MREAD_ISO_B_3 0x16 100 #define MICROREAD_PIPE_ID_HDS_MREAD_BPRIME 0x17 101 #define MICROREAD_PIPE_ID_HDS_MREAD_ISO_A_3 0x18 102 #define MICROREAD_PIPE_ID_HDS_MREAD_GEN 0x1B 103 #define MICROREAD_PIPE_ID_HDS_STACKED_ELEMENT 0x1C 104 #define MICROREAD_PIPE_ID_HDS_INSTANCES 0x1D 105 #define MICROREAD_PIPE_ID_HDS_TESTRF 0x1E 106 #define MICROREAD_PIPE_ID_HDS_P2P_TARGET 0x1F 107 #define MICROREAD_PIPE_ID_HDS_P2P_INITIATOR 0x20 108 109 /* Events */ 110 #define MICROREAD_EVT_MREAD_DISCOVERY_OCCURED NFC_HCI_EVT_TARGET_DISCOVERED 111 #define MICROREAD_EVT_MREAD_CARD_FOUND 0x3D 112 #define MICROREAD_EMCF_A_ATQA 0 113 #define MICROREAD_EMCF_A_SAK 2 114 #define MICROREAD_EMCF_A_LEN 3 115 #define MICROREAD_EMCF_A_UID 4 116 #define MICROREAD_EMCF_A3_ATQA 0 117 #define MICROREAD_EMCF_A3_SAK 2 118 #define MICROREAD_EMCF_A3_LEN 3 119 #define MICROREAD_EMCF_A3_UID 4 120 #define MICROREAD_EMCF_B_UID 0 121 #define MICROREAD_EMCF_T1_ATQA 0 122 #define MICROREAD_EMCF_T1_UID 4 123 #define MICROREAD_EMCF_T3_UID 0 124 #define MICROREAD_EVT_MREAD_DISCOVERY_START NFC_HCI_EVT_READER_REQUESTED 125 #define MICROREAD_EVT_MREAD_DISCOVERY_START_SOME 0x3E 126 #define MICROREAD_EVT_MREAD_DISCOVERY_STOP NFC_HCI_EVT_END_OPERATION 127 #define MICROREAD_EVT_MREAD_SIM_REQUESTS 0x3F 128 #define MICROREAD_EVT_MCARD_EXCHANGE NFC_HCI_EVT_TARGET_DISCOVERED 129 #define MICROREAD_EVT_P2P_INITIATOR_EXCHANGE_TO_RF 0x20 130 #define MICROREAD_EVT_P2P_INITIATOR_EXCHANGE_FROM_RF 0x21 131 #define MICROREAD_EVT_MCARD_FIELD_ON 0x11 132 #define MICROREAD_EVT_P2P_TARGET_ACTIVATED 0x13 133 #define MICROREAD_EVT_P2P_TARGET_DEACTIVATED 0x12 134 #define MICROREAD_EVT_MCARD_FIELD_OFF 0x14 135 136 /* Commands */ 137 #define MICROREAD_CMD_MREAD_EXCHANGE 0x10 138 #define MICROREAD_CMD_MREAD_SUBSCRIBE 0x3F 139 140 /* Hosts IDs */ 141 #define MICROREAD_ELT_ID_HDS NFC_HCI_TERMINAL_HOST_ID 142 #define MICROREAD_ELT_ID_SIM NFC_HCI_UICC_HOST_ID 143 #define MICROREAD_ELT_ID_SE1 0x03 144 #define MICROREAD_ELT_ID_SE2 0x04 145 #define MICROREAD_ELT_ID_SE3 0x05 146 147 static struct nfc_hci_gate microread_gates[] = { 148 {MICROREAD_GATE_ID_ADM, MICROREAD_PIPE_ID_ADMIN}, 149 {MICROREAD_GATE_ID_LOOPBACK, MICROREAD_PIPE_ID_HDS_LOOPBACK}, 150 {MICROREAD_GATE_ID_IDT, MICROREAD_PIPE_ID_HDS_IDT}, 151 {MICROREAD_GATE_ID_LMS, MICROREAD_PIPE_ID_LMS}, 152 {MICROREAD_GATE_ID_MREAD_ISO_B, MICROREAD_PIPE_ID_HDS_MREAD_ISO_B}, 153 {MICROREAD_GATE_ID_MREAD_ISO_A, MICROREAD_PIPE_ID_HDS_MREAD_ISO_A}, 154 {MICROREAD_GATE_ID_MREAD_ISO_A_3, MICROREAD_PIPE_ID_HDS_MREAD_ISO_A_3}, 155 {MICROREAD_GATE_ID_MGT, MICROREAD_PIPE_ID_MGT}, 156 {MICROREAD_GATE_ID_OS, MICROREAD_PIPE_ID_OS}, 157 {MICROREAD_GATE_ID_MREAD_NFC_T1, MICROREAD_PIPE_ID_HDS_MREAD_NFC_T1}, 158 {MICROREAD_GATE_ID_MREAD_NFC_T3, MICROREAD_PIPE_ID_HDS_MREAD_NFC_T3}, 159 {MICROREAD_GATE_ID_P2P_TARGET, MICROREAD_PIPE_ID_HDS_P2P_TARGET}, 160 {MICROREAD_GATE_ID_P2P_INITIATOR, MICROREAD_PIPE_ID_HDS_P2P_INITIATOR} 161 }; 162 163 /* Largest headroom needed for outgoing custom commands */ 164 #define MICROREAD_CMDS_HEADROOM 2 165 #define MICROREAD_CMD_TAILROOM 2 166 167 struct microread_info { 168 struct nfc_phy_ops *phy_ops; 169 void *phy_id; 170 171 struct nfc_hci_dev *hdev; 172 173 int async_cb_type; 174 data_exchange_cb_t async_cb; 175 void *async_cb_context; 176 }; 177 178 static int microread_open(struct nfc_hci_dev *hdev) 179 { 180 struct microread_info *info = nfc_hci_get_clientdata(hdev); 181 182 return info->phy_ops->enable(info->phy_id); 183 } 184 185 static void microread_close(struct nfc_hci_dev *hdev) 186 { 187 struct microread_info *info = nfc_hci_get_clientdata(hdev); 188 189 info->phy_ops->disable(info->phy_id); 190 } 191 192 static int microread_hci_ready(struct nfc_hci_dev *hdev) 193 { 194 int r; 195 u8 param[4]; 196 197 param[0] = 0x03; 198 r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_ISO_A, 199 MICROREAD_CMD_MREAD_SUBSCRIBE, param, 1, NULL); 200 if (r) 201 return r; 202 203 r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_ISO_A_3, 204 MICROREAD_CMD_MREAD_SUBSCRIBE, NULL, 0, NULL); 205 if (r) 206 return r; 207 208 param[0] = 0x00; 209 param[1] = 0x03; 210 param[2] = 0x00; 211 r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_ISO_B, 212 MICROREAD_CMD_MREAD_SUBSCRIBE, param, 3, NULL); 213 if (r) 214 return r; 215 216 r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_NFC_T1, 217 MICROREAD_CMD_MREAD_SUBSCRIBE, NULL, 0, NULL); 218 if (r) 219 return r; 220 221 param[0] = 0xFF; 222 param[1] = 0xFF; 223 param[2] = 0x00; 224 param[3] = 0x00; 225 r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_NFC_T3, 226 MICROREAD_CMD_MREAD_SUBSCRIBE, param, 4, NULL); 227 228 return r; 229 } 230 231 static int microread_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb) 232 { 233 struct microread_info *info = nfc_hci_get_clientdata(hdev); 234 235 return info->phy_ops->write(info->phy_id, skb); 236 } 237 238 static int microread_start_poll(struct nfc_hci_dev *hdev, 239 u32 im_protocols, u32 tm_protocols) 240 { 241 int r; 242 243 u8 param[2]; 244 u8 mode; 245 246 param[0] = 0x00; 247 param[1] = 0x00; 248 249 if (im_protocols & NFC_PROTO_ISO14443_MASK) 250 param[0] |= (1 << 2); 251 252 if (im_protocols & NFC_PROTO_ISO14443_B_MASK) 253 param[0] |= 1; 254 255 if (im_protocols & NFC_PROTO_MIFARE_MASK) 256 param[1] |= 1; 257 258 if (im_protocols & NFC_PROTO_JEWEL_MASK) 259 param[0] |= (1 << 1); 260 261 if (im_protocols & NFC_PROTO_FELICA_MASK) 262 param[0] |= (1 << 5); 263 264 if (im_protocols & NFC_PROTO_NFC_DEP_MASK) 265 param[1] |= (1 << 1); 266 267 if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) { 268 hdev->gb = nfc_get_local_general_bytes(hdev->ndev, 269 &hdev->gb_len); 270 if (hdev->gb == NULL || hdev->gb_len == 0) { 271 im_protocols &= ~NFC_PROTO_NFC_DEP_MASK; 272 tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK; 273 } 274 } 275 276 r = nfc_hci_send_event(hdev, MICROREAD_GATE_ID_MREAD_ISO_A, 277 MICROREAD_EVT_MREAD_DISCOVERY_STOP, NULL, 0); 278 if (r) 279 return r; 280 281 mode = 0xff; 282 r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_TARGET, 283 MICROREAD_PAR_P2P_TARGET_MODE, &mode, 1); 284 if (r) 285 return r; 286 287 if (im_protocols & NFC_PROTO_NFC_DEP_MASK) { 288 r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_INITIATOR, 289 MICROREAD_PAR_P2P_INITIATOR_GI, 290 hdev->gb, hdev->gb_len); 291 if (r) 292 return r; 293 } 294 295 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) { 296 r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_TARGET, 297 MICROREAD_PAR_P2P_TARGET_GT, 298 hdev->gb, hdev->gb_len); 299 if (r) 300 return r; 301 302 mode = 0x02; 303 r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_TARGET, 304 MICROREAD_PAR_P2P_TARGET_MODE, &mode, 1); 305 if (r) 306 return r; 307 } 308 309 return nfc_hci_send_event(hdev, MICROREAD_GATE_ID_MREAD_ISO_A, 310 MICROREAD_EVT_MREAD_DISCOVERY_START_SOME, 311 param, 2); 312 } 313 314 static int microread_dep_link_up(struct nfc_hci_dev *hdev, 315 struct nfc_target *target, u8 comm_mode, 316 u8 *gb, size_t gb_len) 317 { 318 struct sk_buff *rgb_skb = NULL; 319 int r; 320 321 r = nfc_hci_get_param(hdev, target->hci_reader_gate, 322 MICROREAD_PAR_P2P_INITIATOR_GT, &rgb_skb); 323 if (r < 0) 324 return r; 325 326 if (rgb_skb->len == 0 || rgb_skb->len > NFC_GB_MAXSIZE) { 327 r = -EPROTO; 328 goto exit; 329 } 330 331 r = nfc_set_remote_general_bytes(hdev->ndev, rgb_skb->data, 332 rgb_skb->len); 333 if (r == 0) 334 r = nfc_dep_link_is_up(hdev->ndev, target->idx, comm_mode, 335 NFC_RF_INITIATOR); 336 exit: 337 kfree_skb(rgb_skb); 338 339 return r; 340 } 341 342 static int microread_dep_link_down(struct nfc_hci_dev *hdev) 343 { 344 return nfc_hci_send_event(hdev, MICROREAD_GATE_ID_P2P_INITIATOR, 345 MICROREAD_EVT_MREAD_DISCOVERY_STOP, NULL, 0); 346 } 347 348 static int microread_target_from_gate(struct nfc_hci_dev *hdev, u8 gate, 349 struct nfc_target *target) 350 { 351 switch (gate) { 352 case MICROREAD_GATE_ID_P2P_INITIATOR: 353 target->supported_protocols = NFC_PROTO_NFC_DEP_MASK; 354 break; 355 default: 356 return -EPROTO; 357 } 358 359 return 0; 360 } 361 362 static int microread_complete_target_discovered(struct nfc_hci_dev *hdev, 363 u8 gate, 364 struct nfc_target *target) 365 { 366 return 0; 367 } 368 369 #define MICROREAD_CB_TYPE_READER_ALL 1 370 371 static void microread_im_transceive_cb(void *context, struct sk_buff *skb, 372 int err) 373 { 374 struct microread_info *info = context; 375 376 switch (info->async_cb_type) { 377 case MICROREAD_CB_TYPE_READER_ALL: 378 if (err == 0) { 379 if (skb->len == 0) { 380 err = -EPROTO; 381 kfree_skb(skb); 382 info->async_cb(info->async_cb_context, NULL, 383 -EPROTO); 384 return; 385 } 386 387 if (skb->data[skb->len - 1] != 0) { 388 err = nfc_hci_result_to_errno( 389 skb->data[skb->len - 1]); 390 kfree_skb(skb); 391 info->async_cb(info->async_cb_context, NULL, 392 err); 393 return; 394 } 395 396 skb_trim(skb, skb->len - 1); /* RF Error ind. */ 397 } 398 info->async_cb(info->async_cb_context, skb, err); 399 break; 400 default: 401 if (err == 0) 402 kfree_skb(skb); 403 break; 404 } 405 } 406 407 /* 408 * Returns: 409 * <= 0: driver handled the data exchange 410 * 1: driver doesn't especially handle, please do standard processing 411 */ 412 static int microread_im_transceive(struct nfc_hci_dev *hdev, 413 struct nfc_target *target, 414 struct sk_buff *skb, data_exchange_cb_t cb, 415 void *cb_context) 416 { 417 struct microread_info *info = nfc_hci_get_clientdata(hdev); 418 u8 control_bits; 419 u16 crc; 420 421 pr_info("data exchange to gate 0x%x\n", target->hci_reader_gate); 422 423 if (target->hci_reader_gate == MICROREAD_GATE_ID_P2P_INITIATOR) { 424 *skb_push(skb, 1) = 0; 425 426 return nfc_hci_send_event(hdev, target->hci_reader_gate, 427 MICROREAD_EVT_P2P_INITIATOR_EXCHANGE_TO_RF, 428 skb->data, skb->len); 429 } 430 431 switch (target->hci_reader_gate) { 432 case MICROREAD_GATE_ID_MREAD_ISO_A: 433 control_bits = 0xCB; 434 break; 435 case MICROREAD_GATE_ID_MREAD_ISO_A_3: 436 control_bits = 0xCB; 437 break; 438 case MICROREAD_GATE_ID_MREAD_ISO_B: 439 control_bits = 0xCB; 440 break; 441 case MICROREAD_GATE_ID_MREAD_NFC_T1: 442 control_bits = 0x1B; 443 444 crc = crc_ccitt(0xffff, skb->data, skb->len); 445 crc = ~crc; 446 *skb_put(skb, 1) = crc & 0xff; 447 *skb_put(skb, 1) = crc >> 8; 448 break; 449 case MICROREAD_GATE_ID_MREAD_NFC_T3: 450 control_bits = 0xDB; 451 break; 452 default: 453 pr_info("Abort im_transceive to invalid gate 0x%x\n", 454 target->hci_reader_gate); 455 return 1; 456 } 457 458 *skb_push(skb, 1) = control_bits; 459 460 info->async_cb_type = MICROREAD_CB_TYPE_READER_ALL; 461 info->async_cb = cb; 462 info->async_cb_context = cb_context; 463 464 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate, 465 MICROREAD_CMD_MREAD_EXCHANGE, 466 skb->data, skb->len, 467 microread_im_transceive_cb, info); 468 } 469 470 static int microread_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb) 471 { 472 int r; 473 474 r = nfc_hci_send_event(hdev, MICROREAD_GATE_ID_P2P_TARGET, 475 MICROREAD_EVT_MCARD_EXCHANGE, 476 skb->data, skb->len); 477 478 kfree_skb(skb); 479 480 return r; 481 } 482 483 static void microread_target_discovered(struct nfc_hci_dev *hdev, u8 gate, 484 struct sk_buff *skb) 485 { 486 struct nfc_target *targets; 487 int r = 0; 488 489 pr_info("target discovered to gate 0x%x\n", gate); 490 491 targets = kzalloc(sizeof(struct nfc_target), GFP_KERNEL); 492 if (targets == NULL) { 493 r = -ENOMEM; 494 goto exit; 495 } 496 497 targets->hci_reader_gate = gate; 498 499 switch (gate) { 500 case MICROREAD_GATE_ID_MREAD_ISO_A: 501 targets->supported_protocols = 502 nfc_hci_sak_to_protocol(skb->data[MICROREAD_EMCF_A_SAK]); 503 targets->sens_res = 504 be16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_A_ATQA]); 505 targets->sel_res = skb->data[MICROREAD_EMCF_A_SAK]; 506 memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_A_UID], 507 skb->data[MICROREAD_EMCF_A_LEN]); 508 targets->nfcid1_len = skb->data[MICROREAD_EMCF_A_LEN]; 509 break; 510 case MICROREAD_GATE_ID_MREAD_ISO_A_3: 511 targets->supported_protocols = 512 nfc_hci_sak_to_protocol(skb->data[MICROREAD_EMCF_A3_SAK]); 513 targets->sens_res = 514 be16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_A3_ATQA]); 515 targets->sel_res = skb->data[MICROREAD_EMCF_A3_SAK]; 516 memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_A3_UID], 517 skb->data[MICROREAD_EMCF_A3_LEN]); 518 targets->nfcid1_len = skb->data[MICROREAD_EMCF_A3_LEN]; 519 break; 520 case MICROREAD_GATE_ID_MREAD_ISO_B: 521 targets->supported_protocols = NFC_PROTO_ISO14443_B_MASK; 522 memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_B_UID], 4); 523 targets->nfcid1_len = 4; 524 break; 525 case MICROREAD_GATE_ID_MREAD_NFC_T1: 526 targets->supported_protocols = NFC_PROTO_JEWEL_MASK; 527 targets->sens_res = 528 le16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_T1_ATQA]); 529 memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_T1_UID], 4); 530 targets->nfcid1_len = 4; 531 break; 532 case MICROREAD_GATE_ID_MREAD_NFC_T3: 533 targets->supported_protocols = NFC_PROTO_FELICA_MASK; 534 memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_T3_UID], 8); 535 targets->nfcid1_len = 8; 536 break; 537 default: 538 pr_info("discard target discovered to gate 0x%x\n", gate); 539 goto exit_free; 540 } 541 542 r = nfc_targets_found(hdev->ndev, targets, 1); 543 544 exit_free: 545 kfree(targets); 546 547 exit: 548 kfree_skb(skb); 549 550 if (r) 551 pr_err("Failed to handle discovered target err=%d\n", r); 552 } 553 554 static int microread_event_received(struct nfc_hci_dev *hdev, u8 gate, 555 u8 event, struct sk_buff *skb) 556 { 557 int r; 558 u8 mode; 559 560 pr_info("Microread received event 0x%x to gate 0x%x\n", event, gate); 561 562 switch (event) { 563 case MICROREAD_EVT_MREAD_CARD_FOUND: 564 microread_target_discovered(hdev, gate, skb); 565 return 0; 566 567 case MICROREAD_EVT_P2P_INITIATOR_EXCHANGE_FROM_RF: 568 if (skb->len < 1) { 569 kfree_skb(skb); 570 return -EPROTO; 571 } 572 573 if (skb->data[skb->len - 1]) { 574 kfree_skb(skb); 575 return -EIO; 576 } 577 578 skb_trim(skb, skb->len - 1); 579 580 r = nfc_tm_data_received(hdev->ndev, skb); 581 break; 582 583 case MICROREAD_EVT_MCARD_FIELD_ON: 584 case MICROREAD_EVT_MCARD_FIELD_OFF: 585 kfree_skb(skb); 586 return 0; 587 588 case MICROREAD_EVT_P2P_TARGET_ACTIVATED: 589 r = nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK, 590 NFC_COMM_PASSIVE, skb->data, 591 skb->len); 592 593 kfree_skb(skb); 594 break; 595 596 case MICROREAD_EVT_MCARD_EXCHANGE: 597 if (skb->len < 1) { 598 kfree_skb(skb); 599 return -EPROTO; 600 } 601 602 if (skb->data[skb->len-1]) { 603 kfree_skb(skb); 604 return -EIO; 605 } 606 607 skb_trim(skb, skb->len - 1); 608 609 r = nfc_tm_data_received(hdev->ndev, skb); 610 break; 611 612 case MICROREAD_EVT_P2P_TARGET_DEACTIVATED: 613 kfree_skb(skb); 614 615 mode = 0xff; 616 r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_TARGET, 617 MICROREAD_PAR_P2P_TARGET_MODE, &mode, 1); 618 if (r) 619 break; 620 621 r = nfc_hci_send_event(hdev, gate, 622 MICROREAD_EVT_MREAD_DISCOVERY_STOP, NULL, 623 0); 624 break; 625 626 default: 627 return 1; 628 } 629 630 return r; 631 } 632 633 static struct nfc_hci_ops microread_hci_ops = { 634 .open = microread_open, 635 .close = microread_close, 636 .hci_ready = microread_hci_ready, 637 .xmit = microread_xmit, 638 .start_poll = microread_start_poll, 639 .dep_link_up = microread_dep_link_up, 640 .dep_link_down = microread_dep_link_down, 641 .target_from_gate = microread_target_from_gate, 642 .complete_target_discovered = microread_complete_target_discovered, 643 .im_transceive = microread_im_transceive, 644 .tm_send = microread_tm_send, 645 .check_presence = NULL, 646 .event_received = microread_event_received, 647 }; 648 649 int microread_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name, 650 int phy_headroom, int phy_tailroom, int phy_payload, 651 struct nfc_hci_dev **hdev) 652 { 653 struct microread_info *info; 654 unsigned long quirks = 0; 655 u32 protocols; 656 struct nfc_hci_init_data init_data; 657 int r; 658 659 info = kzalloc(sizeof(struct microread_info), GFP_KERNEL); 660 if (!info) { 661 r = -ENOMEM; 662 goto err_info_alloc; 663 } 664 665 info->phy_ops = phy_ops; 666 info->phy_id = phy_id; 667 668 init_data.gate_count = ARRAY_SIZE(microread_gates); 669 memcpy(init_data.gates, microread_gates, sizeof(microread_gates)); 670 671 strcpy(init_data.session_id, "MICROREA"); 672 673 set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks); 674 675 protocols = NFC_PROTO_JEWEL_MASK | 676 NFC_PROTO_MIFARE_MASK | 677 NFC_PROTO_FELICA_MASK | 678 NFC_PROTO_ISO14443_MASK | 679 NFC_PROTO_ISO14443_B_MASK | 680 NFC_PROTO_NFC_DEP_MASK; 681 682 info->hdev = nfc_hci_allocate_device(µread_hci_ops, &init_data, 683 quirks, protocols, llc_name, 684 phy_headroom + 685 MICROREAD_CMDS_HEADROOM, 686 phy_tailroom + 687 MICROREAD_CMD_TAILROOM, 688 phy_payload); 689 if (!info->hdev) { 690 pr_err("Cannot allocate nfc hdev\n"); 691 r = -ENOMEM; 692 goto err_alloc_hdev; 693 } 694 695 nfc_hci_set_clientdata(info->hdev, info); 696 697 r = nfc_hci_register_device(info->hdev); 698 if (r) 699 goto err_regdev; 700 701 *hdev = info->hdev; 702 703 return 0; 704 705 err_regdev: 706 nfc_hci_free_device(info->hdev); 707 708 err_alloc_hdev: 709 kfree(info); 710 711 err_info_alloc: 712 return r; 713 } 714 EXPORT_SYMBOL(microread_probe); 715 716 void microread_remove(struct nfc_hci_dev *hdev) 717 { 718 struct microread_info *info = nfc_hci_get_clientdata(hdev); 719 720 nfc_hci_unregister_device(hdev); 721 nfc_hci_free_device(hdev); 722 kfree(info); 723 } 724 EXPORT_SYMBOL(microread_remove); 725 726 MODULE_LICENSE("GPL"); 727 MODULE_DESCRIPTION(DRIVER_DESC); 728