goldfish: R.I.P.
[openwrt.git] / target / linux / s3c24xx / files-2.6.30 / drivers / input / touchscreen / ts_filter_chain.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
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.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * Copyright (c) 2008,2009 Andy Green <andy@openmoko.com>
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/device.h>
21
22 #include <linux/touchscreen/ts_filter_chain.h>
23 #include <linux/touchscreen/ts_filter.h>
24
25 /*
26  * Tux, would you like the following function in /lib?
27  * It helps us avoid silly code.
28  */
29
30 /**
31  * sptrlen - Count how many non-null pointers are in a pointer array
32  * @arr: The array of pointers
33  */
34 static int sptrlen(const void *arr)
35 {
36         /* All pointers have the same size. */
37         const int **p = (const int **)arr;
38         int len = 0;
39
40         while (*(p++))
41                 len++;
42
43         return len;
44 }
45
46
47 struct ts_filter_chain {
48         /* All of the filters. */
49         struct ts_filter **arr;
50         /* Filters that can propagate values in the chain. */
51         struct ts_filter **pchain;
52         /* Length of the pchain array. */
53         int pchain_len;
54         /* FIXME: Add a spinlock and use it. */
55 };
56
57 struct ts_filter_chain *ts_filter_chain_create(
58         struct platform_device *pdev,
59         const struct ts_filter_chain_configuration conf[],
60         int count_coords)
61 {
62         struct ts_filter_chain *c;
63         int count = 0;
64         int len;
65
66         BUG_ON((count_coords < 1));
67         BUG_ON(count_coords > MAX_TS_FILTER_COORDS);
68
69         c = kzalloc(sizeof(struct ts_filter_chain), GFP_KERNEL);
70         if (!c)
71                 goto create_err_1;
72
73         len = (sptrlen(conf) + 1);
74         /* Memory for two null-terminated arrays of filters. */
75         c->arr = kzalloc(2 * sizeof(struct ts_filter *) * len, GFP_KERNEL);
76         if (!c->arr)
77                 goto create_err_1;
78         c->pchain = c->arr + len;
79
80         while (conf->api) {
81                 /* TODO: Can we get away with only sending pdev->dev? */
82                 struct ts_filter *f =
83                         (conf->api->create)(pdev, conf->config, count_coords);
84                 if (!f) {
85                         dev_info(&pdev->dev, "Filter %d creation failed\n",
86                                  count);
87                         goto create_err_2;
88                 }
89
90                 f->api = conf->api;
91                 c->arr[count++] = f;
92
93                 if (f->api->haspoint && f->api->getpoint && f->api->process)
94                         c->pchain[c->pchain_len++] = f;
95
96                 conf++;
97         }
98
99         dev_info(&pdev->dev, "%d filter(s) initialized\n", count);
100
101         return c;
102
103 create_err_2:
104         ts_filter_chain_destroy(c); /* Also frees c. */
105 create_err_1:
106         dev_info(&pdev->dev, "Error in filter chain initialization\n");
107         /*
108          * FIXME: Individual filters have to return errors this way.
109          * We only have to forward the errors we find.
110          */
111         return ERR_PTR(-ENOMEM);
112 }
113 EXPORT_SYMBOL_GPL(ts_filter_chain_create);
114
115 void ts_filter_chain_destroy(struct ts_filter_chain *c)
116 {
117         if (c->arr) {
118                 struct ts_filter **a = c->arr;
119                 while (*a) {
120                         ((*a)->api->destroy)(*a);
121                         a++;
122                 }
123                 kfree(c->arr);
124         }
125         kfree(c);
126 }
127 EXPORT_SYMBOL_GPL(ts_filter_chain_destroy);
128
129 void ts_filter_chain_clear(struct ts_filter_chain *c)
130 {
131         struct ts_filter **a = c->arr;
132
133         while (*a) {
134                 if ((*a)->api->clear)
135                         ((*a)->api->clear)(*a);
136                 a++;
137         }
138 }
139 EXPORT_SYMBOL_GPL(ts_filter_chain_clear);
140
141 static void ts_filter_chain_scale(struct ts_filter_chain *c, int *coords)
142 {
143         struct ts_filter **a = c->arr;
144         while (*a) {
145                 if ((*a)->api->scale)
146                         ((*a)->api->scale)(*a, coords);
147                 a++;
148         }
149 }
150
151 int ts_filter_chain_feed(struct ts_filter_chain *c, int *coords)
152 {
153         int len = c->pchain_len;
154         int i = len - 1;
155
156         if (!c->pchain[0])
157                 return 1; /* Nothing to do. */
158
159         BUG_ON(c->pchain[0]->api->haspoint(c->pchain[0]));
160
161         if (c->pchain[0]->api->process(c->pchain[0], coords))
162                 return -1;
163
164         while (i >= 0 && i < len) {
165                 if (c->pchain[i]->api->haspoint(c->pchain[i])) {
166                         c->pchain[i]->api->getpoint(c->pchain[i], coords);
167                         if (++i < len &&
168                             c->pchain[i]->api->process(c->pchain[i], coords))
169                                 return -1; /* Error. */
170                 } else {
171                         i--;
172                 }
173         }
174
175         if (i >= 0) {   /* Same as i == len. */
176                 ts_filter_chain_scale(c, coords);
177                 return 1;
178         }
179
180         return 0;
181 }
182 EXPORT_SYMBOL_GPL(ts_filter_chain_feed);
183