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