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. |
Steve Longerbeam | 66beb32 | 2018-09-29 15:54:19 -0400 | [diff] [blame] | 362 | * If @this_index < 0, search the notifier's entire @asd_list. |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 363 | */ |
Steve Longerbeam | a6e7003 | 2018-09-29 15:54:05 -0400 | [diff] [blame] | 364 | static bool |
| 365 | v4l2_async_notifier_has_async_subdev(struct v4l2_async_notifier *notifier, |
| 366 | struct v4l2_async_subdev *asd, |
Steve Longerbeam | 66beb32 | 2018-09-29 15:54:19 -0400 | [diff] [blame] | 367 | int this_index) |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 368 | { |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 369 | struct v4l2_async_subdev *asd_y; |
Steve Longerbeam | 66beb32 | 2018-09-29 15:54:19 -0400 | [diff] [blame] | 370 | int j = 0; |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 371 | |
| 372 | lockdep_assert_held(&list_lock); |
| 373 | |
Steve Longerbeam | a6e7003 | 2018-09-29 15:54:05 -0400 | [diff] [blame] | 374 | /* Check that an asd is not being added more than once. */ |
Steve Longerbeam | 66beb32 | 2018-09-29 15:54:19 -0400 | [diff] [blame] | 375 | list_for_each_entry(asd_y, ¬ifier->asd_list, asd_list) { |
| 376 | if (this_index >= 0 && j++ >= this_index) |
| 377 | break; |
| 378 | if (asd_equal(asd, asd_y)) |
| 379 | return true; |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 380 | } |
| 381 | |
Steve Longerbeam | a6e7003 | 2018-09-29 15:54:05 -0400 | [diff] [blame] | 382 | /* Check that an asd does not exist in other notifiers. */ |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 383 | list_for_each_entry(notifier, ¬ifier_list, list) |
Steve Longerbeam | a6e7003 | 2018-09-29 15:54:05 -0400 | [diff] [blame] | 384 | if (__v4l2_async_notifier_has_async_subdev(notifier, asd)) |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 385 | return true; |
| 386 | |
| 387 | return false; |
| 388 | } |
| 389 | |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 390 | static int v4l2_async_notifier_asd_valid(struct v4l2_async_notifier *notifier, |
| 391 | struct v4l2_async_subdev *asd, |
Steve Longerbeam | 66beb32 | 2018-09-29 15:54:19 -0400 | [diff] [blame] | 392 | int this_index) |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 393 | { |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 394 | struct device *dev = |
| 395 | notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL; |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 396 | |
| 397 | if (!asd) |
| 398 | return -EINVAL; |
| 399 | |
| 400 | switch (asd->match_type) { |
| 401 | case V4L2_ASYNC_MATCH_CUSTOM: |
| 402 | case V4L2_ASYNC_MATCH_DEVNAME: |
| 403 | case V4L2_ASYNC_MATCH_I2C: |
| 404 | case V4L2_ASYNC_MATCH_FWNODE: |
| 405 | if (v4l2_async_notifier_has_async_subdev(notifier, asd, |
| 406 | this_index)) { |
| 407 | dev_dbg(dev, "subdev descriptor already listed in this or other notifiers\n"); |
| 408 | return -EEXIST; |
| 409 | } |
| 410 | break; |
| 411 | default: |
| 412 | dev_err(dev, "Invalid match type %u on %p\n", |
| 413 | asd->match_type, asd); |
| 414 | return -EINVAL; |
| 415 | } |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | void v4l2_async_notifier_init(struct v4l2_async_notifier *notifier) |
| 421 | { |
| 422 | mutex_lock(&list_lock); |
| 423 | |
| 424 | INIT_LIST_HEAD(¬ifier->asd_list); |
| 425 | |
| 426 | mutex_unlock(&list_lock); |
| 427 | } |
| 428 | EXPORT_SYMBOL(v4l2_async_notifier_init); |
| 429 | |
| 430 | static int __v4l2_async_notifier_register(struct v4l2_async_notifier *notifier) |
| 431 | { |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 432 | struct v4l2_async_subdev *asd; |
Steve Longerbeam | 66beb32 | 2018-09-29 15:54:19 -0400 | [diff] [blame] | 433 | int ret, i = 0; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 434 | |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 435 | INIT_LIST_HEAD(¬ifier->waiting); |
| 436 | INIT_LIST_HEAD(¬ifier->done); |
| 437 | |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 438 | mutex_lock(&list_lock); |
| 439 | |
Steve Longerbeam | 66beb32 | 2018-09-29 15:54:19 -0400 | [diff] [blame] | 440 | list_for_each_entry(asd, ¬ifier->asd_list, asd_list) { |
| 441 | ret = v4l2_async_notifier_asd_valid(notifier, asd, i++); |
| 442 | if (ret) |
| 443 | goto err_unlock; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 444 | |
Steve Longerbeam | 66beb32 | 2018-09-29 15:54:19 -0400 | [diff] [blame] | 445 | list_add_tail(&asd->list, ¬ifier->waiting); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 446 | } |
| 447 | |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 448 | ret = v4l2_async_notifier_try_all_subdevs(notifier); |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 449 | if (ret < 0) |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 450 | goto err_unbind; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 451 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 452 | ret = v4l2_async_notifier_try_complete(notifier); |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 453 | if (ret < 0) |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 454 | goto err_unbind; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 455 | |
Tuukka Toivonen | 47b037a | 2017-01-27 08:32:56 -0200 | [diff] [blame] | 456 | /* Keep also completed notifiers on the list */ |
| 457 | list_add(¬ifier->list, ¬ifier_list); |
| 458 | |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 459 | mutex_unlock(&list_lock); |
| 460 | |
| 461 | return 0; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 462 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 463 | err_unbind: |
| 464 | /* |
| 465 | * On failure, unbind all sub-devices registered through this notifier. |
| 466 | */ |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 467 | v4l2_async_notifier_unbind_all_subdevs(notifier); |
| 468 | |
Sakari Ailus | 466cae6 | 2017-09-20 03:51:54 -0400 | [diff] [blame] | 469 | err_unlock: |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 470 | mutex_unlock(&list_lock); |
| 471 | |
| 472 | return ret; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 473 | } |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 474 | |
| 475 | int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev, |
| 476 | struct v4l2_async_notifier *notifier) |
| 477 | { |
| 478 | int ret; |
| 479 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 480 | if (WARN_ON(!v4l2_dev || notifier->sd)) |
Sakari Ailus | a3620cb | 2017-09-24 20:48:08 -0400 | [diff] [blame] | 481 | return -EINVAL; |
| 482 | |
| 483 | notifier->v4l2_dev = v4l2_dev; |
| 484 | |
| 485 | ret = __v4l2_async_notifier_register(notifier); |
| 486 | if (ret) |
| 487 | notifier->v4l2_dev = NULL; |
| 488 | |
| 489 | return ret; |
| 490 | } |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 491 | EXPORT_SYMBOL(v4l2_async_notifier_register); |
| 492 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 493 | int v4l2_async_subdev_notifier_register(struct v4l2_subdev *sd, |
| 494 | struct v4l2_async_notifier *notifier) |
| 495 | { |
| 496 | int ret; |
| 497 | |
| 498 | if (WARN_ON(!sd || notifier->v4l2_dev)) |
| 499 | return -EINVAL; |
| 500 | |
| 501 | notifier->sd = sd; |
| 502 | |
| 503 | ret = __v4l2_async_notifier_register(notifier); |
| 504 | if (ret) |
| 505 | notifier->sd = NULL; |
| 506 | |
| 507 | return ret; |
| 508 | } |
| 509 | EXPORT_SYMBOL(v4l2_async_subdev_notifier_register); |
| 510 | |
Sakari Ailus | aef69d5 | 2017-09-24 18:47:44 -0400 | [diff] [blame] | 511 | static void __v4l2_async_notifier_unregister( |
| 512 | struct v4l2_async_notifier *notifier) |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 513 | { |
Sakari Ailus | aef69d5 | 2017-09-24 18:47:44 -0400 | [diff] [blame] | 514 | if (!notifier || (!notifier->v4l2_dev && !notifier->sd)) |
Laurent Pinchart | 8e3fbfe | 2013-07-03 07:49:06 -0300 | [diff] [blame] | 515 | return; |
| 516 | |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 517 | v4l2_async_notifier_unbind_all_subdevs(notifier); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 518 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 519 | notifier->sd = NULL; |
Laurent Pinchart | 8e3fbfe | 2013-07-03 07:49:06 -0300 | [diff] [blame] | 520 | notifier->v4l2_dev = NULL; |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 521 | |
| 522 | list_del(¬ifier->list); |
Sakari Ailus | aef69d5 | 2017-09-24 18:47:44 -0400 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier) |
| 526 | { |
| 527 | mutex_lock(&list_lock); |
| 528 | |
| 529 | __v4l2_async_notifier_unregister(notifier); |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 530 | |
| 531 | mutex_unlock(&list_lock); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 532 | } |
| 533 | EXPORT_SYMBOL(v4l2_async_notifier_unregister); |
| 534 | |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 535 | static void __v4l2_async_notifier_cleanup(struct v4l2_async_notifier *notifier) |
Sakari Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 536 | { |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 537 | struct v4l2_async_subdev *asd, *tmp; |
Sakari Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 538 | |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 539 | if (!notifier) |
Sakari Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 540 | return; |
| 541 | |
Steve Longerbeam | 66beb32 | 2018-09-29 15:54:19 -0400 | [diff] [blame] | 542 | list_for_each_entry_safe(asd, tmp, ¬ifier->asd_list, asd_list) { |
| 543 | switch (asd->match_type) { |
| 544 | case V4L2_ASYNC_MATCH_FWNODE: |
| 545 | fwnode_handle_put(asd->match.fwnode); |
| 546 | break; |
| 547 | default: |
| 548 | break; |
Sakari Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 549 | } |
| 550 | |
Steve Longerbeam | 66beb32 | 2018-09-29 15:54:19 -0400 | [diff] [blame] | 551 | list_del(&asd->asd_list); |
| 552 | kfree(asd); |
Sakari Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 553 | } |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 554 | } |
Sakari Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 555 | |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 556 | void v4l2_async_notifier_cleanup(struct v4l2_async_notifier *notifier) |
| 557 | { |
| 558 | mutex_lock(&list_lock); |
| 559 | |
| 560 | __v4l2_async_notifier_cleanup(notifier); |
| 561 | |
| 562 | mutex_unlock(&list_lock); |
Sakari Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 563 | } |
| 564 | EXPORT_SYMBOL_GPL(v4l2_async_notifier_cleanup); |
| 565 | |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 566 | int v4l2_async_notifier_add_subdev(struct v4l2_async_notifier *notifier, |
| 567 | struct v4l2_async_subdev *asd) |
| 568 | { |
| 569 | int ret; |
| 570 | |
| 571 | mutex_lock(&list_lock); |
| 572 | |
Steve Longerbeam | 66beb32 | 2018-09-29 15:54:19 -0400 | [diff] [blame] | 573 | ret = v4l2_async_notifier_asd_valid(notifier, asd, -1); |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 574 | if (ret) |
| 575 | goto unlock; |
| 576 | |
| 577 | list_add_tail(&asd->asd_list, ¬ifier->asd_list); |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 578 | |
| 579 | unlock: |
| 580 | mutex_unlock(&list_lock); |
| 581 | return ret; |
| 582 | } |
| 583 | EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_subdev); |
| 584 | |
Steve Longerbeam | 23989b4 | 2018-09-29 15:54:07 -0400 | [diff] [blame] | 585 | struct v4l2_async_subdev * |
| 586 | v4l2_async_notifier_add_fwnode_subdev(struct v4l2_async_notifier *notifier, |
| 587 | struct fwnode_handle *fwnode, |
| 588 | unsigned int asd_struct_size) |
| 589 | { |
| 590 | struct v4l2_async_subdev *asd; |
| 591 | int ret; |
| 592 | |
| 593 | asd = kzalloc(asd_struct_size, GFP_KERNEL); |
| 594 | if (!asd) |
| 595 | return ERR_PTR(-ENOMEM); |
| 596 | |
| 597 | asd->match_type = V4L2_ASYNC_MATCH_FWNODE; |
| 598 | asd->match.fwnode = fwnode; |
| 599 | |
| 600 | ret = v4l2_async_notifier_add_subdev(notifier, asd); |
| 601 | if (ret) { |
| 602 | kfree(asd); |
| 603 | return ERR_PTR(ret); |
| 604 | } |
| 605 | |
| 606 | return asd; |
| 607 | } |
| 608 | EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_fwnode_subdev); |
| 609 | |
| 610 | struct v4l2_async_subdev * |
| 611 | v4l2_async_notifier_add_i2c_subdev(struct v4l2_async_notifier *notifier, |
| 612 | int adapter_id, unsigned short address, |
| 613 | unsigned int asd_struct_size) |
| 614 | { |
| 615 | struct v4l2_async_subdev *asd; |
| 616 | int ret; |
| 617 | |
| 618 | asd = kzalloc(asd_struct_size, GFP_KERNEL); |
| 619 | if (!asd) |
| 620 | return ERR_PTR(-ENOMEM); |
| 621 | |
| 622 | asd->match_type = V4L2_ASYNC_MATCH_I2C; |
| 623 | asd->match.i2c.adapter_id = adapter_id; |
| 624 | asd->match.i2c.address = address; |
| 625 | |
| 626 | ret = v4l2_async_notifier_add_subdev(notifier, asd); |
| 627 | if (ret) { |
| 628 | kfree(asd); |
| 629 | return ERR_PTR(ret); |
| 630 | } |
| 631 | |
| 632 | return asd; |
| 633 | } |
| 634 | EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_i2c_subdev); |
| 635 | |
| 636 | struct v4l2_async_subdev * |
| 637 | v4l2_async_notifier_add_devname_subdev(struct v4l2_async_notifier *notifier, |
| 638 | const char *device_name, |
| 639 | unsigned int asd_struct_size) |
| 640 | { |
| 641 | struct v4l2_async_subdev *asd; |
| 642 | int ret; |
| 643 | |
| 644 | asd = kzalloc(asd_struct_size, GFP_KERNEL); |
| 645 | if (!asd) |
| 646 | return ERR_PTR(-ENOMEM); |
| 647 | |
| 648 | asd->match_type = V4L2_ASYNC_MATCH_DEVNAME; |
| 649 | asd->match.device_name = device_name; |
| 650 | |
| 651 | ret = v4l2_async_notifier_add_subdev(notifier, asd); |
| 652 | if (ret) { |
| 653 | kfree(asd); |
| 654 | return ERR_PTR(ret); |
| 655 | } |
| 656 | |
| 657 | return asd; |
| 658 | } |
| 659 | EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_devname_subdev); |
| 660 | |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 661 | int v4l2_async_register_subdev(struct v4l2_subdev *sd) |
| 662 | { |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 663 | struct v4l2_async_notifier *subdev_notifier; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 664 | struct v4l2_async_notifier *notifier; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 665 | int ret; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 666 | |
Sakari Ailus | 8621765 | 2015-06-11 12:18:01 -0700 | [diff] [blame] | 667 | /* |
| 668 | * No reference taken. The reference is held by the device |
| 669 | * (struct v4l2_subdev.dev), and async sub-device does not |
| 670 | * exist independently of the device at any point of time. |
| 671 | */ |
Sakari Ailus | 859969b | 2016-08-26 20:17:25 -0300 | [diff] [blame] | 672 | if (!sd->fwnode && sd->dev) |
| 673 | sd->fwnode = dev_fwnode(sd->dev); |
Sakari Ailus | 8621765 | 2015-06-11 12:18:01 -0700 | [diff] [blame] | 674 | |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 675 | mutex_lock(&list_lock); |
| 676 | |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 677 | INIT_LIST_HEAD(&sd->async_list); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 678 | |
| 679 | list_for_each_entry(notifier, ¬ifier_list, list) { |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 680 | struct v4l2_device *v4l2_dev = |
| 681 | v4l2_async_notifier_find_v4l2_dev(notifier); |
| 682 | struct v4l2_async_subdev *asd; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 683 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 684 | if (!v4l2_dev) |
| 685 | continue; |
| 686 | |
| 687 | asd = v4l2_async_find_match(notifier, sd); |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 688 | if (!asd) |
| 689 | continue; |
| 690 | |
Niklas Söderlund | 487cc85 | 2017-11-15 10:43:58 -0500 | [diff] [blame] | 691 | ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd); |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 692 | if (ret) |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 693 | goto err_unbind; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 694 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 695 | ret = v4l2_async_notifier_try_complete(notifier); |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 696 | if (ret) |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 697 | goto err_unbind; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 698 | |
| 699 | goto out_unlock; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /* None matched, wait for hot-plugging */ |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 703 | list_add(&sd->async_list, &subdev_list); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 704 | |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 705 | out_unlock: |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 706 | mutex_unlock(&list_lock); |
| 707 | |
| 708 | return 0; |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 709 | |
Sakari Ailus | 2cab00b | 2017-09-24 20:54:31 -0400 | [diff] [blame] | 710 | err_unbind: |
| 711 | /* |
| 712 | * Complete failed. Unbind the sub-devices bound through registering |
| 713 | * this async sub-device. |
| 714 | */ |
| 715 | subdev_notifier = v4l2_async_find_subdev_notifier(sd); |
| 716 | if (subdev_notifier) |
| 717 | v4l2_async_notifier_unbind_all_subdevs(subdev_notifier); |
| 718 | |
| 719 | if (sd->asd) |
| 720 | v4l2_async_notifier_call_unbind(notifier, sd, sd->asd); |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 721 | v4l2_async_cleanup(sd); |
| 722 | |
Sakari Ailus | fb45f43 | 2017-10-02 06:24:54 -0400 | [diff] [blame] | 723 | mutex_unlock(&list_lock); |
| 724 | |
| 725 | return ret; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 726 | } |
| 727 | EXPORT_SYMBOL(v4l2_async_register_subdev); |
| 728 | |
| 729 | void v4l2_async_unregister_subdev(struct v4l2_subdev *sd) |
| 730 | { |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 731 | mutex_lock(&list_lock); |
| 732 | |
Sakari Ailus | aef69d5 | 2017-09-24 18:47:44 -0400 | [diff] [blame] | 733 | __v4l2_async_notifier_unregister(sd->subdev_notifier); |
Steve Longerbeam | b47d7ff | 2018-09-29 15:54:06 -0400 | [diff] [blame] | 734 | __v4l2_async_notifier_cleanup(sd->subdev_notifier); |
Sakari Ailus | aef69d5 | 2017-09-24 18:47:44 -0400 | [diff] [blame] | 735 | kfree(sd->subdev_notifier); |
| 736 | sd->subdev_notifier = NULL; |
| 737 | |
Sakari Ailus | 7fc4fdb | 2017-10-03 02:26:32 -0400 | [diff] [blame] | 738 | if (sd->asd) { |
| 739 | struct v4l2_async_notifier *notifier = sd->notifier; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 740 | |
Sakari Ailus | 7fc4fdb | 2017-10-03 02:26:32 -0400 | [diff] [blame] | 741 | list_add(&sd->asd->list, ¬ifier->waiting); |
| 742 | |
Sakari Ailus | ddddc18 | 2017-09-01 08:27:32 -0400 | [diff] [blame] | 743 | v4l2_async_notifier_call_unbind(notifier, sd, sd->asd); |
Sakari Ailus | 7fc4fdb | 2017-10-03 02:26:32 -0400 | [diff] [blame] | 744 | } |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 745 | |
Niklas Söderlund | 633d185 | 2017-10-02 16:16:52 -0400 | [diff] [blame] | 746 | v4l2_async_cleanup(sd); |
| 747 | |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 748 | mutex_unlock(&list_lock); |
| 749 | } |
| 750 | EXPORT_SYMBOL(v4l2_async_unregister_subdev); |