Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * OMAP mailbox driver |
| 3 | * |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 4 | * Copyright (C) 2006-2009 Nokia Corporation. All rights reserved. |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 5 | * |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 6 | * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com> |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 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 | |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 24 | #include <linux/module.h> |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/device.h> |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 27 | #include <linux/delay.h> |
Hiroshi DOYU | 8dff0fa | 2009-03-23 18:07:32 -0700 | [diff] [blame] | 28 | |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 29 | #include <plat/mailbox.h> |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 30 | |
| 31 | static struct omap_mbox *mboxes; |
| 32 | static DEFINE_RWLOCK(mboxes_lock); |
| 33 | |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 34 | /* Mailbox FIFO handle functions */ |
| 35 | static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox) |
| 36 | { |
| 37 | return mbox->ops->fifo_read(mbox); |
| 38 | } |
| 39 | static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg) |
| 40 | { |
| 41 | mbox->ops->fifo_write(mbox, msg); |
| 42 | } |
| 43 | static inline int mbox_fifo_empty(struct omap_mbox *mbox) |
| 44 | { |
| 45 | return mbox->ops->fifo_empty(mbox); |
| 46 | } |
| 47 | static inline int mbox_fifo_full(struct omap_mbox *mbox) |
| 48 | { |
| 49 | return mbox->ops->fifo_full(mbox); |
| 50 | } |
| 51 | |
| 52 | /* Mailbox IRQ handle functions */ |
| 53 | static inline void enable_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq) |
| 54 | { |
| 55 | mbox->ops->enable_irq(mbox, irq); |
| 56 | } |
| 57 | static inline void disable_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq) |
| 58 | { |
| 59 | mbox->ops->disable_irq(mbox, irq); |
| 60 | } |
| 61 | static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq) |
| 62 | { |
| 63 | if (mbox->ops->ack_irq) |
| 64 | mbox->ops->ack_irq(mbox, irq); |
| 65 | } |
| 66 | static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq) |
| 67 | { |
| 68 | return mbox->ops->is_irq(mbox, irq); |
| 69 | } |
| 70 | |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 71 | /* |
| 72 | * message sender |
| 73 | */ |
| 74 | static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void *arg) |
| 75 | { |
| 76 | int ret = 0, i = 1000; |
| 77 | |
| 78 | while (mbox_fifo_full(mbox)) { |
| 79 | if (mbox->ops->type == OMAP_MBOX_TYPE2) |
| 80 | return -1; |
| 81 | if (--i == 0) |
| 82 | return -1; |
| 83 | udelay(1); |
| 84 | } |
| 85 | |
| 86 | if (arg && mbox->txq->callback) { |
| 87 | ret = mbox->txq->callback(arg); |
| 88 | if (ret) |
| 89 | goto out; |
| 90 | } |
| 91 | |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 92 | mbox_fifo_write(mbox, msg); |
| 93 | out: |
| 94 | return ret; |
| 95 | } |
| 96 | |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 97 | struct omap_msg_tx_data { |
| 98 | mbox_msg_t msg; |
| 99 | void *arg; |
| 100 | }; |
| 101 | |
| 102 | static void omap_msg_tx_end_io(struct request *rq, int error) |
| 103 | { |
| 104 | kfree(rq->special); |
| 105 | __blk_put_request(rq->q, rq); |
| 106 | } |
| 107 | |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 108 | int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void* arg) |
| 109 | { |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 110 | struct omap_msg_tx_data *tx_data; |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 111 | struct request *rq; |
| 112 | struct request_queue *q = mbox->txq->queue; |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 113 | |
| 114 | tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC); |
| 115 | if (unlikely(!tx_data)) |
| 116 | return -ENOMEM; |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 117 | |
| 118 | rq = blk_get_request(q, WRITE, GFP_ATOMIC); |
| 119 | if (unlikely(!rq)) { |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 120 | kfree(tx_data); |
| 121 | return -ENOMEM; |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 124 | tx_data->msg = msg; |
| 125 | tx_data->arg = arg; |
| 126 | rq->end_io = omap_msg_tx_end_io; |
| 127 | blk_insert_request(q, rq, 0, tx_data); |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 128 | |
| 129 | schedule_work(&mbox->txq->work); |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 130 | return 0; |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 131 | } |
| 132 | EXPORT_SYMBOL(omap_mbox_msg_send); |
| 133 | |
| 134 | static void mbox_tx_work(struct work_struct *work) |
| 135 | { |
| 136 | int ret; |
| 137 | struct request *rq; |
| 138 | struct omap_mbox_queue *mq = container_of(work, |
| 139 | struct omap_mbox_queue, work); |
| 140 | struct omap_mbox *mbox = mq->queue->queuedata; |
| 141 | struct request_queue *q = mbox->txq->queue; |
| 142 | |
| 143 | while (1) { |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 144 | struct omap_msg_tx_data *tx_data; |
| 145 | |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 146 | spin_lock(q->queue_lock); |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 147 | rq = blk_fetch_request(q); |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 148 | spin_unlock(q->queue_lock); |
| 149 | |
| 150 | if (!rq) |
| 151 | break; |
| 152 | |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 153 | tx_data = rq->special; |
| 154 | |
| 155 | ret = __mbox_msg_send(mbox, tx_data->msg, tx_data->arg); |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 156 | if (ret) { |
| 157 | enable_mbox_irq(mbox, IRQ_TX); |
Tejun Heo | 296b2f6 | 2009-05-08 11:54:15 +0900 | [diff] [blame] | 158 | spin_lock(q->queue_lock); |
| 159 | blk_requeue_request(q, rq); |
| 160 | spin_unlock(q->queue_lock); |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 161 | return; |
| 162 | } |
| 163 | |
| 164 | spin_lock(q->queue_lock); |
Tejun Heo | 40cbbb7 | 2009-04-23 11:05:19 +0900 | [diff] [blame] | 165 | __blk_end_request_all(rq, 0); |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 166 | spin_unlock(q->queue_lock); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Message receiver(workqueue) |
| 172 | */ |
| 173 | static void mbox_rx_work(struct work_struct *work) |
| 174 | { |
| 175 | struct omap_mbox_queue *mq = |
| 176 | container_of(work, struct omap_mbox_queue, work); |
| 177 | struct omap_mbox *mbox = mq->queue->queuedata; |
| 178 | struct request_queue *q = mbox->rxq->queue; |
| 179 | struct request *rq; |
| 180 | mbox_msg_t msg; |
| 181 | unsigned long flags; |
| 182 | |
| 183 | if (mbox->rxq->callback == NULL) { |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 184 | sysfs_notify(&mbox->dev->kobj, NULL, "mbox"); |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | |
| 188 | while (1) { |
| 189 | spin_lock_irqsave(q->queue_lock, flags); |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 190 | rq = blk_fetch_request(q); |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 191 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 192 | if (!rq) |
| 193 | break; |
| 194 | |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 195 | msg = (mbox_msg_t)rq->special; |
Tejun Heo | 40cbbb7 | 2009-04-23 11:05:19 +0900 | [diff] [blame] | 196 | blk_end_request_all(rq, 0); |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 197 | mbox->rxq->callback((void *)msg); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Mailbox interrupt handler |
| 203 | */ |
Hiroshi DOYU | bfe1f6a | 2009-11-22 10:11:18 -0800 | [diff] [blame^] | 204 | static void mbox_txq_fn(struct request_queue *q) |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 205 | { |
| 206 | } |
| 207 | |
Hiroshi DOYU | bfe1f6a | 2009-11-22 10:11:18 -0800 | [diff] [blame^] | 208 | static void mbox_rxq_fn(struct request_queue *q) |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 209 | { |
| 210 | } |
| 211 | |
| 212 | static void __mbox_tx_interrupt(struct omap_mbox *mbox) |
| 213 | { |
| 214 | disable_mbox_irq(mbox, IRQ_TX); |
| 215 | ack_mbox_irq(mbox, IRQ_TX); |
| 216 | schedule_work(&mbox->txq->work); |
| 217 | } |
| 218 | |
| 219 | static void __mbox_rx_interrupt(struct omap_mbox *mbox) |
| 220 | { |
| 221 | struct request *rq; |
| 222 | mbox_msg_t msg; |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 223 | struct request_queue *q = mbox->rxq->queue; |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 224 | |
| 225 | disable_mbox_irq(mbox, IRQ_RX); |
| 226 | |
| 227 | while (!mbox_fifo_empty(mbox)) { |
| 228 | rq = blk_get_request(q, WRITE, GFP_ATOMIC); |
| 229 | if (unlikely(!rq)) |
| 230 | goto nomem; |
| 231 | |
| 232 | msg = mbox_fifo_read(mbox); |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 233 | |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 234 | |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 235 | blk_insert_request(q, rq, 0, (void *)msg); |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 236 | if (mbox->ops->type == OMAP_MBOX_TYPE1) |
| 237 | break; |
| 238 | } |
| 239 | |
| 240 | /* no more messages in the fifo. clear IRQ source. */ |
| 241 | ack_mbox_irq(mbox, IRQ_RX); |
| 242 | enable_mbox_irq(mbox, IRQ_RX); |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 243 | nomem: |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 244 | schedule_work(&mbox->rxq->work); |
| 245 | } |
| 246 | |
| 247 | static irqreturn_t mbox_interrupt(int irq, void *p) |
| 248 | { |
Jeff Garzik | 2a7057e | 2007-10-26 05:40:22 -0400 | [diff] [blame] | 249 | struct omap_mbox *mbox = p; |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 250 | |
| 251 | if (is_mbox_irq(mbox, IRQ_TX)) |
| 252 | __mbox_tx_interrupt(mbox); |
| 253 | |
| 254 | if (is_mbox_irq(mbox, IRQ_RX)) |
| 255 | __mbox_rx_interrupt(mbox); |
| 256 | |
| 257 | return IRQ_HANDLED; |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * sysfs files |
| 262 | */ |
| 263 | static ssize_t |
| 264 | omap_mbox_write(struct device *dev, struct device_attribute *attr, |
Hiroshi DOYU | bfe1f6a | 2009-11-22 10:11:18 -0800 | [diff] [blame^] | 265 | const char *buf, size_t count) |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 266 | { |
| 267 | int ret; |
| 268 | mbox_msg_t *p = (mbox_msg_t *)buf; |
| 269 | struct omap_mbox *mbox = dev_get_drvdata(dev); |
| 270 | |
| 271 | for (; count >= sizeof(mbox_msg_t); count -= sizeof(mbox_msg_t)) { |
| 272 | ret = omap_mbox_msg_send(mbox, be32_to_cpu(*p), NULL); |
| 273 | if (ret) |
| 274 | return -EAGAIN; |
| 275 | p++; |
| 276 | } |
| 277 | |
| 278 | return (size_t)((char *)p - buf); |
| 279 | } |
| 280 | |
| 281 | static ssize_t |
| 282 | omap_mbox_read(struct device *dev, struct device_attribute *attr, char *buf) |
| 283 | { |
| 284 | unsigned long flags; |
| 285 | struct request *rq; |
| 286 | mbox_msg_t *p = (mbox_msg_t *) buf; |
| 287 | struct omap_mbox *mbox = dev_get_drvdata(dev); |
| 288 | struct request_queue *q = mbox->rxq->queue; |
| 289 | |
| 290 | while (1) { |
| 291 | spin_lock_irqsave(q->queue_lock, flags); |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 292 | rq = blk_fetch_request(q); |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 293 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 294 | |
| 295 | if (!rq) |
| 296 | break; |
| 297 | |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 298 | *p = (mbox_msg_t)rq->special; |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 299 | |
Tejun Heo | 40cbbb7 | 2009-04-23 11:05:19 +0900 | [diff] [blame] | 300 | blk_end_request_all(rq, 0); |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 301 | |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 302 | p++; |
| 303 | } |
| 304 | |
| 305 | pr_debug("%02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3]); |
| 306 | |
| 307 | return (size_t) ((char *)p - buf); |
| 308 | } |
| 309 | |
| 310 | static DEVICE_ATTR(mbox, S_IRUGO | S_IWUSR, omap_mbox_read, omap_mbox_write); |
| 311 | |
| 312 | static ssize_t mbox_show(struct class *class, char *buf) |
| 313 | { |
| 314 | return sprintf(buf, "mbox"); |
| 315 | } |
| 316 | |
| 317 | static CLASS_ATTR(mbox, S_IRUGO, mbox_show, NULL); |
| 318 | |
| 319 | static struct class omap_mbox_class = { |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 320 | .name = "omap-mailbox", |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 321 | }; |
| 322 | |
| 323 | static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox, |
Hiroshi DOYU | bfe1f6a | 2009-11-22 10:11:18 -0800 | [diff] [blame^] | 324 | request_fn_proc *proc, |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 325 | void (*work) (struct work_struct *)) |
| 326 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 327 | struct request_queue *q; |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 328 | struct omap_mbox_queue *mq; |
| 329 | |
| 330 | mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL); |
| 331 | if (!mq) |
| 332 | return NULL; |
| 333 | |
| 334 | spin_lock_init(&mq->lock); |
| 335 | |
| 336 | q = blk_init_queue(proc, &mq->lock); |
| 337 | if (!q) |
| 338 | goto error; |
| 339 | q->queuedata = mbox; |
| 340 | mq->queue = q; |
| 341 | |
| 342 | INIT_WORK(&mq->work, work); |
| 343 | |
| 344 | return mq; |
| 345 | error: |
| 346 | kfree(mq); |
| 347 | return NULL; |
| 348 | } |
| 349 | |
| 350 | static void mbox_queue_free(struct omap_mbox_queue *q) |
| 351 | { |
| 352 | blk_cleanup_queue(q->queue); |
| 353 | kfree(q); |
| 354 | } |
| 355 | |
| 356 | static int omap_mbox_init(struct omap_mbox *mbox) |
| 357 | { |
| 358 | int ret; |
| 359 | struct omap_mbox_queue *mq; |
| 360 | |
| 361 | if (likely(mbox->ops->startup)) { |
| 362 | ret = mbox->ops->startup(mbox); |
| 363 | if (unlikely(ret)) |
| 364 | return ret; |
| 365 | } |
| 366 | |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 367 | ret = request_irq(mbox->irq, mbox_interrupt, IRQF_DISABLED, |
| 368 | mbox->name, mbox); |
| 369 | if (unlikely(ret)) { |
| 370 | printk(KERN_ERR |
| 371 | "failed to register mailbox interrupt:%d\n", ret); |
| 372 | goto fail_request_irq; |
| 373 | } |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 374 | |
| 375 | mq = mbox_queue_alloc(mbox, mbox_txq_fn, mbox_tx_work); |
| 376 | if (!mq) { |
| 377 | ret = -ENOMEM; |
| 378 | goto fail_alloc_txq; |
| 379 | } |
| 380 | mbox->txq = mq; |
| 381 | |
| 382 | mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work); |
| 383 | if (!mq) { |
| 384 | ret = -ENOMEM; |
| 385 | goto fail_alloc_rxq; |
| 386 | } |
| 387 | mbox->rxq = mq; |
| 388 | |
| 389 | return 0; |
| 390 | |
| 391 | fail_alloc_rxq: |
| 392 | mbox_queue_free(mbox->txq); |
| 393 | fail_alloc_txq: |
| 394 | free_irq(mbox->irq, mbox); |
| 395 | fail_request_irq: |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 396 | if (unlikely(mbox->ops->shutdown)) |
| 397 | mbox->ops->shutdown(mbox); |
| 398 | |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | static void omap_mbox_fini(struct omap_mbox *mbox) |
| 403 | { |
| 404 | mbox_queue_free(mbox->txq); |
| 405 | mbox_queue_free(mbox->rxq); |
| 406 | |
| 407 | free_irq(mbox->irq, mbox); |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 408 | |
| 409 | if (unlikely(mbox->ops->shutdown)) |
| 410 | mbox->ops->shutdown(mbox); |
| 411 | } |
| 412 | |
| 413 | static struct omap_mbox **find_mboxes(const char *name) |
| 414 | { |
| 415 | struct omap_mbox **p; |
| 416 | |
| 417 | for (p = &mboxes; *p; p = &(*p)->next) { |
| 418 | if (strcmp((*p)->name, name) == 0) |
| 419 | break; |
| 420 | } |
| 421 | |
| 422 | return p; |
| 423 | } |
| 424 | |
| 425 | struct omap_mbox *omap_mbox_get(const char *name) |
| 426 | { |
| 427 | struct omap_mbox *mbox; |
| 428 | int ret; |
| 429 | |
| 430 | read_lock(&mboxes_lock); |
| 431 | mbox = *(find_mboxes(name)); |
| 432 | if (mbox == NULL) { |
| 433 | read_unlock(&mboxes_lock); |
| 434 | return ERR_PTR(-ENOENT); |
| 435 | } |
| 436 | |
| 437 | read_unlock(&mboxes_lock); |
| 438 | |
| 439 | ret = omap_mbox_init(mbox); |
| 440 | if (ret) |
| 441 | return ERR_PTR(-ENODEV); |
| 442 | |
| 443 | return mbox; |
| 444 | } |
| 445 | EXPORT_SYMBOL(omap_mbox_get); |
| 446 | |
| 447 | void omap_mbox_put(struct omap_mbox *mbox) |
| 448 | { |
| 449 | omap_mbox_fini(mbox); |
| 450 | } |
| 451 | EXPORT_SYMBOL(omap_mbox_put); |
| 452 | |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 453 | int omap_mbox_register(struct device *parent, struct omap_mbox *mbox) |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 454 | { |
| 455 | int ret = 0; |
| 456 | struct omap_mbox **tmp; |
| 457 | |
| 458 | if (!mbox) |
| 459 | return -EINVAL; |
| 460 | if (mbox->next) |
| 461 | return -EBUSY; |
| 462 | |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 463 | mbox->dev = device_create(&omap_mbox_class, |
| 464 | parent, 0, mbox, "%s", mbox->name); |
| 465 | if (IS_ERR(mbox->dev)) |
| 466 | return PTR_ERR(mbox->dev); |
| 467 | |
| 468 | ret = device_create_file(mbox->dev, &dev_attr_mbox); |
| 469 | if (ret) |
| 470 | goto err_sysfs; |
| 471 | |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 472 | write_lock(&mboxes_lock); |
| 473 | tmp = find_mboxes(mbox->name); |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 474 | if (*tmp) { |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 475 | ret = -EBUSY; |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 476 | write_unlock(&mboxes_lock); |
| 477 | goto err_find; |
| 478 | } |
| 479 | *tmp = mbox; |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 480 | write_unlock(&mboxes_lock); |
| 481 | |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 482 | return 0; |
| 483 | |
| 484 | err_find: |
| 485 | device_remove_file(mbox->dev, &dev_attr_mbox); |
| 486 | err_sysfs: |
| 487 | device_unregister(mbox->dev); |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 488 | return ret; |
| 489 | } |
| 490 | EXPORT_SYMBOL(omap_mbox_register); |
| 491 | |
| 492 | int omap_mbox_unregister(struct omap_mbox *mbox) |
| 493 | { |
| 494 | struct omap_mbox **tmp; |
| 495 | |
| 496 | write_lock(&mboxes_lock); |
| 497 | tmp = &mboxes; |
| 498 | while (*tmp) { |
| 499 | if (mbox == *tmp) { |
| 500 | *tmp = mbox->next; |
| 501 | mbox->next = NULL; |
| 502 | write_unlock(&mboxes_lock); |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 503 | device_remove_file(mbox->dev, &dev_attr_mbox); |
| 504 | device_unregister(mbox->dev); |
Hiroshi DOYU | 340a614a | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 505 | return 0; |
| 506 | } |
| 507 | tmp = &(*tmp)->next; |
| 508 | } |
| 509 | write_unlock(&mboxes_lock); |
| 510 | |
| 511 | return -EINVAL; |
| 512 | } |
| 513 | EXPORT_SYMBOL(omap_mbox_unregister); |
| 514 | |
| 515 | static int __init omap_mbox_class_init(void) |
| 516 | { |
| 517 | int ret = class_register(&omap_mbox_class); |
| 518 | if (!ret) |
| 519 | ret = class_create_file(&omap_mbox_class, &class_attr_mbox); |
| 520 | |
| 521 | return ret; |
| 522 | } |
| 523 | |
| 524 | static void __exit omap_mbox_class_exit(void) |
| 525 | { |
| 526 | class_remove_file(&omap_mbox_class, &class_attr_mbox); |
| 527 | class_unregister(&omap_mbox_class); |
| 528 | } |
| 529 | |
| 530 | subsys_initcall(omap_mbox_class_init); |
| 531 | module_exit(omap_mbox_class_exit); |
| 532 | |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 533 | MODULE_LICENSE("GPL v2"); |
| 534 | MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging"); |
| 535 | MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU"); |