]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/plat-omap/dsp/task.c
b43f70409928bfe063dcc97bc220da88fd5c4c70
[linux-2.6-omap-h63xx.git] / arch / arm / plat-omap / dsp / task.c
1 /*
2  * This file is part of OMAP DSP driver (DSP Gateway version 3.3.1)
3  *
4  * Copyright (C) 2002-2006 Nokia Corporation. All rights reserved.
5  *
6  * Contact: Toshihiro Kobayashi <toshihiro.kobayashi@nokia.com>
7  *
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.
11  *
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.
16  *
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
20  * 02110-1301 USA
21  *
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/major.h>
28 #include <linux/fs.h>
29 #include <linux/poll.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
32 #include <linux/sched.h>
33 #include <linux/mm.h>
34 #include <linux/mutex.h>
35 #include <linux/interrupt.h>
36 #include <asm/uaccess.h>
37 #include <asm/io.h>
38 #include <asm/arch/mailbox.h>
39 #include <asm/arch/dsp.h>
40 #include "uaccess_dsp.h"
41 #include "dsp_mbcmd.h"
42 #include "dsp.h"
43 #include "ipbuf.h"
44 #include "fifo.h"
45 #include "proclist.h"
46
47 #define is_aligned(adr,align)   (!((adr)&((align)-1)))
48
49 /*
50  * devstate: task device state machine
51  * NOTASK:      task is not attached.
52  * ATTACHED:    task is attached.
53  * GARBAGE:     task is detached. waiting for all processes to close this device.
54  * ADDREQ:      requesting for tadd
55  * DELREQ:      requesting for tdel. no process is opening this device.
56  * FREEZED:     task is attached, but reserved to be killed.
57  * ADDFAIL:     tadd failed.
58  * ADDING:      tadd in process.
59  * DELING:      tdel in process.
60  * KILLING:     tkill in process.
61  */
62 #define TASKDEV_ST_NOTASK       0x00000001
63 #define TASKDEV_ST_ATTACHED     0x00000002
64 #define TASKDEV_ST_GARBAGE      0x00000004
65 #define TASKDEV_ST_INVALID      0x00000008
66 #define TASKDEV_ST_ADDREQ       0x00000100
67 #define TASKDEV_ST_DELREQ       0x00000200
68 #define TASKDEV_ST_FREEZED      0x00000400
69 #define TASKDEV_ST_ADDFAIL      0x00001000
70 #define TASKDEV_ST_ADDING       0x00010000
71 #define TASKDEV_ST_DELING       0x00020000
72 #define TASKDEV_ST_KILLING      0x00040000
73 #define TASKDEV_ST_STATE_MASK   0x7fffffff
74 #define TASKDEV_ST_STALE        0x80000000
75
76 static struct {
77         long state;
78         char *name;
79 } devstate_desc[] = {
80         { TASKDEV_ST_NOTASK,   "notask" },
81         { TASKDEV_ST_ATTACHED, "attached" },
82         { TASKDEV_ST_GARBAGE,  "garbage" },
83         { TASKDEV_ST_INVALID,  "invalid" },
84         { TASKDEV_ST_ADDREQ,   "addreq" },
85         { TASKDEV_ST_DELREQ,   "delreq" },
86         { TASKDEV_ST_FREEZED,  "freezed" },
87         { TASKDEV_ST_ADDFAIL,  "addfail" },
88         { TASKDEV_ST_ADDING,   "adding" },
89         { TASKDEV_ST_DELING,   "deling" },
90         { TASKDEV_ST_KILLING,  "killing" },
91 };
92
93 static char *devstate_name(long state)
94 {
95         int i;
96         int max = ARRAY_SIZE(devstate_desc);
97
98         for (i = 0; i < max; i++) {
99                 if (state & devstate_desc[i].state)
100                         return devstate_desc[i].name;
101         }
102         return "unknown";
103 }
104
105 struct rcvdt_bk_struct {
106         struct ipblink link;
107         unsigned int rp;
108 };
109
110 struct taskdev {
111         struct bus_type *bus;
112         struct device dev;      /* Generic device interface */
113
114         long state;
115         struct rw_semaphore state_sem;
116         wait_queue_head_t state_wait_q;
117         struct mutex usecount_lock;
118         unsigned int usecount;
119         char name[TNM_LEN];
120         struct file_operations fops;
121         spinlock_t proc_list_lock;
122         struct list_head proc_list;
123         struct dsptask *task;
124
125         /* read stuff */
126         wait_queue_head_t read_wait_q;
127         struct mutex read_mutex;
128         union {
129                 struct fifo_struct fifo;        /* for active word */
130                 struct rcvdt_bk_struct bk;
131         } rcvdt;
132
133         /* write stuff */
134         wait_queue_head_t write_wait_q;
135         struct mutex write_mutex;
136         spinlock_t wsz_lock;
137         size_t wsz;
138
139         /* tctl stuff */
140         wait_queue_head_t tctl_wait_q;
141         struct mutex tctl_mutex;
142         int tctl_stat;
143         int tctl_ret;   /* return value for tctl_show() */
144
145         /* device lock */
146         struct mutex lock;
147         pid_t lock_pid;
148 };
149
150 #define to_taskdev(n) container_of(n, struct taskdev, dev)
151
152 struct dsptask {
153         enum {
154                 TASK_ST_ERR = 0,
155                 TASK_ST_READY,
156                 TASK_ST_CFGREQ
157         } state;
158         u8 tid;
159         char name[TNM_LEN];
160         u16 ttyp;
161         struct taskdev *dev;
162
163         /* read stuff */
164         struct ipbuf_p *ipbuf_pvt_r;
165
166         /* write stuff */
167         struct ipbuf_p *ipbuf_pvt_w;
168
169         /* mmap stuff */
170         void *map_base;
171         size_t map_length;
172 };
173
174 #define sndtyp_acv(ttyp)        ((ttyp) & TTYP_ASND)
175 #define sndtyp_psv(ttyp)        (!((ttyp) & TTYP_ASND))
176 #define sndtyp_bk(ttyp)         ((ttyp) & TTYP_BKDM)
177 #define sndtyp_wd(ttyp)         (!((ttyp) & TTYP_BKDM))
178 #define sndtyp_pvt(ttyp)        ((ttyp) & TTYP_PVDM)
179 #define sndtyp_gbl(ttyp)        (!((ttyp) & TTYP_PVDM))
180 #define rcvtyp_acv(ttyp)        ((ttyp) & TTYP_ARCV)
181 #define rcvtyp_psv(ttyp)        (!((ttyp) & TTYP_ARCV))
182 #define rcvtyp_bk(ttyp)         ((ttyp) & TTYP_BKMD)
183 #define rcvtyp_wd(ttyp)         (!((ttyp) & TTYP_BKMD))
184 #define rcvtyp_pvt(ttyp)        ((ttyp) & TTYP_PVMD)
185 #define rcvtyp_gbl(ttyp)        (!((ttyp) & TTYP_PVMD))
186
187 static inline int has_taskdev_lock(struct taskdev *dev);
188 static int dsp_rmdev_minor(unsigned char minor);
189 static int taskdev_init(struct taskdev *dev, char *name, unsigned char minor);
190 static void taskdev_delete(unsigned char minor);
191 static int taskdev_attach_task(struct taskdev *dev, struct dsptask *task);
192 static int dsp_tdel_bh(struct taskdev *dev, u16 type);
193
194 static struct bus_type dsptask_bus = {
195         .name = "dsptask",
196 };
197
198 static struct class *dsp_task_class;
199 static DEFINE_MUTEX(devmgr_lock);
200 static struct taskdev *taskdev[TASKDEV_MAX];
201 static struct dsptask *dsptask[TASKDEV_MAX];
202 static DEFINE_MUTEX(cfg_lock);
203 static u16 cfg_cmd;
204 static u8 cfg_tid;
205 static DECLARE_WAIT_QUEUE_HEAD(cfg_wait_q);
206 static u8 n_task;       /* static task count */
207 static void *heap;
208
209 #define is_dynamic_task(tid)    ((tid) >= n_task)
210
211 #define devstate_read_lock(dev, devstate) \
212                 devstate_read_lock_timeout(dev, devstate, 0)
213 #define devstate_read_unlock(dev)       up_read(&(dev)->state_sem)
214 #define devstate_write_lock(dev, devstate) \
215                 devstate_write_lock_timeout(dev, devstate, 0)
216 #define devstate_write_unlock(dev)      up_write(&(dev)->state_sem)
217
218 static ssize_t devname_show(struct device *d, struct device_attribute *attr,
219                             char *buf);
220 static ssize_t devstate_show(struct device *d, struct device_attribute *attr,
221                              char *buf);
222 static ssize_t proc_list_show(struct device *d, struct device_attribute *attr,
223                               char *buf);
224 static ssize_t taskname_show(struct device *d, struct device_attribute *attr,
225                              char *buf);
226 static ssize_t ttyp_show(struct device *d, struct device_attribute *attr,
227                          char *buf);
228 static ssize_t fifosz_show(struct device *d, struct device_attribute *attr,
229                            char *buf);
230 static int fifosz_store(struct device *d, struct device_attribute *attr,
231                         const char *buf, size_t count);
232 static ssize_t fifocnt_show(struct device *d, struct device_attribute *attr,
233                             char *buf);
234 static ssize_t ipblink_show(struct device *d, struct device_attribute *attr,
235                             char *buf);
236 static ssize_t wsz_show(struct device *d, struct device_attribute *attr,
237                         char *buf);
238 static ssize_t mmap_show(struct device *d, struct device_attribute *attr,
239                          char *buf);
240
241 #define __ATTR_RW(_name,_mode) { \
242         .attr = {.name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE },     \
243         .show   = _name##_show,                                 \
244         .store  = _name##_store,                                        \
245 }
246
247 static struct device_attribute dev_attr_devname   = __ATTR_RO(devname);
248 static struct device_attribute dev_attr_devstate  = __ATTR_RO(devstate);
249 static struct device_attribute dev_attr_proc_list = __ATTR_RO(proc_list);
250 static struct device_attribute dev_attr_taskname  = __ATTR_RO(taskname);
251 static struct device_attribute dev_attr_ttyp      = __ATTR_RO(ttyp);
252 static struct device_attribute dev_attr_fifosz    = __ATTR_RW(fifosz, 0666);
253 static struct device_attribute dev_attr_fifocnt   = __ATTR_RO(fifocnt);
254 static struct device_attribute dev_attr_ipblink   = __ATTR_RO(ipblink);
255 static struct device_attribute dev_attr_wsz       = __ATTR_RO(wsz);
256 static struct device_attribute dev_attr_mmap      = __ATTR_RO(mmap);
257
258 static inline void set_taskdev_state(struct taskdev *dev, int state)
259 {
260         pr_debug("omapdsp: devstate: CHANGE %s[%d]:\"%s\"->\"%s\"\n",
261                  dev->name,
262                  (dev->task ? dev->task->tid : -1),
263                  devstate_name(dev->state),
264                  devstate_name(state));
265         dev->state = state;
266 }
267
268 /*
269  * devstate_read_lock_timeout()
270  * devstate_write_lock_timeout():
271  * timeout != 0: dev->state can be diffeent from what you want.
272  * timeout == 0: no timeout
273  */
274 #define BUILD_DEVSTATE_LOCK_TIMEOUT(rw)                                         \
275 static int devstate_##rw##_lock_timeout(struct taskdev *dev, long devstate,     \
276                                       int timeout)                              \
277 {                                                                               \
278         DEFINE_WAIT(wait);                                                      \
279         down_##rw(&dev->state_sem);                                             \
280         while (!(dev->state & devstate)) {                                      \
281                 up_##rw(&dev->state_sem);                                       \
282                 prepare_to_wait(&dev->state_wait_q, &wait, TASK_INTERRUPTIBLE); \
283                 if (!timeout)                                                   \
284                         timeout = MAX_SCHEDULE_TIMEOUT;                         \
285                 timeout = schedule_timeout(timeout);                            \
286                 finish_wait(&dev->state_wait_q, &wait);                         \
287                 if (timeout == 0)                                               \
288                         return -ETIME;                                          \
289                 if (signal_pending(current))                                    \
290                         return -EINTR;                                          \
291                 down_##rw(&dev->state_sem);                                     \
292         }                                                                       \
293         return 0;                                                               \
294 }
295 BUILD_DEVSTATE_LOCK_TIMEOUT(read)
296 BUILD_DEVSTATE_LOCK_TIMEOUT(write)
297
298 #define BUILD_DEVSTATE_LOCK_AND_TEST(rw)                                        \
299 static int devstate_##rw##_lock_and_test(struct taskdev *dev, long devstate)    \
300 {                                                                               \
301         down_##rw(&dev->state_sem);                                             \
302         if (dev->state & devstate)                                              \
303                 return 1;       /* success */                                   \
304         /* failure */                                                           \
305         up_##rw(&dev->state_sem);                                               \
306         return 0;                                                               \
307 }
308 BUILD_DEVSTATE_LOCK_AND_TEST(read)
309 BUILD_DEVSTATE_LOCK_AND_TEST(write)
310
311 static int taskdev_lock_interruptible(struct taskdev *dev,
312                                       struct mutex *lock)
313 {
314         int ret;
315
316         if (has_taskdev_lock(dev))
317                 ret = mutex_lock_interruptible(lock);
318         else {
319                 if ((ret = mutex_lock_interruptible(&dev->lock)) != 0)
320                         return ret;
321                 ret = mutex_lock_interruptible(lock);
322                 mutex_unlock(&dev->lock);
323         }
324
325         return ret;
326 }
327
328 static int taskdev_lock_and_statelock_attached(struct taskdev *dev,
329                                                struct mutex *lock)
330 {
331         int ret;
332
333         if (!devstate_read_lock_and_test(dev, TASKDEV_ST_ATTACHED))
334                 return -ENODEV;
335
336         if ((ret = taskdev_lock_interruptible(dev, lock)) != 0)
337                 devstate_read_unlock(dev);
338
339         return ret;
340 }
341
342 static inline void taskdev_unlock_and_stateunlock(struct taskdev *dev,
343                                                       struct mutex *lock)
344 {
345         mutex_unlock(lock);
346         devstate_read_unlock(dev);
347 }
348
349 /*
350  * taskdev_flush_buf()
351  * must be called under state_lock(ATTACHED) and dev->read_mutex.
352  */
353 static int taskdev_flush_buf(struct taskdev *dev)
354 {
355         u16 ttyp = dev->task->ttyp;
356
357         if (sndtyp_wd(ttyp)) {
358                 /* word receiving */
359                 flush_fifo(&dev->rcvdt.fifo);
360         } else {
361                 /* block receiving */
362                 struct rcvdt_bk_struct *rcvdt = &dev->rcvdt.bk;
363
364                 if (sndtyp_gbl(ttyp))
365                         ipblink_flush(&rcvdt->link);
366                 else {
367                         ipblink_flush_pvt(&rcvdt->link);
368                         release_ipbuf_pvt(dev->task->ipbuf_pvt_r);
369                 }
370         }
371
372         return 0;
373 }
374
375 /*
376  * taskdev_set_fifosz()
377  * must be called under dev->read_mutex.
378  */
379 static int taskdev_set_fifosz(struct taskdev *dev, unsigned long sz)
380 {
381         u16 ttyp = dev->task->ttyp;
382         int stat;
383
384         if (!(sndtyp_wd(ttyp) && sndtyp_acv(ttyp))) {
385                 printk(KERN_ERR
386                        "omapdsp: buffer size can be changed only for "
387                        "active word sending task.\n");
388                 return -EINVAL;
389         }
390         if ((sz == 0) || (sz & 1)) {
391                 printk(KERN_ERR "omapdsp: illegal buffer size! (%ld)\n"
392                                 "it must be even and non-zero value.\n", sz);
393                 return -EINVAL;
394         }
395
396         stat = realloc_fifo(&dev->rcvdt.fifo, sz);
397         if (stat == -EBUSY) {
398                 printk(KERN_ERR "omapdsp: buffer is not empty!\n");
399                 return stat;
400         } else if (stat < 0) {
401                 printk(KERN_ERR
402                        "omapdsp: unable to change receive buffer size. "
403                        "(%ld bytes for %s)\n", sz, dev->name);
404                 return stat;
405         }
406
407         return 0;
408 }
409
410 static inline int has_taskdev_lock(struct taskdev *dev)
411 {
412         return (dev->lock_pid == current->pid);
413 }
414
415 static int taskdev_lock(struct taskdev *dev)
416 {
417         if (mutex_lock_interruptible(&dev->lock))
418                 return -EINTR;
419         dev->lock_pid = current->pid;
420         return 0;
421 }
422
423 static int taskdev_unlock(struct taskdev *dev)
424 {
425         if (!has_taskdev_lock(dev)) {
426                 printk(KERN_ERR
427                        "omapdsp: an illegal process attempted to "
428                        "unlock the dsptask lock!\n");
429                 return -EINVAL;
430         }
431         dev->lock_pid = 0;
432         mutex_unlock(&dev->lock);
433         return 0;
434 }
435
436 static int dsp_task_config(struct dsptask *task, u8 tid)
437 {
438         u16 ttyp;
439         int ret;
440
441         task->tid = tid;
442         dsptask[tid] = task;
443
444         /* TCFG request */
445         task->state = TASK_ST_CFGREQ;
446         if (mutex_lock_interruptible(&cfg_lock)) {
447                 ret = -EINTR;
448                 goto fail_out;
449         }
450         cfg_cmd = MBOX_CMD_DSP_TCFG;
451         mbcompose_send_and_wait(TCFG, tid, 0, &cfg_wait_q);
452         cfg_cmd = 0;
453         mutex_unlock(&cfg_lock);
454
455         if (task->state != TASK_ST_READY) {
456                 printk(KERN_ERR "omapdsp: task %d configuration error!\n", tid);
457                 ret = -EINVAL;
458                 goto fail_out;
459         }
460
461         if (strlen(task->name) <= 1)
462                 sprintf(task->name, "%d", tid);
463         pr_info("omapdsp: task %d: name %s\n", tid, task->name);
464
465         ttyp = task->ttyp;
466
467         /*
468          * task info sanity check
469          */
470
471         /* task type check */
472         if (rcvtyp_psv(ttyp) && rcvtyp_pvt(ttyp)) {
473                 printk(KERN_ERR "omapdsp: illegal task type(0x%04x), tid=%d\n",
474                        tid, ttyp);
475                 ret = -EINVAL;
476                 goto fail_out;
477         }
478
479         /* private buffer address check */
480         if (sndtyp_pvt(ttyp) &&
481             (ipbuf_p_validate(task->ipbuf_pvt_r, DIR_D2A) < 0)) {
482                 ret = -EINVAL;
483                 goto fail_out;
484         }
485         if (rcvtyp_pvt(ttyp) &&
486             (ipbuf_p_validate(task->ipbuf_pvt_w, DIR_A2D) < 0)) {
487                 ret = -EINVAL;
488                 goto fail_out;
489         }
490
491         /* mmap buffer configuration check */
492         if ((task->map_length > 0) &&
493             ((!is_aligned((unsigned long)task->map_base, PAGE_SIZE)) ||
494              (!is_aligned(task->map_length, PAGE_SIZE)) ||
495              (dsp_mem_type(task->map_base, task->map_length) != MEM_TYPE_EXTERN))) {
496                 printk(KERN_ERR
497                        "omapdsp: illegal mmap buffer address(0x%p) or "
498                        "length(0x%x).\n"
499                        "  It needs to be page-aligned and located at "
500                        "external memory.\n",
501                        task->map_base, task->map_length);
502                 ret = -EINVAL;
503                 goto fail_out;
504         }
505
506         return 0;
507
508 fail_out:
509         dsptask[tid] = NULL;
510         return ret;
511 }
512
513 static void dsp_task_init(struct dsptask *task)
514 {
515         mbcompose_send(TCTL, task->tid, TCTL_TINIT);
516 }
517
518 int dsp_task_config_all(u8 n)
519 {
520         int i, ret;
521         struct taskdev *devheap;
522         struct dsptask *taskheap;
523         size_t devheapsz, taskheapsz;
524
525         pr_info("omapdsp: found %d task(s)\n", n);
526         if (n == 0)
527                 return 0;
528
529         /*
530          * reducing kmalloc!
531          */
532         devheapsz  = sizeof(struct taskdev) * n;
533         taskheapsz = sizeof(struct dsptask) * n;
534         heap = kzalloc(devheapsz + taskheapsz, GFP_KERNEL);
535         if (heap == NULL)
536                 return -ENOMEM;
537         devheap  = heap;
538         taskheap = heap + devheapsz;
539
540         n_task = n;
541         for (i = 0; i < n; i++) {
542                 struct taskdev *dev  = &devheap[i];
543                 struct dsptask *task = &taskheap[i];
544
545                 if ((ret = dsp_task_config(task, i)) < 0)
546                         return ret;
547                 if ((ret = taskdev_init(dev, task->name, i)) < 0)
548                         return ret;
549                 if ((ret = taskdev_attach_task(dev, task)) < 0)
550                         return ret;
551                 dsp_task_init(task);
552                 pr_info("omapdsp: taskdev %s enabled.\n", dev->name);
553         }
554
555         return 0;
556 }
557
558 static void dsp_task_unconfig(struct dsptask *task)
559 {
560         dsptask[task->tid] = NULL;
561 }
562
563 void dsp_task_unconfig_all(void)
564 {
565         unsigned char minor;
566         u8 tid;
567         struct dsptask *task;
568
569         for (minor = 0; minor < n_task; minor++) {
570                 /*
571                  * taskdev[minor] can be NULL in case of
572                  * configuration failure
573                  */
574                 if (taskdev[minor])
575                         taskdev_delete(minor);
576         }
577         for (; minor < TASKDEV_MAX; minor++) {
578                 if (taskdev[minor])
579                         dsp_rmdev_minor(minor);
580         }
581
582         for (tid = 0; tid < n_task; tid++) {
583                 /*
584                  * dsptask[tid] can be NULL in case of
585                  * configuration failure
586                  */
587                 task = dsptask[tid];
588                 if (task)
589                         dsp_task_unconfig(task);
590         }
591         for (; tid < TASKDEV_MAX; tid++) {
592                 task = dsptask[tid];
593                 if (task) {
594                         /*
595                          * on-demand tasks should be deleted in
596                          * rmdev_minor(), but just in case.
597                          */
598                         dsp_task_unconfig(task);
599                         kfree(task);
600                 }
601         }
602
603         if (heap) {
604                 kfree(heap);
605                 heap = NULL;
606         }
607
608         n_task = 0;
609 }
610
611 static struct device_driver dsptask_driver = {
612         .name   = "dsptask",
613         .bus    = &dsptask_bus,
614 };
615
616 u8 dsp_task_count(void)
617 {
618         return n_task;
619 }
620
621 int dsp_taskmod_busy(void)
622 {
623         struct taskdev *dev;
624         unsigned char minor;
625         unsigned int usecount;
626
627         for (minor = 0; minor < TASKDEV_MAX; minor++) {
628                 dev = taskdev[minor];
629                 if (dev == NULL)
630                         continue;
631                 if ((usecount = dev->usecount) > 0) {
632                         printk("dsp_taskmod_busy(): %s: usecount=%d\n",
633                                dev->name, usecount);
634                         return 1;
635                 }
636 /*
637                 if ((dev->state & (TASKDEV_ST_ADDREQ |
638                                    TASKDEV_ST_DELREQ)) {
639 */
640                 if (dev->state & TASKDEV_ST_ADDREQ) {
641                         printk("dsp_taskmod_busy(): %s is in %s\n",
642                                dev->name, devstate_name(dev->state));
643                         return 1;
644                 }
645         }
646         return 0;
647 }
648
649 /*
650  * DSP task device file operations
651  */
652 static ssize_t dsp_task_read_wd_acv(struct file *file, char __user *buf,
653                                     size_t count, loff_t *ppos)
654 {
655         unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
656         struct taskdev *dev = taskdev[minor];
657         int ret = 0;
658         DEFINE_WAIT(wait);
659
660         if (count == 0) {
661                 return 0;
662         } else if (count & 0x1) {
663                 printk(KERN_ERR
664                        "omapdsp: odd count is illegal for DSP task device.\n");
665                 return -EINVAL;
666         }
667
668         if (taskdev_lock_and_statelock_attached(dev, &dev->read_mutex))
669                 return -ENODEV;
670
671
672         prepare_to_wait(&dev->read_wait_q, &wait, TASK_INTERRUPTIBLE);
673         if (fifo_empty(&dev->rcvdt.fifo))
674                 schedule();
675         finish_wait(&dev->read_wait_q, &wait);
676         if (fifo_empty(&dev->rcvdt.fifo)) {
677                 /* failure */
678                 if (signal_pending(current))
679                         ret = -EINTR;
680                 goto up_out;
681         }
682
683
684         ret = copy_to_user_fm_fifo(buf, &dev->rcvdt.fifo, count);
685
686  up_out:
687         taskdev_unlock_and_stateunlock(dev, &dev->read_mutex);
688         return ret;
689 }
690
691 static ssize_t dsp_task_read_bk_acv(struct file *file, char __user *buf,
692                                     size_t count, loff_t *ppos)
693 {
694         unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
695         struct taskdev *dev = taskdev[minor];
696         struct rcvdt_bk_struct *rcvdt = &dev->rcvdt.bk;
697         ssize_t ret = 0;
698         DEFINE_WAIT(wait);
699
700         if (count == 0) {
701                 return 0;
702         } else if (count & 0x1) {
703                 printk(KERN_ERR
704                        "omapdsp: odd count is illegal for DSP task device.\n");
705                 return -EINVAL;
706         } else if ((int)buf & 0x1) {
707                 printk(KERN_ERR
708                        "omapdsp: buf should be word aligned for "
709                        "dsp_task_read().\n");
710                 return -EINVAL;
711         }
712
713         if (taskdev_lock_and_statelock_attached(dev, &dev->read_mutex))
714                 return -ENODEV;
715
716         prepare_to_wait(&dev->read_wait_q, &wait, TASK_INTERRUPTIBLE);
717         if (ipblink_empty(&rcvdt->link))
718                 schedule();
719         finish_wait(&dev->read_wait_q, &wait);
720         if (ipblink_empty(&rcvdt->link)) {
721                 /* failure */
722                 if (signal_pending(current))
723                         ret = -EINTR;
724                 goto up_out;
725         }
726
727         /* copy from delayed IPBUF */
728         if (sndtyp_pvt(dev->task->ttyp)) {
729                 /* private */
730                 if (!ipblink_empty(&rcvdt->link)) {
731                         struct ipbuf_p *ipbp = dev->task->ipbuf_pvt_r;
732                         unsigned char *base, *src;
733                         size_t bkcnt;
734
735                         if (dsp_mem_enable(ipbp) < 0) {
736                                 ret = -EBUSY;
737                                 goto up_out;
738                         }
739                         base = MKVIRT(ipbp->ah, ipbp->al);
740                         bkcnt = ((unsigned long)ipbp->c) * 2 - rcvdt->rp;
741                         if (dsp_address_validate(base, bkcnt,
742                                                  "task %s read buffer",
743                                                  dev->task->name) < 0) {
744                                 ret = -EINVAL;
745                                 goto pv_out1;
746                         }
747                         if (dsp_mem_enable(base) < 0) {
748                                 ret = -EBUSY;
749                                 goto pv_out1;
750                         }
751                         src = base + rcvdt->rp;
752                         if (bkcnt > count) {
753                                 if (copy_to_user_dsp(buf, src, count)) {
754                                         ret = -EFAULT;
755                                         goto pv_out2;
756                                 }
757                                 ret = count;
758                                 rcvdt->rp += count;
759                         } else {
760                                 if (copy_to_user_dsp(buf, src, bkcnt)) {
761                                         ret = -EFAULT;
762                                         goto pv_out2;
763                                 }
764                                 ret = bkcnt;
765                                 ipblink_del_pvt(&rcvdt->link);
766                                 release_ipbuf_pvt(ipbp);
767                                 rcvdt->rp = 0;
768                         }
769                 pv_out2:
770                         dsp_mem_disable(src);
771                 pv_out1:
772                         dsp_mem_disable(ipbp);
773                 }
774         } else {
775                 /* global */
776                 if (dsp_mem_enable_ipbuf() < 0) {
777                         ret = -EBUSY;
778                         goto up_out;
779                 }
780                 while (!ipblink_empty(&rcvdt->link)) {
781                         unsigned char *src;
782                         size_t bkcnt;
783                         struct ipbuf_head *ipb_h = bid_to_ipbuf(rcvdt->link.top);
784
785                         src = ipb_h->p->d + rcvdt->rp;
786                         bkcnt = ((unsigned long)ipb_h->p->c) * 2 - rcvdt->rp;
787                         if (bkcnt > count) {
788                                 if (copy_to_user_dsp(buf, src, count)) {
789                                         ret = -EFAULT;
790                                         goto gb_out;
791                                 }
792                                 ret += count;
793                                 rcvdt->rp += count;
794                                 break;
795                         } else {
796                                 if (copy_to_user_dsp(buf, src, bkcnt)) {
797                                         ret = -EFAULT;
798                                         goto gb_out;
799                                 }
800                                 ret += bkcnt;
801                                 buf += bkcnt;
802                                 count -= bkcnt;
803                                 ipblink_del_top(&rcvdt->link);
804                                 unuse_ipbuf(ipb_h);
805                                 rcvdt->rp = 0;
806                         }
807                 }
808         gb_out:
809                 dsp_mem_disable_ipbuf();
810         }
811
812  up_out:
813         taskdev_unlock_and_stateunlock(dev, &dev->read_mutex);
814         return ret;
815 }
816
817 static ssize_t dsp_task_read_wd_psv(struct file *file, char __user *buf,
818                                     size_t count, loff_t *ppos)
819 {
820         unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
821         struct taskdev *dev = taskdev[minor];
822         int ret = 0;
823
824         if (count == 0) {
825                 return 0;
826         } else if (count & 0x1) {
827                 printk(KERN_ERR
828                        "omapdsp: odd count is illegal for DSP task device.\n");
829                 return -EINVAL;
830         } else {
831                 /* force! */
832                 count = 2;
833         }
834
835         if (taskdev_lock_and_statelock_attached(dev, &dev->read_mutex))
836                 return -ENODEV;
837
838         mbcompose_send_and_wait(WDREQ, dev->task->tid, 0, &dev->read_wait_q);
839
840         if (fifo_empty(&dev->rcvdt.fifo)) {
841                 /* failure */
842                 if (signal_pending(current))
843                         ret = -EINTR;
844                 goto up_out;
845         }
846
847         ret = copy_to_user_fm_fifo(buf, &dev->rcvdt.fifo, count);
848
849 up_out:
850         taskdev_unlock_and_stateunlock(dev, &dev->read_mutex);
851         return ret;
852 }
853
854 static ssize_t dsp_task_read_bk_psv(struct file *file, char __user *buf,
855                                     size_t count, loff_t *ppos)
856 {
857         unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
858         struct taskdev *dev = taskdev[minor];
859         struct rcvdt_bk_struct *rcvdt = &dev->rcvdt.bk;
860         int ret = 0;
861
862         if (count == 0) {
863                 return 0;
864         } else if (count & 0x1) {
865                 printk(KERN_ERR
866                        "omapdsp: odd count is illegal for DSP task device.\n");
867                 return -EINVAL;
868         } else if ((int)buf & 0x1) {
869                 printk(KERN_ERR
870                        "omapdsp: buf should be word aligned for "
871                        "dsp_task_read().\n");
872                 return -EINVAL;
873         }
874
875         if (taskdev_lock_and_statelock_attached(dev, &dev->read_mutex))
876                 return -ENODEV;
877
878         mbcompose_send_and_wait(BKREQ, dev->task->tid, count/2,
879                                 &dev->read_wait_q);
880
881         if (ipblink_empty(&rcvdt->link)) {
882                 /* failure */
883                 if (signal_pending(current))
884                         ret = -EINTR;
885                 goto up_out;
886         }
887
888         /*
889          * We will not receive more than requested count.
890          */
891         if (sndtyp_pvt(dev->task->ttyp)) {
892                 /* private */
893                 struct ipbuf_p *ipbp = dev->task->ipbuf_pvt_r;
894                 size_t rcvcnt;
895                 void *src;
896
897                 if (dsp_mem_enable(ipbp) < 0) {
898                         ret = -EBUSY;
899                         goto up_out;
900                 }
901                 src = MKVIRT(ipbp->ah, ipbp->al);
902                 rcvcnt = ((unsigned long)ipbp->c) * 2;
903                 if (dsp_address_validate(src, rcvcnt, "task %s read buffer",
904                                          dev->task->name) < 0) {
905                         ret = -EINVAL;
906                         goto pv_out1;
907                 }
908                 if (dsp_mem_enable(src) < 0) {
909                         ret = -EBUSY;
910                         goto pv_out1;
911                 }
912                 if (count > rcvcnt)
913                         count = rcvcnt;
914                 if (copy_to_user_dsp(buf, src, count)) {
915                         ret = -EFAULT;
916                         goto pv_out2;
917                 }
918                 ipblink_del_pvt(&rcvdt->link);
919                 release_ipbuf_pvt(ipbp);
920                 ret = count;
921 pv_out2:
922                 dsp_mem_disable(src);
923 pv_out1:
924                 dsp_mem_disable(ipbp);
925         } else {
926                 /* global */
927                 struct ipbuf_head *ipb_h = bid_to_ipbuf(rcvdt->link.top);
928                 size_t rcvcnt;
929
930                 if (dsp_mem_enable_ipbuf() < 0) {
931                         ret = -EBUSY;
932                         goto up_out;
933                 }
934                 rcvcnt = ((unsigned long)ipb_h->p->c) * 2;
935                 if (count > rcvcnt)
936                         count = rcvcnt;
937                 if (copy_to_user_dsp(buf, ipb_h->p->d, count)) {
938                         ret = -EFAULT;
939                         goto gb_out;
940                 }
941                 ipblink_del_top(&rcvdt->link);
942                 unuse_ipbuf(ipb_h);
943                 ret = count;
944 gb_out:
945                 dsp_mem_disable_ipbuf();
946         }
947
948 up_out:
949         taskdev_unlock_and_stateunlock(dev, &dev->read_mutex);
950         return ret;
951 }
952
953 static ssize_t dsp_task_write_wd(struct file *file, const char __user *buf,
954                                  size_t count, loff_t *ppos)
955 {
956         unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
957         struct taskdev *dev = taskdev[minor];
958         u16 wd;
959         int ret = 0;
960         DEFINE_WAIT(wait);
961
962         if (count == 0) {
963                 return 0;
964         } else if (count & 0x1) {
965                 printk(KERN_ERR
966                        "omapdsp: odd count is illegal for DSP task device.\n");
967                 return -EINVAL;
968         } else {
969                 /* force! */
970                 count = 2;
971         }
972
973         if (taskdev_lock_and_statelock_attached(dev, &dev->write_mutex))
974                 return -ENODEV;
975
976         prepare_to_wait(&dev->write_wait_q, &wait, TASK_INTERRUPTIBLE);
977         if (dev->wsz == 0)
978                 schedule();
979         finish_wait(&dev->write_wait_q, &wait);
980         if (dev->wsz == 0) {
981                 /* failure */
982                 if (signal_pending(current))
983                         ret = -EINTR;
984                 goto up_out;
985         }
986
987         if (copy_from_user(&wd, buf, count)) {
988                 ret = -EFAULT;
989                 goto up_out;
990         }
991
992         spin_lock(&dev->wsz_lock);
993         if (mbcompose_send(WDSND, dev->task->tid, wd) < 0) {
994                 spin_unlock(&dev->wsz_lock);
995                 goto up_out;
996         }
997         ret = count;
998         if (rcvtyp_acv(dev->task->ttyp))
999                 dev->wsz = 0;
1000         spin_unlock(&dev->wsz_lock);
1001
1002  up_out:
1003         taskdev_unlock_and_stateunlock(dev, &dev->write_mutex);
1004         return ret;
1005 }
1006
1007 static ssize_t dsp_task_write_bk(struct file *file, const char __user *buf,
1008                                  size_t count, loff_t *ppos)
1009 {
1010         unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
1011         struct taskdev *dev = taskdev[minor];
1012         int ret = 0;
1013         DEFINE_WAIT(wait);
1014
1015         if (count == 0) {
1016                 return 0;
1017         } else if (count & 0x1) {
1018                 printk(KERN_ERR
1019                        "omapdsp: odd count is illegal for DSP task device.\n");
1020                 return -EINVAL;
1021         } else if ((int)buf & 0x1) {
1022                 printk(KERN_ERR
1023                        "omapdsp: buf should be word aligned for "
1024                        "dsp_task_write().\n");
1025                 return -EINVAL;
1026         }
1027
1028         if (taskdev_lock_and_statelock_attached(dev, &dev->write_mutex))
1029                 return -ENODEV;
1030
1031         prepare_to_wait(&dev->write_wait_q, &wait, TASK_INTERRUPTIBLE);
1032         if (dev->wsz == 0)
1033                 schedule();
1034         finish_wait(&dev->write_wait_q, &wait);
1035         if (dev->wsz == 0) {
1036                 /* failure */
1037                 if (signal_pending(current))
1038                         ret = -EINTR;
1039                 goto up_out;
1040         }
1041
1042         if (count > dev->wsz)
1043                 count = dev->wsz;
1044
1045         if (rcvtyp_pvt(dev->task->ttyp)) {
1046                 /* private */
1047                 struct ipbuf_p *ipbp = dev->task->ipbuf_pvt_w;
1048                 unsigned char *dst;
1049
1050                 if (dsp_mem_enable(ipbp) < 0) {
1051                         ret = -EBUSY;
1052                         goto up_out;
1053                 }
1054                 dst = MKVIRT(ipbp->ah, ipbp->al);
1055                 if (dsp_address_validate(dst, count, "task %s write buffer",
1056                                          dev->task->name) < 0) {
1057                         ret = -EINVAL;
1058                         goto pv_out1;
1059                 }
1060                 if (dsp_mem_enable(dst) < 0) {
1061                         ret = -EBUSY;
1062                         goto pv_out1;
1063                 }
1064                 if (copy_from_user_dsp(dst, buf, count)) {
1065                         ret = -EFAULT;
1066                         goto pv_out2;
1067                 }
1068                 ipbp->c = count/2;
1069                 ipbp->s = dev->task->tid;
1070                 spin_lock(&dev->wsz_lock);
1071                 if (mbcompose_send(BKSNDP, dev->task->tid, 0) == 0) {
1072                         if (rcvtyp_acv(dev->task->ttyp))
1073                                 dev->wsz = 0;
1074                         ret = count;
1075                 }
1076                 spin_unlock(&dev->wsz_lock);
1077         pv_out2:
1078                 dsp_mem_disable(dst);
1079         pv_out1:
1080                 dsp_mem_disable(ipbp);
1081         } else {
1082                 /* global */
1083                 struct ipbuf_head *ipb_h;
1084
1085                 if (dsp_mem_enable_ipbuf() < 0) {
1086                         ret = -EBUSY;
1087                         goto up_out;
1088                 }
1089                 if ((ipb_h = get_free_ipbuf(dev->task->tid)) == NULL)
1090                         goto gb_out;
1091                 if (copy_from_user_dsp(ipb_h->p->d, buf, count)) {
1092                         release_ipbuf(ipb_h);
1093                         ret = -EFAULT;
1094                         goto gb_out;
1095                 }
1096                 ipb_h->p->c  = count/2;
1097                 ipb_h->p->sa = dev->task->tid;
1098                 spin_lock(&dev->wsz_lock);
1099                 if (mbcompose_send(BKSND, dev->task->tid, ipb_h->bid) == 0) {
1100                         if (rcvtyp_acv(dev->task->ttyp))
1101                                 dev->wsz = 0;
1102                         ret = count;
1103                         ipb_bsycnt_inc(&ipbcfg);
1104                 } else
1105                         release_ipbuf(ipb_h);
1106                 spin_unlock(&dev->wsz_lock);
1107         gb_out:
1108                 dsp_mem_disable_ipbuf();
1109         }
1110
1111  up_out:
1112         taskdev_unlock_and_stateunlock(dev, &dev->write_mutex);
1113         return ret;
1114 }
1115
1116 static unsigned int dsp_task_poll(struct file * file, poll_table * wait)
1117 {
1118         unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
1119         struct taskdev *dev = taskdev[minor];
1120         struct dsptask *task = dev->task;
1121         unsigned int mask = 0;
1122
1123         if (!devstate_read_lock_and_test(dev, TASKDEV_ST_ATTACHED))
1124                 return 0;
1125         poll_wait(file, &dev->read_wait_q, wait);
1126         poll_wait(file, &dev->write_wait_q, wait);
1127         if (sndtyp_psv(task->ttyp) ||
1128             (sndtyp_wd(task->ttyp) && !fifo_empty(&dev->rcvdt.fifo)) ||
1129             (sndtyp_bk(task->ttyp) && !ipblink_empty(&dev->rcvdt.bk.link)))
1130                 mask |= POLLIN | POLLRDNORM;
1131         if (dev->wsz)
1132                 mask |= POLLOUT | POLLWRNORM;
1133         devstate_read_unlock(dev);
1134
1135         return mask;
1136 }
1137
1138 static int dsp_tctl_issue(struct taskdev *dev, u16 cmd, int argc, u16 argv[])
1139 {
1140         int tctl_argc;
1141         struct mb_exarg mbarg, *mbargp;
1142         int interactive;
1143         u8 tid;
1144         int ret = 0;
1145
1146         if (cmd < 0x8000) {
1147                 /*
1148                  * 0x0000 - 0x7fff
1149                  * system reserved TCTL commands
1150                  */
1151                 switch (cmd) {
1152                 case TCTL_TEN:
1153                 case TCTL_TDIS:
1154                         tctl_argc = 0;
1155                         interactive = 0;
1156                         break;
1157                 default:
1158                         return -EINVAL;
1159                 }
1160         }
1161         /*
1162          * 0x8000 - 0xffff
1163          * user-defined TCTL commands
1164          */
1165         else if (cmd < 0x8100) {
1166                 /* 0x8000-0x80ff: no arg, non-interactive */
1167                 tctl_argc = 0;
1168                 interactive = 0;
1169         } else if (cmd < 0x8200) {
1170                 /* 0x8100-0x81ff: 1 arg, non-interactive */
1171                 tctl_argc = 1;
1172                 interactive = 0;
1173         } else if (cmd < 0x9000) {
1174                 /* 0x8200-0x8fff: reserved */
1175                 return -EINVAL;
1176         } else if (cmd < 0x9100) {
1177                 /* 0x9000-0x90ff: no arg, interactive */
1178                 tctl_argc = 0;
1179                 interactive = 1;
1180         } else if (cmd < 0x9200) {
1181                 /* 0x9100-0x91ff: 1 arg, interactive */
1182                 tctl_argc = 1;
1183                 interactive = 1;
1184         } else {
1185                 /* 0x9200-0xffff: reserved */
1186                 return -EINVAL;
1187         }
1188
1189         /*
1190          * if argc < 0, use tctl_argc as is.
1191          * if argc >= 0, check arg count.
1192          */
1193         if ((argc >= 0) && (argc != tctl_argc))
1194                 return -EINVAL;
1195
1196         /*
1197          * issue TCTL
1198          */
1199         if (taskdev_lock_interruptible(dev, &dev->tctl_mutex))
1200                 return -EINTR;
1201
1202         tid = dev->task->tid;
1203         if (tctl_argc > 0) {
1204                 mbarg.argc = tctl_argc;
1205                 mbarg.tid  = tid;
1206                 mbarg.argv = argv;
1207                 mbargp = &mbarg;
1208         } else
1209                 mbargp = NULL;
1210
1211         if (interactive) {
1212                 dev->tctl_stat = -EINVAL;
1213
1214                 mbcompose_send_and_wait_exarg(TCTL, tid, cmd, mbargp,
1215                                               &dev->tctl_wait_q);
1216                 if (signal_pending(current)) {
1217                         ret = -EINTR;
1218                         goto up_out;
1219                 }
1220                 if ((ret = dev->tctl_stat) < 0) {
1221                         printk(KERN_ERR "omapdsp: TCTL not responding.\n");
1222                         goto up_out;
1223                 }
1224         } else
1225                 mbcompose_send_exarg(TCTL, tid, cmd, mbargp);
1226
1227 up_out:
1228         mutex_unlock(&dev->tctl_mutex);
1229         return ret;
1230 }
1231
1232 static int dsp_task_ioctl(struct inode *inode, struct file *file,
1233                           unsigned int cmd, unsigned long arg)
1234 {
1235         unsigned int minor = MINOR(inode->i_rdev);
1236         struct taskdev *dev = taskdev[minor];
1237         int ret;
1238
1239         if (cmd < 0x10000) {
1240                 /* issue TCTL */
1241                 u16 mbargv[1];
1242
1243                 mbargv[0] = arg & 0xffff;
1244                 return dsp_tctl_issue(dev, cmd, -1, mbargv);
1245         }
1246
1247         /* non TCTL ioctls */
1248         switch (cmd) {
1249
1250         case TASK_IOCTL_LOCK:
1251                 ret = taskdev_lock(dev);
1252                 break;
1253
1254         case TASK_IOCTL_UNLOCK:
1255                 ret = taskdev_unlock(dev);
1256                 break;
1257
1258         case TASK_IOCTL_BFLSH:
1259                 if (taskdev_lock_and_statelock_attached(dev, &dev->read_mutex))
1260                         return -ENODEV;
1261                 ret = taskdev_flush_buf(dev);
1262                 taskdev_unlock_and_stateunlock(dev, &dev->read_mutex);
1263                 break;
1264
1265         case TASK_IOCTL_SETBSZ:
1266                 if (taskdev_lock_and_statelock_attached(dev, &dev->read_mutex))
1267                         return -ENODEV;
1268                 ret = taskdev_set_fifosz(dev, arg);
1269                 taskdev_unlock_and_stateunlock(dev, &dev->read_mutex);
1270                 break;
1271
1272         case TASK_IOCTL_GETNAME:
1273                 ret = 0;
1274                 if (copy_to_user((void __user *)arg, dev->name,
1275                                  strlen(dev->name) + 1))
1276                         ret = -EFAULT;
1277                 break;
1278
1279         default:
1280                 ret = -ENOIOCTLCMD;
1281
1282         }
1283
1284         return ret;
1285 }
1286
1287 static void dsp_task_mmap_open(struct vm_area_struct *vma)
1288 {
1289         struct taskdev *dev = (struct taskdev *)vma->vm_private_data;
1290         struct dsptask *task;
1291         size_t len = vma->vm_end - vma->vm_start;
1292
1293         BUG_ON(!(dev->state & TASKDEV_ST_ATTACHED));
1294         task = dev->task;
1295         omap_mmu_exmap_use(&dsp_mmu, task->map_base, len);
1296 }
1297
1298 static void dsp_task_mmap_close(struct vm_area_struct *vma)
1299 {
1300         struct taskdev *dev = (struct taskdev *)vma->vm_private_data;
1301         struct dsptask *task;
1302         size_t len = vma->vm_end - vma->vm_start;
1303
1304         BUG_ON(!(dev->state & TASKDEV_ST_ATTACHED));
1305         task = dev->task;
1306         omap_mmu_exmap_unuse(&dsp_mmu, task->map_base, len);
1307 }
1308
1309 /**
1310  * On demand page allocation is not allowed. The mapping area is defined by
1311  * corresponding DSP tasks.
1312  */
1313 static struct page *dsp_task_mmap_nopage(struct vm_area_struct *vma,
1314                                          unsigned long address, int *type)
1315 {
1316         return NOPAGE_SIGBUS;
1317 }
1318
1319 static struct vm_operations_struct dsp_task_vm_ops = {
1320         .open = dsp_task_mmap_open,
1321         .close = dsp_task_mmap_close,
1322         .nopage = dsp_task_mmap_nopage,
1323 };
1324
1325 static int dsp_task_mmap(struct file *filp, struct vm_area_struct *vma)
1326 {
1327         void *tmp_vadr;
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;
1333         int ret = 0;
1334
1335         if (!devstate_read_lock_and_test(dev, TASKDEV_ST_ATTACHED))
1336                 return -ENODEV;
1337         task = dev->task;
1338
1339         /*
1340          * Don't swap this area out
1341          * Don't dump this area to a core file
1342          */
1343         vma->vm_flags |= VM_RESERVED | VM_IO;
1344
1345         /* Do not cache this area */
1346         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1347
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;
1352         do {
1353                 tmp_padr = omap_mmu_virt_to_phys(&dsp_mmu, tmp_vadr, &tmp_len);
1354                 if (tmp_padr == 0) {
1355                         printk(KERN_ERR
1356                                "omapdsp: task %s: illegal address "
1357                                "for mmap: %p", task->name, tmp_vadr);
1358                         /* partial mapping will be cleared in upper layer */
1359                         ret = -EINVAL;
1360                         goto unlock_out;
1361                 }
1362                 if (tmp_len > req_len)
1363                         tmp_len = req_len;
1364
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) {
1370                         printk(KERN_ERR
1371                                "omapdsp: task %s: remap_page_range() failed.\n",
1372                                task->name);
1373                         /* partial mapping will be cleared in upper layer */
1374                         ret = -EINVAL;
1375                         goto unlock_out;
1376                 }
1377
1378                 req_len   -= tmp_len;
1379                 tmp_vmadr += tmp_len;
1380                 tmp_vadr  += tmp_len;
1381         } while (req_len);
1382
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);
1386
1387 unlock_out:
1388         devstate_read_unlock(dev);
1389         return ret;
1390 }
1391
1392 static int dsp_task_open(struct inode *inode, struct file *file)
1393 {
1394         unsigned int minor = MINOR(inode->i_rdev);
1395         struct taskdev *dev;
1396         int ret = 0;
1397
1398         if ((minor >= TASKDEV_MAX) || ((dev = taskdev[minor]) == NULL))
1399                 return -ENODEV;
1400
1401  restart:
1402         mutex_lock(&dev->usecount_lock);
1403         down_write(&dev->state_sem);
1404
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:
1408                 break;
1409         case TASKDEV_ST_ATTACHED:
1410                 goto attached;
1411
1412         case TASKDEV_ST_INVALID:
1413                 up_write(&dev->state_sem);
1414                 mutex_unlock(&dev->usecount_lock);
1415                 return -ENODEV;
1416
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)
1425                         return -EINTR;
1426                 devstate_write_unlock(dev);
1427                 goto restart;
1428         }
1429
1430         /* NOTASK */
1431         set_taskdev_state(dev, TASKDEV_ST_ADDREQ);
1432         /* wake up twch daemon for tadd */
1433         dsp_twch_touch();
1434         up_write(&dev->state_sem);
1435         if (devstate_write_lock(dev, TASKDEV_ST_ATTACHED |
1436                                 TASKDEV_ST_ADDFAIL) < 0) {
1437                 /* cancelled */
1438                 if (!devstate_write_lock_and_test(dev, TASKDEV_ST_ADDREQ)) {
1439                         mutex_unlock(&dev->usecount_lock);
1440                         /* out of control ??? */
1441                         return -EINTR;
1442                 }
1443                 set_taskdev_state(dev, TASKDEV_ST_NOTASK);
1444                 ret = -EINTR;
1445                 goto change_out;
1446         }
1447         if (dev->state & TASKDEV_ST_ADDFAIL) {
1448                 printk(KERN_ERR "omapdsp: task attach failed for %s!\n",
1449                        dev->name);
1450                 ret = -EBUSY;
1451                 set_taskdev_state(dev, TASKDEV_ST_NOTASK);
1452                 goto change_out;
1453         }
1454
1455  attached:
1456         ret = proc_list_add(&dev->proc_list_lock,
1457                             &dev->proc_list, current, file);
1458         if (ret)
1459                 goto out;
1460
1461         dev->usecount++;
1462         file->f_op = &dev->fops;
1463         up_write(&dev->state_sem);
1464         mutex_unlock(&dev->usecount_lock);
1465
1466 #ifdef DSP_PTE_FREE     /* not used currently. */
1467         dsp_map_update(current);
1468         dsp_cur_users_add(current);
1469 #endif /* DSP_PTE_FREE */
1470         return 0;
1471
1472  change_out:
1473         wake_up_interruptible_all(&dev->state_wait_q);
1474  out:
1475         up_write(&dev->state_sem);
1476         mutex_unlock(&dev->usecount_lock);
1477         return ret;
1478 }
1479
1480 static int dsp_task_release(struct inode *inode, struct file *file)
1481 {
1482         unsigned int minor = MINOR(inode->i_rdev);
1483         struct taskdev *dev = taskdev[minor];
1484
1485 #ifdef DSP_PTE_FREE     /* not used currently. */
1486         dsp_cur_users_del(current);
1487 #endif /* DSP_PTE_FREE */
1488
1489         if (has_taskdev_lock(dev))
1490                 taskdev_unlock(dev);
1491
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);
1497                 return 0;
1498         }
1499
1500         /* usecount == 0 */
1501         down_write(&dev->state_sem);
1502
1503         /* state can be ATTACHED/FREEZED, KILLING or GARBAGE here. */
1504         switch (dev->state & TASKDEV_ST_STATE_MASK) {
1505
1506         case TASKDEV_ST_KILLING:
1507                 break;
1508
1509         case TASKDEV_ST_GARBAGE:
1510                 set_taskdev_state(dev, TASKDEV_ST_NOTASK);
1511                 wake_up_interruptible_all(&dev->state_wait_q);
1512                 break;
1513
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 */
1519                         dsp_twch_touch();
1520                 }
1521                 break;
1522
1523         }
1524
1525         up_write(&dev->state_sem);
1526         mutex_unlock(&dev->usecount_lock);
1527         return 0;
1528 }
1529
1530 /*
1531  * mkdev / rmdev
1532  */
1533 int dsp_mkdev(char *name)
1534 {
1535         struct taskdev *dev;
1536         int status;
1537         unsigned char minor;
1538         int ret;
1539
1540         if (dsp_cfgstat_get_stat() != CFGSTAT_READY) {
1541                 printk(KERN_ERR "omapdsp: dsp has not been configured.\n");
1542                 return -EINVAL;
1543         }
1544
1545         if (mutex_lock_interruptible(&devmgr_lock))
1546                 return -EINTR;
1547
1548         /* naming check */
1549         for (minor = 0; minor < TASKDEV_MAX; minor++) {
1550                 if (taskdev[minor] && !strcmp(taskdev[minor]->name, name)) {
1551                         printk(KERN_ERR
1552                                "omapdsp: task device name %s is already "
1553                                "in use.\n", name);
1554                         ret = -EINVAL;
1555                         goto out;
1556                 }
1557         }
1558
1559         /* find free minor number */
1560         for (minor = n_task; minor < TASKDEV_MAX; minor++) {
1561                 if (taskdev[minor] == NULL)
1562                         goto do_make;
1563         }
1564         printk(KERN_ERR "omapdsp: Too many task devices.\n");
1565         ret = -EBUSY;
1566         goto out;
1567
1568 do_make:
1569         if ((dev = kzalloc(sizeof(struct taskdev), GFP_KERNEL)) == NULL) {
1570                 ret = -ENOMEM;
1571                 goto out;
1572         }
1573         if ((status = taskdev_init(dev, name, minor)) < 0) {
1574                 kfree(dev);
1575                 ret = status;
1576                 goto out;
1577         }
1578         ret = minor;
1579
1580 out:
1581         mutex_unlock(&devmgr_lock);
1582         return ret;
1583 }
1584
1585 int dsp_rmdev(char *name)
1586 {
1587         unsigned char minor;
1588         int status;
1589         int ret;
1590
1591         if (dsp_cfgstat_get_stat() != CFGSTAT_READY) {
1592                 printk(KERN_ERR "omapdsp: dsp has not been configured.\n");
1593                 return -EINVAL;
1594         }
1595
1596         if (mutex_lock_interruptible(&devmgr_lock))
1597                 return -EINTR;
1598
1599         /* find in dynamic devices */
1600         for (minor = n_task; minor < TASKDEV_MAX; minor++) {
1601                 if (taskdev[minor] && !strcmp(taskdev[minor]->name, name))
1602                         goto do_remove;
1603         }
1604
1605         /* find in static devices */
1606         for (minor = 0; minor < n_task; minor++) {
1607                 if (taskdev[minor] && !strcmp(taskdev[minor]->name, name)) {
1608                         printk(KERN_ERR
1609                                "omapdsp: task device %s is static.\n", name);
1610                         ret = -EINVAL;
1611                         goto out;
1612                 }
1613         }
1614
1615         printk(KERN_ERR "omapdsp: task device %s not found.\n", name);
1616         return -EINVAL;
1617
1618 do_remove:
1619         ret = minor;
1620         if ((status = dsp_rmdev_minor(minor)) < 0)
1621                 ret = status;
1622 out:
1623         mutex_unlock(&devmgr_lock);
1624         return ret;
1625 }
1626
1627 static int dsp_rmdev_minor(unsigned char minor)
1628 {
1629         struct taskdev *dev = taskdev[minor];
1630
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)) {
1635                         /*
1636                          * task is working. kill it.
1637                          * ATTACHED -> FREEZED can be changed under
1638                          * down_read of state_sem..
1639                          */
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);
1644                 }
1645                 up_read(&dev->state_sem);
1646                 schedule();
1647         }
1648
1649         switch (dev->state & TASKDEV_ST_STATE_MASK) {
1650
1651         case TASKDEV_ST_NOTASK:
1652         case TASKDEV_ST_INVALID:
1653                 /* fine */
1654                 goto notask;
1655
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);
1662                 goto invalidate;
1663
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);
1668                 break;
1669
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);
1674                 break;
1675
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. */
1682                 break;
1683
1684         }
1685
1686         up_write(&dev->state_sem);
1687
1688 invalidate:
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)) {
1692                 printk(KERN_WARNING
1693                        "omapdsp: illegal device state (%s) on rmdev %s.\n",
1694                        devstate_name(dev->state), dev->name);
1695         }
1696 notask:
1697         set_taskdev_state(dev, TASKDEV_ST_INVALID);
1698         devstate_read_unlock(dev);
1699
1700         taskdev_delete(minor);
1701         kfree(dev);
1702
1703         return 0;
1704 }
1705
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,
1712 };
1713
1714 static void dsptask_dev_release(struct device *dev)
1715 {
1716 }
1717
1718 static int taskdev_init(struct taskdev *dev, char *name, unsigned char minor)
1719 {
1720         int ret;
1721         struct device *task_dev;
1722
1723         taskdev[minor] = dev;
1724
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;
1736         dev->lock_pid = 0;
1737
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);
1741         dev->usecount = 0;
1742         mutex_init(&dev->usecount_lock);
1743         memcpy(&dev->fops, &dsp_task_fops, sizeof(struct file_operations));
1744
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);
1750         if (ret) {
1751                 printk(KERN_ERR "device_register failed: %d\n", ret);
1752                 return ret;
1753         }
1754         ret = device_create_file(&dev->dev, &dev_attr_devname);
1755         if (ret)
1756                 goto fail_create_devname;
1757         ret = device_create_file(&dev->dev, &dev_attr_devstate);
1758         if (ret)
1759                 goto fail_create_devstate;
1760         ret = device_create_file(&dev->dev, &dev_attr_proc_list);
1761         if (ret)
1762                 goto fail_create_proclist;
1763
1764         task_dev = device_create(dsp_task_class, NULL,
1765                                  MKDEV(OMAP_DSP_TASK_MAJOR, minor),
1766                                  "dsptask%d", (int)minor);
1767         
1768         if (unlikely(IS_ERR(task_dev))) {
1769                 ret = -EINVAL;
1770                 goto fail_create_taskclass;
1771         }
1772
1773         init_waitqueue_head(&dev->state_wait_q);
1774         init_rwsem(&dev->state_sem);
1775
1776         return 0;
1777
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);
1786         return ret;
1787 }
1788
1789 static void taskdev_delete(unsigned char minor)
1790 {
1791         struct taskdev *dev = taskdev[minor];
1792
1793         if (!dev)
1794                 return;
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;
1802 }
1803
1804 static int taskdev_attach_task(struct taskdev *dev, struct dsptask *task)
1805 {
1806         u16 ttyp = task->ttyp;
1807         int ret;
1808
1809         dev->fops.read =
1810                 sndtyp_acv(ttyp) ?
1811                 sndtyp_wd(ttyp) ? dsp_task_read_wd_acv:
1812                 /* sndtyp_bk */   dsp_task_read_bk_acv:
1813                 /* sndtyp_psv */
1814                 sndtyp_wd(ttyp) ? dsp_task_read_wd_psv:
1815                 /* sndtyp_bk */   dsp_task_read_bk_psv;
1816         if (sndtyp_wd(ttyp)) {
1817                 /* word */
1818                 size_t fifosz;
1819
1820                 fifosz = sndtyp_psv(ttyp) ? 2 : /* passive */
1821                         32;     /* active */
1822                 if (init_fifo(&dev->rcvdt.fifo, fifosz) < 0) {
1823                         printk(KERN_ERR
1824                                "omapdsp: unable to allocate receive buffer. "
1825                                "(%d bytes for %s)\n", fifosz, dev->name);
1826                         return -ENOMEM;
1827                 }
1828         } else {
1829                 /* block */
1830                 INIT_IPBLINK(&dev->rcvdt.bk.link);
1831                 dev->rcvdt.bk.rp = 0;
1832         }
1833
1834         dev->fops.write =
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 */
1840
1841         if (task->map_length)
1842                 dev->fops.mmap = dsp_task_mmap;
1843
1844         ret = device_create_file(&dev->dev, &dev_attr_taskname);
1845         if (unlikely(ret))
1846                 goto fail_create_taskname;
1847         ret = device_create_file(&dev->dev, &dev_attr_ttyp);
1848         if (unlikely(ret))
1849                 goto fail_create_ttyp;
1850         ret = device_create_file(&dev->dev, &dev_attr_wsz);
1851         if (unlikely(ret))
1852                 goto fail_create_wsz;
1853         if (task->map_length) {
1854                 ret = device_create_file(&dev->dev, &dev_attr_mmap);
1855                 if (unlikely(ret))
1856                         goto fail_create_mmap;
1857         }
1858         if (sndtyp_wd(ttyp)) {
1859                 ret = device_create_file(&dev->dev, &dev_attr_fifosz);
1860                 if (unlikely(ret))
1861                         goto fail_create_fifosz;
1862                 ret = device_create_file(&dev->dev, &dev_attr_fifocnt);
1863                 if (unlikely(ret))
1864                         goto fail_create_fifocnt;
1865         } else {
1866                 ret = device_create_file(&dev->dev, &dev_attr_ipblink);
1867                 if (unlikely(ret))
1868                         goto fail_create_ipblink;
1869         }
1870
1871         dev->task = task;
1872         task->dev = dev;
1873
1874         return 0;
1875
1876  fail_create_fifocnt:
1877         device_remove_file(&dev->dev, &dev_attr_fifosz);
1878  fail_create_ipblink:
1879  fail_create_fifosz:
1880         if (task->map_length)
1881                 device_remove_file(&dev->dev, &dev_attr_mmap);
1882  fail_create_mmap:
1883         device_remove_file(&dev->dev, &dev_attr_wsz);
1884  fail_create_wsz:
1885         device_remove_file(&dev->dev, &dev_attr_ttyp);
1886  fail_create_ttyp:
1887         device_remove_file(&dev->dev, &dev_attr_taskname);
1888  fail_create_taskname:
1889         if (task->map_length)
1890                 dev->fops.mmap = NULL;
1891
1892         dev->fops.write = NULL;
1893         dev->wsz = 0;
1894
1895         dev->fops.read = NULL;
1896         taskdev_flush_buf(dev);
1897
1898         if (sndtyp_wd(ttyp))
1899                 free_fifo(&dev->rcvdt.fifo);
1900
1901         dev->task = NULL;
1902
1903         return ret;
1904 }
1905
1906 static void taskdev_detach_task(struct taskdev *dev)
1907 {
1908         u16 ttyp = dev->task->ttyp;
1909
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);
1915         } else
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;
1921         }
1922
1923         dev->fops.read = NULL;
1924         taskdev_flush_buf(dev);
1925         if (sndtyp_wd(ttyp))
1926                 free_fifo(&dev->rcvdt.fifo);
1927
1928         dev->fops.write = NULL;
1929         dev->wsz = 0;
1930
1931         pr_info("omapdsp: taskdev %s disabled.\n", dev->name);
1932         dev->task = NULL;
1933 }
1934
1935 /*
1936  * tadd / tdel / tkill
1937  */
1938 static int dsp_tadd(struct taskdev *dev, dsp_long_t adr)
1939 {
1940         struct dsptask *task;
1941         struct mb_exarg arg;
1942         u8 tid, tid_response;
1943         u16 argv[2];
1944         int ret = 0;
1945
1946         if (!devstate_write_lock_and_test(dev, TASKDEV_ST_ADDREQ)) {
1947                 printk(KERN_ERR
1948                        "omapdsp: taskdev %s is not requesting for tadd. "
1949                        "(state is %s)\n", dev->name, devstate_name(dev->state));
1950                 return -EINVAL;
1951         }
1952         set_taskdev_state(dev, TASKDEV_ST_ADDING);
1953         devstate_write_unlock(dev);
1954
1955         if (adr == TADD_ABORTADR) {
1956                 /* aborting tadd intentionally */
1957                 pr_info("omapdsp: tadd address is ABORTADR.\n");
1958                 goto fail_out;
1959         }
1960         if (adr >= DSPSPACE_SIZE) {
1961                 printk(KERN_ERR
1962                        "omapdsp: illegal address 0x%08x for tadd\n", adr);
1963                 ret = -EINVAL;
1964                 goto fail_out;
1965         }
1966
1967         adr >>= 1;      /* word address */
1968         argv[0] = adr >> 16;    /* addrh */
1969         argv[1] = adr & 0xffff; /* addrl */
1970
1971         if (mutex_lock_interruptible(&cfg_lock)) {
1972                 ret = -EINTR;
1973                 goto fail_out;
1974         }
1975         cfg_tid = TID_ANON;
1976         cfg_cmd = MBOX_CMD_DSP_TADD;
1977         arg.tid  = TID_ANON;
1978         arg.argc = 2;
1979         arg.argv = argv;
1980
1981         if (dsp_mem_sync_inc() < 0) {
1982                 printk(KERN_ERR "omapdsp: memory sync failed!\n");
1983                 ret = -EBUSY;
1984                 goto fail_out;
1985         }
1986         mbcompose_send_and_wait_exarg(TADD, 0, 0, &arg, &cfg_wait_q);
1987
1988         tid = cfg_tid;
1989         cfg_tid = TID_ANON;
1990         cfg_cmd = 0;
1991         mutex_unlock(&cfg_lock);
1992
1993         if (tid == TID_ANON) {
1994                 printk(KERN_ERR "omapdsp: tadd failed!\n");
1995                 ret = -EINVAL;
1996                 goto fail_out;
1997         }
1998         if ((tid < n_task) || dsptask[tid]) {
1999                 printk(KERN_ERR "omapdsp: illegal tid (%d)!\n", tid);
2000                 ret = -EINVAL;
2001                 goto fail_out;
2002         }
2003         if ((task = kzalloc(sizeof(struct dsptask), GFP_KERNEL)) == NULL) {
2004                 ret = -ENOMEM;
2005                 goto del_out;
2006         }
2007
2008         if ((ret = dsp_task_config(task, tid)) < 0)
2009                 goto free_out;
2010
2011         if (strcmp(dev->name, task->name)) {
2012                 printk(KERN_ERR
2013                        "omapdsp: task name (%s) doesn't match with "
2014                        "device name (%s).\n", task->name, dev->name);
2015                 ret = -EINVAL;
2016                 goto free_out;
2017         }
2018
2019         if ((ret = taskdev_attach_task(dev, task)) < 0)
2020                 goto free_out;
2021
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);
2026         return 0;
2027
2028 free_out:
2029         kfree(task);
2030
2031 del_out:
2032         printk(KERN_ERR "omapdsp: deleting the task...\n");
2033
2034         set_taskdev_state(dev, TASKDEV_ST_DELING);
2035
2036         if (mutex_lock_interruptible(&cfg_lock)) {
2037                 printk(KERN_ERR "omapdsp: aborting tdel process. "
2038                                 "DSP side could be corrupted.\n");
2039                 goto fail_out;
2040         }
2041         cfg_tid = TID_ANON;
2042         cfg_cmd = MBOX_CMD_DSP_TDEL;
2043         mbcompose_send_and_wait(TDEL, tid, TDEL_KILL, &cfg_wait_q);
2044         tid_response = cfg_tid;
2045         cfg_tid = TID_ANON;
2046         cfg_cmd = 0;
2047         mutex_unlock(&cfg_lock);
2048
2049         if (tid_response != tid)
2050                 printk(KERN_ERR "omapdsp: tdel failed. "
2051                                 "DSP side could be corrupted.\n");
2052
2053 fail_out:
2054         set_taskdev_state(dev, TASKDEV_ST_ADDFAIL);
2055         wake_up_interruptible_all(&dev->state_wait_q);
2056         return ret;
2057 }
2058
2059 int dsp_tadd_minor(unsigned char minor, dsp_long_t adr)
2060 {
2061         struct taskdev *dev;
2062         int status;
2063         int ret;
2064
2065         if (mutex_lock_interruptible(&devmgr_lock))
2066                 return -EINTR;
2067
2068         if ((minor >= TASKDEV_MAX) || ((dev = taskdev[minor]) == NULL)) {
2069                 printk(KERN_ERR
2070                        "omapdsp: no task device with minor %d\n", minor);
2071                 ret = -EINVAL;
2072                 goto out;
2073         }
2074         ret = minor;
2075         if ((status = dsp_tadd(dev, adr)) < 0)
2076                 ret = status;
2077
2078 out:
2079         mutex_unlock(&devmgr_lock);
2080         return ret;
2081 }
2082
2083 static int dsp_tdel(struct taskdev *dev)
2084 {
2085         if (!devstate_write_lock_and_test(dev, TASKDEV_ST_DELREQ)) {
2086                 printk(KERN_ERR
2087                        "omapdsp: taskdev %s is not requesting for tdel. "
2088                        "(state is %s)\n", dev->name, devstate_name(dev->state));
2089                 return -EINVAL;
2090         }
2091         set_taskdev_state(dev, TASKDEV_ST_DELING);
2092         devstate_write_unlock(dev);
2093
2094         return dsp_tdel_bh(dev, TDEL_SAFE);
2095 }
2096
2097 int dsp_tdel_minor(unsigned char minor)
2098 {
2099         struct taskdev *dev;
2100         int status;
2101         int ret;
2102
2103         if (mutex_lock_interruptible(&devmgr_lock))
2104                 return -EINTR;
2105
2106         if ((minor >= TASKDEV_MAX) || ((dev = taskdev[minor]) == NULL)) {
2107                 printk(KERN_ERR
2108                        "omapdsp: no task device with minor %d\n", minor);
2109                 ret = -EINVAL;
2110                 goto out;
2111         }
2112
2113         ret = minor;
2114         if ((status = dsp_tdel(dev)) < 0)
2115                 ret = status;
2116
2117 out:
2118         mutex_unlock(&devmgr_lock);
2119         return ret;
2120 }
2121
2122 static int dsp_tkill(struct taskdev *dev)
2123 {
2124         while (!down_write_trylock(&dev->state_sem)) {
2125                 if (!devstate_read_lock_and_test(dev, (TASKDEV_ST_ATTACHED |
2126                                                        TASKDEV_ST_FREEZED))) {
2127                         printk(KERN_ERR
2128                                "omapdsp: task has not been attached for "
2129                                "taskdev %s\n", dev->name);
2130                         return -EINVAL;
2131                 }
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);
2138                 schedule();
2139         }
2140
2141         if (!(dev->state & (TASKDEV_ST_ATTACHED |
2142                             TASKDEV_ST_FREEZED))) {
2143                 printk(KERN_ERR
2144                        "omapdsp: task has not been attached for taskdev %s\n",
2145                        dev->name);
2146                 devstate_write_unlock(dev);
2147                 return -EINVAL;
2148         }
2149         if (!is_dynamic_task(dev->task->tid)) {
2150                 printk(KERN_ERR "omapdsp: task %s is not a dynamic task.\n",
2151                        dev->name);
2152                 devstate_write_unlock(dev);
2153                 return -EINVAL;
2154         }
2155         set_taskdev_state(dev, TASKDEV_ST_KILLING);
2156         devstate_write_unlock(dev);
2157
2158         return dsp_tdel_bh(dev, TDEL_KILL);
2159 }
2160
2161 int dsp_tkill_minor(unsigned char minor)
2162 {
2163         struct taskdev *dev;
2164         int status;
2165         int ret;
2166
2167         if (mutex_lock_interruptible(&devmgr_lock))
2168                 return -EINTR;
2169
2170         if ((minor >= TASKDEV_MAX) || ((dev = taskdev[minor]) == NULL)) {
2171                 printk(KERN_ERR
2172                        "omapdsp: no task device with minor %d\n", minor);
2173                 ret = -EINVAL;
2174                 goto out;
2175         }
2176
2177         ret = minor;
2178         if ((status = dsp_tkill(dev)) < 0)
2179                 ret = status;
2180
2181 out:
2182         mutex_unlock(&devmgr_lock);
2183         return ret;
2184 }
2185
2186 static int dsp_tdel_bh(struct taskdev *dev, u16 type)
2187 {
2188         struct dsptask *task;
2189         u8 tid, tid_response;
2190         int ret = 0;
2191
2192         task = dev->task;
2193         tid = task->tid;
2194         if (mutex_lock_interruptible(&cfg_lock)) {
2195                 if (type == TDEL_SAFE) {
2196                         set_taskdev_state(dev, TASKDEV_ST_DELREQ);
2197                         return -EINTR;
2198                 } else {
2199                         tid_response = TID_ANON;
2200                         ret = -EINTR;
2201                         goto detach_out;
2202                 }
2203         }
2204         cfg_tid = 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;
2208         cfg_tid = TID_ANON;
2209         cfg_cmd = 0;
2210         mutex_unlock(&cfg_lock);
2211
2212 detach_out:
2213         taskdev_detach_task(dev);
2214         dsp_task_unconfig(task);
2215         kfree(task);
2216
2217         if (tid_response != tid) {
2218                 printk(KERN_ERR "omapdsp: %s failed!\n",
2219                        (type == TDEL_SAFE) ? "tdel" : "tkill");
2220                 ret = -EINVAL;
2221         }
2222         down_write(&dev->state_sem);
2223         set_taskdev_state(dev, (dev->usecount > 0) ? TASKDEV_ST_GARBAGE :
2224                                            TASKDEV_ST_NOTASK);
2225         wake_up_interruptible_all(&dev->state_wait_q);
2226         up_write(&dev->state_sem);
2227
2228         return ret;
2229 }
2230
2231 /*
2232  * state inquiry
2233  */
2234 long taskdev_state_stale(unsigned char minor)
2235 {
2236         if (taskdev[minor]) {
2237                 long state = taskdev[minor]->state;
2238                 taskdev[minor]->state |= TASKDEV_ST_STALE;
2239                 return state;
2240         } else
2241                 return TASKDEV_ST_NOTASK;
2242 }
2243
2244 /*
2245  * functions called from mailbox interrupt routine
2246  */
2247 void mbox_wdsnd(struct mbcmd *mb)
2248 {
2249         u8 tid = mb->cmd_l;
2250         struct dsptask *task = dsptask[tid];
2251
2252         if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2253                 printk(KERN_ERR "mbox: WDSND with illegal tid! %d\n", tid);
2254                 return;
2255         }
2256         if (sndtyp_bk(task->ttyp)) {
2257                 printk(KERN_ERR
2258                        "mbox: WDSND from block sending task! (task%d)\n", tid);
2259                 return;
2260         }
2261         if (sndtyp_psv(task->ttyp) &&
2262             !waitqueue_active(&task->dev->read_wait_q)) {
2263                 printk(KERN_WARNING
2264                        "mbox: WDSND from passive sending task (task%d) "
2265                        "without request!\n", tid);
2266                 return;
2267         }
2268
2269         write_word_to_fifo(&task->dev->rcvdt.fifo, mb->data);
2270         wake_up_interruptible(&task->dev->read_wait_q);
2271 }
2272
2273 void mbox_wdreq(struct mbcmd *mb)
2274 {
2275         u8 tid = mb->cmd_l;
2276         struct dsptask *task = dsptask[tid];
2277         struct taskdev *dev;
2278
2279         if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2280                 printk(KERN_ERR "mbox: WDREQ with illegal tid! %d\n", tid);
2281                 return;
2282         }
2283         if (rcvtyp_psv(task->ttyp)) {
2284                 printk(KERN_ERR
2285                        "mbox: WDREQ from passive receiving task! (task%d)\n",
2286                        tid);
2287                 return;
2288         }
2289
2290         dev = task->dev;
2291         spin_lock(&dev->wsz_lock);
2292         dev->wsz = 2;
2293         spin_unlock(&dev->wsz_lock);
2294         wake_up_interruptible(&dev->write_wait_q);
2295 }
2296
2297 void mbox_bksnd(struct mbcmd *mb)
2298 {
2299         u8 tid = mb->cmd_l;
2300         u16 bid = mb->data;
2301         struct dsptask *task = dsptask[tid];
2302         struct ipbuf_head *ipb_h;
2303         u16 cnt;
2304
2305         if (bid >= ipbcfg.ln) {
2306                 printk(KERN_ERR "mbox: BKSND with illegal bid! %d\n", bid);
2307                 return;
2308         }
2309         ipb_h = bid_to_ipbuf(bid);
2310         ipb_bsycnt_dec(&ipbcfg);
2311         if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2312                 printk(KERN_ERR "mbox: BKSND with illegal tid! %d\n", tid);
2313                 goto unuse_ipbuf_out;
2314         }
2315         if (sndtyp_wd(task->ttyp)) {
2316                 printk(KERN_ERR
2317                        "mbox: BKSND from word sending task! (task%d)\n", tid);
2318                 goto unuse_ipbuf_out;
2319         }
2320         if (sndtyp_pvt(task->ttyp)) {
2321                 printk(KERN_ERR
2322                        "mbox: BKSND from private sending task! (task%d)\n", tid);
2323                 goto unuse_ipbuf_out;
2324         }
2325         if (sync_with_dsp(&ipb_h->p->sd, tid, 10) < 0) {
2326                 printk(KERN_ERR "mbox: BKSND - IPBUF sync failed!\n");
2327                 return;
2328         }
2329
2330         /* should be done in DSP, but just in case. */
2331         ipb_h->p->next = BID_NULL;
2332
2333         cnt = ipb_h->p->c;
2334         if (cnt > ipbcfg.lsz) {
2335                 printk(KERN_ERR "mbox: BKSND cnt(%d) > ipbuf line size(%d)!\n",
2336                        cnt, ipbcfg.lsz);
2337                 goto unuse_ipbuf_out;
2338         }
2339
2340         if (cnt == 0) {
2341                 /* 0-byte send from DSP */
2342                 unuse_ipbuf_nowait(ipb_h);
2343                 goto done;
2344         }
2345         ipblink_add_tail(&task->dev->rcvdt.bk.link, bid);
2346         /* we keep coming bid and return alternative line to DSP. */
2347         balance_ipbuf();
2348
2349 done:
2350         wake_up_interruptible(&task->dev->read_wait_q);
2351         return;
2352
2353 unuse_ipbuf_out:
2354         unuse_ipbuf_nowait(ipb_h);
2355         return;
2356 }
2357
2358 void mbox_bkreq(struct mbcmd *mb)
2359 {
2360         u8 tid = mb->cmd_l;
2361         u16 cnt = mb->data;
2362         struct dsptask *task = dsptask[tid];
2363         struct taskdev *dev;
2364
2365         if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2366                 printk(KERN_ERR "mbox: BKREQ with illegal tid! %d\n", tid);
2367                 return;
2368         }
2369         if (rcvtyp_wd(task->ttyp)) {
2370                 printk(KERN_ERR
2371                        "mbox: BKREQ from word receiving task! (task%d)\n", tid);
2372                 return;
2373         }
2374         if (rcvtyp_pvt(task->ttyp)) {
2375                 printk(KERN_ERR
2376                        "mbox: BKREQ from private receiving task! (task%d)\n",
2377                        tid);
2378                 return;
2379         }
2380         if (rcvtyp_psv(task->ttyp)) {
2381                 printk(KERN_ERR
2382                        "mbox: BKREQ from passive receiving task! (task%d)\n",
2383                        tid);
2384                 return;
2385         }
2386
2387         dev = task->dev;
2388         spin_lock(&dev->wsz_lock);
2389         dev->wsz = cnt*2;
2390         spin_unlock(&dev->wsz_lock);
2391         wake_up_interruptible(&dev->write_wait_q);
2392 }
2393
2394 void mbox_bkyld(struct mbcmd *mb)
2395 {
2396         u16 bid = mb->data;
2397         struct ipbuf_head *ipb_h;
2398
2399         if (bid >= ipbcfg.ln) {
2400                 printk(KERN_ERR "mbox: BKYLD with illegal bid! %d\n", bid);
2401                 return;
2402         }
2403         ipb_h = bid_to_ipbuf(bid);
2404
2405         /* should be done in DSP, but just in case. */
2406         ipb_h->p->next = BID_NULL;
2407
2408         /* we don't need to sync with DSP */
2409         ipb_bsycnt_dec(&ipbcfg);
2410         release_ipbuf(ipb_h);
2411 }
2412
2413 void mbox_bksndp(struct mbcmd *mb)
2414 {
2415         u8 tid = mb->cmd_l;
2416         struct dsptask *task = dsptask[tid];
2417         struct ipbuf_p *ipbp;
2418
2419         if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2420                 printk(KERN_ERR "mbox: BKSNDP with illegal tid! %d\n", tid);
2421                 return;
2422         }
2423         if (sndtyp_wd(task->ttyp)) {
2424                 printk(KERN_ERR
2425                        "mbox: BKSNDP from word sending task! (task%d)\n", tid);
2426                 return;
2427         }
2428         if (sndtyp_gbl(task->ttyp)) {
2429                 printk(KERN_ERR
2430                        "mbox: BKSNDP from non-private sending task! (task%d)\n",
2431                        tid);
2432                 return;
2433         }
2434
2435         /*
2436          * we should not have delayed block at this point
2437          * because read() routine releases the lock of the buffer and
2438          * until then DSP can't send next data.
2439          */
2440
2441         ipbp = task->ipbuf_pvt_r;
2442         if (sync_with_dsp(&ipbp->s, tid, 10) < 0) {
2443                 printk(KERN_ERR "mbox: BKSNDP - IPBUF sync failed!\n");
2444                 return;
2445         }
2446         pr_debug("mbox: ipbuf_pvt_r->a = 0x%08lx\n",
2447                MKLONG(ipbp->ah, ipbp->al));
2448         ipblink_add_pvt(&task->dev->rcvdt.bk.link);
2449         wake_up_interruptible(&task->dev->read_wait_q);
2450 }
2451
2452 void mbox_bkreqp(struct mbcmd *mb)
2453 {
2454         u8 tid = mb->cmd_l;
2455         struct dsptask *task = dsptask[tid];
2456         struct taskdev *dev;
2457         struct ipbuf_p *ipbp;
2458
2459         if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2460                 printk(KERN_ERR "mbox: BKREQP with illegal tid! %d\n", tid);
2461                 return;
2462         }
2463         if (rcvtyp_wd(task->ttyp)) {
2464                 printk(KERN_ERR
2465                        "mbox: BKREQP from word receiving task! (task%d)\n", tid);
2466                 return;
2467         }
2468         if (rcvtyp_gbl(task->ttyp)) {
2469                 printk(KERN_ERR
2470                        "mbox: BKREQP from non-private receiving task! (task%d)\n", tid);
2471                 return;
2472         }
2473         if (rcvtyp_psv(task->ttyp)) {
2474                 printk(KERN_ERR
2475                        "mbox: BKREQP from passive receiving task! (task%d)\n", tid);
2476                 return;
2477         }
2478
2479         ipbp = task->ipbuf_pvt_w;
2480         if (sync_with_dsp(&ipbp->s, TID_FREE, 10) < 0) {
2481                 printk(KERN_ERR "mbox: BKREQP - IPBUF sync failed!\n");
2482                 return;
2483         }
2484         pr_debug("mbox: ipbuf_pvt_w->a = 0x%08lx\n",
2485                MKLONG(ipbp->ah, ipbp->al));
2486         dev = task->dev;
2487         spin_lock(&dev->wsz_lock);
2488         dev->wsz = ipbp->c*2;
2489         spin_unlock(&dev->wsz_lock);
2490         wake_up_interruptible(&dev->write_wait_q);
2491 }
2492
2493 void mbox_tctl(struct mbcmd *mb)
2494 {
2495         u8 tid = mb->cmd_l;
2496         struct dsptask *task = dsptask[tid];
2497
2498         if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2499                 printk(KERN_ERR "mbox: TCTL with illegal tid! %d\n", tid);
2500                 return;
2501         }
2502
2503         if (!waitqueue_active(&task->dev->tctl_wait_q)) {
2504                 printk(KERN_WARNING "mbox: unexpected TCTL from DSP!\n");
2505                 return;
2506         }
2507
2508         task->dev->tctl_stat = mb->data;
2509         wake_up_interruptible(&task->dev->tctl_wait_q);
2510 }
2511
2512 void mbox_tcfg(struct mbcmd *mb)
2513 {
2514         u8 tid = mb->cmd_l;
2515         struct dsptask *task = dsptask[tid];
2516         u16 *tnm;
2517         volatile u16 *buf;
2518         int i;
2519
2520         if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2521                 printk(KERN_ERR "mbox: TCFG with illegal tid! %d\n", tid);
2522                 return;
2523         }
2524         if ((task->state != TASK_ST_CFGREQ) || (cfg_cmd != MBOX_CMD_DSP_TCFG)) {
2525                 printk(KERN_WARNING "mbox: unexpected TCFG from DSP!\n");
2526                 return;
2527         }
2528
2529         if (dsp_mem_enable(ipbuf_sys_da) < 0) {
2530                 printk(KERN_ERR "mbox: TCFG - ipbuf_sys_da read failed!\n");
2531                 dsp_mem_disable(ipbuf_sys_da);
2532                 goto out;
2533         }
2534         if (sync_with_dsp(&ipbuf_sys_da->s, tid, 10) < 0) {
2535                 printk(KERN_ERR "mbox: TCFG - IPBUF sync failed!\n");
2536                 dsp_mem_disable(ipbuf_sys_da);
2537                 goto out;
2538         }
2539
2540         /*
2541          * read configuration data on system IPBUF
2542          */
2543         buf = ipbuf_sys_da->d;
2544         task->ttyp        = buf[0];
2545         task->ipbuf_pvt_r = MKVIRT(buf[1], buf[2]);
2546         task->ipbuf_pvt_w = MKVIRT(buf[3], buf[4]);
2547         task->map_base    = MKVIRT(buf[5], buf[6]);
2548         task->map_length  = MKLONG(buf[7], buf[8]) << 1;        /* word -> byte */
2549         tnm               = MKVIRT(buf[9], buf[10]);
2550         release_ipbuf_pvt(ipbuf_sys_da);
2551         dsp_mem_disable(ipbuf_sys_da);
2552
2553         /*
2554          * copy task name string
2555          */
2556         if (dsp_address_validate(tnm, TNM_LEN, "task name buffer") < 0) {
2557                 task->name[0] = '\0';
2558                 goto out;
2559         }
2560
2561         for (i = 0; i < TNM_LEN-1; i++) {
2562                 /* avoiding byte access */
2563                 u16 tmp = tnm[i];
2564                 task->name[i] = tmp & 0x00ff;
2565                 if (!tmp)
2566                         break;
2567         }
2568         task->name[TNM_LEN-1] = '\0';
2569
2570         task->state = TASK_ST_READY;
2571 out:
2572         wake_up_interruptible(&cfg_wait_q);
2573 }
2574
2575 void mbox_tadd(struct mbcmd *mb)
2576 {
2577         u8 tid = mb->cmd_l;
2578
2579         if ((!waitqueue_active(&cfg_wait_q)) || (cfg_cmd != MBOX_CMD_DSP_TADD)) {
2580                 printk(KERN_WARNING "mbox: unexpected TADD from DSP!\n");
2581                 return;
2582         }
2583         cfg_tid = tid;
2584         wake_up_interruptible(&cfg_wait_q);
2585 }
2586
2587 void mbox_tdel(struct mbcmd *mb)
2588 {
2589         u8 tid = mb->cmd_l;
2590
2591         if ((!waitqueue_active(&cfg_wait_q)) || (cfg_cmd != MBOX_CMD_DSP_TDEL)) {
2592                 printk(KERN_WARNING "mbox: unexpected TDEL from DSP!\n");
2593                 return;
2594         }
2595         cfg_tid = tid;
2596         wake_up_interruptible(&cfg_wait_q);
2597 }
2598
2599 void mbox_err_fatal(u8 tid)
2600 {
2601         struct dsptask *task = dsptask[tid];
2602         struct taskdev *dev;
2603
2604         if ((tid >= TASKDEV_MAX) || (task == NULL)) {
2605                 printk(KERN_ERR "mbox: FATAL ERR with illegal tid! %d\n", tid);
2606                 return;
2607         }
2608
2609         /* wake up waiting processes */
2610         dev = task->dev;
2611         wake_up_interruptible_all(&dev->read_wait_q);
2612         wake_up_interruptible_all(&dev->write_wait_q);
2613         wake_up_interruptible_all(&dev->tctl_wait_q);
2614 }
2615
2616 static u16 *dbg_buf;
2617 static u16 dbg_buf_sz, dbg_line_sz;
2618 static int dbg_rp;
2619
2620 int dsp_dbg_config(u16 *buf, u16 sz, u16 lsz)
2621 {
2622 #ifdef OLD_BINARY_SUPPORT
2623         if ((mbox_revision == MBREV_3_0) || (mbox_revision == MBREV_3_2)) {
2624                 dbg_buf = NULL;
2625                 dbg_buf_sz = 0;
2626                 dbg_line_sz = 0;
2627                 dbg_rp = 0;
2628                 return 0;
2629         }
2630 #endif
2631
2632         if (dsp_address_validate(buf, sz, "debug buffer") < 0)
2633                 return -1;
2634
2635         if (lsz > sz) {
2636                 printk(KERN_ERR
2637                        "omapdsp: dbg_buf lsz (%d) is greater than its "
2638                        "buffer size (%d)\n", lsz, sz);
2639                 return -1;
2640         }
2641
2642         dbg_buf = buf;
2643         dbg_buf_sz = sz;
2644         dbg_line_sz = lsz;
2645         dbg_rp = 0;
2646
2647         return 0;
2648 }
2649
2650 void dsp_dbg_stop(void)
2651 {
2652         dbg_buf = NULL;
2653 }
2654
2655 #ifdef OLD_BINARY_SUPPORT
2656 static void mbox_dbg_old(struct mbcmd *mb);
2657 #endif
2658
2659 void mbox_dbg(struct mbcmd *mb)
2660 {
2661         u8 tid = mb->cmd_l;
2662         int cnt = mb->data;
2663         char s[80], *s_end = &s[79], *p;
2664         u16 *src;
2665         int i;
2666
2667 #ifdef OLD_BINARY_SUPPORT
2668         if ((mbox_revision == MBREV_3_0) || (mbox_revision == MBREV_3_2)) {
2669                 mbox_dbg_old(mb);
2670                 return;
2671         }
2672 #endif
2673
2674         if (((tid >= TASKDEV_MAX) || (dsptask[tid] == NULL)) &&
2675             (tid != TID_ANON)) {
2676                 printk(KERN_ERR "mbox: DBG with illegal tid! %d\n", tid);
2677                 return;
2678         }
2679         if (dbg_buf == NULL) {
2680                 printk(KERN_ERR "mbox: DBG command received, but "
2681                        "dbg_buf has not been configured yet.\n");
2682                 return;
2683         }
2684
2685         if (dsp_mem_enable(dbg_buf) < 0)
2686                 return;
2687
2688         src = &dbg_buf[dbg_rp];
2689         p = s;
2690         for (i = 0; i < cnt; i++) {
2691                 u16 tmp;
2692                 /*
2693                  * Be carefull that dbg_buf should not be read with
2694                  * 1-byte access since it might be placed in DARAM/SARAM
2695                  * and it can cause unexpected byteswap.
2696                  * For example,
2697                  *   *(p++) = *(src++) & 0xff;
2698                  * causes 1-byte access!
2699                  */
2700                 tmp = *src++;
2701                 *(p++) = tmp & 0xff;
2702                 if (*(p-1) == '\n') {
2703                         *p = '\0';
2704                         pr_info("%s", s);
2705                         p = s;
2706                         continue;
2707                 }
2708                 if (p == s_end) {
2709                         *p = '\0';
2710                         pr_info("%s\n", s);
2711                         p = s;
2712                         continue;
2713                 }
2714         }
2715         if (p > s) {
2716                 *p = '\0';
2717                 pr_info("%s\n", s);
2718         }
2719         if ((dbg_rp += cnt + 1) > dbg_buf_sz - dbg_line_sz)
2720                 dbg_rp = 0;
2721
2722         dsp_mem_disable(dbg_buf);
2723 }
2724
2725 #ifdef OLD_BINARY_SUPPORT
2726 static void mbox_dbg_old(struct mbcmd *mb)
2727 {
2728         u8 tid = mb->cmd_l;
2729         char s[80], *s_end = &s[79], *p;
2730         u16 *src;
2731         volatile u16 *buf;
2732         int cnt;
2733         int i;
2734
2735         if (((tid >= TASKDEV_MAX) || (dsptask[tid] == NULL)) &&
2736             (tid != TID_ANON)) {
2737                 printk(KERN_ERR "mbox: DBG with illegal tid! %d\n", tid);
2738                 return;
2739         }
2740         if (dsp_mem_enable(ipbuf_sys_da) < 0) {
2741                 printk(KERN_ERR "mbox: DBG - ipbuf_sys_da read failed!\n");
2742                 return;
2743         }
2744         if (sync_with_dsp(&ipbuf_sys_da->s, tid, 10) < 0) {
2745                 printk(KERN_ERR "mbox: DBG - IPBUF sync failed!\n");
2746                 goto out1;
2747         }
2748         buf = ipbuf_sys_da->d;
2749         cnt = buf[0];
2750         src = MKVIRT(buf[1], buf[2]);
2751         if (dsp_address_validate(src, cnt, "dbg buffer") < 0)
2752                 goto out2;
2753
2754         if (dsp_mem_enable(src) < 0)
2755                 goto out2;
2756
2757         p = s;
2758         for (i = 0; i < cnt; i++) {
2759                 u16 tmp;
2760                 /*
2761                  * Be carefull that ipbuf should not be read with
2762                  * 1-byte access since it might be placed in DARAM/SARAM
2763                  * and it can cause unexpected byteswap.
2764                  * For example,
2765                  *   *(p++) = *(src++) & 0xff;
2766                  * causes 1-byte access!
2767                  */
2768                 tmp = *src++;
2769                 *(p++) = tmp & 0xff;
2770                 if (*(p-1) == '\n') {
2771                         *p = '\0';
2772                         pr_info("%s", s);
2773                         p = s;
2774                         continue;
2775                 }
2776                 if (p == s_end) {
2777                         *p = '\0';
2778                         pr_info("%s\n", s);
2779                         p = s;
2780                         continue;
2781                 }
2782         }
2783         if (p > s) {
2784                 *p = '\0';
2785                 pr_info("%s\n", s);
2786         }
2787
2788         dsp_mem_disable(src);
2789 out2:
2790         release_ipbuf_pvt(ipbuf_sys_da);
2791 out1:
2792         dsp_mem_disable(ipbuf_sys_da);
2793 }
2794 #endif /* OLD_BINARY_SUPPORT */
2795
2796 /*
2797  * sysfs files: for each device
2798  */
2799
2800 /* devname */
2801 static ssize_t devname_show(struct device *d, struct device_attribute *attr,
2802                             char *buf)
2803 {
2804         return sprintf(buf, "%s\n", to_taskdev(d)->name);
2805 }
2806
2807 /* devstate */
2808 static ssize_t devstate_show(struct device *d, struct device_attribute *attr,
2809                              char *buf)
2810 {
2811         return sprintf(buf, "%s\n", devstate_name(to_taskdev(d)->state));
2812 }
2813
2814 /* proc_list */
2815 static ssize_t proc_list_show(struct device *d, struct device_attribute *attr,
2816                               char *buf)
2817 {
2818         struct taskdev *dev;
2819         struct proc_list *pl;
2820         int len = 0;
2821
2822         dev = to_taskdev(d);
2823         spin_lock(&dev->proc_list_lock);
2824         list_for_each_entry(pl, &dev->proc_list, list_head) {
2825                 /* need to lock tasklist_lock before calling
2826                  * find_task_by_pid_type. */
2827                 if (find_task_by_pid_type(PIDTYPE_PID, pl->pid) != NULL)
2828                         len += sprintf(buf + len, "%d\n", pl->pid);
2829                 read_unlock(&tasklist_lock);
2830         }
2831         spin_unlock(&dev->proc_list_lock);
2832
2833         return len;
2834 }
2835
2836 /* taskname */
2837 static ssize_t taskname_show(struct device *d, struct device_attribute *attr,
2838                              char *buf)
2839 {
2840         struct taskdev *dev = to_taskdev(d);
2841         int len;
2842
2843         if (!devstate_read_lock_and_test(dev, TASKDEV_ST_ATTACHED))
2844                 return -ENODEV;
2845
2846         len = sprintf(buf, "%s\n", dev->task->name);
2847
2848         devstate_read_unlock(dev);
2849         return len;
2850 }
2851
2852 /* ttyp */
2853 static ssize_t ttyp_show(struct device *d, struct device_attribute *attr,
2854                          char *buf)
2855 {
2856         struct taskdev *dev = to_taskdev(d);
2857         u16 ttyp;
2858         int len = 0;
2859
2860         if (!devstate_read_lock_and_test(dev, TASKDEV_ST_ATTACHED))
2861                 return -ENODEV;
2862
2863         ttyp = dev->task->ttyp;
2864         len += sprintf(buf + len, "0x%04x\n", ttyp);
2865         len += sprintf(buf + len, "%s %s send\n",
2866                         (sndtyp_acv(ttyp)) ? "active" :
2867                                              "passive",
2868                         (sndtyp_wd(ttyp))  ? "word" :
2869                         (sndtyp_pvt(ttyp)) ? "private block" :
2870                                              "global block");
2871         len += sprintf(buf + len, "%s %s receive\n",
2872                         (rcvtyp_acv(ttyp)) ? "active" :
2873                                              "passive",
2874                         (rcvtyp_wd(ttyp))  ? "word" :
2875                         (rcvtyp_pvt(ttyp)) ? "private block" :
2876                                              "global block");
2877
2878         devstate_read_unlock(dev);
2879         return len;
2880 }
2881
2882 /* fifosz */
2883 static ssize_t fifosz_show(struct device *d, struct device_attribute *attr,
2884                            char *buf)
2885 {
2886         struct fifo_struct *fifo = &to_taskdev(d)->rcvdt.fifo;
2887         return sprintf(buf, "%d\n", fifo->sz);
2888 }
2889
2890 static int fifosz_store(struct device *d, struct device_attribute *attr,
2891                         const char *buf, size_t count)
2892 {
2893         struct taskdev *dev = to_taskdev(d);
2894         unsigned long fifosz;
2895         int ret;
2896
2897         fifosz = simple_strtol(buf, NULL, 10);
2898         ret = taskdev_set_fifosz(dev, fifosz);
2899
2900         return (ret < 0) ? ret : strlen(buf);
2901 }
2902
2903 /* fifocnt */
2904 static ssize_t fifocnt_show(struct device *d, struct device_attribute *attr,
2905                             char *buf)
2906 {
2907         struct fifo_struct *fifo = &to_taskdev(d)->rcvdt.fifo;
2908         return sprintf(buf, "%d\n", fifo->cnt);
2909 }
2910
2911 /* ipblink */
2912 static inline char *bid_name(u16 bid)
2913 {
2914         static char s[6];
2915
2916         switch (bid) {
2917         case BID_NULL:
2918                 return "NULL";
2919         case BID_PVT:
2920                 return "PRIVATE";
2921         default:
2922                 sprintf(s, "%d", bid);
2923                 return s;
2924         }
2925 }
2926
2927 static ssize_t ipblink_show(struct device *d, struct device_attribute *attr,
2928                             char *buf)
2929 {
2930         struct rcvdt_bk_struct *rcvdt = &to_taskdev(d)->rcvdt.bk;
2931         int len;
2932
2933         spin_lock(&rcvdt->link.lock);
2934         len = sprintf(buf, "top  %s\ntail %s\n",
2935                       bid_name(rcvdt->link.top), bid_name(rcvdt->link.tail));
2936         spin_unlock(&rcvdt->link.lock);
2937
2938         return len;
2939 }
2940
2941 /* wsz */
2942 static ssize_t wsz_show(struct device *d, struct device_attribute *attr,
2943                         char *buf)
2944 {
2945         return sprintf(buf, "%d\n", to_taskdev(d)->wsz);
2946 }
2947
2948 /* mmap */
2949 static ssize_t mmap_show(struct device *d, struct device_attribute *attr,
2950                          char *buf)
2951 {
2952         struct dsptask *task = to_taskdev(d)->task;
2953         return sprintf(buf, "0x%p 0x%x\n", task->map_base, task->map_length);
2954 }
2955
2956 /*
2957  * called from ipbuf_show()
2958  */
2959 int ipbuf_is_held(u8 tid, u16 bid)
2960 {
2961         struct dsptask *task = dsptask[tid];
2962         struct ipblink *link;
2963         u16 b;
2964         int ret = 0;
2965
2966         if (task == NULL)
2967                 return 0;
2968
2969         link = &task->dev->rcvdt.bk.link;
2970         spin_lock(&link->lock);
2971         ipblink_for_each(b, link) {
2972                 if (b == bid) { /* found */
2973                         ret = 1;
2974                         break;
2975                 }
2976         }
2977         spin_unlock(&link->lock);
2978
2979         return ret;
2980 }
2981
2982 int __init dsp_taskmod_init(void)
2983 {
2984         int retval;
2985
2986         memset(taskdev, 0, sizeof(void *) * TASKDEV_MAX);
2987         memset(dsptask, 0, sizeof(void *) * TASKDEV_MAX);
2988
2989         retval = register_chrdev(OMAP_DSP_TASK_MAJOR, "dsptask",
2990                                  &dsp_task_fops);
2991         if (retval < 0) {
2992                 printk(KERN_ERR
2993                        "omapdsp: failed to register task device: %d\n", retval);
2994                 return retval;
2995         }
2996
2997         retval = bus_register(&dsptask_bus);
2998         if (retval) {
2999                 printk(KERN_ERR
3000                        "omapdsp: failed to register DSP task bus: %d\n",
3001                        retval);
3002                 unregister_chrdev(OMAP_DSP_TASK_MAJOR, "dsptask");
3003                 return -EINVAL;
3004         }
3005         retval = driver_register(&dsptask_driver);
3006         if (retval) {
3007                 printk(KERN_ERR
3008                        "omapdsp: failed to register DSP task driver: %d\n",
3009                        retval);
3010                 bus_unregister(&dsptask_bus);
3011                 unregister_chrdev(OMAP_DSP_TASK_MAJOR, "dsptask");
3012                 return -EINVAL;
3013         }
3014         dsp_task_class = class_create(THIS_MODULE, "dsptask");
3015         if (IS_ERR(dsp_task_class)) {
3016                 printk(KERN_ERR "omapdsp: failed to create DSP task class\n");
3017                 driver_unregister(&dsptask_driver);
3018                 bus_unregister(&dsptask_bus);
3019                 unregister_chrdev(OMAP_DSP_TASK_MAJOR, "dsptask");
3020                 return -EINVAL;
3021         }
3022
3023         return 0;
3024 }
3025
3026 void dsp_taskmod_exit(void)
3027 {
3028         class_destroy(dsp_task_class);
3029         driver_unregister(&dsptask_driver);
3030         bus_unregister(&dsptask_bus);
3031         unregister_chrdev(OMAP_DSP_TASK_MAJOR, "dsptask");
3032 }