1
2
3
4
5
6
7
8
9
10
11
12#define KMSG_COMPONENT "appldata"
13#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14
15#include <linux/module.h>
16#include <linux/sched/stat.h>
17#include <linux/init.h>
18#include <linux/slab.h>
19#include <linux/errno.h>
20#include <linux/interrupt.h>
21#include <linux/proc_fs.h>
22#include <linux/mm.h>
23#include <linux/swap.h>
24#include <linux/pagemap.h>
25#include <linux/sysctl.h>
26#include <linux/notifier.h>
27#include <linux/cpu.h>
28#include <linux/workqueue.h>
29#include <linux/suspend.h>
30#include <linux/platform_device.h>
31#include <asm/appldata.h>
32#include <asm/vtimer.h>
33#include <linux/uaccess.h>
34#include <asm/io.h>
35#include <asm/smp.h>
36
37#include "appldata.h"
38
39
40#define APPLDATA_CPU_INTERVAL 10000
41
42
43
44#define TOD_MICRO 0x01000
45
46
47static struct platform_device *appldata_pdev;
48
49
50
51
52static const char appldata_proc_name[APPLDATA_PROC_NAME_LENGTH] = "appldata";
53static int appldata_timer_handler(struct ctl_table *ctl, int write,
54 void *buffer, size_t *lenp, loff_t *ppos);
55static int appldata_interval_handler(struct ctl_table *ctl, int write,
56 void *buffer, size_t *lenp, loff_t *ppos);
57
58static struct ctl_table_header *appldata_sysctl_header;
59static struct ctl_table appldata_table[] = {
60 {
61 .procname = "timer",
62 .mode = S_IRUGO | S_IWUSR,
63 .proc_handler = appldata_timer_handler,
64 },
65 {
66 .procname = "interval",
67 .mode = S_IRUGO | S_IWUSR,
68 .proc_handler = appldata_interval_handler,
69 },
70 { },
71};
72
73static struct ctl_table appldata_dir_table[] = {
74 {
75 .procname = appldata_proc_name,
76 .maxlen = 0,
77 .mode = S_IRUGO | S_IXUGO,
78 .child = appldata_table,
79 },
80 { },
81};
82
83
84
85
86static struct vtimer_list appldata_timer;
87
88static DEFINE_SPINLOCK(appldata_timer_lock);
89static int appldata_interval = APPLDATA_CPU_INTERVAL;
90static int appldata_timer_active;
91static int appldata_timer_suspended = 0;
92
93
94
95
96static struct workqueue_struct *appldata_wq;
97static void appldata_work_fn(struct work_struct *work);
98static DECLARE_WORK(appldata_work, appldata_work_fn);
99
100
101
102
103
104static DEFINE_MUTEX(appldata_ops_mutex);
105static LIST_HEAD(appldata_ops_list);
106
107
108
109
110
111
112
113
114static void appldata_timer_function(unsigned long data)
115{
116 queue_work(appldata_wq, (struct work_struct *) data);
117}
118
119
120
121
122
123
124static void appldata_work_fn(struct work_struct *work)
125{
126 struct list_head *lh;
127 struct appldata_ops *ops;
128
129 mutex_lock(&appldata_ops_mutex);
130 list_for_each(lh, &appldata_ops_list) {
131 ops = list_entry(lh, struct appldata_ops, list);
132 if (ops->active == 1) {
133 ops->callback(ops->data);
134 }
135 }
136 mutex_unlock(&appldata_ops_mutex);
137}
138
139static struct appldata_product_id appldata_id = {
140 .prod_nr = {0xD3, 0xC9, 0xD5, 0xE4,
141 0xE7, 0xD2, 0xD9},
142 .prod_fn = 0xD5D3,
143 .version_nr = 0xF2F6,
144 .release_nr = 0xF0F1,
145};
146
147
148
149
150
151
152int appldata_diag(char record_nr, u16 function, unsigned long buffer,
153 u16 length, char *mod_lvl)
154{
155 struct appldata_parameter_list *parm_list;
156 struct appldata_product_id *id;
157 int rc;
158
159 parm_list = kmalloc(sizeof(*parm_list), GFP_KERNEL);
160 id = kmemdup(&appldata_id, sizeof(appldata_id), GFP_KERNEL);
161 rc = -ENOMEM;
162 if (parm_list && id) {
163 id->record_nr = record_nr;
164 id->mod_lvl = (mod_lvl[0]) << 8 | mod_lvl[1];
165 rc = appldata_asm(parm_list, id, function,
166 (void *) buffer, length);
167 }
168 kfree(id);
169 kfree(parm_list);
170 return rc;
171}
172
173
174
175
176
177#define APPLDATA_ADD_TIMER 0
178#define APPLDATA_DEL_TIMER 1
179#define APPLDATA_MOD_TIMER 2
180
181
182
183
184
185
186
187static void __appldata_vtimer_setup(int cmd)
188{
189 u64 timer_interval = (u64) appldata_interval * 1000 * TOD_MICRO;
190
191 switch (cmd) {
192 case APPLDATA_ADD_TIMER:
193 if (appldata_timer_active)
194 break;
195 appldata_timer.expires = timer_interval;
196 add_virt_timer_periodic(&appldata_timer);
197 appldata_timer_active = 1;
198 break;
199 case APPLDATA_DEL_TIMER:
200 del_virt_timer(&appldata_timer);
201 if (!appldata_timer_active)
202 break;
203 appldata_timer_active = 0;
204 break;
205 case APPLDATA_MOD_TIMER:
206 if (!appldata_timer_active)
207 break;
208 mod_virt_timer_periodic(&appldata_timer, timer_interval);
209 }
210}
211
212
213
214
215
216
217static int
218appldata_timer_handler(struct ctl_table *ctl, int write,
219 void *buffer, size_t *lenp, loff_t *ppos)
220{
221 int timer_active = appldata_timer_active;
222 int rc;
223 struct ctl_table ctl_entry = {
224 .procname = ctl->procname,
225 .data = &timer_active,
226 .maxlen = sizeof(int),
227 .extra1 = SYSCTL_ZERO,
228 .extra2 = SYSCTL_ONE,
229 };
230
231 rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
232 if (rc < 0 || !write)
233 return rc;
234
235 spin_lock(&appldata_timer_lock);
236 if (timer_active)
237 __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
238 else
239 __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
240 spin_unlock(&appldata_timer_lock);
241 return 0;
242}
243
244
245
246
247
248
249
250static int
251appldata_interval_handler(struct ctl_table *ctl, int write,
252 void *buffer, size_t *lenp, loff_t *ppos)
253{
254 int interval = appldata_interval;
255 int rc;
256 struct ctl_table ctl_entry = {
257 .procname = ctl->procname,
258 .data = &interval,
259 .maxlen = sizeof(int),
260 .extra1 = SYSCTL_ONE,
261 };
262
263 rc = proc_dointvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
264 if (rc < 0 || !write)
265 return rc;
266
267 spin_lock(&appldata_timer_lock);
268 appldata_interval = interval;
269 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
270 spin_unlock(&appldata_timer_lock);
271 return 0;
272}
273
274
275
276
277
278
279
280static int
281appldata_generic_handler(struct ctl_table *ctl, int write,
282 void *buffer, size_t *lenp, loff_t *ppos)
283{
284 struct appldata_ops *ops = NULL, *tmp_ops;
285 struct list_head *lh;
286 int rc, found;
287 int active;
288 struct ctl_table ctl_entry = {
289 .data = &active,
290 .maxlen = sizeof(int),
291 .extra1 = SYSCTL_ZERO,
292 .extra2 = SYSCTL_ONE,
293 };
294
295 found = 0;
296 mutex_lock(&appldata_ops_mutex);
297 list_for_each(lh, &appldata_ops_list) {
298 tmp_ops = list_entry(lh, struct appldata_ops, list);
299 if (&tmp_ops->ctl_table[2] == ctl) {
300 found = 1;
301 }
302 }
303 if (!found) {
304 mutex_unlock(&appldata_ops_mutex);
305 return -ENODEV;
306 }
307 ops = ctl->data;
308 if (!try_module_get(ops->owner)) {
309 mutex_unlock(&appldata_ops_mutex);
310 return -ENODEV;
311 }
312 mutex_unlock(&appldata_ops_mutex);
313
314 active = ops->active;
315 rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
316 if (rc < 0 || !write) {
317 module_put(ops->owner);
318 return rc;
319 }
320
321 mutex_lock(&appldata_ops_mutex);
322 if (active && (ops->active == 0)) {
323
324 if (!try_module_get(ops->owner)) {
325 mutex_unlock(&appldata_ops_mutex);
326 module_put(ops->owner);
327 return -ENODEV;
328 }
329 ops->callback(ops->data);
330 rc = appldata_diag(ops->record_nr,
331 APPLDATA_START_INTERVAL_REC,
332 (unsigned long) ops->data, ops->size,
333 ops->mod_lvl);
334 if (rc != 0) {
335 pr_err("Starting the data collection for %s "
336 "failed with rc=%d\n", ops->name, rc);
337 module_put(ops->owner);
338 } else
339 ops->active = 1;
340 } else if (!active && (ops->active == 1)) {
341 ops->active = 0;
342 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
343 (unsigned long) ops->data, ops->size,
344 ops->mod_lvl);
345 if (rc != 0)
346 pr_err("Stopping the data collection for %s "
347 "failed with rc=%d\n", ops->name, rc);
348 module_put(ops->owner);
349 }
350 mutex_unlock(&appldata_ops_mutex);
351 module_put(ops->owner);
352 return 0;
353}
354
355
356
357
358
359
360
361
362
363
364int appldata_register_ops(struct appldata_ops *ops)
365{
366 if (ops->size > APPLDATA_MAX_REC_SIZE)
367 return -EINVAL;
368
369 ops->ctl_table = kcalloc(4, sizeof(struct ctl_table), GFP_KERNEL);
370 if (!ops->ctl_table)
371 return -ENOMEM;
372
373 mutex_lock(&appldata_ops_mutex);
374 list_add(&ops->list, &appldata_ops_list);
375 mutex_unlock(&appldata_ops_mutex);
376
377 ops->ctl_table[0].procname = appldata_proc_name;
378 ops->ctl_table[0].maxlen = 0;
379 ops->ctl_table[0].mode = S_IRUGO | S_IXUGO;
380 ops->ctl_table[0].child = &ops->ctl_table[2];
381
382 ops->ctl_table[2].procname = ops->name;
383 ops->ctl_table[2].mode = S_IRUGO | S_IWUSR;
384 ops->ctl_table[2].proc_handler = appldata_generic_handler;
385 ops->ctl_table[2].data = ops;
386
387 ops->sysctl_header = register_sysctl_table(ops->ctl_table);
388 if (!ops->sysctl_header)
389 goto out;
390 return 0;
391out:
392 mutex_lock(&appldata_ops_mutex);
393 list_del(&ops->list);
394 mutex_unlock(&appldata_ops_mutex);
395 kfree(ops->ctl_table);
396 return -ENOMEM;
397}
398
399
400
401
402
403
404void appldata_unregister_ops(struct appldata_ops *ops)
405{
406 mutex_lock(&appldata_ops_mutex);
407 list_del(&ops->list);
408 mutex_unlock(&appldata_ops_mutex);
409 unregister_sysctl_table(ops->sysctl_header);
410 kfree(ops->ctl_table);
411}
412
413
414
415
416static int appldata_freeze(struct device *dev)
417{
418 struct appldata_ops *ops;
419 int rc;
420 struct list_head *lh;
421
422 spin_lock(&appldata_timer_lock);
423 if (appldata_timer_active) {
424 __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
425 appldata_timer_suspended = 1;
426 }
427 spin_unlock(&appldata_timer_lock);
428
429 mutex_lock(&appldata_ops_mutex);
430 list_for_each(lh, &appldata_ops_list) {
431 ops = list_entry(lh, struct appldata_ops, list);
432 if (ops->active == 1) {
433 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
434 (unsigned long) ops->data, ops->size,
435 ops->mod_lvl);
436 if (rc != 0)
437 pr_err("Stopping the data collection for %s "
438 "failed with rc=%d\n", ops->name, rc);
439 }
440 }
441 mutex_unlock(&appldata_ops_mutex);
442 return 0;
443}
444
445static int appldata_restore(struct device *dev)
446{
447 struct appldata_ops *ops;
448 int rc;
449 struct list_head *lh;
450
451 spin_lock(&appldata_timer_lock);
452 if (appldata_timer_suspended) {
453 __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
454 appldata_timer_suspended = 0;
455 }
456 spin_unlock(&appldata_timer_lock);
457
458 mutex_lock(&appldata_ops_mutex);
459 list_for_each(lh, &appldata_ops_list) {
460 ops = list_entry(lh, struct appldata_ops, list);
461 if (ops->active == 1) {
462 ops->callback(ops->data);
463 rc = appldata_diag(ops->record_nr,
464 APPLDATA_START_INTERVAL_REC,
465 (unsigned long) ops->data, ops->size,
466 ops->mod_lvl);
467 if (rc != 0) {
468 pr_err("Starting the data collection for %s "
469 "failed with rc=%d\n", ops->name, rc);
470 }
471 }
472 }
473 mutex_unlock(&appldata_ops_mutex);
474 return 0;
475}
476
477static int appldata_thaw(struct device *dev)
478{
479 return appldata_restore(dev);
480}
481
482static const struct dev_pm_ops appldata_pm_ops = {
483 .freeze = appldata_freeze,
484 .thaw = appldata_thaw,
485 .restore = appldata_restore,
486};
487
488static struct platform_driver appldata_pdrv = {
489 .driver = {
490 .name = "appldata",
491 .pm = &appldata_pm_ops,
492 },
493};
494
495
496
497
498
499
500
501
502
503
504static int __init appldata_init(void)
505{
506 int rc;
507
508 init_virt_timer(&appldata_timer);
509 appldata_timer.function = appldata_timer_function;
510 appldata_timer.data = (unsigned long) &appldata_work;
511
512 rc = platform_driver_register(&appldata_pdrv);
513 if (rc)
514 return rc;
515
516 appldata_pdev = platform_device_register_simple("appldata", -1, NULL,
517 0);
518 if (IS_ERR(appldata_pdev)) {
519 rc = PTR_ERR(appldata_pdev);
520 goto out_driver;
521 }
522 appldata_wq = alloc_ordered_workqueue("appldata", 0);
523 if (!appldata_wq) {
524 rc = -ENOMEM;
525 goto out_device;
526 }
527
528 appldata_sysctl_header = register_sysctl_table(appldata_dir_table);
529 return 0;
530
531out_device:
532 platform_device_unregister(appldata_pdev);
533out_driver:
534 platform_driver_unregister(&appldata_pdrv);
535 return rc;
536}
537
538__initcall(appldata_init);
539
540
541
542EXPORT_SYMBOL_GPL(appldata_register_ops);
543EXPORT_SYMBOL_GPL(appldata_unregister_ops);
544EXPORT_SYMBOL_GPL(appldata_diag);
545
546#ifdef CONFIG_SWAP
547EXPORT_SYMBOL_GPL(si_swapinfo);
548#endif
549EXPORT_SYMBOL_GPL(nr_threads);
550EXPORT_SYMBOL_GPL(nr_running);
551EXPORT_SYMBOL_GPL(nr_iowait);
552