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 * struct port100_tg_rf_setting - 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 const 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 const 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(const 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(const void *_frame
)
574 const 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(const 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(const void *frame
)
600 const 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(const struct port100
*dev
,
609 const struct port100_frame
*f
= frame
;
611 return (PORT100_FRAME_CMD(f
) == PORT100_CMD_RESPONSE(dev
->cmd
->code
));
614 static void port100_recv_response(struct urb
*urb
)
616 struct port100
*dev
= urb
->context
;
617 struct port100_cmd
*cmd
= dev
->cmd
;
620 cmd
->status
= urb
->status
;
622 switch (urb
->status
) {
627 nfc_dbg(&dev
->interface
->dev
,
628 "The urb has been canceled (status %d)\n", urb
->status
);
632 nfc_err(&dev
->interface
->dev
, "Urb failure (status %d)\n",
637 in_frame
= dev
->in_urb
->transfer_buffer
;
639 if (!port100_rx_frame_is_valid(in_frame
)) {
640 nfc_err(&dev
->interface
->dev
, "Received an invalid frame\n");
645 print_hex_dump_debug("PORT100 RX: ", DUMP_PREFIX_NONE
, 16, 1, in_frame
,
646 port100_rx_frame_size(in_frame
), false);
648 if (!port100_rx_frame_is_cmd_response(dev
, in_frame
)) {
649 nfc_err(&dev
->interface
->dev
,
650 "It's not the response to the last command\n");
656 schedule_work(&dev
->cmd_complete_work
);
659 static int port100_submit_urb_for_response(const struct port100
*dev
,
662 dev
->in_urb
->complete
= port100_recv_response
;
664 return usb_submit_urb(dev
->in_urb
, flags
);
667 static void port100_recv_ack(struct urb
*urb
)
669 struct port100
*dev
= urb
->context
;
670 struct port100_cmd
*cmd
= dev
->cmd
;
671 const struct port100_ack_frame
*in_frame
;
674 cmd
->status
= urb
->status
;
676 switch (urb
->status
) {
681 nfc_dbg(&dev
->interface
->dev
,
682 "The urb has been stopped (status %d)\n", urb
->status
);
686 nfc_err(&dev
->interface
->dev
, "Urb failure (status %d)\n",
691 in_frame
= dev
->in_urb
->transfer_buffer
;
693 if (!port100_rx_frame_is_ack(in_frame
)) {
694 nfc_err(&dev
->interface
->dev
, "Received an invalid ack\n");
699 rc
= port100_submit_urb_for_response(dev
, GFP_ATOMIC
);
701 nfc_err(&dev
->interface
->dev
,
702 "usb_submit_urb failed with result %d\n", rc
);
710 schedule_work(&dev
->cmd_complete_work
);
713 static int port100_submit_urb_for_ack(const struct port100
*dev
, gfp_t flags
)
715 dev
->in_urb
->complete
= port100_recv_ack
;
717 return usb_submit_urb(dev
->in_urb
, flags
);
720 static int port100_send_ack(struct port100
*dev
)
724 mutex_lock(&dev
->out_urb_lock
);
727 * If prior cancel is in-flight (dev->cmd_cancel == true), we
728 * can skip to send cancel. Then this will wait the prior
729 * cancel, or merged into the next cancel rarely if next
730 * cancel was started before waiting done. In any case, this
731 * will be waked up soon or later.
733 if (!dev
->cmd_cancel
) {
734 reinit_completion(&dev
->cmd_cancel_done
);
736 usb_kill_urb(dev
->out_urb
);
738 dev
->out_urb
->transfer_buffer
= ack_frame
;
739 dev
->out_urb
->transfer_buffer_length
= sizeof(ack_frame
);
740 rc
= usb_submit_urb(dev
->out_urb
, GFP_KERNEL
);
743 * Set the cmd_cancel flag only if the URB has been
744 * successfully submitted. It will be reset by the out
745 * URB completion callback port100_send_complete().
747 dev
->cmd_cancel
= !rc
;
750 mutex_unlock(&dev
->out_urb_lock
);
753 wait_for_completion(&dev
->cmd_cancel_done
);
758 static int port100_send_frame_async(struct port100
*dev
,
759 const struct sk_buff
*out
,
760 const struct sk_buff
*in
, int in_len
)
764 mutex_lock(&dev
->out_urb_lock
);
766 /* A command cancel frame as been sent through dev->out_urb. Don't try
767 * to submit a new one.
769 if (dev
->cmd_cancel
) {
774 dev
->out_urb
->transfer_buffer
= out
->data
;
775 dev
->out_urb
->transfer_buffer_length
= out
->len
;
777 dev
->in_urb
->transfer_buffer
= in
->data
;
778 dev
->in_urb
->transfer_buffer_length
= in_len
;
780 print_hex_dump_debug("PORT100 TX: ", DUMP_PREFIX_NONE
, 16, 1,
781 out
->data
, out
->len
, false);
783 rc
= usb_submit_urb(dev
->out_urb
, GFP_KERNEL
);
787 rc
= port100_submit_urb_for_ack(dev
, GFP_KERNEL
);
789 usb_kill_urb(dev
->out_urb
);
792 mutex_unlock(&dev
->out_urb_lock
);
797 static void port100_build_cmd_frame(struct port100
*dev
, u8 cmd_code
,
800 /* payload is already there, just update datalen */
801 int payload_len
= skb
->len
;
803 skb_push(skb
, PORT100_FRAME_HEADER_LEN
);
804 skb_put(skb
, PORT100_FRAME_TAIL_LEN
);
806 port100_tx_frame_init(skb
->data
, cmd_code
);
807 port100_tx_update_payload_len(skb
->data
, payload_len
);
808 port100_tx_frame_finish(skb
->data
);
811 static void port100_send_async_complete(struct port100
*dev
)
813 struct port100_cmd
*cmd
= dev
->cmd
;
814 int status
= cmd
->status
;
816 struct sk_buff
*req
= cmd
->req
;
817 struct sk_buff
*resp
= cmd
->resp
;
824 cmd
->complete_cb(dev
, cmd
->complete_cb_context
,
830 skb_put(resp
, port100_rx_frame_size(resp
->data
));
831 skb_pull(resp
, PORT100_FRAME_HEADER_LEN
);
832 skb_trim(resp
, resp
->len
- PORT100_FRAME_TAIL_LEN
);
834 cmd
->complete_cb(dev
, cmd
->complete_cb_context
, resp
);
840 static int port100_send_cmd_async(struct port100
*dev
, u8 cmd_code
,
842 port100_send_async_complete_t complete_cb
,
843 void *complete_cb_context
)
845 struct port100_cmd
*cmd
;
846 struct sk_buff
*resp
;
848 int resp_len
= PORT100_FRAME_HEADER_LEN
+
849 PORT100_FRAME_MAX_PAYLOAD_LEN
+
850 PORT100_FRAME_TAIL_LEN
;
853 nfc_err(&dev
->interface
->dev
,
854 "A command is still in process\n");
858 resp
= alloc_skb(resp_len
, GFP_KERNEL
);
862 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
868 cmd
->code
= cmd_code
;
871 cmd
->resp_len
= resp_len
;
872 cmd
->complete_cb
= complete_cb
;
873 cmd
->complete_cb_context
= complete_cb_context
;
875 port100_build_cmd_frame(dev
, cmd_code
, req
);
879 rc
= port100_send_frame_async(dev
, req
, resp
, resp_len
);
889 struct port100_sync_cmd_response
{
890 struct sk_buff
*resp
;
891 struct completion done
;
894 static void port100_wq_cmd_complete(struct work_struct
*work
)
896 struct port100
*dev
= container_of(work
, struct port100
,
899 port100_send_async_complete(dev
);
902 static void port100_send_sync_complete(struct port100
*dev
, void *_arg
,
903 struct sk_buff
*resp
)
905 struct port100_sync_cmd_response
*arg
= _arg
;
908 complete(&arg
->done
);
911 static struct sk_buff
*port100_send_cmd_sync(struct port100
*dev
, u8 cmd_code
,
915 struct port100_sync_cmd_response arg
;
917 init_completion(&arg
.done
);
919 rc
= port100_send_cmd_async(dev
, cmd_code
, req
,
920 port100_send_sync_complete
, &arg
);
926 wait_for_completion(&arg
.done
);
931 static void port100_send_complete(struct urb
*urb
)
933 struct port100
*dev
= urb
->context
;
935 if (dev
->cmd_cancel
) {
936 complete_all(&dev
->cmd_cancel_done
);
937 dev
->cmd_cancel
= false;
940 switch (urb
->status
) {
945 nfc_dbg(&dev
->interface
->dev
,
946 "The urb has been stopped (status %d)\n", urb
->status
);
950 nfc_err(&dev
->interface
->dev
, "Urb failure (status %d)\n",
955 static void port100_abort_cmd(struct nfc_digital_dev
*ddev
)
957 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
959 /* An ack will cancel the last issued command */
960 port100_send_ack(dev
);
962 /* cancel the urb request */
963 usb_kill_urb(dev
->in_urb
);
966 static struct sk_buff
*port100_alloc_skb(const struct port100
*dev
, unsigned int size
)
970 skb
= alloc_skb(dev
->skb_headroom
+ dev
->skb_tailroom
+ size
,
973 skb_reserve(skb
, dev
->skb_headroom
);
978 static int port100_set_command_type(struct port100
*dev
, u8 command_type
)
981 struct sk_buff
*resp
;
984 skb
= port100_alloc_skb(dev
, 1);
988 skb_put_u8(skb
, command_type
);
990 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_SET_COMMAND_TYPE
, skb
);
992 return PTR_ERR(resp
);
1001 static u64
port100_get_command_type_mask(struct port100
*dev
)
1003 struct sk_buff
*skb
;
1004 struct sk_buff
*resp
;
1007 skb
= port100_alloc_skb(dev
, 0);
1011 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_GET_COMMAND_TYPE
, skb
);
1018 mask
= be64_to_cpu(*(__be64
*)resp
->data
);
1020 dev_kfree_skb(resp
);
1025 static u16
port100_get_firmware_version(struct port100
*dev
)
1027 struct sk_buff
*skb
;
1028 struct sk_buff
*resp
;
1031 skb
= port100_alloc_skb(dev
, 0);
1035 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_GET_FIRMWARE_VERSION
,
1040 fw_ver
= le16_to_cpu(*(__le16
*)resp
->data
);
1042 dev_kfree_skb(resp
);
1047 static int port100_switch_rf(struct nfc_digital_dev
*ddev
, bool on
)
1049 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1050 struct sk_buff
*skb
, *resp
;
1052 skb
= port100_alloc_skb(dev
, 1);
1056 skb_put_u8(skb
, on
? 1 : 0);
1058 /* Cancel the last command if the device is being switched off */
1060 port100_abort_cmd(ddev
);
1062 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_SWITCH_RF
, skb
);
1065 return PTR_ERR(resp
);
1067 dev_kfree_skb(resp
);
1072 static int port100_in_set_rf(struct nfc_digital_dev
*ddev
, u8 rf
)
1074 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1075 struct sk_buff
*skb
;
1076 struct sk_buff
*resp
;
1079 if (rf
>= NFC_DIGITAL_RF_TECH_LAST
)
1082 skb
= port100_alloc_skb(dev
, sizeof(struct port100_in_rf_setting
));
1086 skb_put_data(skb
, &in_rf_settings
[rf
],
1087 sizeof(struct port100_in_rf_setting
));
1089 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_IN_SET_RF
, skb
);
1092 return PTR_ERR(resp
);
1096 dev_kfree_skb(resp
);
1101 static int port100_in_set_framing(struct nfc_digital_dev
*ddev
, int param
)
1103 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1104 const struct port100_protocol
*protocols
;
1105 struct sk_buff
*skb
;
1106 struct sk_buff
*resp
;
1111 if (param
>= NFC_DIGITAL_FRAMING_LAST
)
1114 protocols
= in_protocols
[param
];
1117 while (protocols
[num_protocols
].number
!= PORT100_IN_PROT_END
)
1123 size
= sizeof(struct port100_protocol
) * num_protocols
;
1125 skb
= port100_alloc_skb(dev
, size
);
1129 skb_put_data(skb
, protocols
, size
);
1131 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_IN_SET_PROTOCOL
, skb
);
1134 return PTR_ERR(resp
);
1138 dev_kfree_skb(resp
);
1143 static int port100_in_configure_hw(struct nfc_digital_dev
*ddev
, int type
,
1146 if (type
== NFC_DIGITAL_CONFIG_RF_TECH
)
1147 return port100_in_set_rf(ddev
, param
);
1149 if (type
== NFC_DIGITAL_CONFIG_FRAMING
)
1150 return port100_in_set_framing(ddev
, param
);
1155 static void port100_in_comm_rf_complete(struct port100
*dev
, void *arg
,
1156 struct sk_buff
*resp
)
1158 const struct port100_cb_arg
*cb_arg
= arg
;
1159 nfc_digital_cmd_complete_t cb
= cb_arg
->complete_cb
;
1168 if (resp
->len
< 4) {
1169 nfc_err(&dev
->interface
->dev
,
1170 "Invalid packet length received\n");
1175 status
= le32_to_cpu(*(__le32
*)resp
->data
);
1177 skb_pull(resp
, sizeof(u32
));
1179 if (status
== PORT100_CMD_STATUS_TIMEOUT
) {
1184 if (status
!= PORT100_CMD_STATUS_OK
) {
1185 nfc_err(&dev
->interface
->dev
,
1186 "in_comm_rf failed with status 0x%08x\n", status
);
1191 /* Remove collision bits byte */
1201 cb(dev
->nfc_digital_dev
, cb_arg
->complete_arg
, resp
);
1206 static int port100_in_send_cmd(struct nfc_digital_dev
*ddev
,
1207 struct sk_buff
*skb
, u16 _timeout
,
1208 nfc_digital_cmd_complete_t cb
, void *arg
)
1210 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1211 struct port100_cb_arg
*cb_arg
;
1214 cb_arg
= kzalloc(sizeof(struct port100_cb_arg
), GFP_KERNEL
);
1218 cb_arg
->complete_cb
= cb
;
1219 cb_arg
->complete_arg
= arg
;
1221 timeout
= cpu_to_le16(_timeout
* 10);
1223 memcpy(skb_push(skb
, sizeof(__le16
)), &timeout
, sizeof(__le16
));
1225 return port100_send_cmd_async(dev
, PORT100_CMD_IN_COMM_RF
, skb
,
1226 port100_in_comm_rf_complete
, cb_arg
);
1229 static int port100_tg_set_rf(struct nfc_digital_dev
*ddev
, u8 rf
)
1231 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1232 struct sk_buff
*skb
;
1233 struct sk_buff
*resp
;
1236 if (rf
>= NFC_DIGITAL_RF_TECH_LAST
)
1239 skb
= port100_alloc_skb(dev
, sizeof(struct port100_tg_rf_setting
));
1243 skb_put_data(skb
, &tg_rf_settings
[rf
],
1244 sizeof(struct port100_tg_rf_setting
));
1246 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_TG_SET_RF
, skb
);
1249 return PTR_ERR(resp
);
1253 dev_kfree_skb(resp
);
1258 static int port100_tg_set_framing(struct nfc_digital_dev
*ddev
, int param
)
1260 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1261 const struct port100_protocol
*protocols
;
1262 struct sk_buff
*skb
;
1263 struct sk_buff
*resp
;
1268 if (param
>= NFC_DIGITAL_FRAMING_LAST
)
1271 protocols
= tg_protocols
[param
];
1274 while (protocols
[num_protocols
].number
!= PORT100_TG_PROT_END
)
1280 size
= sizeof(struct port100_protocol
) * num_protocols
;
1282 skb
= port100_alloc_skb(dev
, size
);
1286 skb_put_data(skb
, protocols
, size
);
1288 resp
= port100_send_cmd_sync(dev
, PORT100_CMD_TG_SET_PROTOCOL
, skb
);
1291 return PTR_ERR(resp
);
1295 dev_kfree_skb(resp
);
1300 static int port100_tg_configure_hw(struct nfc_digital_dev
*ddev
, int type
,
1303 if (type
== NFC_DIGITAL_CONFIG_RF_TECH
)
1304 return port100_tg_set_rf(ddev
, param
);
1306 if (type
== NFC_DIGITAL_CONFIG_FRAMING
)
1307 return port100_tg_set_framing(ddev
, param
);
1312 static bool port100_tg_target_activated(struct port100
*dev
, u8 tgt_activated
)
1316 switch (dev
->cmd_type
) {
1317 case PORT100_CMD_TYPE_0
:
1318 mask
= PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK
;
1320 case PORT100_CMD_TYPE_1
:
1321 mask
= PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK
|
1322 PORT100_MDAA_TGT_WAS_ACTIVATED_MASK
;
1325 nfc_err(&dev
->interface
->dev
, "Unknown command type\n");
1329 return ((tgt_activated
& mask
) == mask
);
1332 static void port100_tg_comm_rf_complete(struct port100
*dev
, void *arg
,
1333 struct sk_buff
*resp
)
1336 const struct port100_cb_arg
*cb_arg
= arg
;
1337 nfc_digital_cmd_complete_t cb
= cb_arg
->complete_cb
;
1338 struct port100_tg_comm_rf_res
*hdr
;
1343 hdr
= (struct port100_tg_comm_rf_res
*)resp
->data
;
1345 status
= le32_to_cpu(hdr
->status
);
1348 !port100_tg_target_activated(dev
, hdr
->target_activated
)) {
1350 resp
= ERR_PTR(-ETIMEDOUT
);
1355 skb_pull(resp
, sizeof(struct port100_tg_comm_rf_res
));
1357 if (status
!= PORT100_CMD_STATUS_OK
) {
1360 if (status
== PORT100_CMD_STATUS_TIMEOUT
)
1361 resp
= ERR_PTR(-ETIMEDOUT
);
1363 resp
= ERR_PTR(-EIO
);
1367 cb(dev
->nfc_digital_dev
, cb_arg
->complete_arg
, resp
);
1372 static int port100_tg_send_cmd(struct nfc_digital_dev
*ddev
,
1373 struct sk_buff
*skb
, u16 timeout
,
1374 nfc_digital_cmd_complete_t cb
, void *arg
)
1376 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1377 struct port100_tg_comm_rf_cmd
*hdr
;
1378 struct port100_cb_arg
*cb_arg
;
1380 cb_arg
= kzalloc(sizeof(struct port100_cb_arg
), GFP_KERNEL
);
1384 cb_arg
->complete_cb
= cb
;
1385 cb_arg
->complete_arg
= arg
;
1387 skb_push(skb
, sizeof(struct port100_tg_comm_rf_cmd
));
1389 hdr
= (struct port100_tg_comm_rf_cmd
*)skb
->data
;
1391 memset(hdr
, 0, sizeof(struct port100_tg_comm_rf_cmd
));
1392 hdr
->guard_time
= cpu_to_le16(500);
1393 hdr
->send_timeout
= cpu_to_le16(0xFFFF);
1394 hdr
->recv_timeout
= cpu_to_le16(timeout
);
1396 return port100_send_cmd_async(dev
, PORT100_CMD_TG_COMM_RF
, skb
,
1397 port100_tg_comm_rf_complete
, cb_arg
);
1400 static int port100_listen_mdaa(struct nfc_digital_dev
*ddev
,
1401 struct digital_tg_mdaa_params
*params
,
1403 nfc_digital_cmd_complete_t cb
, void *arg
)
1405 struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1406 struct port100_tg_comm_rf_cmd
*hdr
;
1407 struct port100_cb_arg
*cb_arg
;
1408 struct sk_buff
*skb
;
1411 rc
= port100_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_RF_TECH
,
1412 NFC_DIGITAL_RF_TECH_106A
);
1416 rc
= port100_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
1417 NFC_DIGITAL_FRAMING_NFCA_NFC_DEP
);
1421 cb_arg
= kzalloc(sizeof(struct port100_cb_arg
), GFP_KERNEL
);
1425 cb_arg
->complete_cb
= cb
;
1426 cb_arg
->complete_arg
= arg
;
1429 skb
= port100_alloc_skb(dev
, 0);
1435 skb_push(skb
, sizeof(struct port100_tg_comm_rf_cmd
));
1436 hdr
= (struct port100_tg_comm_rf_cmd
*)skb
->data
;
1438 memset(hdr
, 0, sizeof(struct port100_tg_comm_rf_cmd
));
1440 hdr
->guard_time
= 0;
1441 hdr
->send_timeout
= cpu_to_le16(0xFFFF);
1443 hdr
->nfca_param
[0] = (params
->sens_res
>> 8) & 0xFF;
1444 hdr
->nfca_param
[1] = params
->sens_res
& 0xFF;
1445 memcpy(hdr
->nfca_param
+ 2, params
->nfcid1
, 3);
1446 hdr
->nfca_param
[5] = params
->sel_res
;
1447 memcpy(hdr
->nfcf_param
, params
->nfcid2
, 8);
1448 hdr
->nfcf_param
[16] = (params
->sc
>> 8) & 0xFF;
1449 hdr
->nfcf_param
[17] = params
->sc
& 0xFF;
1450 hdr
->recv_timeout
= cpu_to_le16(timeout
);
1452 return port100_send_cmd_async(dev
, PORT100_CMD_TG_COMM_RF
, skb
,
1453 port100_tg_comm_rf_complete
, cb_arg
);
1456 static int port100_listen(struct nfc_digital_dev
*ddev
, u16 timeout
,
1457 nfc_digital_cmd_complete_t cb
, void *arg
)
1459 const struct port100
*dev
= nfc_digital_get_drvdata(ddev
);
1460 struct sk_buff
*skb
;
1462 skb
= port100_alloc_skb(dev
, 0);
1466 return port100_tg_send_cmd(ddev
, skb
, timeout
, cb
, arg
);
1469 static const struct nfc_digital_ops port100_digital_ops
= {
1470 .in_configure_hw
= port100_in_configure_hw
,
1471 .in_send_cmd
= port100_in_send_cmd
,
1473 .tg_listen_mdaa
= port100_listen_mdaa
,
1474 .tg_listen
= port100_listen
,
1475 .tg_configure_hw
= port100_tg_configure_hw
,
1476 .tg_send_cmd
= port100_tg_send_cmd
,
1478 .switch_rf
= port100_switch_rf
,
1479 .abort_cmd
= port100_abort_cmd
,
1482 static const struct usb_device_id port100_table
[] = {
1483 { USB_DEVICE(SONY_VENDOR_ID
, RCS380S_PRODUCT_ID
), },
1484 { USB_DEVICE(SONY_VENDOR_ID
, RCS380P_PRODUCT_ID
), },
1487 MODULE_DEVICE_TABLE(usb
, port100_table
);
1489 static int port100_probe(struct usb_interface
*interface
,
1490 const struct usb_device_id
*id
)
1492 struct port100
*dev
;
1494 struct usb_host_interface
*iface_desc
;
1495 struct usb_endpoint_descriptor
*endpoint
;
1502 dev
= devm_kzalloc(&interface
->dev
, sizeof(struct port100
), GFP_KERNEL
);
1506 mutex_init(&dev
->out_urb_lock
);
1507 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
1508 dev
->interface
= interface
;
1509 usb_set_intfdata(interface
, dev
);
1511 in_endpoint
= out_endpoint
= 0;
1512 iface_desc
= interface
->cur_altsetting
;
1513 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1514 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1516 if (!in_endpoint
&& usb_endpoint_is_bulk_in(endpoint
))
1517 in_endpoint
= endpoint
->bEndpointAddress
;
1519 if (!out_endpoint
&& usb_endpoint_is_bulk_out(endpoint
))
1520 out_endpoint
= endpoint
->bEndpointAddress
;
1523 if (!in_endpoint
|| !out_endpoint
) {
1524 nfc_err(&interface
->dev
,
1525 "Could not find bulk-in or bulk-out endpoint\n");
1530 dev
->in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1531 dev
->out_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1533 if (!dev
->in_urb
|| !dev
->out_urb
) {
1534 nfc_err(&interface
->dev
, "Could not allocate USB URBs\n");
1539 usb_fill_bulk_urb(dev
->in_urb
, dev
->udev
,
1540 usb_rcvbulkpipe(dev
->udev
, in_endpoint
),
1541 NULL
, 0, NULL
, dev
);
1542 usb_fill_bulk_urb(dev
->out_urb
, dev
->udev
,
1543 usb_sndbulkpipe(dev
->udev
, out_endpoint
),
1544 NULL
, 0, port100_send_complete
, dev
);
1545 dev
->out_urb
->transfer_flags
= URB_ZERO_PACKET
;
1547 dev
->skb_headroom
= PORT100_FRAME_HEADER_LEN
+
1548 PORT100_COMM_RF_HEAD_MAX_LEN
;
1549 dev
->skb_tailroom
= PORT100_FRAME_TAIL_LEN
;
1551 init_completion(&dev
->cmd_cancel_done
);
1552 INIT_WORK(&dev
->cmd_complete_work
, port100_wq_cmd_complete
);
1554 /* The first thing to do with the Port-100 is to set the command type
1555 * to be used. If supported we use command type 1. 0 otherwise.
1557 cmd_type_mask
= port100_get_command_type_mask(dev
);
1558 if (!cmd_type_mask
) {
1559 nfc_err(&interface
->dev
,
1560 "Could not get supported command types\n");
1565 if (PORT100_CMD_TYPE_IS_SUPPORTED(cmd_type_mask
, PORT100_CMD_TYPE_1
))
1566 dev
->cmd_type
= PORT100_CMD_TYPE_1
;
1568 dev
->cmd_type
= PORT100_CMD_TYPE_0
;
1570 rc
= port100_set_command_type(dev
, dev
->cmd_type
);
1572 nfc_err(&interface
->dev
,
1573 "The device does not support command type %u\n",
1578 fw_version
= port100_get_firmware_version(dev
);
1580 nfc_err(&interface
->dev
,
1581 "Could not get device firmware version\n");
1583 nfc_info(&interface
->dev
,
1584 "Sony NFC Port-100 Series attached (firmware v%x.%02x)\n",
1585 (fw_version
& 0xFF00) >> 8, fw_version
& 0xFF);
1587 dev
->nfc_digital_dev
= nfc_digital_allocate_device(&port100_digital_ops
,
1589 PORT100_CAPABILITIES
,
1592 if (!dev
->nfc_digital_dev
) {
1593 nfc_err(&interface
->dev
,
1594 "Could not allocate nfc_digital_dev\n");
1599 nfc_digital_set_parent_dev(dev
->nfc_digital_dev
, &interface
->dev
);
1600 nfc_digital_set_drvdata(dev
->nfc_digital_dev
, dev
);
1602 rc
= nfc_digital_register_device(dev
->nfc_digital_dev
);
1604 nfc_err(&interface
->dev
,
1605 "Could not register digital device\n");
1612 nfc_digital_free_device(dev
->nfc_digital_dev
);
1615 usb_kill_urb(dev
->in_urb
);
1616 usb_free_urb(dev
->in_urb
);
1617 usb_kill_urb(dev
->out_urb
);
1618 usb_free_urb(dev
->out_urb
);
1619 usb_put_dev(dev
->udev
);
1624 static void port100_disconnect(struct usb_interface
*interface
)
1626 struct port100
*dev
;
1628 dev
= usb_get_intfdata(interface
);
1629 usb_set_intfdata(interface
, NULL
);
1631 nfc_digital_unregister_device(dev
->nfc_digital_dev
);
1632 nfc_digital_free_device(dev
->nfc_digital_dev
);
1634 usb_kill_urb(dev
->in_urb
);
1635 usb_kill_urb(dev
->out_urb
);
1637 usb_free_urb(dev
->in_urb
);
1638 usb_free_urb(dev
->out_urb
);
1639 usb_put_dev(dev
->udev
);
1643 nfc_info(&interface
->dev
, "Sony Port-100 NFC device disconnected\n");
1646 static struct usb_driver port100_driver
= {
1648 .probe
= port100_probe
,
1649 .disconnect
= port100_disconnect
,
1650 .id_table
= port100_table
,
1653 module_usb_driver(port100_driver
);
1655 MODULE_DESCRIPTION("NFC Port-100 series usb driver ver " VERSION
);
1656 MODULE_VERSION(VERSION
);
1657 MODULE_LICENSE("GPL");