diff options
Diffstat (limited to 'package/lqtapi/src')
38 files changed, 3809 insertions, 0 deletions
diff --git a/package/lqtapi/src/Makefile b/package/lqtapi/src/Makefile new file mode 100644 index 0000000000..852accb6a1 --- /dev/null +++ b/package/lqtapi/src/Makefile @@ -0,0 +1 @@ +obj-m = tapi/ mps/ diff --git a/package/lqtapi/src/include/linux/tapi/tapi-event.h b/package/lqtapi/src/include/linux/tapi/tapi-event.h new file mode 100644 index 0000000000..925c71369d --- /dev/null +++ b/package/lqtapi/src/include/linux/tapi/tapi-event.h @@ -0,0 +1,37 @@ +#ifndef __LINUX_TAPI_TAPI_EVENT_H__ +#define __LINUX_TAPI_TAPI_EVENT_H__ + + +struct tapi_device; +struct tapi_port; + +struct tapi_hook_event { + bool on; +}; + +struct tapi_dtmf_event { + unsigned char code; +}; + +enum tapi_event_type { + TAPI_EVENT_TYPE_HOOK, + TAPI_EVENT_TYPE_DTMF, +}; + +struct tapi_event { + struct timeval time; + enum tapi_event_type type; + unsigned int port; + union { + struct tapi_hook_event hook; + struct tapi_dtmf_event dtmf; + }; +}; + +void tapi_report_event(struct tapi_device *tdev, struct tapi_event *event); +void tapi_report_hook_event(struct tapi_device *tdev, struct tapi_port *port, + bool on); +void tapi_report_dtmf_event(struct tapi_device *tdev, struct tapi_port *port, + unsigned char code); + +#endif diff --git a/package/lqtapi/src/include/linux/tapi/tapi-ioctl.h b/package/lqtapi/src/include/linux/tapi/tapi-ioctl.h new file mode 100644 index 0000000000..c43fac1e73 --- /dev/null +++ b/package/lqtapi/src/include/linux/tapi/tapi-ioctl.h @@ -0,0 +1,20 @@ + +#include <linux/ioctl.h> + +#define TAPI_MAGIC 't' +#define TAPI_IOCTL(x) _IO(TAPI_MAGIC, (x)) + +#define TAPI_CONTROL_IOCTL_LINK_ALLOC TAPI_IOCTL(0) +#define TAPI_CONTROL_IOCTL_LINK_FREE TAPI_IOCTL(1) +#define TAPI_CONTROL_IOCTL_LINK_ENABLE TAPI_IOCTL(2) +#define TAPI_CONTROL_IOCTL_LINK_DISABLE TAPI_IOCTL(3) + +#define TAPI_CONTROL_IOCTL_SYNC TAPI_IOCTL(4) + +#define TAPI_PORT_IOCTL_GET_ENDPOINT TAPI_IOCTL(5) +#define TAPI_PORT_IOCTL_SET_RING TAPI_IOCTL(6) + +#define TAPI_STREAM_IOCTL_GET_ENDPOINT TAPI_IOCTL(7) +#define TAPI_STREAM_IOCTL_CONFIGURE TAPI_IOCTL(8) +#define TAPI_STREAM_IOCTL_START TAPI_IOCTL(9) +#define TAPI_STREAM_IOCTL_STOP TAPI_IOCTL(10) diff --git a/package/lqtapi/src/include/linux/tapi/tapi.h b/package/lqtapi/src/include/linux/tapi/tapi.h new file mode 100644 index 0000000000..410655d4c9 --- /dev/null +++ b/package/lqtapi/src/include/linux/tapi/tapi.h @@ -0,0 +1,231 @@ +#ifndef __LINUX_TAPI_H__ +#define __LINUX_TAPI_H__ + +#include <linux/kernel.h> +#include <linux/device.h> +#include <linux/mutex.h> + +#include <linux/input.h> + +#include <asm/atomic.h> +#include <linux/list.h> + +#include <linux/cdev.h> + +#include <linux/skbuff.h> +#include <linux/wait.h> + +#include <linux/tapi/tapi-event.h> + +struct tapi_device; + +struct tapi_char_device { + struct tapi_device *tdev; + struct device dev; + struct cdev cdev; +}; + +static inline struct tapi_char_device *cdev_to_tapi_char_device(struct cdev *cdev) +{ + return container_of(cdev, struct tapi_char_device, cdev); +} + +int tapi_char_device_register(struct tapi_device *tdev, + struct tapi_char_device *tchrdev, const struct file_operations *fops); + + +struct tapi_endpoint { + unsigned int id; + void *data; +}; + +static inline void tapi_endpoint_set_data(struct tapi_endpoint *ep, void *data) +{ + ep->data = data; +} + +static inline void *tapi_endpoint_get_data(struct tapi_endpoint *ep) +{ + return ep->data; +} + +struct tapi_port { + unsigned int id; + struct tapi_endpoint ep; + struct input_dev *input; + struct tapi_char_device chrdev; +}; + +struct tapi_stream { + unsigned int id; + struct list_head head; + struct tapi_endpoint ep; + + struct sk_buff_head recv_queue; + wait_queue_head_t recv_wait; + struct sk_buff_head send_queue; +}; + +struct tapi_link { + unsigned int id; + struct list_head head; +}; + +enum tapi_codec { + TAPI_CODEC_L16, +}; + +struct tapi_stream_config { + enum tapi_codec codec; + unsigned int buffer_size; +}; + +struct tapi_ops { + int (*send_dtmf_events)(struct tapi_device *, struct tapi_port *port, + struct tapi_dtmf_event *, size_t num_events, unsigned int dealy); + int (*send_dtmf_event)(struct tapi_device *, struct tapi_port *port, + struct tapi_dtmf_event *); + int (*ring)(struct tapi_device *, struct tapi_port *port, bool ring); + + struct tapi_stream *(*stream_alloc)(struct tapi_device *); + void (*stream_free)(struct tapi_device *, struct tapi_stream *); + int (*stream_configure)(struct tapi_device *, struct tapi_stream *, + struct tapi_stream_config *); + int (*stream_start)(struct tapi_device *, struct tapi_stream *); + int (*stream_stop)(struct tapi_device *, struct tapi_stream *); + int (*stream_send)(struct tapi_device *, struct tapi_stream *, + struct sk_buff *); + + struct tapi_link *(*link_alloc)(struct tapi_device *, + struct tapi_endpoint *ep1, struct tapi_endpoint *ep2); + void (*link_free)(struct tapi_device *, struct tapi_link *); + int (*link_enable)(struct tapi_device *, struct tapi_link *); + int (*link_disable)(struct tapi_device *, struct tapi_link *); + + int (*sync)(struct tapi_device *); +}; + +int tapi_stream_recv(struct tapi_device *, struct tapi_stream *, struct sk_buff *); + +struct tapi_device { + unsigned int id; + + const struct tapi_ops *ops; + unsigned int num_ports; + + struct device dev; + + struct mutex lock; + + struct tapi_port *ports; + struct list_head streams; + struct list_head links; + atomic_t stream_id; + atomic_t link_id; + + struct tapi_char_device stream_dev; + struct tapi_char_device control_dev; +}; + +static inline struct tapi_device *dev_to_tapi(struct device *dev) +{ + return container_of(dev, struct tapi_device, dev); +} + +static inline struct tapi_stream *tapi_stream_from_id(struct tapi_device *tdev, + unsigned int id) +{ + struct tapi_stream *stream; + + mutex_lock(&tdev->lock); + + list_for_each_entry(stream, &tdev->streams, head) { + if (stream->id == id) + goto out; + } + stream = NULL; + +out: + mutex_unlock(&tdev->lock); + return stream; +} + +struct tapi_link *tapi_link_alloc(struct tapi_device *, struct tapi_endpoint *, + struct tapi_endpoint *); +void tapi_link_free(struct tapi_device *, struct tapi_link *); + +struct tapi_stream *tapi_stream_alloc(struct tapi_device *tdev); +void tapi_stream_free(struct tapi_device *tdev, struct tapi_stream *stream); + +static inline int tapi_sync(struct tapi_device *tdev) +{ + if (!tdev->ops || !tdev->ops->sync) + return 0; + + return tdev->ops->sync(tdev); +} + +static inline int tapi_link_enable(struct tapi_device *tdev, + struct tapi_link *link) +{ + if (!tdev->ops || !tdev->ops->link_enable) + return 0; + + return tdev->ops->link_enable(tdev, link); +} + +static inline int tapi_link_disable(struct tapi_device *tdev, + struct tapi_link *link) +{ + if (!tdev->ops || !tdev->ops->link_disable) + return 0; + + return tdev->ops->link_disable(tdev, link); +} + +static inline int tapi_port_send_dtmf(struct tapi_device *tdev, + struct tapi_port *port, struct tapi_dtmf_event *dtmf) +{ + if (!tdev->ops || !tdev->ops->send_dtmf_event) + return -ENOSYS; + + return tdev->ops->send_dtmf_event(tdev, port, dtmf); +} + +static inline int tapi_port_set_ring(struct tapi_device *tdev, + struct tapi_port *port, bool ring) +{ + if (!tdev->ops || !tdev->ops->ring) + return -ENOSYS; + + return tdev->ops->ring(tdev, port, ring); +} + +static inline int tapi_stream_start(struct tapi_device *tdev, +struct tapi_stream *stream) +{ + if (!tdev->ops || !tdev->ops->stream_start) + return -ENOSYS; + + return tdev->ops->stream_start(tdev, stream); +} + +static inline int tapi_stream_stop(struct tapi_device *tdev, +struct tapi_stream *stream) +{ + if (!tdev->ops || !tdev->ops->stream_stop) + return -ENOSYS; + + return tdev->ops->stream_stop(tdev, stream); +} + +int tapi_device_register(struct tapi_device *tdev, const char *name, + struct device *parent); +void tapi_device_unregister(struct tapi_device *tdev); + +struct tapi_sysfs_port; + +struct tapi_sysfs_port *tapi_port_alloc(struct tapi_device *tdev, unsigned int id); +void tapi_port_delete(struct tapi_sysfs_port *); + +#endif diff --git a/package/lqtapi/src/mps/Makefile b/package/lqtapi/src/mps/Makefile new file mode 100644 index 0000000000..201bccc282 --- /dev/null +++ b/package/lqtapi/src/mps/Makefile @@ -0,0 +1,8 @@ + + +mps-objs := mps-core.o mps-irq.o mps-fifo.o +obj-m += mps.o + +vmmc-objs := vmmc-core.o vmmc-alm.o vmmc-module.o vmmc-link.o vmmc-port.o \ + vmmc-signal.o vmmc-stream.o vmmc-coder.o +obj-m += vmmc.o diff --git a/package/lqtapi/src/mps/mps-core.c b/package/lqtapi/src/mps/mps-core.c new file mode 100644 index 0000000000..27f1c63eb6 --- /dev/null +++ b/package/lqtapi/src/mps/mps-core.c @@ -0,0 +1,238 @@ +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/io.h> +#include <linux/platform_device.h> +#include <linux/slab.h> + +#include <ifxmips.h> +#include <ifxmips_cgu.h> + +#include <ifxmips_prom.h> +#include <ifxmips_irq.h> + +#include "mps.h" + +extern int mps_irq_init(struct mps *mps); +extern void mps_irq_exit(struct mps *mps); + +#define MPS_CPU0_BOOT_RVEC 0x1c0 +#define MPS_CPU0_BOOT_NVEC 0x1c4 +#define MPS_CPU0_BOOT_EVEC 0x1c8 +#define MPS_CPU0_CP0_STATUS 0x1cc +#define MPS_CPU0_CP0_EEPC 0x1d0 +#define MPS_CPU0_CP0_EPC 0x1d4 +#define MPS_CPU0_BOOT_SIZE 0x1d8 +#define MPS_CPU0_CFG_STAT 0x1dc +#define MPS_CPU1_BOOT_RVEC 0x1e0 +#define MPS_CPU1_BOOT_NVEC 0x1e4 +#define MPS_CPU1_BOOT_EVEC 0x1e8 +#define MPS_CPU1_CP0_STATUS 0x1ec +#define MPS_CPU1_CP0_EEPC 0x1f0 +#define MPS_CPU1_CP0_EPC 0x1f4 +#define MPS_CPU1_BOOT_SIZE 0x1f8 +#define MPS_CPU1_CFG_STAT 0x1fc + +static void mps_reset(void) +{ + ifxmips_w32(ifxmips_r32(IFXMIPS_RCU_RST) | IFXMIPS_RCU_RST_CPU1, + IFXMIPS_RCU_RST); + smp_wmb(); +} + +static void mps_release(void) +{ + uint32_t val; + val = ifxmips_r32(IFXMIPS_RCU_RST); + val |= 0x20000000; + val &= ~IFXMIPS_RCU_RST_CPU1; + ifxmips_w32(val, IFXMIPS_RCU_RST); + smp_wmb(); +} + +void mps_load_firmware(struct mps *mps, const void *data, size_t size, + enum mps_boot_config config) +{ + uint32_t cfg = 0; + uint32_t fw_size = size; + + if (config == MPS_BOOT_LEGACY) { + cfg = 0x00020000; + fw_size -= sizeof(uint32_t); + } else { + if(config == MPS_BOOT_ENCRYPTED) { + cfg = __raw_readl(mps->mbox_base + MPS_CPU1_CFG_STAT); + cfg |= 0x00700000; + } else { + printk("PANIC!\n"); + } + } + + mps_reset(); + + memcpy_toio(mps->cp1_base, data, size); + + __raw_writel(cfg, mps->mbox_base + MPS_CPU1_CFG_STAT); + __raw_writel(fw_size, mps->mbox_base + MPS_CPU1_BOOT_SIZE); + __raw_writel((uint32_t)mps->cp1_base, mps->mbox_base + MPS_CPU1_BOOT_RVEC); + + mps_release(); +} +EXPORT_SYMBOL_GPL(mps_load_firmware); + +void mps_configure_fifo(struct mps *mps, struct mps_fifo *fifo, + const struct mps_fifo_config *config) +{ + mps_fifo_init(fifo, mps->mbox_base + config->base, + mps->mbox_base + config->head_addr, + mps->mbox_base + config->tail_addr, + config->size); + + __raw_writel(config->size, mps->mbox_base + config->size_addr); + __raw_writel(mps->mbox_res->start + config->base, + mps->mbox_base + config->base_addr); +} +EXPORT_SYMBOL_GPL(mps_configure_fifo); + +void mps_configure_mailbox(struct mps *mps, struct mps_mailbox *mbox, + const struct mps_fifo_config *upstream_config, + const struct mps_fifo_config *downstream_config) +{ + mps_configure_fifo(mps, &mbox->upstream, upstream_config); + mps_configure_fifo(mps, &mbox->downstream, downstream_config); +} +EXPORT_SYMBOL_GPL(mps_configure_mailbox); + +static int __devinit mps_probe(struct platform_device *pdev) +{ + int ret; + struct mps *mps; + struct resource *res; + + mps = kzalloc(sizeof(*mps), GFP_KERNEL); + + if (!mps) + return -ENOMEM; + + mps->dev = &pdev->dev; + + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem"); + if (!res) { + dev_err(&pdev->dev, "Failed to get mem resource"); + ret = -ENOENT; + goto err_free; + } + + res = request_mem_region(res->start, resource_size(res), + dev_name(&pdev->dev)); + + if (!res) { + dev_err(&pdev->dev, "Failed to request mem resource"); + ret = -EBUSY; + goto err_free; + } + + mps->base = ioremap_nocache(res->start, resource_size(res)); + + if (!mps->base) { + dev_err(&pdev->dev, "Failed to ioremap mem region\n"); + ret = -EBUSY; + goto err_release_mem_region; + } + + mps->res = res; + + + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mailbox"); + if (!res) { + dev_err(&pdev->dev, "Failed to get mailbox mem region\n"); + ret = -ENOENT; + goto err_free; + } + + res = request_mem_region(res->start, resource_size(res), + dev_name(&pdev->dev)); + + if (!res) { + dev_err(&pdev->dev, "Failed to request mailbox mem region\n"); + ret = -EBUSY; + goto err_free; + } + + mps->mbox_base = ioremap_nocache(res->start, resource_size(res)); + + if (!mps->mbox_base) { + dev_err(&pdev->dev, "Failed to ioremap mailbox mem region\n"); + ret = -EBUSY; + goto err_release_mem_region; + } + + mps->mbox_res = res; + + mps->cp1_base = ioremap_nocache((unsigned int)pdev->dev.platform_data, 1 << 20); + + if (!mps->cp1_base) { + dev_err(&pdev->dev, "Failed to ioremap cp1 address\n"); + ret = -EBUSY; + goto err_release_mem_region; + } + + mps->irq_ad0 = INT_NUM_IM4_IRL18; + mps->irq_ad1 = INT_NUM_IM4_IRL19; + mps->irq_base = 160; + + ret = mps_irq_init(mps); + if (ret < 0) + goto err_iounmap; + + platform_set_drvdata(pdev, mps); + + return 0; + +err_iounmap: + iounmap(mps->mbox_base); +err_release_mem_region: + release_mem_region(res->start, resource_size(res)); +err_free: + kfree(mps); + + return ret; +} + +static int __devexit mps_remove(struct platform_device *pdev) +{ + struct mps *mps = platform_get_drvdata(pdev); + + mps_irq_exit(mps); + + iounmap(mps->mbox_base); + release_mem_region(mps->mbox_res->start, resource_size(mps->mbox_res)); + iounmap(mps->base); + release_mem_region(mps->res->start, resource_size(mps->res)); + + kfree(mps); + return 0; +} + +static struct platform_driver mps_driver = { + .probe = mps_probe, + .remove = __devexit_p(mps_remove), + .driver = { + .name = "mps", + .owner = THIS_MODULE + }, +}; + +static int __init mps_init(void) +{ + return platform_driver_register(&mps_driver); +} +module_init(mps_init); + +static void __exit mps_exit(void) +{ + platform_driver_unregister(&mps_driver); +} +module_exit(mps_exit); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); diff --git a/package/lqtapi/src/mps/mps-fifo.c b/package/lqtapi/src/mps/mps-fifo.c new file mode 100644 index 0000000000..9d88a45474 --- /dev/null +++ b/package/lqtapi/src/mps/mps-fifo.c @@ -0,0 +1,102 @@ +#include <linux/io.h> + +#include "mps.h" + +void mps_fifo_init(struct mps_fifo *fifo, void __iomem *base, + void __iomem *head_addr, void __iomem *tail_addr, uint32_t size) +{ + fifo->base = base; + fifo->head_addr = head_addr; + fifo->tail_addr = tail_addr; + fifo->size = size; + mps_fifo_reset(fifo); +} + +void mps_fifo_in(struct mps_fifo *fifo, const uint32_t *from, size_t len) +{ + uint32_t head = __raw_readl(fifo->head_addr); + void __iomem *base = fifo->base + head; + size_t i = 0; + size_t byte_len = len * 4; + + if (head < byte_len) { + for(; i <= head / 4; ++i) { + __raw_writel(from[i], base); + base -= 4; + } + + base += fifo->size; + head += fifo->size; + } + + for(; i < len; ++i) { + __raw_writel(from[i], base); + base -= 4; + } + + head -= byte_len; + __raw_writel(head, fifo->head_addr); + +} +EXPORT_SYMBOL_GPL(mps_fifo_in); + +void mps_fifo_out(struct mps_fifo *fifo, uint32_t *to, size_t len) +{ + uint32_t tail = __raw_readl(fifo->tail_addr); + void __iomem *base = fifo->base + tail; + size_t i = 0; + size_t byte_len = len * 4; + + if (tail < byte_len) { + for(; i <= tail / 4; ++i) { + to[i] = __raw_readl(base); + base -= 4; + } + + base += fifo->size; + tail += fifo->size; + } + + for(; i < len; ++i) { + to[i] = __raw_readl(base); + base -= 4; + } + + tail -= byte_len; + __raw_writel(tail, fifo->tail_addr); +} +EXPORT_SYMBOL_GPL(mps_fifo_out); + +uint32_t mps_fifo_peek(struct mps_fifo *fifo) +{ + uint32_t tail = __raw_readl(fifo->tail_addr); + void __iomem *base = fifo->base + tail; + return __raw_readl(base); +} + +void mps_fifo_reset(struct mps_fifo *fifo) +{ + void __iomem *base = fifo->base + fifo->size - 4; + size_t i; + + __raw_writel(fifo->size - 4, fifo->head_addr); + __raw_writel(fifo->size - 4, fifo->tail_addr); + + for(i = 0; i < 16; ++i) { + __raw_writel(0x0, base); + base -= 4; + } +} + +size_t mps_fifo_len(struct mps_fifo *fifo) +{ + uint32_t head = __raw_readl(fifo->head_addr); + uint32_t tail = __raw_readl(fifo->tail_addr); + + if (tail < head) + return head - tail; + else + return fifo->size - (tail - head); +} +EXPORT_SYMBOL_GPL(mps_fifo_len); + diff --git a/package/lqtapi/src/mps/mps-irq.c b/package/lqtapi/src/mps/mps-irq.c new file mode 100644 index 0000000000..8da0c00c78 --- /dev/null +++ b/package/lqtapi/src/mps/mps-irq.c @@ -0,0 +1,157 @@ +#include <linux/interrupt.h> +#include <linux/kernel_stat.h> +#include <ifxmips_irq.h> + +#include "mps.h" +#include "mps-irq.h" + +#define MPS_REG_AD0_IRQ_BASE 0x40 +#define MPS_REG_AD1_IRQ_BASE 0x44 + +#define MPS_REG_AD_IRQ_STATUS 0x00 +#define MPS_REG_AD_IRQ_SET 0x08 +#define MPS_REG_AD_IRQ_CLEAR 0x10 +#define MPS_REG_AD_IRQ_ENABLE 0x18 + +struct mps_irq_desc +{ + void __iomem *base; + unsigned int irq_base; +}; + +static inline unsigned int mps_irq_bit(struct mps_irq_desc *mps_desc, int irq) +{ + return BIT(irq - mps_desc->irq_base); +} + +static void mps_irq_ack(unsigned int irq) +{ + struct mps_irq_desc *mps_desc = get_irq_chip_data(irq); + + __raw_writel(mps_irq_bit(mps_desc, irq), + mps_desc->base + MPS_REG_AD_IRQ_CLEAR); +} + +static void mps_irq_mask(unsigned int irq) +{ + struct mps_irq_desc *mps_desc = get_irq_chip_data(irq); + uint32_t mask; + + mask = __raw_readl(mps_desc->base + MPS_REG_AD_IRQ_ENABLE); + mask &= ~mps_irq_bit(mps_desc, irq); + __raw_writel(mask, mps_desc->base + MPS_REG_AD_IRQ_ENABLE); +} + +static void mps_irq_unmask(unsigned int irq) +{ + struct mps_irq_desc *mps_desc = get_irq_chip_data(irq); + uint32_t mask; + + mask = __raw_readl(mps_desc->base + MPS_REG_AD_IRQ_ENABLE); + mask |= mps_irq_bit(mps_desc, irq) | 0xffff; + __raw_writel(mask, mps_desc->base + MPS_REG_AD_IRQ_ENABLE); +} + +static struct irq_chip mps_irq_chip = { + .name = "mps", + .ack = mps_irq_ack, + .mask = mps_irq_mask, + .unmask = mps_irq_unmask, +}; + +static void mps_irq_demux_handler(unsigned int irq, struct irq_desc *desc) +{ + struct mps_irq_desc *mps_desc = get_irq_data(irq); + uint32_t val; + int mps_irq; + + desc->chip->mask(irq); + + val = __raw_readl(mps_desc->base + MPS_REG_AD_IRQ_STATUS); + mps_irq = ffs(val); + +/* printk("irq: %d %x\n", mps_irq, val);*/ + + if (mps_irq > 16) + printk("PANIC!\n"); + + if (mps_irq) + generic_handle_irq(mps_irq + mps_desc->irq_base - 1); + + desc->chip->ack(irq); + desc->chip->unmask(irq); +} + +#if 0 + +static const uint32_t ring_msg[] = { +0x01010004, 0x00030000, +}; + +static irqreturn_t mps_irq_ad0(int irq, void *devid) +{ + struct mps *mps = devid; + uint32_t val; + + val = __raw_readl(mps->base + MPS_REG_AD0_IRQ_STATUS); + printk("WOHO ein IRQ: %x\n", val); + __raw_writel(val, mps->base + MPS_REG_AD0_IRQ_CLEAR); + + if (val & BIT(MPS_IRQ_DOWNLOAD_DONE)) + complete(&mps->init_completion); + + if (val & BIT(MPS_IRQ_EVENT)) + mps_fifo_in(&mps->mbox_cmd.downstream, ring_msg, ARRAY_SIZE(ring_msg)); + + return IRQ_HANDLED; +} +#endif + +#define MPS_NUM_AD_IRQS 32 + +struct mps_irq_desc mps_irq_descs[2]; + +int mps_irq_init(struct mps *mps) +{ + int ret = 0; + int irq; + + mps_irq_descs[0].base = mps->base + MPS_REG_AD0_IRQ_BASE; + mps_irq_descs[0].irq_base = mps->irq_base; + mps_irq_descs[1].base = mps->base + MPS_REG_AD1_IRQ_BASE; + mps_irq_descs[1].irq_base = mps->irq_base + 16; + + + set_irq_data(mps->irq_ad0, &mps_irq_descs[0]); + set_irq_chained_handler(mps->irq_ad0, mps_irq_demux_handler); + set_irq_data(mps->irq_ad1, &mps_irq_descs[1]); + set_irq_chained_handler(mps->irq_ad1, mps_irq_demux_handler); + +/* + ret = request_irq(mps->irq_ad0, mps_irq_demux_handler, IRQF_DISABLED, + "mps ad0", &mps_irq_descs[0]); + ret = request_irq(mps->irq_ad1, mps_irq_demux_handler, IRQF_DISABLED, + "mps ad0", &mps_irq_descs[1]); +*/ + for (irq = 0; irq < MPS_NUM_AD_IRQS; ++irq) { + set_irq_chip_data(irq + mps->irq_base, &mps_irq_descs[irq / 16]); + set_irq_chip_and_handler(irq + mps->irq_base, &mps_irq_chip, handle_level_irq); + } + + +/* + res = request_irq(INT_NUM_IM4_IRL18, mps_irq_ad0, IRQF_DISABLED, + "mps ad0", mps); + + irqs = BIT(MPS_IRQ_CMD_UPSTREAM) | BIT(MPS_IRQ_DATA_UPSTREAM) + | BIT(MPS_IRQ_DOWNLOAD_DONE) | BIT(MPS_IRQ_EVENT) | BIT(MPS_IRQ_CMD_ERROR); + + __raw_writel(irqs, mps->base + MPS_REG_AD0_IRQ_ENA); +*/ + return ret; +} + +void mps_irq_exit(struct mps *mps) +{ + free_irq(INT_NUM_IM4_IRL18, mps); +} diff --git a/package/lqtapi/src/mps/mps-irq.h b/package/lqtapi/src/mps/mps-irq.h new file mode 100644 index 0000000000..c343ace3fd --- /dev/null +++ b/package/lqtapi/src/mps/mps-irq.h @@ -0,0 +1,18 @@ + +#define MPS_IRQ_BASE 160 +#define MPS_IRQ(x) (MPS_IRQ_BASE + (x)) +#define MPS_IRQ_CMD_UPSTREAM MPS_IRQ(0) +#define MPS_IRQ_DATA_UPSTREAM MPS_IRQ(1) +#define MPS_IRQ_CMD_DOWNSTREAM MPS_IRQ(2) +#define MPS_IRQ_DATA_DOWNSTREAM MPS_IRQ(3) +#define MPS_IRQ_RCV_OVERFLOW MPS_IRQ(4) +#define MPS_IRQ_EVENT MPS_IRQ(5) +#define MPS_IRQ_EVENT_OVERFLOW MPS_IRQ(6) +#define MPS_IRQ_CMD_ERROR MPS_IRQ(8) +#define MPS_IRQ_PCM_CRASH MPS_IRQ(9) +#define MPS_IRQ_DATA_ERROR MPS_IRQ(10) +#define MPS_IRQ_MIPS_OL MPS_IRQ(11) +#define MPS_IRQ_WATCHDOG_FAIL MPS_IRQ(14) +#define MPS_IRQ_DOWNLOAD_DONE MPS_IRQ(15) + + diff --git a/package/lqtapi/src/mps/mps-msg.c b/package/lqtapi/src/mps/mps-msg.c new file mode 100644 index 0000000000..7fc0b7a992 --- /dev/null +++ b/package/lqtapi/src/mps/mps-msg.c @@ -0,0 +1,19 @@ + + +int mps_message_send(struct mps_fifo *fifo, struct mps_message *msg) +{ +} + +int mps_message_recv(struct mps_fifo *fifo, struct mps_message *msg) +{ + mps_fifo_out(fifo, &msg->header, sizeof(msg->header)); + mps_fifo_out(fifo, &msg->data, mps_message_size(msg)); +} + +struct mps_message *msg mps_message_recv_alloc(struct mps_fifo *fifo) +{ + struct mps_message *msg; + + mps_fifo_out(fifo, &msg->header, sizeof(msg->header)); + mps_fifo_out(fifo, &msg->data, mps_message_size(msg)); +} diff --git a/package/lqtapi/src/mps/mps-msg.h b/package/lqtapi/src/mps/mps-msg.h new file mode 100644 index 0000000000..8d672fbf5d --- /dev/null +++ b/package/lqtapi/src/mps/mps-msg.h @@ -0,0 +1,87 @@ +#ifndef __MPS_MSG_H__ +#define __MPS_MSG_H__ + +#include <linux/slab.h> + +struct mps_message { + uint32_t header; + uint32_t data[0]; +}; + +#define MPS_MSG_HEADER(_rw, _sc, _bc, _cmd, _chan, _mode, _ecmd, _length) \ + (((_rw) << 31) | ((_sc) << 30) | ((_bc) << 29) | ((_cmd) << 24) | \ + ((_chan) << 16) | ((_mode) << 13) | ((_ecmd) << 8) | (_length)) + +#define MPS_MSG_INIT(_msg, _rw, _sc, _bc, _cmd, _chan, _mode, _ecmd, _length) \ + do { \ + (_msg)->header = MPS_MSG_HEADER(_rw, _sc, _bc, _cmd, _chan, _mode, \ + _ecmd, _length); \ + } while(0) + + +static inline void mps_msg_init(struct mps_message *msg, uint32_t rw, uint32_t sc, + uint32_t bc, uint32_t cmd, uint32_t chan, uint32_t mode, uint32_t ecmd, + uint32_t length) +{ + msg->header = MPS_MSG_HEADER(rw, sc, bc, cmd, chan, mode, ecmd, length); +} + +#define DECLARE_MESSAGE(_name, _size) struct mps_message _name; \ + uint32_t __mps_msg_data_ ## __FUNCTION__ ## __LINE__[_size] + +static inline struct mps_message *mps_message_alloc(size_t size) +{ + return kmalloc(sizeof(struct mps_message) + size * sizeof(uint32_t), GFP_KERNEL); +} + +static inline size_t mps_message_size(const struct mps_message *msg) +{ + return msg->header & 0xff; +} + +enum { + MPS_MSG_WRITE = 0, + MPS_MSG_READ = 1, +}; + +enum { + MPS_CMD_ALI = 1, + MPS_CMD_DECT = 3, + MPS_CMD_SDD = 4, + MPS_CMD_EOP = 6, +}; + +#define MOD_PCM 0 +#define MOD_SDD 0 +#define MOD_ALI 1 +#define MOD_SIGNALING 2 +#define MOD_CODER 3 +#define MOD_RESOURCE 6 +#define MOD_SYSTEM 7 +#define ECMD_SYS_VER 6 +#define SYS_CAP_ECMD 7 +#define ECMD_CIDS_DATA 9 +#define ECMD_DCCTL_DEBUG 0x0a + +#define ALI_CHAN_CMD 6 +#define ALI_CHAN_ECMD 1 + +#define MPS_MSG_HEADER_W(_sc, _bc, _cmd, _chan, _mode, _ecmd, _length) \ + MPS_MSG_HEADER(MPS_MSG_WRITE, _sc, _bc, _cmd, _chan, _mode, _ecmd, _length) + +#define MPS_MSG_HEADER_R(_sc, _bc, _cmd, _chan, _mode, _ecmd, _length) \ + MPS_MSG_HEADER(MPS_MSG_READ, _sc, _bc, _cmd, _chan, _mode, _ecmd, _length) + +#define MPS_MSG_CMD_EOP(_mode, _ecmd, _length) \ + MPS_MSG_HEADER_R(0, 0, MPS_CMD_EOP, 0, _mode, _ecmd, _length) + +#define MPS_MSG_CMD_EOP_SYSTEM(_ecmd, _length) \ + MPS_MSG_CMD_EOP(MOD_SYSTEM, _ecmd, _length) + +#define MPS_CMD_GET_VERSION \ + MPS_MSG_CMD_EOP_SYSTEM(ECMD_SYS_VER, 4) + +#define MPS_CMD_ALI(_chan) \ + MPS_MSG_HEADER_W(0, 0, ALI_CHAN_CMD, _chan, MOD_ALI, ALI_CHAN_ECMD, 12) + +#endif diff --git a/package/lqtapi/src/mps/mps.h b/package/lqtapi/src/mps/mps.h new file mode 100644 index 0000000000..e759e5698c --- /dev/null +++ b/package/lqtapi/src/mps/mps.h @@ -0,0 +1,85 @@ +#ifndef __MPS_H__ +#define __MPS_H__ + +#include <linux/platform_device.h> +#include <linux/completion.h> + +struct mps_fifo_config +{ + size_t head_addr; + size_t tail_addr; + size_t base_addr; + size_t size_addr; + + size_t base; + size_t size; +}; + +struct mps_fifo { + void __iomem *base; + void __iomem *head_addr; + void __iomem *tail_addr; + uint32_t size; +}; + +struct mps_mailbox { + struct mps_fifo upstream; + struct mps_fifo downstream; +}; + +enum mps_boot_config +{ + MPS_BOOT_LEGACY = 1, + MPS_BOOT_ENCRYPTED = 2, +}; + +struct mps { + struct resource *res; + void __iomem *base; + + struct resource *mbox_res; + void __iomem *mbox_base; + + struct resource *cp1_res; + void __iomem *cp1_base; + + struct device *dev; + + int irq_ad0; + int irq_ad1; + int irq_base; +}; + +void mps_configure_fifo(struct mps *mps, struct mps_fifo *fifo, + const struct mps_fifo_config *config); + +void mps_configure_mailbox(struct mps *mps, struct mps_mailbox *mbox, + const struct mps_fifo_config *upstream_config, + const struct mps_fifo_config *downstream_config); + +void mps_load_firmware(struct mps *mps, const void *data, size_t size, + enum mps_boot_config config); + +static inline struct mps *device_to_mps(struct device *dev) +{ + return (struct mps *)dev_get_drvdata(dev); +} + +/* fifo */ +void mps_fifo_init(struct mps_fifo *fifo, void __iomem *data_addr, + void __iomem *head_addr, void __iomem *tail_addr, uint32_t size); +void mps_fifo_in(struct mps_fifo *fifo, const uint32_t *from, size_t len); +void mps_fifo_out(struct mps_fifo *fifo, uint32_t *to, size_t len); +uint32_t mps_fifo_peek(struct mps_fifo *fifo); +void mps_fifo_reset(struct mps_fifo *fifo); +size_t mps_fifo_len(struct mps_fifo *fifo); + +/* Mailbox */ +int mps_mailbox_init(struct mps_mailbox *mbox, const char *name, int irq); +int mps_mailbox_command_read(struct mps_mailbox *mbox, uint32_t cmd, + uint32_t *result); +int mps_mailbox_command_write(struct mps_mailbox *mbox, uint32_t cmd, + const uint32_t *data); + + +#endif diff --git a/package/lqtapi/src/mps/vmmc-alm.c b/package/lqtapi/src/mps/vmmc-alm.c new file mode 100644 index 0000000000..828376fb91 --- /dev/null +++ b/package/lqtapi/src/mps/vmmc-alm.c @@ -0,0 +1,129 @@ +#include <linux/kernel.h> + +#include "vmmc.h" +#include "vmmc-cmds.h" +#include "vmmc-alm.h" +#include "vmmc-module.h" +#include "mps.h" + +static inline struct vmmc_alm *vmmc_module_to_alm(struct vmmc_module *module) +{ + return container_of(module, struct vmmc_alm, module); +} + +int vmmc_alm_set_state(struct vmmc_alm *alm, enum vmmc_alm_state state) +{ + int ret; + + ret = vmmc_command_write(alm->vmmc, VMMC_CMD_OPMODE(alm->id), &state); + if (!ret) + alm->state = state; + + return ret; +} + +enum vmmc_alm_state vmmc_alm_get_state(struct vmmc_alm *alm) +{ + return alm->state; +} + +static struct vmmc_alm_coef *vmmc_alm_coef_alloc(unsigned int offset, size_t len) +{ + struct vmmc_alm_coef *coef; + + coef = kzalloc(sizeof(*coef) + sizeof(uint32_t) * DIV_ROUND_UP(len, 4), + GFP_KERNEL); + coef->offset = offset; + coef->len = len; + + return coef; +} + +int vmcc_alm_set_coefficents(struct vmmc_alm *alm, + const struct vmmc_alm_coef *coef_list) +{ + int ret = 0; + uint32_t cmd; + struct vmmc_alm_coef *coef; + struct list_head l; + + INIT_LIST_HEAD(&l); + + coef = vmmc_alm_coef_alloc(0x37, 8); + coef->data[0] = 0x76d7871d; + coef->data[1] = 0x7fbb7ff4; + list_add_tail(&coef->list, &l); + coef = vmmc_alm_coef_alloc(0x5e, 2); + coef->data[0] = 0x7e000000; + list_add_tail(&coef->list, &l); + coef = vmmc_alm_coef_alloc(0x6c, 2); + coef->data[0] = 0x7e000000; + list_add_tail(&coef->list, &l); + + list_for_each_entry(coef, &l, list) { + cmd = VMMC_CMD_ALM_COEF(alm->id, coef->offset, coef->len); + ret = vmmc_command_write(alm->vmmc, cmd, coef->data); + if (ret) + break; + } + + return ret; +} + +static int vmmc_alm_sync(struct vmmc_module *module) +{ + struct vmmc_alm *alm = vmmc_module_to_alm(module); + + alm->cmd_cache[0] = VMMC_CMD_ALI_DATA1(1, 0, 0, 1, 0, module->pins[0], 0x4000); + alm->cmd_cache[1] = VMMC_CMD_ALI_DATA2(0x4000, module->pins[1], module->pins[2]); + alm->cmd_cache[2] = VMMC_CMD_ALI_DATA3(module->pins[3], module->pins[4]); + + return vmmc_command_write(alm->vmmc, VMMC_CMD_ALI(alm->id), alm->cmd_cache); +} + +static int vmmc_alm_enable(struct vmmc_module *module, bool enable) +{ + struct vmmc_alm *alm = vmmc_module_to_alm(module); + + return vmmc_command_write(alm->vmmc, VMMC_CMD_ALI(alm->id), alm->cmd_cache); +} + +static const struct vmmc_module_ops vmmc_alm_module_ops = { + .sync = vmmc_alm_sync, + .enable = vmmc_alm_enable, +}; + +int vmmc_alm_init(struct vmmc_alm *alm, struct vmmc *vmmc, unsigned int id) +{ + int ret; + + ret = vmmc_module_init(&alm->module, 5, &vmmc_alm_module_ops); + if (ret) + return ret; + + alm->id = id; + alm->module.id = id + 0x4; + alm->vmmc = vmmc; + + alm->cmd_cache[0] = VMMC_CMD_ALI_DATA1(1, 0, 0, 1, 0, 0, 0x2000); + alm->cmd_cache[1] = VMMC_CMD_ALI_DATA2(0x2000, 0, 0); + alm->cmd_cache[2] = VMMC_CMD_ALI_DATA3(0, 0); + + vmmc_command_write(alm->vmmc, VMMC_CMD_ALI(alm->id), alm->cmd_cache); + vmcc_alm_set_coefficents(alm, NULL); + vmmc_register_module(vmmc, &alm->module); + +// disable lec +// write lec coef +// write nlp coef +// enable lec + +// ALI_LEC ALI_ES RES_LEC_COEF RES_LEC_NLP_COEF + + return ret; +} + +void vmmc_alm_hook_event_handler(struct vmmc *vmmc, uint32_t id, uint32_t data) +{ + tapi_report_hook_event(&vmmc->tdev, &vmmc->tdev.ports[id], data & 1); +} diff --git a/package/lqtapi/src/mps/vmmc-alm.h b/package/lqtapi/src/mps/vmmc-alm.h new file mode 100644 index 0000000000..4e59b6627e --- /dev/null +++ b/package/lqtapi/src/mps/vmmc-alm.h @@ -0,0 +1,42 @@ +#ifndef __VMMC_ALM_H__ +#define __VMMC_ALM_H__ + +#include "vmmc-module.h" + +struct vmmc; +enum vmmc_alm_state { + VMMC_ALM_STATE_OFF = 0 << 16, + VMMC_ALM_STATE_ONHOOK = 1 << 16, + VMMC_ALM_STATE_RING = 3 << 16, + VMMC_ALM_STATE_ACTIVE = 2 << 16, +}; + +struct vmmc_alm { + struct vmmc *vmmc; + unsigned int id; + + enum vmmc_alm_state state; + + struct vmmc_module module; + + uint32_t cmd_cache[3]; +}; + +struct vmmc_alm_coef { + struct list_head list; + unsigned int offset; + size_t len; + uint32_t data[0]; +}; + +int vmmc_alm_init(struct vmmc_alm *alm, struct vmmc *vmmc, unsigned int id); + +int vmmc_alm_set_state(struct vmmc_alm *alm, enum vmmc_alm_state state); +enum vmmc_alm_state vmmc_alm_get_state(struct vmmc_alm *alm); + +int vmcc_alm_set_coefficents(struct vmmc_alm *alm, + const struct vmmc_alm_coef *coef_list); + +void vmmc_alm_hook_event_handler(struct vmmc *vmmc, uint32_t event, uint32_t data); + +#endif diff --git a/package/lqtapi/src/mps/vmmc-cmds.h b/package/lqtapi/src/mps/vmmc-cmds.h new file mode 100644 index 0000000000..e833062c70 --- /dev/null +++ b/package/lqtapi/src/mps/vmmc-cmds.h @@ -0,0 +1,98 @@ +#ifndef __VMMC_CMDS_H__ +#define __VMMC_CMDS_H__ + +#define _VMMC_CMD(_x) ((_x) << 24) +#define _VMMC_MOD(_x) ((_x) << 13) +#define _VMMC_ECMD(_x) ((_x) << 8) + +#define _VMMC_MSG(_cmd, _mod, _ecmd) \ + (_VMMC_CMD(_cmd) | _VMMC_ECMD(_ecmd) | _VMMC_MOD(_mod)) + +#define _VMMC_CHAN(_chan) ((_chan) << 16) +#define _VMMC_LENGTH(_length) ((_length) << 2) + +#define VMMC_CMD_OPMODE(_chan) (_VMMC_MSG(1, 0, 0) | _VMMC_CHAN(_chan) | _VMMC_LENGTH(1)) + +#define VMMC_CMD_SIG(_chan) (_VMMC_MSG(6, 2, 1) | _VMMC_CHAN(_chan) | _VMMC_LENGTH(1)) +#define VMMC_CMD_SIG_DATA(_enable, _event, _rate, _i1, _i2, _mute1, _mute2) \ + (((_enable) << 31) | ((_event) << 30) | ((_i1) << 24) | ((_i2) << 16) | \ + ((_rate) << 23) | ((_mute1) << 15) | ((_mute2) << 14)) + +#define VMMC_CMD_SIG_SET_ENABLE(_data, _enable) (((_data) & ~BIT(31)) | ((_enable) << 31)) +#define VMMC_CMD_SIG_SET_INPUTS(_data, _i1, _i2) (((_data) & ~0x3f3f0000) | \ + ((_i1) << 24) | ((_i2) << 16)) + +#define VMMC_CMD_DTMFR(_chan) (_VMMC_MSG(6, 2, 4) | _VMMC_CHAN(_chan) | _VMMC_LENGTH(1)) +#define VMMC_CMD_DTMFR_DATA(_enable, _event, _nr) \ + (((_enable) << 31) | ((_event) << 30) | (6 << 27) | ((_nr) << 16)) + +#define VMMC_CMD_CODER(_chan) (_VMMC_MSG(6, 3, 1) | _VMMC_CHAN(_chan) | _VMMC_LENGTH(4)) + +#define VMMC_CMD_CODER_DATA1(_enable, _rate, _ns, _pte, _nr, _i1, _hp, _pf, \ + _cng, _bfi, _dec, _im, _pst, _sic, _em, _enc) \ + (((_enable) << 31) | ((_rate) << 30) | ((_ns) << 29) | ((_pte) << 26) | \ + ((_nr) << 22) | ((_i1) << 16) | ((_hp) << 15) | ((_pf) << 14) | \ + ((_cng) << 13) | ((_bfi) << 12) | ((_dec) << 11) | ((_im) << 10) | \ + ((_pst) << 9) | ((_sic) << 8) | ((_em) << 7) | (_enc)) + +#define VMMC_CMD_CODER_DATA2(_gain1, _gain2) (((_gain1) << 16) | (_gain2)) + +#define VMMC_CMD_CODER_DATA3(_de, _ee, _i2, _red, _i3, _plc, _i4, _i5) \ + (((_de) << 31) | ((_ee) << 30) | ((_i2) << 24) | ((_red) << 22) | \ + ((_i3) << 16) | ((_plc) << 15) | ((_i4) << 8) | (_i5)) + +#define VMMC_CMD_SERR_ACK(_chan) _VMMC_MSG(6, 7, 1) | _VMMC_CHAN(_chan) | _VMMC_LENGTH(1) + +#define VMMC_CMD_SERR_ACK_DATA1(_foo) ((_foo) << 22) + +#define VMMC_CMD_CODER_DATA4(_tsf) ((_tsf) << 31) + +#define VMMC_EVENT_ID_MASK (_VMMC_MSG(0x1f, 0x7, 0x1f) | 0xff) + +#define VMMC_MSG_GET_CHAN(_msg) (((_msg) >> 16) & 0x1f) + +#define VMMC_EVENT_HOOK_STATE(_data) ((_data) & 1) + +#define VMMC_EVENT_HOOK_ID (_VMMC_MSG(9, 1, 1) | _VMMC_LENGTH(1)) +#define VMMC_EVENT_DTMF_ID (_VMMC_MSG(9, 2, 0) | _VMMC_LENGTH(1)) + +#define VMMC_VOICE_DATA(_type, _chan, _len) (((_type) << 24) | ((_chan) << 16) \ + | (_len)) + + +#define VMMC_CMD_ALI(_chan) (_VMMC_MSG(6, 1, 1) | _VMMC_CHAN(_chan) | _VMMC_LENGTH(3)) + +#define VMMC_CMD_ALI_DATA1(_enable, _rate, _ud, _eh, _eo, _i1, _dg1) \ + (((_enable) << 31) | ((_rate) << 30) | ((_ud) << 29) | ((_eh) << 27) | \ + ((_eo) << 26) | ((_i1) << 16) | (_dg1)) + +#define VMMC_CMD_ALI_DATA2(_dg2, _i2, _i3) \ + (((_dg2) << 16) | ((_i2) << 8) | (_i3)) + +#define VMMC_CMD_ALI_DATA3(_i4, _i5) \ + (((_i4) << 24) | ((_i5) << 16)) + +#define VMMC_CMD_ALM_COEF(_chan, _offset, _len) \ + (_VMMC_MSG(2, 0, _offset) | _VMMC_CHAN(_chan) | (_len)) + +#define CMD_VOICEREC_STATUS_PACKET 0x0 +#define CMD_VOICEREC_DATA_PACKET 0x1 +#define CMD_RTP_VOICE_DATA_PACKET 0x4 +#define CMD_RTP_EVENT_PACKET 0x5 +#define CMD_ADDRESS_PACKET 0x8 +#define CMD_FAX_DATA_PACKET 0x10 +#define CMD_FAX_STATUS_PACKET 0x11 +#define CMD_P_PHONE_DATA_PACKET 0x12 +#define CMD_P_PHONE_STATUS_PACKET 0x13 + +#define VMMC_CMD_RTP_CFG_US(_chan) \ + (_VMMC_MSG(6, 3, 17) | _VMMC_CHAN(_chan) | (36)) +#define VMMC_CMD_RTP_CFG_DS(_chan) \ + (_VMMC_MSG(6, 3, 25) | _VMMC_CHAN(_chan) | (32)) + +#define VMMC_CMD_LEC(_chan) \ + (_VMMC_MSG(6, 2, 1) | _VMMC_CHAN(_chan) | _VMMC_LENGTH(1)) +// (_VMMC_MSG(CMD_EOP, ALI_LEC_ECMD, MOD_ALI) | _VMMC_CHAN(_chan) | (32)) + +#define VMMC_CMD_LEC_DATA() +#endif diff --git a/package/lqtapi/src/mps/vmmc-coder.c b/package/lqtapi/src/mps/vmmc-coder.c new file mode 100644 index 0000000000..087a53bc79 --- /dev/null +++ b/package/lqtapi/src/mps/vmmc-coder.c @@ -0,0 +1,221 @@ +#include <linux/kernel.h> + +#include "vmmc.h" +#include "vmmc-cmds.h" +#include "vmmc-coder.h" +#include "vmmc-module.h" +#include "mps.h" + +enum vmmc_coder_encoding { + VMMC_CODER_ENCODING_ALAW = 2, + VMMC_CODER_ENCODING_MLAW = 3, + VMMC_CODER_ENCODING_G726_16 = 4, + VMMC_CODER_ENCODING_G726_24 = 5, + VMMC_CODER_ENCODING_G726_32 = 6, + VMMC_CODER_ENCODING_G726_40 = 7, + VMMC_CODER_ENCODING_AMR_4_75 = 8, + VMMC_CODER_ENCODING_AMR_5_15 = 9, + VMMC_CODER_ENCODING_AMR_5_9 = 10, + VMMC_CODER_ENCODING_AMR_6_7 = 11, + VMMC_CODER_ENCODING_AMR_7_4 = 12, + VMMC_CODER_ENCODING_AMR_7_95 = 13, + VMMC_CODER_ENCODING_AMR_10_2 = 14, + VMMC_CODER_ENCODING_AMR_12_2 = 15, + VMMC_CODER_ENCODING_G728_16 = 16, + VMMC_CODER_ENCODING_G729AB_8 = 18, + VMMC_CODER_ENCODING_G729E_11_8 = 19, + VMMC_CODER_ENCODING_G7221_24 = 20, + VMMC_CODER_ENCODING_G7221_32 = 21, + VMMC_CODER_ENCODING_G722_64 = 22, + VMMC_CODER_ENCODING_L16_8 = 24, + VMMC_CODER_ENCODING_L16_16 = 25, + VMMC_CODER_ENCODING_ILBC_15_2 = 26, + VMMC_CODER_ENCODING_ILBC_13_3 = 27, + VMMC_CODER_ENCODING_G7231_5_3 = 28, + VMMC_CODER_ENCODING_G7231_6_3 = 29, + VMMC_CODER_ENCODING_ALAW_VBD = 30, + VMMC_CODER_ENCODING_MLAW_VBD = 31, +}; + +static const uint8_t vmmc_coder_payload_mapping[] = { + [VMMC_CODER_ENCODING_ALAW] = 8, + [VMMC_CODER_ENCODING_MLAW] = 0, + [VMMC_CODER_ENCODING_G726_16] = 35, + [VMMC_CODER_ENCODING_G726_24] = 36, + [VMMC_CODER_ENCODING_G726_32] = 37, + [VMMC_CODER_ENCODING_G726_40] = 38, + [VMMC_CODER_ENCODING_AMR_4_75] = 39, + [VMMC_CODER_ENCODING_AMR_5_15] = 40, + [VMMC_CODER_ENCODING_AMR_5_9] = 41, + [VMMC_CODER_ENCODING_AMR_6_7] = 42, + [VMMC_CODER_ENCODING_AMR_7_4] = 43, + [VMMC_CODER_ENCODING_AMR_7_95] = 44, + [VMMC_CODER_ENCODING_AMR_10_2] = 45, + [VMMC_CODER_ENCODING_AMR_12_2] = 46, + [VMMC_CODER_ENCODING_G728_16] = 47, + [VMMC_CODER_ENCODING_G729AB_8] = 48, + [VMMC_CODER_ENCODING_G729E_11_8] = 49, + [VMMC_CODER_ENCODING_G7221_24] = 50, + [VMMC_CODER_ENCODING_G7221_32] = 51, + [VMMC_CODER_ENCODING_G722_64] = 52, + [VMMC_CODER_ENCODING_L16_8] = 11, + [VMMC_CODER_ENCODING_L16_16] = 10, + [VMMC_CODER_ENCODING_ILBC_15_2] = 53, + [VMMC_CODER_ENCODING_ILBC_13_3] = 54, + [VMMC_CODER_ENCODING_G7231_5_3] = 4, + [VMMC_CODER_ENCODING_G7231_6_3] = 4, + [VMMC_CODER_ENCODING_ALAW_VBD] = 55, + [VMMC_CODER_ENCODING_MLAW_VBD] = 56, +}; + +static uint32_t payload_enc[9]; +static uint32_t payload_dec[8]; + +static inline struct vmmc_coder *vmmc_module_to_coder(struct vmmc_module *module) +{ + return container_of(module, struct vmmc_coder, module); +} + +static int vmmc_coder_enable(struct vmmc_module *module, bool enable) +{ + struct vmmc_coder *coder = vmmc_module_to_coder(module); + + coder->enabled = enable; + +/* coder->cmd_cache[0] = VMMC_CMD_CODER_DATA1(coder->enabled, 0, 0, 0, coder->id, + module->pins[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); + return vmmc_command_write(coder->vmmc, VMMC_CMD_CODER(coder->id), + coder->cmd_cache);*/ + return 0; +} + +static int vmmc_coder_sync(struct vmmc_module *module) +{ + struct vmmc_coder *coder = vmmc_module_to_coder(module); + + coder->cmd_cache[0] = VMMC_CMD_CODER_DATA1(coder->enabled, 0, 1, 1, coder->id, + module->pins[0], 1, 1, 1, 1, 1, 1, 0, 0, 0, VMMC_CODER_ENCODING_MLAW); + coder->cmd_cache[1] = VMMC_CMD_CODER_DATA2(0x2000, 0x2000); + coder->cmd_cache[2] = VMMC_CMD_CODER_DATA3(0, 0, module->pins[1], 0, + module->pins[2], 0, module->pins[3], module->pins[4]); + + return vmmc_command_write(coder->vmmc, VMMC_CMD_CODER(coder->id), coder->cmd_cache); +} + +static const struct vmmc_module_ops vmmc_coder_module_ops = { + .sync = vmmc_coder_sync, + .enable = vmmc_coder_enable, +}; + +#define VMMC_CMD_CODER_JB(_chan) (_VMMC_MSG(6, 3, 18) | _VMMC_CHAN(_chan) | 8) +#define VMMC_CMD_CODER_JB_DATA1(_sf, _prp, _pje, _dvf, _nam, _rad, _adap, _init_pod) \ + (((_sf) << 31) | ((_prp) << 23) | ((_pje) << 22) | ((_dvf) << 21) | \ + ((_nam) << 20) | ((_rad) << 17) | ((_adap) << 16) | (_init_pod)) +#define VMMC_CMD_CODER_JB_DATA2(_min_pod, _max_pod) \ + (((_min_pod) << 16) | (_max_pod)) +#define VMMC_CMD_CODER_JB_DATA3(_adap_factor, _min_margin, _mode) \ + (((_adap_factor) << 24) | ((_min_margin) << 16) | (_mode)) + +int vmmc_coder_init(struct vmmc_coder *coder, struct vmmc *vmmc, unsigned int id) +{ + int ret; + + ret = vmmc_module_init(&coder->module, 5, &vmmc_coder_module_ops); + if (ret) + return ret; + + coder->id = id; + coder->module.id = id + 0x18; + coder->vmmc = vmmc; + coder->enabled = 0; + + coder->cmd_cache[0] = VMMC_CMD_CODER_DATA1(0, 0, 0, 0, id, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); + coder->cmd_cache[1] = VMMC_CMD_CODER_DATA2(0, 0); + coder->cmd_cache[2] = VMMC_CMD_CODER_DATA3(0, 0, 0, 0, 0, 0, 0, 0); + coder->cmd_cache[3] = VMMC_CMD_CODER_DATA4(0); + + coder->jitter_buffer_cache[0] = VMMC_CMD_CODER_JB_DATA1(0x16, 1, 0, 0, 1, 1, + 1, 0x50); + coder->jitter_buffer_cache[1] = VMMC_CMD_CODER_JB_DATA2(0x50, 0x05a0); + coder->jitter_buffer_cache[2] = VMMC_CMD_CODER_JB_DATA3(0x0d, 0x28, 1); + + vmmc_command_write(coder->vmmc, VMMC_CMD_RTP_CFG_US(coder->id), payload_enc); + vmmc_command_write(coder->vmmc, VMMC_CMD_RTP_CFG_DS(coder->id), payload_dec); + vmmc_command_write(coder->vmmc, VMMC_CMD_CODER_JB(coder->id), + coder->jitter_buffer_cache); + vmmc_command_write(coder->vmmc, VMMC_CMD_CODER(coder->id), coder->cmd_cache); + + vmmc_register_module(vmmc, &coder->module); + + return ret; +} + +void vmmc_coder_event(struct vmmc *vmmc, unsigned int chan, unsigned int type) +{ +} + +/* +void vmmc_coder_event(struct vmmc_coder *coder, unsigned int len) +{ + struct sk_buff *skb; + provide_mem_region(coder->vmmc, vmmc_coder_alloc_paket(coder)->head, 512); + + skb = skb_dequeue(&coder->paket_queue); + skb_put(skb, len); + + tapi_stream_recv(&coder->vmmc->tdev, skb); +} +*/ +/* +int vmmc_coder_set_codec(struct vmmc_coder *coder, enum vmmc_codec codec) +{ +}*/ + +void vmmc_init_coders(struct vmmc *vmmc) +{ + unsigned int i; + + payload_enc[0] = 0; + payload_enc[1] = 0x0; + payload_enc[1] |= vmmc_coder_payload_mapping[2] << 8; + payload_enc[1] |= vmmc_coder_payload_mapping[3] << 8; + for (i = 2; i < 9; ++i) { + payload_enc[i] = vmmc_coder_payload_mapping[i*4 - 4] << 24; + payload_enc[i] |= vmmc_coder_payload_mapping[i*4 - 3] << 16; + payload_enc[i] |= vmmc_coder_payload_mapping[i*4 - 2] << 8; + payload_enc[i] |= vmmc_coder_payload_mapping[i*4 - 1]; + payload_enc[i] |= 0x80808080; + } + for (i = 0; i < 7; ++i) { + payload_dec[i] = vmmc_coder_payload_mapping[i*4 + 2] << 24; + payload_dec[i] |= vmmc_coder_payload_mapping[i*4 + 3] << 16; + payload_dec[i] |= vmmc_coder_payload_mapping[i*4 + 4] << 8; + payload_dec[i] |= vmmc_coder_payload_mapping[i*4 + 5]; + } + payload_dec[i] = vmmc_coder_payload_mapping[i*4 + 2] << 24; + payload_dec[i] |= vmmc_coder_payload_mapping[i*4 + 3] << 16; + + + vmmc->num_coders = 5; + vmmc->coder = kcalloc(sizeof(*vmmc->coder), vmmc->num_coders, GFP_KERNEL); + + for (i = 0; i < vmmc->num_coders; ++i) + vmmc_coder_init(&vmmc->coder[i], vmmc, i); +} + +struct vmmc_coder *vmmc_coder_get(struct vmmc *vmmc) +{ + unsigned int i; + for (i = 0; i < vmmc->num_coders; ++i) { + if (!test_and_set_bit(i, &vmmc->coder_used)) + return &vmmc->coder[i]; + } + + return NULL; +} + +void vmmc_coder_put(struct vmmc *vmmc, struct vmmc_coder *coder) +{ + clear_bit(coder->id, &vmmc->coder_used); +} diff --git a/package/lqtapi/src/mps/vmmc-coder.h b/package/lqtapi/src/mps/vmmc-coder.h new file mode 100644 index 0000000000..f5a87dcef1 --- /dev/null +++ b/package/lqtapi/src/mps/vmmc-coder.h @@ -0,0 +1,14 @@ + +struct vmmc_coder { + struct vmmc *vmmc; + unsigned int id; + + struct vmmc_module module; + + unsigned int enabled; + + uint32_t cmd_cache[4]; + uint32_t jitter_buffer_cache[3]; + + struct tapi_stream *stream; +}; diff --git a/package/lqtapi/src/mps/vmmc-core.c b/package/lqtapi/src/mps/vmmc-core.c new file mode 100644 index 0000000000..99b0d1d3db --- /dev/null +++ b/package/lqtapi/src/mps/vmmc-core.c @@ -0,0 +1,646 @@ +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/slab.h> +#include <linux/interrupt.h> + +#include <linux/firmware.h> + +#include <linux/delay.h> + +#include <ifxmips_gptu.h> + +#include <linux/tapi/tapi.h> + +#include "vmmc.h" +#include "mps.h" +#include "mps-msg.h" +#include "mps-irq.h" +#include "vmmc-cmds.h" + +#include "vmmc-port.h" +#include "vmmc-stream.h" +#include "vmmc-link.h" +#include "vmmc-coder.h" + +struct vmmc_capabilities { + uint8_t num_alm; + uint8_t num_pcm; + uint8_t num_signal; + uint8_t num_coder; + uint8_t num_agc; + uint8_t num_eq; + uint8_t num_nlec; + uint8_t num_wlec; + uint8_t num_nwlec; + uint8_t num_wwlec; + uint8_t num_tone_generators; + uint8_t num_dtmf_generators; + uint8_t num_caller_id_senders; + uint8_t num_caller_id_recivers; +}; + +#define MPS_FIRMWARE_MAGIC 0xcc123456 + +struct vmmc_firmware_head { + uint32_t crc; + uint32_t crc_start_addr; + uint32_t crc_end_addr; + uint32_t version; + uint32_t encrypted; + uint32_t magic; + uint32_t mem; +} __packed; + +#define VMMC_FIFO_UPSTREAM_CMD_BASE_ADDR 0x00 +#define VMMC_FIFO_UPSTREAM_CMD_SIZE_ADDR 0x04 +#define VMMC_FIFO_DOWNSTREAM_CMD_BASE_ADDR 0x08 +#define VMMC_FIFO_DOWNSTREAM_CMD_SIZE_ADDR 0x0c +#define VMMC_FIFO_UPSTREAM_DATA_BASE_ADDR 0x10 +#define VMMC_FIFO_UPSTREAM_DATA_SIZE_ADDR 0x14 +#define VMMC_FIFO_DOWNSTREAM_DATA_BASE_ADDR 0x18 +#define VMMC_FIFO_DOWNSTREAM_DATA_SIZE_ADDR 0x1c +#define VMMC_FIFO_UPSTREAM_CMD_TAIL_ADDR 0x20 +#define VMMC_FIFO_UPSTREAM_CMD_HEAD_ADDR 0x24 +#define VMMC_FIFO_DOWNSTREAM_CMD_TAIL_ADDR 0x28 +#define VMMC_FIFO_DOWNSTREAM_CMD_HEAD_ADDR 0x2c +#define VMMC_FIFO_UPSTREAM_DATA_TAIL_ADDR 0x30 +#define VMMC_FIFO_UPSTREAM_DATA_HEAD_ADDR 0x34 +#define VMMC_FIFO_DOWNSTREAM_DATA_TAIL_ADDR 0x38 +#define VMMC_FIFO_DOWNSTREAM_DATA_HEAD_ADDR 0x3c + +#define VMMC_FIFO_EVENT_BASE_ADDR 0x180 +#define VMMC_FIFO_EVENT_SIZE_ADDR 0x184 +#define VMMC_FIFO_EVENT_TAIL_ADDR 0x188 +#define VMMC_FIFO_EVENT_HEAD_ADDR 0x18c + + +/* Calculates the base of the fifo behind the given fifo */ +#define VMMC_NEXT_FIFO_BASE0(_fifo) \ + (VMMC_FIFO_ ## _fifo ## _BASE + VMMC_FIFO_ ## _fifo ## _SIZE) +#define VMMC_NEXT_FIFO_BASE1(_fifo) \ + (VMMC_FIFO_ ## _fifo ## _BASE + VMMC_FIFO_ ## _fifo ## _SIZE) +#define VMMC_NEXT_FIFO_BASE2(_fifo) \ + (VMMC_FIFO_ ## _fifo ## _BASE + VMMC_FIFO_ ## _fifo ## _SIZE) + +/* Fifo sizes */ +#define VMMC_FIFO_UPSTREAM_CMD_SIZE 64 +#define VMMC_FIFO_DOWNSTREAM_CMD_SIZE 64 +#define VMMC_FIFO_UPSTREAM_DATA_SIZE 64 +#define VMMC_FIFO_DOWNSTREAM_DATA_SIZE 128 +#define VMMC_FIFO_EVENT_SIZE 64 + +/* Fifo addresses */ +#define VMMC_FIFO_UPSTREAM_CMD_BASE 0x40 +#define VMMC_FIFO_DOWNSTREAM_CMD_BASE VMMC_NEXT_FIFO_BASE0(UPSTREAM_CMD) +#define VMMC_FIFO_UPSTREAM_DATA_BASE VMMC_NEXT_FIFO_BASE1(DOWNSTREAM_CMD) +#define VMMC_FIFO_DOWNSTREAM_DATA_BASE VMMC_NEXT_FIFO_BASE2(UPSTREAM_DATA) +#define VMMC_FIFO_EVENT_BASE 0x190 + +#define VMMC_DECLARE_FIFO_CONFIG(_name, _fifo) \ +static const struct mps_fifo_config _name = { \ + .tail_addr = VMMC_FIFO_ ## _fifo ## _TAIL_ADDR, \ + .head_addr = VMMC_FIFO_ ## _fifo ## _HEAD_ADDR, \ + .base_addr = VMMC_FIFO_ ## _fifo ## _BASE_ADDR, \ + .size_addr = VMMC_FIFO_ ## _fifo ## _SIZE_ADDR, \ + .base = VMMC_FIFO_ ## _fifo ## _BASE, \ + .size = VMMC_FIFO_ ## _fifo ## _SIZE, \ +} + +VMMC_DECLARE_FIFO_CONFIG(vmmc_fifo_config_upstream_cmd, UPSTREAM_CMD); +VMMC_DECLARE_FIFO_CONFIG(vmmc_fifo_config_downstream_cmd, DOWNSTREAM_CMD); +VMMC_DECLARE_FIFO_CONFIG(vmmc_fifo_config_upstream_data, UPSTREAM_DATA); +VMMC_DECLARE_FIFO_CONFIG(vmmc_fifo_config_downstream_data, DOWNSTREAM_DATA); +VMMC_DECLARE_FIFO_CONFIG(vmmc_fifo_config_event, EVENT); + +static void vmmc_setup_fifos(struct vmmc *vmmc) +{ + mps_configure_mailbox(vmmc->mps, &vmmc->mbox_cmd, + &vmmc_fifo_config_upstream_cmd, &vmmc_fifo_config_downstream_cmd); + mps_configure_mailbox(vmmc->mps, &vmmc->mbox_data, + &vmmc_fifo_config_upstream_data, &vmmc_fifo_config_downstream_data); + + mps_configure_fifo(vmmc->mps, &vmmc->fifo_event, &vmmc_fifo_config_event); +} + +static uint32_t cram_data[] = { +0x00200000, 0x00008e59, 0x165235cd, 0x17e2f141, 0xe3eef301, 0x0a431281, +0x04fdf20d, 0x7fe363d5, 0xfd4b7333, 0x7ffffd44, 0xfcf80298, 0xfecd00c9, +0xff900042, 0xfff70003, 0x000923b7, 0xe92a354d, 0xc8981f44, 0x9c0f1257, +0x26aacf33, 0x27db9836, 0x10586f5b, 0x9c167d2d, 0x94b679a7, 0x8c227660, +0x83fa7491, 0x7ce0826a, 0x7ff87ff4, 0x296b4e22, 0x76e67fff, 0x008ffc04, +0x02cbfb36, 0x026afeba, 0x009effc3, 0x0013fffd, 0x23b7e92a, 0x354dc898, +0x1f449c0f, 0x125726aa, 0xcf3327db, 0x98361058, 0x74bc93d6, 0x7ebc8f61, +0x7d068986, 0x7b46833b, 0x7a3b7f00, 0x287a47b1, 0x05800367, 0x20ae2715, +0x0fb5da12, 0x1935f53b, 0x01230240, 0xfc717f00, 0x2000d346, +}; + +static void vmmc_push_data_paket(struct vmmc *vmmc, int type, unsigned int chan, + void __iomem *addr, size_t len) +{ + uint32_t data[3]; + + data[0] = VMMC_VOICE_DATA(type, chan, 8); + data[1] = CPHYSADDR(addr); + data[2] = len; + + mps_fifo_in(&vmmc->mbox_data.downstream, data, 3); +} + +static struct sk_buff *vmmc_alloc_data_paket(struct vmmc *vmmc) +{ + struct sk_buff *skb; + + skb = alloc_skb(512, GFP_KERNEL); + + skb_queue_tail(&vmmc->recv_queue, skb); + + return skb; +} + +static void vmmc_provide_paket(struct vmmc *vmmc) +{ + struct sk_buff *skb = vmmc_alloc_data_paket(vmmc); + vmmc_push_data_paket(vmmc, CMD_ADDRESS_PACKET, 0, skb->data, skb->len); +} + +static void vmmc_recv_paket(struct vmmc *vmmc, unsigned int chan, void __iomem *addr, size_t len) +{ + struct sk_buff *skb; + struct sk_buff *tmp; + + skb_queue_walk_safe(&vmmc->recv_queue, skb, tmp) { + if (skb->data == addr) + break; + } + + if (skb == (struct sk_buff *)(&vmmc->recv_queue)) { + printk("AHHHH\n"); + return; + } + + dma_cache_inv((u32)addr, len); + skb_unlink(skb, &vmmc->recv_queue); + + if (!vmmc->coder[chan].stream) { + kfree_skb(skb); + return; + } + + skb_put(skb, len); + tapi_stream_recv(&vmmc->tdev, vmmc->coder[chan].stream, skb); +} + +void vmmc_send_paket(struct vmmc *vmmc, unsigned int chan, struct sk_buff *skb) +{ + skb_queue_tail(&vmmc->send_queue, skb); + + dma_cache_wback((u32)skb->data, skb->len); + vmmc_push_data_paket(vmmc, CMD_RTP_VOICE_DATA_PACKET, chan, skb->data, + skb->len); +} + +static void vmmc_free_paket(struct vmmc *vmmc, void __iomem *addr, size_t len) +{ + struct sk_buff *skb; + struct sk_buff *tmp; + + skb_queue_walk_safe(&vmmc->send_queue, skb, tmp) { + if (skb->data == addr) + break; + } + + if (skb == (struct sk_buff *)(&vmmc->send_queue)) { + printk("AHHHH\n"); + } else { + skb_unlink(skb, &vmmc->send_queue); + kfree_skb(skb); + } +} + + +static void vmmc_write_cram_data(struct vmmc *vmmc, unsigned int id, + uint32_t *data, size_t length) +{ + size_t transfer_length; + size_t offset = 0; + uint32_t cmd; + + length *= 4; + offset = 0x5; + + while (length) { + transfer_length = length > 56 ? 56 : length; + cmd = VMMC_CMD_ALM_COEF(id, offset, transfer_length); + vmmc_command_write(vmmc, cmd, data); + + data += transfer_length >> 2; + offset += transfer_length >> 1; + length -= transfer_length; + } +} + +int vmmc_command_read(struct vmmc *vmmc, uint32_t cmd, uint32_t *result) +{ + struct mps_mailbox *mbox = &vmmc->mbox_cmd; + + INIT_COMPLETION(vmmc->cmd_completion); + + mps_fifo_in(&mbox->downstream, &cmd, 1); + + wait_for_completion(&vmmc->cmd_completion); + + mps_fifo_out(&mbox->upstream, result, 1); + mps_fifo_out(&mbox->upstream, result, (*result & 0xff) / 4); + + return 0; +} + +int vmmc_command_write(struct vmmc *vmmc, uint32_t cmd, + const uint32_t *data) +{ + struct mps_mailbox *mbox = &vmmc->mbox_cmd; +/* int i; + + printk("cmd: %x\n", cmd); + for (i = 0; i < DIV_ROUND_UP((cmd & 0xff), 4); ++i) { + printk("data[%d] = %x\n", i, data[i]); + } +*/ + while (mps_fifo_len(&mbox->downstream) < (cmd & 0xff) + 4) + mdelay(100); + + mps_fifo_in(&mbox->downstream, &cmd, 1); + mps_fifo_in(&mbox->downstream, data, DIV_ROUND_UP((cmd & 0xff), 4)); + + mdelay(100); + + return 0; +} + +static int vmmc_modules_sync(struct tapi_device *tapi) +{ + struct vmmc *vmmc = tdev_to_vmmc(tapi); + struct vmmc_module *module; + + list_for_each_entry(module, &vmmc->modules, head) + vmmc_module_sync(module); + + return 0; +} + +static const struct tapi_ops vmmc_tapi_ops = { + .send_dtmf_event = vmmc_port_send_dtmf_event, + .ring = vmmc_port_ring, + + .sync = vmmc_modules_sync, + + .stream_alloc = vmmc_stream_alloc, + .stream_free = vmmc_stream_free, + .stream_send = vmmc_stream_send, + + .link_alloc = vmmc_tapi_link_alloc, + .link_free = vmmc_tapi_link_free, + .link_enable = vmmc_tapi_link_enable, + .link_disable = vmmc_tapi_link_disable, +}; + +static void setup_alm(struct vmmc *vmmc) +{ + int i; + + vmmc->tdev.ports = kcalloc(2, sizeof(*vmmc->tdev.ports), GFP_KERNEL); + vmmc->ports = kcalloc(2, sizeof(*vmmc->ports), GFP_KERNEL); + + for (i = 0; i < 2; ++i) + vmmc_port_init(vmmc, &vmmc->ports[i], &vmmc->tdev.ports[i], i); + + skb_queue_head_init(&vmmc->send_queue); + skb_queue_head_init(&vmmc->recv_queue); + + for (i = 0; i < 10; ++i) + vmmc_provide_paket(vmmc); + + vmmc->tdev.num_ports = 2; + vmmc->tdev.ops = &vmmc_tapi_ops; + tapi_device_register(&vmmc->tdev, "vmmc", vmmc->dev); +} + +static void vmmc_init_timer(struct vmmc *vmmc) +{ + unsigned int timer; + unsigned int timer_flags; + int ret; + unsigned long loops, count; + + timer = TIMER1B; + + timer_flags = + TIMER_FLAG_16BIT | TIMER_FLAG_COUNTER | TIMER_FLAG_CYCLIC | + TIMER_FLAG_DOWN | TIMER_FLAG_FALL_EDGE | TIMER_FLAG_SYNC | + TIMER_FLAG_CALLBACK_IN_IRQ; + ret = ifxmips_request_timer (timer, timer_flags, 1, 0, 0); + if (ret < 0) { + printk("FAILED TO INIT TIMER\n"); + return; + } + ret = ifxmips_start_timer (timer, 0); + if (ret < 0) { + printk("FAILED TO START TIMER\n"); + return; + } + do + { + loops++; + ifxmips_get_count_value(timer, &count); + } while (count); + + *((volatile uint32_t *) (KSEG1 + 0x1e100a00 + 0x0014)) = 0x000005c5; +} + +static void vmmc_free_timer(struct vmmc *vmmc) +{ + ifxmips_free_timer(TIMER1B); +} + +static void vmmc_get_capabilities(struct vmmc *vmmc) +{ + uint32_t data[10]; + uint8_t len; + + vmmc_command_read(vmmc, + MPS_MSG_CMD_EOP_SYSTEM(SYS_CAP_ECMD, sizeof(uint32_t)), data); + + len = ((data[0] >> 16) & 0xff) - sizeof(uint32_t); + + if (len > sizeof(data)) + len = sizeof(data); + + vmmc_command_read(vmmc, + MPS_MSG_CMD_EOP_SYSTEM(SYS_CAP_ECMD, len), data); + + len /= 4; + +/* for (;len > 0; --len) { + printk("fw cap(%d): %.2x\n", 10-len, data[10-len]); + } +*/ + setup_alm(vmmc); +} + +static void vmmc_get_firmware_version(struct vmmc *vmmc) +{ + uint32_t data[1]; + + vmmc_command_read(vmmc, MPS_CMD_GET_VERSION, data); + + printk("firmware version: %x\n", *data); + + vmmc_get_capabilities(vmmc); +} + +static irqreturn_t vmmc_firmware_loaded_irq(int irq, void *devid) +{ + struct vmmc *vmmc = devid; + complete(&vmmc->firmware_loaded_completion); + printk("Firmware loaded irq\n"); + + return IRQ_HANDLED; +} + +static irqreturn_t vmmc_cmd_error_irq(int irq, void *devid) +{ +/* struct vmmc *vmmc = devid;*/ + + printk("cmd error!!!!\n"); + + return IRQ_HANDLED; +} + +static irqreturn_t vmmc_recv_ov_irq(int irq, void *devid) +{ + struct vmmc *vmmc = devid; + uint32_t data[2] = { + VMMC_CMD_SERR_ACK(0), + VMMC_CMD_SERR_ACK_DATA1(1) + }; + uint32_t voice_data[64]; + + return IRQ_HANDLED; + + mps_fifo_in(&vmmc->mbox_cmd.downstream, data, 2); + mps_fifo_out(&vmmc->mbox_data.upstream, voice_data, 15); + + printk("recv overflow: %x\n", voice_data[0]); + + return IRQ_HANDLED; +} + +static irqreturn_t vmmc_event_fifo_irq(int irq, void *devid) +{ + struct vmmc *vmmc = devid; + uint32_t event, event_id; + uint32_t data = 0; + unsigned int chan; + + mps_fifo_out(&vmmc->fifo_event, &event, 1); + + event_id = event & VMMC_EVENT_ID_MASK; + chan = VMMC_MSG_GET_CHAN(event); + + if (event & 0xff) + mps_fifo_out(&vmmc->fifo_event, &data, 1); + + switch (event_id) { + case VMMC_EVENT_HOOK_ID: + vmmc_alm_hook_event_handler(vmmc, chan, data); + break; + case VMMC_EVENT_DTMF_ID: + vmmc_sig_dtmf_event_handler(vmmc, chan, data); + break; + default: + printk("Ein unbekanntes Event: %x %x\n", event, data); + break; + } + + return IRQ_HANDLED; +} + +static irqreturn_t vmmc_mbox_data_irq_handler(int irq, void *devid) +{ + struct vmmc *vmmc = devid; + struct mps_mailbox *mbox = &vmmc->mbox_data; + unsigned int count, type, chan; + uint32_t data[2]; + void __iomem *addr; + size_t len; + + mps_fifo_out(&mbox->upstream, data, 1); + + count = (data[0] & 0xff) / 8; + type = (data[0] >> 24) & 0xff; + chan = (data[0] >> 16) & 0xff; + + while (count) { + mps_fifo_out(&mbox->upstream, data, 2); + + addr = (void __iomem *)CKSEG0ADDR(data[0]); + len = data[1]; + + switch (type) { + case CMD_ADDRESS_PACKET: + vmmc_free_paket(vmmc, addr, len); + break; + case CMD_RTP_VOICE_DATA_PACKET: + vmmc_provide_paket(vmmc); + vmmc_recv_paket(vmmc, chan, addr, len); + break; + } + --count; + } + + return IRQ_HANDLED; +} + +static irqreturn_t vmmc_mbox_cmd_irq_handler(int irq, void *devid) +{ + struct vmmc *vmmc = devid; + + complete(&vmmc->cmd_completion); + + return IRQ_HANDLED; +} + +static void vmmc_load_firmware(const struct firmware *fw, void *context) +{ + struct vmmc *vmmc = context; + struct vmmc_firmware_head *fw_head; + size_t tail_size; + enum mps_boot_config config; + + if (!fw) { + printk("failed to load tapi firmware\n"); +// request_firmware_nowait(THIS_MODULE, 1, "danube_firmware.bin", vmmc->dev, +// GFP_KERNEL, vmmc, vmmc_load_firmware); + return; + } + + if (fw->size < sizeof(*fw_head)) + return; + + fw_head = (struct vmmc_firmware_head *)((uint8_t *)fw->data + fw->size - sizeof(*fw_head)); + + if (fw_head->magic != MPS_FIRMWARE_MAGIC) { + config = MPS_BOOT_LEGACY; + tail_size = sizeof(uint32_t); + } else { + config = MPS_BOOT_ENCRYPTED; + tail_size = sizeof(*fw_head) - sizeof(uint32_t); + } + + vmmc_setup_fifos(vmmc); + init_completion(&vmmc->firmware_loaded_completion); + mps_load_firmware(vmmc->mps, fw->data, fw->size - tail_size, config); + wait_for_completion_timeout(&vmmc->firmware_loaded_completion, 5*HZ); + vmmc_init_timer(vmmc); + vmmc_write_cram_data(vmmc, 0, cram_data, ARRAY_SIZE(cram_data)); + vmmc_write_cram_data(vmmc, 1, cram_data, ARRAY_SIZE(cram_data)); + vmmc_get_firmware_version(vmmc); + vmmc_init_coders(vmmc); +} + +static int vmmc_request_irqs(struct vmmc *vmmc) +{ + int ret; + + ret = request_irq(vmmc->irq_fw_loaded, vmmc_firmware_loaded_irq, 0, "vmmc fw loaded", vmmc); + ret = request_irq(vmmc->irq_event_fifo, vmmc_event_fifo_irq, 0, "vmmc event fifo", vmmc); + ret = request_irq(vmmc->irq_cmd_error, vmmc_cmd_error_irq, 0, + "cmd error irq", vmmc); + ret = request_irq(MPS_IRQ_RCV_OVERFLOW, vmmc_recv_ov_irq, 0, + "recv_ov irq", vmmc); + + ret = request_irq(vmmc->irq_mbox_cmd, vmmc_mbox_cmd_irq_handler, 0, + "vmmc cmd mailbox irq", vmmc); + + ret = request_irq(vmmc->irq_mbox_data, vmmc_mbox_data_irq_handler, 0, + "vmmc data mailbox irq", vmmc); + + return ret; +} + +static int __devinit vmmc_probe(struct platform_device *pdev) +{ + struct vmmc *vmmc; + int ret = 0; + + vmmc = kzalloc(sizeof(*vmmc), GFP_KERNEL); + + if (!vmmc) + return -ENOMEM; + + vmmc->dev = &pdev->dev; + vmmc->mps = device_to_mps(pdev->dev.parent); + + if (!vmmc->mps) { + goto err_free; + ret = -EBUSY; + } + + INIT_LIST_HEAD(&vmmc->modules); + init_completion(&vmmc->cmd_completion); + + vmmc->irq_fw_loaded = MPS_IRQ_DOWNLOAD_DONE; + vmmc->irq_mbox_cmd = MPS_IRQ_CMD_UPSTREAM; + vmmc->irq_mbox_data = MPS_IRQ_DATA_UPSTREAM; + vmmc->irq_event_fifo = MPS_IRQ_EVENT; + vmmc->irq_cmd_error = MPS_IRQ_CMD_ERROR; + + platform_set_drvdata(pdev, vmmc); + + vmmc_request_irqs(vmmc); + + request_firmware_nowait(THIS_MODULE, 1, "danube_firmware.bin", &pdev->dev, + GFP_KERNEL, vmmc, vmmc_load_firmware); + + return 0; + +err_free: + kfree(vmmc); + + return ret; +} + +static int __devexit vmmc_remove(struct platform_device *pdev) +{ + struct vmmc *vmmc = platform_get_drvdata(pdev); + vmmc_free_timer(vmmc); + + tapi_device_unregister(&vmmc->tdev); + + return 0; +} + +static struct platform_driver vmmc_driver = { + .probe = vmmc_probe, + .remove = __devexit_p(vmmc_remove), + .driver = { + .name = "vmmc", + .owner = THIS_MODULE + }, +}; + +static int __init vmmc_init(void) +{ + return platform_driver_register(&vmmc_driver); +} +module_init(vmmc_init); + +static void __exit vmmc_exit(void) +{ + platform_driver_unregister(&vmmc_driver); +} +module_exit(vmmc_exit); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); diff --git a/package/lqtapi/src/mps/vmmc-link.c b/package/lqtapi/src/mps/vmmc-link.c new file mode 100644 index 0000000000..42d11dbb6e --- /dev/null +++ b/package/lqtapi/src/mps/vmmc-link.c @@ -0,0 +1,52 @@ +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/tapi/tapi.h> + +#include "vmmc-link.h" +#include "vmmc-module.h" + +struct vmmc_tapi_link { + struct tapi_link tapi_link; + struct vmmc_link vmmc_link; +}; + +struct tapi_link *vmmc_tapi_link_alloc(struct tapi_device *tdev, + struct tapi_endpoint *ep1, struct tapi_endpoint *ep2) +{ + struct vmmc_tapi_link *link = kzalloc(sizeof(*link), GFP_KERNEL); + struct vmmc_module *module1 = tapi_endpoint_to_vmmc_module(ep1); + struct vmmc_module *module2 = tapi_endpoint_to_vmmc_module(ep2); + + vmmc_link_init(&link->vmmc_link, module1, module2); + + return &link->tapi_link; +} + +void vmmc_tapi_link_free(struct tapi_device *tdev, struct tapi_link *tapi_link) +{ + struct vmmc_tapi_link *link = container_of(tapi_link, struct vmmc_tapi_link, + tapi_link); + + vmmc_link_put(&link->vmmc_link); + kfree(link); +} + +int vmmc_tapi_link_enable(struct tapi_device *tdev, + struct tapi_link *tapi_link) +{ + struct vmmc_tapi_link *link = container_of(tapi_link, struct vmmc_tapi_link, + tapi_link); + + vmmc_link_enable(&link->vmmc_link); + return 0; +} + +int vmmc_tapi_link_disable(struct tapi_device *tdev, + struct tapi_link *tapi_link) +{ + struct vmmc_tapi_link *link = container_of(tapi_link, struct vmmc_tapi_link, + tapi_link); + + vmmc_link_disable(&link->vmmc_link); + return 0; +} diff --git a/package/lqtapi/src/mps/vmmc-link.h b/package/lqtapi/src/mps/vmmc-link.h new file mode 100644 index 0000000000..9f818bd834 --- /dev/null +++ b/package/lqtapi/src/mps/vmmc-link.h @@ -0,0 +1,10 @@ +#ifndef __VMMC_LINK_H__ +#define __VMMC_LINK_H__ + +struct tapi_link *vmmc_tapi_link_alloc(struct tapi_device *tdev, + struct tapi_endpoint *ep1, struct tapi_endpoint *ep2); +void vmmc_tapi_link_free(struct tapi_device *tdev, struct tapi_link *link); +int vmmc_tapi_link_enable(struct tapi_device *tdev, struct tapi_link *link); +int vmmc_tapi_link_disable(struct tapi_device *tdev, struct tapi_link *link); + +#endif diff --git a/package/lqtapi/src/mps/vmmc-module.c b/package/lqtapi/src/mps/vmmc-module.c new file mode 100644 index 0000000000..181f0c6b7c --- /dev/null +++ b/package/lqtapi/src/mps/vmmc-module.c @@ -0,0 +1,130 @@ +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/mutex.h> + +#include <asm/bitops.h> + +#include "vmmc-module.h" + +int vmmc_module_init(struct vmmc_module *module, size_t num_pins, + const struct vmmc_module_ops *ops) +{ + module->pins = kcalloc(num_pins, sizeof(*module->pins), GFP_KERNEL); + + if (!module->pins) + return -ENOMEM; + + module->num_pins = num_pins; + module->ops = ops; + + mutex_init(&module->lock); + module->refcount = 0; + + return 0; +} + +int vmmc_module_sync(struct vmmc_module *module) +{ + if (!test_and_clear_bit(VMMC_MODULE_FLAG_MODIFIED, &module->flags)) + return 0; + + return module->ops->sync(module); +} + +int vmmc_module_get_pin(struct vmmc_module *module) +{ + size_t i = 0; + int ret = 0; + + for (i = 0; i < module->num_pins; ++i) { + if (!test_and_set_bit(VMMC_MODULE_FLAG_PIN_USED(i), &module->flags)) + break; + } + if (i == module->num_pins) + ret = -EBUSY; + else + ret = i; + + return ret; +} + +void vmmc_module_put_pin(struct vmmc_module *module, unsigned int pin) +{ + module->pins[pin] = 0; + clear_bit(VMMC_MODULE_FLAG_PIN_USED(pin), &module->flags); +} + +void vmmc_module_set_pin_input(struct vmmc_module *module, unsigned int pin, + struct vmmc_module *input) +{ + if (input) + module->pins[pin] = input->id; + else + module->pins[pin] = 0; + + set_bit(VMMC_MODULE_FLAG_MODIFIED, &module->flags); +} + +static void vmmc_module_enable(struct vmmc_module *module) +{ + mutex_lock(&module->lock); + + if (++module->refcount == 1) + module->ops->enable(module, true); + + mutex_unlock(&module->lock); +} + +static void vmmc_module_disable(struct vmmc_module *module) +{ + mutex_lock(&module->lock); + + if (module->refcount <= 0) + printk(KERN_ERR "vmmc module: unbalanced disable\n"); + else if (--module->refcount == 0) + module->ops->enable(module, false); + + mutex_unlock(&module->lock); +} + + +unsigned int vmmc_link_init(struct vmmc_link *link, + struct vmmc_module *a, struct vmmc_module *b) +{ + link->pins[0] = vmmc_module_get_pin(a); + link->pins[1] = vmmc_module_get_pin(b); + link->modules[0] = a; + link->modules[1] = b; + + return 0; +} + +void vmmc_link_put(struct vmmc_link *link) +{ + vmmc_link_disable(link); + vmmc_module_sync(link->modules[0]); + vmmc_module_sync(link->modules[1]); + vmmc_module_put_pin(link->modules[0], link->pins[0]); + vmmc_module_put_pin(link->modules[1], link->pins[1]); +} + +void vmmc_link_enable(struct vmmc_link *link) +{ + vmmc_module_set_pin_input(link->modules[0], link->pins[0], + link->modules[1]); + vmmc_module_set_pin_input(link->modules[1], link->pins[1], + link->modules[0]); + + vmmc_module_enable(link->modules[0]); + vmmc_module_enable(link->modules[1]); +} + +void vmmc_link_disable(struct vmmc_link *link) +{ + vmmc_module_set_pin_input(link->modules[0], link->pins[0], NULL); + vmmc_module_set_pin_input(link->modules[1], link->pins[1], NULL); + + vmmc_module_disable(link->modules[0]); + vmmc_module_disable(link->modules[1]); +} diff --git a/package/lqtapi/src/mps/vmmc-module.h b/package/lqtapi/src/mps/vmmc-module.h new file mode 100644 index 0000000000..b598d0dcd9 --- /dev/null +++ b/package/lqtapi/src/mps/vmmc-module.h @@ -0,0 +1,64 @@ +#ifndef __VMMC_MODULE_H__ +#define __VMMC_MODULE_H__ + +#include <linux/list.h> +#include <linux/kernel.h> +#include <linux/tapi/tapi.h> + +struct vmmc_module; + +struct vmmc_module_ops { + int (*sync)(struct vmmc_module *); + int (*enable)(struct vmmc_module *, bool enable); +}; + +struct vmmc_module +{ + unsigned int id; + + size_t num_pins; + unsigned int *pins; + + const struct vmmc_module_ops *ops; + + unsigned long flags; +#define VMMC_MODULE_FLAG_PIN_USED(x) (x) +#define VMMC_MODULE_FLAG_MODIFIED 31 + struct mutex lock; + struct list_head head; + + unsigned int refcount; +}; + +int vmmc_module_init(struct vmmc_module *module, size_t num_pins, + const struct vmmc_module_ops *ops); +int vmmc_module_sync(struct vmmc_module *module); + +struct vmmc_link { + struct vmmc_module *modules[2]; + unsigned int pins[2]; +}; + +struct vmmc_endpoint { + struct tapi_endpoint ep; + struct vmmc_module *module; +}; + +void vmmc_link_enable(struct vmmc_link *link); +void vmmc_link_disable(struct vmmc_link *link); +unsigned int vmmc_link_init(struct vmmc_link *link, + struct vmmc_module *a, struct vmmc_module *b); +void vmmc_link_put(struct vmmc_link *link); + +int vmmc_module_get_pin(struct vmmc_module *module); +void vmmc_module_put_pin(struct vmmc_module *module, unsigned int pin); +void vmmc_module_set_pin_input(struct vmmc_module *module, unsigned int pin, + struct vmmc_module *input); + + +static inline struct vmmc_module *tapi_endpoint_to_vmmc_module(struct tapi_endpoint *ep) +{ + return tapi_endpoint_get_data(ep); +} + +#endif diff --git a/package/lqtapi/src/mps/vmmc-port.c b/package/lqtapi/src/mps/vmmc-port.c new file mode 100644 index 0000000000..aa620a1e02 --- /dev/null +++ b/package/lqtapi/src/mps/vmmc-port.c @@ -0,0 +1,48 @@ +#include <linux/kernel.h> +#include <linux/slab.h> + +#include <linux/tapi/tapi.h> + +#include "vmmc.h" +#include "vmmc-port.h" +#include "vmmc-alm.h" +#include "vmmc-sig.h" + +int vmmc_port_ring(struct tapi_device *tdev, struct tapi_port *port, bool ring) +{ + struct vmmc *vmmc = tdev_to_vmmc(tdev); + + return vmmc_alm_set_state(&vmmc->ports[port->id].alm, + ring ? VMMC_ALM_STATE_RING : VMMC_ALM_STATE_ONHOOK); +} + +int vmmc_port_send_dtmf_event(struct tapi_device *tdev, + struct tapi_port *port, struct tapi_dtmf_event *event) +{ + return 0; +} + +struct vmmc_port *vmmc_port_init(struct vmmc *vmmc, struct vmmc_port *port, + struct tapi_port *tport, unsigned int id) +{ + vmmc_alm_init(&port->alm, vmmc, id); + vmmc_sig_init(&port->sig, vmmc, id); + + port->sig_pin = vmmc_module_get_pin(&port->sig.module); + vmmc_module_set_pin_input(&port->sig.module, port->sig_pin, + &port->alm.module); + + vmmc_module_sync(&port->sig.module); + + vmmc_alm_set_state(&port->alm, VMMC_ALM_STATE_ONHOOK); + + tport->id = id; + tapi_endpoint_set_data(&tport->ep, &port->alm.module); + + return port; +} + +void vmmc_port_put(struct vmmc *vmmc, struct vmmc_port *port) +{ + vmmc_module_put_pin(&port->sig.module, port->sig_pin); +} diff --git a/package/lqtapi/src/mps/vmmc-port.h b/package/lqtapi/src/mps/vmmc-port.h new file mode 100644 index 0000000000..1efceb0b2d --- /dev/null +++ b/package/lqtapi/src/mps/vmmc-port.h @@ -0,0 +1,22 @@ +#ifndef __VMMC_PORT_H__ +#define __VMMC_PORT_H__ + +#include "vmmc-alm.h" +#include "vmmc-sig.h" + +void vmmc_port_free(struct vmmc *vmmc, struct vmmc_port *port); + +int vmmc_port_ring(struct tapi_device *tdev, struct tapi_port *port, bool ring); +int vmmc_port_send_dtmf_event(struct tapi_device *tdev, + struct tapi_port *port, struct tapi_dtmf_event *event); +struct vmmc_port *vmmc_port_init(struct vmmc *vmmc, struct vmmc_port *port, +struct tapi_port *tport, unsigned int id); + +struct vmmc_port +{ + struct vmmc_alm alm; + struct vmmc_sig sig; + unsigned int sig_pin; +}; + +#endif diff --git a/package/lqtapi/src/mps/vmmc-sig.h b/package/lqtapi/src/mps/vmmc-sig.h new file mode 100644 index 0000000000..e8f047063b --- /dev/null +++ b/package/lqtapi/src/mps/vmmc-sig.h @@ -0,0 +1,19 @@ +#ifndef __VMMC_SIG_H__ +#define __VMMC_SIG_H__ + +#include "vmmc-module.h" + +struct vmmc_sig { + struct vmmc *vmmc; + unsigned int id; + struct vmmc_module module; + + uint32_t sig_cache; + uint32_t dtmfr_cache; +}; + +int vmmc_sig_init(struct vmmc_sig *sig, struct vmmc *vmmc, unsigned int id); + +void vmmc_sig_dtmf_event_handler(struct vmmc *vmmc, uint32_t event, uint32_t data); + +#endif diff --git a/package/lqtapi/src/mps/vmmc-signal.c b/package/lqtapi/src/mps/vmmc-signal.c new file mode 100644 index 0000000000..83fea60839 --- /dev/null +++ b/package/lqtapi/src/mps/vmmc-signal.c @@ -0,0 +1,69 @@ +#include <linux/kernel.h> + +#include "vmmc.h" +#include "vmmc-sig.h" +#include "vmmc-cmds.h" + +static struct vmmc_sig *vmmc_module_to_sig(struct vmmc_module *module) +{ + return container_of(module, struct vmmc_sig, module); +} + +static int vmmc_sig_enable(struct vmmc_module *module, bool enabled) +{ + struct vmmc_sig *sig = vmmc_module_to_sig(module); + + return 0; + + sig->sig_cache = VMMC_CMD_SIG_SET_ENABLE(sig->sig_cache, enabled); + + return vmmc_command_write(sig->vmmc, VMMC_CMD_SIG(sig->id), &sig->sig_cache); +} + +static int vmmc_sig_sync(struct vmmc_module *module) +{ + struct vmmc_sig *sig = vmmc_module_to_sig(module); + + sig->sig_cache = VMMC_CMD_SIG_SET_INPUTS(sig->sig_cache, + module->pins[0], module->pins[1]); + + vmmc_command_write(sig->vmmc, VMMC_CMD_SIG(sig->id), + &sig->sig_cache); + + return vmmc_command_write(sig->vmmc, VMMC_CMD_DTMFR(sig->id), + &sig->dtmfr_cache); +} + +static const struct vmmc_module_ops vmmc_sig_ops = { + .enable = vmmc_sig_enable, + .sync = vmmc_sig_sync, +}; + +int vmmc_sig_init(struct vmmc_sig *sig, struct vmmc *vmmc, unsigned int id) +{ + int ret; + + ret = vmmc_module_init(&sig->module, 2, &vmmc_sig_ops); + if (ret) + return ret; + + sig->id = id; + sig->module.id = id + 0x1e; + sig->vmmc = vmmc; + sig->sig_cache = VMMC_CMD_SIG_DATA(1, 1, 0, 0, 0, 0, 0); + sig->dtmfr_cache = VMMC_CMD_DTMFR_DATA(1, 1, id); + + vmmc_register_module(vmmc, &sig->module); + + vmmc_command_write(sig->vmmc, VMMC_CMD_SIG(sig->id), + &sig->sig_cache); + vmmc_command_write(sig->vmmc, VMMC_CMD_DTMFR(sig->id), + &sig->dtmfr_cache); + + return ret; +} + +void vmmc_sig_dtmf_event_handler(struct vmmc *vmmc, uint32_t id, uint32_t data) +{ + tapi_report_dtmf_event(&vmmc->tdev, &vmmc->tdev.ports[id], data & 0xf); +} diff --git a/package/lqtapi/src/mps/vmmc-stream.c b/package/lqtapi/src/mps/vmmc-stream.c new file mode 100644 index 0000000000..36f5eef80b --- /dev/null +++ b/package/lqtapi/src/mps/vmmc-stream.c @@ -0,0 +1,73 @@ +#include <linux/kernel.h> +#include <linux/tapi/tapi.h> +#include <linux/skbuff.h> + +#include "vmmc.h" +#include "vmmc-coder.h" + +struct vmmc_tapi_stream { + struct vmmc_coder *coder; + struct tapi_stream stream; +}; + +struct vmmc_tapi_stream *tapi_to_vmmc_stream(struct tapi_stream * stream) +{ + return container_of(stream, struct vmmc_tapi_stream, stream); +} + +struct tapi_stream *vmmc_stream_alloc(struct tapi_device *tdev) +{ + struct vmmc *vmmc = tdev_to_vmmc(tdev); + struct vmmc_tapi_stream *stream; + struct vmmc_coder *coder; + + coder = vmmc_coder_get(vmmc); + if (!coder) + return ERR_PTR(-ENODEV); + + stream = kzalloc(sizeof(*stream), GFP_KERNEL); + if (!stream) + return ERR_PTR(-ENOMEM); + + stream->coder = coder; + coder->stream = &stream->stream; + tapi_endpoint_set_data(&stream->stream.ep, &coder->module); + + return &stream->stream; +} + +void vmmc_stream_free(struct tapi_device *tdev, struct tapi_stream *tstream) +{ + struct vmmc *vmmc = tdev_to_vmmc(tdev); + struct vmmc_tapi_stream *stream = tapi_to_vmmc_stream(tstream); + + stream->coder->stream = NULL; + + vmmc_coder_put(vmmc, stream->coder); + kfree(stream); +} + +int vmmc_stream_start(struct tapi_device *tdev, struct tapi_stream *stream) +{ + return 0; +} + +int vmmc_stream_stop(struct tapi_device *tdev, struct tapi_stream *stream) +{ + return 0; +} + +int vmmc_stream_send(struct tapi_device *tdev, struct tapi_stream *stream, + struct sk_buff *skb) +{ + struct vmmc *vmmc = tdev_to_vmmc(tdev); + struct vmmc_coder *coder = tapi_to_vmmc_stream(stream)->coder; + + vmmc_send_paket(vmmc, coder->id, skb); + return 0; +} +/* +int vmmc_stream_recv(struct vmmc_stream *stream) +{ + tapi_stream_recv(&stream->coder->vmmc->tdev stream->stream, skb); +}*/ diff --git a/package/lqtapi/src/mps/vmmc-stream.h b/package/lqtapi/src/mps/vmmc-stream.h new file mode 100644 index 0000000000..7e029734f8 --- /dev/null +++ b/package/lqtapi/src/mps/vmmc-stream.h @@ -0,0 +1,10 @@ +#ifndef __VMMC_STREAM_H__ +#define __VMMC_STREAM_H__ + +struct tapi_stream *vmmc_stream_alloc(struct tapi_device *tdev); +void vmmc_stream_free(struct tapi_device *tdev, struct tapi_stream *stream); +int vmmc_stream_start(struct tapi_device *tdev, struct tapi_stream *stream); +int vmmc_stream_send(struct tapi_device *tdev, struct tapi_stream *stream, + struct sk_buff *skb); + +#endif diff --git a/package/lqtapi/src/mps/vmmc.c b/package/lqtapi/src/mps/vmmc.c new file mode 100644 index 0000000000..ba7d435af4 --- /dev/null +++ b/package/lqtapi/src/mps/vmmc.c @@ -0,0 +1,90 @@ +static uint32_t magic_init[] = { +/* ALI INIT */ +/*0x0601210c, 0x88002000, 0x20000000, 0x00000000, 0xffffffff,*/ +/* COD_CHAN_SPEECH_ECMD */ +/*0x06016110, 0x2462f700, 0x20002000, 0x00000000, 0x00000000, 0xffffffff,*/ +/* COD_DEC_STAT_ECMD */ +/*0x06017504, 0x00c00000, 0xffffffff,*/ +/* COD_JB_CONF_ECMD */ +/*0x06017208, 0x16410050, 0x005005a0, 0xffffffff,*/ +/* SIG_RTP_SUP */ +/*0x0601500c, 0x00000000, 0x00000060, 0x0712007f, 0xffffffff,*/ +/* SIG_CHAN */ +/*0x06014104, 0xc0190000, 0xffffffff,*/ +/* SIG_CIDS_CTRL_ECMD */ +/*0x06014204, 0x3811e000, 0xffffffff, */ +/* SIG_DTMFATG_DATA */ +/*0x06014b04, 0x00010000, 0xffffffff,*/ +/* SIG_DTMFATG_CTRL */ +/*0x06014304, 0x6a110000, 0xffffffff, +0x0601cc04, 0xe21c2000, 0xffffffff, +0x06014404, 0xb0110000, 0xffffffff, +0x06014a04, 0x04510000, 0xffffffff, +0x06014604, 0x04560000, 0xffffffff, +0x06014104, 0xc0190000, 0xffffffff,*/ +/* COD_CHAN_RTP_SUP_CFG_USD */ +/*0x06017124, 0x00000000, 0x00008880, 0xe3e4e5e6, 0x72727272, 0x72727272, +0x0f7f1261, 0x7374097f, 0xf1f06767, 0x04047675, 0xffffffff,*/ +/* COD_CHAN_RTP_SUP_CFG_DS */ +/* 0x06017920, 0x08006364, 0x65667272, 0x72727272, 0x72720f7f, 0x12617374, +0x097f7170, 0x67670404, 0x76750000, 0xffffffff, */ +/* OPMODE_CMD */ +0x01010004, 0x00010000, 0xffffffff, +0x01000004, 0x00030000, 0xffffffff, +/*0x01010004, 0x00010000, 0xffffffff,*/ +/* COD_CHAN_RTP_SUP_CFG_US */ +/* 0x06017124, 0x00000000, 0x00008880, 0xe3e4e5e6, 0x72727272, 0x72727272, +0x0f7f1261, 0x7374097f, 0xf1f06767, 0x04047675, 0xffffffff, */ +/* COD_CHAN_RTP_SUP_CFG_DS */ +/* 0x06017920, 0x08006364, 0x65667272, 0x72727272, 0x72720f7f, 0x12617374, +0x097f7170, 0x67670404, 0x76750000, 0xffffffff, */ +/* COD_JB_CONF */ +/* 0x06017208, 0x16410050, 0x005005a0, 0xffffffff, */ +/* COD_CHAN_RTP_SUP_CFG_US */ +/*0x06017108, 0x00000000, 0x00008880, 0xffffffff,*/ +/* COD_CHAN_RTP_TIMESTAMP */ +/*0x06017004, 0x00000000, 0xffffffff,*/ +/* SIG_RTP_SUP */ +/* 0x0601500c, 0x00000000, 0x00000062, 0x0712007f, 0xffffffff,*/ +/* SIG_DTMFR_CTRL */ +/*0x06014404, 0xb0010000, 0xffffffff,*/ +/* COD_CHAN_SPEECH */ +/* 0x06016110, 0x0462d600, 0x20002000, 0x00000000, 0x00000000, 0xffffffff, */ +/* ALI_CHAN */ +0x0601210c, 0x88232000, 0x20000000, 0x00000000, 0xffffffff, +/* SIG_CHAN */ +/*0x06014104, 0xc5190000, 0xffffffff,*/ +/* SIG_DTMFR_CTRL_ECMD */ +/*0x06014404, 0x30010000, 0xffffffff,*/ +/* SIG_CHAN_ECMD */ +/*0x06014104, 0x45190000, 0xffffffff,*/ +}; + +static void setup_alm(struct mps *mps, int chan) +{ + uint32_t *data = magic_init; + int size, i; + +/* data[0] = ALI_CHAN_DATA1(1, 0, 0, 1, 1, 0, 0); + data[1] = ALI_CHAN_DATA2(0, 0, 0); + data[2] = ALI_CHAN_DATA3(0, 0);*/ + + size = 1; + for (i = 1; i < ARRAY_SIZE(magic_init); ++i) { + if (magic_init[i] == 0xffffffff) { + + printk("cmd: %x\n", *data); + VMMC_FIFO_fifo_in(&mps->mbox_cmd.downstream, data, size); + size = 0; + data = &magic_init[i+1]; + mdelay(500); + } else { + size += 1; + } + } + + +/* VMMC_FIFO_mailbox_command_write(&mps->mbox_cmd, MPS_CMD_ALI(chan), data);*/ +} + + diff --git a/package/lqtapi/src/mps/vmmc.h b/package/lqtapi/src/mps/vmmc.h new file mode 100644 index 0000000000..f442a9ab4d --- /dev/null +++ b/package/lqtapi/src/mps/vmmc.h @@ -0,0 +1,68 @@ +#ifndef __VMMC_H__ + +#include <linux/list.h> +#include <linux/tapi/tapi.h> + +#include "mps.h" +#include "vmmc-module.h" + +struct vmmc +{ + struct mps *mps; + struct device *dev; + + struct vmmc_port *ports; + + struct completion firmware_loaded_completion; + struct completion cmd_completion; + + struct mps_mailbox mbox_cmd; + struct mps_mailbox mbox_data; + struct mps_fifo fifo_event; + + int irq_fw_loaded; + int irq_mbox_cmd; + int irq_mbox_data; + int irq_event_fifo; + int irq_cmd_error; + + unsigned int num_coders; + struct vmmc_coder *coder; + unsigned long coder_used; + + struct list_head modules; + + struct tapi_device tdev; + + struct sk_buff_head recv_queue; + struct sk_buff_head send_queue; +}; + +static inline struct vmmc *tdev_to_vmmc(struct tapi_device *tdev) +{ + return container_of(tdev, struct vmmc, tdev); +} + +static inline void vmmc_register_module(struct vmmc *vmmc, + struct vmmc_module *module) +{ + list_add_tail(&module->head, &vmmc->modules); +} + +static inline void vmmc_unregister_module(struct vmmc *vmmc, + struct vmmc_module *module) +{ + list_del(&module->head); +} + +int vmmc_command_write(struct vmmc *vmmc, uint32_t cmd, + const uint32_t *data); +int vmmc_command_read(struct vmmc *vmmc, uint32_t cmd, uint32_t *result); + +struct vmmc_coder *vmmc_coder_get(struct vmmc *); +void vmmc_coder_put(struct vmmc *, struct vmmc_coder *); +void vmmc_init_coders(struct vmmc *); + +void vmmc_send_paket(struct vmmc *vmmc, unsigned int chan, struct sk_buff *skb); + +#endif diff --git a/package/lqtapi/src/tapi/Makefile b/package/lqtapi/src/tapi/Makefile new file mode 100644 index 0000000000..2992d4b3a3 --- /dev/null +++ b/package/lqtapi/src/tapi/Makefile @@ -0,0 +1,7 @@ + +tapi-objs := tapi-core.o tapi-port.o tapi-input.o +tapi-objs += tapi-control.o +tapi-objs += tapi-stream.o +tapi-objs += tapi-sysfs-port.o + +obj-m += tapi.o diff --git a/package/lqtapi/src/tapi/tapi-control.c b/package/lqtapi/src/tapi/tapi-control.c new file mode 100644 index 0000000000..a4f245e92f --- /dev/null +++ b/package/lqtapi/src/tapi/tapi-control.c @@ -0,0 +1,193 @@ +#include <linux/cdev.h> +#include <linux/device.h> +#include <linux/fs.h> +#include <linux/list.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/slab.h> + +#include <linux/tapi/tapi.h> +#include <linux/tapi/tapi-ioctl.h> + +/* FIXME Does it acutally make sense to allow more then one application at a + * time to open the control device? For example calling sync from one app will + * also sync all others. */ + +struct tapi_control_file { + struct tapi_device *tdev; + struct list_head links; +}; + +static struct tapi_endpoint *tapi_lookup_endpoint(struct tapi_device *tdev, + unsigned int ep_id) +{ + struct tapi_stream *stream; + + if (ep_id < tdev->num_ports) + return &tdev->ports[ep_id].ep; + + list_for_each_entry(stream, &tdev->streams, head) { + if (stream->ep.id == ep_id) + return &stream->ep; + } + + return ERR_PTR(-ENOENT); +} + +static inline struct tapi_device *inode_to_tdev(struct inode *inode) +{ + return container_of(inode->i_cdev, struct tapi_char_device, cdev)->tdev; +} + +static int tapi_control_open(struct inode *inode, struct file *file) +{ + int ret; + struct tapi_device *tdev = inode_to_tdev(inode); + struct tapi_control_file *tctrl; + + get_device(&tdev->dev); + + tctrl = kzalloc(sizeof(*tctrl), GFP_KERNEL); + if (!tctrl) { + ret = -ENOMEM; + goto err_put_device; + } + + INIT_LIST_HEAD(&tctrl->links); + tctrl->tdev = tdev; + + file->private_data = tctrl; + + return 0; + +err_put_device: + put_device(&tdev->dev); + + return ret; +} + +static int tapi_control_release(struct inode *inode, struct file *file) +{ + struct tapi_control_file *tctrl = file->private_data; + struct tapi_link *link; + + if (tctrl) { + list_for_each_entry(link, &tctrl->links, head) + tapi_link_free(tctrl->tdev, link); + + put_device(&tctrl->tdev->dev); + } + + return 0; +} + +static long tapi_control_ioctl_link_alloc(struct tapi_control_file *tctrl, + unsigned long arg) +{ + struct tapi_link *link; + struct tapi_endpoint *ep1, *ep2; + + ep1 = tapi_lookup_endpoint(tctrl->tdev, arg >> 16); + ep2 = tapi_lookup_endpoint(tctrl->tdev, arg & 0xffff); + + link = tapi_link_alloc(tctrl->tdev, ep1, ep2); + if (IS_ERR(link)) + return PTR_ERR(link); + + list_add_tail(&link->head, &tctrl->links); + + return link->id; +} + +struct tapi_link *tapi_control_lookup_link(struct tapi_control_file *tctrl, + unsigned int id) +{ + struct tapi_link *link; + + list_for_each_entry(link, &tctrl->links, head) { + if (link->id == id) + return link; + } + + return NULL; +} + +static long tapi_control_ioctl_link_free(struct tapi_control_file *tctrl, + unsigned long arg) +{ + struct tapi_link *link = tapi_control_lookup_link(tctrl, arg); + if (!link) + return -ENOENT; + + tapi_link_free(tctrl->tdev, link); + list_del(&link->head); + + return 0; +} + +static long tapi_control_ioctl_link_enable(struct tapi_control_file *tctrl, + unsigned long arg) +{ + struct tapi_link *link = tapi_control_lookup_link(tctrl, arg); + if (!link) + return -ENOENT; + + return tapi_link_enable(tctrl->tdev, link); +} + +static long tapi_control_ioctl_link_disable(struct tapi_control_file *tctrl, + unsigned long arg) +{ + struct tapi_link *link = tapi_control_lookup_link(tctrl, arg); + if (!link) + return -ENOENT; + + return tapi_link_disable(tctrl->tdev, link); +} + +static long tapi_control_ioctl_sync(struct tapi_control_file *tctrl) +{ + return tapi_sync(tctrl->tdev); +} + +static long tapi_control_ioctl(struct file *file, unsigned int cmd, + unsigned long arg) +{ + int ret; + struct tapi_control_file *tctrl = file->private_data; + + switch (cmd) { + case TAPI_CONTROL_IOCTL_LINK_ALLOC: + ret = tapi_control_ioctl_link_alloc(tctrl, arg); + break; + case TAPI_CONTROL_IOCTL_LINK_FREE: + ret = tapi_control_ioctl_link_free(tctrl, arg); + break; + case TAPI_CONTROL_IOCTL_LINK_ENABLE: + ret = tapi_control_ioctl_link_enable(tctrl, arg); + break; + case TAPI_CONTROL_IOCTL_LINK_DISABLE: + ret = tapi_control_ioctl_link_disable(tctrl, arg); + break; + case TAPI_CONTROL_IOCTL_SYNC: + ret = tapi_control_ioctl_sync(tctrl); + break; + default: + return -EINVAL; + } + + return ret; +} + +static const struct file_operations tapi_control_file_ops = { + .owner = THIS_MODULE, + .open = tapi_control_open, + .release = tapi_control_release, + .unlocked_ioctl = tapi_control_ioctl, +}; + +int tapi_register_control_device(struct tapi_device* tdev) +{ + dev_set_name(&tdev->control_dev.dev, "tapi%uC", tdev->id); + return tapi_char_device_register(tdev, &tdev->control_dev, &tapi_control_file_ops); +} diff --git a/package/lqtapi/src/tapi/tapi-core.c b/package/lqtapi/src/tapi/tapi-core.c new file mode 100644 index 0000000000..2e112a3769 --- /dev/null +++ b/package/lqtapi/src/tapi/tapi-core.c @@ -0,0 +1,250 @@ +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/device.h> +#include <linux/list.h> + +#include <linux/cdev.h> +#include <linux/err.h> + +#include <linux/tapi/tapi.h> + + +void tapi_alloc_input(struct tapi_device *tdev, struct tapi_port *port); +int tapi_register_port_device(struct tapi_device* tdev, struct tapi_port *port); +int tapi_register_stream_device(struct tapi_device* tdev); +int tapi_register_control_device(struct tapi_device* tdev); + +static struct class *tapi_class; +static int tapi_major; + +#define TAPI_MAX_MINORS 255 + +static bool tapi_minors[TAPI_MAX_MINORS]; + +static int tapi_get_free_minor(void) +{ + int i; + for (i = 0; i < TAPI_MAX_MINORS; ++i) { + if (!tapi_minors[i]) { + tapi_minors[i] = true; + return i; + } + } + + return -1; +} + +/* +int tapi_port_send_dtmf_events(struct tapi_device *tdev, unsigned int port, struct tapi_dtmf *, size_t num_events, unsigned int dealy) +{ +} +EXPORT_SYMBOL_GPL(tapi_port_send_dtmf_events); +*/ + +void tapi_report_hook_event(struct tapi_device *tdev, struct tapi_port *port, + bool on) +{ + struct tapi_event event; + event.type = TAPI_EVENT_TYPE_HOOK; + event.port = port->id; + event.hook.on = on; + + tapi_report_event(tdev, &event); +} +EXPORT_SYMBOL_GPL(tapi_report_hook_event); + +void tapi_report_dtmf_event(struct tapi_device *tdev, struct tapi_port *port, + unsigned char code) +{ + struct tapi_event event; + event.type = TAPI_EVENT_TYPE_DTMF; + event.port = port->id; + event.dtmf.code = code; + + tapi_report_event(tdev, &event); +} +EXPORT_SYMBOL_GPL(tapi_report_dtmf_event); + +struct tapi_stream *tapi_stream_alloc(struct tapi_device *tdev) +{ + struct tapi_stream *stream; + printk("tdev %p\n", tdev); + + if (!tdev->ops || !tdev->ops->stream_alloc) + return ERR_PTR(-ENOSYS); + + stream = tdev->ops->stream_alloc(tdev); + printk("stream %p\n", stream); + if (IS_ERR(stream)) + return stream; + + stream->id = atomic_inc_return(&tdev->stream_id) - 1; + stream->ep.id = stream->id; + +/* mutex_lock(&tdev->lock);*/ + list_add_tail(&stream->head, &tdev->streams); +/* mutex_unlock(&tdev->lock);*/ + + return stream; +} +EXPORT_SYMBOL_GPL(tapi_stream_alloc); + +void tapi_stream_free(struct tapi_device *tdev, struct tapi_stream *stream) +{ + mutex_lock(&tdev->lock); + list_del(&stream->head); + mutex_unlock(&tdev->lock); + + tdev->ops->stream_free(tdev, stream); +} +EXPORT_SYMBOL_GPL(tapi_stream_free); + +struct tapi_link *tapi_link_alloc(struct tapi_device *tdev, + struct tapi_endpoint *ep1, struct tapi_endpoint *ep2) +{ + struct tapi_link *link; + + if (!tdev->ops || !tdev->ops->link_alloc) + return ERR_PTR(-ENOSYS); + + link = tdev->ops->link_alloc(tdev, ep1, ep2); + if (IS_ERR(link)) + return link; + + link->id = atomic_inc_return(&tdev->link_id) - 1; + +/* + mutex_lock(&tdev->lock); + list_add_tail(&link->head, &tdev->links); + mutex_unlock(&tdev->lock); +*/ + return link; +} +EXPORT_SYMBOL_GPL(tapi_link_alloc); + +void tapi_link_free(struct tapi_device *tdev, struct tapi_link *link) +{ +/* + mutex_lock(&tdev->lock); + list_del(&link->head); + mutex_unlock(&tdev->lock); +*/ + tdev->ops->link_free(tdev, link); +} +EXPORT_SYMBOL_GPL(tapi_link_free); + +int tapi_char_device_register(struct tapi_device *tdev, + struct tapi_char_device *tchrdev, const struct file_operations *fops) +{ + int ret; + struct device *dev = &tchrdev->dev; + dev_t devt; + int minor = tapi_get_free_minor(); + + devt = MKDEV(tapi_major, minor); + + dev->devt = devt; + dev->class = tapi_class; + dev->parent = &tdev->dev; + + tchrdev->tdev = tdev; + + cdev_init(&tchrdev->cdev, fops); + tchrdev->cdev.owner = THIS_MODULE; + ret = cdev_add(&tchrdev->cdev, devt, 1); + if (ret) + return ret; + + ret = device_register(&tchrdev->dev); + if (ret) + goto err_cdev_del; + + return 0; + +err_cdev_del: + cdev_del(&tchrdev->cdev); + + return ret; +} + +int tapi_device_register(struct tapi_device *tdev, const char *name, + struct device *parent) +{ + static atomic_t tapi_device_id = ATOMIC_INIT(0); + int ret, i; + + tdev->dev.class = tapi_class; + tdev->dev.parent = parent; + dev_set_name(&tdev->dev, "%s", name); + + ret = device_register(&tdev->dev); + if (ret) + return ret; + + tdev->id = atomic_inc_return(&tapi_device_id) - 1; + + mutex_init(&tdev->lock); + INIT_LIST_HEAD(&tdev->streams); + INIT_LIST_HEAD(&tdev->links); + atomic_set(&tdev->link_id, 0); + atomic_set(&tdev->stream_id, tdev->num_ports); + + tapi_register_stream_device(tdev); + tapi_register_control_device(tdev); + + for (i = 0; i < tdev->num_ports; ++i) { + tapi_port_alloc(tdev, i); + tapi_alloc_input(tdev, &tdev->ports[i]); + tapi_register_port_device(tdev, &tdev->ports[i]); + tdev->ports[i].id = i; + tdev->ports[i].ep.id = i; + } + + return 0; +} +EXPORT_SYMBOL_GPL(tapi_device_register); + +void tapi_device_unregister(struct tapi_device *tdev) +{ + device_unregister(&tdev->dev); +} +EXPORT_SYMBOL_GPL(tapi_device_unregister); + +static int __init tapi_class_init(void) +{ + int ret; + dev_t dev; + + tapi_class = class_create(THIS_MODULE, "tapi"); + + if (IS_ERR(tapi_class)) { + ret = PTR_ERR(tapi_class); + printk(KERN_ERR "tapi: Failed to create device class: %d\n", ret); + goto err; + } + + ret = alloc_chrdev_region(&dev, 0, TAPI_MAX_MINORS, "tapi"); + if (ret) { + printk(KERN_ERR "tapi: Failed to allocate chrdev region: %d\n", ret); + goto err_class_destory; + } + tapi_major = MAJOR(dev); + + return 0; +err_class_destory: + class_destroy(tapi_class); +err: + return ret; +} +subsys_initcall(tapi_class_init); + +static void __exit tapi_class_exit(void) +{ + unregister_chrdev_region(MKDEV(tapi_major, 0), TAPI_MAX_MINORS); + class_destroy(tapi_class); +} +module_exit(tapi_class_exit); + +MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); +MODULE_DESCRIPTION("TAPI class"); +MODULE_LICENSE("GPL"); diff --git a/package/lqtapi/src/tapi/tapi-input.c b/package/lqtapi/src/tapi/tapi-input.c new file mode 100644 index 0000000000..999aaefa98 --- /dev/null +++ b/package/lqtapi/src/tapi/tapi-input.c @@ -0,0 +1,99 @@ + +#include <linux/tapi/tapi.h> + +#include <linux/input.h> + +static unsigned short tapi_keycodes[] = { + [0] = KEY_NUMERIC_0, + [1] = KEY_NUMERIC_1, + [2] = KEY_NUMERIC_2, + [3] = KEY_NUMERIC_3, + [4] = KEY_NUMERIC_4, + [5] = KEY_NUMERIC_5, + [6] = KEY_NUMERIC_6, + [7] = KEY_NUMERIC_7, + [8] = KEY_NUMERIC_8, + [9] = KEY_NUMERIC_9, + [10] = KEY_NUMERIC_STAR, + [11] = KEY_NUMERIC_POUND, + [12] = KEY_ENTER, + [13] = KEY_ESC, +}; + +static int tapi_input_event(struct input_dev *input, unsigned int type, + unsigned int code, int value) +{ + struct tapi_device *tdev = dev_to_tapi(input->dev.parent); + struct tapi_port *port = input_get_drvdata(input); + + + if (type != EV_SND || code != SND_BELL) + return -EINVAL; + + tapi_port_set_ring(tdev, port, value); + + return 0; +} + +void tapi_alloc_input(struct tapi_device *tdev, struct tapi_port *port) +{ + struct input_dev *input; + int i; + char *phys; + + input = input_allocate_device(); + + phys = kzalloc(sizeof("tapi/input000"), GFP_KERNEL); + sprintf(phys, "tapi/input%d", port->id); + + input->name = "tapi"; + input->phys = phys; + input->id.bustype = BUS_HOST; + input->dev.parent = &tdev->dev; + input->evbit[0] = BIT(EV_KEY) | BIT(EV_SND); + input->sndbit[0] = BIT(SND_BELL); + + input->event = tapi_input_event; + + input->keycodesize = sizeof(unsigned short); + input->keycodemax = ARRAY_SIZE(tapi_keycodes); + input->keycode = tapi_keycodes; + + port->input = input; + + for (i = 0; i < ARRAY_SIZE(tapi_keycodes); ++i) + __set_bit(tapi_keycodes[i], input->keybit); + + input_set_drvdata(input, port); + input_register_device(input); +} + +void tapi_report_event(struct tapi_device *tdev, + struct tapi_event *event) +{ + unsigned short key_code; + struct input_dev *input; + + if (!tdev || !tdev->ports) + return; + + switch (event->type) { + case TAPI_EVENT_TYPE_HOOK: + if (event->hook.on) + key_code = KEY_ENTER; + else + key_code = KEY_ESC; + break; + case TAPI_EVENT_TYPE_DTMF: + key_code = tapi_keycodes[event->dtmf.code]; + break; + default: + return; + } + + input = tdev->ports[event->port].input; + input_report_key(input, key_code, 1); + input_sync(input); + input_report_key(input, key_code, 0); + input_sync(input); +} diff --git a/package/lqtapi/src/tapi/tapi-nl.c b/package/lqtapi/src/tapi/tapi-nl.c new file mode 100644 index 0000000000..c066742b65 --- /dev/null +++ b/package/lqtapi/src/tapi/tapi-nl.c @@ -0,0 +1,62 @@ + +static struct tapi_attr default_port[] = { + [PORTS] = { + .type = TAPI_TYPE_PORTS, + .name = "ports", + .description = "foobar", + .set = tapi_set_ports, + .get = tapi_get_ports, + }, +}; + +static const struct nla_policy tapi_policy[] = { + [TAPI_ATTR_ID] = { .type = NLA_U32 }, + [TAPI_ATTR_PORT] = { .type = NLA_U32 }, + [TAPI_ATTR_ENDPOINT] = { .type = NLA_U32 }, + [TAPI_ATTR_STREAM] = { .type = NLA_U32 } +}; + +static const struct nla_policy tapi_port_policy[] = { + [TAPI_PORT_ID] = { .type = NLA_U32 }, +}; + +static const struct nla_policy tapi_endpoint_policy[] = { + [TAPI_ENDPOINT_ID] = { .type = NLA_U32 }, +}; + +static const struct nla_policy tapi_stream_policy[] = { + [TAPI_STREAM_ID] = { .type = NLA_U32 }, +}; + +static struct genl_family tapi_nl_family = { + .id = GENL_ID_GENERATE, + .name = "tapi", + .hdrsize = 0, + .version = 1, + .maxattr = ARRAY_SIZE(tapi_policy), +}; + + + +static struct genl_ops tapi_nl_ops[] = { + TAPI_NL_OP(TAPI_CMD_LIST, list_attr), + +}; + +static int __init tapi_nl_init(void) +{ + ret = genl_unregister_family(&tapi_nl_family); + if (ret) + return ret; + + genl_register_ops(&tapi_nl_family, tapi_nl_ops); + + return 0; +} +module_init(tapi_nl_init); + +static void __exit tapi_nl_exit(void) +{ + genl_unregister_family(&tapi_nl_family); +} + diff --git a/package/lqtapi/src/tapi/tapi-port.c b/package/lqtapi/src/tapi/tapi-port.c new file mode 100644 index 0000000000..0ef9555676 --- /dev/null +++ b/package/lqtapi/src/tapi/tapi-port.c @@ -0,0 +1,82 @@ +#include <linux/cdev.h> +#include <linux/fs.h> +#include <linux/list.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/slab.h> + +#include <linux/tapi/tapi.h> +#include <linux/tapi/tapi-ioctl.h> + +static inline struct tapi_port *tapi_char_device_to_port(struct tapi_char_device *chrdev) +{ + return container_of(chrdev, struct tapi_port, chrdev); +} + +static int tapi_port_open(struct inode *inode, struct file *file) +{ + struct tapi_device *tdev = cdev_to_tapi_char_device(inode->i_cdev)->tdev; + + get_device(&tdev->dev); + file->private_data = cdev_to_tapi_char_device(inode->i_cdev); + + return 0; +} + +static int tapi_port_release(struct inode *inode, struct file *file) +{ + struct tapi_device *tdev = cdev_to_tapi_char_device(inode->i_cdev)->tdev; + + put_device(&tdev->dev); + + return 0; +} + +static long tapi_port_ioctl_get_endpoint(struct tapi_device *tdev, + struct tapi_port *port, unsigned long arg) +{ + return port->ep.id; +} + +static long tapi_port_ioctl_set_ring(struct tapi_device *tdev, + struct tapi_port *port, unsigned long arg) +{ + tapi_port_set_ring(tdev, port, arg); + return 0; +} + +static long tapi_port_ioctl(struct file *file, unsigned int cmd, + unsigned long arg) +{ + int ret; + struct tapi_char_device *tchrdev = file->private_data; + struct tapi_device *tdev = tchrdev->tdev; + struct tapi_port *port = tapi_char_device_to_port(tchrdev); + + switch (cmd) { + case TAPI_PORT_IOCTL_GET_ENDPOINT: + ret = tapi_port_ioctl_get_endpoint(tdev, port, arg); + break; + case TAPI_PORT_IOCTL_SET_RING: + ret = tapi_port_ioctl_set_ring(tdev, port, arg); + break; + default: + ret = -EINVAL; + break; + } + + return ret; +} + +static const struct file_operations tapi_port_file_ops = { + .owner = THIS_MODULE, + .open = tapi_port_open, + .release = tapi_port_release, + .unlocked_ioctl = tapi_port_ioctl, +}; + +int tapi_register_port_device(struct tapi_device* tdev, struct tapi_port *port) +{ + dev_set_name(&port->chrdev.dev, "tapi%uP%u", tdev->id, port->id); + return tapi_char_device_register(tdev, &port->chrdev, &tapi_port_file_ops); +} diff --git a/package/lqtapi/src/tapi/tapi-stream.c b/package/lqtapi/src/tapi/tapi-stream.c new file mode 100644 index 0000000000..7277a5c61c --- /dev/null +++ b/package/lqtapi/src/tapi/tapi-stream.c @@ -0,0 +1,201 @@ +#include <linux/cdev.h> +#include <linux/device.h> +#include <linux/fs.h> +#include <linux/list.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/signal.h> +#include <linux/sched.h> +#include <linux/poll.h> + +#include <linux/tapi/tapi.h> +#include <linux/tapi/tapi-ioctl.h> + + +struct tapi_stream_file { + struct tapi_device *tdev; + struct tapi_stream *stream; +}; + +static inline struct tapi_device *inode_to_tdev(struct inode *inode) +{ + return container_of(inode->i_cdev, struct tapi_char_device, cdev)->tdev; +} + +static int tapi_stream_open(struct inode *inode, struct file *file) +{ + int ret; + struct tapi_device *tdev = inode_to_tdev(inode); + struct tapi_stream_file *stream; + + get_device(&tdev->dev); + + stream = kzalloc(sizeof(*stream), GFP_KERNEL); + if (!stream) { + ret = -ENOMEM; + goto err_put; + } + + stream->stream = tapi_stream_alloc(tdev); + if (IS_ERR(stream->stream)) { + ret = PTR_ERR(stream->stream); + goto err_free; + } + stream->tdev = tdev; + + init_waitqueue_head(&stream->stream->recv_wait); + skb_queue_head_init(&stream->stream->recv_queue); + + file->private_data = stream; + + return 0; + +err_free: + kfree(stream); +err_put: + put_device(&tdev->dev); + return ret; +} + +static int tapi_stream_release(struct inode *inode, struct file *file) +{ + struct tapi_stream_file *stream = file->private_data; + + if (stream) { + tapi_stream_free(stream->tdev, stream->stream); + put_device(&stream->tdev->dev); + kfree(stream); + } + + return 0; +} + +static long tapi_stream_ioctl(struct file *file, unsigned int cmd, + unsigned long arg) +{ + int ret = 0; + struct tapi_stream_file *stream = file->private_data; + struct tapi_device *tdev = stream->tdev; + + switch (cmd) { + case TAPI_STREAM_IOCTL_GET_ENDPOINT: + ret = stream->stream->ep.id; + break; + case TAPI_STREAM_IOCTL_CONFIGURE: + break; + case TAPI_STREAM_IOCTL_START: + ret = tapi_stream_start(tdev, stream->stream); + break; + case TAPI_STREAM_IOCTL_STOP: + ret = tapi_stream_stop(tdev, stream->stream); + break; + default: + ret = -EINVAL; + break; + } + + return ret; +} + +static unsigned int tapi_stream_poll(struct file *file, struct poll_table_struct *wait) +{ + struct tapi_stream_file *stream = file->private_data; + int ret; + + poll_wait(file, &stream->stream->recv_wait, wait); + + ret = POLLOUT; + + if (!skb_queue_empty(&stream->stream->recv_queue)) + ret |= POLLIN; + + return ret; +} + +static ssize_t tapi_stream_read(struct file *file, char __user *buffer, + size_t count, loff_t *offset) +{ + struct tapi_stream_file *stream = file->private_data; + struct sk_buff *skb; + + skb = skb_dequeue(&stream->stream->recv_queue); + if (!skb) { + if (file->f_flags & O_NONBLOCK) + return -EAGAIN; + + do { + interruptible_sleep_on(&stream->stream->recv_wait); + skb = skb_dequeue(&stream->stream->recv_queue); + } while (skb == NULL && !signal_pending(current)); + + if (skb == NULL) + return -ERESTARTNOHAND; + } + + if (skb->len > count) { + skb_queue_head(&stream->stream->recv_queue, skb); + return -EMSGSIZE; + } + + if (copy_to_user(buffer, skb->data, skb->len)) { + skb_queue_head(&stream->stream->recv_queue, skb); + return -EFAULT; + } + + count = skb->len; + + kfree_skb(skb); + + return count; +} + +static ssize_t tapi_stream_write(struct file *file, const char __user *buffer, + size_t count, loff_t *ppos) +{ + struct tapi_stream_file *stream = file->private_data; + struct tapi_device *tdev = stream->tdev; + struct sk_buff *skb; + + if (count == 0) + return 0; + + skb = alloc_skb(count, GFP_USER); + if (!skb) + return -ENOMEM; + + if (copy_from_user(skb_put(skb, count), buffer, count)) { + kfree_skb(skb); + return -EFAULT; + } + + tdev->ops->stream_send(tdev, stream->stream, skb); + + return count; +} + +static const struct file_operations tapi_stream_file_ops = { + .owner = THIS_MODULE, + .read = tapi_stream_read, + .write = tapi_stream_write, + .open = tapi_stream_open, + .release = tapi_stream_release, + .poll = tapi_stream_poll, + .unlocked_ioctl = tapi_stream_ioctl, +}; + +int tapi_register_stream_device(struct tapi_device* tdev) +{ + dev_set_name(&tdev->stream_dev.dev, "tapi%uS", tdev->id); + return tapi_char_device_register(tdev, &tdev->stream_dev, &tapi_stream_file_ops); +} + +int tapi_stream_recv(struct tapi_device *tdev, struct tapi_stream * stream, + struct sk_buff *skb) +{ + skb_queue_tail(&stream->recv_queue, skb); + wake_up(&stream->recv_wait); + + return 0; +} +EXPORT_SYMBOL_GPL(tapi_stream_recv); diff --git a/package/lqtapi/src/tapi/tapi-sysfs-port.c b/package/lqtapi/src/tapi/tapi-sysfs-port.c new file mode 100644 index 0000000000..f6e5ee0661 --- /dev/null +++ b/package/lqtapi/src/tapi/tapi-sysfs-port.c @@ -0,0 +1,107 @@ +#include <linux/device.h> +#include <linux/kernel.h> +#include <linux/sysfs.h> + +#include <linux/err.h> +#include <linux/tapi/tapi.h> + +struct tapi_sysfs_port { + struct tapi_device *tdev; + unsigned int id; + struct kobject kobj; +}; + +struct tapi_sysfs_entry { + ssize_t (*show)(struct tapi_device *, unsigned int port, char *); + ssize_t (*store)(struct tapi_device *, unsigned int port, const char *, size_t); + struct attribute attr; +}; + +static ssize_t tapi_port_store(struct kobject *kobj, struct attribute *attr, + const char *s, size_t len) +{ + struct tapi_sysfs_port *port = container_of(kobj, struct tapi_sysfs_port, kobj); + struct tapi_sysfs_entry *entry = container_of(attr, struct tapi_sysfs_entry, + attr); + + if (!entry->store) + return -ENOSYS; + + return entry->store(port->tdev, port->id, s, len); +} + +static ssize_t tapi_port_show(struct kobject *kobj, struct attribute *attr, + char *s) +{ + return -ENOSYS; +} + +#define TAPI_PORT_ATTR(_name, _mode, _show, _store) \ + struct tapi_sysfs_entry tapi_port_ ## _name ## _attr = \ + __ATTR(_name, _mode, _show, _store) + +static int tapi_port_store_ring(struct tapi_device *tdev, unsigned int port, + const char *s, size_t len) +{ + int ret; + unsigned long val; + + ret = strict_strtoul(s, 10, &val); + + if (ret) + return ret; + + ret = tapi_port_set_ring(tdev, &tdev->ports[port], val); + if (ret) + return ret; + return len; +} + +static TAPI_PORT_ATTR(ring, 0644, NULL, tapi_port_store_ring); + +static struct attribute *tapi_port_default_attrs[] = { + &tapi_port_ring_attr.attr, + NULL, +}; + +static void tapi_port_free(struct kobject *kobj) +{ + struct tapi_sysfs_port *port = container_of(kobj, struct tapi_sysfs_port, kobj); + kfree(port); +} + +static struct sysfs_ops tapi_port_sysfs_ops = { + .show = tapi_port_show, + .store = tapi_port_store, +}; + +static struct kobj_type tapi_port_ktype = { + .release = tapi_port_free, + .sysfs_ops = &tapi_port_sysfs_ops, + .default_attrs = tapi_port_default_attrs, +}; + +struct tapi_sysfs_port *tapi_port_alloc(struct tapi_device *tdev, unsigned int id) +{ + struct tapi_sysfs_port *port; + int ret; + + port = kzalloc(sizeof(*port), GFP_KERNEL); + port->tdev = tdev; + port->id = id; + + ret = kobject_init_and_add(&port->kobj, &tapi_port_ktype, &tdev->dev.kobj, + "port%d", id); + if (ret) { + kfree(port); + return ERR_PTR(ret); + } + + return port; +} + +void tapi_port_delete(struct tapi_sysfs_port *port) +{ + kobject_del(&port->kobj); + kobject_put(&port->kobj); +} |