[AGPGART] Replace kmalloc+memset's with kzalloc's
[linux-2.6/x86.git] / drivers / char / agp / frontend.c
blob17f520c9d4714aa90a145f200c31675b38699882
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/init.h>
35 #include <linux/miscdevice.h>
36 #include <linux/agp_backend.h>
37 #include <linux/agpgart.h>
38 #include <linux/slab.h>
39 #include <linux/mm.h>
40 #include <asm/uaccess.h>
41 #include <asm/pgtable.h>
42 #include "agp.h"
44 static struct agp_front_data agp_fe;
46 static struct agp_memory *agp_find_mem_by_key(int key)
48 struct agp_memory *curr;
50 if (agp_fe.current_controller == NULL)
51 return NULL;
53 curr = agp_fe.current_controller->pool;
55 while (curr != NULL) {
56 if (curr->key == key)
57 break;
58 curr = curr->next;
61 DBG("key=%d -> mem=%p", key, curr);
62 return curr;
65 static void agp_remove_from_pool(struct agp_memory *temp)
67 struct agp_memory *prev;
68 struct agp_memory *next;
70 /* Check to see if this is even in the memory pool */
72 DBG("mem=%p", temp);
73 if (agp_find_mem_by_key(temp->key) != NULL) {
74 next = temp->next;
75 prev = temp->prev;
77 if (prev != NULL) {
78 prev->next = next;
79 if (next != NULL)
80 next->prev = prev;
82 } else {
83 /* This is the first item on the list */
84 if (next != NULL)
85 next->prev = NULL;
87 agp_fe.current_controller->pool = next;
93 * Routines for managing each client's segment list -
94 * These routines handle adding and removing segments
95 * to each auth'ed client.
98 static struct
99 agp_segment_priv *agp_find_seg_in_client(const struct agp_client *client,
100 unsigned long offset,
101 int size, pgprot_t page_prot)
103 struct agp_segment_priv *seg;
104 int num_segments, i;
105 off_t pg_start;
106 size_t pg_count;
108 pg_start = offset / 4096;
109 pg_count = size / 4096;
110 seg = *(client->segments);
111 num_segments = client->num_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 /* Originally taken from linux/mm/mmap.c from the array
155 * protection_map.
156 * The original really should be exported to modules, or
157 * some routine which does the conversion for you
160 static const pgprot_t my_protect_map[16] =
162 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
163 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
166 static pgprot_t agp_convert_mmap_flags(int prot)
168 #define _trans(x,bit1,bit2) \
169 ((bit1==bit2)?(x&bit1):(x&bit1)?bit2:0)
171 unsigned long prot_bits;
172 pgprot_t temp;
174 prot_bits = _trans(prot, PROT_READ, VM_READ) |
175 _trans(prot, PROT_WRITE, VM_WRITE) |
176 _trans(prot, PROT_EXEC, VM_EXEC);
178 prot_bits |= VM_SHARED;
180 temp = my_protect_map[prot_bits & 0x0000000f];
182 return temp;
185 static int agp_create_segment(struct agp_client *client, struct agp_region *region)
187 struct agp_segment_priv **ret_seg;
188 struct agp_segment_priv *seg;
189 struct agp_segment *user_seg;
190 size_t i;
192 seg = kzalloc((sizeof(struct agp_segment_priv) * region->seg_count), GFP_KERNEL);
193 if (seg == NULL) {
194 kfree(region->seg_list);
195 region->seg_list = NULL;
196 return -ENOMEM;
198 user_seg = region->seg_list;
200 for (i = 0; i < region->seg_count; i++) {
201 seg[i].pg_start = user_seg[i].pg_start;
202 seg[i].pg_count = user_seg[i].pg_count;
203 seg[i].prot = agp_convert_mmap_flags(user_seg[i].prot);
205 kfree(region->seg_list);
206 region->seg_list = NULL;
208 ret_seg = kmalloc(sizeof(void *), GFP_KERNEL);
209 if (ret_seg == NULL) {
210 kfree(seg);
211 return -ENOMEM;
213 *ret_seg = seg;
214 agp_add_seg_to_client(client, ret_seg, region->seg_count);
215 return 0;
218 /* End - Routines for managing each client's segment list */
220 /* This function must only be called when current_controller != NULL */
221 static void agp_insert_into_pool(struct agp_memory * temp)
223 struct agp_memory *prev;
225 prev = agp_fe.current_controller->pool;
227 if (prev != NULL) {
228 prev->prev = temp;
229 temp->next = prev;
231 agp_fe.current_controller->pool = temp;
235 /* File private list routines */
237 static struct agp_file_private *agp_find_private(pid_t pid)
239 struct agp_file_private *curr;
241 curr = agp_fe.file_priv_list;
243 while (curr != NULL) {
244 if (curr->my_pid == pid)
245 return curr;
246 curr = curr->next;
249 return NULL;
252 static void agp_insert_file_private(struct agp_file_private * priv)
254 struct agp_file_private *prev;
256 prev = agp_fe.file_priv_list;
258 if (prev != NULL)
259 prev->prev = priv;
260 priv->next = prev;
261 agp_fe.file_priv_list = priv;
264 static void agp_remove_file_private(struct agp_file_private * priv)
266 struct agp_file_private *next;
267 struct agp_file_private *prev;
269 next = priv->next;
270 prev = priv->prev;
272 if (prev != NULL) {
273 prev->next = next;
275 if (next != NULL)
276 next->prev = prev;
278 } else {
279 if (next != NULL)
280 next->prev = NULL;
282 agp_fe.file_priv_list = next;
286 /* End - File flag list routines */
289 * Wrappers for agp_free_memory & agp_allocate_memory
290 * These make sure that internal lists are kept updated.
292 static void agp_free_memory_wrap(struct agp_memory *memory)
294 agp_remove_from_pool(memory);
295 agp_free_memory(memory);
298 static struct agp_memory *agp_allocate_memory_wrap(size_t pg_count, u32 type)
300 struct agp_memory *memory;
302 memory = agp_allocate_memory(agp_bridge, pg_count, type);
303 if (memory == NULL)
304 return NULL;
306 agp_insert_into_pool(memory);
307 return memory;
310 /* Routines for managing the list of controllers -
311 * These routines manage the current controller, and the list of
312 * controllers
315 static struct agp_controller *agp_find_controller_by_pid(pid_t id)
317 struct agp_controller *controller;
319 controller = agp_fe.controllers;
321 while (controller != NULL) {
322 if (controller->pid == id)
323 return controller;
324 controller = controller->next;
327 return NULL;
330 static struct agp_controller *agp_create_controller(pid_t id)
332 struct agp_controller *controller;
334 controller = kzalloc(sizeof(struct agp_controller), GFP_KERNEL);
335 if (controller == NULL)
336 return NULL;
338 controller->pid = id;
339 return controller;
342 static int agp_insert_controller(struct agp_controller *controller)
344 struct agp_controller *prev_controller;
346 prev_controller = agp_fe.controllers;
347 controller->next = prev_controller;
349 if (prev_controller != NULL)
350 prev_controller->prev = controller;
352 agp_fe.controllers = controller;
354 return 0;
357 static void agp_remove_all_clients(struct agp_controller *controller)
359 struct agp_client *client;
360 struct agp_client *temp;
362 client = controller->clients;
364 while (client) {
365 struct agp_file_private *priv;
367 temp = client;
368 agp_remove_seg_from_client(temp);
369 priv = agp_find_private(temp->pid);
371 if (priv != NULL) {
372 clear_bit(AGP_FF_IS_VALID, &priv->access_flags);
373 clear_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
375 client = client->next;
376 kfree(temp);
380 static void agp_remove_all_memory(struct agp_controller *controller)
382 struct agp_memory *memory;
383 struct agp_memory *temp;
385 memory = controller->pool;
387 while (memory) {
388 temp = memory;
389 memory = memory->next;
390 agp_free_memory_wrap(temp);
394 static int agp_remove_controller(struct agp_controller *controller)
396 struct agp_controller *prev_controller;
397 struct agp_controller *next_controller;
399 prev_controller = controller->prev;
400 next_controller = controller->next;
402 if (prev_controller != NULL) {
403 prev_controller->next = next_controller;
404 if (next_controller != NULL)
405 next_controller->prev = prev_controller;
407 } else {
408 if (next_controller != NULL)
409 next_controller->prev = NULL;
411 agp_fe.controllers = next_controller;
414 agp_remove_all_memory(controller);
415 agp_remove_all_clients(controller);
417 if (agp_fe.current_controller == controller) {
418 agp_fe.current_controller = NULL;
419 agp_fe.backend_acquired = FALSE;
420 agp_backend_release(agp_bridge);
422 kfree(controller);
423 return 0;
426 static void agp_controller_make_current(struct agp_controller *controller)
428 struct agp_client *clients;
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 set_bit(AGP_FF_IS_VALID, &priv->access_flags);
439 set_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
441 clients = clients->next;
444 agp_fe.current_controller = controller;
447 static void agp_controller_release_current(struct agp_controller *controller,
448 struct agp_file_private *controller_priv)
450 struct agp_client *clients;
452 clear_bit(AGP_FF_IS_VALID, &controller_priv->access_flags);
453 clients = controller->clients;
455 while (clients != NULL) {
456 struct agp_file_private *priv;
458 priv = agp_find_private(clients->pid);
460 if (priv != NULL)
461 clear_bit(AGP_FF_IS_VALID, &priv->access_flags);
463 clients = clients->next;
466 agp_fe.current_controller = NULL;
467 agp_fe.used_by_controller = FALSE;
468 agp_backend_release(agp_bridge);
472 * Routines for managing client lists -
473 * These routines are for managing the list of auth'ed clients.
476 static struct agp_client
477 *agp_find_client_in_controller(struct agp_controller *controller, pid_t id)
479 struct agp_client *client;
481 if (controller == NULL)
482 return NULL;
484 client = controller->clients;
486 while (client != NULL) {
487 if (client->pid == id)
488 return client;
489 client = client->next;
492 return NULL;
495 static struct agp_controller *agp_find_controller_for_client(pid_t id)
497 struct agp_controller *controller;
499 controller = agp_fe.controllers;
501 while (controller != NULL) {
502 if ((agp_find_client_in_controller(controller, id)) != NULL)
503 return controller;
504 controller = controller->next;
507 return NULL;
510 static struct agp_client *agp_find_client_by_pid(pid_t id)
512 struct agp_client *temp;
514 if (agp_fe.current_controller == NULL)
515 return NULL;
517 temp = agp_find_client_in_controller(agp_fe.current_controller, id);
518 return temp;
521 static void agp_insert_client(struct agp_client *client)
523 struct agp_client *prev_client;
525 prev_client = agp_fe.current_controller->clients;
526 client->next = prev_client;
528 if (prev_client != NULL)
529 prev_client->prev = client;
531 agp_fe.current_controller->clients = client;
532 agp_fe.current_controller->num_clients++;
535 static struct agp_client *agp_create_client(pid_t id)
537 struct agp_client *new_client;
539 new_client = kzalloc(sizeof(struct agp_client), GFP_KERNEL);
540 if (new_client == NULL)
541 return NULL;
543 new_client->pid = id;
544 agp_insert_client(new_client);
545 return new_client;
548 static int agp_remove_client(pid_t id)
550 struct agp_client *client;
551 struct agp_client *prev_client;
552 struct agp_client *next_client;
553 struct agp_controller *controller;
555 controller = agp_find_controller_for_client(id);
556 if (controller == NULL)
557 return -EINVAL;
559 client = agp_find_client_in_controller(controller, id);
560 if (client == NULL)
561 return -EINVAL;
563 prev_client = client->prev;
564 next_client = client->next;
566 if (prev_client != NULL) {
567 prev_client->next = next_client;
568 if (next_client != NULL)
569 next_client->prev = prev_client;
571 } else {
572 if (next_client != NULL)
573 next_client->prev = NULL;
574 controller->clients = next_client;
577 controller->num_clients--;
578 agp_remove_seg_from_client(client);
579 kfree(client);
580 return 0;
583 /* End - Routines for managing client lists */
585 /* File Operations */
587 static int agp_mmap(struct file *file, struct vm_area_struct *vma)
589 unsigned int size, current_size;
590 unsigned long offset;
591 struct agp_client *client;
592 struct agp_file_private *priv = file->private_data;
593 struct agp_kern_info kerninfo;
595 down(&(agp_fe.agp_mutex));
597 if (agp_fe.backend_acquired != TRUE)
598 goto out_eperm;
600 if (!(test_bit(AGP_FF_IS_VALID, &priv->access_flags)))
601 goto out_eperm;
603 agp_copy_info(agp_bridge, &kerninfo);
604 size = vma->vm_end - vma->vm_start;
605 current_size = kerninfo.aper_size;
606 current_size = current_size * 0x100000;
607 offset = vma->vm_pgoff << PAGE_SHIFT;
608 DBG("%lx:%lx", offset, offset+size);
610 if (test_bit(AGP_FF_IS_CLIENT, &priv->access_flags)) {
611 if ((size + offset) > current_size)
612 goto out_inval;
614 client = agp_find_client_by_pid(current->pid);
616 if (client == NULL)
617 goto out_eperm;
619 if (!agp_find_seg_in_client(client, offset, size, vma->vm_page_prot))
620 goto out_inval;
622 DBG("client vm_ops=%p", kerninfo.vm_ops);
623 if (kerninfo.vm_ops) {
624 vma->vm_ops = kerninfo.vm_ops;
625 } else if (io_remap_pfn_range(vma, vma->vm_start,
626 (kerninfo.aper_base + offset) >> PAGE_SHIFT,
627 size, vma->vm_page_prot)) {
628 goto out_again;
630 up(&(agp_fe.agp_mutex));
631 return 0;
634 if (test_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags)) {
635 if (size != current_size)
636 goto out_inval;
638 DBG("controller vm_ops=%p", kerninfo.vm_ops);
639 if (kerninfo.vm_ops) {
640 vma->vm_ops = kerninfo.vm_ops;
641 } else if (io_remap_pfn_range(vma, vma->vm_start,
642 kerninfo.aper_base >> PAGE_SHIFT,
643 size, vma->vm_page_prot)) {
644 goto out_again;
646 up(&(agp_fe.agp_mutex));
647 return 0;
650 out_eperm:
651 up(&(agp_fe.agp_mutex));
652 return -EPERM;
654 out_inval:
655 up(&(agp_fe.agp_mutex));
656 return -EINVAL;
658 out_again:
659 up(&(agp_fe.agp_mutex));
660 return -EAGAIN;
663 static int agp_release(struct inode *inode, struct file *file)
665 struct agp_file_private *priv = file->private_data;
667 down(&(agp_fe.agp_mutex));
669 DBG("priv=%p", priv);
671 if (test_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags)) {
672 struct agp_controller *controller;
674 controller = agp_find_controller_by_pid(priv->my_pid);
676 if (controller != NULL) {
677 if (controller == agp_fe.current_controller)
678 agp_controller_release_current(controller, priv);
679 agp_remove_controller(controller);
680 controller = NULL;
684 if (test_bit(AGP_FF_IS_CLIENT, &priv->access_flags))
685 agp_remove_client(priv->my_pid);
687 agp_remove_file_private(priv);
688 kfree(priv);
689 file->private_data = NULL;
690 up(&(agp_fe.agp_mutex));
691 return 0;
694 static int agp_open(struct inode *inode, struct file *file)
696 int minor = iminor(inode);
697 struct agp_file_private *priv;
698 struct agp_client *client;
699 int rc = -ENXIO;
701 down(&(agp_fe.agp_mutex));
703 if (minor != AGPGART_MINOR)
704 goto err_out;
706 priv = kzalloc(sizeof(struct agp_file_private), GFP_KERNEL);
707 if (priv == NULL)
708 goto err_out_nomem;
710 set_bit(AGP_FF_ALLOW_CLIENT, &priv->access_flags);
711 priv->my_pid = current->pid;
713 if ((current->uid == 0) || (current->suid == 0)) {
714 /* Root priv, can be controller */
715 set_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags);
717 client = agp_find_client_by_pid(current->pid);
719 if (client != NULL) {
720 set_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
721 set_bit(AGP_FF_IS_VALID, &priv->access_flags);
723 file->private_data = (void *) priv;
724 agp_insert_file_private(priv);
725 DBG("private=%p, client=%p", priv, client);
726 up(&(agp_fe.agp_mutex));
727 return 0;
729 err_out_nomem:
730 rc = -ENOMEM;
731 err_out:
732 up(&(agp_fe.agp_mutex));
733 return rc;
737 static ssize_t agp_read(struct file *file, char __user *buf,
738 size_t count, loff_t * ppos)
740 return -EINVAL;
743 static ssize_t agp_write(struct file *file, const char __user *buf,
744 size_t count, loff_t * ppos)
746 return -EINVAL;
749 static int agpioc_info_wrap(struct agp_file_private *priv, void __user *arg)
751 struct agp_info userinfo;
752 struct agp_kern_info kerninfo;
754 agp_copy_info(agp_bridge, &kerninfo);
756 userinfo.version.major = kerninfo.version.major;
757 userinfo.version.minor = kerninfo.version.minor;
758 userinfo.bridge_id = kerninfo.device->vendor |
759 (kerninfo.device->device << 16);
760 userinfo.agp_mode = kerninfo.mode;
761 userinfo.aper_base = kerninfo.aper_base;
762 userinfo.aper_size = kerninfo.aper_size;
763 userinfo.pg_total = userinfo.pg_system = kerninfo.max_memory;
764 userinfo.pg_used = kerninfo.current_memory;
766 if (copy_to_user(arg, &userinfo, sizeof(struct agp_info)))
767 return -EFAULT;
769 return 0;
772 static int agpioc_acquire_wrap(struct agp_file_private *priv)
774 struct agp_controller *controller;
776 DBG("");
778 if (!(test_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags)))
779 return -EPERM;
781 if (agp_fe.current_controller != NULL)
782 return -EBUSY;
784 if(!agp_bridge)
785 return -ENODEV;
787 if (atomic_read(&agp_bridge->agp_in_use))
788 return -EBUSY;
790 atomic_inc(&agp_bridge->agp_in_use);
792 agp_fe.backend_acquired = TRUE;
794 controller = agp_find_controller_by_pid(priv->my_pid);
796 if (controller != NULL) {
797 agp_controller_make_current(controller);
798 } else {
799 controller = agp_create_controller(priv->my_pid);
801 if (controller == NULL) {
802 agp_fe.backend_acquired = FALSE;
803 agp_backend_release(agp_bridge);
804 return -ENOMEM;
806 agp_insert_controller(controller);
807 agp_controller_make_current(controller);
810 set_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags);
811 set_bit(AGP_FF_IS_VALID, &priv->access_flags);
812 return 0;
815 static int agpioc_release_wrap(struct agp_file_private *priv)
817 DBG("");
818 agp_controller_release_current(agp_fe.current_controller, priv);
819 return 0;
822 static int agpioc_setup_wrap(struct agp_file_private *priv, void __user *arg)
824 struct agp_setup mode;
826 DBG("");
827 if (copy_from_user(&mode, arg, sizeof(struct agp_setup)))
828 return -EFAULT;
830 agp_enable(agp_bridge, mode.agp_mode);
831 return 0;
834 static int agpioc_reserve_wrap(struct agp_file_private *priv, void __user *arg)
836 struct agp_region reserve;
837 struct agp_client *client;
838 struct agp_file_private *client_priv;
840 DBG("");
841 if (copy_from_user(&reserve, arg, sizeof(struct agp_region)))
842 return -EFAULT;
844 if ((unsigned) reserve.seg_count >= ~0U/sizeof(struct agp_segment))
845 return -EFAULT;
847 client = agp_find_client_by_pid(reserve.pid);
849 if (reserve.seg_count == 0) {
850 /* remove a client */
851 client_priv = agp_find_private(reserve.pid);
853 if (client_priv != NULL) {
854 set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
855 set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
857 if (client == NULL) {
858 /* client is already removed */
859 return 0;
861 return agp_remove_client(reserve.pid);
862 } else {
863 struct agp_segment *segment;
865 if (reserve.seg_count >= 16384)
866 return -EINVAL;
868 segment = kmalloc((sizeof(struct agp_segment) * reserve.seg_count),
869 GFP_KERNEL);
871 if (segment == NULL)
872 return -ENOMEM;
874 if (copy_from_user(segment, (void __user *) reserve.seg_list,
875 sizeof(struct agp_segment) * reserve.seg_count)) {
876 kfree(segment);
877 return -EFAULT;
879 reserve.seg_list = segment;
881 if (client == NULL) {
882 /* Create the client and add the segment */
883 client = agp_create_client(reserve.pid);
885 if (client == NULL) {
886 kfree(segment);
887 return -ENOMEM;
889 client_priv = agp_find_private(reserve.pid);
891 if (client_priv != NULL) {
892 set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
893 set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
896 return agp_create_segment(client, &reserve);
898 /* Will never really happen */
899 return -EINVAL;
902 static int agpioc_protect_wrap(struct agp_file_private *priv)
904 DBG("");
905 /* This function is not currently implemented */
906 return -EINVAL;
909 static int agpioc_allocate_wrap(struct agp_file_private *priv, void __user *arg)
911 struct agp_memory *memory;
912 struct agp_allocate alloc;
914 DBG("");
915 if (copy_from_user(&alloc, arg, sizeof(struct agp_allocate)))
916 return -EFAULT;
918 memory = agp_allocate_memory_wrap(alloc.pg_count, alloc.type);
920 if (memory == NULL)
921 return -ENOMEM;
923 alloc.key = memory->key;
924 alloc.physical = memory->physical;
926 if (copy_to_user(arg, &alloc, sizeof(struct agp_allocate))) {
927 agp_free_memory_wrap(memory);
928 return -EFAULT;
930 return 0;
933 static int agpioc_deallocate_wrap(struct agp_file_private *priv, int arg)
935 struct agp_memory *memory;
937 DBG("");
938 memory = agp_find_mem_by_key(arg);
940 if (memory == NULL)
941 return -EINVAL;
943 agp_free_memory_wrap(memory);
944 return 0;
947 static int agpioc_bind_wrap(struct agp_file_private *priv, void __user *arg)
949 struct agp_bind bind_info;
950 struct agp_memory *memory;
952 DBG("");
953 if (copy_from_user(&bind_info, arg, sizeof(struct agp_bind)))
954 return -EFAULT;
956 memory = agp_find_mem_by_key(bind_info.key);
958 if (memory == NULL)
959 return -EINVAL;
961 return agp_bind_memory(memory, bind_info.pg_start);
964 static int agpioc_unbind_wrap(struct agp_file_private *priv, void __user *arg)
966 struct agp_memory *memory;
967 struct agp_unbind unbind;
969 DBG("");
970 if (copy_from_user(&unbind, arg, sizeof(struct agp_unbind)))
971 return -EFAULT;
973 memory = agp_find_mem_by_key(unbind.key);
975 if (memory == NULL)
976 return -EINVAL;
978 return agp_unbind_memory(memory);
981 static int agp_ioctl(struct inode *inode, struct file *file,
982 unsigned int cmd, unsigned long arg)
984 struct agp_file_private *curr_priv = file->private_data;
985 int ret_val = -ENOTTY;
987 DBG("priv=%p, cmd=%x", curr_priv, cmd);
988 down(&(agp_fe.agp_mutex));
990 if ((agp_fe.current_controller == NULL) &&
991 (cmd != AGPIOC_ACQUIRE)) {
992 ret_val = -EINVAL;
993 goto ioctl_out;
995 if ((agp_fe.backend_acquired != TRUE) &&
996 (cmd != AGPIOC_ACQUIRE)) {
997 ret_val = -EBUSY;
998 goto ioctl_out;
1000 if (cmd != AGPIOC_ACQUIRE) {
1001 if (!(test_bit(AGP_FF_IS_CONTROLLER, &curr_priv->access_flags))) {
1002 ret_val = -EPERM;
1003 goto ioctl_out;
1005 /* Use the original pid of the controller,
1006 * in case it's threaded */
1008 if (agp_fe.current_controller->pid != curr_priv->my_pid) {
1009 ret_val = -EBUSY;
1010 goto ioctl_out;
1014 switch (cmd) {
1015 case AGPIOC_INFO:
1016 ret_val = agpioc_info_wrap(curr_priv, (void __user *) arg);
1017 break;
1019 case AGPIOC_ACQUIRE:
1020 ret_val = agpioc_acquire_wrap(curr_priv);
1021 break;
1023 case AGPIOC_RELEASE:
1024 ret_val = agpioc_release_wrap(curr_priv);
1025 break;
1027 case AGPIOC_SETUP:
1028 ret_val = agpioc_setup_wrap(curr_priv, (void __user *) arg);
1029 break;
1031 case AGPIOC_RESERVE:
1032 ret_val = agpioc_reserve_wrap(curr_priv, (void __user *) arg);
1033 break;
1035 case AGPIOC_PROTECT:
1036 ret_val = agpioc_protect_wrap(curr_priv);
1037 break;
1039 case AGPIOC_ALLOCATE:
1040 ret_val = agpioc_allocate_wrap(curr_priv, (void __user *) arg);
1041 break;
1043 case AGPIOC_DEALLOCATE:
1044 ret_val = agpioc_deallocate_wrap(curr_priv, (int) arg);
1045 break;
1047 case AGPIOC_BIND:
1048 ret_val = agpioc_bind_wrap(curr_priv, (void __user *) arg);
1049 break;
1051 case AGPIOC_UNBIND:
1052 ret_val = agpioc_unbind_wrap(curr_priv, (void __user *) arg);
1053 break;
1056 ioctl_out:
1057 DBG("ioctl returns %d\n", ret_val);
1058 up(&(agp_fe.agp_mutex));
1059 return ret_val;
1062 static struct file_operations agp_fops =
1064 .owner = THIS_MODULE,
1065 .llseek = no_llseek,
1066 .read = agp_read,
1067 .write = agp_write,
1068 .ioctl = agp_ioctl,
1069 .mmap = agp_mmap,
1070 .open = agp_open,
1071 .release = agp_release,
1074 static struct miscdevice agp_miscdev =
1076 .minor = AGPGART_MINOR,
1077 .name = "agpgart",
1078 .fops = &agp_fops
1081 int agp_frontend_initialize(void)
1083 memset(&agp_fe, 0, sizeof(struct agp_front_data));
1084 sema_init(&(agp_fe.agp_mutex), 1);
1086 if (misc_register(&agp_miscdev)) {
1087 printk(KERN_ERR PFX "unable to get minor: %d\n", AGPGART_MINOR);
1088 return -EIO;
1090 return 0;
1093 void agp_frontend_cleanup(void)
1095 misc_deregister(&agp_miscdev);