media-device.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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. /* We need to access legacy defines from linux/media.h */
  23. #define __NEED_MEDIA_LEGACY_API
  24. #include <linux/compat.h>
  25. #include <linux/export.h>
  26. #include <linux/idr.h>
  27. #include <linux/ioctl.h>
  28. #include <linux/media.h>
  29. #include <linux/slab.h>
  30. #include <linux/types.h>
  31. #include <linux/pci.h>
  32. #include <linux/usb.h>
  33. #include <media/media-device.h>
  34. #include <media/media-devnode.h>
  35. #include <media/media-entity.h>
  36. #ifdef CONFIG_MEDIA_CONTROLLER
  37. /* -----------------------------------------------------------------------------
  38. * Userspace API
  39. */
  40. static inline void __user *media_get_uptr(__u64 arg)
  41. {
  42. return (void __user *)(uintptr_t)arg;
  43. }
  44. static int media_device_open(struct file *filp)
  45. {
  46. return 0;
  47. }
  48. static int media_device_close(struct file *filp)
  49. {
  50. return 0;
  51. }
  52. static int media_device_get_info(struct media_device *dev,
  53. struct media_device_info __user *__info)
  54. {
  55. struct media_device_info info;
  56. memset(&info, 0, sizeof(info));
  57. if (dev->driver_name[0])
  58. strlcpy(info.driver, dev->driver_name, sizeof(info.driver));
  59. else
  60. strlcpy(info.driver, dev->dev->driver->name, sizeof(info.driver));
  61. strlcpy(info.model, dev->model, sizeof(info.model));
  62. strlcpy(info.serial, dev->serial, sizeof(info.serial));
  63. strlcpy(info.bus_info, dev->bus_info, sizeof(info.bus_info));
  64. info.media_version = MEDIA_API_VERSION;
  65. info.hw_revision = dev->hw_revision;
  66. info.driver_version = dev->driver_version;
  67. if (copy_to_user(__info, &info, sizeof(*__info)))
  68. return -EFAULT;
  69. return 0;
  70. }
  71. static struct media_entity *find_entity(struct media_device *mdev, u32 id)
  72. {
  73. struct media_entity *entity;
  74. int next = id & MEDIA_ENT_ID_FLAG_NEXT;
  75. id &= ~MEDIA_ENT_ID_FLAG_NEXT;
  76. media_device_for_each_entity(entity, mdev) {
  77. if (((media_entity_id(entity) == id) && !next) ||
  78. ((media_entity_id(entity) > id) && next)) {
  79. return entity;
  80. }
  81. }
  82. return NULL;
  83. }
  84. static long media_device_enum_entities(struct media_device *mdev,
  85. struct media_entity_desc __user *uent)
  86. {
  87. struct media_entity *ent;
  88. struct media_entity_desc u_ent;
  89. memset(&u_ent, 0, sizeof(u_ent));
  90. if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id)))
  91. return -EFAULT;
  92. ent = find_entity(mdev, u_ent.id);
  93. if (ent == NULL)
  94. return -EINVAL;
  95. u_ent.id = media_entity_id(ent);
  96. if (ent->name)
  97. strlcpy(u_ent.name, ent->name, sizeof(u_ent.name));
  98. u_ent.type = ent->function;
  99. u_ent.revision = 0; /* Unused */
  100. u_ent.flags = ent->flags;
  101. u_ent.group_id = 0; /* Unused */
  102. u_ent.pads = ent->num_pads;
  103. u_ent.links = ent->num_links - ent->num_backlinks;
  104. /*
  105. * Workaround for a bug at media-ctl <= v1.10 that makes it to
  106. * do the wrong thing if the entity function doesn't belong to
  107. * either MEDIA_ENT_F_OLD_BASE or MEDIA_ENT_F_OLD_SUBDEV_BASE
  108. * Ranges.
  109. *
  110. * Non-subdevices are expected to be at the MEDIA_ENT_F_OLD_BASE,
  111. * or, otherwise, will be silently ignored by media-ctl when
  112. * printing the graphviz diagram. So, map them into the devnode
  113. * old range.
  114. */
  115. if (ent->function < MEDIA_ENT_F_OLD_BASE ||
  116. ent->function > MEDIA_ENT_T_DEVNODE_UNKNOWN) {
  117. if (is_media_entity_v4l2_subdev(ent))
  118. u_ent.type = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
  119. else if (ent->function != MEDIA_ENT_F_IO_V4L)
  120. u_ent.type = MEDIA_ENT_T_DEVNODE_UNKNOWN;
  121. }
  122. memcpy(&u_ent.raw, &ent->info, sizeof(ent->info));
  123. if (copy_to_user(uent, &u_ent, sizeof(u_ent)))
  124. return -EFAULT;
  125. return 0;
  126. }
  127. static void media_device_kpad_to_upad(const struct media_pad *kpad,
  128. struct media_pad_desc *upad)
  129. {
  130. upad->entity = media_entity_id(kpad->entity);
  131. upad->index = kpad->index;
  132. upad->flags = kpad->flags;
  133. }
  134. static long __media_device_enum_links(struct media_device *mdev,
  135. struct media_links_enum *links)
  136. {
  137. struct media_entity *entity;
  138. entity = find_entity(mdev, links->entity);
  139. if (entity == NULL)
  140. return -EINVAL;
  141. if (links->pads) {
  142. unsigned int p;
  143. for (p = 0; p < entity->num_pads; p++) {
  144. struct media_pad_desc pad;
  145. memset(&pad, 0, sizeof(pad));
  146. media_device_kpad_to_upad(&entity->pads[p], &pad);
  147. if (copy_to_user(&links->pads[p], &pad, sizeof(pad)))
  148. return -EFAULT;
  149. }
  150. }
  151. if (links->links) {
  152. struct media_link *link;
  153. struct media_link_desc __user *ulink_desc = links->links;
  154. list_for_each_entry(link, &entity->links, list) {
  155. struct media_link_desc klink_desc;
  156. /* Ignore backlinks. */
  157. if (link->source->entity != entity)
  158. continue;
  159. memset(&klink_desc, 0, sizeof(klink_desc));
  160. media_device_kpad_to_upad(link->source,
  161. &klink_desc.source);
  162. media_device_kpad_to_upad(link->sink,
  163. &klink_desc.sink);
  164. klink_desc.flags = link->flags;
  165. if (copy_to_user(ulink_desc, &klink_desc,
  166. sizeof(*ulink_desc)))
  167. return -EFAULT;
  168. ulink_desc++;
  169. }
  170. }
  171. return 0;
  172. }
  173. static long media_device_enum_links(struct media_device *mdev,
  174. struct media_links_enum __user *ulinks)
  175. {
  176. struct media_links_enum links;
  177. int rval;
  178. if (copy_from_user(&links, ulinks, sizeof(links)))
  179. return -EFAULT;
  180. rval = __media_device_enum_links(mdev, &links);
  181. if (rval < 0)
  182. return rval;
  183. if (copy_to_user(ulinks, &links, sizeof(*ulinks)))
  184. return -EFAULT;
  185. return 0;
  186. }
  187. static long media_device_setup_link(struct media_device *mdev,
  188. struct media_link_desc __user *_ulink)
  189. {
  190. struct media_link *link = NULL;
  191. struct media_link_desc ulink;
  192. struct media_entity *source;
  193. struct media_entity *sink;
  194. int ret;
  195. if (copy_from_user(&ulink, _ulink, sizeof(ulink)))
  196. return -EFAULT;
  197. /* Find the source and sink entities and link.
  198. */
  199. source = find_entity(mdev, ulink.source.entity);
  200. sink = find_entity(mdev, ulink.sink.entity);
  201. if (source == NULL || sink == NULL)
  202. return -EINVAL;
  203. if (ulink.source.index >= source->num_pads ||
  204. ulink.sink.index >= sink->num_pads)
  205. return -EINVAL;
  206. link = media_entity_find_link(&source->pads[ulink.source.index],
  207. &sink->pads[ulink.sink.index]);
  208. if (link == NULL)
  209. return -EINVAL;
  210. /* Setup the link on both entities. */
  211. ret = __media_entity_setup_link(link, ulink.flags);
  212. if (copy_to_user(_ulink, &ulink, sizeof(ulink)))
  213. return -EFAULT;
  214. return ret;
  215. }
  216. static long __media_device_get_topology(struct media_device *mdev,
  217. struct media_v2_topology *topo)
  218. {
  219. struct media_entity *entity;
  220. struct media_interface *intf;
  221. struct media_pad *pad;
  222. struct media_link *link;
  223. struct media_v2_entity kentity, __user *uentity;
  224. struct media_v2_interface kintf, __user *uintf;
  225. struct media_v2_pad kpad, __user *upad;
  226. struct media_v2_link klink, __user *ulink;
  227. unsigned int i;
  228. int ret = 0;
  229. topo->topology_version = mdev->topology_version;
  230. /* Get entities and number of entities */
  231. i = 0;
  232. uentity = media_get_uptr(topo->ptr_entities);
  233. media_device_for_each_entity(entity, mdev) {
  234. i++;
  235. if (ret || !uentity)
  236. continue;
  237. if (i > topo->num_entities) {
  238. ret = -ENOSPC;
  239. continue;
  240. }
  241. /* Copy fields to userspace struct if not error */
  242. memset(&kentity, 0, sizeof(kentity));
  243. kentity.id = entity->graph_obj.id;
  244. kentity.function = entity->function;
  245. strncpy(kentity.name, entity->name,
  246. sizeof(kentity.name));
  247. if (copy_to_user(uentity, &kentity, sizeof(kentity)))
  248. ret = -EFAULT;
  249. uentity++;
  250. }
  251. topo->num_entities = i;
  252. /* Get interfaces and number of interfaces */
  253. i = 0;
  254. uintf = media_get_uptr(topo->ptr_interfaces);
  255. media_device_for_each_intf(intf, mdev) {
  256. i++;
  257. if (ret || !uintf)
  258. continue;
  259. if (i > topo->num_interfaces) {
  260. ret = -ENOSPC;
  261. continue;
  262. }
  263. memset(&kintf, 0, sizeof(kintf));
  264. /* Copy intf fields to userspace struct */
  265. kintf.id = intf->graph_obj.id;
  266. kintf.intf_type = intf->type;
  267. kintf.flags = intf->flags;
  268. if (media_type(&intf->graph_obj) == MEDIA_GRAPH_INTF_DEVNODE) {
  269. struct media_intf_devnode *devnode;
  270. devnode = intf_to_devnode(intf);
  271. kintf.devnode.major = devnode->major;
  272. kintf.devnode.minor = devnode->minor;
  273. }
  274. if (copy_to_user(uintf, &kintf, sizeof(kintf)))
  275. ret = -EFAULT;
  276. uintf++;
  277. }
  278. topo->num_interfaces = i;
  279. /* Get pads and number of pads */
  280. i = 0;
  281. upad = media_get_uptr(topo->ptr_pads);
  282. media_device_for_each_pad(pad, mdev) {
  283. i++;
  284. if (ret || !upad)
  285. continue;
  286. if (i > topo->num_pads) {
  287. ret = -ENOSPC;
  288. continue;
  289. }
  290. memset(&kpad, 0, sizeof(kpad));
  291. /* Copy pad fields to userspace struct */
  292. kpad.id = pad->graph_obj.id;
  293. kpad.entity_id = pad->entity->graph_obj.id;
  294. kpad.flags = pad->flags;
  295. if (copy_to_user(upad, &kpad, sizeof(kpad)))
  296. ret = -EFAULT;
  297. upad++;
  298. }
  299. topo->num_pads = i;
  300. /* Get links and number of links */
  301. i = 0;
  302. ulink = media_get_uptr(topo->ptr_links);
  303. media_device_for_each_link(link, mdev) {
  304. if (link->is_backlink)
  305. continue;
  306. i++;
  307. if (ret || !ulink)
  308. continue;
  309. if (i > topo->num_links) {
  310. ret = -ENOSPC;
  311. continue;
  312. }
  313. memset(&klink, 0, sizeof(klink));
  314. /* Copy link fields to userspace struct */
  315. klink.id = link->graph_obj.id;
  316. klink.source_id = link->gobj0->id;
  317. klink.sink_id = link->gobj1->id;
  318. klink.flags = link->flags;
  319. if (copy_to_user(ulink, &klink, sizeof(klink)))
  320. ret = -EFAULT;
  321. ulink++;
  322. }
  323. topo->num_links = i;
  324. return ret;
  325. }
  326. static long media_device_get_topology(struct media_device *mdev,
  327. struct media_v2_topology __user *utopo)
  328. {
  329. struct media_v2_topology ktopo;
  330. int ret;
  331. if (copy_from_user(&ktopo, utopo, sizeof(ktopo)))
  332. return -EFAULT;
  333. ret = __media_device_get_topology(mdev, &ktopo);
  334. if (ret < 0)
  335. return ret;
  336. if (copy_to_user(utopo, &ktopo, sizeof(*utopo)))
  337. return -EFAULT;
  338. return 0;
  339. }
  340. static long media_device_ioctl(struct file *filp, unsigned int cmd,
  341. unsigned long arg)
  342. {
  343. struct media_devnode *devnode = media_devnode_data(filp);
  344. struct media_device *dev = devnode->media_dev;
  345. long ret;
  346. mutex_lock(&dev->graph_mutex);
  347. switch (cmd) {
  348. case MEDIA_IOC_DEVICE_INFO:
  349. ret = media_device_get_info(dev,
  350. (struct media_device_info __user *)arg);
  351. break;
  352. case MEDIA_IOC_ENUM_ENTITIES:
  353. ret = media_device_enum_entities(dev,
  354. (struct media_entity_desc __user *)arg);
  355. break;
  356. case MEDIA_IOC_ENUM_LINKS:
  357. ret = media_device_enum_links(dev,
  358. (struct media_links_enum __user *)arg);
  359. break;
  360. case MEDIA_IOC_SETUP_LINK:
  361. ret = media_device_setup_link(dev,
  362. (struct media_link_desc __user *)arg);
  363. break;
  364. case MEDIA_IOC_G_TOPOLOGY:
  365. ret = media_device_get_topology(dev,
  366. (struct media_v2_topology __user *)arg);
  367. break;
  368. default:
  369. ret = -ENOIOCTLCMD;
  370. }
  371. mutex_unlock(&dev->graph_mutex);
  372. return ret;
  373. }
  374. #ifdef CONFIG_COMPAT
  375. struct media_links_enum32 {
  376. __u32 entity;
  377. compat_uptr_t pads; /* struct media_pad_desc * */
  378. compat_uptr_t links; /* struct media_link_desc * */
  379. __u32 reserved[4];
  380. };
  381. static long media_device_enum_links32(struct media_device *mdev,
  382. struct media_links_enum32 __user *ulinks)
  383. {
  384. struct media_links_enum links;
  385. compat_uptr_t pads_ptr, links_ptr;
  386. memset(&links, 0, sizeof(links));
  387. if (get_user(links.entity, &ulinks->entity)
  388. || get_user(pads_ptr, &ulinks->pads)
  389. || get_user(links_ptr, &ulinks->links))
  390. return -EFAULT;
  391. links.pads = compat_ptr(pads_ptr);
  392. links.links = compat_ptr(links_ptr);
  393. return __media_device_enum_links(mdev, &links);
  394. }
  395. #define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32)
  396. static long media_device_compat_ioctl(struct file *filp, unsigned int cmd,
  397. unsigned long arg)
  398. {
  399. struct media_devnode *devnode = media_devnode_data(filp);
  400. struct media_device *dev = devnode->media_dev;
  401. long ret;
  402. switch (cmd) {
  403. case MEDIA_IOC_ENUM_LINKS32:
  404. mutex_lock(&dev->graph_mutex);
  405. ret = media_device_enum_links32(dev,
  406. (struct media_links_enum32 __user *)arg);
  407. mutex_unlock(&dev->graph_mutex);
  408. break;
  409. default:
  410. return media_device_ioctl(filp, cmd, arg);
  411. }
  412. return ret;
  413. }
  414. #endif /* CONFIG_COMPAT */
  415. static const struct media_file_operations media_device_fops = {
  416. .owner = THIS_MODULE,
  417. .open = media_device_open,
  418. .ioctl = media_device_ioctl,
  419. #ifdef CONFIG_COMPAT
  420. .compat_ioctl = media_device_compat_ioctl,
  421. #endif /* CONFIG_COMPAT */
  422. .release = media_device_close,
  423. };
  424. /* -----------------------------------------------------------------------------
  425. * sysfs
  426. */
  427. static ssize_t show_model(struct device *cd,
  428. struct device_attribute *attr, char *buf)
  429. {
  430. struct media_devnode *devnode = to_media_devnode(cd);
  431. struct media_device *mdev = devnode->media_dev;
  432. return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
  433. }
  434. static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
  435. /* -----------------------------------------------------------------------------
  436. * Registration/unregistration
  437. */
  438. static void media_device_release(struct media_devnode *mdev)
  439. {
  440. dev_dbg(mdev->parent, "Media device released\n");
  441. }
  442. /**
  443. * media_device_register_entity - Register an entity with a media device
  444. * @mdev: The media device
  445. * @entity: The entity
  446. */
  447. int __must_check media_device_register_entity(struct media_device *mdev,
  448. struct media_entity *entity)
  449. {
  450. struct media_entity_notify *notify, *next;
  451. unsigned int i;
  452. int ret;
  453. if (entity->function == MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN ||
  454. entity->function == MEDIA_ENT_F_UNKNOWN)
  455. dev_warn(mdev->dev,
  456. "Entity type for entity %s was not initialized!\n",
  457. entity->name);
  458. /* Warn if we apparently re-register an entity */
  459. WARN_ON(entity->graph_obj.mdev != NULL);
  460. entity->graph_obj.mdev = mdev;
  461. INIT_LIST_HEAD(&entity->links);
  462. entity->num_links = 0;
  463. entity->num_backlinks = 0;
  464. if (!ida_pre_get(&mdev->entity_internal_idx, GFP_KERNEL))
  465. return -ENOMEM;
  466. mutex_lock(&mdev->graph_mutex);
  467. ret = ida_get_new_above(&mdev->entity_internal_idx, 1,
  468. &entity->internal_idx);
  469. if (ret < 0) {
  470. mutex_unlock(&mdev->graph_mutex);
  471. return ret;
  472. }
  473. mdev->entity_internal_idx_max =
  474. max(mdev->entity_internal_idx_max, entity->internal_idx);
  475. /* Initialize media_gobj embedded at the entity */
  476. media_gobj_create(mdev, MEDIA_GRAPH_ENTITY, &entity->graph_obj);
  477. /* Initialize objects at the pads */
  478. for (i = 0; i < entity->num_pads; i++)
  479. media_gobj_create(mdev, MEDIA_GRAPH_PAD,
  480. &entity->pads[i].graph_obj);
  481. /* invoke entity_notify callbacks */
  482. list_for_each_entry_safe(notify, next, &mdev->entity_notify, list) {
  483. (notify)->notify(entity, notify->notify_data);
  484. }
  485. if (mdev->entity_internal_idx_max
  486. >= mdev->pm_count_walk.ent_enum.idx_max) {
  487. struct media_entity_graph new = { .top = 0 };
  488. /*
  489. * Initialise the new graph walk before cleaning up
  490. * the old one in order not to spoil the graph walk
  491. * object of the media device if graph walk init fails.
  492. */
  493. ret = media_entity_graph_walk_init(&new, mdev);
  494. if (ret) {
  495. mutex_unlock(&mdev->graph_mutex);
  496. return ret;
  497. }
  498. media_entity_graph_walk_cleanup(&mdev->pm_count_walk);
  499. mdev->pm_count_walk = new;
  500. }
  501. mutex_unlock(&mdev->graph_mutex);
  502. return 0;
  503. }
  504. EXPORT_SYMBOL_GPL(media_device_register_entity);
  505. static void __media_device_unregister_entity(struct media_entity *entity)
  506. {
  507. struct media_device *mdev = entity->graph_obj.mdev;
  508. struct media_link *link, *tmp;
  509. struct media_interface *intf;
  510. unsigned int i;
  511. ida_simple_remove(&mdev->entity_internal_idx, entity->internal_idx);
  512. /* Remove all interface links pointing to this entity */
  513. list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) {
  514. list_for_each_entry_safe(link, tmp, &intf->links, list) {
  515. if (link->entity == entity)
  516. __media_remove_intf_link(link);
  517. }
  518. }
  519. /* Remove all data links that belong to this entity */
  520. __media_entity_remove_links(entity);
  521. /* Remove all pads that belong to this entity */
  522. for (i = 0; i < entity->num_pads; i++)
  523. media_gobj_destroy(&entity->pads[i].graph_obj);
  524. /* Remove the entity */
  525. media_gobj_destroy(&entity->graph_obj);
  526. /* invoke entity_notify callbacks to handle entity removal?? */
  527. entity->graph_obj.mdev = NULL;
  528. }
  529. void media_device_unregister_entity(struct media_entity *entity)
  530. {
  531. struct media_device *mdev = entity->graph_obj.mdev;
  532. if (mdev == NULL)
  533. return;
  534. mutex_lock(&mdev->graph_mutex);
  535. __media_device_unregister_entity(entity);
  536. mutex_unlock(&mdev->graph_mutex);
  537. }
  538. EXPORT_SYMBOL_GPL(media_device_unregister_entity);
  539. /**
  540. * media_device_init() - initialize a media device
  541. * @mdev: The media device
  542. *
  543. * The caller is responsible for initializing the media device before
  544. * registration. The following fields must be set:
  545. *
  546. * - dev must point to the parent device
  547. * - model must be filled with the device model name
  548. */
  549. void media_device_init(struct media_device *mdev)
  550. {
  551. INIT_LIST_HEAD(&mdev->entities);
  552. INIT_LIST_HEAD(&mdev->interfaces);
  553. INIT_LIST_HEAD(&mdev->pads);
  554. INIT_LIST_HEAD(&mdev->links);
  555. INIT_LIST_HEAD(&mdev->entity_notify);
  556. mutex_init(&mdev->graph_mutex);
  557. ida_init(&mdev->entity_internal_idx);
  558. dev_dbg(mdev->dev, "Media device initialized\n");
  559. }
  560. EXPORT_SYMBOL_GPL(media_device_init);
  561. void media_device_cleanup(struct media_device *mdev)
  562. {
  563. ida_destroy(&mdev->entity_internal_idx);
  564. mdev->entity_internal_idx_max = 0;
  565. media_entity_graph_walk_cleanup(&mdev->pm_count_walk);
  566. mutex_destroy(&mdev->graph_mutex);
  567. }
  568. EXPORT_SYMBOL_GPL(media_device_cleanup);
  569. int __must_check __media_device_register(struct media_device *mdev,
  570. struct module *owner)
  571. {
  572. struct media_devnode *devnode;
  573. int ret;
  574. devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
  575. if (!devnode)
  576. return -ENOMEM;
  577. /* Register the device node. */
  578. mdev->devnode = devnode;
  579. devnode->fops = &media_device_fops;
  580. devnode->parent = mdev->dev;
  581. devnode->release = media_device_release;
  582. /* Set version 0 to indicate user-space that the graph is static */
  583. mdev->topology_version = 0;
  584. ret = media_devnode_register(mdev, devnode, owner);
  585. if (ret < 0) {
  586. /* devnode free is handled in media_devnode_*() */
  587. mdev->devnode = NULL;
  588. return ret;
  589. }
  590. ret = device_create_file(&devnode->dev, &dev_attr_model);
  591. if (ret < 0) {
  592. /* devnode free is handled in media_devnode_*() */
  593. mdev->devnode = NULL;
  594. media_devnode_unregister_prepare(devnode);
  595. media_devnode_unregister(devnode);
  596. return ret;
  597. }
  598. dev_dbg(mdev->dev, "Media device registered\n");
  599. return 0;
  600. }
  601. EXPORT_SYMBOL_GPL(__media_device_register);
  602. int __must_check media_device_register_entity_notify(struct media_device *mdev,
  603. struct media_entity_notify *nptr)
  604. {
  605. mutex_lock(&mdev->graph_mutex);
  606. list_add_tail(&nptr->list, &mdev->entity_notify);
  607. mutex_unlock(&mdev->graph_mutex);
  608. return 0;
  609. }
  610. EXPORT_SYMBOL_GPL(media_device_register_entity_notify);
  611. /*
  612. * Note: Should be called with mdev->lock held.
  613. */
  614. static void __media_device_unregister_entity_notify(struct media_device *mdev,
  615. struct media_entity_notify *nptr)
  616. {
  617. list_del(&nptr->list);
  618. }
  619. void media_device_unregister_entity_notify(struct media_device *mdev,
  620. struct media_entity_notify *nptr)
  621. {
  622. mutex_lock(&mdev->graph_mutex);
  623. __media_device_unregister_entity_notify(mdev, nptr);
  624. mutex_unlock(&mdev->graph_mutex);
  625. }
  626. EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify);
  627. void media_device_unregister(struct media_device *mdev)
  628. {
  629. struct media_entity *entity;
  630. struct media_entity *next;
  631. struct media_interface *intf, *tmp_intf;
  632. struct media_entity_notify *notify, *nextp;
  633. if (mdev == NULL)
  634. return;
  635. mutex_lock(&mdev->graph_mutex);
  636. /* Check if mdev was ever registered at all */
  637. if (!media_devnode_is_registered(mdev->devnode)) {
  638. mutex_unlock(&mdev->graph_mutex);
  639. return;
  640. }
  641. /* Clear the devnode register bit to avoid races with media dev open */
  642. media_devnode_unregister_prepare(mdev->devnode);
  643. /* Remove all entities from the media device */
  644. list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list)
  645. __media_device_unregister_entity(entity);
  646. /* Remove all entity_notify callbacks from the media device */
  647. list_for_each_entry_safe(notify, nextp, &mdev->entity_notify, list)
  648. __media_device_unregister_entity_notify(mdev, notify);
  649. /* Remove all interfaces from the media device */
  650. list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces,
  651. graph_obj.list) {
  652. __media_remove_intf_links(intf);
  653. media_gobj_destroy(&intf->graph_obj);
  654. kfree(intf);
  655. }
  656. mutex_unlock(&mdev->graph_mutex);
  657. dev_dbg(mdev->dev, "Media device unregistered\n");
  658. device_remove_file(&mdev->devnode->dev, &dev_attr_model);
  659. media_devnode_unregister(mdev->devnode);
  660. /* devnode free is handled in media_devnode_*() */
  661. mdev->devnode = NULL;
  662. }
  663. EXPORT_SYMBOL_GPL(media_device_unregister);
  664. static void media_device_release_devres(struct device *dev, void *res)
  665. {
  666. }
  667. struct media_device *media_device_get_devres(struct device *dev)
  668. {
  669. struct media_device *mdev;
  670. mdev = devres_find(dev, media_device_release_devres, NULL, NULL);
  671. if (mdev)
  672. return mdev;
  673. mdev = devres_alloc(media_device_release_devres,
  674. sizeof(struct media_device), GFP_KERNEL);
  675. if (!mdev)
  676. return NULL;
  677. return devres_get(dev, mdev, NULL, NULL);
  678. }
  679. EXPORT_SYMBOL_GPL(media_device_get_devres);
  680. struct media_device *media_device_find_devres(struct device *dev)
  681. {
  682. return devres_find(dev, media_device_release_devres, NULL, NULL);
  683. }
  684. EXPORT_SYMBOL_GPL(media_device_find_devres);
  685. #if IS_ENABLED(CONFIG_PCI)
  686. void media_device_pci_init(struct media_device *mdev,
  687. struct pci_dev *pci_dev,
  688. const char *name)
  689. {
  690. mdev->dev = &pci_dev->dev;
  691. if (name)
  692. strlcpy(mdev->model, name, sizeof(mdev->model));
  693. else
  694. strlcpy(mdev->model, pci_name(pci_dev), sizeof(mdev->model));
  695. sprintf(mdev->bus_info, "PCI:%s", pci_name(pci_dev));
  696. mdev->hw_revision = (pci_dev->subsystem_vendor << 16)
  697. | pci_dev->subsystem_device;
  698. mdev->driver_version = LINUX_VERSION_CODE;
  699. media_device_init(mdev);
  700. }
  701. EXPORT_SYMBOL_GPL(media_device_pci_init);
  702. #endif
  703. #if IS_ENABLED(CONFIG_USB)
  704. void __media_device_usb_init(struct media_device *mdev,
  705. struct usb_device *udev,
  706. const char *board_name,
  707. const char *driver_name)
  708. {
  709. mdev->dev = &udev->dev;
  710. if (driver_name)
  711. strlcpy(mdev->driver_name, driver_name,
  712. sizeof(mdev->driver_name));
  713. if (board_name)
  714. strlcpy(mdev->model, board_name, sizeof(mdev->model));
  715. else if (udev->product)
  716. strlcpy(mdev->model, udev->product, sizeof(mdev->model));
  717. else
  718. strlcpy(mdev->model, "unknown model", sizeof(mdev->model));
  719. if (udev->serial)
  720. strlcpy(mdev->serial, udev->serial, sizeof(mdev->serial));
  721. usb_make_path(udev, mdev->bus_info, sizeof(mdev->bus_info));
  722. mdev->hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
  723. mdev->driver_version = LINUX_VERSION_CODE;
  724. media_device_init(mdev);
  725. }
  726. EXPORT_SYMBOL_GPL(__media_device_usb_init);
  727. #endif
  728. #endif /* CONFIG_MEDIA_CONTROLLER */