smd_pkt.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. /* Copyright (c) 2008-2014, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. /*
  14. * SMD Packet Driver -- Provides a binary SMD non-muxed packet port
  15. * interface.
  16. */
  17. #include <linux/slab.h>
  18. #include <linux/cdev.h>
  19. #include <linux/module.h>
  20. #include <linux/fs.h>
  21. #include <linux/device.h>
  22. #include <linux/sched.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/mutex.h>
  25. #include <linux/delay.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/completion.h>
  30. #include <linux/msm_smd_pkt.h>
  31. #include <linux/poll.h>
  32. #include <asm/ioctls.h>
  33. #include <linux/pm.h>
  34. #include <mach/msm_smd.h>
  35. #include <mach/subsystem_restart.h>
  36. #include <mach/msm_ipc_logging.h>
  37. #include "smd_private.h"
  38. #ifdef CONFIG_ARCH_FSM9XXX
  39. #define NUM_SMD_PKT_PORTS 4
  40. #else
  41. #define NUM_SMD_PKT_PORTS 31
  42. #endif
  43. #define PDRIVER_NAME_MAX_SIZE 32
  44. #define LOOPBACK_INX (NUM_SMD_PKT_PORTS - 1)
  45. #define DEVICE_NAME "smdpkt"
  46. #define WAKEUPSOURCE_TIMEOUT (2000) /* two seconds */
  47. #define SMD_PKT_MAGIC (0xDEADBAAD)
  48. struct smd_pkt_dev {
  49. struct cdev cdev;
  50. struct device *devicep;
  51. void *pil;
  52. char pdriver_name[PDRIVER_NAME_MAX_SIZE];
  53. int magic;
  54. struct platform_driver driver;
  55. struct smd_channel *ch;
  56. struct mutex ch_lock;
  57. struct mutex rx_lock;
  58. struct mutex tx_lock;
  59. wait_queue_head_t ch_read_wait_queue;
  60. wait_queue_head_t ch_write_wait_queue;
  61. wait_queue_head_t ch_opened_wait_queue;
  62. int i;
  63. int ref_cnt;
  64. int blocking_write;
  65. int is_open;
  66. int poll_mode;
  67. unsigned ch_size;
  68. uint open_modem_wait;
  69. int has_reset;
  70. int do_reset_notification;
  71. struct completion ch_allocated;
  72. struct wakeup_source pa_ws; /* Packet Arrival Wakeup Source */
  73. struct work_struct packet_arrival_work;
  74. struct spinlock pa_spinlock;
  75. int ws_locked;
  76. } *smd_pkt_devp[NUM_SMD_PKT_PORTS];
  77. struct class *smd_pkt_classp;
  78. static dev_t smd_pkt_number;
  79. static struct delayed_work loopback_work;
  80. static void check_and_wakeup_reader(struct smd_pkt_dev *smd_pkt_devp);
  81. static void check_and_wakeup_writer(struct smd_pkt_dev *smd_pkt_devp);
  82. static uint32_t is_modem_smsm_inited(void);
  83. #define SMD_PKT_IPC_LOG_PAGE_CNT 2
  84. static void *smd_pkt_ilctxt;
  85. static int msm_smd_pkt_debug_mask;
  86. module_param_named(debug_mask, msm_smd_pkt_debug_mask,
  87. int, S_IRUGO | S_IWUSR | S_IWGRP);
  88. enum {
  89. SMD_PKT_STATUS = 1U << 0,
  90. SMD_PKT_READ = 1U << 1,
  91. SMD_PKT_WRITE = 1U << 2,
  92. SMD_PKT_POLL = 1U << 5,
  93. };
  94. #define DEBUG
  95. #ifdef DEBUG
  96. #define SMD_PKT_LOG_STRING(x...) \
  97. do { \
  98. if (smd_pkt_ilctxt) \
  99. ipc_log_string(smd_pkt_ilctxt, "<SMD_PKT>: "x); \
  100. } while (0)
  101. #define D_STATUS(x...) \
  102. do { \
  103. if (msm_smd_pkt_debug_mask & SMD_PKT_STATUS) \
  104. pr_info("Status: "x); \
  105. SMD_PKT_LOG_STRING(x); \
  106. } while (0)
  107. #define D_READ(x...) \
  108. do { \
  109. if (msm_smd_pkt_debug_mask & SMD_PKT_READ) \
  110. pr_info("Read: "x); \
  111. SMD_PKT_LOG_STRING(x); \
  112. } while (0)
  113. #define D_WRITE(x...) \
  114. do { \
  115. if (msm_smd_pkt_debug_mask & SMD_PKT_WRITE) \
  116. pr_info("Write: "x); \
  117. SMD_PKT_LOG_STRING(x); \
  118. } while (0)
  119. #define D_POLL(x...) \
  120. do { \
  121. if (msm_smd_pkt_debug_mask & SMD_PKT_POLL) \
  122. pr_info("Poll: "x); \
  123. SMD_PKT_LOG_STRING(x); \
  124. } while (0)
  125. #define E_SMD_PKT_SSR(x) \
  126. do { \
  127. if (x->do_reset_notification) \
  128. pr_err("%s notifying reset for smd_pkt_dev id:%d\n", \
  129. __func__, x->i); \
  130. } while (0)
  131. #else
  132. #define D_STATUS(x...) do {} while (0)
  133. #define D_READ(x...) do {} while (0)
  134. #define D_WRITE(x...) do {} while (0)
  135. #define D_POLL(x...) do {} while (0)
  136. #define E_SMD_PKT_SSR(x) do {} while (0)
  137. #endif
  138. static ssize_t open_timeout_store(struct device *d,
  139. struct device_attribute *attr,
  140. const char *buf,
  141. size_t n)
  142. {
  143. int i;
  144. unsigned long tmp;
  145. for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
  146. if (smd_pkt_devp[i]->devicep == d)
  147. break;
  148. }
  149. if (i >= NUM_SMD_PKT_PORTS) {
  150. pr_err("%s: unable to match device to valid smd_pkt port\n",
  151. __func__);
  152. return -EINVAL;
  153. }
  154. if (!strict_strtoul(buf, 10, &tmp)) {
  155. smd_pkt_devp[i]->open_modem_wait = tmp;
  156. return n;
  157. } else {
  158. pr_err("%s: unable to convert: %s to an int\n", __func__,
  159. buf);
  160. return -EINVAL;
  161. }
  162. }
  163. static ssize_t open_timeout_show(struct device *d,
  164. struct device_attribute *attr,
  165. char *buf)
  166. {
  167. int i;
  168. for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
  169. if (smd_pkt_devp[i]->devicep == d)
  170. break;
  171. }
  172. if (i >= NUM_SMD_PKT_PORTS) {
  173. pr_err("%s: unable to match device to valid smd_pkt port\n",
  174. __func__);
  175. return -EINVAL;
  176. }
  177. return snprintf(buf, PAGE_SIZE, "%d\n",
  178. smd_pkt_devp[i]->open_modem_wait);
  179. }
  180. static DEVICE_ATTR(open_timeout, 0664, open_timeout_show, open_timeout_store);
  181. static int notify_reset(struct smd_pkt_dev *smd_pkt_devp)
  182. {
  183. smd_pkt_devp->do_reset_notification = 0;
  184. return -ENETRESET;
  185. }
  186. static void clean_and_signal(struct smd_pkt_dev *smd_pkt_devp)
  187. {
  188. smd_pkt_devp->do_reset_notification = 1;
  189. smd_pkt_devp->has_reset = 1;
  190. smd_pkt_devp->is_open = 0;
  191. wake_up(&smd_pkt_devp->ch_read_wait_queue);
  192. wake_up(&smd_pkt_devp->ch_write_wait_queue);
  193. wake_up_interruptible(&smd_pkt_devp->ch_opened_wait_queue);
  194. D_STATUS("%s smd_pkt_dev id:%d\n", __func__, smd_pkt_devp->i);
  195. }
  196. static void loopback_probe_worker(struct work_struct *work)
  197. {
  198. /* Wait for the modem SMSM to be inited for the SMD
  199. ** Loopback channel to be allocated at the modem. Since
  200. ** the wait need to be done atmost once, using msleep
  201. ** doesn't degrade the performance. */
  202. if (!is_modem_smsm_inited())
  203. schedule_delayed_work(&loopback_work, msecs_to_jiffies(1000));
  204. else
  205. smsm_change_state(SMSM_APPS_STATE,
  206. 0, SMSM_SMD_LOOPBACK);
  207. }
  208. static void packet_arrival_worker(struct work_struct *work)
  209. {
  210. struct smd_pkt_dev *smd_pkt_devp;
  211. unsigned long flags;
  212. smd_pkt_devp = container_of(work, struct smd_pkt_dev,
  213. packet_arrival_work);
  214. mutex_lock(&smd_pkt_devp->ch_lock);
  215. spin_lock_irqsave(&smd_pkt_devp->pa_spinlock, flags);
  216. if (smd_pkt_devp->ch && smd_pkt_devp->ws_locked) {
  217. D_READ("%s locking smd_pkt_dev id:%d wakeup source\n",
  218. __func__, smd_pkt_devp->i);
  219. /*
  220. * Keep system awake long enough to allow userspace client
  221. * to process the packet.
  222. */
  223. __pm_wakeup_event(&smd_pkt_devp->pa_ws, WAKEUPSOURCE_TIMEOUT);
  224. }
  225. spin_unlock_irqrestore(&smd_pkt_devp->pa_spinlock, flags);
  226. mutex_unlock(&smd_pkt_devp->ch_lock);
  227. }
  228. static long smd_pkt_ioctl(struct file *file, unsigned int cmd,
  229. unsigned long arg)
  230. {
  231. int ret;
  232. struct smd_pkt_dev *smd_pkt_devp;
  233. smd_pkt_devp = file->private_data;
  234. if (!smd_pkt_devp)
  235. return -EINVAL;
  236. mutex_lock(&smd_pkt_devp->ch_lock);
  237. switch (cmd) {
  238. case TIOCMGET:
  239. D_STATUS("%s TIOCMGET command on smd_pkt_dev id:%d\n",
  240. __func__, smd_pkt_devp->i);
  241. ret = smd_tiocmget(smd_pkt_devp->ch);
  242. break;
  243. case TIOCMSET:
  244. D_STATUS("%s TIOCSET command on smd_pkt_dev id:%d\n",
  245. __func__, smd_pkt_devp->i);
  246. ret = smd_tiocmset(smd_pkt_devp->ch, arg, ~arg);
  247. break;
  248. case SMD_PKT_IOCTL_BLOCKING_WRITE:
  249. ret = get_user(smd_pkt_devp->blocking_write, (int *)arg);
  250. break;
  251. default:
  252. pr_err("%s: Unrecognized ioctl command %d\n", __func__, cmd);
  253. ret = -1;
  254. }
  255. mutex_unlock(&smd_pkt_devp->ch_lock);
  256. return ret;
  257. }
  258. ssize_t smd_pkt_read(struct file *file,
  259. char __user *buf,
  260. size_t count,
  261. loff_t *ppos)
  262. {
  263. int r;
  264. int bytes_read;
  265. int pkt_size;
  266. struct smd_pkt_dev *smd_pkt_devp;
  267. unsigned long flags;
  268. smd_pkt_devp = file->private_data;
  269. if (!smd_pkt_devp) {
  270. pr_err("%s on NULL smd_pkt_dev\n", __func__);
  271. return -EINVAL;
  272. }
  273. if (!smd_pkt_devp->ch) {
  274. pr_err("%s on a closed smd_pkt_dev id:%d\n",
  275. __func__, smd_pkt_devp->i);
  276. return -EINVAL;
  277. }
  278. if (smd_pkt_devp->do_reset_notification) {
  279. /* notify client that a reset occurred */
  280. E_SMD_PKT_SSR(smd_pkt_devp);
  281. return notify_reset(smd_pkt_devp);
  282. }
  283. D_READ("Begin %s on smd_pkt_dev id:%d buffer_size %d\n",
  284. __func__, smd_pkt_devp->i, count);
  285. wait_for_packet:
  286. r = wait_event_interruptible(smd_pkt_devp->ch_read_wait_queue,
  287. !smd_pkt_devp->ch ||
  288. (smd_cur_packet_size(smd_pkt_devp->ch) > 0
  289. && smd_read_avail(smd_pkt_devp->ch)) ||
  290. smd_pkt_devp->has_reset);
  291. mutex_lock(&smd_pkt_devp->rx_lock);
  292. if (smd_pkt_devp->has_reset) {
  293. mutex_unlock(&smd_pkt_devp->rx_lock);
  294. E_SMD_PKT_SSR(smd_pkt_devp);
  295. return notify_reset(smd_pkt_devp);
  296. }
  297. if (!smd_pkt_devp->ch) {
  298. mutex_unlock(&smd_pkt_devp->rx_lock);
  299. pr_err("%s on a closed smd_pkt_dev id:%d\n",
  300. __func__, smd_pkt_devp->i);
  301. return -EINVAL;
  302. }
  303. if (r < 0) {
  304. mutex_unlock(&smd_pkt_devp->rx_lock);
  305. /* qualify error message */
  306. if (r != -ERESTARTSYS) {
  307. /* we get this anytime a signal comes in */
  308. pr_err("%s: wait_event_interruptible on smd_pkt_dev"
  309. " id:%d ret %i\n",
  310. __func__, smd_pkt_devp->i, r);
  311. }
  312. return r;
  313. }
  314. /* Here we have a whole packet waiting for us */
  315. pkt_size = smd_cur_packet_size(smd_pkt_devp->ch);
  316. if (!pkt_size) {
  317. pr_err("%s: No data on smd_pkt_dev id:%d, False wakeup\n",
  318. __func__, smd_pkt_devp->i);
  319. mutex_unlock(&smd_pkt_devp->rx_lock);
  320. goto wait_for_packet;
  321. }
  322. if (pkt_size > count) {
  323. pr_err("%s: failure on smd_pkt_dev id: %d - packet size %d"
  324. " > buffer size %d,", __func__, smd_pkt_devp->i,
  325. pkt_size, count);
  326. mutex_unlock(&smd_pkt_devp->rx_lock);
  327. return -ETOOSMALL;
  328. }
  329. bytes_read = 0;
  330. do {
  331. r = smd_read_user_buffer(smd_pkt_devp->ch,
  332. (buf + bytes_read),
  333. (pkt_size - bytes_read));
  334. if (r < 0) {
  335. mutex_unlock(&smd_pkt_devp->rx_lock);
  336. if (smd_pkt_devp->has_reset) {
  337. E_SMD_PKT_SSR(smd_pkt_devp);
  338. return notify_reset(smd_pkt_devp);
  339. }
  340. pr_err("%s Error while reading %d\n", __func__, r);
  341. return r;
  342. }
  343. bytes_read += r;
  344. if (pkt_size != bytes_read)
  345. wait_event(smd_pkt_devp->ch_read_wait_queue,
  346. smd_read_avail(smd_pkt_devp->ch) ||
  347. smd_pkt_devp->has_reset);
  348. if (smd_pkt_devp->has_reset) {
  349. mutex_unlock(&smd_pkt_devp->rx_lock);
  350. E_SMD_PKT_SSR(smd_pkt_devp);
  351. return notify_reset(smd_pkt_devp);
  352. }
  353. } while (pkt_size != bytes_read);
  354. mutex_unlock(&smd_pkt_devp->rx_lock);
  355. mutex_lock(&smd_pkt_devp->ch_lock);
  356. spin_lock_irqsave(&smd_pkt_devp->pa_spinlock, flags);
  357. if (smd_pkt_devp->poll_mode &&
  358. !smd_cur_packet_size(smd_pkt_devp->ch)) {
  359. __pm_relax(&smd_pkt_devp->pa_ws);
  360. smd_pkt_devp->ws_locked = 0;
  361. smd_pkt_devp->poll_mode = 0;
  362. D_READ("%s unlocked smd_pkt_dev id:%d wakeup_source\n",
  363. __func__, smd_pkt_devp->i);
  364. }
  365. spin_unlock_irqrestore(&smd_pkt_devp->pa_spinlock, flags);
  366. mutex_unlock(&smd_pkt_devp->ch_lock);
  367. D_READ("Finished %s on smd_pkt_dev id:%d %d bytes\n",
  368. __func__, smd_pkt_devp->i, bytes_read);
  369. /* check and wakeup read threads waiting on this device */
  370. check_and_wakeup_reader(smd_pkt_devp);
  371. return bytes_read;
  372. }
  373. ssize_t smd_pkt_write(struct file *file,
  374. const char __user *buf,
  375. size_t count,
  376. loff_t *ppos)
  377. {
  378. int r = 0, bytes_written;
  379. struct smd_pkt_dev *smd_pkt_devp;
  380. DEFINE_WAIT(write_wait);
  381. smd_pkt_devp = file->private_data;
  382. if (!smd_pkt_devp) {
  383. pr_err("%s on NULL smd_pkt_dev\n", __func__);
  384. return -EINVAL;
  385. }
  386. if (!smd_pkt_devp->ch) {
  387. pr_err("%s on a closed smd_pkt_dev id:%d\n",
  388. __func__, smd_pkt_devp->i);
  389. return -EINVAL;
  390. }
  391. if (smd_pkt_devp->do_reset_notification || smd_pkt_devp->has_reset) {
  392. E_SMD_PKT_SSR(smd_pkt_devp);
  393. /* notify client that a reset occurred */
  394. return notify_reset(smd_pkt_devp);
  395. }
  396. D_WRITE("Begin %s on smd_pkt_dev id:%d data_size %d\n",
  397. __func__, smd_pkt_devp->i, count);
  398. mutex_lock(&smd_pkt_devp->tx_lock);
  399. if (!smd_pkt_devp->blocking_write) {
  400. if (smd_write_avail(smd_pkt_devp->ch) < count) {
  401. pr_err("%s: Not enough space in smd_pkt_dev id:%d\n",
  402. __func__, smd_pkt_devp->i);
  403. mutex_unlock(&smd_pkt_devp->tx_lock);
  404. return -ENOMEM;
  405. }
  406. }
  407. r = smd_write_start(smd_pkt_devp->ch, count);
  408. if (r < 0) {
  409. mutex_unlock(&smd_pkt_devp->tx_lock);
  410. pr_err("%s: Error:%d in smd_pkt_dev id:%d @ smd_write_start\n",
  411. __func__, r, smd_pkt_devp->i);
  412. return r;
  413. }
  414. bytes_written = 0;
  415. do {
  416. prepare_to_wait(&smd_pkt_devp->ch_write_wait_queue,
  417. &write_wait, TASK_UNINTERRUPTIBLE);
  418. if (!smd_write_segment_avail(smd_pkt_devp->ch) &&
  419. !smd_pkt_devp->has_reset) {
  420. smd_enable_read_intr(smd_pkt_devp->ch);
  421. schedule();
  422. }
  423. finish_wait(&smd_pkt_devp->ch_write_wait_queue, &write_wait);
  424. smd_disable_read_intr(smd_pkt_devp->ch);
  425. if (smd_pkt_devp->has_reset) {
  426. mutex_unlock(&smd_pkt_devp->tx_lock);
  427. E_SMD_PKT_SSR(smd_pkt_devp);
  428. return notify_reset(smd_pkt_devp);
  429. } else {
  430. r = smd_write_segment(smd_pkt_devp->ch,
  431. (void *)(buf + bytes_written),
  432. (count - bytes_written), 1);
  433. if (r < 0) {
  434. mutex_unlock(&smd_pkt_devp->tx_lock);
  435. if (smd_pkt_devp->has_reset) {
  436. E_SMD_PKT_SSR(smd_pkt_devp);
  437. return notify_reset(smd_pkt_devp);
  438. }
  439. pr_err("%s on smd_pkt_dev id:%d failed r:%d\n",
  440. __func__, smd_pkt_devp->i, r);
  441. return r;
  442. }
  443. bytes_written += r;
  444. }
  445. } while (bytes_written != count);
  446. smd_write_end(smd_pkt_devp->ch);
  447. mutex_unlock(&smd_pkt_devp->tx_lock);
  448. D_WRITE("Finished %s on smd_pkt_dev id:%d %d bytes\n",
  449. __func__, smd_pkt_devp->i, count);
  450. return count;
  451. }
  452. static unsigned int smd_pkt_poll(struct file *file, poll_table *wait)
  453. {
  454. struct smd_pkt_dev *smd_pkt_devp;
  455. unsigned int mask = 0;
  456. smd_pkt_devp = file->private_data;
  457. if (!smd_pkt_devp) {
  458. pr_err("%s on a NULL device\n", __func__);
  459. return POLLERR;
  460. }
  461. smd_pkt_devp->poll_mode = 1;
  462. poll_wait(file, &smd_pkt_devp->ch_read_wait_queue, wait);
  463. mutex_lock(&smd_pkt_devp->ch_lock);
  464. if (smd_pkt_devp->has_reset || !smd_pkt_devp->ch) {
  465. mutex_unlock(&smd_pkt_devp->ch_lock);
  466. return POLLERR;
  467. }
  468. if (smd_read_avail(smd_pkt_devp->ch)) {
  469. mask |= POLLIN | POLLRDNORM;
  470. D_POLL("%s sets POLLIN for smd_pkt_dev id: %d\n",
  471. __func__, smd_pkt_devp->i);
  472. }
  473. mutex_unlock(&smd_pkt_devp->ch_lock);
  474. return mask;
  475. }
  476. static void check_and_wakeup_reader(struct smd_pkt_dev *smd_pkt_devp)
  477. {
  478. int sz;
  479. unsigned long flags;
  480. if (!smd_pkt_devp) {
  481. pr_err("%s on a NULL device\n", __func__);
  482. return;
  483. }
  484. if (!smd_pkt_devp->ch) {
  485. pr_err("%s on a closed smd_pkt_dev id:%d\n",
  486. __func__, smd_pkt_devp->i);
  487. return;
  488. }
  489. sz = smd_cur_packet_size(smd_pkt_devp->ch);
  490. if (sz == 0) {
  491. D_READ("%s: No packet in smd_pkt_dev id:%d\n",
  492. __func__, smd_pkt_devp->i);
  493. return;
  494. }
  495. if (!smd_read_avail(smd_pkt_devp->ch)) {
  496. D_READ("%s: packet size is %d in smd_pkt_dev id:%d -"
  497. " but the data isn't here\n",
  498. __func__, sz, smd_pkt_devp->i);
  499. return;
  500. }
  501. /* here we have a packet of size sz ready */
  502. spin_lock_irqsave(&smd_pkt_devp->pa_spinlock, flags);
  503. __pm_stay_awake(&smd_pkt_devp->pa_ws);
  504. smd_pkt_devp->ws_locked = 1;
  505. spin_unlock_irqrestore(&smd_pkt_devp->pa_spinlock, flags);
  506. wake_up(&smd_pkt_devp->ch_read_wait_queue);
  507. schedule_work(&smd_pkt_devp->packet_arrival_work);
  508. D_READ("%s: wake_up smd_pkt_dev id:%d\n", __func__, smd_pkt_devp->i);
  509. }
  510. static void check_and_wakeup_writer(struct smd_pkt_dev *smd_pkt_devp)
  511. {
  512. int sz;
  513. if (!smd_pkt_devp) {
  514. pr_err("%s on a NULL device\n", __func__);
  515. return;
  516. }
  517. if (!smd_pkt_devp->ch) {
  518. pr_err("%s on a closed smd_pkt_dev id:%d\n",
  519. __func__, smd_pkt_devp->i);
  520. return;
  521. }
  522. sz = smd_write_segment_avail(smd_pkt_devp->ch);
  523. if (sz) {
  524. D_WRITE("%s: %d bytes write space in smd_pkt_dev id:%d\n",
  525. __func__, sz, smd_pkt_devp->i);
  526. smd_disable_read_intr(smd_pkt_devp->ch);
  527. wake_up(&smd_pkt_devp->ch_write_wait_queue);
  528. }
  529. }
  530. static void ch_notify(void *priv, unsigned event)
  531. {
  532. struct smd_pkt_dev *smd_pkt_devp = priv;
  533. if (smd_pkt_devp->ch == 0) {
  534. if (event != SMD_EVENT_CLOSE)
  535. pr_err("%s on a closed smd_pkt_dev id:%d\n",
  536. __func__, smd_pkt_devp->i);
  537. return;
  538. }
  539. switch (event) {
  540. case SMD_EVENT_DATA: {
  541. D_STATUS("%s: DATA event in smd_pkt_dev id:%d\n",
  542. __func__, smd_pkt_devp->i);
  543. check_and_wakeup_reader(smd_pkt_devp);
  544. if (smd_pkt_devp->blocking_write)
  545. check_and_wakeup_writer(smd_pkt_devp);
  546. break;
  547. }
  548. case SMD_EVENT_OPEN:
  549. D_STATUS("%s: OPEN event in smd_pkt_dev id:%d\n",
  550. __func__, smd_pkt_devp->i);
  551. smd_pkt_devp->has_reset = 0;
  552. smd_pkt_devp->is_open = 1;
  553. wake_up_interruptible(&smd_pkt_devp->ch_opened_wait_queue);
  554. break;
  555. case SMD_EVENT_CLOSE:
  556. D_STATUS("%s: CLOSE event in smd_pkt_dev id:%d\n",
  557. __func__, smd_pkt_devp->i);
  558. smd_pkt_devp->is_open = 0;
  559. /* put port into reset state */
  560. clean_and_signal(smd_pkt_devp);
  561. if (smd_pkt_devp->i == LOOPBACK_INX)
  562. schedule_delayed_work(&loopback_work,
  563. msecs_to_jiffies(1000));
  564. break;
  565. }
  566. }
  567. #ifdef CONFIG_ARCH_FSM9XXX
  568. static char *smd_pkt_dev_name[] = {
  569. "smdcntl1",
  570. "smdcntl2",
  571. "smd22",
  572. "smd_pkt_loopback",
  573. };
  574. static char *smd_ch_name[] = {
  575. "DATA6_CNTL",
  576. "DATA7_CNTL",
  577. "DATA22",
  578. "LOOPBACK",
  579. };
  580. static uint32_t smd_ch_edge[] = {
  581. SMD_APPS_QDSP,
  582. SMD_APPS_QDSP,
  583. SMD_APPS_QDSP,
  584. SMD_APPS_QDSP
  585. };
  586. #else
  587. static char *smd_pkt_dev_name[] = {
  588. "smdcntl0",
  589. "smdcntl1",
  590. "smdcntl2",
  591. "smdcntl3",
  592. "smdcntl4",
  593. "smdcntl5",
  594. "smdcntl6",
  595. "smdcntl7",
  596. "smdcntl9",
  597. "smdcntl10",
  598. "smdcntl11",
  599. "smd22",
  600. "smdcnt_rev0",
  601. "smdcnt_rev1",
  602. "smdcnt_rev2",
  603. "smdcnt_rev3",
  604. "smdcnt_rev4",
  605. "smdcnt_rev5",
  606. "smdcnt_rev6",
  607. "smdcnt_rev7",
  608. "smdcnt_rev8",
  609. "smd_sns_dsps",
  610. "apr_apps2",
  611. "smdcntl8",
  612. "smd_sns_adsp",
  613. "smd_cxm_qmi",
  614. "smd_test_framework",
  615. "smd_logging_0",
  616. "smd_data_0",
  617. "apr",
  618. "smd_pkt_loopback",
  619. };
  620. static char *smd_ch_name[] = {
  621. "DATA5_CNTL",
  622. "DATA6_CNTL",
  623. "DATA7_CNTL",
  624. "DATA8_CNTL",
  625. "DATA9_CNTL",
  626. "DATA12_CNTL",
  627. "DATA13_CNTL",
  628. "DATA14_CNTL",
  629. "DATA15_CNTL",
  630. "DATA16_CNTL",
  631. "DATA17_CNTL",
  632. "DATA22",
  633. "DATA23_CNTL",
  634. "DATA24_CNTL",
  635. "DATA25_CNTL",
  636. "DATA26_CNTL",
  637. "DATA27_CNTL",
  638. "DATA28_CNTL",
  639. "DATA29_CNTL",
  640. "DATA30_CNTL",
  641. "DATA31_CNTL",
  642. "SENSOR",
  643. "apr_apps2",
  644. "DATA40_CNTL",
  645. "SENSOR",
  646. "CXM_QMI_PORT_8064",
  647. "TESTFRAMEWORK",
  648. "LOGGING",
  649. "DATA",
  650. "apr",
  651. "LOOPBACK",
  652. };
  653. static uint32_t smd_ch_edge[] = {
  654. SMD_APPS_MODEM,
  655. SMD_APPS_MODEM,
  656. SMD_APPS_MODEM,
  657. SMD_APPS_MODEM,
  658. SMD_APPS_MODEM,
  659. SMD_APPS_MODEM,
  660. SMD_APPS_MODEM,
  661. SMD_APPS_MODEM,
  662. SMD_APPS_MODEM,
  663. SMD_APPS_MODEM,
  664. SMD_APPS_MODEM,
  665. SMD_APPS_MODEM,
  666. SMD_APPS_MODEM,
  667. SMD_APPS_MODEM,
  668. SMD_APPS_MODEM,
  669. SMD_APPS_MODEM,
  670. SMD_APPS_MODEM,
  671. SMD_APPS_MODEM,
  672. SMD_APPS_MODEM,
  673. SMD_APPS_MODEM,
  674. SMD_APPS_MODEM,
  675. SMD_APPS_DSPS,
  676. SMD_APPS_QDSP,
  677. SMD_APPS_MODEM,
  678. SMD_APPS_QDSP,
  679. SMD_APPS_WCNSS,
  680. SMD_APPS_QDSP,
  681. SMD_APPS_QDSP,
  682. SMD_APPS_QDSP,
  683. SMD_APPS_QDSP,
  684. SMD_APPS_MODEM,
  685. };
  686. #endif
  687. module_param_named(loopback_edge, smd_ch_edge[LOOPBACK_INX],
  688. int, S_IRUGO | S_IWUSR | S_IWGRP);
  689. static int smd_pkt_dummy_probe(struct platform_device *pdev)
  690. {
  691. int i;
  692. for (i = 0; i < NUM_SMD_PKT_PORTS; i++) {
  693. if (smd_ch_edge[i] == pdev->id
  694. && !strncmp(pdev->name, smd_ch_name[i],
  695. SMD_MAX_CH_NAME_LEN)
  696. && smd_pkt_devp[i]->driver.probe) {
  697. complete_all(&smd_pkt_devp[i]->ch_allocated);
  698. D_STATUS("%s allocated SMD ch for smd_pkt_dev id:%d\n",
  699. __func__, i);
  700. break;
  701. }
  702. }
  703. return 0;
  704. }
  705. static uint32_t is_modem_smsm_inited(void)
  706. {
  707. uint32_t modem_state;
  708. uint32_t ready_state = (SMSM_INIT | SMSM_SMDINIT);
  709. modem_state = smsm_get_state(SMSM_MODEM_STATE);
  710. return (modem_state & ready_state) == ready_state;
  711. }
  712. int smd_pkt_open(struct inode *inode, struct file *file)
  713. {
  714. int r = 0;
  715. struct smd_pkt_dev *smd_pkt_devp;
  716. const char *peripheral = NULL;
  717. smd_pkt_devp = container_of(inode->i_cdev, struct smd_pkt_dev, cdev);
  718. if (!smd_pkt_devp) {
  719. pr_err("%s on a NULL device\n", __func__);
  720. return -EINVAL;
  721. }
  722. D_STATUS("Begin %s on smd_pkt_dev id:%d\n", __func__, smd_pkt_devp->i);
  723. file->private_data = smd_pkt_devp;
  724. mutex_lock(&smd_pkt_devp->ch_lock);
  725. if (smd_pkt_devp->ch == 0) {
  726. wakeup_source_init(&smd_pkt_devp->pa_ws,
  727. smd_pkt_dev_name[smd_pkt_devp->i]);
  728. INIT_WORK(&smd_pkt_devp->packet_arrival_work,
  729. packet_arrival_worker);
  730. init_completion(&smd_pkt_devp->ch_allocated);
  731. smd_pkt_devp->driver.probe = smd_pkt_dummy_probe;
  732. scnprintf(smd_pkt_devp->pdriver_name, PDRIVER_NAME_MAX_SIZE,
  733. "%s", smd_ch_name[smd_pkt_devp->i]);
  734. smd_pkt_devp->driver.driver.name = smd_pkt_devp->pdriver_name;
  735. smd_pkt_devp->driver.driver.owner = THIS_MODULE;
  736. r = platform_driver_register(&smd_pkt_devp->driver);
  737. if (r) {
  738. pr_err("%s: %s Platform driver reg. failed\n",
  739. __func__, smd_ch_name[smd_pkt_devp->i]);
  740. goto out;
  741. }
  742. peripheral = smd_edge_to_subsystem(
  743. smd_ch_edge[smd_pkt_devp->i]);
  744. if (peripheral) {
  745. smd_pkt_devp->pil = subsystem_get(peripheral);
  746. if (IS_ERR(smd_pkt_devp->pil)) {
  747. r = PTR_ERR(smd_pkt_devp->pil);
  748. pr_err("%s failed on smd_pkt_dev id:%d - subsystem_get failed for %s\n",
  749. __func__, smd_pkt_devp->i, peripheral);
  750. /*
  751. * Sleep inorder to reduce the frequency of
  752. * retry by user-space modules and to avoid
  753. * possible watchdog bite.
  754. */
  755. msleep((smd_pkt_devp->open_modem_wait * 1000));
  756. goto release_pd;
  757. }
  758. /* Wait for the modem SMSM to be inited for the SMD
  759. ** Loopback channel to be allocated at the modem. Since
  760. ** the wait need to be done atmost once, using msleep
  761. ** doesn't degrade the performance. */
  762. if (!strncmp(smd_ch_name[smd_pkt_devp->i], "LOOPBACK",
  763. SMD_MAX_CH_NAME_LEN)) {
  764. if (!is_modem_smsm_inited())
  765. msleep(5000);
  766. smsm_change_state(SMSM_APPS_STATE,
  767. 0, SMSM_SMD_LOOPBACK);
  768. msleep(100);
  769. }
  770. /*
  771. * Wait for a packet channel to be allocated so we know
  772. * the modem is ready enough.
  773. */
  774. if (smd_pkt_devp->open_modem_wait) {
  775. r = wait_for_completion_interruptible_timeout(
  776. &smd_pkt_devp->ch_allocated,
  777. msecs_to_jiffies(
  778. smd_pkt_devp->open_modem_wait
  779. * 1000));
  780. if (r == 0)
  781. r = -ETIMEDOUT;
  782. if (r < 0) {
  783. pr_err("%s: wait on smd_pkt_dev id:%d"
  784. " allocation failed rc:%d\n",
  785. __func__, smd_pkt_devp->i, r);
  786. goto release_pil;
  787. }
  788. }
  789. }
  790. r = smd_named_open_on_edge(smd_ch_name[smd_pkt_devp->i],
  791. smd_ch_edge[smd_pkt_devp->i],
  792. &smd_pkt_devp->ch,
  793. smd_pkt_devp,
  794. ch_notify);
  795. if (r < 0) {
  796. pr_err("%s: %s open failed %d\n", __func__,
  797. smd_ch_name[smd_pkt_devp->i], r);
  798. goto release_pil;
  799. }
  800. r = wait_event_interruptible_timeout(
  801. smd_pkt_devp->ch_opened_wait_queue,
  802. smd_pkt_devp->is_open, (2 * HZ));
  803. if (r == 0) {
  804. r = -ETIMEDOUT;
  805. /* close the ch to sync smd's state with smd_pkt */
  806. smd_close(smd_pkt_devp->ch);
  807. smd_pkt_devp->ch = NULL;
  808. }
  809. if (r < 0) {
  810. pr_err("%s: wait on smd_pkt_dev id:%d OPEN event failed"
  811. " rc:%d\n", __func__, smd_pkt_devp->i, r);
  812. } else if (!smd_pkt_devp->is_open) {
  813. pr_err("%s: Invalid OPEN event on smd_pkt_dev id:%d\n",
  814. __func__, smd_pkt_devp->i);
  815. r = -ENODEV;
  816. } else {
  817. smd_disable_read_intr(smd_pkt_devp->ch);
  818. smd_pkt_devp->ch_size =
  819. smd_write_avail(smd_pkt_devp->ch);
  820. r = 0;
  821. smd_pkt_devp->ref_cnt++;
  822. D_STATUS("Finished %s on smd_pkt_dev id:%d\n",
  823. __func__, smd_pkt_devp->i);
  824. }
  825. } else {
  826. smd_pkt_devp->ref_cnt++;
  827. }
  828. release_pil:
  829. if (peripheral && (r < 0))
  830. subsystem_put(smd_pkt_devp->pil);
  831. release_pd:
  832. if (r < 0) {
  833. platform_driver_unregister(&smd_pkt_devp->driver);
  834. smd_pkt_devp->driver.probe = NULL;
  835. }
  836. out:
  837. if (!smd_pkt_devp->ch)
  838. wakeup_source_trash(&smd_pkt_devp->pa_ws);
  839. mutex_unlock(&smd_pkt_devp->ch_lock);
  840. return r;
  841. }
  842. int smd_pkt_release(struct inode *inode, struct file *file)
  843. {
  844. int r = 0;
  845. struct smd_pkt_dev *smd_pkt_devp = file->private_data;
  846. if (!smd_pkt_devp) {
  847. pr_err("%s on a NULL device\n", __func__);
  848. return -EINVAL;
  849. }
  850. D_STATUS("Begin %s on smd_pkt_dev id:%d\n",
  851. __func__, smd_pkt_devp->i);
  852. mutex_lock(&smd_pkt_devp->ch_lock);
  853. mutex_lock(&smd_pkt_devp->rx_lock);
  854. mutex_lock(&smd_pkt_devp->tx_lock);
  855. if (smd_pkt_devp->ref_cnt > 0)
  856. smd_pkt_devp->ref_cnt--;
  857. if (smd_pkt_devp->ch != 0 && smd_pkt_devp->ref_cnt == 0) {
  858. clean_and_signal(smd_pkt_devp);
  859. r = smd_close(smd_pkt_devp->ch);
  860. smd_pkt_devp->ch = 0;
  861. smd_pkt_devp->blocking_write = 0;
  862. smd_pkt_devp->poll_mode = 0;
  863. if (smd_pkt_devp->driver.probe) {
  864. platform_driver_unregister(&smd_pkt_devp->driver);
  865. smd_pkt_devp->driver.probe = NULL;
  866. } else {
  867. pr_err("%s: invalid unregister dev id:%d magic %x\n",
  868. __func__, smd_pkt_devp->i,
  869. smd_pkt_devp->magic);
  870. }
  871. if (smd_pkt_devp->pil)
  872. subsystem_put(smd_pkt_devp->pil);
  873. smd_pkt_devp->has_reset = 0;
  874. smd_pkt_devp->do_reset_notification = 0;
  875. smd_pkt_devp->ws_locked = 0;
  876. wakeup_source_trash(&smd_pkt_devp->pa_ws);
  877. }
  878. mutex_unlock(&smd_pkt_devp->tx_lock);
  879. mutex_unlock(&smd_pkt_devp->rx_lock);
  880. mutex_unlock(&smd_pkt_devp->ch_lock);
  881. D_STATUS("Finished %s on smd_pkt_dev id:%d\n",
  882. __func__, smd_pkt_devp->i);
  883. return r;
  884. }
  885. static const struct file_operations smd_pkt_fops = {
  886. .owner = THIS_MODULE,
  887. .open = smd_pkt_open,
  888. .release = smd_pkt_release,
  889. .read = smd_pkt_read,
  890. .write = smd_pkt_write,
  891. .poll = smd_pkt_poll,
  892. .unlocked_ioctl = smd_pkt_ioctl,
  893. };
  894. static int __init smd_pkt_init(void)
  895. {
  896. int i;
  897. int r;
  898. if (ARRAY_SIZE(smd_ch_name) != NUM_SMD_PKT_PORTS ||
  899. ARRAY_SIZE(smd_ch_edge) != NUM_SMD_PKT_PORTS ||
  900. ARRAY_SIZE(smd_pkt_dev_name) != NUM_SMD_PKT_PORTS) {
  901. pr_err("%s: mismatch in number of ports\n", __func__);
  902. BUG();
  903. }
  904. r = alloc_chrdev_region(&smd_pkt_number,
  905. 0,
  906. NUM_SMD_PKT_PORTS,
  907. DEVICE_NAME);
  908. if (IS_ERR_VALUE(r)) {
  909. pr_err("%s: alloc_chrdev_region() failed ret:%i\n",
  910. __func__, r);
  911. goto error0;
  912. }
  913. smd_pkt_classp = class_create(THIS_MODULE, DEVICE_NAME);
  914. if (IS_ERR(smd_pkt_classp)) {
  915. pr_err("%s: class_create() failed ENOMEM\n", __func__);
  916. r = -ENOMEM;
  917. goto error1;
  918. }
  919. for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
  920. smd_pkt_devp[i] = kzalloc(sizeof(struct smd_pkt_dev),
  921. GFP_KERNEL);
  922. if (IS_ERR(smd_pkt_devp[i])) {
  923. pr_err("%s: kzalloc() failed for smd_pkt_dev id:%d\n",
  924. __func__, i);
  925. r = -ENOMEM;
  926. goto error2;
  927. }
  928. smd_pkt_devp[i]->i = i;
  929. init_waitqueue_head(&smd_pkt_devp[i]->ch_read_wait_queue);
  930. init_waitqueue_head(&smd_pkt_devp[i]->ch_write_wait_queue);
  931. smd_pkt_devp[i]->is_open = 0;
  932. smd_pkt_devp[i]->poll_mode = 0;
  933. smd_pkt_devp[i]->ws_locked = 0;
  934. smd_pkt_devp[i]->magic = SMD_PKT_MAGIC;
  935. init_waitqueue_head(&smd_pkt_devp[i]->ch_opened_wait_queue);
  936. spin_lock_init(&smd_pkt_devp[i]->pa_spinlock);
  937. mutex_init(&smd_pkt_devp[i]->ch_lock);
  938. mutex_init(&smd_pkt_devp[i]->rx_lock);
  939. mutex_init(&smd_pkt_devp[i]->tx_lock);
  940. cdev_init(&smd_pkt_devp[i]->cdev, &smd_pkt_fops);
  941. smd_pkt_devp[i]->cdev.owner = THIS_MODULE;
  942. r = cdev_add(&smd_pkt_devp[i]->cdev,
  943. (smd_pkt_number + i),
  944. 1);
  945. if (IS_ERR_VALUE(r)) {
  946. pr_err("%s: cdev_add() failed for smd_pkt_dev id:%d"
  947. " ret:%i\n", __func__, i, r);
  948. kfree(smd_pkt_devp[i]);
  949. goto error2;
  950. }
  951. smd_pkt_devp[i]->devicep =
  952. device_create(smd_pkt_classp,
  953. NULL,
  954. (smd_pkt_number + i),
  955. NULL,
  956. smd_pkt_dev_name[i]);
  957. if (IS_ERR(smd_pkt_devp[i]->devicep)) {
  958. pr_err("%s: device_create() failed for smd_pkt_dev"
  959. " id:%d\n", __func__, i);
  960. r = -ENOMEM;
  961. cdev_del(&smd_pkt_devp[i]->cdev);
  962. kfree(smd_pkt_devp[i]);
  963. goto error2;
  964. }
  965. if (device_create_file(smd_pkt_devp[i]->devicep,
  966. &dev_attr_open_timeout))
  967. pr_err("%s: unable to create device attr for"
  968. " smd_pkt_dev id:%d\n", __func__, i);
  969. }
  970. INIT_DELAYED_WORK(&loopback_work, loopback_probe_worker);
  971. smd_pkt_ilctxt = ipc_log_context_create(SMD_PKT_IPC_LOG_PAGE_CNT,
  972. "smd_pkt", 0);
  973. D_STATUS("SMD Packet Port Driver Initialized.\n");
  974. return 0;
  975. error2:
  976. if (i > 0) {
  977. while (--i >= 0) {
  978. cdev_del(&smd_pkt_devp[i]->cdev);
  979. kfree(smd_pkt_devp[i]);
  980. device_destroy(smd_pkt_classp,
  981. MKDEV(MAJOR(smd_pkt_number), i));
  982. }
  983. }
  984. class_destroy(smd_pkt_classp);
  985. error1:
  986. unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS);
  987. error0:
  988. return r;
  989. }
  990. static void __exit smd_pkt_cleanup(void)
  991. {
  992. int i;
  993. for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
  994. cdev_del(&smd_pkt_devp[i]->cdev);
  995. smd_pkt_devp[i]->magic = 0;
  996. kfree(smd_pkt_devp[i]);
  997. device_destroy(smd_pkt_classp,
  998. MKDEV(MAJOR(smd_pkt_number), i));
  999. }
  1000. class_destroy(smd_pkt_classp);
  1001. unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS);
  1002. }
  1003. module_init(smd_pkt_init);
  1004. module_exit(smd_pkt_cleanup);
  1005. MODULE_DESCRIPTION("MSM Shared Memory Packet Port");
  1006. MODULE_LICENSE("GPL v2");