msm_smd_pkt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /* Copyright (c) 2008-2010, 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 userspace interface to SMD packet ports.
  15. */
  16. #include <linux/slab.h>
  17. #include <linux/cdev.h>
  18. #include <linux/module.h>
  19. #include <linux/fs.h>
  20. #include <linux/device.h>
  21. #include <linux/sched.h>
  22. #include <linux/mutex.h>
  23. #include <linux/delay.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/poll.h>
  27. #include <mach/msm_smd.h>
  28. #define NUM_SMD_PKT_PORTS 9
  29. #define DEVICE_NAME "smdpkt"
  30. #define MAX_BUF_SIZE 2048
  31. struct smd_pkt_dev {
  32. struct cdev cdev;
  33. struct device *devicep;
  34. struct smd_channel *ch;
  35. int open_count;
  36. struct mutex ch_lock;
  37. struct mutex rx_lock;
  38. struct mutex tx_lock;
  39. wait_queue_head_t ch_read_wait_queue;
  40. wait_queue_head_t ch_opened_wait_queue;
  41. int i;
  42. unsigned char tx_buf[MAX_BUF_SIZE];
  43. unsigned char rx_buf[MAX_BUF_SIZE];
  44. int remote_open;
  45. } *smd_pkt_devp[NUM_SMD_PKT_PORTS];
  46. struct class *smd_pkt_classp;
  47. static dev_t smd_pkt_number;
  48. static int msm_smd_pkt_debug_enable;
  49. module_param_named(debug_enable, msm_smd_pkt_debug_enable,
  50. int, S_IRUGO | S_IWUSR | S_IWGRP);
  51. #ifdef DEBUG
  52. #define D_DUMP_BUFFER(prestr, cnt, buf) do { \
  53. int i; \
  54. if (msm_smd_pkt_debug_enable) { \
  55. pr_debug("%s", prestr); \
  56. for (i = 0; i < cnt; i++) \
  57. pr_debug("%.2x", buf[i]); \
  58. pr_debug("\n"); \
  59. } \
  60. } while (0)
  61. #else
  62. #define D_DUMP_BUFFER(prestr, cnt, buf) do {} while (0)
  63. #endif
  64. #ifdef DEBUG
  65. #define DBG(x...) do { \
  66. if (msm_smd_pkt_debug_enable) \
  67. pr_debug(x); \
  68. } while (0)
  69. #else
  70. #define DBG(x...) do {} while (0)
  71. #endif
  72. static void check_and_wakeup_reader(struct smd_pkt_dev *smd_pkt_devp)
  73. {
  74. int sz;
  75. if (!smd_pkt_devp || !smd_pkt_devp->ch)
  76. return;
  77. sz = smd_cur_packet_size(smd_pkt_devp->ch);
  78. if (sz == 0) {
  79. DBG("no packet\n");
  80. return;
  81. }
  82. if (sz > smd_read_avail(smd_pkt_devp->ch)) {
  83. DBG("incomplete packet\n");
  84. return;
  85. }
  86. DBG("waking up reader\n");
  87. wake_up_interruptible(&smd_pkt_devp->ch_read_wait_queue);
  88. }
  89. static int smd_pkt_read(struct file *file, char __user *buf,
  90. size_t count, loff_t *ppos)
  91. {
  92. int r, bytes_read;
  93. struct smd_pkt_dev *smd_pkt_devp;
  94. struct smd_channel *chl;
  95. DBG("read %d bytes\n", count);
  96. if (count > MAX_BUF_SIZE)
  97. return -EINVAL;
  98. smd_pkt_devp = file->private_data;
  99. if (!smd_pkt_devp || !smd_pkt_devp->ch)
  100. return -EINVAL;
  101. chl = smd_pkt_devp->ch;
  102. wait_for_packet:
  103. r = wait_event_interruptible(smd_pkt_devp->ch_read_wait_queue,
  104. (smd_cur_packet_size(chl) > 0 &&
  105. smd_read_avail(chl) >=
  106. smd_cur_packet_size(chl)));
  107. if (r < 0) {
  108. if (r != -ERESTARTSYS)
  109. pr_err("wait returned %d\n", r);
  110. return r;
  111. }
  112. mutex_lock(&smd_pkt_devp->rx_lock);
  113. bytes_read = smd_cur_packet_size(smd_pkt_devp->ch);
  114. if (bytes_read == 0 ||
  115. bytes_read < smd_read_avail(smd_pkt_devp->ch)) {
  116. mutex_unlock(&smd_pkt_devp->rx_lock);
  117. DBG("Nothing to read\n");
  118. goto wait_for_packet;
  119. }
  120. if (bytes_read > count) {
  121. mutex_unlock(&smd_pkt_devp->rx_lock);
  122. pr_info("packet size %d > buffer size %d", bytes_read, count);
  123. return -EINVAL;
  124. }
  125. r = smd_read(smd_pkt_devp->ch, smd_pkt_devp->rx_buf, bytes_read);
  126. if (r != bytes_read) {
  127. mutex_unlock(&smd_pkt_devp->rx_lock);
  128. pr_err("smd_read failed to read %d bytes: %d\n", bytes_read, r);
  129. return -EIO;
  130. }
  131. D_DUMP_BUFFER("read: ", bytes_read, smd_pkt_devp->rx_buf);
  132. r = copy_to_user(buf, smd_pkt_devp->rx_buf, bytes_read);
  133. mutex_unlock(&smd_pkt_devp->rx_lock);
  134. if (r) {
  135. pr_err("copy_to_user failed %d\n", r);
  136. return -EFAULT;
  137. }
  138. DBG("read complete %d bytes\n", bytes_read);
  139. check_and_wakeup_reader(smd_pkt_devp);
  140. return bytes_read;
  141. }
  142. static int smd_pkt_write(struct file *file, const char __user *buf,
  143. size_t count, loff_t *ppos)
  144. {
  145. int r;
  146. struct smd_pkt_dev *smd_pkt_devp;
  147. if (count > MAX_BUF_SIZE)
  148. return -EINVAL;
  149. DBG("writting %d bytes\n", count);
  150. smd_pkt_devp = file->private_data;
  151. if (!smd_pkt_devp || !smd_pkt_devp->ch)
  152. return -EINVAL;
  153. mutex_lock(&smd_pkt_devp->tx_lock);
  154. if (smd_write_avail(smd_pkt_devp->ch) < count) {
  155. mutex_unlock(&smd_pkt_devp->tx_lock);
  156. DBG("Not enough space to write\n");
  157. return -ENOMEM;
  158. }
  159. D_DUMP_BUFFER("write: ", count, buf);
  160. r = copy_from_user(smd_pkt_devp->tx_buf, buf, count);
  161. if (r) {
  162. mutex_unlock(&smd_pkt_devp->tx_lock);
  163. pr_err("copy_from_user failed %d\n", r);
  164. return -EFAULT;
  165. }
  166. r = smd_write(smd_pkt_devp->ch, smd_pkt_devp->tx_buf, count);
  167. if (r != count) {
  168. mutex_unlock(&smd_pkt_devp->tx_lock);
  169. pr_err("smd_write failed to write %d bytes: %d.\n", count, r);
  170. return -EIO;
  171. }
  172. mutex_unlock(&smd_pkt_devp->tx_lock);
  173. DBG("wrote %d bytes\n", count);
  174. return count;
  175. }
  176. static unsigned int smd_pkt_poll(struct file *file, poll_table *wait)
  177. {
  178. struct smd_pkt_dev *smd_pkt_devp;
  179. unsigned int mask = 0;
  180. smd_pkt_devp = file->private_data;
  181. if (!smd_pkt_devp)
  182. return POLLERR;
  183. DBG("poll waiting\n");
  184. poll_wait(file, &smd_pkt_devp->ch_read_wait_queue, wait);
  185. if (smd_read_avail(smd_pkt_devp->ch))
  186. mask |= POLLIN | POLLRDNORM;
  187. DBG("poll return\n");
  188. return mask;
  189. }
  190. static void smd_pkt_ch_notify(void *priv, unsigned event)
  191. {
  192. struct smd_pkt_dev *smd_pkt_devp = priv;
  193. if (smd_pkt_devp->ch == 0)
  194. return;
  195. switch (event) {
  196. case SMD_EVENT_DATA:
  197. DBG("data\n");
  198. check_and_wakeup_reader(smd_pkt_devp);
  199. break;
  200. case SMD_EVENT_OPEN:
  201. DBG("remote open\n");
  202. smd_pkt_devp->remote_open = 1;
  203. wake_up_interruptible(&smd_pkt_devp->ch_opened_wait_queue);
  204. break;
  205. case SMD_EVENT_CLOSE:
  206. smd_pkt_devp->remote_open = 0;
  207. pr_info("remote closed\n");
  208. break;
  209. default:
  210. pr_err("unknown event %d\n", event);
  211. break;
  212. }
  213. }
  214. static char *smd_pkt_dev_name[] = {
  215. "smdcntl0",
  216. "smdcntl1",
  217. "smdcntl2",
  218. "smdcntl3",
  219. "smdcntl4",
  220. "smdcntl5",
  221. "smdcntl6",
  222. "smdcntl7",
  223. "smd22",
  224. };
  225. static char *smd_ch_name[] = {
  226. "DATA5_CNTL",
  227. "DATA6_CNTL",
  228. "DATA7_CNTL",
  229. "DATA8_CNTL",
  230. "DATA9_CNTL",
  231. "DATA12_CNTL",
  232. "DATA13_CNTL",
  233. "DATA14_CNTL",
  234. "DATA22",
  235. };
  236. static int smd_pkt_open(struct inode *inode, struct file *file)
  237. {
  238. int r = 0;
  239. struct smd_pkt_dev *smd_pkt_devp;
  240. smd_pkt_devp = container_of(inode->i_cdev, struct smd_pkt_dev, cdev);
  241. if (!smd_pkt_devp)
  242. return -EINVAL;
  243. file->private_data = smd_pkt_devp;
  244. mutex_lock(&smd_pkt_devp->ch_lock);
  245. if (smd_pkt_devp->open_count == 0) {
  246. r = smd_open(smd_ch_name[smd_pkt_devp->i],
  247. &smd_pkt_devp->ch, smd_pkt_devp,
  248. smd_pkt_ch_notify);
  249. if (r < 0) {
  250. pr_err("smd_open failed for %s, %d\n",
  251. smd_ch_name[smd_pkt_devp->i], r);
  252. goto out;
  253. }
  254. r = wait_event_interruptible_timeout(
  255. smd_pkt_devp->ch_opened_wait_queue,
  256. smd_pkt_devp->remote_open,
  257. msecs_to_jiffies(2 * HZ));
  258. if (r == 0)
  259. r = -ETIMEDOUT;
  260. if (r < 0) {
  261. pr_err("wait returned %d\n", r);
  262. smd_close(smd_pkt_devp->ch);
  263. smd_pkt_devp->ch = 0;
  264. } else {
  265. smd_pkt_devp->open_count++;
  266. r = 0;
  267. }
  268. }
  269. out:
  270. mutex_unlock(&smd_pkt_devp->ch_lock);
  271. return r;
  272. }
  273. static int smd_pkt_release(struct inode *inode, struct file *file)
  274. {
  275. int r = 0;
  276. struct smd_pkt_dev *smd_pkt_devp = file->private_data;
  277. if (!smd_pkt_devp)
  278. return -EINVAL;
  279. mutex_lock(&smd_pkt_devp->ch_lock);
  280. if (--smd_pkt_devp->open_count == 0) {
  281. r = smd_close(smd_pkt_devp->ch);
  282. smd_pkt_devp->ch = 0;
  283. }
  284. mutex_unlock(&smd_pkt_devp->ch_lock);
  285. return r;
  286. }
  287. static const struct file_operations smd_pkt_fops = {
  288. .owner = THIS_MODULE,
  289. .open = smd_pkt_open,
  290. .release = smd_pkt_release,
  291. .read = smd_pkt_read,
  292. .write = smd_pkt_write,
  293. .poll = smd_pkt_poll,
  294. };
  295. static int __init smd_pkt_init(void)
  296. {
  297. int i;
  298. int r;
  299. r = alloc_chrdev_region(&smd_pkt_number, 0,
  300. NUM_SMD_PKT_PORTS, DEVICE_NAME);
  301. if (r) {
  302. pr_err("alloc_chrdev_region() failed %d\n", r);
  303. return r;
  304. }
  305. smd_pkt_classp = class_create(THIS_MODULE, DEVICE_NAME);
  306. if (IS_ERR(smd_pkt_classp)) {
  307. r = PTR_ERR(smd_pkt_classp);
  308. pr_err("class_create() failed %d\n", r);
  309. goto unreg_chardev;
  310. }
  311. for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
  312. smd_pkt_devp[i] = kzalloc(sizeof(struct smd_pkt_dev),
  313. GFP_KERNEL);
  314. if (!smd_pkt_devp[i]) {
  315. pr_err("kmalloc() failed\n");
  316. goto clean_cdevs;
  317. }
  318. smd_pkt_devp[i]->i = i;
  319. init_waitqueue_head(&smd_pkt_devp[i]->ch_read_wait_queue);
  320. smd_pkt_devp[i]->remote_open = 0;
  321. init_waitqueue_head(&smd_pkt_devp[i]->ch_opened_wait_queue);
  322. mutex_init(&smd_pkt_devp[i]->ch_lock);
  323. mutex_init(&smd_pkt_devp[i]->rx_lock);
  324. mutex_init(&smd_pkt_devp[i]->tx_lock);
  325. cdev_init(&smd_pkt_devp[i]->cdev, &smd_pkt_fops);
  326. smd_pkt_devp[i]->cdev.owner = THIS_MODULE;
  327. r = cdev_add(&smd_pkt_devp[i]->cdev,
  328. (smd_pkt_number + i), 1);
  329. if (r) {
  330. pr_err("cdev_add() failed %d\n", r);
  331. kfree(smd_pkt_devp[i]);
  332. goto clean_cdevs;
  333. }
  334. smd_pkt_devp[i]->devicep =
  335. device_create(smd_pkt_classp, NULL,
  336. (smd_pkt_number + i), NULL,
  337. smd_pkt_dev_name[i]);
  338. if (IS_ERR(smd_pkt_devp[i]->devicep)) {
  339. r = PTR_ERR(smd_pkt_devp[i]->devicep);
  340. pr_err("device_create() failed %d\n", r);
  341. cdev_del(&smd_pkt_devp[i]->cdev);
  342. kfree(smd_pkt_devp[i]);
  343. goto clean_cdevs;
  344. }
  345. }
  346. pr_info("SMD Packet Port Driver Initialized.\n");
  347. return 0;
  348. clean_cdevs:
  349. if (i > 0) {
  350. while (--i >= 0) {
  351. mutex_destroy(&smd_pkt_devp[i]->ch_lock);
  352. mutex_destroy(&smd_pkt_devp[i]->rx_lock);
  353. mutex_destroy(&smd_pkt_devp[i]->tx_lock);
  354. cdev_del(&smd_pkt_devp[i]->cdev);
  355. kfree(smd_pkt_devp[i]);
  356. device_destroy(smd_pkt_classp,
  357. MKDEV(MAJOR(smd_pkt_number), i));
  358. }
  359. }
  360. class_destroy(smd_pkt_classp);
  361. unreg_chardev:
  362. unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS);
  363. return r;
  364. }
  365. module_init(smd_pkt_init);
  366. static void __exit smd_pkt_cleanup(void)
  367. {
  368. int i;
  369. for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
  370. mutex_destroy(&smd_pkt_devp[i]->ch_lock);
  371. mutex_destroy(&smd_pkt_devp[i]->rx_lock);
  372. mutex_destroy(&smd_pkt_devp[i]->tx_lock);
  373. cdev_del(&smd_pkt_devp[i]->cdev);
  374. kfree(smd_pkt_devp[i]);
  375. device_destroy(smd_pkt_classp,
  376. MKDEV(MAJOR(smd_pkt_number), i));
  377. }
  378. class_destroy(smd_pkt_classp);
  379. unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS);
  380. }
  381. module_exit(smd_pkt_cleanup);
  382. MODULE_DESCRIPTION("MSM Shared Memory Packet Port");
  383. MODULE_LICENSE("GPL v2");