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.
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.
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
16 * Copyright (c) 2008,2009 Andy Green <andy@openmoko.com>
19 #include <linux/kernel.h>
20 #include <linux/device.h>
22 #include <linux/touchscreen/ts_filter_chain.h>
23 #include <linux/touchscreen/ts_filter.h>
26 * Tux, would you like the following function in /lib?
27 * It helps us avoid silly code.
31 * sptrlen - Count how many non-null pointers are in a pointer array
32 * @arr: The array of pointers
34 static int sptrlen(const void *arr)
36 /* All pointers have the same size. */
37 const int **p = (const int **)arr;
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. */
54 /* FIXME: Add a spinlock and use it. */
57 struct ts_filter_chain *ts_filter_chain_create(
58 struct platform_device *pdev,
59 const struct ts_filter_chain_configuration conf[],
62 struct ts_filter_chain *c;
66 BUG_ON((count_coords < 1));
67 BUG_ON(count_coords > MAX_TS_FILTER_COORDS);
69 c = kzalloc(sizeof(struct ts_filter_chain), GFP_KERNEL);
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);
78 c->pchain = c->arr + len;
81 /* TODO: Can we get away with only sending pdev->dev? */
83 (conf->api->create)(pdev, conf->config, count_coords);
85 dev_info(&pdev->dev, "Filter %d creation failed\n",
93 if (f->api->haspoint && f->api->getpoint && f->api->process)
94 c->pchain[c->pchain_len++] = f;
99 dev_info(&pdev->dev, "%d filter(s) initialized\n", count);
104 ts_filter_chain_destroy(c); /* Also frees c. */
106 dev_info(&pdev->dev, "Error in filter chain initialization\n");
108 * FIXME: Individual filters have to return errors this way.
109 * We only have to forward the errors we find.
111 return ERR_PTR(-ENOMEM);
113 EXPORT_SYMBOL_GPL(ts_filter_chain_create);
115 void ts_filter_chain_destroy(struct ts_filter_chain *c)
118 struct ts_filter **a = c->arr;
120 ((*a)->api->destroy)(*a);
127 EXPORT_SYMBOL_GPL(ts_filter_chain_destroy);
129 void ts_filter_chain_clear(struct ts_filter_chain *c)
131 struct ts_filter **a = c->arr;
134 if ((*a)->api->clear)
135 ((*a)->api->clear)(*a);
139 EXPORT_SYMBOL_GPL(ts_filter_chain_clear);
141 static void ts_filter_chain_scale(struct ts_filter_chain *c, int *coords)
143 struct ts_filter **a = c->arr;
145 if ((*a)->api->scale)
146 ((*a)->api->scale)(*a, coords);
151 int ts_filter_chain_feed(struct ts_filter_chain *c, int *coords)
153 int len = c->pchain_len;
157 return 1; /* Nothing to do. */
159 BUG_ON(c->pchain[0]->api->haspoint(c->pchain[0]));
161 if (c->pchain[0]->api->process(c->pchain[0], coords))
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);
168 c->pchain[i]->api->process(c->pchain[i], coords))
169 return -1; /* Error. */
175 if (i >= 0) { /* Same as i == len. */
176 ts_filter_chain_scale(c, coords);
182 EXPORT_SYMBOL_GPL(ts_filter_chain_feed);