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 | |
4f808bcd | 60 | static ssize_t map_addr_show(struct uio_mem *mem, char *buf) |
beafc54c | 61 | { |
4f808bcd BP |
62 | return sprintf(buf, "0x%lx\n", mem->addr); |
63 | } | |
beafc54c | 64 | |
4f808bcd BP |
65 | static ssize_t map_size_show(struct uio_mem *mem, char *buf) |
66 | { | |
67 | return sprintf(buf, "0x%lx\n", mem->size); | |
beafc54c HK |
68 | } |
69 | ||
4f808bcd BP |
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); | |
beafc54c | 80 | |
81e7c6a6 | 81 | static struct attribute *attrs[] = { |
4f808bcd | 82 | &addr_attribute.attr, |
81e7c6a6 GKH |
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 | ||
4f808bcd BP |
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 | ||
beafc54c | 112 | static struct kobj_type map_attr_type = { |
81e7c6a6 | 113 | .release = map_release, |
4f808bcd | 114 | .sysfs_ops = &uio_sysfs_ops, |
81e7c6a6 | 115 | .default_attrs = attrs, |
beafc54c HK |
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; | |
81e7c6a6 | 172 | struct uio_map *map; |
beafc54c HK |
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; | |
81e7c6a6 GKH |
184 | idev->map_dir = kobject_create_and_add("maps", |
185 | &idev->dev->kobj); | |
186 | if (!idev->map_dir) | |
187 | goto err; | |
beafc54c | 188 | } |
81e7c6a6 GKH |
189 | map = kzalloc(sizeof(*map), GFP_KERNEL); |
190 | if (!map) | |
191 | goto err; | |
f9cb074b | 192 | kobject_init(&map->kobj, &map_attr_type); |
81e7c6a6 GKH |
193 | map->mem = mem; |
194 | mem->map = map; | |
b2d6db58 | 195 | ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi); |
81e7c6a6 GKH |
196 | if (ret) |
197 | goto err; | |
198 | ret = kobject_uevent(&map->kobj, KOBJ_ADD); | |
beafc54c | 199 | if (ret) |
81e7c6a6 | 200 | goto err; |
beafc54c HK |
201 | } |
202 | ||
203 | return 0; | |
204 | ||
81e7c6a6 | 205 | err: |
beafc54c HK |
206 | for (mi--; mi>=0; mi--) { |
207 | mem = &idev->info->mem[mi]; | |
81e7c6a6 | 208 | map = mem->map; |
c10997f6 | 209 | kobject_put(&map->kobj); |
beafc54c | 210 | } |
c10997f6 | 211 | kobject_put(idev->map_dir); |
beafc54c HK |
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; | |
c10997f6 | 226 | kobject_put(&mem->map->kobj); |
beafc54c | 227 | } |
c10997f6 | 228 | kobject_put(idev->map_dir); |
beafc54c HK |
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 | ||
fbc8a81d | 300 | lock_kernel(); |
beafc54c | 301 | idev = idr_find(&uio_idr, iminor(inode)); |
fbc8a81d JC |
302 | if (!idev) { |
303 | ret = -ENODEV; | |
304 | goto out; | |
305 | } | |
beafc54c | 306 | |
fbc8a81d JC |
307 | if (!try_module_get(idev->owner)) { |
308 | ret = -ENODEV; | |
309 | goto out; | |
310 | } | |
610ad506 | 311 | |
beafc54c | 312 | listener = kmalloc(sizeof(*listener), GFP_KERNEL); |
610ad506 UKK |
313 | if (!listener) { |
314 | ret = -ENOMEM; | |
315 | goto err_alloc_listener; | |
316 | } | |
beafc54c HK |
317 | |
318 | listener->dev = idev; | |
319 | listener->event_count = atomic_read(&idev->event); | |
320 | filep->private_data = listener; | |
321 | ||
322 | if (idev->info->open) { | |
beafc54c | 323 | ret = idev->info->open(idev->info, inode); |
610ad506 UKK |
324 | if (ret) |
325 | goto err_infoopen; | |
beafc54c | 326 | } |
fbc8a81d | 327 | unlock_kernel(); |
610ad506 UKK |
328 | return 0; |
329 | ||
330 | err_infoopen: | |
331 | ||
332 | kfree(listener); | |
333 | err_alloc_listener: | |
334 | ||
335 | module_put(idev->owner); | |
beafc54c | 336 | |
fbc8a81d JC |
337 | out: |
338 | unlock_kernel(); | |
beafc54c HK |
339 | return ret; |
340 | } | |
341 | ||
342 | static 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 | ||
350 | static 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 | ||
610ad506 | 356 | if (idev->info->release) |
beafc54c | 357 | ret = idev->info->release(idev->info, inode); |
610ad506 UKK |
358 | |
359 | module_put(idev->owner); | |
360 | ||
beafc54c HK |
361 | if (filep->f_flags & FASYNC) |
362 | ret = uio_fasync(-1, filep, 0); | |
363 | kfree(listener); | |
364 | return ret; | |
365 | } | |
366 | ||
367 | static 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 | ||
381 | static 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 | ||
328a14e7 HK |
430 | static ssize_t uio_write(struct file *filep, const char __user *buf, |
431 | size_t count, loff_t *ppos) | |
432 | { | |
433 | struct uio_listener *listener = filep->private_data; | |
434 | struct uio_device *idev = listener->dev; | |
435 | ssize_t retval; | |
436 | s32 irq_on; | |
437 | ||
438 | if (idev->info->irq == UIO_IRQ_NONE) | |
439 | return -EIO; | |
440 | ||
441 | if (count != sizeof(s32)) | |
442 | return -EINVAL; | |
443 | ||
444 | if (!idev->info->irqcontrol) | |
445 | return -ENOSYS; | |
446 | ||
447 | if (copy_from_user(&irq_on, buf, count)) | |
448 | return -EFAULT; | |
449 | ||
450 | retval = idev->info->irqcontrol(idev->info, irq_on); | |
451 | ||
452 | return retval ? retval : sizeof(s32); | |
453 | } | |
454 | ||
beafc54c HK |
455 | static int uio_find_mem_index(struct vm_area_struct *vma) |
456 | { | |
457 | int mi; | |
458 | struct uio_device *idev = vma->vm_private_data; | |
459 | ||
460 | for (mi = 0; mi < MAX_UIO_MAPS; mi++) { | |
461 | if (idev->info->mem[mi].size == 0) | |
462 | return -1; | |
463 | if (vma->vm_pgoff == mi) | |
464 | return mi; | |
465 | } | |
466 | return -1; | |
467 | } | |
468 | ||
469 | static void uio_vma_open(struct vm_area_struct *vma) | |
470 | { | |
471 | struct uio_device *idev = vma->vm_private_data; | |
472 | idev->vma_count++; | |
473 | } | |
474 | ||
475 | static void uio_vma_close(struct vm_area_struct *vma) | |
476 | { | |
477 | struct uio_device *idev = vma->vm_private_data; | |
478 | idev->vma_count--; | |
479 | } | |
480 | ||
a18b630d | 481 | static int uio_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
beafc54c HK |
482 | { |
483 | struct uio_device *idev = vma->vm_private_data; | |
a18b630d | 484 | struct page *page; |
beafc54c HK |
485 | |
486 | int mi = uio_find_mem_index(vma); | |
487 | if (mi < 0) | |
a18b630d | 488 | return VM_FAULT_SIGBUS; |
beafc54c HK |
489 | |
490 | if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL) | |
491 | page = virt_to_page(idev->info->mem[mi].addr); | |
492 | else | |
493 | page = vmalloc_to_page((void*)idev->info->mem[mi].addr); | |
494 | get_page(page); | |
a18b630d NP |
495 | vmf->page = page; |
496 | return 0; | |
beafc54c HK |
497 | } |
498 | ||
499 | static struct vm_operations_struct uio_vm_ops = { | |
500 | .open = uio_vma_open, | |
501 | .close = uio_vma_close, | |
a18b630d | 502 | .fault = uio_vma_fault, |
beafc54c HK |
503 | }; |
504 | ||
505 | static int uio_mmap_physical(struct vm_area_struct *vma) | |
506 | { | |
507 | struct uio_device *idev = vma->vm_private_data; | |
508 | int mi = uio_find_mem_index(vma); | |
509 | if (mi < 0) | |
510 | return -EINVAL; | |
511 | ||
512 | vma->vm_flags |= VM_IO | VM_RESERVED; | |
c9698d6b JSC |
513 | |
514 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); | |
beafc54c HK |
515 | |
516 | return remap_pfn_range(vma, | |
517 | vma->vm_start, | |
518 | idev->info->mem[mi].addr >> PAGE_SHIFT, | |
519 | vma->vm_end - vma->vm_start, | |
520 | vma->vm_page_prot); | |
521 | } | |
522 | ||
523 | static int uio_mmap_logical(struct vm_area_struct *vma) | |
524 | { | |
525 | vma->vm_flags |= VM_RESERVED; | |
526 | vma->vm_ops = &uio_vm_ops; | |
527 | uio_vma_open(vma); | |
528 | return 0; | |
529 | } | |
530 | ||
531 | static int uio_mmap(struct file *filep, struct vm_area_struct *vma) | |
532 | { | |
533 | struct uio_listener *listener = filep->private_data; | |
534 | struct uio_device *idev = listener->dev; | |
535 | int mi; | |
536 | unsigned long requested_pages, actual_pages; | |
537 | int ret = 0; | |
538 | ||
539 | if (vma->vm_end < vma->vm_start) | |
540 | return -EINVAL; | |
541 | ||
542 | vma->vm_private_data = idev; | |
543 | ||
544 | mi = uio_find_mem_index(vma); | |
545 | if (mi < 0) | |
546 | return -EINVAL; | |
547 | ||
548 | requested_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; | |
549 | actual_pages = (idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT; | |
550 | if (requested_pages > actual_pages) | |
551 | return -EINVAL; | |
552 | ||
553 | if (idev->info->mmap) { | |
beafc54c | 554 | ret = idev->info->mmap(idev->info, vma); |
beafc54c HK |
555 | return ret; |
556 | } | |
557 | ||
558 | switch (idev->info->mem[mi].memtype) { | |
559 | case UIO_MEM_PHYS: | |
560 | return uio_mmap_physical(vma); | |
561 | case UIO_MEM_LOGICAL: | |
562 | case UIO_MEM_VIRTUAL: | |
563 | return uio_mmap_logical(vma); | |
564 | default: | |
565 | return -EINVAL; | |
566 | } | |
567 | } | |
568 | ||
4f014691 | 569 | static const struct file_operations uio_fops = { |
beafc54c HK |
570 | .owner = THIS_MODULE, |
571 | .open = uio_open, | |
572 | .release = uio_release, | |
573 | .read = uio_read, | |
328a14e7 | 574 | .write = uio_write, |
beafc54c HK |
575 | .mmap = uio_mmap, |
576 | .poll = uio_poll, | |
577 | .fasync = uio_fasync, | |
578 | }; | |
579 | ||
580 | static int uio_major_init(void) | |
581 | { | |
582 | uio_major = register_chrdev(0, "uio", &uio_fops); | |
583 | if (uio_major < 0) | |
584 | return uio_major; | |
585 | return 0; | |
586 | } | |
587 | ||
588 | static void uio_major_cleanup(void) | |
589 | { | |
590 | unregister_chrdev(uio_major, "uio"); | |
591 | } | |
592 | ||
593 | static int init_uio_class(void) | |
594 | { | |
595 | int ret = 0; | |
596 | ||
597 | if (uio_class != NULL) { | |
598 | kref_get(&uio_class->kref); | |
599 | goto exit; | |
600 | } | |
601 | ||
602 | /* This is the first time in here, set everything up properly */ | |
603 | ret = uio_major_init(); | |
604 | if (ret) | |
605 | goto exit; | |
606 | ||
607 | uio_class = kzalloc(sizeof(*uio_class), GFP_KERNEL); | |
608 | if (!uio_class) { | |
609 | ret = -ENOMEM; | |
610 | goto err_kzalloc; | |
611 | } | |
612 | ||
613 | kref_init(&uio_class->kref); | |
614 | uio_class->class = class_create(THIS_MODULE, "uio"); | |
615 | if (IS_ERR(uio_class->class)) { | |
616 | ret = IS_ERR(uio_class->class); | |
617 | printk(KERN_ERR "class_create failed for uio\n"); | |
618 | goto err_class_create; | |
619 | } | |
620 | return 0; | |
621 | ||
622 | err_class_create: | |
623 | kfree(uio_class); | |
624 | uio_class = NULL; | |
625 | err_kzalloc: | |
626 | uio_major_cleanup(); | |
627 | exit: | |
628 | return ret; | |
629 | } | |
630 | ||
631 | static void release_uio_class(struct kref *kref) | |
632 | { | |
633 | /* Ok, we cheat as we know we only have one uio_class */ | |
634 | class_destroy(uio_class->class); | |
635 | kfree(uio_class); | |
636 | uio_major_cleanup(); | |
637 | uio_class = NULL; | |
638 | } | |
639 | ||
640 | static void uio_class_destroy(void) | |
641 | { | |
642 | if (uio_class) | |
643 | kref_put(&uio_class->kref, release_uio_class); | |
644 | } | |
645 | ||
646 | /** | |
647 | * uio_register_device - register a new userspace IO device | |
648 | * @owner: module that creates the new device | |
649 | * @parent: parent device | |
650 | * @info: UIO device capabilities | |
651 | * | |
652 | * returns zero on success or a negative error code. | |
653 | */ | |
654 | int __uio_register_device(struct module *owner, | |
655 | struct device *parent, | |
656 | struct uio_info *info) | |
657 | { | |
658 | struct uio_device *idev; | |
659 | int ret = 0; | |
660 | ||
661 | if (!parent || !info || !info->name || !info->version) | |
662 | return -EINVAL; | |
663 | ||
664 | info->uio_dev = NULL; | |
665 | ||
666 | ret = init_uio_class(); | |
667 | if (ret) | |
668 | return ret; | |
669 | ||
670 | idev = kzalloc(sizeof(*idev), GFP_KERNEL); | |
671 | if (!idev) { | |
672 | ret = -ENOMEM; | |
673 | goto err_kzalloc; | |
674 | } | |
675 | ||
676 | idev->owner = owner; | |
677 | idev->info = info; | |
678 | init_waitqueue_head(&idev->wait); | |
679 | atomic_set(&idev->event, 0); | |
680 | ||
681 | ret = uio_get_minor(idev); | |
682 | if (ret) | |
683 | goto err_get_minor; | |
684 | ||
a9b12619 GKH |
685 | idev->dev = device_create(uio_class->class, parent, |
686 | MKDEV(uio_major, idev->minor), idev, | |
687 | "uio%d", idev->minor); | |
beafc54c HK |
688 | if (IS_ERR(idev->dev)) { |
689 | printk(KERN_ERR "UIO: device register failed\n"); | |
690 | ret = PTR_ERR(idev->dev); | |
691 | goto err_device_create; | |
692 | } | |
beafc54c HK |
693 | |
694 | ret = uio_dev_add_attributes(idev); | |
695 | if (ret) | |
696 | goto err_uio_dev_add_attributes; | |
697 | ||
698 | info->uio_dev = idev; | |
699 | ||
700 | if (idev->info->irq >= 0) { | |
701 | ret = request_irq(idev->info->irq, uio_interrupt, | |
702 | idev->info->irq_flags, idev->info->name, idev); | |
703 | if (ret) | |
704 | goto err_request_irq; | |
705 | } | |
706 | ||
707 | return 0; | |
708 | ||
709 | err_request_irq: | |
710 | uio_dev_del_attributes(idev); | |
711 | err_uio_dev_add_attributes: | |
712 | device_destroy(uio_class->class, MKDEV(uio_major, idev->minor)); | |
713 | err_device_create: | |
714 | uio_free_minor(idev); | |
715 | err_get_minor: | |
716 | kfree(idev); | |
717 | err_kzalloc: | |
718 | uio_class_destroy(); | |
719 | return ret; | |
720 | } | |
721 | EXPORT_SYMBOL_GPL(__uio_register_device); | |
722 | ||
723 | /** | |
724 | * uio_unregister_device - unregister a industrial IO device | |
725 | * @info: UIO device capabilities | |
726 | * | |
727 | */ | |
728 | void uio_unregister_device(struct uio_info *info) | |
729 | { | |
730 | struct uio_device *idev; | |
731 | ||
732 | if (!info || !info->uio_dev) | |
733 | return; | |
734 | ||
735 | idev = info->uio_dev; | |
736 | ||
737 | uio_free_minor(idev); | |
738 | ||
739 | if (info->irq >= 0) | |
740 | free_irq(info->irq, idev); | |
741 | ||
742 | uio_dev_del_attributes(idev); | |
743 | ||
744 | dev_set_drvdata(idev->dev, NULL); | |
745 | device_destroy(uio_class->class, MKDEV(uio_major, idev->minor)); | |
746 | kfree(idev); | |
747 | uio_class_destroy(); | |
748 | ||
749 | return; | |
750 | } | |
751 | EXPORT_SYMBOL_GPL(uio_unregister_device); | |
752 | ||
753 | static int __init uio_init(void) | |
754 | { | |
755 | return 0; | |
756 | } | |
757 | ||
758 | static void __exit uio_exit(void) | |
759 | { | |
760 | } | |
761 | ||
762 | module_init(uio_init) | |
763 | module_exit(uio_exit) | |
764 | MODULE_LICENSE("GPL v2"); |