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