e8a01f264540e1cb9675eb821e0f3769c7a9667c
[safe/jmp/linux-2.6] / drivers / uio / uio.c
1 /*
2  * drivers/uio/uio.c
3  *
4  * Copyright(C) 2005, Benedikt Spranger <b.spranger@linutronix.de>
5  * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
6  * Copyright(C) 2006, Hans J. Koch <hjk@linutronix.de>
7  * Copyright(C) 2006, Greg Kroah-Hartman <greg@kroah.com>
8  *
9  * Userspace IO
10  *
11  * Base Functions
12  *
13  * Licensed under the GPLv2 only.
14  */
15
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/poll.h>
19 #include <linux/device.h>
20 #include <linux/mm.h>
21 #include <linux/idr.h>
22 #include <linux/string.h>
23 #include <linux/kobject.h>
24 #include <linux/uio_driver.h>
25
26 #define UIO_MAX_DEVICES 255
27
28 struct uio_device {
29         struct module           *owner;
30         struct device           *dev;
31         int                     minor;
32         atomic_t                event;
33         struct fasync_struct    *async_queue;
34         wait_queue_head_t       wait;
35         int                     vma_count;
36         struct uio_info         *info;
37         struct kobject          *map_dir;
38 };
39
40 static int uio_major;
41 static DEFINE_IDR(uio_idr);
42 static const struct file_operations uio_fops;
43
44 /* UIO class infrastructure */
45 static struct uio_class {
46         struct kref kref;
47         struct class *class;
48 } *uio_class;
49
50 /*
51  * attributes
52  */
53
54 struct uio_map {
55         struct kobject kobj;
56         struct uio_mem *mem;
57 };
58 #define to_map(map) container_of(map, struct uio_map, kobj)
59
60 static ssize_t map_addr_show(struct uio_mem *mem, char *buf)
61 {
62         return sprintf(buf, "0x%lx\n", mem->addr);
63 }
64
65 static ssize_t map_size_show(struct uio_mem *mem, char *buf)
66 {
67         return sprintf(buf, "0x%lx\n", mem->size);
68 }
69
70 struct uio_sysfs_entry {
71         struct attribute attr;
72         ssize_t (*show)(struct uio_mem *, char *);
73         ssize_t (*store)(struct uio_mem *, const char *, size_t);
74 };
75
76 static struct uio_sysfs_entry addr_attribute =
77         __ATTR(addr, S_IRUGO, map_addr_show, NULL);
78 static struct uio_sysfs_entry size_attribute =
79         __ATTR(size, S_IRUGO, map_size_show, NULL);
80
81 static struct attribute *attrs[] = {
82         &addr_attribute.attr,
83         &size_attribute.attr,
84         NULL,   /* need to NULL terminate the list of attributes */
85 };
86
87 static void map_release(struct kobject *kobj)
88 {
89         struct uio_map *map = to_map(kobj);
90         kfree(map);
91 }
92
93 static ssize_t map_type_show(struct kobject *kobj, struct attribute *attr,
94                              char *buf)
95 {
96         struct uio_map *map = to_map(kobj);
97         struct uio_mem *mem = map->mem;
98         struct uio_sysfs_entry *entry;
99
100         entry = container_of(attr, struct uio_sysfs_entry, attr);
101
102         if (!entry->show)
103                 return -EIO;
104
105         return entry->show(mem, buf);
106 }
107
108 static struct sysfs_ops uio_sysfs_ops = {
109         .show = map_type_show,
110 };
111
112 static struct kobj_type map_attr_type = {
113         .release        = map_release,
114         .sysfs_ops      = &uio_sysfs_ops,
115         .default_attrs  = attrs,
116 };
117
118 static ssize_t show_name(struct device *dev,
119                          struct device_attribute *attr, char *buf)
120 {
121         struct uio_device *idev = dev_get_drvdata(dev);
122         if (idev)
123                 return sprintf(buf, "%s\n", idev->info->name);
124         else
125                 return -ENODEV;
126 }
127 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
128
129 static ssize_t show_version(struct device *dev,
130                             struct device_attribute *attr, char *buf)
131 {
132         struct uio_device *idev = dev_get_drvdata(dev);
133         if (idev)
134                 return sprintf(buf, "%s\n", idev->info->version);
135         else
136                 return -ENODEV;
137 }
138 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
139
140 static ssize_t show_event(struct device *dev,
141                           struct device_attribute *attr, char *buf)
142 {
143         struct uio_device *idev = dev_get_drvdata(dev);
144         if (idev)
145                 return sprintf(buf, "%u\n",
146                                 (unsigned int)atomic_read(&idev->event));
147         else
148                 return -ENODEV;
149 }
150 static DEVICE_ATTR(event, S_IRUGO, show_event, NULL);
151
152 static struct attribute *uio_attrs[] = {
153         &dev_attr_name.attr,
154         &dev_attr_version.attr,
155         &dev_attr_event.attr,
156         NULL,
157 };
158
159 static struct attribute_group uio_attr_grp = {
160         .attrs = uio_attrs,
161 };
162
163 /*
164  * device functions
165  */
166 static int uio_dev_add_attributes(struct uio_device *idev)
167 {
168         int ret;
169         int mi;
170         int map_found = 0;
171         struct uio_mem *mem;
172         struct uio_map *map;
173
174         ret = sysfs_create_group(&idev->dev->kobj, &uio_attr_grp);
175         if (ret)
176                 goto err_group;
177
178         for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
179                 mem = &idev->info->mem[mi];
180                 if (mem->size == 0)
181                         break;
182                 if (!map_found) {
183                         map_found = 1;
184                         idev->map_dir = kobject_create_and_add("maps",
185                                                         &idev->dev->kobj);
186                         if (!idev->map_dir)
187                                 goto err;
188                 }
189                 map = kzalloc(sizeof(*map), GFP_KERNEL);
190                 if (!map)
191                         goto err;
192                 kobject_init(&map->kobj, &map_attr_type);
193                 map->mem = mem;
194                 mem->map = map;
195                 ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi);
196                 if (ret)
197                         goto err;
198                 ret = kobject_uevent(&map->kobj, KOBJ_ADD);
199                 if (ret)
200                         goto err;
201         }
202
203         return 0;
204
205 err:
206         for (mi--; mi>=0; mi--) {
207                 mem = &idev->info->mem[mi];
208                 map = mem->map;
209                 kobject_put(&map->kobj);
210         }
211         kobject_put(idev->map_dir);
212         sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
213 err_group:
214         dev_err(idev->dev, "error creating sysfs files (%d)\n", ret);
215         return ret;
216 }
217
218 static void uio_dev_del_attributes(struct uio_device *idev)
219 {
220         int mi;
221         struct uio_mem *mem;
222         for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
223                 mem = &idev->info->mem[mi];
224                 if (mem->size == 0)
225                         break;
226                 kobject_put(&mem->map->kobj);
227         }
228         kobject_put(idev->map_dir);
229         sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
230 }
231
232 static int uio_get_minor(struct uio_device *idev)
233 {
234         static DEFINE_MUTEX(minor_lock);
235         int retval = -ENOMEM;
236         int id;
237
238         mutex_lock(&minor_lock);
239         if (idr_pre_get(&uio_idr, GFP_KERNEL) == 0)
240                 goto exit;
241
242         retval = idr_get_new(&uio_idr, idev, &id);
243         if (retval < 0) {
244                 if (retval == -EAGAIN)
245                         retval = -ENOMEM;
246                 goto exit;
247         }
248         idev->minor = id & MAX_ID_MASK;
249 exit:
250         mutex_unlock(&minor_lock);
251         return retval;
252 }
253
254 static void uio_free_minor(struct uio_device *idev)
255 {
256         idr_remove(&uio_idr, idev->minor);
257 }
258
259 /**
260  * uio_event_notify - trigger an interrupt event
261  * @info: UIO device capabilities
262  */
263 void uio_event_notify(struct uio_info *info)
264 {
265         struct uio_device *idev = info->uio_dev;
266
267         atomic_inc(&idev->event);
268         wake_up_interruptible(&idev->wait);
269         kill_fasync(&idev->async_queue, SIGIO, POLL_IN);
270 }
271 EXPORT_SYMBOL_GPL(uio_event_notify);
272
273 /**
274  * uio_interrupt - hardware interrupt handler
275  * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer
276  * @dev_id: Pointer to the devices uio_device structure
277  */
278 static irqreturn_t uio_interrupt(int irq, void *dev_id)
279 {
280         struct uio_device *idev = (struct uio_device *)dev_id;
281         irqreturn_t ret = idev->info->handler(irq, idev->info);
282
283         if (ret == IRQ_HANDLED)
284                 uio_event_notify(idev->info);
285
286         return ret;
287 }
288
289 struct uio_listener {
290         struct uio_device *dev;
291         s32 event_count;
292 };
293
294 static int uio_open(struct inode *inode, struct file *filep)
295 {
296         struct uio_device *idev;
297         struct uio_listener *listener;
298         int ret = 0;
299
300         idev = idr_find(&uio_idr, iminor(inode));
301         if (!idev)
302                 return -ENODEV;
303
304         listener = kmalloc(sizeof(*listener), GFP_KERNEL);
305         if (!listener)
306                 return -ENOMEM;
307
308         listener->dev = idev;
309         listener->event_count = atomic_read(&idev->event);
310         filep->private_data = listener;
311
312         if (idev->info->open) {
313                 if (!try_module_get(idev->owner))
314                         return -ENODEV;
315                 ret = idev->info->open(idev->info, inode);
316                 module_put(idev->owner);
317         }
318
319         if (ret)
320                 kfree(listener);
321
322         return ret;
323 }
324
325 static int uio_fasync(int fd, struct file *filep, int on)
326 {
327         struct uio_listener *listener = filep->private_data;
328         struct uio_device *idev = listener->dev;
329
330         return fasync_helper(fd, filep, on, &idev->async_queue);
331 }
332
333 static int uio_release(struct inode *inode, struct file *filep)
334 {
335         int ret = 0;
336         struct uio_listener *listener = filep->private_data;
337         struct uio_device *idev = listener->dev;
338
339         if (idev->info->release) {
340                 if (!try_module_get(idev->owner))
341                         return -ENODEV;
342                 ret = idev->info->release(idev->info, inode);
343                 module_put(idev->owner);
344         }
345         if (filep->f_flags & FASYNC)
346                 ret = uio_fasync(-1, filep, 0);
347         kfree(listener);
348         return ret;
349 }
350
351 static unsigned int uio_poll(struct file *filep, poll_table *wait)
352 {
353         struct uio_listener *listener = filep->private_data;
354         struct uio_device *idev = listener->dev;
355
356         if (idev->info->irq == UIO_IRQ_NONE)
357                 return -EIO;
358
359         poll_wait(filep, &idev->wait, wait);
360         if (listener->event_count != atomic_read(&idev->event))
361                 return POLLIN | POLLRDNORM;
362         return 0;
363 }
364
365 static ssize_t uio_read(struct file *filep, char __user *buf,
366                         size_t count, loff_t *ppos)
367 {
368         struct uio_listener *listener = filep->private_data;
369         struct uio_device *idev = listener->dev;
370         DECLARE_WAITQUEUE(wait, current);
371         ssize_t retval;
372         s32 event_count;
373
374         if (idev->info->irq == UIO_IRQ_NONE)
375                 return -EIO;
376
377         if (count != sizeof(s32))
378                 return -EINVAL;
379
380         add_wait_queue(&idev->wait, &wait);
381
382         do {
383                 set_current_state(TASK_INTERRUPTIBLE);
384
385                 event_count = atomic_read(&idev->event);
386                 if (event_count != listener->event_count) {
387                         if (copy_to_user(buf, &event_count, count))
388                                 retval = -EFAULT;
389                         else {
390                                 listener->event_count = event_count;
391                                 retval = count;
392                         }
393                         break;
394                 }
395
396                 if (filep->f_flags & O_NONBLOCK) {
397                         retval = -EAGAIN;
398                         break;
399                 }
400
401                 if (signal_pending(current)) {
402                         retval = -ERESTARTSYS;
403                         break;
404                 }
405                 schedule();
406         } while (1);
407
408         __set_current_state(TASK_RUNNING);
409         remove_wait_queue(&idev->wait, &wait);
410
411         return retval;
412 }
413
414 static int uio_find_mem_index(struct vm_area_struct *vma)
415 {
416         int mi;
417         struct uio_device *idev = vma->vm_private_data;
418
419         for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
420                 if (idev->info->mem[mi].size == 0)
421                         return -1;
422                 if (vma->vm_pgoff == mi)
423                         return mi;
424         }
425         return -1;
426 }
427
428 static void uio_vma_open(struct vm_area_struct *vma)
429 {
430         struct uio_device *idev = vma->vm_private_data;
431         idev->vma_count++;
432 }
433
434 static void uio_vma_close(struct vm_area_struct *vma)
435 {
436         struct uio_device *idev = vma->vm_private_data;
437         idev->vma_count--;
438 }
439
440 static int uio_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
441 {
442         struct uio_device *idev = vma->vm_private_data;
443         struct page *page;
444
445         int mi = uio_find_mem_index(vma);
446         if (mi < 0)
447                 return VM_FAULT_SIGBUS;
448
449         if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
450                 page = virt_to_page(idev->info->mem[mi].addr);
451         else
452                 page = vmalloc_to_page((void*)idev->info->mem[mi].addr);
453         get_page(page);
454         vmf->page = page;
455         return 0;
456 }
457
458 static struct vm_operations_struct uio_vm_ops = {
459         .open = uio_vma_open,
460         .close = uio_vma_close,
461         .fault = uio_vma_fault,
462 };
463
464 static int uio_mmap_physical(struct vm_area_struct *vma)
465 {
466         struct uio_device *idev = vma->vm_private_data;
467         int mi = uio_find_mem_index(vma);
468         if (mi < 0)
469                 return -EINVAL;
470
471         vma->vm_flags |= VM_IO | VM_RESERVED;
472
473         return remap_pfn_range(vma,
474                                vma->vm_start,
475                                idev->info->mem[mi].addr >> PAGE_SHIFT,
476                                vma->vm_end - vma->vm_start,
477                                vma->vm_page_prot);
478 }
479
480 static int uio_mmap_logical(struct vm_area_struct *vma)
481 {
482         vma->vm_flags |= VM_RESERVED;
483         vma->vm_ops = &uio_vm_ops;
484         uio_vma_open(vma);
485         return 0;
486 }
487
488 static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
489 {
490         struct uio_listener *listener = filep->private_data;
491         struct uio_device *idev = listener->dev;
492         int mi;
493         unsigned long requested_pages, actual_pages;
494         int ret = 0;
495
496         if (vma->vm_end < vma->vm_start)
497                 return -EINVAL;
498
499         vma->vm_private_data = idev;
500
501         mi = uio_find_mem_index(vma);
502         if (mi < 0)
503                 return -EINVAL;
504
505         requested_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
506         actual_pages = (idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
507         if (requested_pages > actual_pages)
508                 return -EINVAL;
509
510         if (idev->info->mmap) {
511                 if (!try_module_get(idev->owner))
512                         return -ENODEV;
513                 ret = idev->info->mmap(idev->info, vma);
514                 module_put(idev->owner);
515                 return ret;
516         }
517
518         switch (idev->info->mem[mi].memtype) {
519                 case UIO_MEM_PHYS:
520                         return uio_mmap_physical(vma);
521                 case UIO_MEM_LOGICAL:
522                 case UIO_MEM_VIRTUAL:
523                         return uio_mmap_logical(vma);
524                 default:
525                         return -EINVAL;
526         }
527 }
528
529 static const struct file_operations uio_fops = {
530         .owner          = THIS_MODULE,
531         .open           = uio_open,
532         .release        = uio_release,
533         .read           = uio_read,
534         .mmap           = uio_mmap,
535         .poll           = uio_poll,
536         .fasync         = uio_fasync,
537 };
538
539 static int uio_major_init(void)
540 {
541         uio_major = register_chrdev(0, "uio", &uio_fops);
542         if (uio_major < 0)
543                 return uio_major;
544         return 0;
545 }
546
547 static void uio_major_cleanup(void)
548 {
549         unregister_chrdev(uio_major, "uio");
550 }
551
552 static int init_uio_class(void)
553 {
554         int ret = 0;
555
556         if (uio_class != NULL) {
557                 kref_get(&uio_class->kref);
558                 goto exit;
559         }
560
561         /* This is the first time in here, set everything up properly */
562         ret = uio_major_init();
563         if (ret)
564                 goto exit;
565
566         uio_class = kzalloc(sizeof(*uio_class), GFP_KERNEL);
567         if (!uio_class) {
568                 ret = -ENOMEM;
569                 goto err_kzalloc;
570         }
571
572         kref_init(&uio_class->kref);
573         uio_class->class = class_create(THIS_MODULE, "uio");
574         if (IS_ERR(uio_class->class)) {
575                 ret = IS_ERR(uio_class->class);
576                 printk(KERN_ERR "class_create failed for uio\n");
577                 goto err_class_create;
578         }
579         return 0;
580
581 err_class_create:
582         kfree(uio_class);
583         uio_class = NULL;
584 err_kzalloc:
585         uio_major_cleanup();
586 exit:
587         return ret;
588 }
589
590 static void release_uio_class(struct kref *kref)
591 {
592         /* Ok, we cheat as we know we only have one uio_class */
593         class_destroy(uio_class->class);
594         kfree(uio_class);
595         uio_major_cleanup();
596         uio_class = NULL;
597 }
598
599 static void uio_class_destroy(void)
600 {
601         if (uio_class)
602                 kref_put(&uio_class->kref, release_uio_class);
603 }
604
605 /**
606  * uio_register_device - register a new userspace IO device
607  * @owner:      module that creates the new device
608  * @parent:     parent device
609  * @info:       UIO device capabilities
610  *
611  * returns zero on success or a negative error code.
612  */
613 int __uio_register_device(struct module *owner,
614                           struct device *parent,
615                           struct uio_info *info)
616 {
617         struct uio_device *idev;
618         int ret = 0;
619
620         if (!parent || !info || !info->name || !info->version)
621                 return -EINVAL;
622
623         info->uio_dev = NULL;
624
625         ret = init_uio_class();
626         if (ret)
627                 return ret;
628
629         idev = kzalloc(sizeof(*idev), GFP_KERNEL);
630         if (!idev) {
631                 ret = -ENOMEM;
632                 goto err_kzalloc;
633         }
634
635         idev->owner = owner;
636         idev->info = info;
637         init_waitqueue_head(&idev->wait);
638         atomic_set(&idev->event, 0);
639
640         ret = uio_get_minor(idev);
641         if (ret)
642                 goto err_get_minor;
643
644         idev->dev = device_create(uio_class->class, parent,
645                                   MKDEV(uio_major, idev->minor),
646                                   "uio%d", idev->minor);
647         if (IS_ERR(idev->dev)) {
648                 printk(KERN_ERR "UIO: device register failed\n");
649                 ret = PTR_ERR(idev->dev);
650                 goto err_device_create;
651         }
652         dev_set_drvdata(idev->dev, idev);
653
654         ret = uio_dev_add_attributes(idev);
655         if (ret)
656                 goto err_uio_dev_add_attributes;
657
658         info->uio_dev = idev;
659
660         if (idev->info->irq >= 0) {
661                 ret = request_irq(idev->info->irq, uio_interrupt,
662                                   idev->info->irq_flags, idev->info->name, idev);
663                 if (ret)
664                         goto err_request_irq;
665         }
666
667         return 0;
668
669 err_request_irq:
670         uio_dev_del_attributes(idev);
671 err_uio_dev_add_attributes:
672         device_destroy(uio_class->class, MKDEV(uio_major, idev->minor));
673 err_device_create:
674         uio_free_minor(idev);
675 err_get_minor:
676         kfree(idev);
677 err_kzalloc:
678         uio_class_destroy();
679         return ret;
680 }
681 EXPORT_SYMBOL_GPL(__uio_register_device);
682
683 /**
684  * uio_unregister_device - unregister a industrial IO device
685  * @info:       UIO device capabilities
686  *
687  */
688 void uio_unregister_device(struct uio_info *info)
689 {
690         struct uio_device *idev;
691
692         if (!info || !info->uio_dev)
693                 return;
694
695         idev = info->uio_dev;
696
697         uio_free_minor(idev);
698
699         if (info->irq >= 0)
700                 free_irq(info->irq, idev);
701
702         uio_dev_del_attributes(idev);
703
704         dev_set_drvdata(idev->dev, NULL);
705         device_destroy(uio_class->class, MKDEV(uio_major, idev->minor));
706         kfree(idev);
707         uio_class_destroy();
708
709         return;
710 }
711 EXPORT_SYMBOL_GPL(uio_unregister_device);
712
713 static int __init uio_init(void)
714 {
715         return 0;
716 }
717
718 static void __exit uio_exit(void)
719 {
720 }
721
722 module_init(uio_init)
723 module_exit(uio_exit)
724 MODULE_LICENSE("GPL v2");