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