Input: add semaphore and user count to input_dev structure;
serialize open and close calls and ensure that device's
open and close methods are only called when first user
opens it or last user closes it.
···219219220220int input_open_device(struct input_handle *handle)221221{222222+ struct input_dev *dev = handle->dev;223223+ int err;224224+225225+ err = down_interruptible(&dev->sem);226226+ if (err)227227+ return err;228228+222229 handle->open++;223223- if (handle->dev->open)224224- return handle->dev->open(handle->dev);225225- return 0;230230+231231+ if (!dev->users++ && dev->open)232232+ err = dev->open(dev);233233+234234+ if (err)235235+ handle->open--;236236+237237+ up(&dev->sem);238238+239239+ return err;226240}227241228242int input_flush_device(struct input_handle* handle, struct file* file)···249235250236void input_close_device(struct input_handle *handle)251237{238238+ struct input_dev *dev = handle->dev;239239+252240 input_release_device(handle);253253- if (handle->dev->close)254254- handle->dev->close(handle->dev);241241+242242+ down(&dev->sem);243243+244244+ if (!--dev->users && dev->close)245245+ dev->close(dev);255246 handle->open--;247247+248248+ up(&dev->sem);256249}257250258251static void input_link_handle(struct input_handle *handle)···435414 struct input_device_id *id;436415437416 set_bit(EV_SYN, dev->evbit);417417+418418+ init_MUTEX(&dev->sem);438419439420 /*440421 * If delay and period are pre-set by the driver, then autorepeating
+4
include/linux/input.h
···859859 int (*erase_effect)(struct input_dev *dev, int effect_id);860860861861 struct input_handle *grab;862862+863863+ struct semaphore sem; /* serializes open and close operations */864864+ unsigned int users;865865+862866 struct device *dev;863867864868 struct list_head h_list;