v4l2-device.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 callback 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. /* Iterate over all subdevs. */
  197. #define v4l2_device_for_each_subdev(sd, v4l2_dev) \
  198. list_for_each_entry(sd, &(v4l2_dev)->subdevs, list)
  199. /* Call the specified callback for all subdevs matching the condition.
  200. Ignore any errors. Note that you cannot add or delete a subdev
  201. while walking the subdevs list. */
  202. #define __v4l2_device_call_subdevs_p(v4l2_dev, sd, cond, o, f, args...) \
  203. do { \
  204. list_for_each_entry((sd), &(v4l2_dev)->subdevs, list) \
  205. if ((cond) && (sd)->ops->o && (sd)->ops->o->f) \
  206. (sd)->ops->o->f((sd) , ##args); \
  207. } while (0)
  208. #define __v4l2_device_call_subdevs(v4l2_dev, cond, o, f, args...) \
  209. do { \
  210. struct v4l2_subdev *__sd; \
  211. \
  212. __v4l2_device_call_subdevs_p(v4l2_dev, __sd, cond, o, \
  213. f , ##args); \
  214. } while (0)
  215. /* Call the specified callback for all subdevs matching the condition.
  216. If the callback returns an error other than 0 or -ENOIOCTLCMD, then
  217. return with that error code. Note that you cannot add or delete a
  218. subdev while walking the subdevs list. */
  219. #define __v4l2_device_call_subdevs_until_err_p(v4l2_dev, sd, cond, o, f, args...) \
  220. ({ \
  221. long __err = 0; \
  222. \
  223. list_for_each_entry((sd), &(v4l2_dev)->subdevs, list) { \
  224. if ((cond) && (sd)->ops->o && (sd)->ops->o->f) \
  225. __err = (sd)->ops->o->f((sd) , ##args); \
  226. if (__err && __err != -ENOIOCTLCMD) \
  227. break; \
  228. } \
  229. (__err == -ENOIOCTLCMD) ? 0 : __err; \
  230. })
  231. #define __v4l2_device_call_subdevs_until_err(v4l2_dev, cond, o, f, args...) \
  232. ({ \
  233. struct v4l2_subdev *__sd; \
  234. __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, cond, o, \
  235. f , ##args); \
  236. })
  237. /* Call the specified callback for all subdevs matching grp_id (if 0, then
  238. match them all). Ignore any errors. Note that you cannot add or delete
  239. a subdev while walking the subdevs list. */
  240. #define v4l2_device_call_all(v4l2_dev, grpid, o, f, args...) \
  241. do { \
  242. struct v4l2_subdev *__sd; \
  243. \
  244. __v4l2_device_call_subdevs_p(v4l2_dev, __sd, \
  245. !(grpid) || __sd->grp_id == (grpid), o, f , \
  246. ##args); \
  247. } while (0)
  248. /* Call the specified callback for all subdevs matching grp_id (if 0, then
  249. match them all). If the callback returns an error other than 0 or
  250. -ENOIOCTLCMD, then return with that error code. Note that you cannot
  251. add or delete a subdev while walking the subdevs list. */
  252. #define v4l2_device_call_until_err(v4l2_dev, grpid, o, f, args...) \
  253. ({ \
  254. struct v4l2_subdev *__sd; \
  255. __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, \
  256. !(grpid) || __sd->grp_id == (grpid), o, f , \
  257. ##args); \
  258. })
  259. /*
  260. * Call the specified callback for all subdevs where grp_id & grpmsk != 0
  261. * (if grpmsk == `0, then match them all). Ignore any errors. Note that you
  262. * cannot add or delete a subdev while walking the subdevs list.
  263. */
  264. #define v4l2_device_mask_call_all(v4l2_dev, grpmsk, o, f, args...) \
  265. do { \
  266. struct v4l2_subdev *__sd; \
  267. \
  268. __v4l2_device_call_subdevs_p(v4l2_dev, __sd, \
  269. !(grpmsk) || (__sd->grp_id & (grpmsk)), o, f , \
  270. ##args); \
  271. } while (0)
  272. /*
  273. * Call the specified callback for all subdevs where grp_id & grpmsk != 0
  274. * (if grpmsk == 0, then match them all). If the callback returns an error
  275. * other than 0 or %-ENOIOCTLCMD, then return with that error code. Note that
  276. * you cannot add or delete a subdev while walking the subdevs list.
  277. */
  278. #define v4l2_device_mask_call_until_err(v4l2_dev, grpmsk, o, f, args...) \
  279. ({ \
  280. struct v4l2_subdev *__sd; \
  281. __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, \
  282. !(grpmsk) || (__sd->grp_id & (grpmsk)), o, f , \
  283. ##args); \
  284. })
  285. /*
  286. * Does any subdev with matching grpid (or all if grpid == 0) has the given
  287. * op?
  288. */
  289. #define v4l2_device_has_op(v4l2_dev, grpid, o, f) \
  290. ({ \
  291. struct v4l2_subdev *__sd; \
  292. bool __result = false; \
  293. list_for_each_entry(__sd, &(v4l2_dev)->subdevs, list) { \
  294. if ((grpid) && __sd->grp_id != (grpid)) \
  295. continue; \
  296. if (v4l2_subdev_has_op(__sd, o, f)) { \
  297. __result = true; \
  298. break; \
  299. } \
  300. } \
  301. __result; \
  302. })
  303. /*
  304. * Does any subdev with matching grpmsk (or all if grpmsk == 0) has the given
  305. * op?
  306. */
  307. #define v4l2_device_mask_has_op(v4l2_dev, grpmsk, o, f) \
  308. ({ \
  309. struct v4l2_subdev *__sd; \
  310. bool __result = false; \
  311. list_for_each_entry(__sd, &(v4l2_dev)->subdevs, list) { \
  312. if ((grpmsk) && !(__sd->grp_id & (grpmsk))) \
  313. continue; \
  314. if (v4l2_subdev_has_op(__sd, o, f)) { \
  315. __result = true; \
  316. break; \
  317. } \
  318. } \
  319. __result; \
  320. })
  321. #endif