msm_smd_pkt.c 11 KB

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