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

Merge branch 'for-upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/bluetooth/bluetooth-next

Johan Hedberg says:

====================
pull request: bluetooth-next 2017-04-14

Here's the main batch of Bluetooth & 802.15.4 patches for the 4.12
kernel.

- Many fixes to 6LoWPAN, in particular for BLE
- New CA8210 IEEE 802.15.4 device driver (accounting for most of the
lines of code added in this pull request)
- Added Nokia Bluetooth (UART) HCI driver
- Some serdev & TTY changes that are dependencies for the Nokia
driver (with acks from relevant maintainers and an agreement that
these come through the bluetooth tree)
- Support for new Intel Bluetooth device
- Various other minor cleanups/fixes here and there

Please let me know if there are any issues pulling. Thanks.
====================

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

+5308 -265
+28
Documentation/devicetree/bindings/net/ieee802154/ca8210.txt
··· 1 + * CA8210 IEEE 802.15.4 * 2 + 3 + Required properties: 4 + - compatible: Should be "cascoda,ca8210" 5 + - reg: Controlling chip select 6 + - spi-max-frequency: Maximum clock speed, should be *less than* 7 + 4000000 8 + - spi-cpol: Requires inverted clock polarity 9 + - reset-gpio: GPIO attached to reset 10 + - irq-gpio: GPIO attached to IRQ 11 + Optional properties: 12 + - extclock-enable: Include for the ca8210 to route its 16MHz clock 13 + to an output 14 + - extclock-freq: Frequency in Hz of the external clock 15 + - extclock-gpio: GPIO of the ca8210 to output the clock on 16 + 17 + Example: 18 + ca8210@0 { 19 + compatible = "cascoda,ca8210"; 20 + reg = <0>; 21 + spi-max-frequency = <3000000>; 22 + spi-cpol; 23 + reset-gpio = <&gpio1 1 GPIO_ACTIVE_HIGH>; 24 + irq-gpio = <&gpio1 2 GPIO_ACTIVE_HIGH>; 25 + extclock-enable; 26 + extclock-freq = 16000000; 27 + extclock-gpio = 2; 28 + };
+51
Documentation/devicetree/bindings/net/nokia-bluetooth.txt
··· 1 + Nokia Bluetooth Chips 2 + --------------------- 3 + 4 + Nokia phones often come with UART connected bluetooth chips from different 5 + vendors and modified device API. Those devices speak a protocol named H4+ 6 + (also known as h4p) by Nokia, which is similar to the H4 protocol from the 7 + Bluetooth standard. In addition to the H4 protocol it specifies two more 8 + UART status lines for wakeup of UART transceivers to improve power management 9 + and a few new packet types used to negotiate uart speed. 10 + 11 + Required properties: 12 + 13 + - compatible: should contain "nokia,h4p-bluetooth" as well as one of the following: 14 + * "brcm,bcm2048-nokia" 15 + * "ti,wl1271-bluetooth-nokia" 16 + - reset-gpios: GPIO specifier, used to reset the BT module (active low) 17 + - bluetooth-wakeup-gpios: GPIO specifier, used to wakeup the BT module (active high) 18 + - host-wakeup-gpios: GPIO specifier, used to wakeup the host processor (active high) 19 + - clock-names: should be "sysclk" 20 + - clocks: should contain a clock specifier for every name in clock-names 21 + 22 + Optional properties: 23 + 24 + - None 25 + 26 + Example: 27 + 28 + / { 29 + /* controlled (enabled/disabled) directly by BT module */ 30 + bluetooth_clk: vctcxo { 31 + compatible = "fixed-clock"; 32 + #clock-cells = <0>; 33 + clock-frequency = <38400000>; 34 + }; 35 + }; 36 + 37 + &uart2 { 38 + pinctrl-names = "default"; 39 + pinctrl-0 = <&uart2_pins>; 40 + 41 + bluetooth { 42 + compatible = "ti,wl1271-bluetooth-nokia", "nokia,h4p-bluetooth"; 43 + 44 + reset-gpios = <&gpio1 26 GPIO_ACTIVE_LOW>; /* gpio26 */ 45 + host-wakeup-gpios = <&gpio4 5 GPIO_ACTIVE_HIGH>; /* gpio101 */ 46 + bluetooth-wakeup-gpios = <&gpio2 5 GPIO_ACTIVE_HIGH>; /* gpio37 */ 47 + 48 + clocks = <&bluetooth_clk>; 49 + clock-names = "sysclk"; 50 + }; 51 + };
+1
Documentation/devicetree/bindings/vendor-prefixes.txt
··· 51 51 buffalo Buffalo, Inc. 52 52 calxeda Calxeda 53 53 capella Capella Microsystems, Inc 54 + cascoda Cascoda, Ltd. 54 55 cavium Cavium, Inc. 55 56 cdns Cadence Design Systems Inc. 56 57 ceva Ceva, Inc.
+9
MAINTAINERS
··· 2944 2944 S: Maintained 2945 2945 F: arch/c6x/ 2946 2946 2947 + CA8210 IEEE-802.15.4 RADIO DRIVER 2948 + M: Harry Morris <h.morris@cascoda.com> 2949 + M: linuxdev@cascoda.com 2950 + L: linux-wpan@vger.kernel.org 2951 + W: https://github.com/Cascoda/ca8210-linux.git 2952 + S: Maintained 2953 + F: drivers/net/ieee802154/ca8210.c 2954 + F: Documentation/devicetree/bindings/net/ieee802154/ca8210.txt 2955 + 2947 2956 CACHEFILES: FS-CACHE BACKEND FOR CACHING ON MOUNTED FILESYSTEMS 2948 2957 M: David Howells <dhowells@redhat.com> 2949 2958 L: linux-cachefs@redhat.com (moderated for non-subscribers)
+5
arch/arm64/boot/dts/hisilicon/hi6220-hikey.dts
··· 98 98 assigned-clocks = <&sys_ctrl HI6220_UART1_SRC>; 99 99 assigned-clock-rates = <150000000>; 100 100 status = "ok"; 101 + 102 + bluetooth { 103 + compatible = "ti,wl1835-st"; 104 + enable-gpios = <&gpio1 7 GPIO_ACTIVE_HIGH>; 105 + }; 101 106 }; 102 107 103 108 uart2: uart@f7112000 {
+12
drivers/bluetooth/Kconfig
··· 86 86 87 87 Say Y here to compile support for HCI UART (H4) protocol. 88 88 89 + config BT_HCIUART_NOKIA 90 + tristate "UART Nokia H4+ protocol support" 91 + depends on BT_HCIUART 92 + depends on SERIAL_DEV_BUS 93 + depends on PM 94 + help 95 + Nokia H4+ is serial protocol for communication between Bluetooth 96 + device and host. This protocol is required for Bluetooth devices 97 + with UART interface in Nokia devices. 98 + 99 + Say Y here to compile support for Nokia's H4+ protocol. 100 + 89 101 config BT_HCIUART_BCSP 90 102 bool "BCSP protocol support" 91 103 depends on BT_HCIUART
+3
drivers/bluetooth/Makefile
··· 25 25 obj-$(CONFIG_BT_RTL) += btrtl.o 26 26 obj-$(CONFIG_BT_QCA) += btqca.o 27 27 28 + obj-$(CONFIG_BT_HCIUART_NOKIA) += hci_nokia.o 29 + 28 30 btmrvl-y := btmrvl_main.o 29 31 btmrvl-$(CONFIG_DEBUG_FS) += btmrvl_debugfs.o 30 32 31 33 hci_uart-y := hci_ldisc.o 34 + hci_uart-$(CONFIG_SERIAL_DEV_BUS) += hci_serdev.o 32 35 hci_uart-$(CONFIG_BT_HCIUART_H4) += hci_h4.o 33 36 hci_uart-$(CONFIG_BT_HCIUART_BCSP) += hci_bcsp.o 34 37 hci_uart-$(CONFIG_BT_HCIUART_LL) += hci_ll.o
+2 -3
drivers/bluetooth/bluecard_cs.c
··· 695 695 696 696 spin_lock_init(&(info->lock)); 697 697 698 - init_timer(&(info->timer)); 699 - info->timer.function = &bluecard_activity_led_timeout; 700 - info->timer.data = (u_long)info; 698 + setup_timer(&(info->timer), &bluecard_activity_led_timeout, 699 + (u_long)info); 701 700 702 701 skb_queue_head_init(&(info->txq)); 703 702
+21 -11
drivers/bluetooth/btmrvl_sdio.c
··· 20 20 21 21 #include <linux/firmware.h> 22 22 #include <linux/slab.h> 23 + #include <linux/suspend.h> 23 24 24 25 #include <linux/mmc/sdio_ids.h> 25 26 #include <linux/mmc/sdio_func.h> ··· 61 60 62 61 static irqreturn_t btmrvl_wake_irq_bt(int irq, void *priv) 63 62 { 64 - struct btmrvl_plt_wake_cfg *cfg = priv; 63 + struct btmrvl_sdio_card *card = priv; 64 + struct btmrvl_plt_wake_cfg *cfg = card->plt_wake_cfg; 65 65 66 - if (cfg->irq_bt >= 0) { 67 - pr_info("%s: wake by bt", __func__); 68 - cfg->wake_by_bt = true; 69 - disable_irq_nosync(irq); 70 - } 66 + pr_info("%s: wake by bt", __func__); 67 + cfg->wake_by_bt = true; 68 + disable_irq_nosync(irq); 69 + 70 + pm_wakeup_event(&card->func->dev, 0); 71 + pm_system_wakeup(); 71 72 72 73 return IRQ_HANDLED; 73 74 } ··· 104 101 } else { 105 102 ret = devm_request_irq(dev, cfg->irq_bt, 106 103 btmrvl_wake_irq_bt, 107 - 0, "bt_wake", cfg); 104 + 0, "bt_wake", card); 108 105 if (ret) { 109 106 dev_err(dev, 110 107 "Failed to request irq_bt %d (%d)\n", ··· 1577 1574 MODULE_SHUTDOWN_REQ); 1578 1575 btmrvl_sdio_disable_host_int(card); 1579 1576 } 1580 - BT_DBG("unregester dev"); 1577 + BT_DBG("unregister dev"); 1581 1578 card->priv->surprise_removed = true; 1582 1579 btmrvl_sdio_unregister_dev(card); 1583 1580 btmrvl_remove_card(card->priv); ··· 1628 1625 if (priv->adapter->hs_state != HS_ACTIVATED) { 1629 1626 if (btmrvl_enable_hs(priv)) { 1630 1627 BT_ERR("HS not activated, suspend failed!"); 1628 + /* Disable platform specific wakeup interrupt */ 1629 + if (card->plt_wake_cfg && 1630 + card->plt_wake_cfg->irq_bt >= 0) { 1631 + disable_irq_wake(card->plt_wake_cfg->irq_bt); 1632 + disable_irq(card->plt_wake_cfg->irq_bt); 1633 + } 1634 + 1631 1635 priv->adapter->is_suspending = false; 1632 1636 return -EBUSY; 1633 1637 } ··· 1647 1637 if (priv->adapter->hs_state == HS_ACTIVATED) { 1648 1638 BT_DBG("suspend with MMC_PM_KEEP_POWER"); 1649 1639 return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER); 1650 - } else { 1651 - BT_DBG("suspend without MMC_PM_KEEP_POWER"); 1652 - return 0; 1653 1640 } 1641 + 1642 + BT_DBG("suspend without MMC_PM_KEEP_POWER"); 1643 + return 0; 1654 1644 } 1655 1645 1656 1646 static int btmrvl_sdio_resume(struct device *dev)
+8 -5
drivers/bluetooth/btrtl.c
··· 275 275 276 276 BT_INFO("%s: rtl: loading %s", hdev->name, name); 277 277 ret = request_firmware(&fw, name, &hdev->dev); 278 - if (ret < 0) { 279 - BT_ERR("%s: Failed to load %s", hdev->name, name); 278 + if (ret < 0) 280 279 return ret; 281 - } 282 - 283 280 ret = fw->size; 284 281 *buff = kmemdup(fw->data, ret, GFP_KERNEL); 285 282 ··· 328 331 u8 *cfg_buff = NULL; 329 332 u8 *tbuff; 330 333 char *cfg_name = NULL; 334 + bool config_needed = false; 331 335 332 336 switch (lmp_subver) { 333 337 case RTL_ROM_LMP_8723B: ··· 342 344 break; 343 345 case RTL_ROM_LMP_8822B: 344 346 cfg_name = "rtl_bt/rtl8822b_config.bin"; 347 + config_needed = true; 345 348 break; 346 349 default: 347 350 BT_ERR("%s: rtl: no config according to lmp_subver %04x", ··· 352 353 353 354 if (cfg_name) { 354 355 cfg_sz = rtl_load_config(hdev, cfg_name, &cfg_buff); 355 - if (cfg_sz < 0) 356 + if (cfg_sz < 0) { 356 357 cfg_sz = 0; 358 + if (config_needed) 359 + BT_ERR("Necessary config file %s not found\n", 360 + cfg_name); 361 + } 357 362 } else 358 363 cfg_sz = 0; 359 364
+12 -3
drivers/bluetooth/btusb.c
··· 26 26 #include <linux/firmware.h> 27 27 #include <linux/of_device.h> 28 28 #include <linux/of_irq.h> 29 + #include <linux/suspend.h> 29 30 #include <asm/unaligned.h> 30 31 31 32 #include <net/bluetooth/bluetooth.h> ··· 263 262 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME }, 264 263 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME }, 265 264 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME }, 265 + { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME }, 266 266 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME }, 267 267 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME }, 268 268 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME }, ··· 330 328 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL }, 331 329 332 330 /* Intel Bluetooth devices */ 331 + { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_NEW }, 333 332 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR }, 334 333 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL }, 335 334 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL }, ··· 2027 2024 return -EINVAL; 2028 2025 } 2029 2026 2030 - /* At the moment the iBT 3.0 hardware variants 0x0b (LnP/SfP) 2031 - * and 0x0c (WsP) are supported by this firmware loading method. 2027 + /* Check for supported iBT hardware variants of this firmware 2028 + * loading method. 2032 2029 * 2033 2030 * This check has been put in place to ensure correct forward 2034 2031 * compatibility options when newer hardware variants come along. 2035 2032 */ 2036 - if (ver.hw_variant != 0x0b && ver.hw_variant != 0x0c) { 2033 + switch (ver.hw_variant) { 2034 + case 0x0b: /* SfP */ 2035 + case 0x0c: /* WsP */ 2036 + case 0x12: /* ThP */ 2037 + break; 2038 + default: 2037 2039 BT_ERR("%s: Unsupported Intel hardware variant (%u)", 2038 2040 hdev->name, ver.hw_variant); 2039 2041 return -EINVAL; ··· 2800 2792 struct btusb_data *data = priv; 2801 2793 2802 2794 pm_wakeup_event(&data->udev->dev, 0); 2795 + pm_system_wakeup(); 2803 2796 2804 2797 /* Disable only if not already disabled (keep it balanced) */ 2805 2798 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
+39 -20
drivers/bluetooth/hci_bcm.c
··· 146 146 static int bcm_gpio_set_power(struct bcm_device *dev, bool powered) 147 147 { 148 148 if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled) 149 - clk_enable(dev->clk); 149 + clk_prepare_enable(dev->clk); 150 150 151 151 gpiod_set_value(dev->shutdown, powered); 152 152 gpiod_set_value(dev->device_wakeup, powered); 153 153 154 154 if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled) 155 - clk_disable(dev->clk); 155 + clk_disable_unprepare(dev->clk); 156 156 157 157 dev->clk_enabled = powered; 158 158 ··· 287 287 288 288 hu->priv = bcm; 289 289 290 + if (!hu->tty->dev) 291 + goto out; 292 + 290 293 mutex_lock(&bcm_device_lock); 291 294 list_for_each(p, &bcm_device_list) { 292 295 struct bcm_device *dev = list_entry(p, struct bcm_device, list); ··· 310 307 } 311 308 312 309 mutex_unlock(&bcm_device_lock); 313 - 310 + out: 314 311 return 0; 315 312 } 316 313 ··· 700 697 /* Always tell the ACPI core to skip this resource */ 701 698 return 1; 702 699 } 700 + #endif /* CONFIG_ACPI */ 703 701 704 - static int bcm_acpi_probe(struct bcm_device *dev) 702 + static int bcm_platform_probe(struct bcm_device *dev) 705 703 { 706 704 struct platform_device *pdev = dev->pdev; 707 - LIST_HEAD(resources); 708 - const struct dmi_system_id *dmi_id; 709 - const struct acpi_gpio_mapping *gpio_mapping = acpi_bcm_int_last_gpios; 710 - const struct acpi_device_id *id; 711 - int ret; 712 705 713 706 dev->name = dev_name(&pdev->dev); 714 - 715 - /* Retrieve GPIO data */ 716 - id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev); 717 - if (id) 718 - gpio_mapping = (const struct acpi_gpio_mapping *) id->driver_data; 719 - 720 - ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(&pdev->dev), 721 - gpio_mapping); 722 - if (ret) 723 - return ret; 724 707 725 708 dev->clk = devm_clk_get(&pdev->dev, NULL); 726 709 ··· 744 755 return -EINVAL; 745 756 } 746 757 758 + return 0; 759 + } 760 + 761 + #ifdef CONFIG_ACPI 762 + static int bcm_acpi_probe(struct bcm_device *dev) 763 + { 764 + struct platform_device *pdev = dev->pdev; 765 + LIST_HEAD(resources); 766 + const struct dmi_system_id *dmi_id; 767 + const struct acpi_gpio_mapping *gpio_mapping = acpi_bcm_int_last_gpios; 768 + const struct acpi_device_id *id; 769 + int ret; 770 + 771 + /* Retrieve GPIO data */ 772 + id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev); 773 + if (id) 774 + gpio_mapping = (const struct acpi_gpio_mapping *) id->driver_data; 775 + 776 + ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(&pdev->dev), 777 + gpio_mapping); 778 + if (ret) 779 + return ret; 780 + 781 + ret = bcm_platform_probe(dev); 782 + if (ret) 783 + return ret; 784 + 747 785 /* Retrieve UART ACPI info */ 748 786 ret = acpi_dev_get_resources(ACPI_COMPANION(&dev->pdev->dev), 749 787 &resources, bcm_resource, dev); ··· 805 789 806 790 dev->pdev = pdev; 807 791 808 - ret = bcm_acpi_probe(dev); 792 + if (has_acpi_companion(&pdev->dev)) 793 + ret = bcm_acpi_probe(dev); 794 + else 795 + ret = bcm_platform_probe(dev); 809 796 if (ret) 810 797 return ret; 811 798
+17
drivers/bluetooth/hci_h4.c
··· 171 171 const unsigned char *buffer, int count, 172 172 const struct h4_recv_pkt *pkts, int pkts_count) 173 173 { 174 + struct hci_uart *hu = hci_get_drvdata(hdev); 175 + u8 alignment = hu->alignment; 176 + 174 177 while (count) { 175 178 int i, len; 179 + 180 + /* remove padding bytes from buffer */ 181 + for (; hu->padding && count > 0; hu->padding--) { 182 + count--; 183 + buffer++; 184 + } 185 + if (!count) 186 + break; 176 187 177 188 if (!skb) { 178 189 for (i = 0; i < pkts_count; i++) { ··· 264 253 } 265 254 266 255 if (!dlen) { 256 + hu->padding = (skb->len - 1) % alignment; 257 + hu->padding = (alignment - hu->padding) % alignment; 258 + 267 259 /* No more data, complete frame */ 268 260 (&pkts[i])->recv(hdev, skb); 269 261 skb = NULL; 270 262 } 271 263 } else { 264 + hu->padding = (skb->len - 1) % alignment; 265 + hu->padding = (alignment - hu->padding) % alignment; 266 + 272 267 /* Complete frame */ 273 268 (&pkts[i])->recv(hdev, skb); 274 269 skb = NULL;
+34 -13
drivers/bluetooth/hci_intel.c
··· 307 307 struct list_head *p; 308 308 int err = -ENODEV; 309 309 310 + if (!hu->tty->dev) 311 + return err; 312 + 310 313 mutex_lock(&intel_device_list_lock); 311 314 312 315 list_for_each(p, &intel_device_list) { ··· 381 378 struct list_head *p; 382 379 struct intel_data *intel = container_of(work, struct intel_data, 383 380 busy_work); 381 + 382 + if (!intel->hu->tty->dev) 383 + return; 384 384 385 385 /* Link is busy, delay the suspend */ 386 386 mutex_lock(&intel_device_list_lock); ··· 607 601 return -EINVAL; 608 602 } 609 603 610 - /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is 611 - * supported by this firmware loading method. This check has been 612 - * put in place to ensure correct forward compatibility options 613 - * when newer hardware variants come along. 614 - */ 615 - if (ver.hw_variant != 0x0b) { 604 + /* Check for supported iBT hardware variants of this firmware 605 + * loading method. 606 + * 607 + * This check has been put in place to ensure correct forward 608 + * compatibility options when newer hardware variants come along. 609 + */ 610 + switch (ver.hw_variant) { 611 + case 0x0b: /* LnP */ 612 + case 0x0c: /* WsP */ 613 + case 0x12: /* ThP */ 614 + break; 615 + default: 616 616 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)", 617 617 ver.hw_variant); 618 618 return -EINVAL; ··· 711 699 /* With this Intel bootloader only the hardware variant and device 712 700 * revision information are used to select the right firmware. 713 701 * 714 - * Currently this bootloader support is limited to hardware variant 715 - * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b). 702 + * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi. 703 + * 704 + * Currently the supported hardware variants are: 705 + * 11 (0x0b) for iBT 3.0 (LnP/SfP) 716 706 */ 717 - snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi", 718 - le16_to_cpu(params->dev_revid)); 707 + snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi", 708 + le16_to_cpu(ver.hw_variant), 709 + le16_to_cpu(params->dev_revid)); 719 710 720 711 err = request_firmware(&fw, fwname, &hdev->dev); 721 712 if (err < 0) { ··· 731 716 bt_dev_info(hdev, "Found device firmware: %s", fwname); 732 717 733 718 /* Save the DDC file name for later */ 734 - snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.ddc", 735 - le16_to_cpu(params->dev_revid)); 719 + snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc", 720 + le16_to_cpu(ver.hw_variant), 721 + le16_to_cpu(params->dev_revid)); 736 722 737 723 kfree_skb(skb); 738 724 ··· 905 889 list_for_each(p, &intel_device_list) { 906 890 struct intel_device *dev = list_entry(p, struct intel_device, 907 891 list); 892 + if (!hu->tty->dev) 893 + break; 908 894 if (hu->tty->dev->parent == dev->pdev->dev.parent) { 909 895 if (device_may_wakeup(&dev->pdev->dev)) { 910 896 set_bit(STATE_LPM_ENABLED, &intel->flags); ··· 1074 1056 1075 1057 BT_DBG("hu %p skb %p", hu, skb); 1076 1058 1059 + if (!hu->tty->dev) 1060 + goto out_enqueue; 1061 + 1077 1062 /* Be sure our controller is resumed and potential LPM transaction 1078 1063 * completed before enqueuing any packet. 1079 1064 */ ··· 1093 1072 } 1094 1073 } 1095 1074 mutex_unlock(&intel_device_list_lock); 1096 - 1075 + out_enqueue: 1097 1076 skb_queue_tail(&intel->txq, skb); 1098 1077 1099 1078 return 0;
+5 -19
drivers/bluetooth/hci_ldisc.c
··· 134 134 135 135 return 0; 136 136 } 137 + EXPORT_SYMBOL_GPL(hci_uart_tx_wakeup); 137 138 138 139 static void hci_uart_write_work(struct work_struct *work) 139 140 { ··· 319 318 hu->oper_speed = oper_speed; 320 319 } 321 320 322 - void hci_uart_init_tty(struct hci_uart *hu) 323 - { 324 - struct tty_struct *tty = hu->tty; 325 - struct ktermios ktermios; 326 - 327 - /* Bring the UART into a known 8 bits no parity hw fc state */ 328 - ktermios = tty->termios; 329 - ktermios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | 330 - INLCR | IGNCR | ICRNL | IXON); 331 - ktermios.c_oflag &= ~OPOST; 332 - ktermios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); 333 - ktermios.c_cflag &= ~(CSIZE | PARENB); 334 - ktermios.c_cflag |= CS8; 335 - ktermios.c_cflag |= CRTSCTS; 336 - 337 - /* tty_set_termios() return not checked as it is always 0 */ 338 - tty_set_termios(tty, &ktermios); 339 - } 340 - 341 321 void hci_uart_set_baudrate(struct hci_uart *hu, unsigned int speed) 342 322 { 343 323 struct tty_struct *tty = hu->tty; ··· 440 458 tty->disc_data = hu; 441 459 hu->tty = tty; 442 460 tty->receive_room = 65536; 461 + 462 + /* disable alignment support by default */ 463 + hu->alignment = 1; 464 + hu->padding = 0; 443 465 444 466 INIT_WORK(&hu->init_ready, hci_uart_init_work); 445 467 INIT_WORK(&hu->write_work, hci_uart_write_work);
+261 -1
drivers/bluetooth/hci_ll.c
··· 34 34 #include <linux/sched.h> 35 35 #include <linux/types.h> 36 36 #include <linux/fcntl.h> 37 + #include <linux/firmware.h> 37 38 #include <linux/interrupt.h> 38 39 #include <linux/ptrace.h> 39 40 #include <linux/poll.h> 40 41 41 42 #include <linux/slab.h> 42 - #include <linux/tty.h> 43 43 #include <linux/errno.h> 44 44 #include <linux/string.h> 45 45 #include <linux/signal.h> 46 46 #include <linux/ioctl.h> 47 + #include <linux/of.h> 48 + #include <linux/serdev.h> 47 49 #include <linux/skbuff.h> 50 + #include <linux/ti_wilink_st.h> 48 51 49 52 #include <net/bluetooth/bluetooth.h> 50 53 #include <net/bluetooth/hci_core.h> 54 + #include <linux/gpio/consumer.h> 51 55 52 56 #include "hci_uart.h" 53 57 ··· 79 75 struct hcill_cmd { 80 76 u8 cmd; 81 77 } __packed; 78 + 79 + struct ll_device { 80 + struct hci_uart hu; 81 + struct serdev_device *serdev; 82 + struct gpio_desc *enable_gpio; 83 + }; 82 84 83 85 struct ll_struct { 84 86 unsigned long rx_state; ··· 146 136 147 137 hu->priv = ll; 148 138 139 + if (hu->serdev) 140 + serdev_device_open(hu->serdev); 141 + 149 142 return 0; 150 143 } 151 144 ··· 176 163 skb_queue_purge(&ll->txq); 177 164 178 165 kfree_skb(ll->rx_skb); 166 + 167 + if (hu->serdev) { 168 + struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev); 169 + gpiod_set_value_cansleep(lldev->enable_gpio, 0); 170 + 171 + serdev_device_close(hu->serdev); 172 + } 179 173 180 174 hu->priv = NULL; 181 175 ··· 525 505 return skb_dequeue(&ll->txq); 526 506 } 527 507 508 + #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS) 509 + static int read_local_version(struct hci_dev *hdev) 510 + { 511 + int err = 0; 512 + unsigned short version = 0; 513 + struct sk_buff *skb; 514 + struct hci_rp_read_local_version *ver; 515 + 516 + skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, HCI_INIT_TIMEOUT); 517 + if (IS_ERR(skb)) { 518 + bt_dev_err(hdev, "Reading TI version information failed (%ld)", 519 + PTR_ERR(skb)); 520 + err = PTR_ERR(skb); 521 + goto out; 522 + } 523 + if (skb->len != sizeof(*ver)) { 524 + err = -EILSEQ; 525 + goto out; 526 + } 527 + 528 + ver = (struct hci_rp_read_local_version *)skb->data; 529 + if (le16_to_cpu(ver->manufacturer) != 13) { 530 + err = -ENODEV; 531 + goto out; 532 + } 533 + 534 + version = le16_to_cpu(ver->lmp_subver); 535 + 536 + out: 537 + if (err) bt_dev_err(hdev, "Failed to read TI version info: %d", err); 538 + kfree_skb(skb); 539 + return err ? err : version; 540 + } 541 + 542 + /** 543 + * download_firmware - 544 + * internal function which parses through the .bts firmware 545 + * script file intreprets SEND, DELAY actions only as of now 546 + */ 547 + static int download_firmware(struct ll_device *lldev) 548 + { 549 + unsigned short chip, min_ver, maj_ver; 550 + int version, err, len; 551 + unsigned char *ptr, *action_ptr; 552 + unsigned char bts_scr_name[40]; /* 40 char long bts scr name? */ 553 + const struct firmware *fw; 554 + struct sk_buff *skb; 555 + struct hci_command *cmd; 556 + 557 + version = read_local_version(lldev->hu.hdev); 558 + if (version < 0) 559 + return version; 560 + 561 + chip = (version & 0x7C00) >> 10; 562 + min_ver = (version & 0x007F); 563 + maj_ver = (version & 0x0380) >> 7; 564 + if (version & 0x8000) 565 + maj_ver |= 0x0008; 566 + 567 + snprintf(bts_scr_name, sizeof(bts_scr_name), 568 + "ti-connectivity/TIInit_%d.%d.%d.bts", 569 + chip, maj_ver, min_ver); 570 + 571 + err = request_firmware(&fw, bts_scr_name, &lldev->serdev->dev); 572 + if (err || !fw->data || !fw->size) { 573 + bt_dev_err(lldev->hu.hdev, "request_firmware failed(errno %d) for %s", 574 + err, bts_scr_name); 575 + return -EINVAL; 576 + } 577 + ptr = (void *)fw->data; 578 + len = fw->size; 579 + /* bts_header to remove out magic number and 580 + * version 581 + */ 582 + ptr += sizeof(struct bts_header); 583 + len -= sizeof(struct bts_header); 584 + 585 + while (len > 0 && ptr) { 586 + bt_dev_dbg(lldev->hu.hdev, " action size %d, type %d ", 587 + ((struct bts_action *)ptr)->size, 588 + ((struct bts_action *)ptr)->type); 589 + 590 + action_ptr = &(((struct bts_action *)ptr)->data[0]); 591 + 592 + switch (((struct bts_action *)ptr)->type) { 593 + case ACTION_SEND_COMMAND: /* action send */ 594 + bt_dev_dbg(lldev->hu.hdev, "S"); 595 + cmd = (struct hci_command *)action_ptr; 596 + if (cmd->opcode == 0xff36) { 597 + /* ignore remote change 598 + * baud rate HCI VS command */ 599 + bt_dev_warn(lldev->hu.hdev, "change remote baud rate command in firmware"); 600 + break; 601 + } 602 + if (cmd->prefix != 1) 603 + bt_dev_dbg(lldev->hu.hdev, "command type %d\n", cmd->prefix); 604 + 605 + skb = __hci_cmd_sync(lldev->hu.hdev, cmd->opcode, cmd->plen, &cmd->speed, HCI_INIT_TIMEOUT); 606 + if (IS_ERR(skb)) { 607 + bt_dev_err(lldev->hu.hdev, "send command failed\n"); 608 + goto out_rel_fw; 609 + } 610 + kfree_skb(skb); 611 + break; 612 + case ACTION_WAIT_EVENT: /* wait */ 613 + /* no need to wait as command was synchronous */ 614 + bt_dev_dbg(lldev->hu.hdev, "W"); 615 + break; 616 + case ACTION_DELAY: /* sleep */ 617 + bt_dev_info(lldev->hu.hdev, "sleep command in scr"); 618 + mdelay(((struct bts_action_delay *)action_ptr)->msec); 619 + break; 620 + } 621 + len -= (sizeof(struct bts_action) + 622 + ((struct bts_action *)ptr)->size); 623 + ptr += sizeof(struct bts_action) + 624 + ((struct bts_action *)ptr)->size; 625 + } 626 + 627 + out_rel_fw: 628 + /* fw download complete */ 629 + release_firmware(fw); 630 + return err; 631 + } 632 + 633 + static int ll_setup(struct hci_uart *hu) 634 + { 635 + int err, retry = 3; 636 + struct ll_device *lldev; 637 + struct serdev_device *serdev = hu->serdev; 638 + u32 speed; 639 + 640 + if (!serdev) 641 + return 0; 642 + 643 + lldev = serdev_device_get_drvdata(serdev); 644 + 645 + serdev_device_set_flow_control(serdev, true); 646 + 647 + do { 648 + /* Configure BT_EN to HIGH state */ 649 + gpiod_set_value_cansleep(lldev->enable_gpio, 0); 650 + msleep(5); 651 + gpiod_set_value_cansleep(lldev->enable_gpio, 1); 652 + msleep(100); 653 + 654 + err = download_firmware(lldev); 655 + if (!err) 656 + break; 657 + 658 + /* Toggle BT_EN and retry */ 659 + bt_dev_err(hu->hdev, "download firmware failed, retrying..."); 660 + } while (retry--); 661 + 662 + if (err) 663 + return err; 664 + 665 + /* Operational speed if any */ 666 + if (hu->oper_speed) 667 + speed = hu->oper_speed; 668 + else if (hu->proto->oper_speed) 669 + speed = hu->proto->oper_speed; 670 + else 671 + speed = 0; 672 + 673 + if (speed) { 674 + struct sk_buff *skb = __hci_cmd_sync(hu->hdev, 0xff36, sizeof(speed), &speed, HCI_INIT_TIMEOUT); 675 + if (!IS_ERR(skb)) { 676 + kfree_skb(skb); 677 + serdev_device_set_baudrate(serdev, speed); 678 + } 679 + } 680 + 681 + return 0; 682 + } 683 + 684 + static const struct hci_uart_proto llp; 685 + 686 + static int hci_ti_probe(struct serdev_device *serdev) 687 + { 688 + struct hci_uart *hu; 689 + struct ll_device *lldev; 690 + u32 max_speed = 3000000; 691 + 692 + lldev = devm_kzalloc(&serdev->dev, sizeof(struct ll_device), GFP_KERNEL); 693 + if (!lldev) 694 + return -ENOMEM; 695 + hu = &lldev->hu; 696 + 697 + serdev_device_set_drvdata(serdev, lldev); 698 + lldev->serdev = hu->serdev = serdev; 699 + 700 + lldev->enable_gpio = devm_gpiod_get_optional(&serdev->dev, "enable", GPIOD_OUT_LOW); 701 + if (IS_ERR(lldev->enable_gpio)) 702 + return PTR_ERR(lldev->enable_gpio); 703 + 704 + of_property_read_u32(serdev->dev.of_node, "max-speed", &max_speed); 705 + hci_uart_set_speeds(hu, 115200, max_speed); 706 + 707 + return hci_uart_register_device(hu, &llp); 708 + } 709 + 710 + static void hci_ti_remove(struct serdev_device *serdev) 711 + { 712 + struct ll_device *lldev = serdev_device_get_drvdata(serdev); 713 + struct hci_uart *hu = &lldev->hu; 714 + struct hci_dev *hdev = hu->hdev; 715 + 716 + cancel_work_sync(&hu->write_work); 717 + 718 + hci_unregister_dev(hdev); 719 + hci_free_dev(hdev); 720 + hu->proto->close(hu); 721 + } 722 + 723 + static const struct of_device_id hci_ti_of_match[] = { 724 + { .compatible = "ti,wl1831-st" }, 725 + { .compatible = "ti,wl1835-st" }, 726 + { .compatible = "ti,wl1837-st" }, 727 + {}, 728 + }; 729 + MODULE_DEVICE_TABLE(of, hci_ti_of_match); 730 + 731 + static struct serdev_device_driver hci_ti_drv = { 732 + .driver = { 733 + .name = "hci-ti", 734 + .of_match_table = of_match_ptr(hci_ti_of_match), 735 + }, 736 + .probe = hci_ti_probe, 737 + .remove = hci_ti_remove, 738 + }; 739 + #else 740 + #define ll_setup NULL 741 + #endif 742 + 528 743 static const struct hci_uart_proto llp = { 529 744 .id = HCI_UART_LL, 530 745 .name = "LL", 746 + .setup = ll_setup, 531 747 .open = ll_open, 532 748 .close = ll_close, 533 749 .recv = ll_recv, ··· 774 518 775 519 int __init ll_init(void) 776 520 { 521 + serdev_device_driver_register(&hci_ti_drv); 522 + 777 523 return hci_uart_register_proto(&llp); 778 524 } 779 525 780 526 int __exit ll_deinit(void) 781 527 { 528 + serdev_device_driver_unregister(&hci_ti_drv); 529 + 782 530 return hci_uart_unregister_proto(&llp); 783 531 }
+820
drivers/bluetooth/hci_nokia.c
··· 1 + /* 2 + * Bluetooth HCI UART H4 driver with Nokia Extensions AKA Nokia H4+ 3 + * 4 + * Copyright (C) 2015 Marcel Holtmann <marcel@holtmann.org> 5 + * Copyright (C) 2015-2017 Sebastian Reichel <sre@kernel.org> 6 + * 7 + * This program is free software; you can redistribute it and/or modify 8 + * it under the terms of the GNU General Public License as published by 9 + * the Free Software Foundation; either version 2 of the License, or 10 + * (at your option) any later version. 11 + * 12 + * This program is distributed in the hope that it will be useful, 13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 + * GNU General Public License for more details. 16 + */ 17 + 18 + #include <linux/clk.h> 19 + #include <linux/errno.h> 20 + #include <linux/firmware.h> 21 + #include <linux/gpio/consumer.h> 22 + #include <linux/interrupt.h> 23 + #include <linux/kernel.h> 24 + #include <linux/module.h> 25 + #include <linux/of.h> 26 + #include <linux/pm_runtime.h> 27 + #include <linux/serdev.h> 28 + #include <linux/skbuff.h> 29 + #include <linux/slab.h> 30 + #include <linux/string.h> 31 + #include <linux/types.h> 32 + #include <linux/unaligned/le_struct.h> 33 + #include <net/bluetooth/bluetooth.h> 34 + #include <net/bluetooth/hci_core.h> 35 + 36 + #include "hci_uart.h" 37 + #include "btbcm.h" 38 + 39 + #define NOKIA_ID_BCM2048 0x04 40 + #define NOKIA_ID_TI1271 0x31 41 + 42 + #define FIRMWARE_BCM2048 "nokia/bcmfw.bin" 43 + #define FIRMWARE_TI1271 "nokia/ti1273.bin" 44 + 45 + #define HCI_NOKIA_NEG_PKT 0x06 46 + #define HCI_NOKIA_ALIVE_PKT 0x07 47 + #define HCI_NOKIA_RADIO_PKT 0x08 48 + 49 + #define HCI_NOKIA_NEG_HDR_SIZE 1 50 + #define HCI_NOKIA_MAX_NEG_SIZE 255 51 + #define HCI_NOKIA_ALIVE_HDR_SIZE 1 52 + #define HCI_NOKIA_MAX_ALIVE_SIZE 255 53 + #define HCI_NOKIA_RADIO_HDR_SIZE 2 54 + #define HCI_NOKIA_MAX_RADIO_SIZE 255 55 + 56 + #define NOKIA_PROTO_PKT 0x44 57 + #define NOKIA_PROTO_BYTE 0x4c 58 + 59 + #define NOKIA_NEG_REQ 0x00 60 + #define NOKIA_NEG_ACK 0x20 61 + #define NOKIA_NEG_NAK 0x40 62 + 63 + #define H4_TYPE_SIZE 1 64 + 65 + #define NOKIA_RECV_ALIVE \ 66 + .type = HCI_NOKIA_ALIVE_PKT, \ 67 + .hlen = HCI_NOKIA_ALIVE_HDR_SIZE, \ 68 + .loff = 0, \ 69 + .lsize = 1, \ 70 + .maxlen = HCI_NOKIA_MAX_ALIVE_SIZE \ 71 + 72 + #define NOKIA_RECV_NEG \ 73 + .type = HCI_NOKIA_NEG_PKT, \ 74 + .hlen = HCI_NOKIA_NEG_HDR_SIZE, \ 75 + .loff = 0, \ 76 + .lsize = 1, \ 77 + .maxlen = HCI_NOKIA_MAX_NEG_SIZE \ 78 + 79 + #define NOKIA_RECV_RADIO \ 80 + .type = HCI_NOKIA_RADIO_PKT, \ 81 + .hlen = HCI_NOKIA_RADIO_HDR_SIZE, \ 82 + .loff = 1, \ 83 + .lsize = 1, \ 84 + .maxlen = HCI_NOKIA_MAX_RADIO_SIZE \ 85 + 86 + struct hci_nokia_neg_hdr { 87 + u8 dlen; 88 + } __packed; 89 + 90 + struct hci_nokia_neg_cmd { 91 + u8 ack; 92 + u16 baud; 93 + u16 unused1; 94 + u8 proto; 95 + u16 sys_clk; 96 + u16 unused2; 97 + } __packed; 98 + 99 + #define NOKIA_ALIVE_REQ 0x55 100 + #define NOKIA_ALIVE_RESP 0xcc 101 + 102 + struct hci_nokia_alive_hdr { 103 + u8 dlen; 104 + } __packed; 105 + 106 + struct hci_nokia_alive_pkt { 107 + u8 mid; 108 + u8 unused; 109 + } __packed; 110 + 111 + struct hci_nokia_neg_evt { 112 + u8 ack; 113 + u16 baud; 114 + u16 unused1; 115 + u8 proto; 116 + u16 sys_clk; 117 + u16 unused2; 118 + u8 man_id; 119 + u8 ver_id; 120 + } __packed; 121 + 122 + #define MAX_BAUD_RATE 3692300 123 + #define SETUP_BAUD_RATE 921600 124 + #define INIT_BAUD_RATE 120000 125 + 126 + struct hci_nokia_radio_hdr { 127 + u8 evt; 128 + u8 dlen; 129 + } __packed; 130 + 131 + struct nokia_bt_dev { 132 + struct hci_uart hu; 133 + struct serdev_device *serdev; 134 + 135 + struct gpio_desc *reset; 136 + struct gpio_desc *wakeup_host; 137 + struct gpio_desc *wakeup_bt; 138 + unsigned long sysclk_speed; 139 + 140 + int wake_irq; 141 + struct sk_buff *rx_skb; 142 + struct sk_buff_head txq; 143 + bdaddr_t bdaddr; 144 + 145 + int init_error; 146 + struct completion init_completion; 147 + 148 + u8 man_id; 149 + u8 ver_id; 150 + 151 + bool initialized; 152 + bool tx_enabled; 153 + bool rx_enabled; 154 + }; 155 + 156 + static int nokia_enqueue(struct hci_uart *hu, struct sk_buff *skb); 157 + 158 + static void nokia_flow_control(struct serdev_device *serdev, bool enable) 159 + { 160 + if (enable) { 161 + serdev_device_set_rts(serdev, true); 162 + serdev_device_set_flow_control(serdev, true); 163 + } else { 164 + serdev_device_set_flow_control(serdev, false); 165 + serdev_device_set_rts(serdev, false); 166 + } 167 + } 168 + 169 + static irqreturn_t wakeup_handler(int irq, void *data) 170 + { 171 + struct nokia_bt_dev *btdev = data; 172 + struct device *dev = &btdev->serdev->dev; 173 + int wake_state = gpiod_get_value(btdev->wakeup_host); 174 + 175 + if (btdev->rx_enabled == wake_state) 176 + return IRQ_HANDLED; 177 + 178 + if (wake_state) 179 + pm_runtime_get(dev); 180 + else 181 + pm_runtime_put(dev); 182 + 183 + btdev->rx_enabled = wake_state; 184 + 185 + return IRQ_HANDLED; 186 + } 187 + 188 + static int nokia_reset(struct hci_uart *hu) 189 + { 190 + struct nokia_bt_dev *btdev = hu->priv; 191 + struct device *dev = &btdev->serdev->dev; 192 + int err; 193 + 194 + /* reset routine */ 195 + gpiod_set_value_cansleep(btdev->reset, 1); 196 + gpiod_set_value_cansleep(btdev->wakeup_bt, 1); 197 + 198 + msleep(100); 199 + 200 + /* safety check */ 201 + err = gpiod_get_value_cansleep(btdev->wakeup_host); 202 + if (err == 1) { 203 + dev_err(dev, "reset: host wakeup not low!"); 204 + return -EPROTO; 205 + } 206 + 207 + /* flush queue */ 208 + serdev_device_write_flush(btdev->serdev); 209 + 210 + /* init uart */ 211 + nokia_flow_control(btdev->serdev, false); 212 + serdev_device_set_baudrate(btdev->serdev, INIT_BAUD_RATE); 213 + 214 + gpiod_set_value_cansleep(btdev->reset, 0); 215 + 216 + /* wait for cts */ 217 + err = serdev_device_wait_for_cts(btdev->serdev, true, 200); 218 + if (err < 0) { 219 + dev_err(dev, "CTS not received: %d", err); 220 + return err; 221 + } 222 + 223 + nokia_flow_control(btdev->serdev, true); 224 + 225 + return 0; 226 + } 227 + 228 + static int nokia_send_alive_packet(struct hci_uart *hu) 229 + { 230 + struct nokia_bt_dev *btdev = hu->priv; 231 + struct device *dev = &btdev->serdev->dev; 232 + struct hci_nokia_alive_hdr *hdr; 233 + struct hci_nokia_alive_pkt *pkt; 234 + struct sk_buff *skb; 235 + int len; 236 + 237 + init_completion(&btdev->init_completion); 238 + 239 + len = H4_TYPE_SIZE + sizeof(*hdr) + sizeof(*pkt); 240 + skb = bt_skb_alloc(len, GFP_KERNEL); 241 + if (!skb) 242 + return -ENOMEM; 243 + 244 + hci_skb_pkt_type(skb) = HCI_NOKIA_ALIVE_PKT; 245 + memset(skb->data, 0x00, len); 246 + 247 + hdr = (struct hci_nokia_alive_hdr *)skb_put(skb, sizeof(*hdr)); 248 + hdr->dlen = sizeof(*pkt); 249 + pkt = (struct hci_nokia_alive_pkt *)skb_put(skb, sizeof(*pkt)); 250 + pkt->mid = NOKIA_ALIVE_REQ; 251 + 252 + nokia_enqueue(hu, skb); 253 + hci_uart_tx_wakeup(hu); 254 + 255 + dev_dbg(dev, "Alive sent"); 256 + 257 + if (!wait_for_completion_interruptible_timeout(&btdev->init_completion, 258 + msecs_to_jiffies(1000))) { 259 + return -ETIMEDOUT; 260 + } 261 + 262 + if (btdev->init_error < 0) 263 + return btdev->init_error; 264 + 265 + return 0; 266 + } 267 + 268 + static int nokia_send_negotiation(struct hci_uart *hu) 269 + { 270 + struct nokia_bt_dev *btdev = hu->priv; 271 + struct device *dev = &btdev->serdev->dev; 272 + struct hci_nokia_neg_cmd *neg_cmd; 273 + struct hci_nokia_neg_hdr *neg_hdr; 274 + struct sk_buff *skb; 275 + int len, err; 276 + u16 baud = DIV_ROUND_CLOSEST(btdev->sysclk_speed * 10, SETUP_BAUD_RATE); 277 + int sysclk = btdev->sysclk_speed / 1000; 278 + 279 + len = H4_TYPE_SIZE + sizeof(*neg_hdr) + sizeof(*neg_cmd); 280 + skb = bt_skb_alloc(len, GFP_KERNEL); 281 + if (!skb) 282 + return -ENOMEM; 283 + 284 + hci_skb_pkt_type(skb) = HCI_NOKIA_NEG_PKT; 285 + 286 + neg_hdr = (struct hci_nokia_neg_hdr *)skb_put(skb, sizeof(*neg_hdr)); 287 + neg_hdr->dlen = sizeof(*neg_cmd); 288 + 289 + neg_cmd = (struct hci_nokia_neg_cmd *)skb_put(skb, sizeof(*neg_cmd)); 290 + neg_cmd->ack = NOKIA_NEG_REQ; 291 + neg_cmd->baud = cpu_to_le16(baud); 292 + neg_cmd->unused1 = 0x0000; 293 + neg_cmd->proto = NOKIA_PROTO_BYTE; 294 + neg_cmd->sys_clk = cpu_to_le16(sysclk); 295 + neg_cmd->unused2 = 0x0000; 296 + 297 + btdev->init_error = 0; 298 + init_completion(&btdev->init_completion); 299 + 300 + nokia_enqueue(hu, skb); 301 + hci_uart_tx_wakeup(hu); 302 + 303 + dev_dbg(dev, "Negotiation sent"); 304 + 305 + if (!wait_for_completion_interruptible_timeout(&btdev->init_completion, 306 + msecs_to_jiffies(10000))) { 307 + return -ETIMEDOUT; 308 + } 309 + 310 + if (btdev->init_error < 0) 311 + return btdev->init_error; 312 + 313 + /* Change to previously negotiated speed. Flow Control 314 + * is disabled until bluetooth adapter is ready to avoid 315 + * broken bytes being received. 316 + */ 317 + nokia_flow_control(btdev->serdev, false); 318 + serdev_device_set_baudrate(btdev->serdev, SETUP_BAUD_RATE); 319 + err = serdev_device_wait_for_cts(btdev->serdev, true, 200); 320 + if (err < 0) { 321 + dev_err(dev, "CTS not received: %d", err); 322 + return err; 323 + } 324 + nokia_flow_control(btdev->serdev, true); 325 + 326 + dev_dbg(dev, "Negotiation successful"); 327 + 328 + return 0; 329 + } 330 + 331 + static int nokia_setup_fw(struct hci_uart *hu) 332 + { 333 + struct nokia_bt_dev *btdev = hu->priv; 334 + struct device *dev = &btdev->serdev->dev; 335 + const char *fwname; 336 + const struct firmware *fw; 337 + const u8 *fw_ptr; 338 + size_t fw_size; 339 + int err; 340 + 341 + dev_dbg(dev, "setup firmware"); 342 + 343 + if (btdev->man_id == NOKIA_ID_BCM2048) { 344 + fwname = FIRMWARE_BCM2048; 345 + } else if (btdev->man_id == NOKIA_ID_TI1271) { 346 + fwname = FIRMWARE_TI1271; 347 + } else { 348 + dev_err(dev, "Unsupported bluetooth device!"); 349 + return -ENODEV; 350 + } 351 + 352 + err = request_firmware(&fw, fwname, dev); 353 + if (err < 0) { 354 + dev_err(dev, "%s: Failed to load Nokia firmware file (%d)", 355 + hu->hdev->name, err); 356 + return err; 357 + } 358 + 359 + fw_ptr = fw->data; 360 + fw_size = fw->size; 361 + 362 + while (fw_size >= 4) { 363 + u16 pkt_size = get_unaligned_le16(fw_ptr); 364 + u8 pkt_type = fw_ptr[2]; 365 + const struct hci_command_hdr *cmd; 366 + u16 opcode; 367 + struct sk_buff *skb; 368 + 369 + switch (pkt_type) { 370 + case HCI_COMMAND_PKT: 371 + cmd = (struct hci_command_hdr *)(fw_ptr + 3); 372 + opcode = le16_to_cpu(cmd->opcode); 373 + 374 + skb = __hci_cmd_sync(hu->hdev, opcode, cmd->plen, 375 + fw_ptr + 3 + HCI_COMMAND_HDR_SIZE, 376 + HCI_INIT_TIMEOUT); 377 + if (IS_ERR(skb)) { 378 + err = PTR_ERR(skb); 379 + dev_err(dev, "%s: FW command %04x failed (%d)", 380 + hu->hdev->name, opcode, err); 381 + goto done; 382 + } 383 + kfree_skb(skb); 384 + break; 385 + case HCI_NOKIA_RADIO_PKT: 386 + case HCI_NOKIA_NEG_PKT: 387 + case HCI_NOKIA_ALIVE_PKT: 388 + break; 389 + } 390 + 391 + fw_ptr += pkt_size + 2; 392 + fw_size -= pkt_size + 2; 393 + } 394 + 395 + done: 396 + release_firmware(fw); 397 + return err; 398 + } 399 + 400 + static int nokia_setup(struct hci_uart *hu) 401 + { 402 + struct nokia_bt_dev *btdev = hu->priv; 403 + struct device *dev = &btdev->serdev->dev; 404 + int err; 405 + 406 + btdev->initialized = false; 407 + 408 + nokia_flow_control(btdev->serdev, false); 409 + 410 + pm_runtime_get_sync(dev); 411 + 412 + if (btdev->tx_enabled) { 413 + gpiod_set_value_cansleep(btdev->wakeup_bt, 0); 414 + pm_runtime_put(&btdev->serdev->dev); 415 + btdev->tx_enabled = false; 416 + } 417 + 418 + dev_dbg(dev, "protocol setup"); 419 + 420 + /* 0. reset connection */ 421 + err = nokia_reset(hu); 422 + if (err < 0) { 423 + dev_err(dev, "Reset failed: %d", err); 424 + goto out; 425 + } 426 + 427 + /* 1. negotiate speed etc */ 428 + err = nokia_send_negotiation(hu); 429 + if (err < 0) { 430 + dev_err(dev, "Negotiation failed: %d", err); 431 + goto out; 432 + } 433 + 434 + /* 2. verify correct setup using alive packet */ 435 + err = nokia_send_alive_packet(hu); 436 + if (err < 0) { 437 + dev_err(dev, "Alive check failed: %d", err); 438 + goto out; 439 + } 440 + 441 + /* 3. send firmware */ 442 + err = nokia_setup_fw(hu); 443 + if (err < 0) { 444 + dev_err(dev, "Could not setup FW: %d", err); 445 + goto out; 446 + } 447 + 448 + nokia_flow_control(btdev->serdev, false); 449 + serdev_device_set_baudrate(btdev->serdev, MAX_BAUD_RATE); 450 + nokia_flow_control(btdev->serdev, true); 451 + 452 + if (btdev->man_id == NOKIA_ID_BCM2048) { 453 + hu->hdev->set_bdaddr = btbcm_set_bdaddr; 454 + set_bit(HCI_QUIRK_INVALID_BDADDR, &hu->hdev->quirks); 455 + dev_dbg(dev, "bcm2048 has invalid bluetooth address!"); 456 + } 457 + 458 + dev_dbg(dev, "protocol setup done!"); 459 + 460 + gpiod_set_value_cansleep(btdev->wakeup_bt, 0); 461 + pm_runtime_put(dev); 462 + btdev->tx_enabled = false; 463 + btdev->initialized = true; 464 + 465 + return 0; 466 + out: 467 + pm_runtime_put(dev); 468 + 469 + return err; 470 + } 471 + 472 + static int nokia_open(struct hci_uart *hu) 473 + { 474 + struct device *dev = &hu->serdev->dev; 475 + 476 + dev_dbg(dev, "protocol open"); 477 + 478 + serdev_device_open(hu->serdev); 479 + 480 + pm_runtime_enable(dev); 481 + 482 + return 0; 483 + } 484 + 485 + static int nokia_flush(struct hci_uart *hu) 486 + { 487 + struct nokia_bt_dev *btdev = hu->priv; 488 + 489 + dev_dbg(&btdev->serdev->dev, "flush device"); 490 + 491 + skb_queue_purge(&btdev->txq); 492 + 493 + return 0; 494 + } 495 + 496 + static int nokia_close(struct hci_uart *hu) 497 + { 498 + struct nokia_bt_dev *btdev = hu->priv; 499 + struct device *dev = &btdev->serdev->dev; 500 + 501 + dev_dbg(dev, "close device"); 502 + 503 + btdev->initialized = false; 504 + 505 + skb_queue_purge(&btdev->txq); 506 + 507 + kfree_skb(btdev->rx_skb); 508 + 509 + /* disable module */ 510 + gpiod_set_value(btdev->reset, 1); 511 + gpiod_set_value(btdev->wakeup_bt, 0); 512 + 513 + pm_runtime_disable(&btdev->serdev->dev); 514 + serdev_device_close(btdev->serdev); 515 + 516 + return 0; 517 + } 518 + 519 + /* Enqueue frame for transmittion (padding, crc, etc) */ 520 + static int nokia_enqueue(struct hci_uart *hu, struct sk_buff *skb) 521 + { 522 + struct nokia_bt_dev *btdev = hu->priv; 523 + int err; 524 + 525 + /* Prepend skb with frame type */ 526 + memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1); 527 + 528 + /* Packets must be word aligned */ 529 + if (skb->len % 2) { 530 + err = skb_pad(skb, 1); 531 + if (err) 532 + return err; 533 + *skb_put(skb, 1) = 0x00; 534 + } 535 + 536 + skb_queue_tail(&btdev->txq, skb); 537 + 538 + return 0; 539 + } 540 + 541 + static int nokia_recv_negotiation_packet(struct hci_dev *hdev, 542 + struct sk_buff *skb) 543 + { 544 + struct hci_uart *hu = hci_get_drvdata(hdev); 545 + struct nokia_bt_dev *btdev = hu->priv; 546 + struct device *dev = &btdev->serdev->dev; 547 + struct hci_nokia_neg_hdr *hdr; 548 + struct hci_nokia_neg_evt *evt; 549 + int ret = 0; 550 + 551 + hdr = (struct hci_nokia_neg_hdr *)skb->data; 552 + if (hdr->dlen != sizeof(*evt)) { 553 + btdev->init_error = -EIO; 554 + ret = -EIO; 555 + goto finish_neg; 556 + } 557 + 558 + evt = (struct hci_nokia_neg_evt *)skb_pull(skb, sizeof(*hdr)); 559 + 560 + if (evt->ack != NOKIA_NEG_ACK) { 561 + dev_err(dev, "Negotiation received: wrong reply"); 562 + btdev->init_error = -EINVAL; 563 + ret = -EINVAL; 564 + goto finish_neg; 565 + } 566 + 567 + btdev->man_id = evt->man_id; 568 + btdev->ver_id = evt->ver_id; 569 + 570 + dev_dbg(dev, "Negotiation received: baud=%u:clk=%u:manu=%u:vers=%u", 571 + evt->baud, evt->sys_clk, evt->man_id, evt->ver_id); 572 + 573 + finish_neg: 574 + complete(&btdev->init_completion); 575 + kfree_skb(skb); 576 + return ret; 577 + } 578 + 579 + static int nokia_recv_alive_packet(struct hci_dev *hdev, struct sk_buff *skb) 580 + { 581 + struct hci_uart *hu = hci_get_drvdata(hdev); 582 + struct nokia_bt_dev *btdev = hu->priv; 583 + struct device *dev = &btdev->serdev->dev; 584 + struct hci_nokia_alive_hdr *hdr; 585 + struct hci_nokia_alive_pkt *pkt; 586 + int ret = 0; 587 + 588 + hdr = (struct hci_nokia_alive_hdr *)skb->data; 589 + if (hdr->dlen != sizeof(*pkt)) { 590 + dev_err(dev, "Corrupted alive message"); 591 + btdev->init_error = -EIO; 592 + ret = -EIO; 593 + goto finish_alive; 594 + } 595 + 596 + pkt = (struct hci_nokia_alive_pkt *)skb_pull(skb, sizeof(*hdr)); 597 + 598 + if (pkt->mid != NOKIA_ALIVE_RESP) { 599 + dev_err(dev, "Alive received: invalid response: 0x%02x!", 600 + pkt->mid); 601 + btdev->init_error = -EINVAL; 602 + ret = -EINVAL; 603 + goto finish_alive; 604 + } 605 + 606 + dev_dbg(dev, "Alive received"); 607 + 608 + finish_alive: 609 + complete(&btdev->init_completion); 610 + kfree_skb(skb); 611 + return ret; 612 + } 613 + 614 + static int nokia_recv_radio(struct hci_dev *hdev, struct sk_buff *skb) 615 + { 616 + /* Packets received on the dedicated radio channel are 617 + * HCI events and so feed them back into the core. 618 + */ 619 + hci_skb_pkt_type(skb) = HCI_EVENT_PKT; 620 + return hci_recv_frame(hdev, skb); 621 + } 622 + 623 + /* Recv data */ 624 + static const struct h4_recv_pkt nokia_recv_pkts[] = { 625 + { H4_RECV_ACL, .recv = hci_recv_frame }, 626 + { H4_RECV_SCO, .recv = hci_recv_frame }, 627 + { H4_RECV_EVENT, .recv = hci_recv_frame }, 628 + { NOKIA_RECV_ALIVE, .recv = nokia_recv_alive_packet }, 629 + { NOKIA_RECV_NEG, .recv = nokia_recv_negotiation_packet }, 630 + { NOKIA_RECV_RADIO, .recv = nokia_recv_radio }, 631 + }; 632 + 633 + static int nokia_recv(struct hci_uart *hu, const void *data, int count) 634 + { 635 + struct nokia_bt_dev *btdev = hu->priv; 636 + struct device *dev = &btdev->serdev->dev; 637 + int err; 638 + 639 + if (!test_bit(HCI_UART_REGISTERED, &hu->flags)) 640 + return -EUNATCH; 641 + 642 + btdev->rx_skb = h4_recv_buf(hu->hdev, btdev->rx_skb, data, count, 643 + nokia_recv_pkts, ARRAY_SIZE(nokia_recv_pkts)); 644 + if (IS_ERR(btdev->rx_skb)) { 645 + err = PTR_ERR(btdev->rx_skb); 646 + dev_err(dev, "Frame reassembly failed (%d)", err); 647 + btdev->rx_skb = NULL; 648 + return err; 649 + } 650 + 651 + return count; 652 + } 653 + 654 + static struct sk_buff *nokia_dequeue(struct hci_uart *hu) 655 + { 656 + struct nokia_bt_dev *btdev = hu->priv; 657 + struct device *dev = &btdev->serdev->dev; 658 + struct sk_buff *result = skb_dequeue(&btdev->txq); 659 + 660 + if (!btdev->initialized) 661 + return result; 662 + 663 + if (btdev->tx_enabled == !!result) 664 + return result; 665 + 666 + if (result) { 667 + pm_runtime_get_sync(dev); 668 + gpiod_set_value_cansleep(btdev->wakeup_bt, 1); 669 + } else { 670 + serdev_device_wait_until_sent(btdev->serdev, 0); 671 + gpiod_set_value_cansleep(btdev->wakeup_bt, 0); 672 + pm_runtime_put(dev); 673 + } 674 + 675 + btdev->tx_enabled = !!result; 676 + 677 + return result; 678 + } 679 + 680 + static const struct hci_uart_proto nokia_proto = { 681 + .id = HCI_UART_NOKIA, 682 + .name = "Nokia", 683 + .open = nokia_open, 684 + .close = nokia_close, 685 + .recv = nokia_recv, 686 + .enqueue = nokia_enqueue, 687 + .dequeue = nokia_dequeue, 688 + .flush = nokia_flush, 689 + .setup = nokia_setup, 690 + .manufacturer = 1, 691 + }; 692 + 693 + static int nokia_bluetooth_serdev_probe(struct serdev_device *serdev) 694 + { 695 + struct device *dev = &serdev->dev; 696 + struct nokia_bt_dev *btdev; 697 + struct clk *sysclk; 698 + int err = 0; 699 + 700 + btdev = devm_kzalloc(dev, sizeof(*btdev), GFP_KERNEL); 701 + if (!btdev) 702 + return -ENOMEM; 703 + 704 + btdev->hu.serdev = btdev->serdev = serdev; 705 + serdev_device_set_drvdata(serdev, btdev); 706 + 707 + btdev->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 708 + if (IS_ERR(btdev->reset)) { 709 + err = PTR_ERR(btdev->reset); 710 + dev_err(dev, "could not get reset gpio: %d", err); 711 + return err; 712 + } 713 + 714 + btdev->wakeup_host = devm_gpiod_get(dev, "host-wakeup", GPIOD_IN); 715 + if (IS_ERR(btdev->wakeup_host)) { 716 + err = PTR_ERR(btdev->wakeup_host); 717 + dev_err(dev, "could not get host wakeup gpio: %d", err); 718 + return err; 719 + } 720 + 721 + btdev->wake_irq = gpiod_to_irq(btdev->wakeup_host); 722 + 723 + err = devm_request_threaded_irq(dev, btdev->wake_irq, NULL, 724 + wakeup_handler, 725 + IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 726 + "wakeup", btdev); 727 + if (err) { 728 + dev_err(dev, "could request wakeup irq: %d", err); 729 + return err; 730 + } 731 + 732 + btdev->wakeup_bt = devm_gpiod_get(dev, "bluetooth-wakeup", 733 + GPIOD_OUT_LOW); 734 + if (IS_ERR(btdev->wakeup_bt)) { 735 + err = PTR_ERR(btdev->wakeup_bt); 736 + dev_err(dev, "could not get BT wakeup gpio: %d", err); 737 + return err; 738 + } 739 + 740 + sysclk = devm_clk_get(dev, "sysclk"); 741 + if (IS_ERR(sysclk)) { 742 + err = PTR_ERR(sysclk); 743 + dev_err(dev, "could not get sysclk: %d", err); 744 + return err; 745 + } 746 + 747 + clk_prepare_enable(sysclk); 748 + btdev->sysclk_speed = clk_get_rate(sysclk); 749 + clk_disable_unprepare(sysclk); 750 + 751 + skb_queue_head_init(&btdev->txq); 752 + 753 + btdev->hu.priv = btdev; 754 + btdev->hu.alignment = 2; /* Nokia H4+ is word aligned */ 755 + 756 + err = hci_uart_register_device(&btdev->hu, &nokia_proto); 757 + if (err) { 758 + dev_err(dev, "could not register bluetooth uart: %d", err); 759 + return err; 760 + } 761 + 762 + return 0; 763 + } 764 + 765 + static void nokia_bluetooth_serdev_remove(struct serdev_device *serdev) 766 + { 767 + struct nokia_bt_dev *btdev = serdev_device_get_drvdata(serdev); 768 + struct hci_uart *hu = &btdev->hu; 769 + struct hci_dev *hdev = hu->hdev; 770 + 771 + cancel_work_sync(&hu->write_work); 772 + 773 + hci_unregister_dev(hdev); 774 + hci_free_dev(hdev); 775 + hu->proto->close(hu); 776 + 777 + pm_runtime_disable(&btdev->serdev->dev); 778 + } 779 + 780 + static int nokia_bluetooth_runtime_suspend(struct device *dev) 781 + { 782 + struct serdev_device *serdev = to_serdev_device(dev); 783 + 784 + nokia_flow_control(serdev, false); 785 + return 0; 786 + } 787 + 788 + static int nokia_bluetooth_runtime_resume(struct device *dev) 789 + { 790 + struct serdev_device *serdev = to_serdev_device(dev); 791 + 792 + nokia_flow_control(serdev, true); 793 + return 0; 794 + } 795 + 796 + static const struct dev_pm_ops nokia_bluetooth_pm_ops = { 797 + SET_RUNTIME_PM_OPS(nokia_bluetooth_runtime_suspend, 798 + nokia_bluetooth_runtime_resume, 799 + NULL) 800 + }; 801 + 802 + #ifdef CONFIG_OF 803 + static const struct of_device_id nokia_bluetooth_of_match[] = { 804 + { .compatible = "nokia,h4p-bluetooth", }, 805 + {}, 806 + }; 807 + MODULE_DEVICE_TABLE(of, nokia_bluetooth_of_match); 808 + #endif 809 + 810 + static struct serdev_device_driver nokia_bluetooth_serdev_driver = { 811 + .probe = nokia_bluetooth_serdev_probe, 812 + .remove = nokia_bluetooth_serdev_remove, 813 + .driver = { 814 + .name = "nokia-bluetooth", 815 + .pm = &nokia_bluetooth_pm_ops, 816 + .of_match_table = of_match_ptr(nokia_bluetooth_of_match), 817 + }, 818 + }; 819 + 820 + module_serdev_device_driver(nokia_bluetooth_serdev_driver);
+356
drivers/bluetooth/hci_serdev.c
··· 1 + /* 2 + * Bluetooth HCI serdev driver lib 3 + * 4 + * Copyright (C) 2017 Linaro, Ltd., Rob Herring <robh@kernel.org> 5 + * 6 + * Based on hci_ldisc.c: 7 + * 8 + * Copyright (C) 2000-2001 Qualcomm Incorporated 9 + * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com> 10 + * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org> 11 + * 12 + * This program is free software; you can redistribute it and/or modify 13 + * it under the terms of the GNU General Public License as published by 14 + * the Free Software Foundation; either version 2 of the License, or 15 + * (at your option) any later version. 16 + * 17 + * This program is distributed in the hope that it will be useful, 18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 + * GNU General Public License for more details. 21 + * 22 + */ 23 + 24 + #include <linux/kernel.h> 25 + #include <linux/types.h> 26 + #include <linux/serdev.h> 27 + #include <linux/skbuff.h> 28 + 29 + #include <net/bluetooth/bluetooth.h> 30 + #include <net/bluetooth/hci_core.h> 31 + 32 + #include "hci_uart.h" 33 + 34 + struct serdev_device_ops hci_serdev_client_ops; 35 + 36 + static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type) 37 + { 38 + struct hci_dev *hdev = hu->hdev; 39 + 40 + /* Update HCI stat counters */ 41 + switch (pkt_type) { 42 + case HCI_COMMAND_PKT: 43 + hdev->stat.cmd_tx++; 44 + break; 45 + 46 + case HCI_ACLDATA_PKT: 47 + hdev->stat.acl_tx++; 48 + break; 49 + 50 + case HCI_SCODATA_PKT: 51 + hdev->stat.sco_tx++; 52 + break; 53 + } 54 + } 55 + 56 + static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu) 57 + { 58 + struct sk_buff *skb = hu->tx_skb; 59 + 60 + if (!skb) 61 + skb = hu->proto->dequeue(hu); 62 + else 63 + hu->tx_skb = NULL; 64 + 65 + return skb; 66 + } 67 + 68 + static void hci_uart_write_work(struct work_struct *work) 69 + { 70 + struct hci_uart *hu = container_of(work, struct hci_uart, write_work); 71 + struct serdev_device *serdev = hu->serdev; 72 + struct hci_dev *hdev = hu->hdev; 73 + struct sk_buff *skb; 74 + 75 + /* REVISIT: 76 + * should we cope with bad skbs or ->write() returning an error value? 77 + */ 78 + do { 79 + clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state); 80 + 81 + while ((skb = hci_uart_dequeue(hu))) { 82 + int len; 83 + 84 + len = serdev_device_write_buf(serdev, 85 + skb->data, skb->len); 86 + hdev->stat.byte_tx += len; 87 + 88 + skb_pull(skb, len); 89 + if (skb->len) { 90 + hu->tx_skb = skb; 91 + break; 92 + } 93 + 94 + hci_uart_tx_complete(hu, hci_skb_pkt_type(skb)); 95 + kfree_skb(skb); 96 + } 97 + } while(test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state)); 98 + 99 + clear_bit(HCI_UART_SENDING, &hu->tx_state); 100 + } 101 + 102 + /* ------- Interface to HCI layer ------ */ 103 + 104 + /* Initialize device */ 105 + static int hci_uart_open(struct hci_dev *hdev) 106 + { 107 + BT_DBG("%s %p", hdev->name, hdev); 108 + 109 + return 0; 110 + } 111 + 112 + /* Reset device */ 113 + static int hci_uart_flush(struct hci_dev *hdev) 114 + { 115 + struct hci_uart *hu = hci_get_drvdata(hdev); 116 + 117 + BT_DBG("hdev %p serdev %p", hdev, hu->serdev); 118 + 119 + if (hu->tx_skb) { 120 + kfree_skb(hu->tx_skb); hu->tx_skb = NULL; 121 + } 122 + 123 + /* Flush any pending characters in the driver and discipline. */ 124 + serdev_device_write_flush(hu->serdev); 125 + 126 + if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) 127 + hu->proto->flush(hu); 128 + 129 + return 0; 130 + } 131 + 132 + /* Close device */ 133 + static int hci_uart_close(struct hci_dev *hdev) 134 + { 135 + BT_DBG("hdev %p", hdev); 136 + 137 + hci_uart_flush(hdev); 138 + hdev->flush = NULL; 139 + 140 + return 0; 141 + } 142 + 143 + /* Send frames from HCI layer */ 144 + static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb) 145 + { 146 + struct hci_uart *hu = hci_get_drvdata(hdev); 147 + 148 + BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb), 149 + skb->len); 150 + 151 + hu->proto->enqueue(hu, skb); 152 + 153 + hci_uart_tx_wakeup(hu); 154 + 155 + return 0; 156 + } 157 + 158 + static int hci_uart_setup(struct hci_dev *hdev) 159 + { 160 + struct hci_uart *hu = hci_get_drvdata(hdev); 161 + struct hci_rp_read_local_version *ver; 162 + struct sk_buff *skb; 163 + unsigned int speed; 164 + int err; 165 + 166 + /* Init speed if any */ 167 + if (hu->init_speed) 168 + speed = hu->init_speed; 169 + else if (hu->proto->init_speed) 170 + speed = hu->proto->init_speed; 171 + else 172 + speed = 0; 173 + 174 + if (speed) 175 + serdev_device_set_baudrate(hu->serdev, speed); 176 + 177 + /* Operational speed if any */ 178 + if (hu->oper_speed) 179 + speed = hu->oper_speed; 180 + else if (hu->proto->oper_speed) 181 + speed = hu->proto->oper_speed; 182 + else 183 + speed = 0; 184 + 185 + if (hu->proto->set_baudrate && speed) { 186 + err = hu->proto->set_baudrate(hu, speed); 187 + if (err) 188 + BT_ERR("%s: failed to set baudrate", hdev->name); 189 + else 190 + serdev_device_set_baudrate(hu->serdev, speed); 191 + } 192 + 193 + if (hu->proto->setup) 194 + return hu->proto->setup(hu); 195 + 196 + if (!test_bit(HCI_UART_VND_DETECT, &hu->hdev_flags)) 197 + return 0; 198 + 199 + skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, 200 + HCI_INIT_TIMEOUT); 201 + if (IS_ERR(skb)) { 202 + BT_ERR("%s: Reading local version information failed (%ld)", 203 + hdev->name, PTR_ERR(skb)); 204 + return 0; 205 + } 206 + 207 + if (skb->len != sizeof(*ver)) { 208 + BT_ERR("%s: Event length mismatch for version information", 209 + hdev->name); 210 + } 211 + 212 + kfree_skb(skb); 213 + return 0; 214 + } 215 + 216 + /** hci_uart_write_wakeup - transmit buffer wakeup 217 + * @serdev: serial device 218 + * 219 + * This function is called by the serdev framework when it accepts 220 + * more data being sent. 221 + */ 222 + static void hci_uart_write_wakeup(struct serdev_device *serdev) 223 + { 224 + struct hci_uart *hu = serdev_device_get_drvdata(serdev); 225 + 226 + BT_DBG(""); 227 + 228 + if (!hu || serdev != hu->serdev) { 229 + WARN_ON(1); 230 + return; 231 + } 232 + 233 + if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) 234 + hci_uart_tx_wakeup(hu); 235 + } 236 + 237 + /** hci_uart_receive_buf - receive buffer wakeup 238 + * @serdev: serial device 239 + * @data: pointer to received data 240 + * @count: count of received data in bytes 241 + * 242 + * This function is called by the serdev framework when it received data 243 + * in the RX buffer. 244 + * 245 + * Return: number of processed bytes 246 + */ 247 + static int hci_uart_receive_buf(struct serdev_device *serdev, const u8 *data, 248 + size_t count) 249 + { 250 + struct hci_uart *hu = serdev_device_get_drvdata(serdev); 251 + 252 + if (!hu || serdev != hu->serdev) { 253 + WARN_ON(1); 254 + return 0; 255 + } 256 + 257 + if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) 258 + return 0; 259 + 260 + /* It does not need a lock here as it is already protected by a mutex in 261 + * tty caller 262 + */ 263 + hu->proto->recv(hu, data, count); 264 + 265 + if (hu->hdev) 266 + hu->hdev->stat.byte_rx += count; 267 + 268 + return count; 269 + } 270 + 271 + struct serdev_device_ops hci_serdev_client_ops = { 272 + .receive_buf = hci_uart_receive_buf, 273 + .write_wakeup = hci_uart_write_wakeup, 274 + }; 275 + 276 + int hci_uart_register_device(struct hci_uart *hu, 277 + const struct hci_uart_proto *p) 278 + { 279 + int err; 280 + struct hci_dev *hdev; 281 + 282 + BT_DBG(""); 283 + 284 + serdev_device_set_client_ops(hu->serdev, &hci_serdev_client_ops); 285 + 286 + err = p->open(hu); 287 + if (err) 288 + return err; 289 + 290 + hu->proto = p; 291 + set_bit(HCI_UART_PROTO_READY, &hu->flags); 292 + 293 + /* Initialize and register HCI device */ 294 + hdev = hci_alloc_dev(); 295 + if (!hdev) { 296 + BT_ERR("Can't allocate HCI device"); 297 + err = -ENOMEM; 298 + goto err_alloc; 299 + } 300 + 301 + hu->hdev = hdev; 302 + 303 + hdev->bus = HCI_UART; 304 + hci_set_drvdata(hdev, hu); 305 + 306 + INIT_WORK(&hu->write_work, hci_uart_write_work); 307 + 308 + /* Only when vendor specific setup callback is provided, consider 309 + * the manufacturer information valid. This avoids filling in the 310 + * value for Ericsson when nothing is specified. 311 + */ 312 + if (hu->proto->setup) 313 + hdev->manufacturer = hu->proto->manufacturer; 314 + 315 + hdev->open = hci_uart_open; 316 + hdev->close = hci_uart_close; 317 + hdev->flush = hci_uart_flush; 318 + hdev->send = hci_uart_send_frame; 319 + hdev->setup = hci_uart_setup; 320 + SET_HCIDEV_DEV(hdev, &hu->serdev->dev); 321 + 322 + if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags)) 323 + set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks); 324 + 325 + if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags)) 326 + set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks); 327 + 328 + if (!test_bit(HCI_UART_RESET_ON_INIT, &hu->hdev_flags)) 329 + set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks); 330 + 331 + if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags)) 332 + hdev->dev_type = HCI_AMP; 333 + else 334 + hdev->dev_type = HCI_PRIMARY; 335 + 336 + if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags)) 337 + return 0; 338 + 339 + if (hci_register_dev(hdev) < 0) { 340 + BT_ERR("Can't register HCI device"); 341 + err = -ENODEV; 342 + goto err_register; 343 + } 344 + 345 + set_bit(HCI_UART_REGISTERED, &hu->flags); 346 + 347 + return 0; 348 + 349 + err_register: 350 + hci_free_dev(hdev); 351 + err_alloc: 352 + clear_bit(HCI_UART_PROTO_READY, &hu->flags); 353 + p->close(hu); 354 + return err; 355 + } 356 + EXPORT_SYMBOL_GPL(hci_uart_register_device);
+7 -1
drivers/bluetooth/hci_uart.h
··· 58 58 #define HCI_UART_VND_DETECT 5 59 59 60 60 struct hci_uart; 61 + struct serdev_device; 61 62 62 63 struct hci_uart_proto { 63 64 unsigned int id; ··· 78 77 79 78 struct hci_uart { 80 79 struct tty_struct *tty; 80 + struct serdev_device *serdev; 81 81 struct hci_dev *hdev; 82 82 unsigned long flags; 83 83 unsigned long hdev_flags; ··· 94 92 95 93 unsigned int init_speed; 96 94 unsigned int oper_speed; 95 + 96 + u8 alignment; 97 + u8 padding; 97 98 }; 98 99 99 100 /* HCI_UART proto flag bits */ ··· 110 105 111 106 int hci_uart_register_proto(const struct hci_uart_proto *p); 112 107 int hci_uart_unregister_proto(const struct hci_uart_proto *p); 108 + int hci_uart_register_device(struct hci_uart *hu, const struct hci_uart_proto *p); 109 + 113 110 int hci_uart_tx_wakeup(struct hci_uart *hu); 114 111 int hci_uart_init_ready(struct hci_uart *hu); 115 - void hci_uart_init_tty(struct hci_uart *hu); 116 112 void hci_uart_set_baudrate(struct hci_uart *hu, unsigned int speed); 117 113 void hci_uart_set_flow_control(struct hci_uart *hu, bool enable); 118 114 void hci_uart_set_speeds(struct hci_uart *hu, unsigned int init_speed,
+22
drivers/net/ieee802154/Kconfig
··· 82 82 83 83 This driver can also be built as a module. To do so, say M here. 84 84 the module will be called 'adf7242'. 85 + 86 + config IEEE802154_CA8210 87 + tristate "Cascoda CA8210 transceiver driver" 88 + depends on IEEE802154_DRIVERS && MAC802154 89 + depends on SPI 90 + select COMMON_CLK 91 + ---help--- 92 + Say Y here to enable the CA8210 SPI 802.15.4 wireless 93 + controller. 94 + 95 + This driver can also be built as a module. To do so, say M here. 96 + the module will be called 'ca8210'. 97 + 98 + config IEEE802154_CA8210_DEBUGFS 99 + bool "CA8210 debugfs interface" 100 + depends on IEEE802154_CA8210 101 + depends on DEBUG_FS 102 + ---help--- 103 + This option compiles debugfs code for the ca8210 driver. This 104 + exposes a debugfs node for each CA8210 instance which allows 105 + direct use of the Cascoda API, exposing the 802.15.4 MAC 106 + management entities.
+1
drivers/net/ieee802154/Makefile
··· 4 4 obj-$(CONFIG_IEEE802154_CC2520) += cc2520.o 5 5 obj-$(CONFIG_IEEE802154_ATUSB) += atusb.o 6 6 obj-$(CONFIG_IEEE802154_ADF7242) += adf7242.o 7 + obj-$(CONFIG_IEEE802154_CA8210) += ca8210.o
+3242
drivers/net/ieee802154/ca8210.c
··· 1 + /* 2 + * http://www.cascoda.com/products/ca-821x/ 3 + * Copyright (c) 2016, Cascoda, Ltd. 4 + * All rights reserved. 5 + * 6 + * This code is dual-licensed under both GPLv2 and 3-clause BSD. What follows is 7 + * the license notice for both respectively. 8 + * 9 + ******************************************************************************* 10 + * 11 + * This program is free software; you can redistribute it and/or 12 + * modify it under the terms of the GNU General Public License 13 + * as published by the Free Software Foundation; either version 2 14 + * of the License, or (at your option) any later version. 15 + * 16 + * This program is distributed in the hope that it will be useful, 17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 + * GNU General Public License for more details. 20 + * 21 + ******************************************************************************* 22 + * 23 + * Redistribution and use in source and binary forms, with or without 24 + * modification, are permitted provided that the following conditions are met: 25 + * 26 + * 1. Redistributions of source code must retain the above copyright notice, 27 + * this list of conditions and the following disclaimer. 28 + * 29 + * 2. Redistributions in binary form must reproduce the above copyright notice, 30 + * this list of conditions and the following disclaimer in the documentation 31 + * and/or other materials provided with the distribution. 32 + * 33 + * 3. Neither the name of the copyright holder nor the names of its contributors 34 + * may be used to endorse or promote products derived from this software without 35 + * specific prior written permission. 36 + * 37 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 38 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 39 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 40 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE 41 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 42 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 43 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 44 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 45 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 46 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 47 + * POSSIBILITY OF SUCH DAMAGE. 48 + */ 49 + 50 + #include <linux/cdev.h> 51 + #include <linux/clk-provider.h> 52 + #include <linux/debugfs.h> 53 + #include <linux/delay.h> 54 + #include <linux/gpio.h> 55 + #include <linux/ieee802154.h> 56 + #include <linux/kfifo.h> 57 + #include <linux/of.h> 58 + #include <linux/of_device.h> 59 + #include <linux/of_gpio.h> 60 + #include <linux/module.h> 61 + #include <linux/mutex.h> 62 + #include <linux/poll.h> 63 + #include <linux/skbuff.h> 64 + #include <linux/slab.h> 65 + #include <linux/spi/spi.h> 66 + #include <linux/spinlock.h> 67 + #include <linux/string.h> 68 + #include <linux/workqueue.h> 69 + 70 + #include <net/ieee802154_netdev.h> 71 + #include <net/mac802154.h> 72 + 73 + #define DRIVER_NAME "ca8210" 74 + 75 + /* external clock frequencies */ 76 + #define ONE_MHZ 1000000 77 + #define TWO_MHZ (2 * ONE_MHZ) 78 + #define FOUR_MHZ (4 * ONE_MHZ) 79 + #define EIGHT_MHZ (8 * ONE_MHZ) 80 + #define SIXTEEN_MHZ (16 * ONE_MHZ) 81 + 82 + /* spi constants */ 83 + #define CA8210_SPI_BUF_SIZE 256 84 + #define CA8210_SYNC_TIMEOUT 1000 /* Timeout for synchronous commands [ms] */ 85 + 86 + /* test interface constants */ 87 + #define CA8210_TEST_INT_FILE_NAME "ca8210_test" 88 + #define CA8210_TEST_INT_FIFO_SIZE 256 89 + 90 + /* MAC status enumerations */ 91 + #define MAC_SUCCESS (0x00) 92 + #define MAC_ERROR (0x01) 93 + #define MAC_CANCELLED (0x02) 94 + #define MAC_READY_FOR_POLL (0x03) 95 + #define MAC_COUNTER_ERROR (0xDB) 96 + #define MAC_IMPROPER_KEY_TYPE (0xDC) 97 + #define MAC_IMPROPER_SECURITY_LEVEL (0xDD) 98 + #define MAC_UNSUPPORTED_LEGACY (0xDE) 99 + #define MAC_UNSUPPORTED_SECURITY (0xDF) 100 + #define MAC_BEACON_LOST (0xE0) 101 + #define MAC_CHANNEL_ACCESS_FAILURE (0xE1) 102 + #define MAC_DENIED (0xE2) 103 + #define MAC_DISABLE_TRX_FAILURE (0xE3) 104 + #define MAC_SECURITY_ERROR (0xE4) 105 + #define MAC_FRAME_TOO_LONG (0xE5) 106 + #define MAC_INVALID_GTS (0xE6) 107 + #define MAC_INVALID_HANDLE (0xE7) 108 + #define MAC_INVALID_PARAMETER (0xE8) 109 + #define MAC_NO_ACK (0xE9) 110 + #define MAC_NO_BEACON (0xEA) 111 + #define MAC_NO_DATA (0xEB) 112 + #define MAC_NO_SHORT_ADDRESS (0xEC) 113 + #define MAC_OUT_OF_CAP (0xED) 114 + #define MAC_PAN_ID_CONFLICT (0xEE) 115 + #define MAC_REALIGNMENT (0xEF) 116 + #define MAC_TRANSACTION_EXPIRED (0xF0) 117 + #define MAC_TRANSACTION_OVERFLOW (0xF1) 118 + #define MAC_TX_ACTIVE (0xF2) 119 + #define MAC_UNAVAILABLE_KEY (0xF3) 120 + #define MAC_UNSUPPORTED_ATTRIBUTE (0xF4) 121 + #define MAC_INVALID_ADDRESS (0xF5) 122 + #define MAC_ON_TIME_TOO_LONG (0xF6) 123 + #define MAC_PAST_TIME (0xF7) 124 + #define MAC_TRACKING_OFF (0xF8) 125 + #define MAC_INVALID_INDEX (0xF9) 126 + #define MAC_LIMIT_REACHED (0xFA) 127 + #define MAC_READ_ONLY (0xFB) 128 + #define MAC_SCAN_IN_PROGRESS (0xFC) 129 + #define MAC_SUPERFRAME_OVERLAP (0xFD) 130 + #define MAC_SYSTEM_ERROR (0xFF) 131 + 132 + /* HWME attribute IDs */ 133 + #define HWME_EDTHRESHOLD (0x04) 134 + #define HWME_EDVALUE (0x06) 135 + #define HWME_SYSCLKOUT (0x0F) 136 + #define HWME_LQILIMIT (0x11) 137 + 138 + /* TDME attribute IDs */ 139 + #define TDME_CHANNEL (0x00) 140 + #define TDME_ATM_CONFIG (0x06) 141 + 142 + #define MAX_HWME_ATTRIBUTE_SIZE 16 143 + #define MAX_TDME_ATTRIBUTE_SIZE 2 144 + 145 + /* PHY/MAC PIB Attribute Enumerations */ 146 + #define PHY_CURRENT_CHANNEL (0x00) 147 + #define PHY_TRANSMIT_POWER (0x02) 148 + #define PHY_CCA_MODE (0x03) 149 + #define MAC_ASSOCIATION_PERMIT (0x41) 150 + #define MAC_AUTO_REQUEST (0x42) 151 + #define MAC_BATT_LIFE_EXT (0x43) 152 + #define MAC_BATT_LIFE_EXT_PERIODS (0x44) 153 + #define MAC_BEACON_PAYLOAD (0x45) 154 + #define MAC_BEACON_PAYLOAD_LENGTH (0x46) 155 + #define MAC_BEACON_ORDER (0x47) 156 + #define MAC_GTS_PERMIT (0x4d) 157 + #define MAC_MAX_CSMA_BACKOFFS (0x4e) 158 + #define MAC_MIN_BE (0x4f) 159 + #define MAC_PAN_ID (0x50) 160 + #define MAC_PROMISCUOUS_MODE (0x51) 161 + #define MAC_RX_ON_WHEN_IDLE (0x52) 162 + #define MAC_SHORT_ADDRESS (0x53) 163 + #define MAC_SUPERFRAME_ORDER (0x54) 164 + #define MAC_ASSOCIATED_PAN_COORD (0x56) 165 + #define MAC_MAX_BE (0x57) 166 + #define MAC_MAX_FRAME_RETRIES (0x59) 167 + #define MAC_RESPONSE_WAIT_TIME (0x5A) 168 + #define MAC_SECURITY_ENABLED (0x5D) 169 + 170 + #define MAC_AUTO_REQUEST_SECURITY_LEVEL (0x78) 171 + #define MAC_AUTO_REQUEST_KEY_ID_MODE (0x79) 172 + 173 + #define NS_IEEE_ADDRESS (0xFF) /* Non-standard IEEE address */ 174 + 175 + /* MAC Address Mode Definitions */ 176 + #define MAC_MODE_NO_ADDR (0x00) 177 + #define MAC_MODE_SHORT_ADDR (0x02) 178 + #define MAC_MODE_LONG_ADDR (0x03) 179 + 180 + /* MAC constants */ 181 + #define MAX_BEACON_OVERHEAD (75) 182 + #define MAX_BEACON_PAYLOAD_LENGTH (IEEE802154_MTU - MAX_BEACON_OVERHEAD) 183 + 184 + #define MAX_ATTRIBUTE_SIZE (122) 185 + #define MAX_DATA_SIZE (114) 186 + 187 + #define CA8210_VALID_CHANNELS (0x07FFF800) 188 + 189 + /* MAC workarounds for V1.1 and MPW silicon (V0.x) */ 190 + #define CA8210_MAC_WORKAROUNDS (0) 191 + #define CA8210_MAC_MPW (0) 192 + 193 + /* memory manipulation macros */ 194 + #define LS_BYTE(x) ((u8)((x) & 0xFF)) 195 + #define MS_BYTE(x) ((u8)(((x) >> 8) & 0xFF)) 196 + 197 + /* message ID codes in SPI commands */ 198 + /* downstream */ 199 + #define MCPS_DATA_REQUEST (0x00) 200 + #define MLME_ASSOCIATE_REQUEST (0x02) 201 + #define MLME_ASSOCIATE_RESPONSE (0x03) 202 + #define MLME_DISASSOCIATE_REQUEST (0x04) 203 + #define MLME_GET_REQUEST (0x05) 204 + #define MLME_ORPHAN_RESPONSE (0x06) 205 + #define MLME_RESET_REQUEST (0x07) 206 + #define MLME_RX_ENABLE_REQUEST (0x08) 207 + #define MLME_SCAN_REQUEST (0x09) 208 + #define MLME_SET_REQUEST (0x0A) 209 + #define MLME_START_REQUEST (0x0B) 210 + #define MLME_POLL_REQUEST (0x0D) 211 + #define HWME_SET_REQUEST (0x0E) 212 + #define HWME_GET_REQUEST (0x0F) 213 + #define TDME_SETSFR_REQUEST (0x11) 214 + #define TDME_GETSFR_REQUEST (0x12) 215 + #define TDME_SET_REQUEST (0x14) 216 + /* upstream */ 217 + #define MCPS_DATA_INDICATION (0x00) 218 + #define MCPS_DATA_CONFIRM (0x01) 219 + #define MLME_RESET_CONFIRM (0x0A) 220 + #define MLME_SET_CONFIRM (0x0E) 221 + #define MLME_START_CONFIRM (0x0F) 222 + #define HWME_SET_CONFIRM (0x12) 223 + #define HWME_GET_CONFIRM (0x13) 224 + #define HWME_WAKEUP_INDICATION (0x15) 225 + #define TDME_SETSFR_CONFIRM (0x17) 226 + 227 + /* SPI command IDs */ 228 + /* bit indicating a confirm or indication from slave to master */ 229 + #define SPI_S2M (0x20) 230 + /* bit indicating a synchronous message */ 231 + #define SPI_SYN (0x40) 232 + 233 + /* SPI command definitions */ 234 + #define SPI_IDLE (0xFF) 235 + #define SPI_NACK (0xF0) 236 + 237 + #define SPI_MCPS_DATA_REQUEST (MCPS_DATA_REQUEST) 238 + #define SPI_MCPS_DATA_INDICATION (MCPS_DATA_INDICATION + SPI_S2M) 239 + #define SPI_MCPS_DATA_CONFIRM (MCPS_DATA_CONFIRM + SPI_S2M) 240 + 241 + #define SPI_MLME_ASSOCIATE_REQUEST (MLME_ASSOCIATE_REQUEST) 242 + #define SPI_MLME_RESET_REQUEST (MLME_RESET_REQUEST + SPI_SYN) 243 + #define SPI_MLME_SET_REQUEST (MLME_SET_REQUEST + SPI_SYN) 244 + #define SPI_MLME_START_REQUEST (MLME_START_REQUEST + SPI_SYN) 245 + #define SPI_MLME_RESET_CONFIRM (MLME_RESET_CONFIRM + SPI_S2M + SPI_SYN) 246 + #define SPI_MLME_SET_CONFIRM (MLME_SET_CONFIRM + SPI_S2M + SPI_SYN) 247 + #define SPI_MLME_START_CONFIRM (MLME_START_CONFIRM + SPI_S2M + SPI_SYN) 248 + 249 + #define SPI_HWME_SET_REQUEST (HWME_SET_REQUEST + SPI_SYN) 250 + #define SPI_HWME_GET_REQUEST (HWME_GET_REQUEST + SPI_SYN) 251 + #define SPI_HWME_SET_CONFIRM (HWME_SET_CONFIRM + SPI_S2M + SPI_SYN) 252 + #define SPI_HWME_GET_CONFIRM (HWME_GET_CONFIRM + SPI_S2M + SPI_SYN) 253 + #define SPI_HWME_WAKEUP_INDICATION (HWME_WAKEUP_INDICATION + SPI_S2M) 254 + 255 + #define SPI_TDME_SETSFR_REQUEST (TDME_SETSFR_REQUEST + SPI_SYN) 256 + #define SPI_TDME_SET_REQUEST (TDME_SET_REQUEST + SPI_SYN) 257 + #define SPI_TDME_SETSFR_CONFIRM (TDME_SETSFR_CONFIRM + SPI_S2M + SPI_SYN) 258 + 259 + /* TDME SFR addresses */ 260 + /* Page 0 */ 261 + #define CA8210_SFR_PACFG (0xB1) 262 + #define CA8210_SFR_MACCON (0xD8) 263 + #define CA8210_SFR_PACFGIB (0xFE) 264 + /* Page 1 */ 265 + #define CA8210_SFR_LOTXCAL (0xBF) 266 + #define CA8210_SFR_PTHRH (0xD1) 267 + #define CA8210_SFR_PRECFG (0xD3) 268 + #define CA8210_SFR_LNAGX40 (0xE1) 269 + #define CA8210_SFR_LNAGX41 (0xE2) 270 + #define CA8210_SFR_LNAGX42 (0xE3) 271 + #define CA8210_SFR_LNAGX43 (0xE4) 272 + #define CA8210_SFR_LNAGX44 (0xE5) 273 + #define CA8210_SFR_LNAGX45 (0xE6) 274 + #define CA8210_SFR_LNAGX46 (0xE7) 275 + #define CA8210_SFR_LNAGX47 (0xE9) 276 + 277 + #define PACFGIB_DEFAULT_CURRENT (0x3F) 278 + #define PTHRH_DEFAULT_THRESHOLD (0x5A) 279 + #define LNAGX40_DEFAULT_GAIN (0x29) /* 10dB */ 280 + #define LNAGX41_DEFAULT_GAIN (0x54) /* 21dB */ 281 + #define LNAGX42_DEFAULT_GAIN (0x6C) /* 27dB */ 282 + #define LNAGX43_DEFAULT_GAIN (0x7A) /* 30dB */ 283 + #define LNAGX44_DEFAULT_GAIN (0x84) /* 33dB */ 284 + #define LNAGX45_DEFAULT_GAIN (0x8B) /* 34dB */ 285 + #define LNAGX46_DEFAULT_GAIN (0x92) /* 36dB */ 286 + #define LNAGX47_DEFAULT_GAIN (0x96) /* 37dB */ 287 + 288 + #define CA8210_IOCTL_HARD_RESET (0x00) 289 + 290 + /* Structs/Enums */ 291 + 292 + /** 293 + * struct cas_control - spi transfer structure 294 + * @msg: spi_message for each exchange 295 + * @transfer: spi_transfer for each exchange 296 + * @tx_buf: source array for transmission 297 + * @tx_in_buf: array storing bytes received during transmission 298 + * @priv: pointer to private data 299 + * 300 + * This structure stores all the necessary data passed around during a single 301 + * spi exchange. 302 + */ 303 + struct cas_control { 304 + struct spi_message msg; 305 + struct spi_transfer transfer; 306 + 307 + u8 tx_buf[CA8210_SPI_BUF_SIZE]; 308 + u8 tx_in_buf[CA8210_SPI_BUF_SIZE]; 309 + 310 + struct ca8210_priv *priv; 311 + }; 312 + 313 + /** 314 + * struct ca8210_test - ca8210 test interface structure 315 + * @ca8210_dfs_spi_int: pointer to the entry in the debug fs for this device 316 + * @up_fifo: fifo for upstream messages 317 + * 318 + * This structure stores all the data pertaining to the debug interface 319 + */ 320 + struct ca8210_test { 321 + struct dentry *ca8210_dfs_spi_int; 322 + struct kfifo up_fifo; 323 + wait_queue_head_t readq; 324 + }; 325 + 326 + /** 327 + * struct ca8210_priv - ca8210 private data structure 328 + * @spi: pointer to the ca8210 spi device object 329 + * @hw: pointer to the ca8210 ieee802154_hw object 330 + * @hw_registered: true if hw has been registered with ieee802154 331 + * @lock: spinlock protecting the private data area 332 + * @mlme_workqueue: workqueue for triggering MLME Reset 333 + * @irq_workqueue: workqueue for irq processing 334 + * @tx_skb: current socket buffer to transmit 335 + * @nextmsduhandle: msdu handle to pass to the 15.4 MAC layer for the 336 + * next transmission 337 + * @clk: external clock provided by the ca8210 338 + * @last_dsn: sequence number of last data packet received, for 339 + * resend detection 340 + * @test: test interface data section for this instance 341 + * @async_tx_pending: true if an asynchronous transmission was started and 342 + * is not complete 343 + * @sync_command_response: pointer to buffer to fill with sync response 344 + * @ca8210_is_awake: nonzero if ca8210 is initialised, ready for comms 345 + * @sync_down: counts number of downstream synchronous commands 346 + * @sync_up: counts number of upstream synchronous commands 347 + * @spi_transfer_complete completion object for a single spi_transfer 348 + * @sync_exchange_complete completion object for a complete synchronous API 349 + * exchange 350 + * @promiscuous whether the ca8210 is in promiscuous mode or not 351 + * @retries: records how many times the current pending spi 352 + * transfer has been retried 353 + */ 354 + struct ca8210_priv { 355 + struct spi_device *spi; 356 + struct ieee802154_hw *hw; 357 + bool hw_registered; 358 + spinlock_t lock; 359 + struct workqueue_struct *mlme_workqueue; 360 + struct workqueue_struct *irq_workqueue; 361 + struct sk_buff *tx_skb; 362 + u8 nextmsduhandle; 363 + struct clk *clk; 364 + int last_dsn; 365 + struct ca8210_test test; 366 + bool async_tx_pending; 367 + u8 *sync_command_response; 368 + struct completion ca8210_is_awake; 369 + int sync_down, sync_up; 370 + struct completion spi_transfer_complete, sync_exchange_complete; 371 + bool promiscuous; 372 + int retries; 373 + }; 374 + 375 + /** 376 + * struct work_priv_container - link between a work object and the relevant 377 + * device's private data 378 + * @work: work object being executed 379 + * @priv: device's private data section 380 + * 381 + */ 382 + struct work_priv_container { 383 + struct work_struct work; 384 + struct ca8210_priv *priv; 385 + }; 386 + 387 + /** 388 + * struct ca8210_platform_data - ca8210 platform data structure 389 + * @extclockenable: true if the external clock is to be enabled 390 + * @extclockfreq: frequency of the external clock 391 + * @extclockgpio: ca8210 output gpio of the external clock 392 + * @gpio_reset: gpio number of ca8210 reset line 393 + * @gpio_irq: gpio number of ca8210 interrupt line 394 + * @irq_id: identifier for the ca8210 irq 395 + * 396 + */ 397 + struct ca8210_platform_data { 398 + bool extclockenable; 399 + unsigned int extclockfreq; 400 + unsigned int extclockgpio; 401 + int gpio_reset; 402 + int gpio_irq; 403 + int irq_id; 404 + }; 405 + 406 + /** 407 + * struct fulladdr - full MAC addressing information structure 408 + * @mode: address mode (none, short, extended) 409 + * @pan_id: 16-bit LE pan id 410 + * @address: LE address, variable length as specified by mode 411 + * 412 + */ 413 + struct fulladdr { 414 + u8 mode; 415 + u8 pan_id[2]; 416 + u8 address[8]; 417 + }; 418 + 419 + /** 420 + * union macaddr: generic MAC address container 421 + * @short_addr: 16-bit short address 422 + * @ieee_address: 64-bit extended address as LE byte array 423 + * 424 + */ 425 + union macaddr { 426 + u16 short_address; 427 + u8 ieee_address[8]; 428 + }; 429 + 430 + /** 431 + * struct secspec: security specification for SAP commands 432 + * @security_level: 0-7, controls level of authentication & encryption 433 + * @key_id_mode: 0-3, specifies how to obtain key 434 + * @key_source: extended key retrieval data 435 + * @key_index: single-byte key identifier 436 + * 437 + */ 438 + struct secspec { 439 + u8 security_level; 440 + u8 key_id_mode; 441 + u8 key_source[8]; 442 + u8 key_index; 443 + }; 444 + 445 + /* downlink functions parameter set definitions */ 446 + struct mcps_data_request_pset { 447 + u8 src_addr_mode; 448 + struct fulladdr dst; 449 + u8 msdu_length; 450 + u8 msdu_handle; 451 + u8 tx_options; 452 + u8 msdu[MAX_DATA_SIZE]; 453 + }; 454 + 455 + struct mlme_set_request_pset { 456 + u8 pib_attribute; 457 + u8 pib_attribute_index; 458 + u8 pib_attribute_length; 459 + u8 pib_attribute_value[MAX_ATTRIBUTE_SIZE]; 460 + }; 461 + 462 + struct hwme_set_request_pset { 463 + u8 hw_attribute; 464 + u8 hw_attribute_length; 465 + u8 hw_attribute_value[MAX_HWME_ATTRIBUTE_SIZE]; 466 + }; 467 + 468 + struct hwme_get_request_pset { 469 + u8 hw_attribute; 470 + }; 471 + 472 + struct tdme_setsfr_request_pset { 473 + u8 sfr_page; 474 + u8 sfr_address; 475 + u8 sfr_value; 476 + }; 477 + 478 + /* uplink functions parameter set definitions */ 479 + struct hwme_set_confirm_pset { 480 + u8 status; 481 + u8 hw_attribute; 482 + }; 483 + 484 + struct hwme_get_confirm_pset { 485 + u8 status; 486 + u8 hw_attribute; 487 + u8 hw_attribute_length; 488 + u8 hw_attribute_value[MAX_HWME_ATTRIBUTE_SIZE]; 489 + }; 490 + 491 + struct tdme_setsfr_confirm_pset { 492 + u8 status; 493 + u8 sfr_page; 494 + u8 sfr_address; 495 + }; 496 + 497 + struct mac_message { 498 + u8 command_id; 499 + u8 length; 500 + union { 501 + struct mcps_data_request_pset data_req; 502 + struct mlme_set_request_pset set_req; 503 + struct hwme_set_request_pset hwme_set_req; 504 + struct hwme_get_request_pset hwme_get_req; 505 + struct tdme_setsfr_request_pset tdme_set_sfr_req; 506 + struct hwme_set_confirm_pset hwme_set_cnf; 507 + struct hwme_get_confirm_pset hwme_get_cnf; 508 + struct tdme_setsfr_confirm_pset tdme_set_sfr_cnf; 509 + u8 u8param; 510 + u8 status; 511 + u8 payload[148]; 512 + } pdata; 513 + }; 514 + 515 + union pa_cfg_sfr { 516 + struct { 517 + u8 bias_current_trim : 3; 518 + u8 /* reserved */ : 1; 519 + u8 buffer_capacitor_trim : 3; 520 + u8 boost : 1; 521 + }; 522 + u8 paib; 523 + }; 524 + 525 + struct preamble_cfg_sfr { 526 + u8 timeout_symbols : 3; 527 + u8 acquisition_symbols : 3; 528 + u8 search_symbols : 2; 529 + }; 530 + 531 + static int (*cascoda_api_upstream)( 532 + const u8 *buf, 533 + size_t len, 534 + void *device_ref 535 + ); 536 + 537 + /** 538 + * link_to_linux_err() - Translates an 802.15.4 return code into the closest 539 + * linux error 540 + * @link_status: 802.15.4 status code 541 + * 542 + * Return: 0 or Linux error code 543 + */ 544 + static int link_to_linux_err(int link_status) 545 + { 546 + if (link_status < 0) { 547 + /* status is already a Linux code */ 548 + return link_status; 549 + } 550 + switch (link_status) { 551 + case MAC_SUCCESS: 552 + case MAC_REALIGNMENT: 553 + return 0; 554 + case MAC_IMPROPER_KEY_TYPE: 555 + return -EKEYREJECTED; 556 + case MAC_IMPROPER_SECURITY_LEVEL: 557 + case MAC_UNSUPPORTED_LEGACY: 558 + case MAC_DENIED: 559 + return -EACCES; 560 + case MAC_BEACON_LOST: 561 + case MAC_NO_ACK: 562 + case MAC_NO_BEACON: 563 + return -ENETUNREACH; 564 + case MAC_CHANNEL_ACCESS_FAILURE: 565 + case MAC_TX_ACTIVE: 566 + case MAC_SCAN_IN_PROGRESS: 567 + return -EBUSY; 568 + case MAC_DISABLE_TRX_FAILURE: 569 + case MAC_OUT_OF_CAP: 570 + return -EAGAIN; 571 + case MAC_FRAME_TOO_LONG: 572 + return -EMSGSIZE; 573 + case MAC_INVALID_GTS: 574 + case MAC_PAST_TIME: 575 + return -EBADSLT; 576 + case MAC_INVALID_HANDLE: 577 + return -EBADMSG; 578 + case MAC_INVALID_PARAMETER: 579 + case MAC_UNSUPPORTED_ATTRIBUTE: 580 + case MAC_ON_TIME_TOO_LONG: 581 + case MAC_INVALID_INDEX: 582 + return -EINVAL; 583 + case MAC_NO_DATA: 584 + return -ENODATA; 585 + case MAC_NO_SHORT_ADDRESS: 586 + return -EFAULT; 587 + case MAC_PAN_ID_CONFLICT: 588 + return -EADDRINUSE; 589 + case MAC_TRANSACTION_EXPIRED: 590 + return -ETIME; 591 + case MAC_TRANSACTION_OVERFLOW: 592 + return -ENOBUFS; 593 + case MAC_UNAVAILABLE_KEY: 594 + return -ENOKEY; 595 + case MAC_INVALID_ADDRESS: 596 + return -ENXIO; 597 + case MAC_TRACKING_OFF: 598 + case MAC_SUPERFRAME_OVERLAP: 599 + return -EREMOTEIO; 600 + case MAC_LIMIT_REACHED: 601 + return -EDQUOT; 602 + case MAC_READ_ONLY: 603 + return -EROFS; 604 + default: 605 + return -EPROTO; 606 + } 607 + } 608 + 609 + /** 610 + * ca8210_test_int_driver_write() - Writes a message to the test interface to be 611 + * read by the userspace 612 + * @buf: Buffer containing upstream message 613 + * @len: length of message to write 614 + * @spi: SPI device of message originator 615 + * 616 + * Return: 0 or linux error code 617 + */ 618 + static int ca8210_test_int_driver_write( 619 + const u8 *buf, 620 + size_t len, 621 + void *spi 622 + ) 623 + { 624 + struct ca8210_priv *priv = spi_get_drvdata(spi); 625 + struct ca8210_test *test = &priv->test; 626 + char *fifo_buffer; 627 + int i; 628 + 629 + dev_dbg( 630 + &priv->spi->dev, 631 + "test_interface: Buffering upstream message:\n" 632 + ); 633 + for (i = 0; i < len; i++) 634 + dev_dbg(&priv->spi->dev, "%#03x\n", buf[i]); 635 + 636 + fifo_buffer = kmalloc(len, GFP_KERNEL); 637 + if (!fifo_buffer) 638 + return -ENOMEM; 639 + memcpy(fifo_buffer, buf, len); 640 + kfifo_in(&test->up_fifo, &fifo_buffer, 4); 641 + wake_up_interruptible(&priv->test.readq); 642 + 643 + return 0; 644 + } 645 + 646 + /* SPI Operation */ 647 + 648 + static int ca8210_net_rx( 649 + struct ieee802154_hw *hw, 650 + u8 *command, 651 + size_t len 652 + ); 653 + static u8 mlme_reset_request_sync( 654 + u8 set_default_pib, 655 + void *device_ref 656 + ); 657 + static int ca8210_spi_transfer( 658 + struct spi_device *spi, 659 + const u8 *buf, 660 + size_t len 661 + ); 662 + 663 + /** 664 + * ca8210_reset_send() - Hard resets the ca8210 for a given time 665 + * @spi: Pointer to target ca8210 spi device 666 + * @ms: Milliseconds to hold the reset line low for 667 + */ 668 + static void ca8210_reset_send(struct spi_device *spi, unsigned int ms) 669 + { 670 + struct ca8210_platform_data *pdata = spi->dev.platform_data; 671 + struct ca8210_priv *priv = spi_get_drvdata(spi); 672 + long status; 673 + 674 + gpio_set_value(pdata->gpio_reset, 0); 675 + reinit_completion(&priv->ca8210_is_awake); 676 + msleep(ms); 677 + gpio_set_value(pdata->gpio_reset, 1); 678 + priv->promiscuous = false; 679 + 680 + /* Wait until wakeup indication seen */ 681 + status = wait_for_completion_interruptible_timeout( 682 + &priv->ca8210_is_awake, 683 + msecs_to_jiffies(CA8210_SYNC_TIMEOUT) 684 + ); 685 + if (status == 0) { 686 + dev_crit( 687 + &spi->dev, 688 + "Fatal: No wakeup from ca8210 after reset!\n" 689 + ); 690 + } 691 + 692 + dev_dbg(&spi->dev, "Reset the device\n"); 693 + } 694 + 695 + /** 696 + * ca8210_mlme_reset_worker() - Resets the MLME, Called when the MAC OVERFLOW 697 + * condition happens. 698 + * @work: Pointer to work being executed 699 + */ 700 + static void ca8210_mlme_reset_worker(struct work_struct *work) 701 + { 702 + struct work_priv_container *wpc = container_of( 703 + work, 704 + struct work_priv_container, 705 + work 706 + ); 707 + struct ca8210_priv *priv = wpc->priv; 708 + 709 + mlme_reset_request_sync(0, priv->spi); 710 + kfree(wpc); 711 + } 712 + 713 + /** 714 + * ca8210_rx_done() - Calls various message dispatches responding to a received 715 + * command 716 + * @arg: Pointer to the cas_control object for the relevant spi transfer 717 + * 718 + * Presents a received SAP command from the ca8210 to the Cascoda EVBME, test 719 + * interface and network driver. 720 + */ 721 + static void ca8210_rx_done(struct cas_control *cas_ctl) 722 + { 723 + u8 *buf; 724 + u8 len; 725 + struct work_priv_container *mlme_reset_wpc; 726 + struct ca8210_priv *priv = cas_ctl->priv; 727 + 728 + buf = cas_ctl->tx_in_buf; 729 + len = buf[1] + 2; 730 + if (len > CA8210_SPI_BUF_SIZE) { 731 + dev_crit( 732 + &priv->spi->dev, 733 + "Received packet len (%d) erroneously long\n", 734 + len 735 + ); 736 + goto finish; 737 + } 738 + 739 + if (buf[0] & SPI_SYN) { 740 + if (priv->sync_command_response) { 741 + memcpy(priv->sync_command_response, buf, len); 742 + complete(&priv->sync_exchange_complete); 743 + } else { 744 + if (cascoda_api_upstream) 745 + cascoda_api_upstream(buf, len, priv->spi); 746 + priv->sync_up++; 747 + } 748 + } else { 749 + if (cascoda_api_upstream) 750 + cascoda_api_upstream(buf, len, priv->spi); 751 + } 752 + 753 + ca8210_net_rx(priv->hw, buf, len); 754 + if (buf[0] == SPI_MCPS_DATA_CONFIRM) { 755 + if (buf[3] == MAC_TRANSACTION_OVERFLOW) { 756 + dev_info( 757 + &priv->spi->dev, 758 + "Waiting for transaction overflow to stabilise...\n"); 759 + msleep(2000); 760 + dev_info( 761 + &priv->spi->dev, 762 + "Resetting MAC...\n"); 763 + 764 + mlme_reset_wpc = kmalloc(sizeof(*mlme_reset_wpc), 765 + GFP_KERNEL); 766 + if (!mlme_reset_wpc) 767 + goto finish; 768 + INIT_WORK( 769 + &mlme_reset_wpc->work, 770 + ca8210_mlme_reset_worker 771 + ); 772 + mlme_reset_wpc->priv = priv; 773 + queue_work(priv->mlme_workqueue, &mlme_reset_wpc->work); 774 + } 775 + } else if (buf[0] == SPI_HWME_WAKEUP_INDICATION) { 776 + dev_notice( 777 + &priv->spi->dev, 778 + "Wakeup indication received, reason:\n" 779 + ); 780 + switch (buf[2]) { 781 + case 0: 782 + dev_notice( 783 + &priv->spi->dev, 784 + "Transceiver woken up from Power Up / System Reset\n" 785 + ); 786 + break; 787 + case 1: 788 + dev_notice( 789 + &priv->spi->dev, 790 + "Watchdog Timer Time-Out\n" 791 + ); 792 + break; 793 + case 2: 794 + dev_notice( 795 + &priv->spi->dev, 796 + "Transceiver woken up from Power-Off by Sleep Timer Time-Out\n"); 797 + break; 798 + case 3: 799 + dev_notice( 800 + &priv->spi->dev, 801 + "Transceiver woken up from Power-Off by GPIO Activity\n" 802 + ); 803 + break; 804 + case 4: 805 + dev_notice( 806 + &priv->spi->dev, 807 + "Transceiver woken up from Standby by Sleep Timer Time-Out\n" 808 + ); 809 + break; 810 + case 5: 811 + dev_notice( 812 + &priv->spi->dev, 813 + "Transceiver woken up from Standby by GPIO Activity\n" 814 + ); 815 + break; 816 + case 6: 817 + dev_notice( 818 + &priv->spi->dev, 819 + "Sleep-Timer Time-Out in Active Mode\n" 820 + ); 821 + break; 822 + default: 823 + dev_warn(&priv->spi->dev, "Wakeup reason unknown\n"); 824 + break; 825 + } 826 + complete(&priv->ca8210_is_awake); 827 + } 828 + 829 + finish:; 830 + } 831 + 832 + static int ca8210_remove(struct spi_device *spi_device); 833 + 834 + /** 835 + * ca8210_spi_transfer_complete() - Called when a single spi transfer has 836 + * completed 837 + * @context: Pointer to the cas_control object for the finished transfer 838 + */ 839 + static void ca8210_spi_transfer_complete(void *context) 840 + { 841 + struct cas_control *cas_ctl = context; 842 + struct ca8210_priv *priv = cas_ctl->priv; 843 + bool duplex_rx = false; 844 + int i; 845 + u8 retry_buffer[CA8210_SPI_BUF_SIZE]; 846 + 847 + if ( 848 + cas_ctl->tx_in_buf[0] == SPI_NACK || 849 + (cas_ctl->tx_in_buf[0] == SPI_IDLE && 850 + cas_ctl->tx_in_buf[1] == SPI_NACK) 851 + ) { 852 + /* ca8210 is busy */ 853 + dev_info(&priv->spi->dev, "ca8210 was busy during attempted write\n"); 854 + if (cas_ctl->tx_buf[0] == SPI_IDLE) { 855 + dev_warn( 856 + &priv->spi->dev, 857 + "IRQ servicing NACKd, dropping transfer\n" 858 + ); 859 + kfree(cas_ctl); 860 + return; 861 + } 862 + if (priv->retries > 3) { 863 + dev_err(&priv->spi->dev, "too many retries!\n"); 864 + kfree(cas_ctl); 865 + ca8210_remove(priv->spi); 866 + return; 867 + } 868 + memcpy(retry_buffer, cas_ctl->tx_buf, CA8210_SPI_BUF_SIZE); 869 + kfree(cas_ctl); 870 + ca8210_spi_transfer( 871 + priv->spi, 872 + retry_buffer, 873 + CA8210_SPI_BUF_SIZE 874 + ); 875 + priv->retries++; 876 + dev_info(&priv->spi->dev, "retried spi write\n"); 877 + return; 878 + } else if ( 879 + cas_ctl->tx_in_buf[0] != SPI_IDLE && 880 + cas_ctl->tx_in_buf[0] != SPI_NACK 881 + ) { 882 + duplex_rx = true; 883 + } 884 + 885 + if (duplex_rx) { 886 + dev_dbg(&priv->spi->dev, "READ CMD DURING TX\n"); 887 + for (i = 0; i < cas_ctl->tx_in_buf[1] + 2; i++) 888 + dev_dbg( 889 + &priv->spi->dev, 890 + "%#03x\n", 891 + cas_ctl->tx_in_buf[i] 892 + ); 893 + ca8210_rx_done(cas_ctl); 894 + } 895 + complete(&priv->spi_transfer_complete); 896 + kfree(cas_ctl); 897 + priv->retries = 0; 898 + } 899 + 900 + /** 901 + * ca8210_spi_transfer() - Initiate duplex spi transfer with ca8210 902 + * @spi: Pointer to spi device for transfer 903 + * @buf: Octet array to send 904 + * @len: length of the buffer being sent 905 + * 906 + * Return: 0 or linux error code 907 + */ 908 + static int ca8210_spi_transfer( 909 + struct spi_device *spi, 910 + const u8 *buf, 911 + size_t len 912 + ) 913 + { 914 + int i, status = 0; 915 + struct ca8210_priv *priv = spi_get_drvdata(spi); 916 + struct cas_control *cas_ctl; 917 + 918 + if (!spi) { 919 + dev_crit( 920 + &spi->dev, 921 + "NULL spi device passed to ca8210_spi_transfer\n" 922 + ); 923 + return -ENODEV; 924 + } 925 + 926 + reinit_completion(&priv->spi_transfer_complete); 927 + 928 + dev_dbg(&spi->dev, "ca8210_spi_transfer called\n"); 929 + 930 + cas_ctl = kmalloc(sizeof(*cas_ctl), GFP_ATOMIC); 931 + if (!cas_ctl) 932 + return -ENOMEM; 933 + 934 + cas_ctl->priv = priv; 935 + memset(cas_ctl->tx_buf, SPI_IDLE, CA8210_SPI_BUF_SIZE); 936 + memset(cas_ctl->tx_in_buf, SPI_IDLE, CA8210_SPI_BUF_SIZE); 937 + memcpy(cas_ctl->tx_buf, buf, len); 938 + 939 + for (i = 0; i < len; i++) 940 + dev_dbg(&spi->dev, "%#03x\n", cas_ctl->tx_buf[i]); 941 + 942 + spi_message_init(&cas_ctl->msg); 943 + 944 + cas_ctl->transfer.tx_nbits = 1; /* 1 MOSI line */ 945 + cas_ctl->transfer.rx_nbits = 1; /* 1 MISO line */ 946 + cas_ctl->transfer.speed_hz = 0; /* Use device setting */ 947 + cas_ctl->transfer.bits_per_word = 0; /* Use device setting */ 948 + cas_ctl->transfer.tx_buf = cas_ctl->tx_buf; 949 + cas_ctl->transfer.rx_buf = cas_ctl->tx_in_buf; 950 + cas_ctl->transfer.delay_usecs = 0; 951 + cas_ctl->transfer.cs_change = 0; 952 + cas_ctl->transfer.len = sizeof(struct mac_message); 953 + cas_ctl->msg.complete = ca8210_spi_transfer_complete; 954 + cas_ctl->msg.context = cas_ctl; 955 + 956 + spi_message_add_tail( 957 + &cas_ctl->transfer, 958 + &cas_ctl->msg 959 + ); 960 + 961 + status = spi_async(spi, &cas_ctl->msg); 962 + if (status < 0) { 963 + dev_crit( 964 + &spi->dev, 965 + "status %d from spi_sync in write\n", 966 + status 967 + ); 968 + } 969 + 970 + return status; 971 + } 972 + 973 + /** 974 + * ca8210_spi_exchange() - Exchange API/SAP commands with the radio 975 + * @buf: Octet array of command being sent downstream 976 + * @len: length of buf 977 + * @response: buffer for storing synchronous response 978 + * @device_ref: spi_device pointer for ca8210 979 + * 980 + * Effectively calls ca8210_spi_transfer to write buf[] to the spi, then for 981 + * synchronous commands waits for the corresponding response to be read from 982 + * the spi before returning. The response is written to the response parameter. 983 + * 984 + * Return: 0 or linux error code 985 + */ 986 + static int ca8210_spi_exchange( 987 + const u8 *buf, 988 + size_t len, 989 + u8 *response, 990 + void *device_ref 991 + ) 992 + { 993 + int status = 0; 994 + struct spi_device *spi = device_ref; 995 + struct ca8210_priv *priv = spi->dev.driver_data; 996 + long wait_remaining; 997 + 998 + if ((buf[0] & SPI_SYN) && response) { /* if sync wait for confirm */ 999 + reinit_completion(&priv->sync_exchange_complete); 1000 + priv->sync_command_response = response; 1001 + } 1002 + 1003 + do { 1004 + reinit_completion(&priv->spi_transfer_complete); 1005 + status = ca8210_spi_transfer(priv->spi, buf, len); 1006 + if (status) { 1007 + dev_warn( 1008 + &spi->dev, 1009 + "spi write failed, returned %d\n", 1010 + status 1011 + ); 1012 + if (status == -EBUSY) 1013 + continue; 1014 + if (((buf[0] & SPI_SYN) && response)) 1015 + complete(&priv->sync_exchange_complete); 1016 + goto cleanup; 1017 + } 1018 + 1019 + wait_remaining = wait_for_completion_interruptible_timeout( 1020 + &priv->spi_transfer_complete, 1021 + msecs_to_jiffies(1000) 1022 + ); 1023 + if (wait_remaining == -ERESTARTSYS) { 1024 + status = -ERESTARTSYS; 1025 + } else if (wait_remaining == 0) { 1026 + dev_err( 1027 + &spi->dev, 1028 + "SPI downstream transfer timed out!\n" 1029 + ); 1030 + status = -ETIME; 1031 + goto cleanup; 1032 + } 1033 + } while (status < 0); 1034 + 1035 + if (!((buf[0] & SPI_SYN) && response)) 1036 + goto cleanup; 1037 + 1038 + wait_remaining = wait_for_completion_interruptible_timeout( 1039 + &priv->sync_exchange_complete, 1040 + msecs_to_jiffies(CA8210_SYNC_TIMEOUT) 1041 + ); 1042 + if (wait_remaining == -ERESTARTSYS) { 1043 + status = -ERESTARTSYS; 1044 + } else if (wait_remaining == 0) { 1045 + dev_err( 1046 + &spi->dev, 1047 + "Synchronous confirm timeout\n" 1048 + ); 1049 + status = -ETIME; 1050 + } 1051 + 1052 + cleanup: 1053 + priv->sync_command_response = NULL; 1054 + return status; 1055 + } 1056 + 1057 + /** 1058 + * ca8210_interrupt_handler() - Called when an irq is received from the ca8210 1059 + * @irq: Id of the irq being handled 1060 + * @dev_id: Pointer passed by the system, pointing to the ca8210's private data 1061 + * 1062 + * This function is called when the irq line from the ca8210 is asserted, 1063 + * signifying that the ca8210 has a message to send upstream to us. Starts the 1064 + * asynchronous spi read. 1065 + * 1066 + * Return: irq return code 1067 + */ 1068 + static irqreturn_t ca8210_interrupt_handler(int irq, void *dev_id) 1069 + { 1070 + struct ca8210_priv *priv = dev_id; 1071 + int status; 1072 + 1073 + dev_dbg(&priv->spi->dev, "irq: Interrupt occurred\n"); 1074 + do { 1075 + status = ca8210_spi_transfer(priv->spi, NULL, 0); 1076 + if (status && (status != -EBUSY)) { 1077 + dev_warn( 1078 + &priv->spi->dev, 1079 + "spi read failed, returned %d\n", 1080 + status 1081 + ); 1082 + } 1083 + } while (status == -EBUSY); 1084 + return IRQ_HANDLED; 1085 + } 1086 + 1087 + static int (*cascoda_api_downstream)( 1088 + const u8 *buf, 1089 + size_t len, 1090 + u8 *response, 1091 + void *device_ref 1092 + ) = ca8210_spi_exchange; 1093 + 1094 + /* Cascoda API / 15.4 SAP Primitives */ 1095 + 1096 + /** 1097 + * tdme_setsfr_request_sync() - TDME_SETSFR_request/confirm according to API 1098 + * @sfr_page: SFR Page 1099 + * @sfr_address: SFR Address 1100 + * @sfr_value: SFR Value 1101 + * @device_ref: Nondescript pointer to target device 1102 + * 1103 + * Return: 802.15.4 status code of TDME-SETSFR.confirm 1104 + */ 1105 + static u8 tdme_setsfr_request_sync( 1106 + u8 sfr_page, 1107 + u8 sfr_address, 1108 + u8 sfr_value, 1109 + void *device_ref 1110 + ) 1111 + { 1112 + int ret; 1113 + struct mac_message command, response; 1114 + struct spi_device *spi = device_ref; 1115 + 1116 + command.command_id = SPI_TDME_SETSFR_REQUEST; 1117 + command.length = 3; 1118 + command.pdata.tdme_set_sfr_req.sfr_page = sfr_page; 1119 + command.pdata.tdme_set_sfr_req.sfr_address = sfr_address; 1120 + command.pdata.tdme_set_sfr_req.sfr_value = sfr_value; 1121 + response.command_id = SPI_IDLE; 1122 + ret = cascoda_api_downstream( 1123 + &command.command_id, 1124 + command.length + 2, 1125 + &response.command_id, 1126 + device_ref 1127 + ); 1128 + if (ret) { 1129 + dev_crit(&spi->dev, "cascoda_api_downstream returned %d", ret); 1130 + return MAC_SYSTEM_ERROR; 1131 + } 1132 + 1133 + if (response.command_id != SPI_TDME_SETSFR_CONFIRM) { 1134 + dev_crit( 1135 + &spi->dev, 1136 + "sync response to SPI_TDME_SETSFR_REQUEST was not SPI_TDME_SETSFR_CONFIRM, it was %d\n", 1137 + response.command_id 1138 + ); 1139 + return MAC_SYSTEM_ERROR; 1140 + } 1141 + 1142 + return response.pdata.tdme_set_sfr_cnf.status; 1143 + } 1144 + 1145 + /** 1146 + * tdme_chipinit() - TDME Chip Register Default Initialisation Macro 1147 + * @device_ref: Nondescript pointer to target device 1148 + * 1149 + * Return: 802.15.4 status code of API calls 1150 + */ 1151 + static u8 tdme_chipinit(void *device_ref) 1152 + { 1153 + u8 status = MAC_SUCCESS; 1154 + u8 sfr_address; 1155 + struct spi_device *spi = device_ref; 1156 + struct preamble_cfg_sfr pre_cfg_value = { 1157 + .timeout_symbols = 3, 1158 + .acquisition_symbols = 3, 1159 + .search_symbols = 1, 1160 + }; 1161 + /* LNA Gain Settings */ 1162 + status = tdme_setsfr_request_sync( 1163 + 1, (sfr_address = CA8210_SFR_LNAGX40), 1164 + LNAGX40_DEFAULT_GAIN, device_ref); 1165 + if (status) 1166 + goto finish; 1167 + status = tdme_setsfr_request_sync( 1168 + 1, (sfr_address = CA8210_SFR_LNAGX41), 1169 + LNAGX41_DEFAULT_GAIN, device_ref); 1170 + if (status) 1171 + goto finish; 1172 + status = tdme_setsfr_request_sync( 1173 + 1, (sfr_address = CA8210_SFR_LNAGX42), 1174 + LNAGX42_DEFAULT_GAIN, device_ref); 1175 + if (status) 1176 + goto finish; 1177 + status = tdme_setsfr_request_sync( 1178 + 1, (sfr_address = CA8210_SFR_LNAGX43), 1179 + LNAGX43_DEFAULT_GAIN, device_ref); 1180 + if (status) 1181 + goto finish; 1182 + status = tdme_setsfr_request_sync( 1183 + 1, (sfr_address = CA8210_SFR_LNAGX44), 1184 + LNAGX44_DEFAULT_GAIN, device_ref); 1185 + if (status) 1186 + goto finish; 1187 + status = tdme_setsfr_request_sync( 1188 + 1, (sfr_address = CA8210_SFR_LNAGX45), 1189 + LNAGX45_DEFAULT_GAIN, device_ref); 1190 + if (status) 1191 + goto finish; 1192 + status = tdme_setsfr_request_sync( 1193 + 1, (sfr_address = CA8210_SFR_LNAGX46), 1194 + LNAGX46_DEFAULT_GAIN, device_ref); 1195 + if (status) 1196 + goto finish; 1197 + status = tdme_setsfr_request_sync( 1198 + 1, (sfr_address = CA8210_SFR_LNAGX47), 1199 + LNAGX47_DEFAULT_GAIN, device_ref); 1200 + if (status) 1201 + goto finish; 1202 + /* Preamble Timing Config */ 1203 + status = tdme_setsfr_request_sync( 1204 + 1, (sfr_address = CA8210_SFR_PRECFG), 1205 + *((u8 *)&pre_cfg_value), device_ref); 1206 + if (status) 1207 + goto finish; 1208 + /* Preamble Threshold High */ 1209 + status = tdme_setsfr_request_sync( 1210 + 1, (sfr_address = CA8210_SFR_PTHRH), 1211 + PTHRH_DEFAULT_THRESHOLD, device_ref); 1212 + if (status) 1213 + goto finish; 1214 + /* Tx Output Power 8 dBm */ 1215 + status = tdme_setsfr_request_sync( 1216 + 0, (sfr_address = CA8210_SFR_PACFGIB), 1217 + PACFGIB_DEFAULT_CURRENT, device_ref); 1218 + if (status) 1219 + goto finish; 1220 + 1221 + finish: 1222 + if (status != MAC_SUCCESS) { 1223 + dev_err( 1224 + &spi->dev, 1225 + "failed to set sfr at %#03x, status = %#03x\n", 1226 + sfr_address, 1227 + status 1228 + ); 1229 + } 1230 + return status; 1231 + } 1232 + 1233 + /** 1234 + * tdme_channelinit() - TDME Channel Register Default Initialisation Macro (Tx) 1235 + * @channel: 802.15.4 channel to initialise chip for 1236 + * @device_ref: Nondescript pointer to target device 1237 + * 1238 + * Return: 802.15.4 status code of API calls 1239 + */ 1240 + static u8 tdme_channelinit(u8 channel, void *device_ref) 1241 + { 1242 + /* Transceiver front-end local oscillator tx two-point calibration 1243 + * value. Tuned for the hardware. 1244 + */ 1245 + u8 txcalval; 1246 + 1247 + if (channel >= 25) 1248 + txcalval = 0xA7; 1249 + else if (channel >= 23) 1250 + txcalval = 0xA8; 1251 + else if (channel >= 22) 1252 + txcalval = 0xA9; 1253 + else if (channel >= 20) 1254 + txcalval = 0xAA; 1255 + else if (channel >= 17) 1256 + txcalval = 0xAB; 1257 + else if (channel >= 16) 1258 + txcalval = 0xAC; 1259 + else if (channel >= 14) 1260 + txcalval = 0xAD; 1261 + else if (channel >= 12) 1262 + txcalval = 0xAE; 1263 + else 1264 + txcalval = 0xAF; 1265 + 1266 + return tdme_setsfr_request_sync( 1267 + 1, 1268 + CA8210_SFR_LOTXCAL, 1269 + txcalval, 1270 + device_ref 1271 + ); /* LO Tx Cal */ 1272 + } 1273 + 1274 + /** 1275 + * tdme_checkpibattribute() - Checks Attribute Values that are not checked in 1276 + * MAC 1277 + * @pib_attribute: Attribute Number 1278 + * @pib_attribute_length: Attribute length 1279 + * @pib_attribute_value: Pointer to Attribute Value 1280 + * @device_ref: Nondescript pointer to target device 1281 + * 1282 + * Return: 802.15.4 status code of checks 1283 + */ 1284 + static u8 tdme_checkpibattribute( 1285 + u8 pib_attribute, 1286 + u8 pib_attribute_length, 1287 + const void *pib_attribute_value 1288 + ) 1289 + { 1290 + u8 status = MAC_SUCCESS; 1291 + u8 value; 1292 + 1293 + value = *((u8 *)pib_attribute_value); 1294 + 1295 + switch (pib_attribute) { 1296 + /* PHY */ 1297 + case PHY_TRANSMIT_POWER: 1298 + if (value > 0x3F) 1299 + status = MAC_INVALID_PARAMETER; 1300 + break; 1301 + case PHY_CCA_MODE: 1302 + if (value > 0x03) 1303 + status = MAC_INVALID_PARAMETER; 1304 + break; 1305 + /* MAC */ 1306 + case MAC_BATT_LIFE_EXT_PERIODS: 1307 + if ((value < 6) || (value > 41)) 1308 + status = MAC_INVALID_PARAMETER; 1309 + break; 1310 + case MAC_BEACON_PAYLOAD: 1311 + if (pib_attribute_length > MAX_BEACON_PAYLOAD_LENGTH) 1312 + status = MAC_INVALID_PARAMETER; 1313 + break; 1314 + case MAC_BEACON_PAYLOAD_LENGTH: 1315 + if (value > MAX_BEACON_PAYLOAD_LENGTH) 1316 + status = MAC_INVALID_PARAMETER; 1317 + break; 1318 + case MAC_BEACON_ORDER: 1319 + if (value > 15) 1320 + status = MAC_INVALID_PARAMETER; 1321 + break; 1322 + case MAC_MAX_BE: 1323 + if ((value < 3) || (value > 8)) 1324 + status = MAC_INVALID_PARAMETER; 1325 + break; 1326 + case MAC_MAX_CSMA_BACKOFFS: 1327 + if (value > 5) 1328 + status = MAC_INVALID_PARAMETER; 1329 + break; 1330 + case MAC_MAX_FRAME_RETRIES: 1331 + if (value > 7) 1332 + status = MAC_INVALID_PARAMETER; 1333 + break; 1334 + case MAC_MIN_BE: 1335 + if (value > 8) 1336 + status = MAC_INVALID_PARAMETER; 1337 + break; 1338 + case MAC_RESPONSE_WAIT_TIME: 1339 + if ((value < 2) || (value > 64)) 1340 + status = MAC_INVALID_PARAMETER; 1341 + break; 1342 + case MAC_SUPERFRAME_ORDER: 1343 + if (value > 15) 1344 + status = MAC_INVALID_PARAMETER; 1345 + break; 1346 + /* boolean */ 1347 + case MAC_ASSOCIATED_PAN_COORD: 1348 + case MAC_ASSOCIATION_PERMIT: 1349 + case MAC_AUTO_REQUEST: 1350 + case MAC_BATT_LIFE_EXT: 1351 + case MAC_GTS_PERMIT: 1352 + case MAC_PROMISCUOUS_MODE: 1353 + case MAC_RX_ON_WHEN_IDLE: 1354 + case MAC_SECURITY_ENABLED: 1355 + if (value > 1) 1356 + status = MAC_INVALID_PARAMETER; 1357 + break; 1358 + /* MAC SEC */ 1359 + case MAC_AUTO_REQUEST_SECURITY_LEVEL: 1360 + if (value > 7) 1361 + status = MAC_INVALID_PARAMETER; 1362 + break; 1363 + case MAC_AUTO_REQUEST_KEY_ID_MODE: 1364 + if (value > 3) 1365 + status = MAC_INVALID_PARAMETER; 1366 + break; 1367 + default: 1368 + break; 1369 + } 1370 + 1371 + return status; 1372 + } 1373 + 1374 + /** 1375 + * tdme_settxpower() - Sets the tx power for MLME_SET phyTransmitPower 1376 + * @txp: Transmit Power 1377 + * @device_ref: Nondescript pointer to target device 1378 + * 1379 + * Normalised to 802.15.4 Definition (6-bit, signed): 1380 + * Bit 7-6: not used 1381 + * Bit 5-0: tx power (-32 - +31 dB) 1382 + * 1383 + * Return: 802.15.4 status code of api calls 1384 + */ 1385 + static u8 tdme_settxpower(u8 txp, void *device_ref) 1386 + { 1387 + u8 status; 1388 + s8 txp_val; 1389 + u8 txp_ext; 1390 + union pa_cfg_sfr pa_cfg_val; 1391 + 1392 + /* extend from 6 to 8 bit */ 1393 + txp_ext = 0x3F & txp; 1394 + if (txp_ext & 0x20) 1395 + txp_ext += 0xC0; 1396 + txp_val = (s8)txp_ext; 1397 + 1398 + if (CA8210_MAC_MPW) { 1399 + if (txp_val > 0) { 1400 + /* 8 dBm: ptrim = 5, itrim = +3 => +4 dBm */ 1401 + pa_cfg_val.bias_current_trim = 3; 1402 + pa_cfg_val.buffer_capacitor_trim = 5; 1403 + pa_cfg_val.boost = 1; 1404 + } else { 1405 + /* 0 dBm: ptrim = 7, itrim = +3 => -6 dBm */ 1406 + pa_cfg_val.bias_current_trim = 3; 1407 + pa_cfg_val.buffer_capacitor_trim = 7; 1408 + pa_cfg_val.boost = 0; 1409 + } 1410 + /* write PACFG */ 1411 + status = tdme_setsfr_request_sync( 1412 + 0, 1413 + CA8210_SFR_PACFG, 1414 + pa_cfg_val.paib, 1415 + device_ref 1416 + ); 1417 + } else { 1418 + /* Look-Up Table for Setting Current and Frequency Trim values 1419 + * for desired Output Power 1420 + */ 1421 + if (txp_val > 8) { 1422 + pa_cfg_val.paib = 0x3F; 1423 + } else if (txp_val == 8) { 1424 + pa_cfg_val.paib = 0x32; 1425 + } else if (txp_val == 7) { 1426 + pa_cfg_val.paib = 0x22; 1427 + } else if (txp_val == 6) { 1428 + pa_cfg_val.paib = 0x18; 1429 + } else if (txp_val == 5) { 1430 + pa_cfg_val.paib = 0x10; 1431 + } else if (txp_val == 4) { 1432 + pa_cfg_val.paib = 0x0C; 1433 + } else if (txp_val == 3) { 1434 + pa_cfg_val.paib = 0x08; 1435 + } else if (txp_val == 2) { 1436 + pa_cfg_val.paib = 0x05; 1437 + } else if (txp_val == 1) { 1438 + pa_cfg_val.paib = 0x03; 1439 + } else if (txp_val == 0) { 1440 + pa_cfg_val.paib = 0x01; 1441 + } else { /* < 0 */ 1442 + pa_cfg_val.paib = 0x00; 1443 + } 1444 + /* write PACFGIB */ 1445 + status = tdme_setsfr_request_sync( 1446 + 0, 1447 + CA8210_SFR_PACFGIB, 1448 + pa_cfg_val.paib, 1449 + device_ref 1450 + ); 1451 + } 1452 + 1453 + return status; 1454 + } 1455 + 1456 + /** 1457 + * mcps_data_request() - mcps_data_request (Send Data) according to API Spec 1458 + * @src_addr_mode: Source Addressing Mode 1459 + * @dst_address_mode: Destination Addressing Mode 1460 + * @dst_pan_id: Destination PAN ID 1461 + * @dst_addr: Pointer to Destination Address 1462 + * @msdu_length: length of Data 1463 + * @msdu: Pointer to Data 1464 + * @msdu_handle: Handle of Data 1465 + * @tx_options: Tx Options Bit Field 1466 + * @security: Pointer to Security Structure or NULL 1467 + * @device_ref: Nondescript pointer to target device 1468 + * 1469 + * Return: 802.15.4 status code of action 1470 + */ 1471 + static u8 mcps_data_request( 1472 + u8 src_addr_mode, 1473 + u8 dst_address_mode, 1474 + u16 dst_pan_id, 1475 + union macaddr *dst_addr, 1476 + u8 msdu_length, 1477 + u8 *msdu, 1478 + u8 msdu_handle, 1479 + u8 tx_options, 1480 + struct secspec *security, 1481 + void *device_ref 1482 + ) 1483 + { 1484 + struct secspec *psec; 1485 + struct mac_message command; 1486 + 1487 + command.command_id = SPI_MCPS_DATA_REQUEST; 1488 + command.pdata.data_req.src_addr_mode = src_addr_mode; 1489 + command.pdata.data_req.dst.mode = dst_address_mode; 1490 + if (dst_address_mode != MAC_MODE_NO_ADDR) { 1491 + command.pdata.data_req.dst.pan_id[0] = LS_BYTE(dst_pan_id); 1492 + command.pdata.data_req.dst.pan_id[1] = MS_BYTE(dst_pan_id); 1493 + if (dst_address_mode == MAC_MODE_SHORT_ADDR) { 1494 + command.pdata.data_req.dst.address[0] = LS_BYTE( 1495 + dst_addr->short_address 1496 + ); 1497 + command.pdata.data_req.dst.address[1] = MS_BYTE( 1498 + dst_addr->short_address 1499 + ); 1500 + } else { /* MAC_MODE_LONG_ADDR*/ 1501 + memcpy( 1502 + command.pdata.data_req.dst.address, 1503 + dst_addr->ieee_address, 1504 + 8 1505 + ); 1506 + } 1507 + } 1508 + command.pdata.data_req.msdu_length = msdu_length; 1509 + command.pdata.data_req.msdu_handle = msdu_handle; 1510 + command.pdata.data_req.tx_options = tx_options; 1511 + memcpy(command.pdata.data_req.msdu, msdu, msdu_length); 1512 + psec = (struct secspec *)(command.pdata.data_req.msdu + msdu_length); 1513 + command.length = sizeof(struct mcps_data_request_pset) - 1514 + MAX_DATA_SIZE + msdu_length; 1515 + if (!security || (security->security_level == 0)) { 1516 + psec->security_level = 0; 1517 + command.length += 1; 1518 + } else { 1519 + *psec = *security; 1520 + command.length += sizeof(struct secspec); 1521 + } 1522 + 1523 + if (ca8210_spi_transfer(device_ref, &command.command_id, 1524 + command.length + 2)) 1525 + return MAC_SYSTEM_ERROR; 1526 + 1527 + return MAC_SUCCESS; 1528 + } 1529 + 1530 + /** 1531 + * mlme_reset_request_sync() - MLME_RESET_request/confirm according to API Spec 1532 + * @set_default_pib: Set defaults in PIB 1533 + * @device_ref: Nondescript pointer to target device 1534 + * 1535 + * Return: 802.15.4 status code of MLME-RESET.confirm 1536 + */ 1537 + static u8 mlme_reset_request_sync( 1538 + u8 set_default_pib, 1539 + void *device_ref 1540 + ) 1541 + { 1542 + u8 status; 1543 + struct mac_message command, response; 1544 + struct spi_device *spi = device_ref; 1545 + 1546 + command.command_id = SPI_MLME_RESET_REQUEST; 1547 + command.length = 1; 1548 + command.pdata.u8param = set_default_pib; 1549 + 1550 + if (cascoda_api_downstream( 1551 + &command.command_id, 1552 + command.length + 2, 1553 + &response.command_id, 1554 + device_ref)) { 1555 + dev_err(&spi->dev, "cascoda_api_downstream failed\n"); 1556 + return MAC_SYSTEM_ERROR; 1557 + } 1558 + 1559 + if (response.command_id != SPI_MLME_RESET_CONFIRM) 1560 + return MAC_SYSTEM_ERROR; 1561 + 1562 + status = response.pdata.status; 1563 + 1564 + /* reset COORD Bit for Channel Filtering as Coordinator */ 1565 + if (CA8210_MAC_WORKAROUNDS && set_default_pib && (!status)) { 1566 + status = tdme_setsfr_request_sync( 1567 + 0, 1568 + CA8210_SFR_MACCON, 1569 + 0, 1570 + device_ref 1571 + ); 1572 + } 1573 + 1574 + return status; 1575 + } 1576 + 1577 + /** 1578 + * mlme_set_request_sync() - MLME_SET_request/confirm according to API Spec 1579 + * @pib_attribute: Attribute Number 1580 + * @pib_attribute_index: Index within Attribute if an Array 1581 + * @pib_attribute_length: Attribute length 1582 + * @pib_attribute_value: Pointer to Attribute Value 1583 + * @device_ref: Nondescript pointer to target device 1584 + * 1585 + * Return: 802.15.4 status code of MLME-SET.confirm 1586 + */ 1587 + static u8 mlme_set_request_sync( 1588 + u8 pib_attribute, 1589 + u8 pib_attribute_index, 1590 + u8 pib_attribute_length, 1591 + const void *pib_attribute_value, 1592 + void *device_ref 1593 + ) 1594 + { 1595 + u8 status; 1596 + struct mac_message command, response; 1597 + 1598 + /* pre-check the validity of pib_attribute values that are not checked 1599 + * in MAC 1600 + */ 1601 + if (tdme_checkpibattribute( 1602 + pib_attribute, pib_attribute_length, pib_attribute_value)) { 1603 + return MAC_INVALID_PARAMETER; 1604 + } 1605 + 1606 + if (pib_attribute == PHY_CURRENT_CHANNEL) { 1607 + status = tdme_channelinit( 1608 + *((u8 *)pib_attribute_value), 1609 + device_ref 1610 + ); 1611 + if (status) 1612 + return status; 1613 + } 1614 + 1615 + if (pib_attribute == PHY_TRANSMIT_POWER) { 1616 + return tdme_settxpower( 1617 + *((u8 *)pib_attribute_value), 1618 + device_ref 1619 + ); 1620 + } 1621 + 1622 + command.command_id = SPI_MLME_SET_REQUEST; 1623 + command.length = sizeof(struct mlme_set_request_pset) - 1624 + MAX_ATTRIBUTE_SIZE + pib_attribute_length; 1625 + command.pdata.set_req.pib_attribute = pib_attribute; 1626 + command.pdata.set_req.pib_attribute_index = pib_attribute_index; 1627 + command.pdata.set_req.pib_attribute_length = pib_attribute_length; 1628 + memcpy( 1629 + command.pdata.set_req.pib_attribute_value, 1630 + pib_attribute_value, 1631 + pib_attribute_length 1632 + ); 1633 + 1634 + if (cascoda_api_downstream( 1635 + &command.command_id, 1636 + command.length + 2, 1637 + &response.command_id, 1638 + device_ref)) { 1639 + return MAC_SYSTEM_ERROR; 1640 + } 1641 + 1642 + if (response.command_id != SPI_MLME_SET_CONFIRM) 1643 + return MAC_SYSTEM_ERROR; 1644 + 1645 + return response.pdata.status; 1646 + } 1647 + 1648 + /** 1649 + * hwme_set_request_sync() - HWME_SET_request/confirm according to API Spec 1650 + * @hw_attribute: Attribute Number 1651 + * @hw_attribute_length: Attribute length 1652 + * @hw_attribute_value: Pointer to Attribute Value 1653 + * @device_ref: Nondescript pointer to target device 1654 + * 1655 + * Return: 802.15.4 status code of HWME-SET.confirm 1656 + */ 1657 + static u8 hwme_set_request_sync( 1658 + u8 hw_attribute, 1659 + u8 hw_attribute_length, 1660 + u8 *hw_attribute_value, 1661 + void *device_ref 1662 + ) 1663 + { 1664 + struct mac_message command, response; 1665 + 1666 + command.command_id = SPI_HWME_SET_REQUEST; 1667 + command.length = 2 + hw_attribute_length; 1668 + command.pdata.hwme_set_req.hw_attribute = hw_attribute; 1669 + command.pdata.hwme_set_req.hw_attribute_length = hw_attribute_length; 1670 + memcpy( 1671 + command.pdata.hwme_set_req.hw_attribute_value, 1672 + hw_attribute_value, 1673 + hw_attribute_length 1674 + ); 1675 + 1676 + if (cascoda_api_downstream( 1677 + &command.command_id, 1678 + command.length + 2, 1679 + &response.command_id, 1680 + device_ref)) { 1681 + return MAC_SYSTEM_ERROR; 1682 + } 1683 + 1684 + if (response.command_id != SPI_HWME_SET_CONFIRM) 1685 + return MAC_SYSTEM_ERROR; 1686 + 1687 + return response.pdata.hwme_set_cnf.status; 1688 + } 1689 + 1690 + /** 1691 + * hwme_get_request_sync() - HWME_GET_request/confirm according to API Spec 1692 + * @hw_attribute: Attribute Number 1693 + * @hw_attribute_length: Attribute length 1694 + * @hw_attribute_value: Pointer to Attribute Value 1695 + * @device_ref: Nondescript pointer to target device 1696 + * 1697 + * Return: 802.15.4 status code of HWME-GET.confirm 1698 + */ 1699 + static u8 hwme_get_request_sync( 1700 + u8 hw_attribute, 1701 + u8 *hw_attribute_length, 1702 + u8 *hw_attribute_value, 1703 + void *device_ref 1704 + ) 1705 + { 1706 + struct mac_message command, response; 1707 + 1708 + command.command_id = SPI_HWME_GET_REQUEST; 1709 + command.length = 1; 1710 + command.pdata.hwme_get_req.hw_attribute = hw_attribute; 1711 + 1712 + if (cascoda_api_downstream( 1713 + &command.command_id, 1714 + command.length + 2, 1715 + &response.command_id, 1716 + device_ref)) { 1717 + return MAC_SYSTEM_ERROR; 1718 + } 1719 + 1720 + if (response.command_id != SPI_HWME_GET_CONFIRM) 1721 + return MAC_SYSTEM_ERROR; 1722 + 1723 + if (response.pdata.hwme_get_cnf.status == MAC_SUCCESS) { 1724 + *hw_attribute_length = 1725 + response.pdata.hwme_get_cnf.hw_attribute_length; 1726 + memcpy( 1727 + hw_attribute_value, 1728 + response.pdata.hwme_get_cnf.hw_attribute_value, 1729 + *hw_attribute_length 1730 + ); 1731 + } 1732 + 1733 + return response.pdata.hwme_get_cnf.status; 1734 + } 1735 + 1736 + /* Network driver operation */ 1737 + 1738 + /** 1739 + * ca8210_async_xmit_complete() - Called to announce that an asynchronous 1740 + * transmission has finished 1741 + * @hw: ieee802154_hw of ca8210 that has finished exchange 1742 + * @msduhandle: Identifier of transmission that has completed 1743 + * @status: Returned 802.15.4 status code of the transmission 1744 + * 1745 + * Return: 0 or linux error code 1746 + */ 1747 + static int ca8210_async_xmit_complete( 1748 + struct ieee802154_hw *hw, 1749 + u8 msduhandle, 1750 + u8 status) 1751 + { 1752 + struct ca8210_priv *priv = hw->priv; 1753 + 1754 + if (priv->nextmsduhandle != msduhandle) { 1755 + dev_err( 1756 + &priv->spi->dev, 1757 + "Unexpected msdu_handle on data confirm, Expected %d, got %d\n", 1758 + priv->nextmsduhandle, 1759 + msduhandle 1760 + ); 1761 + return -EIO; 1762 + } 1763 + 1764 + priv->async_tx_pending = false; 1765 + priv->nextmsduhandle++; 1766 + 1767 + if (status) { 1768 + dev_err( 1769 + &priv->spi->dev, 1770 + "Link transmission unsuccessful, status = %d\n", 1771 + status 1772 + ); 1773 + if (status != MAC_TRANSACTION_OVERFLOW) { 1774 + ieee802154_wake_queue(priv->hw); 1775 + return 0; 1776 + } 1777 + } 1778 + ieee802154_xmit_complete(priv->hw, priv->tx_skb, true); 1779 + 1780 + return 0; 1781 + } 1782 + 1783 + /** 1784 + * ca8210_skb_rx() - Contructs a properly framed socket buffer from a received 1785 + * MCPS_DATA_indication 1786 + * @hw: ieee802154_hw that MCPS_DATA_indication was received by 1787 + * @len: length of MCPS_DATA_indication 1788 + * @data_ind: Octet array of MCPS_DATA_indication 1789 + * 1790 + * Called by the spi driver whenever a SAP command is received, this function 1791 + * will ascertain whether the command is of interest to the network driver and 1792 + * take necessary action. 1793 + * 1794 + * Return: 0 or linux error code 1795 + */ 1796 + static int ca8210_skb_rx( 1797 + struct ieee802154_hw *hw, 1798 + size_t len, 1799 + u8 *data_ind 1800 + ) 1801 + { 1802 + struct ieee802154_hdr hdr; 1803 + int msdulen; 1804 + int hlen; 1805 + u8 mpdulinkquality = data_ind[23]; 1806 + struct sk_buff *skb; 1807 + struct ca8210_priv *priv = hw->priv; 1808 + 1809 + /* Allocate mtu size buffer for every rx packet */ 1810 + skb = dev_alloc_skb(IEEE802154_MTU + sizeof(hdr)); 1811 + if (!skb) { 1812 + dev_crit(&priv->spi->dev, "dev_alloc_skb failed\n"); 1813 + return -ENOMEM; 1814 + } 1815 + skb_reserve(skb, sizeof(hdr)); 1816 + 1817 + msdulen = data_ind[22]; /* msdu_length */ 1818 + if (msdulen > IEEE802154_MTU) { 1819 + dev_err( 1820 + &priv->spi->dev, 1821 + "received erroneously large msdu length!\n" 1822 + ); 1823 + kfree_skb(skb); 1824 + return -EMSGSIZE; 1825 + } 1826 + dev_dbg(&priv->spi->dev, "skb buffer length = %d\n", msdulen); 1827 + 1828 + if (priv->promiscuous) 1829 + goto copy_payload; 1830 + 1831 + /* Populate hdr */ 1832 + hdr.sec.level = data_ind[29 + msdulen]; 1833 + dev_dbg(&priv->spi->dev, "security level: %#03x\n", hdr.sec.level); 1834 + if (hdr.sec.level > 0) { 1835 + hdr.sec.key_id_mode = data_ind[30 + msdulen]; 1836 + memcpy(&hdr.sec.extended_src, &data_ind[31 + msdulen], 8); 1837 + hdr.sec.key_id = data_ind[39 + msdulen]; 1838 + } 1839 + hdr.source.mode = data_ind[0]; 1840 + dev_dbg(&priv->spi->dev, "srcAddrMode: %#03x\n", hdr.source.mode); 1841 + hdr.source.pan_id = *(u16 *)&data_ind[1]; 1842 + dev_dbg(&priv->spi->dev, "srcPanId: %#06x\n", hdr.source.pan_id); 1843 + memcpy(&hdr.source.extended_addr, &data_ind[3], 8); 1844 + hdr.dest.mode = data_ind[11]; 1845 + dev_dbg(&priv->spi->dev, "dstAddrMode: %#03x\n", hdr.dest.mode); 1846 + hdr.dest.pan_id = *(u16 *)&data_ind[12]; 1847 + dev_dbg(&priv->spi->dev, "dstPanId: %#06x\n", hdr.dest.pan_id); 1848 + memcpy(&hdr.dest.extended_addr, &data_ind[14], 8); 1849 + 1850 + /* Fill in FC implicitly */ 1851 + hdr.fc.type = 1; /* Data frame */ 1852 + if (hdr.sec.level) 1853 + hdr.fc.security_enabled = 1; 1854 + else 1855 + hdr.fc.security_enabled = 0; 1856 + if (data_ind[1] != data_ind[12] || data_ind[2] != data_ind[13]) 1857 + hdr.fc.intra_pan = 1; 1858 + else 1859 + hdr.fc.intra_pan = 0; 1860 + hdr.fc.dest_addr_mode = hdr.dest.mode; 1861 + hdr.fc.source_addr_mode = hdr.source.mode; 1862 + 1863 + /* Add hdr to front of buffer */ 1864 + hlen = ieee802154_hdr_push(skb, &hdr); 1865 + 1866 + if (hlen < 0) { 1867 + dev_crit(&priv->spi->dev, "failed to push mac hdr onto skb!\n"); 1868 + kfree_skb(skb); 1869 + return hlen; 1870 + } 1871 + 1872 + skb_reset_mac_header(skb); 1873 + skb->mac_len = hlen; 1874 + 1875 + copy_payload: 1876 + /* Add <msdulen> bytes of space to the back of the buffer */ 1877 + /* Copy msdu to skb */ 1878 + memcpy(skb_put(skb, msdulen), &data_ind[29], msdulen); 1879 + 1880 + ieee802154_rx_irqsafe(hw, skb, mpdulinkquality); 1881 + return 0; 1882 + } 1883 + 1884 + /** 1885 + * ca8210_net_rx() - Acts upon received SAP commands relevant to the network 1886 + * driver 1887 + * @hw: ieee802154_hw that command was received by 1888 + * @command: Octet array of received command 1889 + * @len: length of the received command 1890 + * 1891 + * Called by the spi driver whenever a SAP command is received, this function 1892 + * will ascertain whether the command is of interest to the network driver and 1893 + * take necessary action. 1894 + * 1895 + * Return: 0 or linux error code 1896 + */ 1897 + static int ca8210_net_rx(struct ieee802154_hw *hw, u8 *command, size_t len) 1898 + { 1899 + struct ca8210_priv *priv = hw->priv; 1900 + unsigned long flags; 1901 + u8 status; 1902 + 1903 + dev_dbg(&priv->spi->dev, "ca8210_net_rx(), CmdID = %d\n", command[0]); 1904 + 1905 + if (command[0] == SPI_MCPS_DATA_INDICATION) { 1906 + /* Received data */ 1907 + spin_lock_irqsave(&priv->lock, flags); 1908 + if (command[26] == priv->last_dsn) { 1909 + dev_dbg( 1910 + &priv->spi->dev, 1911 + "DSN %d resend received, ignoring...\n", 1912 + command[26] 1913 + ); 1914 + spin_unlock_irqrestore(&priv->lock, flags); 1915 + return 0; 1916 + } 1917 + priv->last_dsn = command[26]; 1918 + spin_unlock_irqrestore(&priv->lock, flags); 1919 + return ca8210_skb_rx(hw, len - 2, command + 2); 1920 + } else if (command[0] == SPI_MCPS_DATA_CONFIRM) { 1921 + status = command[3]; 1922 + if (priv->async_tx_pending) { 1923 + return ca8210_async_xmit_complete( 1924 + hw, 1925 + command[2], 1926 + status 1927 + ); 1928 + } 1929 + } 1930 + 1931 + return 0; 1932 + } 1933 + 1934 + /** 1935 + * ca8210_skb_tx() - Transmits a given socket buffer using the ca8210 1936 + * @skb: Socket buffer to transmit 1937 + * @msduhandle: Data identifier to pass to the 802.15.4 MAC 1938 + * @priv: Pointer to private data section of target ca8210 1939 + * 1940 + * Return: 0 or linux error code 1941 + */ 1942 + static int ca8210_skb_tx( 1943 + struct sk_buff *skb, 1944 + u8 msduhandle, 1945 + struct ca8210_priv *priv 1946 + ) 1947 + { 1948 + int status; 1949 + struct ieee802154_hdr header = { 0 }; 1950 + struct secspec secspec; 1951 + unsigned int mac_len; 1952 + 1953 + dev_dbg(&priv->spi->dev, "ca8210_skb_tx() called\n"); 1954 + 1955 + /* Get addressing info from skb - ieee802154 layer creates a full 1956 + * packet 1957 + */ 1958 + mac_len = ieee802154_hdr_peek_addrs(skb, &header); 1959 + 1960 + secspec.security_level = header.sec.level; 1961 + secspec.key_id_mode = header.sec.key_id_mode; 1962 + if (secspec.key_id_mode == 2) 1963 + memcpy(secspec.key_source, &header.sec.short_src, 4); 1964 + else if (secspec.key_id_mode == 3) 1965 + memcpy(secspec.key_source, &header.sec.extended_src, 8); 1966 + secspec.key_index = header.sec.key_id; 1967 + 1968 + /* Pass to Cascoda API */ 1969 + status = mcps_data_request( 1970 + header.source.mode, 1971 + header.dest.mode, 1972 + header.dest.pan_id, 1973 + (union macaddr *)&header.dest.extended_addr, 1974 + skb->len - mac_len, 1975 + &skb->data[mac_len], 1976 + msduhandle, 1977 + header.fc.ack_request, 1978 + &secspec, 1979 + priv->spi 1980 + ); 1981 + return link_to_linux_err(status); 1982 + } 1983 + 1984 + /** 1985 + * ca8210_start() - Starts the network driver 1986 + * @hw: ieee802154_hw of ca8210 being started 1987 + * 1988 + * Return: 0 or linux error code 1989 + */ 1990 + static int ca8210_start(struct ieee802154_hw *hw) 1991 + { 1992 + int status; 1993 + u8 rx_on_when_idle; 1994 + u8 lqi_threshold = 0; 1995 + struct ca8210_priv *priv = hw->priv; 1996 + 1997 + priv->last_dsn = -1; 1998 + /* Turn receiver on when idle for now just to test rx */ 1999 + rx_on_when_idle = 1; 2000 + status = mlme_set_request_sync( 2001 + MAC_RX_ON_WHEN_IDLE, 2002 + 0, 2003 + 1, 2004 + &rx_on_when_idle, 2005 + priv->spi 2006 + ); 2007 + if (status) { 2008 + dev_crit( 2009 + &priv->spi->dev, 2010 + "Setting rx_on_when_idle failed, status = %d\n", 2011 + status 2012 + ); 2013 + return link_to_linux_err(status); 2014 + } 2015 + status = hwme_set_request_sync( 2016 + HWME_LQILIMIT, 2017 + 1, 2018 + &lqi_threshold, 2019 + priv->spi 2020 + ); 2021 + if (status) { 2022 + dev_crit( 2023 + &priv->spi->dev, 2024 + "Setting lqilimit failed, status = %d\n", 2025 + status 2026 + ); 2027 + return link_to_linux_err(status); 2028 + } 2029 + 2030 + return 0; 2031 + } 2032 + 2033 + /** 2034 + * ca8210_stop() - Stops the network driver 2035 + * @hw: ieee802154_hw of ca8210 being stopped 2036 + * 2037 + * Return: 0 or linux error code 2038 + */ 2039 + static void ca8210_stop(struct ieee802154_hw *hw) 2040 + { 2041 + } 2042 + 2043 + /** 2044 + * ca8210_xmit_async() - Asynchronously transmits a given socket buffer using 2045 + * the ca8210 2046 + * @hw: ieee802154_hw of ca8210 to transmit from 2047 + * @skb: Socket buffer to transmit 2048 + * 2049 + * Return: 0 or linux error code 2050 + */ 2051 + static int ca8210_xmit_async(struct ieee802154_hw *hw, struct sk_buff *skb) 2052 + { 2053 + struct ca8210_priv *priv = hw->priv; 2054 + int status; 2055 + 2056 + dev_dbg(&priv->spi->dev, "calling ca8210_xmit_async()\n"); 2057 + 2058 + priv->tx_skb = skb; 2059 + priv->async_tx_pending = true; 2060 + status = ca8210_skb_tx(skb, priv->nextmsduhandle, priv); 2061 + return status; 2062 + } 2063 + 2064 + /** 2065 + * ca8210_get_ed() - Returns the measured energy on the current channel at this 2066 + * instant in time 2067 + * @hw: ieee802154_hw of target ca8210 2068 + * @level: Measured Energy Detect level 2069 + * 2070 + * Return: 0 or linux error code 2071 + */ 2072 + static int ca8210_get_ed(struct ieee802154_hw *hw, u8 *level) 2073 + { 2074 + u8 lenvar; 2075 + struct ca8210_priv *priv = hw->priv; 2076 + 2077 + return link_to_linux_err( 2078 + hwme_get_request_sync(HWME_EDVALUE, &lenvar, level, priv->spi) 2079 + ); 2080 + } 2081 + 2082 + /** 2083 + * ca8210_set_channel() - Sets the current operating 802.15.4 channel of the 2084 + * ca8210 2085 + * @hw: ieee802154_hw of target ca8210 2086 + * @page: Channel page to set 2087 + * @channel: Channel number to set 2088 + * 2089 + * Return: 0 or linux error code 2090 + */ 2091 + static int ca8210_set_channel( 2092 + struct ieee802154_hw *hw, 2093 + u8 page, 2094 + u8 channel 2095 + ) 2096 + { 2097 + u8 status; 2098 + struct ca8210_priv *priv = hw->priv; 2099 + 2100 + status = mlme_set_request_sync( 2101 + PHY_CURRENT_CHANNEL, 2102 + 0, 2103 + 1, 2104 + &channel, 2105 + priv->spi 2106 + ); 2107 + if (status) { 2108 + dev_err( 2109 + &priv->spi->dev, 2110 + "error setting channel, MLME-SET.confirm status = %d\n", 2111 + status 2112 + ); 2113 + } 2114 + return link_to_linux_err(status); 2115 + } 2116 + 2117 + /** 2118 + * ca8210_set_hw_addr_filt() - Sets the address filtering parameters of the 2119 + * ca8210 2120 + * @hw: ieee802154_hw of target ca8210 2121 + * @filt: Filtering parameters 2122 + * @changed: Bitmap representing which parameters to change 2123 + * 2124 + * Effectively just sets the actual addressing information identifying this node 2125 + * as all filtering is performed by the ca8210 as detailed in the IEEE 802.15.4 2126 + * 2006 specification. 2127 + * 2128 + * Return: 0 or linux error code 2129 + */ 2130 + static int ca8210_set_hw_addr_filt( 2131 + struct ieee802154_hw *hw, 2132 + struct ieee802154_hw_addr_filt *filt, 2133 + unsigned long changed 2134 + ) 2135 + { 2136 + u8 status = 0; 2137 + struct ca8210_priv *priv = hw->priv; 2138 + 2139 + if (changed & IEEE802154_AFILT_PANID_CHANGED) { 2140 + status = mlme_set_request_sync( 2141 + MAC_PAN_ID, 2142 + 0, 2143 + 2, 2144 + &filt->pan_id, priv->spi 2145 + ); 2146 + if (status) { 2147 + dev_err( 2148 + &priv->spi->dev, 2149 + "error setting pan id, MLME-SET.confirm status = %d", 2150 + status 2151 + ); 2152 + return link_to_linux_err(status); 2153 + } 2154 + } 2155 + if (changed & IEEE802154_AFILT_SADDR_CHANGED) { 2156 + status = mlme_set_request_sync( 2157 + MAC_SHORT_ADDRESS, 2158 + 0, 2159 + 2, 2160 + &filt->short_addr, priv->spi 2161 + ); 2162 + if (status) { 2163 + dev_err( 2164 + &priv->spi->dev, 2165 + "error setting short address, MLME-SET.confirm status = %d", 2166 + status 2167 + ); 2168 + return link_to_linux_err(status); 2169 + } 2170 + } 2171 + if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) { 2172 + status = mlme_set_request_sync( 2173 + NS_IEEE_ADDRESS, 2174 + 0, 2175 + 8, 2176 + &filt->ieee_addr, 2177 + priv->spi 2178 + ); 2179 + if (status) { 2180 + dev_err( 2181 + &priv->spi->dev, 2182 + "error setting ieee address, MLME-SET.confirm status = %d", 2183 + status 2184 + ); 2185 + return link_to_linux_err(status); 2186 + } 2187 + } 2188 + /* TODO: Should use MLME_START to set coord bit? */ 2189 + return 0; 2190 + } 2191 + 2192 + /** 2193 + * ca8210_set_tx_power() - Sets the transmit power of the ca8210 2194 + * @hw: ieee802154_hw of target ca8210 2195 + * @mbm: Transmit power in mBm (dBm*100) 2196 + * 2197 + * Return: 0 or linux error code 2198 + */ 2199 + static int ca8210_set_tx_power(struct ieee802154_hw *hw, s32 mbm) 2200 + { 2201 + struct ca8210_priv *priv = hw->priv; 2202 + 2203 + mbm /= 100; 2204 + return link_to_linux_err( 2205 + mlme_set_request_sync(PHY_TRANSMIT_POWER, 0, 1, &mbm, priv->spi) 2206 + ); 2207 + } 2208 + 2209 + /** 2210 + * ca8210_set_cca_mode() - Sets the clear channel assessment mode of the ca8210 2211 + * @hw: ieee802154_hw of target ca8210 2212 + * @cca: CCA mode to set 2213 + * 2214 + * Return: 0 or linux error code 2215 + */ 2216 + static int ca8210_set_cca_mode( 2217 + struct ieee802154_hw *hw, 2218 + const struct wpan_phy_cca *cca 2219 + ) 2220 + { 2221 + u8 status; 2222 + u8 cca_mode; 2223 + struct ca8210_priv *priv = hw->priv; 2224 + 2225 + cca_mode = cca->mode & 3; 2226 + if (cca_mode == 3 && cca->opt == NL802154_CCA_OPT_ENERGY_CARRIER_OR) { 2227 + /* cca_mode 0 == CS OR ED, 3 == CS AND ED */ 2228 + cca_mode = 0; 2229 + } 2230 + status = mlme_set_request_sync( 2231 + PHY_CCA_MODE, 2232 + 0, 2233 + 1, 2234 + &cca_mode, 2235 + priv->spi 2236 + ); 2237 + if (status) { 2238 + dev_err( 2239 + &priv->spi->dev, 2240 + "error setting cca mode, MLME-SET.confirm status = %d", 2241 + status 2242 + ); 2243 + } 2244 + return link_to_linux_err(status); 2245 + } 2246 + 2247 + /** 2248 + * ca8210_set_cca_ed_level() - Sets the CCA ED level of the ca8210 2249 + * @hw: ieee802154_hw of target ca8210 2250 + * @level: ED level to set (in mbm) 2251 + * 2252 + * Sets the minimum threshold of measured energy above which the ca8210 will 2253 + * back off and retry a transmission. 2254 + * 2255 + * Return: 0 or linux error code 2256 + */ 2257 + static int ca8210_set_cca_ed_level(struct ieee802154_hw *hw, s32 level) 2258 + { 2259 + u8 status; 2260 + u8 ed_threshold = (level / 100) * 2 + 256; 2261 + struct ca8210_priv *priv = hw->priv; 2262 + 2263 + status = hwme_set_request_sync( 2264 + HWME_EDTHRESHOLD, 2265 + 1, 2266 + &ed_threshold, 2267 + priv->spi 2268 + ); 2269 + if (status) { 2270 + dev_err( 2271 + &priv->spi->dev, 2272 + "error setting ed threshold, HWME-SET.confirm status = %d", 2273 + status 2274 + ); 2275 + } 2276 + return link_to_linux_err(status); 2277 + } 2278 + 2279 + /** 2280 + * ca8210_set_csma_params() - Sets the CSMA parameters of the ca8210 2281 + * @hw: ieee802154_hw of target ca8210 2282 + * @min_be: Minimum backoff exponent when backing off a transmission 2283 + * @max_be: Maximum backoff exponent when backing off a transmission 2284 + * @retries: Number of times to retry after backing off 2285 + * 2286 + * Return: 0 or linux error code 2287 + */ 2288 + static int ca8210_set_csma_params( 2289 + struct ieee802154_hw *hw, 2290 + u8 min_be, 2291 + u8 max_be, 2292 + u8 retries 2293 + ) 2294 + { 2295 + u8 status; 2296 + struct ca8210_priv *priv = hw->priv; 2297 + 2298 + status = mlme_set_request_sync(MAC_MIN_BE, 0, 1, &min_be, priv->spi); 2299 + if (status) { 2300 + dev_err( 2301 + &priv->spi->dev, 2302 + "error setting min be, MLME-SET.confirm status = %d", 2303 + status 2304 + ); 2305 + return link_to_linux_err(status); 2306 + } 2307 + status = mlme_set_request_sync(MAC_MAX_BE, 0, 1, &max_be, priv->spi); 2308 + if (status) { 2309 + dev_err( 2310 + &priv->spi->dev, 2311 + "error setting max be, MLME-SET.confirm status = %d", 2312 + status 2313 + ); 2314 + return link_to_linux_err(status); 2315 + } 2316 + status = mlme_set_request_sync( 2317 + MAC_MAX_CSMA_BACKOFFS, 2318 + 0, 2319 + 1, 2320 + &retries, 2321 + priv->spi 2322 + ); 2323 + if (status) { 2324 + dev_err( 2325 + &priv->spi->dev, 2326 + "error setting max csma backoffs, MLME-SET.confirm status = %d", 2327 + status 2328 + ); 2329 + } 2330 + return link_to_linux_err(status); 2331 + } 2332 + 2333 + /** 2334 + * ca8210_set_frame_retries() - Sets the maximum frame retries of the ca8210 2335 + * @hw: ieee802154_hw of target ca8210 2336 + * @retries: Number of retries 2337 + * 2338 + * Sets the number of times to retry a transmission if no acknowledgment was 2339 + * was received from the other end when one was requested. 2340 + * 2341 + * Return: 0 or linux error code 2342 + */ 2343 + static int ca8210_set_frame_retries(struct ieee802154_hw *hw, s8 retries) 2344 + { 2345 + u8 status; 2346 + struct ca8210_priv *priv = hw->priv; 2347 + 2348 + status = mlme_set_request_sync( 2349 + MAC_MAX_FRAME_RETRIES, 2350 + 0, 2351 + 1, 2352 + &retries, 2353 + priv->spi 2354 + ); 2355 + if (status) { 2356 + dev_err( 2357 + &priv->spi->dev, 2358 + "error setting frame retries, MLME-SET.confirm status = %d", 2359 + status 2360 + ); 2361 + } 2362 + return link_to_linux_err(status); 2363 + } 2364 + 2365 + static int ca8210_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on) 2366 + { 2367 + u8 status; 2368 + struct ca8210_priv *priv = hw->priv; 2369 + 2370 + status = mlme_set_request_sync( 2371 + MAC_PROMISCUOUS_MODE, 2372 + 0, 2373 + 1, 2374 + (const void*)&on, 2375 + priv->spi 2376 + ); 2377 + if (status) { 2378 + dev_err( 2379 + &priv->spi->dev, 2380 + "error setting promiscuous mode, MLME-SET.confirm status = %d", 2381 + status 2382 + ); 2383 + } else { 2384 + priv->promiscuous = on; 2385 + } 2386 + return link_to_linux_err(status); 2387 + } 2388 + 2389 + static const struct ieee802154_ops ca8210_phy_ops = { 2390 + .start = ca8210_start, 2391 + .stop = ca8210_stop, 2392 + .xmit_async = ca8210_xmit_async, 2393 + .ed = ca8210_get_ed, 2394 + .set_channel = ca8210_set_channel, 2395 + .set_hw_addr_filt = ca8210_set_hw_addr_filt, 2396 + .set_txpower = ca8210_set_tx_power, 2397 + .set_cca_mode = ca8210_set_cca_mode, 2398 + .set_cca_ed_level = ca8210_set_cca_ed_level, 2399 + .set_csma_params = ca8210_set_csma_params, 2400 + .set_frame_retries = ca8210_set_frame_retries, 2401 + .set_promiscuous_mode = ca8210_set_promiscuous_mode 2402 + }; 2403 + 2404 + /* Test/EVBME Interface */ 2405 + 2406 + /** 2407 + * ca8210_test_int_open() - Opens the test interface to the userspace 2408 + * @inodp: inode representation of file interface 2409 + * @filp: file interface 2410 + * 2411 + * Return: 0 or linux error code 2412 + */ 2413 + static int ca8210_test_int_open(struct inode *inodp, struct file *filp) 2414 + { 2415 + struct ca8210_priv *priv = inodp->i_private; 2416 + 2417 + filp->private_data = priv; 2418 + return 0; 2419 + } 2420 + 2421 + /** 2422 + * ca8210_test_check_upstream() - Checks a command received from the upstream 2423 + * testing interface for required action 2424 + * @buf: Buffer containing command to check 2425 + * @device_ref: Nondescript pointer to target device 2426 + * 2427 + * Return: 0 or linux error code 2428 + */ 2429 + static int ca8210_test_check_upstream(u8 *buf, void *device_ref) 2430 + { 2431 + int ret; 2432 + u8 response[CA8210_SPI_BUF_SIZE]; 2433 + 2434 + if (buf[0] == SPI_MLME_SET_REQUEST) { 2435 + ret = tdme_checkpibattribute(buf[2], buf[4], buf + 5); 2436 + if (ret) { 2437 + response[0] = SPI_MLME_SET_CONFIRM; 2438 + response[1] = 3; 2439 + response[2] = MAC_INVALID_PARAMETER; 2440 + response[3] = buf[2]; 2441 + response[4] = buf[3]; 2442 + if (cascoda_api_upstream) 2443 + cascoda_api_upstream(response, 5, device_ref); 2444 + return ret; 2445 + } 2446 + } 2447 + if (buf[0] == SPI_MLME_ASSOCIATE_REQUEST) { 2448 + return tdme_channelinit(buf[2], device_ref); 2449 + } else if (buf[0] == SPI_MLME_START_REQUEST) { 2450 + return tdme_channelinit(buf[4], device_ref); 2451 + } else if ( 2452 + (buf[0] == SPI_MLME_SET_REQUEST) && 2453 + (buf[2] == PHY_CURRENT_CHANNEL) 2454 + ) { 2455 + return tdme_channelinit(buf[5], device_ref); 2456 + } else if ( 2457 + (buf[0] == SPI_TDME_SET_REQUEST) && 2458 + (buf[2] == TDME_CHANNEL) 2459 + ) { 2460 + return tdme_channelinit(buf[4], device_ref); 2461 + } else if ( 2462 + (CA8210_MAC_WORKAROUNDS) && 2463 + (buf[0] == SPI_MLME_RESET_REQUEST) && 2464 + (buf[2] == 1) 2465 + ) { 2466 + /* reset COORD Bit for Channel Filtering as Coordinator */ 2467 + return tdme_setsfr_request_sync( 2468 + 0, 2469 + CA8210_SFR_MACCON, 2470 + 0, 2471 + device_ref 2472 + ); 2473 + } 2474 + return 0; 2475 + } /* End of EVBMECheckSerialCommand() */ 2476 + 2477 + /** 2478 + * ca8210_test_int_user_write() - Called by a process in userspace to send a 2479 + * message to the ca8210 drivers 2480 + * @filp: file interface 2481 + * @in_buf: Buffer containing message to write 2482 + * @len: length of message 2483 + * @off: file offset 2484 + * 2485 + * Return: 0 or linux error code 2486 + */ 2487 + static ssize_t ca8210_test_int_user_write( 2488 + struct file *filp, 2489 + const char __user *in_buf, 2490 + size_t len, 2491 + loff_t *off 2492 + ) 2493 + { 2494 + int ret; 2495 + struct ca8210_priv *priv = filp->private_data; 2496 + u8 command[CA8210_SPI_BUF_SIZE]; 2497 + 2498 + if (len > CA8210_SPI_BUF_SIZE) { 2499 + dev_warn( 2500 + &priv->spi->dev, 2501 + "userspace requested erroneously long write (%zu)\n", 2502 + len 2503 + ); 2504 + return -EMSGSIZE; 2505 + } 2506 + 2507 + ret = copy_from_user(command, in_buf, len); 2508 + if (ret) { 2509 + dev_err( 2510 + &priv->spi->dev, 2511 + "%d bytes could not be copied from userspace\n", 2512 + ret 2513 + ); 2514 + return -EIO; 2515 + } 2516 + 2517 + ret = ca8210_test_check_upstream(command, priv->spi); 2518 + if (ret == 0) { 2519 + ret = ca8210_spi_exchange( 2520 + command, 2521 + command[1] + 2, 2522 + NULL, 2523 + priv->spi 2524 + ); 2525 + if (ret < 0) { 2526 + /* effectively 0 bytes were written successfully */ 2527 + dev_err( 2528 + &priv->spi->dev, 2529 + "spi exchange failed\n" 2530 + ); 2531 + return ret; 2532 + } 2533 + if (command[0] & SPI_SYN) 2534 + priv->sync_down++; 2535 + } 2536 + 2537 + return len; 2538 + } 2539 + 2540 + /** 2541 + * ca8210_test_int_user_read() - Called by a process in userspace to read a 2542 + * message from the ca8210 drivers 2543 + * @filp: file interface 2544 + * @buf: Buffer to write message to 2545 + * @len: length of message to read (ignored) 2546 + * @offp: file offset 2547 + * 2548 + * If the O_NONBLOCK flag was set when opening the file then this function will 2549 + * not block, i.e. it will return if the fifo is empty. Otherwise the function 2550 + * will block, i.e. wait until new data arrives. 2551 + * 2552 + * Return: number of bytes read 2553 + */ 2554 + static ssize_t ca8210_test_int_user_read( 2555 + struct file *filp, 2556 + char __user *buf, 2557 + size_t len, 2558 + loff_t *offp 2559 + ) 2560 + { 2561 + int i, cmdlen; 2562 + struct ca8210_priv *priv = filp->private_data; 2563 + unsigned char *fifo_buffer; 2564 + unsigned long bytes_not_copied; 2565 + 2566 + if (filp->f_flags & O_NONBLOCK) { 2567 + /* Non-blocking mode */ 2568 + if (kfifo_is_empty(&priv->test.up_fifo)) 2569 + return 0; 2570 + } else { 2571 + /* Blocking mode */ 2572 + wait_event_interruptible( 2573 + priv->test.readq, 2574 + !kfifo_is_empty(&priv->test.up_fifo) 2575 + ); 2576 + } 2577 + 2578 + if (kfifo_out(&priv->test.up_fifo, &fifo_buffer, 4) != 4) { 2579 + dev_err( 2580 + &priv->spi->dev, 2581 + "test_interface: Wrong number of elements popped from upstream fifo\n" 2582 + ); 2583 + return 0; 2584 + } 2585 + cmdlen = fifo_buffer[1]; 2586 + bytes_not_copied = cmdlen + 2; 2587 + 2588 + bytes_not_copied = copy_to_user(buf, fifo_buffer, bytes_not_copied); 2589 + if (bytes_not_copied > 0) { 2590 + dev_err( 2591 + &priv->spi->dev, 2592 + "%lu bytes could not be copied to user space!\n", 2593 + bytes_not_copied 2594 + ); 2595 + } 2596 + 2597 + dev_dbg(&priv->spi->dev, "test_interface: Cmd len = %d\n", cmdlen); 2598 + 2599 + dev_dbg(&priv->spi->dev, "test_interface: Read\n"); 2600 + for (i = 0; i < cmdlen + 2; i++) 2601 + dev_dbg(&priv->spi->dev, "%#03x\n", fifo_buffer[i]); 2602 + 2603 + kfree(fifo_buffer); 2604 + 2605 + return cmdlen + 2; 2606 + } 2607 + 2608 + /** 2609 + * ca8210_test_int_ioctl() - Called by a process in userspace to enact an 2610 + * arbitrary action 2611 + * @filp: file interface 2612 + * @ioctl_num: which action to enact 2613 + * @ioctl_param: arbitrary parameter for the action 2614 + * 2615 + * Return: status 2616 + */ 2617 + static long ca8210_test_int_ioctl( 2618 + struct file *filp, 2619 + unsigned int ioctl_num, 2620 + unsigned long ioctl_param 2621 + ) 2622 + { 2623 + struct ca8210_priv *priv = filp->private_data; 2624 + 2625 + switch (ioctl_num) { 2626 + case CA8210_IOCTL_HARD_RESET: 2627 + ca8210_reset_send(priv->spi, ioctl_param); 2628 + break; 2629 + default: 2630 + break; 2631 + } 2632 + return 0; 2633 + } 2634 + 2635 + /** 2636 + * ca8210_test_int_poll() - Called by a process in userspace to determine which 2637 + * actions are currently possible for the file 2638 + * @filp: file interface 2639 + * @ptable: poll table 2640 + * 2641 + * Return: set of poll return flags 2642 + */ 2643 + static unsigned int ca8210_test_int_poll( 2644 + struct file *filp, 2645 + struct poll_table_struct *ptable 2646 + ) 2647 + { 2648 + unsigned int return_flags = 0; 2649 + struct ca8210_priv *priv = filp->private_data; 2650 + 2651 + poll_wait(filp, &priv->test.readq, ptable); 2652 + if (!kfifo_is_empty(&priv->test.up_fifo)) 2653 + return_flags |= (POLLIN | POLLRDNORM); 2654 + if (wait_event_interruptible( 2655 + priv->test.readq, 2656 + !kfifo_is_empty(&priv->test.up_fifo))) { 2657 + return POLLERR; 2658 + } 2659 + return return_flags; 2660 + } 2661 + 2662 + static const struct file_operations test_int_fops = { 2663 + .read = ca8210_test_int_user_read, 2664 + .write = ca8210_test_int_user_write, 2665 + .open = ca8210_test_int_open, 2666 + .release = NULL, 2667 + .unlocked_ioctl = ca8210_test_int_ioctl, 2668 + .poll = ca8210_test_int_poll 2669 + }; 2670 + 2671 + /* Init/Deinit */ 2672 + 2673 + /** 2674 + * ca8210_get_platform_data() - Populate a ca8210_platform_data object 2675 + * @spi_device: Pointer to ca8210 spi device object to get data for 2676 + * @pdata: Pointer to ca8210_platform_data object to populate 2677 + * 2678 + * Return: 0 or linux error code 2679 + */ 2680 + static int ca8210_get_platform_data( 2681 + struct spi_device *spi_device, 2682 + struct ca8210_platform_data *pdata 2683 + ) 2684 + { 2685 + int ret = 0; 2686 + 2687 + if (!spi_device->dev.of_node) 2688 + return -EINVAL; 2689 + 2690 + pdata->extclockenable = of_property_read_bool( 2691 + spi_device->dev.of_node, 2692 + "extclock-enable" 2693 + ); 2694 + if (pdata->extclockenable) { 2695 + ret = of_property_read_u32( 2696 + spi_device->dev.of_node, 2697 + "extclock-freq", 2698 + &pdata->extclockfreq 2699 + ); 2700 + if (ret < 0) 2701 + return ret; 2702 + 2703 + ret = of_property_read_u32( 2704 + spi_device->dev.of_node, 2705 + "extclock-gpio", 2706 + &pdata->extclockgpio 2707 + ); 2708 + } 2709 + 2710 + return ret; 2711 + } 2712 + 2713 + /** 2714 + * ca8210_config_extern_clk() - Configure the external clock provided by the 2715 + * ca8210 2716 + * @pdata: Pointer to ca8210_platform_data containing clock parameters 2717 + * @spi: Pointer to target ca8210 spi device 2718 + * @on: True to turn the clock on, false to turn off 2719 + * 2720 + * The external clock is configured with a frequency and output pin taken from 2721 + * the platform data. 2722 + * 2723 + * Return: 0 or linux error code 2724 + */ 2725 + static int ca8210_config_extern_clk( 2726 + struct ca8210_platform_data *pdata, 2727 + struct spi_device *spi, 2728 + bool on 2729 + ) 2730 + { 2731 + u8 clkparam[2]; 2732 + 2733 + if (on) { 2734 + dev_info(&spi->dev, "Switching external clock on\n"); 2735 + switch (pdata->extclockfreq) { 2736 + case SIXTEEN_MHZ: 2737 + clkparam[0] = 1; 2738 + break; 2739 + case EIGHT_MHZ: 2740 + clkparam[0] = 2; 2741 + break; 2742 + case FOUR_MHZ: 2743 + clkparam[0] = 3; 2744 + break; 2745 + case TWO_MHZ: 2746 + clkparam[0] = 4; 2747 + break; 2748 + case ONE_MHZ: 2749 + clkparam[0] = 5; 2750 + break; 2751 + default: 2752 + dev_crit(&spi->dev, "Invalid extclock-freq\n"); 2753 + return -EINVAL; 2754 + } 2755 + clkparam[1] = pdata->extclockgpio; 2756 + } else { 2757 + dev_info(&spi->dev, "Switching external clock off\n"); 2758 + clkparam[0] = 0; /* off */ 2759 + clkparam[1] = 0; 2760 + } 2761 + return link_to_linux_err( 2762 + hwme_set_request_sync(HWME_SYSCLKOUT, 2, clkparam, spi) 2763 + ); 2764 + } 2765 + 2766 + /** 2767 + * ca8210_register_ext_clock() - Register ca8210's external clock with kernel 2768 + * @spi: Pointer to target ca8210 spi device 2769 + * 2770 + * Return: 0 or linux error code 2771 + */ 2772 + static int ca8210_register_ext_clock(struct spi_device *spi) 2773 + { 2774 + struct device_node *np = spi->dev.of_node; 2775 + struct ca8210_priv *priv = spi_get_drvdata(spi); 2776 + struct ca8210_platform_data *pdata = spi->dev.platform_data; 2777 + int ret = 0; 2778 + 2779 + if (!np) 2780 + return -EFAULT; 2781 + 2782 + priv->clk = clk_register_fixed_rate( 2783 + &spi->dev, 2784 + np->name, 2785 + NULL, 2786 + 0, 2787 + pdata->extclockfreq 2788 + ); 2789 + 2790 + if (IS_ERR(priv->clk)) { 2791 + dev_crit(&spi->dev, "Failed to register external clk\n"); 2792 + return PTR_ERR(priv->clk); 2793 + } 2794 + ret = of_clk_add_provider(np, of_clk_src_simple_get, priv->clk); 2795 + if (ret) { 2796 + clk_unregister(priv->clk); 2797 + dev_crit( 2798 + &spi->dev, 2799 + "Failed to register external clock as clock provider\n" 2800 + ); 2801 + } else { 2802 + dev_info(&spi->dev, "External clock set as clock provider\n"); 2803 + } 2804 + 2805 + return ret; 2806 + } 2807 + 2808 + /** 2809 + * ca8210_unregister_ext_clock() - Unregister ca8210's external clock with 2810 + * kernel 2811 + * @spi: Pointer to target ca8210 spi device 2812 + */ 2813 + static void ca8210_unregister_ext_clock(struct spi_device *spi) 2814 + { 2815 + struct ca8210_priv *priv = spi_get_drvdata(spi); 2816 + 2817 + if (!priv->clk) 2818 + return 2819 + 2820 + of_clk_del_provider(spi->dev.of_node); 2821 + clk_unregister(priv->clk); 2822 + dev_info(&spi->dev, "External clock unregistered\n"); 2823 + } 2824 + 2825 + /** 2826 + * ca8210_reset_init() - Initialise the reset input to the ca8210 2827 + * @spi: Pointer to target ca8210 spi device 2828 + * 2829 + * Return: 0 or linux error code 2830 + */ 2831 + static int ca8210_reset_init(struct spi_device *spi) 2832 + { 2833 + int ret; 2834 + struct ca8210_platform_data *pdata = spi->dev.platform_data; 2835 + 2836 + pdata->gpio_reset = of_get_named_gpio( 2837 + spi->dev.of_node, 2838 + "reset-gpio", 2839 + 0 2840 + ); 2841 + 2842 + ret = gpio_direction_output(pdata->gpio_reset, 1); 2843 + if (ret < 0) { 2844 + dev_crit( 2845 + &spi->dev, 2846 + "Reset GPIO %d did not set to output mode\n", 2847 + pdata->gpio_reset 2848 + ); 2849 + } 2850 + 2851 + return ret; 2852 + } 2853 + 2854 + /** 2855 + * ca8210_interrupt_init() - Initialise the irq output from the ca8210 2856 + * @spi: Pointer to target ca8210 spi device 2857 + * 2858 + * Return: 0 or linux error code 2859 + */ 2860 + static int ca8210_interrupt_init(struct spi_device *spi) 2861 + { 2862 + int ret; 2863 + struct ca8210_platform_data *pdata = spi->dev.platform_data; 2864 + 2865 + pdata->gpio_irq = of_get_named_gpio( 2866 + spi->dev.of_node, 2867 + "irq-gpio", 2868 + 0 2869 + ); 2870 + 2871 + pdata->irq_id = gpio_to_irq(pdata->gpio_irq); 2872 + if (pdata->irq_id < 0) { 2873 + dev_crit( 2874 + &spi->dev, 2875 + "Could not get irq for gpio pin %d\n", 2876 + pdata->gpio_irq 2877 + ); 2878 + gpio_free(pdata->gpio_irq); 2879 + return pdata->irq_id; 2880 + } 2881 + 2882 + ret = request_irq( 2883 + pdata->irq_id, 2884 + ca8210_interrupt_handler, 2885 + IRQF_TRIGGER_FALLING, 2886 + "ca8210-irq", 2887 + spi_get_drvdata(spi) 2888 + ); 2889 + if (ret) { 2890 + dev_crit(&spi->dev, "request_irq %d failed\n", pdata->irq_id); 2891 + gpio_unexport(pdata->gpio_irq); 2892 + gpio_free(pdata->gpio_irq); 2893 + } 2894 + 2895 + return ret; 2896 + } 2897 + 2898 + /** 2899 + * ca8210_dev_com_init() - Initialise the spi communication component 2900 + * @priv: Pointer to private data structure 2901 + * 2902 + * Return: 0 or linux error code 2903 + */ 2904 + static int ca8210_dev_com_init(struct ca8210_priv *priv) 2905 + { 2906 + priv->mlme_workqueue = alloc_ordered_workqueue( 2907 + "MLME work queue", 2908 + WQ_UNBOUND 2909 + ); 2910 + if (!priv->mlme_workqueue) { 2911 + dev_crit(&priv->spi->dev, "alloc of mlme_workqueue failed!\n"); 2912 + return -ENOMEM; 2913 + } 2914 + 2915 + priv->irq_workqueue = alloc_ordered_workqueue( 2916 + "ca8210 irq worker", 2917 + WQ_UNBOUND 2918 + ); 2919 + if (!priv->irq_workqueue) { 2920 + dev_crit(&priv->spi->dev, "alloc of irq_workqueue failed!\n"); 2921 + return -ENOMEM; 2922 + } 2923 + 2924 + return 0; 2925 + } 2926 + 2927 + /** 2928 + * ca8210_dev_com_clear() - Deinitialise the spi communication component 2929 + * @priv: Pointer to private data structure 2930 + */ 2931 + static void ca8210_dev_com_clear(struct ca8210_priv *priv) 2932 + { 2933 + flush_workqueue(priv->mlme_workqueue); 2934 + destroy_workqueue(priv->mlme_workqueue); 2935 + flush_workqueue(priv->irq_workqueue); 2936 + destroy_workqueue(priv->irq_workqueue); 2937 + } 2938 + 2939 + #define CA8210_MAX_TX_POWERS (9) 2940 + static const s32 ca8210_tx_powers[CA8210_MAX_TX_POWERS] = { 2941 + 800, 700, 600, 500, 400, 300, 200, 100, 0 2942 + }; 2943 + 2944 + #define CA8210_MAX_ED_LEVELS (21) 2945 + static const s32 ca8210_ed_levels[CA8210_MAX_ED_LEVELS] = { 2946 + -10300, -10250, -10200, -10150, -10100, -10050, -10000, -9950, -9900, 2947 + -9850, -9800, -9750, -9700, -9650, -9600, -9550, -9500, -9450, -9400, 2948 + -9350, -9300 2949 + }; 2950 + 2951 + /** 2952 + * ca8210_hw_setup() - Populate the ieee802154_hw phy attributes with the 2953 + * ca8210's defaults 2954 + * @ca8210_hw: Pointer to ieee802154_hw to populate 2955 + */ 2956 + static void ca8210_hw_setup(struct ieee802154_hw *ca8210_hw) 2957 + { 2958 + /* Support channels 11-26 */ 2959 + ca8210_hw->phy->supported.channels[0] = CA8210_VALID_CHANNELS; 2960 + ca8210_hw->phy->supported.tx_powers_size = CA8210_MAX_TX_POWERS; 2961 + ca8210_hw->phy->supported.tx_powers = ca8210_tx_powers; 2962 + ca8210_hw->phy->supported.cca_ed_levels_size = CA8210_MAX_ED_LEVELS; 2963 + ca8210_hw->phy->supported.cca_ed_levels = ca8210_ed_levels; 2964 + ca8210_hw->phy->current_channel = 18; 2965 + ca8210_hw->phy->current_page = 0; 2966 + ca8210_hw->phy->transmit_power = 800; 2967 + ca8210_hw->phy->cca.mode = NL802154_CCA_ENERGY_CARRIER; 2968 + ca8210_hw->phy->cca.opt = NL802154_CCA_OPT_ENERGY_CARRIER_AND; 2969 + ca8210_hw->phy->cca_ed_level = -9800; 2970 + ca8210_hw->phy->symbol_duration = 16; 2971 + ca8210_hw->phy->lifs_period = 40; 2972 + ca8210_hw->phy->sifs_period = 12; 2973 + ca8210_hw->flags = 2974 + IEEE802154_HW_AFILT | 2975 + IEEE802154_HW_OMIT_CKSUM | 2976 + IEEE802154_HW_FRAME_RETRIES | 2977 + IEEE802154_HW_PROMISCUOUS | 2978 + IEEE802154_HW_CSMA_PARAMS; 2979 + ca8210_hw->phy->flags = 2980 + WPAN_PHY_FLAG_TXPOWER | 2981 + WPAN_PHY_FLAG_CCA_ED_LEVEL | 2982 + WPAN_PHY_FLAG_CCA_MODE; 2983 + } 2984 + 2985 + /** 2986 + * ca8210_test_interface_init() - Initialise the test file interface 2987 + * @priv: Pointer to private data structure 2988 + * 2989 + * Provided as an alternative to the standard linux network interface, the test 2990 + * interface exposes a file in the filesystem (ca8210_test) that allows 2991 + * 802.15.4 SAP Commands and Cascoda EVBME commands to be sent directly to 2992 + * the stack. 2993 + * 2994 + * Return: 0 or linux error code 2995 + */ 2996 + static int ca8210_test_interface_init(struct ca8210_priv *priv) 2997 + { 2998 + struct ca8210_test *test = &priv->test; 2999 + char node_name[32]; 3000 + 3001 + snprintf( 3002 + node_name, 3003 + sizeof(node_name), 3004 + "ca8210@%d_%d", 3005 + priv->spi->master->bus_num, 3006 + priv->spi->chip_select 3007 + ); 3008 + 3009 + test->ca8210_dfs_spi_int = debugfs_create_file( 3010 + node_name, 3011 + 0600, /* S_IRUSR | S_IWUSR */ 3012 + NULL, 3013 + priv, 3014 + &test_int_fops 3015 + ); 3016 + if (IS_ERR(test->ca8210_dfs_spi_int)) { 3017 + dev_err( 3018 + &priv->spi->dev, 3019 + "Error %ld when creating debugfs node\n", 3020 + PTR_ERR(test->ca8210_dfs_spi_int) 3021 + ); 3022 + return PTR_ERR(test->ca8210_dfs_spi_int); 3023 + } 3024 + debugfs_create_symlink("ca8210", NULL, node_name); 3025 + init_waitqueue_head(&test->readq); 3026 + return kfifo_alloc( 3027 + &test->up_fifo, 3028 + CA8210_TEST_INT_FIFO_SIZE, 3029 + GFP_KERNEL 3030 + ); 3031 + } 3032 + 3033 + /** 3034 + * ca8210_test_interface_clear() - Deinitialise the test file interface 3035 + * @priv: Pointer to private data structure 3036 + */ 3037 + static void ca8210_test_interface_clear(struct ca8210_priv *priv) 3038 + { 3039 + struct ca8210_test *test = &priv->test; 3040 + 3041 + if (!IS_ERR(test->ca8210_dfs_spi_int)) 3042 + debugfs_remove(test->ca8210_dfs_spi_int); 3043 + kfifo_free(&test->up_fifo); 3044 + dev_info(&priv->spi->dev, "Test interface removed\n"); 3045 + } 3046 + 3047 + /** 3048 + * ca8210_remove() - Shut down a ca8210 upon being disconnected 3049 + * @priv: Pointer to private data structure 3050 + * 3051 + * Return: 0 or linux error code 3052 + */ 3053 + static int ca8210_remove(struct spi_device *spi_device) 3054 + { 3055 + struct ca8210_priv *priv; 3056 + struct ca8210_platform_data *pdata; 3057 + 3058 + dev_info(&spi_device->dev, "Removing ca8210\n"); 3059 + 3060 + pdata = spi_device->dev.platform_data; 3061 + if (pdata) { 3062 + if (pdata->extclockenable) { 3063 + ca8210_unregister_ext_clock(spi_device); 3064 + ca8210_config_extern_clk(pdata, spi_device, 0); 3065 + } 3066 + free_irq(pdata->irq_id, spi_device->dev.driver_data); 3067 + kfree(pdata); 3068 + spi_device->dev.platform_data = NULL; 3069 + } 3070 + /* get spi_device private data */ 3071 + priv = spi_get_drvdata(spi_device); 3072 + if (priv) { 3073 + dev_info( 3074 + &spi_device->dev, 3075 + "sync_down = %d, sync_up = %d\n", 3076 + priv->sync_down, 3077 + priv->sync_up 3078 + ); 3079 + ca8210_dev_com_clear(spi_device->dev.driver_data); 3080 + if (priv->hw) { 3081 + if (priv->hw_registered) 3082 + ieee802154_unregister_hw(priv->hw); 3083 + ieee802154_free_hw(priv->hw); 3084 + priv->hw = NULL; 3085 + dev_info( 3086 + &spi_device->dev, 3087 + "Unregistered & freed ieee802154_hw.\n" 3088 + ); 3089 + } 3090 + if (IS_ENABLED(CONFIG_IEEE802154_CA8210_DEBUGFS)) 3091 + ca8210_test_interface_clear(priv); 3092 + } 3093 + 3094 + return 0; 3095 + } 3096 + 3097 + /** 3098 + * ca8210_probe() - Set up a connected ca8210 upon being detected by the system 3099 + * @priv: Pointer to private data structure 3100 + * 3101 + * Return: 0 or linux error code 3102 + */ 3103 + static int ca8210_probe(struct spi_device *spi_device) 3104 + { 3105 + struct ca8210_priv *priv; 3106 + struct ieee802154_hw *hw; 3107 + struct ca8210_platform_data *pdata; 3108 + int ret; 3109 + 3110 + dev_info(&spi_device->dev, "Inserting ca8210\n"); 3111 + 3112 + /* allocate ieee802154_hw and private data */ 3113 + hw = ieee802154_alloc_hw(sizeof(struct ca8210_priv), &ca8210_phy_ops); 3114 + if (!hw) { 3115 + dev_crit(&spi_device->dev, "ieee802154_alloc_hw failed\n"); 3116 + ret = -ENOMEM; 3117 + goto error; 3118 + } 3119 + 3120 + priv = hw->priv; 3121 + priv->hw = hw; 3122 + priv->spi = spi_device; 3123 + hw->parent = &spi_device->dev; 3124 + spin_lock_init(&priv->lock); 3125 + priv->async_tx_pending = false; 3126 + priv->hw_registered = false; 3127 + priv->sync_up = 0; 3128 + priv->sync_down = 0; 3129 + priv->promiscuous = false; 3130 + priv->retries = 0; 3131 + init_completion(&priv->ca8210_is_awake); 3132 + init_completion(&priv->spi_transfer_complete); 3133 + init_completion(&priv->sync_exchange_complete); 3134 + spi_set_drvdata(priv->spi, priv); 3135 + if (IS_ENABLED(CONFIG_IEEE802154_CA8210_DEBUGFS)) { 3136 + cascoda_api_upstream = ca8210_test_int_driver_write; 3137 + ca8210_test_interface_init(priv); 3138 + } else { 3139 + cascoda_api_upstream = NULL; 3140 + } 3141 + ca8210_hw_setup(hw); 3142 + ieee802154_random_extended_addr(&hw->phy->perm_extended_addr); 3143 + 3144 + pdata = kmalloc(sizeof(*pdata), GFP_KERNEL); 3145 + if (!pdata) { 3146 + dev_crit( 3147 + &spi_device->dev, 3148 + "Could not allocate platform data\n" 3149 + ); 3150 + ret = -ENOMEM; 3151 + goto error; 3152 + } 3153 + 3154 + ret = ca8210_get_platform_data(priv->spi, pdata); 3155 + if (ret) { 3156 + dev_crit(&spi_device->dev, "ca8210_get_platform_data failed\n"); 3157 + goto error; 3158 + } 3159 + priv->spi->dev.platform_data = pdata; 3160 + 3161 + ret = ca8210_dev_com_init(priv); 3162 + if (ret) { 3163 + dev_crit(&spi_device->dev, "ca8210_dev_com_init failed\n"); 3164 + goto error; 3165 + } 3166 + ret = ca8210_reset_init(priv->spi); 3167 + if (ret) { 3168 + dev_crit(&spi_device->dev, "ca8210_reset_init failed\n"); 3169 + goto error; 3170 + } 3171 + 3172 + ret = ca8210_interrupt_init(priv->spi); 3173 + if (ret) { 3174 + dev_crit(&spi_device->dev, "ca8210_interrupt_init failed\n"); 3175 + goto error; 3176 + } 3177 + 3178 + msleep(100); 3179 + 3180 + ca8210_reset_send(priv->spi, 1); 3181 + 3182 + ret = tdme_chipinit(priv->spi); 3183 + if (ret) { 3184 + dev_crit(&spi_device->dev, "tdme_chipinit failed\n"); 3185 + goto error; 3186 + } 3187 + 3188 + if (pdata->extclockenable) { 3189 + ret = ca8210_config_extern_clk(pdata, priv->spi, 1); 3190 + if (ret) { 3191 + dev_crit( 3192 + &spi_device->dev, 3193 + "ca8210_config_extern_clk failed\n" 3194 + ); 3195 + goto error; 3196 + } 3197 + ret = ca8210_register_ext_clock(priv->spi); 3198 + if (ret) { 3199 + dev_crit( 3200 + &spi_device->dev, 3201 + "ca8210_register_ext_clock failed\n" 3202 + ); 3203 + goto error; 3204 + } 3205 + } 3206 + 3207 + ret = ieee802154_register_hw(hw); 3208 + if (ret) { 3209 + dev_crit(&spi_device->dev, "ieee802154_register_hw failed\n"); 3210 + goto error; 3211 + } 3212 + priv->hw_registered = true; 3213 + 3214 + return 0; 3215 + error: 3216 + msleep(100); /* wait for pending spi transfers to complete */ 3217 + ca8210_remove(spi_device); 3218 + return link_to_linux_err(ret); 3219 + } 3220 + 3221 + static const struct of_device_id ca8210_of_ids[] = { 3222 + {.compatible = "cascoda,ca8210", }, 3223 + {}, 3224 + }; 3225 + MODULE_DEVICE_TABLE(of, ca8210_of_ids); 3226 + 3227 + static struct spi_driver ca8210_spi_driver = { 3228 + .driver = { 3229 + .name = DRIVER_NAME, 3230 + .owner = THIS_MODULE, 3231 + .of_match_table = of_match_ptr(ca8210_of_ids), 3232 + }, 3233 + .probe = ca8210_probe, 3234 + .remove = ca8210_remove 3235 + }; 3236 + 3237 + module_spi_driver(ca8210_spi_driver); 3238 + 3239 + MODULE_AUTHOR("Harry Morris <h.morris@cascoda.com>"); 3240 + MODULE_DESCRIPTION("CA-8210 SoftMAC driver"); 3241 + MODULE_LICENSE("Dual BSD/GPL"); 3242 + MODULE_VERSION("1.0");
+33
drivers/tty/serdev/core.c
··· 173 173 } 174 174 EXPORT_SYMBOL_GPL(serdev_device_set_flow_control); 175 175 176 + void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout) 177 + { 178 + struct serdev_controller *ctrl = serdev->ctrl; 179 + 180 + if (!ctrl || !ctrl->ops->wait_until_sent) 181 + return; 182 + 183 + ctrl->ops->wait_until_sent(ctrl, timeout); 184 + } 185 + EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent); 186 + 187 + int serdev_device_get_tiocm(struct serdev_device *serdev) 188 + { 189 + struct serdev_controller *ctrl = serdev->ctrl; 190 + 191 + if (!ctrl || !ctrl->ops->get_tiocm) 192 + return -ENOTSUPP; 193 + 194 + return ctrl->ops->get_tiocm(ctrl); 195 + } 196 + EXPORT_SYMBOL_GPL(serdev_device_get_tiocm); 197 + 198 + int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear) 199 + { 200 + struct serdev_controller *ctrl = serdev->ctrl; 201 + 202 + if (!ctrl || !ctrl->ops->set_tiocm) 203 + return -ENOTSUPP; 204 + 205 + return ctrl->ops->set_tiocm(ctrl, set, clear); 206 + } 207 + EXPORT_SYMBOL_GPL(serdev_device_set_tiocm); 208 + 176 209 static int serdev_drv_probe(struct device *dev) 177 210 { 178 211 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
+38 -4
drivers/tty/serdev/serdev-ttyport.c
··· 14 14 #include <linux/serdev.h> 15 15 #include <linux/tty.h> 16 16 #include <linux/tty_driver.h> 17 + #include <linux/poll.h> 17 18 18 19 #define SERPORT_ACTIVE 1 19 20 ··· 47 46 struct serdev_controller *ctrl = port->client_data; 48 47 struct serport *serport = serdev_controller_get_drvdata(ctrl); 49 48 50 - if (!test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &port->tty->flags)) 51 - return; 52 - 53 - if (test_bit(SERPORT_ACTIVE, &serport->flags)) 49 + if (test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &port->tty->flags) && 50 + test_bit(SERPORT_ACTIVE, &serport->flags)) 54 51 serdev_controller_write_wakeup(ctrl); 52 + 53 + wake_up_interruptible_poll(&port->tty->write_wait, POLLOUT); 55 54 } 56 55 57 56 static const struct tty_port_client_operations client_ops = { ··· 168 167 tty_set_termios(tty, &ktermios); 169 168 } 170 169 170 + static void ttyport_wait_until_sent(struct serdev_controller *ctrl, long timeout) 171 + { 172 + struct serport *serport = serdev_controller_get_drvdata(ctrl); 173 + struct tty_struct *tty = serport->tty; 174 + 175 + tty_wait_until_sent(tty, timeout); 176 + } 177 + 178 + static int ttyport_get_tiocm(struct serdev_controller *ctrl) 179 + { 180 + struct serport *serport = serdev_controller_get_drvdata(ctrl); 181 + struct tty_struct *tty = serport->tty; 182 + 183 + if (!tty->ops->tiocmget) 184 + return -ENOTSUPP; 185 + 186 + return tty->driver->ops->tiocmget(tty); 187 + } 188 + 189 + static int ttyport_set_tiocm(struct serdev_controller *ctrl, unsigned int set, unsigned int clear) 190 + { 191 + struct serport *serport = serdev_controller_get_drvdata(ctrl); 192 + struct tty_struct *tty = serport->tty; 193 + 194 + if (!tty->ops->tiocmset) 195 + return -ENOTSUPP; 196 + 197 + return tty->driver->ops->tiocmset(tty, set, clear); 198 + } 199 + 171 200 static const struct serdev_controller_ops ctrl_ops = { 172 201 .write_buf = ttyport_write_buf, 173 202 .write_flush = ttyport_write_flush, ··· 206 175 .close = ttyport_close, 207 176 .set_flow_control = ttyport_set_flow_control, 208 177 .set_baudrate = ttyport_set_baudrate, 178 + .wait_until_sent = ttyport_wait_until_sent, 179 + .get_tiocm = ttyport_get_tiocm, 180 + .set_tiocm = ttyport_set_tiocm, 209 181 }; 210 182 211 183 struct device *serdev_tty_port_register(struct tty_port *port,
+3
drivers/tty/serial/omap-serial.c
··· 1597 1597 1598 1598 of_property_read_u32(dev->of_node, "clock-frequency", 1599 1599 &omap_up_info->uartclk); 1600 + 1601 + omap_up_info->flags = UPF_BOOT_AUTOCONF; 1602 + 1600 1603 return omap_up_info; 1601 1604 } 1602 1605
+47
include/linux/serdev.h
··· 15 15 16 16 #include <linux/types.h> 17 17 #include <linux/device.h> 18 + #include <linux/termios.h> 19 + #include <linux/delay.h> 18 20 19 21 struct serdev_controller; 20 22 struct serdev_device; ··· 83 81 void (*close)(struct serdev_controller *); 84 82 void (*set_flow_control)(struct serdev_controller *, bool); 85 83 unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int); 84 + void (*wait_until_sent)(struct serdev_controller *, long); 85 + int (*get_tiocm)(struct serdev_controller *); 86 + int (*set_tiocm)(struct serdev_controller *, unsigned int, unsigned int); 86 87 }; 87 88 88 89 /** ··· 191 186 void serdev_device_close(struct serdev_device *); 192 187 unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int); 193 188 void serdev_device_set_flow_control(struct serdev_device *, bool); 189 + void serdev_device_wait_until_sent(struct serdev_device *, long); 190 + int serdev_device_get_tiocm(struct serdev_device *); 191 + int serdev_device_set_tiocm(struct serdev_device *, int, int); 194 192 int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t); 195 193 void serdev_device_write_flush(struct serdev_device *); 196 194 int serdev_device_write_room(struct serdev_device *); ··· 231 223 return 0; 232 224 } 233 225 static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {} 226 + static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {} 227 + static inline int serdev_device_get_tiocm(struct serdev_device *serdev) 228 + { 229 + return -ENOTSUPP; 230 + } 231 + static inline int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear) 232 + { 233 + return -ENOTSUPP; 234 + } 234 235 static inline int serdev_device_write_buf(struct serdev_device *sdev, const unsigned char *buf, size_t count) 235 236 { 236 237 return -ENODEV; ··· 254 237 #define serdev_device_driver_unregister(x) 255 238 256 239 #endif /* CONFIG_SERIAL_DEV_BUS */ 240 + 241 + static inline bool serdev_device_get_cts(struct serdev_device *serdev) 242 + { 243 + int status = serdev_device_get_tiocm(serdev); 244 + return !!(status & TIOCM_CTS); 245 + } 246 + 247 + static inline int serdev_device_wait_for_cts(struct serdev_device *serdev, bool state, int timeout_ms) 248 + { 249 + unsigned long timeout; 250 + bool signal; 251 + 252 + timeout = jiffies + msecs_to_jiffies(timeout_ms); 253 + while (time_is_after_jiffies(timeout)) { 254 + signal = serdev_device_get_cts(serdev); 255 + if (signal == state) 256 + return 0; 257 + usleep_range(1000, 2000); 258 + } 259 + 260 + return -ETIMEDOUT; 261 + } 262 + 263 + static inline int serdev_device_set_rts(struct serdev_device *serdev, bool enable) 264 + { 265 + if (enable) 266 + return serdev_device_set_tiocm(serdev, TIOCM_RTS, 0); 267 + else 268 + return serdev_device_set_tiocm(serdev, 0, TIOCM_RTS); 269 + } 257 270 258 271 /* 259 272 * serdev hooks into TTY core
+15
include/net/6lowpan.h
··· 198 198 ipaddr->s6_addr[8] ^= 0x02; 199 199 } 200 200 201 + static inline void lowpan_iphc_uncompress_eui48_lladdr(struct in6_addr *ipaddr, 202 + const void *lladdr) 203 + { 204 + /* fe:80::XXXX:XXff:feXX:XXXX 205 + * \_________________/ 206 + * hwaddr 207 + */ 208 + ipaddr->s6_addr[0] = 0xFE; 209 + ipaddr->s6_addr[1] = 0x80; 210 + memcpy(&ipaddr->s6_addr[8], lladdr, 3); 211 + ipaddr->s6_addr[11] = 0xFF; 212 + ipaddr->s6_addr[12] = 0xFE; 213 + memcpy(&ipaddr->s6_addr[13], lladdr + 3, 3); 214 + } 215 + 201 216 #ifdef DEBUG 202 217 /* print data in line */ 203 218 static inline void raw_dump_inline(const char *caller, char *msg,
+1 -1
include/net/bluetooth/l2cap.h
··· 282 282 #define L2CAP_CR_BAD_KEY_SIZE 0x0007 283 283 #define L2CAP_CR_ENCRYPTION 0x0008 284 284 #define L2CAP_CR_INVALID_SCID 0x0009 285 - #define L2CAP_CR_SCID_IN_USE 0x0010 285 + #define L2CAP_CR_SCID_IN_USE 0x000A 286 286 287 287 /* connect/create channel status */ 288 288 #define L2CAP_CS_NO_INFO 0x0000
+5 -3
include/net/bluetooth/rfcomm.h
··· 21 21 SOFTWARE IS DISCLAIMED. 22 22 */ 23 23 24 + #include <linux/refcount.h> 25 + 24 26 #ifndef __RFCOMM_H 25 27 #define __RFCOMM_H 26 28 ··· 176 174 struct mutex lock; 177 175 unsigned long state; 178 176 unsigned long flags; 179 - atomic_t refcnt; 177 + refcount_t refcnt; 180 178 u8 dlci; 181 179 u8 addr; 182 180 u8 priority; ··· 249 247 250 248 static inline void rfcomm_dlc_hold(struct rfcomm_dlc *d) 251 249 { 252 - atomic_inc(&d->refcnt); 250 + refcount_inc(&d->refcnt); 253 251 } 254 252 255 253 static inline void rfcomm_dlc_put(struct rfcomm_dlc *d) 256 254 { 257 - if (atomic_dec_and_test(&d->refcnt)) 255 + if (refcount_dec_and_test(&d->refcnt)) 258 256 rfcomm_dlc_free(d); 259 257 } 260 258
+10 -2
net/6lowpan/core.c
··· 23 23 { 24 24 int i, ret; 25 25 26 - dev->addr_len = EUI64_ADDR_LEN; 26 + switch (lltype) { 27 + case LOWPAN_LLTYPE_IEEE802154: 28 + dev->addr_len = EUI64_ADDR_LEN; 29 + break; 30 + 31 + case LOWPAN_LLTYPE_BTLE: 32 + dev->addr_len = ETH_ALEN; 33 + break; 34 + } 35 + 27 36 dev->type = ARPHRD_6LOWPAN; 28 37 dev->mtu = IPV6_MIN_MTU; 29 - dev->priv_flags |= IFF_NO_QUEUE; 30 38 31 39 lowpan_dev(dev)->lltype = lltype; 32 40
+46 -11
net/6lowpan/iphc.c
··· 278 278 return ret; 279 279 } 280 280 281 + static void lowpan_iphc_uncompress_lladdr(const struct net_device *dev, 282 + struct in6_addr *ipaddr, 283 + const void *lladdr) 284 + { 285 + switch (dev->addr_len) { 286 + case ETH_ALEN: 287 + lowpan_iphc_uncompress_eui48_lladdr(ipaddr, lladdr); 288 + break; 289 + case EUI64_ADDR_LEN: 290 + lowpan_iphc_uncompress_eui64_lladdr(ipaddr, lladdr); 291 + break; 292 + default: 293 + WARN_ON_ONCE(1); 294 + break; 295 + } 296 + } 297 + 281 298 /* Uncompress address function for source and 282 299 * destination address(non-multicast). 283 300 * ··· 337 320 lowpan_iphc_uncompress_802154_lladdr(ipaddr, lladdr); 338 321 break; 339 322 default: 340 - lowpan_iphc_uncompress_eui64_lladdr(ipaddr, lladdr); 323 + lowpan_iphc_uncompress_lladdr(dev, ipaddr, lladdr); 341 324 break; 342 325 } 343 326 break; ··· 398 381 lowpan_iphc_uncompress_802154_lladdr(ipaddr, lladdr); 399 382 break; 400 383 default: 401 - lowpan_iphc_uncompress_eui64_lladdr(ipaddr, lladdr); 384 + lowpan_iphc_uncompress_lladdr(dev, ipaddr, lladdr); 402 385 break; 403 386 } 404 387 ipv6_addr_prefix_copy(ipaddr, &ctx->pfx, ctx->plen); ··· 683 666 684 667 switch (iphc1 & (LOWPAN_IPHC_M | LOWPAN_IPHC_DAC)) { 685 668 case LOWPAN_IPHC_M | LOWPAN_IPHC_DAC: 669 + skb->pkt_type = PACKET_BROADCAST; 670 + 686 671 spin_lock_bh(&lowpan_dev(dev)->ctx.lock); 687 672 ci = lowpan_iphc_ctx_get_by_id(dev, LOWPAN_IPHC_CID_DCI(cid)); 688 673 if (!ci) { ··· 700 681 spin_unlock_bh(&lowpan_dev(dev)->ctx.lock); 701 682 break; 702 683 case LOWPAN_IPHC_M: 684 + skb->pkt_type = PACKET_BROADCAST; 685 + 703 686 /* multicast */ 704 687 err = lowpan_uncompress_multicast_daddr(skb, &hdr.daddr, 705 688 iphc1 & LOWPAN_IPHC_DAM_MASK); 706 689 break; 707 690 case LOWPAN_IPHC_DAC: 691 + skb->pkt_type = PACKET_HOST; 692 + 708 693 spin_lock_bh(&lowpan_dev(dev)->ctx.lock); 709 694 ci = lowpan_iphc_ctx_get_by_id(dev, LOWPAN_IPHC_CID_DCI(cid)); 710 695 if (!ci) { ··· 724 701 spin_unlock_bh(&lowpan_dev(dev)->ctx.lock); 725 702 break; 726 703 default: 704 + skb->pkt_type = PACKET_HOST; 705 + 727 706 err = lowpan_iphc_uncompress_addr(skb, dev, &hdr.daddr, 728 707 iphc1 & LOWPAN_IPHC_DAM_MASK, 729 708 daddr); ··· 827 802 return lladdr_compress; 828 803 } 829 804 805 + static bool lowpan_iphc_addr_equal(const struct net_device *dev, 806 + const struct lowpan_iphc_ctx *ctx, 807 + const struct in6_addr *ipaddr, 808 + const void *lladdr) 809 + { 810 + struct in6_addr tmp = {}; 811 + 812 + lowpan_iphc_uncompress_lladdr(dev, &tmp, lladdr); 813 + 814 + if (ctx) 815 + ipv6_addr_prefix_copy(&tmp, &ctx->pfx, ctx->plen); 816 + 817 + return ipv6_addr_equal(&tmp, ipaddr); 818 + } 819 + 830 820 static u8 lowpan_compress_ctx_addr(u8 **hc_ptr, const struct net_device *dev, 831 821 const struct in6_addr *ipaddr, 832 822 const struct lowpan_iphc_ctx *ctx, ··· 859 819 } 860 820 break; 861 821 default: 862 - /* check for SAM/DAM = 11 */ 863 - memcpy(&tmp.s6_addr[8], lladdr, EUI64_ADDR_LEN); 864 - /* second bit-flip (Universe/Local) is done according RFC2464 */ 865 - tmp.s6_addr[8] ^= 0x02; 866 - /* context information are always used */ 867 - ipv6_addr_prefix_copy(&tmp, &ctx->pfx, ctx->plen); 868 - if (ipv6_addr_equal(&tmp, ipaddr)) { 822 + if (lowpan_iphc_addr_equal(dev, ctx, ipaddr, lladdr)) { 869 823 dam = LOWPAN_IPHC_DAM_11; 870 824 goto out; 871 825 } ··· 955 921 } 956 922 break; 957 923 default: 958 - if (is_addr_mac_addr_based(ipaddr, lladdr)) { 959 - dam = LOWPAN_IPHC_DAM_11; /* 0-bits */ 924 + if (lowpan_iphc_addr_equal(dev, NULL, ipaddr, lladdr)) { 925 + dam = LOWPAN_IPHC_DAM_11; 960 926 pr_debug("address compression 0 bits\n"); 961 927 goto out; 962 928 } 929 + 963 930 break; 964 931 } 965 932
+49 -143
net/bluetooth/6lowpan.c
··· 20 20 #include <net/ipv6.h> 21 21 #include <net/ip6_route.h> 22 22 #include <net/addrconf.h> 23 + #include <net/pkt_sched.h> 23 24 24 25 #include <net/bluetooth/bluetooth.h> 25 26 #include <net/bluetooth/hci_core.h> ··· 39 38 struct in6_addr addr; 40 39 struct in6_addr gw; 41 40 struct l2cap_chan *chan; 42 - int status; 43 41 }; 44 42 #define lowpan_cb(skb) ((struct skb_cb *)((skb)->cb)) 45 43 ··· 64 64 struct l2cap_chan *chan; 65 65 66 66 /* peer addresses in various formats */ 67 - unsigned char eui64_addr[EUI64_ADDR_LEN]; 67 + unsigned char lladdr[ETH_ALEN]; 68 68 struct in6_addr peer_addr; 69 69 }; 70 70 ··· 270 270 } 271 271 272 272 static int iphc_decompress(struct sk_buff *skb, struct net_device *netdev, 273 - struct l2cap_chan *chan) 273 + struct lowpan_peer *peer) 274 274 { 275 - const u8 *saddr, *daddr; 275 + const u8 *saddr; 276 276 struct lowpan_btle_dev *dev; 277 - struct lowpan_peer *peer; 278 277 279 278 dev = lowpan_btle_dev(netdev); 280 279 281 - rcu_read_lock(); 282 - peer = __peer_lookup_chan(dev, chan); 283 - rcu_read_unlock(); 284 - if (!peer) 285 - return -EINVAL; 280 + saddr = peer->lladdr; 286 281 287 - saddr = peer->eui64_addr; 288 - daddr = dev->netdev->dev_addr; 289 - 290 - return lowpan_header_decompress(skb, netdev, daddr, saddr); 282 + return lowpan_header_decompress(skb, netdev, netdev->dev_addr, saddr); 291 283 } 292 284 293 285 static int recv_pkt(struct sk_buff *skb, struct net_device *dev, 294 - struct l2cap_chan *chan) 286 + struct lowpan_peer *peer) 295 287 { 296 288 struct sk_buff *local_skb; 297 289 int ret; ··· 336 344 337 345 local_skb->dev = dev; 338 346 339 - ret = iphc_decompress(local_skb, dev, chan); 347 + ret = iphc_decompress(local_skb, dev, peer); 340 348 if (ret < 0) { 349 + BT_DBG("iphc_decompress failed: %d", ret); 341 350 kfree_skb(local_skb); 342 351 goto drop; 343 352 } ··· 358 365 consume_skb(local_skb); 359 366 consume_skb(skb); 360 367 } else { 368 + BT_DBG("unknown packet type"); 361 369 goto drop; 362 370 } 363 371 ··· 384 390 if (!dev || !dev->netdev) 385 391 return -ENOENT; 386 392 387 - err = recv_pkt(skb, dev->netdev, chan); 393 + err = recv_pkt(skb, dev->netdev, peer); 388 394 if (err) { 389 395 BT_DBG("recv pkt %d", err); 390 396 err = -EAGAIN; 391 397 } 392 398 393 399 return err; 394 - } 395 - 396 - static u8 get_addr_type_from_eui64(u8 byte) 397 - { 398 - /* Is universal(0) or local(1) bit */ 399 - return ((byte & 0x02) ? BDADDR_LE_RANDOM : BDADDR_LE_PUBLIC); 400 - } 401 - 402 - static void copy_to_bdaddr(struct in6_addr *ip6_daddr, bdaddr_t *addr) 403 - { 404 - u8 *eui64 = ip6_daddr->s6_addr + 8; 405 - 406 - addr->b[0] = eui64[7]; 407 - addr->b[1] = eui64[6]; 408 - addr->b[2] = eui64[5]; 409 - addr->b[3] = eui64[2]; 410 - addr->b[4] = eui64[1]; 411 - addr->b[5] = eui64[0]; 412 - } 413 - 414 - static void convert_dest_bdaddr(struct in6_addr *ip6_daddr, 415 - bdaddr_t *addr, u8 *addr_type) 416 - { 417 - copy_to_bdaddr(ip6_daddr, addr); 418 - 419 - /* We need to toggle the U/L bit that we got from IPv6 address 420 - * so that we get the proper address and type of the BD address. 421 - */ 422 - addr->b[5] ^= 0x02; 423 - 424 - *addr_type = get_addr_type_from_eui64(addr->b[5]); 425 400 } 426 401 427 402 static int setup_header(struct sk_buff *skb, struct net_device *netdev, ··· 400 437 struct ipv6hdr *hdr; 401 438 struct lowpan_btle_dev *dev; 402 439 struct lowpan_peer *peer; 403 - bdaddr_t addr, *any = BDADDR_ANY; 404 - u8 *daddr = any->b; 440 + u8 *daddr; 405 441 int err, status = 0; 406 442 407 443 hdr = ipv6_hdr(skb); ··· 411 449 412 450 if (ipv6_addr_is_multicast(&ipv6_daddr)) { 413 451 lowpan_cb(skb)->chan = NULL; 452 + daddr = NULL; 414 453 } else { 415 - u8 addr_type; 454 + BT_DBG("dest IP %pI6c", &ipv6_daddr); 416 455 417 - /* Get destination BT device from skb. 418 - * If there is no such peer then discard the packet. 456 + /* The packet might be sent to 6lowpan interface 457 + * because of routing (either via default route 458 + * or user set route) so get peer according to 459 + * the destination address. 419 460 */ 420 - convert_dest_bdaddr(&ipv6_daddr, &addr, &addr_type); 421 - 422 - BT_DBG("dest addr %pMR type %d IP %pI6c", &addr, 423 - addr_type, &ipv6_daddr); 424 - 425 - peer = peer_lookup_ba(dev, &addr, addr_type); 461 + peer = peer_lookup_dst(dev, &ipv6_daddr, skb); 426 462 if (!peer) { 427 - /* The packet might be sent to 6lowpan interface 428 - * because of routing (either via default route 429 - * or user set route) so get peer according to 430 - * the destination address. 431 - */ 432 - peer = peer_lookup_dst(dev, &ipv6_daddr, skb); 433 - if (!peer) { 434 - BT_DBG("no such peer %pMR found", &addr); 435 - return -ENOENT; 436 - } 463 + BT_DBG("no such peer"); 464 + return -ENOENT; 437 465 } 438 466 439 - daddr = peer->eui64_addr; 440 - *peer_addr = addr; 441 - *peer_addr_type = addr_type; 467 + daddr = peer->lladdr; 468 + *peer_addr = peer->chan->dst; 469 + *peer_addr_type = peer->chan->dst_type; 442 470 lowpan_cb(skb)->chan = peer->chan; 443 471 444 472 status = 1; ··· 479 527 return 0; 480 528 } 481 529 482 - if (!err) 483 - err = lowpan_cb(skb)->status; 484 - 485 - if (err < 0) { 486 - if (err == -EAGAIN) 487 - netdev->stats.tx_dropped++; 488 - else 489 - netdev->stats.tx_errors++; 490 - } 530 + if (err < 0) 531 + netdev->stats.tx_errors++; 491 532 492 533 return err; 493 534 } ··· 592 647 { 593 648 dev->hard_header_len = 0; 594 649 dev->needed_tailroom = 0; 595 - dev->flags = IFF_RUNNING | IFF_POINTOPOINT | 596 - IFF_MULTICAST; 650 + dev->flags = IFF_RUNNING | IFF_MULTICAST; 597 651 dev->watchdog_timeo = 0; 652 + dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN; 598 653 599 654 dev->netdev_ops = &netdev_ops; 600 655 dev->header_ops = &header_ops; ··· 604 659 static struct device_type bt_type = { 605 660 .name = "bluetooth", 606 661 }; 607 - 608 - static void set_addr(u8 *eui, u8 *addr, u8 addr_type) 609 - { 610 - /* addr is the BT address in little-endian format */ 611 - eui[0] = addr[5]; 612 - eui[1] = addr[4]; 613 - eui[2] = addr[3]; 614 - eui[3] = 0xFF; 615 - eui[4] = 0xFE; 616 - eui[5] = addr[2]; 617 - eui[6] = addr[1]; 618 - eui[7] = addr[0]; 619 - 620 - /* Universal/local bit set, BT 6lowpan draft ch. 3.2.1 */ 621 - if (addr_type == BDADDR_LE_PUBLIC) 622 - eui[0] &= ~0x02; 623 - else 624 - eui[0] |= 0x02; 625 - 626 - BT_DBG("type %d addr %*phC", addr_type, 8, eui); 627 - } 628 - 629 - static void set_dev_addr(struct net_device *netdev, bdaddr_t *addr, 630 - u8 addr_type) 631 - { 632 - netdev->addr_assign_type = NET_ADDR_PERM; 633 - set_addr(netdev->dev_addr, addr->b, addr_type); 634 - } 635 662 636 663 static void ifup(struct net_device *netdev) 637 664 { ··· 663 746 return chan; 664 747 } 665 748 666 - static void set_ip_addr_bits(u8 addr_type, u8 *addr) 667 - { 668 - if (addr_type == BDADDR_LE_PUBLIC) 669 - *addr |= 0x02; 670 - else 671 - *addr &= ~0x02; 672 - } 673 - 674 749 static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan, 675 - struct lowpan_btle_dev *dev) 750 + struct lowpan_btle_dev *dev, 751 + bool new_netdev) 676 752 { 677 753 struct lowpan_peer *peer; 678 754 ··· 676 766 peer->chan = chan; 677 767 memset(&peer->peer_addr, 0, sizeof(struct in6_addr)); 678 768 679 - /* RFC 2464 ch. 5 */ 680 - peer->peer_addr.s6_addr[0] = 0xFE; 681 - peer->peer_addr.s6_addr[1] = 0x80; 682 - set_addr((u8 *)&peer->peer_addr.s6_addr + 8, chan->dst.b, 683 - chan->dst_type); 769 + baswap((void *)peer->lladdr, &chan->dst); 684 770 685 - memcpy(&peer->eui64_addr, (u8 *)&peer->peer_addr.s6_addr + 8, 686 - EUI64_ADDR_LEN); 687 - 688 - /* IPv6 address needs to have the U/L bit set properly so toggle 689 - * it back here. 690 - */ 691 - set_ip_addr_bits(chan->dst_type, (u8 *)&peer->peer_addr.s6_addr + 8); 771 + lowpan_iphc_uncompress_eui48_lladdr(&peer->peer_addr, peer->lladdr); 692 772 693 773 spin_lock(&devices_lock); 694 774 INIT_LIST_HEAD(&peer->list); ··· 686 786 spin_unlock(&devices_lock); 687 787 688 788 /* Notifying peers about us needs to be done without locks held */ 689 - INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers); 789 + if (new_netdev) 790 + INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers); 690 791 schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100)); 691 792 692 793 return peer->chan; ··· 704 803 if (!netdev) 705 804 return -ENOMEM; 706 805 707 - set_dev_addr(netdev, &chan->src, chan->src_type); 806 + netdev->addr_assign_type = NET_ADDR_PERM; 807 + baswap((void *)netdev->dev_addr, &chan->src); 708 808 709 809 netdev->netdev_ops = &netdev_ops; 710 810 SET_NETDEV_DEV(netdev, &chan->conn->hcon->hdev->dev); ··· 745 843 static inline void chan_ready_cb(struct l2cap_chan *chan) 746 844 { 747 845 struct lowpan_btle_dev *dev; 846 + bool new_netdev = false; 748 847 749 848 dev = lookup_dev(chan->conn); 750 849 ··· 756 853 l2cap_chan_del(chan, -ENOENT); 757 854 return; 758 855 } 856 + new_netdev = true; 759 857 } 760 858 761 859 if (!try_module_get(THIS_MODULE)) 762 860 return; 763 861 764 - add_peer_chan(chan, dev); 862 + add_peer_chan(chan, dev, new_netdev); 765 863 ifup(dev->netdev); 766 864 } 767 865 ··· 868 964 869 965 static void chan_suspend_cb(struct l2cap_chan *chan) 870 966 { 871 - struct sk_buff *skb = chan->data; 967 + struct lowpan_btle_dev *dev; 872 968 873 - BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb); 969 + BT_DBG("chan %p suspend", chan); 874 970 875 - if (!skb) 971 + dev = lookup_dev(chan->conn); 972 + if (!dev || !dev->netdev) 876 973 return; 877 974 878 - lowpan_cb(skb)->status = -EAGAIN; 975 + netif_stop_queue(dev->netdev); 879 976 } 880 977 881 978 static void chan_resume_cb(struct l2cap_chan *chan) 882 979 { 883 - struct sk_buff *skb = chan->data; 980 + struct lowpan_btle_dev *dev; 884 981 885 - BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb); 982 + BT_DBG("chan %p resume", chan); 886 983 887 - if (!skb) 984 + dev = lookup_dev(chan->conn); 985 + if (!dev || !dev->netdev) 888 986 return; 889 987 890 - lowpan_cb(skb)->status = 0; 988 + netif_wake_queue(dev->netdev); 891 989 } 892 990 893 991 static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
+26
net/bluetooth/af_bluetooth.c
··· 159 159 BT_DBG("parent %p, sk %p", parent, sk); 160 160 161 161 sock_hold(sk); 162 + lock_sock(sk); 162 163 list_add_tail(&bt_sk(sk)->accept_q, &bt_sk(parent)->accept_q); 163 164 bt_sk(sk)->parent = parent; 165 + release_sock(sk); 164 166 parent->sk_ack_backlog++; 165 167 } 166 168 EXPORT_SYMBOL(bt_accept_enqueue); 167 169 170 + /* Calling function must hold the sk lock. 171 + * bt_sk(sk)->parent must be non-NULL meaning sk is in the parent list. 172 + */ 168 173 void bt_accept_unlink(struct sock *sk) 169 174 { 170 175 BT_DBG("sk %p state %d", sk, sk->sk_state); ··· 188 183 189 184 BT_DBG("parent %p", parent); 190 185 186 + restart: 191 187 list_for_each_entry_safe(s, n, &bt_sk(parent)->accept_q, accept_q) { 192 188 sk = (struct sock *)s; 193 189 190 + /* Prevent early freeing of sk due to unlink and sock_kill */ 191 + sock_hold(sk); 194 192 lock_sock(sk); 193 + 194 + /* Check sk has not already been unlinked via 195 + * bt_accept_unlink() due to serialisation caused by sk locking 196 + */ 197 + if (!bt_sk(sk)->parent) { 198 + BT_DBG("sk %p, already unlinked", sk); 199 + release_sock(sk); 200 + sock_put(sk); 201 + 202 + /* Restart the loop as sk is no longer in the list 203 + * and also avoid a potential infinite loop because 204 + * list_for_each_entry_safe() is not thread safe. 205 + */ 206 + goto restart; 207 + } 208 + 209 + /* sk is safely in the parent list so reduce reference count */ 210 + sock_put(sk); 195 211 196 212 /* FIXME: Is this check still needed */ 197 213 if (sk->sk_state == BT_CLOSED) {
+5 -5
net/bluetooth/amp.c
··· 263 263 struct hci_cp_read_local_amp_assoc cp; 264 264 struct amp_assoc *loc_assoc = &hdev->loc_assoc; 265 265 struct hci_request req; 266 - int err = 0; 266 + int err; 267 267 268 268 BT_DBG("%s handle %d", hdev->name, phy_handle); 269 269 ··· 282 282 { 283 283 struct hci_cp_read_local_amp_assoc cp; 284 284 struct hci_request req; 285 - int err = 0; 285 + int err; 286 286 287 287 memset(&hdev->loc_assoc, 0, sizeof(struct amp_assoc)); 288 288 memset(&cp, 0, sizeof(cp)); ··· 292 292 set_bit(READ_LOC_AMP_ASSOC, &mgr->state); 293 293 hci_req_init(&req, hdev); 294 294 hci_req_add(&req, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp); 295 - hci_req_run_skb(&req, read_local_amp_assoc_complete); 295 + err = hci_req_run_skb(&req, read_local_amp_assoc_complete); 296 296 if (err < 0) 297 297 a2mp_send_getampassoc_rsp(hdev, A2MP_STATUS_INVALID_CTRL_ID); 298 298 } ··· 303 303 struct hci_cp_read_local_amp_assoc cp; 304 304 struct amp_mgr *mgr = hcon->amp_mgr; 305 305 struct hci_request req; 306 - int err = 0; 306 + int err; 307 307 308 308 cp.phy_handle = hcon->handle; 309 309 cp.len_so_far = cpu_to_le16(0); ··· 314 314 /* Read Local AMP Assoc final link information data */ 315 315 hci_req_init(&req, hdev); 316 316 hci_req_add(&req, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp); 317 - hci_req_run_skb(&req, read_local_amp_assoc_complete); 317 + err = hci_req_run_skb(&req, read_local_amp_assoc_complete); 318 318 if (err < 0) 319 319 a2mp_send_getampassoc_rsp(hdev, A2MP_STATUS_INVALID_CTRL_ID); 320 320 }
+2 -2
net/bluetooth/hci_core.c
··· 2950 2950 hdev->le_adv_max_interval = 0x0800; 2951 2951 hdev->le_scan_interval = 0x0060; 2952 2952 hdev->le_scan_window = 0x0030; 2953 - hdev->le_conn_min_interval = 0x0028; 2954 - hdev->le_conn_max_interval = 0x0038; 2953 + hdev->le_conn_min_interval = 0x0018; 2954 + hdev->le_conn_max_interval = 0x0028; 2955 2955 hdev->le_conn_latency = 0x0000; 2956 2956 hdev->le_supv_timeout = 0x002a; 2957 2957 hdev->le_def_tx_len = 0x001b;
+19 -11
net/bluetooth/l2cap_core.c
··· 2425 2425 return 0; 2426 2426 } 2427 2427 2428 + static void l2cap_le_flowctl_send(struct l2cap_chan *chan) 2429 + { 2430 + int sent = 0; 2431 + 2432 + BT_DBG("chan %p", chan); 2433 + 2434 + while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) { 2435 + l2cap_do_send(chan, skb_dequeue(&chan->tx_q)); 2436 + chan->tx_credits--; 2437 + sent++; 2438 + } 2439 + 2440 + BT_DBG("Sent %d credits %u queued %u", sent, chan->tx_credits, 2441 + skb_queue_len(&chan->tx_q)); 2442 + } 2443 + 2428 2444 int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 2429 2445 { 2430 2446 struct sk_buff *skb; ··· 2474 2458 if (len > chan->omtu) 2475 2459 return -EMSGSIZE; 2476 2460 2477 - if (!chan->tx_credits) 2478 - return -EAGAIN; 2479 - 2480 2461 __skb_queue_head_init(&seg_queue); 2481 2462 2482 2463 err = l2cap_segment_le_sdu(chan, &seg_queue, msg, len); ··· 2488 2475 2489 2476 skb_queue_splice_tail_init(&seg_queue, &chan->tx_q); 2490 2477 2491 - while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) { 2492 - l2cap_do_send(chan, skb_dequeue(&chan->tx_q)); 2493 - chan->tx_credits--; 2494 - } 2478 + l2cap_le_flowctl_send(chan); 2495 2479 2496 2480 if (!chan->tx_credits) 2497 2481 chan->ops->suspend(chan); ··· 5580 5570 5581 5571 chan->tx_credits += credits; 5582 5572 5583 - while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) { 5584 - l2cap_do_send(chan, skb_dequeue(&chan->tx_q)); 5585 - chan->tx_credits--; 5586 - } 5573 + /* Resume sending */ 5574 + l2cap_le_flowctl_send(chan); 5587 5575 5588 5576 if (chan->tx_credits) 5589 5577 chan->ops->resume(chan);
+2 -2
net/bluetooth/rfcomm/core.c
··· 311 311 312 312 skb_queue_head_init(&d->tx_queue); 313 313 mutex_init(&d->lock); 314 - atomic_set(&d->refcnt, 1); 314 + refcount_set(&d->refcnt, 1); 315 315 316 316 rfcomm_dlc_clear_state(d); 317 317 ··· 342 342 { 343 343 struct rfcomm_session *s = d->session; 344 344 345 - BT_DBG("dlc %p refcnt %d session %p", d, atomic_read(&d->refcnt), s); 345 + BT_DBG("dlc %p refcnt %d session %p", d, refcount_read(&d->refcnt), s); 346 346 347 347 list_del(&d->list); 348 348 d->session = NULL;
+16 -5
net/ipv6/addrconf.c
··· 2073 2073 __ipv6_dev_ac_dec(ifp->idev, &addr); 2074 2074 } 2075 2075 2076 - static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev) 2076 + static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev) 2077 2077 { 2078 - if (dev->addr_len != EUI64_ADDR_LEN) 2078 + switch (dev->addr_len) { 2079 + case ETH_ALEN: 2080 + memcpy(eui, dev->dev_addr, 3); 2081 + eui[3] = 0xFF; 2082 + eui[4] = 0xFE; 2083 + memcpy(eui + 5, dev->dev_addr + 3, 3); 2084 + break; 2085 + case EUI64_ADDR_LEN: 2086 + memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN); 2087 + eui[0] ^= 2; 2088 + break; 2089 + default: 2079 2090 return -1; 2080 - memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN); 2081 - eui[0] ^= 2; 2091 + } 2092 + 2082 2093 return 0; 2083 2094 } 2084 2095 ··· 2181 2170 case ARPHRD_TUNNEL: 2182 2171 return addrconf_ifid_gre(eui, dev); 2183 2172 case ARPHRD_6LOWPAN: 2184 - return addrconf_ifid_eui64(eui, dev); 2173 + return addrconf_ifid_6lowpan(eui, dev); 2185 2174 case ARPHRD_IEEE1394: 2186 2175 return addrconf_ifid_ieee1394(eui, dev); 2187 2176 case ARPHRD_TUNNEL6: