blob: 4f28f4bf836603e5a78ec1ea8435cc0f63e8dc2c [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
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -060050/* Protect idr accesses */
51static DEFINE_MUTEX(minor_lock);
52
Hans J. Kochbeafc542006-12-07 10:58:29 +010053/*
54 * attributes
55 */
56
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000057struct uio_map {
58 struct kobject kobj;
59 struct uio_mem *mem;
Hans J. Kochbeafc542006-12-07 10:58:29 +010060};
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000061#define to_map(map) container_of(map, struct uio_map, kobj)
Hans J. Kochbeafc542006-12-07 10:58:29 +010062
Brandon Philips4f808bc2008-02-19 01:55:05 -080063static ssize_t map_addr_show(struct uio_mem *mem, char *buf)
Hans J. Kochbeafc542006-12-07 10:58:29 +010064{
Brandon Philips4f808bc2008-02-19 01:55:05 -080065 return sprintf(buf, "0x%lx\n", mem->addr);
Hans J. Kochbeafc542006-12-07 10:58:29 +010066}
67
Brandon Philips4f808bc2008-02-19 01:55:05 -080068static ssize_t map_size_show(struct uio_mem *mem, char *buf)
69{
70 return sprintf(buf, "0x%lx\n", mem->size);
71}
72
73struct uio_sysfs_entry {
74 struct attribute attr;
75 ssize_t (*show)(struct uio_mem *, char *);
76 ssize_t (*store)(struct uio_mem *, const char *, size_t);
77};
78
79static struct uio_sysfs_entry addr_attribute =
80 __ATTR(addr, S_IRUGO, map_addr_show, NULL);
81static struct uio_sysfs_entry size_attribute =
82 __ATTR(size, S_IRUGO, map_size_show, NULL);
Hans J. Kochbeafc542006-12-07 10:58:29 +010083
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000084static struct attribute *attrs[] = {
Brandon Philips4f808bc2008-02-19 01:55:05 -080085 &addr_attribute.attr,
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000086 &size_attribute.attr,
87 NULL, /* need to NULL terminate the list of attributes */
Hans J. Kochbeafc542006-12-07 10:58:29 +010088};
89
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000090static void map_release(struct kobject *kobj)
91{
92 struct uio_map *map = to_map(kobj);
93 kfree(map);
94}
95
Brandon Philips4f808bc2008-02-19 01:55:05 -080096static ssize_t map_type_show(struct kobject *kobj, struct attribute *attr,
97 char *buf)
98{
99 struct uio_map *map = to_map(kobj);
100 struct uio_mem *mem = map->mem;
101 struct uio_sysfs_entry *entry;
102
103 entry = container_of(attr, struct uio_sysfs_entry, attr);
104
105 if (!entry->show)
106 return -EIO;
107
108 return entry->show(mem, buf);
109}
110
111static struct sysfs_ops uio_sysfs_ops = {
112 .show = map_type_show,
113};
114
Hans J. Kochbeafc542006-12-07 10:58:29 +0100115static struct kobj_type map_attr_type = {
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000116 .release = map_release,
Brandon Philips4f808bc2008-02-19 01:55:05 -0800117 .sysfs_ops = &uio_sysfs_ops,
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000118 .default_attrs = attrs,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100119};
120
121static ssize_t show_name(struct device *dev,
122 struct device_attribute *attr, char *buf)
123{
124 struct uio_device *idev = dev_get_drvdata(dev);
125 if (idev)
126 return sprintf(buf, "%s\n", idev->info->name);
127 else
128 return -ENODEV;
129}
130static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
131
132static ssize_t show_version(struct device *dev,
133 struct device_attribute *attr, char *buf)
134{
135 struct uio_device *idev = dev_get_drvdata(dev);
136 if (idev)
137 return sprintf(buf, "%s\n", idev->info->version);
138 else
139 return -ENODEV;
140}
141static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
142
143static ssize_t show_event(struct device *dev,
144 struct device_attribute *attr, char *buf)
145{
146 struct uio_device *idev = dev_get_drvdata(dev);
147 if (idev)
148 return sprintf(buf, "%u\n",
149 (unsigned int)atomic_read(&idev->event));
150 else
151 return -ENODEV;
152}
153static DEVICE_ATTR(event, S_IRUGO, show_event, NULL);
154
155static struct attribute *uio_attrs[] = {
156 &dev_attr_name.attr,
157 &dev_attr_version.attr,
158 &dev_attr_event.attr,
159 NULL,
160};
161
162static struct attribute_group uio_attr_grp = {
163 .attrs = uio_attrs,
164};
165
166/*
167 * device functions
168 */
169static int uio_dev_add_attributes(struct uio_device *idev)
170{
171 int ret;
172 int mi;
173 int map_found = 0;
174 struct uio_mem *mem;
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000175 struct uio_map *map;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100176
177 ret = sysfs_create_group(&idev->dev->kobj, &uio_attr_grp);
178 if (ret)
179 goto err_group;
180
181 for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
182 mem = &idev->info->mem[mi];
183 if (mem->size == 0)
184 break;
185 if (!map_found) {
186 map_found = 1;
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000187 idev->map_dir = kobject_create_and_add("maps",
188 &idev->dev->kobj);
189 if (!idev->map_dir)
190 goto err;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100191 }
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000192 map = kzalloc(sizeof(*map), GFP_KERNEL);
193 if (!map)
194 goto err;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700195 kobject_init(&map->kobj, &map_attr_type);
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000196 map->mem = mem;
197 mem->map = map;
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700198 ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100199 if (ret)
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000200 goto err;
201 ret = kobject_uevent(&map->kobj, KOBJ_ADD);
202 if (ret)
203 goto err;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100204 }
205
206 return 0;
207
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000208err:
Hans J. Kochbeafc542006-12-07 10:58:29 +0100209 for (mi--; mi>=0; mi--) {
210 mem = &idev->info->mem[mi];
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000211 map = mem->map;
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800212 kobject_put(&map->kobj);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100213 }
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800214 kobject_put(idev->map_dir);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100215 sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
216err_group:
217 dev_err(idev->dev, "error creating sysfs files (%d)\n", ret);
218 return ret;
219}
220
221static void uio_dev_del_attributes(struct uio_device *idev)
222{
223 int mi;
224 struct uio_mem *mem;
225 for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
226 mem = &idev->info->mem[mi];
227 if (mem->size == 0)
228 break;
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800229 kobject_put(&mem->map->kobj);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100230 }
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800231 kobject_put(idev->map_dir);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100232 sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
233}
234
235static int uio_get_minor(struct uio_device *idev)
236{
Hans J. Kochbeafc542006-12-07 10:58:29 +0100237 int retval = -ENOMEM;
238 int id;
239
240 mutex_lock(&minor_lock);
241 if (idr_pre_get(&uio_idr, GFP_KERNEL) == 0)
242 goto exit;
243
244 retval = idr_get_new(&uio_idr, idev, &id);
245 if (retval < 0) {
246 if (retval == -EAGAIN)
247 retval = -ENOMEM;
248 goto exit;
249 }
250 idev->minor = id & MAX_ID_MASK;
251exit:
252 mutex_unlock(&minor_lock);
253 return retval;
254}
255
256static void uio_free_minor(struct uio_device *idev)
257{
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -0600258 mutex_lock(&minor_lock);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100259 idr_remove(&uio_idr, idev->minor);
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -0600260 mutex_unlock(&minor_lock);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100261}
262
263/**
264 * uio_event_notify - trigger an interrupt event
265 * @info: UIO device capabilities
266 */
267void uio_event_notify(struct uio_info *info)
268{
269 struct uio_device *idev = info->uio_dev;
270
271 atomic_inc(&idev->event);
272 wake_up_interruptible(&idev->wait);
273 kill_fasync(&idev->async_queue, SIGIO, POLL_IN);
274}
275EXPORT_SYMBOL_GPL(uio_event_notify);
276
277/**
278 * uio_interrupt - hardware interrupt handler
279 * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer
280 * @dev_id: Pointer to the devices uio_device structure
281 */
282static irqreturn_t uio_interrupt(int irq, void *dev_id)
283{
284 struct uio_device *idev = (struct uio_device *)dev_id;
285 irqreturn_t ret = idev->info->handler(irq, idev->info);
286
287 if (ret == IRQ_HANDLED)
288 uio_event_notify(idev->info);
289
290 return ret;
291}
292
293struct uio_listener {
294 struct uio_device *dev;
295 s32 event_count;
296};
297
298static int uio_open(struct inode *inode, struct file *filep)
299{
300 struct uio_device *idev;
301 struct uio_listener *listener;
302 int ret = 0;
303
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -0600304 mutex_lock(&minor_lock);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100305 idev = idr_find(&uio_idr, iminor(inode));
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -0600306 mutex_unlock(&minor_lock);
Jonathan Corbetfbc8a812008-05-15 10:39:37 -0600307 if (!idev) {
308 ret = -ENODEV;
309 goto out;
310 }
Hans J. Kochbeafc542006-12-07 10:58:29 +0100311
Jonathan Corbetfbc8a812008-05-15 10:39:37 -0600312 if (!try_module_get(idev->owner)) {
313 ret = -ENODEV;
314 goto out;
315 }
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200316
Hans J. Kochbeafc542006-12-07 10:58:29 +0100317 listener = kmalloc(sizeof(*listener), GFP_KERNEL);
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200318 if (!listener) {
319 ret = -ENOMEM;
320 goto err_alloc_listener;
321 }
Hans J. Kochbeafc542006-12-07 10:58:29 +0100322
323 listener->dev = idev;
324 listener->event_count = atomic_read(&idev->event);
325 filep->private_data = listener;
326
327 if (idev->info->open) {
Hans J. Kochbeafc542006-12-07 10:58:29 +0100328 ret = idev->info->open(idev->info, inode);
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200329 if (ret)
330 goto err_infoopen;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100331 }
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200332 return 0;
333
334err_infoopen:
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200335 kfree(listener);
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200336
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -0600337err_alloc_listener:
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200338 module_put(idev->owner);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100339
Jonathan Corbetfbc8a812008-05-15 10:39:37 -0600340out:
Hans J. Kochbeafc542006-12-07 10:58:29 +0100341 return ret;
342}
343
344static int uio_fasync(int fd, struct file *filep, int on)
345{
346 struct uio_listener *listener = filep->private_data;
347 struct uio_device *idev = listener->dev;
348
349 return fasync_helper(fd, filep, on, &idev->async_queue);
350}
351
352static int uio_release(struct inode *inode, struct file *filep)
353{
354 int ret = 0;
355 struct uio_listener *listener = filep->private_data;
356 struct uio_device *idev = listener->dev;
357
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200358 if (idev->info->release)
Hans J. Kochbeafc542006-12-07 10:58:29 +0100359 ret = idev->info->release(idev->info, inode);
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200360
361 module_put(idev->owner);
362
Hans J. Kochbeafc542006-12-07 10:58:29 +0100363 if (filep->f_flags & FASYNC)
364 ret = uio_fasync(-1, filep, 0);
365 kfree(listener);
366 return ret;
367}
368
369static unsigned int uio_poll(struct file *filep, poll_table *wait)
370{
371 struct uio_listener *listener = filep->private_data;
372 struct uio_device *idev = listener->dev;
373
374 if (idev->info->irq == UIO_IRQ_NONE)
375 return -EIO;
376
377 poll_wait(filep, &idev->wait, wait);
378 if (listener->event_count != atomic_read(&idev->event))
379 return POLLIN | POLLRDNORM;
380 return 0;
381}
382
383static ssize_t uio_read(struct file *filep, char __user *buf,
384 size_t count, loff_t *ppos)
385{
386 struct uio_listener *listener = filep->private_data;
387 struct uio_device *idev = listener->dev;
388 DECLARE_WAITQUEUE(wait, current);
389 ssize_t retval;
390 s32 event_count;
391
392 if (idev->info->irq == UIO_IRQ_NONE)
393 return -EIO;
394
395 if (count != sizeof(s32))
396 return -EINVAL;
397
398 add_wait_queue(&idev->wait, &wait);
399
400 do {
401 set_current_state(TASK_INTERRUPTIBLE);
402
403 event_count = atomic_read(&idev->event);
404 if (event_count != listener->event_count) {
405 if (copy_to_user(buf, &event_count, count))
406 retval = -EFAULT;
407 else {
408 listener->event_count = event_count;
409 retval = count;
410 }
411 break;
412 }
413
414 if (filep->f_flags & O_NONBLOCK) {
415 retval = -EAGAIN;
416 break;
417 }
418
419 if (signal_pending(current)) {
420 retval = -ERESTARTSYS;
421 break;
422 }
423 schedule();
424 } while (1);
425
426 __set_current_state(TASK_RUNNING);
427 remove_wait_queue(&idev->wait, &wait);
428
429 return retval;
430}
431
Hans J. Koch328a14e2008-05-23 13:50:14 +0200432static ssize_t uio_write(struct file *filep, const char __user *buf,
433 size_t count, loff_t *ppos)
434{
435 struct uio_listener *listener = filep->private_data;
436 struct uio_device *idev = listener->dev;
437 ssize_t retval;
438 s32 irq_on;
439
440 if (idev->info->irq == UIO_IRQ_NONE)
441 return -EIO;
442
443 if (count != sizeof(s32))
444 return -EINVAL;
445
446 if (!idev->info->irqcontrol)
447 return -ENOSYS;
448
449 if (copy_from_user(&irq_on, buf, count))
450 return -EFAULT;
451
452 retval = idev->info->irqcontrol(idev->info, irq_on);
453
454 return retval ? retval : sizeof(s32);
455}
456
Hans J. Kochbeafc542006-12-07 10:58:29 +0100457static int uio_find_mem_index(struct vm_area_struct *vma)
458{
459 int mi;
460 struct uio_device *idev = vma->vm_private_data;
461
462 for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
463 if (idev->info->mem[mi].size == 0)
464 return -1;
465 if (vma->vm_pgoff == mi)
466 return mi;
467 }
468 return -1;
469}
470
471static void uio_vma_open(struct vm_area_struct *vma)
472{
473 struct uio_device *idev = vma->vm_private_data;
474 idev->vma_count++;
475}
476
477static void uio_vma_close(struct vm_area_struct *vma)
478{
479 struct uio_device *idev = vma->vm_private_data;
480 idev->vma_count--;
481}
482
Nick Piggina18b6302008-02-06 01:37:35 -0800483static int uio_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Hans J. Kochbeafc542006-12-07 10:58:29 +0100484{
485 struct uio_device *idev = vma->vm_private_data;
Nick Piggina18b6302008-02-06 01:37:35 -0800486 struct page *page;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100487
488 int mi = uio_find_mem_index(vma);
489 if (mi < 0)
Nick Piggina18b6302008-02-06 01:37:35 -0800490 return VM_FAULT_SIGBUS;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100491
492 if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
493 page = virt_to_page(idev->info->mem[mi].addr);
494 else
495 page = vmalloc_to_page((void*)idev->info->mem[mi].addr);
496 get_page(page);
Nick Piggina18b6302008-02-06 01:37:35 -0800497 vmf->page = page;
498 return 0;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100499}
500
501static struct vm_operations_struct uio_vm_ops = {
502 .open = uio_vma_open,
503 .close = uio_vma_close,
Nick Piggina18b6302008-02-06 01:37:35 -0800504 .fault = uio_vma_fault,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100505};
506
507static int uio_mmap_physical(struct vm_area_struct *vma)
508{
509 struct uio_device *idev = vma->vm_private_data;
510 int mi = uio_find_mem_index(vma);
511 if (mi < 0)
512 return -EINVAL;
513
514 vma->vm_flags |= VM_IO | VM_RESERVED;
515
Jean-Samuel Chenardc9698d6b2008-03-14 11:28:36 +0100516 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
517
Hans J. Kochbeafc542006-12-07 10:58:29 +0100518 return remap_pfn_range(vma,
519 vma->vm_start,
520 idev->info->mem[mi].addr >> PAGE_SHIFT,
521 vma->vm_end - vma->vm_start,
522 vma->vm_page_prot);
523}
524
525static int uio_mmap_logical(struct vm_area_struct *vma)
526{
527 vma->vm_flags |= VM_RESERVED;
528 vma->vm_ops = &uio_vm_ops;
529 uio_vma_open(vma);
530 return 0;
531}
532
533static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
534{
535 struct uio_listener *listener = filep->private_data;
536 struct uio_device *idev = listener->dev;
537 int mi;
538 unsigned long requested_pages, actual_pages;
539 int ret = 0;
540
541 if (vma->vm_end < vma->vm_start)
542 return -EINVAL;
543
544 vma->vm_private_data = idev;
545
546 mi = uio_find_mem_index(vma);
547 if (mi < 0)
548 return -EINVAL;
549
550 requested_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
551 actual_pages = (idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
552 if (requested_pages > actual_pages)
553 return -EINVAL;
554
555 if (idev->info->mmap) {
Hans J. Kochbeafc542006-12-07 10:58:29 +0100556 ret = idev->info->mmap(idev->info, vma);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100557 return ret;
558 }
559
560 switch (idev->info->mem[mi].memtype) {
561 case UIO_MEM_PHYS:
562 return uio_mmap_physical(vma);
563 case UIO_MEM_LOGICAL:
564 case UIO_MEM_VIRTUAL:
565 return uio_mmap_logical(vma);
566 default:
567 return -EINVAL;
568 }
569}
570
Jan Engelhardt4f014692008-01-22 20:50:54 +0100571static const struct file_operations uio_fops = {
Hans J. Kochbeafc542006-12-07 10:58:29 +0100572 .owner = THIS_MODULE,
573 .open = uio_open,
574 .release = uio_release,
575 .read = uio_read,
Hans J. Koch328a14e2008-05-23 13:50:14 +0200576 .write = uio_write,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100577 .mmap = uio_mmap,
578 .poll = uio_poll,
579 .fasync = uio_fasync,
580};
581
582static int uio_major_init(void)
583{
584 uio_major = register_chrdev(0, "uio", &uio_fops);
585 if (uio_major < 0)
586 return uio_major;
587 return 0;
588}
589
590static void uio_major_cleanup(void)
591{
592 unregister_chrdev(uio_major, "uio");
593}
594
595static int init_uio_class(void)
596{
597 int ret = 0;
598
599 if (uio_class != NULL) {
600 kref_get(&uio_class->kref);
601 goto exit;
602 }
603
604 /* This is the first time in here, set everything up properly */
605 ret = uio_major_init();
606 if (ret)
607 goto exit;
608
609 uio_class = kzalloc(sizeof(*uio_class), GFP_KERNEL);
610 if (!uio_class) {
611 ret = -ENOMEM;
612 goto err_kzalloc;
613 }
614
615 kref_init(&uio_class->kref);
616 uio_class->class = class_create(THIS_MODULE, "uio");
617 if (IS_ERR(uio_class->class)) {
618 ret = IS_ERR(uio_class->class);
619 printk(KERN_ERR "class_create failed for uio\n");
620 goto err_class_create;
621 }
622 return 0;
623
624err_class_create:
625 kfree(uio_class);
626 uio_class = NULL;
627err_kzalloc:
628 uio_major_cleanup();
629exit:
630 return ret;
631}
632
633static void release_uio_class(struct kref *kref)
634{
635 /* Ok, we cheat as we know we only have one uio_class */
636 class_destroy(uio_class->class);
637 kfree(uio_class);
638 uio_major_cleanup();
639 uio_class = NULL;
640}
641
642static void uio_class_destroy(void)
643{
644 if (uio_class)
645 kref_put(&uio_class->kref, release_uio_class);
646}
647
648/**
649 * uio_register_device - register a new userspace IO device
650 * @owner: module that creates the new device
651 * @parent: parent device
652 * @info: UIO device capabilities
653 *
654 * returns zero on success or a negative error code.
655 */
656int __uio_register_device(struct module *owner,
657 struct device *parent,
658 struct uio_info *info)
659{
660 struct uio_device *idev;
661 int ret = 0;
662
663 if (!parent || !info || !info->name || !info->version)
664 return -EINVAL;
665
666 info->uio_dev = NULL;
667
668 ret = init_uio_class();
669 if (ret)
670 return ret;
671
672 idev = kzalloc(sizeof(*idev), GFP_KERNEL);
673 if (!idev) {
674 ret = -ENOMEM;
675 goto err_kzalloc;
676 }
677
678 idev->owner = owner;
679 idev->info = info;
680 init_waitqueue_head(&idev->wait);
681 atomic_set(&idev->event, 0);
682
683 ret = uio_get_minor(idev);
684 if (ret)
685 goto err_get_minor;
686
Greg Kroah-Hartman43691da2008-05-16 17:55:12 -0700687 idev->dev = device_create_drvdata(uio_class->class, parent,
688 MKDEV(uio_major, idev->minor), idev,
689 "uio%d", idev->minor);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100690 if (IS_ERR(idev->dev)) {
691 printk(KERN_ERR "UIO: device register failed\n");
692 ret = PTR_ERR(idev->dev);
693 goto err_device_create;
694 }
Hans J. Kochbeafc542006-12-07 10:58:29 +0100695
696 ret = uio_dev_add_attributes(idev);
697 if (ret)
698 goto err_uio_dev_add_attributes;
699
700 info->uio_dev = idev;
701
702 if (idev->info->irq >= 0) {
703 ret = request_irq(idev->info->irq, uio_interrupt,
704 idev->info->irq_flags, idev->info->name, idev);
705 if (ret)
706 goto err_request_irq;
707 }
708
709 return 0;
710
711err_request_irq:
712 uio_dev_del_attributes(idev);
713err_uio_dev_add_attributes:
714 device_destroy(uio_class->class, MKDEV(uio_major, idev->minor));
715err_device_create:
716 uio_free_minor(idev);
717err_get_minor:
718 kfree(idev);
719err_kzalloc:
720 uio_class_destroy();
721 return ret;
722}
723EXPORT_SYMBOL_GPL(__uio_register_device);
724
725/**
726 * uio_unregister_device - unregister a industrial IO device
727 * @info: UIO device capabilities
728 *
729 */
730void uio_unregister_device(struct uio_info *info)
731{
732 struct uio_device *idev;
733
734 if (!info || !info->uio_dev)
735 return;
736
737 idev = info->uio_dev;
738
739 uio_free_minor(idev);
740
741 if (info->irq >= 0)
742 free_irq(info->irq, idev);
743
744 uio_dev_del_attributes(idev);
745
746 dev_set_drvdata(idev->dev, NULL);
747 device_destroy(uio_class->class, MKDEV(uio_major, idev->minor));
748 kfree(idev);
749 uio_class_destroy();
750
751 return;
752}
753EXPORT_SYMBOL_GPL(uio_unregister_device);
754
755static int __init uio_init(void)
756{
757 return 0;
758}
759
760static void __exit uio_exit(void)
761{
762}
763
764module_init(uio_init)
765module_exit(uio_exit)
766MODULE_LICENSE("GPL v2");