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 RCS380_PRODUCT_ID 0x06c1
27 #define PORT100_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
28 NFC_PROTO_MIFARE_MASK | \
29 NFC_PROTO_FELICA_MASK | \
30 NFC_PROTO_NFC_DEP_MASK)
32 #define PORT100_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC | \
33 NFC_DIGITAL_DRV_CAPS_TG_CRC)
35 /* Standard port100 frame definitions */
36 #define PORT100_FRAME_HEADER_LEN (sizeof(struct port100_frame) \
37 + 2) /* data[0] CC, data[1] SCC */
38 #define PORT100_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
40 #define PORT100_COMM_RF_HEAD_MAX_LEN (sizeof(struct port100_tg_comm_rf_cmd))
43 * Max extended frame payload len, excluding CC and SCC
44 * which are already in PORT100_FRAME_HEADER_LEN.
46 #define PORT100_FRAME_MAX_PAYLOAD_LEN 1001
48 #define PORT100_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
50 static u8 ack_frame[PORT100_FRAME_ACK_SIZE] = {
51 0x00, 0x00, 0xff, 0x00, 0xff, 0x00
54 #define PORT100_FRAME_CHECKSUM(f) (f->data[le16_to_cpu(f->datalen)])
55 #define PORT100_FRAME_POSTAMBLE(f) (f->data[le16_to_cpu(f->datalen) + 1])
58 #define PORT100_FRAME_SOF 0x00FF
59 #define PORT100_FRAME_EXT 0xFFFF
60 #define PORT100_FRAME_ACK 0x00FF
62 /* Port-100 command: in or out */
63 #define PORT100_FRAME_DIRECTION(f) (f->data[0]) /* CC */
64 #define PORT100_FRAME_DIR_OUT 0xD6
65 #define PORT100_FRAME_DIR_IN 0xD7
67 /* Port-100 sub-command */
68 #define PORT100_FRAME_CMD(f) (f->data[1]) /* SCC */
70 #define PORT100_CMD_GET_FIRMWARE_VERSION 0x20
71 #define PORT100_CMD_GET_COMMAND_TYPE 0x28
72 #define PORT100_CMD_SET_COMMAND_TYPE 0x2A
74 #define PORT100_CMD_IN_SET_RF 0x00
75 #define PORT100_CMD_IN_SET_PROTOCOL 0x02
76 #define PORT100_CMD_IN_COMM_RF 0x04
78 #define PORT100_CMD_TG_SET_RF 0x40
79 #define PORT100_CMD_TG_SET_PROTOCOL 0x42
80 #define PORT100_CMD_TG_SET_RF_OFF 0x46
81 #define PORT100_CMD_TG_COMM_RF 0x48
83 #define PORT100_CMD_SWITCH_RF 0x06
85 #define PORT100_CMD_RESPONSE(cmd) (cmd + 1)
87 #define PORT100_CMD_TYPE_IS_SUPPORTED(mask, cmd_type) \
88 ((mask) & (0x01 << (cmd_type)))
89 #define PORT100_CMD_TYPE_0 0
90 #define PORT100_CMD_TYPE_1 1
92 #define PORT100_CMD_STATUS_OK 0x00
93 #define PORT100_CMD_STATUS_TIMEOUT 0x80
95 #define PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK 0x01
96 #define PORT100_MDAA_TGT_WAS_ACTIVATED_MASK 0x02
100 typedef void (*port100_send_async_complete_t)(struct port100 *dev, void *arg,
101 struct sk_buff *resp);
104 * Setting sets structure for in_set_rf command
106 * @in_*_set_number: Represent the entry indexes in the port-100 RF Base Table.
107 * This table contains multiple RF setting sets required for RF
110 * @in_*_comm_type: Theses fields set the communication type to be used.
112 struct port100_in_rf_setting {
113 u8 in_send_set_number;
114 u8 in_send_comm_type;
115 u8 in_recv_set_number;
116 u8 in_recv_comm_type;
119 #define PORT100_COMM_TYPE_IN_212F 0x01
120 #define PORT100_COMM_TYPE_IN_424F 0x02
121 #define PORT100_COMM_TYPE_IN_106A 0x03
123 static const struct port100_in_rf_setting in_rf_settings[] = {
124 [NFC_DIGITAL_RF_TECH_212F] = {
125 .in_send_set_number = 1,
126 .in_send_comm_type = PORT100_COMM_TYPE_IN_212F,
127 .in_recv_set_number = 15,
128 .in_recv_comm_type = PORT100_COMM_TYPE_IN_212F,
130 [NFC_DIGITAL_RF_TECH_424F] = {
131 .in_send_set_number = 1,
132 .in_send_comm_type = PORT100_COMM_TYPE_IN_424F,
133 .in_recv_set_number = 15,
134 .in_recv_comm_type = PORT100_COMM_TYPE_IN_424F,
136 [NFC_DIGITAL_RF_TECH_106A] = {
137 .in_send_set_number = 2,
138 .in_send_comm_type = PORT100_COMM_TYPE_IN_106A,
139 .in_recv_set_number = 15,
140 .in_recv_comm_type = PORT100_COMM_TYPE_IN_106A,
145 * Setting sets structure for tg_set_rf command
147 * @tg_set_number: Represents the entry index in the port-100 RF Base Table.
148 * This table contains multiple RF setting sets required for RF
149 * communication. this field is used for both send and receive
152 * @tg_comm_type: Sets the communication type to be used to send and receive
155 struct port100_tg_rf_setting {
160 #define PORT100_COMM_TYPE_TG_106A 0x0B
161 #define PORT100_COMM_TYPE_TG_212F 0x0C
162 #define PORT100_COMM_TYPE_TG_424F 0x0D
164 static const struct port100_tg_rf_setting tg_rf_settings[] = {
165 [NFC_DIGITAL_RF_TECH_106A] = {
167 .tg_comm_type = PORT100_COMM_TYPE_TG_106A,
169 [NFC_DIGITAL_RF_TECH_212F] = {
171 .tg_comm_type = PORT100_COMM_TYPE_TG_212F,
173 [NFC_DIGITAL_RF_TECH_424F] = {
175 .tg_comm_type = PORT100_COMM_TYPE_TG_424F,
179 #define PORT100_IN_PROT_INITIAL_GUARD_TIME 0x00
180 #define PORT100_IN_PROT_ADD_CRC 0x01
181 #define PORT100_IN_PROT_CHECK_CRC 0x02
182 #define PORT100_IN_PROT_MULTI_CARD 0x03
183 #define PORT100_IN_PROT_ADD_PARITY 0x04
184 #define PORT100_IN_PROT_CHECK_PARITY 0x05
185 #define PORT100_IN_PROT_BITWISE_AC_RECV_MODE 0x06
186 #define PORT100_IN_PROT_VALID_BIT_NUMBER 0x07
187 #define PORT100_IN_PROT_CRYPTO1 0x08
188 #define PORT100_IN_PROT_ADD_SOF 0x09
189 #define PORT100_IN_PROT_CHECK_SOF 0x0A
190 #define PORT100_IN_PROT_ADD_EOF 0x0B
191 #define PORT100_IN_PROT_CHECK_EOF 0x0C
192 #define PORT100_IN_PROT_DEAF_TIME 0x0E
193 #define PORT100_IN_PROT_CRM 0x0F
194 #define PORT100_IN_PROT_CRM_MIN_LEN 0x10
195 #define PORT100_IN_PROT_T1_TAG_FRAME 0x11
196 #define PORT100_IN_PROT_RFCA 0x12
197 #define PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR 0x13
198 #define PORT100_IN_PROT_END 0x14
200 #define PORT100_IN_MAX_NUM_PROTOCOLS 19
202 #define PORT100_TG_PROT_TU 0x00
203 #define PORT100_TG_PROT_RF_OFF 0x01
204 #define PORT100_TG_PROT_CRM 0x02
205 #define PORT100_TG_PROT_END 0x03
207 #define PORT100_TG_MAX_NUM_PROTOCOLS 3
209 struct port100_protocol {
214 static struct port100_protocol
215 in_protocols[][PORT100_IN_MAX_NUM_PROTOCOLS + 1] = {
216 [NFC_DIGITAL_FRAMING_NFCA_SHORT] = {
217 { PORT100_IN_PROT_INITIAL_GUARD_TIME, 6 },
218 { PORT100_IN_PROT_ADD_CRC, 0 },
219 { PORT100_IN_PROT_CHECK_CRC, 0 },
220 { PORT100_IN_PROT_MULTI_CARD, 0 },
221 { PORT100_IN_PROT_ADD_PARITY, 0 },
222 { PORT100_IN_PROT_CHECK_PARITY, 1 },
223 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
224 { PORT100_IN_PROT_VALID_BIT_NUMBER, 7 },
225 { PORT100_IN_PROT_CRYPTO1, 0 },
226 { PORT100_IN_PROT_ADD_SOF, 0 },
227 { PORT100_IN_PROT_CHECK_SOF, 0 },
228 { PORT100_IN_PROT_ADD_EOF, 0 },
229 { PORT100_IN_PROT_CHECK_EOF, 0 },
230 { PORT100_IN_PROT_DEAF_TIME, 4 },
231 { PORT100_IN_PROT_CRM, 0 },
232 { PORT100_IN_PROT_CRM_MIN_LEN, 0 },
233 { PORT100_IN_PROT_T1_TAG_FRAME, 0 },
234 { PORT100_IN_PROT_RFCA, 0 },
235 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
236 { PORT100_IN_PROT_END, 0 },
238 [NFC_DIGITAL_FRAMING_NFCA_STANDARD] = {
239 { PORT100_IN_PROT_INITIAL_GUARD_TIME, 6 },
240 { PORT100_IN_PROT_ADD_CRC, 0 },
241 { PORT100_IN_PROT_CHECK_CRC, 0 },
242 { PORT100_IN_PROT_MULTI_CARD, 0 },
243 { PORT100_IN_PROT_ADD_PARITY, 1 },
244 { PORT100_IN_PROT_CHECK_PARITY, 1 },
245 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
246 { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
247 { PORT100_IN_PROT_CRYPTO1, 0 },
248 { PORT100_IN_PROT_ADD_SOF, 0 },
249 { PORT100_IN_PROT_CHECK_SOF, 0 },
250 { PORT100_IN_PROT_ADD_EOF, 0 },
251 { PORT100_IN_PROT_CHECK_EOF, 0 },
252 { PORT100_IN_PROT_DEAF_TIME, 4 },
253 { PORT100_IN_PROT_CRM, 0 },
254 { PORT100_IN_PROT_CRM_MIN_LEN, 0 },
255 { PORT100_IN_PROT_T1_TAG_FRAME, 0 },
256 { PORT100_IN_PROT_RFCA, 0 },
257 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
258 { PORT100_IN_PROT_END, 0 },
260 [NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A] = {
261 { PORT100_IN_PROT_INITIAL_GUARD_TIME, 6 },
262 { PORT100_IN_PROT_ADD_CRC, 1 },
263 { PORT100_IN_PROT_CHECK_CRC, 1 },
264 { PORT100_IN_PROT_MULTI_CARD, 0 },
265 { PORT100_IN_PROT_ADD_PARITY, 1 },
266 { PORT100_IN_PROT_CHECK_PARITY, 1 },
267 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
268 { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
269 { PORT100_IN_PROT_CRYPTO1, 0 },
270 { PORT100_IN_PROT_ADD_SOF, 0 },
271 { PORT100_IN_PROT_CHECK_SOF, 0 },
272 { PORT100_IN_PROT_ADD_EOF, 0 },
273 { PORT100_IN_PROT_CHECK_EOF, 0 },
274 { PORT100_IN_PROT_DEAF_TIME, 4 },
275 { PORT100_IN_PROT_CRM, 0 },
276 { PORT100_IN_PROT_CRM_MIN_LEN, 0 },
277 { PORT100_IN_PROT_T1_TAG_FRAME, 0 },
278 { PORT100_IN_PROT_RFCA, 0 },
279 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
280 { PORT100_IN_PROT_END, 0 },
282 [NFC_DIGITAL_FRAMING_NFCA_T1T] = {
283 /* nfc_digital_framing_nfca_short */
284 { PORT100_IN_PROT_ADD_CRC, 2 },
285 { PORT100_IN_PROT_CHECK_CRC, 2 },
286 { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
287 { PORT100_IN_PROT_T1_TAG_FRAME, 2 },
288 { PORT100_IN_PROT_END, 0 },
290 [NFC_DIGITAL_FRAMING_NFCA_T2T] = {
291 /* nfc_digital_framing_nfca_standard */
292 { PORT100_IN_PROT_ADD_CRC, 1 },
293 { PORT100_IN_PROT_CHECK_CRC, 0 },
294 { PORT100_IN_PROT_END, 0 },
296 [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP] = {
297 /* nfc_digital_framing_nfca_standard */
298 { PORT100_IN_PROT_END, 0 },
300 [NFC_DIGITAL_FRAMING_NFCF] = {
301 { PORT100_IN_PROT_INITIAL_GUARD_TIME, 18 },
302 { PORT100_IN_PROT_ADD_CRC, 1 },
303 { PORT100_IN_PROT_CHECK_CRC, 1 },
304 { PORT100_IN_PROT_MULTI_CARD, 0 },
305 { PORT100_IN_PROT_ADD_PARITY, 0 },
306 { PORT100_IN_PROT_CHECK_PARITY, 0 },
307 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
308 { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
309 { PORT100_IN_PROT_CRYPTO1, 0 },
310 { PORT100_IN_PROT_ADD_SOF, 0 },
311 { PORT100_IN_PROT_CHECK_SOF, 0 },
312 { PORT100_IN_PROT_ADD_EOF, 0 },
313 { PORT100_IN_PROT_CHECK_EOF, 0 },
314 { PORT100_IN_PROT_DEAF_TIME, 4 },
315 { PORT100_IN_PROT_CRM, 0 },
316 { PORT100_IN_PROT_CRM_MIN_LEN, 0 },
317 { PORT100_IN_PROT_T1_TAG_FRAME, 0 },
318 { PORT100_IN_PROT_RFCA, 0 },
319 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
320 { PORT100_IN_PROT_END, 0 },
322 [NFC_DIGITAL_FRAMING_NFCF_T3T] = {
323 /* nfc_digital_framing_nfcf */
324 { PORT100_IN_PROT_END, 0 },
326 [NFC_DIGITAL_FRAMING_NFCF_NFC_DEP] = {
327 /* nfc_digital_framing_nfcf */
328 { PORT100_IN_PROT_END, 0 },
330 [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = {
331 { PORT100_IN_PROT_END, 0 },
335 static struct port100_protocol
336 tg_protocols[][PORT100_TG_MAX_NUM_PROTOCOLS + 1] = {
337 [NFC_DIGITAL_FRAMING_NFCA_SHORT] = {
338 { PORT100_TG_PROT_END, 0 },
340 [NFC_DIGITAL_FRAMING_NFCA_STANDARD] = {
341 { PORT100_TG_PROT_END, 0 },
343 [NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A] = {
344 { PORT100_TG_PROT_END, 0 },
346 [NFC_DIGITAL_FRAMING_NFCA_T1T] = {
347 { PORT100_TG_PROT_END, 0 },
349 [NFC_DIGITAL_FRAMING_NFCA_T2T] = {
350 { PORT100_TG_PROT_END, 0 },
352 [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP] = {
353 { PORT100_TG_PROT_TU, 1 },
354 { PORT100_TG_PROT_RF_OFF, 0 },
355 { PORT100_TG_PROT_CRM, 7 },
356 { PORT100_TG_PROT_END, 0 },
358 [NFC_DIGITAL_FRAMING_NFCF] = {
359 { PORT100_TG_PROT_END, 0 },
361 [NFC_DIGITAL_FRAMING_NFCF_T3T] = {
362 { PORT100_TG_PROT_END, 0 },
364 [NFC_DIGITAL_FRAMING_NFCF_NFC_DEP] = {
365 { PORT100_TG_PROT_TU, 1 },
366 { PORT100_TG_PROT_RF_OFF, 0 },
367 { PORT100_TG_PROT_CRM, 7 },
368 { PORT100_TG_PROT_END, 0 },
370 [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = {
371 { PORT100_TG_PROT_RF_OFF, 1 },
372 { PORT100_TG_PROT_END, 0 },
377 struct nfc_digital_dev *nfc_digital_dev;
382 struct usb_device *udev;
383 struct usb_interface *interface;
388 struct work_struct cmd_complete_work;
392 /* The digital stack serializes commands to be sent. There is no need
393 * for any queuing/locking mechanism at driver level.
395 struct port100_cmd *cmd;
402 struct sk_buff *resp;
404 port100_send_async_complete_t complete_cb;
405 void *complete_cb_context;
408 struct port100_frame {
411 __be16 extended_frame;
417 struct port100_ack_frame {
424 struct port100_cb_arg {
425 nfc_digital_cmd_complete_t complete_cb;
430 struct port100_tg_comm_rf_cmd {
442 struct port100_tg_comm_rf_res {
450 /* The rule: value + checksum = 0 */
451 static inline u8 port100_checksum(u16 value)
453 return ~(((u8 *)&value)[0] + ((u8 *)&value)[1]) + 1;
456 /* The rule: sum(data elements) + checksum = 0 */
457 static u8 port100_data_checksum(u8 *data, int datalen)
462 for (i = 0; i < datalen; i++)
465 return port100_checksum(sum);
468 static void port100_tx_frame_init(void *_frame, u8 cmd_code)
470 struct port100_frame *frame = _frame;
473 frame->start_frame = cpu_to_be16(PORT100_FRAME_SOF);
474 frame->extended_frame = cpu_to_be16(PORT100_FRAME_EXT);
475 PORT100_FRAME_DIRECTION(frame) = PORT100_FRAME_DIR_OUT;
476 PORT100_FRAME_CMD(frame) = cmd_code;
477 frame->datalen = cpu_to_le16(2);
480 static void port100_tx_frame_finish(void *_frame)
482 struct port100_frame *frame = _frame;
484 frame->datalen_checksum = port100_checksum(le16_to_cpu(frame->datalen));
486 PORT100_FRAME_CHECKSUM(frame) =
487 port100_data_checksum(frame->data, le16_to_cpu(frame->datalen));
489 PORT100_FRAME_POSTAMBLE(frame) = 0;
492 static void port100_tx_update_payload_len(void *_frame, int len)
494 struct port100_frame *frame = _frame;
496 frame->datalen = cpu_to_le16(le16_to_cpu(frame->datalen) + len);
499 static bool port100_rx_frame_is_valid(void *_frame)
502 struct port100_frame *frame = _frame;
504 if (frame->start_frame != cpu_to_be16(PORT100_FRAME_SOF) ||
505 frame->extended_frame != cpu_to_be16(PORT100_FRAME_EXT))
508 checksum = port100_checksum(le16_to_cpu(frame->datalen));
509 if (checksum != frame->datalen_checksum)
512 checksum = port100_data_checksum(frame->data,
513 le16_to_cpu(frame->datalen));
514 if (checksum != PORT100_FRAME_CHECKSUM(frame))
520 static bool port100_rx_frame_is_ack(struct port100_ack_frame *frame)
522 return (frame->start_frame == cpu_to_be16(PORT100_FRAME_SOF) &&
523 frame->ack_frame == cpu_to_be16(PORT100_FRAME_ACK));
526 static inline int port100_rx_frame_size(void *frame)
528 struct port100_frame *f = frame;
530 return sizeof(struct port100_frame) + le16_to_cpu(f->datalen) +
531 PORT100_FRAME_TAIL_LEN;
534 static bool port100_rx_frame_is_cmd_response(struct port100 *dev, void *frame)
536 struct port100_frame *f = frame;
538 return (PORT100_FRAME_CMD(f) == PORT100_CMD_RESPONSE(dev->cmd->code));
541 static void port100_recv_response(struct urb *urb)
543 struct port100 *dev = urb->context;
544 struct port100_cmd *cmd = dev->cmd;
547 cmd->status = urb->status;
549 switch (urb->status) {
554 nfc_err(&dev->interface->dev,
555 "The urb has been canceled (status %d)", urb->status);
559 nfc_err(&dev->interface->dev, "Urb failure (status %d)",
564 in_frame = dev->in_urb->transfer_buffer;
566 if (!port100_rx_frame_is_valid(in_frame)) {
567 nfc_err(&dev->interface->dev, "Received an invalid frame");
572 print_hex_dump_debug("PORT100 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
573 port100_rx_frame_size(in_frame), false);
575 if (!port100_rx_frame_is_cmd_response(dev, in_frame)) {
576 nfc_err(&dev->interface->dev,
577 "It's not the response to the last command");
583 schedule_work(&dev->cmd_complete_work);
586 static int port100_submit_urb_for_response(struct port100 *dev, gfp_t flags)
588 dev->in_urb->complete = port100_recv_response;
590 return usb_submit_urb(dev->in_urb, flags);
593 static void port100_recv_ack(struct urb *urb)
595 struct port100 *dev = urb->context;
596 struct port100_cmd *cmd = dev->cmd;
597 struct port100_ack_frame *in_frame;
600 cmd->status = urb->status;
602 switch (urb->status) {
607 nfc_err(&dev->interface->dev,
608 "The urb has been stopped (status %d)", urb->status);
612 nfc_err(&dev->interface->dev, "Urb failure (status %d)",
617 in_frame = dev->in_urb->transfer_buffer;
619 if (!port100_rx_frame_is_ack(in_frame)) {
620 nfc_err(&dev->interface->dev, "Received an invalid ack");
625 rc = port100_submit_urb_for_response(dev, GFP_ATOMIC);
627 nfc_err(&dev->interface->dev,
628 "usb_submit_urb failed with result %d", rc);
636 schedule_work(&dev->cmd_complete_work);
639 static int port100_submit_urb_for_ack(struct port100 *dev, gfp_t flags)
641 dev->in_urb->complete = port100_recv_ack;
643 return usb_submit_urb(dev->in_urb, flags);
646 static int port100_send_ack(struct port100 *dev)
650 dev->out_urb->transfer_buffer = ack_frame;
651 dev->out_urb->transfer_buffer_length = sizeof(ack_frame);
652 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
657 static int port100_send_frame_async(struct port100 *dev, struct sk_buff *out,
658 struct sk_buff *in, int in_len)
662 dev->out_urb->transfer_buffer = out->data;
663 dev->out_urb->transfer_buffer_length = out->len;
665 dev->in_urb->transfer_buffer = in->data;
666 dev->in_urb->transfer_buffer_length = in_len;
668 print_hex_dump_debug("PORT100 TX: ", DUMP_PREFIX_NONE, 16, 1,
669 out->data, out->len, false);
671 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
675 rc = port100_submit_urb_for_ack(dev, GFP_KERNEL);
682 usb_unlink_urb(dev->out_urb);
686 static void port100_build_cmd_frame(struct port100 *dev, u8 cmd_code,
689 /* payload is already there, just update datalen */
690 int payload_len = skb->len;
692 skb_push(skb, PORT100_FRAME_HEADER_LEN);
693 skb_put(skb, PORT100_FRAME_TAIL_LEN);
695 port100_tx_frame_init(skb->data, cmd_code);
696 port100_tx_update_payload_len(skb->data, payload_len);
697 port100_tx_frame_finish(skb->data);
700 static void port100_send_async_complete(struct port100 *dev)
702 struct port100_cmd *cmd = dev->cmd;
703 int status = cmd->status;
705 struct sk_buff *req = cmd->req;
706 struct sk_buff *resp = cmd->resp;
713 cmd->complete_cb(dev, cmd->complete_cb_context,
719 skb_put(resp, port100_rx_frame_size(resp->data));
720 skb_pull(resp, PORT100_FRAME_HEADER_LEN);
721 skb_trim(resp, resp->len - PORT100_FRAME_TAIL_LEN);
723 cmd->complete_cb(dev, cmd->complete_cb_context, resp);
729 static int port100_send_cmd_async(struct port100 *dev, u8 cmd_code,
731 port100_send_async_complete_t complete_cb,
732 void *complete_cb_context)
734 struct port100_cmd *cmd;
735 struct sk_buff *resp;
737 int resp_len = PORT100_FRAME_HEADER_LEN +
738 PORT100_FRAME_MAX_PAYLOAD_LEN +
739 PORT100_FRAME_TAIL_LEN;
741 resp = alloc_skb(resp_len, GFP_KERNEL);
745 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
751 cmd->code = cmd_code;
754 cmd->resp_len = resp_len;
755 cmd->complete_cb = complete_cb;
756 cmd->complete_cb_context = complete_cb_context;
758 port100_build_cmd_frame(dev, cmd_code, req);
762 rc = port100_send_frame_async(dev, req, resp, resp_len);
772 struct port100_sync_cmd_response {
773 struct sk_buff *resp;
774 struct completion done;
777 static void port100_wq_cmd_complete(struct work_struct *work)
779 struct port100 *dev = container_of(work, struct port100,
782 port100_send_async_complete(dev);
785 static void port100_send_sync_complete(struct port100 *dev, void *_arg,
786 struct sk_buff *resp)
788 struct port100_sync_cmd_response *arg = _arg;
791 complete(&arg->done);
794 static struct sk_buff *port100_send_cmd_sync(struct port100 *dev, u8 cmd_code,
798 struct port100_sync_cmd_response arg;
800 init_completion(&arg.done);
802 rc = port100_send_cmd_async(dev, cmd_code, req,
803 port100_send_sync_complete, &arg);
809 wait_for_completion(&arg.done);
814 static void port100_send_complete(struct urb *urb)
816 struct port100 *dev = urb->context;
818 switch (urb->status) {
823 nfc_err(&dev->interface->dev,
824 "The urb has been stopped (status %d)", urb->status);
828 nfc_err(&dev->interface->dev, "Urb failure (status %d)",
833 static void port100_abort_cmd(struct nfc_digital_dev *ddev)
835 struct port100 *dev = nfc_digital_get_drvdata(ddev);
837 /* An ack will cancel the last issued command */
838 port100_send_ack(dev);
840 /* cancel the urb request */
841 usb_kill_urb(dev->in_urb);
844 static struct sk_buff *port100_alloc_skb(struct port100 *dev, unsigned int size)
848 skb = alloc_skb(dev->skb_headroom + dev->skb_tailroom + size,
851 skb_reserve(skb, dev->skb_headroom);
856 static int port100_set_command_type(struct port100 *dev, u8 command_type)
859 struct sk_buff *resp;
862 skb = port100_alloc_skb(dev, 1);
866 *skb_put(skb, sizeof(u8)) = command_type;
868 resp = port100_send_cmd_sync(dev, PORT100_CMD_SET_COMMAND_TYPE, skb);
870 return PTR_ERR(resp);
879 static u64 port100_get_command_type_mask(struct port100 *dev)
882 struct sk_buff *resp;
885 skb = port100_alloc_skb(dev, 0);
889 resp = port100_send_cmd_sync(dev, PORT100_CMD_GET_COMMAND_TYPE, skb);
891 return PTR_ERR(resp);
896 mask = be64_to_cpu(*(__be64 *)resp->data);
903 static u16 port100_get_firmware_version(struct port100 *dev)
906 struct sk_buff *resp;
909 skb = port100_alloc_skb(dev, 0);
913 resp = port100_send_cmd_sync(dev, PORT100_CMD_GET_FIRMWARE_VERSION,
918 fw_ver = le16_to_cpu(*(__le16 *)resp->data);
925 static int port100_switch_rf(struct nfc_digital_dev *ddev, bool on)
927 struct port100 *dev = nfc_digital_get_drvdata(ddev);
928 struct sk_buff *skb, *resp;
930 skb = port100_alloc_skb(dev, 1);
934 *skb_put(skb, 1) = on ? 1 : 0;
936 resp = port100_send_cmd_sync(dev, PORT100_CMD_SWITCH_RF, skb);
939 return PTR_ERR(resp);
946 static int port100_in_set_rf(struct nfc_digital_dev *ddev, u8 rf)
948 struct port100 *dev = nfc_digital_get_drvdata(ddev);
950 struct sk_buff *resp;
953 if (rf >= NFC_DIGITAL_RF_TECH_LAST)
956 skb = port100_alloc_skb(dev, sizeof(struct port100_in_rf_setting));
960 memcpy(skb_put(skb, sizeof(struct port100_in_rf_setting)),
962 sizeof(struct port100_in_rf_setting));
964 resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_RF, skb);
967 return PTR_ERR(resp);
976 static int port100_in_set_framing(struct nfc_digital_dev *ddev, int param)
978 struct port100 *dev = nfc_digital_get_drvdata(ddev);
979 struct port100_protocol *protocols;
981 struct sk_buff *resp;
986 if (param >= NFC_DIGITAL_FRAMING_LAST)
989 protocols = in_protocols[param];
992 while (protocols[num_protocols].number != PORT100_IN_PROT_END)
998 size = sizeof(struct port100_protocol) * num_protocols;
1000 skb = port100_alloc_skb(dev, size);
1004 memcpy(skb_put(skb, size), protocols, size);
1006 resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_PROTOCOL, skb);
1009 return PTR_ERR(resp);
1013 dev_kfree_skb(resp);
1018 static int port100_in_configure_hw(struct nfc_digital_dev *ddev, int type,
1021 if (type == NFC_DIGITAL_CONFIG_RF_TECH)
1022 return port100_in_set_rf(ddev, param);
1024 if (type == NFC_DIGITAL_CONFIG_FRAMING)
1025 return port100_in_set_framing(ddev, param);
1030 static void port100_in_comm_rf_complete(struct port100 *dev, void *arg,
1031 struct sk_buff *resp)
1033 struct port100_cb_arg *cb_arg = arg;
1034 nfc_digital_cmd_complete_t cb = cb_arg->complete_cb;
1043 if (resp->len < 4) {
1044 nfc_err(&dev->interface->dev,
1045 "Invalid packet length received.\n");
1050 status = le32_to_cpu(*(__le32 *)resp->data);
1052 skb_pull(resp, sizeof(u32));
1054 if (status == PORT100_CMD_STATUS_TIMEOUT) {
1059 if (status != PORT100_CMD_STATUS_OK) {
1060 nfc_err(&dev->interface->dev,
1061 "in_comm_rf failed with status 0x%08x\n", status);
1066 /* Remove collision bits byte */
1076 cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp);
1081 static int port100_in_send_cmd(struct nfc_digital_dev *ddev,
1082 struct sk_buff *skb, u16 _timeout,
1083 nfc_digital_cmd_complete_t cb, void *arg)
1085 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1086 struct port100_cb_arg *cb_arg;
1089 cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1093 cb_arg->complete_cb = cb;
1094 cb_arg->complete_arg = arg;
1096 timeout = cpu_to_le16(_timeout * 10);
1098 memcpy(skb_push(skb, sizeof(__le16)), &timeout, sizeof(__le16));
1100 return port100_send_cmd_async(dev, PORT100_CMD_IN_COMM_RF, skb,
1101 port100_in_comm_rf_complete, cb_arg);
1104 static int port100_tg_set_rf(struct nfc_digital_dev *ddev, u8 rf)
1106 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1107 struct sk_buff *skb;
1108 struct sk_buff *resp;
1111 if (rf >= NFC_DIGITAL_RF_TECH_LAST)
1114 skb = port100_alloc_skb(dev, sizeof(struct port100_tg_rf_setting));
1118 memcpy(skb_put(skb, sizeof(struct port100_tg_rf_setting)),
1119 &tg_rf_settings[rf],
1120 sizeof(struct port100_tg_rf_setting));
1122 resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_RF, skb);
1125 return PTR_ERR(resp);
1129 dev_kfree_skb(resp);
1134 static int port100_tg_set_framing(struct nfc_digital_dev *ddev, int param)
1136 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1137 struct port100_protocol *protocols;
1138 struct sk_buff *skb;
1139 struct sk_buff *resp;
1144 if (param >= NFC_DIGITAL_FRAMING_LAST)
1147 protocols = tg_protocols[param];
1150 while (protocols[num_protocols].number != PORT100_TG_PROT_END)
1156 size = sizeof(struct port100_protocol) * num_protocols;
1158 skb = port100_alloc_skb(dev, size);
1162 memcpy(skb_put(skb, size), protocols, size);
1164 resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_PROTOCOL, skb);
1167 return PTR_ERR(resp);
1171 dev_kfree_skb(resp);
1176 static int port100_tg_configure_hw(struct nfc_digital_dev *ddev, int type,
1179 if (type == NFC_DIGITAL_CONFIG_RF_TECH)
1180 return port100_tg_set_rf(ddev, param);
1182 if (type == NFC_DIGITAL_CONFIG_FRAMING)
1183 return port100_tg_set_framing(ddev, param);
1188 static bool port100_tg_target_activated(struct port100 *dev, u8 tgt_activated)
1192 switch (dev->cmd_type) {
1193 case PORT100_CMD_TYPE_0:
1194 mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK;
1196 case PORT100_CMD_TYPE_1:
1197 mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK |
1198 PORT100_MDAA_TGT_WAS_ACTIVATED_MASK;
1201 nfc_err(&dev->interface->dev, "Unknonwn command type.\n");
1205 return ((tgt_activated & mask) == mask);
1208 static void port100_tg_comm_rf_complete(struct port100 *dev, void *arg,
1209 struct sk_buff *resp)
1212 struct port100_cb_arg *cb_arg = arg;
1213 nfc_digital_cmd_complete_t cb = cb_arg->complete_cb;
1214 struct port100_tg_comm_rf_res *hdr;
1219 hdr = (struct port100_tg_comm_rf_res *)resp->data;
1221 status = le32_to_cpu(hdr->status);
1224 !port100_tg_target_activated(dev, hdr->target_activated)) {
1226 resp = ERR_PTR(-ETIMEDOUT);
1231 skb_pull(resp, sizeof(struct port100_tg_comm_rf_res));
1233 if (status != PORT100_CMD_STATUS_OK) {
1236 if (status == PORT100_CMD_STATUS_TIMEOUT)
1237 resp = ERR_PTR(-ETIMEDOUT);
1239 resp = ERR_PTR(-EIO);
1243 cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp);
1248 static int port100_tg_send_cmd(struct nfc_digital_dev *ddev,
1249 struct sk_buff *skb, u16 timeout,
1250 nfc_digital_cmd_complete_t cb, void *arg)
1252 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1253 struct port100_tg_comm_rf_cmd *hdr;
1254 struct port100_cb_arg *cb_arg;
1256 cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1260 cb_arg->complete_cb = cb;
1261 cb_arg->complete_arg = arg;
1263 skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd));
1265 hdr = (struct port100_tg_comm_rf_cmd *)skb->data;
1267 memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd));
1268 hdr->guard_time = cpu_to_le16(500);
1269 hdr->send_timeout = cpu_to_le16(0xFFFF);
1270 hdr->recv_timeout = cpu_to_le16(timeout);
1272 return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb,
1273 port100_tg_comm_rf_complete, cb_arg);
1276 static int port100_listen_mdaa(struct nfc_digital_dev *ddev,
1277 struct digital_tg_mdaa_params *params,
1279 nfc_digital_cmd_complete_t cb, void *arg)
1281 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1282 struct port100_tg_comm_rf_cmd *hdr;
1283 struct port100_cb_arg *cb_arg;
1284 struct sk_buff *skb;
1287 rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
1288 NFC_DIGITAL_RF_TECH_106A);
1292 rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1293 NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
1297 cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1301 cb_arg->complete_cb = cb;
1302 cb_arg->complete_arg = arg;
1305 skb = port100_alloc_skb(dev, 0);
1311 skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd));
1312 hdr = (struct port100_tg_comm_rf_cmd *)skb->data;
1314 memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd));
1316 hdr->guard_time = 0;
1317 hdr->send_timeout = cpu_to_le16(0xFFFF);
1319 hdr->nfca_param[0] = (params->sens_res >> 8) & 0xFF;
1320 hdr->nfca_param[1] = params->sens_res & 0xFF;
1321 memcpy(hdr->nfca_param + 2, params->nfcid1, 3);
1322 hdr->nfca_param[5] = params->sel_res;
1323 memcpy(hdr->nfcf_param, params->nfcid2, 8);
1324 hdr->nfcf_param[16] = (params->sc >> 8) & 0xFF;
1325 hdr->nfcf_param[17] = params->sc & 0xFF;
1326 hdr->recv_timeout = cpu_to_le16(timeout);
1328 return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb,
1329 port100_tg_comm_rf_complete, cb_arg);
1332 static int port100_listen(struct nfc_digital_dev *ddev, u16 timeout,
1333 nfc_digital_cmd_complete_t cb, void *arg)
1335 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1336 struct sk_buff *skb;
1338 skb = port100_alloc_skb(dev, 0);
1342 return port100_tg_send_cmd(ddev, skb, timeout, cb, arg);
1345 static struct nfc_digital_ops port100_digital_ops = {
1346 .in_configure_hw = port100_in_configure_hw,
1347 .in_send_cmd = port100_in_send_cmd,
1349 .tg_listen_mdaa = port100_listen_mdaa,
1350 .tg_listen = port100_listen,
1351 .tg_configure_hw = port100_tg_configure_hw,
1352 .tg_send_cmd = port100_tg_send_cmd,
1354 .switch_rf = port100_switch_rf,
1355 .abort_cmd = port100_abort_cmd,
1358 static const struct usb_device_id port100_table[] = {
1359 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
1360 .idVendor = SONY_VENDOR_ID,
1361 .idProduct = RCS380_PRODUCT_ID,
1365 MODULE_DEVICE_TABLE(usb, port100_table);
1367 static int port100_probe(struct usb_interface *interface,
1368 const struct usb_device_id *id)
1370 struct port100 *dev;
1372 struct usb_host_interface *iface_desc;
1373 struct usb_endpoint_descriptor *endpoint;
1380 dev = devm_kzalloc(&interface->dev, sizeof(struct port100), GFP_KERNEL);
1384 dev->udev = usb_get_dev(interface_to_usbdev(interface));
1385 dev->interface = interface;
1386 usb_set_intfdata(interface, dev);
1388 in_endpoint = out_endpoint = 0;
1389 iface_desc = interface->cur_altsetting;
1390 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1391 endpoint = &iface_desc->endpoint[i].desc;
1393 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
1394 in_endpoint = endpoint->bEndpointAddress;
1396 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
1397 out_endpoint = endpoint->bEndpointAddress;
1400 if (!in_endpoint || !out_endpoint) {
1401 nfc_err(&interface->dev,
1402 "Could not find bulk-in or bulk-out endpoint\n");
1407 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
1408 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
1410 if (!dev->in_urb || !dev->out_urb) {
1411 nfc_err(&interface->dev, "Could not allocate USB URBs\n");
1416 usb_fill_bulk_urb(dev->in_urb, dev->udev,
1417 usb_rcvbulkpipe(dev->udev, in_endpoint),
1418 NULL, 0, NULL, dev);
1419 usb_fill_bulk_urb(dev->out_urb, dev->udev,
1420 usb_sndbulkpipe(dev->udev, out_endpoint),
1421 NULL, 0, port100_send_complete, dev);
1423 dev->skb_headroom = PORT100_FRAME_HEADER_LEN +
1424 PORT100_COMM_RF_HEAD_MAX_LEN;
1425 dev->skb_tailroom = PORT100_FRAME_TAIL_LEN;
1427 INIT_WORK(&dev->cmd_complete_work, port100_wq_cmd_complete);
1429 /* The first thing to do with the Port-100 is to set the command type
1430 * to be used. If supported we use command type 1. 0 otherwise.
1432 cmd_type_mask = port100_get_command_type_mask(dev);
1433 if (!cmd_type_mask) {
1434 nfc_err(&interface->dev,
1435 "Could not get supported command types.\n");
1440 if (PORT100_CMD_TYPE_IS_SUPPORTED(cmd_type_mask, PORT100_CMD_TYPE_1))
1441 dev->cmd_type = PORT100_CMD_TYPE_1;
1443 dev->cmd_type = PORT100_CMD_TYPE_0;
1445 rc = port100_set_command_type(dev, dev->cmd_type);
1447 nfc_err(&interface->dev,
1448 "The device does not support command type %u.\n",
1453 fw_version = port100_get_firmware_version(dev);
1455 nfc_err(&interface->dev,
1456 "Could not get device firmware version.\n");
1458 nfc_info(&interface->dev,
1459 "Sony NFC Port-100 Series attached (firmware v%x.%02x)\n",
1460 (fw_version & 0xFF00) >> 8, fw_version & 0xFF);
1462 dev->nfc_digital_dev = nfc_digital_allocate_device(&port100_digital_ops,
1464 PORT100_CAPABILITIES,
1467 if (!dev->nfc_digital_dev) {
1468 nfc_err(&interface->dev,
1469 "Could not allocate nfc_digital_dev.\n");
1474 nfc_digital_set_parent_dev(dev->nfc_digital_dev, &interface->dev);
1475 nfc_digital_set_drvdata(dev->nfc_digital_dev, dev);
1477 rc = nfc_digital_register_device(dev->nfc_digital_dev);
1479 nfc_err(&interface->dev,
1480 "Could not register digital device.\n");
1487 nfc_digital_free_device(dev->nfc_digital_dev);
1490 usb_free_urb(dev->in_urb);
1491 usb_free_urb(dev->out_urb);
1492 usb_put_dev(dev->udev);
1497 static void port100_disconnect(struct usb_interface *interface)
1499 struct port100 *dev;
1501 dev = usb_get_intfdata(interface);
1502 usb_set_intfdata(interface, NULL);
1504 nfc_digital_unregister_device(dev->nfc_digital_dev);
1505 nfc_digital_free_device(dev->nfc_digital_dev);
1507 usb_kill_urb(dev->in_urb);
1508 usb_kill_urb(dev->out_urb);
1510 usb_free_urb(dev->in_urb);
1511 usb_free_urb(dev->out_urb);
1515 nfc_info(&interface->dev, "Sony Port-100 NFC device disconnected");
1518 static struct usb_driver port100_driver = {
1520 .probe = port100_probe,
1521 .disconnect = port100_disconnect,
1522 .id_table = port100_table,
1525 module_usb_driver(port100_driver);
1527 MODULE_DESCRIPTION("NFC Port-100 series usb driver ver " VERSION);
1528 MODULE_VERSION(VERSION);
1529 MODULE_LICENSE("GPL");