onetouch.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 Thyrén <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 "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. };
  49. static void usb_onetouch_irq(struct urb *urb, struct pt_regs *regs)
  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_regs(dev, regs);
  67. input_report_key(&onetouch->dev, ONETOUCH_BUTTON,
  68. 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->irq->dev = onetouch->udev;
  81. if (usb_submit_urb(onetouch->irq, GFP_KERNEL)) {
  82. err("usb_submit_urb failed");
  83. return -EIO;
  84. }
  85. return 0;
  86. }
  87. static void usb_onetouch_close(struct input_dev *dev)
  88. {
  89. struct usb_onetouch *onetouch = dev->private;
  90. usb_kill_urb(onetouch->irq);
  91. }
  92. int onetouch_connect_input(struct us_data *ss)
  93. {
  94. struct usb_device *udev = ss->pusb_dev;
  95. struct usb_host_interface *interface;
  96. struct usb_endpoint_descriptor *endpoint;
  97. struct usb_onetouch *onetouch;
  98. int pipe, maxp;
  99. char path[64];
  100. interface = ss->pusb_intf->cur_altsetting;
  101. endpoint = &interface->endpoint[2].desc;
  102. if(!(endpoint->bEndpointAddress & 0x80))
  103. return -ENODEV;
  104. if((endpoint->bmAttributes & 3) != 3)
  105. return -ENODEV;
  106. pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
  107. maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
  108. if (!(onetouch = kcalloc(1, sizeof(struct usb_onetouch), GFP_KERNEL)))
  109. return -ENOMEM;
  110. onetouch->data = usb_buffer_alloc(udev, ONETOUCH_PKT_LEN, SLAB_ATOMIC, &onetouch->data_dma);
  111. if (!onetouch->data){
  112. kfree(onetouch);
  113. return -ENOMEM;
  114. }
  115. onetouch->irq = usb_alloc_urb(0, GFP_KERNEL);
  116. if (!onetouch->irq){
  117. kfree(onetouch);
  118. usb_buffer_free(udev, ONETOUCH_PKT_LEN, onetouch->data, onetouch->data_dma);
  119. return -ENODEV;
  120. }
  121. onetouch->udev = udev;
  122. set_bit(EV_KEY, onetouch->dev.evbit);
  123. set_bit(ONETOUCH_BUTTON, onetouch->dev.keybit);
  124. clear_bit(0, onetouch->dev.keybit);
  125. onetouch->dev.private = onetouch;
  126. onetouch->dev.open = usb_onetouch_open;
  127. onetouch->dev.close = usb_onetouch_close;
  128. usb_make_path(udev, path, 64);
  129. sprintf(onetouch->phys, "%s/input0", path);
  130. onetouch->dev.name = onetouch->name;
  131. onetouch->dev.phys = onetouch->phys;
  132. onetouch->dev.id.bustype = BUS_USB;
  133. onetouch->dev.id.vendor = le16_to_cpu(udev->descriptor.idVendor);
  134. onetouch->dev.id.product = le16_to_cpu(udev->descriptor.idProduct);
  135. onetouch->dev.id.version = le16_to_cpu(udev->descriptor.bcdDevice);
  136. onetouch->dev.dev = &udev->dev;
  137. if (udev->manufacturer)
  138. strcat(onetouch->name, udev->manufacturer);
  139. if (udev->product)
  140. sprintf(onetouch->name, "%s %s", onetouch->name,
  141. udev->product);
  142. if (!strlen(onetouch->name))
  143. sprintf(onetouch->name, "Maxtor Onetouch %04x:%04x",
  144. onetouch->dev.id.vendor, onetouch->dev.id.product);
  145. usb_fill_int_urb(onetouch->irq, udev, pipe, onetouch->data,
  146. (maxp > 8 ? 8 : maxp),
  147. usb_onetouch_irq, onetouch, endpoint->bInterval);
  148. onetouch->irq->transfer_dma = onetouch->data_dma;
  149. onetouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  150. ss->extra_destructor = onetouch_release_input;
  151. ss->extra = onetouch;
  152. input_register_device(&onetouch->dev);
  153. printk(KERN_INFO "usb-input: %s on %s\n", onetouch->dev.name, path);
  154. return 0;
  155. }
  156. void onetouch_release_input(void *onetouch_)
  157. {
  158. struct usb_onetouch *onetouch = (struct usb_onetouch *) onetouch_;
  159. if (onetouch) {
  160. usb_kill_urb(onetouch->irq);
  161. input_unregister_device(&onetouch->dev);
  162. usb_free_urb(onetouch->irq);
  163. usb_buffer_free(onetouch->udev, ONETOUCH_PKT_LEN,
  164. onetouch->data, onetouch->data_dma);
  165. printk(KERN_INFO "Maxtor Onetouch %04x:%04x Deregistered\n",
  166. onetouch->dev.id.vendor, onetouch->dev.id.product);
  167. }
  168. }