22 #include <linux/slab.h>
24 #include <linux/module.h>
26 #include <linux/device.h>
27 #include <linux/sched.h>
32 #include <linux/poll.h>
36 #define NUM_SMD_PKT_PORTS 9
37 #define DEVICE_NAME "smdpkt"
38 #define MAX_BUF_SIZE 2048
61 static dev_t smd_pkt_number;
63 static int msm_smd_pkt_debug_enable;
68 #define D_DUMP_BUFFER(prestr, cnt, buf) do { \
70 if (msm_smd_pkt_debug_enable) { \
71 pr_debug("%s", prestr); \
72 for (i = 0; i < cnt; i++) \
73 pr_debug("%.2x", buf[i]); \
78 #define D_DUMP_BUFFER(prestr, cnt, buf) do {} while (0)
82 #define DBG(x...) do { \
83 if (msm_smd_pkt_debug_enable) \
87 #define DBG(x...) do {} while (0)
94 if (!smd_pkt_devp || !smd_pkt_devp->
ch)
103 DBG(
"incomplete packet\n");
107 DBG(
"waking up reader\n");
111 static int smd_pkt_read(
struct file *
file,
char __user *
buf,
112 size_t count, loff_t *ppos)
118 DBG(
"read %d bytes\n", count);
123 if (!smd_pkt_devp || !smd_pkt_devp->
ch)
126 chl = smd_pkt_devp->
ch;
135 pr_err(
"wait returned %d\n", r);
142 if (bytes_read == 0 ||
145 DBG(
"Nothing to read\n");
146 goto wait_for_packet;
149 if (bytes_read > count) {
151 pr_info(
"packet size %d > buffer size %d", bytes_read, count);
156 if (r != bytes_read) {
158 pr_err(
"smd_read failed to read %d bytes: %d\n", bytes_read, r);
166 pr_err(
"copy_to_user failed %d\n", r);
170 DBG(
"read complete %d bytes\n", bytes_read);
171 check_and_wakeup_reader(smd_pkt_devp);
176 static int smd_pkt_write(
struct file *file,
const char __user *buf,
177 size_t count, loff_t *ppos)
185 DBG(
"writting %d bytes\n", count);
188 if (!smd_pkt_devp || !smd_pkt_devp->
ch)
194 DBG(
"Not enough space to write\n");
202 pr_err(
"copy_from_user failed %d\n", r);
209 pr_err(
"smd_write failed to write %d bytes: %d.\n", count, r);
214 DBG(
"wrote %d bytes\n", count);
218 static unsigned int smd_pkt_poll(
struct file *file,
poll_table *
wait)
221 unsigned int mask = 0;
227 DBG(
"poll waiting\n");
232 DBG(
"poll return\n");
236 static void smd_pkt_ch_notify(
void *
priv,
unsigned event)
240 if (smd_pkt_devp->
ch == 0)
246 check_and_wakeup_reader(smd_pkt_devp);
250 DBG(
"remote open\n");
261 pr_err(
"unknown event %d\n", event);
266 static char *smd_pkt_dev_name[] = {
278 static char *smd_ch_name[] = {
290 static int smd_pkt_open(
struct inode *
inode,
struct file *file)
303 r =
smd_open(smd_ch_name[smd_pkt_devp->
i],
304 &smd_pkt_devp->
ch, smd_pkt_devp,
307 pr_err(
"smd_open failed for %s, %d\n",
308 smd_ch_name[smd_pkt_devp->
i], r);
320 pr_err(
"wait returned %d\n", r);
322 smd_pkt_devp->
ch = 0;
333 static int smd_pkt_release(
struct inode *inode,
struct file *file)
344 smd_pkt_devp->
ch = 0;
353 .open = smd_pkt_open,
354 .release = smd_pkt_release,
355 .read = smd_pkt_read,
356 .write = smd_pkt_write,
357 .poll = smd_pkt_poll,
360 static int __init smd_pkt_init(
void)
368 pr_err(
"alloc_chrdev_region() failed %d\n", r);
373 if (IS_ERR(smd_pkt_classp)) {
374 r = PTR_ERR(smd_pkt_classp);
375 pr_err(
"class_create() failed %d\n", r);
382 if (!smd_pkt_devp[i]) {
383 pr_err(
"kmalloc() failed\n");
387 smd_pkt_devp[
i]->
i =
i;
401 (smd_pkt_number + i), 1);
403 pr_err(
"cdev_add() failed %d\n", r);
404 kfree(smd_pkt_devp[i]);
410 (smd_pkt_number + i),
NULL,
411 smd_pkt_dev_name[i]);
412 if (IS_ERR(smd_pkt_devp[i]->devicep)) {
413 r = PTR_ERR(smd_pkt_devp[i]->devicep);
414 pr_err(
"device_create() failed %d\n", r);
416 kfree(smd_pkt_devp[i]);
422 pr_info(
"SMD Packet Port Driver Initialized.\n");
432 kfree(smd_pkt_devp[i]);
445 static void __exit smd_pkt_cleanup(
void)
454 kfree(smd_pkt_devp[i]);