evdev.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. /*
  2. * Event char devices, giving access to raw input device events.
  3. *
  4. * Copyright (c) 1999-2002 Vojtech Pavlik
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #define EVDEV_MINOR_BASE 64
  12. #define EVDEV_MINORS 32
  13. #define EVDEV_MIN_BUFFER_SIZE 64U
  14. #define EVDEV_BUF_PACKETS 8
  15. #include <linux/poll.h>
  16. #include <linux/sched.h>
  17. #include <linux/slab.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/input/mt.h>
  21. #include <linux/major.h>
  22. #include <linux/device.h>
  23. #include <linux/wakelock.h>
  24. #include "input-compat.h"
  25. struct evdev {
  26. int open;
  27. int minor;
  28. struct input_handle handle;
  29. wait_queue_head_t wait;
  30. struct evdev_client __rcu *grab;
  31. struct list_head client_list;
  32. spinlock_t client_lock; /* protects client_list */
  33. struct mutex mutex;
  34. struct device dev;
  35. bool exist;
  36. int hw_ts_sec;
  37. int hw_ts_nsec;
  38. };
  39. struct evdev_client {
  40. unsigned int head;
  41. unsigned int tail;
  42. unsigned int packet_head; /* [future] position of the first element of next packet */
  43. spinlock_t buffer_lock; /* protects access to buffer, head and tail */
  44. struct wake_lock wake_lock;
  45. bool use_wake_lock;
  46. char name[28];
  47. struct fasync_struct *fasync;
  48. struct evdev *evdev;
  49. struct list_head node;
  50. int clkid;
  51. unsigned int bufsize;
  52. struct input_event buffer[];
  53. };
  54. static struct evdev *evdev_table[EVDEV_MINORS];
  55. static DEFINE_MUTEX(evdev_table_mutex);
  56. static __always_inline void
  57. __pass_event(struct evdev_client *client, const struct input_event *event)
  58. {
  59. client->buffer[client->head++] = *event;
  60. client->head &= client->bufsize - 1;
  61. if (unlikely(client->head == client->tail)) {
  62. /*
  63. * This effectively "drops" all unconsumed events, leaving
  64. * EV_SYN/SYN_DROPPED plus the newest event in the queue.
  65. */
  66. client->tail = (client->head - 2) & (client->bufsize - 1);
  67. client->buffer[client->tail].time = event->time;
  68. client->buffer[client->tail].type = EV_SYN;
  69. client->buffer[client->tail].code = SYN_DROPPED;
  70. client->buffer[client->tail].value = 0;
  71. client->packet_head = client->tail;
  72. if (client->use_wake_lock)
  73. wake_unlock(&client->wake_lock);
  74. }
  75. if (event->type == EV_SYN && event->code == SYN_REPORT) {
  76. client->packet_head = client->head;
  77. if (client->use_wake_lock)
  78. wake_lock(&client->wake_lock);
  79. kill_fasync(&client->fasync, SIGIO, POLL_IN);
  80. }
  81. }
  82. static inline void
  83. evdev_pass_values(struct evdev_client *client, const struct input_value *vals,
  84. unsigned int count, ktime_t mono, ktime_t real)
  85. {
  86. struct input_event event = {
  87. .time = ktime_to_timeval(client->clkid == CLOCK_MONOTONIC ?
  88. mono : real),
  89. };
  90. struct evdev *evdev = client->evdev;
  91. const struct input_value *v;
  92. bool wakeup = false;
  93. /* Interrupts are disabled, just acquire the lock */
  94. spin_lock(&client->buffer_lock);
  95. for (v = vals; v != vals + count; v++) {
  96. /* Skip time calibration packets */
  97. if (v->type == EV_SYN &&
  98. (v->code == SYN_TIME_SEC || v->code == SYN_TIME_NSEC))
  99. continue;
  100. event.type = v->type;
  101. event.code = v->code;
  102. event.value = v->value;
  103. __pass_event(client, &event);
  104. if (v->type == EV_SYN && v->code == SYN_REPORT)
  105. wakeup = true;
  106. }
  107. spin_unlock(&client->buffer_lock);
  108. if (wakeup) {
  109. evdev->hw_ts_sec = -1;
  110. evdev->hw_ts_nsec = -1;
  111. wake_up_interruptible(&evdev->wait);
  112. }
  113. }
  114. static inline void
  115. evdev_parse_hwcalib(struct evdev *evdev, const struct input_value *vals)
  116. {
  117. if (vals[0].type == EV_SYN && vals[0].code == SYN_TIME_SEC)
  118. evdev->hw_ts_sec = vals[0].value;
  119. if (vals[1].type == EV_SYN && vals[1].code == SYN_TIME_NSEC)
  120. evdev->hw_ts_nsec = vals[1].value;
  121. }
  122. /*
  123. * Pass incoming events to all connected clients.
  124. */
  125. static void evdev_events(struct input_handle *handle,
  126. const struct input_value *vals,
  127. unsigned int count)
  128. {
  129. struct evdev *evdev = handle->private;
  130. struct evdev_client *client;
  131. ktime_t time_mono, time_real;
  132. /* Calibration data is expected to be within first 2 packets */
  133. if (likely(count > 2))
  134. evdev_parse_hwcalib(evdev, vals);
  135. time_mono = (evdev->hw_ts_sec != -1 && evdev->hw_ts_nsec != -1) ?
  136. ktime_set(evdev->hw_ts_sec, evdev->hw_ts_nsec) : ktime_get();
  137. time_real = ktime_sub(time_mono, ktime_get_monotonic_offset());
  138. rcu_read_lock();
  139. client = rcu_dereference(evdev->grab);
  140. if (client)
  141. evdev_pass_values(client, vals, count, time_mono, time_real);
  142. else
  143. list_for_each_entry_rcu(client, &evdev->client_list, node)
  144. evdev_pass_values(client, vals, count,
  145. time_mono, time_real);
  146. rcu_read_unlock();
  147. }
  148. /*
  149. * Pass incoming event to all connected clients.
  150. */
  151. static void evdev_event(struct input_handle *handle,
  152. u32 type, u32 code, int value)
  153. {
  154. struct input_value vals[] = { { type, code, value } };
  155. evdev_events(handle, vals, 1);
  156. }
  157. static int evdev_fasync(int fd, struct file *file, int on)
  158. {
  159. struct evdev_client *client = file->private_data;
  160. return fasync_helper(fd, file, on, &client->fasync);
  161. }
  162. static int evdev_flush(struct file *file, fl_owner_t id)
  163. {
  164. struct evdev_client *client = file->private_data;
  165. struct evdev *evdev = client->evdev;
  166. int retval;
  167. retval = mutex_lock_interruptible(&evdev->mutex);
  168. if (retval)
  169. return retval;
  170. if (!evdev->exist)
  171. retval = -ENODEV;
  172. else
  173. retval = input_flush_device(&evdev->handle, file);
  174. mutex_unlock(&evdev->mutex);
  175. return retval;
  176. }
  177. static void evdev_free(struct device *dev)
  178. {
  179. struct evdev *evdev = container_of(dev, struct evdev, dev);
  180. input_put_device(evdev->handle.dev);
  181. kfree(evdev);
  182. }
  183. /*
  184. * Grabs an event device (along with underlying input device).
  185. * This function is called with evdev->mutex taken.
  186. */
  187. static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
  188. {
  189. int error;
  190. if (evdev->grab)
  191. return -EBUSY;
  192. error = input_grab_device(&evdev->handle);
  193. if (error)
  194. return error;
  195. rcu_assign_pointer(evdev->grab, client);
  196. return 0;
  197. }
  198. static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
  199. {
  200. if (evdev->grab != client)
  201. return -EINVAL;
  202. rcu_assign_pointer(evdev->grab, NULL);
  203. synchronize_rcu();
  204. input_release_device(&evdev->handle);
  205. return 0;
  206. }
  207. static void evdev_attach_client(struct evdev *evdev,
  208. struct evdev_client *client)
  209. {
  210. spin_lock(&evdev->client_lock);
  211. list_add_tail_rcu(&client->node, &evdev->client_list);
  212. spin_unlock(&evdev->client_lock);
  213. }
  214. static void evdev_detach_client(struct evdev *evdev,
  215. struct evdev_client *client)
  216. {
  217. spin_lock(&evdev->client_lock);
  218. list_del_rcu(&client->node);
  219. spin_unlock(&evdev->client_lock);
  220. synchronize_rcu();
  221. }
  222. static int evdev_open_device(struct evdev *evdev)
  223. {
  224. int retval;
  225. retval = mutex_lock_interruptible(&evdev->mutex);
  226. if (retval)
  227. return retval;
  228. if (!evdev->exist)
  229. retval = -ENODEV;
  230. else if (!evdev->open++) {
  231. retval = input_open_device(&evdev->handle);
  232. if (retval)
  233. evdev->open--;
  234. }
  235. mutex_unlock(&evdev->mutex);
  236. return retval;
  237. }
  238. static void evdev_close_device(struct evdev *evdev)
  239. {
  240. mutex_lock(&evdev->mutex);
  241. if (evdev->exist && !--evdev->open)
  242. input_close_device(&evdev->handle);
  243. mutex_unlock(&evdev->mutex);
  244. }
  245. /*
  246. * Wake up users waiting for IO so they can disconnect from
  247. * dead device.
  248. */
  249. static void evdev_hangup(struct evdev *evdev)
  250. {
  251. struct evdev_client *client;
  252. spin_lock(&evdev->client_lock);
  253. list_for_each_entry(client, &evdev->client_list, node)
  254. kill_fasync(&client->fasync, SIGIO, POLL_HUP);
  255. spin_unlock(&evdev->client_lock);
  256. wake_up_interruptible(&evdev->wait);
  257. }
  258. static int evdev_release(struct inode *inode, struct file *file)
  259. {
  260. struct evdev_client *client = file->private_data;
  261. struct evdev *evdev = client->evdev;
  262. mutex_lock(&evdev->mutex);
  263. if (evdev->grab == client)
  264. evdev_ungrab(evdev, client);
  265. mutex_unlock(&evdev->mutex);
  266. evdev_detach_client(evdev, client);
  267. if (client->use_wake_lock)
  268. wake_lock_destroy(&client->wake_lock);
  269. kfree(client);
  270. evdev_close_device(evdev);
  271. put_device(&evdev->dev);
  272. return 0;
  273. }
  274. static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
  275. {
  276. unsigned int n_events =
  277. max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
  278. EVDEV_MIN_BUFFER_SIZE);
  279. return roundup_pow_of_two(n_events);
  280. }
  281. static int evdev_open(struct inode *inode, struct file *file)
  282. {
  283. struct evdev *evdev;
  284. struct evdev_client *client;
  285. int i = iminor(inode) - EVDEV_MINOR_BASE;
  286. unsigned int bufsize;
  287. int error;
  288. if (i >= EVDEV_MINORS)
  289. return -ENODEV;
  290. error = mutex_lock_interruptible(&evdev_table_mutex);
  291. if (error)
  292. return error;
  293. evdev = evdev_table[i];
  294. if (evdev)
  295. get_device(&evdev->dev);
  296. mutex_unlock(&evdev_table_mutex);
  297. if (!evdev)
  298. return -ENODEV;
  299. bufsize = evdev_compute_buffer_size(evdev->handle.dev);
  300. client = kzalloc(sizeof(struct evdev_client) +
  301. bufsize * sizeof(struct input_event),
  302. GFP_KERNEL);
  303. if (!client) {
  304. error = -ENOMEM;
  305. goto err_put_evdev;
  306. }
  307. client->clkid = CLOCK_MONOTONIC;
  308. client->bufsize = bufsize;
  309. spin_lock_init(&client->buffer_lock);
  310. snprintf(client->name, sizeof(client->name), "%s-%d",
  311. dev_name(&evdev->dev), task_tgid_vnr(current));
  312. client->evdev = evdev;
  313. evdev_attach_client(evdev, client);
  314. error = evdev_open_device(evdev);
  315. if (error)
  316. goto err_free_client;
  317. file->private_data = client;
  318. nonseekable_open(inode, file);
  319. return 0;
  320. err_free_client:
  321. evdev_detach_client(evdev, client);
  322. kfree(client);
  323. err_put_evdev:
  324. put_device(&evdev->dev);
  325. return error;
  326. }
  327. static ssize_t evdev_write(struct file *file, const char __user *buffer,
  328. size_t count, loff_t *ppos)
  329. {
  330. struct evdev_client *client = file->private_data;
  331. struct evdev *evdev = client->evdev;
  332. struct input_event event;
  333. int retval = 0;
  334. if (count < input_event_size())
  335. return -EINVAL;
  336. retval = mutex_lock_interruptible(&evdev->mutex);
  337. if (retval)
  338. return retval;
  339. if (!evdev->exist) {
  340. retval = -ENODEV;
  341. goto out;
  342. }
  343. do {
  344. if (input_event_from_user(buffer + retval, &event)) {
  345. retval = -EFAULT;
  346. goto out;
  347. }
  348. retval += input_event_size();
  349. input_inject_event(&evdev->handle,
  350. event.type, event.code, event.value);
  351. } while (retval + input_event_size() <= count);
  352. out:
  353. mutex_unlock(&evdev->mutex);
  354. return retval;
  355. }
  356. static int evdev_fetch_next_event(struct evdev_client *client,
  357. struct input_event *event)
  358. {
  359. int have_event;
  360. spin_lock_irq(&client->buffer_lock);
  361. have_event = client->packet_head != client->tail;
  362. if (have_event) {
  363. *event = client->buffer[client->tail++];
  364. client->tail &= client->bufsize - 1;
  365. if (client->use_wake_lock &&
  366. client->packet_head == client->tail)
  367. wake_unlock(&client->wake_lock);
  368. }
  369. spin_unlock_irq(&client->buffer_lock);
  370. return have_event;
  371. }
  372. static ssize_t evdev_read(struct file *file, char __user *buffer,
  373. size_t count, loff_t *ppos)
  374. {
  375. struct evdev_client *client = file->private_data;
  376. struct evdev *evdev = client->evdev;
  377. struct input_event event;
  378. int retval = 0;
  379. if (count < input_event_size())
  380. return -EINVAL;
  381. if (!(file->f_flags & O_NONBLOCK)) {
  382. retval = wait_event_interruptible(evdev->wait,
  383. client->packet_head != client->tail ||
  384. !evdev->exist);
  385. if (retval)
  386. return retval;
  387. }
  388. if (!evdev->exist)
  389. return -ENODEV;
  390. while (retval + input_event_size() <= count &&
  391. evdev_fetch_next_event(client, &event)) {
  392. if (input_event_to_user(buffer + retval, &event))
  393. return -EFAULT;
  394. retval += input_event_size();
  395. }
  396. if (retval == 0 && (file->f_flags & O_NONBLOCK))
  397. return -EAGAIN;
  398. return retval;
  399. }
  400. /* No kernel lock - fine */
  401. static unsigned int evdev_poll(struct file *file, poll_table *wait)
  402. {
  403. struct evdev_client *client = file->private_data;
  404. struct evdev *evdev = client->evdev;
  405. unsigned int mask;
  406. poll_wait(file, &evdev->wait, wait);
  407. mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
  408. if (client->packet_head != client->tail)
  409. mask |= POLLIN | POLLRDNORM;
  410. return mask;
  411. }
  412. #ifdef CONFIG_COMPAT
  413. #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
  414. #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
  415. #ifdef __BIG_ENDIAN
  416. static int bits_to_user(unsigned long *bits, unsigned int maxbit,
  417. unsigned int maxlen, void __user *p, int compat)
  418. {
  419. int len, i;
  420. if (compat) {
  421. len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
  422. if (len > maxlen)
  423. len = maxlen;
  424. for (i = 0; i < len / sizeof(compat_long_t); i++)
  425. if (copy_to_user((compat_long_t __user *) p + i,
  426. (compat_long_t *) bits +
  427. i + 1 - ((i % 2) << 1),
  428. sizeof(compat_long_t)))
  429. return -EFAULT;
  430. } else {
  431. len = BITS_TO_LONGS(maxbit) * sizeof(long);
  432. if (len > maxlen)
  433. len = maxlen;
  434. if (copy_to_user(p, bits, len))
  435. return -EFAULT;
  436. }
  437. return len;
  438. }
  439. #else
  440. static int bits_to_user(unsigned long *bits, unsigned int maxbit,
  441. unsigned int maxlen, void __user *p, int compat)
  442. {
  443. int len = compat ?
  444. BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
  445. BITS_TO_LONGS(maxbit) * sizeof(long);
  446. if (len > maxlen)
  447. len = maxlen;
  448. return copy_to_user(p, bits, len) ? -EFAULT : len;
  449. }
  450. #endif /* __BIG_ENDIAN */
  451. #else
  452. static int bits_to_user(unsigned long *bits, unsigned int maxbit,
  453. unsigned int maxlen, void __user *p, int compat)
  454. {
  455. int len = BITS_TO_LONGS(maxbit) * sizeof(long);
  456. if (len > maxlen)
  457. len = maxlen;
  458. return copy_to_user(p, bits, len) ? -EFAULT : len;
  459. }
  460. #endif /* CONFIG_COMPAT */
  461. static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
  462. {
  463. int len;
  464. if (!str)
  465. return -ENOENT;
  466. len = strlen(str) + 1;
  467. if (len > maxlen)
  468. len = maxlen;
  469. return copy_to_user(p, str, len) ? -EFAULT : len;
  470. }
  471. #define OLD_KEY_MAX 0x1ff
  472. static int handle_eviocgbit(struct input_dev *dev,
  473. unsigned int type, unsigned int size,
  474. void __user *p, int compat_mode)
  475. {
  476. static unsigned long keymax_warn_time;
  477. unsigned long *bits;
  478. int len;
  479. switch (type) {
  480. case 0: bits = dev->evbit; len = EV_MAX; break;
  481. case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
  482. case EV_REL: bits = dev->relbit; len = REL_MAX; break;
  483. case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
  484. case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
  485. case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
  486. case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
  487. case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
  488. case EV_SW: bits = dev->swbit; len = SW_MAX; break;
  489. default: return -EINVAL;
  490. }
  491. /*
  492. * Work around bugs in userspace programs that like to do
  493. * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
  494. * should be in bytes, not in bits.
  495. */
  496. if (type == EV_KEY && size == OLD_KEY_MAX) {
  497. len = OLD_KEY_MAX;
  498. if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
  499. pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
  500. "limiting output to %zu bytes. See "
  501. "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
  502. OLD_KEY_MAX,
  503. BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
  504. }
  505. return bits_to_user(bits, len, size, p, compat_mode);
  506. }
  507. #undef OLD_KEY_MAX
  508. static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
  509. {
  510. struct input_keymap_entry ke = {
  511. .len = sizeof(unsigned int),
  512. .flags = 0,
  513. };
  514. int __user *ip = (int __user *)p;
  515. int error;
  516. /* legacy case */
  517. if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
  518. return -EFAULT;
  519. error = input_get_keycode(dev, &ke);
  520. if (error)
  521. return error;
  522. if (put_user(ke.keycode, ip + 1))
  523. return -EFAULT;
  524. return 0;
  525. }
  526. static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
  527. {
  528. struct input_keymap_entry ke;
  529. int error;
  530. if (copy_from_user(&ke, p, sizeof(ke)))
  531. return -EFAULT;
  532. error = input_get_keycode(dev, &ke);
  533. if (error)
  534. return error;
  535. if (copy_to_user(p, &ke, sizeof(ke)))
  536. return -EFAULT;
  537. return 0;
  538. }
  539. static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
  540. {
  541. struct input_keymap_entry ke = {
  542. .len = sizeof(unsigned int),
  543. .flags = 0,
  544. };
  545. int __user *ip = (int __user *)p;
  546. if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
  547. return -EFAULT;
  548. if (get_user(ke.keycode, ip + 1))
  549. return -EFAULT;
  550. return input_set_keycode(dev, &ke);
  551. }
  552. static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
  553. {
  554. struct input_keymap_entry ke;
  555. if (copy_from_user(&ke, p, sizeof(ke)))
  556. return -EFAULT;
  557. if (ke.len > sizeof(ke.scancode))
  558. return -EINVAL;
  559. return input_set_keycode(dev, &ke);
  560. }
  561. static int evdev_handle_mt_request(struct input_dev *dev,
  562. unsigned int size,
  563. int __user *ip)
  564. {
  565. const struct input_mt_slot *mt = dev->mt;
  566. unsigned int code;
  567. int max_slots;
  568. int i;
  569. if (get_user(code, &ip[0]))
  570. return -EFAULT;
  571. if (!input_is_mt_value(code))
  572. return -EINVAL;
  573. max_slots = (size - sizeof(__u32)) / sizeof(__s32);
  574. for (i = 0; i < dev->mtsize && i < max_slots; i++)
  575. if (put_user(input_mt_get_value(&mt[i], code), &ip[1 + i]))
  576. return -EFAULT;
  577. return 0;
  578. }
  579. static int evdev_enable_suspend_block(struct evdev *evdev,
  580. struct evdev_client *client)
  581. {
  582. if (client->use_wake_lock)
  583. return 0;
  584. spin_lock_irq(&client->buffer_lock);
  585. wake_lock_init(&client->wake_lock, WAKE_LOCK_SUSPEND, client->name);
  586. client->use_wake_lock = true;
  587. if (client->packet_head != client->tail)
  588. wake_lock(&client->wake_lock);
  589. spin_unlock_irq(&client->buffer_lock);
  590. return 0;
  591. }
  592. static int evdev_disable_suspend_block(struct evdev *evdev,
  593. struct evdev_client *client)
  594. {
  595. if (!client->use_wake_lock)
  596. return 0;
  597. spin_lock_irq(&client->buffer_lock);
  598. client->use_wake_lock = false;
  599. spin_unlock_irq(&client->buffer_lock);
  600. wake_lock_destroy(&client->wake_lock);
  601. return 0;
  602. }
  603. static long evdev_do_ioctl(struct file *file, unsigned int cmd,
  604. void __user *p, int compat_mode)
  605. {
  606. struct evdev_client *client = file->private_data;
  607. struct evdev *evdev = client->evdev;
  608. struct input_dev *dev = evdev->handle.dev;
  609. struct input_absinfo abs;
  610. struct ff_effect effect;
  611. int __user *ip = (int __user *)p;
  612. unsigned int i, t, u, v;
  613. unsigned int size;
  614. int error;
  615. /* First we check for fixed-length commands */
  616. switch (cmd) {
  617. case EVIOCGVERSION:
  618. return put_user(EV_VERSION, ip);
  619. case EVIOCGID:
  620. if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
  621. return -EFAULT;
  622. return 0;
  623. case EVIOCGREP:
  624. if (!test_bit(EV_REP, dev->evbit))
  625. return -ENOSYS;
  626. if (put_user(dev->rep[REP_DELAY], ip))
  627. return -EFAULT;
  628. if (put_user(dev->rep[REP_PERIOD], ip + 1))
  629. return -EFAULT;
  630. return 0;
  631. case EVIOCSREP:
  632. if (!test_bit(EV_REP, dev->evbit))
  633. return -ENOSYS;
  634. if (get_user(u, ip))
  635. return -EFAULT;
  636. if (get_user(v, ip + 1))
  637. return -EFAULT;
  638. input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
  639. input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
  640. return 0;
  641. case EVIOCRMFF:
  642. return input_ff_erase(dev, (int)(unsigned long) p, file);
  643. case EVIOCGEFFECTS:
  644. i = test_bit(EV_FF, dev->evbit) ?
  645. dev->ff->max_effects : 0;
  646. if (put_user(i, ip))
  647. return -EFAULT;
  648. return 0;
  649. case EVIOCGRAB:
  650. if (p)
  651. return evdev_grab(evdev, client);
  652. else
  653. return evdev_ungrab(evdev, client);
  654. case EVIOCSCLOCKID:
  655. if (copy_from_user(&i, p, sizeof(unsigned int)))
  656. return -EFAULT;
  657. if (i != CLOCK_MONOTONIC && i != CLOCK_REALTIME)
  658. return -EINVAL;
  659. client->clkid = i;
  660. return 0;
  661. case EVIOCGKEYCODE:
  662. return evdev_handle_get_keycode(dev, p);
  663. case EVIOCSKEYCODE:
  664. return evdev_handle_set_keycode(dev, p);
  665. case EVIOCGKEYCODE_V2:
  666. return evdev_handle_get_keycode_v2(dev, p);
  667. case EVIOCSKEYCODE_V2:
  668. return evdev_handle_set_keycode_v2(dev, p);
  669. case EVIOCGSUSPENDBLOCK:
  670. return put_user(client->use_wake_lock, ip);
  671. case EVIOCSSUSPENDBLOCK:
  672. if (p)
  673. return evdev_enable_suspend_block(evdev, client);
  674. else
  675. return evdev_disable_suspend_block(evdev, client);
  676. }
  677. size = _IOC_SIZE(cmd);
  678. /* Now check variable-length commands */
  679. #define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
  680. switch (EVIOC_MASK_SIZE(cmd)) {
  681. case EVIOCGPROP(0):
  682. return bits_to_user(dev->propbit, INPUT_PROP_MAX,
  683. size, p, compat_mode);
  684. case EVIOCGMTSLOTS(0):
  685. return evdev_handle_mt_request(dev, size, ip);
  686. case EVIOCGKEY(0):
  687. return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
  688. case EVIOCGLED(0):
  689. return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
  690. case EVIOCGSND(0):
  691. return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
  692. case EVIOCGSW(0):
  693. return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
  694. case EVIOCGNAME(0):
  695. return str_to_user(dev->name, size, p);
  696. case EVIOCGPHYS(0):
  697. return str_to_user(dev->phys, size, p);
  698. case EVIOCGUNIQ(0):
  699. return str_to_user(dev->uniq, size, p);
  700. case EVIOC_MASK_SIZE(EVIOCSFF):
  701. if (input_ff_effect_from_user(p, size, &effect))
  702. return -EFAULT;
  703. error = input_ff_upload(dev, &effect, file);
  704. if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
  705. return -EFAULT;
  706. return error;
  707. }
  708. /* Multi-number variable-length handlers */
  709. if (_IOC_TYPE(cmd) != 'E')
  710. return -EINVAL;
  711. if (_IOC_DIR(cmd) == _IOC_READ) {
  712. if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
  713. return handle_eviocgbit(dev,
  714. _IOC_NR(cmd) & EV_MAX, size,
  715. p, compat_mode);
  716. #ifdef CONFIG_INPUT_EXPANDED_ABS
  717. if ((EVIOCGABS_CHG_LIMIT(_IOC_NR(cmd)) & ~(EVIOCGABS_CHG_LIMIT(EVIOCGABS_LIMIT) - 1))
  718. == EVIOCGABS_CHG_LIMIT(_IOC_NR(EVIOCGABS(0)))) {
  719. if (!dev->absinfo)
  720. return -EINVAL;
  721. t = EVIOCGABS_CHG_LIMIT(_IOC_NR(cmd)) & (EVIOCGABS_CHG_LIMIT(EVIOCGABS_LIMIT) - 1);
  722. abs = dev->absinfo[t];
  723. if (copy_to_user(p, &abs, min_t(size_t,
  724. size, sizeof(struct input_absinfo))))
  725. return -EFAULT;
  726. return 0;
  727. } else if ((_IOC_NR(cmd) & ~(EVIOCGABS_LIMIT - 1)) == _IOC_NR(EVIOCGABS(0))) {
  728. if (!dev->absinfo)
  729. return -EINVAL;
  730. t = _IOC_NR(cmd) & (EVIOCGABS_LIMIT - 1);
  731. abs = dev->absinfo[t];
  732. if (copy_to_user(p, &abs, min_t(size_t,
  733. size, sizeof(struct input_absinfo))))
  734. return -EFAULT;
  735. return 0;
  736. }
  737. #else
  738. if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
  739. if (!dev->absinfo)
  740. return -EINVAL;
  741. t = _IOC_NR(cmd) & ABS_MAX;
  742. abs = dev->absinfo[t];
  743. if (copy_to_user(p, &abs, min_t(size_t,
  744. size, sizeof(struct input_absinfo))))
  745. return -EFAULT;
  746. return 0;
  747. }
  748. #endif
  749. }
  750. if (_IOC_DIR(cmd) == _IOC_WRITE) {
  751. if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
  752. if (!dev->absinfo)
  753. return -EINVAL;
  754. t = _IOC_NR(cmd) & ABS_MAX;
  755. if (copy_from_user(&abs, p, min_t(size_t,
  756. size, sizeof(struct input_absinfo))))
  757. return -EFAULT;
  758. if (size < sizeof(struct input_absinfo))
  759. abs.resolution = 0;
  760. /* We can't change number of reserved MT slots */
  761. if (t == ABS_MT_SLOT)
  762. return -EINVAL;
  763. /*
  764. * Take event lock to ensure that we are not
  765. * changing device parameters in the middle
  766. * of event.
  767. */
  768. spin_lock_irq(&dev->event_lock);
  769. dev->absinfo[t] = abs;
  770. spin_unlock_irq(&dev->event_lock);
  771. return 0;
  772. }
  773. }
  774. return -EINVAL;
  775. }
  776. static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
  777. void __user *p, int compat_mode)
  778. {
  779. struct evdev_client *client = file->private_data;
  780. struct evdev *evdev = client->evdev;
  781. int retval;
  782. retval = mutex_lock_interruptible(&evdev->mutex);
  783. if (retval)
  784. return retval;
  785. if (!evdev->exist) {
  786. retval = -ENODEV;
  787. goto out;
  788. }
  789. retval = evdev_do_ioctl(file, cmd, p, compat_mode);
  790. out:
  791. mutex_unlock(&evdev->mutex);
  792. return retval;
  793. }
  794. static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  795. {
  796. return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
  797. }
  798. #ifdef CONFIG_COMPAT
  799. static long evdev_ioctl_compat(struct file *file,
  800. unsigned int cmd, unsigned long arg)
  801. {
  802. return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
  803. }
  804. #endif
  805. static const struct file_operations evdev_fops = {
  806. .owner = THIS_MODULE,
  807. .read = evdev_read,
  808. .write = evdev_write,
  809. .poll = evdev_poll,
  810. .open = evdev_open,
  811. .release = evdev_release,
  812. .unlocked_ioctl = evdev_ioctl,
  813. #ifdef CONFIG_COMPAT
  814. .compat_ioctl = evdev_ioctl_compat,
  815. #endif
  816. .fasync = evdev_fasync,
  817. .flush = evdev_flush,
  818. .llseek = no_llseek,
  819. };
  820. static int evdev_install_chrdev(struct evdev *evdev)
  821. {
  822. /*
  823. * No need to do any locking here as calls to connect and
  824. * disconnect are serialized by the input core
  825. */
  826. evdev_table[evdev->minor] = evdev;
  827. return 0;
  828. }
  829. static void evdev_remove_chrdev(struct evdev *evdev)
  830. {
  831. /*
  832. * Lock evdev table to prevent race with evdev_open()
  833. */
  834. mutex_lock(&evdev_table_mutex);
  835. evdev_table[evdev->minor] = NULL;
  836. mutex_unlock(&evdev_table_mutex);
  837. }
  838. /*
  839. * Mark device non-existent. This disables writes, ioctls and
  840. * prevents new users from opening the device. Already posted
  841. * blocking reads will stay, however new ones will fail.
  842. */
  843. static void evdev_mark_dead(struct evdev *evdev)
  844. {
  845. mutex_lock(&evdev->mutex);
  846. evdev->exist = false;
  847. mutex_unlock(&evdev->mutex);
  848. }
  849. static void evdev_cleanup(struct evdev *evdev)
  850. {
  851. struct input_handle *handle = &evdev->handle;
  852. evdev_mark_dead(evdev);
  853. evdev_hangup(evdev);
  854. evdev_remove_chrdev(evdev);
  855. /* evdev is marked dead so no one else accesses evdev->open */
  856. if (evdev->open) {
  857. input_flush_device(handle, NULL);
  858. input_close_device(handle);
  859. }
  860. }
  861. /*
  862. * Create new evdev device. Note that input core serializes calls
  863. * to connect and disconnect so we don't need to lock evdev_table here.
  864. */
  865. static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
  866. const struct input_device_id *id)
  867. {
  868. struct evdev *evdev;
  869. int minor;
  870. int error;
  871. for (minor = 0; minor < EVDEV_MINORS; minor++)
  872. if (!evdev_table[minor])
  873. break;
  874. if (minor == EVDEV_MINORS) {
  875. pr_err("no more free evdev devices\n");
  876. return -ENFILE;
  877. }
  878. evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
  879. if (!evdev)
  880. return -ENOMEM;
  881. INIT_LIST_HEAD(&evdev->client_list);
  882. spin_lock_init(&evdev->client_lock);
  883. mutex_init(&evdev->mutex);
  884. init_waitqueue_head(&evdev->wait);
  885. dev_set_name(&evdev->dev, "event%d", minor);
  886. evdev->exist = true;
  887. evdev->minor = minor;
  888. evdev->hw_ts_sec = -1;
  889. evdev->hw_ts_nsec = -1;
  890. evdev->handle.dev = input_get_device(dev);
  891. evdev->handle.name = dev_name(&evdev->dev);
  892. evdev->handle.handler = handler;
  893. evdev->handle.private = evdev;
  894. evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
  895. evdev->dev.class = &input_class;
  896. evdev->dev.parent = &dev->dev;
  897. evdev->dev.release = evdev_free;
  898. device_initialize(&evdev->dev);
  899. error = input_register_handle(&evdev->handle);
  900. if (error)
  901. goto err_free_evdev;
  902. error = evdev_install_chrdev(evdev);
  903. if (error)
  904. goto err_unregister_handle;
  905. error = device_add(&evdev->dev);
  906. if (error)
  907. goto err_cleanup_evdev;
  908. return 0;
  909. err_cleanup_evdev:
  910. evdev_cleanup(evdev);
  911. err_unregister_handle:
  912. input_unregister_handle(&evdev->handle);
  913. err_free_evdev:
  914. put_device(&evdev->dev);
  915. return error;
  916. }
  917. static void evdev_disconnect(struct input_handle *handle)
  918. {
  919. struct evdev *evdev = handle->private;
  920. device_del(&evdev->dev);
  921. evdev_cleanup(evdev);
  922. input_unregister_handle(handle);
  923. put_device(&evdev->dev);
  924. }
  925. static const struct input_device_id evdev_ids[] = {
  926. { .driver_info = 1 }, /* Matches all devices */
  927. { }, /* Terminating zero entry */
  928. };
  929. MODULE_DEVICE_TABLE(input, evdev_ids);
  930. static struct input_handler evdev_handler = {
  931. .event = evdev_event,
  932. .events = evdev_events,
  933. .connect = evdev_connect,
  934. .disconnect = evdev_disconnect,
  935. .fops = &evdev_fops,
  936. .minor = EVDEV_MINOR_BASE,
  937. .name = "evdev",
  938. .id_table = evdev_ids,
  939. };
  940. static int __init evdev_init(void)
  941. {
  942. return input_register_handler(&evdev_handler);
  943. }
  944. static void __exit evdev_exit(void)
  945. {
  946. input_unregister_handler(&evdev_handler);
  947. }
  948. module_init(evdev_init);
  949. module_exit(evdev_exit);
  950. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  951. MODULE_DESCRIPTION("Input driver event char devices");
  952. MODULE_LICENSE("GPL");