v4l2-device.h 18 KB

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