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