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 | \
31 NFC_PROTO_ISO14443_MASK)
33 #define PORT100_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC | \
34 NFC_DIGITAL_DRV_CAPS_TG_CRC)
36 /* Standard port100 frame definitions */
37 #define PORT100_FRAME_HEADER_LEN (sizeof(struct port100_frame) \
38 + 2) /* data[0] CC, data[1] SCC */
39 #define PORT100_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
41 #define PORT100_COMM_RF_HEAD_MAX_LEN (sizeof(struct port100_tg_comm_rf_cmd))
44 * Max extended frame payload len, excluding CC and SCC
45 * which are already in PORT100_FRAME_HEADER_LEN.
47 #define PORT100_FRAME_MAX_PAYLOAD_LEN 1001
49 #define PORT100_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
51 static u8 ack_frame[PORT100_FRAME_ACK_SIZE] = {
52 0x00, 0x00, 0xff, 0x00, 0xff, 0x00
55 #define PORT100_FRAME_CHECKSUM(f) (f->data[le16_to_cpu(f->datalen)])
56 #define PORT100_FRAME_POSTAMBLE(f) (f->data[le16_to_cpu(f->datalen) + 1])
59 #define PORT100_FRAME_SOF 0x00FF
60 #define PORT100_FRAME_EXT 0xFFFF
61 #define PORT100_FRAME_ACK 0x00FF
63 /* Port-100 command: in or out */
64 #define PORT100_FRAME_DIRECTION(f) (f->data[0]) /* CC */
65 #define PORT100_FRAME_DIR_OUT 0xD6
66 #define PORT100_FRAME_DIR_IN 0xD7
68 /* Port-100 sub-command */
69 #define PORT100_FRAME_CMD(f) (f->data[1]) /* SCC */
71 #define PORT100_CMD_GET_FIRMWARE_VERSION 0x20
72 #define PORT100_CMD_GET_COMMAND_TYPE 0x28
73 #define PORT100_CMD_SET_COMMAND_TYPE 0x2A
75 #define PORT100_CMD_IN_SET_RF 0x00
76 #define PORT100_CMD_IN_SET_PROTOCOL 0x02
77 #define PORT100_CMD_IN_COMM_RF 0x04
79 #define PORT100_CMD_TG_SET_RF 0x40
80 #define PORT100_CMD_TG_SET_PROTOCOL 0x42
81 #define PORT100_CMD_TG_SET_RF_OFF 0x46
82 #define PORT100_CMD_TG_COMM_RF 0x48
84 #define PORT100_CMD_SWITCH_RF 0x06
86 #define PORT100_CMD_RESPONSE(cmd) (cmd + 1)
88 #define PORT100_CMD_TYPE_IS_SUPPORTED(mask, cmd_type) \
89 ((mask) & (0x01 << (cmd_type)))
90 #define PORT100_CMD_TYPE_0 0
91 #define PORT100_CMD_TYPE_1 1
93 #define PORT100_CMD_STATUS_OK 0x00
94 #define PORT100_CMD_STATUS_TIMEOUT 0x80
96 #define PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK 0x01
97 #define PORT100_MDAA_TGT_WAS_ACTIVATED_MASK 0x02
101 typedef void (*port100_send_async_complete_t)(struct port100 *dev, void *arg,
102 struct sk_buff *resp);
105 * Setting sets structure for in_set_rf command
107 * @in_*_set_number: Represent the entry indexes in the port-100 RF Base Table.
108 * This table contains multiple RF setting sets required for RF
111 * @in_*_comm_type: Theses fields set the communication type to be used.
113 struct port100_in_rf_setting {
114 u8 in_send_set_number;
115 u8 in_send_comm_type;
116 u8 in_recv_set_number;
117 u8 in_recv_comm_type;
120 #define PORT100_COMM_TYPE_IN_212F 0x01
121 #define PORT100_COMM_TYPE_IN_424F 0x02
122 #define PORT100_COMM_TYPE_IN_106A 0x03
124 static const struct port100_in_rf_setting in_rf_settings[] = {
125 [NFC_DIGITAL_RF_TECH_212F] = {
126 .in_send_set_number = 1,
127 .in_send_comm_type = PORT100_COMM_TYPE_IN_212F,
128 .in_recv_set_number = 15,
129 .in_recv_comm_type = PORT100_COMM_TYPE_IN_212F,
131 [NFC_DIGITAL_RF_TECH_424F] = {
132 .in_send_set_number = 1,
133 .in_send_comm_type = PORT100_COMM_TYPE_IN_424F,
134 .in_recv_set_number = 15,
135 .in_recv_comm_type = PORT100_COMM_TYPE_IN_424F,
137 [NFC_DIGITAL_RF_TECH_106A] = {
138 .in_send_set_number = 2,
139 .in_send_comm_type = PORT100_COMM_TYPE_IN_106A,
140 .in_recv_set_number = 15,
141 .in_recv_comm_type = PORT100_COMM_TYPE_IN_106A,
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_END, 0 },
340 [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = {
341 { PORT100_IN_PROT_END, 0 },
343 /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
344 [NFC_DIGITAL_FRAMING_LAST] = {
345 { PORT100_IN_PROT_END, 0 },
349 static struct port100_protocol
350 tg_protocols[][PORT100_TG_MAX_NUM_PROTOCOLS + 1] = {
351 [NFC_DIGITAL_FRAMING_NFCA_SHORT] = {
352 { PORT100_TG_PROT_END, 0 },
354 [NFC_DIGITAL_FRAMING_NFCA_STANDARD] = {
355 { PORT100_TG_PROT_END, 0 },
357 [NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A] = {
358 { PORT100_TG_PROT_END, 0 },
360 [NFC_DIGITAL_FRAMING_NFCA_T1T] = {
361 { PORT100_TG_PROT_END, 0 },
363 [NFC_DIGITAL_FRAMING_NFCA_T2T] = {
364 { PORT100_TG_PROT_END, 0 },
366 [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP] = {
367 { PORT100_TG_PROT_TU, 1 },
368 { PORT100_TG_PROT_RF_OFF, 0 },
369 { PORT100_TG_PROT_CRM, 7 },
370 { PORT100_TG_PROT_END, 0 },
372 [NFC_DIGITAL_FRAMING_NFCF] = {
373 { PORT100_TG_PROT_END, 0 },
375 [NFC_DIGITAL_FRAMING_NFCF_T3T] = {
376 { PORT100_TG_PROT_END, 0 },
378 [NFC_DIGITAL_FRAMING_NFCF_NFC_DEP] = {
379 { PORT100_TG_PROT_TU, 1 },
380 { PORT100_TG_PROT_RF_OFF, 0 },
381 { PORT100_TG_PROT_CRM, 7 },
382 { PORT100_TG_PROT_END, 0 },
384 [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = {
385 { PORT100_TG_PROT_RF_OFF, 1 },
386 { PORT100_TG_PROT_END, 0 },
388 /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
389 [NFC_DIGITAL_FRAMING_LAST] = {
390 { PORT100_TG_PROT_END, 0 },
395 struct nfc_digital_dev *nfc_digital_dev;
400 struct usb_device *udev;
401 struct usb_interface *interface;
406 struct work_struct cmd_complete_work;
410 /* The digital stack serializes commands to be sent. There is no need
411 * for any queuing/locking mechanism at driver level.
413 struct port100_cmd *cmd;
420 struct sk_buff *resp;
422 port100_send_async_complete_t complete_cb;
423 void *complete_cb_context;
426 struct port100_frame {
429 __be16 extended_frame;
435 struct port100_ack_frame {
442 struct port100_cb_arg {
443 nfc_digital_cmd_complete_t complete_cb;
448 struct port100_tg_comm_rf_cmd {
460 struct port100_tg_comm_rf_res {
468 /* The rule: value + checksum = 0 */
469 static inline u8 port100_checksum(u16 value)
471 return ~(((u8 *)&value)[0] + ((u8 *)&value)[1]) + 1;
474 /* The rule: sum(data elements) + checksum = 0 */
475 static u8 port100_data_checksum(u8 *data, int datalen)
480 for (i = 0; i < datalen; i++)
483 return port100_checksum(sum);
486 static void port100_tx_frame_init(void *_frame, u8 cmd_code)
488 struct port100_frame *frame = _frame;
491 frame->start_frame = cpu_to_be16(PORT100_FRAME_SOF);
492 frame->extended_frame = cpu_to_be16(PORT100_FRAME_EXT);
493 PORT100_FRAME_DIRECTION(frame) = PORT100_FRAME_DIR_OUT;
494 PORT100_FRAME_CMD(frame) = cmd_code;
495 frame->datalen = cpu_to_le16(2);
498 static void port100_tx_frame_finish(void *_frame)
500 struct port100_frame *frame = _frame;
502 frame->datalen_checksum = port100_checksum(le16_to_cpu(frame->datalen));
504 PORT100_FRAME_CHECKSUM(frame) =
505 port100_data_checksum(frame->data, le16_to_cpu(frame->datalen));
507 PORT100_FRAME_POSTAMBLE(frame) = 0;
510 static void port100_tx_update_payload_len(void *_frame, int len)
512 struct port100_frame *frame = _frame;
514 frame->datalen = cpu_to_le16(le16_to_cpu(frame->datalen) + len);
517 static bool port100_rx_frame_is_valid(void *_frame)
520 struct port100_frame *frame = _frame;
522 if (frame->start_frame != cpu_to_be16(PORT100_FRAME_SOF) ||
523 frame->extended_frame != cpu_to_be16(PORT100_FRAME_EXT))
526 checksum = port100_checksum(le16_to_cpu(frame->datalen));
527 if (checksum != frame->datalen_checksum)
530 checksum = port100_data_checksum(frame->data,
531 le16_to_cpu(frame->datalen));
532 if (checksum != PORT100_FRAME_CHECKSUM(frame))
538 static bool port100_rx_frame_is_ack(struct port100_ack_frame *frame)
540 return (frame->start_frame == cpu_to_be16(PORT100_FRAME_SOF) &&
541 frame->ack_frame == cpu_to_be16(PORT100_FRAME_ACK));
544 static inline int port100_rx_frame_size(void *frame)
546 struct port100_frame *f = frame;
548 return sizeof(struct port100_frame) + le16_to_cpu(f->datalen) +
549 PORT100_FRAME_TAIL_LEN;
552 static bool port100_rx_frame_is_cmd_response(struct port100 *dev, void *frame)
554 struct port100_frame *f = frame;
556 return (PORT100_FRAME_CMD(f) == PORT100_CMD_RESPONSE(dev->cmd->code));
559 static void port100_recv_response(struct urb *urb)
561 struct port100 *dev = urb->context;
562 struct port100_cmd *cmd = dev->cmd;
565 cmd->status = urb->status;
567 switch (urb->status) {
572 nfc_err(&dev->interface->dev,
573 "The urb has been canceled (status %d)", urb->status);
577 nfc_err(&dev->interface->dev, "Urb failure (status %d)",
582 in_frame = dev->in_urb->transfer_buffer;
584 if (!port100_rx_frame_is_valid(in_frame)) {
585 nfc_err(&dev->interface->dev, "Received an invalid frame");
590 print_hex_dump_debug("PORT100 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
591 port100_rx_frame_size(in_frame), false);
593 if (!port100_rx_frame_is_cmd_response(dev, in_frame)) {
594 nfc_err(&dev->interface->dev,
595 "It's not the response to the last command");
601 schedule_work(&dev->cmd_complete_work);
604 static int port100_submit_urb_for_response(struct port100 *dev, gfp_t flags)
606 dev->in_urb->complete = port100_recv_response;
608 return usb_submit_urb(dev->in_urb, flags);
611 static void port100_recv_ack(struct urb *urb)
613 struct port100 *dev = urb->context;
614 struct port100_cmd *cmd = dev->cmd;
615 struct port100_ack_frame *in_frame;
618 cmd->status = urb->status;
620 switch (urb->status) {
625 nfc_err(&dev->interface->dev,
626 "The urb has been stopped (status %d)", urb->status);
630 nfc_err(&dev->interface->dev, "Urb failure (status %d)",
635 in_frame = dev->in_urb->transfer_buffer;
637 if (!port100_rx_frame_is_ack(in_frame)) {
638 nfc_err(&dev->interface->dev, "Received an invalid ack");
643 rc = port100_submit_urb_for_response(dev, GFP_ATOMIC);
645 nfc_err(&dev->interface->dev,
646 "usb_submit_urb failed with result %d", rc);
654 schedule_work(&dev->cmd_complete_work);
657 static int port100_submit_urb_for_ack(struct port100 *dev, gfp_t flags)
659 dev->in_urb->complete = port100_recv_ack;
661 return usb_submit_urb(dev->in_urb, flags);
664 static int port100_send_ack(struct port100 *dev)
668 dev->out_urb->transfer_buffer = ack_frame;
669 dev->out_urb->transfer_buffer_length = sizeof(ack_frame);
670 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
675 static int port100_send_frame_async(struct port100 *dev, struct sk_buff *out,
676 struct sk_buff *in, int in_len)
680 dev->out_urb->transfer_buffer = out->data;
681 dev->out_urb->transfer_buffer_length = out->len;
683 dev->in_urb->transfer_buffer = in->data;
684 dev->in_urb->transfer_buffer_length = in_len;
686 print_hex_dump_debug("PORT100 TX: ", DUMP_PREFIX_NONE, 16, 1,
687 out->data, out->len, false);
689 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
693 rc = port100_submit_urb_for_ack(dev, GFP_KERNEL);
700 usb_unlink_urb(dev->out_urb);
704 static void port100_build_cmd_frame(struct port100 *dev, u8 cmd_code,
707 /* payload is already there, just update datalen */
708 int payload_len = skb->len;
710 skb_push(skb, PORT100_FRAME_HEADER_LEN);
711 skb_put(skb, PORT100_FRAME_TAIL_LEN);
713 port100_tx_frame_init(skb->data, cmd_code);
714 port100_tx_update_payload_len(skb->data, payload_len);
715 port100_tx_frame_finish(skb->data);
718 static void port100_send_async_complete(struct port100 *dev)
720 struct port100_cmd *cmd = dev->cmd;
721 int status = cmd->status;
723 struct sk_buff *req = cmd->req;
724 struct sk_buff *resp = cmd->resp;
731 cmd->complete_cb(dev, cmd->complete_cb_context,
737 skb_put(resp, port100_rx_frame_size(resp->data));
738 skb_pull(resp, PORT100_FRAME_HEADER_LEN);
739 skb_trim(resp, resp->len - PORT100_FRAME_TAIL_LEN);
741 cmd->complete_cb(dev, cmd->complete_cb_context, resp);
747 static int port100_send_cmd_async(struct port100 *dev, u8 cmd_code,
749 port100_send_async_complete_t complete_cb,
750 void *complete_cb_context)
752 struct port100_cmd *cmd;
753 struct sk_buff *resp;
755 int resp_len = PORT100_FRAME_HEADER_LEN +
756 PORT100_FRAME_MAX_PAYLOAD_LEN +
757 PORT100_FRAME_TAIL_LEN;
759 resp = alloc_skb(resp_len, GFP_KERNEL);
763 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
769 cmd->code = cmd_code;
772 cmd->resp_len = resp_len;
773 cmd->complete_cb = complete_cb;
774 cmd->complete_cb_context = complete_cb_context;
776 port100_build_cmd_frame(dev, cmd_code, req);
780 rc = port100_send_frame_async(dev, req, resp, resp_len);
790 struct port100_sync_cmd_response {
791 struct sk_buff *resp;
792 struct completion done;
795 static void port100_wq_cmd_complete(struct work_struct *work)
797 struct port100 *dev = container_of(work, struct port100,
800 port100_send_async_complete(dev);
803 static void port100_send_sync_complete(struct port100 *dev, void *_arg,
804 struct sk_buff *resp)
806 struct port100_sync_cmd_response *arg = _arg;
809 complete(&arg->done);
812 static struct sk_buff *port100_send_cmd_sync(struct port100 *dev, u8 cmd_code,
816 struct port100_sync_cmd_response arg;
818 init_completion(&arg.done);
820 rc = port100_send_cmd_async(dev, cmd_code, req,
821 port100_send_sync_complete, &arg);
827 wait_for_completion(&arg.done);
832 static void port100_send_complete(struct urb *urb)
834 struct port100 *dev = urb->context;
836 switch (urb->status) {
841 nfc_err(&dev->interface->dev,
842 "The urb has been stopped (status %d)", urb->status);
846 nfc_err(&dev->interface->dev, "Urb failure (status %d)",
851 static void port100_abort_cmd(struct nfc_digital_dev *ddev)
853 struct port100 *dev = nfc_digital_get_drvdata(ddev);
855 /* An ack will cancel the last issued command */
856 port100_send_ack(dev);
858 /* cancel the urb request */
859 usb_kill_urb(dev->in_urb);
862 static struct sk_buff *port100_alloc_skb(struct port100 *dev, unsigned int size)
866 skb = alloc_skb(dev->skb_headroom + dev->skb_tailroom + size,
869 skb_reserve(skb, dev->skb_headroom);
874 static int port100_set_command_type(struct port100 *dev, u8 command_type)
877 struct sk_buff *resp;
880 skb = port100_alloc_skb(dev, 1);
884 *skb_put(skb, sizeof(u8)) = command_type;
886 resp = port100_send_cmd_sync(dev, PORT100_CMD_SET_COMMAND_TYPE, skb);
888 return PTR_ERR(resp);
897 static u64 port100_get_command_type_mask(struct port100 *dev)
900 struct sk_buff *resp;
903 skb = port100_alloc_skb(dev, 0);
907 resp = port100_send_cmd_sync(dev, PORT100_CMD_GET_COMMAND_TYPE, skb);
909 return PTR_ERR(resp);
914 mask = be64_to_cpu(*(__be64 *)resp->data);
921 static u16 port100_get_firmware_version(struct port100 *dev)
924 struct sk_buff *resp;
927 skb = port100_alloc_skb(dev, 0);
931 resp = port100_send_cmd_sync(dev, PORT100_CMD_GET_FIRMWARE_VERSION,
936 fw_ver = le16_to_cpu(*(__le16 *)resp->data);
943 static int port100_switch_rf(struct nfc_digital_dev *ddev, bool on)
945 struct port100 *dev = nfc_digital_get_drvdata(ddev);
946 struct sk_buff *skb, *resp;
948 skb = port100_alloc_skb(dev, 1);
952 *skb_put(skb, 1) = on ? 1 : 0;
954 resp = port100_send_cmd_sync(dev, PORT100_CMD_SWITCH_RF, skb);
957 return PTR_ERR(resp);
964 static int port100_in_set_rf(struct nfc_digital_dev *ddev, u8 rf)
966 struct port100 *dev = nfc_digital_get_drvdata(ddev);
968 struct sk_buff *resp;
971 if (rf >= NFC_DIGITAL_RF_TECH_LAST)
974 skb = port100_alloc_skb(dev, sizeof(struct port100_in_rf_setting));
978 memcpy(skb_put(skb, sizeof(struct port100_in_rf_setting)),
980 sizeof(struct port100_in_rf_setting));
982 resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_RF, skb);
985 return PTR_ERR(resp);
994 static int port100_in_set_framing(struct nfc_digital_dev *ddev, int param)
996 struct port100 *dev = nfc_digital_get_drvdata(ddev);
997 struct port100_protocol *protocols;
999 struct sk_buff *resp;
1004 if (param >= NFC_DIGITAL_FRAMING_LAST)
1007 protocols = in_protocols[param];
1010 while (protocols[num_protocols].number != PORT100_IN_PROT_END)
1016 size = sizeof(struct port100_protocol) * num_protocols;
1018 skb = port100_alloc_skb(dev, size);
1022 memcpy(skb_put(skb, size), protocols, size);
1024 resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_PROTOCOL, skb);
1027 return PTR_ERR(resp);
1031 dev_kfree_skb(resp);
1036 static int port100_in_configure_hw(struct nfc_digital_dev *ddev, int type,
1039 if (type == NFC_DIGITAL_CONFIG_RF_TECH)
1040 return port100_in_set_rf(ddev, param);
1042 if (type == NFC_DIGITAL_CONFIG_FRAMING)
1043 return port100_in_set_framing(ddev, param);
1048 static void port100_in_comm_rf_complete(struct port100 *dev, void *arg,
1049 struct sk_buff *resp)
1051 struct port100_cb_arg *cb_arg = arg;
1052 nfc_digital_cmd_complete_t cb = cb_arg->complete_cb;
1061 if (resp->len < 4) {
1062 nfc_err(&dev->interface->dev,
1063 "Invalid packet length received.\n");
1068 status = le32_to_cpu(*(__le32 *)resp->data);
1070 skb_pull(resp, sizeof(u32));
1072 if (status == PORT100_CMD_STATUS_TIMEOUT) {
1077 if (status != PORT100_CMD_STATUS_OK) {
1078 nfc_err(&dev->interface->dev,
1079 "in_comm_rf failed with status 0x%08x\n", status);
1084 /* Remove collision bits byte */
1094 cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp);
1099 static int port100_in_send_cmd(struct nfc_digital_dev *ddev,
1100 struct sk_buff *skb, u16 _timeout,
1101 nfc_digital_cmd_complete_t cb, void *arg)
1103 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1104 struct port100_cb_arg *cb_arg;
1107 cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1111 cb_arg->complete_cb = cb;
1112 cb_arg->complete_arg = arg;
1114 timeout = cpu_to_le16(_timeout * 10);
1116 memcpy(skb_push(skb, sizeof(__le16)), &timeout, sizeof(__le16));
1118 return port100_send_cmd_async(dev, PORT100_CMD_IN_COMM_RF, skb,
1119 port100_in_comm_rf_complete, cb_arg);
1122 static int port100_tg_set_rf(struct nfc_digital_dev *ddev, u8 rf)
1124 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1125 struct sk_buff *skb;
1126 struct sk_buff *resp;
1129 if (rf >= NFC_DIGITAL_RF_TECH_LAST)
1132 skb = port100_alloc_skb(dev, sizeof(struct port100_tg_rf_setting));
1136 memcpy(skb_put(skb, sizeof(struct port100_tg_rf_setting)),
1137 &tg_rf_settings[rf],
1138 sizeof(struct port100_tg_rf_setting));
1140 resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_RF, skb);
1143 return PTR_ERR(resp);
1147 dev_kfree_skb(resp);
1152 static int port100_tg_set_framing(struct nfc_digital_dev *ddev, int param)
1154 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1155 struct port100_protocol *protocols;
1156 struct sk_buff *skb;
1157 struct sk_buff *resp;
1162 if (param >= NFC_DIGITAL_FRAMING_LAST)
1165 protocols = tg_protocols[param];
1168 while (protocols[num_protocols].number != PORT100_TG_PROT_END)
1174 size = sizeof(struct port100_protocol) * num_protocols;
1176 skb = port100_alloc_skb(dev, size);
1180 memcpy(skb_put(skb, size), protocols, size);
1182 resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_PROTOCOL, skb);
1185 return PTR_ERR(resp);
1189 dev_kfree_skb(resp);
1194 static int port100_tg_configure_hw(struct nfc_digital_dev *ddev, int type,
1197 if (type == NFC_DIGITAL_CONFIG_RF_TECH)
1198 return port100_tg_set_rf(ddev, param);
1200 if (type == NFC_DIGITAL_CONFIG_FRAMING)
1201 return port100_tg_set_framing(ddev, param);
1206 static bool port100_tg_target_activated(struct port100 *dev, u8 tgt_activated)
1210 switch (dev->cmd_type) {
1211 case PORT100_CMD_TYPE_0:
1212 mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK;
1214 case PORT100_CMD_TYPE_1:
1215 mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK |
1216 PORT100_MDAA_TGT_WAS_ACTIVATED_MASK;
1219 nfc_err(&dev->interface->dev, "Unknonwn command type.\n");
1223 return ((tgt_activated & mask) == mask);
1226 static void port100_tg_comm_rf_complete(struct port100 *dev, void *arg,
1227 struct sk_buff *resp)
1230 struct port100_cb_arg *cb_arg = arg;
1231 nfc_digital_cmd_complete_t cb = cb_arg->complete_cb;
1232 struct port100_tg_comm_rf_res *hdr;
1237 hdr = (struct port100_tg_comm_rf_res *)resp->data;
1239 status = le32_to_cpu(hdr->status);
1242 !port100_tg_target_activated(dev, hdr->target_activated)) {
1244 resp = ERR_PTR(-ETIMEDOUT);
1249 skb_pull(resp, sizeof(struct port100_tg_comm_rf_res));
1251 if (status != PORT100_CMD_STATUS_OK) {
1254 if (status == PORT100_CMD_STATUS_TIMEOUT)
1255 resp = ERR_PTR(-ETIMEDOUT);
1257 resp = ERR_PTR(-EIO);
1261 cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp);
1266 static int port100_tg_send_cmd(struct nfc_digital_dev *ddev,
1267 struct sk_buff *skb, u16 timeout,
1268 nfc_digital_cmd_complete_t cb, void *arg)
1270 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1271 struct port100_tg_comm_rf_cmd *hdr;
1272 struct port100_cb_arg *cb_arg;
1274 cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1278 cb_arg->complete_cb = cb;
1279 cb_arg->complete_arg = arg;
1281 skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd));
1283 hdr = (struct port100_tg_comm_rf_cmd *)skb->data;
1285 memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd));
1286 hdr->guard_time = cpu_to_le16(500);
1287 hdr->send_timeout = cpu_to_le16(0xFFFF);
1288 hdr->recv_timeout = cpu_to_le16(timeout);
1290 return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb,
1291 port100_tg_comm_rf_complete, cb_arg);
1294 static int port100_listen_mdaa(struct nfc_digital_dev *ddev,
1295 struct digital_tg_mdaa_params *params,
1297 nfc_digital_cmd_complete_t cb, void *arg)
1299 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1300 struct port100_tg_comm_rf_cmd *hdr;
1301 struct port100_cb_arg *cb_arg;
1302 struct sk_buff *skb;
1305 rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
1306 NFC_DIGITAL_RF_TECH_106A);
1310 rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1311 NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
1315 cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1319 cb_arg->complete_cb = cb;
1320 cb_arg->complete_arg = arg;
1323 skb = port100_alloc_skb(dev, 0);
1329 skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd));
1330 hdr = (struct port100_tg_comm_rf_cmd *)skb->data;
1332 memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd));
1334 hdr->guard_time = 0;
1335 hdr->send_timeout = cpu_to_le16(0xFFFF);
1337 hdr->nfca_param[0] = (params->sens_res >> 8) & 0xFF;
1338 hdr->nfca_param[1] = params->sens_res & 0xFF;
1339 memcpy(hdr->nfca_param + 2, params->nfcid1, 3);
1340 hdr->nfca_param[5] = params->sel_res;
1341 memcpy(hdr->nfcf_param, params->nfcid2, 8);
1342 hdr->nfcf_param[16] = (params->sc >> 8) & 0xFF;
1343 hdr->nfcf_param[17] = params->sc & 0xFF;
1344 hdr->recv_timeout = cpu_to_le16(timeout);
1346 return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb,
1347 port100_tg_comm_rf_complete, cb_arg);
1350 static int port100_listen(struct nfc_digital_dev *ddev, u16 timeout,
1351 nfc_digital_cmd_complete_t cb, void *arg)
1353 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1354 struct sk_buff *skb;
1356 skb = port100_alloc_skb(dev, 0);
1360 return port100_tg_send_cmd(ddev, skb, timeout, cb, arg);
1363 static struct nfc_digital_ops port100_digital_ops = {
1364 .in_configure_hw = port100_in_configure_hw,
1365 .in_send_cmd = port100_in_send_cmd,
1367 .tg_listen_mdaa = port100_listen_mdaa,
1368 .tg_listen = port100_listen,
1369 .tg_configure_hw = port100_tg_configure_hw,
1370 .tg_send_cmd = port100_tg_send_cmd,
1372 .switch_rf = port100_switch_rf,
1373 .abort_cmd = port100_abort_cmd,
1376 static const struct usb_device_id port100_table[] = {
1377 { USB_DEVICE(SONY_VENDOR_ID, RCS380_PRODUCT_ID), },
1380 MODULE_DEVICE_TABLE(usb, port100_table);
1382 static int port100_probe(struct usb_interface *interface,
1383 const struct usb_device_id *id)
1385 struct port100 *dev;
1387 struct usb_host_interface *iface_desc;
1388 struct usb_endpoint_descriptor *endpoint;
1395 dev = devm_kzalloc(&interface->dev, sizeof(struct port100), GFP_KERNEL);
1399 dev->udev = usb_get_dev(interface_to_usbdev(interface));
1400 dev->interface = interface;
1401 usb_set_intfdata(interface, dev);
1403 in_endpoint = out_endpoint = 0;
1404 iface_desc = interface->cur_altsetting;
1405 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1406 endpoint = &iface_desc->endpoint[i].desc;
1408 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
1409 in_endpoint = endpoint->bEndpointAddress;
1411 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
1412 out_endpoint = endpoint->bEndpointAddress;
1415 if (!in_endpoint || !out_endpoint) {
1416 nfc_err(&interface->dev,
1417 "Could not find bulk-in or bulk-out endpoint\n");
1422 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
1423 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
1425 if (!dev->in_urb || !dev->out_urb) {
1426 nfc_err(&interface->dev, "Could not allocate USB URBs\n");
1431 usb_fill_bulk_urb(dev->in_urb, dev->udev,
1432 usb_rcvbulkpipe(dev->udev, in_endpoint),
1433 NULL, 0, NULL, dev);
1434 usb_fill_bulk_urb(dev->out_urb, dev->udev,
1435 usb_sndbulkpipe(dev->udev, out_endpoint),
1436 NULL, 0, port100_send_complete, dev);
1438 dev->skb_headroom = PORT100_FRAME_HEADER_LEN +
1439 PORT100_COMM_RF_HEAD_MAX_LEN;
1440 dev->skb_tailroom = PORT100_FRAME_TAIL_LEN;
1442 INIT_WORK(&dev->cmd_complete_work, port100_wq_cmd_complete);
1444 /* The first thing to do with the Port-100 is to set the command type
1445 * to be used. If supported we use command type 1. 0 otherwise.
1447 cmd_type_mask = port100_get_command_type_mask(dev);
1448 if (!cmd_type_mask) {
1449 nfc_err(&interface->dev,
1450 "Could not get supported command types.\n");
1455 if (PORT100_CMD_TYPE_IS_SUPPORTED(cmd_type_mask, PORT100_CMD_TYPE_1))
1456 dev->cmd_type = PORT100_CMD_TYPE_1;
1458 dev->cmd_type = PORT100_CMD_TYPE_0;
1460 rc = port100_set_command_type(dev, dev->cmd_type);
1462 nfc_err(&interface->dev,
1463 "The device does not support command type %u.\n",
1468 fw_version = port100_get_firmware_version(dev);
1470 nfc_err(&interface->dev,
1471 "Could not get device firmware version.\n");
1473 nfc_info(&interface->dev,
1474 "Sony NFC Port-100 Series attached (firmware v%x.%02x)\n",
1475 (fw_version & 0xFF00) >> 8, fw_version & 0xFF);
1477 dev->nfc_digital_dev = nfc_digital_allocate_device(&port100_digital_ops,
1479 PORT100_CAPABILITIES,
1482 if (!dev->nfc_digital_dev) {
1483 nfc_err(&interface->dev,
1484 "Could not allocate nfc_digital_dev.\n");
1489 nfc_digital_set_parent_dev(dev->nfc_digital_dev, &interface->dev);
1490 nfc_digital_set_drvdata(dev->nfc_digital_dev, dev);
1492 rc = nfc_digital_register_device(dev->nfc_digital_dev);
1494 nfc_err(&interface->dev,
1495 "Could not register digital device.\n");
1502 nfc_digital_free_device(dev->nfc_digital_dev);
1505 usb_free_urb(dev->in_urb);
1506 usb_free_urb(dev->out_urb);
1507 usb_put_dev(dev->udev);
1512 static void port100_disconnect(struct usb_interface *interface)
1514 struct port100 *dev;
1516 dev = usb_get_intfdata(interface);
1517 usb_set_intfdata(interface, NULL);
1519 nfc_digital_unregister_device(dev->nfc_digital_dev);
1520 nfc_digital_free_device(dev->nfc_digital_dev);
1522 usb_kill_urb(dev->in_urb);
1523 usb_kill_urb(dev->out_urb);
1525 usb_free_urb(dev->in_urb);
1526 usb_free_urb(dev->out_urb);
1527 usb_put_dev(dev->udev);
1531 nfc_info(&interface->dev, "Sony Port-100 NFC device disconnected");
1534 static struct usb_driver port100_driver = {
1536 .probe = port100_probe,
1537 .disconnect = port100_disconnect,
1538 .id_table = port100_table,
1541 module_usb_driver(port100_driver);
1543 MODULE_DESCRIPTION("NFC Port-100 series usb driver ver " VERSION);
1544 MODULE_VERSION(VERSION);
1545 MODULE_LICENSE("GPL");