xref: /openbmc/linux/drivers/usb/misc/trancevibrator.c (revision 8be98d2f2a0a262f8bf8a0bc1fdf522b3c7aab17)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
29fcde235SGreg Kroah-Hartman /*
39fcde235SGreg Kroah-Hartman  * PlayStation 2 Trance Vibrator driver
49fcde235SGreg Kroah-Hartman  *
59fcde235SGreg Kroah-Hartman  * Copyright (C) 2006 Sam Hocevar <sam@zoy.org>
69fcde235SGreg Kroah-Hartman  */
79fcde235SGreg Kroah-Hartman 
89fcde235SGreg Kroah-Hartman /* Standard include files */
99fcde235SGreg Kroah-Hartman #include <linux/kernel.h>
109fcde235SGreg Kroah-Hartman #include <linux/errno.h>
115a0e3ad6STejun Heo #include <linux/slab.h>
129fcde235SGreg Kroah-Hartman #include <linux/module.h>
139fcde235SGreg Kroah-Hartman #include <linux/usb.h>
149fcde235SGreg Kroah-Hartman 
159fcde235SGreg Kroah-Hartman #define DRIVER_AUTHOR "Sam Hocevar, sam@zoy.org"
169fcde235SGreg Kroah-Hartman #define DRIVER_DESC "PlayStation 2 Trance Vibrator driver"
179fcde235SGreg Kroah-Hartman 
189fcde235SGreg Kroah-Hartman #define TRANCEVIBRATOR_VENDOR_ID	0x0b49	/* ASCII Corporation */
199fcde235SGreg Kroah-Hartman #define TRANCEVIBRATOR_PRODUCT_ID	0x064f	/* Trance Vibrator */
209fcde235SGreg Kroah-Hartman 
2133b9e162SNémeth Márton static const struct usb_device_id id_table[] = {
229fcde235SGreg Kroah-Hartman 	{ USB_DEVICE(TRANCEVIBRATOR_VENDOR_ID, TRANCEVIBRATOR_PRODUCT_ID) },
239fcde235SGreg Kroah-Hartman 	{ },
249fcde235SGreg Kroah-Hartman };
259fcde235SGreg Kroah-Hartman MODULE_DEVICE_TABLE (usb, id_table);
269fcde235SGreg Kroah-Hartman 
279fcde235SGreg Kroah-Hartman /* Driver-local specific stuff */
289fcde235SGreg Kroah-Hartman struct trancevibrator {
299fcde235SGreg Kroah-Hartman 	struct usb_device *udev;
309fcde235SGreg Kroah-Hartman 	unsigned int speed;
319fcde235SGreg Kroah-Hartman };
329fcde235SGreg Kroah-Hartman 
speed_show(struct device * dev,struct device_attribute * attr,char * buf)33ed5bd7a4SGreg Kroah-Hartman static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
349fcde235SGreg Kroah-Hartman 			  char *buf)
359fcde235SGreg Kroah-Hartman {
369fcde235SGreg Kroah-Hartman 	struct usb_interface *intf = to_usb_interface(dev);
379fcde235SGreg Kroah-Hartman 	struct trancevibrator *tv = usb_get_intfdata(intf);
389fcde235SGreg Kroah-Hartman 
399fcde235SGreg Kroah-Hartman 	return sprintf(buf, "%d\n", tv->speed);
409fcde235SGreg Kroah-Hartman }
419fcde235SGreg Kroah-Hartman 
speed_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)42ed5bd7a4SGreg Kroah-Hartman static ssize_t speed_store(struct device *dev, struct device_attribute *attr,
439fcde235SGreg Kroah-Hartman 			 const char *buf, size_t count)
449fcde235SGreg Kroah-Hartman {
459fcde235SGreg Kroah-Hartman 	struct usb_interface *intf = to_usb_interface(dev);
469fcde235SGreg Kroah-Hartman 	struct trancevibrator *tv = usb_get_intfdata(intf);
470cc5e2e7SOliver Neukum 	int temp, retval, old;
489fcde235SGreg Kroah-Hartman 
499d20bca5SDing Xiang 	retval = kstrtoint(buf, 10, &temp);
509d20bca5SDing Xiang 	if (retval)
519d20bca5SDing Xiang 		return retval;
529fcde235SGreg Kroah-Hartman 	if (temp > 255)
539fcde235SGreg Kroah-Hartman 		temp = 255;
549fcde235SGreg Kroah-Hartman 	else if (temp < 0)
559fcde235SGreg Kroah-Hartman 		temp = 0;
560cc5e2e7SOliver Neukum 	old = tv->speed;
579fcde235SGreg Kroah-Hartman 	tv->speed = temp;
589fcde235SGreg Kroah-Hartman 
599fcde235SGreg Kroah-Hartman 	dev_dbg(&tv->udev->dev, "speed = %d\n", tv->speed);
609fcde235SGreg Kroah-Hartman 
619fcde235SGreg Kroah-Hartman 	/* Set speed */
629fcde235SGreg Kroah-Hartman 	retval = usb_control_msg(tv->udev, usb_sndctrlpipe(tv->udev, 0),
639fcde235SGreg Kroah-Hartman 				 0x01, /* vendor request: set speed */
64*746e4acfSJohan Hovold 				 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
659fcde235SGreg Kroah-Hartman 				 tv->speed, /* speed value */
66*746e4acfSJohan Hovold 				 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
679fcde235SGreg Kroah-Hartman 	if (retval) {
680cc5e2e7SOliver Neukum 		tv->speed = old;
699fcde235SGreg Kroah-Hartman 		dev_dbg(&tv->udev->dev, "retval = %d\n", retval);
709fcde235SGreg Kroah-Hartman 		return retval;
719fcde235SGreg Kroah-Hartman 	}
729fcde235SGreg Kroah-Hartman 	return count;
739fcde235SGreg Kroah-Hartman }
74ed5bd7a4SGreg Kroah-Hartman static DEVICE_ATTR_RW(speed);
759fcde235SGreg Kroah-Hartman 
76f9bbcbefSGreg Kroah-Hartman static struct attribute *tv_attrs[] = {
77f9bbcbefSGreg Kroah-Hartman 	&dev_attr_speed.attr,
78f9bbcbefSGreg Kroah-Hartman 	NULL,
79f9bbcbefSGreg Kroah-Hartman };
80f9bbcbefSGreg Kroah-Hartman ATTRIBUTE_GROUPS(tv);
81f9bbcbefSGreg Kroah-Hartman 
tv_probe(struct usb_interface * interface,const struct usb_device_id * id)829fcde235SGreg Kroah-Hartman static int tv_probe(struct usb_interface *interface,
839fcde235SGreg Kroah-Hartman 		    const struct usb_device_id *id)
849fcde235SGreg Kroah-Hartman {
859fcde235SGreg Kroah-Hartman 	struct usb_device *udev = interface_to_usbdev(interface);
869fcde235SGreg Kroah-Hartman 	struct trancevibrator *dev;
879fcde235SGreg Kroah-Hartman 	int retval;
889fcde235SGreg Kroah-Hartman 
899fcde235SGreg Kroah-Hartman 	dev = kzalloc(sizeof(struct trancevibrator), GFP_KERNEL);
9058e61402SWolfram Sang 	if (!dev) {
919fcde235SGreg Kroah-Hartman 		retval = -ENOMEM;
929fcde235SGreg Kroah-Hartman 		goto error;
939fcde235SGreg Kroah-Hartman 	}
949fcde235SGreg Kroah-Hartman 
959fcde235SGreg Kroah-Hartman 	dev->udev = usb_get_dev(udev);
969fcde235SGreg Kroah-Hartman 	usb_set_intfdata(interface, dev);
979fcde235SGreg Kroah-Hartman 
989fcde235SGreg Kroah-Hartman 	return 0;
999fcde235SGreg Kroah-Hartman 
1009fcde235SGreg Kroah-Hartman error:
1019fcde235SGreg Kroah-Hartman 	kfree(dev);
1029fcde235SGreg Kroah-Hartman 	return retval;
1039fcde235SGreg Kroah-Hartman }
1049fcde235SGreg Kroah-Hartman 
tv_disconnect(struct usb_interface * interface)1059fcde235SGreg Kroah-Hartman static void tv_disconnect(struct usb_interface *interface)
1069fcde235SGreg Kroah-Hartman {
1079fcde235SGreg Kroah-Hartman 	struct trancevibrator *dev;
1089fcde235SGreg Kroah-Hartman 
1099fcde235SGreg Kroah-Hartman 	dev = usb_get_intfdata (interface);
11096ca014dSOliver Neukum 	usb_set_intfdata(interface, NULL);
1119fcde235SGreg Kroah-Hartman 	usb_put_dev(dev->udev);
1129fcde235SGreg Kroah-Hartman 	kfree(dev);
1139fcde235SGreg Kroah-Hartman }
1149fcde235SGreg Kroah-Hartman 
1159fcde235SGreg Kroah-Hartman /* USB subsystem object */
1169fcde235SGreg Kroah-Hartman static struct usb_driver tv_driver = {
1179fcde235SGreg Kroah-Hartman 	.name =		"trancevibrator",
1189fcde235SGreg Kroah-Hartman 	.probe =	tv_probe,
1199fcde235SGreg Kroah-Hartman 	.disconnect =	tv_disconnect,
1209fcde235SGreg Kroah-Hartman 	.id_table =	id_table,
121f9bbcbefSGreg Kroah-Hartman 	.dev_groups =	tv_groups,
1229fcde235SGreg Kroah-Hartman };
1239fcde235SGreg Kroah-Hartman 
12465db4305SGreg Kroah-Hartman module_usb_driver(tv_driver);
1259fcde235SGreg Kroah-Hartman 
1269fcde235SGreg Kroah-Hartman MODULE_AUTHOR(DRIVER_AUTHOR);
1279fcde235SGreg Kroah-Hartman MODULE_DESCRIPTION(DRIVER_DESC);
1289fcde235SGreg Kroah-Hartman MODULE_LICENSE("GPL");
129