4 * @brief Implements the device lock class.
5 * @note Copyright (C) 2006 Meilhaus Electronic GmbH (support@meilhaus.de)
6 * @author Guenter Gebhardt
10 * Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
12 * This file is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/spinlock.h>
29 #include "medefines.h"
34 #include "mesubdevice.h"
37 int me_dlock_enter(struct me_dlock *dlock, struct file *filep)
39 PDEBUG_LOCKS("executed.\n");
41 spin_lock(&dlock->spin_lock);
43 if ((dlock->filep) != NULL && (dlock->filep != filep)) {
44 PERROR("Device is locked by another process.\n");
45 spin_unlock(&dlock->spin_lock);
46 return ME_ERRNO_LOCKED;
51 spin_unlock(&dlock->spin_lock);
53 return ME_ERRNO_SUCCESS;
56 int me_dlock_exit(struct me_dlock *dlock, struct file *filep)
58 PDEBUG_LOCKS("executed.\n");
60 spin_lock(&dlock->spin_lock);
62 spin_unlock(&dlock->spin_lock);
64 return ME_ERRNO_SUCCESS;
67 int me_dlock_lock(struct me_dlock *dlock,
68 struct file *filep, int lock, int flags, me_slist_t * slist)
70 int err = ME_ERRNO_SUCCESS;
72 me_subdevice_t *subdevice;
74 PDEBUG_LOCKS("executed.\n");
76 spin_lock(&dlock->spin_lock);
81 if ((dlock->filep == filep) || (dlock->filep == NULL)) {
84 /* Unlock all possibly locked subdevices. */
86 for (i = 0; i < me_slist_get_number_subdevices(slist);
88 subdevice = me_slist_get_subdevice(slist, i);
93 me_subdevice_lock_subdevice
94 (subdevice, filep, ME_LOCK_RELEASE,
97 err = ME_ERRNO_INTERNAL;
105 PERROR("Device is used by another process.\n");
107 } else if ((dlock->filep != NULL) && (dlock->filep != filep)) {
108 PERROR("Device is locked by another process.\n");
109 err = ME_ERRNO_LOCKED;
110 } else if (dlock->filep == NULL) {
111 /* Check any subdevice is locked by another process. */
113 for (i = 0; i < me_slist_get_number_subdevices(slist);
115 subdevice = me_slist_get_subdevice(slist, i);
120 me_subdevice_lock_subdevice
121 (subdevice, filep, ME_LOCK_CHECK,
124 ("A subdevice is locked by another process.\n");
128 err = ME_ERRNO_INTERNAL;
132 /* If no subdevices are locked by other processes,
133 we can take ownership of the device. Otherwise we jump ahead. */
135 dlock->filep = filep;
143 } else if ((dlock->filep != NULL) && (dlock->filep != filep)) {
144 err = ME_ERRNO_LOCKED;
145 } else if (dlock->filep == NULL) {
146 for (i = 0; i < me_slist_get_number_subdevices(slist);
148 subdevice = me_slist_get_subdevice(slist, i);
153 me_subdevice_lock_subdevice
154 (subdevice, filep, ME_LOCK_CHECK,
157 ("A subdevice is locked by another process.\n");
161 err = ME_ERRNO_INTERNAL;
169 PERROR("Invalid lock.\n");
171 err = ME_ERRNO_INVALID_LOCK;
176 spin_unlock(&dlock->spin_lock);
181 void me_dlock_deinit(struct me_dlock *dlock)
183 PDEBUG_LOCKS("executed.\n");
186 int me_dlock_init(me_dlock_t * dlock)
188 PDEBUG_LOCKS("executed.\n");
192 spin_lock_init(&dlock->spin_lock);