media-device.h 16 KB

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