Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / drivers / nfc / port100.c
blob87d509996704039c3ac224dcb95bb4b096570542
1 /*
2 * Sony NFC Port-100 Series driver
3 * Copyright (c) 2013, Intel Corporation.
5 * Partly based/Inspired by Stephen Tiedemann's nfcpy
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
18 #include <linux/module.h>
19 #include <linux/usb.h>
20 #include <net/nfc/digital.h>
22 #define VERSION "0.1"
24 #define SONY_VENDOR_ID 0x054c
25 #define RCS380_PRODUCT_ID 0x06c1
27 #define PORT100_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
28 NFC_PROTO_MIFARE_MASK | \
29 NFC_PROTO_FELICA_MASK | \
30 NFC_PROTO_NFC_DEP_MASK | \
31 NFC_PROTO_ISO14443_MASK | \
32 NFC_PROTO_ISO14443_B_MASK)
34 #define PORT100_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC | \
35 NFC_DIGITAL_DRV_CAPS_TG_CRC)
37 /* Standard port100 frame definitions */
38 #define PORT100_FRAME_HEADER_LEN (sizeof(struct port100_frame) \
39 + 2) /* data[0] CC, data[1] SCC */
40 #define PORT100_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
42 #define PORT100_COMM_RF_HEAD_MAX_LEN (sizeof(struct port100_tg_comm_rf_cmd))
45 * Max extended frame payload len, excluding CC and SCC
46 * which are already in PORT100_FRAME_HEADER_LEN.
48 #define PORT100_FRAME_MAX_PAYLOAD_LEN 1001
50 #define PORT100_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
51 Postamble (1) */
52 static u8 ack_frame[PORT100_FRAME_ACK_SIZE] = {
53 0x00, 0x00, 0xff, 0x00, 0xff, 0x00
56 #define PORT100_FRAME_CHECKSUM(f) (f->data[le16_to_cpu(f->datalen)])
57 #define PORT100_FRAME_POSTAMBLE(f) (f->data[le16_to_cpu(f->datalen) + 1])
59 /* start of frame */
60 #define PORT100_FRAME_SOF 0x00FF
61 #define PORT100_FRAME_EXT 0xFFFF
62 #define PORT100_FRAME_ACK 0x00FF
64 /* Port-100 command: in or out */
65 #define PORT100_FRAME_DIRECTION(f) (f->data[0]) /* CC */
66 #define PORT100_FRAME_DIR_OUT 0xD6
67 #define PORT100_FRAME_DIR_IN 0xD7
69 /* Port-100 sub-command */
70 #define PORT100_FRAME_CMD(f) (f->data[1]) /* SCC */
72 #define PORT100_CMD_GET_FIRMWARE_VERSION 0x20
73 #define PORT100_CMD_GET_COMMAND_TYPE 0x28
74 #define PORT100_CMD_SET_COMMAND_TYPE 0x2A
76 #define PORT100_CMD_IN_SET_RF 0x00
77 #define PORT100_CMD_IN_SET_PROTOCOL 0x02
78 #define PORT100_CMD_IN_COMM_RF 0x04
80 #define PORT100_CMD_TG_SET_RF 0x40
81 #define PORT100_CMD_TG_SET_PROTOCOL 0x42
82 #define PORT100_CMD_TG_SET_RF_OFF 0x46
83 #define PORT100_CMD_TG_COMM_RF 0x48
85 #define PORT100_CMD_SWITCH_RF 0x06
87 #define PORT100_CMD_RESPONSE(cmd) (cmd + 1)
89 #define PORT100_CMD_TYPE_IS_SUPPORTED(mask, cmd_type) \
90 ((mask) & (0x01 << (cmd_type)))
91 #define PORT100_CMD_TYPE_0 0
92 #define PORT100_CMD_TYPE_1 1
94 #define PORT100_CMD_STATUS_OK 0x00
95 #define PORT100_CMD_STATUS_TIMEOUT 0x80
97 #define PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK 0x01
98 #define PORT100_MDAA_TGT_WAS_ACTIVATED_MASK 0x02
100 struct port100;
102 typedef void (*port100_send_async_complete_t)(struct port100 *dev, void *arg,
103 struct sk_buff *resp);
106 * Setting sets structure for in_set_rf command
108 * @in_*_set_number: Represent the entry indexes in the port-100 RF Base Table.
109 * This table contains multiple RF setting sets required for RF
110 * communication.
112 * @in_*_comm_type: Theses fields set the communication type to be used.
114 struct port100_in_rf_setting {
115 u8 in_send_set_number;
116 u8 in_send_comm_type;
117 u8 in_recv_set_number;
118 u8 in_recv_comm_type;
119 } __packed;
121 #define PORT100_COMM_TYPE_IN_212F 0x01
122 #define PORT100_COMM_TYPE_IN_424F 0x02
123 #define PORT100_COMM_TYPE_IN_106A 0x03
124 #define PORT100_COMM_TYPE_IN_106B 0x07
126 static const struct port100_in_rf_setting in_rf_settings[] = {
127 [NFC_DIGITAL_RF_TECH_212F] = {
128 .in_send_set_number = 1,
129 .in_send_comm_type = PORT100_COMM_TYPE_IN_212F,
130 .in_recv_set_number = 15,
131 .in_recv_comm_type = PORT100_COMM_TYPE_IN_212F,
133 [NFC_DIGITAL_RF_TECH_424F] = {
134 .in_send_set_number = 1,
135 .in_send_comm_type = PORT100_COMM_TYPE_IN_424F,
136 .in_recv_set_number = 15,
137 .in_recv_comm_type = PORT100_COMM_TYPE_IN_424F,
139 [NFC_DIGITAL_RF_TECH_106A] = {
140 .in_send_set_number = 2,
141 .in_send_comm_type = PORT100_COMM_TYPE_IN_106A,
142 .in_recv_set_number = 15,
143 .in_recv_comm_type = PORT100_COMM_TYPE_IN_106A,
145 [NFC_DIGITAL_RF_TECH_106B] = {
146 .in_send_set_number = 3,
147 .in_send_comm_type = PORT100_COMM_TYPE_IN_106B,
148 .in_recv_set_number = 15,
149 .in_recv_comm_type = PORT100_COMM_TYPE_IN_106B,
151 /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
152 [NFC_DIGITAL_RF_TECH_LAST] = { 0 },
156 * Setting sets structure for tg_set_rf command
158 * @tg_set_number: Represents the entry index in the port-100 RF Base Table.
159 * This table contains multiple RF setting sets required for RF
160 * communication. this field is used for both send and receive
161 * settings.
163 * @tg_comm_type: Sets the communication type to be used to send and receive
164 * data.
166 struct port100_tg_rf_setting {
167 u8 tg_set_number;
168 u8 tg_comm_type;
169 } __packed;
171 #define PORT100_COMM_TYPE_TG_106A 0x0B
172 #define PORT100_COMM_TYPE_TG_212F 0x0C
173 #define PORT100_COMM_TYPE_TG_424F 0x0D
175 static const struct port100_tg_rf_setting tg_rf_settings[] = {
176 [NFC_DIGITAL_RF_TECH_106A] = {
177 .tg_set_number = 8,
178 .tg_comm_type = PORT100_COMM_TYPE_TG_106A,
180 [NFC_DIGITAL_RF_TECH_212F] = {
181 .tg_set_number = 8,
182 .tg_comm_type = PORT100_COMM_TYPE_TG_212F,
184 [NFC_DIGITAL_RF_TECH_424F] = {
185 .tg_set_number = 8,
186 .tg_comm_type = PORT100_COMM_TYPE_TG_424F,
188 /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
189 [NFC_DIGITAL_RF_TECH_LAST] = { 0 },
193 #define PORT100_IN_PROT_INITIAL_GUARD_TIME 0x00
194 #define PORT100_IN_PROT_ADD_CRC 0x01
195 #define PORT100_IN_PROT_CHECK_CRC 0x02
196 #define PORT100_IN_PROT_MULTI_CARD 0x03
197 #define PORT100_IN_PROT_ADD_PARITY 0x04
198 #define PORT100_IN_PROT_CHECK_PARITY 0x05
199 #define PORT100_IN_PROT_BITWISE_AC_RECV_MODE 0x06
200 #define PORT100_IN_PROT_VALID_BIT_NUMBER 0x07
201 #define PORT100_IN_PROT_CRYPTO1 0x08
202 #define PORT100_IN_PROT_ADD_SOF 0x09
203 #define PORT100_IN_PROT_CHECK_SOF 0x0A
204 #define PORT100_IN_PROT_ADD_EOF 0x0B
205 #define PORT100_IN_PROT_CHECK_EOF 0x0C
206 #define PORT100_IN_PROT_DEAF_TIME 0x0E
207 #define PORT100_IN_PROT_CRM 0x0F
208 #define PORT100_IN_PROT_CRM_MIN_LEN 0x10
209 #define PORT100_IN_PROT_T1_TAG_FRAME 0x11
210 #define PORT100_IN_PROT_RFCA 0x12
211 #define PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR 0x13
212 #define PORT100_IN_PROT_END 0x14
214 #define PORT100_IN_MAX_NUM_PROTOCOLS 19
216 #define PORT100_TG_PROT_TU 0x00
217 #define PORT100_TG_PROT_RF_OFF 0x01
218 #define PORT100_TG_PROT_CRM 0x02
219 #define PORT100_TG_PROT_END 0x03
221 #define PORT100_TG_MAX_NUM_PROTOCOLS 3
223 struct port100_protocol {
224 u8 number;
225 u8 value;
226 } __packed;
228 static struct port100_protocol
229 in_protocols[][PORT100_IN_MAX_NUM_PROTOCOLS + 1] = {
230 [NFC_DIGITAL_FRAMING_NFCA_SHORT] = {
231 { PORT100_IN_PROT_INITIAL_GUARD_TIME, 6 },
232 { PORT100_IN_PROT_ADD_CRC, 0 },
233 { PORT100_IN_PROT_CHECK_CRC, 0 },
234 { PORT100_IN_PROT_MULTI_CARD, 0 },
235 { PORT100_IN_PROT_ADD_PARITY, 0 },
236 { PORT100_IN_PROT_CHECK_PARITY, 1 },
237 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
238 { PORT100_IN_PROT_VALID_BIT_NUMBER, 7 },
239 { PORT100_IN_PROT_CRYPTO1, 0 },
240 { PORT100_IN_PROT_ADD_SOF, 0 },
241 { PORT100_IN_PROT_CHECK_SOF, 0 },
242 { PORT100_IN_PROT_ADD_EOF, 0 },
243 { PORT100_IN_PROT_CHECK_EOF, 0 },
244 { PORT100_IN_PROT_DEAF_TIME, 4 },
245 { PORT100_IN_PROT_CRM, 0 },
246 { PORT100_IN_PROT_CRM_MIN_LEN, 0 },
247 { PORT100_IN_PROT_T1_TAG_FRAME, 0 },
248 { PORT100_IN_PROT_RFCA, 0 },
249 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
250 { PORT100_IN_PROT_END, 0 },
252 [NFC_DIGITAL_FRAMING_NFCA_STANDARD] = {
253 { PORT100_IN_PROT_INITIAL_GUARD_TIME, 6 },
254 { PORT100_IN_PROT_ADD_CRC, 0 },
255 { PORT100_IN_PROT_CHECK_CRC, 0 },
256 { PORT100_IN_PROT_MULTI_CARD, 0 },
257 { PORT100_IN_PROT_ADD_PARITY, 1 },
258 { PORT100_IN_PROT_CHECK_PARITY, 1 },
259 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
260 { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
261 { PORT100_IN_PROT_CRYPTO1, 0 },
262 { PORT100_IN_PROT_ADD_SOF, 0 },
263 { PORT100_IN_PROT_CHECK_SOF, 0 },
264 { PORT100_IN_PROT_ADD_EOF, 0 },
265 { PORT100_IN_PROT_CHECK_EOF, 0 },
266 { PORT100_IN_PROT_DEAF_TIME, 4 },
267 { PORT100_IN_PROT_CRM, 0 },
268 { PORT100_IN_PROT_CRM_MIN_LEN, 0 },
269 { PORT100_IN_PROT_T1_TAG_FRAME, 0 },
270 { PORT100_IN_PROT_RFCA, 0 },
271 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
272 { PORT100_IN_PROT_END, 0 },
274 [NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A] = {
275 { PORT100_IN_PROT_INITIAL_GUARD_TIME, 6 },
276 { PORT100_IN_PROT_ADD_CRC, 1 },
277 { PORT100_IN_PROT_CHECK_CRC, 1 },
278 { PORT100_IN_PROT_MULTI_CARD, 0 },
279 { PORT100_IN_PROT_ADD_PARITY, 1 },
280 { PORT100_IN_PROT_CHECK_PARITY, 1 },
281 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
282 { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
283 { PORT100_IN_PROT_CRYPTO1, 0 },
284 { PORT100_IN_PROT_ADD_SOF, 0 },
285 { PORT100_IN_PROT_CHECK_SOF, 0 },
286 { PORT100_IN_PROT_ADD_EOF, 0 },
287 { PORT100_IN_PROT_CHECK_EOF, 0 },
288 { PORT100_IN_PROT_DEAF_TIME, 4 },
289 { PORT100_IN_PROT_CRM, 0 },
290 { PORT100_IN_PROT_CRM_MIN_LEN, 0 },
291 { PORT100_IN_PROT_T1_TAG_FRAME, 0 },
292 { PORT100_IN_PROT_RFCA, 0 },
293 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
294 { PORT100_IN_PROT_END, 0 },
296 [NFC_DIGITAL_FRAMING_NFCA_T1T] = {
297 /* nfc_digital_framing_nfca_short */
298 { PORT100_IN_PROT_ADD_CRC, 2 },
299 { PORT100_IN_PROT_CHECK_CRC, 2 },
300 { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
301 { PORT100_IN_PROT_T1_TAG_FRAME, 2 },
302 { PORT100_IN_PROT_END, 0 },
304 [NFC_DIGITAL_FRAMING_NFCA_T2T] = {
305 /* nfc_digital_framing_nfca_standard */
306 { PORT100_IN_PROT_ADD_CRC, 1 },
307 { PORT100_IN_PROT_CHECK_CRC, 0 },
308 { PORT100_IN_PROT_END, 0 },
310 [NFC_DIGITAL_FRAMING_NFCA_T4T] = {
311 /* nfc_digital_framing_nfca_standard_with_crc_a */
312 { PORT100_IN_PROT_END, 0 },
314 [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP] = {
315 /* nfc_digital_framing_nfca_standard */
316 { PORT100_IN_PROT_END, 0 },
318 [NFC_DIGITAL_FRAMING_NFCF] = {
319 { PORT100_IN_PROT_INITIAL_GUARD_TIME, 18 },
320 { PORT100_IN_PROT_ADD_CRC, 1 },
321 { PORT100_IN_PROT_CHECK_CRC, 1 },
322 { PORT100_IN_PROT_MULTI_CARD, 0 },
323 { PORT100_IN_PROT_ADD_PARITY, 0 },
324 { PORT100_IN_PROT_CHECK_PARITY, 0 },
325 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
326 { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
327 { PORT100_IN_PROT_CRYPTO1, 0 },
328 { PORT100_IN_PROT_ADD_SOF, 0 },
329 { PORT100_IN_PROT_CHECK_SOF, 0 },
330 { PORT100_IN_PROT_ADD_EOF, 0 },
331 { PORT100_IN_PROT_CHECK_EOF, 0 },
332 { PORT100_IN_PROT_DEAF_TIME, 4 },
333 { PORT100_IN_PROT_CRM, 0 },
334 { PORT100_IN_PROT_CRM_MIN_LEN, 0 },
335 { PORT100_IN_PROT_T1_TAG_FRAME, 0 },
336 { PORT100_IN_PROT_RFCA, 0 },
337 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
338 { PORT100_IN_PROT_END, 0 },
340 [NFC_DIGITAL_FRAMING_NFCF_T3T] = {
341 /* nfc_digital_framing_nfcf */
342 { PORT100_IN_PROT_END, 0 },
344 [NFC_DIGITAL_FRAMING_NFCF_NFC_DEP] = {
345 /* nfc_digital_framing_nfcf */
346 { PORT100_IN_PROT_END, 0 },
348 [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = {
349 { PORT100_IN_PROT_END, 0 },
351 [NFC_DIGITAL_FRAMING_NFCB] = {
352 { PORT100_IN_PROT_INITIAL_GUARD_TIME, 20 },
353 { PORT100_IN_PROT_ADD_CRC, 1 },
354 { PORT100_IN_PROT_CHECK_CRC, 1 },
355 { PORT100_IN_PROT_MULTI_CARD, 0 },
356 { PORT100_IN_PROT_ADD_PARITY, 0 },
357 { PORT100_IN_PROT_CHECK_PARITY, 0 },
358 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
359 { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
360 { PORT100_IN_PROT_CRYPTO1, 0 },
361 { PORT100_IN_PROT_ADD_SOF, 1 },
362 { PORT100_IN_PROT_CHECK_SOF, 1 },
363 { PORT100_IN_PROT_ADD_EOF, 1 },
364 { PORT100_IN_PROT_CHECK_EOF, 1 },
365 { PORT100_IN_PROT_DEAF_TIME, 4 },
366 { PORT100_IN_PROT_CRM, 0 },
367 { PORT100_IN_PROT_CRM_MIN_LEN, 0 },
368 { PORT100_IN_PROT_T1_TAG_FRAME, 0 },
369 { PORT100_IN_PROT_RFCA, 0 },
370 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
371 { PORT100_IN_PROT_END, 0 },
373 [NFC_DIGITAL_FRAMING_NFCB_T4T] = {
374 /* nfc_digital_framing_nfcb */
375 { PORT100_IN_PROT_END, 0 },
377 /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
378 [NFC_DIGITAL_FRAMING_LAST] = {
379 { PORT100_IN_PROT_END, 0 },
383 static struct port100_protocol
384 tg_protocols[][PORT100_TG_MAX_NUM_PROTOCOLS + 1] = {
385 [NFC_DIGITAL_FRAMING_NFCA_SHORT] = {
386 { PORT100_TG_PROT_END, 0 },
388 [NFC_DIGITAL_FRAMING_NFCA_STANDARD] = {
389 { PORT100_TG_PROT_END, 0 },
391 [NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A] = {
392 { PORT100_TG_PROT_END, 0 },
394 [NFC_DIGITAL_FRAMING_NFCA_T1T] = {
395 { PORT100_TG_PROT_END, 0 },
397 [NFC_DIGITAL_FRAMING_NFCA_T2T] = {
398 { PORT100_TG_PROT_END, 0 },
400 [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP] = {
401 { PORT100_TG_PROT_TU, 1 },
402 { PORT100_TG_PROT_RF_OFF, 0 },
403 { PORT100_TG_PROT_CRM, 7 },
404 { PORT100_TG_PROT_END, 0 },
406 [NFC_DIGITAL_FRAMING_NFCF] = {
407 { PORT100_TG_PROT_END, 0 },
409 [NFC_DIGITAL_FRAMING_NFCF_T3T] = {
410 { PORT100_TG_PROT_END, 0 },
412 [NFC_DIGITAL_FRAMING_NFCF_NFC_DEP] = {
413 { PORT100_TG_PROT_TU, 1 },
414 { PORT100_TG_PROT_RF_OFF, 0 },
415 { PORT100_TG_PROT_CRM, 7 },
416 { PORT100_TG_PROT_END, 0 },
418 [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = {
419 { PORT100_TG_PROT_RF_OFF, 1 },
420 { PORT100_TG_PROT_END, 0 },
422 /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
423 [NFC_DIGITAL_FRAMING_LAST] = {
424 { PORT100_TG_PROT_END, 0 },
428 struct port100 {
429 struct nfc_digital_dev *nfc_digital_dev;
431 int skb_headroom;
432 int skb_tailroom;
434 struct usb_device *udev;
435 struct usb_interface *interface;
437 struct urb *out_urb;
438 struct urb *in_urb;
440 struct work_struct cmd_complete_work;
442 u8 cmd_type;
444 /* The digital stack serializes commands to be sent. There is no need
445 * for any queuing/locking mechanism at driver level.
447 struct port100_cmd *cmd;
450 struct port100_cmd {
451 u8 code;
452 int status;
453 struct sk_buff *req;
454 struct sk_buff *resp;
455 int resp_len;
456 port100_send_async_complete_t complete_cb;
457 void *complete_cb_context;
460 struct port100_frame {
461 u8 preamble;
462 __be16 start_frame;
463 __be16 extended_frame;
464 __le16 datalen;
465 u8 datalen_checksum;
466 u8 data[];
467 } __packed;
469 struct port100_ack_frame {
470 u8 preamble;
471 __be16 start_frame;
472 __be16 ack_frame;
473 u8 postambule;
474 } __packed;
476 struct port100_cb_arg {
477 nfc_digital_cmd_complete_t complete_cb;
478 void *complete_arg;
479 u8 mdaa;
482 struct port100_tg_comm_rf_cmd {
483 __le16 guard_time;
484 __le16 send_timeout;
485 u8 mdaa;
486 u8 nfca_param[6];
487 u8 nfcf_param[18];
488 u8 mf_halted;
489 u8 arae_flag;
490 __le16 recv_timeout;
491 u8 data[];
492 } __packed;
494 struct port100_tg_comm_rf_res {
495 u8 comm_type;
496 u8 ar_status;
497 u8 target_activated;
498 __le32 status;
499 u8 data[];
500 } __packed;
502 /* The rule: value + checksum = 0 */
503 static inline u8 port100_checksum(u16 value)
505 return ~(((u8 *)&value)[0] + ((u8 *)&value)[1]) + 1;
508 /* The rule: sum(data elements) + checksum = 0 */
509 static u8 port100_data_checksum(u8 *data, int datalen)
511 u8 sum = 0;
512 int i;
514 for (i = 0; i < datalen; i++)
515 sum += data[i];
517 return port100_checksum(sum);
520 static void port100_tx_frame_init(void *_frame, u8 cmd_code)
522 struct port100_frame *frame = _frame;
524 frame->preamble = 0;
525 frame->start_frame = cpu_to_be16(PORT100_FRAME_SOF);
526 frame->extended_frame = cpu_to_be16(PORT100_FRAME_EXT);
527 PORT100_FRAME_DIRECTION(frame) = PORT100_FRAME_DIR_OUT;
528 PORT100_FRAME_CMD(frame) = cmd_code;
529 frame->datalen = cpu_to_le16(2);
532 static void port100_tx_frame_finish(void *_frame)
534 struct port100_frame *frame = _frame;
536 frame->datalen_checksum = port100_checksum(le16_to_cpu(frame->datalen));
538 PORT100_FRAME_CHECKSUM(frame) =
539 port100_data_checksum(frame->data, le16_to_cpu(frame->datalen));
541 PORT100_FRAME_POSTAMBLE(frame) = 0;
544 static void port100_tx_update_payload_len(void *_frame, int len)
546 struct port100_frame *frame = _frame;
548 frame->datalen = cpu_to_le16(le16_to_cpu(frame->datalen) + len);
551 static bool port100_rx_frame_is_valid(void *_frame)
553 u8 checksum;
554 struct port100_frame *frame = _frame;
556 if (frame->start_frame != cpu_to_be16(PORT100_FRAME_SOF) ||
557 frame->extended_frame != cpu_to_be16(PORT100_FRAME_EXT))
558 return false;
560 checksum = port100_checksum(le16_to_cpu(frame->datalen));
561 if (checksum != frame->datalen_checksum)
562 return false;
564 checksum = port100_data_checksum(frame->data,
565 le16_to_cpu(frame->datalen));
566 if (checksum != PORT100_FRAME_CHECKSUM(frame))
567 return false;
569 return true;
572 static bool port100_rx_frame_is_ack(struct port100_ack_frame *frame)
574 return (frame->start_frame == cpu_to_be16(PORT100_FRAME_SOF) &&
575 frame->ack_frame == cpu_to_be16(PORT100_FRAME_ACK));
578 static inline int port100_rx_frame_size(void *frame)
580 struct port100_frame *f = frame;
582 return sizeof(struct port100_frame) + le16_to_cpu(f->datalen) +
583 PORT100_FRAME_TAIL_LEN;
586 static bool port100_rx_frame_is_cmd_response(struct port100 *dev, void *frame)
588 struct port100_frame *f = frame;
590 return (PORT100_FRAME_CMD(f) == PORT100_CMD_RESPONSE(dev->cmd->code));
593 static void port100_recv_response(struct urb *urb)
595 struct port100 *dev = urb->context;
596 struct port100_cmd *cmd = dev->cmd;
597 u8 *in_frame;
599 cmd->status = urb->status;
601 switch (urb->status) {
602 case 0:
603 break; /* success */
604 case -ECONNRESET:
605 case -ENOENT:
606 nfc_err(&dev->interface->dev,
607 "The urb has been canceled (status %d)\n", urb->status);
608 goto sched_wq;
609 case -ESHUTDOWN:
610 default:
611 nfc_err(&dev->interface->dev, "Urb failure (status %d)\n",
612 urb->status);
613 goto sched_wq;
616 in_frame = dev->in_urb->transfer_buffer;
618 if (!port100_rx_frame_is_valid(in_frame)) {
619 nfc_err(&dev->interface->dev, "Received an invalid frame\n");
620 cmd->status = -EIO;
621 goto sched_wq;
624 print_hex_dump_debug("PORT100 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
625 port100_rx_frame_size(in_frame), false);
627 if (!port100_rx_frame_is_cmd_response(dev, in_frame)) {
628 nfc_err(&dev->interface->dev,
629 "It's not the response to the last command\n");
630 cmd->status = -EIO;
631 goto sched_wq;
634 sched_wq:
635 schedule_work(&dev->cmd_complete_work);
638 static int port100_submit_urb_for_response(struct port100 *dev, gfp_t flags)
640 dev->in_urb->complete = port100_recv_response;
642 return usb_submit_urb(dev->in_urb, flags);
645 static void port100_recv_ack(struct urb *urb)
647 struct port100 *dev = urb->context;
648 struct port100_cmd *cmd = dev->cmd;
649 struct port100_ack_frame *in_frame;
650 int rc;
652 cmd->status = urb->status;
654 switch (urb->status) {
655 case 0:
656 break; /* success */
657 case -ECONNRESET:
658 case -ENOENT:
659 nfc_err(&dev->interface->dev,
660 "The urb has been stopped (status %d)\n", urb->status);
661 goto sched_wq;
662 case -ESHUTDOWN:
663 default:
664 nfc_err(&dev->interface->dev, "Urb failure (status %d)\n",
665 urb->status);
666 goto sched_wq;
669 in_frame = dev->in_urb->transfer_buffer;
671 if (!port100_rx_frame_is_ack(in_frame)) {
672 nfc_err(&dev->interface->dev, "Received an invalid ack\n");
673 cmd->status = -EIO;
674 goto sched_wq;
677 rc = port100_submit_urb_for_response(dev, GFP_ATOMIC);
678 if (rc) {
679 nfc_err(&dev->interface->dev,
680 "usb_submit_urb failed with result %d\n", rc);
681 cmd->status = rc;
682 goto sched_wq;
685 return;
687 sched_wq:
688 schedule_work(&dev->cmd_complete_work);
691 static int port100_submit_urb_for_ack(struct port100 *dev, gfp_t flags)
693 dev->in_urb->complete = port100_recv_ack;
695 return usb_submit_urb(dev->in_urb, flags);
698 static int port100_send_ack(struct port100 *dev)
700 int rc;
702 dev->out_urb->transfer_buffer = ack_frame;
703 dev->out_urb->transfer_buffer_length = sizeof(ack_frame);
704 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
706 return rc;
709 static int port100_send_frame_async(struct port100 *dev, struct sk_buff *out,
710 struct sk_buff *in, int in_len)
712 int rc;
714 dev->out_urb->transfer_buffer = out->data;
715 dev->out_urb->transfer_buffer_length = out->len;
717 dev->in_urb->transfer_buffer = in->data;
718 dev->in_urb->transfer_buffer_length = in_len;
720 print_hex_dump_debug("PORT100 TX: ", DUMP_PREFIX_NONE, 16, 1,
721 out->data, out->len, false);
723 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
724 if (rc)
725 return rc;
727 rc = port100_submit_urb_for_ack(dev, GFP_KERNEL);
728 if (rc)
729 goto error;
731 return 0;
733 error:
734 usb_unlink_urb(dev->out_urb);
735 return rc;
738 static void port100_build_cmd_frame(struct port100 *dev, u8 cmd_code,
739 struct sk_buff *skb)
741 /* payload is already there, just update datalen */
742 int payload_len = skb->len;
744 skb_push(skb, PORT100_FRAME_HEADER_LEN);
745 skb_put(skb, PORT100_FRAME_TAIL_LEN);
747 port100_tx_frame_init(skb->data, cmd_code);
748 port100_tx_update_payload_len(skb->data, payload_len);
749 port100_tx_frame_finish(skb->data);
752 static void port100_send_async_complete(struct port100 *dev)
754 struct port100_cmd *cmd = dev->cmd;
755 int status = cmd->status;
757 struct sk_buff *req = cmd->req;
758 struct sk_buff *resp = cmd->resp;
760 dev_kfree_skb(req);
762 dev->cmd = NULL;
764 if (status < 0) {
765 cmd->complete_cb(dev, cmd->complete_cb_context,
766 ERR_PTR(status));
767 dev_kfree_skb(resp);
768 goto done;
771 skb_put(resp, port100_rx_frame_size(resp->data));
772 skb_pull(resp, PORT100_FRAME_HEADER_LEN);
773 skb_trim(resp, resp->len - PORT100_FRAME_TAIL_LEN);
775 cmd->complete_cb(dev, cmd->complete_cb_context, resp);
777 done:
778 kfree(cmd);
781 static int port100_send_cmd_async(struct port100 *dev, u8 cmd_code,
782 struct sk_buff *req,
783 port100_send_async_complete_t complete_cb,
784 void *complete_cb_context)
786 struct port100_cmd *cmd;
787 struct sk_buff *resp;
788 int rc;
789 int resp_len = PORT100_FRAME_HEADER_LEN +
790 PORT100_FRAME_MAX_PAYLOAD_LEN +
791 PORT100_FRAME_TAIL_LEN;
793 resp = alloc_skb(resp_len, GFP_KERNEL);
794 if (!resp)
795 return -ENOMEM;
797 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
798 if (!cmd) {
799 dev_kfree_skb(resp);
800 return -ENOMEM;
803 cmd->code = cmd_code;
804 cmd->req = req;
805 cmd->resp = resp;
806 cmd->resp_len = resp_len;
807 cmd->complete_cb = complete_cb;
808 cmd->complete_cb_context = complete_cb_context;
810 port100_build_cmd_frame(dev, cmd_code, req);
812 dev->cmd = cmd;
814 rc = port100_send_frame_async(dev, req, resp, resp_len);
815 if (rc) {
816 kfree(cmd);
817 dev_kfree_skb(resp);
818 dev->cmd = NULL;
821 return rc;
824 struct port100_sync_cmd_response {
825 struct sk_buff *resp;
826 struct completion done;
829 static void port100_wq_cmd_complete(struct work_struct *work)
831 struct port100 *dev = container_of(work, struct port100,
832 cmd_complete_work);
834 port100_send_async_complete(dev);
837 static void port100_send_sync_complete(struct port100 *dev, void *_arg,
838 struct sk_buff *resp)
840 struct port100_sync_cmd_response *arg = _arg;
842 arg->resp = resp;
843 complete(&arg->done);
846 static struct sk_buff *port100_send_cmd_sync(struct port100 *dev, u8 cmd_code,
847 struct sk_buff *req)
849 int rc;
850 struct port100_sync_cmd_response arg;
852 init_completion(&arg.done);
854 rc = port100_send_cmd_async(dev, cmd_code, req,
855 port100_send_sync_complete, &arg);
856 if (rc) {
857 dev_kfree_skb(req);
858 return ERR_PTR(rc);
861 wait_for_completion(&arg.done);
863 return arg.resp;
866 static void port100_send_complete(struct urb *urb)
868 struct port100 *dev = urb->context;
870 switch (urb->status) {
871 case 0:
872 break; /* success */
873 case -ECONNRESET:
874 case -ENOENT:
875 nfc_err(&dev->interface->dev,
876 "The urb has been stopped (status %d)\n", urb->status);
877 break;
878 case -ESHUTDOWN:
879 default:
880 nfc_err(&dev->interface->dev, "Urb failure (status %d)\n",
881 urb->status);
885 static void port100_abort_cmd(struct nfc_digital_dev *ddev)
887 struct port100 *dev = nfc_digital_get_drvdata(ddev);
889 /* An ack will cancel the last issued command */
890 port100_send_ack(dev);
892 /* cancel the urb request */
893 usb_kill_urb(dev->in_urb);
896 static struct sk_buff *port100_alloc_skb(struct port100 *dev, unsigned int size)
898 struct sk_buff *skb;
900 skb = alloc_skb(dev->skb_headroom + dev->skb_tailroom + size,
901 GFP_KERNEL);
902 if (skb)
903 skb_reserve(skb, dev->skb_headroom);
905 return skb;
908 static int port100_set_command_type(struct port100 *dev, u8 command_type)
910 struct sk_buff *skb;
911 struct sk_buff *resp;
912 int rc;
914 skb = port100_alloc_skb(dev, 1);
915 if (!skb)
916 return -ENOMEM;
918 *skb_put(skb, sizeof(u8)) = command_type;
920 resp = port100_send_cmd_sync(dev, PORT100_CMD_SET_COMMAND_TYPE, skb);
921 if (IS_ERR(resp))
922 return PTR_ERR(resp);
924 rc = resp->data[0];
926 dev_kfree_skb(resp);
928 return rc;
931 static u64 port100_get_command_type_mask(struct port100 *dev)
933 struct sk_buff *skb;
934 struct sk_buff *resp;
935 u64 mask;
937 skb = port100_alloc_skb(dev, 0);
938 if (!skb)
939 return -ENOMEM;
941 resp = port100_send_cmd_sync(dev, PORT100_CMD_GET_COMMAND_TYPE, skb);
942 if (IS_ERR(resp))
943 return PTR_ERR(resp);
945 if (resp->len < 8)
946 mask = 0;
947 else
948 mask = be64_to_cpu(*(__be64 *)resp->data);
950 dev_kfree_skb(resp);
952 return mask;
955 static u16 port100_get_firmware_version(struct port100 *dev)
957 struct sk_buff *skb;
958 struct sk_buff *resp;
959 u16 fw_ver;
961 skb = port100_alloc_skb(dev, 0);
962 if (!skb)
963 return 0;
965 resp = port100_send_cmd_sync(dev, PORT100_CMD_GET_FIRMWARE_VERSION,
966 skb);
967 if (IS_ERR(resp))
968 return 0;
970 fw_ver = le16_to_cpu(*(__le16 *)resp->data);
972 dev_kfree_skb(resp);
974 return fw_ver;
977 static int port100_switch_rf(struct nfc_digital_dev *ddev, bool on)
979 struct port100 *dev = nfc_digital_get_drvdata(ddev);
980 struct sk_buff *skb, *resp;
982 skb = port100_alloc_skb(dev, 1);
983 if (!skb)
984 return -ENOMEM;
986 *skb_put(skb, 1) = on ? 1 : 0;
988 resp = port100_send_cmd_sync(dev, PORT100_CMD_SWITCH_RF, skb);
990 if (IS_ERR(resp))
991 return PTR_ERR(resp);
993 dev_kfree_skb(resp);
995 return 0;
998 static int port100_in_set_rf(struct nfc_digital_dev *ddev, u8 rf)
1000 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1001 struct sk_buff *skb;
1002 struct sk_buff *resp;
1003 int rc;
1005 if (rf >= NFC_DIGITAL_RF_TECH_LAST)
1006 return -EINVAL;
1008 skb = port100_alloc_skb(dev, sizeof(struct port100_in_rf_setting));
1009 if (!skb)
1010 return -ENOMEM;
1012 memcpy(skb_put(skb, sizeof(struct port100_in_rf_setting)),
1013 &in_rf_settings[rf],
1014 sizeof(struct port100_in_rf_setting));
1016 resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_RF, skb);
1018 if (IS_ERR(resp))
1019 return PTR_ERR(resp);
1021 rc = resp->data[0];
1023 dev_kfree_skb(resp);
1025 return rc;
1028 static int port100_in_set_framing(struct nfc_digital_dev *ddev, int param)
1030 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1031 struct port100_protocol *protocols;
1032 struct sk_buff *skb;
1033 struct sk_buff *resp;
1034 int num_protocols;
1035 size_t size;
1036 int rc;
1038 if (param >= NFC_DIGITAL_FRAMING_LAST)
1039 return -EINVAL;
1041 protocols = in_protocols[param];
1043 num_protocols = 0;
1044 while (protocols[num_protocols].number != PORT100_IN_PROT_END)
1045 num_protocols++;
1047 if (!num_protocols)
1048 return 0;
1050 size = sizeof(struct port100_protocol) * num_protocols;
1052 skb = port100_alloc_skb(dev, size);
1053 if (!skb)
1054 return -ENOMEM;
1056 memcpy(skb_put(skb, size), protocols, size);
1058 resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_PROTOCOL, skb);
1060 if (IS_ERR(resp))
1061 return PTR_ERR(resp);
1063 rc = resp->data[0];
1065 dev_kfree_skb(resp);
1067 return rc;
1070 static int port100_in_configure_hw(struct nfc_digital_dev *ddev, int type,
1071 int param)
1073 if (type == NFC_DIGITAL_CONFIG_RF_TECH)
1074 return port100_in_set_rf(ddev, param);
1076 if (type == NFC_DIGITAL_CONFIG_FRAMING)
1077 return port100_in_set_framing(ddev, param);
1079 return -EINVAL;
1082 static void port100_in_comm_rf_complete(struct port100 *dev, void *arg,
1083 struct sk_buff *resp)
1085 struct port100_cb_arg *cb_arg = arg;
1086 nfc_digital_cmd_complete_t cb = cb_arg->complete_cb;
1087 u32 status;
1088 int rc;
1090 if (IS_ERR(resp)) {
1091 rc = PTR_ERR(resp);
1092 goto exit;
1095 if (resp->len < 4) {
1096 nfc_err(&dev->interface->dev,
1097 "Invalid packet length received\n");
1098 rc = -EIO;
1099 goto error;
1102 status = le32_to_cpu(*(__le32 *)resp->data);
1104 skb_pull(resp, sizeof(u32));
1106 if (status == PORT100_CMD_STATUS_TIMEOUT) {
1107 rc = -ETIMEDOUT;
1108 goto error;
1111 if (status != PORT100_CMD_STATUS_OK) {
1112 nfc_err(&dev->interface->dev,
1113 "in_comm_rf failed with status 0x%08x\n", status);
1114 rc = -EIO;
1115 goto error;
1118 /* Remove collision bits byte */
1119 skb_pull(resp, 1);
1121 goto exit;
1123 error:
1124 kfree_skb(resp);
1125 resp = ERR_PTR(rc);
1127 exit:
1128 cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp);
1130 kfree(cb_arg);
1133 static int port100_in_send_cmd(struct nfc_digital_dev *ddev,
1134 struct sk_buff *skb, u16 _timeout,
1135 nfc_digital_cmd_complete_t cb, void *arg)
1137 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1138 struct port100_cb_arg *cb_arg;
1139 __le16 timeout;
1141 cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1142 if (!cb_arg)
1143 return -ENOMEM;
1145 cb_arg->complete_cb = cb;
1146 cb_arg->complete_arg = arg;
1148 timeout = cpu_to_le16(_timeout * 10);
1150 memcpy(skb_push(skb, sizeof(__le16)), &timeout, sizeof(__le16));
1152 return port100_send_cmd_async(dev, PORT100_CMD_IN_COMM_RF, skb,
1153 port100_in_comm_rf_complete, cb_arg);
1156 static int port100_tg_set_rf(struct nfc_digital_dev *ddev, u8 rf)
1158 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1159 struct sk_buff *skb;
1160 struct sk_buff *resp;
1161 int rc;
1163 if (rf >= NFC_DIGITAL_RF_TECH_LAST)
1164 return -EINVAL;
1166 skb = port100_alloc_skb(dev, sizeof(struct port100_tg_rf_setting));
1167 if (!skb)
1168 return -ENOMEM;
1170 memcpy(skb_put(skb, sizeof(struct port100_tg_rf_setting)),
1171 &tg_rf_settings[rf],
1172 sizeof(struct port100_tg_rf_setting));
1174 resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_RF, skb);
1176 if (IS_ERR(resp))
1177 return PTR_ERR(resp);
1179 rc = resp->data[0];
1181 dev_kfree_skb(resp);
1183 return rc;
1186 static int port100_tg_set_framing(struct nfc_digital_dev *ddev, int param)
1188 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1189 struct port100_protocol *protocols;
1190 struct sk_buff *skb;
1191 struct sk_buff *resp;
1192 int rc;
1193 int num_protocols;
1194 size_t size;
1196 if (param >= NFC_DIGITAL_FRAMING_LAST)
1197 return -EINVAL;
1199 protocols = tg_protocols[param];
1201 num_protocols = 0;
1202 while (protocols[num_protocols].number != PORT100_TG_PROT_END)
1203 num_protocols++;
1205 if (!num_protocols)
1206 return 0;
1208 size = sizeof(struct port100_protocol) * num_protocols;
1210 skb = port100_alloc_skb(dev, size);
1211 if (!skb)
1212 return -ENOMEM;
1214 memcpy(skb_put(skb, size), protocols, size);
1216 resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_PROTOCOL, skb);
1218 if (IS_ERR(resp))
1219 return PTR_ERR(resp);
1221 rc = resp->data[0];
1223 dev_kfree_skb(resp);
1225 return rc;
1228 static int port100_tg_configure_hw(struct nfc_digital_dev *ddev, int type,
1229 int param)
1231 if (type == NFC_DIGITAL_CONFIG_RF_TECH)
1232 return port100_tg_set_rf(ddev, param);
1234 if (type == NFC_DIGITAL_CONFIG_FRAMING)
1235 return port100_tg_set_framing(ddev, param);
1237 return -EINVAL;
1240 static bool port100_tg_target_activated(struct port100 *dev, u8 tgt_activated)
1242 u8 mask;
1244 switch (dev->cmd_type) {
1245 case PORT100_CMD_TYPE_0:
1246 mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK;
1247 break;
1248 case PORT100_CMD_TYPE_1:
1249 mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK |
1250 PORT100_MDAA_TGT_WAS_ACTIVATED_MASK;
1251 break;
1252 default:
1253 nfc_err(&dev->interface->dev, "Unknown command type\n");
1254 return false;
1257 return ((tgt_activated & mask) == mask);
1260 static void port100_tg_comm_rf_complete(struct port100 *dev, void *arg,
1261 struct sk_buff *resp)
1263 u32 status;
1264 struct port100_cb_arg *cb_arg = arg;
1265 nfc_digital_cmd_complete_t cb = cb_arg->complete_cb;
1266 struct port100_tg_comm_rf_res *hdr;
1268 if (IS_ERR(resp))
1269 goto exit;
1271 hdr = (struct port100_tg_comm_rf_res *)resp->data;
1273 status = le32_to_cpu(hdr->status);
1275 if (cb_arg->mdaa &&
1276 !port100_tg_target_activated(dev, hdr->target_activated)) {
1277 kfree_skb(resp);
1278 resp = ERR_PTR(-ETIMEDOUT);
1280 goto exit;
1283 skb_pull(resp, sizeof(struct port100_tg_comm_rf_res));
1285 if (status != PORT100_CMD_STATUS_OK) {
1286 kfree_skb(resp);
1288 if (status == PORT100_CMD_STATUS_TIMEOUT)
1289 resp = ERR_PTR(-ETIMEDOUT);
1290 else
1291 resp = ERR_PTR(-EIO);
1294 exit:
1295 cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp);
1297 kfree(cb_arg);
1300 static int port100_tg_send_cmd(struct nfc_digital_dev *ddev,
1301 struct sk_buff *skb, u16 timeout,
1302 nfc_digital_cmd_complete_t cb, void *arg)
1304 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1305 struct port100_tg_comm_rf_cmd *hdr;
1306 struct port100_cb_arg *cb_arg;
1308 cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1309 if (!cb_arg)
1310 return -ENOMEM;
1312 cb_arg->complete_cb = cb;
1313 cb_arg->complete_arg = arg;
1315 skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd));
1317 hdr = (struct port100_tg_comm_rf_cmd *)skb->data;
1319 memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd));
1320 hdr->guard_time = cpu_to_le16(500);
1321 hdr->send_timeout = cpu_to_le16(0xFFFF);
1322 hdr->recv_timeout = cpu_to_le16(timeout);
1324 return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb,
1325 port100_tg_comm_rf_complete, cb_arg);
1328 static int port100_listen_mdaa(struct nfc_digital_dev *ddev,
1329 struct digital_tg_mdaa_params *params,
1330 u16 timeout,
1331 nfc_digital_cmd_complete_t cb, void *arg)
1333 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1334 struct port100_tg_comm_rf_cmd *hdr;
1335 struct port100_cb_arg *cb_arg;
1336 struct sk_buff *skb;
1337 int rc;
1339 rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
1340 NFC_DIGITAL_RF_TECH_106A);
1341 if (rc)
1342 return rc;
1344 rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1345 NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
1346 if (rc)
1347 return rc;
1349 cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1350 if (!cb_arg)
1351 return -ENOMEM;
1353 cb_arg->complete_cb = cb;
1354 cb_arg->complete_arg = arg;
1355 cb_arg->mdaa = 1;
1357 skb = port100_alloc_skb(dev, 0);
1358 if (!skb) {
1359 kfree(cb_arg);
1360 return -ENOMEM;
1363 skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd));
1364 hdr = (struct port100_tg_comm_rf_cmd *)skb->data;
1366 memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd));
1368 hdr->guard_time = 0;
1369 hdr->send_timeout = cpu_to_le16(0xFFFF);
1370 hdr->mdaa = 1;
1371 hdr->nfca_param[0] = (params->sens_res >> 8) & 0xFF;
1372 hdr->nfca_param[1] = params->sens_res & 0xFF;
1373 memcpy(hdr->nfca_param + 2, params->nfcid1, 3);
1374 hdr->nfca_param[5] = params->sel_res;
1375 memcpy(hdr->nfcf_param, params->nfcid2, 8);
1376 hdr->nfcf_param[16] = (params->sc >> 8) & 0xFF;
1377 hdr->nfcf_param[17] = params->sc & 0xFF;
1378 hdr->recv_timeout = cpu_to_le16(timeout);
1380 return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb,
1381 port100_tg_comm_rf_complete, cb_arg);
1384 static int port100_listen(struct nfc_digital_dev *ddev, u16 timeout,
1385 nfc_digital_cmd_complete_t cb, void *arg)
1387 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1388 struct sk_buff *skb;
1390 skb = port100_alloc_skb(dev, 0);
1391 if (!skb)
1392 return -ENOMEM;
1394 return port100_tg_send_cmd(ddev, skb, timeout, cb, arg);
1397 static struct nfc_digital_ops port100_digital_ops = {
1398 .in_configure_hw = port100_in_configure_hw,
1399 .in_send_cmd = port100_in_send_cmd,
1401 .tg_listen_mdaa = port100_listen_mdaa,
1402 .tg_listen = port100_listen,
1403 .tg_configure_hw = port100_tg_configure_hw,
1404 .tg_send_cmd = port100_tg_send_cmd,
1406 .switch_rf = port100_switch_rf,
1407 .abort_cmd = port100_abort_cmd,
1410 static const struct usb_device_id port100_table[] = {
1411 { USB_DEVICE(SONY_VENDOR_ID, RCS380_PRODUCT_ID), },
1414 MODULE_DEVICE_TABLE(usb, port100_table);
1416 static int port100_probe(struct usb_interface *interface,
1417 const struct usb_device_id *id)
1419 struct port100 *dev;
1420 int rc;
1421 struct usb_host_interface *iface_desc;
1422 struct usb_endpoint_descriptor *endpoint;
1423 int in_endpoint;
1424 int out_endpoint;
1425 u16 fw_version;
1426 u64 cmd_type_mask;
1427 int i;
1429 dev = devm_kzalloc(&interface->dev, sizeof(struct port100), GFP_KERNEL);
1430 if (!dev)
1431 return -ENOMEM;
1433 dev->udev = usb_get_dev(interface_to_usbdev(interface));
1434 dev->interface = interface;
1435 usb_set_intfdata(interface, dev);
1437 in_endpoint = out_endpoint = 0;
1438 iface_desc = interface->cur_altsetting;
1439 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1440 endpoint = &iface_desc->endpoint[i].desc;
1442 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
1443 in_endpoint = endpoint->bEndpointAddress;
1445 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
1446 out_endpoint = endpoint->bEndpointAddress;
1449 if (!in_endpoint || !out_endpoint) {
1450 nfc_err(&interface->dev,
1451 "Could not find bulk-in or bulk-out endpoint\n");
1452 rc = -ENODEV;
1453 goto error;
1456 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
1457 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
1459 if (!dev->in_urb || !dev->out_urb) {
1460 nfc_err(&interface->dev, "Could not allocate USB URBs\n");
1461 rc = -ENOMEM;
1462 goto error;
1465 usb_fill_bulk_urb(dev->in_urb, dev->udev,
1466 usb_rcvbulkpipe(dev->udev, in_endpoint),
1467 NULL, 0, NULL, dev);
1468 usb_fill_bulk_urb(dev->out_urb, dev->udev,
1469 usb_sndbulkpipe(dev->udev, out_endpoint),
1470 NULL, 0, port100_send_complete, dev);
1472 dev->skb_headroom = PORT100_FRAME_HEADER_LEN +
1473 PORT100_COMM_RF_HEAD_MAX_LEN;
1474 dev->skb_tailroom = PORT100_FRAME_TAIL_LEN;
1476 INIT_WORK(&dev->cmd_complete_work, port100_wq_cmd_complete);
1478 /* The first thing to do with the Port-100 is to set the command type
1479 * to be used. If supported we use command type 1. 0 otherwise.
1481 cmd_type_mask = port100_get_command_type_mask(dev);
1482 if (!cmd_type_mask) {
1483 nfc_err(&interface->dev,
1484 "Could not get supported command types\n");
1485 rc = -ENODEV;
1486 goto error;
1489 if (PORT100_CMD_TYPE_IS_SUPPORTED(cmd_type_mask, PORT100_CMD_TYPE_1))
1490 dev->cmd_type = PORT100_CMD_TYPE_1;
1491 else
1492 dev->cmd_type = PORT100_CMD_TYPE_0;
1494 rc = port100_set_command_type(dev, dev->cmd_type);
1495 if (rc) {
1496 nfc_err(&interface->dev,
1497 "The device does not support command type %u\n",
1498 dev->cmd_type);
1499 goto error;
1502 fw_version = port100_get_firmware_version(dev);
1503 if (!fw_version)
1504 nfc_err(&interface->dev,
1505 "Could not get device firmware version\n");
1507 nfc_info(&interface->dev,
1508 "Sony NFC Port-100 Series attached (firmware v%x.%02x)\n",
1509 (fw_version & 0xFF00) >> 8, fw_version & 0xFF);
1511 dev->nfc_digital_dev = nfc_digital_allocate_device(&port100_digital_ops,
1512 PORT100_PROTOCOLS,
1513 PORT100_CAPABILITIES,
1514 dev->skb_headroom,
1515 dev->skb_tailroom);
1516 if (!dev->nfc_digital_dev) {
1517 nfc_err(&interface->dev,
1518 "Could not allocate nfc_digital_dev\n");
1519 rc = -ENOMEM;
1520 goto error;
1523 nfc_digital_set_parent_dev(dev->nfc_digital_dev, &interface->dev);
1524 nfc_digital_set_drvdata(dev->nfc_digital_dev, dev);
1526 rc = nfc_digital_register_device(dev->nfc_digital_dev);
1527 if (rc) {
1528 nfc_err(&interface->dev,
1529 "Could not register digital device\n");
1530 goto free_nfc_dev;
1533 return 0;
1535 free_nfc_dev:
1536 nfc_digital_free_device(dev->nfc_digital_dev);
1538 error:
1539 usb_free_urb(dev->in_urb);
1540 usb_free_urb(dev->out_urb);
1541 usb_put_dev(dev->udev);
1543 return rc;
1546 static void port100_disconnect(struct usb_interface *interface)
1548 struct port100 *dev;
1550 dev = usb_get_intfdata(interface);
1551 usb_set_intfdata(interface, NULL);
1553 nfc_digital_unregister_device(dev->nfc_digital_dev);
1554 nfc_digital_free_device(dev->nfc_digital_dev);
1556 usb_kill_urb(dev->in_urb);
1557 usb_kill_urb(dev->out_urb);
1559 usb_free_urb(dev->in_urb);
1560 usb_free_urb(dev->out_urb);
1561 usb_put_dev(dev->udev);
1563 kfree(dev->cmd);
1565 nfc_info(&interface->dev, "Sony Port-100 NFC device disconnected\n");
1568 static struct usb_driver port100_driver = {
1569 .name = "port100",
1570 .probe = port100_probe,
1571 .disconnect = port100_disconnect,
1572 .id_table = port100_table,
1575 module_usb_driver(port100_driver);
1577 MODULE_DESCRIPTION("NFC Port-100 series usb driver ver " VERSION);
1578 MODULE_VERSION(VERSION);
1579 MODULE_LICENSE("GPL");