]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/soc/qcom/smd.c
Merge remote-tracking branch 'drm/drm-next'
[karo-tx-linux.git] / drivers / soc / qcom / smd.c
1 /*
2  * Copyright (c) 2015, Sony Mobile Communications AB.
3  * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 and
7  * only version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <linux/interrupt.h>
16 #include <linux/io.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/module.h>
19 #include <linux/of_irq.h>
20 #include <linux/of_platform.h>
21 #include <linux/platform_device.h>
22 #include <linux/regmap.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <linux/soc/qcom/smd.h>
26 #include <linux/soc/qcom/smem.h>
27 #include <linux/wait.h>
28
29 /*
30  * The Qualcomm Shared Memory communication solution provides point-to-point
31  * channels for clients to send and receive streaming or packet based data.
32  *
33  * Each channel consists of a control item (channel info) and a ring buffer
34  * pair. The channel info carry information related to channel state, flow
35  * control and the offsets within the ring buffer.
36  *
37  * All allocated channels are listed in an allocation table, identifying the
38  * pair of items by name, type and remote processor.
39  *
40  * Upon creating a new channel the remote processor allocates channel info and
41  * ring buffer items from the smem heap and populate the allocation table. An
42  * interrupt is sent to the other end of the channel and a scan for new
43  * channels should be done. A channel never goes away, it will only change
44  * state.
45  *
46  * The remote processor signals it intent for bring up the communication
47  * channel by setting the state of its end of the channel to "opening" and
48  * sends out an interrupt. We detect this change and register a smd device to
49  * consume the channel. Upon finding a consumer we finish the handshake and the
50  * channel is up.
51  *
52  * Upon closing a channel, the remote processor will update the state of its
53  * end of the channel and signal us, we will then unregister any attached
54  * device and close our end of the channel.
55  *
56  * Devices attached to a channel can use the qcom_smd_send function to push
57  * data to the channel, this is done by copying the data into the tx ring
58  * buffer, updating the pointers in the channel info and signaling the remote
59  * processor.
60  *
61  * The remote processor does the equivalent when it transfer data and upon
62  * receiving the interrupt we check the channel info for new data and delivers
63  * this to the attached device. If the device is not ready to receive the data
64  * we leave it in the ring buffer for now.
65  */
66
67 struct smd_channel_info;
68 struct smd_channel_info_pair;
69 struct smd_channel_info_word;
70 struct smd_channel_info_word_pair;
71
72 #define SMD_ALLOC_TBL_COUNT     2
73 #define SMD_ALLOC_TBL_SIZE      64
74
75 /*
76  * This lists the various smem heap items relevant for the allocation table and
77  * smd channel entries.
78  */
79 static const struct {
80         unsigned alloc_tbl_id;
81         unsigned info_base_id;
82         unsigned fifo_base_id;
83 } smem_items[SMD_ALLOC_TBL_COUNT] = {
84         {
85                 .alloc_tbl_id = 13,
86                 .info_base_id = 14,
87                 .fifo_base_id = 338
88         },
89         {
90                 .alloc_tbl_id = 266,
91                 .info_base_id = 138,
92                 .fifo_base_id = 202,
93         },
94 };
95
96 /**
97  * struct qcom_smd_edge - representing a remote processor
98  * @smd:                handle to qcom_smd
99  * @of_node:            of_node handle for information related to this edge
100  * @edge_id:            identifier of this edge
101  * @remote_pid:         identifier of remote processor
102  * @irq:                interrupt for signals on this edge
103  * @ipc_regmap:         regmap handle holding the outgoing ipc register
104  * @ipc_offset:         offset within @ipc_regmap of the register for ipc
105  * @ipc_bit:            bit in the register at @ipc_offset of @ipc_regmap
106  * @channels:           list of all channels detected on this edge
107  * @channels_lock:      guard for modifications of @channels
108  * @allocated:          array of bitmaps representing already allocated channels
109  * @smem_available:     last available amount of smem triggering a channel scan
110  * @scan_work:          work item for discovering new channels
111  * @state_work:         work item for edge state changes
112  */
113 struct qcom_smd_edge {
114         struct qcom_smd *smd;
115         struct device_node *of_node;
116         unsigned edge_id;
117         unsigned remote_pid;
118
119         int irq;
120
121         struct regmap *ipc_regmap;
122         int ipc_offset;
123         int ipc_bit;
124
125         struct list_head channels;
126         rwlock_t channels_lock;
127
128         DECLARE_BITMAP(allocated[SMD_ALLOC_TBL_COUNT], SMD_ALLOC_TBL_SIZE);
129
130         unsigned smem_available;
131
132         wait_queue_head_t new_channel_event;
133
134         struct work_struct scan_work;
135         struct work_struct state_work;
136 };
137
138 /*
139  * SMD channel states.
140  */
141 enum smd_channel_state {
142         SMD_CHANNEL_CLOSED,
143         SMD_CHANNEL_OPENING,
144         SMD_CHANNEL_OPENED,
145         SMD_CHANNEL_FLUSHING,
146         SMD_CHANNEL_CLOSING,
147         SMD_CHANNEL_RESET,
148         SMD_CHANNEL_RESET_OPENING
149 };
150
151 /**
152  * struct qcom_smd_channel - smd channel struct
153  * @edge:               qcom_smd_edge this channel is living on
154  * @qsdev:              reference to a associated smd client device
155  * @name:               name of the channel
156  * @state:              local state of the channel
157  * @remote_state:       remote state of the channel
158  * @info:               byte aligned outgoing/incoming channel info
159  * @info_word:          word aligned outgoing/incoming channel info
160  * @tx_lock:            lock to make writes to the channel mutually exclusive
161  * @fblockread_event:   wakeup event tied to tx fBLOCKREADINTR
162  * @tx_fifo:            pointer to the outgoing ring buffer
163  * @rx_fifo:            pointer to the incoming ring buffer
164  * @fifo_size:          size of each ring buffer
165  * @bounce_buffer:      bounce buffer for reading wrapped packets
166  * @cb:                 callback function registered for this channel
167  * @recv_lock:          guard for rx info modifications and cb pointer
168  * @pkt_size:           size of the currently handled packet
169  * @list:               lite entry for @channels in qcom_smd_edge
170  */
171 struct qcom_smd_channel {
172         struct qcom_smd_edge *edge;
173
174         struct qcom_smd_device *qsdev;
175
176         char *name;
177         enum smd_channel_state state;
178         enum smd_channel_state remote_state;
179
180         struct smd_channel_info_pair *info;
181         struct smd_channel_info_word_pair *info_word;
182
183         struct mutex tx_lock;
184         wait_queue_head_t fblockread_event;
185
186         void *tx_fifo;
187         void *rx_fifo;
188         int fifo_size;
189
190         void *bounce_buffer;
191         qcom_smd_cb_t cb;
192
193         spinlock_t recv_lock;
194
195         int pkt_size;
196
197         struct list_head list;
198         struct list_head dev_list;
199 };
200
201 /**
202  * struct qcom_smd - smd struct
203  * @dev:        device struct
204  * @num_edges:  number of entries in @edges
205  * @edges:      array of edges to be handled
206  */
207 struct qcom_smd {
208         struct device *dev;
209
210         unsigned num_edges;
211         struct qcom_smd_edge edges[0];
212 };
213
214 /*
215  * Format of the smd_info smem items, for byte aligned channels.
216  */
217 struct smd_channel_info {
218         __le32 state;
219         u8  fDSR;
220         u8  fCTS;
221         u8  fCD;
222         u8  fRI;
223         u8  fHEAD;
224         u8  fTAIL;
225         u8  fSTATE;
226         u8  fBLOCKREADINTR;
227         __le32 tail;
228         __le32 head;
229 };
230
231 struct smd_channel_info_pair {
232         struct smd_channel_info tx;
233         struct smd_channel_info rx;
234 };
235
236 /*
237  * Format of the smd_info smem items, for word aligned channels.
238  */
239 struct smd_channel_info_word {
240         __le32 state;
241         __le32 fDSR;
242         __le32 fCTS;
243         __le32 fCD;
244         __le32 fRI;
245         __le32 fHEAD;
246         __le32 fTAIL;
247         __le32 fSTATE;
248         __le32 fBLOCKREADINTR;
249         __le32 tail;
250         __le32 head;
251 };
252
253 struct smd_channel_info_word_pair {
254         struct smd_channel_info_word tx;
255         struct smd_channel_info_word rx;
256 };
257
258 #define GET_RX_CHANNEL_FLAG(channel, param)                                  \
259         ({                                                                   \
260                 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \
261                 channel->info_word ?                                         \
262                         le32_to_cpu(channel->info_word->rx.param) :          \
263                         channel->info->rx.param;                             \
264         })
265
266 #define GET_RX_CHANNEL_INFO(channel, param)                                   \
267         ({                                                                    \
268                 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \
269                 le32_to_cpu(channel->info_word ?                              \
270                         channel->info_word->rx.param :                        \
271                         channel->info->rx.param);                             \
272         })
273
274 #define SET_RX_CHANNEL_FLAG(channel, param, value)                           \
275         ({                                                                   \
276                 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \
277                 if (channel->info_word)                                      \
278                         channel->info_word->rx.param = cpu_to_le32(value);   \
279                 else                                                         \
280                         channel->info->rx.param = value;                     \
281         })
282
283 #define SET_RX_CHANNEL_INFO(channel, param, value)                            \
284         ({                                                                    \
285                 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \
286                 if (channel->info_word)                                       \
287                         channel->info_word->rx.param = cpu_to_le32(value);    \
288                 else                                                          \
289                         channel->info->rx.param = cpu_to_le32(value);         \
290         })
291
292 #define GET_TX_CHANNEL_FLAG(channel, param)                                  \
293         ({                                                                   \
294                 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \
295                 channel->info_word ?                                         \
296                         le32_to_cpu(channel->info_word->tx.param) :          \
297                         channel->info->tx.param;                             \
298         })
299
300 #define GET_TX_CHANNEL_INFO(channel, param)                                   \
301         ({                                                                    \
302                 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \
303                 le32_to_cpu(channel->info_word ?                              \
304                         channel->info_word->tx.param :                        \
305                         channel->info->tx.param);                             \
306         })
307
308 #define SET_TX_CHANNEL_FLAG(channel, param, value)                           \
309         ({                                                                   \
310                 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \
311                 if (channel->info_word)                                      \
312                         channel->info_word->tx.param = cpu_to_le32(value);   \
313                 else                                                         \
314                         channel->info->tx.param = value;                     \
315         })
316
317 #define SET_TX_CHANNEL_INFO(channel, param, value)                            \
318         ({                                                                    \
319                 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \
320                 if (channel->info_word)                                       \
321                         channel->info_word->tx.param = cpu_to_le32(value);   \
322                 else                                                          \
323                         channel->info->tx.param = cpu_to_le32(value);         \
324         })
325
326 /**
327  * struct qcom_smd_alloc_entry - channel allocation entry
328  * @name:       channel name
329  * @cid:        channel index
330  * @flags:      channel flags and edge id
331  * @ref_count:  reference count of the channel
332  */
333 struct qcom_smd_alloc_entry {
334         u8 name[20];
335         __le32 cid;
336         __le32 flags;
337         __le32 ref_count;
338 } __packed;
339
340 #define SMD_CHANNEL_FLAGS_EDGE_MASK     0xff
341 #define SMD_CHANNEL_FLAGS_STREAM        BIT(8)
342 #define SMD_CHANNEL_FLAGS_PACKET        BIT(9)
343
344 /*
345  * Each smd packet contains a 20 byte header, with the first 4 being the length
346  * of the packet.
347  */
348 #define SMD_PACKET_HEADER_LEN   20
349
350 /*
351  * Signal the remote processor associated with 'channel'.
352  */
353 static void qcom_smd_signal_channel(struct qcom_smd_channel *channel)
354 {
355         struct qcom_smd_edge *edge = channel->edge;
356
357         regmap_write(edge->ipc_regmap, edge->ipc_offset, BIT(edge->ipc_bit));
358 }
359
360 /*
361  * Initialize the tx channel info
362  */
363 static void qcom_smd_channel_reset(struct qcom_smd_channel *channel)
364 {
365         SET_TX_CHANNEL_INFO(channel, state, SMD_CHANNEL_CLOSED);
366         SET_TX_CHANNEL_FLAG(channel, fDSR, 0);
367         SET_TX_CHANNEL_FLAG(channel, fCTS, 0);
368         SET_TX_CHANNEL_FLAG(channel, fCD, 0);
369         SET_TX_CHANNEL_FLAG(channel, fRI, 0);
370         SET_TX_CHANNEL_FLAG(channel, fHEAD, 0);
371         SET_TX_CHANNEL_FLAG(channel, fTAIL, 0);
372         SET_TX_CHANNEL_FLAG(channel, fSTATE, 1);
373         SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1);
374         SET_TX_CHANNEL_INFO(channel, head, 0);
375         SET_TX_CHANNEL_INFO(channel, tail, 0);
376
377         qcom_smd_signal_channel(channel);
378
379         channel->state = SMD_CHANNEL_CLOSED;
380         channel->pkt_size = 0;
381 }
382
383 /*
384  * Set the callback for a channel, with appropriate locking
385  */
386 static void qcom_smd_channel_set_callback(struct qcom_smd_channel *channel,
387                                           qcom_smd_cb_t cb)
388 {
389         unsigned long flags;
390
391         spin_lock_irqsave(&channel->recv_lock, flags);
392         channel->cb = cb;
393         spin_unlock_irqrestore(&channel->recv_lock, flags);
394 };
395
396 /*
397  * Calculate the amount of data available in the rx fifo
398  */
399 static size_t qcom_smd_channel_get_rx_avail(struct qcom_smd_channel *channel)
400 {
401         unsigned head;
402         unsigned tail;
403
404         head = GET_RX_CHANNEL_INFO(channel, head);
405         tail = GET_RX_CHANNEL_INFO(channel, tail);
406
407         return (head - tail) & (channel->fifo_size - 1);
408 }
409
410 /*
411  * Set tx channel state and inform the remote processor
412  */
413 static void qcom_smd_channel_set_state(struct qcom_smd_channel *channel,
414                                        int state)
415 {
416         struct qcom_smd_edge *edge = channel->edge;
417         bool is_open = state == SMD_CHANNEL_OPENED;
418
419         if (channel->state == state)
420                 return;
421
422         dev_dbg(edge->smd->dev, "set_state(%s, %d)\n", channel->name, state);
423
424         SET_TX_CHANNEL_FLAG(channel, fDSR, is_open);
425         SET_TX_CHANNEL_FLAG(channel, fCTS, is_open);
426         SET_TX_CHANNEL_FLAG(channel, fCD, is_open);
427
428         SET_TX_CHANNEL_INFO(channel, state, state);
429         SET_TX_CHANNEL_FLAG(channel, fSTATE, 1);
430
431         channel->state = state;
432         qcom_smd_signal_channel(channel);
433 }
434
435 /*
436  * Copy count bytes of data using 32bit accesses, if that's required.
437  */
438 static void smd_copy_to_fifo(void __iomem *dst,
439                              const void *src,
440                              size_t count,
441                              bool word_aligned)
442 {
443         if (word_aligned) {
444                 __iowrite32_copy(dst, src, count / sizeof(u32));
445         } else {
446                 memcpy_toio(dst, src, count);
447         }
448 }
449
450 /*
451  * Copy count bytes of data using 32bit accesses, if that is required.
452  */
453 static void smd_copy_from_fifo(void *dst,
454                                const void __iomem *src,
455                                size_t count,
456                                bool word_aligned)
457 {
458         if (word_aligned) {
459                 __ioread32_copy(dst, src, count / sizeof(u32));
460         } else {
461                 memcpy_fromio(dst, src, count);
462         }
463 }
464
465 /*
466  * Read count bytes of data from the rx fifo into buf, but don't advance the
467  * tail.
468  */
469 static size_t qcom_smd_channel_peek(struct qcom_smd_channel *channel,
470                                     void *buf, size_t count)
471 {
472         bool word_aligned;
473         unsigned tail;
474         size_t len;
475
476         word_aligned = channel->info_word;
477         tail = GET_RX_CHANNEL_INFO(channel, tail);
478
479         len = min_t(size_t, count, channel->fifo_size - tail);
480         if (len) {
481                 smd_copy_from_fifo(buf,
482                                    channel->rx_fifo + tail,
483                                    len,
484                                    word_aligned);
485         }
486
487         if (len != count) {
488                 smd_copy_from_fifo(buf + len,
489                                    channel->rx_fifo,
490                                    count - len,
491                                    word_aligned);
492         }
493
494         return count;
495 }
496
497 /*
498  * Advance the rx tail by count bytes.
499  */
500 static void qcom_smd_channel_advance(struct qcom_smd_channel *channel,
501                                      size_t count)
502 {
503         unsigned tail;
504
505         tail = GET_RX_CHANNEL_INFO(channel, tail);
506         tail += count;
507         tail &= (channel->fifo_size - 1);
508         SET_RX_CHANNEL_INFO(channel, tail, tail);
509 }
510
511 /*
512  * Read out a single packet from the rx fifo and deliver it to the device
513  */
514 static int qcom_smd_channel_recv_single(struct qcom_smd_channel *channel)
515 {
516         struct qcom_smd_device *qsdev = channel->qsdev;
517         unsigned tail;
518         size_t len;
519         void *ptr;
520         int ret;
521
522         if (!channel->cb)
523                 return 0;
524
525         tail = GET_RX_CHANNEL_INFO(channel, tail);
526
527         /* Use bounce buffer if the data wraps */
528         if (tail + channel->pkt_size >= channel->fifo_size) {
529                 ptr = channel->bounce_buffer;
530                 len = qcom_smd_channel_peek(channel, ptr, channel->pkt_size);
531         } else {
532                 ptr = channel->rx_fifo + tail;
533                 len = channel->pkt_size;
534         }
535
536         ret = channel->cb(qsdev, ptr, len);
537         if (ret < 0)
538                 return ret;
539
540         /* Only forward the tail if the client consumed the data */
541         qcom_smd_channel_advance(channel, len);
542
543         channel->pkt_size = 0;
544
545         return 0;
546 }
547
548 /*
549  * Per channel interrupt handling
550  */
551 static bool qcom_smd_channel_intr(struct qcom_smd_channel *channel)
552 {
553         bool need_state_scan = false;
554         int remote_state;
555         __le32 pktlen;
556         int avail;
557         int ret;
558
559         /* Handle state changes */
560         remote_state = GET_RX_CHANNEL_INFO(channel, state);
561         if (remote_state != channel->remote_state) {
562                 channel->remote_state = remote_state;
563                 need_state_scan = true;
564         }
565         /* Indicate that we have seen any state change */
566         SET_RX_CHANNEL_FLAG(channel, fSTATE, 0);
567
568         /* Signal waiting qcom_smd_send() about the interrupt */
569         if (!GET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR))
570                 wake_up_interruptible(&channel->fblockread_event);
571
572         /* Don't consume any data until we've opened the channel */
573         if (channel->state != SMD_CHANNEL_OPENED)
574                 goto out;
575
576         /* Indicate that we've seen the new data */
577         SET_RX_CHANNEL_FLAG(channel, fHEAD, 0);
578
579         /* Consume data */
580         for (;;) {
581                 avail = qcom_smd_channel_get_rx_avail(channel);
582
583                 if (!channel->pkt_size && avail >= SMD_PACKET_HEADER_LEN) {
584                         qcom_smd_channel_peek(channel, &pktlen, sizeof(pktlen));
585                         qcom_smd_channel_advance(channel, SMD_PACKET_HEADER_LEN);
586                         channel->pkt_size = le32_to_cpu(pktlen);
587                 } else if (channel->pkt_size && avail >= channel->pkt_size) {
588                         ret = qcom_smd_channel_recv_single(channel);
589                         if (ret)
590                                 break;
591                 } else {
592                         break;
593                 }
594         }
595
596         /* Indicate that we have seen and updated tail */
597         SET_RX_CHANNEL_FLAG(channel, fTAIL, 1);
598
599         /* Signal the remote that we've consumed the data (if requested) */
600         if (!GET_RX_CHANNEL_FLAG(channel, fBLOCKREADINTR)) {
601                 /* Ensure ordering of channel info updates */
602                 wmb();
603
604                 qcom_smd_signal_channel(channel);
605         }
606
607 out:
608         return need_state_scan;
609 }
610
611 /*
612  * The edge interrupts are triggered by the remote processor on state changes,
613  * channel info updates or when new channels are created.
614  */
615 static irqreturn_t qcom_smd_edge_intr(int irq, void *data)
616 {
617         struct qcom_smd_edge *edge = data;
618         struct qcom_smd_channel *channel;
619         unsigned available;
620         bool kick_worker = false;
621
622         /*
623          * Handle state changes or data on each of the channels on this edge
624          */
625         read_lock(&edge->channels_lock);
626         list_for_each_entry(channel, &edge->channels, list) {
627                 spin_lock(&channel->recv_lock);
628                 kick_worker |= qcom_smd_channel_intr(channel);
629                 spin_unlock(&channel->recv_lock);
630         }
631         read_unlock(&edge->channels_lock);
632
633         /*
634          * Creating a new channel requires allocating an smem entry, so we only
635          * have to scan if the amount of available space in smem have changed
636          * since last scan.
637          */
638         available = qcom_smem_get_free_space(edge->remote_pid);
639         if (available != edge->smem_available) {
640                 edge->smem_available = available;
641                 kick_worker = true;
642         }
643
644         if (kick_worker)
645                 schedule_work(&edge->scan_work);
646
647         return IRQ_HANDLED;
648 }
649
650 /*
651  * Delivers any outstanding packets in the rx fifo, can be used after probe of
652  * the clients to deliver any packets that wasn't delivered before the client
653  * was setup.
654  */
655 static void qcom_smd_channel_resume(struct qcom_smd_channel *channel)
656 {
657         unsigned long flags;
658
659         spin_lock_irqsave(&channel->recv_lock, flags);
660         qcom_smd_channel_intr(channel);
661         spin_unlock_irqrestore(&channel->recv_lock, flags);
662 }
663
664 /*
665  * Calculate how much space is available in the tx fifo.
666  */
667 static size_t qcom_smd_get_tx_avail(struct qcom_smd_channel *channel)
668 {
669         unsigned head;
670         unsigned tail;
671         unsigned mask = channel->fifo_size - 1;
672
673         head = GET_TX_CHANNEL_INFO(channel, head);
674         tail = GET_TX_CHANNEL_INFO(channel, tail);
675
676         return mask - ((head - tail) & mask);
677 }
678
679 /*
680  * Write count bytes of data into channel, possibly wrapping in the ring buffer
681  */
682 static int qcom_smd_write_fifo(struct qcom_smd_channel *channel,
683                                const void *data,
684                                size_t count)
685 {
686         bool word_aligned;
687         unsigned head;
688         size_t len;
689
690         word_aligned = channel->info_word;
691         head = GET_TX_CHANNEL_INFO(channel, head);
692
693         len = min_t(size_t, count, channel->fifo_size - head);
694         if (len) {
695                 smd_copy_to_fifo(channel->tx_fifo + head,
696                                  data,
697                                  len,
698                                  word_aligned);
699         }
700
701         if (len != count) {
702                 smd_copy_to_fifo(channel->tx_fifo,
703                                  data + len,
704                                  count - len,
705                                  word_aligned);
706         }
707
708         head += count;
709         head &= (channel->fifo_size - 1);
710         SET_TX_CHANNEL_INFO(channel, head, head);
711
712         return count;
713 }
714
715 /**
716  * qcom_smd_send - write data to smd channel
717  * @channel:    channel handle
718  * @data:       buffer of data to write
719  * @len:        number of bytes to write
720  *
721  * This is a blocking write of len bytes into the channel's tx ring buffer and
722  * signal the remote end. It will sleep until there is enough space available
723  * in the tx buffer, utilizing the fBLOCKREADINTR signaling mechanism to avoid
724  * polling.
725  */
726 int qcom_smd_send(struct qcom_smd_channel *channel, const void *data, int len)
727 {
728         __le32 hdr[5] = { cpu_to_le32(len), };
729         int tlen = sizeof(hdr) + len;
730         int ret;
731
732         /* Word aligned channels only accept word size aligned data */
733         if (channel->info_word && len % 4)
734                 return -EINVAL;
735
736         /* Reject packets that are too big */
737         if (tlen >= channel->fifo_size)
738                 return -EINVAL;
739
740         ret = mutex_lock_interruptible(&channel->tx_lock);
741         if (ret)
742                 return ret;
743
744         while (qcom_smd_get_tx_avail(channel) < tlen) {
745                 if (channel->state != SMD_CHANNEL_OPENED) {
746                         ret = -EPIPE;
747                         goto out;
748                 }
749
750                 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 0);
751
752                 ret = wait_event_interruptible(channel->fblockread_event,
753                                        qcom_smd_get_tx_avail(channel) >= tlen ||
754                                        channel->state != SMD_CHANNEL_OPENED);
755                 if (ret)
756                         goto out;
757
758                 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1);
759         }
760
761         SET_TX_CHANNEL_FLAG(channel, fTAIL, 0);
762
763         qcom_smd_write_fifo(channel, hdr, sizeof(hdr));
764         qcom_smd_write_fifo(channel, data, len);
765
766         SET_TX_CHANNEL_FLAG(channel, fHEAD, 1);
767
768         /* Ensure ordering of channel info updates */
769         wmb();
770
771         qcom_smd_signal_channel(channel);
772
773 out:
774         mutex_unlock(&channel->tx_lock);
775
776         return ret;
777 }
778 EXPORT_SYMBOL(qcom_smd_send);
779
780 static struct qcom_smd_device *to_smd_device(struct device *dev)
781 {
782         return container_of(dev, struct qcom_smd_device, dev);
783 }
784
785 static struct qcom_smd_driver *to_smd_driver(struct device *dev)
786 {
787         struct qcom_smd_device *qsdev = to_smd_device(dev);
788
789         return container_of(qsdev->dev.driver, struct qcom_smd_driver, driver);
790 }
791
792 static int qcom_smd_dev_match(struct device *dev, struct device_driver *drv)
793 {
794         struct qcom_smd_device *qsdev = to_smd_device(dev);
795         struct qcom_smd_driver *qsdrv = container_of(drv, struct qcom_smd_driver, driver);
796         const struct qcom_smd_id *match = qsdrv->smd_match_table;
797         const char *name = qsdev->channel->name;
798
799         if (match) {
800                 while (match->name[0]) {
801                         if (!strcmp(match->name, name))
802                                 return 1;
803                         match++;
804                 }
805         }
806
807         return of_driver_match_device(dev, drv);
808 }
809
810 /*
811  * Helper for opening a channel
812  */
813 static int qcom_smd_channel_open(struct qcom_smd_channel *channel,
814                                  qcom_smd_cb_t cb)
815 {
816         size_t bb_size;
817
818         /*
819          * Packets are maximum 4k, but reduce if the fifo is smaller
820          */
821         bb_size = min(channel->fifo_size, SZ_4K);
822         channel->bounce_buffer = kmalloc(bb_size, GFP_KERNEL);
823         if (!channel->bounce_buffer)
824                 return -ENOMEM;
825
826         qcom_smd_channel_set_callback(channel, cb);
827         qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENING);
828         qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENED);
829
830         return 0;
831 }
832
833 /*
834  * Helper for closing and resetting a channel
835  */
836 static void qcom_smd_channel_close(struct qcom_smd_channel *channel)
837 {
838         qcom_smd_channel_set_callback(channel, NULL);
839
840         kfree(channel->bounce_buffer);
841         channel->bounce_buffer = NULL;
842
843         qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
844         qcom_smd_channel_reset(channel);
845 }
846
847 /*
848  * Probe the smd client.
849  *
850  * The remote side have indicated that it want the channel to be opened, so
851  * complete the state handshake and probe our client driver.
852  */
853 static int qcom_smd_dev_probe(struct device *dev)
854 {
855         struct qcom_smd_device *qsdev = to_smd_device(dev);
856         struct qcom_smd_driver *qsdrv = to_smd_driver(dev);
857         struct qcom_smd_channel *channel = qsdev->channel;
858         int ret;
859
860         ret = qcom_smd_channel_open(channel, qsdrv->callback);
861         if (ret)
862                 return ret;
863
864         ret = qsdrv->probe(qsdev);
865         if (ret)
866                 goto err;
867
868         qcom_smd_channel_resume(channel);
869
870         return 0;
871
872 err:
873         dev_err(&qsdev->dev, "probe failed\n");
874
875         qcom_smd_channel_close(channel);
876         return ret;
877 }
878
879 /*
880  * Remove the smd client.
881  *
882  * The channel is going away, for some reason, so remove the smd client and
883  * reset the channel state.
884  */
885 static int qcom_smd_dev_remove(struct device *dev)
886 {
887         struct qcom_smd_device *qsdev = to_smd_device(dev);
888         struct qcom_smd_driver *qsdrv = to_smd_driver(dev);
889         struct qcom_smd_channel *channel = qsdev->channel;
890         struct qcom_smd_channel *tmp;
891         struct qcom_smd_channel *ch;
892
893         qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSING);
894
895         /*
896          * Make sure we don't race with the code receiving data.
897          */
898         qcom_smd_channel_set_callback(channel, NULL);
899
900         /* Wake up any sleepers in qcom_smd_send() */
901         wake_up_interruptible(&channel->fblockread_event);
902
903         /*
904          * We expect that the client might block in remove() waiting for any
905          * outstanding calls to qcom_smd_send() to wake up and finish.
906          */
907         if (qsdrv->remove)
908                 qsdrv->remove(qsdev);
909
910         /*
911          * The client is now gone, close and release all channels associated
912          * with this sdev
913          */
914         list_for_each_entry_safe(ch, tmp, &channel->dev_list, dev_list) {
915                 qcom_smd_channel_close(ch);
916                 list_del(&ch->dev_list);
917                 ch->qsdev = NULL;
918         }
919
920         return 0;
921 }
922
923 static struct bus_type qcom_smd_bus = {
924         .name = "qcom_smd",
925         .match = qcom_smd_dev_match,
926         .probe = qcom_smd_dev_probe,
927         .remove = qcom_smd_dev_remove,
928 };
929
930 /*
931  * Release function for the qcom_smd_device object.
932  */
933 static void qcom_smd_release_device(struct device *dev)
934 {
935         struct qcom_smd_device *qsdev = to_smd_device(dev);
936
937         kfree(qsdev);
938 }
939
940 /*
941  * Finds the device_node for the smd child interested in this channel.
942  */
943 static struct device_node *qcom_smd_match_channel(struct device_node *edge_node,
944                                                   const char *channel)
945 {
946         struct device_node *child;
947         const char *name;
948         const char *key;
949         int ret;
950
951         for_each_available_child_of_node(edge_node, child) {
952                 key = "qcom,smd-channels";
953                 ret = of_property_read_string(child, key, &name);
954                 if (ret)
955                         continue;
956
957                 if (strcmp(name, channel) == 0)
958                         return child;
959         }
960
961         return NULL;
962 }
963
964 /*
965  * Create a smd client device for channel that is being opened.
966  */
967 static int qcom_smd_create_device(struct qcom_smd_channel *channel)
968 {
969         struct qcom_smd_device *qsdev;
970         struct qcom_smd_edge *edge = channel->edge;
971         struct device_node *node;
972         struct qcom_smd *smd = edge->smd;
973         int ret;
974
975         if (channel->qsdev)
976                 return -EEXIST;
977
978         dev_dbg(smd->dev, "registering '%s'\n", channel->name);
979
980         qsdev = kzalloc(sizeof(*qsdev), GFP_KERNEL);
981         if (!qsdev)
982                 return -ENOMEM;
983
984         node = qcom_smd_match_channel(edge->of_node, channel->name);
985         dev_set_name(&qsdev->dev, "%s.%s",
986                      edge->of_node->name,
987                      node ? node->name : channel->name);
988
989         qsdev->dev.parent = smd->dev;
990         qsdev->dev.bus = &qcom_smd_bus;
991         qsdev->dev.release = qcom_smd_release_device;
992         qsdev->dev.of_node = node;
993
994         qsdev->channel = channel;
995
996         channel->qsdev = qsdev;
997
998         ret = device_register(&qsdev->dev);
999         if (ret) {
1000                 dev_err(smd->dev, "device_register failed: %d\n", ret);
1001                 put_device(&qsdev->dev);
1002         }
1003
1004         return ret;
1005 }
1006
1007 /*
1008  * Destroy a smd client device for a channel that's going away.
1009  */
1010 static void qcom_smd_destroy_device(struct qcom_smd_channel *channel)
1011 {
1012         struct device *dev;
1013
1014         BUG_ON(!channel->qsdev);
1015
1016         dev = &channel->qsdev->dev;
1017
1018         device_unregister(dev);
1019         of_node_put(dev->of_node);
1020         put_device(dev);
1021 }
1022
1023 /**
1024  * qcom_smd_driver_register - register a smd driver
1025  * @qsdrv:      qcom_smd_driver struct
1026  */
1027 int qcom_smd_driver_register(struct qcom_smd_driver *qsdrv)
1028 {
1029         qsdrv->driver.bus = &qcom_smd_bus;
1030         return driver_register(&qsdrv->driver);
1031 }
1032 EXPORT_SYMBOL(qcom_smd_driver_register);
1033
1034 /**
1035  * qcom_smd_driver_unregister - unregister a smd driver
1036  * @qsdrv:      qcom_smd_driver struct
1037  */
1038 void qcom_smd_driver_unregister(struct qcom_smd_driver *qsdrv)
1039 {
1040         driver_unregister(&qsdrv->driver);
1041 }
1042 EXPORT_SYMBOL(qcom_smd_driver_unregister);
1043
1044 static struct qcom_smd_channel *
1045 qcom_smd_find_channel(struct qcom_smd_edge *edge, const char *name)
1046 {
1047         struct qcom_smd_channel *channel;
1048         struct qcom_smd_channel *ret = NULL;
1049         unsigned state;
1050
1051         read_lock(&edge->channels_lock);
1052         list_for_each_entry(channel, &edge->channels, list) {
1053                 if (strcmp(channel->name, name))
1054                         continue;
1055
1056                 state = GET_RX_CHANNEL_INFO(channel, state);
1057                 if (state != SMD_CHANNEL_OPENING &&
1058                     state != SMD_CHANNEL_OPENED)
1059                         continue;
1060
1061                 ret = channel;
1062                 break;
1063         }
1064         read_unlock(&edge->channels_lock);
1065
1066         return ret;
1067 }
1068
1069 /**
1070  * qcom_smd_open_channel() - claim additional channels on the same edge
1071  * @sdev:       smd_device handle
1072  * @name:       channel name
1073  * @cb:         callback method to use for incoming data
1074  *
1075  * Returns a channel handle on success, or -EPROBE_DEFER if the channel isn't
1076  * ready.
1077  */
1078 struct qcom_smd_channel *qcom_smd_open_channel(struct qcom_smd_device *sdev,
1079                                                const char *name,
1080                                                qcom_smd_cb_t cb)
1081 {
1082         struct qcom_smd_channel *channel;
1083         struct qcom_smd_edge *edge = sdev->channel->edge;
1084         int ret;
1085
1086         /* Wait up to HZ for the channel to appear */
1087         ret = wait_event_interruptible_timeout(edge->new_channel_event,
1088                         (channel = qcom_smd_find_channel(edge, name)) != NULL,
1089                         HZ);
1090         if (!ret)
1091                 return ERR_PTR(-ETIMEDOUT);
1092
1093         if (channel->state != SMD_CHANNEL_CLOSED) {
1094                 dev_err(&sdev->dev, "channel %s is busy\n", channel->name);
1095                 return ERR_PTR(-EBUSY);
1096         }
1097
1098         channel->qsdev = sdev;
1099         ret = qcom_smd_channel_open(channel, cb);
1100         if (ret) {
1101                 channel->qsdev = NULL;
1102                 return ERR_PTR(ret);
1103         }
1104
1105         /*
1106          * Append the list of channel to the channels associated with the sdev
1107          */
1108         list_add_tail(&channel->dev_list, &sdev->channel->dev_list);
1109
1110         return channel;
1111 }
1112 EXPORT_SYMBOL(qcom_smd_open_channel);
1113
1114 /*
1115  * Allocate the qcom_smd_channel object for a newly found smd channel,
1116  * retrieving and validating the smem items involved.
1117  */
1118 static struct qcom_smd_channel *qcom_smd_create_channel(struct qcom_smd_edge *edge,
1119                                                         unsigned smem_info_item,
1120                                                         unsigned smem_fifo_item,
1121                                                         char *name)
1122 {
1123         struct qcom_smd_channel *channel;
1124         struct qcom_smd *smd = edge->smd;
1125         size_t fifo_size;
1126         size_t info_size;
1127         void *fifo_base;
1128         void *info;
1129         int ret;
1130
1131         channel = devm_kzalloc(smd->dev, sizeof(*channel), GFP_KERNEL);
1132         if (!channel)
1133                 return ERR_PTR(-ENOMEM);
1134
1135         INIT_LIST_HEAD(&channel->dev_list);
1136         channel->edge = edge;
1137         channel->name = devm_kstrdup(smd->dev, name, GFP_KERNEL);
1138         if (!channel->name)
1139                 return ERR_PTR(-ENOMEM);
1140
1141         mutex_init(&channel->tx_lock);
1142         spin_lock_init(&channel->recv_lock);
1143         init_waitqueue_head(&channel->fblockread_event);
1144
1145         info = qcom_smem_get(edge->remote_pid, smem_info_item, &info_size);
1146         if (IS_ERR(info)) {
1147                 ret = PTR_ERR(info);
1148                 goto free_name_and_channel;
1149         }
1150
1151         /*
1152          * Use the size of the item to figure out which channel info struct to
1153          * use.
1154          */
1155         if (info_size == 2 * sizeof(struct smd_channel_info_word)) {
1156                 channel->info_word = info;
1157         } else if (info_size == 2 * sizeof(struct smd_channel_info)) {
1158                 channel->info = info;
1159         } else {
1160                 dev_err(smd->dev,
1161                         "channel info of size %zu not supported\n", info_size);
1162                 ret = -EINVAL;
1163                 goto free_name_and_channel;
1164         }
1165
1166         fifo_base = qcom_smem_get(edge->remote_pid, smem_fifo_item, &fifo_size);
1167         if (IS_ERR(fifo_base)) {
1168                 ret =  PTR_ERR(fifo_base);
1169                 goto free_name_and_channel;
1170         }
1171
1172         /* The channel consist of a rx and tx fifo of equal size */
1173         fifo_size /= 2;
1174
1175         dev_dbg(smd->dev, "new channel '%s' info-size: %zu fifo-size: %zu\n",
1176                           name, info_size, fifo_size);
1177
1178         channel->tx_fifo = fifo_base;
1179         channel->rx_fifo = fifo_base + fifo_size;
1180         channel->fifo_size = fifo_size;
1181
1182         qcom_smd_channel_reset(channel);
1183
1184         return channel;
1185
1186 free_name_and_channel:
1187         devm_kfree(smd->dev, channel->name);
1188         devm_kfree(smd->dev, channel);
1189
1190         return ERR_PTR(ret);
1191 }
1192
1193 /*
1194  * Scans the allocation table for any newly allocated channels, calls
1195  * qcom_smd_create_channel() to create representations of these and add
1196  * them to the edge's list of channels.
1197  */
1198 static void qcom_channel_scan_worker(struct work_struct *work)
1199 {
1200         struct qcom_smd_edge *edge = container_of(work, struct qcom_smd_edge, scan_work);
1201         struct qcom_smd_alloc_entry *alloc_tbl;
1202         struct qcom_smd_alloc_entry *entry;
1203         struct qcom_smd_channel *channel;
1204         struct qcom_smd *smd = edge->smd;
1205         unsigned long flags;
1206         unsigned fifo_id;
1207         unsigned info_id;
1208         int tbl;
1209         int i;
1210         u32 eflags, cid;
1211
1212         for (tbl = 0; tbl < SMD_ALLOC_TBL_COUNT; tbl++) {
1213                 alloc_tbl = qcom_smem_get(edge->remote_pid,
1214                                     smem_items[tbl].alloc_tbl_id, NULL);
1215                 if (IS_ERR(alloc_tbl))
1216                         continue;
1217
1218                 for (i = 0; i < SMD_ALLOC_TBL_SIZE; i++) {
1219                         entry = &alloc_tbl[i];
1220                         eflags = le32_to_cpu(entry->flags);
1221                         if (test_bit(i, edge->allocated[tbl]))
1222                                 continue;
1223
1224                         if (entry->ref_count == 0)
1225                                 continue;
1226
1227                         if (!entry->name[0])
1228                                 continue;
1229
1230                         if (!(eflags & SMD_CHANNEL_FLAGS_PACKET))
1231                                 continue;
1232
1233                         if ((eflags & SMD_CHANNEL_FLAGS_EDGE_MASK) != edge->edge_id)
1234                                 continue;
1235
1236                         cid = le32_to_cpu(entry->cid);
1237                         info_id = smem_items[tbl].info_base_id + cid;
1238                         fifo_id = smem_items[tbl].fifo_base_id + cid;
1239
1240                         channel = qcom_smd_create_channel(edge, info_id, fifo_id, entry->name);
1241                         if (IS_ERR(channel))
1242                                 continue;
1243
1244                         write_lock_irqsave(&edge->channels_lock, flags);
1245                         list_add(&channel->list, &edge->channels);
1246                         write_unlock_irqrestore(&edge->channels_lock, flags);
1247
1248                         dev_dbg(smd->dev, "new channel found: '%s'\n", channel->name);
1249                         set_bit(i, edge->allocated[tbl]);
1250
1251                         wake_up_interruptible(&edge->new_channel_event);
1252                 }
1253         }
1254
1255         schedule_work(&edge->state_work);
1256 }
1257
1258 /*
1259  * This per edge worker scans smem for any new channels and register these. It
1260  * then scans all registered channels for state changes that should be handled
1261  * by creating or destroying smd client devices for the registered channels.
1262  *
1263  * LOCKING: edge->channels_lock only needs to cover the list operations, as the
1264  * worker is killed before any channels are deallocated
1265  */
1266 static void qcom_channel_state_worker(struct work_struct *work)
1267 {
1268         struct qcom_smd_channel *channel;
1269         struct qcom_smd_edge *edge = container_of(work,
1270                                                   struct qcom_smd_edge,
1271                                                   state_work);
1272         unsigned remote_state;
1273
1274         /*
1275          * Register a device for any closed channel where the remote processor
1276          * is showing interest in opening the channel.
1277          */
1278         read_lock(&edge->channels_lock);
1279         list_for_each_entry(channel, &edge->channels, list) {
1280                 if (channel->state != SMD_CHANNEL_CLOSED)
1281                         continue;
1282
1283                 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1284                 if (remote_state != SMD_CHANNEL_OPENING &&
1285                     remote_state != SMD_CHANNEL_OPENED)
1286                         continue;
1287
1288                 read_unlock(&edge->channels_lock);
1289                 qcom_smd_create_device(channel);
1290                 read_lock(&edge->channels_lock);
1291         }
1292
1293         /*
1294          * Unregister the device for any channel that is opened where the
1295          * remote processor is closing the channel.
1296          */
1297         list_for_each_entry(channel, &edge->channels, list) {
1298                 if (channel->state != SMD_CHANNEL_OPENING &&
1299                     channel->state != SMD_CHANNEL_OPENED)
1300                         continue;
1301
1302                 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1303                 if (remote_state == SMD_CHANNEL_OPENING ||
1304                     remote_state == SMD_CHANNEL_OPENED)
1305                         continue;
1306
1307                 read_unlock(&edge->channels_lock);
1308                 qcom_smd_destroy_device(channel);
1309                 read_lock(&edge->channels_lock);
1310         }
1311         read_unlock(&edge->channels_lock);
1312 }
1313
1314 /*
1315  * Parses an of_node describing an edge.
1316  */
1317 static int qcom_smd_parse_edge(struct device *dev,
1318                                struct device_node *node,
1319                                struct qcom_smd_edge *edge)
1320 {
1321         struct device_node *syscon_np;
1322         const char *key;
1323         int irq;
1324         int ret;
1325
1326         INIT_LIST_HEAD(&edge->channels);
1327         rwlock_init(&edge->channels_lock);
1328
1329         INIT_WORK(&edge->scan_work, qcom_channel_scan_worker);
1330         INIT_WORK(&edge->state_work, qcom_channel_state_worker);
1331
1332         edge->of_node = of_node_get(node);
1333
1334         irq = irq_of_parse_and_map(node, 0);
1335         if (irq < 0) {
1336                 dev_err(dev, "required smd interrupt missing\n");
1337                 return -EINVAL;
1338         }
1339
1340         ret = devm_request_irq(dev, irq,
1341                                qcom_smd_edge_intr, IRQF_TRIGGER_RISING,
1342                                node->name, edge);
1343         if (ret) {
1344                 dev_err(dev, "failed to request smd irq\n");
1345                 return ret;
1346         }
1347
1348         edge->irq = irq;
1349
1350         key = "qcom,smd-edge";
1351         ret = of_property_read_u32(node, key, &edge->edge_id);
1352         if (ret) {
1353                 dev_err(dev, "edge missing %s property\n", key);
1354                 return -EINVAL;
1355         }
1356
1357         edge->remote_pid = QCOM_SMEM_HOST_ANY;
1358         key = "qcom,remote-pid";
1359         of_property_read_u32(node, key, &edge->remote_pid);
1360
1361         syscon_np = of_parse_phandle(node, "qcom,ipc", 0);
1362         if (!syscon_np) {
1363                 dev_err(dev, "no qcom,ipc node\n");
1364                 return -ENODEV;
1365         }
1366
1367         edge->ipc_regmap = syscon_node_to_regmap(syscon_np);
1368         if (IS_ERR(edge->ipc_regmap))
1369                 return PTR_ERR(edge->ipc_regmap);
1370
1371         key = "qcom,ipc";
1372         ret = of_property_read_u32_index(node, key, 1, &edge->ipc_offset);
1373         if (ret < 0) {
1374                 dev_err(dev, "no offset in %s\n", key);
1375                 return -EINVAL;
1376         }
1377
1378         ret = of_property_read_u32_index(node, key, 2, &edge->ipc_bit);
1379         if (ret < 0) {
1380                 dev_err(dev, "no bit in %s\n", key);
1381                 return -EINVAL;
1382         }
1383
1384         return 0;
1385 }
1386
1387 static int qcom_smd_probe(struct platform_device *pdev)
1388 {
1389         struct qcom_smd_edge *edge;
1390         struct device_node *node;
1391         struct qcom_smd *smd;
1392         size_t array_size;
1393         int num_edges;
1394         int ret;
1395         int i = 0;
1396         void *p;
1397
1398         /* Wait for smem */
1399         p = qcom_smem_get(QCOM_SMEM_HOST_ANY, smem_items[0].alloc_tbl_id, NULL);
1400         if (PTR_ERR(p) == -EPROBE_DEFER)
1401                 return PTR_ERR(p);
1402
1403         num_edges = of_get_available_child_count(pdev->dev.of_node);
1404         array_size = sizeof(*smd) + num_edges * sizeof(struct qcom_smd_edge);
1405         smd = devm_kzalloc(&pdev->dev, array_size, GFP_KERNEL);
1406         if (!smd)
1407                 return -ENOMEM;
1408         smd->dev = &pdev->dev;
1409
1410         smd->num_edges = num_edges;
1411         for_each_available_child_of_node(pdev->dev.of_node, node) {
1412                 edge = &smd->edges[i++];
1413                 edge->smd = smd;
1414                 init_waitqueue_head(&edge->new_channel_event);
1415
1416                 ret = qcom_smd_parse_edge(&pdev->dev, node, edge);
1417                 if (ret)
1418                         continue;
1419
1420                 schedule_work(&edge->scan_work);
1421         }
1422
1423         platform_set_drvdata(pdev, smd);
1424
1425         return 0;
1426 }
1427
1428 /*
1429  * Shut down all smd clients by making sure that each edge stops processing
1430  * events and scanning for new channels, then call destroy on the devices.
1431  */
1432 static int qcom_smd_remove(struct platform_device *pdev)
1433 {
1434         struct qcom_smd_channel *channel;
1435         struct qcom_smd_edge *edge;
1436         struct qcom_smd *smd = platform_get_drvdata(pdev);
1437         int i;
1438
1439         for (i = 0; i < smd->num_edges; i++) {
1440                 edge = &smd->edges[i];
1441
1442                 disable_irq(edge->irq);
1443                 cancel_work_sync(&edge->scan_work);
1444                 cancel_work_sync(&edge->state_work);
1445
1446                 /* No need to lock here, because the writer is gone */
1447                 list_for_each_entry(channel, &edge->channels, list) {
1448                         if (!channel->qsdev)
1449                                 continue;
1450
1451                         qcom_smd_destroy_device(channel);
1452                 }
1453         }
1454
1455         return 0;
1456 }
1457
1458 static const struct of_device_id qcom_smd_of_match[] = {
1459         { .compatible = "qcom,smd" },
1460         {}
1461 };
1462 MODULE_DEVICE_TABLE(of, qcom_smd_of_match);
1463
1464 static struct platform_driver qcom_smd_driver = {
1465         .probe = qcom_smd_probe,
1466         .remove = qcom_smd_remove,
1467         .driver = {
1468                 .name = "qcom-smd",
1469                 .of_match_table = qcom_smd_of_match,
1470         },
1471 };
1472
1473 static int __init qcom_smd_init(void)
1474 {
1475         int ret;
1476
1477         ret = bus_register(&qcom_smd_bus);
1478         if (ret) {
1479                 pr_err("failed to register smd bus: %d\n", ret);
1480                 return ret;
1481         }
1482
1483         return platform_driver_register(&qcom_smd_driver);
1484 }
1485 postcore_initcall(qcom_smd_init);
1486
1487 static void __exit qcom_smd_exit(void)
1488 {
1489         platform_driver_unregister(&qcom_smd_driver);
1490         bus_unregister(&qcom_smd_bus);
1491 }
1492 module_exit(qcom_smd_exit);
1493
1494 MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
1495 MODULE_DESCRIPTION("Qualcomm Shared Memory Driver");
1496 MODULE_LICENSE("GPL v2");