aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hsi/hsi.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/hsi/hsi.c')
-rw-r--r--drivers/hsi/hsi.c223
1 files changed, 118 insertions, 105 deletions
diff --git a/drivers/hsi/hsi.c b/drivers/hsi/hsi.c
index 4e2d79b79334..2d58f939d27f 100644
--- a/drivers/hsi/hsi.c
+++ b/drivers/hsi/hsi.c
@@ -21,26 +21,13 @@
21 */ 21 */
22#include <linux/hsi/hsi.h> 22#include <linux/hsi/hsi.h>
23#include <linux/compiler.h> 23#include <linux/compiler.h>
24#include <linux/rwsem.h>
25#include <linux/list.h> 24#include <linux/list.h>
26#include <linux/spinlock.h>
27#include <linux/kobject.h> 25#include <linux/kobject.h>
28#include <linux/slab.h> 26#include <linux/slab.h>
29#include <linux/string.h> 27#include <linux/string.h>
28#include <linux/notifier.h>
30#include "hsi_core.h" 29#include "hsi_core.h"
31 30
32static struct device_type hsi_ctrl = {
33 .name = "hsi_controller",
34};
35
36static struct device_type hsi_cl = {
37 .name = "hsi_client",
38};
39
40static struct device_type hsi_port = {
41 .name = "hsi_port",
42};
43
44static ssize_t modalias_show(struct device *dev, 31static ssize_t modalias_show(struct device *dev,
45 struct device_attribute *a __maybe_unused, char *buf) 32 struct device_attribute *a __maybe_unused, char *buf)
46{ 33{
@@ -54,8 +41,7 @@ static struct device_attribute hsi_bus_dev_attrs[] = {
54 41
55static int hsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env) 42static int hsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
56{ 43{
57 if (dev->type == &hsi_cl) 44 add_uevent_var(env, "MODALIAS=hsi:%s", dev_name(dev));
58 add_uevent_var(env, "MODALIAS=hsi:%s", dev_name(dev));
59 45
60 return 0; 46 return 0;
61} 47}
@@ -80,12 +66,10 @@ static void hsi_client_release(struct device *dev)
80static void hsi_new_client(struct hsi_port *port, struct hsi_board_info *info) 66static void hsi_new_client(struct hsi_port *port, struct hsi_board_info *info)
81{ 67{
82 struct hsi_client *cl; 68 struct hsi_client *cl;
83 unsigned long flags;
84 69
85 cl = kzalloc(sizeof(*cl), GFP_KERNEL); 70 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
86 if (!cl) 71 if (!cl)
87 return; 72 return;
88 cl->device.type = &hsi_cl;
89 cl->tx_cfg = info->tx_cfg; 73 cl->tx_cfg = info->tx_cfg;
90 cl->rx_cfg = info->rx_cfg; 74 cl->rx_cfg = info->rx_cfg;
91 cl->device.bus = &hsi_bus_type; 75 cl->device.bus = &hsi_bus_type;
@@ -93,14 +77,11 @@ static void hsi_new_client(struct hsi_port *port, struct hsi_board_info *info)
93 cl->device.release = hsi_client_release; 77 cl->device.release = hsi_client_release;
94 dev_set_name(&cl->device, info->name); 78 dev_set_name(&cl->device, info->name);
95 cl->device.platform_data = info->platform_data; 79 cl->device.platform_data = info->platform_data;
96 spin_lock_irqsave(&port->clock, flags);
97 list_add_tail(&cl->link, &port->clients);
98 spin_unlock_irqrestore(&port->clock, flags);
99 if (info->archdata) 80 if (info->archdata)
100 cl->device.archdata = *info->archdata; 81 cl->device.archdata = *info->archdata;
101 if (device_register(&cl->device) < 0) { 82 if (device_register(&cl->device) < 0) {
102 pr_err("hsi: failed to register client: %s\n", info->name); 83 pr_err("hsi: failed to register client: %s\n", info->name);
103 kfree(cl); 84 put_device(&cl->device);
104 } 85 }
105} 86}
106 87
@@ -120,13 +101,6 @@ static void hsi_scan_board_info(struct hsi_controller *hsi)
120 101
121static int hsi_remove_client(struct device *dev, void *data __maybe_unused) 102static int hsi_remove_client(struct device *dev, void *data __maybe_unused)
122{ 103{
123 struct hsi_client *cl = to_hsi_client(dev);
124 struct hsi_port *port = to_hsi_port(dev->parent);
125 unsigned long flags;
126
127 spin_lock_irqsave(&port->clock, flags);
128 list_del(&cl->link);
129 spin_unlock_irqrestore(&port->clock, flags);
130 device_unregister(dev); 104 device_unregister(dev);
131 105
132 return 0; 106 return 0;
@@ -140,12 +114,17 @@ static int hsi_remove_port(struct device *dev, void *data __maybe_unused)
140 return 0; 114 return 0;
141} 115}
142 116
143static void hsi_controller_release(struct device *dev __maybe_unused) 117static void hsi_controller_release(struct device *dev)
144{ 118{
119 struct hsi_controller *hsi = to_hsi_controller(dev);
120
121 kfree(hsi->port);
122 kfree(hsi);
145} 123}
146 124
147static void hsi_port_release(struct device *dev __maybe_unused) 125static void hsi_port_release(struct device *dev)
148{ 126{
127 kfree(to_hsi_port(dev));
149} 128}
150 129
151/** 130/**
@@ -170,20 +149,12 @@ int hsi_register_controller(struct hsi_controller *hsi)
170 unsigned int i; 149 unsigned int i;
171 int err; 150 int err;
172 151
173 hsi->device.type = &hsi_ctrl; 152 err = device_add(&hsi->device);
174 hsi->device.bus = &hsi_bus_type;
175 hsi->device.release = hsi_controller_release;
176 err = device_register(&hsi->device);
177 if (err < 0) 153 if (err < 0)
178 return err; 154 return err;
179 for (i = 0; i < hsi->num_ports; i++) { 155 for (i = 0; i < hsi->num_ports; i++) {
180 hsi->port[i].device.parent = &hsi->device; 156 hsi->port[i]->device.parent = &hsi->device;
181 hsi->port[i].device.bus = &hsi_bus_type; 157 err = device_add(&hsi->port[i]->device);
182 hsi->port[i].device.release = hsi_port_release;
183 hsi->port[i].device.type = &hsi_port;
184 INIT_LIST_HEAD(&hsi->port[i].clients);
185 spin_lock_init(&hsi->port[i].clock);
186 err = device_register(&hsi->port[i].device);
187 if (err < 0) 158 if (err < 0)
188 goto out; 159 goto out;
189 } 160 }
@@ -192,7 +163,9 @@ int hsi_register_controller(struct hsi_controller *hsi)
192 163
193 return 0; 164 return 0;
194out: 165out:
195 hsi_unregister_controller(hsi); 166 while (i-- > 0)
167 device_del(&hsi->port[i]->device);
168 device_del(&hsi->device);
196 169
197 return err; 170 return err;
198} 171}
@@ -223,6 +196,29 @@ static inline int hsi_dummy_cl(struct hsi_client *cl __maybe_unused)
223} 196}
224 197
225/** 198/**
199 * hsi_put_controller - Free an HSI controller
200 *
201 * @hsi: Pointer to the HSI controller to freed
202 *
203 * HSI controller drivers should only use this function if they need
204 * to free their allocated hsi_controller structures before a successful
205 * call to hsi_register_controller. Other use is not allowed.
206 */
207void hsi_put_controller(struct hsi_controller *hsi)
208{
209 unsigned int i;
210
211 if (!hsi)
212 return;
213
214 for (i = 0; i < hsi->num_ports; i++)
215 if (hsi->port && hsi->port[i])
216 put_device(&hsi->port[i]->device);
217 put_device(&hsi->device);
218}
219EXPORT_SYMBOL_GPL(hsi_put_controller);
220
221/**
226 * hsi_alloc_controller - Allocate an HSI controller and its ports 222 * hsi_alloc_controller - Allocate an HSI controller and its ports
227 * @n_ports: Number of ports on the HSI controller 223 * @n_ports: Number of ports on the HSI controller
228 * @flags: Kernel allocation flags 224 * @flags: Kernel allocation flags
@@ -232,55 +228,52 @@ static inline int hsi_dummy_cl(struct hsi_client *cl __maybe_unused)
232struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags) 228struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags)
233{ 229{
234 struct hsi_controller *hsi; 230 struct hsi_controller *hsi;
235 struct hsi_port *port; 231 struct hsi_port **port;
236 unsigned int i; 232 unsigned int i;
237 233
238 if (!n_ports) 234 if (!n_ports)
239 return NULL; 235 return NULL;
240 236
241 port = kzalloc(sizeof(*port)*n_ports, flags);
242 if (!port)
243 return NULL;
244 hsi = kzalloc(sizeof(*hsi), flags); 237 hsi = kzalloc(sizeof(*hsi), flags);
245 if (!hsi) 238 if (!hsi)
246 goto out; 239 return NULL;
247 for (i = 0; i < n_ports; i++) { 240 port = kzalloc(sizeof(*port)*n_ports, flags);
248 dev_set_name(&port[i].device, "port%d", i); 241 if (!port) {
249 port[i].num = i; 242 kfree(hsi);
250 port[i].async = hsi_dummy_msg; 243 return NULL;
251 port[i].setup = hsi_dummy_cl;
252 port[i].flush = hsi_dummy_cl;
253 port[i].start_tx = hsi_dummy_cl;
254 port[i].stop_tx = hsi_dummy_cl;
255 port[i].release = hsi_dummy_cl;
256 mutex_init(&port[i].lock);
257 } 244 }
258 hsi->num_ports = n_ports; 245 hsi->num_ports = n_ports;
259 hsi->port = port; 246 hsi->port = port;
247 hsi->device.release = hsi_controller_release;
248 device_initialize(&hsi->device);
249
250 for (i = 0; i < n_ports; i++) {
251 port[i] = kzalloc(sizeof(**port), flags);
252 if (port[i] == NULL)
253 goto out;
254 port[i]->num = i;
255 port[i]->async = hsi_dummy_msg;
256 port[i]->setup = hsi_dummy_cl;
257 port[i]->flush = hsi_dummy_cl;
258 port[i]->start_tx = hsi_dummy_cl;
259 port[i]->stop_tx = hsi_dummy_cl;
260 port[i]->release = hsi_dummy_cl;
261 mutex_init(&port[i]->lock);
262 ATOMIC_INIT_NOTIFIER_HEAD(&port[i]->n_head);
263 dev_set_name(&port[i]->device, "port%d", i);
264 hsi->port[i]->device.release = hsi_port_release;
265 device_initialize(&hsi->port[i]->device);
266 }
260 267
261 return hsi; 268 return hsi;
262out: 269out:
263 kfree(port); 270 hsi_put_controller(hsi);
264 271
265 return NULL; 272 return NULL;
266} 273}
267EXPORT_SYMBOL_GPL(hsi_alloc_controller); 274EXPORT_SYMBOL_GPL(hsi_alloc_controller);
268 275
269/** 276/**
270 * hsi_free_controller - Free an HSI controller
271 * @hsi: Pointer to HSI controller
272 */
273void hsi_free_controller(struct hsi_controller *hsi)
274{
275 if (!hsi)
276 return;
277
278 kfree(hsi->port);
279 kfree(hsi);
280}
281EXPORT_SYMBOL_GPL(hsi_free_controller);
282
283/**
284 * hsi_free_msg - Free an HSI message 277 * hsi_free_msg - Free an HSI message
285 * @msg: Pointer to the HSI message 278 * @msg: Pointer to the HSI message
286 * 279 *
@@ -414,37 +407,67 @@ void hsi_release_port(struct hsi_client *cl)
414} 407}
415EXPORT_SYMBOL_GPL(hsi_release_port); 408EXPORT_SYMBOL_GPL(hsi_release_port);
416 409
417static int hsi_start_rx(struct hsi_client *cl, void *data __maybe_unused) 410static int hsi_event_notifier_call(struct notifier_block *nb,
411 unsigned long event, void *data __maybe_unused)
418{ 412{
419 if (cl->hsi_start_rx) 413 struct hsi_client *cl = container_of(nb, struct hsi_client, nb);
420 (*cl->hsi_start_rx)(cl); 414
415 (*cl->ehandler)(cl, event);
421 416
422 return 0; 417 return 0;
423} 418}
424 419
425static int hsi_stop_rx(struct hsi_client *cl, void *data __maybe_unused) 420/**
421 * hsi_register_port_event - Register a client to receive port events
422 * @cl: HSI client that wants to receive port events
423 * @cb: Event handler callback
424 *
425 * Clients should register a callback to be able to receive
426 * events from the ports. Registration should happen after
427 * claiming the port.
428 * The handler can be called in interrupt context.
429 *
430 * Returns -errno on error, or 0 on success.
431 */
432int hsi_register_port_event(struct hsi_client *cl,
433 void (*handler)(struct hsi_client *, unsigned long))
426{ 434{
427 if (cl->hsi_stop_rx) 435 struct hsi_port *port = hsi_get_port(cl);
428 (*cl->hsi_stop_rx)(cl);
429 436
430 return 0; 437 if (!handler || cl->ehandler)
438 return -EINVAL;
439 if (!hsi_port_claimed(cl))
440 return -EACCES;
441 cl->ehandler = handler;
442 cl->nb.notifier_call = hsi_event_notifier_call;
443
444 return atomic_notifier_chain_register(&port->n_head, &cl->nb);
431} 445}
446EXPORT_SYMBOL_GPL(hsi_register_port_event);
432 447
433static int hsi_port_for_each_client(struct hsi_port *port, void *data, 448/**
434 int (*fn)(struct hsi_client *cl, void *data)) 449 * hsi_unregister_port_event - Stop receiving port events for a client
450 * @cl: HSI client that wants to stop receiving port events
451 *
452 * Clients should call this function before releasing their associated
453 * port.
454 *
455 * Returns -errno on error, or 0 on success.
456 */
457int hsi_unregister_port_event(struct hsi_client *cl)
435{ 458{
436 struct hsi_client *cl; 459 struct hsi_port *port = hsi_get_port(cl);
460 int err;
437 461
438 spin_lock(&port->clock); 462 WARN_ON(!hsi_port_claimed(cl));
439 list_for_each_entry(cl, &port->clients, link) {
440 spin_unlock(&port->clock);
441 (*fn)(cl, data);
442 spin_lock(&port->clock);
443 }
444 spin_unlock(&port->clock);
445 463
446 return 0; 464 err = atomic_notifier_chain_unregister(&port->n_head, &cl->nb);
465 if (!err)
466 cl->ehandler = NULL;
467
468 return err;
447} 469}
470EXPORT_SYMBOL_GPL(hsi_unregister_port_event);
448 471
449/** 472/**
450 * hsi_event -Notifies clients about port events 473 * hsi_event -Notifies clients about port events
@@ -458,22 +481,12 @@ static int hsi_port_for_each_client(struct hsi_port *port, void *data,
458 * Events: 481 * Events:
459 * HSI_EVENT_START_RX - Incoming wake line high 482 * HSI_EVENT_START_RX - Incoming wake line high
460 * HSI_EVENT_STOP_RX - Incoming wake line down 483 * HSI_EVENT_STOP_RX - Incoming wake line down
484 *
485 * Returns -errno on error, or 0 on success.
461 */ 486 */
462void hsi_event(struct hsi_port *port, unsigned int event) 487int hsi_event(struct hsi_port *port, unsigned long event)
463{ 488{
464 int (*fn)(struct hsi_client *cl, void *data); 489 return atomic_notifier_call_chain(&port->n_head, event, NULL);
465
466 switch (event) {
467 case HSI_EVENT_START_RX:
468 fn = hsi_start_rx;
469 break;
470 case HSI_EVENT_STOP_RX:
471 fn = hsi_stop_rx;
472 break;
473 default:
474 return;
475 }
476 hsi_port_for_each_client(port, NULL, fn);
477} 490}
478EXPORT_SYMBOL_GPL(hsi_event); 491EXPORT_SYMBOL_GPL(hsi_event);
479 492