onetouch.c 8.3 KB

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