Thierry Escande | 562d4d5 | 2013-10-04 12:12:00 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Sony NFC Port-100 Series driver |
| 3 | * Copyright (c) 2013, Intel Corporation. |
| 4 | * |
| 5 | * Partly based/Inspired by Stephen Tiedemann's nfcpy |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms and conditions of the GNU General Public License, |
| 9 | * version 2, as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | * more details. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/usb.h> |
| 20 | #include <net/nfc/digital.h> |
| 21 | |
| 22 | #define VERSION "0.1" |
| 23 | |
| 24 | #define SONY_VENDOR_ID 0x054c |
| 25 | #define RCS380_PRODUCT_ID 0x06c1 |
| 26 | |
| 27 | #define PORT100_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \ |
| 28 | NFC_PROTO_MIFARE_MASK | \ |
| 29 | NFC_PROTO_FELICA_MASK | \ |
| 30 | NFC_PROTO_NFC_DEP_MASK) |
| 31 | |
| 32 | #define PORT100_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC | \ |
| 33 | NFC_DIGITAL_DRV_CAPS_TG_CRC) |
| 34 | |
Thierry Escande | 0347a6a | 2013-10-04 12:12:01 +0200 | [diff] [blame] | 35 | /* Standard port100 frame definitions */ |
| 36 | #define PORT100_FRAME_HEADER_LEN (sizeof(struct port100_frame) \ |
| 37 | + 2) /* data[0] CC, data[1] SCC */ |
| 38 | #define PORT100_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/ |
| 39 | |
| 40 | #define PORT100_COMM_RF_HEAD_MAX_LEN (sizeof(struct port100_tg_comm_rf_cmd)) |
| 41 | |
| 42 | /* |
| 43 | * Max extended frame payload len, excluding CC and SCC |
| 44 | * which are already in PORT100_FRAME_HEADER_LEN. |
| 45 | */ |
| 46 | #define PORT100_FRAME_MAX_PAYLOAD_LEN 1001 |
| 47 | |
| 48 | #define PORT100_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2), |
| 49 | Postamble (1) */ |
| 50 | static u8 ack_frame[PORT100_FRAME_ACK_SIZE] = { |
| 51 | 0x00, 0x00, 0xff, 0x00, 0xff, 0x00 |
| 52 | }; |
| 53 | |
| 54 | #define PORT100_FRAME_CHECKSUM(f) (f->data[le16_to_cpu(f->datalen)]) |
| 55 | #define PORT100_FRAME_POSTAMBLE(f) (f->data[le16_to_cpu(f->datalen) + 1]) |
| 56 | |
| 57 | /* start of frame */ |
| 58 | #define PORT100_FRAME_SOF 0x00FF |
| 59 | #define PORT100_FRAME_EXT 0xFFFF |
| 60 | #define PORT100_FRAME_ACK 0x00FF |
| 61 | |
| 62 | /* Port-100 command: in or out */ |
| 63 | #define PORT100_FRAME_DIRECTION(f) (f->data[0]) /* CC */ |
| 64 | #define PORT100_FRAME_DIR_OUT 0xD6 |
| 65 | #define PORT100_FRAME_DIR_IN 0xD7 |
| 66 | |
| 67 | /* Port-100 sub-command */ |
| 68 | #define PORT100_FRAME_CMD(f) (f->data[1]) /* SCC */ |
| 69 | |
| 70 | #define PORT100_CMD_GET_FIRMWARE_VERSION 0x20 |
| 71 | #define PORT100_CMD_GET_COMMAND_TYPE 0x28 |
| 72 | #define PORT100_CMD_SET_COMMAND_TYPE 0x2A |
| 73 | |
Thierry Escande | 9f7b57f | 2013-10-04 12:12:02 +0200 | [diff] [blame] | 74 | #define PORT100_CMD_IN_SET_RF 0x00 |
| 75 | #define PORT100_CMD_IN_SET_PROTOCOL 0x02 |
| 76 | #define PORT100_CMD_IN_COMM_RF 0x04 |
| 77 | |
Thierry Escande | 7227c02 | 2013-10-04 12:12:03 +0200 | [diff] [blame] | 78 | #define PORT100_CMD_TG_SET_RF 0x40 |
| 79 | #define PORT100_CMD_TG_SET_PROTOCOL 0x42 |
| 80 | #define PORT100_CMD_TG_SET_RF_OFF 0x46 |
| 81 | #define PORT100_CMD_TG_COMM_RF 0x48 |
| 82 | |
Thierry Escande | 9f7b57f | 2013-10-04 12:12:02 +0200 | [diff] [blame] | 83 | #define PORT100_CMD_SWITCH_RF 0x06 |
| 84 | |
Thierry Escande | 0347a6a | 2013-10-04 12:12:01 +0200 | [diff] [blame] | 85 | #define PORT100_CMD_RESPONSE(cmd) (cmd + 1) |
| 86 | |
| 87 | #define PORT100_CMD_TYPE_IS_SUPPORTED(mask, cmd_type) \ |
| 88 | ((mask) & (0x01 << (cmd_type))) |
| 89 | #define PORT100_CMD_TYPE_0 0 |
| 90 | #define PORT100_CMD_TYPE_1 1 |
| 91 | |
Thierry Escande | 9f7b57f | 2013-10-04 12:12:02 +0200 | [diff] [blame] | 92 | #define PORT100_CMD_STATUS_OK 0x00 |
| 93 | #define PORT100_CMD_STATUS_TIMEOUT 0x80 |
| 94 | |
Thierry Escande | 7227c02 | 2013-10-04 12:12:03 +0200 | [diff] [blame] | 95 | #define PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK 0x01 |
| 96 | #define PORT100_MDAA_TGT_WAS_ACTIVATED_MASK 0x02 |
| 97 | |
Thierry Escande | 0347a6a | 2013-10-04 12:12:01 +0200 | [diff] [blame] | 98 | struct port100; |
| 99 | |
| 100 | typedef void (*port100_send_async_complete_t)(struct port100 *dev, void *arg, |
| 101 | struct sk_buff *resp); |
| 102 | |
Thierry Escande | 9f7b57f | 2013-10-04 12:12:02 +0200 | [diff] [blame] | 103 | /** |
| 104 | * Setting sets structure for in_set_rf command |
| 105 | * |
| 106 | * @in_*_set_number: Represent the entry indexes in the port-100 RF Base Table. |
| 107 | * This table contains multiple RF setting sets required for RF |
| 108 | * communication. |
| 109 | * |
| 110 | * @in_*_comm_type: Theses fields set the communication type to be used. |
| 111 | */ |
| 112 | struct port100_in_rf_setting { |
| 113 | u8 in_send_set_number; |
| 114 | u8 in_send_comm_type; |
| 115 | u8 in_recv_set_number; |
| 116 | u8 in_recv_comm_type; |
| 117 | } __packed; |
| 118 | |
| 119 | #define PORT100_COMM_TYPE_IN_212F 0x01 |
| 120 | #define PORT100_COMM_TYPE_IN_424F 0x02 |
| 121 | #define PORT100_COMM_TYPE_IN_106A 0x03 |
| 122 | |
| 123 | static const struct port100_in_rf_setting in_rf_settings[] = { |
| 124 | [NFC_DIGITAL_RF_TECH_212F] = { |
| 125 | .in_send_set_number = 1, |
| 126 | .in_send_comm_type = PORT100_COMM_TYPE_IN_212F, |
| 127 | .in_recv_set_number = 15, |
| 128 | .in_recv_comm_type = PORT100_COMM_TYPE_IN_212F, |
| 129 | }, |
| 130 | [NFC_DIGITAL_RF_TECH_424F] = { |
| 131 | .in_send_set_number = 1, |
| 132 | .in_send_comm_type = PORT100_COMM_TYPE_IN_424F, |
| 133 | .in_recv_set_number = 15, |
| 134 | .in_recv_comm_type = PORT100_COMM_TYPE_IN_424F, |
| 135 | }, |
| 136 | [NFC_DIGITAL_RF_TECH_106A] = { |
| 137 | .in_send_set_number = 2, |
| 138 | .in_send_comm_type = PORT100_COMM_TYPE_IN_106A, |
| 139 | .in_recv_set_number = 15, |
| 140 | .in_recv_comm_type = PORT100_COMM_TYPE_IN_106A, |
| 141 | }, |
Thierry Escande | d3815ea | 2014-01-27 00:31:14 +0100 | [diff] [blame^] | 142 | /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */ |
| 143 | [NFC_DIGITAL_RF_TECH_LAST] = { 0 }, |
Thierry Escande | 9f7b57f | 2013-10-04 12:12:02 +0200 | [diff] [blame] | 144 | }; |
| 145 | |
Thierry Escande | 7227c02 | 2013-10-04 12:12:03 +0200 | [diff] [blame] | 146 | /** |
| 147 | * Setting sets structure for tg_set_rf command |
| 148 | * |
| 149 | * @tg_set_number: Represents the entry index in the port-100 RF Base Table. |
| 150 | * This table contains multiple RF setting sets required for RF |
| 151 | * communication. this field is used for both send and receive |
| 152 | * settings. |
| 153 | * |
| 154 | * @tg_comm_type: Sets the communication type to be used to send and receive |
| 155 | * data. |
| 156 | */ |
| 157 | struct port100_tg_rf_setting { |
| 158 | u8 tg_set_number; |
| 159 | u8 tg_comm_type; |
| 160 | } __packed; |
| 161 | |
| 162 | #define PORT100_COMM_TYPE_TG_106A 0x0B |
| 163 | #define PORT100_COMM_TYPE_TG_212F 0x0C |
| 164 | #define PORT100_COMM_TYPE_TG_424F 0x0D |
| 165 | |
| 166 | static const struct port100_tg_rf_setting tg_rf_settings[] = { |
| 167 | [NFC_DIGITAL_RF_TECH_106A] = { |
| 168 | .tg_set_number = 8, |
| 169 | .tg_comm_type = PORT100_COMM_TYPE_TG_106A, |
| 170 | }, |
| 171 | [NFC_DIGITAL_RF_TECH_212F] = { |
| 172 | .tg_set_number = 8, |
| 173 | .tg_comm_type = PORT100_COMM_TYPE_TG_212F, |
| 174 | }, |
| 175 | [NFC_DIGITAL_RF_TECH_424F] = { |
| 176 | .tg_set_number = 8, |
| 177 | .tg_comm_type = PORT100_COMM_TYPE_TG_424F, |
| 178 | }, |
Thierry Escande | d3815ea | 2014-01-27 00:31:14 +0100 | [diff] [blame^] | 179 | /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */ |
| 180 | [NFC_DIGITAL_RF_TECH_LAST] = { 0 }, |
| 181 | |
Thierry Escande | 7227c02 | 2013-10-04 12:12:03 +0200 | [diff] [blame] | 182 | }; |
| 183 | |
Thierry Escande | 9f7b57f | 2013-10-04 12:12:02 +0200 | [diff] [blame] | 184 | #define PORT100_IN_PROT_INITIAL_GUARD_TIME 0x00 |
| 185 | #define PORT100_IN_PROT_ADD_CRC 0x01 |
| 186 | #define PORT100_IN_PROT_CHECK_CRC 0x02 |
| 187 | #define PORT100_IN_PROT_MULTI_CARD 0x03 |
| 188 | #define PORT100_IN_PROT_ADD_PARITY 0x04 |
| 189 | #define PORT100_IN_PROT_CHECK_PARITY 0x05 |
| 190 | #define PORT100_IN_PROT_BITWISE_AC_RECV_MODE 0x06 |
| 191 | #define PORT100_IN_PROT_VALID_BIT_NUMBER 0x07 |
| 192 | #define PORT100_IN_PROT_CRYPTO1 0x08 |
| 193 | #define PORT100_IN_PROT_ADD_SOF 0x09 |
| 194 | #define PORT100_IN_PROT_CHECK_SOF 0x0A |
| 195 | #define PORT100_IN_PROT_ADD_EOF 0x0B |
| 196 | #define PORT100_IN_PROT_CHECK_EOF 0x0C |
| 197 | #define PORT100_IN_PROT_DEAF_TIME 0x0E |
| 198 | #define PORT100_IN_PROT_CRM 0x0F |
| 199 | #define PORT100_IN_PROT_CRM_MIN_LEN 0x10 |
| 200 | #define PORT100_IN_PROT_T1_TAG_FRAME 0x11 |
| 201 | #define PORT100_IN_PROT_RFCA 0x12 |
| 202 | #define PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR 0x13 |
| 203 | #define PORT100_IN_PROT_END 0x14 |
| 204 | |
| 205 | #define PORT100_IN_MAX_NUM_PROTOCOLS 19 |
| 206 | |
Thierry Escande | 7227c02 | 2013-10-04 12:12:03 +0200 | [diff] [blame] | 207 | #define PORT100_TG_PROT_TU 0x00 |
| 208 | #define PORT100_TG_PROT_RF_OFF 0x01 |
| 209 | #define PORT100_TG_PROT_CRM 0x02 |
| 210 | #define PORT100_TG_PROT_END 0x03 |
| 211 | |
| 212 | #define PORT100_TG_MAX_NUM_PROTOCOLS 3 |
| 213 | |
Thierry Escande | 9f7b57f | 2013-10-04 12:12:02 +0200 | [diff] [blame] | 214 | struct port100_protocol { |
| 215 | u8 number; |
| 216 | u8 value; |
| 217 | } __packed; |
| 218 | |
| 219 | static struct port100_protocol |
| 220 | in_protocols[][PORT100_IN_MAX_NUM_PROTOCOLS + 1] = { |
| 221 | [NFC_DIGITAL_FRAMING_NFCA_SHORT] = { |
| 222 | { PORT100_IN_PROT_INITIAL_GUARD_TIME, 6 }, |
| 223 | { PORT100_IN_PROT_ADD_CRC, 0 }, |
| 224 | { PORT100_IN_PROT_CHECK_CRC, 0 }, |
| 225 | { PORT100_IN_PROT_MULTI_CARD, 0 }, |
| 226 | { PORT100_IN_PROT_ADD_PARITY, 0 }, |
| 227 | { PORT100_IN_PROT_CHECK_PARITY, 1 }, |
| 228 | { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 }, |
| 229 | { PORT100_IN_PROT_VALID_BIT_NUMBER, 7 }, |
| 230 | { PORT100_IN_PROT_CRYPTO1, 0 }, |
| 231 | { PORT100_IN_PROT_ADD_SOF, 0 }, |
| 232 | { PORT100_IN_PROT_CHECK_SOF, 0 }, |
| 233 | { PORT100_IN_PROT_ADD_EOF, 0 }, |
| 234 | { PORT100_IN_PROT_CHECK_EOF, 0 }, |
| 235 | { PORT100_IN_PROT_DEAF_TIME, 4 }, |
| 236 | { PORT100_IN_PROT_CRM, 0 }, |
| 237 | { PORT100_IN_PROT_CRM_MIN_LEN, 0 }, |
| 238 | { PORT100_IN_PROT_T1_TAG_FRAME, 0 }, |
| 239 | { PORT100_IN_PROT_RFCA, 0 }, |
| 240 | { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 }, |
| 241 | { PORT100_IN_PROT_END, 0 }, |
| 242 | }, |
| 243 | [NFC_DIGITAL_FRAMING_NFCA_STANDARD] = { |
| 244 | { PORT100_IN_PROT_INITIAL_GUARD_TIME, 6 }, |
| 245 | { PORT100_IN_PROT_ADD_CRC, 0 }, |
| 246 | { PORT100_IN_PROT_CHECK_CRC, 0 }, |
| 247 | { PORT100_IN_PROT_MULTI_CARD, 0 }, |
| 248 | { PORT100_IN_PROT_ADD_PARITY, 1 }, |
| 249 | { PORT100_IN_PROT_CHECK_PARITY, 1 }, |
| 250 | { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 }, |
| 251 | { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 }, |
| 252 | { PORT100_IN_PROT_CRYPTO1, 0 }, |
| 253 | { PORT100_IN_PROT_ADD_SOF, 0 }, |
| 254 | { PORT100_IN_PROT_CHECK_SOF, 0 }, |
| 255 | { PORT100_IN_PROT_ADD_EOF, 0 }, |
| 256 | { PORT100_IN_PROT_CHECK_EOF, 0 }, |
| 257 | { PORT100_IN_PROT_DEAF_TIME, 4 }, |
| 258 | { PORT100_IN_PROT_CRM, 0 }, |
| 259 | { PORT100_IN_PROT_CRM_MIN_LEN, 0 }, |
| 260 | { PORT100_IN_PROT_T1_TAG_FRAME, 0 }, |
| 261 | { PORT100_IN_PROT_RFCA, 0 }, |
| 262 | { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 }, |
| 263 | { PORT100_IN_PROT_END, 0 }, |
| 264 | }, |
| 265 | [NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A] = { |
| 266 | { PORT100_IN_PROT_INITIAL_GUARD_TIME, 6 }, |
| 267 | { PORT100_IN_PROT_ADD_CRC, 1 }, |
| 268 | { PORT100_IN_PROT_CHECK_CRC, 1 }, |
| 269 | { PORT100_IN_PROT_MULTI_CARD, 0 }, |
| 270 | { PORT100_IN_PROT_ADD_PARITY, 1 }, |
| 271 | { PORT100_IN_PROT_CHECK_PARITY, 1 }, |
| 272 | { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 }, |
| 273 | { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 }, |
| 274 | { PORT100_IN_PROT_CRYPTO1, 0 }, |
| 275 | { PORT100_IN_PROT_ADD_SOF, 0 }, |
| 276 | { PORT100_IN_PROT_CHECK_SOF, 0 }, |
| 277 | { PORT100_IN_PROT_ADD_EOF, 0 }, |
| 278 | { PORT100_IN_PROT_CHECK_EOF, 0 }, |
| 279 | { PORT100_IN_PROT_DEAF_TIME, 4 }, |
| 280 | { PORT100_IN_PROT_CRM, 0 }, |
| 281 | { PORT100_IN_PROT_CRM_MIN_LEN, 0 }, |
| 282 | { PORT100_IN_PROT_T1_TAG_FRAME, 0 }, |
| 283 | { PORT100_IN_PROT_RFCA, 0 }, |
| 284 | { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 }, |
| 285 | { PORT100_IN_PROT_END, 0 }, |
| 286 | }, |
| 287 | [NFC_DIGITAL_FRAMING_NFCA_T1T] = { |
| 288 | /* nfc_digital_framing_nfca_short */ |
| 289 | { PORT100_IN_PROT_ADD_CRC, 2 }, |
| 290 | { PORT100_IN_PROT_CHECK_CRC, 2 }, |
| 291 | { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 }, |
| 292 | { PORT100_IN_PROT_T1_TAG_FRAME, 2 }, |
| 293 | { PORT100_IN_PROT_END, 0 }, |
| 294 | }, |
| 295 | [NFC_DIGITAL_FRAMING_NFCA_T2T] = { |
| 296 | /* nfc_digital_framing_nfca_standard */ |
| 297 | { PORT100_IN_PROT_ADD_CRC, 1 }, |
| 298 | { PORT100_IN_PROT_CHECK_CRC, 0 }, |
| 299 | { PORT100_IN_PROT_END, 0 }, |
| 300 | }, |
| 301 | [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP] = { |
| 302 | /* nfc_digital_framing_nfca_standard */ |
| 303 | { PORT100_IN_PROT_END, 0 }, |
| 304 | }, |
| 305 | [NFC_DIGITAL_FRAMING_NFCF] = { |
| 306 | { PORT100_IN_PROT_INITIAL_GUARD_TIME, 18 }, |
| 307 | { PORT100_IN_PROT_ADD_CRC, 1 }, |
| 308 | { PORT100_IN_PROT_CHECK_CRC, 1 }, |
| 309 | { PORT100_IN_PROT_MULTI_CARD, 0 }, |
| 310 | { PORT100_IN_PROT_ADD_PARITY, 0 }, |
| 311 | { PORT100_IN_PROT_CHECK_PARITY, 0 }, |
| 312 | { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 }, |
| 313 | { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 }, |
| 314 | { PORT100_IN_PROT_CRYPTO1, 0 }, |
| 315 | { PORT100_IN_PROT_ADD_SOF, 0 }, |
| 316 | { PORT100_IN_PROT_CHECK_SOF, 0 }, |
| 317 | { PORT100_IN_PROT_ADD_EOF, 0 }, |
| 318 | { PORT100_IN_PROT_CHECK_EOF, 0 }, |
| 319 | { PORT100_IN_PROT_DEAF_TIME, 4 }, |
| 320 | { PORT100_IN_PROT_CRM, 0 }, |
| 321 | { PORT100_IN_PROT_CRM_MIN_LEN, 0 }, |
| 322 | { PORT100_IN_PROT_T1_TAG_FRAME, 0 }, |
| 323 | { PORT100_IN_PROT_RFCA, 0 }, |
| 324 | { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 }, |
| 325 | { PORT100_IN_PROT_END, 0 }, |
| 326 | }, |
| 327 | [NFC_DIGITAL_FRAMING_NFCF_T3T] = { |
| 328 | /* nfc_digital_framing_nfcf */ |
| 329 | { PORT100_IN_PROT_END, 0 }, |
| 330 | }, |
| 331 | [NFC_DIGITAL_FRAMING_NFCF_NFC_DEP] = { |
| 332 | /* nfc_digital_framing_nfcf */ |
| 333 | { PORT100_IN_PROT_END, 0 }, |
| 334 | }, |
| 335 | [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = { |
| 336 | { PORT100_IN_PROT_END, 0 }, |
| 337 | }, |
Thierry Escande | d3815ea | 2014-01-27 00:31:14 +0100 | [diff] [blame^] | 338 | /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */ |
| 339 | [NFC_DIGITAL_FRAMING_LAST] = { |
| 340 | { PORT100_IN_PROT_END, 0 }, |
| 341 | }, |
Thierry Escande | 9f7b57f | 2013-10-04 12:12:02 +0200 | [diff] [blame] | 342 | }; |
| 343 | |
Thierry Escande | 7227c02 | 2013-10-04 12:12:03 +0200 | [diff] [blame] | 344 | static struct port100_protocol |
| 345 | tg_protocols[][PORT100_TG_MAX_NUM_PROTOCOLS + 1] = { |
| 346 | [NFC_DIGITAL_FRAMING_NFCA_SHORT] = { |
| 347 | { PORT100_TG_PROT_END, 0 }, |
| 348 | }, |
| 349 | [NFC_DIGITAL_FRAMING_NFCA_STANDARD] = { |
| 350 | { PORT100_TG_PROT_END, 0 }, |
| 351 | }, |
| 352 | [NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A] = { |
| 353 | { PORT100_TG_PROT_END, 0 }, |
| 354 | }, |
| 355 | [NFC_DIGITAL_FRAMING_NFCA_T1T] = { |
| 356 | { PORT100_TG_PROT_END, 0 }, |
| 357 | }, |
| 358 | [NFC_DIGITAL_FRAMING_NFCA_T2T] = { |
| 359 | { PORT100_TG_PROT_END, 0 }, |
| 360 | }, |
| 361 | [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP] = { |
| 362 | { PORT100_TG_PROT_TU, 1 }, |
| 363 | { PORT100_TG_PROT_RF_OFF, 0 }, |
| 364 | { PORT100_TG_PROT_CRM, 7 }, |
| 365 | { PORT100_TG_PROT_END, 0 }, |
| 366 | }, |
| 367 | [NFC_DIGITAL_FRAMING_NFCF] = { |
| 368 | { PORT100_TG_PROT_END, 0 }, |
| 369 | }, |
| 370 | [NFC_DIGITAL_FRAMING_NFCF_T3T] = { |
| 371 | { PORT100_TG_PROT_END, 0 }, |
| 372 | }, |
| 373 | [NFC_DIGITAL_FRAMING_NFCF_NFC_DEP] = { |
| 374 | { PORT100_TG_PROT_TU, 1 }, |
| 375 | { PORT100_TG_PROT_RF_OFF, 0 }, |
| 376 | { PORT100_TG_PROT_CRM, 7 }, |
| 377 | { PORT100_TG_PROT_END, 0 }, |
| 378 | }, |
| 379 | [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = { |
| 380 | { PORT100_TG_PROT_RF_OFF, 1 }, |
| 381 | { PORT100_TG_PROT_END, 0 }, |
| 382 | }, |
Thierry Escande | d3815ea | 2014-01-27 00:31:14 +0100 | [diff] [blame^] | 383 | /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */ |
| 384 | [NFC_DIGITAL_FRAMING_LAST] = { |
| 385 | { PORT100_TG_PROT_END, 0 }, |
| 386 | }, |
Thierry Escande | 7227c02 | 2013-10-04 12:12:03 +0200 | [diff] [blame] | 387 | }; |
| 388 | |
Thierry Escande | 562d4d5 | 2013-10-04 12:12:00 +0200 | [diff] [blame] | 389 | struct port100 { |
| 390 | struct nfc_digital_dev *nfc_digital_dev; |
| 391 | |
| 392 | int skb_headroom; |
| 393 | int skb_tailroom; |
| 394 | |
| 395 | struct usb_device *udev; |
| 396 | struct usb_interface *interface; |
Thierry Escande | 0347a6a | 2013-10-04 12:12:01 +0200 | [diff] [blame] | 397 | |
| 398 | struct urb *out_urb; |
| 399 | struct urb *in_urb; |
| 400 | |
| 401 | struct work_struct cmd_complete_work; |
| 402 | |
| 403 | u8 cmd_type; |
| 404 | |
| 405 | /* The digital stack serializes commands to be sent. There is no need |
| 406 | * for any queuing/locking mechanism at driver level. |
| 407 | */ |
| 408 | struct port100_cmd *cmd; |
Thierry Escande | 562d4d5 | 2013-10-04 12:12:00 +0200 | [diff] [blame] | 409 | }; |
| 410 | |
Thierry Escande | 0347a6a | 2013-10-04 12:12:01 +0200 | [diff] [blame] | 411 | struct port100_cmd { |
| 412 | u8 code; |
| 413 | int status; |
| 414 | struct sk_buff *req; |
| 415 | struct sk_buff *resp; |
| 416 | int resp_len; |
| 417 | port100_send_async_complete_t complete_cb; |
| 418 | void *complete_cb_context; |
| 419 | }; |
| 420 | |
| 421 | struct port100_frame { |
| 422 | u8 preamble; |
| 423 | __be16 start_frame; |
| 424 | __be16 extended_frame; |
| 425 | __le16 datalen; |
| 426 | u8 datalen_checksum; |
| 427 | u8 data[]; |
| 428 | } __packed; |
| 429 | |
| 430 | struct port100_ack_frame { |
| 431 | u8 preamble; |
| 432 | __be16 start_frame; |
| 433 | __be16 ack_frame; |
| 434 | u8 postambule; |
| 435 | } __packed; |
| 436 | |
| 437 | struct port100_cb_arg { |
| 438 | nfc_digital_cmd_complete_t complete_cb; |
| 439 | void *complete_arg; |
| 440 | u8 mdaa; |
| 441 | }; |
| 442 | |
| 443 | struct port100_tg_comm_rf_cmd { |
| 444 | __le16 guard_time; |
| 445 | __le16 send_timeout; |
| 446 | u8 mdaa; |
| 447 | u8 nfca_param[6]; |
| 448 | u8 nfcf_param[18]; |
| 449 | u8 mf_halted; |
| 450 | u8 arae_flag; |
| 451 | __le16 recv_timeout; |
| 452 | u8 data[]; |
| 453 | } __packed; |
| 454 | |
Thierry Escande | 7227c02 | 2013-10-04 12:12:03 +0200 | [diff] [blame] | 455 | struct port100_tg_comm_rf_res { |
| 456 | u8 comm_type; |
| 457 | u8 ar_status; |
| 458 | u8 target_activated; |
| 459 | __le32 status; |
| 460 | u8 data[]; |
| 461 | } __packed; |
| 462 | |
Thierry Escande | 0347a6a | 2013-10-04 12:12:01 +0200 | [diff] [blame] | 463 | /* The rule: value + checksum = 0 */ |
| 464 | static inline u8 port100_checksum(u16 value) |
| 465 | { |
| 466 | return ~(((u8 *)&value)[0] + ((u8 *)&value)[1]) + 1; |
| 467 | } |
| 468 | |
| 469 | /* The rule: sum(data elements) + checksum = 0 */ |
| 470 | static u8 port100_data_checksum(u8 *data, int datalen) |
| 471 | { |
| 472 | u8 sum = 0; |
| 473 | int i; |
| 474 | |
| 475 | for (i = 0; i < datalen; i++) |
| 476 | sum += data[i]; |
| 477 | |
| 478 | return port100_checksum(sum); |
| 479 | } |
| 480 | |
| 481 | static void port100_tx_frame_init(void *_frame, u8 cmd_code) |
| 482 | { |
| 483 | struct port100_frame *frame = _frame; |
| 484 | |
| 485 | frame->preamble = 0; |
| 486 | frame->start_frame = cpu_to_be16(PORT100_FRAME_SOF); |
| 487 | frame->extended_frame = cpu_to_be16(PORT100_FRAME_EXT); |
| 488 | PORT100_FRAME_DIRECTION(frame) = PORT100_FRAME_DIR_OUT; |
| 489 | PORT100_FRAME_CMD(frame) = cmd_code; |
| 490 | frame->datalen = cpu_to_le16(2); |
| 491 | } |
| 492 | |
| 493 | static void port100_tx_frame_finish(void *_frame) |
| 494 | { |
| 495 | struct port100_frame *frame = _frame; |
| 496 | |
| 497 | frame->datalen_checksum = port100_checksum(le16_to_cpu(frame->datalen)); |
| 498 | |
| 499 | PORT100_FRAME_CHECKSUM(frame) = |
| 500 | port100_data_checksum(frame->data, le16_to_cpu(frame->datalen)); |
| 501 | |
| 502 | PORT100_FRAME_POSTAMBLE(frame) = 0; |
| 503 | } |
| 504 | |
| 505 | static void port100_tx_update_payload_len(void *_frame, int len) |
| 506 | { |
| 507 | struct port100_frame *frame = _frame; |
| 508 | |
| 509 | frame->datalen = cpu_to_le16(le16_to_cpu(frame->datalen) + len); |
| 510 | } |
| 511 | |
| 512 | static bool port100_rx_frame_is_valid(void *_frame) |
| 513 | { |
| 514 | u8 checksum; |
| 515 | struct port100_frame *frame = _frame; |
| 516 | |
| 517 | if (frame->start_frame != cpu_to_be16(PORT100_FRAME_SOF) || |
| 518 | frame->extended_frame != cpu_to_be16(PORT100_FRAME_EXT)) |
| 519 | return false; |
| 520 | |
| 521 | checksum = port100_checksum(le16_to_cpu(frame->datalen)); |
| 522 | if (checksum != frame->datalen_checksum) |
| 523 | return false; |
| 524 | |
| 525 | checksum = port100_data_checksum(frame->data, |
| 526 | le16_to_cpu(frame->datalen)); |
| 527 | if (checksum != PORT100_FRAME_CHECKSUM(frame)) |
| 528 | return false; |
| 529 | |
| 530 | return true; |
| 531 | } |
| 532 | |
| 533 | static bool port100_rx_frame_is_ack(struct port100_ack_frame *frame) |
| 534 | { |
| 535 | return (frame->start_frame == cpu_to_be16(PORT100_FRAME_SOF) && |
| 536 | frame->ack_frame == cpu_to_be16(PORT100_FRAME_ACK)); |
| 537 | } |
| 538 | |
| 539 | static inline int port100_rx_frame_size(void *frame) |
| 540 | { |
| 541 | struct port100_frame *f = frame; |
| 542 | |
| 543 | return sizeof(struct port100_frame) + le16_to_cpu(f->datalen) + |
| 544 | PORT100_FRAME_TAIL_LEN; |
| 545 | } |
| 546 | |
| 547 | static bool port100_rx_frame_is_cmd_response(struct port100 *dev, void *frame) |
| 548 | { |
| 549 | struct port100_frame *f = frame; |
| 550 | |
| 551 | return (PORT100_FRAME_CMD(f) == PORT100_CMD_RESPONSE(dev->cmd->code)); |
| 552 | } |
| 553 | |
| 554 | static void port100_recv_response(struct urb *urb) |
| 555 | { |
| 556 | struct port100 *dev = urb->context; |
| 557 | struct port100_cmd *cmd = dev->cmd; |
| 558 | u8 *in_frame; |
| 559 | |
| 560 | cmd->status = urb->status; |
| 561 | |
| 562 | switch (urb->status) { |
| 563 | case 0: |
| 564 | break; /* success */ |
| 565 | case -ECONNRESET: |
| 566 | case -ENOENT: |
| 567 | nfc_err(&dev->interface->dev, |
| 568 | "The urb has been canceled (status %d)", urb->status); |
| 569 | goto sched_wq; |
| 570 | case -ESHUTDOWN: |
| 571 | default: |
| 572 | nfc_err(&dev->interface->dev, "Urb failure (status %d)", |
| 573 | urb->status); |
| 574 | goto sched_wq; |
| 575 | } |
| 576 | |
| 577 | in_frame = dev->in_urb->transfer_buffer; |
| 578 | |
| 579 | if (!port100_rx_frame_is_valid(in_frame)) { |
| 580 | nfc_err(&dev->interface->dev, "Received an invalid frame"); |
| 581 | cmd->status = -EIO; |
| 582 | goto sched_wq; |
| 583 | } |
| 584 | |
| 585 | print_hex_dump_debug("PORT100 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame, |
| 586 | port100_rx_frame_size(in_frame), false); |
| 587 | |
| 588 | if (!port100_rx_frame_is_cmd_response(dev, in_frame)) { |
| 589 | nfc_err(&dev->interface->dev, |
| 590 | "It's not the response to the last command"); |
| 591 | cmd->status = -EIO; |
| 592 | goto sched_wq; |
| 593 | } |
| 594 | |
| 595 | sched_wq: |
| 596 | schedule_work(&dev->cmd_complete_work); |
| 597 | } |
| 598 | |
| 599 | static int port100_submit_urb_for_response(struct port100 *dev, gfp_t flags) |
| 600 | { |
| 601 | dev->in_urb->complete = port100_recv_response; |
| 602 | |
| 603 | return usb_submit_urb(dev->in_urb, flags); |
| 604 | } |
| 605 | |
| 606 | static void port100_recv_ack(struct urb *urb) |
| 607 | { |
| 608 | struct port100 *dev = urb->context; |
| 609 | struct port100_cmd *cmd = dev->cmd; |
| 610 | struct port100_ack_frame *in_frame; |
| 611 | int rc; |
| 612 | |
| 613 | cmd->status = urb->status; |
| 614 | |
| 615 | switch (urb->status) { |
| 616 | case 0: |
| 617 | break; /* success */ |
| 618 | case -ECONNRESET: |
| 619 | case -ENOENT: |
| 620 | nfc_err(&dev->interface->dev, |
| 621 | "The urb has been stopped (status %d)", urb->status); |
| 622 | goto sched_wq; |
| 623 | case -ESHUTDOWN: |
| 624 | default: |
| 625 | nfc_err(&dev->interface->dev, "Urb failure (status %d)", |
| 626 | urb->status); |
| 627 | goto sched_wq; |
| 628 | } |
| 629 | |
| 630 | in_frame = dev->in_urb->transfer_buffer; |
| 631 | |
| 632 | if (!port100_rx_frame_is_ack(in_frame)) { |
| 633 | nfc_err(&dev->interface->dev, "Received an invalid ack"); |
| 634 | cmd->status = -EIO; |
| 635 | goto sched_wq; |
| 636 | } |
| 637 | |
| 638 | rc = port100_submit_urb_for_response(dev, GFP_ATOMIC); |
| 639 | if (rc) { |
| 640 | nfc_err(&dev->interface->dev, |
| 641 | "usb_submit_urb failed with result %d", rc); |
| 642 | cmd->status = rc; |
| 643 | goto sched_wq; |
| 644 | } |
| 645 | |
| 646 | return; |
| 647 | |
| 648 | sched_wq: |
| 649 | schedule_work(&dev->cmd_complete_work); |
| 650 | } |
| 651 | |
| 652 | static int port100_submit_urb_for_ack(struct port100 *dev, gfp_t flags) |
| 653 | { |
| 654 | dev->in_urb->complete = port100_recv_ack; |
| 655 | |
| 656 | return usb_submit_urb(dev->in_urb, flags); |
| 657 | } |
| 658 | |
| 659 | static int port100_send_ack(struct port100 *dev) |
| 660 | { |
| 661 | int rc; |
| 662 | |
| 663 | dev->out_urb->transfer_buffer = ack_frame; |
| 664 | dev->out_urb->transfer_buffer_length = sizeof(ack_frame); |
| 665 | rc = usb_submit_urb(dev->out_urb, GFP_KERNEL); |
| 666 | |
| 667 | return rc; |
| 668 | } |
| 669 | |
| 670 | static int port100_send_frame_async(struct port100 *dev, struct sk_buff *out, |
| 671 | struct sk_buff *in, int in_len) |
| 672 | { |
| 673 | int rc; |
| 674 | |
| 675 | dev->out_urb->transfer_buffer = out->data; |
| 676 | dev->out_urb->transfer_buffer_length = out->len; |
| 677 | |
| 678 | dev->in_urb->transfer_buffer = in->data; |
| 679 | dev->in_urb->transfer_buffer_length = in_len; |
| 680 | |
| 681 | print_hex_dump_debug("PORT100 TX: ", DUMP_PREFIX_NONE, 16, 1, |
| 682 | out->data, out->len, false); |
| 683 | |
| 684 | rc = usb_submit_urb(dev->out_urb, GFP_KERNEL); |
| 685 | if (rc) |
| 686 | return rc; |
| 687 | |
| 688 | rc = port100_submit_urb_for_ack(dev, GFP_KERNEL); |
| 689 | if (rc) |
| 690 | goto error; |
| 691 | |
| 692 | return 0; |
| 693 | |
| 694 | error: |
| 695 | usb_unlink_urb(dev->out_urb); |
| 696 | return rc; |
| 697 | } |
| 698 | |
| 699 | static void port100_build_cmd_frame(struct port100 *dev, u8 cmd_code, |
| 700 | struct sk_buff *skb) |
| 701 | { |
| 702 | /* payload is already there, just update datalen */ |
| 703 | int payload_len = skb->len; |
| 704 | |
| 705 | skb_push(skb, PORT100_FRAME_HEADER_LEN); |
| 706 | skb_put(skb, PORT100_FRAME_TAIL_LEN); |
| 707 | |
| 708 | port100_tx_frame_init(skb->data, cmd_code); |
| 709 | port100_tx_update_payload_len(skb->data, payload_len); |
| 710 | port100_tx_frame_finish(skb->data); |
| 711 | } |
| 712 | |
| 713 | static void port100_send_async_complete(struct port100 *dev) |
| 714 | { |
| 715 | struct port100_cmd *cmd = dev->cmd; |
| 716 | int status = cmd->status; |
| 717 | |
| 718 | struct sk_buff *req = cmd->req; |
| 719 | struct sk_buff *resp = cmd->resp; |
| 720 | |
| 721 | dev_kfree_skb(req); |
| 722 | |
| 723 | dev->cmd = NULL; |
| 724 | |
| 725 | if (status < 0) { |
| 726 | cmd->complete_cb(dev, cmd->complete_cb_context, |
| 727 | ERR_PTR(status)); |
| 728 | dev_kfree_skb(resp); |
| 729 | goto done; |
| 730 | } |
| 731 | |
| 732 | skb_put(resp, port100_rx_frame_size(resp->data)); |
| 733 | skb_pull(resp, PORT100_FRAME_HEADER_LEN); |
| 734 | skb_trim(resp, resp->len - PORT100_FRAME_TAIL_LEN); |
| 735 | |
| 736 | cmd->complete_cb(dev, cmd->complete_cb_context, resp); |
| 737 | |
| 738 | done: |
| 739 | kfree(cmd); |
| 740 | } |
| 741 | |
| 742 | static int port100_send_cmd_async(struct port100 *dev, u8 cmd_code, |
| 743 | struct sk_buff *req, |
| 744 | port100_send_async_complete_t complete_cb, |
| 745 | void *complete_cb_context) |
| 746 | { |
| 747 | struct port100_cmd *cmd; |
| 748 | struct sk_buff *resp; |
| 749 | int rc; |
| 750 | int resp_len = PORT100_FRAME_HEADER_LEN + |
| 751 | PORT100_FRAME_MAX_PAYLOAD_LEN + |
| 752 | PORT100_FRAME_TAIL_LEN; |
| 753 | |
| 754 | resp = alloc_skb(resp_len, GFP_KERNEL); |
| 755 | if (!resp) |
| 756 | return -ENOMEM; |
| 757 | |
| 758 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
| 759 | if (!cmd) { |
| 760 | dev_kfree_skb(resp); |
| 761 | return -ENOMEM; |
| 762 | } |
| 763 | |
| 764 | cmd->code = cmd_code; |
| 765 | cmd->req = req; |
| 766 | cmd->resp = resp; |
| 767 | cmd->resp_len = resp_len; |
| 768 | cmd->complete_cb = complete_cb; |
| 769 | cmd->complete_cb_context = complete_cb_context; |
| 770 | |
| 771 | port100_build_cmd_frame(dev, cmd_code, req); |
| 772 | |
| 773 | dev->cmd = cmd; |
| 774 | |
| 775 | rc = port100_send_frame_async(dev, req, resp, resp_len); |
| 776 | if (rc) { |
| 777 | kfree(cmd); |
| 778 | dev_kfree_skb(resp); |
| 779 | dev->cmd = NULL; |
| 780 | } |
| 781 | |
| 782 | return rc; |
| 783 | } |
| 784 | |
| 785 | struct port100_sync_cmd_response { |
| 786 | struct sk_buff *resp; |
| 787 | struct completion done; |
| 788 | }; |
| 789 | |
| 790 | static void port100_wq_cmd_complete(struct work_struct *work) |
| 791 | { |
| 792 | struct port100 *dev = container_of(work, struct port100, |
| 793 | cmd_complete_work); |
| 794 | |
| 795 | port100_send_async_complete(dev); |
| 796 | } |
| 797 | |
| 798 | static void port100_send_sync_complete(struct port100 *dev, void *_arg, |
| 799 | struct sk_buff *resp) |
| 800 | { |
| 801 | struct port100_sync_cmd_response *arg = _arg; |
| 802 | |
| 803 | arg->resp = resp; |
| 804 | complete(&arg->done); |
| 805 | } |
| 806 | |
| 807 | static struct sk_buff *port100_send_cmd_sync(struct port100 *dev, u8 cmd_code, |
| 808 | struct sk_buff *req) |
| 809 | { |
| 810 | int rc; |
| 811 | struct port100_sync_cmd_response arg; |
| 812 | |
| 813 | init_completion(&arg.done); |
| 814 | |
| 815 | rc = port100_send_cmd_async(dev, cmd_code, req, |
| 816 | port100_send_sync_complete, &arg); |
| 817 | if (rc) { |
| 818 | dev_kfree_skb(req); |
| 819 | return ERR_PTR(rc); |
| 820 | } |
| 821 | |
| 822 | wait_for_completion(&arg.done); |
| 823 | |
| 824 | return arg.resp; |
| 825 | } |
| 826 | |
| 827 | static void port100_send_complete(struct urb *urb) |
| 828 | { |
| 829 | struct port100 *dev = urb->context; |
| 830 | |
| 831 | switch (urb->status) { |
| 832 | case 0: |
| 833 | break; /* success */ |
| 834 | case -ECONNRESET: |
| 835 | case -ENOENT: |
| 836 | nfc_err(&dev->interface->dev, |
| 837 | "The urb has been stopped (status %d)", urb->status); |
| 838 | break; |
| 839 | case -ESHUTDOWN: |
| 840 | default: |
| 841 | nfc_err(&dev->interface->dev, "Urb failure (status %d)", |
| 842 | urb->status); |
| 843 | } |
| 844 | } |
| 845 | |
Thierry Escande | 562d4d5 | 2013-10-04 12:12:00 +0200 | [diff] [blame] | 846 | static void port100_abort_cmd(struct nfc_digital_dev *ddev) |
| 847 | { |
Thierry Escande | 0347a6a | 2013-10-04 12:12:01 +0200 | [diff] [blame] | 848 | struct port100 *dev = nfc_digital_get_drvdata(ddev); |
| 849 | |
| 850 | /* An ack will cancel the last issued command */ |
| 851 | port100_send_ack(dev); |
| 852 | |
| 853 | /* cancel the urb request */ |
| 854 | usb_kill_urb(dev->in_urb); |
| 855 | } |
| 856 | |
| 857 | static struct sk_buff *port100_alloc_skb(struct port100 *dev, unsigned int size) |
| 858 | { |
| 859 | struct sk_buff *skb; |
| 860 | |
| 861 | skb = alloc_skb(dev->skb_headroom + dev->skb_tailroom + size, |
| 862 | GFP_KERNEL); |
| 863 | if (skb) |
| 864 | skb_reserve(skb, dev->skb_headroom); |
| 865 | |
| 866 | return skb; |
| 867 | } |
| 868 | |
| 869 | static int port100_set_command_type(struct port100 *dev, u8 command_type) |
| 870 | { |
| 871 | struct sk_buff *skb; |
| 872 | struct sk_buff *resp; |
| 873 | int rc; |
| 874 | |
| 875 | skb = port100_alloc_skb(dev, 1); |
| 876 | if (!skb) |
| 877 | return -ENOMEM; |
| 878 | |
| 879 | *skb_put(skb, sizeof(u8)) = command_type; |
| 880 | |
| 881 | resp = port100_send_cmd_sync(dev, PORT100_CMD_SET_COMMAND_TYPE, skb); |
| 882 | if (IS_ERR(resp)) |
| 883 | return PTR_ERR(resp); |
| 884 | |
| 885 | rc = resp->data[0]; |
| 886 | |
| 887 | dev_kfree_skb(resp); |
| 888 | |
| 889 | return rc; |
| 890 | } |
| 891 | |
| 892 | static u64 port100_get_command_type_mask(struct port100 *dev) |
| 893 | { |
| 894 | struct sk_buff *skb; |
| 895 | struct sk_buff *resp; |
| 896 | u64 mask; |
| 897 | |
| 898 | skb = port100_alloc_skb(dev, 0); |
| 899 | if (!skb) |
| 900 | return -ENOMEM; |
| 901 | |
| 902 | resp = port100_send_cmd_sync(dev, PORT100_CMD_GET_COMMAND_TYPE, skb); |
| 903 | if (IS_ERR(resp)) |
| 904 | return PTR_ERR(resp); |
| 905 | |
| 906 | if (resp->len < 8) |
| 907 | mask = 0; |
| 908 | else |
| 909 | mask = be64_to_cpu(*(__be64 *)resp->data); |
| 910 | |
| 911 | dev_kfree_skb(resp); |
| 912 | |
| 913 | return mask; |
| 914 | } |
| 915 | |
| 916 | static u16 port100_get_firmware_version(struct port100 *dev) |
| 917 | { |
| 918 | struct sk_buff *skb; |
| 919 | struct sk_buff *resp; |
| 920 | u16 fw_ver; |
| 921 | |
| 922 | skb = port100_alloc_skb(dev, 0); |
| 923 | if (!skb) |
| 924 | return 0; |
| 925 | |
| 926 | resp = port100_send_cmd_sync(dev, PORT100_CMD_GET_FIRMWARE_VERSION, |
| 927 | skb); |
| 928 | if (IS_ERR(resp)) |
| 929 | return 0; |
| 930 | |
| 931 | fw_ver = le16_to_cpu(*(__le16 *)resp->data); |
| 932 | |
| 933 | dev_kfree_skb(resp); |
| 934 | |
| 935 | return fw_ver; |
Thierry Escande | 562d4d5 | 2013-10-04 12:12:00 +0200 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | static int port100_switch_rf(struct nfc_digital_dev *ddev, bool on) |
| 939 | { |
Thierry Escande | 9f7b57f | 2013-10-04 12:12:02 +0200 | [diff] [blame] | 940 | struct port100 *dev = nfc_digital_get_drvdata(ddev); |
| 941 | struct sk_buff *skb, *resp; |
| 942 | |
| 943 | skb = port100_alloc_skb(dev, 1); |
| 944 | if (!skb) |
| 945 | return -ENOMEM; |
| 946 | |
| 947 | *skb_put(skb, 1) = on ? 1 : 0; |
| 948 | |
| 949 | resp = port100_send_cmd_sync(dev, PORT100_CMD_SWITCH_RF, skb); |
| 950 | |
| 951 | if (IS_ERR(resp)) |
| 952 | return PTR_ERR(resp); |
| 953 | |
| 954 | dev_kfree_skb(resp); |
| 955 | |
| 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | static int port100_in_set_rf(struct nfc_digital_dev *ddev, u8 rf) |
| 960 | { |
| 961 | struct port100 *dev = nfc_digital_get_drvdata(ddev); |
| 962 | struct sk_buff *skb; |
| 963 | struct sk_buff *resp; |
| 964 | int rc; |
| 965 | |
| 966 | if (rf >= NFC_DIGITAL_RF_TECH_LAST) |
| 967 | return -EINVAL; |
| 968 | |
| 969 | skb = port100_alloc_skb(dev, sizeof(struct port100_in_rf_setting)); |
| 970 | if (!skb) |
| 971 | return -ENOMEM; |
| 972 | |
| 973 | memcpy(skb_put(skb, sizeof(struct port100_in_rf_setting)), |
| 974 | &in_rf_settings[rf], |
| 975 | sizeof(struct port100_in_rf_setting)); |
| 976 | |
| 977 | resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_RF, skb); |
| 978 | |
| 979 | if (IS_ERR(resp)) |
| 980 | return PTR_ERR(resp); |
| 981 | |
| 982 | rc = resp->data[0]; |
| 983 | |
| 984 | dev_kfree_skb(resp); |
| 985 | |
| 986 | return rc; |
| 987 | } |
| 988 | |
| 989 | static int port100_in_set_framing(struct nfc_digital_dev *ddev, int param) |
| 990 | { |
| 991 | struct port100 *dev = nfc_digital_get_drvdata(ddev); |
| 992 | struct port100_protocol *protocols; |
| 993 | struct sk_buff *skb; |
| 994 | struct sk_buff *resp; |
| 995 | int num_protocols; |
| 996 | size_t size; |
| 997 | int rc; |
| 998 | |
| 999 | if (param >= NFC_DIGITAL_FRAMING_LAST) |
| 1000 | return -EINVAL; |
| 1001 | |
| 1002 | protocols = in_protocols[param]; |
| 1003 | |
| 1004 | num_protocols = 0; |
| 1005 | while (protocols[num_protocols].number != PORT100_IN_PROT_END) |
| 1006 | num_protocols++; |
| 1007 | |
| 1008 | if (!num_protocols) |
| 1009 | return 0; |
| 1010 | |
| 1011 | size = sizeof(struct port100_protocol) * num_protocols; |
| 1012 | |
| 1013 | skb = port100_alloc_skb(dev, size); |
| 1014 | if (!skb) |
| 1015 | return -ENOMEM; |
| 1016 | |
| 1017 | memcpy(skb_put(skb, size), protocols, size); |
| 1018 | |
| 1019 | resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_PROTOCOL, skb); |
| 1020 | |
| 1021 | if (IS_ERR(resp)) |
| 1022 | return PTR_ERR(resp); |
| 1023 | |
| 1024 | rc = resp->data[0]; |
| 1025 | |
| 1026 | dev_kfree_skb(resp); |
| 1027 | |
| 1028 | return rc; |
Thierry Escande | 562d4d5 | 2013-10-04 12:12:00 +0200 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | static int port100_in_configure_hw(struct nfc_digital_dev *ddev, int type, |
| 1032 | int param) |
| 1033 | { |
Thierry Escande | 9f7b57f | 2013-10-04 12:12:02 +0200 | [diff] [blame] | 1034 | if (type == NFC_DIGITAL_CONFIG_RF_TECH) |
| 1035 | return port100_in_set_rf(ddev, param); |
| 1036 | |
| 1037 | if (type == NFC_DIGITAL_CONFIG_FRAMING) |
| 1038 | return port100_in_set_framing(ddev, param); |
| 1039 | |
| 1040 | return -EINVAL; |
| 1041 | } |
| 1042 | |
| 1043 | static void port100_in_comm_rf_complete(struct port100 *dev, void *arg, |
| 1044 | struct sk_buff *resp) |
| 1045 | { |
| 1046 | struct port100_cb_arg *cb_arg = arg; |
| 1047 | nfc_digital_cmd_complete_t cb = cb_arg->complete_cb; |
| 1048 | u32 status; |
| 1049 | int rc; |
| 1050 | |
| 1051 | if (IS_ERR(resp)) { |
| 1052 | rc = PTR_ERR(resp); |
| 1053 | goto exit; |
| 1054 | } |
| 1055 | |
| 1056 | if (resp->len < 4) { |
| 1057 | nfc_err(&dev->interface->dev, |
| 1058 | "Invalid packet length received.\n"); |
| 1059 | rc = -EIO; |
| 1060 | goto error; |
| 1061 | } |
| 1062 | |
| 1063 | status = le32_to_cpu(*(__le32 *)resp->data); |
| 1064 | |
| 1065 | skb_pull(resp, sizeof(u32)); |
| 1066 | |
| 1067 | if (status == PORT100_CMD_STATUS_TIMEOUT) { |
| 1068 | rc = -ETIMEDOUT; |
| 1069 | goto error; |
| 1070 | } |
| 1071 | |
| 1072 | if (status != PORT100_CMD_STATUS_OK) { |
| 1073 | nfc_err(&dev->interface->dev, |
| 1074 | "in_comm_rf failed with status 0x%08x\n", status); |
| 1075 | rc = -EIO; |
| 1076 | goto error; |
| 1077 | } |
| 1078 | |
| 1079 | /* Remove collision bits byte */ |
| 1080 | skb_pull(resp, 1); |
| 1081 | |
| 1082 | goto exit; |
| 1083 | |
| 1084 | error: |
| 1085 | kfree_skb(resp); |
| 1086 | resp = ERR_PTR(rc); |
| 1087 | |
| 1088 | exit: |
| 1089 | cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp); |
| 1090 | |
| 1091 | kfree(cb_arg); |
Thierry Escande | 562d4d5 | 2013-10-04 12:12:00 +0200 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | static int port100_in_send_cmd(struct nfc_digital_dev *ddev, |
| 1095 | struct sk_buff *skb, u16 _timeout, |
| 1096 | nfc_digital_cmd_complete_t cb, void *arg) |
| 1097 | { |
Thierry Escande | 9f7b57f | 2013-10-04 12:12:02 +0200 | [diff] [blame] | 1098 | struct port100 *dev = nfc_digital_get_drvdata(ddev); |
| 1099 | struct port100_cb_arg *cb_arg; |
| 1100 | __le16 timeout; |
| 1101 | |
| 1102 | cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL); |
| 1103 | if (!cb_arg) |
| 1104 | return -ENOMEM; |
| 1105 | |
| 1106 | cb_arg->complete_cb = cb; |
| 1107 | cb_arg->complete_arg = arg; |
| 1108 | |
| 1109 | timeout = cpu_to_le16(_timeout * 10); |
| 1110 | |
| 1111 | memcpy(skb_push(skb, sizeof(__le16)), &timeout, sizeof(__le16)); |
| 1112 | |
| 1113 | return port100_send_cmd_async(dev, PORT100_CMD_IN_COMM_RF, skb, |
| 1114 | port100_in_comm_rf_complete, cb_arg); |
Thierry Escande | 562d4d5 | 2013-10-04 12:12:00 +0200 | [diff] [blame] | 1115 | } |
| 1116 | |
Thierry Escande | 7227c02 | 2013-10-04 12:12:03 +0200 | [diff] [blame] | 1117 | static int port100_tg_set_rf(struct nfc_digital_dev *ddev, u8 rf) |
| 1118 | { |
| 1119 | struct port100 *dev = nfc_digital_get_drvdata(ddev); |
| 1120 | struct sk_buff *skb; |
| 1121 | struct sk_buff *resp; |
| 1122 | int rc; |
| 1123 | |
| 1124 | if (rf >= NFC_DIGITAL_RF_TECH_LAST) |
| 1125 | return -EINVAL; |
| 1126 | |
| 1127 | skb = port100_alloc_skb(dev, sizeof(struct port100_tg_rf_setting)); |
| 1128 | if (!skb) |
| 1129 | return -ENOMEM; |
| 1130 | |
| 1131 | memcpy(skb_put(skb, sizeof(struct port100_tg_rf_setting)), |
| 1132 | &tg_rf_settings[rf], |
| 1133 | sizeof(struct port100_tg_rf_setting)); |
| 1134 | |
| 1135 | resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_RF, skb); |
| 1136 | |
| 1137 | if (IS_ERR(resp)) |
| 1138 | return PTR_ERR(resp); |
| 1139 | |
| 1140 | rc = resp->data[0]; |
| 1141 | |
| 1142 | dev_kfree_skb(resp); |
| 1143 | |
| 1144 | return rc; |
| 1145 | } |
| 1146 | |
| 1147 | static int port100_tg_set_framing(struct nfc_digital_dev *ddev, int param) |
| 1148 | { |
| 1149 | struct port100 *dev = nfc_digital_get_drvdata(ddev); |
| 1150 | struct port100_protocol *protocols; |
| 1151 | struct sk_buff *skb; |
| 1152 | struct sk_buff *resp; |
| 1153 | int rc; |
| 1154 | int num_protocols; |
| 1155 | size_t size; |
| 1156 | |
| 1157 | if (param >= NFC_DIGITAL_FRAMING_LAST) |
| 1158 | return -EINVAL; |
| 1159 | |
| 1160 | protocols = tg_protocols[param]; |
| 1161 | |
| 1162 | num_protocols = 0; |
| 1163 | while (protocols[num_protocols].number != PORT100_TG_PROT_END) |
| 1164 | num_protocols++; |
| 1165 | |
| 1166 | if (!num_protocols) |
| 1167 | return 0; |
| 1168 | |
| 1169 | size = sizeof(struct port100_protocol) * num_protocols; |
| 1170 | |
| 1171 | skb = port100_alloc_skb(dev, size); |
| 1172 | if (!skb) |
| 1173 | return -ENOMEM; |
| 1174 | |
| 1175 | memcpy(skb_put(skb, size), protocols, size); |
| 1176 | |
| 1177 | resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_PROTOCOL, skb); |
| 1178 | |
| 1179 | if (IS_ERR(resp)) |
| 1180 | return PTR_ERR(resp); |
| 1181 | |
| 1182 | rc = resp->data[0]; |
| 1183 | |
| 1184 | dev_kfree_skb(resp); |
| 1185 | |
| 1186 | return rc; |
| 1187 | } |
| 1188 | |
Thierry Escande | 562d4d5 | 2013-10-04 12:12:00 +0200 | [diff] [blame] | 1189 | static int port100_tg_configure_hw(struct nfc_digital_dev *ddev, int type, |
| 1190 | int param) |
| 1191 | { |
Thierry Escande | 7227c02 | 2013-10-04 12:12:03 +0200 | [diff] [blame] | 1192 | if (type == NFC_DIGITAL_CONFIG_RF_TECH) |
| 1193 | return port100_tg_set_rf(ddev, param); |
| 1194 | |
| 1195 | if (type == NFC_DIGITAL_CONFIG_FRAMING) |
| 1196 | return port100_tg_set_framing(ddev, param); |
| 1197 | |
| 1198 | return -EINVAL; |
| 1199 | } |
| 1200 | |
| 1201 | static bool port100_tg_target_activated(struct port100 *dev, u8 tgt_activated) |
| 1202 | { |
| 1203 | u8 mask; |
| 1204 | |
| 1205 | switch (dev->cmd_type) { |
| 1206 | case PORT100_CMD_TYPE_0: |
| 1207 | mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK; |
| 1208 | break; |
| 1209 | case PORT100_CMD_TYPE_1: |
| 1210 | mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK | |
| 1211 | PORT100_MDAA_TGT_WAS_ACTIVATED_MASK; |
| 1212 | break; |
| 1213 | default: |
| 1214 | nfc_err(&dev->interface->dev, "Unknonwn command type.\n"); |
| 1215 | return false; |
| 1216 | } |
| 1217 | |
| 1218 | return ((tgt_activated & mask) == mask); |
| 1219 | } |
| 1220 | |
| 1221 | static void port100_tg_comm_rf_complete(struct port100 *dev, void *arg, |
| 1222 | struct sk_buff *resp) |
| 1223 | { |
| 1224 | u32 status; |
| 1225 | struct port100_cb_arg *cb_arg = arg; |
| 1226 | nfc_digital_cmd_complete_t cb = cb_arg->complete_cb; |
| 1227 | struct port100_tg_comm_rf_res *hdr; |
| 1228 | |
| 1229 | if (IS_ERR(resp)) |
| 1230 | goto exit; |
| 1231 | |
| 1232 | hdr = (struct port100_tg_comm_rf_res *)resp->data; |
| 1233 | |
| 1234 | status = le32_to_cpu(hdr->status); |
| 1235 | |
| 1236 | if (cb_arg->mdaa && |
| 1237 | !port100_tg_target_activated(dev, hdr->target_activated)) { |
| 1238 | kfree_skb(resp); |
| 1239 | resp = ERR_PTR(-ETIMEDOUT); |
| 1240 | |
| 1241 | goto exit; |
| 1242 | } |
| 1243 | |
| 1244 | skb_pull(resp, sizeof(struct port100_tg_comm_rf_res)); |
| 1245 | |
| 1246 | if (status != PORT100_CMD_STATUS_OK) { |
| 1247 | kfree_skb(resp); |
| 1248 | |
| 1249 | if (status == PORT100_CMD_STATUS_TIMEOUT) |
| 1250 | resp = ERR_PTR(-ETIMEDOUT); |
| 1251 | else |
| 1252 | resp = ERR_PTR(-EIO); |
| 1253 | } |
| 1254 | |
| 1255 | exit: |
| 1256 | cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp); |
| 1257 | |
| 1258 | kfree(cb_arg); |
Thierry Escande | 562d4d5 | 2013-10-04 12:12:00 +0200 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | static int port100_tg_send_cmd(struct nfc_digital_dev *ddev, |
| 1262 | struct sk_buff *skb, u16 timeout, |
| 1263 | nfc_digital_cmd_complete_t cb, void *arg) |
| 1264 | { |
Thierry Escande | 7227c02 | 2013-10-04 12:12:03 +0200 | [diff] [blame] | 1265 | struct port100 *dev = nfc_digital_get_drvdata(ddev); |
| 1266 | struct port100_tg_comm_rf_cmd *hdr; |
| 1267 | struct port100_cb_arg *cb_arg; |
| 1268 | |
| 1269 | cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL); |
| 1270 | if (!cb_arg) |
| 1271 | return -ENOMEM; |
| 1272 | |
| 1273 | cb_arg->complete_cb = cb; |
| 1274 | cb_arg->complete_arg = arg; |
| 1275 | |
| 1276 | skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd)); |
| 1277 | |
| 1278 | hdr = (struct port100_tg_comm_rf_cmd *)skb->data; |
| 1279 | |
| 1280 | memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd)); |
| 1281 | hdr->guard_time = cpu_to_le16(500); |
| 1282 | hdr->send_timeout = cpu_to_le16(0xFFFF); |
| 1283 | hdr->recv_timeout = cpu_to_le16(timeout); |
| 1284 | |
| 1285 | return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb, |
| 1286 | port100_tg_comm_rf_complete, cb_arg); |
Thierry Escande | 562d4d5 | 2013-10-04 12:12:00 +0200 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | static int port100_listen_mdaa(struct nfc_digital_dev *ddev, |
| 1290 | struct digital_tg_mdaa_params *params, |
| 1291 | u16 timeout, |
| 1292 | nfc_digital_cmd_complete_t cb, void *arg) |
| 1293 | { |
Thierry Escande | 7227c02 | 2013-10-04 12:12:03 +0200 | [diff] [blame] | 1294 | struct port100 *dev = nfc_digital_get_drvdata(ddev); |
| 1295 | struct port100_tg_comm_rf_cmd *hdr; |
| 1296 | struct port100_cb_arg *cb_arg; |
| 1297 | struct sk_buff *skb; |
| 1298 | int rc; |
| 1299 | |
| 1300 | rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, |
| 1301 | NFC_DIGITAL_RF_TECH_106A); |
| 1302 | if (rc) |
| 1303 | return rc; |
| 1304 | |
| 1305 | rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, |
| 1306 | NFC_DIGITAL_FRAMING_NFCA_NFC_DEP); |
| 1307 | if (rc) |
| 1308 | return rc; |
| 1309 | |
| 1310 | cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL); |
| 1311 | if (!cb_arg) |
| 1312 | return -ENOMEM; |
| 1313 | |
| 1314 | cb_arg->complete_cb = cb; |
| 1315 | cb_arg->complete_arg = arg; |
| 1316 | cb_arg->mdaa = 1; |
| 1317 | |
| 1318 | skb = port100_alloc_skb(dev, 0); |
| 1319 | if (!skb) { |
| 1320 | kfree(cb_arg); |
| 1321 | return -ENOMEM; |
| 1322 | } |
| 1323 | |
| 1324 | skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd)); |
| 1325 | hdr = (struct port100_tg_comm_rf_cmd *)skb->data; |
| 1326 | |
| 1327 | memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd)); |
| 1328 | |
| 1329 | hdr->guard_time = 0; |
| 1330 | hdr->send_timeout = cpu_to_le16(0xFFFF); |
| 1331 | hdr->mdaa = 1; |
| 1332 | hdr->nfca_param[0] = (params->sens_res >> 8) & 0xFF; |
| 1333 | hdr->nfca_param[1] = params->sens_res & 0xFF; |
| 1334 | memcpy(hdr->nfca_param + 2, params->nfcid1, 3); |
| 1335 | hdr->nfca_param[5] = params->sel_res; |
| 1336 | memcpy(hdr->nfcf_param, params->nfcid2, 8); |
| 1337 | hdr->nfcf_param[16] = (params->sc >> 8) & 0xFF; |
| 1338 | hdr->nfcf_param[17] = params->sc & 0xFF; |
| 1339 | hdr->recv_timeout = cpu_to_le16(timeout); |
| 1340 | |
| 1341 | return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb, |
| 1342 | port100_tg_comm_rf_complete, cb_arg); |
Thierry Escande | 562d4d5 | 2013-10-04 12:12:00 +0200 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | static int port100_listen(struct nfc_digital_dev *ddev, u16 timeout, |
| 1346 | nfc_digital_cmd_complete_t cb, void *arg) |
| 1347 | { |
Thierry Escande | 7227c02 | 2013-10-04 12:12:03 +0200 | [diff] [blame] | 1348 | struct port100 *dev = nfc_digital_get_drvdata(ddev); |
| 1349 | struct sk_buff *skb; |
| 1350 | |
| 1351 | skb = port100_alloc_skb(dev, 0); |
| 1352 | if (!skb) |
| 1353 | return -ENOMEM; |
| 1354 | |
| 1355 | return port100_tg_send_cmd(ddev, skb, timeout, cb, arg); |
Thierry Escande | 562d4d5 | 2013-10-04 12:12:00 +0200 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | static struct nfc_digital_ops port100_digital_ops = { |
| 1359 | .in_configure_hw = port100_in_configure_hw, |
| 1360 | .in_send_cmd = port100_in_send_cmd, |
| 1361 | |
| 1362 | .tg_listen_mdaa = port100_listen_mdaa, |
| 1363 | .tg_listen = port100_listen, |
| 1364 | .tg_configure_hw = port100_tg_configure_hw, |
| 1365 | .tg_send_cmd = port100_tg_send_cmd, |
| 1366 | |
| 1367 | .switch_rf = port100_switch_rf, |
| 1368 | .abort_cmd = port100_abort_cmd, |
| 1369 | }; |
| 1370 | |
| 1371 | static const struct usb_device_id port100_table[] = { |
| 1372 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE, |
| 1373 | .idVendor = SONY_VENDOR_ID, |
| 1374 | .idProduct = RCS380_PRODUCT_ID, |
| 1375 | }, |
| 1376 | { } |
| 1377 | }; |
| 1378 | MODULE_DEVICE_TABLE(usb, port100_table); |
| 1379 | |
| 1380 | static int port100_probe(struct usb_interface *interface, |
| 1381 | const struct usb_device_id *id) |
| 1382 | { |
| 1383 | struct port100 *dev; |
| 1384 | int rc; |
Thierry Escande | 0347a6a | 2013-10-04 12:12:01 +0200 | [diff] [blame] | 1385 | struct usb_host_interface *iface_desc; |
| 1386 | struct usb_endpoint_descriptor *endpoint; |
| 1387 | int in_endpoint; |
| 1388 | int out_endpoint; |
| 1389 | u16 fw_version; |
| 1390 | u64 cmd_type_mask; |
| 1391 | int i; |
Thierry Escande | 562d4d5 | 2013-10-04 12:12:00 +0200 | [diff] [blame] | 1392 | |
| 1393 | dev = devm_kzalloc(&interface->dev, sizeof(struct port100), GFP_KERNEL); |
| 1394 | if (!dev) |
| 1395 | return -ENOMEM; |
| 1396 | |
| 1397 | dev->udev = usb_get_dev(interface_to_usbdev(interface)); |
| 1398 | dev->interface = interface; |
| 1399 | usb_set_intfdata(interface, dev); |
| 1400 | |
Thierry Escande | 0347a6a | 2013-10-04 12:12:01 +0200 | [diff] [blame] | 1401 | in_endpoint = out_endpoint = 0; |
| 1402 | iface_desc = interface->cur_altsetting; |
| 1403 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
| 1404 | endpoint = &iface_desc->endpoint[i].desc; |
| 1405 | |
| 1406 | if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) |
| 1407 | in_endpoint = endpoint->bEndpointAddress; |
| 1408 | |
| 1409 | if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) |
| 1410 | out_endpoint = endpoint->bEndpointAddress; |
| 1411 | } |
| 1412 | |
| 1413 | if (!in_endpoint || !out_endpoint) { |
| 1414 | nfc_err(&interface->dev, |
| 1415 | "Could not find bulk-in or bulk-out endpoint\n"); |
| 1416 | rc = -ENODEV; |
| 1417 | goto error; |
| 1418 | } |
| 1419 | |
| 1420 | dev->in_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1421 | dev->out_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1422 | |
| 1423 | if (!dev->in_urb || !dev->out_urb) { |
| 1424 | nfc_err(&interface->dev, "Could not allocate USB URBs\n"); |
| 1425 | rc = -ENOMEM; |
| 1426 | goto error; |
| 1427 | } |
| 1428 | |
| 1429 | usb_fill_bulk_urb(dev->in_urb, dev->udev, |
| 1430 | usb_rcvbulkpipe(dev->udev, in_endpoint), |
| 1431 | NULL, 0, NULL, dev); |
| 1432 | usb_fill_bulk_urb(dev->out_urb, dev->udev, |
| 1433 | usb_sndbulkpipe(dev->udev, out_endpoint), |
| 1434 | NULL, 0, port100_send_complete, dev); |
| 1435 | |
| 1436 | dev->skb_headroom = PORT100_FRAME_HEADER_LEN + |
| 1437 | PORT100_COMM_RF_HEAD_MAX_LEN; |
| 1438 | dev->skb_tailroom = PORT100_FRAME_TAIL_LEN; |
| 1439 | |
| 1440 | INIT_WORK(&dev->cmd_complete_work, port100_wq_cmd_complete); |
| 1441 | |
| 1442 | /* The first thing to do with the Port-100 is to set the command type |
| 1443 | * to be used. If supported we use command type 1. 0 otherwise. |
| 1444 | */ |
| 1445 | cmd_type_mask = port100_get_command_type_mask(dev); |
| 1446 | if (!cmd_type_mask) { |
| 1447 | nfc_err(&interface->dev, |
| 1448 | "Could not get supported command types.\n"); |
| 1449 | rc = -ENODEV; |
| 1450 | goto error; |
| 1451 | } |
| 1452 | |
| 1453 | if (PORT100_CMD_TYPE_IS_SUPPORTED(cmd_type_mask, PORT100_CMD_TYPE_1)) |
| 1454 | dev->cmd_type = PORT100_CMD_TYPE_1; |
| 1455 | else |
| 1456 | dev->cmd_type = PORT100_CMD_TYPE_0; |
| 1457 | |
| 1458 | rc = port100_set_command_type(dev, dev->cmd_type); |
| 1459 | if (rc) { |
| 1460 | nfc_err(&interface->dev, |
| 1461 | "The device does not support command type %u.\n", |
| 1462 | dev->cmd_type); |
| 1463 | goto error; |
| 1464 | } |
| 1465 | |
| 1466 | fw_version = port100_get_firmware_version(dev); |
| 1467 | if (!fw_version) |
| 1468 | nfc_err(&interface->dev, |
| 1469 | "Could not get device firmware version.\n"); |
| 1470 | |
| 1471 | nfc_info(&interface->dev, |
| 1472 | "Sony NFC Port-100 Series attached (firmware v%x.%02x)\n", |
| 1473 | (fw_version & 0xFF00) >> 8, fw_version & 0xFF); |
Thierry Escande | 562d4d5 | 2013-10-04 12:12:00 +0200 | [diff] [blame] | 1474 | |
| 1475 | dev->nfc_digital_dev = nfc_digital_allocate_device(&port100_digital_ops, |
| 1476 | PORT100_PROTOCOLS, |
| 1477 | PORT100_CAPABILITIES, |
| 1478 | dev->skb_headroom, |
| 1479 | dev->skb_tailroom); |
| 1480 | if (!dev->nfc_digital_dev) { |
| 1481 | nfc_err(&interface->dev, |
| 1482 | "Could not allocate nfc_digital_dev.\n"); |
| 1483 | rc = -ENOMEM; |
| 1484 | goto error; |
| 1485 | } |
| 1486 | |
| 1487 | nfc_digital_set_parent_dev(dev->nfc_digital_dev, &interface->dev); |
| 1488 | nfc_digital_set_drvdata(dev->nfc_digital_dev, dev); |
| 1489 | |
| 1490 | rc = nfc_digital_register_device(dev->nfc_digital_dev); |
| 1491 | if (rc) { |
| 1492 | nfc_err(&interface->dev, |
| 1493 | "Could not register digital device.\n"); |
| 1494 | goto free_nfc_dev; |
| 1495 | } |
| 1496 | |
| 1497 | return 0; |
| 1498 | |
| 1499 | free_nfc_dev: |
| 1500 | nfc_digital_free_device(dev->nfc_digital_dev); |
| 1501 | |
| 1502 | error: |
Thierry Escande | 0347a6a | 2013-10-04 12:12:01 +0200 | [diff] [blame] | 1503 | usb_free_urb(dev->in_urb); |
| 1504 | usb_free_urb(dev->out_urb); |
| 1505 | usb_put_dev(dev->udev); |
| 1506 | |
Thierry Escande | 562d4d5 | 2013-10-04 12:12:00 +0200 | [diff] [blame] | 1507 | return rc; |
| 1508 | } |
| 1509 | |
| 1510 | static void port100_disconnect(struct usb_interface *interface) |
| 1511 | { |
| 1512 | struct port100 *dev; |
| 1513 | |
| 1514 | dev = usb_get_intfdata(interface); |
| 1515 | usb_set_intfdata(interface, NULL); |
| 1516 | |
| 1517 | nfc_digital_unregister_device(dev->nfc_digital_dev); |
| 1518 | nfc_digital_free_device(dev->nfc_digital_dev); |
| 1519 | |
Thierry Escande | 0347a6a | 2013-10-04 12:12:01 +0200 | [diff] [blame] | 1520 | usb_kill_urb(dev->in_urb); |
| 1521 | usb_kill_urb(dev->out_urb); |
| 1522 | |
| 1523 | usb_free_urb(dev->in_urb); |
| 1524 | usb_free_urb(dev->out_urb); |
Alexey Khoroshilov | c36aeba | 2014-01-05 00:08:05 +0400 | [diff] [blame] | 1525 | usb_put_dev(dev->udev); |
Thierry Escande | 0347a6a | 2013-10-04 12:12:01 +0200 | [diff] [blame] | 1526 | |
| 1527 | kfree(dev->cmd); |
| 1528 | |
Thierry Escande | 562d4d5 | 2013-10-04 12:12:00 +0200 | [diff] [blame] | 1529 | nfc_info(&interface->dev, "Sony Port-100 NFC device disconnected"); |
| 1530 | } |
| 1531 | |
| 1532 | static struct usb_driver port100_driver = { |
| 1533 | .name = "port100", |
| 1534 | .probe = port100_probe, |
| 1535 | .disconnect = port100_disconnect, |
| 1536 | .id_table = port100_table, |
| 1537 | }; |
| 1538 | |
| 1539 | module_usb_driver(port100_driver); |
| 1540 | |
| 1541 | MODULE_DESCRIPTION("NFC Port-100 series usb driver ver " VERSION); |
| 1542 | MODULE_VERSION(VERSION); |
| 1543 | MODULE_LICENSE("GPL"); |