1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/poll.h>
18#include <linux/device.h>
19#include <linux/slab.h>
20#include <linux/mm.h>
21#include <linux/idr.h>
22#include <linux/sched/signal.h>
23#include <linux/string.h>
24#include <linux/kobject.h>
25#include <linux/cdev.h>
26#include <linux/uio_driver.h>
27#include <linux/list.h>
28#include <linux/mutex.h>
29
30#include <uapi/linux/uio/uio.h>
31
32#include "uio_dmabuf.h"
33
34#define UIO_MAX_DEVICES (1U << MINORBITS)
35
36static int uio_major;
37static struct cdev *uio_cdev;
38static DEFINE_IDR(uio_idr);
39static const struct file_operations uio_fops;
40
41
42static DEFINE_MUTEX(minor_lock);
43
44
45
46
47
48struct uio_map {
49 struct kobject kobj;
50 struct uio_mem *mem;
51};
52#define to_map(map) container_of(map, struct uio_map, kobj)
53
54static ssize_t map_name_show(struct uio_mem *mem, char *buf)
55{
56 if (unlikely(!mem->name))
57 mem->name = "";
58
59 return sprintf(buf, "%s\n", mem->name);
60}
61
62static ssize_t map_addr_show(struct uio_mem *mem, char *buf)
63{
64 return sprintf(buf, "%pa\n", &mem->addr);
65}
66
67static ssize_t map_size_show(struct uio_mem *mem, char *buf)
68{
69 return sprintf(buf, "%pa\n", &mem->size);
70}
71
72static ssize_t map_offset_show(struct uio_mem *mem, char *buf)
73{
74 return sprintf(buf, "0x%llx\n", (unsigned long long)mem->offs);
75}
76
77struct map_sysfs_entry {
78 struct attribute attr;
79 ssize_t (*show)(struct uio_mem *, char *);
80 ssize_t (*store)(struct uio_mem *, const char *, size_t);
81};
82
83static struct map_sysfs_entry name_attribute =
84 __ATTR(name, S_IRUGO, map_name_show, NULL);
85static struct map_sysfs_entry addr_attribute =
86 __ATTR(addr, S_IRUGO, map_addr_show, NULL);
87static struct map_sysfs_entry size_attribute =
88 __ATTR(size, S_IRUGO, map_size_show, NULL);
89static struct map_sysfs_entry offset_attribute =
90 __ATTR(offset, S_IRUGO, map_offset_show, NULL);
91
92static struct attribute *attrs[] = {
93 &name_attribute.attr,
94 &addr_attribute.attr,
95 &size_attribute.attr,
96 &offset_attribute.attr,
97 NULL,
98};
99
100static void map_release(struct kobject *kobj)
101{
102 struct uio_map *map = to_map(kobj);
103 kfree(map);
104}
105
106static ssize_t map_type_show(struct kobject *kobj, struct attribute *attr,
107 char *buf)
108{
109 struct uio_map *map = to_map(kobj);
110 struct uio_mem *mem = map->mem;
111 struct map_sysfs_entry *entry;
112
113 entry = container_of(attr, struct map_sysfs_entry, attr);
114
115 if (!entry->show)
116 return -EIO;
117
118 return entry->show(mem, buf);
119}
120
121static const struct sysfs_ops map_sysfs_ops = {
122 .show = map_type_show,
123};
124
125static struct kobj_type map_attr_type = {
126 .release = map_release,
127 .sysfs_ops = &map_sysfs_ops,
128 .default_attrs = attrs,
129};
130
131struct uio_portio {
132 struct kobject kobj;
133 struct uio_port *port;
134};
135#define to_portio(portio) container_of(portio, struct uio_portio, kobj)
136
137static ssize_t portio_name_show(struct uio_port *port, char *buf)
138{
139 if (unlikely(!port->name))
140 port->name = "";
141
142 return sprintf(buf, "%s\n", port->name);
143}
144
145static ssize_t portio_start_show(struct uio_port *port, char *buf)
146{
147 return sprintf(buf, "0x%lx\n", port->start);
148}
149
150static ssize_t portio_size_show(struct uio_port *port, char *buf)
151{
152 return sprintf(buf, "0x%lx\n", port->size);
153}
154
155static ssize_t portio_porttype_show(struct uio_port *port, char *buf)
156{
157 const char *porttypes[] = {"none", "x86", "gpio", "other"};
158
159 if ((port->porttype < 0) || (port->porttype > UIO_PORT_OTHER))
160 return -EINVAL;
161
162 return sprintf(buf, "port_%s\n", porttypes[port->porttype]);
163}
164
165struct portio_sysfs_entry {
166 struct attribute attr;
167 ssize_t (*show)(struct uio_port *, char *);
168 ssize_t (*store)(struct uio_port *, const char *, size_t);
169};
170
171static struct portio_sysfs_entry portio_name_attribute =
172 __ATTR(name, S_IRUGO, portio_name_show, NULL);
173static struct portio_sysfs_entry portio_start_attribute =
174 __ATTR(start, S_IRUGO, portio_start_show, NULL);
175static struct portio_sysfs_entry portio_size_attribute =
176 __ATTR(size, S_IRUGO, portio_size_show, NULL);
177static struct portio_sysfs_entry portio_porttype_attribute =
178 __ATTR(porttype, S_IRUGO, portio_porttype_show, NULL);
179
180static struct attribute *portio_attrs[] = {
181 &portio_name_attribute.attr,
182 &portio_start_attribute.attr,
183 &portio_size_attribute.attr,
184 &portio_porttype_attribute.attr,
185 NULL,
186};
187
188static void portio_release(struct kobject *kobj)
189{
190 struct uio_portio *portio = to_portio(kobj);
191 kfree(portio);
192}
193
194static ssize_t portio_type_show(struct kobject *kobj, struct attribute *attr,
195 char *buf)
196{
197 struct uio_portio *portio = to_portio(kobj);
198 struct uio_port *port = portio->port;
199 struct portio_sysfs_entry *entry;
200
201 entry = container_of(attr, struct portio_sysfs_entry, attr);
202
203 if (!entry->show)
204 return -EIO;
205
206 return entry->show(port, buf);
207}
208
209static const struct sysfs_ops portio_sysfs_ops = {
210 .show = portio_type_show,
211};
212
213static struct kobj_type portio_attr_type = {
214 .release = portio_release,
215 .sysfs_ops = &portio_sysfs_ops,
216 .default_attrs = portio_attrs,
217};
218
219static ssize_t name_show(struct device *dev,
220 struct device_attribute *attr, char *buf)
221{
222 struct uio_device *idev = dev_get_drvdata(dev);
223 int ret;
224
225 mutex_lock(&idev->info_lock);
226 if (!idev->info) {
227 ret = -EINVAL;
228 dev_err(dev, "the device has been unregistered\n");
229 goto out;
230 }
231
232 ret = sprintf(buf, "%s\n", idev->info->name);
233
234out:
235 mutex_unlock(&idev->info_lock);
236 return ret;
237}
238static DEVICE_ATTR_RO(name);
239
240static ssize_t version_show(struct device *dev,
241 struct device_attribute *attr, char *buf)
242{
243 struct uio_device *idev = dev_get_drvdata(dev);
244 int ret;
245
246 mutex_lock(&idev->info_lock);
247 if (!idev->info) {
248 ret = -EINVAL;
249 dev_err(dev, "the device has been unregistered\n");
250 goto out;
251 }
252
253 ret = sprintf(buf, "%s\n", idev->info->version);
254
255out:
256 mutex_unlock(&idev->info_lock);
257 return ret;
258}
259static DEVICE_ATTR_RO(version);
260
261static ssize_t event_show(struct device *dev,
262 struct device_attribute *attr, char *buf)
263{
264 struct uio_device *idev = dev_get_drvdata(dev);
265 return sprintf(buf, "%u\n", (unsigned int)atomic_read(&idev->event));
266}
267static DEVICE_ATTR_RO(event);
268
269static struct attribute *uio_attrs[] = {
270 &dev_attr_name.attr,
271 &dev_attr_version.attr,
272 &dev_attr_event.attr,
273 NULL,
274};
275ATTRIBUTE_GROUPS(uio);
276
277
278static struct class uio_class = {
279 .name = "uio",
280 .dev_groups = uio_groups,
281};
282
283static bool uio_class_registered;
284
285
286
287
288static int uio_dev_add_attributes(struct uio_device *idev)
289{
290 int ret;
291 int mi, pi;
292 int map_found = 0;
293 int portio_found = 0;
294 struct uio_mem *mem;
295 struct uio_map *map;
296 struct uio_port *port;
297 struct uio_portio *portio;
298
299 for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
300 mem = &idev->info->mem[mi];
301 if (mem->size == 0)
302 break;
303 if (!map_found) {
304 map_found = 1;
305 idev->map_dir = kobject_create_and_add("maps",
306 &idev->dev.kobj);
307 if (!idev->map_dir) {
308 ret = -ENOMEM;
309 goto err_map;
310 }
311 }
312 map = kzalloc(sizeof(*map), GFP_KERNEL);
313 if (!map) {
314 ret = -ENOMEM;
315 goto err_map;
316 }
317 kobject_init(&map->kobj, &map_attr_type);
318 map->mem = mem;
319 mem->map = map;
320 ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi);
321 if (ret)
322 goto err_map_kobj;
323 ret = kobject_uevent(&map->kobj, KOBJ_ADD);
324 if (ret)
325 goto err_map_kobj;
326 }
327
328 for (pi = 0; pi < MAX_UIO_PORT_REGIONS; pi++) {
329 port = &idev->info->port[pi];
330 if (port->size == 0)
331 break;
332 if (!portio_found) {
333 portio_found = 1;
334 idev->portio_dir = kobject_create_and_add("portio",
335 &idev->dev.kobj);
336 if (!idev->portio_dir) {
337 ret = -ENOMEM;
338 goto err_portio;
339 }
340 }
341 portio = kzalloc(sizeof(*portio), GFP_KERNEL);
342 if (!portio) {
343 ret = -ENOMEM;
344 goto err_portio;
345 }
346 kobject_init(&portio->kobj, &portio_attr_type);
347 portio->port = port;
348 port->portio = portio;
349 ret = kobject_add(&portio->kobj, idev->portio_dir,
350 "port%d", pi);
351 if (ret)
352 goto err_portio_kobj;
353 ret = kobject_uevent(&portio->kobj, KOBJ_ADD);
354 if (ret)
355 goto err_portio_kobj;
356 }
357
358 return 0;
359
360err_portio:
361 pi--;
362err_portio_kobj:
363 for (; pi >= 0; pi--) {
364 port = &idev->info->port[pi];
365 portio = port->portio;
366 kobject_put(&portio->kobj);
367 }
368 kobject_put(idev->portio_dir);
369err_map:
370 mi--;
371err_map_kobj:
372 for (; mi >= 0; mi--) {
373 mem = &idev->info->mem[mi];
374 map = mem->map;
375 kobject_put(&map->kobj);
376 }
377 kobject_put(idev->map_dir);
378 dev_err(&idev->dev, "error creating sysfs files (%d)\n", ret);
379 return ret;
380}
381
382static void uio_dev_del_attributes(struct uio_device *idev)
383{
384 int i;
385 struct uio_mem *mem;
386 struct uio_port *port;
387
388 for (i = 0; i < MAX_UIO_MAPS; i++) {
389 mem = &idev->info->mem[i];
390 if (mem->size == 0)
391 break;
392 kobject_put(&mem->map->kobj);
393 }
394 kobject_put(idev->map_dir);
395
396 for (i = 0; i < MAX_UIO_PORT_REGIONS; i++) {
397 port = &idev->info->port[i];
398 if (port->size == 0)
399 break;
400 kobject_put(&port->portio->kobj);
401 }
402 kobject_put(idev->portio_dir);
403}
404
405static int uio_get_minor(struct uio_device *idev)
406{
407 int retval = -ENOMEM;
408
409 mutex_lock(&minor_lock);
410 retval = idr_alloc(&uio_idr, idev, 0, UIO_MAX_DEVICES, GFP_KERNEL);
411 if (retval >= 0) {
412 idev->minor = retval;
413 retval = 0;
414 } else if (retval == -ENOSPC) {
415 dev_err(&idev->dev, "too many uio devices\n");
416 retval = -EINVAL;
417 }
418 mutex_unlock(&minor_lock);
419 return retval;
420}
421
422static void uio_free_minor(struct uio_device *idev)
423{
424 mutex_lock(&minor_lock);
425 idr_remove(&uio_idr, idev->minor);
426 mutex_unlock(&minor_lock);
427}
428
429
430
431
432
433void uio_event_notify(struct uio_info *info)
434{
435 struct uio_device *idev = info->uio_dev;
436
437 atomic_inc(&idev->event);
438 wake_up_interruptible(&idev->wait);
439 kill_fasync(&idev->async_queue, SIGIO, POLL_IN);
440}
441EXPORT_SYMBOL_GPL(uio_event_notify);
442
443
444
445
446
447
448static irqreturn_t uio_interrupt(int irq, void *dev_id)
449{
450 struct uio_device *idev = (struct uio_device *)dev_id;
451 irqreturn_t ret;
452
453 ret = idev->info->handler(irq, idev->info);
454 if (ret == IRQ_HANDLED)
455 uio_event_notify(idev->info);
456
457 return ret;
458}
459
460struct uio_listener {
461 struct uio_device *dev;
462 s32 event_count;
463 struct list_head dbufs;
464 struct mutex dbufs_lock;
465};
466
467static int uio_open(struct inode *inode, struct file *filep)
468{
469 struct uio_device *idev;
470 struct uio_listener *listener;
471 int ret = 0;
472
473 mutex_lock(&minor_lock);
474 idev = idr_find(&uio_idr, iminor(inode));
475 mutex_unlock(&minor_lock);
476 if (!idev) {
477 ret = -ENODEV;
478 goto out;
479 }
480
481 get_device(&idev->dev);
482
483 if (!try_module_get(idev->owner)) {
484 ret = -ENODEV;
485 goto err_module_get;
486 }
487
488 listener = kmalloc(sizeof(*listener), GFP_KERNEL);
489 if (!listener) {
490 ret = -ENOMEM;
491 goto err_alloc_listener;
492 }
493
494 listener->dev = idev;
495 listener->event_count = atomic_read(&idev->event);
496 filep->private_data = listener;
497
498 mutex_lock(&idev->info_lock);
499 if (!idev->info) {
500 mutex_unlock(&idev->info_lock);
501 ret = -EINVAL;
502 goto err_infoopen;
503 }
504
505 if (idev->info->open)
506 ret = idev->info->open(idev->info, inode);
507 mutex_unlock(&idev->info_lock);
508 if (ret)
509 goto err_infoopen;
510
511 INIT_LIST_HEAD(&listener->dbufs);
512 mutex_init(&listener->dbufs_lock);
513
514 return 0;
515
516err_infoopen:
517 kfree(listener);
518
519err_alloc_listener:
520 module_put(idev->owner);
521
522err_module_get:
523 put_device(&idev->dev);
524
525out:
526 return ret;
527}
528
529static int uio_fasync(int fd, struct file *filep, int on)
530{
531 struct uio_listener *listener = filep->private_data;
532 struct uio_device *idev = listener->dev;
533
534 return fasync_helper(fd, filep, on, &idev->async_queue);
535}
536
537static int uio_release(struct inode *inode, struct file *filep)
538{
539 int ret = 0;
540 struct uio_listener *listener = filep->private_data;
541 struct uio_device *idev = listener->dev;
542
543 ret = uio_dmabuf_cleanup(idev, &listener->dbufs, &listener->dbufs_lock);
544 if (ret)
545 dev_err(&idev->dev, "failed to clean up the dma bufs\n");
546
547 mutex_lock(&idev->info_lock);
548 if (idev->info && idev->info->release)
549 ret = idev->info->release(idev->info, inode);
550 mutex_unlock(&idev->info_lock);
551
552 module_put(idev->owner);
553 kfree(listener);
554 put_device(&idev->dev);
555 return ret;
556}
557
558static __poll_t uio_poll(struct file *filep, poll_table *wait)
559{
560 struct uio_listener *listener = filep->private_data;
561 struct uio_device *idev = listener->dev;
562 __poll_t ret = 0;
563
564 mutex_lock(&idev->info_lock);
565 if (!idev->info || !idev->info->irq)
566 ret = -EIO;
567 mutex_unlock(&idev->info_lock);
568
569 if (ret)
570 return ret;
571
572 poll_wait(filep, &idev->wait, wait);
573 if (listener->event_count != atomic_read(&idev->event))
574 return EPOLLIN | EPOLLRDNORM;
575 return 0;
576}
577
578static ssize_t uio_read(struct file *filep, char __user *buf,
579 size_t count, loff_t *ppos)
580{
581 struct uio_listener *listener = filep->private_data;
582 struct uio_device *idev = listener->dev;
583 DECLARE_WAITQUEUE(wait, current);
584 ssize_t retval = 0;
585 s32 event_count;
586
587 if (count != sizeof(s32))
588 return -EINVAL;
589
590 add_wait_queue(&idev->wait, &wait);
591
592 do {
593 mutex_lock(&idev->info_lock);
594 if (!idev->info || !idev->info->irq) {
595 retval = -EIO;
596 mutex_unlock(&idev->info_lock);
597 break;
598 }
599 mutex_unlock(&idev->info_lock);
600
601 set_current_state(TASK_INTERRUPTIBLE);
602
603 event_count = atomic_read(&idev->event);
604 if (event_count != listener->event_count) {
605 __set_current_state(TASK_RUNNING);
606 if (copy_to_user(buf, &event_count, count))
607 retval = -EFAULT;
608 else {
609 listener->event_count = event_count;
610 retval = count;
611 }
612 break;
613 }
614
615 if (filep->f_flags & O_NONBLOCK) {
616 retval = -EAGAIN;
617 break;
618 }
619
620 if (signal_pending(current)) {
621 retval = -ERESTARTSYS;
622 break;
623 }
624 schedule();
625 } while (1);
626
627 __set_current_state(TASK_RUNNING);
628 remove_wait_queue(&idev->wait, &wait);
629
630 return retval;
631}
632
633static ssize_t uio_write(struct file *filep, const char __user *buf,
634 size_t count, loff_t *ppos)
635{
636 struct uio_listener *listener = filep->private_data;
637 struct uio_device *idev = listener->dev;
638 ssize_t retval;
639 s32 irq_on;
640
641 if (count != sizeof(s32))
642 return -EINVAL;
643
644 if (copy_from_user(&irq_on, buf, count))
645 return -EFAULT;
646
647 mutex_lock(&idev->info_lock);
648 if (!idev->info) {
649 retval = -EINVAL;
650 goto out;
651 }
652
653 if (!idev->info->irq) {
654 retval = -EIO;
655 goto out;
656 }
657
658 if (!idev->info->irqcontrol) {
659 retval = -ENOSYS;
660 goto out;
661 }
662
663 retval = idev->info->irqcontrol(idev->info, irq_on);
664
665out:
666 mutex_unlock(&idev->info_lock);
667 return retval ? retval : sizeof(s32);
668}
669
670static long uio_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
671{
672 struct uio_listener *listener = filep->private_data;
673 struct uio_device *idev = listener->dev;
674 long ret;
675
676 if (!idev->info)
677 return -EIO;
678
679 switch (cmd) {
680 case UIO_IOC_MAP_DMABUF:
681 ret = uio_dmabuf_map(idev, &listener->dbufs,
682 &listener->dbufs_lock, (void __user *)arg);
683 break;
684 case UIO_IOC_UNMAP_DMABUF:
685 ret = uio_dmabuf_unmap(idev, &listener->dbufs,
686 &listener->dbufs_lock,
687 (void __user *)arg);
688 break;
689 default:
690 ret = -EINVAL;
691 break;
692 }
693
694 return ret;
695}
696
697static int uio_find_mem_index(struct vm_area_struct *vma)
698{
699 struct uio_device *idev = vma->vm_private_data;
700
701 if (vma->vm_pgoff < MAX_UIO_MAPS) {
702 if (idev->info->mem[vma->vm_pgoff].size == 0)
703 return -1;
704 return (int)vma->vm_pgoff;
705 }
706 return -1;
707}
708
709static vm_fault_t uio_vma_fault(struct vm_fault *vmf)
710{
711 struct uio_device *idev = vmf->vma->vm_private_data;
712 struct page *page;
713 unsigned long offset;
714 void *addr;
715 vm_fault_t ret = 0;
716 int mi;
717
718 mutex_lock(&idev->info_lock);
719 if (!idev->info) {
720 ret = VM_FAULT_SIGBUS;
721 goto out;
722 }
723
724 mi = uio_find_mem_index(vmf->vma);
725 if (mi < 0) {
726 ret = VM_FAULT_SIGBUS;
727 goto out;
728 }
729
730
731
732
733
734 offset = (vmf->pgoff - mi) << PAGE_SHIFT;
735
736 addr = (void *)(unsigned long)idev->info->mem[mi].addr + offset;
737 if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
738 page = virt_to_page(addr);
739 else
740 page = vmalloc_to_page(addr);
741 get_page(page);
742 vmf->page = page;
743
744out:
745 mutex_unlock(&idev->info_lock);
746
747 return ret;
748}
749
750static const struct vm_operations_struct uio_logical_vm_ops = {
751 .fault = uio_vma_fault,
752};
753
754static int uio_mmap_logical(struct vm_area_struct *vma)
755{
756 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
757 vma->vm_ops = &uio_logical_vm_ops;
758 return 0;
759}
760
761static const struct vm_operations_struct uio_physical_vm_ops = {
762#ifdef CONFIG_HAVE_IOREMAP_PROT
763 .access = generic_access_phys,
764#endif
765};
766
767static int uio_mmap_physical(struct vm_area_struct *vma)
768{
769 struct uio_device *idev = vma->vm_private_data;
770 int mi = uio_find_mem_index(vma);
771 struct uio_mem *mem;
772
773 if (mi < 0)
774 return -EINVAL;
775 mem = idev->info->mem + mi;
776
777 if (mem->addr & ~PAGE_MASK)
778 return -ENODEV;
779 if (vma->vm_end - vma->vm_start > mem->size)
780 return -EINVAL;
781
782 vma->vm_ops = &uio_physical_vm_ops;
783 if (idev->info->mem[mi].memtype == UIO_MEM_PHYS)
784 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
785
786
787
788
789
790
791
792
793
794
795 return remap_pfn_range(vma,
796 vma->vm_start,
797 mem->addr >> PAGE_SHIFT,
798 vma->vm_end - vma->vm_start,
799 vma->vm_page_prot);
800}
801
802static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
803{
804 struct uio_listener *listener = filep->private_data;
805 struct uio_device *idev = listener->dev;
806 int mi;
807 unsigned long requested_pages, actual_pages;
808 int ret = 0;
809
810 if (vma->vm_end < vma->vm_start)
811 return -EINVAL;
812
813 vma->vm_private_data = idev;
814
815 mutex_lock(&idev->info_lock);
816 if (!idev->info) {
817 ret = -EINVAL;
818 goto out;
819 }
820
821 mi = uio_find_mem_index(vma);
822 if (mi < 0) {
823 ret = -EINVAL;
824 goto out;
825 }
826
827 requested_pages = vma_pages(vma);
828 actual_pages = ((idev->info->mem[mi].addr & ~PAGE_MASK)
829 + idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
830 if (requested_pages > actual_pages) {
831 ret = -EINVAL;
832 goto out;
833 }
834
835 if (idev->info->mmap) {
836 ret = idev->info->mmap(idev->info, vma);
837 goto out;
838 }
839
840 switch (idev->info->mem[mi].memtype) {
841 case UIO_MEM_IOVA:
842 case UIO_MEM_PHYS:
843 ret = uio_mmap_physical(vma);
844 break;
845 case UIO_MEM_LOGICAL:
846 case UIO_MEM_VIRTUAL:
847 ret = uio_mmap_logical(vma);
848 break;
849 default:
850 ret = -EINVAL;
851 }
852
853 out:
854 mutex_unlock(&idev->info_lock);
855 return ret;
856}
857
858static const struct file_operations uio_fops = {
859 .owner = THIS_MODULE,
860 .open = uio_open,
861 .release = uio_release,
862 .read = uio_read,
863 .write = uio_write,
864 .mmap = uio_mmap,
865 .poll = uio_poll,
866 .unlocked_ioctl = uio_ioctl,
867 .fasync = uio_fasync,
868 .llseek = noop_llseek,
869};
870
871static int uio_major_init(void)
872{
873 static const char name[] = "uio";
874 struct cdev *cdev = NULL;
875 dev_t uio_dev = 0;
876 int result;
877
878 result = alloc_chrdev_region(&uio_dev, 0, UIO_MAX_DEVICES, name);
879 if (result)
880 goto out;
881
882 result = -ENOMEM;
883 cdev = cdev_alloc();
884 if (!cdev)
885 goto out_unregister;
886
887 cdev->owner = THIS_MODULE;
888 cdev->ops = &uio_fops;
889 kobject_set_name(&cdev->kobj, "%s", name);
890
891 result = cdev_add(cdev, uio_dev, UIO_MAX_DEVICES);
892 if (result)
893 goto out_put;
894
895 uio_major = MAJOR(uio_dev);
896 uio_cdev = cdev;
897 return 0;
898out_put:
899 kobject_put(&cdev->kobj);
900out_unregister:
901 unregister_chrdev_region(uio_dev, UIO_MAX_DEVICES);
902out:
903 return result;
904}
905
906static void uio_major_cleanup(void)
907{
908 unregister_chrdev_region(MKDEV(uio_major, 0), UIO_MAX_DEVICES);
909 cdev_del(uio_cdev);
910}
911
912static int init_uio_class(void)
913{
914 int ret;
915
916
917 ret = uio_major_init();
918 if (ret)
919 goto exit;
920
921 ret = class_register(&uio_class);
922 if (ret) {
923 printk(KERN_ERR "class_register failed for uio\n");
924 goto err_class_register;
925 }
926
927 uio_class_registered = true;
928
929 return 0;
930
931err_class_register:
932 uio_major_cleanup();
933exit:
934 return ret;
935}
936
937static void release_uio_class(void)
938{
939 uio_class_registered = false;
940 class_unregister(&uio_class);
941 uio_major_cleanup();
942}
943
944static void uio_device_release(struct device *dev)
945{
946 struct uio_device *idev = dev_get_drvdata(dev);
947
948 kfree(idev);
949}
950
951
952
953
954
955
956
957
958
959int __uio_register_device(struct module *owner,
960 struct device *parent,
961 struct uio_info *info)
962{
963 struct uio_device *idev;
964 int ret = 0;
965
966 if (!uio_class_registered)
967 return -EPROBE_DEFER;
968
969 if (!parent || !info || !info->name || !info->version)
970 return -EINVAL;
971
972 info->uio_dev = NULL;
973
974 idev = kzalloc(sizeof(*idev), GFP_KERNEL);
975 if (!idev) {
976 return -ENOMEM;
977 }
978
979 idev->owner = owner;
980 idev->info = info;
981 mutex_init(&idev->info_lock);
982 init_waitqueue_head(&idev->wait);
983 atomic_set(&idev->event, 0);
984
985 ret = uio_get_minor(idev);
986 if (ret) {
987 kfree(idev);
988 return ret;
989 }
990
991 device_initialize(&idev->dev);
992 idev->dev.devt = MKDEV(uio_major, idev->minor);
993 idev->dev.class = &uio_class;
994 idev->dev.parent = parent;
995 idev->dev.release = uio_device_release;
996 dev_set_drvdata(&idev->dev, idev);
997
998 ret = dev_set_name(&idev->dev, "uio%d", idev->minor);
999 if (ret)
1000 goto err_device_create;
1001
1002 ret = device_add(&idev->dev);
1003 if (ret)
1004 goto err_device_create;
1005
1006 ret = uio_dev_add_attributes(idev);
1007 if (ret)
1008 goto err_uio_dev_add_attributes;
1009
1010 info->uio_dev = idev;
1011
1012 if (info->irq && (info->irq != UIO_IRQ_CUSTOM)) {
1013
1014
1015
1016
1017
1018
1019
1020
1021 ret = request_irq(info->irq, uio_interrupt,
1022 info->irq_flags, info->name, idev);
1023 if (ret) {
1024 info->uio_dev = NULL;
1025 goto err_request_irq;
1026 }
1027 }
1028
1029 return 0;
1030
1031err_request_irq:
1032 uio_dev_del_attributes(idev);
1033err_uio_dev_add_attributes:
1034 device_del(&idev->dev);
1035err_device_create:
1036 uio_free_minor(idev);
1037 put_device(&idev->dev);
1038 return ret;
1039}
1040EXPORT_SYMBOL_GPL(__uio_register_device);
1041
1042
1043
1044
1045
1046
1047void uio_unregister_device(struct uio_info *info)
1048{
1049 struct uio_device *idev;
1050
1051 if (!info || !info->uio_dev)
1052 return;
1053
1054 idev = info->uio_dev;
1055
1056 uio_free_minor(idev);
1057
1058 mutex_lock(&idev->info_lock);
1059 uio_dev_del_attributes(idev);
1060
1061 if (info->irq && info->irq != UIO_IRQ_CUSTOM)
1062 free_irq(info->irq, idev);
1063
1064 idev->info = NULL;
1065 mutex_unlock(&idev->info_lock);
1066
1067 wake_up_interruptible(&idev->wait);
1068 kill_fasync(&idev->async_queue, SIGIO, POLL_HUP);
1069
1070 device_unregister(&idev->dev);
1071
1072 return;
1073}
1074EXPORT_SYMBOL_GPL(uio_unregister_device);
1075
1076static int __init uio_init(void)
1077{
1078 return init_uio_class();
1079}
1080
1081static void __exit uio_exit(void)
1082{
1083 release_uio_class();
1084 idr_destroy(&uio_idr);
1085}
1086
1087module_init(uio_init)
1088module_exit(uio_exit)
1089MODULE_LICENSE("GPL v2");
1090