onetouch.c 6.2 KB

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