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