interconnect: qcom: Fix Kconfig indentation
[linux/fpc-iii.git] / drivers / char / agp / frontend.c
blob47098648502d2776aa296d742f8d756d20831958
1 /*
2 * AGPGART driver frontend
3 * Copyright (C) 2004 Silicon Graphics, Inc.
4 * Copyright (C) 2002-2003 Dave Jones
5 * Copyright (C) 1999 Jeff Hartmann
6 * Copyright (C) 1999 Precision Insight, Inc.
7 * Copyright (C) 1999 Xi Graphics, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * JEFF HARTMANN, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
25 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #include <linux/types.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/mman.h>
33 #include <linux/pci.h>
34 #include <linux/miscdevice.h>
35 #include <linux/agp_backend.h>
36 #include <linux/agpgart.h>
37 #include <linux/slab.h>
38 #include <linux/mm.h>
39 #include <linux/fs.h>
40 #include <linux/sched.h>
41 #include <linux/uaccess.h>
42 #include <asm/pgtable.h>
43 #include "agp.h"
45 struct agp_front_data agp_fe;
47 struct agp_memory *agp_find_mem_by_key(int key)
49 struct agp_memory *curr;
51 if (agp_fe.current_controller == NULL)
52 return NULL;
54 curr = agp_fe.current_controller->pool;
56 while (curr != NULL) {
57 if (curr->key == key)
58 break;
59 curr = curr->next;
62 DBG("key=%d -> mem=%p", key, curr);
63 return curr;
66 static void agp_remove_from_pool(struct agp_memory *temp)
68 struct agp_memory *prev;
69 struct agp_memory *next;
71 /* Check to see if this is even in the memory pool */
73 DBG("mem=%p", temp);
74 if (agp_find_mem_by_key(temp->key) != NULL) {
75 next = temp->next;
76 prev = temp->prev;
78 if (prev != NULL) {
79 prev->next = next;
80 if (next != NULL)
81 next->prev = prev;
83 } else {
84 /* This is the first item on the list */
85 if (next != NULL)
86 next->prev = NULL;
88 agp_fe.current_controller->pool = next;
94 * Routines for managing each client's segment list -
95 * These routines handle adding and removing segments
96 * to each auth'ed client.
99 static struct
100 agp_segment_priv *agp_find_seg_in_client(const struct agp_client *client,
101 unsigned long offset,
102 int size, pgprot_t page_prot)
104 struct agp_segment_priv *seg;
105 int i;
106 off_t pg_start;
107 size_t pg_count;
109 pg_start = offset / 4096;
110 pg_count = size / 4096;
111 seg = *(client->segments);
113 for (i = 0; i < client->num_segments; i++) {
114 if ((seg[i].pg_start == pg_start) &&
115 (seg[i].pg_count == pg_count) &&
116 (pgprot_val(seg[i].prot) == pgprot_val(page_prot))) {
117 return seg + i;
121 return NULL;
124 static void agp_remove_seg_from_client(struct agp_client *client)
126 DBG("client=%p", client);
128 if (client->segments != NULL) {
129 if (*(client->segments) != NULL) {
130 DBG("Freeing %p from client %p", *(client->segments), client);
131 kfree(*(client->segments));
133 DBG("Freeing %p from client %p", client->segments, client);
134 kfree(client->segments);
135 client->segments = NULL;
139 static void agp_add_seg_to_client(struct agp_client *client,
140 struct agp_segment_priv ** seg, int num_segments)
142 struct agp_segment_priv **prev_seg;
144 prev_seg = client->segments;
146 if (prev_seg != NULL)
147 agp_remove_seg_from_client(client);
149 DBG("Adding seg %p (%d segments) to client %p", seg, num_segments, client);
150 client->num_segments = num_segments;
151 client->segments = seg;
154 static pgprot_t agp_convert_mmap_flags(int prot)
156 unsigned long prot_bits;
158 prot_bits = calc_vm_prot_bits(prot, 0) | VM_SHARED;
159 return vm_get_page_prot(prot_bits);
162 int agp_create_segment(struct agp_client *client, struct agp_region *region)
164 struct agp_segment_priv **ret_seg;
165 struct agp_segment_priv *seg;
166 struct agp_segment *user_seg;
167 size_t i;
169 seg = kzalloc((sizeof(struct agp_segment_priv) * region->seg_count), GFP_KERNEL);
170 if (seg == NULL) {
171 kfree(region->seg_list);
172 region->seg_list = NULL;
173 return -ENOMEM;
175 user_seg = region->seg_list;
177 for (i = 0; i < region->seg_count; i++) {
178 seg[i].pg_start = user_seg[i].pg_start;
179 seg[i].pg_count = user_seg[i].pg_count;
180 seg[i].prot = agp_convert_mmap_flags(user_seg[i].prot);
182 kfree(region->seg_list);
183 region->seg_list = NULL;
185 ret_seg = kmalloc(sizeof(void *), GFP_KERNEL);
186 if (ret_seg == NULL) {
187 kfree(seg);
188 return -ENOMEM;
190 *ret_seg = seg;
191 agp_add_seg_to_client(client, ret_seg, region->seg_count);
192 return 0;
195 /* End - Routines for managing each client's segment list */
197 /* This function must only be called when current_controller != NULL */
198 static void agp_insert_into_pool(struct agp_memory * temp)
200 struct agp_memory *prev;
202 prev = agp_fe.current_controller->pool;
204 if (prev != NULL) {
205 prev->prev = temp;
206 temp->next = prev;
208 agp_fe.current_controller->pool = temp;
212 /* File private list routines */
214 struct agp_file_private *agp_find_private(pid_t pid)
216 struct agp_file_private *curr;
218 curr = agp_fe.file_priv_list;
220 while (curr != NULL) {
221 if (curr->my_pid == pid)
222 return curr;
223 curr = curr->next;
226 return NULL;
229 static void agp_insert_file_private(struct agp_file_private * priv)
231 struct agp_file_private *prev;
233 prev = agp_fe.file_priv_list;
235 if (prev != NULL)
236 prev->prev = priv;
237 priv->next = prev;
238 agp_fe.file_priv_list = priv;
241 static void agp_remove_file_private(struct agp_file_private * priv)
243 struct agp_file_private *next;
244 struct agp_file_private *prev;
246 next = priv->next;
247 prev = priv->prev;
249 if (prev != NULL) {
250 prev->next = next;
252 if (next != NULL)
253 next->prev = prev;
255 } else {
256 if (next != NULL)
257 next->prev = NULL;
259 agp_fe.file_priv_list = next;
263 /* End - File flag list routines */
266 * Wrappers for agp_free_memory & agp_allocate_memory
267 * These make sure that internal lists are kept updated.
269 void agp_free_memory_wrap(struct agp_memory *memory)
271 agp_remove_from_pool(memory);
272 agp_free_memory(memory);
275 struct agp_memory *agp_allocate_memory_wrap(size_t pg_count, u32 type)
277 struct agp_memory *memory;
279 memory = agp_allocate_memory(agp_bridge, pg_count, type);
280 if (memory == NULL)
281 return NULL;
283 agp_insert_into_pool(memory);
284 return memory;
287 /* Routines for managing the list of controllers -
288 * These routines manage the current controller, and the list of
289 * controllers
292 static struct agp_controller *agp_find_controller_by_pid(pid_t id)
294 struct agp_controller *controller;
296 controller = agp_fe.controllers;
298 while (controller != NULL) {
299 if (controller->pid == id)
300 return controller;
301 controller = controller->next;
304 return NULL;
307 static struct agp_controller *agp_create_controller(pid_t id)
309 struct agp_controller *controller;
311 controller = kzalloc(sizeof(struct agp_controller), GFP_KERNEL);
312 if (controller == NULL)
313 return NULL;
315 controller->pid = id;
316 return controller;
319 static int agp_insert_controller(struct agp_controller *controller)
321 struct agp_controller *prev_controller;
323 prev_controller = agp_fe.controllers;
324 controller->next = prev_controller;
326 if (prev_controller != NULL)
327 prev_controller->prev = controller;
329 agp_fe.controllers = controller;
331 return 0;
334 static void agp_remove_all_clients(struct agp_controller *controller)
336 struct agp_client *client;
337 struct agp_client *temp;
339 client = controller->clients;
341 while (client) {
342 struct agp_file_private *priv;
344 temp = client;
345 agp_remove_seg_from_client(temp);
346 priv = agp_find_private(temp->pid);
348 if (priv != NULL) {
349 clear_bit(AGP_FF_IS_VALID, &priv->access_flags);
350 clear_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
352 client = client->next;
353 kfree(temp);
357 static void agp_remove_all_memory(struct agp_controller *controller)
359 struct agp_memory *memory;
360 struct agp_memory *temp;
362 memory = controller->pool;
364 while (memory) {
365 temp = memory;
366 memory = memory->next;
367 agp_free_memory_wrap(temp);
371 static int agp_remove_controller(struct agp_controller *controller)
373 struct agp_controller *prev_controller;
374 struct agp_controller *next_controller;
376 prev_controller = controller->prev;
377 next_controller = controller->next;
379 if (prev_controller != NULL) {
380 prev_controller->next = next_controller;
381 if (next_controller != NULL)
382 next_controller->prev = prev_controller;
384 } else {
385 if (next_controller != NULL)
386 next_controller->prev = NULL;
388 agp_fe.controllers = next_controller;
391 agp_remove_all_memory(controller);
392 agp_remove_all_clients(controller);
394 if (agp_fe.current_controller == controller) {
395 agp_fe.current_controller = NULL;
396 agp_fe.backend_acquired = false;
397 agp_backend_release(agp_bridge);
399 kfree(controller);
400 return 0;
403 static void agp_controller_make_current(struct agp_controller *controller)
405 struct agp_client *clients;
407 clients = controller->clients;
409 while (clients != NULL) {
410 struct agp_file_private *priv;
412 priv = agp_find_private(clients->pid);
414 if (priv != NULL) {
415 set_bit(AGP_FF_IS_VALID, &priv->access_flags);
416 set_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
418 clients = clients->next;
421 agp_fe.current_controller = controller;
424 static void agp_controller_release_current(struct agp_controller *controller,
425 struct agp_file_private *controller_priv)
427 struct agp_client *clients;
429 clear_bit(AGP_FF_IS_VALID, &controller_priv->access_flags);
430 clients = controller->clients;
432 while (clients != NULL) {
433 struct agp_file_private *priv;
435 priv = agp_find_private(clients->pid);
437 if (priv != NULL)
438 clear_bit(AGP_FF_IS_VALID, &priv->access_flags);
440 clients = clients->next;
443 agp_fe.current_controller = NULL;
444 agp_fe.used_by_controller = false;
445 agp_backend_release(agp_bridge);
449 * Routines for managing client lists -
450 * These routines are for managing the list of auth'ed clients.
453 static struct agp_client
454 *agp_find_client_in_controller(struct agp_controller *controller, pid_t id)
456 struct agp_client *client;
458 if (controller == NULL)
459 return NULL;
461 client = controller->clients;
463 while (client != NULL) {
464 if (client->pid == id)
465 return client;
466 client = client->next;
469 return NULL;
472 static struct agp_controller *agp_find_controller_for_client(pid_t id)
474 struct agp_controller *controller;
476 controller = agp_fe.controllers;
478 while (controller != NULL) {
479 if ((agp_find_client_in_controller(controller, id)) != NULL)
480 return controller;
481 controller = controller->next;
484 return NULL;
487 struct agp_client *agp_find_client_by_pid(pid_t id)
489 struct agp_client *temp;
491 if (agp_fe.current_controller == NULL)
492 return NULL;
494 temp = agp_find_client_in_controller(agp_fe.current_controller, id);
495 return temp;
498 static void agp_insert_client(struct agp_client *client)
500 struct agp_client *prev_client;
502 prev_client = agp_fe.current_controller->clients;
503 client->next = prev_client;
505 if (prev_client != NULL)
506 prev_client->prev = client;
508 agp_fe.current_controller->clients = client;
509 agp_fe.current_controller->num_clients++;
512 struct agp_client *agp_create_client(pid_t id)
514 struct agp_client *new_client;
516 new_client = kzalloc(sizeof(struct agp_client), GFP_KERNEL);
517 if (new_client == NULL)
518 return NULL;
520 new_client->pid = id;
521 agp_insert_client(new_client);
522 return new_client;
525 int agp_remove_client(pid_t id)
527 struct agp_client *client;
528 struct agp_client *prev_client;
529 struct agp_client *next_client;
530 struct agp_controller *controller;
532 controller = agp_find_controller_for_client(id);
533 if (controller == NULL)
534 return -EINVAL;
536 client = agp_find_client_in_controller(controller, id);
537 if (client == NULL)
538 return -EINVAL;
540 prev_client = client->prev;
541 next_client = client->next;
543 if (prev_client != NULL) {
544 prev_client->next = next_client;
545 if (next_client != NULL)
546 next_client->prev = prev_client;
548 } else {
549 if (next_client != NULL)
550 next_client->prev = NULL;
551 controller->clients = next_client;
554 controller->num_clients--;
555 agp_remove_seg_from_client(client);
556 kfree(client);
557 return 0;
560 /* End - Routines for managing client lists */
562 /* File Operations */
564 static int agp_mmap(struct file *file, struct vm_area_struct *vma)
566 unsigned int size, current_size;
567 unsigned long offset;
568 struct agp_client *client;
569 struct agp_file_private *priv = file->private_data;
570 struct agp_kern_info kerninfo;
572 mutex_lock(&(agp_fe.agp_mutex));
574 if (agp_fe.backend_acquired != true)
575 goto out_eperm;
577 if (!(test_bit(AGP_FF_IS_VALID, &priv->access_flags)))
578 goto out_eperm;
580 agp_copy_info(agp_bridge, &kerninfo);
581 size = vma->vm_end - vma->vm_start;
582 current_size = kerninfo.aper_size;
583 current_size = current_size * 0x100000;
584 offset = vma->vm_pgoff << PAGE_SHIFT;
585 DBG("%lx:%lx", offset, offset+size);
587 if (test_bit(AGP_FF_IS_CLIENT, &priv->access_flags)) {
588 if ((size + offset) > current_size)
589 goto out_inval;
591 client = agp_find_client_by_pid(current->pid);
593 if (client == NULL)
594 goto out_eperm;
596 if (!agp_find_seg_in_client(client, offset, size, vma->vm_page_prot))
597 goto out_inval;
599 DBG("client vm_ops=%p", kerninfo.vm_ops);
600 if (kerninfo.vm_ops) {
601 vma->vm_ops = kerninfo.vm_ops;
602 } else if (io_remap_pfn_range(vma, vma->vm_start,
603 (kerninfo.aper_base + offset) >> PAGE_SHIFT,
604 size,
605 pgprot_writecombine(vma->vm_page_prot))) {
606 goto out_again;
608 mutex_unlock(&(agp_fe.agp_mutex));
609 return 0;
612 if (test_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags)) {
613 if (size != current_size)
614 goto out_inval;
616 DBG("controller vm_ops=%p", kerninfo.vm_ops);
617 if (kerninfo.vm_ops) {
618 vma->vm_ops = kerninfo.vm_ops;
619 } else if (io_remap_pfn_range(vma, vma->vm_start,
620 kerninfo.aper_base >> PAGE_SHIFT,
621 size,
622 pgprot_writecombine(vma->vm_page_prot))) {
623 goto out_again;
625 mutex_unlock(&(agp_fe.agp_mutex));
626 return 0;
629 out_eperm:
630 mutex_unlock(&(agp_fe.agp_mutex));
631 return -EPERM;
633 out_inval:
634 mutex_unlock(&(agp_fe.agp_mutex));
635 return -EINVAL;
637 out_again:
638 mutex_unlock(&(agp_fe.agp_mutex));
639 return -EAGAIN;
642 static int agp_release(struct inode *inode, struct file *file)
644 struct agp_file_private *priv = file->private_data;
646 mutex_lock(&(agp_fe.agp_mutex));
648 DBG("priv=%p", priv);
650 if (test_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags)) {
651 struct agp_controller *controller;
653 controller = agp_find_controller_by_pid(priv->my_pid);
655 if (controller != NULL) {
656 if (controller == agp_fe.current_controller)
657 agp_controller_release_current(controller, priv);
658 agp_remove_controller(controller);
659 controller = NULL;
663 if (test_bit(AGP_FF_IS_CLIENT, &priv->access_flags))
664 agp_remove_client(priv->my_pid);
666 agp_remove_file_private(priv);
667 kfree(priv);
668 file->private_data = NULL;
669 mutex_unlock(&(agp_fe.agp_mutex));
670 return 0;
673 static int agp_open(struct inode *inode, struct file *file)
675 int minor = iminor(inode);
676 struct agp_file_private *priv;
677 struct agp_client *client;
679 if (minor != AGPGART_MINOR)
680 return -ENXIO;
682 mutex_lock(&(agp_fe.agp_mutex));
684 priv = kzalloc(sizeof(struct agp_file_private), GFP_KERNEL);
685 if (priv == NULL) {
686 mutex_unlock(&(agp_fe.agp_mutex));
687 return -ENOMEM;
690 set_bit(AGP_FF_ALLOW_CLIENT, &priv->access_flags);
691 priv->my_pid = current->pid;
693 if (capable(CAP_SYS_RAWIO))
694 /* Root priv, can be controller */
695 set_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags);
697 client = agp_find_client_by_pid(current->pid);
699 if (client != NULL) {
700 set_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
701 set_bit(AGP_FF_IS_VALID, &priv->access_flags);
703 file->private_data = (void *) priv;
704 agp_insert_file_private(priv);
705 DBG("private=%p, client=%p", priv, client);
707 mutex_unlock(&(agp_fe.agp_mutex));
709 return 0;
712 static int agpioc_info_wrap(struct agp_file_private *priv, void __user *arg)
714 struct agp_info userinfo;
715 struct agp_kern_info kerninfo;
717 agp_copy_info(agp_bridge, &kerninfo);
719 memset(&userinfo, 0, sizeof(userinfo));
720 userinfo.version.major = kerninfo.version.major;
721 userinfo.version.minor = kerninfo.version.minor;
722 userinfo.bridge_id = kerninfo.device->vendor |
723 (kerninfo.device->device << 16);
724 userinfo.agp_mode = kerninfo.mode;
725 userinfo.aper_base = kerninfo.aper_base;
726 userinfo.aper_size = kerninfo.aper_size;
727 userinfo.pg_total = userinfo.pg_system = kerninfo.max_memory;
728 userinfo.pg_used = kerninfo.current_memory;
730 if (copy_to_user(arg, &userinfo, sizeof(struct agp_info)))
731 return -EFAULT;
733 return 0;
736 int agpioc_acquire_wrap(struct agp_file_private *priv)
738 struct agp_controller *controller;
740 DBG("");
742 if (!(test_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags)))
743 return -EPERM;
745 if (agp_fe.current_controller != NULL)
746 return -EBUSY;
748 if (!agp_bridge)
749 return -ENODEV;
751 if (atomic_read(&agp_bridge->agp_in_use))
752 return -EBUSY;
754 atomic_inc(&agp_bridge->agp_in_use);
756 agp_fe.backend_acquired = true;
758 controller = agp_find_controller_by_pid(priv->my_pid);
760 if (controller != NULL) {
761 agp_controller_make_current(controller);
762 } else {
763 controller = agp_create_controller(priv->my_pid);
765 if (controller == NULL) {
766 agp_fe.backend_acquired = false;
767 agp_backend_release(agp_bridge);
768 return -ENOMEM;
770 agp_insert_controller(controller);
771 agp_controller_make_current(controller);
774 set_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags);
775 set_bit(AGP_FF_IS_VALID, &priv->access_flags);
776 return 0;
779 int agpioc_release_wrap(struct agp_file_private *priv)
781 DBG("");
782 agp_controller_release_current(agp_fe.current_controller, priv);
783 return 0;
786 int agpioc_setup_wrap(struct agp_file_private *priv, void __user *arg)
788 struct agp_setup mode;
790 DBG("");
791 if (copy_from_user(&mode, arg, sizeof(struct agp_setup)))
792 return -EFAULT;
794 agp_enable(agp_bridge, mode.agp_mode);
795 return 0;
798 static int agpioc_reserve_wrap(struct agp_file_private *priv, void __user *arg)
800 struct agp_region reserve;
801 struct agp_client *client;
802 struct agp_file_private *client_priv;
804 DBG("");
805 if (copy_from_user(&reserve, arg, sizeof(struct agp_region)))
806 return -EFAULT;
808 if ((unsigned) reserve.seg_count >= ~0U/sizeof(struct agp_segment))
809 return -EFAULT;
811 client = agp_find_client_by_pid(reserve.pid);
813 if (reserve.seg_count == 0) {
814 /* remove a client */
815 client_priv = agp_find_private(reserve.pid);
817 if (client_priv != NULL) {
818 set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
819 set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
821 if (client == NULL) {
822 /* client is already removed */
823 return 0;
825 return agp_remove_client(reserve.pid);
826 } else {
827 struct agp_segment *segment;
829 if (reserve.seg_count >= 16384)
830 return -EINVAL;
832 segment = kmalloc((sizeof(struct agp_segment) * reserve.seg_count),
833 GFP_KERNEL);
835 if (segment == NULL)
836 return -ENOMEM;
838 if (copy_from_user(segment, (void __user *) reserve.seg_list,
839 sizeof(struct agp_segment) * reserve.seg_count)) {
840 kfree(segment);
841 return -EFAULT;
843 reserve.seg_list = segment;
845 if (client == NULL) {
846 /* Create the client and add the segment */
847 client = agp_create_client(reserve.pid);
849 if (client == NULL) {
850 kfree(segment);
851 return -ENOMEM;
853 client_priv = agp_find_private(reserve.pid);
855 if (client_priv != NULL) {
856 set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
857 set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
860 return agp_create_segment(client, &reserve);
862 /* Will never really happen */
863 return -EINVAL;
866 int agpioc_protect_wrap(struct agp_file_private *priv)
868 DBG("");
869 /* This function is not currently implemented */
870 return -EINVAL;
873 static int agpioc_allocate_wrap(struct agp_file_private *priv, void __user *arg)
875 struct agp_memory *memory;
876 struct agp_allocate alloc;
878 DBG("");
879 if (copy_from_user(&alloc, arg, sizeof(struct agp_allocate)))
880 return -EFAULT;
882 if (alloc.type >= AGP_USER_TYPES)
883 return -EINVAL;
885 memory = agp_allocate_memory_wrap(alloc.pg_count, alloc.type);
887 if (memory == NULL)
888 return -ENOMEM;
890 alloc.key = memory->key;
891 alloc.physical = memory->physical;
893 if (copy_to_user(arg, &alloc, sizeof(struct agp_allocate))) {
894 agp_free_memory_wrap(memory);
895 return -EFAULT;
897 return 0;
900 int agpioc_deallocate_wrap(struct agp_file_private *priv, int arg)
902 struct agp_memory *memory;
904 DBG("");
905 memory = agp_find_mem_by_key(arg);
907 if (memory == NULL)
908 return -EINVAL;
910 agp_free_memory_wrap(memory);
911 return 0;
914 static int agpioc_bind_wrap(struct agp_file_private *priv, void __user *arg)
916 struct agp_bind bind_info;
917 struct agp_memory *memory;
919 DBG("");
920 if (copy_from_user(&bind_info, arg, sizeof(struct agp_bind)))
921 return -EFAULT;
923 memory = agp_find_mem_by_key(bind_info.key);
925 if (memory == NULL)
926 return -EINVAL;
928 return agp_bind_memory(memory, bind_info.pg_start);
931 static int agpioc_unbind_wrap(struct agp_file_private *priv, void __user *arg)
933 struct agp_memory *memory;
934 struct agp_unbind unbind;
936 DBG("");
937 if (copy_from_user(&unbind, arg, sizeof(struct agp_unbind)))
938 return -EFAULT;
940 memory = agp_find_mem_by_key(unbind.key);
942 if (memory == NULL)
943 return -EINVAL;
945 return agp_unbind_memory(memory);
948 static long agp_ioctl(struct file *file,
949 unsigned int cmd, unsigned long arg)
951 struct agp_file_private *curr_priv = file->private_data;
952 int ret_val = -ENOTTY;
954 DBG("priv=%p, cmd=%x", curr_priv, cmd);
955 mutex_lock(&(agp_fe.agp_mutex));
957 if ((agp_fe.current_controller == NULL) &&
958 (cmd != AGPIOC_ACQUIRE)) {
959 ret_val = -EINVAL;
960 goto ioctl_out;
962 if ((agp_fe.backend_acquired != true) &&
963 (cmd != AGPIOC_ACQUIRE)) {
964 ret_val = -EBUSY;
965 goto ioctl_out;
967 if (cmd != AGPIOC_ACQUIRE) {
968 if (!(test_bit(AGP_FF_IS_CONTROLLER, &curr_priv->access_flags))) {
969 ret_val = -EPERM;
970 goto ioctl_out;
972 /* Use the original pid of the controller,
973 * in case it's threaded */
975 if (agp_fe.current_controller->pid != curr_priv->my_pid) {
976 ret_val = -EBUSY;
977 goto ioctl_out;
981 switch (cmd) {
982 case AGPIOC_INFO:
983 ret_val = agpioc_info_wrap(curr_priv, (void __user *) arg);
984 break;
986 case AGPIOC_ACQUIRE:
987 ret_val = agpioc_acquire_wrap(curr_priv);
988 break;
990 case AGPIOC_RELEASE:
991 ret_val = agpioc_release_wrap(curr_priv);
992 break;
994 case AGPIOC_SETUP:
995 ret_val = agpioc_setup_wrap(curr_priv, (void __user *) arg);
996 break;
998 case AGPIOC_RESERVE:
999 ret_val = agpioc_reserve_wrap(curr_priv, (void __user *) arg);
1000 break;
1002 case AGPIOC_PROTECT:
1003 ret_val = agpioc_protect_wrap(curr_priv);
1004 break;
1006 case AGPIOC_ALLOCATE:
1007 ret_val = agpioc_allocate_wrap(curr_priv, (void __user *) arg);
1008 break;
1010 case AGPIOC_DEALLOCATE:
1011 ret_val = agpioc_deallocate_wrap(curr_priv, (int) arg);
1012 break;
1014 case AGPIOC_BIND:
1015 ret_val = agpioc_bind_wrap(curr_priv, (void __user *) arg);
1016 break;
1018 case AGPIOC_UNBIND:
1019 ret_val = agpioc_unbind_wrap(curr_priv, (void __user *) arg);
1020 break;
1022 case AGPIOC_CHIPSET_FLUSH:
1023 break;
1026 ioctl_out:
1027 DBG("ioctl returns %d\n", ret_val);
1028 mutex_unlock(&(agp_fe.agp_mutex));
1029 return ret_val;
1032 static const struct file_operations agp_fops =
1034 .owner = THIS_MODULE,
1035 .llseek = no_llseek,
1036 .unlocked_ioctl = agp_ioctl,
1037 #ifdef CONFIG_COMPAT
1038 .compat_ioctl = compat_agp_ioctl,
1039 #endif
1040 .mmap = agp_mmap,
1041 .open = agp_open,
1042 .release = agp_release,
1045 static struct miscdevice agp_miscdev =
1047 .minor = AGPGART_MINOR,
1048 .name = "agpgart",
1049 .fops = &agp_fops
1052 int agp_frontend_initialize(void)
1054 memset(&agp_fe, 0, sizeof(struct agp_front_data));
1055 mutex_init(&(agp_fe.agp_mutex));
1057 if (misc_register(&agp_miscdev)) {
1058 printk(KERN_ERR PFX "unable to get minor: %d\n", AGPGART_MINOR);
1059 return -EIO;
1061 return 0;
1064 void agp_frontend_cleanup(void)
1066 misc_deregister(&agp_miscdev);