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