Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HCI based Driver for NXP PN544 NFC Chip |
| 3 | * |
| 4 | * Copyright (C) 2012 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 | #include <linux/crc-ccitt.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/miscdevice.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/gpio.h> |
| 28 | #include <linux/i2c.h> |
| 29 | |
| 30 | #include <linux/nfc.h> |
| 31 | #include <net/nfc/hci.h> |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 32 | #include <net/nfc/llc.h> |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 33 | |
| 34 | #include <linux/nfc/pn544.h> |
| 35 | |
| 36 | #define DRIVER_DESC "HCI NFC driver for PN544" |
| 37 | |
| 38 | #define PN544_HCI_DRIVER_NAME "pn544_hci" |
| 39 | |
| 40 | /* Timing restrictions (ms) */ |
| 41 | #define PN544_HCI_RESETVEN_TIME 30 |
| 42 | |
| 43 | static struct i2c_device_id pn544_hci_id_table[] = { |
| 44 | {"pn544", 0}, |
| 45 | {} |
| 46 | }; |
| 47 | |
| 48 | MODULE_DEVICE_TABLE(i2c, pn544_hci_id_table); |
| 49 | |
| 50 | #define HCI_MODE 0 |
| 51 | #define FW_MODE 1 |
| 52 | |
| 53 | /* framing in HCI mode */ |
| 54 | #define PN544_HCI_LLC_LEN 1 |
| 55 | #define PN544_HCI_LLC_CRC 2 |
| 56 | #define PN544_HCI_LLC_LEN_CRC (PN544_HCI_LLC_LEN + PN544_HCI_LLC_CRC) |
| 57 | #define PN544_HCI_LLC_MIN_SIZE (1 + PN544_HCI_LLC_LEN_CRC) |
| 58 | #define PN544_HCI_LLC_MAX_PAYLOAD 29 |
| 59 | #define PN544_HCI_LLC_MAX_SIZE (PN544_HCI_LLC_LEN_CRC + 1 + \ |
| 60 | PN544_HCI_LLC_MAX_PAYLOAD) |
| 61 | |
| 62 | enum pn544_state { |
| 63 | PN544_ST_COLD, |
| 64 | PN544_ST_FW_READY, |
| 65 | PN544_ST_READY, |
| 66 | }; |
| 67 | |
| 68 | #define FULL_VERSION_LEN 11 |
| 69 | |
| 70 | /* Proprietary commands */ |
| 71 | #define PN544_WRITE 0x3f |
| 72 | |
| 73 | /* Proprietary gates, events, commands and registers */ |
| 74 | |
| 75 | /* NFC_HCI_RF_READER_A_GATE additional registers and commands */ |
| 76 | #define PN544_RF_READER_A_AUTO_ACTIVATION 0x10 |
| 77 | #define PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION 0x12 |
| 78 | #define PN544_MIFARE_CMD 0x21 |
| 79 | |
| 80 | /* Commands that apply to all RF readers */ |
| 81 | #define PN544_RF_READER_CMD_PRESENCE_CHECK 0x30 |
| 82 | #define PN544_RF_READER_CMD_ACTIVATE_NEXT 0x32 |
| 83 | |
| 84 | /* NFC_HCI_ID_MGMT_GATE additional registers */ |
| 85 | #define PN544_ID_MGMT_FULL_VERSION_SW 0x10 |
| 86 | |
| 87 | #define PN544_RF_READER_ISO15693_GATE 0x12 |
| 88 | |
| 89 | #define PN544_RF_READER_F_GATE 0x14 |
| 90 | #define PN544_FELICA_ID 0x04 |
| 91 | #define PN544_FELICA_RAW 0x20 |
| 92 | |
| 93 | #define PN544_RF_READER_JEWEL_GATE 0x15 |
| 94 | #define PN544_JEWEL_RAW_CMD 0x23 |
| 95 | |
| 96 | #define PN544_RF_READER_NFCIP1_INITIATOR_GATE 0x30 |
| 97 | #define PN544_RF_READER_NFCIP1_TARGET_GATE 0x31 |
| 98 | |
| 99 | #define PN544_SYS_MGMT_GATE 0x90 |
| 100 | #define PN544_SYS_MGMT_INFO_NOTIFICATION 0x02 |
| 101 | |
| 102 | #define PN544_POLLING_LOOP_MGMT_GATE 0x94 |
Arron Wang | 7e2afc9 | 2012-09-27 17:32:54 +0800 | [diff] [blame] | 103 | #define PN544_DEP_MODE 0x01 |
| 104 | #define PN544_DEP_ATR_REQ 0x02 |
| 105 | #define PN544_DEP_ATR_RES 0x03 |
| 106 | #define PN544_DEP_MERGE 0x0D |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 107 | #define PN544_PL_RDPHASES 0x06 |
| 108 | #define PN544_PL_EMULATION 0x07 |
| 109 | #define PN544_PL_NFCT_DEACTIVATED 0x09 |
| 110 | |
| 111 | #define PN544_SWP_MGMT_GATE 0xA0 |
| 112 | |
| 113 | #define PN544_NFC_WI_MGMT_GATE 0xA1 |
| 114 | |
Arron Wang | f7a5f6c | 2012-09-27 17:32:55 +0800 | [diff] [blame] | 115 | #define PN544_HCI_EVT_SND_DATA 0x01 |
| 116 | #define PN544_HCI_EVT_ACTIVATED 0x02 |
| 117 | #define PN544_HCI_EVT_DEACTIVATED 0x03 |
| 118 | #define PN544_HCI_EVT_RCV_DATA 0x04 |
| 119 | #define PN544_HCI_EVT_CONTINUE_MI 0x05 |
| 120 | |
Arron Wang | 928326f | 2012-09-27 17:32:56 +0800 | [diff] [blame^] | 121 | #define PN544_HCI_CMD_CONTINUE_ACTIVATION 0x13 |
| 122 | |
Eric Lapuyade | a10d595 | 2012-06-05 14:42:11 +0200 | [diff] [blame] | 123 | static struct nfc_hci_gate pn544_gates[] = { |
| 124 | {NFC_HCI_ADMIN_GATE, NFC_HCI_INVALID_PIPE}, |
| 125 | {NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE}, |
| 126 | {NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE}, |
| 127 | {NFC_HCI_LINK_MGMT_GATE, NFC_HCI_INVALID_PIPE}, |
| 128 | {NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE}, |
| 129 | {NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE}, |
| 130 | {PN544_SYS_MGMT_GATE, NFC_HCI_INVALID_PIPE}, |
| 131 | {PN544_SWP_MGMT_GATE, NFC_HCI_INVALID_PIPE}, |
| 132 | {PN544_POLLING_LOOP_MGMT_GATE, NFC_HCI_INVALID_PIPE}, |
| 133 | {PN544_NFC_WI_MGMT_GATE, NFC_HCI_INVALID_PIPE}, |
| 134 | {PN544_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE}, |
| 135 | {PN544_RF_READER_JEWEL_GATE, NFC_HCI_INVALID_PIPE}, |
| 136 | {PN544_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE}, |
| 137 | {PN544_RF_READER_NFCIP1_INITIATOR_GATE, NFC_HCI_INVALID_PIPE}, |
| 138 | {PN544_RF_READER_NFCIP1_TARGET_GATE, NFC_HCI_INVALID_PIPE} |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | /* Largest headroom needed for outgoing custom commands */ |
| 142 | #define PN544_CMDS_HEADROOM 2 |
Waldemar Rymarkiewicz | ade67208 | 2012-09-07 11:08:29 +0200 | [diff] [blame] | 143 | #define PN544_FRAME_HEADROOM 1 |
| 144 | #define PN544_FRAME_TAILROOM 2 |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 145 | |
| 146 | struct pn544_hci_info { |
| 147 | struct i2c_client *i2c_dev; |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 148 | struct nfc_hci_dev *hdev; |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 149 | |
| 150 | enum pn544_state state; |
| 151 | |
| 152 | struct mutex info_lock; |
| 153 | |
| 154 | unsigned int gpio_en; |
| 155 | unsigned int gpio_irq; |
| 156 | unsigned int gpio_fw; |
| 157 | unsigned int en_polarity; |
| 158 | |
| 159 | int hard_fault; /* |
| 160 | * < 0 if hardware error occured (e.g. i2c err) |
| 161 | * and prevents normal operation. |
| 162 | */ |
Eric Lapuyade | f3e8fb5 | 2012-09-11 10:43:50 +0200 | [diff] [blame] | 163 | int async_cb_type; |
| 164 | data_exchange_cb_t async_cb; |
| 165 | void *async_cb_context; |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | static void pn544_hci_platform_init(struct pn544_hci_info *info) |
| 169 | { |
| 170 | int polarity, retry, ret; |
| 171 | char rset_cmd[] = { 0x05, 0xF9, 0x04, 0x00, 0xC3, 0xE5 }; |
| 172 | int count = sizeof(rset_cmd); |
| 173 | |
| 174 | pr_info(DRIVER_DESC ": %s\n", __func__); |
| 175 | dev_info(&info->i2c_dev->dev, "Detecting nfc_en polarity\n"); |
| 176 | |
| 177 | /* Disable fw download */ |
| 178 | gpio_set_value(info->gpio_fw, 0); |
| 179 | |
| 180 | for (polarity = 0; polarity < 2; polarity++) { |
| 181 | info->en_polarity = polarity; |
| 182 | retry = 3; |
| 183 | while (retry--) { |
| 184 | /* power off */ |
| 185 | gpio_set_value(info->gpio_en, !info->en_polarity); |
| 186 | usleep_range(10000, 15000); |
| 187 | |
| 188 | /* power on */ |
| 189 | gpio_set_value(info->gpio_en, info->en_polarity); |
| 190 | usleep_range(10000, 15000); |
| 191 | |
| 192 | /* send reset */ |
| 193 | dev_dbg(&info->i2c_dev->dev, "Sending reset cmd\n"); |
| 194 | ret = i2c_master_send(info->i2c_dev, rset_cmd, count); |
| 195 | if (ret == count) { |
| 196 | dev_info(&info->i2c_dev->dev, |
| 197 | "nfc_en polarity : active %s\n", |
| 198 | (polarity == 0 ? "low" : "high")); |
| 199 | goto out; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | dev_err(&info->i2c_dev->dev, |
| 205 | "Could not detect nfc_en polarity, fallback to active high\n"); |
| 206 | |
| 207 | out: |
| 208 | gpio_set_value(info->gpio_en, !info->en_polarity); |
| 209 | } |
| 210 | |
| 211 | static int pn544_hci_enable(struct pn544_hci_info *info, int mode) |
| 212 | { |
| 213 | pr_info(DRIVER_DESC ": %s\n", __func__); |
| 214 | |
| 215 | gpio_set_value(info->gpio_fw, 0); |
| 216 | gpio_set_value(info->gpio_en, info->en_polarity); |
| 217 | usleep_range(10000, 15000); |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static void pn544_hci_disable(struct pn544_hci_info *info) |
| 223 | { |
| 224 | pr_info(DRIVER_DESC ": %s\n", __func__); |
| 225 | |
| 226 | gpio_set_value(info->gpio_fw, 0); |
| 227 | gpio_set_value(info->gpio_en, !info->en_polarity); |
| 228 | usleep_range(10000, 15000); |
| 229 | |
| 230 | gpio_set_value(info->gpio_en, info->en_polarity); |
| 231 | usleep_range(10000, 15000); |
| 232 | |
| 233 | gpio_set_value(info->gpio_en, !info->en_polarity); |
| 234 | usleep_range(10000, 15000); |
| 235 | } |
| 236 | |
| 237 | static int pn544_hci_i2c_write(struct i2c_client *client, u8 *buf, int len) |
| 238 | { |
| 239 | int r; |
| 240 | |
| 241 | usleep_range(3000, 6000); |
| 242 | |
| 243 | r = i2c_master_send(client, buf, len); |
| 244 | |
| 245 | if (r == -EREMOTEIO) { /* Retry, chip was in standby */ |
| 246 | usleep_range(6000, 10000); |
| 247 | r = i2c_master_send(client, buf, len); |
| 248 | } |
| 249 | |
Waldemar Rymarkiewicz | 96e3240 | 2012-09-20 08:59:10 +0200 | [diff] [blame] | 250 | if (r >= 0) { |
| 251 | if (r != len) |
| 252 | return -EREMOTEIO; |
| 253 | else |
| 254 | return 0; |
| 255 | } |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 256 | |
| 257 | return r; |
| 258 | } |
| 259 | |
| 260 | static int check_crc(u8 *buf, int buflen) |
| 261 | { |
Dan Carpenter | 885ba1d | 2012-05-18 10:36:47 +0300 | [diff] [blame] | 262 | int len; |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 263 | u16 crc; |
| 264 | |
| 265 | len = buf[0] + 1; |
| 266 | crc = crc_ccitt(0xffff, buf, len - 2); |
| 267 | crc = ~crc; |
| 268 | |
| 269 | if (buf[len - 2] != (crc & 0xff) || buf[len - 1] != (crc >> 8)) { |
| 270 | pr_err(PN544_HCI_DRIVER_NAME ": CRC error 0x%x != 0x%x 0x%x\n", |
| 271 | crc, buf[len - 1], buf[len - 2]); |
| 272 | |
| 273 | pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__); |
| 274 | print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE, |
| 275 | 16, 2, buf, buflen, false); |
| 276 | return -EPERM; |
| 277 | } |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees |
| 283 | * that i2c bus will be flushed and that next read will start on a new frame. |
| 284 | * returned skb contains only LLC header and payload. |
| 285 | * returns: |
| 286 | * -EREMOTEIO : i2c read error (fatal) |
| 287 | * -EBADMSG : frame was incorrect and discarded |
| 288 | * -ENOMEM : cannot allocate skb, frame dropped |
| 289 | */ |
| 290 | static int pn544_hci_i2c_read(struct i2c_client *client, struct sk_buff **skb) |
| 291 | { |
| 292 | int r; |
| 293 | u8 len; |
| 294 | u8 tmp[PN544_HCI_LLC_MAX_SIZE - 1]; |
| 295 | |
| 296 | r = i2c_master_recv(client, &len, 1); |
| 297 | if (r != 1) { |
| 298 | dev_err(&client->dev, "cannot read len byte\n"); |
| 299 | return -EREMOTEIO; |
| 300 | } |
| 301 | |
| 302 | if ((len < (PN544_HCI_LLC_MIN_SIZE - 1)) || |
| 303 | (len > (PN544_HCI_LLC_MAX_SIZE - 1))) { |
| 304 | dev_err(&client->dev, "invalid len byte\n"); |
| 305 | r = -EBADMSG; |
| 306 | goto flush; |
| 307 | } |
| 308 | |
| 309 | *skb = alloc_skb(1 + len, GFP_KERNEL); |
| 310 | if (*skb == NULL) { |
| 311 | r = -ENOMEM; |
| 312 | goto flush; |
| 313 | } |
| 314 | |
| 315 | *skb_put(*skb, 1) = len; |
| 316 | |
| 317 | r = i2c_master_recv(client, skb_put(*skb, len), len); |
| 318 | if (r != len) { |
| 319 | kfree_skb(*skb); |
| 320 | return -EREMOTEIO; |
| 321 | } |
| 322 | |
| 323 | r = check_crc((*skb)->data, (*skb)->len); |
| 324 | if (r != 0) { |
| 325 | kfree_skb(*skb); |
| 326 | r = -EBADMSG; |
| 327 | goto flush; |
| 328 | } |
| 329 | |
| 330 | skb_pull(*skb, 1); |
| 331 | skb_trim(*skb, (*skb)->len - 2); |
| 332 | |
| 333 | usleep_range(3000, 6000); |
| 334 | |
| 335 | return 0; |
| 336 | |
| 337 | flush: |
| 338 | if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0) |
| 339 | r = -EREMOTEIO; |
| 340 | |
| 341 | usleep_range(3000, 6000); |
| 342 | |
| 343 | return r; |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * Reads an shdlc frame from the chip. This is not as straightforward as it |
| 348 | * seems. There are cases where we could loose the frame start synchronization. |
| 349 | * The frame format is len-data-crc, and corruption can occur anywhere while |
| 350 | * transiting on i2c bus, such that we could read an invalid len. |
| 351 | * In order to recover synchronization with the next frame, we must be sure |
| 352 | * to read the real amount of data without using the len byte. We do this by |
| 353 | * assuming the following: |
| 354 | * - the chip will always present only one single complete frame on the bus |
| 355 | * before triggering the interrupt |
| 356 | * - the chip will not present a new frame until we have completely read |
| 357 | * the previous one (or until we have handled the interrupt). |
| 358 | * The tricky case is when we read a corrupted len that is less than the real |
| 359 | * len. We must detect this here in order to determine that we need to flush |
| 360 | * the bus. This is the reason why we check the crc here. |
| 361 | */ |
| 362 | static irqreturn_t pn544_hci_irq_thread_fn(int irq, void *dev_id) |
| 363 | { |
| 364 | struct pn544_hci_info *info = dev_id; |
Wei Yongjun | c51ad34 | 2012-09-08 21:19:44 +0800 | [diff] [blame] | 365 | struct i2c_client *client; |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 366 | struct sk_buff *skb = NULL; |
| 367 | int r; |
| 368 | |
Wei Yongjun | c51ad34 | 2012-09-08 21:19:44 +0800 | [diff] [blame] | 369 | if (!info || irq != info->i2c_dev->irq) { |
| 370 | WARN_ON_ONCE(1); |
| 371 | return IRQ_NONE; |
| 372 | } |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 373 | |
Wei Yongjun | c51ad34 | 2012-09-08 21:19:44 +0800 | [diff] [blame] | 374 | client = info->i2c_dev; |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 375 | dev_dbg(&client->dev, "IRQ\n"); |
| 376 | |
| 377 | if (info->hard_fault != 0) |
| 378 | return IRQ_HANDLED; |
| 379 | |
| 380 | r = pn544_hci_i2c_read(client, &skb); |
| 381 | if (r == -EREMOTEIO) { |
| 382 | info->hard_fault = r; |
| 383 | |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 384 | nfc_hci_recv_frame(info->hdev, NULL); |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 385 | |
| 386 | return IRQ_HANDLED; |
| 387 | } else if ((r == -ENOMEM) || (r == -EBADMSG)) { |
| 388 | return IRQ_HANDLED; |
| 389 | } |
| 390 | |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 391 | nfc_hci_recv_frame(info->hdev, skb); |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 392 | |
| 393 | return IRQ_HANDLED; |
| 394 | } |
| 395 | |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 396 | static int pn544_hci_open(struct nfc_hci_dev *hdev) |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 397 | { |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 398 | struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev); |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 399 | int r = 0; |
| 400 | |
| 401 | mutex_lock(&info->info_lock); |
| 402 | |
| 403 | if (info->state != PN544_ST_COLD) { |
| 404 | r = -EBUSY; |
| 405 | goto out; |
| 406 | } |
| 407 | |
| 408 | r = pn544_hci_enable(info, HCI_MODE); |
| 409 | |
Eric Lapuyade | eae202a | 2012-05-30 18:13:06 +0200 | [diff] [blame] | 410 | if (r == 0) |
| 411 | info->state = PN544_ST_READY; |
| 412 | |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 413 | out: |
| 414 | mutex_unlock(&info->info_lock); |
| 415 | return r; |
| 416 | } |
| 417 | |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 418 | static void pn544_hci_close(struct nfc_hci_dev *hdev) |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 419 | { |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 420 | struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev); |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 421 | |
| 422 | mutex_lock(&info->info_lock); |
| 423 | |
| 424 | if (info->state == PN544_ST_COLD) |
| 425 | goto out; |
| 426 | |
| 427 | pn544_hci_disable(info); |
| 428 | |
Eric Lapuyade | eae202a | 2012-05-30 18:13:06 +0200 | [diff] [blame] | 429 | info->state = PN544_ST_COLD; |
| 430 | |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 431 | out: |
| 432 | mutex_unlock(&info->info_lock); |
| 433 | } |
| 434 | |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 435 | static int pn544_hci_ready(struct nfc_hci_dev *hdev) |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 436 | { |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 437 | struct sk_buff *skb; |
| 438 | static struct hw_config { |
| 439 | u8 adr[2]; |
| 440 | u8 value; |
| 441 | } hw_config[] = { |
| 442 | {{0x9f, 0x9a}, 0x00}, |
| 443 | |
| 444 | {{0x98, 0x10}, 0xbc}, |
| 445 | |
| 446 | {{0x9e, 0x71}, 0x00}, |
| 447 | |
| 448 | {{0x98, 0x09}, 0x00}, |
| 449 | |
| 450 | {{0x9e, 0xb4}, 0x00}, |
| 451 | |
| 452 | {{0x9e, 0xd9}, 0xff}, |
| 453 | {{0x9e, 0xda}, 0xff}, |
| 454 | {{0x9e, 0xdb}, 0x23}, |
| 455 | {{0x9e, 0xdc}, 0x21}, |
| 456 | {{0x9e, 0xdd}, 0x22}, |
| 457 | {{0x9e, 0xde}, 0x24}, |
| 458 | |
| 459 | {{0x9c, 0x01}, 0x08}, |
| 460 | |
| 461 | {{0x9e, 0xaa}, 0x01}, |
| 462 | |
| 463 | {{0x9b, 0xd1}, 0x0d}, |
| 464 | {{0x9b, 0xd2}, 0x24}, |
| 465 | {{0x9b, 0xd3}, 0x0a}, |
| 466 | {{0x9b, 0xd4}, 0x22}, |
| 467 | {{0x9b, 0xd5}, 0x08}, |
| 468 | {{0x9b, 0xd6}, 0x1e}, |
| 469 | {{0x9b, 0xdd}, 0x1c}, |
| 470 | |
| 471 | {{0x9b, 0x84}, 0x13}, |
| 472 | {{0x99, 0x81}, 0x7f}, |
| 473 | {{0x99, 0x31}, 0x70}, |
| 474 | |
| 475 | {{0x98, 0x00}, 0x3f}, |
| 476 | |
| 477 | {{0x9f, 0x09}, 0x00}, |
| 478 | |
| 479 | {{0x9f, 0x0a}, 0x05}, |
| 480 | |
| 481 | {{0x9e, 0xd1}, 0xa1}, |
| 482 | {{0x99, 0x23}, 0x00}, |
| 483 | |
| 484 | {{0x9e, 0x74}, 0x80}, |
| 485 | |
| 486 | {{0x9f, 0x28}, 0x10}, |
| 487 | |
| 488 | {{0x9f, 0x35}, 0x14}, |
| 489 | |
| 490 | {{0x9f, 0x36}, 0x60}, |
| 491 | |
| 492 | {{0x9c, 0x31}, 0x00}, |
| 493 | |
| 494 | {{0x9c, 0x32}, 0xc8}, |
| 495 | |
| 496 | {{0x9c, 0x19}, 0x40}, |
| 497 | |
| 498 | {{0x9c, 0x1a}, 0x40}, |
| 499 | |
| 500 | {{0x9c, 0x0c}, 0x00}, |
| 501 | |
| 502 | {{0x9c, 0x0d}, 0x00}, |
| 503 | |
| 504 | {{0x9c, 0x12}, 0x00}, |
| 505 | |
| 506 | {{0x9c, 0x13}, 0x00}, |
| 507 | |
| 508 | {{0x98, 0xa2}, 0x0e}, |
| 509 | |
| 510 | {{0x98, 0x93}, 0x40}, |
| 511 | |
| 512 | {{0x98, 0x7d}, 0x02}, |
| 513 | {{0x98, 0x7e}, 0x00}, |
| 514 | {{0x9f, 0xc8}, 0x01}, |
| 515 | }; |
| 516 | struct hw_config *p = hw_config; |
| 517 | int count = ARRAY_SIZE(hw_config); |
| 518 | struct sk_buff *res_skb; |
| 519 | u8 param[4]; |
| 520 | int r; |
| 521 | |
| 522 | param[0] = 0; |
| 523 | while (count--) { |
| 524 | param[1] = p->adr[0]; |
| 525 | param[2] = p->adr[1]; |
| 526 | param[3] = p->value; |
| 527 | |
| 528 | r = nfc_hci_send_cmd(hdev, PN544_SYS_MGMT_GATE, PN544_WRITE, |
| 529 | param, 4, &res_skb); |
| 530 | if (r < 0) |
| 531 | return r; |
| 532 | |
| 533 | if (res_skb->len != 1) { |
| 534 | kfree_skb(res_skb); |
| 535 | return -EPROTO; |
| 536 | } |
| 537 | |
| 538 | if (res_skb->data[0] != p->value) { |
| 539 | kfree_skb(res_skb); |
| 540 | return -EIO; |
| 541 | } |
| 542 | |
| 543 | kfree_skb(res_skb); |
| 544 | |
| 545 | p++; |
| 546 | } |
| 547 | |
| 548 | param[0] = NFC_HCI_UICC_HOST_ID; |
| 549 | r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE, |
| 550 | NFC_HCI_ADMIN_WHITELIST, param, 1); |
| 551 | if (r < 0) |
| 552 | return r; |
| 553 | |
| 554 | param[0] = 0x3d; |
| 555 | r = nfc_hci_set_param(hdev, PN544_SYS_MGMT_GATE, |
| 556 | PN544_SYS_MGMT_INFO_NOTIFICATION, param, 1); |
| 557 | if (r < 0) |
| 558 | return r; |
| 559 | |
| 560 | param[0] = 0x0; |
| 561 | r = nfc_hci_set_param(hdev, NFC_HCI_RF_READER_A_GATE, |
| 562 | PN544_RF_READER_A_AUTO_ACTIVATION, param, 1); |
| 563 | if (r < 0) |
| 564 | return r; |
| 565 | |
| 566 | r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE, |
| 567 | NFC_HCI_EVT_END_OPERATION, NULL, 0); |
| 568 | if (r < 0) |
| 569 | return r; |
| 570 | |
| 571 | param[0] = 0x1; |
| 572 | r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE, |
| 573 | PN544_PL_NFCT_DEACTIVATED, param, 1); |
| 574 | if (r < 0) |
| 575 | return r; |
| 576 | |
| 577 | param[0] = 0x0; |
| 578 | r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE, |
| 579 | PN544_PL_RDPHASES, param, 1); |
| 580 | if (r < 0) |
| 581 | return r; |
| 582 | |
| 583 | r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE, |
| 584 | PN544_ID_MGMT_FULL_VERSION_SW, &skb); |
| 585 | if (r < 0) |
| 586 | return r; |
| 587 | |
| 588 | if (skb->len != FULL_VERSION_LEN) { |
| 589 | kfree_skb(skb); |
| 590 | return -EINVAL; |
| 591 | } |
| 592 | |
| 593 | print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ", |
| 594 | DUMP_PREFIX_NONE, 16, 1, |
| 595 | skb->data, FULL_VERSION_LEN, false); |
| 596 | |
| 597 | kfree_skb(skb); |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
Waldemar Rymarkiewicz | ade67208 | 2012-09-07 11:08:29 +0200 | [diff] [blame] | 602 | static void pn544_hci_add_len_crc(struct sk_buff *skb) |
| 603 | { |
| 604 | u16 crc; |
| 605 | int len; |
| 606 | |
| 607 | len = skb->len + 2; |
| 608 | *skb_push(skb, 1) = len; |
| 609 | |
| 610 | crc = crc_ccitt(0xffff, skb->data, skb->len); |
| 611 | crc = ~crc; |
| 612 | *skb_put(skb, 1) = crc & 0xff; |
| 613 | *skb_put(skb, 1) = crc >> 8; |
| 614 | } |
| 615 | |
| 616 | static void pn544_hci_remove_len_crc(struct sk_buff *skb) |
| 617 | { |
| 618 | skb_pull(skb, PN544_FRAME_HEADROOM); |
| 619 | skb_trim(skb, PN544_FRAME_TAILROOM); |
| 620 | } |
| 621 | |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 622 | static int pn544_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb) |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 623 | { |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 624 | struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev); |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 625 | struct i2c_client *client = info->i2c_dev; |
Waldemar Rymarkiewicz | ade67208 | 2012-09-07 11:08:29 +0200 | [diff] [blame] | 626 | int r; |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 627 | |
| 628 | if (info->hard_fault != 0) |
| 629 | return info->hard_fault; |
| 630 | |
Waldemar Rymarkiewicz | ade67208 | 2012-09-07 11:08:29 +0200 | [diff] [blame] | 631 | pn544_hci_add_len_crc(skb); |
| 632 | r = pn544_hci_i2c_write(client, skb->data, skb->len); |
| 633 | pn544_hci_remove_len_crc(skb); |
| 634 | |
| 635 | return r; |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 636 | } |
| 637 | |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 638 | static int pn544_hci_start_poll(struct nfc_hci_dev *hdev, |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 639 | u32 im_protocols, u32 tm_protocols) |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 640 | { |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 641 | u8 phases = 0; |
| 642 | int r; |
| 643 | u8 duration[2]; |
| 644 | u8 activated; |
Arron Wang | 7e2afc9 | 2012-09-27 17:32:54 +0800 | [diff] [blame] | 645 | u8 i_mode = 0x3f; /* Enable all supported modes */ |
| 646 | u8 t_mode = 0x0f; |
| 647 | u8 t_merge = 0x01; /* Enable merge by default */ |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 648 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 649 | pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n", |
| 650 | __func__, im_protocols, tm_protocols); |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 651 | |
| 652 | r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE, |
| 653 | NFC_HCI_EVT_END_OPERATION, NULL, 0); |
| 654 | if (r < 0) |
| 655 | return r; |
| 656 | |
| 657 | duration[0] = 0x18; |
| 658 | duration[1] = 0x6a; |
| 659 | r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE, |
| 660 | PN544_PL_EMULATION, duration, 2); |
| 661 | if (r < 0) |
| 662 | return r; |
| 663 | |
| 664 | activated = 0; |
| 665 | r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE, |
| 666 | PN544_PL_NFCT_DEACTIVATED, &activated, 1); |
| 667 | if (r < 0) |
| 668 | return r; |
| 669 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 670 | if (im_protocols & (NFC_PROTO_ISO14443_MASK | NFC_PROTO_MIFARE_MASK | |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 671 | NFC_PROTO_JEWEL_MASK)) |
| 672 | phases |= 1; /* Type A */ |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 673 | if (im_protocols & NFC_PROTO_FELICA_MASK) { |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 674 | phases |= (1 << 2); /* Type F 212 */ |
| 675 | phases |= (1 << 3); /* Type F 424 */ |
| 676 | } |
| 677 | |
| 678 | phases |= (1 << 5); /* NFC active */ |
| 679 | |
| 680 | r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE, |
| 681 | PN544_PL_RDPHASES, &phases, 1); |
| 682 | if (r < 0) |
| 683 | return r; |
| 684 | |
Arron Wang | 7e2afc9 | 2012-09-27 17:32:54 +0800 | [diff] [blame] | 685 | if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) { |
| 686 | hdev->gb = nfc_get_local_general_bytes(hdev->ndev, |
| 687 | &hdev->gb_len); |
| 688 | pr_debug("generate local bytes %p", hdev->gb); |
| 689 | if (hdev->gb == NULL || hdev->gb_len == 0) { |
| 690 | im_protocols &= ~NFC_PROTO_NFC_DEP_MASK; |
| 691 | tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | if (im_protocols & NFC_PROTO_NFC_DEP_MASK) { |
| 696 | r = nfc_hci_send_event(hdev, |
| 697 | PN544_RF_READER_NFCIP1_INITIATOR_GATE, |
| 698 | NFC_HCI_EVT_END_OPERATION, NULL, 0); |
| 699 | if (r < 0) |
| 700 | return r; |
| 701 | |
| 702 | r = nfc_hci_set_param(hdev, |
| 703 | PN544_RF_READER_NFCIP1_INITIATOR_GATE, |
| 704 | PN544_DEP_MODE, &i_mode, 1); |
| 705 | if (r < 0) |
| 706 | return r; |
| 707 | |
| 708 | r = nfc_hci_set_param(hdev, |
| 709 | PN544_RF_READER_NFCIP1_INITIATOR_GATE, |
| 710 | PN544_DEP_ATR_REQ, hdev->gb, hdev->gb_len); |
| 711 | if (r < 0) |
| 712 | return r; |
| 713 | |
| 714 | r = nfc_hci_send_event(hdev, |
| 715 | PN544_RF_READER_NFCIP1_INITIATOR_GATE, |
| 716 | NFC_HCI_EVT_READER_REQUESTED, NULL, 0); |
| 717 | if (r < 0) |
| 718 | nfc_hci_send_event(hdev, |
| 719 | PN544_RF_READER_NFCIP1_INITIATOR_GATE, |
| 720 | NFC_HCI_EVT_END_OPERATION, NULL, 0); |
| 721 | } |
| 722 | |
| 723 | if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) { |
| 724 | r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE, |
| 725 | PN544_DEP_MODE, &t_mode, 1); |
| 726 | if (r < 0) |
| 727 | return r; |
| 728 | |
| 729 | r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE, |
| 730 | PN544_DEP_ATR_RES, hdev->gb, hdev->gb_len); |
| 731 | if (r < 0) |
| 732 | return r; |
| 733 | |
| 734 | r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE, |
| 735 | PN544_DEP_MERGE, &t_merge, 1); |
| 736 | if (r < 0) |
| 737 | return r; |
| 738 | } |
| 739 | |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 740 | r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE, |
| 741 | NFC_HCI_EVT_READER_REQUESTED, NULL, 0); |
| 742 | if (r < 0) |
| 743 | nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE, |
| 744 | NFC_HCI_EVT_END_OPERATION, NULL, 0); |
| 745 | |
| 746 | return r; |
| 747 | } |
| 748 | |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 749 | static int pn544_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate, |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 750 | struct nfc_target *target) |
| 751 | { |
| 752 | switch (gate) { |
| 753 | case PN544_RF_READER_F_GATE: |
| 754 | target->supported_protocols = NFC_PROTO_FELICA_MASK; |
| 755 | break; |
| 756 | case PN544_RF_READER_JEWEL_GATE: |
| 757 | target->supported_protocols = NFC_PROTO_JEWEL_MASK; |
| 758 | target->sens_res = 0x0c00; |
| 759 | break; |
Arron Wang | 928326f | 2012-09-27 17:32:56 +0800 | [diff] [blame^] | 760 | case PN544_RF_READER_NFCIP1_INITIATOR_GATE: |
| 761 | target->supported_protocols = NFC_PROTO_NFC_DEP_MASK; |
| 762 | break; |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 763 | default: |
| 764 | return -EPROTO; |
| 765 | } |
| 766 | |
| 767 | return 0; |
| 768 | } |
| 769 | |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 770 | static int pn544_hci_complete_target_discovered(struct nfc_hci_dev *hdev, |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 771 | u8 gate, |
| 772 | struct nfc_target *target) |
| 773 | { |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 774 | struct sk_buff *uid_skb; |
| 775 | int r = 0; |
| 776 | |
Arron Wang | 928326f | 2012-09-27 17:32:56 +0800 | [diff] [blame^] | 777 | if (gate == PN544_RF_READER_NFCIP1_INITIATOR_GATE) |
| 778 | return r; |
| 779 | |
| 780 | if (target->supported_protocols & NFC_PROTO_NFC_DEP_MASK) { |
| 781 | r = nfc_hci_send_cmd(hdev, |
| 782 | PN544_RF_READER_NFCIP1_INITIATOR_GATE, |
| 783 | PN544_HCI_CMD_CONTINUE_ACTIVATION, NULL, 0, NULL); |
| 784 | if (r < 0) |
| 785 | return r; |
| 786 | |
| 787 | target->hci_reader_gate = PN544_RF_READER_NFCIP1_INITIATOR_GATE; |
| 788 | } else if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) { |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 789 | if (target->nfcid1_len != 4 && target->nfcid1_len != 7 && |
| 790 | target->nfcid1_len != 10) |
| 791 | return -EPROTO; |
| 792 | |
| 793 | r = nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE, |
| 794 | PN544_RF_READER_CMD_ACTIVATE_NEXT, |
| 795 | target->nfcid1, target->nfcid1_len, NULL); |
| 796 | } else if (target->supported_protocols & NFC_PROTO_FELICA_MASK) { |
| 797 | r = nfc_hci_get_param(hdev, PN544_RF_READER_F_GATE, |
| 798 | PN544_FELICA_ID, &uid_skb); |
| 799 | if (r < 0) |
| 800 | return r; |
| 801 | |
| 802 | if (uid_skb->len != 8) { |
| 803 | kfree_skb(uid_skb); |
| 804 | return -EPROTO; |
| 805 | } |
| 806 | |
| 807 | r = nfc_hci_send_cmd(hdev, PN544_RF_READER_F_GATE, |
| 808 | PN544_RF_READER_CMD_ACTIVATE_NEXT, |
| 809 | uid_skb->data, uid_skb->len, NULL); |
| 810 | kfree_skb(uid_skb); |
Arron Wang | 928326f | 2012-09-27 17:32:56 +0800 | [diff] [blame^] | 811 | |
| 812 | r = nfc_hci_send_cmd(hdev, |
| 813 | PN544_RF_READER_NFCIP1_INITIATOR_GATE, |
| 814 | PN544_HCI_CMD_CONTINUE_ACTIVATION, |
| 815 | NULL, 0, NULL); |
| 816 | if (r < 0) |
| 817 | return r; |
| 818 | |
| 819 | target->hci_reader_gate = PN544_RF_READER_NFCIP1_INITIATOR_GATE; |
| 820 | target->supported_protocols = NFC_PROTO_NFC_DEP_MASK; |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 821 | } else if (target->supported_protocols & NFC_PROTO_ISO14443_MASK) { |
| 822 | /* |
| 823 | * TODO: maybe other ISO 14443 require some kind of continue |
| 824 | * activation, but for now we've seen only this one below. |
| 825 | */ |
| 826 | if (target->sens_res == 0x4403) /* Type 4 Mifare DESFire */ |
| 827 | r = nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE, |
| 828 | PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION, |
| 829 | NULL, 0, NULL); |
| 830 | } |
| 831 | |
| 832 | return r; |
| 833 | } |
| 834 | |
Eric Lapuyade | f3e8fb5 | 2012-09-11 10:43:50 +0200 | [diff] [blame] | 835 | #define PN544_CB_TYPE_READER_F 1 |
| 836 | |
| 837 | static void pn544_hci_data_exchange_cb(void *context, struct sk_buff *skb, |
| 838 | int err) |
| 839 | { |
| 840 | struct pn544_hci_info *info = context; |
| 841 | |
| 842 | switch (info->async_cb_type) { |
| 843 | case PN544_CB_TYPE_READER_F: |
| 844 | if (err == 0) |
| 845 | skb_pull(skb, 1); |
| 846 | info->async_cb(info->async_cb_context, skb, err); |
| 847 | break; |
| 848 | default: |
| 849 | if (err == 0) |
| 850 | kfree_skb(skb); |
| 851 | break; |
| 852 | } |
| 853 | } |
| 854 | |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 855 | #define MIFARE_CMD_AUTH_KEY_A 0x60 |
| 856 | #define MIFARE_CMD_AUTH_KEY_B 0x61 |
| 857 | #define MIFARE_CMD_HEADER 2 |
| 858 | #define MIFARE_UID_LEN 4 |
| 859 | #define MIFARE_KEY_LEN 6 |
| 860 | #define MIFARE_CMD_LEN 12 |
| 861 | /* |
| 862 | * Returns: |
| 863 | * <= 0: driver handled the data exchange |
| 864 | * 1: driver doesn't especially handle, please do standard processing |
| 865 | */ |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 866 | static int pn544_hci_data_exchange(struct nfc_hci_dev *hdev, |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 867 | struct nfc_target *target, |
Eric Lapuyade | f3e8fb5 | 2012-09-11 10:43:50 +0200 | [diff] [blame] | 868 | struct sk_buff *skb, data_exchange_cb_t cb, |
| 869 | void *cb_context) |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 870 | { |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 871 | struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev); |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 872 | |
| 873 | pr_info(DRIVER_DESC ": %s for gate=%d\n", __func__, |
| 874 | target->hci_reader_gate); |
| 875 | |
| 876 | switch (target->hci_reader_gate) { |
| 877 | case NFC_HCI_RF_READER_A_GATE: |
| 878 | if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) { |
| 879 | /* |
| 880 | * It seems that pn544 is inverting key and UID for |
| 881 | * MIFARE authentication commands. |
| 882 | */ |
| 883 | if (skb->len == MIFARE_CMD_LEN && |
| 884 | (skb->data[0] == MIFARE_CMD_AUTH_KEY_A || |
| 885 | skb->data[0] == MIFARE_CMD_AUTH_KEY_B)) { |
| 886 | u8 uid[MIFARE_UID_LEN]; |
| 887 | u8 *data = skb->data + MIFARE_CMD_HEADER; |
| 888 | |
| 889 | memcpy(uid, data + MIFARE_KEY_LEN, |
| 890 | MIFARE_UID_LEN); |
| 891 | memmove(data + MIFARE_UID_LEN, data, |
| 892 | MIFARE_KEY_LEN); |
| 893 | memcpy(data, uid, MIFARE_UID_LEN); |
| 894 | } |
| 895 | |
Eric Lapuyade | f3e8fb5 | 2012-09-11 10:43:50 +0200 | [diff] [blame] | 896 | return nfc_hci_send_cmd_async(hdev, |
| 897 | target->hci_reader_gate, |
| 898 | PN544_MIFARE_CMD, |
| 899 | skb->data, skb->len, |
| 900 | cb, cb_context); |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 901 | } else |
| 902 | return 1; |
| 903 | case PN544_RF_READER_F_GATE: |
| 904 | *skb_push(skb, 1) = 0; |
| 905 | *skb_push(skb, 1) = 0; |
| 906 | |
Eric Lapuyade | f3e8fb5 | 2012-09-11 10:43:50 +0200 | [diff] [blame] | 907 | info->async_cb_type = PN544_CB_TYPE_READER_F; |
| 908 | info->async_cb = cb; |
| 909 | info->async_cb_context = cb_context; |
| 910 | |
| 911 | return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate, |
| 912 | PN544_FELICA_RAW, skb->data, |
| 913 | skb->len, |
| 914 | pn544_hci_data_exchange_cb, info); |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 915 | case PN544_RF_READER_JEWEL_GATE: |
Eric Lapuyade | f3e8fb5 | 2012-09-11 10:43:50 +0200 | [diff] [blame] | 916 | return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate, |
| 917 | PN544_JEWEL_RAW_CMD, skb->data, |
| 918 | skb->len, cb, cb_context); |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 919 | default: |
| 920 | return 1; |
| 921 | } |
| 922 | } |
| 923 | |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 924 | static int pn544_hci_check_presence(struct nfc_hci_dev *hdev, |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 925 | struct nfc_target *target) |
| 926 | { |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 927 | return nfc_hci_send_cmd(hdev, target->hci_reader_gate, |
| 928 | PN544_RF_READER_CMD_PRESENCE_CHECK, |
| 929 | NULL, 0, NULL); |
| 930 | } |
| 931 | |
Arron Wang | f7a5f6c | 2012-09-27 17:32:55 +0800 | [diff] [blame] | 932 | void pn544_hci_event_received(struct nfc_hci_dev *hdev, u8 gate, u8 event, |
| 933 | struct sk_buff *skb) |
| 934 | { |
| 935 | struct sk_buff *rgb_skb = NULL; |
| 936 | int r = 0; |
| 937 | |
| 938 | pr_debug("hci event %d", event); |
| 939 | switch (event) { |
| 940 | case PN544_HCI_EVT_ACTIVATED: |
| 941 | if (gate == PN544_RF_READER_NFCIP1_INITIATOR_GATE) |
| 942 | nfc_hci_target_discovered(hdev, gate); |
| 943 | else if (gate == PN544_RF_READER_NFCIP1_TARGET_GATE) { |
| 944 | r = nfc_hci_get_param(hdev, gate, PN544_DEP_ATR_REQ, |
| 945 | &rgb_skb); |
| 946 | |
| 947 | if (r < 0) |
| 948 | goto exit; |
| 949 | |
| 950 | nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK, |
| 951 | NFC_COMM_PASSIVE, rgb_skb->data, |
| 952 | rgb_skb->len); |
| 953 | |
| 954 | kfree_skb(rgb_skb); |
| 955 | } |
| 956 | |
| 957 | break; |
| 958 | case PN544_HCI_EVT_DEACTIVATED: |
| 959 | nfc_hci_send_event(hdev, gate, |
| 960 | NFC_HCI_EVT_END_OPERATION, NULL, 0); |
| 961 | break; |
| 962 | default: |
| 963 | break; |
| 964 | } |
| 965 | |
| 966 | exit: |
| 967 | kfree_skb(skb); |
| 968 | } |
| 969 | |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 970 | static struct nfc_hci_ops pn544_hci_ops = { |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 971 | .open = pn544_hci_open, |
| 972 | .close = pn544_hci_close, |
| 973 | .hci_ready = pn544_hci_ready, |
| 974 | .xmit = pn544_hci_xmit, |
| 975 | .start_poll = pn544_hci_start_poll, |
| 976 | .target_from_gate = pn544_hci_target_from_gate, |
| 977 | .complete_target_discovered = pn544_hci_complete_target_discovered, |
| 978 | .data_exchange = pn544_hci_data_exchange, |
| 979 | .check_presence = pn544_hci_check_presence, |
Arron Wang | f7a5f6c | 2012-09-27 17:32:55 +0800 | [diff] [blame] | 980 | .event_received = pn544_hci_event_received, |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 981 | }; |
| 982 | |
| 983 | static int __devinit pn544_hci_probe(struct i2c_client *client, |
| 984 | const struct i2c_device_id *id) |
| 985 | { |
| 986 | struct pn544_hci_info *info; |
| 987 | struct pn544_nfc_platform_data *pdata; |
| 988 | int r = 0; |
| 989 | u32 protocols; |
| 990 | struct nfc_hci_init_data init_data; |
| 991 | |
| 992 | dev_dbg(&client->dev, "%s\n", __func__); |
| 993 | dev_dbg(&client->dev, "IRQ: %d\n", client->irq); |
| 994 | |
| 995 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 996 | dev_err(&client->dev, "Need I2C_FUNC_I2C\n"); |
| 997 | return -ENODEV; |
| 998 | } |
| 999 | |
| 1000 | info = kzalloc(sizeof(struct pn544_hci_info), GFP_KERNEL); |
| 1001 | if (!info) { |
| 1002 | dev_err(&client->dev, |
| 1003 | "Cannot allocate memory for pn544_hci_info.\n"); |
| 1004 | r = -ENOMEM; |
| 1005 | goto err_info_alloc; |
| 1006 | } |
| 1007 | |
| 1008 | info->i2c_dev = client; |
| 1009 | info->state = PN544_ST_COLD; |
| 1010 | mutex_init(&info->info_lock); |
| 1011 | i2c_set_clientdata(client, info); |
| 1012 | |
| 1013 | pdata = client->dev.platform_data; |
| 1014 | if (pdata == NULL) { |
| 1015 | dev_err(&client->dev, "No platform data\n"); |
| 1016 | r = -EINVAL; |
| 1017 | goto err_pdata; |
| 1018 | } |
| 1019 | |
| 1020 | if (pdata->request_resources == NULL) { |
| 1021 | dev_err(&client->dev, "request_resources() missing\n"); |
| 1022 | r = -EINVAL; |
| 1023 | goto err_pdata; |
| 1024 | } |
| 1025 | |
| 1026 | r = pdata->request_resources(client); |
| 1027 | if (r) { |
| 1028 | dev_err(&client->dev, "Cannot get platform resources\n"); |
| 1029 | goto err_pdata; |
| 1030 | } |
| 1031 | |
| 1032 | info->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE); |
| 1033 | info->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET); |
| 1034 | info->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ); |
| 1035 | |
| 1036 | pn544_hci_platform_init(info); |
| 1037 | |
| 1038 | r = request_threaded_irq(client->irq, NULL, pn544_hci_irq_thread_fn, |
Samuel Ortiz | f2ce398 | 2012-08-25 00:40:16 +0200 | [diff] [blame] | 1039 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
| 1040 | PN544_HCI_DRIVER_NAME, info); |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 1041 | if (r < 0) { |
| 1042 | dev_err(&client->dev, "Unable to register IRQ handler\n"); |
| 1043 | goto err_rti; |
| 1044 | } |
| 1045 | |
Eric Lapuyade | a10d595 | 2012-06-05 14:42:11 +0200 | [diff] [blame] | 1046 | init_data.gate_count = ARRAY_SIZE(pn544_gates); |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 1047 | |
Eric Lapuyade | a10d595 | 2012-06-05 14:42:11 +0200 | [diff] [blame] | 1048 | memcpy(init_data.gates, pn544_gates, sizeof(pn544_gates)); |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 1049 | |
| 1050 | /* |
| 1051 | * TODO: Session id must include the driver name + some bus addr |
| 1052 | * persistent info to discriminate 2 identical chips |
| 1053 | */ |
| 1054 | strcpy(init_data.session_id, "ID544HCI"); |
| 1055 | |
| 1056 | protocols = NFC_PROTO_JEWEL_MASK | |
| 1057 | NFC_PROTO_MIFARE_MASK | |
| 1058 | NFC_PROTO_FELICA_MASK | |
| 1059 | NFC_PROTO_ISO14443_MASK | |
Samuel Ortiz | 01d719a | 2012-07-04 00:14:04 +0200 | [diff] [blame] | 1060 | NFC_PROTO_ISO14443_B_MASK | |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 1061 | NFC_PROTO_NFC_DEP_MASK; |
| 1062 | |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 1063 | info->hdev = nfc_hci_allocate_device(&pn544_hci_ops, &init_data, |
| 1064 | protocols, LLC_SHDLC_NAME, |
| 1065 | PN544_FRAME_HEADROOM + |
| 1066 | PN544_CMDS_HEADROOM, |
| 1067 | PN544_FRAME_TAILROOM, |
| 1068 | PN544_HCI_LLC_MAX_PAYLOAD); |
| 1069 | if (!info->hdev) { |
| 1070 | dev_err(&client->dev, "Cannot allocate nfc hdev.\n"); |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 1071 | r = -ENOMEM; |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 1072 | goto err_alloc_hdev; |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 1073 | } |
| 1074 | |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 1075 | nfc_hci_set_clientdata(info->hdev, info); |
| 1076 | |
| 1077 | r = nfc_hci_register_device(info->hdev); |
| 1078 | if (r) |
| 1079 | goto err_regdev; |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 1080 | |
| 1081 | return 0; |
| 1082 | |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 1083 | err_regdev: |
| 1084 | nfc_hci_free_device(info->hdev); |
| 1085 | |
| 1086 | err_alloc_hdev: |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 1087 | free_irq(client->irq, info); |
| 1088 | |
| 1089 | err_rti: |
| 1090 | if (pdata->free_resources != NULL) |
| 1091 | pdata->free_resources(); |
| 1092 | |
| 1093 | err_pdata: |
| 1094 | kfree(info); |
| 1095 | |
| 1096 | err_info_alloc: |
| 1097 | return r; |
| 1098 | } |
| 1099 | |
| 1100 | static __devexit int pn544_hci_remove(struct i2c_client *client) |
| 1101 | { |
| 1102 | struct pn544_hci_info *info = i2c_get_clientdata(client); |
| 1103 | struct pn544_nfc_platform_data *pdata = client->dev.platform_data; |
| 1104 | |
| 1105 | dev_dbg(&client->dev, "%s\n", __func__); |
| 1106 | |
Eric Lapuyade | 412fda5 | 2012-09-18 19:45:48 +0200 | [diff] [blame] | 1107 | nfc_hci_free_device(info->hdev); |
Eric Lapuyade | bbed0de | 2012-05-07 12:31:29 +0200 | [diff] [blame] | 1108 | |
| 1109 | if (info->state != PN544_ST_COLD) { |
| 1110 | if (pdata->disable) |
| 1111 | pdata->disable(); |
| 1112 | } |
| 1113 | |
| 1114 | free_irq(client->irq, info); |
| 1115 | if (pdata->free_resources) |
| 1116 | pdata->free_resources(); |
| 1117 | |
| 1118 | kfree(info); |
| 1119 | |
| 1120 | return 0; |
| 1121 | } |
| 1122 | |
| 1123 | static struct i2c_driver pn544_hci_driver = { |
| 1124 | .driver = { |
| 1125 | .name = PN544_HCI_DRIVER_NAME, |
| 1126 | }, |
| 1127 | .probe = pn544_hci_probe, |
| 1128 | .id_table = pn544_hci_id_table, |
| 1129 | .remove = __devexit_p(pn544_hci_remove), |
| 1130 | }; |
| 1131 | |
| 1132 | static int __init pn544_hci_init(void) |
| 1133 | { |
| 1134 | int r; |
| 1135 | |
| 1136 | pr_debug(DRIVER_DESC ": %s\n", __func__); |
| 1137 | |
| 1138 | r = i2c_add_driver(&pn544_hci_driver); |
| 1139 | if (r) { |
| 1140 | pr_err(PN544_HCI_DRIVER_NAME ": driver registration failed\n"); |
| 1141 | return r; |
| 1142 | } |
| 1143 | |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
| 1147 | static void __exit pn544_hci_exit(void) |
| 1148 | { |
| 1149 | i2c_del_driver(&pn544_hci_driver); |
| 1150 | } |
| 1151 | |
| 1152 | module_init(pn544_hci_init); |
| 1153 | module_exit(pn544_hci_exit); |
| 1154 | |
| 1155 | MODULE_LICENSE("GPL"); |
| 1156 | MODULE_DESCRIPTION(DRIVER_DESC); |