Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 1 | /* |
| 2 | * HSI core. |
| 3 | * |
| 4 | * Copyright (C) 2010 Nokia Corporation. All rights reserved. |
| 5 | * |
| 6 | * Contact: Carlos Chinea <carlos.chinea@nokia.com> |
| 7 | * |
| 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. |
| 11 | * |
| 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. |
| 16 | * |
| 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 |
| 21 | */ |
| 22 | #include <linux/hsi/hsi.h> |
| 23 | #include <linux/compiler.h> |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 24 | #include <linux/list.h> |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 25 | #include <linux/kobject.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/string.h> |
Carlos Chinea | ec1c56f | 2012-04-11 10:55:53 +0300 | [diff] [blame] | 28 | #include <linux/notifier.h> |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 29 | #include "hsi_core.h" |
| 30 | |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 31 | static ssize_t modalias_show(struct device *dev, |
| 32 | struct device_attribute *a __maybe_unused, char *buf) |
| 33 | { |
| 34 | return sprintf(buf, "hsi:%s\n", dev_name(dev)); |
| 35 | } |
Greg Kroah-Hartman | 0017314 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 36 | static DEVICE_ATTR_RO(modalias); |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 37 | |
Greg Kroah-Hartman | 0017314 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 38 | static struct attribute *hsi_bus_dev_attrs[] = { |
| 39 | &dev_attr_modalias.attr, |
| 40 | NULL, |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 41 | }; |
Greg Kroah-Hartman | 0017314 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 42 | ATTRIBUTE_GROUPS(hsi_bus_dev); |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 43 | |
| 44 | static int hsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 45 | { |
Carlos Chinea | 6f02b9e | 2012-04-10 15:11:24 +0300 | [diff] [blame] | 46 | add_uevent_var(env, "MODALIAS=hsi:%s", dev_name(dev)); |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static int hsi_bus_match(struct device *dev, struct device_driver *driver) |
| 52 | { |
| 53 | return strcmp(dev_name(dev), driver->name) == 0; |
| 54 | } |
| 55 | |
| 56 | static struct bus_type hsi_bus_type = { |
| 57 | .name = "hsi", |
Greg Kroah-Hartman | 0017314 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 58 | .dev_groups = hsi_bus_dev_groups, |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 59 | .match = hsi_bus_match, |
| 60 | .uevent = hsi_bus_uevent, |
| 61 | }; |
| 62 | |
| 63 | static void hsi_client_release(struct device *dev) |
| 64 | { |
| 65 | kfree(to_hsi_client(dev)); |
| 66 | } |
| 67 | |
| 68 | static void hsi_new_client(struct hsi_port *port, struct hsi_board_info *info) |
| 69 | { |
| 70 | struct hsi_client *cl; |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 71 | |
| 72 | cl = kzalloc(sizeof(*cl), GFP_KERNEL); |
| 73 | if (!cl) |
| 74 | return; |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 75 | cl->tx_cfg = info->tx_cfg; |
| 76 | cl->rx_cfg = info->rx_cfg; |
| 77 | cl->device.bus = &hsi_bus_type; |
| 78 | cl->device.parent = &port->device; |
| 79 | cl->device.release = hsi_client_release; |
Kees Cook | 02aa2a3 | 2013-07-03 15:04:56 -0700 | [diff] [blame] | 80 | dev_set_name(&cl->device, "%s", info->name); |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 81 | cl->device.platform_data = info->platform_data; |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 82 | if (info->archdata) |
| 83 | cl->device.archdata = *info->archdata; |
| 84 | if (device_register(&cl->device) < 0) { |
| 85 | pr_err("hsi: failed to register client: %s\n", info->name); |
Carlos Chinea | 90e41f9 | 2012-04-11 11:01:11 +0300 | [diff] [blame] | 86 | put_device(&cl->device); |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | static void hsi_scan_board_info(struct hsi_controller *hsi) |
| 91 | { |
| 92 | struct hsi_cl_info *cl_info; |
| 93 | struct hsi_port *p; |
| 94 | |
| 95 | list_for_each_entry(cl_info, &hsi_board_list, list) |
| 96 | if (cl_info->info.hsi_id == hsi->id) { |
| 97 | p = hsi_find_port_num(hsi, cl_info->info.port); |
| 98 | if (!p) |
| 99 | continue; |
| 100 | hsi_new_client(p, &cl_info->info); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | static int hsi_remove_client(struct device *dev, void *data __maybe_unused) |
| 105 | { |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 106 | device_unregister(dev); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static int hsi_remove_port(struct device *dev, void *data __maybe_unused) |
| 112 | { |
| 113 | device_for_each_child(dev, NULL, hsi_remove_client); |
| 114 | device_unregister(dev); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
Carlos Chinea | 5a218ce | 2012-04-04 14:11:45 +0300 | [diff] [blame] | 119 | static void hsi_controller_release(struct device *dev) |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 120 | { |
Carlos Chinea | 5a218ce | 2012-04-04 14:11:45 +0300 | [diff] [blame] | 121 | struct hsi_controller *hsi = to_hsi_controller(dev); |
| 122 | |
| 123 | kfree(hsi->port); |
| 124 | kfree(hsi); |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 125 | } |
| 126 | |
Carlos Chinea | 5a218ce | 2012-04-04 14:11:45 +0300 | [diff] [blame] | 127 | static void hsi_port_release(struct device *dev) |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 128 | { |
Carlos Chinea | 5a218ce | 2012-04-04 14:11:45 +0300 | [diff] [blame] | 129 | kfree(to_hsi_port(dev)); |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
Sebastian Reichel | a0bf37e | 2013-10-06 20:23:49 +0200 | [diff] [blame^] | 133 | * hsi_unregister_port - Unregister an HSI port |
| 134 | * @port: The HSI port to unregister |
| 135 | */ |
| 136 | void hsi_port_unregister_clients(struct hsi_port *port) |
| 137 | { |
| 138 | device_for_each_child(&port->device, NULL, hsi_remove_client); |
| 139 | } |
| 140 | EXPORT_SYMBOL_GPL(hsi_port_unregister_clients); |
| 141 | |
| 142 | /** |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 143 | * hsi_unregister_controller - Unregister an HSI controller |
| 144 | * @hsi: The HSI controller to register |
| 145 | */ |
| 146 | void hsi_unregister_controller(struct hsi_controller *hsi) |
| 147 | { |
| 148 | device_for_each_child(&hsi->device, NULL, hsi_remove_port); |
| 149 | device_unregister(&hsi->device); |
| 150 | } |
| 151 | EXPORT_SYMBOL_GPL(hsi_unregister_controller); |
| 152 | |
| 153 | /** |
| 154 | * hsi_register_controller - Register an HSI controller and its ports |
| 155 | * @hsi: The HSI controller to register |
| 156 | * |
| 157 | * Returns -errno on failure, 0 on success. |
| 158 | */ |
| 159 | int hsi_register_controller(struct hsi_controller *hsi) |
| 160 | { |
| 161 | unsigned int i; |
| 162 | int err; |
| 163 | |
Carlos Chinea | 5a218ce | 2012-04-04 14:11:45 +0300 | [diff] [blame] | 164 | err = device_add(&hsi->device); |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 165 | if (err < 0) |
| 166 | return err; |
| 167 | for (i = 0; i < hsi->num_ports; i++) { |
Carlos Chinea | 5a218ce | 2012-04-04 14:11:45 +0300 | [diff] [blame] | 168 | hsi->port[i]->device.parent = &hsi->device; |
Carlos Chinea | 5a218ce | 2012-04-04 14:11:45 +0300 | [diff] [blame] | 169 | err = device_add(&hsi->port[i]->device); |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 170 | if (err < 0) |
| 171 | goto out; |
| 172 | } |
| 173 | /* Populate HSI bus with HSI clients */ |
| 174 | hsi_scan_board_info(hsi); |
| 175 | |
| 176 | return 0; |
| 177 | out: |
Carlos Chinea | 5a218ce | 2012-04-04 14:11:45 +0300 | [diff] [blame] | 178 | while (i-- > 0) |
| 179 | device_del(&hsi->port[i]->device); |
| 180 | device_del(&hsi->device); |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 181 | |
| 182 | return err; |
| 183 | } |
| 184 | EXPORT_SYMBOL_GPL(hsi_register_controller); |
| 185 | |
| 186 | /** |
| 187 | * hsi_register_client_driver - Register an HSI client to the HSI bus |
| 188 | * @drv: HSI client driver to register |
| 189 | * |
| 190 | * Returns -errno on failure, 0 on success. |
| 191 | */ |
| 192 | int hsi_register_client_driver(struct hsi_client_driver *drv) |
| 193 | { |
| 194 | drv->driver.bus = &hsi_bus_type; |
| 195 | |
| 196 | return driver_register(&drv->driver); |
| 197 | } |
| 198 | EXPORT_SYMBOL_GPL(hsi_register_client_driver); |
| 199 | |
| 200 | static inline int hsi_dummy_msg(struct hsi_msg *msg __maybe_unused) |
| 201 | { |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static inline int hsi_dummy_cl(struct hsi_client *cl __maybe_unused) |
| 206 | { |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | /** |
Carlos Chinea | 5a218ce | 2012-04-04 14:11:45 +0300 | [diff] [blame] | 211 | * hsi_put_controller - Free an HSI controller |
| 212 | * |
| 213 | * @hsi: Pointer to the HSI controller to freed |
| 214 | * |
| 215 | * HSI controller drivers should only use this function if they need |
| 216 | * to free their allocated hsi_controller structures before a successful |
| 217 | * call to hsi_register_controller. Other use is not allowed. |
| 218 | */ |
| 219 | void hsi_put_controller(struct hsi_controller *hsi) |
| 220 | { |
| 221 | unsigned int i; |
| 222 | |
| 223 | if (!hsi) |
| 224 | return; |
| 225 | |
| 226 | for (i = 0; i < hsi->num_ports; i++) |
| 227 | if (hsi->port && hsi->port[i]) |
| 228 | put_device(&hsi->port[i]->device); |
| 229 | put_device(&hsi->device); |
| 230 | } |
| 231 | EXPORT_SYMBOL_GPL(hsi_put_controller); |
| 232 | |
| 233 | /** |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 234 | * hsi_alloc_controller - Allocate an HSI controller and its ports |
| 235 | * @n_ports: Number of ports on the HSI controller |
| 236 | * @flags: Kernel allocation flags |
| 237 | * |
| 238 | * Return NULL on failure or a pointer to an hsi_controller on success. |
| 239 | */ |
| 240 | struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags) |
| 241 | { |
| 242 | struct hsi_controller *hsi; |
Carlos Chinea | 5a218ce | 2012-04-04 14:11:45 +0300 | [diff] [blame] | 243 | struct hsi_port **port; |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 244 | unsigned int i; |
| 245 | |
| 246 | if (!n_ports) |
| 247 | return NULL; |
| 248 | |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 249 | hsi = kzalloc(sizeof(*hsi), flags); |
| 250 | if (!hsi) |
Carlos Chinea | 5a218ce | 2012-04-04 14:11:45 +0300 | [diff] [blame] | 251 | return NULL; |
| 252 | port = kzalloc(sizeof(*port)*n_ports, flags); |
| 253 | if (!port) { |
| 254 | kfree(hsi); |
| 255 | return NULL; |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 256 | } |
| 257 | hsi->num_ports = n_ports; |
| 258 | hsi->port = port; |
Carlos Chinea | 5a218ce | 2012-04-04 14:11:45 +0300 | [diff] [blame] | 259 | hsi->device.release = hsi_controller_release; |
| 260 | device_initialize(&hsi->device); |
| 261 | |
| 262 | for (i = 0; i < n_ports; i++) { |
| 263 | port[i] = kzalloc(sizeof(**port), flags); |
| 264 | if (port[i] == NULL) |
| 265 | goto out; |
| 266 | port[i]->num = i; |
| 267 | port[i]->async = hsi_dummy_msg; |
| 268 | port[i]->setup = hsi_dummy_cl; |
| 269 | port[i]->flush = hsi_dummy_cl; |
| 270 | port[i]->start_tx = hsi_dummy_cl; |
| 271 | port[i]->stop_tx = hsi_dummy_cl; |
| 272 | port[i]->release = hsi_dummy_cl; |
| 273 | mutex_init(&port[i]->lock); |
Carlos Chinea | ec1c56f | 2012-04-11 10:55:53 +0300 | [diff] [blame] | 274 | ATOMIC_INIT_NOTIFIER_HEAD(&port[i]->n_head); |
Carlos Chinea | 5a218ce | 2012-04-04 14:11:45 +0300 | [diff] [blame] | 275 | dev_set_name(&port[i]->device, "port%d", i); |
| 276 | hsi->port[i]->device.release = hsi_port_release; |
| 277 | device_initialize(&hsi->port[i]->device); |
| 278 | } |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 279 | |
| 280 | return hsi; |
| 281 | out: |
Carlos Chinea | 5a218ce | 2012-04-04 14:11:45 +0300 | [diff] [blame] | 282 | hsi_put_controller(hsi); |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 283 | |
| 284 | return NULL; |
| 285 | } |
| 286 | EXPORT_SYMBOL_GPL(hsi_alloc_controller); |
| 287 | |
| 288 | /** |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 289 | * hsi_free_msg - Free an HSI message |
| 290 | * @msg: Pointer to the HSI message |
| 291 | * |
| 292 | * Client is responsible to free the buffers pointed by the scatterlists. |
| 293 | */ |
| 294 | void hsi_free_msg(struct hsi_msg *msg) |
| 295 | { |
| 296 | if (!msg) |
| 297 | return; |
| 298 | sg_free_table(&msg->sgt); |
| 299 | kfree(msg); |
| 300 | } |
| 301 | EXPORT_SYMBOL_GPL(hsi_free_msg); |
| 302 | |
| 303 | /** |
| 304 | * hsi_alloc_msg - Allocate an HSI message |
| 305 | * @nents: Number of memory entries |
| 306 | * @flags: Kernel allocation flags |
| 307 | * |
| 308 | * nents can be 0. This mainly makes sense for read transfer. |
| 309 | * In that case, HSI drivers will call the complete callback when |
| 310 | * there is data to be read without consuming it. |
| 311 | * |
| 312 | * Return NULL on failure or a pointer to an hsi_msg on success. |
| 313 | */ |
| 314 | struct hsi_msg *hsi_alloc_msg(unsigned int nents, gfp_t flags) |
| 315 | { |
| 316 | struct hsi_msg *msg; |
| 317 | int err; |
| 318 | |
| 319 | msg = kzalloc(sizeof(*msg), flags); |
| 320 | if (!msg) |
| 321 | return NULL; |
| 322 | |
| 323 | if (!nents) |
| 324 | return msg; |
| 325 | |
| 326 | err = sg_alloc_table(&msg->sgt, nents, flags); |
| 327 | if (unlikely(err)) { |
| 328 | kfree(msg); |
| 329 | msg = NULL; |
| 330 | } |
| 331 | |
| 332 | return msg; |
| 333 | } |
| 334 | EXPORT_SYMBOL_GPL(hsi_alloc_msg); |
| 335 | |
| 336 | /** |
| 337 | * hsi_async - Submit an HSI transfer to the controller |
| 338 | * @cl: HSI client sending the transfer |
| 339 | * @msg: The HSI transfer passed to controller |
| 340 | * |
| 341 | * The HSI message must have the channel, ttype, complete and destructor |
| 342 | * fields set beforehand. If nents > 0 then the client has to initialize |
| 343 | * also the scatterlists to point to the buffers to write to or read from. |
| 344 | * |
| 345 | * HSI controllers relay on pre-allocated buffers from their clients and they |
| 346 | * do not allocate buffers on their own. |
| 347 | * |
| 348 | * Once the HSI message transfer finishes, the HSI controller calls the |
| 349 | * complete callback with the status and actual_len fields of the HSI message |
| 350 | * updated. The complete callback can be called before returning from |
| 351 | * hsi_async. |
| 352 | * |
| 353 | * Returns -errno on failure or 0 on success |
| 354 | */ |
| 355 | int hsi_async(struct hsi_client *cl, struct hsi_msg *msg) |
| 356 | { |
| 357 | struct hsi_port *port = hsi_get_port(cl); |
| 358 | |
| 359 | if (!hsi_port_claimed(cl)) |
| 360 | return -EACCES; |
| 361 | |
| 362 | WARN_ON_ONCE(!msg->destructor || !msg->complete); |
| 363 | msg->cl = cl; |
| 364 | |
| 365 | return port->async(msg); |
| 366 | } |
| 367 | EXPORT_SYMBOL_GPL(hsi_async); |
| 368 | |
| 369 | /** |
| 370 | * hsi_claim_port - Claim the HSI client's port |
| 371 | * @cl: HSI client that wants to claim its port |
| 372 | * @share: Flag to indicate if the client wants to share the port or not. |
| 373 | * |
| 374 | * Returns -errno on failure, 0 on success. |
| 375 | */ |
| 376 | int hsi_claim_port(struct hsi_client *cl, unsigned int share) |
| 377 | { |
| 378 | struct hsi_port *port = hsi_get_port(cl); |
| 379 | int err = 0; |
| 380 | |
| 381 | mutex_lock(&port->lock); |
| 382 | if ((port->claimed) && (!port->shared || !share)) { |
| 383 | err = -EBUSY; |
| 384 | goto out; |
| 385 | } |
| 386 | if (!try_module_get(to_hsi_controller(port->device.parent)->owner)) { |
| 387 | err = -ENODEV; |
| 388 | goto out; |
| 389 | } |
| 390 | port->claimed++; |
| 391 | port->shared = !!share; |
| 392 | cl->pclaimed = 1; |
| 393 | out: |
| 394 | mutex_unlock(&port->lock); |
| 395 | |
| 396 | return err; |
| 397 | } |
| 398 | EXPORT_SYMBOL_GPL(hsi_claim_port); |
| 399 | |
| 400 | /** |
| 401 | * hsi_release_port - Release the HSI client's port |
| 402 | * @cl: HSI client which previously claimed its port |
| 403 | */ |
| 404 | void hsi_release_port(struct hsi_client *cl) |
| 405 | { |
| 406 | struct hsi_port *port = hsi_get_port(cl); |
| 407 | |
| 408 | mutex_lock(&port->lock); |
| 409 | /* Allow HW driver to do some cleanup */ |
| 410 | port->release(cl); |
| 411 | if (cl->pclaimed) |
| 412 | port->claimed--; |
| 413 | BUG_ON(port->claimed < 0); |
| 414 | cl->pclaimed = 0; |
| 415 | if (!port->claimed) |
| 416 | port->shared = 0; |
| 417 | module_put(to_hsi_controller(port->device.parent)->owner); |
| 418 | mutex_unlock(&port->lock); |
| 419 | } |
| 420 | EXPORT_SYMBOL_GPL(hsi_release_port); |
| 421 | |
Carlos Chinea | ec1c56f | 2012-04-11 10:55:53 +0300 | [diff] [blame] | 422 | static int hsi_event_notifier_call(struct notifier_block *nb, |
| 423 | unsigned long event, void *data __maybe_unused) |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 424 | { |
Carlos Chinea | ec1c56f | 2012-04-11 10:55:53 +0300 | [diff] [blame] | 425 | struct hsi_client *cl = container_of(nb, struct hsi_client, nb); |
| 426 | |
| 427 | (*cl->ehandler)(cl, event); |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
Carlos Chinea | ec1c56f | 2012-04-11 10:55:53 +0300 | [diff] [blame] | 432 | /** |
| 433 | * hsi_register_port_event - Register a client to receive port events |
| 434 | * @cl: HSI client that wants to receive port events |
Randy Dunlap | 8eae508 | 2013-03-01 11:11:47 -0800 | [diff] [blame] | 435 | * @handler: Event handler callback |
Carlos Chinea | ec1c56f | 2012-04-11 10:55:53 +0300 | [diff] [blame] | 436 | * |
| 437 | * Clients should register a callback to be able to receive |
| 438 | * events from the ports. Registration should happen after |
| 439 | * claiming the port. |
| 440 | * The handler can be called in interrupt context. |
| 441 | * |
| 442 | * Returns -errno on error, or 0 on success. |
| 443 | */ |
| 444 | int hsi_register_port_event(struct hsi_client *cl, |
| 445 | void (*handler)(struct hsi_client *, unsigned long)) |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 446 | { |
Carlos Chinea | ec1c56f | 2012-04-11 10:55:53 +0300 | [diff] [blame] | 447 | struct hsi_port *port = hsi_get_port(cl); |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 448 | |
Carlos Chinea | ec1c56f | 2012-04-11 10:55:53 +0300 | [diff] [blame] | 449 | if (!handler || cl->ehandler) |
| 450 | return -EINVAL; |
| 451 | if (!hsi_port_claimed(cl)) |
| 452 | return -EACCES; |
| 453 | cl->ehandler = handler; |
| 454 | cl->nb.notifier_call = hsi_event_notifier_call; |
| 455 | |
| 456 | return atomic_notifier_chain_register(&port->n_head, &cl->nb); |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 457 | } |
Carlos Chinea | ec1c56f | 2012-04-11 10:55:53 +0300 | [diff] [blame] | 458 | EXPORT_SYMBOL_GPL(hsi_register_port_event); |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 459 | |
Carlos Chinea | ec1c56f | 2012-04-11 10:55:53 +0300 | [diff] [blame] | 460 | /** |
| 461 | * hsi_unregister_port_event - Stop receiving port events for a client |
| 462 | * @cl: HSI client that wants to stop receiving port events |
| 463 | * |
| 464 | * Clients should call this function before releasing their associated |
| 465 | * port. |
| 466 | * |
| 467 | * Returns -errno on error, or 0 on success. |
| 468 | */ |
| 469 | int hsi_unregister_port_event(struct hsi_client *cl) |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 470 | { |
Carlos Chinea | ec1c56f | 2012-04-11 10:55:53 +0300 | [diff] [blame] | 471 | struct hsi_port *port = hsi_get_port(cl); |
| 472 | int err; |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 473 | |
Carlos Chinea | ec1c56f | 2012-04-11 10:55:53 +0300 | [diff] [blame] | 474 | WARN_ON(!hsi_port_claimed(cl)); |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 475 | |
Carlos Chinea | ec1c56f | 2012-04-11 10:55:53 +0300 | [diff] [blame] | 476 | err = atomic_notifier_chain_unregister(&port->n_head, &cl->nb); |
| 477 | if (!err) |
| 478 | cl->ehandler = NULL; |
| 479 | |
| 480 | return err; |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 481 | } |
Carlos Chinea | ec1c56f | 2012-04-11 10:55:53 +0300 | [diff] [blame] | 482 | EXPORT_SYMBOL_GPL(hsi_unregister_port_event); |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 483 | |
| 484 | /** |
| 485 | * hsi_event -Notifies clients about port events |
| 486 | * @port: Port where the event occurred |
| 487 | * @event: The event type |
| 488 | * |
| 489 | * Clients should not be concerned about wake line behavior. However, due |
| 490 | * to a race condition in HSI HW protocol, clients need to be notified |
| 491 | * about wake line changes, so they can implement a workaround for it. |
| 492 | * |
| 493 | * Events: |
| 494 | * HSI_EVENT_START_RX - Incoming wake line high |
| 495 | * HSI_EVENT_STOP_RX - Incoming wake line down |
Carlos Chinea | ec1c56f | 2012-04-11 10:55:53 +0300 | [diff] [blame] | 496 | * |
| 497 | * Returns -errno on error, or 0 on success. |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 498 | */ |
Carlos Chinea | ec1c56f | 2012-04-11 10:55:53 +0300 | [diff] [blame] | 499 | int hsi_event(struct hsi_port *port, unsigned long event) |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 500 | { |
Carlos Chinea | ec1c56f | 2012-04-11 10:55:53 +0300 | [diff] [blame] | 501 | return atomic_notifier_call_chain(&port->n_head, event, NULL); |
Carlos Chinea | a056ab8 | 2010-04-16 19:01:02 +0300 | [diff] [blame] | 502 | } |
| 503 | EXPORT_SYMBOL_GPL(hsi_event); |
| 504 | |
| 505 | static int __init hsi_init(void) |
| 506 | { |
| 507 | return bus_register(&hsi_bus_type); |
| 508 | } |
| 509 | postcore_initcall(hsi_init); |
| 510 | |
| 511 | static void __exit hsi_exit(void) |
| 512 | { |
| 513 | bus_unregister(&hsi_bus_type); |
| 514 | } |
| 515 | module_exit(hsi_exit); |
| 516 | |
| 517 | MODULE_AUTHOR("Carlos Chinea <carlos.chinea@nokia.com>"); |
| 518 | MODULE_DESCRIPTION("High-speed Synchronous Serial Interface (HSI) framework"); |
| 519 | MODULE_LICENSE("GPL v2"); |