v4l2-device.h 19 KB

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