onetouch.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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_ch9.h>
  35. #include <linux/usb/input.h>
  36. #include "usb.h"
  37. #include "onetouch.h"
  38. #include "debug.h"
  39. void onetouch_release_input(void *onetouch_);
  40. struct usb_onetouch {
  41. char name[128];
  42. char phys[64];
  43. struct input_dev *dev; /* input device interface */
  44. struct usb_device *udev; /* usb device */
  45. struct urb *irq; /* urb for interrupt in report */
  46. unsigned char *data; /* input data */
  47. dma_addr_t data_dma;
  48. unsigned int is_open:1;
  49. };
  50. static void usb_onetouch_irq(struct urb *urb, struct pt_regs *regs)
  51. {
  52. struct usb_onetouch *onetouch = urb->context;
  53. signed char *data = onetouch->data;
  54. struct input_dev *dev = onetouch->dev;
  55. int status;
  56. switch (urb->status) {
  57. case 0: /* success */
  58. break;
  59. case -ECONNRESET: /* unlink */
  60. case -ENOENT:
  61. case -ESHUTDOWN:
  62. return;
  63. /* -EPIPE: should clear the halt */
  64. default: /* error */
  65. goto resubmit;
  66. }
  67. input_regs(dev, regs);
  68. input_report_key(dev, ONETOUCH_BUTTON, data[0] & 0x02);
  69. input_sync(dev);
  70. resubmit:
  71. status = usb_submit_urb (urb, SLAB_ATOMIC);
  72. if (status)
  73. err ("can't resubmit intr, %s-%s/input0, status %d",
  74. onetouch->udev->bus->bus_name,
  75. onetouch->udev->devpath, status);
  76. }
  77. static int usb_onetouch_open(struct input_dev *dev)
  78. {
  79. struct usb_onetouch *onetouch = dev->private;
  80. onetouch->is_open = 1;
  81. onetouch->irq->dev = onetouch->udev;
  82. if (usb_submit_urb(onetouch->irq, GFP_KERNEL)) {
  83. err("usb_submit_urb failed");
  84. return -EIO;
  85. }
  86. return 0;
  87. }
  88. static void usb_onetouch_close(struct input_dev *dev)
  89. {
  90. struct usb_onetouch *onetouch = dev->private;
  91. usb_kill_urb(onetouch->irq);
  92. onetouch->is_open = 0;
  93. }
  94. #ifdef CONFIG_PM
  95. static void usb_onetouch_pm_hook(struct us_data *us, int action)
  96. {
  97. struct usb_onetouch *onetouch = (struct usb_onetouch *) us->extra;
  98. if (onetouch->is_open) {
  99. switch (action) {
  100. case US_SUSPEND:
  101. usb_kill_urb(onetouch->irq);
  102. break;
  103. case US_RESUME:
  104. if (usb_submit_urb(onetouch->irq, GFP_KERNEL) != 0)
  105. err("usb_submit_urb failed");
  106. break;
  107. default:
  108. break;
  109. }
  110. }
  111. }
  112. #endif /* CONFIG_PM */
  113. int onetouch_connect_input(struct us_data *ss)
  114. {
  115. struct usb_device *udev = ss->pusb_dev;
  116. struct usb_host_interface *interface;
  117. struct usb_endpoint_descriptor *endpoint;
  118. struct usb_onetouch *onetouch;
  119. struct input_dev *input_dev;
  120. int pipe, maxp;
  121. int error = -ENOMEM;
  122. interface = ss->pusb_intf->cur_altsetting;
  123. if (interface->desc.bNumEndpoints != 3)
  124. return -ENODEV;
  125. endpoint = &interface->endpoint[2].desc;
  126. if (!(endpoint->bEndpointAddress & USB_DIR_IN))
  127. return -ENODEV;
  128. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  129. != USB_ENDPOINT_XFER_INT)
  130. return -ENODEV;
  131. pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
  132. maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
  133. onetouch = kzalloc(sizeof(struct usb_onetouch), GFP_KERNEL);
  134. input_dev = input_allocate_device();
  135. if (!onetouch || !input_dev)
  136. goto fail1;
  137. onetouch->data = usb_buffer_alloc(udev, ONETOUCH_PKT_LEN,
  138. SLAB_ATOMIC, &onetouch->data_dma);
  139. if (!onetouch->data)
  140. goto fail1;
  141. onetouch->irq = usb_alloc_urb(0, GFP_KERNEL);
  142. if (!onetouch->irq)
  143. goto fail2;
  144. onetouch->udev = udev;
  145. onetouch->dev = input_dev;
  146. if (udev->manufacturer)
  147. strlcpy(onetouch->name, udev->manufacturer,
  148. sizeof(onetouch->name));
  149. if (udev->product) {
  150. if (udev->manufacturer)
  151. strlcat(onetouch->name, " ", sizeof(onetouch->name));
  152. strlcat(onetouch->name, udev->product, sizeof(onetouch->name));
  153. }
  154. if (!strlen(onetouch->name))
  155. snprintf(onetouch->name, sizeof(onetouch->name),
  156. "Maxtor Onetouch %04x:%04x",
  157. le16_to_cpu(udev->descriptor.idVendor),
  158. le16_to_cpu(udev->descriptor.idProduct));
  159. usb_make_path(udev, onetouch->phys, sizeof(onetouch->phys));
  160. strlcat(onetouch->phys, "/input0", sizeof(onetouch->phys));
  161. input_dev->name = onetouch->name;
  162. input_dev->phys = onetouch->phys;
  163. usb_to_input_id(udev, &input_dev->id);
  164. input_dev->cdev.dev = &udev->dev;
  165. set_bit(EV_KEY, input_dev->evbit);
  166. set_bit(ONETOUCH_BUTTON, input_dev->keybit);
  167. clear_bit(0, input_dev->keybit);
  168. input_dev->private = onetouch;
  169. input_dev->open = usb_onetouch_open;
  170. input_dev->close = usb_onetouch_close;
  171. usb_fill_int_urb(onetouch->irq, udev, pipe, onetouch->data,
  172. (maxp > 8 ? 8 : maxp),
  173. usb_onetouch_irq, onetouch, endpoint->bInterval);
  174. onetouch->irq->transfer_dma = onetouch->data_dma;
  175. onetouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  176. ss->extra_destructor = onetouch_release_input;
  177. ss->extra = onetouch;
  178. #ifdef CONFIG_PM
  179. ss->suspend_resume_hook = usb_onetouch_pm_hook;
  180. #endif
  181. error = input_register_device(onetouch->dev);
  182. if (error)
  183. goto fail3;
  184. return 0;
  185. fail3: usb_free_urb(onetouch->irq);
  186. fail2: usb_buffer_free(udev, ONETOUCH_PKT_LEN,
  187. onetouch->data, onetouch->data_dma);
  188. fail1: kfree(onetouch);
  189. input_free_device(input_dev);
  190. return error;
  191. }
  192. void onetouch_release_input(void *onetouch_)
  193. {
  194. struct usb_onetouch *onetouch = (struct usb_onetouch *) onetouch_;
  195. if (onetouch) {
  196. usb_kill_urb(onetouch->irq);
  197. input_unregister_device(onetouch->dev);
  198. usb_free_urb(onetouch->irq);
  199. usb_buffer_free(onetouch->udev, ONETOUCH_PKT_LEN,
  200. onetouch->data, onetouch->data_dma);
  201. }
  202. }