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