xref: /openbmc/linux/drivers/usb/storage/onetouch.c (revision b8d312aa)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Support for the Maxtor OneTouch USB hard drive's button
4  *
5  * Current development and maintenance by:
6  *	Copyright (c) 2005 Nick Sillik <n.sillik@temple.edu>
7  *
8  * Initial work by:
9  *	Copyright (c) 2003 Erik Thyren <erth7411@student.uu.se>
10  *
11  * Based on usbmouse.c (Vojtech Pavlik) and xpad.c (Marko Friedemann)
12  *
13  */
14 #include <linux/kernel.h>
15 #include <linux/input.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/usb/input.h>
19 #include "usb.h"
20 #include "debug.h"
21 #include "scsiglue.h"
22 
23 #define DRV_NAME "ums-onetouch"
24 
25 MODULE_DESCRIPTION("Maxtor USB OneTouch hard drive button driver");
26 MODULE_AUTHOR("Nick Sillik <n.sillik@temple.edu>");
27 MODULE_LICENSE("GPL");
28 
29 #define ONETOUCH_PKT_LEN        0x02
30 #define ONETOUCH_BUTTON         KEY_PROG1
31 
32 static int onetouch_connect_input(struct us_data *ss);
33 static void onetouch_release_input(void *onetouch_);
34 
35 struct usb_onetouch {
36 	char name[128];
37 	char phys[64];
38 	struct input_dev *dev;	/* input device interface */
39 	struct usb_device *udev;	/* usb device */
40 
41 	struct urb *irq;	/* urb for interrupt in report */
42 	unsigned char *data;	/* input data */
43 	dma_addr_t data_dma;
44 	unsigned int is_open:1;
45 };
46 
47 
48 /*
49  * The table of devices
50  */
51 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
52 		    vendorName, productName, useProtocol, useTransport, \
53 		    initFunction, flags) \
54 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
55   .driver_info = (flags) }
56 
57 static struct usb_device_id onetouch_usb_ids[] = {
58 #	include "unusual_onetouch.h"
59 	{ }		/* Terminating entry */
60 };
61 MODULE_DEVICE_TABLE(usb, onetouch_usb_ids);
62 
63 #undef UNUSUAL_DEV
64 
65 /*
66  * The flags table
67  */
68 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
69 		    vendor_name, product_name, use_protocol, use_transport, \
70 		    init_function, Flags) \
71 { \
72 	.vendorName = vendor_name,	\
73 	.productName = product_name,	\
74 	.useProtocol = use_protocol,	\
75 	.useTransport = use_transport,	\
76 	.initFunction = init_function,	\
77 }
78 
79 static struct us_unusual_dev onetouch_unusual_dev_list[] = {
80 #	include "unusual_onetouch.h"
81 	{ }		/* Terminating entry */
82 };
83 
84 #undef UNUSUAL_DEV
85 
86 
87 static void usb_onetouch_irq(struct urb *urb)
88 {
89 	struct usb_onetouch *onetouch = urb->context;
90 	signed char *data = onetouch->data;
91 	struct input_dev *dev = onetouch->dev;
92 	int status = urb->status;
93 	int retval;
94 
95 	switch (status) {
96 	case 0:			/* success */
97 		break;
98 	case -ECONNRESET:	/* unlink */
99 	case -ENOENT:
100 	case -ESHUTDOWN:
101 		return;
102 	/* -EPIPE:  should clear the halt */
103 	default:		/* error */
104 		goto resubmit;
105 	}
106 
107 	input_report_key(dev, ONETOUCH_BUTTON, data[0] & 0x02);
108 	input_sync(dev);
109 
110 resubmit:
111 	retval = usb_submit_urb (urb, GFP_ATOMIC);
112 	if (retval)
113 		dev_err(&dev->dev, "can't resubmit intr, %s-%s/input0, "
114 			"retval %d\n", onetouch->udev->bus->bus_name,
115 			onetouch->udev->devpath, retval);
116 }
117 
118 static int usb_onetouch_open(struct input_dev *dev)
119 {
120 	struct usb_onetouch *onetouch = input_get_drvdata(dev);
121 
122 	onetouch->is_open = 1;
123 	onetouch->irq->dev = onetouch->udev;
124 	if (usb_submit_urb(onetouch->irq, GFP_KERNEL)) {
125 		dev_err(&dev->dev, "usb_submit_urb failed\n");
126 		return -EIO;
127 	}
128 
129 	return 0;
130 }
131 
132 static void usb_onetouch_close(struct input_dev *dev)
133 {
134 	struct usb_onetouch *onetouch = input_get_drvdata(dev);
135 
136 	usb_kill_urb(onetouch->irq);
137 	onetouch->is_open = 0;
138 }
139 
140 #ifdef CONFIG_PM
141 static void usb_onetouch_pm_hook(struct us_data *us, int action)
142 {
143 	struct usb_onetouch *onetouch = (struct usb_onetouch *) us->extra;
144 
145 	if (onetouch->is_open) {
146 		switch (action) {
147 		case US_SUSPEND:
148 			usb_kill_urb(onetouch->irq);
149 			break;
150 		case US_RESUME:
151 			if (usb_submit_urb(onetouch->irq, GFP_NOIO) != 0)
152 				dev_err(&onetouch->irq->dev->dev,
153 					"usb_submit_urb failed\n");
154 			break;
155 		default:
156 			break;
157 		}
158 	}
159 }
160 #endif /* CONFIG_PM */
161 
162 static int onetouch_connect_input(struct us_data *ss)
163 {
164 	struct usb_device *udev = ss->pusb_dev;
165 	struct usb_host_interface *interface;
166 	struct usb_endpoint_descriptor *endpoint;
167 	struct usb_onetouch *onetouch;
168 	struct input_dev *input_dev;
169 	int pipe, maxp;
170 	int error = -ENOMEM;
171 
172 	interface = ss->pusb_intf->cur_altsetting;
173 
174 	if (interface->desc.bNumEndpoints != 3)
175 		return -ENODEV;
176 
177 	endpoint = &interface->endpoint[2].desc;
178 	if (!usb_endpoint_is_int_in(endpoint))
179 		return -ENODEV;
180 
181 	pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
182 	maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
183 	maxp = min(maxp, ONETOUCH_PKT_LEN);
184 
185 	onetouch = kzalloc(sizeof(struct usb_onetouch), GFP_KERNEL);
186 	input_dev = input_allocate_device();
187 	if (!onetouch || !input_dev)
188 		goto fail1;
189 
190 	onetouch->data = usb_alloc_coherent(udev, ONETOUCH_PKT_LEN,
191 					    GFP_KERNEL, &onetouch->data_dma);
192 	if (!onetouch->data)
193 		goto fail1;
194 
195 	onetouch->irq = usb_alloc_urb(0, GFP_KERNEL);
196 	if (!onetouch->irq)
197 		goto fail2;
198 
199 	onetouch->udev = udev;
200 	onetouch->dev = input_dev;
201 
202 	if (udev->manufacturer)
203 		strlcpy(onetouch->name, udev->manufacturer,
204 			sizeof(onetouch->name));
205 	if (udev->product) {
206 		if (udev->manufacturer)
207 			strlcat(onetouch->name, " ", sizeof(onetouch->name));
208 		strlcat(onetouch->name, udev->product, sizeof(onetouch->name));
209 	}
210 
211 	if (!strlen(onetouch->name))
212 		snprintf(onetouch->name, sizeof(onetouch->name),
213 			 "Maxtor Onetouch %04x:%04x",
214 			 le16_to_cpu(udev->descriptor.idVendor),
215 			 le16_to_cpu(udev->descriptor.idProduct));
216 
217 	usb_make_path(udev, onetouch->phys, sizeof(onetouch->phys));
218 	strlcat(onetouch->phys, "/input0", sizeof(onetouch->phys));
219 
220 	input_dev->name = onetouch->name;
221 	input_dev->phys = onetouch->phys;
222 	usb_to_input_id(udev, &input_dev->id);
223 	input_dev->dev.parent = &udev->dev;
224 
225 	set_bit(EV_KEY, input_dev->evbit);
226 	set_bit(ONETOUCH_BUTTON, input_dev->keybit);
227 	clear_bit(0, input_dev->keybit);
228 
229 	input_set_drvdata(input_dev, onetouch);
230 
231 	input_dev->open = usb_onetouch_open;
232 	input_dev->close = usb_onetouch_close;
233 
234 	usb_fill_int_urb(onetouch->irq, udev, pipe, onetouch->data, maxp,
235 			 usb_onetouch_irq, onetouch, endpoint->bInterval);
236 	onetouch->irq->transfer_dma = onetouch->data_dma;
237 	onetouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
238 
239 	ss->extra_destructor = onetouch_release_input;
240 	ss->extra = onetouch;
241 #ifdef CONFIG_PM
242 	ss->suspend_resume_hook = usb_onetouch_pm_hook;
243 #endif
244 
245 	error = input_register_device(onetouch->dev);
246 	if (error)
247 		goto fail3;
248 
249 	return 0;
250 
251  fail3:	usb_free_urb(onetouch->irq);
252  fail2:	usb_free_coherent(udev, ONETOUCH_PKT_LEN,
253 			  onetouch->data, onetouch->data_dma);
254  fail1:	kfree(onetouch);
255 	input_free_device(input_dev);
256 	return error;
257 }
258 
259 static void onetouch_release_input(void *onetouch_)
260 {
261 	struct usb_onetouch *onetouch = (struct usb_onetouch *) onetouch_;
262 
263 	if (onetouch) {
264 		usb_kill_urb(onetouch->irq);
265 		input_unregister_device(onetouch->dev);
266 		usb_free_urb(onetouch->irq);
267 		usb_free_coherent(onetouch->udev, ONETOUCH_PKT_LEN,
268 				  onetouch->data, onetouch->data_dma);
269 	}
270 }
271 
272 static struct scsi_host_template onetouch_host_template;
273 
274 static int onetouch_probe(struct usb_interface *intf,
275 			 const struct usb_device_id *id)
276 {
277 	struct us_data *us;
278 	int result;
279 
280 	result = usb_stor_probe1(&us, intf, id,
281 			(id - onetouch_usb_ids) + onetouch_unusual_dev_list,
282 			&onetouch_host_template);
283 	if (result)
284 		return result;
285 
286 	/* Use default transport and protocol */
287 
288 	result = usb_stor_probe2(us);
289 	return result;
290 }
291 
292 static struct usb_driver onetouch_driver = {
293 	.name =		DRV_NAME,
294 	.probe =	onetouch_probe,
295 	.disconnect =	usb_stor_disconnect,
296 	.suspend =	usb_stor_suspend,
297 	.resume =	usb_stor_resume,
298 	.reset_resume =	usb_stor_reset_resume,
299 	.pre_reset =	usb_stor_pre_reset,
300 	.post_reset =	usb_stor_post_reset,
301 	.id_table =	onetouch_usb_ids,
302 	.soft_unbind =	1,
303 	.no_dynamic_id = 1,
304 };
305 
306 module_usb_stor_driver(onetouch_driver, onetouch_host_template, DRV_NAME);
307