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