trancevibrator.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * PlayStation 2 Trance Vibrator driver
  3. *
  4. * Copyright (C) 2006 Sam Hocevar <sam@zoy.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. /* Standard include files */
  21. #include <linux/kernel.h>
  22. #include <linux/errno.h>
  23. #include <linux/slab.h>
  24. #include <linux/module.h>
  25. #include <linux/usb.h>
  26. /* Version Information */
  27. #define DRIVER_VERSION "v1.1"
  28. #define DRIVER_AUTHOR "Sam Hocevar, sam@zoy.org"
  29. #define DRIVER_DESC "PlayStation 2 Trance Vibrator driver"
  30. #define TRANCEVIBRATOR_VENDOR_ID 0x0b49 /* ASCII Corporation */
  31. #define TRANCEVIBRATOR_PRODUCT_ID 0x064f /* Trance Vibrator */
  32. static const struct usb_device_id id_table[] = {
  33. { USB_DEVICE(TRANCEVIBRATOR_VENDOR_ID, TRANCEVIBRATOR_PRODUCT_ID) },
  34. { },
  35. };
  36. MODULE_DEVICE_TABLE (usb, id_table);
  37. /* Driver-local specific stuff */
  38. struct trancevibrator {
  39. struct usb_device *udev;
  40. unsigned int speed;
  41. };
  42. static ssize_t show_speed(struct device *dev, struct device_attribute *attr,
  43. char *buf)
  44. {
  45. struct usb_interface *intf = to_usb_interface(dev);
  46. struct trancevibrator *tv = usb_get_intfdata(intf);
  47. return sprintf(buf, "%d\n", tv->speed);
  48. }
  49. static ssize_t set_speed(struct device *dev, struct device_attribute *attr,
  50. const char *buf, size_t count)
  51. {
  52. struct usb_interface *intf = to_usb_interface(dev);
  53. struct trancevibrator *tv = usb_get_intfdata(intf);
  54. int temp, retval, old;
  55. temp = simple_strtoul(buf, NULL, 10);
  56. if (temp > 255)
  57. temp = 255;
  58. else if (temp < 0)
  59. temp = 0;
  60. old = tv->speed;
  61. tv->speed = temp;
  62. dev_dbg(&tv->udev->dev, "speed = %d\n", tv->speed);
  63. /* Set speed */
  64. retval = usb_control_msg(tv->udev, usb_sndctrlpipe(tv->udev, 0),
  65. 0x01, /* vendor request: set speed */
  66. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  67. tv->speed, /* speed value */
  68. 0, NULL, 0, USB_CTRL_GET_TIMEOUT);
  69. if (retval) {
  70. tv->speed = old;
  71. dev_dbg(&tv->udev->dev, "retval = %d\n", retval);
  72. return retval;
  73. }
  74. return count;
  75. }
  76. static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR, show_speed, set_speed);
  77. static int tv_probe(struct usb_interface *interface,
  78. const struct usb_device_id *id)
  79. {
  80. struct usb_device *udev = interface_to_usbdev(interface);
  81. struct trancevibrator *dev;
  82. int retval;
  83. dev = kzalloc(sizeof(struct trancevibrator), GFP_KERNEL);
  84. if (dev == NULL) {
  85. dev_err(&interface->dev, "Out of memory\n");
  86. retval = -ENOMEM;
  87. goto error;
  88. }
  89. dev->udev = usb_get_dev(udev);
  90. usb_set_intfdata(interface, dev);
  91. retval = device_create_file(&interface->dev, &dev_attr_speed);
  92. if (retval)
  93. goto error_create_file;
  94. return 0;
  95. error_create_file:
  96. usb_put_dev(udev);
  97. usb_set_intfdata(interface, NULL);
  98. error:
  99. kfree(dev);
  100. return retval;
  101. }
  102. static void tv_disconnect(struct usb_interface *interface)
  103. {
  104. struct trancevibrator *dev;
  105. dev = usb_get_intfdata (interface);
  106. device_remove_file(&interface->dev, &dev_attr_speed);
  107. usb_set_intfdata(interface, NULL);
  108. usb_put_dev(dev->udev);
  109. kfree(dev);
  110. }
  111. /* USB subsystem object */
  112. static struct usb_driver tv_driver = {
  113. .name = "trancevibrator",
  114. .probe = tv_probe,
  115. .disconnect = tv_disconnect,
  116. .id_table = id_table,
  117. };
  118. module_usb_driver(tv_driver);
  119. MODULE_AUTHOR(DRIVER_AUTHOR);
  120. MODULE_DESCRIPTION(DRIVER_DESC);
  121. MODULE_LICENSE("GPL");