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