v4l2-device.h 18 KB

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