summaryrefslogtreecommitdiffstats
path: root/package
diff options
context:
space:
mode:
authorblogic <blogic@3c298f89-4303-0410-b956-a3cf2f4a3e73>2010-11-03 19:12:34 +0000
committerblogic <blogic@3c298f89-4303-0410-b956-a3cf2f4a3e73>2010-11-03 19:12:34 +0000
commit6ea7ec75d2fe0dd52bc3e86135412bfc92c3158c (patch)
tree1b900bd32ce90c62703905c460db950c7c2ba9ec /package
parent74f6f2f5a244b9ba7aec5d7114fe763f8c20e96c (diff)
[ifxmips]
* adds a rewrite of the tapi drivers + sip app. this is the result of lars' gsoc 2010 project, Thanks ! git-svn-id: svn://svn.openwrt.org/openwrt/trunk@23840 3c298f89-4303-0410-b956-a3cf2f4a3e73
Diffstat (limited to 'package')
-rw-r--r--package/libtapi/Makefile43
-rw-r--r--package/libtapi/src/Makefile27
-rw-r--r--package/libtapi/src/dialdetector.c109
-rw-r--r--package/libtapi/src/dialdetector.h109
-rw-r--r--package/libtapi/src/events.c58
-rw-r--r--package/libtapi/src/events.h18
-rw-r--r--package/libtapi/src/list.h601
-rw-r--r--package/libtapi/src/tapi-device.c46
-rw-r--r--package/libtapi/src/tapi-device.h25
-rw-r--r--package/libtapi/src/tapi-ioctl.h20
-rw-r--r--package/libtapi/src/tapi-port.c126
-rw-r--r--package/libtapi/src/tapi-port.h66
-rw-r--r--package/libtapi/src/tapi-session.c122
-rw-r--r--package/libtapi/src/tapi-session.h14
-rw-r--r--package/libtapi/src/tapi-stream.c42
-rw-r--r--package/libtapi/src/tapi-stream.h19
-rw-r--r--package/libtapi/src/tapidemo.c56
-rw-r--r--package/libtapi/src/timer_fd.c33
-rw-r--r--package/libtapi/src/timerfd.h61
-rw-r--r--package/lqtapi/Makefile73
-rw-r--r--package/lqtapi/src/Makefile1
-rw-r--r--package/lqtapi/src/include/linux/tapi/tapi-event.h37
-rw-r--r--package/lqtapi/src/include/linux/tapi/tapi-ioctl.h20
-rw-r--r--package/lqtapi/src/include/linux/tapi/tapi.h231
-rw-r--r--package/lqtapi/src/mps/Makefile8
-rw-r--r--package/lqtapi/src/mps/mps-core.c238
-rw-r--r--package/lqtapi/src/mps/mps-fifo.c102
-rw-r--r--package/lqtapi/src/mps/mps-irq.c157
-rw-r--r--package/lqtapi/src/mps/mps-irq.h18
-rw-r--r--package/lqtapi/src/mps/mps-msg.c19
-rw-r--r--package/lqtapi/src/mps/mps-msg.h87
-rw-r--r--package/lqtapi/src/mps/mps.h85
-rw-r--r--package/lqtapi/src/mps/vmmc-alm.c129
-rw-r--r--package/lqtapi/src/mps/vmmc-alm.h42
-rw-r--r--package/lqtapi/src/mps/vmmc-cmds.h98
-rw-r--r--package/lqtapi/src/mps/vmmc-coder.c221
-rw-r--r--package/lqtapi/src/mps/vmmc-coder.h14
-rw-r--r--package/lqtapi/src/mps/vmmc-core.c646
-rw-r--r--package/lqtapi/src/mps/vmmc-link.c52
-rw-r--r--package/lqtapi/src/mps/vmmc-link.h10
-rw-r--r--package/lqtapi/src/mps/vmmc-module.c130
-rw-r--r--package/lqtapi/src/mps/vmmc-module.h64
-rw-r--r--package/lqtapi/src/mps/vmmc-port.c48
-rw-r--r--package/lqtapi/src/mps/vmmc-port.h22
-rw-r--r--package/lqtapi/src/mps/vmmc-sig.h19
-rw-r--r--package/lqtapi/src/mps/vmmc-signal.c69
-rw-r--r--package/lqtapi/src/mps/vmmc-stream.c73
-rw-r--r--package/lqtapi/src/mps/vmmc-stream.h10
-rw-r--r--package/lqtapi/src/mps/vmmc.c90
-rw-r--r--package/lqtapi/src/mps/vmmc.h68
-rw-r--r--package/lqtapi/src/tapi/Makefile7
-rw-r--r--package/lqtapi/src/tapi/tapi-control.c193
-rw-r--r--package/lqtapi/src/tapi/tapi-core.c250
-rw-r--r--package/lqtapi/src/tapi/tapi-input.c99
-rw-r--r--package/lqtapi/src/tapi/tapi-nl.c62
-rw-r--r--package/lqtapi/src/tapi/tapi-port.c82
-rw-r--r--package/lqtapi/src/tapi/tapi-stream.c201
-rw-r--r--package/lqtapi/src/tapi/tapi-sysfs-port.c107
-rw-r--r--package/pjsip/Makefile58
-rw-r--r--package/tapi_sip/Makefile41
-rw-r--r--package/tapi_sip/files/telephony.conf28
-rwxr-xr-xpackage/tapi_sip/files/telephony.init50
-rw-r--r--package/tapi_sip/src/Makefile21
-rw-r--r--package/tapi_sip/src/agent.h41
-rw-r--r--package/tapi_sip/src/contact.c162
-rw-r--r--package/tapi_sip/src/contact.h33
-rw-r--r--package/tapi_sip/src/dialdetector.c148
-rw-r--r--package/tapi_sip/src/dialdetector.h49
-rw-r--r--package/tapi_sip/src/list.h601
-rw-r--r--package/tapi_sip/src/session.c84
-rw-r--r--package/tapi_sip/src/session.h14
-rw-r--r--package/tapi_sip/src/sip_agent.h27
-rw-r--r--package/tapi_sip/src/sip_client.c800
-rw-r--r--package/tapi_sip/src/sip_client.h61
-rw-r--r--package/tapi_sip/src/stun.c243
-rw-r--r--package/tapi_sip/src/stun.h12
-rw-r--r--package/tapi_sip/src/tapi_agent.c105
-rw-r--r--package/tapi_sip/src/tapi_agent.h37
-rw-r--r--package/tapi_sip/src/tapi_sip.c179
79 files changed, 8271 insertions, 0 deletions
diff --git a/package/libtapi/Makefile b/package/libtapi/Makefile
new file mode 100644
index 000000000..f85d55476
--- /dev/null
+++ b/package/libtapi/Makefile
@@ -0,0 +1,43 @@
+#
+# This is free software, licensed under the GNU General Public License v2.
+# See /LICENSE for more information.
+#
+
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=libtapi
+PKG_RELEASE:=1
+
+include $(INCLUDE_DIR)/package.mk
+include $(INCLUDE_DIR)/kernel.mk
+
+define Package/libtapi
+ SECTION:=libs
+ CATEGORY:=Libraries
+ TITLE:=libtapi
+ DEPENDS:=@TARGET_ifxmips_danube
+endef
+
+define Build/Prepare
+ mkdir -p $(PKG_BUILD_DIR)
+ $(CP) ./src/* $(PKG_BUILD_DIR)/
+endef
+
+define Build/Compile
+ CFLAGS="$(TARGT_CPPFLAGS) $(TARGET_CFLAGS)" \
+ $(MAKE) -C $(PKG_BUILD_DIR) \
+ $(TARGET_CONFIGURE_OPTS)
+endef
+
+define Build/InstallDev
+ $(INSTALL_DIR) $(1)/usr/{include,lib}
+ $(INSTALL_DATA) $(PKG_BUILD_DIR)/*.h $(1)/usr/include/
+ $(INSTALL_DATA) $(PKG_BUILD_DIR)/*.so $(1)/usr/lib/
+endef
+
+define Package/libtapi/install
+ $(INSTALL_DIR) $(1)/usr/lib
+ $(INSTALL_BIN) $(PKG_BUILD_DIR)/libtapi.so* $(1)/usr/lib/
+endef
+
+$(eval $(call BuildPackage,libtapi))
diff --git a/package/libtapi/src/Makefile b/package/libtapi/src/Makefile
new file mode 100644
index 000000000..70b46eb57
--- /dev/null
+++ b/package/libtapi/src/Makefile
@@ -0,0 +1,27 @@
+ifndef CFLAGS
+CFLAGS = -O2 -g -I ../src
+endif
+
+FPIC=-fPIC
+
+all: libtapi.so
+
+%.o: %.c
+ $(CC) $(CFLAGS) -c -o $@ $^ $(FPIC)
+
+TAPI_OBJS = \
+ timer_fd.o \
+ events.o \
+ tapi-port.o \
+ tapi-device.o \
+ tapi-session.o \
+ tapi-stream.o
+
+tapidemo: tapidemo.o libtapi.so
+ $(CC) $(LDFLAGS) -o $@ $^
+
+libtapi.so: $(TAPI_OBJS)
+ $(CC) $(LDFLAGS) -shared -o $@ $^ $(FPIC)
+
+clean:
+ rm -rf *.o *.so
diff --git a/package/libtapi/src/dialdetector.c b/package/libtapi/src/dialdetector.c
new file mode 100644
index 000000000..ff91b37aa
--- /dev/null
+++ b/package/libtapi/src/dialdetector.c
@@ -0,0 +1,109 @@
+#include <linux/input.h>
+#include <sys/epoll.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "events.h"
+#include "timerfd.h"
+
+#include "tapi-port.h"
+#include "dialdetector.h"
+
+static const struct itimerspec dialdetector_timeout = {
+ .it_value.tv_sec = 3,
+};
+
+static void dialdetector_note_digit(struct dialdetector *d, unsigned char digit)
+{
+ printf("note digit: %d\n", d->num_digits);
+ d->digits[d->num_digits] = digit;
+ ++d->num_digits;
+}
+
+static void dialdetector_reset(struct dialdetector *d)
+{
+ event_unregister(d->timer_fd);
+ d->num_digits = 0;
+ d->state = DIALDETECTOR_IDLE;
+}
+
+static bool dialdetector_timeout_event(int events, void *data)
+{
+ char num[20];
+ struct dialdetector *dialdetector = data;
+ int i;
+
+ for (i = 0; i < dialdetector->num_digits; ++i) {
+ num[i] = '0' + dialdetector->digits[i];
+ }
+ num[i] = '\0';
+
+ printf("Dialing: %s\n", num);
+ dialdetector->dial_callback(dialdetector->port, dialdetector->num_digits,
+ dialdetector->digits);
+
+ dialdetector_reset(dialdetector);
+
+ return false;
+}
+
+static void dialdetector_port_event(struct tapi_port *port,
+ struct tapi_event *event, void *data)
+{
+ struct dialdetector *d = data;
+
+ printf("port event: %d %d\n", d->state, event->hook.on);
+
+ switch (d->state) {
+ case DIALDETECTOR_IDLE:
+ if (event->type == TAPI_EVENT_TYPE_HOOK && event->hook.on == false) {
+ d->state = DIALDETECTOR_WAIT_FOR_NUMBER;
+ event_register(d->timer_fd, EPOLLIN, &d->timeout_cb);
+ timerfd_settime(d->timer_fd, 0, &dialdetector_timeout, NULL);
+ }
+ break;
+ case DIALDETECTOR_WAIT_FOR_NUMBER:
+ case DIALDETECTOR_WAIT_FOR_NUMBER_TIMEOUT:
+ switch (event->type) {
+ case TAPI_EVENT_TYPE_HOOK:
+ if (event->hook.on == true)
+ dialdetector_reset(d);
+ break;
+ case TAPI_EVENT_TYPE_DTMF:
+ if (d->state == DIALDETECTOR_WAIT_FOR_NUMBER)
+ event_register(d->timer_fd, EPOLLIN, &d->timeout_cb);
+ timerfd_settime(d->timer_fd, 0, &dialdetector_timeout, NULL);
+ d->state = DIALDETECTOR_WAIT_FOR_NUMBER_TIMEOUT;
+ dialdetector_note_digit(d, event->dtmf.code);
+ break;
+ default:
+ break;
+ }
+ }
+}
+
+struct dialdetector *dialdetector_alloc(struct tapi_port *port)
+{
+ struct dialdetector *dialdetector;
+ dialdetector = malloc(sizeof(*dialdetector));
+
+ dialdetector->timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
+ dialdetector->port = port;
+ dialdetector->num_digits = 0;
+ dialdetector->state = DIALDETECTOR_IDLE;
+
+ dialdetector->timeout_cb.callback = dialdetector_timeout_event;
+ dialdetector->timeout_cb.data = dialdetector;
+
+ dialdetector->port_listener.callback = dialdetector_port_event;
+ dialdetector->port_listener.data = dialdetector;
+
+ tapi_port_register_event(port, &dialdetector->port_listener);
+
+
+ return dialdetector;
+}
diff --git a/package/libtapi/src/dialdetector.h b/package/libtapi/src/dialdetector.h
new file mode 100644
index 000000000..ff91b37aa
--- /dev/null
+++ b/package/libtapi/src/dialdetector.h
@@ -0,0 +1,109 @@
+#include <linux/input.h>
+#include <sys/epoll.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "events.h"
+#include "timerfd.h"
+
+#include "tapi-port.h"
+#include "dialdetector.h"
+
+static const struct itimerspec dialdetector_timeout = {
+ .it_value.tv_sec = 3,
+};
+
+static void dialdetector_note_digit(struct dialdetector *d, unsigned char digit)
+{
+ printf("note digit: %d\n", d->num_digits);
+ d->digits[d->num_digits] = digit;
+ ++d->num_digits;
+}
+
+static void dialdetector_reset(struct dialdetector *d)
+{
+ event_unregister(d->timer_fd);
+ d->num_digits = 0;
+ d->state = DIALDETECTOR_IDLE;
+}
+
+static bool dialdetector_timeout_event(int events, void *data)
+{
+ char num[20];
+ struct dialdetector *dialdetector = data;
+ int i;
+
+ for (i = 0; i < dialdetector->num_digits; ++i) {
+ num[i] = '0' + dialdetector->digits[i];
+ }
+ num[i] = '\0';
+
+ printf("Dialing: %s\n", num);
+ dialdetector->dial_callback(dialdetector->port, dialdetector->num_digits,
+ dialdetector->digits);
+
+ dialdetector_reset(dialdetector);
+
+ return false;
+}
+
+static void dialdetector_port_event(struct tapi_port *port,
+ struct tapi_event *event, void *data)
+{
+ struct dialdetector *d = data;
+
+ printf("port event: %d %d\n", d->state, event->hook.on);
+
+ switch (d->state) {
+ case DIALDETECTOR_IDLE:
+ if (event->type == TAPI_EVENT_TYPE_HOOK && event->hook.on == false) {
+ d->state = DIALDETECTOR_WAIT_FOR_NUMBER;
+ event_register(d->timer_fd, EPOLLIN, &d->timeout_cb);
+ timerfd_settime(d->timer_fd, 0, &dialdetector_timeout, NULL);
+ }
+ break;
+ case DIALDETECTOR_WAIT_FOR_NUMBER:
+ case DIALDETECTOR_WAIT_FOR_NUMBER_TIMEOUT:
+ switch (event->type) {
+ case TAPI_EVENT_TYPE_HOOK:
+ if (event->hook.on == true)
+ dialdetector_reset(d);
+ break;
+ case TAPI_EVENT_TYPE_DTMF:
+ if (d->state == DIALDETECTOR_WAIT_FOR_NUMBER)
+ event_register(d->timer_fd, EPOLLIN, &d->timeout_cb);
+ timerfd_settime(d->timer_fd, 0, &dialdetector_timeout, NULL);
+ d->state = DIALDETECTOR_WAIT_FOR_NUMBER_TIMEOUT;
+ dialdetector_note_digit(d, event->dtmf.code);
+ break;
+ default:
+ break;
+ }
+ }
+}
+
+struct dialdetector *dialdetector_alloc(struct tapi_port *port)
+{
+ struct dialdetector *dialdetector;
+ dialdetector = malloc(sizeof(*dialdetector));
+
+ dialdetector->timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
+ dialdetector->port = port;
+ dialdetector->num_digits = 0;
+ dialdetector->state = DIALDETECTOR_IDLE;
+
+ dialdetector->timeout_cb.callback = dialdetector_timeout_event;
+ dialdetector->timeout_cb.data = dialdetector;
+
+ dialdetector->port_listener.callback = dialdetector_port_event;
+ dialdetector->port_listener.data = dialdetector;
+
+ tapi_port_register_event(port, &dialdetector->port_listener);
+
+
+ return dialdetector;
+}
diff --git a/package/libtapi/src/events.c b/package/libtapi/src/events.c
new file mode 100644
index 000000000..36e9b62a0
--- /dev/null
+++ b/package/libtapi/src/events.c
@@ -0,0 +1,58 @@
+#include <stdbool.h>
+#include <sys/epoll.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#include <stdio.h>
+
+#include "events.h"
+
+struct event_callback *event_callbacks;
+
+static int event_epoll_fd = -1;
+
+int event_register(int fd, int events, struct event_callback *cb)
+{
+ struct epoll_event ev;
+
+ if (event_epoll_fd == -1)
+ event_epoll_fd = epoll_create(1);
+
+ ev.events = events;
+ ev.data.ptr = cb;
+
+ cb->fd = fd;
+
+ return epoll_ctl(event_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
+}
+
+int event_unregister(int fd)
+{
+ return epoll_ctl(event_epoll_fd, EPOLL_CTL_DEL, fd, NULL);
+}
+
+int tapi_mainloop(void)
+{
+ struct epoll_event ev[10];
+ struct event_callback *cb;
+ int ret;
+ bool keep;
+ int i;
+
+ if (event_epoll_fd == -1)
+ event_epoll_fd = epoll_create(1);
+
+ while(true) {
+ ret = epoll_wait(event_epoll_fd, ev, 10, -1);
+ for(i = 0; i < ret; ++i) {
+ cb = ev[i].data.ptr;
+ keep = cb->callback(ev[i].events, cb->data);
+ if (!keep)
+ event_unregister(cb->fd);
+ }
+ if (ret < 0)
+ printf("epoll: %d\n", errno);
+ }
+
+ return 0;
+}
diff --git a/package/libtapi/src/events.h b/package/libtapi/src/events.h
new file mode 100644
index 000000000..9912fac3a
--- /dev/null
+++ b/package/libtapi/src/events.h
@@ -0,0 +1,18 @@
+#ifndef __EVENTS_H__
+#define __EVENTS_H__
+
+struct event_callback {
+ bool (*callback)(int events, void *data);
+ void *data;
+ int fd;
+};
+
+int event_register(int fd, int events,
+ struct event_callback *callback);
+
+int event_unregister(int fd);
+
+
+int tapi_mainloop(void);
+
+#endif
diff --git a/package/libtapi/src/list.h b/package/libtapi/src/list.h
new file mode 100644
index 000000000..2959a061d
--- /dev/null
+++ b/package/libtapi/src/list.h
@@ -0,0 +1,601 @@
+#ifndef _LINUX_LIST_H
+#define _LINUX_LIST_H
+
+#include <stddef.h>
+/**
+ * container_of - cast a member of a structure out to the containing structure
+ * @ptr: the pointer to the member.
+ * @type: the type of the container struct this is embedded in.
+ * @member: the name of the member within the struct.
+ *
+ */
+#ifndef container_of
+#define container_of(ptr, type, member) ( \
+ (type *)( (char *)ptr - offsetof(type,member) ))
+#endif
+
+
+/*
+ * Simple doubly linked list implementation.
+ *
+ * Some of the internal functions ("__xxx") are useful when
+ * manipulating whole lists rather than single entries, as
+ * sometimes we already know the next/prev entries and we can
+ * generate better code by using them directly rather than
+ * using the generic single-entry routines.
+ */
+
+struct list_head {
+ struct list_head *next, *prev;
+};
+
+#define LIST_HEAD_INIT(name) { &(name), &(name) }
+
+#define LIST_HEAD(name) \
+ struct list_head name = LIST_HEAD_INIT(name)
+
+static inline void INIT_LIST_HEAD(struct list_head *list)
+{
+ list->next = list;
+ list->prev = list;
+}
+
+/*
+ * Insert a new entry between two known consecutive entries.
+ *
+ * This is only for internal list manipulation where we know
+ * the prev/next entries already!
+ */
+static inline void __list_add(struct list_head *new,
+ struct list_head *prev,
+ struct list_head *next)
+{
+ next->prev = new;
+ new->next = next;
+ new->prev = prev;
+ prev->next = new;
+}
+
+/**
+ * list_add - add a new entry
+ * @new: new entry to be added
+ * @head: list head to add it after
+ *
+ * Insert a new entry after the specified head.
+ * This is good for implementing stacks.
+ */
+static inline void list_add(struct list_head *new, struct list_head *head)
+{
+ __list_add(new, head, head->next);
+}
+
+
+/**
+ * list_add_tail - add a new entry
+ * @new: new entry to be added
+ * @head: list head to add it before
+ *
+ * Insert a new entry before the specified head.
+ * This is useful for implementing queues.
+ */
+static inline void list_add_tail(struct list_head *new, struct list_head *head)
+{
+ __list_add(new, head->prev, head);
+}
+
+
+/*
+ * Delete a list entry by making the prev/next entries
+ * point to each other.
+ *
+ * This is only for internal list manipulation where we know
+ * the prev/next entries already!
+ */
+static inline void __list_del(struct list_head * prev, struct list_head * next)
+{
+ next->prev = prev;
+ prev->next = next;
+}
+
+/**
+ * list_del - deletes entry from list.
+ * @entry: the element to delete from the list.
+ * Note: list_empty() on entry does not return true after this, the entry is
+ * in an undefined state.
+ */
+static inline void list_del(struct list_head *entry)
+{
+ __list_del(entry->prev, entry->next);
+ entry->next = NULL;
+ entry->prev = NULL;
+}
+
+/**
+ * list_replace - replace old entry by new one
+ * @old : the element to be replaced
+ * @new : the new element to insert
+ *
+ * If @old was empty, it will be overwritten.
+ */
+static inline void list_replace(struct list_head *old,
+ struct list_head *new)
+{
+ new->next = old->next;
+ new->next->prev = new;
+ new->prev = old->prev;
+ new->prev->next = new;
+}
+
+static inline void list_replace_init(struct list_head *old,
+ struct list_head *new)
+{
+ list_replace(old, new);
+ INIT_LIST_HEAD(old);
+}
+
+/**
+ * list_del_init - deletes entry from list and reinitialize it.
+ * @entry: the element to delete from the list.
+ */
+static inline void list_del_init(struct list_head *entry)
+{
+ __list_del(entry->prev, entry->next);
+ INIT_LIST_HEAD(entry);
+}
+
+/**
+ * list_move - delete from one list and add as another's head
+ * @list: the entry to move
+ * @head: the head that will precede our entry
+ */
+static inline void list_move(struct list_head *list, struct list_head *head)
+{
+ __list_del(list->prev, list->next);
+ list_add(list, head);
+}
+
+/**
+ * list_move_tail - delete from one list and add as another's tail
+ * @list: the entry to move
+ * @head: the head that will follow our entry
+ */
+static inline void list_move_tail(struct list_head *list,
+ struct list_head *head)
+{
+ __list_del(list->prev, list->next);
+ list_add_tail(list, head);
+}
+
+/**
+ * list_is_last - tests whether @list is the last entry in list @head
+ * @list: the entry to test
+ * @head: the head of the list
+ */
+static inline int list_is_last(const struct list_head *list,
+ const struct list_head *head)
+{
+ return list->next == head;
+}
+
+/**
+ * list_empty - tests whether a list is empty
+ * @head: the list to test.
+ */
+static inline int list_empty(const struct list_head *head)
+{
+ return head->next == head;
+}
+
+/**
+ * list_empty_careful - tests whether a list is empty and not being modified
+ * @head: the list to test
+ *
+ * Description:
+ * tests whether a list is empty _and_ checks that no other CPU might be
+ * in the process of modifying either member (next or prev)
+ *
+ * NOTE: using list_empty_careful() without synchronization
+ * can only be safe if the only activity that can happen
+ * to the list entry is list_del_init(). Eg. it cannot be used
+ * if another CPU could re-list_add() it.
+ */
+static inline int list_empty_careful(const struct list_head *head)
+{
+ struct list_head *next = head->next;
+ return (next == head) && (next == head->prev);
+}
+
+static inline void __list_splice(struct list_head *list,
+ struct list_head *head)
+{
+ struct list_head *first = list->next;
+ struct list_head *last = list->prev;
+ struct list_head *at = head->next;
+
+ first->prev = head;
+ head->next = first;
+
+ last->next = at;
+ at->prev = last;
+}
+
+/**
+ * list_splice - join two lists
+ * @list: the new list to add.
+ * @head: the place to add it in the first list.
+ */
+static inline void list_splice(struct list_head *list, struct list_head *head)
+{
+ if (!list_empty(list))
+ __list_splice(list, head);
+}
+
+/**
+ * list_splice_init - join two lists and reinitialise the emptied list.
+ * @list: the new list to add.
+ * @head: the place to add it in the first list.
+ *
+ * The list at @list is reinitialised
+ */
+static inline void list_splice_init(struct list_head *list,
+ struct list_head *head)
+{
+ if (!list_empty(list)) {
+ __list_splice(list, head);
+ INIT_LIST_HEAD(list);
+ }
+}
+
+/**
+ * list_entry - get the struct for this entry
+ * @ptr: the &struct list_head pointer.
+ * @type: the type of the struct this is embedded in.
+ * @member: the name of the list_struct within the struct.
+ */
+#define list_entry(ptr, type, member) \
+ container_of(ptr, type, member)
+
+/**
+ * list_first_entry - get the first element from a list
+ * @ptr: the list head to take the element from.
+ * @type: the type of the struct this is embedded in.
+ * @member: the name of the list_struct within the struct.
+ *
+ * Note, that list is expected to be not empty.
+ */
+#define list_first_entry(ptr, type, member) \
+ list_entry((ptr)->next, type, member)
+
+/**
+ * list_for_each - iterate over a list
+ * @pos: the &struct list_head to use as a loop cursor.
+ * @head: the head for your list.
+ */
+#define list_for_each(pos, head) \
+ for (pos = (head)->next; pos != (head); \
+ pos = pos->next)
+
+/**
+ * __list_for_each - iterate over a list
+ * @pos: the &struct list_head to use as a loop cursor.
+ * @head: the head for your list.
+ *
+ * This variant differs from list_for_each() in that it's the
+ * simplest possible list iteration code, no prefetching is done.
+ * Use this for code that knows the list to be very short (empty
+ * or 1 entry) most of the time.
+ */
+#define __list_for_each(pos, head) \
+ for (pos = (head)->next; pos != (head); pos = pos->next)
+
+/**
+ * list_for_each_prev - iterate over a list backwards
+ * @pos: the &struct list_head to use as a loop cursor.
+ * @head: the head for your list.
+ */
+#define list_for_each_prev(pos, head) \
+ for (pos = (head)->prev; pos != (head); \
+ pos = pos->prev)
+
+/**
+ * list_for_each_safe - iterate over a list safe against removal of list entry
+ * @pos: the &struct list_head to use as a loop cursor.
+ * @n: another &struct list_head to use as temporary storage
+ * @head: the head for your list.
+ */
+#define list_for_each_safe(pos, n, head) \
+ for (pos = (head)->next, n = pos->next; pos != (head); \
+ pos = n, n = pos->next)
+
+/**
+ * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
+ * @pos: the &struct list_head to use as a loop cursor.
+ * @n: another &struct list_head to use as temporary storage
+ * @head: the head for your list.
+ */
+#define list_for_each_prev_safe(pos, n, head) \
+ for (pos = (head)->prev, n = pos->prev; \
+ pos != (head); \
+ pos = n, n = pos->prev)
+
+/**
+ * list_for_each_entry - iterate over list of given type
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ */
+#define list_for_each_entry(pos, head, member) \
+ for (pos = list_entry((head)->next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = list_entry(pos->member.next, typeof(*pos), member))
+
+/**
+ * list_for_each_entry_reverse - iterate backwards over list of given type.
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ */
+#define list_for_each_entry_reverse(pos, head, member) \
+ for (pos = list_entry((head)->prev, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = list_entry(pos->member.prev, typeof(*pos), member))
+
+/**
+ * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
+ * @pos: the type * to use as a start point
+ * @head: the head of the list
+ * @member: the name of the list_struct within the struct.
+ *
+ * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
+ */
+#define list_prepare_entry(pos, head, member) \
+ ((pos) ? : list_entry(head, typeof(*pos), member))
+
+/**
+ * list_for_each_entry_continue - continue iteration over list of given type
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ *
+ * Continue to iterate over list of given type, continuing after
+ * the current position.
+ */
+#define list_for_each_entry_continue(pos, head, member) \
+ for (pos = list_entry(pos->member.next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = list_entry(pos->member.next, typeof(*pos), member))
+
+/**
+ * list_for_each_entry_continue_reverse - iterate backwards from the given point
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ *
+ * Start to iterate over list of given type backwards, continuing after
+ * the current position.
+ */
+#define list_for_each_entry_continue_reverse(pos, head, member) \
+ for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = list_entry(pos->member.prev, typeof(*pos), member))
+
+/**
+ * list_for_each_entry_from - iterate over list of given type from the current point
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ *
+ * Iterate over list of given type, continuing from current position.
+ */
+#define list_for_each_entry_from(pos, head, member) \
+ for (; &pos->member != (head); \
+ pos = list_entry(pos->member.next, typeof(*pos), member))
+
+/**
+ * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
+ * @pos: the type * to use as a loop cursor.
+ * @n: another type * to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ */
+#define list_for_each_entry_safe(pos, n, head, member) \
+ for (pos = list_entry((head)->next, typeof(*pos), member), \
+ n = list_entry(pos->member.next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = n, n = list_entry(n->member.next, typeof(*n), member))
+
+/**
+ * list_for_each_entry_safe_continue
+ * @pos: the type * to use as a loop cursor.
+ * @n: another type * to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ *
+ * Iterate over list of given type, continuing after current point,
+ * safe against removal of list entry.
+ */
+#define list_for_each_entry_safe_continue(pos, n, head, member) \
+ for (pos = list_entry(pos->member.next, typeof(*pos), member), \
+ n = list_entry(pos->member.next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = n, n = list_entry(n->member.next, typeof(*n), member))
+
+/**
+ * list_for_each_entry_safe_from
+ * @pos: the type * to use as a loop cursor.
+ * @n: another type * to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ *
+ * Iterate over list of given type from current point, safe against
+ * removal of list entry.
+ */
+#define list_for_each_entry_safe_from(pos, n, head, member) \
+ for (n = list_entry(pos->member.next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = n, n = list_entry(n->member.next, typeof(*n), member))
+
+/**
+ * list_for_each_entry_safe_reverse
+ * @pos: the type * to use as a loop cursor.
+ * @n: another type * to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ *
+ * Iterate backwards over list of given type, safe against removal
+ * of list entry.
+ */
+#define list_for_each_entry_safe_reverse(pos, n, head, member) \
+ for (pos = list_entry((head)->prev, typeof(*pos), member), \
+ n = list_entry(pos->member.prev, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = n, n = list_entry(n->member.prev, typeof(*n), member))
+
+/*
+ * Double linked lists with a single pointer list head.
+ * Mostly useful for hash tables where the two pointer list head is
+ * too wasteful.
+ * You lose the ability to access the tail in O(1).
+ */
+
+struct hlist_head {
+ struct hlist_node *first;
+};
+
+struct hlist_node {
+ struct hlist_node *next, **pprev;
+};
+
+#define HLIST_HEAD_INIT { .first = NULL }
+#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
+#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
+static inline void INIT_HLIST_NODE(struct hlist_node *h)
+{
+ h->next = NULL;
+ h->pprev = NULL;
+}
+
+static inline int hlist_unhashed(const struct hlist_node *h)
+{
+ return !h->pprev;
+}
+
+static inline int hlist_empty(const struct hlist_head *h)
+{
+ return !h->first;
+}
+
+static inline void __hlist_del(struct hlist_node *n)
+{
+ struct hlist_node *next = n->next;
+ struct hlist_node **pprev = n->pprev;
+ *pprev = next;
+ if (next)
+ next->pprev = pprev;
+}
+
+static inline void hlist_del(struct hlist_node *n)
+{
+ __hlist_del(n);
+ n->next = NULL;
+ n->pprev = NULL;
+}
+
+static inline void hlist_del_init(struct hlist_node *n)
+{
+ if (!hlist_unhashed(n)) {
+ __hlist_del(n);
+ INIT_HLIST_NODE(n);
+ }
+}
+
+
+static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
+{
+ struct hlist_node *first = h->first;
+ n->next = first;
+ if (first)
+ first->pprev = &n->next;
+ h->first = n;
+ n->pprev = &h->first;
+}
+
+
+/* next must be != NULL */
+static inline void hlist_add_before(struct hlist_node *n,
+ struct hlist_node *next)
+{
+ n->pprev = next->pprev;
+ n->next = next;
+ next->pprev = &n->next;
+ *(n->pprev) = n;
+}
+
+static inline void hlist_add_after(struct hlist_node *n,
+ struct hlist_node *next)
+{
+ next->next = n->next;
+ n->next = next;
+ next->pprev = &n->next;
+
+ if(next->next)
+ next->next->pprev = &next->next;
+}
+
+#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
+
+#define hlist_for_each(pos, head) \
+ for (pos = (head)->first; pos; pos = pos->next)
+
+#define hlist_for_each_safe(pos, n, head) \
+ for (pos = (head)->first; pos; pos = n)
+
+/**
+ * hlist_for_each_entry - iterate over list of given type
+ * @tpos: the type * to use as a loop cursor.
+ * @pos: the &struct hlist_node to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the hlist_node within the struct.
+ */
+#define hlist_for_each_entry(tpos, pos, head, member) \
+ for (pos = (head)->first; pos && \
+ ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
+ pos = pos->next)
+
+/**
+ * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
+ * @tpos: the type * to use as a loop cursor.
+ * @pos: the &struct hlist_node to use as a loop cursor.
+ * @member: the name of the hlist_node within the struct.
+ */
+#define hlist_for_each_entry_continue(tpos, pos, member) \
+ for (pos = (pos)->next; pos && \
+ ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
+ pos = pos->next)
+
+/**
+ * hlist_for_each_entry_from - iterate over a hlist continuing from current point
+ * @tpos: the type * to use as a loop cursor.
+ * @pos: the &struct hlist_node to use as a loop cursor.
+ * @member: the name of the hlist_node within the struct.
+ */
+#define hlist_for_each_entry_from(tpos, pos, member) \
+ for (; pos && \
+ ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
+ pos = pos->next)
+
+/**
+ * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
+ * @tpos: the type * to use as a loop cursor.
+ * @pos: the &struct hlist_node to use as a loop cursor.
+ * @n: another &struct hlist_node to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the hlist_node within the struct.
+ */
+#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
+ for (pos = (head)->first; \
+ pos && ({ n = pos->next; 1; }) && \
+ ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
+ pos = n)
+
+#endif
diff --git a/package/libtapi/src/tapi-device.c b/package/libtapi/src/tapi-device.c
new file mode 100644
index 000000000..66e4260a0
--- /dev/null
+++ b/package/libtapi/src/tapi-device.c
@@ -0,0 +1,46 @@
+#include <stdio.h>
+
+#include "tapi-device.h"
+#include "tapi-ioctl.h"
+
+int tapi_device_open(unsigned int id, struct tapi_device *dev)
+{
+ char path[100];
+
+ snprintf(path, sizeof(path), "/dev/tapi%dC", id);
+ dev->control_fd = open(path, 0);
+ if (dev->control_fd < 0)
+ return -1;
+
+ snprintf(dev->stream_path, 100, "/dev/tapi%dS", id);
+
+ dev->id = id;
+ dev->num_ports = 2;
+
+ return 0;
+}
+
+int tapi_link_alloc(struct tapi_device *dev, unsigned int ep1, unsigned int ep2)
+{
+ return ioctl(dev->control_fd, TAPI_CONTROL_IOCTL_LINK_ALLOC, (ep1 << 16) | ep2);
+}
+
+int tapi_link_free(struct tapi_device *dev, unsigned int link)
+{
+ return ioctl(dev->control_fd, TAPI_CONTROL_IOCTL_LINK_FREE, link);
+}
+
+int tapi_link_enable(struct tapi_device *dev, unsigned int link)
+{
+ return ioctl(dev->control_fd, TAPI_CONTROL_IOCTL_LINK_ENABLE, link);
+}
+
+int tapi_link_disable(struct tapi_device *dev, unsigned int link)
+{
+ return ioctl(dev->control_fd, TAPI_CONTROL_IOCTL_LINK_DISABLE, link);
+}
+
+int tapi_sync(struct tapi_device *dev)
+{
+ return ioctl(dev->control_fd, TAPI_CONTROL_IOCTL_SYNC, 0);
+}
diff --git a/package/libtapi/src/tapi-device.h b/package/libtapi/src/tapi-device.h
new file mode 100644
index 000000000..27658b9d8
--- /dev/null
+++ b/package/libtapi/src/tapi-device.h
@@ -0,0 +1,25 @@
+#ifndef __TAPI_DEVICE_H__
+#define __TAPI_DEVICE_H__
+
+struct tapi_device {
+ int control_fd;
+ int stream_fd;
+ struct tapi_port *ports;
+ char stream_path[100];
+
+ unsigned int id;
+
+ unsigned int num_ports;
+};
+
+struct tapi_endpoint;
+
+int tapi_device_open(unsigned int id, struct tapi_device *dev);
+
+int tapi_link_alloc(struct tapi_device *dev, unsigned int ep1, unsigned int ep2);
+int tapi_link_free(struct tapi_device *dev, unsigned int link);
+int tapi_link_enable(struct tapi_device *dev, unsigned int link);
+int tapi_link_disable(struct tapi_device *dev, unsigned int link);
+int tapi_sync(struct tapi_device *dev);
+
+#endif
diff --git a/package/libtapi/src/tapi-ioctl.h b/package/libtapi/src/tapi-ioctl.h
new file mode 100644
index 000000000..c43fac1e7
--- /dev/null
+++ b/package/libtapi/src/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/libtapi/src/tapi-port.c b/package/libtapi/src/tapi-port.c
new file mode 100644
index 000000000..d06d946e1
--- /dev/null
+++ b/package/libtapi/src/tapi-port.c
@@ -0,0 +1,126 @@
+#include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/epoll.h>
+#include <unistd.h>
+
+#include <linux/input.h>
+
+#include "tapi-ioctl.h"
+#include "tapi-device.h"
+#include "tapi-port.h"
+
+#include "events.h"
+#include "list.h"
+
+static void tapi_port_event_dispatch(struct tapi_port *port,
+ struct tapi_event *event)
+{
+ struct tapi_port_event_listener *l;
+
+ list_for_each_entry(l, &port->event_listeners, head) {
+ l->callback(port, event, l->data);
+ }
+}
+
+static bool tapi_port_input_event(int events, void *data)
+{
+ struct tapi_port *port = data;
+ struct input_event event;
+ struct tapi_event tapi_event;
+ int ret;
+
+ ret = read(port->input_fd, &event, sizeof(event));
+ if (ret < 0) {
+ fprintf(stderr, "Port %d failed to read from input device: %d\n",
+ port->id, errno);
+ return true;
+ }
+
+ if (!event.value)
+ return true;
+
+ switch (event.code) {
+ case KEY_NUMERIC_0 ... KEY_NUMERIC_9:
+ tapi_event.type = TAPI_EVENT_TYPE_DTMF;
+ tapi_event.dtmf.code = event.code - KEY_NUMERIC_0;
+ break;
+ case KEY_NUMERIC_POUND:
+ tapi_event.type = TAPI_EVENT_TYPE_DTMF;
+ tapi_event.dtmf.code = 10;
+ break;
+ case KEY_NUMERIC_STAR:
+ tapi_event.type = TAPI_EVENT_TYPE_DTMF;
+ tapi_event.dtmf.code = 11;
+ break;
+ case KEY_ESC:
+ tapi_event.type = TAPI_EVENT_TYPE_HOOK;
+ tapi_event.hook.on = true;
+ break;
+ case KEY_ENTER:
+ tapi_event.type = TAPI_EVENT_TYPE_HOOK;
+ tapi_event.hook.on = false;
+ break;
+ default:
+ return true;
+ }
+
+ if (tapi_event.type == TAPI_EVENT_TYPE_DTMF)
+ tapi_event.dtmf.time = event.time;
+
+ tapi_port_event_dispatch(port, &tapi_event);
+
+ return true;
+}
+
+int tapi_port_open(struct tapi_device *dev, unsigned int id, struct tapi_port *port)
+{
+ int ret;
+ char path[100];
+
+ port->id = id;
+
+ snprintf(path, 100, "/dev/tapi%uP%u", dev->id, id);
+ port->fd = open(path, 0);
+ if (port->fd < 0) {
+ printf("Failed to open %s: %d\n", path, errno);
+ return errno;
+ }
+
+ snprintf(path, 100, "/dev/event%u", id);
+ port->input_fd = open(path, O_RDONLY);
+ if (port->input_fd < 0) {
+ printf("Failed to open %s: %d\n", path, errno);
+ return errno;
+ }
+
+ port->ep = ioctl(port->fd, TAPI_PORT_IOCTL_GET_ENDPOINT, 0);
+
+ INIT_LIST_HEAD(&port->event_listeners);
+
+ port->input_cb.callback = tapi_port_input_event;
+ port->input_cb.data = port;
+
+ return event_register(port->input_fd, EPOLLIN,
+ &port->input_cb);
+}
+
+int tapi_port_set_ring(struct tapi_port *port, bool ring)
+{
+ return ioctl(port->fd, TAPI_PORT_IOCTL_SET_RING, ring);
+}
+
+int tapi_port_register_event(struct tapi_port *port,
+ struct tapi_port_event_listener *cb)
+{
+ list_add_tail(&cb->head, &port->event_listeners);
+ return 0;
+}
+
+void tapi_port_unregister_event(struct tapi_port *port,
+ struct tapi_port_event_listener *cb)
+{
+ list_del(&cb->head);
+}
diff --git a/package/libtapi/src/tapi-port.h b/package/libtapi/src/tapi-port.h
new file mode 100644
index 000000000..a290a4002
--- /dev/null
+++ b/package/libtapi/src/tapi-port.h
@@ -0,0 +1,66 @@
+#ifndef __TAPI_PORT_H__
+#define __TAPI_PORT_H__
+
+#include <sys/time.h>
+
+#include "list.h"
+#include "events.h"
+
+struct tapi_port;
+struct tapi_device;
+
+struct tapi_dtmf_event {
+ struct timeval time;
+ unsigned char code;
+};
+
+struct tapi_hook_event {
+ bool on;
+};
+
+enum tapi_event_type {
+ TAPI_EVENT_TYPE_DTMF,
+ TAPI_EVENT_TYPE_HOOK,
+};
+
+struct tapi_event {
+ enum tapi_event_type type;
+ union {
+ struct tapi_dtmf_event dtmf;
+ struct tapi_hook_event hook;
+ };
+};
+
+struct tapi_port_event_listener {
+ void (*callback)(struct tapi_port *, struct tapi_event *event, void *data);
+ void *data;
+
+ struct list_head head;
+};
+
+struct tapi_port {
+ int id;
+ int fd;
+ int input_fd;
+ unsigned int ep;
+
+ struct event_callback input_cb;
+
+ struct list_head event_listeners;
+};
+
+int tapi_port_open(struct tapi_device *dev, unsigned int id, struct tapi_port
+*port);
+int tapi_port_set_ring(struct tapi_port *port, bool ring);
+int tapi_port_register_event(struct tapi_port *port,
+ struct tapi_port_event_listener *cb);
+
+void tapi_port_unregister_event(struct tapi_port *port,
+ struct tapi_port_event_listener *cb);
+
+static inline int tapi_port_get_endpoint(struct tapi_port *port)
+{
+ return port->ep;
+}
+
+#endif
diff --git a/package/libtapi/src/tapi-session.c b/package/libtapi/src/tapi-session.c
new file mode 100644
index 000000000..8b1705311
--- /dev/null
+++ b/package/libtapi/src/tapi-session.c
@@ -0,0 +1,122 @@
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "tapi-device.h"
+#include "tapi-port.h"
+#include "tapi-session.h"
+
+enum tapi_session_port_state {
+ TAPI_SESSION_PORT_STATE_IDLE,
+ TAPI_SESSION_PORT_STATE_RINGING,
+ TAPI_SESSION_PORT_STATE_ACTIVE,
+};
+
+struct tapi_session_port {
+ struct tapi_port *port;
+ struct tapi_port_event_listener event_listener;
+
+ enum tapi_session_port_state state;
+};
+
+struct tapi_session {
+ struct tapi_device *dev;
+ struct tapi_session_port caller;
+ struct tapi_session_port callee;
+
+ bool active;
+ unsigned int link;
+
+ void (*release)(struct tapi_session *session, void *data);
+};
+
+static void tapi_session_terminate(struct tapi_session *session)
+{
+ if (session->active) {
+ tapi_link_enable(session->dev, session->link);
+ tapi_sync(session->dev);
+ tapi_link_free(session->dev, session->link);
+ }
+
+ switch (session->callee.state) {
+ case TAPI_SESSION_PORT_STATE_RINGING:
+ tapi_port_set_ring(session->callee.port, false);
+ break;
+ default:
+ break;
+ }
+
+ session->active = false;
+}
+
+static void tapi_session_caller_event(struct tapi_port *port,
+ struct tapi_event *event, void *data)
+{
+ struct tapi_session *session = data;
+
+ if (event->type != TAPI_EVENT_TYPE_HOOK)
+ return;
+
+ if (event->hook.on) {
+ tapi_session_terminate(session);
+ }
+}
+
+static void tapi_session_callee_event(struct tapi_port *port,
+ struct tapi_event *event, void *data)
+{
+ struct tapi_session *session = data;
+
+ if (event->type != TAPI_EVENT_TYPE_HOOK)
+ return;
+
+ if (event->hook.on) {
+ if (session->callee.state == TAPI_SESSION_PORT_STATE_ACTIVE) {
+ tapi_session_terminate(session);
+ }
+ } else {
+ if (session->callee.state == TAPI_SESSION_PORT_STATE_RINGING) {
+ tapi_port_set_ring(session->callee.port, false);
+ session->link = tapi_link_alloc(session->dev,
+ session->caller.port->ep, session->callee.port->ep);
+ session->callee.state = TAPI_SESSION_PORT_STATE_ACTIVE;
+ tapi_link_enable(session->dev, session->link);
+ tapi_sync(session->dev);
+ session->active = true;
+ }
+ }
+}
+
+struct tapi_session *tapi_session_alloc(struct tapi_device *dev,
+ struct tapi_port *caller, struct tapi_port *callee,
+ void (*release)(struct tapi_session *session, void *data), void *release_data)
+{
+ struct tapi_session *session;
+ struct tapi_session_port *session_port;
+
+ session = malloc(sizeof(*session));
+
+ session->dev = dev;
+
+ session->callee.port = callee;
+ session->callee.state = TAPI_SESSION_PORT_STATE_RINGING;
+ session->callee.event_listener.callback = tapi_session_callee_event;
+ session->callee.event_listener.data = session;
+ tapi_port_register_event(callee, &session->callee.event_listener);
+
+ session->caller.port = caller;
+ session->caller.state = TAPI_SESSION_PORT_STATE_ACTIVE;
+ session->caller.event_listener.callback = tapi_session_caller_event;
+ session->caller.event_listener.data = session;
+ tapi_port_register_event(caller, &session->caller.event_listener);
+
+ tapi_port_set_ring(callee, true);
+}
+
+void tapi_session_free(struct tapi_session *session)
+{
+ tapi_session_terminate(session);
+ tapi_port_register_event(session->callee.port, &session->callee.event_listener);
+ tapi_port_register_event(session->caller.port, &session->caller.event_listener);
+ free(session);
+}
diff --git a/package/libtapi/src/tapi-session.h b/package/libtapi/src/tapi-session.h
new file mode 100644
index 000000000..15d94735e
--- /dev/null
+++ b/package/libtapi/src/tapi-session.h
@@ -0,0 +1,14 @@
+#ifndef __TAPI_SESSION_H__
+#define __TAPI_SESSION_H__
+
+struct tapi_device;
+struct tapi_port;
+struct tapi_session;
+
+struct tapi_session *tapi_session_alloc(struct tapi_device *dev,
+ struct tapi_port *caller, struct tapi_port *callee,
+ void (*release)(struct tapi_session *session, void *data), void *release_data);
+
+void tapi_session_free(struct tapi_session *session);
+
+#endif
diff --git a/package/libtapi/src/tapi-stream.c b/package/libtapi/src/tapi-stream.c
new file mode 100644
index 000000000..5ae44a163
--- /dev/null
+++ b/package/libtapi/src/tapi-stream.c
@@ -0,0 +1,42 @@
+#include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "tapi-device.h"
+#include "tapi-stream.h"
+#include "tapi-ioctl.h"
+
+struct tapi_stream *tapi_stream_alloc(struct tapi_device *dev)
+{
+ struct tapi_stream *stream;
+
+ stream = malloc(sizeof(*stream));
+ if (!stream)
+ return NULL;
+
+ stream->fd = open(dev->stream_path, O_RDWR);
+
+ if (stream->fd < 0) {
+ free(stream);
+ return NULL;
+ }
+
+ stream->ep = ioctl(stream->fd, TAPI_STREAM_IOCTL_GET_ENDPOINT, 0);
+
+ if (stream->ep < 0) {
+ close(stream->fd);
+ free(stream);
+ return NULL;
+ }
+
+ return stream;
+}
+
+void tapi_stream_free(struct tapi_stream *stream)
+{
+ close(stream->fd);
+ free(stream);
+}
diff --git a/package/libtapi/src/tapi-stream.h b/package/libtapi/src/tapi-stream.h
new file mode 100644
index 000000000..0e3e04af2
--- /dev/null
+++ b/package/libtapi/src/tapi-stream.h
@@ -0,0 +1,19 @@
+#ifndef __TAPI_STREAM_H__
+#define __TAPI_STREAM_H__
+
+struct tapi_device;
+
+struct tapi_stream {
+ int fd;
+ int ep;
+};
+
+struct tapi_stream *tapi_stream_alloc(struct tapi_device *);
+void tapi_stream_free(struct tapi_stream *);
+
+static inline int tapi_stream_get_endpoint(struct tapi_stream *stream)
+{
+ return stream->ep;
+}
+
+#endif
diff --git a/package/libtapi/src/tapidemo.c b/package/libtapi/src/tapidemo.c
new file mode 100644
index 000000000..63098b89c
--- /dev/null
+++ b/package/libtapi/src/tapidemo.c
@@ -0,0 +1,56 @@
+
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <poll.h>
+
+#include <linux/input.h>
+/*#include <sys/timerfd.h>*/
+#include "timerfd.h"
+#include "dialdetector.h"
+
+#include "tapi-ioctl.h"
+
+#include "tapi-device.h"
+#include "tapi-port.h"
+
+static struct tapi_device dev;
+static struct tapi_port ports[2];
+
+void dial_callback(struct tapi_port *port, size_t num_digits, const unsigned char *digits)
+{
+ unsigned int link;
+
+ if (num_digits != 1)
+ return;
+ if (port->id == digits[0] || digits[0] > 1)
+ return;
+
+ tapi_port_set_ring(&ports[digits[0]], true);
+
+ tapi_session_alloc(&dev, port, &ports[digits[0]]);
+}
+
+int main(int argc, char *argv[])
+{
+ struct dialdetector *dd, *dd2;
+ unsigned int link;
+ unsigned char buf[1024];
+ int ret;
+
+ tapi_device_open(0, &dev);
+ tapi_port_open(0, 0, &ports[0]);
+ tapi_port_open(0, 1, &ports[1]);
+
+ dd = dialdetector_alloc(&ports[0]);
+ dd->dial_callback = dial_callback;
+ dd2 = dialdetector_alloc(&ports[1]);
+ dd2->dial_callback = dial_callback;
+
+ tapi_mainloop();
+
+ return 0;
+}
diff --git a/package/libtapi/src/timer_fd.c b/package/libtapi/src/timer_fd.c
new file mode 100644
index 000000000..fbc951a9f
--- /dev/null
+++ b/package/libtapi/src/timer_fd.c
@@ -0,0 +1,33 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * timerfd_create() / timerfd_settime() / timerfd_gettime() for uClibc
+ *
+ * Copyright (C) 2009 Stephan Raue <stephan@openelec.tv>
+ *
+ * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
+ */
+
+#include <sys/syscall.h>
+/*#include <sys/timerfd.h>*/
+#include "timerfd.h"
+
+/*
+ * timerfd_create()
+ */
+#ifdef __NR_timerfd_create
+int timerfd_create(int clockid, int flags)
+{
+ return syscall(__NR_timerfd_create, clockid, flags);
+}
+#endif
+
+/*
+ * timerfd_settime()
+ */
+#ifdef __NR_timerfd_settime
+int timerfd_settime(int ufd, int flags, const struct itimerspec *umtr, struct itimerspec *otmr)
+{
+ return syscall(__NR_timerfd_settime, ufd, flags, umtr, otmr);
+}
+#endif
+
diff --git a/package/libtapi/src/timerfd.h b/package/libtapi/src/timerfd.h
new file mode 100644
index 000000000..f17bc798a
--- /dev/null
+++ b/package/libtapi/src/timerfd.h
@@ -0,0 +1,61 @@
+/* Copyright (C) 2008 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#ifndef _SYS_TIMERFD_H
+#define _SYS_TIMERFD_H 1
+
+#include <time.h>
+
+
+/* Bits to be set in the FLAGS parameter of `timerfd_create'. */
+enum
+ {
+ TFD_CLOEXEC = 02000000,
+#define TFD_CLOEXEC TFD_CLOEXEC
+ TFD_NONBLOCK = 04000
+#define TFD_NONBLOCK TFD_NONBLOCK
+ };
+
+
+/* Bits to be set in the FLAGS parameter of `timerfd_settime'. */
+enum
+ {
+ TFD_TIMER_ABSTIME = 1 << 0
+#define TFD_TIMER_ABSTIME TFD_TIMER_ABSTIME
+ };
+
+
+__BEGIN_DECLS
+
+/* Return file descriptor for new interval timer source. */
+extern int timerfd_create (clockid_t __clock_id, int __flags) __THROW;
+
+/* Set next expiration time of interval timer source UFD to UTMR. If
+ FLAGS has the TFD_TIMER_ABSTIME flag set the timeout value is
+ absolute. Optionally return the old expiration time in OTMR. */
+extern int timerfd_settime (int __ufd, int __flags,
+ __const struct itimerspec *__utmr,
+ struct itimerspec *__otmr) __THROW;
+
+/* Return the next expiration time of UFD. */
+extern int timerfd_gettime (int __ufd, struct itimerspec *__otmr) __THROW;
+
+__END_DECLS
+
+#endif /* sys/timerfd.h */
+
diff --git a/package/lqtapi/Makefile b/package/lqtapi/Makefile
new file mode 100644
index 000000000..40d114c9d
--- /dev/null
+++ b/package/lqtapi/Makefile
@@ -0,0 +1,73 @@
+#
+# Copyright (C) 2006-2010 OpenWrt.org
+#
+# This is free software, licensed under the GNU General Public License v2.
+# See /LICENSE for more information.
+#
+
+include $(TOPDIR)/rules.mk
+include $(INCLUDE_DIR)/kernel.mk
+
+PKG_NAME:=lqtapi
+PKG_VERSION:=1
+PKG_RELEASE:=1
+
+FW_SOURCE:=danube_firmware.bin
+FW_URL:=http://www.arcor.de/hilfe/files/pdf/
+FW_FILE=arcor_A800_452CPW_FW_1.02.206(20081201).bin
+FW_MD5SUM:=19d9af4e369287a0f0abaed415cdac10
+
+include $(INCLUDE_DIR)/package.mk
+
+define Download/firmware
+ FILE:=$(FW_FILE)
+ URL:=$(FW_URL)
+ MD5SUM:=$(FW_MD5SUM)
+endef
+$(eval $(call Download,firmware))
+
+define KernelPackage/lqtapi
+ SUBMENU:=Voice over IP
+ TITLE:=Tapi drivers for Lantiq SoC
+ DEPENDS:=@TARGET_ifxmips_danube
+ FILES:=$(PKG_BUILD_DIR)/tapi/tapi.ko \
+ $(PKG_BUILD_DIR)/mps/mps.ko \
+ $(PKG_BUILD_DIR)/mps/vmmc.ko
+ AUTOLOAD:=$(call AutoLoad,80,tapi) \
+ $(call AutoLoad,81,mps)
+endef
+
+define Package/lqtapi-firmware-danube
+ SECTION:=sys
+ CATEGORY:=Kernel modules
+ SUBMENU:=Voice over IP
+ TITLE:=Danube firmware
+ DEPENDS:=+kmod-lqtapi
+endef
+
+include $(INCLUDE_DIR)/kernel-defaults.mk
+
+define KernelPackage/lqtapi/description
+ FOSS drivers for Lantiq SoC voip core
+endef
+
+define Build/Prepare
+ mkdir -p $(PKG_BUILD_DIR)
+ $(CP) src/* $(PKG_BUILD_DIR)/
+endef
+
+define Build/Compile
+ $(MAKE) $(KERNEL_MAKEOPTS) \
+ SUBDIRS="$(PKG_BUILD_DIR)" \
+ EXTRA_CFLAGS=-I$(PKG_BUILD_DIR)/include/ \
+ modules
+endef
+
+define Package/lqtapi-firmware-danube/install
+ $(TOPDIR)/target/linux/$(BOARD)/extract.sh $(DL_DIR) '$(FW_FILE)'
+ $(INSTALL_DIR) $(1)/lib/firmware/
+ $(INSTALL_DATA) $(DL_DIR)/voip.bin $(1)/lib/firmware/$(FW_SOURCE)
+endef
+
+$(eval $(call KernelPackage,lqtapi))
+$(eval $(call BuildPackage,lqtapi-firmware-danube))
diff --git a/package/lqtapi/src/Makefile b/package/lqtapi/src/Makefile
new file mode 100644
index 000000000..852accb6a
--- /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 000000000..925c71369
--- /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 000000000..c43fac1e7
--- /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 000000000..410655d4c
--- /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 000000000..201bccc28
--- /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 000000000..27f1c63eb
--- /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 000000000..9d88a4547
--- /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 000000000..8da0c00c7
--- /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 000000000..c343ace3f
--- /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 000000000..7fc0b7a99
--- /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 000000000..8d672fbf5
--- /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 000000000..e759e5698
--- /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 000000000..828376fb9
--- /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 000000000..4e59b6627
--- /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 000000000..e833062c7
--- /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 000000000..087a53bc7
--- /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 000000000..f5a87dcef
--- /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 000000000..99b0d1d3d
--- /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 000000000..42d11dbb6
--- /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 000000000..9f818bd83
--- /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 000000000..181f0c6b7
--- /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 000000000..b598d0dcd
--- /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 000000000..aa620a1e0
--- /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 000000000..1efceb0b2
--- /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 000000000..e8f047063
--- /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 000000000..83fea6083
--- /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 000000000..36f5eef80
--- /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 000000000..7e029734f
--- /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 000000000..ba7d435af
--- /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 000000000..f442a9ab4
--- /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 000000000..2992d4b3a
--- /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 000000000..a4f245e92
--- /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 000000000..2e112a376
--- /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 000000000..999aaefa9
--- /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 000000000..c066742b6
--- /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 000000000..0ef955567
--- /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 000000000..7277a5c61
--- /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 000000000..f6e5ee066
--- /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);
+}
diff --git a/package/pjsip/Makefile b/package/pjsip/Makefile
new file mode 100644
index 000000000..6216ebfc9
--- /dev/null
+++ b/package/pjsip/Makefile
@@ -0,0 +1,58 @@
+
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=pjsip
+PKG_VERSION:=1.6
+PKG_RELEASE:=1
+
+PKG_SOURCE:=pjproject-$(PKG_VERSION).tar.bz2
+PKG_SOURCE_URL:=http://www.pjsip.org/release/1.6/
+#PKG_MD5SUM:=9b7dc52656f5cbec846a7ba3299f73bd
+PKG_INSTALL:=1
+
+PKG_BUILD_DIR:=$(BUILD_DIR)/pjproject-$(PKG_VERSION)
+
+include $(INCLUDE_DIR)/package.mk
+
+define Package/pjsip
+ SECTION:=lib
+ CATEGORY:=Libraries
+ TITLE:=Voip lib
+ URL:=http://www.pjsip.org/
+ DEPENDS:=+libuuid
+endef
+
+CONFIGURE_ARGS += \
+ --disable-sound \
+ --disable-oss \
+ --disable-ext-sound \
+ --disable-speex-aec \
+ --enable-g711-codec \
+ --disable-l16-codec \
+ --disable-gsm-codec \
+ --disable-g722-codec \
+ --disable-g7221-codec \
+ --disable-speex-codec \
+ --disable-ilbc-coder \
+ --disable-libsamplerate \
+ --disable-ipp \
+ --disable-ssl \
+
+define Build/InstallDev
+ $(INSTALL_DIR) $(1)/usr/{include,lib}
+ $(CP) $(PKG_INSTALL_DIR)/usr/include/* \
+ $(1)/usr/include/
+ $(CP) $(PKG_INSTALL_DIR)/usr/lib/* \
+ $(1)/usr/lib/
+endef
+
+define Package/pjsip/install
+ $(INSTALL_DIR) $(1)/usr/sbin
+endef
+
+define Build/Compile
+ $(MAKE_VARS) $(MAKE) -C $(PKG_BUILD_DIR)/$(MAKE_PATH)
+endef
+
+
+$(eval $(call BuildPackage,pjsip))
diff --git a/package/tapi_sip/Makefile b/package/tapi_sip/Makefile
new file mode 100644
index 000000000..7a3d6766a
--- /dev/null
+++ b/package/tapi_sip/Makefile
@@ -0,0 +1,41 @@
+#
+# This is free software, licensed under the GNU General Public License v2.
+# See /LICENSE for more information.
+#
+
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=tapi_sip
+PKG_RELEASE:=1
+
+include $(INCLUDE_DIR)/package.mk
+
+define Package/tapi_sip
+ SECTION:=utils
+ CATEGORY:=Utilities
+ TITLE:=tapi_sip
+ DEPENDS:=+libuci +libtapi +pjsip +kmod-lqtapi
+endef
+
+define Build/Prepare
+ mkdir -p $(PKG_BUILD_DIR)
+ $(CP) ./src/* $(PKG_BUILD_DIR)/
+endef
+
+define Build/Compile
+ CFLAGS="$(TARGET_CPPFLAGS) $(TARGET_CFLAGS)" \
+ LDFLAGS="$(TARGET_LDFLAGS)" \
+ $(MAKE) -C $(PKG_BUILD_DIR) \
+ $(TARGET_CONFIGURE_OPTS)
+endef
+
+define Package/tapi_sip/install
+ $(INSTALL_DIR) $(1)/usr/bin
+ $(INSTALL_BIN) $(PKG_BUILD_DIR)/tapi-sip $(1)/usr/bin/
+
+ $(INSTALL_DIR) $(1)/etc/config $(1)/etc/init.d
+ $(INSTALL_DATA) ./files/telephony.conf $(1)/etc/config/telephony
+ $(INSTALL_BIN) ./files/telephony.init $(1)/etc/init.d/telephony
+endef
+
+$(eval $(call BuildPackage,tapi_sip))
diff --git a/package/tapi_sip/files/telephony.conf b/package/tapi_sip/files/telephony.conf
new file mode 100644
index 000000000..6d87f45a8
--- /dev/null
+++ b/package/tapi_sip/files/telephony.conf
@@ -0,0 +1,28 @@
+config 'config' 'config'
+# option 'fw_url' 'http://192.168.1.100/danube_firmware.bin'
+ option 'fw_file' 'danube_firmware.bin'
+ option 'netdev' 'pppoe-wan'
+ option 'disable' '1'
+
+config 'account' 'account'
+ option 'realm' 'example.com'
+ option 'username' 'user'
+ option 'password' 'password'
+ option 'stun_host' 'stun.example.com'
+ option 'stun_port' '3478'
+ option 'sip_port' '5600'
+
+config 'contact'
+ option 'name' 'sip example'
+ option 'identifier' 'sip:user@example.net'
+ option 'number' '123'
+
+config 'contact'
+ option 'name' 'local1'
+ option 'identifier' 'tel:1'
+ option 'number' '01'
+
+config 'contact'
+ option 'name' 'local2'
+ option 'identifier' 'tel:2'
+ option 'number' '02'
diff --git a/package/tapi_sip/files/telephony.init b/package/tapi_sip/files/telephony.init
new file mode 100755
index 000000000..082024c23
--- /dev/null
+++ b/package/tapi_sip/files/telephony.init
@@ -0,0 +1,50 @@
+#!/bin/sh /etc/rc.common
+START=80
+
+download_fw()
+{
+ config_load telephony
+ config_get fw_url config fw_url
+ config_get fw_file config fw_file
+ wget $fw_url -O /tmp/$fw_file || {
+ echo "failed to load $fw_url"
+ exit 1
+ }
+}
+
+load_module()
+{
+ M=`lsmod | grep vmmc`
+ [ -z "$M" ] || return
+ config_load telephony
+ config_get fw_file config fw_file
+ [ -z "fw_file" ] && exit 1
+ F=/lib/firmware/$fw_file
+ [ ! -f "$F" -a ! -L "$F" ] && {
+ echo "missing firmware file"
+ exit 1
+ }
+ [ -L "$F" -a -f /tmp/$fw_file ] && F=/tmp/$fw_file
+ insmod vmmc
+ sleep 3
+}
+
+stop()
+{
+ killall tapi-sip 2>/dev/null
+}
+
+start()
+{
+ stop
+ config_load telephony
+ config_get fw_url config fw_url
+ config_get fw_file config fw_file
+ config_get netdev config netdev
+ config_get disable config disable
+ [ "$disable" != "1" ] && {
+ [ ! -z "$fw_url" -a ! -f "/tmp/$fw_file" ] && download_fw
+ load_module
+ /usr/bin/tapi-sip $netdev &
+ }
+}
diff --git a/package/tapi_sip/src/Makefile b/package/tapi_sip/src/Makefile
new file mode 100644
index 000000000..56013700f
--- /dev/null
+++ b/package/tapi_sip/src/Makefile
@@ -0,0 +1,21 @@
+ifndef CFLAGS
+CFLAGS = -O2 -g -I ../src
+endif
+
+CFLAGS += -Werror -Wall -std=gnu99
+
+PJ_CFLAGS ?= `pkg-config libpjproject --cflags`
+PJ_LDFLAGS ?= `pkg-config libpjproject --libs`
+
+CFLAGS += $(PJ_CFLAGS)
+LDFLAGS += $(PJ_LDFLAGS)
+
+FPIC=-fPIC
+
+all: tapi-sip
+
+%.o: %.c
+ $(CC) -c -o $@ $^ $(CFLAGS)
+
+tapi-sip: contact.o session.o tapi_agent.o tapi_sip.o sip_client.o stun.o dialdetector.o
+ $(CC) -o $@ $^ -ltapi -luci $(LDFLAGS)
diff --git a/package/tapi_sip/src/agent.h b/package/tapi_sip/src/agent.h
new file mode 100644
index 000000000..795994178
--- /dev/null
+++ b/package/tapi_sip/src/agent.h
@@ -0,0 +1,41 @@
+#ifndef __AGENT_H__
+#define __AGENT_H__
+
+#include "agent.h"
+
+struct session;
+struct agent;
+
+struct agent_ops {
+ int (*invite)(struct agent *, struct session *);
+ int (*accept)(struct agent *, struct session *);
+ int (*hangup)(struct agent *, struct session *);
+
+ int (*get_endpoint)(struct agent *, struct session *);
+};
+
+struct agent {
+ const struct agent_ops *ops;
+};
+
+static inline int agent_invite(struct agent *agent, struct session *session)
+{
+ return agent->ops->invite(agent, session);
+}
+
+static inline int agent_accept(struct agent *agent, struct session *session)
+{
+ return agent->ops->accept(agent, session);
+}
+
+static inline int agent_hangup(struct agent *agent, struct session *session)
+{
+ return agent->ops->hangup(agent, session);
+}
+
+static inline int agent_get_endpoint(struct agent *agent, struct session *session)
+{
+ return agent->ops->get_endpoint(agent, session);
+}
+
+#endif
diff --git a/package/tapi_sip/src/contact.c b/package/tapi_sip/src/contact.c
new file mode 100644
index 000000000..eeb38b418
--- /dev/null
+++ b/package/tapi_sip/src/contact.c
@@ -0,0 +1,162 @@
+#include <malloc.h>
+#include <string.h>
+
+#include <uci.h>
+#include <ucimap.h>
+
+#include "list.h"
+#include "contact.h"
+
+static struct uci_context *ctx;
+static struct uci_package *pkg;
+static struct list_head contact_list;
+static struct list_head account_list;
+
+static int contact_init(struct uci_map *map, void *section,
+ struct uci_section *s)
+{
+ struct contact *p = section;
+ p->name = strdup(s->e.name);
+ return 0;
+}
+
+static int contact_add(struct uci_map *map, void *section)
+{
+ struct contact *c = section;
+ printf("add contact: %s\n", c->name);
+ list_add_tail(&c->head, &contact_list);
+ return 0;
+}
+
+static struct uci_optmap contact_uci_map[] = {
+ {
+ UCIMAP_OPTION(struct contact, identifier),
+ .type = UCIMAP_STRING,
+ .name = "identifier",
+ },
+ {
+ UCIMAP_OPTION(struct contact, number),
+ .type = UCIMAP_STRING,
+ .name = "number",
+ },
+};
+
+static struct uci_sectionmap contact_sectionmap = {
+ UCIMAP_SECTION(struct contact, map),
+ .type = "contact",
+ .init = contact_init,
+ .add = contact_add,
+ .options = contact_uci_map,
+ .n_options = ARRAY_SIZE(contact_uci_map),
+ .options_size = sizeof(struct uci_optmap),
+};
+
+static int account_init(struct uci_map *map, void *section,
+ struct uci_section *s)
+{
+ struct account *a = section;
+ a->name = strdup(s->e.name);
+ return 0;
+}
+
+static int account_add(struct uci_map *map, void *section)
+{
+ struct account *a = section;
+ list_add_tail(&a->head, &account_list);
+ return 0;
+}
+
+static struct uci_optmap account_uci_map[] = {
+ {
+ UCIMAP_OPTION(struct account, realm),
+ .type = UCIMAP_STRING,
+ .name = "realm",
+ },
+ {
+ UCIMAP_OPTION(struct account, username),
+ .type = UCIMAP_STRING,
+ .name = "username",
+ },
+ {
+ UCIMAP_OPTION(struct account, sip_port),
+ .type = UCIMAP_INT,
+ .name = "sip_port",
+ },
+ {
+ UCIMAP_OPTION(struct account, password),
+ .type = UCIMAP_STRING,
+ .name = "password",
+ },
+ {
+ UCIMAP_OPTION(struct account, stun_host),
+ .type = UCIMAP_STRING,
+ .name = "stun_host",
+ },
+ {
+ UCIMAP_OPTION(struct account, stun_port),
+ .type = UCIMAP_INT,
+ .name = "stun_port",
+ },
+};
+
+static struct uci_sectionmap account_sectionmap = {
+ UCIMAP_SECTION(struct account, map),
+ .type = "account",
+ .init = account_init,
+ .add = account_add,
+ .options = account_uci_map,
+ .n_options = ARRAY_SIZE(account_uci_map),
+ .options_size = sizeof(struct uci_optmap),
+};
+
+static struct uci_sectionmap *network_smap[] = {
+ &contact_sectionmap,
+ &account_sectionmap,
+};
+
+static struct uci_map contact_map = {
+ .sections = network_smap,
+ .n_sections = ARRAY_SIZE(network_smap),
+};
+
+int contacts_init(void)
+{
+ int ret;
+
+ INIT_LIST_HEAD(&contact_list);
+ INIT_LIST_HEAD(&account_list);
+ ctx = uci_alloc_context();
+
+ ucimap_init(&contact_map);
+ ret = uci_load(ctx, "telephony", &pkg);
+ if (ret)
+ return ret;
+
+ ucimap_parse(&contact_map, pkg);
+
+ return 0;
+}
+
+void contacts_free(void)
+{
+}
+
+struct contact *contact_get(const char *number)
+{
+ struct contact *contact;
+ list_for_each_entry(contact, &contact_list, head)
+ {
+ if (strcmp(contact->number, number) == 0)
+ return contact;
+ }
+
+ return NULL;
+}
+
+struct account *get_account(void)
+{
+ if (list_empty(&account_list))
+ return NULL;
+
+ return list_first_entry(&account_list, struct account, head);
+}
diff --git a/package/tapi_sip/src/contact.h b/package/tapi_sip/src/contact.h
new file mode 100644
index 000000000..1fb0037bf
--- /dev/null
+++ b/package/tapi_sip/src/contact.h
@@ -0,0 +1,33 @@
+#ifndef __CONTACT_H__
+#define __CONTACT_H__
+
+#include <ucimap.h>
+
+struct account {
+ struct ucimap_section_data map;
+ const char *name;
+ char *realm;
+ char *username;
+ char *password;
+ int sip_port;
+ char *stun_host;
+ int stun_port;
+ struct list_head head;
+};
+
+struct contact {
+ struct ucimap_section_data map;
+ const char *name;
+ const char *identifier;
+ const char *number;
+
+ struct list_head head;
+};
+
+int contacts_init(void);
+void contacts_free(void);
+struct contact *contact_get(const char *number);
+
+struct account *get_account(void);
+
+#endif
diff --git a/package/tapi_sip/src/dialdetector.c b/package/tapi_sip/src/dialdetector.c
new file mode 100644
index 000000000..277b0296c
--- /dev/null
+++ b/package/tapi_sip/src/dialdetector.c
@@ -0,0 +1,148 @@
+#include <linux/input.h>
+#include <sys/epoll.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include <events.h>
+#include "timerfd.h"
+
+#include "tapi-port.h"
+#include "dialdetector.h"
+
+static const struct itimerspec dialdetector_timeout = {
+ .it_value.tv_sec = 3,
+};
+
+static const struct itimerspec dialdetector_impulse_timeout = {
+ .it_value.tv_nsec = 200000000,
+};
+
+static void dialdetector_note_digit(struct dialdetector *d, unsigned char digit)
+{
+ printf("note digit: %d\n", d->num_digits);
+
+ d->digits[d->num_digits] = digit;
+ ++d->num_digits;
+
+ timerfd_settime(d->timer_fd, 0, &dialdetector_timeout, NULL);
+ d->dial_state = DIALDETECTOR_DIAL_WAIT_TIMEOUT;
+}
+
+static void dialdetector_reset(struct dialdetector *d)
+{
+ d->num_digits = 0;
+ d->impulses = 0;
+ d->dial_state = DIALDETECTOR_DIAL_WAIT;
+ d->port_state = DIALDETECTOR_PORT_INACTIVE;
+}
+
+static bool dialdetector_timeout_event(int events, void *data)
+{
+ char num[20];
+ struct dialdetector *dialdetector = data;
+ int i;
+ uint64_t tmp;
+
+ read(dialdetector->timer_fd, &tmp, sizeof(tmp));
+
+ for (i = 0; i < dialdetector->num_digits; ++i) {
+ num[i] = '0' + dialdetector->digits[i];
+ }
+ num[i] = '\0';
+
+ dialdetector->dial_callback(dialdetector->port, dialdetector->num_digits,
+ dialdetector->digits);
+
+ dialdetector_reset(dialdetector);
+
+ return true;
+}
+
+static bool dialdetector_impulse_timeout_cb(int events, void *data)
+{
+ struct dialdetector *d = data;
+ uint64_t tmp;
+
+ read(d->impulse_timer_fd, &tmp, sizeof(tmp));
+
+ if (d->port_state == DIALDETECTOR_PORT_ACTIVE_DOWN) {
+ d->port_state = DIALDETECTOR_PORT_INACTIVE;
+ } else {
+ printf("impulse: %d\n", d->impulses);
+ if (d->impulses > 0)
+ dialdetector_note_digit(d, d->impulses < 10 ? d->impulses : 0);
+ d->impulses = 0;
+ }
+
+ return true;
+}
+
+static void dialdetector_port_event(struct tapi_port *port,
+ struct tapi_event *event, void *data)
+{
+ struct dialdetector *d = data;
+
+ printf("port event: %d %d\n", d->port_state, event->hook.on);
+
+ switch (d->port_state) {
+ case DIALDETECTOR_PORT_INACTIVE:
+ if (event->type == TAPI_EVENT_TYPE_HOOK && event->hook.on == false)
+ d->port_state = DIALDETECTOR_PORT_ACTIVE;
+ break;
+ case DIALDETECTOR_PORT_ACTIVE:
+ switch (event->type) {
+ case TAPI_EVENT_TYPE_HOOK:
+ if (event->hook.on == true) {
+ d->port_state = DIALDETECTOR_PORT_ACTIVE_DOWN;
+ timerfd_settime(d->impulse_timer_fd, 0, &dialdetector_impulse_timeout, NULL);
+ }
+ break;
+ case TAPI_EVENT_TYPE_DTMF:
+ dialdetector_note_digit(d, event->dtmf.code);
+ break;
+ }
+ break;
+ case DIALDETECTOR_PORT_ACTIVE_DOWN:
+ if (event->type == TAPI_EVENT_TYPE_HOOK && event->hook.on == false) {
+ timerfd_settime(d->timer_fd, 0, &dialdetector_timeout, NULL);
+ ++d->impulses;
+ d->port_state = DIALDETECTOR_PORT_ACTIVE;
+ }
+ break;
+ }
+}
+
+struct dialdetector *dialdetector_alloc(struct tapi_port *port)
+{
+ struct dialdetector *dialdetector;
+ dialdetector = malloc(sizeof(*dialdetector));
+
+ dialdetector->timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
+ dialdetector->impulse_timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
+ dialdetector->port = port;
+ dialdetector->num_digits = 0;
+ dialdetector->impulses = 0;
+ dialdetector->dial_state = DIALDETECTOR_DIAL_WAIT;
+ dialdetector->port_state = DIALDETECTOR_PORT_INACTIVE;
+
+ dialdetector->timeout_cb.callback = dialdetector_timeout_event;
+ dialdetector->timeout_cb.data = dialdetector;
+ dialdetector->impulse_cb.callback = dialdetector_impulse_timeout_cb;
+ dialdetector->impulse_cb.data = dialdetector;
+
+ dialdetector->port_listener.callback = dialdetector_port_event;
+ dialdetector->port_listener.data = dialdetector;
+
+ tapi_port_register_event(port, &dialdetector->port_listener);
+
+ event_register(dialdetector->impulse_timer_fd, EPOLLIN,
+ &dialdetector->impulse_cb);
+ event_register(dialdetector->timer_fd, EPOLLIN, &dialdetector->timeout_cb);
+
+ return dialdetector;
+}
diff --git a/package/tapi_sip/src/dialdetector.h b/package/tapi_sip/src/dialdetector.h
new file mode 100644
index 000000000..c3737c36e
--- /dev/null
+++ b/package/tapi_sip/src/dialdetector.h
@@ -0,0 +1,49 @@
+#include <linux/input.h>
+#include <sys/epoll.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "events.h"
+#include "timerfd.h"
+
+#include "tapi-port.h"
+
+enum dialdetector_dial_state {
+ DIALDETECTOR_DIAL_WAIT = 1,
+ DIALDETECTOR_DIAL_WAIT_TIMEOUT = 2,
+};
+
+enum dialdetector_port_state {
+ DIALDETECTOR_PORT_INACTIVE = 0,
+ DIALDETECTOR_PORT_ACTIVE = 1,
+ DIALDETECTOR_PORT_ACTIVE_DOWN = 2,
+};
+
+struct dialdetector {
+ enum dialdetector_dial_state dial_state;
+ enum dialdetector_port_state port_state;
+
+ struct tapi_port *port;
+ int timer_fd;
+ int impulse_timer_fd;
+
+ struct event_callback timeout_cb;
+ struct event_callback impulse_cb;
+ struct tapi_port_event_listener port_listener;
+
+ size_t num_digits;
+ unsigned char digits[20];
+
+ unsigned int impulses;
+
+ void (*dial_callback)(struct tapi_port *port, size_t num_digits, const unsigned char *digits);
+};
+
+
+struct tapi_port;
+
+struct dialdetector *dialdetector_alloc(struct tapi_port *port);
diff --git a/package/tapi_sip/src/list.h b/package/tapi_sip/src/list.h
new file mode 100644
index 000000000..2959a061d
--- /dev/null
+++ b/package/tapi_sip/src/list.h
@@ -0,0 +1,601 @@
+#ifndef _LINUX_LIST_H
+#define _LINUX_LIST_H
+
+#include <stddef.h>
+/**
+ * container_of - cast a member of a structure out to the containing structure
+ * @ptr: the pointer to the member.
+ * @type: the type of the container struct this is embedded in.
+ * @member: the name of the member within the struct.
+ *
+ */
+#ifndef container_of
+#define container_of(ptr, type, member) ( \
+ (type *)( (char *)ptr - offsetof(type,member) ))
+#endif
+
+
+/*
+ * Simple doubly linked list implementation.
+ *
+ * Some of the internal functions ("__xxx") are useful when
+ * manipulating whole lists rather than single entries, as
+ * sometimes we already know the next/prev entries and we can
+ * generate better code by using them directly rather than
+ * using the generic single-entry routines.
+ */
+
+struct list_head {
+ struct list_head *next, *prev;
+};
+
+#define LIST_HEAD_INIT(name) { &(name), &(name) }
+
+#define LIST_HEAD(name) \
+ struct list_head name = LIST_HEAD_INIT(name)
+
+static inline void INIT_LIST_HEAD(struct list_head *list)
+{
+ list->next = list;
+ list->prev = list;
+}
+
+/*
+ * Insert a new entry between two known consecutive entries.
+ *
+ * This is only for internal list manipulation where we know
+ * the prev/next entries already!
+ */
+static inline void __list_add(struct list_head *new,
+ struct list_head *prev,
+ struct list_head *next)
+{
+ next->prev = new;
+ new->next = next;
+ new->prev = prev;
+ prev->next = new;
+}
+
+/**
+ * list_add - add a new entry
+ * @new: new entry to be added
+ * @head: list head to add it after
+ *
+ * Insert a new entry after the specified head.
+ * This is good for implementing stacks.
+ */
+static inline void list_add(struct list_head *new, struct list_head *head)
+{
+ __list_add(new, head, head->next);
+}
+
+
+/**
+ * list_add_tail - add a new entry
+ * @new: new entry to be added
+ * @head: list head to add it before
+ *
+ * Insert a new entry before the specified head.
+ * This is useful for implementing queues.
+ */
+static inline void list_add_tail(struct list_head *new, struct list_head *head)
+{
+ __list_add(new, head->prev, head);
+}
+
+
+/*
+ * Delete a list entry by making the prev/next entries
+ * point to each other.
+ *
+ * This is only for internal list manipulation where we know
+ * the prev/next entries already!
+ */
+static inline void __list_del(struct list_head * prev, struct list_head * next)
+{
+ next->prev = prev;
+ prev->next = next;
+}
+
+/**
+ * list_del - deletes entry from list.
+ * @entry: the element to delete from the list.
+ * Note: list_empty() on entry does not return true after this, the entry is
+ * in an undefined state.
+ */
+static inline void list_del(struct list_head *entry)
+{
+ __list_del(entry->prev, entry->next);
+ entry->next = NULL;
+ entry->prev = NULL;
+}
+
+/**
+ * list_replace - replace old entry by new one
+ * @old : the element to be replaced
+ * @new : the new element to insert
+ *
+ * If @old was empty, it will be overwritten.
+ */
+static inline void list_replace(struct list_head *old,
+ struct list_head *new)
+{
+ new->next = old->next;
+ new->next->prev = new;
+ new->prev = old->prev;
+ new->prev->next = new;
+}
+
+static inline void list_replace_init(struct list_head *old,
+ struct list_head *new)
+{
+ list_replace(old, new);
+ INIT_LIST_HEAD(old);
+}
+
+/**
+ * list_del_init - deletes entry from list and reinitialize it.
+ * @entry: the element to delete from the list.
+ */
+static inline void list_del_init(struct list_head *entry)
+{
+ __list_del(entry->prev, entry->next);
+ INIT_LIST_HEAD(entry);
+}
+
+/**
+ * list_move - delete from one list and add as another's head
+ * @list: the entry to move
+ * @head: the head that will precede our entry
+ */
+static inline void list_move(struct list_head *list, struct list_head *head)
+{
+ __list_del(list->prev, list->next);
+ list_add(list, head);
+}
+
+/**
+ * list_move_tail - delete from one list and add as another's tail
+ * @list: the entry to move
+ * @head: the head that will follow our entry
+ */
+static inline void list_move_tail(struct list_head *list,
+ struct list_head *head)
+{
+ __list_del(list->prev, list->next);
+ list_add_tail(list, head);
+}
+
+/**
+ * list_is_last - tests whether @list is the last entry in list @head
+ * @list: the entry to test
+ * @head: the head of the list
+ */
+static inline int list_is_last(const struct list_head *list,
+ const struct list_head *head)
+{
+ return list->next == head;
+}
+
+/**
+ * list_empty - tests whether a list is empty
+ * @head: the list to test.
+ */
+static inline int list_empty(const struct list_head *head)
+{
+ return head->next == head;
+}
+
+/**
+ * list_empty_careful - tests whether a list is empty and not being modified
+ * @head: the list to test
+ *
+ * Description:
+ * tests whether a list is empty _and_ checks that no other CPU might be
+ * in the process of modifying either member (next or prev)
+ *
+ * NOTE: using list_empty_careful() without synchronization
+ * can only be safe if the only activity that can happen
+ * to the list entry is list_del_init(). Eg. it cannot be used
+ * if another CPU could re-list_add() it.
+ */
+static inline int list_empty_careful(const struct list_head *head)
+{
+ struct list_head *next = head->next;
+ return (next == head) && (next == head->prev);
+}
+
+static inline void __list_splice(struct list_head *list,
+ struct list_head *head)
+{
+ struct list_head *first = list->next;
+ struct list_head *last = list->prev;
+ struct list_head *at = head->next;
+
+ first->prev = head;
+ head->next = first;
+
+ last->next = at;
+ at->prev = last;
+}
+
+/**
+ * list_splice - join two lists
+ * @list: the new list to add.
+ * @head: the place to add it in the first list.
+ */
+static inline void list_splice(struct list_head *list, struct list_head *head)
+{
+ if (!list_empty(list))
+ __list_splice(list, head);
+}
+
+/**
+ * list_splice_init - join two lists and reinitialise the emptied list.
+ * @list: the new list to add.
+ * @head: the place to add it in the first list.
+ *
+ * The list at @list is reinitialised
+ */
+static inline void list_splice_init(struct list_head *list,
+ struct list_head *head)
+{
+ if (!list_empty(list)) {
+ __list_splice(list, head);
+ INIT_LIST_HEAD(list);
+ }
+}
+
+/**
+ * list_entry - get the struct for this entry
+ * @ptr: the &struct list_head pointer.
+ * @type: the type of the struct this is embedded in.
+ * @member: the name of the list_struct within the struct.
+ */
+#define list_entry(ptr, type, member) \
+ container_of(ptr, type, member)
+
+/**
+ * list_first_entry - get the first element from a list
+ * @ptr: the list head to take the element from.
+ * @type: the type of the struct this is embedded in.
+ * @member: the name of the list_struct within the struct.
+ *
+ * Note, that list is expected to be not empty.
+ */
+#define list_first_entry(ptr, type, member) \
+ list_entry((ptr)->next, type, member)
+
+/**
+ * list_for_each - iterate over a list
+ * @pos: the &struct list_head to use as a loop cursor.
+ * @head: the head for your list.
+ */
+#define list_for_each(pos, head) \
+ for (pos = (head)->next; pos != (head); \
+ pos = pos->next)
+
+/**
+ * __list_for_each - iterate over a list
+ * @pos: the &struct list_head to use as a loop cursor.
+ * @head: the head for your list.
+ *
+ * This variant differs from list_for_each() in that it's the
+ * simplest possible list iteration code, no prefetching is done.
+ * Use this for code that knows the list to be very short (empty
+ * or 1 entry) most of the time.
+ */
+#define __list_for_each(pos, head) \
+ for (pos = (head)->next; pos != (head); pos = pos->next)
+
+/**
+ * list_for_each_prev - iterate over a list backwards
+ * @pos: the &struct list_head to use as a loop cursor.
+ * @head: the head for your list.
+ */
+#define list_for_each_prev(pos, head) \
+ for (pos = (head)->prev; pos != (head); \
+ pos = pos->prev)
+
+/**
+ * list_for_each_safe - iterate over a list safe against removal of list entry
+ * @pos: the &struct list_head to use as a loop cursor.
+ * @n: another &struct list_head to use as temporary storage
+ * @head: the head for your list.
+ */
+#define list_for_each_safe(pos, n, head) \
+ for (pos = (head)->next, n = pos->next; pos != (head); \
+ pos = n, n = pos->next)
+
+/**
+ * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
+ * @pos: the &struct list_head to use as a loop cursor.
+ * @n: another &struct list_head to use as temporary storage
+ * @head: the head for your list.
+ */
+#define list_for_each_prev_safe(pos, n, head) \
+ for (pos = (head)->prev, n = pos->prev; \
+ pos != (head); \
+ pos = n, n = pos->prev)
+
+/**
+ * list_for_each_entry - iterate over list of given type
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ */
+#define list_for_each_entry(pos, head, member) \
+ for (pos = list_entry((head)->next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = list_entry(pos->member.next, typeof(*pos), member))
+
+/**
+ * list_for_each_entry_reverse - iterate backwards over list of given type.
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ */
+#define list_for_each_entry_reverse(pos, head, member) \
+ for (pos = list_entry((head)->prev, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = list_entry(pos->member.prev, typeof(*pos), member))
+
+/**
+ * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
+ * @pos: the type * to use as a start point
+ * @head: the head of the list
+ * @member: the name of the list_struct within the struct.
+ *
+ * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
+ */
+#define list_prepare_entry(pos, head, member) \
+ ((pos) ? : list_entry(head, typeof(*pos), member))
+
+/**
+ * list_for_each_entry_continue - continue iteration over list of given type
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ *
+ * Continue to iterate over list of given type, continuing after
+ * the current position.
+ */
+#define list_for_each_entry_continue(pos, head, member) \
+ for (pos = list_entry(pos->member.next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = list_entry(pos->member.next, typeof(*pos), member))
+
+/**
+ * list_for_each_entry_continue_reverse - iterate backwards from the given point
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ *
+ * Start to iterate over list of given type backwards, continuing after
+ * the current position.
+ */
+#define list_for_each_entry_continue_reverse(pos, head, member) \
+ for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = list_entry(pos->member.prev, typeof(*pos), member))
+
+/**
+ * list_for_each_entry_from - iterate over list of given type from the current point
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ *
+ * Iterate over list of given type, continuing from current position.
+ */
+#define list_for_each_entry_from(pos, head, member) \
+ for (; &pos->member != (head); \
+ pos = list_entry(pos->member.next, typeof(*pos), member))
+
+/**
+ * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
+ * @pos: the type * to use as a loop cursor.
+ * @n: another type * to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ */
+#define list_for_each_entry_safe(pos, n, head, member) \
+ for (pos = list_entry((head)->next, typeof(*pos), member), \
+ n = list_entry(pos->member.next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = n, n = list_entry(n->member.next, typeof(*n), member))
+
+/**
+ * list_for_each_entry_safe_continue
+ * @pos: the type * to use as a loop cursor.
+ * @n: another type * to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ *
+ * Iterate over list of given type, continuing after current point,
+ * safe against removal of list entry.
+ */
+#define list_for_each_entry_safe_continue(pos, n, head, member) \
+ for (pos = list_entry(pos->member.next, typeof(*pos), member), \
+ n = list_entry(pos->member.next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = n, n = list_entry(n->member.next, typeof(*n), member))
+
+/**
+ * list_for_each_entry_safe_from
+ * @pos: the type * to use as a loop cursor.
+ * @n: another type * to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ *
+ * Iterate over list of given type from current point, safe against
+ * removal of list entry.
+ */
+#define list_for_each_entry_safe_from(pos, n, head, member) \
+ for (n = list_entry(pos->member.next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = n, n = list_entry(n->member.next, typeof(*n), member))
+
+/**
+ * list_for_each_entry_safe_reverse
+ * @pos: the type * to use as a loop cursor.
+ * @n: another type * to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ *
+ * Iterate backwards over list of given type, safe against removal
+ * of list entry.
+ */
+#define list_for_each_entry_safe_reverse(pos, n, head, member) \
+ for (pos = list_entry((head)->prev, typeof(*pos), member), \
+ n = list_entry(pos->member.prev, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = n, n = list_entry(n->member.prev, typeof(*n), member))
+
+/*
+ * Double linked lists with a single pointer list head.
+ * Mostly useful for hash tables where the two pointer list head is
+ * too wasteful.
+ * You lose the ability to access the tail in O(1).
+ */
+
+struct hlist_head {
+ struct hlist_node *first;
+};
+
+struct hlist_node {
+ struct hlist_node *next, **pprev;
+};
+
+#define HLIST_HEAD_INIT { .first = NULL }
+#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
+#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
+static inline void INIT_HLIST_NODE(struct hlist_node *h)
+{
+ h->next = NULL;
+ h->pprev = NULL;
+}
+
+static inline int hlist_unhashed(const struct hlist_node *h)
+{
+ return !h->pprev;
+}
+
+static inline int hlist_empty(const struct hlist_head *h)
+{
+ return !h->first;
+}
+
+static inline void __hlist_del(struct hlist_node *n)
+{
+ struct hlist_node *next = n->next;
+ struct hlist_node **pprev = n->pprev;
+ *pprev = next;
+ if (next)
+ next->pprev = pprev;
+}
+
+static inline void hlist_del(struct hlist_node *n)
+{
+ __hlist_del(n);
+ n->next = NULL;
+ n->pprev = NULL;
+}
+
+static inline void hlist_del_init(struct hlist_node *n)
+{
+ if (!hlist_unhashed(n)) {
+ __hlist_del(n);
+ INIT_HLIST_NODE(n);
+ }
+}
+
+
+static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
+{
+ struct hlist_node *first = h->first;
+ n->next = first;
+ if (first)
+ first->pprev = &n->next;
+ h->first = n;
+ n->pprev = &h->first;
+}
+
+
+/* next must be != NULL */
+static inline void hlist_add_before(struct hlist_node *n,
+ struct hlist_node *next)
+{
+ n->pprev = next->pprev;
+ n->next = next;
+ next->pprev = &n->next;
+ *(n->pprev) = n;
+}
+
+static inline void hlist_add_after(struct hlist_node *n,
+ struct hlist_node *next)
+{
+ next->next = n->next;
+ n->next = next;
+ next->pprev = &n->next;
+
+ if(next->next)
+ next->next->pprev = &next->next;
+}
+
+#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
+
+#define hlist_for_each(pos, head) \
+ for (pos = (head)->first; pos; pos = pos->next)
+
+#define hlist_for_each_safe(pos, n, head) \
+ for (pos = (head)->first; pos; pos = n)
+
+/**
+ * hlist_for_each_entry - iterate over list of given type
+ * @tpos: the type * to use as a loop cursor.
+ * @pos: the &struct hlist_node to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the hlist_node within the struct.
+ */
+#define hlist_for_each_entry(tpos, pos, head, member) \
+ for (pos = (head)->first; pos && \
+ ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
+ pos = pos->next)
+
+/**
+ * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
+ * @tpos: the type * to use as a loop cursor.
+ * @pos: the &struct hlist_node to use as a loop cursor.
+ * @member: the name of the hlist_node within the struct.
+ */
+#define hlist_for_each_entry_continue(tpos, pos, member) \
+ for (pos = (pos)->next; pos && \
+ ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
+ pos = pos->next)
+
+/**
+ * hlist_for_each_entry_from - iterate over a hlist continuing from current point
+ * @tpos: the type * to use as a loop cursor.
+ * @pos: the &struct hlist_node to use as a loop cursor.
+ * @member: the name of the hlist_node within the struct.
+ */
+#define hlist_for_each_entry_from(tpos, pos, member) \
+ for (; pos && \
+ ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
+ pos = pos->next)
+
+/**
+ * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
+ * @tpos: the type * to use as a loop cursor.
+ * @pos: the &struct hlist_node to use as a loop cursor.
+ * @n: another &struct hlist_node to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the hlist_node within the struct.
+ */
+#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
+ for (pos = (head)->first; \
+ pos && ({ n = pos->next; 1; }) && \
+ ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
+ pos = n)
+
+#endif
diff --git a/package/tapi_sip/src/session.c b/package/tapi_sip/src/session.c
new file mode 100644
index 000000000..97f6f06fe
--- /dev/null
+++ b/package/tapi_sip/src/session.c
@@ -0,0 +1,84 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "agent.h"
+#include "session.h"
+
+#include <tapi-device.h>
+
+
+struct session
+{
+ struct agent *agents[2];
+
+ struct tapi_device *tdev;
+
+ void (*release)(struct session *);
+
+ int link;
+};
+
+struct session *session_alloc(struct tapi_device *dev, struct agent *caller,
+ struct agent *callee, void (*release)(struct session *))
+{
+ struct session *session;
+ int ret;
+
+ session = malloc(sizeof(*session));
+
+ session->tdev = dev;
+
+ session->agents[0] = caller;
+ session->agents[1] = callee;
+
+ ret = agent_invite(callee, session);
+ if (ret < 0) {
+ session_hangup(session, callee);
+ free(session);
+ return NULL;
+ }
+
+ session->release = release;
+
+ return session;
+}
+
+void session_accept(struct session *session, struct agent *agent)
+{
+ int ep[2];
+
+ printf("session_accept: %p %p\n", session, agent);
+ printf("session agents: %p %p\n", session->agents[0], session->agents[1]);
+ printf("session tdev: %p\n", session->tdev);
+
+ agent_accept(session->agents[0], session);
+
+ ep[0] = agent_get_endpoint(session->agents[0], session);
+ ep[1] = agent_get_endpoint(session->agents[1], session);
+ session->link = tapi_link_alloc(session->tdev, ep[0], ep[1]);
+
+ printf("eps: %d %d\n", ep[0], ep[1]);
+
+ tapi_link_enable(session->tdev, session->link);
+
+ tapi_sync(session->tdev);
+}
+
+void session_hangup(struct session *session, struct agent *agent)
+{
+ struct agent *other_agent;
+
+ if (session->agents[0] == agent)
+ other_agent = session->agents[1];
+ else
+ other_agent = session->agents[0];
+
+ agent_hangup(other_agent, session);
+
+ tapi_link_disable(session->tdev, session->link);
+ tapi_link_free(session->tdev, session->link);
+ tapi_sync(session->tdev);
+
+ if (session->release)
+ session->release(session);
+}
diff --git a/package/tapi_sip/src/session.h b/package/tapi_sip/src/session.h
new file mode 100644
index 000000000..7403785f5
--- /dev/null
+++ b/package/tapi_sip/src/session.h
@@ -0,0 +1,14 @@
+#ifndef __SESSION_H__
+#define __SESSION_H__
+
+struct agent;
+struct session;
+struct tapi_device;
+
+struct session *session_alloc(struct tapi_device *, struct agent *caller,
+ struct agent *callee, void (*release)(struct session *));
+void session_hangup(struct session *, struct agent *);
+void session_accept(struct session *, struct agent *);
+void session_free(struct session *);
+
+#endif
diff --git a/package/tapi_sip/src/sip_agent.h b/package/tapi_sip/src/sip_agent.h
new file mode 100644
index 000000000..7f711da43
--- /dev/null
+++ b/package/tapi_sip/src/sip_agent.h
@@ -0,0 +1,27 @@
+#ifndef __SIP_AGENT_H__
+#define __SIP_AGENT_H__
+
+#include "agent.h"
+#include <events.h>
+
+struct sip_agent {
+ struct sip_client *client;
+ const char *identifier;
+
+ struct tapi_stream *stream;
+ struct session *session;
+
+ struct pjsip_inv_session *inv;
+
+ int rtp_sockfd;
+
+ struct sockaddr_storage remote_addr;
+ struct sockaddr_storage local_addr;
+
+ struct agent agent;
+
+ struct event_callback rtp_recv_callback;
+ struct event_callback stream_recv_callback;
+};
+
+#endif
diff --git a/package/tapi_sip/src/sip_client.c b/package/tapi_sip/src/sip_client.c
new file mode 100644
index 000000000..0b1e2ddd8
--- /dev/null
+++ b/package/tapi_sip/src/sip_client.c
@@ -0,0 +1,800 @@
+#include "sip_client.h"
+
+#include <stdint.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <errno.h>
+#include <sys/epoll.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
+
+#include "stun.h"
+#include "sip_agent.h"
+#include "session.h"
+#include "list.h"
+
+static inline struct sip_agent *agent_to_sip_agent(struct agent *agent)
+{
+ return container_of(agent, struct sip_agent, agent);
+}
+
+static int iface_get_addr(const char *iface, struct sockaddr_storage *addr)
+{
+ int fd;
+ int ret;
+ struct ifreq ifr;
+
+ fd = socket(AF_INET, SOCK_DGRAM, 0);
+
+ ifr.ifr_addr.sa_family = AF_INET;
+ strncpy(ifr.ifr_name, iface, IFNAMSIZ-1);
+ ret = ioctl(fd, SIOCGIFADDR, &ifr);
+ if (ret < 0)
+ perror("Failed to get interface address");
+
+ close(fd);
+
+ if (ret == 0)
+ memcpy(addr, &ifr.ifr_addr, sizeof(ifr.ifr_addr));
+
+ return ret;
+}
+
+#if 0
+static bool sockaddr_is_local(struct sockaddr_storage *addr)
+{
+ unsigned long s_addr;
+ bool is_local = false;
+
+ switch (addr->ss_family) {
+ case AF_INET:
+ s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
+ if ((s_addr & 0xff000000) == 0x10000000)
+ is_local = true;
+ else if ((s_addr & 0xfff00000) == 0xac100000)
+ is_local = true;
+ else if ((s_addr & 0xffff0000) == 0xc0a80000)
+ is_local = true;
+ break;
+ default:
+ break;
+ }
+
+ return is_local;
+}
+#endif
+
+static uint16_t sockaddr_get_port(struct sockaddr_storage *addr)
+{
+ uint16_t port;
+
+ switch (addr->ss_family) {
+ case AF_INET:
+ port = ((struct sockaddr_in *)addr)->sin_port;
+ break;
+ case AF_INET6:
+ port = ((struct sockaddr_in6 *)addr)->sin6_port;
+ break;
+ default:
+ port = 0;
+ break;
+ }
+
+ return port;
+}
+
+static void sockaddr_set_port(struct sockaddr_storage *addr, uint16_t port)
+{
+ switch (addr->ss_family) {
+ case AF_INET:
+ ((struct sockaddr_in *)addr)->sin_port = port;
+ break;
+ case AF_INET6:
+ ((struct sockaddr_in6 *)addr)->sin6_port = port;
+ break;
+ default:
+ break;
+ }
+}
+
+static void *sockaddr_get_addr(struct sockaddr_storage *addr)
+{
+ void *a;
+ switch (addr->ss_family) {
+ case AF_INET:
+ a = &((struct sockaddr_in *)addr)->sin_addr.s_addr;
+ break;
+ case AF_INET6:
+ a = ((struct sockaddr_in6 *)addr)->sin6_addr.s6_addr;
+ break;
+ default:
+ a = NULL;
+ break;
+ }
+
+ return a;
+}
+
+static int sockaddr_to_string(struct sockaddr_storage *addr, char *s, size_t n)
+{
+ return inet_ntop(addr->ss_family, sockaddr_get_addr(addr), s, n) == NULL ? -1 : 0;
+}
+
+static pjsip_module mod_siprtp;
+static struct sip_client *global_client;
+
+/* Creates a datagram socket and binds it to a port in the range of
+ * start_port-end_port */
+static int sip_client_create_socket(struct sip_client *client,
+ struct sockaddr_storage *sockaddr, uint16_t start_port,
+ uint16_t end_port)
+{
+ int sockfd;
+ int ret;
+
+ sockfd = socket(AF_INET, SOCK_DGRAM, 0);
+ if (sockfd < 0)
+ return sockfd;
+
+ memcpy(sockaddr, &client->local_addr, sizeof(client->local_addr));
+ do {
+ sockaddr_set_port(sockaddr, start_port);
+ ret = bind(sockfd, (struct sockaddr *)sockaddr, sizeof(*sockaddr));
+ ++start_port;
+ } while (ret == -1 && start_port < end_port);
+
+ if (ret == -1)
+ return -1;
+
+ return sockfd;
+}
+
+static int sip_worker_thread(void *arg)
+{
+ struct sip_client *client = arg;
+
+ while (1) {
+ pj_time_val timeout = {0, 10};
+ pjsip_endpt_handle_events(client->sip_endpt, &timeout);
+ }
+
+ return 0;
+}
+
+static bool sip_agent_stream_recv_callback(int events, void *data)
+{
+ struct sip_agent *agent = data;
+ char buf[512];
+ int len;
+ int ret;
+
+ len = read(agent->stream->fd, buf, 512);
+ if (len < 0)
+ return true;
+
+ ret = sendto(agent->rtp_sockfd, buf, len, 0,
+ (struct sockaddr *)&agent->remote_addr, sizeof(agent->remote_addr));
+
+ if (ret < 0)
+ printf("failed to send rtp data: %d\n", errno);
+
+ return true;
+}
+
+
+static bool sip_agent_rtp_recv_callback(int events, void *data)
+{
+ struct sip_agent *agent = data;
+ char buf[512];
+ int len;
+
+ len = recvfrom(agent->rtp_sockfd, buf, 512, 0, NULL, NULL);
+ if (agent->stream)
+ write(agent->stream->fd, buf, len);
+
+ return true;
+}
+
+static pj_status_t sip_client_create_sdp(struct sip_client *client, pj_pool_t *pool,
+ struct sip_agent *agent,
+ pjmedia_sdp_session **p_sdp)
+{
+ pj_time_val tv;
+ pjmedia_sdp_session *sdp;
+ pjmedia_sdp_media *m;
+ pjmedia_sdp_attr *attr;
+ struct sockaddr_storage rtp_addr;
+ char addr[INET6_ADDRSTRLEN];
+
+ PJ_ASSERT_RETURN(pool && p_sdp, PJ_EINVAL);
+
+ agent->rtp_sockfd = sip_client_create_socket(client, &rtp_addr, 4000, 5000);
+ if (client->stun)
+ stun_client_resolve(client->stun, agent->rtp_sockfd,
+ (struct sockaddr *)&rtp_addr);
+
+ agent->rtp_recv_callback.callback = sip_agent_rtp_recv_callback;
+ agent->rtp_recv_callback.data = agent;
+ event_register(agent->rtp_sockfd, EPOLLIN, &agent->rtp_recv_callback);
+
+ /* Create and initialize basic SDP session */
+ sdp = pj_pool_zalloc (pool, sizeof(pjmedia_sdp_session));
+
+ pj_gettimeofday(&tv);
+ sdp->origin.user = pj_str("pjsip-siprtp");
+ sdp->origin.version = sdp->origin.id = tv.sec + 2208988800UL;
+ sdp->origin.net_type = pj_str("IN");
+ sdp->origin.addr_type = pj_str("IP4");
+ sdp->origin.addr = *pj_gethostname();
+ sdp->name = pj_str("pjsip");
+
+ /* Since we only support one media stream at present, put the
+ * SDP connection line in the session level.
+ */
+ sdp->conn = pj_pool_zalloc(pool, sizeof(pjmedia_sdp_conn));
+ sdp->conn->net_type = pj_str("IN");
+ sdp->conn->addr_type = pj_str("IP4");
+ sockaddr_to_string(&rtp_addr, addr, sizeof(addr));
+ pj_strdup2_with_null(pool, &sdp->conn->addr, addr);
+
+ /* SDP time and attributes. */
+ sdp->time.start = sdp->time.stop = 0;
+ sdp->attr_count = 0;
+
+ /* Create media stream 0: */
+
+ sdp->media_count = 1;
+ m = pj_pool_zalloc(pool, sizeof(pjmedia_sdp_media));
+ sdp->media[0] = m;
+
+ /* Standard media info: */
+ m->desc.media = pj_str("audio");
+ m->desc.port = sockaddr_get_port(&rtp_addr);
+ m->desc.port_count = 1;
+ m->desc.transport = pj_str("RTP/AVP");
+
+ /* Add format and rtpmap for each codec. */
+ m->desc.fmt_count = 1;
+ m->attr_count = 0;
+
+ {
+ pjmedia_sdp_rtpmap rtpmap;
+ char ptstr[10];
+
+ sprintf(ptstr, "%d", 0);
+ pj_strdup2_with_null(pool, &m->desc.fmt[0], ptstr);
+ rtpmap.pt = m->desc.fmt[0];
+ rtpmap.clock_rate = 64000;
+ rtpmap.enc_name = pj_str("PCMU");
+ rtpmap.param.slen = 0;
+
+ }
+
+ /* Add sendrecv attribute. */
+ attr = pj_pool_zalloc(pool, sizeof(pjmedia_sdp_attr));
+ attr->name = pj_str("sendrecv");
+ m->attr[m->attr_count++] = attr;
+
+ /* Done */
+ *p_sdp = sdp;
+
+ return PJ_SUCCESS;
+}
+
+static int sip_agent_invite(struct agent *agent, struct session *session)
+{
+ struct sip_agent *sip_agent = agent_to_sip_agent(agent);
+ struct sip_client *client = sip_agent->client;
+ pjsip_dialog *dlg;
+ pjmedia_sdp_session *sdp;
+ pjsip_tx_data *tdata;
+ pj_status_t status;
+ pj_str_t dst_uri;
+
+ sip_agent->session = session;
+
+ dst_uri = pj_str((char *)sip_agent->identifier);
+
+ /* Create UAC dialog */
+ status = pjsip_dlg_create_uac(pjsip_ua_instance(),
+ &client->local_contact, /* local URI */
+ &client->local_contact, /* local Contact */
+ &dst_uri, /* remote URI */
+ &dst_uri, /* remote target */
+ &dlg); /* dialog */
+ if (status != PJ_SUCCESS) {
+ fprintf(stderr, "Failed to create uac dialog\n");
+ return -1;
+ }
+
+ pjsip_auth_clt_set_credentials(&dlg->auth_sess, 1, &client->cred);
+
+ /* Create SDP */
+ sip_client_create_sdp(client, dlg->pool, sip_agent, &sdp);
+
+ /* Create the INVITE session. */
+ status = pjsip_inv_create_uac(dlg, sdp, 0, &sip_agent->inv);
+ if (status != PJ_SUCCESS) {
+ fprintf(stderr, "Failed to create invite session\n");
+ pjsip_dlg_terminate(dlg);
+ return -1;
+ }
+
+
+ /* Attach call data to invite session */
+ sip_agent->inv->mod_data[mod_siprtp.id] = sip_agent;
+
+ /* Create initial INVITE request.
+ * This INVITE request will contain a perfectly good request and
+ * an SDP body as well.
+ */
+ status = pjsip_inv_invite(sip_agent->inv, &tdata);
+ PJ_ASSERT_RETURN(status == PJ_SUCCESS, -1); /*TODO*/
+
+ /* Send initial INVITE request.
+ * From now on, the invite session's state will be reported to us
+ * via the invite session callbacks.
+ */
+ status = pjsip_inv_send_msg(sip_agent->inv, tdata);
+ PJ_ASSERT_RETURN(status == PJ_SUCCESS, -1); /*TODO*/
+
+ return 0;
+}
+
+static int sip_agent_hangup(struct agent *agent, struct session *session)
+{
+ struct sip_agent *sip_agent = agent_to_sip_agent(agent);
+ pjsip_tx_data *tdata;
+ pj_status_t status;
+
+ printf("hangup %p\n", sip_agent->inv);
+
+ status = pjsip_inv_end_session(sip_agent->inv, 603, NULL, &tdata);
+ if (status == PJ_SUCCESS && tdata != NULL)
+ pjsip_inv_send_msg(sip_agent->inv, tdata);
+
+ if (sip_agent->rtp_sockfd) {
+ event_unregister(sip_agent->rtp_sockfd);
+ close(sip_agent->rtp_sockfd);
+ }
+ if (sip_agent->stream) {
+ event_unregister(sip_agent->stream->fd);
+ tapi_stream_free(sip_agent->stream);
+ }
+
+ sip_agent->inv->mod_data[mod_siprtp.id] = NULL;
+ free(sip_agent);
+
+ return 0;
+}
+
+static int sip_agent_alloc_stream(struct sip_agent *agent)
+{
+ int flags;
+
+ if (agent->stream)
+ printf("BUG!!!! %s:%s[%d]\n", __FILE__, __func__, __LINE__);
+
+ agent->stream = tapi_stream_alloc(agent->client->tdev);
+ agent->stream_recv_callback.callback = sip_agent_stream_recv_callback;
+ agent->stream_recv_callback.data = agent;
+
+ flags = fcntl(agent->stream->fd, F_GETFL, 0);
+ fcntl(agent->stream->fd, F_SETFL, flags | O_NONBLOCK);
+
+ event_register(agent->stream->fd, EPOLLIN, &agent->stream_recv_callback);
+
+ return 0;
+}
+
+static void sip_agent_free_stream(struct sip_agent *agent)
+{
+ if (!agent->stream)
+ return;
+
+ event_unregister(agent->stream->fd);
+ tapi_stream_free(agent->stream);
+ agent->stream = NULL;
+}
+
+static int sip_agent_accept(struct agent *agent, struct session *session)
+{
+ struct sip_agent *sip_agent = agent_to_sip_agent(agent);
+ pj_status_t status;
+ pjsip_tx_data *tdata;
+
+ /* Create 200 response .*/
+ status = pjsip_inv_answer(sip_agent->inv, 200,
+ NULL, NULL, &tdata);
+ if (status != PJ_SUCCESS) {
+ status = pjsip_inv_answer(sip_agent->inv,
+ PJSIP_SC_NOT_ACCEPTABLE,
+ NULL, NULL, &tdata);
+ if (status == PJ_SUCCESS)
+ pjsip_inv_send_msg(sip_agent->inv, tdata);
+ else
+ pjsip_inv_terminate(sip_agent->inv, 500, PJ_FALSE);
+ return -1;
+ }
+
+ /* Send the 200 response. */
+ status = pjsip_inv_send_msg(sip_agent->inv, tdata);
+ PJ_ASSERT_ON_FAIL(status == PJ_SUCCESS, return -1);
+
+ sip_agent_alloc_stream(sip_agent);
+
+ return 0;
+}
+
+static int sip_agent_get_endpoint(struct agent *agent, struct session *session)
+{
+ struct sip_agent *sip_agent = agent_to_sip_agent(agent);
+ return tapi_stream_get_endpoint(sip_agent->stream);
+}
+
+static const struct agent_ops sip_agent_ops = {
+ .invite = sip_agent_invite,
+ .accept = sip_agent_accept,
+ .hangup = sip_agent_hangup,
+ .get_endpoint = sip_agent_get_endpoint,
+};
+
+struct sip_agent *sip_client_alloc_agent(struct sip_client *client,
+ const char *identifier)
+{
+ struct sip_agent *agent;
+
+ agent = malloc(sizeof(*agent));
+ memset(agent, 0, sizeof(*agent));
+
+ agent->agent.ops = &sip_agent_ops;
+ agent->identifier = identifier;
+ agent->client = client;
+
+ return agent;
+}
+
+/*
+ * Receive incoming call
+ */
+static void process_incoming_call(struct sip_client *client, pjsip_rx_data *rdata)
+{
+ unsigned options;
+ struct sip_agent *agent;
+ pjsip_tx_data *tdata;
+ pj_status_t status;
+ pjsip_dialog *dlg;
+ pjmedia_sdp_session *sdp;
+
+ agent = sip_client_alloc_agent(client, "extern");
+
+ /* Verify that we can handle the request. */
+ options = 0;
+ status = pjsip_inv_verify_request(rdata, &options, NULL, NULL,
+ client->sip_endpt, &tdata);
+ if (status != PJ_SUCCESS) {
+ /*
+ * No we can't handle the incoming INVITE request.
+ */
+ if (tdata) {
+ pjsip_response_addr res_addr;
+ pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
+ pjsip_endpt_send_response(client->sip_endpt, &res_addr, tdata,
+ NULL, NULL);
+ } else {
+ /* Respond with 500 (Internal Server Error) */
+ pjsip_endpt_respond_stateless(client->sip_endpt, rdata, 500, NULL,
+ NULL, NULL);
+ }
+
+ return;
+ }
+
+ /* Create UAS dialog */
+ status = pjsip_dlg_create_uas(pjsip_ua_instance(), rdata,
+ &client->local_contact, &dlg);
+ if (status != PJ_SUCCESS) {
+ const pj_str_t reason = pj_str("Unable to create dialog");
+ pjsip_endpt_respond_stateless(client->sip_endpt, rdata,
+ 500, &reason,
+ NULL, NULL);
+ return;
+ }
+
+ /* Create SDP */
+ sip_client_create_sdp(client, dlg->pool, agent, &sdp);
+
+ /* Create UAS invite session */
+ status = pjsip_inv_create_uas(dlg, rdata, sdp, 0, &agent->inv);
+ if (status != PJ_SUCCESS) {
+ pjsip_dlg_create_response(dlg, rdata, 500, NULL, &tdata);
+ pjsip_dlg_send_response(dlg, pjsip_rdata_get_tsx(rdata), tdata);
+ return;
+ }
+
+ /* Attach call data to invite session */
+ agent->inv->mod_data[mod_siprtp.id] = agent;
+
+ /* Create 180 response .*/
+ status = pjsip_inv_initial_answer(agent->inv, rdata, 180,
+ NULL, NULL, &tdata);
+ if (status != PJ_SUCCESS) {
+ status = pjsip_inv_initial_answer(agent->inv, rdata,
+ PJSIP_SC_NOT_ACCEPTABLE,
+ NULL, NULL, &tdata);
+ if (status == PJ_SUCCESS)
+ pjsip_inv_send_msg(agent->inv, tdata);
+ else
+ pjsip_inv_terminate(agent->inv, 500, PJ_FALSE);
+ return;
+ }
+
+ /* Send the 180 response. */
+ status = pjsip_inv_send_msg(agent->inv, tdata);
+ PJ_ASSERT_ON_FAIL(status == PJ_SUCCESS, return);
+
+ if (client->incoming_call_cb)
+ client->incoming_call_cb(client, agent);
+}
+
+/* Callback to be called to handle incoming requests outside dialogs: */
+static pj_bool_t on_rx_request(pjsip_rx_data *rdata)
+{
+ struct sip_client *client = global_client;
+
+ /* Ignore strandled ACKs (must not send respone */
+ if (rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD)
+ return PJ_FALSE;
+
+ /* Respond (statelessly) any non-INVITE requests with 500 */
+ if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD) {
+ pj_str_t reason = pj_str("Unsupported Operation");
+ pjsip_endpt_respond_stateless(client->sip_endpt, rdata,
+ 500, &reason,
+ NULL, NULL);
+ return PJ_TRUE;
+ }
+
+ /* Handle incoming INVITE */
+ process_incoming_call(client, rdata);
+
+ /* Done */
+ return PJ_TRUE;
+}
+
+static pjsip_module sip_client_mod = {
+ NULL, NULL, /* prev, next. */
+ { "mod-tapisip", 13 }, /* Name. */
+ -1, /* Id */
+ PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
+ NULL, /* load() */
+ NULL, /* start() */
+ NULL, /* stop() */
+ NULL, /* unload() */
+ &on_rx_request, /* on_rx_request() */
+ NULL, /* on_rx_response() */
+ NULL, /* on_tx_request. */
+ NULL, /* on_tx_response() */
+ NULL, /* on_tsx_state() */
+};
+
+/* Callback to be called when dialog has forked: */
+static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e)
+{
+ PJ_UNUSED_ARG(inv);
+ PJ_UNUSED_ARG(e);
+}
+
+/* Callback to be called when invite session's state has changed: */
+static void call_on_state_changed(pjsip_inv_session *inv, pjsip_event *e)
+{
+ struct sip_agent *agent = inv->mod_data[mod_siprtp.id];
+
+ printf("state changed: %d\n", inv->state);
+
+ if (!agent)
+ return;
+
+ switch (inv->state) {
+ case PJSIP_INV_STATE_DISCONNECTED:
+ printf("Disconnected\n");
+ if (agent->session)
+ session_hangup(agent->session, &agent->agent);
+ if (agent->rtp_sockfd) {
+ event_unregister(agent->rtp_sockfd);
+ close(agent->rtp_sockfd);
+ }
+ sip_agent_free_stream(agent);
+ free(agent);
+ inv->mod_data[mod_siprtp.id] = NULL;
+ break;
+ case PJSIP_INV_STATE_CONFIRMED:
+ printf("Connected: %p\n", agent->stream);
+ if (agent->stream)
+ break;
+ sip_agent_alloc_stream(agent);
+ session_accept(agent->session, &agent->agent);
+ break;
+ default:
+ break;
+ }
+}
+
+static void call_on_media_update(pjsip_inv_session *inv, pj_status_t status)
+{
+ struct sip_agent *agent;
+ pj_pool_t *pool;
+ const pjmedia_sdp_session *local_sdp, *remote_sdp;
+ char local[100];
+ char remote[100];
+ int i;
+
+ printf("media updte\n");
+
+ agent = inv->mod_data[mod_siprtp.id];
+ pool = inv->dlg->pool;
+
+ /* Do nothing if media negotiation has failed */
+ if (status != PJ_SUCCESS)
+ return;
+ /* Capture stream definition from the SDP */
+ pjmedia_sdp_neg_get_active_local(inv->neg, &local_sdp);
+ pjmedia_sdp_neg_get_active_remote(inv->neg, &remote_sdp);
+
+ strlcpy(local, local_sdp->conn->addr.ptr, local_sdp->conn->addr.slen + 1);
+ printf("local media count: %d\n", local_sdp->media_count);
+ printf("local: %s %d\n", local,
+ ntohs(local_sdp->media[0]->desc.port));
+ strlcpy(remote, remote_sdp->conn->addr.ptr, remote_sdp->conn->addr.slen + 1);
+ printf("remote media count: %d\n", remote_sdp->media_count);
+ printf("remote: %s %d\n", remote,
+ ntohs(remote_sdp->media[0]->desc.port));
+
+ agent->remote_addr.ss_family = AF_INET;
+ inet_pton(AF_INET, remote,
+ sockaddr_get_addr(&agent->remote_addr));
+ sockaddr_set_port(&agent->remote_addr, remote_sdp->media[0]->desc.port);
+
+ printf("attributes: %d\n", remote_sdp->attr_count);
+ for (i = 0; i < remote_sdp->attr_count; ++i)
+ printf("%s: %s\n", remote_sdp->attr[i]->name.ptr,
+ remote_sdp->attr[i]->value.ptr);
+
+}
+
+static int sip_client_init_sip_endpoint(struct sip_client *client)
+{
+ pj_status_t status;
+ pjsip_host_port addrname;
+ pjsip_inv_callback inv_cb;
+ pjsip_transport *tp;
+ char public_addr[INET6_ADDRSTRLEN];
+
+ global_client = client;
+
+ pj_caching_pool_init(&client->cp, &pj_pool_factory_default_policy, 0);
+ client->pool = pj_pool_create(&client->cp.factory, "tapi sip", 1000, 1000, NULL);
+
+ status = pjsip_endpt_create(&client->cp.factory, NULL,
+ &client->sip_endpt);
+
+ client->sockfd = sip_client_create_socket(client, &client->public_addr, 5060, 5100);
+ if (client->stun)
+ stun_client_resolve(client->stun, client->sockfd, (struct sockaddr *)&client->public_addr);
+
+ sockaddr_to_string(&client->public_addr, public_addr, sizeof(public_addr));
+
+ addrname.host = pj_str(public_addr);
+ addrname.port = sockaddr_get_port(&client->public_addr);
+
+ pjsip_udp_transport_attach(client->sip_endpt, client->sockfd, &addrname, 1, &tp);
+
+ status = pjsip_tsx_layer_init_module(client->sip_endpt);
+ PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
+
+ status = pjsip_ua_init_module(client->sip_endpt, NULL);
+ PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
+
+ status = pjsip_100rel_init_module(client->sip_endpt);
+ PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
+
+ pj_bzero(&inv_cb, sizeof(inv_cb));
+ inv_cb.on_state_changed = &call_on_state_changed;
+ inv_cb.on_new_session = &call_on_forked;
+ inv_cb.on_media_update = &call_on_media_update;
+
+ status = pjsip_inv_usage_init(client->sip_endpt, &inv_cb);
+ PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
+
+ status = pjsip_endpt_register_module(client->sip_endpt, &sip_client_mod);
+
+ pj_thread_create(client->pool, "sip client", &sip_worker_thread, client,
+ 0, 0, &client->sip_thread);
+
+ return status;
+}
+
+static void sip_client_register_callback(struct pjsip_regc_cbparam *param)
+{
+ if (param->status != PJ_SUCCESS || param->code / 100 != 2)
+ printf("Failed to register: %d %d", param->status, param->code);
+}
+
+void sip_client_free(struct sip_client *client)
+{
+ pjsip_regc_destroy(client->regc);
+ pjsip_endpt_destroy(client->sip_endpt);
+}
+
+void sip_client_register(struct sip_client *client)
+{
+ pjsip_tx_data *tdata;
+
+ pjsip_regc_register(client->regc, true, &tdata);
+ pjsip_regc_send(client->regc, tdata);
+}
+
+void sip_client_set_cred(struct sip_client *client)
+{
+ char local_contact[100];
+ char server_uri[100];
+ char s[INET6_ADDRSTRLEN];
+ char contact_addr[INET6_ADDRSTRLEN + 10];
+ pj_str_t pj_contact_addr;
+
+ sockaddr_to_string(&client->public_addr, s, sizeof(s));
+
+ snprintf(contact_addr, sizeof(contact_addr), "sip:%s:%d", s,
+ sockaddr_get_port(&client->public_addr));
+ pj_contact_addr = pj_str(contact_addr);
+
+ client->cred.realm = pj_str((char *)client->config->host);
+ client->cred.scheme = pj_str("digest");
+ client->cred.username = pj_str((char *)client->config->username);
+ client->cred.data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
+ client->cred.data = pj_str((char *)client->config->password);
+
+ snprintf(local_contact, sizeof(local_contact), "sip:%s@%s",
+ client->config->username, client->config->host);
+ pj_strdup2_with_null(client->pool, &client->local_contact, local_contact);
+
+ snprintf(server_uri, sizeof(server_uri), "sip:%s\n", client->config->host);
+ pj_strdup2_with_null(client->pool, &client->server_uri, server_uri);
+
+ pjsip_regc_init(client->regc, &client->server_uri, &client->local_contact,
+ &client->local_contact, 1, &pj_contact_addr, 3600);
+ pjsip_regc_set_credentials(client->regc, 1, &client->cred);
+
+ sip_client_register(client);
+}
+
+void sip_client_init(struct sip_client *client, struct tapi_device *tdev,
+ const struct sip_client_config *config)
+{
+ global_client = client;
+ client->config = config;
+
+ client->tdev = tdev;
+
+ iface_get_addr(config->iface, &client->local_addr);
+
+ if (config->stun_host && config->stun_port)
+ client->stun = stun_client_alloc(config->stun_host, config->stun_port);
+ else
+ client->stun = NULL;
+
+ sip_client_init_sip_endpoint(client);
+
+ pjsip_regc_create(client->sip_endpt, client, sip_client_register_callback,
+ &client->regc);
+
+ sip_client_set_cred(client);
+}
+
+
diff --git a/package/tapi_sip/src/sip_client.h b/package/tapi_sip/src/sip_client.h
new file mode 100644
index 000000000..1c494b6dc
--- /dev/null
+++ b/package/tapi_sip/src/sip_client.h
@@ -0,0 +1,61 @@
+#ifndef __SIP_CLIENT_H__
+#define __SIP_CLIENT_H__
+
+#include <tapi-stream.h>
+#include <tapi-device.h>
+
+#include <pjsip.h>
+#include <pjsip_ua.h>
+#include <pjsip_simple.h>
+#include <pjlib-util.h>
+#include <pjlib.h>
+
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <stdint.h>
+
+struct stun_client;
+struct sip_agent;
+
+struct sip_client_config {
+ const char *iface;
+
+ const char *host;
+ uint16_t port;
+ const char *username;
+ const char *password;
+
+ const char *stun_host;
+ uint16_t stun_port;
+};
+
+struct sip_client {
+ const struct sip_client_config *config;
+
+ struct tapi_device *tdev;
+ struct stun_client *stun;
+
+ struct sockaddr_storage public_addr;
+ struct sockaddr_storage local_addr;
+
+ int sockfd;
+
+ pj_thread_t *sip_thread;
+ pj_caching_pool cp;
+ pj_pool_t *pool;
+ pjsip_endpoint *sip_endpt;
+ pjsip_cred_info cred;
+ pj_str_t local_contact;
+ pj_str_t server_uri;
+
+ pjsip_regc *regc;
+
+ int (*incoming_call_cb)(struct sip_client *client, struct sip_agent *agent);
+};
+
+void sip_client_init(struct sip_client *client, struct tapi_device *dev,
+ const struct sip_client_config *config);
+
+struct sip_agent *sip_client_alloc_agent(struct sip_client *client, const char *dst_uri);
+
+#endif
diff --git a/package/tapi_sip/src/stun.c b/package/tapi_sip/src/stun.c
new file mode 100644
index 000000000..5c6b24067
--- /dev/null
+++ b/package/tapi_sip/src/stun.c
@@ -0,0 +1,243 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+
+#include <poll.h>
+
+
+struct stun_client {
+ struct addrinfo *serverinfo;
+};
+
+struct stun_response {
+ struct sockaddr addr;
+};
+
+struct stun_header {
+ uint16_t type;
+ uint16_t length;
+ uint32_t cookie;
+ uint32_t id[3];
+} __attribute((packed));
+
+struct stun_packet {
+ struct stun_header header;
+ uint8_t data[0];
+} __attribute((packed));
+
+#define STUN_CLASS(c0, c1) (((c0) << 4) | ((c1) << 8))
+
+#define STUN_CLASS_REQUEST STUN_CLASS(0, 0)
+#define STUN_CLASS_INDICATION STUN_CLASS(0, 1)
+#define STUN_CLASS_SUCCESS STUN_CLASS(1, 0)
+#define STUN_CLASS_ERROR STUN_CLASS(1, 1)
+
+#define STUN_CLASS_MASK STUN_CLASS(1, 1)
+
+#define STUN_MESSAGE(msg) (((msg & 0xf10) << 2) | ((msg & 0x70) << 1) | (msg & 0xf))
+#define STUN_MESSAGE_BIND STUN_MESSAGE(1)
+
+#define STUN_COOKIE 0x2112a442
+
+enum {
+ STUN_ATTR_TYPE_MAPPED_ADDRESS = 0x1,
+ STUN_ATTR_TYPE_XOR_MAPPED_ADDRESS = 0x20,
+ STUN_ATTR_TYPE_XOR_MAPPED_ADDRESS2 = 0x8020,
+};
+
+static inline uint16_t get_unaligned_be16(const uint8_t *buf)
+{
+ return (buf[0] << 8) | buf[1];
+}
+
+static inline uint16_t get_unaligned_be32(const uint8_t *buf)
+{
+ return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
+}
+
+static int stun_parse_xor_mapped_address(struct stun_response *response,
+ const uint8_t *buf, int length)
+{
+ uint8_t fam = buf[1];
+ uint16_t port = get_unaligned_be16(&buf[2]);
+ struct sockaddr_in *sin = (struct sockaddr_in *)&response->addr;
+ struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&response->addr;
+
+
+ switch (fam) {
+ case 0x1:
+ sin->sin_family = AF_INET;
+ sin->sin_port = htons((port ^ (uint16_t)((STUN_COOKIE & 0xffff0000) >> 16)));
+ memcpy(&sin->sin_addr.s_addr, buf + 4, 4);
+ sin->sin_addr.s_addr ^= htonl(STUN_COOKIE);
+ printf("xor port: %d\n", sin->sin_port);
+ break;
+ case 0x2:
+ sin6->sin6_family = AF_INET6;
+ sin->sin_port = htons((port ^ (uint16_t)((STUN_COOKIE & 0xffff0000) >> 16)));
+ memcpy(sin6->sin6_addr.s6_addr, buf + 4, 16);
+ break;
+ }
+
+ return 0;
+}
+
+static int stun_parse_mapped_address(struct stun_response *response,
+ const uint8_t *buf, int length)
+{
+ uint8_t fam = buf[1];
+ uint16_t port = get_unaligned_be16(&buf[2]);
+ struct sockaddr_in *sin = (struct sockaddr_in *)&response->addr;
+ struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&response->addr;
+
+ printf("port: %d\n", port);
+
+ switch (fam) {
+ case 0x1:
+ sin->sin_family = AF_INET;
+ sin->sin_port = htons(port);
+ memcpy(&sin->sin_addr.s_addr, buf + 4, 4);
+ break;
+ case 0x2:
+ sin6->sin6_family = AF_INET6;
+ sin6->sin6_port = htons(port);
+ memcpy(sin6->sin6_addr.s6_addr, buf + 4, 16);
+ break;
+ }
+
+ return 0;
+}
+
+static int stun_parse_response(struct stun_response *response,
+ const struct stun_packet *packet)
+{
+ uint16_t attr_type, attr_length;
+ const uint8_t *buf;
+ int length = ntohs(packet->header.length);
+ int ret;
+ int i = 0;
+
+ if (packet->header.cookie != htonl(STUN_COOKIE))
+ return -1;
+
+ if (packet->header.length < 4)
+ return 0;
+
+ buf = packet->data;
+
+ do {
+ attr_type = get_unaligned_be16(&buf[i]);
+ attr_length = get_unaligned_be16(&buf[i + 2]);
+ i += 4;
+
+ if (i + attr_length > length)
+ break;
+
+ switch (attr_type) {
+ case STUN_ATTR_TYPE_MAPPED_ADDRESS:
+ ret = stun_parse_mapped_address(response, &buf[i], attr_length);
+ break;
+ case STUN_ATTR_TYPE_XOR_MAPPED_ADDRESS:
+ case STUN_ATTR_TYPE_XOR_MAPPED_ADDRESS2:
+ ret = stun_parse_xor_mapped_address(response, &buf[i], attr_length);
+ break;
+ }
+
+ i += attr_length;
+
+ } while (i < length && ret == 0);
+
+ return 0;
+}
+
+static struct stun_packet *stun_packet_alloc(size_t data_size)
+{
+ return malloc(sizeof(struct stun_packet) + data_size);
+}
+
+int stun_client_resolve(struct stun_client *stun, int sockfd, struct sockaddr *addr)
+{
+ struct stun_packet *packet = stun_packet_alloc(200);
+ struct stun_response response;
+ int ret;
+ int retries = 4;
+ int timeout = 500;
+ struct pollfd pollfd;
+
+ pollfd.events = POLLIN;
+ pollfd.fd = sockfd;
+
+ packet->header.type = htons(STUN_CLASS_REQUEST | STUN_MESSAGE_BIND);
+ packet->header.cookie = htonl(STUN_COOKIE);
+ packet->header.id[0] = 0x12345678;
+ packet->header.id[1] = 0x12345678;
+ packet->header.id[2] = 0x12345678;
+ packet->header.length = 0;
+
+ while (retries--) {
+ ret = sendto(sockfd, packet, sizeof(struct stun_header) + packet->header.length,
+ 0, stun->serverinfo->ai_addr, stun->serverinfo->ai_addrlen);
+
+ ret = poll(&pollfd, 1, timeout);
+ switch (ret) {
+ case 0:
+ timeout <<= 1;
+ case -EINTR:
+ printf("retry\n");
+ continue;
+ default:
+ retries = 0;
+ }
+ ret = recvfrom(sockfd, packet, 200, 0, NULL, NULL);
+ }
+
+ if (ret <= 0)
+ return ret ? ret : -ETIMEDOUT;
+
+ memset(&response, 0, sizeof(response));
+ ret = stun_parse_response(&response, packet);
+
+ *addr = response.addr;
+
+ return ret;
+}
+
+struct stun_client *stun_client_alloc(const char *hostname, uint16_t port)
+{
+ struct addrinfo hints;
+ struct stun_client *stun;
+ int ret;
+ char p[6];
+
+
+ stun = malloc(sizeof(*stun));
+ if (!stun)
+ return NULL;
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_DGRAM;
+ hints.ai_flags = AI_NUMERICSERV;
+
+ snprintf(p, sizeof(p), "%d", port);
+ if ((ret = getaddrinfo(hostname, p, &hints, &stun->serverinfo)) != 0) {
+ fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
+ return NULL;
+ }
+
+
+ return stun;
+}
+
+void stun_client_free(struct stun_client *stun)
+{
+ freeaddrinfo(stun->serverinfo);
+ free(stun);
+}
diff --git a/package/tapi_sip/src/stun.h b/package/tapi_sip/src/stun.h
new file mode 100644
index 000000000..2cd61c9f1
--- /dev/null
+++ b/package/tapi_sip/src/stun.h
@@ -0,0 +1,12 @@
+#ifndef __STUN_H__
+#define __STUN_H__
+
+#include <stdint.h>
+
+struct stun_client;
+
+struct stun_client *stun_client_alloc(const char *hostname, uint16_t port);
+void stun_client_free(struct stun_client *);
+int stun_client_resolve(struct stun_client *stun, int sockfd, struct sockaddr *addr);
+
+#endif
diff --git a/package/tapi_sip/src/tapi_agent.c b/package/tapi_sip/src/tapi_agent.c
new file mode 100644
index 000000000..93c73c24d
--- /dev/null
+++ b/package/tapi_sip/src/tapi_agent.c
@@ -0,0 +1,105 @@
+
+#include <stdbool.h>
+#include <stdio.h>
+#include <tapi-port.h>
+
+#include "session.h"
+#include "agent.h"
+#include "tapi_agent.h"
+
+static int tapi_agent_invite(struct agent *agent, struct session *session)
+{
+ struct tapi_agent *tagent = agent_to_tapi_agent(agent);
+
+ if (tagent->session)
+ return -1;
+
+ tagent->state = TAPI_AGENT_STATE_RINGING;
+ tapi_port_set_ring(&tagent->port, true);
+
+ tagent->session = session;
+
+ return 0;
+}
+
+static int tapi_agent_accept(struct agent *agent, struct session *session)
+{
+ return 0;
+}
+
+static int tapi_agent_hangup(struct agent *agent, struct session *session)
+{
+ struct tapi_agent *tagent = agent_to_tapi_agent(agent);
+
+ switch (tagent->state) {
+ case TAPI_AGENT_STATE_RINGING:
+ tapi_port_set_ring(&tagent->port, false);
+ break;
+ default:
+ break;
+ }
+
+ tagent->state = TAPI_AGENT_STATE_IDLE;
+ tagent->session = NULL;
+
+ return 0;
+}
+
+static int tapi_agent_get_endpoint(struct agent *agent, struct session *session)
+{
+ struct tapi_agent *tagent = agent_to_tapi_agent(agent);
+ return tapi_port_get_endpoint(&tagent->port);
+}
+
+static const struct agent_ops tapi_agent_ops = {
+ .invite = tapi_agent_invite,
+ .accept = tapi_agent_accept,
+ .hangup = tapi_agent_hangup,
+ .get_endpoint = tapi_agent_get_endpoint,
+};
+
+static void tapi_agent_event(struct tapi_port *port, struct tapi_event *event,
+ void *data)
+{
+ struct tapi_agent *tagent = data;
+
+ if (event->type != TAPI_EVENT_TYPE_HOOK)
+ return;
+
+ if (!tagent->session)
+ return;
+
+ if (event->hook.on) {
+ session_hangup(tagent->session, &tagent->agent);
+ tagent->state = TAPI_AGENT_STATE_IDLE;
+ tagent->session = NULL;
+ } else {
+ session_accept(tagent->session, &tagent->agent);
+ tagent->state = TAPI_AGENT_STATE_ACTIVE;
+ }
+}
+
+void tapi_agent_init(struct tapi_device *tdev, int port, struct tapi_agent *tagent)
+{
+ int ret;
+
+ tagent->agent.ops = &tapi_agent_ops;
+ tagent->state = TAPI_AGENT_STATE_IDLE;
+ tagent->session = NULL;
+
+ ret = tapi_port_open(tdev, port, &tagent->port);
+ if (ret) {
+ printf("Failed to open tapi port %d: %d\n", port, ret);
+ return;
+ }
+
+ tagent->event_listener.callback = tapi_agent_event;
+ tagent->event_listener.data = tagent;
+
+ tapi_port_register_event(&tagent->port, &tagent->event_listener);
+}
+
+void tapi_agent_free(struct tapi_agent *tagent)
+{
+ tapi_port_unregister_event(&tagent->port, &tagent->event_listener);
+}
diff --git a/package/tapi_sip/src/tapi_agent.h b/package/tapi_sip/src/tapi_agent.h
new file mode 100644
index 000000000..12e870121
--- /dev/null
+++ b/package/tapi_sip/src/tapi_agent.h
@@ -0,0 +1,37 @@
+#ifndef __TAPI_AGENT_H__
+#define __TAPI_AGENT_H__
+
+#include "agent.h"
+#include <tapi-port.h>
+
+struct session;
+
+enum tapi_agent_state {
+ TAPI_AGENT_STATE_IDLE,
+ TAPI_AGENT_STATE_RINGING,
+ TAPI_AGENT_STATE_ACTIVE,
+};
+
+struct tapi_agent {
+ struct agent agent;
+ struct tapi_port port;
+ struct tapi_port_event_listener event_listener;
+
+ enum tapi_agent_state state;
+
+ struct session *session;
+};
+
+static inline struct tapi_agent *agent_to_tapi_agent(struct agent *agent)
+{
+ return container_of(agent, struct tapi_agent, agent);
+}
+
+static inline struct tapi_agent *port_to_tapi_agent(struct tapi_port *port)
+{
+ return container_of(port, struct tapi_agent, port);
+}
+
+void tapi_agent_init(struct tapi_device *tdev, int port, struct tapi_agent *tagent);
+
+#endif
diff --git a/package/tapi_sip/src/tapi_sip.c b/package/tapi_sip/src/tapi_sip.c
new file mode 100644
index 000000000..518c63c3d
--- /dev/null
+++ b/package/tapi_sip/src/tapi_sip.c
@@ -0,0 +1,179 @@
+
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <poll.h>
+#include <string.h>
+
+#include <linux/input.h>
+#include "dialdetector.h"
+
+#include <tapi-ioctl.h>
+
+#include <tapi-device.h>
+#include <tapi-port.h>
+
+#include "contact.h"
+#include "session.h"
+#include "sip_client.h"
+#include "sip_agent.h"
+#include "tapi_agent.h"
+
+static struct tapi_device dev;
+static struct tapi_agent *ports;
+
+static struct sip_client sip_client;
+
+static void release_session(struct session *session)
+{
+ free(session);
+}
+
+static void dial(struct tapi_agent *caller, struct agent *callee)
+{
+ struct session *session;
+
+ session = session_alloc(&dev, &caller->agent, callee, release_session);
+
+ if (!session)
+ return;
+
+ caller->session = session;
+}
+
+static void tel_dial(struct tapi_agent *caller, const char *number)
+{
+ int callee;
+
+ callee = atoi(number) - 1;
+
+ if (callee < 0 || callee > 1)
+ return;
+ dial(caller, &ports[callee].agent);
+}
+
+static void sip_dial(struct tapi_agent *caller, const char *identifier)
+{
+ struct sip_agent *callee;
+
+ callee = sip_client_alloc_agent(&sip_client, identifier);
+ if (!callee)
+ return;
+
+ dial(caller, &callee->agent);
+}
+
+static void dial_callback(struct tapi_port *port, size_t num_digits, const unsigned char *digits)
+{
+ struct tapi_agent *tagent = port_to_tapi_agent(port);
+ char number[100];
+ struct contact *contact;
+ size_t i;
+
+ if (tagent->state != TAPI_AGENT_STATE_IDLE)
+ return;
+
+ for (i = 0; i < num_digits; ++i) {
+ if (digits[0] > 9)
+ break;
+ number[i] = digits[i] + '0';
+ }
+ number[i] = '\0';
+
+ printf("dial callback: %s\n", number);
+
+ contact = contact_get(number);
+
+ if (!contact)
+ return;
+
+ if (strncmp("tel:", contact->identifier, 4) == 0) {
+ tel_dial(tagent, contact->identifier + 4);
+ } else if (strncmp("sip:", contact->identifier, 4) == 0) {
+ sip_dial(tagent, contact->identifier);
+ }
+ tagent->state = TAPI_AGENT_STATE_ACTIVE;
+}
+
+static int incoming_sip_call(struct sip_client *client,
+ struct sip_agent *caller)
+{
+ struct tapi_agent *callee = NULL;;
+ struct session *session;
+ int i;
+
+ for (i = 0; i < 2; ++i) {
+ if (ports[i].state == TAPI_AGENT_STATE_IDLE) {
+ callee = &ports[i];
+ break;
+ }
+ }
+
+ if (callee == NULL)
+ return -1;
+
+ session = session_alloc(&dev, &caller->agent, &callee->agent,
+ release_session);
+ caller->session = session;
+
+ return 0;
+}
+
+int main(int argc, char *argv[])
+{
+ struct dialdetector *dd, *dd2;
+ struct account *account;
+ struct sip_client_config config;
+ const char *interface = "eth0";
+ int ret;
+ int i;
+
+ if (argc > 1)
+ interface = argv[1];
+
+ pj_init();
+ pjlib_util_init();
+
+ contacts_init();
+
+ account = get_account();
+ if (!account) {
+ printf("No account\n");
+ return 1;
+ }
+
+ ret = tapi_device_open(0, &dev);
+ if (ret) {
+ printf("Failed to open tapi device: %d\n", ret);
+ return 1;
+ }
+
+ ports = calloc(dev.num_ports, sizeof(*ports));
+ for (i = 0; i < dev.num_ports; ++i)
+ tapi_agent_init(&dev, i, &ports[i]);
+
+ dd = dialdetector_alloc(&ports[0].port);
+ dd->dial_callback = dial_callback;
+ dd2 = dialdetector_alloc(&ports[1].port);
+ dd2->dial_callback = dial_callback;
+
+ config.iface = interface;
+ config.host = account->realm;
+ config.port = account->sip_port;
+ config.username = account->username;
+ config.password = account->password;
+
+ config.stun_host = account->stun_host;
+ config.stun_port = account->stun_port;
+
+ sip_client_init(&sip_client, &dev, &config);
+
+ sip_client.incoming_call_cb = incoming_sip_call;
+
+ tapi_mainloop();
+
+ return 0;
+}