Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Instituto Nokia de Tecnologia |
Waldemar Rymarkiewicz | e70b96e | 2013-04-03 08:02:01 +0200 | [diff] [blame] | 3 | * Copyright (C) 2012-2013 Tieto Poland |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the |
| 17 | * Free Software Foundation, Inc., |
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/device.h> |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/usb.h> |
| 26 | #include <linux/nfc.h> |
| 27 | #include <linux/netdevice.h> |
Ilan Elias | 55eb94f | 2011-09-18 11:19:34 +0300 | [diff] [blame] | 28 | #include <net/nfc/nfc.h> |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 29 | |
Waldemar Rymarkiewicz | 495af72 | 2013-04-03 08:02:16 +0200 | [diff] [blame] | 30 | #define VERSION "0.2" |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 31 | |
| 32 | #define PN533_VENDOR_ID 0x4CC |
| 33 | #define PN533_PRODUCT_ID 0x2533 |
| 34 | |
| 35 | #define SCM_VENDOR_ID 0x4E6 |
| 36 | #define SCL3711_PRODUCT_ID 0x5591 |
| 37 | |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 38 | #define SONY_VENDOR_ID 0x054c |
| 39 | #define PASORI_PRODUCT_ID 0x02e1 |
| 40 | |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 41 | #define ACS_VENDOR_ID 0x072f |
| 42 | #define ACR122U_PRODUCT_ID 0x2200 |
| 43 | |
| 44 | #define PN533_DEVICE_STD 0x1 |
| 45 | #define PN533_DEVICE_PASORI 0x2 |
| 46 | #define PN533_DEVICE_ACR122U 0x3 |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 47 | |
Samuel Ortiz | 01d719a | 2012-07-04 00:14:04 +0200 | [diff] [blame] | 48 | #define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\ |
| 49 | NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\ |
| 50 | NFC_PROTO_NFC_DEP_MASK |\ |
| 51 | NFC_PROTO_ISO14443_B_MASK) |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 52 | |
| 53 | #define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \ |
| 54 | NFC_PROTO_MIFARE_MASK | \ |
| 55 | NFC_PROTO_FELICA_MASK | \ |
Samuel Ortiz | 01d719a | 2012-07-04 00:14:04 +0200 | [diff] [blame] | 56 | NFC_PROTO_ISO14443_MASK | \ |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 57 | NFC_PROTO_NFC_DEP_MASK) |
| 58 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 59 | static const struct usb_device_id pn533_table[] = { |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 60 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE, |
| 61 | .idVendor = PN533_VENDOR_ID, |
| 62 | .idProduct = PN533_PRODUCT_ID, |
| 63 | .driver_info = PN533_DEVICE_STD, |
| 64 | }, |
| 65 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE, |
| 66 | .idVendor = SCM_VENDOR_ID, |
| 67 | .idProduct = SCL3711_PRODUCT_ID, |
| 68 | .driver_info = PN533_DEVICE_STD, |
| 69 | }, |
| 70 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE, |
| 71 | .idVendor = SONY_VENDOR_ID, |
| 72 | .idProduct = PASORI_PRODUCT_ID, |
| 73 | .driver_info = PN533_DEVICE_PASORI, |
| 74 | }, |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 75 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE, |
| 76 | .idVendor = ACS_VENDOR_ID, |
| 77 | .idProduct = ACR122U_PRODUCT_ID, |
| 78 | .driver_info = PN533_DEVICE_ACR122U, |
| 79 | }, |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 80 | { } |
| 81 | }; |
| 82 | MODULE_DEVICE_TABLE(usb, pn533_table); |
| 83 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 84 | /* How much time we spend listening for initiators */ |
| 85 | #define PN533_LISTEN_TIME 2 |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 86 | /* Delay between each poll frame (ms) */ |
| 87 | #define PN533_POLL_INTERVAL 10 |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 88 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 89 | /* Standard pn533 frame definitions (standard and extended)*/ |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 90 | #define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \ |
Waldemar Rymarkiewicz | b1bb290 | 2012-11-26 14:18:32 +0100 | [diff] [blame] | 91 | + 2) /* data[0] TFI, data[1] CC */ |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 92 | #define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/ |
Waldemar Rymarkiewicz | 82dec34 | 2012-10-11 14:03:58 +0200 | [diff] [blame] | 93 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 94 | #define PN533_EXT_FRAME_HEADER_LEN (sizeof(struct pn533_ext_frame) \ |
| 95 | + 2) /* data[0] TFI, data[1] CC */ |
| 96 | |
| 97 | #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262 |
| 98 | #define PN533_CMD_DATAFRAME_MAXLEN 240 /* max data length (send) */ |
| 99 | |
Waldemar Rymarkiewicz | 15461ae | 2012-11-26 14:18:35 +0100 | [diff] [blame] | 100 | /* |
| 101 | * Max extended frame payload len, excluding TFI and CC |
| 102 | * which are already in PN533_FRAME_HEADER_LEN. |
| 103 | */ |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 104 | #define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263 |
Waldemar Rymarkiewicz | 15461ae | 2012-11-26 14:18:35 +0100 | [diff] [blame] | 105 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 106 | #define PN533_STD_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2), |
Waldemar Rymarkiewicz | 5b5a443 | 2012-12-10 14:42:56 +0100 | [diff] [blame] | 107 | Postamble (1) */ |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 108 | #define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen]) |
| 109 | #define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1]) |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 110 | /* Half start code (3), LEN (4) should be 0xffff for extended frame */ |
| 111 | #define PN533_STD_IS_EXTENDED(hdr) ((hdr)->datalen == 0xFF \ |
| 112 | && (hdr)->datalen_checksum == 0xFF) |
| 113 | #define PN533_EXT_FRAME_CHECKSUM(f) (f->data[be16_to_cpu(f->datalen)]) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 114 | |
| 115 | /* start of frame */ |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 116 | #define PN533_STD_FRAME_SOF 0x00FF |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 117 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 118 | /* standard frame identifier: in/out/error */ |
| 119 | #define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */ |
| 120 | #define PN533_STD_FRAME_DIR_OUT 0xD4 |
| 121 | #define PN533_STD_FRAME_DIR_IN 0xD5 |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 122 | |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 123 | /* ACS ACR122 pn533 frame definitions */ |
| 124 | #define PN533_ACR122_TX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_tx_frame) \ |
| 125 | + 2) |
| 126 | #define PN533_ACR122_TX_FRAME_TAIL_LEN 0 |
| 127 | #define PN533_ACR122_RX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_rx_frame) \ |
| 128 | + 2) |
| 129 | #define PN533_ACR122_RX_FRAME_TAIL_LEN 2 |
| 130 | #define PN533_ACR122_FRAME_MAX_PAYLOAD_LEN PN533_STD_FRAME_MAX_PAYLOAD_LEN |
| 131 | |
| 132 | /* CCID messages types */ |
| 133 | #define PN533_ACR122_PC_TO_RDR_ICCPOWERON 0x62 |
| 134 | #define PN533_ACR122_PC_TO_RDR_ESCAPE 0x6B |
| 135 | |
| 136 | #define PN533_ACR122_RDR_TO_PC_ESCAPE 0x83 |
| 137 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 138 | /* PN533 Commands */ |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 139 | #define PN533_FRAME_CMD(f) (f->data[1]) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 140 | |
| 141 | #define PN533_CMD_GET_FIRMWARE_VERSION 0x02 |
| 142 | #define PN533_CMD_RF_CONFIGURATION 0x32 |
| 143 | #define PN533_CMD_IN_DATA_EXCHANGE 0x40 |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 144 | #define PN533_CMD_IN_COMM_THRU 0x42 |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 145 | #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A |
| 146 | #define PN533_CMD_IN_ATR 0x50 |
| 147 | #define PN533_CMD_IN_RELEASE 0x52 |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 148 | #define PN533_CMD_IN_JUMP_FOR_DEP 0x56 |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 149 | |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 150 | #define PN533_CMD_TG_INIT_AS_TARGET 0x8c |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 151 | #define PN533_CMD_TG_GET_DATA 0x86 |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 152 | #define PN533_CMD_TG_SET_DATA 0x8e |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 153 | #define PN533_CMD_TG_SET_META_DATA 0x94 |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 154 | #define PN533_CMD_UNDEF 0xff |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 155 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 156 | #define PN533_CMD_RESPONSE(cmd) (cmd + 1) |
| 157 | |
| 158 | /* PN533 Return codes */ |
| 159 | #define PN533_CMD_RET_MASK 0x3F |
| 160 | #define PN533_CMD_MI_MASK 0x40 |
| 161 | #define PN533_CMD_RET_SUCCESS 0x00 |
| 162 | |
| 163 | struct pn533; |
| 164 | |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 165 | typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg, |
| 166 | struct sk_buff *resp); |
| 167 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 168 | /* structs for pn533 commands */ |
| 169 | |
| 170 | /* PN533_CMD_GET_FIRMWARE_VERSION */ |
| 171 | struct pn533_fw_version { |
| 172 | u8 ic; |
| 173 | u8 ver; |
| 174 | u8 rev; |
| 175 | u8 support; |
| 176 | }; |
| 177 | |
| 178 | /* PN533_CMD_RF_CONFIGURATION */ |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 179 | #define PN533_CFGITEM_RF_FIELD 0x01 |
| 180 | #define PN533_CFGITEM_TIMING 0x02 |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 181 | #define PN533_CFGITEM_MAX_RETRIES 0x05 |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 182 | #define PN533_CFGITEM_PASORI 0x82 |
| 183 | |
Samuel Ortiz | 3a8eab3 | 2013-07-01 17:26:58 +0200 | [diff] [blame] | 184 | #define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2 |
| 185 | #define PN533_CFGITEM_RF_FIELD_ON 0x1 |
| 186 | #define PN533_CFGITEM_RF_FIELD_OFF 0x0 |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 187 | |
Samuel Ortiz | 34a85bf | 2012-05-29 21:34:08 +0200 | [diff] [blame] | 188 | #define PN533_CONFIG_TIMING_102 0xb |
| 189 | #define PN533_CONFIG_TIMING_204 0xc |
| 190 | #define PN533_CONFIG_TIMING_409 0xd |
| 191 | #define PN533_CONFIG_TIMING_819 0xe |
| 192 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 193 | #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00 |
| 194 | #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF |
| 195 | |
| 196 | struct pn533_config_max_retries { |
| 197 | u8 mx_rty_atr; |
| 198 | u8 mx_rty_psl; |
| 199 | u8 mx_rty_passive_act; |
| 200 | } __packed; |
| 201 | |
Samuel Ortiz | 34a85bf | 2012-05-29 21:34:08 +0200 | [diff] [blame] | 202 | struct pn533_config_timing { |
| 203 | u8 rfu; |
| 204 | u8 atr_res_timeout; |
| 205 | u8 dep_timeout; |
| 206 | } __packed; |
| 207 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 208 | /* PN533_CMD_IN_LIST_PASSIVE_TARGET */ |
| 209 | |
| 210 | /* felica commands opcode */ |
| 211 | #define PN533_FELICA_OPC_SENSF_REQ 0 |
| 212 | #define PN533_FELICA_OPC_SENSF_RES 1 |
| 213 | /* felica SENSF_REQ parameters */ |
| 214 | #define PN533_FELICA_SENSF_SC_ALL 0xFFFF |
| 215 | #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0 |
| 216 | #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1 |
| 217 | #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2 |
| 218 | |
| 219 | /* type B initiator_data values */ |
| 220 | #define PN533_TYPE_B_AFI_ALL_FAMILIES 0 |
| 221 | #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0 |
| 222 | #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1 |
| 223 | |
| 224 | union pn533_cmd_poll_initdata { |
| 225 | struct { |
| 226 | u8 afi; |
| 227 | u8 polling_method; |
| 228 | } __packed type_b; |
| 229 | struct { |
| 230 | u8 opcode; |
| 231 | __be16 sc; |
| 232 | u8 rc; |
| 233 | u8 tsn; |
| 234 | } __packed felica; |
| 235 | }; |
| 236 | |
| 237 | /* Poll modulations */ |
| 238 | enum { |
| 239 | PN533_POLL_MOD_106KBPS_A, |
| 240 | PN533_POLL_MOD_212KBPS_FELICA, |
| 241 | PN533_POLL_MOD_424KBPS_FELICA, |
| 242 | PN533_POLL_MOD_106KBPS_JEWEL, |
| 243 | PN533_POLL_MOD_847KBPS_B, |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 244 | PN533_LISTEN_MOD, |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 245 | |
| 246 | __PN533_POLL_MOD_AFTER_LAST, |
| 247 | }; |
| 248 | #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1) |
| 249 | |
| 250 | struct pn533_poll_modulations { |
| 251 | struct { |
| 252 | u8 maxtg; |
| 253 | u8 brty; |
| 254 | union pn533_cmd_poll_initdata initiator_data; |
| 255 | } __packed data; |
| 256 | u8 len; |
| 257 | }; |
| 258 | |
Waldemar Rymarkiewicz | ef3d56e | 2013-01-16 15:51:51 +0100 | [diff] [blame] | 259 | static const struct pn533_poll_modulations poll_mod[] = { |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 260 | [PN533_POLL_MOD_106KBPS_A] = { |
| 261 | .data = { |
| 262 | .maxtg = 1, |
| 263 | .brty = 0, |
| 264 | }, |
| 265 | .len = 2, |
| 266 | }, |
| 267 | [PN533_POLL_MOD_212KBPS_FELICA] = { |
| 268 | .data = { |
| 269 | .maxtg = 1, |
| 270 | .brty = 1, |
| 271 | .initiator_data.felica = { |
| 272 | .opcode = PN533_FELICA_OPC_SENSF_REQ, |
| 273 | .sc = PN533_FELICA_SENSF_SC_ALL, |
Samuel Ortiz | a94e10f | 2013-06-28 15:43:19 +0200 | [diff] [blame] | 274 | .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE, |
Samuel Ortiz | 31c4446 | 2013-05-27 14:59:40 +0200 | [diff] [blame] | 275 | .tsn = 0x03, |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 276 | }, |
| 277 | }, |
| 278 | .len = 7, |
| 279 | }, |
| 280 | [PN533_POLL_MOD_424KBPS_FELICA] = { |
| 281 | .data = { |
| 282 | .maxtg = 1, |
| 283 | .brty = 2, |
| 284 | .initiator_data.felica = { |
| 285 | .opcode = PN533_FELICA_OPC_SENSF_REQ, |
| 286 | .sc = PN533_FELICA_SENSF_SC_ALL, |
Samuel Ortiz | a94e10f | 2013-06-28 15:43:19 +0200 | [diff] [blame] | 287 | .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE, |
Samuel Ortiz | 31c4446 | 2013-05-27 14:59:40 +0200 | [diff] [blame] | 288 | .tsn = 0x03, |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 289 | }, |
| 290 | }, |
| 291 | .len = 7, |
| 292 | }, |
| 293 | [PN533_POLL_MOD_106KBPS_JEWEL] = { |
| 294 | .data = { |
| 295 | .maxtg = 1, |
| 296 | .brty = 4, |
| 297 | }, |
| 298 | .len = 2, |
| 299 | }, |
| 300 | [PN533_POLL_MOD_847KBPS_B] = { |
| 301 | .data = { |
| 302 | .maxtg = 1, |
| 303 | .brty = 8, |
| 304 | .initiator_data.type_b = { |
| 305 | .afi = PN533_TYPE_B_AFI_ALL_FAMILIES, |
| 306 | .polling_method = |
| 307 | PN533_TYPE_B_POLL_METHOD_TIMESLOT, |
| 308 | }, |
| 309 | }, |
| 310 | .len = 3, |
| 311 | }, |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 312 | [PN533_LISTEN_MOD] = { |
| 313 | .len = 0, |
| 314 | }, |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 315 | }; |
| 316 | |
| 317 | /* PN533_CMD_IN_ATR */ |
| 318 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 319 | struct pn533_cmd_activate_response { |
| 320 | u8 status; |
| 321 | u8 nfcid3t[10]; |
| 322 | u8 didt; |
| 323 | u8 bst; |
| 324 | u8 brt; |
| 325 | u8 to; |
| 326 | u8 ppt; |
| 327 | /* optional */ |
| 328 | u8 gt[]; |
| 329 | } __packed; |
| 330 | |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 331 | struct pn533_cmd_jump_dep_response { |
| 332 | u8 status; |
| 333 | u8 tg; |
| 334 | u8 nfcid3t[10]; |
| 335 | u8 didt; |
| 336 | u8 bst; |
| 337 | u8 brt; |
| 338 | u8 to; |
| 339 | u8 ppt; |
| 340 | /* optional */ |
| 341 | u8 gt[]; |
| 342 | } __packed; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 343 | |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 344 | |
| 345 | /* PN533_TG_INIT_AS_TARGET */ |
| 346 | #define PN533_INIT_TARGET_PASSIVE 0x1 |
| 347 | #define PN533_INIT_TARGET_DEP 0x2 |
| 348 | |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 349 | #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3 |
| 350 | #define PN533_INIT_TARGET_RESP_ACTIVE 0x1 |
| 351 | #define PN533_INIT_TARGET_RESP_DEP 0x4 |
| 352 | |
Waldemar Rymarkiewicz | 5852037 | 2013-04-03 08:02:12 +0200 | [diff] [blame] | 353 | enum pn533_protocol_type { |
| 354 | PN533_PROTO_REQ_ACK_RESP = 0, |
| 355 | PN533_PROTO_REQ_RESP |
| 356 | }; |
| 357 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 358 | struct pn533 { |
| 359 | struct usb_device *udev; |
| 360 | struct usb_interface *interface; |
| 361 | struct nfc_dev *nfc_dev; |
Waldemar Rymarkiewicz | d5590bb | 2013-04-03 08:02:10 +0200 | [diff] [blame] | 362 | u32 device_type; |
Waldemar Rymarkiewicz | 5852037 | 2013-04-03 08:02:12 +0200 | [diff] [blame] | 363 | enum pn533_protocol_type protocol_type; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 364 | |
| 365 | struct urb *out_urb; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 366 | struct urb *in_urb; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 367 | |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 368 | struct sk_buff_head resp_q; |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 369 | struct sk_buff_head fragment_skb; |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 370 | |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 371 | struct workqueue_struct *wq; |
| 372 | struct work_struct cmd_work; |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 373 | struct work_struct cmd_complete_work; |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 374 | struct delayed_work poll_work; |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 375 | struct work_struct mi_rx_work; |
| 376 | struct work_struct mi_tx_work; |
Olivier Guiter | 3c13b24 | 2013-09-23 12:24:38 +0200 | [diff] [blame] | 377 | struct work_struct mi_tm_rx_work; |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 378 | struct work_struct mi_tm_tx_work; |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 379 | struct work_struct tg_work; |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 380 | struct work_struct rf_work; |
Waldemar Rymarkiewicz | d5590bb | 2013-04-03 08:02:10 +0200 | [diff] [blame] | 381 | |
| 382 | struct list_head cmd_queue; |
| 383 | struct pn533_cmd *cmd; |
| 384 | u8 cmd_pending; |
Waldemar Rymarkiewicz | d5590bb | 2013-04-03 08:02:10 +0200 | [diff] [blame] | 385 | struct mutex cmd_lock; /* protects cmd queue */ |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 386 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 387 | void *cmd_complete_mi_arg; |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 388 | void *cmd_complete_dep_arg; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 389 | |
| 390 | struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1]; |
| 391 | u8 poll_mod_count; |
| 392 | u8 poll_mod_curr; |
Samuel Ortiz | 673088f | 2013-08-21 15:06:55 +0200 | [diff] [blame] | 393 | u8 poll_dep; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 394 | u32 poll_protocols; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 395 | u32 listen_protocols; |
Waldemar Rymarkiewicz | d5590bb | 2013-04-03 08:02:10 +0200 | [diff] [blame] | 396 | struct timer_list listen_timer; |
| 397 | int cancel_listen; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 398 | |
| 399 | u8 *gb; |
| 400 | size_t gb_len; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 401 | |
| 402 | u8 tgt_available_prots; |
| 403 | u8 tgt_active_prot; |
Samuel Ortiz | 51ad304 | 2012-05-31 20:01:32 +0200 | [diff] [blame] | 404 | u8 tgt_mode; |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 405 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 406 | struct pn533_frame_ops *ops; |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 407 | }; |
| 408 | |
| 409 | struct pn533_cmd { |
| 410 | struct list_head queue; |
Waldemar Rymarkiewicz | 4b2a953 | 2013-04-03 08:02:06 +0200 | [diff] [blame] | 411 | u8 code; |
Waldemar Rymarkiewicz | f87bc9f | 2013-04-03 08:02:11 +0200 | [diff] [blame] | 412 | int status; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 413 | struct sk_buff *req; |
| 414 | struct sk_buff *resp; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 415 | int resp_len; |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 416 | pn533_send_async_complete_t complete_cb; |
| 417 | void *complete_cb_context; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 418 | }; |
| 419 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 420 | struct pn533_std_frame { |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 421 | u8 preamble; |
| 422 | __be16 start_frame; |
| 423 | u8 datalen; |
| 424 | u8 datalen_checksum; |
| 425 | u8 data[]; |
| 426 | } __packed; |
| 427 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 428 | struct pn533_ext_frame { /* Extended Information frame */ |
| 429 | u8 preamble; |
| 430 | __be16 start_frame; |
| 431 | __be16 eif_flag; /* fixed to 0xFFFF */ |
| 432 | __be16 datalen; |
| 433 | u8 datalen_checksum; |
| 434 | u8 data[]; |
| 435 | } __packed; |
| 436 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 437 | struct pn533_frame_ops { |
| 438 | void (*tx_frame_init)(void *frame, u8 cmd_code); |
| 439 | void (*tx_frame_finish)(void *frame); |
| 440 | void (*tx_update_payload_len)(void *frame, int len); |
| 441 | int tx_header_len; |
| 442 | int tx_tail_len; |
| 443 | |
Olivier Guiter | 56a63c82 | 2013-06-13 13:43:29 +0000 | [diff] [blame] | 444 | bool (*rx_is_frame_valid)(void *frame, struct pn533 *dev); |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 445 | int (*rx_frame_size)(void *frame); |
| 446 | int rx_header_len; |
| 447 | int rx_tail_len; |
| 448 | |
| 449 | int max_payload_len; |
| 450 | u8 (*get_cmd_code)(void *frame); |
| 451 | }; |
| 452 | |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 453 | struct pn533_acr122_ccid_hdr { |
| 454 | u8 type; |
| 455 | u32 datalen; |
| 456 | u8 slot; |
| 457 | u8 seq; |
| 458 | u8 params[3]; /* 3 msg specific bytes or status, error and 1 specific |
| 459 | byte for reposnse msg */ |
| 460 | u8 data[]; /* payload */ |
| 461 | } __packed; |
| 462 | |
| 463 | struct pn533_acr122_apdu_hdr { |
| 464 | u8 class; |
| 465 | u8 ins; |
| 466 | u8 p1; |
| 467 | u8 p2; |
| 468 | } __packed; |
| 469 | |
| 470 | struct pn533_acr122_tx_frame { |
| 471 | struct pn533_acr122_ccid_hdr ccid; |
| 472 | struct pn533_acr122_apdu_hdr apdu; |
| 473 | u8 datalen; |
| 474 | u8 data[]; /* pn533 frame: TFI ... */ |
| 475 | } __packed; |
| 476 | |
| 477 | struct pn533_acr122_rx_frame { |
| 478 | struct pn533_acr122_ccid_hdr ccid; |
| 479 | u8 data[]; /* pn533 frame : TFI ... */ |
| 480 | } __packed; |
| 481 | |
| 482 | static void pn533_acr122_tx_frame_init(void *_frame, u8 cmd_code) |
| 483 | { |
| 484 | struct pn533_acr122_tx_frame *frame = _frame; |
| 485 | |
| 486 | frame->ccid.type = PN533_ACR122_PC_TO_RDR_ESCAPE; |
| 487 | frame->ccid.datalen = sizeof(frame->apdu) + 1; /* sizeof(apdu_hdr) + |
| 488 | sizeof(datalen) */ |
| 489 | frame->ccid.slot = 0; |
| 490 | frame->ccid.seq = 0; |
| 491 | frame->ccid.params[0] = 0; |
| 492 | frame->ccid.params[1] = 0; |
| 493 | frame->ccid.params[2] = 0; |
| 494 | |
| 495 | frame->data[0] = PN533_STD_FRAME_DIR_OUT; |
| 496 | frame->data[1] = cmd_code; |
| 497 | frame->datalen = 2; /* data[0] + data[1] */ |
| 498 | |
| 499 | frame->apdu.class = 0xFF; |
| 500 | frame->apdu.ins = 0; |
| 501 | frame->apdu.p1 = 0; |
| 502 | frame->apdu.p2 = 0; |
| 503 | } |
| 504 | |
| 505 | static void pn533_acr122_tx_frame_finish(void *_frame) |
| 506 | { |
| 507 | struct pn533_acr122_tx_frame *frame = _frame; |
| 508 | |
| 509 | frame->ccid.datalen += frame->datalen; |
| 510 | } |
| 511 | |
| 512 | static void pn533_acr122_tx_update_payload_len(void *_frame, int len) |
| 513 | { |
| 514 | struct pn533_acr122_tx_frame *frame = _frame; |
| 515 | |
| 516 | frame->datalen += len; |
| 517 | } |
| 518 | |
Olivier Guiter | 56a63c82 | 2013-06-13 13:43:29 +0000 | [diff] [blame] | 519 | static bool pn533_acr122_is_rx_frame_valid(void *_frame, struct pn533 *dev) |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 520 | { |
| 521 | struct pn533_acr122_rx_frame *frame = _frame; |
| 522 | |
| 523 | if (frame->ccid.type != 0x83) |
| 524 | return false; |
| 525 | |
| 526 | if (frame->data[frame->ccid.datalen - 2] == 0x63) |
| 527 | return false; |
| 528 | |
| 529 | return true; |
| 530 | } |
| 531 | |
| 532 | static int pn533_acr122_rx_frame_size(void *frame) |
| 533 | { |
| 534 | struct pn533_acr122_rx_frame *f = frame; |
| 535 | |
| 536 | /* f->ccid.datalen already includes tail length */ |
| 537 | return sizeof(struct pn533_acr122_rx_frame) + f->ccid.datalen; |
| 538 | } |
| 539 | |
| 540 | static u8 pn533_acr122_get_cmd_code(void *frame) |
| 541 | { |
| 542 | struct pn533_acr122_rx_frame *f = frame; |
| 543 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 544 | return PN533_FRAME_CMD(f); |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | static struct pn533_frame_ops pn533_acr122_frame_ops = { |
| 548 | .tx_frame_init = pn533_acr122_tx_frame_init, |
| 549 | .tx_frame_finish = pn533_acr122_tx_frame_finish, |
| 550 | .tx_update_payload_len = pn533_acr122_tx_update_payload_len, |
| 551 | .tx_header_len = PN533_ACR122_TX_FRAME_HEADER_LEN, |
| 552 | .tx_tail_len = PN533_ACR122_TX_FRAME_TAIL_LEN, |
| 553 | |
| 554 | .rx_is_frame_valid = pn533_acr122_is_rx_frame_valid, |
| 555 | .rx_header_len = PN533_ACR122_RX_FRAME_HEADER_LEN, |
| 556 | .rx_tail_len = PN533_ACR122_RX_FRAME_TAIL_LEN, |
| 557 | .rx_frame_size = pn533_acr122_rx_frame_size, |
| 558 | |
| 559 | .max_payload_len = PN533_ACR122_FRAME_MAX_PAYLOAD_LEN, |
| 560 | .get_cmd_code = pn533_acr122_get_cmd_code, |
| 561 | }; |
| 562 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 563 | /* The rule: value(high byte) + value(low byte) + checksum = 0 */ |
| 564 | static inline u8 pn533_ext_checksum(u16 value) |
| 565 | { |
| 566 | return ~(u8)(((value & 0xFF00) >> 8) + (u8)(value & 0xFF)) + 1; |
| 567 | } |
| 568 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 569 | /* The rule: value + checksum = 0 */ |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 570 | static inline u8 pn533_std_checksum(u8 value) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 571 | { |
| 572 | return ~value + 1; |
| 573 | } |
| 574 | |
| 575 | /* The rule: sum(data elements) + checksum = 0 */ |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 576 | static u8 pn533_std_data_checksum(u8 *data, int datalen) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 577 | { |
| 578 | u8 sum = 0; |
| 579 | int i; |
| 580 | |
| 581 | for (i = 0; i < datalen; i++) |
| 582 | sum += data[i]; |
| 583 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 584 | return pn533_std_checksum(sum); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 585 | } |
| 586 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 587 | static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 588 | { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 589 | struct pn533_std_frame *frame = _frame; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 590 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 591 | frame->preamble = 0; |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 592 | frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF); |
| 593 | PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT; |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 594 | PN533_FRAME_CMD(frame) = cmd_code; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 595 | frame->datalen = 2; |
| 596 | } |
| 597 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 598 | static void pn533_std_tx_frame_finish(void *_frame) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 599 | { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 600 | struct pn533_std_frame *frame = _frame; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 601 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 602 | frame->datalen_checksum = pn533_std_checksum(frame->datalen); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 603 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 604 | PN533_STD_FRAME_CHECKSUM(frame) = |
| 605 | pn533_std_data_checksum(frame->data, frame->datalen); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 606 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 607 | PN533_STD_FRAME_POSTAMBLE(frame) = 0; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 608 | } |
| 609 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 610 | static void pn533_std_tx_update_payload_len(void *_frame, int len) |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 611 | { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 612 | struct pn533_std_frame *frame = _frame; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 613 | |
| 614 | frame->datalen += len; |
| 615 | } |
| 616 | |
Olivier Guiter | 56a63c82 | 2013-06-13 13:43:29 +0000 | [diff] [blame] | 617 | static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 618 | { |
| 619 | u8 checksum; |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 620 | struct pn533_std_frame *stdf = _frame; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 621 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 622 | if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 623 | return false; |
| 624 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 625 | if (likely(!PN533_STD_IS_EXTENDED(stdf))) { |
| 626 | /* Standard frame code */ |
Olivier Guiter | 56a63c82 | 2013-06-13 13:43:29 +0000 | [diff] [blame] | 627 | dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 628 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 629 | checksum = pn533_std_checksum(stdf->datalen); |
| 630 | if (checksum != stdf->datalen_checksum) |
| 631 | return false; |
| 632 | |
| 633 | checksum = pn533_std_data_checksum(stdf->data, stdf->datalen); |
| 634 | if (checksum != PN533_STD_FRAME_CHECKSUM(stdf)) |
| 635 | return false; |
| 636 | } else { |
| 637 | /* Extended */ |
| 638 | struct pn533_ext_frame *eif = _frame; |
| 639 | |
Olivier Guiter | 56a63c82 | 2013-06-13 13:43:29 +0000 | [diff] [blame] | 640 | dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN; |
| 641 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 642 | checksum = pn533_ext_checksum(be16_to_cpu(eif->datalen)); |
| 643 | if (checksum != eif->datalen_checksum) |
| 644 | return false; |
| 645 | |
| 646 | /* check data checksum */ |
| 647 | checksum = pn533_std_data_checksum(eif->data, |
| 648 | be16_to_cpu(eif->datalen)); |
| 649 | if (checksum != PN533_EXT_FRAME_CHECKSUM(eif)) |
| 650 | return false; |
| 651 | } |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 652 | |
| 653 | return true; |
| 654 | } |
| 655 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 656 | static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 657 | { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 658 | if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 659 | return false; |
| 660 | |
| 661 | if (frame->datalen != 0 || frame->datalen_checksum != 0xFF) |
| 662 | return false; |
| 663 | |
| 664 | return true; |
| 665 | } |
| 666 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 667 | static inline int pn533_std_rx_frame_size(void *frame) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 668 | { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 669 | struct pn533_std_frame *f = frame; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 670 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 671 | /* check for Extended Information frame */ |
| 672 | if (PN533_STD_IS_EXTENDED(f)) { |
| 673 | struct pn533_ext_frame *eif = frame; |
| 674 | |
| 675 | return sizeof(struct pn533_ext_frame) |
| 676 | + be16_to_cpu(eif->datalen) + PN533_STD_FRAME_TAIL_LEN; |
| 677 | } |
| 678 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 679 | return sizeof(struct pn533_std_frame) + f->datalen + |
| 680 | PN533_STD_FRAME_TAIL_LEN; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 681 | } |
| 682 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 683 | static u8 pn533_std_get_cmd_code(void *frame) |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 684 | { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 685 | struct pn533_std_frame *f = frame; |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 686 | struct pn533_ext_frame *eif = frame; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 687 | |
Olivier Guiter | 1575b9d | 2013-06-13 15:43:27 +0200 | [diff] [blame] | 688 | if (PN533_STD_IS_EXTENDED(f)) |
| 689 | return PN533_FRAME_CMD(eif); |
| 690 | else |
| 691 | return PN533_FRAME_CMD(f); |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 692 | } |
| 693 | |
Waldemar Rymarkiewicz | ef3d56e | 2013-01-16 15:51:51 +0100 | [diff] [blame] | 694 | static struct pn533_frame_ops pn533_std_frame_ops = { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 695 | .tx_frame_init = pn533_std_tx_frame_init, |
| 696 | .tx_frame_finish = pn533_std_tx_frame_finish, |
| 697 | .tx_update_payload_len = pn533_std_tx_update_payload_len, |
| 698 | .tx_header_len = PN533_STD_FRAME_HEADER_LEN, |
| 699 | .tx_tail_len = PN533_STD_FRAME_TAIL_LEN, |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 700 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 701 | .rx_is_frame_valid = pn533_std_rx_frame_is_valid, |
| 702 | .rx_frame_size = pn533_std_rx_frame_size, |
| 703 | .rx_header_len = PN533_STD_FRAME_HEADER_LEN, |
| 704 | .rx_tail_len = PN533_STD_FRAME_TAIL_LEN, |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 705 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 706 | .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN, |
| 707 | .get_cmd_code = pn533_std_get_cmd_code, |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 708 | }; |
| 709 | |
| 710 | static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame) |
| 711 | { |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 712 | return (dev->ops->get_cmd_code(frame) == |
Waldemar Rymarkiewicz | 4b2a953 | 2013-04-03 08:02:06 +0200 | [diff] [blame] | 713 | PN533_CMD_RESPONSE(dev->cmd->code)); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 714 | } |
| 715 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 716 | static void pn533_recv_response(struct urb *urb) |
| 717 | { |
| 718 | struct pn533 *dev = urb->context; |
Waldemar Rymarkiewicz | f87bc9f | 2013-04-03 08:02:11 +0200 | [diff] [blame] | 719 | struct pn533_cmd *cmd = dev->cmd; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 720 | u8 *in_frame; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 721 | |
Waldemar Rymarkiewicz | f87bc9f | 2013-04-03 08:02:11 +0200 | [diff] [blame] | 722 | cmd->status = urb->status; |
| 723 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 724 | switch (urb->status) { |
| 725 | case 0: |
Waldemar Rymarkiewicz | f8f9917 | 2012-12-10 14:42:54 +0100 | [diff] [blame] | 726 | break; /* success */ |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 727 | case -ECONNRESET: |
| 728 | case -ENOENT: |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 729 | dev_dbg(&dev->interface->dev, |
| 730 | "The urb has been canceled (status %d)\n", |
| 731 | urb->status); |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 732 | goto sched_wq; |
Waldemar Rymarkiewicz | f8f9917 | 2012-12-10 14:42:54 +0100 | [diff] [blame] | 733 | case -ESHUTDOWN: |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 734 | default: |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 735 | nfc_err(&dev->interface->dev, |
| 736 | "Urb failure (status %d)\n", urb->status); |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 737 | goto sched_wq; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | in_frame = dev->in_urb->transfer_buffer; |
| 741 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 742 | dev_dbg(&dev->interface->dev, "Received a frame\n"); |
Samuel Ortiz | e279f84 | 2013-03-20 11:27:57 +0100 | [diff] [blame] | 743 | print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame, |
| 744 | dev->ops->rx_frame_size(in_frame), false); |
Waldemar Rymarkiewicz | 99e591b | 2012-12-10 14:42:47 +0100 | [diff] [blame] | 745 | |
Olivier Guiter | 56a63c82 | 2013-06-13 13:43:29 +0000 | [diff] [blame] | 746 | if (!dev->ops->rx_is_frame_valid(in_frame, dev)) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 747 | nfc_err(&dev->interface->dev, "Received an invalid frame\n"); |
Waldemar Rymarkiewicz | f87bc9f | 2013-04-03 08:02:11 +0200 | [diff] [blame] | 748 | cmd->status = -EIO; |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 749 | goto sched_wq; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 750 | } |
| 751 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 752 | if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 753 | nfc_err(&dev->interface->dev, |
| 754 | "It it not the response to the last command\n"); |
Waldemar Rymarkiewicz | f87bc9f | 2013-04-03 08:02:11 +0200 | [diff] [blame] | 755 | cmd->status = -EIO; |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 756 | goto sched_wq; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 757 | } |
| 758 | |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 759 | sched_wq: |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 760 | queue_work(dev->wq, &dev->cmd_complete_work); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags) |
| 764 | { |
| 765 | dev->in_urb->complete = pn533_recv_response; |
| 766 | |
| 767 | return usb_submit_urb(dev->in_urb, flags); |
| 768 | } |
| 769 | |
| 770 | static void pn533_recv_ack(struct urb *urb) |
| 771 | { |
| 772 | struct pn533 *dev = urb->context; |
Waldemar Rymarkiewicz | f87bc9f | 2013-04-03 08:02:11 +0200 | [diff] [blame] | 773 | struct pn533_cmd *cmd = dev->cmd; |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 774 | struct pn533_std_frame *in_frame; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 775 | int rc; |
| 776 | |
Waldemar Rymarkiewicz | f87bc9f | 2013-04-03 08:02:11 +0200 | [diff] [blame] | 777 | cmd->status = urb->status; |
| 778 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 779 | switch (urb->status) { |
| 780 | case 0: |
Waldemar Rymarkiewicz | f8f9917 | 2012-12-10 14:42:54 +0100 | [diff] [blame] | 781 | break; /* success */ |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 782 | case -ECONNRESET: |
| 783 | case -ENOENT: |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 784 | dev_dbg(&dev->interface->dev, |
| 785 | "The urb has been stopped (status %d)\n", |
| 786 | urb->status); |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 787 | goto sched_wq; |
Waldemar Rymarkiewicz | f8f9917 | 2012-12-10 14:42:54 +0100 | [diff] [blame] | 788 | case -ESHUTDOWN: |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 789 | default: |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 790 | nfc_err(&dev->interface->dev, |
| 791 | "Urb failure (status %d)\n", urb->status); |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 792 | goto sched_wq; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | in_frame = dev->in_urb->transfer_buffer; |
| 796 | |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 797 | if (!pn533_std_rx_frame_is_ack(in_frame)) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 798 | nfc_err(&dev->interface->dev, "Received an invalid ack\n"); |
Waldemar Rymarkiewicz | f87bc9f | 2013-04-03 08:02:11 +0200 | [diff] [blame] | 799 | cmd->status = -EIO; |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 800 | goto sched_wq; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 801 | } |
| 802 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 803 | rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC); |
| 804 | if (rc) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 805 | nfc_err(&dev->interface->dev, |
| 806 | "usb_submit_urb failed with result %d\n", rc); |
Waldemar Rymarkiewicz | f87bc9f | 2013-04-03 08:02:11 +0200 | [diff] [blame] | 807 | cmd->status = rc; |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 808 | goto sched_wq; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | return; |
| 812 | |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 813 | sched_wq: |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 814 | queue_work(dev->wq, &dev->cmd_complete_work); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags) |
| 818 | { |
| 819 | dev->in_urb->complete = pn533_recv_ack; |
| 820 | |
| 821 | return usb_submit_urb(dev->in_urb, flags); |
| 822 | } |
| 823 | |
| 824 | static int pn533_send_ack(struct pn533 *dev, gfp_t flags) |
| 825 | { |
Waldemar Rymarkiewicz | 6312310 | 2013-04-03 08:01:58 +0200 | [diff] [blame] | 826 | u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00}; |
Waldemar Rymarkiewicz | 5b5a443 | 2012-12-10 14:42:56 +0100 | [diff] [blame] | 827 | /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */ |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 828 | int rc; |
| 829 | |
Waldemar Rymarkiewicz | 5b5a443 | 2012-12-10 14:42:56 +0100 | [diff] [blame] | 830 | dev->out_urb->transfer_buffer = ack; |
| 831 | dev->out_urb->transfer_buffer_length = sizeof(ack); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 832 | rc = usb_submit_urb(dev->out_urb, flags); |
| 833 | |
| 834 | return rc; |
| 835 | } |
| 836 | |
Waldemar Rymarkiewicz | e8f4053 | 2012-12-10 14:42:57 +0100 | [diff] [blame] | 837 | static int __pn533_send_frame_async(struct pn533 *dev, |
| 838 | struct sk_buff *out, |
| 839 | struct sk_buff *in, |
Waldemar Rymarkiewicz | ddf19d2 | 2013-04-03 08:02:08 +0200 | [diff] [blame] | 840 | int in_len) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 841 | { |
| 842 | int rc; |
| 843 | |
Waldemar Rymarkiewicz | e8f4053 | 2012-12-10 14:42:57 +0100 | [diff] [blame] | 844 | dev->out_urb->transfer_buffer = out->data; |
| 845 | dev->out_urb->transfer_buffer_length = out->len; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 846 | |
Waldemar Rymarkiewicz | e8f4053 | 2012-12-10 14:42:57 +0100 | [diff] [blame] | 847 | dev->in_urb->transfer_buffer = in->data; |
| 848 | dev->in_urb->transfer_buffer_length = in_len; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 849 | |
Samuel Ortiz | e279f84 | 2013-03-20 11:27:57 +0100 | [diff] [blame] | 850 | print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1, |
| 851 | out->data, out->len, false); |
Waldemar Rymarkiewicz | 99e591b | 2012-12-10 14:42:47 +0100 | [diff] [blame] | 852 | |
Waldemar Rymarkiewicz | d94ea4f | 2012-11-26 14:18:33 +0100 | [diff] [blame] | 853 | rc = usb_submit_urb(dev->out_urb, GFP_KERNEL); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 854 | if (rc) |
| 855 | return rc; |
| 856 | |
Waldemar Rymarkiewicz | 5852037 | 2013-04-03 08:02:12 +0200 | [diff] [blame] | 857 | if (dev->protocol_type == PN533_PROTO_REQ_RESP) { |
| 858 | /* request for response for sent packet directly */ |
| 859 | rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC); |
| 860 | if (rc) |
| 861 | goto error; |
| 862 | } else if (dev->protocol_type == PN533_PROTO_REQ_ACK_RESP) { |
| 863 | /* request for ACK if that's the case */ |
| 864 | rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL); |
| 865 | if (rc) |
| 866 | goto error; |
| 867 | } |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 868 | |
| 869 | return 0; |
| 870 | |
| 871 | error: |
| 872 | usb_unlink_urb(dev->out_urb); |
| 873 | return rc; |
| 874 | } |
| 875 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 876 | static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code, |
| 877 | struct sk_buff *skb) |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 878 | { |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 879 | /* payload is already there, just update datalen */ |
| 880 | int payload_len = skb->len; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 881 | struct pn533_frame_ops *ops = dev->ops; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 882 | |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 883 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 884 | skb_push(skb, ops->tx_header_len); |
| 885 | skb_put(skb, ops->tx_tail_len); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 886 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 887 | ops->tx_frame_init(skb->data, cmd_code); |
| 888 | ops->tx_update_payload_len(skb->data, payload_len); |
| 889 | ops->tx_frame_finish(skb->data); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 890 | } |
| 891 | |
Waldemar Rymarkiewicz | ddf19d2 | 2013-04-03 08:02:08 +0200 | [diff] [blame] | 892 | static int pn533_send_async_complete(struct pn533 *dev) |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 893 | { |
Waldemar Rymarkiewicz | ddf19d2 | 2013-04-03 08:02:08 +0200 | [diff] [blame] | 894 | struct pn533_cmd *cmd = dev->cmd; |
Waldemar Rymarkiewicz | f87bc9f | 2013-04-03 08:02:11 +0200 | [diff] [blame] | 895 | int status = cmd->status; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 896 | |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 897 | struct sk_buff *req = cmd->req; |
| 898 | struct sk_buff *resp = cmd->resp; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 899 | |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 900 | int rc; |
| 901 | |
| 902 | dev_kfree_skb(req); |
| 903 | |
Waldemar Rymarkiewicz | 0c33d26 | 2012-12-10 14:42:59 +0100 | [diff] [blame] | 904 | if (status < 0) { |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 905 | rc = cmd->complete_cb(dev, cmd->complete_cb_context, |
| 906 | ERR_PTR(status)); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 907 | dev_kfree_skb(resp); |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 908 | goto done; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 909 | } |
| 910 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 911 | skb_put(resp, dev->ops->rx_frame_size(resp->data)); |
| 912 | skb_pull(resp, dev->ops->rx_header_len); |
| 913 | skb_trim(resp, resp->len - dev->ops->rx_tail_len); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 914 | |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 915 | rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 916 | |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 917 | done: |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 918 | kfree(cmd); |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 919 | dev->cmd = NULL; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 920 | return rc; |
| 921 | } |
| 922 | |
| 923 | static int __pn533_send_async(struct pn533 *dev, u8 cmd_code, |
| 924 | struct sk_buff *req, struct sk_buff *resp, |
| 925 | int resp_len, |
| 926 | pn533_send_async_complete_t complete_cb, |
| 927 | void *complete_cb_context) |
| 928 | { |
| 929 | struct pn533_cmd *cmd; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 930 | int rc = 0; |
| 931 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 932 | dev_dbg(&dev->interface->dev, "Sending command 0x%x\n", cmd_code); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 933 | |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 934 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
| 935 | if (!cmd) |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 936 | return -ENOMEM; |
| 937 | |
Waldemar Rymarkiewicz | 4b2a953 | 2013-04-03 08:02:06 +0200 | [diff] [blame] | 938 | cmd->code = cmd_code; |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 939 | cmd->req = req; |
| 940 | cmd->resp = resp; |
| 941 | cmd->resp_len = resp_len; |
| 942 | cmd->complete_cb = complete_cb; |
| 943 | cmd->complete_cb_context = complete_cb_context; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 944 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 945 | pn533_build_cmd_frame(dev, cmd_code, req); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 946 | |
| 947 | mutex_lock(&dev->cmd_lock); |
| 948 | |
| 949 | if (!dev->cmd_pending) { |
Waldemar Rymarkiewicz | ddf19d2 | 2013-04-03 08:02:08 +0200 | [diff] [blame] | 950 | rc = __pn533_send_frame_async(dev, req, resp, resp_len); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 951 | if (rc) |
| 952 | goto error; |
| 953 | |
| 954 | dev->cmd_pending = 1; |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 955 | dev->cmd = cmd; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 956 | goto unlock; |
| 957 | } |
| 958 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 959 | dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x\n", |
| 960 | __func__, cmd_code); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 961 | |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 962 | INIT_LIST_HEAD(&cmd->queue); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 963 | list_add_tail(&cmd->queue, &dev->cmd_queue); |
| 964 | |
| 965 | goto unlock; |
| 966 | |
| 967 | error: |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 968 | kfree(cmd); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 969 | unlock: |
| 970 | mutex_unlock(&dev->cmd_lock); |
| 971 | return rc; |
| 972 | } |
| 973 | |
Waldemar Rymarkiewicz | 15461ae | 2012-11-26 14:18:35 +0100 | [diff] [blame] | 974 | static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code, |
| 975 | struct sk_buff *req, |
| 976 | pn533_send_async_complete_t complete_cb, |
| 977 | void *complete_cb_context) |
| 978 | { |
| 979 | struct sk_buff *resp; |
| 980 | int rc; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 981 | int resp_len = dev->ops->rx_header_len + |
| 982 | dev->ops->max_payload_len + |
| 983 | dev->ops->rx_tail_len; |
Waldemar Rymarkiewicz | 15461ae | 2012-11-26 14:18:35 +0100 | [diff] [blame] | 984 | |
Waldemar Rymarkiewicz | 15461ae | 2012-11-26 14:18:35 +0100 | [diff] [blame] | 985 | resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL); |
| 986 | if (!resp) |
| 987 | return -ENOMEM; |
| 988 | |
| 989 | rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb, |
| 990 | complete_cb_context); |
| 991 | if (rc) |
| 992 | dev_kfree_skb(resp); |
| 993 | |
| 994 | return rc; |
| 995 | } |
| 996 | |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 997 | static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code, |
| 998 | struct sk_buff *req, |
| 999 | pn533_send_async_complete_t complete_cb, |
| 1000 | void *complete_cb_context) |
| 1001 | { |
| 1002 | struct sk_buff *resp; |
| 1003 | int rc; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1004 | int resp_len = dev->ops->rx_header_len + |
| 1005 | dev->ops->max_payload_len + |
| 1006 | dev->ops->rx_tail_len; |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 1007 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1008 | resp = alloc_skb(resp_len, GFP_KERNEL); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 1009 | if (!resp) |
| 1010 | return -ENOMEM; |
| 1011 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1012 | rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb, |
| 1013 | complete_cb_context); |
Waldemar Rymarkiewicz | aada17a | 2012-11-26 14:18:34 +0100 | [diff] [blame] | 1014 | if (rc) |
| 1015 | dev_kfree_skb(resp); |
| 1016 | |
| 1017 | return rc; |
| 1018 | } |
| 1019 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1020 | /* |
| 1021 | * pn533_send_cmd_direct_async |
| 1022 | * |
| 1023 | * The function sends a piority cmd directly to the chip omiting the cmd |
| 1024 | * queue. It's intended to be used by chaining mechanism of received responses |
| 1025 | * where the host has to request every single chunk of data before scheduling |
| 1026 | * next cmd from the queue. |
| 1027 | */ |
| 1028 | static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code, |
| 1029 | struct sk_buff *req, |
| 1030 | pn533_send_async_complete_t complete_cb, |
| 1031 | void *complete_cb_context) |
| 1032 | { |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1033 | struct sk_buff *resp; |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 1034 | struct pn533_cmd *cmd; |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1035 | int rc; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1036 | int resp_len = dev->ops->rx_header_len + |
| 1037 | dev->ops->max_payload_len + |
| 1038 | dev->ops->rx_tail_len; |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1039 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1040 | resp = alloc_skb(resp_len, GFP_KERNEL); |
| 1041 | if (!resp) |
| 1042 | return -ENOMEM; |
| 1043 | |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 1044 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
| 1045 | if (!cmd) { |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1046 | dev_kfree_skb(resp); |
| 1047 | return -ENOMEM; |
| 1048 | } |
| 1049 | |
Waldemar Rymarkiewicz | 4b2a953 | 2013-04-03 08:02:06 +0200 | [diff] [blame] | 1050 | cmd->code = cmd_code; |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 1051 | cmd->req = req; |
| 1052 | cmd->resp = resp; |
| 1053 | cmd->resp_len = resp_len; |
| 1054 | cmd->complete_cb = complete_cb; |
| 1055 | cmd->complete_cb_context = complete_cb_context; |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1056 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1057 | pn533_build_cmd_frame(dev, cmd_code, req); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1058 | |
Waldemar Rymarkiewicz | ddf19d2 | 2013-04-03 08:02:08 +0200 | [diff] [blame] | 1059 | rc = __pn533_send_frame_async(dev, req, resp, resp_len); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1060 | if (rc < 0) { |
| 1061 | dev_kfree_skb(resp); |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 1062 | kfree(cmd); |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 1063 | } else { |
| 1064 | dev->cmd = cmd; |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | return rc; |
| 1068 | } |
| 1069 | |
Waldemar Rymarkiewicz | c79490e | 2013-04-03 08:02:09 +0200 | [diff] [blame] | 1070 | static void pn533_wq_cmd_complete(struct work_struct *work) |
| 1071 | { |
| 1072 | struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work); |
| 1073 | int rc; |
| 1074 | |
| 1075 | rc = pn533_send_async_complete(dev); |
| 1076 | if (rc != -EINPROGRESS) |
| 1077 | queue_work(dev->wq, &dev->cmd_work); |
| 1078 | } |
| 1079 | |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 1080 | static void pn533_wq_cmd(struct work_struct *work) |
| 1081 | { |
| 1082 | struct pn533 *dev = container_of(work, struct pn533, cmd_work); |
| 1083 | struct pn533_cmd *cmd; |
Waldemar Rymarkiewicz | 0ce1fbd | 2013-04-03 08:02:03 +0200 | [diff] [blame] | 1084 | int rc; |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 1085 | |
| 1086 | mutex_lock(&dev->cmd_lock); |
| 1087 | |
| 1088 | if (list_empty(&dev->cmd_queue)) { |
| 1089 | dev->cmd_pending = 0; |
| 1090 | mutex_unlock(&dev->cmd_lock); |
| 1091 | return; |
| 1092 | } |
| 1093 | |
| 1094 | cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue); |
| 1095 | |
Szymon Janc | 60ad07a | 2012-10-25 17:29:45 +0200 | [diff] [blame] | 1096 | list_del(&cmd->queue); |
| 1097 | |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 1098 | mutex_unlock(&dev->cmd_lock); |
| 1099 | |
Waldemar Rymarkiewicz | ddf19d2 | 2013-04-03 08:02:08 +0200 | [diff] [blame] | 1100 | rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len); |
Waldemar Rymarkiewicz | 0ce1fbd | 2013-04-03 08:02:03 +0200 | [diff] [blame] | 1101 | if (rc < 0) { |
| 1102 | dev_kfree_skb(cmd->req); |
| 1103 | dev_kfree_skb(cmd->resp); |
Waldemar Rymarkiewicz | 4231604 | 2013-04-03 08:02:04 +0200 | [diff] [blame] | 1104 | kfree(cmd); |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 1105 | return; |
Waldemar Rymarkiewicz | 0ce1fbd | 2013-04-03 08:02:03 +0200 | [diff] [blame] | 1106 | } |
Waldemar Rymarkiewicz | 2c206fb | 2013-04-03 08:02:05 +0200 | [diff] [blame] | 1107 | |
| 1108 | dev->cmd = cmd; |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 1109 | } |
| 1110 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1111 | struct pn533_sync_cmd_response { |
Waldemar Rymarkiewicz | 94c5c15 | 2012-11-26 14:18:36 +0100 | [diff] [blame] | 1112 | struct sk_buff *resp; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1113 | struct completion done; |
| 1114 | }; |
| 1115 | |
Waldemar Rymarkiewicz | 94c5c15 | 2012-11-26 14:18:36 +0100 | [diff] [blame] | 1116 | static int pn533_send_sync_complete(struct pn533 *dev, void *_arg, |
| 1117 | struct sk_buff *resp) |
| 1118 | { |
| 1119 | struct pn533_sync_cmd_response *arg = _arg; |
| 1120 | |
Waldemar Rymarkiewicz | 94c5c15 | 2012-11-26 14:18:36 +0100 | [diff] [blame] | 1121 | arg->resp = resp; |
| 1122 | complete(&arg->done); |
| 1123 | |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | /* pn533_send_cmd_sync |
| 1128 | * |
| 1129 | * Please note the req parameter is freed inside the function to |
| 1130 | * limit a number of return value interpretations by the caller. |
| 1131 | * |
| 1132 | * 1. negative in case of error during TX path -> req should be freed |
| 1133 | * |
| 1134 | * 2. negative in case of error during RX path -> req should not be freed |
| 1135 | * as it's been already freed at the begining of RX path by |
| 1136 | * async_complete_cb. |
| 1137 | * |
| 1138 | * 3. valid pointer in case of succesfult RX path |
| 1139 | * |
| 1140 | * A caller has to check a return value with IS_ERR macro. If the test pass, |
| 1141 | * the returned pointer is valid. |
| 1142 | * |
| 1143 | * */ |
| 1144 | static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code, |
| 1145 | struct sk_buff *req) |
| 1146 | { |
| 1147 | int rc; |
| 1148 | struct pn533_sync_cmd_response arg; |
| 1149 | |
Waldemar Rymarkiewicz | 94c5c15 | 2012-11-26 14:18:36 +0100 | [diff] [blame] | 1150 | init_completion(&arg.done); |
| 1151 | |
| 1152 | rc = pn533_send_cmd_async(dev, cmd_code, req, |
| 1153 | pn533_send_sync_complete, &arg); |
| 1154 | if (rc) { |
| 1155 | dev_kfree_skb(req); |
| 1156 | return ERR_PTR(rc); |
| 1157 | } |
| 1158 | |
| 1159 | wait_for_completion(&arg.done); |
| 1160 | |
| 1161 | return arg.resp; |
| 1162 | } |
| 1163 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1164 | static void pn533_send_complete(struct urb *urb) |
| 1165 | { |
| 1166 | struct pn533 *dev = urb->context; |
| 1167 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1168 | switch (urb->status) { |
| 1169 | case 0: |
Waldemar Rymarkiewicz | f8f9917 | 2012-12-10 14:42:54 +0100 | [diff] [blame] | 1170 | break; /* success */ |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1171 | case -ECONNRESET: |
| 1172 | case -ENOENT: |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1173 | dev_dbg(&dev->interface->dev, |
| 1174 | "The urb has been stopped (status %d)\n", |
| 1175 | urb->status); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1176 | break; |
Waldemar Rymarkiewicz | f8f9917 | 2012-12-10 14:42:54 +0100 | [diff] [blame] | 1177 | case -ESHUTDOWN: |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1178 | default: |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1179 | nfc_err(&dev->interface->dev, "Urb failure (status %d)\n", |
| 1180 | urb->status); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1181 | } |
| 1182 | } |
| 1183 | |
Waldemar Rymarkiewicz | 10cff29 | 2013-04-03 08:02:14 +0200 | [diff] [blame] | 1184 | static void pn533_abort_cmd(struct pn533 *dev, gfp_t flags) |
| 1185 | { |
| 1186 | /* ACR122U does not support any command which aborts last |
| 1187 | * issued command i.e. as ACK for standard PN533. Additionally, |
| 1188 | * it behaves stange, sending broken or incorrect responses, |
| 1189 | * when we cancel urb before the chip will send response. |
| 1190 | */ |
| 1191 | if (dev->device_type == PN533_DEVICE_ACR122U) |
| 1192 | return; |
| 1193 | |
| 1194 | /* An ack will cancel the last issued command */ |
| 1195 | pn533_send_ack(dev, flags); |
| 1196 | |
| 1197 | /* cancel the urb request */ |
| 1198 | usb_kill_urb(dev->in_urb); |
| 1199 | } |
| 1200 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1201 | static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size) |
Waldemar Rymarkiewicz | d22b2db | 2012-11-26 14:18:37 +0100 | [diff] [blame] | 1202 | { |
| 1203 | struct sk_buff *skb; |
| 1204 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1205 | skb = alloc_skb(dev->ops->tx_header_len + |
Waldemar Rymarkiewicz | d22b2db | 2012-11-26 14:18:37 +0100 | [diff] [blame] | 1206 | size + |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1207 | dev->ops->tx_tail_len, GFP_KERNEL); |
Waldemar Rymarkiewicz | d22b2db | 2012-11-26 14:18:37 +0100 | [diff] [blame] | 1208 | |
| 1209 | if (skb) |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1210 | skb_reserve(skb, dev->ops->tx_header_len); |
Waldemar Rymarkiewicz | d22b2db | 2012-11-26 14:18:37 +0100 | [diff] [blame] | 1211 | |
| 1212 | return skb; |
| 1213 | } |
| 1214 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1215 | struct pn533_target_type_a { |
| 1216 | __be16 sens_res; |
| 1217 | u8 sel_res; |
| 1218 | u8 nfcid_len; |
| 1219 | u8 nfcid_data[]; |
| 1220 | } __packed; |
| 1221 | |
| 1222 | |
| 1223 | #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6)) |
| 1224 | #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0)) |
| 1225 | #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8)) |
| 1226 | |
| 1227 | #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00 |
| 1228 | #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C |
| 1229 | |
| 1230 | #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5) |
| 1231 | #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2) |
| 1232 | |
| 1233 | #define PN533_TYPE_A_SEL_PROT_MIFARE 0 |
| 1234 | #define PN533_TYPE_A_SEL_PROT_ISO14443 1 |
| 1235 | #define PN533_TYPE_A_SEL_PROT_DEP 2 |
| 1236 | #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3 |
| 1237 | |
| 1238 | static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a, |
| 1239 | int target_data_len) |
| 1240 | { |
| 1241 | u8 ssd; |
| 1242 | u8 platconf; |
| 1243 | |
| 1244 | if (target_data_len < sizeof(struct pn533_target_type_a)) |
| 1245 | return false; |
| 1246 | |
| 1247 | /* The lenght check of nfcid[] and ats[] are not being performed because |
| 1248 | the values are not being used */ |
| 1249 | |
| 1250 | /* Requirement 4.6.3.3 from NFC Forum Digital Spec */ |
| 1251 | ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res); |
| 1252 | platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res); |
| 1253 | |
| 1254 | if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL && |
Waldemar Rymarkiewicz | 5d46774 | 2012-12-10 14:42:51 +0100 | [diff] [blame] | 1255 | platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) || |
| 1256 | (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL && |
| 1257 | platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1258 | return false; |
| 1259 | |
| 1260 | /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */ |
| 1261 | if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0) |
| 1262 | return false; |
| 1263 | |
| 1264 | return true; |
| 1265 | } |
| 1266 | |
| 1267 | static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data, |
| 1268 | int tgt_data_len) |
| 1269 | { |
| 1270 | struct pn533_target_type_a *tgt_type_a; |
| 1271 | |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 1272 | tgt_type_a = (struct pn533_target_type_a *)tgt_data; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1273 | |
| 1274 | if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len)) |
| 1275 | return -EPROTO; |
| 1276 | |
| 1277 | switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) { |
| 1278 | case PN533_TYPE_A_SEL_PROT_MIFARE: |
| 1279 | nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK; |
| 1280 | break; |
| 1281 | case PN533_TYPE_A_SEL_PROT_ISO14443: |
| 1282 | nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK; |
| 1283 | break; |
| 1284 | case PN533_TYPE_A_SEL_PROT_DEP: |
| 1285 | nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK; |
| 1286 | break; |
| 1287 | case PN533_TYPE_A_SEL_PROT_ISO14443_DEP: |
| 1288 | nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK | |
| 1289 | NFC_PROTO_NFC_DEP_MASK; |
| 1290 | break; |
| 1291 | } |
| 1292 | |
| 1293 | nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res); |
| 1294 | nfc_tgt->sel_res = tgt_type_a->sel_res; |
Samuel Ortiz | c3b1e1e | 2012-03-05 01:03:33 +0100 | [diff] [blame] | 1295 | nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len; |
| 1296 | memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1297 | |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | struct pn533_target_felica { |
| 1302 | u8 pol_res; |
| 1303 | u8 opcode; |
Samuel Ortiz | 322bce9 | 2013-05-27 15:29:11 +0200 | [diff] [blame] | 1304 | u8 nfcid2[NFC_NFCID2_MAXSIZE]; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1305 | u8 pad[8]; |
| 1306 | /* optional */ |
| 1307 | u8 syst_code[]; |
| 1308 | } __packed; |
| 1309 | |
| 1310 | #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01 |
| 1311 | #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE |
| 1312 | |
| 1313 | static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica, |
| 1314 | int target_data_len) |
| 1315 | { |
| 1316 | if (target_data_len < sizeof(struct pn533_target_felica)) |
| 1317 | return false; |
| 1318 | |
| 1319 | if (felica->opcode != PN533_FELICA_OPC_SENSF_RES) |
| 1320 | return false; |
| 1321 | |
| 1322 | return true; |
| 1323 | } |
| 1324 | |
| 1325 | static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data, |
| 1326 | int tgt_data_len) |
| 1327 | { |
| 1328 | struct pn533_target_felica *tgt_felica; |
| 1329 | |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 1330 | tgt_felica = (struct pn533_target_felica *)tgt_data; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1331 | |
| 1332 | if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len)) |
| 1333 | return -EPROTO; |
| 1334 | |
Waldemar Rymarkiewicz | 5d46774 | 2012-12-10 14:42:51 +0100 | [diff] [blame] | 1335 | if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) && |
| 1336 | (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1337 | nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK; |
| 1338 | else |
| 1339 | nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK; |
| 1340 | |
Samuel Ortiz | 7975754 | 2012-03-05 01:03:45 +0100 | [diff] [blame] | 1341 | memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9); |
| 1342 | nfc_tgt->sensf_res_len = 9; |
| 1343 | |
Samuel Ortiz | 322bce9 | 2013-05-27 15:29:11 +0200 | [diff] [blame] | 1344 | memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE); |
| 1345 | nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE; |
| 1346 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1347 | return 0; |
| 1348 | } |
| 1349 | |
| 1350 | struct pn533_target_jewel { |
| 1351 | __be16 sens_res; |
| 1352 | u8 jewelid[4]; |
| 1353 | } __packed; |
| 1354 | |
| 1355 | static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel, |
| 1356 | int target_data_len) |
| 1357 | { |
| 1358 | u8 ssd; |
| 1359 | u8 platconf; |
| 1360 | |
| 1361 | if (target_data_len < sizeof(struct pn533_target_jewel)) |
| 1362 | return false; |
| 1363 | |
| 1364 | /* Requirement 4.6.3.3 from NFC Forum Digital Spec */ |
| 1365 | ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res); |
| 1366 | platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res); |
| 1367 | |
| 1368 | if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL && |
Waldemar Rymarkiewicz | 5d46774 | 2012-12-10 14:42:51 +0100 | [diff] [blame] | 1369 | platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) || |
| 1370 | (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL && |
| 1371 | platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1372 | return false; |
| 1373 | |
| 1374 | return true; |
| 1375 | } |
| 1376 | |
| 1377 | static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data, |
| 1378 | int tgt_data_len) |
| 1379 | { |
| 1380 | struct pn533_target_jewel *tgt_jewel; |
| 1381 | |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 1382 | tgt_jewel = (struct pn533_target_jewel *)tgt_data; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1383 | |
| 1384 | if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len)) |
| 1385 | return -EPROTO; |
| 1386 | |
| 1387 | nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK; |
| 1388 | nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res); |
Samuel Ortiz | d8dc107 | 2012-03-05 01:03:46 +0100 | [diff] [blame] | 1389 | nfc_tgt->nfcid1_len = 4; |
| 1390 | memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1391 | |
| 1392 | return 0; |
| 1393 | } |
| 1394 | |
| 1395 | struct pn533_type_b_prot_info { |
| 1396 | u8 bitrate; |
| 1397 | u8 fsci_type; |
| 1398 | u8 fwi_adc_fo; |
| 1399 | } __packed; |
| 1400 | |
| 1401 | #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4) |
| 1402 | #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0) |
| 1403 | #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8 |
| 1404 | |
| 1405 | struct pn533_type_b_sens_res { |
| 1406 | u8 opcode; |
| 1407 | u8 nfcid[4]; |
| 1408 | u8 appdata[4]; |
| 1409 | struct pn533_type_b_prot_info prot_info; |
| 1410 | } __packed; |
| 1411 | |
| 1412 | #define PN533_TYPE_B_OPC_SENSB_RES 0x50 |
| 1413 | |
| 1414 | struct pn533_target_type_b { |
| 1415 | struct pn533_type_b_sens_res sensb_res; |
| 1416 | u8 attrib_res_len; |
| 1417 | u8 attrib_res[]; |
| 1418 | } __packed; |
| 1419 | |
| 1420 | static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b, |
| 1421 | int target_data_len) |
| 1422 | { |
| 1423 | if (target_data_len < sizeof(struct pn533_target_type_b)) |
| 1424 | return false; |
| 1425 | |
| 1426 | if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES) |
| 1427 | return false; |
| 1428 | |
| 1429 | if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) & |
| 1430 | PN533_TYPE_B_PROT_TYPE_RFU_MASK) |
| 1431 | return false; |
| 1432 | |
| 1433 | return true; |
| 1434 | } |
| 1435 | |
| 1436 | static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data, |
| 1437 | int tgt_data_len) |
| 1438 | { |
| 1439 | struct pn533_target_type_b *tgt_type_b; |
| 1440 | |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 1441 | tgt_type_b = (struct pn533_target_type_b *)tgt_data; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1442 | |
| 1443 | if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len)) |
| 1444 | return -EPROTO; |
| 1445 | |
Samuel Ortiz | 01d719a | 2012-07-04 00:14:04 +0200 | [diff] [blame] | 1446 | nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1447 | |
| 1448 | return 0; |
| 1449 | } |
| 1450 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1451 | static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata, |
| 1452 | int tgdata_len) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1453 | { |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1454 | struct nfc_target nfc_tgt; |
| 1455 | int rc; |
| 1456 | |
Joe Perches | 17936b4 | 2013-04-05 12:27:39 -0700 | [diff] [blame] | 1457 | dev_dbg(&dev->interface->dev, "%s: modulation=%d\n", |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1458 | __func__, dev->poll_mod_curr); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1459 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1460 | if (tg != 1) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1461 | return -EPROTO; |
| 1462 | |
Samuel Ortiz | 98b3ac1 | 2012-03-05 01:03:39 +0100 | [diff] [blame] | 1463 | memset(&nfc_tgt, 0, sizeof(struct nfc_target)); |
| 1464 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1465 | switch (dev->poll_mod_curr) { |
| 1466 | case PN533_POLL_MOD_106KBPS_A: |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1467 | rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1468 | break; |
| 1469 | case PN533_POLL_MOD_212KBPS_FELICA: |
| 1470 | case PN533_POLL_MOD_424KBPS_FELICA: |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1471 | rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1472 | break; |
| 1473 | case PN533_POLL_MOD_106KBPS_JEWEL: |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1474 | rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1475 | break; |
| 1476 | case PN533_POLL_MOD_847KBPS_B: |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1477 | rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1478 | break; |
| 1479 | default: |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1480 | nfc_err(&dev->interface->dev, |
| 1481 | "Unknown current poll modulation\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1482 | return -EPROTO; |
| 1483 | } |
| 1484 | |
| 1485 | if (rc) |
| 1486 | return rc; |
| 1487 | |
| 1488 | if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) { |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1489 | dev_dbg(&dev->interface->dev, |
| 1490 | "The Tg found doesn't have the desired protocol\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1491 | return -EAGAIN; |
| 1492 | } |
| 1493 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1494 | dev_dbg(&dev->interface->dev, |
| 1495 | "Target found - supported protocols: 0x%x\n", |
| 1496 | nfc_tgt.supported_protocols); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1497 | |
| 1498 | dev->tgt_available_prots = nfc_tgt.supported_protocols; |
| 1499 | |
| 1500 | nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1); |
| 1501 | |
| 1502 | return 0; |
| 1503 | } |
| 1504 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1505 | static inline void pn533_poll_next_mod(struct pn533 *dev) |
| 1506 | { |
| 1507 | dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count; |
| 1508 | } |
| 1509 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1510 | static void pn533_poll_reset_mod_list(struct pn533 *dev) |
| 1511 | { |
| 1512 | dev->poll_mod_count = 0; |
| 1513 | } |
| 1514 | |
| 1515 | static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index) |
| 1516 | { |
| 1517 | dev->poll_mod_active[dev->poll_mod_count] = |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 1518 | (struct pn533_poll_modulations *)&poll_mod[mod_index]; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1519 | dev->poll_mod_count++; |
| 1520 | } |
| 1521 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1522 | static void pn533_poll_create_mod_list(struct pn533 *dev, |
| 1523 | u32 im_protocols, u32 tm_protocols) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1524 | { |
| 1525 | pn533_poll_reset_mod_list(dev); |
| 1526 | |
Waldemar Rymarkiewicz | b08e860 | 2012-12-10 14:42:52 +0100 | [diff] [blame] | 1527 | if ((im_protocols & NFC_PROTO_MIFARE_MASK) || |
| 1528 | (im_protocols & NFC_PROTO_ISO14443_MASK) || |
| 1529 | (im_protocols & NFC_PROTO_NFC_DEP_MASK)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1530 | pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A); |
| 1531 | |
Waldemar Rymarkiewicz | b08e860 | 2012-12-10 14:42:52 +0100 | [diff] [blame] | 1532 | if (im_protocols & NFC_PROTO_FELICA_MASK || |
| 1533 | im_protocols & NFC_PROTO_NFC_DEP_MASK) { |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1534 | pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA); |
| 1535 | pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA); |
| 1536 | } |
| 1537 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1538 | if (im_protocols & NFC_PROTO_JEWEL_MASK) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1539 | pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL); |
| 1540 | |
Samuel Ortiz | 01d719a | 2012-07-04 00:14:04 +0200 | [diff] [blame] | 1541 | if (im_protocols & NFC_PROTO_ISO14443_B_MASK) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1542 | pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1543 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1544 | if (tm_protocols) |
| 1545 | pn533_poll_add_mod(dev, PN533_LISTEN_MOD); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1546 | } |
| 1547 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1548 | static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1549 | { |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1550 | u8 nbtg, tg, *tgdata; |
| 1551 | int rc, tgdata_len; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1552 | |
Samuel Ortiz | 673088f | 2013-08-21 15:06:55 +0200 | [diff] [blame] | 1553 | /* Toggle the DEP polling */ |
| 1554 | dev->poll_dep = 1; |
| 1555 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1556 | nbtg = resp->data[0]; |
| 1557 | tg = resp->data[1]; |
| 1558 | tgdata = &resp->data[2]; |
| 1559 | tgdata_len = resp->len - 2; /* nbtg + tg */ |
| 1560 | |
| 1561 | if (nbtg) { |
| 1562 | rc = pn533_target_found(dev, tg, tgdata, tgdata_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1563 | |
| 1564 | /* We must stop the poll after a valid target found */ |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1565 | if (rc == 0) { |
| 1566 | pn533_poll_reset_mod_list(dev); |
| 1567 | return 0; |
| 1568 | } |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1569 | } |
| 1570 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1571 | return -EAGAIN; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 1572 | } |
| 1573 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1574 | static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev) |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1575 | { |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1576 | struct sk_buff *skb; |
| 1577 | u8 *felica, *nfcid3, *gb; |
| 1578 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1579 | u8 *gbytes = dev->gb; |
| 1580 | size_t gbytes_len = dev->gb_len; |
| 1581 | |
Samuel Ortiz | 51d9e80 | 2012-05-30 01:48:46 +0200 | [diff] [blame] | 1582 | u8 felica_params[18] = {0x1, 0xfe, /* DEP */ |
| 1583 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */ |
| 1584 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 1585 | 0xff, 0xff}; /* System code */ |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1586 | |
Samuel Ortiz | 51d9e80 | 2012-05-30 01:48:46 +0200 | [diff] [blame] | 1587 | u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */ |
| 1588 | 0x0, 0x0, 0x0, |
| 1589 | 0x40}; /* SEL_RES for DEP */ |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1590 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1591 | unsigned int skb_len = 36 + /* mode (1), mifare (6), |
| 1592 | felica (18), nfcid3 (10), gb_len (1) */ |
| 1593 | gbytes_len + |
| 1594 | 1; /* len Tk*/ |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1595 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1596 | skb = pn533_alloc_skb(dev, skb_len); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1597 | if (!skb) |
| 1598 | return NULL; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1599 | |
| 1600 | /* DEP support only */ |
Thierry Escande | 52f2eae | 2013-02-05 10:08:35 +0100 | [diff] [blame] | 1601 | *skb_put(skb, 1) = PN533_INIT_TARGET_DEP; |
Samuel Ortiz | 51d9e80 | 2012-05-30 01:48:46 +0200 | [diff] [blame] | 1602 | |
| 1603 | /* MIFARE params */ |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1604 | memcpy(skb_put(skb, 6), mifare_params, 6); |
| 1605 | |
| 1606 | /* Felica params */ |
| 1607 | felica = skb_put(skb, 18); |
| 1608 | memcpy(felica, felica_params, 18); |
| 1609 | get_random_bytes(felica + 2, 6); |
| 1610 | |
| 1611 | /* NFCID3 */ |
| 1612 | nfcid3 = skb_put(skb, 10); |
| 1613 | memset(nfcid3, 0, 10); |
| 1614 | memcpy(nfcid3, felica, 8); |
Samuel Ortiz | 51d9e80 | 2012-05-30 01:48:46 +0200 | [diff] [blame] | 1615 | |
| 1616 | /* General bytes */ |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1617 | *skb_put(skb, 1) = gbytes_len; |
| 1618 | |
| 1619 | gb = skb_put(skb, gbytes_len); |
| 1620 | memcpy(gb, gbytes, gbytes_len); |
Samuel Ortiz | 51d9e80 | 2012-05-30 01:48:46 +0200 | [diff] [blame] | 1621 | |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1622 | /* Len Tk */ |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1623 | *skb_put(skb, 1) = 0; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1624 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1625 | return skb; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1626 | } |
| 1627 | |
Waldemar Rymarkiewicz | b1bb290 | 2012-11-26 14:18:32 +0100 | [diff] [blame] | 1628 | #define PN533_CMD_DATAEXCH_HEAD_LEN 1 |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1629 | #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262 |
Olivier Guiter | 3c13b24 | 2013-09-23 12:24:38 +0200 | [diff] [blame] | 1630 | static void pn533_wq_tm_mi_recv(struct work_struct *work); |
| 1631 | static struct sk_buff *pn533_build_response(struct pn533 *dev); |
| 1632 | |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1633 | static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg, |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 1634 | struct sk_buff *resp) |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1635 | { |
Olivier Guiter | 3c13b24 | 2013-09-23 12:24:38 +0200 | [diff] [blame] | 1636 | struct sk_buff *skb; |
| 1637 | u8 status, ret, mi; |
| 1638 | int rc; |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1639 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1640 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1641 | |
Olivier Guiter | 3c13b24 | 2013-09-23 12:24:38 +0200 | [diff] [blame] | 1642 | if (IS_ERR(resp)) { |
| 1643 | skb_queue_purge(&dev->resp_q); |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 1644 | return PTR_ERR(resp); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1645 | } |
| 1646 | |
Olivier Guiter | 3c13b24 | 2013-09-23 12:24:38 +0200 | [diff] [blame] | 1647 | status = resp->data[0]; |
| 1648 | |
| 1649 | ret = status & PN533_CMD_RET_MASK; |
| 1650 | mi = status & PN533_CMD_MI_MASK; |
| 1651 | |
| 1652 | skb_pull(resp, sizeof(status)); |
| 1653 | |
| 1654 | if (ret != PN533_CMD_RET_SUCCESS) { |
| 1655 | rc = -EIO; |
| 1656 | goto error; |
| 1657 | } |
| 1658 | |
| 1659 | skb_queue_tail(&dev->resp_q, resp); |
| 1660 | |
| 1661 | if (mi) { |
| 1662 | queue_work(dev->wq, &dev->mi_tm_rx_work); |
| 1663 | return -EINPROGRESS; |
| 1664 | } |
| 1665 | |
| 1666 | skb = pn533_build_response(dev); |
| 1667 | if (!skb) { |
| 1668 | rc = -EIO; |
| 1669 | goto error; |
| 1670 | } |
| 1671 | |
| 1672 | return nfc_tm_data_received(dev->nfc_dev, skb); |
| 1673 | |
| 1674 | error: |
| 1675 | nfc_tm_deactivated(dev->nfc_dev); |
| 1676 | dev->tgt_mode = 0; |
| 1677 | skb_queue_purge(&dev->resp_q); |
| 1678 | dev_kfree_skb(resp); |
| 1679 | |
| 1680 | return rc; |
| 1681 | } |
| 1682 | |
| 1683 | static void pn533_wq_tm_mi_recv(struct work_struct *work) |
| 1684 | { |
| 1685 | struct pn533 *dev = container_of(work, struct pn533, mi_tm_rx_work); |
| 1686 | struct sk_buff *skb; |
| 1687 | int rc; |
| 1688 | |
| 1689 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
| 1690 | |
| 1691 | skb = pn533_alloc_skb(dev, 0); |
| 1692 | if (!skb) |
| 1693 | return; |
| 1694 | |
| 1695 | rc = pn533_send_cmd_direct_async(dev, |
| 1696 | PN533_CMD_TG_GET_DATA, |
| 1697 | skb, |
| 1698 | pn533_tm_get_data_complete, |
| 1699 | NULL); |
| 1700 | |
| 1701 | if (rc < 0) |
| 1702 | dev_kfree_skb(skb); |
| 1703 | |
| 1704 | return; |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1705 | } |
| 1706 | |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 1707 | static int pn533_tm_send_complete(struct pn533 *dev, void *arg, |
| 1708 | struct sk_buff *resp); |
| 1709 | static void pn533_wq_tm_mi_send(struct work_struct *work) |
| 1710 | { |
| 1711 | struct pn533 *dev = container_of(work, struct pn533, mi_tm_tx_work); |
| 1712 | struct sk_buff *skb; |
| 1713 | int rc; |
| 1714 | |
| 1715 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
| 1716 | |
| 1717 | /* Grab the first skb in the queue */ |
| 1718 | skb = skb_dequeue(&dev->fragment_skb); |
| 1719 | if (skb == NULL) { /* No more data */ |
| 1720 | /* Reset the queue for future use */ |
| 1721 | skb_queue_head_init(&dev->fragment_skb); |
| 1722 | goto error; |
| 1723 | } |
| 1724 | |
| 1725 | /* last entry - remove MI bit */ |
| 1726 | if (skb_queue_len(&dev->fragment_skb) == 0) { |
| 1727 | rc = pn533_send_cmd_direct_async(dev, PN533_CMD_TG_SET_DATA, |
| 1728 | skb, pn533_tm_send_complete, NULL); |
| 1729 | } else |
| 1730 | rc = pn533_send_cmd_direct_async(dev, |
| 1731 | PN533_CMD_TG_SET_META_DATA, |
| 1732 | skb, pn533_tm_send_complete, NULL); |
| 1733 | |
| 1734 | if (rc == 0) /* success */ |
| 1735 | return; |
| 1736 | |
| 1737 | dev_err(&dev->interface->dev, |
| 1738 | "Error %d when trying to perform set meta data_exchange", rc); |
| 1739 | |
| 1740 | dev_kfree_skb(skb); |
| 1741 | |
| 1742 | error: |
| 1743 | pn533_send_ack(dev, GFP_KERNEL); |
| 1744 | queue_work(dev->wq, &dev->cmd_work); |
| 1745 | } |
| 1746 | |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1747 | static void pn533_wq_tg_get_data(struct work_struct *work) |
| 1748 | { |
| 1749 | struct pn533 *dev = container_of(work, struct pn533, tg_work); |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 1750 | struct sk_buff *skb; |
| 1751 | int rc; |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1752 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1753 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1754 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 1755 | skb = pn533_alloc_skb(dev, 0); |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 1756 | if (!skb) |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1757 | return; |
| 1758 | |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 1759 | rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb, |
| 1760 | pn533_tm_get_data_complete, NULL); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1761 | |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 1762 | if (rc < 0) |
| 1763 | dev_kfree_skb(skb); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1764 | |
| 1765 | return; |
| 1766 | } |
| 1767 | |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 1768 | #define ATR_REQ_GB_OFFSET 17 |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1769 | static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp) |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1770 | { |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1771 | u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb; |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 1772 | size_t gb_len; |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1773 | int rc; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1774 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1775 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1776 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1777 | if (resp->len < ATR_REQ_GB_OFFSET + 1) |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 1778 | return -EINVAL; |
| 1779 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1780 | mode = resp->data[0]; |
| 1781 | cmd = &resp->data[1]; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1782 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1783 | dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n", |
| 1784 | mode, resp->len); |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1785 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1786 | if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) == |
| 1787 | PN533_INIT_TARGET_RESP_ACTIVE) |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 1788 | comm_mode = NFC_COMM_ACTIVE; |
| 1789 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1790 | if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */ |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 1791 | return -EOPNOTSUPP; |
| 1792 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1793 | gb = cmd + ATR_REQ_GB_OFFSET; |
| 1794 | gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1); |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 1795 | |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1796 | rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK, |
| 1797 | comm_mode, gb, gb_len); |
| 1798 | if (rc < 0) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1799 | nfc_err(&dev->interface->dev, |
| 1800 | "Error when signaling target activation\n"); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1801 | return rc; |
| 1802 | } |
| 1803 | |
Samuel Ortiz | 51ad304 | 2012-05-31 20:01:32 +0200 | [diff] [blame] | 1804 | dev->tgt_mode = 1; |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 1805 | queue_work(dev->wq, &dev->tg_work); |
| 1806 | |
| 1807 | return 0; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1808 | } |
| 1809 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1810 | static void pn533_listen_mode_timer(unsigned long data) |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 1811 | { |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 1812 | struct pn533 *dev = (struct pn533 *)data; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1813 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1814 | dev_dbg(&dev->interface->dev, "Listen mode timeout\n"); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1815 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1816 | dev->cancel_listen = 1; |
| 1817 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1818 | pn533_poll_next_mod(dev); |
| 1819 | |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 1820 | queue_delayed_work(dev->wq, &dev->poll_work, |
| 1821 | msecs_to_jiffies(PN533_POLL_INTERVAL)); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1822 | } |
| 1823 | |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1824 | static int pn533_rf_complete(struct pn533 *dev, void *arg, |
| 1825 | struct sk_buff *resp) |
| 1826 | { |
| 1827 | int rc = 0; |
| 1828 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1829 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1830 | |
| 1831 | if (IS_ERR(resp)) { |
| 1832 | rc = PTR_ERR(resp); |
| 1833 | |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1834 | nfc_err(&dev->interface->dev, "RF setting error %d", rc); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1835 | |
| 1836 | return rc; |
| 1837 | } |
| 1838 | |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 1839 | queue_delayed_work(dev->wq, &dev->poll_work, |
| 1840 | msecs_to_jiffies(PN533_POLL_INTERVAL)); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1841 | |
| 1842 | dev_kfree_skb(resp); |
| 1843 | return rc; |
| 1844 | } |
| 1845 | |
| 1846 | static void pn533_wq_rf(struct work_struct *work) |
| 1847 | { |
| 1848 | struct pn533 *dev = container_of(work, struct pn533, rf_work); |
| 1849 | struct sk_buff *skb; |
| 1850 | int rc; |
| 1851 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1852 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1853 | |
| 1854 | skb = pn533_alloc_skb(dev, 2); |
| 1855 | if (!skb) |
| 1856 | return; |
| 1857 | |
| 1858 | *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD; |
Samuel Ortiz | 3a8eab3 | 2013-07-01 17:26:58 +0200 | [diff] [blame] | 1859 | *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD_AUTO_RFCA; |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1860 | |
| 1861 | rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb, |
| 1862 | pn533_rf_complete, NULL); |
| 1863 | if (rc < 0) { |
| 1864 | dev_kfree_skb(skb); |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1865 | nfc_err(&dev->interface->dev, "RF setting error %d\n", rc); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | return; |
| 1869 | } |
| 1870 | |
Samuel Ortiz | 673088f | 2013-08-21 15:06:55 +0200 | [diff] [blame] | 1871 | static int pn533_poll_dep_complete(struct pn533 *dev, void *arg, |
| 1872 | struct sk_buff *resp) |
| 1873 | { |
| 1874 | struct pn533_cmd_jump_dep_response *rsp; |
| 1875 | struct nfc_target nfc_target; |
| 1876 | u8 target_gt_len; |
| 1877 | int rc; |
| 1878 | |
| 1879 | if (IS_ERR(resp)) |
| 1880 | return PTR_ERR(resp); |
| 1881 | |
| 1882 | rsp = (struct pn533_cmd_jump_dep_response *)resp->data; |
| 1883 | |
| 1884 | rc = rsp->status & PN533_CMD_RET_MASK; |
| 1885 | if (rc != PN533_CMD_RET_SUCCESS) { |
| 1886 | /* Not target found, turn radio off */ |
| 1887 | queue_work(dev->wq, &dev->rf_work); |
| 1888 | |
| 1889 | dev_kfree_skb(resp); |
| 1890 | return 0; |
| 1891 | } |
| 1892 | |
| 1893 | dev_dbg(&dev->interface->dev, "Creating new target"); |
| 1894 | |
| 1895 | nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK; |
| 1896 | nfc_target.nfcid1_len = 10; |
| 1897 | memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len); |
| 1898 | rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1); |
| 1899 | if (rc) |
| 1900 | goto error; |
| 1901 | |
| 1902 | dev->tgt_available_prots = 0; |
| 1903 | dev->tgt_active_prot = NFC_PROTO_NFC_DEP; |
| 1904 | |
| 1905 | /* ATR_RES general bytes are located at offset 17 */ |
| 1906 | target_gt_len = resp->len - 17; |
| 1907 | rc = nfc_set_remote_general_bytes(dev->nfc_dev, |
| 1908 | rsp->gt, target_gt_len); |
| 1909 | if (!rc) { |
| 1910 | rc = nfc_dep_link_is_up(dev->nfc_dev, |
| 1911 | dev->nfc_dev->targets[0].idx, |
| 1912 | 0, NFC_RF_INITIATOR); |
| 1913 | |
| 1914 | if (!rc) |
| 1915 | pn533_poll_reset_mod_list(dev); |
| 1916 | } |
| 1917 | error: |
| 1918 | dev_kfree_skb(resp); |
| 1919 | return rc; |
| 1920 | } |
| 1921 | |
| 1922 | #define PASSIVE_DATA_LEN 5 |
| 1923 | static int pn533_poll_dep(struct nfc_dev *nfc_dev) |
| 1924 | { |
| 1925 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 1926 | struct sk_buff *skb; |
| 1927 | int rc, skb_len; |
| 1928 | u8 *next, nfcid3[NFC_NFCID3_MAXSIZE]; |
| 1929 | u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3}; |
| 1930 | |
| 1931 | dev_dbg(&dev->interface->dev, "%s", __func__); |
| 1932 | |
| 1933 | if (!dev->gb) { |
| 1934 | dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len); |
| 1935 | |
| 1936 | if (!dev->gb || !dev->gb_len) { |
| 1937 | dev->poll_dep = 0; |
| 1938 | queue_work(dev->wq, &dev->rf_work); |
| 1939 | } |
| 1940 | } |
| 1941 | |
| 1942 | skb_len = 3 + dev->gb_len; /* ActPass + BR + Next */ |
| 1943 | skb_len += PASSIVE_DATA_LEN; |
| 1944 | |
| 1945 | /* NFCID3 */ |
| 1946 | skb_len += NFC_NFCID3_MAXSIZE; |
| 1947 | nfcid3[0] = 0x1; |
| 1948 | nfcid3[1] = 0xfe; |
| 1949 | get_random_bytes(nfcid3 + 2, 6); |
| 1950 | |
| 1951 | skb = pn533_alloc_skb(dev, skb_len); |
| 1952 | if (!skb) |
| 1953 | return -ENOMEM; |
| 1954 | |
| 1955 | *skb_put(skb, 1) = 0x01; /* Active */ |
| 1956 | *skb_put(skb, 1) = 0x02; /* 424 kbps */ |
| 1957 | |
| 1958 | next = skb_put(skb, 1); /* Next */ |
| 1959 | *next = 0; |
| 1960 | |
| 1961 | /* Copy passive data */ |
| 1962 | memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN); |
| 1963 | *next |= 1; |
| 1964 | |
| 1965 | /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */ |
| 1966 | memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3, |
| 1967 | NFC_NFCID3_MAXSIZE); |
| 1968 | *next |= 2; |
| 1969 | |
| 1970 | memcpy(skb_put(skb, dev->gb_len), dev->gb, dev->gb_len); |
| 1971 | *next |= 4; /* We have some Gi */ |
| 1972 | |
| 1973 | rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb, |
| 1974 | pn533_poll_dep_complete, NULL); |
| 1975 | |
| 1976 | if (rc < 0) |
| 1977 | dev_kfree_skb(skb); |
| 1978 | |
| 1979 | return rc; |
| 1980 | } |
| 1981 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1982 | static int pn533_poll_complete(struct pn533 *dev, void *arg, |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1983 | struct sk_buff *resp) |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1984 | { |
| 1985 | struct pn533_poll_modulations *cur_mod; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 1986 | int rc; |
| 1987 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 1988 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1989 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1990 | if (IS_ERR(resp)) { |
| 1991 | rc = PTR_ERR(resp); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1992 | |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 1993 | nfc_err(&dev->interface->dev, "%s Poll complete error %d\n", |
| 1994 | __func__, rc); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 1995 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 1996 | if (rc == -ENOENT) { |
| 1997 | if (dev->poll_mod_count != 0) |
| 1998 | return rc; |
| 1999 | else |
| 2000 | goto stop_poll; |
| 2001 | } else if (rc < 0) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2002 | nfc_err(&dev->interface->dev, |
| 2003 | "Error %d when running poll\n", rc); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2004 | goto stop_poll; |
| 2005 | } |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | cur_mod = dev->poll_mod_active[dev->poll_mod_curr]; |
| 2009 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2010 | if (cur_mod->len == 0) { /* Target mode */ |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2011 | del_timer(&dev->listen_timer); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2012 | rc = pn533_init_target_complete(dev, resp); |
| 2013 | goto done; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2014 | } |
| 2015 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2016 | /* Initiator mode */ |
| 2017 | rc = pn533_start_poll_complete(dev, resp); |
| 2018 | if (!rc) |
| 2019 | goto done; |
| 2020 | |
Waldemar Rymarkiewicz | 95cb9e1 | 2013-04-03 08:02:00 +0200 | [diff] [blame] | 2021 | if (!dev->poll_mod_count) { |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2022 | dev_dbg(&dev->interface->dev, "Polling has been stopped\n"); |
Waldemar Rymarkiewicz | 95cb9e1 | 2013-04-03 08:02:00 +0200 | [diff] [blame] | 2023 | goto done; |
| 2024 | } |
| 2025 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2026 | pn533_poll_next_mod(dev); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 2027 | /* Not target found, turn radio off */ |
| 2028 | queue_work(dev->wq, &dev->rf_work); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2029 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2030 | done: |
| 2031 | dev_kfree_skb(resp); |
| 2032 | return rc; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2033 | |
| 2034 | stop_poll: |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2035 | nfc_err(&dev->interface->dev, "Polling operation has been stopped\n"); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2036 | |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 2037 | pn533_poll_reset_mod_list(dev); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2038 | dev->poll_protocols = 0; |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2039 | return rc; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2040 | } |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 2041 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2042 | static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev, |
| 2043 | struct pn533_poll_modulations *mod) |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2044 | { |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2045 | struct sk_buff *skb; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 2046 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2047 | skb = pn533_alloc_skb(dev, mod->len); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2048 | if (!skb) |
| 2049 | return NULL; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2050 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2051 | memcpy(skb_put(skb, mod->len), &mod->data, mod->len); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2052 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2053 | return skb; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | static int pn533_send_poll_frame(struct pn533 *dev) |
| 2057 | { |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2058 | struct pn533_poll_modulations *mod; |
| 2059 | struct sk_buff *skb; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2060 | int rc; |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2061 | u8 cmd_code; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2062 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2063 | mod = dev->poll_mod_active[dev->poll_mod_curr]; |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2064 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2065 | dev_dbg(&dev->interface->dev, "%s mod len %d\n", |
| 2066 | __func__, mod->len); |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 2067 | |
Samuel Ortiz | 673088f | 2013-08-21 15:06:55 +0200 | [diff] [blame] | 2068 | if (dev->poll_dep) { |
| 2069 | dev->poll_dep = 0; |
| 2070 | return pn533_poll_dep(dev->nfc_dev); |
| 2071 | } |
| 2072 | |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2073 | if (mod->len == 0) { /* Listen mode */ |
| 2074 | cmd_code = PN533_CMD_TG_INIT_AS_TARGET; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2075 | skb = pn533_alloc_poll_tg_frame(dev); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2076 | } else { /* Polling mode */ |
| 2077 | cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET; |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2078 | skb = pn533_alloc_poll_in_frame(dev, mod); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | if (!skb) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2082 | nfc_err(&dev->interface->dev, "Failed to allocate skb\n"); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2083 | return -ENOMEM; |
| 2084 | } |
| 2085 | |
| 2086 | rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete, |
| 2087 | NULL); |
| 2088 | if (rc < 0) { |
| 2089 | dev_kfree_skb(skb); |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2090 | nfc_err(&dev->interface->dev, "Polling loop error %d\n", rc); |
Waldemar Rymarkiewicz | b5193e5 | 2012-12-10 14:42:48 +0100 | [diff] [blame] | 2091 | } |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 2092 | |
| 2093 | return rc; |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 2094 | } |
| 2095 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2096 | static void pn533_wq_poll(struct work_struct *work) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2097 | { |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 2098 | struct pn533 *dev = container_of(work, struct pn533, poll_work.work); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2099 | struct pn533_poll_modulations *cur_mod; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2100 | int rc; |
| 2101 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2102 | cur_mod = dev->poll_mod_active[dev->poll_mod_curr]; |
| 2103 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2104 | dev_dbg(&dev->interface->dev, |
| 2105 | "%s cancel_listen %d modulation len %d\n", |
| 2106 | __func__, dev->cancel_listen, cur_mod->len); |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2107 | |
| 2108 | if (dev->cancel_listen == 1) { |
| 2109 | dev->cancel_listen = 0; |
Waldemar Rymarkiewicz | 10cff29 | 2013-04-03 08:02:14 +0200 | [diff] [blame] | 2110 | pn533_abort_cmd(dev, GFP_ATOMIC); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2111 | } |
| 2112 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2113 | rc = pn533_send_poll_frame(dev); |
| 2114 | if (rc) |
| 2115 | return; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2116 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2117 | if (cur_mod->len == 0 && dev->poll_mod_count > 1) |
| 2118 | mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2119 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2120 | return; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2121 | } |
| 2122 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 2123 | static int pn533_start_poll(struct nfc_dev *nfc_dev, |
| 2124 | u32 im_protocols, u32 tm_protocols) |
| 2125 | { |
| 2126 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
Samuel Ortiz | cec4b8e | 2013-08-21 15:12:06 +0200 | [diff] [blame] | 2127 | struct pn533_poll_modulations *cur_mod; |
Samuel Ortiz | dfccd0f | 2013-07-03 15:14:17 +0200 | [diff] [blame] | 2128 | u8 rand_mod; |
Samuel Ortiz | cec4b8e | 2013-08-21 15:12:06 +0200 | [diff] [blame] | 2129 | int rc; |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 2130 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2131 | dev_dbg(&dev->interface->dev, |
| 2132 | "%s: im protocols 0x%x tm protocols 0x%x\n", |
| 2133 | __func__, im_protocols, tm_protocols); |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 2134 | |
| 2135 | if (dev->tgt_active_prot) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2136 | nfc_err(&dev->interface->dev, |
| 2137 | "Cannot poll with a target already activated\n"); |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 2138 | return -EBUSY; |
| 2139 | } |
| 2140 | |
Samuel Ortiz | 51ad304 | 2012-05-31 20:01:32 +0200 | [diff] [blame] | 2141 | if (dev->tgt_mode) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2142 | nfc_err(&dev->interface->dev, |
| 2143 | "Cannot poll while already being activated\n"); |
Samuel Ortiz | 51ad304 | 2012-05-31 20:01:32 +0200 | [diff] [blame] | 2144 | return -EBUSY; |
| 2145 | } |
| 2146 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2147 | if (tm_protocols) { |
| 2148 | dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len); |
| 2149 | if (dev->gb == NULL) |
| 2150 | tm_protocols = 0; |
| 2151 | } |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 2152 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2153 | pn533_poll_create_mod_list(dev, im_protocols, tm_protocols); |
| 2154 | dev->poll_protocols = im_protocols; |
| 2155 | dev->listen_protocols = tm_protocols; |
Samuel Ortiz | ad3823c | 2012-05-30 23:54:55 +0200 | [diff] [blame] | 2156 | |
Samuel Ortiz | dfccd0f | 2013-07-03 15:14:17 +0200 | [diff] [blame] | 2157 | /* Do not always start polling from the same modulation */ |
| 2158 | get_random_bytes(&rand_mod, sizeof(rand_mod)); |
| 2159 | rand_mod %= dev->poll_mod_count; |
| 2160 | dev->poll_mod_curr = rand_mod; |
| 2161 | |
Samuel Ortiz | cec4b8e | 2013-08-21 15:12:06 +0200 | [diff] [blame] | 2162 | cur_mod = dev->poll_mod_active[dev->poll_mod_curr]; |
| 2163 | |
| 2164 | rc = pn533_send_poll_frame(dev); |
| 2165 | |
| 2166 | /* Start listen timer */ |
| 2167 | if (!rc && cur_mod->len == 0 && dev->poll_mod_count > 1) |
| 2168 | mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ); |
| 2169 | |
| 2170 | return rc; |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 2171 | } |
| 2172 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2173 | static void pn533_stop_poll(struct nfc_dev *nfc_dev) |
| 2174 | { |
| 2175 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 2176 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2177 | del_timer(&dev->listen_timer); |
| 2178 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2179 | if (!dev->poll_mod_count) { |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2180 | dev_dbg(&dev->interface->dev, |
| 2181 | "Polling operation was not running\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2182 | return; |
| 2183 | } |
| 2184 | |
Waldemar Rymarkiewicz | 10cff29 | 2013-04-03 08:02:14 +0200 | [diff] [blame] | 2185 | pn533_abort_cmd(dev, GFP_KERNEL); |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 2186 | flush_delayed_work(&dev->poll_work); |
Samuel Ortiz | 7c2a04a93 | 2012-05-21 16:20:01 +0200 | [diff] [blame] | 2187 | pn533_poll_reset_mod_list(dev); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | static int pn533_activate_target_nfcdep(struct pn533 *dev) |
| 2191 | { |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2192 | struct pn533_cmd_activate_response *rsp; |
Samuel Ortiz | 541d920 | 2011-12-14 16:43:10 +0100 | [diff] [blame] | 2193 | u16 gt_len; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2194 | int rc; |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2195 | struct sk_buff *skb; |
| 2196 | struct sk_buff *resp; |
| 2197 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2198 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2199 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2200 | skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/ |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2201 | if (!skb) |
| 2202 | return -ENOMEM; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2203 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2204 | *skb_put(skb, sizeof(u8)) = 1; /* TG */ |
| 2205 | *skb_put(skb, sizeof(u8)) = 0; /* Next */ |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2206 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2207 | resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb); |
| 2208 | if (IS_ERR(resp)) |
| 2209 | return PTR_ERR(resp); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2210 | |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 2211 | rsp = (struct pn533_cmd_activate_response *)resp->data; |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2212 | rc = rsp->status & PN533_CMD_RET_MASK; |
Waldemar Rymarkiewicz | 8a0ecfe | 2012-12-21 10:39:01 +0100 | [diff] [blame] | 2213 | if (rc != PN533_CMD_RET_SUCCESS) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2214 | nfc_err(&dev->interface->dev, |
| 2215 | "Target activation failed (error 0x%x)\n", rc); |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2216 | dev_kfree_skb(resp); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2217 | return -EIO; |
Waldemar Rymarkiewicz | 8a0ecfe | 2012-12-21 10:39:01 +0100 | [diff] [blame] | 2218 | } |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2219 | |
Samuel Ortiz | 541d920 | 2011-12-14 16:43:10 +0100 | [diff] [blame] | 2220 | /* ATR_RES general bytes are located at offset 16 */ |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2221 | gt_len = resp->len - 16; |
| 2222 | rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len); |
Samuel Ortiz | 541d920 | 2011-12-14 16:43:10 +0100 | [diff] [blame] | 2223 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2224 | dev_kfree_skb(resp); |
Samuel Ortiz | 541d920 | 2011-12-14 16:43:10 +0100 | [diff] [blame] | 2225 | return rc; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2226 | } |
| 2227 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 2228 | static int pn533_activate_target(struct nfc_dev *nfc_dev, |
| 2229 | struct nfc_target *target, u32 protocol) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2230 | { |
| 2231 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 2232 | int rc; |
| 2233 | |
Joe Perches | 17936b4 | 2013-04-05 12:27:39 -0700 | [diff] [blame] | 2234 | dev_dbg(&dev->interface->dev, "%s: protocol=%u\n", __func__, protocol); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2235 | |
| 2236 | if (dev->poll_mod_count) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2237 | nfc_err(&dev->interface->dev, |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2238 | "Cannot activate while polling\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2239 | return -EBUSY; |
| 2240 | } |
| 2241 | |
| 2242 | if (dev->tgt_active_prot) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2243 | nfc_err(&dev->interface->dev, |
| 2244 | "There is already an active target\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2245 | return -EBUSY; |
| 2246 | } |
| 2247 | |
| 2248 | if (!dev->tgt_available_prots) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2249 | nfc_err(&dev->interface->dev, |
| 2250 | "There is no available target to activate\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2251 | return -EINVAL; |
| 2252 | } |
| 2253 | |
| 2254 | if (!(dev->tgt_available_prots & (1 << protocol))) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2255 | nfc_err(&dev->interface->dev, |
| 2256 | "Target doesn't support requested proto %u\n", |
| 2257 | protocol); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2258 | return -EINVAL; |
| 2259 | } |
| 2260 | |
| 2261 | if (protocol == NFC_PROTO_NFC_DEP) { |
| 2262 | rc = pn533_activate_target_nfcdep(dev); |
| 2263 | if (rc) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2264 | nfc_err(&dev->interface->dev, |
| 2265 | "Activating target with DEP failed %d\n", rc); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2266 | return rc; |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | dev->tgt_active_prot = protocol; |
| 2271 | dev->tgt_available_prots = 0; |
| 2272 | |
| 2273 | return 0; |
| 2274 | } |
| 2275 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 2276 | static void pn533_deactivate_target(struct nfc_dev *nfc_dev, |
| 2277 | struct nfc_target *target) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2278 | { |
| 2279 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2280 | struct sk_buff *skb; |
| 2281 | struct sk_buff *resp; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2282 | int rc; |
| 2283 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2284 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2285 | |
| 2286 | if (!dev->tgt_active_prot) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2287 | nfc_err(&dev->interface->dev, "There is no active target\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2288 | return; |
| 2289 | } |
| 2290 | |
| 2291 | dev->tgt_active_prot = 0; |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2292 | skb_queue_purge(&dev->resp_q); |
| 2293 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2294 | skb = pn533_alloc_skb(dev, sizeof(u8)); |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2295 | if (!skb) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2296 | return; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2297 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2298 | *skb_put(skb, 1) = 1; /* TG*/ |
| 2299 | |
| 2300 | resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb); |
| 2301 | if (IS_ERR(resp)) |
| 2302 | return; |
| 2303 | |
| 2304 | rc = resp->data[0] & PN533_CMD_RET_MASK; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2305 | if (rc != PN533_CMD_RET_SUCCESS) |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2306 | nfc_err(&dev->interface->dev, |
| 2307 | "Error 0x%x when releasing the target\n", rc); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2308 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2309 | dev_kfree_skb(resp); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2310 | return; |
| 2311 | } |
| 2312 | |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2313 | |
| 2314 | static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg, |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2315 | struct sk_buff *resp) |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2316 | { |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2317 | struct pn533_cmd_jump_dep_response *rsp; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2318 | u8 target_gt_len; |
| 2319 | int rc; |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2320 | u8 active = *(u8 *)arg; |
Waldemar Rymarkiewicz | 70418e6 | 2012-10-11 14:04:00 +0200 | [diff] [blame] | 2321 | |
| 2322 | kfree(arg); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2323 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2324 | if (IS_ERR(resp)) |
| 2325 | return PTR_ERR(resp); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2326 | |
| 2327 | if (dev->tgt_available_prots && |
| 2328 | !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2329 | nfc_err(&dev->interface->dev, |
| 2330 | "The target does not support DEP\n"); |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2331 | rc = -EINVAL; |
| 2332 | goto error; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2333 | } |
| 2334 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2335 | rsp = (struct pn533_cmd_jump_dep_response *)resp->data; |
| 2336 | |
| 2337 | rc = rsp->status & PN533_CMD_RET_MASK; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2338 | if (rc != PN533_CMD_RET_SUCCESS) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2339 | nfc_err(&dev->interface->dev, |
| 2340 | "Bringing DEP link up failed (error 0x%x)\n", rc); |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2341 | goto error; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | if (!dev->tgt_available_prots) { |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2345 | struct nfc_target nfc_target; |
| 2346 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2347 | dev_dbg(&dev->interface->dev, "Creating new target\n"); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2348 | |
| 2349 | nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK; |
Samuel Ortiz | 2fbabfa | 2012-03-05 01:03:47 +0100 | [diff] [blame] | 2350 | nfc_target.nfcid1_len = 10; |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2351 | memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2352 | rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1); |
| 2353 | if (rc) |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2354 | goto error; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2355 | |
| 2356 | dev->tgt_available_prots = 0; |
| 2357 | } |
| 2358 | |
| 2359 | dev->tgt_active_prot = NFC_PROTO_NFC_DEP; |
| 2360 | |
| 2361 | /* ATR_RES general bytes are located at offset 17 */ |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2362 | target_gt_len = resp->len - 17; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2363 | rc = nfc_set_remote_general_bytes(dev->nfc_dev, |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2364 | rsp->gt, target_gt_len); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2365 | if (rc == 0) |
| 2366 | rc = nfc_dep_link_is_up(dev->nfc_dev, |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2367 | dev->nfc_dev->targets[0].idx, |
| 2368 | !active, NFC_RF_INITIATOR); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2369 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2370 | error: |
| 2371 | dev_kfree_skb(resp); |
| 2372 | return rc; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2373 | } |
| 2374 | |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 2375 | static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf); |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 2376 | static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target, |
Waldemar Rymarkiewicz | 37cf4fc | 2012-12-10 14:42:50 +0100 | [diff] [blame] | 2377 | u8 comm_mode, u8 *gb, size_t gb_len) |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2378 | { |
| 2379 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2380 | struct sk_buff *skb; |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 2381 | int rc, skb_len; |
| 2382 | u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE]; |
Samuel Ortiz | d7f3345 | 2012-05-29 21:45:21 +0200 | [diff] [blame] | 2383 | u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3}; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2384 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2385 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2386 | |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2387 | if (dev->poll_mod_count) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2388 | nfc_err(&dev->interface->dev, |
| 2389 | "Cannot bring the DEP link up while polling\n"); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2390 | return -EBUSY; |
| 2391 | } |
| 2392 | |
| 2393 | if (dev->tgt_active_prot) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2394 | nfc_err(&dev->interface->dev, |
| 2395 | "There is already an active target\n"); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2396 | return -EBUSY; |
| 2397 | } |
| 2398 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2399 | skb_len = 3 + gb_len; /* ActPass + BR + Next */ |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 2400 | skb_len += PASSIVE_DATA_LEN; |
Samuel Ortiz | d7f3345 | 2012-05-29 21:45:21 +0200 | [diff] [blame] | 2401 | |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 2402 | /* NFCID3 */ |
| 2403 | skb_len += NFC_NFCID3_MAXSIZE; |
| 2404 | if (target && !target->nfcid2_len) { |
| 2405 | nfcid3[0] = 0x1; |
| 2406 | nfcid3[1] = 0xfe; |
| 2407 | get_random_bytes(nfcid3 + 2, 6); |
| 2408 | } |
Samuel Ortiz | 322bce9 | 2013-05-27 15:29:11 +0200 | [diff] [blame] | 2409 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2410 | skb = pn533_alloc_skb(dev, skb_len); |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2411 | if (!skb) |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2412 | return -ENOMEM; |
| 2413 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2414 | *skb_put(skb, 1) = !comm_mode; /* ActPass */ |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 2415 | *skb_put(skb, 1) = 0x02; /* 424 kbps */ |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2416 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2417 | next = skb_put(skb, 1); /* Next */ |
| 2418 | *next = 0; |
| 2419 | |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 2420 | /* Copy passive data */ |
| 2421 | memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN); |
| 2422 | *next |= 1; |
Samuel Ortiz | d7f3345 | 2012-05-29 21:45:21 +0200 | [diff] [blame] | 2423 | |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 2424 | /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */ |
| 2425 | if (target && target->nfcid2_len) |
Samuel Ortiz | 322bce9 | 2013-05-27 15:29:11 +0200 | [diff] [blame] | 2426 | memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2, |
| 2427 | target->nfcid2_len); |
Samuel Ortiz | 5eef484 | 2013-07-01 10:58:12 +0200 | [diff] [blame] | 2428 | else |
| 2429 | memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3, |
| 2430 | NFC_NFCID3_MAXSIZE); |
| 2431 | *next |= 2; |
Samuel Ortiz | 322bce9 | 2013-05-27 15:29:11 +0200 | [diff] [blame] | 2432 | |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 2433 | if (gb != NULL && gb_len > 0) { |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2434 | memcpy(skb_put(skb, gb_len), gb, gb_len); |
| 2435 | *next |= 4; /* We have some Gi */ |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2436 | } else { |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2437 | *next = 0; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2438 | } |
| 2439 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2440 | arg = kmalloc(sizeof(*arg), GFP_KERNEL); |
| 2441 | if (!arg) { |
| 2442 | dev_kfree_skb(skb); |
| 2443 | return -ENOMEM; |
| 2444 | } |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2445 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2446 | *arg = !comm_mode; |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2447 | |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 2448 | pn533_rf_field(dev->nfc_dev, 0); |
| 2449 | |
Waldemar Rymarkiewicz | 1300364 | 2012-12-10 14:42:45 +0100 | [diff] [blame] | 2450 | rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb, |
| 2451 | pn533_in_dep_link_up_complete, arg); |
| 2452 | |
| 2453 | if (rc < 0) { |
| 2454 | dev_kfree_skb(skb); |
| 2455 | kfree(arg); |
| 2456 | } |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2457 | |
| 2458 | return rc; |
| 2459 | } |
| 2460 | |
| 2461 | static int pn533_dep_link_down(struct nfc_dev *nfc_dev) |
| 2462 | { |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2463 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 2464 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2465 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Waldemar Rymarkiewicz | fcfafc7 | 2012-12-10 14:42:55 +0100 | [diff] [blame] | 2466 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 2467 | pn533_poll_reset_mod_list(dev); |
| 2468 | |
Waldemar Rymarkiewicz | 10cff29 | 2013-04-03 08:02:14 +0200 | [diff] [blame] | 2469 | if (dev->tgt_mode || dev->tgt_active_prot) |
| 2470 | pn533_abort_cmd(dev, GFP_KERNEL); |
Samuel Ortiz | 51ad304 | 2012-05-31 20:01:32 +0200 | [diff] [blame] | 2471 | |
| 2472 | dev->tgt_active_prot = 0; |
| 2473 | dev->tgt_mode = 0; |
| 2474 | |
| 2475 | skb_queue_purge(&dev->resp_q); |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 2476 | |
| 2477 | return 0; |
| 2478 | } |
| 2479 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2480 | struct pn533_data_exchange_arg { |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2481 | data_exchange_cb_t cb; |
| 2482 | void *cb_context; |
| 2483 | }; |
| 2484 | |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2485 | static struct sk_buff *pn533_build_response(struct pn533 *dev) |
| 2486 | { |
| 2487 | struct sk_buff *skb, *tmp, *t; |
| 2488 | unsigned int skb_len = 0, tmp_len = 0; |
| 2489 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2490 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2491 | |
| 2492 | if (skb_queue_empty(&dev->resp_q)) |
| 2493 | return NULL; |
| 2494 | |
| 2495 | if (skb_queue_len(&dev->resp_q) == 1) { |
| 2496 | skb = skb_dequeue(&dev->resp_q); |
| 2497 | goto out; |
| 2498 | } |
| 2499 | |
| 2500 | skb_queue_walk_safe(&dev->resp_q, tmp, t) |
| 2501 | skb_len += tmp->len; |
| 2502 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2503 | dev_dbg(&dev->interface->dev, "%s total length %d\n", |
| 2504 | __func__, skb_len); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2505 | |
| 2506 | skb = alloc_skb(skb_len, GFP_KERNEL); |
| 2507 | if (skb == NULL) |
| 2508 | goto out; |
| 2509 | |
| 2510 | skb_put(skb, skb_len); |
| 2511 | |
| 2512 | skb_queue_walk_safe(&dev->resp_q, tmp, t) { |
| 2513 | memcpy(skb->data + tmp_len, tmp->data, tmp->len); |
| 2514 | tmp_len += tmp->len; |
| 2515 | } |
| 2516 | |
| 2517 | out: |
| 2518 | skb_queue_purge(&dev->resp_q); |
| 2519 | |
| 2520 | return skb; |
| 2521 | } |
| 2522 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2523 | static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg, |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2524 | struct sk_buff *resp) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2525 | { |
| 2526 | struct pn533_data_exchange_arg *arg = _arg; |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2527 | struct sk_buff *skb; |
| 2528 | int rc = 0; |
| 2529 | u8 status, ret, mi; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2530 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2531 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2532 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2533 | if (IS_ERR(resp)) { |
| 2534 | rc = PTR_ERR(resp); |
| 2535 | goto _error; |
| 2536 | } |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2537 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2538 | status = resp->data[0]; |
| 2539 | ret = status & PN533_CMD_RET_MASK; |
| 2540 | mi = status & PN533_CMD_MI_MASK; |
| 2541 | |
| 2542 | skb_pull(resp, sizeof(status)); |
| 2543 | |
| 2544 | if (ret != PN533_CMD_RET_SUCCESS) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2545 | nfc_err(&dev->interface->dev, |
| 2546 | "Exchanging data failed (error 0x%x)\n", ret); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2547 | rc = -EIO; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2548 | goto error; |
| 2549 | } |
| 2550 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2551 | skb_queue_tail(&dev->resp_q, resp); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2552 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2553 | if (mi) { |
| 2554 | dev->cmd_complete_mi_arg = arg; |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2555 | queue_work(dev->wq, &dev->mi_rx_work); |
| 2556 | return -EINPROGRESS; |
| 2557 | } |
| 2558 | |
| 2559 | /* Prepare for the next round */ |
| 2560 | if (skb_queue_len(&dev->fragment_skb) > 0) { |
| 2561 | dev->cmd_complete_dep_arg = arg; |
| 2562 | queue_work(dev->wq, &dev->mi_tx_work); |
| 2563 | |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2564 | return -EINPROGRESS; |
| 2565 | } |
| 2566 | |
| 2567 | skb = pn533_build_response(dev); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2568 | if (!skb) |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2569 | goto error; |
| 2570 | |
| 2571 | arg->cb(arg->cb_context, skb, 0); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2572 | kfree(arg); |
| 2573 | return 0; |
| 2574 | |
| 2575 | error: |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2576 | dev_kfree_skb(resp); |
| 2577 | _error: |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2578 | skb_queue_purge(&dev->resp_q); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2579 | arg->cb(arg->cb_context, NULL, rc); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2580 | kfree(arg); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2581 | return rc; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2582 | } |
| 2583 | |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2584 | /* Split the Tx skb into small chunks */ |
| 2585 | static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb) |
| 2586 | { |
| 2587 | struct sk_buff *frag; |
| 2588 | int frag_size; |
| 2589 | |
| 2590 | do { |
| 2591 | /* Remaining size */ |
| 2592 | if (skb->len > PN533_CMD_DATAFRAME_MAXLEN) |
| 2593 | frag_size = PN533_CMD_DATAFRAME_MAXLEN; |
| 2594 | else |
| 2595 | frag_size = skb->len; |
| 2596 | |
| 2597 | /* Allocate and reserve */ |
| 2598 | frag = pn533_alloc_skb(dev, frag_size); |
| 2599 | if (!frag) { |
| 2600 | skb_queue_purge(&dev->fragment_skb); |
| 2601 | break; |
| 2602 | } |
| 2603 | |
Olivier Guiter | 22953f9 | 2013-09-23 12:24:37 +0200 | [diff] [blame] | 2604 | if (!dev->tgt_mode) { |
| 2605 | /* Reserve the TG/MI byte */ |
| 2606 | skb_reserve(frag, 1); |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2607 | |
Olivier Guiter | 22953f9 | 2013-09-23 12:24:37 +0200 | [diff] [blame] | 2608 | /* MI + TG */ |
| 2609 | if (frag_size == PN533_CMD_DATAFRAME_MAXLEN) |
| 2610 | *skb_push(frag, sizeof(u8)) = |
| 2611 | (PN533_CMD_MI_MASK | 1); |
| 2612 | else |
| 2613 | *skb_push(frag, sizeof(u8)) = 1; /* TG */ |
| 2614 | } |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2615 | |
| 2616 | memcpy(skb_put(frag, frag_size), skb->data, frag_size); |
| 2617 | |
| 2618 | /* Reduce the size of incoming buffer */ |
| 2619 | skb_pull(skb, frag_size); |
| 2620 | |
| 2621 | /* Add this to skb_queue */ |
| 2622 | skb_queue_tail(&dev->fragment_skb, frag); |
| 2623 | |
| 2624 | } while (skb->len > 0); |
| 2625 | |
| 2626 | dev_kfree_skb(skb); |
| 2627 | |
| 2628 | return skb_queue_len(&dev->fragment_skb); |
| 2629 | } |
| 2630 | |
Samuel Ortiz | be9ae4c | 2012-05-16 15:55:48 +0200 | [diff] [blame] | 2631 | static int pn533_transceive(struct nfc_dev *nfc_dev, |
| 2632 | struct nfc_target *target, struct sk_buff *skb, |
| 2633 | data_exchange_cb_t cb, void *cb_context) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2634 | { |
| 2635 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2636 | struct pn533_data_exchange_arg *arg = NULL; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2637 | int rc; |
| 2638 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2639 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2640 | |
| 2641 | if (!dev->tgt_active_prot) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2642 | nfc_err(&dev->interface->dev, |
| 2643 | "Can't exchange data if there is no active target\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2644 | rc = -EINVAL; |
| 2645 | goto error; |
| 2646 | } |
| 2647 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2648 | arg = kmalloc(sizeof(*arg), GFP_KERNEL); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2649 | if (!arg) { |
| 2650 | rc = -ENOMEM; |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2651 | goto error; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2652 | } |
| 2653 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2654 | arg->cb = cb; |
| 2655 | arg->cb_context = cb_context; |
| 2656 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2657 | switch (dev->device_type) { |
| 2658 | case PN533_DEVICE_PASORI: |
| 2659 | if (dev->tgt_active_prot == NFC_PROTO_FELICA) { |
| 2660 | rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU, |
| 2661 | skb, |
| 2662 | pn533_data_exchange_complete, |
| 2663 | arg); |
| 2664 | |
| 2665 | break; |
| 2666 | } |
| 2667 | default: |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2668 | /* jumbo frame ? */ |
| 2669 | if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) { |
| 2670 | rc = pn533_fill_fragment_skbs(dev, skb); |
| 2671 | if (rc <= 0) |
| 2672 | goto error; |
| 2673 | |
| 2674 | skb = skb_dequeue(&dev->fragment_skb); |
| 2675 | if (!skb) { |
| 2676 | rc = -EIO; |
| 2677 | goto error; |
| 2678 | } |
| 2679 | } else { |
| 2680 | *skb_push(skb, sizeof(u8)) = 1; /* TG */ |
| 2681 | } |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2682 | |
| 2683 | rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE, |
| 2684 | skb, pn533_data_exchange_complete, |
| 2685 | arg); |
| 2686 | |
| 2687 | break; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2688 | } |
| 2689 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2690 | if (rc < 0) /* rc from send_async */ |
| 2691 | goto error; |
| 2692 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2693 | return 0; |
| 2694 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2695 | error: |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2696 | kfree(arg); |
| 2697 | dev_kfree_skb(skb); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2698 | return rc; |
| 2699 | } |
| 2700 | |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2701 | static int pn533_tm_send_complete(struct pn533 *dev, void *arg, |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2702 | struct sk_buff *resp) |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2703 | { |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2704 | u8 status; |
Thierry Escande | 5b412fd | 2012-11-15 18:24:28 +0100 | [diff] [blame] | 2705 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2706 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2707 | |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2708 | if (IS_ERR(resp)) |
| 2709 | return PTR_ERR(resp); |
Thierry Escande | 5b412fd | 2012-11-15 18:24:28 +0100 | [diff] [blame] | 2710 | |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2711 | status = resp->data[0]; |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2712 | |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 2713 | /* Prepare for the next round */ |
| 2714 | if (skb_queue_len(&dev->fragment_skb) > 0) { |
| 2715 | queue_work(dev->wq, &dev->mi_tm_tx_work); |
| 2716 | return -EINPROGRESS; |
| 2717 | } |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2718 | dev_kfree_skb(resp); |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2719 | |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2720 | if (status != 0) { |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2721 | nfc_tm_deactivated(dev->nfc_dev); |
| 2722 | |
Samuel Ortiz | 51ad304 | 2012-05-31 20:01:32 +0200 | [diff] [blame] | 2723 | dev->tgt_mode = 0; |
| 2724 | |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2725 | return 0; |
| 2726 | } |
| 2727 | |
| 2728 | queue_work(dev->wq, &dev->tg_work); |
| 2729 | |
| 2730 | return 0; |
| 2731 | } |
| 2732 | |
| 2733 | static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb) |
| 2734 | { |
| 2735 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2736 | int rc; |
| 2737 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2738 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2739 | |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 2740 | /* let's split in multiple chunks if size's too big */ |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2741 | if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) { |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 2742 | rc = pn533_fill_fragment_skbs(dev, skb); |
| 2743 | if (rc <= 0) |
| 2744 | goto error; |
| 2745 | |
| 2746 | /* get the first skb */ |
| 2747 | skb = skb_dequeue(&dev->fragment_skb); |
| 2748 | if (!skb) { |
| 2749 | rc = -EIO; |
| 2750 | goto error; |
| 2751 | } |
| 2752 | |
| 2753 | rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_META_DATA, skb, |
| 2754 | pn533_tm_send_complete, NULL); |
| 2755 | } else { |
| 2756 | /* Send th skb */ |
| 2757 | rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb, |
| 2758 | pn533_tm_send_complete, NULL); |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2759 | } |
| 2760 | |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 2761 | error: |
| 2762 | if (rc < 0) { |
Waldemar Rymarkiewicz | e487882 | 2012-12-10 14:42:44 +0100 | [diff] [blame] | 2763 | dev_kfree_skb(skb); |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 2764 | skb_queue_purge(&dev->fragment_skb); |
| 2765 | } |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 2766 | |
| 2767 | return rc; |
| 2768 | } |
| 2769 | |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2770 | static void pn533_wq_mi_recv(struct work_struct *work) |
| 2771 | { |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2772 | struct pn533 *dev = container_of(work, struct pn533, mi_rx_work); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2773 | struct sk_buff *skb; |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2774 | int rc; |
| 2775 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2776 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2777 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2778 | skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN); |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2779 | if (!skb) |
| 2780 | goto error; |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2781 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2782 | switch (dev->device_type) { |
| 2783 | case PN533_DEVICE_PASORI: |
| 2784 | if (dev->tgt_active_prot == NFC_PROTO_FELICA) { |
| 2785 | rc = pn533_send_cmd_direct_async(dev, |
| 2786 | PN533_CMD_IN_COMM_THRU, |
| 2787 | skb, |
| 2788 | pn533_data_exchange_complete, |
| 2789 | dev->cmd_complete_mi_arg); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2790 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2791 | break; |
| 2792 | } |
| 2793 | default: |
| 2794 | *skb_put(skb, sizeof(u8)) = 1; /*TG*/ |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2795 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2796 | rc = pn533_send_cmd_direct_async(dev, |
| 2797 | PN533_CMD_IN_DATA_EXCHANGE, |
| 2798 | skb, |
| 2799 | pn533_data_exchange_complete, |
| 2800 | dev->cmd_complete_mi_arg); |
Waldemar Rymarkiewicz | b1bb290 | 2012-11-26 14:18:32 +0100 | [diff] [blame] | 2801 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2802 | break; |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2803 | } |
| 2804 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2805 | if (rc == 0) /* success */ |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2806 | return; |
| 2807 | |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2808 | nfc_err(&dev->interface->dev, |
| 2809 | "Error %d when trying to perform data_exchange\n", rc); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2810 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2811 | dev_kfree_skb(skb); |
Waldemar Rymarkiewicz | 140ef7f | 2013-04-03 08:02:07 +0200 | [diff] [blame] | 2812 | kfree(dev->cmd_complete_mi_arg); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2813 | |
Waldemar Rymarkiewicz | b1e666f | 2012-12-10 14:42:46 +0100 | [diff] [blame] | 2814 | error: |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2815 | pn533_send_ack(dev, GFP_KERNEL); |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 2816 | queue_work(dev->wq, &dev->cmd_work); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 2817 | } |
| 2818 | |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2819 | static void pn533_wq_mi_send(struct work_struct *work) |
| 2820 | { |
| 2821 | struct pn533 *dev = container_of(work, struct pn533, mi_tx_work); |
| 2822 | struct sk_buff *skb; |
| 2823 | int rc; |
| 2824 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2825 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2826 | |
| 2827 | /* Grab the first skb in the queue */ |
| 2828 | skb = skb_dequeue(&dev->fragment_skb); |
| 2829 | |
| 2830 | if (skb == NULL) { /* No more data */ |
| 2831 | /* Reset the queue for future use */ |
| 2832 | skb_queue_head_init(&dev->fragment_skb); |
| 2833 | goto error; |
| 2834 | } |
| 2835 | |
| 2836 | switch (dev->device_type) { |
| 2837 | case PN533_DEVICE_PASORI: |
| 2838 | if (dev->tgt_active_prot != NFC_PROTO_FELICA) { |
| 2839 | rc = -EIO; |
| 2840 | break; |
| 2841 | } |
| 2842 | |
| 2843 | rc = pn533_send_cmd_direct_async(dev, PN533_CMD_IN_COMM_THRU, |
| 2844 | skb, |
| 2845 | pn533_data_exchange_complete, |
| 2846 | dev->cmd_complete_dep_arg); |
| 2847 | |
| 2848 | break; |
| 2849 | |
| 2850 | default: |
| 2851 | /* Still some fragments? */ |
| 2852 | rc = pn533_send_cmd_direct_async(dev,PN533_CMD_IN_DATA_EXCHANGE, |
| 2853 | skb, |
| 2854 | pn533_data_exchange_complete, |
| 2855 | dev->cmd_complete_dep_arg); |
| 2856 | |
| 2857 | break; |
| 2858 | } |
| 2859 | |
| 2860 | if (rc == 0) /* success */ |
| 2861 | return; |
| 2862 | |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2863 | nfc_err(&dev->interface->dev, |
| 2864 | "Error %d when trying to perform data_exchange\n", rc); |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 2865 | |
| 2866 | dev_kfree_skb(skb); |
| 2867 | kfree(dev->cmd_complete_dep_arg); |
| 2868 | |
| 2869 | error: |
| 2870 | pn533_send_ack(dev, GFP_KERNEL); |
| 2871 | queue_work(dev->wq, &dev->cmd_work); |
| 2872 | } |
| 2873 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2874 | static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata, |
| 2875 | u8 cfgdata_len) |
| 2876 | { |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2877 | struct sk_buff *skb; |
| 2878 | struct sk_buff *resp; |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2879 | int skb_len; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2880 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2881 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2882 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2883 | skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */ |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2884 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2885 | skb = pn533_alloc_skb(dev, skb_len); |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2886 | if (!skb) |
| 2887 | return -ENOMEM; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2888 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2889 | *skb_put(skb, sizeof(cfgitem)) = cfgitem; |
| 2890 | memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2891 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2892 | resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb); |
| 2893 | if (IS_ERR(resp)) |
| 2894 | return PTR_ERR(resp); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2895 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2896 | dev_kfree_skb(resp); |
| 2897 | return 0; |
| 2898 | } |
| 2899 | |
| 2900 | static int pn533_get_firmware_version(struct pn533 *dev, |
| 2901 | struct pn533_fw_version *fv) |
| 2902 | { |
| 2903 | struct sk_buff *skb; |
| 2904 | struct sk_buff *resp; |
| 2905 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2906 | skb = pn533_alloc_skb(dev, 0); |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2907 | if (!skb) |
| 2908 | return -ENOMEM; |
| 2909 | |
| 2910 | resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb); |
| 2911 | if (IS_ERR(resp)) |
| 2912 | return PTR_ERR(resp); |
| 2913 | |
| 2914 | fv->ic = resp->data[0]; |
| 2915 | fv->ver = resp->data[1]; |
| 2916 | fv->rev = resp->data[2]; |
| 2917 | fv->support = resp->data[3]; |
| 2918 | |
| 2919 | dev_kfree_skb(resp); |
| 2920 | return 0; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 2921 | } |
| 2922 | |
Waldemar Rymarkiewicz | f75c291 | 2013-04-03 08:02:02 +0200 | [diff] [blame] | 2923 | static int pn533_pasori_fw_reset(struct pn533 *dev) |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2924 | { |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2925 | struct sk_buff *skb; |
| 2926 | struct sk_buff *resp; |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2927 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2928 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2929 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 2930 | skb = pn533_alloc_skb(dev, sizeof(u8)); |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2931 | if (!skb) |
| 2932 | return -ENOMEM; |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2933 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2934 | *skb_put(skb, sizeof(u8)) = 0x1; |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2935 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2936 | resp = pn533_send_cmd_sync(dev, 0x18, skb); |
| 2937 | if (IS_ERR(resp)) |
| 2938 | return PTR_ERR(resp); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2939 | |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 2940 | dev_kfree_skb(resp); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2941 | |
Waldemar Rymarkiewicz | 94c5c15 | 2012-11-26 14:18:36 +0100 | [diff] [blame] | 2942 | return 0; |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 2943 | } |
| 2944 | |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 2945 | struct pn533_acr122_poweron_rdr_arg { |
| 2946 | int rc; |
| 2947 | struct completion done; |
| 2948 | }; |
| 2949 | |
| 2950 | static void pn533_acr122_poweron_rdr_resp(struct urb *urb) |
| 2951 | { |
| 2952 | struct pn533_acr122_poweron_rdr_arg *arg = urb->context; |
| 2953 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2954 | dev_dbg(&urb->dev->dev, "%s\n", __func__); |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 2955 | |
Olivier Guiter | 86eca4e | 2013-06-03 12:02:29 +0200 | [diff] [blame] | 2956 | print_hex_dump_debug("ACR122 RX: ", DUMP_PREFIX_NONE, 16, 1, |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 2957 | urb->transfer_buffer, urb->transfer_buffer_length, |
| 2958 | false); |
| 2959 | |
| 2960 | arg->rc = urb->status; |
| 2961 | complete(&arg->done); |
| 2962 | } |
| 2963 | |
| 2964 | static int pn533_acr122_poweron_rdr(struct pn533 *dev) |
| 2965 | { |
| 2966 | /* Power on th reader (CCID cmd) */ |
| 2967 | u8 cmd[10] = {PN533_ACR122_PC_TO_RDR_ICCPOWERON, |
| 2968 | 0, 0, 0, 0, 0, 0, 3, 0, 0}; |
| 2969 | u8 buf[255]; |
| 2970 | int rc; |
| 2971 | void *cntx; |
| 2972 | struct pn533_acr122_poweron_rdr_arg arg; |
| 2973 | |
Joe Perches | b483483 | 2013-04-05 12:27:37 -0700 | [diff] [blame] | 2974 | dev_dbg(&dev->interface->dev, "%s\n", __func__); |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 2975 | |
| 2976 | init_completion(&arg.done); |
| 2977 | cntx = dev->in_urb->context; /* backup context */ |
| 2978 | |
| 2979 | dev->in_urb->transfer_buffer = buf; |
| 2980 | dev->in_urb->transfer_buffer_length = 255; |
| 2981 | dev->in_urb->complete = pn533_acr122_poweron_rdr_resp; |
| 2982 | dev->in_urb->context = &arg; |
| 2983 | |
| 2984 | dev->out_urb->transfer_buffer = cmd; |
| 2985 | dev->out_urb->transfer_buffer_length = sizeof(cmd); |
| 2986 | |
Olivier Guiter | 86eca4e | 2013-06-03 12:02:29 +0200 | [diff] [blame] | 2987 | print_hex_dump_debug("ACR122 TX: ", DUMP_PREFIX_NONE, 16, 1, |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 2988 | cmd, sizeof(cmd), false); |
| 2989 | |
| 2990 | rc = usb_submit_urb(dev->out_urb, GFP_KERNEL); |
| 2991 | if (rc) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2992 | nfc_err(&dev->interface->dev, |
| 2993 | "Reader power on cmd error %d\n", rc); |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 2994 | return rc; |
| 2995 | } |
| 2996 | |
| 2997 | rc = usb_submit_urb(dev->in_urb, GFP_KERNEL); |
| 2998 | if (rc) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 2999 | nfc_err(&dev->interface->dev, |
| 3000 | "Can't submit reader poweron cmd response %d\n", rc); |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 3001 | return rc; |
| 3002 | } |
| 3003 | |
| 3004 | wait_for_completion(&arg.done); |
| 3005 | dev->in_urb->context = cntx; /* restore context */ |
| 3006 | |
| 3007 | return arg.rc; |
| 3008 | } |
| 3009 | |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 3010 | static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf) |
| 3011 | { |
| 3012 | struct pn533 *dev = nfc_get_drvdata(nfc_dev); |
| 3013 | u8 rf_field = !!rf; |
| 3014 | int rc; |
| 3015 | |
Samuel Ortiz | 3a8eab3 | 2013-07-01 17:26:58 +0200 | [diff] [blame] | 3016 | rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA; |
| 3017 | |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 3018 | rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD, |
| 3019 | (u8 *)&rf_field, 1); |
| 3020 | if (rc) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3021 | nfc_err(&dev->interface->dev, "Error on setting RF field\n"); |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 3022 | return rc; |
| 3023 | } |
| 3024 | |
| 3025 | return rc; |
| 3026 | } |
| 3027 | |
Sachin Kamat | e44666b | 2013-09-20 14:30:41 +0530 | [diff] [blame] | 3028 | static int pn533_dev_up(struct nfc_dev *nfc_dev) |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 3029 | { |
| 3030 | return pn533_rf_field(nfc_dev, 1); |
| 3031 | } |
| 3032 | |
Sachin Kamat | e44666b | 2013-09-20 14:30:41 +0530 | [diff] [blame] | 3033 | static int pn533_dev_down(struct nfc_dev *nfc_dev) |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 3034 | { |
| 3035 | return pn533_rf_field(nfc_dev, 0); |
| 3036 | } |
| 3037 | |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3038 | static struct nfc_ops pn533_nfc_ops = { |
Samuel Ortiz | 60d9edd5 | 2013-04-11 11:45:41 +0200 | [diff] [blame] | 3039 | .dev_up = pn533_dev_up, |
| 3040 | .dev_down = pn533_dev_down, |
Samuel Ortiz | 361f3cb | 2011-12-14 16:43:11 +0100 | [diff] [blame] | 3041 | .dep_link_up = pn533_dep_link_up, |
| 3042 | .dep_link_down = pn533_dep_link_down, |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3043 | .start_poll = pn533_start_poll, |
| 3044 | .stop_poll = pn533_stop_poll, |
| 3045 | .activate_target = pn533_activate_target, |
| 3046 | .deactivate_target = pn533_deactivate_target, |
Samuel Ortiz | be9ae4c | 2012-05-16 15:55:48 +0200 | [diff] [blame] | 3047 | .im_transceive = pn533_transceive, |
Samuel Ortiz | dadb06f | 2012-05-31 00:09:11 +0200 | [diff] [blame] | 3048 | .tm_send = pn533_tm_send, |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3049 | }; |
| 3050 | |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3051 | static int pn533_setup(struct pn533 *dev) |
| 3052 | { |
| 3053 | struct pn533_config_max_retries max_retries; |
| 3054 | struct pn533_config_timing timing; |
| 3055 | u8 pasori_cfg[3] = {0x08, 0x01, 0x08}; |
| 3056 | int rc; |
| 3057 | |
| 3058 | switch (dev->device_type) { |
| 3059 | case PN533_DEVICE_STD: |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3060 | case PN533_DEVICE_PASORI: |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 3061 | case PN533_DEVICE_ACR122U: |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3062 | max_retries.mx_rty_atr = 0x2; |
| 3063 | max_retries.mx_rty_psl = 0x1; |
| 3064 | max_retries.mx_rty_passive_act = |
| 3065 | PN533_CONFIG_MAX_RETRIES_NO_RETRY; |
| 3066 | |
| 3067 | timing.rfu = PN533_CONFIG_TIMING_102; |
| 3068 | timing.atr_res_timeout = PN533_CONFIG_TIMING_102; |
| 3069 | timing.dep_timeout = PN533_CONFIG_TIMING_204; |
| 3070 | |
| 3071 | break; |
| 3072 | |
| 3073 | default: |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3074 | nfc_err(&dev->interface->dev, "Unknown device type %d\n", |
| 3075 | dev->device_type); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3076 | return -EINVAL; |
| 3077 | } |
| 3078 | |
| 3079 | rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES, |
| 3080 | (u8 *)&max_retries, sizeof(max_retries)); |
| 3081 | if (rc) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3082 | nfc_err(&dev->interface->dev, |
| 3083 | "Error on setting MAX_RETRIES config\n"); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3084 | return rc; |
| 3085 | } |
| 3086 | |
| 3087 | |
| 3088 | rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING, |
| 3089 | (u8 *)&timing, sizeof(timing)); |
| 3090 | if (rc) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3091 | nfc_err(&dev->interface->dev, "Error on setting RF timings\n"); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3092 | return rc; |
| 3093 | } |
| 3094 | |
| 3095 | switch (dev->device_type) { |
| 3096 | case PN533_DEVICE_STD: |
| 3097 | break; |
| 3098 | |
| 3099 | case PN533_DEVICE_PASORI: |
Waldemar Rymarkiewicz | f75c291 | 2013-04-03 08:02:02 +0200 | [diff] [blame] | 3100 | pn533_pasori_fw_reset(dev); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3101 | |
| 3102 | rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI, |
| 3103 | pasori_cfg, 3); |
| 3104 | if (rc) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3105 | nfc_err(&dev->interface->dev, |
| 3106 | "Error while settings PASORI config\n"); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3107 | return rc; |
| 3108 | } |
| 3109 | |
Waldemar Rymarkiewicz | f75c291 | 2013-04-03 08:02:02 +0200 | [diff] [blame] | 3110 | pn533_pasori_fw_reset(dev); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3111 | |
| 3112 | break; |
| 3113 | } |
| 3114 | |
| 3115 | return 0; |
| 3116 | } |
| 3117 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3118 | static int pn533_probe(struct usb_interface *interface, |
| 3119 | const struct usb_device_id *id) |
| 3120 | { |
Waldemar Rymarkiewicz | cb950d9 | 2012-11-26 14:18:38 +0100 | [diff] [blame] | 3121 | struct pn533_fw_version fw_ver; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3122 | struct pn533 *dev; |
| 3123 | struct usb_host_interface *iface_desc; |
| 3124 | struct usb_endpoint_descriptor *endpoint; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3125 | int in_endpoint = 0; |
| 3126 | int out_endpoint = 0; |
| 3127 | int rc = -ENOMEM; |
| 3128 | int i; |
| 3129 | u32 protocols; |
| 3130 | |
| 3131 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 3132 | if (!dev) |
| 3133 | return -ENOMEM; |
| 3134 | |
| 3135 | dev->udev = usb_get_dev(interface_to_usbdev(interface)); |
| 3136 | dev->interface = interface; |
Samuel Ortiz | 0201ed0 | 2012-05-31 17:56:46 +0200 | [diff] [blame] | 3137 | mutex_init(&dev->cmd_lock); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3138 | |
| 3139 | iface_desc = interface->cur_altsetting; |
| 3140 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
| 3141 | endpoint = &iface_desc->endpoint[i].desc; |
| 3142 | |
Waldemar Rymarkiewicz | 8d25ca7 | 2012-11-26 14:18:30 +0100 | [diff] [blame] | 3143 | if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3144 | in_endpoint = endpoint->bEndpointAddress; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3145 | |
Waldemar Rymarkiewicz | 8d25ca7 | 2012-11-26 14:18:30 +0100 | [diff] [blame] | 3146 | if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3147 | out_endpoint = endpoint->bEndpointAddress; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | if (!in_endpoint || !out_endpoint) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3151 | nfc_err(&interface->dev, |
| 3152 | "Could not find bulk-in or bulk-out endpoint\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3153 | rc = -ENODEV; |
| 3154 | goto error; |
| 3155 | } |
| 3156 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3157 | dev->in_urb = usb_alloc_urb(0, GFP_KERNEL); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3158 | dev->out_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 3159 | |
Waldemar Rymarkiewicz | a579809 | 2012-12-10 14:42:58 +0100 | [diff] [blame] | 3160 | if (!dev->in_urb || !dev->out_urb) |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3161 | goto error; |
| 3162 | |
| 3163 | usb_fill_bulk_urb(dev->in_urb, dev->udev, |
Waldemar Rymarkiewicz | 5d46774 | 2012-12-10 14:42:51 +0100 | [diff] [blame] | 3164 | usb_rcvbulkpipe(dev->udev, in_endpoint), |
| 3165 | NULL, 0, NULL, dev); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3166 | usb_fill_bulk_urb(dev->out_urb, dev->udev, |
Waldemar Rymarkiewicz | 5d46774 | 2012-12-10 14:42:51 +0100 | [diff] [blame] | 3167 | usb_sndbulkpipe(dev->udev, out_endpoint), |
| 3168 | NULL, 0, pn533_send_complete, dev); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3169 | |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 3170 | INIT_WORK(&dev->cmd_work, pn533_wq_cmd); |
| 3171 | INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete); |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 3172 | INIT_WORK(&dev->mi_rx_work, pn533_wq_mi_recv); |
| 3173 | INIT_WORK(&dev->mi_tx_work, pn533_wq_mi_send); |
Samuel Ortiz | 103b34c | 2012-05-31 00:07:51 +0200 | [diff] [blame] | 3174 | INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data); |
Olivier Guiter | 3c13b24 | 2013-09-23 12:24:38 +0200 | [diff] [blame] | 3175 | INIT_WORK(&dev->mi_tm_rx_work, pn533_wq_tm_mi_recv); |
Olivier Guiter | 93ad420 | 2013-09-23 12:24:39 +0200 | [diff] [blame] | 3176 | INIT_WORK(&dev->mi_tm_tx_work, pn533_wq_tm_mi_send); |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 3177 | INIT_DELAYED_WORK(&dev->poll_work, pn533_wq_poll); |
Samuel Ortiz | 17e9d9d | 2013-06-26 18:16:21 +0200 | [diff] [blame] | 3178 | INIT_WORK(&dev->rf_work, pn533_wq_rf); |
Tejun Heo | 58637c9 | 2012-08-22 16:28:46 -0700 | [diff] [blame] | 3179 | dev->wq = alloc_ordered_workqueue("pn533", 0); |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 3180 | if (dev->wq == NULL) |
| 3181 | goto error; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3182 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 3183 | init_timer(&dev->listen_timer); |
| 3184 | dev->listen_timer.data = (unsigned long) dev; |
| 3185 | dev->listen_timer.function = pn533_listen_mode_timer; |
| 3186 | |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 3187 | skb_queue_head_init(&dev->resp_q); |
Olivier Guiter | 963a82e | 2013-06-13 13:43:28 +0000 | [diff] [blame] | 3188 | skb_queue_head_init(&dev->fragment_skb); |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 3189 | |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 3190 | INIT_LIST_HEAD(&dev->cmd_queue); |
| 3191 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3192 | usb_set_intfdata(interface, dev); |
| 3193 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 3194 | dev->ops = &pn533_std_frame_ops; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3195 | |
Waldemar Rymarkiewicz | 5852037 | 2013-04-03 08:02:12 +0200 | [diff] [blame] | 3196 | dev->protocol_type = PN533_PROTO_REQ_ACK_RESP; |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3197 | dev->device_type = id->driver_info; |
| 3198 | switch (dev->device_type) { |
| 3199 | case PN533_DEVICE_STD: |
| 3200 | protocols = PN533_ALL_PROTOCOLS; |
| 3201 | break; |
| 3202 | |
| 3203 | case PN533_DEVICE_PASORI: |
| 3204 | protocols = PN533_NO_TYPE_B_PROTOCOLS; |
| 3205 | break; |
| 3206 | |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 3207 | case PN533_DEVICE_ACR122U: |
| 3208 | protocols = PN533_NO_TYPE_B_PROTOCOLS; |
| 3209 | dev->ops = &pn533_acr122_frame_ops; |
| 3210 | dev->protocol_type = PN533_PROTO_REQ_RESP, |
| 3211 | |
| 3212 | rc = pn533_acr122_poweron_rdr(dev); |
| 3213 | if (rc < 0) { |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3214 | nfc_err(&dev->interface->dev, |
| 3215 | "Couldn't poweron the reader (error %d)\n", rc); |
Waldemar Rymarkiewicz | 53cf483 | 2013-04-03 08:02:13 +0200 | [diff] [blame] | 3216 | goto destroy_wq; |
| 3217 | } |
| 3218 | break; |
| 3219 | |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3220 | default: |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3221 | nfc_err(&dev->interface->dev, "Unknown device type %d\n", |
| 3222 | dev->device_type); |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3223 | rc = -EINVAL; |
| 3224 | goto destroy_wq; |
| 3225 | } |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3226 | |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 3227 | memset(&fw_ver, 0, sizeof(fw_ver)); |
| 3228 | rc = pn533_get_firmware_version(dev, &fw_ver); |
| 3229 | if (rc < 0) |
| 3230 | goto destroy_wq; |
| 3231 | |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3232 | nfc_info(&dev->interface->dev, |
| 3233 | "NXP PN5%02X firmware ver %d.%d now attached\n", |
| 3234 | fw_ver.ic, fw_ver.ver, fw_ver.rev); |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 3235 | |
| 3236 | |
Samuel Ortiz | e875304 | 2011-08-19 15:47:11 +0200 | [diff] [blame] | 3237 | dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols, |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 3238 | dev->ops->tx_header_len + |
Samuel Ortiz | e875304 | 2011-08-19 15:47:11 +0200 | [diff] [blame] | 3239 | PN533_CMD_DATAEXCH_HEAD_LEN, |
Waldemar Rymarkiewicz | 9e2d493 | 2012-12-10 14:43:01 +0100 | [diff] [blame] | 3240 | dev->ops->tx_tail_len); |
Wei Yongjun | 4674d0f | 2013-05-07 20:07:52 +0800 | [diff] [blame] | 3241 | if (!dev->nfc_dev) { |
| 3242 | rc = -ENOMEM; |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 3243 | goto destroy_wq; |
Wei Yongjun | 4674d0f | 2013-05-07 20:07:52 +0800 | [diff] [blame] | 3244 | } |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3245 | |
| 3246 | nfc_set_parent_dev(dev->nfc_dev, &interface->dev); |
| 3247 | nfc_set_drvdata(dev->nfc_dev, dev); |
| 3248 | |
| 3249 | rc = nfc_register_device(dev->nfc_dev); |
| 3250 | if (rc) |
| 3251 | goto free_nfc_dev; |
| 3252 | |
Samuel Ortiz | 5c7b053 | 2012-07-02 20:04:01 +0200 | [diff] [blame] | 3253 | rc = pn533_setup(dev); |
| 3254 | if (rc) |
Samuel Ortiz | 9f2f8ba | 2012-05-29 21:28:58 +0200 | [diff] [blame] | 3255 | goto unregister_nfc_dev; |
Samuel Ortiz | 34a85bf | 2012-05-29 21:34:08 +0200 | [diff] [blame] | 3256 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3257 | return 0; |
| 3258 | |
Samuel Ortiz | 9f2f8ba | 2012-05-29 21:28:58 +0200 | [diff] [blame] | 3259 | unregister_nfc_dev: |
| 3260 | nfc_unregister_device(dev->nfc_dev); |
| 3261 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3262 | free_nfc_dev: |
| 3263 | nfc_free_device(dev->nfc_dev); |
Samuel Ortiz | 9f2f8ba | 2012-05-29 21:28:58 +0200 | [diff] [blame] | 3264 | |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 3265 | destroy_wq: |
| 3266 | destroy_workqueue(dev->wq); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3267 | error: |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3268 | usb_free_urb(dev->in_urb); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3269 | usb_free_urb(dev->out_urb); |
Marina Makienko | 7c5a54f | 2013-02-26 11:41:18 +0400 | [diff] [blame] | 3270 | usb_put_dev(dev->udev); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3271 | kfree(dev); |
| 3272 | return rc; |
| 3273 | } |
| 3274 | |
| 3275 | static void pn533_disconnect(struct usb_interface *interface) |
| 3276 | { |
| 3277 | struct pn533 *dev; |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 3278 | struct pn533_cmd *cmd, *n; |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3279 | |
| 3280 | dev = usb_get_intfdata(interface); |
| 3281 | usb_set_intfdata(interface, NULL); |
| 3282 | |
| 3283 | nfc_unregister_device(dev->nfc_dev); |
| 3284 | nfc_free_device(dev->nfc_dev); |
| 3285 | |
| 3286 | usb_kill_urb(dev->in_urb); |
| 3287 | usb_kill_urb(dev->out_urb); |
| 3288 | |
Samuel Ortiz | 46f793b | 2013-07-03 14:50:36 +0200 | [diff] [blame] | 3289 | flush_delayed_work(&dev->poll_work); |
Samuel Ortiz | 4849f85 | 2012-04-10 19:43:17 +0200 | [diff] [blame] | 3290 | destroy_workqueue(dev->wq); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3291 | |
Samuel Ortiz | 6ff73fd | 2012-04-10 19:43:18 +0200 | [diff] [blame] | 3292 | skb_queue_purge(&dev->resp_q); |
| 3293 | |
Samuel Ortiz | 6fbbdc1 | 2012-05-30 17:20:25 +0200 | [diff] [blame] | 3294 | del_timer(&dev->listen_timer); |
| 3295 | |
Samuel Ortiz | 5d50b36 | 2012-08-17 23:47:54 +0200 | [diff] [blame] | 3296 | list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) { |
| 3297 | list_del(&cmd->queue); |
| 3298 | kfree(cmd); |
| 3299 | } |
| 3300 | |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3301 | usb_free_urb(dev->in_urb); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3302 | usb_free_urb(dev->out_urb); |
| 3303 | kfree(dev); |
| 3304 | |
Joe Perches | 073a625 | 2013-04-05 12:27:38 -0700 | [diff] [blame] | 3305 | nfc_info(&interface->dev, "NXP PN533 NFC device disconnected\n"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3306 | } |
| 3307 | |
| 3308 | static struct usb_driver pn533_driver = { |
| 3309 | .name = "pn533", |
| 3310 | .probe = pn533_probe, |
| 3311 | .disconnect = pn533_disconnect, |
| 3312 | .id_table = pn533_table, |
| 3313 | }; |
| 3314 | |
Greg Kroah-Hartman | fe74848 | 2011-11-18 09:52:10 -0800 | [diff] [blame] | 3315 | module_usb_driver(pn533_driver); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3316 | |
Waldemar Rymarkiewicz | e70b96e | 2013-04-03 08:02:01 +0200 | [diff] [blame] | 3317 | MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>"); |
| 3318 | MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>"); |
| 3319 | MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>"); |
Aloisio Almeida Jr | c46ee38 | 2011-07-01 19:31:37 -0300 | [diff] [blame] | 3320 | MODULE_DESCRIPTION("PN533 usb driver ver " VERSION); |
| 3321 | MODULE_VERSION(VERSION); |
| 3322 | MODULE_LICENSE("GPL"); |