media-device.c 23 KB

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