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
18 #include <linux/module.h>
19 #include <linux/usb.h>
20 #include <net/nfc/digital.h>
24 #define SONY_VENDOR_ID 0x054c
25 #define RCS380S_PRODUCT_ID 0x06c1
26 #define RCS380P_PRODUCT_ID 0x06c3
28 #define PORT100_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
29 NFC_PROTO_MIFARE_MASK | \
30 NFC_PROTO_FELICA_MASK | \
31 NFC_PROTO_NFC_DEP_MASK | \
32 NFC_PROTO_ISO14443_MASK | \
33 NFC_PROTO_ISO14443_B_MASK)
35 #define PORT100_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC | \
36 NFC_DIGITAL_DRV_CAPS_TG_CRC)
38 /* Standard port100 frame definitions */
39 #define PORT100_FRAME_HEADER_LEN (sizeof(struct port100_frame) \
40 + 2) /* data[0] CC, data[1] SCC */
41 #define PORT100_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
43 #define PORT100_COMM_RF_HEAD_MAX_LEN (sizeof(struct port100_tg_comm_rf_cmd))
46 * Max extended frame payload len, excluding CC and SCC
47 * which are already in PORT100_FRAME_HEADER_LEN.
49 #define PORT100_FRAME_MAX_PAYLOAD_LEN 1001
51 #define PORT100_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
53 static u8 ack_frame
[PORT100_FRAME_ACK_SIZE
] = {
54 0x00, 0x00, 0xff, 0x00, 0xff, 0x00
57 #define PORT100_FRAME_CHECKSUM(f) (f->data[le16_to_cpu(f->datalen)])
58 #define PORT100_FRAME_POSTAMBLE(f) (f->data[le16_to_cpu(f->datalen) + 1])
61 #define PORT100_FRAME_SOF 0x00FF
62 #define PORT100_FRAME_EXT 0xFFFF
63 #define PORT100_FRAME_ACK 0x00FF
65 /* Port-100 command: in or out */
66 #define PORT100_FRAME_DIRECTION(f) (f->data[0]) /* CC */
67 #define PORT100_FRAME_DIR_OUT 0xD6
68 #define PORT100_FRAME_DIR_IN 0xD7
70 /* Port-100 sub-command */
71 #define PORT100_FRAME_CMD(f) (f->data[1]) /* SCC */
73 #define PORT100_CMD_GET_FIRMWARE_VERSION 0x20
74 #define PORT100_CMD_GET_COMMAND_TYPE 0x28
75 #define PORT100_CMD_SET_COMMAND_TYPE 0x2A
77 #define PORT100_CMD_IN_SET_RF 0x00
78 #define PORT100_CMD_IN_SET_PROTOCOL 0x02
79 #define PORT100_CMD_IN_COMM_RF 0x04
81 #define PORT100_CMD_TG_SET_RF 0x40
82 #define PORT100_CMD_TG_SET_PROTOCOL 0x42
83 #define PORT100_CMD_TG_SET_RF_OFF 0x46
84 #define PORT100_CMD_TG_COMM_RF 0x48
86 #define PORT100_CMD_SWITCH_RF 0x06
88 #define PORT100_CMD_RESPONSE(cmd) (cmd + 1)
90 #define PORT100_CMD_TYPE_IS_SUPPORTED(mask, cmd_type) \
91 ((mask) & (0x01 << (cmd_type)))
92 #define PORT100_CMD_TYPE_0 0
93 #define PORT100_CMD_TYPE_1 1
95 #define PORT100_CMD_STATUS_OK 0x00
96 #define PORT100_CMD_STATUS_TIMEOUT 0x80
98 #define PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK 0x01
99 #define PORT100_MDAA_TGT_WAS_ACTIVATED_MASK 0x02
103 typedef void (*port100_send_async_complete_t
)(struct port100
*dev
, void *arg
,
104 struct sk_buff
*resp
);
107 * Setting sets structure for in_set_rf command
109 * @in_*_set_number: Represent the entry indexes in the port-100 RF Base Table.
110 * This table contains multiple RF setting sets required for RF
113 * @in_*_comm_type: Theses fields set the communication type to be used.
115 struct port100_in_rf_setting
{
116 u8 in_send_set_number
;
117 u8 in_send_comm_type
;
118 u8 in_recv_set_number
;
119 u8 in_recv_comm_type
;
122 #define PORT100_COMM_TYPE_IN_212F 0x01
123 #define PORT100_COMM_TYPE_IN_424F 0x02
124 #define PORT100_COMM_TYPE_IN_106A 0x03
125 #define PORT100_COMM_TYPE_IN_106B 0x07
127 static const struct port100_in_rf_setting in_rf_settings
[] = {
128 [NFC_DIGITAL_RF_TECH_212F
] = {
129 .in_send_set_number
= 1,
130 .in_send_comm_type
= PORT100_COMM_TYPE_IN_212F
,
131 .in_recv_set_number
= 15,
132 .in_recv_comm_type
= PORT100_COMM_TYPE_IN_212F
,
134 [NFC_DIGITAL_RF_TECH_424F
] = {
135 .in_send_set_number
= 1,
136 .in_send_comm_type
= PORT100_COMM_TYPE_IN_424F
,
137 .in_recv_set_number
= 15,
138 .in_recv_comm_type
= PORT100_COMM_TYPE_IN_424F
,
140 [NFC_DIGITAL_RF_TECH_106A
] = {
141 .in_send_set_number
= 2,
142 .in_send_comm_type
= PORT100_COMM_TYPE_IN_106A
,
143 .in_recv_set_number
= 15,
144 .in_recv_comm_type
= PORT100_COMM_TYPE_IN_106A
,
146 [NFC_DIGITAL_RF_TECH_106B
] = {
147 .in_send_set_number
= 3,
148 .in_send_comm_type
= PORT100_COMM_TYPE_IN_106B
,
149 .in_recv_set_number
= 15,
150 .in_recv_comm_type
= PORT100_COMM_TYPE_IN_106B
,
152 /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
153 [NFC_DIGITAL_RF_TECH_LAST
] = { 0 },
157 * Setting sets structure for tg_set_rf command
159 * @tg_set_number: Represents the entry index in the port-100 RF Base Table.
160 * This table contains multiple RF setting sets required for RF
161 * communication. this field is used for both send and receive
164 * @tg_comm_type: Sets the communication type to be used to send and receive
167 struct port100_tg_rf_setting
{
172 #define PORT100_COMM_TYPE_TG_106A 0x0B
173 #define PORT100_COMM_TYPE_TG_212F 0x0C
174 #define PORT100_COMM_TYPE_TG_424F 0x0D
176 static const struct port100_tg_rf_setting tg_rf_settings
[] = {
177 [NFC_DIGITAL_RF_TECH_106A
] = {
179 .tg_comm_type
= PORT100_COMM_TYPE_TG_106A
,
181 [NFC_DIGITAL_RF_TECH_212F
] = {
183 .tg_comm_type
= PORT100_COMM_TYPE_TG_212F
,
185 [NFC_DIGITAL_RF_TECH_424F
] = {
187 .tg_comm_type
= PORT100_COMM_TYPE_TG_424F
,
189 /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
190 [NFC_DIGITAL_RF_TECH_LAST
] = { 0 },
194 #define PORT100_IN_PROT_INITIAL_GUARD_TIME 0x00
195 #define PORT100_IN_PROT_ADD_CRC 0x01
196 #define PORT100_IN_PROT_CHECK_CRC 0x02
197 #define PORT100_IN_PROT_MULTI_CARD 0x03
198 #define PORT100_IN_PROT_ADD_PARITY 0x04
199 #define PORT100_IN_PROT_CHECK_PARITY 0x05
200 #define PORT100_IN_PROT_BITWISE_AC_RECV_MODE 0x06
201 #define PORT100_IN_PROT_VALID_BIT_NUMBER 0x07
202 #define PORT100_IN_PROT_CRYPTO1 0x08
203 #define PORT100_IN_PROT_ADD_SOF 0x09
204 #define PORT100_IN_PROT_CHECK_SOF 0x0A
205 #define PORT100_IN_PROT_ADD_EOF 0x0B
206 #define PORT100_IN_PROT_CHECK_EOF 0x0C
207 #define PORT100_IN_PROT_DEAF_TIME 0x0E
208 #define PORT100_IN_PROT_CRM 0x0F
209 #define PORT100_IN_PROT_CRM_MIN_LEN 0x10
210 #define PORT100_IN_PROT_T1_TAG_FRAME 0x11
211 #define PORT100_IN_PROT_RFCA 0x12
212 #define PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR 0x13
213 #define PORT100_IN_PROT_END 0x14
215 #define PORT100_IN_MAX_NUM_PROTOCOLS 19
217 #define PORT100_TG_PROT_TU 0x00
218 #define PORT100_TG_PROT_RF_OFF 0x01
219 #define PORT100_TG_PROT_CRM 0x02
220 #define PORT100_TG_PROT_END 0x03
222 #define PORT100_TG_MAX_NUM_PROTOCOLS 3
224 struct port100_protocol
{
229 static struct port100_protocol
230 in_protocols
[][PORT100_IN_MAX_NUM_PROTOCOLS
+ 1] = {
231 [NFC_DIGITAL_FRAMING_NFCA_SHORT
] = {
232 { PORT100_IN_PROT_INITIAL_GUARD_TIME
, 6 },
233 { PORT100_IN_PROT_ADD_CRC
, 0 },
234 { PORT100_IN_PROT_CHECK_CRC
, 0 },
235 { PORT100_IN_PROT_MULTI_CARD
, 0 },
236 { PORT100_IN_PROT_ADD_PARITY
, 0 },
237 { PORT100_IN_PROT_CHECK_PARITY
, 1 },
238 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE
, 0 },
239 { PORT100_IN_PROT_VALID_BIT_NUMBER
, 7 },
240 { PORT100_IN_PROT_CRYPTO1
, 0 },
241 { PORT100_IN_PROT_ADD_SOF
, 0 },
242 { PORT100_IN_PROT_CHECK_SOF
, 0 },
243 { PORT100_IN_PROT_ADD_EOF
, 0 },
244 { PORT100_IN_PROT_CHECK_EOF
, 0 },
245 { PORT100_IN_PROT_DEAF_TIME
, 4 },
246 { PORT100_IN_PROT_CRM
, 0 },
247 { PORT100_IN_PROT_CRM_MIN_LEN
, 0 },
248 { PORT100_IN_PROT_T1_TAG_FRAME
, 0 },
249 { PORT100_IN_PROT_RFCA
, 0 },
250 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR
, 6 },
251 { PORT100_IN_PROT_END
, 0 },
253 [NFC_DIGITAL_FRAMING_NFCA_STANDARD
] = {
254 { PORT100_IN_PROT_INITIAL_GUARD_TIME
, 6 },
255 { PORT100_IN_PROT_ADD_CRC
, 0 },
256 { PORT100_IN_PROT_CHECK_CRC
, 0 },
257 { PORT100_IN_PROT_MULTI_CARD
, 0 },
258 { PORT100_IN_PROT_ADD_PARITY
, 1 },
259 { PORT100_IN_PROT_CHECK_PARITY
, 1 },
260 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE
, 0 },
261 { PORT100_IN_PROT_VALID_BIT_NUMBER
, 8 },
262 { PORT100_IN_PROT_CRYPTO1
, 0 },
263 { PORT100_IN_PROT_ADD_SOF
, 0 },
264 { PORT100_IN_PROT_CHECK_SOF
, 0 },
265 { PORT100_IN_PROT_ADD_EOF
, 0 },
266 { PORT100_IN_PROT_CHECK_EOF
, 0 },
267 { PORT100_IN_PROT_DEAF_TIME
, 4 },
268 { PORT100_IN_PROT_CRM
, 0 },
269 { PORT100_IN_PROT_CRM_MIN_LEN
, 0 },
270 { PORT100_IN_PROT_T1_TAG_FRAME
, 0 },
271 { PORT100_IN_PROT_RFCA
, 0 },
272 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR
, 6 },
273 { PORT100_IN_PROT_END
, 0 },
275 [NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A
] = {
276 { PORT100_IN_PROT_INITIAL_GUARD_TIME
, 6 },
277 { PORT100_IN_PROT_ADD_CRC
, 1 },
278 { PORT100_IN_PROT_CHECK_CRC
, 1 },
279 { PORT100_IN_PROT_MULTI_CARD
, 0 },
280 { PORT100_IN_PROT_ADD_PARITY
, 1 },
281 { PORT100_IN_PROT_CHECK_PARITY
, 1 },
282 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE
, 0 },
283 { PORT100_IN_PROT_VALID_BIT_NUMBER
, 8 },
284 { PORT100_IN_PROT_CRYPTO1
, 0 },
285 { PORT100_IN_PROT_ADD_SOF
, 0 },
286 { PORT100_IN_PROT_CHECK_SOF
, 0 },
287 { PORT100_IN_PROT_ADD_EOF
, 0 },
288 { PORT100_IN_PROT_CHECK_EOF
, 0 },
289 { PORT100_IN_PROT_DEAF_TIME
, 4 },
290 { PORT100_IN_PROT_CRM
, 0 },
291 { PORT100_IN_PROT_CRM_MIN_LEN
, 0 },
292 { PORT100_IN_PROT_T1_TAG_FRAME
, 0 },
293 { PORT100_IN_PROT_RFCA
, 0 },
294 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR
, 6 },
295 { PORT100_IN_PROT_END
, 0 },
297 [NFC_DIGITAL_FRAMING_NFCA_T1T
] = {
298 /* nfc_digital_framing_nfca_short */
299 { PORT100_IN_PROT_ADD_CRC
, 2 },
300 { PORT100_IN_PROT_CHECK_CRC
, 2 },
301 { PORT100_IN_PROT_VALID_BIT_NUMBER
, 8 },
302 { PORT100_IN_PROT_T1_TAG_FRAME
, 2 },
303 { PORT100_IN_PROT_END
, 0 },
305 [NFC_DIGITAL_FRAMING_NFCA_T2T
] = {
306 /* nfc_digital_framing_nfca_standard */
307 { PORT100_IN_PROT_ADD_CRC
, 1 },
308 { PORT100_IN_PROT_CHECK_CRC
, 0 },
309 { PORT100_IN_PROT_END
, 0 },
311 [NFC_DIGITAL_FRAMING_NFCA_T4T
] = {
312 /* nfc_digital_framing_nfca_standard_with_crc_a */
313 { PORT100_IN_PROT_END
, 0 },
315 [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP
] = {
316 /* nfc_digital_framing_nfca_standard */
317 { PORT100_IN_PROT_END
, 0 },
319 [NFC_DIGITAL_FRAMING_NFCF
] = {
320 { PORT100_IN_PROT_INITIAL_GUARD_TIME
, 18 },
321 { PORT100_IN_PROT_ADD_CRC
, 1 },
322 { PORT100_IN_PROT_CHECK_CRC
, 1 },
323 { PORT100_IN_PROT_MULTI_CARD
, 0 },
324 { PORT100_IN_PROT_ADD_PARITY
, 0 },
325 { PORT100_IN_PROT_CHECK_PARITY
, 0 },
326 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE
, 0 },
327 { PORT100_IN_PROT_VALID_BIT_NUMBER
, 8 },
328 { PORT100_IN_PROT_CRYPTO1
, 0 },
329 { PORT100_IN_PROT_ADD_SOF
, 0 },
330 { PORT100_IN_PROT_CHECK_SOF
, 0 },
331 { PORT100_IN_PROT_ADD_EOF
, 0 },
332 { PORT100_IN_PROT_CHECK_EOF
, 0 },
333 { PORT100_IN_PROT_DEAF_TIME
, 4 },
334 { PORT100_IN_PROT_CRM
, 0 },
335 { PORT100_IN_PROT_CRM_MIN_LEN
, 0 },
336 { PORT100_IN_PROT_T1_TAG_FRAME
, 0 },
337 { PORT100_IN_PROT_RFCA
, 0 },
338 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR
, 6 },
339 { PORT100_IN_PROT_END
, 0 },
341 [NFC_DIGITAL_FRAMING_NFCF_T3T
] = {
342 /* nfc_digital_framing_nfcf */
343 { PORT100_IN_PROT_END
, 0 },
345 [NFC_DIGITAL_FRAMING_NFCF_NFC_DEP
] = {
346 /* nfc_digital_framing_nfcf */
347 { PORT100_IN_PROT_INITIAL_GUARD_TIME
, 18 },
348 { PORT100_IN_PROT_ADD_CRC
, 1 },
349 { PORT100_IN_PROT_CHECK_CRC
, 1 },
350 { PORT100_IN_PROT_MULTI_CARD
, 0 },
351 { PORT100_IN_PROT_ADD_PARITY
, 0 },
352 { PORT100_IN_PROT_CHECK_PARITY
, 0 },
353 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE
, 0 },
354 { PORT100_IN_PROT_VALID_BIT_NUMBER
, 8 },
355 { PORT100_IN_PROT_CRYPTO1
, 0 },
356 { PORT100_IN_PROT_ADD_SOF
, 0 },
357 { PORT100_IN_PROT_CHECK_SOF
, 0 },
358 { PORT100_IN_PROT_ADD_EOF
, 0 },
359 { PORT100_IN_PROT_CHECK_EOF
, 0 },
360 { PORT100_IN_PROT_DEAF_TIME
, 4 },
361 { PORT100_IN_PROT_CRM
, 0 },
362 { PORT100_IN_PROT_CRM_MIN_LEN
, 0 },
363 { PORT100_IN_PROT_T1_TAG_FRAME
, 0 },
364 { PORT100_IN_PROT_RFCA
, 0 },
365 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR
, 6 },
366 { PORT100_IN_PROT_END
, 0 },
368 [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED
] = {
369 { PORT100_IN_PROT_END
, 0 },
371 [NFC_DIGITAL_FRAMING_NFCB
] = {
372 { PORT100_IN_PROT_INITIAL_GUARD_TIME
, 20 },
373 { PORT100_IN_PROT_ADD_CRC
, 1 },
374 { PORT100_IN_PROT_CHECK_CRC
, 1 },
375 { PORT100_IN_PROT_MULTI_CARD
, 0 },
376 { PORT100_IN_PROT_ADD_PARITY
, 0 },
377 { PORT100_IN_PROT_CHECK_PARITY
, 0 },
378 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE
, 0 },
379 { PORT100_IN_PROT_VALID_BIT_NUMBER
, 8 },
380 { PORT100_IN_PROT_CRYPTO1
, 0 },
381 { PORT100_IN_PROT_ADD_SOF
, 1 },
382 { PORT100_IN_PROT_CHECK_SOF
, 1 },
383 { PORT100_IN_PROT_ADD_EOF
, 1 },
384 { PORT100_IN_PROT_CHECK_EOF
, 1 },
385 { PORT100_IN_PROT_DEAF_TIME
, 4 },
386 { PORT100_IN_PROT_CRM
, 0 },
387 { PORT100_IN_PROT_CRM_MIN_LEN
, 0 },
388 { PORT100_IN_PROT_T1_TAG_FRAME
, 0 },
389 { PORT100_IN_PROT_RFCA
, 0 },
390 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR
, 6 },
391 { PORT100_IN_PROT_END
, 0 },
393 [NFC_DIGITAL_FRAMING_NFCB_T4T
] = {
394 /* nfc_digital_framing_nfcb */
395 { PORT100_IN_PROT_END
, 0 },
397 /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
398 [NFC_DIGITAL_FRAMING_LAST
] = {
399 { PORT100_IN_PROT_END
, 0 },
403 static struct port100_protocol
404 tg_protocols
[][PORT100_TG_MAX_NUM_PROTOCOLS
+ 1] = {
405 [NFC_DIGITAL_FRAMING_NFCA_SHORT
] = {
406 { PORT100_TG_PROT_END
, 0 },
408 [NFC_DIGITAL_FRAMING_NFCA_STANDARD
] = {
409 { PORT100_TG_PROT_END
, 0 },
411 [NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A
] = {
412 { PORT100_TG_PROT_END
, 0 },
414 [NFC_DIGITAL_FRAMING_NFCA_T1T
] = {
415 { PORT100_TG_PROT_END
, 0 },
417 [NFC_DIGITAL_FRAMING_NFCA_T2T
] = {
418 { PORT100_TG_PROT_END
, 0 },
420 [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP
] = {
421 { PORT100_TG_PROT_TU
, 1 },
422 { PORT100_TG_PROT_RF_OFF
, 0 },
423 { PORT100_TG_PROT_CRM
, 7 },
424 { PORT100_TG_PROT_END
, 0 },
426 [NFC_DIGITAL_FRAMING_NFCF
] = {
427 { PORT100_TG_PROT_END
, 0 },
429 [NFC_DIGITAL_FRAMING_NFCF_T3T
] = {
430 { PORT100_TG_PROT_END
, 0 },
432 [NFC_DIGITAL_FRAMING_NFCF_NFC_DEP
] = {
433 { PORT100_TG_PROT_TU
, 1 },
434 { PORT100_TG_PROT_RF_OFF
, 0 },
435 { PORT100_TG_PROT_CRM
, 7 },
436 { PORT100_TG_PROT_END
, 0 },
438 [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED
] = {
439 { PORT100_TG_PROT_RF_OFF
, 1 },
440 { PORT100_TG_PROT_END
, 0 },
442 /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
443 [NFC_DIGITAL_FRAMING_LAST
] = {
444 { PORT100_TG_PROT_END
, 0 },
449 struct nfc_digital_dev
*nfc_digital_dev
;
454 struct usb_device
*udev
;
455 struct usb_interface
*interface
;
460 /* This mutex protects the out_urb and avoids to submit a new command
461 * through port100_send_frame_async() while the previous one is being
462 * canceled through port100_abort_cmd().
464 struct mutex out_urb_lock
;
466 struct work_struct cmd_complete_work
;
470 /* The digital stack serializes commands to be sent. There is no need
471 * for any queuing/locking mechanism at driver level.
473 struct port100_cmd
*cmd
;
476 struct completion cmd_cancel_done
;
483 struct sk_buff
*resp
;
485 port100_send_async_complete_t complete_cb
;
486 void *complete_cb_context
;
489 struct port100_frame
{
492 __be16 extended_frame
;
498 struct port100_ack_frame
{
505 struct port100_cb_arg
{
506 nfc_digital_cmd_complete_t complete_cb
;
511 struct port100_tg_comm_rf_cmd
{
523 struct port100_tg_comm_rf_res
{
531 /* The rule: value + checksum = 0 */
532 static inline u8
port100_checksum(u16 value
)
534 return ~(((u8
*)&value
)[0] + ((u8
*)&value
)[1]) + 1;
537 /* The rule: sum(data elements) + checksum = 0 */
538 static u8
port100_data_checksum(u8
*data
, int datalen
)
543 for (i
= 0; i
< datalen
; i
++)
546 return port100_checksum(sum
);
549 static void port100_tx_frame_init(void *_frame
, u8 cmd_code
)
551 struct port100_frame
*frame
= _frame
;
554 frame
->start_frame
= cpu_to_be16(PORT100_FRAME_SOF
);
555 frame
->extended_frame
= cpu_to_be16(PORT100_FRAME_EXT
);
556 PORT100_FRAME_DIRECTION(frame
) = PORT100_FRAME_DIR_OUT
;
557 PORT100_FRAME_CMD(frame
) = cmd_code
;
558 frame
->datalen
= cpu_to_le16(2);
561 static void port100_tx_frame_finish(void *_frame
)
563 struct port100_frame
*frame
= _frame
;
565 frame
->datalen_checksum
= port100_checksum(le16_to_cpu(frame
->datalen
));
567 PORT100_FRAME_CHECKSUM(frame
) =
568 port100_data_checksum(frame
->data
, le16_to_cpu(frame
->datalen
));
570 PORT100_FRAME_POSTAMBLE(frame
) = 0;
573 static void port100_tx_update_payload_len(void *_frame
, int len
)
575 struct port100_frame
*frame
= _frame
;
577 frame
->datalen
= cpu_to_le16(le16_to_cpu(frame
->datalen
) + len
);
580 static bool port100_rx_frame_is_valid(void *_frame
)
583 struct port100_frame
*frame
= _frame
;
585 if (frame
->start_frame
!= cpu_to_be16(PORT100_FRAME_SOF
) ||
586 frame
->extended_frame
!= cpu_to_be16(PORT100_FRAME_EXT
))
589 checksum
= port100_checksum(le16_to_cpu(frame
->datalen
));
590 if (checksum
!= frame
->datalen_checksum
)
593 checksum
= port100_data_checksum(frame
->data
,
594 le16_to_cpu(frame
->datalen
));
595 if (checksum
!= PORT100_FRAME_CHECKSUM(frame
))
601 static bool port100_rx_frame_is_ack(struct port100_ack_frame
*frame
)
603 return (frame
->start_frame
== cpu_to_be16(PORT100_FRAME_SOF
) &&
604 frame
->ack_frame
== cpu_to_be16(PORT100_FRAME_ACK
));
607 static inline int port100_rx_frame_size(void *frame
)
609 struct port100_frame
*f
= frame
;
611 return sizeof(struct port100_frame
) + le16_to_cpu(f
->datalen
) +
612 PORT100_FRAME_TAIL_LEN
;
615 static bool port100_rx_frame_is_cmd_response(struct port100
*dev
, void *frame
)
617 struct port100_frame
*f
= frame
;
619 return (PORT100_FRAME_CMD(f
) == PORT100_CMD_RESPONSE(dev
->cmd
->code
));
622 static void port100_recv_response(struct urb
*urb
)
624 struct port100
*dev
= urb
->context
;
625 struct port100_cmd
*cmd
= dev
->cmd
;
628 cmd
->status
= urb
->status
;
630 switch (urb
->status
) {
635 nfc_err(&dev
->interface
->dev
,
636 "The urb has been canceled (status %d)\n", urb
->status
);
640 nfc_err(&dev
->interface
->dev
, "Urb failure (status %d)\n",
645 in_frame
= dev
->in_urb
->transfer_buffer
;
647 if (!port100_rx_frame_is_valid(in_frame
)) {
648 nfc_err(&dev
->interface
->dev
, "Received an invalid frame\n");
653 print_hex_dump_debug("PORT100 RX: ", DUMP_PREFIX_NONE
, 16, 1, in_frame
,
654 port100_rx_frame_size(in_frame
), false);
656 if (!port100_rx_frame_is_cmd_response(dev
, in_frame
)) {
657 nfc_err(&dev
->interface
->dev
,
658 "It's not the response to the last command\n");
664 schedule_work(&dev
->cmd_complete_work
);
667 static int port100_submit_urb_for_response(struct port100
*dev
, gfp_t flags
)
669 dev
->in_urb
->complete
= port100_recv_response
;
671 return usb_submit_urb(dev
->in_urb
, flags
);
674 static void port100_recv_ack(struct urb
*urb
)
676 struct port100
*dev
= urb
->context
;
677 struct port100_cmd
*cmd
= dev
->cmd
;
678 struct port100_ack_frame
*in_frame
;
681 cmd
->status
= urb
->status
;
683 switch (urb
->status
) {
688 nfc_err(&dev
->interface
->dev
,
689 "The urb has been stopped (status %d)\n", urb
->status
);
693 nfc_err(&dev
->interface
->dev
, "Urb failure (status %d)\n",
698 in_frame
= dev
->in_urb
->transfer_buffer
;
700 if (!port100_rx_frame_is_ack(in_frame
)) {
701 nfc_err(&dev
->interface
->dev
, "Received an invalid ack\n");
706 rc
= port100_submit_urb_for_response(dev
, GFP_ATOMIC
);
708 nfc_err(&dev
->interface
->dev
,
709 "usb_submit_urb failed with result %d\n", rc
);
717 schedule_work(&dev
->cmd_complete_work
);
720 static int port100_submit_urb_for_ack(struct port100
*dev
, gfp_t flags
)
722 dev
->in_urb
->complete
= port100_recv_ack
;
724 return usb_submit_urb(dev
->in_urb
, flags
);
727 static int port100_send_ack(struct port100
*dev
)
731 mutex_lock(&dev
->out_urb_lock
);
734 * If prior cancel is in-flight (dev->cmd_cancel == true), we
735 * can skip to send cancel. Then this will wait the prior
736 * cancel, or merged into the next cancel rarely if next
737 * cancel was started before waiting done. In any case, this
738 * will be waked up soon or later.
740 if (!dev
->cmd_cancel
) {
741 reinit_completion(&dev
->cmd_cancel_done
);
743 usb_kill_urb(dev
->out_urb
);
745 dev
->out_urb
->transfer_buffer
= ack_frame
;
746 dev
->out_urb
->transfer_buffer_length
= sizeof(ack_frame
);
747 rc
= usb_submit_urb(dev
->out_urb
, GFP_KERNEL
);
750 * Set the cmd_cancel flag only if the URB has been
751 * successfully submitted. It will be reset by the out
752 * URB completion callback port100_send_complete().
754 dev
->cmd_cancel
= !rc
;
757 mutex_unlock(&dev
->out_urb_lock
);
760 wait_for_completion(&dev
->cmd_cancel_done
);
765 static int port100_send_frame_async(struct port100
*dev
, struct sk_buff
*out
,
766 struct sk_buff
*in
, int in_len
)
770 mutex_lock(&dev
->out_urb_lock
);
772 /* A command cancel frame as been sent through dev->out_urb. Don't try
773 * to submit a new one.
775 if (dev
->cmd_cancel
) {
780 dev
->out_urb
->transfer_buffer
= out
->data
;
781 dev
->out_urb
->transfer_buffer_length
= out
->len
;
783 dev
->in_urb
->transfer_buffer
= in
->data
;
784 dev
->in_urb
->transfer_buffer_length
= in_len
;
786 print_hex_dump_debug("PORT100 TX: ", DUMP_PREFIX_NONE
, 16, 1,
787 out
->data
, out
->len
, false);
789 rc
= usb_submit_urb(dev
->out_urb
, GFP_KERNEL
);
793 rc
= port100_submit_urb_for_ack(dev
, GFP_KERNEL
);
795 usb_unlink_urb(dev
->out_urb
);
798 mutex_unlock(&dev
->out_urb_lock
);
803 static void port100_build_cmd_frame(struct port100
*dev
, u8 cmd_code
,
806 /* payload is already there, just update datalen */
807 int payload_len
= skb
->len
;
809 skb_push(skb
, PORT100_FRAME_HEADER_LEN
);
810 skb_put(skb
, PORT100_FRAME_TAIL_LEN
);
812 port100_tx_frame_init(skb
->data
, cmd_code
);
813 port100_tx_update_payload_len(skb
->data
, payload_len
);
814 port100_tx_frame_finish(skb
->data
);
817 static void port100_send_async_complete(struct port100
*dev
)
819 struct port100_cmd
*cmd
= dev
->cmd
;
820 int status
= cmd
->status
;
822 struct sk_buff
*req
= cmd
->req
;
823 struct sk_buff
*resp
= cmd
->resp
;
830 cmd
->complete_cb(dev
, cmd
->complete_cb_context
,
836 skb_put(resp
, port100_rx_frame_size(resp
->data
));
837 skb_pull(resp
, PORT100_FRAME_HEADER_LEN
);
838 skb_trim(resp
, resp
->len
- PORT100_FRAME_TAIL_LEN
);
840 cmd
->complete_cb(dev
, cmd
->complete_cb_context
, resp
);
846 static int port100_send_cmd_async(struct port100
*dev
, u8 cmd_code
,
848 port100_send_async_complete_t complete_cb
,
849 void *complete_cb_context
)
851 struct port100_cmd
*cmd
;
852 struct sk_buff
*resp
;
854 int resp_len
= PORT100_FRAME_HEADER_LEN
+
855 PORT100_FRAME_MAX_PAYLOAD_LEN
+
856 PORT100_FRAME_TAIL_LEN
;
859 nfc_err(&dev
->interface
->dev
,
860 "A command is still in process\n");
864 resp
= alloc_skb(resp_len
, GFP_KERNEL
);
868 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
874 cmd
->code
= cmd_code
;
877 cmd
->resp_len
= resp_len
;
878 cmd
->complete_cb
= complete_cb
;
879 cmd
->complete_cb_context
= complete_cb_context
;
881 port100_build_cmd_frame(dev
, cmd_code
, req
);
885 rc
= port100_send_frame_async(dev
, req
, resp
, resp_len
);
895 struct port100_sync_cmd_response
{
896 struct sk_buff
*resp
;
897 struct completion done
;
900 static void port100_wq_cmd_complete(struct work_struct
*work
)
902 struct port100
*dev
= container_of(work
, struct port100
,
905 port100_send_async_complete(dev
);
908 static void port100_send_sync_complete(struct port100
*dev
, void *_arg
,
909 struct sk_buff
*resp
)
911 struct port100_sync_cmd_response
*arg
= _arg
;
914 complete(&arg
->done
);
917 static struct sk_buff
*port100_send_cmd_sync(struct port100
*dev
, u8 cmd_code
,
921 struct port100_sync_cmd_response arg
;
923 init_completion(&arg
.done
);
925 rc
= port100_send_cmd_async(dev
, cmd_code
, req
,
926 port100_send_sync_complete
, &arg
);
932 wait_for_completion(&arg
.done
);
937 static void port100_send_complete(struct urb
*urb
)
939 struct port100
*dev
= urb
->context
;
941 if (dev
->cmd_cancel
) {
942 complete_all(&dev
->cmd_cancel_done
);
943 dev
->cmd_cancel
= false;
946 switch (urb
->status
) {
951 nfc_err(&dev
->interface
->dev
,
952 "The urb has been stopped (status %d)\n", urb
->status
);
956 nfc_err(&dev
->interface
->dev
, "Urb failure (status %d)\n",
961 static void port100_abort_cmd(struct nfc_digital_dev
*ddev
)
963 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
965 /* An ack will cancel the last issued command */
966 port100_send_ack(dev
);
968 /* cancel the urb request */
969 usb_kill_urb(dev
->in_urb
);
972 static struct sk_buff
*port100_alloc_skb(struct port100
*dev
, unsigned int size
)
976 skb
= alloc_skb(dev
->skb_headroom
+ dev
->skb_tailroom
+ size
,
979 skb_reserve(skb
, dev
->skb_headroom
);
984 static int port100_set_command_type(struct port100
*dev
, u8 command_type
)
987 struct sk_buff
*resp
;
990 skb
= port100_alloc_skb(dev
, 1);
994 *skb_put(skb
, sizeof(u8
)) = command_type
;
996 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_SET_COMMAND_TYPE
, skb
);
998 return PTR_ERR(resp
);
1002 dev_kfree_skb(resp
);
1007 static u64
port100_get_command_type_mask(struct port100
*dev
)
1009 struct sk_buff
*skb
;
1010 struct sk_buff
*resp
;
1013 skb
= port100_alloc_skb(dev
, 0);
1017 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_GET_COMMAND_TYPE
, skb
);
1019 return PTR_ERR(resp
);
1024 mask
= be64_to_cpu(*(__be64
*)resp
->data
);
1026 dev_kfree_skb(resp
);
1031 static u16
port100_get_firmware_version(struct port100
*dev
)
1033 struct sk_buff
*skb
;
1034 struct sk_buff
*resp
;
1037 skb
= port100_alloc_skb(dev
, 0);
1041 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_GET_FIRMWARE_VERSION
,
1046 fw_ver
= le16_to_cpu(*(__le16
*)resp
->data
);
1048 dev_kfree_skb(resp
);
1053 static int port100_switch_rf(struct nfc_digital_dev
*ddev
, bool on
)
1055 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1056 struct sk_buff
*skb
, *resp
;
1058 skb
= port100_alloc_skb(dev
, 1);
1062 *skb_put(skb
, 1) = on
? 1 : 0;
1064 /* Cancel the last command if the device is being switched off */
1066 port100_abort_cmd(ddev
);
1068 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_SWITCH_RF
, skb
);
1071 return PTR_ERR(resp
);
1073 dev_kfree_skb(resp
);
1078 static int port100_in_set_rf(struct nfc_digital_dev
*ddev
, u8 rf
)
1080 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1081 struct sk_buff
*skb
;
1082 struct sk_buff
*resp
;
1085 if (rf
>= NFC_DIGITAL_RF_TECH_LAST
)
1088 skb
= port100_alloc_skb(dev
, sizeof(struct port100_in_rf_setting
));
1092 memcpy(skb_put(skb
, sizeof(struct port100_in_rf_setting
)),
1093 &in_rf_settings
[rf
],
1094 sizeof(struct port100_in_rf_setting
));
1096 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_IN_SET_RF
, skb
);
1099 return PTR_ERR(resp
);
1103 dev_kfree_skb(resp
);
1108 static int port100_in_set_framing(struct nfc_digital_dev
*ddev
, int param
)
1110 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1111 struct port100_protocol
*protocols
;
1112 struct sk_buff
*skb
;
1113 struct sk_buff
*resp
;
1118 if (param
>= NFC_DIGITAL_FRAMING_LAST
)
1121 protocols
= in_protocols
[param
];
1124 while (protocols
[num_protocols
].number
!= PORT100_IN_PROT_END
)
1130 size
= sizeof(struct port100_protocol
) * num_protocols
;
1132 skb
= port100_alloc_skb(dev
, size
);
1136 memcpy(skb_put(skb
, size
), protocols
, size
);
1138 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_IN_SET_PROTOCOL
, skb
);
1141 return PTR_ERR(resp
);
1145 dev_kfree_skb(resp
);
1150 static int port100_in_configure_hw(struct nfc_digital_dev
*ddev
, int type
,
1153 if (type
== NFC_DIGITAL_CONFIG_RF_TECH
)
1154 return port100_in_set_rf(ddev
, param
);
1156 if (type
== NFC_DIGITAL_CONFIG_FRAMING
)
1157 return port100_in_set_framing(ddev
, param
);
1162 static void port100_in_comm_rf_complete(struct port100
*dev
, void *arg
,
1163 struct sk_buff
*resp
)
1165 struct port100_cb_arg
*cb_arg
= arg
;
1166 nfc_digital_cmd_complete_t cb
= cb_arg
->complete_cb
;
1175 if (resp
->len
< 4) {
1176 nfc_err(&dev
->interface
->dev
,
1177 "Invalid packet length received\n");
1182 status
= le32_to_cpu(*(__le32
*)resp
->data
);
1184 skb_pull(resp
, sizeof(u32
));
1186 if (status
== PORT100_CMD_STATUS_TIMEOUT
) {
1191 if (status
!= PORT100_CMD_STATUS_OK
) {
1192 nfc_err(&dev
->interface
->dev
,
1193 "in_comm_rf failed with status 0x%08x\n", status
);
1198 /* Remove collision bits byte */
1208 cb(dev
->nfc_digital_dev
, cb_arg
->complete_arg
, resp
);
1213 static int port100_in_send_cmd(struct nfc_digital_dev
*ddev
,
1214 struct sk_buff
*skb
, u16 _timeout
,
1215 nfc_digital_cmd_complete_t cb
, void *arg
)
1217 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1218 struct port100_cb_arg
*cb_arg
;
1221 cb_arg
= kzalloc(sizeof(struct port100_cb_arg
), GFP_KERNEL
);
1225 cb_arg
->complete_cb
= cb
;
1226 cb_arg
->complete_arg
= arg
;
1228 timeout
= cpu_to_le16(_timeout
* 10);
1230 memcpy(skb_push(skb
, sizeof(__le16
)), &timeout
, sizeof(__le16
));
1232 return port100_send_cmd_async(dev
, PORT100_CMD_IN_COMM_RF
, skb
,
1233 port100_in_comm_rf_complete
, cb_arg
);
1236 static int port100_tg_set_rf(struct nfc_digital_dev
*ddev
, u8 rf
)
1238 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1239 struct sk_buff
*skb
;
1240 struct sk_buff
*resp
;
1243 if (rf
>= NFC_DIGITAL_RF_TECH_LAST
)
1246 skb
= port100_alloc_skb(dev
, sizeof(struct port100_tg_rf_setting
));
1250 memcpy(skb_put(skb
, sizeof(struct port100_tg_rf_setting
)),
1251 &tg_rf_settings
[rf
],
1252 sizeof(struct port100_tg_rf_setting
));
1254 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_TG_SET_RF
, skb
);
1257 return PTR_ERR(resp
);
1261 dev_kfree_skb(resp
);
1266 static int port100_tg_set_framing(struct nfc_digital_dev
*ddev
, int param
)
1268 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1269 struct port100_protocol
*protocols
;
1270 struct sk_buff
*skb
;
1271 struct sk_buff
*resp
;
1276 if (param
>= NFC_DIGITAL_FRAMING_LAST
)
1279 protocols
= tg_protocols
[param
];
1282 while (protocols
[num_protocols
].number
!= PORT100_TG_PROT_END
)
1288 size
= sizeof(struct port100_protocol
) * num_protocols
;
1290 skb
= port100_alloc_skb(dev
, size
);
1294 memcpy(skb_put(skb
, size
), protocols
, size
);
1296 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_TG_SET_PROTOCOL
, skb
);
1299 return PTR_ERR(resp
);
1303 dev_kfree_skb(resp
);
1308 static int port100_tg_configure_hw(struct nfc_digital_dev
*ddev
, int type
,
1311 if (type
== NFC_DIGITAL_CONFIG_RF_TECH
)
1312 return port100_tg_set_rf(ddev
, param
);
1314 if (type
== NFC_DIGITAL_CONFIG_FRAMING
)
1315 return port100_tg_set_framing(ddev
, param
);
1320 static bool port100_tg_target_activated(struct port100
*dev
, u8 tgt_activated
)
1324 switch (dev
->cmd_type
) {
1325 case PORT100_CMD_TYPE_0
:
1326 mask
= PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK
;
1328 case PORT100_CMD_TYPE_1
:
1329 mask
= PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK
|
1330 PORT100_MDAA_TGT_WAS_ACTIVATED_MASK
;
1333 nfc_err(&dev
->interface
->dev
, "Unknown command type\n");
1337 return ((tgt_activated
& mask
) == mask
);
1340 static void port100_tg_comm_rf_complete(struct port100
*dev
, void *arg
,
1341 struct sk_buff
*resp
)
1344 struct port100_cb_arg
*cb_arg
= arg
;
1345 nfc_digital_cmd_complete_t cb
= cb_arg
->complete_cb
;
1346 struct port100_tg_comm_rf_res
*hdr
;
1351 hdr
= (struct port100_tg_comm_rf_res
*)resp
->data
;
1353 status
= le32_to_cpu(hdr
->status
);
1356 !port100_tg_target_activated(dev
, hdr
->target_activated
)) {
1358 resp
= ERR_PTR(-ETIMEDOUT
);
1363 skb_pull(resp
, sizeof(struct port100_tg_comm_rf_res
));
1365 if (status
!= PORT100_CMD_STATUS_OK
) {
1368 if (status
== PORT100_CMD_STATUS_TIMEOUT
)
1369 resp
= ERR_PTR(-ETIMEDOUT
);
1371 resp
= ERR_PTR(-EIO
);
1375 cb(dev
->nfc_digital_dev
, cb_arg
->complete_arg
, resp
);
1380 static int port100_tg_send_cmd(struct nfc_digital_dev
*ddev
,
1381 struct sk_buff
*skb
, u16 timeout
,
1382 nfc_digital_cmd_complete_t cb
, void *arg
)
1384 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1385 struct port100_tg_comm_rf_cmd
*hdr
;
1386 struct port100_cb_arg
*cb_arg
;
1388 cb_arg
= kzalloc(sizeof(struct port100_cb_arg
), GFP_KERNEL
);
1392 cb_arg
->complete_cb
= cb
;
1393 cb_arg
->complete_arg
= arg
;
1395 skb_push(skb
, sizeof(struct port100_tg_comm_rf_cmd
));
1397 hdr
= (struct port100_tg_comm_rf_cmd
*)skb
->data
;
1399 memset(hdr
, 0, sizeof(struct port100_tg_comm_rf_cmd
));
1400 hdr
->guard_time
= cpu_to_le16(500);
1401 hdr
->send_timeout
= cpu_to_le16(0xFFFF);
1402 hdr
->recv_timeout
= cpu_to_le16(timeout
);
1404 return port100_send_cmd_async(dev
, PORT100_CMD_TG_COMM_RF
, skb
,
1405 port100_tg_comm_rf_complete
, cb_arg
);
1408 static int port100_listen_mdaa(struct nfc_digital_dev
*ddev
,
1409 struct digital_tg_mdaa_params
*params
,
1411 nfc_digital_cmd_complete_t cb
, void *arg
)
1413 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1414 struct port100_tg_comm_rf_cmd
*hdr
;
1415 struct port100_cb_arg
*cb_arg
;
1416 struct sk_buff
*skb
;
1419 rc
= port100_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_RF_TECH
,
1420 NFC_DIGITAL_RF_TECH_106A
);
1424 rc
= port100_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
1425 NFC_DIGITAL_FRAMING_NFCA_NFC_DEP
);
1429 cb_arg
= kzalloc(sizeof(struct port100_cb_arg
), GFP_KERNEL
);
1433 cb_arg
->complete_cb
= cb
;
1434 cb_arg
->complete_arg
= arg
;
1437 skb
= port100_alloc_skb(dev
, 0);
1443 skb_push(skb
, sizeof(struct port100_tg_comm_rf_cmd
));
1444 hdr
= (struct port100_tg_comm_rf_cmd
*)skb
->data
;
1446 memset(hdr
, 0, sizeof(struct port100_tg_comm_rf_cmd
));
1448 hdr
->guard_time
= 0;
1449 hdr
->send_timeout
= cpu_to_le16(0xFFFF);
1451 hdr
->nfca_param
[0] = (params
->sens_res
>> 8) & 0xFF;
1452 hdr
->nfca_param
[1] = params
->sens_res
& 0xFF;
1453 memcpy(hdr
->nfca_param
+ 2, params
->nfcid1
, 3);
1454 hdr
->nfca_param
[5] = params
->sel_res
;
1455 memcpy(hdr
->nfcf_param
, params
->nfcid2
, 8);
1456 hdr
->nfcf_param
[16] = (params
->sc
>> 8) & 0xFF;
1457 hdr
->nfcf_param
[17] = params
->sc
& 0xFF;
1458 hdr
->recv_timeout
= cpu_to_le16(timeout
);
1460 return port100_send_cmd_async(dev
, PORT100_CMD_TG_COMM_RF
, skb
,
1461 port100_tg_comm_rf_complete
, cb_arg
);
1464 static int port100_listen(struct nfc_digital_dev
*ddev
, u16 timeout
,
1465 nfc_digital_cmd_complete_t cb
, void *arg
)
1467 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1468 struct sk_buff
*skb
;
1470 skb
= port100_alloc_skb(dev
, 0);
1474 return port100_tg_send_cmd(ddev
, skb
, timeout
, cb
, arg
);
1477 static struct nfc_digital_ops port100_digital_ops
= {
1478 .in_configure_hw
= port100_in_configure_hw
,
1479 .in_send_cmd
= port100_in_send_cmd
,
1481 .tg_listen_mdaa
= port100_listen_mdaa
,
1482 .tg_listen
= port100_listen
,
1483 .tg_configure_hw
= port100_tg_configure_hw
,
1484 .tg_send_cmd
= port100_tg_send_cmd
,
1486 .switch_rf
= port100_switch_rf
,
1487 .abort_cmd
= port100_abort_cmd
,
1490 static const struct usb_device_id port100_table
[] = {
1491 { USB_DEVICE(SONY_VENDOR_ID
, RCS380S_PRODUCT_ID
), },
1492 { USB_DEVICE(SONY_VENDOR_ID
, RCS380P_PRODUCT_ID
), },
1495 MODULE_DEVICE_TABLE(usb
, port100_table
);
1497 static int port100_probe(struct usb_interface
*interface
,
1498 const struct usb_device_id
*id
)
1500 struct port100
*dev
;
1502 struct usb_host_interface
*iface_desc
;
1503 struct usb_endpoint_descriptor
*endpoint
;
1510 dev
= devm_kzalloc(&interface
->dev
, sizeof(struct port100
), GFP_KERNEL
);
1514 mutex_init(&dev
->out_urb_lock
);
1515 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
1516 dev
->interface
= interface
;
1517 usb_set_intfdata(interface
, dev
);
1519 in_endpoint
= out_endpoint
= 0;
1520 iface_desc
= interface
->cur_altsetting
;
1521 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1522 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1524 if (!in_endpoint
&& usb_endpoint_is_bulk_in(endpoint
))
1525 in_endpoint
= endpoint
->bEndpointAddress
;
1527 if (!out_endpoint
&& usb_endpoint_is_bulk_out(endpoint
))
1528 out_endpoint
= endpoint
->bEndpointAddress
;
1531 if (!in_endpoint
|| !out_endpoint
) {
1532 nfc_err(&interface
->dev
,
1533 "Could not find bulk-in or bulk-out endpoint\n");
1538 dev
->in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1539 dev
->out_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1541 if (!dev
->in_urb
|| !dev
->out_urb
) {
1542 nfc_err(&interface
->dev
, "Could not allocate USB URBs\n");
1547 usb_fill_bulk_urb(dev
->in_urb
, dev
->udev
,
1548 usb_rcvbulkpipe(dev
->udev
, in_endpoint
),
1549 NULL
, 0, NULL
, dev
);
1550 usb_fill_bulk_urb(dev
->out_urb
, dev
->udev
,
1551 usb_sndbulkpipe(dev
->udev
, out_endpoint
),
1552 NULL
, 0, port100_send_complete
, dev
);
1553 dev
->out_urb
->transfer_flags
= URB_ZERO_PACKET
;
1555 dev
->skb_headroom
= PORT100_FRAME_HEADER_LEN
+
1556 PORT100_COMM_RF_HEAD_MAX_LEN
;
1557 dev
->skb_tailroom
= PORT100_FRAME_TAIL_LEN
;
1559 init_completion(&dev
->cmd_cancel_done
);
1560 INIT_WORK(&dev
->cmd_complete_work
, port100_wq_cmd_complete
);
1562 /* The first thing to do with the Port-100 is to set the command type
1563 * to be used. If supported we use command type 1. 0 otherwise.
1565 cmd_type_mask
= port100_get_command_type_mask(dev
);
1566 if (!cmd_type_mask
) {
1567 nfc_err(&interface
->dev
,
1568 "Could not get supported command types\n");
1573 if (PORT100_CMD_TYPE_IS_SUPPORTED(cmd_type_mask
, PORT100_CMD_TYPE_1
))
1574 dev
->cmd_type
= PORT100_CMD_TYPE_1
;
1576 dev
->cmd_type
= PORT100_CMD_TYPE_0
;
1578 rc
= port100_set_command_type(dev
, dev
->cmd_type
);
1580 nfc_err(&interface
->dev
,
1581 "The device does not support command type %u\n",
1586 fw_version
= port100_get_firmware_version(dev
);
1588 nfc_err(&interface
->dev
,
1589 "Could not get device firmware version\n");
1591 nfc_info(&interface
->dev
,
1592 "Sony NFC Port-100 Series attached (firmware v%x.%02x)\n",
1593 (fw_version
& 0xFF00) >> 8, fw_version
& 0xFF);
1595 dev
->nfc_digital_dev
= nfc_digital_allocate_device(&port100_digital_ops
,
1597 PORT100_CAPABILITIES
,
1600 if (!dev
->nfc_digital_dev
) {
1601 nfc_err(&interface
->dev
,
1602 "Could not allocate nfc_digital_dev\n");
1607 nfc_digital_set_parent_dev(dev
->nfc_digital_dev
, &interface
->dev
);
1608 nfc_digital_set_drvdata(dev
->nfc_digital_dev
, dev
);
1610 rc
= nfc_digital_register_device(dev
->nfc_digital_dev
);
1612 nfc_err(&interface
->dev
,
1613 "Could not register digital device\n");
1620 nfc_digital_free_device(dev
->nfc_digital_dev
);
1623 usb_free_urb(dev
->in_urb
);
1624 usb_free_urb(dev
->out_urb
);
1625 usb_put_dev(dev
->udev
);
1630 static void port100_disconnect(struct usb_interface
*interface
)
1632 struct port100
*dev
;
1634 dev
= usb_get_intfdata(interface
);
1635 usb_set_intfdata(interface
, NULL
);
1637 nfc_digital_unregister_device(dev
->nfc_digital_dev
);
1638 nfc_digital_free_device(dev
->nfc_digital_dev
);
1640 usb_kill_urb(dev
->in_urb
);
1641 usb_kill_urb(dev
->out_urb
);
1643 usb_free_urb(dev
->in_urb
);
1644 usb_free_urb(dev
->out_urb
);
1645 usb_put_dev(dev
->udev
);
1649 nfc_info(&interface
->dev
, "Sony Port-100 NFC device disconnected\n");
1652 static struct usb_driver port100_driver
= {
1654 .probe
= port100_probe
,
1655 .disconnect
= port100_disconnect
,
1656 .id_table
= port100_table
,
1659 module_usb_driver(port100_driver
);
1661 MODULE_DESCRIPTION("NFC Port-100 series usb driver ver " VERSION
);
1662 MODULE_VERSION(VERSION
);
1663 MODULE_LICENSE("GPL");