| 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 int pn532_dev_up(struct pn533 *dev) |
| 104 |
{ |
| 105 |
struct pn532_uart_phy *pn532 = dev->phy; |
| 106 |
int ret = 0; |
| 107 |
|
| 108 |
ret = serdev_device_open(pn532->serdev); |
| 109 |
if (ret) |
| 110 |
return ret; |
| 111 |
|
| 112 |
pn532->send_wakeup = PN532_SEND_LAST_WAKEUP; |
| 113 |
return ret; |
| 114 |
} |
| 115 |
|
| 116 |
static int pn532_dev_down(struct pn533 *dev) |
| 117 |
{ |
| 118 |
struct pn532_uart_phy *pn532 = dev->phy; |
| 119 |
|
| 120 |
serdev_device_close(pn532->serdev); |
| 121 |
pn532->send_wakeup = PN532_SEND_WAKEUP; |
| 122 |
|
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
static const struct pn533_phy_ops uart_phy_ops = { |
| 127 |
.send_frame = pn532_uart_send_frame, |
| 128 |
.send_ack = pn532_uart_send_ack, |
| 129 |
.abort_cmd = pn532_uart_abort_cmd, |
| 130 |
.dev_up = pn532_dev_up, |
| 131 |
.dev_down = pn532_dev_down, |
| 132 |
}; |
| 133 |
|
| 134 |
static void pn532_cmd_timeout(struct timer_list *t) |
| 135 |
{ |
| 136 |
struct pn532_uart_phy *dev = timer_container_of(dev, t, cmd_timeout); |
| 137 |
|
| 138 |
pn532_uart_send_frame(dev->priv, dev->cur_out_buf); |
| 139 |
} |
| 140 |
|
| 141 |
/* |
| 142 |
* scans the buffer if it contains a pn532 frame. It is not checked if the |
| 143 |
* frame is really valid. This is later done with pn533_rx_frame_is_valid. |
| 144 |
* This is useful for malformed or errornous transmitted frames. Adjusts the |
| 145 |
* bufferposition where the frame starts, since pn533_recv_frame expects a |
| 146 |
* well formed frame. |
| 147 |
*/ |
| 148 |
static int pn532_uart_rx_is_frame(struct sk_buff *skb) |
| 149 |
{ |
| 150 |
struct pn533_std_frame *std; |
| 151 |
struct pn533_ext_frame *ext; |
| 152 |
u16 frame_len; |
| 153 |
int i; |
| 154 |
|
| 155 |
for (i = 0; i + PN533_STD_FRAME_ACK_SIZE <= skb->len; i++) { |
| 156 |
std = (struct pn533_std_frame *)&skb->data[i]; |
| 157 |
/* search start code */ |
| 158 |
if (std->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF)) |
| 159 |
continue; |
| 160 |
|
| 161 |
/* frame type */ |
| 162 |
switch (std->datalen) { |
| 163 |
case PN533_FRAME_DATALEN_ACK: |
| 164 |
if (std->datalen_checksum == 0xff) { |
| 165 |
skb_pull(skb, i); |
| 166 |
return 1; |
| 167 |
} |
| 168 |
|
| 169 |
break; |
| 170 |
case PN533_FRAME_DATALEN_ERROR: |
| 171 |
if ((std->datalen_checksum == 0xff) && |
| 172 |
(skb->len >= |
| 173 |
PN533_STD_ERROR_FRAME_SIZE)) { |
| 174 |
skb_pull(skb, i); |
| 175 |
return 1; |
| 176 |
} |
| 177 |
|
| 178 |
break; |
| 179 |
case PN533_FRAME_DATALEN_EXTENDED: |
| 180 |
ext = (struct pn533_ext_frame *)&skb->data[i]; |
| 181 |
frame_len = be16_to_cpu(ext->datalen); |
| 182 |
if (skb->len >= frame_len + |
| 183 |
sizeof(struct pn533_ext_frame) + |
| 184 |
2 /* CKS + Postamble */) { |
| 185 |
skb_pull(skb, i); |
| 186 |
return 1; |
| 187 |
} |
| 188 |
|
| 189 |
break; |
| 190 |
default: /* normal information frame */ |
| 191 |
frame_len = std->datalen; |
| 192 |
if (skb->len >= frame_len + |
| 193 |
sizeof(struct pn533_std_frame) + |
| 194 |
2 /* CKS + Postamble */) { |
| 195 |
skb_pull(skb, i); |
| 196 |
return 1; |
| 197 |
} |
| 198 |
|
| 199 |
break; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
return 0; |
| 204 |
} |
| 205 |
|
| 206 |
static size_t pn532_receive_buf(struct serdev_device *serdev, |
| 207 |
const u8 *data, size_t count) |
| 208 |
{ |
| 209 |
struct pn532_uart_phy *dev = serdev_device_get_drvdata(serdev); |
| 210 |
size_t i; |
| 211 |
|
| 212 |
timer_delete(&dev->cmd_timeout); |
| 213 |
for (i = 0; i < count; i++) { |
| 214 |
if (!dev->recv_skb) { |
| 215 |
dev->recv_skb = alloc_skb(PN532_UART_SKB_BUFF_LEN, |
| 216 |
GFP_KERNEL); |
| 217 |
if (!dev->recv_skb) |
| 218 |
return i; |
| 219 |
} |
| 220 |
|
| 221 |
if (unlikely(!skb_tailroom(dev->recv_skb))) |
| 222 |
skb_trim(dev->recv_skb, 0); |
| 223 |
|
| 224 |
skb_put_u8(dev->recv_skb, *data++); |
| 225 |
if (!pn532_uart_rx_is_frame(dev->recv_skb)) |
| 226 |
continue; |
| 227 |
|
| 228 |
pn533_recv_frame(dev->priv, dev->recv_skb, 0); |
| 229 |
dev->recv_skb = NULL; |
| 230 |
} |
| 231 |
|
| 232 |
return i; |
| 233 |
} |
| 234 |
|
| 235 |
static const struct serdev_device_ops pn532_serdev_ops = { |
| 236 |
.receive_buf = pn532_receive_buf, |
| 237 |
.write_wakeup = serdev_device_write_wakeup, |
| 238 |
}; |
| 239 |
|
| 240 |
static const struct of_device_id pn532_uart_of_match[] = { |
| 241 |
{ .compatible = "nxp,pn532", }, |
| 242 |
{}, |
| 243 |
}; |
| 244 |
MODULE_DEVICE_TABLE(of, pn532_uart_of_match); |
| 245 |
|
| 246 |
static int pn532_uart_probe(struct serdev_device *serdev) |
| 247 |
{ |
| 248 |
struct pn532_uart_phy *pn532; |
| 249 |
struct pn533 *priv; |
| 250 |
int err; |
| 251 |
|
| 252 |
err = -ENOMEM; |
| 253 |
pn532 = kzalloc_obj(*pn532); |
| 254 |
if (!pn532) |
| 255 |
goto err_exit; |
| 256 |
|
| 257 |
pn532->recv_skb = alloc_skb(PN532_UART_SKB_BUFF_LEN, GFP_KERNEL); |
| 258 |
if (!pn532->recv_skb) |
| 259 |
goto err_free; |
| 260 |
|
| 261 |
pn532->serdev = serdev; |
| 262 |
serdev_device_set_drvdata(serdev, pn532); |
| 263 |
serdev_device_set_client_ops(serdev, &pn532_serdev_ops); |
| 264 |
err = serdev_device_open(serdev); |
| 265 |
if (err) { |
| 266 |
dev_err(&serdev->dev, "Unable to open device\n"); |
| 267 |
goto err_skb; |
| 268 |
} |
| 269 |
|
| 270 |
err = serdev_device_set_baudrate(serdev, 115200); |
| 271 |
if (err != 115200) { |
| 272 |
err = -EINVAL; |
| 273 |
goto err_serdev; |
| 274 |
} |
| 275 |
|
| 276 |
serdev_device_set_flow_control(serdev, false); |
| 277 |
pn532->send_wakeup = PN532_SEND_WAKEUP; |
| 278 |
timer_setup(&pn532->cmd_timeout, pn532_cmd_timeout, 0); |
| 279 |
priv = pn53x_common_init(PN533_DEVICE_PN532_AUTOPOLL, |
| 280 |
PN533_PROTO_REQ_ACK_RESP, |
| 281 |
pn532, &uart_phy_ops, NULL, |
| 282 |
&pn532->serdev->dev); |
| 283 |
if (IS_ERR(priv)) { |
| 284 |
err = PTR_ERR(priv); |
| 285 |
goto err_serdev; |
| 286 |
} |
| 287 |
|
| 288 |
pn532->priv = priv; |
| 289 |
err = pn533_finalize_setup(pn532->priv); |
| 290 |
if (err) |
| 291 |
goto err_clean; |
| 292 |
|
| 293 |
serdev_device_close(serdev); |
| 294 |
err = pn53x_register_nfc(priv, PN533_NO_TYPE_B_PROTOCOLS, &serdev->dev); |
| 295 |
if (err) { |
| 296 |
pn53x_common_clean(pn532->priv); |
| 297 |
goto err_skb; |
| 298 |
} |
| 299 |
|
| 300 |
return err; |
| 301 |
|
| 302 |
err_clean: |
| 303 |
pn53x_common_clean(pn532->priv); |
| 304 |
err_serdev: |
| 305 |
serdev_device_close(serdev); |
| 306 |
err_skb: |
| 307 |
kfree_skb(pn532->recv_skb); |
| 308 |
err_free: |
| 309 |
kfree(pn532); |
| 310 |
err_exit: |
| 311 |
return err; |
| 312 |
} |
| 313 |
|
| 314 |
static void pn532_uart_remove(struct serdev_device *serdev) |
| 315 |
{ |
| 316 |
struct pn532_uart_phy *pn532 = serdev_device_get_drvdata(serdev); |
| 317 |
|
| 318 |
pn53x_unregister_nfc(pn532->priv); |
| 319 |
serdev_device_close(serdev); |
| 320 |
pn53x_common_clean(pn532->priv); |
| 321 |
timer_shutdown_sync(&pn532->cmd_timeout); |
| 322 |
kfree_skb(pn532->recv_skb); |
| 323 |
kfree(pn532); |
| 324 |
} |
| 325 |
|
| 326 |
static struct serdev_device_driver pn532_uart_driver = { |
| 327 |
.probe = pn532_uart_probe, |
| 328 |
.remove = pn532_uart_remove, |
| 329 |
.driver = { |
| 330 |
.name = "pn532_uart", |
| 331 |
.of_match_table = pn532_uart_of_match, |
| 332 |
}, |
| 333 |
}; |
| 334 |
|
| 335 |
module_serdev_device_driver(pn532_uart_driver); |
| 336 |
|
| 337 |
MODULE_AUTHOR("Lars Pöschel <[email protected]>"); |
| 338 |
MODULE_DESCRIPTION("PN532 UART driver"); |
| 339 |
MODULE_LICENSE("GPL"); |