trancevibrator.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PlayStation 2 Trance Vibrator driver
  4. *
  5. * Copyright (C) 2006 Sam Hocevar <sam@zoy.org>
  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, sam@zoy.org"
  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. temp = simple_strtoul(buf, NULL, 10);
  41. if (temp > 255)
  42. temp = 255;
  43. else if (temp < 0)
  44. temp = 0;
  45. old = tv->speed;
  46. tv->speed = temp;
  47. dev_dbg(&tv->udev->dev, "speed = %d\n", tv->speed);
  48. /* Set speed */
  49. retval = usb_control_msg(tv->udev, usb_sndctrlpipe(tv->udev, 0),
  50. 0x01, /* vendor request: set speed */
  51. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  52. tv->speed, /* speed value */
  53. 0, NULL, 0, USB_CTRL_GET_TIMEOUT);
  54. if (retval) {
  55. tv->speed = old;
  56. dev_dbg(&tv->udev->dev, "retval = %d\n", retval);
  57. return retval;
  58. }
  59. return count;
  60. }
  61. static DEVICE_ATTR_RW(speed);
  62. static int tv_probe(struct usb_interface *interface,
  63. const struct usb_device_id *id)
  64. {
  65. struct usb_device *udev = interface_to_usbdev(interface);
  66. struct trancevibrator *dev;
  67. int retval;
  68. dev = kzalloc(sizeof(struct trancevibrator), GFP_KERNEL);
  69. if (!dev) {
  70. retval = -ENOMEM;
  71. goto error;
  72. }
  73. dev->udev = usb_get_dev(udev);
  74. usb_set_intfdata(interface, dev);
  75. retval = device_create_file(&interface->dev, &dev_attr_speed);
  76. if (retval)
  77. goto error_create_file;
  78. return 0;
  79. error_create_file:
  80. usb_put_dev(udev);
  81. usb_set_intfdata(interface, NULL);
  82. error:
  83. kfree(dev);
  84. return retval;
  85. }
  86. static void tv_disconnect(struct usb_interface *interface)
  87. {
  88. struct trancevibrator *dev;
  89. dev = usb_get_intfdata (interface);
  90. device_remove_file(&interface->dev, &dev_attr_speed);
  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. };
  102. module_usb_driver(tv_driver);
  103. MODULE_AUTHOR(DRIVER_AUTHOR);
  104. MODULE_DESCRIPTION(DRIVER_DESC);
  105. MODULE_LICENSE("GPL");