David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* ------------------------------------------------------------------------- |
| 3 | * Copyright (C) 2014-2016, Intel Corporation |
| 4 | * |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5 | * ------------------------------------------------------------------------- |
| 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/nfc.h> |
| 10 | #include <linux/i2c.h> |
| 11 | #include <linux/delay.h> |
| 12 | #include <linux/firmware.h> |
| 13 | #include <net/nfc/nci_core.h> |
| 14 | |
| 15 | #include "fdp.h" |
| 16 | |
| 17 | #define FDP_OTP_PATCH_NAME "otp.bin" |
| 18 | #define FDP_RAM_PATCH_NAME "ram.bin" |
| 19 | #define FDP_FW_HEADER_SIZE 576 |
| 20 | #define FDP_FW_UPDATE_SLEEP 1000 |
| 21 | |
| 22 | #define NCI_GET_VERSION_TIMEOUT 8000 |
| 23 | #define NCI_PATCH_REQUEST_TIMEOUT 8000 |
| 24 | #define FDP_PATCH_CONN_DEST 0xC2 |
| 25 | #define FDP_PATCH_CONN_PARAM_TYPE 0xA0 |
| 26 | |
| 27 | #define NCI_PATCH_TYPE_RAM 0x00 |
| 28 | #define NCI_PATCH_TYPE_OTP 0x01 |
| 29 | #define NCI_PATCH_TYPE_EOT 0xFF |
| 30 | |
| 31 | #define NCI_PARAM_ID_FW_RAM_VERSION 0xA0 |
| 32 | #define NCI_PARAM_ID_FW_OTP_VERSION 0xA1 |
| 33 | #define NCI_PARAM_ID_OTP_LIMITED_VERSION 0xC5 |
| 34 | #define NCI_PARAM_ID_KEY_INDEX_ID 0xC6 |
| 35 | |
| 36 | #define NCI_GID_PROP 0x0F |
| 37 | #define NCI_OP_PROP_PATCH_OID 0x08 |
| 38 | #define NCI_OP_PROP_SET_PDATA_OID 0x23 |
| 39 | |
| 40 | struct fdp_nci_info { |
| 41 | struct nfc_phy_ops *phy_ops; |
| 42 | struct fdp_i2c_phy *phy; |
| 43 | struct nci_dev *ndev; |
| 44 | |
| 45 | const struct firmware *otp_patch; |
| 46 | const struct firmware *ram_patch; |
| 47 | u32 otp_patch_version; |
| 48 | u32 ram_patch_version; |
| 49 | |
| 50 | u32 otp_version; |
| 51 | u32 ram_version; |
| 52 | u32 limited_otp_version; |
| 53 | u8 key_index; |
| 54 | |
| 55 | u8 *fw_vsc_cfg; |
| 56 | u8 clock_type; |
| 57 | u32 clock_freq; |
| 58 | |
| 59 | atomic_t data_pkt_counter; |
| 60 | void (*data_pkt_counter_cb)(struct nci_dev *ndev); |
| 61 | u8 setup_patch_sent; |
| 62 | u8 setup_patch_ntf; |
| 63 | u8 setup_patch_status; |
| 64 | u8 setup_reset_ntf; |
| 65 | wait_queue_head_t setup_wq; |
| 66 | }; |
| 67 | |
| 68 | static u8 nci_core_get_config_otp_ram_version[5] = { |
| 69 | 0x04, |
| 70 | NCI_PARAM_ID_FW_RAM_VERSION, |
| 71 | NCI_PARAM_ID_FW_OTP_VERSION, |
| 72 | NCI_PARAM_ID_OTP_LIMITED_VERSION, |
| 73 | NCI_PARAM_ID_KEY_INDEX_ID |
| 74 | }; |
| 75 | |
| 76 | struct nci_core_get_config_rsp { |
| 77 | u8 status; |
| 78 | u8 count; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 79 | u8 data[]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | static int fdp_nci_create_conn(struct nci_dev *ndev) |
| 83 | { |
| 84 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 85 | struct core_conn_create_dest_spec_params param; |
| 86 | int r; |
| 87 | |
| 88 | /* proprietary destination specific paramerer without value */ |
| 89 | param.type = FDP_PATCH_CONN_PARAM_TYPE; |
| 90 | param.length = 0x00; |
| 91 | |
| 92 | r = nci_core_conn_create(info->ndev, FDP_PATCH_CONN_DEST, 1, |
| 93 | sizeof(param), ¶m); |
| 94 | if (r) |
| 95 | return r; |
| 96 | |
| 97 | return nci_get_conn_info_by_dest_type_params(ndev, |
| 98 | FDP_PATCH_CONN_DEST, NULL); |
| 99 | } |
| 100 | |
| 101 | static inline int fdp_nci_get_versions(struct nci_dev *ndev) |
| 102 | { |
| 103 | return nci_core_cmd(ndev, NCI_OP_CORE_GET_CONFIG_CMD, |
| 104 | sizeof(nci_core_get_config_otp_ram_version), |
| 105 | (__u8 *) &nci_core_get_config_otp_ram_version); |
| 106 | } |
| 107 | |
| 108 | static inline int fdp_nci_patch_cmd(struct nci_dev *ndev, u8 type) |
| 109 | { |
| 110 | return nci_prop_cmd(ndev, NCI_OP_PROP_PATCH_OID, sizeof(type), &type); |
| 111 | } |
| 112 | |
| 113 | static inline int fdp_nci_set_production_data(struct nci_dev *ndev, u8 len, |
| 114 | char *data) |
| 115 | { |
| 116 | return nci_prop_cmd(ndev, NCI_OP_PROP_SET_PDATA_OID, len, data); |
| 117 | } |
| 118 | |
| 119 | static int fdp_nci_set_clock(struct nci_dev *ndev, u8 clock_type, |
| 120 | u32 clock_freq) |
| 121 | { |
| 122 | u32 fc = 13560; |
| 123 | u32 nd, num, delta; |
| 124 | char data[9]; |
| 125 | |
| 126 | nd = (24 * fc) / clock_freq; |
| 127 | delta = 24 * fc - nd * clock_freq; |
| 128 | num = (32768 * delta) / clock_freq; |
| 129 | |
| 130 | data[0] = 0x00; |
| 131 | data[1] = 0x00; |
| 132 | data[2] = 0x00; |
| 133 | |
| 134 | data[3] = 0x10; |
| 135 | data[4] = 0x04; |
| 136 | data[5] = num & 0xFF; |
| 137 | data[6] = (num >> 8) & 0xff; |
| 138 | data[7] = nd; |
| 139 | data[8] = clock_type; |
| 140 | |
| 141 | return fdp_nci_set_production_data(ndev, 9, data); |
| 142 | } |
| 143 | |
| 144 | static void fdp_nci_send_patch_cb(struct nci_dev *ndev) |
| 145 | { |
| 146 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 147 | |
| 148 | info->setup_patch_sent = 1; |
| 149 | wake_up(&info->setup_wq); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Register a packet sent counter and a callback |
| 154 | * |
| 155 | * We have no other way of knowing when all firmware packets were sent out |
| 156 | * on the i2c bus. We need to know that in order to close the connection and |
| 157 | * send the patch end message. |
| 158 | */ |
| 159 | static void fdp_nci_set_data_pkt_counter(struct nci_dev *ndev, |
| 160 | void (*cb)(struct nci_dev *ndev), int count) |
| 161 | { |
| 162 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 163 | struct device *dev = &info->phy->i2c_dev->dev; |
| 164 | |
| 165 | dev_dbg(dev, "NCI data pkt counter %d\n", count); |
| 166 | atomic_set(&info->data_pkt_counter, count); |
| 167 | info->data_pkt_counter_cb = cb; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * The device is expecting a stream of packets. All packets need to |
| 172 | * have the PBF flag set to 0x0 (last packet) even if the firmware |
| 173 | * file is segmented and there are multiple packets. If we give the |
| 174 | * whole firmware to nci_send_data it will segment it and it will set |
| 175 | * the PBF flag to 0x01 so we need to do the segmentation here. |
| 176 | * |
| 177 | * The firmware will be analyzed and applied when we send NCI_OP_PROP_PATCH_CMD |
| 178 | * command with NCI_PATCH_TYPE_EOT parameter. The device will send a |
| 179 | * NFCC_PATCH_NTF packaet and a NCI_OP_CORE_RESET_NTF packet. |
| 180 | */ |
| 181 | static int fdp_nci_send_patch(struct nci_dev *ndev, u8 conn_id, u8 type) |
| 182 | { |
| 183 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 184 | const struct firmware *fw; |
| 185 | struct sk_buff *skb; |
| 186 | unsigned long len; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 187 | int max_size, payload_size; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 188 | int rc = 0; |
| 189 | |
| 190 | if ((type == NCI_PATCH_TYPE_OTP && !info->otp_patch) || |
| 191 | (type == NCI_PATCH_TYPE_RAM && !info->ram_patch)) |
| 192 | return -EINVAL; |
| 193 | |
| 194 | if (type == NCI_PATCH_TYPE_OTP) |
| 195 | fw = info->otp_patch; |
| 196 | else |
| 197 | fw = info->ram_patch; |
| 198 | |
| 199 | max_size = nci_conn_max_data_pkt_payload_size(ndev, conn_id); |
| 200 | if (max_size <= 0) |
| 201 | return -EINVAL; |
| 202 | |
| 203 | len = fw->size; |
| 204 | |
| 205 | fdp_nci_set_data_pkt_counter(ndev, fdp_nci_send_patch_cb, |
| 206 | DIV_ROUND_UP(fw->size, max_size)); |
| 207 | |
| 208 | while (len) { |
| 209 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 210 | payload_size = min_t(unsigned long, max_size, len); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 211 | |
| 212 | skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + payload_size), |
| 213 | GFP_KERNEL); |
| 214 | if (!skb) { |
| 215 | fdp_nci_set_data_pkt_counter(ndev, NULL, 0); |
| 216 | return -ENOMEM; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | skb_reserve(skb, NCI_CTRL_HDR_SIZE); |
| 221 | |
| 222 | skb_put_data(skb, fw->data + (fw->size - len), payload_size); |
| 223 | |
| 224 | rc = nci_send_data(ndev, conn_id, skb); |
| 225 | |
| 226 | if (rc) { |
| 227 | fdp_nci_set_data_pkt_counter(ndev, NULL, 0); |
| 228 | return rc; |
| 229 | } |
| 230 | |
| 231 | len -= payload_size; |
| 232 | } |
| 233 | |
| 234 | return rc; |
| 235 | } |
| 236 | |
| 237 | static int fdp_nci_open(struct nci_dev *ndev) |
| 238 | { |
| 239 | int r; |
| 240 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 241 | struct device *dev = &info->phy->i2c_dev->dev; |
| 242 | |
| 243 | dev_dbg(dev, "%s\n", __func__); |
| 244 | |
| 245 | r = info->phy_ops->enable(info->phy); |
| 246 | |
| 247 | return r; |
| 248 | } |
| 249 | |
| 250 | static int fdp_nci_close(struct nci_dev *ndev) |
| 251 | { |
| 252 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 253 | struct device *dev = &info->phy->i2c_dev->dev; |
| 254 | |
| 255 | dev_dbg(dev, "%s\n", __func__); |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static int fdp_nci_send(struct nci_dev *ndev, struct sk_buff *skb) |
| 260 | { |
| 261 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 262 | struct device *dev = &info->phy->i2c_dev->dev; |
| 263 | |
| 264 | dev_dbg(dev, "%s\n", __func__); |
| 265 | |
| 266 | if (atomic_dec_and_test(&info->data_pkt_counter)) |
| 267 | info->data_pkt_counter_cb(ndev); |
| 268 | |
| 269 | return info->phy_ops->write(info->phy, skb); |
| 270 | } |
| 271 | |
| 272 | int fdp_nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb) |
| 273 | { |
| 274 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 275 | struct device *dev = &info->phy->i2c_dev->dev; |
| 276 | |
| 277 | dev_dbg(dev, "%s\n", __func__); |
| 278 | return nci_recv_frame(ndev, skb); |
| 279 | } |
| 280 | EXPORT_SYMBOL(fdp_nci_recv_frame); |
| 281 | |
| 282 | static int fdp_nci_request_firmware(struct nci_dev *ndev) |
| 283 | { |
| 284 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 285 | struct device *dev = &info->phy->i2c_dev->dev; |
| 286 | u8 *data; |
| 287 | int r; |
| 288 | |
| 289 | r = request_firmware(&info->ram_patch, FDP_RAM_PATCH_NAME, dev); |
| 290 | if (r < 0) { |
| 291 | nfc_err(dev, "RAM patch request error\n"); |
| 292 | goto error; |
| 293 | } |
| 294 | |
| 295 | data = (u8 *) info->ram_patch->data; |
| 296 | info->ram_patch_version = |
| 297 | data[FDP_FW_HEADER_SIZE] | |
| 298 | (data[FDP_FW_HEADER_SIZE + 1] << 8) | |
| 299 | (data[FDP_FW_HEADER_SIZE + 2] << 16) | |
| 300 | (data[FDP_FW_HEADER_SIZE + 3] << 24); |
| 301 | |
| 302 | dev_dbg(dev, "RAM patch version: %d, size: %d\n", |
| 303 | info->ram_patch_version, (int) info->ram_patch->size); |
| 304 | |
| 305 | |
| 306 | r = request_firmware(&info->otp_patch, FDP_OTP_PATCH_NAME, dev); |
| 307 | if (r < 0) { |
| 308 | nfc_err(dev, "OTP patch request error\n"); |
| 309 | goto out; |
| 310 | } |
| 311 | |
| 312 | data = (u8 *) info->otp_patch->data; |
| 313 | info->otp_patch_version = |
| 314 | data[FDP_FW_HEADER_SIZE] | |
| 315 | (data[FDP_FW_HEADER_SIZE + 1] << 8) | |
| 316 | (data[FDP_FW_HEADER_SIZE+2] << 16) | |
| 317 | (data[FDP_FW_HEADER_SIZE+3] << 24); |
| 318 | |
| 319 | dev_dbg(dev, "OTP patch version: %d, size: %d\n", |
| 320 | info->otp_patch_version, (int) info->otp_patch->size); |
| 321 | out: |
| 322 | return 0; |
| 323 | error: |
| 324 | return r; |
| 325 | } |
| 326 | |
| 327 | static void fdp_nci_release_firmware(struct nci_dev *ndev) |
| 328 | { |
| 329 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 330 | |
| 331 | if (info->otp_patch) { |
| 332 | release_firmware(info->otp_patch); |
| 333 | info->otp_patch = NULL; |
| 334 | } |
| 335 | |
| 336 | if (info->ram_patch) { |
| 337 | release_firmware(info->ram_patch); |
| 338 | info->ram_patch = NULL; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | static int fdp_nci_patch_otp(struct nci_dev *ndev) |
| 343 | { |
| 344 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 345 | struct device *dev = &info->phy->i2c_dev->dev; |
| 346 | int conn_id; |
| 347 | int r = 0; |
| 348 | |
| 349 | if (info->otp_version >= info->otp_patch_version) |
| 350 | goto out; |
| 351 | |
| 352 | info->setup_patch_sent = 0; |
| 353 | info->setup_reset_ntf = 0; |
| 354 | info->setup_patch_ntf = 0; |
| 355 | |
| 356 | /* Patch init request */ |
| 357 | r = fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_OTP); |
| 358 | if (r) |
| 359 | goto out; |
| 360 | |
| 361 | /* Patch data connection creation */ |
| 362 | conn_id = fdp_nci_create_conn(ndev); |
| 363 | if (conn_id < 0) { |
| 364 | r = conn_id; |
| 365 | goto out; |
| 366 | } |
| 367 | |
| 368 | /* Send the patch over the data connection */ |
| 369 | r = fdp_nci_send_patch(ndev, conn_id, NCI_PATCH_TYPE_OTP); |
| 370 | if (r) |
| 371 | goto out; |
| 372 | |
| 373 | /* Wait for all the packets to be send over i2c */ |
| 374 | wait_event_interruptible(info->setup_wq, |
| 375 | info->setup_patch_sent == 1); |
| 376 | |
| 377 | /* make sure that the NFCC processed the last data packet */ |
| 378 | msleep(FDP_FW_UPDATE_SLEEP); |
| 379 | |
| 380 | /* Close the data connection */ |
| 381 | r = nci_core_conn_close(info->ndev, conn_id); |
| 382 | if (r) |
| 383 | goto out; |
| 384 | |
| 385 | /* Patch finish message */ |
| 386 | if (fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_EOT)) { |
| 387 | nfc_err(dev, "OTP patch error 0x%x\n", r); |
| 388 | r = -EINVAL; |
| 389 | goto out; |
| 390 | } |
| 391 | |
| 392 | /* If the patch notification didn't arrive yet, wait for it */ |
| 393 | wait_event_interruptible(info->setup_wq, info->setup_patch_ntf); |
| 394 | |
| 395 | /* Check if the patching was successful */ |
| 396 | r = info->setup_patch_status; |
| 397 | if (r) { |
| 398 | nfc_err(dev, "OTP patch error 0x%x\n", r); |
| 399 | r = -EINVAL; |
| 400 | goto out; |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * We need to wait for the reset notification before we |
| 405 | * can continue |
| 406 | */ |
| 407 | wait_event_interruptible(info->setup_wq, info->setup_reset_ntf); |
| 408 | |
| 409 | out: |
| 410 | return r; |
| 411 | } |
| 412 | |
| 413 | static int fdp_nci_patch_ram(struct nci_dev *ndev) |
| 414 | { |
| 415 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 416 | struct device *dev = &info->phy->i2c_dev->dev; |
| 417 | int conn_id; |
| 418 | int r = 0; |
| 419 | |
| 420 | if (info->ram_version >= info->ram_patch_version) |
| 421 | goto out; |
| 422 | |
| 423 | info->setup_patch_sent = 0; |
| 424 | info->setup_reset_ntf = 0; |
| 425 | info->setup_patch_ntf = 0; |
| 426 | |
| 427 | /* Patch init request */ |
| 428 | r = fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_RAM); |
| 429 | if (r) |
| 430 | goto out; |
| 431 | |
| 432 | /* Patch data connection creation */ |
| 433 | conn_id = fdp_nci_create_conn(ndev); |
| 434 | if (conn_id < 0) { |
| 435 | r = conn_id; |
| 436 | goto out; |
| 437 | } |
| 438 | |
| 439 | /* Send the patch over the data connection */ |
| 440 | r = fdp_nci_send_patch(ndev, conn_id, NCI_PATCH_TYPE_RAM); |
| 441 | if (r) |
| 442 | goto out; |
| 443 | |
| 444 | /* Wait for all the packets to be send over i2c */ |
| 445 | wait_event_interruptible(info->setup_wq, |
| 446 | info->setup_patch_sent == 1); |
| 447 | |
| 448 | /* make sure that the NFCC processed the last data packet */ |
| 449 | msleep(FDP_FW_UPDATE_SLEEP); |
| 450 | |
| 451 | /* Close the data connection */ |
| 452 | r = nci_core_conn_close(info->ndev, conn_id); |
| 453 | if (r) |
| 454 | goto out; |
| 455 | |
| 456 | /* Patch finish message */ |
| 457 | if (fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_EOT)) { |
| 458 | nfc_err(dev, "RAM patch error 0x%x\n", r); |
| 459 | r = -EINVAL; |
| 460 | goto out; |
| 461 | } |
| 462 | |
| 463 | /* If the patch notification didn't arrive yet, wait for it */ |
| 464 | wait_event_interruptible(info->setup_wq, info->setup_patch_ntf); |
| 465 | |
| 466 | /* Check if the patching was successful */ |
| 467 | r = info->setup_patch_status; |
| 468 | if (r) { |
| 469 | nfc_err(dev, "RAM patch error 0x%x\n", r); |
| 470 | r = -EINVAL; |
| 471 | goto out; |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * We need to wait for the reset notification before we |
| 476 | * can continue |
| 477 | */ |
| 478 | wait_event_interruptible(info->setup_wq, info->setup_reset_ntf); |
| 479 | |
| 480 | out: |
| 481 | return r; |
| 482 | } |
| 483 | |
| 484 | static int fdp_nci_setup(struct nci_dev *ndev) |
| 485 | { |
| 486 | /* Format: total length followed by an NCI packet */ |
| 487 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 488 | struct device *dev = &info->phy->i2c_dev->dev; |
| 489 | int r; |
| 490 | u8 patched = 0; |
| 491 | |
| 492 | dev_dbg(dev, "%s\n", __func__); |
| 493 | |
| 494 | r = nci_core_init(ndev); |
| 495 | if (r) |
| 496 | goto error; |
| 497 | |
| 498 | /* Get RAM and OTP version */ |
| 499 | r = fdp_nci_get_versions(ndev); |
| 500 | if (r) |
| 501 | goto error; |
| 502 | |
| 503 | /* Load firmware from disk */ |
| 504 | r = fdp_nci_request_firmware(ndev); |
| 505 | if (r) |
| 506 | goto error; |
| 507 | |
| 508 | /* Update OTP */ |
| 509 | if (info->otp_version < info->otp_patch_version) { |
| 510 | r = fdp_nci_patch_otp(ndev); |
| 511 | if (r) |
| 512 | goto error; |
| 513 | patched = 1; |
| 514 | } |
| 515 | |
| 516 | /* Update RAM */ |
| 517 | if (info->ram_version < info->ram_patch_version) { |
| 518 | r = fdp_nci_patch_ram(ndev); |
| 519 | if (r) |
| 520 | goto error; |
| 521 | patched = 1; |
| 522 | } |
| 523 | |
| 524 | /* Release the firmware buffers */ |
| 525 | fdp_nci_release_firmware(ndev); |
| 526 | |
| 527 | /* If a patch was applied the new version is checked */ |
| 528 | if (patched) { |
| 529 | r = nci_core_init(ndev); |
| 530 | if (r) |
| 531 | goto error; |
| 532 | |
| 533 | r = fdp_nci_get_versions(ndev); |
| 534 | if (r) |
| 535 | goto error; |
| 536 | |
| 537 | if (info->otp_version != info->otp_patch_version || |
| 538 | info->ram_version != info->ram_patch_version) { |
| 539 | nfc_err(dev, "Firmware update failed"); |
| 540 | r = -EINVAL; |
| 541 | goto error; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | * We initialized the devices but the NFC subsystem expects |
| 547 | * it to not be initialized. |
| 548 | */ |
| 549 | return nci_core_reset(ndev); |
| 550 | |
| 551 | error: |
| 552 | fdp_nci_release_firmware(ndev); |
| 553 | nfc_err(dev, "Setup error %d\n", r); |
| 554 | return r; |
| 555 | } |
| 556 | |
| 557 | static int fdp_nci_post_setup(struct nci_dev *ndev) |
| 558 | { |
| 559 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 560 | struct device *dev = &info->phy->i2c_dev->dev; |
| 561 | int r; |
| 562 | |
| 563 | /* Check if the device has VSC */ |
| 564 | if (info->fw_vsc_cfg && info->fw_vsc_cfg[0]) { |
| 565 | |
| 566 | /* Set the vendor specific configuration */ |
| 567 | r = fdp_nci_set_production_data(ndev, info->fw_vsc_cfg[3], |
| 568 | &info->fw_vsc_cfg[4]); |
| 569 | if (r) { |
| 570 | nfc_err(dev, "Vendor specific config set error %d\n", |
| 571 | r); |
| 572 | return r; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | /* Set clock type and frequency */ |
| 577 | r = fdp_nci_set_clock(ndev, info->clock_type, info->clock_freq); |
| 578 | if (r) { |
| 579 | nfc_err(dev, "Clock set error %d\n", r); |
| 580 | return r; |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | * In order to apply the VSC FDP needs a reset |
| 585 | */ |
| 586 | r = nci_core_reset(ndev); |
| 587 | if (r) |
| 588 | return r; |
| 589 | |
| 590 | /** |
| 591 | * The nci core was initialized when post setup was called |
| 592 | * so we leave it like that |
| 593 | */ |
| 594 | return nci_core_init(ndev); |
| 595 | } |
| 596 | |
| 597 | static int fdp_nci_core_reset_ntf_packet(struct nci_dev *ndev, |
| 598 | struct sk_buff *skb) |
| 599 | { |
| 600 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 601 | struct device *dev = &info->phy->i2c_dev->dev; |
| 602 | |
| 603 | dev_dbg(dev, "%s\n", __func__); |
| 604 | info->setup_reset_ntf = 1; |
| 605 | wake_up(&info->setup_wq); |
| 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | static int fdp_nci_prop_patch_ntf_packet(struct nci_dev *ndev, |
| 611 | struct sk_buff *skb) |
| 612 | { |
| 613 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 614 | struct device *dev = &info->phy->i2c_dev->dev; |
| 615 | |
| 616 | dev_dbg(dev, "%s\n", __func__); |
| 617 | info->setup_patch_ntf = 1; |
| 618 | info->setup_patch_status = skb->data[0]; |
| 619 | wake_up(&info->setup_wq); |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | static int fdp_nci_prop_patch_rsp_packet(struct nci_dev *ndev, |
| 625 | struct sk_buff *skb) |
| 626 | { |
| 627 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 628 | struct device *dev = &info->phy->i2c_dev->dev; |
| 629 | u8 status = skb->data[0]; |
| 630 | |
| 631 | dev_dbg(dev, "%s: status 0x%x\n", __func__, status); |
| 632 | nci_req_complete(ndev, status); |
| 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | static int fdp_nci_prop_set_production_data_rsp_packet(struct nci_dev *ndev, |
| 638 | struct sk_buff *skb) |
| 639 | { |
| 640 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 641 | struct device *dev = &info->phy->i2c_dev->dev; |
| 642 | u8 status = skb->data[0]; |
| 643 | |
| 644 | dev_dbg(dev, "%s: status 0x%x\n", __func__, status); |
| 645 | nci_req_complete(ndev, status); |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | static int fdp_nci_core_get_config_rsp_packet(struct nci_dev *ndev, |
| 651 | struct sk_buff *skb) |
| 652 | { |
| 653 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 654 | struct device *dev = &info->phy->i2c_dev->dev; |
| 655 | struct nci_core_get_config_rsp *rsp = (void *) skb->data; |
| 656 | u8 i, *p; |
| 657 | |
| 658 | if (rsp->status == NCI_STATUS_OK) { |
| 659 | |
| 660 | p = rsp->data; |
| 661 | for (i = 0; i < 4; i++) { |
| 662 | |
| 663 | switch (*p++) { |
| 664 | case NCI_PARAM_ID_FW_RAM_VERSION: |
| 665 | p++; |
| 666 | info->ram_version = le32_to_cpup((__le32 *) p); |
| 667 | p += 4; |
| 668 | break; |
| 669 | case NCI_PARAM_ID_FW_OTP_VERSION: |
| 670 | p++; |
| 671 | info->otp_version = le32_to_cpup((__le32 *) p); |
| 672 | p += 4; |
| 673 | break; |
| 674 | case NCI_PARAM_ID_OTP_LIMITED_VERSION: |
| 675 | p++; |
| 676 | info->otp_version = le32_to_cpup((__le32 *) p); |
| 677 | p += 4; |
| 678 | break; |
| 679 | case NCI_PARAM_ID_KEY_INDEX_ID: |
| 680 | p++; |
| 681 | info->key_index = *p++; |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | dev_dbg(dev, "OTP version %d\n", info->otp_version); |
| 687 | dev_dbg(dev, "RAM version %d\n", info->ram_version); |
| 688 | dev_dbg(dev, "key index %d\n", info->key_index); |
| 689 | dev_dbg(dev, "%s: status 0x%x\n", __func__, rsp->status); |
| 690 | |
| 691 | nci_req_complete(ndev, rsp->status); |
| 692 | |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | static struct nci_driver_ops fdp_core_ops[] = { |
| 697 | { |
| 698 | .opcode = NCI_OP_CORE_GET_CONFIG_RSP, |
| 699 | .rsp = fdp_nci_core_get_config_rsp_packet, |
| 700 | }, |
| 701 | { |
| 702 | .opcode = NCI_OP_CORE_RESET_NTF, |
| 703 | .ntf = fdp_nci_core_reset_ntf_packet, |
| 704 | }, |
| 705 | }; |
| 706 | |
| 707 | static struct nci_driver_ops fdp_prop_ops[] = { |
| 708 | { |
| 709 | .opcode = nci_opcode_pack(NCI_GID_PROP, NCI_OP_PROP_PATCH_OID), |
| 710 | .rsp = fdp_nci_prop_patch_rsp_packet, |
| 711 | .ntf = fdp_nci_prop_patch_ntf_packet, |
| 712 | }, |
| 713 | { |
| 714 | .opcode = nci_opcode_pack(NCI_GID_PROP, |
| 715 | NCI_OP_PROP_SET_PDATA_OID), |
| 716 | .rsp = fdp_nci_prop_set_production_data_rsp_packet, |
| 717 | }, |
| 718 | }; |
| 719 | |
| 720 | static struct nci_ops nci_ops = { |
| 721 | .open = fdp_nci_open, |
| 722 | .close = fdp_nci_close, |
| 723 | .send = fdp_nci_send, |
| 724 | .setup = fdp_nci_setup, |
| 725 | .post_setup = fdp_nci_post_setup, |
| 726 | .prop_ops = fdp_prop_ops, |
| 727 | .n_prop_ops = ARRAY_SIZE(fdp_prop_ops), |
| 728 | .core_ops = fdp_core_ops, |
| 729 | .n_core_ops = ARRAY_SIZE(fdp_core_ops), |
| 730 | }; |
| 731 | |
| 732 | int fdp_nci_probe(struct fdp_i2c_phy *phy, struct nfc_phy_ops *phy_ops, |
| 733 | struct nci_dev **ndevp, int tx_headroom, |
| 734 | int tx_tailroom, u8 clock_type, u32 clock_freq, |
| 735 | u8 *fw_vsc_cfg) |
| 736 | { |
| 737 | struct device *dev = &phy->i2c_dev->dev; |
| 738 | struct fdp_nci_info *info; |
| 739 | struct nci_dev *ndev; |
| 740 | u32 protocols; |
| 741 | int r; |
| 742 | |
| 743 | info = devm_kzalloc(dev, sizeof(struct fdp_nci_info), GFP_KERNEL); |
| 744 | if (!info) |
| 745 | return -ENOMEM; |
| 746 | |
| 747 | info->phy = phy; |
| 748 | info->phy_ops = phy_ops; |
| 749 | info->clock_type = clock_type; |
| 750 | info->clock_freq = clock_freq; |
| 751 | info->fw_vsc_cfg = fw_vsc_cfg; |
| 752 | |
| 753 | init_waitqueue_head(&info->setup_wq); |
| 754 | |
| 755 | protocols = NFC_PROTO_JEWEL_MASK | |
| 756 | NFC_PROTO_MIFARE_MASK | |
| 757 | NFC_PROTO_FELICA_MASK | |
| 758 | NFC_PROTO_ISO14443_MASK | |
| 759 | NFC_PROTO_ISO14443_B_MASK | |
| 760 | NFC_PROTO_NFC_DEP_MASK | |
| 761 | NFC_PROTO_ISO15693_MASK; |
| 762 | |
| 763 | ndev = nci_allocate_device(&nci_ops, protocols, tx_headroom, |
| 764 | tx_tailroom); |
| 765 | if (!ndev) { |
| 766 | nfc_err(dev, "Cannot allocate nfc ndev\n"); |
| 767 | return -ENOMEM; |
| 768 | } |
| 769 | |
| 770 | r = nci_register_device(ndev); |
| 771 | if (r) |
| 772 | goto err_regdev; |
| 773 | |
| 774 | *ndevp = ndev; |
| 775 | info->ndev = ndev; |
| 776 | |
| 777 | nci_set_drvdata(ndev, info); |
| 778 | |
| 779 | return 0; |
| 780 | |
| 781 | err_regdev: |
| 782 | nci_free_device(ndev); |
| 783 | return r; |
| 784 | } |
| 785 | EXPORT_SYMBOL(fdp_nci_probe); |
| 786 | |
| 787 | void fdp_nci_remove(struct nci_dev *ndev) |
| 788 | { |
| 789 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 790 | struct device *dev = &info->phy->i2c_dev->dev; |
| 791 | |
| 792 | dev_dbg(dev, "%s\n", __func__); |
| 793 | |
| 794 | nci_unregister_device(ndev); |
| 795 | nci_free_device(ndev); |
| 796 | } |
| 797 | EXPORT_SYMBOL(fdp_nci_remove); |
| 798 | |
| 799 | MODULE_LICENSE("GPL"); |
| 800 | MODULE_DESCRIPTION("NFC NCI driver for Intel Fields Peak NFC controller"); |
| 801 | MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>"); |