blob: 685365467f261afe811b071fe038a8626783c5bc [file] [log] [blame]
Dixon Peterson32e70bb2011-12-16 13:26:45 -08001/* Copyright (c) 2008-2012, Code Aurora Forum. 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 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070012#include <linux/slab.h>
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/err.h>
17#include <linux/platform_device.h>
18#include <linux/sched.h>
19#include <linux/workqueue.h>
20#include <linux/pm_runtime.h>
21#include <linux/diagchar.h>
22#include <linux/delay.h>
23#include <linux/reboot.h>
24#ifdef CONFIG_DIAG_OVER_USB
25#include <mach/usbdiag.h>
26#endif
27#include <mach/msm_smd.h>
28#include <mach/socinfo.h>
29#include <mach/restart.h>
30#include "diagmem.h"
31#include "diagchar.h"
32#include "diagfwd.h"
33#include "diagfwd_cntl.h"
34#include "diagchar_hdlc.h"
35#ifdef CONFIG_DIAG_SDIO_PIPE
36#include "diagfwd_sdio.h"
37#endif
38#define MODE_CMD 41
39#define RESET_ID 2
40
41int diag_debug_buf_idx;
42unsigned char diag_debug_buf[1024];
43static unsigned int buf_tbl_size = 8; /*Number of entries in table of buffers */
44struct diag_master_table entry;
Shalabh Jainc6c2b1d2011-12-20 04:32:34 -080045smd_channel_t *ch_temp, *chqdsp_temp, *ch_wcnss_temp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070046struct diag_send_desc_type send = { NULL, NULL, DIAG_STATE_START, 0 };
47struct diag_hdlc_dest_type enc = { NULL, NULL, 0 };
48
49#define ENCODE_RSP_AND_SEND(buf_length) \
50do { \
51 send.state = DIAG_STATE_START; \
52 send.pkt = driver->apps_rsp_buf; \
53 send.last = (void *)(driver->apps_rsp_buf + buf_length); \
54 send.terminate = 1; \
55 if (!driver->in_busy_1) { \
56 enc.dest = driver->buf_in_1; \
57 enc.dest_last = (void *)(driver->buf_in_1 + 499); \
58 diag_hdlc_encode(&send, &enc); \
59 driver->write_ptr_1->buf = driver->buf_in_1; \
60 driver->write_ptr_1->length = (int)(enc.dest - \
61 (void *)(driver->buf_in_1)); \
Shalabh Jain3893bf92011-09-18 18:37:16 -070062 driver->in_busy_1 = 1; \
Shalabh Jain69890aa2011-10-10 12:59:16 -070063 diag_device_write(driver->buf_in_1, MODEM_DATA, \
64 driver->write_ptr_1); \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070065 memset(driver->apps_rsp_buf, '\0', 500); \
66 } \
67} while (0)
68
69#define CHK_OVERFLOW(bufStart, start, end, length) \
70((bufStart <= start) && (end - start >= length)) ? 1 : 0
71
Shalabh Jainfb8e3c12011-10-19 17:29:42 -070072int chk_config_get_id(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070073{
Shalabh Jain482bf122011-12-06 03:54:47 -080074 /* For all Fusion targets, Modem will always be present */
75 if (machine_is_msm8x60_fusion() || machine_is_msm8x60_fusn_ffa())
76 return 0;
77
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070078 switch (socinfo_get_id()) {
79 case APQ8060_MACHINE_ID:
80 case MSM8660_MACHINE_ID:
81 return APQ8060_TOOLS_ID;
82 case AO8960_MACHINE_ID:
83 return AO8960_TOOLS_ID;
Shalabh Jainfb8e3c12011-10-19 17:29:42 -070084 case APQ8064_MACHINE_ID:
85 return APQ8064_TOOLS_ID;
Shalabh Jain2f4cea62011-12-19 21:50:03 -080086 case MSM8930_MACHINE_ID:
87 return MSM8930_TOOLS_ID;
Shalabh Jainfb8e3c12011-10-19 17:29:42 -070088 default:
89 return 0;
90 }
91}
92
93/*
Shalabh Jain2c783cb2012-01-15 11:09:29 +053094 * This will return TRUE for targets which support apps only mode.
Shalabh Jainfb8e3c12011-10-19 17:29:42 -070095 * This applies to 8960 and newer targets.
96 */
97int chk_apps_only(void)
98{
99 switch (socinfo_get_id()) {
100 case AO8960_MACHINE_ID:
101 case APQ8064_MACHINE_ID:
Shalabh Jain2f4cea62011-12-19 21:50:03 -0800102 case MSM8930_MACHINE_ID:
103 case MSM8630_MACHINE_ID:
104 case MSM8230_MACHINE_ID:
105 case APQ8030_MACHINE_ID:
106 case MSM8627_MACHINE_ID:
107 case MSM8227_MACHINE_ID:
Shalabh Jainfb8e3c12011-10-19 17:29:42 -0700108 return 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109 default:
110 return 0;
111 }
112}
113
114void __diag_smd_send_req(void)
115{
116 void *buf = NULL;
117 int *in_busy_ptr = NULL;
118 struct diag_request *write_ptr_modem = NULL;
119
120 if (!driver->in_busy_1) {
121 buf = driver->buf_in_1;
122 write_ptr_modem = driver->write_ptr_1;
123 in_busy_ptr = &(driver->in_busy_1);
124 } else if (!driver->in_busy_2) {
125 buf = driver->buf_in_2;
126 write_ptr_modem = driver->write_ptr_2;
127 in_busy_ptr = &(driver->in_busy_2);
128 }
129
130 if (driver->ch && buf) {
131 int r = smd_read_avail(driver->ch);
132
133 if (r > IN_BUF_SIZE) {
134 if (r < MAX_IN_BUF_SIZE) {
135 pr_err("diag: SMD sending in "
136 "packets upto %d bytes", r);
137 buf = krealloc(buf, r, GFP_KERNEL);
138 } else {
139 pr_err("diag: SMD sending in "
140 "packets more than %d bytes", MAX_IN_BUF_SIZE);
141 return;
142 }
143 }
144 if (r > 0) {
145 if (!buf)
146 pr_info("Out of diagmem for Modem\n");
147 else {
148 APPEND_DEBUG('i');
149 smd_read(driver->ch, buf, r);
150 APPEND_DEBUG('j');
151 write_ptr_modem->length = r;
152 *in_busy_ptr = 1;
153 diag_device_write(buf, MODEM_DATA,
154 write_ptr_modem);
155 }
156 }
157 }
158}
159
160int diag_device_write(void *buf, int proc_num, struct diag_request *write_ptr)
161{
162 int i, err = 0;
163
164 if (driver->logging_mode == MEMORY_DEVICE_MODE) {
165 if (proc_num == APPS_DATA) {
166 for (i = 0; i < driver->poolsize_write_struct; i++)
167 if (driver->buf_tbl[i].length == 0) {
168 driver->buf_tbl[i].buf = buf;
169 driver->buf_tbl[i].length =
170 driver->used;
171#ifdef DIAG_DEBUG
172 pr_debug("diag: ENQUEUE buf ptr"
173 " and length is %x , %d\n",
174 (unsigned int)(driver->buf_
175 tbl[i].buf), driver->buf_tbl[i].length);
176#endif
177 break;
178 }
179 }
180 for (i = 0; i < driver->num_clients; i++)
181 if (driver->client_map[i].pid ==
182 driver->logging_process_id)
183 break;
184 if (i < driver->num_clients) {
Shalabh Jain69890aa2011-10-10 12:59:16 -0700185 driver->data_ready[i] |= USER_SPACE_LOG_TYPE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186 wake_up_interruptible(&driver->wait_q);
187 } else
188 return -EINVAL;
189 } else if (driver->logging_mode == NO_LOGGING_MODE) {
190 if (proc_num == MODEM_DATA) {
191 driver->in_busy_1 = 0;
192 driver->in_busy_2 = 0;
193 queue_work(driver->diag_wq, &(driver->
194 diag_read_smd_work));
195 } else if (proc_num == QDSP_DATA) {
196 driver->in_busy_qdsp_1 = 0;
197 driver->in_busy_qdsp_2 = 0;
198 queue_work(driver->diag_wq, &(driver->
199 diag_read_smd_qdsp_work));
200 } else if (proc_num == WCNSS_DATA) {
201 driver->in_busy_wcnss = 0;
202 queue_work(driver->diag_wq, &(driver->
203 diag_read_smd_wcnss_work));
204 }
Shalabh Jain482bf122011-12-06 03:54:47 -0800205#ifdef CONFIG_DIAG_SDIO_PIPE
206 else if (proc_num == SDIO_DATA) {
207 driver->in_busy_sdio = 0;
208 queue_work(driver->diag_sdio_wq,
209 &(driver->diag_read_sdio_work));
210 }
211#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212 err = -1;
213 }
214#ifdef CONFIG_DIAG_OVER_USB
215 else if (driver->logging_mode == USB_MODE) {
216 if (proc_num == APPS_DATA) {
217 driver->write_ptr_svc = (struct diag_request *)
218 (diagmem_alloc(driver, sizeof(struct diag_request),
219 POOL_TYPE_WRITE_STRUCT));
220 if (driver->write_ptr_svc) {
221 driver->write_ptr_svc->length = driver->used;
222 driver->write_ptr_svc->buf = buf;
223 err = usb_diag_write(driver->legacy_ch,
224 driver->write_ptr_svc);
225 } else
226 err = -1;
227 } else if (proc_num == MODEM_DATA) {
228 write_ptr->buf = buf;
229#ifdef DIAG_DEBUG
230 printk(KERN_INFO "writing data to USB,"
231 "pkt length %d\n", write_ptr->length);
232 print_hex_dump(KERN_DEBUG, "Written Packet Data to"
233 " USB: ", 16, 1, DUMP_PREFIX_ADDRESS,
234 buf, write_ptr->length, 1);
235#endif /* DIAG DEBUG */
236 err = usb_diag_write(driver->legacy_ch, write_ptr);
237 } else if (proc_num == QDSP_DATA) {
238 write_ptr->buf = buf;
239 err = usb_diag_write(driver->legacy_ch, write_ptr);
240 } else if (proc_num == WCNSS_DATA) {
241 write_ptr->buf = buf;
242 err = usb_diag_write(driver->legacy_ch, write_ptr);
243 }
244#ifdef CONFIG_DIAG_SDIO_PIPE
245 else if (proc_num == SDIO_DATA) {
246 if (machine_is_msm8x60_fusion() ||
Shalabh Jain482bf122011-12-06 03:54:47 -0800247 machine_is_msm8x60_fusn_ffa()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248 write_ptr->buf = buf;
249 err = usb_diag_write(driver->mdm_ch, write_ptr);
250 } else
Dixon Peterson32e70bb2011-12-16 13:26:45 -0800251 pr_err("diag: Incorrect sdio data "
252 "while USB write\n");
253 }
254#endif
255#ifdef CONFIG_DIAG_HSIC_PIPE
256 else if (proc_num == HSIC_DATA) {
257 if (driver->hsic_device_enabled) {
258 write_ptr->buf = buf;
259 err = usb_diag_write(driver->mdm_ch, write_ptr);
260 } else
261 pr_err("diag: Incorrect hsic data "
262 "while USB write\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263 }
264#endif
265 APPEND_DEBUG('d');
266 }
267#endif /* DIAG OVER USB */
268 return err;
269}
270
271void __diag_smd_wcnss_send_req(void)
272{
273 void *buf = driver->buf_in_wcnss;
274 int *in_busy_wcnss_ptr = &(driver->in_busy_wcnss);
275 struct diag_request *write_ptr_wcnss = driver->write_ptr_wcnss;
276
Shalabh Jain3893bf92011-09-18 18:37:16 -0700277 if ((!driver->in_busy_wcnss) && driver->ch_wcnss && buf) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700278 int r = smd_read_avail(driver->ch_wcnss);
279 if (r > IN_BUF_SIZE) {
280 if (r < MAX_IN_BUF_SIZE) {
281 pr_err("diag: wcnss packets > %d bytes", r);
282 buf = krealloc(buf, r, GFP_KERNEL);
283 } else {
284 pr_err("diag: wcnss pkt > %d", MAX_IN_BUF_SIZE);
285 return;
286 }
287 }
288 if (r > 0) {
289 if (!buf) {
290 pr_err("Out of diagmem for wcnss\n");
291 } else {
292 APPEND_DEBUG('i');
293 smd_read(driver->ch_wcnss, buf, r);
294 APPEND_DEBUG('j');
295 write_ptr_wcnss->length = r;
296 *in_busy_wcnss_ptr = 1;
297 diag_device_write(buf, WCNSS_DATA,
298 write_ptr_wcnss);
299 }
300 }
301 }
302}
303
304void __diag_smd_qdsp_send_req(void)
305{
306 void *buf = NULL;
307 int *in_busy_qdsp_ptr = NULL;
308 struct diag_request *write_ptr_qdsp = NULL;
309
310 if (!driver->in_busy_qdsp_1) {
311 buf = driver->buf_in_qdsp_1;
312 write_ptr_qdsp = driver->write_ptr_qdsp_1;
313 in_busy_qdsp_ptr = &(driver->in_busy_qdsp_1);
314 } else if (!driver->in_busy_qdsp_2) {
315 buf = driver->buf_in_qdsp_2;
316 write_ptr_qdsp = driver->write_ptr_qdsp_2;
317 in_busy_qdsp_ptr = &(driver->in_busy_qdsp_2);
318 }
319
320 if (driver->chqdsp && buf) {
321 int r = smd_read_avail(driver->chqdsp);
322
323 if (r > IN_BUF_SIZE) {
324 if (r < MAX_IN_BUF_SIZE) {
325 pr_err("diag: SMD sending in "
326 "packets upto %d bytes", r);
327 buf = krealloc(buf, r, GFP_KERNEL);
328 } else {
329 pr_err("diag: SMD sending in "
330 "packets more than %d bytes", MAX_IN_BUF_SIZE);
331 return;
332 }
333 }
334 if (r > 0) {
335 if (!buf)
336 printk(KERN_INFO "Out of diagmem for QDSP\n");
337 else {
338 APPEND_DEBUG('i');
339 smd_read(driver->chqdsp, buf, r);
340 APPEND_DEBUG('j');
341 write_ptr_qdsp->length = r;
342 *in_busy_qdsp_ptr = 1;
343 diag_device_write(buf, QDSP_DATA,
344 write_ptr_qdsp);
345 }
346 }
347 }
348}
349
350static void diag_print_mask_table(void)
351{
352/* Enable this to print mask table when updated */
353#ifdef MASK_DEBUG
354 int first;
355 int last;
356 uint8_t *ptr = driver->msg_masks;
357 int i = 0;
358
359 while (*(uint32_t *)(ptr + 4)) {
360 first = *(uint32_t *)ptr;
361 ptr += 4;
362 last = *(uint32_t *)ptr;
363 ptr += 4;
364 printk(KERN_INFO "SSID %d - %d\n", first, last);
365 for (i = 0 ; i <= last - first ; i++)
366 printk(KERN_INFO "MASK:%x\n", *((uint32_t *)ptr + i));
367 ptr += ((last - first) + 1)*4;
368
369 }
370#endif
371}
372
373static void diag_update_msg_mask(int start, int end , uint8_t *buf)
374{
375 int found = 0;
376 int first;
377 int last;
378 uint8_t *ptr = driver->msg_masks;
379 uint8_t *ptr_buffer_start = &(*(driver->msg_masks));
380 uint8_t *ptr_buffer_end = &(*(driver->msg_masks)) + MSG_MASK_SIZE;
381
382 mutex_lock(&driver->diagchar_mutex);
383 /* First SSID can be zero : So check that last is non-zero */
Shalabh Jain2c783cb2012-01-15 11:09:29 +0530384
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700385 while (*(uint32_t *)(ptr + 4)) {
386 first = *(uint32_t *)ptr;
387 ptr += 4;
388 last = *(uint32_t *)ptr;
389 ptr += 4;
390 if (start >= first && start <= last) {
391 ptr += (start - first)*4;
392 if (end <= last)
393 if (CHK_OVERFLOW(ptr_buffer_start, ptr,
394 ptr_buffer_end,
395 (((end - start)+1)*4)))
396 memcpy(ptr, buf , ((end - start)+1)*4);
397 else
398 printk(KERN_CRIT "Not enough"
399 " buffer space for"
400 " MSG_MASK\n");
401 else
402 printk(KERN_INFO "Unable to copy"
403 " mask change\n");
Shalabh Jain2c783cb2012-01-15 11:09:29 +0530404
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700405 found = 1;
406 break;
407 } else {
408 ptr += ((last - first) + 1)*4;
409 }
410 }
411 /* Entry was not found - add new table */
412 if (!found) {
413 if (CHK_OVERFLOW(ptr_buffer_start, ptr, ptr_buffer_end,
414 8 + ((end - start) + 1)*4)) {
415 memcpy(ptr, &(start) , 4);
416 ptr += 4;
417 memcpy(ptr, &(end), 4);
418 ptr += 4;
419 memcpy(ptr, buf , ((end - start) + 1)*4);
420 } else
421 printk(KERN_CRIT " Not enough buffer"
422 " space for MSG_MASK\n");
423 }
424 mutex_unlock(&driver->diagchar_mutex);
425 diag_print_mask_table();
Shalabh Jain2c783cb2012-01-15 11:09:29 +0530426
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427}
428
Shalabh Jain2c783cb2012-01-15 11:09:29 +0530429static void diag_update_event_mask(uint8_t *buf, int toggle, int num_bits)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700430{
431 uint8_t *ptr = driver->event_masks;
432 uint8_t *temp = buf + 2;
433
434 mutex_lock(&driver->diagchar_mutex);
435 if (!toggle)
436 memset(ptr, 0 , EVENT_MASK_SIZE);
437 else
438 if (CHK_OVERFLOW(ptr, ptr,
439 ptr+EVENT_MASK_SIZE,
Shalabh Jain2c783cb2012-01-15 11:09:29 +0530440 num_bits/8 + 1))
441 memcpy(ptr, temp , num_bits/8 + 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700442 else
443 printk(KERN_CRIT "Not enough buffer space "
444 "for EVENT_MASK\n");
445 mutex_unlock(&driver->diagchar_mutex);
446}
447
448static void diag_update_log_mask(int equip_id, uint8_t *buf, int num_items)
449{
450 uint8_t *temp = buf;
Shalabh Jain2c783cb2012-01-15 11:09:29 +0530451 struct mask_info {
452 int equip_id;
453 int index;
454 };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700455 int i = 0;
456 unsigned char *ptr_data;
457 int offset = 8*MAX_EQUIP_ID;
458 struct mask_info *ptr = (struct mask_info *)driver->log_masks;
459
460 mutex_lock(&driver->diagchar_mutex);
461 /* Check if we already know index of this equipment ID */
462 for (i = 0; i < MAX_EQUIP_ID; i++) {
463 if ((ptr->equip_id == equip_id) && (ptr->index != 0)) {
464 offset = ptr->index;
465 break;
466 }
467 if ((ptr->equip_id == 0) && (ptr->index == 0)) {
Shalabh Jain2c783cb2012-01-15 11:09:29 +0530468 /*Reached a null entry */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700469 ptr->equip_id = equip_id;
470 ptr->index = driver->log_masks_length;
471 offset = driver->log_masks_length;
472 driver->log_masks_length += ((num_items+7)/8);
473 break;
474 }
475 ptr++;
476 }
477 ptr_data = driver->log_masks + offset;
478 if (CHK_OVERFLOW(driver->log_masks, ptr_data, driver->log_masks
479 + LOG_MASK_SIZE, (num_items+7)/8))
480 memcpy(ptr_data, temp , (num_items+7)/8);
481 else
482 printk(KERN_CRIT " Not enough buffer space for LOG_MASK\n");
483 mutex_unlock(&driver->diagchar_mutex);
484}
485
486static void diag_update_pkt_buffer(unsigned char *buf)
487{
488 unsigned char *ptr = driver->pkt_buf;
489 unsigned char *temp = buf;
490
491 mutex_lock(&driver->diagchar_mutex);
492 if (CHK_OVERFLOW(ptr, ptr, ptr + PKT_SIZE, driver->pkt_length))
493 memcpy(ptr, temp , driver->pkt_length);
494 else
495 printk(KERN_CRIT " Not enough buffer space for PKT_RESP\n");
496 mutex_unlock(&driver->diagchar_mutex);
497}
498
499void diag_update_userspace_clients(unsigned int type)
500{
501 int i;
502
503 mutex_lock(&driver->diagchar_mutex);
504 for (i = 0; i < driver->num_clients; i++)
505 if (driver->client_map[i].pid != 0)
506 driver->data_ready[i] |= type;
507 wake_up_interruptible(&driver->wait_q);
508 mutex_unlock(&driver->diagchar_mutex);
509}
510
511void diag_update_sleeping_process(int process_id)
512{
513 int i;
514
515 mutex_lock(&driver->diagchar_mutex);
516 for (i = 0; i < driver->num_clients; i++)
517 if (driver->client_map[i].pid == process_id) {
518 driver->data_ready[i] |= PKT_TYPE;
519 break;
520 }
521 wake_up_interruptible(&driver->wait_q);
522 mutex_unlock(&driver->diagchar_mutex);
523}
524
525void diag_send_data(struct diag_master_table entry, unsigned char *buf,
526 int len, int type)
527{
528 driver->pkt_length = len;
529 if (entry.process_id != NON_APPS_PROC && type != MODEM_DATA) {
530 diag_update_pkt_buffer(buf);
531 diag_update_sleeping_process(entry.process_id);
532 } else {
533 if (len > 0) {
Shalabh Jainc9f35092011-07-28 18:36:17 -0700534 if (entry.client_id == MODEM_PROC && driver->ch) {
Dixon Petersonff425d12011-12-06 18:12:35 -0800535 if ((cpu_is_msm8960() || cpu_is_msm8930() ||
536 cpu_is_msm9615()) &&
Shalabh Jainc9f35092011-07-28 18:36:17 -0700537 (int)(*(char *)buf) == MODE_CMD)
Dixon Petersonff425d12011-12-06 18:12:35 -0800538 if ((int)(*(char *)(buf+1)) ==
539 RESET_ID)
Shalabh Jainc9f35092011-07-28 18:36:17 -0700540 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541 smd_write(driver->ch, buf, len);
Shalabh Jainc9f35092011-07-28 18:36:17 -0700542 } else if (entry.client_id == QDSP_PROC &&
543 driver->chqdsp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700544 smd_write(driver->chqdsp, buf, len);
Shalabh Jainc9f35092011-07-28 18:36:17 -0700545 } else if (entry.client_id == WCNSS_PROC &&
546 driver->ch_wcnss) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700547 smd_write(driver->ch_wcnss, buf, len);
Shalabh Jainc9f35092011-07-28 18:36:17 -0700548 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700549 pr_alert("diag: incorrect channel");
Shalabh Jainc9f35092011-07-28 18:36:17 -0700550 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551 }
552 }
553}
554
555static int diag_process_apps_pkt(unsigned char *buf, int len)
556{
557 uint16_t subsys_cmd_code;
558 int subsys_id, ssid_first, ssid_last, ssid_range;
559 int packet_type = 1, i, cmd_code;
560 unsigned char *temp = buf;
561 int data_type;
562#if defined(CONFIG_DIAG_OVER_USB)
563 int payload_length;
564 unsigned char *ptr;
565#endif
566
567 /* Check for registered clients and forward packet to apropriate proc */
568 cmd_code = (int)(*(char *)buf);
569 temp++;
570 subsys_id = (int)(*(char *)temp);
571 temp++;
572 subsys_cmd_code = *(uint16_t *)temp;
573 temp += 2;
574 data_type = APPS_DATA;
575 /* Dont send any command other than mode reset */
Dixon Petersonff425d12011-12-06 18:12:35 -0800576 if ((cpu_is_msm8960() || cpu_is_msm8930() || cpu_is_msm9615()) &&
577 cmd_code == MODE_CMD) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700578 if (subsys_id != RESET_ID)
579 data_type = MODEM_DATA;
580 }
581
582 pr_debug("diag: %d %d %d", cmd_code, subsys_id, subsys_cmd_code);
583 for (i = 0; i < diag_max_registration; i++) {
584 entry = driver->table[i];
585 if (entry.process_id != NO_PROCESS) {
586 if (entry.cmd_code == cmd_code && entry.subsys_id ==
587 subsys_id && entry.cmd_code_lo <=
588 subsys_cmd_code &&
589 entry.cmd_code_hi >= subsys_cmd_code) {
590 diag_send_data(entry, buf, len, data_type);
591 packet_type = 0;
592 } else if (entry.cmd_code == 255
593 && cmd_code == 75) {
594 if (entry.subsys_id ==
595 subsys_id &&
596 entry.cmd_code_lo <=
597 subsys_cmd_code &&
598 entry.cmd_code_hi >=
599 subsys_cmd_code) {
600 diag_send_data(entry, buf, len,
601 data_type);
602 packet_type = 0;
603 }
604 } else if (entry.cmd_code == 255 &&
605 entry.subsys_id == 255) {
606 if (entry.cmd_code_lo <=
607 cmd_code &&
608 entry.
609 cmd_code_hi >= cmd_code) {
610 diag_send_data(entry, buf, len,
611 data_type);
612 packet_type = 0;
613 }
614 }
615 }
616 }
Shalabh Jain2c783cb2012-01-15 11:09:29 +0530617 /* set event mask */
618 if (*buf == 0x82) {
619 buf += 4;
620 diag_update_event_mask(buf, 1, *(uint16_t *)buf);
621 diag_update_userspace_clients(EVENT_MASKS_TYPE);
622 }
623 /* event mask change */
624 else if ((*buf == 0x60) && (*(buf+1) == 0x0)) {
625 diag_update_event_mask(buf+1, 0, 0);
626 diag_update_userspace_clients(EVENT_MASKS_TYPE);
627#if defined(CONFIG_DIAG_OVER_USB)
628 /* Check for Apps Only */
629 if (!(driver->ch) && chk_apps_only()) {
630 /* echo response back for apps only DIAG */
631 driver->apps_rsp_buf[0] = 0x60;
632 driver->apps_rsp_buf[1] = 0x0;
633 driver->apps_rsp_buf[2] = 0x0;
634 ENCODE_RSP_AND_SEND(2);
635 return 0;
636 }
637#endif
638 }
639 /* Set log masks */
640 else if (*buf == 0x73 && *(int *)(buf+4) == 3) {
641 buf += 8;
642 /* Read Equip ID and pass as first param below*/
643 diag_update_log_mask(*(int *)buf, buf+8, *(int *)(buf+4));
644 diag_update_userspace_clients(LOG_MASKS_TYPE);
645#if defined(CONFIG_DIAG_OVER_USB)
646 /* Check for Apps Only */
647 if (!(driver->ch) && chk_apps_only()) {
648 /* echo response back for Apps only DIAG */
649 driver->apps_rsp_buf[0] = 0x73;
650 *(int *)(driver->apps_rsp_buf + 4) = 0x3; /* op. ID */
651 *(int *)(driver->apps_rsp_buf + 8) = 0x0; /* success */
652 payload_length = 8 + ((*(int *)(buf + 4)) + 7)/8;
653 for (i = 0; i < payload_length; i++)
654 *(int *)(driver->apps_rsp_buf+12+i) =
655 *(buf+8+i);
656 ENCODE_RSP_AND_SEND(12 + payload_length - 1);
657 return 0;
658 }
659#endif
660 }
661 /* Check for set message mask */
662 else if ((*buf == 0x7d) && (*(buf+1) == 0x4)) {
663 ssid_first = *(uint16_t *)(buf + 2);
664 ssid_last = *(uint16_t *)(buf + 4);
665 ssid_range = 4 * (ssid_last - ssid_first + 1);
666 diag_update_msg_mask(ssid_first, ssid_last , buf + 8);
667 diag_update_userspace_clients(MSG_MASKS_TYPE);
668#if defined(CONFIG_DIAG_OVER_USB)
669 if (!(driver->ch) && chk_apps_only()) {
670 /* echo response back for apps only DIAG */
671 for (i = 0; i < 8 + ssid_range; i++)
672 *(driver->apps_rsp_buf + i) = *(buf+i);
673 ENCODE_RSP_AND_SEND(8 + ssid_range - 1);
674 return 0;
675 }
676#endif
677 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700678#if defined(CONFIG_DIAG_OVER_USB)
Shalabh Jainfb8e3c12011-10-19 17:29:42 -0700679 /* Check for Apps Only & get event mask request */
Shalabh Jain2c783cb2012-01-15 11:09:29 +0530680 else if (!(driver->ch) && chk_apps_only() && *buf == 0x81) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700681 driver->apps_rsp_buf[0] = 0x81;
682 driver->apps_rsp_buf[1] = 0x0;
683 *(uint16_t *)(driver->apps_rsp_buf + 2) = 0x0;
684 *(uint16_t *)(driver->apps_rsp_buf + 4) = EVENT_LAST_ID + 1;
685 for (i = 0; i < EVENT_LAST_ID/8 + 1; i++)
686 *(unsigned char *)(driver->apps_rsp_buf + 6 + i) = 0x0;
687 ENCODE_RSP_AND_SEND(6 + EVENT_LAST_ID/8);
688 return 0;
689 }
Shalabh Jainfb8e3c12011-10-19 17:29:42 -0700690 /* Get log ID range & Check for Apps Only */
691 else if (!(driver->ch) && chk_apps_only()
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700692 && (*buf == 0x73) && *(int *)(buf+4) == 1) {
693 driver->apps_rsp_buf[0] = 0x73;
694 *(int *)(driver->apps_rsp_buf + 4) = 0x1; /* operation ID */
695 *(int *)(driver->apps_rsp_buf + 8) = 0x0; /* success code */
696 *(int *)(driver->apps_rsp_buf + 12) = LOG_GET_ITEM_NUM(LOG_0);
697 *(int *)(driver->apps_rsp_buf + 16) = LOG_GET_ITEM_NUM(LOG_1);
698 *(int *)(driver->apps_rsp_buf + 20) = LOG_GET_ITEM_NUM(LOG_2);
699 *(int *)(driver->apps_rsp_buf + 24) = LOG_GET_ITEM_NUM(LOG_3);
700 *(int *)(driver->apps_rsp_buf + 28) = LOG_GET_ITEM_NUM(LOG_4);
701 *(int *)(driver->apps_rsp_buf + 32) = LOG_GET_ITEM_NUM(LOG_5);
702 *(int *)(driver->apps_rsp_buf + 36) = LOG_GET_ITEM_NUM(LOG_6);
703 *(int *)(driver->apps_rsp_buf + 40) = LOG_GET_ITEM_NUM(LOG_7);
704 *(int *)(driver->apps_rsp_buf + 44) = LOG_GET_ITEM_NUM(LOG_8);
705 *(int *)(driver->apps_rsp_buf + 48) = LOG_GET_ITEM_NUM(LOG_9);
706 *(int *)(driver->apps_rsp_buf + 52) = LOG_GET_ITEM_NUM(LOG_10);
707 *(int *)(driver->apps_rsp_buf + 56) = LOG_GET_ITEM_NUM(LOG_11);
708 *(int *)(driver->apps_rsp_buf + 60) = LOG_GET_ITEM_NUM(LOG_12);
709 *(int *)(driver->apps_rsp_buf + 64) = LOG_GET_ITEM_NUM(LOG_13);
710 *(int *)(driver->apps_rsp_buf + 68) = LOG_GET_ITEM_NUM(LOG_14);
711 *(int *)(driver->apps_rsp_buf + 72) = LOG_GET_ITEM_NUM(LOG_15);
712 ENCODE_RSP_AND_SEND(75);
713 return 0;
714 }
715 /* Respond to Get SSID Range request message */
Shalabh Jainfb8e3c12011-10-19 17:29:42 -0700716 else if (!(driver->ch) && chk_apps_only()
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717 && (*buf == 0x7d) && (*(buf+1) == 0x1)) {
718 driver->apps_rsp_buf[0] = 0x7d;
719 driver->apps_rsp_buf[1] = 0x1;
720 driver->apps_rsp_buf[2] = 0x1;
721 driver->apps_rsp_buf[3] = 0x0;
722 *(int *)(driver->apps_rsp_buf + 4) = MSG_MASK_TBL_CNT;
723 *(uint16_t *)(driver->apps_rsp_buf + 8) = MSG_SSID_0;
724 *(uint16_t *)(driver->apps_rsp_buf + 10) = MSG_SSID_0_LAST;
725 *(uint16_t *)(driver->apps_rsp_buf + 12) = MSG_SSID_1;
726 *(uint16_t *)(driver->apps_rsp_buf + 14) = MSG_SSID_1_LAST;
727 *(uint16_t *)(driver->apps_rsp_buf + 16) = MSG_SSID_2;
728 *(uint16_t *)(driver->apps_rsp_buf + 18) = MSG_SSID_2_LAST;
729 *(uint16_t *)(driver->apps_rsp_buf + 20) = MSG_SSID_3;
730 *(uint16_t *)(driver->apps_rsp_buf + 22) = MSG_SSID_3_LAST;
731 *(uint16_t *)(driver->apps_rsp_buf + 24) = MSG_SSID_4;
732 *(uint16_t *)(driver->apps_rsp_buf + 26) = MSG_SSID_4_LAST;
733 *(uint16_t *)(driver->apps_rsp_buf + 28) = MSG_SSID_5;
734 *(uint16_t *)(driver->apps_rsp_buf + 30) = MSG_SSID_5_LAST;
735 *(uint16_t *)(driver->apps_rsp_buf + 32) = MSG_SSID_6;
736 *(uint16_t *)(driver->apps_rsp_buf + 34) = MSG_SSID_6_LAST;
737 *(uint16_t *)(driver->apps_rsp_buf + 36) = MSG_SSID_7;
738 *(uint16_t *)(driver->apps_rsp_buf + 38) = MSG_SSID_7_LAST;
739 *(uint16_t *)(driver->apps_rsp_buf + 40) = MSG_SSID_8;
740 *(uint16_t *)(driver->apps_rsp_buf + 42) = MSG_SSID_8_LAST;
741 *(uint16_t *)(driver->apps_rsp_buf + 44) = MSG_SSID_9;
742 *(uint16_t *)(driver->apps_rsp_buf + 46) = MSG_SSID_9_LAST;
743 *(uint16_t *)(driver->apps_rsp_buf + 48) = MSG_SSID_10;
744 *(uint16_t *)(driver->apps_rsp_buf + 50) = MSG_SSID_10_LAST;
745 *(uint16_t *)(driver->apps_rsp_buf + 52) = MSG_SSID_11;
746 *(uint16_t *)(driver->apps_rsp_buf + 54) = MSG_SSID_11_LAST;
747 *(uint16_t *)(driver->apps_rsp_buf + 56) = MSG_SSID_12;
748 *(uint16_t *)(driver->apps_rsp_buf + 58) = MSG_SSID_12_LAST;
749 *(uint16_t *)(driver->apps_rsp_buf + 60) = MSG_SSID_13;
750 *(uint16_t *)(driver->apps_rsp_buf + 62) = MSG_SSID_13_LAST;
751 *(uint16_t *)(driver->apps_rsp_buf + 64) = MSG_SSID_14;
752 *(uint16_t *)(driver->apps_rsp_buf + 66) = MSG_SSID_14_LAST;
753 *(uint16_t *)(driver->apps_rsp_buf + 68) = MSG_SSID_15;
754 *(uint16_t *)(driver->apps_rsp_buf + 70) = MSG_SSID_15_LAST;
755 *(uint16_t *)(driver->apps_rsp_buf + 72) = MSG_SSID_16;
756 *(uint16_t *)(driver->apps_rsp_buf + 74) = MSG_SSID_16_LAST;
757 *(uint16_t *)(driver->apps_rsp_buf + 76) = MSG_SSID_17;
758 *(uint16_t *)(driver->apps_rsp_buf + 78) = MSG_SSID_17_LAST;
759 *(uint16_t *)(driver->apps_rsp_buf + 80) = MSG_SSID_18;
760 *(uint16_t *)(driver->apps_rsp_buf + 82) = MSG_SSID_18_LAST;
761 ENCODE_RSP_AND_SEND(83);
762 return 0;
763 }
Shalabh Jainfb8e3c12011-10-19 17:29:42 -0700764 /* Check for Apps Only Respond to Get Subsys Build mask */
765 else if (!(driver->ch) && chk_apps_only()
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700766 && (*buf == 0x7d) && (*(buf+1) == 0x2)) {
767 ssid_first = *(uint16_t *)(buf + 2);
768 ssid_last = *(uint16_t *)(buf + 4);
769 ssid_range = 4 * (ssid_last - ssid_first + 1);
770 /* frame response */
771 driver->apps_rsp_buf[0] = 0x7d;
772 driver->apps_rsp_buf[1] = 0x2;
773 *(uint16_t *)(driver->apps_rsp_buf + 2) = ssid_first;
774 *(uint16_t *)(driver->apps_rsp_buf + 4) = ssid_last;
775 driver->apps_rsp_buf[6] = 0x1;
776 driver->apps_rsp_buf[7] = 0x0;
777 ptr = driver->apps_rsp_buf + 8;
778 /* bld time masks */
779 switch (ssid_first) {
780 case MSG_SSID_0:
781 for (i = 0; i < ssid_range; i += 4)
782 *(int *)(ptr + i) = msg_bld_masks_0[i/4];
783 break;
784 case MSG_SSID_1:
785 for (i = 0; i < ssid_range; i += 4)
786 *(int *)(ptr + i) = msg_bld_masks_1[i/4];
787 break;
788 case MSG_SSID_2:
789 for (i = 0; i < ssid_range; i += 4)
790 *(int *)(ptr + i) = msg_bld_masks_2[i/4];
791 break;
792 case MSG_SSID_3:
793 for (i = 0; i < ssid_range; i += 4)
794 *(int *)(ptr + i) = msg_bld_masks_3[i/4];
795 break;
796 case MSG_SSID_4:
797 for (i = 0; i < ssid_range; i += 4)
798 *(int *)(ptr + i) = msg_bld_masks_4[i/4];
799 break;
800 case MSG_SSID_5:
801 for (i = 0; i < ssid_range; i += 4)
802 *(int *)(ptr + i) = msg_bld_masks_5[i/4];
803 break;
804 case MSG_SSID_6:
805 for (i = 0; i < ssid_range; i += 4)
806 *(int *)(ptr + i) = msg_bld_masks_6[i/4];
807 break;
808 case MSG_SSID_7:
809 for (i = 0; i < ssid_range; i += 4)
810 *(int *)(ptr + i) = msg_bld_masks_7[i/4];
811 break;
812 case MSG_SSID_8:
813 for (i = 0; i < ssid_range; i += 4)
814 *(int *)(ptr + i) = msg_bld_masks_8[i/4];
815 break;
816 case MSG_SSID_9:
817 for (i = 0; i < ssid_range; i += 4)
818 *(int *)(ptr + i) = msg_bld_masks_9[i/4];
819 break;
820 case MSG_SSID_10:
821 for (i = 0; i < ssid_range; i += 4)
822 *(int *)(ptr + i) = msg_bld_masks_10[i/4];
823 break;
824 case MSG_SSID_11:
825 for (i = 0; i < ssid_range; i += 4)
826 *(int *)(ptr + i) = msg_bld_masks_11[i/4];
827 break;
828 case MSG_SSID_12:
829 for (i = 0; i < ssid_range; i += 4)
830 *(int *)(ptr + i) = msg_bld_masks_12[i/4];
831 break;
832 case MSG_SSID_13:
833 for (i = 0; i < ssid_range; i += 4)
834 *(int *)(ptr + i) = msg_bld_masks_13[i/4];
835 break;
836 case MSG_SSID_14:
837 for (i = 0; i < ssid_range; i += 4)
838 *(int *)(ptr + i) = msg_bld_masks_14[i/4];
839 break;
840 case MSG_SSID_15:
841 for (i = 0; i < ssid_range; i += 4)
842 *(int *)(ptr + i) = msg_bld_masks_15[i/4];
843 break;
844 case MSG_SSID_16:
845 for (i = 0; i < ssid_range; i += 4)
846 *(int *)(ptr + i) = msg_bld_masks_16[i/4];
847 break;
848 case MSG_SSID_17:
849 for (i = 0; i < ssid_range; i += 4)
850 *(int *)(ptr + i) = msg_bld_masks_17[i/4];
851 break;
852 case MSG_SSID_18:
853 for (i = 0; i < ssid_range; i += 4)
854 *(int *)(ptr + i) = msg_bld_masks_18[i/4];
855 break;
856 }
857 ENCODE_RSP_AND_SEND(8 + ssid_range - 1);
858 return 0;
859 }
860 /* Check for download command */
Dixon Peterson4cf7d3b2011-11-08 21:15:12 -0800861 else if ((cpu_is_msm8x60() || cpu_is_msm8960() || cpu_is_msm8930()
862 || cpu_is_msm9615()) && (*buf == 0x3A)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700863 /* send response back */
864 driver->apps_rsp_buf[0] = *buf;
865 ENCODE_RSP_AND_SEND(0);
866 msleep(5000);
867 /* call download API */
868 msm_set_restart_mode(RESTART_DLOAD);
869 printk(KERN_CRIT "diag: download mode set, Rebooting SoC..\n");
870 kernel_restart(NULL);
871 /* Not required, represents that command isnt sent to modem */
872 return 0;
873 }
874 /* Check for ID for NO MODEM present */
875 else if (!(driver->ch)) {
876 /* Respond to polling for Apps only DIAG */
877 if ((*buf == 0x4b) && (*(buf+1) == 0x32) &&
878 (*(buf+2) == 0x03)) {
879 for (i = 0; i < 3; i++)
880 driver->apps_rsp_buf[i] = *(buf+i);
881 for (i = 0; i < 13; i++)
882 driver->apps_rsp_buf[i+3] = 0;
883
884 ENCODE_RSP_AND_SEND(15);
885 return 0;
886 }
887 /* respond to 0x0 command */
888 else if (*buf == 0x00) {
889 for (i = 0; i < 55; i++)
890 driver->apps_rsp_buf[i] = 0;
891
892 ENCODE_RSP_AND_SEND(54);
893 return 0;
894 }
895 /* respond to 0x7c command */
896 else if (*buf == 0x7c) {
897 driver->apps_rsp_buf[0] = 0x7c;
898 for (i = 1; i < 8; i++)
899 driver->apps_rsp_buf[i] = 0;
900 /* Tools ID for APQ 8060 */
901 *(int *)(driver->apps_rsp_buf + 8) =
902 chk_config_get_id();
903 *(unsigned char *)(driver->apps_rsp_buf + 12) = '\0';
904 *(unsigned char *)(driver->apps_rsp_buf + 13) = '\0';
905 ENCODE_RSP_AND_SEND(13);
906 return 0;
907 }
908 }
909#endif
910 return packet_type;
911}
912
913#ifdef CONFIG_DIAG_OVER_USB
914void diag_send_error_rsp(int index)
915{
916 int i;
Shalabh Jain1fedab92011-12-22 13:15:22 +0530917
918 if (index > 490) {
919 pr_err("diag: error response too huge, aborting\n");
920 return;
921 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700922 driver->apps_rsp_buf[0] = 0x13; /* error code 13 */
923 for (i = 0; i < index; i++)
924 driver->apps_rsp_buf[i+1] = *(driver->hdlc_buf+i);
925 ENCODE_RSP_AND_SEND(index - 3);
926}
927#else
928static inline void diag_send_error_rsp(int index) {}
929#endif
930
931void diag_process_hdlc(void *data, unsigned len)
932{
933 struct diag_hdlc_decode_type hdlc;
934 int ret, type = 0;
935 pr_debug("diag: HDLC decode fn, len of data %d\n", len);
936 hdlc.dest_ptr = driver->hdlc_buf;
937 hdlc.dest_size = USB_MAX_OUT_BUF;
938 hdlc.src_ptr = data;
939 hdlc.src_size = len;
940 hdlc.src_idx = 0;
941 hdlc.dest_idx = 0;
942 hdlc.escaping = 0;
943
944 ret = diag_hdlc_decode(&hdlc);
945
946 if (ret)
947 type = diag_process_apps_pkt(driver->hdlc_buf,
948 hdlc.dest_idx - 3);
949 else if (driver->debug_flag) {
950 printk(KERN_ERR "Packet dropped due to bad HDLC coding/CRC"
951 " errors or partial packet received, packet"
952 " length = %d\n", len);
953 print_hex_dump(KERN_DEBUG, "Dropped Packet Data: ", 16, 1,
954 DUMP_PREFIX_ADDRESS, data, len, 1);
955 driver->debug_flag = 0;
956 }
957 /* send error responses from APPS for Central Routing */
Shalabh Jainfb8e3c12011-10-19 17:29:42 -0700958 if (type == 1 && chk_apps_only()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700959 diag_send_error_rsp(hdlc.dest_idx);
960 type = 0;
961 }
962 /* implies this packet is NOT meant for apps */
963 if (!(driver->ch) && type == 1) {
Shalabh Jainfb8e3c12011-10-19 17:29:42 -0700964 if (chk_apps_only()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700965 diag_send_error_rsp(hdlc.dest_idx);
966 } else { /* APQ 8060, Let Q6 respond */
967 if (driver->chqdsp)
968 smd_write(driver->chqdsp, driver->hdlc_buf,
969 hdlc.dest_idx - 3);
970 }
971 type = 0;
972 }
973
974#ifdef DIAG_DEBUG
975 pr_debug("diag: hdlc.dest_idx = %d", hdlc.dest_idx);
976 for (i = 0; i < hdlc.dest_idx; i++)
977 printk(KERN_DEBUG "\t%x", *(((unsigned char *)
978 driver->hdlc_buf)+i));
979#endif /* DIAG DEBUG */
980 /* ignore 2 bytes for CRC, one for 7E and send */
981 if ((driver->ch) && (ret) && (type) && (hdlc.dest_idx > 3)) {
982 APPEND_DEBUG('g');
983 smd_write(driver->ch, driver->hdlc_buf, hdlc.dest_idx - 3);
984 APPEND_DEBUG('h');
985#ifdef DIAG_DEBUG
986 printk(KERN_INFO "writing data to SMD, pkt length %d\n", len);
987 print_hex_dump(KERN_DEBUG, "Written Packet Data to SMD: ", 16,
988 1, DUMP_PREFIX_ADDRESS, data, len, 1);
989#endif /* DIAG DEBUG */
990 }
991}
992
993#ifdef CONFIG_DIAG_OVER_USB
Shalabh Jain8e9750a2011-09-09 13:06:29 -0700994/* 2+1 for modem ; 2 for LPASS ; 1 for WCNSS */
995#define N_LEGACY_WRITE (driver->poolsize + 6)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700996#define N_LEGACY_READ 1
997
998int diagfwd_connect(void)
999{
1000 int err;
1001
1002 printk(KERN_DEBUG "diag: USB connected\n");
1003 err = usb_diag_alloc_req(driver->legacy_ch, N_LEGACY_WRITE,
1004 N_LEGACY_READ);
1005 if (err)
1006 printk(KERN_ERR "diag: unable to alloc USB req on legacy ch");
1007
1008 driver->usb_connected = 1;
1009 driver->in_busy_1 = 0;
1010 driver->in_busy_2 = 0;
1011 driver->in_busy_qdsp_1 = 0;
1012 driver->in_busy_qdsp_2 = 0;
1013 driver->in_busy_wcnss = 0;
1014
1015 /* Poll SMD channels to check for data*/
1016 queue_work(driver->diag_wq, &(driver->diag_read_smd_work));
1017 queue_work(driver->diag_wq, &(driver->diag_read_smd_qdsp_work));
1018 queue_work(driver->diag_wq, &(driver->diag_read_smd_wcnss_work));
Shalabh Jaincf5f20e2011-08-22 12:29:52 -07001019 /* Poll SMD CNTL channels to check for data */
1020 queue_work(driver->diag_wq, &(driver->diag_read_smd_cntl_work));
1021 queue_work(driver->diag_wq, &(driver->diag_read_smd_qdsp_cntl_work));
1022 queue_work(driver->diag_wq, &(driver->diag_read_smd_wcnss_cntl_work));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001023 /* Poll USB channel to check for data*/
1024 queue_work(driver->diag_wq, &(driver->diag_read_work));
1025#ifdef CONFIG_DIAG_SDIO_PIPE
1026 if (machine_is_msm8x60_fusion() || machine_is_msm8x60_fusn_ffa()) {
1027 if (driver->mdm_ch && !IS_ERR(driver->mdm_ch))
1028 diagfwd_connect_sdio();
1029 else
1030 printk(KERN_INFO "diag: No USB MDM ch");
1031 }
1032#endif
1033 return 0;
1034}
1035
1036int diagfwd_disconnect(void)
1037{
1038 printk(KERN_DEBUG "diag: USB disconnected\n");
1039 driver->usb_connected = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001040 driver->debug_flag = 1;
1041 usb_diag_free_req(driver->legacy_ch);
Shalabh Jain69890aa2011-10-10 12:59:16 -07001042 if (driver->logging_mode == USB_MODE) {
1043 driver->in_busy_1 = 1;
1044 driver->in_busy_2 = 1;
1045 driver->in_busy_qdsp_1 = 1;
1046 driver->in_busy_qdsp_2 = 1;
1047 driver->in_busy_wcnss = 1;
1048 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001049#ifdef CONFIG_DIAG_SDIO_PIPE
1050 if (machine_is_msm8x60_fusion() || machine_is_msm8x60_fusn_ffa())
1051 if (driver->mdm_ch && !IS_ERR(driver->mdm_ch))
1052 diagfwd_disconnect_sdio();
1053#endif
1054 /* TBD - notify and flow control SMD */
1055 return 0;
1056}
1057
1058int diagfwd_write_complete(struct diag_request *diag_write_ptr)
1059{
1060 unsigned char *buf = diag_write_ptr->buf;
1061 /*Determine if the write complete is for data from modem/apps/q6 */
1062 /* Need a context variable here instead */
1063 if (buf == (void *)driver->buf_in_1) {
1064 driver->in_busy_1 = 0;
1065 APPEND_DEBUG('o');
1066 queue_work(driver->diag_wq, &(driver->diag_read_smd_work));
1067 } else if (buf == (void *)driver->buf_in_2) {
1068 driver->in_busy_2 = 0;
1069 APPEND_DEBUG('O');
1070 queue_work(driver->diag_wq, &(driver->diag_read_smd_work));
1071 } else if (buf == (void *)driver->buf_in_qdsp_1) {
1072 driver->in_busy_qdsp_1 = 0;
1073 APPEND_DEBUG('p');
1074 queue_work(driver->diag_wq, &(driver->diag_read_smd_qdsp_work));
1075 } else if (buf == (void *)driver->buf_in_qdsp_2) {
1076 driver->in_busy_qdsp_2 = 0;
1077 APPEND_DEBUG('P');
1078 queue_work(driver->diag_wq, &(driver->diag_read_smd_qdsp_work));
1079 } else if (buf == (void *)driver->buf_in_wcnss) {
1080 driver->in_busy_wcnss = 0;
1081 APPEND_DEBUG('R');
1082 queue_work(driver->diag_wq,
1083 &(driver->diag_read_smd_wcnss_work));
1084 }
1085#ifdef CONFIG_DIAG_SDIO_PIPE
1086 else if (buf == (void *)driver->buf_in_sdio)
1087 if (machine_is_msm8x60_fusion() ||
Shalabh Jain482bf122011-12-06 03:54:47 -08001088 machine_is_msm8x60_fusn_ffa())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001089 diagfwd_write_complete_sdio();
1090 else
1091 pr_err("diag: Incorrect buffer pointer while WRITE");
1092#endif
1093 else {
1094 diagmem_free(driver, (unsigned char *)buf, POOL_TYPE_HDLC);
1095 diagmem_free(driver, (unsigned char *)diag_write_ptr,
1096 POOL_TYPE_WRITE_STRUCT);
1097 APPEND_DEBUG('q');
1098 }
1099 return 0;
1100}
1101
1102int diagfwd_read_complete(struct diag_request *diag_read_ptr)
1103{
1104 int status = diag_read_ptr->status;
1105 unsigned char *buf = diag_read_ptr->buf;
1106
1107 /* Determine if the read complete is for data on legacy/mdm ch */
1108 if (buf == (void *)driver->usb_buf_out) {
1109 driver->read_len_legacy = diag_read_ptr->actual;
1110 APPEND_DEBUG('s');
1111#ifdef DIAG_DEBUG
1112 printk(KERN_INFO "read data from USB, pkt length %d",
1113 diag_read_ptr->actual);
1114 print_hex_dump(KERN_DEBUG, "Read Packet Data from USB: ", 16, 1,
1115 DUMP_PREFIX_ADDRESS, diag_read_ptr->buf,
1116 diag_read_ptr->actual, 1);
1117#endif /* DIAG DEBUG */
1118 if (driver->logging_mode == USB_MODE) {
1119 if (status != -ECONNRESET && status != -ESHUTDOWN)
1120 queue_work(driver->diag_wq,
1121 &(driver->diag_proc_hdlc_work));
1122 else
1123 queue_work(driver->diag_wq,
1124 &(driver->diag_read_work));
1125 }
1126 }
1127#ifdef CONFIG_DIAG_SDIO_PIPE
1128 else if (buf == (void *)driver->usb_buf_mdm_out) {
1129 if (machine_is_msm8x60_fusion() ||
Shalabh Jain482bf122011-12-06 03:54:47 -08001130 machine_is_msm8x60_fusn_ffa()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001131 driver->read_len_mdm = diag_read_ptr->actual;
1132 diagfwd_read_complete_sdio();
1133 } else
1134 pr_err("diag: Incorrect buffer pointer while READ");
1135 }
1136#endif
1137 else
1138 printk(KERN_ERR "diag: Unknown buffer ptr from USB");
1139
1140 return 0;
1141}
1142
1143void diag_read_work_fn(struct work_struct *work)
1144{
1145 APPEND_DEBUG('d');
1146 driver->usb_read_ptr->buf = driver->usb_buf_out;
1147 driver->usb_read_ptr->length = USB_MAX_OUT_BUF;
1148 usb_diag_read(driver->legacy_ch, driver->usb_read_ptr);
1149 APPEND_DEBUG('e');
1150}
1151
1152void diag_process_hdlc_fn(struct work_struct *work)
1153{
1154 APPEND_DEBUG('D');
1155 diag_process_hdlc(driver->usb_buf_out, driver->read_len_legacy);
1156 diag_read_work_fn(work);
1157 APPEND_DEBUG('E');
1158}
1159
1160void diag_usb_legacy_notifier(void *priv, unsigned event,
1161 struct diag_request *d_req)
1162{
1163 switch (event) {
1164 case USB_DIAG_CONNECT:
1165 diagfwd_connect();
1166 break;
1167 case USB_DIAG_DISCONNECT:
1168 diagfwd_disconnect();
1169 break;
1170 case USB_DIAG_READ_DONE:
1171 diagfwd_read_complete(d_req);
1172 break;
1173 case USB_DIAG_WRITE_DONE:
1174 diagfwd_write_complete(d_req);
1175 break;
1176 default:
1177 printk(KERN_ERR "Unknown event from USB diag\n");
1178 break;
1179 }
1180}
1181
1182#endif /* DIAG OVER USB */
1183
1184static void diag_smd_notify(void *ctxt, unsigned event)
1185{
Shalabh Jainc2ec8292011-10-14 12:34:55 -07001186 if (event == SMD_EVENT_CLOSE) {
1187 pr_info("diag: clean modem registration\n");
1188 diag_clear_reg(MODEM_PROC);
Shalabh Jaineefee052011-11-08 23:46:03 -08001189 driver->ch = 0;
1190 return;
1191 } else if (event == SMD_EVENT_OPEN) {
1192 driver->ch = ch_temp;
1193 }
1194 queue_work(driver->diag_wq, &(driver->diag_read_smd_work));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001195}
1196
1197#if defined(CONFIG_MSM_N_WAY_SMD)
1198static void diag_smd_qdsp_notify(void *ctxt, unsigned event)
1199{
Shalabh Jainc6c2b1d2011-12-20 04:32:34 -08001200 if (event == SMD_EVENT_CLOSE) {
1201 pr_info("diag: clean lpass registration\n");
1202 diag_clear_reg(QDSP_PROC);
1203 driver->chqdsp = 0;
1204 return;
1205 } else if (event == SMD_EVENT_OPEN) {
1206 driver->chqdsp = chqdsp_temp;
1207 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001208 queue_work(driver->diag_wq, &(driver->diag_read_smd_qdsp_work));
1209}
1210#endif
1211
1212static void diag_smd_wcnss_notify(void *ctxt, unsigned event)
1213{
Shalabh Jainc6c2b1d2011-12-20 04:32:34 -08001214 if (event == SMD_EVENT_CLOSE) {
1215 pr_info("diag: clean wcnss registration\n");
1216 diag_clear_reg(WCNSS_PROC);
1217 driver->ch_wcnss = 0;
1218 return;
1219 } else if (event == SMD_EVENT_OPEN) {
1220 driver->ch_wcnss = ch_wcnss_temp;
1221 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001222 queue_work(driver->diag_wq, &(driver->diag_read_smd_wcnss_work));
1223}
1224
1225static int diag_smd_probe(struct platform_device *pdev)
1226{
1227 int r = 0;
1228
Shalabh Jaineefee052011-11-08 23:46:03 -08001229 if (pdev->id == SMD_APPS_MODEM) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001230 r = smd_open("DIAG", &driver->ch, driver, diag_smd_notify);
Shalabh Jaineefee052011-11-08 23:46:03 -08001231 ch_temp = driver->ch;
1232 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001233#if defined(CONFIG_MSM_N_WAY_SMD)
Shalabh Jainc6c2b1d2011-12-20 04:32:34 -08001234 if (pdev->id == SMD_APPS_QDSP) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001235 r = smd_named_open_on_edge("DIAG", SMD_APPS_QDSP
1236 , &driver->chqdsp, driver, diag_smd_qdsp_notify);
Shalabh Jainc6c2b1d2011-12-20 04:32:34 -08001237 chqdsp_temp = driver->chqdsp;
1238 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001239#endif
Shalabh Jainc6c2b1d2011-12-20 04:32:34 -08001240 if (pdev->id == SMD_APPS_WCNSS) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001241 r = smd_named_open_on_edge("APPS_RIVA_DATA", SMD_APPS_WCNSS
1242 , &driver->ch_wcnss, driver, diag_smd_wcnss_notify);
Shalabh Jainc6c2b1d2011-12-20 04:32:34 -08001243 ch_wcnss_temp = driver->ch_wcnss;
1244 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001245 pm_runtime_set_active(&pdev->dev);
1246 pm_runtime_enable(&pdev->dev);
1247 pr_debug("diag: open SMD port, Id = %d, r = %d\n", pdev->id, r);
1248
1249 return 0;
1250}
1251
1252static int diagfwd_runtime_suspend(struct device *dev)
1253{
1254 dev_dbg(dev, "pm_runtime: suspending...\n");
1255 return 0;
1256}
1257
1258static int diagfwd_runtime_resume(struct device *dev)
1259{
1260 dev_dbg(dev, "pm_runtime: resuming...\n");
1261 return 0;
1262}
1263
1264static const struct dev_pm_ops diagfwd_dev_pm_ops = {
1265 .runtime_suspend = diagfwd_runtime_suspend,
1266 .runtime_resume = diagfwd_runtime_resume,
1267};
1268
1269static struct platform_driver msm_smd_ch1_driver = {
1270
1271 .probe = diag_smd_probe,
1272 .driver = {
1273 .name = "DIAG",
1274 .owner = THIS_MODULE,
1275 .pm = &diagfwd_dev_pm_ops,
1276 },
1277};
1278
1279static struct platform_driver diag_smd_lite_driver = {
1280
1281 .probe = diag_smd_probe,
1282 .driver = {
1283 .name = "APPS_RIVA_DATA",
1284 .owner = THIS_MODULE,
1285 .pm = &diagfwd_dev_pm_ops,
1286 },
1287};
1288
1289void diagfwd_init(void)
1290{
1291 diag_debug_buf_idx = 0;
1292 driver->read_len_legacy = 0;
1293 if (driver->buf_in_1 == NULL) {
1294 driver->buf_in_1 = kzalloc(IN_BUF_SIZE, GFP_KERNEL);
1295 if (driver->buf_in_1 == NULL)
1296 goto err;
1297 }
1298 if (driver->buf_in_2 == NULL) {
1299 driver->buf_in_2 = kzalloc(IN_BUF_SIZE, GFP_KERNEL);
1300 if (driver->buf_in_2 == NULL)
1301 goto err;
1302 }
1303 if (driver->buf_in_qdsp_1 == NULL) {
1304 driver->buf_in_qdsp_1 = kzalloc(IN_BUF_SIZE, GFP_KERNEL);
1305 if (driver->buf_in_qdsp_1 == NULL)
1306 goto err;
1307 }
1308 if (driver->buf_in_qdsp_2 == NULL) {
1309 driver->buf_in_qdsp_2 = kzalloc(IN_BUF_SIZE, GFP_KERNEL);
1310 if (driver->buf_in_qdsp_2 == NULL)
1311 goto err;
1312 }
1313 if (driver->buf_in_wcnss == NULL) {
1314 driver->buf_in_wcnss = kzalloc(IN_BUF_SIZE, GFP_KERNEL);
1315 if (driver->buf_in_wcnss == NULL)
1316 goto err;
1317 }
1318 if (driver->usb_buf_out == NULL &&
1319 (driver->usb_buf_out = kzalloc(USB_MAX_OUT_BUF,
1320 GFP_KERNEL)) == NULL)
1321 goto err;
1322 if (driver->hdlc_buf == NULL
1323 && (driver->hdlc_buf = kzalloc(HDLC_MAX, GFP_KERNEL)) == NULL)
1324 goto err;
Shalabh Jain69890aa2011-10-10 12:59:16 -07001325 if (driver->user_space_data == NULL)
1326 driver->user_space_data = kzalloc(USER_SPACE_DATA, GFP_KERNEL);
1327 if (driver->user_space_data == NULL)
1328 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001329 if (driver->msg_masks == NULL
1330 && (driver->msg_masks = kzalloc(MSG_MASK_SIZE,
1331 GFP_KERNEL)) == NULL)
1332 goto err;
1333 if (driver->log_masks == NULL &&
1334 (driver->log_masks = kzalloc(LOG_MASK_SIZE, GFP_KERNEL)) == NULL)
1335 goto err;
1336 driver->log_masks_length = 8*MAX_EQUIP_ID;
1337 if (driver->event_masks == NULL &&
1338 (driver->event_masks = kzalloc(EVENT_MASK_SIZE,
1339 GFP_KERNEL)) == NULL)
1340 goto err;
1341 if (driver->client_map == NULL &&
1342 (driver->client_map = kzalloc
1343 ((driver->num_clients) * sizeof(struct diag_client_map),
1344 GFP_KERNEL)) == NULL)
1345 goto err;
1346 if (driver->buf_tbl == NULL)
1347 driver->buf_tbl = kzalloc(buf_tbl_size *
1348 sizeof(struct diag_write_device), GFP_KERNEL);
1349 if (driver->buf_tbl == NULL)
1350 goto err;
1351 if (driver->data_ready == NULL &&
1352 (driver->data_ready = kzalloc(driver->num_clients * sizeof(int)
1353 , GFP_KERNEL)) == NULL)
1354 goto err;
1355 if (driver->table == NULL &&
1356 (driver->table = kzalloc(diag_max_registration*
1357 sizeof(struct diag_master_table),
1358 GFP_KERNEL)) == NULL)
1359 goto err;
1360 if (driver->write_ptr_1 == NULL) {
1361 driver->write_ptr_1 = kzalloc(
1362 sizeof(struct diag_request), GFP_KERNEL);
1363 if (driver->write_ptr_1 == NULL)
1364 goto err;
1365 }
1366 if (driver->write_ptr_2 == NULL) {
1367 driver->write_ptr_2 = kzalloc(
1368 sizeof(struct diag_request), GFP_KERNEL);
1369 if (driver->write_ptr_2 == NULL)
1370 goto err;
1371 }
1372 if (driver->write_ptr_qdsp_1 == NULL) {
1373 driver->write_ptr_qdsp_1 = kzalloc(
1374 sizeof(struct diag_request), GFP_KERNEL);
1375 if (driver->write_ptr_qdsp_1 == NULL)
1376 goto err;
1377 }
1378 if (driver->write_ptr_qdsp_2 == NULL) {
1379 driver->write_ptr_qdsp_2 = kzalloc(
1380 sizeof(struct diag_request), GFP_KERNEL);
1381 if (driver->write_ptr_qdsp_2 == NULL)
1382 goto err;
1383 }
1384 if (driver->write_ptr_wcnss == NULL) {
1385 driver->write_ptr_wcnss = kzalloc(
1386 sizeof(struct diag_request), GFP_KERNEL);
1387 if (driver->write_ptr_wcnss == NULL)
1388 goto err;
1389 }
1390 if (driver->usb_read_ptr == NULL) {
1391 driver->usb_read_ptr = kzalloc(
1392 sizeof(struct diag_request), GFP_KERNEL);
1393 if (driver->usb_read_ptr == NULL)
1394 goto err;
1395 }
1396 if (driver->pkt_buf == NULL &&
1397 (driver->pkt_buf = kzalloc(PKT_SIZE,
1398 GFP_KERNEL)) == NULL)
1399 goto err;
1400 if (driver->apps_rsp_buf == NULL) {
1401 driver->apps_rsp_buf = kzalloc(500, GFP_KERNEL);
1402 if (driver->apps_rsp_buf == NULL)
1403 goto err;
1404 }
1405 driver->diag_wq = create_singlethread_workqueue("diag_wq");
1406#ifdef CONFIG_DIAG_OVER_USB
1407 INIT_WORK(&(driver->diag_proc_hdlc_work), diag_process_hdlc_fn);
1408 INIT_WORK(&(driver->diag_read_work), diag_read_work_fn);
1409 driver->legacy_ch = usb_diag_open(DIAG_LEGACY, driver,
1410 diag_usb_legacy_notifier);
1411 if (IS_ERR(driver->legacy_ch)) {
1412 printk(KERN_ERR "Unable to open USB diag legacy channel\n");
1413 goto err;
1414 }
1415#endif
1416 platform_driver_register(&msm_smd_ch1_driver);
1417 platform_driver_register(&diag_smd_lite_driver);
1418
1419 return;
1420err:
1421 pr_err("diag: Could not initialize diag buffers");
1422 kfree(driver->buf_in_1);
1423 kfree(driver->buf_in_2);
1424 kfree(driver->buf_in_qdsp_1);
1425 kfree(driver->buf_in_qdsp_2);
1426 kfree(driver->buf_in_wcnss);
1427 kfree(driver->usb_buf_out);
1428 kfree(driver->hdlc_buf);
1429 kfree(driver->msg_masks);
1430 kfree(driver->log_masks);
1431 kfree(driver->event_masks);
1432 kfree(driver->client_map);
1433 kfree(driver->buf_tbl);
1434 kfree(driver->data_ready);
1435 kfree(driver->table);
1436 kfree(driver->pkt_buf);
1437 kfree(driver->write_ptr_1);
1438 kfree(driver->write_ptr_2);
1439 kfree(driver->write_ptr_qdsp_1);
1440 kfree(driver->write_ptr_qdsp_2);
1441 kfree(driver->write_ptr_wcnss);
1442 kfree(driver->usb_read_ptr);
1443 kfree(driver->apps_rsp_buf);
Shalabh Jain69890aa2011-10-10 12:59:16 -07001444 kfree(driver->user_space_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001445 if (driver->diag_wq)
1446 destroy_workqueue(driver->diag_wq);
1447}
1448
1449void diagfwd_exit(void)
1450{
1451 smd_close(driver->ch);
1452 smd_close(driver->chqdsp);
1453 smd_close(driver->ch_wcnss);
1454 driver->ch = 0; /* SMD can make this NULL */
1455 driver->chqdsp = 0;
1456 driver->ch_wcnss = 0;
1457#ifdef CONFIG_DIAG_OVER_USB
1458 if (driver->usb_connected)
1459 usb_diag_free_req(driver->legacy_ch);
1460 usb_diag_close(driver->legacy_ch);
1461#endif
1462 platform_driver_unregister(&msm_smd_ch1_driver);
1463 platform_driver_unregister(&diag_smd_lite_driver);
1464 kfree(driver->buf_in_1);
1465 kfree(driver->buf_in_2);
1466 kfree(driver->buf_in_qdsp_1);
1467 kfree(driver->buf_in_qdsp_2);
1468 kfree(driver->buf_in_wcnss);
1469 kfree(driver->usb_buf_out);
1470 kfree(driver->hdlc_buf);
1471 kfree(driver->msg_masks);
1472 kfree(driver->log_masks);
1473 kfree(driver->event_masks);
1474 kfree(driver->client_map);
1475 kfree(driver->buf_tbl);
1476 kfree(driver->data_ready);
1477 kfree(driver->table);
1478 kfree(driver->pkt_buf);
1479 kfree(driver->write_ptr_1);
1480 kfree(driver->write_ptr_2);
1481 kfree(driver->write_ptr_qdsp_1);
1482 kfree(driver->write_ptr_qdsp_2);
1483 kfree(driver->write_ptr_wcnss);
1484 kfree(driver->usb_read_ptr);
1485 kfree(driver->apps_rsp_buf);
Shalabh Jain69890aa2011-10-10 12:59:16 -07001486 kfree(driver->user_space_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001487 destroy_workqueue(driver->diag_wq);
1488}