arm: msm: smd: checkpatch clean up of smd/proc_comm
[safe/jmp/linux-2.6] / arch / arm / mach-msm / smd.c
1 /* arch/arm/mach-msm/smd.c
2  *
3  * Copyright (C) 2007 Google, Inc.
4  * Author: Brian Swetland <swetland@google.com>
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 #include <linux/platform_device.h>
18 #include <linux/module.h>
19 #include <linux/fs.h>
20 #include <linux/cdev.h>
21 #include <linux/device.h>
22 #include <linux/wait.h>
23 #include <linux/interrupt.h>
24 #include <linux/irq.h>
25 #include <linux/list.h>
26 #include <linux/slab.h>
27 #include <linux/debugfs.h>
28 #include <linux/delay.h>
29 #include <linux/io.h>
30
31 #include <mach/msm_smd.h>
32 #include <mach/msm_iomap.h>
33 #include <mach/system.h>
34
35 #include "smd_private.h"
36 #include "proc_comm.h"
37
38 #if defined(CONFIG_ARCH_QSD8X50)
39 #define CONFIG_QDSP6 1
40 #endif
41
42 void (*msm_hw_reset_hook)(void);
43
44 #define MODULE_NAME "msm_smd"
45
46 enum {
47         MSM_SMD_DEBUG = 1U << 0,
48         MSM_SMSM_DEBUG = 1U << 0,
49 };
50
51 static int msm_smd_debug_mask;
52
53 struct shared_info {
54         int ready;
55         unsigned state;
56 };
57
58 static unsigned dummy_state[SMSM_STATE_COUNT];
59
60 static struct shared_info smd_info = {
61         .state = (unsigned) &dummy_state,
62 };
63
64 module_param_named(debug_mask, msm_smd_debug_mask,
65                    int, S_IRUGO | S_IWUSR | S_IWGRP);
66
67 void *smem_item(unsigned id, unsigned *size);
68 static void smd_diag(void);
69
70 static unsigned last_heap_free = 0xffffffff;
71
72 static inline void msm_a2m_int(uint32_t irq)
73 {
74 #if defined(CONFIG_ARCH_MSM7X30)
75         writel(1 << irq, MSM_GCC_BASE + 0x8);
76 #else
77         writel(1, MSM_CSR_BASE + 0x400 + (irq * 4));
78 #endif
79 }
80
81
82 static inline void notify_other_smsm(void)
83 {
84         msm_a2m_int(5);
85 #ifdef CONFIG_QDSP6
86         msm_a2m_int(8);
87 #endif
88 }
89
90 static inline void notify_modem_smd(void)
91 {
92         msm_a2m_int(0);
93 }
94
95 static inline void notify_dsp_smd(void)
96 {
97         msm_a2m_int(8);
98 }
99
100 static void smd_diag(void)
101 {
102         char *x;
103
104         x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
105         if (x != 0) {
106                 x[SZ_DIAG_ERR_MSG - 1] = 0;
107                 pr_info("smem: DIAG '%s'\n", x);
108         }
109 }
110
111 /* call when SMSM_RESET flag is set in the A9's smsm_state */
112 static void handle_modem_crash(void)
113 {
114         pr_err("ARM9 has CRASHED\n");
115         smd_diag();
116
117         /* hard reboot if possible */
118         if (msm_hw_reset_hook)
119                 msm_hw_reset_hook();
120
121         /* in this case the modem or watchdog should reboot us */
122         for (;;)
123                 ;
124 }
125
126 uint32_t raw_smsm_get_state(enum smsm_state_item item)
127 {
128         return readl(smd_info.state + item * 4);
129 }
130
131 static int check_for_modem_crash(void)
132 {
133         if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET) {
134                 handle_modem_crash();
135                 return -1;
136         }
137         return 0;
138 }
139
140 /* the spinlock is used to synchronize between the
141  * irq handler and code that mutates the channel
142  * list or fiddles with channel state
143  */
144 DEFINE_SPINLOCK(smd_lock);
145 DEFINE_SPINLOCK(smem_lock);
146
147 /* the mutex is used during open() and close()
148  * operations to avoid races while creating or
149  * destroying smd_channel structures
150  */
151 static DEFINE_MUTEX(smd_creation_mutex);
152
153 static int smd_initialized;
154
155 LIST_HEAD(smd_ch_closed_list);
156 LIST_HEAD(smd_ch_list_modem);
157 LIST_HEAD(smd_ch_list_dsp);
158
159 static unsigned char smd_ch_allocated[64];
160 static struct work_struct probe_work;
161
162 static int smd_alloc_channel(const char *name, uint32_t cid, uint32_t type);
163
164 static void smd_channel_probe_worker(struct work_struct *work)
165 {
166         struct smd_alloc_elm *shared;
167         unsigned ctype;
168         unsigned type;
169         unsigned n;
170
171         shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
172         if (!shared) {
173                 pr_err("smd: cannot find allocation table\n");
174                 return;
175         }
176         for (n = 0; n < 64; n++) {
177                 if (smd_ch_allocated[n])
178                         continue;
179                 if (!shared[n].ref_count)
180                         continue;
181                 if (!shared[n].name[0])
182                         continue;
183                 ctype = shared[n].ctype;
184                 type = ctype & SMD_TYPE_MASK;
185
186                 /* DAL channels are stream but neither the modem,
187                  * nor the DSP correctly indicate this.  Fixup manually.
188                  */
189                 if (!memcmp(shared[n].name, "DAL", 3))
190                         ctype = (ctype & (~SMD_KIND_MASK)) | SMD_KIND_STREAM;
191
192                 type = shared[n].ctype & SMD_TYPE_MASK;
193                 if ((type == SMD_TYPE_APPS_MODEM) ||
194                     (type == SMD_TYPE_APPS_DSP))
195                         if (!smd_alloc_channel(shared[n].name, shared[n].cid, ctype))
196                                 smd_ch_allocated[n] = 1;
197         }
198 }
199
200 /* how many bytes are available for reading */
201 static int smd_stream_read_avail(struct smd_channel *ch)
202 {
203         return (ch->recv->head - ch->recv->tail) & ch->fifo_mask;
204 }
205
206 /* how many bytes we are free to write */
207 static int smd_stream_write_avail(struct smd_channel *ch)
208 {
209         return ch->fifo_mask -
210                 ((ch->send->head - ch->send->tail) & ch->fifo_mask);
211 }
212
213 static int smd_packet_read_avail(struct smd_channel *ch)
214 {
215         if (ch->current_packet) {
216                 int n = smd_stream_read_avail(ch);
217                 if (n > ch->current_packet)
218                         n = ch->current_packet;
219                 return n;
220         } else {
221                 return 0;
222         }
223 }
224
225 static int smd_packet_write_avail(struct smd_channel *ch)
226 {
227         int n = smd_stream_write_avail(ch);
228         return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
229 }
230
231 static int ch_is_open(struct smd_channel *ch)
232 {
233         return (ch->recv->state == SMD_SS_OPENED) &&
234                 (ch->send->state == SMD_SS_OPENED);
235 }
236
237 /* provide a pointer and length to readable data in the fifo */
238 static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
239 {
240         unsigned head = ch->recv->head;
241         unsigned tail = ch->recv->tail;
242         *ptr = (void *) (ch->recv_data + tail);
243
244         if (tail <= head)
245                 return head - tail;
246         else
247                 return ch->fifo_size - tail;
248 }
249
250 /* advance the fifo read pointer after data from ch_read_buffer is consumed */
251 static void ch_read_done(struct smd_channel *ch, unsigned count)
252 {
253         BUG_ON(count > smd_stream_read_avail(ch));
254         ch->recv->tail = (ch->recv->tail + count) & ch->fifo_mask;
255         ch->send->fTAIL = 1;
256 }
257
258 /* basic read interface to ch_read_{buffer,done} used
259  * by smd_*_read() and update_packet_state()
260  * will read-and-discard if the _data pointer is null
261  */
262 static int ch_read(struct smd_channel *ch, void *_data, int len)
263 {
264         void *ptr;
265         unsigned n;
266         unsigned char *data = _data;
267         int orig_len = len;
268
269         while (len > 0) {
270                 n = ch_read_buffer(ch, &ptr);
271                 if (n == 0)
272                         break;
273
274                 if (n > len)
275                         n = len;
276                 if (_data)
277                         memcpy(data, ptr, n);
278
279                 data += n;
280                 len -= n;
281                 ch_read_done(ch, n);
282         }
283
284         return orig_len - len;
285 }
286
287 static void update_stream_state(struct smd_channel *ch)
288 {
289         /* streams have no special state requiring updating */
290 }
291
292 static void update_packet_state(struct smd_channel *ch)
293 {
294         unsigned hdr[5];
295         int r;
296
297         /* can't do anything if we're in the middle of a packet */
298         if (ch->current_packet != 0)
299                 return;
300
301         /* don't bother unless we can get the full header */
302         if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
303                 return;
304
305         r = ch_read(ch, hdr, SMD_HEADER_SIZE);
306         BUG_ON(r != SMD_HEADER_SIZE);
307
308         ch->current_packet = hdr[0];
309 }
310
311 /* provide a pointer and length to next free space in the fifo */
312 static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
313 {
314         unsigned head = ch->send->head;
315         unsigned tail = ch->send->tail;
316         *ptr = (void *) (ch->send_data + head);
317
318         if (head < tail) {
319                 return tail - head - 1;
320         } else {
321                 if (tail == 0)
322                         return ch->fifo_size - head - 1;
323                 else
324                         return ch->fifo_size - head;
325         }
326 }
327
328 /* advace the fifo write pointer after freespace
329  * from ch_write_buffer is filled
330  */
331 static void ch_write_done(struct smd_channel *ch, unsigned count)
332 {
333         BUG_ON(count > smd_stream_write_avail(ch));
334         ch->send->head = (ch->send->head + count) & ch->fifo_mask;
335         ch->send->fHEAD = 1;
336 }
337
338 static void ch_set_state(struct smd_channel *ch, unsigned n)
339 {
340         if (n == SMD_SS_OPENED) {
341                 ch->send->fDSR = 1;
342                 ch->send->fCTS = 1;
343                 ch->send->fCD = 1;
344         } else {
345                 ch->send->fDSR = 0;
346                 ch->send->fCTS = 0;
347                 ch->send->fCD = 0;
348         }
349         ch->send->state = n;
350         ch->send->fSTATE = 1;
351         ch->notify_other_cpu();
352 }
353
354 static void do_smd_probe(void)
355 {
356         struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
357         if (shared->heap_info.free_offset != last_heap_free) {
358                 last_heap_free = shared->heap_info.free_offset;
359                 schedule_work(&probe_work);
360         }
361 }
362
363 static void smd_state_change(struct smd_channel *ch,
364                              unsigned last, unsigned next)
365 {
366         ch->last_state = next;
367
368         pr_info("SMD: ch %d %d -> %d\n", ch->n, last, next);
369
370         switch (next) {
371         case SMD_SS_OPENING:
372                 ch->recv->tail = 0;
373         case SMD_SS_OPENED:
374                 if (ch->send->state != SMD_SS_OPENED)
375                         ch_set_state(ch, SMD_SS_OPENED);
376                 ch->notify(ch->priv, SMD_EVENT_OPEN);
377                 break;
378         case SMD_SS_FLUSHING:
379         case SMD_SS_RESET:
380                 /* we should force them to close? */
381         default:
382                 ch->notify(ch->priv, SMD_EVENT_CLOSE);
383         }
384 }
385
386 static void handle_smd_irq(struct list_head *list, void (*notify)(void))
387 {
388         unsigned long flags;
389         struct smd_channel *ch;
390         int do_notify = 0;
391         unsigned ch_flags;
392         unsigned tmp;
393
394         spin_lock_irqsave(&smd_lock, flags);
395         list_for_each_entry(ch, list, ch_list) {
396                 ch_flags = 0;
397                 if (ch_is_open(ch)) {
398                         if (ch->recv->fHEAD) {
399                                 ch->recv->fHEAD = 0;
400                                 ch_flags |= 1;
401                                 do_notify |= 1;
402                         }
403                         if (ch->recv->fTAIL) {
404                                 ch->recv->fTAIL = 0;
405                                 ch_flags |= 2;
406                                 do_notify |= 1;
407                         }
408                         if (ch->recv->fSTATE) {
409                                 ch->recv->fSTATE = 0;
410                                 ch_flags |= 4;
411                                 do_notify |= 1;
412                         }
413                 }
414                 tmp = ch->recv->state;
415                 if (tmp != ch->last_state)
416                         smd_state_change(ch, ch->last_state, tmp);
417                 if (ch_flags) {
418                         ch->update_state(ch);
419                         ch->notify(ch->priv, SMD_EVENT_DATA);
420                 }
421         }
422         if (do_notify)
423                 notify();
424         spin_unlock_irqrestore(&smd_lock, flags);
425         do_smd_probe();
426 }
427
428 static irqreturn_t smd_modem_irq_handler(int irq, void *data)
429 {
430         handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
431         return IRQ_HANDLED;
432 }
433
434 static irqreturn_t smd_dsp_irq_handler(int irq, void *data)
435 {
436         handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
437         return IRQ_HANDLED;
438 }
439
440 static void smd_fake_irq_handler(unsigned long arg)
441 {
442         handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
443         handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
444 }
445
446 static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
447
448 static inline int smd_need_int(struct smd_channel *ch)
449 {
450         if (ch_is_open(ch)) {
451                 if (ch->recv->fHEAD || ch->recv->fTAIL || ch->recv->fSTATE)
452                         return 1;
453                 if (ch->recv->state != ch->last_state)
454                         return 1;
455         }
456         return 0;
457 }
458
459 void smd_sleep_exit(void)
460 {
461         unsigned long flags;
462         struct smd_channel *ch;
463         int need_int = 0;
464
465         spin_lock_irqsave(&smd_lock, flags);
466         list_for_each_entry(ch, &smd_ch_list_modem, ch_list) {
467                 if (smd_need_int(ch)) {
468                         need_int = 1;
469                         break;
470                 }
471         }
472         list_for_each_entry(ch, &smd_ch_list_dsp, ch_list) {
473                 if (smd_need_int(ch)) {
474                         need_int = 1;
475                         break;
476                 }
477         }
478         spin_unlock_irqrestore(&smd_lock, flags);
479         do_smd_probe();
480
481         if (need_int) {
482                 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
483                         pr_info("smd_sleep_exit need interrupt\n");
484                 tasklet_schedule(&smd_fake_irq_tasklet);
485         }
486 }
487
488
489 void smd_kick(smd_channel_t *ch)
490 {
491         unsigned long flags;
492         unsigned tmp;
493
494         spin_lock_irqsave(&smd_lock, flags);
495         ch->update_state(ch);
496         tmp = ch->recv->state;
497         if (tmp != ch->last_state) {
498                 ch->last_state = tmp;
499                 if (tmp == SMD_SS_OPENED)
500                         ch->notify(ch->priv, SMD_EVENT_OPEN);
501                 else
502                         ch->notify(ch->priv, SMD_EVENT_CLOSE);
503         }
504         ch->notify(ch->priv, SMD_EVENT_DATA);
505         ch->notify_other_cpu();
506         spin_unlock_irqrestore(&smd_lock, flags);
507 }
508
509 static int smd_is_packet(int chn, unsigned type)
510 {
511         type &= SMD_KIND_MASK;
512         if (type == SMD_KIND_PACKET)
513                 return 1;
514         if (type == SMD_KIND_STREAM)
515                 return 0;
516
517         /* older AMSS reports SMD_KIND_UNKNOWN always */
518         if ((chn > 4) || (chn == 1))
519                 return 1;
520         else
521                 return 0;
522 }
523
524 static int smd_stream_write(smd_channel_t *ch, const void *_data, int len)
525 {
526         void *ptr;
527         const unsigned char *buf = _data;
528         unsigned xfer;
529         int orig_len = len;
530
531         if (len < 0)
532                 return -EINVAL;
533
534         while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
535                 if (!ch_is_open(ch))
536                         break;
537                 if (xfer > len)
538                         xfer = len;
539                 memcpy(ptr, buf, xfer);
540                 ch_write_done(ch, xfer);
541                 len -= xfer;
542                 buf += xfer;
543                 if (len == 0)
544                         break;
545         }
546
547         ch->notify_other_cpu();
548
549         return orig_len - len;
550 }
551
552 static int smd_packet_write(smd_channel_t *ch, const void *_data, int len)
553 {
554         unsigned hdr[5];
555
556         if (len < 0)
557                 return -EINVAL;
558
559         if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
560                 return -ENOMEM;
561
562         hdr[0] = len;
563         hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
564
565         smd_stream_write(ch, hdr, sizeof(hdr));
566         smd_stream_write(ch, _data, len);
567
568         return len;
569 }
570
571 static int smd_stream_read(smd_channel_t *ch, void *data, int len)
572 {
573         int r;
574
575         if (len < 0)
576                 return -EINVAL;
577
578         r = ch_read(ch, data, len);
579         if (r > 0)
580                 ch->notify_other_cpu();
581
582         return r;
583 }
584
585 static int smd_packet_read(smd_channel_t *ch, void *data, int len)
586 {
587         unsigned long flags;
588         int r;
589
590         if (len < 0)
591                 return -EINVAL;
592
593         if (len > ch->current_packet)
594                 len = ch->current_packet;
595
596         r = ch_read(ch, data, len);
597         if (r > 0)
598                 ch->notify_other_cpu();
599
600         spin_lock_irqsave(&smd_lock, flags);
601         ch->current_packet -= r;
602         update_packet_state(ch);
603         spin_unlock_irqrestore(&smd_lock, flags);
604
605         return r;
606 }
607
608 static int smd_alloc_v2(struct smd_channel *ch)
609 {
610         struct smd_shared_v2 *shared2;
611         void *buffer;
612         unsigned buffer_sz;
613
614         shared2 = smem_alloc(SMEM_SMD_BASE_ID + ch->n, sizeof(*shared2));
615         buffer = smem_item(SMEM_SMD_FIFO_BASE_ID + ch->n, &buffer_sz);
616
617         if (!buffer)
618                 return -1;
619
620         /* buffer must be a power-of-two size */
621         if (buffer_sz & (buffer_sz - 1))
622                 return -1;
623
624         buffer_sz /= 2;
625         ch->send = &shared2->ch0;
626         ch->recv = &shared2->ch1;
627         ch->send_data = buffer;
628         ch->recv_data = buffer + buffer_sz;
629         ch->fifo_size = buffer_sz;
630         return 0;
631 }
632
633 static int smd_alloc_v1(struct smd_channel *ch)
634 {
635         struct smd_shared_v1 *shared1;
636         shared1 = smem_alloc(ID_SMD_CHANNELS + ch->n, sizeof(*shared1));
637         if (!shared1) {
638                 pr_err("smd_alloc_channel() cid %d does not exist\n", ch->n);
639                 return -1;
640         }
641         ch->send = &shared1->ch0;
642         ch->recv = &shared1->ch1;
643         ch->send_data = shared1->data0;
644         ch->recv_data = shared1->data1;
645         ch->fifo_size = SMD_BUF_SIZE;
646         return 0;
647 }
648
649
650 static int smd_alloc_channel(const char *name, uint32_t cid, uint32_t type)
651 {
652         struct smd_channel *ch;
653
654         ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
655         if (ch == 0) {
656                 pr_err("smd_alloc_channel() out of memory\n");
657                 return -1;
658         }
659         ch->n = cid;
660
661         if (smd_alloc_v2(ch) && smd_alloc_v1(ch)) {
662                 kfree(ch);
663                 return -1;
664         }
665
666         ch->fifo_mask = ch->fifo_size - 1;
667         ch->type = type;
668
669         if ((type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
670                 ch->notify_other_cpu = notify_modem_smd;
671         else
672                 ch->notify_other_cpu = notify_dsp_smd;
673
674         if (smd_is_packet(cid, type)) {
675                 ch->read = smd_packet_read;
676                 ch->write = smd_packet_write;
677                 ch->read_avail = smd_packet_read_avail;
678                 ch->write_avail = smd_packet_write_avail;
679                 ch->update_state = update_packet_state;
680         } else {
681                 ch->read = smd_stream_read;
682                 ch->write = smd_stream_write;
683                 ch->read_avail = smd_stream_read_avail;
684                 ch->write_avail = smd_stream_write_avail;
685                 ch->update_state = update_stream_state;
686         }
687
688         if ((type & 0xff) == 0)
689                 memcpy(ch->name, "SMD_", 4);
690         else
691                 memcpy(ch->name, "DSP_", 4);
692         memcpy(ch->name + 4, name, 20);
693         ch->name[23] = 0;
694         ch->pdev.name = ch->name;
695         ch->pdev.id = -1;
696
697         pr_info("smd_alloc_channel() cid=%02d size=%05d '%s'\n",
698                 ch->n, ch->fifo_size, ch->name);
699
700         mutex_lock(&smd_creation_mutex);
701         list_add(&ch->ch_list, &smd_ch_closed_list);
702         mutex_unlock(&smd_creation_mutex);
703
704         platform_device_register(&ch->pdev);
705         return 0;
706 }
707
708 static void do_nothing_notify(void *priv, unsigned flags)
709 {
710 }
711
712 struct smd_channel *smd_get_channel(const char *name)
713 {
714         struct smd_channel *ch;
715
716         mutex_lock(&smd_creation_mutex);
717         list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
718                 if (!strcmp(name, ch->name)) {
719                         list_del(&ch->ch_list);
720                         mutex_unlock(&smd_creation_mutex);
721                         return ch;
722                 }
723         }
724         mutex_unlock(&smd_creation_mutex);
725
726         return NULL;
727 }
728
729 int smd_open(const char *name, smd_channel_t **_ch,
730              void *priv, void (*notify)(void *, unsigned))
731 {
732         struct smd_channel *ch;
733         unsigned long flags;
734
735         if (smd_initialized == 0) {
736                 pr_info("smd_open() before smd_init()\n");
737                 return -ENODEV;
738         }
739
740         ch = smd_get_channel(name);
741         if (!ch)
742                 return -ENODEV;
743
744         if (notify == 0)
745                 notify = do_nothing_notify;
746
747         ch->notify = notify;
748         ch->current_packet = 0;
749         ch->last_state = SMD_SS_CLOSED;
750         ch->priv = priv;
751
752         *_ch = ch;
753
754         spin_lock_irqsave(&smd_lock, flags);
755
756         if ((ch->type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
757                 list_add(&ch->ch_list, &smd_ch_list_modem);
758         else
759                 list_add(&ch->ch_list, &smd_ch_list_dsp);
760
761         /* If the remote side is CLOSING, we need to get it to
762          * move to OPENING (which we'll do by moving from CLOSED to
763          * OPENING) and then get it to move from OPENING to
764          * OPENED (by doing the same state change ourselves).
765          *
766          * Otherwise, it should be OPENING and we can move directly
767          * to OPENED so that it will follow.
768          */
769         if (ch->recv->state == SMD_SS_CLOSING) {
770                 ch->send->head = 0;
771                 ch_set_state(ch, SMD_SS_OPENING);
772         } else {
773                 ch_set_state(ch, SMD_SS_OPENED);
774         }
775         spin_unlock_irqrestore(&smd_lock, flags);
776         smd_kick(ch);
777
778         return 0;
779 }
780
781 int smd_close(smd_channel_t *ch)
782 {
783         unsigned long flags;
784
785         pr_info("smd_close(%p)\n", ch);
786
787         if (ch == 0)
788                 return -1;
789
790         spin_lock_irqsave(&smd_lock, flags);
791         ch->notify = do_nothing_notify;
792         list_del(&ch->ch_list);
793         ch_set_state(ch, SMD_SS_CLOSED);
794         spin_unlock_irqrestore(&smd_lock, flags);
795
796         mutex_lock(&smd_creation_mutex);
797         list_add(&ch->ch_list, &smd_ch_closed_list);
798         mutex_unlock(&smd_creation_mutex);
799
800         return 0;
801 }
802
803 int smd_read(smd_channel_t *ch, void *data, int len)
804 {
805         return ch->read(ch, data, len);
806 }
807
808 int smd_write(smd_channel_t *ch, const void *data, int len)
809 {
810         return ch->write(ch, data, len);
811 }
812
813 int smd_write_atomic(smd_channel_t *ch, const void *data, int len)
814 {
815         unsigned long flags;
816         int res;
817         spin_lock_irqsave(&smd_lock, flags);
818         res = ch->write(ch, data, len);
819         spin_unlock_irqrestore(&smd_lock, flags);
820         return res;
821 }
822
823 int smd_read_avail(smd_channel_t *ch)
824 {
825         return ch->read_avail(ch);
826 }
827
828 int smd_write_avail(smd_channel_t *ch)
829 {
830         return ch->write_avail(ch);
831 }
832
833 int smd_wait_until_readable(smd_channel_t *ch, int bytes)
834 {
835         return -1;
836 }
837
838 int smd_wait_until_writable(smd_channel_t *ch, int bytes)
839 {
840         return -1;
841 }
842
843 int smd_cur_packet_size(smd_channel_t *ch)
844 {
845         return ch->current_packet;
846 }
847
848
849 /* ------------------------------------------------------------------------- */
850
851 void *smem_alloc(unsigned id, unsigned size)
852 {
853         return smem_find(id, size);
854 }
855
856 void *smem_item(unsigned id, unsigned *size)
857 {
858         struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
859         struct smem_heap_entry *toc = shared->heap_toc;
860
861         if (id >= SMEM_NUM_ITEMS)
862                 return 0;
863
864         if (toc[id].allocated) {
865                 *size = toc[id].size;
866                 return (void *) (MSM_SHARED_RAM_BASE + toc[id].offset);
867         } else {
868                 *size = 0;
869         }
870
871         return 0;
872 }
873
874 void *smem_find(unsigned id, unsigned size_in)
875 {
876         unsigned size;
877         void *ptr;
878
879         ptr = smem_item(id, &size);
880         if (!ptr)
881                 return 0;
882
883         size_in = ALIGN(size_in, 8);
884         if (size_in != size) {
885                 pr_err("smem_find(%d, %d): wrong size %d\n",
886                        id, size_in, size);
887                 return 0;
888         }
889
890         return ptr;
891 }
892
893 static irqreturn_t smsm_irq_handler(int irq, void *data)
894 {
895         unsigned long flags;
896         unsigned apps, modm;
897
898         spin_lock_irqsave(&smem_lock, flags);
899
900         apps = raw_smsm_get_state(SMSM_STATE_APPS);
901         modm = raw_smsm_get_state(SMSM_STATE_MODEM);
902
903         if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
904                 pr_info("<SM %08x %08x>\n", apps, modm);
905         if (modm & SMSM_RESET)
906                 handle_modem_crash();
907
908         do_smd_probe();
909
910         spin_unlock_irqrestore(&smem_lock, flags);
911         return IRQ_HANDLED;
912 }
913
914 int smsm_change_state(enum smsm_state_item item,
915                       uint32_t clear_mask, uint32_t set_mask)
916 {
917         unsigned long flags;
918         unsigned state;
919         unsigned addr = smd_info.state + item * 4;
920
921         if (!smd_info.ready)
922                 return -EIO;
923
924         spin_lock_irqsave(&smem_lock, flags);
925
926         if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET)
927                 handle_modem_crash();
928
929         state = (readl(addr) & ~clear_mask) | set_mask;
930         writel(state, addr);
931
932         if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
933                 pr_info("smsm_change_state %d %x\n", item, state);
934         notify_other_smsm();
935
936         spin_unlock_irqrestore(&smem_lock, flags);
937
938         return 0;
939 }
940
941 uint32_t smsm_get_state(enum smsm_state_item item)
942 {
943         unsigned long flags;
944         uint32_t rv;
945
946         spin_lock_irqsave(&smem_lock, flags);
947
948         rv = readl(smd_info.state + item * 4);
949
950         if (item == SMSM_STATE_MODEM && (rv & SMSM_RESET))
951                 handle_modem_crash();
952
953         spin_unlock_irqrestore(&smem_lock, flags);
954
955         return rv;
956 }
957
958 #ifdef CONFIG_ARCH_MSM_SCORPION
959
960 int smsm_set_sleep_duration(uint32_t delay)
961 {
962         struct msm_dem_slave_data *ptr;
963
964         ptr = smem_find(SMEM_APPS_DEM_SLAVE_DATA, sizeof(*ptr));
965         if (ptr == NULL) {
966                 pr_err("smsm_set_sleep_duration <SM NO APPS_DEM_SLAVE_DATA>\n");
967                 return -EIO;
968         }
969         if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
970                 pr_info("smsm_set_sleep_duration %d -> %d\n",
971                        ptr->sleep_time, delay);
972         ptr->sleep_time = delay;
973         return 0;
974 }
975
976 #else
977
978 int smsm_set_sleep_duration(uint32_t delay)
979 {
980         uint32_t *ptr;
981
982         ptr = smem_find(SMEM_SMSM_SLEEP_DELAY, sizeof(*ptr));
983         if (ptr == NULL) {
984                 pr_err("smsm_set_sleep_duration <SM NO SLEEP_DELAY>\n");
985                 return -EIO;
986         }
987         if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
988                 pr_info("smsm_set_sleep_duration %d -> %d\n",
989                        *ptr, delay);
990         *ptr = delay;
991         return 0;
992 }
993
994 #endif
995
996 int smd_core_init(void)
997 {
998         int r;
999         pr_info("smd_core_init()\n");
1000
1001         /* wait for essential items to be initialized */
1002         for (;;) {
1003                 unsigned size;
1004                 void *state;
1005                 state = smem_item(SMEM_SMSM_SHARED_STATE, &size);
1006                 if (size == SMSM_V1_SIZE || size == SMSM_V2_SIZE) {
1007                         smd_info.state = (unsigned)state;
1008                         break;
1009                 }
1010         }
1011
1012         smd_info.ready = 1;
1013
1014         r = request_irq(INT_A9_M2A_0, smd_modem_irq_handler,
1015                         IRQF_TRIGGER_RISING, "smd_dev", 0);
1016         if (r < 0)
1017                 return r;
1018         r = enable_irq_wake(INT_A9_M2A_0);
1019         if (r < 0)
1020                 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_0\n");
1021
1022         r = request_irq(INT_A9_M2A_5, smsm_irq_handler,
1023                         IRQF_TRIGGER_RISING, "smsm_dev", 0);
1024         if (r < 0) {
1025                 free_irq(INT_A9_M2A_0, 0);
1026                 return r;
1027         }
1028         r = enable_irq_wake(INT_A9_M2A_5);
1029         if (r < 0)
1030                 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_5\n");
1031
1032 #if defined(CONFIG_QDSP6)
1033         r = request_irq(INT_ADSP_A11, smd_dsp_irq_handler,
1034                         IRQF_TRIGGER_RISING, "smd_dsp", 0);
1035         if (r < 0) {
1036                 free_irq(INT_A9_M2A_0, 0);
1037                 free_irq(INT_A9_M2A_5, 0);
1038                 return r;
1039         }
1040 #endif
1041
1042         /* check for any SMD channels that may already exist */
1043         do_smd_probe();
1044
1045         /* indicate that we're up and running */
1046         smsm_change_state(SMSM_STATE_APPS,
1047                           ~0, SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT | SMSM_RUN);
1048 #ifdef CONFIG_ARCH_MSM_SCORPION
1049         smsm_change_state(SMSM_STATE_APPS_DEM, ~0, 0);
1050 #endif
1051
1052         pr_info("smd_core_init() done\n");
1053
1054         return 0;
1055 }
1056
1057 static int __init msm_smd_probe(struct platform_device *pdev)
1058 {
1059         pr_info("smd_init()\n");
1060
1061         INIT_WORK(&probe_work, smd_channel_probe_worker);
1062
1063         if (smd_core_init()) {
1064                 pr_err("smd_core_init() failed\n");
1065                 return -1;
1066         }
1067
1068         do_smd_probe();
1069
1070         msm_check_for_modem_crash = check_for_modem_crash;
1071
1072         msm_init_last_radio_log(THIS_MODULE);
1073
1074         smd_initialized = 1;
1075
1076         return 0;
1077 }
1078
1079 static struct platform_driver msm_smd_driver = {
1080         .probe = msm_smd_probe,
1081         .driver = {
1082                 .name = MODULE_NAME,
1083                 .owner = THIS_MODULE,
1084         },
1085 };
1086
1087 static int __init msm_smd_init(void)
1088 {
1089         return platform_driver_register(&msm_smd_driver);
1090 }
1091
1092 module_init(msm_smd_init);
1093
1094 MODULE_DESCRIPTION("MSM Shared Memory Core");
1095 MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
1096 MODULE_LICENSE("GPL");