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