| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* Driver for NXP PN533 NFC Chip - core functions |
| 4 |
* |
| 5 |
* Copyright (C) 2011 Instituto Nokia de Tecnologia |
| 6 |
* Copyright (C) 2012-2013 Tieto Poland |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <linux/device.h> |
| 10 |
#include <linux/kernel.h> |
| 11 |
#include <linux/module.h> |
| 12 |
#include <linux/slab.h> |
| 13 |
#include <linux/nfc.h> |
| 14 |
#include <linux/netdevice.h> |
| 15 |
#include <net/nfc/nfc.h> |
| 16 |
#include "pn533.h" |
| 17 |
|
| 18 |
#define VERSION "0.3" |
| 19 |
|
| 20 |
/* How much time we spend listening for initiators */ |
| 21 |
#define PN533_LISTEN_TIME 2 |
| 22 |
/* Delay between each poll frame (ms) */ |
| 23 |
#define PN533_POLL_INTERVAL 10 |
| 24 |
|
| 25 |
/* structs for pn533 commands */ |
| 26 |
|
| 27 |
/* PN533_CMD_GET_FIRMWARE_VERSION */ |
| 28 |
struct pn533_fw_version { |
| 29 |
u8 ic; |
| 30 |
u8 ver; |
| 31 |
u8 rev; |
| 32 |
u8 support; |
| 33 |
}; |
| 34 |
|
| 35 |
/* PN533_CMD_RF_CONFIGURATION */ |
| 36 |
#define PN533_CFGITEM_RF_FIELD 0x01 |
| 37 |
#define PN533_CFGITEM_TIMING 0x02 |
| 38 |
#define PN533_CFGITEM_MAX_RETRIES 0x05 |
| 39 |
#define PN533_CFGITEM_PASORI 0x82 |
| 40 |
|
| 41 |
#define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2 |
| 42 |
#define PN533_CFGITEM_RF_FIELD_ON 0x1 |
| 43 |
#define PN533_CFGITEM_RF_FIELD_OFF 0x0 |
| 44 |
|
| 45 |
#define PN533_CONFIG_TIMING_102 0xb |
| 46 |
#define PN533_CONFIG_TIMING_204 0xc |
| 47 |
#define PN533_CONFIG_TIMING_409 0xd |
| 48 |
#define PN533_CONFIG_TIMING_819 0xe |
| 49 |
|
| 50 |
#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00 |
| 51 |
#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF |
| 52 |
|
| 53 |
struct pn533_config_max_retries { |
| 54 |
u8 mx_rty_atr; |
| 55 |
u8 mx_rty_psl; |
| 56 |
u8 mx_rty_passive_act; |
| 57 |
} __packed; |
| 58 |
|
| 59 |
struct pn533_config_timing { |
| 60 |
u8 rfu; |
| 61 |
u8 atr_res_timeout; |
| 62 |
u8 dep_timeout; |
| 63 |
} __packed; |
| 64 |
|
| 65 |
/* PN533_CMD_IN_LIST_PASSIVE_TARGET */ |
| 66 |
|
| 67 |
/* felica commands opcode */ |
| 68 |
#define PN533_FELICA_OPC_SENSF_REQ 0 |
| 69 |
#define PN533_FELICA_OPC_SENSF_RES 1 |
| 70 |
/* felica SENSF_REQ parameters */ |
| 71 |
#define PN533_FELICA_SENSF_SC_ALL 0xFFFF |
| 72 |
#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0 |
| 73 |
#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1 |
| 74 |
#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2 |
| 75 |
|
| 76 |
/* type B initiator_data values */ |
| 77 |
#define PN533_TYPE_B_AFI_ALL_FAMILIES 0 |
| 78 |
#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0 |
| 79 |
#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1 |
| 80 |
|
| 81 |
union pn533_cmd_poll_initdata { |
| 82 |
struct { |
| 83 |
u8 afi; |
| 84 |
u8 polling_method; |
| 85 |
} __packed type_b; |
| 86 |
struct { |
| 87 |
u8 opcode; |
| 88 |
__be16 sc; |
| 89 |
u8 rc; |
| 90 |
u8 tsn; |
| 91 |
} __packed felica; |
| 92 |
}; |
| 93 |
|
| 94 |
struct pn533_poll_modulations { |
| 95 |
struct { |
| 96 |
u8 maxtg; |
| 97 |
u8 brty; |
| 98 |
union pn533_cmd_poll_initdata initiator_data; |
| 99 |
} __packed data; |
| 100 |
u8 len; |
| 101 |
}; |
| 102 |
|
| 103 |
static const struct pn533_poll_modulations poll_mod[] = { |
| 104 |
[PN533_POLL_MOD_106KBPS_A] = { |
| 105 |
.data = { |
| 106 |
.maxtg = 1, |
| 107 |
.brty = 0, |
| 108 |
}, |
| 109 |
.len = 2, |
| 110 |
}, |
| 111 |
[PN533_POLL_MOD_212KBPS_FELICA] = { |
| 112 |
.data = { |
| 113 |
.maxtg = 1, |
| 114 |
.brty = 1, |
| 115 |
.initiator_data.felica = { |
| 116 |
.opcode = PN533_FELICA_OPC_SENSF_REQ, |
| 117 |
.sc = PN533_FELICA_SENSF_SC_ALL, |
| 118 |
.rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE, |
| 119 |
.tsn = 0x03, |
| 120 |
}, |
| 121 |
}, |
| 122 |
.len = 7, |
| 123 |
}, |
| 124 |
[PN533_POLL_MOD_424KBPS_FELICA] = { |
| 125 |
.data = { |
| 126 |
.maxtg = 1, |
| 127 |
.brty = 2, |
| 128 |
.initiator_data.felica = { |
| 129 |
.opcode = PN533_FELICA_OPC_SENSF_REQ, |
| 130 |
.sc = PN533_FELICA_SENSF_SC_ALL, |
| 131 |
.rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE, |
| 132 |
.tsn = 0x03, |
| 133 |
}, |
| 134 |
}, |
| 135 |
.len = 7, |
| 136 |
}, |
| 137 |
[PN533_POLL_MOD_106KBPS_JEWEL] = { |
| 138 |
.data = { |
| 139 |
.maxtg = 1, |
| 140 |
.brty = 4, |
| 141 |
}, |
| 142 |
.len = 2, |
| 143 |
}, |
| 144 |
[PN533_POLL_MOD_847KBPS_B] = { |
| 145 |
.data = { |
| 146 |
.maxtg = 1, |
| 147 |
.brty = 8, |
| 148 |
.initiator_data.type_b = { |
| 149 |
.afi = PN533_TYPE_B_AFI_ALL_FAMILIES, |
| 150 |
.polling_method = |
| 151 |
PN533_TYPE_B_POLL_METHOD_TIMESLOT, |
| 152 |
}, |
| 153 |
}, |
| 154 |
.len = 3, |
| 155 |
}, |
| 156 |
[PN533_LISTEN_MOD] = { |
| 157 |
.len = 0, |
| 158 |
}, |
| 159 |
}; |
| 160 |
|
| 161 |
/* PN533_CMD_IN_ATR */ |
| 162 |
|
| 163 |
struct pn533_cmd_activate_response { |
| 164 |
u8 status; |
| 165 |
u8 nfcid3t[10]; |
| 166 |
u8 didt; |
| 167 |
u8 bst; |
| 168 |
u8 brt; |
| 169 |
u8 to; |
| 170 |
u8 ppt; |
| 171 |
/* optional */ |
| 172 |
u8 gt[]; |
| 173 |
} __packed; |
| 174 |
|
| 175 |
struct pn533_cmd_jump_dep_response { |
| 176 |
u8 status; |
| 177 |
u8 tg; |
| 178 |
u8 nfcid3t[10]; |
| 179 |
u8 didt; |
| 180 |
u8 bst; |
| 181 |
u8 brt; |
| 182 |
u8 to; |
| 183 |
u8 ppt; |
| 184 |
/* optional */ |
| 185 |
u8 gt[]; |
| 186 |
} __packed; |
| 187 |
|
| 188 |
struct pn532_autopoll_resp { |
| 189 |
u8 type; |
| 190 |
u8 ln; |
| 191 |
u8 tg; |
| 192 |
u8 tgdata[]; |
| 193 |
}; |
| 194 |
|
| 195 |
/* PN532_CMD_IN_AUTOPOLL */ |
| 196 |
#define PN532_AUTOPOLL_POLLNR_INFINITE 0xff |
| 197 |
#define PN532_AUTOPOLL_PERIOD 0x03 /* in units of 150 ms */ |
| 198 |
|
| 199 |
#define PN532_AUTOPOLL_TYPE_GENERIC_106 0x00 |
| 200 |
#define PN532_AUTOPOLL_TYPE_GENERIC_212 0x01 |
| 201 |
#define PN532_AUTOPOLL_TYPE_GENERIC_424 0x02 |
| 202 |
#define PN532_AUTOPOLL_TYPE_JEWEL 0x04 |
| 203 |
#define PN532_AUTOPOLL_TYPE_MIFARE 0x10 |
| 204 |
#define PN532_AUTOPOLL_TYPE_FELICA212 0x11 |
| 205 |
#define PN532_AUTOPOLL_TYPE_FELICA424 0x12 |
| 206 |
#define PN532_AUTOPOLL_TYPE_ISOA 0x20 |
| 207 |
#define PN532_AUTOPOLL_TYPE_ISOB 0x23 |
| 208 |
#define PN532_AUTOPOLL_TYPE_DEP_PASSIVE_106 0x40 |
| 209 |
#define PN532_AUTOPOLL_TYPE_DEP_PASSIVE_212 0x41 |
| 210 |
#define PN532_AUTOPOLL_TYPE_DEP_PASSIVE_424 0x42 |
| 211 |
#define PN532_AUTOPOLL_TYPE_DEP_ACTIVE_106 0x80 |
| 212 |
#define PN532_AUTOPOLL_TYPE_DEP_ACTIVE_212 0x81 |
| 213 |
#define PN532_AUTOPOLL_TYPE_DEP_ACTIVE_424 0x82 |
| 214 |
|
| 215 |
/* PN533_TG_INIT_AS_TARGET */ |
| 216 |
#define PN533_INIT_TARGET_PASSIVE 0x1 |
| 217 |
#define PN533_INIT_TARGET_DEP 0x2 |
| 218 |
|
| 219 |
#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3 |
| 220 |
#define PN533_INIT_TARGET_RESP_ACTIVE 0x1 |
| 221 |
#define PN533_INIT_TARGET_RESP_DEP 0x4 |
| 222 |
|
| 223 |
/* The rule: value(high byte) + value(low byte) + checksum = 0 */ |
| 224 |
static inline u8 pn533_ext_checksum(u16 value) |
| 225 |
{ |
| 226 |
return ~(u8)(((value & 0xFF00) >> 8) + (u8)(value & 0xFF)) + 1; |
| 227 |
} |
| 228 |
|
| 229 |
/* The rule: value + checksum = 0 */ |
| 230 |
static inline u8 pn533_std_checksum(u8 value) |
| 231 |
{ |
| 232 |
return ~value + 1; |
| 233 |
} |
| 234 |
|
| 235 |
/* The rule: sum(data elements) + checksum = 0 */ |
| 236 |
static u8 pn533_std_data_checksum(u8 *data, int datalen) |
| 237 |
{ |
| 238 |
u8 sum = 0; |
| 239 |
int i; |
| 240 |
|
| 241 |
for (i = 0; i < datalen; i++) |
| 242 |
sum += data[i]; |
| 243 |
|
| 244 |
return pn533_std_checksum(sum); |
| 245 |
} |
| 246 |
|
| 247 |
static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code) |
| 248 |
{ |
| 249 |
struct pn533_std_frame *frame = _frame; |
| 250 |
|
| 251 |
frame->preamble = 0; |
| 252 |
frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF); |
| 253 |
PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT; |
| 254 |
PN533_FRAME_CMD(frame) = cmd_code; |
| 255 |
frame->datalen = 2; |
| 256 |
} |
| 257 |
|
| 258 |
static void pn533_std_tx_frame_finish(void *_frame) |
| 259 |
{ |
| 260 |
struct pn533_std_frame *frame = _frame; |
| 261 |
|
| 262 |
frame->datalen_checksum = pn533_std_checksum(frame->datalen); |
| 263 |
|
| 264 |
PN533_STD_FRAME_CHECKSUM(frame) = |
| 265 |
pn533_std_data_checksum(frame->data, frame->datalen); |
| 266 |
|
| 267 |
PN533_STD_FRAME_POSTAMBLE(frame) = 0; |
| 268 |
} |
| 269 |
|
| 270 |
static void pn533_std_tx_update_payload_len(void *_frame, int len) |
| 271 |
{ |
| 272 |
struct pn533_std_frame *frame = _frame; |
| 273 |
|
| 274 |
frame->datalen += len; |
| 275 |
} |
| 276 |
|
| 277 |
static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev) |
| 278 |
{ |
| 279 |
u8 checksum; |
| 280 |
struct pn533_std_frame *stdf = _frame; |
| 281 |
|
| 282 |
if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF)) |
| 283 |
return false; |
| 284 |
|
| 285 |
if (likely(!PN533_STD_IS_EXTENDED(stdf))) { |
| 286 |
/* Standard frame code */ |
| 287 |
dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN; |
| 288 |
|
| 289 |
checksum = pn533_std_checksum(stdf->datalen); |
| 290 |
if (checksum != stdf->datalen_checksum) |
| 291 |
return false; |
| 292 |
|
| 293 |
checksum = pn533_std_data_checksum(stdf->data, stdf->datalen); |
| 294 |
if (checksum != PN533_STD_FRAME_CHECKSUM(stdf)) |
| 295 |
return false; |
| 296 |
} else { |
| 297 |
/* Extended */ |
| 298 |
struct pn533_ext_frame *eif = _frame; |
| 299 |
|
| 300 |
dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN; |
| 301 |
|
| 302 |
checksum = pn533_ext_checksum(be16_to_cpu(eif->datalen)); |
| 303 |
if (checksum != eif->datalen_checksum) |
| 304 |
return false; |
| 305 |
|
| 306 |
/* check data checksum */ |
| 307 |
checksum = pn533_std_data_checksum(eif->data, |
| 308 |
be16_to_cpu(eif->datalen)); |
| 309 |
if (checksum != PN533_EXT_FRAME_CHECKSUM(eif)) |
| 310 |
return false; |
| 311 |
} |
| 312 |
|
| 313 |
return true; |
| 314 |
} |
| 315 |
|
| 316 |
bool pn533_rx_frame_is_ack(void *_frame) |
| 317 |
{ |
| 318 |
struct pn533_std_frame *frame = _frame; |
| 319 |
|
| 320 |
if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF)) |
| 321 |
return false; |
| 322 |
|
| 323 |
if (frame->datalen != 0 || frame->datalen_checksum != 0xFF) |
| 324 |
return false; |
| 325 |
|
| 326 |
return true; |
| 327 |
} |
| 328 |
EXPORT_SYMBOL_GPL(pn533_rx_frame_is_ack); |
| 329 |
|
| 330 |
static inline int pn533_std_rx_frame_size(void *frame) |
| 331 |
{ |
| 332 |
struct pn533_std_frame *f = frame; |
| 333 |
|
| 334 |
/* check for Extended Information frame */ |
| 335 |
if (PN533_STD_IS_EXTENDED(f)) { |
| 336 |
struct pn533_ext_frame *eif = frame; |
| 337 |
|
| 338 |
return sizeof(struct pn533_ext_frame) |
| 339 |
+ be16_to_cpu(eif->datalen) + PN533_STD_FRAME_TAIL_LEN; |
| 340 |
} |
| 341 |
|
| 342 |
return sizeof(struct pn533_std_frame) + f->datalen + |
| 343 |
PN533_STD_FRAME_TAIL_LEN; |
| 344 |
} |
| 345 |
|
| 346 |
static u8 pn533_std_get_cmd_code(void *frame) |
| 347 |
{ |
| 348 |
struct pn533_std_frame *f = frame; |
| 349 |
struct pn533_ext_frame *eif = frame; |
| 350 |
|
| 351 |
if (PN533_STD_IS_EXTENDED(f)) |
| 352 |
return PN533_FRAME_CMD(eif); |
| 353 |
else |
| 354 |
return PN533_FRAME_CMD(f); |
| 355 |
} |
| 356 |
|
| 357 |
bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame) |
| 358 |
{ |
| 359 |
return (dev->ops->get_cmd_code(frame) == |
| 360 |
PN533_CMD_RESPONSE(dev->cmd->code)); |
| 361 |
} |
| 362 |
EXPORT_SYMBOL_GPL(pn533_rx_frame_is_cmd_response); |
| 363 |
|
| 364 |
|
| 365 |
static struct pn533_frame_ops pn533_std_frame_ops = { |
| 366 |
.tx_frame_init = pn533_std_tx_frame_init, |
| 367 |
.tx_frame_finish = pn533_std_tx_frame_finish, |
| 368 |
.tx_update_payload_len = pn533_std_tx_update_payload_len, |
| 369 |
.tx_header_len = PN533_STD_FRAME_HEADER_LEN, |
| 370 |
.tx_tail_len = PN533_STD_FRAME_TAIL_LEN, |
| 371 |
|
| 372 |
.rx_is_frame_valid = pn533_std_rx_frame_is_valid, |
| 373 |
.rx_frame_size = pn533_std_rx_frame_size, |
| 374 |
.rx_header_len = PN533_STD_FRAME_HEADER_LEN, |
| 375 |
.rx_tail_len = PN533_STD_FRAME_TAIL_LEN, |
| 376 |
|
| 377 |
.max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN, |
| 378 |
.get_cmd_code = pn533_std_get_cmd_code, |
| 379 |
}; |
| 380 |
|
| 381 |
static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code, |
| 382 |
struct sk_buff *skb) |
| 383 |
{ |
| 384 |
/* payload is already there, just update datalen */ |
| 385 |
int payload_len = skb->len; |
| 386 |
struct pn533_frame_ops *ops = dev->ops; |
| 387 |
|
| 388 |
|
| 389 |
skb_push(skb, ops->tx_header_len); |
| 390 |
skb_put(skb, ops->tx_tail_len); |
| 391 |
|
| 392 |
ops->tx_frame_init(skb->data, cmd_code); |
| 393 |
ops->tx_update_payload_len(skb->data, payload_len); |
| 394 |
ops->tx_frame_finish(skb->data); |
| 395 |
} |
| 396 |
|
| 397 |
static int pn533_send_async_complete(struct pn533 *dev) |
| 398 |
{ |
| 399 |
struct pn533_cmd *cmd = dev->cmd; |
| 400 |
struct sk_buff *resp; |
| 401 |
int status, rc = 0; |
| 402 |
|
| 403 |
if (!cmd) { |
| 404 |
dev_dbg(dev->dev, "%s: cmd not set\n", __func__); |
| 405 |
goto done; |
| 406 |
} |
| 407 |
|
| 408 |
dev_kfree_skb(cmd->req); |
| 409 |
|
| 410 |
status = cmd->status; |
| 411 |
resp = cmd->resp; |
| 412 |
|
| 413 |
if (status < 0) { |
| 414 |
rc = cmd->complete_cb(dev, cmd->complete_cb_context, |
| 415 |
ERR_PTR(status)); |
| 416 |
dev_kfree_skb(resp); |
| 417 |
goto done; |
| 418 |
} |
| 419 |
|
| 420 |
/* when no response is set we got interrupted */ |
| 421 |
if (!resp) |
| 422 |
resp = ERR_PTR(-EINTR); |
| 423 |
|
| 424 |
if (!IS_ERR(resp)) { |
| 425 |
skb_pull(resp, dev->ops->rx_header_len); |
| 426 |
skb_trim(resp, resp->len - dev->ops->rx_tail_len); |
| 427 |
} |
| 428 |
|
| 429 |
rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp); |
| 430 |
|
| 431 |
done: |
| 432 |
kfree(cmd); |
| 433 |
dev->cmd = NULL; |
| 434 |
return rc; |
| 435 |
} |
| 436 |
|
| 437 |
static int __pn533_send_async(struct pn533 *dev, u8 cmd_code, |
| 438 |
struct sk_buff *req, |
| 439 |
pn533_send_async_complete_t complete_cb, |
| 440 |
void *complete_cb_context) |
| 441 |
{ |
| 442 |
struct pn533_cmd *cmd; |
| 443 |
int rc = 0; |
| 444 |
|
| 445 |
dev_dbg(dev->dev, "Sending command 0x%x\n", cmd_code); |
| 446 |
|
| 447 |
cmd = kzalloc_obj(*cmd); |
| 448 |
if (!cmd) |
| 449 |
return -ENOMEM; |
| 450 |
|
| 451 |
cmd->code = cmd_code; |
| 452 |
cmd->req = req; |
| 453 |
cmd->complete_cb = complete_cb; |
| 454 |
cmd->complete_cb_context = complete_cb_context; |
| 455 |
|
| 456 |
pn533_build_cmd_frame(dev, cmd_code, req); |
| 457 |
|
| 458 |
mutex_lock(&dev->cmd_lock); |
| 459 |
|
| 460 |
if (!dev->cmd_pending) { |
| 461 |
dev->cmd = cmd; |
| 462 |
rc = dev->phy_ops->send_frame(dev, req); |
| 463 |
if (rc) { |
| 464 |
dev->cmd = NULL; |
| 465 |
goto error; |
| 466 |
} |
| 467 |
|
| 468 |
dev->cmd_pending = 1; |
| 469 |
goto unlock; |
| 470 |
} |
| 471 |
|
| 472 |
dev_dbg(dev->dev, "%s Queueing command 0x%x\n", |
| 473 |
__func__, cmd_code); |
| 474 |
|
| 475 |
INIT_LIST_HEAD(&cmd->queue); |
| 476 |
list_add_tail(&cmd->queue, &dev->cmd_queue); |
| 477 |
|
| 478 |
goto unlock; |
| 479 |
|
| 480 |
error: |
| 481 |
kfree(cmd); |
| 482 |
unlock: |
| 483 |
mutex_unlock(&dev->cmd_lock); |
| 484 |
return rc; |
| 485 |
} |
| 486 |
|
| 487 |
static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code, |
| 488 |
struct sk_buff *req, |
| 489 |
pn533_send_async_complete_t complete_cb, |
| 490 |
void *complete_cb_context) |
| 491 |
{ |
| 492 |
return __pn533_send_async(dev, cmd_code, req, complete_cb, |
| 493 |
complete_cb_context); |
| 494 |
} |
| 495 |
|
| 496 |
static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code, |
| 497 |
struct sk_buff *req, |
| 498 |
pn533_send_async_complete_t complete_cb, |
| 499 |
void *complete_cb_context) |
| 500 |
{ |
| 501 |
return __pn533_send_async(dev, cmd_code, req, complete_cb, |
| 502 |
complete_cb_context); |
| 503 |
} |
| 504 |
|
| 505 |
/* |
| 506 |
* pn533_send_cmd_direct_async |
| 507 |
* |
| 508 |
* The function sends a priority cmd directly to the chip omitting the cmd |
| 509 |
* queue. It's intended to be used by chaining mechanism of received responses |
| 510 |
* where the host has to request every single chunk of data before scheduling |
| 511 |
* next cmd from the queue. |
| 512 |
*/ |
| 513 |
static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code, |
| 514 |
struct sk_buff *req, |
| 515 |
pn533_send_async_complete_t complete_cb, |
| 516 |
void *complete_cb_context) |
| 517 |
{ |
| 518 |
struct pn533_cmd *cmd; |
| 519 |
int rc; |
| 520 |
|
| 521 |
cmd = kzalloc_obj(*cmd); |
| 522 |
if (!cmd) |
| 523 |
return -ENOMEM; |
| 524 |
|
| 525 |
cmd->code = cmd_code; |
| 526 |
cmd->req = req; |
| 527 |
cmd->complete_cb = complete_cb; |
| 528 |
cmd->complete_cb_context = complete_cb_context; |
| 529 |
|
| 530 |
pn533_build_cmd_frame(dev, cmd_code, req); |
| 531 |
|
| 532 |
dev->cmd = cmd; |
| 533 |
rc = dev->phy_ops->send_frame(dev, req); |
| 534 |
if (rc < 0) { |
| 535 |
dev->cmd = NULL; |
| 536 |
kfree(cmd); |
| 537 |
} |
| 538 |
|
| 539 |
return rc; |
| 540 |
} |
| 541 |
|
| 542 |
static void pn533_wq_cmd_complete(struct work_struct *work) |
| 543 |
{ |
| 544 |
struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work); |
| 545 |
int rc; |
| 546 |
|
| 547 |
rc = pn533_send_async_complete(dev); |
| 548 |
if (rc != -EINPROGRESS) |
| 549 |
queue_work(dev->wq, &dev->cmd_work); |
| 550 |
} |
| 551 |
|
| 552 |
static void pn533_wq_cmd(struct work_struct *work) |
| 553 |
{ |
| 554 |
struct pn533 *dev = container_of(work, struct pn533, cmd_work); |
| 555 |
struct pn533_cmd *cmd; |
| 556 |
int rc; |
| 557 |
|
| 558 |
mutex_lock(&dev->cmd_lock); |
| 559 |
|
| 560 |
if (list_empty(&dev->cmd_queue)) { |
| 561 |
dev->cmd_pending = 0; |
| 562 |
mutex_unlock(&dev->cmd_lock); |
| 563 |
return; |
| 564 |
} |
| 565 |
|
| 566 |
cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue); |
| 567 |
|
| 568 |
list_del(&cmd->queue); |
| 569 |
|
| 570 |
mutex_unlock(&dev->cmd_lock); |
| 571 |
|
| 572 |
dev->cmd = cmd; |
| 573 |
rc = dev->phy_ops->send_frame(dev, cmd->req); |
| 574 |
if (rc < 0) { |
| 575 |
dev->cmd = NULL; |
| 576 |
dev_kfree_skb(cmd->req); |
| 577 |
kfree(cmd); |
| 578 |
return; |
| 579 |
} |
| 580 |
|
| 581 |
} |
| 582 |
|
| 583 |
struct pn533_sync_cmd_response { |
| 584 |
struct sk_buff *resp; |
| 585 |
struct completion done; |
| 586 |
}; |
| 587 |
|
| 588 |
static int pn533_send_sync_complete(struct pn533 *dev, void *_arg, |
| 589 |
struct sk_buff *resp) |
| 590 |
{ |
| 591 |
struct pn533_sync_cmd_response *arg = _arg; |
| 592 |
|
| 593 |
arg->resp = resp; |
| 594 |
complete(&arg->done); |
| 595 |
|
| 596 |
return 0; |
| 597 |
} |
| 598 |
|
| 599 |
/* pn533_send_cmd_sync |
| 600 |
* |
| 601 |
* Please note the req parameter is freed inside the function to |
| 602 |
* limit a number of return value interpretations by the caller. |
| 603 |
* |
| 604 |
* 1. negative in case of error during TX path -> req should be freed |
| 605 |
* |
| 606 |
* 2. negative in case of error during RX path -> req should not be freed |
| 607 |
* as it's been already freed at the beginning of RX path by |
| 608 |
* async_complete_cb. |
| 609 |
* |
| 610 |
* 3. valid pointer in case of successful RX path |
| 611 |
* |
| 612 |
* A caller has to check a return value with IS_ERR macro. If the test pass, |
| 613 |
* the returned pointer is valid. |
| 614 |
* |
| 615 |
*/ |
| 616 |
static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code, |
| 617 |
struct sk_buff *req) |
| 618 |
{ |
| 619 |
int rc; |
| 620 |
struct pn533_sync_cmd_response arg; |
| 621 |
|
| 622 |
init_completion(&arg.done); |
| 623 |
|
| 624 |
rc = pn533_send_cmd_async(dev, cmd_code, req, |
| 625 |
pn533_send_sync_complete, &arg); |
| 626 |
if (rc) { |
| 627 |
dev_kfree_skb(req); |
| 628 |
return ERR_PTR(rc); |
| 629 |
} |
| 630 |
|
| 631 |
wait_for_completion(&arg.done); |
| 632 |
|
| 633 |
return arg.resp; |
| 634 |
} |
| 635 |
|
| 636 |
static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size) |
| 637 |
{ |
| 638 |
struct sk_buff *skb; |
| 639 |
|
| 640 |
skb = alloc_skb(dev->ops->tx_header_len + |
| 641 |
size + |
| 642 |
dev->ops->tx_tail_len, GFP_KERNEL); |
| 643 |
|
| 644 |
if (skb) |
| 645 |
skb_reserve(skb, dev->ops->tx_header_len); |
| 646 |
|
| 647 |
return skb; |
| 648 |
} |
| 649 |
|
| 650 |
struct pn533_target_type_a { |
| 651 |
__be16 sens_res; |
| 652 |
u8 sel_res; |
| 653 |
u8 nfcid_len; |
| 654 |
u8 nfcid_data[]; |
| 655 |
} __packed; |
| 656 |
|
| 657 |
|
| 658 |
#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6)) |
| 659 |
#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0)) |
| 660 |
#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8)) |
| 661 |
|
| 662 |
#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00 |
| 663 |
#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C |
| 664 |
|
| 665 |
#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5) |
| 666 |
#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2) |
| 667 |
|
| 668 |
#define PN533_TYPE_A_SEL_PROT_MIFARE 0 |
| 669 |
#define PN533_TYPE_A_SEL_PROT_ISO14443 1 |
| 670 |
#define PN533_TYPE_A_SEL_PROT_DEP 2 |
| 671 |
#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3 |
| 672 |
|
| 673 |
static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a, |
| 674 |
int target_data_len) |
| 675 |
{ |
| 676 |
u8 ssd; |
| 677 |
u8 platconf; |
| 678 |
|
| 679 |
if (target_data_len < sizeof(struct pn533_target_type_a)) |
| 680 |
return false; |
| 681 |
|
| 682 |
/* |
| 683 |
* The length check of nfcid[] and ats[] are not being performed because |
| 684 |
* the values are not being used |
| 685 |
*/ |
| 686 |
|
| 687 |
/* Requirement 4.6.3.3 from NFC Forum Digital Spec */ |
| 688 |
ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res); |
| 689 |
platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res); |
| 690 |
|
| 691 |
if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL && |
| 692 |
platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) || |
| 693 |
(ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL && |
| 694 |
platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL)) |
| 695 |
return false; |
| 696 |
|
| 697 |
/* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */ |
| 698 |
if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0) |
| 699 |
return false; |
| 700 |
|
| 701 |
if (type_a->nfcid_len > NFC_NFCID1_MAXSIZE) |
| 702 |
return false; |
| 703 |
|
| 704 |
return true; |
| 705 |
} |
| 706 |
|
| 707 |
static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data, |
| 708 |
int tgt_data_len) |
| 709 |
{ |
| 710 |
struct pn533_target_type_a *tgt_type_a; |
| 711 |
|
| 712 |
tgt_type_a = (struct pn533_target_type_a *)tgt_data; |
| 713 |
|
| 714 |
if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len)) |
| 715 |
return -EPROTO; |
| 716 |
|
| 717 |
switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) { |
| 718 |
case PN533_TYPE_A_SEL_PROT_MIFARE: |
| 719 |
nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK; |
| 720 |
break; |
| 721 |
case PN533_TYPE_A_SEL_PROT_ISO14443: |
| 722 |
nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK; |
| 723 |
break; |
| 724 |
case PN533_TYPE_A_SEL_PROT_DEP: |
| 725 |
nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK; |
| 726 |
break; |
| 727 |
case PN533_TYPE_A_SEL_PROT_ISO14443_DEP: |
| 728 |
nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK | |
| 729 |
NFC_PROTO_NFC_DEP_MASK; |
| 730 |
break; |
| 731 |
} |
| 732 |
|
| 733 |
nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res); |
| 734 |
nfc_tgt->sel_res = tgt_type_a->sel_res; |
| 735 |
nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len; |
| 736 |
memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len); |
| 737 |
|
| 738 |
return 0; |
| 739 |
} |
| 740 |
|
| 741 |
struct pn533_target_felica { |
| 742 |
u8 pol_res; |
| 743 |
u8 opcode; |
| 744 |
u8 nfcid2[NFC_NFCID2_MAXSIZE]; |
| 745 |
u8 pad[8]; |
| 746 |
/* optional */ |
| 747 |
u8 syst_code[]; |
| 748 |
} __packed; |
| 749 |
|
| 750 |
#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01 |
| 751 |
#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE |
| 752 |
|
| 753 |
static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica, |
| 754 |
int target_data_len) |
| 755 |
{ |
| 756 |
if (target_data_len < sizeof(struct pn533_target_felica)) |
| 757 |
return false; |
| 758 |
|
| 759 |
if (felica->opcode != PN533_FELICA_OPC_SENSF_RES) |
| 760 |
return false; |
| 761 |
|
| 762 |
return true; |
| 763 |
} |
| 764 |
|
| 765 |
static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data, |
| 766 |
int tgt_data_len) |
| 767 |
{ |
| 768 |
struct pn533_target_felica *tgt_felica; |
| 769 |
|
| 770 |
tgt_felica = (struct pn533_target_felica *)tgt_data; |
| 771 |
|
| 772 |
if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len)) |
| 773 |
return -EPROTO; |
| 774 |
|
| 775 |
if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) && |
| 776 |
(tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2)) |
| 777 |
nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK; |
| 778 |
else |
| 779 |
nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK; |
| 780 |
|
| 781 |
memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9); |
| 782 |
nfc_tgt->sensf_res_len = 9; |
| 783 |
|
| 784 |
memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE); |
| 785 |
nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE; |
| 786 |
|
| 787 |
return 0; |
| 788 |
} |
| 789 |
|
| 790 |
struct pn533_target_jewel { |
| 791 |
__be16 sens_res; |
| 792 |
u8 jewelid[4]; |
| 793 |
} __packed; |
| 794 |
|
| 795 |
static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel, |
| 796 |
int target_data_len) |
| 797 |
{ |
| 798 |
u8 ssd; |
| 799 |
u8 platconf; |
| 800 |
|
| 801 |
if (target_data_len < sizeof(struct pn533_target_jewel)) |
| 802 |
return false; |
| 803 |
|
| 804 |
/* Requirement 4.6.3.3 from NFC Forum Digital Spec */ |
| 805 |
ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res); |
| 806 |
platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res); |
| 807 |
|
| 808 |
if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL && |
| 809 |
platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) || |
| 810 |
(ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL && |
| 811 |
platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL)) |
| 812 |
return false; |
| 813 |
|
| 814 |
return true; |
| 815 |
} |
| 816 |
|
| 817 |
static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data, |
| 818 |
int tgt_data_len) |
| 819 |
{ |
| 820 |
struct pn533_target_jewel *tgt_jewel; |
| 821 |
|
| 822 |
tgt_jewel = (struct pn533_target_jewel *)tgt_data; |
| 823 |
|
| 824 |
if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len)) |
| 825 |
return -EPROTO; |
| 826 |
|
| 827 |
nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK; |
| 828 |
nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res); |
| 829 |
nfc_tgt->nfcid1_len = 4; |
| 830 |
memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len); |
| 831 |
|
| 832 |
return 0; |
| 833 |
} |
| 834 |
|
| 835 |
struct pn533_type_b_prot_info { |
| 836 |
u8 bitrate; |
| 837 |
u8 fsci_type; |
| 838 |
u8 fwi_adc_fo; |
| 839 |
} __packed; |
| 840 |
|
| 841 |
#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4) |
| 842 |
#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0) |
| 843 |
#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8 |
| 844 |
|
| 845 |
struct pn533_type_b_sens_res { |
| 846 |
u8 opcode; |
| 847 |
u8 nfcid[4]; |
| 848 |
u8 appdata[4]; |
| 849 |
struct pn533_type_b_prot_info prot_info; |
| 850 |
} __packed; |
| 851 |
|
| 852 |
#define PN533_TYPE_B_OPC_SENSB_RES 0x50 |
| 853 |
|
| 854 |
struct pn533_target_type_b { |
| 855 |
struct pn533_type_b_sens_res sensb_res; |
| 856 |
u8 attrib_res_len; |
| 857 |
u8 attrib_res[]; |
| 858 |
} __packed; |
| 859 |
|
| 860 |
static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b, |
| 861 |
int target_data_len) |
| 862 |
{ |
| 863 |
if (target_data_len < sizeof(struct pn533_target_type_b)) |
| 864 |
return false; |
| 865 |
|
| 866 |
if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES) |
| 867 |
return false; |
| 868 |
|
| 869 |
if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) & |
| 870 |
PN533_TYPE_B_PROT_TYPE_RFU_MASK) |
| 871 |
return false; |
| 872 |
|
| 873 |
return true; |
| 874 |
} |
| 875 |
|
| 876 |
static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data, |
| 877 |
int tgt_data_len) |
| 878 |
{ |
| 879 |
struct pn533_target_type_b *tgt_type_b; |
| 880 |
|
| 881 |
tgt_type_b = (struct pn533_target_type_b *)tgt_data; |
| 882 |
|
| 883 |
if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len)) |
| 884 |
return -EPROTO; |
| 885 |
|
| 886 |
nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK; |
| 887 |
|
| 888 |
return 0; |
| 889 |
} |
| 890 |
|
| 891 |
static void pn533_poll_reset_mod_list(struct pn533 *dev); |
| 892 |
static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata, |
| 893 |
int tgdata_len) |
| 894 |
{ |
| 895 |
struct nfc_target nfc_tgt; |
| 896 |
int rc; |
| 897 |
|
| 898 |
dev_dbg(dev->dev, "%s: modulation=%d\n", |
| 899 |
__func__, dev->poll_mod_curr); |
| 900 |
|
| 901 |
if (tg != 1) |
| 902 |
return -EPROTO; |
| 903 |
|
| 904 |
memset(&nfc_tgt, 0, sizeof(struct nfc_target)); |
| 905 |
|
| 906 |
switch (dev->poll_mod_curr) { |
| 907 |
case PN533_POLL_MOD_106KBPS_A: |
| 908 |
rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len); |
| 909 |
break; |
| 910 |
case PN533_POLL_MOD_212KBPS_FELICA: |
| 911 |
case PN533_POLL_MOD_424KBPS_FELICA: |
| 912 |
rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len); |
| 913 |
break; |
| 914 |
case PN533_POLL_MOD_106KBPS_JEWEL: |
| 915 |
rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len); |
| 916 |
break; |
| 917 |
case PN533_POLL_MOD_847KBPS_B: |
| 918 |
rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len); |
| 919 |
break; |
| 920 |
default: |
| 921 |
nfc_err(dev->dev, |
| 922 |
"Unknown current poll modulation\n"); |
| 923 |
return -EPROTO; |
| 924 |
} |
| 925 |
|
| 926 |
if (rc) |
| 927 |
return rc; |
| 928 |
|
| 929 |
if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) { |
| 930 |
dev_dbg(dev->dev, |
| 931 |
"The Tg found doesn't have the desired protocol\n"); |
| 932 |
return -EAGAIN; |
| 933 |
} |
| 934 |
|
| 935 |
dev_dbg(dev->dev, |
| 936 |
"Target found - supported protocols: 0x%x\n", |
| 937 |
nfc_tgt.supported_protocols); |
| 938 |
|
| 939 |
dev->tgt_available_prots = nfc_tgt.supported_protocols; |
| 940 |
|
| 941 |
pn533_poll_reset_mod_list(dev); |
| 942 |
nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1); |
| 943 |
|
| 944 |
return 0; |
| 945 |
} |
| 946 |
|
| 947 |
static inline void pn533_poll_next_mod(struct pn533 *dev) |
| 948 |
{ |
| 949 |
dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count; |
| 950 |
} |
| 951 |
|
| 952 |
static void pn533_poll_reset_mod_list(struct pn533 *dev) |
| 953 |
{ |
| 954 |
dev->poll_mod_count = 0; |
| 955 |
} |
| 956 |
|
| 957 |
static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index) |
| 958 |
{ |
| 959 |
dev->poll_mod_active[dev->poll_mod_count] = |
| 960 |
(struct pn533_poll_modulations *)&poll_mod[mod_index]; |
| 961 |
dev->poll_mod_count++; |
| 962 |
} |
| 963 |
|
| 964 |
static void pn533_poll_create_mod_list(struct pn533 *dev, |
| 965 |
u32 im_protocols, u32 tm_protocols) |
| 966 |
{ |
| 967 |
pn533_poll_reset_mod_list(dev); |
| 968 |
|
| 969 |
if ((im_protocols & NFC_PROTO_MIFARE_MASK) || |
| 970 |
(im_protocols & NFC_PROTO_ISO14443_MASK) || |
| 971 |
(im_protocols & NFC_PROTO_NFC_DEP_MASK)) |
| 972 |
pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A); |
| 973 |
|
| 974 |
if (im_protocols & NFC_PROTO_FELICA_MASK || |
| 975 |
im_protocols & NFC_PROTO_NFC_DEP_MASK) { |
| 976 |
pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA); |
| 977 |
pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA); |
| 978 |
} |
| 979 |
|
| 980 |
if (im_protocols & NFC_PROTO_JEWEL_MASK) |
| 981 |
pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL); |
| 982 |
|
| 983 |
if (im_protocols & NFC_PROTO_ISO14443_B_MASK) |
| 984 |
pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B); |
| 985 |
|
| 986 |
if (tm_protocols) |
| 987 |
pn533_poll_add_mod(dev, PN533_LISTEN_MOD); |
| 988 |
} |
| 989 |
|
| 990 |
static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp) |
| 991 |
{ |
| 992 |
u8 nbtg, tg, *tgdata; |
| 993 |
int rc, tgdata_len; |
| 994 |
|
| 995 |
/* Toggle the DEP polling */ |
| 996 |
if (dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK) |
| 997 |
dev->poll_dep = 1; |
| 998 |
|
| 999 |
nbtg = resp->data[0]; |
| 1000 |
tg = resp->data[1]; |
| 1001 |
tgdata = &resp->data[2]; |
| 1002 |
tgdata_len = resp->len - 2; /* nbtg + tg */ |
| 1003 |
|
| 1004 |
if (nbtg) { |
| 1005 |
rc = pn533_target_found(dev, tg, tgdata, tgdata_len); |
| 1006 |
|
| 1007 |
/* We must stop the poll after a valid target found */ |
| 1008 |
if (rc == 0) |
| 1009 |
return 0; |
| 1010 |
} |
| 1011 |
|
| 1012 |
return -EAGAIN; |
| 1013 |
} |
| 1014 |
|
| 1015 |
static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev) |
| 1016 |
{ |
| 1017 |
struct sk_buff *skb; |
| 1018 |
u8 *felica, *nfcid3; |
| 1019 |
|
| 1020 |
u8 *gbytes = dev->gb; |
| 1021 |
size_t gbytes_len = dev->gb_len; |
| 1022 |
|
| 1023 |
u8 felica_params[18] = {0x1, 0xfe, /* DEP */ |
| 1024 |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */ |
| 1025 |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 1026 |
0xff, 0xff}; /* System code */ |
| 1027 |
|
| 1028 |
u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */ |
| 1029 |
0x0, 0x0, 0x0, |
| 1030 |
0x40}; /* SEL_RES for DEP */ |
| 1031 |
|
| 1032 |
unsigned int skb_len = 36 + /* |
| 1033 |
* mode (1), mifare (6), |
| 1034 |
* felica (18), nfcid3 (10), gb_len (1) |
| 1035 |
*/ |
| 1036 |
gbytes_len + |
| 1037 |
1; /* len Tk*/ |
| 1038 |
|
| 1039 |
skb = pn533_alloc_skb(dev, skb_len); |
| 1040 |
if (!skb) |
| 1041 |
return NULL; |
| 1042 |
|
| 1043 |
/* DEP support only */ |
| 1044 |
skb_put_u8(skb, PN533_INIT_TARGET_DEP); |
| 1045 |
|
| 1046 |
/* MIFARE params */ |
| 1047 |
skb_put_data(skb, mifare_params, 6); |
| 1048 |
|
| 1049 |
/* Felica params */ |
| 1050 |
felica = skb_put_data(skb, felica_params, 18); |
| 1051 |
get_random_bytes(felica + 2, 6); |
| 1052 |
|
| 1053 |
/* NFCID3 */ |
| 1054 |
nfcid3 = skb_put_zero(skb, 10); |
| 1055 |
memcpy(nfcid3, felica, 8); |
| 1056 |
|
| 1057 |
/* General bytes */ |
| 1058 |
skb_put_u8(skb, gbytes_len); |
| 1059 |
|
| 1060 |
skb_put_data(skb, gbytes, gbytes_len); |
| 1061 |
|
| 1062 |
/* Len Tk */ |
| 1063 |
skb_put_u8(skb, 0); |
| 1064 |
|
| 1065 |
return skb; |
| 1066 |
} |
| 1067 |
|
| 1068 |
static void pn533_wq_tm_mi_recv(struct work_struct *work); |
| 1069 |
static struct sk_buff *pn533_build_response(struct pn533 *dev); |
| 1070 |
|
| 1071 |
static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg, |
| 1072 |
struct sk_buff *resp) |
| 1073 |
{ |
| 1074 |
struct sk_buff *skb; |
| 1075 |
u8 status, ret, mi; |
| 1076 |
int rc; |
| 1077 |
|
| 1078 |
if (IS_ERR(resp)) { |
| 1079 |
skb_queue_purge(&dev->resp_q); |
| 1080 |
return PTR_ERR(resp); |
| 1081 |
} |
| 1082 |
|
| 1083 |
status = resp->data[0]; |
| 1084 |
|
| 1085 |
ret = status & PN533_CMD_RET_MASK; |
| 1086 |
mi = status & PN533_CMD_MI_MASK; |
| 1087 |
|
| 1088 |
skb_pull(resp, sizeof(status)); |
| 1089 |
|
| 1090 |
if (ret != PN533_CMD_RET_SUCCESS) { |
| 1091 |
rc = -EIO; |
| 1092 |
goto error; |
| 1093 |
} |
| 1094 |
|
| 1095 |
skb_queue_tail(&dev->resp_q, resp); |
| 1096 |
|
| 1097 |
if (mi) { |
| 1098 |
queue_work(dev->wq, &dev->mi_tm_rx_work); |
| 1099 |
return -EINPROGRESS; |
| 1100 |
} |
| 1101 |
|
| 1102 |
skb = pn533_build_response(dev); |
| 1103 |
if (!skb) { |
| 1104 |
rc = -EIO; |
| 1105 |
goto error; |
| 1106 |
} |
| 1107 |
|
| 1108 |
return nfc_tm_data_received(dev->nfc_dev, skb); |
| 1109 |
|
| 1110 |
error: |
| 1111 |
nfc_tm_deactivated(dev->nfc_dev); |
| 1112 |
dev->tgt_mode = 0; |
| 1113 |
skb_queue_purge(&dev->resp_q); |
| 1114 |
dev_kfree_skb(resp); |
| 1115 |
|
| 1116 |
return rc; |
| 1117 |
} |
| 1118 |
|
| 1119 |
static void pn533_wq_tm_mi_recv(struct work_struct *work) |
| 1120 |
{ |
| 1121 |
struct pn533 *dev = container_of(work, struct pn533, mi_tm_rx_work); |
| 1122 |
struct sk_buff *skb; |
| 1123 |
int rc; |
| 1124 |
|
| 1125 |
skb = pn533_alloc_skb(dev, 0); |
| 1126 |
if (!skb) |
| 1127 |
return; |
| 1128 |
|
| 1129 |
rc = pn533_send_cmd_direct_async(dev, |
| 1130 |
PN533_CMD_TG_GET_DATA, |
| 1131 |
skb, |
| 1132 |
pn533_tm_get_data_complete, |
| 1133 |
NULL); |
| 1134 |
|
| 1135 |
if (rc < 0) |
| 1136 |
dev_kfree_skb(skb); |
| 1137 |
} |
| 1138 |
|
| 1139 |
static int pn533_tm_send_complete(struct pn533 *dev, void *arg, |
| 1140 |
struct sk_buff *resp); |
| 1141 |
static void pn533_wq_tm_mi_send(struct work_struct *work) |
| 1142 |
{ |
| 1143 |
struct pn533 *dev = container_of(work, struct pn533, mi_tm_tx_work); |
| 1144 |
struct sk_buff *skb; |
| 1145 |
int rc; |
| 1146 |
|
| 1147 |
/* Grab the first skb in the queue */ |
| 1148 |
skb = skb_dequeue(&dev->fragment_skb); |
| 1149 |
if (skb == NULL) { /* No more data */ |
| 1150 |
/* Reset the queue for future use */ |
| 1151 |
skb_queue_head_init(&dev->fragment_skb); |
| 1152 |
goto error; |
| 1153 |
} |
| 1154 |
|
| 1155 |
/* last entry - remove MI bit */ |
| 1156 |
if (skb_queue_len(&dev->fragment_skb) == 0) { |
| 1157 |
rc = pn533_send_cmd_direct_async(dev, PN533_CMD_TG_SET_DATA, |
| 1158 |
skb, pn533_tm_send_complete, NULL); |
| 1159 |
} else |
| 1160 |
rc = pn533_send_cmd_direct_async(dev, |
| 1161 |
PN533_CMD_TG_SET_META_DATA, |
| 1162 |
skb, pn533_tm_send_complete, NULL); |
| 1163 |
|
| 1164 |
if (rc == 0) /* success */ |
| 1165 |
return; |
| 1166 |
|
| 1167 |
dev_err(dev->dev, |
| 1168 |
"Error %d when trying to perform set meta data_exchange", rc); |
| 1169 |
|
| 1170 |
dev_kfree_skb(skb); |
| 1171 |
|
| 1172 |
error: |
| 1173 |
dev->phy_ops->send_ack(dev, GFP_KERNEL); |
| 1174 |
queue_work(dev->wq, &dev->cmd_work); |
| 1175 |
} |
| 1176 |
|
| 1177 |
static void pn533_wq_tg_get_data(struct work_struct *work) |
| 1178 |
{ |
| 1179 |
struct pn533 *dev = container_of(work, struct pn533, tg_work); |
| 1180 |
struct sk_buff *skb; |
| 1181 |
int rc; |
| 1182 |
|
| 1183 |
skb = pn533_alloc_skb(dev, 0); |
| 1184 |
if (!skb) |
| 1185 |
return; |
| 1186 |
|
| 1187 |
rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb, |
| 1188 |
pn533_tm_get_data_complete, NULL); |
| 1189 |
|
| 1190 |
if (rc < 0) |
| 1191 |
dev_kfree_skb(skb); |
| 1192 |
} |
| 1193 |
|
| 1194 |
#define ATR_REQ_GB_OFFSET 17 |
| 1195 |
static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp) |
| 1196 |
{ |
| 1197 |
u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb; |
| 1198 |
size_t gb_len; |
| 1199 |
int rc; |
| 1200 |
|
| 1201 |
if (resp->len < ATR_REQ_GB_OFFSET + 1) |
| 1202 |
return -EINVAL; |
| 1203 |
|
| 1204 |
mode = resp->data[0]; |
| 1205 |
cmd = &resp->data[1]; |
| 1206 |
|
| 1207 |
dev_dbg(dev->dev, "Target mode 0x%x len %d\n", |
| 1208 |
mode, resp->len); |
| 1209 |
|
| 1210 |
if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) == |
| 1211 |
PN533_INIT_TARGET_RESP_ACTIVE) |
| 1212 |
comm_mode = NFC_COMM_ACTIVE; |
| 1213 |
|
| 1214 |
if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */ |
| 1215 |
return -EOPNOTSUPP; |
| 1216 |
|
| 1217 |
gb = cmd + ATR_REQ_GB_OFFSET; |
| 1218 |
gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1); |
| 1219 |
|
| 1220 |
rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK, |
| 1221 |
comm_mode, gb, gb_len); |
| 1222 |
if (rc < 0) { |
| 1223 |
nfc_err(dev->dev, |
| 1224 |
"Error when signaling target activation\n"); |
| 1225 |
return rc; |
| 1226 |
} |
| 1227 |
|
| 1228 |
dev->tgt_mode = 1; |
| 1229 |
queue_work(dev->wq, &dev->tg_work); |
| 1230 |
|
| 1231 |
return 0; |
| 1232 |
} |
| 1233 |
|
| 1234 |
static void pn533_listen_mode_timer(struct timer_list *t) |
| 1235 |
{ |
| 1236 |
struct pn533 *dev = timer_container_of(dev, t, listen_timer); |
| 1237 |
|
| 1238 |
dev->cancel_listen = 1; |
| 1239 |
|
| 1240 |
pn533_poll_next_mod(dev); |
| 1241 |
|
| 1242 |
queue_delayed_work(dev->wq, &dev->poll_work, |
| 1243 |
msecs_to_jiffies(PN533_POLL_INTERVAL)); |
| 1244 |
} |
| 1245 |
|
| 1246 |
static int pn533_rf_complete(struct pn533 *dev, void *arg, |
| 1247 |
struct sk_buff *resp) |
| 1248 |
{ |
| 1249 |
int rc = 0; |
| 1250 |
|
| 1251 |
if (IS_ERR(resp)) { |
| 1252 |
rc = PTR_ERR(resp); |
| 1253 |
|
| 1254 |
nfc_err(dev->dev, "RF setting error %d\n", rc); |
| 1255 |
|
| 1256 |
return rc; |
| 1257 |
} |
| 1258 |
|
| 1259 |
queue_delayed_work(dev->wq, &dev->poll_work, |
| 1260 |
msecs_to_jiffies(PN533_POLL_INTERVAL)); |
| 1261 |
|
| 1262 |
dev_kfree_skb(resp); |
| 1263 |
return rc; |
| 1264 |
} |
| 1265 |
|
| 1266 |
static void pn533_wq_rf(struct work_struct *work) |
| 1267 |
{ |
| 1268 |
struct pn533 *dev = container_of(work, struct pn533, rf_work); |
| 1269 |
struct sk_buff *skb; |
| 1270 |
int rc; |
| 1271 |
|
| 1272 |
skb = pn533_alloc_skb(dev, 2); |
| 1273 |
if (!skb) |
| 1274 |
return; |
| 1275 |
|
| 1276 |
skb_put_u8(skb, PN533_CFGITEM_RF_FIELD); |
| 1277 |
skb_put_u8(skb, PN533_CFGITEM_RF_FIELD_AUTO_RFCA); |
| 1278 |
|
| 1279 |
rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb, |
| 1280 |
pn533_rf_complete, NULL); |
| 1281 |
if (rc < 0) { |
| 1282 |
dev_kfree_skb(skb); |
| 1283 |
nfc_err(dev->dev, "RF setting error %d\n", rc); |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
static int pn533_poll_dep_complete(struct pn533 *dev, void *arg, |
| 1288 |
struct sk_buff *resp) |
| 1289 |
{ |
| 1290 |
struct pn533_cmd_jump_dep_response *rsp; |
| 1291 |
struct nfc_target nfc_target; |
| 1292 |
u8 target_gt_len; |
| 1293 |
int rc; |
| 1294 |
|
| 1295 |
if (IS_ERR(resp)) |
| 1296 |
return PTR_ERR(resp); |
| 1297 |
|
| 1298 |
memset(&nfc_target, 0, sizeof(struct nfc_target)); |
| 1299 |
|
| 1300 |
rsp = (struct pn533_cmd_jump_dep_response *)resp->data; |
| 1301 |
|
| 1302 |
rc = rsp->status & PN533_CMD_RET_MASK; |
| 1303 |
if (rc != PN533_CMD_RET_SUCCESS) { |
| 1304 |
/* Not target found, turn radio off */ |
| 1305 |
queue_work(dev->wq, &dev->rf_work); |
| 1306 |
|
| 1307 |
dev_kfree_skb(resp); |
| 1308 |
return 0; |
| 1309 |
} |
| 1310 |
|
| 1311 |
dev_dbg(dev->dev, "Creating new target"); |
| 1312 |
|
| 1313 |
nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK; |
| 1314 |
nfc_target.nfcid1_len = 10; |
| 1315 |
memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len); |
| 1316 |
rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1); |
| 1317 |
if (rc) |
| 1318 |
goto error; |
| 1319 |
|
| 1320 |
dev->tgt_available_prots = 0; |
| 1321 |
dev->tgt_active_prot = NFC_PROTO_NFC_DEP; |
| 1322 |
|
| 1323 |
/* ATR_RES general bytes are located at offset 17 */ |
| 1324 |
target_gt_len = resp->len - 17; |
| 1325 |
rc = nfc_set_remote_general_bytes(dev->nfc_dev, |
| 1326 |
rsp->gt, target_gt_len); |
| 1327 |
if (!rc) { |
| 1328 |
rc = nfc_dep_link_is_up(dev->nfc_dev, |
| 1329 |
dev->nfc_dev->targets[0].idx, |
| 1330 |
0, NFC_RF_INITIATOR); |
| 1331 |
|
| 1332 |
if (!rc) |
| 1333 |
pn533_poll_reset_mod_list(dev); |
| 1334 |
} |
| 1335 |
error: |
| 1336 |
dev_kfree_skb(resp); |
| 1337 |
return rc; |
| 1338 |
} |
| 1339 |
|
| 1340 |
#define PASSIVE_DATA_LEN 5 |
| 1341 |
static int pn533_poll_dep(struct nfc_dev *nfc_dev) |
| 1342 |
{ |
| 1343 |
struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 1344 |
struct sk_buff *skb; |
| 1345 |
int rc, skb_len; |
| 1346 |
u8 *next, nfcid3[NFC_NFCID3_MAXSIZE]; |
| 1347 |
u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3}; |
| 1348 |
|
| 1349 |
if (!dev->gb) { |
| 1350 |
dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len); |
| 1351 |
|
| 1352 |
if (!dev->gb || !dev->gb_len) { |
| 1353 |
dev->poll_dep = 0; |
| 1354 |
queue_work(dev->wq, &dev->rf_work); |
| 1355 |
} |
| 1356 |
} |
| 1357 |
|
| 1358 |
skb_len = 3 + dev->gb_len; /* ActPass + BR + Next */ |
| 1359 |
skb_len += PASSIVE_DATA_LEN; |
| 1360 |
|
| 1361 |
/* NFCID3 */ |
| 1362 |
skb_len += NFC_NFCID3_MAXSIZE; |
| 1363 |
nfcid3[0] = 0x1; |
| 1364 |
nfcid3[1] = 0xfe; |
| 1365 |
get_random_bytes(nfcid3 + 2, 6); |
| 1366 |
|
| 1367 |
skb = pn533_alloc_skb(dev, skb_len); |
| 1368 |
if (!skb) |
| 1369 |
return -ENOMEM; |
| 1370 |
|
| 1371 |
skb_put_u8(skb, 0x01); /* Active */ |
| 1372 |
skb_put_u8(skb, 0x02); /* 424 kbps */ |
| 1373 |
|
| 1374 |
next = skb_put(skb, 1); /* Next */ |
| 1375 |
*next = 0; |
| 1376 |
|
| 1377 |
/* Copy passive data */ |
| 1378 |
skb_put_data(skb, passive_data, PASSIVE_DATA_LEN); |
| 1379 |
*next |= 1; |
| 1380 |
|
| 1381 |
/* Copy NFCID3 (which is NFCID2 from SENSF_RES) */ |
| 1382 |
skb_put_data(skb, nfcid3, NFC_NFCID3_MAXSIZE); |
| 1383 |
*next |= 2; |
| 1384 |
|
| 1385 |
skb_put_data(skb, dev->gb, dev->gb_len); |
| 1386 |
*next |= 4; /* We have some Gi */ |
| 1387 |
|
| 1388 |
rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb, |
| 1389 |
pn533_poll_dep_complete, NULL); |
| 1390 |
|
| 1391 |
if (rc < 0) |
| 1392 |
dev_kfree_skb(skb); |
| 1393 |
|
| 1394 |
return rc; |
| 1395 |
} |
| 1396 |
|
| 1397 |
static int pn533_autopoll_complete(struct pn533 *dev, void *arg, |
| 1398 |
struct sk_buff *resp) |
| 1399 |
{ |
| 1400 |
struct pn532_autopoll_resp *apr; |
| 1401 |
struct nfc_target nfc_tgt; |
| 1402 |
u8 nbtg; |
| 1403 |
int rc; |
| 1404 |
|
| 1405 |
if (IS_ERR(resp)) { |
| 1406 |
rc = PTR_ERR(resp); |
| 1407 |
|
| 1408 |
nfc_err(dev->dev, "%s autopoll complete error %d\n", |
| 1409 |
__func__, rc); |
| 1410 |
|
| 1411 |
if (rc == -ENOENT) { |
| 1412 |
if (dev->poll_mod_count != 0) |
| 1413 |
return rc; |
| 1414 |
goto stop_poll; |
| 1415 |
} |
| 1416 |
nfc_err(dev->dev, "Error %d when running autopoll\n", rc); |
| 1417 |
goto stop_poll; |
| 1418 |
} |
| 1419 |
|
| 1420 |
nbtg = resp->data[0]; |
| 1421 |
if ((nbtg > 2) || (nbtg <= 0)) |
| 1422 |
return -EAGAIN; |
| 1423 |
|
| 1424 |
apr = (struct pn532_autopoll_resp *)&resp->data[1]; |
| 1425 |
while (nbtg--) { |
| 1426 |
memset(&nfc_tgt, 0, sizeof(struct nfc_target)); |
| 1427 |
switch (apr->type) { |
| 1428 |
case PN532_AUTOPOLL_TYPE_ISOA: |
| 1429 |
dev_dbg(dev->dev, "ISOA\n"); |
| 1430 |
rc = pn533_target_found_type_a(&nfc_tgt, apr->tgdata, |
| 1431 |
apr->ln - 1); |
| 1432 |
break; |
| 1433 |
case PN532_AUTOPOLL_TYPE_FELICA212: |
| 1434 |
case PN532_AUTOPOLL_TYPE_FELICA424: |
| 1435 |
dev_dbg(dev->dev, "FELICA\n"); |
| 1436 |
rc = pn533_target_found_felica(&nfc_tgt, apr->tgdata, |
| 1437 |
apr->ln - 1); |
| 1438 |
break; |
| 1439 |
case PN532_AUTOPOLL_TYPE_JEWEL: |
| 1440 |
dev_dbg(dev->dev, "JEWEL\n"); |
| 1441 |
rc = pn533_target_found_jewel(&nfc_tgt, apr->tgdata, |
| 1442 |
apr->ln - 1); |
| 1443 |
break; |
| 1444 |
case PN532_AUTOPOLL_TYPE_ISOB: |
| 1445 |
dev_dbg(dev->dev, "ISOB\n"); |
| 1446 |
rc = pn533_target_found_type_b(&nfc_tgt, apr->tgdata, |
| 1447 |
apr->ln - 1); |
| 1448 |
break; |
| 1449 |
case PN532_AUTOPOLL_TYPE_MIFARE: |
| 1450 |
dev_dbg(dev->dev, "Mifare\n"); |
| 1451 |
rc = pn533_target_found_type_a(&nfc_tgt, apr->tgdata, |
| 1452 |
apr->ln - 1); |
| 1453 |
break; |
| 1454 |
default: |
| 1455 |
nfc_err(dev->dev, |
| 1456 |
"Unknown current poll modulation\n"); |
| 1457 |
rc = -EPROTO; |
| 1458 |
} |
| 1459 |
|
| 1460 |
if (rc) |
| 1461 |
goto done; |
| 1462 |
|
| 1463 |
if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) { |
| 1464 |
nfc_err(dev->dev, |
| 1465 |
"The Tg found doesn't have the desired protocol\n"); |
| 1466 |
rc = -EAGAIN; |
| 1467 |
goto done; |
| 1468 |
} |
| 1469 |
|
| 1470 |
dev->tgt_available_prots = nfc_tgt.supported_protocols; |
| 1471 |
apr = (struct pn532_autopoll_resp *) |
| 1472 |
(apr->tgdata + (apr->ln - 1)); |
| 1473 |
} |
| 1474 |
|
| 1475 |
pn533_poll_reset_mod_list(dev); |
| 1476 |
nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1); |
| 1477 |
|
| 1478 |
done: |
| 1479 |
dev_kfree_skb(resp); |
| 1480 |
return rc; |
| 1481 |
|
| 1482 |
stop_poll: |
| 1483 |
nfc_err(dev->dev, "autopoll operation has been stopped\n"); |
| 1484 |
|
| 1485 |
pn533_poll_reset_mod_list(dev); |
| 1486 |
dev->poll_protocols = 0; |
| 1487 |
return rc; |
| 1488 |
} |
| 1489 |
|
| 1490 |
static int pn533_poll_complete(struct pn533 *dev, void *arg, |
| 1491 |
struct sk_buff *resp) |
| 1492 |
{ |
| 1493 |
struct pn533_poll_modulations *cur_mod; |
| 1494 |
int rc; |
| 1495 |
|
| 1496 |
if (IS_ERR(resp)) { |
| 1497 |
rc = PTR_ERR(resp); |
| 1498 |
|
| 1499 |
nfc_err(dev->dev, "%s Poll complete error %d\n", |
| 1500 |
__func__, rc); |
| 1501 |
|
| 1502 |
if (rc == -ENOENT) { |
| 1503 |
if (dev->poll_mod_count != 0) |
| 1504 |
return rc; |
| 1505 |
goto stop_poll; |
| 1506 |
} |
| 1507 |
nfc_err(dev->dev, "Error %d when running poll\n", rc); |
| 1508 |
goto stop_poll; |
| 1509 |
} |
| 1510 |
|
| 1511 |
cur_mod = dev->poll_mod_active[dev->poll_mod_curr]; |
| 1512 |
|
| 1513 |
if (cur_mod->len == 0) { /* Target mode */ |
| 1514 |
timer_delete(&dev->listen_timer); |
| 1515 |
rc = pn533_init_target_complete(dev, resp); |
| 1516 |
goto done; |
| 1517 |
} |
| 1518 |
|
| 1519 |
/* Initiator mode */ |
| 1520 |
rc = pn533_start_poll_complete(dev, resp); |
| 1521 |
if (!rc) |
| 1522 |
goto done; |
| 1523 |
|
| 1524 |
if (!dev->poll_mod_count) { |
| 1525 |
dev_dbg(dev->dev, "Polling has been stopped\n"); |
| 1526 |
goto done; |
| 1527 |
} |
| 1528 |
|
| 1529 |
pn533_poll_next_mod(dev); |
| 1530 |
/* Not target found, turn radio off */ |
| 1531 |
queue_work(dev->wq, &dev->rf_work); |
| 1532 |
|
| 1533 |
done: |
| 1534 |
dev_kfree_skb(resp); |
| 1535 |
return rc; |
| 1536 |
|
| 1537 |
stop_poll: |
| 1538 |
nfc_err(dev->dev, "Polling operation has been stopped\n"); |
| 1539 |
|
| 1540 |
pn533_poll_reset_mod_list(dev); |
| 1541 |
dev->poll_protocols = 0; |
| 1542 |
return rc; |
| 1543 |
} |
| 1544 |
|
| 1545 |
static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev, |
| 1546 |
struct pn533_poll_modulations *mod) |
| 1547 |
{ |
| 1548 |
struct sk_buff *skb; |
| 1549 |
|
| 1550 |
skb = pn533_alloc_skb(dev, mod->len); |
| 1551 |
if (!skb) |
| 1552 |
return NULL; |
| 1553 |
|
| 1554 |
skb_put_data(skb, &mod->data, mod->len); |
| 1555 |
|
| 1556 |
return skb; |
| 1557 |
} |
| 1558 |
|
| 1559 |
static int pn533_send_poll_frame(struct pn533 *dev) |
| 1560 |
{ |
| 1561 |
struct pn533_poll_modulations *mod; |
| 1562 |
struct sk_buff *skb; |
| 1563 |
int rc; |
| 1564 |
u8 cmd_code; |
| 1565 |
|
| 1566 |
mod = dev->poll_mod_active[dev->poll_mod_curr]; |
| 1567 |
|
| 1568 |
dev_dbg(dev->dev, "%s mod len %d\n", |
| 1569 |
__func__, mod->len); |
| 1570 |
|
| 1571 |
if ((dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK) && dev->poll_dep) { |
| 1572 |
dev->poll_dep = 0; |
| 1573 |
return pn533_poll_dep(dev->nfc_dev); |
| 1574 |
} |
| 1575 |
|
| 1576 |
if (mod->len == 0) { /* Listen mode */ |
| 1577 |
cmd_code = PN533_CMD_TG_INIT_AS_TARGET; |
| 1578 |
skb = pn533_alloc_poll_tg_frame(dev); |
| 1579 |
} else { /* Polling mode */ |
| 1580 |
cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET; |
| 1581 |
skb = pn533_alloc_poll_in_frame(dev, mod); |
| 1582 |
} |
| 1583 |
|
| 1584 |
if (!skb) { |
| 1585 |
nfc_err(dev->dev, "Failed to allocate skb\n"); |
| 1586 |
return -ENOMEM; |
| 1587 |
} |
| 1588 |
|
| 1589 |
rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete, |
| 1590 |
NULL); |
| 1591 |
if (rc < 0) { |
| 1592 |
dev_kfree_skb(skb); |
| 1593 |
nfc_err(dev->dev, "Polling loop error %d\n", rc); |
| 1594 |
} |
| 1595 |
|
| 1596 |
return rc; |
| 1597 |
} |
| 1598 |
|
| 1599 |
static void pn533_wq_poll(struct work_struct *work) |
| 1600 |
{ |
| 1601 |
struct pn533 *dev = container_of(work, struct pn533, poll_work.work); |
| 1602 |
struct pn533_poll_modulations *cur_mod; |
| 1603 |
int rc; |
| 1604 |
|
| 1605 |
cur_mod = dev->poll_mod_active[dev->poll_mod_curr]; |
| 1606 |
|
| 1607 |
dev_dbg(dev->dev, |
| 1608 |
"%s cancel_listen %d modulation len %d\n", |
| 1609 |
__func__, dev->cancel_listen, cur_mod->len); |
| 1610 |
|
| 1611 |
if (dev->cancel_listen == 1) { |
| 1612 |
dev->cancel_listen = 0; |
| 1613 |
dev->phy_ops->abort_cmd(dev, GFP_ATOMIC); |
| 1614 |
} |
| 1615 |
|
| 1616 |
rc = pn533_send_poll_frame(dev); |
| 1617 |
if (rc) |
| 1618 |
return; |
| 1619 |
|
| 1620 |
if (cur_mod->len == 0 && dev->poll_mod_count > 1) |
| 1621 |
mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ); |
| 1622 |
} |
| 1623 |
|
| 1624 |
static int pn533_start_poll(struct nfc_dev *nfc_dev, |
| 1625 |
u32 im_protocols, u32 tm_protocols) |
| 1626 |
{ |
| 1627 |
struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 1628 |
struct pn533_poll_modulations *cur_mod; |
| 1629 |
struct sk_buff *skb; |
| 1630 |
u8 rand_mod; |
| 1631 |
int rc; |
| 1632 |
|
| 1633 |
dev_dbg(dev->dev, |
| 1634 |
"%s: im protocols 0x%x tm protocols 0x%x\n", |
| 1635 |
__func__, im_protocols, tm_protocols); |
| 1636 |
|
| 1637 |
if (dev->tgt_active_prot) { |
| 1638 |
nfc_err(dev->dev, |
| 1639 |
"Cannot poll with a target already activated\n"); |
| 1640 |
return -EBUSY; |
| 1641 |
} |
| 1642 |
|
| 1643 |
if (dev->tgt_mode) { |
| 1644 |
nfc_err(dev->dev, |
| 1645 |
"Cannot poll while already being activated\n"); |
| 1646 |
return -EBUSY; |
| 1647 |
} |
| 1648 |
|
| 1649 |
if (tm_protocols) { |
| 1650 |
dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len); |
| 1651 |
if (dev->gb == NULL) |
| 1652 |
tm_protocols = 0; |
| 1653 |
} |
| 1654 |
|
| 1655 |
dev->poll_protocols = im_protocols; |
| 1656 |
dev->listen_protocols = tm_protocols; |
| 1657 |
if (dev->device_type == PN533_DEVICE_PN532_AUTOPOLL) { |
| 1658 |
skb = pn533_alloc_skb(dev, 4 + 6); |
| 1659 |
if (!skb) |
| 1660 |
return -ENOMEM; |
| 1661 |
|
| 1662 |
*((u8 *)skb_put(skb, sizeof(u8))) = |
| 1663 |
PN532_AUTOPOLL_POLLNR_INFINITE; |
| 1664 |
*((u8 *)skb_put(skb, sizeof(u8))) = PN532_AUTOPOLL_PERIOD; |
| 1665 |
|
| 1666 |
if ((im_protocols & NFC_PROTO_MIFARE_MASK) && |
| 1667 |
(im_protocols & NFC_PROTO_ISO14443_MASK) && |
| 1668 |
(im_protocols & NFC_PROTO_NFC_DEP_MASK)) |
| 1669 |
*((u8 *)skb_put(skb, sizeof(u8))) = |
| 1670 |
PN532_AUTOPOLL_TYPE_GENERIC_106; |
| 1671 |
else { |
| 1672 |
if (im_protocols & NFC_PROTO_MIFARE_MASK) |
| 1673 |
*((u8 *)skb_put(skb, sizeof(u8))) = |
| 1674 |
PN532_AUTOPOLL_TYPE_MIFARE; |
| 1675 |
|
| 1676 |
if (im_protocols & NFC_PROTO_ISO14443_MASK) |
| 1677 |
*((u8 *)skb_put(skb, sizeof(u8))) = |
| 1678 |
PN532_AUTOPOLL_TYPE_ISOA; |
| 1679 |
|
| 1680 |
if (im_protocols & NFC_PROTO_NFC_DEP_MASK) { |
| 1681 |
*((u8 *)skb_put(skb, sizeof(u8))) = |
| 1682 |
PN532_AUTOPOLL_TYPE_DEP_PASSIVE_106; |
| 1683 |
*((u8 *)skb_put(skb, sizeof(u8))) = |
| 1684 |
PN532_AUTOPOLL_TYPE_DEP_PASSIVE_212; |
| 1685 |
*((u8 *)skb_put(skb, sizeof(u8))) = |
| 1686 |
PN532_AUTOPOLL_TYPE_DEP_PASSIVE_424; |
| 1687 |
} |
| 1688 |
} |
| 1689 |
|
| 1690 |
if (im_protocols & NFC_PROTO_FELICA_MASK || |
| 1691 |
im_protocols & NFC_PROTO_NFC_DEP_MASK) { |
| 1692 |
*((u8 *)skb_put(skb, sizeof(u8))) = |
| 1693 |
PN532_AUTOPOLL_TYPE_FELICA212; |
| 1694 |
*((u8 *)skb_put(skb, sizeof(u8))) = |
| 1695 |
PN532_AUTOPOLL_TYPE_FELICA424; |
| 1696 |
} |
| 1697 |
|
| 1698 |
if (im_protocols & NFC_PROTO_JEWEL_MASK) |
| 1699 |
*((u8 *)skb_put(skb, sizeof(u8))) = |
| 1700 |
PN532_AUTOPOLL_TYPE_JEWEL; |
| 1701 |
|
| 1702 |
if (im_protocols & NFC_PROTO_ISO14443_B_MASK) |
| 1703 |
*((u8 *)skb_put(skb, sizeof(u8))) = |
| 1704 |
PN532_AUTOPOLL_TYPE_ISOB; |
| 1705 |
|
| 1706 |
if (tm_protocols) |
| 1707 |
*((u8 *)skb_put(skb, sizeof(u8))) = |
| 1708 |
PN532_AUTOPOLL_TYPE_DEP_ACTIVE_106; |
| 1709 |
|
| 1710 |
rc = pn533_send_cmd_async(dev, PN533_CMD_IN_AUTOPOLL, skb, |
| 1711 |
pn533_autopoll_complete, NULL); |
| 1712 |
|
| 1713 |
if (rc < 0) |
| 1714 |
dev_kfree_skb(skb); |
| 1715 |
else |
| 1716 |
dev->poll_mod_count++; |
| 1717 |
|
| 1718 |
return rc; |
| 1719 |
} |
| 1720 |
|
| 1721 |
pn533_poll_create_mod_list(dev, im_protocols, tm_protocols); |
| 1722 |
if (!dev->poll_mod_count) { |
| 1723 |
nfc_err(dev->dev, |
| 1724 |
"Poll mod list is empty\n"); |
| 1725 |
return -EINVAL; |
| 1726 |
} |
| 1727 |
|
| 1728 |
/* Do not always start polling from the same modulation */ |
| 1729 |
get_random_bytes(&rand_mod, sizeof(rand_mod)); |
| 1730 |
rand_mod %= dev->poll_mod_count; |
| 1731 |
dev->poll_mod_curr = rand_mod; |
| 1732 |
|
| 1733 |
cur_mod = dev->poll_mod_active[dev->poll_mod_curr]; |
| 1734 |
|
| 1735 |
rc = pn533_send_poll_frame(dev); |
| 1736 |
|
| 1737 |
/* Start listen timer */ |
| 1738 |
if (!rc && cur_mod->len == 0 && dev->poll_mod_count > 1) |
| 1739 |
mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ); |
| 1740 |
|
| 1741 |
return rc; |
| 1742 |
} |
| 1743 |
|
| 1744 |
static void pn533_stop_poll(struct nfc_dev *nfc_dev) |
| 1745 |
{ |
| 1746 |
struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 1747 |
|
| 1748 |
timer_delete(&dev->listen_timer); |
| 1749 |
|
| 1750 |
if (!dev->poll_mod_count) { |
| 1751 |
dev_dbg(dev->dev, |
| 1752 |
"Polling operation was not running\n"); |
| 1753 |
return; |
| 1754 |
} |
| 1755 |
|
| 1756 |
dev->phy_ops->abort_cmd(dev, GFP_KERNEL); |
| 1757 |
flush_delayed_work(&dev->poll_work); |
| 1758 |
pn533_poll_reset_mod_list(dev); |
| 1759 |
} |
| 1760 |
|
| 1761 |
static int pn533_activate_target_nfcdep(struct pn533 *dev) |
| 1762 |
{ |
| 1763 |
struct pn533_cmd_activate_response *rsp; |
| 1764 |
u16 gt_len; |
| 1765 |
int rc; |
| 1766 |
struct sk_buff *skb; |
| 1767 |
struct sk_buff *resp; |
| 1768 |
|
| 1769 |
skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/ |
| 1770 |
if (!skb) |
| 1771 |
return -ENOMEM; |
| 1772 |
|
| 1773 |
skb_put_u8(skb, 1); /* TG */ |
| 1774 |
skb_put_u8(skb, 0); /* Next */ |
| 1775 |
|
| 1776 |
resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb); |
| 1777 |
if (IS_ERR(resp)) |
| 1778 |
return PTR_ERR(resp); |
| 1779 |
|
| 1780 |
rsp = (struct pn533_cmd_activate_response *)resp->data; |
| 1781 |
rc = rsp->status & PN533_CMD_RET_MASK; |
| 1782 |
if (rc != PN533_CMD_RET_SUCCESS) { |
| 1783 |
nfc_err(dev->dev, |
| 1784 |
"Target activation failed (error 0x%x)\n", rc); |
| 1785 |
dev_kfree_skb(resp); |
| 1786 |
return -EIO; |
| 1787 |
} |
| 1788 |
|
| 1789 |
/* ATR_RES general bytes are located at offset 16 */ |
| 1790 |
gt_len = resp->len - 16; |
| 1791 |
rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len); |
| 1792 |
|
| 1793 |
dev_kfree_skb(resp); |
| 1794 |
return rc; |
| 1795 |
} |
| 1796 |
|
| 1797 |
static int pn533_activate_target(struct nfc_dev *nfc_dev, |
| 1798 |
struct nfc_target *target, u32 protocol) |
| 1799 |
{ |
| 1800 |
struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 1801 |
int rc; |
| 1802 |
|
| 1803 |
dev_dbg(dev->dev, "%s: protocol=%u\n", __func__, protocol); |
| 1804 |
|
| 1805 |
if (dev->poll_mod_count) { |
| 1806 |
nfc_err(dev->dev, |
| 1807 |
"Cannot activate while polling\n"); |
| 1808 |
return -EBUSY; |
| 1809 |
} |
| 1810 |
|
| 1811 |
if (dev->tgt_active_prot) { |
| 1812 |
nfc_err(dev->dev, |
| 1813 |
"There is already an active target\n"); |
| 1814 |
return -EBUSY; |
| 1815 |
} |
| 1816 |
|
| 1817 |
if (!dev->tgt_available_prots) { |
| 1818 |
nfc_err(dev->dev, |
| 1819 |
"There is no available target to activate\n"); |
| 1820 |
return -EINVAL; |
| 1821 |
} |
| 1822 |
|
| 1823 |
if (!(dev->tgt_available_prots & (1 << protocol))) { |
| 1824 |
nfc_err(dev->dev, |
| 1825 |
"Target doesn't support requested proto %u\n", |
| 1826 |
protocol); |
| 1827 |
return -EINVAL; |
| 1828 |
} |
| 1829 |
|
| 1830 |
if (protocol == NFC_PROTO_NFC_DEP) { |
| 1831 |
rc = pn533_activate_target_nfcdep(dev); |
| 1832 |
if (rc) { |
| 1833 |
nfc_err(dev->dev, |
| 1834 |
"Activating target with DEP failed %d\n", rc); |
| 1835 |
return rc; |
| 1836 |
} |
| 1837 |
} |
| 1838 |
|
| 1839 |
dev->tgt_active_prot = protocol; |
| 1840 |
dev->tgt_available_prots = 0; |
| 1841 |
|
| 1842 |
return 0; |
| 1843 |
} |
| 1844 |
|
| 1845 |
static int pn533_deactivate_target_complete(struct pn533 *dev, void *arg, |
| 1846 |
struct sk_buff *resp) |
| 1847 |
{ |
| 1848 |
int rc = 0; |
| 1849 |
|
| 1850 |
if (IS_ERR(resp)) { |
| 1851 |
rc = PTR_ERR(resp); |
| 1852 |
|
| 1853 |
nfc_err(dev->dev, "Target release error %d\n", rc); |
| 1854 |
|
| 1855 |
return rc; |
| 1856 |
} |
| 1857 |
|
| 1858 |
rc = resp->data[0] & PN533_CMD_RET_MASK; |
| 1859 |
if (rc != PN533_CMD_RET_SUCCESS) |
| 1860 |
nfc_err(dev->dev, |
| 1861 |
"Error 0x%x when releasing the target\n", rc); |
| 1862 |
|
| 1863 |
dev_kfree_skb(resp); |
| 1864 |
return rc; |
| 1865 |
} |
| 1866 |
|
| 1867 |
static void pn533_deactivate_target(struct nfc_dev *nfc_dev, |
| 1868 |
struct nfc_target *target, u8 mode) |
| 1869 |
{ |
| 1870 |
struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 1871 |
struct sk_buff *skb; |
| 1872 |
int rc; |
| 1873 |
|
| 1874 |
if (!dev->tgt_active_prot) { |
| 1875 |
nfc_err(dev->dev, "There is no active target\n"); |
| 1876 |
return; |
| 1877 |
} |
| 1878 |
|
| 1879 |
dev->tgt_active_prot = 0; |
| 1880 |
skb_queue_purge(&dev->resp_q); |
| 1881 |
|
| 1882 |
skb = pn533_alloc_skb(dev, sizeof(u8)); |
| 1883 |
if (!skb) |
| 1884 |
return; |
| 1885 |
|
| 1886 |
skb_put_u8(skb, 1); /* TG*/ |
| 1887 |
|
| 1888 |
rc = pn533_send_cmd_async(dev, PN533_CMD_IN_RELEASE, skb, |
| 1889 |
pn533_deactivate_target_complete, NULL); |
| 1890 |
if (rc < 0) { |
| 1891 |
dev_kfree_skb(skb); |
| 1892 |
nfc_err(dev->dev, "Target release error %d\n", rc); |
| 1893 |
} |
| 1894 |
} |
| 1895 |
|
| 1896 |
|
| 1897 |
static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg, |
| 1898 |
struct sk_buff *resp) |
| 1899 |
{ |
| 1900 |
struct pn533_cmd_jump_dep_response *rsp; |
| 1901 |
u8 target_gt_len; |
| 1902 |
int rc; |
| 1903 |
u8 active = *(u8 *)arg; |
| 1904 |
|
| 1905 |
kfree(arg); |
| 1906 |
|
| 1907 |
if (IS_ERR(resp)) |
| 1908 |
return PTR_ERR(resp); |
| 1909 |
|
| 1910 |
if (dev->tgt_available_prots && |
| 1911 |
!(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) { |
| 1912 |
nfc_err(dev->dev, |
| 1913 |
"The target does not support DEP\n"); |
| 1914 |
rc = -EINVAL; |
| 1915 |
goto error; |
| 1916 |
} |
| 1917 |
|
| 1918 |
rsp = (struct pn533_cmd_jump_dep_response *)resp->data; |
| 1919 |
|
| 1920 |
rc = rsp->status & PN533_CMD_RET_MASK; |
| 1921 |
if (rc != PN533_CMD_RET_SUCCESS) { |
| 1922 |
nfc_err(dev->dev, |
| 1923 |
"Bringing DEP link up failed (error 0x%x)\n", rc); |
| 1924 |
goto error; |
| 1925 |
} |
| 1926 |
|
| 1927 |
if (!dev->tgt_available_prots) { |
| 1928 |
struct nfc_target nfc_target; |
| 1929 |
|
| 1930 |
dev_dbg(dev->dev, "Creating new target\n"); |
| 1931 |
|
| 1932 |
memset(&nfc_target, 0, sizeof(struct nfc_target)); |
| 1933 |
|
| 1934 |
nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK; |
| 1935 |
nfc_target.nfcid1_len = 10; |
| 1936 |
memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len); |
| 1937 |
rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1); |
| 1938 |
if (rc) |
| 1939 |
goto error; |
| 1940 |
|
| 1941 |
dev->tgt_available_prots = 0; |
| 1942 |
} |
| 1943 |
|
| 1944 |
dev->tgt_active_prot = NFC_PROTO_NFC_DEP; |
| 1945 |
|
| 1946 |
/* ATR_RES general bytes are located at offset 17 */ |
| 1947 |
target_gt_len = resp->len - 17; |
| 1948 |
rc = nfc_set_remote_general_bytes(dev->nfc_dev, |
| 1949 |
rsp->gt, target_gt_len); |
| 1950 |
if (rc == 0) |
| 1951 |
rc = nfc_dep_link_is_up(dev->nfc_dev, |
| 1952 |
dev->nfc_dev->targets[0].idx, |
| 1953 |
!active, NFC_RF_INITIATOR); |
| 1954 |
|
| 1955 |
error: |
| 1956 |
dev_kfree_skb(resp); |
| 1957 |
return rc; |
| 1958 |
} |
| 1959 |
|
| 1960 |
static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf); |
| 1961 |
static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target, |
| 1962 |
u8 comm_mode, u8 *gb, size_t gb_len) |
| 1963 |
{ |
| 1964 |
struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 1965 |
struct sk_buff *skb; |
| 1966 |
int rc, skb_len; |
| 1967 |
u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE]; |
| 1968 |
u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3}; |
| 1969 |
|
| 1970 |
if (dev->poll_mod_count) { |
| 1971 |
nfc_err(dev->dev, |
| 1972 |
"Cannot bring the DEP link up while polling\n"); |
| 1973 |
return -EBUSY; |
| 1974 |
} |
| 1975 |
|
| 1976 |
if (dev->tgt_active_prot) { |
| 1977 |
nfc_err(dev->dev, |
| 1978 |
"There is already an active target\n"); |
| 1979 |
return -EBUSY; |
| 1980 |
} |
| 1981 |
|
| 1982 |
skb_len = 3 + gb_len; /* ActPass + BR + Next */ |
| 1983 |
skb_len += PASSIVE_DATA_LEN; |
| 1984 |
|
| 1985 |
/* NFCID3 */ |
| 1986 |
skb_len += NFC_NFCID3_MAXSIZE; |
| 1987 |
if (target && !target->nfcid2_len) { |
| 1988 |
nfcid3[0] = 0x1; |
| 1989 |
nfcid3[1] = 0xfe; |
| 1990 |
get_random_bytes(nfcid3 + 2, 6); |
| 1991 |
} |
| 1992 |
|
| 1993 |
skb = pn533_alloc_skb(dev, skb_len); |
| 1994 |
if (!skb) |
| 1995 |
return -ENOMEM; |
| 1996 |
|
| 1997 |
skb_put_u8(skb, !comm_mode); /* ActPass */ |
| 1998 |
skb_put_u8(skb, 0x02); /* 424 kbps */ |
| 1999 |
|
| 2000 |
next = skb_put(skb, 1); /* Next */ |
| 2001 |
*next = 0; |
| 2002 |
|
| 2003 |
/* Copy passive data */ |
| 2004 |
skb_put_data(skb, passive_data, PASSIVE_DATA_LEN); |
| 2005 |
*next |= 1; |
| 2006 |
|
| 2007 |
/* Copy NFCID3 (which is NFCID2 from SENSF_RES) */ |
| 2008 |
if (target && target->nfcid2_len) |
| 2009 |
memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2, |
| 2010 |
target->nfcid2_len); |
| 2011 |
else |
| 2012 |
skb_put_data(skb, nfcid3, NFC_NFCID3_MAXSIZE); |
| 2013 |
*next |= 2; |
| 2014 |
|
| 2015 |
if (gb != NULL && gb_len > 0) { |
| 2016 |
skb_put_data(skb, gb, gb_len); |
| 2017 |
*next |= 4; /* We have some Gi */ |
| 2018 |
} else { |
| 2019 |
*next = 0; |
| 2020 |
} |
| 2021 |
|
| 2022 |
arg = kmalloc_obj(*arg); |
| 2023 |
if (!arg) { |
| 2024 |
dev_kfree_skb(skb); |
| 2025 |
return -ENOMEM; |
| 2026 |
} |
| 2027 |
|
| 2028 |
*arg = !comm_mode; |
| 2029 |
|
| 2030 |
pn533_rf_field(dev->nfc_dev, 0); |
| 2031 |
|
| 2032 |
rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb, |
| 2033 |
pn533_in_dep_link_up_complete, arg); |
| 2034 |
|
| 2035 |
if (rc < 0) { |
| 2036 |
dev_kfree_skb(skb); |
| 2037 |
kfree(arg); |
| 2038 |
} |
| 2039 |
|
| 2040 |
return rc; |
| 2041 |
} |
| 2042 |
|
| 2043 |
static int pn533_dep_link_down(struct nfc_dev *nfc_dev) |
| 2044 |
{ |
| 2045 |
struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 2046 |
|
| 2047 |
pn533_poll_reset_mod_list(dev); |
| 2048 |
|
| 2049 |
if (dev->tgt_mode || dev->tgt_active_prot) |
| 2050 |
dev->phy_ops->abort_cmd(dev, GFP_KERNEL); |
| 2051 |
|
| 2052 |
dev->tgt_active_prot = 0; |
| 2053 |
dev->tgt_mode = 0; |
| 2054 |
|
| 2055 |
skb_queue_purge(&dev->resp_q); |
| 2056 |
|
| 2057 |
return 0; |
| 2058 |
} |
| 2059 |
|
| 2060 |
struct pn533_data_exchange_arg { |
| 2061 |
data_exchange_cb_t cb; |
| 2062 |
void *cb_context; |
| 2063 |
}; |
| 2064 |
|
| 2065 |
static struct sk_buff *pn533_build_response(struct pn533 *dev) |
| 2066 |
{ |
| 2067 |
struct sk_buff *skb, *tmp, *t; |
| 2068 |
unsigned int skb_len = 0, tmp_len = 0; |
| 2069 |
|
| 2070 |
if (skb_queue_empty(&dev->resp_q)) |
| 2071 |
return NULL; |
| 2072 |
|
| 2073 |
if (skb_queue_len(&dev->resp_q) == 1) { |
| 2074 |
skb = skb_dequeue(&dev->resp_q); |
| 2075 |
goto out; |
| 2076 |
} |
| 2077 |
|
| 2078 |
skb_queue_walk_safe(&dev->resp_q, tmp, t) |
| 2079 |
skb_len += tmp->len; |
| 2080 |
|
| 2081 |
dev_dbg(dev->dev, "%s total length %d\n", |
| 2082 |
__func__, skb_len); |
| 2083 |
|
| 2084 |
skb = alloc_skb(skb_len, GFP_KERNEL); |
| 2085 |
if (skb == NULL) |
| 2086 |
goto out; |
| 2087 |
|
| 2088 |
skb_put(skb, skb_len); |
| 2089 |
|
| 2090 |
skb_queue_walk_safe(&dev->resp_q, tmp, t) { |
| 2091 |
memcpy(skb->data + tmp_len, tmp->data, tmp->len); |
| 2092 |
tmp_len += tmp->len; |
| 2093 |
} |
| 2094 |
|
| 2095 |
out: |
| 2096 |
skb_queue_purge(&dev->resp_q); |
| 2097 |
|
| 2098 |
return skb; |
| 2099 |
} |
| 2100 |
|
| 2101 |
static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg, |
| 2102 |
struct sk_buff *resp) |
| 2103 |
{ |
| 2104 |
struct pn533_data_exchange_arg *arg = _arg; |
| 2105 |
struct sk_buff *skb; |
| 2106 |
int rc = 0; |
| 2107 |
u8 status, ret, mi; |
| 2108 |
|
| 2109 |
if (IS_ERR(resp)) { |
| 2110 |
rc = PTR_ERR(resp); |
| 2111 |
goto _error; |
| 2112 |
} |
| 2113 |
|
| 2114 |
status = resp->data[0]; |
| 2115 |
ret = status & PN533_CMD_RET_MASK; |
| 2116 |
mi = status & PN533_CMD_MI_MASK; |
| 2117 |
|
| 2118 |
skb_pull(resp, sizeof(status)); |
| 2119 |
|
| 2120 |
if (ret != PN533_CMD_RET_SUCCESS) { |
| 2121 |
nfc_err(dev->dev, |
| 2122 |
"Exchanging data failed (error 0x%x)\n", ret); |
| 2123 |
rc = -EIO; |
| 2124 |
goto error; |
| 2125 |
} |
| 2126 |
|
| 2127 |
skb_queue_tail(&dev->resp_q, resp); |
| 2128 |
|
| 2129 |
if (mi) { |
| 2130 |
dev->cmd_complete_mi_arg = arg; |
| 2131 |
queue_work(dev->wq, &dev->mi_rx_work); |
| 2132 |
return -EINPROGRESS; |
| 2133 |
} |
| 2134 |
|
| 2135 |
/* Prepare for the next round */ |
| 2136 |
if (skb_queue_len(&dev->fragment_skb) > 0) { |
| 2137 |
dev->cmd_complete_dep_arg = arg; |
| 2138 |
queue_work(dev->wq, &dev->mi_tx_work); |
| 2139 |
|
| 2140 |
return -EINPROGRESS; |
| 2141 |
} |
| 2142 |
|
| 2143 |
skb = pn533_build_response(dev); |
| 2144 |
if (!skb) { |
| 2145 |
rc = -ENOMEM; |
| 2146 |
goto error; |
| 2147 |
} |
| 2148 |
|
| 2149 |
arg->cb(arg->cb_context, skb, 0); |
| 2150 |
kfree(arg); |
| 2151 |
return 0; |
| 2152 |
|
| 2153 |
error: |
| 2154 |
dev_kfree_skb(resp); |
| 2155 |
_error: |
| 2156 |
skb_queue_purge(&dev->resp_q); |
| 2157 |
arg->cb(arg->cb_context, NULL, rc); |
| 2158 |
kfree(arg); |
| 2159 |
return rc; |
| 2160 |
} |
| 2161 |
|
| 2162 |
/* |
| 2163 |
* Receive an incoming pn533 frame. skb contains only header and payload. |
| 2164 |
* If skb == NULL, it is a notification that the link below is dead. |
| 2165 |
*/ |
| 2166 |
void pn533_recv_frame(struct pn533 *dev, struct sk_buff *skb, int status) |
| 2167 |
{ |
| 2168 |
if (!dev->cmd) |
| 2169 |
goto sched_wq; |
| 2170 |
|
| 2171 |
dev->cmd->status = status; |
| 2172 |
|
| 2173 |
if (status != 0) { |
| 2174 |
dev_dbg(dev->dev, "%s: Error received: %d\n", __func__, status); |
| 2175 |
goto sched_wq; |
| 2176 |
} |
| 2177 |
|
| 2178 |
if (skb == NULL) { |
| 2179 |
dev_err(dev->dev, "NULL Frame -> link is dead\n"); |
| 2180 |
goto sched_wq; |
| 2181 |
} |
| 2182 |
|
| 2183 |
if (pn533_rx_frame_is_ack(skb->data)) { |
| 2184 |
dev_dbg(dev->dev, "%s: Received ACK frame\n", __func__); |
| 2185 |
dev_kfree_skb(skb); |
| 2186 |
return; |
| 2187 |
} |
| 2188 |
|
| 2189 |
print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, skb->data, |
| 2190 |
dev->ops->rx_frame_size(skb->data), false); |
| 2191 |
|
| 2192 |
if (!dev->ops->rx_is_frame_valid(skb->data, dev)) { |
| 2193 |
nfc_err(dev->dev, "Received an invalid frame\n"); |
| 2194 |
dev->cmd->status = -EIO; |
| 2195 |
} else if (!pn533_rx_frame_is_cmd_response(dev, skb->data)) { |
| 2196 |
nfc_err(dev->dev, "It it not the response to the last command\n"); |
| 2197 |
dev->cmd->status = -EIO; |
| 2198 |
} |
| 2199 |
|
| 2200 |
dev->cmd->resp = skb; |
| 2201 |
|
| 2202 |
sched_wq: |
| 2203 |
queue_work(dev->wq, &dev->cmd_complete_work); |
| 2204 |
} |
| 2205 |
EXPORT_SYMBOL(pn533_recv_frame); |
| 2206 |
|
| 2207 |
/* Split the Tx skb into small chunks */ |
| 2208 |
static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb) |
| 2209 |
{ |
| 2210 |
struct sk_buff *frag; |
| 2211 |
int frag_size; |
| 2212 |
|
| 2213 |
do { |
| 2214 |
/* Remaining size */ |
| 2215 |
if (skb->len > PN533_CMD_DATAFRAME_MAXLEN) |
| 2216 |
frag_size = PN533_CMD_DATAFRAME_MAXLEN; |
| 2217 |
else |
| 2218 |
frag_size = skb->len; |
| 2219 |
|
| 2220 |
/* Allocate and reserve */ |
| 2221 |
frag = pn533_alloc_skb(dev, frag_size); |
| 2222 |
if (!frag) { |
| 2223 |
skb_queue_purge(&dev->fragment_skb); |
| 2224 |
return -ENOMEM; |
| 2225 |
} |
| 2226 |
|
| 2227 |
if (!dev->tgt_mode) { |
| 2228 |
/* Reserve the TG/MI byte */ |
| 2229 |
skb_reserve(frag, 1); |
| 2230 |
|
| 2231 |
/* MI + TG */ |
| 2232 |
if (frag_size == PN533_CMD_DATAFRAME_MAXLEN) |
| 2233 |
*(u8 *)skb_push(frag, sizeof(u8)) = |
| 2234 |
(PN533_CMD_MI_MASK | 1); |
| 2235 |
else |
| 2236 |
*(u8 *)skb_push(frag, sizeof(u8)) = 1; /* TG */ |
| 2237 |
} |
| 2238 |
|
| 2239 |
skb_put_data(frag, skb->data, frag_size); |
| 2240 |
|
| 2241 |
/* Reduce the size of incoming buffer */ |
| 2242 |
skb_pull(skb, frag_size); |
| 2243 |
|
| 2244 |
/* Add this to skb_queue */ |
| 2245 |
skb_queue_tail(&dev->fragment_skb, frag); |
| 2246 |
|
| 2247 |
} while (skb->len > 0); |
| 2248 |
|
| 2249 |
dev_kfree_skb(skb); |
| 2250 |
|
| 2251 |
return skb_queue_len(&dev->fragment_skb); |
| 2252 |
} |
| 2253 |
|
| 2254 |
static int pn533_transceive(struct nfc_dev *nfc_dev, |
| 2255 |
struct nfc_target *target, struct sk_buff *skb, |
| 2256 |
data_exchange_cb_t cb, void *cb_context) |
| 2257 |
{ |
| 2258 |
struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 2259 |
struct pn533_data_exchange_arg *arg = NULL; |
| 2260 |
int rc; |
| 2261 |
|
| 2262 |
if (!dev->tgt_active_prot) { |
| 2263 |
nfc_err(dev->dev, |
| 2264 |
"Can't exchange data if there is no active target\n"); |
| 2265 |
rc = -EINVAL; |
| 2266 |
goto error; |
| 2267 |
} |
| 2268 |
|
| 2269 |
arg = kmalloc_obj(*arg); |
| 2270 |
if (!arg) { |
| 2271 |
rc = -ENOMEM; |
| 2272 |
goto error; |
| 2273 |
} |
| 2274 |
|
| 2275 |
arg->cb = cb; |
| 2276 |
arg->cb_context = cb_context; |
| 2277 |
|
| 2278 |
switch (dev->device_type) { |
| 2279 |
case PN533_DEVICE_PASORI: |
| 2280 |
if (dev->tgt_active_prot == NFC_PROTO_FELICA) { |
| 2281 |
rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU, |
| 2282 |
skb, |
| 2283 |
pn533_data_exchange_complete, |
| 2284 |
arg); |
| 2285 |
|
| 2286 |
break; |
| 2287 |
} |
| 2288 |
fallthrough; |
| 2289 |
default: |
| 2290 |
/* jumbo frame ? */ |
| 2291 |
if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) { |
| 2292 |
rc = pn533_fill_fragment_skbs(dev, skb); |
| 2293 |
if (rc < 0) |
| 2294 |
goto error; |
| 2295 |
|
| 2296 |
skb = skb_dequeue(&dev->fragment_skb); |
| 2297 |
if (!skb) { |
| 2298 |
rc = -EIO; |
| 2299 |
goto error; |
| 2300 |
} |
| 2301 |
} else { |
| 2302 |
*(u8 *)skb_push(skb, sizeof(u8)) = 1; /* TG */ |
| 2303 |
} |
| 2304 |
|
| 2305 |
rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE, |
| 2306 |
skb, pn533_data_exchange_complete, |
| 2307 |
arg); |
| 2308 |
|
| 2309 |
break; |
| 2310 |
} |
| 2311 |
|
| 2312 |
if (rc < 0) /* rc from send_async */ |
| 2313 |
goto error; |
| 2314 |
|
| 2315 |
return 0; |
| 2316 |
|
| 2317 |
error: |
| 2318 |
kfree(arg); |
| 2319 |
dev_kfree_skb(skb); |
| 2320 |
return rc; |
| 2321 |
} |
| 2322 |
|
| 2323 |
static int pn533_tm_send_complete(struct pn533 *dev, void *arg, |
| 2324 |
struct sk_buff *resp) |
| 2325 |
{ |
| 2326 |
u8 status; |
| 2327 |
|
| 2328 |
if (IS_ERR(resp)) |
| 2329 |
return PTR_ERR(resp); |
| 2330 |
|
| 2331 |
status = resp->data[0]; |
| 2332 |
|
| 2333 |
/* Prepare for the next round */ |
| 2334 |
if (skb_queue_len(&dev->fragment_skb) > 0) { |
| 2335 |
queue_work(dev->wq, &dev->mi_tm_tx_work); |
| 2336 |
return -EINPROGRESS; |
| 2337 |
} |
| 2338 |
dev_kfree_skb(resp); |
| 2339 |
|
| 2340 |
if (status != 0) { |
| 2341 |
nfc_tm_deactivated(dev->nfc_dev); |
| 2342 |
|
| 2343 |
dev->tgt_mode = 0; |
| 2344 |
|
| 2345 |
return 0; |
| 2346 |
} |
| 2347 |
|
| 2348 |
queue_work(dev->wq, &dev->tg_work); |
| 2349 |
|
| 2350 |
return 0; |
| 2351 |
} |
| 2352 |
|
| 2353 |
static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb) |
| 2354 |
{ |
| 2355 |
struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 2356 |
int rc; |
| 2357 |
|
| 2358 |
/* let's split in multiple chunks if size's too big */ |
| 2359 |
if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) { |
| 2360 |
rc = pn533_fill_fragment_skbs(dev, skb); |
| 2361 |
if (rc < 0) |
| 2362 |
goto error; |
| 2363 |
|
| 2364 |
/* get the first skb */ |
| 2365 |
skb = skb_dequeue(&dev->fragment_skb); |
| 2366 |
if (!skb) { |
| 2367 |
rc = -EIO; |
| 2368 |
goto error; |
| 2369 |
} |
| 2370 |
|
| 2371 |
rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_META_DATA, skb, |
| 2372 |
pn533_tm_send_complete, NULL); |
| 2373 |
} else { |
| 2374 |
/* Send th skb */ |
| 2375 |
rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb, |
| 2376 |
pn533_tm_send_complete, NULL); |
| 2377 |
} |
| 2378 |
|
| 2379 |
error: |
| 2380 |
if (rc < 0) { |
| 2381 |
dev_kfree_skb(skb); |
| 2382 |
skb_queue_purge(&dev->fragment_skb); |
| 2383 |
} |
| 2384 |
|
| 2385 |
return rc; |
| 2386 |
} |
| 2387 |
|
| 2388 |
static void pn533_wq_mi_recv(struct work_struct *work) |
| 2389 |
{ |
| 2390 |
struct pn533 *dev = container_of(work, struct pn533, mi_rx_work); |
| 2391 |
struct sk_buff *skb; |
| 2392 |
int rc; |
| 2393 |
|
| 2394 |
skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN); |
| 2395 |
if (!skb) |
| 2396 |
goto error; |
| 2397 |
|
| 2398 |
switch (dev->device_type) { |
| 2399 |
case PN533_DEVICE_PASORI: |
| 2400 |
if (dev->tgt_active_prot == NFC_PROTO_FELICA) { |
| 2401 |
rc = pn533_send_cmd_direct_async(dev, |
| 2402 |
PN533_CMD_IN_COMM_THRU, |
| 2403 |
skb, |
| 2404 |
pn533_data_exchange_complete, |
| 2405 |
dev->cmd_complete_mi_arg); |
| 2406 |
|
| 2407 |
break; |
| 2408 |
} |
| 2409 |
fallthrough; |
| 2410 |
default: |
| 2411 |
skb_put_u8(skb, 1); /*TG*/ |
| 2412 |
|
| 2413 |
rc = pn533_send_cmd_direct_async(dev, |
| 2414 |
PN533_CMD_IN_DATA_EXCHANGE, |
| 2415 |
skb, |
| 2416 |
pn533_data_exchange_complete, |
| 2417 |
dev->cmd_complete_mi_arg); |
| 2418 |
|
| 2419 |
break; |
| 2420 |
} |
| 2421 |
|
| 2422 |
if (rc == 0) /* success */ |
| 2423 |
return; |
| 2424 |
|
| 2425 |
nfc_err(dev->dev, |
| 2426 |
"Error %d when trying to perform data_exchange\n", rc); |
| 2427 |
|
| 2428 |
dev_kfree_skb(skb); |
| 2429 |
kfree(dev->cmd_complete_mi_arg); |
| 2430 |
|
| 2431 |
error: |
| 2432 |
dev->phy_ops->send_ack(dev, GFP_KERNEL); |
| 2433 |
queue_work(dev->wq, &dev->cmd_work); |
| 2434 |
} |
| 2435 |
|
| 2436 |
static void pn533_wq_mi_send(struct work_struct *work) |
| 2437 |
{ |
| 2438 |
struct pn533 *dev = container_of(work, struct pn533, mi_tx_work); |
| 2439 |
struct sk_buff *skb; |
| 2440 |
int rc; |
| 2441 |
|
| 2442 |
/* Grab the first skb in the queue */ |
| 2443 |
skb = skb_dequeue(&dev->fragment_skb); |
| 2444 |
|
| 2445 |
if (skb == NULL) { /* No more data */ |
| 2446 |
/* Reset the queue for future use */ |
| 2447 |
skb_queue_head_init(&dev->fragment_skb); |
| 2448 |
goto error; |
| 2449 |
} |
| 2450 |
|
| 2451 |
switch (dev->device_type) { |
| 2452 |
case PN533_DEVICE_PASORI: |
| 2453 |
if (dev->tgt_active_prot != NFC_PROTO_FELICA) { |
| 2454 |
rc = -EIO; |
| 2455 |
break; |
| 2456 |
} |
| 2457 |
|
| 2458 |
rc = pn533_send_cmd_direct_async(dev, PN533_CMD_IN_COMM_THRU, |
| 2459 |
skb, |
| 2460 |
pn533_data_exchange_complete, |
| 2461 |
dev->cmd_complete_dep_arg); |
| 2462 |
|
| 2463 |
break; |
| 2464 |
|
| 2465 |
default: |
| 2466 |
/* Still some fragments? */ |
| 2467 |
rc = pn533_send_cmd_direct_async(dev, |
| 2468 |
PN533_CMD_IN_DATA_EXCHANGE, |
| 2469 |
skb, |
| 2470 |
pn533_data_exchange_complete, |
| 2471 |
dev->cmd_complete_dep_arg); |
| 2472 |
|
| 2473 |
break; |
| 2474 |
} |
| 2475 |
|
| 2476 |
if (rc == 0) /* success */ |
| 2477 |
return; |
| 2478 |
|
| 2479 |
nfc_err(dev->dev, |
| 2480 |
"Error %d when trying to perform data_exchange\n", rc); |
| 2481 |
|
| 2482 |
dev_kfree_skb(skb); |
| 2483 |
kfree(dev->cmd_complete_dep_arg); |
| 2484 |
|
| 2485 |
error: |
| 2486 |
dev->phy_ops->send_ack(dev, GFP_KERNEL); |
| 2487 |
queue_work(dev->wq, &dev->cmd_work); |
| 2488 |
} |
| 2489 |
|
| 2490 |
static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata, |
| 2491 |
u8 cfgdata_len) |
| 2492 |
{ |
| 2493 |
struct sk_buff *skb; |
| 2494 |
struct sk_buff *resp; |
| 2495 |
int skb_len; |
| 2496 |
|
| 2497 |
skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */ |
| 2498 |
|
| 2499 |
skb = pn533_alloc_skb(dev, skb_len); |
| 2500 |
if (!skb) |
| 2501 |
return -ENOMEM; |
| 2502 |
|
| 2503 |
skb_put_u8(skb, cfgitem); |
| 2504 |
skb_put_data(skb, cfgdata, cfgdata_len); |
| 2505 |
|
| 2506 |
resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb); |
| 2507 |
if (IS_ERR(resp)) |
| 2508 |
return PTR_ERR(resp); |
| 2509 |
|
| 2510 |
dev_kfree_skb(resp); |
| 2511 |
return 0; |
| 2512 |
} |
| 2513 |
|
| 2514 |
static int pn533_get_firmware_version(struct pn533 *dev, |
| 2515 |
struct pn533_fw_version *fv) |
| 2516 |
{ |
| 2517 |
struct sk_buff *skb; |
| 2518 |
struct sk_buff *resp; |
| 2519 |
|
| 2520 |
skb = pn533_alloc_skb(dev, 0); |
| 2521 |
if (!skb) |
| 2522 |
return -ENOMEM; |
| 2523 |
|
| 2524 |
resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb); |
| 2525 |
if (IS_ERR(resp)) |
| 2526 |
return PTR_ERR(resp); |
| 2527 |
|
| 2528 |
fv->ic = resp->data[0]; |
| 2529 |
fv->ver = resp->data[1]; |
| 2530 |
fv->rev = resp->data[2]; |
| 2531 |
fv->support = resp->data[3]; |
| 2532 |
|
| 2533 |
dev_kfree_skb(resp); |
| 2534 |
return 0; |
| 2535 |
} |
| 2536 |
|
| 2537 |
static int pn533_pasori_fw_reset(struct pn533 *dev) |
| 2538 |
{ |
| 2539 |
struct sk_buff *skb; |
| 2540 |
struct sk_buff *resp; |
| 2541 |
|
| 2542 |
skb = pn533_alloc_skb(dev, sizeof(u8)); |
| 2543 |
if (!skb) |
| 2544 |
return -ENOMEM; |
| 2545 |
|
| 2546 |
skb_put_u8(skb, 0x1); |
| 2547 |
|
| 2548 |
resp = pn533_send_cmd_sync(dev, 0x18, skb); |
| 2549 |
if (IS_ERR(resp)) |
| 2550 |
return PTR_ERR(resp); |
| 2551 |
|
| 2552 |
dev_kfree_skb(resp); |
| 2553 |
|
| 2554 |
return 0; |
| 2555 |
} |
| 2556 |
|
| 2557 |
static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf) |
| 2558 |
{ |
| 2559 |
struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 2560 |
u8 rf_field = !!rf; |
| 2561 |
int rc; |
| 2562 |
|
| 2563 |
rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA; |
| 2564 |
|
| 2565 |
rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD, |
| 2566 |
(u8 *)&rf_field, 1); |
| 2567 |
if (rc) { |
| 2568 |
nfc_err(dev->dev, "Error on setting RF field\n"); |
| 2569 |
return rc; |
| 2570 |
} |
| 2571 |
|
| 2572 |
return 0; |
| 2573 |
} |
| 2574 |
|
| 2575 |
static int pn532_sam_configuration(struct nfc_dev *nfc_dev) |
| 2576 |
{ |
| 2577 |
struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 2578 |
struct sk_buff *skb; |
| 2579 |
struct sk_buff *resp; |
| 2580 |
|
| 2581 |
skb = pn533_alloc_skb(dev, 1); |
| 2582 |
if (!skb) |
| 2583 |
return -ENOMEM; |
| 2584 |
|
| 2585 |
skb_put_u8(skb, 0x01); |
| 2586 |
|
| 2587 |
resp = pn533_send_cmd_sync(dev, PN533_CMD_SAM_CONFIGURATION, skb); |
| 2588 |
if (IS_ERR(resp)) |
| 2589 |
return PTR_ERR(resp); |
| 2590 |
|
| 2591 |
dev_kfree_skb(resp); |
| 2592 |
return 0; |
| 2593 |
} |
| 2594 |
|
| 2595 |
static int pn533_dev_up(struct nfc_dev *nfc_dev) |
| 2596 |
{ |
| 2597 |
struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 2598 |
int rc; |
| 2599 |
|
| 2600 |
if (dev->phy_ops->dev_up) { |
| 2601 |
rc = dev->phy_ops->dev_up(dev); |
| 2602 |
if (rc) |
| 2603 |
return rc; |
| 2604 |
} |
| 2605 |
|
| 2606 |
if ((dev->device_type == PN533_DEVICE_PN532) || |
| 2607 |
(dev->device_type == PN533_DEVICE_PN532_AUTOPOLL)) { |
| 2608 |
rc = pn532_sam_configuration(nfc_dev); |
| 2609 |
|
| 2610 |
if (rc) |
| 2611 |
return rc; |
| 2612 |
} |
| 2613 |
|
| 2614 |
return pn533_rf_field(nfc_dev, 1); |
| 2615 |
} |
| 2616 |
|
| 2617 |
static int pn533_dev_down(struct nfc_dev *nfc_dev) |
| 2618 |
{ |
| 2619 |
struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 2620 |
int ret; |
| 2621 |
|
| 2622 |
ret = pn533_rf_field(nfc_dev, 0); |
| 2623 |
if (dev->phy_ops->dev_down && !ret) |
| 2624 |
ret = dev->phy_ops->dev_down(dev); |
| 2625 |
|
| 2626 |
return ret; |
| 2627 |
} |
| 2628 |
|
| 2629 |
static const struct nfc_ops pn533_nfc_ops = { |
| 2630 |
.dev_up = pn533_dev_up, |
| 2631 |
.dev_down = pn533_dev_down, |
| 2632 |
.dep_link_up = pn533_dep_link_up, |
| 2633 |
.dep_link_down = pn533_dep_link_down, |
| 2634 |
.start_poll = pn533_start_poll, |
| 2635 |
.stop_poll = pn533_stop_poll, |
| 2636 |
.activate_target = pn533_activate_target, |
| 2637 |
.deactivate_target = pn533_deactivate_target, |
| 2638 |
.im_transceive = pn533_transceive, |
| 2639 |
.tm_send = pn533_tm_send, |
| 2640 |
}; |
| 2641 |
|
| 2642 |
static int pn533_setup(struct pn533 *dev) |
| 2643 |
{ |
| 2644 |
struct pn533_config_max_retries max_retries; |
| 2645 |
struct pn533_config_timing timing; |
| 2646 |
u8 pasori_cfg[3] = {0x08, 0x01, 0x08}; |
| 2647 |
int rc; |
| 2648 |
|
| 2649 |
switch (dev->device_type) { |
| 2650 |
case PN533_DEVICE_STD: |
| 2651 |
case PN533_DEVICE_PASORI: |
| 2652 |
case PN533_DEVICE_ACR122U: |
| 2653 |
case PN533_DEVICE_PN532: |
| 2654 |
case PN533_DEVICE_PN532_AUTOPOLL: |
| 2655 |
max_retries.mx_rty_atr = 0x2; |
| 2656 |
max_retries.mx_rty_psl = 0x1; |
| 2657 |
max_retries.mx_rty_passive_act = |
| 2658 |
PN533_CONFIG_MAX_RETRIES_NO_RETRY; |
| 2659 |
|
| 2660 |
timing.rfu = PN533_CONFIG_TIMING_102; |
| 2661 |
timing.atr_res_timeout = PN533_CONFIG_TIMING_102; |
| 2662 |
timing.dep_timeout = PN533_CONFIG_TIMING_204; |
| 2663 |
|
| 2664 |
break; |
| 2665 |
|
| 2666 |
default: |
| 2667 |
nfc_err(dev->dev, "Unknown device type %d\n", |
| 2668 |
dev->device_type); |
| 2669 |
return -EINVAL; |
| 2670 |
} |
| 2671 |
|
| 2672 |
rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES, |
| 2673 |
(u8 *)&max_retries, sizeof(max_retries)); |
| 2674 |
if (rc) { |
| 2675 |
nfc_err(dev->dev, |
| 2676 |
"Error on setting MAX_RETRIES config\n"); |
| 2677 |
return rc; |
| 2678 |
} |
| 2679 |
|
| 2680 |
|
| 2681 |
rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING, |
| 2682 |
(u8 *)&timing, sizeof(timing)); |
| 2683 |
if (rc) { |
| 2684 |
nfc_err(dev->dev, "Error on setting RF timings\n"); |
| 2685 |
return rc; |
| 2686 |
} |
| 2687 |
|
| 2688 |
switch (dev->device_type) { |
| 2689 |
case PN533_DEVICE_STD: |
| 2690 |
case PN533_DEVICE_PN532: |
| 2691 |
case PN533_DEVICE_PN532_AUTOPOLL: |
| 2692 |
break; |
| 2693 |
|
| 2694 |
case PN533_DEVICE_PASORI: |
| 2695 |
pn533_pasori_fw_reset(dev); |
| 2696 |
|
| 2697 |
rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI, |
| 2698 |
pasori_cfg, 3); |
| 2699 |
if (rc) { |
| 2700 |
nfc_err(dev->dev, |
| 2701 |
"Error while settings PASORI config\n"); |
| 2702 |
return rc; |
| 2703 |
} |
| 2704 |
|
| 2705 |
pn533_pasori_fw_reset(dev); |
| 2706 |
|
| 2707 |
break; |
| 2708 |
} |
| 2709 |
|
| 2710 |
return 0; |
| 2711 |
} |
| 2712 |
|
| 2713 |
int pn533_finalize_setup(struct pn533 *dev) |
| 2714 |
{ |
| 2715 |
|
| 2716 |
struct pn533_fw_version fw_ver; |
| 2717 |
int rc; |
| 2718 |
|
| 2719 |
memset(&fw_ver, 0, sizeof(fw_ver)); |
| 2720 |
|
| 2721 |
rc = pn533_get_firmware_version(dev, &fw_ver); |
| 2722 |
if (rc) { |
| 2723 |
nfc_err(dev->dev, "Unable to get FW version\n"); |
| 2724 |
return rc; |
| 2725 |
} |
| 2726 |
|
| 2727 |
nfc_info(dev->dev, "NXP PN5%02X firmware ver %d.%d now attached\n", |
| 2728 |
fw_ver.ic, fw_ver.ver, fw_ver.rev); |
| 2729 |
|
| 2730 |
rc = pn533_setup(dev); |
| 2731 |
if (rc) |
| 2732 |
return rc; |
| 2733 |
|
| 2734 |
return 0; |
| 2735 |
} |
| 2736 |
EXPORT_SYMBOL_GPL(pn533_finalize_setup); |
| 2737 |
|
| 2738 |
struct pn533 *pn53x_common_init(u32 device_type, |
| 2739 |
enum pn533_protocol_type protocol_type, |
| 2740 |
void *phy, |
| 2741 |
const struct pn533_phy_ops *phy_ops, |
| 2742 |
struct pn533_frame_ops *fops, |
| 2743 |
struct device *dev) |
| 2744 |
{ |
| 2745 |
struct pn533 *priv; |
| 2746 |
|
| 2747 |
priv = kzalloc_obj(*priv); |
| 2748 |
if (!priv) |
| 2749 |
return ERR_PTR(-ENOMEM); |
| 2750 |
|
| 2751 |
priv->phy = phy; |
| 2752 |
priv->phy_ops = phy_ops; |
| 2753 |
priv->dev = dev; |
| 2754 |
if (fops != NULL) |
| 2755 |
priv->ops = fops; |
| 2756 |
else |
| 2757 |
priv->ops = &pn533_std_frame_ops; |
| 2758 |
|
| 2759 |
priv->protocol_type = protocol_type; |
| 2760 |
priv->device_type = device_type; |
| 2761 |
|
| 2762 |
mutex_init(&priv->cmd_lock); |
| 2763 |
|
| 2764 |
INIT_WORK(&priv->cmd_work, pn533_wq_cmd); |
| 2765 |
INIT_WORK(&priv->cmd_complete_work, pn533_wq_cmd_complete); |
| 2766 |
INIT_WORK(&priv->mi_rx_work, pn533_wq_mi_recv); |
| 2767 |
INIT_WORK(&priv->mi_tx_work, pn533_wq_mi_send); |
| 2768 |
INIT_WORK(&priv->tg_work, pn533_wq_tg_get_data); |
| 2769 |
INIT_WORK(&priv->mi_tm_rx_work, pn533_wq_tm_mi_recv); |
| 2770 |
INIT_WORK(&priv->mi_tm_tx_work, pn533_wq_tm_mi_send); |
| 2771 |
INIT_DELAYED_WORK(&priv->poll_work, pn533_wq_poll); |
| 2772 |
INIT_WORK(&priv->rf_work, pn533_wq_rf); |
| 2773 |
priv->wq = alloc_ordered_workqueue("pn533", 0); |
| 2774 |
if (priv->wq == NULL) |
| 2775 |
goto error; |
| 2776 |
|
| 2777 |
timer_setup(&priv->listen_timer, pn533_listen_mode_timer, 0); |
| 2778 |
|
| 2779 |
skb_queue_head_init(&priv->resp_q); |
| 2780 |
skb_queue_head_init(&priv->fragment_skb); |
| 2781 |
|
| 2782 |
INIT_LIST_HEAD(&priv->cmd_queue); |
| 2783 |
return priv; |
| 2784 |
|
| 2785 |
error: |
| 2786 |
kfree(priv); |
| 2787 |
return ERR_PTR(-ENOMEM); |
| 2788 |
} |
| 2789 |
EXPORT_SYMBOL_GPL(pn53x_common_init); |
| 2790 |
|
| 2791 |
void pn53x_common_clean(struct pn533 *priv) |
| 2792 |
{ |
| 2793 |
struct pn533_cmd *cmd, *n; |
| 2794 |
|
| 2795 |
/* delete the timer before cleanup the worker */ |
| 2796 |
timer_shutdown_sync(&priv->listen_timer); |
| 2797 |
|
| 2798 |
flush_delayed_work(&priv->poll_work); |
| 2799 |
destroy_workqueue(priv->wq); |
| 2800 |
|
| 2801 |
skb_queue_purge(&priv->resp_q); |
| 2802 |
|
| 2803 |
list_for_each_entry_safe(cmd, n, &priv->cmd_queue, queue) { |
| 2804 |
list_del(&cmd->queue); |
| 2805 |
kfree(cmd); |
| 2806 |
} |
| 2807 |
|
| 2808 |
kfree(priv); |
| 2809 |
} |
| 2810 |
EXPORT_SYMBOL_GPL(pn53x_common_clean); |
| 2811 |
|
| 2812 |
int pn532_i2c_nfc_alloc(struct pn533 *priv, u32 protocols, |
| 2813 |
struct device *parent) |
| 2814 |
{ |
| 2815 |
priv->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols, |
| 2816 |
priv->ops->tx_header_len + |
| 2817 |
PN533_CMD_DATAEXCH_HEAD_LEN, |
| 2818 |
priv->ops->tx_tail_len); |
| 2819 |
if (!priv->nfc_dev) |
| 2820 |
return -ENOMEM; |
| 2821 |
|
| 2822 |
nfc_set_parent_dev(priv->nfc_dev, parent); |
| 2823 |
nfc_set_drvdata(priv->nfc_dev, priv); |
| 2824 |
return 0; |
| 2825 |
} |
| 2826 |
EXPORT_SYMBOL_GPL(pn532_i2c_nfc_alloc); |
| 2827 |
|
| 2828 |
int pn53x_register_nfc(struct pn533 *priv, u32 protocols, |
| 2829 |
struct device *parent) |
| 2830 |
{ |
| 2831 |
int rc; |
| 2832 |
|
| 2833 |
rc = pn532_i2c_nfc_alloc(priv, protocols, parent); |
| 2834 |
if (rc) |
| 2835 |
return rc; |
| 2836 |
|
| 2837 |
rc = nfc_register_device(priv->nfc_dev); |
| 2838 |
if (rc) |
| 2839 |
nfc_free_device(priv->nfc_dev); |
| 2840 |
|
| 2841 |
return rc; |
| 2842 |
} |
| 2843 |
EXPORT_SYMBOL_GPL(pn53x_register_nfc); |
| 2844 |
|
| 2845 |
void pn53x_unregister_nfc(struct pn533 *priv) |
| 2846 |
{ |
| 2847 |
nfc_unregister_device(priv->nfc_dev); |
| 2848 |
nfc_free_device(priv->nfc_dev); |
| 2849 |
} |
| 2850 |
EXPORT_SYMBOL_GPL(pn53x_unregister_nfc); |
| 2851 |
|
| 2852 |
MODULE_AUTHOR("Lauro Ramos Venancio <[email protected]>"); |
| 2853 |
MODULE_AUTHOR("Aloisio Almeida Jr <[email protected]>"); |
| 2854 |
MODULE_AUTHOR("Waldemar Rymarkiewicz <[email protected]>"); |
| 2855 |
MODULE_DESCRIPTION("PN533 driver ver " VERSION); |
| 2856 |
MODULE_VERSION(VERSION); |
| 2857 |
MODULE_LICENSE("GPL"); |