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