onetouch.c 7.6 KB

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