Commit 50c2f5e8 authored by John W. Linville's avatar John W. Linville

Merge tag 'nfc-next-3.9-1' of git://git.kernel.org/pub/scm/linux/kernel/git/sameo/nfc-next

Samuel Ortiz <sameo@linux.intel.com> says:

"This is the first NFC patchset targeted at the 3.9 merge window.

It brings the following goodies:

- LLCP socket timestamping (To be used e.g with the recently released nfctool
  application for a more efficient skb timestamping when sniffing).
- A pretty big pn533 rework from Waldemar, preparing the driver to support
  more flavours of pn533 based devices.
- HCI changes from Eric in preparation for the microread driver support.
- Some LLCP memory leak fixes, cleanups and slight improvements.
- pn544 and nfcwilink move to the devm_kzalloc API.
- An initial Secure Element (SE) API.
- An nfc.h license change from the original author, allowing non GPL
  application code to safely include it."
Signed-off-by: default avatarJohn W. Linville <linville@tuxdriver.com>
parents 990debe2 d3710e74
...@@ -17,10 +17,12 @@ HCI ...@@ -17,10 +17,12 @@ HCI
HCI registers as an nfc device with NFC Core. Requests coming from userspace are HCI registers as an nfc device with NFC Core. Requests coming from userspace are
routed through netlink sockets to NFC Core and then to HCI. From this point, routed through netlink sockets to NFC Core and then to HCI. From this point,
they are translated in a sequence of HCI commands sent to the HCI layer in the they are translated in a sequence of HCI commands sent to the HCI layer in the
host controller (the chip). The sending context blocks while waiting for the host controller (the chip). Commands can be executed synchronously (the sending
response to arrive. context blocks waiting for response) or asynchronously (the response is returned
from HCI Rx context).
HCI events can also be received from the host controller. They will be handled HCI events can also be received from the host controller. They will be handled
and a translation will be forwarded to NFC Core as needed. and a translation will be forwarded to NFC Core as needed. There are hooks to
let the HCI driver handle proprietary events or override standard behavior.
HCI uses 2 execution contexts: HCI uses 2 execution contexts:
- one for executing commands : nfc_hci_msg_tx_work(). Only one command - one for executing commands : nfc_hci_msg_tx_work(). Only one command
can be executing at any given moment. can be executing at any given moment.
...@@ -33,6 +35,8 @@ The Session initialization is an HCI standard which must unfortunately ...@@ -33,6 +35,8 @@ The Session initialization is an HCI standard which must unfortunately
support proprietary gates. This is the reason why the driver will pass a list support proprietary gates. This is the reason why the driver will pass a list
of proprietary gates that must be part of the session. HCI will ensure all of proprietary gates that must be part of the session. HCI will ensure all
those gates have pipes connected when the hci device is set up. those gates have pipes connected when the hci device is set up.
In case the chip supports pre-opened gates and pseudo-static pipes, the driver
can pass that information to HCI core.
HCI Gates and Pipes HCI Gates and Pipes
------------------- -------------------
...@@ -46,6 +50,13 @@ without knowing the pipe connected to it. ...@@ -46,6 +50,13 @@ without knowing the pipe connected to it.
Driver interface Driver interface
---------------- ----------------
A driver is generally written in two parts : the physical link management and
the HCI management. This makes it easier to maintain a driver for a chip that
can be connected using various phy (i2c, spi, ...)
HCI Management
--------------
A driver would normally register itself with HCI and provide the following A driver would normally register itself with HCI and provide the following
entry points: entry points:
...@@ -53,58 +64,113 @@ struct nfc_hci_ops { ...@@ -53,58 +64,113 @@ struct nfc_hci_ops {
int (*open)(struct nfc_hci_dev *hdev); int (*open)(struct nfc_hci_dev *hdev);
void (*close)(struct nfc_hci_dev *hdev); void (*close)(struct nfc_hci_dev *hdev);
int (*hci_ready) (struct nfc_hci_dev *hdev); int (*hci_ready) (struct nfc_hci_dev *hdev);
int (*xmit)(struct nfc_hci_dev *hdev, struct sk_buff *skb); int (*xmit) (struct nfc_hci_dev *hdev, struct sk_buff *skb);
int (*start_poll)(struct nfc_hci_dev *hdev, u32 protocols); int (*start_poll) (struct nfc_hci_dev *hdev,
int (*target_from_gate)(struct nfc_hci_dev *hdev, u8 gate, u32 im_protocols, u32 tm_protocols);
struct nfc_target *target); int (*dep_link_up)(struct nfc_hci_dev *hdev, struct nfc_target *target,
u8 comm_mode, u8 *gb, size_t gb_len);
int (*dep_link_down)(struct nfc_hci_dev *hdev);
int (*target_from_gate) (struct nfc_hci_dev *hdev, u8 gate,
struct nfc_target *target);
int (*complete_target_discovered) (struct nfc_hci_dev *hdev, u8 gate, int (*complete_target_discovered) (struct nfc_hci_dev *hdev, u8 gate,
struct nfc_target *target); struct nfc_target *target);
int (*data_exchange) (struct nfc_hci_dev *hdev, int (*im_transceive) (struct nfc_hci_dev *hdev,
struct nfc_target *target, struct nfc_target *target, struct sk_buff *skb,
struct sk_buff *skb, struct sk_buff **res_skb); data_exchange_cb_t cb, void *cb_context);
int (*tm_send)(struct nfc_hci_dev *hdev, struct sk_buff *skb);
int (*check_presence)(struct nfc_hci_dev *hdev, int (*check_presence)(struct nfc_hci_dev *hdev,
struct nfc_target *target); struct nfc_target *target);
int (*event_received)(struct nfc_hci_dev *hdev, u8 gate, u8 event,
struct sk_buff *skb);
}; };
- open() and close() shall turn the hardware on and off. - open() and close() shall turn the hardware on and off.
- hci_ready() is an optional entry point that is called right after the hci - hci_ready() is an optional entry point that is called right after the hci
session has been set up. The driver can use it to do additional initialization session has been set up. The driver can use it to do additional initialization
that must be performed using HCI commands. that must be performed using HCI commands.
- xmit() shall simply write a frame to the chip. - xmit() shall simply write a frame to the physical link.
- start_poll() is an optional entrypoint that shall set the hardware in polling - start_poll() is an optional entrypoint that shall set the hardware in polling
mode. This must be implemented only if the hardware uses proprietary gates or a mode. This must be implemented only if the hardware uses proprietary gates or a
mechanism slightly different from the HCI standard. mechanism slightly different from the HCI standard.
- dep_link_up() is called after a p2p target has been detected, to finish
the p2p connection setup with hardware parameters that need to be passed back
to nfc core.
- dep_link_down() is called to bring the p2p link down.
- target_from_gate() is an optional entrypoint to return the nfc protocols - target_from_gate() is an optional entrypoint to return the nfc protocols
corresponding to a proprietary gate. corresponding to a proprietary gate.
- complete_target_discovered() is an optional entry point to let the driver - complete_target_discovered() is an optional entry point to let the driver
perform additional proprietary processing necessary to auto activate the perform additional proprietary processing necessary to auto activate the
discovered target. discovered target.
- data_exchange() must be implemented by the driver if proprietary HCI commands - im_transceive() must be implemented by the driver if proprietary HCI commands
are required to send data to the tag. Some tag types will require custom are required to send data to the tag. Some tag types will require custom
commands, others can be written to using the standard HCI commands. The driver commands, others can be written to using the standard HCI commands. The driver
can check the tag type and either do proprietary processing, or return 1 to ask can check the tag type and either do proprietary processing, or return 1 to ask
for standard processing. for standard processing. The data exchange command itself must be sent
asynchronously.
- tm_send() is called to send data in the case of a p2p connection
- check_presence() is an optional entry point that will be called regularly - check_presence() is an optional entry point that will be called regularly
by the core to check that an activated tag is still in the field. If this is by the core to check that an activated tag is still in the field. If this is
not implemented, the core will not be able to push tag_lost events to the user not implemented, the core will not be able to push tag_lost events to the user
space space
- event_received() is called to handle an event coming from the chip. Driver
can handle the event or return 1 to let HCI attempt standard processing.
On the rx path, the driver is responsible to push incoming HCP frames to HCI On the rx path, the driver is responsible to push incoming HCP frames to HCI
using nfc_hci_recv_frame(). HCI will take care of re-aggregation and handling using nfc_hci_recv_frame(). HCI will take care of re-aggregation and handling
This must be done from a context that can sleep. This must be done from a context that can sleep.
SHDLC PHY Management
----- --------------
The physical link (i2c, ...) management is defined by the following struture:
struct nfc_phy_ops {
int (*write)(void *dev_id, struct sk_buff *skb);
int (*enable)(void *dev_id);
void (*disable)(void *dev_id);
};
enable(): turn the phy on (power on), make it ready to transfer data
disable(): turn the phy off
write(): Send a data frame to the chip. Note that to enable higher
layers such as an llc to store the frame for re-emission, this function must
not alter the skb. It must also not return a positive result (return 0 for
success, negative for failure).
Data coming from the chip shall be sent directly to nfc_hci_recv_frame().
LLC
---
Communication between the CPU and the chip often requires some link layer
protocol. Those are isolated as modules managed by the HCI layer. There are
currently two modules : nop (raw transfert) and shdlc.
A new llc must implement the following functions:
struct nfc_llc_ops {
void *(*init) (struct nfc_hci_dev *hdev, xmit_to_drv_t xmit_to_drv,
rcv_to_hci_t rcv_to_hci, int tx_headroom,
int tx_tailroom, int *rx_headroom, int *rx_tailroom,
llc_failure_t llc_failure);
void (*deinit) (struct nfc_llc *llc);
int (*start) (struct nfc_llc *llc);
int (*stop) (struct nfc_llc *llc);
void (*rcv_from_drv) (struct nfc_llc *llc, struct sk_buff *skb);
int (*xmit_from_hci) (struct nfc_llc *llc, struct sk_buff *skb);
};
- init() : allocate and init your private storage
- deinit() : cleanup
- start() : establish the logical connection
- stop () : terminate the logical connection
- rcv_from_drv() : handle data coming from the chip, going to HCI
- xmit_from_hci() : handle data sent by HCI, going to the chip
Most chips use shdlc to ensure integrity and delivery ordering of the HCP The llc must be registered with nfc before it can be used. Do that by
frames between the host controller (the chip) and hosts (entities connected calling nfc_llc_register(const char *name, struct nfc_llc_ops *ops);
to the chip, like the cpu). In order to simplify writing the driver, an shdlc
layer is available for use by the driver. Again, note that the llc does not handle the physical link. It is thus very
When used, the driver actually registers with shdlc, and shdlc will register easy to mix any physical link with any llc for a given chip driver.
with HCI. HCI sees shdlc as the driver and thus send its HCP frames
through shdlc->xmit.
SHDLC adds a new execution context (nfc_shdlc_sm_work()) to run its state
machine and handle both its rx and tx path.
Included Drivers Included Drivers
---------------- ----------------
...@@ -117,10 +183,12 @@ Execution Contexts ...@@ -117,10 +183,12 @@ Execution Contexts
The execution contexts are the following: The execution contexts are the following:
- IRQ handler (IRQH): - IRQ handler (IRQH):
fast, cannot sleep. stores incoming frames into an shdlc rx queue fast, cannot sleep. sends incoming frames to HCI where they are passed to
the current llc. In case of shdlc, the frame is queued in shdlc rx queue.
- SHDLC State Machine worker (SMW) - SHDLC State Machine worker (SMW)
handles shdlc rx & tx queues. Dispatches HCI cmd responses. Only when llc_shdlc is used: handles shdlc rx & tx queues.
Dispatches HCI cmd responses.
- HCI Tx Cmd worker (MSGTXWQ) - HCI Tx Cmd worker (MSGTXWQ)
Serializes execution of HCI commands. Completes execution in case of response Serializes execution of HCI commands. Completes execution in case of response
...@@ -166,6 +234,15 @@ waiting command execution. Response processing involves invoking the completion ...@@ -166,6 +234,15 @@ waiting command execution. Response processing involves invoking the completion
callback that was provided by nfc_hci_msg_tx_work() when it sent the command. callback that was provided by nfc_hci_msg_tx_work() when it sent the command.
The completion callback will then wake the syscall context. The completion callback will then wake the syscall context.
It is also possible to execute the command asynchronously using this API:
static int nfc_hci_execute_cmd_async(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
const u8 *param, size_t param_len,
data_exchange_cb_t cb, void *cb_context)
The workflow is the same, except that the API call returns immediately, and
the callback will be called with the result from the SMW context.
Workflow receiving an HCI event or command Workflow receiving an HCI event or command
------------------------------------------ ------------------------------------------
......
Kernel driver for the NXP Semiconductors PN544 Near Field Kernel driver for the NXP Semiconductors PN544 Near Field
Communication chip Communication chip
Author: Jari Vanhala
Contact: Matti Aaltonen (matti.j.aaltonen at nokia.com)
General General
------- -------
The PN544 is an integrated transmission module for contactless The PN544 is an integrated transmission module for contactless
communication. The driver goes under drives/nfc/ and is compiled as a communication. The driver goes under drives/nfc/ and is compiled as a
module named "pn544". It registers a misc device and creates a device module named "pn544".
file named "/dev/pn544".
Host Interfaces: I2C, SPI and HSU, this driver supports currently only I2C. Host Interfaces: I2C, SPI and HSU, this driver supports currently only I2C.
The Interface
-------------
The driver offers a sysfs interface for a hardware test and an IOCTL
interface for selecting between two operating modes. There are read,
write and poll functions for transferring messages. The two operating
modes are the normal (HCI) mode and the firmware update mode.
PN544 is controlled by sending messages from the userspace to the
chip. The main function of the driver is just to pass those messages
without caring about the message content.
Protocols Protocols
--------- ---------
...@@ -47,68 +30,3 @@ and third (LSB) bytes of the message. The maximum FW message length is ...@@ -47,68 +30,3 @@ and third (LSB) bytes of the message. The maximum FW message length is
For the ETSI HCI specification see For the ETSI HCI specification see
http://www.etsi.org/WebSite/Technologies/ProtocolSpecification.aspx http://www.etsi.org/WebSite/Technologies/ProtocolSpecification.aspx
The Hardware Test
-----------------
The idea of the test is that it can performed by reading from the
corresponding sysfs file. The test is implemented in the board file
and it should test that PN544 can be put into the firmware update
mode. If the test is not implemented the sysfs file does not get
created.
Example:
> cat /sys/module/pn544/drivers/i2c\:pn544/3-002b/nfc_test
1
Normal Operation
----------------
PN544 is powered up when the device file is opened, otherwise it's
turned off. Only one instance can use the device at a time.
Userspace applications control PN544 with HCI messages. The hardware
sends an interrupt when data is available for reading. Data is
physically read when the read function is called by a userspace
application. Poll() checks the read interrupt state. Configuration and
self testing are also done from the userspace using read and write.
Example platform data:
static int rx71_pn544_nfc_request_resources(struct i2c_client *client)
{
/* Get and setup the HW resources for the device */
}
static void rx71_pn544_nfc_free_resources(void)
{
/* Release the HW resources */
}
static void rx71_pn544_nfc_enable(int fw)
{
/* Turn the device on */
}
static int rx71_pn544_nfc_test(void)
{
/*
* Put the device into the FW update mode
* and then back to the normal mode.
* Check the behavior and return one on success,
* zero on failure.
*/
}
static void rx71_pn544_nfc_disable(void)
{
/* turn the power off */
}
static struct pn544_nfc_platform_data rx71_nfc_data = {
.request_resources = rx71_pn544_nfc_request_resources,
.free_resources = rx71_pn544_nfc_free_resources,
.enable = rx71_pn544_nfc_enable,
.test = rx71_pn544_nfc_test,
.disable = rx71_pn544_nfc_disable,
};
...@@ -5,19 +5,6 @@ ...@@ -5,19 +5,6 @@
menu "Near Field Communication (NFC) devices" menu "Near Field Communication (NFC) devices"
depends on NFC depends on NFC
config PN544_HCI_NFC
tristate "HCI PN544 NFC driver"
depends on I2C && NFC_HCI && NFC_SHDLC
select CRC_CCITT
default n
---help---
NXP PN544 i2c driver.
This is a driver based on the SHDLC and HCI NFC kernel layers and
will thus not work with NXP libnfc library.
To compile this driver as a module, choose m here. The module will
be called pn544_hci.
config NFC_PN533 config NFC_PN533
tristate "NXP PN533 USB driver" tristate "NXP PN533 USB driver"
depends on USB depends on USB
...@@ -39,4 +26,6 @@ config NFC_WILINK ...@@ -39,4 +26,6 @@ config NFC_WILINK
Say Y here to compile support for Texas Instrument's NFC WiLink driver Say Y here to compile support for Texas Instrument's NFC WiLink driver
into the kernel or say M to compile it as module. into the kernel or say M to compile it as module.
source "drivers/nfc/pn544/Kconfig"
endmenu endmenu
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# Makefile for nfc devices # Makefile for nfc devices
# #
obj-$(CONFIG_PN544_HCI_NFC) += pn544/ obj-$(CONFIG_NFC_PN544) += pn544/
obj-$(CONFIG_NFC_PN533) += pn533.o obj-$(CONFIG_NFC_PN533) += pn533.o
obj-$(CONFIG_NFC_WILINK) += nfcwilink.o obj-$(CONFIG_NFC_WILINK) += nfcwilink.o
......
...@@ -542,6 +542,7 @@ static int nfcwilink_probe(struct platform_device *pdev) ...@@ -542,6 +542,7 @@ static int nfcwilink_probe(struct platform_device *pdev)
drv->ndev = nci_allocate_device(&nfcwilink_ops, drv->ndev = nci_allocate_device(&nfcwilink_ops,
protocols, protocols,
NFC_SE_NONE,
NFCWILINK_HDR_LEN, NFCWILINK_HDR_LEN,
0); 0);
if (!drv->ndev) { if (!drv->ndev) {
......
...@@ -41,11 +41,6 @@ ...@@ -41,11 +41,6 @@
#define SONY_VENDOR_ID 0x054c #define SONY_VENDOR_ID 0x054c
#define PASORI_PRODUCT_ID 0x02e1 #define PASORI_PRODUCT_ID 0x02e1
#define PN533_QUIRKS_TYPE_A BIT(0)
#define PN533_QUIRKS_TYPE_F BIT(1)
#define PN533_QUIRKS_DEP BIT(2)
#define PN533_QUIRKS_RAW_EXCHANGE BIT(3)
#define PN533_DEVICE_STD 0x1 #define PN533_DEVICE_STD 0x1
#define PN533_DEVICE_PASORI 0x2 #define PN533_DEVICE_PASORI 0x2
...@@ -84,14 +79,18 @@ MODULE_DEVICE_TABLE(usb, pn533_table); ...@@ -84,14 +79,18 @@ MODULE_DEVICE_TABLE(usb, pn533_table);
#define PN533_LISTEN_TIME 2 #define PN533_LISTEN_TIME 2
/* frame definitions */ /* frame definitions */
#define PN533_NORMAL_FRAME_MAX_LEN 262 /* 6 (PREAMBLE, SOF, LEN, LCS, TFI) #define PN533_FRAME_HEADER_LEN (sizeof(struct pn533_frame) \
254 (DATA) + 2) /* data[0] TFI, data[1] CC */
2 (DCS, postamble) */ #define PN533_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
#define PN533_FRAME_TAIL_SIZE 2 /*
#define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \ * Max extended frame payload len, excluding TFI and CC
PN533_FRAME_TAIL_SIZE) * which are already in PN533_FRAME_HEADER_LEN.
#define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1) */
#define PN533_FRAME_MAX_PAYLOAD_LEN 263
#define PN533_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
Postamble (1) */
#define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen]) #define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
#define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1]) #define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
...@@ -105,8 +104,6 @@ MODULE_DEVICE_TABLE(usb, pn533_table); ...@@ -105,8 +104,6 @@ MODULE_DEVICE_TABLE(usb, pn533_table);
/* PN533 Commands */ /* PN533 Commands */
#define PN533_FRAME_CMD(f) (f->data[1]) #define PN533_FRAME_CMD(f) (f->data[1])
#define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
#define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
#define PN533_CMD_GET_FIRMWARE_VERSION 0x02 #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
#define PN533_CMD_RF_CONFIGURATION 0x32 #define PN533_CMD_RF_CONFIGURATION 0x32
...@@ -120,6 +117,7 @@ MODULE_DEVICE_TABLE(usb, pn533_table); ...@@ -120,6 +117,7 @@ MODULE_DEVICE_TABLE(usb, pn533_table);
#define PN533_CMD_TG_INIT_AS_TARGET 0x8c #define PN533_CMD_TG_INIT_AS_TARGET 0x8c
#define PN533_CMD_TG_GET_DATA 0x86 #define PN533_CMD_TG_GET_DATA 0x86
#define PN533_CMD_TG_SET_DATA 0x8e #define PN533_CMD_TG_SET_DATA 0x8e
#define PN533_CMD_UNDEF 0xff
#define PN533_CMD_RESPONSE(cmd) (cmd + 1) #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
...@@ -128,13 +126,12 @@ MODULE_DEVICE_TABLE(usb, pn533_table); ...@@ -128,13 +126,12 @@ MODULE_DEVICE_TABLE(usb, pn533_table);
#define PN533_CMD_MI_MASK 0x40 #define PN533_CMD_MI_MASK 0x40
#define PN533_CMD_RET_SUCCESS 0x00 #define PN533_CMD_RET_SUCCESS 0x00
/* PN533 status codes */
#define PN533_STATUS_TARGET_RELEASED 0x29
struct pn533; struct pn533;
typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg, typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg, int status);
u8 *params, int params_len);
typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
struct sk_buff *resp);
/* structs for pn533 commands */ /* structs for pn533 commands */
...@@ -282,11 +279,6 @@ const struct pn533_poll_modulations poll_mod[] = { ...@@ -282,11 +279,6 @@ const struct pn533_poll_modulations poll_mod[] = {
/* PN533_CMD_IN_ATR */ /* PN533_CMD_IN_ATR */
struct pn533_cmd_activate_param {
u8 tg;
u8 next;
} __packed;
struct pn533_cmd_activate_response { struct pn533_cmd_activate_response {
u8 status; u8 status;
u8 nfcid3t[10]; u8 nfcid3t[10];
...@@ -299,14 +291,6 @@ struct pn533_cmd_activate_response { ...@@ -299,14 +291,6 @@ struct pn533_cmd_activate_response {
u8 gt[]; u8 gt[];
} __packed; } __packed;
/* PN533_CMD_IN_JUMP_FOR_DEP */
struct pn533_cmd_jump_dep {
u8 active;
u8 baud;
u8 next;
u8 data[];
} __packed;
struct pn533_cmd_jump_dep_response { struct pn533_cmd_jump_dep_response {
u8 status; u8 status;
u8 tg; u8 tg;
...@@ -329,32 +313,13 @@ struct pn533_cmd_jump_dep_response { ...@@ -329,32 +313,13 @@ struct pn533_cmd_jump_dep_response {
#define PN533_INIT_TARGET_RESP_ACTIVE 0x1 #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
#define PN533_INIT_TARGET_RESP_DEP 0x4 #define PN533_INIT_TARGET_RESP_DEP 0x4
struct pn533_cmd_init_target {
u8 mode;
u8 mifare[6];
u8 felica[18];
u8 nfcid3[10];
u8 gb_len;
u8 gb[];
} __packed;
struct pn533_cmd_init_target_response {
u8 mode;
u8 cmd[];
} __packed;
struct pn533 { struct pn533 {
struct usb_device *udev; struct usb_device *udev;
struct usb_interface *interface; struct usb_interface *interface;
struct nfc_dev *nfc_dev; struct nfc_dev *nfc_dev;
struct urb *out_urb; struct urb *out_urb;
int out_maxlen;
struct pn533_frame *out_frame;
struct urb *in_urb; struct urb *in_urb;
int in_maxlen;
struct pn533_frame *in_frame;
struct sk_buff_head resp_q; struct sk_buff_head resp_q;
...@@ -365,12 +330,12 @@ struct pn533 { ...@@ -365,12 +330,12 @@ struct pn533 {
struct work_struct mi_work; struct work_struct mi_work;
struct work_struct tg_work; struct work_struct tg_work;
struct timer_list listen_timer; struct timer_list listen_timer;
struct pn533_frame *wq_in_frame;
int wq_in_error; int wq_in_error;
int cancel_listen; int cancel_listen;
pn533_cmd_complete_t cmd_complete; pn533_cmd_complete_t cmd_complete;
void *cmd_complete_arg; void *cmd_complete_arg;
void *cmd_complete_mi_arg;
struct mutex cmd_lock; struct mutex cmd_lock;
u8 cmd; u8 cmd;
...@@ -391,16 +356,17 @@ struct pn533 { ...@@ -391,16 +356,17 @@ struct pn533 {
struct list_head cmd_queue; struct list_head cmd_queue;
u8 cmd_pending; u8 cmd_pending;
struct pn533_frame_ops *ops;
}; };
struct pn533_cmd { struct pn533_cmd {
struct list_head queue; struct list_head queue;
struct pn533_frame *out_frame; u8 cmd_code;
struct pn533_frame *in_frame; struct sk_buff *req;
int in_frame_len; struct sk_buff *resp;
pn533_cmd_complete_t cmd_complete; int resp_len;
void *arg; void *arg;
gfp_t flags;
}; };
struct pn533_frame { struct pn533_frame {
...@@ -411,6 +377,22 @@ struct pn533_frame { ...@@ -411,6 +377,22 @@ struct pn533_frame {
u8 data[]; u8 data[];
} __packed; } __packed;
struct pn533_frame_ops {
void (*tx_frame_init)(void *frame, u8 cmd_code);
void (*tx_frame_finish)(void *frame);
void (*tx_update_payload_len)(void *frame, int len);
int tx_header_len;
int tx_tail_len;
bool (*rx_is_frame_valid)(void *frame);
int (*rx_frame_size)(void *frame);
int rx_header_len;
int rx_tail_len;
int max_payload_len;
u8 (*get_cmd_code)(void *frame);
};
/* The rule: value + checksum = 0 */ /* The rule: value + checksum = 0 */
static inline u8 pn533_checksum(u8 value) static inline u8 pn533_checksum(u8 value)
{ {
...@@ -429,37 +411,21 @@ static u8 pn533_data_checksum(u8 *data, int datalen) ...@@ -429,37 +411,21 @@ static u8 pn533_data_checksum(u8 *data, int datalen)
return pn533_checksum(sum); return pn533_checksum(sum);
} }
/** static void pn533_tx_frame_init(void *_frame, u8 cmd_code)
* pn533_tx_frame_ack - create a ack frame
* @frame: The frame to be set as ack
*
* Ack is different type of standard frame. As a standard frame, it has
* preamble and start_frame. However the checksum of this frame must fail,
* i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
* fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
* After datalen_checksum field, the postamble is placed.
*/
static void pn533_tx_frame_ack(struct pn533_frame *frame)
{ {
frame->preamble = 0; struct pn533_frame *frame = _frame;
frame->start_frame = cpu_to_be16(PN533_SOF);
frame->datalen = 0;
frame->datalen_checksum = 0xFF;
/* data[0] is used as postamble */
frame->data[0] = 0;
}
static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
{
frame->preamble = 0; frame->preamble = 0;
frame->start_frame = cpu_to_be16(PN533_SOF); frame->start_frame = cpu_to_be16(PN533_SOF);
PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT; PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
PN533_FRAME_CMD(frame) = cmd; PN533_FRAME_CMD(frame) = cmd_code;
frame->datalen = 2; frame->datalen = 2;
} }
static void pn533_tx_frame_finish(struct pn533_frame *frame) static void pn533_tx_frame_finish(void *_frame)
{ {
struct pn533_frame *frame = _frame;
frame->datalen_checksum = pn533_checksum(frame->datalen); frame->datalen_checksum = pn533_checksum(frame->datalen);
PN533_FRAME_CHECKSUM(frame) = PN533_FRAME_CHECKSUM(frame) =
...@@ -468,9 +434,17 @@ static void pn533_tx_frame_finish(struct pn533_frame *frame) ...@@ -468,9 +434,17 @@ static void pn533_tx_frame_finish(struct pn533_frame *frame)
PN533_FRAME_POSTAMBLE(frame) = 0; PN533_FRAME_POSTAMBLE(frame) = 0;
} }
static bool pn533_rx_frame_is_valid(struct pn533_frame *frame) static void pn533_tx_update_payload_len(void *_frame, int len)
{
struct pn533_frame *frame = _frame;
frame->datalen += len;
}
static bool pn533_rx_frame_is_valid(void *_frame)
{ {
u8 checksum; u8 checksum;
struct pn533_frame *frame = _frame;
if (frame->start_frame != cpu_to_be16(PN533_SOF)) if (frame->start_frame != cpu_to_be16(PN533_SOF))
return false; return false;
...@@ -497,28 +471,48 @@ static bool pn533_rx_frame_is_ack(struct pn533_frame *frame) ...@@ -497,28 +471,48 @@ static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
return true; return true;
} }
static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd) static inline int pn533_rx_frame_size(void *frame)
{
struct pn533_frame *f = frame;
return sizeof(struct pn533_frame) + f->datalen + PN533_FRAME_TAIL_LEN;
}
static u8 pn533_get_cmd_code(void *frame)
{
struct pn533_frame *f = frame;
return PN533_FRAME_CMD(f);
}
struct pn533_frame_ops pn533_std_frame_ops = {
.tx_frame_init = pn533_tx_frame_init,
.tx_frame_finish = pn533_tx_frame_finish,
.tx_update_payload_len = pn533_tx_update_payload_len,
.tx_header_len = PN533_FRAME_HEADER_LEN,
.tx_tail_len = PN533_FRAME_TAIL_LEN,
.rx_is_frame_valid = pn533_rx_frame_is_valid,
.rx_frame_size = pn533_rx_frame_size,
.rx_header_len = PN533_FRAME_HEADER_LEN,
.rx_tail_len = PN533_FRAME_TAIL_LEN,
.max_payload_len = PN533_FRAME_MAX_PAYLOAD_LEN,
.get_cmd_code = pn533_get_cmd_code,
};
static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
{ {
return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd)); return (dev->ops->get_cmd_code(frame) == PN533_CMD_RESPONSE(dev->cmd));
} }
static void pn533_wq_cmd_complete(struct work_struct *work) static void pn533_wq_cmd_complete(struct work_struct *work)
{ {
struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work); struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
struct pn533_frame *in_frame;
int rc; int rc;
in_frame = dev->wq_in_frame; rc = dev->cmd_complete(dev, dev->cmd_complete_arg, dev->wq_in_error);
if (dev->wq_in_error)
rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
dev->wq_in_error);
else
rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
PN533_FRAME_CMD_PARAMS_PTR(in_frame),
PN533_FRAME_CMD_PARAMS_LEN(in_frame));
if (rc != -EINPROGRESS) if (rc != -EINPROGRESS)
queue_work(dev->wq, &dev->cmd_work); queue_work(dev->wq, &dev->cmd_work);
} }
...@@ -526,46 +520,47 @@ static void pn533_wq_cmd_complete(struct work_struct *work) ...@@ -526,46 +520,47 @@ static void pn533_wq_cmd_complete(struct work_struct *work)
static void pn533_recv_response(struct urb *urb) static void pn533_recv_response(struct urb *urb)
{ {
struct pn533 *dev = urb->context; struct pn533 *dev = urb->context;
struct pn533_frame *in_frame; u8 *in_frame;
dev->wq_in_frame = NULL;
switch (urb->status) { switch (urb->status) {
case 0: case 0:
/* success */ break; /* success */
break;
case -ECONNRESET: case -ECONNRESET:
case -ENOENT: case -ENOENT:
case -ESHUTDOWN: nfc_dev_dbg(&dev->interface->dev,
nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with" "The urb has been canceled (status %d)",
" status: %d", urb->status); urb->status);
dev->wq_in_error = urb->status; dev->wq_in_error = urb->status;
goto sched_wq; goto sched_wq;
break;
case -ESHUTDOWN:
default: default:
nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:" nfc_dev_err(&dev->interface->dev,
" %d", urb->status); "Urb failure (status %d)", urb->status);
dev->wq_in_error = urb->status; dev->wq_in_error = urb->status;
goto sched_wq; goto sched_wq;
} }
in_frame = dev->in_urb->transfer_buffer; in_frame = dev->in_urb->transfer_buffer;
if (!pn533_rx_frame_is_valid(in_frame)) { nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
print_hex_dump(KERN_DEBUG, "PN533 RX: ", DUMP_PREFIX_NONE, 16, 1,
in_frame, dev->ops->rx_frame_size(in_frame), false);
if (!dev->ops->rx_is_frame_valid(in_frame)) {
nfc_dev_err(&dev->interface->dev, "Received an invalid frame"); nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
dev->wq_in_error = -EIO; dev->wq_in_error = -EIO;
goto sched_wq; goto sched_wq;
} }
if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) { if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
nfc_dev_err(&dev->interface->dev, "The received frame is not " nfc_dev_err(&dev->interface->dev,
"response to the last command"); "It it not the response to the last command");
dev->wq_in_error = -EIO; dev->wq_in_error = -EIO;
goto sched_wq; goto sched_wq;
} }
nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
dev->wq_in_error = 0; dev->wq_in_error = 0;
dev->wq_in_frame = in_frame;
sched_wq: sched_wq:
queue_work(dev->wq, &dev->cmd_complete_work); queue_work(dev->wq, &dev->cmd_complete_work);
...@@ -586,18 +581,19 @@ static void pn533_recv_ack(struct urb *urb) ...@@ -586,18 +581,19 @@ static void pn533_recv_ack(struct urb *urb)
switch (urb->status) { switch (urb->status) {
case 0: case 0:
/* success */ break; /* success */
break;
case -ECONNRESET: case -ECONNRESET:
case -ENOENT: case -ENOENT:
case -ESHUTDOWN: nfc_dev_dbg(&dev->interface->dev,
nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with" "The urb has been stopped (status %d)",
" status: %d", urb->status); urb->status);
dev->wq_in_error = urb->status; dev->wq_in_error = urb->status;
goto sched_wq; goto sched_wq;
break;
case -ESHUTDOWN:
default: default:
nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:" nfc_dev_err(&dev->interface->dev,
" %d", urb->status); "Urb failure (status %d)", urb->status);
dev->wq_in_error = urb->status; dev->wq_in_error = urb->status;
goto sched_wq; goto sched_wq;
} }
...@@ -610,12 +606,10 @@ static void pn533_recv_ack(struct urb *urb) ...@@ -610,12 +606,10 @@ static void pn533_recv_ack(struct urb *urb)
goto sched_wq; goto sched_wq;
} }
nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC); rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
if (rc) { if (rc) {
nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with" nfc_dev_err(&dev->interface->dev,
" result %d", rc); "usb_submit_urb failed with result %d", rc);
dev->wq_in_error = rc; dev->wq_in_error = rc;
goto sched_wq; goto sched_wq;
} }
...@@ -623,7 +617,6 @@ static void pn533_recv_ack(struct urb *urb) ...@@ -623,7 +617,6 @@ static void pn533_recv_ack(struct urb *urb)
return; return;
sched_wq: sched_wq:
dev->wq_in_frame = NULL;
queue_work(dev->wq, &dev->cmd_complete_work); queue_work(dev->wq, &dev->cmd_complete_work);
} }
...@@ -636,47 +629,46 @@ static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags) ...@@ -636,47 +629,46 @@ static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
static int pn533_send_ack(struct pn533 *dev, gfp_t flags) static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
{ {
u8 ack[PN533_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
/* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
int rc; int rc;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__); nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
pn533_tx_frame_ack(dev->out_frame); dev->out_urb->transfer_buffer = ack;
dev->out_urb->transfer_buffer_length = sizeof(ack);
dev->out_urb->transfer_buffer = dev->out_frame;
dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
rc = usb_submit_urb(dev->out_urb, flags); rc = usb_submit_urb(dev->out_urb, flags);
return rc; return rc;
} }
static int __pn533_send_cmd_frame_async(struct pn533 *dev, static int __pn533_send_frame_async(struct pn533 *dev,
struct pn533_frame *out_frame, struct sk_buff *out,
struct pn533_frame *in_frame, struct sk_buff *in,
int in_frame_len, int in_len,
pn533_cmd_complete_t cmd_complete, pn533_cmd_complete_t cmd_complete,
void *arg, gfp_t flags) void *arg)
{ {
int rc; int rc;
nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", dev->cmd = dev->ops->get_cmd_code(out->data);
PN533_FRAME_CMD(out_frame));
dev->cmd = PN533_FRAME_CMD(out_frame);
dev->cmd_complete = cmd_complete; dev->cmd_complete = cmd_complete;
dev->cmd_complete_arg = arg; dev->cmd_complete_arg = arg;
dev->out_urb->transfer_buffer = out_frame; dev->out_urb->transfer_buffer = out->data;
dev->out_urb->transfer_buffer_length = dev->out_urb->transfer_buffer_length = out->len;
PN533_FRAME_SIZE(out_frame);
dev->in_urb->transfer_buffer = in_frame; dev->in_urb->transfer_buffer = in->data;
dev->in_urb->transfer_buffer_length = in_frame_len; dev->in_urb->transfer_buffer_length = in_len;
rc = usb_submit_urb(dev->out_urb, flags); print_hex_dump(KERN_DEBUG, "PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
out->data, out->len, false);
rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
if (rc) if (rc)
return rc; return rc;
rc = pn533_submit_urb_for_ack(dev, flags); rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
if (rc) if (rc)
goto error; goto error;
...@@ -687,146 +679,325 @@ static int __pn533_send_cmd_frame_async(struct pn533 *dev, ...@@ -687,146 +679,325 @@ static int __pn533_send_cmd_frame_async(struct pn533 *dev,
return rc; return rc;
} }
static void pn533_wq_cmd(struct work_struct *work) static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
struct sk_buff *skb)
{ {
struct pn533 *dev = container_of(work, struct pn533, cmd_work); /* payload is already there, just update datalen */
struct pn533_cmd *cmd; int payload_len = skb->len;
struct pn533_frame_ops *ops = dev->ops;
mutex_lock(&dev->cmd_lock);
if (list_empty(&dev->cmd_queue)) { skb_push(skb, ops->tx_header_len);
dev->cmd_pending = 0; skb_put(skb, ops->tx_tail_len);
mutex_unlock(&dev->cmd_lock);
return;
}
cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue); ops->tx_frame_init(skb->data, cmd_code);
ops->tx_update_payload_len(skb->data, payload_len);
ops->tx_frame_finish(skb->data);
}
list_del(&cmd->queue); struct pn533_send_async_complete_arg {
pn533_send_async_complete_t complete_cb;
void *complete_cb_context;
struct sk_buff *resp;
struct sk_buff *req;
};
mutex_unlock(&dev->cmd_lock); static int pn533_send_async_complete(struct pn533 *dev, void *_arg, int status)
{
struct pn533_send_async_complete_arg *arg = _arg;
__pn533_send_cmd_frame_async(dev, cmd->out_frame, cmd->in_frame, struct sk_buff *req = arg->req;
cmd->in_frame_len, cmd->cmd_complete, struct sk_buff *resp = arg->resp;
cmd->arg, cmd->flags);
kfree(cmd); int rc;
dev_kfree_skb(req);
if (status < 0) {
arg->complete_cb(dev, arg->complete_cb_context,
ERR_PTR(status));
dev_kfree_skb(resp);
kfree(arg);
return status;
}
skb_put(resp, dev->ops->rx_frame_size(resp->data));
skb_pull(resp, dev->ops->rx_header_len);
skb_trim(resp, resp->len - dev->ops->rx_tail_len);
rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
kfree(arg);
return rc;
} }
static int pn533_send_cmd_frame_async(struct pn533 *dev, static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
struct pn533_frame *out_frame, struct sk_buff *req, struct sk_buff *resp,
struct pn533_frame *in_frame, int resp_len,
int in_frame_len, pn533_send_async_complete_t complete_cb,
pn533_cmd_complete_t cmd_complete, void *complete_cb_context)
void *arg, gfp_t flags)
{ {
struct pn533_cmd *cmd; struct pn533_cmd *cmd;
struct pn533_send_async_complete_arg *arg;
int rc = 0; int rc = 0;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__); nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
arg = kzalloc(sizeof(*arg), GFP_KERNEL);
if (!arg)
return -ENOMEM;
arg->complete_cb = complete_cb;
arg->complete_cb_context = complete_cb_context;
arg->resp = resp;
arg->req = req;
pn533_build_cmd_frame(dev, cmd_code, req);
mutex_lock(&dev->cmd_lock); mutex_lock(&dev->cmd_lock);
if (!dev->cmd_pending) { if (!dev->cmd_pending) {
rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame, rc = __pn533_send_frame_async(dev, req, resp, resp_len,
in_frame_len, cmd_complete, pn533_send_async_complete, arg);
arg, flags); if (rc)
if (!rc) goto error;
dev->cmd_pending = 1;
dev->cmd_pending = 1;
goto unlock; goto unlock;
} }
nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__); nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
cmd_code);
cmd = kzalloc(sizeof(struct pn533_cmd), flags); cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
if (!cmd) { if (!cmd) {
rc = -ENOMEM; rc = -ENOMEM;
goto unlock; goto error;
} }
INIT_LIST_HEAD(&cmd->queue); INIT_LIST_HEAD(&cmd->queue);
cmd->out_frame = out_frame; cmd->cmd_code = cmd_code;
cmd->in_frame = in_frame; cmd->req = req;
cmd->in_frame_len = in_frame_len; cmd->resp = resp;
cmd->cmd_complete = cmd_complete; cmd->resp_len = resp_len;
cmd->arg = arg; cmd->arg = arg;
cmd->flags = flags;
list_add_tail(&cmd->queue, &dev->cmd_queue); list_add_tail(&cmd->queue, &dev->cmd_queue);
goto unlock;
error:
kfree(arg);
unlock: unlock:
mutex_unlock(&dev->cmd_lock); mutex_unlock(&dev->cmd_lock);
return rc;
}
static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
struct sk_buff *req,
pn533_send_async_complete_t complete_cb,
void *complete_cb_context)
{
struct sk_buff *resp;
int rc;
int resp_len = dev->ops->rx_header_len +
dev->ops->max_payload_len +
dev->ops->rx_tail_len;
resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
if (!resp)
return -ENOMEM;
rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
complete_cb_context);
if (rc)
dev_kfree_skb(resp);
return rc; return rc;
} }
struct pn533_sync_cmd_response { static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
struct sk_buff *req,
pn533_send_async_complete_t complete_cb,
void *complete_cb_context)
{
struct sk_buff *resp;
int rc; int rc;
struct completion done; int resp_len = dev->ops->rx_header_len +
}; dev->ops->max_payload_len +
dev->ops->rx_tail_len;
resp = alloc_skb(resp_len, GFP_KERNEL);
if (!resp)
return -ENOMEM;
rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
complete_cb_context);
if (rc)
dev_kfree_skb(resp);
static int pn533_sync_cmd_complete(struct pn533 *dev, void *_arg, return rc;
u8 *params, int params_len) }
/*
* pn533_send_cmd_direct_async
*
* The function sends a piority cmd directly to the chip omiting the cmd
* queue. It's intended to be used by chaining mechanism of received responses
* where the host has to request every single chunk of data before scheduling
* next cmd from the queue.
*/
static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
struct sk_buff *req,
pn533_send_async_complete_t complete_cb,
void *complete_cb_context)
{ {
struct pn533_sync_cmd_response *arg = _arg; struct pn533_send_async_complete_arg *arg;
struct sk_buff *resp;
int rc;
int resp_len = dev->ops->rx_header_len +
dev->ops->max_payload_len +
dev->ops->rx_tail_len;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__); resp = alloc_skb(resp_len, GFP_KERNEL);
if (!resp)
return -ENOMEM;
arg = kzalloc(sizeof(*arg), GFP_KERNEL);
if (!arg) {
dev_kfree_skb(resp);
return -ENOMEM;
}
arg->rc = 0; arg->complete_cb = complete_cb;
arg->complete_cb_context = complete_cb_context;
arg->resp = resp;
arg->req = req;
if (params_len < 0) /* error */ pn533_build_cmd_frame(dev, cmd_code, req);
arg->rc = params_len;
rc = __pn533_send_frame_async(dev, req, resp, resp_len,
pn533_send_async_complete, arg);
if (rc < 0) {
dev_kfree_skb(resp);
kfree(arg);
}
return rc;
}
static void pn533_wq_cmd(struct work_struct *work)
{
struct pn533 *dev = container_of(work, struct pn533, cmd_work);
struct pn533_cmd *cmd;
mutex_lock(&dev->cmd_lock);
if (list_empty(&dev->cmd_queue)) {
dev->cmd_pending = 0;
mutex_unlock(&dev->cmd_lock);
return;
}
cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
list_del(&cmd->queue);
mutex_unlock(&dev->cmd_lock);
__pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len,
pn533_send_async_complete, cmd->arg);
kfree(cmd);
}
struct pn533_sync_cmd_response {
struct sk_buff *resp;
struct completion done;
};
static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
struct sk_buff *resp)
{
struct pn533_sync_cmd_response *arg = _arg;
arg->resp = resp;
complete(&arg->done); complete(&arg->done);
return 0; return 0;
} }
static int pn533_send_cmd_frame_sync(struct pn533 *dev, /* pn533_send_cmd_sync
struct pn533_frame *out_frame, *
struct pn533_frame *in_frame, * Please note the req parameter is freed inside the function to
int in_frame_len) * limit a number of return value interpretations by the caller.
*
* 1. negative in case of error during TX path -> req should be freed
*
* 2. negative in case of error during RX path -> req should not be freed
* as it's been already freed at the begining of RX path by
* async_complete_cb.
*
* 3. valid pointer in case of succesfult RX path
*
* A caller has to check a return value with IS_ERR macro. If the test pass,
* the returned pointer is valid.
*
* */
static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
struct sk_buff *req)
{ {
int rc; int rc;
struct pn533_sync_cmd_response arg; struct pn533_sync_cmd_response arg;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
init_completion(&arg.done); init_completion(&arg.done);
rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, in_frame_len, rc = pn533_send_cmd_async(dev, cmd_code, req,
pn533_sync_cmd_complete, &arg, GFP_KERNEL); pn533_send_sync_complete, &arg);
if (rc) if (rc) {
return rc; dev_kfree_skb(req);
return ERR_PTR(rc);
}
wait_for_completion(&arg.done); wait_for_completion(&arg.done);
return arg.rc; return arg.resp;
} }
static void pn533_send_complete(struct urb *urb) static void pn533_send_complete(struct urb *urb)
{ {
struct pn533 *dev = urb->context; struct pn533 *dev = urb->context;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
switch (urb->status) { switch (urb->status) {
case 0: case 0:
/* success */ break; /* success */
break;
case -ECONNRESET: case -ECONNRESET:
case -ENOENT: case -ENOENT:
case -ESHUTDOWN: nfc_dev_dbg(&dev->interface->dev,
nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with" "The urb has been stopped (status %d)",
" status: %d", urb->status); urb->status);
break; break;
case -ESHUTDOWN:
default: default:
nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:" nfc_dev_err(&dev->interface->dev,
" %d", urb->status); "Urb failure (status %d)", urb->status);
} }
} }
static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
{
struct sk_buff *skb;
skb = alloc_skb(dev->ops->tx_header_len +
size +
dev->ops->tx_tail_len, GFP_KERNEL);
if (skb)
skb_reserve(skb, dev->ops->tx_header_len);
return skb;
}
struct pn533_target_type_a { struct pn533_target_type_a {
__be16 sens_res; __be16 sens_res;
u8 sel_res; u8 sel_res;
...@@ -867,9 +1038,9 @@ static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a, ...@@ -867,9 +1038,9 @@ static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res); platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL && if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) || platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
(ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL && (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL)) platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
return false; return false;
/* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */ /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
...@@ -884,7 +1055,7 @@ static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data, ...@@ -884,7 +1055,7 @@ static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
{ {
struct pn533_target_type_a *tgt_type_a; struct pn533_target_type_a *tgt_type_a;
tgt_type_a = (struct pn533_target_type_a *) tgt_data; tgt_type_a = (struct pn533_target_type_a *)tgt_data;
if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len)) if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
return -EPROTO; return -EPROTO;
...@@ -942,14 +1113,13 @@ static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data, ...@@ -942,14 +1113,13 @@ static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
{ {
struct pn533_target_felica *tgt_felica; struct pn533_target_felica *tgt_felica;
tgt_felica = (struct pn533_target_felica *) tgt_data; tgt_felica = (struct pn533_target_felica *)tgt_data;
if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len)) if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
return -EPROTO; return -EPROTO;
if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 && if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
tgt_felica->nfcid2[1] == (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
PN533_FELICA_SENSF_NFCID2_DEP_B2)
nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK; nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
else else
nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK; nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
...@@ -979,9 +1149,9 @@ static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel, ...@@ -979,9 +1149,9 @@ static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res); platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL && if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) || platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
(ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL && (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL)) platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
return false; return false;
return true; return true;
...@@ -992,7 +1162,7 @@ static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data, ...@@ -992,7 +1162,7 @@ static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
{ {
struct pn533_target_jewel *tgt_jewel; struct pn533_target_jewel *tgt_jewel;
tgt_jewel = (struct pn533_target_jewel *) tgt_data; tgt_jewel = (struct pn533_target_jewel *)tgt_data;
if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len)) if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
return -EPROTO; return -EPROTO;
...@@ -1051,7 +1221,7 @@ static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data, ...@@ -1051,7 +1221,7 @@ static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
{ {
struct pn533_target_type_b *tgt_type_b; struct pn533_target_type_b *tgt_type_b;
tgt_type_b = (struct pn533_target_type_b *) tgt_data; tgt_type_b = (struct pn533_target_type_b *)tgt_data;
if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len)) if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
return -EPROTO; return -EPROTO;
...@@ -1061,50 +1231,37 @@ static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data, ...@@ -1061,50 +1231,37 @@ static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
return 0; return 0;
} }
struct pn533_poll_response { static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
u8 nbtg; int tgdata_len)
u8 tg;
u8 target_data[];
} __packed;
static int pn533_target_found(struct pn533 *dev,
struct pn533_poll_response *resp, int resp_len)
{ {
int target_data_len;
struct nfc_target nfc_tgt; struct nfc_target nfc_tgt;
int rc; int rc;
nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__, nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
dev->poll_mod_curr); dev->poll_mod_curr);
if (resp->tg != 1) if (tg != 1)
return -EPROTO; return -EPROTO;
memset(&nfc_tgt, 0, sizeof(struct nfc_target)); memset(&nfc_tgt, 0, sizeof(struct nfc_target));
target_data_len = resp_len - sizeof(struct pn533_poll_response);
switch (dev->poll_mod_curr) { switch (dev->poll_mod_curr) {
case PN533_POLL_MOD_106KBPS_A: case PN533_POLL_MOD_106KBPS_A:
rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data, rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
target_data_len);
break; break;
case PN533_POLL_MOD_212KBPS_FELICA: case PN533_POLL_MOD_212KBPS_FELICA:
case PN533_POLL_MOD_424KBPS_FELICA: case PN533_POLL_MOD_424KBPS_FELICA:
rc = pn533_target_found_felica(&nfc_tgt, resp->target_data, rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
target_data_len);
break; break;
case PN533_POLL_MOD_106KBPS_JEWEL: case PN533_POLL_MOD_106KBPS_JEWEL:
rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data, rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
target_data_len);
break; break;
case PN533_POLL_MOD_847KBPS_B: case PN533_POLL_MOD_847KBPS_B:
rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data, rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
target_data_len);
break; break;
default: default:
nfc_dev_err(&dev->interface->dev, "Unknown current poll" nfc_dev_err(&dev->interface->dev,
" modulation"); "Unknown current poll modulation");
return -EPROTO; return -EPROTO;
} }
...@@ -1112,13 +1269,14 @@ static int pn533_target_found(struct pn533 *dev, ...@@ -1112,13 +1269,14 @@ static int pn533_target_found(struct pn533 *dev,
return rc; return rc;
if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) { if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
nfc_dev_dbg(&dev->interface->dev, "The target found does not" nfc_dev_dbg(&dev->interface->dev,
" have the desired protocol"); "The Tg found doesn't have the desired protocol");
return -EAGAIN; return -EAGAIN;
} }
nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: " nfc_dev_dbg(&dev->interface->dev,
"0x%x", nfc_tgt.supported_protocols); "Target found - supported protocols: 0x%x",
nfc_tgt.supported_protocols);
dev->tgt_available_prots = nfc_tgt.supported_protocols; dev->tgt_available_prots = nfc_tgt.supported_protocols;
...@@ -1140,7 +1298,7 @@ static void pn533_poll_reset_mod_list(struct pn533 *dev) ...@@ -1140,7 +1298,7 @@ static void pn533_poll_reset_mod_list(struct pn533 *dev)
static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index) static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
{ {
dev->poll_mod_active[dev->poll_mod_count] = dev->poll_mod_active[dev->poll_mod_count] =
(struct pn533_poll_modulations *) &poll_mod[mod_index]; (struct pn533_poll_modulations *)&poll_mod[mod_index];
dev->poll_mod_count++; dev->poll_mod_count++;
} }
...@@ -1149,13 +1307,13 @@ static void pn533_poll_create_mod_list(struct pn533 *dev, ...@@ -1149,13 +1307,13 @@ static void pn533_poll_create_mod_list(struct pn533 *dev,
{ {
pn533_poll_reset_mod_list(dev); pn533_poll_reset_mod_list(dev);
if (im_protocols & NFC_PROTO_MIFARE_MASK if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
|| im_protocols & NFC_PROTO_ISO14443_MASK (im_protocols & NFC_PROTO_ISO14443_MASK) ||
|| im_protocols & NFC_PROTO_NFC_DEP_MASK) (im_protocols & NFC_PROTO_NFC_DEP_MASK))
pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A); pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
if (im_protocols & NFC_PROTO_FELICA_MASK if (im_protocols & NFC_PROTO_FELICA_MASK ||
|| im_protocols & NFC_PROTO_NFC_DEP_MASK) { im_protocols & NFC_PROTO_NFC_DEP_MASK) {
pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA); pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA); pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
} }
...@@ -1170,16 +1328,20 @@ static void pn533_poll_create_mod_list(struct pn533 *dev, ...@@ -1170,16 +1328,20 @@ static void pn533_poll_create_mod_list(struct pn533 *dev,
pn533_poll_add_mod(dev, PN533_LISTEN_MOD); pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
} }
static int pn533_start_poll_complete(struct pn533 *dev, u8 *params, int params_len) static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
{ {
struct pn533_poll_response *resp; u8 nbtg, tg, *tgdata;
int rc; int rc, tgdata_len;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__); nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
resp = (struct pn533_poll_response *) params; nbtg = resp->data[0];
if (resp->nbtg) { tg = resp->data[1];
rc = pn533_target_found(dev, resp, params_len); tgdata = &resp->data[2];
tgdata_len = resp->len - 2; /* nbtg + tg */
if (nbtg) {
rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
/* We must stop the poll after a valid target found */ /* We must stop the poll after a valid target found */
if (rc == 0) { if (rc == 0) {
...@@ -1191,158 +1353,134 @@ static int pn533_start_poll_complete(struct pn533 *dev, u8 *params, int params_l ...@@ -1191,158 +1353,134 @@ static int pn533_start_poll_complete(struct pn533 *dev, u8 *params, int params_l
return -EAGAIN; return -EAGAIN;
} }
static int pn533_init_target_frame(struct pn533_frame *frame, static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
u8 *gb, size_t gb_len)
{ {
struct pn533_cmd_init_target *cmd; struct sk_buff *skb;
size_t cmd_len; u8 *felica, *nfcid3, *gb;
u8 *gbytes = dev->gb;
size_t gbytes_len = dev->gb_len;
u8 felica_params[18] = {0x1, 0xfe, /* DEP */ u8 felica_params[18] = {0x1, 0xfe, /* DEP */
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0xff, 0xff}; /* System code */ 0xff, 0xff}; /* System code */
u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */ u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x40}; /* SEL_RES for DEP */ 0x40}; /* SEL_RES for DEP */
cmd_len = sizeof(struct pn533_cmd_init_target) + gb_len + 1; unsigned int skb_len = 36 + /* mode (1), mifare (6),
cmd = kzalloc(cmd_len, GFP_KERNEL); felica (18), nfcid3 (10), gb_len (1) */
if (cmd == NULL) gbytes_len +
return -ENOMEM; 1; /* len Tk*/
pn533_tx_frame_init(frame, PN533_CMD_TG_INIT_AS_TARGET); skb = pn533_alloc_skb(dev, skb_len);
if (!skb)
return NULL;
/* DEP support only */ /* DEP support only */
cmd->mode |= PN533_INIT_TARGET_DEP; *skb_put(skb, 1) |= PN533_INIT_TARGET_DEP;
/* MIFARE params */
memcpy(skb_put(skb, 6), mifare_params, 6);
/* Felica params */ /* Felica params */
memcpy(cmd->felica, felica_params, 18); felica = skb_put(skb, 18);
get_random_bytes(cmd->felica + 2, 6); memcpy(felica, felica_params, 18);
get_random_bytes(felica + 2, 6);
/* NFCID3 */ /* NFCID3 */
memset(cmd->nfcid3, 0, 10); nfcid3 = skb_put(skb, 10);
memcpy(cmd->nfcid3, cmd->felica, 8); memset(nfcid3, 0, 10);
memcpy(nfcid3, felica, 8);
/* MIFARE params */
memcpy(cmd->mifare, mifare_params, 6);
/* General bytes */ /* General bytes */
cmd->gb_len = gb_len; *skb_put(skb, 1) = gbytes_len;
memcpy(cmd->gb, gb, gb_len);
/* Len Tk */
cmd->gb[gb_len] = 0;
memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), cmd, cmd_len);
frame->datalen += cmd_len; gb = skb_put(skb, gbytes_len);
memcpy(gb, gbytes, gbytes_len);
pn533_tx_frame_finish(frame); /* Len Tk */
*skb_put(skb, 1) = 0;
kfree(cmd);
return 0; return skb;
} }
#define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3) #define PN533_CMD_DATAEXCH_HEAD_LEN 1
#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg, static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
u8 *params, int params_len) struct sk_buff *resp)
{ {
struct sk_buff *skb_resp = arg; u8 status;
struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__); nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
if (params_len < 0) { if (IS_ERR(resp))
nfc_dev_err(&dev->interface->dev, return PTR_ERR(resp);
"Error %d when starting as a target",
params_len);
return params_len; status = resp->data[0];
} skb_pull(resp, sizeof(status));
if (params_len > 0 && params[0] != 0) { if (status != 0) {
nfc_tm_deactivated(dev->nfc_dev); nfc_tm_deactivated(dev->nfc_dev);
dev->tgt_mode = 0; dev->tgt_mode = 0;
dev_kfree_skb(resp);
kfree_skb(skb_resp);
return 0; return 0;
} }
skb_put(skb_resp, PN533_FRAME_SIZE(in_frame)); return nfc_tm_data_received(dev->nfc_dev, resp);
skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
return nfc_tm_data_received(dev->nfc_dev, skb_resp);
} }
static void pn533_wq_tg_get_data(struct work_struct *work) static void pn533_wq_tg_get_data(struct work_struct *work)
{ {
struct pn533 *dev = container_of(work, struct pn533, tg_work); struct pn533 *dev = container_of(work, struct pn533, tg_work);
struct pn533_frame *in_frame;
struct sk_buff *skb_resp;
size_t skb_resp_len;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__); struct sk_buff *skb;
int rc;
skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN + nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
PN533_CMD_DATAEXCH_DATA_MAXLEN +
PN533_FRAME_TAIL_SIZE;
skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL); skb = pn533_alloc_skb(dev, 0);
if (!skb_resp) if (!skb)
return; return;
in_frame = (struct pn533_frame *)skb_resp->data; rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
pn533_tm_get_data_complete, NULL);
pn533_tx_frame_init(dev->out_frame, PN533_CMD_TG_GET_DATA); if (rc < 0)
pn533_tx_frame_finish(dev->out_frame); dev_kfree_skb(skb);
pn533_send_cmd_frame_async(dev, dev->out_frame, in_frame,
skb_resp_len,
pn533_tm_get_data_complete,
skb_resp, GFP_KERNEL);
return; return;
} }
#define ATR_REQ_GB_OFFSET 17 #define ATR_REQ_GB_OFFSET 17
static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_len) static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
{ {
struct pn533_cmd_init_target_response *resp; u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
size_t gb_len; size_t gb_len;
int rc; int rc;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__); nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
if (params_len < 0) { if (resp->len < ATR_REQ_GB_OFFSET + 1)
nfc_dev_err(&dev->interface->dev,
"Error %d when starting as a target",
params_len);
return params_len;
}
if (params_len < ATR_REQ_GB_OFFSET + 1)
return -EINVAL; return -EINVAL;
resp = (struct pn533_cmd_init_target_response *) params; mode = resp->data[0];
cmd = &resp->data[1];
nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n", nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
resp->mode, params_len); mode, resp->len);
frame = resp->mode & PN533_INIT_TARGET_RESP_FRAME_MASK; if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
if (frame == PN533_INIT_TARGET_RESP_ACTIVE) PN533_INIT_TARGET_RESP_ACTIVE)
comm_mode = NFC_COMM_ACTIVE; comm_mode = NFC_COMM_ACTIVE;
/* Again, only DEP */ if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
if ((resp->mode & PN533_INIT_TARGET_RESP_DEP) == 0)
return -EOPNOTSUPP; return -EOPNOTSUPP;
gb = resp->cmd + ATR_REQ_GB_OFFSET; gb = cmd + ATR_REQ_GB_OFFSET;
gb_len = params_len - (ATR_REQ_GB_OFFSET + 1); gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK, rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
comm_mode, gb, gb_len); comm_mode, gb, gb_len);
...@@ -1353,7 +1491,6 @@ static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_ ...@@ -1353,7 +1491,6 @@ static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_
} }
dev->tgt_mode = 1; dev->tgt_mode = 1;
queue_work(dev->wq, &dev->tg_work); queue_work(dev->wq, &dev->tg_work);
return 0; return 0;
...@@ -1361,7 +1498,7 @@ static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_ ...@@ -1361,7 +1498,7 @@ static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_
static void pn533_listen_mode_timer(unsigned long data) static void pn533_listen_mode_timer(unsigned long data)
{ {
struct pn533 *dev = (struct pn533 *) data; struct pn533 *dev = (struct pn533 *)data;
nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout"); nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
...@@ -1376,88 +1513,104 @@ static void pn533_listen_mode_timer(unsigned long data) ...@@ -1376,88 +1513,104 @@ static void pn533_listen_mode_timer(unsigned long data)
} }
static int pn533_poll_complete(struct pn533 *dev, void *arg, static int pn533_poll_complete(struct pn533 *dev, void *arg,
u8 *params, int params_len) struct sk_buff *resp)
{ {
struct pn533_poll_modulations *cur_mod; struct pn533_poll_modulations *cur_mod;
int rc; int rc;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__); nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
if (params_len == -ENOENT) { if (IS_ERR(resp)) {
if (dev->poll_mod_count != 0) rc = PTR_ERR(resp);
return 0;
nfc_dev_err(&dev->interface->dev,
"Polling operation has been stopped");
goto stop_poll;
}
if (params_len < 0) { nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
nfc_dev_err(&dev->interface->dev, __func__, rc);
"Error %d when running poll", params_len);
goto stop_poll; if (rc == -ENOENT) {
if (dev->poll_mod_count != 0)
return rc;
else
goto stop_poll;
} else if (rc < 0) {
nfc_dev_err(&dev->interface->dev,
"Error %d when running poll", rc);
goto stop_poll;
}
} }
cur_mod = dev->poll_mod_active[dev->poll_mod_curr]; cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
if (cur_mod->len == 0) { if (cur_mod->len == 0) { /* Target mode */
del_timer(&dev->listen_timer); del_timer(&dev->listen_timer);
rc = pn533_init_target_complete(dev, resp);
return pn533_init_target_complete(dev, params, params_len); goto done;
} else {
rc = pn533_start_poll_complete(dev, params, params_len);
if (!rc)
return rc;
} }
pn533_poll_next_mod(dev); /* Initiator mode */
rc = pn533_start_poll_complete(dev, resp);
if (!rc)
goto done;
pn533_poll_next_mod(dev);
queue_work(dev->wq, &dev->poll_work); queue_work(dev->wq, &dev->poll_work);
return 0; done:
dev_kfree_skb(resp);
return rc;
stop_poll: stop_poll:
nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
pn533_poll_reset_mod_list(dev); pn533_poll_reset_mod_list(dev);
dev->poll_protocols = 0; dev->poll_protocols = 0;
return 0; return rc;
} }
static void pn533_build_poll_frame(struct pn533 *dev, static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
struct pn533_frame *frame, struct pn533_poll_modulations *mod)
struct pn533_poll_modulations *mod)
{ {
nfc_dev_dbg(&dev->interface->dev, "mod len %d\n", mod->len); struct sk_buff *skb;
if (mod->len == 0) { skb = pn533_alloc_skb(dev, mod->len);
/* Listen mode */ if (!skb)
pn533_init_target_frame(frame, dev->gb, dev->gb_len); return NULL;
} else {
/* Polling mode */
pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len); memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
frame->datalen += mod->len;
pn533_tx_frame_finish(frame); return skb;
}
} }
static int pn533_send_poll_frame(struct pn533 *dev) static int pn533_send_poll_frame(struct pn533 *dev)
{ {
struct pn533_poll_modulations *cur_mod; struct pn533_poll_modulations *mod;
struct sk_buff *skb;
int rc; int rc;
u8 cmd_code;
cur_mod = dev->poll_mod_active[dev->poll_mod_curr]; mod = dev->poll_mod_active[dev->poll_mod_curr];
pn533_build_poll_frame(dev, dev->out_frame, cur_mod); nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
__func__, mod->len);
rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame, if (mod->len == 0) { /* Listen mode */
dev->in_maxlen, pn533_poll_complete, cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
NULL, GFP_KERNEL); skb = pn533_alloc_poll_tg_frame(dev);
if (rc) } else { /* Polling mode */
cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
skb = pn533_alloc_poll_in_frame(dev, mod);
}
if (!skb) {
nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
return -ENOMEM;
}
rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
NULL);
if (rc < 0) {
dev_kfree_skb(skb);
nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc); nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
}
return rc; return rc;
} }
...@@ -1533,8 +1686,8 @@ static void pn533_stop_poll(struct nfc_dev *nfc_dev) ...@@ -1533,8 +1686,8 @@ static void pn533_stop_poll(struct nfc_dev *nfc_dev)
del_timer(&dev->listen_timer); del_timer(&dev->listen_timer);
if (!dev->poll_mod_count) { if (!dev->poll_mod_count) {
nfc_dev_dbg(&dev->interface->dev, "Polling operation was not" nfc_dev_dbg(&dev->interface->dev,
" running"); "Polling operation was not running");
return; return;
} }
...@@ -1549,38 +1702,38 @@ static void pn533_stop_poll(struct nfc_dev *nfc_dev) ...@@ -1549,38 +1702,38 @@ static void pn533_stop_poll(struct nfc_dev *nfc_dev)
static int pn533_activate_target_nfcdep(struct pn533 *dev) static int pn533_activate_target_nfcdep(struct pn533 *dev)
{ {
struct pn533_cmd_activate_param param; struct pn533_cmd_activate_response *rsp;
struct pn533_cmd_activate_response *resp;
u16 gt_len; u16 gt_len;
int rc; int rc;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__); struct sk_buff *skb;
struct sk_buff *resp;
pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR); nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
param.tg = 1; skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
param.next = 0; if (!skb)
memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &param, return -ENOMEM;
sizeof(struct pn533_cmd_activate_param));
dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
pn533_tx_frame_finish(dev->out_frame); *skb_put(skb, sizeof(u8)) = 1; /* TG */
*skb_put(skb, sizeof(u8)) = 0; /* Next */
rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame, resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
dev->in_maxlen); if (IS_ERR(resp))
if (rc) return PTR_ERR(resp);
return rc;
resp = (struct pn533_cmd_activate_response *) rsp = (struct pn533_cmd_activate_response *)resp->data;
PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame); rc = rsp->status & PN533_CMD_RET_MASK;
rc = resp->status & PN533_CMD_RET_MASK; if (rc != PN533_CMD_RET_SUCCESS) {
if (rc != PN533_CMD_RET_SUCCESS) dev_kfree_skb(resp);
return -EIO; return -EIO;
}
/* ATR_RES general bytes are located at offset 16 */ /* ATR_RES general bytes are located at offset 16 */
gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 16; gt_len = resp->len - 16;
rc = nfc_set_remote_general_bytes(dev->nfc_dev, resp->gt, gt_len); rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
dev_kfree_skb(resp);
return rc; return rc;
} }
...@@ -1591,38 +1744,38 @@ static int pn533_activate_target(struct nfc_dev *nfc_dev, ...@@ -1591,38 +1744,38 @@ static int pn533_activate_target(struct nfc_dev *nfc_dev,
int rc; int rc;
nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__, nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
protocol); protocol);
if (dev->poll_mod_count) { if (dev->poll_mod_count) {
nfc_dev_err(&dev->interface->dev, "Cannot activate while" nfc_dev_err(&dev->interface->dev,
" polling"); "Cannot activate while polling");
return -EBUSY; return -EBUSY;
} }
if (dev->tgt_active_prot) { if (dev->tgt_active_prot) {
nfc_dev_err(&dev->interface->dev, "There is already an active" nfc_dev_err(&dev->interface->dev,
" target"); "There is already an active target");
return -EBUSY; return -EBUSY;
} }
if (!dev->tgt_available_prots) { if (!dev->tgt_available_prots) {
nfc_dev_err(&dev->interface->dev, "There is no available target" nfc_dev_err(&dev->interface->dev,
" to activate"); "There is no available target to activate");
return -EINVAL; return -EINVAL;
} }
if (!(dev->tgt_available_prots & (1 << protocol))) { if (!(dev->tgt_available_prots & (1 << protocol))) {
nfc_dev_err(&dev->interface->dev, "The target does not support" nfc_dev_err(&dev->interface->dev,
" the requested protocol %u", protocol); "Target doesn't support requested proto %u",
protocol);
return -EINVAL; return -EINVAL;
} }
if (protocol == NFC_PROTO_NFC_DEP) { if (protocol == NFC_PROTO_NFC_DEP) {
rc = pn533_activate_target_nfcdep(dev); rc = pn533_activate_target_nfcdep(dev);
if (rc) { if (rc) {
nfc_dev_err(&dev->interface->dev, "Error %d when" nfc_dev_err(&dev->interface->dev,
" activating target with" "Activating target with DEP failed %d", rc);
" NFC_DEP protocol", rc);
return rc; return rc;
} }
} }
...@@ -1637,8 +1790,10 @@ static void pn533_deactivate_target(struct nfc_dev *nfc_dev, ...@@ -1637,8 +1790,10 @@ static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
struct nfc_target *target) struct nfc_target *target)
{ {
struct pn533 *dev = nfc_get_drvdata(nfc_dev); struct pn533 *dev = nfc_get_drvdata(nfc_dev);
u8 tg;
u8 status; struct sk_buff *skb;
struct sk_buff *resp;
int rc; int rc;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__); nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
...@@ -1649,83 +1804,69 @@ static void pn533_deactivate_target(struct nfc_dev *nfc_dev, ...@@ -1649,83 +1804,69 @@ static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
} }
dev->tgt_active_prot = 0; dev->tgt_active_prot = 0;
skb_queue_purge(&dev->resp_q); skb_queue_purge(&dev->resp_q);
pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE); skb = pn533_alloc_skb(dev, sizeof(u8));
if (!skb)
tg = 1; return;
memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
dev->out_frame->datalen += sizeof(u8);
pn533_tx_frame_finish(dev->out_frame); *skb_put(skb, 1) = 1; /* TG*/
rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame, resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
dev->in_maxlen); if (IS_ERR(resp))
if (rc) {
nfc_dev_err(&dev->interface->dev, "Error when sending release"
" command to the controller");
return; return;
}
status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0]; rc = resp->data[0] & PN533_CMD_RET_MASK;
rc = status & PN533_CMD_RET_MASK;
if (rc != PN533_CMD_RET_SUCCESS) if (rc != PN533_CMD_RET_SUCCESS)
nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing" nfc_dev_err(&dev->interface->dev,
" the target", rc); "Error 0x%x when releasing the target", rc);
dev_kfree_skb(resp);
return; return;
} }
static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg, static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
u8 *params, int params_len) struct sk_buff *resp)
{ {
struct pn533_cmd_jump_dep_response *resp; struct pn533_cmd_jump_dep_response *rsp;
struct nfc_target nfc_target;
u8 target_gt_len; u8 target_gt_len;
int rc; int rc;
struct pn533_cmd_jump_dep *cmd = (struct pn533_cmd_jump_dep *)arg; u8 active = *(u8 *)arg;
u8 active = cmd->active;
kfree(arg); kfree(arg);
if (params_len == -ENOENT) { if (IS_ERR(resp))
nfc_dev_dbg(&dev->interface->dev, ""); return PTR_ERR(resp);
return 0;
}
if (params_len < 0) {
nfc_dev_err(&dev->interface->dev,
"Error %d when bringing DEP link up",
params_len);
return 0;
}
if (dev->tgt_available_prots && if (dev->tgt_available_prots &&
!(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) { !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
nfc_dev_err(&dev->interface->dev, nfc_dev_err(&dev->interface->dev,
"The target does not support DEP"); "The target does not support DEP");
return -EINVAL; rc = -EINVAL;
goto error;
} }
resp = (struct pn533_cmd_jump_dep_response *) params; rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
rc = resp->status & PN533_CMD_RET_MASK;
rc = rsp->status & PN533_CMD_RET_MASK;
if (rc != PN533_CMD_RET_SUCCESS) { if (rc != PN533_CMD_RET_SUCCESS) {
nfc_dev_err(&dev->interface->dev, nfc_dev_err(&dev->interface->dev,
"Bringing DEP link up failed %d", rc); "Bringing DEP link up failed %d", rc);
return 0; goto error;
} }
if (!dev->tgt_available_prots) { if (!dev->tgt_available_prots) {
struct nfc_target nfc_target;
nfc_dev_dbg(&dev->interface->dev, "Creating new target"); nfc_dev_dbg(&dev->interface->dev, "Creating new target");
nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK; nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
nfc_target.nfcid1_len = 10; nfc_target.nfcid1_len = 10;
memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len); memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1); rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
if (rc) if (rc)
return 0; goto error;
dev->tgt_available_prots = 0; dev->tgt_available_prots = 0;
} }
...@@ -1733,15 +1874,17 @@ static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg, ...@@ -1733,15 +1874,17 @@ static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
dev->tgt_active_prot = NFC_PROTO_NFC_DEP; dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
/* ATR_RES general bytes are located at offset 17 */ /* ATR_RES general bytes are located at offset 17 */
target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17; target_gt_len = resp->len - 17;
rc = nfc_set_remote_general_bytes(dev->nfc_dev, rc = nfc_set_remote_general_bytes(dev->nfc_dev,
resp->gt, target_gt_len); rsp->gt, target_gt_len);
if (rc == 0) if (rc == 0)
rc = nfc_dep_link_is_up(dev->nfc_dev, rc = nfc_dep_link_is_up(dev->nfc_dev,
dev->nfc_dev->targets[0].idx, dev->nfc_dev->targets[0].idx,
!active, NFC_RF_INITIATOR); !active, NFC_RF_INITIATOR);
return 0; error:
dev_kfree_skb(resp);
return rc;
} }
static int pn533_mod_to_baud(struct pn533 *dev) static int pn533_mod_to_baud(struct pn533 *dev)
...@@ -1760,25 +1903,26 @@ static int pn533_mod_to_baud(struct pn533 *dev) ...@@ -1760,25 +1903,26 @@ static int pn533_mod_to_baud(struct pn533 *dev)
#define PASSIVE_DATA_LEN 5 #define PASSIVE_DATA_LEN 5
static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target, static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
u8 comm_mode, u8* gb, size_t gb_len) u8 comm_mode, u8 *gb, size_t gb_len)
{ {
struct pn533 *dev = nfc_get_drvdata(nfc_dev); struct pn533 *dev = nfc_get_drvdata(nfc_dev);
struct pn533_cmd_jump_dep *cmd; struct sk_buff *skb;
u8 cmd_len, *data_ptr; int rc, baud, skb_len;
u8 *next, *arg;
u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3}; u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
int rc, baud;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__); nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
if (dev->poll_mod_count) { if (dev->poll_mod_count) {
nfc_dev_err(&dev->interface->dev, nfc_dev_err(&dev->interface->dev,
"Cannot bring the DEP link up while polling"); "Cannot bring the DEP link up while polling");
return -EBUSY; return -EBUSY;
} }
if (dev->tgt_active_prot) { if (dev->tgt_active_prot) {
nfc_dev_err(&dev->interface->dev, nfc_dev_err(&dev->interface->dev,
"There is already an active target"); "There is already an active target");
return -EBUSY; return -EBUSY;
} }
...@@ -1789,43 +1933,48 @@ static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target, ...@@ -1789,43 +1933,48 @@ static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
return baud; return baud;
} }
cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len; skb_len = 3 + gb_len; /* ActPass + BR + Next */
if (comm_mode == NFC_COMM_PASSIVE) if (comm_mode == NFC_COMM_PASSIVE)
cmd_len += PASSIVE_DATA_LEN; skb_len += PASSIVE_DATA_LEN;
cmd = kzalloc(cmd_len, GFP_KERNEL); skb = pn533_alloc_skb(dev, skb_len);
if (cmd == NULL) if (!skb)
return -ENOMEM; return -ENOMEM;
pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP); *skb_put(skb, 1) = !comm_mode; /* ActPass */
*skb_put(skb, 1) = baud; /* Baud rate */
cmd->active = !comm_mode; next = skb_put(skb, 1); /* Next */
cmd->next = 0; *next = 0;
cmd->baud = baud;
data_ptr = cmd->data; if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
if (comm_mode == NFC_COMM_PASSIVE && cmd->baud > 0) { memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
memcpy(data_ptr, passive_data, PASSIVE_DATA_LEN); PASSIVE_DATA_LEN);
cmd->next |= 1; *next |= 1;
data_ptr += PASSIVE_DATA_LEN;
} }
if (gb != NULL && gb_len > 0) { if (gb != NULL && gb_len > 0) {
cmd->next |= 4; /* We have some Gi */ memcpy(skb_put(skb, gb_len), gb, gb_len);
memcpy(data_ptr, gb, gb_len); *next |= 4; /* We have some Gi */
} else { } else {
cmd->next = 0; *next = 0;
} }
memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len); arg = kmalloc(sizeof(*arg), GFP_KERNEL);
dev->out_frame->datalen += cmd_len; if (!arg) {
dev_kfree_skb(skb);
return -ENOMEM;
}
pn533_tx_frame_finish(dev->out_frame); *arg = !comm_mode;
rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame, rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
dev->in_maxlen, pn533_in_dep_link_up_complete, pn533_in_dep_link_up_complete, arg);
cmd, GFP_KERNEL);
if (rc < 0) if (rc < 0) {
kfree(cmd); dev_kfree_skb(skb);
kfree(arg);
}
return rc; return rc;
} }
...@@ -1834,6 +1983,8 @@ static int pn533_dep_link_down(struct nfc_dev *nfc_dev) ...@@ -1834,6 +1983,8 @@ static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
{ {
struct pn533 *dev = nfc_get_drvdata(nfc_dev); struct pn533 *dev = nfc_get_drvdata(nfc_dev);
nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
pn533_poll_reset_mod_list(dev); pn533_poll_reset_mod_list(dev);
if (dev->tgt_mode || dev->tgt_active_prot) { if (dev->tgt_mode || dev->tgt_active_prot) {
...@@ -1849,68 +2000,7 @@ static int pn533_dep_link_down(struct nfc_dev *nfc_dev) ...@@ -1849,68 +2000,7 @@ static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
return 0; return 0;
} }
static int pn533_build_tx_frame(struct pn533 *dev, struct sk_buff *skb,
bool target)
{
int payload_len = skb->len;
struct pn533_frame *out_frame;
u8 tg;
nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
payload_len);
if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
/* TODO: Implement support to multi-part data exchange */
nfc_dev_err(&dev->interface->dev, "Data length greater than the"
" max allowed: %d",
PN533_CMD_DATAEXCH_DATA_MAXLEN);
return -ENOSYS;
}
if (target == true) {
switch (dev->device_type) {
case PN533_DEVICE_PASORI:
if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
out_frame = (struct pn533_frame *) skb->data;
pn533_tx_frame_init(out_frame,
PN533_CMD_IN_COMM_THRU);
break;
}
default:
skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
out_frame = (struct pn533_frame *) skb->data;
pn533_tx_frame_init(out_frame,
PN533_CMD_IN_DATA_EXCHANGE);
tg = 1;
memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame),
&tg, sizeof(u8));
out_frame->datalen += sizeof(u8);
break;
}
} else {
skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
out_frame = (struct pn533_frame *) skb->data;
pn533_tx_frame_init(out_frame, PN533_CMD_TG_SET_DATA);
}
/* The data is already in the out_frame, just update the datalen */
out_frame->datalen += payload_len;
pn533_tx_frame_finish(out_frame);
skb_put(skb, PN533_FRAME_TAIL_SIZE);
return 0;
}
struct pn533_data_exchange_arg { struct pn533_data_exchange_arg {
struct sk_buff *skb_resp;
struct sk_buff *skb_out;
data_exchange_cb_t cb; data_exchange_cb_t cb;
void *cb_context; void *cb_context;
}; };
...@@ -1920,7 +2010,7 @@ static struct sk_buff *pn533_build_response(struct pn533 *dev) ...@@ -1920,7 +2010,7 @@ static struct sk_buff *pn533_build_response(struct pn533 *dev)
struct sk_buff *skb, *tmp, *t; struct sk_buff *skb, *tmp, *t;
unsigned int skb_len = 0, tmp_len = 0; unsigned int skb_len = 0, tmp_len = 0;
nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__); nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
if (skb_queue_empty(&dev->resp_q)) if (skb_queue_empty(&dev->resp_q))
return NULL; return NULL;
...@@ -1954,46 +2044,44 @@ static struct sk_buff *pn533_build_response(struct pn533 *dev) ...@@ -1954,46 +2044,44 @@ static struct sk_buff *pn533_build_response(struct pn533 *dev)
} }
static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg, static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
u8 *params, int params_len) struct sk_buff *resp)
{ {
struct pn533_data_exchange_arg *arg = _arg; struct pn533_data_exchange_arg *arg = _arg;
struct sk_buff *skb = NULL, *skb_resp = arg->skb_resp; struct sk_buff *skb;
struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data; int rc = 0;
int err = 0; u8 status, ret, mi;
u8 status;
u8 cmd_ret;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__); nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
dev_kfree_skb(arg->skb_out); if (IS_ERR(resp)) {
rc = PTR_ERR(resp);
if (params_len < 0) { /* error */ goto _error;
err = params_len;
goto error;
} }
status = params[0]; status = resp->data[0];
ret = status & PN533_CMD_RET_MASK;
mi = status & PN533_CMD_MI_MASK;
skb_pull(resp, sizeof(status));
cmd_ret = status & PN533_CMD_RET_MASK; if (ret != PN533_CMD_RET_SUCCESS) {
if (cmd_ret != PN533_CMD_RET_SUCCESS) { nfc_dev_err(&dev->interface->dev,
nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when" "PN533 reported error %d when exchanging data",
" exchanging data", cmd_ret); ret);
err = -EIO; rc = -EIO;
goto error; goto error;
} }
skb_put(skb_resp, PN533_FRAME_SIZE(in_frame)); skb_queue_tail(&dev->resp_q, resp);
skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
skb_queue_tail(&dev->resp_q, skb_resp);
if (status & PN533_CMD_MI_MASK) { if (mi) {
dev->cmd_complete_mi_arg = arg;
queue_work(dev->wq, &dev->mi_work); queue_work(dev->wq, &dev->mi_work);
return -EINPROGRESS; return -EINPROGRESS;
} }
skb = pn533_build_response(dev); skb = pn533_build_response(dev);
if (skb == NULL) if (!skb)
goto error; goto error;
arg->cb(arg->cb_context, skb, 0); arg->cb(arg->cb_context, skb, 0);
...@@ -2001,11 +2089,12 @@ static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg, ...@@ -2001,11 +2089,12 @@ static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
return 0; return 0;
error: error:
dev_kfree_skb(resp);
_error:
skb_queue_purge(&dev->resp_q); skb_queue_purge(&dev->resp_q);
dev_kfree_skb(skb_resp); arg->cb(arg->cb_context, NULL, rc);
arg->cb(arg->cb_context, NULL, err);
kfree(arg); kfree(arg);
return 0; return rc;
} }
static int pn533_transceive(struct nfc_dev *nfc_dev, static int pn533_transceive(struct nfc_dev *nfc_dev,
...@@ -2013,87 +2102,82 @@ static int pn533_transceive(struct nfc_dev *nfc_dev, ...@@ -2013,87 +2102,82 @@ static int pn533_transceive(struct nfc_dev *nfc_dev,
data_exchange_cb_t cb, void *cb_context) data_exchange_cb_t cb, void *cb_context)
{ {
struct pn533 *dev = nfc_get_drvdata(nfc_dev); struct pn533 *dev = nfc_get_drvdata(nfc_dev);
struct pn533_frame *out_frame, *in_frame; struct pn533_data_exchange_arg *arg = NULL;
struct pn533_data_exchange_arg *arg;
struct sk_buff *skb_resp;
int skb_resp_len;
int rc; int rc;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__); nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
if (!dev->tgt_active_prot) { if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
nfc_dev_err(&dev->interface->dev, "Cannot exchange data if" /* TODO: Implement support to multi-part data exchange */
" there is no active target"); nfc_dev_err(&dev->interface->dev,
rc = -EINVAL; "Data length greater than the max allowed: %d",
PN533_CMD_DATAEXCH_DATA_MAXLEN);
rc = -ENOSYS;
goto error; goto error;
} }
rc = pn533_build_tx_frame(dev, skb, true); if (!dev->tgt_active_prot) {
if (rc) nfc_dev_err(&dev->interface->dev,
goto error; "Can't exchange data if there is no active target");
rc = -EINVAL;
skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
PN533_CMD_DATAEXCH_DATA_MAXLEN +
PN533_FRAME_TAIL_SIZE;
skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
if (!skb_resp) {
rc = -ENOMEM;
goto error; goto error;
} }
in_frame = (struct pn533_frame *) skb_resp->data; arg = kmalloc(sizeof(*arg), GFP_KERNEL);
out_frame = (struct pn533_frame *) skb->data;
arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
if (!arg) { if (!arg) {
rc = -ENOMEM; rc = -ENOMEM;
goto free_skb_resp; goto error;
} }
arg->skb_resp = skb_resp;
arg->skb_out = skb;
arg->cb = cb; arg->cb = cb;
arg->cb_context = cb_context; arg->cb_context = cb_context;
rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len, switch (dev->device_type) {
pn533_data_exchange_complete, arg, case PN533_DEVICE_PASORI:
GFP_KERNEL); if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
if (rc) { rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
nfc_dev_err(&dev->interface->dev, "Error %d when trying to" skb,
" perform data_exchange", rc); pn533_data_exchange_complete,
goto free_arg; arg);
break;
}
default:
*skb_push(skb, sizeof(u8)) = 1; /*TG*/
rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
skb, pn533_data_exchange_complete,
arg);
break;
} }
if (rc < 0) /* rc from send_async */
goto error;
return 0; return 0;
free_arg:
kfree(arg);
free_skb_resp:
kfree_skb(skb_resp);
error: error:
kfree_skb(skb); kfree(arg);
dev_kfree_skb(skb);
return rc; return rc;
} }
static int pn533_tm_send_complete(struct pn533 *dev, void *arg, static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
u8 *params, int params_len) struct sk_buff *resp)
{ {
struct sk_buff *skb_out = arg; u8 status;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__); nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
dev_kfree_skb(skb_out); if (IS_ERR(resp))
return PTR_ERR(resp);
if (params_len < 0) { status = resp->data[0];
nfc_dev_err(&dev->interface->dev,
"Error %d when sending data",
params_len);
return params_len; dev_kfree_skb(resp);
}
if (params_len > 0 && params[0] != 0) { if (status != 0) {
nfc_tm_deactivated(dev->nfc_dev); nfc_tm_deactivated(dev->nfc_dev);
dev->tgt_mode = 0; dev->tgt_mode = 0;
...@@ -2109,30 +2193,21 @@ static int pn533_tm_send_complete(struct pn533 *dev, void *arg, ...@@ -2109,30 +2193,21 @@ static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb) static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
{ {
struct pn533 *dev = nfc_get_drvdata(nfc_dev); struct pn533 *dev = nfc_get_drvdata(nfc_dev);
struct pn533_frame *out_frame;
int rc; int rc;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__); nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
rc = pn533_build_tx_frame(dev, skb, false); if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
if (rc)
goto error;
out_frame = (struct pn533_frame *) skb->data;
rc = pn533_send_cmd_frame_async(dev, out_frame, dev->in_frame,
dev->in_maxlen, pn533_tm_send_complete,
skb, GFP_KERNEL);
if (rc) {
nfc_dev_err(&dev->interface->dev, nfc_dev_err(&dev->interface->dev,
"Error %d when trying to send data", rc); "Data length greater than the max allowed: %d",
goto error; PN533_CMD_DATAEXCH_DATA_MAXLEN);
return -ENOSYS;
} }
return 0; rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
pn533_tm_send_complete, NULL);
error: if (rc < 0)
kfree_skb(skb); dev_kfree_skb(skb);
return rc; return rc;
} }
...@@ -2140,107 +2215,123 @@ static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb) ...@@ -2140,107 +2215,123 @@ static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
static void pn533_wq_mi_recv(struct work_struct *work) static void pn533_wq_mi_recv(struct work_struct *work)
{ {
struct pn533 *dev = container_of(work, struct pn533, mi_work); struct pn533 *dev = container_of(work, struct pn533, mi_work);
struct sk_buff *skb_cmd;
struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg; struct sk_buff *skb;
struct pn533_frame *out_frame, *in_frame;
struct sk_buff *skb_resp;
int skb_resp_len;
int rc; int rc;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__); nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
/* This is a zero payload size skb */ skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
skb_cmd = alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN + PN533_FRAME_TAIL_SIZE, if (!skb)
GFP_KERNEL); goto error;
if (skb_cmd == NULL)
goto error_cmd;
skb_reserve(skb_cmd, PN533_CMD_DATAEXCH_HEAD_LEN);
rc = pn533_build_tx_frame(dev, skb_cmd, true); switch (dev->device_type) {
if (rc) case PN533_DEVICE_PASORI:
goto error_frame; if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
rc = pn533_send_cmd_direct_async(dev,
PN533_CMD_IN_COMM_THRU,
skb,
pn533_data_exchange_complete,
dev->cmd_complete_mi_arg);
skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN + break;
PN533_CMD_DATAEXCH_DATA_MAXLEN + }
PN533_FRAME_TAIL_SIZE; default:
skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL); *skb_put(skb, sizeof(u8)) = 1; /*TG*/
if (!skb_resp) {
rc = -ENOMEM;
goto error_frame;
}
in_frame = (struct pn533_frame *) skb_resp->data; rc = pn533_send_cmd_direct_async(dev,
out_frame = (struct pn533_frame *) skb_cmd->data; PN533_CMD_IN_DATA_EXCHANGE,
skb,
pn533_data_exchange_complete,
dev->cmd_complete_mi_arg);
arg->skb_resp = skb_resp; break;
arg->skb_out = skb_cmd; }
rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame, if (rc == 0) /* success */
skb_resp_len,
pn533_data_exchange_complete,
dev->cmd_complete_arg, GFP_KERNEL);
if (!rc)
return; return;
nfc_dev_err(&dev->interface->dev, "Error %d when trying to" nfc_dev_err(&dev->interface->dev,
" perform data_exchange", rc); "Error %d when trying to perform data_exchange", rc);
kfree_skb(skb_resp);
error_frame: dev_kfree_skb(skb);
kfree_skb(skb_cmd); kfree(dev->cmd_complete_arg);
error_cmd: error:
pn533_send_ack(dev, GFP_KERNEL); pn533_send_ack(dev, GFP_KERNEL);
kfree(arg);
queue_work(dev->wq, &dev->cmd_work); queue_work(dev->wq, &dev->cmd_work);
} }
static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata, static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
u8 cfgdata_len) u8 cfgdata_len)
{ {
int rc; struct sk_buff *skb;
u8 *params; struct sk_buff *resp;
int skb_len;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__); nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION); skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame); skb = pn533_alloc_skb(dev, skb_len);
params[0] = cfgitem; if (!skb)
memcpy(&params[1], cfgdata, cfgdata_len); return -ENOMEM;
dev->out_frame->datalen += (1 + cfgdata_len);
pn533_tx_frame_finish(dev->out_frame); *skb_put(skb, sizeof(cfgitem)) = cfgitem;
memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame, resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
dev->in_maxlen); if (IS_ERR(resp))
return PTR_ERR(resp);
return rc; dev_kfree_skb(resp);
return 0;
}
static int pn533_get_firmware_version(struct pn533 *dev,
struct pn533_fw_version *fv)
{
struct sk_buff *skb;
struct sk_buff *resp;
skb = pn533_alloc_skb(dev, 0);
if (!skb)
return -ENOMEM;
resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
if (IS_ERR(resp))
return PTR_ERR(resp);
fv->ic = resp->data[0];
fv->ver = resp->data[1];
fv->rev = resp->data[2];
fv->support = resp->data[3];
dev_kfree_skb(resp);
return 0;
} }
static int pn533_fw_reset(struct pn533 *dev) static int pn533_fw_reset(struct pn533 *dev)
{ {
int rc; struct sk_buff *skb;
u8 *params; struct sk_buff *resp;
nfc_dev_dbg(&dev->interface->dev, "%s", __func__); nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
pn533_tx_frame_init(dev->out_frame, 0x18); skb = pn533_alloc_skb(dev, sizeof(u8));
if (!skb)
return -ENOMEM;
params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame); *skb_put(skb, sizeof(u8)) = 0x1;
params[0] = 0x1;
dev->out_frame->datalen += 1;
pn533_tx_frame_finish(dev->out_frame); resp = pn533_send_cmd_sync(dev, 0x18, skb);
if (IS_ERR(resp))
return PTR_ERR(resp);
rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame, dev_kfree_skb(resp);
dev->in_maxlen);
return rc; return 0;
} }
static struct nfc_ops pn533_nfc_ops = { static struct nfc_ops pn533_nfc_ops = {
...@@ -2337,7 +2428,7 @@ static int pn533_setup(struct pn533 *dev) ...@@ -2337,7 +2428,7 @@ static int pn533_setup(struct pn533 *dev)
static int pn533_probe(struct usb_interface *interface, static int pn533_probe(struct usb_interface *interface,
const struct usb_device_id *id) const struct usb_device_id *id)
{ {
struct pn533_fw_version *fw_ver; struct pn533_fw_version fw_ver;
struct pn533 *dev; struct pn533 *dev;
struct usb_host_interface *iface_desc; struct usb_host_interface *iface_desc;
struct usb_endpoint_descriptor *endpoint; struct usb_endpoint_descriptor *endpoint;
...@@ -2359,41 +2450,32 @@ static int pn533_probe(struct usb_interface *interface, ...@@ -2359,41 +2450,32 @@ static int pn533_probe(struct usb_interface *interface,
for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
endpoint = &iface_desc->endpoint[i].desc; endpoint = &iface_desc->endpoint[i].desc;
if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) { if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
dev->in_maxlen = le16_to_cpu(endpoint->wMaxPacketSize);
in_endpoint = endpoint->bEndpointAddress; in_endpoint = endpoint->bEndpointAddress;
}
if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) { if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
dev->out_maxlen =
le16_to_cpu(endpoint->wMaxPacketSize);
out_endpoint = endpoint->bEndpointAddress; out_endpoint = endpoint->bEndpointAddress;
}
} }
if (!in_endpoint || !out_endpoint) { if (!in_endpoint || !out_endpoint) {
nfc_dev_err(&interface->dev, "Could not find bulk-in or" nfc_dev_err(&interface->dev,
" bulk-out endpoint"); "Could not find bulk-in or bulk-out endpoint");
rc = -ENODEV; rc = -ENODEV;
goto error; goto error;
} }
dev->in_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
dev->in_urb = usb_alloc_urb(0, GFP_KERNEL); dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
dev->out_urb = usb_alloc_urb(0, GFP_KERNEL); dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
if (!dev->in_frame || !dev->out_frame || if (!dev->in_urb || !dev->out_urb)
!dev->in_urb || !dev->out_urb)
goto error; goto error;
usb_fill_bulk_urb(dev->in_urb, dev->udev, usb_fill_bulk_urb(dev->in_urb, dev->udev,
usb_rcvbulkpipe(dev->udev, in_endpoint), usb_rcvbulkpipe(dev->udev, in_endpoint),
NULL, 0, NULL, dev); NULL, 0, NULL, dev);
usb_fill_bulk_urb(dev->out_urb, dev->udev, usb_fill_bulk_urb(dev->out_urb, dev->udev,
usb_sndbulkpipe(dev->udev, out_endpoint), usb_sndbulkpipe(dev->udev, out_endpoint),
NULL, 0, NULL, 0, pn533_send_complete, dev);
pn533_send_complete, dev);
INIT_WORK(&dev->cmd_work, pn533_wq_cmd); INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete); INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
...@@ -2414,18 +2496,7 @@ static int pn533_probe(struct usb_interface *interface, ...@@ -2414,18 +2496,7 @@ static int pn533_probe(struct usb_interface *interface,
usb_set_intfdata(interface, dev); usb_set_intfdata(interface, dev);
pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION); dev->ops = &pn533_std_frame_ops;
pn533_tx_frame_finish(dev->out_frame);
rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
dev->in_maxlen);
if (rc)
goto destroy_wq;
fw_ver = (struct pn533_fw_version *)
PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
" attached", fw_ver->ver, fw_ver->rev);
dev->device_type = id->driver_info; dev->device_type = id->driver_info;
switch (dev->device_type) { switch (dev->device_type) {
...@@ -2444,9 +2515,21 @@ static int pn533_probe(struct usb_interface *interface, ...@@ -2444,9 +2515,21 @@ static int pn533_probe(struct usb_interface *interface,
goto destroy_wq; goto destroy_wq;
} }
memset(&fw_ver, 0, sizeof(fw_ver));
rc = pn533_get_firmware_version(dev, &fw_ver);
if (rc < 0)
goto destroy_wq;
nfc_dev_info(&dev->interface->dev,
"NXP PN533 firmware ver %d.%d now attached",
fw_ver.ver, fw_ver.rev);
dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols, dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
NFC_SE_NONE,
dev->ops->tx_header_len +
PN533_CMD_DATAEXCH_HEAD_LEN, PN533_CMD_DATAEXCH_HEAD_LEN,
PN533_FRAME_TAIL_SIZE); dev->ops->tx_tail_len);
if (!dev->nfc_dev) if (!dev->nfc_dev)
goto destroy_wq; goto destroy_wq;
...@@ -2472,9 +2555,7 @@ static int pn533_probe(struct usb_interface *interface, ...@@ -2472,9 +2555,7 @@ static int pn533_probe(struct usb_interface *interface,
destroy_wq: destroy_wq:
destroy_workqueue(dev->wq); destroy_workqueue(dev->wq);
error: error:
kfree(dev->in_frame);
usb_free_urb(dev->in_urb); usb_free_urb(dev->in_urb);
kfree(dev->out_frame);
usb_free_urb(dev->out_urb); usb_free_urb(dev->out_urb);
kfree(dev); kfree(dev);
return rc; return rc;
...@@ -2505,9 +2586,7 @@ static void pn533_disconnect(struct usb_interface *interface) ...@@ -2505,9 +2586,7 @@ static void pn533_disconnect(struct usb_interface *interface)
kfree(cmd); kfree(cmd);
} }
kfree(dev->in_frame);
usb_free_urb(dev->in_urb); usb_free_urb(dev->in_urb);
kfree(dev->out_frame);
usb_free_urb(dev->out_urb); usb_free_urb(dev->out_urb);
kfree(dev); kfree(dev);
......
config NFC_PN544
tristate "NXP PN544 NFC driver"
depends on NFC_HCI
select CRC_CCITT
default n
---help---
NXP PN544 core driver.
This is a driver based on the HCI NFC kernel layers and
will thus not work with NXP libnfc library.
To compile this driver as a module, choose m here. The module will
be called pn544.
Say N if unsure.
config NFC_PN544_I2C
tristate "NFC PN544 i2c support"
depends on NFC_PN544 && I2C && NFC_SHDLC
---help---
This module adds support for the NXP pn544 i2c interface.
Select this if your platform is using the i2c bus.
If you choose to build a module, it'll be called pn544_i2c.
Say N if unsure.
\ No newline at end of file
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
# Makefile for PN544 HCI based NFC driver # Makefile for PN544 HCI based NFC driver
# #
obj-$(CONFIG_PN544_HCI_NFC) += pn544_i2c.o pn544_i2c-objs = i2c.o
pn544_i2c-y := pn544.o i2c.o obj-$(CONFIG_NFC_PN544) += pn544.o
obj-$(CONFIG_NFC_PN544_I2C) += pn544_i2c.o
...@@ -376,12 +376,12 @@ static int __devinit pn544_hci_i2c_probe(struct i2c_client *client, ...@@ -376,12 +376,12 @@ static int __devinit pn544_hci_i2c_probe(struct i2c_client *client,
return -ENODEV; return -ENODEV;
} }
phy = kzalloc(sizeof(struct pn544_i2c_phy), GFP_KERNEL); phy = devm_kzalloc(&client->dev, sizeof(struct pn544_i2c_phy),
GFP_KERNEL);
if (!phy) { if (!phy) {
dev_err(&client->dev, dev_err(&client->dev,
"Cannot allocate memory for pn544 i2c phy.\n"); "Cannot allocate memory for pn544 i2c phy.\n");
r = -ENOMEM; return -ENOMEM;
goto err_phy_alloc;
} }
phy->i2c_dev = client; phy->i2c_dev = client;
...@@ -390,20 +390,18 @@ static int __devinit pn544_hci_i2c_probe(struct i2c_client *client, ...@@ -390,20 +390,18 @@ static int __devinit pn544_hci_i2c_probe(struct i2c_client *client,
pdata = client->dev.platform_data; pdata = client->dev.platform_data;
if (pdata == NULL) { if (pdata == NULL) {
dev_err(&client->dev, "No platform data\n"); dev_err(&client->dev, "No platform data\n");
r = -EINVAL; return -EINVAL;
goto err_pdata;
} }
if (pdata->request_resources == NULL) { if (pdata->request_resources == NULL) {
dev_err(&client->dev, "request_resources() missing\n"); dev_err(&client->dev, "request_resources() missing\n");
r = -EINVAL; return -EINVAL;
goto err_pdata;
} }
r = pdata->request_resources(client); r = pdata->request_resources(client);
if (r) { if (r) {
dev_err(&client->dev, "Cannot get platform resources\n"); dev_err(&client->dev, "Cannot get platform resources\n");
goto err_pdata; return r;
} }
phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE); phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE);
...@@ -435,10 +433,6 @@ static int __devinit pn544_hci_i2c_probe(struct i2c_client *client, ...@@ -435,10 +433,6 @@ static int __devinit pn544_hci_i2c_probe(struct i2c_client *client,
if (pdata->free_resources != NULL) if (pdata->free_resources != NULL)
pdata->free_resources(); pdata->free_resources();
err_pdata:
kfree(phy);
err_phy_alloc:
return r; return r;
} }
...@@ -458,8 +452,6 @@ static __devexit int pn544_hci_i2c_remove(struct i2c_client *client) ...@@ -458,8 +452,6 @@ static __devexit int pn544_hci_i2c_remove(struct i2c_client *client)
if (pdata->free_resources) if (pdata->free_resources)
pdata->free_resources(); pdata->free_resources();
kfree(phy);
return 0; return 0;
} }
...@@ -472,29 +464,7 @@ static struct i2c_driver pn544_hci_i2c_driver = { ...@@ -472,29 +464,7 @@ static struct i2c_driver pn544_hci_i2c_driver = {
.remove = __devexit_p(pn544_hci_i2c_remove), .remove = __devexit_p(pn544_hci_i2c_remove),
}; };
static int __init pn544_hci_i2c_init(void) module_i2c_driver(pn544_hci_i2c_driver);
{
int r;
pr_debug(DRIVER_DESC ": %s\n", __func__);
r = i2c_add_driver(&pn544_hci_i2c_driver);
if (r) {
pr_err(PN544_HCI_I2C_DRIVER_NAME
": driver registration failed\n");
return r;
}
return 0;
}
static void __exit pn544_hci_i2c_exit(void)
{
i2c_del_driver(&pn544_hci_i2c_driver);
}
module_init(pn544_hci_i2c_init);
module_exit(pn544_hci_i2c_exit);
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
MODULE_DESCRIPTION(DRIVER_DESC); MODULE_DESCRIPTION(DRIVER_DESC);
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/module.h>
#include <linux/nfc.h> #include <linux/nfc.h>
#include <net/nfc/hci.h> #include <net/nfc/hci.h>
...@@ -675,11 +676,17 @@ static int pn544_hci_im_transceive(struct nfc_hci_dev *hdev, ...@@ -675,11 +676,17 @@ static int pn544_hci_im_transceive(struct nfc_hci_dev *hdev,
static int pn544_hci_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb) static int pn544_hci_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
{ {
int r;
/* Set default false for multiple information chaining */ /* Set default false for multiple information chaining */
*skb_push(skb, 1) = 0; *skb_push(skb, 1) = 0;
return nfc_hci_send_event(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE, r = nfc_hci_send_event(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
PN544_HCI_EVT_SND_DATA, skb->data, skb->len); PN544_HCI_EVT_SND_DATA, skb->data, skb->len);
kfree_skb(skb);
return r;
} }
static int pn544_hci_check_presence(struct nfc_hci_dev *hdev, static int pn544_hci_check_presence(struct nfc_hci_dev *hdev,
...@@ -714,35 +721,40 @@ static int pn544_hci_check_presence(struct nfc_hci_dev *hdev, ...@@ -714,35 +721,40 @@ static int pn544_hci_check_presence(struct nfc_hci_dev *hdev,
return 0; return 0;
} }
static void pn544_hci_event_received(struct nfc_hci_dev *hdev, u8 gate, /*
u8 event, struct sk_buff *skb) * Returns:
* <= 0: driver handled the event, skb consumed
* 1: driver does not handle the event, please do standard processing
*/
static int pn544_hci_event_received(struct nfc_hci_dev *hdev, u8 gate, u8 event,
struct sk_buff *skb)
{ {
struct sk_buff *rgb_skb = NULL; struct sk_buff *rgb_skb = NULL;
int r = 0; int r;
pr_debug("hci event %d", event); pr_debug("hci event %d", event);
switch (event) { switch (event) {
case PN544_HCI_EVT_ACTIVATED: case PN544_HCI_EVT_ACTIVATED:
if (gate == PN544_RF_READER_NFCIP1_INITIATOR_GATE) if (gate == PN544_RF_READER_NFCIP1_INITIATOR_GATE) {
nfc_hci_target_discovered(hdev, gate); r = nfc_hci_target_discovered(hdev, gate);
else if (gate == PN544_RF_READER_NFCIP1_TARGET_GATE) { } else if (gate == PN544_RF_READER_NFCIP1_TARGET_GATE) {
r = nfc_hci_get_param(hdev, gate, PN544_DEP_ATR_REQ, r = nfc_hci_get_param(hdev, gate, PN544_DEP_ATR_REQ,
&rgb_skb); &rgb_skb);
if (r < 0) if (r < 0)
goto exit; goto exit;
nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK, r = nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK,
NFC_COMM_PASSIVE, rgb_skb->data, NFC_COMM_PASSIVE, rgb_skb->data,
rgb_skb->len); rgb_skb->len);
kfree_skb(rgb_skb); kfree_skb(rgb_skb);
} else {
r = -EINVAL;
} }
break; break;
case PN544_HCI_EVT_DEACTIVATED: case PN544_HCI_EVT_DEACTIVATED:
nfc_hci_send_event(hdev, gate, r = nfc_hci_send_event(hdev, gate, NFC_HCI_EVT_END_OPERATION,
NFC_HCI_EVT_END_OPERATION, NULL, 0); NULL, 0);
break; break;
case PN544_HCI_EVT_RCV_DATA: case PN544_HCI_EVT_RCV_DATA:
if (skb->len < 2) { if (skb->len < 2) {
...@@ -757,15 +769,15 @@ static void pn544_hci_event_received(struct nfc_hci_dev *hdev, u8 gate, ...@@ -757,15 +769,15 @@ static void pn544_hci_event_received(struct nfc_hci_dev *hdev, u8 gate,
} }
skb_pull(skb, 2); skb_pull(skb, 2);
nfc_tm_data_received(hdev->ndev, skb); return nfc_tm_data_received(hdev->ndev, skb);
return;
default: default:
break; return 1;
} }
exit: exit:
kfree_skb(skb); kfree_skb(skb);
return r;
} }
static struct nfc_hci_ops pn544_hci_ops = { static struct nfc_hci_ops pn544_hci_ops = {
...@@ -789,7 +801,7 @@ int pn544_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name, ...@@ -789,7 +801,7 @@ int pn544_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name,
struct nfc_hci_dev **hdev) struct nfc_hci_dev **hdev)
{ {
struct pn544_hci_info *info; struct pn544_hci_info *info;
u32 protocols; u32 protocols, se;
struct nfc_hci_init_data init_data; struct nfc_hci_init_data init_data;
int r; int r;
...@@ -822,8 +834,10 @@ int pn544_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name, ...@@ -822,8 +834,10 @@ int pn544_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name,
NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_ISO14443_B_MASK |
NFC_PROTO_NFC_DEP_MASK; NFC_PROTO_NFC_DEP_MASK;
info->hdev = nfc_hci_allocate_device(&pn544_hci_ops, &init_data, se = NFC_SE_UICC | NFC_SE_EMBEDDED;
protocols, llc_name,
info->hdev = nfc_hci_allocate_device(&pn544_hci_ops, &init_data, 0,
protocols, se, llc_name,
phy_headroom + PN544_CMDS_HEADROOM, phy_headroom + PN544_CMDS_HEADROOM,
phy_tailroom, phy_payload); phy_tailroom, phy_payload);
if (!info->hdev) { if (!info->hdev) {
...@@ -851,6 +865,7 @@ int pn544_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name, ...@@ -851,6 +865,7 @@ int pn544_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name,
err_info_alloc: err_info_alloc:
return r; return r;
} }
EXPORT_SYMBOL(pn544_hci_probe);
void pn544_hci_remove(struct nfc_hci_dev *hdev) void pn544_hci_remove(struct nfc_hci_dev *hdev)
{ {
...@@ -860,3 +875,7 @@ void pn544_hci_remove(struct nfc_hci_dev *hdev) ...@@ -860,3 +875,7 @@ void pn544_hci_remove(struct nfc_hci_dev *hdev)
nfc_hci_free_device(hdev); nfc_hci_free_device(hdev);
kfree(info); kfree(info);
} }
EXPORT_SYMBOL(pn544_hci_remove);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION(DRIVER_DESC);
...@@ -57,8 +57,10 @@ struct nfc_hci_ops { ...@@ -57,8 +57,10 @@ struct nfc_hci_ops {
int (*tm_send)(struct nfc_hci_dev *hdev, struct sk_buff *skb); int (*tm_send)(struct nfc_hci_dev *hdev, struct sk_buff *skb);
int (*check_presence)(struct nfc_hci_dev *hdev, int (*check_presence)(struct nfc_hci_dev *hdev,
struct nfc_target *target); struct nfc_target *target);
void (*event_received)(struct nfc_hci_dev *hdev, u8 gate, u8 event, int (*event_received)(struct nfc_hci_dev *hdev, u8 gate, u8 event,
struct sk_buff *skb); struct sk_buff *skb);
int (*enable_se)(struct nfc_dev *dev, u32 secure_element);
int (*disable_se)(struct nfc_dev *dev, u32 secure_element);
}; };
/* Pipes */ /* Pipes */
...@@ -82,11 +84,23 @@ typedef int (*xmit) (struct sk_buff *skb, void *cb_data); ...@@ -82,11 +84,23 @@ typedef int (*xmit) (struct sk_buff *skb, void *cb_data);
#define NFC_HCI_MAX_GATES 256 #define NFC_HCI_MAX_GATES 256
/*
* These values can be specified by a driver to indicate it requires some
* adaptation of the HCI standard.
*
* NFC_HCI_QUIRK_SHORT_CLEAR - send HCI_ADM_CLEAR_ALL_PIPE cmd with no params
*/
enum {
NFC_HCI_QUIRK_SHORT_CLEAR = 0,
};
struct nfc_hci_dev { struct nfc_hci_dev {
struct nfc_dev *ndev; struct nfc_dev *ndev;
u32 max_data_link_payload; u32 max_data_link_payload;
bool shutting_down;
struct mutex msg_tx_mutex; struct mutex msg_tx_mutex;
struct list_head msg_tx_queue; struct list_head msg_tx_queue;
...@@ -129,12 +143,16 @@ struct nfc_hci_dev { ...@@ -129,12 +143,16 @@ struct nfc_hci_dev {
u8 *gb; u8 *gb;
size_t gb_len; size_t gb_len;
unsigned long quirks;
}; };
/* hci device allocation */ /* hci device allocation */
struct nfc_hci_dev *nfc_hci_allocate_device(struct nfc_hci_ops *ops, struct nfc_hci_dev *nfc_hci_allocate_device(struct nfc_hci_ops *ops,
struct nfc_hci_init_data *init_data, struct nfc_hci_init_data *init_data,
unsigned long quirks,
u32 protocols, u32 protocols,
u32 supported_se,
const char *llc_name, const char *llc_name,
int tx_headroom, int tx_headroom,
int tx_tailroom, int tx_tailroom,
......
...@@ -147,6 +147,7 @@ struct nci_dev { ...@@ -147,6 +147,7 @@ struct nci_dev {
/* ----- NCI Devices ----- */ /* ----- NCI Devices ----- */
struct nci_dev *nci_allocate_device(struct nci_ops *ops, struct nci_dev *nci_allocate_device(struct nci_ops *ops,
__u32 supported_protocols, __u32 supported_protocols,
__u32 supported_se,
int tx_headroom, int tx_headroom,
int tx_tailroom); int tx_tailroom);
void nci_free_device(struct nci_dev *ndev); void nci_free_device(struct nci_dev *ndev);
......
...@@ -68,6 +68,8 @@ struct nfc_ops { ...@@ -68,6 +68,8 @@ struct nfc_ops {
void *cb_context); void *cb_context);
int (*tm_send)(struct nfc_dev *dev, struct sk_buff *skb); int (*tm_send)(struct nfc_dev *dev, struct sk_buff *skb);
int (*check_presence)(struct nfc_dev *dev, struct nfc_target *target); int (*check_presence)(struct nfc_dev *dev, struct nfc_target *target);
int (*enable_se)(struct nfc_dev *dev, u32 secure_element);
int (*disable_se)(struct nfc_dev *dev, u32 secure_element);
}; };
#define NFC_TARGET_IDX_ANY -1 #define NFC_TARGET_IDX_ANY -1
...@@ -109,12 +111,17 @@ struct nfc_dev { ...@@ -109,12 +111,17 @@ struct nfc_dev {
struct nfc_genl_data genl_data; struct nfc_genl_data genl_data;
u32 supported_protocols; u32 supported_protocols;
u32 supported_se;
u32 active_se;
int tx_headroom; int tx_headroom;
int tx_tailroom; int tx_tailroom;
struct timer_list check_pres_timer; struct timer_list check_pres_timer;
struct work_struct check_pres_work; struct work_struct check_pres_work;
bool shutting_down;
struct nfc_ops *ops; struct nfc_ops *ops;
}; };
#define to_nfc_dev(_dev) container_of(_dev, struct nfc_dev, dev) #define to_nfc_dev(_dev) container_of(_dev, struct nfc_dev, dev)
...@@ -123,6 +130,7 @@ extern struct class nfc_class; ...@@ -123,6 +130,7 @@ extern struct class nfc_class;
struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops, struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
u32 supported_protocols, u32 supported_protocols,
u32 supported_se,
int tx_headroom, int tx_headroom,
int tx_tailroom); int tx_tailroom);
......
...@@ -5,20 +5,17 @@ ...@@ -5,20 +5,17 @@
* Lauro Ramos Venancio <lauro.venancio@openbossa.org> * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
* Aloisio Almeida Jr <aloisio.almeida@openbossa.org> * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
* *
* This program is free software; you can redistribute it and/or modify * Permission to use, copy, modify, and/or distribute this software for any
* it under the terms of the GNU General Public License as published by * purpose with or without fee is hereby granted, provided that the above
* the Free Software Foundation; either version 2 of the License, or * copyright notice and this permission notice appear in all copies.
* (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* but WITHOUT ANY WARRANTY; without even the implied warranty of * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* GNU General Public License for more details. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* You should have received a copy of the GNU General Public License * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* along with this program; if not, write to the * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/ */
#ifndef __LINUX_NFC_H #ifndef __LINUX_NFC_H
...@@ -67,6 +64,11 @@ ...@@ -67,6 +64,11 @@
* subsequent CONNECT and CC messages. * subsequent CONNECT and CC messages.
* If one of the passed parameters is wrong none is set and -EINVAL is * If one of the passed parameters is wrong none is set and -EINVAL is
* returned. * returned.
* @NFC_CMD_ENABLE_SE: Enable the physical link to a specific secure element.
* Once enabled a secure element will handle card emulation mode, i.e.
* starting a poll from a device which has a secure element enabled means
* we want to do SE based card emulation.
* @NFC_CMD_DISABLE_SE: Disable the physical link to a specific secure element.
*/ */
enum nfc_commands { enum nfc_commands {
NFC_CMD_UNSPEC, NFC_CMD_UNSPEC,
...@@ -86,6 +88,8 @@ enum nfc_commands { ...@@ -86,6 +88,8 @@ enum nfc_commands {
NFC_EVENT_TM_DEACTIVATED, NFC_EVENT_TM_DEACTIVATED,
NFC_CMD_LLC_GET_PARAMS, NFC_CMD_LLC_GET_PARAMS,
NFC_CMD_LLC_SET_PARAMS, NFC_CMD_LLC_SET_PARAMS,
NFC_CMD_ENABLE_SE,
NFC_CMD_DISABLE_SE,
/* private: internal use only */ /* private: internal use only */
__NFC_CMD_AFTER_LAST __NFC_CMD_AFTER_LAST
}; };
...@@ -114,6 +118,7 @@ enum nfc_commands { ...@@ -114,6 +118,7 @@ enum nfc_commands {
* @NFC_ATTR_LLC_PARAM_LTO: Link TimeOut parameter * @NFC_ATTR_LLC_PARAM_LTO: Link TimeOut parameter
* @NFC_ATTR_LLC_PARAM_RW: Receive Window size parameter * @NFC_ATTR_LLC_PARAM_RW: Receive Window size parameter
* @NFC_ATTR_LLC_PARAM_MIUX: MIU eXtension parameter * @NFC_ATTR_LLC_PARAM_MIUX: MIU eXtension parameter
* @NFC_ATTR_SE: Available Secure Elements
*/ */
enum nfc_attrs { enum nfc_attrs {
NFC_ATTR_UNSPEC, NFC_ATTR_UNSPEC,
...@@ -134,6 +139,7 @@ enum nfc_attrs { ...@@ -134,6 +139,7 @@ enum nfc_attrs {
NFC_ATTR_LLC_PARAM_LTO, NFC_ATTR_LLC_PARAM_LTO,
NFC_ATTR_LLC_PARAM_RW, NFC_ATTR_LLC_PARAM_RW,
NFC_ATTR_LLC_PARAM_MIUX, NFC_ATTR_LLC_PARAM_MIUX,
NFC_ATTR_SE,
/* private: internal use only */ /* private: internal use only */
__NFC_ATTR_AFTER_LAST __NFC_ATTR_AFTER_LAST
}; };
...@@ -172,6 +178,11 @@ enum nfc_attrs { ...@@ -172,6 +178,11 @@ enum nfc_attrs {
#define NFC_PROTO_NFC_DEP_MASK (1 << NFC_PROTO_NFC_DEP) #define NFC_PROTO_NFC_DEP_MASK (1 << NFC_PROTO_NFC_DEP)
#define NFC_PROTO_ISO14443_B_MASK (1 << NFC_PROTO_ISO14443_B) #define NFC_PROTO_ISO14443_B_MASK (1 << NFC_PROTO_ISO14443_B)
/* NFC Secure Elements */
#define NFC_SE_NONE 0x0
#define NFC_SE_UICC 0x1
#define NFC_SE_EMBEDDED 0x2
struct sockaddr_nfc { struct sockaddr_nfc {
sa_family_t sa_family; sa_family_t sa_family;
__u32 dev_idx; __u32 dev_idx;
......
...@@ -338,7 +338,7 @@ int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol) ...@@ -338,7 +338,7 @@ int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
dev->active_target = target; dev->active_target = target;
dev->rf_mode = NFC_RF_INITIATOR; dev->rf_mode = NFC_RF_INITIATOR;
if (dev->ops->check_presence) if (dev->ops->check_presence && !dev->shutting_down)
mod_timer(&dev->check_pres_timer, jiffies + mod_timer(&dev->check_pres_timer, jiffies +
msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
} }
...@@ -429,7 +429,7 @@ int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb, ...@@ -429,7 +429,7 @@ int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb, rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
cb_context); cb_context);
if (!rc && dev->ops->check_presence) if (!rc && dev->ops->check_presence && !dev->shutting_down)
mod_timer(&dev->check_pres_timer, jiffies + mod_timer(&dev->check_pres_timer, jiffies +
msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
} else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) { } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
...@@ -684,11 +684,6 @@ static void nfc_release(struct device *d) ...@@ -684,11 +684,6 @@ static void nfc_release(struct device *d)
pr_debug("dev_name=%s\n", dev_name(&dev->dev)); pr_debug("dev_name=%s\n", dev_name(&dev->dev));
if (dev->ops->check_presence) {
del_timer_sync(&dev->check_pres_timer);
cancel_work_sync(&dev->check_pres_work);
}
nfc_genl_data_exit(&dev->genl_data); nfc_genl_data_exit(&dev->genl_data);
kfree(dev->targets); kfree(dev->targets);
kfree(dev); kfree(dev);
...@@ -706,15 +701,16 @@ static void nfc_check_pres_work(struct work_struct *work) ...@@ -706,15 +701,16 @@ static void nfc_check_pres_work(struct work_struct *work)
rc = dev->ops->check_presence(dev, dev->active_target); rc = dev->ops->check_presence(dev, dev->active_target);
if (rc == -EOPNOTSUPP) if (rc == -EOPNOTSUPP)
goto exit; goto exit;
if (!rc) { if (rc) {
mod_timer(&dev->check_pres_timer, jiffies +
msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
} else {
u32 active_target_idx = dev->active_target->idx; u32 active_target_idx = dev->active_target->idx;
device_unlock(&dev->dev); device_unlock(&dev->dev);
nfc_target_lost(dev, active_target_idx); nfc_target_lost(dev, active_target_idx);
return; return;
} }
if (!dev->shutting_down)
mod_timer(&dev->check_pres_timer, jiffies +
msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
} }
exit: exit:
...@@ -761,6 +757,7 @@ struct nfc_dev *nfc_get_device(unsigned int idx) ...@@ -761,6 +757,7 @@ struct nfc_dev *nfc_get_device(unsigned int idx)
*/ */
struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops, struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
u32 supported_protocols, u32 supported_protocols,
u32 supported_se,
int tx_headroom, int tx_tailroom) int tx_headroom, int tx_tailroom)
{ {
struct nfc_dev *dev; struct nfc_dev *dev;
...@@ -778,6 +775,8 @@ struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops, ...@@ -778,6 +775,8 @@ struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
dev->ops = ops; dev->ops = ops;
dev->supported_protocols = supported_protocols; dev->supported_protocols = supported_protocols;
dev->supported_se = supported_se;
dev->active_se = NFC_SE_NONE;
dev->tx_headroom = tx_headroom; dev->tx_headroom = tx_headroom;
dev->tx_tailroom = tx_tailroom; dev->tx_tailroom = tx_tailroom;
...@@ -853,26 +852,27 @@ void nfc_unregister_device(struct nfc_dev *dev) ...@@ -853,26 +852,27 @@ void nfc_unregister_device(struct nfc_dev *dev)
id = dev->idx; id = dev->idx;
mutex_lock(&nfc_devlist_mutex); if (dev->ops->check_presence) {
nfc_devlist_generation++; device_lock(&dev->dev);
dev->shutting_down = true;
/* lock to avoid unregistering a device while an operation device_unlock(&dev->dev);
is in progress */ del_timer_sync(&dev->check_pres_timer);
device_lock(&dev->dev); cancel_work_sync(&dev->check_pres_work);
device_del(&dev->dev); }
device_unlock(&dev->dev);
mutex_unlock(&nfc_devlist_mutex); rc = nfc_genl_device_removed(dev);
if (rc)
pr_debug("The userspace won't be notified that the device %s "
"was removed\n", dev_name(&dev->dev));
nfc_llcp_unregister_device(dev); nfc_llcp_unregister_device(dev);
rc = nfc_genl_device_removed(dev); mutex_lock(&nfc_devlist_mutex);
if (rc) nfc_devlist_generation++;
pr_debug("The userspace won't be notified that the device %s was removed\n", device_del(&dev->dev);
dev_name(&dev->dev)); mutex_unlock(&nfc_devlist_mutex);
ida_simple_remove(&nfc_index_ida, id); ida_simple_remove(&nfc_index_ida, id);
} }
EXPORT_SYMBOL(nfc_unregister_device); EXPORT_SYMBOL(nfc_unregister_device);
......
...@@ -280,14 +280,19 @@ static int nfc_hci_delete_pipe(struct nfc_hci_dev *hdev, u8 pipe) ...@@ -280,14 +280,19 @@ static int nfc_hci_delete_pipe(struct nfc_hci_dev *hdev, u8 pipe)
static int nfc_hci_clear_all_pipes(struct nfc_hci_dev *hdev) static int nfc_hci_clear_all_pipes(struct nfc_hci_dev *hdev)
{ {
u8 param[2]; u8 param[2];
size_t param_len = 2;
/* TODO: Find out what the identity reference data is /* TODO: Find out what the identity reference data is
* and fill param with it. HCI spec 6.1.3.5 */ * and fill param with it. HCI spec 6.1.3.5 */
pr_debug("\n"); pr_debug("\n");
if (test_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &hdev->quirks))
param_len = 0;
return nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE, return nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
NFC_HCI_ADM_CLEAR_ALL_PIPE, param, 2, NULL); NFC_HCI_ADM_CLEAR_ALL_PIPE, param, param_len,
NULL);
} }
int nfc_hci_disconnect_gate(struct nfc_hci_dev *hdev, u8 gate) int nfc_hci_disconnect_gate(struct nfc_hci_dev *hdev, u8 gate)
......
...@@ -57,6 +57,8 @@ static void nfc_hci_msg_tx_work(struct work_struct *work) ...@@ -57,6 +57,8 @@ static void nfc_hci_msg_tx_work(struct work_struct *work)
int r = 0; int r = 0;
mutex_lock(&hdev->msg_tx_mutex); mutex_lock(&hdev->msg_tx_mutex);
if (hdev->shutting_down)
goto exit;
if (hdev->cmd_pending_msg) { if (hdev->cmd_pending_msg) {
if (timer_pending(&hdev->cmd_timer) == 0) { if (timer_pending(&hdev->cmd_timer) == 0) {
...@@ -295,6 +297,12 @@ void nfc_hci_event_received(struct nfc_hci_dev *hdev, u8 pipe, u8 event, ...@@ -295,6 +297,12 @@ void nfc_hci_event_received(struct nfc_hci_dev *hdev, u8 pipe, u8 event,
goto exit; goto exit;
} }
if (hdev->ops->event_received) {
r = hdev->ops->event_received(hdev, gate, event, skb);
if (r <= 0)
goto exit_noskb;
}
switch (event) { switch (event) {
case NFC_HCI_EVT_TARGET_DISCOVERED: case NFC_HCI_EVT_TARGET_DISCOVERED:
if (skb->len < 1) { /* no status data? */ if (skb->len < 1) { /* no status data? */
...@@ -320,17 +328,15 @@ void nfc_hci_event_received(struct nfc_hci_dev *hdev, u8 pipe, u8 event, ...@@ -320,17 +328,15 @@ void nfc_hci_event_received(struct nfc_hci_dev *hdev, u8 pipe, u8 event,
r = nfc_hci_target_discovered(hdev, gate); r = nfc_hci_target_discovered(hdev, gate);
break; break;
default: default:
if (hdev->ops->event_received) { pr_info("Discarded unknown event %x to gate %x\n", event, gate);
hdev->ops->event_received(hdev, gate, event, skb); r = -EINVAL;
return;
}
break; break;
} }
exit: exit:
kfree_skb(skb); kfree_skb(skb);
exit_noskb:
if (r) { if (r) {
/* TODO: There was an error dispatching the event, /* TODO: There was an error dispatching the event,
* how to propagate up to nfc core? * how to propagate up to nfc core?
...@@ -669,8 +675,10 @@ static int hci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb) ...@@ -669,8 +675,10 @@ static int hci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
if (hdev->ops->tm_send) if (hdev->ops->tm_send)
return hdev->ops->tm_send(hdev, skb); return hdev->ops->tm_send(hdev, skb);
else
return -ENOTSUPP; kfree_skb(skb);
return -ENOTSUPP;
} }
static int hci_check_presence(struct nfc_dev *nfc_dev, static int hci_check_presence(struct nfc_dev *nfc_dev,
...@@ -787,7 +795,9 @@ static struct nfc_ops hci_nfc_ops = { ...@@ -787,7 +795,9 @@ static struct nfc_ops hci_nfc_ops = {
struct nfc_hci_dev *nfc_hci_allocate_device(struct nfc_hci_ops *ops, struct nfc_hci_dev *nfc_hci_allocate_device(struct nfc_hci_ops *ops,
struct nfc_hci_init_data *init_data, struct nfc_hci_init_data *init_data,
unsigned long quirks,
u32 protocols, u32 protocols,
u32 supported_se,
const char *llc_name, const char *llc_name,
int tx_headroom, int tx_headroom,
int tx_tailroom, int tx_tailroom,
...@@ -813,7 +823,7 @@ struct nfc_hci_dev *nfc_hci_allocate_device(struct nfc_hci_ops *ops, ...@@ -813,7 +823,7 @@ struct nfc_hci_dev *nfc_hci_allocate_device(struct nfc_hci_ops *ops,
return NULL; return NULL;
} }
hdev->ndev = nfc_allocate_device(&hci_nfc_ops, protocols, hdev->ndev = nfc_allocate_device(&hci_nfc_ops, protocols, supported_se,
tx_headroom + HCI_CMDS_HEADROOM, tx_headroom + HCI_CMDS_HEADROOM,
tx_tailroom); tx_tailroom);
if (!hdev->ndev) { if (!hdev->ndev) {
...@@ -830,6 +840,8 @@ struct nfc_hci_dev *nfc_hci_allocate_device(struct nfc_hci_ops *ops, ...@@ -830,6 +840,8 @@ struct nfc_hci_dev *nfc_hci_allocate_device(struct nfc_hci_ops *ops,
memset(hdev->gate2pipe, NFC_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe)); memset(hdev->gate2pipe, NFC_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe));
hdev->quirks = quirks;
return hdev; return hdev;
} }
EXPORT_SYMBOL(nfc_hci_allocate_device); EXPORT_SYMBOL(nfc_hci_allocate_device);
...@@ -868,6 +880,28 @@ void nfc_hci_unregister_device(struct nfc_hci_dev *hdev) ...@@ -868,6 +880,28 @@ void nfc_hci_unregister_device(struct nfc_hci_dev *hdev)
{ {
struct hci_msg *msg, *n; struct hci_msg *msg, *n;
mutex_lock(&hdev->msg_tx_mutex);
if (hdev->cmd_pending_msg) {
if (hdev->cmd_pending_msg->cb)
hdev->cmd_pending_msg->cb(
hdev->cmd_pending_msg->cb_context,
NULL, -ESHUTDOWN);
kfree(hdev->cmd_pending_msg);
hdev->cmd_pending_msg = NULL;
}
hdev->shutting_down = true;
mutex_unlock(&hdev->msg_tx_mutex);
del_timer_sync(&hdev->cmd_timer);
cancel_work_sync(&hdev->msg_tx_work);
cancel_work_sync(&hdev->msg_rx_work);
nfc_unregister_device(hdev->ndev);
skb_queue_purge(&hdev->rx_hcp_frags); skb_queue_purge(&hdev->rx_hcp_frags);
skb_queue_purge(&hdev->msg_rx_queue); skb_queue_purge(&hdev->msg_rx_queue);
...@@ -876,13 +910,6 @@ void nfc_hci_unregister_device(struct nfc_hci_dev *hdev) ...@@ -876,13 +910,6 @@ void nfc_hci_unregister_device(struct nfc_hci_dev *hdev)
skb_queue_purge(&msg->msg_frags); skb_queue_purge(&msg->msg_frags);
kfree(msg); kfree(msg);
} }
del_timer_sync(&hdev->cmd_timer);
nfc_unregister_device(hdev->ndev);
cancel_work_sync(&hdev->msg_tx_work);
cancel_work_sync(&hdev->msg_rx_work);
} }
EXPORT_SYMBOL(nfc_hci_unregister_device); EXPORT_SYMBOL(nfc_hci_unregister_device);
......
...@@ -105,6 +105,13 @@ int nfc_hci_hcp_message_tx(struct nfc_hci_dev *hdev, u8 pipe, ...@@ -105,6 +105,13 @@ int nfc_hci_hcp_message_tx(struct nfc_hci_dev *hdev, u8 pipe,
} }
mutex_lock(&hdev->msg_tx_mutex); mutex_lock(&hdev->msg_tx_mutex);
if (hdev->shutting_down) {
err = -ESHUTDOWN;
mutex_unlock(&hdev->msg_tx_mutex);
goto out_skb_err;
}
list_add_tail(&cmd->msg_l, &hdev->msg_tx_queue); list_add_tail(&cmd->msg_l, &hdev->msg_tx_queue);
mutex_unlock(&hdev->msg_tx_mutex); mutex_unlock(&hdev->msg_tx_mutex);
......
...@@ -304,6 +304,8 @@ int nfc_llcp_send_symm(struct nfc_dev *dev) ...@@ -304,6 +304,8 @@ int nfc_llcp_send_symm(struct nfc_dev *dev)
skb = llcp_add_header(skb, 0, 0, LLCP_PDU_SYMM); skb = llcp_add_header(skb, 0, 0, LLCP_PDU_SYMM);
__net_timestamp(skb);
nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_TX); nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_TX);
return nfc_data_exchange(dev, local->target_idx, skb, return nfc_data_exchange(dev, local->target_idx, skb,
......
...@@ -54,7 +54,6 @@ static void nfc_llcp_socket_purge(struct nfc_llcp_sock *sock) ...@@ -54,7 +54,6 @@ static void nfc_llcp_socket_purge(struct nfc_llcp_sock *sock)
skb_queue_purge(&sock->tx_queue); skb_queue_purge(&sock->tx_queue);
skb_queue_purge(&sock->tx_pending_queue); skb_queue_purge(&sock->tx_pending_queue);
skb_queue_purge(&sock->tx_backlog_queue);
if (local == NULL) if (local == NULL)
return; return;
...@@ -668,6 +667,8 @@ static void nfc_llcp_tx_work(struct work_struct *work) ...@@ -668,6 +667,8 @@ static void nfc_llcp_tx_work(struct work_struct *work)
if (ptype == LLCP_PDU_I) if (ptype == LLCP_PDU_I)
copy_skb = skb_copy(skb, GFP_ATOMIC); copy_skb = skb_copy(skb, GFP_ATOMIC);
__net_timestamp(skb);
nfc_llcp_send_to_raw_sock(local, skb, nfc_llcp_send_to_raw_sock(local, skb,
NFC_LLCP_DIRECTION_TX); NFC_LLCP_DIRECTION_TX);
...@@ -781,9 +782,15 @@ static void nfc_llcp_recv_ui(struct nfc_llcp_local *local, ...@@ -781,9 +782,15 @@ static void nfc_llcp_recv_ui(struct nfc_llcp_local *local,
/* There is no sequence with UI frames */ /* There is no sequence with UI frames */
skb_pull(skb, LLCP_HEADER_SIZE); skb_pull(skb, LLCP_HEADER_SIZE);
if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) { if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
pr_err("receive queue is full\n"); /*
skb_queue_head(&llcp_sock->tx_backlog_queue, skb); * UI frames will be freed from the socket layer, so we
* need to keep them alive until someone receives them.
*/
skb_get(skb);
} else {
pr_err("Receive queue is full\n");
kfree_skb(skb);
} }
nfc_llcp_sock_put(llcp_sock); nfc_llcp_sock_put(llcp_sock);
...@@ -976,9 +983,15 @@ static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local, ...@@ -976,9 +983,15 @@ static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
pr_err("Received out of sequence I PDU\n"); pr_err("Received out of sequence I PDU\n");
skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE); skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE);
if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) { if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
pr_err("receive queue is full\n"); /*
skb_queue_head(&llcp_sock->tx_backlog_queue, skb); * I frames will be freed from the socket layer, so we
* need to keep them alive until someone receives them.
*/
skb_get(skb);
} else {
pr_err("Receive queue is full\n");
kfree_skb(skb);
} }
} }
...@@ -1245,6 +1258,8 @@ static void nfc_llcp_rx_work(struct work_struct *work) ...@@ -1245,6 +1258,8 @@ static void nfc_llcp_rx_work(struct work_struct *work)
print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET, print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET,
16, 1, skb->data, skb->len, true); 16, 1, skb->data, skb->len, true);
__net_timestamp(skb);
nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_RX); nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_RX);
switch (ptype) { switch (ptype) {
...@@ -1296,6 +1311,13 @@ static void nfc_llcp_rx_work(struct work_struct *work) ...@@ -1296,6 +1311,13 @@ static void nfc_llcp_rx_work(struct work_struct *work)
local->rx_pending = NULL; local->rx_pending = NULL;
} }
static void __nfc_llcp_recv(struct nfc_llcp_local *local, struct sk_buff *skb)
{
local->rx_pending = skb;
del_timer(&local->link_timer);
schedule_work(&local->rx_work);
}
void nfc_llcp_recv(void *data, struct sk_buff *skb, int err) void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
{ {
struct nfc_llcp_local *local = (struct nfc_llcp_local *) data; struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
...@@ -1306,9 +1328,7 @@ void nfc_llcp_recv(void *data, struct sk_buff *skb, int err) ...@@ -1306,9 +1328,7 @@ void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
return; return;
} }
local->rx_pending = skb_get(skb); __nfc_llcp_recv(local, skb);
del_timer(&local->link_timer);
schedule_work(&local->rx_work);
} }
int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb) int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb)
...@@ -1319,9 +1339,7 @@ int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb) ...@@ -1319,9 +1339,7 @@ int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb)
if (local == NULL) if (local == NULL)
return -ENODEV; return -ENODEV;
local->rx_pending = skb_get(skb); __nfc_llcp_recv(local, skb);
del_timer(&local->link_timer);
schedule_work(&local->rx_work);
return 0; return 0;
} }
......
...@@ -121,7 +121,6 @@ struct nfc_llcp_sock { ...@@ -121,7 +121,6 @@ struct nfc_llcp_sock {
struct sk_buff_head tx_queue; struct sk_buff_head tx_queue;
struct sk_buff_head tx_pending_queue; struct sk_buff_head tx_pending_queue;
struct sk_buff_head tx_backlog_queue;
struct list_head accept_queue; struct list_head accept_queue;
struct sock *parent; struct sock *parent;
......
...@@ -672,25 +672,27 @@ static int llcp_sock_recvmsg(struct kiocb *iocb, struct socket *sock, ...@@ -672,25 +672,27 @@ static int llcp_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
copied = min_t(unsigned int, rlen, len); copied = min_t(unsigned int, rlen, len);
cskb = skb; cskb = skb;
if (memcpy_toiovec(msg->msg_iov, cskb->data, copied)) { if (skb_copy_datagram_iovec(cskb, 0, msg->msg_iov, copied)) {
if (!(flags & MSG_PEEK)) if (!(flags & MSG_PEEK))
skb_queue_head(&sk->sk_receive_queue, skb); skb_queue_head(&sk->sk_receive_queue, skb);
return -EFAULT; return -EFAULT;
} }
sock_recv_timestamp(msg, sk, skb);
if (sk->sk_type == SOCK_DGRAM && msg->msg_name) { if (sk->sk_type == SOCK_DGRAM && msg->msg_name) {
struct nfc_llcp_ui_cb *ui_cb = nfc_llcp_ui_skb_cb(skb); struct nfc_llcp_ui_cb *ui_cb = nfc_llcp_ui_skb_cb(skb);
struct sockaddr_nfc_llcp sockaddr; struct sockaddr_nfc_llcp *sockaddr =
(struct sockaddr_nfc_llcp *) msg->msg_name;
pr_debug("Datagram socket %d %d\n", ui_cb->dsap, ui_cb->ssap); msg->msg_namelen = sizeof(struct sockaddr_nfc_llcp);
sockaddr.sa_family = AF_NFC; pr_debug("Datagram socket %d %d\n", ui_cb->dsap, ui_cb->ssap);
sockaddr.nfc_protocol = NFC_PROTO_NFC_DEP;
sockaddr.dsap = ui_cb->dsap;
sockaddr.ssap = ui_cb->ssap;
memcpy(msg->msg_name, &sockaddr, sizeof(sockaddr)); sockaddr->sa_family = AF_NFC;
msg->msg_namelen = sizeof(sockaddr); sockaddr->nfc_protocol = NFC_PROTO_NFC_DEP;
sockaddr->dsap = ui_cb->dsap;
sockaddr->ssap = ui_cb->ssap;
} }
/* Mark read part of skb as used */ /* Mark read part of skb as used */
...@@ -806,7 +808,6 @@ struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp) ...@@ -806,7 +808,6 @@ struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp)
llcp_sock->reserved_ssap = LLCP_SAP_MAX; llcp_sock->reserved_ssap = LLCP_SAP_MAX;
skb_queue_head_init(&llcp_sock->tx_queue); skb_queue_head_init(&llcp_sock->tx_queue);
skb_queue_head_init(&llcp_sock->tx_pending_queue); skb_queue_head_init(&llcp_sock->tx_pending_queue);
skb_queue_head_init(&llcp_sock->tx_backlog_queue);
INIT_LIST_HEAD(&llcp_sock->accept_queue); INIT_LIST_HEAD(&llcp_sock->accept_queue);
if (sock != NULL) if (sock != NULL)
...@@ -821,7 +822,6 @@ void nfc_llcp_sock_free(struct nfc_llcp_sock *sock) ...@@ -821,7 +822,6 @@ void nfc_llcp_sock_free(struct nfc_llcp_sock *sock)
skb_queue_purge(&sock->tx_queue); skb_queue_purge(&sock->tx_queue);
skb_queue_purge(&sock->tx_pending_queue); skb_queue_purge(&sock->tx_pending_queue);
skb_queue_purge(&sock->tx_backlog_queue);
list_del_init(&sock->accept_queue); list_del_init(&sock->accept_queue);
......
...@@ -658,6 +658,7 @@ static struct nfc_ops nci_nfc_ops = { ...@@ -658,6 +658,7 @@ static struct nfc_ops nci_nfc_ops = {
*/ */
struct nci_dev *nci_allocate_device(struct nci_ops *ops, struct nci_dev *nci_allocate_device(struct nci_ops *ops,
__u32 supported_protocols, __u32 supported_protocols,
__u32 supported_se,
int tx_headroom, int tx_tailroom) int tx_headroom, int tx_tailroom)
{ {
struct nci_dev *ndev; struct nci_dev *ndev;
...@@ -680,6 +681,7 @@ struct nci_dev *nci_allocate_device(struct nci_ops *ops, ...@@ -680,6 +681,7 @@ struct nci_dev *nci_allocate_device(struct nci_ops *ops,
ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops, ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
supported_protocols, supported_protocols,
supported_se,
tx_headroom + NCI_DATA_HDR_SIZE, tx_headroom + NCI_DATA_HDR_SIZE,
tx_tailroom); tx_tailroom);
if (!ndev->nfc_dev) if (!ndev->nfc_dev)
......
...@@ -366,6 +366,7 @@ static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev, ...@@ -366,6 +366,7 @@ static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) || nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
nla_put_u32(msg, NFC_ATTR_SE, dev->supported_se) ||
nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) || nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode)) nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
goto nla_put_failure; goto nla_put_failure;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment