summaryrefslogtreecommitdiff
path: root/debug.c
blob: 1432eeec471c09302446d6d4ed604f371d9efb4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
// SPDX-License-Identifier: GPL-2.0
/*
 * This file is part of the QM35 UCI stack for linux.
 *
 * Copyright (c) 2022 Qorvo US, Inc.
 *
 * This software is provided under the GNU General Public License, version 2
 * (GPLv2), as well as under a Qorvo commercial license.
 *
 * You may choose to use this software under the terms of the GPLv2 License,
 * version 2 ("GPLv2"), as published by the Free Software Foundation.
 * You should have received a copy of the GPLv2 along with this program.  If
 * not, see <http://www.gnu.org/licenses/>.
 *
 * This program is distributed under the GPLv2 in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GPLv2 for more
 * details.
 *
 * If you cannot meet the requirements of the GPLv2, you may not use this
 * software for any purpose without first obtaining a commercial license from
 * Qorvo.
 * Please contact Qorvo to inquire about licensing terms.
 *
 * QM35 LOG layer HSSPI Protocol
 */

#include <linux/debugfs.h>
#include <linux/poll.h>
#include <linux/fsnotify.h>

#include <qmrom.h>
#include <qmrom_spi.h>

#include "qm35.h"
#include "debug.h"
#include "hsspi_test.h"

#if IS_ENABLED(CONFIG_QM35_SPI_DEBUG_FW)
extern void debug_rom_code_init(struct debug *debug);
#endif

static const struct file_operations debug_enable_fops;
static const struct file_operations debug_log_level_fops;
static const struct file_operations debug_test_hsspi_sleep_fops;

static void *priv_from_file(const struct file *filp)
{
	return filp->f_path.dentry->d_inode->i_private;
}

static ssize_t debug_enable_write(struct file *filp, const char __user *buff,
				  size_t count, loff_t *off)
{
	struct debug *debug;
	u8 enabled;

	debug = priv_from_file(filp);

	if (kstrtou8_from_user(buff, count, 10, &enabled))
		return -EFAULT;

	if (debug->trace_ops)
		debug->trace_ops->enable_set(debug, enabled == 1 ? 1 : 0);
	else
		return -ENOSYS;

	return count;
}

static ssize_t debug_enable_read(struct file *filp, char __user *buff,
				 size_t count, loff_t *off)
{
	char enabled[2];
	struct debug *debug;

	debug = priv_from_file(filp);

	if (debug->trace_ops)
		enabled[0] = debug->trace_ops->enable_get(debug) + '0';
	else
		return -ENOSYS;

	enabled[1] = '\n';

	return simple_read_from_buffer(buff, count, off, enabled,
				       sizeof(enabled));
}

static ssize_t debug_log_level_write(struct file *filp, const char __user *buff,
				     size_t count, loff_t *off)
{
	u8 log_level = 0;
	struct log_module *log_module;

	log_module = priv_from_file(filp);
	if (kstrtou8_from_user(buff, count, 10, &log_level))
		return -EFAULT;

	if (log_module->debug->trace_ops)
		log_module->debug->trace_ops->level_set(log_module->debug,
							log_module, log_level);
	else
		return -ENOSYS;

	return count;
}

static ssize_t debug_log_level_read(struct file *filp, char __user *buff,
				    size_t count, loff_t *off)
{
	char log_level[2];
	struct log_module *log_module;

	log_module = priv_from_file(filp);

	if (log_module->debug->trace_ops)
		log_level[0] = log_module->debug->trace_ops->level_get(
				       log_module->debug, log_module) +
			       '0';
	else
		return -ENOSYS;

	log_level[1] = '\n';

	return simple_read_from_buffer(buff, count, off, log_level,
				       sizeof(log_level));
}

static ssize_t debug_test_hsspi_sleep_write(struct file *filp,
					    const char __user *buff,
					    size_t count, loff_t *off)
{
	int sleep_inter_frame_ms;

	if (kstrtoint_from_user(buff, count, 10, &sleep_inter_frame_ms))
		return -EFAULT;

	hsspi_test_set_inter_frame_ms(sleep_inter_frame_ms);
	return count;
}

static ssize_t debug_traces_read(struct file *filp, char __user *buff,
				 size_t count, loff_t *off)
{
	char *entry;
	rb_entry_size_t entry_size;
	uint16_t ret;
	struct debug *debug;

	debug = priv_from_file(filp);

	if (!debug->trace_ops)
		return -ENOSYS;

	entry_size = debug->trace_ops->trace_get_next_size(debug);
	if (!entry_size) {
		if (filp->f_flags & O_NONBLOCK)
			return 0;

		ret = wait_event_interruptible(
			debug->wq,
			(entry_size =
				 debug->trace_ops->trace_get_next_size(debug)));
		if (ret)
			return ret;
	}

	if (entry_size > count)
		return -EMSGSIZE;

	entry = debug->trace_ops->trace_get_next(debug, &entry_size);
	if (!entry)
		return 0;

	ret = copy_to_user(buff, entry, entry_size);

	kfree(entry);

	return ret ? -EFAULT : entry_size;
}

