Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'nfc-next-3.20-1' of git://git.kernel.org/pub/scm/linux/kernel/git/sameo/nfc-next

NFC: 3.20 first pull request

This is the first NFC pull request for 3.20.

With this one we have:

- Secure element support for the ST Micro st21nfca driver. This depends
on a few HCI internal changes in order for example to support more
than one secure element per controller.

- ACPI support for NXP's pn544 HCI driver. This controller is found on
many x86 SoCs and is typically enumerated on the ACPI bus there.

- A few st21nfca and st21nfcb fixes.

Signed-off-by: David S. Miller <davem@davemloft.net>

+886 -149
+9 -2
Documentation/devicetree/bindings/net/nfc/st21nfca.txt
··· 1 1 * STMicroelectronics SAS. ST21NFCA NFC Controller 2 2 3 3 Required properties: 4 - - compatible: Should be "st,st21nfca_i2c". 4 + - compatible: Should be "st,st21nfca-i2c". 5 5 - clock-frequency: I²C work frequency. 6 6 - reg: address on the bus 7 7 - interrupt-parent: phandle for the interrupt gpio controller ··· 11 11 Optional SoC Specific Properties: 12 12 - pinctrl-names: Contains only one value - "default". 13 13 - pintctrl-0: Specifies the pin control groups used for this controller. 14 + - ese-present: Specifies that an ese is physically connected to the nfc 15 + controller. 16 + - uicc-present: Specifies that the uicc swp signal can be physically 17 + connected to the nfc controller. 14 18 15 19 Example (for ARM-based BeagleBoard xM with ST21NFCA on I2C2): 16 20 ··· 24 20 25 21 st21nfca: st21nfca@1 { 26 22 27 - compatible = "st,st21nfca_i2c"; 23 + compatible = "st,st21nfca-i2c"; 28 24 29 25 reg = <0x01>; 30 26 clock-frequency = <400000>; ··· 33 29 interrupts = <2 IRQ_TYPE_LEVEL_LOW>; 34 30 35 31 enable-gpios = <&gpio5 29 GPIO_ACTIVE_HIGH>; 32 + 33 + ese-present; 34 + uicc-present; 36 35 }; 37 36 };
+2 -2
Documentation/devicetree/bindings/net/nfc/st21nfcb.txt
··· 1 1 * STMicroelectronics SAS. ST21NFCB NFC Controller 2 2 3 3 Required properties: 4 - - compatible: Should be "st,st21nfcb_i2c". 4 + - compatible: Should be "st,st21nfcb-i2c". 5 5 - clock-frequency: I²C work frequency. 6 6 - reg: address on the bus 7 7 - interrupt-parent: phandle for the interrupt gpio controller ··· 20 20 21 21 st21nfcb: st21nfcb@8 { 22 22 23 - compatible = "st,st21nfcb_i2c"; 23 + compatible = "st,st21nfcb-i2c"; 24 24 25 25 reg = <0x08>; 26 26 clock-frequency = <400000>;
+2 -1
drivers/nfc/microread/microread.c
··· 557 557 pr_err("Failed to handle discovered target err=%d\n", r); 558 558 } 559 559 560 - static int microread_event_received(struct nfc_hci_dev *hdev, u8 gate, 560 + static int microread_event_received(struct nfc_hci_dev *hdev, u8 pipe, 561 561 u8 event, struct sk_buff *skb) 562 562 { 563 563 int r; 564 + u8 gate = hdev->pipes[pipe].gate; 564 565 u8 mode; 565 566 566 567 pr_info("Microread received event 0x%x to gate 0x%x\n", event, gate);
+121 -12
drivers/nfc/pn544/i2c.c
··· 24 24 #include <linux/gpio.h> 25 25 #include <linux/of_gpio.h> 26 26 #include <linux/of_irq.h> 27 + #include <linux/acpi.h> 27 28 #include <linux/miscdevice.h> 28 29 #include <linux/interrupt.h> 29 30 #include <linux/delay.h> 30 31 #include <linux/nfc.h> 31 32 #include <linux/firmware.h> 33 + #include <linux/gpio/consumer.h> 32 34 #include <linux/platform_data/pn544.h> 33 35 #include <asm/unaligned.h> 34 36 ··· 42 40 43 41 #define PN544_I2C_FRAME_HEADROOM 1 44 42 #define PN544_I2C_FRAME_TAILROOM 2 43 + 44 + /* GPIO names */ 45 + #define PN544_GPIO_NAME_IRQ "pn544_irq" 46 + #define PN544_GPIO_NAME_FW "pn544_fw" 47 + #define PN544_GPIO_NAME_EN "pn544_en" 45 48 46 49 /* framing in HCI mode */ 47 50 #define PN544_HCI_I2C_LLC_LEN 1 ··· 64 57 }; 65 58 66 59 MODULE_DEVICE_TABLE(i2c, pn544_hci_i2c_id_table); 60 + 61 + static const struct acpi_device_id pn544_hci_i2c_acpi_match[] = { 62 + {"NXP5440", 0}, 63 + {} 64 + }; 65 + 66 + MODULE_DEVICE_TABLE(acpi, pn544_hci_i2c_acpi_match); 67 67 68 68 #define PN544_HCI_I2C_DRIVER_NAME "pn544_hci_i2c" 69 69 ··· 209 195 nfc_info(&phy->i2c_dev->dev, "Detecting nfc_en polarity\n"); 210 196 211 197 /* Disable fw download */ 212 - gpio_set_value(phy->gpio_fw, 0); 198 + gpio_set_value_cansleep(phy->gpio_fw, 0); 213 199 214 200 for (polarity = 0; polarity < 2; polarity++) { 215 201 phy->en_polarity = polarity; 216 202 retry = 3; 217 203 while (retry--) { 218 204 /* power off */ 219 - gpio_set_value(phy->gpio_en, !phy->en_polarity); 205 + gpio_set_value_cansleep(phy->gpio_en, 206 + !phy->en_polarity); 220 207 usleep_range(10000, 15000); 221 208 222 209 /* power on */ 223 - gpio_set_value(phy->gpio_en, phy->en_polarity); 210 + gpio_set_value_cansleep(phy->gpio_en, phy->en_polarity); 224 211 usleep_range(10000, 15000); 225 212 226 213 /* send reset */ ··· 240 225 "Could not detect nfc_en polarity, fallback to active high\n"); 241 226 242 227 out: 243 - gpio_set_value(phy->gpio_en, !phy->en_polarity); 228 + gpio_set_value_cansleep(phy->gpio_en, !phy->en_polarity); 244 229 } 245 230 246 231 static void pn544_hci_i2c_enable_mode(struct pn544_i2c_phy *phy, int run_mode) 247 232 { 248 - gpio_set_value(phy->gpio_fw, run_mode == PN544_FW_MODE ? 1 : 0); 249 - gpio_set_value(phy->gpio_en, phy->en_polarity); 233 + gpio_set_value_cansleep(phy->gpio_fw, 234 + run_mode == PN544_FW_MODE ? 1 : 0); 235 + gpio_set_value_cansleep(phy->gpio_en, phy->en_polarity); 250 236 usleep_range(10000, 15000); 251 237 252 238 phy->run_mode = run_mode; ··· 270 254 { 271 255 struct pn544_i2c_phy *phy = phy_id; 272 256 273 - gpio_set_value(phy->gpio_fw, 0); 274 - gpio_set_value(phy->gpio_en, !phy->en_polarity); 257 + gpio_set_value_cansleep(phy->gpio_fw, 0); 258 + gpio_set_value_cansleep(phy->gpio_en, !phy->en_polarity); 275 259 usleep_range(10000, 15000); 276 260 277 - gpio_set_value(phy->gpio_en, phy->en_polarity); 261 + gpio_set_value_cansleep(phy->gpio_en, phy->en_polarity); 278 262 usleep_range(10000, 15000); 279 263 280 - gpio_set_value(phy->gpio_en, !phy->en_polarity); 264 + gpio_set_value_cansleep(phy->gpio_en, !phy->en_polarity); 281 265 usleep_range(10000, 15000); 282 266 283 267 phy->powered = 0; ··· 875 859 } 876 860 } 877 861 862 + static int pn544_hci_i2c_acpi_request_resources(struct i2c_client *client) 863 + { 864 + struct pn544_i2c_phy *phy = i2c_get_clientdata(client); 865 + const struct acpi_device_id *id; 866 + struct gpio_desc *gpiod_en, *gpiod_irq, *gpiod_fw; 867 + struct device *dev; 868 + int ret; 869 + 870 + if (!client) 871 + return -EINVAL; 872 + 873 + dev = &client->dev; 874 + 875 + /* Match the struct device against a given list of ACPI IDs */ 876 + id = acpi_match_device(dev->driver->acpi_match_table, dev); 877 + 878 + if (!id) 879 + return -ENODEV; 880 + 881 + /* Get EN GPIO from ACPI */ 882 + gpiod_en = devm_gpiod_get_index(dev, PN544_GPIO_NAME_EN, 1); 883 + if (IS_ERR(gpiod_en)) { 884 + nfc_err(dev, 885 + "Unable to get EN GPIO\n"); 886 + return -ENODEV; 887 + } 888 + 889 + phy->gpio_en = desc_to_gpio(gpiod_en); 890 + 891 + /* Configuration EN GPIO */ 892 + ret = gpiod_direction_output(gpiod_en, 0); 893 + if (ret) { 894 + nfc_err(dev, "Fail EN pin direction\n"); 895 + return ret; 896 + } 897 + 898 + /* Get FW GPIO from ACPI */ 899 + gpiod_fw = devm_gpiod_get_index(dev, PN544_GPIO_NAME_FW, 2); 900 + if (IS_ERR(gpiod_fw)) { 901 + nfc_err(dev, 902 + "Unable to get FW GPIO\n"); 903 + return -ENODEV; 904 + } 905 + 906 + phy->gpio_fw = desc_to_gpio(gpiod_fw); 907 + 908 + /* Configuration FW GPIO */ 909 + ret = gpiod_direction_output(gpiod_fw, 0); 910 + if (ret) { 911 + nfc_err(dev, "Fail FW pin direction\n"); 912 + return ret; 913 + } 914 + 915 + /* Get IRQ GPIO */ 916 + gpiod_irq = devm_gpiod_get_index(dev, PN544_GPIO_NAME_IRQ, 0); 917 + if (IS_ERR(gpiod_irq)) { 918 + nfc_err(dev, 919 + "Unable to get IRQ GPIO\n"); 920 + return -ENODEV; 921 + } 922 + 923 + phy->gpio_irq = desc_to_gpio(gpiod_irq); 924 + 925 + /* Configure IRQ GPIO */ 926 + ret = gpiod_direction_input(gpiod_irq); 927 + if (ret) { 928 + nfc_err(dev, "Fail IRQ pin direction\n"); 929 + return ret; 930 + } 931 + 932 + /* Map the pin to an IRQ */ 933 + ret = gpiod_to_irq(gpiod_irq); 934 + if (ret < 0) { 935 + nfc_err(dev, "Fail pin IRQ mapping\n"); 936 + return ret; 937 + } 938 + 939 + nfc_info(dev, "GPIO resource, no:%d irq:%d\n", 940 + desc_to_gpio(gpiod_irq), ret); 941 + client->irq = ret; 942 + 943 + return 0; 944 + } 945 + 878 946 #ifdef CONFIG_OF 879 947 880 948 static int pn544_hci_i2c_of_request_resources(struct i2c_client *client) ··· 984 884 phy->gpio_en = ret; 985 885 986 886 /* Configuration of EN GPIO */ 987 - ret = gpio_request(phy->gpio_en, "pn544_en"); 887 + ret = gpio_request(phy->gpio_en, PN544_GPIO_NAME_EN); 988 888 if (ret) { 989 889 nfc_err(&client->dev, "Fail EN pin\n"); 990 890 goto err_dt; ··· 1006 906 phy->gpio_fw = ret; 1007 907 1008 908 /* Configuration of FW GPIO */ 1009 - ret = gpio_request(phy->gpio_fw, "pn544_fw"); 909 + ret = gpio_request(phy->gpio_fw, PN544_GPIO_NAME_FW); 1010 910 if (ret) { 1011 911 nfc_err(&client->dev, "Fail FW pin\n"); 1012 912 goto err_gpio_en; ··· 1101 1001 phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE); 1102 1002 phy->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET); 1103 1003 phy->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ); 1004 + /* Using ACPI */ 1005 + } else if (ACPI_HANDLE(&client->dev)) { 1006 + r = pn544_hci_i2c_acpi_request_resources(client); 1007 + if (r) { 1008 + nfc_err(&client->dev, 1009 + "Cannot get ACPI data\n"); 1010 + return r; 1011 + } 1104 1012 } else { 1105 1013 nfc_err(&client->dev, "No platform data\n"); 1106 1014 return -EINVAL; ··· 1188 1080 .name = PN544_HCI_I2C_DRIVER_NAME, 1189 1081 .owner = THIS_MODULE, 1190 1082 .of_match_table = of_match_ptr(of_pn544_i2c_match), 1083 + .acpi_match_table = ACPI_PTR(pn544_hci_i2c_acpi_match), 1191 1084 }, 1192 1085 .probe = pn544_hci_i2c_probe, 1193 1086 .id_table = pn544_hci_i2c_id_table,
+2 -1
drivers/nfc/pn544/pn544.c
··· 724 724 * <= 0: driver handled the event, skb consumed 725 725 * 1: driver does not handle the event, please do standard processing 726 726 */ 727 - static int pn544_hci_event_received(struct nfc_hci_dev *hdev, u8 gate, u8 event, 727 + static int pn544_hci_event_received(struct nfc_hci_dev *hdev, u8 pipe, u8 event, 728 728 struct sk_buff *skb) 729 729 { 730 730 struct sk_buff *rgb_skb = NULL; 731 + u8 gate = hdev->pipes[pipe].gate; 731 732 int r; 732 733 733 734 pr_debug("hci event %d\n", event);
+1 -1
drivers/nfc/st21nfca/Makefile
··· 2 2 # Makefile for ST21NFCA HCI based NFC driver 3 3 # 4 4 5 - st21nfca_hci-objs = st21nfca.o st21nfca_dep.o 5 + st21nfca_hci-objs = st21nfca.o st21nfca_dep.o st21nfca_se.o 6 6 obj-$(CONFIG_NFC_ST21NFCA) += st21nfca_hci.o 7 7 8 8 st21nfca_i2c-objs = i2c.o
+17 -6
drivers/nfc/st21nfca/i2c.c
··· 74 74 unsigned int gpio_ena; 75 75 unsigned int irq_polarity; 76 76 77 + struct st21nfca_se_status se_status; 78 + 77 79 struct sk_buff *pending_skb; 78 80 int current_read_len; 79 81 /* ··· 539 537 540 538 phy->irq_polarity = irq_get_trigger_type(client->irq); 541 539 540 + phy->se_status.is_ese_present = 541 + of_property_read_bool(pp, "ese-present"); 542 + phy->se_status.is_uicc_present = 543 + of_property_read_bool(pp, "uicc-present"); 544 + 542 545 return 0; 543 546 } 544 547 #else ··· 578 571 } 579 572 } 580 573 574 + phy->se_status.is_ese_present = pdata->is_ese_present; 575 + phy->se_status.is_uicc_present = pdata->is_uicc_present; 576 + 581 577 return 0; 582 578 } 583 579 ··· 601 591 602 592 phy = devm_kzalloc(&client->dev, sizeof(struct st21nfca_i2c_phy), 603 593 GFP_KERNEL); 604 - if (!phy) { 605 - nfc_err(&client->dev, 606 - "Cannot allocate memory for st21nfca i2c phy.\n"); 594 + if (!phy) 607 595 return -ENOMEM; 608 - } 609 596 610 597 phy->i2c_dev = client; 611 598 phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL); ··· 648 641 } 649 642 650 643 return st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME, 651 - ST21NFCA_FRAME_HEADROOM, ST21NFCA_FRAME_TAILROOM, 652 - ST21NFCA_HCI_LLC_MAX_PAYLOAD, &phy->hdev); 644 + ST21NFCA_FRAME_HEADROOM, 645 + ST21NFCA_FRAME_TAILROOM, 646 + ST21NFCA_HCI_LLC_MAX_PAYLOAD, 647 + &phy->hdev, 648 + &phy->se_status); 653 649 } 654 650 655 651 static int st21nfca_hci_i2c_remove(struct i2c_client *client) ··· 671 661 672 662 #ifdef CONFIG_OF 673 663 static const struct of_device_id of_st21nfca_i2c_match[] = { 664 + { .compatible = "st,st21nfca-i2c", }, 674 665 { .compatible = "st,st21nfca_i2c", }, 675 666 {} 676 667 };
+142 -44
drivers/nfc/st21nfca/st21nfca.c
··· 23 23 24 24 #include "st21nfca.h" 25 25 #include "st21nfca_dep.h" 26 + #include "st21nfca_se.h" 26 27 27 28 #define DRIVER_DESC "HCI NFC driver for ST21NFCA" 28 29 ··· 63 62 #define ST21NFCA_RF_CARD_F_DATARATE 0x08 64 63 #define ST21NFCA_RF_CARD_F_DATARATE_212_424 0x01 65 64 66 - #define ST21NFCA_DEVICE_MGNT_GATE 0x01 67 65 #define ST21NFCA_DEVICE_MGNT_PIPE 0x02 68 66 69 67 #define ST21NFCA_DM_GETINFO 0x13 ··· 78 78 79 79 #define ST21NFCA_NFC_MODE 0x03 /* NFC_MODE parameter*/ 80 80 81 + #define ST21NFCA_EVT_HOT_PLUG 0x03 82 + #define ST21NFCA_EVT_HOT_PLUG_IS_INHIBITED(x) (x->data[0] & 0x80) 83 + 84 + #define ST21NFCA_SE_TO_PIPES 2000 85 + 81 86 static DECLARE_BITMAP(dev_mask, ST21NFCA_NUM_DEVICES); 82 87 83 88 static struct nfc_hci_gate st21nfca_gates[] = { ··· 97 92 {ST21NFCA_RF_READER_14443_3_A_GATE, NFC_HCI_INVALID_PIPE}, 98 93 {ST21NFCA_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE}, 99 94 {ST21NFCA_RF_CARD_F_GATE, NFC_HCI_INVALID_PIPE}, 95 + 96 + /* Secure element pipes are created by secure element host */ 97 + {ST21NFCA_CONNECTIVITY_GATE, NFC_HCI_DO_NOT_CREATE_PIPE}, 98 + {ST21NFCA_APDU_READER_GATE, NFC_HCI_DO_NOT_CREATE_PIPE}, 100 99 }; 101 100 102 101 struct st21nfca_pipe_info { ··· 127 118 NFC_HCI_TERMINAL_HOST_ID, 0 128 119 }; 129 120 130 - skb_pipe_list = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL); 131 - if (!skb_pipe_list) { 132 - r = -ENOMEM; 133 - goto free_list; 134 - } 135 - 136 - skb_pipe_info = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL); 137 - if (!skb_pipe_info) { 138 - r = -ENOMEM; 139 - goto free_info; 140 - } 141 - 142 121 /* On ST21NFCA device pipes number are dynamics 143 122 * A maximum of 16 pipes can be created at the same time 144 123 * If pipes are already created, hci_dev_up will fail. ··· 145 148 * Pipe can be closed and need to be open. 146 149 */ 147 150 r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID, 148 - ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE); 151 + ST21NFCA_DEVICE_MGNT_GATE, 152 + ST21NFCA_DEVICE_MGNT_PIPE); 149 153 if (r < 0) 150 154 goto free_info; 151 155 ··· 177 179 * - destination gid (1byte) 178 180 */ 179 181 info = (struct st21nfca_pipe_info *) skb_pipe_info->data; 182 + if (info->dst_gate_id == ST21NFCA_APDU_READER_GATE && 183 + info->src_host_id != ST21NFCA_ESE_HOST_ID) { 184 + pr_err("Unexpected apdu_reader pipe on host %x\n", 185 + info->src_host_id); 186 + continue; 187 + } 188 + 180 189 for (j = 0; (j < ARRAY_SIZE(st21nfca_gates)) && 181 - (st21nfca_gates[j].gate != info->dst_gate_id); 182 - j++) 190 + (st21nfca_gates[j].gate != info->dst_gate_id) ; j++) 183 191 ; 184 192 185 193 if (j < ARRAY_SIZE(st21nfca_gates) && 186 194 st21nfca_gates[j].gate == info->dst_gate_id && 187 195 ST21NFCA_DM_IS_PIPE_OPEN(info->pipe_state)) { 188 196 st21nfca_gates[j].pipe = pipe_info[2]; 197 + 189 198 hdev->gate2pipe[st21nfca_gates[j].gate] = 190 - st21nfca_gates[j].pipe; 199 + st21nfca_gates[j].pipe; 200 + hdev->pipes[st21nfca_gates[j].pipe].gate = 201 + st21nfca_gates[j].gate; 202 + hdev->pipes[st21nfca_gates[j].pipe].dest_host = 203 + info->src_host_id; 191 204 } 192 205 } 193 206 ··· 208 199 */ 209 200 if (skb_pipe_list->len + 3 < ARRAY_SIZE(st21nfca_gates)) { 210 201 for (i = skb_pipe_list->len + 3; 211 - i < ARRAY_SIZE(st21nfca_gates); i++) { 202 + i < ARRAY_SIZE(st21nfca_gates) - 2; i++) { 212 203 r = nfc_hci_connect_gate(hdev, 213 204 NFC_HCI_HOST_CONTROLLER_ID, 214 205 st21nfca_gates[i].gate, ··· 221 212 memcpy(hdev->init_data.gates, st21nfca_gates, sizeof(st21nfca_gates)); 222 213 free_info: 223 214 kfree_skb(skb_pipe_info); 224 - free_list: 225 215 kfree_skb(skb_pipe_list); 226 216 return r; 227 217 } ··· 265 257 266 258 static int st21nfca_hci_ready(struct nfc_hci_dev *hdev) 267 259 { 260 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 268 261 struct sk_buff *skb; 269 262 270 263 u8 param; 264 + u8 white_list[2]; 265 + int wl_size = 0; 271 266 int r; 272 267 273 - param = NFC_HCI_UICC_HOST_ID; 274 - r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE, 275 - NFC_HCI_ADMIN_WHITELIST, &param, 1); 276 - if (r < 0) 277 - return r; 268 + if (info->se_status->is_ese_present && 269 + info->se_status->is_uicc_present) { 270 + white_list[wl_size++] = NFC_HCI_UICC_HOST_ID; 271 + white_list[wl_size++] = ST21NFCA_ESE_HOST_ID; 272 + } else if (!info->se_status->is_ese_present && 273 + info->se_status->is_uicc_present) { 274 + white_list[wl_size++] = NFC_HCI_UICC_HOST_ID; 275 + } else if (info->se_status->is_ese_present && 276 + !info->se_status->is_uicc_present) { 277 + white_list[wl_size++] = ST21NFCA_ESE_HOST_ID; 278 + } 279 + 280 + if (wl_size) { 281 + r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE, 282 + NFC_HCI_ADMIN_WHITELIST, 283 + (u8 *) &white_list, wl_size); 284 + if (r < 0) 285 + return r; 286 + } 278 287 279 288 /* Set NFC_MODE in device management gate to enable */ 280 289 r = nfc_hci_get_param(hdev, ST21NFCA_DEVICE_MGNT_GATE, ··· 299 274 if (r < 0) 300 275 return r; 301 276 302 - if (skb->data[0] == 0) { 303 - kfree_skb(skb); 277 + param = skb->data[0]; 278 + kfree_skb(skb); 279 + if (param == 0) { 304 280 param = 1; 305 281 306 282 r = nfc_hci_set_param(hdev, ST21NFCA_DEVICE_MGNT_GATE, ··· 443 417 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE, 444 418 ST21NFCA_RF_CARD_F_DATARATE, 445 419 param, 1); 446 - if (r < 0) 420 + if (r < 0) { 421 + kfree_skb(datarate_skb); 447 422 return r; 423 + } 448 424 } 425 + kfree_skb(datarate_skb); 449 426 450 427 /* 451 428 * Configure sens_res ··· 702 673 struct nfc_target *target) 703 674 { 704 675 int r; 705 - struct sk_buff *nfcid2_skb = NULL, *nfcid1_skb; 676 + struct sk_buff *nfcid_skb = NULL; 706 677 707 678 if (gate == ST21NFCA_RF_READER_F_GATE) { 708 679 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE, 709 - ST21NFCA_RF_READER_F_NFCID2, &nfcid2_skb); 680 + ST21NFCA_RF_READER_F_NFCID2, &nfcid_skb); 710 681 if (r < 0) 711 682 goto exit; 712 683 713 - if (nfcid2_skb->len > NFC_SENSF_RES_MAXSIZE) { 684 + if (nfcid_skb->len > NFC_SENSF_RES_MAXSIZE) { 714 685 r = -EPROTO; 715 686 goto exit; 716 687 } ··· 722 693 * - After the reception of SEL_RES with NFCIP-1 compliant bit 723 694 * set for type A frame NFCID1 will be updated 724 695 */ 725 - if (nfcid2_skb->len > 0) { 696 + if (nfcid_skb->len > 0) { 726 697 /* P2P in type F */ 727 - memcpy(target->sensf_res, nfcid2_skb->data, 728 - nfcid2_skb->len); 729 - target->sensf_res_len = nfcid2_skb->len; 698 + memcpy(target->sensf_res, nfcid_skb->data, 699 + nfcid_skb->len); 700 + target->sensf_res_len = nfcid_skb->len; 730 701 /* NFC Forum Digital Protocol Table 44 */ 731 702 if (target->sensf_res[0] == 0x01 && 732 703 target->sensf_res[1] == 0xfe) ··· 736 707 target->supported_protocols = 737 708 NFC_PROTO_FELICA_MASK; 738 709 } else { 710 + kfree_skb(nfcid_skb); 739 711 /* P2P in type A */ 740 712 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE, 741 713 ST21NFCA_RF_READER_F_NFCID1, 742 - &nfcid1_skb); 714 + &nfcid_skb); 743 715 if (r < 0) 744 716 goto exit; 745 717 746 - if (nfcid1_skb->len > NFC_NFCID1_MAXSIZE) { 718 + if (nfcid_skb->len > NFC_NFCID1_MAXSIZE) { 747 719 r = -EPROTO; 748 720 goto exit; 749 721 } 750 - memcpy(target->sensf_res, nfcid1_skb->data, 751 - nfcid1_skb->len); 752 - target->sensf_res_len = nfcid1_skb->len; 722 + memcpy(target->sensf_res, nfcid_skb->data, 723 + nfcid_skb->len); 724 + target->sensf_res_len = nfcid_skb->len; 753 725 target->supported_protocols = NFC_PROTO_NFC_DEP_MASK; 754 726 } 755 727 target->hci_reader_gate = ST21NFCA_RF_READER_F_GATE; 756 728 } 757 729 r = 1; 758 730 exit: 759 - kfree_skb(nfcid2_skb); 731 + kfree_skb(nfcid_skb); 760 732 return r; 761 733 } 762 734 ··· 859 829 } 860 830 } 861 831 832 + static void st21nfca_hci_cmd_received(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd, 833 + struct sk_buff *skb) 834 + { 835 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 836 + u8 gate = hdev->pipes[pipe].gate; 837 + 838 + pr_debug("cmd: %x\n", cmd); 839 + 840 + switch (cmd) { 841 + case NFC_HCI_ANY_OPEN_PIPE: 842 + if (gate != ST21NFCA_APDU_READER_GATE && 843 + hdev->pipes[pipe].dest_host != NFC_HCI_UICC_HOST_ID) 844 + info->se_info.count_pipes++; 845 + 846 + if (info->se_info.count_pipes == info->se_info.expected_pipes) { 847 + del_timer_sync(&info->se_info.se_active_timer); 848 + info->se_info.se_active = false; 849 + info->se_info.count_pipes = 0; 850 + complete(&info->se_info.req_completion); 851 + } 852 + break; 853 + } 854 + } 855 + 856 + static int st21nfca_admin_event_received(struct nfc_hci_dev *hdev, u8 event, 857 + struct sk_buff *skb) 858 + { 859 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 860 + 861 + pr_debug("admin event: %x\n", event); 862 + 863 + switch (event) { 864 + case ST21NFCA_EVT_HOT_PLUG: 865 + if (info->se_info.se_active) { 866 + if (!ST21NFCA_EVT_HOT_PLUG_IS_INHIBITED(skb)) { 867 + del_timer_sync(&info->se_info.se_active_timer); 868 + info->se_info.se_active = false; 869 + complete(&info->se_info.req_completion); 870 + } else { 871 + mod_timer(&info->se_info.se_active_timer, 872 + jiffies + 873 + msecs_to_jiffies(ST21NFCA_SE_TO_PIPES)); 874 + } 875 + } 876 + break; 877 + } 878 + kfree_skb(skb); 879 + return 0; 880 + } 881 + 862 882 /* 863 883 * Returns: 864 884 * <= 0: driver handled the event, skb consumed 865 885 * 1: driver does not handle the event, please do standard processing 866 886 */ 867 - static int st21nfca_hci_event_received(struct nfc_hci_dev *hdev, u8 gate, 887 + static int st21nfca_hci_event_received(struct nfc_hci_dev *hdev, u8 pipe, 868 888 u8 event, struct sk_buff *skb) 869 889 { 890 + u8 gate = hdev->pipes[pipe].gate; 891 + u8 host = hdev->pipes[pipe].dest_host; 892 + 870 893 pr_debug("hci event: %d gate: %x\n", event, gate); 871 894 872 895 switch (gate) { 896 + case NFC_HCI_ADMIN_GATE: 897 + return st21nfca_admin_event_received(hdev, event, skb); 873 898 case ST21NFCA_RF_CARD_F_GATE: 874 899 return st21nfca_dep_event_received(hdev, event, skb); 900 + case ST21NFCA_CONNECTIVITY_GATE: 901 + return st21nfca_connectivity_event_received(hdev, host, 902 + event, skb); 903 + case ST21NFCA_APDU_READER_GATE: 904 + return st21nfca_apdu_reader_event_received(hdev, event, skb); 875 905 default: 876 906 return 1; 877 907 } 878 - kfree_skb(skb); 879 - return 0; 880 908 } 881 909 882 910 static struct nfc_hci_ops st21nfca_hci_ops = { ··· 953 865 .tm_send = st21nfca_hci_tm_send, 954 866 .check_presence = st21nfca_hci_check_presence, 955 867 .event_received = st21nfca_hci_event_received, 868 + .cmd_received = st21nfca_hci_cmd_received, 869 + .discover_se = st21nfca_hci_discover_se, 870 + .enable_se = st21nfca_hci_enable_se, 871 + .disable_se = st21nfca_hci_disable_se, 872 + .se_io = st21nfca_hci_se_io, 956 873 }; 957 874 958 875 int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, 959 876 char *llc_name, int phy_headroom, int phy_tailroom, 960 - int phy_payload, struct nfc_hci_dev **hdev) 877 + int phy_payload, struct nfc_hci_dev **hdev, 878 + struct st21nfca_se_status *se_status) 961 879 { 962 880 struct st21nfca_hci_info *info; 963 881 int r = 0; ··· 1023 929 goto err_alloc_hdev; 1024 930 } 1025 931 932 + info->se_status = se_status; 933 + 1026 934 nfc_hci_set_clientdata(info->hdev, info); 1027 935 1028 936 r = nfc_hci_register_device(info->hdev); ··· 1033 937 1034 938 *hdev = info->hdev; 1035 939 st21nfca_dep_init(info->hdev); 940 + st21nfca_se_init(info->hdev); 1036 941 1037 942 return 0; 1038 943 ··· 1052 955 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 1053 956 1054 957 st21nfca_dep_deinit(hdev); 958 + st21nfca_se_deinit(hdev); 1055 959 nfc_hci_unregister_device(hdev); 1056 960 nfc_hci_free_device(hdev); 1057 961 kfree(info);
+16 -5
drivers/nfc/st21nfca/st21nfca.h
··· 20 20 #include <net/nfc/hci.h> 21 21 22 22 #include "st21nfca_dep.h" 23 + #include "st21nfca_se.h" 23 24 24 25 #define HCI_MODE 0 25 26 ··· 52 51 53 52 #define ST21NFCA_NUM_DEVICES 256 54 53 54 + struct st21nfca_se_status { 55 + bool is_ese_present; 56 + bool is_uicc_present; 57 + }; 58 + 55 59 int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, 56 60 char *llc_name, int phy_headroom, int phy_tailroom, 57 - int phy_payload, struct nfc_hci_dev **hdev); 61 + int phy_payload, struct nfc_hci_dev **hdev, 62 + struct st21nfca_se_status *se_status); 58 63 void st21nfca_hci_remove(struct nfc_hci_dev *hdev); 59 64 60 65 enum st21nfca_state { ··· 73 66 void *phy_id; 74 67 75 68 struct nfc_hci_dev *hdev; 69 + struct st21nfca_se_status *se_status; 76 70 77 71 enum st21nfca_state state; 78 72 ··· 84 76 void *async_cb_context; 85 77 86 78 struct st21nfca_dep_info dep_info; 79 + struct st21nfca_se_info se_info; 87 80 }; 88 81 89 82 /* Reader RF commands */ 90 - #define ST21NFCA_WR_XCHG_DATA 0x10 83 + #define ST21NFCA_WR_XCHG_DATA 0x10 91 84 92 - #define ST21NFCA_RF_READER_F_GATE 0x14 93 - 94 - #define ST21NFCA_RF_CARD_F_GATE 0x24 85 + #define ST21NFCA_DEVICE_MGNT_GATE 0x01 86 + #define ST21NFCA_RF_READER_F_GATE 0x14 87 + #define ST21NFCA_RF_CARD_F_GATE 0x24 88 + #define ST21NFCA_APDU_READER_GATE 0xf0 89 + #define ST21NFCA_CONNECTIVITY_GATE 0x41 95 90 96 91 #endif /* __LOCAL_ST21NFCA_H_ */
+390
drivers/nfc/st21nfca/st21nfca_se.c
··· 1 + /* 2 + * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved. 3 + * 4 + * This program is free software; you can redistribute it and/or modify it 5 + * under the terms and conditions of the GNU General Public License, 6 + * version 2, as published by the Free Software Foundation. 7 + * 8 + * This program is distributed in the hope that it will be useful, 9 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 + * GNU General Public License for more details. 12 + * 13 + * You should have received a copy of the GNU General Public License 14 + * along with this program; if not, see <http://www.gnu.org/licenses/>. 15 + */ 16 + 17 + #include <net/nfc/hci.h> 18 + 19 + #include "st21nfca.h" 20 + #include "st21nfca_se.h" 21 + 22 + #define ST21NFCA_EVT_UICC_ACTIVATE 0x10 23 + #define ST21NFCA_EVT_UICC_DEACTIVATE 0x13 24 + #define ST21NFCA_EVT_SE_HARD_RESET 0x20 25 + #define ST21NFCA_EVT_SE_SOFT_RESET 0x11 26 + #define ST21NFCA_EVT_SE_END_OF_APDU_TRANSFER 0x21 27 + #define ST21NFCA_EVT_SE_ACTIVATE 0x22 28 + #define ST21NFCA_EVT_SE_DEACTIVATE 0x23 29 + 30 + #define ST21NFCA_EVT_TRANSMIT_DATA 0x10 31 + #define ST21NFCA_EVT_WTX_REQUEST 0x11 32 + 33 + #define ST21NFCA_EVT_CONNECTIVITY 0x10 34 + #define ST21NFCA_EVT_TRANSACTION 0x12 35 + 36 + #define ST21NFCA_ESE_HOST_ID 0xc0 37 + 38 + #define ST21NFCA_SE_TO_HOT_PLUG 1000 39 + /* Connectivity pipe only */ 40 + #define ST21NFCA_SE_COUNT_PIPE_UICC 0x01 41 + /* Connectivity + APDU Reader pipe */ 42 + #define ST21NFCA_SE_COUNT_PIPE_EMBEDDED 0x02 43 + 44 + #define ST21NFCA_SE_MODE_OFF 0x00 45 + #define ST21NFCA_SE_MODE_ON 0x01 46 + 47 + #define ST21NFCA_PARAM_ATR 0x01 48 + #define ST21NFCA_ATR_DEFAULT_BWI 0x04 49 + 50 + /* 51 + * WT = 2^BWI/10[s], convert into msecs and add a secure 52 + * room by increasing by 2 this timeout 53 + */ 54 + #define ST21NFCA_BWI_TO_TIMEOUT(x) ((1 << x) * 200) 55 + #define ST21NFCA_ATR_GET_Y_FROM_TD(x) (x >> 4) 56 + 57 + /* If TA is present bit 0 is set */ 58 + #define ST21NFCA_ATR_TA_PRESENT(x) (x & 0x01) 59 + /* If TB is present bit 1 is set */ 60 + #define ST21NFCA_ATR_TB_PRESENT(x) (x & 0x02) 61 + 62 + static u8 st21nfca_se_get_bwi(struct nfc_hci_dev *hdev) 63 + { 64 + int i; 65 + u8 td; 66 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 67 + 68 + /* Bits 8 to 5 of the first TB for T=1 encode BWI from zero to nine */ 69 + for (i = 1; i < ST21NFCA_ESE_MAX_LENGTH; i++) { 70 + td = ST21NFCA_ATR_GET_Y_FROM_TD(info->se_info.atr[i]); 71 + if (ST21NFCA_ATR_TA_PRESENT(td)) 72 + i++; 73 + if (ST21NFCA_ATR_TB_PRESENT(td)) { 74 + i++; 75 + return info->se_info.atr[i] >> 4; 76 + } 77 + } 78 + return ST21NFCA_ATR_DEFAULT_BWI; 79 + } 80 + 81 + static void st21nfca_se_get_atr(struct nfc_hci_dev *hdev) 82 + { 83 + int r; 84 + struct sk_buff *skb; 85 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 86 + 87 + r = nfc_hci_get_param(hdev, ST21NFCA_APDU_READER_GATE, 88 + ST21NFCA_PARAM_ATR, &skb); 89 + if (r < 0) 90 + return; 91 + 92 + if (skb->len <= ST21NFCA_ESE_MAX_LENGTH) { 93 + memcpy(info->se_info.atr, skb->data, skb->len); 94 + info->se_info.wt_timeout = 95 + ST21NFCA_BWI_TO_TIMEOUT(st21nfca_se_get_bwi(hdev)); 96 + } 97 + kfree_skb(skb); 98 + } 99 + 100 + static int st21nfca_hci_control_se(struct nfc_hci_dev *hdev, u32 se_idx, 101 + u8 state) 102 + { 103 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 104 + int r; 105 + struct sk_buff *sk_host_list; 106 + u8 se_event, host_id; 107 + 108 + switch (se_idx) { 109 + case NFC_HCI_UICC_HOST_ID: 110 + se_event = (state == ST21NFCA_SE_MODE_ON ? 111 + ST21NFCA_EVT_UICC_ACTIVATE : 112 + ST21NFCA_EVT_UICC_DEACTIVATE); 113 + 114 + info->se_info.count_pipes = 0; 115 + info->se_info.expected_pipes = ST21NFCA_SE_COUNT_PIPE_UICC; 116 + break; 117 + case ST21NFCA_ESE_HOST_ID: 118 + se_event = (state == ST21NFCA_SE_MODE_ON ? 119 + ST21NFCA_EVT_SE_ACTIVATE : 120 + ST21NFCA_EVT_SE_DEACTIVATE); 121 + 122 + info->se_info.count_pipes = 0; 123 + info->se_info.expected_pipes = ST21NFCA_SE_COUNT_PIPE_EMBEDDED; 124 + break; 125 + default: 126 + return -EINVAL; 127 + } 128 + 129 + /* 130 + * Wait for an EVT_HOT_PLUG in order to 131 + * retrieve a relevant host list. 132 + */ 133 + reinit_completion(&info->se_info.req_completion); 134 + r = nfc_hci_send_event(hdev, ST21NFCA_DEVICE_MGNT_GATE, se_event, 135 + NULL, 0); 136 + if (r < 0) 137 + return r; 138 + 139 + mod_timer(&info->se_info.se_active_timer, jiffies + 140 + msecs_to_jiffies(ST21NFCA_SE_TO_HOT_PLUG)); 141 + info->se_info.se_active = true; 142 + 143 + /* Ignore return value and check in any case the host_list */ 144 + wait_for_completion_interruptible(&info->se_info.req_completion); 145 + 146 + r = nfc_hci_get_param(hdev, NFC_HCI_ADMIN_GATE, 147 + NFC_HCI_ADMIN_HOST_LIST, 148 + &sk_host_list); 149 + if (r < 0) 150 + return r; 151 + 152 + host_id = sk_host_list->data[sk_host_list->len - 1]; 153 + kfree_skb(sk_host_list); 154 + 155 + if (state == ST21NFCA_SE_MODE_ON && host_id == se_idx) 156 + return se_idx; 157 + else if (state == ST21NFCA_SE_MODE_OFF && host_id != se_idx) 158 + return se_idx; 159 + 160 + return -1; 161 + } 162 + 163 + int st21nfca_hci_discover_se(struct nfc_hci_dev *hdev) 164 + { 165 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 166 + int se_count = 0; 167 + 168 + if (info->se_status->is_uicc_present) { 169 + nfc_add_se(hdev->ndev, NFC_HCI_UICC_HOST_ID, NFC_SE_UICC); 170 + se_count++; 171 + } 172 + 173 + if (info->se_status->is_ese_present) { 174 + nfc_add_se(hdev->ndev, ST21NFCA_ESE_HOST_ID, NFC_SE_EMBEDDED); 175 + se_count++; 176 + } 177 + 178 + return !se_count; 179 + } 180 + EXPORT_SYMBOL(st21nfca_hci_discover_se); 181 + 182 + int st21nfca_hci_enable_se(struct nfc_hci_dev *hdev, u32 se_idx) 183 + { 184 + int r; 185 + 186 + /* 187 + * According to upper layer, se_idx == NFC_SE_UICC when 188 + * info->se_status->is_uicc_enable is true should never happen. 189 + * Same for eSE. 190 + */ 191 + r = st21nfca_hci_control_se(hdev, se_idx, ST21NFCA_SE_MODE_ON); 192 + 193 + if (r == ST21NFCA_ESE_HOST_ID) { 194 + st21nfca_se_get_atr(hdev); 195 + r = nfc_hci_send_event(hdev, ST21NFCA_APDU_READER_GATE, 196 + ST21NFCA_EVT_SE_SOFT_RESET, NULL, 0); 197 + if (r < 0) 198 + return r; 199 + } else if (r < 0) { 200 + /* 201 + * The activation tentative failed, the secure element 202 + * is not connected. Remove from the list. 203 + */ 204 + nfc_remove_se(hdev->ndev, se_idx); 205 + return r; 206 + } 207 + 208 + return 0; 209 + } 210 + EXPORT_SYMBOL(st21nfca_hci_enable_se); 211 + 212 + int st21nfca_hci_disable_se(struct nfc_hci_dev *hdev, u32 se_idx) 213 + { 214 + int r; 215 + 216 + /* 217 + * According to upper layer, se_idx == NFC_SE_UICC when 218 + * info->se_status->is_uicc_enable is true should never happen 219 + * Same for eSE. 220 + */ 221 + r = st21nfca_hci_control_se(hdev, se_idx, ST21NFCA_SE_MODE_OFF); 222 + if (r < 0) 223 + return r; 224 + 225 + return 0; 226 + } 227 + EXPORT_SYMBOL(st21nfca_hci_disable_se); 228 + 229 + int st21nfca_hci_se_io(struct nfc_hci_dev *hdev, u32 se_idx, 230 + u8 *apdu, size_t apdu_length, 231 + se_io_cb_t cb, void *cb_context) 232 + { 233 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 234 + 235 + pr_debug("se_io %x\n", se_idx); 236 + 237 + switch (se_idx) { 238 + case ST21NFCA_ESE_HOST_ID: 239 + info->se_info.cb = cb; 240 + info->se_info.cb_context = cb_context; 241 + mod_timer(&info->se_info.bwi_timer, jiffies + 242 + msecs_to_jiffies(info->se_info.wt_timeout)); 243 + info->se_info.bwi_active = true; 244 + return nfc_hci_send_event(hdev, ST21NFCA_APDU_READER_GATE, 245 + ST21NFCA_EVT_TRANSMIT_DATA, 246 + apdu, apdu_length); 247 + default: 248 + return -ENODEV; 249 + } 250 + } 251 + EXPORT_SYMBOL(st21nfca_hci_se_io); 252 + 253 + static void st21nfca_se_wt_timeout(unsigned long data) 254 + { 255 + /* 256 + * No answer from the secure element 257 + * within the defined timeout. 258 + * Let's send a reset request as recovery procedure. 259 + * According to the situation, we first try to send a software reset 260 + * to the secure element. If the next command is still not 261 + * answering in time, we send to the CLF a secure element hardware 262 + * reset request. 263 + */ 264 + /* hardware reset managed through VCC_UICC_OUT power supply */ 265 + u8 param = 0x01; 266 + struct st21nfca_hci_info *info = (struct st21nfca_hci_info *) data; 267 + 268 + pr_debug("\n"); 269 + 270 + info->se_info.bwi_active = false; 271 + 272 + if (!info->se_info.xch_error) { 273 + info->se_info.xch_error = true; 274 + nfc_hci_send_event(info->hdev, ST21NFCA_APDU_READER_GATE, 275 + ST21NFCA_EVT_SE_SOFT_RESET, NULL, 0); 276 + } else { 277 + info->se_info.xch_error = false; 278 + nfc_hci_send_event(info->hdev, ST21NFCA_DEVICE_MGNT_GATE, 279 + ST21NFCA_EVT_SE_HARD_RESET, &param, 1); 280 + } 281 + info->se_info.cb(info->se_info.cb_context, NULL, 0, -ETIME); 282 + } 283 + 284 + static void st21nfca_se_activation_timeout(unsigned long data) 285 + { 286 + struct st21nfca_hci_info *info = (struct st21nfca_hci_info *) data; 287 + 288 + pr_debug("\n"); 289 + 290 + info->se_info.se_active = false; 291 + 292 + complete(&info->se_info.req_completion); 293 + } 294 + 295 + /* 296 + * Returns: 297 + * <= 0: driver handled the event, skb consumed 298 + * 1: driver does not handle the event, please do standard processing 299 + */ 300 + int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host, 301 + u8 event, struct sk_buff *skb) 302 + { 303 + int r = 0; 304 + 305 + pr_debug("connectivity gate event: %x\n", event); 306 + 307 + switch (event) { 308 + case ST21NFCA_EVT_CONNECTIVITY: 309 + break; 310 + case ST21NFCA_EVT_TRANSACTION: 311 + break; 312 + default: 313 + return 1; 314 + } 315 + kfree_skb(skb); 316 + return r; 317 + } 318 + EXPORT_SYMBOL(st21nfca_connectivity_event_received); 319 + 320 + int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev, 321 + u8 event, struct sk_buff *skb) 322 + { 323 + int r = 0; 324 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 325 + 326 + pr_debug("apdu reader gate event: %x\n", event); 327 + 328 + switch (event) { 329 + case ST21NFCA_EVT_TRANSMIT_DATA: 330 + del_timer_sync(&info->se_info.bwi_timer); 331 + info->se_info.bwi_active = false; 332 + r = nfc_hci_send_event(hdev, ST21NFCA_DEVICE_MGNT_GATE, 333 + ST21NFCA_EVT_SE_END_OF_APDU_TRANSFER, NULL, 0); 334 + if (r < 0) 335 + goto exit; 336 + 337 + info->se_info.cb(info->se_info.cb_context, 338 + skb->data, skb->len, 0); 339 + break; 340 + case ST21NFCA_EVT_WTX_REQUEST: 341 + mod_timer(&info->se_info.bwi_timer, jiffies + 342 + msecs_to_jiffies(info->se_info.wt_timeout)); 343 + break; 344 + } 345 + 346 + exit: 347 + kfree_skb(skb); 348 + return r; 349 + } 350 + EXPORT_SYMBOL(st21nfca_apdu_reader_event_received); 351 + 352 + void st21nfca_se_init(struct nfc_hci_dev *hdev) 353 + { 354 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 355 + 356 + init_completion(&info->se_info.req_completion); 357 + /* initialize timers */ 358 + init_timer(&info->se_info.bwi_timer); 359 + info->se_info.bwi_timer.data = (unsigned long)info; 360 + info->se_info.bwi_timer.function = st21nfca_se_wt_timeout; 361 + info->se_info.bwi_active = false; 362 + 363 + init_timer(&info->se_info.se_active_timer); 364 + info->se_info.se_active_timer.data = (unsigned long)info; 365 + info->se_info.se_active_timer.function = st21nfca_se_activation_timeout; 366 + info->se_info.se_active = false; 367 + 368 + info->se_info.count_pipes = 0; 369 + info->se_info.expected_pipes = 0; 370 + 371 + info->se_info.xch_error = false; 372 + 373 + info->se_info.wt_timeout = 374 + ST21NFCA_BWI_TO_TIMEOUT(ST21NFCA_ATR_DEFAULT_BWI); 375 + } 376 + EXPORT_SYMBOL(st21nfca_se_init); 377 + 378 + void st21nfca_se_deinit(struct nfc_hci_dev *hdev) 379 + { 380 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 381 + 382 + if (info->se_info.bwi_active) 383 + del_timer_sync(&info->se_info.bwi_timer); 384 + if (info->se_info.se_active) 385 + del_timer_sync(&info->se_info.se_active_timer); 386 + 387 + info->se_info.bwi_active = false; 388 + info->se_info.se_active = false; 389 + } 390 + EXPORT_SYMBOL(st21nfca_se_deinit);
+63
drivers/nfc/st21nfca/st21nfca_se.h
··· 1 + /* 2 + * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved. 3 + * 4 + * This program is free software; you can redistribute it and/or modify it 5 + * under the terms and conditions of the GNU General Public License, 6 + * version 2, as published by the Free Software Foundation. 7 + * 8 + * This program is distributed in the hope that it will be useful, 9 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 + * GNU General Public License for more details. 12 + * 13 + * You should have received a copy of the GNU General Public License 14 + * along with this program; if not, see <http://www.gnu.org/licenses/>. 15 + */ 16 + 17 + #ifndef __ST21NFCA_SE_H 18 + #define __ST21NFCA_SE_H 19 + 20 + #include <linux/skbuff.h> 21 + #include <linux/workqueue.h> 22 + 23 + /* 24 + * ref ISO7816-3 chap 8.1. the initial character TS is followed by a 25 + * sequence of at most 32 characters. 26 + */ 27 + #define ST21NFCA_ESE_MAX_LENGTH 33 28 + #define ST21NFCA_ESE_HOST_ID 0xc0 29 + 30 + struct st21nfca_se_info { 31 + u8 atr[ST21NFCA_ESE_MAX_LENGTH]; 32 + struct completion req_completion; 33 + 34 + struct timer_list bwi_timer; 35 + int wt_timeout; /* in msecs */ 36 + bool bwi_active; 37 + 38 + struct timer_list se_active_timer; 39 + bool se_active; 40 + int expected_pipes; 41 + int count_pipes; 42 + 43 + bool xch_error; 44 + 45 + se_io_cb_t cb; 46 + void *cb_context; 47 + }; 48 + 49 + int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host, 50 + u8 event, struct sk_buff *skb); 51 + int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev, 52 + u8 event, struct sk_buff *skb); 53 + 54 + int st21nfca_hci_discover_se(struct nfc_hci_dev *hdev); 55 + int st21nfca_hci_enable_se(struct nfc_hci_dev *hdev, u32 se_idx); 56 + int st21nfca_hci_disable_se(struct nfc_hci_dev *hdev, u32 se_idx); 57 + int st21nfca_hci_se_io(struct nfc_hci_dev *hdev, u32 se_idx, 58 + u8 *apdu, size_t apdu_length, 59 + se_io_cb_t cb, void *cb_context); 60 + 61 + void st21nfca_se_init(struct nfc_hci_dev *hdev); 62 + void st21nfca_se_deinit(struct nfc_hci_dev *hdev); 63 + #endif /* __ST21NFCA_SE_H */
+12 -7
drivers/nfc/st21nfcb/i2c.c
··· 199 199 struct sk_buff *skb = NULL; 200 200 int r; 201 201 202 - if (!phy || irq != phy->i2c_dev->irq) { 202 + if (!phy || !phy->ndlc || irq != phy->i2c_dev->irq) { 203 203 WARN_ON_ONCE(1); 204 204 return IRQ_NONE; 205 205 } ··· 343 343 return -ENODEV; 344 344 } 345 345 346 + r = ndlc_probe(phy, &i2c_phy_ops, &client->dev, 347 + ST21NFCB_FRAME_HEADROOM, ST21NFCB_FRAME_TAILROOM, 348 + &phy->ndlc); 349 + if (r < 0) { 350 + nfc_err(&client->dev, "Unable to register ndlc layer\n"); 351 + return r; 352 + } 353 + 346 354 r = devm_request_threaded_irq(&client->dev, client->irq, NULL, 347 355 st21nfcb_nci_irq_thread_fn, 348 356 phy->irq_polarity | IRQF_ONESHOT, 349 357 ST21NFCB_NCI_DRIVER_NAME, phy); 350 - if (r < 0) { 358 + if (r < 0) 351 359 nfc_err(&client->dev, "Unable to register IRQ handler\n"); 352 - return r; 353 - } 354 360 355 - return ndlc_probe(phy, &i2c_phy_ops, &client->dev, 356 - ST21NFCB_FRAME_HEADROOM, ST21NFCB_FRAME_TAILROOM, 357 - &phy->ndlc); 361 + return r; 358 362 } 359 363 360 364 static int st21nfcb_nci_i2c_remove(struct i2c_client *client) ··· 377 373 378 374 #ifdef CONFIG_OF 379 375 static const struct of_device_id of_st21nfcb_i2c_match[] = { 376 + { .compatible = "st,st21nfcb-i2c", }, 380 377 { .compatible = "st,st21nfcb_i2c", }, 381 378 {} 382 379 };
+1 -2
drivers/nfc/st21nfcb/ndlc.c
··· 138 138 default: 139 139 pr_err("UNKNOWN Packet Control Byte=%d\n", pcb); 140 140 kfree_skb(skb); 141 - break; 141 + continue; 142 142 } 143 143 skb_queue_head(&ndlc->send_q, skb); 144 144 } ··· 297 297 skb_queue_purge(&ndlc->send_q); 298 298 299 299 st21nfcb_nci_remove(ndlc->ndev); 300 - kfree(ndlc); 301 300 } 302 301 EXPORT_SYMBOL(ndlc_remove);
+2
include/linux/platform_data/st21nfca.h
··· 26 26 struct st21nfca_nfc_platform_data { 27 27 unsigned int gpio_ena; 28 28 unsigned int irq_polarity; 29 + bool is_ese_present; 30 + bool is_uicc_present; 29 31 }; 30 32 31 33 #endif /* _ST21NFCA_HCI_H_ */
+1 -3
include/linux/platform_data/st21nfcb.h
··· 19 19 #ifndef _ST21NFCB_NCI_H_ 20 20 #define _ST21NFCB_NCI_H_ 21 21 22 - #include <linux/i2c.h> 23 - 24 22 #define ST21NFCB_NCI_DRIVER_NAME "st21nfcb_nci" 25 23 26 24 struct st21nfcb_nfc_platform_data { ··· 26 28 unsigned int irq_polarity; 27 29 }; 28 30 29 - #endif /* _ST21NFCA_HCI_H_ */ 31 + #endif /* _ST21NFCB_NCI_H_ */
+21 -4
include/net/nfc/hci.h
··· 51 51 int (*tm_send)(struct nfc_hci_dev *hdev, struct sk_buff *skb); 52 52 int (*check_presence)(struct nfc_hci_dev *hdev, 53 53 struct nfc_target *target); 54 - int (*event_received)(struct nfc_hci_dev *hdev, u8 gate, u8 event, 54 + int (*event_received)(struct nfc_hci_dev *hdev, u8 pipe, u8 event, 55 55 struct sk_buff *skb); 56 + void (*cmd_received)(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd, 57 + struct sk_buff *skb); 56 58 int (*fw_download)(struct nfc_hci_dev *hdev, const char *firmware_name); 57 59 int (*discover_se)(struct nfc_hci_dev *dev); 58 60 int (*enable_se)(struct nfc_hci_dev *dev, u32 se_idx); ··· 65 63 }; 66 64 67 65 /* Pipes */ 68 - #define NFC_HCI_INVALID_PIPE 0x80 69 66 #define NFC_HCI_DO_NOT_CREATE_PIPE 0x81 67 + #define NFC_HCI_INVALID_PIPE 0x80 68 + #define NFC_HCI_INVALID_GATE 0xFF 69 + #define NFC_HCI_INVALID_HOST 0x80 70 70 #define NFC_HCI_LINK_MGMT_PIPE 0x00 71 71 #define NFC_HCI_ADMIN_PIPE 0x01 72 72 ··· 77 73 u8 pipe; 78 74 }; 79 75 76 + struct nfc_hci_pipe { 77 + u8 gate; 78 + u8 dest_host; 79 + }; 80 + 80 81 #define NFC_HCI_MAX_CUSTOM_GATES 50 82 + #define NFC_HCI_MAX_PIPES 127 81 83 struct nfc_hci_init_data { 82 84 u8 gate_count; 83 85 struct nfc_hci_gate gates[NFC_HCI_MAX_CUSTOM_GATES]; ··· 135 125 void *clientdata; 136 126 137 127 u8 gate2pipe[NFC_HCI_MAX_GATES]; 128 + struct nfc_hci_pipe pipes[NFC_HCI_MAX_PIPES]; 138 129 139 130 u8 sw_romlib; 140 131 u8 sw_patch; ··· 178 167 void nfc_hci_driver_failure(struct nfc_hci_dev *hdev, int err); 179 168 180 169 int nfc_hci_result_to_errno(u8 result); 170 + void nfc_hci_reset_pipes(struct nfc_hci_dev *dev); 171 + void nfc_hci_reset_pipes_per_host(struct nfc_hci_dev *hdev, u8 host); 181 172 182 173 /* Host IDs */ 183 174 #define NFC_HCI_HOST_CONTROLLER_ID 0x00 ··· 232 219 #define NFC_HCI_EVT_POST_DATA 0x02 233 220 #define NFC_HCI_EVT_HOT_PLUG 0x03 234 221 222 + /* Generic commands */ 223 + #define NFC_HCI_ANY_SET_PARAMETER 0x01 224 + #define NFC_HCI_ANY_GET_PARAMETER 0x02 225 + #define NFC_HCI_ANY_OPEN_PIPE 0x03 226 + #define NFC_HCI_ANY_CLOSE_PIPE 0x04 227 + 235 228 /* Reader RF gates events */ 236 229 #define NFC_HCI_EVT_READER_REQUESTED 0x10 237 230 #define NFC_HCI_EVT_END_OPERATION 0x11 ··· 268 249 int nfc_hci_send_cmd_async(struct nfc_hci_dev *hdev, u8 gate, u8 cmd, 269 250 const u8 *param, size_t param_len, 270 251 data_exchange_cb_t cb, void *cb_context); 271 - int nfc_hci_send_response(struct nfc_hci_dev *hdev, u8 gate, u8 response, 272 - const u8 *param, size_t param_len); 273 252 int nfc_hci_send_event(struct nfc_hci_dev *hdev, u8 gate, u8 event, 274 253 const u8 *param, size_t param_len); 275 254 int nfc_hci_target_discovered(struct nfc_hci_dev *hdev, u8 gate);
-2
net/nfc/core.c
··· 555 555 556 556 int nfc_enable_se(struct nfc_dev *dev, u32 se_idx) 557 557 { 558 - 559 558 struct nfc_se *se; 560 559 int rc; 561 560 ··· 604 605 605 606 int nfc_disable_se(struct nfc_dev *dev, u32 se_idx) 606 607 { 607 - 608 608 struct nfc_se *se; 609 609 int rc; 610 610
+4 -19
net/nfc/hci/command.c
··· 116 116 } 117 117 EXPORT_SYMBOL(nfc_hci_send_event); 118 118 119 - int nfc_hci_send_response(struct nfc_hci_dev *hdev, u8 gate, u8 response, 120 - const u8 *param, size_t param_len) 121 - { 122 - u8 pipe; 123 - 124 - pr_debug("\n"); 125 - 126 - pipe = hdev->gate2pipe[gate]; 127 - if (pipe == NFC_HCI_INVALID_PIPE) 128 - return -EADDRNOTAVAIL; 129 - 130 - return nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_RESPONSE, 131 - response, param, param_len, NULL, NULL, 132 - 0); 133 - } 134 - EXPORT_SYMBOL(nfc_hci_send_response); 135 - 136 119 /* 137 120 * Execute an hci command sent to gate. 138 121 * skb will contain response data if success. skb can be NULL if you are not ··· 314 331 if (r < 0) 315 332 return r; 316 333 317 - memset(hdev->gate2pipe, NFC_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe)); 334 + nfc_hci_reset_pipes(hdev); 318 335 319 336 return 0; 320 337 } ··· 328 345 329 346 pr_debug("\n"); 330 347 331 - if (hdev->gate2pipe[dest_gate] == NFC_HCI_DO_NOT_CREATE_PIPE) 348 + if (pipe == NFC_HCI_DO_NOT_CREATE_PIPE) 332 349 return 0; 333 350 334 351 if (hdev->gate2pipe[dest_gate] != NFC_HCI_INVALID_PIPE) ··· 363 380 return r; 364 381 } 365 382 383 + hdev->pipes[pipe].gate = dest_gate; 384 + hdev->pipes[pipe].dest_host = dest_host; 366 385 hdev->gate2pipe[dest_gate] = pipe; 367 386 368 387 return 0;
+72 -25
net/nfc/hci/core.c
··· 46 46 } 47 47 EXPORT_SYMBOL(nfc_hci_result_to_errno); 48 48 49 + void nfc_hci_reset_pipes(struct nfc_hci_dev *hdev) 50 + { 51 + int i = 0; 52 + 53 + for (i = 0; i < NFC_HCI_MAX_PIPES; i++) { 54 + hdev->pipes[i].gate = NFC_HCI_INVALID_GATE; 55 + hdev->pipes[i].dest_host = NFC_HCI_INVALID_HOST; 56 + } 57 + memset(hdev->gate2pipe, NFC_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe)); 58 + } 59 + EXPORT_SYMBOL(nfc_hci_reset_pipes); 60 + 61 + void nfc_hci_reset_pipes_per_host(struct nfc_hci_dev *hdev, u8 host) 62 + { 63 + int i = 0; 64 + 65 + for (i = 0; i < NFC_HCI_MAX_PIPES; i++) { 66 + if (hdev->pipes[i].dest_host != host) 67 + continue; 68 + 69 + hdev->pipes[i].gate = NFC_HCI_INVALID_GATE; 70 + hdev->pipes[i].dest_host = NFC_HCI_INVALID_HOST; 71 + } 72 + } 73 + EXPORT_SYMBOL(nfc_hci_reset_pipes_per_host); 74 + 49 75 static void nfc_hci_msg_tx_work(struct work_struct *work) 50 76 { 51 77 struct nfc_hci_dev *hdev = container_of(work, struct nfc_hci_dev, ··· 193 167 void nfc_hci_cmd_received(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd, 194 168 struct sk_buff *skb) 195 169 { 196 - int r = 0; 197 - u8 gate = nfc_hci_pipe2gate(hdev, pipe); 198 - u8 local_gate, new_pipe; 199 - u8 gate_opened = 0x00; 170 + u8 gate = hdev->pipes[pipe].gate; 171 + u8 status = NFC_HCI_ANY_OK; 172 + struct hci_create_pipe_resp *create_info; 173 + struct hci_delete_pipe_noti *delete_info; 174 + struct hci_all_pipe_cleared_noti *cleared_info; 200 175 201 176 pr_debug("from gate %x pipe %x cmd %x\n", gate, pipe, cmd); 202 177 203 178 switch (cmd) { 204 179 case NFC_HCI_ADM_NOTIFY_PIPE_CREATED: 205 180 if (skb->len != 5) { 206 - r = -EPROTO; 207 - break; 181 + status = NFC_HCI_ANY_E_NOK; 182 + goto exit; 208 183 } 184 + create_info = (struct hci_create_pipe_resp *)skb->data; 209 185 210 - local_gate = skb->data[3]; 211 - new_pipe = skb->data[4]; 212 - nfc_hci_send_response(hdev, gate, NFC_HCI_ANY_OK, NULL, 0); 213 - 214 - /* save the new created pipe and bind with local gate, 186 + /* Save the new created pipe and bind with local gate, 215 187 * the description for skb->data[3] is destination gate id 216 188 * but since we received this cmd from host controller, we 217 189 * are the destination and it is our local gate 218 190 */ 219 - hdev->gate2pipe[local_gate] = new_pipe; 191 + hdev->gate2pipe[create_info->dest_gate] = create_info->pipe; 192 + hdev->pipes[create_info->pipe].gate = create_info->dest_gate; 193 + hdev->pipes[create_info->pipe].dest_host = 194 + create_info->src_host; 220 195 break; 221 196 case NFC_HCI_ANY_OPEN_PIPE: 222 - /* if the pipe is already created, we allow remote host to 223 - * open it 224 - */ 225 - if (gate != 0xff) 226 - nfc_hci_send_response(hdev, gate, NFC_HCI_ANY_OK, 227 - &gate_opened, 1); 197 + if (gate == NFC_HCI_INVALID_GATE) { 198 + status = NFC_HCI_ANY_E_NOK; 199 + goto exit; 200 + } 201 + break; 202 + case NFC_HCI_ADM_NOTIFY_PIPE_DELETED: 203 + if (skb->len != 1) { 204 + status = NFC_HCI_ANY_E_NOK; 205 + goto exit; 206 + } 207 + delete_info = (struct hci_delete_pipe_noti *)skb->data; 208 + 209 + hdev->pipes[delete_info->pipe].gate = NFC_HCI_INVALID_GATE; 210 + hdev->pipes[delete_info->pipe].dest_host = NFC_HCI_INVALID_HOST; 228 211 break; 229 212 case NFC_HCI_ADM_NOTIFY_ALL_PIPE_CLEARED: 230 - nfc_hci_send_response(hdev, gate, NFC_HCI_ANY_OK, NULL, 0); 213 + if (skb->len != 1) { 214 + status = NFC_HCI_ANY_E_NOK; 215 + goto exit; 216 + } 217 + cleared_info = (struct hci_all_pipe_cleared_noti *)skb->data; 218 + 219 + nfc_hci_reset_pipes_per_host(hdev, cleared_info->host); 231 220 break; 232 221 default: 233 222 pr_info("Discarded unknown cmd %x to gate %x\n", cmd, gate); 234 - r = -EINVAL; 235 223 break; 236 224 } 225 + 226 + if (hdev->ops->cmd_received) 227 + hdev->ops->cmd_received(hdev, pipe, cmd, skb); 228 + 229 + exit: 230 + nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_RESPONSE, 231 + status, NULL, 0, NULL, NULL, 0); 237 232 238 233 kfree_skb(skb); 239 234 } ··· 377 330 struct sk_buff *skb) 378 331 { 379 332 int r = 0; 380 - u8 gate = nfc_hci_pipe2gate(hdev, pipe); 333 + u8 gate = hdev->pipes[pipe].gate; 381 334 382 - if (gate == 0xff) { 335 + if (gate == NFC_HCI_INVALID_GATE) { 383 336 pr_err("Discarded event %x to unopened pipe %x\n", event, pipe); 384 337 goto exit; 385 338 } 386 339 387 340 if (hdev->ops->event_received) { 388 - r = hdev->ops->event_received(hdev, gate, event, skb); 341 + r = hdev->ops->event_received(hdev, pipe, event, skb); 389 342 if (r <= 0) 390 343 goto exit_noskb; 391 344 } ··· 620 573 if (hdev->ops->close) 621 574 hdev->ops->close(hdev); 622 575 623 - memset(hdev->gate2pipe, NFC_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe)); 576 + nfc_hci_reset_pipes(hdev); 624 577 625 578 return 0; 626 579 } ··· 979 932 980 933 nfc_set_drvdata(hdev->ndev, hdev); 981 934 982 - memset(hdev->gate2pipe, NFC_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe)); 935 + nfc_hci_reset_pipes(hdev); 983 936 984 937 hdev->quirks = quirks; 985 938
+8 -2
net/nfc/hci/hci.h
··· 65 65 u8 pipe; 66 66 } __packed; 67 67 68 + struct hci_delete_pipe_noti { 69 + u8 pipe; 70 + } __packed; 71 + 72 + struct hci_all_pipe_cleared_noti { 73 + u8 host; 74 + } __packed; 75 + 68 76 #define NFC_HCI_FRAGMENT 0x7f 69 77 70 78 #define HCP_HEADER(type, instr) ((((type) & 0x03) << 6) | ((instr) & 0x3f)) ··· 84 76 const u8 *payload, size_t payload_len, 85 77 data_exchange_cb_t cb, void *cb_context, 86 78 unsigned long completion_delay); 87 - 88 - u8 nfc_hci_pipe2gate(struct nfc_hci_dev *hdev, u8 pipe); 89 79 90 80 void nfc_hci_hcp_message_rx(struct nfc_hci_dev *hdev, u8 pipe, u8 type, 91 81 u8 instruction, struct sk_buff *skb);
-11
net/nfc/hci/hcp.c
··· 124 124 return err; 125 125 } 126 126 127 - u8 nfc_hci_pipe2gate(struct nfc_hci_dev *hdev, u8 pipe) 128 - { 129 - int gate; 130 - 131 - for (gate = 0; gate < NFC_HCI_MAX_GATES; gate++) 132 - if (hdev->gate2pipe[gate] == pipe) 133 - return gate; 134 - 135 - return 0xff; 136 - } 137 - 138 127 /* 139 128 * Receive hcp message for pipe, with type and cmd. 140 129 * skb contains optional message data only.