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