Skip to content

Commit c656aa4

Browse files
poescheldavem330
authored andcommitted
nfc: pn533: add UART phy driver
This adds the UART phy interface for the pn533 driver. The pn533 driver can be used through UART interface this way. It is implemented as a serdev device. Cc: Johan Hovold <[email protected]> Cc: Claudiu Beznea <[email protected]> Cc: David Miller <[email protected]> Signed-off-by: Lars Poeschel <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 843cc92 commit c656aa4

File tree

4 files changed

+344
-0
lines changed

4 files changed

+344
-0
lines changed

drivers/nfc/pn533/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,14 @@ config NFC_PN533_I2C
2626

2727
If you choose to build a module, it'll be called pn533_i2c.
2828
Say N if unsure.
29+
30+
config NFC_PN532_UART
31+
tristate "NFC PN532 device support (UART)"
32+
depends on SERIAL_DEV_BUS
33+
select NFC_PN533
34+
---help---
35+
This module adds support for the NXP pn532 UART interface.
36+
Select this if your platform is using the UART bus.
37+
38+
If you choose to build a module, it'll be called pn532_uart.
39+
Say N if unsure.

drivers/nfc/pn533/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#
55
pn533_usb-objs = usb.o
66
pn533_i2c-objs = i2c.o
7+
pn532_uart-objs = uart.o
78

89
obj-$(CONFIG_NFC_PN533) += pn533.o
910
obj-$(CONFIG_NFC_PN533_USB) += pn533_usb.o
1011
obj-$(CONFIG_NFC_PN533_I2C) += pn533_i2c.o
12+
obj-$(CONFIG_NFC_PN532_UART) += pn532_uart.o

drivers/nfc/pn533/pn533.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343

4444
/* Preamble (1), SoPC (2), ACK Code (2), Postamble (1) */
4545
#define PN533_STD_FRAME_ACK_SIZE 6
46+
/*
47+
* Preamble (1), SoPC (2), Packet Length (1), Packet Length Checksum (1),
48+
* Specific Application Level Error Code (1) , Postamble (1)
49+
*/
50+
#define PN533_STD_ERROR_FRAME_SIZE 8
4651
#define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen])
4752
#define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
4853
/* Half start code (3), LEN (4) should be 0xffff for extended frame */
@@ -84,6 +89,9 @@
8489
#define PN533_CMD_MI_MASK 0x40
8590
#define PN533_CMD_RET_SUCCESS 0x00
8691

92+
#define PN533_FRAME_DATALEN_ACK 0x00
93+
#define PN533_FRAME_DATALEN_ERROR 0x01
94+
#define PN533_FRAME_DATALEN_EXTENDED 0xFF
8795

