blob: 1a0415e77a3053facfe5b5263babc0ad94d72c04 [file] [log] [blame]
Hans J. Kochbeafc542006-12-07 10:58:29 +01001/*
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
28struct 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;
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000037 struct kobject *map_dir;
Hans J. Kochbeafc542006-12-07 10:58:29 +010038};
39
40static int uio_major;
41static DEFINE_IDR(uio_idr);
Jan Engelhardt4f014692008-01-22 20:50:54 +010042static const struct file_operations uio_fops;
Hans J. Kochbeafc542006-12-07 10:58:29 +010043
44/* UIO class infrastructure */
45static struct uio_class {
46 struct kref kref;
47 struct class *class;
48} *uio_class;
49
50/*
51 * attributes
52 */
53
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000054struct uio_map {
55 struct kobject kobj;
56 struct uio_mem *mem;
Hans J. Kochbeafc542006-12-07 10:58:29 +010057};
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000058#define to_map(map) container_of(map, struct uio_map, kobj)
Hans J. Kochbeafc542006-12-07 10:58:29 +010059
Brandon Philips4f808bc2008-02-19 01:55:05 -080060static ssize_t map_addr_show(struct uio_mem *mem, char *buf)
Hans J. Kochbeafc542006-12-07 10:58:29 +010061{
Brandon Philips4f808bc2008-02-19 01:55:05 -080062 return sprintf(buf, "0x%lx\n", mem->addr);
Hans J. Kochbeafc542006-12-07 10:58:29 +010063}
64
Brandon Philips4f808bc2008-02-19 01:55:05 -080065static ssize_t map_size_show(struct uio_mem *mem, char *buf)
66{
67 return sprintf(buf, "0x%lx\n", mem->size);
68}
69
70struct 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
76static struct uio_sysfs_entry addr_attribute =
77 __ATTR(addr, S_IRUGO, map_addr_show, NULL);
78static struct uio_sysfs_entry size_attribute =
79 __ATTR(size, S_IRUGO, map_size_show, NULL);
Hans J. Kochbeafc542006-12-07 10:58:29 +010080
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000081static struct attribute *attrs[] = {
Brandon Philips4f808bc2008-02-19 01:55:05 -080082 &addr_attribute.attr,
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000083 &size_attribute.attr,
84 NULL, /* need to NULL terminate the list of attributes */
Hans J. Kochbeafc542006-12-07 10:58:29 +010085};
86
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000087static void map_release(struct kobject *kobj)
88{
89 struct uio_map *map = to_map(kobj);
90 kfree(map);
91}
92
Brandon Philips4f808bc2008-02-19 01:55:05 -080093static 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
108static struct sysfs_ops uio_sysfs_ops = {
109 .show = map_type_show,
110};
111
Hans J. Kochbeafc542006-12-07 10:58:29 +0100112static struct kobj_type map_attr_type = {
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000113 .release = map_release,
Brandon Philips4f808bc2008-02-19 01:55:05 -0800114 .sysfs_ops = &uio_sysfs_ops,
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000115 .default_attrs = attrs,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100116};
117
118static 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}
127static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
128
129static 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}
138static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
139
140static 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}
150static DEVICE_ATTR(event, S_IRUGO, show_event, NULL);
151
152static struct attribute *uio_attrs[] = {
153 &dev_attr_name.attr,
154 &dev_attr_version.attr,
155 &dev_attr_event.attr,
156 NULL,
157};
158
159static struct attribute_group uio_attr_grp = {
160 .attrs = uio_attrs,
161};
162
163/*
164 * device functions
165 */
166static 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;
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000172 struct uio_map *map;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100173
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;
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000184 idev->map_dir = kobject_create_and_add("maps",
185 &idev->dev->kobj);
186 if (!idev->map_dir)
187 goto err;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100188 }
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000189 map = kzalloc(sizeof(*map), GFP_KERNEL);
190 if (!map)
191 goto err;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700192 kobject_init(&map->kobj, &map_attr_type);
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000193 map->mem = mem;
194 mem->map = map;
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700195 ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100196 if (ret)
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000197 goto err;
198 ret = kobject_uevent(&map->kobj, KOBJ_ADD);
199 if (ret)
200 goto err;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100201 }
202
203 return 0;
204
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000205err:
Hans J. Kochbeafc542006-12-07 10:58:29 +0100206 for (mi--; mi>=0; mi--) {
207 mem = &idev->info->mem[mi];
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000208 map = mem->map;
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800209 kobject_put(&map->kobj);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100210 }
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800211 kobject_put(idev->map_dir);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100212 sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
213err_group:
214 dev_err(idev->dev, "error creating sysfs files (%d)\n", ret);
215 return ret;
216}
217
218static 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;
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800226 kobject_put(&mem->map->kobj);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100227 }
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800228 kobject_put(idev->map_dir);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100229 sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
230}
231
232static 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;
249exit:
250 mutex_unlock(&minor_lock);
251 return retval;
252}
253
254static 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 */
263void 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}
271EXPORT_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 */
278static 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
289struct uio_listener {
290 struct uio_device *dev;
291 s32 event_count;
292};
293
294static 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
Jonathan Corbetfbc8a812008-05-15 10:39:37 -0600300 lock_kernel();
Hans J. Kochbeafc542006-12-07 10:58:29 +0100301 idev = idr_find(&uio_idr, iminor(inode));
Jonathan Corbetfbc8a812008-05-15 10:39:37 -0600302 if (!idev) {
303 ret = -ENODEV;
304 goto out;
305 }
Hans J. Kochbeafc542006-12-07 10:58:29 +0100306
Jonathan Corbetfbc8a812008-05-15 10:39:37 -0600307 if (!try_module_get(idev->owner)) {
308 ret = -ENODEV;
309 goto out;
310 }
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200311
Hans J. Kochbeafc542006-12-07 10:58:29 +0100312 listener = kmalloc(sizeof(*listener), GFP_KERNEL);
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200313 if (!listener) {
314 ret = -ENOMEM;
315 goto err_alloc_listener;
316 }
Hans J. Kochbeafc542006-12-07 10:58:29 +0100317
318 listener->dev = idev;
319 listener->event_count = atomic_read(&idev->event);
320 filep->private_data = listener;
321
322 if (idev->info->open) {
Hans J. Kochbeafc542006-12-07 10:58:29 +0100323 ret = idev->info->open(idev->info, inode);
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200324 if (ret)
325 goto err_infoopen;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100326 }
Jonathan Corbetfbc8a812008-05-15 10:39:37 -0600327 unlock_kernel();
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200328 return 0;
329
330err_infoopen:
331
332 kfree(listener);
333err_alloc_listener:
334
335 module_put(idev->owner);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100336
Jonathan Corbetfbc8a812008-05-15 10:39:37 -0600337out:
338 unlock_kernel();
Hans J. Kochbeafc542006-12-07 10:58:29 +0100339 return ret;
340}
341
342static int uio_fasync(int fd, struct file *filep, int on)
343{
344 struct uio_listener *listener = filep->private_data;
345 struct uio_device *idev = listener->dev;
346
347 return fasync_helper(fd, filep, on, &idev->async_queue);
348}
349
350static int uio_release(struct inode *inode, struct file *filep)
351{
352 int ret = 0;
353 struct uio_listener *listener = filep->private_data;
354 struct uio_device *idev = listener->dev;
355
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200356 if (idev->info->release)
Hans J. Kochbeafc542006-12-07 10:58:29 +0100357 ret = idev->info->release(idev->info, inode);
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200358
359 module_put(idev->owner);
360
Hans J. Kochbeafc542006-12-07 10:58:29 +0100361 if (filep->f_flags & FASYNC)
362 ret = uio_fasync(-1, filep, 0);
363 kfree(listener);
364 return ret;
365}
366
367static unsigned int uio_poll(struct file *filep, poll_table *wait)
368{
369 struct uio_listener *listener = filep->private_data;
370 struct uio_device *idev = listener->dev;
371
372 if (idev->info->irq == UIO_IRQ_NONE)
373 return -EIO;
374
375 poll_wait(filep, &idev->wait, wait);
376 if (listener->event_count != atomic_read(&idev->event))
377 return POLLIN | POLLRDNORM;
378 return 0;
379}
380
381static ssize_t uio_read(struct file *filep, char __user *buf,
382 size_t count, loff_t *ppos)
383{
384 struct uio_listener *listener = filep->private_data;
385 struct uio_device *idev = listener->dev;
386 DECLARE_WAITQUEUE(wait, current);
387 ssize_t retval;
388 s32 event_count;
389
390 if (idev->info->irq == UIO_IRQ_NONE)
391 return -EIO;
392
393 if (count != sizeof(s32))
394 return -EINVAL;
395
396 add_wait_queue(&idev->wait, &wait);
397
398 do {
399 set_current_state(TASK_INTERRUPTIBLE);
400
401 event_count = atomic_read(&idev->event);
402 if (event_count != listener->event_count) {
403 if (copy_to_user(buf, &event_count, count))
404 retval = -EFAULT;
405 else {
406 listener->event_count = event_count;
407 retval = count;
408 }
409 break;
410 }
411
412 if (filep->f_flags & O_NONBLOCK) {
413 retval = -EAGAIN;
414 break;
415 }
416
417 if (signal_pending(current)) {
418 retval = -ERESTARTSYS;
419 break;
420 }
421 schedule();
422 } while (1);
423
424 __set_current_state(TASK_RUNNING);
425 remove_wait_queue(&idev->wait, &wait);
426
427 return retval;
428}
429
430static int uio_find_mem_index(struct vm_area_struct *vma)
431{
432 int mi;
433 struct uio_device *idev = vma->vm_private_data;
434
435 for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
436 if (idev->info->mem[mi].size == 0)
437 return -1;
438 if (vma->vm_pgoff == mi)
439 return mi;
440 }
441 return -1;
442}
443
444static void uio_vma_open(struct vm_area_struct *vma)
445{
446 struct uio_device *idev = vma->vm_private_data;
447 idev->vma_count++;
448}
449
450static void uio_vma_close(struct vm_area_struct *vma)
451{
452 struct uio_device *idev = vma->vm_private_data;
453 idev->vma_count--;
454}
455
Nick Piggina18b6302008-02-06 01:37:35 -0800456static int uio_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Hans J. Kochbeafc542006-12-07 10:58:29 +0100457{
458 struct uio_device *idev = vma->vm_private_data;
Nick Piggina18b6302008-02-06 01:37:35 -0800459 struct page *page;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100460
461 int mi = uio_find_mem_index(vma);
462 if (mi < 0)
Nick Piggina18b6302008-02-06 01:37:35 -0800463 return VM_FAULT_SIGBUS;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100464
465 if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
466 page = virt_to_page(idev->info->mem[mi].addr);
467 else
468 page = vmalloc_to_page((void*)idev->info->mem[mi].addr);
469 get_page(page);
Nick Piggina18b6302008-02-06 01:37:35 -0800470 vmf->page = page;
471 return 0;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100472}
473
474static struct vm_operations_struct uio_vm_ops = {
475 .open = uio_vma_open,
476 .close = uio_vma_close,
Nick Piggina18b6302008-02-06 01:37:35 -0800477 .fault = uio_vma_fault,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100478};
479
480static int uio_mmap_physical(struct vm_area_struct *vma)
481{
482 struct uio_device *idev = vma->vm_private_data;
483 int mi = uio_find_mem_index(vma);
484 if (mi < 0)
485 return -EINVAL;
486
487 vma->vm_flags |= VM_IO | VM_RESERVED;
488
Jean-Samuel Chenardc9698d62008-03-14 11:28:36 +0100489 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
490
Hans J. Kochbeafc542006-12-07 10:58:29 +0100491 return remap_pfn_range(vma,
492 vma->vm_start,
493 idev->info->mem[mi].addr >> PAGE_SHIFT,
494 vma->vm_end - vma->vm_start,
495 vma->vm_page_prot);
496}
497
498static int uio_mmap_logical(struct vm_area_struct *vma)
499{
500 vma->vm_flags |= VM_RESERVED;
501 vma->vm_ops = &uio_vm_ops;
502 uio_vma_open(vma);
503 return 0;
504}
505
506static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
507{
508 struct uio_listener *listener = filep->private_data;
509 struct uio_device *idev = listener->dev;
510 int mi;
511 unsigned long requested_pages, actual_pages;
512 int ret = 0;
513
514 if (vma->vm_end < vma->vm_start)
515 return -EINVAL;
516
517 vma->vm_private_data = idev;
518
519 mi = uio_find_mem_index(vma);
520 if (mi < 0)
521 return -EINVAL;
522
523 requested_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
524 actual_pages = (idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
525 if (requested_pages > actual_pages)
526 return -EINVAL;
527
528 if (idev->info->mmap) {
Hans J. Kochbeafc542006-12-07 10:58:29 +0100529 ret = idev->info->mmap(idev->info, vma);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100530 return ret;
531 }
532
533 switch (idev->info->mem[mi].memtype) {
534 case UIO_MEM_PHYS:
535 return uio_mmap_physical(vma);
536 case UIO_MEM_LOGICAL:
537 case UIO_MEM_VIRTUAL:
538 return uio_mmap_logical(vma);
539 default:
540 return -EINVAL;
541 }
542}
543
Jan Engelhardt4f014692008-01-22 20:50:54 +0100544static const struct file_operations uio_fops = {
Hans J. Kochbeafc542006-12-07 10:58:29 +0100545 .owner = THIS_MODULE,
546 .open = uio_open,
547 .release = uio_release,
548 .read = uio_read,
549 .mmap = uio_mmap,
550 .poll = uio_poll,
551 .fasync = uio_fasync,
552};
553
554static int uio_major_init(void)
555{
556 uio_major = register_chrdev(0, "uio", &uio_fops);
557 if (uio_major < 0)
558 return uio_major;
559 return 0;
560}
561
562static void uio_major_cleanup(void)
563{
564 unregister_chrdev(uio_major, "uio");
565}
566
567static int init_uio_class(void)
568{
569 int ret = 0;
570
571 if (uio_class != NULL) {
572 kref_get(&uio_class->kref);
573 goto exit;
574 }
575
576 /* This is the first time in here, set everything up properly */
577 ret = uio_major_init();
578 if (ret)
579 goto exit;
580
581 uio_class = kzalloc(sizeof(*uio_class), GFP_KERNEL);
582 if (!uio_class) {
583 ret = -ENOMEM;
584 goto err_kzalloc;
585 }
586
587 kref_init(&uio_class->kref);
588 uio_class->class = class_create(THIS_MODULE, "uio");
589 if (IS_ERR(uio_class->class)) {
590 ret = IS_ERR(uio_class->class);
591 printk(KERN_ERR "class_create failed for uio\n");
592 goto err_class_create;
593 }
594 return 0;
595
596err_class_create:
597 kfree(uio_class);
598 uio_class = NULL;
599err_kzalloc:
600 uio_major_cleanup();
601exit:
602 return ret;
603}
604
605static void release_uio_class(struct kref *kref)
606{
607 /* Ok, we cheat as we know we only have one uio_class */
608 class_destroy(uio_class->class);
609 kfree(uio_class);
610 uio_major_cleanup();
611 uio_class = NULL;
612}
613
614static void uio_class_destroy(void)
615{
616 if (uio_class)
617 kref_put(&uio_class->kref, release_uio_class);
618}
619
620/**
621 * uio_register_device - register a new userspace IO device
622 * @owner: module that creates the new device
623 * @parent: parent device
624 * @info: UIO device capabilities
625 *
626 * returns zero on success or a negative error code.
627 */
628int __uio_register_device(struct module *owner,
629 struct device *parent,
630 struct uio_info *info)
631{
632 struct uio_device *idev;
633 int ret = 0;
634
635 if (!parent || !info || !info->name || !info->version)
636 return -EINVAL;
637
638 info->uio_dev = NULL;
639
640 ret = init_uio_class();
641 if (ret)
642 return ret;
643
644 idev = kzalloc(sizeof(*idev), GFP_KERNEL);
645 if (!idev) {
646 ret = -ENOMEM;
647 goto err_kzalloc;
648 }
649
650 idev->owner = owner;
651 idev->info = info;
652 init_waitqueue_head(&idev->wait);
653 atomic_set(&idev->event, 0);
654
655 ret = uio_get_minor(idev);
656 if (ret)
657 goto err_get_minor;
658
659 idev->dev = device_create(uio_class->class, parent,
660 MKDEV(uio_major, idev->minor),
661 "uio%d", idev->minor);
662 if (IS_ERR(idev->dev)) {
663 printk(KERN_ERR "UIO: device register failed\n");
664 ret = PTR_ERR(idev->dev);
665 goto err_device_create;
666 }
667 dev_set_drvdata(idev->dev, idev);
668
669 ret = uio_dev_add_attributes(idev);
670 if (ret)
671 goto err_uio_dev_add_attributes;
672
673 info->uio_dev = idev;
674
675 if (idev->info->irq >= 0) {
676 ret = request_irq(idev->info->irq, uio_interrupt,
677 idev->info->irq_flags, idev->info->name, idev);
678 if (ret)
679 goto err_request_irq;
680 }
681
682 return 0;
683
684err_request_irq:
685 uio_dev_del_attributes(idev);
686err_uio_dev_add_attributes:
687 device_destroy(uio_class->class, MKDEV(uio_major, idev->minor));
688err_device_create:
689 uio_free_minor(idev);
690err_get_minor:
691 kfree(idev);
692err_kzalloc:
693 uio_class_destroy();
694 return ret;
695}
696EXPORT_SYMBOL_GPL(__uio_register_device);
697
698/**
699 * uio_unregister_device - unregister a industrial IO device
700 * @info: UIO device capabilities
701 *
702 */
703void uio_unregister_device(struct uio_info *info)
704{
705 struct uio_device *idev;
706
707 if (!info || !info->uio_dev)
708 return;
709
710 idev = info->uio_dev;
711
712 uio_free_minor(idev);
713
714 if (info->irq >= 0)
715 free_irq(info->irq, idev);
716
717 uio_dev_del_attributes(idev);
718
719 dev_set_drvdata(idev->dev, NULL);
720 device_destroy(uio_class->class, MKDEV(uio_major, idev->minor));
721 kfree(idev);
722 uio_class_destroy();
723
724 return;
725}
726EXPORT_SYMBOL_GPL(uio_unregister_device);
727
728static int __init uio_init(void)
729{
730 return 0;
731}
732
733static void __exit uio_exit(void)
734{
735}
736
737module_init(uio_init)
738module_exit(uio_exit)
739MODULE_LICENSE("GPL v2");