trancevibrator.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PlayStation 2 Trance Vibrator driver
  4. *
  5. * Copyright (C) 2006 Sam Hocevar <[email protected]>
  6. */
  7. /* Standard include files */
  8. #include <linux/kernel.h>
  9. #include <linux/errno.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/usb.h>
  13. #define DRIVER_AUTHOR "Sam Hocevar, [email protected]"
  14. #define DRIVER_DESC "PlayStation 2 Trance Vibrator driver"
  15. #define TRANCEVIBRATOR_VENDOR_ID 0x0b49 /* ASCII Corporation */
  16. #define TRANCEVIBRATOR_PRODUCT_ID 0x064f /* Trance Vibrator */
  17. static const struct usb_device_id id_table[] = {
  18. { USB_DEVICE(TRANCEVIBRATOR_VENDOR_ID, TRANCEVIBRATOR_PRODUCT_ID) },
  19. { },
  20. };
  21. MODULE_DEVICE_TABLE (usb, id_table);
  22. /* Driver-local specific stuff */
  23. struct trancevibrator {
  24. struct usb_device *udev;
  25. unsigned int speed;
  26. };
  27. static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
  28. char *buf)
  29. {
  30. struct usb_interface *intf = to_usb_interface(dev);
  31. struct trancevibrator *tv = usb_get_intfdata(intf);
  32. return sprintf(buf, "%d\n", tv->speed);
  33. }
  34. static ssize_t speed_store(struct device *dev, struct device_attribute *attr,
  35. const char *buf, size_t count)
  36. {
  37. struct usb_interface *intf = to_usb_interface(dev);
  38. struct trancevibrator *tv = usb_get_intfdata(intf);
  39. int temp, retval, old;
  40. retval = kstrtoint(buf, 10, &temp);
  41. if (retval)
  42. return retval;
  43. if (temp > 255)
  44. temp = 255;
  45. else if (temp < 0)
  46. temp = 0;
  47. old = tv->speed;
  48. tv->speed = temp;
  49. dev_dbg(&tv->udev->dev, "speed = %d\n", tv->speed);
  50. /* Set speed */
  51. retval = usb_control_msg(tv->udev, usb_sndctrlpipe(tv->udev, 0),
  52. 0x01, /* vendor request: set speed */
  53. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  54. tv->speed, /* speed value */
  55. 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
  56. if (retval) {
  57. tv->speed = old;
  58. dev_dbg(&tv->udev->dev, "retval = %d\n", retval);
  59. return retval;
  60. }
  61. return count;
  62. }
  63. static DEVICE_ATTR_RW(speed);
  64. static struct attribute *tv_attrs[] = {
  65. &dev_attr_speed.attr,
  66. NULL,
  67. };
  68. ATTRIBUTE_GROUPS(tv);
  69. static int tv_probe(struct usb_interface *interface,
  70. const struct usb_device_id *id)
  71. {
  72. struct usb_device *udev = interface_to_usbdev(interface);
  73. struct trancevibrator *dev;
  74. int retval;
  75. dev = kzalloc(sizeof(struct trancevibrator), GFP_KERNEL);
  76. if (!dev) {
  77. retval = -ENOMEM;
  78. goto error;
  79. }
  80. dev->udev = usb_get_dev(udev);
  81. usb_set_intfdata(interface, dev);
  82. return 0;
  83. error:
  84. kfree(dev);
  85. return retval;
  86. }
  87. static void tv_disconnect(struct usb_interface *interface)
  88. {
  89. struct trancevibrator *dev;
  90. dev = usb_get_intfdata (interface);
  91. usb_set_intfdata(interface, NULL);
  92. usb_put_dev(dev->udev);
  93. kfree(dev);
  94. }
  95. /* USB subsystem object */
  96. static struct usb_driver tv_driver = {
  97. .name = "trancevibrator",
  98. .probe = tv_probe,
  99. .disconnect = tv_disconnect,
  100. .id_table = id_table,
  101. .dev_groups = tv_groups,
  102. };
  103. module_usb_driver(tv_driver);
  104. MODULE_AUTHOR(DRIVER_AUTHOR);
  105. MODULE_DESCRIPTION(DRIVER_DESC);
  106. MODULE_LICENSE("GPL");