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> |
| 23 | #include <linux/gpio.h> |
| 24 | #include <linux/miscdevice.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/nfc.h> |
| 28 | #include <linux/firmware.h> |
| 29 | #include <linux/unaligned/access_ok.h> |
| 30 | #include <linux/platform_data/st21nfca.h> |
| 31 | |
| 32 | #include <net/nfc/hci.h> |
| 33 | #include <net/nfc/llc.h> |
| 34 | #include <net/nfc/nfc.h> |
| 35 | |
| 36 | #include "st21nfca.h" |
| 37 | |
| 38 | /* |
| 39 | * Every frame starts with ST21NFCA_SOF_EOF and ends with ST21NFCA_SOF_EOF. |
| 40 | * Because ST21NFCA_SOF_EOF is a possible data value, there is a mecanism |
| 41 | * called byte stuffing has been introduced. |
| 42 | * |
| 43 | * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING |
| 44 | * - insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte) |
| 45 | * - xor byte with ST21NFCA_BYTE_STUFFING_MASK |
| 46 | */ |
| 47 | #define ST21NFCA_SOF_EOF 0x7e |
| 48 | #define ST21NFCA_BYTE_STUFFING_MASK 0x20 |
| 49 | #define ST21NFCA_ESCAPE_BYTE_STUFFING 0x7d |
| 50 | |
Christophe Ricard | e1fb97b | 2014-04-24 23:19:31 +0200 | [diff] [blame^] | 51 | /* SOF + 00 */ |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 52 | #define ST21NFCA_FRAME_HEADROOM 2 |
| 53 | |
Christophe Ricard | e1fb97b | 2014-04-24 23:19:31 +0200 | [diff] [blame^] | 54 | /* 2 bytes crc + EOF */ |
| 55 | #define ST21NFCA_FRAME_TAILROOM 3 |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 56 | |
| 57 | #define ST21NFCA_HCI_I2C_DRIVER_NAME "st21nfca_hci_i2c" |
| 58 | |
| 59 | static struct i2c_device_id st21nfca_hci_i2c_id_table[] = { |
| 60 | {ST21NFCA_HCI_DRIVER_NAME, 0}, |
| 61 | {} |
| 62 | }; |
| 63 | |
| 64 | MODULE_DEVICE_TABLE(i2c, st21nfca_hci_i2c_id_table); |
| 65 | |
| 66 | struct st21nfca_i2c_phy { |
| 67 | struct i2c_client *i2c_dev; |
| 68 | struct nfc_hci_dev *hdev; |
| 69 | |
| 70 | unsigned int gpio_ena; |
| 71 | unsigned int gpio_irq; |
| 72 | unsigned int irq_polarity; |
| 73 | |
| 74 | struct sk_buff *pending_skb; |
| 75 | int current_read_len; |
| 76 | /* |
| 77 | * crc might have fail because i2c macro |
| 78 | * is disable due to other interface activity |
| 79 | */ |
| 80 | int crc_trials; |
| 81 | |
| 82 | int powered; |
| 83 | int run_mode; |
| 84 | |
| 85 | /* |
| 86 | * < 0 if hardware error occured (e.g. i2c err) |
| 87 | * and prevents normal operation. |
| 88 | */ |
| 89 | int hard_fault; |
| 90 | }; |
| 91 | static u8 len_seq[] = { 13, 24, 15, 29 }; |
| 92 | static u16 wait_tab[] = { 2, 3, 5, 15, 20, 40}; |
| 93 | |
| 94 | #define I2C_DUMP_SKB(info, skb) \ |
| 95 | do { \ |
| 96 | pr_debug("%s:\n", info); \ |
| 97 | print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \ |
| 98 | 16, 1, (skb)->data, (skb)->len, 0); \ |
| 99 | } while (0) |
| 100 | |
Christophe Ricard | 18d2c62 | 2014-04-01 00:34:07 +0200 | [diff] [blame] | 101 | /* |
| 102 | * In order to get the CLF in a known state we generate an internal reboot |
| 103 | * using a proprietary command. |
| 104 | * Once the reboot is completed, we expect to receive a ST21NFCA_SOF_EOF |
| 105 | * fill buffer. |
| 106 | */ |
| 107 | static int st21nfca_hci_platform_init(struct st21nfca_i2c_phy *phy) |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 108 | { |
Christophe Ricard | c5b0c37 | 2014-04-01 00:34:03 +0200 | [diff] [blame] | 109 | u16 wait_reboot[] = { 50, 300, 1000 }; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 110 | char reboot_cmd[] = { 0x7E, 0x66, 0x48, 0xF6, 0x7E }; |
| 111 | u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE]; |
| 112 | int i, r = -1; |
| 113 | |
Christophe Ricard | 18d2c62 | 2014-04-01 00:34:07 +0200 | [diff] [blame] | 114 | for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) { |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 115 | r = i2c_master_send(phy->i2c_dev, reboot_cmd, |
| 116 | sizeof(reboot_cmd)); |
Christophe Ricard | 18d2c62 | 2014-04-01 00:34:07 +0200 | [diff] [blame] | 117 | if (r < 0) |
| 118 | msleep(wait_reboot[i]); |
| 119 | } |
| 120 | if (r < 0) |
| 121 | return r; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 122 | |
Christophe Ricard | 18d2c62 | 2014-04-01 00:34:07 +0200 | [diff] [blame] | 123 | /* CLF is spending about 20ms to do an internal reboot */ |
| 124 | msleep(20); |
| 125 | r = -1; |
| 126 | for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) { |
| 127 | r = i2c_master_recv(phy->i2c_dev, tmp, |
| 128 | ST21NFCA_HCI_LLC_MAX_SIZE); |
| 129 | if (r < 0) |
| 130 | msleep(wait_reboot[i]); |
| 131 | } |
| 132 | if (r < 0) |
| 133 | return r; |
| 134 | |
| 135 | for (i = 0; i < ST21NFCA_HCI_LLC_MAX_SIZE && |
| 136 | tmp[i] == ST21NFCA_SOF_EOF; i++) |
| 137 | ; |
| 138 | |
| 139 | if (r != ST21NFCA_HCI_LLC_MAX_SIZE) |
| 140 | return -ENODEV; |
| 141 | |
| 142 | usleep_range(1000, 1500); |
| 143 | return 0; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static int st21nfca_hci_i2c_enable(void *phy_id) |
| 147 | { |
| 148 | struct st21nfca_i2c_phy *phy = phy_id; |
| 149 | |
| 150 | gpio_set_value(phy->gpio_ena, 1); |
| 151 | phy->powered = 1; |
| 152 | phy->run_mode = ST21NFCA_HCI_MODE; |
| 153 | |
| 154 | usleep_range(10000, 15000); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static void st21nfca_hci_i2c_disable(void *phy_id) |
| 160 | { |
| 161 | struct st21nfca_i2c_phy *phy = phy_id; |
| 162 | |
| 163 | pr_info("\n"); |
| 164 | gpio_set_value(phy->gpio_ena, 0); |
| 165 | |
| 166 | phy->powered = 0; |
| 167 | } |
| 168 | |
Christophe Ricard | e1fb97b | 2014-04-24 23:19:31 +0200 | [diff] [blame^] | 169 | static void st21nfca_hci_add_len_crc(struct sk_buff *skb) |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 170 | { |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 171 | u16 crc; |
| 172 | u8 tmp; |
| 173 | |
| 174 | *skb_push(skb, 1) = 0; |
| 175 | |
| 176 | crc = crc_ccitt(0xffff, skb->data, skb->len); |
| 177 | crc = ~crc; |
| 178 | |
| 179 | tmp = crc & 0x00ff; |
| 180 | *skb_put(skb, 1) = tmp; |
| 181 | |
| 182 | tmp = (crc >> 8) & 0x00ff; |
| 183 | *skb_put(skb, 1) = tmp; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Christophe Ricard | e1fb97b | 2014-04-24 23:19:31 +0200 | [diff] [blame^] | 186 | static void st21nfca_hci_remove_len_crc(struct sk_buff *skb) |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 187 | { |
| 188 | skb_pull(skb, ST21NFCA_FRAME_HEADROOM); |
Christophe Ricard | e1fb97b | 2014-04-24 23:19:31 +0200 | [diff] [blame^] | 189 | skb_trim(skb, skb->len - ST21NFCA_FRAME_TAILROOM); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Writing a frame must not return the number of written bytes. |
| 194 | * It must return either zero for success, or <0 for error. |
| 195 | * In addition, it must not alter the skb |
| 196 | */ |
| 197 | static int st21nfca_hci_i2c_write(void *phy_id, struct sk_buff *skb) |
| 198 | { |
Christophe Ricard | e1fb97b | 2014-04-24 23:19:31 +0200 | [diff] [blame^] | 199 | int r = -1, i, j; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 200 | struct st21nfca_i2c_phy *phy = phy_id; |
| 201 | struct i2c_client *client = phy->i2c_dev; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 202 | u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE * 2]; |
| 203 | |
| 204 | I2C_DUMP_SKB("st21nfca_hci_i2c_write", skb); |
| 205 | |
| 206 | |
| 207 | if (phy->hard_fault != 0) |
| 208 | return phy->hard_fault; |
| 209 | |
| 210 | /* |
| 211 | * Compute CRC before byte stuffing computation on frame |
| 212 | * Note st21nfca_hci_add_len_crc is doing a byte stuffing |
| 213 | * on its own value |
| 214 | */ |
Christophe Ricard | e1fb97b | 2014-04-24 23:19:31 +0200 | [diff] [blame^] | 215 | st21nfca_hci_add_len_crc(skb); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 216 | |
| 217 | /* add ST21NFCA_SOF_EOF on tail */ |
| 218 | *skb_put(skb, 1) = ST21NFCA_SOF_EOF; |
| 219 | /* add ST21NFCA_SOF_EOF on head */ |
| 220 | *skb_push(skb, 1) = ST21NFCA_SOF_EOF; |
| 221 | |
| 222 | /* |
| 223 | * Compute byte stuffing |
| 224 | * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING |
| 225 | * insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte) |
| 226 | * xor byte with ST21NFCA_BYTE_STUFFING_MASK |
| 227 | */ |
| 228 | tmp[0] = skb->data[0]; |
| 229 | for (i = 1, j = 1; i < skb->len - 1; i++, j++) { |
| 230 | if (skb->data[i] == ST21NFCA_SOF_EOF |
| 231 | || skb->data[i] == ST21NFCA_ESCAPE_BYTE_STUFFING) { |
| 232 | tmp[j] = ST21NFCA_ESCAPE_BYTE_STUFFING; |
| 233 | j++; |
| 234 | tmp[j] = skb->data[i] ^ ST21NFCA_BYTE_STUFFING_MASK; |
| 235 | } else { |
| 236 | tmp[j] = skb->data[i]; |
| 237 | } |
| 238 | } |
| 239 | tmp[j] = skb->data[i]; |
| 240 | j++; |
| 241 | |
| 242 | /* |
| 243 | * Manage sleep mode |
| 244 | * Try 3 times to send data with delay between each |
| 245 | */ |
| 246 | for (i = 0; i < ARRAY_SIZE(wait_tab) && r < 0; i++) { |
| 247 | r = i2c_master_send(client, tmp, j); |
| 248 | if (r < 0) |
| 249 | msleep(wait_tab[i]); |
| 250 | } |
| 251 | |
| 252 | if (r >= 0) { |
| 253 | if (r != j) |
| 254 | r = -EREMOTEIO; |
| 255 | else |
| 256 | r = 0; |
| 257 | } |
| 258 | |
Christophe Ricard | e1fb97b | 2014-04-24 23:19:31 +0200 | [diff] [blame^] | 259 | st21nfca_hci_remove_len_crc(skb); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 260 | |
| 261 | return r; |
| 262 | } |
| 263 | |
| 264 | static int get_frame_size(u8 *buf, int buflen) |
| 265 | { |
| 266 | int len = 0; |
| 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; |
| 307 | if (skb->len < 1 || (skb->len > 1 && skb->data[1] != 0)) |
| 308 | return -EBADMSG; |
| 309 | |
| 310 | size = get_frame_size(skb->data, skb->len); |
| 311 | if (size > 0) { |
| 312 | skb_trim(skb, size); |
| 313 | /* remove ST21NFCA byte stuffing for upper layer */ |
| 314 | for (i = 1, j = 0; i < skb->len; i++) { |
Christophe Ricard | 3096e25 | 2014-04-24 23:19:32 +0200 | [diff] [blame] | 315 | if (skb->data[i + j] == |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 316 | (u8) ST21NFCA_ESCAPE_BYTE_STUFFING) { |
Christophe Ricard | 3096e25 | 2014-04-24 23:19:32 +0200 | [diff] [blame] | 317 | skb->data[i] = skb->data[i + j + 1] |
| 318 | | ST21NFCA_BYTE_STUFFING_MASK; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 319 | i++; |
| 320 | j++; |
| 321 | } |
| 322 | skb->data[i] = skb->data[i + j]; |
| 323 | } |
| 324 | /* remove byte stuffing useless byte */ |
| 325 | skb_trim(skb, i - j); |
| 326 | /* remove ST21NFCA_SOF_EOF from head */ |
| 327 | skb_pull(skb, 1); |
| 328 | |
| 329 | r = check_crc(skb->data, skb->len); |
| 330 | if (r != 0) { |
| 331 | i = 0; |
| 332 | return -EBADMSG; |
| 333 | } |
| 334 | |
| 335 | /* remove headbyte */ |
| 336 | skb_pull(skb, 1); |
| 337 | /* remove crc. Byte Stuffing is already removed here */ |
| 338 | skb_trim(skb, skb->len - 2); |
| 339 | return skb->len; |
| 340 | } |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees |
| 346 | * that i2c bus will be flushed and that next read will start on a new frame. |
| 347 | * returned skb contains only LLC header and payload. |
| 348 | * returns: |
| 349 | * frame size : if received frame is complete (find ST21NFCA_SOF_EOF at |
| 350 | * end of read) |
| 351 | * -EAGAIN : if received frame is incomplete (not find ST21NFCA_SOF_EOF |
| 352 | * at end of read) |
| 353 | * -EREMOTEIO : i2c read error (fatal) |
| 354 | * -EBADMSG : frame was incorrect and discarded |
| 355 | * (value returned from st21nfca_hci_i2c_repack) |
| 356 | * -EIO : if no ST21NFCA_SOF_EOF is found after reaching |
| 357 | * the read length end sequence |
| 358 | */ |
| 359 | static int st21nfca_hci_i2c_read(struct st21nfca_i2c_phy *phy, |
| 360 | struct sk_buff *skb) |
| 361 | { |
| 362 | int r, i; |
| 363 | u8 len; |
| 364 | struct i2c_client *client = phy->i2c_dev; |
| 365 | |
| 366 | if (phy->current_read_len < ARRAY_SIZE(len_seq)) { |
| 367 | len = len_seq[phy->current_read_len]; |
| 368 | |
| 369 | /* |
| 370 | * Add retry mecanism |
| 371 | * Operation on I2C interface may fail in case of operation on |
| 372 | * RF or SWP interface |
| 373 | */ |
| 374 | r = 0; |
| 375 | for (i = 0; i < ARRAY_SIZE(wait_tab) && r <= 0; i++) { |
| 376 | r = i2c_master_recv(client, skb_put(skb, len), len); |
| 377 | if (r < 0) |
| 378 | msleep(wait_tab[i]); |
| 379 | } |
| 380 | |
| 381 | if (r != len) { |
| 382 | phy->current_read_len = 0; |
| 383 | return -EREMOTEIO; |
| 384 | } |
| 385 | |
| 386 | if (memchr(skb->data + 2, ST21NFCA_SOF_EOF, |
| 387 | skb->len - 2) != NULL) { |
| 388 | phy->current_read_len = 0; |
| 389 | return st21nfca_hci_i2c_repack(skb); |
| 390 | } |
| 391 | phy->current_read_len++; |
| 392 | return -EAGAIN; |
| 393 | } |
| 394 | return -EIO; |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | * Reads an shdlc frame from the chip. This is not as straightforward as it |
| 399 | * seems. The frame format is data-crc, and corruption can occur anywhere |
| 400 | * while transiting on i2c bus, such that we could read an invalid data. |
| 401 | * The tricky case is when we read a corrupted data or crc. We must detect |
| 402 | * this here in order to determine that data can be transmitted to the hci |
| 403 | * core. This is the reason why we check the crc here. |
| 404 | * The CLF will repeat a frame until we send a RR on that frame. |
| 405 | * |
| 406 | * On ST21NFCA, IRQ goes in idle when read starts. As no size information are |
| 407 | * available in the incoming data, other IRQ might come. Every IRQ will trigger |
| 408 | * a read sequence with different length and will fill the current frame. |
| 409 | * The reception is complete once we reach a ST21NFCA_SOF_EOF. |
| 410 | */ |
| 411 | static irqreturn_t st21nfca_hci_irq_thread_fn(int irq, void *phy_id) |
| 412 | { |
| 413 | struct st21nfca_i2c_phy *phy = phy_id; |
| 414 | struct i2c_client *client; |
| 415 | |
| 416 | int r; |
| 417 | |
| 418 | if (!phy || irq != phy->i2c_dev->irq) { |
| 419 | WARN_ON_ONCE(1); |
| 420 | return IRQ_NONE; |
| 421 | } |
| 422 | |
| 423 | client = phy->i2c_dev; |
| 424 | dev_dbg(&client->dev, "IRQ\n"); |
| 425 | |
| 426 | if (phy->hard_fault != 0) |
| 427 | return IRQ_HANDLED; |
| 428 | |
| 429 | r = st21nfca_hci_i2c_read(phy, phy->pending_skb); |
| 430 | if (r == -EREMOTEIO) { |
| 431 | phy->hard_fault = r; |
| 432 | |
| 433 | nfc_hci_recv_frame(phy->hdev, NULL); |
| 434 | |
| 435 | return IRQ_HANDLED; |
| 436 | } else if (r == -EAGAIN || r == -EIO) { |
| 437 | return IRQ_HANDLED; |
| 438 | } else if (r == -EBADMSG && phy->crc_trials < ARRAY_SIZE(wait_tab)) { |
| 439 | /* |
| 440 | * With ST21NFCA, only one interface (I2C, RF or SWP) |
| 441 | * may be active at a time. |
| 442 | * Having incorrect crc is usually due to i2c macrocell |
| 443 | * deactivation in the middle of a transmission. |
| 444 | * It may generate corrupted data on i2c. |
| 445 | * We give sometime to get i2c back. |
| 446 | * The complete frame will be repeated. |
| 447 | */ |
| 448 | msleep(wait_tab[phy->crc_trials]); |
| 449 | phy->crc_trials++; |
| 450 | phy->current_read_len = 0; |
| 451 | } else if (r > 0) { |
| 452 | /* |
| 453 | * We succeeded to read data from the CLF and |
| 454 | * data is valid. |
| 455 | * Reset counter. |
| 456 | */ |
| 457 | nfc_hci_recv_frame(phy->hdev, phy->pending_skb); |
| 458 | phy->crc_trials = 0; |
| 459 | } |
| 460 | |
| 461 | phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL); |
| 462 | if (phy->pending_skb == NULL) { |
| 463 | phy->hard_fault = -ENOMEM; |
| 464 | nfc_hci_recv_frame(phy->hdev, NULL); |
| 465 | } |
| 466 | |
| 467 | return IRQ_HANDLED; |
| 468 | } |
| 469 | |
| 470 | static struct nfc_phy_ops i2c_phy_ops = { |
| 471 | .write = st21nfca_hci_i2c_write, |
| 472 | .enable = st21nfca_hci_i2c_enable, |
| 473 | .disable = st21nfca_hci_i2c_disable, |
| 474 | }; |
| 475 | |
Christophe Ricard | fcb45e6 | 2014-04-01 00:34:06 +0200 | [diff] [blame] | 476 | static int st21nfca_hci_i2c_request_resources(struct i2c_client *client) |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 477 | { |
| 478 | struct st21nfca_nfc_platform_data *pdata; |
Christophe Ricard | fcb45e6 | 2014-04-01 00:34:06 +0200 | [diff] [blame] | 479 | struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 480 | int r; |
| 481 | |
| 482 | pdata = client->dev.platform_data; |
| 483 | if (pdata == NULL) { |
| 484 | nfc_err(&client->dev, "No platform data\n"); |
| 485 | return -EINVAL; |
| 486 | } |
| 487 | |
| 488 | /* store for later use */ |
| 489 | phy->gpio_irq = pdata->gpio_irq; |
| 490 | phy->gpio_ena = pdata->gpio_ena; |
| 491 | phy->irq_polarity = pdata->irq_polarity; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 492 | |
| 493 | r = devm_gpio_request(&client->dev, phy->gpio_irq, "wake_up"); |
| 494 | if (r) { |
| 495 | pr_err("%s : gpio_request failed\n", __FILE__); |
| 496 | return -ENODEV; |
| 497 | } |
| 498 | |
| 499 | r = gpio_direction_input(phy->gpio_irq); |
| 500 | if (r) { |
| 501 | pr_err("%s : gpio_direction_input failed\n", __FILE__); |
| 502 | return -ENODEV; |
| 503 | } |
| 504 | |
Christophe Ricard | fcb45e6 | 2014-04-01 00:34:06 +0200 | [diff] [blame] | 505 | if (phy->gpio_ena > 0) { |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 506 | r = devm_gpio_request(&client->dev, |
| 507 | phy->gpio_ena, "clf_enable"); |
| 508 | if (r) { |
| 509 | pr_err("%s : ena gpio_request failed\n", __FILE__); |
| 510 | return -ENODEV; |
| 511 | } |
| 512 | r = gpio_direction_output(phy->gpio_ena, 1); |
| 513 | |
| 514 | if (r) { |
| 515 | pr_err("%s : ena gpio_direction_output failed\n", |
| 516 | __FILE__); |
| 517 | return -ENODEV; |
| 518 | } |
| 519 | } |
| 520 | |
Christophe Ricard | fcb45e6 | 2014-04-01 00:34:06 +0200 | [diff] [blame] | 521 | return 0; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | static int st21nfca_hci_i2c_probe(struct i2c_client *client, |
| 525 | const struct i2c_device_id *id) |
| 526 | { |
| 527 | struct st21nfca_i2c_phy *phy; |
| 528 | struct st21nfca_nfc_platform_data *pdata; |
Christophe Ricard | fcb45e6 | 2014-04-01 00:34:06 +0200 | [diff] [blame] | 529 | int r; |
| 530 | int irq; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 531 | |
| 532 | dev_dbg(&client->dev, "%s\n", __func__); |
| 533 | dev_dbg(&client->dev, "IRQ: %d\n", client->irq); |
| 534 | |
| 535 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 536 | nfc_err(&client->dev, "Need I2C_FUNC_I2C\n"); |
| 537 | return -ENODEV; |
| 538 | } |
| 539 | |
| 540 | phy = devm_kzalloc(&client->dev, sizeof(struct st21nfca_i2c_phy), |
| 541 | GFP_KERNEL); |
| 542 | if (!phy) { |
| 543 | nfc_err(&client->dev, |
| 544 | "Cannot allocate memory for st21nfca i2c phy.\n"); |
| 545 | return -ENOMEM; |
| 546 | } |
| 547 | |
| 548 | phy->i2c_dev = client; |
Christophe Ricard | fcb45e6 | 2014-04-01 00:34:06 +0200 | [diff] [blame] | 549 | phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL); |
| 550 | if (phy->pending_skb == NULL) |
| 551 | return -ENOMEM; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 552 | |
Christophe Ricard | fcb45e6 | 2014-04-01 00:34:06 +0200 | [diff] [blame] | 553 | phy->current_read_len = 0; |
| 554 | phy->crc_trials = 0; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 555 | i2c_set_clientdata(client, phy); |
| 556 | |
| 557 | pdata = client->dev.platform_data; |
Christophe Ricard | fcb45e6 | 2014-04-01 00:34:06 +0200 | [diff] [blame] | 558 | if (!pdata) { |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 559 | nfc_err(&client->dev, "No platform data\n"); |
| 560 | return -EINVAL; |
| 561 | } |
| 562 | |
Christophe Ricard | fcb45e6 | 2014-04-01 00:34:06 +0200 | [diff] [blame] | 563 | r = st21nfca_hci_i2c_request_resources(client); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 564 | if (r) { |
| 565 | nfc_err(&client->dev, "Cannot get platform resources\n"); |
| 566 | return r; |
| 567 | } |
| 568 | |
Christophe Ricard | fcb45e6 | 2014-04-01 00:34:06 +0200 | [diff] [blame] | 569 | /* IRQ */ |
| 570 | irq = gpio_to_irq(phy->gpio_irq); |
| 571 | if (irq < 0) { |
| 572 | nfc_err(&client->dev, |
| 573 | "Unable to get irq number for GPIO %d error %d\n", |
| 574 | phy->gpio_irq, r); |
| 575 | return -ENODEV; |
| 576 | } |
| 577 | client->irq = irq; |
| 578 | |
Christophe Ricard | 18d2c62 | 2014-04-01 00:34:07 +0200 | [diff] [blame] | 579 | r = st21nfca_hci_platform_init(phy); |
| 580 | if (r < 0) { |
| 581 | nfc_err(&client->dev, "Unable to reboot st21nfca\n"); |
| 582 | return -ENODEV; |
| 583 | } |
| 584 | |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 585 | r = devm_request_threaded_irq(&client->dev, client->irq, NULL, |
| 586 | st21nfca_hci_irq_thread_fn, |
| 587 | phy->irq_polarity | IRQF_ONESHOT, |
| 588 | ST21NFCA_HCI_DRIVER_NAME, phy); |
| 589 | if (r < 0) { |
| 590 | nfc_err(&client->dev, "Unable to register IRQ handler\n"); |
| 591 | return r; |
| 592 | } |
| 593 | |
Christophe Ricard | 9bac75d | 2014-04-01 00:34:05 +0200 | [diff] [blame] | 594 | return st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME, |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 595 | ST21NFCA_FRAME_HEADROOM, ST21NFCA_FRAME_TAILROOM, |
| 596 | ST21NFCA_HCI_LLC_MAX_PAYLOAD, &phy->hdev); |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | static int st21nfca_hci_i2c_remove(struct i2c_client *client) |
| 600 | { |
| 601 | struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client); |
| 602 | |
| 603 | dev_dbg(&client->dev, "%s\n", __func__); |
| 604 | |
| 605 | st21nfca_hci_remove(phy->hdev); |
| 606 | |
| 607 | if (phy->powered) |
| 608 | st21nfca_hci_i2c_disable(phy); |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | static struct i2c_driver st21nfca_hci_i2c_driver = { |
| 614 | .driver = { |
Christophe Ricard | fcb45e6 | 2014-04-01 00:34:06 +0200 | [diff] [blame] | 615 | .owner = THIS_MODULE, |
| 616 | .name = ST21NFCA_HCI_I2C_DRIVER_NAME, |
| 617 | }, |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 618 | .probe = st21nfca_hci_i2c_probe, |
| 619 | .id_table = st21nfca_hci_i2c_id_table, |
| 620 | .remove = st21nfca_hci_i2c_remove, |
| 621 | }; |
| 622 | |
| 623 | module_i2c_driver(st21nfca_hci_i2c_driver); |
| 624 | |
| 625 | MODULE_LICENSE("GPL"); |
| 626 | MODULE_DESCRIPTION(DRIVER_DESC); |