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