onetouch.c 7.6 KB

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