blob: d9cd6002824648ff2e6c53b15bfd1bf5f23dcf63 [file] [log] [blame]
Jordan Crousea73ed092012-01-24 15:40:22 -07001/* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Jordan Crouse29f66af2011-11-17 13:39:20 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/fb.h>
14#include <linux/slab.h>
15#include <linux/module.h>
16#include <linux/list.h>
17#include <linux/file.h>
18#include <linux/sched.h>
19#include <linux/fs.h>
20#include <linux/wait.h>
21#include <linux/uaccess.h>
22#include <linux/anon_inodes.h>
23#include <linux/miscdevice.h>
24#include <linux/genlock.h>
25
26/* Lock states - can either be unlocked, held as an exclusive write lock or a
27 * shared read lock
28 */
29
30#define _UNLOCKED 0
31#define _RDLOCK GENLOCK_RDLOCK
32#define _WRLOCK GENLOCK_WRLOCK
33
Naomi Luisb558aed2011-12-12 12:07:48 -080034#define GENLOCK_LOG_ERR(fmt, args...) \
35pr_err("genlock: %s: " fmt, __func__, ##args)
36
Jordan Crouse29f66af2011-11-17 13:39:20 -070037struct genlock {
38 struct list_head active; /* List of handles holding lock */
39 spinlock_t lock; /* Spinlock to protect the lock internals */
40 wait_queue_head_t queue; /* Holding pen for processes pending lock */
41 struct file *file; /* File structure for exported lock */
42 int state; /* Current state of the lock */
Jordan Crouse4a2879b2011-12-01 11:52:59 -070043 struct kref refcount;
Jordan Crouse29f66af2011-11-17 13:39:20 -070044};
45
46struct genlock_handle {
47 struct genlock *lock; /* Lock currently attached to the handle */
48 struct list_head entry; /* List node for attaching to a lock */
49 struct file *file; /* File structure associated with handle */
50 int active; /* Number of times the active lock has been
51 taken */
52};
53
Jordan Crouse03fbfbc2011-12-16 14:28:28 -070054/*
55 * Create a spinlock to protect against a race condition when a lock gets
56 * released while another process tries to attach it
57 */
58
59static DEFINE_SPINLOCK(genlock_file_lock);
60
Jordan Crouse4a2879b2011-12-01 11:52:59 -070061static void genlock_destroy(struct kref *kref)
62{
63 struct genlock *lock = container_of(kref, struct genlock,
64 refcount);
65
Jordan Crouse03fbfbc2011-12-16 14:28:28 -070066 /*
67 * Clear the private data for the file descriptor in case the fd is
68 * still active after the lock gets released
69 */
70
71 spin_lock(&genlock_file_lock);
72 if (lock->file)
73 lock->file->private_data = NULL;
74 spin_unlock(&genlock_file_lock);
75
Jordan Crouse4a2879b2011-12-01 11:52:59 -070076 kfree(lock);
77}
78
Jordan Crouse29f66af2011-11-17 13:39:20 -070079/*
80 * Release the genlock object. Called when all the references to
81 * the genlock file descriptor are released
82 */
83
84static int genlock_release(struct inode *inodep, struct file *file)
85{
Jordan Crouse03fbfbc2011-12-16 14:28:28 -070086 struct genlock *lock = file->private_data;
87 /*
88 * Clear the refrence back to this file structure to avoid
89 * somehow reusing the lock after the file has been destroyed
90 */
91
92 if (lock)
93 lock->file = NULL;
94
Jordan Crouse29f66af2011-11-17 13:39:20 -070095 return 0;
96}
97
98static const struct file_operations genlock_fops = {
99 .release = genlock_release,
100};
101
102/**
103 * genlock_create_lock - Create a new lock
104 * @handle - genlock handle to attach the lock to
105 *
106 * Returns: a pointer to the genlock
107 */
108
109struct genlock *genlock_create_lock(struct genlock_handle *handle)
110{
111 struct genlock *lock;
112
Jordan Crousea73ed092012-01-24 15:40:22 -0700113 if (IS_ERR_OR_NULL(handle)) {
114 GENLOCK_LOG_ERR("Invalid handle\n");
115 return ERR_PTR(-EINVAL);
116 }
117
Naomi Luisb558aed2011-12-12 12:07:48 -0800118 if (handle->lock != NULL) {
119 GENLOCK_LOG_ERR("Handle already has a lock attached\n");
Jordan Crouse29f66af2011-11-17 13:39:20 -0700120 return ERR_PTR(-EINVAL);
Naomi Luisb558aed2011-12-12 12:07:48 -0800121 }
Jordan Crouse29f66af2011-11-17 13:39:20 -0700122
123 lock = kzalloc(sizeof(*lock), GFP_KERNEL);
Naomi Luisb558aed2011-12-12 12:07:48 -0800124 if (lock == NULL) {
125 GENLOCK_LOG_ERR("Unable to allocate memory for a lock\n");
Jordan Crouse29f66af2011-11-17 13:39:20 -0700126 return ERR_PTR(-ENOMEM);
Naomi Luisb558aed2011-12-12 12:07:48 -0800127 }
Jordan Crouse29f66af2011-11-17 13:39:20 -0700128
129 INIT_LIST_HEAD(&lock->active);
130 init_waitqueue_head(&lock->queue);
131 spin_lock_init(&lock->lock);
132
133 lock->state = _UNLOCKED;
134
135 /*
136 * Create an anonyonmous inode for the object that can exported to
137 * other processes
138 */
139
140 lock->file = anon_inode_getfile("genlock", &genlock_fops,
141 lock, O_RDWR);
142
143 /* Attach the new lock to the handle */
144 handle->lock = lock;
Jordan Crouse4a2879b2011-12-01 11:52:59 -0700145 kref_init(&lock->refcount);
Jordan Crouse29f66af2011-11-17 13:39:20 -0700146
147 return lock;
148}
149EXPORT_SYMBOL(genlock_create_lock);
150
151/*
152 * Get a file descriptor reference to a lock suitable for sharing with
153 * other processes
154 */
155
156static int genlock_get_fd(struct genlock *lock)
157{
158 int ret;
159
Naomi Luisb558aed2011-12-12 12:07:48 -0800160 if (!lock->file) {
161 GENLOCK_LOG_ERR("No file attached to the lock\n");
Jordan Crouse29f66af2011-11-17 13:39:20 -0700162 return -EINVAL;
Naomi Luisb558aed2011-12-12 12:07:48 -0800163 }
Jordan Crouse29f66af2011-11-17 13:39:20 -0700164
165 ret = get_unused_fd_flags(0);
166 if (ret < 0)
167 return ret;
168 fd_install(ret, lock->file);
169 return ret;
170}
171
172/**
173 * genlock_attach_lock - Attach an existing lock to a handle
174 * @handle - Pointer to a genlock handle to attach the lock to
175 * @fd - file descriptor for the exported lock
176 *
177 * Returns: A pointer to the attached lock structure
178 */
179
180struct genlock *genlock_attach_lock(struct genlock_handle *handle, int fd)
181{
182 struct file *file;
Jordan Crouse4a2879b2011-12-01 11:52:59 -0700183 struct genlock *lock;
Jordan Crouse29f66af2011-11-17 13:39:20 -0700184
Jordan Crousea73ed092012-01-24 15:40:22 -0700185 if (IS_ERR_OR_NULL(handle)) {
186 GENLOCK_LOG_ERR("Invalid handle\n");
187 return ERR_PTR(-EINVAL);
188 }
189
Naomi Luisb558aed2011-12-12 12:07:48 -0800190 if (handle->lock != NULL) {
191 GENLOCK_LOG_ERR("Handle already has a lock attached\n");
Jordan Crouse29f66af2011-11-17 13:39:20 -0700192 return ERR_PTR(-EINVAL);
Naomi Luisb558aed2011-12-12 12:07:48 -0800193 }
Jordan Crouse29f66af2011-11-17 13:39:20 -0700194
195 file = fget(fd);
Naomi Luisb558aed2011-12-12 12:07:48 -0800196 if (file == NULL) {
197 GENLOCK_LOG_ERR("Bad file descriptor\n");
Jordan Crouse29f66af2011-11-17 13:39:20 -0700198 return ERR_PTR(-EBADF);
Naomi Luisb558aed2011-12-12 12:07:48 -0800199 }
Jordan Crouse29f66af2011-11-17 13:39:20 -0700200
Jordan Crouse03fbfbc2011-12-16 14:28:28 -0700201 /*
202 * take a spinlock to avoid a race condition if the lock is
203 * released and then attached
204 */
205
206 spin_lock(&genlock_file_lock);
Jordan Crouse4a2879b2011-12-01 11:52:59 -0700207 lock = file->private_data;
Jordan Crouse03fbfbc2011-12-16 14:28:28 -0700208 spin_unlock(&genlock_file_lock);
Jordan Crouse29f66af2011-11-17 13:39:20 -0700209
Jordan Crouse4a2879b2011-12-01 11:52:59 -0700210 fput(file);
211
Naomi Luisb558aed2011-12-12 12:07:48 -0800212 if (lock == NULL) {
213 GENLOCK_LOG_ERR("File descriptor is invalid\n");
Jordan Crouse4a2879b2011-12-01 11:52:59 -0700214 return ERR_PTR(-EINVAL);
Naomi Luisb558aed2011-12-12 12:07:48 -0800215 }
Jordan Crouse4a2879b2011-12-01 11:52:59 -0700216
217 handle->lock = lock;
218 kref_get(&lock->refcount);
219
220 return lock;
Jordan Crouse29f66af2011-11-17 13:39:20 -0700221}
222EXPORT_SYMBOL(genlock_attach_lock);
223
224/* Helper function that returns 1 if the specified handle holds the lock */
225
226static int handle_has_lock(struct genlock *lock, struct genlock_handle *handle)
227{
228 struct genlock_handle *h;
229
230 list_for_each_entry(h, &lock->active, entry) {
231 if (h == handle)
232 return 1;
233 }
234
235 return 0;
236}
237
238/* If the lock just became available, signal the next entity waiting for it */
239
240static void _genlock_signal(struct genlock *lock)
241{
242 if (list_empty(&lock->active)) {
243 /* If the list is empty, then the lock is free */
244 lock->state = _UNLOCKED;
245 /* Wake up the first process sitting in the queue */
246 wake_up(&lock->queue);
247 }
248}
249
250/* Attempt to release the handle's ownership of the lock */
251
252static int _genlock_unlock(struct genlock *lock, struct genlock_handle *handle)
253{
254 int ret = -EINVAL;
255 unsigned long irqflags;
256
257 spin_lock_irqsave(&lock->lock, irqflags);
258
Naomi Luisb558aed2011-12-12 12:07:48 -0800259 if (lock->state == _UNLOCKED) {
260 GENLOCK_LOG_ERR("Trying to unlock an unlocked handle\n");
Jordan Crouse29f66af2011-11-17 13:39:20 -0700261 goto done;
Naomi Luisb558aed2011-12-12 12:07:48 -0800262 }
Jordan Crouse29f66af2011-11-17 13:39:20 -0700263
264 /* Make sure this handle is an owner of the lock */
Naomi Luisb558aed2011-12-12 12:07:48 -0800265 if (!handle_has_lock(lock, handle)) {
266 GENLOCK_LOG_ERR("handle does not have lock attached to it\n");
Jordan Crouse29f66af2011-11-17 13:39:20 -0700267 goto done;
Naomi Luisb558aed2011-12-12 12:07:48 -0800268 }
Jordan Crouse29f66af2011-11-17 13:39:20 -0700269 /* If the handle holds no more references to the lock then
270 release it (maybe) */
271
272 if (--handle->active == 0) {
273 list_del(&handle->entry);
274 _genlock_signal(lock);
275 }
276
277 ret = 0;
278
279done:
280 spin_unlock_irqrestore(&lock->lock, irqflags);
281 return ret;
282}
283
284/* Attempt to acquire the lock for the handle */
285
286static int _genlock_lock(struct genlock *lock, struct genlock_handle *handle,
287 int op, int flags, uint32_t timeout)
288{
289 unsigned long irqflags;
290 int ret = 0;
Jordan Crousebe669bc2012-03-20 14:09:29 -0600291 unsigned long ticks = msecs_to_jiffies(timeout);
Jordan Crouse29f66af2011-11-17 13:39:20 -0700292
293 spin_lock_irqsave(&lock->lock, irqflags);
294
295 /* Sanity check - no blocking locks in a debug context. Even if it
296 * succeed to not block, the mere idea is too dangerous to continue
297 */
298
299 if (in_interrupt() && !(flags & GENLOCK_NOBLOCK))
300 BUG();
301
302 /* Fast path - the lock is unlocked, so go do the needful */
303
304 if (lock->state == _UNLOCKED)
305 goto dolock;
306
307 if (handle_has_lock(lock, handle)) {
308
309 /*
310 * If the handle already holds the lock and the type matches,
311 * then just increment the active pointer. This allows the
312 * handle to do recursive locks
313 */
314
315 if (lock->state == op) {
316 handle->active++;
317 goto done;
318 }
319
320 /*
321 * If the handle holds a write lock then the owner can switch
322 * to a read lock if they want. Do the transition atomically
323 * then wake up any pending waiters in case they want a read
324 * lock too.
325 */
326
327 if (op == _RDLOCK && handle->active == 1) {
328 lock->state = _RDLOCK;
329 wake_up(&lock->queue);
330 goto done;
331 }
332
333 /*
334 * Otherwise the user tried to turn a read into a write, and we
335 * don't allow that.
336 */
Naomi Luisb558aed2011-12-12 12:07:48 -0800337 GENLOCK_LOG_ERR("Trying to upgrade a read lock to a write"
338 "lock\n");
Jordan Crouse29f66af2011-11-17 13:39:20 -0700339 ret = -EINVAL;
340 goto done;
341 }
342
343 /*
344 * If we request a read and the lock is held by a read, then go
345 * ahead and share the lock
346 */
347
348 if (op == GENLOCK_RDLOCK && lock->state == _RDLOCK)
349 goto dolock;
350
351 /* Treat timeout 0 just like a NOBLOCK flag and return if the
352 lock cannot be aquired without blocking */
353
354 if (flags & GENLOCK_NOBLOCK || timeout == 0) {
355 ret = -EAGAIN;
356 goto done;
357 }
358
359 /* Wait while the lock remains in an incompatible state */
360
361 while (lock->state != _UNLOCKED) {
Jordan Crousebe669bc2012-03-20 14:09:29 -0600362 signed long elapsed;
Jordan Crouse29f66af2011-11-17 13:39:20 -0700363
364 spin_unlock_irqrestore(&lock->lock, irqflags);
365
366 elapsed = wait_event_interruptible_timeout(lock->queue,
367 lock->state == _UNLOCKED, ticks);
368
369 spin_lock_irqsave(&lock->lock, irqflags);
370
371 if (elapsed <= 0) {
372 ret = (elapsed < 0) ? elapsed : -ETIMEDOUT;
373 goto done;
374 }
375
Jordan Crousebe669bc2012-03-20 14:09:29 -0600376 ticks = (unsigned long) elapsed;
Jordan Crouse29f66af2011-11-17 13:39:20 -0700377 }
378
379dolock:
380 /* We can now get the lock, add ourselves to the list of owners */
381
382 list_add_tail(&handle->entry, &lock->active);
383 lock->state = op;
384 handle->active = 1;
385
386done:
387 spin_unlock_irqrestore(&lock->lock, irqflags);
388 return ret;
389
390}
391
392/**
393 * genlock_lock - Acquire or release a lock
394 * @handle - pointer to the genlock handle that is requesting the lock
395 * @op - the operation to perform (RDLOCK, WRLOCK, UNLOCK)
396 * @flags - flags to control the operation
397 * @timeout - optional timeout to wait for the lock to come free
398 *
399 * Returns: 0 on success or error code on failure
400 */
401
402int genlock_lock(struct genlock_handle *handle, int op, int flags,
403 uint32_t timeout)
404{
Jordan Crousea73ed092012-01-24 15:40:22 -0700405 struct genlock *lock;
406
Jordan Crouse29f66af2011-11-17 13:39:20 -0700407 int ret = 0;
408
Jordan Crousea73ed092012-01-24 15:40:22 -0700409 if (IS_ERR_OR_NULL(handle)) {
410 GENLOCK_LOG_ERR("Invalid handle\n");
411 return -EINVAL;
412 }
413
414 lock = handle->lock;
415
Naomi Luisb558aed2011-12-12 12:07:48 -0800416 if (lock == NULL) {
417 GENLOCK_LOG_ERR("Handle does not have a lock attached\n");
Jordan Crouse29f66af2011-11-17 13:39:20 -0700418 return -EINVAL;
Naomi Luisb558aed2011-12-12 12:07:48 -0800419 }
Jordan Crouse29f66af2011-11-17 13:39:20 -0700420
421 switch (op) {
422 case GENLOCK_UNLOCK:
423 ret = _genlock_unlock(lock, handle);
424 break;
425 case GENLOCK_RDLOCK:
426 case GENLOCK_WRLOCK:
427 ret = _genlock_lock(lock, handle, op, flags, timeout);
428 break;
429 default:
Naomi Luisb558aed2011-12-12 12:07:48 -0800430 GENLOCK_LOG_ERR("Invalid lock operation\n");
Jordan Crouse29f66af2011-11-17 13:39:20 -0700431 ret = -EINVAL;
432 break;
433 }
434
435 return ret;
436}
437EXPORT_SYMBOL(genlock_lock);
438
439/**
440 * genlock_wait - Wait for the lock to be released
441 * @handle - pointer to the genlock handle that is waiting for the lock
442 * @timeout - optional timeout to wait for the lock to get released
443 */
444
445int genlock_wait(struct genlock_handle *handle, uint32_t timeout)
446{
Jordan Crousea73ed092012-01-24 15:40:22 -0700447 struct genlock *lock;
Jordan Crouse29f66af2011-11-17 13:39:20 -0700448 unsigned long irqflags;
449 int ret = 0;
Jordan Crousebe669bc2012-03-20 14:09:29 -0600450 unsigned long ticks = msecs_to_jiffies(timeout);
Jordan Crouse29f66af2011-11-17 13:39:20 -0700451
Jordan Crousea73ed092012-01-24 15:40:22 -0700452 if (IS_ERR_OR_NULL(handle)) {
453 GENLOCK_LOG_ERR("Invalid handle\n");
454 return -EINVAL;
455 }
456
457 lock = handle->lock;
458
Naomi Luisb558aed2011-12-12 12:07:48 -0800459 if (lock == NULL) {
460 GENLOCK_LOG_ERR("Handle does not have a lock attached\n");
Jordan Crouse29f66af2011-11-17 13:39:20 -0700461 return -EINVAL;
Naomi Luisb558aed2011-12-12 12:07:48 -0800462 }
Jordan Crouse29f66af2011-11-17 13:39:20 -0700463
464 spin_lock_irqsave(&lock->lock, irqflags);
465
466 /*
467 * if timeout is 0 and the lock is already unlocked, then success
468 * otherwise return -EAGAIN
469 */
470
471 if (timeout == 0) {
472 ret = (lock->state == _UNLOCKED) ? 0 : -EAGAIN;
473 goto done;
474 }
475
476 while (lock->state != _UNLOCKED) {
Jordan Crousebe669bc2012-03-20 14:09:29 -0600477 signed long elapsed;
Jordan Crouse29f66af2011-11-17 13:39:20 -0700478
479 spin_unlock_irqrestore(&lock->lock, irqflags);
480
481 elapsed = wait_event_interruptible_timeout(lock->queue,
482 lock->state == _UNLOCKED, ticks);
483
484 spin_lock_irqsave(&lock->lock, irqflags);
485
486 if (elapsed <= 0) {
487 ret = (elapsed < 0) ? elapsed : -ETIMEDOUT;
488 break;
489 }
490
Jordan Crousebe669bc2012-03-20 14:09:29 -0600491 ticks = (unsigned long) elapsed;
Jordan Crouse29f66af2011-11-17 13:39:20 -0700492 }
493
494done:
495 spin_unlock_irqrestore(&lock->lock, irqflags);
496 return ret;
497}
498
Jordan Crouse4df70a22012-01-25 14:40:51 -0700499static void genlock_release_lock(struct genlock_handle *handle)
Jordan Crouse29f66af2011-11-17 13:39:20 -0700500{
501 unsigned long flags;
502
503 if (handle == NULL || handle->lock == NULL)
504 return;
505
506 spin_lock_irqsave(&handle->lock->lock, flags);
507
508 /* If the handle is holding the lock, then force it closed */
509
510 if (handle_has_lock(handle->lock, handle)) {
511 list_del(&handle->entry);
512 _genlock_signal(handle->lock);
513 }
514 spin_unlock_irqrestore(&handle->lock->lock, flags);
515
Jordan Crouse4a2879b2011-12-01 11:52:59 -0700516 kref_put(&handle->lock->refcount, genlock_destroy);
Jordan Crouse29f66af2011-11-17 13:39:20 -0700517 handle->lock = NULL;
518 handle->active = 0;
519}
Jordan Crouse29f66af2011-11-17 13:39:20 -0700520
521/*
522 * Release function called when all references to a handle are released
523 */
524
525static int genlock_handle_release(struct inode *inodep, struct file *file)
526{
527 struct genlock_handle *handle = file->private_data;
528
529 genlock_release_lock(handle);
530 kfree(handle);
531
532 return 0;
533}
534
535static const struct file_operations genlock_handle_fops = {
536 .release = genlock_handle_release
537};
538
539/*
540 * Allocate a new genlock handle
541 */
542
543static struct genlock_handle *_genlock_get_handle(void)
544{
545 struct genlock_handle *handle = kzalloc(sizeof(*handle), GFP_KERNEL);
Naomi Luisb558aed2011-12-12 12:07:48 -0800546 if (handle == NULL) {
547 GENLOCK_LOG_ERR("Unable to allocate memory for the handle\n");
Jordan Crouse29f66af2011-11-17 13:39:20 -0700548 return ERR_PTR(-ENOMEM);
Naomi Luisb558aed2011-12-12 12:07:48 -0800549 }
Jordan Crouse29f66af2011-11-17 13:39:20 -0700550
551 return handle;
552}
553
554/**
555 * genlock_get_handle - Create a new genlock handle
556 *
557 * Returns: A pointer to a new genlock handle
558 */
559
560struct genlock_handle *genlock_get_handle(void)
561{
562 struct genlock_handle *handle = _genlock_get_handle();
563 if (IS_ERR(handle))
564 return handle;
565
566 handle->file = anon_inode_getfile("genlock-handle",
567 &genlock_handle_fops, handle, O_RDWR);
568
569 return handle;
570}
571EXPORT_SYMBOL(genlock_get_handle);
572
573/**
574 * genlock_put_handle - release a reference to a genlock handle
575 * @handle - A pointer to the handle to release
576 */
577
578void genlock_put_handle(struct genlock_handle *handle)
579{
580 if (handle)
581 fput(handle->file);
582}
583EXPORT_SYMBOL(genlock_put_handle);
584
585/**
586 * genlock_get_handle_fd - Get a handle reference from a file descriptor
587 * @fd - The file descriptor for a genlock handle
588 */
589
590struct genlock_handle *genlock_get_handle_fd(int fd)
591{
592 struct file *file = fget(fd);
593
594 if (file == NULL)
595 return ERR_PTR(-EINVAL);
596
597 return file->private_data;
598}
599EXPORT_SYMBOL(genlock_get_handle_fd);
600
601#ifdef CONFIG_GENLOCK_MISCDEVICE
602
603static long genlock_dev_ioctl(struct file *filep, unsigned int cmd,
604 unsigned long arg)
605{
606 struct genlock_lock param;
607 struct genlock_handle *handle = filep->private_data;
608 struct genlock *lock;
609 int ret;
610
Jordan Crousea73ed092012-01-24 15:40:22 -0700611 if (IS_ERR_OR_NULL(handle))
612 return -EINVAL;
613
Jordan Crouse29f66af2011-11-17 13:39:20 -0700614 switch (cmd) {
615 case GENLOCK_IOC_NEW: {
616 lock = genlock_create_lock(handle);
617 if (IS_ERR(lock))
618 return PTR_ERR(lock);
619
620 return 0;
621 }
622 case GENLOCK_IOC_EXPORT: {
Naomi Luisb558aed2011-12-12 12:07:48 -0800623 if (handle->lock == NULL) {
624 GENLOCK_LOG_ERR("Handle does not have a lock"
625 "attached\n");
Jordan Crouse29f66af2011-11-17 13:39:20 -0700626 return -EINVAL;
Naomi Luisb558aed2011-12-12 12:07:48 -0800627 }
Jordan Crouse29f66af2011-11-17 13:39:20 -0700628
629 ret = genlock_get_fd(handle->lock);
630 if (ret < 0)
631 return ret;
632
633 param.fd = ret;
634
635 if (copy_to_user((void __user *) arg, &param,
636 sizeof(param)))
637 return -EFAULT;
638
639 return 0;
640 }
641 case GENLOCK_IOC_ATTACH: {
642 if (copy_from_user(&param, (void __user *) arg,
643 sizeof(param)))
644 return -EFAULT;
645
646 lock = genlock_attach_lock(handle, param.fd);
647 if (IS_ERR(lock))
648 return PTR_ERR(lock);
649
650 return 0;
651 }
652 case GENLOCK_IOC_LOCK: {
653 if (copy_from_user(&param, (void __user *) arg,
654 sizeof(param)))
655 return -EFAULT;
656
657 return genlock_lock(handle, param.op, param.flags,
658 param.timeout);
659 }
660 case GENLOCK_IOC_WAIT: {
661 if (copy_from_user(&param, (void __user *) arg,
662 sizeof(param)))
663 return -EFAULT;
664
665 return genlock_wait(handle, param.timeout);
666 }
667 case GENLOCK_IOC_RELEASE: {
Jordan Crouse4df70a22012-01-25 14:40:51 -0700668 /*
669 * Return error - this ioctl has been deprecated.
670 * Locks should only be released when the handle is
671 * destroyed
672 */
673 GENLOCK_LOG_ERR("Deprecated RELEASE ioctl called\n");
674 return -EINVAL;
Jordan Crouse29f66af2011-11-17 13:39:20 -0700675 }
676 default:
Naomi Luisb558aed2011-12-12 12:07:48 -0800677 GENLOCK_LOG_ERR("Invalid ioctl\n");
Jordan Crouse29f66af2011-11-17 13:39:20 -0700678 return -EINVAL;
679 }
680}
681
682static int genlock_dev_release(struct inode *inodep, struct file *file)
683{
684 struct genlock_handle *handle = file->private_data;
685
Jordan Crouse4a2879b2011-12-01 11:52:59 -0700686 genlock_release_lock(handle);
687 kfree(handle);
Jordan Crouse29f66af2011-11-17 13:39:20 -0700688
689 return 0;
690}
691
692static int genlock_dev_open(struct inode *inodep, struct file *file)
693{
694 struct genlock_handle *handle = _genlock_get_handle();
695 if (IS_ERR(handle))
696 return PTR_ERR(handle);
697
698 handle->file = file;
699 file->private_data = handle;
700 return 0;
701}
702
703static const struct file_operations genlock_dev_fops = {
704 .open = genlock_dev_open,
705 .release = genlock_dev_release,
706 .unlocked_ioctl = genlock_dev_ioctl,
707};
708
709static struct miscdevice genlock_dev;
710
711static int genlock_dev_init(void)
712{
713 genlock_dev.minor = MISC_DYNAMIC_MINOR;
714 genlock_dev.name = "genlock";
715 genlock_dev.fops = &genlock_dev_fops;
716 genlock_dev.parent = NULL;
717
718 return misc_register(&genlock_dev);
719}
720
721static void genlock_dev_close(void)
722{
723 misc_deregister(&genlock_dev);
724}
725
726module_init(genlock_dev_init);
727module_exit(genlock_dev_close);
728
729#endif