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