smd.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  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. u32 *dst = (u32 *)_dst;
  387. u32 *src = (u32 *)_src;
  388. if (word_aligned) {
  389. count /= sizeof(u32);
  390. while (count--)
  391. *dst++ = __raw_readl(src++);
  392. } else {
  393. memcpy_fromio(_dst, _src, count);
  394. }
  395. }
  396. /*
  397. * Read count bytes of data from the rx fifo into buf, but don't advance the
  398. * tail.
  399. */
  400. static size_t qcom_smd_channel_peek(struct qcom_smd_channel *channel,
  401. void *buf, size_t count)
  402. {
  403. bool word_aligned;
  404. unsigned tail;
  405. size_t len;
  406. word_aligned = channel->info_word;
  407. tail = GET_RX_CHANNEL_INFO(channel, tail);
  408. len = min_t(size_t, count, channel->fifo_size - tail);
  409. if (len) {
  410. smd_copy_from_fifo(buf,
  411. channel->rx_fifo + tail,
  412. len,
  413. word_aligned);
  414. }
  415. if (len != count) {
  416. smd_copy_from_fifo(buf + len,
  417. channel->rx_fifo,
  418. count - len,
  419. word_aligned);
  420. }
  421. return count;
  422. }
  423. /*
  424. * Advance the rx tail by count bytes.
  425. */
  426. static void qcom_smd_channel_advance(struct qcom_smd_channel *channel,
  427. size_t count)
  428. {
  429. unsigned tail;
  430. tail = GET_RX_CHANNEL_INFO(channel, tail);
  431. tail += count;
  432. tail &= (channel->fifo_size - 1);
  433. SET_RX_CHANNEL_INFO(channel, tail, tail);
  434. }
  435. /*
  436. * Read out a single packet from the rx fifo and deliver it to the device
  437. */
  438. static int qcom_smd_channel_recv_single(struct qcom_smd_channel *channel)
  439. {
  440. struct qcom_smd_device *qsdev = channel->qsdev;
  441. unsigned tail;
  442. size_t len;
  443. void *ptr;
  444. int ret;
  445. if (!channel->cb)
  446. return 0;
  447. tail = GET_RX_CHANNEL_INFO(channel, tail);
  448. /* Use bounce buffer if the data wraps */
  449. if (tail + channel->pkt_size >= channel->fifo_size) {
  450. ptr = channel->bounce_buffer;
  451. len = qcom_smd_channel_peek(channel, ptr, channel->pkt_size);
  452. } else {
  453. ptr = channel->rx_fifo + tail;
  454. len = channel->pkt_size;
  455. }
  456. ret = channel->cb(qsdev, ptr, len);
  457. if (ret < 0)
  458. return ret;
  459. /* Only forward the tail if the client consumed the data */
  460. qcom_smd_channel_advance(channel, len);
  461. channel->pkt_size = 0;
  462. return 0;
  463. }
  464. /*
  465. * Per channel interrupt handling
  466. */
  467. static bool qcom_smd_channel_intr(struct qcom_smd_channel *channel)
  468. {
  469. bool need_state_scan = false;
  470. int remote_state;
  471. __le32 pktlen;
  472. int avail;
  473. int ret;
  474. /* Handle state changes */
  475. remote_state = GET_RX_CHANNEL_INFO(channel, state);
  476. if (remote_state != channel->remote_state) {
  477. channel->remote_state = remote_state;
  478. need_state_scan = true;
  479. }
  480. /* Indicate that we have seen any state change */
  481. SET_RX_CHANNEL_FLAG(channel, fSTATE, 0);
  482. /* Signal waiting qcom_smd_send() about the interrupt */
  483. if (!GET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR))
  484. wake_up_interruptible(&channel->fblockread_event);
  485. /* Don't consume any data until we've opened the channel */
  486. if (channel->state != SMD_CHANNEL_OPENED)
  487. goto out;
  488. /* Indicate that we've seen the new data */
  489. SET_RX_CHANNEL_FLAG(channel, fHEAD, 0);
  490. /* Consume data */
  491. for (;;) {
  492. avail = qcom_smd_channel_get_rx_avail(channel);
  493. if (!channel->pkt_size && avail >= SMD_PACKET_HEADER_LEN) {
  494. qcom_smd_channel_peek(channel, &pktlen, sizeof(pktlen));
  495. qcom_smd_channel_advance(channel, SMD_PACKET_HEADER_LEN);
  496. channel->pkt_size = le32_to_cpu(pktlen);
  497. } else if (channel->pkt_size && avail >= channel->pkt_size) {
  498. ret = qcom_smd_channel_recv_single(channel);
  499. if (ret)
  500. break;
  501. } else {
  502. break;
  503. }
  504. }
  505. /* Indicate that we have seen and updated tail */
  506. SET_RX_CHANNEL_FLAG(channel, fTAIL, 1);
  507. /* Signal the remote that we've consumed the data (if requested) */
  508. if (!GET_RX_CHANNEL_FLAG(channel, fBLOCKREADINTR)) {
  509. /* Ensure ordering of channel info updates */
  510. wmb();
  511. qcom_smd_signal_channel(channel);
  512. }
  513. out:
  514. return need_state_scan;
  515. }
  516. /*
  517. * The edge interrupts are triggered by the remote processor on state changes,
  518. * channel info updates or when new channels are created.
  519. */
  520. static irqreturn_t qcom_smd_edge_intr(int irq, void *data)
  521. {
  522. struct qcom_smd_edge *edge = data;
  523. struct qcom_smd_channel *channel;
  524. unsigned available;
  525. bool kick_worker = false;
  526. /*
  527. * Handle state changes or data on each of the channels on this edge
  528. */
  529. spin_lock(&edge->channels_lock);
  530. list_for_each_entry(channel, &edge->channels, list) {
  531. spin_lock(&channel->recv_lock);
  532. kick_worker |= qcom_smd_channel_intr(channel);
  533. spin_unlock(&channel->recv_lock);
  534. }
  535. spin_unlock(&edge->channels_lock);
  536. /*
  537. * Creating a new channel requires allocating an smem entry, so we only
  538. * have to scan if the amount of available space in smem have changed
  539. * since last scan.
  540. */
  541. available = qcom_smem_get_free_space(edge->remote_pid);
  542. if (available != edge->smem_available) {
  543. edge->smem_available = available;
  544. edge->need_rescan = true;
  545. kick_worker = true;
  546. }
  547. if (kick_worker)
  548. schedule_work(&edge->work);
  549. return IRQ_HANDLED;
  550. }
  551. /*
  552. * Delivers any outstanding packets in the rx fifo, can be used after probe of
  553. * the clients to deliver any packets that wasn't delivered before the client
  554. * was setup.
  555. */
  556. static void qcom_smd_channel_resume(struct qcom_smd_channel *channel)
  557. {
  558. unsigned long flags;
  559. spin_lock_irqsave(&channel->recv_lock, flags);
  560. qcom_smd_channel_intr(channel);
  561. spin_unlock_irqrestore(&channel->recv_lock, flags);
  562. }
  563. /*
  564. * Calculate how much space is available in the tx fifo.
  565. */
  566. static size_t qcom_smd_get_tx_avail(struct qcom_smd_channel *channel)
  567. {
  568. unsigned head;
  569. unsigned tail;
  570. unsigned mask = channel->fifo_size - 1;
  571. head = GET_TX_CHANNEL_INFO(channel, head);
  572. tail = GET_TX_CHANNEL_INFO(channel, tail);
  573. return mask - ((head - tail) & mask);
  574. }
  575. /*
  576. * Write count bytes of data into channel, possibly wrapping in the ring buffer
  577. */
  578. static int qcom_smd_write_fifo(struct qcom_smd_channel *channel,
  579. const void *data,
  580. size_t count)
  581. {
  582. bool word_aligned;
  583. unsigned head;
  584. size_t len;
  585. word_aligned = channel->info_word;
  586. head = GET_TX_CHANNEL_INFO(channel, head);
  587. len = min_t(size_t, count, channel->fifo_size - head);
  588. if (len) {
  589. smd_copy_to_fifo(channel->tx_fifo + head,
  590. data,
  591. len,
  592. word_aligned);
  593. }
  594. if (len != count) {
  595. smd_copy_to_fifo(channel->tx_fifo,
  596. data + len,
  597. count - len,
  598. word_aligned);
  599. }
  600. head += count;
  601. head &= (channel->fifo_size - 1);
  602. SET_TX_CHANNEL_INFO(channel, head, head);
  603. return count;
  604. }
  605. /**
  606. * qcom_smd_send - write data to smd channel
  607. * @channel: channel handle
  608. * @data: buffer of data to write
  609. * @len: number of bytes to write
  610. *
  611. * This is a blocking write of len bytes into the channel's tx ring buffer and
  612. * signal the remote end. It will sleep until there is enough space available
  613. * in the tx buffer, utilizing the fBLOCKREADINTR signaling mechanism to avoid
  614. * polling.
  615. */
  616. int qcom_smd_send(struct qcom_smd_channel *channel, const void *data, int len)
  617. {
  618. __le32 hdr[5] = { cpu_to_le32(len), };
  619. int tlen = sizeof(hdr) + len;
  620. int ret;
  621. /* Word aligned channels only accept word size aligned data */
  622. if (channel->info_word && len % 4)
  623. return -EINVAL;
  624. /* Reject packets that are too big */
  625. if (tlen >= channel->fifo_size)
  626. return -EINVAL;
  627. ret = mutex_lock_interruptible(&channel->tx_lock);
  628. if (ret)
  629. return ret;
  630. while (qcom_smd_get_tx_avail(channel) < tlen) {
  631. if (channel->state != SMD_CHANNEL_OPENED) {
  632. ret = -EPIPE;
  633. goto out;
  634. }
  635. SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 0);
  636. ret = wait_event_interruptible(channel->fblockread_event,
  637. qcom_smd_get_tx_avail(channel) >= tlen ||
  638. channel->state != SMD_CHANNEL_OPENED);
  639. if (ret)
  640. goto out;
  641. SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1);
  642. }
  643. SET_TX_CHANNEL_FLAG(channel, fTAIL, 0);
  644. qcom_smd_write_fifo(channel, hdr, sizeof(hdr));
  645. qcom_smd_write_fifo(channel, data, len);
  646. SET_TX_CHANNEL_FLAG(channel, fHEAD, 1);
  647. /* Ensure ordering of channel info updates */
  648. wmb();
  649. qcom_smd_signal_channel(channel);
  650. out:
  651. mutex_unlock(&channel->tx_lock);
  652. return ret;
  653. }
  654. EXPORT_SYMBOL(qcom_smd_send);
  655. static struct qcom_smd_device *to_smd_device(struct device *dev)
  656. {
  657. return container_of(dev, struct qcom_smd_device, dev);
  658. }
  659. static struct qcom_smd_driver *to_smd_driver(struct device *dev)
  660. {
  661. struct qcom_smd_device *qsdev = to_smd_device(dev);
  662. return container_of(qsdev->dev.driver, struct qcom_smd_driver, driver);
  663. }
  664. static int qcom_smd_dev_match(struct device *dev, struct device_driver *drv)
  665. {
  666. struct qcom_smd_device *qsdev = to_smd_device(dev);
  667. struct qcom_smd_driver *qsdrv = container_of(drv, struct qcom_smd_driver, driver);
  668. const struct qcom_smd_id *match = qsdrv->smd_match_table;
  669. const char *name = qsdev->channel->name;
  670. if (match) {
  671. while (match->name[0]) {
  672. if (!strcmp(match->name, name))
  673. return 1;
  674. match++;
  675. }
  676. }
  677. return of_driver_match_device(dev, drv);
  678. }
  679. /*
  680. * Probe the smd client.
  681. *
  682. * The remote side have indicated that it want the channel to be opened, so
  683. * complete the state handshake and probe our client driver.
  684. */
  685. static int qcom_smd_dev_probe(struct device *dev)
  686. {
  687. struct qcom_smd_device *qsdev = to_smd_device(dev);
  688. struct qcom_smd_driver *qsdrv = to_smd_driver(dev);
  689. struct qcom_smd_channel *channel = qsdev->channel;
  690. size_t bb_size;
  691. int ret;
  692. /*
  693. * Packets are maximum 4k, but reduce if the fifo is smaller
  694. */
  695. bb_size = min(channel->fifo_size, SZ_4K);
  696. channel->bounce_buffer = kmalloc(bb_size, GFP_KERNEL);
  697. if (!channel->bounce_buffer)
  698. return -ENOMEM;
  699. channel->cb = qsdrv->callback;
  700. qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENING);
  701. qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENED);
  702. ret = qsdrv->probe(qsdev);
  703. if (ret)
  704. goto err;
  705. qcom_smd_channel_resume(channel);
  706. return 0;
  707. err:
  708. dev_err(&qsdev->dev, "probe failed\n");
  709. channel->cb = NULL;
  710. kfree(channel->bounce_buffer);
  711. channel->bounce_buffer = NULL;
  712. qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
  713. return ret;
  714. }
  715. /*
  716. * Remove the smd client.
  717. *
  718. * The channel is going away, for some reason, so remove the smd client and
  719. * reset the channel state.
  720. */
  721. static int qcom_smd_dev_remove(struct device *dev)
  722. {
  723. struct qcom_smd_device *qsdev = to_smd_device(dev);
  724. struct qcom_smd_driver *qsdrv = to_smd_driver(dev);
  725. struct qcom_smd_channel *channel = qsdev->channel;
  726. unsigned long flags;
  727. qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSING);
  728. /*
  729. * Make sure we don't race with the code receiving data.
  730. */
  731. spin_lock_irqsave(&channel->recv_lock, flags);
  732. channel->cb = NULL;
  733. spin_unlock_irqrestore(&channel->recv_lock, flags);
  734. /* Wake up any sleepers in qcom_smd_send() */
  735. wake_up_interruptible(&channel->fblockread_event);
  736. /*
  737. * We expect that the client might block in remove() waiting for any
  738. * outstanding calls to qcom_smd_send() to wake up and finish.
  739. */
  740. if (qsdrv->remove)
  741. qsdrv->remove(qsdev);
  742. /*
  743. * The client is now gone, cleanup and reset the channel state.
  744. */
  745. channel->qsdev = NULL;
  746. kfree(channel->bounce_buffer);
  747. channel->bounce_buffer = NULL;
  748. qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
  749. qcom_smd_channel_reset(channel);
  750. return 0;
  751. }
  752. static struct bus_type qcom_smd_bus = {
  753. .name = "qcom_smd",
  754. .match = qcom_smd_dev_match,
  755. .probe = qcom_smd_dev_probe,
  756. .remove = qcom_smd_dev_remove,
  757. };
  758. /*
  759. * Release function for the qcom_smd_device object.
  760. */
  761. static void qcom_smd_release_device(struct device *dev)
  762. {
  763. struct qcom_smd_device *qsdev = to_smd_device(dev);
  764. kfree(qsdev);
  765. }
  766. /*
  767. * Finds the device_node for the smd child interested in this channel.
  768. */
  769. static struct device_node *qcom_smd_match_channel(struct device_node *edge_node,
  770. const char *channel)
  771. {
  772. struct device_node *child;
  773. const char *name;
  774. const char *key;
  775. int ret;
  776. for_each_available_child_of_node(edge_node, child) {
  777. key = "qcom,smd-channels";
  778. ret = of_property_read_string(child, key, &name);
  779. if (ret)
  780. continue;
  781. if (strcmp(name, channel) == 0)
  782. return child;
  783. }
  784. return NULL;
  785. }
  786. /*
  787. * Create a smd client device for channel that is being opened.
  788. */
  789. static int qcom_smd_create_device(struct qcom_smd_channel *channel)
  790. {
  791. struct qcom_smd_device *qsdev;
  792. struct qcom_smd_edge *edge = channel->edge;
  793. struct device_node *node;
  794. struct qcom_smd *smd = edge->smd;
  795. int ret;
  796. if (channel->qsdev)
  797. return -EEXIST;
  798. dev_dbg(smd->dev, "registering '%s'\n", channel->name);
  799. qsdev = kzalloc(sizeof(*qsdev), GFP_KERNEL);
  800. if (!qsdev)
  801. return -ENOMEM;
  802. node = qcom_smd_match_channel(edge->of_node, channel->name);
  803. dev_set_name(&qsdev->dev, "%s.%s",
  804. edge->of_node->name,
  805. node ? node->name : channel->name);
  806. qsdev->dev.parent = smd->dev;
  807. qsdev->dev.bus = &qcom_smd_bus;
  808. qsdev->dev.release = qcom_smd_release_device;
  809. qsdev->dev.of_node = node;
  810. qsdev->channel = channel;
  811. channel->qsdev = qsdev;
  812. ret = device_register(&qsdev->dev);
  813. if (ret) {
  814. dev_err(smd->dev, "device_register failed: %d\n", ret);
  815. put_device(&qsdev->dev);
  816. }
  817. return ret;
  818. }
  819. /*
  820. * Destroy a smd client device for a channel that's going away.
  821. */
  822. static void qcom_smd_destroy_device(struct qcom_smd_channel *channel)
  823. {
  824. struct device *dev;
  825. BUG_ON(!channel->qsdev);
  826. dev = &channel->qsdev->dev;
  827. device_unregister(dev);
  828. of_node_put(dev->of_node);
  829. put_device(dev);
  830. }
  831. /**
  832. * qcom_smd_driver_register - register a smd driver
  833. * @qsdrv: qcom_smd_driver struct
  834. */
  835. int qcom_smd_driver_register(struct qcom_smd_driver *qsdrv)
  836. {
  837. qsdrv->driver.bus = &qcom_smd_bus;
  838. return driver_register(&qsdrv->driver);
  839. }
  840. EXPORT_SYMBOL(qcom_smd_driver_register);
  841. /**
  842. * qcom_smd_driver_unregister - unregister a smd driver
  843. * @qsdrv: qcom_smd_driver struct
  844. */
  845. void qcom_smd_driver_unregister(struct qcom_smd_driver *qsdrv)
  846. {
  847. driver_unregister(&qsdrv->driver);
  848. }
  849. EXPORT_SYMBOL(qcom_smd_driver_unregister);
  850. /*
  851. * Allocate the qcom_smd_channel object for a newly found smd channel,
  852. * retrieving and validating the smem items involved.
  853. */
  854. static struct qcom_smd_channel *qcom_smd_create_channel(struct qcom_smd_edge *edge,
  855. unsigned smem_info_item,
  856. unsigned smem_fifo_item,
  857. char *name)
  858. {
  859. struct qcom_smd_channel *channel;
  860. struct qcom_smd *smd = edge->smd;
  861. size_t fifo_size;
  862. size_t info_size;
  863. void *fifo_base;
  864. void *info;
  865. int ret;
  866. channel = devm_kzalloc(smd->dev, sizeof(*channel), GFP_KERNEL);
  867. if (!channel)
  868. return ERR_PTR(-ENOMEM);
  869. channel->edge = edge;
  870. channel->name = devm_kstrdup(smd->dev, name, GFP_KERNEL);
  871. if (!channel->name)
  872. return ERR_PTR(-ENOMEM);
  873. mutex_init(&channel->tx_lock);
  874. spin_lock_init(&channel->recv_lock);
  875. init_waitqueue_head(&channel->fblockread_event);
  876. info = qcom_smem_get(edge->remote_pid, smem_info_item, &info_size);
  877. if (IS_ERR(info)) {
  878. ret = PTR_ERR(info);
  879. goto free_name_and_channel;
  880. }
  881. /*
  882. * Use the size of the item to figure out which channel info struct to
  883. * use.
  884. */
  885. if (info_size == 2 * sizeof(struct smd_channel_info_word)) {
  886. channel->info_word = info;
  887. } else if (info_size == 2 * sizeof(struct smd_channel_info)) {
  888. channel->info = info;
  889. } else {
  890. dev_err(smd->dev,
  891. "channel info of size %zu not supported\n", info_size);
  892. ret = -EINVAL;
  893. goto free_name_and_channel;
  894. }
  895. fifo_base = qcom_smem_get(edge->remote_pid, smem_fifo_item, &fifo_size);
  896. if (IS_ERR(fifo_base)) {
  897. ret = PTR_ERR(fifo_base);
  898. goto free_name_and_channel;
  899. }
  900. /* The channel consist of a rx and tx fifo of equal size */
  901. fifo_size /= 2;
  902. dev_dbg(smd->dev, "new channel '%s' info-size: %zu fifo-size: %zu\n",
  903. name, info_size, fifo_size);
  904. channel->tx_fifo = fifo_base;
  905. channel->rx_fifo = fifo_base + fifo_size;
  906. channel->fifo_size = fifo_size;
  907. qcom_smd_channel_reset(channel);
  908. return channel;
  909. free_name_and_channel:
  910. devm_kfree(smd->dev, channel->name);
  911. devm_kfree(smd->dev, channel);
  912. return ERR_PTR(ret);
  913. }
  914. /*
  915. * Scans the allocation table for any newly allocated channels, calls
  916. * qcom_smd_create_channel() to create representations of these and add
  917. * them to the edge's list of channels.
  918. */
  919. static void qcom_discover_channels(struct qcom_smd_edge *edge)
  920. {
  921. struct qcom_smd_alloc_entry *alloc_tbl;
  922. struct qcom_smd_alloc_entry *entry;
  923. struct qcom_smd_channel *channel;
  924. struct qcom_smd *smd = edge->smd;
  925. unsigned long flags;
  926. unsigned fifo_id;
  927. unsigned info_id;
  928. int tbl;
  929. int i;
  930. u32 eflags, cid;
  931. for (tbl = 0; tbl < SMD_ALLOC_TBL_COUNT; tbl++) {
  932. alloc_tbl = qcom_smem_get(edge->remote_pid,
  933. smem_items[tbl].alloc_tbl_id, NULL);
  934. if (IS_ERR(alloc_tbl))
  935. continue;
  936. for (i = 0; i < SMD_ALLOC_TBL_SIZE; i++) {
  937. entry = &alloc_tbl[i];
  938. eflags = le32_to_cpu(entry->flags);
  939. if (test_bit(i, edge->allocated[tbl]))
  940. continue;
  941. if (entry->ref_count == 0)
  942. continue;
  943. if (!entry->name[0])
  944. continue;
  945. if (!(eflags & SMD_CHANNEL_FLAGS_PACKET))
  946. continue;
  947. if ((eflags & SMD_CHANNEL_FLAGS_EDGE_MASK) != edge->edge_id)
  948. continue;
  949. cid = le32_to_cpu(entry->cid);
  950. info_id = smem_items[tbl].info_base_id + cid;
  951. fifo_id = smem_items[tbl].fifo_base_id + cid;
  952. channel = qcom_smd_create_channel(edge, info_id, fifo_id, entry->name);
  953. if (IS_ERR(channel))
  954. continue;
  955. spin_lock_irqsave(&edge->channels_lock, flags);
  956. list_add(&channel->list, &edge->channels);
  957. spin_unlock_irqrestore(&edge->channels_lock, flags);
  958. dev_dbg(smd->dev, "new channel found: '%s'\n", channel->name);
  959. set_bit(i, edge->allocated[tbl]);
  960. }
  961. }
  962. schedule_work(&edge->work);
  963. }
  964. /*
  965. * This per edge worker scans smem for any new channels and register these. It
  966. * then scans all registered channels for state changes that should be handled
  967. * by creating or destroying smd client devices for the registered channels.
  968. *
  969. * LOCKING: edge->channels_lock is not needed to be held during the traversal
  970. * of the channels list as it's done synchronously with the only writer.
  971. */
  972. static void qcom_channel_state_worker(struct work_struct *work)
  973. {
  974. struct qcom_smd_channel *channel;
  975. struct qcom_smd_edge *edge = container_of(work,
  976. struct qcom_smd_edge,
  977. work);
  978. unsigned remote_state;
  979. /*
  980. * Rescan smem if we have reason to belive that there are new channels.
  981. */
  982. if (edge->need_rescan) {
  983. edge->need_rescan = false;
  984. qcom_discover_channels(edge);
  985. }
  986. /*
  987. * Register a device for any closed channel where the remote processor
  988. * is showing interest in opening the channel.
  989. */
  990. list_for_each_entry(channel, &edge->channels, list) {
  991. if (channel->state != SMD_CHANNEL_CLOSED)
  992. continue;
  993. remote_state = GET_RX_CHANNEL_INFO(channel, state);
  994. if (remote_state != SMD_CHANNEL_OPENING &&
  995. remote_state != SMD_CHANNEL_OPENED)
  996. continue;
  997. qcom_smd_create_device(channel);
  998. }
  999. /*
  1000. * Unregister the device for any channel that is opened where the
  1001. * remote processor is closing the channel.
  1002. */
  1003. list_for_each_entry(channel, &edge->channels, list) {
  1004. if (channel->state != SMD_CHANNEL_OPENING &&
  1005. channel->state != SMD_CHANNEL_OPENED)
  1006. continue;
  1007. remote_state = GET_RX_CHANNEL_INFO(channel, state);
  1008. if (remote_state == SMD_CHANNEL_OPENING ||
  1009. remote_state == SMD_CHANNEL_OPENED)
  1010. continue;
  1011. qcom_smd_destroy_device(channel);
  1012. }
  1013. }
  1014. /*
  1015. * Parses an of_node describing an edge.
  1016. */
  1017. static int qcom_smd_parse_edge(struct device *dev,
  1018. struct device_node *node,
  1019. struct qcom_smd_edge *edge)
  1020. {
  1021. struct device_node *syscon_np;
  1022. const char *key;
  1023. int irq;
  1024. int ret;
  1025. INIT_LIST_HEAD(&edge->channels);
  1026. spin_lock_init(&edge->channels_lock);
  1027. INIT_WORK(&edge->work, qcom_channel_state_worker);
  1028. edge->of_node = of_node_get(node);
  1029. irq = irq_of_parse_and_map(node, 0);
  1030. if (irq < 0) {
  1031. dev_err(dev, "required smd interrupt missing\n");
  1032. return -EINVAL;
  1033. }
  1034. ret = devm_request_irq(dev, irq,
  1035. qcom_smd_edge_intr, IRQF_TRIGGER_RISING,
  1036. node->name, edge);
  1037. if (ret) {
  1038. dev_err(dev, "failed to request smd irq\n");
  1039. return ret;
  1040. }
  1041. edge->irq = irq;
  1042. key = "qcom,smd-edge";
  1043. ret = of_property_read_u32(node, key, &edge->edge_id);
  1044. if (ret) {
  1045. dev_err(dev, "edge missing %s property\n", key);
  1046. return -EINVAL;
  1047. }
  1048. edge->remote_pid = QCOM_SMEM_HOST_ANY;
  1049. key = "qcom,remote-pid";
  1050. of_property_read_u32(node, key, &edge->remote_pid);
  1051. syscon_np = of_parse_phandle(node, "qcom,ipc", 0);
  1052. if (!syscon_np) {
  1053. dev_err(dev, "no qcom,ipc node\n");
  1054. return -ENODEV;
  1055. }
  1056. edge->ipc_regmap = syscon_node_to_regmap(syscon_np);
  1057. if (IS_ERR(edge->ipc_regmap))
  1058. return PTR_ERR(edge->ipc_regmap);
  1059. key = "qcom,ipc";
  1060. ret = of_property_read_u32_index(node, key, 1, &edge->ipc_offset);
  1061. if (ret < 0) {
  1062. dev_err(dev, "no offset in %s\n", key);
  1063. return -EINVAL;
  1064. }
  1065. ret = of_property_read_u32_index(node, key, 2, &edge->ipc_bit);
  1066. if (ret < 0) {
  1067. dev_err(dev, "no bit in %s\n", key);
  1068. return -EINVAL;
  1069. }
  1070. return 0;
  1071. }
  1072. static int qcom_smd_probe(struct platform_device *pdev)
  1073. {
  1074. struct qcom_smd_edge *edge;
  1075. struct device_node *node;
  1076. struct qcom_smd *smd;
  1077. size_t array_size;
  1078. int num_edges;
  1079. int ret;
  1080. int i = 0;
  1081. void *p;
  1082. /* Wait for smem */
  1083. p = qcom_smem_get(QCOM_SMEM_HOST_ANY, smem_items[0].alloc_tbl_id, NULL);
  1084. if (PTR_ERR(p) == -EPROBE_DEFER)
  1085. return PTR_ERR(p);
  1086. num_edges = of_get_available_child_count(pdev->dev.of_node);
  1087. array_size = sizeof(*smd) + num_edges * sizeof(struct qcom_smd_edge);
  1088. smd = devm_kzalloc(&pdev->dev, array_size, GFP_KERNEL);
  1089. if (!smd)
  1090. return -ENOMEM;
  1091. smd->dev = &pdev->dev;
  1092. smd->num_edges = num_edges;
  1093. for_each_available_child_of_node(pdev->dev.of_node, node) {
  1094. edge = &smd->edges[i++];
  1095. edge->smd = smd;
  1096. ret = qcom_smd_parse_edge(&pdev->dev, node, edge);
  1097. if (ret)
  1098. continue;
  1099. edge->need_rescan = true;
  1100. schedule_work(&edge->work);
  1101. }
  1102. platform_set_drvdata(pdev, smd);
  1103. return 0;
  1104. }
  1105. /*
  1106. * Shut down all smd clients by making sure that each edge stops processing
  1107. * events and scanning for new channels, then call destroy on the devices.
  1108. */
  1109. static int qcom_smd_remove(struct platform_device *pdev)
  1110. {
  1111. struct qcom_smd_channel *channel;
  1112. struct qcom_smd_edge *edge;
  1113. struct qcom_smd *smd = platform_get_drvdata(pdev);
  1114. int i;
  1115. for (i = 0; i < smd->num_edges; i++) {
  1116. edge = &smd->edges[i];
  1117. disable_irq(edge->irq);
  1118. cancel_work_sync(&edge->work);
  1119. list_for_each_entry(channel, &edge->channels, list) {
  1120. if (!channel->qsdev)
  1121. continue;
  1122. qcom_smd_destroy_device(channel);
  1123. }
  1124. }
  1125. return 0;
  1126. }
  1127. static const struct of_device_id qcom_smd_of_match[] = {
  1128. { .compatible = "qcom,smd" },
  1129. {}
  1130. };
  1131. MODULE_DEVICE_TABLE(of, qcom_smd_of_match);
  1132. static struct platform_driver qcom_smd_driver = {
  1133. .probe = qcom_smd_probe,
  1134. .remove = qcom_smd_remove,
  1135. .driver = {
  1136. .name = "qcom-smd",
  1137. .of_match_table = qcom_smd_of_match,
  1138. },
  1139. };
  1140. static int __init qcom_smd_init(void)
  1141. {
  1142. int ret;
  1143. ret = bus_register(&qcom_smd_bus);
  1144. if (ret) {
  1145. pr_err("failed to register smd bus: %d\n", ret);
  1146. return ret;
  1147. }
  1148. return platform_driver_register(&qcom_smd_driver);
  1149. }
  1150. postcore_initcall(qcom_smd_init);
  1151. static void __exit qcom_smd_exit(void)
  1152. {
  1153. platform_driver_unregister(&qcom_smd_driver);
  1154. bus_unregister(&qcom_smd_bus);
  1155. }
  1156. module_exit(qcom_smd_exit);
  1157. MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
  1158. MODULE_DESCRIPTION("Qualcomm Shared Memory Driver");
  1159. MODULE_LICENSE("GPL v2");