smd.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  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. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/mfd/syscon.h>
  17. #include <linux/module.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regmap.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include <linux/soc/qcom/smd.h>
  25. #include <linux/soc/qcom/smem.h>
  26. #include <linux/wait.h>
  27. /*
  28. * The Qualcomm Shared Memory communication solution provides point-to-point
  29. * channels for clients to send and receive streaming or packet based data.
  30. *
  31. * Each channel consists of a control item (channel info) and a ring buffer
  32. * pair. The channel info carry information related to channel state, flow
  33. * control and the offsets within the ring buffer.
  34. *
  35. * All allocated channels are listed in an allocation table, identifying the
  36. * pair of items by name, type and remote processor.
  37. *
  38. * Upon creating a new channel the remote processor allocates channel info and
  39. * ring buffer items from the smem heap and populate the allocation table. An
  40. * interrupt is sent to the other end of the channel and a scan for new
  41. * channels should be done. A channel never goes away, it will only change
  42. * state.
  43. *
  44. * The remote processor signals it intent for bring up the communication
  45. * channel by setting the state of its end of the channel to "opening" and
  46. * sends out an interrupt. We detect this change and register a smd device to
  47. * consume the channel. Upon finding a consumer we finish the handshake and the
  48. * channel is up.
  49. *
  50. * Upon closing a channel, the remote processor will update the state of its
  51. * end of the channel and signal us, we will then unregister any attached
  52. * device and close our end of the channel.
  53. *
  54. * Devices attached to a channel can use the qcom_smd_send function to push
  55. * data to the channel, this is done by copying the data into the tx ring
  56. * buffer, updating the pointers in the channel info and signaling the remote
  57. * processor.
  58. *
  59. * The remote processor does the equivalent when it transfer data and upon
  60. * receiving the interrupt we check the channel info for new data and delivers
  61. * this to the attached device. If the device is not ready to receive the data
  62. * we leave it in the ring buffer for now.
  63. */
  64. struct smd_channel_info;
  65. struct smd_channel_info_pair;
  66. struct smd_channel_info_word;
  67. struct smd_channel_info_word_pair;
  68. #define SMD_ALLOC_TBL_COUNT 2
  69. #define SMD_ALLOC_TBL_SIZE 64
  70. /*
  71. * This lists the various smem heap items relevant for the allocation table and
  72. * smd channel entries.
  73. */
  74. static const struct {
  75. unsigned alloc_tbl_id;
  76. unsigned info_base_id;
  77. unsigned fifo_base_id;
  78. } smem_items[SMD_ALLOC_TBL_COUNT] = {
  79. {
  80. .alloc_tbl_id = 13,
  81. .info_base_id = 14,
  82. .fifo_base_id = 338
  83. },
  84. {
  85. .alloc_tbl_id = 266,
  86. .info_base_id = 138,
  87. .fifo_base_id = 202,
  88. },
  89. };
  90. /**
  91. * struct qcom_smd_edge - representing a remote processor
  92. * @smd: handle to qcom_smd
  93. * @of_node: of_node handle for information related to this edge
  94. * @edge_id: identifier of this edge
  95. * @remote_pid: identifier of remote processor
  96. * @irq: interrupt for signals on this edge
  97. * @ipc_regmap: regmap handle holding the outgoing ipc register
  98. * @ipc_offset: offset within @ipc_regmap of the register for ipc
  99. * @ipc_bit: bit in the register at @ipc_offset of @ipc_regmap
  100. * @channels: list of all channels detected on this edge
  101. * @channels_lock: guard for modifications of @channels
  102. * @allocated: array of bitmaps representing already allocated channels
  103. * @need_rescan: flag that the @work needs to scan smem for new channels
  104. * @smem_available: last available amount of smem triggering a channel scan
  105. * @work: work item for edge house keeping
  106. */
  107. struct qcom_smd_edge {
  108. struct qcom_smd *smd;
  109. struct device_node *of_node;
  110. unsigned edge_id;
  111. unsigned remote_pid;
  112. int irq;
  113. struct regmap *ipc_regmap;
  114. int ipc_offset;
  115. int ipc_bit;
  116. struct list_head channels;
  117. spinlock_t channels_lock;
  118. DECLARE_BITMAP(allocated[SMD_ALLOC_TBL_COUNT], SMD_ALLOC_TBL_SIZE);
  119. bool need_rescan;
  120. unsigned smem_available;
  121. struct work_struct work;
  122. };
  123. /*
  124. * SMD channel states.
  125. */
  126. enum smd_channel_state {
  127. SMD_CHANNEL_CLOSED,
  128. SMD_CHANNEL_OPENING,
  129. SMD_CHANNEL_OPENED,
  130. SMD_CHANNEL_FLUSHING,
  131. SMD_CHANNEL_CLOSING,
  132. SMD_CHANNEL_RESET,
  133. SMD_CHANNEL_RESET_OPENING
  134. };
  135. /**
  136. * struct qcom_smd_channel - smd channel struct
  137. * @edge: qcom_smd_edge this channel is living on
  138. * @qsdev: reference to a associated smd client device
  139. * @name: name of the channel
  140. * @state: local state of the channel
  141. * @remote_state: remote state of the channel
  142. * @info: byte aligned outgoing/incoming channel info
  143. * @info_word: word aligned outgoing/incoming channel info
  144. * @tx_lock: lock to make writes to the channel mutually exclusive
  145. * @fblockread_event: wakeup event tied to tx fBLOCKREADINTR
  146. * @tx_fifo: pointer to the outgoing ring buffer
  147. * @rx_fifo: pointer to the incoming ring buffer
  148. * @fifo_size: size of each ring buffer
  149. * @bounce_buffer: bounce buffer for reading wrapped packets
  150. * @cb: callback function registered for this channel
  151. * @recv_lock: guard for rx info modifications and cb pointer
  152. * @pkt_size: size of the currently handled packet
  153. * @list: lite entry for @channels in qcom_smd_edge
  154. */
  155. struct qcom_smd_channel {
  156. struct qcom_smd_edge *edge;
  157. struct qcom_smd_device *qsdev;
  158. char *name;
  159. enum smd_channel_state state;
  160. enum smd_channel_state remote_state;
  161. struct smd_channel_info_pair *info;
  162. struct smd_channel_info_word_pair *info_word;
  163. struct mutex tx_lock;
  164. wait_queue_head_t fblockread_event;
  165. void *tx_fifo;
  166. void *rx_fifo;
  167. int fifo_size;
  168. void *bounce_buffer;
  169. int (*cb)(struct qcom_smd_device *, const void *, size_t);
  170. spinlock_t recv_lock;
  171. int pkt_size;
  172. struct list_head list;
  173. };
  174. /**
  175. * struct qcom_smd - smd struct
  176. * @dev: device struct
  177. * @num_edges: number of entries in @edges
  178. * @edges: array of edges to be handled
  179. */
  180. struct qcom_smd {
  181. struct device *dev;
  182. unsigned num_edges;
  183. struct qcom_smd_edge edges[0];
  184. };
  185. /*
  186. * Format of the smd_info smem items, for byte aligned channels.
  187. */
  188. struct smd_channel_info {
  189. __le32 state;
  190. u8 fDSR;
  191. u8 fCTS;
  192. u8 fCD;
  193. u8 fRI;
  194. u8 fHEAD;
  195. u8 fTAIL;
  196. u8 fSTATE;
  197. u8 fBLOCKREADINTR;
  198. __le32 tail;
  199. __le32 head;
  200. };
  201. struct smd_channel_info_pair {
  202. struct smd_channel_info tx;
  203. struct smd_channel_info rx;
  204. };
  205. /*
  206. * Format of the smd_info smem items, for word aligned channels.
  207. */
  208. struct smd_channel_info_word {
  209. __le32 state;
  210. __le32 fDSR;
  211. __le32 fCTS;
  212. __le32 fCD;
  213. __le32 fRI;
  214. __le32 fHEAD;
  215. __le32 fTAIL;
  216. __le32 fSTATE;
  217. __le32 fBLOCKREADINTR;
  218. __le32 tail;
  219. __le32 head;
  220. };
  221. struct smd_channel_info_word_pair {
  222. struct smd_channel_info_word tx;
  223. struct smd_channel_info_word rx;
  224. };
  225. #define GET_RX_CHANNEL_FLAG(channel, param) \
  226. ({ \
  227. BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \
  228. channel->info_word ? \
  229. le32_to_cpu(channel->info_word->rx.param) : \
  230. channel->info->rx.param; \
  231. })
  232. #define GET_RX_CHANNEL_INFO(channel, param) \
  233. ({ \
  234. BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \
  235. le32_to_cpu(channel->info_word ? \
  236. channel->info_word->rx.param : \
  237. channel->info->rx.param); \
  238. })
  239. #define SET_RX_CHANNEL_FLAG(channel, param, value) \
  240. ({ \
  241. BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \
  242. if (channel->info_word) \
  243. channel->info_word->rx.param = cpu_to_le32(value); \
  244. else \
  245. channel->info->rx.param = value; \
  246. })
  247. #define SET_RX_CHANNEL_INFO(channel, param, value) \
  248. ({ \
  249. BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \
  250. if (channel->info_word) \
  251. channel->info_word->rx.param = cpu_to_le32(value); \
  252. else \
  253. channel->info->rx.param = cpu_to_le32(value); \
  254. })
  255. #define GET_TX_CHANNEL_FLAG(channel, param) \
  256. ({ \
  257. BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \
  258. channel->info_word ? \
  259. le32_to_cpu(channel->info_word->tx.param) : \
  260. channel->info->tx.param; \
  261. })
  262. #define GET_TX_CHANNEL_INFO(channel, param) \
  263. ({ \
  264. BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \
  265. le32_to_cpu(channel->info_word ? \
  266. channel->info_word->tx.param : \
  267. channel->info->tx.param); \
  268. })
  269. #define SET_TX_CHANNEL_FLAG(channel, param, value) \
  270. ({ \
  271. BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \
  272. if (channel->info_word) \
  273. channel->info_word->tx.param = cpu_to_le32(value); \
  274. else \
  275. channel->info->tx.param = value; \
  276. })
  277. #define SET_TX_CHANNEL_INFO(channel, param, value) \
  278. ({ \
  279. BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \
  280. if (channel->info_word) \
  281. channel->info_word->tx.param = cpu_to_le32(value); \
  282. else \
  283. channel->info->tx.param = cpu_to_le32(value); \
  284. })
  285. /**
  286. * struct qcom_smd_alloc_entry - channel allocation entry
  287. * @name: channel name
  288. * @cid: channel index
  289. * @flags: channel flags and edge id
  290. * @ref_count: reference count of the channel
  291. */
  292. struct qcom_smd_alloc_entry {
  293. u8 name[20];
  294. __le32 cid;
  295. __le32 flags;
  296. __le32 ref_count;
  297. } __packed;
  298. #define SMD_CHANNEL_FLAGS_EDGE_MASK 0xff
  299. #define SMD_CHANNEL_FLAGS_STREAM BIT(8)
  300. #define SMD_CHANNEL_FLAGS_PACKET BIT(9)
  301. /*
  302. * Each smd packet contains a 20 byte header, with the first 4 being the length
  303. * of the packet.
  304. */
  305. #define SMD_PACKET_HEADER_LEN 20
  306. /*
  307. * Signal the remote processor associated with 'channel'.
  308. */
  309. static void qcom_smd_signal_channel(struct qcom_smd_channel *channel)
  310. {
  311. struct qcom_smd_edge *edge = channel->edge;
  312. regmap_write(edge->ipc_regmap, edge->ipc_offset, BIT(edge->ipc_bit));
  313. }
  314. /*
  315. * Initialize the tx channel info
  316. */
  317. static void qcom_smd_channel_reset(struct qcom_smd_channel *channel)
  318. {
  319. SET_TX_CHANNEL_INFO(channel, state, SMD_CHANNEL_CLOSED);
  320. SET_TX_CHANNEL_FLAG(channel, fDSR, 0);
  321. SET_TX_CHANNEL_FLAG(channel, fCTS, 0);
  322. SET_TX_CHANNEL_FLAG(channel, fCD, 0);
  323. SET_TX_CHANNEL_FLAG(channel, fRI, 0);
  324. SET_TX_CHANNEL_FLAG(channel, fHEAD, 0);
  325. SET_TX_CHANNEL_FLAG(channel, fTAIL, 0);
  326. SET_TX_CHANNEL_FLAG(channel, fSTATE, 1);
  327. SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1);
  328. SET_TX_CHANNEL_INFO(channel, head, 0);
  329. SET_TX_CHANNEL_INFO(channel, tail, 0);
  330. qcom_smd_signal_channel(channel);
  331. channel->state = SMD_CHANNEL_CLOSED;
  332. channel->pkt_size = 0;
  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. head = GET_RX_CHANNEL_INFO(channel, head);
  342. tail = GET_RX_CHANNEL_INFO(channel, tail);
  343. return (head - tail) & (channel->fifo_size - 1);
  344. }
  345. /*
  346. * Set tx channel state and inform the remote processor
  347. */
  348. static void qcom_smd_channel_set_state(struct qcom_smd_channel *channel,
  349. int state)
  350. {
  351. struct qcom_smd_edge *edge = channel->edge;
  352. bool is_open = state == SMD_CHANNEL_OPENED;
  353. if (channel->state == state)
  354. return;
  355. dev_dbg(edge->smd->dev, "set_state(%s, %d)\n", channel->name, state);
  356. SET_TX_CHANNEL_FLAG(channel, fDSR, is_open);
  357. SET_TX_CHANNEL_FLAG(channel, fCTS, is_open);
  358. SET_TX_CHANNEL_FLAG(channel, fCD, is_open);
  359. SET_TX_CHANNEL_INFO(channel, state, state);
  360. SET_TX_CHANNEL_FLAG(channel, fSTATE, 1);
  361. channel->state = state;
  362. qcom_smd_signal_channel(channel);
  363. }
  364. /*
  365. * Copy count bytes of data using 32bit accesses, if that's required.
  366. */
  367. static void smd_copy_to_fifo(void __iomem *dst,
  368. const void *src,
  369. size_t count,
  370. bool word_aligned)
  371. {
  372. if (word_aligned) {
  373. __iowrite32_copy(dst, src, count / sizeof(u32));
  374. } else {
  375. memcpy_toio(dst, src, count);
  376. }
  377. }
  378. /*
  379. * Copy count bytes of data using 32bit accesses, if that is required.
  380. */
  381. static void smd_copy_from_fifo(void *dst,
  382. const void __iomem *src,
  383. size_t count,
  384. bool word_aligned)
  385. {
  386. if (word_aligned) {
  387. __ioread32_copy(dst, src, count / sizeof(u32));
  388. } else {
  389. memcpy_fromio(dst, src, count);
  390. }
  391. }
  392. /*
  393. * Read count bytes of data from the rx fifo into buf, but don't advance the
  394. * tail.
  395. */
  396. static size_t qcom_smd_channel_peek(struct qcom_smd_channel *channel,
  397. void *buf, size_t count)
  398. {
  399. bool word_aligned;
  400. unsigned tail;
  401. size_t len;
  402. word_aligned = channel->info_word;
  403. tail = GET_RX_CHANNEL_INFO(channel, tail);
  404. len = min_t(size_t, count, channel->fifo_size - tail);
  405. if (len) {
  406. smd_copy_from_fifo(buf,
  407. channel->rx_fifo + tail,
  408. len,
  409. word_aligned);
  410. }
  411. if (len != count) {
  412. smd_copy_from_fifo(buf + len,
  413. channel->rx_fifo,
  414. count - len,
  415. word_aligned);
  416. }
  417. return count;
  418. }
  419. /*
  420. * Advance the rx tail by count bytes.
  421. */
  422. static void qcom_smd_channel_advance(struct qcom_smd_channel *channel,
  423. size_t count)
  424. {
  425. unsigned tail;
  426. tail = GET_RX_CHANNEL_INFO(channel, tail);
  427. tail += count;
  428. tail &= (channel->fifo_size - 1);
  429. SET_RX_CHANNEL_INFO(channel, tail, tail);
  430. }
  431. /*
  432. * Read out a single packet from the rx fifo and deliver it to the device
  433. */
  434. static int qcom_smd_channel_recv_single(struct qcom_smd_channel *channel)
  435. {
  436. struct qcom_smd_device *qsdev = channel->qsdev;
  437. unsigned tail;
  438. size_t len;
  439. void *ptr;
  440. int ret;
  441. if (!channel->cb)
  442. return 0;
  443. tail = GET_RX_CHANNEL_INFO(channel, tail);
  444. /* Use bounce buffer if the data wraps */
  445. if (tail + channel->pkt_size >= channel->fifo_size) {
  446. ptr = channel->bounce_buffer;
  447. len = qcom_smd_channel_peek(channel, ptr, channel->pkt_size);
  448. } else {
  449. ptr = channel->rx_fifo + tail;
  450. len = channel->pkt_size;
  451. }
  452. ret = channel->cb(qsdev, ptr, len);
  453. if (ret < 0)
  454. return ret;
  455. /* Only forward the tail if the client consumed the data */
  456. qcom_smd_channel_advance(channel, len);
  457. channel->pkt_size = 0;
  458. return 0;
  459. }
  460. /*
  461. * Per channel interrupt handling
  462. */
  463. static bool qcom_smd_channel_intr(struct qcom_smd_channel *channel)
  464. {
  465. bool need_state_scan = false;
  466. int remote_state;
  467. __le32 pktlen;
  468. int avail;
  469. int ret;
  470. /* Handle state changes */
  471. remote_state = GET_RX_CHANNEL_INFO(channel, state);
  472. if (remote_state != channel->remote_state) {
  473. channel->remote_state = remote_state;
  474. need_state_scan = true;
  475. }
  476. /* Indicate that we have seen any state change */
  477. SET_RX_CHANNEL_FLAG(channel, fSTATE, 0);
  478. /* Signal waiting qcom_smd_send() about the interrupt */
  479. if (!GET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR))
  480. wake_up_interruptible(&channel->fblockread_event);
  481. /* Don't consume any data until we've opened the channel */
  482. if (channel->state != SMD_CHANNEL_OPENED)
  483. goto out;
  484. /* Indicate that we've seen the new data */
  485. SET_RX_CHANNEL_FLAG(channel, fHEAD, 0);
  486. /* Consume data */
  487. for (;;) {
  488. avail = qcom_smd_channel_get_rx_avail(channel);
  489. if (!channel->pkt_size && avail >= SMD_PACKET_HEADER_LEN) {
  490. qcom_smd_channel_peek(channel, &pktlen, sizeof(pktlen));
  491. qcom_smd_channel_advance(channel, SMD_PACKET_HEADER_LEN);
  492. channel->pkt_size = le32_to_cpu(pktlen);
  493. } else if (channel->pkt_size && avail >= channel->pkt_size) {
  494. ret = qcom_smd_channel_recv_single(channel);
  495. if (ret)
  496. break;
  497. } else {
  498. break;
  499. }
  500. }
  501. /* Indicate that we have seen and updated tail */
  502. SET_RX_CHANNEL_FLAG(channel, fTAIL, 1);
  503. /* Signal the remote that we've consumed the data (if requested) */
  504. if (!GET_RX_CHANNEL_FLAG(channel, fBLOCKREADINTR)) {
  505. /* Ensure ordering of channel info updates */
  506. wmb();
  507. qcom_smd_signal_channel(channel);
  508. }
  509. out:
  510. return need_state_scan;
  511. }
  512. /*
  513. * The edge interrupts are triggered by the remote processor on state changes,
  514. * channel info updates or when new channels are created.
  515. */
  516. static irqreturn_t qcom_smd_edge_intr(int irq, void *data)
  517. {
  518. struct qcom_smd_edge *edge = data;
  519. struct qcom_smd_channel *channel;
  520. unsigned available;
  521. bool kick_worker = false;
  522. /*
  523. * Handle state changes or data on each of the channels on this edge
  524. */
  525. spin_lock(&edge->channels_lock);
  526. list_for_each_entry(channel, &edge->channels, list) {
  527. spin_lock(&channel->recv_lock);
  528. kick_worker |= qcom_smd_channel_intr(channel);
  529. spin_unlock(&channel->recv_lock);
  530. }
  531. spin_unlock(&edge->channels_lock);
  532. /*
  533. * Creating a new channel requires allocating an smem entry, so we only
  534. * have to scan if the amount of available space in smem have changed
  535. * since last scan.
  536. */
  537. available = qcom_smem_get_free_space(edge->remote_pid);
  538. if (available != edge->smem_available) {
  539. edge->smem_available = available;
  540. edge->need_rescan = true;
  541. kick_worker = true;
  542. }
  543. if (kick_worker)
  544. schedule_work(&edge->work);
  545. return IRQ_HANDLED;
  546. }
  547. /*
  548. * Delivers any outstanding packets in the rx fifo, can be used after probe of
  549. * the clients to deliver any packets that wasn't delivered before the client
  550. * was setup.
  551. */
  552. static void qcom_smd_channel_resume(struct qcom_smd_channel *channel)
  553. {
  554. unsigned long flags;
  555. spin_lock_irqsave(&channel->recv_lock, flags);
  556. qcom_smd_channel_intr(channel);
  557. spin_unlock_irqrestore(&channel->recv_lock, flags);
  558. }
  559. /*
  560. * Calculate how much space is available in the tx fifo.
  561. */
  562. static size_t qcom_smd_get_tx_avail(struct qcom_smd_channel *channel)
  563. {
  564. unsigned head;
  565. unsigned tail;
  566. unsigned mask = channel->fifo_size - 1;
  567. head = GET_TX_CHANNEL_INFO(channel, head);
  568. tail = GET_TX_CHANNEL_INFO(channel, tail);
  569. return mask - ((head - tail) & mask);
  570. }
  571. /*
  572. * Write count bytes of data into channel, possibly wrapping in the ring buffer
  573. */
  574. static int qcom_smd_write_fifo(struct qcom_smd_channel *channel,
  575. const void *data,
  576. size_t count)
  577. {
  578. bool word_aligned;
  579. unsigned head;
  580. size_t len;
  581. word_aligned = channel->info_word;
  582. head = GET_TX_CHANNEL_INFO(channel, head);
  583. len = min_t(size_t, count, channel->fifo_size - head);
  584. if (len) {
  585. smd_copy_to_fifo(channel->tx_fifo + head,
  586. data,
  587. len,
  588. word_aligned);
  589. }
  590. if (len != count) {
  591. smd_copy_to_fifo(channel->tx_fifo,
  592. data + len,
  593. count - len,
  594. word_aligned);
  595. }
  596. head += count;
  597. head &= (channel->fifo_size - 1);
  598. SET_TX_CHANNEL_INFO(channel, head, head);
  599. return count;
  600. }
  601. /**
  602. * qcom_smd_send - write data to smd channel
  603. * @channel: channel handle
  604. * @data: buffer of data to write
  605. * @len: number of bytes to write
  606. *
  607. * This is a blocking write of len bytes into the channel's tx ring buffer and
  608. * signal the remote end. It will sleep until there is enough space available
  609. * in the tx buffer, utilizing the fBLOCKREADINTR signaling mechanism to avoid
  610. * polling.
  611. */
  612. int qcom_smd_send(struct qcom_smd_channel *channel, const void *data, int len)
  613. {
  614. __le32 hdr[5] = { cpu_to_le32(len), };
  615. int tlen = sizeof(hdr) + len;
  616. int ret;
  617. /* Word aligned channels only accept word size aligned data */
  618. if (channel->info_word && len % 4)
  619. return -EINVAL;
  620. /* Reject packets that are too big */
  621. if (tlen >= channel->fifo_size)
  622. return -EINVAL;
  623. ret = mutex_lock_interruptible(&channel->tx_lock);
  624. if (ret)
  625. return ret;
  626. while (qcom_smd_get_tx_avail(channel) < tlen) {
  627. if (channel->state != SMD_CHANNEL_OPENED) {
  628. ret = -EPIPE;
  629. goto out;
  630. }
  631. SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 0);
  632. ret = wait_event_interruptible(channel->fblockread_event,
  633. qcom_smd_get_tx_avail(channel) >= tlen ||
  634. channel->state != SMD_CHANNEL_OPENED);
  635. if (ret)
  636. goto out;
  637. SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1);
  638. }
  639. SET_TX_CHANNEL_FLAG(channel, fTAIL, 0);
  640. qcom_smd_write_fifo(channel, hdr, sizeof(hdr));
  641. qcom_smd_write_fifo(channel, data, len);
  642. SET_TX_CHANNEL_FLAG(channel, fHEAD, 1);
  643. /* Ensure ordering of channel info updates */
  644. wmb();
  645. qcom_smd_signal_channel(channel);
  646. out:
  647. mutex_unlock(&channel->tx_lock);
  648. return ret;
  649. }
  650. EXPORT_SYMBOL(qcom_smd_send);
  651. static struct qcom_smd_device *to_smd_device(struct device *dev)
  652. {
  653. return container_of(dev, struct qcom_smd_device, dev);
  654. }
  655. static struct qcom_smd_driver *to_smd_driver(struct device *dev)
  656. {
  657. struct qcom_smd_device *qsdev = to_smd_device(dev);
  658. return container_of(qsdev->dev.driver, struct qcom_smd_driver, driver);
  659. }
  660. static int qcom_smd_dev_match(struct device *dev, struct device_driver *drv)
  661. {
  662. struct qcom_smd_device *qsdev = to_smd_device(dev);
  663. struct qcom_smd_driver *qsdrv = container_of(drv, struct qcom_smd_driver, driver);
  664. const struct qcom_smd_id *match = qsdrv->smd_match_table;
  665. const char *name = qsdev->channel->name;
  666. if (match) {
  667. while (match->name[0]) {
  668. if (!strcmp(match->name, name))
  669. return 1;
  670. match++;
  671. }
  672. }
  673. return of_driver_match_device(dev, drv);
  674. }
  675. /*
  676. * Probe the smd client.
  677. *
  678. * The remote side have indicated that it want the channel to be opened, so
  679. * complete the state handshake and probe our client driver.
  680. */
  681. static int qcom_smd_dev_probe(struct device *dev)
  682. {
  683. struct qcom_smd_device *qsdev = to_smd_device(dev);
  684. struct qcom_smd_driver *qsdrv = to_smd_driver(dev);
  685. struct qcom_smd_channel *channel = qsdev->channel;
  686. size_t bb_size;
  687. int ret;
  688. /*
  689. * Packets are maximum 4k, but reduce if the fifo is smaller
  690. */
  691. bb_size = min(channel->fifo_size, SZ_4K);
  692. channel->bounce_buffer = kmalloc(bb_size, GFP_KERNEL);
  693. if (!channel->bounce_buffer)
  694. return -ENOMEM;
  695. channel->cb = qsdrv->callback;
  696. qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENING);
  697. qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENED);
  698. ret = qsdrv->probe(qsdev);
  699. if (ret)
  700. goto err;
  701. qcom_smd_channel_resume(channel);
  702. return 0;
  703. err:
  704. dev_err(&qsdev->dev, "probe failed\n");
  705. channel->cb = NULL;
  706. kfree(channel->bounce_buffer);
  707. channel->bounce_buffer = NULL;
  708. qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
  709. return ret;
  710. }
  711. /*
  712. * Remove the smd client.
  713. *
  714. * The channel is going away, for some reason, so remove the smd client and
  715. * reset the channel state.
  716. */
  717. static int qcom_smd_dev_remove(struct device *dev)
  718. {
  719. struct qcom_smd_device *qsdev = to_smd_device(dev);
  720. struct qcom_smd_driver *qsdrv = to_smd_driver(dev);
  721. struct qcom_smd_channel *channel = qsdev->channel;
  722. unsigned long flags;
  723. qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSING);
  724. /*
  725. * Make sure we don't race with the code receiving data.
  726. */
  727. spin_lock_irqsave(&channel->recv_lock, flags);
  728. channel->cb = NULL;
  729. spin_unlock_irqrestore(&channel->recv_lock, flags);
  730. /* Wake up any sleepers in qcom_smd_send() */
  731. wake_up_interruptible(&channel->fblockread_event);
  732. /*
  733. * We expect that the client might block in remove() waiting for any
  734. * outstanding calls to qcom_smd_send() to wake up and finish.
  735. */
  736. if (qsdrv->remove)
  737. qsdrv->remove(qsdev);
  738. /*
  739. * The client is now gone, cleanup and reset the channel state.
  740. */
  741. channel->qsdev = NULL;
  742. kfree(channel->bounce_buffer);
  743. channel->bounce_buffer = NULL;
  744. qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
  745. qcom_smd_channel_reset(channel);
  746. return 0;
  747. }
  748. static struct bus_type qcom_smd_bus = {
  749. .name = "qcom_smd",
  750. .match = qcom_smd_dev_match,
  751. .probe = qcom_smd_dev_probe,
  752. .remove = qcom_smd_dev_remove,
  753. };
  754. /*
  755. * Release function for the qcom_smd_device object.
  756. */
  757. static void qcom_smd_release_device(struct device *dev)
  758. {
  759. struct qcom_smd_device *qsdev = to_smd_device(dev);
  760. kfree(qsdev);
  761. }
  762. /*
  763. * Finds the device_node for the smd child interested in this channel.
  764. */
  765. static struct device_node *qcom_smd_match_channel(struct device_node *edge_node,
  766. const char *channel)
  767. {
  768. struct device_node *child;
  769. const char *name;
  770. const char *key;
  771. int ret;
  772. for_each_available_child_of_node(edge_node, child) {
  773. key = "qcom,smd-channels";
  774. ret = of_property_read_string(child, key, &name);
  775. if (ret)
  776. continue;
  777. if (strcmp(name, channel) == 0)
  778. return child;
  779. }
  780. return NULL;
  781. }
  782. /*
  783. * Create a smd client device for channel that is being opened.
  784. */
  785. static int qcom_smd_create_device(struct qcom_smd_channel *channel)
  786. {
  787. struct qcom_smd_device *qsdev;
  788. struct qcom_smd_edge *edge = channel->edge;
  789. struct device_node *node;
  790. struct qcom_smd *smd = edge->smd;
  791. int ret;
  792. if (channel->qsdev)
  793. return -EEXIST;
  794. dev_dbg(smd->dev, "registering '%s'\n", channel->name);
  795. qsdev = kzalloc(sizeof(*qsdev), GFP_KERNEL);
  796. if (!qsdev)
  797. return -ENOMEM;
  798. node = qcom_smd_match_channel(edge->of_node, channel->name);
  799. dev_set_name(&qsdev->dev, "%s.%s",
  800. edge->of_node->name,
  801. node ? node->name : channel->name);
  802. qsdev->dev.parent = smd->dev;
  803. qsdev->dev.bus = &qcom_smd_bus;
  804. qsdev->dev.release = qcom_smd_release_device;
  805. qsdev->dev.of_node = node;
  806. qsdev->channel = channel;
  807. channel->qsdev = qsdev;
  808. ret = device_register(&qsdev->dev);
  809. if (ret) {
  810. dev_err(smd->dev, "device_register failed: %d\n", ret);
  811. put_device(&qsdev->dev);
  812. }
  813. return ret;
  814. }
  815. /*
  816. * Destroy a smd client device for a channel that's going away.
  817. */
  818. static void qcom_smd_destroy_device(struct qcom_smd_channel *channel)
  819. {
  820. struct device *dev;
  821. BUG_ON(!channel->qsdev);
  822. dev = &channel->qsdev->dev;
  823. device_unregister(dev);
  824. of_node_put(dev->of_node);
  825. put_device(dev);
  826. }
  827. /**
  828. * qcom_smd_driver_register - register a smd driver
  829. * @qsdrv: qcom_smd_driver struct
  830. */
  831. int qcom_smd_driver_register(struct qcom_smd_driver *qsdrv)
  832. {
  833. qsdrv->driver.bus = &qcom_smd_bus;
  834. return driver_register(&qsdrv->driver);
  835. }
  836. EXPORT_SYMBOL(qcom_smd_driver_register);
  837. /**
  838. * qcom_smd_driver_unregister - unregister a smd driver
  839. * @qsdrv: qcom_smd_driver struct
  840. */
  841. void qcom_smd_driver_unregister(struct qcom_smd_driver *qsdrv)
  842. {
  843. driver_unregister(&qsdrv->driver);
  844. }
  845. EXPORT_SYMBOL(qcom_smd_driver_unregister);
  846. /*
  847. * Allocate the qcom_smd_channel object for a newly found smd channel,
  848. * retrieving and validating the smem items involved.
  849. */
  850. static struct qcom_smd_channel *qcom_smd_create_channel(struct qcom_smd_edge *edge,
  851. unsigned smem_info_item,
  852. unsigned smem_fifo_item,
  853. char *name)
  854. {
  855. struct qcom_smd_channel *channel;
  856. struct qcom_smd *smd = edge->smd;
  857. size_t fifo_size;
  858. size_t info_size;
  859. void *fifo_base;
  860. void *info;
  861. int ret;
  862. channel = devm_kzalloc(smd->dev, sizeof(*channel), GFP_KERNEL);
  863. if (!channel)
  864. return ERR_PTR(-ENOMEM);
  865. channel->edge = edge;
  866. channel->name = devm_kstrdup(smd->dev, name, GFP_KERNEL);
  867. if (!channel->name)
  868. return ERR_PTR(-ENOMEM);
  869. mutex_init(&channel->tx_lock);
  870. spin_lock_init(&channel->recv_lock);
  871. init_waitqueue_head(&channel->fblockread_event);
  872. info = qcom_smem_get(edge->remote_pid, smem_info_item, &info_size);
  873. if (IS_ERR(info)) {
  874. ret = PTR_ERR(info);
  875. goto free_name_and_channel;
  876. }
  877. /*
  878. * Use the size of the item to figure out which channel info struct to
  879. * use.
  880. */
  881. if (info_size == 2 * sizeof(struct smd_channel_info_word)) {
  882. channel->info_word = info;
  883. } else if (info_size == 2 * sizeof(struct smd_channel_info)) {
  884. channel->info = info;
  885. } else {
  886. dev_err(smd->dev,
  887. "channel info of size %zu not supported\n", info_size);
  888. ret = -EINVAL;
  889. goto free_name_and_channel;
  890. }
  891. fifo_base = qcom_smem_get(edge->remote_pid, smem_fifo_item, &fifo_size);
  892. if (IS_ERR(fifo_base)) {
  893. ret = PTR_ERR(fifo_base);
  894. goto free_name_and_channel;
  895. }
  896. /* The channel consist of a rx and tx fifo of equal size */
  897. fifo_size /= 2;
  898. dev_dbg(smd->dev, "new channel '%s' info-size: %zu fifo-size: %zu\n",
  899. name, info_size, fifo_size);
  900. channel->tx_fifo = fifo_base;
  901. channel->rx_fifo = fifo_base + fifo_size;
  902. channel->fifo_size = fifo_size;
  903. qcom_smd_channel_reset(channel);
  904. return channel;
  905. free_name_and_channel:
  906. devm_kfree(smd->dev, channel->name);
  907. devm_kfree(smd->dev, channel);
  908. return ERR_PTR(ret);
  909. }
  910. /*
  911. * Scans the allocation table for any newly allocated channels, calls
  912. * qcom_smd_create_channel() to create representations of these and add
  913. * them to the edge's list of channels.
  914. */
  915. static void qcom_discover_channels(struct qcom_smd_edge *edge)
  916. {
  917. struct qcom_smd_alloc_entry *alloc_tbl;
  918. struct qcom_smd_alloc_entry *entry;
  919. struct qcom_smd_channel *channel;
  920. struct qcom_smd *smd = edge->smd;
  921. unsigned long flags;
  922. unsigned fifo_id;
  923. unsigned info_id;
  924. int tbl;
  925. int i;
  926. u32 eflags, cid;
  927. for (tbl = 0; tbl < SMD_ALLOC_TBL_COUNT; tbl++) {
  928. alloc_tbl = qcom_smem_get(edge->remote_pid,
  929. smem_items[tbl].alloc_tbl_id, NULL);
  930. if (IS_ERR(alloc_tbl))
  931. continue;
  932. for (i = 0; i < SMD_ALLOC_TBL_SIZE; i++) {
  933. entry = &alloc_tbl[i];
  934. eflags = le32_to_cpu(entry->flags);
  935. if (test_bit(i, edge->allocated[tbl]))
  936. continue;
  937. if (entry->ref_count == 0)
  938. continue;
  939. if (!entry->name[0])
  940. continue;
  941. if (!(eflags & SMD_CHANNEL_FLAGS_PACKET))
  942. continue;
  943. if ((eflags & SMD_CHANNEL_FLAGS_EDGE_MASK) != edge->edge_id)
  944. continue;
  945. cid = le32_to_cpu(entry->cid);
  946. info_id = smem_items[tbl].info_base_id + cid;
  947. fifo_id = smem_items[tbl].fifo_base_id + cid;
  948. channel = qcom_smd_create_channel(edge, info_id, fifo_id, entry->name);
  949. if (IS_ERR(channel))
  950. continue;
  951. spin_lock_irqsave(&edge->channels_lock, flags);
  952. list_add(&channel->list, &edge->channels);
  953. spin_unlock_irqrestore(&edge->channels_lock, flags);
  954. dev_dbg(smd->dev, "new channel found: '%s'\n", channel->name);
  955. set_bit(i, edge->allocated[tbl]);
  956. }
  957. }
  958. schedule_work(&edge->work);
  959. }
  960. /*
  961. * This per edge worker scans smem for any new channels and register these. It
  962. * then scans all registered channels for state changes that should be handled
  963. * by creating or destroying smd client devices for the registered channels.
  964. *
  965. * LOCKING: edge->channels_lock is not needed to be held during the traversal
  966. * of the channels list as it's done synchronously with the only writer.
  967. */
  968. static void qcom_channel_state_worker(struct work_struct *work)
  969. {
  970. struct qcom_smd_channel *channel;
  971. struct qcom_smd_edge *edge = container_of(work,
  972. struct qcom_smd_edge,
  973. work);
  974. unsigned remote_state;
  975. /*
  976. * Rescan smem if we have reason to belive that there are new channels.
  977. */
  978. if (edge->need_rescan) {
  979. edge->need_rescan = false;
  980. qcom_discover_channels(edge);
  981. }
  982. /*
  983. * Register a device for any closed channel where the remote processor
  984. * is showing interest in opening the channel.
  985. */
  986. list_for_each_entry(channel, &edge->channels, list) {
  987. if (channel->state != SMD_CHANNEL_CLOSED)
  988. continue;
  989. remote_state = GET_RX_CHANNEL_INFO(channel, state);
  990. if (remote_state != SMD_CHANNEL_OPENING &&
  991. remote_state != SMD_CHANNEL_OPENED)
  992. continue;
  993. qcom_smd_create_device(channel);
  994. }
  995. /*
  996. * Unregister the device for any channel that is opened where the
  997. * remote processor is closing the channel.
  998. */
  999. list_for_each_entry(channel, &edge->channels, list) {
  1000. if (channel->state != SMD_CHANNEL_OPENING &&
  1001. channel->state != SMD_CHANNEL_OPENED)
  1002. continue;
  1003. remote_state = GET_RX_CHANNEL_INFO(channel, state);
  1004. if (remote_state == SMD_CHANNEL_OPENING ||
  1005. remote_state == SMD_CHANNEL_OPENED)
  1006. continue;
  1007. qcom_smd_destroy_device(channel);
  1008. }
  1009. }
  1010. /*
  1011. * Parses an of_node describing an edge.
  1012. */
  1013. static int qcom_smd_parse_edge(struct device *dev,
  1014. struct device_node *node,
  1015. struct qcom_smd_edge *edge)
  1016. {
  1017. struct device_node *syscon_np;
  1018. const char *key;
  1019. int irq;
  1020. int ret;
  1021. INIT_LIST_HEAD(&edge->channels);
  1022. spin_lock_init(&edge->channels_lock);
  1023. INIT_WORK(&edge->work, qcom_channel_state_worker);
  1024. edge->of_node = of_node_get(node);
  1025. irq = irq_of_parse_and_map(node, 0);
  1026. if (irq < 0) {
  1027. dev_err(dev, "required smd interrupt missing\n");
  1028. return -EINVAL;
  1029. }
  1030. ret = devm_request_irq(dev, irq,
  1031. qcom_smd_edge_intr, IRQF_TRIGGER_RISING,
  1032. node->name, edge);
  1033. if (ret) {
  1034. dev_err(dev, "failed to request smd irq\n");
  1035. return ret;
  1036. }
  1037. edge->irq = irq;
  1038. key = "qcom,smd-edge";
  1039. ret = of_property_read_u32(node, key, &edge->edge_id);
  1040. if (ret) {
  1041. dev_err(dev, "edge missing %s property\n", key);
  1042. return -EINVAL;
  1043. }
  1044. edge->remote_pid = QCOM_SMEM_HOST_ANY;
  1045. key = "qcom,remote-pid";
  1046. of_property_read_u32(node, key, &edge->remote_pid);
  1047. syscon_np = of_parse_phandle(node, "qcom,ipc", 0);
  1048. if (!syscon_np) {
  1049. dev_err(dev, "no qcom,ipc node\n");
  1050. return -ENODEV;
  1051. }
  1052. edge->ipc_regmap = syscon_node_to_regmap(syscon_np);
  1053. if (IS_ERR(edge->ipc_regmap))
  1054. return PTR_ERR(edge->ipc_regmap);
  1055. key = "qcom,ipc";
  1056. ret = of_property_read_u32_index(node, key, 1, &edge->ipc_offset);
  1057. if (ret < 0) {
  1058. dev_err(dev, "no offset in %s\n", key);
  1059. return -EINVAL;
  1060. }
  1061. ret = of_property_read_u32_index(node, key, 2, &edge->ipc_bit);
  1062. if (ret < 0) {
  1063. dev_err(dev, "no bit in %s\n", key);
  1064. return -EINVAL;
  1065. }
  1066. return 0;
  1067. }
  1068. static int qcom_smd_probe(struct platform_device *pdev)
  1069. {
  1070. struct qcom_smd_edge *edge;
  1071. struct device_node *node;
  1072. struct qcom_smd *smd;
  1073. size_t array_size;
  1074. int num_edges;
  1075. int ret;
  1076. int i = 0;
  1077. void *p;
  1078. /* Wait for smem */
  1079. p = qcom_smem_get(QCOM_SMEM_HOST_ANY, smem_items[0].alloc_tbl_id, NULL);
  1080. if (PTR_ERR(p) == -EPROBE_DEFER)
  1081. return PTR_ERR(p);
  1082. num_edges = of_get_available_child_count(pdev->dev.of_node);
  1083. array_size = sizeof(*smd) + num_edges * sizeof(struct qcom_smd_edge);
  1084. smd = devm_kzalloc(&pdev->dev, array_size, GFP_KERNEL);
  1085. if (!smd)
  1086. return -ENOMEM;
  1087. smd->dev = &pdev->dev;
  1088. smd->num_edges = num_edges;
  1089. for_each_available_child_of_node(pdev->dev.of_node, node) {
  1090. edge = &smd->edges[i++];
  1091. edge->smd = smd;
  1092. ret = qcom_smd_parse_edge(&pdev->dev, node, edge);
  1093. if (ret)
  1094. continue;
  1095. edge->need_rescan = true;
  1096. schedule_work(&edge->work);
  1097. }
  1098. platform_set_drvdata(pdev, smd);
  1099. return 0;
  1100. }
  1101. /*
  1102. * Shut down all smd clients by making sure that each edge stops processing
  1103. * events and scanning for new channels, then call destroy on the devices.
  1104. */
  1105. static int qcom_smd_remove(struct platform_device *pdev)
  1106. {
  1107. struct qcom_smd_channel *channel;
  1108. struct qcom_smd_edge *edge;
  1109. struct qcom_smd *smd = platform_get_drvdata(pdev);
  1110. int i;
  1111. for (i = 0; i < smd->num_edges; i++) {
  1112. edge = &smd->edges[i];
  1113. disable_irq(edge->irq);
  1114. cancel_work_sync(&edge->work);
  1115. list_for_each_entry(channel, &edge->channels, list) {
  1116. if (!channel->qsdev)
  1117. continue;
  1118. qcom_smd_destroy_device(channel);
  1119. }
  1120. }
  1121. return 0;
  1122. }
  1123. static const struct of_device_id qcom_smd_of_match[] = {
  1124. { .compatible = "qcom,smd" },
  1125. {}
  1126. };
  1127. MODULE_DEVICE_TABLE(of, qcom_smd_of_match);
  1128. static struct platform_driver qcom_smd_driver = {
  1129. .probe = qcom_smd_probe,
  1130. .remove = qcom_smd_remove,
  1131. .driver = {
  1132. .name = "qcom-smd",
  1133. .of_match_table = qcom_smd_of_match,
  1134. },
  1135. };
  1136. static int __init qcom_smd_init(void)
  1137. {
  1138. int ret;
  1139. ret = bus_register(&qcom_smd_bus);
  1140. if (ret) {
  1141. pr_err("failed to register smd bus: %d\n", ret);
  1142. return ret;
  1143. }
  1144. return platform_driver_register(&qcom_smd_driver);
  1145. }
  1146. postcore_initcall(qcom_smd_init);
  1147. static void __exit qcom_smd_exit(void)
  1148. {
  1149. platform_driver_unregister(&qcom_smd_driver);
  1150. bus_unregister(&qcom_smd_bus);
  1151. }
  1152. module_exit(qcom_smd_exit);
  1153. MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
  1154. MODULE_DESCRIPTION("Qualcomm Shared Memory Driver");
  1155. MODULE_LICENSE("GPL v2");