onetouch.c 8.1 KB

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