batman-adv: add broadcast duplicate check
[linux-2.6.git] / drivers / hsi / hsi.c
blob4e2d79b793349c1d6c3b375677b2af7a87db2ccb
1 /*
2 * HSI core.
4 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
6 * Contact: Carlos Chinea <carlos.chinea@nokia.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
22 #include <linux/hsi/hsi.h>
23 #include <linux/compiler.h>
24 #include <linux/rwsem.h>
25 #include <linux/list.h>
26 #include <linux/spinlock.h>
27 #include <linux/kobject.h>
28 #include <linux/slab.h>
29 #include <linux/string.h>
30 #include "hsi_core.h"
32 static struct device_type hsi_ctrl = {
33 .name = "hsi_controller",
36 static struct device_type hsi_cl = {
37 .name = "hsi_client",
40 static struct device_type hsi_port = {
41 .name = "hsi_port",
44 static ssize_t modalias_show(struct device *dev,
45 struct device_attribute *a __maybe_unused, char *buf)
47 return sprintf(buf, "hsi:%s\n", dev_name(dev));
50 static struct device_attribute hsi_bus_dev_attrs[] = {
51 __ATTR_RO(modalias),
52 __ATTR_NULL,
55 static int hsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
57 if (dev->type == &hsi_cl)
58 add_uevent_var(env, "MODALIAS=hsi:%s", dev_name(dev));
60 return 0;
63 static int hsi_bus_match(struct device *dev, struct device_driver *driver)
65 return strcmp(dev_name(dev), driver->name) == 0;
68 static struct bus_type hsi_bus_type = {
69 .name = "hsi",
70 .dev_attrs = hsi_bus_dev_attrs,
71 .match = hsi_bus_match,
72 .uevent = hsi_bus_uevent,
75 static void hsi_client_release(struct device *dev)
77 kfree(to_hsi_client(dev));
80 static void hsi_new_client(struct hsi_port *port, struct hsi_board_info *info)
82 struct hsi_client *cl;
83 unsigned long flags;
85 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
86 if (!cl)
87 return;
88 cl->device.type = &hsi_cl;
89 cl->tx_cfg = info->tx_cfg;
90 cl->rx_cfg = info->rx_cfg;
91 cl->device.bus = &hsi_bus_type;
92 cl->device.parent = &port->device;
93 cl->device.release = hsi_client_release;
94 dev_set_name(&cl->device, info->name);
95 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)
100 cl->device.archdata = *info->archdata;
101 if (device_register(&cl->device) < 0) {
102 pr_err("hsi: failed to register client: %s\n", info->name);
103 kfree(cl);
107 static void hsi_scan_board_info(struct hsi_controller *hsi)
109 struct hsi_cl_info *cl_info;
110 struct hsi_port *p;
112 list_for_each_entry(cl_info, &hsi_board_list, list)
113 if (cl_info->info.hsi_id == hsi->id) {
114 p = hsi_find_port_num(hsi, cl_info->info.port);
115 if (!p)
116 continue;
117 hsi_new_client(p, &cl_info->info);
121 static int hsi_remove_client(struct device *dev, void *data __maybe_unused)
123 struct hsi_client *cl = to_hsi_client(dev);
124 struct hsi_port *port = to_hsi_port(dev->parent);
125 unsigned long flags;
127 spin_lock_irqsave(&port->clock, flags);
128 list_del(&cl->link);
129 spin_unlock_irqrestore(&port->clock, flags);
130 device_unregister(dev);
132 return 0;
135 static int hsi_remove_port(struct device *dev, void *data __maybe_unused)
137 device_for_each_child(dev, NULL, hsi_remove_client);
138 device_unregister(dev);
140 return 0;
143 static void hsi_controller_release(struct device *dev __maybe_unused)
147 static void hsi_port_release(struct device *dev __maybe_unused)
152 * hsi_unregister_controller - Unregister an HSI controller
153 * @hsi: The HSI controller to register
155 void hsi_unregister_controller(struct hsi_controller *hsi)
157 device_for_each_child(&hsi->device, NULL, hsi_remove_port);
158 device_unregister(&hsi->device);
160 EXPORT_SYMBOL_GPL(hsi_unregister_controller);
163 * hsi_register_controller - Register an HSI controller and its ports
164 * @hsi: The HSI controller to register
166 * Returns -errno on failure, 0 on success.
168 int hsi_register_controller(struct hsi_controller *hsi)
170 unsigned int i;
171 int err;
173 hsi->device.type = &hsi_ctrl;
174 hsi->device.bus = &hsi_bus_type;
175 hsi->device.release = hsi_controller_release;
176 err = device_register(&hsi->device);
177 if (err < 0)
178 return err;
179 for (i = 0; i < hsi->num_ports; i++) {
180 hsi->port[i].device.parent = &hsi->device;
181 hsi->port[i].device.bus = &hsi_bus_type;
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)
188 goto out;
190 /* Populate HSI bus with HSI clients */
191 hsi_scan_board_info(hsi);
193 return 0;
194 out:
195 hsi_unregister_controller(hsi);
197 return err;
199 EXPORT_SYMBOL_GPL(hsi_register_controller);
202 * hsi_register_client_driver - Register an HSI client to the HSI bus
203 * @drv: HSI client driver to register
205 * Returns -errno on failure, 0 on success.
207 int hsi_register_client_driver(struct hsi_client_driver *drv)
209 drv->driver.bus = &hsi_bus_type;
211 return driver_register(&drv->driver);
213 EXPORT_SYMBOL_GPL(hsi_register_client_driver);
215 static inline int hsi_dummy_msg(struct hsi_msg *msg __maybe_unused)
217 return 0;
220 static inline int hsi_dummy_cl(struct hsi_client *cl __maybe_unused)
222 return 0;
226 * hsi_alloc_controller - Allocate an HSI controller and its ports
227 * @n_ports: Number of ports on the HSI controller
228 * @flags: Kernel allocation flags
230 * Return NULL on failure or a pointer to an hsi_controller on success.
232 struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags)
234 struct hsi_controller *hsi;
235 struct hsi_port *port;
236 unsigned int i;
238 if (!n_ports)
239 return NULL;
241 port = kzalloc(sizeof(*port)*n_ports, flags);
242 if (!port)
243 return NULL;
244 hsi = kzalloc(sizeof(*hsi), flags);
245 if (!hsi)
246 goto out;
247 for (i = 0; i < n_ports; i++) {
248 dev_set_name(&port[i].device, "port%d", i);
249 port[i].num = i;
250 port[i].async = hsi_dummy_msg;
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);
258 hsi->num_ports = n_ports;
259 hsi->port = port;
261 return hsi;
262 out:
263 kfree(port);
265 return NULL;
267 EXPORT_SYMBOL_GPL(hsi_alloc_controller);
270 * hsi_free_controller - Free an HSI controller
271 * @hsi: Pointer to HSI controller
273 void hsi_free_controller(struct hsi_controller *hsi)
275 if (!hsi)
276 return;
278 kfree(hsi->port);
279 kfree(hsi);
281 EXPORT_SYMBOL_GPL(hsi_free_controller);
284 * hsi_free_msg - Free an HSI message
285 * @msg: Pointer to the HSI message
287 * Client is responsible to free the buffers pointed by the scatterlists.
289 void hsi_free_msg(struct hsi_msg *msg)
291 if (!msg)
292 return;
293 sg_free_table(&msg->sgt);
294 kfree(msg);
296 EXPORT_SYMBOL_GPL(hsi_free_msg);
299 * hsi_alloc_msg - Allocate an HSI message
300 * @nents: Number of memory entries
301 * @flags: Kernel allocation flags
303 * nents can be 0. This mainly makes sense for read transfer.
304 * In that case, HSI drivers will call the complete callback when
305 * there is data to be read without consuming it.
307 * Return NULL on failure or a pointer to an hsi_msg on success.
309 struct hsi_msg *hsi_alloc_msg(unsigned int nents, gfp_t flags)
311 struct hsi_msg *msg;
312 int err;
314 msg = kzalloc(sizeof(*msg), flags);
315 if (!msg)
316 return NULL;
318 if (!nents)
319 return msg;
321 err = sg_alloc_table(&msg->sgt, nents, flags);
322 if (unlikely(err)) {
323 kfree(msg);
324 msg = NULL;
327 return msg;
329 EXPORT_SYMBOL_GPL(hsi_alloc_msg);
332 * hsi_async - Submit an HSI transfer to the controller
333 * @cl: HSI client sending the transfer
334 * @msg: The HSI transfer passed to controller
336 * The HSI message must have the channel, ttype, complete and destructor
337 * fields set beforehand. If nents > 0 then the client has to initialize
338 * also the scatterlists to point to the buffers to write to or read from.
340 * HSI controllers relay on pre-allocated buffers from their clients and they
341 * do not allocate buffers on their own.
343 * Once the HSI message transfer finishes, the HSI controller calls the
344 * complete callback with the status and actual_len fields of the HSI message
345 * updated. The complete callback can be called before returning from
346 * hsi_async.
348 * Returns -errno on failure or 0 on success
350 int hsi_async(struct hsi_client *cl, struct hsi_msg *msg)
352 struct hsi_port *port = hsi_get_port(cl);
354 if (!hsi_port_claimed(cl))
355 return -EACCES;
357 WARN_ON_ONCE(!msg->destructor || !msg->complete);
358 msg->cl = cl;
360 return port->async(msg);
362 EXPORT_SYMBOL_GPL(hsi_async);
365 * hsi_claim_port - Claim the HSI client's port
366 * @cl: HSI client that wants to claim its port
367 * @share: Flag to indicate if the client wants to share the port or not.
369 * Returns -errno on failure, 0 on success.
371 int hsi_claim_port(struct hsi_client *cl, unsigned int share)
373 struct hsi_port *port = hsi_get_port(cl);
374 int err = 0;
376 mutex_lock(&port->lock);
377 if ((port->claimed) && (!port->shared || !share)) {
378 err = -EBUSY;
379 goto out;
381 if (!try_module_get(to_hsi_controller(port->device.parent)->owner)) {
382 err = -ENODEV;
383 goto out;
385 port->claimed++;
386 port->shared = !!share;
387 cl->pclaimed = 1;
388 out:
389 mutex_unlock(&port->lock);
391 return err;
393 EXPORT_SYMBOL_GPL(hsi_claim_port);
396 * hsi_release_port - Release the HSI client's port
397 * @cl: HSI client which previously claimed its port
399 void hsi_release_port(struct hsi_client *cl)
401 struct hsi_port *port = hsi_get_port(cl);
403 mutex_lock(&port->lock);
404 /* Allow HW driver to do some cleanup */
405 port->release(cl);
406 if (cl->pclaimed)
407 port->claimed--;
408 BUG_ON(port->claimed < 0);
409 cl->pclaimed = 0;
410 if (!port->claimed)
411 port->shared = 0;
412 module_put(to_hsi_controller(port->device.parent)->owner);
413 mutex_unlock(&port->lock);
415 EXPORT_SYMBOL_GPL(hsi_release_port);
417 static int hsi_start_rx(struct hsi_client *cl, void *data __maybe_unused)
419 if (cl->hsi_start_rx)
420 (*cl->hsi_start_rx)(cl);
422 return 0;
425 static int hsi_stop_rx(struct hsi_client *cl, void *data __maybe_unused)
427 if (cl->hsi_stop_rx)
428 (*cl->hsi_stop_rx)(cl);
430 return 0;
433 static int hsi_port_for_each_client(struct hsi_port *port, void *data,
434 int (*fn)(struct hsi_client *cl, void *data))
436 struct hsi_client *cl;
438 spin_lock(&port->clock);
439 list_for_each_entry(cl, &port->clients, link) {
440 spin_unlock(&port->clock);
441 (*fn)(cl, data);
442 spin_lock(&port->clock);
444 spin_unlock(&port->clock);
446 return 0;
450 * hsi_event -Notifies clients about port events
451 * @port: Port where the event occurred
452 * @event: The event type
454 * Clients should not be concerned about wake line behavior. However, due
455 * to a race condition in HSI HW protocol, clients need to be notified
456 * about wake line changes, so they can implement a workaround for it.
458 * Events:
459 * HSI_EVENT_START_RX - Incoming wake line high
460 * HSI_EVENT_STOP_RX - Incoming wake line down
462 void hsi_event(struct hsi_port *port, unsigned int event)
464 int (*fn)(struct hsi_client *cl, void *data);
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;
476 hsi_port_for_each_client(port, NULL, fn);
478 EXPORT_SYMBOL_GPL(hsi_event);
480 static int __init hsi_init(void)
482 return bus_register(&hsi_bus_type);
484 postcore_initcall(hsi_init);
486 static void __exit hsi_exit(void)
488 bus_unregister(&hsi_bus_type);
490 module_exit(hsi_exit);
492 MODULE_AUTHOR("Carlos Chinea <carlos.chinea@nokia.com>");
493 MODULE_DESCRIPTION("High-speed Synchronous Serial Interface (HSI) framework");
494 MODULE_LICENSE("GPL v2");