edb73efd53f7d86a8d65ca292717844a13a079c1
[cascardo/linux.git] / drivers / staging / greybus / svc_watchdog.c
1 /*
2  * SVC Greybus "watchdog" driver.
3  *
4  * Copyright 2016 Google Inc.
5  *
6  * Released under the GPLv2 only.
7  */
8
9 #include <linux/delay.h>
10 #include <linux/workqueue.h>
11 #include "greybus.h"
12
13 #define SVC_WATCHDOG_PERIOD     (2*HZ)
14
15 struct gb_svc_watchdog {
16         struct delayed_work     work;
17         struct gb_svc           *svc;
18         bool                    finished;
19 };
20
21 static struct delayed_work reset_work;
22
23 static void greybus_reset(struct work_struct *work)
24 {
25         static char start_path[256] = "/system/bin/start";
26         static char *envp[] = {
27                 "HOME=/",
28                 "PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin",
29                 NULL,
30         };
31         static char *argv[] = {
32                 start_path,
33                 "unipro_reset",
34                 NULL,
35         };
36
37         printk(KERN_ERR "svc_watchdog: calling \"%s %s\" to reset greybus network!\n",
38                argv[0], argv[1]);
39         call_usermodehelper(start_path, argv, envp, UMH_WAIT_EXEC);
40 }
41
42 static void do_work(struct work_struct *work)
43 {
44         struct gb_svc_watchdog *watchdog;
45         struct gb_svc *svc;
46         int retval;
47
48         watchdog = container_of(work, struct gb_svc_watchdog, work.work);
49         svc = watchdog->svc;
50
51         dev_dbg(&svc->dev, "%s: ping.\n", __func__);
52         retval = gb_svc_ping(svc);
53         if (retval) {
54                 /*
55                  * Something went really wrong, let's warn userspace and then
56                  * pull the plug and reset the whole greybus network.
57                  * We need to do this outside of this workqueue as we will be
58                  * tearing down the svc device itself.  So queue up
59                  * yet-another-callback to do that.
60                  */
61                 dev_err(&svc->dev,
62                         "SVC ping has returned %d, something is wrong!!!\n",
63                         retval);
64                 dev_err(&svc->dev, "Resetting the greybus network, watch out!!!\n");
65
66                 INIT_DELAYED_WORK(&reset_work, greybus_reset);
67                 queue_delayed_work(system_wq, &reset_work, HZ/2);
68                 return;
69         }
70
71         /* resubmit our work to happen again, if we are still "alive" */
72         if (!watchdog->finished)
73                 queue_delayed_work(system_wq, &watchdog->work,
74                                    SVC_WATCHDOG_PERIOD);
75 }
76
77 int gb_svc_watchdog_create(struct gb_svc *svc)
78 {
79         struct gb_svc_watchdog *watchdog;
80
81         if (svc->watchdog)
82                 return 0;
83
84         watchdog = kmalloc(sizeof(*watchdog), GFP_KERNEL);
85         if (!watchdog)
86                 return -ENOMEM;
87
88         watchdog->finished = false;
89         watchdog->svc = svc;
90         INIT_DELAYED_WORK(&watchdog->work, do_work);
91         svc->watchdog = watchdog;
92
93         queue_delayed_work(system_wq, &watchdog->work,
94                            SVC_WATCHDOG_PERIOD);
95         return 0;
96 }
97
98 void gb_svc_watchdog_destroy(struct gb_svc *svc)
99 {
100         struct gb_svc_watchdog *watchdog = svc->watchdog;
101
102         if (!watchdog)
103                 return;
104
105         watchdog->finished = true;
106         cancel_delayed_work_sync(&watchdog->work);
107         svc->watchdog = NULL;
108         kfree(watchdog);
109 }