blob: 28600552861f58ea6fbb0a07f042dc7b806e5f54 [file] [log] [blame]
Shalabh Jain1c99e4c2012-03-26 18:47:59 -07001/* Copyright (c) 2012, 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
13#include <linux/slab.h>
14#include <linux/init.h>
15#include <linux/uaccess.h>
16#include <linux/diagchar.h>
17#include <linux/sched.h>
18#include <linux/err.h>
Shalabh Jain5e9a92b2012-06-07 21:53:49 -070019#include <linux/delay.h>
Shalabh Jain1c99e4c2012-03-26 18:47:59 -070020#include <linux/workqueue.h>
21#include <linux/pm_runtime.h>
22#include <linux/platform_device.h>
23#include <asm/current.h>
24#ifdef CONFIG_DIAG_OVER_USB
25#include <mach/usbdiag.h>
26#endif
27#include "diagchar_hdlc.h"
28#include "diagmem.h"
29#include "diagchar.h"
30#include "diagfwd.h"
31#include "diag_dci.h"
32
33unsigned int dci_max_reg = 100;
34unsigned int dci_max_clients = 10;
35
36static void diag_smd_dci_send_req(int proc_num)
37{
38 void *buf = NULL;
39 smd_channel_t *smd_ch = NULL;
40 int i, r, found = 1;
41 int cmd_code_len = 1;
42
43 if (driver->in_busy_dci)
44 return;
45
46 if (proc_num == MODEM_PROC) {
47 buf = driver->buf_in_dci;
48 smd_ch = driver->ch_dci;
49 }
50
51 if (!smd_ch || !buf)
52 return;
53
54 r = smd_read_avail(smd_ch);
55 if (r > IN_BUF_SIZE) {
56 if (r < MAX_IN_BUF_SIZE) {
57 pr_err("diag: SMD DCI sending pkt upto %d bytes", r);
58 buf = krealloc(buf, r, GFP_KERNEL);
59 } else {
60 pr_err("diag: DCI pkt > %d bytes", MAX_IN_BUF_SIZE);
61 return;
62 }
63 }
64 if (buf && r > 0) {
65 smd_read(smd_ch, buf, r);
66 pr_debug("diag: data received ---\n");
67 for (i = 0; i < r; i++)
68 pr_debug("\t %x \t", *(((unsigned char *)buf)+i));
69
70 if (*(uint8_t *)(buf+4) != DCI_CMD_CODE)
71 cmd_code_len = 4; /* delayed response */
72 driver->write_ptr_dci->length =
73 (int)(*(uint16_t *)(buf+2)) - (4+cmd_code_len);
74 pr_debug("diag: len = %d\n", (int)(*(uint16_t *)(buf+2))
75 - (4+cmd_code_len));
76 /* look up DCI client with tag */
77 for (i = 0; i < dci_max_reg; i++) {
78 if (driver->dci_tbl[i].tag ==
79 *(int *)(buf+(4+cmd_code_len))) {
80 found = 0;
81 break;
82 }
83 }
84 if (found)
85 pr_alert("diag: No matching PID for DCI data\n");
86 pr_debug("\n diag PID = %d", driver->dci_tbl[i].pid);
87 if (driver->dci_tbl[i].pid == 0)
88 pr_alert("diag: Receiving DCI process deleted\n");
89 *(int *)(buf+4+cmd_code_len) = driver->dci_tbl[i].uid;
90 /* update len after adding UID */
91 driver->write_ptr_dci->length =
92 driver->write_ptr_dci->length + 4;
93 pr_debug("diag: data receivd, wake process\n");
94 driver->in_busy_dci = 1;
95 diag_update_sleeping_process(driver->dci_tbl[i].pid,
96 DCI_DATA_TYPE);
97 /* delete immediate response entry */
98 if (driver->buf_in_dci[8+cmd_code_len] != 0x80)
99 driver->dci_tbl[i].pid = 0;
100 for (i = 0; i < dci_max_reg; i++)
101 if (driver->dci_tbl[i].pid != 0)
102 pr_debug("diag: PID = %d, UID = %d, tag = %d\n",
103 driver->dci_tbl[i].pid, driver->dci_tbl[i].uid,
104 driver->dci_tbl[i].tag);
105 pr_debug("diag: completed clearing table\n");
106 }
107}
108
109void diag_read_smd_dci_work_fn(struct work_struct *work)
110{
111 diag_smd_dci_send_req(MODEM_PROC);
112}
113
114static void diag_smd_dci_notify(void *ctxt, unsigned event)
115{
116 queue_work(driver->diag_wq, &(driver->diag_read_smd_dci_work));
117}
118
Shalabh Jain5e9a92b2012-06-07 21:53:49 -0700119void diag_dci_notify_client(int peripheral_mask)
120{
121 int i, stat;
122
123 /* Notify the DCI process that the peripheral DCI Channel is up */
124 for (i = 0; i < MAX_DCI_CLIENT; i++) {
125 if (driver->dci_notify_tbl[i].list & peripheral_mask) {
126 pr_debug("diag: sending signal now\n");
127 stat = send_sig(driver->dci_notify_tbl[i].signal_type,
128 driver->dci_notify_tbl[i].client, 0);
129 if (stat)
130 pr_err("diag: Err send sig stat: %d\n", stat);
131 break;
132 }
133 } /* end of loop for all DCI clients */
134}
135
Shalabh Jain1c99e4c2012-03-26 18:47:59 -0700136static int diag_dci_probe(struct platform_device *pdev)
137{
138 int err = 0;
139
140 if (pdev->id == SMD_APPS_MODEM) {
141 err = smd_open("DIAG_2", &driver->ch_dci, driver,
142 diag_smd_dci_notify);
143 if (err)
144 pr_err("diag: cannot open DCI port, Id = %d, err ="
145 " %d\n", pdev->id, err);
Shalabh Jain5e9a92b2012-06-07 21:53:49 -0700146 else
147 diag_dci_notify_client(DIAG_CON_MPSS);
Shalabh Jain1c99e4c2012-03-26 18:47:59 -0700148 }
149 return err;
150}
151
152
153int diag_send_dci_pkt(struct diag_master_table entry, unsigned char *buf,
154 int len, int index)
155{
156 int i;
157
158 /* remove UID from user space pkt before sending to peripheral */
159 buf = buf + 4;
160 len = len - 4;
161 mutex_lock(&driver->dci_mutex);
162 /* prepare DCI packet */
163 driver->apps_dci_buf[0] = CONTROL_CHAR; /* start */
164 driver->apps_dci_buf[1] = 1; /* version */
165 *(uint16_t *)(driver->apps_dci_buf + 2) = len + 4 + 1; /* length */
166 driver->apps_dci_buf[4] = DCI_CMD_CODE; /* DCI ID */
167 *(int *)(driver->apps_dci_buf + 5) = driver->dci_tbl[index].tag;
168 for (i = 0; i < len; i++)
169 driver->apps_dci_buf[i+9] = *(buf+i);
170 driver->apps_dci_buf[9+len] = CONTROL_CHAR; /* end */
171
172 if (entry.client_id == MODEM_PROC && driver->ch_dci) {
173 smd_write(driver->ch_dci, driver->apps_dci_buf, len + 10);
174 i = DIAG_DCI_NO_ERROR;
175 } else {
176 pr_alert("diag: check DCI channel\n");
177 i = DIAG_DCI_SEND_DATA_FAIL;
178 }
179 mutex_unlock(&driver->dci_mutex);
180 return i;
181}
182
183int diag_register_dci_transaction(int uid)
184{
185 int i, new_dci_client = 1, ret = -1;
186
187 for (i = 0; i < dci_max_reg; i++) {
188 if (driver->dci_tbl[i].pid == current->tgid) {
189 new_dci_client = 0;
190 break;
191 }
192 }
193 mutex_lock(&driver->dci_mutex);
194 if (new_dci_client)
195 driver->num_dci_client++;
196 if (driver->num_dci_client > MAX_DCI_CLIENT) {
197 pr_info("diag: Max DCI Client limit reached\n");
198 driver->num_dci_client--;
199 mutex_unlock(&driver->dci_mutex);
200 return ret;
201 }
202 /* Make an entry in kernel DCI table */
203 driver->dci_tag++;
204 for (i = 0; i < dci_max_reg; i++) {
205 if (driver->dci_tbl[i].pid == 0) {
206 driver->dci_tbl[i].pid = current->tgid;
207 driver->dci_tbl[i].uid = uid;
208 driver->dci_tbl[i].tag = driver->dci_tag;
209 ret = i;
210 break;
211 }
212 }
213 mutex_unlock(&driver->dci_mutex);
214 return ret;
215}
216
217int diag_process_dci_client(unsigned char *buf, int len)
218{
219 unsigned char *temp = buf;
220 uint16_t subsys_cmd_code;
221 int subsys_id, cmd_code, i, ret = -1, index = -1;
222 struct diag_master_table entry;
223
224 /* enter this UID into kernel table and return index */
225 index = diag_register_dci_transaction(*(int *)temp);
226 if (index < 0) {
227 pr_alert("diag: registering new DCI transaction failed\n");
228 return DIAG_DCI_NO_REG;
229 }
230 temp += 4;
231 /* Check for registered peripheral and fwd pkt to apropriate proc */
232 cmd_code = (int)(*(char *)buf);
233 temp++;
234 subsys_id = (int)(*(char *)temp);
235 temp++;
236 subsys_cmd_code = *(uint16_t *)temp;
237 temp += 2;
238 pr_debug("diag: %d %d %d", cmd_code, subsys_id, subsys_cmd_code);
239 for (i = 0; i < diag_max_reg; i++) {
240 entry = driver->table[i];
241 if (entry.process_id != NO_PROCESS) {
242 if (entry.cmd_code == cmd_code && entry.subsys_id ==
243 subsys_id && entry.cmd_code_lo <=
244 subsys_cmd_code &&
245 entry.cmd_code_hi >= subsys_cmd_code) {
246 ret = diag_send_dci_pkt(entry, buf, len, index);
247 } else if (entry.cmd_code == 255
248 && cmd_code == 75) {
249 if (entry.subsys_id ==
250 subsys_id &&
251 entry.cmd_code_lo <=
252 subsys_cmd_code &&
253 entry.cmd_code_hi >=
254 subsys_cmd_code) {
255 ret = diag_send_dci_pkt(entry, buf, len,
256 index);
257 }
258 } else if (entry.cmd_code == 255 &&
259 entry.subsys_id == 255) {
260 if (entry.cmd_code_lo <=
261 cmd_code &&
262 entry.
263 cmd_code_hi >= cmd_code) {
264 ret = diag_send_dci_pkt(entry, buf, len,
265 index);
266 }
267 }
268 }
269 }
270 return ret;
271}
272
273static int diag_dci_runtime_suspend(struct device *dev)
274{
275 dev_dbg(dev, "pm_runtime: suspending...\n");
276 return 0;
277}
278
279static int diag_dci_runtime_resume(struct device *dev)
280{
281 dev_dbg(dev, "pm_runtime: resuming...\n");
282 return 0;
283}
284
285static const struct dev_pm_ops diag_dci_dev_pm_ops = {
286 .runtime_suspend = diag_dci_runtime_suspend,
287 .runtime_resume = diag_dci_runtime_resume,
288};
289
290struct platform_driver msm_diag_dci_driver = {
291 .probe = diag_dci_probe,
292 .driver = {
293 .name = "DIAG_2",
294 .owner = THIS_MODULE,
295 .pm = &diag_dci_dev_pm_ops,
296 },
297};
298
299int diag_dci_init(void)
300{
301 int success = 0;
302
303 driver->dci_tag = 0;
304 driver->dci_client_id = 0;
305 driver->num_dci_client = 0;
306 driver->in_busy_dci = 0;
307 mutex_init(&driver->dci_mutex);
308 if (driver->buf_in_dci == NULL) {
309 driver->buf_in_dci = kzalloc(IN_BUF_SIZE, GFP_KERNEL);
310 if (driver->buf_in_dci == NULL)
311 goto err;
312 }
313 if (driver->write_ptr_dci == NULL) {
314 driver->write_ptr_dci = kzalloc(
315 sizeof(struct diag_write_device), GFP_KERNEL);
316 if (driver->write_ptr_dci == NULL)
317 goto err;
318 }
319 if (driver->dci_tbl == NULL) {
320 driver->dci_tbl = kzalloc(dci_max_reg *
321 sizeof(struct diag_dci_tbl), GFP_KERNEL);
322 if (driver->dci_tbl == NULL)
323 goto err;
324 }
Shalabh Jain5e9a92b2012-06-07 21:53:49 -0700325 if (driver->dci_notify_tbl == NULL) {
326 driver->dci_notify_tbl = kzalloc(MAX_DCI_CLIENT *
327 sizeof(struct dci_notification_tbl), GFP_KERNEL);
328 if (driver->dci_notify_tbl == NULL)
329 goto err;
330 }
Shalabh Jain1c99e4c2012-03-26 18:47:59 -0700331 if (driver->apps_dci_buf == NULL) {
332 driver->apps_dci_buf = kzalloc(APPS_BUF_SIZE, GFP_KERNEL);
333 if (driver->apps_dci_buf == NULL)
334 goto err;
335 }
336 success = platform_driver_register(&msm_diag_dci_driver);
337 if (success) {
338 pr_err("diag: Could not register DCI driver\n");
339 goto err;
340 }
341 return DIAG_DCI_NO_ERROR;
342err:
343 pr_err("diag: Could not initialize diag DCI buffers");
344 kfree(driver->dci_tbl);
Shalabh Jain5e9a92b2012-06-07 21:53:49 -0700345 kfree(driver->dci_notify_tbl);
Shalabh Jain1c99e4c2012-03-26 18:47:59 -0700346 kfree(driver->apps_dci_buf);
347 kfree(driver->buf_in_dci);
348 kfree(driver->write_ptr_dci);
349 return DIAG_DCI_NO_REG;
350}
351
352void diag_dci_exit(void)
353{
354 smd_close(driver->ch_dci);
355 driver->ch_dci = 0;
356 platform_driver_unregister(&msm_diag_dci_driver);
357 kfree(driver->dci_tbl);
Shalabh Jain5e9a92b2012-06-07 21:53:49 -0700358 kfree(driver->dci_notify_tbl);
Shalabh Jain1c99e4c2012-03-26 18:47:59 -0700359 kfree(driver->apps_dci_buf);
360 kfree(driver->buf_in_dci);
361 kfree(driver->write_ptr_dci);
362}
363