msm_smd_pkt.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  1. /* Copyright (c) 2008-2018, 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 <soc/qcom/smd.h>
  33. #include <soc/qcom/smsm.h>
  34. #include <soc/qcom/subsystem_restart.h>
  35. #include <asm/ioctls.h>
  36. #include <linux/pm.h>
  37. #include <linux/of.h>
  38. #include <linux/ipc_logging.h>
  39. #define MODULE_NAME "msm_smdpkt"
  40. #define DEVICE_NAME "smdpkt"
  41. #define WAKEUPSOURCE_TIMEOUT (2000) /* two seconds */
  42. struct smd_pkt_dev {
  43. struct list_head dev_list;
  44. char dev_name[SMD_MAX_CH_NAME_LEN];
  45. char ch_name[SMD_MAX_CH_NAME_LEN];
  46. uint32_t edge;
  47. struct cdev cdev;
  48. struct device *devicep;
  49. void *pil;
  50. struct smd_channel *ch;
  51. struct mutex ch_lock;
  52. struct mutex rx_lock;
  53. struct mutex tx_lock;
  54. wait_queue_head_t ch_read_wait_queue;
  55. wait_queue_head_t ch_write_wait_queue;
  56. wait_queue_head_t ch_opened_wait_queue;
  57. int i;
  58. int ref_cnt;
  59. int blocking_write;
  60. int is_open;
  61. int poll_mode;
  62. unsigned int ch_size;
  63. uint open_modem_wait;
  64. int has_reset;
  65. int do_reset_notification;
  66. struct completion ch_allocated;
  67. struct wakeup_source pa_ws; /* Packet Arrival Wakeup Source */
  68. struct work_struct packet_arrival_work;
  69. spinlock_t pa_spinlock;
  70. int ws_locked;
  71. int sigs_updated;
  72. };
  73. struct smd_pkt_driver {
  74. struct list_head list;
  75. int ref_cnt;
  76. char pdriver_name[SMD_MAX_CH_NAME_LEN];
  77. struct platform_driver driver;
  78. };
  79. static DEFINE_MUTEX(smd_pkt_driver_lock_lha1);
  80. static LIST_HEAD(smd_pkt_driver_list);
  81. struct class *smd_pkt_classp;
  82. static dev_t smd_pkt_number;
  83. static struct delayed_work loopback_work;
  84. static void check_and_wakeup_reader(struct smd_pkt_dev *smd_pkt_devp);
  85. static void check_and_wakeup_writer(struct smd_pkt_dev *smd_pkt_devp);
  86. static uint32_t is_modem_smsm_inited(void);
  87. static DEFINE_MUTEX(smd_pkt_dev_lock_lha1);
  88. static LIST_HEAD(smd_pkt_dev_list);
  89. static int num_smd_pkt_ports;
  90. #define SMD_PKT_IPC_LOG_PAGE_CNT 2
  91. static void *smd_pkt_ilctxt;
  92. static int msm_smd_pkt_debug_mask;
  93. module_param_named(debug_mask, msm_smd_pkt_debug_mask, int, 0664);
  94. enum {
  95. SMD_PKT_STATUS = 1U << 0,
  96. SMD_PKT_READ = 1U << 1,
  97. SMD_PKT_WRITE = 1U << 2,
  98. SMD_PKT_POLL = 1U << 5,
  99. };
  100. #define DEBUG
  101. #ifdef DEBUG
  102. #define SMD_PKT_LOG_STRING(x...) \
  103. do { \
  104. if (smd_pkt_ilctxt) \
  105. ipc_log_string(smd_pkt_ilctxt, "<SMD_PKT>: "x); \
  106. } while (0)
  107. #define D_STATUS(x...) \
  108. do { \
  109. if (msm_smd_pkt_debug_mask & SMD_PKT_STATUS) \
  110. pr_info("Status: "x); \
  111. SMD_PKT_LOG_STRING(x); \
  112. } while (0)
  113. #define D_READ(x...) \
  114. do { \
  115. if (msm_smd_pkt_debug_mask & SMD_PKT_READ) \
  116. pr_info("Read: "x); \
  117. SMD_PKT_LOG_STRING(x); \
  118. } while (0)
  119. #define D_WRITE(x...) \
  120. do { \
  121. if (msm_smd_pkt_debug_mask & SMD_PKT_WRITE) \
  122. pr_info("Write: "x); \
  123. SMD_PKT_LOG_STRING(x); \
  124. } while (0)
  125. #define D_POLL(x...) \
  126. do { \
  127. if (msm_smd_pkt_debug_mask & SMD_PKT_POLL) \
  128. pr_info("Poll: "x); \
  129. SMD_PKT_LOG_STRING(x); \
  130. } while (0)
  131. #define E_SMD_PKT_SSR(x) \
  132. do { \
  133. if (x->do_reset_notification) \
  134. pr_err("%s notifying reset for smd_pkt_dev id:%d\n", \
  135. __func__, x->i); \
  136. } while (0)
  137. #else
  138. #define D_STATUS(x...) do {} while (0)
  139. #define D_READ(x...) do {} while (0)
  140. #define D_WRITE(x...) do {} while (0)
  141. #define D_POLL(x...) do {} while (0)
  142. #define E_SMD_PKT_SSR(x) do {} while (0)
  143. #endif
  144. static ssize_t open_timeout_store(struct device *d,
  145. struct device_attribute *attr,
  146. const char *buf,
  147. size_t n)
  148. {
  149. struct smd_pkt_dev *smd_pkt_devp;
  150. unsigned long tmp;
  151. mutex_lock(&smd_pkt_dev_lock_lha1);
  152. list_for_each_entry(smd_pkt_devp, &smd_pkt_dev_list, dev_list) {
  153. if (smd_pkt_devp->devicep == d) {
  154. if (!kstrtoul(buf, 10, &tmp)) {
  155. smd_pkt_devp->open_modem_wait = tmp;
  156. mutex_unlock(&smd_pkt_dev_lock_lha1);
  157. return n;
  158. }
  159. mutex_unlock(&smd_pkt_dev_lock_lha1);
  160. pr_err("%s: unable to convert: %s to an int\n",
  161. __func__, buf);
  162. return -EINVAL;
  163. }
  164. }
  165. mutex_unlock(&smd_pkt_dev_lock_lha1);
  166. pr_err("%s: unable to match device to valid smd_pkt port\n", __func__);
  167. return -EINVAL;
  168. }
  169. static ssize_t open_timeout_show(struct device *d,
  170. struct device_attribute *attr,
  171. char *buf)
  172. {
  173. struct smd_pkt_dev *smd_pkt_devp;
  174. mutex_lock(&smd_pkt_dev_lock_lha1);
  175. list_for_each_entry(smd_pkt_devp, &smd_pkt_dev_list, dev_list) {
  176. if (smd_pkt_devp->devicep == d) {
  177. mutex_unlock(&smd_pkt_dev_lock_lha1);
  178. return snprintf(buf, PAGE_SIZE, "%d\n",
  179. smd_pkt_devp->open_modem_wait);
  180. }
  181. }
  182. mutex_unlock(&smd_pkt_dev_lock_lha1);
  183. pr_err("%s: unable to match device to valid smd_pkt port\n", __func__);
  184. return -EINVAL;
  185. }
  186. static DEVICE_ATTR(open_timeout, 0664, open_timeout_show, open_timeout_store);
  187. /**
  188. * loopback_edge_store() - Set the edge type for loopback device
  189. * @d: Linux device structure
  190. * @attr: Device attribute structure
  191. * @buf: Input string
  192. * @n: Length of the input string
  193. *
  194. * This function is used to set the loopback device edge runtime
  195. * by writing to the loopback_edge node.
  196. */
  197. static ssize_t loopback_edge_store(struct device *d,
  198. struct device_attribute *attr,
  199. const char *buf,
  200. size_t n)
  201. {
  202. struct smd_pkt_dev *smd_pkt_devp;
  203. unsigned long tmp;
  204. mutex_lock(&smd_pkt_dev_lock_lha1);
  205. list_for_each_entry(smd_pkt_devp, &smd_pkt_dev_list, dev_list) {
  206. if (smd_pkt_devp->devicep == d) {
  207. if (!kstrtoul(buf, 10, &tmp)) {
  208. smd_pkt_devp->edge = tmp;
  209. mutex_unlock(&smd_pkt_dev_lock_lha1);
  210. return n;
  211. }
  212. mutex_unlock(&smd_pkt_dev_lock_lha1);
  213. pr_err("%s: unable to convert: %s to an int\n",
  214. __func__, buf);
  215. return -EINVAL;
  216. }
  217. }
  218. mutex_unlock(&smd_pkt_dev_lock_lha1);
  219. pr_err("%s: unable to match device to valid smd_pkt port\n", __func__);
  220. return -EINVAL;
  221. }
  222. /**
  223. * loopback_edge_show() - Get the edge type for loopback device
  224. * @d: Linux device structure
  225. * @attr: Device attribute structure
  226. * @buf: Output buffer
  227. *
  228. * This function is used to get the loopback device edge runtime
  229. * by reading the loopback_edge node.
  230. */
  231. static ssize_t loopback_edge_show(struct device *d,
  232. struct device_attribute *attr,
  233. char *buf)
  234. {
  235. struct smd_pkt_dev *smd_pkt_devp;
  236. mutex_lock(&smd_pkt_dev_lock_lha1);
  237. list_for_each_entry(smd_pkt_devp, &smd_pkt_dev_list, dev_list) {
  238. if (smd_pkt_devp->devicep == d) {
  239. mutex_unlock(&smd_pkt_dev_lock_lha1);
  240. return snprintf(buf, PAGE_SIZE, "%d\n",
  241. smd_pkt_devp->edge);
  242. }
  243. }
  244. mutex_unlock(&smd_pkt_dev_lock_lha1);
  245. pr_err("%s: unable to match device to valid smd_pkt port\n", __func__);
  246. return -EINVAL;
  247. }
  248. static DEVICE_ATTR(loopback_edge, 0664, loopback_edge_show,
  249. loopback_edge_store);
  250. static int notify_reset(struct smd_pkt_dev *smd_pkt_devp)
  251. {
  252. smd_pkt_devp->do_reset_notification = 0;
  253. return -ENETRESET;
  254. }
  255. static void clean_and_signal(struct smd_pkt_dev *smd_pkt_devp)
  256. {
  257. smd_pkt_devp->do_reset_notification = 1;
  258. smd_pkt_devp->has_reset = 1;
  259. smd_pkt_devp->is_open = 0;
  260. wake_up(&smd_pkt_devp->ch_read_wait_queue);
  261. wake_up(&smd_pkt_devp->ch_write_wait_queue);
  262. wake_up_interruptible(&smd_pkt_devp->ch_opened_wait_queue);
  263. D_STATUS("%s smd_pkt_dev id:%d\n", __func__, smd_pkt_devp->i);
  264. }
  265. static void loopback_probe_worker(struct work_struct *work)
  266. {
  267. /* Wait for the modem SMSM to be inited for the SMD
  268. ** Loopback channel to be allocated at the modem. Since
  269. ** the wait need to be done atmost once, using msleep
  270. ** doesn't degrade the performance.
  271. */
  272. if (!is_modem_smsm_inited())
  273. schedule_delayed_work(&loopback_work, msecs_to_jiffies(1000));
  274. else
  275. smsm_change_state(SMSM_APPS_STATE,
  276. 0, SMSM_SMD_LOOPBACK);
  277. }
  278. static void packet_arrival_worker(struct work_struct *work)
  279. {
  280. struct smd_pkt_dev *smd_pkt_devp;
  281. unsigned long flags;
  282. smd_pkt_devp = container_of(work, struct smd_pkt_dev,
  283. packet_arrival_work);
  284. mutex_lock(&smd_pkt_devp->ch_lock);
  285. spin_lock_irqsave(&smd_pkt_devp->pa_spinlock, flags);
  286. if (smd_pkt_devp->ch && smd_pkt_devp->ws_locked) {
  287. D_READ("%s locking smd_pkt_dev id:%d wakeup source\n",
  288. __func__, smd_pkt_devp->i);
  289. /*
  290. * Keep system awake long enough to allow userspace client
  291. * to process the packet.
  292. */
  293. __pm_wakeup_event(&smd_pkt_devp->pa_ws, WAKEUPSOURCE_TIMEOUT);
  294. }
  295. spin_unlock_irqrestore(&smd_pkt_devp->pa_spinlock, flags);
  296. mutex_unlock(&smd_pkt_devp->ch_lock);
  297. }
  298. static long smd_pkt_ioctl(struct file *file, unsigned int cmd,
  299. unsigned long arg)
  300. {
  301. int ret;
  302. struct smd_pkt_dev *smd_pkt_devp;
  303. uint32_t val;
  304. smd_pkt_devp = file->private_data;
  305. if (!smd_pkt_devp)
  306. return -EINVAL;
  307. mutex_lock(&smd_pkt_devp->ch_lock);
  308. switch (cmd) {
  309. case TIOCMGET:
  310. smd_pkt_devp->sigs_updated = false;
  311. ret = smd_tiocmget(smd_pkt_devp->ch);
  312. D_STATUS("%s TIOCMGET command on smd_pkt_dev id:%d [%d]\n",
  313. __func__, smd_pkt_devp->i, ret);
  314. if (ret > 0)
  315. ret = put_user((uint32_t)ret, (uint32_t __user *)arg);
  316. break;
  317. case TIOCMSET:
  318. ret = get_user(val, (uint32_t *)arg);
  319. if (ret) {
  320. pr_err("Error getting TIOCMSET value\n");
  321. mutex_unlock(&smd_pkt_devp->ch_lock);
  322. return ret;
  323. }
  324. D_STATUS("%s TIOCSET command on smd_pkt_dev id:%d arg[0x%x]\n",
  325. __func__, smd_pkt_devp->i, val);
  326. ret = smd_tiocmset(smd_pkt_devp->ch, val, ~val);
  327. break;
  328. case SMD_PKT_IOCTL_BLOCKING_WRITE:
  329. ret = get_user(smd_pkt_devp->blocking_write, (int *)arg);
  330. break;
  331. default:
  332. pr_err_ratelimited("%s: Unrecognized ioctl command %d\n",
  333. __func__, cmd);
  334. ret = -ENOIOCTLCMD;
  335. }
  336. mutex_unlock(&smd_pkt_devp->ch_lock);
  337. return ret;
  338. }
  339. ssize_t smd_pkt_read(struct file *file,
  340. char __user *_buf,
  341. size_t count,
  342. loff_t *ppos)
  343. {
  344. int r;
  345. int bytes_read;
  346. int pkt_size;
  347. struct smd_pkt_dev *smd_pkt_devp;
  348. unsigned long flags;
  349. void *buf;
  350. smd_pkt_devp = file->private_data;
  351. if (!smd_pkt_devp) {
  352. pr_err_ratelimited("%s on NULL smd_pkt_dev\n", __func__);
  353. return -EINVAL;
  354. }
  355. if (!smd_pkt_devp->ch) {
  356. pr_err_ratelimited("%s on a closed smd_pkt_dev id:%d\n",
  357. __func__, smd_pkt_devp->i);
  358. return -EINVAL;
  359. }
  360. if (smd_pkt_devp->do_reset_notification) {
  361. /* notify client that a reset occurred */
  362. E_SMD_PKT_SSR(smd_pkt_devp);
  363. return notify_reset(smd_pkt_devp);
  364. }
  365. D_READ("Begin %s on smd_pkt_dev id:%d buffer_size %zu\n",
  366. __func__, smd_pkt_devp->i, count);
  367. buf = kmalloc(count, GFP_KERNEL);
  368. if (!buf)
  369. return -ENOMEM;
  370. wait_for_packet:
  371. r = wait_event_interruptible(smd_pkt_devp->ch_read_wait_queue,
  372. !smd_pkt_devp->ch ||
  373. (smd_cur_packet_size(smd_pkt_devp->ch) > 0
  374. && smd_read_avail(smd_pkt_devp->ch)) ||
  375. smd_pkt_devp->has_reset);
  376. mutex_lock(&smd_pkt_devp->rx_lock);
  377. if (smd_pkt_devp->has_reset) {
  378. mutex_unlock(&smd_pkt_devp->rx_lock);
  379. E_SMD_PKT_SSR(smd_pkt_devp);
  380. kfree(buf);
  381. return notify_reset(smd_pkt_devp);
  382. }
  383. if (!smd_pkt_devp->ch) {
  384. mutex_unlock(&smd_pkt_devp->rx_lock);
  385. pr_err_ratelimited("%s on a closed smd_pkt_dev id:%d\n",
  386. __func__, smd_pkt_devp->i);
  387. kfree(buf);
  388. return -EINVAL;
  389. }
  390. if (r < 0) {
  391. mutex_unlock(&smd_pkt_devp->rx_lock);
  392. /* qualify error message */
  393. if (r != -ERESTARTSYS) {
  394. /* we get this anytime a signal comes in */
  395. pr_err_ratelimited("%s: wait_event_interruptible on smd_pkt_dev id:%d ret %i\n",
  396. __func__, smd_pkt_devp->i, r);
  397. }
  398. kfree(buf);
  399. return r;
  400. }
  401. /* Here we have a whole packet waiting for us */
  402. pkt_size = smd_cur_packet_size(smd_pkt_devp->ch);
  403. if (!pkt_size) {
  404. pr_err_ratelimited("%s: No data on smd_pkt_dev id:%d, False wakeup\n",
  405. __func__, smd_pkt_devp->i);
  406. mutex_unlock(&smd_pkt_devp->rx_lock);
  407. goto wait_for_packet;
  408. }
  409. if (pkt_size < 0) {
  410. pr_err_ratelimited("%s: Error %d obtaining packet size for Channel %s",
  411. __func__, pkt_size, smd_pkt_devp->ch_name);
  412. kfree(buf);
  413. return pkt_size;
  414. }
  415. if ((uint32_t)pkt_size > count) {
  416. pr_err_ratelimited("%s: failure on smd_pkt_dev id: %d - packet size %d > buffer size %zu,",
  417. __func__, smd_pkt_devp->i,
  418. pkt_size, count);
  419. mutex_unlock(&smd_pkt_devp->rx_lock);
  420. kfree(buf);
  421. return -ETOOSMALL;
  422. }
  423. bytes_read = 0;
  424. do {
  425. r = smd_read(smd_pkt_devp->ch,
  426. (buf + bytes_read),
  427. (pkt_size - bytes_read));
  428. if (r < 0) {
  429. mutex_unlock(&smd_pkt_devp->rx_lock);
  430. if (smd_pkt_devp->has_reset) {
  431. E_SMD_PKT_SSR(smd_pkt_devp);
  432. return notify_reset(smd_pkt_devp);
  433. }
  434. pr_err_ratelimited("%s Error while reading %d\n",
  435. __func__, r);
  436. kfree(buf);
  437. return r;
  438. }
  439. bytes_read += r;
  440. if (pkt_size != bytes_read)
  441. wait_event(smd_pkt_devp->ch_read_wait_queue,
  442. smd_read_avail(smd_pkt_devp->ch) ||
  443. smd_pkt_devp->has_reset);
  444. if (smd_pkt_devp->has_reset) {
  445. mutex_unlock(&smd_pkt_devp->rx_lock);
  446. E_SMD_PKT_SSR(smd_pkt_devp);
  447. kfree(buf);
  448. return notify_reset(smd_pkt_devp);
  449. }
  450. } while (pkt_size != bytes_read);
  451. mutex_unlock(&smd_pkt_devp->rx_lock);
  452. mutex_lock(&smd_pkt_devp->ch_lock);
  453. spin_lock_irqsave(&smd_pkt_devp->pa_spinlock, flags);
  454. if (smd_pkt_devp->poll_mode &&
  455. !smd_cur_packet_size(smd_pkt_devp->ch)) {
  456. __pm_relax(&smd_pkt_devp->pa_ws);
  457. smd_pkt_devp->ws_locked = 0;
  458. smd_pkt_devp->poll_mode = 0;
  459. D_READ("%s unlocked smd_pkt_dev id:%d wakeup_source\n",
  460. __func__, smd_pkt_devp->i);
  461. }
  462. spin_unlock_irqrestore(&smd_pkt_devp->pa_spinlock, flags);
  463. mutex_unlock(&smd_pkt_devp->ch_lock);
  464. r = copy_to_user(_buf, buf, bytes_read);
  465. if (r) {
  466. kfree(buf);
  467. return -EFAULT;
  468. }
  469. D_READ("Finished %s on smd_pkt_dev id:%d %d bytes\n",
  470. __func__, smd_pkt_devp->i, bytes_read);
  471. kfree(buf);
  472. /* check and wakeup read threads waiting on this device */
  473. check_and_wakeup_reader(smd_pkt_devp);
  474. return bytes_read;
  475. }
  476. ssize_t smd_pkt_write(struct file *file,
  477. const char __user *_buf,
  478. size_t count,
  479. loff_t *ppos)
  480. {
  481. int r = 0, bytes_written;
  482. struct smd_pkt_dev *smd_pkt_devp;
  483. DEFINE_WAIT(write_wait);
  484. void *buf;
  485. smd_pkt_devp = file->private_data;
  486. if (!smd_pkt_devp) {
  487. pr_err_ratelimited("%s on NULL smd_pkt_dev\n", __func__);
  488. return -EINVAL;
  489. }
  490. if (!smd_pkt_devp->ch) {
  491. pr_err_ratelimited("%s on a closed smd_pkt_dev id:%d\n",
  492. __func__, smd_pkt_devp->i);
  493. return -EINVAL;
  494. }
  495. if (smd_pkt_devp->do_reset_notification || smd_pkt_devp->has_reset) {
  496. E_SMD_PKT_SSR(smd_pkt_devp);
  497. /* notify client that a reset occurred */
  498. return notify_reset(smd_pkt_devp);
  499. }
  500. D_WRITE("Begin %s on smd_pkt_dev id:%d data_size %zu\n",
  501. __func__, smd_pkt_devp->i, count);
  502. buf = kmalloc(count, GFP_KERNEL);
  503. if (!buf)
  504. return -ENOMEM;
  505. r = copy_from_user(buf, _buf, count);
  506. if (r) {
  507. kfree(buf);
  508. return -EFAULT;
  509. }
  510. mutex_lock(&smd_pkt_devp->tx_lock);
  511. if (!smd_pkt_devp->blocking_write) {
  512. if (smd_write_avail(smd_pkt_devp->ch) < count) {
  513. pr_err_ratelimited("%s: Not enough space in smd_pkt_dev id:%d\n",
  514. __func__, smd_pkt_devp->i);
  515. mutex_unlock(&smd_pkt_devp->tx_lock);
  516. kfree(buf);
  517. return -ENOMEM;
  518. }
  519. }
  520. r = smd_write_start(smd_pkt_devp->ch, count);
  521. if (r < 0) {
  522. mutex_unlock(&smd_pkt_devp->tx_lock);
  523. pr_err_ratelimited("%s: Error:%d in smd_pkt_dev id:%d @ smd_write_start\n",
  524. __func__, r, smd_pkt_devp->i);
  525. kfree(buf);
  526. return r;
  527. }
  528. bytes_written = 0;
  529. do {
  530. prepare_to_wait(&smd_pkt_devp->ch_write_wait_queue,
  531. &write_wait, TASK_UNINTERRUPTIBLE);
  532. if (!smd_write_segment_avail(smd_pkt_devp->ch) &&
  533. !smd_pkt_devp->has_reset) {
  534. smd_enable_read_intr(smd_pkt_devp->ch);
  535. schedule();
  536. }
  537. finish_wait(&smd_pkt_devp->ch_write_wait_queue, &write_wait);
  538. smd_disable_read_intr(smd_pkt_devp->ch);
  539. if (smd_pkt_devp->has_reset) {
  540. mutex_unlock(&smd_pkt_devp->tx_lock);
  541. E_SMD_PKT_SSR(smd_pkt_devp);
  542. kfree(buf);
  543. return notify_reset(smd_pkt_devp);
  544. }
  545. r = smd_write_segment(smd_pkt_devp->ch,
  546. (void *)(buf + bytes_written),
  547. (count - bytes_written));
  548. if (r < 0) {
  549. mutex_unlock(&smd_pkt_devp->tx_lock);
  550. if (smd_pkt_devp->has_reset) {
  551. E_SMD_PKT_SSR(smd_pkt_devp);
  552. return notify_reset(smd_pkt_devp);
  553. }
  554. pr_err_ratelimited("%s on smd_pkt_dev id:%d failed r:%d\n",
  555. __func__, smd_pkt_devp->i, r);
  556. kfree(buf);
  557. return r;
  558. }
  559. bytes_written += r;
  560. } while (bytes_written != count);
  561. smd_write_end(smd_pkt_devp->ch);
  562. mutex_unlock(&smd_pkt_devp->tx_lock);
  563. D_WRITE("Finished %s on smd_pkt_dev id:%d %zu bytes\n",
  564. __func__, smd_pkt_devp->i, count);
  565. kfree(buf);
  566. return count;
  567. }
  568. static unsigned int smd_pkt_poll(struct file *file, poll_table *wait)
  569. {
  570. struct smd_pkt_dev *smd_pkt_devp;
  571. unsigned int mask = 0;
  572. smd_pkt_devp = file->private_data;
  573. if (!smd_pkt_devp) {
  574. pr_err_ratelimited("%s on a NULL device\n", __func__);
  575. return POLLERR;
  576. }
  577. smd_pkt_devp->poll_mode = 1;
  578. poll_wait(file, &smd_pkt_devp->ch_read_wait_queue, wait);
  579. mutex_lock(&smd_pkt_devp->ch_lock);
  580. if (smd_pkt_devp->has_reset || !smd_pkt_devp->ch) {
  581. mutex_unlock(&smd_pkt_devp->ch_lock);
  582. return POLLERR;
  583. }
  584. if (smd_read_avail(smd_pkt_devp->ch)) {
  585. mask |= POLLIN | POLLRDNORM;
  586. D_POLL("%s sets POLLIN for smd_pkt_dev id: %d\n",
  587. __func__, smd_pkt_devp->i);
  588. }
  589. if (smd_pkt_devp->sigs_updated) {
  590. mask |= POLLPRI;
  591. D_POLL("%s sets POLLPRI for smd_pkt_dev id: %d\n",
  592. __func__, smd_pkt_devp->i);
  593. }
  594. mutex_unlock(&smd_pkt_devp->ch_lock);
  595. return mask;
  596. }
  597. static void check_and_wakeup_reader(struct smd_pkt_dev *smd_pkt_devp)
  598. {
  599. int sz;
  600. unsigned long flags;
  601. if (!smd_pkt_devp) {
  602. pr_err("%s on a NULL device\n", __func__);
  603. return;
  604. }
  605. if (!smd_pkt_devp->ch) {
  606. pr_err("%s on a closed smd_pkt_dev id:%d\n",
  607. __func__, smd_pkt_devp->i);
  608. return;
  609. }
  610. sz = smd_cur_packet_size(smd_pkt_devp->ch);
  611. if (sz == 0) {
  612. D_READ("%s: No packet in smd_pkt_dev id:%d\n",
  613. __func__, smd_pkt_devp->i);
  614. return;
  615. }
  616. if (!smd_read_avail(smd_pkt_devp->ch)) {
  617. D_READ(
  618. "%s: packet size is %d in smd_pkt_dev id:%d - but the data isn't here\n",
  619. __func__, sz, smd_pkt_devp->i);
  620. return;
  621. }
  622. /* here we have a packet of size sz ready */
  623. spin_lock_irqsave(&smd_pkt_devp->pa_spinlock, flags);
  624. __pm_stay_awake(&smd_pkt_devp->pa_ws);
  625. smd_pkt_devp->ws_locked = 1;
  626. spin_unlock_irqrestore(&smd_pkt_devp->pa_spinlock, flags);
  627. wake_up(&smd_pkt_devp->ch_read_wait_queue);
  628. schedule_work(&smd_pkt_devp->packet_arrival_work);
  629. D_READ("%s: wake_up smd_pkt_dev id:%d\n", __func__, smd_pkt_devp->i);
  630. }
  631. static void check_and_wakeup_writer(struct smd_pkt_dev *smd_pkt_devp)
  632. {
  633. int sz;
  634. if (!smd_pkt_devp) {
  635. pr_err("%s on a NULL device\n", __func__);
  636. return;
  637. }
  638. if (!smd_pkt_devp->ch) {
  639. pr_err("%s on a closed smd_pkt_dev id:%d\n",
  640. __func__, smd_pkt_devp->i);
  641. return;
  642. }
  643. sz = smd_write_segment_avail(smd_pkt_devp->ch);
  644. if (sz) {
  645. D_WRITE("%s: %d bytes write space in smd_pkt_dev id:%d\n",
  646. __func__, sz, smd_pkt_devp->i);
  647. smd_disable_read_intr(smd_pkt_devp->ch);
  648. wake_up(&smd_pkt_devp->ch_write_wait_queue);
  649. }
  650. }
  651. static void ch_notify(void *priv, unsigned int event)
  652. {
  653. struct smd_pkt_dev *smd_pkt_devp = priv;
  654. if (smd_pkt_devp->ch == 0) {
  655. if (event != SMD_EVENT_CLOSE)
  656. pr_err("%s on a closed smd_pkt_dev id:%d\n",
  657. __func__, smd_pkt_devp->i);
  658. return;
  659. }
  660. switch (event) {
  661. case SMD_EVENT_DATA: {
  662. D_STATUS("%s: DATA event in smd_pkt_dev id:%d\n",
  663. __func__, smd_pkt_devp->i);
  664. check_and_wakeup_reader(smd_pkt_devp);
  665. if (smd_pkt_devp->blocking_write)
  666. check_and_wakeup_writer(smd_pkt_devp);
  667. break;
  668. }
  669. case SMD_EVENT_OPEN:
  670. D_STATUS("%s: OPEN event in smd_pkt_dev id:%d\n",
  671. __func__, smd_pkt_devp->i);
  672. smd_pkt_devp->has_reset = 0;
  673. smd_pkt_devp->is_open = 1;
  674. wake_up_interruptible(&smd_pkt_devp->ch_opened_wait_queue);
  675. break;
  676. case SMD_EVENT_CLOSE:
  677. D_STATUS("%s: CLOSE event in smd_pkt_dev id:%d\n",
  678. __func__, smd_pkt_devp->i);
  679. smd_pkt_devp->is_open = 0;
  680. /* put port into reset state */
  681. clean_and_signal(smd_pkt_devp);
  682. if (!strcmp(smd_pkt_devp->ch_name, "LOOPBACK"))
  683. schedule_delayed_work(&loopback_work,
  684. msecs_to_jiffies(1000));
  685. break;
  686. case SMD_EVENT_STATUS:
  687. smd_pkt_devp->sigs_updated = true;
  688. break;
  689. }
  690. }
  691. static int smd_pkt_dummy_probe(struct platform_device *pdev)
  692. {
  693. struct smd_pkt_dev *smd_pkt_devp;
  694. mutex_lock(&smd_pkt_dev_lock_lha1);
  695. list_for_each_entry(smd_pkt_devp, &smd_pkt_dev_list, dev_list) {
  696. if (smd_pkt_devp->edge == pdev->id
  697. && !strcmp(pdev->name, smd_pkt_devp->ch_name)) {
  698. complete_all(&smd_pkt_devp->ch_allocated);
  699. D_STATUS("%s allocated SMD ch for smd_pkt_dev id:%d\n",
  700. __func__, smd_pkt_devp->i);
  701. break;
  702. }
  703. }
  704. mutex_unlock(&smd_pkt_dev_lock_lha1);
  705. return 0;
  706. }
  707. static uint32_t is_modem_smsm_inited(void)
  708. {
  709. uint32_t modem_state;
  710. uint32_t ready_state = (SMSM_INIT | SMSM_SMDINIT);
  711. modem_state = smsm_get_state(SMSM_MODEM_STATE);
  712. return (modem_state & ready_state) == ready_state;
  713. }
  714. /**
  715. * smd_pkt_add_driver() - Add platform drivers for smd pkt device
  716. *
  717. * @smd_pkt_devp: pointer to the smd pkt device structure
  718. *
  719. * @returns: 0 for success, standard Linux error code otherwise
  720. *
  721. * This function is used to register platform driver once for all
  722. * smd pkt devices which have same names and increment the reference
  723. * count for 2nd to nth devices.
  724. */
  725. static int smd_pkt_add_driver(struct smd_pkt_dev *smd_pkt_devp)
  726. {
  727. int r = 0;
  728. struct smd_pkt_driver *smd_pkt_driverp;
  729. struct smd_pkt_driver *item;
  730. if (!smd_pkt_devp) {
  731. pr_err("%s on a NULL device\n", __func__);
  732. return -EINVAL;
  733. }
  734. D_STATUS("Begin %s on smd_pkt_ch[%s]\n", __func__,
  735. smd_pkt_devp->ch_name);
  736. mutex_lock(&smd_pkt_driver_lock_lha1);
  737. list_for_each_entry(item, &smd_pkt_driver_list, list) {
  738. if (!strcmp(item->pdriver_name, smd_pkt_devp->ch_name)) {
  739. D_STATUS("%s:%s Already Platform driver reg. cnt:%d\n",
  740. __func__, smd_pkt_devp->ch_name, item->ref_cnt);
  741. ++item->ref_cnt;
  742. goto exit;
  743. }
  744. }
  745. smd_pkt_driverp = kzalloc(sizeof(*smd_pkt_driverp), GFP_KERNEL);
  746. if (IS_ERR_OR_NULL(smd_pkt_driverp)) {
  747. pr_err("%s: kzalloc() failed for smd_pkt_driver[%s]\n",
  748. __func__, smd_pkt_devp->ch_name);
  749. r = -ENOMEM;
  750. goto exit;
  751. }
  752. smd_pkt_driverp->driver.probe = smd_pkt_dummy_probe;
  753. scnprintf(smd_pkt_driverp->pdriver_name, SMD_MAX_CH_NAME_LEN,
  754. "%s", smd_pkt_devp->ch_name);
  755. smd_pkt_driverp->driver.driver.name = smd_pkt_driverp->pdriver_name;
  756. smd_pkt_driverp->driver.driver.owner = THIS_MODULE;
  757. r = platform_driver_register(&smd_pkt_driverp->driver);
  758. if (r) {
  759. pr_err("%s: %s Platform driver reg. failed\n",
  760. __func__, smd_pkt_devp->ch_name);
  761. kfree(smd_pkt_driverp);
  762. goto exit;
  763. }
  764. ++smd_pkt_driverp->ref_cnt;
  765. list_add(&smd_pkt_driverp->list, &smd_pkt_driver_list);
  766. exit:
  767. D_STATUS("End %s on smd_pkt_ch[%s]\n", __func__, smd_pkt_devp->ch_name);
  768. mutex_unlock(&smd_pkt_driver_lock_lha1);
  769. return r;
  770. }
  771. /**
  772. * smd_pkt_remove_driver() - Remove the platform drivers for smd pkt device
  773. *
  774. * @smd_pkt_devp: pointer to the smd pkt device structure
  775. *
  776. * This function is used to decrement the reference count on
  777. * platform drivers for smd pkt devices and removes the drivers
  778. * when the reference count becomes zero.
  779. */
  780. static void smd_pkt_remove_driver(struct smd_pkt_dev *smd_pkt_devp)
  781. {
  782. struct smd_pkt_driver *smd_pkt_driverp;
  783. bool found_item = false;
  784. if (!smd_pkt_devp) {
  785. pr_err("%s on a NULL device\n", __func__);
  786. return;
  787. }
  788. D_STATUS("Begin %s on smd_pkt_ch[%s]\n", __func__,
  789. smd_pkt_devp->ch_name);
  790. mutex_lock(&smd_pkt_driver_lock_lha1);
  791. list_for_each_entry(smd_pkt_driverp, &smd_pkt_driver_list, list) {
  792. if (!strcmp(smd_pkt_driverp->pdriver_name,
  793. smd_pkt_devp->ch_name)) {
  794. found_item = true;
  795. D_STATUS("%s:%s Platform driver cnt:%d\n",
  796. __func__, smd_pkt_devp->ch_name,
  797. smd_pkt_driverp->ref_cnt);
  798. if (smd_pkt_driverp->ref_cnt > 0)
  799. --smd_pkt_driverp->ref_cnt;
  800. else
  801. pr_warn("%s reference count <= 0\n", __func__);
  802. break;
  803. }
  804. }
  805. if (!found_item)
  806. pr_err("%s:%s No item found in list.\n",
  807. __func__, smd_pkt_devp->ch_name);
  808. if (found_item && smd_pkt_driverp->ref_cnt == 0) {
  809. platform_driver_unregister(&smd_pkt_driverp->driver);
  810. smd_pkt_driverp->driver.probe = NULL;
  811. list_del(&smd_pkt_driverp->list);
  812. kfree(smd_pkt_driverp);
  813. }
  814. mutex_unlock(&smd_pkt_driver_lock_lha1);
  815. D_STATUS("End %s on smd_pkt_ch[%s]\n", __func__, smd_pkt_devp->ch_name);
  816. }
  817. int smd_pkt_open(struct inode *inode, struct file *file)
  818. {
  819. int r = 0;
  820. struct smd_pkt_dev *smd_pkt_devp;
  821. const char *peripheral = NULL;
  822. smd_pkt_devp = container_of(inode->i_cdev, struct smd_pkt_dev, cdev);
  823. if (!smd_pkt_devp) {
  824. pr_err_ratelimited("%s on a NULL device\n", __func__);
  825. return -EINVAL;
  826. }
  827. D_STATUS("Begin %s on smd_pkt_dev id:%d\n", __func__, smd_pkt_devp->i);
  828. file->private_data = smd_pkt_devp;
  829. mutex_lock(&smd_pkt_devp->ch_lock);
  830. if (smd_pkt_devp->ch == 0) {
  831. unsigned int open_wait_rem;
  832. open_wait_rem = smd_pkt_devp->open_modem_wait * 1000;
  833. reinit_completion(&smd_pkt_devp->ch_allocated);
  834. r = smd_pkt_add_driver(smd_pkt_devp);
  835. if (r) {
  836. pr_err_ratelimited("%s: %s Platform driver reg. failed\n",
  837. __func__, smd_pkt_devp->ch_name);
  838. goto out;
  839. }
  840. peripheral = smd_edge_to_pil_str(smd_pkt_devp->edge);
  841. if (!IS_ERR_OR_NULL(peripheral)) {
  842. smd_pkt_devp->pil = subsystem_get(peripheral);
  843. if (IS_ERR(smd_pkt_devp->pil)) {
  844. r = PTR_ERR(smd_pkt_devp->pil);
  845. pr_err_ratelimited("%s failed on smd_pkt_dev id:%d - subsystem_get failed for %s\n",
  846. __func__, smd_pkt_devp->i, peripheral);
  847. /*
  848. * Sleep inorder to reduce the frequency of
  849. * retry by user-space modules and to avoid
  850. * possible watchdog bite.
  851. */
  852. msleep(open_wait_rem);
  853. goto release_pd;
  854. }
  855. }
  856. /* Wait for the modem SMSM to be inited for the SMD
  857. ** Loopback channel to be allocated at the modem. Since
  858. ** the wait need to be done atmost once, using msleep
  859. ** doesn't degrade the performance.
  860. */
  861. if (!strcmp(smd_pkt_devp->ch_name, "LOOPBACK")) {
  862. if (!is_modem_smsm_inited())
  863. msleep(5000);
  864. smsm_change_state(SMSM_APPS_STATE,
  865. 0, SMSM_SMD_LOOPBACK);
  866. msleep(100);
  867. }
  868. /*
  869. * Wait for a packet channel to be allocated so we know
  870. * the modem is ready enough.
  871. */
  872. if (open_wait_rem) {
  873. r = wait_for_completion_interruptible_timeout(
  874. &smd_pkt_devp->ch_allocated,
  875. msecs_to_jiffies(open_wait_rem));
  876. if (r >= 0)
  877. open_wait_rem = jiffies_to_msecs(r);
  878. if (r == 0)
  879. r = -ETIMEDOUT;
  880. if (r == -ERESTARTSYS) {
  881. pr_info_ratelimited("%s: wait on smd_pkt_dev id:%d allocation interrupted\n",
  882. __func__, smd_pkt_devp->i);
  883. goto release_pil;
  884. }
  885. if (r < 0) {
  886. pr_err_ratelimited("%s: wait on smd_pkt_dev id:%d allocation failed rc:%d\n",
  887. __func__, smd_pkt_devp->i, r);
  888. goto release_pil;
  889. }
  890. }
  891. r = smd_named_open_on_edge(smd_pkt_devp->ch_name,
  892. smd_pkt_devp->edge,
  893. &smd_pkt_devp->ch,
  894. smd_pkt_devp,
  895. ch_notify);
  896. if (r < 0) {
  897. pr_err_ratelimited("%s: %s open failed %d\n", __func__,
  898. smd_pkt_devp->ch_name, r);
  899. goto release_pil;
  900. }
  901. open_wait_rem = max_t(unsigned int, 2000, open_wait_rem);
  902. r = wait_event_interruptible_timeout(
  903. smd_pkt_devp->ch_opened_wait_queue,
  904. smd_pkt_devp->is_open,
  905. msecs_to_jiffies(open_wait_rem));
  906. if (r == 0)
  907. r = -ETIMEDOUT;
  908. if (r < 0) {
  909. /* close the ch to sync smd's state with smd_pkt */
  910. smd_close(smd_pkt_devp->ch);
  911. smd_pkt_devp->ch = NULL;
  912. }
  913. if (r == -ERESTARTSYS) {
  914. pr_info_ratelimited("%s: wait on smd_pkt_dev id:%d OPEN interrupted\n",
  915. __func__, smd_pkt_devp->i);
  916. } else if (r < 0) {
  917. pr_err_ratelimited("%s: wait on smd_pkt_dev id:%d OPEN event failed rc:%d\n",
  918. __func__, smd_pkt_devp->i, r);
  919. } else if (!smd_pkt_devp->is_open) {
  920. pr_err_ratelimited("%s: Invalid OPEN event on smd_pkt_dev id:%d\n",
  921. __func__, smd_pkt_devp->i);
  922. r = -ENODEV;
  923. } else {
  924. smd_disable_read_intr(smd_pkt_devp->ch);
  925. smd_pkt_devp->ch_size =
  926. smd_write_avail(smd_pkt_devp->ch);
  927. r = 0;
  928. smd_pkt_devp->ref_cnt++;
  929. D_STATUS("Finished %s on smd_pkt_dev id:%d\n",
  930. __func__, smd_pkt_devp->i);
  931. }
  932. } else {
  933. smd_pkt_devp->ref_cnt++;
  934. }
  935. release_pil:
  936. if (peripheral && (r < 0)) {
  937. subsystem_put(smd_pkt_devp->pil);
  938. smd_pkt_devp->pil = NULL;
  939. }
  940. release_pd:
  941. if (r < 0)
  942. smd_pkt_remove_driver(smd_pkt_devp);
  943. out:
  944. mutex_unlock(&smd_pkt_devp->ch_lock);
  945. return r;
  946. }
  947. int smd_pkt_release(struct inode *inode, struct file *file)
  948. {
  949. int r = 0;
  950. struct smd_pkt_dev *smd_pkt_devp = file->private_data;
  951. unsigned long flags;
  952. if (!smd_pkt_devp) {
  953. pr_err_ratelimited("%s on a NULL device\n", __func__);
  954. return -EINVAL;
  955. }
  956. D_STATUS("Begin %s on smd_pkt_dev id:%d\n",
  957. __func__, smd_pkt_devp->i);
  958. mutex_lock(&smd_pkt_devp->ch_lock);
  959. mutex_lock(&smd_pkt_devp->rx_lock);
  960. mutex_lock(&smd_pkt_devp->tx_lock);
  961. if (smd_pkt_devp->ref_cnt > 0)
  962. smd_pkt_devp->ref_cnt--;
  963. if (smd_pkt_devp->ch != 0 && smd_pkt_devp->ref_cnt == 0) {
  964. clean_and_signal(smd_pkt_devp);
  965. r = smd_close(smd_pkt_devp->ch);
  966. smd_pkt_devp->ch = 0;
  967. smd_pkt_devp->blocking_write = 0;
  968. smd_pkt_devp->poll_mode = 0;
  969. smd_pkt_remove_driver(smd_pkt_devp);
  970. if (smd_pkt_devp->pil)
  971. subsystem_put(smd_pkt_devp->pil);
  972. smd_pkt_devp->has_reset = 0;
  973. smd_pkt_devp->do_reset_notification = 0;
  974. spin_lock_irqsave(&smd_pkt_devp->pa_spinlock, flags);
  975. if (smd_pkt_devp->ws_locked) {
  976. __pm_relax(&smd_pkt_devp->pa_ws);
  977. smd_pkt_devp->ws_locked = 0;
  978. }
  979. spin_unlock_irqrestore(&smd_pkt_devp->pa_spinlock, flags);
  980. smd_pkt_devp->sigs_updated = false;
  981. }
  982. mutex_unlock(&smd_pkt_devp->tx_lock);
  983. mutex_unlock(&smd_pkt_devp->rx_lock);
  984. mutex_unlock(&smd_pkt_devp->ch_lock);
  985. if (flush_work(&smd_pkt_devp->packet_arrival_work))
  986. D_STATUS("%s: Flushed work for smd_pkt_dev id:%d\n", __func__,
  987. smd_pkt_devp->i);
  988. D_STATUS("Finished %s on smd_pkt_dev id:%d\n",
  989. __func__, smd_pkt_devp->i);
  990. return r;
  991. }
  992. static const struct file_operations smd_pkt_fops = {
  993. .owner = THIS_MODULE,
  994. .open = smd_pkt_open,
  995. .release = smd_pkt_release,
  996. .read = smd_pkt_read,
  997. .write = smd_pkt_write,
  998. .poll = smd_pkt_poll,
  999. .unlocked_ioctl = smd_pkt_ioctl,
  1000. .compat_ioctl = smd_pkt_ioctl,
  1001. };
  1002. static int smd_pkt_init_add_device(struct smd_pkt_dev *smd_pkt_devp, int i)
  1003. {
  1004. int r = 0;
  1005. smd_pkt_devp->i = i;
  1006. init_waitqueue_head(&smd_pkt_devp->ch_read_wait_queue);
  1007. init_waitqueue_head(&smd_pkt_devp->ch_write_wait_queue);
  1008. smd_pkt_devp->is_open = 0;
  1009. smd_pkt_devp->poll_mode = 0;
  1010. smd_pkt_devp->ws_locked = 0;
  1011. init_waitqueue_head(&smd_pkt_devp->ch_opened_wait_queue);
  1012. spin_lock_init(&smd_pkt_devp->pa_spinlock);
  1013. mutex_init(&smd_pkt_devp->ch_lock);
  1014. mutex_init(&smd_pkt_devp->rx_lock);
  1015. mutex_init(&smd_pkt_devp->tx_lock);
  1016. wakeup_source_init(&smd_pkt_devp->pa_ws, smd_pkt_devp->dev_name);
  1017. INIT_WORK(&smd_pkt_devp->packet_arrival_work, packet_arrival_worker);
  1018. init_completion(&smd_pkt_devp->ch_allocated);
  1019. cdev_init(&smd_pkt_devp->cdev, &smd_pkt_fops);
  1020. smd_pkt_devp->cdev.owner = THIS_MODULE;
  1021. r = cdev_add(&smd_pkt_devp->cdev, (smd_pkt_number + i), 1);
  1022. if (r) {
  1023. pr_err("%s: cdev_add() failed for smd_pkt_dev id:%d ret:%i\n",
  1024. __func__, i, r);
  1025. return r;
  1026. }
  1027. smd_pkt_devp->devicep =
  1028. device_create(smd_pkt_classp,
  1029. NULL,
  1030. (smd_pkt_number + i),
  1031. NULL,
  1032. smd_pkt_devp->dev_name);
  1033. if (IS_ERR_OR_NULL(smd_pkt_devp->devicep)) {
  1034. pr_err("%s: device_create() failed for smd_pkt_dev id:%d\n",
  1035. __func__, i);
  1036. r = -ENOMEM;
  1037. cdev_del(&smd_pkt_devp->cdev);
  1038. wakeup_source_trash(&smd_pkt_devp->pa_ws);
  1039. return r;
  1040. }
  1041. if (device_create_file(smd_pkt_devp->devicep,
  1042. &dev_attr_open_timeout))
  1043. pr_err("%s: unable to create device attr for smd_pkt_dev id:%d\n",
  1044. __func__, i);
  1045. if (!strcmp(smd_pkt_devp->ch_name, "LOOPBACK")) {
  1046. if (device_create_file(smd_pkt_devp->devicep,
  1047. &dev_attr_loopback_edge))
  1048. pr_err("%s: unable to create device attr for smd_pkt_dev id:%d\n",
  1049. __func__, i);
  1050. }
  1051. mutex_lock(&smd_pkt_dev_lock_lha1);
  1052. list_add(&smd_pkt_devp->dev_list, &smd_pkt_dev_list);
  1053. mutex_unlock(&smd_pkt_dev_lock_lha1);
  1054. return r;
  1055. }
  1056. static void smd_pkt_core_deinit(void)
  1057. {
  1058. struct smd_pkt_dev *smd_pkt_devp;
  1059. struct smd_pkt_dev *index;
  1060. mutex_lock(&smd_pkt_dev_lock_lha1);
  1061. list_for_each_entry_safe(smd_pkt_devp, index, &smd_pkt_dev_list,
  1062. dev_list) {
  1063. cdev_del(&smd_pkt_devp->cdev);
  1064. list_del(&smd_pkt_devp->dev_list);
  1065. device_destroy(smd_pkt_classp,
  1066. MKDEV(MAJOR(smd_pkt_number), smd_pkt_devp->i));
  1067. kfree(smd_pkt_devp);
  1068. }
  1069. mutex_unlock(&smd_pkt_dev_lock_lha1);
  1070. if (!IS_ERR_OR_NULL(smd_pkt_classp))
  1071. class_destroy(smd_pkt_classp);
  1072. unregister_chrdev_region(MAJOR(smd_pkt_number), num_smd_pkt_ports);
  1073. }
  1074. static int smd_pkt_alloc_chrdev_region(void)
  1075. {
  1076. int r = alloc_chrdev_region(&smd_pkt_number,
  1077. 0,
  1078. num_smd_pkt_ports,
  1079. DEVICE_NAME);
  1080. if (r) {
  1081. pr_err("%s: alloc_chrdev_region() failed ret:%i\n",
  1082. __func__, r);
  1083. return r;
  1084. }
  1085. smd_pkt_classp = class_create(THIS_MODULE, DEVICE_NAME);
  1086. if (IS_ERR(smd_pkt_classp)) {
  1087. pr_err("%s: class_create() failed ENOMEM\n", __func__);
  1088. r = -ENOMEM;
  1089. unregister_chrdev_region(MAJOR(smd_pkt_number),
  1090. num_smd_pkt_ports);
  1091. return r;
  1092. }
  1093. return 0;
  1094. }
  1095. static int parse_smdpkt_devicetree(struct device_node *node,
  1096. struct smd_pkt_dev *smd_pkt_devp)
  1097. {
  1098. int edge;
  1099. char *key;
  1100. const char *ch_name;
  1101. const char *dev_name;
  1102. const char *remote_ss;
  1103. key = "qcom,smdpkt-remote";
  1104. remote_ss = of_get_property(node, key, NULL);
  1105. if (!remote_ss)
  1106. goto error;
  1107. edge = smd_remote_ss_to_edge(remote_ss);
  1108. if (edge < 0)
  1109. goto error;
  1110. smd_pkt_devp->edge = edge;
  1111. D_STATUS("%s: %s = %d", __func__, key, edge);
  1112. key = "qcom,smdpkt-port-name";
  1113. ch_name = of_get_property(node, key, NULL);
  1114. if (!ch_name)
  1115. goto error;
  1116. strlcpy(smd_pkt_devp->ch_name, ch_name, SMD_MAX_CH_NAME_LEN);
  1117. D_STATUS("%s ch_name = %s\n", __func__, ch_name);
  1118. key = "qcom,smdpkt-dev-name";
  1119. dev_name = of_get_property(node, key, NULL);
  1120. if (!dev_name)
  1121. goto error;
  1122. strlcpy(smd_pkt_devp->dev_name, dev_name, SMD_MAX_CH_NAME_LEN);
  1123. D_STATUS("%s dev_name = %s\n", __func__, dev_name);
  1124. return 0;
  1125. error:
  1126. pr_err("%s: missing key: %s\n", __func__, key);
  1127. return -ENODEV;
  1128. }
  1129. static int smd_pkt_devicetree_init(struct platform_device *pdev)
  1130. {
  1131. int ret;
  1132. int i = 0;
  1133. struct device_node *node;
  1134. struct smd_pkt_dev *smd_pkt_devp;
  1135. int subnode_num = 0;
  1136. for_each_child_of_node(pdev->dev.of_node, node)
  1137. ++subnode_num;
  1138. num_smd_pkt_ports = subnode_num;
  1139. ret = smd_pkt_alloc_chrdev_region();
  1140. if (ret) {
  1141. pr_err("%s: smd_pkt_alloc_chrdev_region() failed ret:%i\n",
  1142. __func__, ret);
  1143. return ret;
  1144. }
  1145. for_each_child_of_node(pdev->dev.of_node, node) {
  1146. smd_pkt_devp = kzalloc(sizeof(struct smd_pkt_dev), GFP_KERNEL);
  1147. if (IS_ERR_OR_NULL(smd_pkt_devp)) {
  1148. pr_err("%s: kzalloc() failed for smd_pkt_dev id:%d\n",
  1149. __func__, i);
  1150. ret = -ENOMEM;
  1151. goto error_destroy;
  1152. }
  1153. ret = parse_smdpkt_devicetree(node, smd_pkt_devp);
  1154. if (ret) {
  1155. pr_err(" failed to parse_smdpkt_devicetree %d\n", i);
  1156. kfree(smd_pkt_devp);
  1157. goto error_destroy;
  1158. }
  1159. ret = smd_pkt_init_add_device(smd_pkt_devp, i);
  1160. if (ret < 0) {
  1161. pr_err("add device failed for idx:%d ret=%d\n", i, ret);
  1162. kfree(smd_pkt_devp);
  1163. goto error_destroy;
  1164. }
  1165. i++;
  1166. }
  1167. INIT_DELAYED_WORK(&loopback_work, loopback_probe_worker);
  1168. D_STATUS("SMD Packet Port Driver Initialized.\n");
  1169. return 0;
  1170. error_destroy:
  1171. smd_pkt_core_deinit();
  1172. return ret;
  1173. }
  1174. static int msm_smd_pkt_probe(struct platform_device *pdev)
  1175. {
  1176. int ret;
  1177. if (pdev) {
  1178. if (pdev->dev.of_node) {
  1179. D_STATUS("%s device tree implementation\n", __func__);
  1180. ret = smd_pkt_devicetree_init(pdev);
  1181. if (ret)
  1182. pr_err("%s: device tree init failed\n",
  1183. __func__);
  1184. }
  1185. }
  1186. return 0;
  1187. }
  1188. static const struct of_device_id msm_smd_pkt_match_table[] = {
  1189. { .compatible = "qcom,smdpkt" },
  1190. {},
  1191. };
  1192. static struct platform_driver msm_smd_pkt_driver = {
  1193. .probe = msm_smd_pkt_probe,
  1194. .driver = {
  1195. .name = MODULE_NAME,
  1196. .owner = THIS_MODULE,
  1197. .of_match_table = msm_smd_pkt_match_table,
  1198. },
  1199. };
  1200. static int __init smd_pkt_init(void)
  1201. {
  1202. int rc;
  1203. INIT_LIST_HEAD(&smd_pkt_dev_list);
  1204. INIT_LIST_HEAD(&smd_pkt_driver_list);
  1205. rc = platform_driver_register(&msm_smd_pkt_driver);
  1206. if (rc) {
  1207. pr_err("%s: msm_smd_driver register failed %d\n",
  1208. __func__, rc);
  1209. return rc;
  1210. }
  1211. smd_pkt_ilctxt = ipc_log_context_create(SMD_PKT_IPC_LOG_PAGE_CNT,
  1212. "smd_pkt", 0);
  1213. return 0;
  1214. }
  1215. static void __exit smd_pkt_cleanup(void)
  1216. {
  1217. smd_pkt_core_deinit();
  1218. }
  1219. module_init(smd_pkt_init);
  1220. module_exit(smd_pkt_cleanup);
  1221. MODULE_DESCRIPTION("MSM Shared Memory Packet Port");
  1222. MODULE_LICENSE("GPL v2");