Mailbox: Check valid registered callback before calling
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / arm / plat-omap / mailbox.c
blob72efbe5be5781233c2a98ba636f0f1fb26199fc0
1 /*
2 * OMAP mailbox driver
4 * Copyright (C) 2006-2009 Nokia Corporation. All rights reserved.
6 * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
24 #include <linux/module.h>
25 #include <linux/interrupt.h>
26 #include <linux/device.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
30 #include <plat/mailbox.h>
32 static struct workqueue_struct *mboxd;
33 static struct omap_mbox *mboxes;
34 static DEFINE_RWLOCK(mboxes_lock);
36 static int mbox_configured;
38 /* Mailbox FIFO handle functions */
39 static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
41 return mbox->ops->fifo_read(mbox);
43 static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
45 mbox->ops->fifo_write(mbox, msg);
47 static inline int mbox_fifo_empty(struct omap_mbox *mbox)
49 return mbox->ops->fifo_empty(mbox);
51 static inline int mbox_fifo_full(struct omap_mbox *mbox)
53 return mbox->ops->fifo_full(mbox);
56 /* Mailbox IRQ handle functions */
57 static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
59 if (mbox->ops->ack_irq)
60 mbox->ops->ack_irq(mbox, irq);
62 static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
64 return mbox->ops->is_irq(mbox, irq);
68 * message sender
70 static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
72 int ret = 0, i = 1000;
74 while (mbox_fifo_full(mbox)) {
75 if (mbox->ops->type == OMAP_MBOX_TYPE2)
76 return -1;
77 if (--i == 0)
78 return -1;
79 udelay(1);
81 mbox_fifo_write(mbox, msg);
82 return ret;
86 int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
89 struct request *rq;
90 struct request_queue *q = mbox->txq->queue;
92 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
93 if (unlikely(!rq))
94 return -ENOMEM;
96 blk_insert_request(q, rq, 0, (void *) msg);
97 tasklet_schedule(&mbox->txq->tasklet);
99 return 0;
101 EXPORT_SYMBOL(omap_mbox_msg_send);
103 static void mbox_tx_tasklet(unsigned long tx_data)
105 int ret;
106 struct request *rq;
107 struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
108 struct request_queue *q = mbox->txq->queue;
110 while (1) {
112 rq = blk_fetch_request(q);
114 if (!rq)
115 break;
117 ret = __mbox_msg_send(mbox, (mbox_msg_t)rq->special);
118 if (ret) {
119 omap_mbox_enable_irq(mbox, IRQ_TX);
120 blk_requeue_request(q, rq);
121 return;
123 blk_end_request_all(rq, 0);
128 * Message receiver(workqueue)
130 static void mbox_rx_work(struct work_struct *work)
132 struct omap_mbox_queue *mq =
133 container_of(work, struct omap_mbox_queue, work);
134 struct omap_mbox *mbox = mq->queue->queuedata;
135 struct request_queue *q = mbox->rxq->queue;
136 struct request *rq;
137 mbox_msg_t msg;
138 unsigned long flags;
140 while (1) {
141 spin_lock_irqsave(q->queue_lock, flags);
142 rq = blk_fetch_request(q);
143 spin_unlock_irqrestore(q->queue_lock, flags);
144 if (!rq)
145 break;
147 msg = (mbox_msg_t)rq->special;
148 blk_end_request_all(rq, 0);
149 if (mbox->rxq->callback)
150 mbox->rxq->callback((void *)msg);
155 * Mailbox interrupt handler
157 static void mbox_txq_fn(struct request_queue *q)
161 static void mbox_rxq_fn(struct request_queue *q)
165 static void __mbox_tx_interrupt(struct omap_mbox *mbox)
167 omap_mbox_disable_irq(mbox, IRQ_TX);
168 ack_mbox_irq(mbox, IRQ_TX);
169 tasklet_schedule(&mbox->txq->tasklet);
172 static void __mbox_rx_interrupt(struct omap_mbox *mbox)
174 struct request *rq;
175 mbox_msg_t msg;
176 struct request_queue *q = mbox->rxq->queue;
178 while (!mbox_fifo_empty(mbox)) {
179 rq = blk_get_request(q, WRITE, GFP_ATOMIC);
180 if (unlikely(!rq))
181 goto nomem;
183 msg = mbox_fifo_read(mbox);
186 blk_insert_request(q, rq, 0, (void *)msg);
187 if (mbox->ops->type == OMAP_MBOX_TYPE1)
188 break;
191 /* no more messages in the fifo. clear IRQ source. */
192 ack_mbox_irq(mbox, IRQ_RX);
193 nomem:
194 queue_work(mboxd, &mbox->rxq->work);
197 static irqreturn_t mbox_interrupt(int irq, void *p)
199 struct omap_mbox *mbox = p;
201 if (is_mbox_irq(mbox, IRQ_TX))
202 __mbox_tx_interrupt(mbox);
204 if (is_mbox_irq(mbox, IRQ_RX))
205 __mbox_rx_interrupt(mbox);
207 return IRQ_HANDLED;
210 static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
211 request_fn_proc *proc,
212 void (*work) (struct work_struct *),
213 void (*tasklet)(unsigned long))
215 struct request_queue *q;
216 struct omap_mbox_queue *mq;
218 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
219 if (!mq)
220 return NULL;
222 spin_lock_init(&mq->lock);
224 q = blk_init_queue(proc, &mq->lock);
225 if (!q)
226 goto error;
227 q->queuedata = mbox;
228 mq->queue = q;
230 if (work)
231 INIT_WORK(&mq->work, work);
233 if (tasklet)
234 tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
235 return mq;
236 error:
237 kfree(mq);
238 return NULL;
241 static void mbox_queue_free(struct omap_mbox_queue *q)
243 blk_cleanup_queue(q->queue);
244 kfree(q);
247 static int omap_mbox_startup(struct omap_mbox *mbox)
249 int ret = 0;
250 struct omap_mbox_queue *mq;
252 if (likely(mbox->ops->startup)) {
253 write_lock(&mboxes_lock);
254 if (!mbox_configured)
255 ret = mbox->ops->startup(mbox);
257 if (unlikely(ret)) {
258 write_unlock(&mboxes_lock);
259 return ret;
261 mbox_configured++;
262 write_unlock(&mboxes_lock);
265 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
266 mbox->name, mbox);
267 if (unlikely(ret)) {
268 printk(KERN_ERR
269 "failed to register mailbox interrupt:%d\n", ret);
270 goto fail_request_irq;
273 mq = mbox_queue_alloc(mbox, mbox_txq_fn, NULL, mbox_tx_tasklet);
274 if (!mq) {
275 ret = -ENOMEM;
276 goto fail_alloc_txq;
278 mbox->txq = mq;
280 mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work, NULL);
281 if (!mq) {
282 ret = -ENOMEM;
283 goto fail_alloc_rxq;
285 mbox->rxq = mq;
287 return 0;
289 fail_alloc_rxq:
290 mbox_queue_free(mbox->txq);
291 fail_alloc_txq:
292 free_irq(mbox->irq, mbox);
293 fail_request_irq:
294 if (unlikely(mbox->ops->shutdown))
295 mbox->ops->shutdown(mbox);
297 return ret;
300 static void omap_mbox_fini(struct omap_mbox *mbox)
302 free_irq(mbox->irq, mbox);
303 tasklet_kill(&mbox->txq->tasklet);
304 flush_work(&mbox->rxq->work);
305 mbox_queue_free(mbox->txq);
306 mbox_queue_free(mbox->rxq);
308 if (unlikely(mbox->ops->shutdown)) {
309 write_lock(&mboxes_lock);
310 if (mbox_configured > 0)
311 mbox_configured--;
312 if (!mbox_configured)
313 mbox->ops->shutdown(mbox);
314 write_unlock(&mboxes_lock);
318 static struct omap_mbox **find_mboxes(const char *name)
320 struct omap_mbox **p;
322 for (p = &mboxes; *p; p = &(*p)->next) {
323 if (strcmp((*p)->name, name) == 0)
324 break;
327 return p;
330 struct omap_mbox *omap_mbox_get(const char *name)
332 struct omap_mbox *mbox;
333 int ret;
335 read_lock(&mboxes_lock);
336 mbox = *(find_mboxes(name));
337 if (mbox == NULL) {
338 read_unlock(&mboxes_lock);
339 return ERR_PTR(-ENOENT);
342 read_unlock(&mboxes_lock);
344 ret = omap_mbox_startup(mbox);
345 if (ret)
346 return ERR_PTR(-ENODEV);
348 return mbox;
350 EXPORT_SYMBOL(omap_mbox_get);
352 void omap_mbox_put(struct omap_mbox *mbox)
354 omap_mbox_fini(mbox);
356 EXPORT_SYMBOL(omap_mbox_put);
358 int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
360 int ret = 0;
361 struct omap_mbox **tmp;
363 if (!mbox)
364 return -EINVAL;
365 if (mbox->next)
366 return -EBUSY;
368 write_lock(&mboxes_lock);
369 tmp = find_mboxes(mbox->name);
370 if (*tmp) {
371 ret = -EBUSY;
372 write_unlock(&mboxes_lock);
373 goto err_find;
375 *tmp = mbox;
376 write_unlock(&mboxes_lock);
378 return 0;
380 err_find:
381 return ret;
383 EXPORT_SYMBOL(omap_mbox_register);
385 int omap_mbox_unregister(struct omap_mbox *mbox)
387 struct omap_mbox **tmp;
389 write_lock(&mboxes_lock);
390 tmp = &mboxes;
391 while (*tmp) {
392 if (mbox == *tmp) {
393 *tmp = mbox->next;
394 mbox->next = NULL;
395 write_unlock(&mboxes_lock);
396 return 0;
398 tmp = &(*tmp)->next;
400 write_unlock(&mboxes_lock);
402 return -EINVAL;
404 EXPORT_SYMBOL(omap_mbox_unregister);
406 static int __init omap_mbox_init(void)
408 mboxd = create_workqueue("mboxd");
409 if (!mboxd)
410 return -ENOMEM;
412 return 0;
414 module_init(omap_mbox_init);
416 static void __exit omap_mbox_exit(void)
418 destroy_workqueue(mboxd);
420 module_exit(omap_mbox_exit);
422 MODULE_LICENSE("GPL v2");
423 MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
424 MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");