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> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/mutex.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/types.h> |
| 20 | |
| 21 | #include <media/v4l2-async.h> |
| 22 | #include <media/v4l2-device.h> |
| 23 | #include <media/v4l2-subdev.h> |
| 24 | |
| 25 | static bool match_i2c(struct device *dev, struct v4l2_async_subdev *asd) |
| 26 | { |
Guennadi Liakhovetski | fe05e14 | 2013-06-24 05:13:51 -0300 | [diff] [blame] | 27 | #if IS_ENABLED(CONFIG_I2C) |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 28 | struct i2c_client *client = i2c_verify_client(dev); |
| 29 | return client && |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 30 | asd->match.i2c.adapter_id == client->adapter->nr && |
| 31 | asd->match.i2c.address == client->addr; |
Guennadi Liakhovetski | fe05e14 | 2013-06-24 05:13:51 -0300 | [diff] [blame] | 32 | #else |
| 33 | return false; |
| 34 | #endif |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 35 | } |
| 36 | |
Sylwester Nawrocki | cfca764 | 2013-07-19 12:14:46 -0300 | [diff] [blame] | 37 | static bool match_devname(struct device *dev, struct v4l2_async_subdev *asd) |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 38 | { |
Sylwester Nawrocki | cfca764 | 2013-07-19 12:14:46 -0300 | [diff] [blame] | 39 | return !strcmp(asd->match.device_name.name, dev_name(dev)); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 40 | } |
| 41 | |
Sylwester Nawrocki | e7359f8 | 2013-07-19 12:21:29 -0300 | [diff] [blame] | 42 | static bool match_of(struct device *dev, struct v4l2_async_subdev *asd) |
| 43 | { |
| 44 | return dev->of_node == asd->match.of.node; |
| 45 | } |
| 46 | |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 47 | static LIST_HEAD(subdev_list); |
| 48 | static LIST_HEAD(notifier_list); |
| 49 | static DEFINE_MUTEX(list_lock); |
| 50 | |
| 51 | static struct v4l2_async_subdev *v4l2_async_belongs(struct v4l2_async_notifier *notifier, |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 52 | struct v4l2_subdev *sd) |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 53 | { |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 54 | struct v4l2_async_subdev *asd; |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 55 | bool (*match)(struct device *, struct v4l2_async_subdev *); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 56 | |
| 57 | list_for_each_entry(asd, ¬ifier->waiting, list) { |
| 58 | /* bus_type has been verified valid before */ |
Sylwester Nawrocki | cfca764 | 2013-07-19 12:14:46 -0300 | [diff] [blame] | 59 | switch (asd->match_type) { |
| 60 | case V4L2_ASYNC_MATCH_CUSTOM: |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 61 | match = asd->match.custom.match; |
| 62 | if (!match) |
| 63 | /* Match always */ |
| 64 | return asd; |
| 65 | break; |
Sylwester Nawrocki | cfca764 | 2013-07-19 12:14:46 -0300 | [diff] [blame] | 66 | case V4L2_ASYNC_MATCH_DEVNAME: |
| 67 | match = match_devname; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 68 | break; |
Sylwester Nawrocki | cfca764 | 2013-07-19 12:14:46 -0300 | [diff] [blame] | 69 | case V4L2_ASYNC_MATCH_I2C: |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 70 | match = match_i2c; |
| 71 | break; |
Sylwester Nawrocki | e7359f8 | 2013-07-19 12:21:29 -0300 | [diff] [blame] | 72 | case V4L2_ASYNC_MATCH_OF: |
| 73 | match = match_of; |
| 74 | break; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 75 | default: |
| 76 | /* Cannot happen, unless someone breaks us */ |
| 77 | WARN_ON(true); |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | /* match cannot be NULL here */ |
| 82 | if (match(sd->dev, asd)) |
| 83 | return asd; |
| 84 | } |
| 85 | |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | static int v4l2_async_test_notify(struct v4l2_async_notifier *notifier, |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 90 | struct v4l2_subdev *sd, |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 91 | struct v4l2_async_subdev *asd) |
| 92 | { |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 93 | int ret; |
| 94 | |
| 95 | /* Remove from the waiting list */ |
| 96 | list_del(&asd->list); |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 97 | sd->asd = asd; |
| 98 | sd->notifier = notifier; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 99 | |
| 100 | if (notifier->bound) { |
| 101 | ret = notifier->bound(notifier, sd, asd); |
| 102 | if (ret < 0) |
| 103 | return ret; |
| 104 | } |
| 105 | /* Move from the global subdevice list to notifier's done */ |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 106 | list_move(&sd->async_list, ¬ifier->done); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 107 | |
| 108 | ret = v4l2_device_register_subdev(notifier->v4l2_dev, sd); |
| 109 | if (ret < 0) { |
| 110 | if (notifier->unbind) |
| 111 | notifier->unbind(notifier, sd, asd); |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | if (list_empty(¬ifier->waiting) && notifier->complete) |
| 116 | return notifier->complete(notifier); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 121 | static void v4l2_async_cleanup(struct v4l2_subdev *sd) |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 122 | { |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 123 | v4l2_device_unregister_subdev(sd); |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 124 | /* Subdevice driver will reprobe and put the subdev back onto the list */ |
| 125 | list_del_init(&sd->async_list); |
| 126 | sd->asd = NULL; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 127 | sd->dev = NULL; |
| 128 | } |
| 129 | |
| 130 | int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev, |
| 131 | struct v4l2_async_notifier *notifier) |
| 132 | { |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 133 | struct v4l2_subdev *sd, *tmp; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 134 | struct v4l2_async_subdev *asd; |
| 135 | int i; |
| 136 | |
| 137 | if (!notifier->num_subdevs || notifier->num_subdevs > V4L2_MAX_SUBDEVS) |
| 138 | return -EINVAL; |
| 139 | |
| 140 | notifier->v4l2_dev = v4l2_dev; |
| 141 | INIT_LIST_HEAD(¬ifier->waiting); |
| 142 | INIT_LIST_HEAD(¬ifier->done); |
| 143 | |
| 144 | for (i = 0; i < notifier->num_subdevs; i++) { |
Sylwester Nawrocki | e8419d0 | 2013-07-19 12:31:10 -0300 | [diff] [blame] | 145 | asd = notifier->subdevs[i]; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 146 | |
Sylwester Nawrocki | cfca764 | 2013-07-19 12:14:46 -0300 | [diff] [blame] | 147 | switch (asd->match_type) { |
| 148 | case V4L2_ASYNC_MATCH_CUSTOM: |
| 149 | case V4L2_ASYNC_MATCH_DEVNAME: |
| 150 | case V4L2_ASYNC_MATCH_I2C: |
Sylwester Nawrocki | e7359f8 | 2013-07-19 12:21:29 -0300 | [diff] [blame] | 151 | case V4L2_ASYNC_MATCH_OF: |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 152 | break; |
| 153 | default: |
| 154 | dev_err(notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL, |
Sylwester Nawrocki | cfca764 | 2013-07-19 12:14:46 -0300 | [diff] [blame] | 155 | "Invalid match type %u on %p\n", |
| 156 | asd->match_type, asd); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 157 | return -EINVAL; |
| 158 | } |
| 159 | list_add_tail(&asd->list, ¬ifier->waiting); |
| 160 | } |
| 161 | |
| 162 | mutex_lock(&list_lock); |
| 163 | |
| 164 | /* Keep also completed notifiers on the list */ |
| 165 | list_add(¬ifier->list, ¬ifier_list); |
| 166 | |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 167 | list_for_each_entry_safe(sd, tmp, &subdev_list, async_list) { |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 168 | int ret; |
| 169 | |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 170 | asd = v4l2_async_belongs(notifier, sd); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 171 | if (!asd) |
| 172 | continue; |
| 173 | |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 174 | ret = v4l2_async_test_notify(notifier, sd, asd); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 175 | if (ret < 0) { |
| 176 | mutex_unlock(&list_lock); |
| 177 | return ret; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | mutex_unlock(&list_lock); |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | EXPORT_SYMBOL(v4l2_async_notifier_register); |
| 186 | |
| 187 | void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier) |
| 188 | { |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 189 | struct v4l2_subdev *sd, *tmp; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 190 | unsigned int notif_n_subdev = notifier->num_subdevs; |
| 191 | unsigned int n_subdev = min(notif_n_subdev, V4L2_MAX_SUBDEVS); |
| 192 | struct device *dev[n_subdev]; |
| 193 | int i = 0; |
| 194 | |
Laurent Pinchart | 8e3fbfe | 2013-07-03 07:49:06 -0300 | [diff] [blame] | 195 | if (!notifier->v4l2_dev) |
| 196 | return; |
| 197 | |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 198 | mutex_lock(&list_lock); |
| 199 | |
| 200 | list_del(¬ifier->list); |
| 201 | |
Sylwester Nawrocki | ceedcc4 | 2013-07-31 13:10:18 -0300 | [diff] [blame^] | 202 | list_for_each_entry_safe(sd, tmp, ¬ifier->done, async_list) { |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 203 | dev[i] = get_device(sd->dev); |
| 204 | |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 205 | v4l2_async_cleanup(sd); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 206 | |
| 207 | /* If we handled USB devices, we'd have to lock the parent too */ |
| 208 | device_release_driver(dev[i++]); |
| 209 | |
| 210 | if (notifier->unbind) |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 211 | notifier->unbind(notifier, sd, sd->asd); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | mutex_unlock(&list_lock); |
| 215 | |
| 216 | while (i--) { |
| 217 | struct device *d = dev[i]; |
| 218 | |
| 219 | if (d && device_attach(d) < 0) { |
| 220 | const char *name = "(none)"; |
| 221 | int lock = device_trylock(d); |
| 222 | |
| 223 | if (lock && d->driver) |
| 224 | name = d->driver->name; |
| 225 | dev_err(d, "Failed to re-probe to %s\n", name); |
| 226 | if (lock) |
| 227 | device_unlock(d); |
| 228 | } |
| 229 | put_device(d); |
| 230 | } |
Laurent Pinchart | 8e3fbfe | 2013-07-03 07:49:06 -0300 | [diff] [blame] | 231 | |
| 232 | notifier->v4l2_dev = NULL; |
| 233 | |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 234 | /* |
| 235 | * Don't care about the waiting list, it is initialised and populated |
| 236 | * upon notifier registration. |
| 237 | */ |
| 238 | } |
| 239 | EXPORT_SYMBOL(v4l2_async_notifier_unregister); |
| 240 | |
| 241 | int v4l2_async_register_subdev(struct v4l2_subdev *sd) |
| 242 | { |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 243 | struct v4l2_async_notifier *notifier; |
| 244 | |
| 245 | mutex_lock(&list_lock); |
| 246 | |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 247 | INIT_LIST_HEAD(&sd->async_list); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 248 | |
| 249 | list_for_each_entry(notifier, ¬ifier_list, list) { |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 250 | struct v4l2_async_subdev *asd = v4l2_async_belongs(notifier, sd); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 251 | if (asd) { |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 252 | int ret = v4l2_async_test_notify(notifier, sd, asd); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 253 | mutex_unlock(&list_lock); |
| 254 | return ret; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /* None matched, wait for hot-plugging */ |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 259 | list_add(&sd->async_list, &subdev_list); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 260 | |
| 261 | mutex_unlock(&list_lock); |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | EXPORT_SYMBOL(v4l2_async_register_subdev); |
| 266 | |
| 267 | void v4l2_async_unregister_subdev(struct v4l2_subdev *sd) |
| 268 | { |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 269 | struct v4l2_async_notifier *notifier = sd->notifier; |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 270 | |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 271 | if (!sd->asd) { |
| 272 | if (!list_empty(&sd->async_list)) |
| 273 | v4l2_async_cleanup(sd); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 274 | return; |
| 275 | } |
| 276 | |
| 277 | mutex_lock(&list_lock); |
| 278 | |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 279 | list_add(&sd->asd->list, ¬ifier->waiting); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 280 | |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 281 | v4l2_async_cleanup(sd); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 282 | |
| 283 | if (notifier->unbind) |
Sylwester Nawrocki | b426b3a | 2013-07-22 08:01:33 -0300 | [diff] [blame] | 284 | notifier->unbind(notifier, sd, sd->asd); |
Guennadi Liakhovetski | e9e3104 | 2013-01-08 07:06:31 -0300 | [diff] [blame] | 285 | |
| 286 | mutex_unlock(&list_lock); |
| 287 | } |
| 288 | EXPORT_SYMBOL(v4l2_async_unregister_subdev); |