xref: /linux/drivers/usb/misc/trancevibrator.c (revision 44f57d78)
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 
8 /* Standard include files */
9 #include <linux/kernel.h>
10 #include <linux/errno.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/usb.h>
14 
15 #define DRIVER_AUTHOR "Sam Hocevar, sam@zoy.org"
16 #define DRIVER_DESC "PlayStation 2 Trance Vibrator driver"
17 
18 #define TRANCEVIBRATOR_VENDOR_ID	0x0b49	/* ASCII Corporation */
19 #define TRANCEVIBRATOR_PRODUCT_ID	0x064f	/* Trance Vibrator */
20 
21 static const struct usb_device_id id_table[] = {
22 	{ USB_DEVICE(TRANCEVIBRATOR_VENDOR_ID, TRANCEVIBRATOR_PRODUCT_ID) },
23 	{ },
24 };
25 MODULE_DEVICE_TABLE (usb, id_table);
26 
27 /* Driver-local specific stuff */
28 struct trancevibrator {
29 	struct usb_device *udev;
30 	unsigned int speed;
31 };
32 
33 static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
34 			  char *buf)
35 {
36 	struct usb_interface *intf = to_usb_interface(dev);
37 	struct trancevibrator *tv = usb_get_intfdata(intf);
38 
39 	return sprintf(buf, "%d\n", tv->speed);
40 }
41 
42 static ssize_t speed_store(struct device *dev, struct device_attribute *attr,
43 			 const char *buf, size_t count)
44 {
45 	struct usb_interface *intf = to_usb_interface(dev);
46 	struct trancevibrator *tv = usb_get_intfdata(intf);
47 	int temp, retval, old;
48 
49 	retval = kstrtoint(buf, 10, &temp);
50 	if (retval)
51 		return retval;
52 	if (temp > 255)
53 		temp = 255;
54 	else if (temp < 0)
55 		temp = 0;
56 	old = tv->speed;
57 	tv->speed = temp;
58 
59 	dev_dbg(&tv->udev->dev, "speed = %d\n", tv->speed);
60 
61 	/* Set speed */
62 	retval = usb_control_msg(tv->udev, usb_sndctrlpipe(tv->udev, 0),
63 				 0x01, /* vendor request: set speed */
64 				 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
65 				 tv->speed, /* speed value */
66 				 0, NULL, 0, USB_CTRL_GET_TIMEOUT);
67 	if (retval) {
68 		tv->speed = old;
69 		dev_dbg(&tv->udev->dev, "retval = %d\n", retval);
70 		return retval;
71 	}
72 	return count;
73 }
74 
75 static DEVICE_ATTR_RW(speed);
76 
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 
84 	dev = kzalloc(sizeof(struct trancevibrator), GFP_KERNEL);
85 	if (!dev) {
86 		retval = -ENOMEM;
87 		goto error;
88 	}
89 
90 	dev->udev = usb_get_dev(udev);
91 	usb_set_intfdata(interface, dev);
92 	retval = device_create_file(&interface->dev, &dev_attr_speed);
93 	if (retval)
94 		goto error_create_file;
95 
96 	return 0;
97 
98 error_create_file:
99 	usb_put_dev(udev);
100 	usb_set_intfdata(interface, NULL);
101 error:
102 	kfree(dev);
103 	return retval;
104 }
105 
106 static void tv_disconnect(struct usb_interface *interface)
107 {
108 	struct trancevibrator *dev;
109 
110 	dev = usb_get_intfdata (interface);
111 	device_remove_file(&interface->dev, &dev_attr_speed);
112 	usb_set_intfdata(interface, NULL);
113 	usb_put_dev(dev->udev);
114 	kfree(dev);
115 }
116 
117 /* USB subsystem object */
118 static struct usb_driver tv_driver = {
119 	.name =		"trancevibrator",
120 	.probe =	tv_probe,
121 	.disconnect =	tv_disconnect,
122 	.id_table =	id_table,
123 };
124 
125 module_usb_driver(tv_driver);
126 
127 MODULE_AUTHOR(DRIVER_AUTHOR);
128 MODULE_DESCRIPTION(DRIVER_DESC);
129 MODULE_LICENSE("GPL");
130