2 * This file is part of OMAP DSP driver (DSP Gateway version 3.3.1)
4 * Copyright (C) 2002-2006 Nokia Corporation. All rights reserved.
6 * Contact: Toshihiro Kobayashi <toshihiro.kobayashi@nokia.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/major.h>
29 #include <linux/poll.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
32 #include <linux/sched.h>
34 #include <linux/mutex.h>
35 #include <linux/interrupt.h>
36 #include <linux/kfifo.h>
37 #include <asm/uaccess.h>
39 #include <mach/mailbox.h>
41 #include "uaccess_dsp.h"
42 #include "dsp_mbcmd.h"
48 * devstate: task device state machine
49 * NOTASK: task is not attached.
50 * ATTACHED: task is attached.
51 * GARBAGE: task is detached. waiting for all processes to close this device.
52 * ADDREQ: requesting for tadd
53 * DELREQ: requesting for tdel. no process is opening this device.
54 * FREEZED: task is attached, but reserved to be killed.
55 * ADDFAIL: tadd failed.
56 * ADDING: tadd in process.
57 * DELING: tdel in process.
58 * KILLING: tkill in process.
60 #define TASKDEV_ST_NOTASK 0x00000001
61 #define TASKDEV_ST_ATTACHED 0x00000002
62 #define TASKDEV_ST_GARBAGE 0x00000004
63 #define TASKDEV_ST_INVALID 0x00000008
64 #define TASKDEV_ST_ADDREQ 0x00000100
65 #define TASKDEV_ST_DELREQ 0x00000200
66 #define TASKDEV_ST_FREEZED 0x00000400
67 #define TASKDEV_ST_ADDFAIL 0x00001000
68 #define TASKDEV_ST_ADDING 0x00010000
69 #define TASKDEV_ST_DELING 0x00020000
70 #define TASKDEV_ST_KILLING 0x00040000
71 #define TASKDEV_ST_STATE_MASK 0x7fffffff
72 #define TASKDEV_ST_STALE 0x80000000
78 { TASKDEV_ST_NOTASK, "notask" },
79 { TASKDEV_ST_ATTACHED, "attached" },
80 { TASKDEV_ST_GARBAGE, "garbage" },
81 { TASKDEV_ST_INVALID, "invalid" },
82 { TASKDEV_ST_ADDREQ, "addreq" },
83 { TASKDEV_ST_DELREQ, "delreq" },
84 { TASKDEV_ST_FREEZED, "freezed" },
85 { TASKDEV_ST_ADDFAIL, "addfail" },
86 { TASKDEV_ST_ADDING, "adding" },
87 { TASKDEV_ST_DELING, "deling" },
88 { TASKDEV_ST_KILLING, "killing" },
91 static char *devstate_name(long state)
94 int max = ARRAY_SIZE(devstate_desc);
96 for (i = 0; i < max; i++) {
97 if (state & devstate_desc[i].state)
98 return devstate_desc[i].name;
103 struct rcvdt_bk_struct {
109 struct bus_type *bus;
110 struct device dev; /* Generic device interface */
113 struct rw_semaphore state_sem;
114 wait_queue_head_t state_wait_q;
115 struct mutex usecount_lock;
116 unsigned int usecount;
118 struct file_operations fops;
119 spinlock_t proc_list_lock;
120 struct list_head proc_list;
121 struct dsptask *task;
124 wait_queue_head_t read_wait_q;
125 struct mutex read_mutex;
126 spinlock_t read_lock;
128 struct kfifo *fifo; /* for active word */
129 struct rcvdt_bk_struct bk;
133 wait_queue_head_t write_wait_q;
134 struct mutex write_mutex;
139 wait_queue_head_t tctl_wait_q;
140 struct mutex tctl_mutex;
142 int tctl_ret; /* return value for tctl_show() */
149 #define to_taskdev(n) container_of(n, struct taskdev, dev)
163 struct ipbuf_p *ipbuf_pvt_r;
166 struct ipbuf_p *ipbuf_pvt_w;
173 #define sndtyp_acv(ttyp) ((ttyp) & TTYP_ASND)
174 #define sndtyp_psv(ttyp) (!((ttyp) & TTYP_ASND))
175 #define sndtyp_bk(ttyp) ((ttyp) & TTYP_BKDM)
176 #define sndtyp_wd(ttyp) (!((ttyp) & TTYP_BKDM))
177 #define sndtyp_pvt(ttyp) ((ttyp) & TTYP_PVDM)
178 #define sndtyp_gbl(ttyp) (!((ttyp) & TTYP_PVDM))
179 #define rcvtyp_acv(ttyp) ((ttyp) & TTYP_ARCV)
180 #define rcvtyp_psv(ttyp) (!((ttyp) & TTYP_ARCV))
181 #define rcvtyp_bk(ttyp) ((ttyp) & TTYP_BKMD)
182 #define rcvtyp_wd(ttyp) (!((ttyp) & TTYP_BKMD))
183 #define rcvtyp_pvt(ttyp) ((ttyp) & TTYP_PVMD)
184 #define rcvtyp_gbl(ttyp) (!((ttyp) & TTYP_PVMD))
186 static inline int has_taskdev_lock(struct taskdev *dev);
187 static int dsp_rmdev_minor(unsigned char minor);
188 static int taskdev_init(struct taskdev *dev, char *name, unsigned char minor);
189 static void taskdev_delete(unsigned char minor);
190 static int taskdev_attach_task(struct taskdev *dev, struct dsptask *task);
191 static int dsp_tdel_bh(struct taskdev *dev, u16 type);
193 static struct bus_type dsptask_bus = {
197 static struct class *dsp_task_class;
198 static DEFINE_MUTEX(devmgr_lock);
199 static struct taskdev *taskdev[TASKDEV_MAX];
200 static struct dsptask *dsptask[TASKDEV_MAX];
201 static DEFINE_MUTEX(cfg_lock);
204 static DECLARE_WAIT_QUEUE_HEAD(cfg_wait_q);
205 static u8 n_task; /* static task count */
208 #define is_dynamic_task(tid) ((tid) >= n_task)
210 #define devstate_read_lock(dev, devstate) \
211 devstate_read_lock_timeout(dev, devstate, 0)
212 #define devstate_read_unlock(dev) up_read(&(dev)->state_sem)
213 #define devstate_write_lock(dev, devstate) \
214 devstate_write_lock_timeout(dev, devstate, 0)
215 #define devstate_write_unlock(dev) up_write(&(dev)->state_sem)
217 static ssize_t devname_show(struct device *d, struct device_attribute *attr,
219 static ssize_t devstate_show(struct device *d, struct device_attribute *attr,
221 static ssize_t proc_list_show(struct device *d, struct device_attribute *attr,
223 static ssize_t taskname_show(struct device *d, struct device_attribute *attr,
225 static ssize_t ttyp_show(struct device *d, struct device_attribute *attr,
227 static ssize_t fifosz_show(struct device *d, struct device_attribute *attr,
229 static int fifosz_store(struct device *d, struct device_attribute *attr,
230 const char *buf, size_t count);
231 static ssize_t fifocnt_show(struct device *d, struct device_attribute *attr,
233 static ssize_t ipblink_show(struct device *d, struct device_attribute *attr,
235 static ssize_t wsz_show(struct device *d, struct device_attribute *attr,
237 static ssize_t mmap_show(struct device *d, struct device_attribute *attr,
240 #define __ATTR_RW(_name,_mode) { \
241 .attr = {.name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE }, \
242 .show = _name##_show, \
243 .store = _name##_store, \
246 static struct device_attribute dev_attr_devname = __ATTR_RO(devname);
247 static struct device_attribute dev_attr_devstate = __ATTR_RO(devstate);
248 static struct device_attribute dev_attr_proc_list = __ATTR_RO(proc_list);
249 static struct device_attribute dev_attr_taskname = __ATTR_RO(taskname);
250 static struct device_attribute dev_attr_ttyp = __ATTR_RO(ttyp);
251 static struct device_attribute dev_attr_fifosz = __ATTR_RW(fifosz, 0666);
252 static struct device_attribute dev_attr_fifocnt = __ATTR_RO(fifocnt);
253 static struct device_attribute dev_attr_ipblink = __ATTR_RO(ipblink);
254 static struct device_attribute dev_attr_wsz = __ATTR_RO(wsz);
255 static struct device_attribute dev_attr_mmap = __ATTR_RO(mmap);
257 static inline void set_taskdev_state(struct taskdev *dev, int state)
259 pr_debug("omapdsp: devstate: CHANGE %s[%d]:\"%s\"->\"%s\"\n",
261 (dev->task ? dev->task->tid : -1),
262 devstate_name(dev->state),
263 devstate_name(state));
268 * devstate_read_lock_timeout()
269 * devstate_write_lock_timeout():
270 * timeout != 0: dev->state can be diffeent from what you want.
271 * timeout == 0: no timeout
273 #define BUILD_DEVSTATE_LOCK_TIMEOUT(rw) \
274 static int devstate_##rw##_lock_timeout(struct taskdev *dev, long devstate, \
278 down_##rw(&dev->state_sem); \
279 while (!(dev->state & devstate)) { \
280 up_##rw(&dev->state_sem); \
281 prepare_to_wait(&dev->state_wait_q, &wait, TASK_INTERRUPTIBLE); \
283 timeout = MAX_SCHEDULE_TIMEOUT; \
284 timeout = schedule_timeout(timeout); \
285 finish_wait(&dev->state_wait_q, &wait); \
288 if (signal_pending(current)) \
290 down_##rw(&dev->state_sem); \
294 BUILD_DEVSTATE_LOCK_TIMEOUT(read)
295 BUILD_DEVSTATE_LOCK_TIMEOUT(write)
297 #define BUILD_DEVSTATE_LOCK_AND_TEST(rw) \
298 static int devstate_##rw##_lock_and_test(struct taskdev *dev, long devstate) \
300 down_##rw(&dev->state_sem); \
301 if (dev->state & devstate) \
302 return 1; /* success */ \
304 up_##rw(&dev->state_sem); \
307 BUILD_DEVSTATE_LOCK_AND_TEST(read)
308 BUILD_DEVSTATE_LOCK_AND_TEST(write)
310 static int taskdev_lock_interruptible(struct taskdev *dev,
315 if (has_taskdev_lock(dev))
316 ret = mutex_lock_interruptible(lock);
318 if ((ret = mutex_lock_interruptible(&dev->lock)) != 0)
320 ret = mutex_lock_interruptible(lock);
321 mutex_unlock(&dev->lock);
327 static int taskdev_lock_and_statelock_attached(struct taskdev *dev,
332 if (!devstate_read_lock_and_test(dev, TASKDEV_ST_ATTACHED))
335 if ((ret = taskdev_lock_interruptible(dev, lock)) != 0)
336 devstate_read_unlock(dev);
341 static inline void taskdev_unlock_and_stateunlock(struct taskdev *dev,
345 devstate_read_unlock(dev);
349 * taskdev_flush_buf()
350 * must be called under state_lock(ATTACHED) and dev->read_mutex.
352 static int taskdev_flush_buf(struct taskdev *dev)
354 u16 ttyp = dev->task->ttyp;
356 if (sndtyp_wd(ttyp)) {
358 kfifo_reset(dev->rcvdt.fifo);
360 /* block receiving */
361 struct rcvdt_bk_struct *rcvdt = &dev->rcvdt.bk;
363 if (sndtyp_gbl(ttyp))
364 ipblink_flush(&rcvdt->link);
366 ipblink_flush_pvt(&rcvdt->link);
367 release_ipbuf_pvt(dev->task->ipbuf_pvt_r);
375 * taskdev_set_fifosz()
376 * must be called under dev->read_mutex.
378 static int taskdev_set_fifosz(struct taskdev *dev, unsigned long sz)
380 u16 ttyp = dev->task->ttyp;
382 if (!(sndtyp_wd(ttyp) && sndtyp_acv(ttyp))) {
384 "omapdsp: buffer size can be changed only for "
385 "active word sending task.\n");
388 if ((sz == 0) || (sz & 1)) {
389 printk(KERN_ERR "omapdsp: illegal buffer size! (%ld)\n"
390 "it must be even and non-zero value.\n", sz);
394 if (kfifo_len(dev->rcvdt.fifo)) {
395 printk(KERN_ERR "omapdsp: buffer is not empty!\n");
399 kfifo_free(dev->rcvdt.fifo);
400 dev->rcvdt.fifo = kfifo_alloc(sz, GFP_KERNEL, &dev->read_lock);
401 if (IS_ERR(dev->rcvdt.fifo)) {
403 "omapdsp: unable to change receive buffer size. "
404 "(%ld bytes for %s)\n", sz, dev->name);
411 static inline int has_taskdev_lock(struct taskdev *dev)
413 return (dev->lock_pid == current->pid);
416 static int taskdev_lock(struct taskdev *dev)
418 if (mutex_lock_interruptible(&dev->lock))
420 dev->lock_pid = current->pid;
424 static int taskdev_unlock(struct taskdev *dev)
426 if (!has_taskdev_lock(dev)) {
428 "omapdsp: an illegal process attempted to "
429 "unlock the dsptask lock!\n");
433 mutex_unlock(&dev->lock);
437 static int dsp_task_config(struct dsptask *task, u8 tid)
446 task->state = TASK_ST_CFGREQ;
447 if (mutex_lock_interruptible(&cfg_lock)) {
451 cfg_cmd = MBOX_CMD_DSP_TCFG;
452 mbcompose_send_and_wait(TCFG, tid, 0, &cfg_wait_q);
454 mutex_unlock(&cfg_lock);
456 if (task->state != TASK_ST_READY) {
457 printk(KERN_ERR "omapdsp: task %d configuration error!\n", tid);
462 if (strlen(task->name) <= 1)
463 sprintf(task->name, "%d", tid);
464 pr_info("omapdsp: task %d: name %s\n", tid, task->name);
469 * task info sanity check
472 /* task type check */
473 if (rcvtyp_psv(ttyp) && rcvtyp_pvt(ttyp)) {
474 printk(KERN_ERR "omapdsp: illegal task type(0x%04x), tid=%d\n",
480 /* private buffer address check */
481 if (sndtyp_pvt(ttyp) &&
482 (ipbuf_p_validate(task->ipbuf_pvt_r, DIR_D2A) < 0)) {
486 if (rcvtyp_pvt(ttyp) &&
487 (ipbuf_p_validate(task->ipbuf_pvt_w, DIR_A2D) < 0)) {
492 /* mmap buffer configuration check */
493 if ((task->map_length > 0) &&
494 ((!ALIGN((unsigned long)task->map_base, PAGE_SIZE)) ||
495 (!ALIGN(task->map_length, PAGE_SIZE)) ||
496 (dsp_mem_type(task->map_base, task->map_length) != MEM_TYPE_EXTERN))) {
498 "omapdsp: illegal mmap buffer address(0x%p) or "
500 " It needs to be page-aligned and located at "
501 "external memory.\n",
502 task->map_base, task->map_length);
514 static void dsp_task_init(struct dsptask *task)
516 mbcompose_send(TCTL, task->tid, TCTL_TINIT);
519 int dsp_task_config_all(u8 n)
522 struct taskdev *devheap;
523 struct dsptask *taskheap;
524 size_t devheapsz, taskheapsz;
526 pr_info("omapdsp: found %d task(s)\n", n);
533 devheapsz = sizeof(struct taskdev) * n;
534 taskheapsz = sizeof(struct dsptask) * n;
535 heap = kzalloc(devheapsz + taskheapsz, GFP_KERNEL);
539 taskheap = heap + devheapsz;
542 for (i = 0; i < n; i++) {
543 struct taskdev *dev = &devheap[i];
544 struct dsptask *task = &taskheap[i];
546 if ((ret = dsp_task_config(task, i)) < 0)
548 if ((ret = taskdev_init(dev, task->name, i)) < 0)
550 if ((ret = taskdev_attach_task(dev, task)) < 0)
553 pr_info("omapdsp: taskdev %s enabled.\n", dev->name);
559 static void dsp_task_unconfig(struct dsptask *task)
561 dsptask[task->tid] = NULL;
564 void dsp_task_unconfig_all(void)
568 struct dsptask *task;
570 for (minor = 0; minor < n_task; minor++) {
572 * taskdev[minor] can be NULL in case of
573 * configuration failure
576 taskdev_delete(minor);
578 for (; minor < TASKDEV_MAX; minor++) {
580 dsp_rmdev_minor(minor);
583 for (tid = 0; tid < n_task; tid++) {
585 * dsptask[tid] can be NULL in case of
586 * configuration failure
590 dsp_task_unconfig(task);
592 for (; tid < TASKDEV_MAX; tid++) {
596 * on-demand tasks should be deleted in
597 * rmdev_minor(), but just in case.
599 dsp_task_unconfig(task);
612 static struct device_driver dsptask_driver = {
617 u8 dsp_task_count(void)
622 int dsp_taskmod_busy(void)
626 unsigned int usecount;
628 for (minor = 0; minor < TASKDEV_MAX; minor++) {
629 dev = taskdev[minor];
632 if ((usecount = dev->usecount) > 0) {
633 printk("dsp_taskmod_busy(): %s: usecount=%d\n",
634 dev->name, usecount);
638 if ((dev->state & (TASKDEV_ST_ADDREQ |
639 TASKDEV_ST_DELREQ)) {
641 if (dev->state & TASKDEV_ST_ADDREQ) {
642 printk("dsp_taskmod_busy(): %s is in %s\n",
643 dev->name, devstate_name(dev->state));
651 * DSP task device file operations
653 static ssize_t dsp_task_read_wd_acv(struct file *file, char __user *buf,
654 size_t count, loff_t *ppos)
656 unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
657 struct taskdev *dev = taskdev[minor];
663 } else if (count & 0x1) {
665 "omapdsp: odd count is illegal for DSP task device.\n");
669 if (taskdev_lock_and_statelock_attached(dev, &dev->read_mutex))
673 prepare_to_wait(&dev->read_wait_q, &wait, TASK_INTERRUPTIBLE);
674 if (kfifo_len(dev->rcvdt.fifo) == 0)
676 finish_wait(&dev->read_wait_q, &wait);
677 if (kfifo_len(dev->rcvdt.fifo) == 0) {
679 if (signal_pending(current))
685 ret = kfifo_get_to_user(dev->rcvdt.fifo, buf, count);
688 taskdev_unlock_and_stateunlock(dev, &dev->read_mutex);
692 static ssize_t dsp_task_read_bk_acv(struct file *file, char __user *buf,
693 size_t count, loff_t *ppos)
695 unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
696 struct taskdev *dev = taskdev[minor];
697 struct rcvdt_bk_struct *rcvdt = &dev->rcvdt.bk;
703 } else if (count & 0x1) {
705 "omapdsp: odd count is illegal for DSP task device.\n");
707 } else if ((int)buf & 0x1) {
709 "omapdsp: buf should be word aligned for "
710 "dsp_task_read().\n");
714 if (taskdev_lock_and_statelock_attached(dev, &dev->read_mutex))
717 prepare_to_wait(&dev->read_wait_q, &wait, TASK_INTERRUPTIBLE);
718 if (ipblink_empty(&rcvdt->link))
720 finish_wait(&dev->read_wait_q, &wait);
721 if (ipblink_empty(&rcvdt->link)) {
723 if (signal_pending(current))
728 /* copy from delayed IPBUF */
729 if (sndtyp_pvt(dev->task->ttyp)) {
731 if (!ipblink_empty(&rcvdt->link)) {
732 struct ipbuf_p *ipbp = dev->task->ipbuf_pvt_r;
733 unsigned char *base, *src;
736 if (dsp_mem_enable(ipbp) < 0) {
740 base = MKVIRT(ipbp->ah, ipbp->al);
741 bkcnt = ((unsigned long)ipbp->c) * 2 - rcvdt->rp;
742 if (dsp_address_validate(base, bkcnt,
743 "task %s read buffer",
744 dev->task->name) < 0) {
748 if (dsp_mem_enable(base) < 0) {
752 src = base + rcvdt->rp;
754 if (copy_to_user_dsp(buf, src, count)) {
761 if (copy_to_user_dsp(buf, src, bkcnt)) {
766 ipblink_del_pvt(&rcvdt->link);
767 release_ipbuf_pvt(ipbp);
771 dsp_mem_disable(src);
773 dsp_mem_disable(ipbp);
777 if (dsp_mem_enable_ipbuf() < 0) {
781 while (!ipblink_empty(&rcvdt->link)) {
784 struct ipbuf_head *ipb_h = bid_to_ipbuf(rcvdt->link.top);
786 src = ipb_h->p->d + rcvdt->rp;
787 bkcnt = ((unsigned long)ipb_h->p->c) * 2 - rcvdt->rp;
789 if (copy_to_user_dsp(buf, src, count)) {
797 if (copy_to_user_dsp(buf, src, bkcnt)) {
804 ipblink_del_top(&rcvdt->link);
810 dsp_mem_disable_ipbuf();
814 taskdev_unlock_and_stateunlock(dev, &dev->read_mutex);
818 static ssize_t dsp_task_read_wd_psv(struct file *file, char __user *buf,
819 size_t count, loff_t *ppos)
821 unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
822 struct taskdev *dev = taskdev[minor];
827 } else if (count & 0x1) {
829 "omapdsp: odd count is illegal for DSP task device.\n");
836 if (taskdev_lock_and_statelock_attached(dev, &dev->read_mutex))
839 mbcompose_send_and_wait(WDREQ, dev->task->tid, 0, &dev->read_wait_q);
841 if (kfifo_len(dev->rcvdt.fifo) == 0) {
843 if (signal_pending(current))
848 ret = kfifo_get_to_user(dev->rcvdt.fifo, buf, count);
851 taskdev_unlock_and_stateunlock(dev, &dev->read_mutex);
855 static ssize_t dsp_task_read_bk_psv(struct file *file, char __user *buf,
856 size_t count, loff_t *ppos)
858 unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
859 struct taskdev *dev = taskdev[minor];
860 struct rcvdt_bk_struct *rcvdt = &dev->rcvdt.bk;
865 } else if (count & 0x1) {
867 "omapdsp: odd count is illegal for DSP task device.\n");
869 } else if ((int)buf & 0x1) {
871 "omapdsp: buf should be word aligned for "
872 "dsp_task_read().\n");
876 if (taskdev_lock_and_statelock_attached(dev, &dev->read_mutex))
879 mbcompose_send_and_wait(BKREQ, dev->task->tid, count/2,
882 if (ipblink_empty(&rcvdt->link)) {
884 if (signal_pending(current))
890 * We will not receive more than requested count.
892 if (sndtyp_pvt(dev->task->ttyp)) {
894 struct ipbuf_p *ipbp = dev->task->ipbuf_pvt_r;
898 if (dsp_mem_enable(ipbp) < 0) {
902 src = MKVIRT(ipbp->ah, ipbp->al);
903 rcvcnt = ((unsigned long)ipbp->c) * 2;
904 if (dsp_address_validate(src, rcvcnt, "task %s read buffer",
905 dev->task->name) < 0) {
909 if (dsp_mem_enable(src) < 0) {
915 if (copy_to_user_dsp(buf, src, count)) {
919 ipblink_del_pvt(&rcvdt->link);
920 release_ipbuf_pvt(ipbp);
923 dsp_mem_disable(src);
925 dsp_mem_disable(ipbp);
928 struct ipbuf_head *ipb_h = bid_to_ipbuf(rcvdt->link.top);
931 if (dsp_mem_enable_ipbuf() < 0) {
935 rcvcnt = ((unsigned long)ipb_h->p->c) * 2;
938 if (copy_to_user_dsp(buf, ipb_h->p->d, count)) {
942 ipblink_del_top(&rcvdt->link);
946 dsp_mem_disable_ipbuf();
950 taskdev_unlock_and_stateunlock(dev, &dev->read_mutex);
954 static ssize_t dsp_task_write_wd(struct file *file, const char __user *buf,
955 size_t count, loff_t *ppos)
957 unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
958 struct taskdev *dev = taskdev[minor];
965 } else if (count & 0x1) {
967 "omapdsp: odd count is illegal for DSP task device.\n");
974 if (taskdev_lock_and_statelock_attached(dev, &dev->write_mutex))
977 prepare_to_wait(&dev->write_wait_q, &wait, TASK_INTERRUPTIBLE);
980 finish_wait(&dev->write_wait_q, &wait);
983 if (signal_pending(current))
988 if (copy_from_user(&wd, buf, count)) {
993 spin_lock(&dev->wsz_lock);
994 if (mbcompose_send(WDSND, dev->task->tid, wd) < 0) {
995 spin_unlock(&dev->wsz_lock);
999 if (rcvtyp_acv(dev->task->ttyp))
1001 spin_unlock(&dev->wsz_lock);
1004 taskdev_unlock_and_stateunlock(dev, &dev->write_mutex);
1008 static ssize_t dsp_task_write_bk(struct file *file, const char __user *buf,
1009 size_t count, loff_t *ppos)
1011 unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
1012 struct taskdev *dev = taskdev[minor];
1018 } else if (count & 0x1) {
1020 "omapdsp: odd count is illegal for DSP task device.\n");
1022 } else if ((int)buf & 0x1) {
1024 "omapdsp: buf should be word aligned for "
1025 "dsp_task_write().\n");
1029 if (taskdev_lock_and_statelock_attached(dev, &dev->write_mutex))
1032 prepare_to_wait(&dev->write_wait_q, &wait, TASK_INTERRUPTIBLE);
1035 finish_wait(&dev->write_wait_q, &wait);
1036 if (dev->wsz == 0) {
1038 if (signal_pending(current))
1043 if (count > dev->wsz)
1046 if (rcvtyp_pvt(dev->task->ttyp)) {
1048 struct ipbuf_p *ipbp = dev->task->ipbuf_pvt_w;
1051 if (dsp_mem_enable(ipbp) < 0) {
1055 dst = MKVIRT(ipbp->ah, ipbp->al);
1056 if (dsp_address_validate(dst, count, "task %s write buffer",
1057 dev->task->name) < 0) {
1061 if (dsp_mem_enable(dst) < 0) {
1065 if (copy_from_user_dsp(dst, buf, count)) {
1070 ipbp->s = dev->task->tid;
1071 spin_lock(&dev->wsz_lock);
1072 if (mbcompose_send(BKSNDP, dev->task->tid, 0) == 0) {
1073 if (rcvtyp_acv(dev->task->ttyp))
1077 spin_unlock(&dev->wsz_lock);
1079 dsp_mem_disable(dst);
1081 dsp_mem_disable(ipbp);
1084 struct ipbuf_head *ipb_h;
1086 if (dsp_mem_enable_ipbuf() < 0) {
1090 if ((ipb_h = get_free_ipbuf(dev->task->tid)) == NULL)
1092 if (copy_from_user_dsp(ipb_h->p->d, buf, count)) {
1093 release_ipbuf(ipb_h);
1097 ipb_h->p->c = count/2;
1098 ipb_h->p->sa = dev->task->tid;
1099 spin_lock(&dev->wsz_lock);
1100 if (mbcompose_send(BKSND, dev->task->tid, ipb_h->bid) == 0) {
1101 if (rcvtyp_acv(dev->task->ttyp))
1104 ipb_bsycnt_inc(&ipbcfg);
1106 release_ipbuf(ipb_h);
1107 spin_unlock(&dev->wsz_lock);
1109 dsp_mem_disable_ipbuf();
1113 taskdev_unlock_and_stateunlock(dev, &dev->write_mutex);
1117 static unsigned int dsp_task_poll(struct file * file, poll_table * wait)
1119 unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
1120 struct taskdev *dev = taskdev[minor];
1121 struct dsptask *task = dev->task;
1122 unsigned int mask = 0;
1124 if (!devstate_read_lock_and_test(dev, TASKDEV_ST_ATTACHED))
1126 poll_wait(file, &dev->read_wait_q, wait);
1127 poll_wait(file, &dev->write_wait_q, wait);
1128 if (sndtyp_psv(task->ttyp) ||
1129 (sndtyp_wd(task->ttyp) && kfifo_len(dev->rcvdt.fifo)) ||
1130 (sndtyp_bk(task->ttyp) && !ipblink_empty(&dev->rcvdt.bk.link)))
1131 mask |= POLLIN | POLLRDNORM;
1133 mask |= POLLOUT | POLLWRNORM;
1134 devstate_read_unlock(dev);
1139 static int dsp_tctl_issue(struct taskdev *dev, u16 cmd, int argc, u16 argv[])
1142 struct mb_exarg mbarg, *mbargp;
1150 * system reserved TCTL commands
1164 * user-defined TCTL commands
1166 else if (cmd < 0x8100) {
1167 /* 0x8000-0x80ff: no arg, non-interactive */
1170 } else if (cmd < 0x8200) {
1171 /* 0x8100-0x81ff: 1 arg, non-interactive */
1174 } else if (cmd < 0x9000) {
1175 /* 0x8200-0x8fff: reserved */
1177 } else if (cmd < 0x9100) {
1178 /* 0x9000-0x90ff: no arg, interactive */
1181 } else if (cmd < 0x9200) {
1182 /* 0x9100-0x91ff: 1 arg, interactive */
1186 /* 0x9200-0xffff: reserved */
1191 * if argc < 0, use tctl_argc as is.
1192 * if argc >= 0, check arg count.
1194 if ((argc >= 0) && (argc != tctl_argc))
1200 if (taskdev_lock_interruptible(dev, &dev->tctl_mutex))
1203 tid = dev->task->tid;
1204 if (tctl_argc > 0) {
1205 mbarg.argc = tctl_argc;
1213 dev->tctl_stat = -EINVAL;
1215 mbcompose_send_and_wait_exarg(TCTL, tid, cmd, mbargp,
1217 if (signal_pending(current)) {
1221 if ((ret = dev->tctl_stat) < 0) {
1222 printk(KERN_ERR "omapdsp: TCTL not responding.\n");
1226 mbcompose_send_exarg(TCTL, tid, cmd, mbargp);
1229 mutex_unlock(&dev->tctl_mutex);
1233 static int dsp_task_ioctl(struct inode *inode, struct file *file,
1234 unsigned int cmd, unsigned long arg)
1236 unsigned int minor = MINOR(inode->i_rdev);
1237 struct taskdev *dev = taskdev[minor];
1240 if (cmd < 0x10000) {
1244 mbargv[0] = arg & 0xffff;
1245 return dsp_tctl_issue(dev, cmd, -1, mbargv);
1248 /* non TCTL ioctls */
1251 case TASK_IOCTL_LOCK:
1252 ret = taskdev_lock(dev);
1255 case TASK_IOCTL_UNLOCK:
1256 ret = taskdev_unlock(dev);
1259 case TASK_IOCTL_BFLSH:
1260 if (taskdev_lock_and_statelock_attached(dev, &dev->read_mutex))
1262 ret = taskdev_flush_buf(dev);
1263 taskdev_unlock_and_stateunlock(dev, &dev->read_mutex);
1266 case TASK_IOCTL_SETBSZ:
1267 if (taskdev_lock_and_statelock_attached(dev, &dev->read_mutex))
1269 ret = taskdev_set_fifosz(dev, arg);
1270 taskdev_unlock_and_stateunlock(dev, &dev->read_mutex);
1273 case TASK_IOCTL_GETNAME:
1275 if (copy_to_user((void __user *)arg, dev->name,
1276 strlen(dev->name) + 1))
1288 static void dsp_task_mmap_open(struct vm_area_struct *vma)
1290 struct taskdev *dev = (struct taskdev *)vma->vm_private_data;
1291 struct dsptask *task;
1292 size_t len = vma->vm_end - vma->vm_start;
1294 BUG_ON(!(dev->state & TASKDEV_ST_ATTACHED));
1296 omap_mmu_exmap_use(&dsp_mmu, task->map_base, len);
1299 static void dsp_task_mmap_close(struct vm_area_struct *vma)
1301 struct taskdev *dev = (struct taskdev *)vma->vm_private_data;
1302 struct dsptask *task;
1303 size_t len = vma->vm_end - vma->vm_start;
1305 BUG_ON(!(dev->state & TASKDEV_ST_ATTACHED));
1307 omap_mmu_exmap_unuse(&dsp_mmu, task->map_base, len);
1311 * On demand page allocation is not allowed. The mapping area is defined by
1312 * corresponding DSP tasks.
1314 static int dsp_task_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1316 return VM_FAULT_NOPAGE;
1319 static struct vm_operations_struct dsp_task_vm_ops = {
1320 .open = dsp_task_mmap_open,
1321 .close = dsp_task_mmap_close,
1322 .fault = dsp_task_mmap_fault,
1325 static int dsp_task_mmap(struct file *filp, struct vm_area_struct *vma)
1328 unsigned long tmp_padr, tmp_vmadr, off;
1329 size_t req_len, tmp_len;
1330 unsigned int minor = MINOR(filp->f_dentry->d_inode->i_rdev);
1331 struct taskdev *dev = taskdev[minor];
1332 struct dsptask *task;
1335 if (!devstate_read_lock_and_test(dev, TASKDEV_ST_ATTACHED))
1340 * Don't swap this area out
1341 * Don't dump this area to a core file
1343 vma->vm_flags |= VM_RESERVED | VM_IO;
1345 /* Do not cache this area */
1346 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1348 req_len = vma->vm_end - vma->vm_start;
1349 off = vma->vm_pgoff << PAGE_SHIFT;
1350 tmp_vmadr = vma->vm_start;
1351 tmp_vadr = task->map_base + off;
1353 tmp_padr = omap_mmu_virt_to_phys(&dsp_mmu, tmp_vadr, &tmp_len);
1354 if (tmp_padr == 0) {
1356 "omapdsp: task %s: illegal address "
1357 "for mmap: %p", task->name, tmp_vadr);
1358 /* partial mapping will be cleared in upper layer */
1362 if (tmp_len > req_len)
1365 pr_debug("omapdsp: mmap info: "
1366 "vmadr = %08lx, padr = %08lx, len = %x\n",
1367 tmp_vmadr, tmp_padr, tmp_len);
1368 if (remap_pfn_range(vma, tmp_vmadr, tmp_padr >> PAGE_SHIFT,
1369 tmp_len, vma->vm_page_prot) != 0) {
1371 "omapdsp: task %s: remap_page_range() failed.\n",
1373 /* partial mapping will be cleared in upper layer */
1379 tmp_vmadr += tmp_len;
1380 tmp_vadr += tmp_len;
1383 vma->vm_ops = &dsp_task_vm_ops;
1384 vma->vm_private_data = dev;
1385 omap_mmu_exmap_use(&dsp_mmu, task->map_base, vma->vm_end - vma->vm_start);
1388 devstate_read_unlock(dev);
1392 static int dsp_task_open(struct inode *inode, struct file *file)
1394 unsigned int minor = MINOR(inode->i_rdev);
1395 struct taskdev *dev;
1398 if ((minor >= TASKDEV_MAX) || ((dev = taskdev[minor]) == NULL))
1402 mutex_lock(&dev->usecount_lock);
1403 down_write(&dev->state_sem);
1405 /* state can be NOTASK, ATTACHED/FREEZED, KILLING, GARBAGE or INVALID here. */
1406 switch (dev->state & TASKDEV_ST_STATE_MASK) {
1407 case TASKDEV_ST_NOTASK:
1409 case TASKDEV_ST_ATTACHED:
1412 case TASKDEV_ST_INVALID:
1413 up_write(&dev->state_sem);
1414 mutex_unlock(&dev->usecount_lock);
1417 case TASKDEV_ST_FREEZED:
1418 case TASKDEV_ST_KILLING:
1419 case TASKDEV_ST_GARBAGE:
1420 case TASKDEV_ST_DELREQ:
1421 /* on the kill process. wait until it becomes NOTASK. */
1422 up_write(&dev->state_sem);
1423 mutex_unlock(&dev->usecount_lock);
1424 if (devstate_write_lock(dev, TASKDEV_ST_NOTASK) < 0)
1426 devstate_write_unlock(dev);
1431 set_taskdev_state(dev, TASKDEV_ST_ADDREQ);
1432 /* wake up twch daemon for tadd */
1434 up_write(&dev->state_sem);
1435 if (devstate_write_lock(dev, TASKDEV_ST_ATTACHED |
1436 TASKDEV_ST_ADDFAIL) < 0) {
1438 if (!devstate_write_lock_and_test(dev, TASKDEV_ST_ADDREQ)) {
1439 mutex_unlock(&dev->usecount_lock);
1440 /* out of control ??? */
1443 set_taskdev_state(dev, TASKDEV_ST_NOTASK);
1447 if (dev->state & TASKDEV_ST_ADDFAIL) {
1448 printk(KERN_ERR "omapdsp: task attach failed for %s!\n",
1451 set_taskdev_state(dev, TASKDEV_ST_NOTASK);
1456 ret = proc_list_add(&dev->proc_list_lock,
1457 &dev->proc_list, current, file);
1462 file->f_op = &dev->fops;
1463 up_write(&dev->state_sem);
1464 mutex_unlock(&dev->usecount_lock);
1466 #ifdef DSP_PTE_FREE /* not used currently. */
1467 dsp_map_update(current);
1468 dsp_cur_users_add(current);
1469 #endif /* DSP_PTE_FREE */
1473 wake_up_interruptible_all(&dev->state_wait_q);
1475 up_write(&dev->state_sem);
1476 mutex_unlock(&dev->usecount_lock);
1480 static int dsp_task_release(struct inode *inode, struct file *file)
1482 unsigned int minor = MINOR(inode->i_rdev);
1483 struct taskdev *dev = taskdev[minor];
1485 #ifdef DSP_PTE_FREE /* not used currently. */
1486 dsp_cur_users_del(current);
1487 #endif /* DSP_PTE_FREE */
1489 if (has_taskdev_lock(dev))
1490 taskdev_unlock(dev);
1492 proc_list_del(&dev->proc_list_lock, &dev->proc_list, current, file);
1493 mutex_lock(&dev->usecount_lock);
1494 if (--dev->usecount > 0) {
1495 /* other processes are using this device. no state change. */
1496 mutex_unlock(&dev->usecount_lock);
1501 down_write(&dev->state_sem);
1503 /* state can be ATTACHED/FREEZED, KILLING or GARBAGE here. */
1504 switch (dev->state & TASKDEV_ST_STATE_MASK) {
1506 case TASKDEV_ST_KILLING:
1509 case TASKDEV_ST_GARBAGE:
1510 set_taskdev_state(dev, TASKDEV_ST_NOTASK);
1511 wake_up_interruptible_all(&dev->state_wait_q);
1514 case TASKDEV_ST_ATTACHED:
1515 case TASKDEV_ST_FREEZED:
1516 if (is_dynamic_task(minor)) {
1517 set_taskdev_state(dev, TASKDEV_ST_DELREQ);
1518 /* wake up twch daemon for tdel */
1525 up_write(&dev->state_sem);
1526 mutex_unlock(&dev->usecount_lock);
1533 int dsp_mkdev(char *name)
1535 struct taskdev *dev;
1537 unsigned char minor;
1540 if (dsp_cfgstat_get_stat() != CFGSTAT_READY) {
1541 printk(KERN_ERR "omapdsp: dsp has not been configured.\n");
1545 if (mutex_lock_interruptible(&devmgr_lock))
1549 for (minor = 0; minor < TASKDEV_MAX; minor++) {
1550 if (taskdev[minor] && !strcmp(taskdev[minor]->name, name)) {
1552 "omapdsp: task device name %s is already "
1559 /* find free minor number */
1560 for (minor = n_task; minor < TASKDEV_MAX; minor++) {
1561 if (taskdev[minor] == NULL)
1564 printk(KERN_ERR "omapdsp: Too many task devices.\n");
1569 if ((dev = kzalloc(sizeof(struct taskdev), GFP_KERNEL)) == NULL) {
1573 if ((status = taskdev_init(dev, name, minor)) < 0) {
1581 mutex_unlock(&devmgr_lock);
1585 int dsp_rmdev(char *name)
1587 unsigned char minor;
1591 if (dsp_cfgstat_get_stat() != CFGSTAT_READY) {
1592 printk(KERN_ERR "omapdsp: dsp has not been configured.\n");
1596 if (mutex_lock_interruptible(&devmgr_lock))
1599 /* find in dynamic devices */
1600 for (minor = n_task; minor < TASKDEV_MAX; minor++) {
1601 if (taskdev[minor] && !strcmp(taskdev[minor]->name, name))
1605 /* find in static devices */
1606 for (minor = 0; minor < n_task; minor++) {
1607 if (taskdev[minor] && !strcmp(taskdev[minor]->name, name)) {
1609 "omapdsp: task device %s is static.\n", name);
1615 printk(KERN_ERR "omapdsp: task device %s not found.\n", name);
1620 if ((status = dsp_rmdev_minor(minor)) < 0)
1623 mutex_unlock(&devmgr_lock);
1627 static int dsp_rmdev_minor(unsigned char minor)
1629 struct taskdev *dev = taskdev[minor];
1631 while (!down_write_trylock(&dev->state_sem)) {
1632 down_read(&dev->state_sem);
1633 if (dev->state & (TASKDEV_ST_ATTACHED |
1634 TASKDEV_ST_FREEZED)) {
1636 * task is working. kill it.
1637 * ATTACHED -> FREEZED can be changed under
1638 * down_read of state_sem..
1640 set_taskdev_state(dev, TASKDEV_ST_FREEZED);
1641 wake_up_interruptible_all(&dev->read_wait_q);
1642 wake_up_interruptible_all(&dev->write_wait_q);
1643 wake_up_interruptible_all(&dev->tctl_wait_q);
1645 up_read(&dev->state_sem);
1649 switch (dev->state & TASKDEV_ST_STATE_MASK) {
1651 case TASKDEV_ST_NOTASK:
1652 case TASKDEV_ST_INVALID:
1656 case TASKDEV_ST_ATTACHED:
1657 case TASKDEV_ST_FREEZED:
1658 /* task is working. kill it. */
1659 set_taskdev_state(dev, TASKDEV_ST_KILLING);
1660 up_write(&dev->state_sem);
1661 dsp_tdel_bh(dev, TDEL_KILL);
1664 case TASKDEV_ST_ADDREQ:
1665 /* open() is waiting. drain it. */
1666 set_taskdev_state(dev, TASKDEV_ST_ADDFAIL);
1667 wake_up_interruptible_all(&dev->state_wait_q);
1670 case TASKDEV_ST_DELREQ:
1671 /* nobody is waiting. */
1672 set_taskdev_state(dev, TASKDEV_ST_NOTASK);
1673 wake_up_interruptible_all(&dev->state_wait_q);
1676 case TASKDEV_ST_ADDING:
1677 case TASKDEV_ST_DELING:
1678 case TASKDEV_ST_KILLING:
1679 case TASKDEV_ST_GARBAGE:
1680 case TASKDEV_ST_ADDFAIL:
1681 /* transient state. wait for a moment. */
1686 up_write(&dev->state_sem);
1689 /* wait for some time and hope the state is settled */
1690 devstate_read_lock_timeout(dev, TASKDEV_ST_NOTASK, 5 * HZ);
1691 if (!(dev->state & TASKDEV_ST_NOTASK)) {
1693 "omapdsp: illegal device state (%s) on rmdev %s.\n",
1694 devstate_name(dev->state), dev->name);
1697 set_taskdev_state(dev, TASKDEV_ST_INVALID);
1698 devstate_read_unlock(dev);
1700 taskdev_delete(minor);
1706 static struct file_operations dsp_task_fops = {
1707 .owner = THIS_MODULE,
1708 .poll = dsp_task_poll,
1709 .ioctl = dsp_task_ioctl,
1710 .open = dsp_task_open,
1711 .release = dsp_task_release,
1714 static void dsptask_dev_release(struct device *dev)
1718 static int taskdev_init(struct taskdev *dev, char *name, unsigned char minor)
1721 struct device *task_dev;
1723 taskdev[minor] = dev;
1725 spin_lock_init(&dev->proc_list_lock);
1726 INIT_LIST_HEAD(&dev->proc_list);
1727 init_waitqueue_head(&dev->read_wait_q);
1728 init_waitqueue_head(&dev->write_wait_q);
1729 init_waitqueue_head(&dev->tctl_wait_q);
1730 mutex_init(&dev->read_mutex);
1731 mutex_init(&dev->write_mutex);
1732 mutex_init(&dev->tctl_mutex);
1733 mutex_init(&dev->lock);
1734 spin_lock_init(&dev->wsz_lock);
1735 dev->tctl_ret = -EINVAL;
1738 strncpy(dev->name, name, TNM_LEN);
1739 dev->name[TNM_LEN-1] = '\0';
1740 set_taskdev_state(dev, (minor < n_task) ? TASKDEV_ST_ATTACHED : TASKDEV_ST_NOTASK);
1742 mutex_init(&dev->usecount_lock);
1743 memcpy(&dev->fops, &dsp_task_fops, sizeof(struct file_operations));
1745 dev->dev.parent = omap_dsp->dev;
1746 dev->dev.bus = &dsptask_bus;
1747 sprintf(dev->dev.bus_id, "dsptask%d", minor);
1748 dev->dev.release = dsptask_dev_release;
1749 ret = device_register(&dev->dev);
1751 printk(KERN_ERR "device_register failed: %d\n", ret);
1754 ret = device_create_file(&dev->dev, &dev_attr_devname);
1756 goto fail_create_devname;
1757 ret = device_create_file(&dev->dev, &dev_attr_devstate);
1759 goto fail_create_devstate;
1760 ret = device_create_file(&dev->dev, &dev_attr_proc_list);
1762 goto fail_create_proclist;
1764 task_dev = device_create(dsp_task_class, NULL,
1765 MKDEV(OMAP_DSP_TASK_MAJOR, minor), NULL,
1766 "dsptask%d", (int)minor);
1768 if (unlikely(IS_ERR(task_dev))) {
1770 goto fail_create_taskclass;
1773 init_waitqueue_head(&dev->state_wait_q);
1774 init_rwsem(&dev->state_sem);
1778 fail_create_taskclass:
1779 device_remove_file(&dev->dev, &dev_attr_proc_list);
1780 fail_create_proclist:
1781 device_remove_file(&dev->dev, &dev_attr_devstate);
1782 fail_create_devstate:
1783 device_remove_file(&dev->dev, &dev_attr_devname);
1784 fail_create_devname:
1785 device_unregister(&dev->dev);
1789 static void taskdev_delete(unsigned char minor)
1791 struct taskdev *dev = taskdev[minor];
1795 device_remove_file(&dev->dev, &dev_attr_devname);
1796 device_remove_file(&dev->dev, &dev_attr_devstate);
1797 device_remove_file(&dev->dev, &dev_attr_proc_list);
1798 device_destroy(dsp_task_class, MKDEV(OMAP_DSP_TASK_MAJOR, minor));
1799 device_unregister(&dev->dev);
1800 proc_list_flush(&dev->proc_list_lock, &dev->proc_list);
1801 taskdev[minor] = NULL;
1804 static int taskdev_attach_task(struct taskdev *dev, struct dsptask *task)
1806 u16 ttyp = task->ttyp;
1811 sndtyp_wd(ttyp) ? dsp_task_read_wd_acv:
1812 /* sndtyp_bk */ dsp_task_read_bk_acv:
1814 sndtyp_wd(ttyp) ? dsp_task_read_wd_psv:
1815 /* sndtyp_bk */ dsp_task_read_bk_psv;
1816 if (sndtyp_wd(ttyp)) {
1818 size_t fifosz = sndtyp_psv(ttyp) ? 2:32; /* passive:active */
1820 dev->rcvdt.fifo = kfifo_alloc(fifosz, GFP_KERNEL,
1822 if (IS_ERR(dev->rcvdt.fifo)) {
1824 "omapdsp: unable to allocate receive buffer. "
1825 "(%d bytes for %s)\n", fifosz, dev->name);
1830 INIT_IPBLINK(&dev->rcvdt.bk.link);
1831 dev->rcvdt.bk.rp = 0;
1835 rcvtyp_wd(ttyp) ? dsp_task_write_wd:
1836 /* rcvbyp_bk */ dsp_task_write_bk;
1837 dev->wsz = rcvtyp_acv(ttyp) ? 0 : /* active */
1838 rcvtyp_wd(ttyp) ? 2 : /* passive word */
1839 ipbcfg.lsz*2; /* passive block */
1841 if (task->map_length)
1842 dev->fops.mmap = dsp_task_mmap;
1844 ret = device_create_file(&dev->dev, &dev_attr_taskname);
1846 goto fail_create_taskname;
1847 ret = device_create_file(&dev->dev, &dev_attr_ttyp);
1849 goto fail_create_ttyp;
1850 ret = device_create_file(&dev->dev, &dev_attr_wsz);
1852 goto fail_create_wsz;
1853 if (task->map_length) {
1854 ret = device_create_file(&dev->dev, &dev_attr_mmap);
1856 goto fail_create_mmap;
1858 if (sndtyp_wd(ttyp)) {
1859 ret = device_create_file(&dev->dev, &dev_attr_fifosz);
1861 goto fail_create_fifosz;
1862 ret = device_create_file(&dev->dev, &dev_attr_fifocnt);
1864 goto fail_create_fifocnt;
1866 ret = device_create_file(&dev->dev, &dev_attr_ipblink);
1868 goto fail_create_ipblink;
1876 fail_create_fifocnt:
1877 device_remove_file(&dev->dev, &dev_attr_fifosz);
1878 fail_create_ipblink:
1880 if (task->map_length)
1881 device_remove_file(&dev->dev, &dev_attr_mmap);
1883 device_remove_file(&dev->dev, &dev_attr_wsz);
1885 device_remove_file(&dev->dev, &dev_attr_ttyp);
1887 device_remove_file(&dev->dev, &dev_attr_taskname);
1888 fail_create_taskname:
1889 if (task->map_length)
1890 dev->fops.mmap = NULL;
1892 dev->fops.write = NULL;
1895 dev->fops.read = NULL;
1896 taskdev_flush_buf(dev);
1898 if (sndtyp_wd(ttyp))
1899 kfifo_free(dev->rcvdt.fifo);
1906 static void taskdev_detach_task(struct taskdev *dev)
1908 u16 ttyp = dev->task->ttyp;
1910 device_remove_file(&dev->dev, &dev_attr_taskname);
1911 device_remove_file(&dev->dev, &dev_attr_ttyp);
1912 if (sndtyp_wd(ttyp)) {
1913 device_remove_file(&dev->dev, &dev_attr_fifosz);
1914 device_remove_file(&dev->dev, &dev_attr_fifocnt);
1916 device_remove_file(&dev->dev, &dev_attr_ipblink);
1917 device_remove_file(&dev->dev, &dev_attr_wsz);
1918 if (dev->task->map_length) {
1919 device_remove_file(&dev->dev, &dev_attr_mmap);
1920 dev->fops.mmap = NULL;
1923 dev->fops.read = NULL;
1924 taskdev_flush_buf(dev);
1925 if (sndtyp_wd(ttyp))
1926 kfifo_free(dev->rcvdt.fifo);
1928 dev->fops.write = NULL;
1931 pr_info("omapdsp: taskdev %s disabled.\n", dev->name);
1936 * tadd / tdel / tkill
1938 static int dsp_tadd(struct taskdev *dev, dsp_long_t adr)
1940 struct dsptask *task;
1941 struct mb_exarg arg;
1942 u8 tid, tid_response;
1946 if (!devstate_write_lock_and_test(dev, TASKDEV_ST_ADDREQ)) {
1948 "omapdsp: taskdev %s is not requesting for tadd. "
1949 "(state is %s)\n", dev->name, devstate_name(dev->state));
1952 set_taskdev_state(dev, TASKDEV_ST_ADDING);
1953 devstate_write_unlock(dev);
1955 if (adr == TADD_ABORTADR) {
1956 /* aborting tadd intentionally */
1957 pr_info("omapdsp: tadd address is ABORTADR.\n");
1960 if (adr >= DSPSPACE_SIZE) {
1962 "omapdsp: illegal address 0x%08x for tadd\n", adr);
1967 adr >>= 1; /* word address */
1968 argv[0] = adr >> 16; /* addrh */
1969 argv[1] = adr & 0xffff; /* addrl */
1971 if (mutex_lock_interruptible(&cfg_lock)) {
1976 cfg_cmd = MBOX_CMD_DSP_TADD;
1981 if (dsp_mem_sync_inc() < 0) {
1982 printk(KERN_ERR "omapdsp: memory sync failed!\n");
1986 mbcompose_send_and_wait_exarg(TADD, 0, 0, &arg, &cfg_wait_q);
1991 mutex_unlock(&cfg_lock);
1993 if (tid == TID_ANON) {
1994 printk(KERN_ERR "omapdsp: tadd failed!\n");
1998 if ((tid < n_task) || dsptask[tid]) {
1999 printk(KERN_ERR "omapdsp: illegal tid (%d)!\n", tid);
2003 if ((task = kzalloc(sizeof(struct dsptask), GFP_KERNEL)) == NULL) {
2008 if ((ret = dsp_task_config(task, tid)) < 0)
2011 if (strcmp(dev->name, task->name)) {
2013 "omapdsp: task name (%s) doesn't match with "
2014 "device name (%s).\n", task->name, dev->name);
2019 if ((ret = taskdev_attach_task(dev, task)) < 0)
2022 dsp_task_init(task);
2023 pr_info("omapdsp: taskdev %s enabled.\n", dev->name);
2024 set_taskdev_state(dev, TASKDEV_ST_ATTACHED);
2025 wake_up_interruptible_all(&dev->state_wait_q);
2032 printk(KERN_ERR "omapdsp: deleting the task...\n");
2034 set_taskdev_state(dev, TASKDEV_ST_DELING);
2036 if (mutex_lock_interruptible(&cfg_lock)) {
2037 printk(KERN_ERR "omapdsp: aborting tdel process. "
2038 "DSP side could be corrupted.\n");
2042 cfg_cmd = MBOX_CMD_DSP_TDEL;
2043 mbcompose_send_and_wait(TDEL, tid, TDEL_KILL, &cfg_wait_q);
2044 tid_response = cfg_tid;
2047 mutex_unlock(&cfg_lock);
2049 if (tid_response != tid)
2050 printk(KERN_ERR "omapdsp: tdel failed. "
2051 "DSP side could be corrupted.\n");
2054 set_taskdev_state(dev, TASKDEV_ST_ADDFAIL);
2055 wake_up_interruptible_all(&dev->state_wait_q);
2059 int dsp_tadd_minor(unsigned char minor, dsp_long_t adr)
2061 struct taskdev *dev;
2065 if (mutex_lock_interruptible(&devmgr_lock))
2068 if ((minor >= TASKDEV_MAX) || ((dev = taskdev[minor]) == NULL)) {
2070 "omapdsp: no task device with minor %d\n", minor);
2075 if ((status = dsp_tadd(dev, adr)) < 0)
2079 mutex_unlock(&devmgr_lock);
2083 static int dsp_tdel(struct taskdev *dev)
2085 if (!devstate_write_lock_and_test(dev, TASKDEV_ST_DELREQ)) {
2087 "omapdsp: taskdev %s is not requesting for tdel. "
2088 "(state is %s)\n", dev->name, devstate_name(dev->state));
2091 set_taskdev_state(dev, TASKDEV_ST_DELING);
2092 devstate_write_unlock(dev);
2094 return dsp_tdel_bh(dev, TDEL_SAFE);
2097 int dsp_tdel_minor(unsigned char minor)
2099 struct taskdev *dev;
2103 if (mutex_lock_interruptible(&devmgr_lock))
2106 if ((minor >= TASKDEV_MAX) || ((dev = taskdev[minor]) == NULL)) {
2108 "omapdsp: no task device with minor %d\n", minor);
2114 if ((status = dsp_tdel(dev)) < 0)
2118 mutex_unlock(&devmgr_lock);
2122 static int dsp_tkill(struct taskdev *dev)
2124 while (!down_write_trylock(&dev->state_sem)) {
2125 if (!devstate_read_lock_and_test(dev, (TASKDEV_ST_ATTACHED |
2126 TASKDEV_ST_FREEZED))) {
2128 "omapdsp: task has not been attached for "
2129 "taskdev %s\n", dev->name);
2132 /* ATTACHED -> FREEZED can be changed under read semaphore. */
2133 set_taskdev_state(dev, TASKDEV_ST_FREEZED);
2134 wake_up_interruptible_all(&dev->read_wait_q);
2135 wake_up_interruptible_all(&dev->write_wait_q);
2136 wake_up_interruptible_all(&dev->tctl_wait_q);
2137 devstate_read_unlock(dev);
2141 if (!(dev->state & (TASKDEV_ST_ATTACHED |
2142 TASKDEV_ST_FREEZED))) {
2144 "omapdsp: task has not been attached for taskdev %s\n",
2146 devstate_write_unlock(dev);
2149 if (!is_dynamic_task(dev->task->tid)) {
2150 printk(KERN_ERR "omapdsp: task %s is not a dynamic task.\n",
2152 devstate_write_unlock(dev);
2155 set_taskdev_state(dev, TASKDEV_ST_KILLING);
2156 devstate_write_unlock(dev);
2158 return dsp_tdel_bh(dev, TDEL_KILL);
2161 int dsp_tkill_minor(unsigned char minor)
2163 struct taskdev *dev;
2167 if (mutex_lock_interruptible(&devmgr_lock))
2170 if ((minor >= TASKDEV_MAX) || ((dev = taskdev[minor]) == NULL)) {
2172 "omapdsp: no task device with minor %d\n", minor);
2178 if ((status = dsp_tkill(dev)) < 0)
2182 mutex_unlock(&devmgr_lock);
2186 static int dsp_tdel_bh(struct taskdev *dev, u16 type)
2188 struct dsptask *task;
2189 u8 tid, tid_response;
2194 if (mutex_lock_interruptible(&cfg_lock)) {
2195 if (type == TDEL_SAFE) {
2196 set_taskdev_state(dev, TASKDEV_ST_DELREQ);
2199 tid_response = TID_ANON;
2205 cfg_cmd = MBOX_CMD_DSP_TDEL;
2206 mbcompose_send_and_wait(TDEL, tid, type, &cfg_wait_q);
2207 tid_response = cfg_tid;
2210 mutex_unlock(&cfg_lock);
2213 taskdev_detach_task(dev);
2214 dsp_task_unconfig(task);
2217 if (tid_response != tid) {
2218 printk(KERN_ERR "omapdsp: %s failed!\n",
2219 (type == TDEL_SAFE) ? "tdel" : "tkill");
2222 down_write(&dev->state_sem);
2223 set_taskdev_state(dev, (dev->usecount > 0) ? TASKDEV_ST_GARBAGE :
2225 wake_up_interruptible_all(&dev->state_wait_q);
2226 up_write(&dev->state_sem);
2234 long taskdev_state_stale(unsigned char minor)
2236 if (taskdev[minor]) {
2237 long state = taskdev[minor]->state;
2238 taskdev[minor]->state |= TASKDEV_ST_STALE;
2241 return TASKDEV_ST_NOTASK;
2245 * functions called from mailbox interrupt routine
2247 void mbox_wdsnd(struct mbcmd *mb)
2251 u16 data = mb->data;
2252 struct dsptask *task = dsptask[tid];
2254 if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2255 printk(KERN_ERR "mbox: WDSND with illegal tid! %d\n", tid);
2258 if (sndtyp_bk(task->ttyp)) {
2260 "mbox: WDSND from block sending task! (task%d)\n", tid);
2263 if (sndtyp_psv(task->ttyp) &&
2264 !waitqueue_active(&task->dev->read_wait_q)) {
2266 "mbox: WDSND from passive sending task (task%d) "
2267 "without request!\n", tid);
2271 n = kfifo_put(task->dev->rcvdt.fifo, (unsigned char *)&data,
2273 if (n != sizeof(data))
2274 printk(KERN_WARNING "Receive FIFO(%d) is full\n", tid);
2276 wake_up_interruptible(&task->dev->read_wait_q);
2279 void mbox_wdreq(struct mbcmd *mb)
2282 struct dsptask *task = dsptask[tid];
2283 struct taskdev *dev;
2285 if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2286 printk(KERN_ERR "mbox: WDREQ with illegal tid! %d\n", tid);
2289 if (rcvtyp_psv(task->ttyp)) {
2291 "mbox: WDREQ from passive receiving task! (task%d)\n",
2297 spin_lock(&dev->wsz_lock);
2299 spin_unlock(&dev->wsz_lock);
2300 wake_up_interruptible(&dev->write_wait_q);
2303 void mbox_bksnd(struct mbcmd *mb)
2307 struct dsptask *task = dsptask[tid];
2308 struct ipbuf_head *ipb_h;
2311 if (bid >= ipbcfg.ln) {
2312 printk(KERN_ERR "mbox: BKSND with illegal bid! %d\n", bid);
2315 ipb_h = bid_to_ipbuf(bid);
2316 ipb_bsycnt_dec(&ipbcfg);
2317 if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2318 printk(KERN_ERR "mbox: BKSND with illegal tid! %d\n", tid);
2319 goto unuse_ipbuf_out;
2321 if (sndtyp_wd(task->ttyp)) {
2323 "mbox: BKSND from word sending task! (task%d)\n", tid);
2324 goto unuse_ipbuf_out;
2326 if (sndtyp_pvt(task->ttyp)) {
2328 "mbox: BKSND from private sending task! (task%d)\n", tid);
2329 goto unuse_ipbuf_out;
2331 if (sync_with_dsp(&ipb_h->p->sd, tid, 10) < 0) {
2332 printk(KERN_ERR "mbox: BKSND - IPBUF sync failed!\n");
2336 /* should be done in DSP, but just in case. */
2337 ipb_h->p->next = BID_NULL;
2340 if (cnt > ipbcfg.lsz) {
2341 printk(KERN_ERR "mbox: BKSND cnt(%d) > ipbuf line size(%d)!\n",
2343 goto unuse_ipbuf_out;
2347 /* 0-byte send from DSP */
2348 unuse_ipbuf_nowait(ipb_h);
2351 ipblink_add_tail(&task->dev->rcvdt.bk.link, bid);
2352 /* we keep coming bid and return alternative line to DSP. */
2356 wake_up_interruptible(&task->dev->read_wait_q);
2360 unuse_ipbuf_nowait(ipb_h);
2364 void mbox_bkreq(struct mbcmd *mb)
2368 struct dsptask *task = dsptask[tid];
2369 struct taskdev *dev;
2371 if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2372 printk(KERN_ERR "mbox: BKREQ with illegal tid! %d\n", tid);
2375 if (rcvtyp_wd(task->ttyp)) {
2377 "mbox: BKREQ from word receiving task! (task%d)\n", tid);
2380 if (rcvtyp_pvt(task->ttyp)) {
2382 "mbox: BKREQ from private receiving task! (task%d)\n",
2386 if (rcvtyp_psv(task->ttyp)) {
2388 "mbox: BKREQ from passive receiving task! (task%d)\n",
2394 spin_lock(&dev->wsz_lock);
2396 spin_unlock(&dev->wsz_lock);
2397 wake_up_interruptible(&dev->write_wait_q);
2400 void mbox_bkyld(struct mbcmd *mb)
2403 struct ipbuf_head *ipb_h;
2405 if (bid >= ipbcfg.ln) {
2406 printk(KERN_ERR "mbox: BKYLD with illegal bid! %d\n", bid);
2409 ipb_h = bid_to_ipbuf(bid);
2411 /* should be done in DSP, but just in case. */
2412 ipb_h->p->next = BID_NULL;
2414 /* we don't need to sync with DSP */
2415 ipb_bsycnt_dec(&ipbcfg);
2416 release_ipbuf(ipb_h);
2419 void mbox_bksndp(struct mbcmd *mb)
2422 struct dsptask *task = dsptask[tid];
2423 struct ipbuf_p *ipbp;
2425 if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2426 printk(KERN_ERR "mbox: BKSNDP with illegal tid! %d\n", tid);
2429 if (sndtyp_wd(task->ttyp)) {
2431 "mbox: BKSNDP from word sending task! (task%d)\n", tid);
2434 if (sndtyp_gbl(task->ttyp)) {
2436 "mbox: BKSNDP from non-private sending task! (task%d)\n",
2442 * we should not have delayed block at this point
2443 * because read() routine releases the lock of the buffer and
2444 * until then DSP can't send next data.
2447 ipbp = task->ipbuf_pvt_r;
2448 if (sync_with_dsp(&ipbp->s, tid, 10) < 0) {
2449 printk(KERN_ERR "mbox: BKSNDP - IPBUF sync failed!\n");
2452 pr_debug("mbox: ipbuf_pvt_r->a = 0x%08lx\n",
2453 MKLONG(ipbp->ah, ipbp->al));
2454 ipblink_add_pvt(&task->dev->rcvdt.bk.link);
2455 wake_up_interruptible(&task->dev->read_wait_q);
2458 void mbox_bkreqp(struct mbcmd *mb)
2461 struct dsptask *task = dsptask[tid];
2462 struct taskdev *dev;
2463 struct ipbuf_p *ipbp;
2465 if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2466 printk(KERN_ERR "mbox: BKREQP with illegal tid! %d\n", tid);
2469 if (rcvtyp_wd(task->ttyp)) {
2471 "mbox: BKREQP from word receiving task! (task%d)\n", tid);
2474 if (rcvtyp_gbl(task->ttyp)) {
2476 "mbox: BKREQP from non-private receiving task! (task%d)\n", tid);
2479 if (rcvtyp_psv(task->ttyp)) {
2481 "mbox: BKREQP from passive receiving task! (task%d)\n", tid);
2485 ipbp = task->ipbuf_pvt_w;
2486 if (sync_with_dsp(&ipbp->s, TID_FREE, 10) < 0) {
2487 printk(KERN_ERR "mbox: BKREQP - IPBUF sync failed!\n");
2490 pr_debug("mbox: ipbuf_pvt_w->a = 0x%08lx\n",
2491 MKLONG(ipbp->ah, ipbp->al));
2493 spin_lock(&dev->wsz_lock);
2494 dev->wsz = ipbp->c*2;
2495 spin_unlock(&dev->wsz_lock);
2496 wake_up_interruptible(&dev->write_wait_q);
2499 void mbox_tctl(struct mbcmd *mb)
2502 struct dsptask *task = dsptask[tid];
2504 if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2505 printk(KERN_ERR "mbox: TCTL with illegal tid! %d\n", tid);
2509 if (!waitqueue_active(&task->dev->tctl_wait_q)) {
2510 printk(KERN_WARNING "mbox: unexpected TCTL from DSP!\n");
2514 task->dev->tctl_stat = mb->data;
2515 wake_up_interruptible(&task->dev->tctl_wait_q);
2518 void mbox_tcfg(struct mbcmd *mb)
2521 struct dsptask *task = dsptask[tid];
2526 if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2527 printk(KERN_ERR "mbox: TCFG with illegal tid! %d\n", tid);
2530 if ((task->state != TASK_ST_CFGREQ) || (cfg_cmd != MBOX_CMD_DSP_TCFG)) {
2531 printk(KERN_WARNING "mbox: unexpected TCFG from DSP!\n");
2535 if (dsp_mem_enable(ipbuf_sys_da) < 0) {
2536 printk(KERN_ERR "mbox: TCFG - ipbuf_sys_da read failed!\n");
2537 dsp_mem_disable(ipbuf_sys_da);
2540 if (sync_with_dsp(&ipbuf_sys_da->s, tid, 10) < 0) {
2541 printk(KERN_ERR "mbox: TCFG - IPBUF sync failed!\n");
2542 dsp_mem_disable(ipbuf_sys_da);
2547 * read configuration data on system IPBUF
2549 buf = ipbuf_sys_da->d;
2550 task->ttyp = buf[0];
2551 task->ipbuf_pvt_r = MKVIRT(buf[1], buf[2]);
2552 task->ipbuf_pvt_w = MKVIRT(buf[3], buf[4]);
2553 task->map_base = MKVIRT(buf[5], buf[6]);
2554 task->map_length = MKLONG(buf[7], buf[8]) << 1; /* word -> byte */
2555 tnm = MKVIRT(buf[9], buf[10]);
2556 release_ipbuf_pvt(ipbuf_sys_da);
2557 dsp_mem_disable(ipbuf_sys_da);
2560 * copy task name string
2562 if (dsp_address_validate(tnm, TNM_LEN, "task name buffer") < 0) {
2563 task->name[0] = '\0';
2567 for (i = 0; i < TNM_LEN-1; i++) {
2568 /* avoiding byte access */
2570 task->name[i] = tmp & 0x00ff;
2574 task->name[TNM_LEN-1] = '\0';
2576 task->state = TASK_ST_READY;
2578 wake_up_interruptible(&cfg_wait_q);
2581 void mbox_tadd(struct mbcmd *mb)
2585 if ((!waitqueue_active(&cfg_wait_q)) || (cfg_cmd != MBOX_CMD_DSP_TADD)) {
2586 printk(KERN_WARNING "mbox: unexpected TADD from DSP!\n");
2590 wake_up_interruptible(&cfg_wait_q);
2593 void mbox_tdel(struct mbcmd *mb)
2597 if ((!waitqueue_active(&cfg_wait_q)) || (cfg_cmd != MBOX_CMD_DSP_TDEL)) {
2598 printk(KERN_WARNING "mbox: unexpected TDEL from DSP!\n");
2602 wake_up_interruptible(&cfg_wait_q);
2605 void mbox_err_fatal(u8 tid)
2607 struct dsptask *task = dsptask[tid];
2608 struct taskdev *dev;
2610 if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2611 printk(KERN_ERR "mbox: FATAL ERR with illegal tid! %d\n", tid);
2615 /* wake up waiting processes */
2617 wake_up_interruptible_all(&dev->read_wait_q);
2618 wake_up_interruptible_all(&dev->write_wait_q);
2619 wake_up_interruptible_all(&dev->tctl_wait_q);
2622 static u16 *dbg_buf;
2623 static u16 dbg_buf_sz, dbg_line_sz;
2626 int dsp_dbg_config(u16 *buf, u16 sz, u16 lsz)
2628 #ifdef OLD_BINARY_SUPPORT
2629 if ((mbox_revision == MBREV_3_0) || (mbox_revision == MBREV_3_2)) {
2638 if (dsp_address_validate(buf, sz, "debug buffer") < 0)
2643 "omapdsp: dbg_buf lsz (%d) is greater than its "
2644 "buffer size (%d)\n", lsz, sz);
2656 void dsp_dbg_stop(void)
2661 #ifdef OLD_BINARY_SUPPORT
2662 static void mbox_dbg_old(struct mbcmd *mb);
2665 void mbox_dbg(struct mbcmd *mb)
2669 char s[80], *s_end = &s[79], *p;
2673 #ifdef OLD_BINARY_SUPPORT
2674 if ((mbox_revision == MBREV_3_0) || (mbox_revision == MBREV_3_2)) {
2680 if (((tid >= TASKDEV_MAX) || (dsptask[tid] == NULL)) &&
2681 (tid != TID_ANON)) {
2682 printk(KERN_ERR "mbox: DBG with illegal tid! %d\n", tid);
2685 if (dbg_buf == NULL) {
2686 printk(KERN_ERR "mbox: DBG command received, but "
2687 "dbg_buf has not been configured yet.\n");
2691 if (dsp_mem_enable(dbg_buf) < 0)
2694 src = &dbg_buf[dbg_rp];
2696 for (i = 0; i < cnt; i++) {
2699 * Be carefull that dbg_buf should not be read with
2700 * 1-byte access since it might be placed in DARAM/SARAM
2701 * and it can cause unexpected byteswap.
2703 * *(p++) = *(src++) & 0xff;
2704 * causes 1-byte access!
2707 *(p++) = tmp & 0xff;
2708 if (*(p-1) == '\n') {
2725 if ((dbg_rp += cnt + 1) > dbg_buf_sz - dbg_line_sz)
2728 dsp_mem_disable(dbg_buf);
2731 #ifdef OLD_BINARY_SUPPORT
2732 static void mbox_dbg_old(struct mbcmd *mb)
2735 char s[80], *s_end = &s[79], *p;
2741 if (((tid >= TASKDEV_MAX) || (dsptask[tid] == NULL)) &&
2742 (tid != TID_ANON)) {
2743 printk(KERN_ERR "mbox: DBG with illegal tid! %d\n", tid);
2746 if (dsp_mem_enable(ipbuf_sys_da) < 0) {
2747 printk(KERN_ERR "mbox: DBG - ipbuf_sys_da read failed!\n");
2750 if (sync_with_dsp(&ipbuf_sys_da->s, tid, 10) < 0) {
2751 printk(KERN_ERR "mbox: DBG - IPBUF sync failed!\n");
2754 buf = ipbuf_sys_da->d;
2756 src = MKVIRT(buf[1], buf[2]);
2757 if (dsp_address_validate(src, cnt, "dbg buffer") < 0)
2760 if (dsp_mem_enable(src) < 0)
2764 for (i = 0; i < cnt; i++) {
2767 * Be carefull that ipbuf should not be read with
2768 * 1-byte access since it might be placed in DARAM/SARAM
2769 * and it can cause unexpected byteswap.
2771 * *(p++) = *(src++) & 0xff;
2772 * causes 1-byte access!
2775 *(p++) = tmp & 0xff;
2776 if (*(p-1) == '\n') {
2794 dsp_mem_disable(src);
2796 release_ipbuf_pvt(ipbuf_sys_da);
2798 dsp_mem_disable(ipbuf_sys_da);
2800 #endif /* OLD_BINARY_SUPPORT */
2803 * sysfs files: for each device
2807 static ssize_t devname_show(struct device *d, struct device_attribute *attr,
2810 return sprintf(buf, "%s\n", to_taskdev(d)->name);
2814 static ssize_t devstate_show(struct device *d, struct device_attribute *attr,
2817 return sprintf(buf, "%s\n", devstate_name(to_taskdev(d)->state));
2821 static ssize_t proc_list_show(struct device *d, struct device_attribute *attr,
2824 struct taskdev *dev;
2825 struct proc_list *pl;
2828 dev = to_taskdev(d);
2829 spin_lock(&dev->proc_list_lock);
2830 list_for_each_entry(pl, &dev->proc_list, list_head) {
2831 /* need to lock tasklist_lock before calling
2832 * find_task_by_pid_type. */
2833 if (find_task_by_pid_type_ns(PIDTYPE_PID, pl->pid, &init_pid_ns) != NULL)
2834 len += sprintf(buf + len, "%d\n", pl->pid);
2835 read_unlock(&tasklist_lock);
2837 spin_unlock(&dev->proc_list_lock);
2843 static ssize_t taskname_show(struct device *d, struct device_attribute *attr,
2846 struct taskdev *dev = to_taskdev(d);
2849 if (!devstate_read_lock_and_test(dev, TASKDEV_ST_ATTACHED))
2852 len = sprintf(buf, "%s\n", dev->task->name);
2854 devstate_read_unlock(dev);
2859 static ssize_t ttyp_show(struct device *d, struct device_attribute *attr,
2862 struct taskdev *dev = to_taskdev(d);
2866 if (!devstate_read_lock_and_test(dev, TASKDEV_ST_ATTACHED))
2869 ttyp = dev->task->ttyp;
2870 len += sprintf(buf + len, "0x%04x\n", ttyp);
2871 len += sprintf(buf + len, "%s %s send\n",
2872 (sndtyp_acv(ttyp)) ? "active" :
2874 (sndtyp_wd(ttyp)) ? "word" :
2875 (sndtyp_pvt(ttyp)) ? "private block" :
2877 len += sprintf(buf + len, "%s %s receive\n",
2878 (rcvtyp_acv(ttyp)) ? "active" :
2880 (rcvtyp_wd(ttyp)) ? "word" :
2881 (rcvtyp_pvt(ttyp)) ? "private block" :
2884 devstate_read_unlock(dev);
2889 static ssize_t fifosz_show(struct device *d, struct device_attribute *attr,
2892 struct kfifo *fifo = to_taskdev(d)->rcvdt.fifo;
2893 return sprintf(buf, "%d\n", fifo->size);
2896 static int fifosz_store(struct device *d, struct device_attribute *attr,
2897 const char *buf, size_t count)
2899 struct taskdev *dev = to_taskdev(d);
2900 unsigned long fifosz;
2903 fifosz = simple_strtol(buf, NULL, 10);
2904 if (taskdev_lock_and_statelock_attached(dev, &dev->read_mutex))
2906 ret = taskdev_set_fifosz(dev, fifosz);
2907 taskdev_unlock_and_stateunlock(dev, &dev->read_mutex);
2909 return (ret < 0) ? ret : strlen(buf);
2913 static ssize_t fifocnt_show(struct device *d, struct device_attribute *attr,
2916 struct kfifo *fifo = to_taskdev(d)->rcvdt.fifo;
2917 return sprintf(buf, "%d\n", fifo->size);
2921 static inline char *bid_name(u16 bid)
2931 sprintf(s, "%d", bid);
2936 static ssize_t ipblink_show(struct device *d, struct device_attribute *attr,
2939 struct rcvdt_bk_struct *rcvdt = &to_taskdev(d)->rcvdt.bk;
2942 spin_lock(&rcvdt->link.lock);
2943 len = sprintf(buf, "top %s\ntail %s\n",
2944 bid_name(rcvdt->link.top), bid_name(rcvdt->link.tail));
2945 spin_unlock(&rcvdt->link.lock);
2951 static ssize_t wsz_show(struct device *d, struct device_attribute *attr,
2954 return sprintf(buf, "%d\n", to_taskdev(d)->wsz);
2958 static ssize_t mmap_show(struct device *d, struct device_attribute *attr,
2961 struct dsptask *task = to_taskdev(d)->task;
2962 return sprintf(buf, "0x%p 0x%x\n", task->map_base, task->map_length);
2966 * called from ipbuf_show()
2968 int ipbuf_is_held(u8 tid, u16 bid)
2970 struct dsptask *task = dsptask[tid];
2971 struct ipblink *link;
2978 link = &task->dev->rcvdt.bk.link;
2979 spin_lock(&link->lock);
2980 ipblink_for_each(b, link) {
2981 if (b == bid) { /* found */
2986 spin_unlock(&link->lock);
2991 int __init dsp_taskmod_init(void)
2995 memset(taskdev, 0, sizeof(void *) * TASKDEV_MAX);
2996 memset(dsptask, 0, sizeof(void *) * TASKDEV_MAX);
2998 retval = register_chrdev(OMAP_DSP_TASK_MAJOR, "dsptask",
3002 "omapdsp: failed to register task device: %d\n", retval);
3006 retval = bus_register(&dsptask_bus);
3009 "omapdsp: failed to register DSP task bus: %d\n",
3011 unregister_chrdev(OMAP_DSP_TASK_MAJOR, "dsptask");
3014 retval = driver_register(&dsptask_driver);
3017 "omapdsp: failed to register DSP task driver: %d\n",
3019 bus_unregister(&dsptask_bus);
3020 unregister_chrdev(OMAP_DSP_TASK_MAJOR, "dsptask");
3023 dsp_task_class = class_create(THIS_MODULE, "dsptask");
3024 if (IS_ERR(dsp_task_class)) {
3025 printk(KERN_ERR "omapdsp: failed to create DSP task class\n");
3026 driver_unregister(&dsptask_driver);
3027 bus_unregister(&dsptask_bus);
3028 unregister_chrdev(OMAP_DSP_TASK_MAJOR, "dsptask");
3035 void dsp_taskmod_exit(void)
3037 class_destroy(dsp_task_class);
3038 driver_unregister(&dsptask_driver);
3039 bus_unregister(&dsptask_bus);
3040 unregister_chrdev(OMAP_DSP_TASK_MAJOR, "dsptask");