trancevibrator.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. 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_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  54. tv->speed, /* speed value */
  55. 0, NULL, 0, USB_CTRL_GET_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 int tv_probe(struct usb_interface *interface,
  65. const struct usb_device_id *id)
  66. {
  67. struct usb_device *udev = interface_to_usbdev(interface);
  68. struct trancevibrator *dev;
  69. int retval;
  70. dev = kzalloc(sizeof(struct trancevibrator), GFP_KERNEL);
  71. if (!dev) {
  72. retval = -ENOMEM;
  73. goto error;
  74. }
  75. dev->udev = usb_get_dev(udev);
  76. usb_set_intfdata(interface, dev);
  77. retval = device_create_file(&interface->dev, &dev_attr_speed);
  78. if (retval)
  79. goto error_create_file;
  80. return 0;
  81. error_create_file:
  82. usb_put_dev(udev);
  83. usb_set_intfdata(interface, NULL);
  84. error:
  85. kfree(dev);
  86. return retval;
  87. }
  88. static void tv_disconnect(struct usb_interface *interface)
  89. {
  90. struct trancevibrator *dev;
  91. dev = usb_get_intfdata (interface);
  92. device_remove_file(&interface->dev, &dev_attr_speed);
  93. usb_set_intfdata(interface, NULL);
  94. usb_put_dev(dev->udev);
  95. kfree(dev);
  96. }
  97. /* USB subsystem object */
  98. static struct usb_driver tv_driver = {
  99. .name = "trancevibrator",
  100. .probe = tv_probe,
  101. .disconnect = tv_disconnect,
  102. .id_table = id_table,
  103. };
  104. module_usb_driver(tv_driver);
  105. MODULE_AUTHOR(DRIVER_AUTHOR);
  106. MODULE_DESCRIPTION(DRIVER_DESC);
  107. MODULE_LICENSE("GPL");