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