onetouch.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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/slab.h>
  32. #include <linux/module.h>
  33. #include <linux/usb/input.h>
  34. #include "usb.h"
  35. #include "debug.h"
  36. MODULE_DESCRIPTION("Maxtor USB OneTouch hard drive button driver");
  37. MODULE_AUTHOR("Nick Sillik <n.sillik@temple.edu>");
  38. MODULE_LICENSE("GPL");
  39. #define ONETOUCH_PKT_LEN 0x02
  40. #define ONETOUCH_BUTTON KEY_PROG1
  41. static int onetouch_connect_input(struct us_data *ss);
  42. static void onetouch_release_input(void *onetouch_);
  43. struct usb_onetouch {
  44. char name[128];
  45. char phys[64];
  46. struct input_dev *dev; /* input device interface */
  47. struct usb_device *udev; /* usb device */
  48. struct urb *irq; /* urb for interrupt in report */
  49. unsigned char *data; /* input data */
  50. dma_addr_t data_dma;
  51. unsigned int is_open:1;
  52. };
  53. /*
  54. * The table of devices
  55. */
  56. #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
  57. vendorName, productName, useProtocol, useTransport, \
  58. initFunction, flags) \
  59. { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
  60. .driver_info = (flags) }
  61. static struct usb_device_id onetouch_usb_ids[] = {
  62. # include "unusual_onetouch.h"
  63. { } /* Terminating entry */
  64. };
  65. MODULE_DEVICE_TABLE(usb, onetouch_usb_ids);
  66. #undef UNUSUAL_DEV
  67. /*
  68. * The flags table
  69. */
  70. #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
  71. vendor_name, product_name, use_protocol, use_transport, \
  72. init_function, Flags) \
  73. { \
  74. .vendorName = vendor_name, \
  75. .productName = product_name, \
  76. .useProtocol = use_protocol, \
  77. .useTransport = use_transport, \
  78. .initFunction = init_function, \
  79. }
  80. static struct us_unusual_dev onetouch_unusual_dev_list[] = {
  81. # include "unusual_onetouch.h"
  82. { } /* Terminating entry */
  83. };
  84. #undef UNUSUAL_DEV
  85. static void usb_onetouch_irq(struct urb *urb)
  86. {
  87. struct usb_onetouch *onetouch = urb->context;
  88. signed char *data = onetouch->data;
  89. struct input_dev *dev = onetouch->dev;
  90. int status = urb->status;
  91. int retval;
  92. switch (status) {
  93. case 0: /* success */
  94. break;
  95. case -ECONNRESET: /* unlink */
  96. case -ENOENT:
  97. case -ESHUTDOWN:
  98. return;
  99. /* -EPIPE: should clear the halt */
  100. default: /* error */
  101. goto resubmit;
  102. }
  103. input_report_key(dev, ONETOUCH_BUTTON, data[0] & 0x02);
  104. input_sync(dev);
  105. resubmit:
  106. retval = usb_submit_urb (urb, GFP_ATOMIC);
  107. if (retval)
  108. dev_err(&dev->dev, "can't resubmit intr, %s-%s/input0, "
  109. "retval %d\n", onetouch->udev->bus->bus_name,
  110. onetouch->udev->devpath, retval);
  111. }
  112. static int usb_onetouch_open(struct input_dev *dev)
  113. {
  114. struct usb_onetouch *onetouch = input_get_drvdata(dev);
  115. onetouch->is_open = 1;
  116. onetouch->irq->dev = onetouch->udev;
  117. if (usb_submit_urb(onetouch->irq, GFP_KERNEL)) {
  118. dev_err(&dev->dev, "usb_submit_urb failed\n");
  119. return -EIO;
  120. }
  121. return 0;
  122. }
  123. static void usb_onetouch_close(struct input_dev *dev)
  124. {
  125. struct usb_onetouch *onetouch = input_get_drvdata(dev);
  126. usb_kill_urb(onetouch->irq);
  127. onetouch->is_open = 0;
  128. }
  129. #ifdef CONFIG_PM
  130. static void usb_onetouch_pm_hook(struct us_data *us, int action)
  131. {
  132. struct usb_onetouch *onetouch = (struct usb_onetouch *) us->extra;
  133. if (onetouch->is_open) {
  134. switch (action) {
  135. case US_SUSPEND:
  136. usb_kill_urb(onetouch->irq);
  137. break;
  138. case US_RESUME:
  139. if (usb_submit_urb(onetouch->irq, GFP_NOIO) != 0)
  140. dev_err(&onetouch->irq->dev->dev,
  141. "usb_submit_urb failed\n");
  142. break;
  143. default:
  144. break;
  145. }
  146. }
  147. }
  148. #endif /* CONFIG_PM */
  149. static int onetouch_connect_input(struct us_data *ss)
  150. {
  151. struct usb_device *udev = ss->pusb_dev;
  152. struct usb_host_interface *interface;
  153. struct usb_endpoint_descriptor *endpoint;
  154. struct usb_onetouch *onetouch;
  155. struct input_dev *input_dev;
  156. int pipe, maxp;
  157. int error = -ENOMEM;
  158. interface = ss->pusb_intf->cur_altsetting;
  159. if (interface->desc.bNumEndpoints != 3)
  160. return -ENODEV;
  161. endpoint = &interface->endpoint[2].desc;
  162. if (!usb_endpoint_is_int_in(endpoint))
  163. return -ENODEV;
  164. pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
  165. maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
  166. maxp = min(maxp, ONETOUCH_PKT_LEN);
  167. onetouch = kzalloc(sizeof(struct usb_onetouch), GFP_KERNEL);
  168. input_dev = input_allocate_device();
  169. if (!onetouch || !input_dev)
  170. goto fail1;
  171. onetouch->data = usb_alloc_coherent(udev, ONETOUCH_PKT_LEN,
  172. GFP_KERNEL, &onetouch->data_dma);
  173. if (!onetouch->data)
  174. goto fail1;
  175. onetouch->irq = usb_alloc_urb(0, GFP_KERNEL);
  176. if (!onetouch->irq)
  177. goto fail2;
  178. onetouch->udev = udev;
  179. onetouch->dev = input_dev;
  180. if (udev->manufacturer)
  181. strlcpy(onetouch->name, udev->manufacturer,
  182. sizeof(onetouch->name));
  183. if (udev->product) {
  184. if (udev->manufacturer)
  185. strlcat(onetouch->name, " ", sizeof(onetouch->name));
  186. strlcat(onetouch->name, udev->product, sizeof(onetouch->name));
  187. }
  188. if (!strlen(onetouch->name))
  189. snprintf(onetouch->name, sizeof(onetouch->name),
  190. "Maxtor Onetouch %04x:%04x",
  191. le16_to_cpu(udev->descriptor.idVendor),
  192. le16_to_cpu(udev->descriptor.idProduct));
  193. usb_make_path(udev, onetouch->phys, sizeof(onetouch->phys));
  194. strlcat(onetouch->phys, "/input0", sizeof(onetouch->phys));
  195. input_dev->name = onetouch->name;
  196. input_dev->phys = onetouch->phys;
  197. usb_to_input_id(udev, &input_dev->id);
  198. input_dev->dev.parent = &udev->dev;
  199. set_bit(EV_KEY, input_dev->evbit);
  200. set_bit(ONETOUCH_BUTTON, input_dev->keybit);
  201. clear_bit(0, input_dev->keybit);
  202. input_set_drvdata(input_dev, onetouch);
  203. input_dev->open = usb_onetouch_open;
  204. input_dev->close = usb_onetouch_close;
  205. usb_fill_int_urb(onetouch->irq, udev, pipe, onetouch->data, maxp,
  206. usb_onetouch_irq, onetouch, endpoint->bInterval);
  207. onetouch->irq->transfer_dma = onetouch->data_dma;
  208. onetouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  209. ss->extra_destructor = onetouch_release_input;
  210. ss->extra = onetouch;
  211. #ifdef CONFIG_PM
  212. ss->suspend_resume_hook = usb_onetouch_pm_hook;
  213. #endif
  214. error = input_register_device(onetouch->dev);
  215. if (error)
  216. goto fail3;
  217. return 0;
  218. fail3: usb_free_urb(onetouch->irq);
  219. fail2: usb_free_coherent(udev, ONETOUCH_PKT_LEN,
  220. onetouch->data, onetouch->data_dma);
  221. fail1: kfree(onetouch);
  222. input_free_device(input_dev);
  223. return error;
  224. }
  225. static void onetouch_release_input(void *onetouch_)
  226. {
  227. struct usb_onetouch *onetouch = (struct usb_onetouch *) onetouch_;
  228. if (onetouch) {
  229. usb_kill_urb(onetouch->irq);
  230. input_unregister_device(onetouch->dev);
  231. usb_free_urb(onetouch->irq);
  232. usb_free_coherent(onetouch->udev, ONETOUCH_PKT_LEN,
  233. onetouch->data, onetouch->data_dma);
  234. }
  235. }
  236. static int onetouch_probe(struct usb_interface *intf,
  237. const struct usb_device_id *id)
  238. {
  239. struct us_data *us;
  240. int result;
  241. result = usb_stor_probe1(&us, intf, id,
  242. (id - onetouch_usb_ids) + onetouch_unusual_dev_list);
  243. if (result)
  244. return result;
  245. /* Use default transport and protocol */
  246. result = usb_stor_probe2(us);
  247. return result;
  248. }
  249. static struct usb_driver onetouch_driver = {
  250. .name = "ums-onetouch",
  251. .probe = onetouch_probe,
  252. .disconnect = usb_stor_disconnect,
  253. .suspend = usb_stor_suspend,
  254. .resume = usb_stor_resume,
  255. .reset_resume = usb_stor_reset_resume,
  256. .pre_reset = usb_stor_pre_reset,
  257. .post_reset = usb_stor_post_reset,
  258. .id_table = onetouch_usb_ids,
  259. .soft_unbind = 1,
  260. .no_dynamic_id = 1,
  261. };
  262. module_usb_driver(onetouch_driver);