Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 1 | /* |
| 2 | * I2C Link Layer for ST21NFCA HCI based Driver |
| 3 | * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 19 | |
| 20 | #include <linux/crc-ccitt.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/i2c.h> |
Christophe Ricard | dfa8070 | 2015-12-23 23:45:11 +0100 | [diff] [blame] | 23 | #include <linux/gpio/consumer.h> |
Christophe Ricard | c44cb2e | 2014-05-13 22:03:39 +0200 | [diff] [blame] | 24 | #include <linux/of_irq.h> |
| 25 | #include <linux/of_gpio.h> |
Christophe Ricard | dfa8070 | 2015-12-23 23:45:11 +0100 | [diff] [blame] | 26 | #include <linux/acpi.h> |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/delay.h> |
| 29 | #include <linux/nfc.h> |
| 30 | #include <linux/firmware.h> |
Andy Shevchenko | 79557b3 | 2017-03-07 12:25:43 +0200 | [diff] [blame] | 31 | |
Johannes Berg | db083bc | 2014-11-19 21:17:22 +0100 | [diff] [blame] | 32 | #include <asm/unaligned.h> |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 33 | |
| 34 | #include <net/nfc/hci.h> |
| 35 | #include <net/nfc/llc.h> |
| 36 | #include <net/nfc/nfc.h> |
| 37 | |
| 38 | #include "st21nfca.h" |
| 39 | |
| 40 | /* |
| 41 | * Every frame starts with ST21NFCA_SOF_EOF and ends with ST21NFCA_SOF_EOF. |
| 42 | * Because ST21NFCA_SOF_EOF is a possible data value, there is a mecanism |
| 43 | * called byte stuffing has been introduced. |
| 44 | * |
| 45 | * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING |
| 46 | * - insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte) |
| 47 | * - xor byte with ST21NFCA_BYTE_STUFFING_MASK |
| 48 | */ |
| 49 | #define ST21NFCA_SOF_EOF 0x7e |
| 50 | #define ST21NFCA_BYTE_STUFFING_MASK 0x20 |
| 51 | #define ST21NFCA_ESCAPE_BYTE_STUFFING 0x7d |
| 52 | |
Christophe Ricard | e1fb97b | 2014-04-24 23:19:31 +0200 | [diff] [blame] | 53 | /* SOF + 00 */ |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 54 | #define ST21NFCA_FRAME_HEADROOM 2 |
| 55 | |
Christophe Ricard | e1fb97b | 2014-04-24 23:19:31 +0200 | [diff] [blame] | 56 | /* 2 bytes crc + EOF */ |
| 57 | #define ST21NFCA_FRAME_TAILROOM 3 |
Christophe Ricard | c97ffdb | 2014-04-24 23:19:33 +0200 | [diff] [blame] | 58 | #define IS_START_OF_FRAME(buf) (buf[0] == ST21NFCA_SOF_EOF && \ |
| 59 | buf[1] == 0) |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 60 | |
Andy Shevchenko | 79557b3 | 2017-03-07 12:25:43 +0200 | [diff] [blame] | 61 | #define ST21NFCA_HCI_DRIVER_NAME "st21nfca_hci" |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 62 | #define ST21NFCA_HCI_I2C_DRIVER_NAME "st21nfca_hci_i2c" |
| 63 | |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 64 | struct st21nfca_i2c_phy { |
| 65 | struct i2c_client *i2c_dev; |
| 66 | struct nfc_hci_dev *hdev; |
| 67 | |
Andy Shevchenko | 8d3c50e | 2017-03-07 12:25:45 +0200 | [diff] [blame] | 68 | struct gpio_desc *gpiod_ena; |
Christophe Ricard | 2130fb9 | 2015-01-27 01:18:19 +0100 | [diff] [blame] | 69 | struct st21nfca_se_status se_status; |
| 70 | |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 71 | struct sk_buff *pending_skb; |
| 72 | int current_read_len; |
| 73 | /* |
| 74 | * crc might have fail because i2c macro |
| 75 | * is disable due to other interface activity |
| 76 | */ |
| 77 | int crc_trials; |
| 78 | |
| 79 | int powered; |
| 80 | int run_mode; |
| 81 | |
| 82 | /* |
| 83 | * < 0 if hardware error occured (e.g. i2c err) |
| 84 | * and prevents normal operation. |
| 85 | */ |
| 86 | int hard_fault; |
Christophe Ricard | a3c5d8f | 2014-04-24 23:19:34 +0200 | [diff] [blame] | 87 | struct mutex phy_lock; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 88 | }; |
Christophe Ricard | 57dc828 | 2015-10-25 22:54:45 +0100 | [diff] [blame] | 89 | |
Christophe Ricard | 0531107 | 2014-05-20 22:21:56 +0200 | [diff] [blame] | 90 | static u8 len_seq[] = { 16, 24, 12, 29 }; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 91 | static u16 wait_tab[] = { 2, 3, 5, 15, 20, 40}; |
| 92 | |
| 93 | #define I2C_DUMP_SKB(info, skb) \ |
| 94 | do { \ |
| 95 | pr_debug("%s:\n", info); \ |
| 96 | print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \ |
| 97 | 16, 1, (skb)->data, (skb)->len, 0); \ |
| 98 | } while (0) |
| 99 | |
Christophe Ricard | 18d2c62 | 2014-04-01 00:34:07 +0200 | [diff] [blame] | 100 | /* |
| 101 | * In order to get the CLF in a known state we generate an internal reboot |
| 102 | * using a proprietary command. |
| 103 | * Once the reboot is completed, we expect to receive a ST21NFCA_SOF_EOF |
| 104 | * fill buffer. |
| 105 | */ |
| 106 | static int st21nfca_hci_platform_init(struct st21nfca_i2c_phy *phy) |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 107 | { |
Christophe Ricard | c5b0c37 | 2014-04-01 00:34:03 +0200 | [diff] [blame] | 108 | u16 wait_reboot[] = { 50, 300, 1000 }; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 109 | char reboot_cmd[] = { 0x7E, 0x66, 0x48, 0xF6, 0x7E }; |
| 110 | u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE]; |
| 111 | int i, r = -1; |
| 112 | |
Christophe Ricard | 18d2c62 | 2014-04-01 00:34:07 +0200 | [diff] [blame] | 113 | for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) { |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 114 | r = i2c_master_send(phy->i2c_dev, reboot_cmd, |
| 115 | sizeof(reboot_cmd)); |
Christophe Ricard | 18d2c62 | 2014-04-01 00:34:07 +0200 | [diff] [blame] | 116 | if (r < 0) |
| 117 | msleep(wait_reboot[i]); |
| 118 | } |
| 119 | if (r < 0) |
| 120 | return r; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 121 | |
Christophe Ricard | 18d2c62 | 2014-04-01 00:34:07 +0200 | [diff] [blame] | 122 | /* CLF is spending about 20ms to do an internal reboot */ |
| 123 | msleep(20); |
| 124 | r = -1; |
| 125 | for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) { |
| 126 | r = i2c_master_recv(phy->i2c_dev, tmp, |
| 127 | ST21NFCA_HCI_LLC_MAX_SIZE); |
| 128 | if (r < 0) |
| 129 | msleep(wait_reboot[i]); |
| 130 | } |
| 131 | if (r < 0) |
| 132 | return r; |
| 133 | |
| 134 | for (i = 0; i < ST21NFCA_HCI_LLC_MAX_SIZE && |
| 135 | tmp[i] == ST21NFCA_SOF_EOF; i++) |
| 136 | ; |
| 137 | |
| 138 | if (r != ST21NFCA_HCI_LLC_MAX_SIZE) |
| 139 | return -ENODEV; |
| 140 | |
| 141 | usleep_range(1000, 1500); |
| 142 | return 0; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static int st21nfca_hci_i2c_enable(void *phy_id) |
| 146 | { |
| 147 | struct st21nfca_i2c_phy *phy = phy_id; |
| 148 | |
Andy Shevchenko | 8d3c50e | 2017-03-07 12:25:45 +0200 | [diff] [blame] | 149 | gpiod_set_value(phy->gpiod_ena, 1); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 150 | phy->powered = 1; |
| 151 | phy->run_mode = ST21NFCA_HCI_MODE; |
| 152 | |
| 153 | usleep_range(10000, 15000); |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static void st21nfca_hci_i2c_disable(void *phy_id) |
| 159 | { |
| 160 | struct st21nfca_i2c_phy *phy = phy_id; |
| 161 | |
Andy Shevchenko | 8d3c50e | 2017-03-07 12:25:45 +0200 | [diff] [blame] | 162 | gpiod_set_value(phy->gpiod_ena, 0); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 163 | |
| 164 | phy->powered = 0; |
| 165 | } |
| 166 | |
Christophe Ricard | e1fb97b | 2014-04-24 23:19:31 +0200 | [diff] [blame] | 167 | static void st21nfca_hci_add_len_crc(struct sk_buff *skb) |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 168 | { |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 169 | u16 crc; |
| 170 | u8 tmp; |
| 171 | |
Johannes Berg | d58ff35 | 2017-06-16 14:29:23 +0200 | [diff] [blame] | 172 | *(u8 *)skb_push(skb, 1) = 0; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 173 | |
| 174 | crc = crc_ccitt(0xffff, skb->data, skb->len); |
| 175 | crc = ~crc; |
| 176 | |
| 177 | tmp = crc & 0x00ff; |
Johannes Berg | 634fef6 | 2017-06-16 14:29:24 +0200 | [diff] [blame] | 178 | skb_put_u8(skb, tmp); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 179 | |
| 180 | tmp = (crc >> 8) & 0x00ff; |
Johannes Berg | 634fef6 | 2017-06-16 14:29:24 +0200 | [diff] [blame] | 181 | skb_put_u8(skb, tmp); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Christophe Ricard | e1fb97b | 2014-04-24 23:19:31 +0200 | [diff] [blame] | 184 | static void st21nfca_hci_remove_len_crc(struct sk_buff *skb) |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 185 | { |
| 186 | skb_pull(skb, ST21NFCA_FRAME_HEADROOM); |
Christophe Ricard | e1fb97b | 2014-04-24 23:19:31 +0200 | [diff] [blame] | 187 | skb_trim(skb, skb->len - ST21NFCA_FRAME_TAILROOM); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Writing a frame must not return the number of written bytes. |
| 192 | * It must return either zero for success, or <0 for error. |
| 193 | * In addition, it must not alter the skb |
| 194 | */ |
| 195 | static int st21nfca_hci_i2c_write(void *phy_id, struct sk_buff *skb) |
| 196 | { |
Christophe Ricard | e1fb97b | 2014-04-24 23:19:31 +0200 | [diff] [blame] | 197 | int r = -1, i, j; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 198 | struct st21nfca_i2c_phy *phy = phy_id; |
| 199 | struct i2c_client *client = phy->i2c_dev; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 200 | u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE * 2]; |
| 201 | |
| 202 | I2C_DUMP_SKB("st21nfca_hci_i2c_write", skb); |
| 203 | |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 204 | if (phy->hard_fault != 0) |
| 205 | return phy->hard_fault; |
| 206 | |
| 207 | /* |
| 208 | * Compute CRC before byte stuffing computation on frame |
| 209 | * Note st21nfca_hci_add_len_crc is doing a byte stuffing |
| 210 | * on its own value |
| 211 | */ |
Christophe Ricard | e1fb97b | 2014-04-24 23:19:31 +0200 | [diff] [blame] | 212 | st21nfca_hci_add_len_crc(skb); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 213 | |
| 214 | /* add ST21NFCA_SOF_EOF on tail */ |
Johannes Berg | 634fef6 | 2017-06-16 14:29:24 +0200 | [diff] [blame] | 215 | skb_put_u8(skb, ST21NFCA_SOF_EOF); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 216 | /* add ST21NFCA_SOF_EOF on head */ |
Johannes Berg | d58ff35 | 2017-06-16 14:29:23 +0200 | [diff] [blame] | 217 | *(u8 *)skb_push(skb, 1) = ST21NFCA_SOF_EOF; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 218 | |
| 219 | /* |
| 220 | * Compute byte stuffing |
| 221 | * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING |
| 222 | * insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte) |
| 223 | * xor byte with ST21NFCA_BYTE_STUFFING_MASK |
| 224 | */ |
| 225 | tmp[0] = skb->data[0]; |
| 226 | for (i = 1, j = 1; i < skb->len - 1; i++, j++) { |
| 227 | if (skb->data[i] == ST21NFCA_SOF_EOF |
| 228 | || skb->data[i] == ST21NFCA_ESCAPE_BYTE_STUFFING) { |
| 229 | tmp[j] = ST21NFCA_ESCAPE_BYTE_STUFFING; |
| 230 | j++; |
| 231 | tmp[j] = skb->data[i] ^ ST21NFCA_BYTE_STUFFING_MASK; |
| 232 | } else { |
| 233 | tmp[j] = skb->data[i]; |
| 234 | } |
| 235 | } |
| 236 | tmp[j] = skb->data[i]; |
| 237 | j++; |
| 238 | |
| 239 | /* |
| 240 | * Manage sleep mode |
| 241 | * Try 3 times to send data with delay between each |
| 242 | */ |
Christophe Ricard | a3c5d8f | 2014-04-24 23:19:34 +0200 | [diff] [blame] | 243 | mutex_lock(&phy->phy_lock); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 244 | for (i = 0; i < ARRAY_SIZE(wait_tab) && r < 0; i++) { |
| 245 | r = i2c_master_send(client, tmp, j); |
| 246 | if (r < 0) |
| 247 | msleep(wait_tab[i]); |
| 248 | } |
Christophe Ricard | a3c5d8f | 2014-04-24 23:19:34 +0200 | [diff] [blame] | 249 | mutex_unlock(&phy->phy_lock); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 250 | |
| 251 | if (r >= 0) { |
| 252 | if (r != j) |
| 253 | r = -EREMOTEIO; |
| 254 | else |
| 255 | r = 0; |
| 256 | } |
| 257 | |
Christophe Ricard | e1fb97b | 2014-04-24 23:19:31 +0200 | [diff] [blame] | 258 | st21nfca_hci_remove_len_crc(skb); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 259 | |
| 260 | return r; |
| 261 | } |
| 262 | |
| 263 | static int get_frame_size(u8 *buf, int buflen) |
| 264 | { |
| 265 | int len = 0; |
Christophe Ricard | 3e6df91 | 2014-07-28 18:11:31 +0200 | [diff] [blame] | 266 | |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 267 | if (buf[len + 1] == ST21NFCA_SOF_EOF) |
| 268 | return 0; |
| 269 | |
| 270 | for (len = 1; len < buflen && buf[len] != ST21NFCA_SOF_EOF; len++) |
| 271 | ; |
| 272 | |
| 273 | return len; |
| 274 | } |
| 275 | |
| 276 | static int check_crc(u8 *buf, int buflen) |
| 277 | { |
| 278 | u16 crc; |
| 279 | |
| 280 | crc = crc_ccitt(0xffff, buf, buflen - 2); |
| 281 | crc = ~crc; |
| 282 | |
| 283 | if (buf[buflen - 2] != (crc & 0xff) || buf[buflen - 1] != (crc >> 8)) { |
| 284 | pr_err(ST21NFCA_HCI_DRIVER_NAME |
| 285 | ": CRC error 0x%x != 0x%x 0x%x\n", crc, buf[buflen - 1], |
| 286 | buf[buflen - 2]); |
| 287 | |
| 288 | pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__); |
| 289 | print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE, |
| 290 | 16, 2, buf, buflen, false); |
| 291 | return -EPERM; |
| 292 | } |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Prepare received data for upper layer. |
| 298 | * Received data include byte stuffing, crc and sof/eof |
| 299 | * which is not usable by hci part. |
| 300 | * returns: |
| 301 | * frame size without sof/eof, header and byte stuffing |
| 302 | * -EBADMSG : frame was incorrect and discarded |
| 303 | */ |
| 304 | static int st21nfca_hci_i2c_repack(struct sk_buff *skb) |
| 305 | { |
| 306 | int i, j, r, size; |
Christophe Ricard | 3e6df91 | 2014-07-28 18:11:31 +0200 | [diff] [blame] | 307 | |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 308 | if (skb->len < 1 || (skb->len > 1 && skb->data[1] != 0)) |
| 309 | return -EBADMSG; |
| 310 | |
| 311 | size = get_frame_size(skb->data, skb->len); |
| 312 | if (size > 0) { |
| 313 | skb_trim(skb, size); |
| 314 | /* remove ST21NFCA byte stuffing for upper layer */ |
| 315 | for (i = 1, j = 0; i < skb->len; i++) { |
Christophe Ricard | 3096e25 | 2014-04-24 23:19:32 +0200 | [diff] [blame] | 316 | if (skb->data[i + j] == |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 317 | (u8) ST21NFCA_ESCAPE_BYTE_STUFFING) { |
Christophe Ricard | 3096e25 | 2014-04-24 23:19:32 +0200 | [diff] [blame] | 318 | skb->data[i] = skb->data[i + j + 1] |
| 319 | | ST21NFCA_BYTE_STUFFING_MASK; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 320 | i++; |
| 321 | j++; |
| 322 | } |
| 323 | skb->data[i] = skb->data[i + j]; |
| 324 | } |
| 325 | /* remove byte stuffing useless byte */ |
| 326 | skb_trim(skb, i - j); |
| 327 | /* remove ST21NFCA_SOF_EOF from head */ |
| 328 | skb_pull(skb, 1); |
| 329 | |
| 330 | r = check_crc(skb->data, skb->len); |
| 331 | if (r != 0) { |
| 332 | i = 0; |
| 333 | return -EBADMSG; |
| 334 | } |
| 335 | |
| 336 | /* remove headbyte */ |
| 337 | skb_pull(skb, 1); |
| 338 | /* remove crc. Byte Stuffing is already removed here */ |
| 339 | skb_trim(skb, skb->len - 2); |
| 340 | return skb->len; |
| 341 | } |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees |
| 347 | * that i2c bus will be flushed and that next read will start on a new frame. |
| 348 | * returned skb contains only LLC header and payload. |
| 349 | * returns: |
| 350 | * frame size : if received frame is complete (find ST21NFCA_SOF_EOF at |
| 351 | * end of read) |
| 352 | * -EAGAIN : if received frame is incomplete (not find ST21NFCA_SOF_EOF |
| 353 | * at end of read) |
| 354 | * -EREMOTEIO : i2c read error (fatal) |
| 355 | * -EBADMSG : frame was incorrect and discarded |
| 356 | * (value returned from st21nfca_hci_i2c_repack) |
| 357 | * -EIO : if no ST21NFCA_SOF_EOF is found after reaching |
| 358 | * the read length end sequence |
| 359 | */ |
| 360 | static int st21nfca_hci_i2c_read(struct st21nfca_i2c_phy *phy, |
| 361 | struct sk_buff *skb) |
| 362 | { |
| 363 | int r, i; |
| 364 | u8 len; |
Christophe Ricard | c97ffdb | 2014-04-24 23:19:33 +0200 | [diff] [blame] | 365 | u8 buf[ST21NFCA_HCI_LLC_MAX_PAYLOAD]; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 366 | struct i2c_client *client = phy->i2c_dev; |
| 367 | |
| 368 | if (phy->current_read_len < ARRAY_SIZE(len_seq)) { |
| 369 | len = len_seq[phy->current_read_len]; |
| 370 | |
| 371 | /* |
| 372 | * Add retry mecanism |
| 373 | * Operation on I2C interface may fail in case of operation on |
| 374 | * RF or SWP interface |
| 375 | */ |
| 376 | r = 0; |
Christophe Ricard | a3c5d8f | 2014-04-24 23:19:34 +0200 | [diff] [blame] | 377 | mutex_lock(&phy->phy_lock); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 378 | for (i = 0; i < ARRAY_SIZE(wait_tab) && r <= 0; i++) { |
Christophe Ricard | c97ffdb | 2014-04-24 23:19:33 +0200 | [diff] [blame] | 379 | r = i2c_master_recv(client, buf, len); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 380 | if (r < 0) |
| 381 | msleep(wait_tab[i]); |
| 382 | } |
Christophe Ricard | a3c5d8f | 2014-04-24 23:19:34 +0200 | [diff] [blame] | 383 | mutex_unlock(&phy->phy_lock); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 384 | |
| 385 | if (r != len) { |
| 386 | phy->current_read_len = 0; |
| 387 | return -EREMOTEIO; |
| 388 | } |
| 389 | |
Christophe Ricard | c97ffdb | 2014-04-24 23:19:33 +0200 | [diff] [blame] | 390 | /* |
| 391 | * The first read sequence does not start with SOF. |
| 392 | * Data is corrupeted so we drop it. |
| 393 | */ |
Christophe Ricard | 8e9466c | 2014-05-20 22:21:55 +0200 | [diff] [blame] | 394 | if (!phy->current_read_len && !IS_START_OF_FRAME(buf)) { |
Christophe Ricard | c97ffdb | 2014-04-24 23:19:33 +0200 | [diff] [blame] | 395 | skb_trim(skb, 0); |
| 396 | phy->current_read_len = 0; |
| 397 | return -EIO; |
Christophe Ricard | 8e9466c | 2014-05-20 22:21:55 +0200 | [diff] [blame] | 398 | } else if (phy->current_read_len && IS_START_OF_FRAME(buf)) { |
Christophe Ricard | c97ffdb | 2014-04-24 23:19:33 +0200 | [diff] [blame] | 399 | /* |
| 400 | * Previous frame transmission was interrupted and |
| 401 | * the frame got repeated. |
| 402 | * Received frame start with ST21NFCA_SOF_EOF + 00. |
| 403 | */ |
| 404 | skb_trim(skb, 0); |
| 405 | phy->current_read_len = 0; |
| 406 | } |
| 407 | |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 408 | skb_put_data(skb, buf, len); |
Christophe Ricard | c97ffdb | 2014-04-24 23:19:33 +0200 | [diff] [blame] | 409 | |
| 410 | if (skb->data[skb->len - 1] == ST21NFCA_SOF_EOF) { |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 411 | phy->current_read_len = 0; |
| 412 | return st21nfca_hci_i2c_repack(skb); |
| 413 | } |
| 414 | phy->current_read_len++; |
| 415 | return -EAGAIN; |
| 416 | } |
| 417 | return -EIO; |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * Reads an shdlc frame from the chip. This is not as straightforward as it |
| 422 | * seems. The frame format is data-crc, and corruption can occur anywhere |
| 423 | * while transiting on i2c bus, such that we could read an invalid data. |
| 424 | * The tricky case is when we read a corrupted data or crc. We must detect |
| 425 | * this here in order to determine that data can be transmitted to the hci |
| 426 | * core. This is the reason why we check the crc here. |
| 427 | * The CLF will repeat a frame until we send a RR on that frame. |
| 428 | * |
| 429 | * On ST21NFCA, IRQ goes in idle when read starts. As no size information are |
| 430 | * available in the incoming data, other IRQ might come. Every IRQ will trigger |
| 431 | * a read sequence with different length and will fill the current frame. |
| 432 | * The reception is complete once we reach a ST21NFCA_SOF_EOF. |
| 433 | */ |
| 434 | static irqreturn_t st21nfca_hci_irq_thread_fn(int irq, void *phy_id) |
| 435 | { |
| 436 | struct st21nfca_i2c_phy *phy = phy_id; |
| 437 | struct i2c_client *client; |
| 438 | |
| 439 | int r; |
| 440 | |
| 441 | if (!phy || irq != phy->i2c_dev->irq) { |
| 442 | WARN_ON_ONCE(1); |
| 443 | return IRQ_NONE; |
| 444 | } |
| 445 | |
| 446 | client = phy->i2c_dev; |
| 447 | dev_dbg(&client->dev, "IRQ\n"); |
| 448 | |
| 449 | if (phy->hard_fault != 0) |
| 450 | return IRQ_HANDLED; |
| 451 | |
| 452 | r = st21nfca_hci_i2c_read(phy, phy->pending_skb); |
| 453 | if (r == -EREMOTEIO) { |
| 454 | phy->hard_fault = r; |
| 455 | |
| 456 | nfc_hci_recv_frame(phy->hdev, NULL); |
| 457 | |
| 458 | return IRQ_HANDLED; |
| 459 | } else if (r == -EAGAIN || r == -EIO) { |
| 460 | return IRQ_HANDLED; |
| 461 | } else if (r == -EBADMSG && phy->crc_trials < ARRAY_SIZE(wait_tab)) { |
| 462 | /* |
| 463 | * With ST21NFCA, only one interface (I2C, RF or SWP) |
| 464 | * may be active at a time. |
| 465 | * Having incorrect crc is usually due to i2c macrocell |
| 466 | * deactivation in the middle of a transmission. |
| 467 | * It may generate corrupted data on i2c. |
| 468 | * We give sometime to get i2c back. |
| 469 | * The complete frame will be repeated. |
| 470 | */ |
| 471 | msleep(wait_tab[phy->crc_trials]); |
| 472 | phy->crc_trials++; |
| 473 | phy->current_read_len = 0; |
Christophe Ricard | 0c942b0 | 2014-04-24 23:19:35 +0200 | [diff] [blame] | 474 | kfree_skb(phy->pending_skb); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 475 | } else if (r > 0) { |
| 476 | /* |
| 477 | * We succeeded to read data from the CLF and |
| 478 | * data is valid. |
| 479 | * Reset counter. |
| 480 | */ |
| 481 | nfc_hci_recv_frame(phy->hdev, phy->pending_skb); |
| 482 | phy->crc_trials = 0; |
Christophe Ricard | cf57734 | 2014-05-20 22:21:54 +0200 | [diff] [blame] | 483 | } else { |
| 484 | kfree_skb(phy->pending_skb); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL); |
| 488 | if (phy->pending_skb == NULL) { |
| 489 | phy->hard_fault = -ENOMEM; |
| 490 | nfc_hci_recv_frame(phy->hdev, NULL); |
| 491 | } |
| 492 | |
| 493 | return IRQ_HANDLED; |
| 494 | } |
| 495 | |
| 496 | static struct nfc_phy_ops i2c_phy_ops = { |
| 497 | .write = st21nfca_hci_i2c_write, |
| 498 | .enable = st21nfca_hci_i2c_enable, |
| 499 | .disable = st21nfca_hci_i2c_disable, |
| 500 | }; |
| 501 | |
Andy Shevchenko | 394671e | 2017-06-19 13:08:47 +0300 | [diff] [blame] | 502 | static const struct acpi_gpio_params enable_gpios = { 1, 0, false }; |
| 503 | |
| 504 | static const struct acpi_gpio_mapping acpi_st21nfca_gpios[] = { |
| 505 | { "enable-gpios", &enable_gpios, 1 }, |
| 506 | {}, |
| 507 | }; |
| 508 | |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 509 | static int st21nfca_hci_i2c_probe(struct i2c_client *client, |
| 510 | const struct i2c_device_id *id) |
| 511 | { |
Andy Shevchenko | d31748b | 2017-06-19 13:08:48 +0300 | [diff] [blame] | 512 | struct device *dev = &client->dev; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 513 | struct st21nfca_i2c_phy *phy; |
Christophe Ricard | fcb45e6 | 2014-04-01 00:34:06 +0200 | [diff] [blame] | 514 | int r; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 515 | |
| 516 | dev_dbg(&client->dev, "%s\n", __func__); |
| 517 | dev_dbg(&client->dev, "IRQ: %d\n", client->irq); |
| 518 | |
| 519 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 520 | nfc_err(&client->dev, "Need I2C_FUNC_I2C\n"); |
| 521 | return -ENODEV; |
| 522 | } |
| 523 | |
| 524 | phy = devm_kzalloc(&client->dev, sizeof(struct st21nfca_i2c_phy), |
| 525 | GFP_KERNEL); |
Christophe Ricard | 2b70283 | 2015-01-25 23:33:27 +0100 | [diff] [blame] | 526 | if (!phy) |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 527 | return -ENOMEM; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 528 | |
| 529 | phy->i2c_dev = client; |
Christophe Ricard | fcb45e6 | 2014-04-01 00:34:06 +0200 | [diff] [blame] | 530 | phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL); |
| 531 | if (phy->pending_skb == NULL) |
| 532 | return -ENOMEM; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 533 | |
Christophe Ricard | fcb45e6 | 2014-04-01 00:34:06 +0200 | [diff] [blame] | 534 | phy->current_read_len = 0; |
| 535 | phy->crc_trials = 0; |
Christophe Ricard | a3c5d8f | 2014-04-24 23:19:34 +0200 | [diff] [blame] | 536 | mutex_init(&phy->phy_lock); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 537 | i2c_set_clientdata(client, phy); |
| 538 | |
Andy Shevchenko | d31748b | 2017-06-19 13:08:48 +0300 | [diff] [blame] | 539 | r = devm_acpi_dev_add_driver_gpios(dev, acpi_st21nfca_gpios); |
| 540 | if (r) |
| 541 | dev_dbg(dev, "Unable to add GPIO mapping table\n"); |
| 542 | |
| 543 | /* Get EN GPIO from resource provider */ |
| 544 | phy->gpiod_ena = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW); |
| 545 | if (IS_ERR(phy->gpiod_ena)) { |
| 546 | nfc_err(dev, "Unable to get ENABLE GPIO\n"); |
| 547 | return PTR_ERR(phy->gpiod_ena); |
Christophe Ricard | fcb45e6 | 2014-04-01 00:34:06 +0200 | [diff] [blame] | 548 | } |
Christophe Ricard | fcb45e6 | 2014-04-01 00:34:06 +0200 | [diff] [blame] | 549 | |
Andy Shevchenko | 682fd61 | 2017-03-07 12:25:46 +0200 | [diff] [blame] | 550 | phy->se_status.is_ese_present = |
| 551 | device_property_read_bool(&client->dev, "ese-present"); |
| 552 | phy->se_status.is_uicc_present = |
| 553 | device_property_read_bool(&client->dev, "uicc-present"); |
| 554 | |
Christophe Ricard | 18d2c62 | 2014-04-01 00:34:07 +0200 | [diff] [blame] | 555 | r = st21nfca_hci_platform_init(phy); |
| 556 | if (r < 0) { |
| 557 | nfc_err(&client->dev, "Unable to reboot st21nfca\n"); |
Christophe Ricard | 67df3f9 | 2014-12-02 21:27:55 +0100 | [diff] [blame] | 558 | return r; |
Christophe Ricard | 18d2c62 | 2014-04-01 00:34:07 +0200 | [diff] [blame] | 559 | } |
| 560 | |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 561 | r = devm_request_threaded_irq(&client->dev, client->irq, NULL, |
| 562 | st21nfca_hci_irq_thread_fn, |
Andy Shevchenko | 8e7836d | 2017-03-07 12:25:44 +0200 | [diff] [blame] | 563 | IRQF_ONESHOT, |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 564 | ST21NFCA_HCI_DRIVER_NAME, phy); |
| 565 | if (r < 0) { |
| 566 | nfc_err(&client->dev, "Unable to register IRQ handler\n"); |
| 567 | return r; |
| 568 | } |
| 569 | |
Christophe Ricard | 9bac75d | 2014-04-01 00:34:05 +0200 | [diff] [blame] | 570 | return st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME, |
Christophe Ricard | 2130fb9 | 2015-01-27 01:18:19 +0100 | [diff] [blame] | 571 | ST21NFCA_FRAME_HEADROOM, |
| 572 | ST21NFCA_FRAME_TAILROOM, |
| 573 | ST21NFCA_HCI_LLC_MAX_PAYLOAD, |
| 574 | &phy->hdev, |
| 575 | &phy->se_status); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | static int st21nfca_hci_i2c_remove(struct i2c_client *client) |
| 579 | { |
| 580 | struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client); |
| 581 | |
| 582 | dev_dbg(&client->dev, "%s\n", __func__); |
| 583 | |
| 584 | st21nfca_hci_remove(phy->hdev); |
| 585 | |
| 586 | if (phy->powered) |
| 587 | st21nfca_hci_i2c_disable(phy); |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
Arvind Yadav | a122ffd | 2017-08-21 22:34:00 +0530 | [diff] [blame] | 592 | static const struct i2c_device_id st21nfca_hci_i2c_id_table[] = { |
Christophe Ricard | 39db2d2 | 2015-12-23 23:45:06 +0100 | [diff] [blame] | 593 | {ST21NFCA_HCI_DRIVER_NAME, 0}, |
| 594 | {} |
| 595 | }; |
| 596 | MODULE_DEVICE_TABLE(i2c, st21nfca_hci_i2c_id_table); |
| 597 | |
Christophe Ricard | dfa8070 | 2015-12-23 23:45:11 +0100 | [diff] [blame] | 598 | static const struct acpi_device_id st21nfca_hci_i2c_acpi_match[] = { |
| 599 | {"SMO2100", 0}, |
| 600 | {} |
| 601 | }; |
| 602 | MODULE_DEVICE_TABLE(acpi, st21nfca_hci_i2c_acpi_match); |
| 603 | |
Christophe Ricard | c44cb2e | 2014-05-13 22:03:39 +0200 | [diff] [blame] | 604 | static const struct of_device_id of_st21nfca_i2c_match[] = { |
Christophe Ricard | 6b5fba4 | 2014-12-08 22:08:06 +0100 | [diff] [blame] | 605 | { .compatible = "st,st21nfca-i2c", }, |
Christophe Ricard | c44cb2e | 2014-05-13 22:03:39 +0200 | [diff] [blame] | 606 | { .compatible = "st,st21nfca_i2c", }, |
| 607 | {} |
| 608 | }; |
Christophe Ricard | 17e40107 | 2014-11-13 00:30:22 +0100 | [diff] [blame] | 609 | MODULE_DEVICE_TABLE(of, of_st21nfca_i2c_match); |
Christophe Ricard | c44cb2e | 2014-05-13 22:03:39 +0200 | [diff] [blame] | 610 | |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 611 | static struct i2c_driver st21nfca_hci_i2c_driver = { |
| 612 | .driver = { |
Christophe Ricard | fcb45e6 | 2014-04-01 00:34:06 +0200 | [diff] [blame] | 613 | .name = ST21NFCA_HCI_I2C_DRIVER_NAME, |
Christophe Ricard | c44cb2e | 2014-05-13 22:03:39 +0200 | [diff] [blame] | 614 | .of_match_table = of_match_ptr(of_st21nfca_i2c_match), |
Christophe Ricard | dfa8070 | 2015-12-23 23:45:11 +0100 | [diff] [blame] | 615 | .acpi_match_table = ACPI_PTR(st21nfca_hci_i2c_acpi_match), |
Christophe Ricard | fcb45e6 | 2014-04-01 00:34:06 +0200 | [diff] [blame] | 616 | }, |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 617 | .probe = st21nfca_hci_i2c_probe, |
| 618 | .id_table = st21nfca_hci_i2c_id_table, |
| 619 | .remove = st21nfca_hci_i2c_remove, |
| 620 | }; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 621 | module_i2c_driver(st21nfca_hci_i2c_driver); |
| 622 | |
| 623 | MODULE_LICENSE("GPL"); |
| 624 | MODULE_DESCRIPTION(DRIVER_DESC); |