onetouch.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. };
  51. static void usb_onetouch_irq(struct urb *urb, struct pt_regs *regs)
  52. {
  53. struct usb_onetouch *onetouch = urb->context;
  54. signed char *data = onetouch->data;
  55. struct input_dev *dev = &onetouch->dev;
  56. int status;
  57. switch (urb->status) {
  58. case 0: /* success */
  59. break;
  60. case -ECONNRESET: /* unlink */
  61. case -ENOENT:
  62. case -ESHUTDOWN:
  63. return;
  64. /* -EPIPE: should clear the halt */
  65. default: /* error */
  66. goto resubmit;
  67. }
  68. input_regs(dev, regs);
  69. input_report_key(&onetouch->dev, ONETOUCH_BUTTON,
  70. 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->irq->dev = onetouch->udev;
  83. if (usb_submit_urb(onetouch->irq, GFP_KERNEL)) {
  84. err("usb_submit_urb failed");
  85. return -EIO;
  86. }
  87. return 0;
  88. }
  89. static void usb_onetouch_close(struct input_dev *dev)
  90. {
  91. struct usb_onetouch *onetouch = dev->private;
  92. usb_kill_urb(onetouch->irq);
  93. }
  94. int onetouch_connect_input(struct us_data *ss)
  95. {
  96. struct usb_device *udev = ss->pusb_dev;
  97. struct usb_host_interface *interface;
  98. struct usb_endpoint_descriptor *endpoint;
  99. struct usb_onetouch *onetouch;
  100. int pipe, maxp;
  101. char path[64];
  102. interface = ss->pusb_intf->cur_altsetting;
  103. if (interface->desc.bNumEndpoints != 3)
  104. return -ENODEV;
  105. endpoint = &interface->endpoint[2].desc;
  106. if(!(endpoint->bEndpointAddress & USB_DIR_IN))
  107. return -ENODEV;
  108. if((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  109. != USB_ENDPOINT_XFER_INT)
  110. return -ENODEV;
  111. pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
  112. maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
  113. if (!(onetouch = kcalloc(1, sizeof(struct usb_onetouch), GFP_KERNEL)))
  114. return -ENOMEM;
  115. onetouch->data = usb_buffer_alloc(udev, ONETOUCH_PKT_LEN,
  116. SLAB_ATOMIC, &onetouch->data_dma);
  117. if (!onetouch->data){
  118. kfree(onetouch);
  119. return -ENOMEM;
  120. }
  121. onetouch->irq = usb_alloc_urb(0, GFP_KERNEL);
  122. if (!onetouch->irq){
  123. kfree(onetouch);
  124. usb_buffer_free(udev, ONETOUCH_PKT_LEN,
  125. onetouch->data, onetouch->data_dma);
  126. return -ENODEV;
  127. }
  128. onetouch->udev = udev;
  129. set_bit(EV_KEY, onetouch->dev.evbit);
  130. set_bit(ONETOUCH_BUTTON, onetouch->dev.keybit);
  131. clear_bit(0, onetouch->dev.keybit);
  132. onetouch->dev.private = onetouch;
  133. onetouch->dev.open = usb_onetouch_open;
  134. onetouch->dev.close = usb_onetouch_close;
  135. usb_make_path(udev, path, sizeof(path));
  136. sprintf(onetouch->phys, "%s/input0", path);
  137. onetouch->dev.name = onetouch->name;
  138. onetouch->dev.phys = onetouch->phys;
  139. usb_to_input_id(udev, &onetouch->dev.id);
  140. onetouch->dev.dev = &udev->dev;
  141. if (udev->manufacturer)
  142. strcat(onetouch->name, udev->manufacturer);
  143. if (udev->product)
  144. sprintf(onetouch->name, "%s %s", onetouch->name,
  145. udev->product);
  146. if (!strlen(onetouch->name))
  147. sprintf(onetouch->name, "Maxtor Onetouch %04x:%04x",
  148. onetouch->dev.id.vendor, onetouch->dev.id.product);
  149. usb_fill_int_urb(onetouch->irq, udev, pipe, onetouch->data,
  150. (maxp > 8 ? 8 : maxp),
  151. usb_onetouch_irq, onetouch, endpoint->bInterval);
  152. onetouch->irq->transfer_dma = onetouch->data_dma;
  153. onetouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  154. ss->extra_destructor = onetouch_release_input;
  155. ss->extra = onetouch;
  156. input_register_device(&onetouch->dev);
  157. printk(KERN_INFO "usb-input: %s on %s\n", onetouch->dev.name, path);
  158. return 0;
  159. }
  160. void onetouch_release_input(void *onetouch_)
  161. {
  162. struct usb_onetouch *onetouch = (struct usb_onetouch *) onetouch_;
  163. if (onetouch) {
  164. usb_kill_urb(onetouch->irq);
  165. input_unregister_device(&onetouch->dev);
  166. usb_free_urb(onetouch->irq);
  167. usb_buffer_free(onetouch->udev, ONETOUCH_PKT_LEN,
  168. onetouch->data, onetouch->data_dma);
  169. printk(KERN_INFO "usb-input: deregistering %s\n",
  170. onetouch->dev.name);
  171. }
  172. }