FILE
mc-request.c
File preview with syntax highlighting.
linux-6.19.14/drivers/media/mc/mc-request.c
1// SPDX-License-Identifier: GPL-2.0 2/* 3 * Media device request objects 4 * 5 * Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved. 6 * Copyright (C) 2018 Intel Corporation 7 * Copyright (C) 2018 Google, Inc. 8 * 9 * Author: Hans Verkuil <hverkuil@kernel.org> 10 * Author: Sakari Ailus <sakari.ailus@linux.intel.com> 11 */ 12 13#include <linux/anon_inodes.h> 14#include <linux/file.h> 15#include <linux/refcount.h> 16 17#include <media/media-device.h> 18#include <media/media-request.h> 19 20static const char * const request_state[] = { 21 [MEDIA_REQUEST_STATE_IDLE] = "idle", 22 [MEDIA_REQUEST_STATE_VALIDATING] = "validating", 23 [MEDIA_REQUEST_STATE_QUEUED] = "queued", 24 [MEDIA_REQUEST_STATE_COMPLETE] = "complete", 25 [MEDIA_REQUEST_STATE_CLEANING] = "cleaning", 26 [MEDIA_REQUEST_STATE_UPDATING] = "updating", 27}; 28 29static const char * 30media_request_state_str(enum media_request_state state) 31{ 32 BUILD_BUG_ON(ARRAY_SIZE(request_state) != NR_OF_MEDIA_REQUEST_STATE); 33 34 if (WARN_ON(state >= ARRAY_SIZE(request_state))) 35 return "invalid"; 36 return request_state[state]; 37} 38 39static void media_request_clean(struct media_request *req) 40{ 41 struct media_request_object *obj, *obj_safe; 42 43 /* Just a sanity check. No other code path is allowed to change this. */ 44 WARN_ON(req->state != MEDIA_REQUEST_STATE_CLEANING); 45 WARN_ON(req->updating_count); 46 WARN_ON(req->access_count); 47 48 list_for_each_entry_safe(obj, obj_safe, &req->objects, list) { 49 media_request_object_unbind(obj); 50 media_request_object_put(obj); 51 } 52 53 req->updating_count = 0; 54 req->access_count = 0; 55 WARN_ON(req->num_incomplete_objects); 56 req->num_incomplete_objects = 0; 57 wake_up_interruptible_all(&req->poll_wait); 58} 59 60static void media_request_release(struct kref *kref) 61{ 62 struct media_request *req = 63 container_of(kref, struct media_request, kref); 64 struct media_device *mdev = req->mdev; 65 66 dev_dbg(mdev->dev, "request: release %s\n", req->debug_str); 67 68 /* No other users, no need for a spinlock */ 69 req->state = MEDIA_REQUEST_STATE_CLEANING; 70 71 media_request_clean(req); 72 73 if (mdev->ops->req_free) 74 mdev->ops->req_free(req); 75 else 76 kfree(req); 77} 78 79void media_request_put(struct media_request *req) 80{ 81 kref_put(&req->kref, media_request_release); 82} 83EXPORT_SYMBOL_GPL(media_request_put); 84 85static int media_request_close(struct inode *inode, struct file *filp) 86{ 87 struct media_request *req = filp->private_data; 88 89 media_request_put(req); 90 return 0; 91} 92 93static __poll_t media_request_poll(struct file *filp, 94 struct poll_table_struct *wait) 95{ 96 struct media_request *req = filp->private_data; 97 unsigned long flags; 98 __poll_t ret = 0; 99 100 if (!(poll_requested_events(wait) & EPOLLPRI)) 101 return 0; 102 103 poll_wait(filp, &req->poll_wait, wait); 104 spin_lock_irqsave(&req->lock, flags); 105 if (req->state == MEDIA_REQUEST_STATE_COMPLETE) { 106 ret = EPOLLPRI; 107 goto unlock; 108 } 109 if (req->state != MEDIA_REQUEST_STATE_QUEUED) { 110 ret = EPOLLERR; 111 goto unlock; 112 } 113 114unlock: 115 spin_unlock_irqrestore(&req->lock, flags); 116 return ret; 117} 118 119static long media_request_ioctl_queue(struct media_request *req) 120{ 121 struct media_device *mdev = req->mdev; 122 enum media_request_state state; 123 unsigned long flags; 124 int ret; 125 126 dev_dbg(mdev->dev, "request: queue %s\n", req->debug_str); 127 128 /* 129 * Ensure the request that is validated will be the one that gets queued 130 * next by serialising the queueing process. This mutex is also used 131 * to serialize with canceling a vb2 queue and with setting values such 132 * as controls in a request. 133 */ 134 mutex_lock(&mdev->req_queue_mutex); 135 136 media_request_get(req); 137 138 spin_lock_irqsave(&req->lock, flags); 139 if (req->state == MEDIA_REQUEST_STATE_IDLE) 140 req->state = MEDIA_REQUEST_STATE_VALIDATING; 141 state = req->state; 142 spin_unlock_irqrestore(&req->lock, flags); 143 if (state != MEDIA_REQUEST_STATE_VALIDATING) { 144 dev_dbg(mdev->dev, 145 "request: unable to queue %s, request in state %s\n", 146 req->debug_str, media_request_state_str(state)); 147 media_request_put(req); 148 mutex_unlock(&mdev->req_queue_mutex); 149 return -EBUSY; 150 } 151 152 ret = mdev->ops->req_validate(req); 153 154 /* 155 * If the req_validate was successful, then we mark the state as QUEUED 156 * and call req_queue. The reason we set the state first is that this 157 * allows req_queue to unbind or complete the queued objects in case 158 * they are immediately 'consumed'. State changes from QUEUED to another 159 * state can only happen if either the driver changes the state or if 160 * the user cancels the vb2 queue. The driver can only change the state 161 * after each object is queued through the req_queue op (and note that 162 * that op cannot fail), so setting the state to QUEUED up front is 163 * safe. 164 * 165 * The other reason for changing the state is if the vb2 queue is 166 * canceled, and that uses the req_queue_mutex which is still locked 167 * while req_queue is called, so that's safe as well. 168 */ 169 spin_lock_irqsave(&req->lock, flags); 170 req->state = ret ? MEDIA_REQUEST_STATE_IDLE 171 : MEDIA_REQUEST_STATE_QUEUED; 172 spin_unlock_irqrestore(&req->lock, flags); 173 174 if (!ret) 175 mdev->ops->req_queue(req); 176 177 mutex_unlock(&mdev->req_queue_mutex); 178 179 if (ret) { 180 dev_dbg(mdev->dev, "request: can't queue %s (%d)\n", 181 req->debug_str, ret); 182 media_request_put(req); 183 } 184 185 return ret; 186} 187 188static long media_request_ioctl_reinit(struct media_request *req) 189{ 190 struct media_device *mdev = req->mdev; 191 unsigned long flags; 192 193 mutex_lock(&mdev->req_queue_mutex); 194 195 spin_lock_irqsave(&req->lock, flags); 196 if (req->state != MEDIA_REQUEST_STATE_IDLE && 197 req->state != MEDIA_REQUEST_STATE_COMPLETE) { 198 dev_dbg(mdev->dev, 199 "request: %s not in idle or complete state, cannot reinit\n", 200 req->debug_str); 201 spin_unlock_irqrestore(&req->lock, flags); 202 mutex_unlock(&mdev->req_queue_mutex); 203 return -EBUSY; 204 } 205 if (req->access_count) { 206 dev_dbg(mdev->dev, 207 "request: %s is being accessed, cannot reinit\n", 208 req->debug_str); 209 spin_unlock_irqrestore(&req->lock, flags); 210 mutex_unlock(&mdev->req_queue_mutex); 211 return -EBUSY; 212 } 213 req->state = MEDIA_REQUEST_STATE_CLEANING; 214 spin_unlock_irqrestore(&req->lock, flags); 215 216 media_request_clean(req); 217 218 spin_lock_irqsave(&req->lock, flags); 219 req->state = MEDIA_REQUEST_STATE_IDLE; 220 spin_unlock_irqrestore(&req->lock, flags); 221 mutex_unlock(&mdev->req_queue_mutex); 222 223 return 0; 224} 225 226static long media_request_ioctl(struct file *filp, unsigned int cmd, 227 unsigned long arg) 228{ 229 struct media_request *req = filp->private_data; 230 231 switch (cmd) { 232 case MEDIA_REQUEST_IOC_QUEUE: 233 return media_request_ioctl_queue(req); 234 case MEDIA_REQUEST_IOC_REINIT: 235 return media_request_ioctl_reinit(req); 236 default: 237 return -ENOIOCTLCMD; 238 } 239} 240 241static const struct file_operations request_fops = { 242 .owner = THIS_MODULE, 243 .poll = media_request_poll, 244 .unlocked_ioctl = media_request_ioctl, 245#ifdef CONFIG_COMPAT 246 .compat_ioctl = media_request_ioctl, 247#endif /* CONFIG_COMPAT */ 248 .release = media_request_close, 249}; 250 251struct media_request * 252media_request_get_by_fd(struct media_device *mdev, int request_fd) 253{ 254 struct media_request *req; 255 256 if (!mdev || !mdev->ops || 257 !mdev->ops->req_validate || !mdev->ops->req_queue) 258 return ERR_PTR(-EBADR); 259 260 CLASS(fd, f)(request_fd); 261 if (fd_empty(f)) 262 goto err; 263 264 if (fd_file(f)->f_op != &request_fops) 265 goto err; 266 req = fd_file(f)->private_data; 267 if (req->mdev != mdev) 268 goto err; 269 270 /* 271 * Note: as long as someone has an open filehandle of the request, 272 * the request can never be released. The fdget() above ensures that 273 * even if userspace closes the request filehandle, the release() 274 * fop won't be called, so the media_request_get() always succeeds 275 * and there is no race condition where the request was released 276 * before media_request_get() is called. 277 */ 278 media_request_get(req); 279 return req; 280 281err: 282 dev_dbg(mdev->dev, "cannot find request_fd %d\n", request_fd); 283 return ERR_PTR(-EINVAL); 284} 285EXPORT_SYMBOL_GPL(media_request_get_by_fd); 286 287int media_request_alloc(struct media_device *mdev, int *alloc_fd) 288{ 289 struct media_request *req; 290 int ret; 291 292 /* Either both are NULL or both are non-NULL */ 293 if (WARN_ON(!mdev->ops->req_alloc ^ !mdev->ops->req_free)) 294 return -ENOMEM; 295 296 if (mdev->ops->req_alloc) 297 req = mdev->ops->req_alloc(mdev); 298 else 299 req = kzalloc(sizeof(*req), GFP_KERNEL); 300 if (!req) 301 return -ENOMEM; 302 303 req->mdev = mdev; 304 req->state = MEDIA_REQUEST_STATE_IDLE; 305 req->num_incomplete_objects = 0; 306 kref_init(&req->kref); 307 INIT_LIST_HEAD(&req->objects); 308 spin_lock_init(&req->lock); 309 init_waitqueue_head(&req->poll_wait); 310 req->updating_count = 0; 311 req->access_count = 0; 312 313 FD_PREPARE(fdf, O_CLOEXEC, 314 anon_inode_getfile("request", &request_fops, NULL, 315 O_CLOEXEC)); 316 if (fdf.err) { 317 ret = fdf.err; 318 goto err_free_req; 319 } 320 321 fd_prepare_file(fdf)->private_data = req; 322 323 snprintf(req->debug_str, sizeof(req->debug_str), "%u:%d", 324 atomic_inc_return(&mdev->request_id), fd_prepare_fd(fdf)); 325 dev_dbg(mdev->dev, "request: allocated %s\n", req->debug_str); 326 327 *alloc_fd = fd_publish(fdf); 328 329 return 0; 330 331err_free_req: 332 if (mdev->ops->req_free) 333 mdev->ops->req_free(req); 334 else 335 kfree(req); 336 337 return ret; 338} 339 340static void media_request_object_release(struct kref *kref) 341{ 342 struct media_request_object *obj = 343 container_of(kref, struct media_request_object, kref); 344 struct media_request *req = obj->req; 345 346 if (WARN_ON(req)) 347 media_request_object_unbind(obj); 348 obj->ops->release(obj); 349} 350 351struct media_request_object * 352media_request_object_find(struct media_request *req, 353 const struct media_request_object_ops *ops, 354 void *priv) 355{ 356 struct media_request_object *obj; 357 struct media_request_object *found = NULL; 358 unsigned long flags; 359 360 if (WARN_ON(!ops || !priv)) 361 return NULL; 362 363 spin_lock_irqsave(&req->lock, flags); 364 list_for_each_entry(obj, &req->objects, list) { 365 if (obj->ops == ops && obj->priv == priv) { 366 media_request_object_get(obj); 367 found = obj; 368 break; 369 } 370 } 371 spin_unlock_irqrestore(&req->lock, flags); 372 return found; 373} 374EXPORT_SYMBOL_GPL(media_request_object_find); 375 376void media_request_object_put(struct media_request_object *obj) 377{ 378 kref_put(&obj->kref, media_request_object_release); 379} 380EXPORT_SYMBOL_GPL(media_request_object_put); 381 382void media_request_object_init(struct media_request_object *obj) 383{ 384 obj->ops = NULL; 385 obj->req = NULL; 386 obj->priv = NULL; 387 obj->completed = false; 388 INIT_LIST_HEAD(&obj->list); 389 kref_init(&obj->kref); 390} 391EXPORT_SYMBOL_GPL(media_request_object_init); 392 393int media_request_object_bind(struct media_request *req, 394 const struct media_request_object_ops *ops, 395 void *priv, bool is_buffer, 396 struct media_request_object *obj) 397{ 398 unsigned long flags; 399 int ret = -EBUSY; 400 401 if (WARN_ON(!ops->release)) 402 return -EBADR; 403 404 spin_lock_irqsave(&req->lock, flags); 405 406 if (WARN_ON(req->state != MEDIA_REQUEST_STATE_UPDATING && 407 req->state != MEDIA_REQUEST_STATE_QUEUED)) 408 goto unlock; 409 410 obj->req = req; 411 obj->ops = ops; 412 obj->priv = priv; 413 414 if (is_buffer) 415 list_add_tail(&obj->list, &req->objects); 416 else 417 list_add(&obj->list, &req->objects); 418 req->num_incomplete_objects++; 419 ret = 0; 420 421unlock: 422 spin_unlock_irqrestore(&req->lock, flags); 423 return ret; 424} 425EXPORT_SYMBOL_GPL(media_request_object_bind); 426 427void media_request_object_unbind(struct media_request_object *obj) 428{ 429 struct media_request *req = obj->req; 430 unsigned long flags; 431 bool completed = false; 432 433 if (WARN_ON(!req)) 434 return; 435 436 spin_lock_irqsave(&req->lock, flags); 437 list_del(&obj->list); 438 obj->req = NULL; 439 440 if (req->state == MEDIA_REQUEST_STATE_COMPLETE) 441 goto unlock; 442 443 if (WARN_ON(req->state == MEDIA_REQUEST_STATE_VALIDATING)) 444 goto unlock; 445 446 if (req->state == MEDIA_REQUEST_STATE_CLEANING) { 447 if (!obj->completed) 448 req->num_incomplete_objects--; 449 goto unlock; 450 } 451 452 if (WARN_ON(!req->num_incomplete_objects)) 453 goto unlock; 454 455 req->num_incomplete_objects--; 456 if (req->state == MEDIA_REQUEST_STATE_QUEUED && 457 !req->num_incomplete_objects) { 458 req->state = MEDIA_REQUEST_STATE_COMPLETE; 459 completed = true; 460 wake_up_interruptible_all(&req->poll_wait); 461 } 462 463unlock: 464 spin_unlock_irqrestore(&req->lock, flags); 465 if (obj->ops->unbind) 466 obj->ops->unbind(obj); 467 if (completed) 468 media_request_put(req); 469} 470EXPORT_SYMBOL_GPL(media_request_object_unbind); 471 472void media_request_object_complete(struct media_request_object *obj) 473{ 474 struct media_request *req = obj->req; 475 unsigned long flags; 476 bool completed = false; 477 478 spin_lock_irqsave(&req->lock, flags); 479 if (obj->completed) 480 goto unlock; 481 obj->completed = true; 482 if (WARN_ON(!req->num_incomplete_objects) || 483 WARN_ON(req->state != MEDIA_REQUEST_STATE_QUEUED)) 484 goto unlock; 485 486 if (!--req->num_incomplete_objects) { 487 req->state = MEDIA_REQUEST_STATE_COMPLETE; 488 wake_up_interruptible_all(&req->poll_wait); 489 completed = true; 490 } 491unlock: 492 spin_unlock_irqrestore(&req->lock, flags); 493 if (completed) 494 media_request_put(req); 495} 496EXPORT_SYMBOL_GPL(media_request_object_complete);
~/
Read-only file preview.