static __poll_t debug_traces_poll(struct file *filp,
				  struct poll_table_struct *wait)
{
	struct debug *debug;
	__poll_t mask = 0;

	debug = priv_from_file(filp);

	poll_wait(filp, &debug->wq, wait);

	if (debug->trace_ops && debug->trace_ops->trace_next_avail(debug))
		mask |= POLLIN;

	return mask;
}

static int debug_traces_open(struct inode *inodep, struct file *filep)
{
	struct debug *debug;

	debug = priv_from_file(filep);

	mutex_lock(&debug->pv_filp_lock);
	if (debug->pv_filp) {
		mutex_unlock(&debug->pv_filp_lock);
		return -EBUSY;
	}

	debug->pv_filp = filep;

	if (debug->trace_ops)
		debug->trace_ops->trace_reset(debug);

	mutex_unlock(&debug->pv_filp_lock);

	return 0;
}

static int debug_traces_release(struct inode *inodep, struct file *filep)
{
	struct debug *debug;

	debug = priv_from_file(filep);

	mutex_lock(&debug->pv_filp_lock);
	debug->pv_filp = NULL;
	mutex_unlock(&debug->pv_filp_lock);

	return 0;
}

static ssize_t debug_coredump_read(struct file *filep, char __user *buff,
				   size_t count, loff_t *off)
{
	struct qm35_ctx *qm35_hdl;
	struct debug *debug;
	char *cd;
	size_t cd_len = 0;

	debug = priv_from_file(filep);
	qm35_hdl = container_of(debug, struct qm35_ctx, debug);

	if (!debug->coredump_ops)
		return -ENOSYS;

	cd = debug->coredump_ops->coredump_get(debug, &cd_len);

	return simple_read_from_buffer(buff, count, off, cd, cd_len);
}

static ssize_t debug_coredump_write(struct file *filp, const char __user *buff,
				    size_t count, loff_t *off)
{
	struct debug *debug;
	u8 force;

	debug = priv_from_file(filp);

	if (kstrtou8_from_user(buff, count, 10, &force))
		return -EFAULT;

	if (debug->coredump_ops && force != 0)
		debug->coredump_ops->coredump_force(debug);
	else if (force == 0)
		pr_warn("qm35: write non null value to force coredump\n");
	else
		return -ENOSYS;

	return count;
}

static ssize_t debug_hw_reset_write(struct file *filp, const char __user *buff,
				    size_t count, loff_t *off)
{
	struct qm35_ctx *qm35_hdl;
	struct debug *debug;
	int ret;
	u8 reset;

	debug = priv_from_file(filp);
	qm35_hdl = container_of(debug, struct qm35_ctx, debug);

	if (kstrtou8_from_user(buff, count, 10, &reset))
		return -EFAULT;

	ret = -1;
	if (reset != 0) {
		pr_info("qm35: resetting chip...\n");
		ret = qm35_reset_sync(qm35_hdl);
	} else
		pr_warn("qm35: write non null value to force a hw reset\n");

	if (ret)
		return -ENOSYS;

	return count;
}

static const struct file_operations debug_enable_fops = {
	.owner = THIS_MODULE,
	.write = debug_enable_write,
	.read = debug_enable_read,
};

static const struct file_operations debug_log_level_fops = {
	.owner = THIS_MODULE,
	.write = debug_log_level_write,
	.read = debug_log_level_read
};

static const struct file_operations debug_test_hsspi_sleep_fops = {
	.owner = THIS_MODULE,
	.write = debug_test_hsspi_sleep_write
};

static const struct file_operations debug_traces_fops = {
	.owner = THIS_MODULE,
	.open = debug_traces_open,
	.release = debug_traces_release,
	.read = debug_traces_read,
	.poll = debug_traces_poll,
	.llseek = no_llseek,
};

static const struct file_operations debug_coredump_fops = {
	.owner = THIS_MODULE,
	.read = debug_coredump_read,
	.write = debug_coredump_write,
};

static const struct file_operations debug_hw_reset_fops = {
	.owner = THIS_MODULE,
	.write = debug_hw_reset_write,
};

int debug_create_module_entry(struct debug *debug,
			      struct log_module *log_module)
{
	struct dentry *dir;
	struct dentry *file;

	dir = debugfs_create_dir(log_module->name, debug->fw_dir);
	if (!dir) {
		pr_err("qm35: failed to create /sys/kernel/debug/uwb0/%s\n",
		       log_module->name);
		return -1;
	}

	file = debugfs_create_file("log_level", 0644, dir, log_module,
				   &debug_log_level_fops);
	if (!file) {
		pr_err("qm35: failed to create /sys/kernel/debug/uwb0/%s/log_level\n",
		       log_module->name);
		return -1;
	}

