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