media-device.h 16 KB

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