	pr_info("qm35 debug: created /sys/kernel/debug/uwb0/%s/log_level\n",
		log_module->name);

	return 0;
}

void debug_new_trace_available(struct debug *debug)
{
	if (debug->pv_filp)
		fsnotify_modify(debug->pv_filp);

	wake_up_interruptible(&debug->wq);
}

static int debug_devid_show(struct seq_file *s, void *unused)
{
	struct debug *debug = (struct debug *)s->private;
	uint16_t dev_id;
	int rc;

	if (debug->trace_ops && debug->trace_ops->get_dev_id) {
		rc = debug->trace_ops->get_dev_id(debug, &dev_id);
		if (rc < 0)
			return -EIO;
		seq_printf(s, "deca%04x\n", dev_id);
	}
	return 0;
}

static int debug_socid_show(struct seq_file *s, void *unused)
{
	struct debug *debug = (struct debug *)s->private;
	uint8_t soc_id[QM357XX_ROM_SOC_ID_LEN];
	int rc;

	if (debug->trace_ops && debug->trace_ops->get_soc_id) {
		rc = debug->trace_ops->get_soc_id(debug, soc_id);
		if (rc < 0)
			return -EIO;
		seq_printf(s, "%*phN\n", QM357XX_ROM_SOC_ID_LEN, soc_id);
	}
	return 0;
}

DEFINE_SHOW_ATTRIBUTE(debug_devid);
DEFINE_SHOW_ATTRIBUTE(debug_socid);

void debug_soc_info_available(struct debug *debug)
{
	struct dentry *file;

	file = debugfs_create_file("dev_id", 0444, debug->chip_dir, debug,
				   &debug_devid_fops);
	if (!file) {
		pr_err("qm35: failed to create /sys/kernel/debug/uwb0/fw/dev_id\n");
		goto unregister;
	}

	file = debugfs_create_file("soc_id", 0444, debug->chip_dir, debug,
				   &debug_socid_fops);
	if (!file) {
		pr_err("qm35: failed to create /sys/kernel/debug/uwb0/fw/soc_id\n");
		goto unregister;
	}

	return;

unregister:
	debugfs_remove_recursive(debug->chip_dir);
}

int debug_init_root(struct debug *debug, struct dentry *root)
{
	debug->root_dir = debugfs_create_dir("uwb0", root);
	if (!debug->root_dir) {
		pr_err("qm35: failed to create /sys/kernel/debug/uwb0\n");
		return -1;
	}

	return 0;
}

int debug_init(struct debug *debug)
{
	struct dentry *file;

	init_waitqueue_head(&debug->wq);
	mutex_init(&debug->pv_filp_lock);
	debug->pv_filp = NULL;

	debug->fw_dir = debugfs_create_dir("fw", debug->root_dir);
	if (!debug->fw_dir) {
		pr_err("qm35: failed to create /sys/kernel/debug/uwb0/fw\n");
		goto unregister;
	}

	debug->chip_dir = debugfs_create_dir("chip", debug->root_dir);
	if (!debug->chip_dir) {
		pr_err("qm35: failed to create /sys/kernel/debug/uwb0/chip\n");
		goto unregister;
	}

	file = debugfs_create_file("hw_reset", 0444, debug->chip_dir, debug,
				   &debug_hw_reset_fops);
	if (!file) {
		pr_err("qm35: failed to create /sys/kernel/debug/uwb0/chip/hw_reset\n");
		goto unregister;
	}

	file = debugfs_create_file("enable", 0644, debug->fw_dir, debug,
				   &debug_enable_fops);
	if (!file) {
		pr_err("qm35: failed to create /sys/kernel/debug/uwb0/fw/enable\n");
		goto unregister;
	}

	file = debugfs_create_file("traces", 0444, debug->fw_dir, debug,
				   &debug_traces_fops);
	if (!file) {
		pr_err("qm35: failed to create /sys/kernel/debug/uwb0/fw/traces\n");
		goto unregister;
	}

	file = debugfs_create_file("coredump", 0444, debug->fw_dir, debug,
				   &debug_coredump_fops);
	if (!file) {
		pr_err("qm35: failed to create /sys/kernel/debug/uwb0/fw/coredump\n");
		goto unregister;
	}

	file = debugfs_create_file("test_sleep_hsspi_ms", 0200, debug->fw_dir,
				   debug, &debug_test_hsspi_sleep_fops);
	if (!file) {
		pr_err("qm35: failed to create /sys/kernel/debug/uwb0/fw/test_sleep_hsspi\n");
		goto unregister;
	}

#if IS_ENABLED(CONFIG_QM35_SPI_DEBUG_FW)
	debug_rom_code_init(debug);
#endif

	return 0;

unregister:
	debug_deinit(debug);
	return -1;
}

void debug_deinit(struct debug *debug)
{
	wake_up_interruptible(&debug->wq);
	debugfs_remove_recursive(debug->root_dir);
}