media-device.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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
  38. * new entities get registered with the media device.
  39. */
  40. struct media_entity_notify {
  41. struct list_head list;
  42. void *notify_data;
  43. void (*notify)(struct media_entity *entity, void *notify_data);
  44. };
  45. /**
  46. * struct media_device_ops - Media device operations
  47. * @link_notify: Link state change notification callback. This callback is
  48. * called with the graph_mutex held.
  49. */
  50. struct media_device_ops {
  51. int (*link_notify)(struct media_link *link, u32 flags,
  52. unsigned int notification);
  53. };
  54. /**
  55. * struct media_device - Media device
  56. * @dev: Parent device
  57. * @devnode: Media device node
  58. * @driver_name: Optional device driver name. If not set, calls to
  59. * %MEDIA_IOC_DEVICE_INFO will return ``dev->driver->name``.
  60. * This is needed for USB drivers for example, as otherwise
  61. * they'll all appear as if the driver name was "usb".
  62. * @model: Device model name
  63. * @serial: Device serial number (optional)
  64. * @bus_info: Unique and stable device location identifier
  65. * @hw_revision: Hardware device revision
  66. * @driver_version: Device driver version
  67. * @topology_version: Monotonic counter for storing the version of the graph
  68. * topology. Should be incremented each time the topology changes.
  69. * @id: Unique ID used on the last registered graph object
  70. * @entity_internal_idx: Unique internal entity ID used by the graph traversal
  71. * algorithms
  72. * @entity_internal_idx_max: Allocated internal entity indices
  73. * @entities: List of registered entities
  74. * @interfaces: List of registered interfaces
  75. * @pads: List of registered pads
  76. * @links: List of registered links
  77. * @entity_notify: List of registered entity_notify callbacks
  78. * @graph_mutex: Protects access to struct media_device data
  79. * @pm_count_walk: Graph walk for power state walk. Access serialised using
  80. * graph_mutex.
  81. *
  82. * @source_priv: Driver Private data for enable/disable source handlers
  83. * @enable_source: Enable Source Handler function pointer
  84. * @disable_source: Disable Source Handler function pointer
  85. *
  86. * @ops: Operation handler callbacks
  87. *
  88. * This structure represents an abstract high-level media device. It allows easy
  89. * access to entities and provides basic media device-level support. The
  90. * structure can be allocated directly or embedded in a larger structure.
  91. *
  92. * The parent @dev is a physical device. It must be set before registering the
  93. * media device.
  94. *
  95. * @model is a descriptive model name exported through sysfs. It doesn't have to
  96. * be unique.
  97. *
  98. * @enable_source is a handler to find source entity for the
  99. * sink entity and activate the link between them if source
  100. * entity is free. Drivers should call this handler before
  101. * accessing the source.
  102. *
  103. * @disable_source is a handler to find source entity for the
  104. * sink entity and deactivate the link between them. Drivers
  105. * should call this handler to release the source.
  106. *
  107. * Use-case: find tuner entity connected to the decoder
  108. * entity and check if it is available, and activate the
  109. * the link between them from @enable_source and deactivate
  110. * from @disable_source.
  111. *
  112. * .. note::
  113. *
  114. * Bridge driver is expected to implement and set the
  115. * handler when &media_device is registered or when
  116. * bridge driver finds the media_device during probe.
  117. * Bridge driver sets source_priv with information
  118. * necessary to run @enable_source 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_entity_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. /**
  346. * media_device_get_devres() - get media device as device resource
  347. * creates if one doesn't exist
  348. *
  349. * @dev: pointer to struct &device.
  350. *
  351. * Sometimes, the media controller &media_device needs to be shared by more
  352. * than one driver. This function adds support for that, by dynamically
  353. * allocating the &media_device and allowing it to be obtained from the
  354. * struct &device associated with the common device where all sub-device
  355. * components belong. So, for example, on an USB device with multiple
  356. * interfaces, each interface may be handled by a separate per-interface
  357. * drivers. While each interface have its own &device, they all share a
  358. * common &device associated with the hole USB device.
  359. */
  360. struct media_device *media_device_get_devres(struct device *dev);
  361. /**
  362. * media_device_find_devres() - find media device as device resource
  363. *
  364. * @dev: pointer to struct &device.
  365. */
  366. struct media_device *media_device_find_devres(struct device *dev);
  367. /* Iterate over all entities. */
  368. #define media_device_for_each_entity(entity, mdev) \
  369. list_for_each_entry(entity, &(mdev)->entities, graph_obj.list)
  370. /* Iterate over all interfaces. */
  371. #define media_device_for_each_intf(intf, mdev) \
  372. list_for_each_entry(intf, &(mdev)->interfaces, graph_obj.list)
  373. /* Iterate over all pads. */
  374. #define media_device_for_each_pad(pad, mdev) \
  375. list_for_each_entry(pad, &(mdev)->pads, graph_obj.list)
  376. /* Iterate over all links. */
  377. #define media_device_for_each_link(link, mdev) \
  378. list_for_each_entry(link, &(mdev)->links, graph_obj.list)
  379. /**
  380. * media_device_pci_init() - create and initialize a
  381. * struct &media_device from a PCI device.
  382. *
  383. * @mdev: pointer to struct &media_device
  384. * @pci_dev: pointer to struct pci_dev
  385. * @name: media device name. If %NULL, the routine will use the default
  386. * name for the pci device, given by pci_name() macro.
  387. */
  388. void media_device_pci_init(struct media_device *mdev,
  389. struct pci_dev *pci_dev,
  390. const char *name);
  391. /**
  392. * __media_device_usb_init() - create and initialize a
  393. * struct &media_device from a PCI device.
  394. *
  395. * @mdev: pointer to struct &media_device
  396. * @udev: pointer to struct usb_device
  397. * @board_name: media device name. If %NULL, the routine will use the usb
  398. * product name, if available.
  399. * @driver_name: name of the driver. if %NULL, the routine will use the name
  400. * given by ``udev->dev->driver->name``, with is usually the wrong
  401. * thing to do.
  402. *
  403. * .. note::
  404. *
  405. * It is better to call media_device_usb_init() instead, as
  406. * such macro fills driver_name with %KBUILD_MODNAME.
  407. */
  408. void __media_device_usb_init(struct media_device *mdev,
  409. struct usb_device *udev,
  410. const char *board_name,
  411. const char *driver_name);
  412. #else
  413. static inline int media_device_register(struct media_device *mdev)
  414. {
  415. return 0;
  416. }
  417. static inline void media_device_unregister(struct media_device *mdev)
  418. {
  419. }
  420. static inline int media_device_register_entity(struct media_device *mdev,
  421. struct media_entity *entity)
  422. {
  423. return 0;
  424. }
  425. static inline void media_device_unregister_entity(struct media_entity *entity)
  426. {
  427. }
  428. static inline int media_device_register_entity_notify(
  429. struct media_device *mdev,
  430. struct media_entity_notify *nptr)
  431. {
  432. return 0;
  433. }
  434. static inline void media_device_unregister_entity_notify(
  435. struct media_device *mdev,
  436. struct media_entity_notify *nptr)
  437. {
  438. }
  439. static inline struct media_device *media_device_get_devres(struct device *dev)
  440. {
  441. return NULL;
  442. }
  443. static inline struct media_device *media_device_find_devres(struct device *dev)
  444. {
  445. return NULL;
  446. }
  447. static inline void media_device_pci_init(struct media_device *mdev,
  448. struct pci_dev *pci_dev,
  449. char *name)
  450. {
  451. }
  452. static inline void __media_device_usb_init(struct media_device *mdev,
  453. struct usb_device *udev,
  454. char *board_name,
  455. char *driver_name)
  456. {
  457. }
  458. #endif /* CONFIG_MEDIA_CONTROLLER */
  459. /**
  460. * media_device_usb_init() - create and initialize a
  461. * struct &media_device from a PCI device.
  462. *
  463. * @mdev: pointer to struct &media_device
  464. * @udev: pointer to struct usb_device
  465. * @name: media device name. If %NULL, the routine will use the usb
  466. * product name, if available.
  467. *
  468. * This macro calls media_device_usb_init() passing the
  469. * media_device_usb_init() **driver_name** parameter filled with
  470. * %KBUILD_MODNAME.
  471. */
  472. #define media_device_usb_init(mdev, udev, name) \
  473. __media_device_usb_init(mdev, udev, name, KBUILD_MODNAME)
  474. #endif