blob: caf3901e6905c508f76a5b57e675f3b1aa2e54f6 [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;
46
47struct 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) {
506 if (cpu_is_msm8960() &&
507 (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 */
545 if (cpu_is_msm8960() && cmd_code == MODE_CMD) {
546 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 */
829 else if ((cpu_is_msm8x60() || cpu_is_msm8960()) && (*buf == 0x3A)) {
830 /* send response back */
831 driver->apps_rsp_buf[0] = *buf;
832 ENCODE_RSP_AND_SEND(0);
833 msleep(5000);
834 /* call download API */
835 msm_set_restart_mode(RESTART_DLOAD);
836 printk(KERN_CRIT "diag: download mode set, Rebooting SoC..\n");
837 kernel_restart(NULL);
838 /* Not required, represents that command isnt sent to modem */
839 return 0;
840 }
841 /* Check for ID for NO MODEM present */
842 else if (!(driver->ch)) {
843 /* Respond to polling for Apps only DIAG */
844 if ((*buf == 0x4b) && (*(buf+1) == 0x32) &&
845 (*(buf+2) == 0x03)) {
846 for (i = 0; i < 3; i++)
847 driver->apps_rsp_buf[i] = *(buf+i);
848 for (i = 0; i < 13; i++)
849 driver->apps_rsp_buf[i+3] = 0;
850
851 ENCODE_RSP_AND_SEND(15);
852 return 0;
853 }
854 /* respond to 0x0 command */
855 else if (*buf == 0x00) {
856 for (i = 0; i < 55; i++)
857 driver->apps_rsp_buf[i] = 0;
858
859 ENCODE_RSP_AND_SEND(54);
860 return 0;
861 }
862 /* respond to 0x7c command */
863 else if (*buf == 0x7c) {
864 driver->apps_rsp_buf[0] = 0x7c;
865 for (i = 1; i < 8; i++)
866 driver->apps_rsp_buf[i] = 0;
867 /* Tools ID for APQ 8060 */
868 *(int *)(driver->apps_rsp_buf + 8) =
869 chk_config_get_id();
870 *(unsigned char *)(driver->apps_rsp_buf + 12) = '\0';
871 *(unsigned char *)(driver->apps_rsp_buf + 13) = '\0';
872 ENCODE_RSP_AND_SEND(13);
873 return 0;
874 }
875 }
876#endif
877 return packet_type;
878}
879
880#ifdef CONFIG_DIAG_OVER_USB
881void diag_send_error_rsp(int index)
882{
883 int i;
884 driver->apps_rsp_buf[0] = 0x13; /* error code 13 */
885 for (i = 0; i < index; i++)
886 driver->apps_rsp_buf[i+1] = *(driver->hdlc_buf+i);
887 ENCODE_RSP_AND_SEND(index - 3);
888}
889#else
890static inline void diag_send_error_rsp(int index) {}
891#endif
892
893void diag_process_hdlc(void *data, unsigned len)
894{
895 struct diag_hdlc_decode_type hdlc;
896 int ret, type = 0;
897 pr_debug("diag: HDLC decode fn, len of data %d\n", len);
898 hdlc.dest_ptr = driver->hdlc_buf;
899 hdlc.dest_size = USB_MAX_OUT_BUF;
900 hdlc.src_ptr = data;
901 hdlc.src_size = len;
902 hdlc.src_idx = 0;
903 hdlc.dest_idx = 0;
904 hdlc.escaping = 0;
905
906 ret = diag_hdlc_decode(&hdlc);
907
908 if (ret)
909 type = diag_process_apps_pkt(driver->hdlc_buf,
910 hdlc.dest_idx - 3);
911 else if (driver->debug_flag) {
912 printk(KERN_ERR "Packet dropped due to bad HDLC coding/CRC"
913 " errors or partial packet received, packet"
914 " length = %d\n", len);
915 print_hex_dump(KERN_DEBUG, "Dropped Packet Data: ", 16, 1,
916 DUMP_PREFIX_ADDRESS, data, len, 1);
917 driver->debug_flag = 0;
918 }
919 /* send error responses from APPS for Central Routing */
Shalabh Jainfb8e3c12011-10-19 17:29:42 -0700920 if (type == 1 && chk_apps_only()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700921 diag_send_error_rsp(hdlc.dest_idx);
922 type = 0;
923 }
924 /* implies this packet is NOT meant for apps */
925 if (!(driver->ch) && type == 1) {
Shalabh Jainfb8e3c12011-10-19 17:29:42 -0700926 if (chk_apps_only()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700927 diag_send_error_rsp(hdlc.dest_idx);
928 } else { /* APQ 8060, Let Q6 respond */
929 if (driver->chqdsp)
930 smd_write(driver->chqdsp, driver->hdlc_buf,
931 hdlc.dest_idx - 3);
932 }
933 type = 0;
934 }
935
936#ifdef DIAG_DEBUG
937 pr_debug("diag: hdlc.dest_idx = %d", hdlc.dest_idx);
938 for (i = 0; i < hdlc.dest_idx; i++)
939 printk(KERN_DEBUG "\t%x", *(((unsigned char *)
940 driver->hdlc_buf)+i));
941#endif /* DIAG DEBUG */
942 /* ignore 2 bytes for CRC, one for 7E and send */
943 if ((driver->ch) && (ret) && (type) && (hdlc.dest_idx > 3)) {
944 APPEND_DEBUG('g');
945 smd_write(driver->ch, driver->hdlc_buf, hdlc.dest_idx - 3);
946 APPEND_DEBUG('h');
947#ifdef DIAG_DEBUG
948 printk(KERN_INFO "writing data to SMD, pkt length %d\n", len);
949 print_hex_dump(KERN_DEBUG, "Written Packet Data to SMD: ", 16,
950 1, DUMP_PREFIX_ADDRESS, data, len, 1);
951#endif /* DIAG DEBUG */
952 }
953}
954
955#ifdef CONFIG_DIAG_OVER_USB
Shalabh Jain8e9750a2011-09-09 13:06:29 -0700956/* 2+1 for modem ; 2 for LPASS ; 1 for WCNSS */
957#define N_LEGACY_WRITE (driver->poolsize + 6)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700958#define N_LEGACY_READ 1
959
960int diagfwd_connect(void)
961{
962 int err;
963
964 printk(KERN_DEBUG "diag: USB connected\n");
965 err = usb_diag_alloc_req(driver->legacy_ch, N_LEGACY_WRITE,
966 N_LEGACY_READ);
967 if (err)
968 printk(KERN_ERR "diag: unable to alloc USB req on legacy ch");
969
970 driver->usb_connected = 1;
971 driver->in_busy_1 = 0;
972 driver->in_busy_2 = 0;
973 driver->in_busy_qdsp_1 = 0;
974 driver->in_busy_qdsp_2 = 0;
975 driver->in_busy_wcnss = 0;
976
977 /* Poll SMD channels to check for data*/
978 queue_work(driver->diag_wq, &(driver->diag_read_smd_work));
979 queue_work(driver->diag_wq, &(driver->diag_read_smd_qdsp_work));
980 queue_work(driver->diag_wq, &(driver->diag_read_smd_wcnss_work));
Shalabh Jaincf5f20e2011-08-22 12:29:52 -0700981 /* Poll SMD CNTL channels to check for data */
982 queue_work(driver->diag_wq, &(driver->diag_read_smd_cntl_work));
983 queue_work(driver->diag_wq, &(driver->diag_read_smd_qdsp_cntl_work));
984 queue_work(driver->diag_wq, &(driver->diag_read_smd_wcnss_cntl_work));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700985 /* Poll USB channel to check for data*/
986 queue_work(driver->diag_wq, &(driver->diag_read_work));
987#ifdef CONFIG_DIAG_SDIO_PIPE
988 if (machine_is_msm8x60_fusion() || machine_is_msm8x60_fusn_ffa()) {
989 if (driver->mdm_ch && !IS_ERR(driver->mdm_ch))
990 diagfwd_connect_sdio();
991 else
992 printk(KERN_INFO "diag: No USB MDM ch");
993 }
994#endif
995 return 0;
996}
997
998int diagfwd_disconnect(void)
999{
1000 printk(KERN_DEBUG "diag: USB disconnected\n");
1001 driver->usb_connected = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001002 driver->debug_flag = 1;
1003 usb_diag_free_req(driver->legacy_ch);
Shalabh Jain69890aa2011-10-10 12:59:16 -07001004 if (driver->logging_mode == USB_MODE) {
1005 driver->in_busy_1 = 1;
1006 driver->in_busy_2 = 1;
1007 driver->in_busy_qdsp_1 = 1;
1008 driver->in_busy_qdsp_2 = 1;
1009 driver->in_busy_wcnss = 1;
1010 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001011#ifdef CONFIG_DIAG_SDIO_PIPE
1012 if (machine_is_msm8x60_fusion() || machine_is_msm8x60_fusn_ffa())
1013 if (driver->mdm_ch && !IS_ERR(driver->mdm_ch))
1014 diagfwd_disconnect_sdio();
1015#endif
1016 /* TBD - notify and flow control SMD */
1017 return 0;
1018}
1019
1020int diagfwd_write_complete(struct diag_request *diag_write_ptr)
1021{
1022 unsigned char *buf = diag_write_ptr->buf;
1023 /*Determine if the write complete is for data from modem/apps/q6 */
1024 /* Need a context variable here instead */
1025 if (buf == (void *)driver->buf_in_1) {
1026 driver->in_busy_1 = 0;
1027 APPEND_DEBUG('o');
1028 queue_work(driver->diag_wq, &(driver->diag_read_smd_work));
1029 } else if (buf == (void *)driver->buf_in_2) {
1030 driver->in_busy_2 = 0;
1031 APPEND_DEBUG('O');
1032 queue_work(driver->diag_wq, &(driver->diag_read_smd_work));
1033 } else if (buf == (void *)driver->buf_in_qdsp_1) {
1034 driver->in_busy_qdsp_1 = 0;
1035 APPEND_DEBUG('p');
1036 queue_work(driver->diag_wq, &(driver->diag_read_smd_qdsp_work));
1037 } else if (buf == (void *)driver->buf_in_qdsp_2) {
1038 driver->in_busy_qdsp_2 = 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_wcnss) {
1042 driver->in_busy_wcnss = 0;
1043 APPEND_DEBUG('R');
1044 queue_work(driver->diag_wq,
1045 &(driver->diag_read_smd_wcnss_work));
1046 }
1047#ifdef CONFIG_DIAG_SDIO_PIPE
1048 else if (buf == (void *)driver->buf_in_sdio)
1049 if (machine_is_msm8x60_fusion() ||
1050 machine_is_msm8x60_fusn_ffa())
1051 diagfwd_write_complete_sdio();
1052 else
1053 pr_err("diag: Incorrect buffer pointer while WRITE");
1054#endif
1055 else {
1056 diagmem_free(driver, (unsigned char *)buf, POOL_TYPE_HDLC);
1057 diagmem_free(driver, (unsigned char *)diag_write_ptr,
1058 POOL_TYPE_WRITE_STRUCT);
1059 APPEND_DEBUG('q');
1060 }
1061 return 0;
1062}
1063
1064int diagfwd_read_complete(struct diag_request *diag_read_ptr)
1065{
1066 int status = diag_read_ptr->status;
1067 unsigned char *buf = diag_read_ptr->buf;
1068
1069 /* Determine if the read complete is for data on legacy/mdm ch */
1070 if (buf == (void *)driver->usb_buf_out) {
1071 driver->read_len_legacy = diag_read_ptr->actual;
1072 APPEND_DEBUG('s');
1073#ifdef DIAG_DEBUG
1074 printk(KERN_INFO "read data from USB, pkt length %d",
1075 diag_read_ptr->actual);
1076 print_hex_dump(KERN_DEBUG, "Read Packet Data from USB: ", 16, 1,
1077 DUMP_PREFIX_ADDRESS, diag_read_ptr->buf,
1078 diag_read_ptr->actual, 1);
1079#endif /* DIAG DEBUG */
1080 if (driver->logging_mode == USB_MODE) {
1081 if (status != -ECONNRESET && status != -ESHUTDOWN)
1082 queue_work(driver->diag_wq,
1083 &(driver->diag_proc_hdlc_work));
1084 else
1085 queue_work(driver->diag_wq,
1086 &(driver->diag_read_work));
1087 }
1088 }
1089#ifdef CONFIG_DIAG_SDIO_PIPE
1090 else if (buf == (void *)driver->usb_buf_mdm_out) {
1091 if (machine_is_msm8x60_fusion() ||
1092 machine_is_msm8x60_fusn_ffa()) {
1093 driver->read_len_mdm = diag_read_ptr->actual;
1094 diagfwd_read_complete_sdio();
1095 } else
1096 pr_err("diag: Incorrect buffer pointer while READ");
1097 }
1098#endif
1099 else
1100 printk(KERN_ERR "diag: Unknown buffer ptr from USB");
1101
1102 return 0;
1103}
1104
1105void diag_read_work_fn(struct work_struct *work)
1106{
1107 APPEND_DEBUG('d');
1108 driver->usb_read_ptr->buf = driver->usb_buf_out;
1109 driver->usb_read_ptr->length = USB_MAX_OUT_BUF;
1110 usb_diag_read(driver->legacy_ch, driver->usb_read_ptr);
1111 APPEND_DEBUG('e');
1112}
1113
1114void diag_process_hdlc_fn(struct work_struct *work)
1115{
1116 APPEND_DEBUG('D');
1117 diag_process_hdlc(driver->usb_buf_out, driver->read_len_legacy);
1118 diag_read_work_fn(work);
1119 APPEND_DEBUG('E');
1120}
1121
1122void diag_usb_legacy_notifier(void *priv, unsigned event,
1123 struct diag_request *d_req)
1124{
1125 switch (event) {
1126 case USB_DIAG_CONNECT:
1127 diagfwd_connect();
1128 break;
1129 case USB_DIAG_DISCONNECT:
1130 diagfwd_disconnect();
1131 break;
1132 case USB_DIAG_READ_DONE:
1133 diagfwd_read_complete(d_req);
1134 break;
1135 case USB_DIAG_WRITE_DONE:
1136 diagfwd_write_complete(d_req);
1137 break;
1138 default:
1139 printk(KERN_ERR "Unknown event from USB diag\n");
1140 break;
1141 }
1142}
1143
1144#endif /* DIAG OVER USB */
1145
1146static void diag_smd_notify(void *ctxt, unsigned event)
1147{
Shalabh Jainc2ec8292011-10-14 12:34:55 -07001148 if (event == SMD_EVENT_CLOSE) {
1149 pr_info("diag: clean modem registration\n");
1150 diag_clear_reg(MODEM_PROC);
1151 } else
1152 queue_work(driver->diag_wq, &(driver->diag_read_smd_work));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001153}
1154
1155#if defined(CONFIG_MSM_N_WAY_SMD)
1156static void diag_smd_qdsp_notify(void *ctxt, unsigned event)
1157{
1158 queue_work(driver->diag_wq, &(driver->diag_read_smd_qdsp_work));
1159}
1160#endif
1161
1162static void diag_smd_wcnss_notify(void *ctxt, unsigned event)
1163{
1164 queue_work(driver->diag_wq, &(driver->diag_read_smd_wcnss_work));
1165}
1166
1167static int diag_smd_probe(struct platform_device *pdev)
1168{
1169 int r = 0;
1170
1171 if (pdev->id == SMD_APPS_MODEM)
1172 r = smd_open("DIAG", &driver->ch, driver, diag_smd_notify);
1173#if defined(CONFIG_MSM_N_WAY_SMD)
1174 if (pdev->id == SMD_APPS_QDSP)
1175 r = smd_named_open_on_edge("DIAG", SMD_APPS_QDSP
1176 , &driver->chqdsp, driver, diag_smd_qdsp_notify);
1177#endif
1178 if (pdev->id == SMD_APPS_WCNSS)
1179 r = smd_named_open_on_edge("APPS_RIVA_DATA", SMD_APPS_WCNSS
1180 , &driver->ch_wcnss, driver, diag_smd_wcnss_notify);
1181 pm_runtime_set_active(&pdev->dev);
1182 pm_runtime_enable(&pdev->dev);
1183 pr_debug("diag: open SMD port, Id = %d, r = %d\n", pdev->id, r);
1184
1185 return 0;
1186}
1187
1188static int diagfwd_runtime_suspend(struct device *dev)
1189{
1190 dev_dbg(dev, "pm_runtime: suspending...\n");
1191 return 0;
1192}
1193
1194static int diagfwd_runtime_resume(struct device *dev)
1195{
1196 dev_dbg(dev, "pm_runtime: resuming...\n");
1197 return 0;
1198}
1199
1200static const struct dev_pm_ops diagfwd_dev_pm_ops = {
1201 .runtime_suspend = diagfwd_runtime_suspend,
1202 .runtime_resume = diagfwd_runtime_resume,
1203};
1204
1205static struct platform_driver msm_smd_ch1_driver = {
1206
1207 .probe = diag_smd_probe,
1208 .driver = {
1209 .name = "DIAG",
1210 .owner = THIS_MODULE,
1211 .pm = &diagfwd_dev_pm_ops,
1212 },
1213};
1214
1215static struct platform_driver diag_smd_lite_driver = {
1216
1217 .probe = diag_smd_probe,
1218 .driver = {
1219 .name = "APPS_RIVA_DATA",
1220 .owner = THIS_MODULE,
1221 .pm = &diagfwd_dev_pm_ops,
1222 },
1223};
1224
1225void diagfwd_init(void)
1226{
1227 diag_debug_buf_idx = 0;
1228 driver->read_len_legacy = 0;
1229 if (driver->buf_in_1 == NULL) {
1230 driver->buf_in_1 = kzalloc(IN_BUF_SIZE, GFP_KERNEL);
1231 if (driver->buf_in_1 == NULL)
1232 goto err;
1233 }
1234 if (driver->buf_in_2 == NULL) {
1235 driver->buf_in_2 = kzalloc(IN_BUF_SIZE, GFP_KERNEL);
1236 if (driver->buf_in_2 == NULL)
1237 goto err;
1238 }
1239 if (driver->buf_in_qdsp_1 == NULL) {
1240 driver->buf_in_qdsp_1 = kzalloc(IN_BUF_SIZE, GFP_KERNEL);
1241 if (driver->buf_in_qdsp_1 == NULL)
1242 goto err;
1243 }
1244 if (driver->buf_in_qdsp_2 == NULL) {
1245 driver->buf_in_qdsp_2 = kzalloc(IN_BUF_SIZE, GFP_KERNEL);
1246 if (driver->buf_in_qdsp_2 == NULL)
1247 goto err;
1248 }
1249 if (driver->buf_in_wcnss == NULL) {
1250 driver->buf_in_wcnss = kzalloc(IN_BUF_SIZE, GFP_KERNEL);
1251 if (driver->buf_in_wcnss == NULL)
1252 goto err;
1253 }
1254 if (driver->usb_buf_out == NULL &&
1255 (driver->usb_buf_out = kzalloc(USB_MAX_OUT_BUF,
1256 GFP_KERNEL)) == NULL)
1257 goto err;
1258 if (driver->hdlc_buf == NULL
1259 && (driver->hdlc_buf = kzalloc(HDLC_MAX, GFP_KERNEL)) == NULL)
1260 goto err;
Shalabh Jain69890aa2011-10-10 12:59:16 -07001261 if (driver->user_space_data == NULL)
1262 driver->user_space_data = kzalloc(USER_SPACE_DATA, GFP_KERNEL);
1263 if (driver->user_space_data == NULL)
1264 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001265 if (driver->msg_masks == NULL
1266 && (driver->msg_masks = kzalloc(MSG_MASK_SIZE,
1267 GFP_KERNEL)) == NULL)
1268 goto err;
1269 if (driver->log_masks == NULL &&
1270 (driver->log_masks = kzalloc(LOG_MASK_SIZE, GFP_KERNEL)) == NULL)
1271 goto err;
1272 driver->log_masks_length = 8*MAX_EQUIP_ID;
1273 if (driver->event_masks == NULL &&
1274 (driver->event_masks = kzalloc(EVENT_MASK_SIZE,
1275 GFP_KERNEL)) == NULL)
1276 goto err;
1277 if (driver->client_map == NULL &&
1278 (driver->client_map = kzalloc
1279 ((driver->num_clients) * sizeof(struct diag_client_map),
1280 GFP_KERNEL)) == NULL)
1281 goto err;
1282 if (driver->buf_tbl == NULL)
1283 driver->buf_tbl = kzalloc(buf_tbl_size *
1284 sizeof(struct diag_write_device), GFP_KERNEL);
1285 if (driver->buf_tbl == NULL)
1286 goto err;
1287 if (driver->data_ready == NULL &&
1288 (driver->data_ready = kzalloc(driver->num_clients * sizeof(int)
1289 , GFP_KERNEL)) == NULL)
1290 goto err;
1291 if (driver->table == NULL &&
1292 (driver->table = kzalloc(diag_max_registration*
1293 sizeof(struct diag_master_table),
1294 GFP_KERNEL)) == NULL)
1295 goto err;
1296 if (driver->write_ptr_1 == NULL) {
1297 driver->write_ptr_1 = kzalloc(
1298 sizeof(struct diag_request), GFP_KERNEL);
1299 if (driver->write_ptr_1 == NULL)
1300 goto err;
1301 }
1302 if (driver->write_ptr_2 == NULL) {
1303 driver->write_ptr_2 = kzalloc(
1304 sizeof(struct diag_request), GFP_KERNEL);
1305 if (driver->write_ptr_2 == NULL)
1306 goto err;
1307 }
1308 if (driver->write_ptr_qdsp_1 == NULL) {
1309 driver->write_ptr_qdsp_1 = kzalloc(
1310 sizeof(struct diag_request), GFP_KERNEL);
1311 if (driver->write_ptr_qdsp_1 == NULL)
1312 goto err;
1313 }
1314 if (driver->write_ptr_qdsp_2 == NULL) {
1315 driver->write_ptr_qdsp_2 = kzalloc(
1316 sizeof(struct diag_request), GFP_KERNEL);
1317 if (driver->write_ptr_qdsp_2 == NULL)
1318 goto err;
1319 }
1320 if (driver->write_ptr_wcnss == NULL) {
1321 driver->write_ptr_wcnss = kzalloc(
1322 sizeof(struct diag_request), GFP_KERNEL);
1323 if (driver->write_ptr_wcnss == NULL)
1324 goto err;
1325 }
1326 if (driver->usb_read_ptr == NULL) {
1327 driver->usb_read_ptr = kzalloc(
1328 sizeof(struct diag_request), GFP_KERNEL);
1329 if (driver->usb_read_ptr == NULL)
1330 goto err;
1331 }
1332 if (driver->pkt_buf == NULL &&
1333 (driver->pkt_buf = kzalloc(PKT_SIZE,
1334 GFP_KERNEL)) == NULL)
1335 goto err;
1336 if (driver->apps_rsp_buf == NULL) {
1337 driver->apps_rsp_buf = kzalloc(500, GFP_KERNEL);
1338 if (driver->apps_rsp_buf == NULL)
1339 goto err;
1340 }
1341 driver->diag_wq = create_singlethread_workqueue("diag_wq");
1342#ifdef CONFIG_DIAG_OVER_USB
1343 INIT_WORK(&(driver->diag_proc_hdlc_work), diag_process_hdlc_fn);
1344 INIT_WORK(&(driver->diag_read_work), diag_read_work_fn);
1345 driver->legacy_ch = usb_diag_open(DIAG_LEGACY, driver,
1346 diag_usb_legacy_notifier);
1347 if (IS_ERR(driver->legacy_ch)) {
1348 printk(KERN_ERR "Unable to open USB diag legacy channel\n");
1349 goto err;
1350 }
1351#endif
1352 platform_driver_register(&msm_smd_ch1_driver);
1353 platform_driver_register(&diag_smd_lite_driver);
1354
1355 return;
1356err:
1357 pr_err("diag: Could not initialize diag buffers");
1358 kfree(driver->buf_in_1);
1359 kfree(driver->buf_in_2);
1360 kfree(driver->buf_in_qdsp_1);
1361 kfree(driver->buf_in_qdsp_2);
1362 kfree(driver->buf_in_wcnss);
1363 kfree(driver->usb_buf_out);
1364 kfree(driver->hdlc_buf);
1365 kfree(driver->msg_masks);
1366 kfree(driver->log_masks);
1367 kfree(driver->event_masks);
1368 kfree(driver->client_map);
1369 kfree(driver->buf_tbl);
1370 kfree(driver->data_ready);
1371 kfree(driver->table);
1372 kfree(driver->pkt_buf);
1373 kfree(driver->write_ptr_1);
1374 kfree(driver->write_ptr_2);
1375 kfree(driver->write_ptr_qdsp_1);
1376 kfree(driver->write_ptr_qdsp_2);
1377 kfree(driver->write_ptr_wcnss);
1378 kfree(driver->usb_read_ptr);
1379 kfree(driver->apps_rsp_buf);
Shalabh Jain69890aa2011-10-10 12:59:16 -07001380 kfree(driver->user_space_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001381 if (driver->diag_wq)
1382 destroy_workqueue(driver->diag_wq);
1383}
1384
1385void diagfwd_exit(void)
1386{
1387 smd_close(driver->ch);
1388 smd_close(driver->chqdsp);
1389 smd_close(driver->ch_wcnss);
1390 driver->ch = 0; /* SMD can make this NULL */
1391 driver->chqdsp = 0;
1392 driver->ch_wcnss = 0;
1393#ifdef CONFIG_DIAG_OVER_USB
1394 if (driver->usb_connected)
1395 usb_diag_free_req(driver->legacy_ch);
1396 usb_diag_close(driver->legacy_ch);
1397#endif
1398 platform_driver_unregister(&msm_smd_ch1_driver);
1399 platform_driver_unregister(&diag_smd_lite_driver);
1400 kfree(driver->buf_in_1);
1401 kfree(driver->buf_in_2);
1402 kfree(driver->buf_in_qdsp_1);
1403 kfree(driver->buf_in_qdsp_2);
1404 kfree(driver->buf_in_wcnss);
1405 kfree(driver->usb_buf_out);
1406 kfree(driver->hdlc_buf);
1407 kfree(driver->msg_masks);
1408 kfree(driver->log_masks);
1409 kfree(driver->event_masks);
1410 kfree(driver->client_map);
1411 kfree(driver->buf_tbl);
1412 kfree(driver->data_ready);
1413 kfree(driver->table);
1414 kfree(driver->pkt_buf);
1415 kfree(driver->write_ptr_1);
1416 kfree(driver->write_ptr_2);
1417 kfree(driver->write_ptr_qdsp_1);
1418 kfree(driver->write_ptr_qdsp_2);
1419 kfree(driver->write_ptr_wcnss);
1420 kfree(driver->usb_read_ptr);
1421 kfree(driver->apps_rsp_buf);
Shalabh Jain69890aa2011-10-10 12:59:16 -07001422 kfree(driver->user_space_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001423 destroy_workqueue(driver->diag_wq);
1424}