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