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