Lines Matching +full:byte +full:- +full:len
1 // SPDX-License-Identifier: GPL-2.0-only
9 #include <linux/crc-ccitt.h>
30 * called byte stuffing has been introduced.
32 * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
33 * - insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
34 * - xor byte with ST21NFCA_BYTE_STUFFING_MASK
84 16, 1, (skb)->data, (skb)->len, 0); \
98 int i, r = -1; in st21nfca_hci_platform_init()
101 r = i2c_master_send(phy->i2c_dev, reboot_cmd, in st21nfca_hci_platform_init()
111 r = -1; in st21nfca_hci_platform_init()
113 r = i2c_master_recv(phy->i2c_dev, tmp, in st21nfca_hci_platform_init()
126 return -ENODEV; in st21nfca_hci_platform_init()
136 gpiod_set_value(phy->gpiod_ena, 1); in st21nfca_hci_i2c_enable()
137 phy->powered = 1; in st21nfca_hci_i2c_enable()
138 phy->run_mode = ST21NFCA_HCI_MODE; in st21nfca_hci_i2c_enable()
149 gpiod_set_value(phy->gpiod_ena, 0); in st21nfca_hci_i2c_disable()
151 phy->powered = 0; in st21nfca_hci_i2c_disable()
161 crc = crc_ccitt(0xffff, skb->data, skb->len); in st21nfca_hci_add_len_crc()
174 skb_trim(skb, skb->len - ST21NFCA_FRAME_TAILROOM); in st21nfca_hci_remove_len_crc()
184 int r = -1, i, j; in st21nfca_hci_i2c_write()
186 struct i2c_client *client = phy->i2c_dev; in st21nfca_hci_i2c_write()
191 if (phy->hard_fault != 0) in st21nfca_hci_i2c_write()
192 return phy->hard_fault; in st21nfca_hci_i2c_write()
195 * Compute CRC before byte stuffing computation on frame in st21nfca_hci_i2c_write()
196 * Note st21nfca_hci_add_len_crc is doing a byte stuffing in st21nfca_hci_i2c_write()
207 * Compute byte stuffing in st21nfca_hci_i2c_write()
208 * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING in st21nfca_hci_i2c_write()
209 * insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte) in st21nfca_hci_i2c_write()
210 * xor byte with ST21NFCA_BYTE_STUFFING_MASK in st21nfca_hci_i2c_write()
212 tmp[0] = skb->data[0]; in st21nfca_hci_i2c_write()
213 for (i = 1, j = 1; i < skb->len - 1; i++, j++) { in st21nfca_hci_i2c_write()
214 if (skb->data[i] == ST21NFCA_SOF_EOF in st21nfca_hci_i2c_write()
215 || skb->data[i] == ST21NFCA_ESCAPE_BYTE_STUFFING) { in st21nfca_hci_i2c_write()
218 tmp[j] = skb->data[i] ^ ST21NFCA_BYTE_STUFFING_MASK; in st21nfca_hci_i2c_write()
220 tmp[j] = skb->data[i]; in st21nfca_hci_i2c_write()
223 tmp[j] = skb->data[i]; in st21nfca_hci_i2c_write()
230 mutex_lock(&phy->phy_lock); in st21nfca_hci_i2c_write()
236 mutex_unlock(&phy->phy_lock); in st21nfca_hci_i2c_write()
240 r = -EREMOTEIO; in st21nfca_hci_i2c_write()
252 int len = 0; in get_frame_size() local
254 if (buf[len + 1] == ST21NFCA_SOF_EOF) in get_frame_size()
257 for (len = 1; len < buflen && buf[len] != ST21NFCA_SOF_EOF; len++) in get_frame_size()
260 return len; in get_frame_size()
267 crc = crc_ccitt(0xffff, buf, buflen - 2); in check_crc()
270 if (buf[buflen - 2] != (crc & 0xff) || buf[buflen - 1] != (crc >> 8)) { in check_crc()
272 ": CRC error 0x%x != 0x%x 0x%x\n", crc, buf[buflen - 1], in check_crc()
273 buf[buflen - 2]); in check_crc()
278 return -EPERM; in check_crc()
285 * Received data include byte stuffing, crc and sof/eof
288 * frame size without sof/eof, header and byte stuffing
289 * -EBADMSG : frame was incorrect and discarded
295 if (skb->len < 1 || (skb->len > 1 && skb->data[1] != 0)) in st21nfca_hci_i2c_repack()
296 return -EBADMSG; in st21nfca_hci_i2c_repack()
298 size = get_frame_size(skb->data, skb->len); in st21nfca_hci_i2c_repack()
301 /* remove ST21NFCA byte stuffing for upper layer */ in st21nfca_hci_i2c_repack()
302 for (i = 1, j = 0; i < skb->len; i++) { in st21nfca_hci_i2c_repack()
303 if (skb->data[i + j] == in st21nfca_hci_i2c_repack()
305 skb->data[i] = skb->data[i + j + 1] in st21nfca_hci_i2c_repack()
310 skb->data[i] = skb->data[i + j]; in st21nfca_hci_i2c_repack()
312 /* remove byte stuffing useless byte */ in st21nfca_hci_i2c_repack()
313 skb_trim(skb, i - j); in st21nfca_hci_i2c_repack()
317 r = check_crc(skb->data, skb->len); in st21nfca_hci_i2c_repack()
319 return -EBADMSG; in st21nfca_hci_i2c_repack()
323 /* remove crc. Byte Stuffing is already removed here */ in st21nfca_hci_i2c_repack()
324 skb_trim(skb, skb->len - 2); in st21nfca_hci_i2c_repack()
325 return skb->len; in st21nfca_hci_i2c_repack()
337 * -EAGAIN : if received frame is incomplete (not find ST21NFCA_SOF_EOF
339 * -EREMOTEIO : i2c read error (fatal)
340 * -EBADMSG : frame was incorrect and discarded
342 * -EIO : if no ST21NFCA_SOF_EOF is found after reaching
349 u8 len; in st21nfca_hci_i2c_read() local
351 struct i2c_client *client = phy->i2c_dev; in st21nfca_hci_i2c_read()
353 if (phy->current_read_len < ARRAY_SIZE(len_seq)) { in st21nfca_hci_i2c_read()
354 len = len_seq[phy->current_read_len]; in st21nfca_hci_i2c_read()
362 mutex_lock(&phy->phy_lock); in st21nfca_hci_i2c_read()
364 r = i2c_master_recv(client, buf, len); in st21nfca_hci_i2c_read()
368 mutex_unlock(&phy->phy_lock); in st21nfca_hci_i2c_read()
370 if (r != len) { in st21nfca_hci_i2c_read()
371 phy->current_read_len = 0; in st21nfca_hci_i2c_read()
372 return -EREMOTEIO; in st21nfca_hci_i2c_read()
379 if (!phy->current_read_len && !IS_START_OF_FRAME(buf)) { in st21nfca_hci_i2c_read()
381 phy->current_read_len = 0; in st21nfca_hci_i2c_read()
382 return -EIO; in st21nfca_hci_i2c_read()
383 } else if (phy->current_read_len && IS_START_OF_FRAME(buf)) { in st21nfca_hci_i2c_read()
390 phy->current_read_len = 0; in st21nfca_hci_i2c_read()
393 skb_put_data(skb, buf, len); in st21nfca_hci_i2c_read()
395 if (skb->data[skb->len - 1] == ST21NFCA_SOF_EOF) { in st21nfca_hci_i2c_read()
396 phy->current_read_len = 0; in st21nfca_hci_i2c_read()
399 phy->current_read_len++; in st21nfca_hci_i2c_read()
400 return -EAGAIN; in st21nfca_hci_i2c_read()
402 return -EIO; in st21nfca_hci_i2c_read()
407 * seems. The frame format is data-crc, and corruption can occur anywhere
425 if (!phy || irq != phy->i2c_dev->irq) { in st21nfca_hci_irq_thread_fn()
430 if (phy->hard_fault != 0) in st21nfca_hci_irq_thread_fn()
433 r = st21nfca_hci_i2c_read(phy, phy->pending_skb); in st21nfca_hci_irq_thread_fn()
434 if (r == -EREMOTEIO) { in st21nfca_hci_irq_thread_fn()
435 phy->hard_fault = r; in st21nfca_hci_irq_thread_fn()
437 nfc_hci_recv_frame(phy->hdev, NULL); in st21nfca_hci_irq_thread_fn()
440 } else if (r == -EAGAIN || r == -EIO) { in st21nfca_hci_irq_thread_fn()
442 } else if (r == -EBADMSG && phy->crc_trials < ARRAY_SIZE(wait_tab)) { in st21nfca_hci_irq_thread_fn()
452 msleep(wait_tab[phy->crc_trials]); in st21nfca_hci_irq_thread_fn()
453 phy->crc_trials++; in st21nfca_hci_irq_thread_fn()
454 phy->current_read_len = 0; in st21nfca_hci_irq_thread_fn()
455 kfree_skb(phy->pending_skb); in st21nfca_hci_irq_thread_fn()
462 nfc_hci_recv_frame(phy->hdev, phy->pending_skb); in st21nfca_hci_irq_thread_fn()
463 phy->crc_trials = 0; in st21nfca_hci_irq_thread_fn()
465 kfree_skb(phy->pending_skb); in st21nfca_hci_irq_thread_fn()
468 phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL); in st21nfca_hci_irq_thread_fn()
469 if (phy->pending_skb == NULL) { in st21nfca_hci_irq_thread_fn()
470 phy->hard_fault = -ENOMEM; in st21nfca_hci_irq_thread_fn()
471 nfc_hci_recv_frame(phy->hdev, NULL); in st21nfca_hci_irq_thread_fn()
486 { "enable-gpios", &enable_gpios, 1 },
492 struct device *dev = &client->dev; in st21nfca_hci_i2c_probe()
496 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { in st21nfca_hci_i2c_probe()
497 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n"); in st21nfca_hci_i2c_probe()
498 return -ENODEV; in st21nfca_hci_i2c_probe()
501 phy = devm_kzalloc(&client->dev, sizeof(struct st21nfca_i2c_phy), in st21nfca_hci_i2c_probe()
504 return -ENOMEM; in st21nfca_hci_i2c_probe()
506 phy->i2c_dev = client; in st21nfca_hci_i2c_probe()
507 phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL); in st21nfca_hci_i2c_probe()
508 if (phy->pending_skb == NULL) in st21nfca_hci_i2c_probe()
509 return -ENOMEM; in st21nfca_hci_i2c_probe()
511 phy->current_read_len = 0; in st21nfca_hci_i2c_probe()
512 phy->crc_trials = 0; in st21nfca_hci_i2c_probe()
513 mutex_init(&phy->phy_lock); in st21nfca_hci_i2c_probe()
521 phy->gpiod_ena = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW); in st21nfca_hci_i2c_probe()
522 if (IS_ERR(phy->gpiod_ena)) { in st21nfca_hci_i2c_probe()
524 r = PTR_ERR(phy->gpiod_ena); in st21nfca_hci_i2c_probe()
528 phy->se_status.is_ese_present = in st21nfca_hci_i2c_probe()
529 device_property_read_bool(&client->dev, "ese-present"); in st21nfca_hci_i2c_probe()
530 phy->se_status.is_uicc_present = in st21nfca_hci_i2c_probe()
531 device_property_read_bool(&client->dev, "uicc-present"); in st21nfca_hci_i2c_probe()
535 nfc_err(&client->dev, "Unable to reboot st21nfca\n"); in st21nfca_hci_i2c_probe()
539 r = devm_request_threaded_irq(&client->dev, client->irq, NULL, in st21nfca_hci_i2c_probe()
544 nfc_err(&client->dev, "Unable to register IRQ handler\n"); in st21nfca_hci_i2c_probe()
552 &phy->hdev, in st21nfca_hci_i2c_probe()
553 &phy->se_status); in st21nfca_hci_i2c_probe()
560 kfree_skb(phy->pending_skb); in st21nfca_hci_i2c_probe()
568 st21nfca_hci_remove(phy->hdev); in st21nfca_hci_i2c_remove()
570 if (phy->powered) in st21nfca_hci_i2c_remove()
572 kfree_skb(phy->pending_skb); in st21nfca_hci_i2c_remove()
588 { .compatible = "st,st21nfca-i2c", },