// SPDX-License-Identifier: GPL-2.0-only /* * I2C Link Layer for ST21NFCA HCI based Driver * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved. */ #define pr_fmt(fmt) … #include <linux/crc-ccitt.h> #include <linux/module.h> #include <linux/i2c.h> #include <linux/gpio/consumer.h> #include <linux/of_irq.h> #include <linux/of_gpio.h> #include <linux/acpi.h> #include <linux/interrupt.h> #include <linux/delay.h> #include <linux/nfc.h> #include <linux/firmware.h> #include <net/nfc/hci.h> #include <net/nfc/llc.h> #include <net/nfc/nfc.h> #include "st21nfca.h" /* * Every frame starts with ST21NFCA_SOF_EOF and ends with ST21NFCA_SOF_EOF. * Because ST21NFCA_SOF_EOF is a possible data value, there is a mecanism * called byte stuffing has been introduced. * * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING * - insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte) * - xor byte with ST21NFCA_BYTE_STUFFING_MASK */ #define ST21NFCA_SOF_EOF … #define ST21NFCA_BYTE_STUFFING_MASK … #define ST21NFCA_ESCAPE_BYTE_STUFFING … /* SOF + 00 */ #define ST21NFCA_FRAME_HEADROOM … /* 2 bytes crc + EOF */ #define ST21NFCA_FRAME_TAILROOM … #define IS_START_OF_FRAME(buf) … #define ST21NFCA_HCI_DRIVER_NAME … #define ST21NFCA_HCI_I2C_DRIVER_NAME … struct st21nfca_i2c_phy { … }; static const u8 len_seq[] = …; static const u16 wait_tab[] = …; #define I2C_DUMP_SKB(info, skb) … /* * In order to get the CLF in a known state we generate an internal reboot * using a proprietary command. * Once the reboot is completed, we expect to receive a ST21NFCA_SOF_EOF * fill buffer. */ static int st21nfca_hci_platform_init(struct st21nfca_i2c_phy *phy) { … } static int st21nfca_hci_i2c_enable(void *phy_id) { … } static void st21nfca_hci_i2c_disable(void *phy_id) { … } static void st21nfca_hci_add_len_crc(struct sk_buff *skb) { … } static void st21nfca_hci_remove_len_crc(struct sk_buff *skb) { … } /* * Writing a frame must not return the number of written bytes. * It must return either zero for success, or <0 for error. * In addition, it must not alter the skb */ static int st21nfca_hci_i2c_write(void *phy_id, struct sk_buff *skb) { … } static int get_frame_size(u8 *buf, int buflen) { … } static int check_crc(u8 *buf, int buflen) { … } /* * Prepare received data for upper layer. * Received data include byte stuffing, crc and sof/eof * which is not usable by hci part. * returns: * frame size without sof/eof, header and byte stuffing * -EBADMSG : frame was incorrect and discarded */ static int st21nfca_hci_i2c_repack(struct sk_buff *skb) { … } /* * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees * that i2c bus will be flushed and that next read will start on a new frame. * returned skb contains only LLC header and payload. * returns: * frame size : if received frame is complete (find ST21NFCA_SOF_EOF at * end of read) * -EAGAIN : if received frame is incomplete (not find ST21NFCA_SOF_EOF * at end of read) * -EREMOTEIO : i2c read error (fatal) * -EBADMSG : frame was incorrect and discarded * (value returned from st21nfca_hci_i2c_repack) * -EIO : if no ST21NFCA_SOF_EOF is found after reaching * the read length end sequence */ static int st21nfca_hci_i2c_read(struct st21nfca_i2c_phy *phy, struct sk_buff *skb) { … } /* * Reads an shdlc frame from the chip. This is not as straightforward as it * seems. The frame format is data-crc, and corruption can occur anywhere * while transiting on i2c bus, such that we could read an invalid data. * The tricky case is when we read a corrupted data or crc. We must detect * this here in order to determine that data can be transmitted to the hci * core. This is the reason why we check the crc here. * The CLF will repeat a frame until we send a RR on that frame. * * On ST21NFCA, IRQ goes in idle when read starts. As no size information are * available in the incoming data, other IRQ might come. Every IRQ will trigger * a read sequence with different length and will fill the current frame. * The reception is complete once we reach a ST21NFCA_SOF_EOF. */ static irqreturn_t st21nfca_hci_irq_thread_fn(int irq, void *phy_id) { … } static const struct nfc_phy_ops i2c_phy_ops = …; static const struct acpi_gpio_params enable_gpios = …; static const struct acpi_gpio_mapping acpi_st21nfca_gpios[] = …; static int st21nfca_hci_i2c_probe(struct i2c_client *client) { … } static void st21nfca_hci_i2c_remove(struct i2c_client *client) { … } static const struct i2c_device_id st21nfca_hci_i2c_id_table[] = …; MODULE_DEVICE_TABLE(i2c, st21nfca_hci_i2c_id_table); static const struct acpi_device_id st21nfca_hci_i2c_acpi_match[] __maybe_unused = …; MODULE_DEVICE_TABLE(acpi, st21nfca_hci_i2c_acpi_match); static const struct of_device_id of_st21nfca_i2c_match[] __maybe_unused = …; MODULE_DEVICE_TABLE(of, of_st21nfca_i2c_match); static struct i2c_driver st21nfca_hci_i2c_driver = …; module_i2c_driver(…) …; MODULE_LICENSE(…) …; MODULE_DESCRIPTION(…);