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