1562d4d59SThierry Escande /* 2562d4d59SThierry Escande * Sony NFC Port-100 Series driver 3562d4d59SThierry Escande * Copyright (c) 2013, Intel Corporation. 4562d4d59SThierry Escande * 5562d4d59SThierry Escande * Partly based/Inspired by Stephen Tiedemann's nfcpy 6562d4d59SThierry Escande * 7562d4d59SThierry Escande * This program is free software; you can redistribute it and/or modify it 8562d4d59SThierry Escande * under the terms and conditions of the GNU General Public License, 9562d4d59SThierry Escande * version 2, as published by the Free Software Foundation. 10562d4d59SThierry Escande * 11562d4d59SThierry Escande * This program is distributed in the hope it will be useful, but WITHOUT 12562d4d59SThierry Escande * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13562d4d59SThierry Escande * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14562d4d59SThierry Escande * more details. 15562d4d59SThierry Escande * 16562d4d59SThierry Escande */ 17562d4d59SThierry Escande 18562d4d59SThierry Escande #include <linux/module.h> 19562d4d59SThierry Escande #include <linux/usb.h> 20562d4d59SThierry Escande #include <net/nfc/digital.h> 21562d4d59SThierry Escande 22562d4d59SThierry Escande #define VERSION "0.1" 23562d4d59SThierry Escande 24562d4d59SThierry Escande #define SONY_VENDOR_ID 0x054c 259728ee92SOGAWA Hirofumi #define RCS380S_PRODUCT_ID 0x06c1 269728ee92SOGAWA Hirofumi #define RCS380P_PRODUCT_ID 0x06c3 27562d4d59SThierry Escande 28562d4d59SThierry Escande #define PORT100_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \ 29562d4d59SThierry Escande NFC_PROTO_MIFARE_MASK | \ 30562d4d59SThierry Escande NFC_PROTO_FELICA_MASK | \ 312a26f9a2SThierry Escande NFC_PROTO_NFC_DEP_MASK | \ 3209592ccfSThierry Escande NFC_PROTO_ISO14443_MASK | \ 3309592ccfSThierry Escande NFC_PROTO_ISO14443_B_MASK) 34562d4d59SThierry Escande 35562d4d59SThierry Escande #define PORT100_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC | \ 36562d4d59SThierry Escande NFC_DIGITAL_DRV_CAPS_TG_CRC) 37562d4d59SThierry Escande 380347a6abSThierry Escande /* Standard port100 frame definitions */ 390347a6abSThierry Escande #define PORT100_FRAME_HEADER_LEN (sizeof(struct port100_frame) \ 400347a6abSThierry Escande + 2) /* data[0] CC, data[1] SCC */ 410347a6abSThierry Escande #define PORT100_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/ 420347a6abSThierry Escande 430347a6abSThierry Escande #define PORT100_COMM_RF_HEAD_MAX_LEN (sizeof(struct port100_tg_comm_rf_cmd)) 440347a6abSThierry Escande 450347a6abSThierry Escande /* 460347a6abSThierry Escande * Max extended frame payload len, excluding CC and SCC 470347a6abSThierry Escande * which are already in PORT100_FRAME_HEADER_LEN. 480347a6abSThierry Escande */ 490347a6abSThierry Escande #define PORT100_FRAME_MAX_PAYLOAD_LEN 1001 500347a6abSThierry Escande 510347a6abSThierry Escande #define PORT100_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2), 520347a6abSThierry Escande Postamble (1) */ 530347a6abSThierry Escande static u8 ack_frame[PORT100_FRAME_ACK_SIZE] = { 540347a6abSThierry Escande 0x00, 0x00, 0xff, 0x00, 0xff, 0x00 550347a6abSThierry Escande }; 560347a6abSThierry Escande 570347a6abSThierry Escande #define PORT100_FRAME_CHECKSUM(f) (f->data[le16_to_cpu(f->datalen)]) 580347a6abSThierry Escande #define PORT100_FRAME_POSTAMBLE(f) (f->data[le16_to_cpu(f->datalen) + 1]) 590347a6abSThierry Escande 600347a6abSThierry Escande /* start of frame */ 610347a6abSThierry Escande #define PORT100_FRAME_SOF 0x00FF 620347a6abSThierry Escande #define PORT100_FRAME_EXT 0xFFFF 630347a6abSThierry Escande #define PORT100_FRAME_ACK 0x00FF 640347a6abSThierry Escande 650347a6abSThierry Escande /* Port-100 command: in or out */ 660347a6abSThierry Escande #define PORT100_FRAME_DIRECTION(f) (f->data[0]) /* CC */ 670347a6abSThierry Escande #define PORT100_FRAME_DIR_OUT 0xD6 680347a6abSThierry Escande #define PORT100_FRAME_DIR_IN 0xD7 690347a6abSThierry Escande 700347a6abSThierry Escande /* Port-100 sub-command */ 710347a6abSThierry Escande #define PORT100_FRAME_CMD(f) (f->data[1]) /* SCC */ 720347a6abSThierry Escande 730347a6abSThierry Escande #define PORT100_CMD_GET_FIRMWARE_VERSION 0x20 740347a6abSThierry Escande #define PORT100_CMD_GET_COMMAND_TYPE 0x28 750347a6abSThierry Escande #define PORT100_CMD_SET_COMMAND_TYPE 0x2A 760347a6abSThierry Escande 779f7b57f2SThierry Escande #define PORT100_CMD_IN_SET_RF 0x00 789f7b57f2SThierry Escande #define PORT100_CMD_IN_SET_PROTOCOL 0x02 799f7b57f2SThierry Escande #define PORT100_CMD_IN_COMM_RF 0x04 809f7b57f2SThierry Escande 817227c021SThierry Escande #define PORT100_CMD_TG_SET_RF 0x40 827227c021SThierry Escande #define PORT100_CMD_TG_SET_PROTOCOL 0x42 837227c021SThierry Escande #define PORT100_CMD_TG_SET_RF_OFF 0x46 847227c021SThierry Escande #define PORT100_CMD_TG_COMM_RF 0x48 857227c021SThierry Escande 869f7b57f2SThierry Escande #define PORT100_CMD_SWITCH_RF 0x06 879f7b57f2SThierry Escande 880347a6abSThierry Escande #define PORT100_CMD_RESPONSE(cmd) (cmd + 1) 890347a6abSThierry Escande 900347a6abSThierry Escande #define PORT100_CMD_TYPE_IS_SUPPORTED(mask, cmd_type) \ 910347a6abSThierry Escande ((mask) & (0x01 << (cmd_type))) 920347a6abSThierry Escande #define PORT100_CMD_TYPE_0 0 930347a6abSThierry Escande #define PORT100_CMD_TYPE_1 1 940347a6abSThierry Escande 959f7b57f2SThierry Escande #define PORT100_CMD_STATUS_OK 0x00 969f7b57f2SThierry Escande #define PORT100_CMD_STATUS_TIMEOUT 0x80 979f7b57f2SThierry Escande 987227c021SThierry Escande #define PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK 0x01 997227c021SThierry Escande #define PORT100_MDAA_TGT_WAS_ACTIVATED_MASK 0x02 1007227c021SThierry Escande 1010347a6abSThierry Escande struct port100; 1020347a6abSThierry Escande 1030347a6abSThierry Escande typedef void (*port100_send_async_complete_t)(struct port100 *dev, void *arg, 1040347a6abSThierry Escande struct sk_buff *resp); 1050347a6abSThierry Escande 1069f7b57f2SThierry Escande /** 1079f7b57f2SThierry Escande * Setting sets structure for in_set_rf command 1089f7b57f2SThierry Escande * 1099f7b57f2SThierry Escande * @in_*_set_number: Represent the entry indexes in the port-100 RF Base Table. 1109f7b57f2SThierry Escande * This table contains multiple RF setting sets required for RF 1119f7b57f2SThierry Escande * communication. 1129f7b57f2SThierry Escande * 1139f7b57f2SThierry Escande * @in_*_comm_type: Theses fields set the communication type to be used. 1149f7b57f2SThierry Escande */ 1159f7b57f2SThierry Escande struct port100_in_rf_setting { 1169f7b57f2SThierry Escande u8 in_send_set_number; 1179f7b57f2SThierry Escande u8 in_send_comm_type; 1189f7b57f2SThierry Escande u8 in_recv_set_number; 1199f7b57f2SThierry Escande u8 in_recv_comm_type; 1209f7b57f2SThierry Escande } __packed; 1219f7b57f2SThierry Escande 1229f7b57f2SThierry Escande #define PORT100_COMM_TYPE_IN_212F 0x01 1239f7b57f2SThierry Escande #define PORT100_COMM_TYPE_IN_424F 0x02 1249f7b57f2SThierry Escande #define PORT100_COMM_TYPE_IN_106A 0x03 12509592ccfSThierry Escande #define PORT100_COMM_TYPE_IN_106B 0x07 1269f7b57f2SThierry Escande 1279f7b57f2SThierry Escande static const struct port100_in_rf_setting in_rf_settings[] = { 1289f7b57f2SThierry Escande [NFC_DIGITAL_RF_TECH_212F] = { 1299f7b57f2SThierry Escande .in_send_set_number = 1, 1309f7b57f2SThierry Escande .in_send_comm_type = PORT100_COMM_TYPE_IN_212F, 1319f7b57f2SThierry Escande .in_recv_set_number = 15, 1329f7b57f2SThierry Escande .in_recv_comm_type = PORT100_COMM_TYPE_IN_212F, 1339f7b57f2SThierry Escande }, 1349f7b57f2SThierry Escande [NFC_DIGITAL_RF_TECH_424F] = { 1359f7b57f2SThierry Escande .in_send_set_number = 1, 1369f7b57f2SThierry Escande .in_send_comm_type = PORT100_COMM_TYPE_IN_424F, 1379f7b57f2SThierry Escande .in_recv_set_number = 15, 1389f7b57f2SThierry Escande .in_recv_comm_type = PORT100_COMM_TYPE_IN_424F, 1399f7b57f2SThierry Escande }, 1409f7b57f2SThierry Escande [NFC_DIGITAL_RF_TECH_106A] = { 1419f7b57f2SThierry Escande .in_send_set_number = 2, 1429f7b57f2SThierry Escande .in_send_comm_type = PORT100_COMM_TYPE_IN_106A, 1439f7b57f2SThierry Escande .in_recv_set_number = 15, 1449f7b57f2SThierry Escande .in_recv_comm_type = PORT100_COMM_TYPE_IN_106A, 1459f7b57f2SThierry Escande }, 14609592ccfSThierry Escande [NFC_DIGITAL_RF_TECH_106B] = { 14709592ccfSThierry Escande .in_send_set_number = 3, 14809592ccfSThierry Escande .in_send_comm_type = PORT100_COMM_TYPE_IN_106B, 14909592ccfSThierry Escande .in_recv_set_number = 15, 15009592ccfSThierry Escande .in_recv_comm_type = PORT100_COMM_TYPE_IN_106B, 15109592ccfSThierry Escande }, 152d3815ea9SThierry Escande /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */ 153d3815ea9SThierry Escande [NFC_DIGITAL_RF_TECH_LAST] = { 0 }, 1549f7b57f2SThierry Escande }; 1559f7b57f2SThierry Escande 1567227c021SThierry Escande /** 1577227c021SThierry Escande * Setting sets structure for tg_set_rf command 1587227c021SThierry Escande * 1597227c021SThierry Escande * @tg_set_number: Represents the entry index in the port-100 RF Base Table. 1607227c021SThierry Escande * This table contains multiple RF setting sets required for RF 1617227c021SThierry Escande * communication. this field is used for both send and receive 1627227c021SThierry Escande * settings. 1637227c021SThierry Escande * 1647227c021SThierry Escande * @tg_comm_type: Sets the communication type to be used to send and receive 1657227c021SThierry Escande * data. 1667227c021SThierry Escande */ 1677227c021SThierry Escande struct port100_tg_rf_setting { 1687227c021SThierry Escande u8 tg_set_number; 1697227c021SThierry Escande u8 tg_comm_type; 1707227c021SThierry Escande } __packed; 1717227c021SThierry Escande 1727227c021SThierry Escande #define PORT100_COMM_TYPE_TG_106A 0x0B 1737227c021SThierry Escande #define PORT100_COMM_TYPE_TG_212F 0x0C 1747227c021SThierry Escande #define PORT100_COMM_TYPE_TG_424F 0x0D 1757227c021SThierry Escande 1767227c021SThierry Escande static const struct port100_tg_rf_setting tg_rf_settings[] = { 1777227c021SThierry Escande [NFC_DIGITAL_RF_TECH_106A] = { 1787227c021SThierry Escande .tg_set_number = 8, 1797227c021SThierry Escande .tg_comm_type = PORT100_COMM_TYPE_TG_106A, 1807227c021SThierry Escande }, 1817227c021SThierry Escande [NFC_DIGITAL_RF_TECH_212F] = { 1827227c021SThierry Escande .tg_set_number = 8, 1837227c021SThierry Escande .tg_comm_type = PORT100_COMM_TYPE_TG_212F, 1847227c021SThierry Escande }, 1857227c021SThierry Escande [NFC_DIGITAL_RF_TECH_424F] = { 1867227c021SThierry Escande .tg_set_number = 8, 1877227c021SThierry Escande .tg_comm_type = PORT100_COMM_TYPE_TG_424F, 1887227c021SThierry Escande }, 189d3815ea9SThierry Escande /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */ 190d3815ea9SThierry Escande [NFC_DIGITAL_RF_TECH_LAST] = { 0 }, 191d3815ea9SThierry Escande 1927227c021SThierry Escande }; 1937227c021SThierry Escande 1949f7b57f2SThierry Escande #define PORT100_IN_PROT_INITIAL_GUARD_TIME 0x00 1959f7b57f2SThierry Escande #define PORT100_IN_PROT_ADD_CRC 0x01 1969f7b57f2SThierry Escande #define PORT100_IN_PROT_CHECK_CRC 0x02 1979f7b57f2SThierry Escande #define PORT100_IN_PROT_MULTI_CARD 0x03 1989f7b57f2SThierry Escande #define PORT100_IN_PROT_ADD_PARITY 0x04 1999f7b57f2SThierry Escande #define PORT100_IN_PROT_CHECK_PARITY 0x05 2009f7b57f2SThierry Escande #define PORT100_IN_PROT_BITWISE_AC_RECV_MODE 0x06 2019f7b57f2SThierry Escande #define PORT100_IN_PROT_VALID_BIT_NUMBER 0x07 2029f7b57f2SThierry Escande #define PORT100_IN_PROT_CRYPTO1 0x08 2039f7b57f2SThierry Escande #define PORT100_IN_PROT_ADD_SOF 0x09 2049f7b57f2SThierry Escande #define PORT100_IN_PROT_CHECK_SOF 0x0A 2059f7b57f2SThierry Escande #define PORT100_IN_PROT_ADD_EOF 0x0B 2069f7b57f2SThierry Escande #define PORT100_IN_PROT_CHECK_EOF 0x0C 2079f7b57f2SThierry Escande #define PORT100_IN_PROT_DEAF_TIME 0x0E 2089f7b57f2SThierry Escande #define PORT100_IN_PROT_CRM 0x0F 2099f7b57f2SThierry Escande #define PORT100_IN_PROT_CRM_MIN_LEN 0x10 2109f7b57f2SThierry Escande #define PORT100_IN_PROT_T1_TAG_FRAME 0x11 2119f7b57f2SThierry Escande #define PORT100_IN_PROT_RFCA 0x12 2129f7b57f2SThierry Escande #define PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR 0x13 2139f7b57f2SThierry Escande #define PORT100_IN_PROT_END 0x14 2149f7b57f2SThierry Escande 2159f7b57f2SThierry Escande #define PORT100_IN_MAX_NUM_PROTOCOLS 19 2169f7b57f2SThierry Escande 2177227c021SThierry Escande #define PORT100_TG_PROT_TU 0x00 2187227c021SThierry Escande #define PORT100_TG_PROT_RF_OFF 0x01 2197227c021SThierry Escande #define PORT100_TG_PROT_CRM 0x02 2207227c021SThierry Escande #define PORT100_TG_PROT_END 0x03 2217227c021SThierry Escande 2227227c021SThierry Escande #define PORT100_TG_MAX_NUM_PROTOCOLS 3 2237227c021SThierry Escande 2249f7b57f2SThierry Escande struct port100_protocol { 2259f7b57f2SThierry Escande u8 number; 2269f7b57f2SThierry Escande u8 value; 2279f7b57f2SThierry Escande } __packed; 2289f7b57f2SThierry Escande 2299f7b57f2SThierry Escande static struct port100_protocol 2309f7b57f2SThierry Escande in_protocols[][PORT100_IN_MAX_NUM_PROTOCOLS + 1] = { 2319f7b57f2SThierry Escande [NFC_DIGITAL_FRAMING_NFCA_SHORT] = { 2329f7b57f2SThierry Escande { PORT100_IN_PROT_INITIAL_GUARD_TIME, 6 }, 2339f7b57f2SThierry Escande { PORT100_IN_PROT_ADD_CRC, 0 }, 2349f7b57f2SThierry Escande { PORT100_IN_PROT_CHECK_CRC, 0 }, 2359f7b57f2SThierry Escande { PORT100_IN_PROT_MULTI_CARD, 0 }, 2369f7b57f2SThierry Escande { PORT100_IN_PROT_ADD_PARITY, 0 }, 2379f7b57f2SThierry Escande { PORT100_IN_PROT_CHECK_PARITY, 1 }, 2389f7b57f2SThierry Escande { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 }, 2399f7b57f2SThierry Escande { PORT100_IN_PROT_VALID_BIT_NUMBER, 7 }, 2409f7b57f2SThierry Escande { PORT100_IN_PROT_CRYPTO1, 0 }, 2419f7b57f2SThierry Escande { PORT100_IN_PROT_ADD_SOF, 0 }, 2429f7b57f2SThierry Escande { PORT100_IN_PROT_CHECK_SOF, 0 }, 2439f7b57f2SThierry Escande { PORT100_IN_PROT_ADD_EOF, 0 }, 2449f7b57f2SThierry Escande { PORT100_IN_PROT_CHECK_EOF, 0 }, 2459f7b57f2SThierry Escande { PORT100_IN_PROT_DEAF_TIME, 4 }, 2469f7b57f2SThierry Escande { PORT100_IN_PROT_CRM, 0 }, 2479f7b57f2SThierry Escande { PORT100_IN_PROT_CRM_MIN_LEN, 0 }, 2489f7b57f2SThierry Escande { PORT100_IN_PROT_T1_TAG_FRAME, 0 }, 2499f7b57f2SThierry Escande { PORT100_IN_PROT_RFCA, 0 }, 2509f7b57f2SThierry Escande { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 }, 2519f7b57f2SThierry Escande { PORT100_IN_PROT_END, 0 }, 2529f7b57f2SThierry Escande }, 2539f7b57f2SThierry Escande [NFC_DIGITAL_FRAMING_NFCA_STANDARD] = { 2549f7b57f2SThierry Escande { PORT100_IN_PROT_INITIAL_GUARD_TIME, 6 }, 2559f7b57f2SThierry Escande { PORT100_IN_PROT_ADD_CRC, 0 }, 2569f7b57f2SThierry Escande { PORT100_IN_PROT_CHECK_CRC, 0 }, 2579f7b57f2SThierry Escande { PORT100_IN_PROT_MULTI_CARD, 0 }, 2589f7b57f2SThierry Escande { PORT100_IN_PROT_ADD_PARITY, 1 }, 2599f7b57f2SThierry Escande { PORT100_IN_PROT_CHECK_PARITY, 1 }, 2609f7b57f2SThierry Escande { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 }, 2619f7b57f2SThierry Escande { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 }, 2629f7b57f2SThierry Escande { PORT100_IN_PROT_CRYPTO1, 0 }, 2639f7b57f2SThierry Escande { PORT100_IN_PROT_ADD_SOF, 0 }, 2649f7b57f2SThierry Escande { PORT100_IN_PROT_CHECK_SOF, 0 }, 2659f7b57f2SThierry Escande { PORT100_IN_PROT_ADD_EOF, 0 }, 2669f7b57f2SThierry Escande { PORT100_IN_PROT_CHECK_EOF, 0 }, 2679f7b57f2SThierry Escande { PORT100_IN_PROT_DEAF_TIME, 4 }, 2689f7b57f2SThierry Escande { PORT100_IN_PROT_CRM, 0 }, 2699f7b57f2SThierry Escande { PORT100_IN_PROT_CRM_MIN_LEN, 0 }, 2709f7b57f2SThierry Escande { PORT100_IN_PROT_T1_TAG_FRAME, 0 }, 2719f7b57f2SThierry Escande { PORT100_IN_PROT_RFCA, 0 }, 2729f7b57f2SThierry Escande { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 }, 2739f7b57f2SThierry Escande { PORT100_IN_PROT_END, 0 }, 2749f7b57f2SThierry Escande }, 2759f7b57f2SThierry Escande [NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A] = { 2769f7b57f2SThierry Escande { PORT100_IN_PROT_INITIAL_GUARD_TIME, 6 }, 2779f7b57f2SThierry Escande { PORT100_IN_PROT_ADD_CRC, 1 }, 2789f7b57f2SThierry Escande { PORT100_IN_PROT_CHECK_CRC, 1 }, 2799f7b57f2SThierry Escande { PORT100_IN_PROT_MULTI_CARD, 0 }, 2809f7b57f2SThierry Escande { PORT100_IN_PROT_ADD_PARITY, 1 }, 2819f7b57f2SThierry Escande { PORT100_IN_PROT_CHECK_PARITY, 1 }, 2829f7b57f2SThierry Escande { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 }, 2839f7b57f2SThierry Escande { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 }, 2849f7b57f2SThierry Escande { PORT100_IN_PROT_CRYPTO1, 0 }, 2859f7b57f2SThierry Escande { PORT100_IN_PROT_ADD_SOF, 0 }, 2869f7b57f2SThierry Escande { PORT100_IN_PROT_CHECK_SOF, 0 }, 2879f7b57f2SThierry Escande { PORT100_IN_PROT_ADD_EOF, 0 }, 2889f7b57f2SThierry Escande { PORT100_IN_PROT_CHECK_EOF, 0 }, 2899f7b57f2SThierry Escande { PORT100_IN_PROT_DEAF_TIME, 4 }, 2909f7b57f2SThierry Escande { PORT100_IN_PROT_CRM, 0 }, 2919f7b57f2SThierry Escande { PORT100_IN_PROT_CRM_MIN_LEN, 0 }, 2929f7b57f2SThierry Escande { PORT100_IN_PROT_T1_TAG_FRAME, 0 }, 2939f7b57f2SThierry Escande { PORT100_IN_PROT_RFCA, 0 }, 2949f7b57f2SThierry Escande { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 }, 2959f7b57f2SThierry Escande { PORT100_IN_PROT_END, 0 }, 2969f7b57f2SThierry Escande }, 2979f7b57f2SThierry Escande [NFC_DIGITAL_FRAMING_NFCA_T1T] = { 2989f7b57f2SThierry Escande /* nfc_digital_framing_nfca_short */ 2999f7b57f2SThierry Escande { PORT100_IN_PROT_ADD_CRC, 2 }, 3009f7b57f2SThierry Escande { PORT100_IN_PROT_CHECK_CRC, 2 }, 3019f7b57f2SThierry Escande { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 }, 3029f7b57f2SThierry Escande { PORT100_IN_PROT_T1_TAG_FRAME, 2 }, 3039f7b57f2SThierry Escande { PORT100_IN_PROT_END, 0 }, 3049f7b57f2SThierry Escande }, 3059f7b57f2SThierry Escande [NFC_DIGITAL_FRAMING_NFCA_T2T] = { 3069f7b57f2SThierry Escande /* nfc_digital_framing_nfca_standard */ 3079f7b57f2SThierry Escande { PORT100_IN_PROT_ADD_CRC, 1 }, 3089f7b57f2SThierry Escande { PORT100_IN_PROT_CHECK_CRC, 0 }, 3099f7b57f2SThierry Escande { PORT100_IN_PROT_END, 0 }, 3109f7b57f2SThierry Escande }, 3112a26f9a2SThierry Escande [NFC_DIGITAL_FRAMING_NFCA_T4T] = { 3122a26f9a2SThierry Escande /* nfc_digital_framing_nfca_standard_with_crc_a */ 3132a26f9a2SThierry Escande { PORT100_IN_PROT_END, 0 }, 3142a26f9a2SThierry Escande }, 3159f7b57f2SThierry Escande [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP] = { 3169f7b57f2SThierry Escande /* nfc_digital_framing_nfca_standard */ 3179f7b57f2SThierry Escande { PORT100_IN_PROT_END, 0 }, 3189f7b57f2SThierry Escande }, 3199f7b57f2SThierry Escande [NFC_DIGITAL_FRAMING_NFCF] = { 3209f7b57f2SThierry Escande { PORT100_IN_PROT_INITIAL_GUARD_TIME, 18 }, 3219f7b57f2SThierry Escande { PORT100_IN_PROT_ADD_CRC, 1 }, 3229f7b57f2SThierry Escande { PORT100_IN_PROT_CHECK_CRC, 1 }, 3239f7b57f2SThierry Escande { PORT100_IN_PROT_MULTI_CARD, 0 }, 3249f7b57f2SThierry Escande { PORT100_IN_PROT_ADD_PARITY, 0 }, 3259f7b57f2SThierry Escande { PORT100_IN_PROT_CHECK_PARITY, 0 }, 3269f7b57f2SThierry Escande { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 }, 3279f7b57f2SThierry Escande { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 }, 3289f7b57f2SThierry Escande { PORT100_IN_PROT_CRYPTO1, 0 }, 3299f7b57f2SThierry Escande { PORT100_IN_PROT_ADD_SOF, 0 }, 3309f7b57f2SThierry Escande { PORT100_IN_PROT_CHECK_SOF, 0 }, 3319f7b57f2SThierry Escande { PORT100_IN_PROT_ADD_EOF, 0 }, 3329f7b57f2SThierry Escande { PORT100_IN_PROT_CHECK_EOF, 0 }, 3339f7b57f2SThierry Escande { PORT100_IN_PROT_DEAF_TIME, 4 }, 3349f7b57f2SThierry Escande { PORT100_IN_PROT_CRM, 0 }, 3359f7b57f2SThierry Escande { PORT100_IN_PROT_CRM_MIN_LEN, 0 }, 3369f7b57f2SThierry Escande { PORT100_IN_PROT_T1_TAG_FRAME, 0 }, 3379f7b57f2SThierry Escande { PORT100_IN_PROT_RFCA, 0 }, 3389f7b57f2SThierry Escande { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 }, 3399f7b57f2SThierry Escande { PORT100_IN_PROT_END, 0 }, 3409f7b57f2SThierry Escande }, 3419f7b57f2SThierry Escande [NFC_DIGITAL_FRAMING_NFCF_T3T] = { 3429f7b57f2SThierry Escande /* nfc_digital_framing_nfcf */ 3439f7b57f2SThierry Escande { PORT100_IN_PROT_END, 0 }, 3449f7b57f2SThierry Escande }, 3459f7b57f2SThierry Escande [NFC_DIGITAL_FRAMING_NFCF_NFC_DEP] = { 3469f7b57f2SThierry Escande /* nfc_digital_framing_nfcf */ 347a81ba50aSThierry Escande { PORT100_IN_PROT_INITIAL_GUARD_TIME, 18 }, 348a81ba50aSThierry Escande { PORT100_IN_PROT_ADD_CRC, 1 }, 349a81ba50aSThierry Escande { PORT100_IN_PROT_CHECK_CRC, 1 }, 350a81ba50aSThierry Escande { PORT100_IN_PROT_MULTI_CARD, 0 }, 351a81ba50aSThierry Escande { PORT100_IN_PROT_ADD_PARITY, 0 }, 352a81ba50aSThierry Escande { PORT100_IN_PROT_CHECK_PARITY, 0 }, 353a81ba50aSThierry Escande { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 }, 354a81ba50aSThierry Escande { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 }, 355a81ba50aSThierry Escande { PORT100_IN_PROT_CRYPTO1, 0 }, 356a81ba50aSThierry Escande { PORT100_IN_PROT_ADD_SOF, 0 }, 357a81ba50aSThierry Escande { PORT100_IN_PROT_CHECK_SOF, 0 }, 358a81ba50aSThierry Escande { PORT100_IN_PROT_ADD_EOF, 0 }, 359a81ba50aSThierry Escande { PORT100_IN_PROT_CHECK_EOF, 0 }, 360a81ba50aSThierry Escande { PORT100_IN_PROT_DEAF_TIME, 4 }, 361a81ba50aSThierry Escande { PORT100_IN_PROT_CRM, 0 }, 362a81ba50aSThierry Escande { PORT100_IN_PROT_CRM_MIN_LEN, 0 }, 363a81ba50aSThierry Escande { PORT100_IN_PROT_T1_TAG_FRAME, 0 }, 364a81ba50aSThierry Escande { PORT100_IN_PROT_RFCA, 0 }, 365a81ba50aSThierry Escande { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 }, 3669f7b57f2SThierry Escande { PORT100_IN_PROT_END, 0 }, 3679f7b57f2SThierry Escande }, 3689f7b57f2SThierry Escande [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = { 3699f7b57f2SThierry Escande { PORT100_IN_PROT_END, 0 }, 3709f7b57f2SThierry Escande }, 37109592ccfSThierry Escande [NFC_DIGITAL_FRAMING_NFCB] = { 37209592ccfSThierry Escande { PORT100_IN_PROT_INITIAL_GUARD_TIME, 20 }, 37309592ccfSThierry Escande { PORT100_IN_PROT_ADD_CRC, 1 }, 37409592ccfSThierry Escande { PORT100_IN_PROT_CHECK_CRC, 1 }, 37509592ccfSThierry Escande { PORT100_IN_PROT_MULTI_CARD, 0 }, 37609592ccfSThierry Escande { PORT100_IN_PROT_ADD_PARITY, 0 }, 37709592ccfSThierry Escande { PORT100_IN_PROT_CHECK_PARITY, 0 }, 37809592ccfSThierry Escande { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 }, 37909592ccfSThierry Escande { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 }, 38009592ccfSThierry Escande { PORT100_IN_PROT_CRYPTO1, 0 }, 38109592ccfSThierry Escande { PORT100_IN_PROT_ADD_SOF, 1 }, 38209592ccfSThierry Escande { PORT100_IN_PROT_CHECK_SOF, 1 }, 38309592ccfSThierry Escande { PORT100_IN_PROT_ADD_EOF, 1 }, 38409592ccfSThierry Escande { PORT100_IN_PROT_CHECK_EOF, 1 }, 38509592ccfSThierry Escande { PORT100_IN_PROT_DEAF_TIME, 4 }, 38609592ccfSThierry Escande { PORT100_IN_PROT_CRM, 0 }, 38709592ccfSThierry Escande { PORT100_IN_PROT_CRM_MIN_LEN, 0 }, 38809592ccfSThierry Escande { PORT100_IN_PROT_T1_TAG_FRAME, 0 }, 38909592ccfSThierry Escande { PORT100_IN_PROT_RFCA, 0 }, 39009592ccfSThierry Escande { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 }, 39109592ccfSThierry Escande { PORT100_IN_PROT_END, 0 }, 39209592ccfSThierry Escande }, 39309592ccfSThierry Escande [NFC_DIGITAL_FRAMING_NFCB_T4T] = { 39409592ccfSThierry Escande /* nfc_digital_framing_nfcb */ 39509592ccfSThierry Escande { PORT100_IN_PROT_END, 0 }, 39609592ccfSThierry Escande }, 397d3815ea9SThierry Escande /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */ 398d3815ea9SThierry Escande [NFC_DIGITAL_FRAMING_LAST] = { 399d3815ea9SThierry Escande { PORT100_IN_PROT_END, 0 }, 400d3815ea9SThierry Escande }, 4019f7b57f2SThierry Escande }; 4029f7b57f2SThierry Escande 4037227c021SThierry Escande static struct port100_protocol 4047227c021SThierry Escande tg_protocols[][PORT100_TG_MAX_NUM_PROTOCOLS + 1] = { 4057227c021SThierry Escande [NFC_DIGITAL_FRAMING_NFCA_SHORT] = { 4067227c021SThierry Escande { PORT100_TG_PROT_END, 0 }, 4077227c021SThierry Escande }, 4087227c021SThierry Escande [NFC_DIGITAL_FRAMING_NFCA_STANDARD] = { 4097227c021SThierry Escande { PORT100_TG_PROT_END, 0 }, 4107227c021SThierry Escande }, 4117227c021SThierry Escande [NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A] = { 4127227c021SThierry Escande { PORT100_TG_PROT_END, 0 }, 4137227c021SThierry Escande }, 4147227c021SThierry Escande [NFC_DIGITAL_FRAMING_NFCA_T1T] = { 4157227c021SThierry Escande { PORT100_TG_PROT_END, 0 }, 4167227c021SThierry Escande }, 4177227c021SThierry Escande [NFC_DIGITAL_FRAMING_NFCA_T2T] = { 4187227c021SThierry Escande { PORT100_TG_PROT_END, 0 }, 4197227c021SThierry Escande }, 4207227c021SThierry Escande [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP] = { 4217227c021SThierry Escande { PORT100_TG_PROT_TU, 1 }, 4227227c021SThierry Escande { PORT100_TG_PROT_RF_OFF, 0 }, 4237227c021SThierry Escande { PORT100_TG_PROT_CRM, 7 }, 4247227c021SThierry Escande { PORT100_TG_PROT_END, 0 }, 4257227c021SThierry Escande }, 4267227c021SThierry Escande [NFC_DIGITAL_FRAMING_NFCF] = { 4277227c021SThierry Escande { PORT100_TG_PROT_END, 0 }, 4287227c021SThierry Escande }, 4297227c021SThierry Escande [NFC_DIGITAL_FRAMING_NFCF_T3T] = { 4307227c021SThierry Escande { PORT100_TG_PROT_END, 0 }, 4317227c021SThierry Escande }, 4327227c021SThierry Escande [NFC_DIGITAL_FRAMING_NFCF_NFC_DEP] = { 4337227c021SThierry Escande { PORT100_TG_PROT_TU, 1 }, 4347227c021SThierry Escande { PORT100_TG_PROT_RF_OFF, 0 }, 4357227c021SThierry Escande { PORT100_TG_PROT_CRM, 7 }, 4367227c021SThierry Escande { PORT100_TG_PROT_END, 0 }, 4377227c021SThierry Escande }, 4387227c021SThierry Escande [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = { 4397227c021SThierry Escande { PORT100_TG_PROT_RF_OFF, 1 }, 4407227c021SThierry Escande { PORT100_TG_PROT_END, 0 }, 4417227c021SThierry Escande }, 442d3815ea9SThierry Escande /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */ 443d3815ea9SThierry Escande [NFC_DIGITAL_FRAMING_LAST] = { 444d3815ea9SThierry Escande { PORT100_TG_PROT_END, 0 }, 445d3815ea9SThierry Escande }, 4467227c021SThierry Escande }; 4477227c021SThierry Escande 448562d4d59SThierry Escande struct port100 { 449562d4d59SThierry Escande struct nfc_digital_dev *nfc_digital_dev; 450562d4d59SThierry Escande 451562d4d59SThierry Escande int skb_headroom; 452562d4d59SThierry Escande int skb_tailroom; 453562d4d59SThierry Escande 454562d4d59SThierry Escande struct usb_device *udev; 455562d4d59SThierry Escande struct usb_interface *interface; 4560347a6abSThierry Escande 4570347a6abSThierry Escande struct urb *out_urb; 4580347a6abSThierry Escande struct urb *in_urb; 4590347a6abSThierry Escande 460b74584c1SThierry Escande /* This mutex protects the out_urb and avoids to submit a new command 461b74584c1SThierry Escande * through port100_send_frame_async() while the previous one is being 462b74584c1SThierry Escande * canceled through port100_abort_cmd(). 463b74584c1SThierry Escande */ 464b74584c1SThierry Escande struct mutex out_urb_lock; 465b74584c1SThierry Escande 4660347a6abSThierry Escande struct work_struct cmd_complete_work; 4670347a6abSThierry Escande 4680347a6abSThierry Escande u8 cmd_type; 4690347a6abSThierry Escande 4700347a6abSThierry Escande /* The digital stack serializes commands to be sent. There is no need 4710347a6abSThierry Escande * for any queuing/locking mechanism at driver level. 4720347a6abSThierry Escande */ 4730347a6abSThierry Escande struct port100_cmd *cmd; 474b74584c1SThierry Escande 475b74584c1SThierry Escande bool cmd_cancel; 476a52bd7d2SThierry Escande struct completion cmd_cancel_done; 477562d4d59SThierry Escande }; 478562d4d59SThierry Escande 4790347a6abSThierry Escande struct port100_cmd { 4800347a6abSThierry Escande u8 code; 4810347a6abSThierry Escande int status; 4820347a6abSThierry Escande struct sk_buff *req; 4830347a6abSThierry Escande struct sk_buff *resp; 4840347a6abSThierry Escande int resp_len; 4850347a6abSThierry Escande port100_send_async_complete_t complete_cb; 4860347a6abSThierry Escande void *complete_cb_context; 4870347a6abSThierry Escande }; 4880347a6abSThierry Escande 4890347a6abSThierry Escande struct port100_frame { 4900347a6abSThierry Escande u8 preamble; 4910347a6abSThierry Escande __be16 start_frame; 4920347a6abSThierry Escande __be16 extended_frame; 4930347a6abSThierry Escande __le16 datalen; 4940347a6abSThierry Escande u8 datalen_checksum; 4950347a6abSThierry Escande u8 data[]; 4960347a6abSThierry Escande } __packed; 4970347a6abSThierry Escande 4980347a6abSThierry Escande struct port100_ack_frame { 4990347a6abSThierry Escande u8 preamble; 5000347a6abSThierry Escande __be16 start_frame; 5010347a6abSThierry Escande __be16 ack_frame; 5020347a6abSThierry Escande u8 postambule; 5030347a6abSThierry Escande } __packed; 5040347a6abSThierry Escande 5050347a6abSThierry Escande struct port100_cb_arg { 5060347a6abSThierry Escande nfc_digital_cmd_complete_t complete_cb; 5070347a6abSThierry Escande void *complete_arg; 5080347a6abSThierry Escande u8 mdaa; 5090347a6abSThierry Escande }; 5100347a6abSThierry Escande 5110347a6abSThierry Escande struct port100_tg_comm_rf_cmd { 5120347a6abSThierry Escande __le16 guard_time; 5130347a6abSThierry Escande __le16 send_timeout; 5140347a6abSThierry Escande u8 mdaa; 5150347a6abSThierry Escande u8 nfca_param[6]; 5160347a6abSThierry Escande u8 nfcf_param[18]; 5170347a6abSThierry Escande u8 mf_halted; 5180347a6abSThierry Escande u8 arae_flag; 5190347a6abSThierry Escande __le16 recv_timeout; 5200347a6abSThierry Escande u8 data[]; 5210347a6abSThierry Escande } __packed; 5220347a6abSThierry Escande 5237227c021SThierry Escande struct port100_tg_comm_rf_res { 5247227c021SThierry Escande u8 comm_type; 5257227c021SThierry Escande u8 ar_status; 5267227c021SThierry Escande u8 target_activated; 5277227c021SThierry Escande __le32 status; 5287227c021SThierry Escande u8 data[]; 5297227c021SThierry Escande } __packed; 5307227c021SThierry Escande 5310347a6abSThierry Escande /* The rule: value + checksum = 0 */ 5320347a6abSThierry Escande static inline u8 port100_checksum(u16 value) 5330347a6abSThierry Escande { 5340347a6abSThierry Escande return ~(((u8 *)&value)[0] + ((u8 *)&value)[1]) + 1; 5350347a6abSThierry Escande } 5360347a6abSThierry Escande 5370347a6abSThierry Escande /* The rule: sum(data elements) + checksum = 0 */ 5380347a6abSThierry Escande static u8 port100_data_checksum(u8 *data, int datalen) 5390347a6abSThierry Escande { 5400347a6abSThierry Escande u8 sum = 0; 5410347a6abSThierry Escande int i; 5420347a6abSThierry Escande 5430347a6abSThierry Escande for (i = 0; i < datalen; i++) 5440347a6abSThierry Escande sum += data[i]; 5450347a6abSThierry Escande 5460347a6abSThierry Escande return port100_checksum(sum); 5470347a6abSThierry Escande } 5480347a6abSThierry Escande 5490347a6abSThierry Escande static void port100_tx_frame_init(void *_frame, u8 cmd_code) 5500347a6abSThierry Escande { 5510347a6abSThierry Escande struct port100_frame *frame = _frame; 5520347a6abSThierry Escande 5530347a6abSThierry Escande frame->preamble = 0; 5540347a6abSThierry Escande frame->start_frame = cpu_to_be16(PORT100_FRAME_SOF); 5550347a6abSThierry Escande frame->extended_frame = cpu_to_be16(PORT100_FRAME_EXT); 5560347a6abSThierry Escande PORT100_FRAME_DIRECTION(frame) = PORT100_FRAME_DIR_OUT; 5570347a6abSThierry Escande PORT100_FRAME_CMD(frame) = cmd_code; 5580347a6abSThierry Escande frame->datalen = cpu_to_le16(2); 5590347a6abSThierry Escande } 5600347a6abSThierry Escande 5610347a6abSThierry Escande static void port100_tx_frame_finish(void *_frame) 5620347a6abSThierry Escande { 5630347a6abSThierry Escande struct port100_frame *frame = _frame; 5640347a6abSThierry Escande 5650347a6abSThierry Escande frame->datalen_checksum = port100_checksum(le16_to_cpu(frame->datalen)); 5660347a6abSThierry Escande 5670347a6abSThierry Escande PORT100_FRAME_CHECKSUM(frame) = 5680347a6abSThierry Escande port100_data_checksum(frame->data, le16_to_cpu(frame->datalen)); 5690347a6abSThierry Escande 5700347a6abSThierry Escande PORT100_FRAME_POSTAMBLE(frame) = 0; 5710347a6abSThierry Escande } 5720347a6abSThierry Escande 5730347a6abSThierry Escande static void port100_tx_update_payload_len(void *_frame, int len) 5740347a6abSThierry Escande { 5750347a6abSThierry Escande struct port100_frame *frame = _frame; 5760347a6abSThierry Escande 5770347a6abSThierry Escande frame->datalen = cpu_to_le16(le16_to_cpu(frame->datalen) + len); 5780347a6abSThierry Escande } 5790347a6abSThierry Escande 5800347a6abSThierry Escande static bool port100_rx_frame_is_valid(void *_frame) 5810347a6abSThierry Escande { 5820347a6abSThierry Escande u8 checksum; 5830347a6abSThierry Escande struct port100_frame *frame = _frame; 5840347a6abSThierry Escande 5850347a6abSThierry Escande if (frame->start_frame != cpu_to_be16(PORT100_FRAME_SOF) || 5860347a6abSThierry Escande frame->extended_frame != cpu_to_be16(PORT100_FRAME_EXT)) 5870347a6abSThierry Escande return false; 5880347a6abSThierry Escande 5890347a6abSThierry Escande checksum = port100_checksum(le16_to_cpu(frame->datalen)); 5900347a6abSThierry Escande if (checksum != frame->datalen_checksum) 5910347a6abSThierry Escande return false; 5920347a6abSThierry Escande 5930347a6abSThierry Escande checksum = port100_data_checksum(frame->data, 5940347a6abSThierry Escande le16_to_cpu(frame->datalen)); 5950347a6abSThierry Escande if (checksum != PORT100_FRAME_CHECKSUM(frame)) 5960347a6abSThierry Escande return false; 5970347a6abSThierry Escande 5980347a6abSThierry Escande return true; 5990347a6abSThierry Escande } 6000347a6abSThierry Escande 6010347a6abSThierry Escande static bool port100_rx_frame_is_ack(struct port100_ack_frame *frame) 6020347a6abSThierry Escande { 6030347a6abSThierry Escande return (frame->start_frame == cpu_to_be16(PORT100_FRAME_SOF) && 6040347a6abSThierry Escande frame->ack_frame == cpu_to_be16(PORT100_FRAME_ACK)); 6050347a6abSThierry Escande } 6060347a6abSThierry Escande 6070347a6abSThierry Escande static inline int port100_rx_frame_size(void *frame) 6080347a6abSThierry Escande { 6090347a6abSThierry Escande struct port100_frame *f = frame; 6100347a6abSThierry Escande 6110347a6abSThierry Escande return sizeof(struct port100_frame) + le16_to_cpu(f->datalen) + 6120347a6abSThierry Escande PORT100_FRAME_TAIL_LEN; 6130347a6abSThierry Escande } 6140347a6abSThierry Escande 6150347a6abSThierry Escande static bool port100_rx_frame_is_cmd_response(struct port100 *dev, void *frame) 6160347a6abSThierry Escande { 6170347a6abSThierry Escande struct port100_frame *f = frame; 6180347a6abSThierry Escande 6190347a6abSThierry Escande return (PORT100_FRAME_CMD(f) == PORT100_CMD_RESPONSE(dev->cmd->code)); 6200347a6abSThierry Escande } 6210347a6abSThierry Escande 6220347a6abSThierry Escande static void port100_recv_response(struct urb *urb) 6230347a6abSThierry Escande { 6240347a6abSThierry Escande struct port100 *dev = urb->context; 6250347a6abSThierry Escande struct port100_cmd *cmd = dev->cmd; 6260347a6abSThierry Escande u8 *in_frame; 6270347a6abSThierry Escande 6280347a6abSThierry Escande cmd->status = urb->status; 6290347a6abSThierry Escande 6300347a6abSThierry Escande switch (urb->status) { 6310347a6abSThierry Escande case 0: 6320347a6abSThierry Escande break; /* success */ 6330347a6abSThierry Escande case -ECONNRESET: 6340347a6abSThierry Escande case -ENOENT: 6350347a6abSThierry Escande nfc_err(&dev->interface->dev, 6363590ebc0SJoe Perches "The urb has been canceled (status %d)\n", urb->status); 6370347a6abSThierry Escande goto sched_wq; 6380347a6abSThierry Escande case -ESHUTDOWN: 6390347a6abSThierry Escande default: 6403590ebc0SJoe Perches nfc_err(&dev->interface->dev, "Urb failure (status %d)\n", 6410347a6abSThierry Escande urb->status); 6420347a6abSThierry Escande goto sched_wq; 6430347a6abSThierry Escande } 6440347a6abSThierry Escande 6450347a6abSThierry Escande in_frame = dev->in_urb->transfer_buffer; 6460347a6abSThierry Escande 6470347a6abSThierry Escande if (!port100_rx_frame_is_valid(in_frame)) { 6483590ebc0SJoe Perches nfc_err(&dev->interface->dev, "Received an invalid frame\n"); 6490347a6abSThierry Escande cmd->status = -EIO; 6500347a6abSThierry Escande goto sched_wq; 6510347a6abSThierry Escande } 6520347a6abSThierry Escande 6530347a6abSThierry Escande print_hex_dump_debug("PORT100 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame, 6540347a6abSThierry Escande port100_rx_frame_size(in_frame), false); 6550347a6abSThierry Escande 6560347a6abSThierry Escande if (!port100_rx_frame_is_cmd_response(dev, in_frame)) { 6570347a6abSThierry Escande nfc_err(&dev->interface->dev, 6583590ebc0SJoe Perches "It's not the response to the last command\n"); 6590347a6abSThierry Escande cmd->status = -EIO; 6600347a6abSThierry Escande goto sched_wq; 6610347a6abSThierry Escande } 6620347a6abSThierry Escande 6630347a6abSThierry Escande sched_wq: 6640347a6abSThierry Escande schedule_work(&dev->cmd_complete_work); 6650347a6abSThierry Escande } 6660347a6abSThierry Escande 6670347a6abSThierry Escande static int port100_submit_urb_for_response(struct port100 *dev, gfp_t flags) 6680347a6abSThierry Escande { 6690347a6abSThierry Escande dev->in_urb->complete = port100_recv_response; 6700347a6abSThierry Escande 6710347a6abSThierry Escande return usb_submit_urb(dev->in_urb, flags); 6720347a6abSThierry Escande } 6730347a6abSThierry Escande 6740347a6abSThierry Escande static void port100_recv_ack(struct urb *urb) 6750347a6abSThierry Escande { 6760347a6abSThierry Escande struct port100 *dev = urb->context; 6770347a6abSThierry Escande struct port100_cmd *cmd = dev->cmd; 6780347a6abSThierry Escande struct port100_ack_frame *in_frame; 6790347a6abSThierry Escande int rc; 6800347a6abSThierry Escande 6810347a6abSThierry Escande cmd->status = urb->status; 6820347a6abSThierry Escande 6830347a6abSThierry Escande switch (urb->status) { 6840347a6abSThierry Escande case 0: 6850347a6abSThierry Escande break; /* success */ 6860347a6abSThierry Escande case -ECONNRESET: 6870347a6abSThierry Escande case -ENOENT: 6880347a6abSThierry Escande nfc_err(&dev->interface->dev, 6893590ebc0SJoe Perches "The urb has been stopped (status %d)\n", urb->status); 6900347a6abSThierry Escande goto sched_wq; 6910347a6abSThierry Escande case -ESHUTDOWN: 6920347a6abSThierry Escande default: 6933590ebc0SJoe Perches nfc_err(&dev->interface->dev, "Urb failure (status %d)\n", 6940347a6abSThierry Escande urb->status); 6950347a6abSThierry Escande goto sched_wq; 6960347a6abSThierry Escande } 6970347a6abSThierry Escande 6980347a6abSThierry Escande in_frame = dev->in_urb->transfer_buffer; 6990347a6abSThierry Escande 7000347a6abSThierry Escande if (!port100_rx_frame_is_ack(in_frame)) { 7013590ebc0SJoe Perches nfc_err(&dev->interface->dev, "Received an invalid ack\n"); 7020347a6abSThierry Escande cmd->status = -EIO; 7030347a6abSThierry Escande goto sched_wq; 7040347a6abSThierry Escande } 7050347a6abSThierry Escande 7060347a6abSThierry Escande rc = port100_submit_urb_for_response(dev, GFP_ATOMIC); 7070347a6abSThierry Escande if (rc) { 7080347a6abSThierry Escande nfc_err(&dev->interface->dev, 7093590ebc0SJoe Perches "usb_submit_urb failed with result %d\n", rc); 7100347a6abSThierry Escande cmd->status = rc; 7110347a6abSThierry Escande goto sched_wq; 7120347a6abSThierry Escande } 7130347a6abSThierry Escande 7140347a6abSThierry Escande return; 7150347a6abSThierry Escande 7160347a6abSThierry Escande sched_wq: 7170347a6abSThierry Escande schedule_work(&dev->cmd_complete_work); 7180347a6abSThierry Escande } 7190347a6abSThierry Escande 7200347a6abSThierry Escande static int port100_submit_urb_for_ack(struct port100 *dev, gfp_t flags) 7210347a6abSThierry Escande { 7220347a6abSThierry Escande dev->in_urb->complete = port100_recv_ack; 7230347a6abSThierry Escande 7240347a6abSThierry Escande return usb_submit_urb(dev->in_urb, flags); 7250347a6abSThierry Escande } 7260347a6abSThierry Escande 7270347a6abSThierry Escande static int port100_send_ack(struct port100 *dev) 7280347a6abSThierry Escande { 7290347a6abSThierry Escande int rc; 7300347a6abSThierry Escande 731b74584c1SThierry Escande mutex_lock(&dev->out_urb_lock); 732b74584c1SThierry Escande 733a52bd7d2SThierry Escande init_completion(&dev->cmd_cancel_done); 734a52bd7d2SThierry Escande 735b74584c1SThierry Escande usb_kill_urb(dev->out_urb); 736b74584c1SThierry Escande 7370347a6abSThierry Escande dev->out_urb->transfer_buffer = ack_frame; 7380347a6abSThierry Escande dev->out_urb->transfer_buffer_length = sizeof(ack_frame); 7390347a6abSThierry Escande rc = usb_submit_urb(dev->out_urb, GFP_KERNEL); 7400347a6abSThierry Escande 741b74584c1SThierry Escande /* Set the cmd_cancel flag only if the URB has been successfully 742b74584c1SThierry Escande * submitted. It will be reset by the out URB completion callback 743b74584c1SThierry Escande * port100_send_complete(). 744b74584c1SThierry Escande */ 745b74584c1SThierry Escande dev->cmd_cancel = !rc; 746b74584c1SThierry Escande 747b74584c1SThierry Escande mutex_unlock(&dev->out_urb_lock); 748b74584c1SThierry Escande 749a52bd7d2SThierry Escande if (!rc) 750a52bd7d2SThierry Escande wait_for_completion(&dev->cmd_cancel_done); 751a52bd7d2SThierry Escande 7520347a6abSThierry Escande return rc; 7530347a6abSThierry Escande } 7540347a6abSThierry Escande 7550347a6abSThierry Escande static int port100_send_frame_async(struct port100 *dev, struct sk_buff *out, 7560347a6abSThierry Escande struct sk_buff *in, int in_len) 7570347a6abSThierry Escande { 7580347a6abSThierry Escande int rc; 7590347a6abSThierry Escande 760b74584c1SThierry Escande mutex_lock(&dev->out_urb_lock); 761b74584c1SThierry Escande 762b74584c1SThierry Escande /* A command cancel frame as been sent through dev->out_urb. Don't try 763b74584c1SThierry Escande * to submit a new one. 764b74584c1SThierry Escande */ 765b74584c1SThierry Escande if (dev->cmd_cancel) { 766b74584c1SThierry Escande rc = -EAGAIN; 767b74584c1SThierry Escande goto exit; 768b74584c1SThierry Escande } 769b74584c1SThierry Escande 7700347a6abSThierry Escande dev->out_urb->transfer_buffer = out->data; 7710347a6abSThierry Escande dev->out_urb->transfer_buffer_length = out->len; 7720347a6abSThierry Escande 7730347a6abSThierry Escande dev->in_urb->transfer_buffer = in->data; 7740347a6abSThierry Escande dev->in_urb->transfer_buffer_length = in_len; 7750347a6abSThierry Escande 7760347a6abSThierry Escande print_hex_dump_debug("PORT100 TX: ", DUMP_PREFIX_NONE, 16, 1, 7770347a6abSThierry Escande out->data, out->len, false); 7780347a6abSThierry Escande 7790347a6abSThierry Escande rc = usb_submit_urb(dev->out_urb, GFP_KERNEL); 7800347a6abSThierry Escande if (rc) 781b74584c1SThierry Escande goto exit; 7820347a6abSThierry Escande 7830347a6abSThierry Escande rc = port100_submit_urb_for_ack(dev, GFP_KERNEL); 7840347a6abSThierry Escande if (rc) 7850347a6abSThierry Escande usb_unlink_urb(dev->out_urb); 786b74584c1SThierry Escande 787b74584c1SThierry Escande exit: 788b74584c1SThierry Escande mutex_unlock(&dev->out_urb_lock); 789b74584c1SThierry Escande 7900347a6abSThierry Escande return rc; 7910347a6abSThierry Escande } 7920347a6abSThierry Escande 7930347a6abSThierry Escande static void port100_build_cmd_frame(struct port100 *dev, u8 cmd_code, 7940347a6abSThierry Escande struct sk_buff *skb) 7950347a6abSThierry Escande { 7960347a6abSThierry Escande /* payload is already there, just update datalen */ 7970347a6abSThierry Escande int payload_len = skb->len; 7980347a6abSThierry Escande 7990347a6abSThierry Escande skb_push(skb, PORT100_FRAME_HEADER_LEN); 8000347a6abSThierry Escande skb_put(skb, PORT100_FRAME_TAIL_LEN); 8010347a6abSThierry Escande 8020347a6abSThierry Escande port100_tx_frame_init(skb->data, cmd_code); 8030347a6abSThierry Escande port100_tx_update_payload_len(skb->data, payload_len); 8040347a6abSThierry Escande port100_tx_frame_finish(skb->data); 8050347a6abSThierry Escande } 8060347a6abSThierry Escande 8070347a6abSThierry Escande static void port100_send_async_complete(struct port100 *dev) 8080347a6abSThierry Escande { 8090347a6abSThierry Escande struct port100_cmd *cmd = dev->cmd; 8100347a6abSThierry Escande int status = cmd->status; 8110347a6abSThierry Escande 8120347a6abSThierry Escande struct sk_buff *req = cmd->req; 8130347a6abSThierry Escande struct sk_buff *resp = cmd->resp; 8140347a6abSThierry Escande 8150347a6abSThierry Escande dev_kfree_skb(req); 8160347a6abSThierry Escande 8170347a6abSThierry Escande dev->cmd = NULL; 8180347a6abSThierry Escande 8190347a6abSThierry Escande if (status < 0) { 8200347a6abSThierry Escande cmd->complete_cb(dev, cmd->complete_cb_context, 8210347a6abSThierry Escande ERR_PTR(status)); 8220347a6abSThierry Escande dev_kfree_skb(resp); 8230347a6abSThierry Escande goto done; 8240347a6abSThierry Escande } 8250347a6abSThierry Escande 8260347a6abSThierry Escande skb_put(resp, port100_rx_frame_size(resp->data)); 8270347a6abSThierry Escande skb_pull(resp, PORT100_FRAME_HEADER_LEN); 8280347a6abSThierry Escande skb_trim(resp, resp->len - PORT100_FRAME_TAIL_LEN); 8290347a6abSThierry Escande 8300347a6abSThierry Escande cmd->complete_cb(dev, cmd->complete_cb_context, resp); 8310347a6abSThierry Escande 8320347a6abSThierry Escande done: 8330347a6abSThierry Escande kfree(cmd); 8340347a6abSThierry Escande } 8350347a6abSThierry Escande 8360347a6abSThierry Escande static int port100_send_cmd_async(struct port100 *dev, u8 cmd_code, 8370347a6abSThierry Escande struct sk_buff *req, 8380347a6abSThierry Escande port100_send_async_complete_t complete_cb, 8390347a6abSThierry Escande void *complete_cb_context) 8400347a6abSThierry Escande { 8410347a6abSThierry Escande struct port100_cmd *cmd; 8420347a6abSThierry Escande struct sk_buff *resp; 8430347a6abSThierry Escande int rc; 8440347a6abSThierry Escande int resp_len = PORT100_FRAME_HEADER_LEN + 8450347a6abSThierry Escande PORT100_FRAME_MAX_PAYLOAD_LEN + 8460347a6abSThierry Escande PORT100_FRAME_TAIL_LEN; 8470347a6abSThierry Escande 848e3e02588SThierry Escande if (dev->cmd) { 849e3e02588SThierry Escande nfc_err(&dev->interface->dev, 850e3e02588SThierry Escande "A command is still in process\n"); 851e3e02588SThierry Escande return -EBUSY; 852e3e02588SThierry Escande } 853e3e02588SThierry Escande 8540347a6abSThierry Escande resp = alloc_skb(resp_len, GFP_KERNEL); 8550347a6abSThierry Escande if (!resp) 8560347a6abSThierry Escande return -ENOMEM; 8570347a6abSThierry Escande 8580347a6abSThierry Escande cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 8590347a6abSThierry Escande if (!cmd) { 8600347a6abSThierry Escande dev_kfree_skb(resp); 8610347a6abSThierry Escande return -ENOMEM; 8620347a6abSThierry Escande } 8630347a6abSThierry Escande 8640347a6abSThierry Escande cmd->code = cmd_code; 8650347a6abSThierry Escande cmd->req = req; 8660347a6abSThierry Escande cmd->resp = resp; 8670347a6abSThierry Escande cmd->resp_len = resp_len; 8680347a6abSThierry Escande cmd->complete_cb = complete_cb; 8690347a6abSThierry Escande cmd->complete_cb_context = complete_cb_context; 8700347a6abSThierry Escande 8710347a6abSThierry Escande port100_build_cmd_frame(dev, cmd_code, req); 8720347a6abSThierry Escande 8730347a6abSThierry Escande dev->cmd = cmd; 8740347a6abSThierry Escande 8750347a6abSThierry Escande rc = port100_send_frame_async(dev, req, resp, resp_len); 8760347a6abSThierry Escande if (rc) { 8770347a6abSThierry Escande kfree(cmd); 8780347a6abSThierry Escande dev_kfree_skb(resp); 8790347a6abSThierry Escande dev->cmd = NULL; 8800347a6abSThierry Escande } 8810347a6abSThierry Escande 8820347a6abSThierry Escande return rc; 8830347a6abSThierry Escande } 8840347a6abSThierry Escande 8850347a6abSThierry Escande struct port100_sync_cmd_response { 8860347a6abSThierry Escande struct sk_buff *resp; 8870347a6abSThierry Escande struct completion done; 8880347a6abSThierry Escande }; 8890347a6abSThierry Escande 8900347a6abSThierry Escande static void port100_wq_cmd_complete(struct work_struct *work) 8910347a6abSThierry Escande { 8920347a6abSThierry Escande struct port100 *dev = container_of(work, struct port100, 8930347a6abSThierry Escande cmd_complete_work); 8940347a6abSThierry Escande 8950347a6abSThierry Escande port100_send_async_complete(dev); 8960347a6abSThierry Escande } 8970347a6abSThierry Escande 8980347a6abSThierry Escande static void port100_send_sync_complete(struct port100 *dev, void *_arg, 8990347a6abSThierry Escande struct sk_buff *resp) 9000347a6abSThierry Escande { 9010347a6abSThierry Escande struct port100_sync_cmd_response *arg = _arg; 9020347a6abSThierry Escande 9030347a6abSThierry Escande arg->resp = resp; 9040347a6abSThierry Escande complete(&arg->done); 9050347a6abSThierry Escande } 9060347a6abSThierry Escande 9070347a6abSThierry Escande static struct sk_buff *port100_send_cmd_sync(struct port100 *dev, u8 cmd_code, 9080347a6abSThierry Escande struct sk_buff *req) 9090347a6abSThierry Escande { 9100347a6abSThierry Escande int rc; 9110347a6abSThierry Escande struct port100_sync_cmd_response arg; 9120347a6abSThierry Escande 9130347a6abSThierry Escande init_completion(&arg.done); 9140347a6abSThierry Escande 9150347a6abSThierry Escande rc = port100_send_cmd_async(dev, cmd_code, req, 9160347a6abSThierry Escande port100_send_sync_complete, &arg); 9170347a6abSThierry Escande if (rc) { 9180347a6abSThierry Escande dev_kfree_skb(req); 9190347a6abSThierry Escande return ERR_PTR(rc); 9200347a6abSThierry Escande } 9210347a6abSThierry Escande 9220347a6abSThierry Escande wait_for_completion(&arg.done); 9230347a6abSThierry Escande 9240347a6abSThierry Escande return arg.resp; 9250347a6abSThierry Escande } 9260347a6abSThierry Escande 9270347a6abSThierry Escande static void port100_send_complete(struct urb *urb) 9280347a6abSThierry Escande { 9290347a6abSThierry Escande struct port100 *dev = urb->context; 9300347a6abSThierry Escande 931a52bd7d2SThierry Escande if (dev->cmd_cancel) { 932b74584c1SThierry Escande dev->cmd_cancel = false; 933a52bd7d2SThierry Escande complete(&dev->cmd_cancel_done); 934a52bd7d2SThierry Escande } 935b74584c1SThierry Escande 9360347a6abSThierry Escande switch (urb->status) { 9370347a6abSThierry Escande case 0: 9380347a6abSThierry Escande break; /* success */ 9390347a6abSThierry Escande case -ECONNRESET: 9400347a6abSThierry Escande case -ENOENT: 9410347a6abSThierry Escande nfc_err(&dev->interface->dev, 9423590ebc0SJoe Perches "The urb has been stopped (status %d)\n", urb->status); 9430347a6abSThierry Escande break; 9440347a6abSThierry Escande case -ESHUTDOWN: 9450347a6abSThierry Escande default: 9463590ebc0SJoe Perches nfc_err(&dev->interface->dev, "Urb failure (status %d)\n", 9470347a6abSThierry Escande urb->status); 9480347a6abSThierry Escande } 9490347a6abSThierry Escande } 9500347a6abSThierry Escande 951562d4d59SThierry Escande static void port100_abort_cmd(struct nfc_digital_dev *ddev) 952562d4d59SThierry Escande { 9530347a6abSThierry Escande struct port100 *dev = nfc_digital_get_drvdata(ddev); 9540347a6abSThierry Escande 9550347a6abSThierry Escande /* An ack will cancel the last issued command */ 9560347a6abSThierry Escande port100_send_ack(dev); 9570347a6abSThierry Escande 9580347a6abSThierry Escande /* cancel the urb request */ 9590347a6abSThierry Escande usb_kill_urb(dev->in_urb); 9600347a6abSThierry Escande } 9610347a6abSThierry Escande 9620347a6abSThierry Escande static struct sk_buff *port100_alloc_skb(struct port100 *dev, unsigned int size) 9630347a6abSThierry Escande { 9640347a6abSThierry Escande struct sk_buff *skb; 9650347a6abSThierry Escande 9660347a6abSThierry Escande skb = alloc_skb(dev->skb_headroom + dev->skb_tailroom + size, 9670347a6abSThierry Escande GFP_KERNEL); 9680347a6abSThierry Escande if (skb) 9690347a6abSThierry Escande skb_reserve(skb, dev->skb_headroom); 9700347a6abSThierry Escande 9710347a6abSThierry Escande return skb; 9720347a6abSThierry Escande } 9730347a6abSThierry Escande 9740347a6abSThierry Escande static int port100_set_command_type(struct port100 *dev, u8 command_type) 9750347a6abSThierry Escande { 9760347a6abSThierry Escande struct sk_buff *skb; 9770347a6abSThierry Escande struct sk_buff *resp; 9780347a6abSThierry Escande int rc; 9790347a6abSThierry Escande 9800347a6abSThierry Escande skb = port100_alloc_skb(dev, 1); 9810347a6abSThierry Escande if (!skb) 9820347a6abSThierry Escande return -ENOMEM; 9830347a6abSThierry Escande 9840347a6abSThierry Escande *skb_put(skb, sizeof(u8)) = command_type; 9850347a6abSThierry Escande 9860347a6abSThierry Escande resp = port100_send_cmd_sync(dev, PORT100_CMD_SET_COMMAND_TYPE, skb); 9870347a6abSThierry Escande if (IS_ERR(resp)) 9880347a6abSThierry Escande return PTR_ERR(resp); 9890347a6abSThierry Escande 9900347a6abSThierry Escande rc = resp->data[0]; 9910347a6abSThierry Escande 9920347a6abSThierry Escande dev_kfree_skb(resp); 9930347a6abSThierry Escande 9940347a6abSThierry Escande return rc; 9950347a6abSThierry Escande } 9960347a6abSThierry Escande 9970347a6abSThierry Escande static u64 port100_get_command_type_mask(struct port100 *dev) 9980347a6abSThierry Escande { 9990347a6abSThierry Escande struct sk_buff *skb; 10000347a6abSThierry Escande struct sk_buff *resp; 10010347a6abSThierry Escande u64 mask; 10020347a6abSThierry Escande 10030347a6abSThierry Escande skb = port100_alloc_skb(dev, 0); 10040347a6abSThierry Escande if (!skb) 10050347a6abSThierry Escande return -ENOMEM; 10060347a6abSThierry Escande 10070347a6abSThierry Escande resp = port100_send_cmd_sync(dev, PORT100_CMD_GET_COMMAND_TYPE, skb); 10080347a6abSThierry Escande if (IS_ERR(resp)) 10090347a6abSThierry Escande return PTR_ERR(resp); 10100347a6abSThierry Escande 10110347a6abSThierry Escande if (resp->len < 8) 10120347a6abSThierry Escande mask = 0; 10130347a6abSThierry Escande else 10140347a6abSThierry Escande mask = be64_to_cpu(*(__be64 *)resp->data); 10150347a6abSThierry Escande 10160347a6abSThierry Escande dev_kfree_skb(resp); 10170347a6abSThierry Escande 10180347a6abSThierry Escande return mask; 10190347a6abSThierry Escande } 10200347a6abSThierry Escande 10210347a6abSThierry Escande static u16 port100_get_firmware_version(struct port100 *dev) 10220347a6abSThierry Escande { 10230347a6abSThierry Escande struct sk_buff *skb; 10240347a6abSThierry Escande struct sk_buff *resp; 10250347a6abSThierry Escande u16 fw_ver; 10260347a6abSThierry Escande 10270347a6abSThierry Escande skb = port100_alloc_skb(dev, 0); 10280347a6abSThierry Escande if (!skb) 10290347a6abSThierry Escande return 0; 10300347a6abSThierry Escande 10310347a6abSThierry Escande resp = port100_send_cmd_sync(dev, PORT100_CMD_GET_FIRMWARE_VERSION, 10320347a6abSThierry Escande skb); 10330347a6abSThierry Escande if (IS_ERR(resp)) 10340347a6abSThierry Escande return 0; 10350347a6abSThierry Escande 10360347a6abSThierry Escande fw_ver = le16_to_cpu(*(__le16 *)resp->data); 10370347a6abSThierry Escande 10380347a6abSThierry Escande dev_kfree_skb(resp); 10390347a6abSThierry Escande 10400347a6abSThierry Escande return fw_ver; 1041562d4d59SThierry Escande } 1042562d4d59SThierry Escande 1043562d4d59SThierry Escande static int port100_switch_rf(struct nfc_digital_dev *ddev, bool on) 1044562d4d59SThierry Escande { 10459f7b57f2SThierry Escande struct port100 *dev = nfc_digital_get_drvdata(ddev); 10469f7b57f2SThierry Escande struct sk_buff *skb, *resp; 10479f7b57f2SThierry Escande 10489f7b57f2SThierry Escande skb = port100_alloc_skb(dev, 1); 10499f7b57f2SThierry Escande if (!skb) 10509f7b57f2SThierry Escande return -ENOMEM; 10519f7b57f2SThierry Escande 10529f7b57f2SThierry Escande *skb_put(skb, 1) = on ? 1 : 0; 10539f7b57f2SThierry Escande 10549f0c4542SThierry Escande /* Cancel the last command if the device is being switched off */ 10559f0c4542SThierry Escande if (!on) 10569f0c4542SThierry Escande port100_abort_cmd(ddev); 10579f0c4542SThierry Escande 10589f7b57f2SThierry Escande resp = port100_send_cmd_sync(dev, PORT100_CMD_SWITCH_RF, skb); 10599f7b57f2SThierry Escande 10609f7b57f2SThierry Escande if (IS_ERR(resp)) 10619f7b57f2SThierry Escande return PTR_ERR(resp); 10629f7b57f2SThierry Escande 10639f7b57f2SThierry Escande dev_kfree_skb(resp); 10649f7b57f2SThierry Escande 10659f7b57f2SThierry Escande return 0; 10669f7b57f2SThierry Escande } 10679f7b57f2SThierry Escande 10689f7b57f2SThierry Escande static int port100_in_set_rf(struct nfc_digital_dev *ddev, u8 rf) 10699f7b57f2SThierry Escande { 10709f7b57f2SThierry Escande struct port100 *dev = nfc_digital_get_drvdata(ddev); 10719f7b57f2SThierry Escande struct sk_buff *skb; 10729f7b57f2SThierry Escande struct sk_buff *resp; 10739f7b57f2SThierry Escande int rc; 10749f7b57f2SThierry Escande 10759f7b57f2SThierry Escande if (rf >= NFC_DIGITAL_RF_TECH_LAST) 10769f7b57f2SThierry Escande return -EINVAL; 10779f7b57f2SThierry Escande 10789f7b57f2SThierry Escande skb = port100_alloc_skb(dev, sizeof(struct port100_in_rf_setting)); 10799f7b57f2SThierry Escande if (!skb) 10809f7b57f2SThierry Escande return -ENOMEM; 10819f7b57f2SThierry Escande 10829f7b57f2SThierry Escande memcpy(skb_put(skb, sizeof(struct port100_in_rf_setting)), 10839f7b57f2SThierry Escande &in_rf_settings[rf], 10849f7b57f2SThierry Escande sizeof(struct port100_in_rf_setting)); 10859f7b57f2SThierry Escande 10869f7b57f2SThierry Escande resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_RF, skb); 10879f7b57f2SThierry Escande 10889f7b57f2SThierry Escande if (IS_ERR(resp)) 10899f7b57f2SThierry Escande return PTR_ERR(resp); 10909f7b57f2SThierry Escande 10919f7b57f2SThierry Escande rc = resp->data[0]; 10929f7b57f2SThierry Escande 10939f7b57f2SThierry Escande dev_kfree_skb(resp); 10949f7b57f2SThierry Escande 10959f7b57f2SThierry Escande return rc; 10969f7b57f2SThierry Escande } 10979f7b57f2SThierry Escande 10989f7b57f2SThierry Escande static int port100_in_set_framing(struct nfc_digital_dev *ddev, int param) 10999f7b57f2SThierry Escande { 11009f7b57f2SThierry Escande struct port100 *dev = nfc_digital_get_drvdata(ddev); 11019f7b57f2SThierry Escande struct port100_protocol *protocols; 11029f7b57f2SThierry Escande struct sk_buff *skb; 11039f7b57f2SThierry Escande struct sk_buff *resp; 11049f7b57f2SThierry Escande int num_protocols; 11059f7b57f2SThierry Escande size_t size; 11069f7b57f2SThierry Escande int rc; 11079f7b57f2SThierry Escande 11089f7b57f2SThierry Escande if (param >= NFC_DIGITAL_FRAMING_LAST) 11099f7b57f2SThierry Escande return -EINVAL; 11109f7b57f2SThierry Escande 11119f7b57f2SThierry Escande protocols = in_protocols[param]; 11129f7b57f2SThierry Escande 11139f7b57f2SThierry Escande num_protocols = 0; 11149f7b57f2SThierry Escande while (protocols[num_protocols].number != PORT100_IN_PROT_END) 11159f7b57f2SThierry Escande num_protocols++; 11169f7b57f2SThierry Escande 11179f7b57f2SThierry Escande if (!num_protocols) 11189f7b57f2SThierry Escande return 0; 11199f7b57f2SThierry Escande 11209f7b57f2SThierry Escande size = sizeof(struct port100_protocol) * num_protocols; 11219f7b57f2SThierry Escande 11229f7b57f2SThierry Escande skb = port100_alloc_skb(dev, size); 11239f7b57f2SThierry Escande if (!skb) 11249f7b57f2SThierry Escande return -ENOMEM; 11259f7b57f2SThierry Escande 11269f7b57f2SThierry Escande memcpy(skb_put(skb, size), protocols, size); 11279f7b57f2SThierry Escande 11289f7b57f2SThierry Escande resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_PROTOCOL, skb); 11299f7b57f2SThierry Escande 11309f7b57f2SThierry Escande if (IS_ERR(resp)) 11319f7b57f2SThierry Escande return PTR_ERR(resp); 11329f7b57f2SThierry Escande 11339f7b57f2SThierry Escande rc = resp->data[0]; 11349f7b57f2SThierry Escande 11359f7b57f2SThierry Escande dev_kfree_skb(resp); 11369f7b57f2SThierry Escande 11379f7b57f2SThierry Escande return rc; 1138562d4d59SThierry Escande } 1139562d4d59SThierry Escande 1140562d4d59SThierry Escande static int port100_in_configure_hw(struct nfc_digital_dev *ddev, int type, 1141562d4d59SThierry Escande int param) 1142562d4d59SThierry Escande { 11439f7b57f2SThierry Escande if (type == NFC_DIGITAL_CONFIG_RF_TECH) 11449f7b57f2SThierry Escande return port100_in_set_rf(ddev, param); 11459f7b57f2SThierry Escande 11469f7b57f2SThierry Escande if (type == NFC_DIGITAL_CONFIG_FRAMING) 11479f7b57f2SThierry Escande return port100_in_set_framing(ddev, param); 11489f7b57f2SThierry Escande 11499f7b57f2SThierry Escande return -EINVAL; 11509f7b57f2SThierry Escande } 11519f7b57f2SThierry Escande 11529f7b57f2SThierry Escande static void port100_in_comm_rf_complete(struct port100 *dev, void *arg, 11539f7b57f2SThierry Escande struct sk_buff *resp) 11549f7b57f2SThierry Escande { 11559f7b57f2SThierry Escande struct port100_cb_arg *cb_arg = arg; 11569f7b57f2SThierry Escande nfc_digital_cmd_complete_t cb = cb_arg->complete_cb; 11579f7b57f2SThierry Escande u32 status; 11589f7b57f2SThierry Escande int rc; 11599f7b57f2SThierry Escande 11609f7b57f2SThierry Escande if (IS_ERR(resp)) { 11619f7b57f2SThierry Escande rc = PTR_ERR(resp); 11629f7b57f2SThierry Escande goto exit; 11639f7b57f2SThierry Escande } 11649f7b57f2SThierry Escande 11659f7b57f2SThierry Escande if (resp->len < 4) { 11669f7b57f2SThierry Escande nfc_err(&dev->interface->dev, 11673590ebc0SJoe Perches "Invalid packet length received\n"); 11689f7b57f2SThierry Escande rc = -EIO; 11699f7b57f2SThierry Escande goto error; 11709f7b57f2SThierry Escande } 11719f7b57f2SThierry Escande 11729f7b57f2SThierry Escande status = le32_to_cpu(*(__le32 *)resp->data); 11739f7b57f2SThierry Escande 11749f7b57f2SThierry Escande skb_pull(resp, sizeof(u32)); 11759f7b57f2SThierry Escande 11769f7b57f2SThierry Escande if (status == PORT100_CMD_STATUS_TIMEOUT) { 11779f7b57f2SThierry Escande rc = -ETIMEDOUT; 11789f7b57f2SThierry Escande goto error; 11799f7b57f2SThierry Escande } 11809f7b57f2SThierry Escande 11819f7b57f2SThierry Escande if (status != PORT100_CMD_STATUS_OK) { 11829f7b57f2SThierry Escande nfc_err(&dev->interface->dev, 11839f7b57f2SThierry Escande "in_comm_rf failed with status 0x%08x\n", status); 11849f7b57f2SThierry Escande rc = -EIO; 11859f7b57f2SThierry Escande goto error; 11869f7b57f2SThierry Escande } 11879f7b57f2SThierry Escande 11889f7b57f2SThierry Escande /* Remove collision bits byte */ 11899f7b57f2SThierry Escande skb_pull(resp, 1); 11909f7b57f2SThierry Escande 11919f7b57f2SThierry Escande goto exit; 11929f7b57f2SThierry Escande 11939f7b57f2SThierry Escande error: 11949f7b57f2SThierry Escande kfree_skb(resp); 11959f7b57f2SThierry Escande resp = ERR_PTR(rc); 11969f7b57f2SThierry Escande 11979f7b57f2SThierry Escande exit: 11989f7b57f2SThierry Escande cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp); 11999f7b57f2SThierry Escande 12009f7b57f2SThierry Escande kfree(cb_arg); 1201562d4d59SThierry Escande } 1202562d4d59SThierry Escande 1203562d4d59SThierry Escande static int port100_in_send_cmd(struct nfc_digital_dev *ddev, 1204562d4d59SThierry Escande struct sk_buff *skb, u16 _timeout, 1205562d4d59SThierry Escande nfc_digital_cmd_complete_t cb, void *arg) 1206562d4d59SThierry Escande { 12079f7b57f2SThierry Escande struct port100 *dev = nfc_digital_get_drvdata(ddev); 12089f7b57f2SThierry Escande struct port100_cb_arg *cb_arg; 12099f7b57f2SThierry Escande __le16 timeout; 12109f7b57f2SThierry Escande 12119f7b57f2SThierry Escande cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL); 12129f7b57f2SThierry Escande if (!cb_arg) 12139f7b57f2SThierry Escande return -ENOMEM; 12149f7b57f2SThierry Escande 12159f7b57f2SThierry Escande cb_arg->complete_cb = cb; 12169f7b57f2SThierry Escande cb_arg->complete_arg = arg; 12179f7b57f2SThierry Escande 12189f7b57f2SThierry Escande timeout = cpu_to_le16(_timeout * 10); 12199f7b57f2SThierry Escande 12209f7b57f2SThierry Escande memcpy(skb_push(skb, sizeof(__le16)), &timeout, sizeof(__le16)); 12219f7b57f2SThierry Escande 12229f7b57f2SThierry Escande return port100_send_cmd_async(dev, PORT100_CMD_IN_COMM_RF, skb, 12239f7b57f2SThierry Escande port100_in_comm_rf_complete, cb_arg); 1224562d4d59SThierry Escande } 1225562d4d59SThierry Escande 12267227c021SThierry Escande static int port100_tg_set_rf(struct nfc_digital_dev *ddev, u8 rf) 12277227c021SThierry Escande { 12287227c021SThierry Escande struct port100 *dev = nfc_digital_get_drvdata(ddev); 12297227c021SThierry Escande struct sk_buff *skb; 12307227c021SThierry Escande struct sk_buff *resp; 12317227c021SThierry Escande int rc; 12327227c021SThierry Escande 12337227c021SThierry Escande if (rf >= NFC_DIGITAL_RF_TECH_LAST) 12347227c021SThierry Escande return -EINVAL; 12357227c021SThierry Escande 12367227c021SThierry Escande skb = port100_alloc_skb(dev, sizeof(struct port100_tg_rf_setting)); 12377227c021SThierry Escande if (!skb) 12387227c021SThierry Escande return -ENOMEM; 12397227c021SThierry Escande 12407227c021SThierry Escande memcpy(skb_put(skb, sizeof(struct port100_tg_rf_setting)), 12417227c021SThierry Escande &tg_rf_settings[rf], 12427227c021SThierry Escande sizeof(struct port100_tg_rf_setting)); 12437227c021SThierry Escande 12447227c021SThierry Escande resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_RF, skb); 12457227c021SThierry Escande 12467227c021SThierry Escande if (IS_ERR(resp)) 12477227c021SThierry Escande return PTR_ERR(resp); 12487227c021SThierry Escande 12497227c021SThierry Escande rc = resp->data[0]; 12507227c021SThierry Escande 12517227c021SThierry Escande dev_kfree_skb(resp); 12527227c021SThierry Escande 12537227c021SThierry Escande return rc; 12547227c021SThierry Escande } 12557227c021SThierry Escande 12567227c021SThierry Escande static int port100_tg_set_framing(struct nfc_digital_dev *ddev, int param) 12577227c021SThierry Escande { 12587227c021SThierry Escande struct port100 *dev = nfc_digital_get_drvdata(ddev); 12597227c021SThierry Escande struct port100_protocol *protocols; 12607227c021SThierry Escande struct sk_buff *skb; 12617227c021SThierry Escande struct sk_buff *resp; 12627227c021SThierry Escande int rc; 12637227c021SThierry Escande int num_protocols; 12647227c021SThierry Escande size_t size; 12657227c021SThierry Escande 12667227c021SThierry Escande if (param >= NFC_DIGITAL_FRAMING_LAST) 12677227c021SThierry Escande return -EINVAL; 12687227c021SThierry Escande 12697227c021SThierry Escande protocols = tg_protocols[param]; 12707227c021SThierry Escande 12717227c021SThierry Escande num_protocols = 0; 12727227c021SThierry Escande while (protocols[num_protocols].number != PORT100_TG_PROT_END) 12737227c021SThierry Escande num_protocols++; 12747227c021SThierry Escande 12757227c021SThierry Escande if (!num_protocols) 12767227c021SThierry Escande return 0; 12777227c021SThierry Escande 12787227c021SThierry Escande size = sizeof(struct port100_protocol) * num_protocols; 12797227c021SThierry Escande 12807227c021SThierry Escande skb = port100_alloc_skb(dev, size); 12817227c021SThierry Escande if (!skb) 12827227c021SThierry Escande return -ENOMEM; 12837227c021SThierry Escande 12847227c021SThierry Escande memcpy(skb_put(skb, size), protocols, size); 12857227c021SThierry Escande 12867227c021SThierry Escande resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_PROTOCOL, skb); 12877227c021SThierry Escande 12887227c021SThierry Escande if (IS_ERR(resp)) 12897227c021SThierry Escande return PTR_ERR(resp); 12907227c021SThierry Escande 12917227c021SThierry Escande rc = resp->data[0]; 12927227c021SThierry Escande 12937227c021SThierry Escande dev_kfree_skb(resp); 12947227c021SThierry Escande 12957227c021SThierry Escande return rc; 12967227c021SThierry Escande } 12977227c021SThierry Escande 1298562d4d59SThierry Escande static int port100_tg_configure_hw(struct nfc_digital_dev *ddev, int type, 1299562d4d59SThierry Escande int param) 1300562d4d59SThierry Escande { 13017227c021SThierry Escande if (type == NFC_DIGITAL_CONFIG_RF_TECH) 13027227c021SThierry Escande return port100_tg_set_rf(ddev, param); 13037227c021SThierry Escande 13047227c021SThierry Escande if (type == NFC_DIGITAL_CONFIG_FRAMING) 13057227c021SThierry Escande return port100_tg_set_framing(ddev, param); 13067227c021SThierry Escande 13077227c021SThierry Escande return -EINVAL; 13087227c021SThierry Escande } 13097227c021SThierry Escande 13107227c021SThierry Escande static bool port100_tg_target_activated(struct port100 *dev, u8 tgt_activated) 13117227c021SThierry Escande { 13127227c021SThierry Escande u8 mask; 13137227c021SThierry Escande 13147227c021SThierry Escande switch (dev->cmd_type) { 13157227c021SThierry Escande case PORT100_CMD_TYPE_0: 13167227c021SThierry Escande mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK; 13177227c021SThierry Escande break; 13187227c021SThierry Escande case PORT100_CMD_TYPE_1: 13197227c021SThierry Escande mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK | 13207227c021SThierry Escande PORT100_MDAA_TGT_WAS_ACTIVATED_MASK; 13217227c021SThierry Escande break; 13227227c021SThierry Escande default: 13233590ebc0SJoe Perches nfc_err(&dev->interface->dev, "Unknown command type\n"); 13247227c021SThierry Escande return false; 13257227c021SThierry Escande } 13267227c021SThierry Escande 13277227c021SThierry Escande return ((tgt_activated & mask) == mask); 13287227c021SThierry Escande } 13297227c021SThierry Escande 13307227c021SThierry Escande static void port100_tg_comm_rf_complete(struct port100 *dev, void *arg, 13317227c021SThierry Escande struct sk_buff *resp) 13327227c021SThierry Escande { 13337227c021SThierry Escande u32 status; 13347227c021SThierry Escande struct port100_cb_arg *cb_arg = arg; 13357227c021SThierry Escande nfc_digital_cmd_complete_t cb = cb_arg->complete_cb; 13367227c021SThierry Escande struct port100_tg_comm_rf_res *hdr; 13377227c021SThierry Escande 13387227c021SThierry Escande if (IS_ERR(resp)) 13397227c021SThierry Escande goto exit; 13407227c021SThierry Escande 13417227c021SThierry Escande hdr = (struct port100_tg_comm_rf_res *)resp->data; 13427227c021SThierry Escande 13437227c021SThierry Escande status = le32_to_cpu(hdr->status); 13447227c021SThierry Escande 13457227c021SThierry Escande if (cb_arg->mdaa && 13467227c021SThierry Escande !port100_tg_target_activated(dev, hdr->target_activated)) { 13477227c021SThierry Escande kfree_skb(resp); 13487227c021SThierry Escande resp = ERR_PTR(-ETIMEDOUT); 13497227c021SThierry Escande 13507227c021SThierry Escande goto exit; 13517227c021SThierry Escande } 13527227c021SThierry Escande 13537227c021SThierry Escande skb_pull(resp, sizeof(struct port100_tg_comm_rf_res)); 13547227c021SThierry Escande 13557227c021SThierry Escande if (status != PORT100_CMD_STATUS_OK) { 13567227c021SThierry Escande kfree_skb(resp); 13577227c021SThierry Escande 13587227c021SThierry Escande if (status == PORT100_CMD_STATUS_TIMEOUT) 13597227c021SThierry Escande resp = ERR_PTR(-ETIMEDOUT); 13607227c021SThierry Escande else 13617227c021SThierry Escande resp = ERR_PTR(-EIO); 13627227c021SThierry Escande } 13637227c021SThierry Escande 13647227c021SThierry Escande exit: 13657227c021SThierry Escande cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp); 13667227c021SThierry Escande 13677227c021SThierry Escande kfree(cb_arg); 1368562d4d59SThierry Escande } 1369562d4d59SThierry Escande 1370562d4d59SThierry Escande static int port100_tg_send_cmd(struct nfc_digital_dev *ddev, 1371562d4d59SThierry Escande struct sk_buff *skb, u16 timeout, 1372562d4d59SThierry Escande nfc_digital_cmd_complete_t cb, void *arg) 1373562d4d59SThierry Escande { 13747227c021SThierry Escande struct port100 *dev = nfc_digital_get_drvdata(ddev); 13757227c021SThierry Escande struct port100_tg_comm_rf_cmd *hdr; 13767227c021SThierry Escande struct port100_cb_arg *cb_arg; 13777227c021SThierry Escande 13787227c021SThierry Escande cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL); 13797227c021SThierry Escande if (!cb_arg) 13807227c021SThierry Escande return -ENOMEM; 13817227c021SThierry Escande 13827227c021SThierry Escande cb_arg->complete_cb = cb; 13837227c021SThierry Escande cb_arg->complete_arg = arg; 13847227c021SThierry Escande 13857227c021SThierry Escande skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd)); 13867227c021SThierry Escande 13877227c021SThierry Escande hdr = (struct port100_tg_comm_rf_cmd *)skb->data; 13887227c021SThierry Escande 13897227c021SThierry Escande memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd)); 13907227c021SThierry Escande hdr->guard_time = cpu_to_le16(500); 13917227c021SThierry Escande hdr->send_timeout = cpu_to_le16(0xFFFF); 13927227c021SThierry Escande hdr->recv_timeout = cpu_to_le16(timeout); 13937227c021SThierry Escande 13947227c021SThierry Escande return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb, 13957227c021SThierry Escande port100_tg_comm_rf_complete, cb_arg); 1396562d4d59SThierry Escande } 1397562d4d59SThierry Escande 1398562d4d59SThierry Escande static int port100_listen_mdaa(struct nfc_digital_dev *ddev, 1399562d4d59SThierry Escande struct digital_tg_mdaa_params *params, 1400562d4d59SThierry Escande u16 timeout, 1401562d4d59SThierry Escande nfc_digital_cmd_complete_t cb, void *arg) 1402562d4d59SThierry Escande { 14037227c021SThierry Escande struct port100 *dev = nfc_digital_get_drvdata(ddev); 14047227c021SThierry Escande struct port100_tg_comm_rf_cmd *hdr; 14057227c021SThierry Escande struct port100_cb_arg *cb_arg; 14067227c021SThierry Escande struct sk_buff *skb; 14077227c021SThierry Escande int rc; 14087227c021SThierry Escande 14097227c021SThierry Escande rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, 14107227c021SThierry Escande NFC_DIGITAL_RF_TECH_106A); 14117227c021SThierry Escande if (rc) 14127227c021SThierry Escande return rc; 14137227c021SThierry Escande 14147227c021SThierry Escande rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, 14157227c021SThierry Escande NFC_DIGITAL_FRAMING_NFCA_NFC_DEP); 14167227c021SThierry Escande if (rc) 14177227c021SThierry Escande return rc; 14187227c021SThierry Escande 14197227c021SThierry Escande cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL); 14207227c021SThierry Escande if (!cb_arg) 14217227c021SThierry Escande return -ENOMEM; 14227227c021SThierry Escande 14237227c021SThierry Escande cb_arg->complete_cb = cb; 14247227c021SThierry Escande cb_arg->complete_arg = arg; 14257227c021SThierry Escande cb_arg->mdaa = 1; 14267227c021SThierry Escande 14277227c021SThierry Escande skb = port100_alloc_skb(dev, 0); 14287227c021SThierry Escande if (!skb) { 14297227c021SThierry Escande kfree(cb_arg); 14307227c021SThierry Escande return -ENOMEM; 14317227c021SThierry Escande } 14327227c021SThierry Escande 14337227c021SThierry Escande skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd)); 14347227c021SThierry Escande hdr = (struct port100_tg_comm_rf_cmd *)skb->data; 14357227c021SThierry Escande 14367227c021SThierry Escande memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd)); 14377227c021SThierry Escande 14387227c021SThierry Escande hdr->guard_time = 0; 14397227c021SThierry Escande hdr->send_timeout = cpu_to_le16(0xFFFF); 14407227c021SThierry Escande hdr->mdaa = 1; 14417227c021SThierry Escande hdr->nfca_param[0] = (params->sens_res >> 8) & 0xFF; 14427227c021SThierry Escande hdr->nfca_param[1] = params->sens_res & 0xFF; 14437227c021SThierry Escande memcpy(hdr->nfca_param + 2, params->nfcid1, 3); 14447227c021SThierry Escande hdr->nfca_param[5] = params->sel_res; 14457227c021SThierry Escande memcpy(hdr->nfcf_param, params->nfcid2, 8); 14467227c021SThierry Escande hdr->nfcf_param[16] = (params->sc >> 8) & 0xFF; 14477227c021SThierry Escande hdr->nfcf_param[17] = params->sc & 0xFF; 14487227c021SThierry Escande hdr->recv_timeout = cpu_to_le16(timeout); 14497227c021SThierry Escande 14507227c021SThierry Escande return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb, 14517227c021SThierry Escande port100_tg_comm_rf_complete, cb_arg); 1452562d4d59SThierry Escande } 1453562d4d59SThierry Escande 1454562d4d59SThierry Escande static int port100_listen(struct nfc_digital_dev *ddev, u16 timeout, 1455562d4d59SThierry Escande nfc_digital_cmd_complete_t cb, void *arg) 1456562d4d59SThierry Escande { 14577227c021SThierry Escande struct port100 *dev = nfc_digital_get_drvdata(ddev); 14587227c021SThierry Escande struct sk_buff *skb; 14597227c021SThierry Escande 14607227c021SThierry Escande skb = port100_alloc_skb(dev, 0); 14617227c021SThierry Escande if (!skb) 14627227c021SThierry Escande return -ENOMEM; 14637227c021SThierry Escande 14647227c021SThierry Escande return port100_tg_send_cmd(ddev, skb, timeout, cb, arg); 1465562d4d59SThierry Escande } 1466562d4d59SThierry Escande 1467562d4d59SThierry Escande static struct nfc_digital_ops port100_digital_ops = { 1468562d4d59SThierry Escande .in_configure_hw = port100_in_configure_hw, 1469562d4d59SThierry Escande .in_send_cmd = port100_in_send_cmd, 1470562d4d59SThierry Escande 1471562d4d59SThierry Escande .tg_listen_mdaa = port100_listen_mdaa, 1472562d4d59SThierry Escande .tg_listen = port100_listen, 1473562d4d59SThierry Escande .tg_configure_hw = port100_tg_configure_hw, 1474562d4d59SThierry Escande .tg_send_cmd = port100_tg_send_cmd, 1475562d4d59SThierry Escande 1476562d4d59SThierry Escande .switch_rf = port100_switch_rf, 1477562d4d59SThierry Escande .abort_cmd = port100_abort_cmd, 1478562d4d59SThierry Escande }; 1479562d4d59SThierry Escande 1480562d4d59SThierry Escande static const struct usb_device_id port100_table[] = { 14819728ee92SOGAWA Hirofumi { USB_DEVICE(SONY_VENDOR_ID, RCS380S_PRODUCT_ID), }, 14829728ee92SOGAWA Hirofumi { USB_DEVICE(SONY_VENDOR_ID, RCS380P_PRODUCT_ID), }, 1483562d4d59SThierry Escande { } 1484562d4d59SThierry Escande }; 1485562d4d59SThierry Escande MODULE_DEVICE_TABLE(usb, port100_table); 1486562d4d59SThierry Escande 1487562d4d59SThierry Escande static int port100_probe(struct usb_interface *interface, 1488562d4d59SThierry Escande const struct usb_device_id *id) 1489562d4d59SThierry Escande { 1490562d4d59SThierry Escande struct port100 *dev; 1491562d4d59SThierry Escande int rc; 14920347a6abSThierry Escande struct usb_host_interface *iface_desc; 14930347a6abSThierry Escande struct usb_endpoint_descriptor *endpoint; 14940347a6abSThierry Escande int in_endpoint; 14950347a6abSThierry Escande int out_endpoint; 14960347a6abSThierry Escande u16 fw_version; 14970347a6abSThierry Escande u64 cmd_type_mask; 14980347a6abSThierry Escande int i; 1499562d4d59SThierry Escande 1500562d4d59SThierry Escande dev = devm_kzalloc(&interface->dev, sizeof(struct port100), GFP_KERNEL); 1501562d4d59SThierry Escande if (!dev) 1502562d4d59SThierry Escande return -ENOMEM; 1503562d4d59SThierry Escande 1504b74584c1SThierry Escande mutex_init(&dev->out_urb_lock); 1505562d4d59SThierry Escande dev->udev = usb_get_dev(interface_to_usbdev(interface)); 1506562d4d59SThierry Escande dev->interface = interface; 1507562d4d59SThierry Escande usb_set_intfdata(interface, dev); 1508562d4d59SThierry Escande 15090347a6abSThierry Escande in_endpoint = out_endpoint = 0; 15100347a6abSThierry Escande iface_desc = interface->cur_altsetting; 15110347a6abSThierry Escande for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 15120347a6abSThierry Escande endpoint = &iface_desc->endpoint[i].desc; 15130347a6abSThierry Escande 15140347a6abSThierry Escande if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) 15150347a6abSThierry Escande in_endpoint = endpoint->bEndpointAddress; 15160347a6abSThierry Escande 15170347a6abSThierry Escande if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) 15180347a6abSThierry Escande out_endpoint = endpoint->bEndpointAddress; 15190347a6abSThierry Escande } 15200347a6abSThierry Escande 15210347a6abSThierry Escande if (!in_endpoint || !out_endpoint) { 15220347a6abSThierry Escande nfc_err(&interface->dev, 15230347a6abSThierry Escande "Could not find bulk-in or bulk-out endpoint\n"); 15240347a6abSThierry Escande rc = -ENODEV; 15250347a6abSThierry Escande goto error; 15260347a6abSThierry Escande } 15270347a6abSThierry Escande 15280347a6abSThierry Escande dev->in_urb = usb_alloc_urb(0, GFP_KERNEL); 15290347a6abSThierry Escande dev->out_urb = usb_alloc_urb(0, GFP_KERNEL); 15300347a6abSThierry Escande 15310347a6abSThierry Escande if (!dev->in_urb || !dev->out_urb) { 15320347a6abSThierry Escande nfc_err(&interface->dev, "Could not allocate USB URBs\n"); 15330347a6abSThierry Escande rc = -ENOMEM; 15340347a6abSThierry Escande goto error; 15350347a6abSThierry Escande } 15360347a6abSThierry Escande 15370347a6abSThierry Escande usb_fill_bulk_urb(dev->in_urb, dev->udev, 15380347a6abSThierry Escande usb_rcvbulkpipe(dev->udev, in_endpoint), 15390347a6abSThierry Escande NULL, 0, NULL, dev); 15400347a6abSThierry Escande usb_fill_bulk_urb(dev->out_urb, dev->udev, 15410347a6abSThierry Escande usb_sndbulkpipe(dev->udev, out_endpoint), 15420347a6abSThierry Escande NULL, 0, port100_send_complete, dev); 1543*0ada0768SOGAWA Hirofumi dev->out_urb->transfer_flags = URB_ZERO_PACKET; 15440347a6abSThierry Escande 15450347a6abSThierry Escande dev->skb_headroom = PORT100_FRAME_HEADER_LEN + 15460347a6abSThierry Escande PORT100_COMM_RF_HEAD_MAX_LEN; 15470347a6abSThierry Escande dev->skb_tailroom = PORT100_FRAME_TAIL_LEN; 15480347a6abSThierry Escande 15490347a6abSThierry Escande INIT_WORK(&dev->cmd_complete_work, port100_wq_cmd_complete); 15500347a6abSThierry Escande 15510347a6abSThierry Escande /* The first thing to do with the Port-100 is to set the command type 15520347a6abSThierry Escande * to be used. If supported we use command type 1. 0 otherwise. 15530347a6abSThierry Escande */ 15540347a6abSThierry Escande cmd_type_mask = port100_get_command_type_mask(dev); 15550347a6abSThierry Escande if (!cmd_type_mask) { 15560347a6abSThierry Escande nfc_err(&interface->dev, 15573590ebc0SJoe Perches "Could not get supported command types\n"); 15580347a6abSThierry Escande rc = -ENODEV; 15590347a6abSThierry Escande goto error; 15600347a6abSThierry Escande } 15610347a6abSThierry Escande 15620347a6abSThierry Escande if (PORT100_CMD_TYPE_IS_SUPPORTED(cmd_type_mask, PORT100_CMD_TYPE_1)) 15630347a6abSThierry Escande dev->cmd_type = PORT100_CMD_TYPE_1; 15640347a6abSThierry Escande else 15650347a6abSThierry Escande dev->cmd_type = PORT100_CMD_TYPE_0; 15660347a6abSThierry Escande 15670347a6abSThierry Escande rc = port100_set_command_type(dev, dev->cmd_type); 15680347a6abSThierry Escande if (rc) { 15690347a6abSThierry Escande nfc_err(&interface->dev, 15703590ebc0SJoe Perches "The device does not support command type %u\n", 15710347a6abSThierry Escande dev->cmd_type); 15720347a6abSThierry Escande goto error; 15730347a6abSThierry Escande } 15740347a6abSThierry Escande 15750347a6abSThierry Escande fw_version = port100_get_firmware_version(dev); 15760347a6abSThierry Escande if (!fw_version) 15770347a6abSThierry Escande nfc_err(&interface->dev, 15783590ebc0SJoe Perches "Could not get device firmware version\n"); 15790347a6abSThierry Escande 15800347a6abSThierry Escande nfc_info(&interface->dev, 15810347a6abSThierry Escande "Sony NFC Port-100 Series attached (firmware v%x.%02x)\n", 15820347a6abSThierry Escande (fw_version & 0xFF00) >> 8, fw_version & 0xFF); 1583562d4d59SThierry Escande 1584562d4d59SThierry Escande dev->nfc_digital_dev = nfc_digital_allocate_device(&port100_digital_ops, 1585562d4d59SThierry Escande PORT100_PROTOCOLS, 1586562d4d59SThierry Escande PORT100_CAPABILITIES, 1587562d4d59SThierry Escande dev->skb_headroom, 1588562d4d59SThierry Escande dev->skb_tailroom); 1589562d4d59SThierry Escande if (!dev->nfc_digital_dev) { 1590562d4d59SThierry Escande nfc_err(&interface->dev, 15913590ebc0SJoe Perches "Could not allocate nfc_digital_dev\n"); 1592562d4d59SThierry Escande rc = -ENOMEM; 1593562d4d59SThierry Escande goto error; 1594562d4d59SThierry Escande } 1595562d4d59SThierry Escande 1596562d4d59SThierry Escande nfc_digital_set_parent_dev(dev->nfc_digital_dev, &interface->dev); 1597562d4d59SThierry Escande nfc_digital_set_drvdata(dev->nfc_digital_dev, dev); 1598562d4d59SThierry Escande 1599562d4d59SThierry Escande rc = nfc_digital_register_device(dev->nfc_digital_dev); 1600562d4d59SThierry Escande if (rc) { 1601562d4d59SThierry Escande nfc_err(&interface->dev, 16023590ebc0SJoe Perches "Could not register digital device\n"); 1603562d4d59SThierry Escande goto free_nfc_dev; 1604562d4d59SThierry Escande } 1605562d4d59SThierry Escande 1606562d4d59SThierry Escande return 0; 1607562d4d59SThierry Escande 1608562d4d59SThierry Escande free_nfc_dev: 1609562d4d59SThierry Escande nfc_digital_free_device(dev->nfc_digital_dev); 1610562d4d59SThierry Escande 1611562d4d59SThierry Escande error: 16120347a6abSThierry Escande usb_free_urb(dev->in_urb); 16130347a6abSThierry Escande usb_free_urb(dev->out_urb); 16140347a6abSThierry Escande usb_put_dev(dev->udev); 16150347a6abSThierry Escande 1616562d4d59SThierry Escande return rc; 1617562d4d59SThierry Escande } 1618562d4d59SThierry Escande 1619562d4d59SThierry Escande static void port100_disconnect(struct usb_interface *interface) 1620562d4d59SThierry Escande { 1621562d4d59SThierry Escande struct port100 *dev; 1622562d4d59SThierry Escande 1623562d4d59SThierry Escande dev = usb_get_intfdata(interface); 1624562d4d59SThierry Escande usb_set_intfdata(interface, NULL); 1625562d4d59SThierry Escande 1626562d4d59SThierry Escande nfc_digital_unregister_device(dev->nfc_digital_dev); 1627562d4d59SThierry Escande nfc_digital_free_device(dev->nfc_digital_dev); 1628562d4d59SThierry Escande 16290347a6abSThierry Escande usb_kill_urb(dev->in_urb); 16300347a6abSThierry Escande usb_kill_urb(dev->out_urb); 16310347a6abSThierry Escande 16320347a6abSThierry Escande usb_free_urb(dev->in_urb); 16330347a6abSThierry Escande usb_free_urb(dev->out_urb); 1634c36aeba8SAlexey Khoroshilov usb_put_dev(dev->udev); 16350347a6abSThierry Escande 16360347a6abSThierry Escande kfree(dev->cmd); 16370347a6abSThierry Escande 16383590ebc0SJoe Perches nfc_info(&interface->dev, "Sony Port-100 NFC device disconnected\n"); 1639562d4d59SThierry Escande } 1640562d4d59SThierry Escande 1641562d4d59SThierry Escande static struct usb_driver port100_driver = { 1642562d4d59SThierry Escande .name = "port100", 1643562d4d59SThierry Escande .probe = port100_probe, 1644562d4d59SThierry Escande .disconnect = port100_disconnect, 1645562d4d59SThierry Escande .id_table = port100_table, 1646562d4d59SThierry Escande }; 1647562d4d59SThierry Escande 1648562d4d59SThierry Escande module_usb_driver(port100_driver); 1649562d4d59SThierry Escande 1650562d4d59SThierry Escande MODULE_DESCRIPTION("NFC Port-100 series usb driver ver " VERSION); 1651562d4d59SThierry Escande MODULE_VERSION(VERSION); 1652562d4d59SThierry Escande MODULE_LICENSE("GPL"); 1653