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