1 /* Copyright (c) 2012-2019, The Linux Foundation. All rights reserved.
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.
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.
13 #include <linux/slab.h>
14 #include <linux/init.h>
15 #include <linux/uaccess.h>
16 #include <linux/diagchar.h>
17 #include <linux/sched.h>
18 #include <linux/err.h>
19 #include <linux/delay.h>
20 #include <linux/workqueue.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_wakeup.h>
24 #include <linux/spinlock.h>
25 #include <linux/ratelimit.h>
26 #include <linux/reboot.h>
27 #include <asm/current.h>
28 #include <soc/qcom/restart.h>
29 #include <linux/vmalloc.h>
30 #ifdef CONFIG_DIAG_OVER_USB
31 #include <linux/usb/usbdiag.h>
33 #include "diagchar_hdlc.h"
37 #include "diagfwd_cntl.h"
39 #include "diag_masks.h"
40 #include "diagfwd_bridge.h"
41 #include "diagfwd_peripheral.h"
42 #include "diag_ipc_logging.h"
44 static struct timer_list dci_drain_timer;
45 static int dci_timer_in_progress;
46 static struct work_struct dci_data_drain_work;
48 struct diag_dci_partial_pkt_t partial_pkt;
50 unsigned int dci_max_reg = 100;
51 unsigned int dci_max_clients = 10;
52 struct mutex dci_log_mask_mutex;
53 struct mutex dci_event_mask_mutex;
56 * DCI_HANDSHAKE_RETRY_TIME: Time to wait (in microseconds) before checking the
57 * connection status again.
59 * DCI_HANDSHAKE_WAIT_TIME: Timeout (in milliseconds) to check for dci
62 #define DCI_HANDSHAKE_RETRY_TIME 500000
63 #define DCI_HANDSHAKE_WAIT_TIME 200
66 unsigned long ws_lock_flags;
68 struct dci_ops_tbl_t dci_ops_tbl[NUM_DCI_PROC] = {
71 .send_log_mask = diag_send_dci_log_mask,
72 .send_event_mask = diag_send_dci_event_mask,
73 .peripheral_status = 0,
76 #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
78 .ctx = DIAGFWD_MDM_DCI,
79 .send_log_mask = diag_send_dci_log_mask_remote,
80 .send_event_mask = diag_send_dci_event_mask_remote,
81 .peripheral_status = 0,
82 .mempool = POOL_TYPE_MDM_DCI_WRITE,
87 struct dci_channel_status_t dci_channel_status[NUM_DCI_PROC] = {
93 #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
95 .id = DIAGFWD_MDM_DCI,
102 /* Number of milliseconds anticipated to process the DCI data */
103 #define DCI_WAKEUP_TIMEOUT 1
105 #define DCI_CAN_ADD_BUF_TO_LIST(buf) \
106 (buf && buf->data && !buf->in_busy && buf->data_len > 0) \
108 #ifdef CONFIG_DEBUG_FS
109 struct diag_dci_data_info *dci_traffic;
110 struct mutex dci_stat_mutex;
111 void diag_dci_record_traffic(int read_bytes, uint8_t ch_type,
112 uint8_t peripheral, uint8_t proc)
114 static int curr_dci_data;
115 static unsigned long iteration;
116 struct diag_dci_data_info *temp_data = dci_traffic;
119 mutex_lock(&dci_stat_mutex);
120 if (curr_dci_data == DIAG_DCI_DEBUG_CNT)
122 temp_data += curr_dci_data;
123 temp_data->iteration = iteration + 1;
124 temp_data->data_size = read_bytes;
125 temp_data->peripheral = peripheral;
126 temp_data->ch_type = ch_type;
127 temp_data->proc = proc;
128 diag_get_timestamp(temp_data->time_stamp);
131 mutex_unlock(&dci_stat_mutex);
134 void diag_dci_record_traffic(int read_bytes, uint8_t ch_type,
135 uint8_t peripheral, uint8_t proc) { }
138 static int check_peripheral_dci_support(int peripheral_id, int dci_proc_id)
140 int dci_peripheral_list = 0;
142 if (dci_proc_id < 0 || dci_proc_id >= NUM_DCI_PROC) {
143 pr_err("diag:In %s,not a supported DCI proc id\n", __func__);
146 if (peripheral_id < 0 || peripheral_id >= NUM_PERIPHERALS) {
147 pr_err("diag:In %s,not a valid peripheral id\n", __func__);
150 dci_peripheral_list = dci_ops_tbl[dci_proc_id].peripheral_status;
152 if (dci_peripheral_list <= 0 || dci_peripheral_list > DIAG_CON_ALL) {
153 pr_err("diag:In %s,not a valid dci peripheral mask\n",
157 /* Remove APSS bit mask information */
158 dci_peripheral_list = dci_peripheral_list >> 1;
160 if ((1 << peripheral_id) & (dci_peripheral_list))
166 static void create_dci_log_mask_tbl(unsigned char *mask, uint8_t dirty)
168 unsigned char *temp = mask;
174 /* create hard coded table for log mask with 16 categories */
175 for (i = 0; i < DCI_MAX_LOG_CODES; i++) {
178 *temp = dirty ? 1 : 0;
180 memset(temp, 0, DCI_MAX_ITEMS_PER_LOG_CODE);
181 temp += DCI_MAX_ITEMS_PER_LOG_CODE;
185 static void create_dci_event_mask_tbl(unsigned char *tbl_buf)
188 memset(tbl_buf, 0, DCI_EVENT_MASK_SIZE);
191 void dci_drain_data(unsigned long data)
193 queue_work(driver->diag_dci_wq, &dci_data_drain_work);
196 static void dci_check_drain_timer(void)
198 if (!dci_timer_in_progress) {
199 dci_timer_in_progress = 1;
200 mod_timer(&dci_drain_timer, jiffies + msecs_to_jiffies(200));
204 #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
205 static void dci_handshake_work_fn(struct work_struct *work)
210 struct dci_channel_status_t *status = container_of(work,
211 struct dci_channel_status_t,
215 pr_debug("diag: In %s, remote dci channel is open, index: %d\n",
216 __func__, status->id);
220 if (status->retry_count == max_retries) {
221 status->retry_count = 0;
222 pr_info("diag: dci channel connection handshake timed out, id: %d\n",
224 err = diagfwd_bridge_close(TOKEN_TO_BRIDGE(status->id));
226 pr_err("diag: In %s, unable to close dci channel id: %d, err: %d\n",
227 __func__, status->id, err);
231 status->retry_count++;
233 * Sleep for sometime to check for the connection status again. The
234 * value should be optimum to include a roundabout time for a small
235 * packet to the remote processor.
237 usleep_range(DCI_HANDSHAKE_RETRY_TIME, DCI_HANDSHAKE_RETRY_TIME + 100);
238 mod_timer(&status->wait_time,
239 jiffies + msecs_to_jiffies(DCI_HANDSHAKE_WAIT_TIME));
242 static void dci_chk_handshake(unsigned long data)
244 int index = (int)data;
246 if (index < 0 || index >= NUM_DCI_PROC)
249 queue_work(driver->diag_dci_wq,
250 &dci_channel_status[index].handshake_work);
254 static int diag_dci_init_buffer(struct diag_dci_buffer_t *buffer, int type)
256 if (!buffer || buffer->data)
260 case DCI_BUF_PRIMARY:
261 buffer->capacity = IN_BUF_SIZE;
262 buffer->data = vzalloc(buffer->capacity);
266 case DCI_BUF_SECONDARY:
268 buffer->capacity = IN_BUF_SIZE;
271 buffer->capacity = DIAG_MAX_REQ_SIZE + DCI_BUF_SIZE;
272 buffer->data = vzalloc(buffer->capacity);
277 pr_err("diag: In %s, unknown type %d", __func__, type);
281 buffer->data_len = 0;
283 buffer->buf_type = type;
284 mutex_init(&buffer->data_mutex);
289 static inline int diag_dci_check_buffer(struct diag_dci_buffer_t *buf, int len)
294 /* Return 1 if the buffer is not busy and can hold new data */
295 if ((buf->data_len + len < buf->capacity) && !buf->in_busy)
301 static void dci_add_buffer_to_list(struct diag_dci_client_tbl *client,
302 struct diag_dci_buffer_t *buf)
304 if (!buf || !client || !buf->data)
307 if (buf->in_list || buf->data_len == 0)
310 mutex_lock(&client->write_buf_mutex);
311 list_add_tail(&buf->buf_track, &client->list_write_buf);
313 * In the case of DCI, there can be multiple packets in one read. To
314 * calculate the wakeup source reference count, we must account for each
315 * packet in a single read.
317 diag_ws_on_read(DIAG_WS_DCI, buf->data_len);
318 mutex_lock(&buf->data_mutex);
321 mutex_unlock(&buf->data_mutex);
322 mutex_unlock(&client->write_buf_mutex);
325 static int diag_dci_get_buffer(struct diag_dci_client_tbl *client,
326 int data_source, int len)
328 struct diag_dci_buffer_t *buf_primary = NULL;
329 struct diag_dci_buffer_t *buf_temp = NULL;
330 struct diag_dci_buffer_t *curr = NULL;
334 if (len < 0 || len > IN_BUF_SIZE)
337 curr = client->buffers[data_source].buf_curr;
338 buf_primary = client->buffers[data_source].buf_primary;
340 if (curr && diag_dci_check_buffer(curr, len) == 1)
343 dci_add_buffer_to_list(client, curr);
344 client->buffers[data_source].buf_curr = NULL;
346 if (diag_dci_check_buffer(buf_primary, len) == 1) {
347 client->buffers[data_source].buf_curr = buf_primary;
351 buf_temp = kzalloc(sizeof(struct diag_dci_buffer_t), GFP_KERNEL);
355 if (!diag_dci_init_buffer(buf_temp, DCI_BUF_SECONDARY)) {
356 buf_temp->data = diagmem_alloc(driver, IN_BUF_SIZE,
358 if (!buf_temp->data) {
363 client->buffers[data_source].buf_curr = buf_temp;
372 void diag_dci_wakeup_clients()
374 struct list_head *start, *temp;
375 struct diag_dci_client_tbl *entry = NULL;
377 mutex_lock(&driver->dci_mutex);
378 list_for_each_safe(start, temp, &driver->dci_client_list) {
379 entry = list_entry(start, struct diag_dci_client_tbl, track);
382 * Don't wake up the client when there is no pending buffer to
383 * write or when it is writing to user space
385 if (!list_empty(&entry->list_write_buf) && !entry->in_service) {
386 mutex_lock(&entry->write_buf_mutex);
387 entry->in_service = 1;
388 mutex_unlock(&entry->write_buf_mutex);
389 diag_update_sleeping_process(entry->client->tgid,
393 mutex_unlock(&driver->dci_mutex);
396 void dci_data_drain_work_fn(struct work_struct *work)
399 struct list_head *start, *temp;
400 struct diag_dci_client_tbl *entry = NULL;
401 struct diag_dci_buf_peripheral_t *proc_buf = NULL;
402 struct diag_dci_buffer_t *buf_temp = NULL;
404 mutex_lock(&driver->dci_mutex);
405 list_for_each_safe(start, temp, &driver->dci_client_list) {
406 entry = list_entry(start, struct diag_dci_client_tbl, track);
407 for (i = 0; i < entry->num_buffers; i++) {
408 proc_buf = &entry->buffers[i];
410 mutex_lock(&proc_buf->buf_mutex);
411 buf_temp = proc_buf->buf_primary;
412 if (DCI_CAN_ADD_BUF_TO_LIST(buf_temp))
413 dci_add_buffer_to_list(entry, buf_temp);
415 buf_temp = proc_buf->buf_cmd;
416 if (DCI_CAN_ADD_BUF_TO_LIST(buf_temp))
417 dci_add_buffer_to_list(entry, buf_temp);
419 buf_temp = proc_buf->buf_curr;
420 if (DCI_CAN_ADD_BUF_TO_LIST(buf_temp)) {
421 dci_add_buffer_to_list(entry, buf_temp);
422 proc_buf->buf_curr = NULL;
424 mutex_unlock(&proc_buf->buf_mutex);
426 if (!list_empty(&entry->list_write_buf) && !entry->in_service) {
427 mutex_lock(&entry->write_buf_mutex);
428 entry->in_service = 1;
429 mutex_unlock(&entry->write_buf_mutex);
430 diag_update_sleeping_process(entry->client->tgid,
434 mutex_unlock(&driver->dci_mutex);
435 dci_timer_in_progress = 0;
438 static int diag_process_single_dci_pkt(unsigned char *buf, int len,
439 int data_source, int token)
441 uint8_t cmd_code = 0;
443 if (!buf || len < 0) {
444 pr_err("diag: Invalid input in %s, buf: %pK, len: %d\n",
449 cmd_code = *(uint8_t *)buf;
453 extract_dci_log(buf, len, data_source, token, NULL);
456 extract_dci_events(buf, len, data_source, token, NULL);
458 case EXT_HDR_CMD_CODE:
459 extract_dci_ext_pkt(buf, len, data_source, token);
461 case DCI_PKT_RSP_CODE:
462 case DCI_DELAYED_RSP_CODE:
463 extract_dci_pkt_rsp(buf, len, data_source, token);
465 case DCI_CONTROL_PKT_CODE:
466 extract_dci_ctrl_pkt(buf, len, token);
469 pr_err("diag: Unable to process single DCI packet, cmd_code: %d, data_source: %d",
470 cmd_code, data_source);
477 /* Process the data read from apps userspace client */
478 void diag_process_apps_dci_read_data(int data_type, void *buf, int recd_bytes)
483 pr_err_ratelimited("diag: In %s, Null buf pointer\n", __func__);
487 if (data_type != DATA_TYPE_DCI_LOG && data_type != DATA_TYPE_DCI_EVENT
488 && data_type != DCI_PKT_TYPE) {
489 pr_err("diag: In %s, unsupported data_type: 0x%x\n",
490 __func__, (unsigned int)data_type);
494 err = diag_process_single_dci_pkt(buf, recd_bytes, APPS_DATA,
499 /* wake up all sleeping DCI clients which have some data */
500 diag_dci_wakeup_clients();
501 dci_check_drain_timer();
504 void diag_process_remote_dci_read_data(int index, void *buf, int recd_bytes)
506 int read_bytes = 0, err = 0;
507 uint16_t dci_pkt_len;
508 struct diag_dci_header_t *header = NULL;
509 int header_len = sizeof(struct diag_dci_header_t);
510 int token = BRIDGE_TO_TOKEN(index);
515 diag_dci_record_traffic(recd_bytes, 0, 0, token);
517 if (!partial_pkt.processing)
520 if (partial_pkt.remaining > recd_bytes) {
521 if ((partial_pkt.read_len + recd_bytes) >
522 (MAX_DCI_PACKET_SZ)) {
523 pr_err("diag: Invalid length %d, %d received in %s\n",
524 partial_pkt.read_len, recd_bytes, __func__);
527 memcpy(partial_pkt.data + partial_pkt.read_len, buf,
529 read_bytes += recd_bytes;
531 partial_pkt.read_len += recd_bytes;
532 partial_pkt.remaining -= recd_bytes;
534 if ((partial_pkt.read_len + partial_pkt.remaining) >
535 (MAX_DCI_PACKET_SZ)) {
536 pr_err("diag: Invalid length during partial read %d, %d received in %s\n",
537 partial_pkt.read_len,
538 partial_pkt.remaining, __func__);
541 memcpy(partial_pkt.data + partial_pkt.read_len, buf,
542 partial_pkt.remaining);
543 read_bytes += partial_pkt.remaining;
545 partial_pkt.read_len += partial_pkt.remaining;
546 partial_pkt.remaining = 0;
549 if (partial_pkt.remaining == 0) {
551 * Retrieve from the DCI control packet after the header = start
552 * (1 byte) + version (1 byte) + length (2 bytes)
554 diag_process_single_dci_pkt(partial_pkt.data + 4,
555 partial_pkt.read_len - header_len,
556 DCI_REMOTE_DATA, token);
557 partial_pkt.read_len = 0;
558 partial_pkt.total_len = 0;
559 partial_pkt.processing = 0;
565 while (read_bytes < recd_bytes) {
566 header = (struct diag_dci_header_t *)buf;
567 dci_pkt_len = header->length;
569 if (header->cmd_code != DCI_CONTROL_PKT_CODE &&
570 driver->num_dci_client == 0) {
571 read_bytes += header_len + dci_pkt_len;
572 buf += header_len + dci_pkt_len;
576 if (dci_pkt_len + header_len > MAX_DCI_PACKET_SZ) {
577 pr_err("diag: Invalid length in the dci packet field %d\n",
582 if ((dci_pkt_len + header_len) > (recd_bytes - read_bytes)) {
583 partial_pkt.read_len = recd_bytes - read_bytes;
584 partial_pkt.total_len = dci_pkt_len + header_len;
585 partial_pkt.remaining = partial_pkt.total_len -
586 partial_pkt.read_len;
587 partial_pkt.processing = 1;
588 memcpy(partial_pkt.data, buf, partial_pkt.read_len);
592 * Retrieve from the DCI control packet after the header = start
593 * (1 byte) + version (1 byte) + length (2 bytes)
595 err = diag_process_single_dci_pkt(buf + 4, dci_pkt_len,
596 DCI_REMOTE_DATA, DCI_MDM_PROC);
599 read_bytes += header_len + dci_pkt_len;
600 buf += header_len + dci_pkt_len; /* advance to next DCI pkt */
605 /* wake up all sleeping DCI clients which have some data */
606 diag_dci_wakeup_clients();
607 dci_check_drain_timer();
611 /* Process the data read from the peripheral dci channels */
612 void diag_dci_process_peripheral_data(struct diagfwd_info *p_info, void *buf,
615 int read_bytes = 0, err = 0;
616 uint16_t dci_pkt_len;
617 struct diag_dci_pkt_header_t *header = NULL;
618 uint8_t recv_pkt_cmd_code;
624 * Release wakeup source when there are no more clients to
627 if (driver->num_dci_client == 0) {
628 diag_ws_reset(DIAG_WS_DCI);
632 diag_dci_record_traffic(recd_bytes, p_info->type, p_info->peripheral,
634 while (read_bytes < recd_bytes) {
635 header = (struct diag_dci_pkt_header_t *)buf;
636 recv_pkt_cmd_code = header->pkt_code;
637 dci_pkt_len = header->len;
640 * Check if the length of the current packet is lesser than the
641 * remaining bytes in the received buffer. This includes space
642 * for the Start byte (1), Version byte (1), length bytes (2)
645 if ((dci_pkt_len + 5) > (recd_bytes - read_bytes)) {
646 pr_err("diag: Invalid length in %s, len: %d, dci_pkt_len: %d",
647 __func__, recd_bytes, dci_pkt_len);
652 * Retrieve from the DCI control packet after the header = start
653 * (1 byte) + version (1 byte) + length (2 bytes)
655 err = diag_process_single_dci_pkt(buf + 4, dci_pkt_len,
656 (int)p_info->peripheral,
662 read_bytes += 5 + dci_pkt_len;
663 buf += 5 + dci_pkt_len; /* advance to next DCI pkt */
668 /* wake up all sleeping DCI clients which have some data */
669 diag_dci_wakeup_clients();
670 dci_check_drain_timer();
674 int diag_dci_query_log_mask(struct diag_dci_client_tbl *entry,
678 uint8_t equip_id, *log_mask_ptr, byte_mask;
679 int byte_index, offset;
682 pr_err("diag: In %s, invalid client entry\n", __func__);
686 equip_id = LOG_GET_EQUIP_ID(log_code);
687 item_num = LOG_GET_ITEM_NUM(log_code);
688 byte_index = item_num/8 + 2;
689 byte_mask = 0x01 << (item_num % 8);
690 offset = equip_id * 514;
692 if (offset + byte_index >= DCI_LOG_MASK_SIZE) {
693 pr_err("diag: In %s, invalid offset: %d, log_code: %d, byte_index: %d\n",
694 __func__, offset, log_code, byte_index);
698 log_mask_ptr = entry->dci_log_mask;
699 log_mask_ptr = log_mask_ptr + offset + byte_index;
700 return ((*log_mask_ptr & byte_mask) == byte_mask) ? 1 : 0;
704 int diag_dci_query_event_mask(struct diag_dci_client_tbl *entry,
707 uint8_t *event_mask_ptr, byte_mask;
708 int byte_index, bit_index;
711 pr_err("diag: In %s, invalid client entry\n", __func__);
715 byte_index = event_id/8;
716 bit_index = event_id % 8;
717 byte_mask = 0x1 << bit_index;
719 if (byte_index >= DCI_EVENT_MASK_SIZE) {
720 pr_err("diag: In %s, invalid, event_id: %d, byte_index: %d\n",
721 __func__, event_id, byte_index);
725 event_mask_ptr = entry->dci_event_mask;
726 event_mask_ptr = event_mask_ptr + byte_index;
727 return ((*event_mask_ptr & byte_mask) == byte_mask) ? 1 : 0;
730 static int diag_dci_filter_commands(struct diag_pkt_header_t *header)
735 switch (header->cmd_code) {
736 case 0x7d: /* Msg Mask Configuration */
737 case 0x73: /* Log Mask Configuration */
738 case 0x81: /* Event Mask Configuration */
739 case 0x82: /* Event Mask Change */
740 case 0x60: /* Event Mask Toggle */
744 if (header->cmd_code == 0x4b && header->subsys_id == 0x12) {
745 switch (header->subsys_cmd_code) {
746 case 0x60: /* Extended Event Mask Config */
747 case 0x61: /* Extended Msg Mask Config */
748 case 0x62: /* Extended Log Mask Config */
749 case 0x20C: /* Set current Preset ID */
750 case 0x20D: /* Get current Preset ID */
751 case 0x218: /* HDLC Disabled Command */
759 static struct dci_pkt_req_entry_t *diag_register_dci_transaction(int uid,
762 struct dci_pkt_req_entry_t *entry = NULL;
763 entry = kzalloc(sizeof(struct dci_pkt_req_entry_t), GFP_KERNEL);
768 entry->client_id = client_id;
770 entry->tag = driver->dci_tag;
771 pr_debug("diag: Registering DCI cmd req, client_id: %d, uid: %d, tag:%d\n",
772 entry->client_id, entry->uid, entry->tag);
773 list_add_tail(&entry->track, &driver->dci_req_list);
778 static struct dci_pkt_req_entry_t *diag_dci_get_request_entry(int tag)
780 struct list_head *start, *temp;
781 struct dci_pkt_req_entry_t *entry = NULL;
782 list_for_each_safe(start, temp, &driver->dci_req_list) {
783 entry = list_entry(start, struct dci_pkt_req_entry_t, track);
784 if (entry->tag == tag)
790 static int diag_dci_remove_req_entry(unsigned char *buf, int len,
791 struct dci_pkt_req_entry_t *entry)
793 uint16_t rsp_count = 0, delayed_rsp_id = 0;
794 if (!buf || len <= 0 || !entry) {
795 pr_err("diag: In %s, invalid input buf: %pK, len: %d, entry: %pK\n",
796 __func__, buf, len, entry);
800 /* It is an immediate response, delete it from the table */
802 list_del(&entry->track);
808 /* It is a delayed response. Check if the length is valid */
809 if (len < MIN_DELAYED_RSP_LEN) {
810 pr_err("diag: Invalid delayed rsp packet length %d\n", len);
815 * If the delayed response id field (uint16_t at byte 8) is 0 then
816 * there is only one response and we can remove the request entry.
818 delayed_rsp_id = *(uint16_t *)(buf + 8);
819 if (delayed_rsp_id == 0) {
820 list_del(&entry->track);
827 * Check the response count field (uint16 at byte 10). The request
828 * entry can be deleted it it is the last response in the sequence.
829 * It is the last response in the sequence if the response count
830 * is 1 or if the signed bit gets dropped.
832 rsp_count = *(uint16_t *)(buf + 10);
833 if (rsp_count > 0 && rsp_count < 0x1000) {
834 list_del(&entry->track);
843 static void dci_process_ctrl_status(unsigned char *buf, int len, int token)
845 struct diag_ctrl_dci_status *header = NULL;
846 unsigned char *temp = buf;
847 uint32_t read_len = 0;
849 int peripheral_mask, status;
851 if (!buf || (len < sizeof(struct diag_ctrl_dci_status))) {
852 pr_err("diag: In %s, invalid buf %pK or length: %d\n",
857 if (!VALID_DCI_TOKEN(token)) {
858 pr_err("diag: In %s, invalid DCI token %d\n", __func__, token);
862 header = (struct diag_ctrl_dci_status *)temp;
863 temp += sizeof(struct diag_ctrl_dci_status);
864 read_len += sizeof(struct diag_ctrl_dci_status);
866 for (i = 0; i < header->count; i++) {
867 if (read_len > (len - 2)) {
868 pr_err("diag: In %s, Invalid length len: %d\n",
873 switch (*(uint8_t *)temp) {
874 case PERIPHERAL_MODEM:
875 peripheral_mask = DIAG_CON_MPSS;
877 case PERIPHERAL_LPASS:
878 peripheral_mask = DIAG_CON_LPASS;
880 case PERIPHERAL_WCNSS:
881 peripheral_mask = DIAG_CON_WCNSS;
883 case PERIPHERAL_SENSORS:
884 peripheral_mask = DIAG_CON_SENSORS;
887 pr_err("diag: In %s, unknown peripheral, peripheral: %d\n",
888 __func__, *(uint8_t *)temp);
891 temp += sizeof(uint8_t);
892 read_len += sizeof(uint8_t);
894 status = (*(uint8_t *)temp) ? DIAG_STATUS_OPEN :
896 temp += sizeof(uint8_t);
897 read_len += sizeof(uint8_t);
898 diag_dci_notify_client(peripheral_mask, status, token);
902 static void dci_process_ctrl_handshake_pkt(unsigned char *buf, int len,
905 struct diag_ctrl_dci_handshake_pkt *header = NULL;
906 unsigned char *temp = buf;
909 if (!buf || (len < sizeof(struct diag_ctrl_dci_handshake_pkt)))
912 if (!VALID_DCI_TOKEN(token))
915 header = (struct diag_ctrl_dci_handshake_pkt *)temp;
916 if (header->magic == DCI_MAGIC) {
917 dci_channel_status[token].open = 1;
918 err = dci_ops_tbl[token].send_log_mask(token);
920 pr_err("diag: In %s, unable to send log mask to token: %d, err: %d\n",
921 __func__, token, err);
923 err = dci_ops_tbl[token].send_event_mask(token);
925 pr_err("diag: In %s, unable to send event mask to token: %d, err: %d\n",
926 __func__, token, err);
931 void extract_dci_ctrl_pkt(unsigned char *buf, int len, int token)
933 unsigned char *temp = buf;
934 uint32_t ctrl_pkt_id;
936 diag_ws_on_read(DIAG_WS_DCI, len);
938 pr_err("diag: Invalid buffer in %s\n", __func__);
942 if (len < (sizeof(uint8_t) + sizeof(uint32_t))) {
943 pr_err("diag: In %s, invalid length %d\n", __func__, len);
947 /* Skip the Control packet command code */
948 temp += sizeof(uint8_t);
949 len -= sizeof(uint8_t);
950 ctrl_pkt_id = *(uint32_t *)temp;
951 switch (ctrl_pkt_id) {
952 case DIAG_CTRL_MSG_DCI_CONNECTION_STATUS:
953 dci_process_ctrl_status(temp, len, token);
955 case DIAG_CTRL_MSG_DCI_HANDSHAKE_PKT:
956 dci_process_ctrl_handshake_pkt(temp, len, token);
959 pr_debug("diag: In %s, unknown control pkt %d\n",
960 __func__, ctrl_pkt_id);
966 * DCI control packets are not consumed by the clients. Mimic client
967 * consumption by setting and clearing the wakeup source copy_count
970 diag_ws_on_copy_fail(DIAG_WS_DCI);
973 void extract_dci_pkt_rsp(unsigned char *buf, int len, int data_source,
977 struct diag_dci_client_tbl *entry = NULL;
978 void *temp_buf = NULL;
979 uint8_t dci_cmd_code, cmd_code_len, delete_flag = 0;
980 uint32_t rsp_len = 0;
981 struct diag_dci_buffer_t *rsp_buf = NULL;
982 struct dci_pkt_req_entry_t *req_entry = NULL;
983 unsigned char *temp = buf;
984 int save_req_uid = 0;
985 struct diag_dci_pkt_rsp_header_t pkt_rsp_header;
988 pr_err("diag: Invalid pointer in %s\n", __func__);
991 dci_cmd_code = *(uint8_t *)(temp);
992 if (dci_cmd_code == DCI_PKT_RSP_CODE) {
993 cmd_code_len = sizeof(uint8_t);
994 } else if (dci_cmd_code == DCI_DELAYED_RSP_CODE) {
995 cmd_code_len = sizeof(uint32_t);
997 pr_err("diag: In %s, invalid command code %d\n", __func__,
1001 temp += cmd_code_len;
1003 temp += sizeof(int);
1006 * The size of the response is (total length) - (length of the command
1007 * code, the tag (int)
1009 rsp_len = len - (cmd_code_len + sizeof(int));
1010 if ((rsp_len == 0) || (rsp_len > (len - 5))) {
1011 pr_err("diag: Invalid length in %s, len: %d, rsp_len: %d",
1012 __func__, len, rsp_len);
1016 mutex_lock(&driver->dci_mutex);
1017 req_entry = diag_dci_get_request_entry(tag);
1019 pr_err_ratelimited("diag: No matching client for DCI data\n");
1020 mutex_unlock(&driver->dci_mutex);
1024 entry = diag_dci_get_client_entry(req_entry->client_id);
1026 pr_err("diag: In %s, couldn't find client entry, id:%d\n",
1027 __func__, req_entry->client_id);
1028 mutex_unlock(&driver->dci_mutex);
1032 save_req_uid = req_entry->uid;
1033 /* Remove the headers and send only the response to this function */
1034 delete_flag = diag_dci_remove_req_entry(temp, rsp_len, req_entry);
1035 if (delete_flag < 0) {
1036 mutex_unlock(&driver->dci_mutex);
1040 mutex_lock(&entry->buffers[data_source].buf_mutex);
1041 rsp_buf = entry->buffers[data_source].buf_cmd;
1043 mutex_lock(&rsp_buf->data_mutex);
1045 * Check if we can fit the data in the rsp buffer. The total length of
1046 * the rsp is the rsp length (write_len) + DCI_PKT_RSP_TYPE header (int)
1047 * + field for length (int) + delete_flag (uint8_t)
1049 if ((rsp_buf->data_len + 9 + rsp_len) > rsp_buf->capacity) {
1050 pr_alert("diag: create capacity for pkt rsp\n");
1051 rsp_buf->capacity += 9 + rsp_len;
1052 temp_buf = krealloc(rsp_buf->data, rsp_buf->capacity,
1055 pr_err("diag: DCI realloc failed\n");
1056 mutex_unlock(&rsp_buf->data_mutex);
1057 mutex_unlock(&entry->buffers[data_source].buf_mutex);
1058 mutex_unlock(&driver->dci_mutex);
1061 rsp_buf->data = temp_buf;
1065 /* Fill in packet response header information */
1066 pkt_rsp_header.type = DCI_PKT_RSP_TYPE;
1067 /* Packet Length = Response Length + Length of uid field (int) */
1068 pkt_rsp_header.length = rsp_len + sizeof(int);
1069 pkt_rsp_header.delete_flag = delete_flag;
1070 pkt_rsp_header.uid = save_req_uid;
1071 memcpy(rsp_buf->data + rsp_buf->data_len, &pkt_rsp_header,
1072 sizeof(struct diag_dci_pkt_rsp_header_t));
1073 rsp_buf->data_len += sizeof(struct diag_dci_pkt_rsp_header_t);
1074 memcpy(rsp_buf->data + rsp_buf->data_len, temp, rsp_len);
1075 rsp_buf->data_len += rsp_len;
1076 rsp_buf->data_source = data_source;
1078 mutex_unlock(&rsp_buf->data_mutex);
1081 * Add directly to the list for writing responses to the
1082 * userspace as these shouldn't be buffered and shouldn't wait
1083 * for log and event buffers to be full
1085 dci_add_buffer_to_list(entry, rsp_buf);
1086 mutex_unlock(&entry->buffers[data_source].buf_mutex);
1087 mutex_unlock(&driver->dci_mutex);
1090 static void copy_ext_hdr(struct diag_dci_buffer_t *data_buffer, void *ext_hdr)
1093 pr_err("diag: In %s, data buffer is NULL", __func__);
1097 *(int *)(data_buffer->data + data_buffer->data_len) =
1099 data_buffer->data_len += sizeof(int);
1100 memcpy(data_buffer->data + data_buffer->data_len, ext_hdr,
1102 data_buffer->data_len += EXT_HDR_LEN;
1105 static void copy_dci_event(unsigned char *buf, int len,
1106 struct diag_dci_client_tbl *client, int data_source,
1109 struct diag_dci_buffer_t *data_buffer = NULL;
1110 struct diag_dci_buf_peripheral_t *proc_buf = NULL;
1111 int err = 0, total_len = 0;
1113 if (!buf || !client) {
1114 pr_err("diag: Invalid pointers in %s", __func__);
1118 total_len = sizeof(int) + len;
1120 total_len += sizeof(int) + EXT_HDR_LEN;
1122 proc_buf = &client->buffers[data_source];
1123 mutex_lock(&proc_buf->buf_mutex);
1124 mutex_lock(&proc_buf->health_mutex);
1125 err = diag_dci_get_buffer(client, data_source, total_len);
1128 proc_buf->health.dropped_events++;
1130 pr_err("diag: In %s, invalid packet\n", __func__);
1131 mutex_unlock(&proc_buf->health_mutex);
1132 mutex_unlock(&proc_buf->buf_mutex);
1136 data_buffer = proc_buf->buf_curr;
1138 proc_buf->health.received_events++;
1139 mutex_unlock(&proc_buf->health_mutex);
1140 mutex_unlock(&proc_buf->buf_mutex);
1142 mutex_lock(&data_buffer->data_mutex);
1144 copy_ext_hdr(data_buffer, ext_hdr);
1146 *(int *)(data_buffer->data + data_buffer->data_len) = DCI_EVENT_TYPE;
1147 data_buffer->data_len += sizeof(int);
1148 memcpy(data_buffer->data + data_buffer->data_len, buf, len);
1149 data_buffer->data_len += len;
1150 data_buffer->data_source = data_source;
1151 mutex_unlock(&data_buffer->data_mutex);
1155 void extract_dci_events(unsigned char *buf, int len, int data_source,
1156 int token, void *ext_hdr)
1158 uint16_t event_id, event_id_packet, length, temp_len;
1159 uint8_t payload_len, payload_len_field;
1160 uint8_t timestamp[8] = {0}, timestamp_len;
1161 unsigned char event_data[MAX_EVENT_SIZE];
1162 unsigned int total_event_len;
1163 struct list_head *start, *temp;
1164 struct diag_dci_client_tbl *entry = NULL;
1167 pr_err("diag: In %s buffer is NULL\n", __func__);
1171 * 1 byte for event code and 2 bytes for the length field.
1172 * The length field indicates the total length removing the cmd_code
1173 * and the lenght field. The event parsing in that case should happen
1177 pr_err("diag: In %s invalid len: %d\n", __func__, len);
1180 length = *(uint16_t *)(buf + 1); /* total length of event series */
1181 if ((length == 0) || (len != (length + 3))) {
1182 pr_err("diag: Incoming dci event length: %d is invalid\n",
1187 * Move directly to the start of the event series.
1188 * The event parsing should happen from start of event
1189 * series till the end.
1192 while (temp_len < length) {
1193 event_id_packet = *(uint16_t *)(buf + temp_len);
1194 event_id = event_id_packet & 0x0FFF; /* extract 12 bits */
1195 if (event_id_packet & 0x8000) {
1196 /* The packet has the two smallest byte of the
1201 /* The packet has the full timestamp. The first event
1202 * will always have full timestamp. Save it in the
1203 * timestamp buffer and use it for subsequent events if
1207 if ((temp_len + timestamp_len + 2) <= len)
1208 memcpy(timestamp, buf + temp_len + 2,
1211 pr_err("diag: Invalid length in %s, len: %d, temp_len: %d",
1212 __func__, len, temp_len);
1216 /* 13th and 14th bit represent the payload length */
1217 if (((event_id_packet & 0x6000) >> 13) == 3) {
1218 payload_len_field = 1;
1219 if ((temp_len + timestamp_len + 3) <= len) {
1220 payload_len = *(uint8_t *)
1221 (buf + temp_len + 2 + timestamp_len);
1223 pr_err("diag: Invalid length in %s, len: %d, temp_len: %d",
1224 __func__, len, temp_len);
1227 if ((payload_len < (MAX_EVENT_SIZE - 13)) &&
1228 ((temp_len + timestamp_len + payload_len + 3) <= len)) {
1230 * Copy the payload length and the payload
1231 * after skipping temp_len bytes for already
1232 * parsed packet, timestamp_len for timestamp
1233 * buffer, 2 bytes for event_id_packet.
1235 memcpy(event_data + 12, buf + temp_len + 2 +
1237 memcpy(event_data + 13, buf + temp_len + 2 +
1238 timestamp_len + 1, payload_len);
1240 pr_err("diag: event > %d, payload_len = %d, temp_len = %d\n",
1241 (MAX_EVENT_SIZE - 13), payload_len, temp_len);
1245 payload_len_field = 0;
1246 payload_len = (event_id_packet & 0x6000) >> 13;
1248 * Copy the payload after skipping temp_len bytes
1249 * for already parsed packet, timestamp_len for
1250 * timestamp buffer, 2 bytes for event_id_packet.
1252 if ((payload_len < (MAX_EVENT_SIZE - 12)) &&
1253 ((temp_len + timestamp_len + payload_len + 2) <= len))
1254 memcpy(event_data + 12, buf + temp_len + 2 +
1255 timestamp_len, payload_len);
1257 pr_err("diag: event > %d, payload_len = %d, temp_len = %d\n",
1258 (MAX_EVENT_SIZE - 12), payload_len, temp_len);
1263 /* Before copying the data to userspace, check if we are still
1264 * within the buffer limit. This is an error case, don't count
1265 * it towards the health statistics.
1267 * Here, the offset of 2 bytes(uint16_t) is for the
1268 * event_id_packet length
1270 temp_len += sizeof(uint16_t) + timestamp_len +
1271 payload_len_field + payload_len;
1272 if (temp_len > len) {
1273 pr_err("diag: Invalid length in %s, len: %d, read: %d",
1274 __func__, len, temp_len);
1278 /* 2 bytes for the event id & timestamp len is hard coded to 8,
1279 as individual events have full timestamp */
1280 *(uint16_t *)(event_data) = 10 +
1281 payload_len_field + payload_len;
1282 *(uint16_t *)(event_data + 2) = event_id_packet & 0x7FFF;
1283 memcpy(event_data + 4, timestamp, 8);
1284 /* 2 bytes for the event length field which is added to
1286 total_event_len = 2 + 10 + payload_len_field + payload_len;
1287 /* parse through event mask tbl of each client and check mask */
1288 mutex_lock(&driver->dci_mutex);
1289 list_for_each_safe(start, temp, &driver->dci_client_list) {
1290 entry = list_entry(start, struct diag_dci_client_tbl,
1292 if (entry->client_info.token != token)
1294 if (diag_dci_query_event_mask(entry, event_id)) {
1295 /* copy to client buffer */
1296 copy_dci_event(event_data, total_event_len,
1297 entry, data_source, ext_hdr);
1300 mutex_unlock(&driver->dci_mutex);
1304 static void copy_dci_log(unsigned char *buf, int len,
1305 struct diag_dci_client_tbl *client, int data_source,
1308 uint16_t log_length = 0;
1309 struct diag_dci_buffer_t *data_buffer = NULL;
1310 struct diag_dci_buf_peripheral_t *proc_buf = NULL;
1311 int err = 0, total_len = 0;
1313 if (!buf || !client) {
1314 pr_err("diag: Invalid pointers in %s", __func__);
1318 log_length = *(uint16_t *)(buf + 2);
1319 if (log_length > USHRT_MAX - 4) {
1320 pr_err("diag: Integer overflow in %s, log_len: %d",
1321 __func__, log_length);
1324 total_len = sizeof(int) + log_length;
1326 total_len += sizeof(int) + EXT_HDR_LEN;
1328 /* Check if we are within the len. The check should include the
1329 * first 4 bytes for the Log code(2) and the length bytes (2)
1331 if ((log_length + sizeof(uint16_t) + 2) > len) {
1332 pr_err("diag: Invalid length in %s, log_len: %d, len: %d",
1333 __func__, log_length, len);
1337 proc_buf = &client->buffers[data_source];
1338 mutex_lock(&proc_buf->buf_mutex);
1339 mutex_lock(&proc_buf->health_mutex);
1340 err = diag_dci_get_buffer(client, data_source, total_len);
1343 proc_buf->health.dropped_logs++;
1345 pr_err("diag: In %s, invalid packet\n", __func__);
1346 mutex_unlock(&proc_buf->health_mutex);
1347 mutex_unlock(&proc_buf->buf_mutex);
1351 data_buffer = proc_buf->buf_curr;
1352 proc_buf->health.received_logs++;
1353 mutex_unlock(&proc_buf->health_mutex);
1354 mutex_unlock(&proc_buf->buf_mutex);
1356 mutex_lock(&data_buffer->data_mutex);
1357 if (!data_buffer->data) {
1358 mutex_unlock(&data_buffer->data_mutex);
1362 copy_ext_hdr(data_buffer, ext_hdr);
1364 *(int *)(data_buffer->data + data_buffer->data_len) = DCI_LOG_TYPE;
1365 data_buffer->data_len += sizeof(int);
1366 memcpy(data_buffer->data + data_buffer->data_len, buf + sizeof(int),
1368 data_buffer->data_len += log_length;
1369 data_buffer->data_source = data_source;
1370 mutex_unlock(&data_buffer->data_mutex);
1373 void extract_dci_log(unsigned char *buf, int len, int data_source, int token,
1376 uint16_t log_code, read_bytes = 0;
1377 struct list_head *start, *temp;
1378 struct diag_dci_client_tbl *entry = NULL;
1381 pr_err("diag: In %s buffer is NULL\n", __func__);
1385 * The first eight bytes for the incoming log packet contains
1386 * Command code (2), the length of the packet (2), the length
1387 * of the log (2) and log code (2)
1390 pr_err("diag: In %s invalid len: %d\n", __func__, len);
1394 log_code = *(uint16_t *)(buf + 6);
1395 read_bytes += sizeof(uint16_t) + 6;
1397 /* parse through log mask table of each client and check mask */
1398 mutex_lock(&driver->dci_mutex);
1399 list_for_each_safe(start, temp, &driver->dci_client_list) {
1400 entry = list_entry(start, struct diag_dci_client_tbl, track);
1401 if (entry->client_info.token != token)
1403 if (diag_dci_query_log_mask(entry, log_code)) {
1404 pr_debug("\t log code %x needed by client %d",
1405 log_code, entry->client->tgid);
1406 /* copy to client buffer */
1407 copy_dci_log(buf, len, entry, data_source, ext_hdr);
1410 mutex_unlock(&driver->dci_mutex);
1413 void extract_dci_ext_pkt(unsigned char *buf, int len, int data_source,
1416 uint8_t version, pkt_cmd_code = 0;
1417 unsigned char *pkt = NULL;
1420 pr_err("diag: In %s buffer is NULL\n", __func__);
1423 if (len < (EXT_HDR_LEN + sizeof(uint8_t))) {
1424 pr_err("diag: In %s invalid len: %d\n", __func__, len);
1428 version = *(uint8_t *)buf + 1;
1429 if (version < EXT_HDR_VERSION) {
1430 pr_err("diag: %s, Extended header with invalid version: %d\n",
1435 pkt = buf + EXT_HDR_LEN;
1436 pkt_cmd_code = *(uint8_t *)pkt;
1439 switch (pkt_cmd_code) {
1441 extract_dci_log(pkt, len, data_source, token, buf);
1443 case EVENT_CMD_CODE:
1444 extract_dci_events(pkt, len, data_source, token, buf);
1447 pr_err("diag: %s unsupported cmd_code: %d, data_source: %d\n",
1448 __func__, pkt_cmd_code, data_source);
1453 void diag_dci_channel_open_work(struct work_struct *work)
1456 char dirty_bits[16];
1457 uint8_t *client_log_mask_ptr;
1458 uint8_t *log_mask_ptr;
1460 struct list_head *start, *temp;
1461 struct diag_dci_client_tbl *entry = NULL;
1463 /* Update apps and peripheral(s) with the dci log and event masks */
1464 memset(dirty_bits, 0, 16 * sizeof(uint8_t));
1467 * From each log entry used by each client, determine
1468 * which log entries in the cumulative logs that need
1469 * to be updated on the peripheral.
1471 mutex_lock(&driver->dci_mutex);
1472 list_for_each_safe(start, temp, &driver->dci_client_list) {
1473 entry = list_entry(start, struct diag_dci_client_tbl, track);
1474 if (entry->client_info.token != DCI_LOCAL_PROC)
1476 client_log_mask_ptr = entry->dci_log_mask;
1477 for (j = 0; j < 16; j++) {
1478 if (*(client_log_mask_ptr+1))
1480 client_log_mask_ptr += 514;
1483 mutex_unlock(&driver->dci_mutex);
1485 mutex_lock(&dci_log_mask_mutex);
1486 /* Update the appropriate dirty bits in the cumulative mask */
1487 log_mask_ptr = dci_ops_tbl[DCI_LOCAL_PROC].log_mask_composite;
1488 for (i = 0; i < 16; i++) {
1490 *(log_mask_ptr+1) = dirty_bits[i];
1492 log_mask_ptr += 514;
1494 mutex_unlock(&dci_log_mask_mutex);
1496 /* Send updated mask to userspace clients */
1497 diag_update_userspace_clients(DCI_LOG_MASKS_TYPE);
1498 /* Send updated log mask to peripherals */
1499 ret = dci_ops_tbl[DCI_LOCAL_PROC].send_log_mask(DCI_LOCAL_PROC);
1501 /* Send updated event mask to userspace clients */
1502 diag_update_userspace_clients(DCI_EVENT_MASKS_TYPE);
1503 /* Send updated event mask to peripheral */
1504 ret = dci_ops_tbl[DCI_LOCAL_PROC].send_event_mask(DCI_LOCAL_PROC);
1507 void diag_dci_notify_client(int peripheral_mask, int data, int proc)
1510 struct siginfo info;
1511 struct list_head *start, *temp;
1512 struct diag_dci_client_tbl *entry = NULL;
1513 struct pid *pid_struct = NULL;
1514 struct task_struct *dci_task = NULL;
1516 memset(&info, 0, sizeof(struct siginfo));
1517 info.si_code = SI_QUEUE;
1518 info.si_int = (peripheral_mask | data);
1519 if (data == DIAG_STATUS_OPEN)
1520 dci_ops_tbl[proc].peripheral_status |= peripheral_mask;
1522 dci_ops_tbl[proc].peripheral_status &= ~peripheral_mask;
1524 /* Notify the DCI process that the peripheral DCI Channel is up */
1525 mutex_lock(&driver->dci_mutex);
1526 list_for_each_safe(start, temp, &driver->dci_client_list) {
1527 entry = list_entry(start, struct diag_dci_client_tbl, track);
1528 if (entry->client_info.token != proc)
1530 if (entry->client_info.notification_list & peripheral_mask) {
1531 info.si_signo = entry->client_info.signal_type;
1532 pid_struct = find_get_pid(entry->tgid);
1534 dci_task = get_pid_task(pid_struct,
1537 DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
1538 "diag: dci client with pid = %d Exited..\n",
1540 put_pid(pid_struct);
1541 mutex_unlock(&driver->dci_mutex);
1544 if (entry->client &&
1545 entry->tgid == dci_task->tgid) {
1546 DIAG_LOG(DIAG_DEBUG_DCI,
1547 "entry tgid = %d, dci client tgid = %d\n",
1548 entry->tgid, dci_task->tgid);
1549 stat = send_sig_info(
1550 entry->client_info.signal_type,
1553 pr_err("diag: Err sending dci signal to client, signal data: 0x%x, stat: %d\n",
1556 pr_err("diag: client data is corrupted, signal data: 0x%x, stat: %d\n",
1559 put_task_struct(dci_task);
1560 put_pid(pid_struct);
1564 mutex_unlock(&driver->dci_mutex);
1567 static int diag_send_dci_pkt(struct diag_cmd_reg_t *entry,
1568 unsigned char *buf, int len, int tag)
1570 int i, status = DIAG_DCI_NO_ERROR;
1571 uint32_t write_len = 0;
1572 struct diag_dci_pkt_header_t header;
1577 if (len < 1 || len > DIAG_MAX_REQ_SIZE) {
1578 pr_err("diag: dci: In %s, invalid length %d, max_length: %d\n",
1579 __func__, len, (int)(DCI_REQ_BUF_SIZE - sizeof(header)));
1583 if ((len + sizeof(header) + sizeof(uint8_t)) > DCI_BUF_SIZE) {
1584 pr_err("diag: dci: In %s, invalid length %d for apps_dci_buf, max_length: %d\n",
1585 __func__, len, DIAG_MAX_REQ_SIZE);
1589 mutex_lock(&driver->dci_mutex);
1590 /* prepare DCI packet */
1591 header.start = CONTROL_CHAR;
1593 header.len = len + sizeof(int) + sizeof(uint8_t);
1594 header.pkt_code = DCI_PKT_RSP_CODE;
1596 memcpy(driver->apps_dci_buf, &header, sizeof(header));
1597 write_len += sizeof(header);
1598 memcpy(driver->apps_dci_buf + write_len , buf, len);
1600 *(uint8_t *)(driver->apps_dci_buf + write_len) = CONTROL_CHAR;
1601 write_len += sizeof(uint8_t);
1603 /* This command is registered locally on the Apps */
1604 if (entry->proc == APPS_DATA) {
1605 diag_update_pkt_buffer(driver->apps_dci_buf, write_len,
1607 diag_update_sleeping_process(entry->pid, DCI_PKT_TYPE);
1608 mutex_unlock(&driver->dci_mutex);
1609 return DIAG_DCI_NO_ERROR;
1612 for (i = 0; i < NUM_PERIPHERALS; i++)
1613 if (entry->proc == i) {
1619 status = diag_dci_write_proc(entry->proc,
1621 driver->apps_dci_buf,
1624 pr_err("diag: Cannot send packet to peripheral %d",
1626 status = DIAG_DCI_SEND_DATA_FAIL;
1628 mutex_unlock(&driver->dci_mutex);
1632 #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
1633 unsigned char *dci_get_buffer_from_bridge(int token)
1635 uint8_t retries = 0, max_retries = 3;
1636 unsigned char *buf = NULL;
1639 buf = diagmem_alloc(driver, DIAG_MDM_BUF_SIZE,
1640 dci_ops_tbl[token].mempool);
1642 usleep_range(5000, 5100);
1646 } while (retries < max_retries);
1651 int diag_dci_write_bridge(int token, unsigned char *buf, int len)
1653 return diagfwd_bridge_write(TOKEN_TO_BRIDGE(token), buf, len);
1656 int diag_dci_write_done_bridge(int index, unsigned char *buf, int len)
1658 int token = BRIDGE_TO_TOKEN(index);
1659 if (!VALID_DCI_TOKEN(token)) {
1660 pr_err("diag: Invalid DCI token %d in %s\n", token, __func__);
1663 diagmem_free(driver, buf, dci_ops_tbl[token].mempool);
1668 #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
1669 static int diag_send_dci_pkt_remote(unsigned char *data, int len, int tag,
1672 unsigned char *buf = NULL;
1673 struct diag_dci_header_t dci_header;
1674 int dci_header_size = sizeof(struct diag_dci_header_t);
1675 int ret = DIAG_DCI_NO_ERROR;
1676 uint32_t write_len = 0;
1681 buf = dci_get_buffer_from_bridge(token);
1683 pr_err("diag: In %s, unable to get dci buffers to write data\n",
1688 dci_header.start = CONTROL_CHAR;
1689 dci_header.version = 1;
1691 * The Length of the DCI packet = length of the command + tag (int) +
1692 * the command code size (uint8_t)
1694 dci_header.length = len + sizeof(int) + sizeof(uint8_t);
1695 dci_header.cmd_code = DCI_PKT_RSP_CODE;
1697 memcpy(buf + write_len, &dci_header, dci_header_size);
1698 write_len += dci_header_size;
1699 *(int *)(buf + write_len) = tag;
1700 write_len += sizeof(int);
1701 memcpy(buf + write_len, data, len);
1703 *(buf + write_len) = CONTROL_CHAR; /* End Terminator */
1704 write_len += sizeof(uint8_t);
1706 ret = diag_dci_write_bridge(token, buf, write_len);
1708 pr_err("diag: error writing dci pkt to remote proc, token: %d, err: %d\n",
1710 diagmem_free(driver, buf, dci_ops_tbl[token].mempool);
1712 ret = DIAG_DCI_NO_ERROR;
1718 static int diag_send_dci_pkt_remote(unsigned char *data, int len, int tag,
1721 return DIAG_DCI_NO_ERROR;
1725 #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
1726 int diag_dci_send_handshake_pkt(int index)
1729 int token = BRIDGE_TO_TOKEN(index);
1731 struct diag_ctrl_dci_handshake_pkt ctrl_pkt;
1732 unsigned char *buf = NULL;
1733 struct diag_dci_header_t dci_header;
1735 if (!VALID_DCI_TOKEN(token)) {
1736 pr_err("diag: In %s, invalid DCI token %d\n", __func__, token);
1740 buf = dci_get_buffer_from_bridge(token);
1742 pr_err("diag: In %s, unable to get dci buffers to write data\n",
1747 dci_header.start = CONTROL_CHAR;
1748 dci_header.version = 1;
1749 /* Include the cmd code (uint8_t) in the length */
1750 dci_header.length = sizeof(ctrl_pkt) + sizeof(uint8_t);
1751 dci_header.cmd_code = DCI_CONTROL_PKT_CODE;
1752 memcpy(buf, &dci_header, sizeof(dci_header));
1753 write_len += sizeof(dci_header);
1755 ctrl_pkt.ctrl_pkt_id = DIAG_CTRL_MSG_DCI_HANDSHAKE_PKT;
1757 * The control packet data length accounts for the version (uint32_t)
1758 * of the packet and the magic number (uint32_t).
1760 ctrl_pkt.ctrl_pkt_data_len = 2 * sizeof(uint32_t);
1761 ctrl_pkt.version = 1;
1762 ctrl_pkt.magic = DCI_MAGIC;
1763 memcpy(buf + write_len, &ctrl_pkt, sizeof(ctrl_pkt));
1764 write_len += sizeof(ctrl_pkt);
1766 *(uint8_t *)(buf + write_len) = CONTROL_CHAR;
1767 write_len += sizeof(uint8_t);
1769 err = diag_dci_write_bridge(token, buf, write_len);
1771 pr_err("diag: error writing ack packet to remote proc, token: %d, err: %d\n",
1773 diagmem_free(driver, buf, dci_ops_tbl[token].mempool);
1777 mod_timer(&(dci_channel_status[token].wait_time),
1778 jiffies + msecs_to_jiffies(DCI_HANDSHAKE_WAIT_TIME));
1783 int diag_dci_send_handshake_pkt(int index)
1789 static int diag_dci_process_apps_pkt(struct diag_pkt_header_t *pkt_header,
1790 unsigned char *req_buf, int req_len,
1793 uint8_t cmd_code, subsys_id, i, goto_download = 0;
1794 uint8_t header_len = sizeof(struct diag_dci_pkt_header_t);
1795 uint16_t ss_cmd_code;
1796 uint32_t write_len = 0;
1797 unsigned char *dest_buf = driver->apps_dci_buf;
1798 unsigned char *payload_ptr = driver->apps_dci_buf + header_len;
1799 struct diag_dci_pkt_header_t dci_header;
1801 if (!pkt_header || !req_buf || req_len <= 0 || tag < 0)
1804 cmd_code = pkt_header->cmd_code;
1805 subsys_id = pkt_header->subsys_id;
1806 ss_cmd_code = pkt_header->subsys_cmd_code;
1808 if (cmd_code == DIAG_CMD_DOWNLOAD) {
1809 *payload_ptr = DIAG_CMD_DOWNLOAD;
1810 write_len = sizeof(uint8_t);
1813 } else if (cmd_code == DIAG_CMD_VERSION) {
1814 if (chk_polling_response()) {
1815 for (i = 0; i < 55; i++, write_len++, payload_ptr++)
1819 } else if (cmd_code == DIAG_CMD_EXT_BUILD) {
1820 if (chk_polling_response()) {
1821 *payload_ptr = DIAG_CMD_EXT_BUILD;
1822 write_len = sizeof(uint8_t);
1823 payload_ptr += sizeof(uint8_t);
1824 for (i = 0; i < 8; i++, write_len++, payload_ptr++)
1826 *(int *)(payload_ptr) = chk_config_get_id();
1827 write_len += sizeof(int);
1830 } else if (cmd_code == DIAG_CMD_LOG_ON_DMND) {
1831 write_len = diag_cmd_log_on_demand(req_buf, req_len,
1833 APPS_BUF_SIZE - header_len);
1835 } else if (cmd_code != DIAG_CMD_DIAG_SUBSYS) {
1836 return DIAG_DCI_TABLE_ERR;
1839 if (subsys_id == DIAG_SS_DIAG) {
1840 if (ss_cmd_code == DIAG_DIAG_MAX_PKT_SZ) {
1841 memcpy(payload_ptr, pkt_header,
1842 sizeof(struct diag_pkt_header_t));
1843 write_len = sizeof(struct diag_pkt_header_t);
1844 *(uint32_t *)(payload_ptr + write_len) =
1846 write_len += sizeof(uint32_t);
1847 } else if (ss_cmd_code == DIAG_DIAG_STM) {
1848 write_len = diag_process_stm_cmd(req_buf, payload_ptr);
1850 } else if (subsys_id == DIAG_SS_PARAMS) {
1851 if (ss_cmd_code == DIAG_DIAG_POLL) {
1852 if (chk_polling_response()) {
1853 memcpy(payload_ptr, pkt_header,
1854 sizeof(struct diag_pkt_header_t));
1855 write_len = sizeof(struct diag_pkt_header_t);
1856 payload_ptr += write_len;
1857 for (i = 0; i < 12; i++, write_len++) {
1862 } else if (ss_cmd_code == DIAG_DEL_RSP_WRAP) {
1863 memcpy(payload_ptr, pkt_header,
1864 sizeof(struct diag_pkt_header_t));
1865 write_len = sizeof(struct diag_pkt_header_t);
1866 *(int *)(payload_ptr + write_len) = wrap_enabled;
1867 write_len += sizeof(int);
1868 } else if (ss_cmd_code == DIAG_DEL_RSP_WRAP_CNT) {
1869 wrap_enabled = true;
1870 memcpy(payload_ptr, pkt_header,
1871 sizeof(struct diag_pkt_header_t));
1872 write_len = sizeof(struct diag_pkt_header_t);
1873 *(uint16_t *)(payload_ptr + write_len) = wrap_count;
1874 write_len += sizeof(uint16_t);
1875 } else if (ss_cmd_code == DIAG_EXT_MOBILE_ID) {
1876 write_len = diag_cmd_get_mobile_id(req_buf, req_len,
1878 APPS_BUF_SIZE - header_len);
1883 if (write_len > 0) {
1884 /* Check if we are within the range of the buffer*/
1885 if (write_len + header_len > DIAG_MAX_REQ_SIZE) {
1886 pr_err("diag: In %s, invalid length %d\n", __func__,
1887 write_len + header_len);
1890 dci_header.start = CONTROL_CHAR;
1891 dci_header.version = 1;
1893 * Length of the rsp pkt = actual data len + pkt rsp code
1894 * (uint8_t) + tag (int)
1896 dci_header.len = write_len + sizeof(uint8_t) + sizeof(int);
1897 dci_header.pkt_code = DCI_PKT_RSP_CODE;
1898 dci_header.tag = tag;
1899 driver->in_busy_dcipktdata = 1;
1900 memcpy(dest_buf, &dci_header, header_len);
1901 diag_process_apps_dci_read_data(DCI_PKT_TYPE, dest_buf + 4,
1903 driver->in_busy_dcipktdata = 0;
1905 if (goto_download) {
1907 * Sleep for sometime so that the response reaches the
1908 * client. The value 5000 empirically as an optimum
1909 * time for the response to reach the client.
1911 usleep_range(5000, 5100);
1912 /* call download API */
1913 msm_set_restart_mode(RESTART_DLOAD);
1914 pr_alert("diag: download mode set, Rebooting SoC..\n");
1915 kernel_restart(NULL);
1917 return DIAG_DCI_NO_ERROR;
1920 return DIAG_DCI_TABLE_ERR;
1923 static int diag_process_dci_pkt_rsp(unsigned char *buf, int len)
1925 int ret = DIAG_DCI_TABLE_ERR;
1927 struct diag_pkt_header_t *header = NULL;
1928 unsigned char *temp = buf;
1929 unsigned char *req_buf = NULL;
1930 uint8_t retry_count = 0, max_retries = 3;
1931 uint32_t read_len = 0, req_len = len;
1932 struct dci_pkt_req_entry_t *req_entry = NULL;
1933 struct diag_dci_client_tbl *dci_entry = NULL;
1934 struct dci_pkt_req_t req_hdr;
1935 struct diag_cmd_reg_t *reg_item;
1936 struct diag_cmd_reg_entry_t reg_entry;
1937 struct diag_cmd_reg_entry_t *temp_entry;
1942 if (len <= (sizeof(struct dci_pkt_req_t) +
1943 sizeof(struct diag_pkt_header_t)) ||
1944 len > DCI_REQ_BUF_SIZE) {
1945 pr_err("diag: dci: Invalid length %d len in %s", len, __func__);
1949 req_hdr = *(struct dci_pkt_req_t *)temp;
1950 temp += sizeof(struct dci_pkt_req_t);
1951 read_len += sizeof(struct dci_pkt_req_t);
1952 req_len -= sizeof(struct dci_pkt_req_t);
1953 req_buf = temp; /* Start of the Request */
1954 header = (struct diag_pkt_header_t *)temp;
1955 temp += sizeof(struct diag_pkt_header_t);
1956 read_len += sizeof(struct diag_pkt_header_t);
1957 if (read_len >= DCI_REQ_BUF_SIZE) {
1958 pr_err("diag: dci: In %s, invalid read_len: %d\n", __func__,
1963 mutex_lock(&driver->dci_mutex);
1964 dci_entry = diag_dci_get_client_entry(req_hdr.client_id);
1966 pr_err("diag: Invalid client %d in %s\n",
1967 req_hdr.client_id, __func__);
1968 mutex_unlock(&driver->dci_mutex);
1969 return DIAG_DCI_NO_REG;
1972 /* Check if the command is allowed on DCI */
1973 if (diag_dci_filter_commands(header)) {
1974 pr_debug("diag: command not supported %d %d %d",
1975 header->cmd_code, header->subsys_id,
1976 header->subsys_cmd_code);
1977 mutex_unlock(&driver->dci_mutex);
1978 return DIAG_DCI_SEND_DATA_FAIL;
1981 common_cmd = diag_check_common_cmd(header);
1982 if (common_cmd < 0) {
1983 pr_debug("diag: error in checking common command, %d\n",
1985 mutex_unlock(&driver->dci_mutex);
1986 return DIAG_DCI_SEND_DATA_FAIL;
1990 * Previous packet is yet to be consumed by the client. Wait
1991 * till the buffer is free.
1993 while (retry_count < max_retries) {
1995 if (driver->in_busy_dcipktdata)
1996 usleep_range(10000, 10100);
2000 /* The buffer is still busy */
2001 if (driver->in_busy_dcipktdata) {
2002 pr_err("diag: In %s, apps dci buffer is still busy. Dropping packet\n",
2004 mutex_unlock(&driver->dci_mutex);
2008 /* Register this new DCI packet */
2009 req_entry = diag_register_dci_transaction(req_hdr.uid,
2012 pr_alert("diag: registering new DCI transaction failed\n");
2013 mutex_unlock(&driver->dci_mutex);
2014 return DIAG_DCI_NO_REG;
2016 mutex_unlock(&driver->dci_mutex);
2019 * If the client has registered for remote data, route the packet to the
2022 if (dci_entry->client_info.token > 0) {
2023 ret = diag_send_dci_pkt_remote(req_buf, req_len, req_entry->tag,
2024 dci_entry->client_info.token);
2028 /* Check if it is a dedicated Apps command */
2029 ret = diag_dci_process_apps_pkt(header, req_buf, req_len,
2031 if ((ret == DIAG_DCI_NO_ERROR && !common_cmd) || ret < 0)
2034 reg_entry.cmd_code = header->cmd_code;
2035 reg_entry.subsys_id = header->subsys_id;
2036 reg_entry.cmd_code_hi = header->subsys_cmd_code;
2037 reg_entry.cmd_code_lo = header->subsys_cmd_code;
2039 mutex_lock(&driver->cmd_reg_mutex);
2040 temp_entry = diag_cmd_search(®_entry, ALL_PROC);
2042 reg_item = container_of(temp_entry, struct diag_cmd_reg_t,
2044 ret = diag_send_dci_pkt(reg_item, req_buf, req_len,
2047 DIAG_LOG(DIAG_DEBUG_DCI, "Command not found: %02x %02x %02x\n",
2048 reg_entry.cmd_code, reg_entry.subsys_id,
2049 reg_entry.cmd_code_hi);
2051 mutex_unlock(&driver->cmd_reg_mutex);
2056 int diag_process_dci_transaction(unsigned char *buf, int len)
2058 unsigned char *temp = buf;
2059 uint16_t log_code, item_num;
2060 int ret = -1, found = 0, client_id = 0, client_token = 0;
2061 int count, set_mask, num_codes, bit_index, event_id, offset = 0;
2062 unsigned int byte_index, read_len = 0;
2063 uint8_t equip_id, *log_mask_ptr, *head_log_mask_ptr, byte_mask;
2064 uint8_t *event_mask_ptr;
2065 struct diag_dci_client_tbl *dci_entry = NULL;
2068 pr_err("diag: Invalid buffer in %s\n", __func__);
2072 /* This is Pkt request/response transaction */
2073 if (*(int *)temp > 0) {
2074 return diag_process_dci_pkt_rsp(buf, len);
2075 } else if (*(int *)temp == DCI_LOG_TYPE) {
2076 /* Minimum length of a log mask config is 12 + 2 bytes for
2077 atleast one log code to be set or reset */
2078 if (len < DCI_LOG_CON_MIN_LEN || len > USER_SPACE_DATA) {
2079 pr_err("diag: dci: Invalid length in %s\n", __func__);
2083 /* Extract each log code and put in client table */
2084 temp += sizeof(int);
2085 read_len += sizeof(int);
2086 client_id = *(int *)temp;
2087 temp += sizeof(int);
2088 read_len += sizeof(int);
2089 set_mask = *(int *)temp;
2090 temp += sizeof(int);
2091 read_len += sizeof(int);
2092 num_codes = *(int *)temp;
2093 temp += sizeof(int);
2094 read_len += sizeof(int);
2096 /* find client table entry */
2097 mutex_lock(&driver->dci_mutex);
2098 dci_entry = diag_dci_get_client_entry(client_id);
2100 pr_err("diag: In %s, invalid client\n", __func__);
2101 mutex_unlock(&driver->dci_mutex);
2104 client_token = dci_entry->client_info.token;
2106 if (num_codes == 0 || (num_codes >= (USER_SPACE_DATA - 8)/2)) {
2107 pr_err("diag: dci: Invalid number of log codes %d\n",
2109 mutex_unlock(&driver->dci_mutex);
2113 head_log_mask_ptr = dci_entry->dci_log_mask;
2114 if (!head_log_mask_ptr) {
2115 pr_err("diag: dci: Invalid Log mask pointer in %s\n",
2117 mutex_unlock(&driver->dci_mutex);
2120 pr_debug("diag: head of dci log mask %pK\n", head_log_mask_ptr);
2121 count = 0; /* iterator for extracting log codes */
2123 while (count < num_codes) {
2124 if (read_len >= USER_SPACE_DATA) {
2125 pr_err("diag: dci: Invalid length for log type in %s",
2127 mutex_unlock(&driver->dci_mutex);
2130 log_code = *(uint16_t *)temp;
2131 equip_id = LOG_GET_EQUIP_ID(log_code);
2132 item_num = LOG_GET_ITEM_NUM(log_code);
2133 byte_index = item_num/8 + 2;
2134 if (byte_index >= (DCI_MAX_ITEMS_PER_LOG_CODE+2)) {
2135 pr_err("diag: dci: Log type, invalid byte index\n");
2136 mutex_unlock(&driver->dci_mutex);
2139 byte_mask = 0x01 << (item_num % 8);
2141 * Parse through log mask table and find
2144 log_mask_ptr = head_log_mask_ptr;
2147 while (log_mask_ptr && (offset < DCI_LOG_MASK_SIZE)) {
2148 if (*log_mask_ptr == equip_id) {
2150 pr_debug("diag: find equip id = %x at %pK\n",
2151 equip_id, log_mask_ptr);
2154 pr_debug("diag: did not find equip id = %x at %d\n",
2155 equip_id, *log_mask_ptr);
2156 log_mask_ptr += 514;
2161 pr_err("diag: dci equip id not found\n");
2162 mutex_unlock(&driver->dci_mutex);
2165 *(log_mask_ptr+1) = 1; /* set the dirty byte */
2166 log_mask_ptr = log_mask_ptr + byte_index;
2168 *log_mask_ptr |= byte_mask;
2170 *log_mask_ptr &= ~byte_mask;
2171 /* add to cumulative mask */
2172 update_dci_cumulative_log_mask(
2174 byte_mask, client_token);
2178 ret = DIAG_DCI_NO_ERROR;
2180 /* send updated mask to userspace clients */
2181 if (client_token == DCI_LOCAL_PROC)
2182 diag_update_userspace_clients(DCI_LOG_MASKS_TYPE);
2183 /* send updated mask to peripherals */
2184 ret = dci_ops_tbl[client_token].send_log_mask(client_token);
2185 mutex_unlock(&driver->dci_mutex);
2186 } else if (*(int *)temp == DCI_EVENT_TYPE) {
2187 /* Minimum length of a event mask config is 12 + 4 bytes for
2188 atleast one event id to be set or reset. */
2189 if (len < DCI_EVENT_CON_MIN_LEN || len > USER_SPACE_DATA) {
2190 pr_err("diag: dci: Invalid length in %s\n", __func__);
2194 /* Extract each event id and put in client table */
2195 temp += sizeof(int);
2196 read_len += sizeof(int);
2197 client_id = *(int *)temp;
2198 temp += sizeof(int);
2199 read_len += sizeof(int);
2200 set_mask = *(int *)temp;
2201 temp += sizeof(int);
2202 read_len += sizeof(int);
2203 num_codes = *(int *)temp;
2204 temp += sizeof(int);
2205 read_len += sizeof(int);
2207 /* find client table entry */
2208 mutex_lock(&driver->dci_mutex);
2209 dci_entry = diag_dci_get_client_entry(client_id);
2211 pr_err("diag: In %s, invalid client\n", __func__);
2212 mutex_unlock(&driver->dci_mutex);
2215 client_token = dci_entry->client_info.token;
2217 /* Check for positive number of event ids. Also, the number of
2218 event ids should fit in the buffer along with set_mask and
2219 num_codes which are 4 bytes each */
2220 if (num_codes == 0 || (num_codes >= (USER_SPACE_DATA - 8)/2)) {
2221 pr_err("diag: dci: Invalid number of event ids %d\n",
2223 mutex_unlock(&driver->dci_mutex);
2227 event_mask_ptr = dci_entry->dci_event_mask;
2228 if (!event_mask_ptr) {
2229 pr_err("diag: dci: Invalid event mask pointer in %s\n",
2231 mutex_unlock(&driver->dci_mutex);
2234 pr_debug("diag: head of dci event mask %pK\n", event_mask_ptr);
2235 count = 0; /* iterator for extracting log codes */
2236 while (count < num_codes) {
2237 if (read_len >= USER_SPACE_DATA) {
2238 pr_err("diag: dci: Invalid length for event type in %s",
2240 mutex_unlock(&driver->dci_mutex);
2243 event_id = *(int *)temp;
2244 byte_index = event_id/8;
2245 if (byte_index >= DCI_EVENT_MASK_SIZE) {
2246 pr_err("diag: dci: Event type, invalid byte index\n");
2247 mutex_unlock(&driver->dci_mutex);
2250 bit_index = event_id % 8;
2251 byte_mask = 0x1 << bit_index;
2253 * Parse through event mask table and set
2254 * relevant byte & bit combination
2257 *(event_mask_ptr + byte_index) |= byte_mask;
2259 *(event_mask_ptr + byte_index) &= ~byte_mask;
2260 /* add to cumulative mask */
2261 update_dci_cumulative_event_mask(byte_index, byte_mask,
2263 temp += sizeof(int);
2264 read_len += sizeof(int);
2266 ret = DIAG_DCI_NO_ERROR;
2268 /* send updated mask to userspace clients */
2269 if (dci_entry->client_info.token == DCI_LOCAL_PROC)
2270 diag_update_userspace_clients(DCI_EVENT_MASKS_TYPE);
2271 /* send updated mask to peripherals */
2272 ret = dci_ops_tbl[client_token].send_event_mask(client_token);
2273 mutex_unlock(&driver->dci_mutex);
2275 pr_alert("diag: Incorrect DCI transaction\n");
2281 struct diag_dci_client_tbl *diag_dci_get_client_entry(int client_id)
2283 struct list_head *start, *temp;
2284 struct diag_dci_client_tbl *entry = NULL;
2285 list_for_each_safe(start, temp, &driver->dci_client_list) {
2286 entry = list_entry(start, struct diag_dci_client_tbl, track);
2287 if (entry->client_info.client_id == client_id)
2293 struct diag_dci_client_tbl *dci_lookup_client_entry_pid(int tgid)
2295 struct list_head *start, *temp;
2296 struct diag_dci_client_tbl *entry = NULL;
2297 struct pid *pid_struct = NULL;
2298 struct task_struct *task_s = NULL;
2300 list_for_each_safe(start, temp, &driver->dci_client_list) {
2301 entry = list_entry(start, struct diag_dci_client_tbl, track);
2302 pid_struct = find_get_pid(entry->tgid);
2304 DIAG_LOG(DIAG_DEBUG_DCI,
2305 "diag: Exited pid (%d) doesn't match dci client of pid (%d)\n",
2309 task_s = get_pid_task(pid_struct, PIDTYPE_PID);
2311 DIAG_LOG(DIAG_DEBUG_DCI,
2312 "diag: valid task doesn't exist for pid = %d\n",
2314 put_pid(pid_struct);
2317 if (task_s == entry->client) {
2318 if (entry->client->tgid == tgid) {
2319 put_task_struct(task_s);
2320 put_pid(pid_struct);
2324 put_task_struct(task_s);
2325 put_pid(pid_struct);
2330 void update_dci_cumulative_event_mask(int offset, uint8_t byte_mask, int token)
2332 uint8_t *event_mask_ptr, *update_ptr = NULL;
2333 struct list_head *start, *temp;
2334 struct diag_dci_client_tbl *entry = NULL;
2335 bool is_set = false;
2337 mutex_lock(&dci_event_mask_mutex);
2338 update_ptr = dci_ops_tbl[token].event_mask_composite;
2340 mutex_unlock(&dci_event_mask_mutex);
2343 update_ptr += offset;
2344 list_for_each_safe(start, temp, &driver->dci_client_list) {
2345 entry = list_entry(start, struct diag_dci_client_tbl, track);
2346 if (entry->client_info.token != token)
2348 event_mask_ptr = entry->dci_event_mask;
2349 event_mask_ptr += offset;
2350 if ((*event_mask_ptr & byte_mask) == byte_mask) {
2352 /* break even if one client has the event mask set */
2356 if (is_set == false)
2357 *update_ptr &= ~byte_mask;
2359 *update_ptr |= byte_mask;
2360 mutex_unlock(&dci_event_mask_mutex);
2363 void diag_dci_invalidate_cumulative_event_mask(int token)
2366 struct list_head *start, *temp;
2367 struct diag_dci_client_tbl *entry = NULL;
2368 uint8_t *event_mask_ptr, *update_ptr = NULL;
2370 mutex_lock(&dci_event_mask_mutex);
2371 update_ptr = dci_ops_tbl[token].event_mask_composite;
2373 mutex_unlock(&dci_event_mask_mutex);
2377 create_dci_event_mask_tbl(update_ptr);
2378 list_for_each_safe(start, temp, &driver->dci_client_list) {
2379 entry = list_entry(start, struct diag_dci_client_tbl, track);
2380 if (entry->client_info.token != token)
2382 event_mask_ptr = entry->dci_event_mask;
2383 for (i = 0; i < DCI_EVENT_MASK_SIZE; i++)
2384 *(update_ptr+i) |= *(event_mask_ptr+i);
2386 mutex_unlock(&dci_event_mask_mutex);
2389 #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
2390 int diag_send_dci_event_mask_remote(int token)
2392 unsigned char *buf = NULL;
2393 struct diag_dci_header_t dci_header;
2394 struct diag_ctrl_event_mask event_mask;
2395 int dci_header_size = sizeof(struct diag_dci_header_t);
2396 int event_header_size = sizeof(struct diag_ctrl_event_mask);
2397 int i, ret = DIAG_DCI_NO_ERROR, err = DIAG_DCI_NO_ERROR;
2398 unsigned char *event_mask_ptr = NULL;
2399 uint32_t write_len = 0;
2401 mutex_lock(&dci_event_mask_mutex);
2402 event_mask_ptr = dci_ops_tbl[token].event_mask_composite;
2403 if (!event_mask_ptr) {
2404 mutex_unlock(&dci_event_mask_mutex);
2407 buf = dci_get_buffer_from_bridge(token);
2409 pr_err("diag: In %s, unable to get dci buffers to write data\n",
2411 mutex_unlock(&dci_event_mask_mutex);
2415 /* Frame the DCI header */
2416 dci_header.start = CONTROL_CHAR;
2417 dci_header.version = 1;
2418 dci_header.length = event_header_size + DCI_EVENT_MASK_SIZE + 1;
2419 dci_header.cmd_code = DCI_CONTROL_PKT_CODE;
2421 event_mask.cmd_type = DIAG_CTRL_MSG_EVENT_MASK;
2422 event_mask.data_len = EVENT_MASK_CTRL_HEADER_LEN + DCI_EVENT_MASK_SIZE;
2423 event_mask.stream_id = DCI_MASK_STREAM;
2424 event_mask.status = DIAG_CTRL_MASK_VALID;
2425 event_mask.event_config = 0; /* event config */
2426 event_mask.event_mask_size = DCI_EVENT_MASK_SIZE;
2427 for (i = 0; i < DCI_EVENT_MASK_SIZE; i++) {
2428 if (event_mask_ptr[i] != 0) {
2429 event_mask.event_config = 1;
2433 memcpy(buf + write_len, &dci_header, dci_header_size);
2434 write_len += dci_header_size;
2435 memcpy(buf + write_len, &event_mask, event_header_size);
2436 write_len += event_header_size;
2437 memcpy(buf + write_len, event_mask_ptr, DCI_EVENT_MASK_SIZE);
2438 write_len += DCI_EVENT_MASK_SIZE;
2439 *(buf + write_len) = CONTROL_CHAR; /* End Terminator */
2440 write_len += sizeof(uint8_t);
2441 err = diag_dci_write_bridge(token, buf, write_len);
2443 pr_err("diag: error writing event mask to remote proc, token: %d, err: %d\n",
2445 diagmem_free(driver, buf, dci_ops_tbl[token].mempool);
2448 ret = DIAG_DCI_NO_ERROR;
2450 mutex_unlock(&dci_event_mask_mutex);
2455 int diag_send_dci_event_mask(int token)
2457 void *buf = event_mask.update_buf;
2458 struct diag_ctrl_event_mask header;
2459 int header_size = sizeof(struct diag_ctrl_event_mask);
2460 int ret = DIAG_DCI_NO_ERROR, err = DIAG_DCI_NO_ERROR, i;
2461 unsigned char *event_mask_ptr = NULL;
2463 mutex_lock(&dci_event_mask_mutex);
2464 event_mask_ptr = dci_ops_tbl[DCI_LOCAL_PROC].event_mask_composite;
2465 if (!event_mask_ptr) {
2466 mutex_unlock(&dci_event_mask_mutex);
2470 mutex_lock(&event_mask.lock);
2471 /* send event mask update */
2472 header.cmd_type = DIAG_CTRL_MSG_EVENT_MASK;
2473 header.data_len = EVENT_MASK_CTRL_HEADER_LEN + DCI_EVENT_MASK_SIZE;
2474 header.stream_id = DCI_MASK_STREAM;
2475 header.status = DIAG_CTRL_MASK_VALID;
2476 header.event_config = 0; /* event config */
2477 header.event_mask_size = DCI_EVENT_MASK_SIZE;
2478 for (i = 0; i < DCI_EVENT_MASK_SIZE; i++) {
2479 if (event_mask_ptr[i] != 0) {
2480 header.event_config = 1;
2484 memcpy(buf, &header, header_size);
2485 memcpy(buf+header_size, event_mask_ptr, DCI_EVENT_MASK_SIZE);
2486 for (i = 0; i < NUM_PERIPHERALS; i++) {
2488 * Don't send to peripheral if its regular channel
2489 * is down. It may also mean that the peripheral doesn't
2492 if (check_peripheral_dci_support(i, DCI_LOCAL_PROC)) {
2493 err = diag_dci_write_proc(i, DIAG_CNTL_TYPE, buf,
2494 header_size + DCI_EVENT_MASK_SIZE);
2495 if (err != DIAG_DCI_NO_ERROR)
2496 ret = DIAG_DCI_SEND_DATA_FAIL;
2500 mutex_unlock(&event_mask.lock);
2501 mutex_unlock(&dci_event_mask_mutex);
2506 void update_dci_cumulative_log_mask(int offset, unsigned int byte_index,
2507 uint8_t byte_mask, int token)
2509 uint8_t *log_mask_ptr, *update_ptr = NULL;
2510 bool is_set = false;
2511 struct list_head *start, *temp;
2512 struct diag_dci_client_tbl *entry = NULL;
2514 mutex_lock(&dci_log_mask_mutex);
2515 update_ptr = dci_ops_tbl[token].log_mask_composite;
2517 mutex_unlock(&dci_log_mask_mutex);
2521 update_ptr += offset;
2522 /* update the dirty bit */
2523 *(update_ptr+1) = 1;
2524 update_ptr = update_ptr + byte_index;
2525 list_for_each_safe(start, temp, &driver->dci_client_list) {
2526 entry = list_entry(start, struct diag_dci_client_tbl, track);
2527 if (entry->client_info.token != token)
2529 log_mask_ptr = entry->dci_log_mask;
2530 log_mask_ptr = log_mask_ptr + offset + byte_index;
2531 if ((*log_mask_ptr & byte_mask) == byte_mask) {
2533 /* break even if one client has the log mask set */
2538 if (is_set == false)
2539 *update_ptr &= ~byte_mask;
2541 *update_ptr |= byte_mask;
2542 mutex_unlock(&dci_log_mask_mutex);
2545 void diag_dci_invalidate_cumulative_log_mask(int token)
2548 struct list_head *start, *temp;
2549 struct diag_dci_client_tbl *entry = NULL;
2550 uint8_t *log_mask_ptr, *update_ptr = NULL;
2552 /* Clear the composite mask and redo all the masks */
2553 mutex_lock(&dci_log_mask_mutex);
2554 update_ptr = dci_ops_tbl[token].log_mask_composite;
2556 mutex_unlock(&dci_log_mask_mutex);
2560 create_dci_log_mask_tbl(update_ptr, DCI_LOG_MASK_DIRTY);
2561 list_for_each_safe(start, temp, &driver->dci_client_list) {
2562 entry = list_entry(start, struct diag_dci_client_tbl, track);
2563 if (entry->client_info.token != token)
2565 log_mask_ptr = entry->dci_log_mask;
2566 for (i = 0; i < DCI_LOG_MASK_SIZE; i++)
2567 *(update_ptr+i) |= *(log_mask_ptr+i);
2569 mutex_unlock(&dci_log_mask_mutex);
2572 static int dci_fill_log_mask(unsigned char *dest_ptr, unsigned char *src_ptr)
2574 struct diag_ctrl_log_mask header;
2575 int header_len = sizeof(struct diag_ctrl_log_mask);
2577 header.cmd_type = DIAG_CTRL_MSG_LOG_MASK;
2578 header.num_items = DCI_MAX_ITEMS_PER_LOG_CODE;
2579 header.data_len = 11 + DCI_MAX_ITEMS_PER_LOG_CODE;
2580 header.stream_id = DCI_MASK_STREAM;
2582 header.equip_id = *src_ptr;
2583 header.log_mask_size = DCI_MAX_ITEMS_PER_LOG_CODE;
2584 memcpy(dest_ptr, &header, header_len);
2585 memcpy(dest_ptr + header_len, src_ptr + 2, DCI_MAX_ITEMS_PER_LOG_CODE);
2587 return header_len + DCI_MAX_ITEMS_PER_LOG_CODE;
2590 #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
2591 int diag_send_dci_log_mask_remote(int token)
2594 unsigned char *buf = NULL;
2595 struct diag_dci_header_t dci_header;
2596 int dci_header_size = sizeof(struct diag_dci_header_t);
2597 int log_header_size = sizeof(struct diag_ctrl_log_mask);
2598 uint8_t *log_mask_ptr = NULL;
2599 int i, ret = DIAG_DCI_NO_ERROR, err = DIAG_DCI_NO_ERROR;
2601 uint32_t write_len = 0;
2603 mutex_lock(&dci_log_mask_mutex);
2604 log_mask_ptr = dci_ops_tbl[token].log_mask_composite;
2605 if (!log_mask_ptr) {
2606 mutex_unlock(&dci_log_mask_mutex);
2610 /* DCI header is common to all equipment IDs */
2611 dci_header.start = CONTROL_CHAR;
2612 dci_header.version = 1;
2613 dci_header.length = log_header_size + DCI_MAX_ITEMS_PER_LOG_CODE + 1;
2614 dci_header.cmd_code = DCI_CONTROL_PKT_CODE;
2616 for (i = 0; i < DCI_MAX_LOG_CODES; i++) {
2619 if (!*(log_mask_ptr + 1)) {
2620 log_mask_ptr += 514;
2624 buf = dci_get_buffer_from_bridge(token);
2626 pr_err("diag: In %s, unable to get dci buffers to write data\n",
2628 mutex_unlock(&dci_log_mask_mutex);
2632 memcpy(buf + write_len, &dci_header, dci_header_size);
2633 write_len += dci_header_size;
2634 write_len += dci_fill_log_mask(buf + write_len, log_mask_ptr);
2635 *(buf + write_len) = CONTROL_CHAR; /* End Terminator */
2636 write_len += sizeof(uint8_t);
2637 err = diag_dci_write_bridge(token, buf, write_len);
2639 pr_err("diag: error writing log mask to remote processor, equip_id: %d, token: %d, err: %d\n",
2641 diagmem_free(driver, buf, dci_ops_tbl[token].mempool);
2645 *(log_mask_ptr + 1) = 0; /* clear dirty byte */
2646 log_mask_ptr += 514;
2648 mutex_unlock(&dci_log_mask_mutex);
2653 int diag_send_dci_log_mask(int token)
2655 void *buf = log_mask.update_buf;
2657 uint8_t *log_mask_ptr = NULL;
2658 int i, j, ret = DIAG_DCI_NO_ERROR, err = DIAG_DCI_NO_ERROR;
2662 mutex_lock(&dci_log_mask_mutex);
2663 log_mask_ptr = dci_ops_tbl[DCI_LOCAL_PROC].log_mask_composite;
2664 if (!log_mask_ptr) {
2665 mutex_unlock(&dci_log_mask_mutex);
2669 mutex_lock(&log_mask.lock);
2670 for (i = 0; i < 16; i++) {
2672 /* Dirty bit is set don't update the mask for this equip id */
2673 if (!(*(log_mask_ptr + 1))) {
2674 log_mask_ptr += 514;
2677 write_len = dci_fill_log_mask(buf, log_mask_ptr);
2678 for (j = 0; j < NUM_PERIPHERALS && write_len; j++) {
2679 if (check_peripheral_dci_support(j, DCI_LOCAL_PROC)) {
2680 err = diag_dci_write_proc(j, DIAG_CNTL_TYPE,
2682 if (err != DIAG_DCI_NO_ERROR) {
2684 ret = DIAG_DCI_SEND_DATA_FAIL;
2689 *(log_mask_ptr+1) = 0; /* clear dirty byte */
2690 log_mask_ptr += 514;
2692 mutex_unlock(&log_mask.lock);
2693 mutex_unlock(&dci_log_mask_mutex);
2697 static int diag_dci_init_local(void)
2699 struct dci_ops_tbl_t *temp = &dci_ops_tbl[DCI_LOCAL_PROC];
2701 create_dci_log_mask_tbl(temp->log_mask_composite, DCI_LOG_MASK_CLEAN);
2702 create_dci_event_mask_tbl(temp->event_mask_composite);
2703 temp->peripheral_status |= DIAG_CON_APSS;
2708 #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
2709 static void diag_dci_init_handshake_remote(void)
2712 struct dci_channel_status_t *temp = NULL;
2714 for (i = DCI_REMOTE_BASE; i < NUM_DCI_PROC; i++) {
2715 temp = &dci_channel_status[i];
2717 setup_timer(&temp->wait_time, dci_chk_handshake, i);
2718 INIT_WORK(&temp->handshake_work, dci_handshake_work_fn);
2722 static int diag_dci_init_remote(void)
2725 struct dci_ops_tbl_t *temp = NULL;
2727 diagmem_init(driver, POOL_TYPE_MDM_DCI_WRITE);
2729 for (i = DCI_REMOTE_BASE; i < DCI_REMOTE_LAST; i++) {
2730 temp = &dci_ops_tbl[i];
2731 create_dci_log_mask_tbl(temp->log_mask_composite,
2732 DCI_LOG_MASK_CLEAN);
2733 create_dci_event_mask_tbl(temp->event_mask_composite);
2736 partial_pkt.data = vzalloc(MAX_DCI_PACKET_SZ);
2737 if (!partial_pkt.data) {
2738 pr_err("diag: Unable to create partial pkt data\n");
2742 partial_pkt.total_len = 0;
2743 partial_pkt.read_len = 0;
2744 partial_pkt.remaining = 0;
2745 partial_pkt.processing = 0;
2747 diag_dci_init_handshake_remote();
2752 static int diag_dci_init_remote(void)
2758 static int diag_dci_init_ops_tbl(void)
2762 err = diag_dci_init_local();
2765 err = diag_dci_init_remote();
2775 int diag_dci_init(void)
2779 driver->dci_tag = 0;
2780 driver->dci_client_id = 0;
2781 driver->num_dci_client = 0;
2782 mutex_init(&driver->dci_mutex);
2783 mutex_init(&dci_log_mask_mutex);
2784 mutex_init(&dci_event_mask_mutex);
2785 spin_lock_init(&ws_lock);
2787 ret = diag_dci_init_ops_tbl();
2791 if (driver->apps_dci_buf == NULL) {
2792 driver->apps_dci_buf = vzalloc(DCI_BUF_SIZE);
2793 if (driver->apps_dci_buf == NULL)
2796 INIT_LIST_HEAD(&driver->dci_client_list);
2797 INIT_LIST_HEAD(&driver->dci_req_list);
2799 driver->diag_dci_wq = create_singlethread_workqueue("diag_dci_wq");
2800 if (!driver->diag_dci_wq)
2803 INIT_WORK(&dci_data_drain_work, dci_data_drain_work_fn);
2805 setup_timer(&dci_drain_timer, dci_drain_data, 0);
2806 return DIAG_DCI_NO_ERROR;
2808 pr_err("diag: Could not initialize diag DCI buffers");
2809 vfree(driver->apps_dci_buf);
2810 driver->apps_dci_buf = NULL;
2812 if (driver->diag_dci_wq)
2813 destroy_workqueue(driver->diag_dci_wq);
2814 vfree(partial_pkt.data);
2815 partial_pkt.data = NULL;
2816 mutex_destroy(&driver->dci_mutex);
2817 mutex_destroy(&dci_log_mask_mutex);
2818 mutex_destroy(&dci_event_mask_mutex);
2819 return DIAG_DCI_NO_REG;
2822 void diag_dci_channel_init(void)
2826 for (peripheral = 0; peripheral < NUM_PERIPHERALS; peripheral++) {
2827 diagfwd_open(peripheral, TYPE_DCI);
2828 diagfwd_open(peripheral, TYPE_DCI_CMD);
2832 void diag_dci_exit(void)
2834 vfree(partial_pkt.data);
2835 partial_pkt.data = NULL;
2836 vfree(driver->apps_dci_buf);
2837 driver->apps_dci_buf = NULL;
2838 mutex_destroy(&driver->dci_mutex);
2839 mutex_destroy(&dci_log_mask_mutex);
2840 mutex_destroy(&dci_event_mask_mutex);
2841 destroy_workqueue(driver->diag_dci_wq);
2844 int diag_dci_clear_log_mask(int client_id)
2846 int err = DIAG_DCI_NO_ERROR, token = DCI_LOCAL_PROC;
2847 uint8_t *update_ptr;
2848 struct diag_dci_client_tbl *entry = NULL;
2850 entry = diag_dci_get_client_entry(client_id);
2852 pr_err("diag: In %s, invalid client entry\n", __func__);
2853 return DIAG_DCI_TABLE_ERR;
2855 token = entry->client_info.token;
2856 update_ptr = dci_ops_tbl[token].log_mask_composite;
2858 create_dci_log_mask_tbl(entry->dci_log_mask, DCI_LOG_MASK_CLEAN);
2859 diag_dci_invalidate_cumulative_log_mask(token);
2862 * Send updated mask to userspace clients only if the client
2863 * is registered on the local processor
2865 if (token == DCI_LOCAL_PROC)
2866 diag_update_userspace_clients(DCI_LOG_MASKS_TYPE);
2867 /* Send updated mask to peripherals */
2868 err = dci_ops_tbl[token].send_log_mask(token);
2872 int diag_dci_clear_event_mask(int client_id)
2874 int err = DIAG_DCI_NO_ERROR, token = DCI_LOCAL_PROC;
2875 uint8_t *update_ptr;
2876 struct diag_dci_client_tbl *entry = NULL;
2878 entry = diag_dci_get_client_entry(client_id);
2880 pr_err("diag: In %s, invalid client entry\n", __func__);
2881 return DIAG_DCI_TABLE_ERR;
2883 token = entry->client_info.token;
2884 update_ptr = dci_ops_tbl[token].event_mask_composite;
2886 create_dci_event_mask_tbl(entry->dci_event_mask);
2887 diag_dci_invalidate_cumulative_event_mask(token);
2890 * Send updated mask to userspace clients only if the client is
2891 * registerted on the local processor
2893 if (token == DCI_LOCAL_PROC)
2894 diag_update_userspace_clients(DCI_EVENT_MASKS_TYPE);
2895 /* Send updated mask to peripherals */
2896 err = dci_ops_tbl[token].send_event_mask(token);
2900 uint8_t diag_dci_get_cumulative_real_time(int token)
2902 uint8_t real_time = MODE_NONREALTIME;
2903 struct list_head *start, *temp;
2904 struct diag_dci_client_tbl *entry = NULL;
2906 list_for_each_safe(start, temp, &driver->dci_client_list) {
2907 entry = list_entry(start, struct diag_dci_client_tbl, track);
2908 if (entry->real_time == MODE_REALTIME &&
2909 entry->client_info.token == token) {
2917 int diag_dci_set_real_time(struct diag_dci_client_tbl *entry, uint8_t real_time)
2920 pr_err("diag: In %s, invalid client entry\n", __func__);
2923 entry->real_time = real_time;
2927 int diag_dci_register_client(struct diag_dci_reg_tbl_t *reg_entry)
2930 struct diag_dci_client_tbl *new_entry = NULL;
2931 struct diag_dci_buf_peripheral_t *proc_buf = NULL;
2934 return DIAG_DCI_NO_REG;
2935 if (!VALID_DCI_TOKEN(reg_entry->token)) {
2936 pr_alert("diag: Invalid DCI client token, %d\n",
2938 return DIAG_DCI_NO_REG;
2941 if (driver->dci_state == DIAG_DCI_NO_REG)
2942 return DIAG_DCI_NO_REG;
2944 if (driver->num_dci_client >= MAX_DCI_CLIENTS)
2945 return DIAG_DCI_NO_REG;
2947 new_entry = kzalloc(sizeof(struct diag_dci_client_tbl), GFP_KERNEL);
2948 if (new_entry == NULL) {
2949 pr_err("diag: unable to alloc memory\n");
2950 return DIAG_DCI_NO_REG;
2953 mutex_lock(&driver->dci_mutex);
2955 get_task_struct(current);
2956 new_entry->client = current;
2957 new_entry->tgid = current->tgid;
2958 new_entry->client_info.notification_list =
2959 reg_entry->notification_list;
2960 new_entry->client_info.signal_type =
2961 reg_entry->signal_type;
2962 new_entry->client_info.token = reg_entry->token;
2963 switch (reg_entry->token) {
2964 case DCI_LOCAL_PROC:
2965 new_entry->num_buffers = NUM_DCI_PERIPHERALS;
2968 new_entry->num_buffers = 1;
2972 new_entry->buffers = NULL;
2973 new_entry->real_time = MODE_REALTIME;
2974 new_entry->in_service = 0;
2975 INIT_LIST_HEAD(&new_entry->list_write_buf);
2976 mutex_init(&new_entry->write_buf_mutex);
2977 new_entry->dci_log_mask = vzalloc(DCI_LOG_MASK_SIZE);
2978 if (!new_entry->dci_log_mask) {
2979 pr_err("diag: Unable to create log mask for client, %d",
2980 driver->dci_client_id);
2983 create_dci_log_mask_tbl(new_entry->dci_log_mask, DCI_LOG_MASK_CLEAN);
2985 new_entry->dci_event_mask = vzalloc(DCI_EVENT_MASK_SIZE);
2986 if (!new_entry->dci_event_mask) {
2987 pr_err("diag: Unable to create event mask for client, %d",
2988 driver->dci_client_id);
2991 create_dci_event_mask_tbl(new_entry->dci_event_mask);
2993 new_entry->buffers = kzalloc(new_entry->num_buffers *
2994 sizeof(struct diag_dci_buf_peripheral_t),
2996 if (!new_entry->buffers) {
2997 pr_err("diag: Unable to allocate buffers for peripherals in %s\n",
3002 for (i = 0; i < new_entry->num_buffers; i++) {
3003 proc_buf = &new_entry->buffers[i];
3007 mutex_init(&proc_buf->health_mutex);
3008 mutex_init(&proc_buf->buf_mutex);
3009 proc_buf->health.dropped_events = 0;
3010 proc_buf->health.dropped_logs = 0;
3011 proc_buf->health.received_events = 0;
3012 proc_buf->health.received_logs = 0;
3013 proc_buf->buf_primary = kzalloc(
3014 sizeof(struct diag_dci_buffer_t),
3016 if (!proc_buf->buf_primary)
3018 proc_buf->buf_cmd = kzalloc(sizeof(struct diag_dci_buffer_t),
3020 if (!proc_buf->buf_cmd)
3022 err = diag_dci_init_buffer(proc_buf->buf_primary,
3026 err = diag_dci_init_buffer(proc_buf->buf_cmd, DCI_BUF_CMD);
3029 proc_buf->buf_curr = proc_buf->buf_primary;
3032 list_add_tail(&new_entry->track, &driver->dci_client_list);
3033 driver->dci_client_id++;
3034 new_entry->client_info.client_id = driver->dci_client_id;
3035 reg_entry->client_id = driver->dci_client_id;
3036 driver->num_dci_client++;
3037 if (driver->num_dci_client == 1)
3038 diag_update_proc_vote(DIAG_PROC_DCI, VOTE_UP, reg_entry->token);
3039 queue_work(driver->diag_real_time_wq, &driver->diag_real_time_work);
3040 mutex_unlock(&driver->dci_mutex);
3042 return driver->dci_client_id;
3046 for (i = 0; ((i < new_entry->num_buffers) &&
3047 new_entry->buffers); i++) {
3048 proc_buf = &new_entry->buffers[i];
3050 mutex_destroy(&proc_buf->health_mutex);
3051 if (proc_buf->buf_primary) {
3052 vfree(proc_buf->buf_primary->data);
3053 proc_buf->buf_primary->data = NULL;
3055 &proc_buf->buf_primary->data_mutex);
3057 kfree(proc_buf->buf_primary);
3058 proc_buf->buf_primary = NULL;
3059 if (proc_buf->buf_cmd) {
3060 vfree(proc_buf->buf_cmd->data);
3061 proc_buf->buf_cmd->data = NULL;
3063 &proc_buf->buf_cmd->data_mutex);
3065 kfree(proc_buf->buf_cmd);
3066 proc_buf->buf_cmd = NULL;
3069 vfree(new_entry->dci_event_mask);
3070 new_entry->dci_event_mask = NULL;
3071 vfree(new_entry->dci_log_mask);
3072 new_entry->dci_log_mask = NULL;
3073 kfree(new_entry->buffers);
3074 new_entry->buffers = NULL;
3078 mutex_unlock(&driver->dci_mutex);
3079 return DIAG_DCI_NO_REG;
3082 int diag_dci_deinit_client(struct diag_dci_client_tbl *entry)
3084 int ret = DIAG_DCI_NO_ERROR, real_time = MODE_REALTIME, i, peripheral;
3085 struct diag_dci_buf_peripheral_t *proc_buf = NULL;
3086 struct diag_dci_buffer_t *buf_entry, *temp;
3087 struct list_head *start, *req_temp;
3088 struct dci_pkt_req_entry_t *req_entry = NULL;
3089 int token = DCI_LOCAL_PROC;
3092 return DIAG_DCI_NOT_SUPPORTED;
3094 token = entry->client_info.token;
3096 * Remove the entry from the list before freeing the buffers
3097 * to ensure that we don't have any invalid access.
3099 if (!list_empty(&entry->track))
3100 list_del(&entry->track);
3101 driver->num_dci_client--;
3103 put_task_struct(entry->client);
3104 entry->client = NULL;
3106 * Clear the client's log and event masks, update the cumulative
3107 * masks and send the masks to peripherals
3109 vfree(entry->dci_log_mask);
3110 entry->dci_log_mask = NULL;
3111 diag_dci_invalidate_cumulative_log_mask(token);
3112 if (token == DCI_LOCAL_PROC)
3113 diag_update_userspace_clients(DCI_LOG_MASKS_TYPE);
3114 ret = dci_ops_tbl[token].send_log_mask(token);
3115 if (ret != DIAG_DCI_NO_ERROR) {
3118 vfree(entry->dci_event_mask);
3119 entry->dci_event_mask = NULL;
3120 diag_dci_invalidate_cumulative_event_mask(token);
3121 if (token == DCI_LOCAL_PROC)
3122 diag_update_userspace_clients(DCI_EVENT_MASKS_TYPE);
3123 ret = dci_ops_tbl[token].send_event_mask(token);
3124 if (ret != DIAG_DCI_NO_ERROR) {
3128 list_for_each_safe(start, req_temp, &driver->dci_req_list) {
3129 req_entry = list_entry(start, struct dci_pkt_req_entry_t,
3131 if (req_entry->client_id == entry->client_info.client_id) {
3132 if (!list_empty(&req_entry->track))
3133 list_del(&req_entry->track);
3139 /* Clean up any buffer that is pending write */
3140 mutex_lock(&entry->write_buf_mutex);
3141 list_for_each_entry_safe(buf_entry, temp, &entry->list_write_buf,
3143 if (!list_empty(&buf_entry->buf_track))
3144 list_del(&buf_entry->buf_track);
3145 if (buf_entry->buf_type == DCI_BUF_SECONDARY) {
3146 mutex_lock(&buf_entry->data_mutex);
3147 diagmem_free(driver, buf_entry->data, POOL_TYPE_DCI);
3148 buf_entry->data = NULL;
3149 mutex_unlock(&buf_entry->data_mutex);
3152 } else if (buf_entry->buf_type == DCI_BUF_CMD) {
3153 peripheral = buf_entry->data_source;
3154 if (peripheral == APPS_DATA)
3158 * These are buffers that can't be written to the client which
3159 * means that the copy cannot be completed. Make sure that we
3160 * remove those references in DCI wakeup source.
3162 diag_ws_on_copy_fail(DIAG_WS_DCI);
3164 mutex_unlock(&entry->write_buf_mutex);
3166 for (i = 0; i < entry->num_buffers; i++) {
3167 proc_buf = &entry->buffers[i];
3168 buf_entry = proc_buf->buf_curr;
3169 mutex_lock(&proc_buf->buf_mutex);
3170 /* Clean up secondary buffer from mempool that is active */
3171 if (buf_entry && buf_entry->buf_type == DCI_BUF_SECONDARY) {
3172 mutex_lock(&buf_entry->data_mutex);
3173 diagmem_free(driver, buf_entry->data, POOL_TYPE_DCI);
3174 buf_entry->data = NULL;
3175 mutex_unlock(&buf_entry->data_mutex);
3176 mutex_destroy(&buf_entry->data_mutex);
3181 mutex_lock(&proc_buf->buf_primary->data_mutex);
3182 vfree(proc_buf->buf_primary->data);
3183 proc_buf->buf_primary->data = NULL;
3184 mutex_unlock(&proc_buf->buf_primary->data_mutex);
3186 mutex_lock(&proc_buf->buf_cmd->data_mutex);
3187 vfree(proc_buf->buf_cmd->data);
3188 proc_buf->buf_cmd->data = NULL;
3189 mutex_unlock(&proc_buf->buf_cmd->data_mutex);
3191 mutex_destroy(&proc_buf->health_mutex);
3192 mutex_destroy(&proc_buf->buf_primary->data_mutex);
3193 mutex_destroy(&proc_buf->buf_cmd->data_mutex);
3195 kfree(proc_buf->buf_primary);
3196 proc_buf->buf_primary = NULL;
3197 kfree(proc_buf->buf_cmd);
3198 proc_buf->buf_cmd = NULL;
3199 mutex_unlock(&proc_buf->buf_mutex);
3201 mutex_destroy(&entry->write_buf_mutex);
3203 kfree(entry->buffers);
3204 entry->buffers = NULL;
3208 if (driver->num_dci_client == 0) {
3209 diag_update_proc_vote(DIAG_PROC_DCI, VOTE_DOWN, token);
3211 real_time = diag_dci_get_cumulative_real_time(token);
3212 diag_update_real_time_vote(DIAG_PROC_DCI, real_time, token);
3214 queue_work(driver->diag_real_time_wq, &driver->diag_real_time_work);
3216 return DIAG_DCI_NO_ERROR;
3219 int diag_dci_write_proc(uint8_t peripheral, int pkt_type, char *buf, int len)
3221 uint8_t dest_channel = TYPE_DATA;
3224 if (!buf || peripheral >= NUM_PERIPHERALS || len < 0 ||
3225 !(driver->feature[PERIPHERAL_MODEM].rcvd_feature_mask)) {
3226 DIAG_LOG(DIAG_DEBUG_DCI,
3227 "buf: 0x%pK, p: %d, len: %d, f_mask: %d\n",
3228 buf, peripheral, len,
3229 driver->feature[PERIPHERAL_MODEM].rcvd_feature_mask);
3233 if (pkt_type == DIAG_DATA_TYPE) {
3234 dest_channel = TYPE_DCI_CMD;
3235 } else if (pkt_type == DIAG_CNTL_TYPE) {
3236 dest_channel = TYPE_CNTL;
3238 pr_err("diag: Invalid DCI pkt type in %s", __func__);
3242 err = diagfwd_write(peripheral, dest_channel, buf, len);
3243 if (err && err != -ENODEV) {
3244 pr_err("diag: In %s, unable to write to peripheral: %d, type: %d, len: %d, err: %d\n",
3245 __func__, peripheral, dest_channel, len, err);
3247 err = DIAG_DCI_NO_ERROR;
3253 int diag_dci_copy_health_stats(struct diag_dci_health_stats_proc *stats_proc)
3255 struct diag_dci_client_tbl *entry = NULL;
3256 struct diag_dci_health_t *health = NULL;
3257 struct diag_dci_health_stats *stats = NULL;
3263 stats = &stats_proc->health;
3264 proc = stats_proc->proc;
3265 if (proc < ALL_PROC || proc > APPS_DATA)
3268 entry = diag_dci_get_client_entry(stats_proc->client_id);
3270 return DIAG_DCI_NOT_SUPPORTED;
3273 * If the client has registered for remote processor, the
3274 * proc field doesn't have any effect as they have only one buffer.
3276 if (entry->client_info.token)
3279 stats->stats.dropped_logs = 0;
3280 stats->stats.dropped_events = 0;
3281 stats->stats.received_logs = 0;
3282 stats->stats.received_events = 0;
3284 if (proc != ALL_PROC) {
3285 health = &entry->buffers[proc].health;
3286 stats->stats.dropped_logs = health->dropped_logs;
3287 stats->stats.dropped_events = health->dropped_events;
3288 stats->stats.received_logs = health->received_logs;
3289 stats->stats.received_events = health->received_events;
3290 if (stats->reset_status) {
3291 mutex_lock(&entry->buffers[proc].health_mutex);
3292 health->dropped_logs = 0;
3293 health->dropped_events = 0;
3294 health->received_logs = 0;
3295 health->received_events = 0;
3296 mutex_unlock(&entry->buffers[proc].health_mutex);
3298 return DIAG_DCI_NO_ERROR;
3301 for (i = 0; i < entry->num_buffers; i++) {
3302 health = &entry->buffers[i].health;
3303 stats->stats.dropped_logs += health->dropped_logs;
3304 stats->stats.dropped_events += health->dropped_events;
3305 stats->stats.received_logs += health->received_logs;
3306 stats->stats.received_events += health->received_events;
3307 if (stats->reset_status) {
3308 mutex_lock(&entry->buffers[i].health_mutex);
3309 health->dropped_logs = 0;
3310 health->dropped_events = 0;
3311 health->received_logs = 0;
3312 health->received_events = 0;
3313 mutex_unlock(&entry->buffers[i].health_mutex);
3316 return DIAG_DCI_NO_ERROR;
3319 int diag_dci_get_support_list(struct diag_dci_peripherals_t *support_list)
3324 if (!VALID_DCI_TOKEN(support_list->proc))
3327 support_list->list = dci_ops_tbl[support_list->proc].peripheral_status;
3328 return DIAG_DCI_NO_ERROR;