1 // SPDX-License-Identifier: GPL-2.0-only
3 * Sony NFC Port-100 Series driver
4 * Copyright (c) 2013, Intel Corporation.
6 * Partly based/Inspired by Stephen Tiedemann's nfcpy
9 #include <linux/module.h>
10 #include <linux/usb.h>
11 #include <net/nfc/digital.h>
15 #define SONY_VENDOR_ID 0x054c
16 #define RCS380S_PRODUCT_ID 0x06c1
17 #define RCS380P_PRODUCT_ID 0x06c3
19 #define PORT100_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
20 NFC_PROTO_MIFARE_MASK | \
21 NFC_PROTO_FELICA_MASK | \
22 NFC_PROTO_NFC_DEP_MASK | \
23 NFC_PROTO_ISO14443_MASK | \
24 NFC_PROTO_ISO14443_B_MASK)
26 #define PORT100_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC | \
27 NFC_DIGITAL_DRV_CAPS_TG_CRC)
29 /* Standard port100 frame definitions */
30 #define PORT100_FRAME_HEADER_LEN (sizeof(struct port100_frame) \
31 + 2) /* data[0] CC, data[1] SCC */
32 #define PORT100_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
34 #define PORT100_COMM_RF_HEAD_MAX_LEN (sizeof(struct port100_tg_comm_rf_cmd))
37 * Max extended frame payload len, excluding CC and SCC
38 * which are already in PORT100_FRAME_HEADER_LEN.
40 #define PORT100_FRAME_MAX_PAYLOAD_LEN 1001
42 #define PORT100_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
44 static u8 ack_frame
[PORT100_FRAME_ACK_SIZE
] = {
45 0x00, 0x00, 0xff, 0x00, 0xff, 0x00
48 #define PORT100_FRAME_CHECKSUM(f) (f->data[le16_to_cpu(f->datalen)])
49 #define PORT100_FRAME_POSTAMBLE(f) (f->data[le16_to_cpu(f->datalen) + 1])
52 #define PORT100_FRAME_SOF 0x00FF
53 #define PORT100_FRAME_EXT 0xFFFF
54 #define PORT100_FRAME_ACK 0x00FF
56 /* Port-100 command: in or out */
57 #define PORT100_FRAME_DIRECTION(f) (f->data[0]) /* CC */
58 #define PORT100_FRAME_DIR_OUT 0xD6
59 #define PORT100_FRAME_DIR_IN 0xD7
61 /* Port-100 sub-command */
62 #define PORT100_FRAME_CMD(f) (f->data[1]) /* SCC */
64 #define PORT100_CMD_GET_FIRMWARE_VERSION 0x20
65 #define PORT100_CMD_GET_COMMAND_TYPE 0x28
66 #define PORT100_CMD_SET_COMMAND_TYPE 0x2A
68 #define PORT100_CMD_IN_SET_RF 0x00
69 #define PORT100_CMD_IN_SET_PROTOCOL 0x02
70 #define PORT100_CMD_IN_COMM_RF 0x04
72 #define PORT100_CMD_TG_SET_RF 0x40
73 #define PORT100_CMD_TG_SET_PROTOCOL 0x42
74 #define PORT100_CMD_TG_SET_RF_OFF 0x46
75 #define PORT100_CMD_TG_COMM_RF 0x48
77 #define PORT100_CMD_SWITCH_RF 0x06
79 #define PORT100_CMD_RESPONSE(cmd) (cmd + 1)
81 #define PORT100_CMD_TYPE_IS_SUPPORTED(mask, cmd_type) \
82 ((mask) & (0x01 << (cmd_type)))
83 #define PORT100_CMD_TYPE_0 0
84 #define PORT100_CMD_TYPE_1 1
86 #define PORT100_CMD_STATUS_OK 0x00
87 #define PORT100_CMD_STATUS_TIMEOUT 0x80
89 #define PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK 0x01
90 #define PORT100_MDAA_TGT_WAS_ACTIVATED_MASK 0x02
94 typedef void (*port100_send_async_complete_t
)(struct port100
*dev
, void *arg
,
95 struct sk_buff
*resp
);
98 * Setting sets structure for in_set_rf command
100 * @in_*_set_number: Represent the entry indexes in the port-100 RF Base Table.
101 * This table contains multiple RF setting sets required for RF
104 * @in_*_comm_type: Theses fields set the communication type to be used.
106 struct port100_in_rf_setting
{
107 u8 in_send_set_number
;
108 u8 in_send_comm_type
;
109 u8 in_recv_set_number
;
110 u8 in_recv_comm_type
;
113 #define PORT100_COMM_TYPE_IN_212F 0x01
114 #define PORT100_COMM_TYPE_IN_424F 0x02
115 #define PORT100_COMM_TYPE_IN_106A 0x03
116 #define PORT100_COMM_TYPE_IN_106B 0x07
118 static const struct port100_in_rf_setting in_rf_settings
[] = {
119 [NFC_DIGITAL_RF_TECH_212F
] = {
120 .in_send_set_number
= 1,
121 .in_send_comm_type
= PORT100_COMM_TYPE_IN_212F
,
122 .in_recv_set_number
= 15,
123 .in_recv_comm_type
= PORT100_COMM_TYPE_IN_212F
,
125 [NFC_DIGITAL_RF_TECH_424F
] = {
126 .in_send_set_number
= 1,
127 .in_send_comm_type
= PORT100_COMM_TYPE_IN_424F
,
128 .in_recv_set_number
= 15,
129 .in_recv_comm_type
= PORT100_COMM_TYPE_IN_424F
,
131 [NFC_DIGITAL_RF_TECH_106A
] = {
132 .in_send_set_number
= 2,
133 .in_send_comm_type
= PORT100_COMM_TYPE_IN_106A
,
134 .in_recv_set_number
= 15,
135 .in_recv_comm_type
= PORT100_COMM_TYPE_IN_106A
,
137 [NFC_DIGITAL_RF_TECH_106B
] = {
138 .in_send_set_number
= 3,
139 .in_send_comm_type
= PORT100_COMM_TYPE_IN_106B
,
140 .in_recv_set_number
= 15,
141 .in_recv_comm_type
= PORT100_COMM_TYPE_IN_106B
,
143 /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
144 [NFC_DIGITAL_RF_TECH_LAST
] = { 0 },
148 * Setting sets structure for tg_set_rf command
150 * @tg_set_number: Represents the entry index in the port-100 RF Base Table.
151 * This table contains multiple RF setting sets required for RF
152 * communication. this field is used for both send and receive
155 * @tg_comm_type: Sets the communication type to be used to send and receive
158 struct port100_tg_rf_setting
{
163 #define PORT100_COMM_TYPE_TG_106A 0x0B
164 #define PORT100_COMM_TYPE_TG_212F 0x0C
165 #define PORT100_COMM_TYPE_TG_424F 0x0D
167 static const struct port100_tg_rf_setting tg_rf_settings
[] = {
168 [NFC_DIGITAL_RF_TECH_106A
] = {
170 .tg_comm_type
= PORT100_COMM_TYPE_TG_106A
,
172 [NFC_DIGITAL_RF_TECH_212F
] = {
174 .tg_comm_type
= PORT100_COMM_TYPE_TG_212F
,
176 [NFC_DIGITAL_RF_TECH_424F
] = {
178 .tg_comm_type
= PORT100_COMM_TYPE_TG_424F
,
180 /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
181 [NFC_DIGITAL_RF_TECH_LAST
] = { 0 },
185 #define PORT100_IN_PROT_INITIAL_GUARD_TIME 0x00
186 #define PORT100_IN_PROT_ADD_CRC 0x01
187 #define PORT100_IN_PROT_CHECK_CRC 0x02
188 #define PORT100_IN_PROT_MULTI_CARD 0x03
189 #define PORT100_IN_PROT_ADD_PARITY 0x04
190 #define PORT100_IN_PROT_CHECK_PARITY 0x05
191 #define PORT100_IN_PROT_BITWISE_AC_RECV_MODE 0x06
192 #define PORT100_IN_PROT_VALID_BIT_NUMBER 0x07
193 #define PORT100_IN_PROT_CRYPTO1 0x08
194 #define PORT100_IN_PROT_ADD_SOF 0x09
195 #define PORT100_IN_PROT_CHECK_SOF 0x0A
196 #define PORT100_IN_PROT_ADD_EOF 0x0B
197 #define PORT100_IN_PROT_CHECK_EOF 0x0C
198 #define PORT100_IN_PROT_DEAF_TIME 0x0E
199 #define PORT100_IN_PROT_CRM 0x0F
200 #define PORT100_IN_PROT_CRM_MIN_LEN 0x10
201 #define PORT100_IN_PROT_T1_TAG_FRAME 0x11
202 #define PORT100_IN_PROT_RFCA 0x12
203 #define PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR 0x13
204 #define PORT100_IN_PROT_END 0x14
206 #define PORT100_IN_MAX_NUM_PROTOCOLS 19
208 #define PORT100_TG_PROT_TU 0x00
209 #define PORT100_TG_PROT_RF_OFF 0x01
210 #define PORT100_TG_PROT_CRM 0x02
211 #define PORT100_TG_PROT_END 0x03
213 #define PORT100_TG_MAX_NUM_PROTOCOLS 3
215 struct port100_protocol
{
220 static struct port100_protocol
221 in_protocols
[][PORT100_IN_MAX_NUM_PROTOCOLS
+ 1] = {
222 [NFC_DIGITAL_FRAMING_NFCA_SHORT
] = {
223 { PORT100_IN_PROT_INITIAL_GUARD_TIME
, 6 },
224 { PORT100_IN_PROT_ADD_CRC
, 0 },
225 { PORT100_IN_PROT_CHECK_CRC
, 0 },
226 { PORT100_IN_PROT_MULTI_CARD
, 0 },
227 { PORT100_IN_PROT_ADD_PARITY
, 0 },
228 { PORT100_IN_PROT_CHECK_PARITY
, 1 },
229 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE
, 0 },
230 { PORT100_IN_PROT_VALID_BIT_NUMBER
, 7 },
231 { PORT100_IN_PROT_CRYPTO1
, 0 },
232 { PORT100_IN_PROT_ADD_SOF
, 0 },
233 { PORT100_IN_PROT_CHECK_SOF
, 0 },
234 { PORT100_IN_PROT_ADD_EOF
, 0 },
235 { PORT100_IN_PROT_CHECK_EOF
, 0 },
236 { PORT100_IN_PROT_DEAF_TIME
, 4 },
237 { PORT100_IN_PROT_CRM
, 0 },
238 { PORT100_IN_PROT_CRM_MIN_LEN
, 0 },
239 { PORT100_IN_PROT_T1_TAG_FRAME
, 0 },
240 { PORT100_IN_PROT_RFCA
, 0 },
241 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR
, 6 },
242 { PORT100_IN_PROT_END
, 0 },
244 [NFC_DIGITAL_FRAMING_NFCA_STANDARD
] = {
245 { PORT100_IN_PROT_INITIAL_GUARD_TIME
, 6 },
246 { PORT100_IN_PROT_ADD_CRC
, 0 },
247 { PORT100_IN_PROT_CHECK_CRC
, 0 },
248 { PORT100_IN_PROT_MULTI_CARD
, 0 },
249 { PORT100_IN_PROT_ADD_PARITY
, 1 },
250 { PORT100_IN_PROT_CHECK_PARITY
, 1 },
251 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE
, 0 },
252 { PORT100_IN_PROT_VALID_BIT_NUMBER
, 8 },
253 { PORT100_IN_PROT_CRYPTO1
, 0 },
254 { PORT100_IN_PROT_ADD_SOF
, 0 },
255 { PORT100_IN_PROT_CHECK_SOF
, 0 },
256 { PORT100_IN_PROT_ADD_EOF
, 0 },
257 { PORT100_IN_PROT_CHECK_EOF
, 0 },
258 { PORT100_IN_PROT_DEAF_TIME
, 4 },
259 { PORT100_IN_PROT_CRM
, 0 },
260 { PORT100_IN_PROT_CRM_MIN_LEN
, 0 },
261 { PORT100_IN_PROT_T1_TAG_FRAME
, 0 },
262 { PORT100_IN_PROT_RFCA
, 0 },
263 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR
, 6 },
264 { PORT100_IN_PROT_END
, 0 },
266 [NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A
] = {
267 { PORT100_IN_PROT_INITIAL_GUARD_TIME
, 6 },
268 { PORT100_IN_PROT_ADD_CRC
, 1 },
269 { PORT100_IN_PROT_CHECK_CRC
, 1 },
270 { PORT100_IN_PROT_MULTI_CARD
, 0 },
271 { PORT100_IN_PROT_ADD_PARITY
, 1 },
272 { PORT100_IN_PROT_CHECK_PARITY
, 1 },
273 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE
, 0 },
274 { PORT100_IN_PROT_VALID_BIT_NUMBER
, 8 },
275 { PORT100_IN_PROT_CRYPTO1
, 0 },
276 { PORT100_IN_PROT_ADD_SOF
, 0 },
277 { PORT100_IN_PROT_CHECK_SOF
, 0 },
278 { PORT100_IN_PROT_ADD_EOF
, 0 },
279 { PORT100_IN_PROT_CHECK_EOF
, 0 },
280 { PORT100_IN_PROT_DEAF_TIME
, 4 },
281 { PORT100_IN_PROT_CRM
, 0 },
282 { PORT100_IN_PROT_CRM_MIN_LEN
, 0 },
283 { PORT100_IN_PROT_T1_TAG_FRAME
, 0 },
284 { PORT100_IN_PROT_RFCA
, 0 },
285 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR
, 6 },
286 { PORT100_IN_PROT_END
, 0 },
288 [NFC_DIGITAL_FRAMING_NFCA_T1T
] = {
289 /* nfc_digital_framing_nfca_short */
290 { PORT100_IN_PROT_ADD_CRC
, 2 },
291 { PORT100_IN_PROT_CHECK_CRC
, 2 },
292 { PORT100_IN_PROT_VALID_BIT_NUMBER
, 8 },
293 { PORT100_IN_PROT_T1_TAG_FRAME
, 2 },
294 { PORT100_IN_PROT_END
, 0 },
296 [NFC_DIGITAL_FRAMING_NFCA_T2T
] = {
297 /* nfc_digital_framing_nfca_standard */
298 { PORT100_IN_PROT_ADD_CRC
, 1 },
299 { PORT100_IN_PROT_CHECK_CRC
, 0 },
300 { PORT100_IN_PROT_END
, 0 },
302 [NFC_DIGITAL_FRAMING_NFCA_T4T
] = {
303 /* nfc_digital_framing_nfca_standard_with_crc_a */
304 { PORT100_IN_PROT_END
, 0 },
306 [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP
] = {
307 /* nfc_digital_framing_nfca_standard */
308 { PORT100_IN_PROT_END
, 0 },
310 [NFC_DIGITAL_FRAMING_NFCF
] = {
311 { PORT100_IN_PROT_INITIAL_GUARD_TIME
, 18 },
312 { PORT100_IN_PROT_ADD_CRC
, 1 },
313 { PORT100_IN_PROT_CHECK_CRC
, 1 },
314 { PORT100_IN_PROT_MULTI_CARD
, 0 },
315 { PORT100_IN_PROT_ADD_PARITY
, 0 },
316 { PORT100_IN_PROT_CHECK_PARITY
, 0 },
317 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE
, 0 },
318 { PORT100_IN_PROT_VALID_BIT_NUMBER
, 8 },
319 { PORT100_IN_PROT_CRYPTO1
, 0 },
320 { PORT100_IN_PROT_ADD_SOF
, 0 },
321 { PORT100_IN_PROT_CHECK_SOF
, 0 },
322 { PORT100_IN_PROT_ADD_EOF
, 0 },
323 { PORT100_IN_PROT_CHECK_EOF
, 0 },
324 { PORT100_IN_PROT_DEAF_TIME
, 4 },
325 { PORT100_IN_PROT_CRM
, 0 },
326 { PORT100_IN_PROT_CRM_MIN_LEN
, 0 },
327 { PORT100_IN_PROT_T1_TAG_FRAME
, 0 },
328 { PORT100_IN_PROT_RFCA
, 0 },
329 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR
, 6 },
330 { PORT100_IN_PROT_END
, 0 },
332 [NFC_DIGITAL_FRAMING_NFCF_T3T
] = {
333 /* nfc_digital_framing_nfcf */
334 { PORT100_IN_PROT_END
, 0 },
336 [NFC_DIGITAL_FRAMING_NFCF_NFC_DEP
] = {
337 /* nfc_digital_framing_nfcf */
338 { PORT100_IN_PROT_INITIAL_GUARD_TIME
, 18 },
339 { PORT100_IN_PROT_ADD_CRC
, 1 },
340 { PORT100_IN_PROT_CHECK_CRC
, 1 },
341 { PORT100_IN_PROT_MULTI_CARD
, 0 },
342 { PORT100_IN_PROT_ADD_PARITY
, 0 },
343 { PORT100_IN_PROT_CHECK_PARITY
, 0 },
344 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE
, 0 },
345 { PORT100_IN_PROT_VALID_BIT_NUMBER
, 8 },
346 { PORT100_IN_PROT_CRYPTO1
, 0 },
347 { PORT100_IN_PROT_ADD_SOF
, 0 },
348 { PORT100_IN_PROT_CHECK_SOF
, 0 },
349 { PORT100_IN_PROT_ADD_EOF
, 0 },
350 { PORT100_IN_PROT_CHECK_EOF
, 0 },
351 { PORT100_IN_PROT_DEAF_TIME
, 4 },
352 { PORT100_IN_PROT_CRM
, 0 },
353 { PORT100_IN_PROT_CRM_MIN_LEN
, 0 },
354 { PORT100_IN_PROT_T1_TAG_FRAME
, 0 },
355 { PORT100_IN_PROT_RFCA
, 0 },
356 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR
, 6 },
357 { PORT100_IN_PROT_END
, 0 },
359 [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED
] = {
360 { PORT100_IN_PROT_END
, 0 },
362 [NFC_DIGITAL_FRAMING_NFCB
] = {
363 { PORT100_IN_PROT_INITIAL_GUARD_TIME
, 20 },
364 { PORT100_IN_PROT_ADD_CRC
, 1 },
365 { PORT100_IN_PROT_CHECK_CRC
, 1 },
366 { PORT100_IN_PROT_MULTI_CARD
, 0 },
367 { PORT100_IN_PROT_ADD_PARITY
, 0 },
368 { PORT100_IN_PROT_CHECK_PARITY
, 0 },
369 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE
, 0 },
370 { PORT100_IN_PROT_VALID_BIT_NUMBER
, 8 },
371 { PORT100_IN_PROT_CRYPTO1
, 0 },
372 { PORT100_IN_PROT_ADD_SOF
, 1 },
373 { PORT100_IN_PROT_CHECK_SOF
, 1 },
374 { PORT100_IN_PROT_ADD_EOF
, 1 },
375 { PORT100_IN_PROT_CHECK_EOF
, 1 },
376 { PORT100_IN_PROT_DEAF_TIME
, 4 },
377 { PORT100_IN_PROT_CRM
, 0 },
378 { PORT100_IN_PROT_CRM_MIN_LEN
, 0 },
379 { PORT100_IN_PROT_T1_TAG_FRAME
, 0 },
380 { PORT100_IN_PROT_RFCA
, 0 },
381 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR
, 6 },
382 { PORT100_IN_PROT_END
, 0 },
384 [NFC_DIGITAL_FRAMING_NFCB_T4T
] = {
385 /* nfc_digital_framing_nfcb */
386 { PORT100_IN_PROT_END
, 0 },
388 /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
389 [NFC_DIGITAL_FRAMING_LAST
] = {
390 { PORT100_IN_PROT_END
, 0 },
394 static struct port100_protocol
395 tg_protocols
[][PORT100_TG_MAX_NUM_PROTOCOLS
+ 1] = {
396 [NFC_DIGITAL_FRAMING_NFCA_SHORT
] = {
397 { PORT100_TG_PROT_END
, 0 },
399 [NFC_DIGITAL_FRAMING_NFCA_STANDARD
] = {
400 { PORT100_TG_PROT_END
, 0 },
402 [NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A
] = {
403 { PORT100_TG_PROT_END
, 0 },
405 [NFC_DIGITAL_FRAMING_NFCA_T1T
] = {
406 { PORT100_TG_PROT_END
, 0 },
408 [NFC_DIGITAL_FRAMING_NFCA_T2T
] = {
409 { PORT100_TG_PROT_END
, 0 },
411 [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP
] = {
412 { PORT100_TG_PROT_TU
, 1 },
413 { PORT100_TG_PROT_RF_OFF
, 0 },
414 { PORT100_TG_PROT_CRM
, 7 },
415 { PORT100_TG_PROT_END
, 0 },
417 [NFC_DIGITAL_FRAMING_NFCF
] = {
418 { PORT100_TG_PROT_END
, 0 },
420 [NFC_DIGITAL_FRAMING_NFCF_T3T
] = {
421 { PORT100_TG_PROT_END
, 0 },
423 [NFC_DIGITAL_FRAMING_NFCF_NFC_DEP
] = {
424 { PORT100_TG_PROT_TU
, 1 },
425 { PORT100_TG_PROT_RF_OFF
, 0 },
426 { PORT100_TG_PROT_CRM
, 7 },
427 { PORT100_TG_PROT_END
, 0 },
429 [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED
] = {
430 { PORT100_TG_PROT_RF_OFF
, 1 },
431 { PORT100_TG_PROT_END
, 0 },
433 /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
434 [NFC_DIGITAL_FRAMING_LAST
] = {
435 { PORT100_TG_PROT_END
, 0 },
440 struct nfc_digital_dev
*nfc_digital_dev
;
445 struct usb_device
*udev
;
446 struct usb_interface
*interface
;
451 /* This mutex protects the out_urb and avoids to submit a new command
452 * through port100_send_frame_async() while the previous one is being
453 * canceled through port100_abort_cmd().
455 struct mutex out_urb_lock
;
457 struct work_struct cmd_complete_work
;
461 /* The digital stack serializes commands to be sent. There is no need
462 * for any queuing/locking mechanism at driver level.
464 struct port100_cmd
*cmd
;
467 struct completion cmd_cancel_done
;
474 struct sk_buff
*resp
;
476 port100_send_async_complete_t complete_cb
;
477 void *complete_cb_context
;
480 struct port100_frame
{
483 __be16 extended_frame
;
489 struct port100_ack_frame
{
496 struct port100_cb_arg
{
497 nfc_digital_cmd_complete_t complete_cb
;
502 struct port100_tg_comm_rf_cmd
{
514 struct port100_tg_comm_rf_res
{
522 /* The rule: value + checksum = 0 */
523 static inline u8
port100_checksum(u16 value
)
525 return ~(((u8
*)&value
)[0] + ((u8
*)&value
)[1]) + 1;
528 /* The rule: sum(data elements) + checksum = 0 */
529 static u8
port100_data_checksum(u8
*data
, int datalen
)
534 for (i
= 0; i
< datalen
; i
++)
537 return port100_checksum(sum
);
540 static void port100_tx_frame_init(void *_frame
, u8 cmd_code
)
542 struct port100_frame
*frame
= _frame
;
545 frame
->start_frame
= cpu_to_be16(PORT100_FRAME_SOF
);
546 frame
->extended_frame
= cpu_to_be16(PORT100_FRAME_EXT
);
547 PORT100_FRAME_DIRECTION(frame
) = PORT100_FRAME_DIR_OUT
;
548 PORT100_FRAME_CMD(frame
) = cmd_code
;
549 frame
->datalen
= cpu_to_le16(2);
552 static void port100_tx_frame_finish(void *_frame
)
554 struct port100_frame
*frame
= _frame
;
556 frame
->datalen_checksum
= port100_checksum(le16_to_cpu(frame
->datalen
));
558 PORT100_FRAME_CHECKSUM(frame
) =
559 port100_data_checksum(frame
->data
, le16_to_cpu(frame
->datalen
));
561 PORT100_FRAME_POSTAMBLE(frame
) = 0;
564 static void port100_tx_update_payload_len(void *_frame
, int len
)
566 struct port100_frame
*frame
= _frame
;
568 le16_add_cpu(&frame
->datalen
, len
);
571 static bool port100_rx_frame_is_valid(void *_frame
)
574 struct port100_frame
*frame
= _frame
;
576 if (frame
->start_frame
!= cpu_to_be16(PORT100_FRAME_SOF
) ||
577 frame
->extended_frame
!= cpu_to_be16(PORT100_FRAME_EXT
))
580 checksum
= port100_checksum(le16_to_cpu(frame
->datalen
));
581 if (checksum
!= frame
->datalen_checksum
)
584 checksum
= port100_data_checksum(frame
->data
,
585 le16_to_cpu(frame
->datalen
));
586 if (checksum
!= PORT100_FRAME_CHECKSUM(frame
))
592 static bool port100_rx_frame_is_ack(struct port100_ack_frame
*frame
)
594 return (frame
->start_frame
== cpu_to_be16(PORT100_FRAME_SOF
) &&
595 frame
->ack_frame
== cpu_to_be16(PORT100_FRAME_ACK
));
598 static inline int port100_rx_frame_size(void *frame
)
600 struct port100_frame
*f
= frame
;
602 return sizeof(struct port100_frame
) + le16_to_cpu(f
->datalen
) +
603 PORT100_FRAME_TAIL_LEN
;
606 static bool port100_rx_frame_is_cmd_response(struct port100
*dev
, void *frame
)
608 struct port100_frame
*f
= frame
;
610 return (PORT100_FRAME_CMD(f
) == PORT100_CMD_RESPONSE(dev
->cmd
->code
));
613 static void port100_recv_response(struct urb
*urb
)
615 struct port100
*dev
= urb
->context
;
616 struct port100_cmd
*cmd
= dev
->cmd
;
619 cmd
->status
= urb
->status
;
621 switch (urb
->status
) {
626 nfc_err(&dev
->interface
->dev
,
627 "The urb has been canceled (status %d)\n", urb
->status
);
631 nfc_err(&dev
->interface
->dev
, "Urb failure (status %d)\n",
636 in_frame
= dev
->in_urb
->transfer_buffer
;
638 if (!port100_rx_frame_is_valid(in_frame
)) {
639 nfc_err(&dev
->interface
->dev
, "Received an invalid frame\n");
644 print_hex_dump_debug("PORT100 RX: ", DUMP_PREFIX_NONE
, 16, 1, in_frame
,
645 port100_rx_frame_size(in_frame
), false);
647 if (!port100_rx_frame_is_cmd_response(dev
, in_frame
)) {
648 nfc_err(&dev
->interface
->dev
,
649 "It's not the response to the last command\n");
655 schedule_work(&dev
->cmd_complete_work
);
658 static int port100_submit_urb_for_response(struct port100
*dev
, gfp_t flags
)
660 dev
->in_urb
->complete
= port100_recv_response
;
662 return usb_submit_urb(dev
->in_urb
, flags
);
665 static void port100_recv_ack(struct urb
*urb
)
667 struct port100
*dev
= urb
->context
;
668 struct port100_cmd
*cmd
= dev
->cmd
;
669 struct port100_ack_frame
*in_frame
;
672 cmd
->status
= urb
->status
;
674 switch (urb
->status
) {
679 nfc_err(&dev
->interface
->dev
,
680 "The urb has been stopped (status %d)\n", urb
->status
);
684 nfc_err(&dev
->interface
->dev
, "Urb failure (status %d)\n",
689 in_frame
= dev
->in_urb
->transfer_buffer
;
691 if (!port100_rx_frame_is_ack(in_frame
)) {
692 nfc_err(&dev
->interface
->dev
, "Received an invalid ack\n");
697 rc
= port100_submit_urb_for_response(dev
, GFP_ATOMIC
);
699 nfc_err(&dev
->interface
->dev
,
700 "usb_submit_urb failed with result %d\n", rc
);
708 schedule_work(&dev
->cmd_complete_work
);
711 static int port100_submit_urb_for_ack(struct port100
*dev
, gfp_t flags
)
713 dev
->in_urb
->complete
= port100_recv_ack
;
715 return usb_submit_urb(dev
->in_urb
, flags
);
718 static int port100_send_ack(struct port100
*dev
)
722 mutex_lock(&dev
->out_urb_lock
);
725 * If prior cancel is in-flight (dev->cmd_cancel == true), we
726 * can skip to send cancel. Then this will wait the prior
727 * cancel, or merged into the next cancel rarely if next
728 * cancel was started before waiting done. In any case, this
729 * will be waked up soon or later.
731 if (!dev
->cmd_cancel
) {
732 reinit_completion(&dev
->cmd_cancel_done
);
734 usb_kill_urb(dev
->out_urb
);
736 dev
->out_urb
->transfer_buffer
= ack_frame
;
737 dev
->out_urb
->transfer_buffer_length
= sizeof(ack_frame
);
738 rc
= usb_submit_urb(dev
->out_urb
, GFP_KERNEL
);
741 * Set the cmd_cancel flag only if the URB has been
742 * successfully submitted. It will be reset by the out
743 * URB completion callback port100_send_complete().
745 dev
->cmd_cancel
= !rc
;
748 mutex_unlock(&dev
->out_urb_lock
);
751 wait_for_completion(&dev
->cmd_cancel_done
);
756 static int port100_send_frame_async(struct port100
*dev
, struct sk_buff
*out
,
757 struct sk_buff
*in
, int in_len
)
761 mutex_lock(&dev
->out_urb_lock
);
763 /* A command cancel frame as been sent through dev->out_urb. Don't try
764 * to submit a new one.
766 if (dev
->cmd_cancel
) {
771 dev
->out_urb
->transfer_buffer
= out
->data
;
772 dev
->out_urb
->transfer_buffer_length
= out
->len
;
774 dev
->in_urb
->transfer_buffer
= in
->data
;
775 dev
->in_urb
->transfer_buffer_length
= in_len
;
777 print_hex_dump_debug("PORT100 TX: ", DUMP_PREFIX_NONE
, 16, 1,
778 out
->data
, out
->len
, false);
780 rc
= usb_submit_urb(dev
->out_urb
, GFP_KERNEL
);
784 rc
= port100_submit_urb_for_ack(dev
, GFP_KERNEL
);
786 usb_kill_urb(dev
->out_urb
);
789 mutex_unlock(&dev
->out_urb_lock
);
794 static void port100_build_cmd_frame(struct port100
*dev
, u8 cmd_code
,
797 /* payload is already there, just update datalen */
798 int payload_len
= skb
->len
;
800 skb_push(skb
, PORT100_FRAME_HEADER_LEN
);
801 skb_put(skb
, PORT100_FRAME_TAIL_LEN
);
803 port100_tx_frame_init(skb
->data
, cmd_code
);
804 port100_tx_update_payload_len(skb
->data
, payload_len
);
805 port100_tx_frame_finish(skb
->data
);
808 static void port100_send_async_complete(struct port100
*dev
)
810 struct port100_cmd
*cmd
= dev
->cmd
;
811 int status
= cmd
->status
;
813 struct sk_buff
*req
= cmd
->req
;
814 struct sk_buff
*resp
= cmd
->resp
;
821 cmd
->complete_cb(dev
, cmd
->complete_cb_context
,
827 skb_put(resp
, port100_rx_frame_size(resp
->data
));
828 skb_pull(resp
, PORT100_FRAME_HEADER_LEN
);
829 skb_trim(resp
, resp
->len
- PORT100_FRAME_TAIL_LEN
);
831 cmd
->complete_cb(dev
, cmd
->complete_cb_context
, resp
);
837 static int port100_send_cmd_async(struct port100
*dev
, u8 cmd_code
,
839 port100_send_async_complete_t complete_cb
,
840 void *complete_cb_context
)
842 struct port100_cmd
*cmd
;
843 struct sk_buff
*resp
;
845 int resp_len
= PORT100_FRAME_HEADER_LEN
+
846 PORT100_FRAME_MAX_PAYLOAD_LEN
+
847 PORT100_FRAME_TAIL_LEN
;
850 nfc_err(&dev
->interface
->dev
,
851 "A command is still in process\n");
855 resp
= alloc_skb(resp_len
, GFP_KERNEL
);
859 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
865 cmd
->code
= cmd_code
;
868 cmd
->resp_len
= resp_len
;
869 cmd
->complete_cb
= complete_cb
;
870 cmd
->complete_cb_context
= complete_cb_context
;
872 port100_build_cmd_frame(dev
, cmd_code
, req
);
876 rc
= port100_send_frame_async(dev
, req
, resp
, resp_len
);
886 struct port100_sync_cmd_response
{
887 struct sk_buff
*resp
;
888 struct completion done
;
891 static void port100_wq_cmd_complete(struct work_struct
*work
)
893 struct port100
*dev
= container_of(work
, struct port100
,
896 port100_send_async_complete(dev
);
899 static void port100_send_sync_complete(struct port100
*dev
, void *_arg
,
900 struct sk_buff
*resp
)
902 struct port100_sync_cmd_response
*arg
= _arg
;
905 complete(&arg
->done
);
908 static struct sk_buff
*port100_send_cmd_sync(struct port100
*dev
, u8 cmd_code
,
912 struct port100_sync_cmd_response arg
;
914 init_completion(&arg
.done
);
916 rc
= port100_send_cmd_async(dev
, cmd_code
, req
,
917 port100_send_sync_complete
, &arg
);
923 wait_for_completion(&arg
.done
);
928 static void port100_send_complete(struct urb
*urb
)
930 struct port100
*dev
= urb
->context
;
932 if (dev
->cmd_cancel
) {
933 complete_all(&dev
->cmd_cancel_done
);
934 dev
->cmd_cancel
= false;
937 switch (urb
->status
) {
942 nfc_err(&dev
->interface
->dev
,
943 "The urb has been stopped (status %d)\n", urb
->status
);
947 nfc_err(&dev
->interface
->dev
, "Urb failure (status %d)\n",
952 static void port100_abort_cmd(struct nfc_digital_dev
*ddev
)
954 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
956 /* An ack will cancel the last issued command */
957 port100_send_ack(dev
);
959 /* cancel the urb request */
960 usb_kill_urb(dev
->in_urb
);
963 static struct sk_buff
*port100_alloc_skb(struct port100
*dev
, unsigned int size
)
967 skb
= alloc_skb(dev
->skb_headroom
+ dev
->skb_tailroom
+ size
,
970 skb_reserve(skb
, dev
->skb_headroom
);
975 static int port100_set_command_type(struct port100
*dev
, u8 command_type
)
978 struct sk_buff
*resp
;
981 skb
= port100_alloc_skb(dev
, 1);
985 skb_put_u8(skb
, command_type
);
987 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_SET_COMMAND_TYPE
, skb
);
989 return PTR_ERR(resp
);
998 static u64
port100_get_command_type_mask(struct port100
*dev
)
1000 struct sk_buff
*skb
;
1001 struct sk_buff
*resp
;
1004 skb
= port100_alloc_skb(dev
, 0);
1008 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_GET_COMMAND_TYPE
, skb
);
1010 return PTR_ERR(resp
);
1015 mask
= be64_to_cpu(*(__be64
*)resp
->data
);
1017 dev_kfree_skb(resp
);
1022 static u16
port100_get_firmware_version(struct port100
*dev
)
1024 struct sk_buff
*skb
;
1025 struct sk_buff
*resp
;
1028 skb
= port100_alloc_skb(dev
, 0);
1032 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_GET_FIRMWARE_VERSION
,
1037 fw_ver
= le16_to_cpu(*(__le16
*)resp
->data
);
1039 dev_kfree_skb(resp
);
1044 static int port100_switch_rf(struct nfc_digital_dev
*ddev
, bool on
)
1046 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1047 struct sk_buff
*skb
, *resp
;
1049 skb
= port100_alloc_skb(dev
, 1);
1053 skb_put_u8(skb
, on
? 1 : 0);
1055 /* Cancel the last command if the device is being switched off */
1057 port100_abort_cmd(ddev
);
1059 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_SWITCH_RF
, skb
);
1062 return PTR_ERR(resp
);
1064 dev_kfree_skb(resp
);
1069 static int port100_in_set_rf(struct nfc_digital_dev
*ddev
, u8 rf
)
1071 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1072 struct sk_buff
*skb
;
1073 struct sk_buff
*resp
;
1076 if (rf
>= NFC_DIGITAL_RF_TECH_LAST
)
1079 skb
= port100_alloc_skb(dev
, sizeof(struct port100_in_rf_setting
));
1083 skb_put_data(skb
, &in_rf_settings
[rf
],
1084 sizeof(struct port100_in_rf_setting
));
1086 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_IN_SET_RF
, skb
);
1089 return PTR_ERR(resp
);
1093 dev_kfree_skb(resp
);
1098 static int port100_in_set_framing(struct nfc_digital_dev
*ddev
, int param
)
1100 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1101 struct port100_protocol
*protocols
;
1102 struct sk_buff
*skb
;
1103 struct sk_buff
*resp
;
1108 if (param
>= NFC_DIGITAL_FRAMING_LAST
)
1111 protocols
= in_protocols
[param
];
1114 while (protocols
[num_protocols
].number
!= PORT100_IN_PROT_END
)
1120 size
= sizeof(struct port100_protocol
) * num_protocols
;
1122 skb
= port100_alloc_skb(dev
, size
);
1126 skb_put_data(skb
, protocols
, size
);
1128 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_IN_SET_PROTOCOL
, skb
);
1131 return PTR_ERR(resp
);
1135 dev_kfree_skb(resp
);
1140 static int port100_in_configure_hw(struct nfc_digital_dev
*ddev
, int type
,
1143 if (type
== NFC_DIGITAL_CONFIG_RF_TECH
)
1144 return port100_in_set_rf(ddev
, param
);
1146 if (type
== NFC_DIGITAL_CONFIG_FRAMING
)
1147 return port100_in_set_framing(ddev
, param
);
1152 static void port100_in_comm_rf_complete(struct port100
*dev
, void *arg
,
1153 struct sk_buff
*resp
)
1155 struct port100_cb_arg
*cb_arg
= arg
;
1156 nfc_digital_cmd_complete_t cb
= cb_arg
->complete_cb
;
1165 if (resp
->len
< 4) {
1166 nfc_err(&dev
->interface
->dev
,
1167 "Invalid packet length received\n");
1172 status
= le32_to_cpu(*(__le32
*)resp
->data
);
1174 skb_pull(resp
, sizeof(u32
));
1176 if (status
== PORT100_CMD_STATUS_TIMEOUT
) {
1181 if (status
!= PORT100_CMD_STATUS_OK
) {
1182 nfc_err(&dev
->interface
->dev
,
1183 "in_comm_rf failed with status 0x%08x\n", status
);
1188 /* Remove collision bits byte */
1198 cb(dev
->nfc_digital_dev
, cb_arg
->complete_arg
, resp
);
1203 static int port100_in_send_cmd(struct nfc_digital_dev
*ddev
,
1204 struct sk_buff
*skb
, u16 _timeout
,
1205 nfc_digital_cmd_complete_t cb
, void *arg
)
1207 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1208 struct port100_cb_arg
*cb_arg
;
1211 cb_arg
= kzalloc(sizeof(struct port100_cb_arg
), GFP_KERNEL
);
1215 cb_arg
->complete_cb
= cb
;
1216 cb_arg
->complete_arg
= arg
;
1218 timeout
= cpu_to_le16(_timeout
* 10);
1220 memcpy(skb_push(skb
, sizeof(__le16
)), &timeout
, sizeof(__le16
));
1222 return port100_send_cmd_async(dev
, PORT100_CMD_IN_COMM_RF
, skb
,
1223 port100_in_comm_rf_complete
, cb_arg
);
1226 static int port100_tg_set_rf(struct nfc_digital_dev
*ddev
, u8 rf
)
1228 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1229 struct sk_buff
*skb
;
1230 struct sk_buff
*resp
;
1233 if (rf
>= NFC_DIGITAL_RF_TECH_LAST
)
1236 skb
= port100_alloc_skb(dev
, sizeof(struct port100_tg_rf_setting
));
1240 skb_put_data(skb
, &tg_rf_settings
[rf
],
1241 sizeof(struct port100_tg_rf_setting
));
1243 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_TG_SET_RF
, skb
);
1246 return PTR_ERR(resp
);
1250 dev_kfree_skb(resp
);
1255 static int port100_tg_set_framing(struct nfc_digital_dev
*ddev
, int param
)
1257 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1258 struct port100_protocol
*protocols
;
1259 struct sk_buff
*skb
;
1260 struct sk_buff
*resp
;
1265 if (param
>= NFC_DIGITAL_FRAMING_LAST
)
1268 protocols
= tg_protocols
[param
];
1271 while (protocols
[num_protocols
].number
!= PORT100_TG_PROT_END
)
1277 size
= sizeof(struct port100_protocol
) * num_protocols
;
1279 skb
= port100_alloc_skb(dev
, size
);
1283 skb_put_data(skb
, protocols
, size
);
1285 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_TG_SET_PROTOCOL
, skb
);
1288 return PTR_ERR(resp
);
1292 dev_kfree_skb(resp
);
1297 static int port100_tg_configure_hw(struct nfc_digital_dev
*ddev
, int type
,
1300 if (type
== NFC_DIGITAL_CONFIG_RF_TECH
)
1301 return port100_tg_set_rf(ddev
, param
);
1303 if (type
== NFC_DIGITAL_CONFIG_FRAMING
)
1304 return port100_tg_set_framing(ddev
, param
);
1309 static bool port100_tg_target_activated(struct port100
*dev
, u8 tgt_activated
)
1313 switch (dev
->cmd_type
) {
1314 case PORT100_CMD_TYPE_0
:
1315 mask
= PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK
;
1317 case PORT100_CMD_TYPE_1
:
1318 mask
= PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK
|
1319 PORT100_MDAA_TGT_WAS_ACTIVATED_MASK
;
1322 nfc_err(&dev
->interface
->dev
, "Unknown command type\n");
1326 return ((tgt_activated
& mask
) == mask
);
1329 static void port100_tg_comm_rf_complete(struct port100
*dev
, void *arg
,
1330 struct sk_buff
*resp
)
1333 struct port100_cb_arg
*cb_arg
= arg
;
1334 nfc_digital_cmd_complete_t cb
= cb_arg
->complete_cb
;
1335 struct port100_tg_comm_rf_res
*hdr
;
1340 hdr
= (struct port100_tg_comm_rf_res
*)resp
->data
;
1342 status
= le32_to_cpu(hdr
->status
);
1345 !port100_tg_target_activated(dev
, hdr
->target_activated
)) {
1347 resp
= ERR_PTR(-ETIMEDOUT
);
1352 skb_pull(resp
, sizeof(struct port100_tg_comm_rf_res
));
1354 if (status
!= PORT100_CMD_STATUS_OK
) {
1357 if (status
== PORT100_CMD_STATUS_TIMEOUT
)
1358 resp
= ERR_PTR(-ETIMEDOUT
);
1360 resp
= ERR_PTR(-EIO
);
1364 cb(dev
->nfc_digital_dev
, cb_arg
->complete_arg
, resp
);
1369 static int port100_tg_send_cmd(struct nfc_digital_dev
*ddev
,
1370 struct sk_buff
*skb
, u16 timeout
,
1371 nfc_digital_cmd_complete_t cb
, void *arg
)
1373 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1374 struct port100_tg_comm_rf_cmd
*hdr
;
1375 struct port100_cb_arg
*cb_arg
;
1377 cb_arg
= kzalloc(sizeof(struct port100_cb_arg
), GFP_KERNEL
);
1381 cb_arg
->complete_cb
= cb
;
1382 cb_arg
->complete_arg
= arg
;
1384 skb_push(skb
, sizeof(struct port100_tg_comm_rf_cmd
));
1386 hdr
= (struct port100_tg_comm_rf_cmd
*)skb
->data
;
1388 memset(hdr
, 0, sizeof(struct port100_tg_comm_rf_cmd
));
1389 hdr
->guard_time
= cpu_to_le16(500);
1390 hdr
->send_timeout
= cpu_to_le16(0xFFFF);
1391 hdr
->recv_timeout
= cpu_to_le16(timeout
);
1393 return port100_send_cmd_async(dev
, PORT100_CMD_TG_COMM_RF
, skb
,
1394 port100_tg_comm_rf_complete
, cb_arg
);
1397 static int port100_listen_mdaa(struct nfc_digital_dev
*ddev
,
1398 struct digital_tg_mdaa_params
*params
,
1400 nfc_digital_cmd_complete_t cb
, void *arg
)
1402 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1403 struct port100_tg_comm_rf_cmd
*hdr
;
1404 struct port100_cb_arg
*cb_arg
;
1405 struct sk_buff
*skb
;
1408 rc
= port100_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_RF_TECH
,
1409 NFC_DIGITAL_RF_TECH_106A
);
1413 rc
= port100_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
1414 NFC_DIGITAL_FRAMING_NFCA_NFC_DEP
);
1418 cb_arg
= kzalloc(sizeof(struct port100_cb_arg
), GFP_KERNEL
);
1422 cb_arg
->complete_cb
= cb
;
1423 cb_arg
->complete_arg
= arg
;
1426 skb
= port100_alloc_skb(dev
, 0);
1432 skb_push(skb
, sizeof(struct port100_tg_comm_rf_cmd
));
1433 hdr
= (struct port100_tg_comm_rf_cmd
*)skb
->data
;
1435 memset(hdr
, 0, sizeof(struct port100_tg_comm_rf_cmd
));
1437 hdr
->guard_time
= 0;
1438 hdr
->send_timeout
= cpu_to_le16(0xFFFF);
1440 hdr
->nfca_param
[0] = (params
->sens_res
>> 8) & 0xFF;
1441 hdr
->nfca_param
[1] = params
->sens_res
& 0xFF;
1442 memcpy(hdr
->nfca_param
+ 2, params
->nfcid1
, 3);
1443 hdr
->nfca_param
[5] = params
->sel_res
;
1444 memcpy(hdr
->nfcf_param
, params
->nfcid2
, 8);
1445 hdr
->nfcf_param
[16] = (params
->sc
>> 8) & 0xFF;
1446 hdr
->nfcf_param
[17] = params
->sc
& 0xFF;
1447 hdr
->recv_timeout
= cpu_to_le16(timeout
);
1449 return port100_send_cmd_async(dev
, PORT100_CMD_TG_COMM_RF
, skb
,
1450 port100_tg_comm_rf_complete
, cb_arg
);
1453 static int port100_listen(struct nfc_digital_dev
*ddev
, u16 timeout
,
1454 nfc_digital_cmd_complete_t cb
, void *arg
)
1456 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1457 struct sk_buff
*skb
;
1459 skb
= port100_alloc_skb(dev
, 0);
1463 return port100_tg_send_cmd(ddev
, skb
, timeout
, cb
, arg
);
1466 static struct nfc_digital_ops port100_digital_ops
= {
1467 .in_configure_hw
= port100_in_configure_hw
,
1468 .in_send_cmd
= port100_in_send_cmd
,
1470 .tg_listen_mdaa
= port100_listen_mdaa
,
1471 .tg_listen
= port100_listen
,
1472 .tg_configure_hw
= port100_tg_configure_hw
,
1473 .tg_send_cmd
= port100_tg_send_cmd
,
1475 .switch_rf
= port100_switch_rf
,
1476 .abort_cmd
= port100_abort_cmd
,
1479 static const struct usb_device_id port100_table
[] = {
1480 { USB_DEVICE(SONY_VENDOR_ID
, RCS380S_PRODUCT_ID
), },
1481 { USB_DEVICE(SONY_VENDOR_ID
, RCS380P_PRODUCT_ID
), },
1484 MODULE_DEVICE_TABLE(usb
, port100_table
);
1486 static int port100_probe(struct usb_interface
*interface
,
1487 const struct usb_device_id
*id
)
1489 struct port100
*dev
;
1491 struct usb_host_interface
*iface_desc
;
1492 struct usb_endpoint_descriptor
*endpoint
;
1499 dev
= devm_kzalloc(&interface
->dev
, sizeof(struct port100
), GFP_KERNEL
);
1503 mutex_init(&dev
->out_urb_lock
);
1504 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
1505 dev
->interface
= interface
;
1506 usb_set_intfdata(interface
, dev
);
1508 in_endpoint
= out_endpoint
= 0;
1509 iface_desc
= interface
->cur_altsetting
;
1510 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1511 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1513 if (!in_endpoint
&& usb_endpoint_is_bulk_in(endpoint
))
1514 in_endpoint
= endpoint
->bEndpointAddress
;
1516 if (!out_endpoint
&& usb_endpoint_is_bulk_out(endpoint
))
1517 out_endpoint
= endpoint
->bEndpointAddress
;
1520 if (!in_endpoint
|| !out_endpoint
) {
1521 nfc_err(&interface
->dev
,
1522 "Could not find bulk-in or bulk-out endpoint\n");
1527 dev
->in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1528 dev
->out_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1530 if (!dev
->in_urb
|| !dev
->out_urb
) {
1531 nfc_err(&interface
->dev
, "Could not allocate USB URBs\n");
1536 usb_fill_bulk_urb(dev
->in_urb
, dev
->udev
,
1537 usb_rcvbulkpipe(dev
->udev
, in_endpoint
),
1538 NULL
, 0, NULL
, dev
);
1539 usb_fill_bulk_urb(dev
->out_urb
, dev
->udev
,
1540 usb_sndbulkpipe(dev
->udev
, out_endpoint
),
1541 NULL
, 0, port100_send_complete
, dev
);
1542 dev
->out_urb
->transfer_flags
= URB_ZERO_PACKET
;
1544 dev
->skb_headroom
= PORT100_FRAME_HEADER_LEN
+
1545 PORT100_COMM_RF_HEAD_MAX_LEN
;
1546 dev
->skb_tailroom
= PORT100_FRAME_TAIL_LEN
;
1548 init_completion(&dev
->cmd_cancel_done
);
1549 INIT_WORK(&dev
->cmd_complete_work
, port100_wq_cmd_complete
);
1551 /* The first thing to do with the Port-100 is to set the command type
1552 * to be used. If supported we use command type 1. 0 otherwise.
1554 cmd_type_mask
= port100_get_command_type_mask(dev
);
1555 if (!cmd_type_mask
) {
1556 nfc_err(&interface
->dev
,
1557 "Could not get supported command types\n");
1562 if (PORT100_CMD_TYPE_IS_SUPPORTED(cmd_type_mask
, PORT100_CMD_TYPE_1
))
1563 dev
->cmd_type
= PORT100_CMD_TYPE_1
;
1565 dev
->cmd_type
= PORT100_CMD_TYPE_0
;
1567 rc
= port100_set_command_type(dev
, dev
->cmd_type
);
1569 nfc_err(&interface
->dev
,
1570 "The device does not support command type %u\n",
1575 fw_version
= port100_get_firmware_version(dev
);
1577 nfc_err(&interface
->dev
,
1578 "Could not get device firmware version\n");
1580 nfc_info(&interface
->dev
,
1581 "Sony NFC Port-100 Series attached (firmware v%x.%02x)\n",
1582 (fw_version
& 0xFF00) >> 8, fw_version
& 0xFF);
1584 dev
->nfc_digital_dev
= nfc_digital_allocate_device(&port100_digital_ops
,
1586 PORT100_CAPABILITIES
,
1589 if (!dev
->nfc_digital_dev
) {
1590 nfc_err(&interface
->dev
,
1591 "Could not allocate nfc_digital_dev\n");
1596 nfc_digital_set_parent_dev(dev
->nfc_digital_dev
, &interface
->dev
);
1597 nfc_digital_set_drvdata(dev
->nfc_digital_dev
, dev
);
1599 rc
= nfc_digital_register_device(dev
->nfc_digital_dev
);
1601 nfc_err(&interface
->dev
,
1602 "Could not register digital device\n");
1609 nfc_digital_free_device(dev
->nfc_digital_dev
);
1612 usb_free_urb(dev
->in_urb
);
1613 usb_free_urb(dev
->out_urb
);
1614 usb_put_dev(dev
->udev
);
1619 static void port100_disconnect(struct usb_interface
*interface
)
1621 struct port100
*dev
;
1623 dev
= usb_get_intfdata(interface
);
1624 usb_set_intfdata(interface
, NULL
);
1626 nfc_digital_unregister_device(dev
->nfc_digital_dev
);
1627 nfc_digital_free_device(dev
->nfc_digital_dev
);
1629 usb_kill_urb(dev
->in_urb
);
1630 usb_kill_urb(dev
->out_urb
);
1632 usb_free_urb(dev
->in_urb
);
1633 usb_free_urb(dev
->out_urb
);
1634 usb_put_dev(dev
->udev
);
1638 nfc_info(&interface
->dev
, "Sony Port-100 NFC device disconnected\n");
1641 static struct usb_driver port100_driver
= {
1643 .probe
= port100_probe
,
1644 .disconnect
= port100_disconnect
,
1645 .id_table
= port100_table
,
1648 module_usb_driver(port100_driver
);
1650 MODULE_DESCRIPTION("NFC Port-100 series usb driver ver " VERSION
);
1651 MODULE_VERSION(VERSION
);
1652 MODULE_LICENSE("GPL");