media-device.c 23 KB

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