blob: 59c7323f98d459b3dabd5571a98f7d65e0d7249f [file] [log] [blame]
Mauro Carvalho Chehab503efe52013-03-10 09:04:44 -03001/***********************************************************************
2 *
3 * Copyright(c) 2013 Mauro Carvalho Chehab <mchehab@redhat.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 ***********************************************************************/
19
20#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22#include <linux/module.h>
23#include <linux/slab.h>
24#include <linux/init.h>
25#include <linux/debugfs.h>
26#include <linux/spinlock.h>
27#include <linux/usb.h>
28
29#include "dmxdev.h"
30#include "dvbdev.h"
31#include "dvb_demux.h"
32#include "dvb_frontend.h"
33
34#include "smscoreapi.h"
35
36#include "smsdvb.h"
37
38static struct dentry *smsdvb_debugfs_usb_root;
39
40struct smsdvb_debugfs {
41 struct kref refcount;
42 spinlock_t lock;
43
44 char stats_data[PAGE_SIZE];
45 unsigned stats_count;
46 bool stats_was_read;
47
48 wait_queue_head_t stats_queue;
49};
50
51void smsdvb_print_dvb_stats(struct smsdvb_debugfs *debug_data,
52 struct SMSHOSTLIB_STATISTICS_ST *p)
53{
54 int n = 0;
55 char *buf;
56
57 spin_lock(&debug_data->lock);
58 if (debug_data->stats_count) {
59 spin_unlock(&debug_data->lock);
60 return;
61 }
62
63 buf = debug_data->stats_data;
64
65 n += snprintf(&buf[n], PAGE_SIZE - n,
66 "IsRfLocked = %d\n", p->IsRfLocked);
67 n += snprintf(&buf[n], PAGE_SIZE - n,
68 "IsDemodLocked = %d\n", p->IsDemodLocked);
69 n += snprintf(&buf[n], PAGE_SIZE - n,
70 "IsExternalLNAOn = %d\n", p->IsExternalLNAOn);
71 n += snprintf(&buf[n], PAGE_SIZE - n,
72 "SNR = %d\n", p->SNR);
73 n += snprintf(&buf[n], PAGE_SIZE - n,
74 "BER = %d\n", p->BER);
75 n += snprintf(&buf[n], PAGE_SIZE - n,
76 "FIB_CRC = %d\n", p->FIB_CRC);
77 n += snprintf(&buf[n], PAGE_SIZE - n,
78 "TS_PER = %d\n", p->TS_PER);
79 n += snprintf(&buf[n], PAGE_SIZE - n,
80 "MFER = %d\n", p->MFER);
81 n += snprintf(&buf[n], PAGE_SIZE - n,
82 "RSSI = %d\n", p->RSSI);
83 n += snprintf(&buf[n], PAGE_SIZE - n,
84 "InBandPwr = %d\n", p->InBandPwr);
85 n += snprintf(&buf[n], PAGE_SIZE - n,
86 "CarrierOffset = %d\n", p->CarrierOffset);
87 n += snprintf(&buf[n], PAGE_SIZE - n,
88 "ModemState = %d\n", p->ModemState);
89 n += snprintf(&buf[n], PAGE_SIZE - n,
90 "Frequency = %d\n", p->Frequency);
91 n += snprintf(&buf[n], PAGE_SIZE - n,
92 "Bandwidth = %d\n", p->Bandwidth);
93 n += snprintf(&buf[n], PAGE_SIZE - n,
94 "TransmissionMode = %d\n", p->TransmissionMode);
95 n += snprintf(&buf[n], PAGE_SIZE - n,
96 "ModemState = %d\n", p->ModemState);
97 n += snprintf(&buf[n], PAGE_SIZE - n,
98 "GuardInterval = %d\n", p->GuardInterval);
99 n += snprintf(&buf[n], PAGE_SIZE - n,
100 "CodeRate = %d\n", p->CodeRate);
101 n += snprintf(&buf[n], PAGE_SIZE - n,
102 "LPCodeRate = %d\n", p->LPCodeRate);
103 n += snprintf(&buf[n], PAGE_SIZE - n,
104 "Hierarchy = %d\n", p->Hierarchy);
105 n += snprintf(&buf[n], PAGE_SIZE - n,
106 "Constellation = %d\n", p->Constellation);
107 n += snprintf(&buf[n], PAGE_SIZE - n,
108 "BurstSize = %d\n", p->BurstSize);
109 n += snprintf(&buf[n], PAGE_SIZE - n,
110 "BurstDuration = %d\n", p->BurstDuration);
111 n += snprintf(&buf[n], PAGE_SIZE - n,
112 "BurstCycleTime = %d\n", p->BurstCycleTime);
113 n += snprintf(&buf[n], PAGE_SIZE - n,
114 "CalculatedBurstCycleTime = %d\n",
115 p->CalculatedBurstCycleTime);
116 n += snprintf(&buf[n], PAGE_SIZE - n,
117 "NumOfRows = %d\n", p->NumOfRows);
118 n += snprintf(&buf[n], PAGE_SIZE - n,
119 "NumOfPaddCols = %d\n", p->NumOfPaddCols);
120 n += snprintf(&buf[n], PAGE_SIZE - n,
121 "NumOfPunctCols = %d\n", p->NumOfPunctCols);
122 n += snprintf(&buf[n], PAGE_SIZE - n,
123 "ErrorTSPackets = %d\n", p->ErrorTSPackets);
124 n += snprintf(&buf[n], PAGE_SIZE - n,
125 "TotalTSPackets = %d\n", p->TotalTSPackets);
126 n += snprintf(&buf[n], PAGE_SIZE - n,
127 "NumOfValidMpeTlbs = %d\n", p->NumOfValidMpeTlbs);
128 n += snprintf(&buf[n], PAGE_SIZE - n,
129 "NumOfInvalidMpeTlbs = %d\n", p->NumOfInvalidMpeTlbs);
130 n += snprintf(&buf[n], PAGE_SIZE - n,
131 "NumOfCorrectedMpeTlbs = %d\n", p->NumOfCorrectedMpeTlbs);
132 n += snprintf(&buf[n], PAGE_SIZE - n,
133 "BERErrorCount = %d\n", p->BERErrorCount);
134 n += snprintf(&buf[n], PAGE_SIZE - n,
135 "BERBitCount = %d\n", p->BERBitCount);
136 n += snprintf(&buf[n], PAGE_SIZE - n,
137 "SmsToHostTxErrors = %d\n", p->SmsToHostTxErrors);
138 n += snprintf(&buf[n], PAGE_SIZE - n,
139 "PreBER = %d\n", p->PreBER);
140 n += snprintf(&buf[n], PAGE_SIZE - n,
141 "CellId = %d\n", p->CellId);
142 n += snprintf(&buf[n], PAGE_SIZE - n,
143 "DvbhSrvIndHP = %d\n", p->DvbhSrvIndHP);
144 n += snprintf(&buf[n], PAGE_SIZE - n,
145 "DvbhSrvIndLP = %d\n", p->DvbhSrvIndLP);
146 n += snprintf(&buf[n], PAGE_SIZE - n,
147 "NumMPEReceived = %d\n", p->NumMPEReceived);
148
149 debug_data->stats_count = n;
150 spin_unlock(&debug_data->lock);
151 wake_up(&debug_data->stats_queue);
152}
153
154void smsdvb_print_isdb_stats(struct smsdvb_debugfs *debug_data,
155 struct SMSHOSTLIB_STATISTICS_ISDBT_ST *p)
156{
157 int i, n = 0;
158 char *buf;
159
160 spin_lock(&debug_data->lock);
161 if (debug_data->stats_count) {
162 spin_unlock(&debug_data->lock);
163 return;
164 }
165
166 buf = debug_data->stats_data;
167
168 n += snprintf(&buf[n], PAGE_SIZE - n,
Mauro Carvalho Chehab4cce1f42013-03-16 14:32:25 -0300169 "StatisticsType = %d\t", p->StatisticsType);
170 n += snprintf(&buf[n], PAGE_SIZE - n,
171 "FullSize = %d\n", p->FullSize);
172
173 n += snprintf(&buf[n], PAGE_SIZE - n,
Mauro Carvalho Chehab503efe52013-03-10 09:04:44 -0300174 "IsRfLocked = %d\t\t", p->IsRfLocked);
175 n += snprintf(&buf[n], PAGE_SIZE - n,
176 "IsDemodLocked = %d\t", p->IsDemodLocked);
177 n += snprintf(&buf[n], PAGE_SIZE - n,
178 "IsExternalLNAOn = %d\n", p->IsExternalLNAOn);
179 n += snprintf(&buf[n], PAGE_SIZE - n,
180 "SNR = %d dB\t\t", p->SNR);
181 n += snprintf(&buf[n], PAGE_SIZE - n,
182 "RSSI = %d dBm\t\t", p->RSSI);
183 n += snprintf(&buf[n], PAGE_SIZE - n,
184 "InBandPwr = %d dBm\n", p->InBandPwr);
185 n += snprintf(&buf[n], PAGE_SIZE - n,
186 "CarrierOffset = %d\t", p->CarrierOffset);
187 n += snprintf(&buf[n], PAGE_SIZE - n,
188 "Bandwidth = %d\t\t", p->Bandwidth);
189 n += snprintf(&buf[n], PAGE_SIZE - n,
190 "Frequency = %d Hz\n", p->Frequency);
191 n += snprintf(&buf[n], PAGE_SIZE - n,
192 "TransmissionMode = %d\t", p->TransmissionMode);
193 n += snprintf(&buf[n], PAGE_SIZE - n,
194 "ModemState = %d\t\t", p->ModemState);
195 n += snprintf(&buf[n], PAGE_SIZE - n,
196 "GuardInterval = %d\n", p->GuardInterval);
197 n += snprintf(&buf[n], PAGE_SIZE - n,
198 "SystemType = %d\t\t", p->SystemType);
199 n += snprintf(&buf[n], PAGE_SIZE - n,
200 "PartialReception = %d\t", p->PartialReception);
201 n += snprintf(&buf[n], PAGE_SIZE - n,
202 "NumOfLayers = %d\n", p->NumOfLayers);
203 n += snprintf(&buf[n], PAGE_SIZE - n,
204 "SmsToHostTxErrors = %d\n", p->SmsToHostTxErrors);
205
206 for (i = 0; i < 3; i++) {
207 if (p->LayerInfo[i].NumberOfSegments < 1 ||
208 p->LayerInfo[i].NumberOfSegments > 13)
209 continue;
210
211 n += snprintf(&buf[n], PAGE_SIZE - n, "\nLayer %d\n", i);
212 n += snprintf(&buf[n], PAGE_SIZE - n, "\tCodeRate = %d\t",
213 p->LayerInfo[i].CodeRate);
214 n += snprintf(&buf[n], PAGE_SIZE - n, "Constellation = %d\n",
215 p->LayerInfo[i].Constellation);
216 n += snprintf(&buf[n], PAGE_SIZE - n, "\tBER = %-5d\t",
217 p->LayerInfo[i].BER);
218 n += snprintf(&buf[n], PAGE_SIZE - n, "\tBERErrorCount = %-5d\t",
219 p->LayerInfo[i].BERErrorCount);
220 n += snprintf(&buf[n], PAGE_SIZE - n, "BERBitCount = %-5d\n",
221 p->LayerInfo[i].BERBitCount);
222 n += snprintf(&buf[n], PAGE_SIZE - n, "\tPreBER = %-5d\t",
223 p->LayerInfo[i].PreBER);
224 n += snprintf(&buf[n], PAGE_SIZE - n, "\tTS_PER = %-5d\n",
225 p->LayerInfo[i].TS_PER);
226 n += snprintf(&buf[n], PAGE_SIZE - n, "\tErrorTSPackets = %-5d\t",
227 p->LayerInfo[i].ErrorTSPackets);
228 n += snprintf(&buf[n], PAGE_SIZE - n, "TotalTSPackets = %-5d\t",
229 p->LayerInfo[i].TotalTSPackets);
230 n += snprintf(&buf[n], PAGE_SIZE - n, "TILdepthI = %d\n",
231 p->LayerInfo[i].TILdepthI);
232 n += snprintf(&buf[n], PAGE_SIZE - n,
233 "\tNumberOfSegments = %d\t",
234 p->LayerInfo[i].NumberOfSegments);
235 n += snprintf(&buf[n], PAGE_SIZE - n, "TMCCErrors = %d\n",
236 p->LayerInfo[i].TMCCErrors);
237 }
238
239 debug_data->stats_count = n;
240 spin_unlock(&debug_data->lock);
241 wake_up(&debug_data->stats_queue);
242}
243
244void smsdvb_print_isdb_stats_ex(struct smsdvb_debugfs *debug_data,
245 struct SMSHOSTLIB_STATISTICS_ISDBT_EX_ST *p)
246{
247 int i, n = 0;
248 char *buf;
249
250 spin_lock(&debug_data->lock);
251 if (debug_data->stats_count) {
252 spin_unlock(&debug_data->lock);
253 return;
254 }
255
256 buf = debug_data->stats_data;
257
258 n += snprintf(&buf[n], PAGE_SIZE - n,
Mauro Carvalho Chehab4cce1f42013-03-16 14:32:25 -0300259 "StatisticsType = %d\t", p->StatisticsType);
260 n += snprintf(&buf[n], PAGE_SIZE - n,
261 "FullSize = %d\n", p->FullSize);
262
263 n += snprintf(&buf[n], PAGE_SIZE - n,
Mauro Carvalho Chehab503efe52013-03-10 09:04:44 -0300264 "IsRfLocked = %d\t\t", p->IsRfLocked);
265 n += snprintf(&buf[n], PAGE_SIZE - n,
266 "IsDemodLocked = %d\t", p->IsDemodLocked);
267 n += snprintf(&buf[n], PAGE_SIZE - n,
268 "IsExternalLNAOn = %d\n", p->IsExternalLNAOn);
269 n += snprintf(&buf[n], PAGE_SIZE - n,
270 "SNR = %d dB\t\t", p->SNR);
271 n += snprintf(&buf[n], PAGE_SIZE - n,
272 "RSSI = %d dBm\t\t", p->RSSI);
273 n += snprintf(&buf[n], PAGE_SIZE - n,
274 "InBandPwr = %d dBm\n", p->InBandPwr);
275 n += snprintf(&buf[n], PAGE_SIZE - n,
276 "CarrierOffset = %d\t", p->CarrierOffset);
277 n += snprintf(&buf[n], PAGE_SIZE - n,
278 "Bandwidth = %d\t\t", p->Bandwidth);
279 n += snprintf(&buf[n], PAGE_SIZE - n,
280 "Frequency = %d Hz\n", p->Frequency);
281 n += snprintf(&buf[n], PAGE_SIZE - n,
282 "TransmissionMode = %d\t", p->TransmissionMode);
283 n += snprintf(&buf[n], PAGE_SIZE - n,
284 "ModemState = %d\t\t", p->ModemState);
285 n += snprintf(&buf[n], PAGE_SIZE - n,
286 "GuardInterval = %d\n", p->GuardInterval);
287 n += snprintf(&buf[n], PAGE_SIZE - n,
288 "SystemType = %d\t\t", p->SystemType);
289 n += snprintf(&buf[n], PAGE_SIZE - n,
290 "PartialReception = %d\t", p->PartialReception);
291 n += snprintf(&buf[n], PAGE_SIZE - n,
292 "NumOfLayers = %d\n", p->NumOfLayers);
293 n += snprintf(&buf[n], PAGE_SIZE - n, "SegmentNumber = %d\t",
294 p->SegmentNumber);
295 n += snprintf(&buf[n], PAGE_SIZE - n, "TuneBW = %d\n",
296 p->TuneBW);
297
298 for (i = 0; i < 3; i++) {
299 if (p->LayerInfo[i].NumberOfSegments < 1 ||
300 p->LayerInfo[i].NumberOfSegments > 13)
301 continue;
302
303 n += snprintf(&buf[n], PAGE_SIZE - n, "\nLayer %d\n", i);
304 n += snprintf(&buf[n], PAGE_SIZE - n, "\tCodeRate = %d\t",
305 p->LayerInfo[i].CodeRate);
306 n += snprintf(&buf[n], PAGE_SIZE - n, "Constellation = %d\n",
307 p->LayerInfo[i].Constellation);
308 n += snprintf(&buf[n], PAGE_SIZE - n, "\tBER = %-5d\t",
309 p->LayerInfo[i].BER);
310 n += snprintf(&buf[n], PAGE_SIZE - n, "\tBERErrorCount = %-5d\t",
311 p->LayerInfo[i].BERErrorCount);
312 n += snprintf(&buf[n], PAGE_SIZE - n, "BERBitCount = %-5d\n",
313 p->LayerInfo[i].BERBitCount);
314 n += snprintf(&buf[n], PAGE_SIZE - n, "\tPreBER = %-5d\t",
315 p->LayerInfo[i].PreBER);
316 n += snprintf(&buf[n], PAGE_SIZE - n, "\tTS_PER = %-5d\n",
317 p->LayerInfo[i].TS_PER);
318 n += snprintf(&buf[n], PAGE_SIZE - n, "\tErrorTSPackets = %-5d\t",
319 p->LayerInfo[i].ErrorTSPackets);
320 n += snprintf(&buf[n], PAGE_SIZE - n, "TotalTSPackets = %-5d\t",
321 p->LayerInfo[i].TotalTSPackets);
322 n += snprintf(&buf[n], PAGE_SIZE - n, "TILdepthI = %d\n",
323 p->LayerInfo[i].TILdepthI);
324 n += snprintf(&buf[n], PAGE_SIZE - n,
325 "\tNumberOfSegments = %d\t",
326 p->LayerInfo[i].NumberOfSegments);
327 n += snprintf(&buf[n], PAGE_SIZE - n, "TMCCErrors = %d\n",
328 p->LayerInfo[i].TMCCErrors);
329 }
330
331
332 debug_data->stats_count = n;
333 spin_unlock(&debug_data->lock);
334
335 wake_up(&debug_data->stats_queue);
336}
337
338static int smsdvb_stats_open(struct inode *inode, struct file *file)
339{
340 struct smsdvb_client_t *client = inode->i_private;
341 struct smsdvb_debugfs *debug_data = client->debug_data;
342
343 kref_get(&debug_data->refcount);
344
345 spin_lock(&debug_data->lock);
346 debug_data->stats_count = 0;
347 debug_data->stats_was_read = false;
348 spin_unlock(&debug_data->lock);
349
350 file->private_data = debug_data;
351
352 return 0;
353}
354
355static int smsdvb_stats_wait_read(struct smsdvb_debugfs *debug_data)
356{
357 int rc = 1;
358
359 spin_lock(&debug_data->lock);
360
361 if (debug_data->stats_was_read)
362 goto exit;
363
364 rc = debug_data->stats_count;
365
366exit:
367 spin_unlock(&debug_data->lock);
368 return rc;
369}
370
371static ssize_t smsdvb_stats_read(struct file *file, char __user *user_buf,
372 size_t nbytes, loff_t *ppos)
373{
374 int rc = 0;
375 struct smsdvb_debugfs *debug_data = file->private_data;
376
377 rc = wait_event_interruptible(debug_data->stats_queue,
378 smsdvb_stats_wait_read(debug_data));
379 if (rc < 0)
380 return rc;
381
382 rc = simple_read_from_buffer(user_buf, nbytes, ppos,
383 debug_data->stats_data,
384 debug_data->stats_count);
385 spin_lock(&debug_data->lock);
386 debug_data->stats_was_read = true;
387 spin_unlock(&debug_data->lock);
388
389 return rc;
390}
391
392static void smsdvb_debugfs_data_release(struct kref *ref)
393{
394 struct smsdvb_debugfs *debug_data;
395
396 debug_data = container_of(ref, struct smsdvb_debugfs, refcount);
397 kfree(debug_data);
398}
399
400static int smsdvb_stats_release(struct inode *inode, struct file *file)
401{
402 struct smsdvb_debugfs *debug_data = file->private_data;
403
404 spin_lock(&debug_data->lock);
405 debug_data->stats_was_read = true;
406 spin_unlock(&debug_data->lock);
407 wake_up_interruptible_sync(&debug_data->stats_queue);
408
409 kref_put(&debug_data->refcount, smsdvb_debugfs_data_release);
410 file->private_data = NULL;
411
412 return 0;
413}
414
415static const struct file_operations debugfs_stats_ops = {
416 .open = smsdvb_stats_open,
417 .read = smsdvb_stats_read,
418 .release = smsdvb_stats_release,
419 .llseek = generic_file_llseek,
420};
421
422/*
423 * Functions used by smsdvb, in order to create the interfaces
424 */
425
426int smsdvb_debugfs_create(struct smsdvb_client_t *client)
427{
428 struct smscore_device_t *coredev = client->coredev;
429 struct dentry *d;
430 struct smsdvb_debugfs *debug_data;
431
432 if (!smsdvb_debugfs_usb_root || !coredev->is_usb_device)
433 return -ENODEV;
434
435 client->debugfs = debugfs_create_dir(coredev->devpath,
436 smsdvb_debugfs_usb_root);
437 if (IS_ERR_OR_NULL(client->debugfs)) {
438 pr_info("Unable to create debugfs %s directory.\n",
439 coredev->devpath);
440 return -ENODEV;
441 }
442
443 d = debugfs_create_file("stats", S_IRUGO | S_IWUSR, client->debugfs,
444 client, &debugfs_stats_ops);
445 if (!d) {
446 debugfs_remove(client->debugfs);
447 return -ENOMEM;
448 }
449
450 debug_data = kzalloc(sizeof(*client->debug_data), GFP_KERNEL);
451 if (!debug_data)
452 return -ENOMEM;
453
454 client->debug_data = debug_data;
455 client->prt_dvb_stats = smsdvb_print_dvb_stats;
456 client->prt_isdb_stats = smsdvb_print_isdb_stats;
457 client->prt_isdb_stats_ex = smsdvb_print_isdb_stats_ex;
458
459 init_waitqueue_head(&debug_data->stats_queue);
460 spin_lock_init(&debug_data->lock);
461 kref_init(&debug_data->refcount);
462
463 return 0;
464}
465
466void smsdvb_debugfs_release(struct smsdvb_client_t *client)
467{
468 if (!client->debugfs)
469 return;
470
471printk("%s\n", __func__);
472
473 client->prt_dvb_stats = NULL;
474 client->prt_isdb_stats = NULL;
475 client->prt_isdb_stats_ex = NULL;
476
477 debugfs_remove_recursive(client->debugfs);
478 kref_put(&client->debug_data->refcount, smsdvb_debugfs_data_release);
479
480 client->debug_data = NULL;
481 client->debugfs = NULL;
482}
483
484int smsdvb_debugfs_register(void)
485{
486 struct dentry *d;
487
488 /*
489 * FIXME: This was written to debug Siano USB devices. So, it creates
490 * the debugfs node under <debugfs>/usb.
491 * A similar logic would be needed for Siano sdio devices, but, in that
492 * case, usb_debug_root is not a good choice.
493 *
494 * Perhaps the right fix here would be to create another sysfs root
495 * node for sdio-based boards, but this may need some logic at sdio
496 * subsystem.
497 */
498 d = debugfs_create_dir("smsdvb", usb_debug_root);
499 if (IS_ERR_OR_NULL(d)) {
500 sms_err("Couldn't create sysfs node for smsdvb");
501 return PTR_ERR(d);
502 } else {
503 smsdvb_debugfs_usb_root = d;
504 }
505 return 0;
506}
507
508void smsdvb_debugfs_unregister(void)
509{
510 if (smsdvb_debugfs_usb_root)
511 debugfs_remove_recursive(smsdvb_debugfs_usb_root);
512 smsdvb_debugfs_usb_root = NULL;
513}