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 | { |
Mauro Carvalho Chehab | 4e48afe | 2017-09-27 10:12:00 -0400 | [diff] [blame] | 71 | return !strcmp(asd->match.device_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 | { |
Mauro Carvalho Chehab | 4e48afe | 2017-09-27 10:12:00 -0400 | [diff] [blame] | 76 | return sd->fwnode == asd->match.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 | |
Steve Longerbeam | a6e7003 | 2018-09-29 15:54:05 -0400 | [diff] [blame] | 127 | /* Compare two async sub-device descriptors for equivalence */ |
| 128 | static bool asd_equal(struct v4l2_async_subdev *asd_x, |
| 129 | struct v4l2_async_subdev *asd_y) |
| 130 | { |
| 131 | if (asd_x->match_type != asd_y->match_type) |
| 132 | return false; |
| 133 | |
| 134 | switch (asd_x->match_type) { |
| 135 | case V4L2_ASYNC_MATCH_DEVNAME: |
| 136 | return strcmp(asd_x->match.device_name, |
| 137 | asd_y->match.device_name) == 0; |
| 138 | case V4L2_ASYNC_MATCH_I2C: |
| 139 | return asd_x->match.i2c.adapter_id == |
| 140 | asd_y->match.i2c.adapter_id && |
| 141 | asd_x->match.i2c.address == |
| 142 | asd_y->match.i2c.address; |
| 143 | case V4L2_ASYNC_MATCH_FWNODE: |
| 144 | return asd_x->match.fwnode == asd_y->match.fwnode; |
| 145 | default: |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | return false; |
| 150 | } |
| 151 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 152 | /* Find the sub-device notifier registered by a sub-device driver. */ |
| 153 | static struct v4l2_async_notifier *v4l2_async_find_subdev_notifier( |
| 154 | struct v4l2_subdev *sd) |
| 155 | { |
| 156 | struct v4l2_async_notifier *n; |
| 157 | |
| 158 | list_for_each_entry(n, ¬ifier_list, list) |
| 159 | if (n->sd == sd) |
| 160 | return n; |
| 161 | |
| 162 | return NULL; |
| 163 | } |
| 164 | |
| 165 | /* Get v4l2_device related to the notifier if one can be found. */ |
| 166 | static struct v4l2_device *v4l2_async_notifier_find_v4l2_dev( |
| 167 | struct v4l2_async_notifier *notifier) |
| 168 | { |
| 169 | while (notifier->parent) |
| 170 | notifier = notifier->parent; |
| 171 | |
| 172 | return notifier->v4l2_dev; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Return true if all child sub-device notifiers are complete, false otherwise. |
| 177 | */ |
| 178 | static bool v4l2_async_notifier_can_complete( |
| 179 | struct v4l2_async_notifier *notifier) |
| 180 | { |
| 181 | struct v4l2_subdev *sd; |
| 182 | |
| 183 | if (!list_empty(¬ifier->waiting)) |
| 184 | return false; |
| 185 | |
| 186 | list_for_each_entry(sd, ¬ifier->done, async_list) { |
| 187 | struct v4l2_async_notifier *subdev_notifier = |
| 188 | v4l2_async_find_subdev_notifier(sd); |
| 189 | |
| 190 | if (subdev_notifier && |
| 191 | !v4l2_async_notifier_can_complete(subdev_notifier)) |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Complete the master notifier if possible. This is done when all async |
| 200 | * sub-devices have been bound; v4l2_device is also available then. |
| 201 | */ |
| 202 | static int v4l2_async_notifier_try_complete( |
| 203 | struct v4l2_async_notifier *notifier) |
| 204 | { |
| 205 | /* Quick check whether there are still more sub-devices here. */ |
| 206 | if (!list_empty(¬ifier->waiting)) |
| 207 | return 0; |
| 208 | |
| 209 | /* Check the entire notifier tree; find the root notifier first. */ |
| 210 | while (notifier->parent) |
| 211 | notifier = notifier->parent; |
| 212 | |
| 213 | /* This is root if it has v4l2_dev. */ |
| 214 | if (!notifier->v4l2_dev) |
| 215 | return 0; |
| 216 | |
| 217 | /* Is everything ready? */ |
| 218 | if (!v4l2_async_notifier_can_complete(notifier)) |
| 219 | return 0; |
| 220 | |
| 221 | return v4l2_async_notifier_call_complete(notifier); |
| 222 | } |
| 223 | |
| 224 | static int v4l2_async_notifier_try_all_subdevs( |
| 225 | struct v4l2_async_notifier *notifier); |
| 226 | |
Sakari Ailus | c8114d9 | 2017-09-04 12:44:39 -0400 | [diff] [blame] | 227 | static int v4l2_async_match_notify(struct v4l2_async_notifier *notifier, |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 228 | struct v4l2_device *v4l2_dev, |
Sakari Ailus | c8114d9 | 2017-09-04 12:44:39 -0400 | [diff] [blame] | 229 | struct v4l2_subdev *sd, |
| 230 | struct v4l2_async_subdev *asd) |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 231 | { |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 232 | struct v4l2_async_notifier *subdev_notifier; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 233 | int ret; |
| 234 | |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 235 | ret = v4l2_device_register_subdev(v4l2_dev, sd); |
Sakari Ailus | ddddc18 | 2017-09-01 08:27:32 -0400 | [diff] [blame] | 236 | if (ret < 0) |
| 237 | return ret; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 238 | |
Sakari Ailus | 24def9b | 2017-07-17 10:04:20 -0400 | [diff] [blame] | 239 | ret = v4l2_async_notifier_call_bound(notifier, sd, asd); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 240 | if (ret < 0) { |
Sakari Ailus | 24def9b | 2017-07-17 10:04:20 -0400 | [diff] [blame] | 241 | v4l2_device_unregister_subdev(sd); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 242 | return ret; |
| 243 | } |
| 244 | |
Tuukka Toivonen | 47b037a | 2017-01-27 08:32:56 -0200 | [diff] [blame] | 245 | /* Remove from the waiting list */ |
| 246 | list_del(&asd->list); |
| 247 | sd->asd = asd; |
| 248 | sd->notifier = notifier; |
| 249 | |
| 250 | /* Move from the global subdevice list to notifier's done */ |
| 251 | list_move(&sd->async_list, ¬ifier->done); |
| 252 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 253 | /* |
| 254 | * See if the sub-device has a notifier. If not, return here. |
| 255 | */ |
| 256 | subdev_notifier = v4l2_async_find_subdev_notifier(sd); |
| 257 | if (!subdev_notifier || subdev_notifier->parent) |
| 258 | return 0; |
| 259 | |
| 260 | /* |
| 261 | * Proceed with checking for the sub-device notifier's async |
| 262 | * sub-devices, and return the result. The error will be handled by the |
| 263 | * caller. |
| 264 | */ |
| 265 | subdev_notifier->parent = notifier; |
| 266 | |
| 267 | return v4l2_async_notifier_try_all_subdevs(subdev_notifier); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 268 | } |
| 269 | |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 270 | /* Test all async sub-devices in a notifier for a match. */ |
| 271 | static int v4l2_async_notifier_try_all_subdevs( |
| 272 | struct v4l2_async_notifier *notifier) |
| 273 | { |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 274 | struct v4l2_device *v4l2_dev = |
| 275 | v4l2_async_notifier_find_v4l2_dev(notifier); |
| 276 | struct v4l2_subdev *sd; |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 277 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 278 | if (!v4l2_dev) |
| 279 | return 0; |
| 280 | |
| 281 | again: |
| 282 | list_for_each_entry(sd, &subdev_list, async_list) { |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 283 | struct v4l2_async_subdev *asd; |
| 284 | int ret; |
| 285 | |
| 286 | asd = v4l2_async_find_match(notifier, sd); |
| 287 | if (!asd) |
| 288 | continue; |
| 289 | |
| 290 | ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd); |
| 291 | if (ret < 0) |
| 292 | return ret; |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 293 | |
| 294 | /* |
| 295 | * v4l2_async_match_notify() may lead to registering a |
| 296 | * new notifier and thus changing the async subdevs |
| 297 | * list. In order to proceed safely from here, restart |
| 298 | * parsing the list from the beginning. |
| 299 | */ |
| 300 | goto again; |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 306 | static void v4l2_async_cleanup(struct v4l2_subdev *sd) |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 307 | { |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 308 | v4l2_device_unregister_subdev(sd); |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 309 | /* Subdevice driver will reprobe and put the subdev back onto the list */ |
| 310 | list_del_init(&sd->async_list); |
| 311 | sd->asd = NULL; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 312 | } |
| 313 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 314 | /* Unbind all sub-devices in the notifier tree. */ |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 315 | static void v4l2_async_notifier_unbind_all_subdevs( |
| 316 | struct v4l2_async_notifier *notifier) |
| 317 | { |
| 318 | struct v4l2_subdev *sd, *tmp; |
| 319 | |
| 320 | list_for_each_entry_safe(sd, tmp, ¬ifier->done, async_list) { |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 321 | struct v4l2_async_notifier *subdev_notifier = |
| 322 | v4l2_async_find_subdev_notifier(sd); |
| 323 | |
| 324 | if (subdev_notifier) |
| 325 | v4l2_async_notifier_unbind_all_subdevs(subdev_notifier); |
| 326 | |
Sakari Ailus | ddddc18 | 2017-09-01 08:27:32 -0400 | [diff] [blame] | 327 | v4l2_async_notifier_call_unbind(notifier, sd, sd->asd); |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 328 | v4l2_async_cleanup(sd); |
| 329 | |
| 330 | list_move(&sd->async_list, &subdev_list); |
| 331 | } |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 332 | |
| 333 | notifier->parent = NULL; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 334 | } |
| 335 | |
Steve Longerbeam | a6e7003 | 2018-09-29 15:54:05 -0400 | [diff] [blame] | 336 | /* See if an async sub-device can be found in a notifier's lists. */ |
| 337 | static bool |
| 338 | __v4l2_async_notifier_has_async_subdev(struct v4l2_async_notifier *notifier, |
| 339 | struct v4l2_async_subdev *asd) |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 340 | { |
Steve Longerbeam | a6e7003 | 2018-09-29 15:54:05 -0400 | [diff] [blame] | 341 | struct v4l2_async_subdev *asd_y; |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 342 | struct v4l2_subdev *sd; |
| 343 | |
Steve Longerbeam | a6e7003 | 2018-09-29 15:54:05 -0400 | [diff] [blame] | 344 | list_for_each_entry(asd_y, ¬ifier->waiting, list) |
| 345 | if (asd_equal(asd, asd_y)) |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 346 | return true; |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 347 | |
| 348 | list_for_each_entry(sd, ¬ifier->done, async_list) { |
| 349 | if (WARN_ON(!sd->asd)) |
| 350 | continue; |
| 351 | |
Steve Longerbeam | a6e7003 | 2018-09-29 15:54:05 -0400 | [diff] [blame] | 352 | if (asd_equal(asd, sd->asd)) |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 353 | return true; |
| 354 | } |
| 355 | |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | /* |
Steve Longerbeam | a6e7003 | 2018-09-29 15:54:05 -0400 | [diff] [blame] | 360 | * Find out whether an async sub-device was set up already or |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 361 | * whether it exists in a given notifier before @this_index. |
| 362 | */ |
Steve Longerbeam | a6e7003 | 2018-09-29 15:54:05 -0400 | [diff] [blame] | 363 | static bool |
| 364 | v4l2_async_notifier_has_async_subdev(struct v4l2_async_notifier *notifier, |
| 365 | struct v4l2_async_subdev *asd, |
| 366 | unsigned int this_index) |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 367 | { |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 368 | struct v4l2_async_subdev *asd_y; |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 369 | unsigned int j; |
| 370 | |
| 371 | lockdep_assert_held(&list_lock); |
| 372 | |
Steve Longerbeam | a6e7003 | 2018-09-29 15:54:05 -0400 | [diff] [blame] | 373 | /* Check that an asd is not being added more than once. */ |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 374 | if (notifier->subdevs) { |
| 375 | for (j = 0; j < this_index; j++) { |
| 376 | asd_y = notifier->subdevs[j]; |
| 377 | if (asd_equal(asd, asd_y)) |
| 378 | return true; |
| 379 | } |
| 380 | } else { |
| 381 | j = 0; |
| 382 | list_for_each_entry(asd_y, ¬ifier->asd_list, asd_list) { |
| 383 | if (j++ >= this_index) |
| 384 | break; |
| 385 | if (asd_equal(asd, asd_y)) |
| 386 | return true; |
| 387 | } |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 388 | } |
| 389 | |
Steve Longerbeam | a6e7003 | 2018-09-29 15:54:05 -0400 | [diff] [blame] | 390 | /* Check that an asd does not exist in other notifiers. */ |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 391 | list_for_each_entry(notifier, ¬ifier_list, list) |
Steve Longerbeam | a6e7003 | 2018-09-29 15:54:05 -0400 | [diff] [blame] | 392 | if (__v4l2_async_notifier_has_async_subdev(notifier, asd)) |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 393 | return true; |
| 394 | |
| 395 | return false; |
| 396 | } |
| 397 | |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 398 | static int v4l2_async_notifier_asd_valid(struct v4l2_async_notifier *notifier, |
| 399 | struct v4l2_async_subdev *asd, |
| 400 | unsigned int this_index) |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 401 | { |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 402 | struct device *dev = |
| 403 | notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL; |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 404 | |
| 405 | if (!asd) |
| 406 | return -EINVAL; |
| 407 | |
| 408 | switch (asd->match_type) { |
| 409 | case V4L2_ASYNC_MATCH_CUSTOM: |
| 410 | case V4L2_ASYNC_MATCH_DEVNAME: |
| 411 | case V4L2_ASYNC_MATCH_I2C: |
| 412 | case V4L2_ASYNC_MATCH_FWNODE: |
| 413 | if (v4l2_async_notifier_has_async_subdev(notifier, asd, |
| 414 | this_index)) { |
| 415 | dev_dbg(dev, "subdev descriptor already listed in this or other notifiers\n"); |
| 416 | return -EEXIST; |
| 417 | } |
| 418 | break; |
| 419 | default: |
| 420 | dev_err(dev, "Invalid match type %u on %p\n", |
| 421 | asd->match_type, asd); |
| 422 | return -EINVAL; |
| 423 | } |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | void v4l2_async_notifier_init(struct v4l2_async_notifier *notifier) |
| 429 | { |
| 430 | mutex_lock(&list_lock); |
| 431 | |
| 432 | INIT_LIST_HEAD(¬ifier->asd_list); |
| 433 | |
| 434 | mutex_unlock(&list_lock); |
| 435 | } |
| 436 | EXPORT_SYMBOL(v4l2_async_notifier_init); |
| 437 | |
| 438 | static int __v4l2_async_notifier_register(struct v4l2_async_notifier *notifier) |
| 439 | { |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 440 | struct v4l2_async_subdev *asd; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 441 | int ret; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 442 | int i; |
| 443 | |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 444 | if (notifier->num_subdevs > V4L2_MAX_SUBDEVS) |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 445 | return -EINVAL; |
| 446 | |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 447 | INIT_LIST_HEAD(¬ifier->waiting); |
| 448 | INIT_LIST_HEAD(¬ifier->done); |
| 449 | |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 450 | mutex_lock(&list_lock); |
| 451 | |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 452 | if (notifier->subdevs) { |
| 453 | for (i = 0; i < notifier->num_subdevs; i++) { |
| 454 | asd = notifier->subdevs[i]; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 455 | |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 456 | ret = v4l2_async_notifier_asd_valid(notifier, asd, i); |
| 457 | if (ret) |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 458 | goto err_unlock; |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 459 | |
| 460 | list_add_tail(&asd->list, ¬ifier->waiting); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 461 | } |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 462 | } else { |
| 463 | i = 0; |
| 464 | list_for_each_entry(asd, ¬ifier->asd_list, asd_list) { |
| 465 | ret = v4l2_async_notifier_asd_valid(notifier, asd, i++); |
| 466 | if (ret) |
| 467 | goto err_unlock; |
| 468 | |
| 469 | list_add_tail(&asd->list, ¬ifier->waiting); |
| 470 | } |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 471 | } |
| 472 | |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 473 | ret = v4l2_async_notifier_try_all_subdevs(notifier); |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 474 | if (ret < 0) |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 475 | goto err_unbind; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 476 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 477 | ret = v4l2_async_notifier_try_complete(notifier); |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 478 | if (ret < 0) |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 479 | goto err_unbind; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 480 | |
Tuukka Toivonen | 47b037a | 2017-01-27 08:32:56 -0200 | [diff] [blame] | 481 | /* Keep also completed notifiers on the list */ |
| 482 | list_add(¬ifier->list, ¬ifier_list); |
| 483 | |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 484 | mutex_unlock(&list_lock); |
| 485 | |
| 486 | return 0; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 487 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 488 | err_unbind: |
| 489 | /* |
| 490 | * On failure, unbind all sub-devices registered through this notifier. |
| 491 | */ |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 492 | v4l2_async_notifier_unbind_all_subdevs(notifier); |
| 493 | |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 494 | err_unlock: |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 495 | mutex_unlock(&list_lock); |
| 496 | |
| 497 | return ret; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 498 | } |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 499 | |
| 500 | int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev, |
| 501 | struct v4l2_async_notifier *notifier) |
| 502 | { |
| 503 | int ret; |
| 504 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 505 | if (WARN_ON(!v4l2_dev || notifier->sd)) |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 506 | return -EINVAL; |
| 507 | |
| 508 | notifier->v4l2_dev = v4l2_dev; |
| 509 | |
| 510 | ret = __v4l2_async_notifier_register(notifier); |
| 511 | if (ret) |
| 512 | notifier->v4l2_dev = NULL; |
| 513 | |
| 514 | return ret; |
| 515 | } |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 516 | EXPORT_SYMBOL(v4l2_async_notifier_register); |
| 517 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 518 | int v4l2_async_subdev_notifier_register(struct v4l2_subdev *sd, |
| 519 | struct v4l2_async_notifier *notifier) |
| 520 | { |
| 521 | int ret; |
| 522 | |
| 523 | if (WARN_ON(!sd || notifier->v4l2_dev)) |
| 524 | return -EINVAL; |
| 525 | |
| 526 | notifier->sd = sd; |
| 527 | |
| 528 | ret = __v4l2_async_notifier_register(notifier); |
| 529 | if (ret) |
| 530 | notifier->sd = NULL; |
| 531 | |
| 532 | return ret; |
| 533 | } |
| 534 | EXPORT_SYMBOL(v4l2_async_subdev_notifier_register); |
| 535 | |
Sakari Ailus | aef69d5 | 2017-09-24 18:47:44 -0400 | [diff] [blame] | 536 | static void __v4l2_async_notifier_unregister( |
| 537 | struct v4l2_async_notifier *notifier) |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 538 | { |
Sakari Ailus | aef69d5 | 2017-09-24 18:47:44 -0400 | [diff] [blame] | 539 | if (!notifier || (!notifier->v4l2_dev && !notifier->sd)) |
Laurent Pinchart | 8e3fbfe | 2013-07-03 07:49:06 -0300 | [diff] [blame] | 540 | return; |
| 541 | |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 542 | v4l2_async_notifier_unbind_all_subdevs(notifier); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 543 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 544 | notifier->sd = NULL; |
Laurent Pinchart | 8e3fbfe | 2013-07-03 07:49:06 -0300 | [diff] [blame] | 545 | notifier->v4l2_dev = NULL; |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 546 | |
| 547 | list_del(¬ifier->list); |
Sakari Ailus | aef69d5 | 2017-09-24 18:47:44 -0400 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier) |
| 551 | { |
| 552 | mutex_lock(&list_lock); |
| 553 | |
| 554 | __v4l2_async_notifier_unregister(notifier); |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 555 | |
| 556 | mutex_unlock(&list_lock); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 557 | } |
| 558 | EXPORT_SYMBOL(v4l2_async_notifier_unregister); |
| 559 | |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 560 | static void __v4l2_async_notifier_cleanup(struct v4l2_async_notifier *notifier) |
Sakari Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 561 | { |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 562 | struct v4l2_async_subdev *asd, *tmp; |
Sakari Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 563 | unsigned int i; |
| 564 | |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 565 | if (!notifier) |
Sakari Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 566 | return; |
| 567 | |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 568 | if (notifier->subdevs) { |
| 569 | if (!notifier->max_subdevs) |
| 570 | return; |
Sakari Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 571 | |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 572 | for (i = 0; i < notifier->num_subdevs; i++) { |
| 573 | asd = notifier->subdevs[i]; |
| 574 | |
| 575 | switch (asd->match_type) { |
| 576 | case V4L2_ASYNC_MATCH_FWNODE: |
| 577 | fwnode_handle_put(asd->match.fwnode); |
| 578 | break; |
| 579 | default: |
| 580 | break; |
| 581 | } |
| 582 | |
| 583 | kfree(asd); |
Sakari Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 584 | } |
| 585 | |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 586 | notifier->max_subdevs = 0; |
| 587 | kvfree(notifier->subdevs); |
| 588 | notifier->subdevs = NULL; |
| 589 | } else { |
| 590 | list_for_each_entry_safe(asd, tmp, |
| 591 | ¬ifier->asd_list, asd_list) { |
| 592 | switch (asd->match_type) { |
| 593 | case V4L2_ASYNC_MATCH_FWNODE: |
| 594 | fwnode_handle_put(asd->match.fwnode); |
| 595 | break; |
| 596 | default: |
| 597 | break; |
| 598 | } |
| 599 | |
| 600 | list_del(&asd->asd_list); |
| 601 | kfree(asd); |
| 602 | } |
Sakari Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 603 | } |
| 604 | |
Sakari Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 605 | notifier->num_subdevs = 0; |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 606 | } |
Sakari Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 607 | |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 608 | void v4l2_async_notifier_cleanup(struct v4l2_async_notifier *notifier) |
| 609 | { |
| 610 | mutex_lock(&list_lock); |
| 611 | |
| 612 | __v4l2_async_notifier_cleanup(notifier); |
| 613 | |
| 614 | mutex_unlock(&list_lock); |
Sakari Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 615 | } |
| 616 | EXPORT_SYMBOL_GPL(v4l2_async_notifier_cleanup); |
| 617 | |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 618 | int v4l2_async_notifier_add_subdev(struct v4l2_async_notifier *notifier, |
| 619 | struct v4l2_async_subdev *asd) |
| 620 | { |
| 621 | int ret; |
| 622 | |
| 623 | mutex_lock(&list_lock); |
| 624 | |
| 625 | if (notifier->num_subdevs >= V4L2_MAX_SUBDEVS) { |
| 626 | ret = -EINVAL; |
| 627 | goto unlock; |
| 628 | } |
| 629 | |
| 630 | /* |
| 631 | * If caller uses this function, it cannot also allocate and |
| 632 | * place asd's in the notifier->subdevs array. |
| 633 | */ |
| 634 | if (WARN_ON(notifier->subdevs)) { |
| 635 | ret = -EINVAL; |
| 636 | goto unlock; |
| 637 | } |
| 638 | |
| 639 | ret = v4l2_async_notifier_asd_valid(notifier, asd, |
| 640 | notifier->num_subdevs); |
| 641 | if (ret) |
| 642 | goto unlock; |
| 643 | |
| 644 | list_add_tail(&asd->asd_list, ¬ifier->asd_list); |
| 645 | notifier->num_subdevs++; |
| 646 | |
| 647 | unlock: |
| 648 | mutex_unlock(&list_lock); |
| 649 | return ret; |
| 650 | } |
| 651 | EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_subdev); |
| 652 | |
Steve Longerbeam | 23989b4 | 2018-09-29 15:54:07 -0400 | [diff] [blame^] | 653 | struct v4l2_async_subdev * |
| 654 | v4l2_async_notifier_add_fwnode_subdev(struct v4l2_async_notifier *notifier, |
| 655 | struct fwnode_handle *fwnode, |
| 656 | unsigned int asd_struct_size) |
| 657 | { |
| 658 | struct v4l2_async_subdev *asd; |
| 659 | int ret; |
| 660 | |
| 661 | asd = kzalloc(asd_struct_size, GFP_KERNEL); |
| 662 | if (!asd) |
| 663 | return ERR_PTR(-ENOMEM); |
| 664 | |
| 665 | asd->match_type = V4L2_ASYNC_MATCH_FWNODE; |
| 666 | asd->match.fwnode = fwnode; |
| 667 | |
| 668 | ret = v4l2_async_notifier_add_subdev(notifier, asd); |
| 669 | if (ret) { |
| 670 | kfree(asd); |
| 671 | return ERR_PTR(ret); |
| 672 | } |
| 673 | |
| 674 | return asd; |
| 675 | } |
| 676 | EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_fwnode_subdev); |
| 677 | |
| 678 | struct v4l2_async_subdev * |
| 679 | v4l2_async_notifier_add_i2c_subdev(struct v4l2_async_notifier *notifier, |
| 680 | int adapter_id, unsigned short address, |
| 681 | unsigned int asd_struct_size) |
| 682 | { |
| 683 | struct v4l2_async_subdev *asd; |
| 684 | int ret; |
| 685 | |
| 686 | asd = kzalloc(asd_struct_size, GFP_KERNEL); |
| 687 | if (!asd) |
| 688 | return ERR_PTR(-ENOMEM); |
| 689 | |
| 690 | asd->match_type = V4L2_ASYNC_MATCH_I2C; |
| 691 | asd->match.i2c.adapter_id = adapter_id; |
| 692 | asd->match.i2c.address = address; |
| 693 | |
| 694 | ret = v4l2_async_notifier_add_subdev(notifier, asd); |
| 695 | if (ret) { |
| 696 | kfree(asd); |
| 697 | return ERR_PTR(ret); |
| 698 | } |
| 699 | |
| 700 | return asd; |
| 701 | } |
| 702 | EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_i2c_subdev); |
| 703 | |
| 704 | struct v4l2_async_subdev * |
| 705 | v4l2_async_notifier_add_devname_subdev(struct v4l2_async_notifier *notifier, |
| 706 | const char *device_name, |
| 707 | unsigned int asd_struct_size) |
| 708 | { |
| 709 | struct v4l2_async_subdev *asd; |
| 710 | int ret; |
| 711 | |
| 712 | asd = kzalloc(asd_struct_size, GFP_KERNEL); |
| 713 | if (!asd) |
| 714 | return ERR_PTR(-ENOMEM); |
| 715 | |
| 716 | asd->match_type = V4L2_ASYNC_MATCH_DEVNAME; |
| 717 | asd->match.device_name = device_name; |
| 718 | |
| 719 | ret = v4l2_async_notifier_add_subdev(notifier, asd); |
| 720 | if (ret) { |
| 721 | kfree(asd); |
| 722 | return ERR_PTR(ret); |
| 723 | } |
| 724 | |
| 725 | return asd; |
| 726 | } |
| 727 | EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_devname_subdev); |
| 728 | |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 729 | int v4l2_async_register_subdev(struct v4l2_subdev *sd) |
| 730 | { |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 731 | struct v4l2_async_notifier *subdev_notifier; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 732 | struct v4l2_async_notifier *notifier; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 733 | int ret; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 734 | |
Sakari Ailus | 8621765 | 2015-06-11 12:18:01 -0700 | [diff] [blame] | 735 | /* |
| 736 | * No reference taken. The reference is held by the device |
| 737 | * (struct v4l2_subdev.dev), and async sub-device does not |
| 738 | * exist independently of the device at any point of time. |
| 739 | */ |
Sakari Ailus | 859969b | 2016-08-26 20:17:25 -0300 | [diff] [blame] | 740 | if (!sd->fwnode && sd->dev) |
| 741 | sd->fwnode = dev_fwnode(sd->dev); |
Sakari Ailus | 8621765 | 2015-06-11 12:18:01 -0700 | [diff] [blame] | 742 | |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 743 | mutex_lock(&list_lock); |
| 744 | |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 745 | INIT_LIST_HEAD(&sd->async_list); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 746 | |
| 747 | list_for_each_entry(notifier, ¬ifier_list, list) { |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 748 | struct v4l2_device *v4l2_dev = |
| 749 | v4l2_async_notifier_find_v4l2_dev(notifier); |
| 750 | struct v4l2_async_subdev *asd; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 751 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 752 | if (!v4l2_dev) |
| 753 | continue; |
| 754 | |
| 755 | asd = v4l2_async_find_match(notifier, sd); |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 756 | if (!asd) |
| 757 | continue; |
| 758 | |
Niklas Söderlund | 487cc85 | 2017-11-15 10:43:58 -0500 | [diff] [blame] | 759 | ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd); |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 760 | if (ret) |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 761 | goto err_unbind; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 762 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 763 | ret = v4l2_async_notifier_try_complete(notifier); |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 764 | if (ret) |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 765 | goto err_unbind; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 766 | |
| 767 | goto out_unlock; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | /* None matched, wait for hot-plugging */ |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 771 | list_add(&sd->async_list, &subdev_list); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 772 | |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 773 | out_unlock: |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 774 | mutex_unlock(&list_lock); |
| 775 | |
| 776 | return 0; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 777 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 778 | err_unbind: |
| 779 | /* |
| 780 | * Complete failed. Unbind the sub-devices bound through registering |
| 781 | * this async sub-device. |
| 782 | */ |
| 783 | subdev_notifier = v4l2_async_find_subdev_notifier(sd); |
| 784 | if (subdev_notifier) |
| 785 | v4l2_async_notifier_unbind_all_subdevs(subdev_notifier); |
| 786 | |
| 787 | if (sd->asd) |
| 788 | v4l2_async_notifier_call_unbind(notifier, sd, sd->asd); |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 789 | v4l2_async_cleanup(sd); |
| 790 | |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 791 | mutex_unlock(&list_lock); |
| 792 | |
| 793 | return ret; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 794 | } |
| 795 | EXPORT_SYMBOL(v4l2_async_register_subdev); |
| 796 | |
| 797 | void v4l2_async_unregister_subdev(struct v4l2_subdev *sd) |
| 798 | { |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 799 | mutex_lock(&list_lock); |
| 800 | |
Sakari Ailus | aef69d5 | 2017-09-24 18:47:44 -0400 | [diff] [blame] | 801 | __v4l2_async_notifier_unregister(sd->subdev_notifier); |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 802 | __v4l2_async_notifier_cleanup(sd->subdev_notifier); |
Sakari Ailus | aef69d5 | 2017-09-24 18:47:44 -0400 | [diff] [blame] | 803 | kfree(sd->subdev_notifier); |
| 804 | sd->subdev_notifier = NULL; |
| 805 | |
Sakari Ailus | 7fc4fdb | 2017-10-03 02:26:32 -0400 | [diff] [blame] | 806 | if (sd->asd) { |
| 807 | struct v4l2_async_notifier *notifier = sd->notifier; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 808 | |
Sakari Ailus | 7fc4fdb | 2017-10-03 02:26:32 -0400 | [diff] [blame] | 809 | list_add(&sd->asd->list, ¬ifier->waiting); |
| 810 | |
Sakari Ailus | ddddc18 | 2017-09-01 08:27:32 -0400 | [diff] [blame] | 811 | v4l2_async_notifier_call_unbind(notifier, sd, sd->asd); |
Sakari Ailus | 7fc4fdb | 2017-10-03 02:26:32 -0400 | [diff] [blame] | 812 | } |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 813 | |
Niklas Söderlund | 633d185 | 2017-10-02 16:16:52 -0400 | [diff] [blame] | 814 | v4l2_async_cleanup(sd); |
| 815 | |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 816 | mutex_unlock(&list_lock); |
| 817 | } |
| 818 | EXPORT_SYMBOL(v4l2_async_unregister_subdev); |