Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 1 | /* |
| 2 | * V4L2 asynchronous subdevice registration API |
| 3 | * |
| 4 | * Copyright (C) 2012-2013, Guennadi Liakhovetski <g.liakhovetski@gmx.de> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/err.h> |
| 13 | #include <linux/i2c.h> |
| 14 | #include <linux/list.h> |
Tomasz Figa | 758d90e | 2017-06-19 00:53:43 -0300 | [diff] [blame] | 15 | #include <linux/mm.h> |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/mutex.h> |
Sakari Ailus | ecdf0cf | 2016-08-16 06:54:59 -0300 | [diff] [blame] | 18 | #include <linux/of.h> |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/types.h> |
| 22 | |
| 23 | #include <media/v4l2-async.h> |
| 24 | #include <media/v4l2-device.h> |
Sakari Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 25 | #include <media/v4l2-fwnode.h> |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 26 | #include <media/v4l2-subdev.h> |
| 27 | |
Sakari Ailus | ddddc18 | 2017-09-01 08:27:32 -0400 | [diff] [blame] | 28 | static int v4l2_async_notifier_call_bound(struct v4l2_async_notifier *n, |
| 29 | struct v4l2_subdev *subdev, |
| 30 | struct v4l2_async_subdev *asd) |
| 31 | { |
| 32 | if (!n->ops || !n->ops->bound) |
| 33 | return 0; |
| 34 | |
| 35 | return n->ops->bound(n, subdev, asd); |
| 36 | } |
| 37 | |
| 38 | static void v4l2_async_notifier_call_unbind(struct v4l2_async_notifier *n, |
| 39 | struct v4l2_subdev *subdev, |
| 40 | struct v4l2_async_subdev *asd) |
| 41 | { |
| 42 | if (!n->ops || !n->ops->unbind) |
| 43 | return; |
| 44 | |
| 45 | n->ops->unbind(n, subdev, asd); |
| 46 | } |
| 47 | |
| 48 | static int v4l2_async_notifier_call_complete(struct v4l2_async_notifier *n) |
| 49 | { |
| 50 | if (!n->ops || !n->ops->complete) |
| 51 | return 0; |
| 52 | |
| 53 | return n->ops->complete(n); |
| 54 | } |
| 55 | |
Sakari Ailus | 8621765 | 2015-06-11 12:18:01 -0700 | [diff] [blame] | 56 | static bool match_i2c(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd) |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 57 | { |
Guennadi Liakhovetski | fe05e14 | 2013-06-24 05:13:51 -0300 | [diff] [blame] | 58 | #if IS_ENABLED(CONFIG_I2C) |
Sakari Ailus | 8621765 | 2015-06-11 12:18:01 -0700 | [diff] [blame] | 59 | struct i2c_client *client = i2c_verify_client(sd->dev); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 60 | return client && |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 61 | asd->match.i2c.adapter_id == client->adapter->nr && |
| 62 | asd->match.i2c.address == client->addr; |
Guennadi Liakhovetski | fe05e14 | 2013-06-24 05:13:51 -0300 | [diff] [blame] | 63 | #else |
| 64 | return false; |
| 65 | #endif |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 66 | } |
| 67 | |
Sakari Ailus | 8621765 | 2015-06-11 12:18:01 -0700 | [diff] [blame] | 68 | static bool match_devname(struct v4l2_subdev *sd, |
| 69 | struct v4l2_async_subdev *asd) |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 70 | { |
Sakari Ailus | 8621765 | 2015-06-11 12:18:01 -0700 | [diff] [blame] | 71 | return !strcmp(asd->match.device_name.name, dev_name(sd->dev)); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 72 | } |
| 73 | |
Sakari Ailus | ecdf0cf | 2016-08-16 06:54:59 -0300 | [diff] [blame] | 74 | static bool match_fwnode(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd) |
| 75 | { |
Rob Herring | 12f9286 | 2017-07-20 18:06:22 -0400 | [diff] [blame] | 76 | return sd->fwnode == asd->match.fwnode.fwnode; |
Sakari Ailus | ecdf0cf | 2016-08-16 06:54:59 -0300 | [diff] [blame] | 77 | } |
| 78 | |
Sakari Ailus | 8621765 | 2015-06-11 12:18:01 -0700 | [diff] [blame] | 79 | static bool match_custom(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd) |
| 80 | { |
| 81 | if (!asd->match.custom.match) |
| 82 | /* Match always */ |
| 83 | return true; |
| 84 | |
| 85 | return asd->match.custom.match(sd->dev, asd); |
Sylwester Nawrocki | e7359f8 | 2013-07-19 12:21:29 -0300 | [diff] [blame] | 86 | } |
| 87 | |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 88 | static LIST_HEAD(subdev_list); |
| 89 | static LIST_HEAD(notifier_list); |
| 90 | static DEFINE_MUTEX(list_lock); |
| 91 | |
Sakari Ailus | c8114d9 | 2017-09-04 12:44:39 -0400 | [diff] [blame] | 92 | static struct v4l2_async_subdev *v4l2_async_find_match( |
| 93 | struct v4l2_async_notifier *notifier, struct v4l2_subdev *sd) |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 94 | { |
Sakari Ailus | 8621765 | 2015-06-11 12:18:01 -0700 | [diff] [blame] | 95 | bool (*match)(struct v4l2_subdev *, struct v4l2_async_subdev *); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 96 | struct v4l2_async_subdev *asd; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 97 | |
| 98 | list_for_each_entry(asd, ¬ifier->waiting, list) { |
| 99 | /* bus_type has been verified valid before */ |
Sylwester Nawrocki | cfca764 | 2013-07-19 12:14:46 -0300 | [diff] [blame] | 100 | switch (asd->match_type) { |
| 101 | case V4L2_ASYNC_MATCH_CUSTOM: |
Sakari Ailus | 8621765 | 2015-06-11 12:18:01 -0700 | [diff] [blame] | 102 | match = match_custom; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 103 | break; |
Sylwester Nawrocki | cfca764 | 2013-07-19 12:14:46 -0300 | [diff] [blame] | 104 | case V4L2_ASYNC_MATCH_DEVNAME: |
| 105 | match = match_devname; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 106 | break; |
Sylwester Nawrocki | cfca764 | 2013-07-19 12:14:46 -0300 | [diff] [blame] | 107 | case V4L2_ASYNC_MATCH_I2C: |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 108 | match = match_i2c; |
| 109 | break; |
Sakari Ailus | ecdf0cf | 2016-08-16 06:54:59 -0300 | [diff] [blame] | 110 | case V4L2_ASYNC_MATCH_FWNODE: |
| 111 | match = match_fwnode; |
| 112 | break; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 113 | default: |
| 114 | /* Cannot happen, unless someone breaks us */ |
| 115 | WARN_ON(true); |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | /* match cannot be NULL here */ |
Sakari Ailus | 8621765 | 2015-06-11 12:18:01 -0700 | [diff] [blame] | 120 | if (match(sd, asd)) |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 121 | return asd; |
| 122 | } |
| 123 | |
| 124 | return NULL; |
| 125 | } |
| 126 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 127 | /* Find the sub-device notifier registered by a sub-device driver. */ |
| 128 | static struct v4l2_async_notifier *v4l2_async_find_subdev_notifier( |
| 129 | struct v4l2_subdev *sd) |
| 130 | { |
| 131 | struct v4l2_async_notifier *n; |
| 132 | |
| 133 | list_for_each_entry(n, ¬ifier_list, list) |
| 134 | if (n->sd == sd) |
| 135 | return n; |
| 136 | |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | /* Get v4l2_device related to the notifier if one can be found. */ |
| 141 | static struct v4l2_device *v4l2_async_notifier_find_v4l2_dev( |
| 142 | struct v4l2_async_notifier *notifier) |
| 143 | { |
| 144 | while (notifier->parent) |
| 145 | notifier = notifier->parent; |
| 146 | |
| 147 | return notifier->v4l2_dev; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Return true if all child sub-device notifiers are complete, false otherwise. |
| 152 | */ |
| 153 | static bool v4l2_async_notifier_can_complete( |
| 154 | struct v4l2_async_notifier *notifier) |
| 155 | { |
| 156 | struct v4l2_subdev *sd; |
| 157 | |
| 158 | if (!list_empty(¬ifier->waiting)) |
| 159 | return false; |
| 160 | |
| 161 | list_for_each_entry(sd, ¬ifier->done, async_list) { |
| 162 | struct v4l2_async_notifier *subdev_notifier = |
| 163 | v4l2_async_find_subdev_notifier(sd); |
| 164 | |
| 165 | if (subdev_notifier && |
| 166 | !v4l2_async_notifier_can_complete(subdev_notifier)) |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Complete the master notifier if possible. This is done when all async |
| 175 | * sub-devices have been bound; v4l2_device is also available then. |
| 176 | */ |
| 177 | static int v4l2_async_notifier_try_complete( |
| 178 | struct v4l2_async_notifier *notifier) |
| 179 | { |
| 180 | /* Quick check whether there are still more sub-devices here. */ |
| 181 | if (!list_empty(¬ifier->waiting)) |
| 182 | return 0; |
| 183 | |
| 184 | /* Check the entire notifier tree; find the root notifier first. */ |
| 185 | while (notifier->parent) |
| 186 | notifier = notifier->parent; |
| 187 | |
| 188 | /* This is root if it has v4l2_dev. */ |
| 189 | if (!notifier->v4l2_dev) |
| 190 | return 0; |
| 191 | |
| 192 | /* Is everything ready? */ |
| 193 | if (!v4l2_async_notifier_can_complete(notifier)) |
| 194 | return 0; |
| 195 | |
| 196 | return v4l2_async_notifier_call_complete(notifier); |
| 197 | } |
| 198 | |
| 199 | static int v4l2_async_notifier_try_all_subdevs( |
| 200 | struct v4l2_async_notifier *notifier); |
| 201 | |
Sakari Ailus | c8114d9 | 2017-09-04 12:44:39 -0400 | [diff] [blame] | 202 | static int v4l2_async_match_notify(struct v4l2_async_notifier *notifier, |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 203 | struct v4l2_device *v4l2_dev, |
Sakari Ailus | c8114d9 | 2017-09-04 12:44:39 -0400 | [diff] [blame] | 204 | struct v4l2_subdev *sd, |
| 205 | struct v4l2_async_subdev *asd) |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 206 | { |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 207 | struct v4l2_async_notifier *subdev_notifier; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 208 | int ret; |
| 209 | |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 210 | ret = v4l2_device_register_subdev(v4l2_dev, sd); |
Sakari Ailus | ddddc18 | 2017-09-01 08:27:32 -0400 | [diff] [blame] | 211 | if (ret < 0) |
| 212 | return ret; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 213 | |
Sakari Ailus | 24def9b | 2017-07-17 10:04:20 -0400 | [diff] [blame] | 214 | ret = v4l2_async_notifier_call_bound(notifier, sd, asd); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 215 | if (ret < 0) { |
Sakari Ailus | 24def9b | 2017-07-17 10:04:20 -0400 | [diff] [blame] | 216 | v4l2_device_unregister_subdev(sd); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 217 | return ret; |
| 218 | } |
| 219 | |
Tuukka Toivonen | 47b037a | 2017-01-27 08:32:56 -0200 | [diff] [blame] | 220 | /* Remove from the waiting list */ |
| 221 | list_del(&asd->list); |
| 222 | sd->asd = asd; |
| 223 | sd->notifier = notifier; |
| 224 | |
| 225 | /* Move from the global subdevice list to notifier's done */ |
| 226 | list_move(&sd->async_list, ¬ifier->done); |
| 227 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 228 | /* |
| 229 | * See if the sub-device has a notifier. If not, return here. |
| 230 | */ |
| 231 | subdev_notifier = v4l2_async_find_subdev_notifier(sd); |
| 232 | if (!subdev_notifier || subdev_notifier->parent) |
| 233 | return 0; |
| 234 | |
| 235 | /* |
| 236 | * Proceed with checking for the sub-device notifier's async |
| 237 | * sub-devices, and return the result. The error will be handled by the |
| 238 | * caller. |
| 239 | */ |
| 240 | subdev_notifier->parent = notifier; |
| 241 | |
| 242 | return v4l2_async_notifier_try_all_subdevs(subdev_notifier); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 243 | } |
| 244 | |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 245 | /* Test all async sub-devices in a notifier for a match. */ |
| 246 | static int v4l2_async_notifier_try_all_subdevs( |
| 247 | struct v4l2_async_notifier *notifier) |
| 248 | { |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 249 | struct v4l2_device *v4l2_dev = |
| 250 | v4l2_async_notifier_find_v4l2_dev(notifier); |
| 251 | struct v4l2_subdev *sd; |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 252 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 253 | if (!v4l2_dev) |
| 254 | return 0; |
| 255 | |
| 256 | again: |
| 257 | list_for_each_entry(sd, &subdev_list, async_list) { |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 258 | struct v4l2_async_subdev *asd; |
| 259 | int ret; |
| 260 | |
| 261 | asd = v4l2_async_find_match(notifier, sd); |
| 262 | if (!asd) |
| 263 | continue; |
| 264 | |
| 265 | ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd); |
| 266 | if (ret < 0) |
| 267 | return ret; |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 268 | |
| 269 | /* |
| 270 | * v4l2_async_match_notify() may lead to registering a |
| 271 | * new notifier and thus changing the async subdevs |
| 272 | * list. In order to proceed safely from here, restart |
| 273 | * parsing the list from the beginning. |
| 274 | */ |
| 275 | goto again; |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 281 | static void v4l2_async_cleanup(struct v4l2_subdev *sd) |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 282 | { |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 283 | v4l2_device_unregister_subdev(sd); |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 284 | /* Subdevice driver will reprobe and put the subdev back onto the list */ |
| 285 | list_del_init(&sd->async_list); |
| 286 | sd->asd = NULL; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 287 | } |
| 288 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 289 | /* Unbind all sub-devices in the notifier tree. */ |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 290 | static void v4l2_async_notifier_unbind_all_subdevs( |
| 291 | struct v4l2_async_notifier *notifier) |
| 292 | { |
| 293 | struct v4l2_subdev *sd, *tmp; |
| 294 | |
| 295 | list_for_each_entry_safe(sd, tmp, ¬ifier->done, async_list) { |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 296 | struct v4l2_async_notifier *subdev_notifier = |
| 297 | v4l2_async_find_subdev_notifier(sd); |
| 298 | |
| 299 | if (subdev_notifier) |
| 300 | v4l2_async_notifier_unbind_all_subdevs(subdev_notifier); |
| 301 | |
Sakari Ailus | ddddc18 | 2017-09-01 08:27:32 -0400 | [diff] [blame] | 302 | v4l2_async_notifier_call_unbind(notifier, sd, sd->asd); |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 303 | v4l2_async_cleanup(sd); |
| 304 | |
| 305 | list_move(&sd->async_list, &subdev_list); |
| 306 | } |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 307 | |
| 308 | notifier->parent = NULL; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 309 | } |
| 310 | |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 311 | /* See if an fwnode can be found in a notifier's lists. */ |
| 312 | static bool __v4l2_async_notifier_fwnode_has_async_subdev( |
| 313 | struct v4l2_async_notifier *notifier, struct fwnode_handle *fwnode) |
| 314 | { |
| 315 | struct v4l2_async_subdev *asd; |
| 316 | struct v4l2_subdev *sd; |
| 317 | |
| 318 | list_for_each_entry(asd, ¬ifier->waiting, list) { |
| 319 | if (asd->match_type != V4L2_ASYNC_MATCH_FWNODE) |
| 320 | continue; |
| 321 | |
| 322 | if (asd->match.fwnode.fwnode == fwnode) |
| 323 | return true; |
| 324 | } |
| 325 | |
| 326 | list_for_each_entry(sd, ¬ifier->done, async_list) { |
| 327 | if (WARN_ON(!sd->asd)) |
| 328 | continue; |
| 329 | |
| 330 | if (sd->asd->match_type != V4L2_ASYNC_MATCH_FWNODE) |
| 331 | continue; |
| 332 | |
| 333 | if (sd->asd->match.fwnode.fwnode == fwnode) |
| 334 | return true; |
| 335 | } |
| 336 | |
| 337 | return false; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Find out whether an async sub-device was set up for an fwnode already or |
| 342 | * whether it exists in a given notifier before @this_index. |
| 343 | */ |
| 344 | static bool v4l2_async_notifier_fwnode_has_async_subdev( |
| 345 | struct v4l2_async_notifier *notifier, struct fwnode_handle *fwnode, |
| 346 | unsigned int this_index) |
| 347 | { |
| 348 | unsigned int j; |
| 349 | |
| 350 | lockdep_assert_held(&list_lock); |
| 351 | |
| 352 | /* Check that an fwnode is not being added more than once. */ |
| 353 | for (j = 0; j < this_index; j++) { |
| 354 | struct v4l2_async_subdev *asd = notifier->subdevs[this_index]; |
| 355 | struct v4l2_async_subdev *other_asd = notifier->subdevs[j]; |
| 356 | |
| 357 | if (other_asd->match_type == V4L2_ASYNC_MATCH_FWNODE && |
| 358 | asd->match.fwnode.fwnode == |
| 359 | other_asd->match.fwnode.fwnode) |
| 360 | return true; |
| 361 | } |
| 362 | |
| 363 | /* Check than an fwnode did not exist in other notifiers. */ |
| 364 | list_for_each_entry(notifier, ¬ifier_list, list) |
| 365 | if (__v4l2_async_notifier_fwnode_has_async_subdev( |
| 366 | notifier, fwnode)) |
| 367 | return true; |
| 368 | |
| 369 | return false; |
| 370 | } |
| 371 | |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 372 | static int __v4l2_async_notifier_register(struct v4l2_async_notifier *notifier) |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 373 | { |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 374 | struct device *dev = |
| 375 | notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 376 | struct v4l2_async_subdev *asd; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 377 | int ret; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 378 | int i; |
| 379 | |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 380 | if (notifier->num_subdevs > V4L2_MAX_SUBDEVS) |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 381 | return -EINVAL; |
| 382 | |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 383 | INIT_LIST_HEAD(¬ifier->waiting); |
| 384 | INIT_LIST_HEAD(¬ifier->done); |
| 385 | |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 386 | mutex_lock(&list_lock); |
| 387 | |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 388 | for (i = 0; i < notifier->num_subdevs; i++) { |
Sylwester Nawrocki | e8419d0 | 2013-07-19 12:31:10 -0300 | [diff] [blame] | 389 | asd = notifier->subdevs[i]; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 390 | |
Sylwester Nawrocki | cfca764 | 2013-07-19 12:14:46 -0300 | [diff] [blame] | 391 | switch (asd->match_type) { |
| 392 | case V4L2_ASYNC_MATCH_CUSTOM: |
| 393 | case V4L2_ASYNC_MATCH_DEVNAME: |
| 394 | case V4L2_ASYNC_MATCH_I2C: |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 395 | break; |
Sakari Ailus | ecdf0cf | 2016-08-16 06:54:59 -0300 | [diff] [blame] | 396 | case V4L2_ASYNC_MATCH_FWNODE: |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 397 | if (v4l2_async_notifier_fwnode_has_async_subdev( |
| 398 | notifier, asd->match.fwnode.fwnode, i)) { |
| 399 | dev_err(dev, |
| 400 | "fwnode has already been registered or in notifier's subdev list\n"); |
| 401 | ret = -EEXIST; |
| 402 | goto err_unlock; |
| 403 | } |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 404 | break; |
| 405 | default: |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 406 | dev_err(dev, "Invalid match type %u on %p\n", |
Sylwester Nawrocki | cfca764 | 2013-07-19 12:14:46 -0300 | [diff] [blame] | 407 | asd->match_type, asd); |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 408 | ret = -EINVAL; |
| 409 | goto err_unlock; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 410 | } |
| 411 | list_add_tail(&asd->list, ¬ifier->waiting); |
| 412 | } |
| 413 | |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 414 | ret = v4l2_async_notifier_try_all_subdevs(notifier); |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 415 | if (ret < 0) |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 416 | goto err_unbind; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 417 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 418 | ret = v4l2_async_notifier_try_complete(notifier); |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 419 | if (ret < 0) |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 420 | goto err_unbind; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 421 | |
Tuukka Toivonen | 47b037a | 2017-01-27 08:32:56 -0200 | [diff] [blame] | 422 | /* Keep also completed notifiers on the list */ |
| 423 | list_add(¬ifier->list, ¬ifier_list); |
| 424 | |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 425 | mutex_unlock(&list_lock); |
| 426 | |
| 427 | return 0; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 428 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 429 | err_unbind: |
| 430 | /* |
| 431 | * On failure, unbind all sub-devices registered through this notifier. |
| 432 | */ |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 433 | v4l2_async_notifier_unbind_all_subdevs(notifier); |
| 434 | |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 435 | err_unlock: |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 436 | mutex_unlock(&list_lock); |
| 437 | |
| 438 | return ret; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 439 | } |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 440 | |
| 441 | int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev, |
| 442 | struct v4l2_async_notifier *notifier) |
| 443 | { |
| 444 | int ret; |
| 445 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 446 | if (WARN_ON(!v4l2_dev || notifier->sd)) |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 447 | return -EINVAL; |
| 448 | |
| 449 | notifier->v4l2_dev = v4l2_dev; |
| 450 | |
| 451 | ret = __v4l2_async_notifier_register(notifier); |
| 452 | if (ret) |
| 453 | notifier->v4l2_dev = NULL; |
| 454 | |
| 455 | return ret; |
| 456 | } |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 457 | EXPORT_SYMBOL(v4l2_async_notifier_register); |
| 458 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 459 | int v4l2_async_subdev_notifier_register(struct v4l2_subdev *sd, |
| 460 | struct v4l2_async_notifier *notifier) |
| 461 | { |
| 462 | int ret; |
| 463 | |
| 464 | if (WARN_ON(!sd || notifier->v4l2_dev)) |
| 465 | return -EINVAL; |
| 466 | |
| 467 | notifier->sd = sd; |
| 468 | |
| 469 | ret = __v4l2_async_notifier_register(notifier); |
| 470 | if (ret) |
| 471 | notifier->sd = NULL; |
| 472 | |
| 473 | return ret; |
| 474 | } |
| 475 | EXPORT_SYMBOL(v4l2_async_subdev_notifier_register); |
| 476 | |
Sakari Ailus | aef69d5 | 2017-09-24 18:47:44 -0400 | [diff] [blame] | 477 | static void __v4l2_async_notifier_unregister( |
| 478 | struct v4l2_async_notifier *notifier) |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 479 | { |
Sakari Ailus | aef69d5 | 2017-09-24 18:47:44 -0400 | [diff] [blame] | 480 | if (!notifier || (!notifier->v4l2_dev && !notifier->sd)) |
Laurent Pinchart | 8e3fbfe | 2013-07-03 07:49:06 -0300 | [diff] [blame] | 481 | return; |
| 482 | |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 483 | v4l2_async_notifier_unbind_all_subdevs(notifier); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 484 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 485 | notifier->sd = NULL; |
Laurent Pinchart | 8e3fbfe | 2013-07-03 07:49:06 -0300 | [diff] [blame] | 486 | notifier->v4l2_dev = NULL; |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 487 | |
| 488 | list_del(¬ifier->list); |
Sakari Ailus | aef69d5 | 2017-09-24 18:47:44 -0400 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier) |
| 492 | { |
| 493 | mutex_lock(&list_lock); |
| 494 | |
| 495 | __v4l2_async_notifier_unregister(notifier); |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 496 | |
| 497 | mutex_unlock(&list_lock); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 498 | } |
| 499 | EXPORT_SYMBOL(v4l2_async_notifier_unregister); |
| 500 | |
Sakari Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 501 | void v4l2_async_notifier_cleanup(struct v4l2_async_notifier *notifier) |
| 502 | { |
| 503 | unsigned int i; |
| 504 | |
Niklas Söderlund | 1453ad8 | 2017-11-03 22:25:56 -0400 | [diff] [blame] | 505 | if (!notifier || !notifier->max_subdevs) |
Sakari Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 506 | return; |
| 507 | |
| 508 | for (i = 0; i < notifier->num_subdevs; i++) { |
| 509 | struct v4l2_async_subdev *asd = notifier->subdevs[i]; |
| 510 | |
| 511 | switch (asd->match_type) { |
| 512 | case V4L2_ASYNC_MATCH_FWNODE: |
| 513 | fwnode_handle_put(asd->match.fwnode.fwnode); |
| 514 | break; |
| 515 | default: |
| 516 | WARN_ON_ONCE(true); |
| 517 | break; |
| 518 | } |
| 519 | |
| 520 | kfree(asd); |
| 521 | } |
| 522 | |
| 523 | notifier->max_subdevs = 0; |
| 524 | notifier->num_subdevs = 0; |
| 525 | |
| 526 | kvfree(notifier->subdevs); |
| 527 | notifier->subdevs = NULL; |
| 528 | } |
| 529 | EXPORT_SYMBOL_GPL(v4l2_async_notifier_cleanup); |
| 530 | |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 531 | int v4l2_async_register_subdev(struct v4l2_subdev *sd) |
| 532 | { |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 533 | struct v4l2_async_notifier *subdev_notifier; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 534 | struct v4l2_async_notifier *notifier; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 535 | int ret; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 536 | |
Sakari Ailus | 8621765 | 2015-06-11 12:18:01 -0700 | [diff] [blame] | 537 | /* |
| 538 | * No reference taken. The reference is held by the device |
| 539 | * (struct v4l2_subdev.dev), and async sub-device does not |
| 540 | * exist independently of the device at any point of time. |
| 541 | */ |
Sakari Ailus | 859969b | 2016-08-26 20:17:25 -0300 | [diff] [blame] | 542 | if (!sd->fwnode && sd->dev) |
| 543 | sd->fwnode = dev_fwnode(sd->dev); |
Sakari Ailus | 8621765 | 2015-06-11 12:18:01 -0700 | [diff] [blame] | 544 | |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 545 | mutex_lock(&list_lock); |
| 546 | |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 547 | INIT_LIST_HEAD(&sd->async_list); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 548 | |
| 549 | list_for_each_entry(notifier, ¬ifier_list, list) { |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 550 | struct v4l2_device *v4l2_dev = |
| 551 | v4l2_async_notifier_find_v4l2_dev(notifier); |
| 552 | struct v4l2_async_subdev *asd; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 553 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 554 | if (!v4l2_dev) |
| 555 | continue; |
| 556 | |
| 557 | asd = v4l2_async_find_match(notifier, sd); |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 558 | if (!asd) |
| 559 | continue; |
| 560 | |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 561 | ret = v4l2_async_match_notify(notifier, notifier->v4l2_dev, sd, |
| 562 | asd); |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 563 | if (ret) |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 564 | goto err_unbind; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 565 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 566 | ret = v4l2_async_notifier_try_complete(notifier); |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 567 | if (ret) |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 568 | goto err_unbind; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 569 | |
| 570 | goto out_unlock; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /* None matched, wait for hot-plugging */ |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 574 | list_add(&sd->async_list, &subdev_list); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 575 | |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 576 | out_unlock: |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 577 | mutex_unlock(&list_lock); |
| 578 | |
| 579 | return 0; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 580 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 581 | err_unbind: |
| 582 | /* |
| 583 | * Complete failed. Unbind the sub-devices bound through registering |
| 584 | * this async sub-device. |
| 585 | */ |
| 586 | subdev_notifier = v4l2_async_find_subdev_notifier(sd); |
| 587 | if (subdev_notifier) |
| 588 | v4l2_async_notifier_unbind_all_subdevs(subdev_notifier); |
| 589 | |
| 590 | if (sd->asd) |
| 591 | v4l2_async_notifier_call_unbind(notifier, sd, sd->asd); |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 592 | v4l2_async_cleanup(sd); |
| 593 | |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 594 | mutex_unlock(&list_lock); |
| 595 | |
| 596 | return ret; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 597 | } |
| 598 | EXPORT_SYMBOL(v4l2_async_register_subdev); |
| 599 | |
| 600 | void v4l2_async_unregister_subdev(struct v4l2_subdev *sd) |
| 601 | { |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 602 | mutex_lock(&list_lock); |
| 603 | |
Sakari Ailus | aef69d5 | 2017-09-24 18:47:44 -0400 | [diff] [blame] | 604 | __v4l2_async_notifier_unregister(sd->subdev_notifier); |
| 605 | v4l2_async_notifier_cleanup(sd->subdev_notifier); |
| 606 | kfree(sd->subdev_notifier); |
| 607 | sd->subdev_notifier = NULL; |
| 608 | |
Sakari Ailus | 7fc4fdb | 2017-10-03 02:26:32 -0400 | [diff] [blame] | 609 | if (sd->asd) { |
| 610 | struct v4l2_async_notifier *notifier = sd->notifier; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 611 | |
Sakari Ailus | 7fc4fdb | 2017-10-03 02:26:32 -0400 | [diff] [blame] | 612 | list_add(&sd->asd->list, ¬ifier->waiting); |
| 613 | |
Sakari Ailus | ddddc18 | 2017-09-01 08:27:32 -0400 | [diff] [blame] | 614 | v4l2_async_notifier_call_unbind(notifier, sd, sd->asd); |
Sakari Ailus | 7fc4fdb | 2017-10-03 02:26:32 -0400 | [diff] [blame] | 615 | } |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 616 | |
Niklas Söderlund | 633d185 | 2017-10-02 16:16:52 -0400 | [diff] [blame] | 617 | v4l2_async_cleanup(sd); |
| 618 | |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 619 | mutex_unlock(&list_lock); |
| 620 | } |
| 621 | EXPORT_SYMBOL(v4l2_async_unregister_subdev); |