media-device.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Media device
  3. *
  4. * Copyright (C) 2010 Nokia Corporation
  5. *
  6. * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  7. * Sakari Ailus <sakari.ailus@iki.fi>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #ifndef _MEDIA_DEVICE_H
  23. #define _MEDIA_DEVICE_H
  24. #include <linux/list.h>
  25. #include <linux/mutex.h>
  26. #include <media/media-devnode.h>
  27. #include <media/media-entity.h>
  28. struct ida;
  29. struct device;
  30. /**
  31. * struct media_entity_notify - Media Entity Notify
  32. *
  33. * @list: List head
  34. * @notify_data: Input data to invoke the callback
  35. * @notify: Callback function pointer
  36. *
  37. * Drivers may register a callback to take action when new entities get
  38. * registered with the media device. This handler is intended for creating
  39. * links between existing entities and should not create entities and register
  40. * them.
  41. */
  42. struct media_entity_notify {
  43. struct list_head list;
  44. void *notify_data;
  45. void (*notify)(struct media_entity *entity, void *notify_data);
  46. };
  47. /**
  48. * struct media_device_ops - Media device operations
  49. * @link_notify: Link state change notification callback. This callback is
  50. * called with the graph_mutex held.
  51. */
  52. struct media_device_ops {
  53. int (*link_notify)(struct media_link *link, u32 flags,
  54. unsigned int notification);
  55. };
  56. /**
  57. * struct media_device - Media device
  58. * @dev: Parent device
  59. * @devnode: Media device node
  60. * @driver_name: Optional device driver name. If not set, calls to
  61. * %MEDIA_IOC_DEVICE_INFO will return ``dev->driver->name``.
  62. * This is needed for USB drivers for example, as otherwise
  63. * they'll all appear as if the driver name was "usb".
  64. * @model: Device model name
  65. * @serial: Device serial number (optional)
  66. * @bus_info: Unique and stable device location identifier
  67. * @hw_revision: Hardware device revision
  68. * @driver_version: Device driver version
  69. * @topology_version: Monotonic counter for storing the version of the graph
  70. * topology. Should be incremented each time the topology changes.
  71. * @id: Unique ID used on the last registered graph object
  72. * @entity_internal_idx: Unique internal entity ID used by the graph traversal
  73. * algorithms
  74. * @entity_internal_idx_max: Allocated internal entity indices
  75. * @entities: List of registered entities
  76. * @interfaces: List of registered interfaces
  77. * @pads: List of registered pads
  78. * @links: List of registered links
  79. * @entity_notify: List of registered entity_notify callbacks
  80. * @graph_mutex: Protects access to struct media_device data
  81. * @pm_count_walk: Graph walk for power state walk. Access serialised using
  82. * graph_mutex.
  83. *
  84. * @source_priv: Driver Private data for enable/disable source handlers
  85. * @enable_source: Enable Source Handler function pointer
  86. * @disable_source: Disable Source Handler function pointer
  87. *
  88. * @ops: Operation handler callbacks
  89. *
  90. * This structure represents an abstract high-level media device. It allows easy
  91. * access to entities and provides basic media device-level support. The
  92. * structure can be allocated directly or embedded in a larger structure.
  93. *
  94. * The parent @dev is a physical device. It must be set before registering the
  95. * media device.
  96. *
  97. * @model is a descriptive model name exported through sysfs. It doesn't have to
  98. * be unique.
  99. *
  100. * @enable_source is a handler to find source entity for the
  101. * sink entity and activate the link between them if source
  102. * entity is free. Drivers should call this handler before
  103. * accessing the source.
  104. *
  105. * @disable_source is a handler to find source entity for the
  106. * sink entity and deactivate the link between them. Drivers
  107. * should call this handler to release the source.
  108. *
  109. * Use-case: find tuner entity connected to the decoder
  110. * entity and check if it is available, and activate the
  111. * the link between them from @enable_source and deactivate
  112. * from @disable_source.
  113. *
  114. * .. note::
  115. *
  116. * Bridge driver is expected to implement and set the
  117. * handler when &media_device is registered or when
  118. * bridge driver finds the media_device during probe.
  119. * Bridge driver sets source_priv with information
  120. * necessary to run @enable_source and @disable_source handlers.
  121. */
  122. struct media_device {
  123. /* dev->driver_data points to this struct. */
  124. struct device *dev;
  125. struct media_devnode *devnode;
  126. char model[32];
  127. char driver_name[32];
  128. char serial[40];
  129. char bus_info[32];
  130. u32 hw_revision;
  131. u32 driver_version;
  132. u64 topology_version;
  133. u32 id;
  134. struct ida entity_internal_idx;
  135. int entity_internal_idx_max;
  136. struct list_head entities;
  137. struct list_head interfaces;
  138. struct list_head pads;
  139. struct list_head links;
  140. /* notify callback list invoked when a new entity is registered */
  141. struct list_head entity_notify;
  142. /* Serializes graph operations. */
  143. struct mutex graph_mutex;
  144. struct media_entity_graph pm_count_walk;
  145. void *source_priv;
  146. int (*enable_source)(struct media_entity *entity,
  147. struct media_pipeline *pipe);
  148. void (*disable_source)(struct media_entity *entity);
  149. const struct media_device_ops *ops;
  150. };
  151. /* We don't need to include pci.h or usb.h here */
  152. struct pci_dev;
  153. struct usb_device;
  154. #ifdef CONFIG_MEDIA_CONTROLLER
  155. /* Supported link_notify @notification values. */
  156. #define MEDIA_DEV_NOTIFY_PRE_LINK_CH 0
  157. #define MEDIA_DEV_NOTIFY_POST_LINK_CH 1
  158. /**
  159. * media_entity_enum_init - Initialise an entity enumeration
  160. *
  161. * @ent_enum: Entity enumeration to be initialised
  162. * @mdev: The related media device
  163. *
  164. * Return: zero on success or a negative error code.
  165. */
  166. static inline __must_check int media_entity_enum_init(
  167. struct media_entity_enum *ent_enum, struct media_device *mdev)
  168. {
  169. return __media_entity_enum_init(ent_enum,
  170. mdev->entity_internal_idx_max + 1);
  171. }
  172. /**
  173. * media_device_init() - Initializes a media device element
  174. *
  175. * @mdev: pointer to struct &media_device
  176. *
  177. * This function initializes the media device prior to its registration.
  178. * The media device initialization and registration is split in two functions
  179. * to avoid race conditions and make the media device available to user-space
  180. * before the media graph has been completed.
  181. *
  182. * So drivers need to first initialize the media device, register any entity
  183. * within the media device, create pad to pad links and then finally register
  184. * the media device by calling media_device_register() as a final step.
  185. */
  186. void media_device_init(struct media_device *mdev);
  187. /**
  188. * media_device_cleanup() - Cleanups a media device element
  189. *
  190. * @mdev: pointer to struct &media_device
  191. *
  192. * This function that will destroy the graph_mutex that is
  193. * initialized in media_device_init().
  194. */
  195. void media_device_cleanup(struct media_device *mdev);
  196. /**
  197. * __media_device_register() - Registers a media device element
  198. *
  199. * @mdev: pointer to struct &media_device
  200. * @owner: should be filled with %THIS_MODULE
  201. *
  202. * Users, should, instead, call the media_device_register() macro.
  203. *
  204. * The caller is responsible for initializing the &media_device structure
  205. * before registration. The following fields of &media_device must be set:
  206. *
  207. * - &media_entity.dev must point to the parent device (usually a &pci_dev,
  208. * &usb_interface or &platform_device instance).
  209. *
  210. * - &media_entity.model must be filled with the device model name as a
  211. * NUL-terminated UTF-8 string. The device/model revision must not be
  212. * stored in this field.
  213. *
  214. * The following fields are optional:
  215. *
  216. * - &media_entity.serial is a unique serial number stored as a
  217. * NUL-terminated ASCII string. The field is big enough to store a GUID
  218. * in text form. If the hardware doesn't provide a unique serial number
  219. * this field must be left empty.
  220. *
  221. * - &media_entity.bus_info represents the location of the device in the
  222. * system as a NUL-terminated ASCII string. For PCI/PCIe devices
  223. * &media_entity.bus_info must be set to "PCI:" (or "PCIe:") followed by
  224. * the value of pci_name(). For USB devices,the usb_make_path() function
  225. * must be used. This field is used by applications to distinguish between
  226. * otherwise identical devices that don't provide a serial number.
  227. *
  228. * - &media_entity.hw_revision is the hardware device revision in a
  229. * driver-specific format. When possible the revision should be formatted
  230. * with the KERNEL_VERSION() macro.
  231. *
  232. * - &media_entity.driver_version is formatted with the KERNEL_VERSION()
  233. * macro. The version minor must be incremented when new features are added
  234. * to the userspace API without breaking binary compatibility. The version
  235. * major must be incremented when binary compatibility is broken.
  236. *
  237. * .. note::
  238. *
  239. * #) Upon successful registration a character device named media[0-9]+ is created. The device major and minor numbers are dynamic. The model name is exported as a sysfs attribute.
  240. *
  241. * #) Unregistering a media device that hasn't been registered is **NOT** safe.
  242. *
  243. * Return: returns zero on success or a negative error code.
  244. */
  245. int __must_check __media_device_register(struct media_device *mdev,
  246. struct module *owner);
  247. /**
  248. * media_device_register() - Registers a media device element
  249. *
  250. * @mdev: pointer to struct &media_device
  251. *
  252. * This macro calls __media_device_register() passing %THIS_MODULE as
  253. * the __media_device_register() second argument (**owner**).
  254. */
  255. #define media_device_register(mdev) __media_device_register(mdev, THIS_MODULE)
  256. /**
  257. * media_device_unregister() - Unregisters a media device element
  258. *
  259. * @mdev: pointer to struct &media_device
  260. *
  261. * It is safe to call this function on an unregistered (but initialised)
  262. * media device.
  263. */
  264. void media_device_unregister(struct media_device *mdev);
  265. /**
  266. * media_device_register_entity() - registers a media entity inside a
  267. * previously registered media device.
  268. *
  269. * @mdev: pointer to struct &media_device
  270. * @entity: pointer to struct &media_entity to be registered
  271. *
  272. * Entities are identified by a unique positive integer ID. The media
  273. * controller framework will such ID automatically. IDs are not guaranteed
  274. * to be contiguous, and the ID number can change on newer Kernel versions.
  275. * So, neither the driver nor userspace should hardcode ID numbers to refer
  276. * to the entities, but, instead, use the framework to find the ID, when
  277. * needed.
  278. *
  279. * The media_entity name, type and flags fields should be initialized before
  280. * calling media_device_register_entity(). Entities embedded in higher-level
  281. * standard structures can have some of those fields set by the higher-level
  282. * framework.
  283. *
  284. * If the device has pads, media_entity_pads_init() should be called before
  285. * this function. Otherwise, the &media_entity.pad and &media_entity.num_pads
  286. * should be zeroed before calling this function.
  287. *
  288. * Entities have flags that describe the entity capabilities and state:
  289. *
  290. * %MEDIA_ENT_FL_DEFAULT
  291. * indicates the default entity for a given type.
  292. * This can be used to report the default audio and video devices or the
  293. * default camera sensor.
  294. *
  295. * .. note::
  296. *
  297. * Drivers should set the entity function before calling this function.
  298. * Please notice that the values %MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN and
  299. * %MEDIA_ENT_F_UNKNOWN should not be used by the drivers.
  300. */
  301. int __must_check media_device_register_entity(struct media_device *mdev,
  302. struct media_entity *entity);
  303. /**
  304. * media_device_unregister_entity() - unregisters a media entity.
  305. *
  306. * @entity: pointer to struct &media_entity to be unregistered
  307. *
  308. * All links associated with the entity and all PADs are automatically
  309. * unregistered from the media_device when this function is called.
  310. *
  311. * Unregistering an entity will not change the IDs of the other entities and
  312. * the previoully used ID will never be reused for a newly registered entities.
  313. *
  314. * When a media device is unregistered, all its entities are unregistered
  315. * automatically. No manual entities unregistration is then required.
  316. *
  317. * .. note::
  318. *
  319. * The media_entity instance itself must be freed explicitly by
  320. * the driver if required.
  321. */
  322. void media_device_unregister_entity(struct media_entity *entity);
  323. /**
  324. * media_device_register_entity_notify() - Registers a media entity_notify
  325. * callback
  326. *
  327. * @mdev: The media device
  328. * @nptr: The media_entity_notify
  329. *
  330. * .. note::
  331. *
  332. * When a new entity is registered, all the registered
  333. * media_entity_notify callbacks are invoked.
  334. */
  335. int __must_check media_device_register_entity_notify(struct media_device *mdev,
  336. struct media_entity_notify *nptr);
  337. /**
  338. * media_device_unregister_entity_notify() - Unregister a media entity notify
  339. * callback
  340. *
  341. * @mdev: The media device
  342. * @nptr: The media_entity_notify
  343. *
  344. */
  345. void media_device_unregister_entity_notify(struct media_device *mdev,
  346. struct media_entity_notify *nptr);
  347. /* Iterate over all entities. */
  348. #define media_device_for_each_entity(entity, mdev) \
  349. list_for_each_entry(entity, &(mdev)->entities, graph_obj.list)
  350. /* Iterate over all interfaces. */
  351. #define media_device_for_each_intf(intf, mdev) \
  352. list_for_each_entry(intf, &(mdev)->interfaces, graph_obj.list)
  353. /* Iterate over all pads. */
  354. #define media_device_for_each_pad(pad, mdev) \
  355. list_for_each_entry(pad, &(mdev)->pads, graph_obj.list)
  356. /* Iterate over all links. */
  357. #define media_device_for_each_link(link, mdev) \
  358. list_for_each_entry(link, &(mdev)->links, graph_obj.list)
  359. /**
  360. * media_device_pci_init() - create and initialize a
  361. * struct &media_device from a PCI device.
  362. *
  363. * @mdev: pointer to struct &media_device
  364. * @pci_dev: pointer to struct pci_dev
  365. * @name: media device name. If %NULL, the routine will use the default
  366. * name for the pci device, given by pci_name() macro.
  367. */
  368. void media_device_pci_init(struct media_device *mdev,
  369. struct pci_dev *pci_dev,
  370. const char *name);
  371. /**
  372. * __media_device_usb_init() - create and initialize a
  373. * struct &media_device from a PCI device.
  374. *
  375. * @mdev: pointer to struct &media_device
  376. * @udev: pointer to struct usb_device
  377. * @board_name: media device name. If %NULL, the routine will use the usb
  378. * product name, if available.
  379. * @driver_name: name of the driver. if %NULL, the routine will use the name
  380. * given by ``udev->dev->driver->name``, with is usually the wrong
  381. * thing to do.
  382. *
  383. * .. note::
  384. *
  385. * It is better to call media_device_usb_init() instead, as
  386. * such macro fills driver_name with %KBUILD_MODNAME.
  387. */
  388. void __media_device_usb_init(struct media_device *mdev,
  389. struct usb_device *udev,
  390. const char *board_name,
  391. const char *driver_name);
  392. #else
  393. static inline int media_device_register(struct media_device *mdev)
  394. {
  395. return 0;
  396. }
  397. static inline void media_device_unregister(struct media_device *mdev)
  398. {
  399. }
  400. static inline int media_device_register_entity(struct media_device *mdev,
  401. struct media_entity *entity)
  402. {
  403. return 0;
  404. }
  405. static inline void media_device_unregister_entity(struct media_entity *entity)
  406. {
  407. }
  408. static inline int media_device_register_entity_notify(
  409. struct media_device *mdev,
  410. struct media_entity_notify *nptr)
  411. {
  412. return 0;
  413. }
  414. static inline void media_device_unregister_entity_notify(
  415. struct media_device *mdev,
  416. struct media_entity_notify *nptr)
  417. {
  418. }
  419. static inline void media_device_pci_init(struct media_device *mdev,
  420. struct pci_dev *pci_dev,
  421. char *name)
  422. {
  423. }
  424. static inline void __media_device_usb_init(struct media_device *mdev,
  425. struct usb_device *udev,
  426. char *board_name,
  427. char *driver_name)
  428. {
  429. }
  430. #endif /* CONFIG_MEDIA_CONTROLLER */
  431. /**
  432. * media_device_usb_init() - create and initialize a
  433. * struct &media_device from a PCI device.
  434. *
  435. * @mdev: pointer to struct &media_device
  436. * @udev: pointer to struct usb_device
  437. * @name: media device name. If %NULL, the routine will use the usb
  438. * product name, if available.
  439. *
  440. * This macro calls media_device_usb_init() passing the
  441. * media_device_usb_init() **driver_name** parameter filled with
  442. * %KBUILD_MODNAME.
  443. */
  444. #define media_device_usb_init(mdev, udev, name) \
  445. __media_device_usb_init(mdev, udev, name, KBUILD_MODNAME)
  446. #endif