v4l2-device.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. V4L2 device support header.
  4. Copyright (C) 2008 Hans Verkuil <hverkuil@kernel.org>
  5. */
  6. #ifndef _V4L2_DEVICE_H
  7. #define _V4L2_DEVICE_H
  8. #include <media/media-device.h>
  9. #include <media/v4l2-subdev.h>
  10. #include <media/v4l2-dev.h>
  11. struct v4l2_ctrl_handler;
  12. /**
  13. * struct v4l2_device - main struct to for V4L2 device drivers
  14. *
  15. * @dev: pointer to struct device.
  16. * @mdev: pointer to struct media_device, may be NULL.
  17. * @subdevs: used to keep track of the registered subdevs
  18. * @lock: lock this struct; can be used by the driver as well
  19. * if this struct is embedded into a larger struct.
  20. * @name: unique device name, by default the driver name + bus ID
  21. * @notify: notify operation called by some sub-devices.
  22. * @ctrl_handler: The control handler. May be %NULL.
  23. * @prio: Device's priority state
  24. * @ref: Keep track of the references to this struct.
  25. * @release: Release function that is called when the ref count
  26. * goes to 0.
  27. *
  28. * Each instance of a V4L2 device should create the v4l2_device struct,
  29. * either stand-alone or embedded in a larger struct.
  30. *
  31. * It allows easy access to sub-devices (see v4l2-subdev.h) and provides
  32. * basic V4L2 device-level support.
  33. *
  34. * .. note::
  35. *
  36. * #) @dev->driver_data points to this struct.
  37. * #) @dev might be %NULL if there is no parent device
  38. */
  39. struct v4l2_device {
  40. struct device *dev;
  41. struct media_device *mdev;
  42. struct list_head subdevs;
  43. spinlock_t lock;
  44. char name[36];
  45. void (*notify)(struct v4l2_subdev *sd,
  46. unsigned int notification, void *arg);
  47. struct v4l2_ctrl_handler *ctrl_handler;
  48. struct v4l2_prio_state prio;
  49. struct kref ref;
  50. void (*release)(struct v4l2_device *v4l2_dev);
  51. };
  52. /**
  53. * v4l2_device_get - gets a V4L2 device reference
  54. *
  55. * @v4l2_dev: pointer to struct &v4l2_device
  56. *
  57. * This is an ancillary routine meant to increment the usage for the
  58. * struct &v4l2_device pointed by @v4l2_dev.
  59. */
  60. static inline void v4l2_device_get(struct v4l2_device *v4l2_dev)
  61. {
  62. kref_get(&v4l2_dev->ref);
  63. }
  64. /**
  65. * v4l2_device_put - puts a V4L2 device reference
  66. *
  67. * @v4l2_dev: pointer to struct &v4l2_device
  68. *
  69. * This is an ancillary routine meant to decrement the usage for the
  70. * struct &v4l2_device pointed by @v4l2_dev.
  71. */
  72. int v4l2_device_put(struct v4l2_device *v4l2_dev);
  73. /**
  74. * v4l2_device_register - Initialize v4l2_dev and make @dev->driver_data
  75. * point to @v4l2_dev.
  76. *
  77. * @dev: pointer to struct &device
  78. * @v4l2_dev: pointer to struct &v4l2_device
  79. *
  80. * .. note::
  81. * @dev may be %NULL in rare cases (ISA devices).
  82. * In such case the caller must fill in the @v4l2_dev->name field
  83. * before calling this function.
  84. */
  85. int __must_check v4l2_device_register(struct device *dev,
  86. struct v4l2_device *v4l2_dev);
  87. /**
  88. * v4l2_device_set_name - Optional function to initialize the
  89. * name field of struct &v4l2_device
  90. *
  91. * @v4l2_dev: pointer to struct &v4l2_device
  92. * @basename: base name for the device name
  93. * @instance: pointer to a static atomic_t var with the instance usage for
  94. * the device driver.
  95. *
  96. * v4l2_device_set_name() initializes the name field of struct &v4l2_device
  97. * using the driver name and a driver-global atomic_t instance.
  98. *
  99. * This function will increment the instance counter and returns the
  100. * instance value used in the name.
  101. *
  102. * Example:
  103. *
  104. * static atomic_t drv_instance = ATOMIC_INIT(0);
  105. *
  106. * ...
  107. *
  108. * instance = v4l2_device_set_name(&\ v4l2_dev, "foo", &\ drv_instance);
  109. *
  110. * The first time this is called the name field will be set to foo0 and
  111. * this function returns 0. If the name ends with a digit (e.g. cx18),
  112. * then the name will be set to cx18-0 since cx180 would look really odd.
  113. */
  114. int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
  115. atomic_t *instance);
  116. /**
  117. * v4l2_device_disconnect - Change V4L2 device state to disconnected.
  118. *
  119. * @v4l2_dev: pointer to struct v4l2_device
  120. *
  121. * Should be called when the USB parent disconnects.
  122. * Since the parent disappears, this ensures that @v4l2_dev doesn't have
  123. * an invalid parent pointer.
  124. *
  125. * .. note:: This function sets @v4l2_dev->dev to NULL.
  126. */
  127. void v4l2_device_disconnect(struct v4l2_device *v4l2_dev);
  128. /**
  129. * v4l2_device_unregister - Unregister all sub-devices and any other
  130. * resources related to @v4l2_dev.
  131. *
  132. * @v4l2_dev: pointer to struct v4l2_device
  133. */
  134. void v4l2_device_unregister(struct v4l2_device *v4l2_dev);
  135. /**
  136. * v4l2_device_register_subdev - Registers a subdev with a v4l2 device.
  137. *
  138. * @v4l2_dev: pointer to struct &v4l2_device
  139. * @sd: pointer to &struct v4l2_subdev
  140. *
  141. * While registered, the subdev module is marked as in-use.
  142. *
  143. * An error is returned if the module is no longer loaded on any attempts
  144. * to register it.
  145. */
  146. #define v4l2_device_register_subdev(v4l2_dev, sd) \
  147. __v4l2_device_register_subdev(v4l2_dev, sd, THIS_MODULE)
  148. int __must_check __v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
  149. struct v4l2_subdev *sd,
  150. struct module *module);
  151. /**
  152. * v4l2_device_unregister_subdev - Unregisters a subdev with a v4l2 device.
  153. *
  154. * @sd: pointer to &struct v4l2_subdev
  155. *
  156. * .. note ::
  157. *
  158. * Can also be called if the subdev wasn't registered. In such
  159. * case, it will do nothing.
  160. */
  161. void v4l2_device_unregister_subdev(struct v4l2_subdev *sd);
  162. /**
  163. * __v4l2_device_register_subdev_nodes - Registers device nodes for
  164. * all subdevs of the v4l2 device that are marked with the
  165. * %V4L2_SUBDEV_FL_HAS_DEVNODE flag.
  166. *
  167. * @v4l2_dev: pointer to struct v4l2_device
  168. * @read_only: subdevices read-only flag. True to register the subdevices
  169. * device nodes in read-only mode, false to allow full access to the
  170. * subdevice userspace API.
  171. */
  172. int __must_check
  173. __v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
  174. bool read_only);
  175. /**
  176. * v4l2_device_register_subdev_nodes - Registers subdevices device nodes with
  177. * unrestricted access to the subdevice userspace operations
  178. *
  179. * Internally calls __v4l2_device_register_subdev_nodes(). See its documentation
  180. * for more details.
  181. *
  182. * @v4l2_dev: pointer to struct v4l2_device
  183. */
  184. static inline int __must_check
  185. v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
  186. {
  187. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  188. return __v4l2_device_register_subdev_nodes(v4l2_dev, false);
  189. #else
  190. return 0;
  191. #endif
  192. }
  193. /**
  194. * v4l2_device_register_ro_subdev_nodes - Registers subdevices device nodes
  195. * in read-only mode
  196. *
  197. * Internally calls __v4l2_device_register_subdev_nodes(). See its documentation
  198. * for more details.
  199. *
  200. * @v4l2_dev: pointer to struct v4l2_device
  201. */
  202. static inline int __must_check
  203. v4l2_device_register_ro_subdev_nodes(struct v4l2_device *v4l2_dev)
  204. {
  205. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  206. return __v4l2_device_register_subdev_nodes(v4l2_dev, true);
  207. #else
  208. return 0;
  209. #endif
  210. }
  211. /**
  212. * v4l2_subdev_notify - Sends a notification to v4l2_device.
  213. *
  214. * @sd: pointer to &struct v4l2_subdev
  215. * @notification: type of notification. Please notice that the notification
  216. * type is driver-specific.
  217. * @arg: arguments for the notification. Those are specific to each
  218. * notification type.
  219. */
  220. static inline void v4l2_subdev_notify(struct v4l2_subdev *sd,
  221. unsigned int notification, void *arg)
  222. {
  223. if (sd && sd->v4l2_dev && sd->v4l2_dev->notify)
  224. sd->v4l2_dev->notify(sd, notification, arg);
  225. }
  226. /**
  227. * v4l2_device_supports_requests - Test if requests are supported.
  228. *
  229. * @v4l2_dev: pointer to struct v4l2_device
  230. */
  231. static inline bool v4l2_device_supports_requests(struct v4l2_device *v4l2_dev)
  232. {
  233. return v4l2_dev->mdev && v4l2_dev->mdev->ops &&
  234. v4l2_dev->mdev->ops->req_queue;
  235. }
  236. /* Helper macros to iterate over all subdevs. */
  237. /**
  238. * v4l2_device_for_each_subdev - Helper macro that interates over all
  239. * sub-devices of a given &v4l2_device.
  240. *
  241. * @sd: pointer that will be filled by the macro with all
  242. * &struct v4l2_subdev pointer used as an iterator by the loop.
  243. * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
  244. *
  245. * This macro iterates over all sub-devices owned by the @v4l2_dev device.
  246. * It acts as a for loop iterator and executes the next statement with
  247. * the @sd variable pointing to each sub-device in turn.
  248. */
  249. #define v4l2_device_for_each_subdev(sd, v4l2_dev) \
  250. list_for_each_entry(sd, &(v4l2_dev)->subdevs, list)
  251. /**
  252. * __v4l2_device_call_subdevs_p - Calls the specified operation for
  253. * all subdevs matching the condition.
  254. *
  255. * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
  256. * @sd: pointer that will be filled by the macro with all
  257. * &struct v4l2_subdev pointer used as an iterator by the loop.
  258. * @cond: condition to be match
  259. * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
  260. * Each element there groups a set of operations functions.
  261. * @f: operation function that will be called if @cond matches.
  262. * The operation functions are defined in groups, according to
  263. * each element at &struct v4l2_subdev_ops.
  264. * @args: arguments for @f.
  265. *
  266. * Ignore any errors.
  267. *
  268. * Note: subdevs cannot be added or deleted while walking
  269. * the subdevs list.
  270. */
  271. #define __v4l2_device_call_subdevs_p(v4l2_dev, sd, cond, o, f, args...) \
  272. do { \
  273. list_for_each_entry((sd), &(v4l2_dev)->subdevs, list) \
  274. if ((cond) && (sd)->ops->o && (sd)->ops->o->f) \
  275. (sd)->ops->o->f((sd) , ##args); \
  276. } while (0)
  277. /**
  278. * __v4l2_device_call_subdevs - Calls the specified operation for
  279. * all subdevs matching the condition.
  280. *
  281. * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
  282. * @cond: condition to be match
  283. * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
  284. * Each element there groups a set of operations functions.
  285. * @f: operation function that will be called if @cond matches.
  286. * The operation functions are defined in groups, according to
  287. * each element at &struct v4l2_subdev_ops.
  288. * @args: arguments for @f.
  289. *
  290. * Ignore any errors.
  291. *
  292. * Note: subdevs cannot be added or deleted while walking
  293. * the subdevs list.
  294. */
  295. #define __v4l2_device_call_subdevs(v4l2_dev, cond, o, f, args...) \
  296. do { \
  297. struct v4l2_subdev *__sd; \
  298. \
  299. __v4l2_device_call_subdevs_p(v4l2_dev, __sd, cond, o, \
  300. f , ##args); \
  301. } while (0)
  302. /**
  303. * __v4l2_device_call_subdevs_until_err_p - Calls the specified operation for
  304. * all subdevs matching the condition.
  305. *
  306. * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
  307. * @sd: pointer that will be filled by the macro with all
  308. * &struct v4l2_subdev sub-devices associated with @v4l2_dev.
  309. * @cond: condition to be match
  310. * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
  311. * Each element there groups a set of operations functions.
  312. * @f: operation function that will be called if @cond matches.
  313. * The operation functions are defined in groups, according to
  314. * each element at &struct v4l2_subdev_ops.
  315. * @args: arguments for @f.
  316. *
  317. * Return:
  318. *
  319. * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
  320. * for any subdevice, then abort and return with that error code, zero
  321. * otherwise.
  322. *
  323. * Note: subdevs cannot be added or deleted while walking
  324. * the subdevs list.
  325. */
  326. #define __v4l2_device_call_subdevs_until_err_p(v4l2_dev, sd, cond, o, f, args...) \
  327. ({ \
  328. long __err = 0; \
  329. \
  330. list_for_each_entry((sd), &(v4l2_dev)->subdevs, list) { \
  331. if ((cond) && (sd)->ops->o && (sd)->ops->o->f) \
  332. __err = (sd)->ops->o->f((sd) , ##args); \
  333. if (__err && __err != -ENOIOCTLCMD) \
  334. break; \
  335. } \
  336. (__err == -ENOIOCTLCMD) ? 0 : __err; \
  337. })
  338. /**
  339. * __v4l2_device_call_subdevs_until_err - Calls the specified operation for
  340. * all subdevs matching the condition.
  341. *
  342. * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
  343. * @cond: condition to be match
  344. * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
  345. * Each element there groups a set of operations functions.
  346. * @f: operation function that will be called if @cond matches.
  347. * The operation functions are defined in groups, according to
  348. * each element at &struct v4l2_subdev_ops.
  349. * @args: arguments for @f.
  350. *
  351. * Return:
  352. *
  353. * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
  354. * for any subdevice, then abort and return with that error code,
  355. * zero otherwise.
  356. *
  357. * Note: subdevs cannot be added or deleted while walking
  358. * the subdevs list.
  359. */
  360. #define __v4l2_device_call_subdevs_until_err(v4l2_dev, cond, o, f, args...) \
  361. ({ \
  362. struct v4l2_subdev *__sd; \
  363. __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, cond, o, \
  364. f , ##args); \
  365. })
  366. /**
  367. * v4l2_device_call_all - Calls the specified operation for
  368. * all subdevs matching the &v4l2_subdev.grp_id, as assigned
  369. * by the bridge driver.
  370. *
  371. * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
  372. * @grpid: &struct v4l2_subdev->grp_id group ID to match.
  373. * Use 0 to match them all.
  374. * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
  375. * Each element there groups a set of operations functions.
  376. * @f: operation function that will be called if @cond matches.
  377. * The operation functions are defined in groups, according to
  378. * each element at &struct v4l2_subdev_ops.
  379. * @args: arguments for @f.
  380. *
  381. * Ignore any errors.
  382. *
  383. * Note: subdevs cannot be added or deleted while walking
  384. * the subdevs list.
  385. */
  386. #define v4l2_device_call_all(v4l2_dev, grpid, o, f, args...) \
  387. do { \
  388. struct v4l2_subdev *__sd; \
  389. \
  390. __v4l2_device_call_subdevs_p(v4l2_dev, __sd, \
  391. (grpid) == 0 || __sd->grp_id == (grpid), o, f , \
  392. ##args); \
  393. } while (0)
  394. /**
  395. * v4l2_device_call_until_err - Calls the specified operation for
  396. * all subdevs matching the &v4l2_subdev.grp_id, as assigned
  397. * by the bridge driver, until an error occurs.
  398. *
  399. * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
  400. * @grpid: &struct v4l2_subdev->grp_id group ID to match.
  401. * Use 0 to match them all.
  402. * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
  403. * Each element there groups a set of operations functions.
  404. * @f: operation function that will be called if @cond matches.
  405. * The operation functions are defined in groups, according to
  406. * each element at &struct v4l2_subdev_ops.
  407. * @args: arguments for @f.
  408. *
  409. * Return:
  410. *
  411. * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
  412. * for any subdevice, then abort and return with that error code,
  413. * zero otherwise.
  414. *
  415. * Note: subdevs cannot be added or deleted while walking
  416. * the subdevs list.
  417. */
  418. #define v4l2_device_call_until_err(v4l2_dev, grpid, o, f, args...) \
  419. ({ \
  420. struct v4l2_subdev *__sd; \
  421. __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, \
  422. (grpid) == 0 || __sd->grp_id == (grpid), o, f , \
  423. ##args); \
  424. })
  425. /**
  426. * v4l2_device_mask_call_all - Calls the specified operation for
  427. * all subdevices where a group ID matches a specified bitmask.
  428. *
  429. * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
  430. * @grpmsk: bitmask to be checked against &struct v4l2_subdev->grp_id
  431. * group ID to be matched. Use 0 to match them all.
  432. * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
  433. * Each element there groups a set of operations functions.
  434. * @f: operation function that will be called if @cond matches.
  435. * The operation functions are defined in groups, according to
  436. * each element at &struct v4l2_subdev_ops.
  437. * @args: arguments for @f.
  438. *
  439. * Ignore any errors.
  440. *
  441. * Note: subdevs cannot be added or deleted while walking
  442. * the subdevs list.
  443. */
  444. #define v4l2_device_mask_call_all(v4l2_dev, grpmsk, o, f, args...) \
  445. do { \
  446. struct v4l2_subdev *__sd; \
  447. \
  448. __v4l2_device_call_subdevs_p(v4l2_dev, __sd, \
  449. (grpmsk) == 0 || (__sd->grp_id & (grpmsk)), o, \
  450. f , ##args); \
  451. } while (0)
  452. /**
  453. * v4l2_device_mask_call_until_err - Calls the specified operation for
  454. * all subdevices where a group ID matches a specified bitmask.
  455. *
  456. * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
  457. * @grpmsk: bitmask to be checked against &struct v4l2_subdev->grp_id
  458. * group ID to be matched. Use 0 to match them all.
  459. * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
  460. * Each element there groups a set of operations functions.
  461. * @f: operation function that will be called if @cond matches.
  462. * The operation functions are defined in groups, according to
  463. * each element at &struct v4l2_subdev_ops.
  464. * @args: arguments for @f.
  465. *
  466. * Return:
  467. *
  468. * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
  469. * for any subdevice, then abort and return with that error code,
  470. * zero otherwise.
  471. *
  472. * Note: subdevs cannot be added or deleted while walking
  473. * the subdevs list.
  474. */
  475. #define v4l2_device_mask_call_until_err(v4l2_dev, grpmsk, o, f, args...) \
  476. ({ \
  477. struct v4l2_subdev *__sd; \
  478. __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, \
  479. (grpmsk) == 0 || (__sd->grp_id & (grpmsk)), o, \
  480. f , ##args); \
  481. })
  482. /**
  483. * v4l2_device_has_op - checks if any subdev with matching grpid has a
  484. * given ops.
  485. *
  486. * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
  487. * @grpid: &struct v4l2_subdev->grp_id group ID to match.
  488. * Use 0 to match them all.
  489. * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
  490. * Each element there groups a set of operations functions.
  491. * @f: operation function that will be called if @cond matches.
  492. * The operation functions are defined in groups, according to
  493. * each element at &struct v4l2_subdev_ops.
  494. */
  495. #define v4l2_device_has_op(v4l2_dev, grpid, o, f) \
  496. ({ \
  497. struct v4l2_subdev *__sd; \
  498. bool __result = false; \
  499. list_for_each_entry(__sd, &(v4l2_dev)->subdevs, list) { \
  500. if ((grpid) && __sd->grp_id != (grpid)) \
  501. continue; \
  502. if (v4l2_subdev_has_op(__sd, o, f)) { \
  503. __result = true; \
  504. break; \
  505. } \
  506. } \
  507. __result; \
  508. })
  509. /**
  510. * v4l2_device_mask_has_op - checks if any subdev with matching group
  511. * mask has a given ops.
  512. *
  513. * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
  514. * @grpmsk: bitmask to be checked against &struct v4l2_subdev->grp_id
  515. * group ID to be matched. Use 0 to match them all.
  516. * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
  517. * Each element there groups a set of operations functions.
  518. * @f: operation function that will be called if @cond matches.
  519. * The operation functions are defined in groups, according to
  520. * each element at &struct v4l2_subdev_ops.
  521. */
  522. #define v4l2_device_mask_has_op(v4l2_dev, grpmsk, o, f) \
  523. ({ \
  524. struct v4l2_subdev *__sd; \
  525. bool __result = false; \
  526. list_for_each_entry(__sd, &(v4l2_dev)->subdevs, list) { \
  527. if ((grpmsk) && !(__sd->grp_id & (grpmsk))) \
  528. continue; \
  529. if (v4l2_subdev_has_op(__sd, o, f)) { \
  530. __result = true; \
  531. break; \
  532. } \
  533. } \
  534. __result; \
  535. })
  536. #endif