0001
0002
0003
0004
0005
0006
0007
0008
0009 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0010
0011 #include <asm/io.h>
0012 #include <linux/delay.h>
0013 #include <linux/errno.h>
0014 #include <linux/joystick.h>
0015 #include <linux/input.h>
0016 #include <linux/kernel.h>
0017 #include <linux/major.h>
0018 #include <linux/sched.h>
0019 #include <linux/slab.h>
0020 #include <linux/mm.h>
0021 #include <linux/module.h>
0022 #include <linux/poll.h>
0023 #include <linux/init.h>
0024 #include <linux/device.h>
0025 #include <linux/cdev.h>
0026
0027 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
0028 MODULE_DESCRIPTION("Joystick device interfaces");
0029 MODULE_LICENSE("GPL");
0030
0031 #define JOYDEV_MINOR_BASE 0
0032 #define JOYDEV_MINORS 16
0033 #define JOYDEV_BUFFER_SIZE 64
0034
0035 struct joydev {
0036 int open;
0037 struct input_handle handle;
0038 wait_queue_head_t wait;
0039 struct list_head client_list;
0040 spinlock_t client_lock;
0041 struct mutex mutex;
0042 struct device dev;
0043 struct cdev cdev;
0044 bool exist;
0045
0046 struct js_corr corr[ABS_CNT];
0047 struct JS_DATA_SAVE_TYPE glue;
0048 int nabs;
0049 int nkey;
0050 __u16 keymap[KEY_MAX - BTN_MISC + 1];
0051 __u16 keypam[KEY_MAX - BTN_MISC + 1];
0052 __u8 absmap[ABS_CNT];
0053 __u8 abspam[ABS_CNT];
0054 __s16 abs[ABS_CNT];
0055 };
0056
0057 struct joydev_client {
0058 struct js_event buffer[JOYDEV_BUFFER_SIZE];
0059 int head;
0060 int tail;
0061 int startup;
0062 spinlock_t buffer_lock;
0063 struct fasync_struct *fasync;
0064 struct joydev *joydev;
0065 struct list_head node;
0066 };
0067
0068 static int joydev_correct(int value, struct js_corr *corr)
0069 {
0070 switch (corr->type) {
0071
0072 case JS_CORR_NONE:
0073 break;
0074
0075 case JS_CORR_BROKEN:
0076 value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 :
0077 ((corr->coef[3] * (value - corr->coef[1])) >> 14)) :
0078 ((corr->coef[2] * (value - corr->coef[0])) >> 14);
0079 break;
0080
0081 default:
0082 return 0;
0083 }
0084
0085 return clamp(value, -32767, 32767);
0086 }
0087
0088 static void joydev_pass_event(struct joydev_client *client,
0089 struct js_event *event)
0090 {
0091 struct joydev *joydev = client->joydev;
0092
0093
0094
0095
0096 spin_lock(&client->buffer_lock);
0097
0098 client->buffer[client->head] = *event;
0099
0100 if (client->startup == joydev->nabs + joydev->nkey) {
0101 client->head++;
0102 client->head &= JOYDEV_BUFFER_SIZE - 1;
0103 if (client->tail == client->head)
0104 client->startup = 0;
0105 }
0106
0107 spin_unlock(&client->buffer_lock);
0108
0109 kill_fasync(&client->fasync, SIGIO, POLL_IN);
0110 }
0111
0112 static void joydev_event(struct input_handle *handle,
0113 unsigned int type, unsigned int code, int value)
0114 {
0115 struct joydev *joydev = handle->private;
0116 struct joydev_client *client;
0117 struct js_event event;
0118
0119 switch (type) {
0120
0121 case EV_KEY:
0122 if (code < BTN_MISC || value == 2)
0123 return;
0124 event.type = JS_EVENT_BUTTON;
0125 event.number = joydev->keymap[code - BTN_MISC];
0126 event.value = value;
0127 break;
0128
0129 case EV_ABS:
0130 event.type = JS_EVENT_AXIS;
0131 event.number = joydev->absmap[code];
0132 event.value = joydev_correct(value,
0133 &joydev->corr[event.number]);
0134 if (event.value == joydev->abs[event.number])
0135 return;
0136 joydev->abs[event.number] = event.value;
0137 break;
0138
0139 default:
0140 return;
0141 }
0142
0143 event.time = jiffies_to_msecs(jiffies);
0144
0145 rcu_read_lock();
0146 list_for_each_entry_rcu(client, &joydev->client_list, node)
0147 joydev_pass_event(client, &event);
0148 rcu_read_unlock();
0149
0150 wake_up_interruptible(&joydev->wait);
0151 }
0152
0153 static int joydev_fasync(int fd, struct file *file, int on)
0154 {
0155 struct joydev_client *client = file->private_data;
0156
0157 return fasync_helper(fd, file, on, &client->fasync);
0158 }
0159
0160 static void joydev_free(struct device *dev)
0161 {
0162 struct joydev *joydev = container_of(dev, struct joydev, dev);
0163
0164 input_put_device(joydev->handle.dev);
0165 kfree(joydev);
0166 }
0167
0168 static void joydev_attach_client(struct joydev *joydev,
0169 struct joydev_client *client)
0170 {
0171 spin_lock(&joydev->client_lock);
0172 list_add_tail_rcu(&client->node, &joydev->client_list);
0173 spin_unlock(&joydev->client_lock);
0174 }
0175
0176 static void joydev_detach_client(struct joydev *joydev,
0177 struct joydev_client *client)
0178 {
0179 spin_lock(&joydev->client_lock);
0180 list_del_rcu(&client->node);
0181 spin_unlock(&joydev->client_lock);
0182 synchronize_rcu();
0183 }
0184
0185 static void joydev_refresh_state(struct joydev *joydev)
0186 {
0187 struct input_dev *dev = joydev->handle.dev;
0188 int i, val;
0189
0190 for (i = 0; i < joydev->nabs; i++) {
0191 val = input_abs_get_val(dev, joydev->abspam[i]);
0192 joydev->abs[i] = joydev_correct(val, &joydev->corr[i]);
0193 }
0194 }
0195
0196 static int joydev_open_device(struct joydev *joydev)
0197 {
0198 int retval;
0199
0200 retval = mutex_lock_interruptible(&joydev->mutex);
0201 if (retval)
0202 return retval;
0203
0204 if (!joydev->exist)
0205 retval = -ENODEV;
0206 else if (!joydev->open++) {
0207 retval = input_open_device(&joydev->handle);
0208 if (retval)
0209 joydev->open--;
0210 else
0211 joydev_refresh_state(joydev);
0212 }
0213
0214 mutex_unlock(&joydev->mutex);
0215 return retval;
0216 }
0217
0218 static void joydev_close_device(struct joydev *joydev)
0219 {
0220 mutex_lock(&joydev->mutex);
0221
0222 if (joydev->exist && !--joydev->open)
0223 input_close_device(&joydev->handle);
0224
0225 mutex_unlock(&joydev->mutex);
0226 }
0227
0228
0229
0230
0231
0232 static void joydev_hangup(struct joydev *joydev)
0233 {
0234 struct joydev_client *client;
0235
0236 spin_lock(&joydev->client_lock);
0237 list_for_each_entry(client, &joydev->client_list, node)
0238 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
0239 spin_unlock(&joydev->client_lock);
0240
0241 wake_up_interruptible(&joydev->wait);
0242 }
0243
0244 static int joydev_release(struct inode *inode, struct file *file)
0245 {
0246 struct joydev_client *client = file->private_data;
0247 struct joydev *joydev = client->joydev;
0248
0249 joydev_detach_client(joydev, client);
0250 kfree(client);
0251
0252 joydev_close_device(joydev);
0253
0254 return 0;
0255 }
0256
0257 static int joydev_open(struct inode *inode, struct file *file)
0258 {
0259 struct joydev *joydev =
0260 container_of(inode->i_cdev, struct joydev, cdev);
0261 struct joydev_client *client;
0262 int error;
0263
0264 client = kzalloc(sizeof(struct joydev_client), GFP_KERNEL);
0265 if (!client)
0266 return -ENOMEM;
0267
0268 spin_lock_init(&client->buffer_lock);
0269 client->joydev = joydev;
0270 joydev_attach_client(joydev, client);
0271
0272 error = joydev_open_device(joydev);
0273 if (error)
0274 goto err_free_client;
0275
0276 file->private_data = client;
0277 stream_open(inode, file);
0278
0279 return 0;
0280
0281 err_free_client:
0282 joydev_detach_client(joydev, client);
0283 kfree(client);
0284 return error;
0285 }
0286
0287 static int joydev_generate_startup_event(struct joydev_client *client,
0288 struct input_dev *input,
0289 struct js_event *event)
0290 {
0291 struct joydev *joydev = client->joydev;
0292 int have_event;
0293
0294 spin_lock_irq(&client->buffer_lock);
0295
0296 have_event = client->startup < joydev->nabs + joydev->nkey;
0297
0298 if (have_event) {
0299
0300 event->time = jiffies_to_msecs(jiffies);
0301 if (client->startup < joydev->nkey) {
0302 event->type = JS_EVENT_BUTTON | JS_EVENT_INIT;
0303 event->number = client->startup;
0304 event->value = !!test_bit(joydev->keypam[event->number],
0305 input->key);
0306 } else {
0307 event->type = JS_EVENT_AXIS | JS_EVENT_INIT;
0308 event->number = client->startup - joydev->nkey;
0309 event->value = joydev->abs[event->number];
0310 }
0311 client->startup++;
0312 }
0313
0314 spin_unlock_irq(&client->buffer_lock);
0315
0316 return have_event;
0317 }
0318
0319 static int joydev_fetch_next_event(struct joydev_client *client,
0320 struct js_event *event)
0321 {
0322 int have_event;
0323
0324 spin_lock_irq(&client->buffer_lock);
0325
0326 have_event = client->head != client->tail;
0327 if (have_event) {
0328 *event = client->buffer[client->tail++];
0329 client->tail &= JOYDEV_BUFFER_SIZE - 1;
0330 }
0331
0332 spin_unlock_irq(&client->buffer_lock);
0333
0334 return have_event;
0335 }
0336
0337
0338
0339
0340 static ssize_t joydev_0x_read(struct joydev_client *client,
0341 struct input_dev *input,
0342 char __user *buf)
0343 {
0344 struct joydev *joydev = client->joydev;
0345 struct JS_DATA_TYPE data;
0346 int i;
0347
0348 spin_lock_irq(&input->event_lock);
0349
0350
0351
0352
0353 for (data.buttons = i = 0; i < 32 && i < joydev->nkey; i++)
0354 data.buttons |=
0355 test_bit(joydev->keypam[i], input->key) ? (1 << i) : 0;
0356 data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x;
0357 data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y;
0358
0359
0360
0361
0362 spin_lock(&client->buffer_lock);
0363 client->startup = 0;
0364 client->tail = client->head;
0365 spin_unlock(&client->buffer_lock);
0366
0367 spin_unlock_irq(&input->event_lock);
0368
0369 if (copy_to_user(buf, &data, sizeof(struct JS_DATA_TYPE)))
0370 return -EFAULT;
0371
0372 return sizeof(struct JS_DATA_TYPE);
0373 }
0374
0375 static inline int joydev_data_pending(struct joydev_client *client)
0376 {
0377 struct joydev *joydev = client->joydev;
0378
0379 return client->startup < joydev->nabs + joydev->nkey ||
0380 client->head != client->tail;
0381 }
0382
0383 static ssize_t joydev_read(struct file *file, char __user *buf,
0384 size_t count, loff_t *ppos)
0385 {
0386 struct joydev_client *client = file->private_data;
0387 struct joydev *joydev = client->joydev;
0388 struct input_dev *input = joydev->handle.dev;
0389 struct js_event event;
0390 int retval;
0391
0392 if (!joydev->exist)
0393 return -ENODEV;
0394
0395 if (count < sizeof(struct js_event))
0396 return -EINVAL;
0397
0398 if (count == sizeof(struct JS_DATA_TYPE))
0399 return joydev_0x_read(client, input, buf);
0400
0401 if (!joydev_data_pending(client) && (file->f_flags & O_NONBLOCK))
0402 return -EAGAIN;
0403
0404 retval = wait_event_interruptible(joydev->wait,
0405 !joydev->exist || joydev_data_pending(client));
0406 if (retval)
0407 return retval;
0408
0409 if (!joydev->exist)
0410 return -ENODEV;
0411
0412 while (retval + sizeof(struct js_event) <= count &&
0413 joydev_generate_startup_event(client, input, &event)) {
0414
0415 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
0416 return -EFAULT;
0417
0418 retval += sizeof(struct js_event);
0419 }
0420
0421 while (retval + sizeof(struct js_event) <= count &&
0422 joydev_fetch_next_event(client, &event)) {
0423
0424 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
0425 return -EFAULT;
0426
0427 retval += sizeof(struct js_event);
0428 }
0429
0430 return retval;
0431 }
0432
0433
0434 static __poll_t joydev_poll(struct file *file, poll_table *wait)
0435 {
0436 struct joydev_client *client = file->private_data;
0437 struct joydev *joydev = client->joydev;
0438
0439 poll_wait(file, &joydev->wait, wait);
0440 return (joydev_data_pending(client) ? (EPOLLIN | EPOLLRDNORM) : 0) |
0441 (joydev->exist ? 0 : (EPOLLHUP | EPOLLERR));
0442 }
0443
0444 static int joydev_handle_JSIOCSAXMAP(struct joydev *joydev,
0445 void __user *argp, size_t len)
0446 {
0447 __u8 *abspam;
0448 int i;
0449 int retval = 0;
0450
0451 len = min(len, sizeof(joydev->abspam));
0452
0453
0454 abspam = memdup_user(argp, len);
0455 if (IS_ERR(abspam))
0456 return PTR_ERR(abspam);
0457
0458 for (i = 0; i < len && i < joydev->nabs; i++) {
0459 if (abspam[i] > ABS_MAX) {
0460 retval = -EINVAL;
0461 goto out;
0462 }
0463 }
0464
0465 memcpy(joydev->abspam, abspam, len);
0466
0467 for (i = 0; i < joydev->nabs; i++)
0468 joydev->absmap[joydev->abspam[i]] = i;
0469
0470 out:
0471 kfree(abspam);
0472 return retval;
0473 }
0474
0475 static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev,
0476 void __user *argp, size_t len)
0477 {
0478 __u16 *keypam;
0479 int i;
0480 int retval = 0;
0481
0482 if (len % sizeof(*keypam))
0483 return -EINVAL;
0484
0485 len = min(len, sizeof(joydev->keypam));
0486
0487
0488 keypam = memdup_user(argp, len);
0489 if (IS_ERR(keypam))
0490 return PTR_ERR(keypam);
0491
0492 for (i = 0; i < (len / 2) && i < joydev->nkey; i++) {
0493 if (keypam[i] > KEY_MAX || keypam[i] < BTN_MISC) {
0494 retval = -EINVAL;
0495 goto out;
0496 }
0497 }
0498
0499 memcpy(joydev->keypam, keypam, len);
0500
0501 for (i = 0; i < joydev->nkey; i++)
0502 joydev->keymap[joydev->keypam[i] - BTN_MISC] = i;
0503
0504 out:
0505 kfree(keypam);
0506 return retval;
0507 }
0508
0509
0510 static int joydev_ioctl_common(struct joydev *joydev,
0511 unsigned int cmd, void __user *argp)
0512 {
0513 struct input_dev *dev = joydev->handle.dev;
0514 size_t len;
0515 int i;
0516 const char *name;
0517
0518
0519 switch (cmd) {
0520
0521 case JS_SET_CAL:
0522 return copy_from_user(&joydev->glue.JS_CORR, argp,
0523 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
0524
0525 case JS_GET_CAL:
0526 return copy_to_user(argp, &joydev->glue.JS_CORR,
0527 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
0528
0529 case JS_SET_TIMEOUT:
0530 return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
0531
0532 case JS_GET_TIMEOUT:
0533 return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
0534
0535 case JSIOCGVERSION:
0536 return put_user(JS_VERSION, (__u32 __user *) argp);
0537
0538 case JSIOCGAXES:
0539 return put_user(joydev->nabs, (__u8 __user *) argp);
0540
0541 case JSIOCGBUTTONS:
0542 return put_user(joydev->nkey, (__u8 __user *) argp);
0543
0544 case JSIOCSCORR:
0545 if (copy_from_user(joydev->corr, argp,
0546 sizeof(joydev->corr[0]) * joydev->nabs))
0547 return -EFAULT;
0548
0549 for (i = 0; i < joydev->nabs; i++) {
0550 int val = input_abs_get_val(dev, joydev->abspam[i]);
0551 joydev->abs[i] = joydev_correct(val, &joydev->corr[i]);
0552 }
0553 return 0;
0554
0555 case JSIOCGCORR:
0556 return copy_to_user(argp, joydev->corr,
0557 sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0;
0558
0559 }
0560
0561
0562
0563
0564
0565
0566 switch (cmd & ~IOCSIZE_MASK) {
0567
0568 case (JSIOCSAXMAP & ~IOCSIZE_MASK):
0569 return joydev_handle_JSIOCSAXMAP(joydev, argp, _IOC_SIZE(cmd));
0570
0571 case (JSIOCGAXMAP & ~IOCSIZE_MASK):
0572 len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->abspam));
0573 return copy_to_user(argp, joydev->abspam, len) ? -EFAULT : len;
0574
0575 case (JSIOCSBTNMAP & ~IOCSIZE_MASK):
0576 return joydev_handle_JSIOCSBTNMAP(joydev, argp, _IOC_SIZE(cmd));
0577
0578 case (JSIOCGBTNMAP & ~IOCSIZE_MASK):
0579 len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->keypam));
0580 return copy_to_user(argp, joydev->keypam, len) ? -EFAULT : len;
0581
0582 case JSIOCGNAME(0):
0583 name = dev->name;
0584 if (!name)
0585 return 0;
0586
0587 len = min_t(size_t, _IOC_SIZE(cmd), strlen(name) + 1);
0588 return copy_to_user(argp, name, len) ? -EFAULT : len;
0589 }
0590
0591 return -EINVAL;
0592 }
0593
0594 #ifdef CONFIG_COMPAT
0595 static long joydev_compat_ioctl(struct file *file,
0596 unsigned int cmd, unsigned long arg)
0597 {
0598 struct joydev_client *client = file->private_data;
0599 struct joydev *joydev = client->joydev;
0600 void __user *argp = (void __user *)arg;
0601 s32 tmp32;
0602 struct JS_DATA_SAVE_TYPE_32 ds32;
0603 int retval;
0604
0605 retval = mutex_lock_interruptible(&joydev->mutex);
0606 if (retval)
0607 return retval;
0608
0609 if (!joydev->exist) {
0610 retval = -ENODEV;
0611 goto out;
0612 }
0613
0614 switch (cmd) {
0615
0616 case JS_SET_TIMELIMIT:
0617 retval = get_user(tmp32, (s32 __user *) arg);
0618 if (retval == 0)
0619 joydev->glue.JS_TIMELIMIT = tmp32;
0620 break;
0621
0622 case JS_GET_TIMELIMIT:
0623 tmp32 = joydev->glue.JS_TIMELIMIT;
0624 retval = put_user(tmp32, (s32 __user *) arg);
0625 break;
0626
0627 case JS_SET_ALL:
0628 retval = copy_from_user(&ds32, argp,
0629 sizeof(ds32)) ? -EFAULT : 0;
0630 if (retval == 0) {
0631 joydev->glue.JS_TIMEOUT = ds32.JS_TIMEOUT;
0632 joydev->glue.BUSY = ds32.BUSY;
0633 joydev->glue.JS_EXPIRETIME = ds32.JS_EXPIRETIME;
0634 joydev->glue.JS_TIMELIMIT = ds32.JS_TIMELIMIT;
0635 joydev->glue.JS_SAVE = ds32.JS_SAVE;
0636 joydev->glue.JS_CORR = ds32.JS_CORR;
0637 }
0638 break;
0639
0640 case JS_GET_ALL:
0641 ds32.JS_TIMEOUT = joydev->glue.JS_TIMEOUT;
0642 ds32.BUSY = joydev->glue.BUSY;
0643 ds32.JS_EXPIRETIME = joydev->glue.JS_EXPIRETIME;
0644 ds32.JS_TIMELIMIT = joydev->glue.JS_TIMELIMIT;
0645 ds32.JS_SAVE = joydev->glue.JS_SAVE;
0646 ds32.JS_CORR = joydev->glue.JS_CORR;
0647
0648 retval = copy_to_user(argp, &ds32, sizeof(ds32)) ? -EFAULT : 0;
0649 break;
0650
0651 default:
0652 retval = joydev_ioctl_common(joydev, cmd, argp);
0653 break;
0654 }
0655
0656 out:
0657 mutex_unlock(&joydev->mutex);
0658 return retval;
0659 }
0660 #endif
0661
0662 static long joydev_ioctl(struct file *file,
0663 unsigned int cmd, unsigned long arg)
0664 {
0665 struct joydev_client *client = file->private_data;
0666 struct joydev *joydev = client->joydev;
0667 void __user *argp = (void __user *)arg;
0668 int retval;
0669
0670 retval = mutex_lock_interruptible(&joydev->mutex);
0671 if (retval)
0672 return retval;
0673
0674 if (!joydev->exist) {
0675 retval = -ENODEV;
0676 goto out;
0677 }
0678
0679 switch (cmd) {
0680
0681 case JS_SET_TIMELIMIT:
0682 retval = get_user(joydev->glue.JS_TIMELIMIT,
0683 (long __user *) arg);
0684 break;
0685
0686 case JS_GET_TIMELIMIT:
0687 retval = put_user(joydev->glue.JS_TIMELIMIT,
0688 (long __user *) arg);
0689 break;
0690
0691 case JS_SET_ALL:
0692 retval = copy_from_user(&joydev->glue, argp,
0693 sizeof(joydev->glue)) ? -EFAULT : 0;
0694 break;
0695
0696 case JS_GET_ALL:
0697 retval = copy_to_user(argp, &joydev->glue,
0698 sizeof(joydev->glue)) ? -EFAULT : 0;
0699 break;
0700
0701 default:
0702 retval = joydev_ioctl_common(joydev, cmd, argp);
0703 break;
0704 }
0705 out:
0706 mutex_unlock(&joydev->mutex);
0707 return retval;
0708 }
0709
0710 static const struct file_operations joydev_fops = {
0711 .owner = THIS_MODULE,
0712 .read = joydev_read,
0713 .poll = joydev_poll,
0714 .open = joydev_open,
0715 .release = joydev_release,
0716 .unlocked_ioctl = joydev_ioctl,
0717 #ifdef CONFIG_COMPAT
0718 .compat_ioctl = joydev_compat_ioctl,
0719 #endif
0720 .fasync = joydev_fasync,
0721 .llseek = no_llseek,
0722 };
0723
0724
0725
0726
0727
0728
0729 static void joydev_mark_dead(struct joydev *joydev)
0730 {
0731 mutex_lock(&joydev->mutex);
0732 joydev->exist = false;
0733 mutex_unlock(&joydev->mutex);
0734 }
0735
0736 static void joydev_cleanup(struct joydev *joydev)
0737 {
0738 struct input_handle *handle = &joydev->handle;
0739
0740 joydev_mark_dead(joydev);
0741 joydev_hangup(joydev);
0742
0743
0744 if (joydev->open)
0745 input_close_device(handle);
0746 }
0747
0748
0749
0750
0751
0752 #define USB_VENDOR_ID_SONY 0x054c
0753 #define USB_DEVICE_ID_SONY_PS3_CONTROLLER 0x0268
0754 #define USB_DEVICE_ID_SONY_PS4_CONTROLLER 0x05c4
0755 #define USB_DEVICE_ID_SONY_PS4_CONTROLLER_2 0x09cc
0756 #define USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE 0x0ba0
0757
0758 #define USB_VENDOR_ID_THQ 0x20d6
0759 #define USB_DEVICE_ID_THQ_PS3_UDRAW 0xcb17
0760
0761 #define USB_VENDOR_ID_NINTENDO 0x057e
0762 #define USB_DEVICE_ID_NINTENDO_JOYCONL 0x2006
0763 #define USB_DEVICE_ID_NINTENDO_JOYCONR 0x2007
0764 #define USB_DEVICE_ID_NINTENDO_PROCON 0x2009
0765 #define USB_DEVICE_ID_NINTENDO_CHRGGRIP 0x200E
0766
0767 #define ACCEL_DEV(vnd, prd) \
0768 { \
0769 .flags = INPUT_DEVICE_ID_MATCH_VENDOR | \
0770 INPUT_DEVICE_ID_MATCH_PRODUCT | \
0771 INPUT_DEVICE_ID_MATCH_PROPBIT, \
0772 .vendor = (vnd), \
0773 .product = (prd), \
0774 .propbit = { BIT_MASK(INPUT_PROP_ACCELEROMETER) }, \
0775 }
0776
0777 static const struct input_device_id joydev_blacklist[] = {
0778
0779 {
0780 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
0781 INPUT_DEVICE_ID_MATCH_KEYBIT,
0782 .evbit = { BIT_MASK(EV_KEY) },
0783 .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
0784 },
0785
0786 {
0787 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
0788 INPUT_DEVICE_ID_MATCH_KEYBIT,
0789 .evbit = { BIT_MASK(EV_KEY) },
0790 .keybit = { [BIT_WORD(BTN_DIGI)] = BIT_MASK(BTN_DIGI) },
0791 },
0792
0793 ACCEL_DEV(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
0794 ACCEL_DEV(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
0795 ACCEL_DEV(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2),
0796 ACCEL_DEV(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE),
0797 ACCEL_DEV(USB_VENDOR_ID_THQ, USB_DEVICE_ID_THQ_PS3_UDRAW),
0798 ACCEL_DEV(USB_VENDOR_ID_NINTENDO, USB_DEVICE_ID_NINTENDO_PROCON),
0799 ACCEL_DEV(USB_VENDOR_ID_NINTENDO, USB_DEVICE_ID_NINTENDO_CHRGGRIP),
0800 ACCEL_DEV(USB_VENDOR_ID_NINTENDO, USB_DEVICE_ID_NINTENDO_JOYCONL),
0801 ACCEL_DEV(USB_VENDOR_ID_NINTENDO, USB_DEVICE_ID_NINTENDO_JOYCONR),
0802 { }
0803 };
0804
0805 static bool joydev_dev_is_blacklisted(struct input_dev *dev)
0806 {
0807 const struct input_device_id *id;
0808
0809 for (id = joydev_blacklist; id->flags; id++) {
0810 if (input_match_device_id(dev, id)) {
0811 dev_dbg(&dev->dev,
0812 "joydev: blacklisting '%s'\n", dev->name);
0813 return true;
0814 }
0815 }
0816
0817 return false;
0818 }
0819
0820 static bool joydev_dev_is_absolute_mouse(struct input_dev *dev)
0821 {
0822 DECLARE_BITMAP(jd_scratch, KEY_CNT);
0823 bool ev_match = false;
0824
0825 BUILD_BUG_ON(ABS_CNT > KEY_CNT || EV_CNT > KEY_CNT);
0826
0827
0828
0829
0830
0831
0832
0833
0834
0835
0836
0837
0838
0839
0840
0841
0842
0843
0844
0845
0846
0847
0848
0849
0850
0851
0852
0853 bitmap_zero(jd_scratch, EV_CNT);
0854
0855 __set_bit(EV_ABS, jd_scratch);
0856 __set_bit(EV_KEY, jd_scratch);
0857 __set_bit(EV_SYN, jd_scratch);
0858 if (bitmap_equal(jd_scratch, dev->evbit, EV_CNT))
0859 ev_match = true;
0860
0861
0862 __set_bit(EV_MSC, jd_scratch);
0863 if (bitmap_equal(jd_scratch, dev->evbit, EV_CNT))
0864 ev_match = true;
0865
0866
0867 __set_bit(EV_REL, jd_scratch);
0868 if (bitmap_equal(jd_scratch, dev->evbit, EV_CNT))
0869 ev_match = true;
0870
0871 if (!ev_match)
0872 return false;
0873
0874 bitmap_zero(jd_scratch, ABS_CNT);
0875 __set_bit(ABS_X, jd_scratch);
0876 __set_bit(ABS_Y, jd_scratch);
0877 if (!bitmap_equal(dev->absbit, jd_scratch, ABS_CNT))
0878 return false;
0879
0880 bitmap_zero(jd_scratch, KEY_CNT);
0881 __set_bit(BTN_LEFT, jd_scratch);
0882 __set_bit(BTN_RIGHT, jd_scratch);
0883 __set_bit(BTN_MIDDLE, jd_scratch);
0884
0885 if (!bitmap_equal(dev->keybit, jd_scratch, KEY_CNT))
0886 return false;
0887
0888
0889
0890
0891
0892 if (dev->id.bustype == BUS_AMIGA)
0893 return false;
0894
0895 return true;
0896 }
0897
0898 static bool joydev_match(struct input_handler *handler, struct input_dev *dev)
0899 {
0900
0901 if (joydev_dev_is_blacklisted(dev))
0902 return false;
0903
0904
0905 if (joydev_dev_is_absolute_mouse(dev))
0906 return false;
0907
0908 return true;
0909 }
0910
0911 static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
0912 const struct input_device_id *id)
0913 {
0914 struct joydev *joydev;
0915 int i, j, t, minor, dev_no;
0916 int error;
0917
0918 minor = input_get_new_minor(JOYDEV_MINOR_BASE, JOYDEV_MINORS, true);
0919 if (minor < 0) {
0920 error = minor;
0921 pr_err("failed to reserve new minor: %d\n", error);
0922 return error;
0923 }
0924
0925 joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL);
0926 if (!joydev) {
0927 error = -ENOMEM;
0928 goto err_free_minor;
0929 }
0930
0931 INIT_LIST_HEAD(&joydev->client_list);
0932 spin_lock_init(&joydev->client_lock);
0933 mutex_init(&joydev->mutex);
0934 init_waitqueue_head(&joydev->wait);
0935 joydev->exist = true;
0936
0937 dev_no = minor;
0938
0939 if (dev_no < JOYDEV_MINOR_BASE + JOYDEV_MINORS)
0940 dev_no -= JOYDEV_MINOR_BASE;
0941 dev_set_name(&joydev->dev, "js%d", dev_no);
0942
0943 joydev->handle.dev = input_get_device(dev);
0944 joydev->handle.name = dev_name(&joydev->dev);
0945 joydev->handle.handler = handler;
0946 joydev->handle.private = joydev;
0947
0948 for_each_set_bit(i, dev->absbit, ABS_CNT) {
0949 joydev->absmap[i] = joydev->nabs;
0950 joydev->abspam[joydev->nabs] = i;
0951 joydev->nabs++;
0952 }
0953
0954 for (i = BTN_JOYSTICK - BTN_MISC; i < KEY_MAX - BTN_MISC + 1; i++)
0955 if (test_bit(i + BTN_MISC, dev->keybit)) {
0956 joydev->keymap[i] = joydev->nkey;
0957 joydev->keypam[joydev->nkey] = i + BTN_MISC;
0958 joydev->nkey++;
0959 }
0960
0961 for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++)
0962 if (test_bit(i + BTN_MISC, dev->keybit)) {
0963 joydev->keymap[i] = joydev->nkey;
0964 joydev->keypam[joydev->nkey] = i + BTN_MISC;
0965 joydev->nkey++;
0966 }
0967
0968 for (i = 0; i < joydev->nabs; i++) {
0969 j = joydev->abspam[i];
0970 if (input_abs_get_max(dev, j) == input_abs_get_min(dev, j)) {
0971 joydev->corr[i].type = JS_CORR_NONE;
0972 continue;
0973 }
0974 joydev->corr[i].type = JS_CORR_BROKEN;
0975 joydev->corr[i].prec = input_abs_get_fuzz(dev, j);
0976
0977 t = (input_abs_get_max(dev, j) + input_abs_get_min(dev, j)) / 2;
0978 joydev->corr[i].coef[0] = t - input_abs_get_flat(dev, j);
0979 joydev->corr[i].coef[1] = t + input_abs_get_flat(dev, j);
0980
0981 t = (input_abs_get_max(dev, j) - input_abs_get_min(dev, j)) / 2
0982 - 2 * input_abs_get_flat(dev, j);
0983 if (t) {
0984 joydev->corr[i].coef[2] = (1 << 29) / t;
0985 joydev->corr[i].coef[3] = (1 << 29) / t;
0986 }
0987 }
0988
0989 joydev->dev.devt = MKDEV(INPUT_MAJOR, minor);
0990 joydev->dev.class = &input_class;
0991 joydev->dev.parent = &dev->dev;
0992 joydev->dev.release = joydev_free;
0993 device_initialize(&joydev->dev);
0994
0995 error = input_register_handle(&joydev->handle);
0996 if (error)
0997 goto err_free_joydev;
0998
0999 cdev_init(&joydev->cdev, &joydev_fops);
1000
1001 error = cdev_device_add(&joydev->cdev, &joydev->dev);
1002 if (error)
1003 goto err_cleanup_joydev;
1004
1005 return 0;
1006
1007 err_cleanup_joydev:
1008 joydev_cleanup(joydev);
1009 input_unregister_handle(&joydev->handle);
1010 err_free_joydev:
1011 put_device(&joydev->dev);
1012 err_free_minor:
1013 input_free_minor(minor);
1014 return error;
1015 }
1016
1017 static void joydev_disconnect(struct input_handle *handle)
1018 {
1019 struct joydev *joydev = handle->private;
1020
1021 cdev_device_del(&joydev->cdev, &joydev->dev);
1022 joydev_cleanup(joydev);
1023 input_free_minor(MINOR(joydev->dev.devt));
1024 input_unregister_handle(handle);
1025 put_device(&joydev->dev);
1026 }
1027
1028 static const struct input_device_id joydev_ids[] = {
1029 {
1030 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
1031 INPUT_DEVICE_ID_MATCH_ABSBIT,
1032 .evbit = { BIT_MASK(EV_ABS) },
1033 .absbit = { BIT_MASK(ABS_X) },
1034 },
1035 {
1036 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
1037 INPUT_DEVICE_ID_MATCH_ABSBIT,
1038 .evbit = { BIT_MASK(EV_ABS) },
1039 .absbit = { BIT_MASK(ABS_Z) },
1040 },
1041 {
1042 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
1043 INPUT_DEVICE_ID_MATCH_ABSBIT,
1044 .evbit = { BIT_MASK(EV_ABS) },
1045 .absbit = { BIT_MASK(ABS_WHEEL) },
1046 },
1047 {
1048 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
1049 INPUT_DEVICE_ID_MATCH_ABSBIT,
1050 .evbit = { BIT_MASK(EV_ABS) },
1051 .absbit = { BIT_MASK(ABS_THROTTLE) },
1052 },
1053 {
1054 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
1055 INPUT_DEVICE_ID_MATCH_KEYBIT,
1056 .evbit = { BIT_MASK(EV_KEY) },
1057 .keybit = {[BIT_WORD(BTN_JOYSTICK)] = BIT_MASK(BTN_JOYSTICK) },
1058 },
1059 {
1060 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
1061 INPUT_DEVICE_ID_MATCH_KEYBIT,
1062 .evbit = { BIT_MASK(EV_KEY) },
1063 .keybit = { [BIT_WORD(BTN_GAMEPAD)] = BIT_MASK(BTN_GAMEPAD) },
1064 },
1065 {
1066 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
1067 INPUT_DEVICE_ID_MATCH_KEYBIT,
1068 .evbit = { BIT_MASK(EV_KEY) },
1069 .keybit = { [BIT_WORD(BTN_TRIGGER_HAPPY)] = BIT_MASK(BTN_TRIGGER_HAPPY) },
1070 },
1071 { }
1072 };
1073
1074 MODULE_DEVICE_TABLE(input, joydev_ids);
1075
1076 static struct input_handler joydev_handler = {
1077 .event = joydev_event,
1078 .match = joydev_match,
1079 .connect = joydev_connect,
1080 .disconnect = joydev_disconnect,
1081 .legacy_minors = true,
1082 .minor = JOYDEV_MINOR_BASE,
1083 .name = "joydev",
1084 .id_table = joydev_ids,
1085 };
1086
1087 static int __init joydev_init(void)
1088 {
1089 return input_register_handler(&joydev_handler);
1090 }
1091
1092 static void __exit joydev_exit(void)
1093 {
1094 input_unregister_handler(&joydev_handler);
1095 }
1096
1097 module_init(joydev_init);
1098 module_exit(joydev_exit);