onetouch.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Support for the Maxtor OneTouch USB hard drive's button
  3. *
  4. * Current development and maintenance by:
  5. * Copyright (c) 2005 Nick Sillik <n.sillik@temple.edu>
  6. *
  7. * Initial work by:
  8. * Copyright (c) 2003 Erik Thyren <erth7411@student.uu.se>
  9. *
  10. * Based on usbmouse.c (Vojtech Pavlik) and xpad.c (Marko Friedemann)
  11. *
  12. */
  13. /*
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/input.h>
  31. #include <linux/init.h>
  32. #include <linux/slab.h>
  33. #include <linux/module.h>
  34. #include <linux/usb/input.h>
  35. #include "usb.h"
  36. #include "onetouch.h"
  37. #include "debug.h"
  38. void onetouch_release_input(void *onetouch_);
  39. struct usb_onetouch {
  40. char name[128];
  41. char phys[64];
  42. struct input_dev *dev; /* input device interface */
  43. struct usb_device *udev; /* usb device */
  44. struct urb *irq; /* urb for interrupt in report */
  45. unsigned char *data; /* input data */
  46. dma_addr_t data_dma;
  47. unsigned int is_open:1;
  48. };
  49. static void usb_onetouch_irq(struct urb *urb)
  50. {
  51. struct usb_onetouch *onetouch = urb->context;
  52. signed char *data = onetouch->data;
  53. struct input_dev *dev = onetouch->dev;
  54. int status;
  55. switch (urb->status) {
  56. case 0: /* success */
  57. break;
  58. case -ECONNRESET: /* unlink */
  59. case -ENOENT:
  60. case -ESHUTDOWN:
  61. return;
  62. /* -EPIPE: should clear the halt */
  63. default: /* error */
  64. goto resubmit;
  65. }
  66. input_report_key(dev, ONETOUCH_BUTTON, data[0] & 0x02);
  67. input_sync(dev);
  68. resubmit:
  69. status = usb_submit_urb (urb, GFP_ATOMIC);
  70. if (status)
  71. err ("can't resubmit intr, %s-%s/input0, status %d",
  72. onetouch->udev->bus->bus_name,
  73. onetouch->udev->devpath, status);
  74. }
  75. static int usb_onetouch_open(struct input_dev *dev)
  76. {
  77. struct usb_onetouch *onetouch = dev->private;
  78. onetouch->is_open = 1;
  79. onetouch->irq->dev = onetouch->udev;
  80. if (usb_submit_urb(onetouch->irq, GFP_KERNEL)) {
  81. err("usb_submit_urb failed");
  82. return -EIO;
  83. }
  84. return 0;
  85. }
  86. static void usb_onetouch_close(struct input_dev *dev)
  87. {
  88. struct usb_onetouch *onetouch = dev->private;
  89. usb_kill_urb(onetouch->irq);
  90. onetouch->is_open = 0;
  91. }
  92. #ifdef CONFIG_PM
  93. static void usb_onetouch_pm_hook(struct us_data *us, int action)
  94. {
  95. struct usb_onetouch *onetouch = (struct usb_onetouch *) us->extra;
  96. if (onetouch->is_open) {
  97. switch (action) {
  98. case US_SUSPEND:
  99. usb_kill_urb(onetouch->irq);
  100. break;
  101. case US_RESUME:
  102. if (usb_submit_urb(onetouch->irq, GFP_KERNEL) != 0)
  103. err("usb_submit_urb failed");
  104. break;
  105. default:
  106. break;
  107. }
  108. }
  109. }
  110. #endif /* CONFIG_PM */
  111. int onetouch_connect_input(struct us_data *ss)
  112. {
  113. struct usb_device *udev = ss->pusb_dev;
  114. struct usb_host_interface *interface;
  115. struct usb_endpoint_descriptor *endpoint;
  116. struct usb_onetouch *onetouch;
  117. struct input_dev *input_dev;
  118. int pipe, maxp;
  119. int error = -ENOMEM;
  120. interface = ss->pusb_intf->cur_altsetting;
  121. if (interface->desc.bNumEndpoints != 3)
  122. return -ENODEV;
  123. endpoint = &interface->endpoint[2].desc;
  124. if (!usb_endpoint_is_int_in(endpoint))
  125. return -ENODEV;
  126. pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
  127. maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
  128. onetouch = kzalloc(sizeof(struct usb_onetouch), GFP_KERNEL);
  129. input_dev = input_allocate_device();
  130. if (!onetouch || !input_dev)
  131. goto fail1;
  132. onetouch->data = usb_buffer_alloc(udev, ONETOUCH_PKT_LEN,
  133. GFP_ATOMIC, &onetouch->data_dma);
  134. if (!onetouch->data)
  135. goto fail1;
  136. onetouch->irq = usb_alloc_urb(0, GFP_KERNEL);
  137. if (!onetouch->irq)
  138. goto fail2;
  139. onetouch->udev = udev;
  140. onetouch->dev = input_dev;
  141. if (udev->manufacturer)
  142. strlcpy(onetouch->name, udev->manufacturer,
  143. sizeof(onetouch->name));
  144. if (udev->product) {
  145. if (udev->manufacturer)
  146. strlcat(onetouch->name, " ", sizeof(onetouch->name));
  147. strlcat(onetouch->name, udev->product, sizeof(onetouch->name));
  148. }
  149. if (!strlen(onetouch->name))
  150. snprintf(onetouch->name, sizeof(onetouch->name),
  151. "Maxtor Onetouch %04x:%04x",
  152. le16_to_cpu(udev->descriptor.idVendor),
  153. le16_to_cpu(udev->descriptor.idProduct));
  154. usb_make_path(udev, onetouch->phys, sizeof(onetouch->phys));
  155. strlcat(onetouch->phys, "/input0", sizeof(onetouch->phys));
  156. input_dev->name = onetouch->name;
  157. input_dev->phys = onetouch->phys;
  158. usb_to_input_id(udev, &input_dev->id);
  159. input_dev->cdev.dev = &udev->dev;
  160. set_bit(EV_KEY, input_dev->evbit);
  161. set_bit(ONETOUCH_BUTTON, input_dev->keybit);
  162. clear_bit(0, input_dev->keybit);
  163. input_dev->private = onetouch;
  164. input_dev->open = usb_onetouch_open;
  165. input_dev->close = usb_onetouch_close;
  166. usb_fill_int_urb(onetouch->irq, udev, pipe, onetouch->data,
  167. (maxp > 8 ? 8 : maxp),
  168. usb_onetouch_irq, onetouch, endpoint->bInterval);
  169. onetouch->irq->transfer_dma = onetouch->data_dma;
  170. onetouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  171. ss->extra_destructor = onetouch_release_input;
  172. ss->extra = onetouch;
  173. #ifdef CONFIG_PM
  174. ss->suspend_resume_hook = usb_onetouch_pm_hook;
  175. #endif
  176. error = input_register_device(onetouch->dev);
  177. if (error)
  178. goto fail3;
  179. return 0;
  180. fail3: usb_free_urb(onetouch->irq);
  181. fail2: usb_buffer_free(udev, ONETOUCH_PKT_LEN,
  182. onetouch->data, onetouch->data_dma);
  183. fail1: kfree(onetouch);
  184. input_free_device(input_dev);
  185. return error;
  186. }
  187. void onetouch_release_input(void *onetouch_)
  188. {
  189. struct usb_onetouch *onetouch = (struct usb_onetouch *) onetouch_;
  190. if (onetouch) {
  191. usb_kill_urb(onetouch->irq);
  192. input_unregister_device(onetouch->dev);
  193. usb_free_urb(onetouch->irq);
  194. usb_buffer_free(onetouch->udev, ONETOUCH_PKT_LEN,
  195. onetouch->data, onetouch->data_dma);
  196. }
  197. }