blob: d1b2b2d7b3d6c1acaba7d4cec89cf20a615c4c67 [file] [log] [blame]
Duy Truong790f06d2013-02-13 16:38:12 -08001/* Copyright (c) 2008-2009, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
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 NMEA Driver -- Provides GPS NMEA device to SMD port interface.
15 *
16 */
17
18#include <linux/module.h>
19#include <linux/fs.h>
20#include <linux/cdev.h>
21#include <linux/device.h>
22#include <linux/wait.h>
23#include <linux/sched.h>
24#include <linux/miscdevice.h>
25#include <linux/workqueue.h>
26#include <linux/uaccess.h>
27
28#include <mach/msm_smd.h>
29
30#define MAX_BUF_SIZE 200
31
32static DEFINE_MUTEX(nmea_ch_lock);
33static DEFINE_MUTEX(nmea_rx_buf_lock);
34
35static DECLARE_WAIT_QUEUE_HEAD(nmea_wait_queue);
36
37struct nmea_device_t {
38 struct miscdevice misc;
39
40 struct smd_channel *ch;
41
42 unsigned char rx_buf[MAX_BUF_SIZE];
43 unsigned int bytes_read;
44};
45
46struct nmea_device_t *nmea_devp;
47
48static void nmea_work_func(struct work_struct *ws)
49{
50 int sz;
51
52 for (;;) {
53 sz = smd_cur_packet_size(nmea_devp->ch);
54 if (sz == 0)
55 break;
56 if (sz > smd_read_avail(nmea_devp->ch))
57 break;
58 if (sz > MAX_BUF_SIZE) {
59 smd_read(nmea_devp->ch, 0, sz);
60 continue;
61 }
62
63 mutex_lock(&nmea_rx_buf_lock);
64 if (smd_read(nmea_devp->ch, nmea_devp->rx_buf, sz) != sz) {
65 mutex_unlock(&nmea_rx_buf_lock);
66 printk(KERN_ERR "nmea: not enough data?!\n");
67 continue;
68 }
69 nmea_devp->bytes_read = sz;
70 mutex_unlock(&nmea_rx_buf_lock);
71 wake_up_interruptible(&nmea_wait_queue);
72 }
73}
74
75struct workqueue_struct *nmea_wq;
76static DECLARE_WORK(nmea_work, nmea_work_func);
77
78static void nmea_notify(void *priv, unsigned event)
79{
80 switch (event) {
81 case SMD_EVENT_DATA: {
82 int sz;
83 sz = smd_cur_packet_size(nmea_devp->ch);
84 if ((sz > 0) && (sz <= smd_read_avail(nmea_devp->ch)))
85 queue_work(nmea_wq, &nmea_work);
86 break;
87 }
88 case SMD_EVENT_OPEN:
89 printk(KERN_INFO "nmea: smd opened\n");
90 break;
91 case SMD_EVENT_CLOSE:
92 printk(KERN_INFO "nmea: smd closed\n");
93 break;
94 }
95}
96
97static ssize_t nmea_read(struct file *fp, char __user *buf,
98 size_t count, loff_t *pos)
99{
100 int r;
101 int bytes_read;
102
103 r = wait_event_interruptible(nmea_wait_queue,
104 nmea_devp->bytes_read);
105 if (r < 0) {
106 /* qualify error message */
107 if (r != -ERESTARTSYS) {
108 /* we get this anytime a signal comes in */
109 printk(KERN_ERR "ERROR:%s:%i:%s: "
110 "wait_event_interruptible ret %i\n",
111 __FILE__,
112 __LINE__,
113 __func__,
114 r
115 );
116 }
117 return r;
118 }
119
120 mutex_lock(&nmea_rx_buf_lock);
121 bytes_read = nmea_devp->bytes_read;
122 nmea_devp->bytes_read = 0;
123 r = copy_to_user(buf, nmea_devp->rx_buf, bytes_read);
124 mutex_unlock(&nmea_rx_buf_lock);
125
126 if (r > 0) {
127 printk(KERN_ERR "ERROR:%s:%i:%s: "
128 "copy_to_user could not copy %i bytes.\n",
129 __FILE__,
130 __LINE__,
131 __func__,
132 r);
133 return r;
134 }
135
136 return bytes_read;
137}
138
139static int nmea_open(struct inode *ip, struct file *fp)
140{
141 int r = 0;
142
143 mutex_lock(&nmea_ch_lock);
144 if (nmea_devp->ch == 0)
145 r = smd_open("GPSNMEA", &nmea_devp->ch, nmea_devp, nmea_notify);
146 mutex_unlock(&nmea_ch_lock);
147
148 return r;
149}
150
151static int nmea_release(struct inode *ip, struct file *fp)
152{
153 int r = 0;
154
155 mutex_lock(&nmea_ch_lock);
156 if (nmea_devp->ch != 0) {
157 r = smd_close(nmea_devp->ch);
158 nmea_devp->ch = 0;
159 }
160 mutex_unlock(&nmea_ch_lock);
161
162 return r;
163}
164
165static const struct file_operations nmea_fops = {
166 .owner = THIS_MODULE,
167 .read = nmea_read,
168 .open = nmea_open,
169 .release = nmea_release,
170};
171
172static struct nmea_device_t nmea_device = {
173 .misc = {
174 .minor = MISC_DYNAMIC_MINOR,
175 .name = "nmea",
176 .fops = &nmea_fops,
177 }
178};
179
180static void __exit nmea_exit(void)
181{
182 destroy_workqueue(nmea_wq);
183 misc_deregister(&nmea_device.misc);
184}
185
186static int __init nmea_init(void)
187{
188 int ret;
189
190 nmea_device.bytes_read = 0;
191 nmea_devp = &nmea_device;
192
193 nmea_wq = create_singlethread_workqueue("nmea");
194 if (nmea_wq == 0)
195 return -ENOMEM;
196
197 ret = misc_register(&nmea_device.misc);
198 return ret;
199}
200
201module_init(nmea_init);
202module_exit(nmea_exit);
203
204MODULE_DESCRIPTION("MSM Shared Memory NMEA Driver");
205MODULE_LICENSE("GPL v2");