blob: 58c9b9a6b7b1f10edc53b716466a8843bfd8ead2 [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) {
Stepan Moskovchenko021e0492011-10-25 14:46:00 -0700506 if ((cpu_is_msm8960() || cpu_is_msm8930()) &&
Shalabh Jainc9f35092011-07-28 18:36:17 -0700507 (int)(*(char *)buf) == MODE_CMD)
508 if ((int)(*(char *)(buf+1)) == RESET_ID)
509 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510 smd_write(driver->ch, buf, len);
Shalabh Jainc9f35092011-07-28 18:36:17 -0700511 } else if (entry.client_id == QDSP_PROC &&
512 driver->chqdsp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513 smd_write(driver->chqdsp, buf, len);
Shalabh Jainc9f35092011-07-28 18:36:17 -0700514 } else if (entry.client_id == WCNSS_PROC &&
515 driver->ch_wcnss) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516 smd_write(driver->ch_wcnss, buf, len);
Shalabh Jainc9f35092011-07-28 18:36:17 -0700517 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700518 pr_alert("diag: incorrect channel");
Shalabh Jainc9f35092011-07-28 18:36:17 -0700519 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520 }
521 }
522}
523
524static int diag_process_apps_pkt(unsigned char *buf, int len)
525{
526 uint16_t subsys_cmd_code;
527 int subsys_id, ssid_first, ssid_last, ssid_range;
528 int packet_type = 1, i, cmd_code;
529 unsigned char *temp = buf;
530 int data_type;
531#if defined(CONFIG_DIAG_OVER_USB)
532 int payload_length;
533 unsigned char *ptr;
534#endif
535
536 /* Check for registered clients and forward packet to apropriate proc */
537 cmd_code = (int)(*(char *)buf);
538 temp++;
539 subsys_id = (int)(*(char *)temp);
540 temp++;
541 subsys_cmd_code = *(uint16_t *)temp;
542 temp += 2;
543 data_type = APPS_DATA;
544 /* Dont send any command other than mode reset */
Stepan Moskovchenko021e0492011-10-25 14:46:00 -0700545 if ((cpu_is_msm8960() || cpu_is_msm8930()) && cmd_code == MODE_CMD) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700546 if (subsys_id != RESET_ID)
547 data_type = MODEM_DATA;
548 }
549
550 pr_debug("diag: %d %d %d", cmd_code, subsys_id, subsys_cmd_code);
551 for (i = 0; i < diag_max_registration; i++) {
552 entry = driver->table[i];
553 if (entry.process_id != NO_PROCESS) {
554 if (entry.cmd_code == cmd_code && entry.subsys_id ==
555 subsys_id && entry.cmd_code_lo <=
556 subsys_cmd_code &&
557 entry.cmd_code_hi >= subsys_cmd_code) {
558 diag_send_data(entry, buf, len, data_type);
559 packet_type = 0;
560 } else if (entry.cmd_code == 255
561 && cmd_code == 75) {
562 if (entry.subsys_id ==
563 subsys_id &&
564 entry.cmd_code_lo <=
565 subsys_cmd_code &&
566 entry.cmd_code_hi >=
567 subsys_cmd_code) {
568 diag_send_data(entry, buf, len,
569 data_type);
570 packet_type = 0;
571 }
572 } else if (entry.cmd_code == 255 &&
573 entry.subsys_id == 255) {
574 if (entry.cmd_code_lo <=
575 cmd_code &&
576 entry.
577 cmd_code_hi >= cmd_code) {
578 diag_send_data(entry, buf, len,
579 data_type);
580 packet_type = 0;
581 }
582 }
583 }
584 }
585 /* set event mask */
586 if (*buf == 0x82) {
587 buf += 4;
588 diag_update_event_mask(buf, 1, *(uint16_t *)buf);
589 diag_update_userspace_clients(EVENT_MASKS_TYPE);
590 }
591 /* event mask change */
592 else if ((*buf == 0x60) && (*(buf+1) == 0x0)) {
593 diag_update_event_mask(buf+1, 0, 0);
594 diag_update_userspace_clients(EVENT_MASKS_TYPE);
595#if defined(CONFIG_DIAG_OVER_USB)
Shalabh Jainfb8e3c12011-10-19 17:29:42 -0700596 /* Check for Apps Only */
597 if (!(driver->ch) && chk_apps_only()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700598 /* echo response back for apps only DIAG */
599 driver->apps_rsp_buf[0] = 0x60;
600 driver->apps_rsp_buf[1] = 0x0;
601 driver->apps_rsp_buf[2] = 0x0;
602 ENCODE_RSP_AND_SEND(2);
603 return 0;
604 }
605#endif
606 }
607 /* Set log masks */
608 else if (*buf == 0x73 && *(int *)(buf+4) == 3) {
609 buf += 8;
610 /* Read Equip ID and pass as first param below*/
611 diag_update_log_mask(*(int *)buf, buf+8, *(int *)(buf+4));
612 diag_update_userspace_clients(LOG_MASKS_TYPE);
613#if defined(CONFIG_DIAG_OVER_USB)
Shalabh Jainfb8e3c12011-10-19 17:29:42 -0700614 /* Check for Apps Only */
615 if (!(driver->ch) && chk_apps_only()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700616 /* echo response back for Apps only DIAG */
617 driver->apps_rsp_buf[0] = 0x73;
618 *(int *)(driver->apps_rsp_buf + 4) = 0x3; /* op. ID */
619 *(int *)(driver->apps_rsp_buf + 8) = 0x0; /* success */
620 payload_length = 8 + ((*(int *)(buf + 4)) + 7)/8;
621 for (i = 0; i < payload_length; i++)
622 *(int *)(driver->apps_rsp_buf+12+i) =
623 *(buf+8+i);
624 ENCODE_RSP_AND_SEND(12 + payload_length - 1);
625 return 0;
626 }
627#endif
628 }
629 /* Check for set message mask */
630 else if ((*buf == 0x7d) && (*(buf+1) == 0x4)) {
631 ssid_first = *(uint16_t *)(buf + 2);
632 ssid_last = *(uint16_t *)(buf + 4);
633 ssid_range = 4 * (ssid_last - ssid_first + 1);
634 diag_update_msg_mask(ssid_first, ssid_last , buf + 8);
635 diag_update_userspace_clients(MSG_MASKS_TYPE);
636#if defined(CONFIG_DIAG_OVER_USB)
Shalabh Jainfb8e3c12011-10-19 17:29:42 -0700637 if (!(driver->ch) && chk_apps_only()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700638 /* echo response back for apps only DIAG */
639 for (i = 0; i < 8 + ssid_range; i++)
640 *(driver->apps_rsp_buf + i) = *(buf+i);
641 ENCODE_RSP_AND_SEND(8 + ssid_range - 1);
642 return 0;
643 }
644#endif
645 }
646#if defined(CONFIG_DIAG_OVER_USB)
Shalabh Jainfb8e3c12011-10-19 17:29:42 -0700647 /* Check for Apps Only & get event mask request */
648 else if (!(driver->ch) && chk_apps_only() && *buf == 0x81) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649 driver->apps_rsp_buf[0] = 0x81;
650 driver->apps_rsp_buf[1] = 0x0;
651 *(uint16_t *)(driver->apps_rsp_buf + 2) = 0x0;
652 *(uint16_t *)(driver->apps_rsp_buf + 4) = EVENT_LAST_ID + 1;
653 for (i = 0; i < EVENT_LAST_ID/8 + 1; i++)
654 *(unsigned char *)(driver->apps_rsp_buf + 6 + i) = 0x0;
655 ENCODE_RSP_AND_SEND(6 + EVENT_LAST_ID/8);
656 return 0;
657 }
Shalabh Jainfb8e3c12011-10-19 17:29:42 -0700658 /* Get log ID range & Check for Apps Only */
659 else if (!(driver->ch) && chk_apps_only()
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700660 && (*buf == 0x73) && *(int *)(buf+4) == 1) {
661 driver->apps_rsp_buf[0] = 0x73;
662 *(int *)(driver->apps_rsp_buf + 4) = 0x1; /* operation ID */
663 *(int *)(driver->apps_rsp_buf + 8) = 0x0; /* success code */
664 *(int *)(driver->apps_rsp_buf + 12) = LOG_GET_ITEM_NUM(LOG_0);
665 *(int *)(driver->apps_rsp_buf + 16) = LOG_GET_ITEM_NUM(LOG_1);
666 *(int *)(driver->apps_rsp_buf + 20) = LOG_GET_ITEM_NUM(LOG_2);
667 *(int *)(driver->apps_rsp_buf + 24) = LOG_GET_ITEM_NUM(LOG_3);
668 *(int *)(driver->apps_rsp_buf + 28) = LOG_GET_ITEM_NUM(LOG_4);
669 *(int *)(driver->apps_rsp_buf + 32) = LOG_GET_ITEM_NUM(LOG_5);
670 *(int *)(driver->apps_rsp_buf + 36) = LOG_GET_ITEM_NUM(LOG_6);
671 *(int *)(driver->apps_rsp_buf + 40) = LOG_GET_ITEM_NUM(LOG_7);
672 *(int *)(driver->apps_rsp_buf + 44) = LOG_GET_ITEM_NUM(LOG_8);
673 *(int *)(driver->apps_rsp_buf + 48) = LOG_GET_ITEM_NUM(LOG_9);
674 *(int *)(driver->apps_rsp_buf + 52) = LOG_GET_ITEM_NUM(LOG_10);
675 *(int *)(driver->apps_rsp_buf + 56) = LOG_GET_ITEM_NUM(LOG_11);
676 *(int *)(driver->apps_rsp_buf + 60) = LOG_GET_ITEM_NUM(LOG_12);
677 *(int *)(driver->apps_rsp_buf + 64) = LOG_GET_ITEM_NUM(LOG_13);
678 *(int *)(driver->apps_rsp_buf + 68) = LOG_GET_ITEM_NUM(LOG_14);
679 *(int *)(driver->apps_rsp_buf + 72) = LOG_GET_ITEM_NUM(LOG_15);
680 ENCODE_RSP_AND_SEND(75);
681 return 0;
682 }
683 /* Respond to Get SSID Range request message */
Shalabh Jainfb8e3c12011-10-19 17:29:42 -0700684 else if (!(driver->ch) && chk_apps_only()
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700685 && (*buf == 0x7d) && (*(buf+1) == 0x1)) {
686 driver->apps_rsp_buf[0] = 0x7d;
687 driver->apps_rsp_buf[1] = 0x1;
688 driver->apps_rsp_buf[2] = 0x1;
689 driver->apps_rsp_buf[3] = 0x0;
690 *(int *)(driver->apps_rsp_buf + 4) = MSG_MASK_TBL_CNT;
691 *(uint16_t *)(driver->apps_rsp_buf + 8) = MSG_SSID_0;
692 *(uint16_t *)(driver->apps_rsp_buf + 10) = MSG_SSID_0_LAST;
693 *(uint16_t *)(driver->apps_rsp_buf + 12) = MSG_SSID_1;
694 *(uint16_t *)(driver->apps_rsp_buf + 14) = MSG_SSID_1_LAST;
695 *(uint16_t *)(driver->apps_rsp_buf + 16) = MSG_SSID_2;
696 *(uint16_t *)(driver->apps_rsp_buf + 18) = MSG_SSID_2_LAST;
697 *(uint16_t *)(driver->apps_rsp_buf + 20) = MSG_SSID_3;
698 *(uint16_t *)(driver->apps_rsp_buf + 22) = MSG_SSID_3_LAST;
699 *(uint16_t *)(driver->apps_rsp_buf + 24) = MSG_SSID_4;
700 *(uint16_t *)(driver->apps_rsp_buf + 26) = MSG_SSID_4_LAST;
701 *(uint16_t *)(driver->apps_rsp_buf + 28) = MSG_SSID_5;
702 *(uint16_t *)(driver->apps_rsp_buf + 30) = MSG_SSID_5_LAST;
703 *(uint16_t *)(driver->apps_rsp_buf + 32) = MSG_SSID_6;
704 *(uint16_t *)(driver->apps_rsp_buf + 34) = MSG_SSID_6_LAST;
705 *(uint16_t *)(driver->apps_rsp_buf + 36) = MSG_SSID_7;
706 *(uint16_t *)(driver->apps_rsp_buf + 38) = MSG_SSID_7_LAST;
707 *(uint16_t *)(driver->apps_rsp_buf + 40) = MSG_SSID_8;
708 *(uint16_t *)(driver->apps_rsp_buf + 42) = MSG_SSID_8_LAST;
709 *(uint16_t *)(driver->apps_rsp_buf + 44) = MSG_SSID_9;
710 *(uint16_t *)(driver->apps_rsp_buf + 46) = MSG_SSID_9_LAST;
711 *(uint16_t *)(driver->apps_rsp_buf + 48) = MSG_SSID_10;
712 *(uint16_t *)(driver->apps_rsp_buf + 50) = MSG_SSID_10_LAST;
713 *(uint16_t *)(driver->apps_rsp_buf + 52) = MSG_SSID_11;
714 *(uint16_t *)(driver->apps_rsp_buf + 54) = MSG_SSID_11_LAST;
715 *(uint16_t *)(driver->apps_rsp_buf + 56) = MSG_SSID_12;
716 *(uint16_t *)(driver->apps_rsp_buf + 58) = MSG_SSID_12_LAST;
717 *(uint16_t *)(driver->apps_rsp_buf + 60) = MSG_SSID_13;
718 *(uint16_t *)(driver->apps_rsp_buf + 62) = MSG_SSID_13_LAST;
719 *(uint16_t *)(driver->apps_rsp_buf + 64) = MSG_SSID_14;
720 *(uint16_t *)(driver->apps_rsp_buf + 66) = MSG_SSID_14_LAST;
721 *(uint16_t *)(driver->apps_rsp_buf + 68) = MSG_SSID_15;
722 *(uint16_t *)(driver->apps_rsp_buf + 70) = MSG_SSID_15_LAST;
723 *(uint16_t *)(driver->apps_rsp_buf + 72) = MSG_SSID_16;
724 *(uint16_t *)(driver->apps_rsp_buf + 74) = MSG_SSID_16_LAST;
725 *(uint16_t *)(driver->apps_rsp_buf + 76) = MSG_SSID_17;
726 *(uint16_t *)(driver->apps_rsp_buf + 78) = MSG_SSID_17_LAST;
727 *(uint16_t *)(driver->apps_rsp_buf + 80) = MSG_SSID_18;
728 *(uint16_t *)(driver->apps_rsp_buf + 82) = MSG_SSID_18_LAST;
729 ENCODE_RSP_AND_SEND(83);
730 return 0;
731 }
Shalabh Jainfb8e3c12011-10-19 17:29:42 -0700732 /* Check for Apps Only Respond to Get Subsys Build mask */
733 else if (!(driver->ch) && chk_apps_only()
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700734 && (*buf == 0x7d) && (*(buf+1) == 0x2)) {
735 ssid_first = *(uint16_t *)(buf + 2);
736 ssid_last = *(uint16_t *)(buf + 4);
737 ssid_range = 4 * (ssid_last - ssid_first + 1);
738 /* frame response */
739 driver->apps_rsp_buf[0] = 0x7d;
740 driver->apps_rsp_buf[1] = 0x2;
741 *(uint16_t *)(driver->apps_rsp_buf + 2) = ssid_first;
742 *(uint16_t *)(driver->apps_rsp_buf + 4) = ssid_last;
743 driver->apps_rsp_buf[6] = 0x1;
744 driver->apps_rsp_buf[7] = 0x0;
745 ptr = driver->apps_rsp_buf + 8;
746 /* bld time masks */
747 switch (ssid_first) {
748 case MSG_SSID_0:
749 for (i = 0; i < ssid_range; i += 4)
750 *(int *)(ptr + i) = msg_bld_masks_0[i/4];
751 break;
752 case MSG_SSID_1:
753 for (i = 0; i < ssid_range; i += 4)
754 *(int *)(ptr + i) = msg_bld_masks_1[i/4];
755 break;
756 case MSG_SSID_2:
757 for (i = 0; i < ssid_range; i += 4)
758 *(int *)(ptr + i) = msg_bld_masks_2[i/4];
759 break;
760 case MSG_SSID_3:
761 for (i = 0; i < ssid_range; i += 4)
762 *(int *)(ptr + i) = msg_bld_masks_3[i/4];
763 break;
764 case MSG_SSID_4:
765 for (i = 0; i < ssid_range; i += 4)
766 *(int *)(ptr + i) = msg_bld_masks_4[i/4];
767 break;
768 case MSG_SSID_5:
769 for (i = 0; i < ssid_range; i += 4)
770 *(int *)(ptr + i) = msg_bld_masks_5[i/4];
771 break;
772 case MSG_SSID_6:
773 for (i = 0; i < ssid_range; i += 4)
774 *(int *)(ptr + i) = msg_bld_masks_6[i/4];
775 break;
776 case MSG_SSID_7:
777 for (i = 0; i < ssid_range; i += 4)
778 *(int *)(ptr + i) = msg_bld_masks_7[i/4];
779 break;
780 case MSG_SSID_8:
781 for (i = 0; i < ssid_range; i += 4)
782 *(int *)(ptr + i) = msg_bld_masks_8[i/4];
783 break;
784 case MSG_SSID_9:
785 for (i = 0; i < ssid_range; i += 4)
786 *(int *)(ptr + i) = msg_bld_masks_9[i/4];
787 break;
788 case MSG_SSID_10:
789 for (i = 0; i < ssid_range; i += 4)
790 *(int *)(ptr + i) = msg_bld_masks_10[i/4];
791 break;
792 case MSG_SSID_11:
793 for (i = 0; i < ssid_range; i += 4)
794 *(int *)(ptr + i) = msg_bld_masks_11[i/4];
795 break;
796 case MSG_SSID_12:
797 for (i = 0; i < ssid_range; i += 4)
798 *(int *)(ptr + i) = msg_bld_masks_12[i/4];
799 break;
800 case MSG_SSID_13:
801 for (i = 0; i < ssid_range; i += 4)
802 *(int *)(ptr + i) = msg_bld_masks_13[i/4];
803 break;
804 case MSG_SSID_14:
805 for (i = 0; i < ssid_range; i += 4)
806 *(int *)(ptr + i) = msg_bld_masks_14[i/4];
807 break;
808 case MSG_SSID_15:
809 for (i = 0; i < ssid_range; i += 4)
810 *(int *)(ptr + i) = msg_bld_masks_15[i/4];
811 break;
812 case MSG_SSID_16:
813 for (i = 0; i < ssid_range; i += 4)
814 *(int *)(ptr + i) = msg_bld_masks_16[i/4];
815 break;
816 case MSG_SSID_17:
817 for (i = 0; i < ssid_range; i += 4)
818 *(int *)(ptr + i) = msg_bld_masks_17[i/4];
819 break;
820 case MSG_SSID_18:
821 for (i = 0; i < ssid_range; i += 4)
822 *(int *)(ptr + i) = msg_bld_masks_18[i/4];
823 break;
824 }
825 ENCODE_RSP_AND_SEND(8 + ssid_range - 1);
826 return 0;
827 }
828 /* Check for download command */
Dixon Peterson4cf7d3b2011-11-08 21:15:12 -0800829 else if ((cpu_is_msm8x60() || cpu_is_msm8960() || cpu_is_msm8930()
830 || cpu_is_msm9615()) && (*buf == 0x3A)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700831 /* send response back */
832 driver->apps_rsp_buf[0] = *buf;
833 ENCODE_RSP_AND_SEND(0);
834 msleep(5000);
835 /* call download API */
836 msm_set_restart_mode(RESTART_DLOAD);
837 printk(KERN_CRIT "diag: download mode set, Rebooting SoC..\n");
838 kernel_restart(NULL);
839 /* Not required, represents that command isnt sent to modem */
840 return 0;
841 }
842 /* Check for ID for NO MODEM present */
843 else if (!(driver->ch)) {
844 /* Respond to polling for Apps only DIAG */
845 if ((*buf == 0x4b) && (*(buf+1) == 0x32) &&
846 (*(buf+2) == 0x03)) {
847 for (i = 0; i < 3; i++)
848 driver->apps_rsp_buf[i] = *(buf+i);
849 for (i = 0; i < 13; i++)
850 driver->apps_rsp_buf[i+3] = 0;
851
852 ENCODE_RSP_AND_SEND(15);
853 return 0;
854 }
855 /* respond to 0x0 command */
856 else if (*buf == 0x00) {
857 for (i = 0; i < 55; i++)
858 driver->apps_rsp_buf[i] = 0;
859
860 ENCODE_RSP_AND_SEND(54);
861 return 0;
862 }
863 /* respond to 0x7c command */
864 else if (*buf == 0x7c) {
865 driver->apps_rsp_buf[0] = 0x7c;
866 for (i = 1; i < 8; i++)
867 driver->apps_rsp_buf[i] = 0;
868 /* Tools ID for APQ 8060 */
869 *(int *)(driver->apps_rsp_buf + 8) =
870 chk_config_get_id();
871 *(unsigned char *)(driver->apps_rsp_buf + 12) = '\0';
872 *(unsigned char *)(driver->apps_rsp_buf + 13) = '\0';
873 ENCODE_RSP_AND_SEND(13);
874 return 0;
875 }
876 }
877#endif
878 return packet_type;
879}
880
881#ifdef CONFIG_DIAG_OVER_USB
882void diag_send_error_rsp(int index)
883{
884 int i;
885 driver->apps_rsp_buf[0] = 0x13; /* error code 13 */
886 for (i = 0; i < index; i++)
887 driver->apps_rsp_buf[i+1] = *(driver->hdlc_buf+i);
888 ENCODE_RSP_AND_SEND(index - 3);
889}
890#else
891static inline void diag_send_error_rsp(int index) {}
892#endif
893
894void diag_process_hdlc(void *data, unsigned len)
895{
896 struct diag_hdlc_decode_type hdlc;
897 int ret, type = 0;
898 pr_debug("diag: HDLC decode fn, len of data %d\n", len);
899 hdlc.dest_ptr = driver->hdlc_buf;
900 hdlc.dest_size = USB_MAX_OUT_BUF;
901 hdlc.src_ptr = data;
902 hdlc.src_size = len;
903 hdlc.src_idx = 0;
904 hdlc.dest_idx = 0;
905 hdlc.escaping = 0;
906
907 ret = diag_hdlc_decode(&hdlc);
908
909 if (ret)
910 type = diag_process_apps_pkt(driver->hdlc_buf,
911 hdlc.dest_idx - 3);
912 else if (driver->debug_flag) {
913 printk(KERN_ERR "Packet dropped due to bad HDLC coding/CRC"
914 " errors or partial packet received, packet"
915 " length = %d\n", len);
916 print_hex_dump(KERN_DEBUG, "Dropped Packet Data: ", 16, 1,
917 DUMP_PREFIX_ADDRESS, data, len, 1);
918 driver->debug_flag = 0;
919 }
920 /* send error responses from APPS for Central Routing */
Shalabh Jainfb8e3c12011-10-19 17:29:42 -0700921 if (type == 1 && chk_apps_only()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700922 diag_send_error_rsp(hdlc.dest_idx);
923 type = 0;
924 }
925 /* implies this packet is NOT meant for apps */
926 if (!(driver->ch) && type == 1) {
Shalabh Jainfb8e3c12011-10-19 17:29:42 -0700927 if (chk_apps_only()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700928 diag_send_error_rsp(hdlc.dest_idx);
929 } else { /* APQ 8060, Let Q6 respond */
930 if (driver->chqdsp)
931 smd_write(driver->chqdsp, driver->hdlc_buf,
932 hdlc.dest_idx - 3);
933 }
934 type = 0;
935 }
936
937#ifdef DIAG_DEBUG
938 pr_debug("diag: hdlc.dest_idx = %d", hdlc.dest_idx);
939 for (i = 0; i < hdlc.dest_idx; i++)
940 printk(KERN_DEBUG "\t%x", *(((unsigned char *)
941 driver->hdlc_buf)+i));
942#endif /* DIAG DEBUG */
943 /* ignore 2 bytes for CRC, one for 7E and send */
944 if ((driver->ch) && (ret) && (type) && (hdlc.dest_idx > 3)) {
945 APPEND_DEBUG('g');
946 smd_write(driver->ch, driver->hdlc_buf, hdlc.dest_idx - 3);
947 APPEND_DEBUG('h');
948#ifdef DIAG_DEBUG
949 printk(KERN_INFO "writing data to SMD, pkt length %d\n", len);
950 print_hex_dump(KERN_DEBUG, "Written Packet Data to SMD: ", 16,
951 1, DUMP_PREFIX_ADDRESS, data, len, 1);
952#endif /* DIAG DEBUG */
953 }
954}
955
956#ifdef CONFIG_DIAG_OVER_USB
Shalabh Jain8e9750a2011-09-09 13:06:29 -0700957/* 2+1 for modem ; 2 for LPASS ; 1 for WCNSS */
958#define N_LEGACY_WRITE (driver->poolsize + 6)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700959#define N_LEGACY_READ 1
960
961int diagfwd_connect(void)
962{
963 int err;
964
965 printk(KERN_DEBUG "diag: USB connected\n");
966 err = usb_diag_alloc_req(driver->legacy_ch, N_LEGACY_WRITE,
967 N_LEGACY_READ);
968 if (err)
969 printk(KERN_ERR "diag: unable to alloc USB req on legacy ch");
970
971 driver->usb_connected = 1;
972 driver->in_busy_1 = 0;
973 driver->in_busy_2 = 0;
974 driver->in_busy_qdsp_1 = 0;
975 driver->in_busy_qdsp_2 = 0;
976 driver->in_busy_wcnss = 0;
977
978 /* Poll SMD channels to check for data*/
979 queue_work(driver->diag_wq, &(driver->diag_read_smd_work));
980 queue_work(driver->diag_wq, &(driver->diag_read_smd_qdsp_work));
981 queue_work(driver->diag_wq, &(driver->diag_read_smd_wcnss_work));
Shalabh Jaincf5f20e2011-08-22 12:29:52 -0700982 /* Poll SMD CNTL channels to check for data */
983 queue_work(driver->diag_wq, &(driver->diag_read_smd_cntl_work));
984 queue_work(driver->diag_wq, &(driver->diag_read_smd_qdsp_cntl_work));
985 queue_work(driver->diag_wq, &(driver->diag_read_smd_wcnss_cntl_work));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700986 /* Poll USB channel to check for data*/
987 queue_work(driver->diag_wq, &(driver->diag_read_work));
988#ifdef CONFIG_DIAG_SDIO_PIPE
989 if (machine_is_msm8x60_fusion() || machine_is_msm8x60_fusn_ffa()) {
990 if (driver->mdm_ch && !IS_ERR(driver->mdm_ch))
991 diagfwd_connect_sdio();
992 else
993 printk(KERN_INFO "diag: No USB MDM ch");
994 }
995#endif
996 return 0;
997}
998
999int diagfwd_disconnect(void)
1000{
1001 printk(KERN_DEBUG "diag: USB disconnected\n");
1002 driver->usb_connected = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001003 driver->debug_flag = 1;
1004 usb_diag_free_req(driver->legacy_ch);
Shalabh Jain69890aa2011-10-10 12:59:16 -07001005 if (driver->logging_mode == USB_MODE) {
1006 driver->in_busy_1 = 1;
1007 driver->in_busy_2 = 1;
1008 driver->in_busy_qdsp_1 = 1;
1009 driver->in_busy_qdsp_2 = 1;
1010 driver->in_busy_wcnss = 1;
1011 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001012#ifdef CONFIG_DIAG_SDIO_PIPE
1013 if (machine_is_msm8x60_fusion() || machine_is_msm8x60_fusn_ffa())
1014 if (driver->mdm_ch && !IS_ERR(driver->mdm_ch))
1015 diagfwd_disconnect_sdio();
1016#endif
1017 /* TBD - notify and flow control SMD */
1018 return 0;
1019}
1020
1021int diagfwd_write_complete(struct diag_request *diag_write_ptr)
1022{
1023 unsigned char *buf = diag_write_ptr->buf;
1024 /*Determine if the write complete is for data from modem/apps/q6 */
1025 /* Need a context variable here instead */
1026 if (buf == (void *)driver->buf_in_1) {
1027 driver->in_busy_1 = 0;
1028 APPEND_DEBUG('o');
1029 queue_work(driver->diag_wq, &(driver->diag_read_smd_work));
1030 } else if (buf == (void *)driver->buf_in_2) {
1031 driver->in_busy_2 = 0;
1032 APPEND_DEBUG('O');
1033 queue_work(driver->diag_wq, &(driver->diag_read_smd_work));
1034 } else if (buf == (void *)driver->buf_in_qdsp_1) {
1035 driver->in_busy_qdsp_1 = 0;
1036 APPEND_DEBUG('p');
1037 queue_work(driver->diag_wq, &(driver->diag_read_smd_qdsp_work));
1038 } else if (buf == (void *)driver->buf_in_qdsp_2) {
1039 driver->in_busy_qdsp_2 = 0;
1040 APPEND_DEBUG('P');
1041 queue_work(driver->diag_wq, &(driver->diag_read_smd_qdsp_work));
1042 } else if (buf == (void *)driver->buf_in_wcnss) {
1043 driver->in_busy_wcnss = 0;
1044 APPEND_DEBUG('R');
1045 queue_work(driver->diag_wq,
1046 &(driver->diag_read_smd_wcnss_work));
1047 }
1048#ifdef CONFIG_DIAG_SDIO_PIPE
1049 else if (buf == (void *)driver->buf_in_sdio)
1050 if (machine_is_msm8x60_fusion() ||
1051 machine_is_msm8x60_fusn_ffa())
1052 diagfwd_write_complete_sdio();
1053 else
1054 pr_err("diag: Incorrect buffer pointer while WRITE");
1055#endif
1056 else {
1057 diagmem_free(driver, (unsigned char *)buf, POOL_TYPE_HDLC);
1058 diagmem_free(driver, (unsigned char *)diag_write_ptr,
1059 POOL_TYPE_WRITE_STRUCT);
1060 APPEND_DEBUG('q');
1061 }
1062 return 0;
1063}
1064
1065int diagfwd_read_complete(struct diag_request *diag_read_ptr)
1066{
1067 int status = diag_read_ptr->status;
1068 unsigned char *buf = diag_read_ptr->buf;
1069
1070 /* Determine if the read complete is for data on legacy/mdm ch */
1071 if (buf == (void *)driver->usb_buf_out) {
1072 driver->read_len_legacy = diag_read_ptr->actual;
1073 APPEND_DEBUG('s');
1074#ifdef DIAG_DEBUG
1075 printk(KERN_INFO "read data from USB, pkt length %d",
1076 diag_read_ptr->actual);
1077 print_hex_dump(KERN_DEBUG, "Read Packet Data from USB: ", 16, 1,
1078 DUMP_PREFIX_ADDRESS, diag_read_ptr->buf,
1079 diag_read_ptr->actual, 1);
1080#endif /* DIAG DEBUG */
1081 if (driver->logging_mode == USB_MODE) {
1082 if (status != -ECONNRESET && status != -ESHUTDOWN)
1083 queue_work(driver->diag_wq,
1084 &(driver->diag_proc_hdlc_work));
1085 else
1086 queue_work(driver->diag_wq,
1087 &(driver->diag_read_work));
1088 }
1089 }
1090#ifdef CONFIG_DIAG_SDIO_PIPE
1091 else if (buf == (void *)driver->usb_buf_mdm_out) {
1092 if (machine_is_msm8x60_fusion() ||
1093 machine_is_msm8x60_fusn_ffa()) {
1094 driver->read_len_mdm = diag_read_ptr->actual;
1095 diagfwd_read_complete_sdio();
1096 } else
1097 pr_err("diag: Incorrect buffer pointer while READ");
1098 }
1099#endif
1100 else
1101 printk(KERN_ERR "diag: Unknown buffer ptr from USB");
1102
1103 return 0;
1104}
1105
1106void diag_read_work_fn(struct work_struct *work)
1107{
1108 APPEND_DEBUG('d');
1109 driver->usb_read_ptr->buf = driver->usb_buf_out;
1110 driver->usb_read_ptr->length = USB_MAX_OUT_BUF;
1111 usb_diag_read(driver->legacy_ch, driver->usb_read_ptr);
1112 APPEND_DEBUG('e');
1113}
1114
1115void diag_process_hdlc_fn(struct work_struct *work)
1116{
1117 APPEND_DEBUG('D');
1118 diag_process_hdlc(driver->usb_buf_out, driver->read_len_legacy);
1119 diag_read_work_fn(work);
1120 APPEND_DEBUG('E');
1121}
1122
1123void diag_usb_legacy_notifier(void *priv, unsigned event,
1124 struct diag_request *d_req)
1125{
1126 switch (event) {
1127 case USB_DIAG_CONNECT:
1128 diagfwd_connect();
1129 break;
1130 case USB_DIAG_DISCONNECT:
1131 diagfwd_disconnect();
1132 break;
1133 case USB_DIAG_READ_DONE:
1134 diagfwd_read_complete(d_req);
1135 break;
1136 case USB_DIAG_WRITE_DONE:
1137 diagfwd_write_complete(d_req);
1138 break;
1139 default:
1140 printk(KERN_ERR "Unknown event from USB diag\n");
1141 break;
1142 }
1143}
1144
1145#endif /* DIAG OVER USB */
1146
1147static void diag_smd_notify(void *ctxt, unsigned event)
1148{
Shalabh Jainc2ec8292011-10-14 12:34:55 -07001149 if (event == SMD_EVENT_CLOSE) {
1150 pr_info("diag: clean modem registration\n");
1151 diag_clear_reg(MODEM_PROC);
Shalabh Jaineefee052011-11-08 23:46:03 -08001152 driver->ch = 0;
1153 return;
1154 } else if (event == SMD_EVENT_OPEN) {
1155 driver->ch = ch_temp;
1156 }
1157 queue_work(driver->diag_wq, &(driver->diag_read_smd_work));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001158}
1159
1160#if defined(CONFIG_MSM_N_WAY_SMD)
1161static void diag_smd_qdsp_notify(void *ctxt, unsigned event)
1162{
1163 queue_work(driver->diag_wq, &(driver->diag_read_smd_qdsp_work));
1164}
1165#endif
1166
1167static void diag_smd_wcnss_notify(void *ctxt, unsigned event)
1168{
1169 queue_work(driver->diag_wq, &(driver->diag_read_smd_wcnss_work));
1170}
1171
1172static int diag_smd_probe(struct platform_device *pdev)
1173{
1174 int r = 0;
1175
Shalabh Jaineefee052011-11-08 23:46:03 -08001176 if (pdev->id == SMD_APPS_MODEM) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177 r = smd_open("DIAG", &driver->ch, driver, diag_smd_notify);
Shalabh Jaineefee052011-11-08 23:46:03 -08001178 ch_temp = driver->ch;
1179 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001180#if defined(CONFIG_MSM_N_WAY_SMD)
1181 if (pdev->id == SMD_APPS_QDSP)
1182 r = smd_named_open_on_edge("DIAG", SMD_APPS_QDSP
1183 , &driver->chqdsp, driver, diag_smd_qdsp_notify);
1184#endif
1185 if (pdev->id == SMD_APPS_WCNSS)
1186 r = smd_named_open_on_edge("APPS_RIVA_DATA", SMD_APPS_WCNSS
1187 , &driver->ch_wcnss, driver, diag_smd_wcnss_notify);
1188 pm_runtime_set_active(&pdev->dev);
1189 pm_runtime_enable(&pdev->dev);
1190 pr_debug("diag: open SMD port, Id = %d, r = %d\n", pdev->id, r);
1191
1192 return 0;
1193}
1194
1195static int diagfwd_runtime_suspend(struct device *dev)
1196{
1197 dev_dbg(dev, "pm_runtime: suspending...\n");
1198 return 0;
1199}
1200
1201static int diagfwd_runtime_resume(struct device *dev)
1202{
1203 dev_dbg(dev, "pm_runtime: resuming...\n");
1204 return 0;
1205}
1206
1207static const struct dev_pm_ops diagfwd_dev_pm_ops = {
1208 .runtime_suspend = diagfwd_runtime_suspend,
1209 .runtime_resume = diagfwd_runtime_resume,
1210};
1211
1212static struct platform_driver msm_smd_ch1_driver = {
1213
1214 .probe = diag_smd_probe,
1215 .driver = {
1216 .name = "DIAG",
1217 .owner = THIS_MODULE,
1218 .pm = &diagfwd_dev_pm_ops,
1219 },
1220};
1221
1222static struct platform_driver diag_smd_lite_driver = {
1223
1224 .probe = diag_smd_probe,
1225 .driver = {
1226 .name = "APPS_RIVA_DATA",
1227 .owner = THIS_MODULE,
1228 .pm = &diagfwd_dev_pm_ops,
1229 },
1230};
1231
1232void diagfwd_init(void)
1233{
1234 diag_debug_buf_idx = 0;
1235 driver->read_len_legacy = 0;
1236 if (driver->buf_in_1 == NULL) {
1237 driver->buf_in_1 = kzalloc(IN_BUF_SIZE, GFP_KERNEL);
1238 if (driver->buf_in_1 == NULL)
1239 goto err;
1240 }
1241 if (driver->buf_in_2 == NULL) {
1242 driver->buf_in_2 = kzalloc(IN_BUF_SIZE, GFP_KERNEL);
1243 if (driver->buf_in_2 == NULL)
1244 goto err;
1245 }
1246 if (driver->buf_in_qdsp_1 == NULL) {
1247 driver->buf_in_qdsp_1 = kzalloc(IN_BUF_SIZE, GFP_KERNEL);
1248 if (driver->buf_in_qdsp_1 == NULL)
1249 goto err;
1250 }
1251 if (driver->buf_in_qdsp_2 == NULL) {
1252 driver->buf_in_qdsp_2 = kzalloc(IN_BUF_SIZE, GFP_KERNEL);
1253 if (driver->buf_in_qdsp_2 == NULL)
1254 goto err;
1255 }
1256 if (driver->buf_in_wcnss == NULL) {
1257 driver->buf_in_wcnss = kzalloc(IN_BUF_SIZE, GFP_KERNEL);
1258 if (driver->buf_in_wcnss == NULL)
1259 goto err;
1260 }
1261 if (driver->usb_buf_out == NULL &&
1262 (driver->usb_buf_out = kzalloc(USB_MAX_OUT_BUF,
1263 GFP_KERNEL)) == NULL)
1264 goto err;
1265 if (driver->hdlc_buf == NULL
1266 && (driver->hdlc_buf = kzalloc(HDLC_MAX, GFP_KERNEL)) == NULL)
1267 goto err;
Shalabh Jain69890aa2011-10-10 12:59:16 -07001268 if (driver->user_space_data == NULL)
1269 driver->user_space_data = kzalloc(USER_SPACE_DATA, GFP_KERNEL);
1270 if (driver->user_space_data == NULL)
1271 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001272 if (driver->msg_masks == NULL
1273 && (driver->msg_masks = kzalloc(MSG_MASK_SIZE,
1274 GFP_KERNEL)) == NULL)
1275 goto err;
1276 if (driver->log_masks == NULL &&
1277 (driver->log_masks = kzalloc(LOG_MASK_SIZE, GFP_KERNEL)) == NULL)
1278 goto err;
1279 driver->log_masks_length = 8*MAX_EQUIP_ID;
1280 if (driver->event_masks == NULL &&
1281 (driver->event_masks = kzalloc(EVENT_MASK_SIZE,
1282 GFP_KERNEL)) == NULL)
1283 goto err;
1284 if (driver->client_map == NULL &&
1285 (driver->client_map = kzalloc
1286 ((driver->num_clients) * sizeof(struct diag_client_map),
1287 GFP_KERNEL)) == NULL)
1288 goto err;
1289 if (driver->buf_tbl == NULL)
1290 driver->buf_tbl = kzalloc(buf_tbl_size *
1291 sizeof(struct diag_write_device), GFP_KERNEL);
1292 if (driver->buf_tbl == NULL)
1293 goto err;
1294 if (driver->data_ready == NULL &&
1295 (driver->data_ready = kzalloc(driver->num_clients * sizeof(int)
1296 , GFP_KERNEL)) == NULL)
1297 goto err;
1298 if (driver->table == NULL &&
1299 (driver->table = kzalloc(diag_max_registration*
1300 sizeof(struct diag_master_table),
1301 GFP_KERNEL)) == NULL)
1302 goto err;
1303 if (driver->write_ptr_1 == NULL) {
1304 driver->write_ptr_1 = kzalloc(
1305 sizeof(struct diag_request), GFP_KERNEL);
1306 if (driver->write_ptr_1 == NULL)
1307 goto err;
1308 }
1309 if (driver->write_ptr_2 == NULL) {
1310 driver->write_ptr_2 = kzalloc(
1311 sizeof(struct diag_request), GFP_KERNEL);
1312 if (driver->write_ptr_2 == NULL)
1313 goto err;
1314 }
1315 if (driver->write_ptr_qdsp_1 == NULL) {
1316 driver->write_ptr_qdsp_1 = kzalloc(
1317 sizeof(struct diag_request), GFP_KERNEL);
1318 if (driver->write_ptr_qdsp_1 == NULL)
1319 goto err;
1320 }
1321 if (driver->write_ptr_qdsp_2 == NULL) {
1322 driver->write_ptr_qdsp_2 = kzalloc(
1323 sizeof(struct diag_request), GFP_KERNEL);
1324 if (driver->write_ptr_qdsp_2 == NULL)
1325 goto err;
1326 }
1327 if (driver->write_ptr_wcnss == NULL) {
1328 driver->write_ptr_wcnss = kzalloc(
1329 sizeof(struct diag_request), GFP_KERNEL);
1330 if (driver->write_ptr_wcnss == NULL)
1331 goto err;
1332 }
1333 if (driver->usb_read_ptr == NULL) {
1334 driver->usb_read_ptr = kzalloc(
1335 sizeof(struct diag_request), GFP_KERNEL);
1336 if (driver->usb_read_ptr == NULL)
1337 goto err;
1338 }
1339 if (driver->pkt_buf == NULL &&
1340 (driver->pkt_buf = kzalloc(PKT_SIZE,
1341 GFP_KERNEL)) == NULL)
1342 goto err;
1343 if (driver->apps_rsp_buf == NULL) {
1344 driver->apps_rsp_buf = kzalloc(500, GFP_KERNEL);
1345 if (driver->apps_rsp_buf == NULL)
1346 goto err;
1347 }
1348 driver->diag_wq = create_singlethread_workqueue("diag_wq");
1349#ifdef CONFIG_DIAG_OVER_USB
1350 INIT_WORK(&(driver->diag_proc_hdlc_work), diag_process_hdlc_fn);
1351 INIT_WORK(&(driver->diag_read_work), diag_read_work_fn);
1352 driver->legacy_ch = usb_diag_open(DIAG_LEGACY, driver,
1353 diag_usb_legacy_notifier);
1354 if (IS_ERR(driver->legacy_ch)) {
1355 printk(KERN_ERR "Unable to open USB diag legacy channel\n");
1356 goto err;
1357 }
1358#endif
1359 platform_driver_register(&msm_smd_ch1_driver);
1360 platform_driver_register(&diag_smd_lite_driver);
1361
1362 return;
1363err:
1364 pr_err("diag: Could not initialize diag buffers");
1365 kfree(driver->buf_in_1);
1366 kfree(driver->buf_in_2);
1367 kfree(driver->buf_in_qdsp_1);
1368 kfree(driver->buf_in_qdsp_2);
1369 kfree(driver->buf_in_wcnss);
1370 kfree(driver->usb_buf_out);
1371 kfree(driver->hdlc_buf);
1372 kfree(driver->msg_masks);
1373 kfree(driver->log_masks);
1374 kfree(driver->event_masks);
1375 kfree(driver->client_map);
1376 kfree(driver->buf_tbl);
1377 kfree(driver->data_ready);
1378 kfree(driver->table);
1379 kfree(driver->pkt_buf);
1380 kfree(driver->write_ptr_1);
1381 kfree(driver->write_ptr_2);
1382 kfree(driver->write_ptr_qdsp_1);
1383 kfree(driver->write_ptr_qdsp_2);
1384 kfree(driver->write_ptr_wcnss);
1385 kfree(driver->usb_read_ptr);
1386 kfree(driver->apps_rsp_buf);
Shalabh Jain69890aa2011-10-10 12:59:16 -07001387 kfree(driver->user_space_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001388 if (driver->diag_wq)
1389 destroy_workqueue(driver->diag_wq);
1390}
1391
1392void diagfwd_exit(void)
1393{
1394 smd_close(driver->ch);
1395 smd_close(driver->chqdsp);
1396 smd_close(driver->ch_wcnss);
1397 driver->ch = 0; /* SMD can make this NULL */
1398 driver->chqdsp = 0;
1399 driver->ch_wcnss = 0;
1400#ifdef CONFIG_DIAG_OVER_USB
1401 if (driver->usb_connected)
1402 usb_diag_free_req(driver->legacy_ch);
1403 usb_diag_close(driver->legacy_ch);
1404#endif
1405 platform_driver_unregister(&msm_smd_ch1_driver);
1406 platform_driver_unregister(&diag_smd_lite_driver);
1407 kfree(driver->buf_in_1);
1408 kfree(driver->buf_in_2);
1409 kfree(driver->buf_in_qdsp_1);
1410 kfree(driver->buf_in_qdsp_2);
1411 kfree(driver->buf_in_wcnss);
1412 kfree(driver->usb_buf_out);
1413 kfree(driver->hdlc_buf);
1414 kfree(driver->msg_masks);
1415 kfree(driver->log_masks);
1416 kfree(driver->event_masks);
1417 kfree(driver->client_map);
1418 kfree(driver->buf_tbl);
1419 kfree(driver->data_ready);
1420 kfree(driver->table);
1421 kfree(driver->pkt_buf);
1422 kfree(driver->write_ptr_1);
1423 kfree(driver->write_ptr_2);
1424 kfree(driver->write_ptr_qdsp_1);
1425 kfree(driver->write_ptr_qdsp_2);
1426 kfree(driver->write_ptr_wcnss);
1427 kfree(driver->usb_read_ptr);
1428 kfree(driver->apps_rsp_buf);
Shalabh Jain69890aa2011-10-10 12:59:16 -07001429 kfree(driver->user_space_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001430 destroy_workqueue(driver->diag_wq);
1431}