From d1f3619befe5ca773d0f6056ddb6e5d2ab84f5c5 Mon Sep 17 00:00:00 2001 From: Laxminath Kasam Date: Thu, 3 Aug 2017 18:54:01 +0530 Subject: [PATCH] audio-lnx: Propagate changes from kernel for wdsp-glink/avtimer Pick the WDSP-Glink and avtimer drivers from 4.9 kernel at below cutoff - (80be7d6a313 - "msm: ADSPRPC: Fix for NULL pointer dereference") This changes are done to migrate wdsp-glink/avtimer drivers to this new audio kernel techpack. Also remove the CDSP loader from techpack audio as it is non-audio related. Change-Id: I4f55429c5cbcd24920fdcb8eb2fe3aec02af320e Signed-off-by: Laxminath Kasam --- dsp/Makefile | 2 +- dsp/avtimer.c | 548 ++++++++++++++++++++ dsp/cdsp-loader.c | 280 ---------- ipc/Makefile | 1 + ipc/wcd-dsp-glink.c | 1201 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 1751 insertions(+), 281 deletions(-) create mode 100644 dsp/avtimer.c delete mode 100644 dsp/cdsp-loader.c create mode 100644 ipc/wcd-dsp-glink.c diff --git a/dsp/Makefile b/dsp/Makefile index a31aeb22a2..b609df228a 100644 --- a/dsp/Makefile +++ b/dsp/Makefile @@ -5,6 +5,6 @@ obj-$(CONFIG_MSM_ADSP_LOADER) += adsp-loader.o obj-$(CONFIG_MSM_QDSP6_SSR) += audio_ssr.o obj-$(CONFIG_MSM_QDSP6_PDR) += audio_pdr.o obj-$(CONFIG_MSM_QDSP6_NOTIFIER) += audio_notifier.o -obj-$(CONFIG_MSM_CDSP_LOADER) += cdsp-loader.o obj-$(CONFIG_MSM_ULTRASOUND) += usf.o usfcdev.o q6usm.o +obj-$(CONFIG_MSM_AVTIMER) += avtimer.o obj-$(CONFIG_MSM_QDSP6V2_CODECS) += codecs/ diff --git a/dsp/avtimer.c b/dsp/avtimer.c new file mode 100644 index 0000000000..63044db6e4 --- /dev/null +++ b/dsp/avtimer.c @@ -0,0 +1,548 @@ +/* + * Copyright (c) 2012-2015, 2017 The Linux Foundation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEVICE_NAME "avtimer" +#define TIMEOUT_MS 1000 +#define CORE_CLIENT 1 +#define TEMP_PORT ((CORE_CLIENT << 8) | 0x0001) +#define SSR_WAKETIME 1000 +#define Q6_READY_RETRY 250 +#define Q6_READY_MAX_RETRIES 40 + +#define AVCS_CMD_REMOTE_AVTIMER_VOTE_REQUEST 0x00012914 +#define AVCS_CMD_RSP_REMOTE_AVTIMER_VOTE_REQUEST 0x00012915 +#define AVCS_CMD_REMOTE_AVTIMER_RELEASE_REQUEST 0x00012916 +#define AVTIMER_REG_CNT 2 + +struct adsp_avt_timer { + struct apr_hdr hdr; + union { + char client_name[8]; + u32 avtimer_handle; + }; +} __packed; + +static int major; + +struct avtimer_t { + struct apr_svc *core_handle_q; + struct cdev myc; + struct class *avtimer_class; + struct mutex avtimer_lock; + int avtimer_open_cnt; + struct delayed_work ssr_dwork; + wait_queue_head_t adsp_resp_wait; + int enable_timer_resp_received; + int timer_handle; + void __iomem *p_avtimer_msw; + void __iomem *p_avtimer_lsw; + uint32_t clk_div; + uint32_t clk_mult; + atomic_t adsp_ready; + int num_retries; +}; + +static struct avtimer_t avtimer; + +static int32_t aprv2_core_fn_q(struct apr_client_data *data, void *priv) +{ + uint32_t *payload1; + + if (!data) { + pr_err("%s: Invalid params\n", __func__); + return -EINVAL; + } + pr_debug("%s: core msg: payload len = %u, apr resp opcode = 0x%X\n", + __func__, data->payload_size, data->opcode); + + switch (data->opcode) { + + case APR_BASIC_RSP_RESULT:{ + + if (!data->payload_size) { + pr_err("%s: APR_BASIC_RSP_RESULT No Payload ", + __func__); + return 0; + } + + payload1 = data->payload; + switch (payload1[0]) { + case AVCS_CMD_REMOTE_AVTIMER_RELEASE_REQUEST: + pr_debug("%s: Cmd = TIMER RELEASE status[0x%x]\n", + __func__, payload1[1]); + break; + default: + pr_err("Invalid cmd rsp[0x%x][0x%x]\n", + payload1[0], payload1[1]); + break; + } + break; + } + + case RESET_EVENTS:{ + pr_debug("%s: Reset event received in AV timer\n", __func__); + apr_reset(avtimer.core_handle_q); + avtimer.core_handle_q = NULL; + avtimer.avtimer_open_cnt = 0; + atomic_set(&avtimer.adsp_ready, 0); + schedule_delayed_work(&avtimer.ssr_dwork, + msecs_to_jiffies(SSR_WAKETIME)); + break; + } + + case AVCS_CMD_RSP_REMOTE_AVTIMER_VOTE_REQUEST: + payload1 = data->payload; + pr_debug("%s: RSP_REMOTE_AVTIMER_VOTE_REQUEST handle %x\n", + __func__, payload1[0]); + avtimer.timer_handle = payload1[0]; + avtimer.enable_timer_resp_received = 1; + wake_up(&avtimer.adsp_resp_wait); + break; + default: + pr_err("%s: Message adspcore svc: %d\n", + __func__, data->opcode); + break; + } + + return 0; +} + +int avcs_core_open(void) +{ + if (!avtimer.core_handle_q) + avtimer.core_handle_q = apr_register("ADSP", "CORE", + aprv2_core_fn_q, TEMP_PORT, NULL); + pr_debug("%s: Open_q %p\n", __func__, avtimer.core_handle_q); + if (!avtimer.core_handle_q) { + pr_err("%s: Unable to register CORE\n", __func__); + return -EINVAL; + } + return 0; +} +EXPORT_SYMBOL(avcs_core_open); + +static int avcs_core_disable_avtimer(int timerhandle) +{ + int rc = -EINVAL; + struct adsp_avt_timer payload; + + if (!timerhandle) { + pr_err("%s: Invalid timer handle\n", __func__); + return -EINVAL; + } + memset(&payload, 0, sizeof(payload)); + rc = avcs_core_open(); + if (!rc && avtimer.core_handle_q) { + payload.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, + APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER); + payload.hdr.pkt_size = + sizeof(struct adsp_avt_timer); + payload.hdr.src_svc = avtimer.core_handle_q->id; + payload.hdr.src_domain = APR_DOMAIN_APPS; + payload.hdr.dest_domain = APR_DOMAIN_ADSP; + payload.hdr.dest_svc = APR_SVC_ADSP_CORE; + payload.hdr.src_port = TEMP_PORT; + payload.hdr.dest_port = TEMP_PORT; + payload.hdr.token = CORE_CLIENT; + payload.hdr.opcode = AVCS_CMD_REMOTE_AVTIMER_RELEASE_REQUEST; + payload.avtimer_handle = timerhandle; + pr_debug("%s: disable avtimer opcode %x handle %x\n", + __func__, payload.hdr.opcode, payload.avtimer_handle); + rc = apr_send_pkt(avtimer.core_handle_q, + (uint32_t *)&payload); + if (rc < 0) + pr_err("%s: Enable AVtimer failed op[0x%x]rc[%d]\n", + __func__, payload.hdr.opcode, rc); + else + rc = 0; + } + return rc; +} + +static int avcs_core_enable_avtimer(char *client_name) +{ + int rc = -EINVAL, ret = -EINVAL; + struct adsp_avt_timer payload; + + if (!client_name) { + pr_err("%s: Invalid params\n", __func__); + return -EINVAL; + } + memset(&payload, 0, sizeof(payload)); + rc = avcs_core_open(); + if (!rc && avtimer.core_handle_q) { + avtimer.enable_timer_resp_received = 0; + payload.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_EVENT, + APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER); + payload.hdr.pkt_size = + sizeof(struct adsp_avt_timer); + payload.hdr.src_svc = avtimer.core_handle_q->id; + payload.hdr.src_domain = APR_DOMAIN_APPS; + payload.hdr.dest_domain = APR_DOMAIN_ADSP; + payload.hdr.dest_svc = APR_SVC_ADSP_CORE; + payload.hdr.src_port = TEMP_PORT; + payload.hdr.dest_port = TEMP_PORT; + payload.hdr.token = CORE_CLIENT; + payload.hdr.opcode = AVCS_CMD_REMOTE_AVTIMER_VOTE_REQUEST; + strlcpy(payload.client_name, client_name, + sizeof(payload.client_name)); + pr_debug("%s: enable avtimer opcode %x client name %s\n", + __func__, payload.hdr.opcode, payload.client_name); + rc = apr_send_pkt(avtimer.core_handle_q, + (uint32_t *)&payload); + if (rc < 0) { + pr_err("%s: Enable AVtimer failed op[0x%x]rc[%d]\n", + __func__, payload.hdr.opcode, rc); + goto bail; + } else + rc = 0; + ret = wait_event_timeout(avtimer.adsp_resp_wait, + (avtimer.enable_timer_resp_received == 1), + msecs_to_jiffies(TIMEOUT_MS)); + if (!ret) { + pr_err("%s: wait_event timeout for Enable timer\n", + __func__); + rc = -ETIMEDOUT; + } + if (rc) + avtimer.timer_handle = 0; + } +bail: + return rc; +} + +int avcs_core_disable_power_collapse(int enable) +{ + int rc = 0; + + mutex_lock(&avtimer.avtimer_lock); + if (enable) { + if (avtimer.avtimer_open_cnt) { + avtimer.avtimer_open_cnt++; + pr_debug("%s: opened avtimer open count=%d\n", + __func__, avtimer.avtimer_open_cnt); + rc = 0; + goto done; + } + rc = avcs_core_enable_avtimer("timer"); + if (!rc) { + avtimer.avtimer_open_cnt++; + atomic_set(&avtimer.adsp_ready, 1); + } + } else { + if (avtimer.avtimer_open_cnt > 0) { + avtimer.avtimer_open_cnt--; + if (!avtimer.avtimer_open_cnt) { + rc = avcs_core_disable_avtimer( + avtimer.timer_handle); + avtimer.timer_handle = 0; + } + } + } +done: + mutex_unlock(&avtimer.avtimer_lock); + return rc; +} +EXPORT_SYMBOL(avcs_core_disable_power_collapse); + +static void reset_work(struct work_struct *work) +{ + if (q6core_is_adsp_ready()) { + avcs_core_disable_power_collapse(1); + avtimer.num_retries = Q6_READY_MAX_RETRIES; + return; + } + pr_debug("%s:Q6 not ready-retry after sometime\n", __func__); + if (--avtimer.num_retries > 0) { + schedule_delayed_work(&avtimer.ssr_dwork, + msecs_to_jiffies(Q6_READY_RETRY)); + } else { + pr_err("%s: Q6 failed responding after multiple retries\n", + __func__); + avtimer.num_retries = Q6_READY_MAX_RETRIES; + } +} + +int avcs_core_query_timer(uint64_t *avtimer_tick) +{ + uint32_t avtimer_msw = 0, avtimer_lsw = 0; + uint64_t avtimer_tick_temp; + + if (!atomic_read(&avtimer.adsp_ready)) { + pr_debug("%s:In SSR, return\n", __func__); + return -ENETRESET; + } + avtimer_lsw = ioread32(avtimer.p_avtimer_lsw); + avtimer_msw = ioread32(avtimer.p_avtimer_msw); + + avtimer_tick_temp = (uint64_t)((uint64_t)avtimer_msw << 32) + | avtimer_lsw; + *avtimer_tick = mul_u64_u32_div(avtimer_tick_temp, avtimer.clk_mult, + avtimer.clk_div); + pr_debug_ratelimited("%s:Avtimer: msw: %u, lsw: %u, tick: %llu\n", + __func__, + avtimer_msw, avtimer_lsw, *avtimer_tick); + return 0; +} +EXPORT_SYMBOL(avcs_core_query_timer); + +static int avtimer_open(struct inode *inode, struct file *file) +{ + return avcs_core_disable_power_collapse(1); +} + +static int avtimer_release(struct inode *inode, struct file *file) +{ + return avcs_core_disable_power_collapse(0); +} + +/* + * ioctl call provides GET_AVTIMER + */ +static long avtimer_ioctl(struct file *file, unsigned int ioctl_num, + unsigned long ioctl_param) +{ + switch (ioctl_num) { + case IOCTL_GET_AVTIMER_TICK: + { + uint64_t avtimer_tick = 0; + int rc; + + rc = avcs_core_query_timer(&avtimer_tick); + + if (rc) { + pr_err("%s: Error: Invalid AV Timer tick, rc = %d\n", + __func__, rc); + return rc; + } + + pr_debug_ratelimited("%s: AV Timer tick: time %llx\n", + __func__, avtimer_tick); + if (copy_to_user((void *) ioctl_param, &avtimer_tick, + sizeof(avtimer_tick))) { + pr_err("%s: copy_to_user failed\n", __func__); + return -EFAULT; + } + } + break; + + default: + pr_err("%s: invalid cmd\n", __func__); + return -EINVAL; + } + return 0; +} + +static const struct file_operations avtimer_fops = { + .unlocked_ioctl = avtimer_ioctl, + .compat_ioctl = avtimer_ioctl, + .open = avtimer_open, + .release = avtimer_release +}; + +static int dev_avtimer_probe(struct platform_device *pdev) +{ + int result = 0; + dev_t dev = MKDEV(major, 0); + struct device *device_handle; + struct resource *reg_lsb = NULL, *reg_msb = NULL; + uint32_t clk_div_val; + uint32_t clk_mult_val; + + if (!pdev) { + pr_err("%s: Invalid params\n", __func__); + return -EINVAL; + } + reg_lsb = platform_get_resource_byname(pdev, + IORESOURCE_MEM, "avtimer_lsb_addr"); + if (!reg_lsb) { + dev_err(&pdev->dev, "%s: Looking up %s property", + "avtimer_lsb_addr", __func__); + return -EINVAL; + } + reg_msb = platform_get_resource_byname(pdev, + IORESOURCE_MEM, "avtimer_msb_addr"); + if (!reg_msb) { + dev_err(&pdev->dev, "%s: Looking up %s property", + "avtimer_msb_addr", __func__); + return -EINVAL; + } + INIT_DELAYED_WORK(&avtimer.ssr_dwork, reset_work); + + avtimer.p_avtimer_lsw = devm_ioremap_nocache(&pdev->dev, + reg_lsb->start, resource_size(reg_lsb)); + if (!avtimer.p_avtimer_lsw) { + dev_err(&pdev->dev, "%s: ioremap failed for lsb avtimer register", + __func__); + return -ENOMEM; + } + + avtimer.p_avtimer_msw = devm_ioremap_nocache(&pdev->dev, + reg_msb->start, resource_size(reg_msb)); + if (!avtimer.p_avtimer_msw) { + dev_err(&pdev->dev, "%s: ioremap failed for msb avtimer register", + __func__); + goto unmap; + } + avtimer.num_retries = Q6_READY_MAX_RETRIES; + /* get the device number */ + if (major) + result = register_chrdev_region(dev, 1, DEVICE_NAME); + else { + result = alloc_chrdev_region(&dev, 0, 1, DEVICE_NAME); + major = MAJOR(dev); + } + + if (result < 0) { + dev_err(&pdev->dev, "%s: Registering avtimer device failed\n", + __func__); + goto unmap; + } + + avtimer.avtimer_class = class_create(THIS_MODULE, "avtimer"); + if (IS_ERR(avtimer.avtimer_class)) { + result = PTR_ERR(avtimer.avtimer_class); + dev_err(&pdev->dev, "%s: Error creating avtimer class: %d\n", + __func__, result); + goto unregister_chrdev_region; + } + + cdev_init(&avtimer.myc, &avtimer_fops); + result = cdev_add(&avtimer.myc, dev, 1); + + if (result < 0) { + dev_err(&pdev->dev, "%s: Registering file operations failed\n", + __func__); + goto class_destroy; + } + + device_handle = device_create(avtimer.avtimer_class, + NULL, avtimer.myc.dev, NULL, "avtimer"); + if (IS_ERR(device_handle)) { + result = PTR_ERR(device_handle); + pr_err("%s: device_create failed: %d\n", __func__, result); + goto class_destroy; + } + init_waitqueue_head(&avtimer.adsp_resp_wait); + mutex_init(&avtimer.avtimer_lock); + avtimer.avtimer_open_cnt = 0; + + pr_debug("%s: Device create done for avtimer major=%d\n", + __func__, major); + + if (of_property_read_u32(pdev->dev.of_node, + "qcom,clk-div", &clk_div_val)) + avtimer.clk_div = 1; + else + avtimer.clk_div = clk_div_val; + + if (of_property_read_u32(pdev->dev.of_node, + "qcom,clk-mult", &clk_mult_val)) + avtimer.clk_mult = 1; + else + avtimer.clk_mult = clk_mult_val; + + pr_debug("%s: avtimer.clk_div = %d, avtimer.clk_mult = %d\n", + __func__, avtimer.clk_div, avtimer.clk_mult); + return 0; + +class_destroy: + class_destroy(avtimer.avtimer_class); +unregister_chrdev_region: + unregister_chrdev_region(MKDEV(major, 0), 1); +unmap: + if (avtimer.p_avtimer_lsw) + devm_iounmap(&pdev->dev, avtimer.p_avtimer_lsw); + if (avtimer.p_avtimer_msw) + devm_iounmap(&pdev->dev, avtimer.p_avtimer_msw); + avtimer.p_avtimer_lsw = NULL; + avtimer.p_avtimer_msw = NULL; + return result; + +} + +static int dev_avtimer_remove(struct platform_device *pdev) +{ + pr_debug("%s: dev_avtimer_remove\n", __func__); + + if (avtimer.p_avtimer_lsw) + devm_iounmap(&pdev->dev, avtimer.p_avtimer_lsw); + if (avtimer.p_avtimer_msw) + devm_iounmap(&pdev->dev, avtimer.p_avtimer_msw); + device_destroy(avtimer.avtimer_class, avtimer.myc.dev); + cdev_del(&avtimer.myc); + class_destroy(avtimer.avtimer_class); + unregister_chrdev_region(MKDEV(major, 0), 1); + + return 0; +} + +static const struct of_device_id avtimer_machine_of_match[] = { + { .compatible = "qcom,avtimer", }, + {}, +}; +static struct platform_driver dev_avtimer_driver = { + .probe = dev_avtimer_probe, + .remove = dev_avtimer_remove, + .driver = { + .name = "dev_avtimer", + .of_match_table = avtimer_machine_of_match, + }, +}; + +static int __init avtimer_init(void) +{ + s32 rc; + + rc = platform_driver_register(&dev_avtimer_driver); + if (rc < 0) { + pr_err("%s: platform_driver_register failed\n", __func__); + goto error_platform_driver; + } + pr_debug("%s: dev_avtimer_init : done\n", __func__); + + return 0; +error_platform_driver: + + pr_err("%s: encounterd error\n", __func__); + return rc; +} + +static void __exit avtimer_exit(void) +{ + pr_debug("%s: avtimer_exit\n", __func__); + platform_driver_unregister(&dev_avtimer_driver); +} + +module_init(avtimer_init); +module_exit(avtimer_exit); + +MODULE_DESCRIPTION("avtimer driver"); +MODULE_LICENSE("GPL v2"); diff --git a/dsp/cdsp-loader.c b/dsp/cdsp-loader.c deleted file mode 100644 index 70977d36f0..0000000000 --- a/dsp/cdsp-loader.c +++ /dev/null @@ -1,280 +0,0 @@ -/* - * Copyright (c) 2012-2014, 2017, The Linux Foundation. All rights reserved. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 and - * only version 2 as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#define BOOT_CMD 1 -#define IMAGE_UNLOAD_CMD 0 - -#define CDSP_SUBSYS_DOWN 0 -#define CDSP_SUBSYS_LOADED 1 - -static ssize_t cdsp_boot_store(struct kobject *kobj, - struct kobj_attribute *attr, - const char *buf, size_t count); - -struct cdsp_loader_private { - void *pil_h; - struct kobject *boot_cdsp_obj; - struct attribute_group *attr_group; -}; - -static struct kobj_attribute cdsp_boot_attribute = - __ATTR(boot, 0220, NULL, cdsp_boot_store); - -static struct attribute *attrs[] = { - &cdsp_boot_attribute.attr, - NULL, -}; - -static u32 cdsp_state = CDSP_SUBSYS_DOWN; -static struct platform_device *cdsp_private; -static struct work_struct cdsp_ldr_work; -static void cdsp_loader_unload(struct platform_device *pdev); - -static void cdsp_load_fw(struct work_struct *cdsp_ldr_work) -{ - struct platform_device *pdev = cdsp_private; - struct cdsp_loader_private *priv = NULL; - - int rc = 0; - const char *img_name; - - if (!pdev) { - dev_err(&pdev->dev, "%s: Platform device null\n", __func__); - goto fail; - } - - if (!pdev->dev.of_node) { - dev_err(&pdev->dev, - "%s: Device tree information missing\n", __func__); - - goto fail; - } - - rc = of_property_read_string(pdev->dev.of_node, - "qcom,proc-img-to-load", - &img_name); - if (rc) - goto fail; - - if (!strcmp(img_name, "cdsp")) { - /* cdsp_state always returns "0".*/ - if (cdsp_state == CDSP_SUBSYS_DOWN) { - priv = platform_get_drvdata(pdev); - if (!priv) { - dev_err(&pdev->dev, - " %s: Private data get failed\n", __func__); - goto fail; - } - - priv->pil_h = subsystem_get("cdsp"); - if (IS_ERR(priv->pil_h)) { - dev_err(&pdev->dev, "%s: pil get failed,\n", - __func__); - goto fail; - } - - /* Set the state of the CDSP.*/ - cdsp_state = CDSP_SUBSYS_LOADED; - } else if (cdsp_state == CDSP_SUBSYS_LOADED) { - dev_dbg(&pdev->dev, - "%s: CDSP state = %x\n", __func__, cdsp_state); - } - - dev_dbg(&pdev->dev, "%s: CDSP image is loaded\n", __func__); - return; - } - -fail: - dev_err(&pdev->dev, "%s: CDSP image loading failed\n", __func__); -} - -static void cdsp_loader_do(struct platform_device *pdev) -{ - schedule_work(&cdsp_ldr_work); -} - -static ssize_t cdsp_boot_store(struct kobject *kobj, - struct kobj_attribute *attr, - const char *buf, - size_t count) -{ - int boot = 0, ret = 0; - - ret = sscanf(buf, "%du", &boot); - - if (ret != 1) - pr_debug("%s: invalid arguments for cdsp_loader.\n", __func__); - - if (boot == BOOT_CMD) { - pr_debug("%s: going to call cdsp_loader_do\n", __func__); - cdsp_loader_do(cdsp_private); - } else if (boot == IMAGE_UNLOAD_CMD) { - pr_debug("%s: going to call cdsp_unloader\n", __func__); - cdsp_loader_unload(cdsp_private); - } - return count; -} - -static void cdsp_loader_unload(struct platform_device *pdev) -{ - struct cdsp_loader_private *priv = NULL; - - priv = platform_get_drvdata(pdev); - - if (!priv) - return; - - if (priv->pil_h) { - dev_dbg(&pdev->dev, "%s: calling subsystem put\n", __func__); - subsystem_put(priv->pil_h); - priv->pil_h = NULL; - } -} - -static int cdsp_loader_init_sysfs(struct platform_device *pdev) -{ - int ret = -EINVAL; - struct cdsp_loader_private *priv = NULL; - - cdsp_private = NULL; - - priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); - if (!priv) { - ret = -ENOMEM; - return ret; - } - - platform_set_drvdata(pdev, priv); - - priv->pil_h = NULL; - priv->boot_cdsp_obj = NULL; - priv->attr_group = devm_kzalloc(&pdev->dev, - sizeof(*(priv->attr_group)), - GFP_KERNEL); - if (!priv->attr_group) { - dev_err(&pdev->dev, "%s: malloc attr_group failed\n", - __func__); - ret = -ENOMEM; - goto error_return; - } - - priv->attr_group->attrs = attrs; - - priv->boot_cdsp_obj = kobject_create_and_add("boot_cdsp", kernel_kobj); - if (!priv->boot_cdsp_obj) { - dev_err(&pdev->dev, "%s: sysfs create and add failed\n", - __func__); - ret = -ENOMEM; - goto error_return; - } - - ret = sysfs_create_group(priv->boot_cdsp_obj, priv->attr_group); - if (ret) { - dev_err(&pdev->dev, "%s: sysfs create group failed %d\n", - __func__, ret); - goto error_return; - } - - cdsp_private = pdev; - - return 0; - -error_return: - - if (priv->boot_cdsp_obj) { - kobject_del(priv->boot_cdsp_obj); - priv->boot_cdsp_obj = NULL; - } - - return ret; -} - -static int cdsp_loader_remove(struct platform_device *pdev) -{ - struct cdsp_loader_private *priv = NULL; - - priv = platform_get_drvdata(pdev); - - if (!priv) - return 0; - - if (priv->pil_h) { - subsystem_put(priv->pil_h); - priv->pil_h = NULL; - } - - if (priv->boot_cdsp_obj) { - sysfs_remove_group(priv->boot_cdsp_obj, priv->attr_group); - kobject_del(priv->boot_cdsp_obj); - priv->boot_cdsp_obj = NULL; - } - - return 0; -} - -static int cdsp_loader_probe(struct platform_device *pdev) -{ - int ret = cdsp_loader_init_sysfs(pdev); - - if (ret != 0) { - dev_err(&pdev->dev, "%s: Error in initing sysfs\n", __func__); - return ret; - } - - INIT_WORK(&cdsp_ldr_work, cdsp_load_fw); - - return 0; -} - -static const struct of_device_id cdsp_loader_dt_match[] = { - { .compatible = "qcom,cdsp-loader" }, - { } -}; -MODULE_DEVICE_TABLE(of, cdsp_loader_dt_match); - -static struct platform_driver cdsp_loader_driver = { - .driver = { - .name = "cdsp-loader", - .owner = THIS_MODULE, - .of_match_table = cdsp_loader_dt_match, - }, - .probe = cdsp_loader_probe, - .remove = cdsp_loader_remove, -}; - -static int __init cdsp_loader_init(void) -{ - return platform_driver_register(&cdsp_loader_driver); -} -module_init(cdsp_loader_init); - -static void __exit cdsp_loader_exit(void) -{ - platform_driver_unregister(&cdsp_loader_driver); -} -module_exit(cdsp_loader_exit); - -MODULE_DESCRIPTION("CDSP Loader module"); -MODULE_LICENSE("GPL v2"); diff --git a/ipc/Makefile b/ipc/Makefile index fd840c94d1..69389153b7 100644 --- a/ipc/Makefile +++ b/ipc/Makefile @@ -1,2 +1,3 @@ obj-$(CONFIG_MSM_QDSP6_APRV2_GLINK) += apr.o apr_v2.o apr_tal_glink.o obj-$(CONFIG_MSM_QDSP6_APRV3_GLINK) += apr.o apr_v3.o apr_tal_glink.o +obj-$(CONFIG_WCD_DSP_GLINK) += wcd-dsp-glink.o diff --git a/ipc/wcd-dsp-glink.c b/ipc/wcd-dsp-glink.c new file mode 100644 index 0000000000..870b9f7455 --- /dev/null +++ b/ipc/wcd-dsp-glink.c @@ -0,0 +1,1201 @@ +/* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "sound/wcd-dsp-glink.h" + +#define WDSP_GLINK_DRIVER_NAME "wcd-dsp-glink" +#define WDSP_MAX_WRITE_SIZE (256 * 1024) +#define WDSP_MAX_READ_SIZE (4 * 1024) +#define WDSP_MAX_NO_OF_INTENTS (20) +#define WDSP_MAX_NO_OF_CHANNELS (10) +#define WDSP_WRITE_PKT_SIZE (sizeof(struct wdsp_write_pkt)) +#define WDSP_REG_PKT_SIZE (sizeof(struct wdsp_reg_pkt)) +#define WDSP_CMD_PKT_SIZE (sizeof(struct wdsp_cmd_pkt)) +#define WDSP_CH_CFG_SIZE (sizeof(struct wdsp_glink_ch_cfg)) + +#define MINOR_NUMBER_COUNT 1 +#define WDSP_EDGE "wdsp" +#define RESP_QUEUE_SIZE 3 +#define QOS_PKT_SIZE 1024 +#define TIMEOUT_MS 1000 + +struct wdsp_glink_dev { + struct class *cls; + struct device *dev; + struct cdev cdev; + dev_t dev_num; +}; + +struct wdsp_glink_rsp_que { + /* Size of valid data in buffer */ + u32 buf_size; + + /* Response buffer */ + u8 buf[WDSP_MAX_READ_SIZE]; +}; + +struct wdsp_glink_tx_buf { + struct work_struct tx_work; + struct work_struct free_tx_work; + + /* Glink channel information */ + struct wdsp_glink_ch *ch; + + /* Tx buffer to send to glink */ + u8 buf[0]; +}; + +struct wdsp_glink_ch { + struct wdsp_glink_priv *wpriv; + + /* Glink channel handle */ + void *handle; + + /* Channel states like connect, disconnect */ + int channel_state; + struct mutex mutex; + + /* To free up the channel memory */ + bool free_mem; + + /* Glink local channel open work */ + struct work_struct lcl_ch_open_wrk; + + /* Glink local channel close work */ + struct work_struct lcl_ch_cls_wrk; + + /* Wait for ch connect state before sending any command */ + wait_queue_head_t ch_connect_wait; + + /* + * Glink channel configuration. This has to be the last + * member of the strucuture as it has variable size + */ + struct wdsp_glink_ch_cfg ch_cfg; +}; + +struct wdsp_glink_state { + /* Glink link state information */ + enum glink_link_state link_state; + void *handle; +}; + +struct wdsp_glink_priv { + /* Respone buffer related */ + u8 rsp_cnt; + struct wdsp_glink_rsp_que rsp[RESP_QUEUE_SIZE]; + struct completion rsp_complete; + struct mutex rsp_mutex; + + /* Glink channel related */ + struct mutex glink_mutex; + struct wdsp_glink_state glink_state; + struct wdsp_glink_ch **ch; + u8 no_of_channels; + struct work_struct ch_open_cls_wrk; + struct workqueue_struct *work_queue; + + wait_queue_head_t link_state_wait; + + struct device *dev; +}; + +static int wdsp_glink_close_ch(struct wdsp_glink_ch *ch); +static int wdsp_glink_open_ch(struct wdsp_glink_ch *ch); + +/* + * wdsp_glink_free_tx_buf_work - Work function to free tx pkt + * work: Work structure + */ +static void wdsp_glink_free_tx_buf_work(struct work_struct *work) +{ + struct wdsp_glink_tx_buf *tx_buf; + + tx_buf = container_of(work, struct wdsp_glink_tx_buf, + free_tx_work); + vfree(tx_buf); +} + +/* + * wdsp_glink_free_tx_buf - Function to free tx buffer + * priv: Pointer to the channel + * pkt_priv: Pointer to the tx buffer + */ +static void wdsp_glink_free_tx_buf(const void *priv, const void *pkt_priv) +{ + struct wdsp_glink_tx_buf *tx_buf = (struct wdsp_glink_tx_buf *)pkt_priv; + struct wdsp_glink_priv *wpriv; + struct wdsp_glink_ch *ch; + + if (!priv) { + pr_err("%s: Invalid priv\n", __func__); + return; + } + if (!tx_buf) { + pr_err("%s: Invalid tx_buf\n", __func__); + return; + } + + ch = (struct wdsp_glink_ch *)priv; + wpriv = ch->wpriv; + /* Work queue to free tx pkt */ + INIT_WORK(&tx_buf->free_tx_work, wdsp_glink_free_tx_buf_work); + queue_work(wpriv->work_queue, &tx_buf->free_tx_work); +} + +/* + * wdsp_glink_notify_rx - Glink notify rx callback for responses + * handle: Opaque Channel handle returned by GLink + * priv: Private pointer to the channel + * pkt_priv: Private pointer to the packet + * ptr: Pointer to the Rx data + * size: Size of the Rx data + */ +static void wdsp_glink_notify_rx(void *handle, const void *priv, + const void *pkt_priv, const void *ptr, + size_t size) +{ + u8 *rx_buf; + u8 rsp_cnt; + struct wdsp_glink_ch *ch; + struct wdsp_glink_priv *wpriv; + + if (!ptr || !priv) { + pr_err("%s: Invalid parameters\n", __func__); + return; + } + + ch = (struct wdsp_glink_ch *)priv; + wpriv = ch->wpriv; + rx_buf = (u8 *)ptr; + if (size > WDSP_MAX_READ_SIZE) { + dev_err(wpriv->dev, "%s: Size %zd is greater than allowed %d\n", + __func__, size, WDSP_MAX_READ_SIZE); + size = WDSP_MAX_READ_SIZE; + } + + mutex_lock(&wpriv->rsp_mutex); + rsp_cnt = wpriv->rsp_cnt; + if (rsp_cnt >= RESP_QUEUE_SIZE) { + dev_err(wpriv->dev, "%s: Resp Queue is Full\n", __func__); + rsp_cnt = 0; + } + dev_dbg(wpriv->dev, "%s: copy into buffer %d\n", __func__, rsp_cnt); + + memcpy(wpriv->rsp[rsp_cnt].buf, rx_buf, size); + wpriv->rsp[rsp_cnt].buf_size = size; + wpriv->rsp_cnt = ++rsp_cnt; + mutex_unlock(&wpriv->rsp_mutex); + + glink_rx_done(handle, ptr, true); + complete(&wpriv->rsp_complete); +} + +/* + * wdsp_glink_notify_tx_done - Glink notify tx done callback to + * free tx buffer + * handle: Opaque Channel handle returned by GLink + * priv: Private pointer to the channel + * pkt_priv: Private pointer to the packet + * ptr: Pointer to the Tx data + */ +static void wdsp_glink_notify_tx_done(void *handle, const void *priv, + const void *pkt_priv, const void *ptr) +{ + wdsp_glink_free_tx_buf(priv, pkt_priv); +} +/* + * wdsp_glink_notify_tx_abort - Glink notify tx abort callback to + * free tx buffer + * handle: Opaque Channel handle returned by GLink + * priv: Private pointer to the channel + * pkt_priv: Private pointer to the packet + */ +static void wdsp_glink_notify_tx_abort(void *handle, const void *priv, + const void *pkt_priv) +{ + wdsp_glink_free_tx_buf(priv, pkt_priv); +} + +/* + * wdsp_glink_notify_rx_intent_req - Glink notify rx intent request callback + * to queue buffer to receive from remote client + * handle: Opaque channel handle returned by GLink + * priv: Private pointer to the channel + * req_size: Size of intent to be queued + */ +static bool wdsp_glink_notify_rx_intent_req(void *handle, const void *priv, + size_t req_size) +{ + struct wdsp_glink_priv *wpriv; + struct wdsp_glink_ch *ch; + int rc = 0; + bool ret = false; + + if (!priv) { + pr_err("%s: Invalid priv\n", __func__); + goto done; + } + if (req_size > WDSP_MAX_READ_SIZE) { + pr_err("%s: Invalid req_size %zd\n", __func__, req_size); + goto done; + } + + ch = (struct wdsp_glink_ch *)priv; + wpriv = ch->wpriv; + + dev_dbg(wpriv->dev, "%s: intent size %zd requested for ch name %s", + __func__, req_size, ch->ch_cfg.name); + + mutex_lock(&ch->mutex); + rc = glink_queue_rx_intent(ch->handle, ch, req_size); + if (rc < 0) { + dev_err(wpriv->dev, "%s: Failed to queue rx intent, rc = %d\n", + __func__, rc); + mutex_unlock(&ch->mutex); + goto done; + } + mutex_unlock(&ch->mutex); + ret = true; + +done: + return ret; +} + +/* + * wdsp_glink_lcl_ch_open_wrk - Work function to open channel again + * when local disconnect event happens + * work: Work structure + */ +static void wdsp_glink_lcl_ch_open_wrk(struct work_struct *work) +{ + struct wdsp_glink_ch *ch; + + ch = container_of(work, struct wdsp_glink_ch, + lcl_ch_open_wrk); + + wdsp_glink_open_ch(ch); +} + +/* + * wdsp_glink_lcl_ch_cls_wrk - Work function to close channel locally + * when remote disconnect event happens + * work: Work structure + */ +static void wdsp_glink_lcl_ch_cls_wrk(struct work_struct *work) +{ + struct wdsp_glink_ch *ch; + + ch = container_of(work, struct wdsp_glink_ch, + lcl_ch_cls_wrk); + + wdsp_glink_close_ch(ch); +} + +/* + * wdsp_glink_notify_state - Glink channel state information event callback + * handle: Opaque Channel handle returned by GLink + * priv: Private pointer to the channel + * event: channel state event + */ +static void wdsp_glink_notify_state(void *handle, const void *priv, + unsigned int event) +{ + struct wdsp_glink_priv *wpriv; + struct wdsp_glink_ch *ch; + int i, ret = 0; + + if (!priv) { + pr_err("%s: Invalid priv\n", __func__); + return; + } + + ch = (struct wdsp_glink_ch *)priv; + wpriv = ch->wpriv; + + mutex_lock(&ch->mutex); + ch->channel_state = event; + if (event == GLINK_CONNECTED) { + dev_dbg(wpriv->dev, "%s: glink channel: %s connected\n", + __func__, ch->ch_cfg.name); + + for (i = 0; i < ch->ch_cfg.no_of_intents; i++) { + dev_dbg(wpriv->dev, "%s: intent_size = %d\n", __func__, + ch->ch_cfg.intents_size[i]); + ret = glink_queue_rx_intent(ch->handle, ch, + ch->ch_cfg.intents_size[i]); + if (ret < 0) + dev_warn(wpriv->dev, "%s: Failed to queue intent %d of size %d\n", + __func__, i, + ch->ch_cfg.intents_size[i]); + } + + ret = glink_qos_latency(ch->handle, ch->ch_cfg.latency_in_us, + QOS_PKT_SIZE); + if (ret < 0) + dev_warn(wpriv->dev, "%s: Failed to request qos %d for ch %s\n", + __func__, ch->ch_cfg.latency_in_us, + ch->ch_cfg.name); + + wake_up(&ch->ch_connect_wait); + mutex_unlock(&ch->mutex); + } else if (event == GLINK_LOCAL_DISCONNECTED) { + /* + * Don't use dev_dbg here as dev may not be valid if channel + * closed from driver close. + */ + pr_debug("%s: channel: %s disconnected locally\n", + __func__, ch->ch_cfg.name); + mutex_unlock(&ch->mutex); + + if (ch->free_mem) { + kfree(ch); + ch = NULL; + } + } else if (event == GLINK_REMOTE_DISCONNECTED) { + dev_dbg(wpriv->dev, "%s: remote channel: %s disconnected remotely\n", + __func__, ch->ch_cfg.name); + mutex_unlock(&ch->mutex); + /* + * If remote disconnect happens, local side also has + * to close the channel as per glink design in a + * separate work_queue. + */ + queue_work(wpriv->work_queue, &ch->lcl_ch_cls_wrk); + } +} + +/* + * wdsp_glink_close_ch - Internal function to close glink channel + * ch: Glink Channel structure. + */ +static int wdsp_glink_close_ch(struct wdsp_glink_ch *ch) +{ + struct wdsp_glink_priv *wpriv = ch->wpriv; + int ret = 0; + + mutex_lock(&wpriv->glink_mutex); + if (ch->handle) { + ret = glink_close(ch->handle); + if (ret < 0) { + dev_err(wpriv->dev, "%s: glink_close is failed, ret = %d\n", + __func__, ret); + } else { + ch->handle = NULL; + dev_dbg(wpriv->dev, "%s: ch %s is closed\n", __func__, + ch->ch_cfg.name); + } + } else { + dev_dbg(wpriv->dev, "%s: ch %s is already closed\n", __func__, + ch->ch_cfg.name); + } + mutex_unlock(&wpriv->glink_mutex); + + + return ret; +} + +/* + * wdsp_glink_open_ch - Internal function to open glink channel + * ch: Glink Channel structure. + */ +static int wdsp_glink_open_ch(struct wdsp_glink_ch *ch) +{ + struct wdsp_glink_priv *wpriv = ch->wpriv; + struct glink_open_config open_cfg; + int ret = 0; + + mutex_lock(&wpriv->glink_mutex); + if (!ch->handle) { + memset(&open_cfg, 0, sizeof(open_cfg)); + open_cfg.options = GLINK_OPT_INITIAL_XPORT; + open_cfg.edge = WDSP_EDGE; + open_cfg.notify_rx = wdsp_glink_notify_rx; + open_cfg.notify_tx_done = wdsp_glink_notify_tx_done; + open_cfg.notify_tx_abort = wdsp_glink_notify_tx_abort; + open_cfg.notify_state = wdsp_glink_notify_state; + open_cfg.notify_rx_intent_req = wdsp_glink_notify_rx_intent_req; + open_cfg.priv = ch; + open_cfg.name = ch->ch_cfg.name; + + dev_dbg(wpriv->dev, "%s: ch->ch_cfg.name = %s, latency_in_us = %d, intents = %d\n", + __func__, ch->ch_cfg.name, ch->ch_cfg.latency_in_us, + ch->ch_cfg.no_of_intents); + + ch->handle = glink_open(&open_cfg); + if (IS_ERR_OR_NULL(ch->handle)) { + dev_err(wpriv->dev, "%s: glink_open failed for ch %s\n", + __func__, ch->ch_cfg.name); + ch->handle = NULL; + ret = -EINVAL; + } + } else { + dev_err(wpriv->dev, "%s: ch %s is already opened\n", __func__, + ch->ch_cfg.name); + } + mutex_unlock(&wpriv->glink_mutex); + + return ret; +} + +/* + * wdsp_glink_close_all_ch - Internal function to close all glink channels + * wpriv: Wdsp_glink private structure + */ +static void wdsp_glink_close_all_ch(struct wdsp_glink_priv *wpriv) +{ + int i; + + for (i = 0; i < wpriv->no_of_channels; i++) + if (wpriv->ch && wpriv->ch[i]) + wdsp_glink_close_ch(wpriv->ch[i]); +} + +/* + * wdsp_glink_open_all_ch - Internal function to open all glink channels + * wpriv: Wdsp_glink private structure + */ +static int wdsp_glink_open_all_ch(struct wdsp_glink_priv *wpriv) +{ + int ret = 0, i, j; + + for (i = 0; i < wpriv->no_of_channels; i++) { + if (wpriv->ch && wpriv->ch[i]) { + ret = wdsp_glink_open_ch(wpriv->ch[i]); + if (ret < 0) + goto err_open; + } + } + goto done; + +err_open: + for (j = 0; j < i; j++) + if (wpriv->ch[i]) + wdsp_glink_close_ch(wpriv->ch[j]); + +done: + return ret; +} + +/* + * wdsp_glink_ch_open_wq - Work function to open glink channels + * work: Work structure + */ +static void wdsp_glink_ch_open_cls_wrk(struct work_struct *work) +{ + struct wdsp_glink_priv *wpriv; + + wpriv = container_of(work, struct wdsp_glink_priv, + ch_open_cls_wrk); + + if (wpriv->glink_state.link_state == GLINK_LINK_STATE_DOWN) { + dev_info(wpriv->dev, "%s: GLINK_LINK_STATE_DOWN\n", + __func__); + + wdsp_glink_close_all_ch(wpriv); + } else if (wpriv->glink_state.link_state == GLINK_LINK_STATE_UP) { + dev_info(wpriv->dev, "%s: GLINK_LINK_STATE_UP\n", + __func__); + + wdsp_glink_open_all_ch(wpriv); + } +} + +/* + * wdsp_glink_link_state_cb - Glink link state callback to inform + * about link states + * cb_info: Glink link state callback information structure + * priv: Private structure of link state passed while register + */ +static void wdsp_glink_link_state_cb(struct glink_link_state_cb_info *cb_info, + void *priv) +{ + struct wdsp_glink_priv *wpriv; + + if (!cb_info || !priv) { + pr_err("%s: Invalid parameters\n", __func__); + return; + } + + wpriv = (struct wdsp_glink_priv *)priv; + + mutex_lock(&wpriv->glink_mutex); + wpriv->glink_state.link_state = cb_info->link_state; + wake_up(&wpriv->link_state_wait); + mutex_unlock(&wpriv->glink_mutex); + + queue_work(wpriv->work_queue, &wpriv->ch_open_cls_wrk); +} + +/* + * wdsp_glink_ch_info_init- Internal function to allocate channel memory + * and register with glink + * wpriv: Wdsp_glink private structure. + * pkt: Glink registration packet contains glink channel information. + * pkt_size: Size of the pkt. + */ +static int wdsp_glink_ch_info_init(struct wdsp_glink_priv *wpriv, + struct wdsp_reg_pkt *pkt, size_t pkt_size) +{ + int ret = 0, i, j; + struct glink_link_info link_info; + struct wdsp_glink_ch_cfg *ch_cfg; + struct wdsp_glink_ch **ch; + u8 no_of_channels; + u8 *payload; + u32 ch_size, ch_cfg_size; + size_t size = WDSP_WRITE_PKT_SIZE + WDSP_REG_PKT_SIZE; + + mutex_lock(&wpriv->glink_mutex); + if (wpriv->ch) { + dev_err(wpriv->dev, "%s: glink ch memory is already allocated\n", + __func__); + ret = -EINVAL; + goto done; + } + payload = (u8 *)pkt->payload; + no_of_channels = pkt->no_of_channels; + + if (no_of_channels > WDSP_MAX_NO_OF_CHANNELS) { + dev_err(wpriv->dev, "%s: no_of_channels: %d but max allowed are %d\n", + __func__, no_of_channels, WDSP_MAX_NO_OF_CHANNELS); + ret = -EINVAL; + goto done; + } + ch = kcalloc(no_of_channels, sizeof(struct wdsp_glink_ch *), + GFP_ATOMIC); + if (!ch) { + ret = -ENOMEM; + goto done; + } + wpriv->ch = ch; + wpriv->no_of_channels = no_of_channels; + + for (i = 0; i < no_of_channels; i++) { + ch_cfg = (struct wdsp_glink_ch_cfg *)payload; + + size += WDSP_CH_CFG_SIZE; + if (size > pkt_size) { + dev_err(wpriv->dev, "%s: Invalid size = %zd, pkt_size = %zd\n", + __func__, size, pkt_size); + ret = -EINVAL; + goto err_ch_mem; + } + if (ch_cfg->no_of_intents > WDSP_MAX_NO_OF_INTENTS) { + dev_err(wpriv->dev, "%s: Invalid no_of_intents = %d\n", + __func__, ch_cfg->no_of_intents); + ret = -EINVAL; + goto err_ch_mem; + } + size += (sizeof(u32) * ch_cfg->no_of_intents); + if (size > pkt_size) { + dev_err(wpriv->dev, "%s: Invalid size = %zd, pkt_size = %zd\n", + __func__, size, pkt_size); + ret = -EINVAL; + goto err_ch_mem; + } + + ch_cfg_size = sizeof(struct wdsp_glink_ch_cfg) + + (sizeof(u32) * ch_cfg->no_of_intents); + ch_size = sizeof(struct wdsp_glink_ch) + + (sizeof(u32) * ch_cfg->no_of_intents); + + dev_dbg(wpriv->dev, "%s: channels: %d ch_cfg_size: %d, size: %zd, pkt_size: %zd", + __func__, no_of_channels, ch_cfg_size, size, pkt_size); + + ch[i] = kzalloc(ch_size, GFP_KERNEL); + if (!ch[i]) { + ret = -ENOMEM; + goto err_ch_mem; + } + ch[i]->channel_state = GLINK_LOCAL_DISCONNECTED; + memcpy(&ch[i]->ch_cfg, payload, ch_cfg_size); + payload += ch_cfg_size; + + mutex_init(&ch[i]->mutex); + ch[i]->wpriv = wpriv; + INIT_WORK(&ch[i]->lcl_ch_open_wrk, wdsp_glink_lcl_ch_open_wrk); + INIT_WORK(&ch[i]->lcl_ch_cls_wrk, wdsp_glink_lcl_ch_cls_wrk); + init_waitqueue_head(&ch[i]->ch_connect_wait); + } + + INIT_WORK(&wpriv->ch_open_cls_wrk, wdsp_glink_ch_open_cls_wrk); + + /* Register glink link_state notification */ + link_info.glink_link_state_notif_cb = wdsp_glink_link_state_cb; + link_info.transport = NULL; + link_info.edge = WDSP_EDGE; + + wpriv->glink_state.link_state = GLINK_LINK_STATE_DOWN; + wpriv->glink_state.handle = glink_register_link_state_cb(&link_info, + wpriv); + if (!wpriv->glink_state.handle) { + dev_err(wpriv->dev, "%s: Unable to register wdsp link state\n", + __func__); + ret = -EINVAL; + goto err_ch_mem; + } + goto done; + +err_ch_mem: + for (j = 0; j < i; j++) { + mutex_destroy(&ch[j]->mutex); + kfree(wpriv->ch[j]); + wpriv->ch[j] = NULL; + } + kfree(wpriv->ch); + wpriv->ch = NULL; + wpriv->no_of_channels = 0; + +done: + mutex_unlock(&wpriv->glink_mutex); + return ret; +} + +/* + * wdsp_glink_tx_buf_work - Work queue function to send tx buffer to glink + * work: Work structure + */ +static void wdsp_glink_tx_buf_work(struct work_struct *work) +{ + struct wdsp_glink_priv *wpriv; + struct wdsp_glink_ch *ch; + struct wdsp_glink_tx_buf *tx_buf; + struct wdsp_write_pkt *wpkt; + struct wdsp_cmd_pkt *cpkt; + int ret = 0; + + tx_buf = container_of(work, struct wdsp_glink_tx_buf, + tx_work); + ch = tx_buf->ch; + wpriv = ch->wpriv; + wpkt = (struct wdsp_write_pkt *)tx_buf->buf; + cpkt = (struct wdsp_cmd_pkt *)wpkt->payload; + dev_dbg(wpriv->dev, "%s: ch name = %s, payload size = %d\n", + __func__, cpkt->ch_name, cpkt->payload_size); + + mutex_lock(&tx_buf->ch->mutex); + if (ch->channel_state == GLINK_CONNECTED) { + mutex_unlock(&tx_buf->ch->mutex); + ret = glink_tx(ch->handle, tx_buf, + cpkt->payload, cpkt->payload_size, + GLINK_TX_REQ_INTENT); + if (ret < 0) { + dev_err(wpriv->dev, "%s: glink tx failed, ret = %d\n", + __func__, ret); + /* + * If glink_tx() is failed then free tx_buf here as + * there won't be any tx_done notification to + * free the buffer. + */ + vfree(tx_buf); + } + } else { + mutex_unlock(&tx_buf->ch->mutex); + dev_err(wpriv->dev, "%s: channel %s is not in connected state\n", + __func__, ch->ch_cfg.name); + /* + * Free tx_buf here as there won't be any tx_done + * notification in this case also. + */ + vfree(tx_buf); + } +} + +/* + * wdsp_glink_read - Read API to send the data to userspace + * file: Pointer to the file structure + * buf: Pointer to the userspace buffer + * count: Number bytes to read from the file + * ppos: Pointer to the position into the file + */ +static ssize_t wdsp_glink_read(struct file *file, char __user *buf, + size_t count, loff_t *ppos) +{ + int ret = 0, ret1 = 0; + struct wdsp_glink_rsp_que *rsp; + struct wdsp_glink_priv *wpriv; + + wpriv = (struct wdsp_glink_priv *)file->private_data; + if (!wpriv) { + pr_err("%s: Invalid private data\n", __func__); + ret = -EINVAL; + goto done; + } + + if (count > WDSP_MAX_READ_SIZE) { + dev_info(wpriv->dev, "%s: count = %zd is more than WDSP_MAX_READ_SIZE\n", + __func__, count); + count = WDSP_MAX_READ_SIZE; + } + /* + * Complete signal has given from glink rx notification callback + * or from flush API. Also use interruptible wait_for_completion API + * to allow the system to go in suspend. + */ + ret = wait_for_completion_interruptible(&wpriv->rsp_complete); + if (ret) + goto done; + + mutex_lock(&wpriv->rsp_mutex); + if (wpriv->rsp_cnt) { + wpriv->rsp_cnt--; + dev_dbg(wpriv->dev, "%s: read from buffer %d\n", + __func__, wpriv->rsp_cnt); + + rsp = &wpriv->rsp[wpriv->rsp_cnt]; + if (count < rsp->buf_size) { + ret1 = copy_to_user(buf, &rsp->buf, count); + /* Return the number of bytes copied */ + ret = count; + } else { + ret1 = copy_to_user(buf, &rsp->buf, rsp->buf_size); + /* Return the number of bytes copied */ + ret = rsp->buf_size; + } + + if (ret1) { + mutex_unlock(&wpriv->rsp_mutex); + dev_err(wpriv->dev, "%s: copy_to_user failed %d\n", + __func__, ret); + ret = -EFAULT; + goto done; + } + } else { + /* + * This will execute only if flush API is called or + * something wrong with ref_cnt + */ + dev_dbg(wpriv->dev, "%s: resp count = %d\n", __func__, + wpriv->rsp_cnt); + ret = -EINVAL; + } + mutex_unlock(&wpriv->rsp_mutex); + +done: + return ret; +} + +/* + * wdsp_glink_write - Write API to receive the data from userspace + * file: Pointer to the file structure + * buf: Pointer to the userspace buffer + * count: Number bytes to read from the file + * ppos: Pointer to the position into the file + */ +static ssize_t wdsp_glink_write(struct file *file, const char __user *buf, + size_t count, loff_t *ppos) +{ + int ret = 0, i, tx_buf_size; + struct wdsp_write_pkt *wpkt; + struct wdsp_cmd_pkt *cpkt; + struct wdsp_glink_tx_buf *tx_buf; + struct wdsp_glink_priv *wpriv; + size_t pkt_max_size; + + wpriv = (struct wdsp_glink_priv *)file->private_data; + if (!wpriv) { + pr_err("%s: Invalid private data\n", __func__); + ret = -EINVAL; + goto done; + } + + if ((count < WDSP_WRITE_PKT_SIZE) || + (count > WDSP_MAX_WRITE_SIZE)) { + dev_err(wpriv->dev, "%s: Invalid count = %zd\n", + __func__, count); + ret = -EINVAL; + goto done; + } + + dev_dbg(wpriv->dev, "%s: count = %zd\n", __func__, count); + + tx_buf_size = count + sizeof(struct wdsp_glink_tx_buf); + tx_buf = vzalloc(tx_buf_size); + if (!tx_buf) { + ret = -ENOMEM; + goto done; + } + + ret = copy_from_user(tx_buf->buf, buf, count); + if (ret) { + dev_err(wpriv->dev, "%s: copy_from_user failed %d\n", + __func__, ret); + ret = -EFAULT; + goto free_buf; + } + + wpkt = (struct wdsp_write_pkt *)tx_buf->buf; + switch (wpkt->pkt_type) { + case WDSP_REG_PKT: + if (count < (WDSP_WRITE_PKT_SIZE + WDSP_REG_PKT_SIZE + + WDSP_CH_CFG_SIZE)) { + dev_err(wpriv->dev, "%s: Invalid reg pkt size = %zd\n", + __func__, count); + ret = -EINVAL; + goto free_buf; + } + ret = wdsp_glink_ch_info_init(wpriv, + (struct wdsp_reg_pkt *)wpkt->payload, + count); + if (ret < 0) + dev_err(wpriv->dev, "%s: glink register failed, ret = %d\n", + __func__, ret); + vfree(tx_buf); + break; + case WDSP_READY_PKT: + ret = wait_event_timeout(wpriv->link_state_wait, + (wpriv->glink_state.link_state == + GLINK_LINK_STATE_UP), + msecs_to_jiffies(TIMEOUT_MS)); + if (!ret) { + dev_err(wpriv->dev, "%s: Link state wait timeout\n", + __func__); + ret = -ETIMEDOUT; + goto free_buf; + } + ret = 0; + vfree(tx_buf); + break; + case WDSP_CMD_PKT: + if (count <= (WDSP_WRITE_PKT_SIZE + WDSP_CMD_PKT_SIZE)) { + dev_err(wpriv->dev, "%s: Invalid cmd pkt size = %zd\n", + __func__, count); + ret = -EINVAL; + goto free_buf; + } + mutex_lock(&wpriv->glink_mutex); + if (wpriv->glink_state.link_state == GLINK_LINK_STATE_DOWN) { + mutex_unlock(&wpriv->glink_mutex); + dev_err(wpriv->dev, "%s: Link state is Down\n", + __func__); + + ret = -ENETRESET; + goto free_buf; + } + mutex_unlock(&wpriv->glink_mutex); + cpkt = (struct wdsp_cmd_pkt *)wpkt->payload; + pkt_max_size = sizeof(struct wdsp_write_pkt) + + sizeof(struct wdsp_cmd_pkt) + + cpkt->payload_size; + if (count < pkt_max_size) { + dev_err(wpriv->dev, "%s: Invalid cmd pkt count = %zd, pkt_size = %zd\n", + __func__, count, pkt_max_size); + ret = -EINVAL; + goto free_buf; + } + dev_dbg(wpriv->dev, "%s: requested ch_name: %s, pkt_size: %zd\n", + __func__, cpkt->ch_name, pkt_max_size); + for (i = 0; i < wpriv->no_of_channels; i++) { + if (wpriv->ch && wpriv->ch[i] && + (!strcmp(cpkt->ch_name, + wpriv->ch[i]->ch_cfg.name))) { + tx_buf->ch = wpriv->ch[i]; + break; + } + } + if (!tx_buf->ch) { + dev_err(wpriv->dev, "%s: Failed to get glink channel\n", + __func__); + ret = -EINVAL; + goto free_buf; + } + + ret = wait_event_timeout(tx_buf->ch->ch_connect_wait, + (tx_buf->ch->channel_state == + GLINK_CONNECTED), + msecs_to_jiffies(TIMEOUT_MS)); + if (!ret) { + dev_err(wpriv->dev, "%s: glink channel %s is not in connected state %d\n", + __func__, tx_buf->ch->ch_cfg.name, + tx_buf->ch->channel_state); + ret = -ETIMEDOUT; + goto free_buf; + } + ret = 0; + + INIT_WORK(&tx_buf->tx_work, wdsp_glink_tx_buf_work); + queue_work(wpriv->work_queue, &tx_buf->tx_work); + break; + default: + dev_err(wpriv->dev, "%s: Invalid packet type\n", __func__); + ret = -EINVAL; + vfree(tx_buf); + break; + } + goto done; + +free_buf: + vfree(tx_buf); + +done: + return ret; +} + +/* + * wdsp_glink_open - Open API to initialize private data + * inode: Pointer to the inode structure + * file: Pointer to the file structure + */ +static int wdsp_glink_open(struct inode *inode, struct file *file) +{ + int ret = 0; + struct wdsp_glink_priv *wpriv; + struct wdsp_glink_dev *wdev; + + if (!inode->i_cdev) { + pr_err("%s: cdev is NULL\n", __func__); + ret = -EINVAL; + goto done; + } + wdev = container_of(inode->i_cdev, struct wdsp_glink_dev, cdev); + + wpriv = kzalloc(sizeof(struct wdsp_glink_priv), GFP_KERNEL); + if (!wpriv) { + ret = -ENOMEM; + goto done; + } + wpriv->dev = wdev->dev; + wpriv->work_queue = create_singlethread_workqueue("wdsp_glink_wq"); + if (!wpriv->work_queue) { + dev_err(wpriv->dev, "%s: Error creating wdsp_glink_wq\n", + __func__); + ret = -EINVAL; + goto err_wq; + } + + init_completion(&wpriv->rsp_complete); + init_waitqueue_head(&wpriv->link_state_wait); + mutex_init(&wpriv->rsp_mutex); + mutex_init(&wpriv->glink_mutex); + file->private_data = wpriv; + + goto done; + +err_wq: + kfree(wpriv); + +done: + return ret; +} + +/* + * wdsp_glink_flush - Flush API to unblock read. + * file: Pointer to the file structure + * id: Lock owner ID + */ +static int wdsp_glink_flush(struct file *file, fl_owner_t id) +{ + struct wdsp_glink_priv *wpriv; + + wpriv = (struct wdsp_glink_priv *)file->private_data; + if (!wpriv) { + pr_err("%s: Invalid private data\n", __func__); + return -EINVAL; + } + + complete(&wpriv->rsp_complete); + + return 0; +} + +/* + * wdsp_glink_release - Release API to clean up resources. + * Whenever a file structure is shared across multiple threads, + * release won't be invoked until all copies are closed + * (file->f_count.counter should be 0). If we need to flush pending + * data when any copy is closed, you should implement the flush method. + * + * inode: Pointer to the inode structure + * file: Pointer to the file structure + */ +static int wdsp_glink_release(struct inode *inode, struct file *file) +{ + int i, ret = 0; + struct wdsp_glink_priv *wpriv; + + wpriv = (struct wdsp_glink_priv *)file->private_data; + if (!wpriv) { + pr_err("%s: Invalid private data\n", __func__); + ret = -EINVAL; + goto done; + } + + if (wpriv->glink_state.handle) + glink_unregister_link_state_cb(wpriv->glink_state.handle); + + flush_workqueue(wpriv->work_queue); + destroy_workqueue(wpriv->work_queue); + + /* + * Clean up glink channel memory in channel state + * callback only if close channels are called from here. + */ + if (wpriv->ch) { + for (i = 0; i < wpriv->no_of_channels; i++) { + if (wpriv->ch[i]) { + wpriv->ch[i]->free_mem = true; + /* + * Channel handle NULL means channel is already + * closed. Free the channel memory here itself. + */ + if (!wpriv->ch[i]->handle) { + kfree(wpriv->ch[i]); + wpriv->ch[i] = NULL; + } else { + wdsp_glink_close_ch(wpriv->ch[i]); + } + } + } + + kfree(wpriv->ch); + wpriv->ch = NULL; + } + + mutex_destroy(&wpriv->glink_mutex); + mutex_destroy(&wpriv->rsp_mutex); + kfree(wpriv); + file->private_data = NULL; + +done: + return ret; +} + +static const struct file_operations wdsp_glink_fops = { + .owner = THIS_MODULE, + .open = wdsp_glink_open, + .read = wdsp_glink_read, + .write = wdsp_glink_write, + .flush = wdsp_glink_flush, + .release = wdsp_glink_release, +}; + +/* + * wdsp_glink_probe - Driver probe to expose char device + * pdev: Pointer to device tree data. + */ +static int wdsp_glink_probe(struct platform_device *pdev) +{ + int ret; + struct wdsp_glink_dev *wdev; + + wdev = devm_kzalloc(&pdev->dev, sizeof(*wdev), GFP_KERNEL); + if (!wdev) { + ret = -ENOMEM; + goto done; + } + + ret = alloc_chrdev_region(&wdev->dev_num, 0, MINOR_NUMBER_COUNT, + WDSP_GLINK_DRIVER_NAME); + if (ret < 0) { + dev_err(&pdev->dev, "%s: Failed to alloc char dev, err = %d\n", + __func__, ret); + goto err_chrdev; + } + + wdev->cls = class_create(THIS_MODULE, WDSP_GLINK_DRIVER_NAME); + if (IS_ERR(wdev->cls)) { + ret = PTR_ERR(wdev->cls); + dev_err(&pdev->dev, "%s: Failed to create class, err = %d\n", + __func__, ret); + goto err_class; + } + + wdev->dev = device_create(wdev->cls, NULL, wdev->dev_num, + NULL, WDSP_GLINK_DRIVER_NAME); + if (IS_ERR(wdev->dev)) { + ret = PTR_ERR(wdev->dev); + dev_err(&pdev->dev, "%s: Failed to create device, err = %d\n", + __func__, ret); + goto err_dev_create; + } + + cdev_init(&wdev->cdev, &wdsp_glink_fops); + ret = cdev_add(&wdev->cdev, wdev->dev_num, MINOR_NUMBER_COUNT); + if (ret < 0) { + dev_err(&pdev->dev, "%s: Failed to register char dev, err = %d\n", + __func__, ret); + goto err_cdev_add; + } + platform_set_drvdata(pdev, wdev); + goto done; + +err_cdev_add: + device_destroy(wdev->cls, wdev->dev_num); + +err_dev_create: + class_destroy(wdev->cls); + +err_class: + unregister_chrdev_region(0, MINOR_NUMBER_COUNT); + +err_chrdev: + devm_kfree(&pdev->dev, wdev); + +done: + return ret; +} + +/* + * wdsp_glink_remove - Driver remove to handle cleanup + * pdev: Pointer to device tree data. + */ +static int wdsp_glink_remove(struct platform_device *pdev) +{ + struct wdsp_glink_dev *wdev = platform_get_drvdata(pdev); + + if (wdev) { + cdev_del(&wdev->cdev); + device_destroy(wdev->cls, wdev->dev_num); + class_destroy(wdev->cls); + unregister_chrdev_region(0, MINOR_NUMBER_COUNT); + devm_kfree(&pdev->dev, wdev); + } else { + dev_err(&pdev->dev, "%s: Invalid device data\n", __func__); + } + + return 0; +} + +static const struct of_device_id wdsp_glink_of_match[] = { + {.compatible = "qcom,wcd-dsp-glink"}, + { } +}; +MODULE_DEVICE_TABLE(of, wdsp_glink_of_match); + +static struct platform_driver wdsp_glink_driver = { + .probe = wdsp_glink_probe, + .remove = wdsp_glink_remove, + .driver = { + .name = WDSP_GLINK_DRIVER_NAME, + .owner = THIS_MODULE, + .of_match_table = wdsp_glink_of_match, + }, +}; + +module_platform_driver(wdsp_glink_driver); + +MODULE_DESCRIPTION("SoC WCD_DSP GLINK Driver"); +MODULE_LICENSE("GPL v2");