8896
enum pn533_protocol_type {
8997
PN533_PROTO_REQ_ACK_RESP = 0,

drivers/nfc/pn533/uart.c

Lines changed: 323 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,323 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
/*
3+
* Driver for NXP PN532 NFC Chip - UART transport layer
4+
*
5+
* Copyright (C) 2018 Lemonage Software GmbH
6+
* Author: Lars Pöschel <[email protected]>
7+
* All rights reserved.
8+
*/
9+
10+
#include <linux/device.h>
11+
#include <linux/kernel.h>
12+
#include <linux/module.h>
13+
#include <linux/nfc.h>
14+
#include <linux/netdevice.h>
15+
#include <linux/of.h>
16+
#include <linux/serdev.h>
17+
#include "pn533.h"
18+
19+
#define PN532_UART_SKB_BUFF_LEN (PN533_CMD_DATAEXCH_DATA_MAXLEN * 2)
20+
21+
enum send_wakeup {
22+
PN532_SEND_NO_WAKEUP = 0,
23+
PN532_SEND_WAKEUP,
24+
PN532_SEND_LAST_WAKEUP,
25+
};
26+
27+
28+
struct pn532_uart_phy {
29+
struct serdev_device *serdev;
30+
struct sk_buff *recv_skb;
31+
struct pn533 *priv;
32+
/*
33+
* send_wakeup variable is used to control if we need to send a wakeup
34+
* request to the pn532 chip prior to our actual command. There is a
35+
* little propability of a race condition. We decided to not mutex the
36+
* variable as the worst that could happen is, that we send a wakeup
37+
* to the chip that is already awake. This does not hurt. It is a
38+
* no-op to the chip.
39+
*/
40+
enum send_wakeup send_wakeup;
41+
struct timer_list cmd_timeout;
42+
struct sk_buff *cur_out_buf;
43+
};
44+
45+
static int pn532_uart_send_frame(struct pn533 *dev,
46+
struct sk_buff *out)
47+
{
48+
/* wakeup sequence and dummy bytes for waiting time */
49+
static const u8 wakeup[] = {
50+
0x55, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
52+
struct pn532_uart_phy *pn532 = dev->phy;
53+
int err;
54+
55+
print_hex_dump_debug("PN532_uart TX: ", DUMP_PREFIX_NONE, 16, 1,
56+
out->data, out->len, false);
57+
58+
pn532->cur_out_buf = out;
59+
if (pn532->send_wakeup) {
60+
err = serdev_device_write(pn532->serdev,
61+
wakeup, sizeof(wakeup),
62+
MAX_SCHEDULE_TIMEOUT);
63+
if (err < 0)
64+
return err;
65+
}
66+
67+
if (pn532->send_wakeup == PN532_SEND_LAST_WAKEUP)
68+
pn532->send_wakeup = PN532_SEND_NO_WAKEUP;
69+
70+
err = serdev_device_write(pn532->serdev, out->data, out->len,
71+
MAX_SCHEDULE_TIMEOUT);
72+
if (err < 0)
73+
return err;
74+
75+
mod_timer(&pn532->cmd_timeout, HZ / 40 + jiffies);
76+
return 0;
77+
}
78+
79+
static int pn532_uart_send_ack(struct pn533 *dev, gfp_t flags)
80+
{
81+
/* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
82+
static const u8 ack[PN533_STD_FRAME_ACK_SIZE] = {
83+
0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
84+
struct pn532_uart_phy *pn532 = dev->phy;
85+
int err;
86+
87+
err = serdev_device_write(pn532->serdev, ack, sizeof(ack),
88+
MAX_SCHEDULE_TIMEOUT);
89+
if (err < 0)
90+
return err;
91+
92+
return 0;
93+
}
94+
95+
static void pn532_uart_abort_cmd(struct pn533 *dev, gfp_t flags)
96+
{
97+
/* An ack will cancel the last issued command */
98+
pn532_uart_send_ack(dev, flags);
99+
/* schedule cmd_complete_work to finish current command execution */
100+
pn533_recv_frame(dev, NULL, -ENOENT);
101+
}
102+
103+
static void pn532_dev_up(struct pn533 *dev)
104+
{
105+
struct pn532_uart_phy *pn532 = dev->phy;
106+
107+
serdev_device_open(pn532->serdev);
108+
pn532->send_wakeup = PN532_SEND_LAST_WAKEUP;
109+
}
110+
111+
static void pn532_dev_down(struct pn533 *dev)
112+
{
113+
struct pn532_uart_phy *pn532 = dev->phy;
114+
115+
serdev_device_close(pn532->serdev);
116+
pn532->send_wakeup = PN532_SEND_WAKEUP;
117+
}
118+
119+
static struct pn533_phy_ops uart_phy_ops = {
120+
.send_frame = pn532_uart_send_frame,
121+
.send_ack = pn532_uart_send_ack,
122+
.abort_cmd = pn532_uart_abort_cmd,
123+
.dev_up = pn532_dev_up,
124+
.dev_down = pn532_dev_down,
125+
};
126+
127+
static void pn532_cmd_timeout(struct timer_list *t)
128+
{
129+
struct pn532_uart_phy *dev = from_timer(dev, t, cmd_timeout);
130+
131+
pn532_uart_send_frame(dev->priv, dev->cur_out_buf);
132+
}
133+
134+
/*
135+
* scans the buffer if it contains a pn532 frame. It is not checked if the
136+
* frame is really valid. This is later done with pn533_rx_frame_is_valid.
137+
* This is useful for malformed or errornous transmitted frames. Adjusts the
138+
* bufferposition where the frame starts, since pn533_recv_frame expects a
139+
* well formed frame.
140+
*/
141+
static int pn532_uart_rx_is_frame(struct sk_buff *skb)
142+
{
143+
struct pn533_std_frame *std;
144+
struct pn533_ext_frame *ext;
145+
u16 frame_len;
146+
int i;
147+
148+
for (i = 0; i + PN533_STD_FRAME_ACK_SIZE <= skb->len; i++) {
149+
std = (struct pn533_std_frame *)&skb->data[i];
150+
/* search start code */
151+
if (std->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
152+
continue;
153+
154+
/* frame type */
155+
switch (std->datalen) {
156+
case PN533_FRAME_DATALEN_ACK:
157+
if (std->datalen_checksum == 0xff) {
158+
skb_pull(skb, i);
159+
return 1;
160+
}
161+
162+
break;
163+
case PN533_FRAME_DATALEN_ERROR:
164+
if ((std->datalen_checksum == 0xff) &&
165+
(skb->len >=
166+
PN533_STD_ERROR_FRAME_SIZE)) {
167+
skb_pull(skb, i);
168+
return 1;
169+
}
170+
171+
break;
172+
case PN533_FRAME_DATALEN_EXTENDED:
173+
ext = (struct pn533_ext_frame *)&skb->data[i];
174+
frame_len = be16_to_cpu(ext->datalen);
175+
if (skb->len >= frame_len +
176+
sizeof(struct pn533_ext_frame) +
177+
2 /* CKS + Postamble */) {
178+
skb_pull(skb, i);
179+
return 1;
180+
}
181+
182+
break;
183+
default: /* normal information frame */
184+
frame_len = std->datalen;
185+
if (skb->len >= frame_len +
186+
sizeof(struct pn533_std_frame) +
187+
2 /* CKS + Postamble */) {
188+
skb_pull(skb, i);
189+
return 1;
190+
}
191+
192+
break;
193+
}
194+
}
195+
196+
return 0;
197+
}
198+
199+
static int pn532_receive_buf(struct serdev_device *serdev,
200+
const unsigned char *data, size_t count)
201+
{
202+
struct pn532_uart_phy *dev = serdev_device_get_drvdata(serdev);
203+
size_t i;
204+
205+
del_timer(&dev->cmd_timeout);
206+
for (i = 0; i < count; i++) {
207+
skb_put_u8(dev->recv_skb, *data++);
208+
if (!pn532_uart_rx_is_frame(dev->recv_skb))
209+
continue;
210+
211+
pn533_recv_frame(dev->priv, dev->recv_skb, 0);
212+
dev->recv_skb = alloc_skb(PN532_UART_SKB_BUFF_LEN, GFP_KERNEL);
213+
if (!dev->recv_skb)
214+
return 0;
215+
}
216+
217+
return i;
218+
}
219+
220+
static struct serdev_device_ops pn532_serdev_ops = {
221+
.receive_buf = pn532_receive_buf,
222+
.write_wakeup = serdev_device_write_wakeup,
223+
};
224+
225+
static const struct of_device_id pn532_uart_of_match[] = {
226+
{ .compatible = "nxp,pn532", },
227+
{},
228+
};
229+
MODULE_DEVICE_TABLE(of, pn532_uart_of_match);
230+
231+
static int pn532_uart_probe(struct serdev_device *serdev)
232+
{
233+
struct pn532_uart_phy *pn532;
234+
struct pn533 *priv;
235+
int err;
236+
237+
err = -ENOMEM;
238+
pn532 = kzalloc(sizeof(*pn532), GFP_KERNEL);
239+
if (!pn532)
240+
goto err_exit;
241+
242+
pn532->recv_skb = alloc_skb(PN532_UART_SKB_BUFF_LEN, GFP_KERNEL);
243+
if (!pn532->recv_skb)
244+
goto err_free;
245+
246+
pn532->serdev = serdev;
247+
serdev_device_set_drvdata(serdev, pn532);
248+
serdev_device_set_client_ops(serdev, &pn532_serdev_ops);
249+
err = serdev_device_open(serdev);
250+
if (err) {
251+
dev_err(&serdev->dev, "Unable to open device\n");
252+
goto err_skb;
253+
}
254+
255+
err = serdev_device_set_baudrate(serdev, 115200);
256+
if (err != 115200) {
257+
err = -EINVAL;
258+
goto err_serdev;
259+
}
260+
261+
serdev_device_set_flow_control(serdev, false);
262+
pn532->send_wakeup = PN532_SEND_WAKEUP;
263+
timer_setup(&pn532->cmd_timeout, pn532_cmd_timeout, 0);
264+
priv = pn53x_common_init(PN533_DEVICE_PN532,
265+
PN533_PROTO_REQ_ACK_RESP,
266+
pn532, &uart_phy_ops, NULL,
267+
&pn532->serdev->dev);
268+
if (IS_ERR(priv)) {
269+
err = PTR_ERR(priv);
270+
goto err_serdev;
271+
}
272+
273+
pn532->priv = priv;
274+
err = pn533_finalize_setup(pn532->priv);
275+
if (err)
276+
goto err_clean;
277+
278+
serdev_device_close(serdev);
279+
err = pn53x_register_nfc(priv, PN533_NO_TYPE_B_PROTOCOLS, &serdev->dev);
280+
if (err) {
281+
pn53x_common_clean(pn532->priv);
282+
goto err_skb;
283+
}
284+
285+
return err;
286+
287+
err_clean:
288+
pn53x_common_clean(pn532->priv);
289+
err_serdev:
290+
serdev_device_close(serdev);
291+
err_skb:
292+
kfree_skb(pn532->recv_skb);
293+
err_free:
294+
kfree(pn532);
295+
err_exit:
296+
return err;
297+
}
298+
299+
static void pn532_uart_remove(struct serdev_device *serdev)
300+
{
301+
struct pn532_uart_phy *pn532 = serdev_device_get_drvdata(serdev);
302+
303+
pn53x_unregister_nfc(pn532->priv);
304+
serdev_device_close(serdev);
305+
pn53x_common_clean(pn532->priv);
306+
kfree_skb(pn532->recv_skb);
307+
kfree(pn532);
308+
}
309+
310+
static struct serdev_device_driver pn532_uart_driver = {
311+
.probe = pn532_uart_probe,
312+
.remove = pn532_uart_remove,
313+
.driver = {
314+
.name = "pn532_uart",
315+
.of_match_table = of_match_ptr(pn532_uart_of_match),
316+
},
317+
};
318+
319+
module_serdev_device_driver(pn532_uart_driver);
320+
321+
MODULE_AUTHOR("Lars Pöschel <[email protected]>");
322+
MODULE_DESCRIPTION("PN532 UART driver");
323+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)