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 <asm/arch/mailbox.h>
40 #include <asm/arch/dsp.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 struct page *dsp_task_mmap_nopage(struct vm_area_struct *vma,
1315 unsigned long address, int *type)
1317 return NOPAGE_SIGBUS;
1320 static struct vm_operations_struct dsp_task_vm_ops = {
1321 .open = dsp_task_mmap_open,
1322 .close = dsp_task_mmap_close,
1323 .nopage = dsp_task_mmap_nopage,
1326 static int dsp_task_mmap(struct file *filp, struct vm_area_struct *vma)
1329 unsigned long tmp_padr, tmp_vmadr, off;
1330 size_t req_len, tmp_len;
1331 unsigned int minor = MINOR(filp->f_dentry->d_inode->i_rdev);
1332 struct taskdev *dev = taskdev[minor];
1333 struct dsptask *task;
1336 if (!devstate_read_lock_and_test(dev, TASKDEV_ST_ATTACHED))
1341 * Don't swap this area out
1342 * Don't dump this area to a core file
1344 vma->vm_flags |= VM_RESERVED | VM_IO;
1346 /* Do not cache this area */
1347 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1349 req_len = vma->vm_end - vma->vm_start;
1350 off = vma->vm_pgoff << PAGE_SHIFT;
1351 tmp_vmadr = vma->vm_start;
1352 tmp_vadr = task->map_base + off;
1354 tmp_padr = omap_mmu_virt_to_phys(&dsp_mmu, tmp_vadr, &tmp_len);
1355 if (tmp_padr == 0) {
1357 "omapdsp: task %s: illegal address "
1358 "for mmap: %p", task->name, tmp_vadr);
1359 /* partial mapping will be cleared in upper layer */
1363 if (tmp_len > req_len)
1366 pr_debug("omapdsp: mmap info: "
1367 "vmadr = %08lx, padr = %08lx, len = %x\n",
1368 tmp_vmadr, tmp_padr, tmp_len);
1369 if (remap_pfn_range(vma, tmp_vmadr, tmp_padr >> PAGE_SHIFT,
1370 tmp_len, vma->vm_page_prot) != 0) {
1372 "omapdsp: task %s: remap_page_range() failed.\n",
1374 /* partial mapping will be cleared in upper layer */
1380 tmp_vmadr += tmp_len;
1381 tmp_vadr += tmp_len;
1384 vma->vm_ops = &dsp_task_vm_ops;
1385 vma->vm_private_data = dev;
1386 omap_mmu_exmap_use(&dsp_mmu, task->map_base, vma->vm_end - vma->vm_start);
1389 devstate_read_unlock(dev);
1393 static int dsp_task_open(struct inode *inode, struct file *file)
1395 unsigned int minor = MINOR(inode->i_rdev);
1396 struct taskdev *dev;
1399 if ((minor >= TASKDEV_MAX) || ((dev = taskdev[minor]) == NULL))
1403 mutex_lock(&dev->usecount_lock);
1404 down_write(&dev->state_sem);
1406 /* state can be NOTASK, ATTACHED/FREEZED, KILLING, GARBAGE or INVALID here. */
1407 switch (dev->state & TASKDEV_ST_STATE_MASK) {
1408 case TASKDEV_ST_NOTASK:
1410 case TASKDEV_ST_ATTACHED:
1413 case TASKDEV_ST_INVALID:
1414 up_write(&dev->state_sem);
1415 mutex_unlock(&dev->usecount_lock);
1418 case TASKDEV_ST_FREEZED:
1419 case TASKDEV_ST_KILLING:
1420 case TASKDEV_ST_GARBAGE:
1421 case TASKDEV_ST_DELREQ:
1422 /* on the kill process. wait until it becomes NOTASK. */
1423 up_write(&dev->state_sem);
1424 mutex_unlock(&dev->usecount_lock);
1425 if (devstate_write_lock(dev, TASKDEV_ST_NOTASK) < 0)
1427 devstate_write_unlock(dev);
1432 set_taskdev_state(dev, TASKDEV_ST_ADDREQ);
1433 /* wake up twch daemon for tadd */
1435 up_write(&dev->state_sem);
1436 if (devstate_write_lock(dev, TASKDEV_ST_ATTACHED |
1437 TASKDEV_ST_ADDFAIL) < 0) {
1439 if (!devstate_write_lock_and_test(dev, TASKDEV_ST_ADDREQ)) {
1440 mutex_unlock(&dev->usecount_lock);
1441 /* out of control ??? */
1444 set_taskdev_state(dev, TASKDEV_ST_NOTASK);
1448 if (dev->state & TASKDEV_ST_ADDFAIL) {
1449 printk(KERN_ERR "omapdsp: task attach failed for %s!\n",
1452 set_taskdev_state(dev, TASKDEV_ST_NOTASK);
1457 ret = proc_list_add(&dev->proc_list_lock,
1458 &dev->proc_list, current, file);
1463 file->f_op = &dev->fops;
1464 up_write(&dev->state_sem);
1465 mutex_unlock(&dev->usecount_lock);
1467 #ifdef DSP_PTE_FREE /* not used currently. */
1468 dsp_map_update(current);
1469 dsp_cur_users_add(current);
1470 #endif /* DSP_PTE_FREE */
1474 wake_up_interruptible_all(&dev->state_wait_q);
1476 up_write(&dev->state_sem);
1477 mutex_unlock(&dev->usecount_lock);
1481 static int dsp_task_release(struct inode *inode, struct file *file)
1483 unsigned int minor = MINOR(inode->i_rdev);
1484 struct taskdev *dev = taskdev[minor];
1486 #ifdef DSP_PTE_FREE /* not used currently. */
1487 dsp_cur_users_del(current);
1488 #endif /* DSP_PTE_FREE */
1490 if (has_taskdev_lock(dev))
1491 taskdev_unlock(dev);
1493 proc_list_del(&dev->proc_list_lock, &dev->proc_list, current, file);
1494 mutex_lock(&dev->usecount_lock);
1495 if (--dev->usecount > 0) {
1496 /* other processes are using this device. no state change. */
1497 mutex_unlock(&dev->usecount_lock);
1502 down_write(&dev->state_sem);
1504 /* state can be ATTACHED/FREEZED, KILLING or GARBAGE here. */
1505 switch (dev->state & TASKDEV_ST_STATE_MASK) {
1507 case TASKDEV_ST_KILLING:
1510 case TASKDEV_ST_GARBAGE:
1511 set_taskdev_state(dev, TASKDEV_ST_NOTASK);
1512 wake_up_interruptible_all(&dev->state_wait_q);
1515 case TASKDEV_ST_ATTACHED:
1516 case TASKDEV_ST_FREEZED:
1517 if (is_dynamic_task(minor)) {
1518 set_taskdev_state(dev, TASKDEV_ST_DELREQ);
1519 /* wake up twch daemon for tdel */
1526 up_write(&dev->state_sem);
1527 mutex_unlock(&dev->usecount_lock);
1534 int dsp_mkdev(char *name)
1536 struct taskdev *dev;
1538 unsigned char minor;
1541 if (dsp_cfgstat_get_stat() != CFGSTAT_READY) {
1542 printk(KERN_ERR "omapdsp: dsp has not been configured.\n");
1546 if (mutex_lock_interruptible(&devmgr_lock))
1550 for (minor = 0; minor < TASKDEV_MAX; minor++) {
1551 if (taskdev[minor] && !strcmp(taskdev[minor]->name, name)) {
1553 "omapdsp: task device name %s is already "
1560 /* find free minor number */
1561 for (minor = n_task; minor < TASKDEV_MAX; minor++) {
1562 if (taskdev[minor] == NULL)
1565 printk(KERN_ERR "omapdsp: Too many task devices.\n");
1570 if ((dev = kzalloc(sizeof(struct taskdev), GFP_KERNEL)) == NULL) {
1574 if ((status = taskdev_init(dev, name, minor)) < 0) {
1582 mutex_unlock(&devmgr_lock);
1586 int dsp_rmdev(char *name)
1588 unsigned char minor;
1592 if (dsp_cfgstat_get_stat() != CFGSTAT_READY) {
1593 printk(KERN_ERR "omapdsp: dsp has not been configured.\n");
1597 if (mutex_lock_interruptible(&devmgr_lock))
1600 /* find in dynamic devices */
1601 for (minor = n_task; minor < TASKDEV_MAX; minor++) {
1602 if (taskdev[minor] && !strcmp(taskdev[minor]->name, name))
1606 /* find in static devices */
1607 for (minor = 0; minor < n_task; minor++) {
1608 if (taskdev[minor] && !strcmp(taskdev[minor]->name, name)) {
1610 "omapdsp: task device %s is static.\n", name);
1616 printk(KERN_ERR "omapdsp: task device %s not found.\n", name);
1621 if ((status = dsp_rmdev_minor(minor)) < 0)
1624 mutex_unlock(&devmgr_lock);
1628 static int dsp_rmdev_minor(unsigned char minor)
1630 struct taskdev *dev = taskdev[minor];
1632 while (!down_write_trylock(&dev->state_sem)) {
1633 down_read(&dev->state_sem);
1634 if (dev->state & (TASKDEV_ST_ATTACHED |
1635 TASKDEV_ST_FREEZED)) {
1637 * task is working. kill it.
1638 * ATTACHED -> FREEZED can be changed under
1639 * down_read of state_sem..
1641 set_taskdev_state(dev, TASKDEV_ST_FREEZED);
1642 wake_up_interruptible_all(&dev->read_wait_q);
1643 wake_up_interruptible_all(&dev->write_wait_q);
1644 wake_up_interruptible_all(&dev->tctl_wait_q);
1646 up_read(&dev->state_sem);
1650 switch (dev->state & TASKDEV_ST_STATE_MASK) {
1652 case TASKDEV_ST_NOTASK:
1653 case TASKDEV_ST_INVALID:
1657 case TASKDEV_ST_ATTACHED:
1658 case TASKDEV_ST_FREEZED:
1659 /* task is working. kill it. */
1660 set_taskdev_state(dev, TASKDEV_ST_KILLING);
1661 up_write(&dev->state_sem);
1662 dsp_tdel_bh(dev, TDEL_KILL);
1665 case TASKDEV_ST_ADDREQ:
1666 /* open() is waiting. drain it. */
1667 set_taskdev_state(dev, TASKDEV_ST_ADDFAIL);
1668 wake_up_interruptible_all(&dev->state_wait_q);
1671 case TASKDEV_ST_DELREQ:
1672 /* nobody is waiting. */
1673 set_taskdev_state(dev, TASKDEV_ST_NOTASK);
1674 wake_up_interruptible_all(&dev->state_wait_q);
1677 case TASKDEV_ST_ADDING:
1678 case TASKDEV_ST_DELING:
1679 case TASKDEV_ST_KILLING:
1680 case TASKDEV_ST_GARBAGE:
1681 case TASKDEV_ST_ADDFAIL:
1682 /* transient state. wait for a moment. */
1687 up_write(&dev->state_sem);
1690 /* wait for some time and hope the state is settled */
1691 devstate_read_lock_timeout(dev, TASKDEV_ST_NOTASK, 5 * HZ);
1692 if (!(dev->state & TASKDEV_ST_NOTASK)) {
1694 "omapdsp: illegal device state (%s) on rmdev %s.\n",
1695 devstate_name(dev->state), dev->name);
1698 set_taskdev_state(dev, TASKDEV_ST_INVALID);
1699 devstate_read_unlock(dev);
1701 taskdev_delete(minor);
1707 static struct file_operations dsp_task_fops = {
1708 .owner = THIS_MODULE,
1709 .poll = dsp_task_poll,
1710 .ioctl = dsp_task_ioctl,
1711 .open = dsp_task_open,
1712 .release = dsp_task_release,
1715 static void dsptask_dev_release(struct device *dev)
1719 static int taskdev_init(struct taskdev *dev, char *name, unsigned char minor)
1722 struct device *task_dev;
1724 taskdev[minor] = dev;
1726 spin_lock_init(&dev->proc_list_lock);
1727 INIT_LIST_HEAD(&dev->proc_list);
1728 init_waitqueue_head(&dev->read_wait_q);
1729 init_waitqueue_head(&dev->write_wait_q);
1730 init_waitqueue_head(&dev->tctl_wait_q);
1731 mutex_init(&dev->read_mutex);
1732 mutex_init(&dev->write_mutex);
1733 mutex_init(&dev->tctl_mutex);
1734 mutex_init(&dev->lock);
1735 spin_lock_init(&dev->wsz_lock);
1736 dev->tctl_ret = -EINVAL;
1739 strncpy(dev->name, name, TNM_LEN);
1740 dev->name[TNM_LEN-1] = '\0';
1741 set_taskdev_state(dev, (minor < n_task) ? TASKDEV_ST_ATTACHED : TASKDEV_ST_NOTASK);
1743 mutex_init(&dev->usecount_lock);
1744 memcpy(&dev->fops, &dsp_task_fops, sizeof(struct file_operations));
1746 dev->dev.parent = omap_dsp->dev;
1747 dev->dev.bus = &dsptask_bus;
1748 sprintf(dev->dev.bus_id, "dsptask%d", minor);
1749 dev->dev.release = dsptask_dev_release;
1750 ret = device_register(&dev->dev);
1752 printk(KERN_ERR "device_register failed: %d\n", ret);
1755 ret = device_create_file(&dev->dev, &dev_attr_devname);
1757 goto fail_create_devname;
1758 ret = device_create_file(&dev->dev, &dev_attr_devstate);
1760 goto fail_create_devstate;
1761 ret = device_create_file(&dev->dev, &dev_attr_proc_list);
1763 goto fail_create_proclist;
1765 task_dev = device_create(dsp_task_class, NULL,
1766 MKDEV(OMAP_DSP_TASK_MAJOR, minor),
1767 "dsptask%d", (int)minor);
1769 if (unlikely(IS_ERR(task_dev))) {
1771 goto fail_create_taskclass;
1774 init_waitqueue_head(&dev->state_wait_q);
1775 init_rwsem(&dev->state_sem);
1779 fail_create_taskclass:
1780 device_remove_file(&dev->dev, &dev_attr_proc_list);
1781 fail_create_proclist:
1782 device_remove_file(&dev->dev, &dev_attr_devstate);
1783 fail_create_devstate:
1784 device_remove_file(&dev->dev, &dev_attr_devname);
1785 fail_create_devname:
1786 device_unregister(&dev->dev);
1790 static void taskdev_delete(unsigned char minor)
1792 struct taskdev *dev = taskdev[minor];
1796 device_remove_file(&dev->dev, &dev_attr_devname);
1797 device_remove_file(&dev->dev, &dev_attr_devstate);
1798 device_remove_file(&dev->dev, &dev_attr_proc_list);
1799 device_destroy(dsp_task_class, MKDEV(OMAP_DSP_TASK_MAJOR, minor));
1800 device_unregister(&dev->dev);
1801 proc_list_flush(&dev->proc_list_lock, &dev->proc_list);
1802 taskdev[minor] = NULL;
1805 static int taskdev_attach_task(struct taskdev *dev, struct dsptask *task)
1807 u16 ttyp = task->ttyp;
1812 sndtyp_wd(ttyp) ? dsp_task_read_wd_acv:
1813 /* sndtyp_bk */ dsp_task_read_bk_acv:
1815 sndtyp_wd(ttyp) ? dsp_task_read_wd_psv:
1816 /* sndtyp_bk */ dsp_task_read_bk_psv;
1817 if (sndtyp_wd(ttyp)) {
1819 size_t fifosz = sndtyp_psv(ttyp) ? 2:32; /* passive:active */
1821 dev->rcvdt.fifo = kfifo_alloc(fifosz, GFP_KERNEL,
1823 if (IS_ERR(dev->rcvdt.fifo)) {
1825 "omapdsp: unable to allocate receive buffer. "
1826 "(%d bytes for %s)\n", fifosz, dev->name);
1831 INIT_IPBLINK(&dev->rcvdt.bk.link);
1832 dev->rcvdt.bk.rp = 0;
1836 rcvtyp_wd(ttyp) ? dsp_task_write_wd:
1837 /* rcvbyp_bk */ dsp_task_write_bk;
1838 dev->wsz = rcvtyp_acv(ttyp) ? 0 : /* active */
1839 rcvtyp_wd(ttyp) ? 2 : /* passive word */
1840 ipbcfg.lsz*2; /* passive block */
1842 if (task->map_length)
1843 dev->fops.mmap = dsp_task_mmap;
1845 ret = device_create_file(&dev->dev, &dev_attr_taskname);
1847 goto fail_create_taskname;
1848 ret = device_create_file(&dev->dev, &dev_attr_ttyp);
1850 goto fail_create_ttyp;
1851 ret = device_create_file(&dev->dev, &dev_attr_wsz);
1853 goto fail_create_wsz;
1854 if (task->map_length) {
1855 ret = device_create_file(&dev->dev, &dev_attr_mmap);
1857 goto fail_create_mmap;
1859 if (sndtyp_wd(ttyp)) {
1860 ret = device_create_file(&dev->dev, &dev_attr_fifosz);
1862 goto fail_create_fifosz;
1863 ret = device_create_file(&dev->dev, &dev_attr_fifocnt);
1865 goto fail_create_fifocnt;
1867 ret = device_create_file(&dev->dev, &dev_attr_ipblink);
1869 goto fail_create_ipblink;
1877 fail_create_fifocnt:
1878 device_remove_file(&dev->dev, &dev_attr_fifosz);
1879 fail_create_ipblink:
1881 if (task->map_length)
1882 device_remove_file(&dev->dev, &dev_attr_mmap);
1884 device_remove_file(&dev->dev, &dev_attr_wsz);
1886 device_remove_file(&dev->dev, &dev_attr_ttyp);
1888 device_remove_file(&dev->dev, &dev_attr_taskname);
1889 fail_create_taskname:
1890 if (task->map_length)
1891 dev->fops.mmap = NULL;
1893 dev->fops.write = NULL;
1896 dev->fops.read = NULL;
1897 taskdev_flush_buf(dev);
1899 if (sndtyp_wd(ttyp))
1900 kfifo_free(dev->rcvdt.fifo);
1907 static void taskdev_detach_task(struct taskdev *dev)
1909 u16 ttyp = dev->task->ttyp;
1911 device_remove_file(&dev->dev, &dev_attr_taskname);
1912 device_remove_file(&dev->dev, &dev_attr_ttyp);
1913 if (sndtyp_wd(ttyp)) {
1914 device_remove_file(&dev->dev, &dev_attr_fifosz);
1915 device_remove_file(&dev->dev, &dev_attr_fifocnt);
1917 device_remove_file(&dev->dev, &dev_attr_ipblink);
1918 device_remove_file(&dev->dev, &dev_attr_wsz);
1919 if (dev->task->map_length) {
1920 device_remove_file(&dev->dev, &dev_attr_mmap);
1921 dev->fops.mmap = NULL;
1924 dev->fops.read = NULL;
1925 taskdev_flush_buf(dev);
1926 if (sndtyp_wd(ttyp))
1927 kfifo_free(dev->rcvdt.fifo);
1929 dev->fops.write = NULL;
1932 pr_info("omapdsp: taskdev %s disabled.\n", dev->name);
1937 * tadd / tdel / tkill
1939 static int dsp_tadd(struct taskdev *dev, dsp_long_t adr)
1941 struct dsptask *task;
1942 struct mb_exarg arg;
1943 u8 tid, tid_response;
1947 if (!devstate_write_lock_and_test(dev, TASKDEV_ST_ADDREQ)) {
1949 "omapdsp: taskdev %s is not requesting for tadd. "
1950 "(state is %s)\n", dev->name, devstate_name(dev->state));
1953 set_taskdev_state(dev, TASKDEV_ST_ADDING);
1954 devstate_write_unlock(dev);
1956 if (adr == TADD_ABORTADR) {
1957 /* aborting tadd intentionally */
1958 pr_info("omapdsp: tadd address is ABORTADR.\n");
1961 if (adr >= DSPSPACE_SIZE) {
1963 "omapdsp: illegal address 0x%08x for tadd\n", adr);
1968 adr >>= 1; /* word address */
1969 argv[0] = adr >> 16; /* addrh */
1970 argv[1] = adr & 0xffff; /* addrl */
1972 if (mutex_lock_interruptible(&cfg_lock)) {
1977 cfg_cmd = MBOX_CMD_DSP_TADD;
1982 if (dsp_mem_sync_inc() < 0) {
1983 printk(KERN_ERR "omapdsp: memory sync failed!\n");
1987 mbcompose_send_and_wait_exarg(TADD, 0, 0, &arg, &cfg_wait_q);
1992 mutex_unlock(&cfg_lock);
1994 if (tid == TID_ANON) {
1995 printk(KERN_ERR "omapdsp: tadd failed!\n");
1999 if ((tid < n_task) || dsptask[tid]) {
2000 printk(KERN_ERR "omapdsp: illegal tid (%d)!\n", tid);
2004 if ((task = kzalloc(sizeof(struct dsptask), GFP_KERNEL)) == NULL) {
2009 if ((ret = dsp_task_config(task, tid)) < 0)
2012 if (strcmp(dev->name, task->name)) {
2014 "omapdsp: task name (%s) doesn't match with "
2015 "device name (%s).\n", task->name, dev->name);
2020 if ((ret = taskdev_attach_task(dev, task)) < 0)
2023 dsp_task_init(task);
2024 pr_info("omapdsp: taskdev %s enabled.\n", dev->name);
2025 set_taskdev_state(dev, TASKDEV_ST_ATTACHED);
2026 wake_up_interruptible_all(&dev->state_wait_q);
2033 printk(KERN_ERR "omapdsp: deleting the task...\n");
2035 set_taskdev_state(dev, TASKDEV_ST_DELING);
2037 if (mutex_lock_interruptible(&cfg_lock)) {
2038 printk(KERN_ERR "omapdsp: aborting tdel process. "
2039 "DSP side could be corrupted.\n");
2043 cfg_cmd = MBOX_CMD_DSP_TDEL;
2044 mbcompose_send_and_wait(TDEL, tid, TDEL_KILL, &cfg_wait_q);
2045 tid_response = cfg_tid;
2048 mutex_unlock(&cfg_lock);
2050 if (tid_response != tid)
2051 printk(KERN_ERR "omapdsp: tdel failed. "
2052 "DSP side could be corrupted.\n");
2055 set_taskdev_state(dev, TASKDEV_ST_ADDFAIL);
2056 wake_up_interruptible_all(&dev->state_wait_q);
2060 int dsp_tadd_minor(unsigned char minor, dsp_long_t adr)
2062 struct taskdev *dev;
2066 if (mutex_lock_interruptible(&devmgr_lock))
2069 if ((minor >= TASKDEV_MAX) || ((dev = taskdev[minor]) == NULL)) {
2071 "omapdsp: no task device with minor %d\n", minor);
2076 if ((status = dsp_tadd(dev, adr)) < 0)
2080 mutex_unlock(&devmgr_lock);
2084 static int dsp_tdel(struct taskdev *dev)
2086 if (!devstate_write_lock_and_test(dev, TASKDEV_ST_DELREQ)) {
2088 "omapdsp: taskdev %s is not requesting for tdel. "
2089 "(state is %s)\n", dev->name, devstate_name(dev->state));
2092 set_taskdev_state(dev, TASKDEV_ST_DELING);
2093 devstate_write_unlock(dev);
2095 return dsp_tdel_bh(dev, TDEL_SAFE);
2098 int dsp_tdel_minor(unsigned char minor)
2100 struct taskdev *dev;
2104 if (mutex_lock_interruptible(&devmgr_lock))
2107 if ((minor >= TASKDEV_MAX) || ((dev = taskdev[minor]) == NULL)) {
2109 "omapdsp: no task device with minor %d\n", minor);
2115 if ((status = dsp_tdel(dev)) < 0)
2119 mutex_unlock(&devmgr_lock);
2123 static int dsp_tkill(struct taskdev *dev)
2125 while (!down_write_trylock(&dev->state_sem)) {
2126 if (!devstate_read_lock_and_test(dev, (TASKDEV_ST_ATTACHED |
2127 TASKDEV_ST_FREEZED))) {
2129 "omapdsp: task has not been attached for "
2130 "taskdev %s\n", dev->name);
2133 /* ATTACHED -> FREEZED can be changed under read semaphore. */
2134 set_taskdev_state(dev, TASKDEV_ST_FREEZED);
2135 wake_up_interruptible_all(&dev->read_wait_q);
2136 wake_up_interruptible_all(&dev->write_wait_q);
2137 wake_up_interruptible_all(&dev->tctl_wait_q);
2138 devstate_read_unlock(dev);
2142 if (!(dev->state & (TASKDEV_ST_ATTACHED |
2143 TASKDEV_ST_FREEZED))) {
2145 "omapdsp: task has not been attached for taskdev %s\n",
2147 devstate_write_unlock(dev);
2150 if (!is_dynamic_task(dev->task->tid)) {
2151 printk(KERN_ERR "omapdsp: task %s is not a dynamic task.\n",
2153 devstate_write_unlock(dev);
2156 set_taskdev_state(dev, TASKDEV_ST_KILLING);
2157 devstate_write_unlock(dev);
2159 return dsp_tdel_bh(dev, TDEL_KILL);
2162 int dsp_tkill_minor(unsigned char minor)
2164 struct taskdev *dev;
2168 if (mutex_lock_interruptible(&devmgr_lock))
2171 if ((minor >= TASKDEV_MAX) || ((dev = taskdev[minor]) == NULL)) {
2173 "omapdsp: no task device with minor %d\n", minor);
2179 if ((status = dsp_tkill(dev)) < 0)
2183 mutex_unlock(&devmgr_lock);
2187 static int dsp_tdel_bh(struct taskdev *dev, u16 type)
2189 struct dsptask *task;
2190 u8 tid, tid_response;
2195 if (mutex_lock_interruptible(&cfg_lock)) {
2196 if (type == TDEL_SAFE) {
2197 set_taskdev_state(dev, TASKDEV_ST_DELREQ);
2200 tid_response = TID_ANON;
2206 cfg_cmd = MBOX_CMD_DSP_TDEL;
2207 mbcompose_send_and_wait(TDEL, tid, type, &cfg_wait_q);
2208 tid_response = cfg_tid;
2211 mutex_unlock(&cfg_lock);
2214 taskdev_detach_task(dev);
2215 dsp_task_unconfig(task);
2218 if (tid_response != tid) {
2219 printk(KERN_ERR "omapdsp: %s failed!\n",
2220 (type == TDEL_SAFE) ? "tdel" : "tkill");
2223 down_write(&dev->state_sem);
2224 set_taskdev_state(dev, (dev->usecount > 0) ? TASKDEV_ST_GARBAGE :
2226 wake_up_interruptible_all(&dev->state_wait_q);
2227 up_write(&dev->state_sem);
2235 long taskdev_state_stale(unsigned char minor)
2237 if (taskdev[minor]) {
2238 long state = taskdev[minor]->state;
2239 taskdev[minor]->state |= TASKDEV_ST_STALE;
2242 return TASKDEV_ST_NOTASK;
2246 * functions called from mailbox interrupt routine
2248 void mbox_wdsnd(struct mbcmd *mb)
2252 u16 data = mb->data;
2253 struct dsptask *task = dsptask[tid];
2255 if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2256 printk(KERN_ERR "mbox: WDSND with illegal tid! %d\n", tid);
2259 if (sndtyp_bk(task->ttyp)) {
2261 "mbox: WDSND from block sending task! (task%d)\n", tid);
2264 if (sndtyp_psv(task->ttyp) &&
2265 !waitqueue_active(&task->dev->read_wait_q)) {
2267 "mbox: WDSND from passive sending task (task%d) "
2268 "without request!\n", tid);
2272 n = kfifo_put(task->dev->rcvdt.fifo, (unsigned char *)&data,
2274 if (n != sizeof(data))
2275 printk(KERN_WARNING "Receive FIFO(%d) is full\n", tid);
2277 wake_up_interruptible(&task->dev->read_wait_q);
2280 void mbox_wdreq(struct mbcmd *mb)
2283 struct dsptask *task = dsptask[tid];
2284 struct taskdev *dev;
2286 if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2287 printk(KERN_ERR "mbox: WDREQ with illegal tid! %d\n", tid);
2290 if (rcvtyp_psv(task->ttyp)) {
2292 "mbox: WDREQ from passive receiving task! (task%d)\n",
2298 spin_lock(&dev->wsz_lock);
2300 spin_unlock(&dev->wsz_lock);
2301 wake_up_interruptible(&dev->write_wait_q);
2304 void mbox_bksnd(struct mbcmd *mb)
2308 struct dsptask *task = dsptask[tid];
2309 struct ipbuf_head *ipb_h;
2312 if (bid >= ipbcfg.ln) {
2313 printk(KERN_ERR "mbox: BKSND with illegal bid! %d\n", bid);
2316 ipb_h = bid_to_ipbuf(bid);
2317 ipb_bsycnt_dec(&ipbcfg);
2318 if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2319 printk(KERN_ERR "mbox: BKSND with illegal tid! %d\n", tid);
2320 goto unuse_ipbuf_out;
2322 if (sndtyp_wd(task->ttyp)) {
2324 "mbox: BKSND from word sending task! (task%d)\n", tid);
2325 goto unuse_ipbuf_out;
2327 if (sndtyp_pvt(task->ttyp)) {
2329 "mbox: BKSND from private sending task! (task%d)\n", tid);
2330 goto unuse_ipbuf_out;
2332 if (sync_with_dsp(&ipb_h->p->sd, tid, 10) < 0) {
2333 printk(KERN_ERR "mbox: BKSND - IPBUF sync failed!\n");
2337 /* should be done in DSP, but just in case. */
2338 ipb_h->p->next = BID_NULL;
2341 if (cnt > ipbcfg.lsz) {
2342 printk(KERN_ERR "mbox: BKSND cnt(%d) > ipbuf line size(%d)!\n",
2344 goto unuse_ipbuf_out;
2348 /* 0-byte send from DSP */
2349 unuse_ipbuf_nowait(ipb_h);
2352 ipblink_add_tail(&task->dev->rcvdt.bk.link, bid);
2353 /* we keep coming bid and return alternative line to DSP. */
2357 wake_up_interruptible(&task->dev->read_wait_q);
2361 unuse_ipbuf_nowait(ipb_h);
2365 void mbox_bkreq(struct mbcmd *mb)
2369 struct dsptask *task = dsptask[tid];
2370 struct taskdev *dev;
2372 if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2373 printk(KERN_ERR "mbox: BKREQ with illegal tid! %d\n", tid);
2376 if (rcvtyp_wd(task->ttyp)) {
2378 "mbox: BKREQ from word receiving task! (task%d)\n", tid);
2381 if (rcvtyp_pvt(task->ttyp)) {
2383 "mbox: BKREQ from private receiving task! (task%d)\n",
2387 if (rcvtyp_psv(task->ttyp)) {
2389 "mbox: BKREQ from passive receiving task! (task%d)\n",
2395 spin_lock(&dev->wsz_lock);
2397 spin_unlock(&dev->wsz_lock);
2398 wake_up_interruptible(&dev->write_wait_q);
2401 void mbox_bkyld(struct mbcmd *mb)
2404 struct ipbuf_head *ipb_h;
2406 if (bid >= ipbcfg.ln) {
2407 printk(KERN_ERR "mbox: BKYLD with illegal bid! %d\n", bid);
2410 ipb_h = bid_to_ipbuf(bid);
2412 /* should be done in DSP, but just in case. */
2413 ipb_h->p->next = BID_NULL;
2415 /* we don't need to sync with DSP */
2416 ipb_bsycnt_dec(&ipbcfg);
2417 release_ipbuf(ipb_h);
2420 void mbox_bksndp(struct mbcmd *mb)
2423 struct dsptask *task = dsptask[tid];
2424 struct ipbuf_p *ipbp;
2426 if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2427 printk(KERN_ERR "mbox: BKSNDP with illegal tid! %d\n", tid);
2430 if (sndtyp_wd(task->ttyp)) {
2432 "mbox: BKSNDP from word sending task! (task%d)\n", tid);
2435 if (sndtyp_gbl(task->ttyp)) {
2437 "mbox: BKSNDP from non-private sending task! (task%d)\n",
2443 * we should not have delayed block at this point
2444 * because read() routine releases the lock of the buffer and
2445 * until then DSP can't send next data.
2448 ipbp = task->ipbuf_pvt_r;
2449 if (sync_with_dsp(&ipbp->s, tid, 10) < 0) {
2450 printk(KERN_ERR "mbox: BKSNDP - IPBUF sync failed!\n");
2453 pr_debug("mbox: ipbuf_pvt_r->a = 0x%08lx\n",
2454 MKLONG(ipbp->ah, ipbp->al));
2455 ipblink_add_pvt(&task->dev->rcvdt.bk.link);
2456 wake_up_interruptible(&task->dev->read_wait_q);
2459 void mbox_bkreqp(struct mbcmd *mb)
2462 struct dsptask *task = dsptask[tid];
2463 struct taskdev *dev;
2464 struct ipbuf_p *ipbp;
2466 if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2467 printk(KERN_ERR "mbox: BKREQP with illegal tid! %d\n", tid);
2470 if (rcvtyp_wd(task->ttyp)) {
2472 "mbox: BKREQP from word receiving task! (task%d)\n", tid);
2475 if (rcvtyp_gbl(task->ttyp)) {
2477 "mbox: BKREQP from non-private receiving task! (task%d)\n", tid);
2480 if (rcvtyp_psv(task->ttyp)) {
2482 "mbox: BKREQP from passive receiving task! (task%d)\n", tid);
2486 ipbp = task->ipbuf_pvt_w;
2487 if (sync_with_dsp(&ipbp->s, TID_FREE, 10) < 0) {
2488 printk(KERN_ERR "mbox: BKREQP - IPBUF sync failed!\n");
2491 pr_debug("mbox: ipbuf_pvt_w->a = 0x%08lx\n",
2492 MKLONG(ipbp->ah, ipbp->al));
2494 spin_lock(&dev->wsz_lock);
2495 dev->wsz = ipbp->c*2;
2496 spin_unlock(&dev->wsz_lock);
2497 wake_up_interruptible(&dev->write_wait_q);
2500 void mbox_tctl(struct mbcmd *mb)
2503 struct dsptask *task = dsptask[tid];
2505 if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2506 printk(KERN_ERR "mbox: TCTL with illegal tid! %d\n", tid);
2510 if (!waitqueue_active(&task->dev->tctl_wait_q)) {
2511 printk(KERN_WARNING "mbox: unexpected TCTL from DSP!\n");
2515 task->dev->tctl_stat = mb->data;
2516 wake_up_interruptible(&task->dev->tctl_wait_q);
2519 void mbox_tcfg(struct mbcmd *mb)
2522 struct dsptask *task = dsptask[tid];
2527 if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2528 printk(KERN_ERR "mbox: TCFG with illegal tid! %d\n", tid);
2531 if ((task->state != TASK_ST_CFGREQ) || (cfg_cmd != MBOX_CMD_DSP_TCFG)) {
2532 printk(KERN_WARNING "mbox: unexpected TCFG from DSP!\n");
2536 if (dsp_mem_enable(ipbuf_sys_da) < 0) {
2537 printk(KERN_ERR "mbox: TCFG - ipbuf_sys_da read failed!\n");
2538 dsp_mem_disable(ipbuf_sys_da);
2541 if (sync_with_dsp(&ipbuf_sys_da->s, tid, 10) < 0) {
2542 printk(KERN_ERR "mbox: TCFG - IPBUF sync failed!\n");
2543 dsp_mem_disable(ipbuf_sys_da);
2548 * read configuration data on system IPBUF
2550 buf = ipbuf_sys_da->d;
2551 task->ttyp = buf[0];
2552 task->ipbuf_pvt_r = MKVIRT(buf[1], buf[2]);
2553 task->ipbuf_pvt_w = MKVIRT(buf[3], buf[4]);
2554 task->map_base = MKVIRT(buf[5], buf[6]);
2555 task->map_length = MKLONG(buf[7], buf[8]) << 1; /* word -> byte */
2556 tnm = MKVIRT(buf[9], buf[10]);
2557 release_ipbuf_pvt(ipbuf_sys_da);
2558 dsp_mem_disable(ipbuf_sys_da);
2561 * copy task name string
2563 if (dsp_address_validate(tnm, TNM_LEN, "task name buffer") < 0) {
2564 task->name[0] = '\0';
2568 for (i = 0; i < TNM_LEN-1; i++) {
2569 /* avoiding byte access */
2571 task->name[i] = tmp & 0x00ff;
2575 task->name[TNM_LEN-1] = '\0';
2577 task->state = TASK_ST_READY;
2579 wake_up_interruptible(&cfg_wait_q);
2582 void mbox_tadd(struct mbcmd *mb)
2586 if ((!waitqueue_active(&cfg_wait_q)) || (cfg_cmd != MBOX_CMD_DSP_TADD)) {
2587 printk(KERN_WARNING "mbox: unexpected TADD from DSP!\n");
2591 wake_up_interruptible(&cfg_wait_q);
2594 void mbox_tdel(struct mbcmd *mb)
2598 if ((!waitqueue_active(&cfg_wait_q)) || (cfg_cmd != MBOX_CMD_DSP_TDEL)) {
2599 printk(KERN_WARNING "mbox: unexpected TDEL from DSP!\n");
2603 wake_up_interruptible(&cfg_wait_q);
2606 void mbox_err_fatal(u8 tid)
2608 struct dsptask *task = dsptask[tid];
2609 struct taskdev *dev;
2611 if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2612 printk(KERN_ERR "mbox: FATAL ERR with illegal tid! %d\n", tid);
2616 /* wake up waiting processes */
2618 wake_up_interruptible_all(&dev->read_wait_q);
2619 wake_up_interruptible_all(&dev->write_wait_q);
2620 wake_up_interruptible_all(&dev->tctl_wait_q);
2623 static u16 *dbg_buf;
2624 static u16 dbg_buf_sz, dbg_line_sz;
2627 int dsp_dbg_config(u16 *buf, u16 sz, u16 lsz)
2629 #ifdef OLD_BINARY_SUPPORT
2630 if ((mbox_revision == MBREV_3_0) || (mbox_revision == MBREV_3_2)) {
2639 if (dsp_address_validate(buf, sz, "debug buffer") < 0)
2644 "omapdsp: dbg_buf lsz (%d) is greater than its "
2645 "buffer size (%d)\n", lsz, sz);
2657 void dsp_dbg_stop(void)
2662 #ifdef OLD_BINARY_SUPPORT
2663 static void mbox_dbg_old(struct mbcmd *mb);
2666 void mbox_dbg(struct mbcmd *mb)
2670 char s[80], *s_end = &s[79], *p;
2674 #ifdef OLD_BINARY_SUPPORT
2675 if ((mbox_revision == MBREV_3_0) || (mbox_revision == MBREV_3_2)) {
2681 if (((tid >= TASKDEV_MAX) || (dsptask[tid] == NULL)) &&
2682 (tid != TID_ANON)) {
2683 printk(KERN_ERR "mbox: DBG with illegal tid! %d\n", tid);
2686 if (dbg_buf == NULL) {
2687 printk(KERN_ERR "mbox: DBG command received, but "
2688 "dbg_buf has not been configured yet.\n");
2692 if (dsp_mem_enable(dbg_buf) < 0)
2695 src = &dbg_buf[dbg_rp];
2697 for (i = 0; i < cnt; i++) {
2700 * Be carefull that dbg_buf should not be read with
2701 * 1-byte access since it might be placed in DARAM/SARAM
2702 * and it can cause unexpected byteswap.
2704 * *(p++) = *(src++) & 0xff;
2705 * causes 1-byte access!
2708 *(p++) = tmp & 0xff;
2709 if (*(p-1) == '\n') {
2726 if ((dbg_rp += cnt + 1) > dbg_buf_sz - dbg_line_sz)
2729 dsp_mem_disable(dbg_buf);
2732 #ifdef OLD_BINARY_SUPPORT
2733 static void mbox_dbg_old(struct mbcmd *mb)
2736 char s[80], *s_end = &s[79], *p;
2742 if (((tid >= TASKDEV_MAX) || (dsptask[tid] == NULL)) &&
2743 (tid != TID_ANON)) {
2744 printk(KERN_ERR "mbox: DBG with illegal tid! %d\n", tid);
2747 if (dsp_mem_enable(ipbuf_sys_da) < 0) {
2748 printk(KERN_ERR "mbox: DBG - ipbuf_sys_da read failed!\n");
2751 if (sync_with_dsp(&ipbuf_sys_da->s, tid, 10) < 0) {
2752 printk(KERN_ERR "mbox: DBG - IPBUF sync failed!\n");
2755 buf = ipbuf_sys_da->d;
2757 src = MKVIRT(buf[1], buf[2]);
2758 if (dsp_address_validate(src, cnt, "dbg buffer") < 0)
2761 if (dsp_mem_enable(src) < 0)
2765 for (i = 0; i < cnt; i++) {
2768 * Be carefull that ipbuf should not be read with
2769 * 1-byte access since it might be placed in DARAM/SARAM
2770 * and it can cause unexpected byteswap.
2772 * *(p++) = *(src++) & 0xff;
2773 * causes 1-byte access!
2776 *(p++) = tmp & 0xff;
2777 if (*(p-1) == '\n') {
2795 dsp_mem_disable(src);
2797 release_ipbuf_pvt(ipbuf_sys_da);
2799 dsp_mem_disable(ipbuf_sys_da);
2801 #endif /* OLD_BINARY_SUPPORT */
2804 * sysfs files: for each device
2808 static ssize_t devname_show(struct device *d, struct device_attribute *attr,
2811 return sprintf(buf, "%s\n", to_taskdev(d)->name);
2815 static ssize_t devstate_show(struct device *d, struct device_attribute *attr,
2818 return sprintf(buf, "%s\n", devstate_name(to_taskdev(d)->state));
2822 static ssize_t proc_list_show(struct device *d, struct device_attribute *attr,
2825 struct taskdev *dev;
2826 struct proc_list *pl;
2829 dev = to_taskdev(d);
2830 spin_lock(&dev->proc_list_lock);
2831 list_for_each_entry(pl, &dev->proc_list, list_head) {
2832 /* need to lock tasklist_lock before calling
2833 * find_task_by_pid_type. */
2834 if (find_task_by_pid(pl->pid) != NULL)
2835 len += sprintf(buf + len, "%d\n", pl->pid);
2836 read_unlock(&tasklist_lock);
2838 spin_unlock(&dev->proc_list_lock);
2844 static ssize_t taskname_show(struct device *d, struct device_attribute *attr,
2847 struct taskdev *dev = to_taskdev(d);
2850 if (!devstate_read_lock_and_test(dev, TASKDEV_ST_ATTACHED))
2853 len = sprintf(buf, "%s\n", dev->task->name);
2855 devstate_read_unlock(dev);
2860 static ssize_t ttyp_show(struct device *d, struct device_attribute *attr,
2863 struct taskdev *dev = to_taskdev(d);
2867 if (!devstate_read_lock_and_test(dev, TASKDEV_ST_ATTACHED))
2870 ttyp = dev->task->ttyp;
2871 len += sprintf(buf + len, "0x%04x\n", ttyp);
2872 len += sprintf(buf + len, "%s %s send\n",
2873 (sndtyp_acv(ttyp)) ? "active" :
2875 (sndtyp_wd(ttyp)) ? "word" :
2876 (sndtyp_pvt(ttyp)) ? "private block" :
2878 len += sprintf(buf + len, "%s %s receive\n",
2879 (rcvtyp_acv(ttyp)) ? "active" :
2881 (rcvtyp_wd(ttyp)) ? "word" :
2882 (rcvtyp_pvt(ttyp)) ? "private block" :
2885 devstate_read_unlock(dev);
2890 static ssize_t fifosz_show(struct device *d, struct device_attribute *attr,
2893 struct kfifo *fifo = to_taskdev(d)->rcvdt.fifo;
2894 return sprintf(buf, "%d\n", fifo->size);
2897 static int fifosz_store(struct device *d, struct device_attribute *attr,
2898 const char *buf, size_t count)
2900 struct taskdev *dev = to_taskdev(d);
2901 unsigned long fifosz;
2904 fifosz = simple_strtol(buf, NULL, 10);
2905 if (taskdev_lock_and_statelock_attached(dev, &dev->read_mutex))
2907 ret = taskdev_set_fifosz(dev, fifosz);
2908 taskdev_unlock_and_stateunlock(dev, &dev->read_mutex);
2910 return (ret < 0) ? ret : strlen(buf);
2914 static ssize_t fifocnt_show(struct device *d, struct device_attribute *attr,
2917 struct kfifo *fifo = to_taskdev(d)->rcvdt.fifo;
2918 return sprintf(buf, "%d\n", fifo->size);
2922 static inline char *bid_name(u16 bid)
2932 sprintf(s, "%d", bid);
2937 static ssize_t ipblink_show(struct device *d, struct device_attribute *attr,
2940 struct rcvdt_bk_struct *rcvdt = &to_taskdev(d)->rcvdt.bk;
2943 spin_lock(&rcvdt->link.lock);
2944 len = sprintf(buf, "top %s\ntail %s\n",
2945 bid_name(rcvdt->link.top), bid_name(rcvdt->link.tail));
2946 spin_unlock(&rcvdt->link.lock);
2952 static ssize_t wsz_show(struct device *d, struct device_attribute *attr,
2955 return sprintf(buf, "%d\n", to_taskdev(d)->wsz);
2959 static ssize_t mmap_show(struct device *d, struct device_attribute *attr,
2962 struct dsptask *task = to_taskdev(d)->task;
2963 return sprintf(buf, "0x%p 0x%x\n", task->map_base, task->map_length);
2967 * called from ipbuf_show()
2969 int ipbuf_is_held(u8 tid, u16 bid)
2971 struct dsptask *task = dsptask[tid];
2972 struct ipblink *link;
2979 link = &task->dev->rcvdt.bk.link;
2980 spin_lock(&link->lock);
2981 ipblink_for_each(b, link) {
2982 if (b == bid) { /* found */
2987 spin_unlock(&link->lock);
2992 int __init dsp_taskmod_init(void)
2996 memset(taskdev, 0, sizeof(void *) * TASKDEV_MAX);
2997 memset(dsptask, 0, sizeof(void *) * TASKDEV_MAX);
2999 retval = register_chrdev(OMAP_DSP_TASK_MAJOR, "dsptask",
3003 "omapdsp: failed to register task device: %d\n", retval);
3007 retval = bus_register(&dsptask_bus);
3010 "omapdsp: failed to register DSP task bus: %d\n",
3012 unregister_chrdev(OMAP_DSP_TASK_MAJOR, "dsptask");
3015 retval = driver_register(&dsptask_driver);
3018 "omapdsp: failed to register DSP task driver: %d\n",
3020 bus_unregister(&dsptask_bus);
3021 unregister_chrdev(OMAP_DSP_TASK_MAJOR, "dsptask");
3024 dsp_task_class = class_create(THIS_MODULE, "dsptask");
3025 if (IS_ERR(dsp_task_class)) {
3026 printk(KERN_ERR "omapdsp: failed to create DSP task class\n");
3027 driver_unregister(&dsptask_driver);
3028 bus_unregister(&dsptask_bus);
3029 unregister_chrdev(OMAP_DSP_TASK_MAJOR, "dsptask");
3036 void dsp_taskmod_exit(void)
3038 class_destroy(dsp_task_class);
3039 driver_unregister(&dsptask_driver);
3040 bus_unregister(&dsptask_bus);
3041 unregister_chrdev(OMAP_DSP_TASK_MAJOR, "dsptask");