cypress_cy7c63.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * cypress_cy7c63.c
  3. *
  4. * Copyright (c) 2006-2007 Oliver Bock (bock@tfh-berlin.de)
  5. *
  6. * This driver is based on the Cypress USB Driver by Marcus Maul
  7. * (cyport) and the 2.0 version of Greg Kroah-Hartman's
  8. * USB Skeleton driver.
  9. *
  10. * This is a generic driver for the Cypress CY7C63xxx family.
  11. * For the time being it enables you to read from and write to
  12. * the single I/O ports of the device.
  13. *
  14. * Supported vendors: AK Modul-Bus Computer GmbH
  15. * (Firmware "Port-Chip")
  16. *
  17. * Supported devices: CY7C63001A-PC
  18. * CY7C63001C-PXC
  19. * CY7C63001C-SXC
  20. *
  21. * Supported functions: Read/Write Ports
  22. *
  23. *
  24. * For up-to-date information please visit:
  25. * http://www.obock.de/kernel/cypress
  26. *
  27. * This program is free software; you can redistribute it and/or
  28. * modify it under the terms of the GNU General Public License as
  29. * published by the Free Software Foundation, version 2.
  30. */
  31. #include <linux/init.h>
  32. #include <linux/module.h>
  33. #include <linux/kernel.h>
  34. #include <linux/usb.h>
  35. #define DRIVER_AUTHOR "Oliver Bock (bock@tfh-berlin.de)"
  36. #define DRIVER_DESC "Cypress CY7C63xxx USB driver"
  37. #define CYPRESS_VENDOR_ID 0xa2c
  38. #define CYPRESS_PRODUCT_ID 0x8
  39. #define CYPRESS_READ_PORT 0x4
  40. #define CYPRESS_WRITE_PORT 0x5
  41. #define CYPRESS_READ_RAM 0x2
  42. #define CYPRESS_WRITE_RAM 0x3
  43. #define CYPRESS_READ_ROM 0x1
  44. #define CYPRESS_READ_PORT_ID0 0
  45. #define CYPRESS_WRITE_PORT_ID0 0
  46. #define CYPRESS_READ_PORT_ID1 0x2
  47. #define CYPRESS_WRITE_PORT_ID1 1
  48. #define CYPRESS_MAX_REQSIZE 8
  49. /* table of devices that work with this driver */
  50. static struct usb_device_id cypress_table [] = {
  51. { USB_DEVICE(CYPRESS_VENDOR_ID, CYPRESS_PRODUCT_ID) },
  52. { }
  53. };
  54. MODULE_DEVICE_TABLE(usb, cypress_table);
  55. /* structure to hold all of our device specific stuff */
  56. struct cypress {
  57. struct usb_device * udev;
  58. unsigned char port[2];
  59. };
  60. /* used to send usb control messages to device */
  61. static int vendor_command(struct cypress *dev, unsigned char request,
  62. unsigned char address, unsigned char data)
  63. {
  64. int retval = 0;
  65. unsigned int pipe;
  66. unsigned char *iobuf;
  67. /* allocate some memory for the i/o buffer*/
  68. iobuf = kzalloc(CYPRESS_MAX_REQSIZE, GFP_KERNEL);
  69. if (!iobuf) {
  70. dev_err(&dev->udev->dev, "Out of memory!\n");
  71. retval = -ENOMEM;
  72. goto error;
  73. }
  74. dev_dbg(&dev->udev->dev, "Sending usb_control_msg (data: %d)\n", data);
  75. /* prepare usb control message and send it upstream */
  76. pipe = usb_rcvctrlpipe(dev->udev, 0);
  77. retval = usb_control_msg(dev->udev, pipe, request,
  78. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  79. address, data, iobuf, CYPRESS_MAX_REQSIZE,
  80. USB_CTRL_GET_TIMEOUT);
  81. /* store returned data (more READs to be added) */
  82. switch (request) {
  83. case CYPRESS_READ_PORT:
  84. if (address == CYPRESS_READ_PORT_ID0) {
  85. dev->port[0] = iobuf[1];
  86. dev_dbg(&dev->udev->dev,
  87. "READ_PORT0 returned: %d\n",
  88. dev->port[0]);
  89. }
  90. else if (address == CYPRESS_READ_PORT_ID1) {
  91. dev->port[1] = iobuf[1];
  92. dev_dbg(&dev->udev->dev,
  93. "READ_PORT1 returned: %d\n",
  94. dev->port[1]);
  95. }
  96. break;
  97. }
  98. kfree(iobuf);
  99. error:
  100. return retval;
  101. }
  102. /* write port value */
  103. static ssize_t write_port(struct device *dev, struct device_attribute *attr,
  104. const char *buf, size_t count,
  105. int port_num, int write_id)
  106. {
  107. int value = -1;
  108. int result = 0;
  109. struct usb_interface *intf = to_usb_interface(dev);
  110. struct cypress *cyp = usb_get_intfdata(intf);
  111. dev_dbg(&cyp->udev->dev, "WRITE_PORT%d called\n", port_num);
  112. /* validate input data */
  113. if (sscanf(buf, "%d", &value) < 1) {
  114. result = -EINVAL;
  115. goto error;
  116. }
  117. if (value < 0 || value > 255) {
  118. result = -EINVAL;
  119. goto error;
  120. }
  121. result = vendor_command(cyp, CYPRESS_WRITE_PORT, write_id,
  122. (unsigned char)value);
  123. dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result);
  124. error:
  125. return result < 0 ? result : count;
  126. }
  127. /* attribute callback handler (write) */
  128. static ssize_t set_port0_handler(struct device *dev,
  129. struct device_attribute *attr,
  130. const char *buf, size_t count)
  131. {
  132. return write_port(dev, attr, buf, count, 0, CYPRESS_WRITE_PORT_ID0);
  133. }
  134. /* attribute callback handler (write) */
  135. static ssize_t set_port1_handler(struct device *dev,
  136. struct device_attribute *attr,
  137. const char *buf, size_t count)
  138. {
  139. return write_port(dev, attr, buf, count, 1, CYPRESS_WRITE_PORT_ID1);
  140. }
  141. /* read port value */
  142. static ssize_t read_port(struct device *dev, struct device_attribute *attr,
  143. char *buf, int port_num, int read_id)
  144. {
  145. int result = 0;
  146. struct usb_interface *intf = to_usb_interface(dev);
  147. struct cypress *cyp = usb_get_intfdata(intf);
  148. dev_dbg(&cyp->udev->dev, "READ_PORT%d called\n", port_num);
  149. result = vendor_command(cyp, CYPRESS_READ_PORT, read_id, 0);
  150. dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result);
  151. return sprintf(buf, "%d", cyp->port[port_num]);
  152. }
  153. /* attribute callback handler (read) */
  154. static ssize_t get_port0_handler(struct device *dev,
  155. struct device_attribute *attr, char *buf)
  156. {
  157. return read_port(dev, attr, buf, 0, CYPRESS_READ_PORT_ID0);
  158. }
  159. /* attribute callback handler (read) */
  160. static ssize_t get_port1_handler(struct device *dev,
  161. struct device_attribute *attr, char *buf)
  162. {
  163. return read_port(dev, attr, buf, 1, CYPRESS_READ_PORT_ID1);
  164. }
  165. static DEVICE_ATTR(port0, S_IWUGO | S_IRUGO,
  166. get_port0_handler, set_port0_handler);
  167. static DEVICE_ATTR(port1, S_IWUGO | S_IRUGO,
  168. get_port1_handler, set_port1_handler);
  169. static int cypress_probe(struct usb_interface *interface,
  170. const struct usb_device_id *id)
  171. {
  172. struct cypress *dev = NULL;
  173. int retval = -ENOMEM;
  174. /* allocate memory for our device state and initialize it */
  175. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  176. if (dev == NULL) {
  177. dev_err(&interface->dev, "Out of memory!\n");
  178. goto error_mem;
  179. }
  180. dev->udev = usb_get_dev(interface_to_usbdev(interface));
  181. /* save our data pointer in this interface device */
  182. usb_set_intfdata(interface, dev);
  183. /* create device attribute files */
  184. retval = device_create_file(&interface->dev, &dev_attr_port0);
  185. if (retval)
  186. goto error;
  187. retval = device_create_file(&interface->dev, &dev_attr_port1);
  188. if (retval)
  189. goto error;
  190. /* let the user know that the device is now attached */
  191. dev_info(&interface->dev,
  192. "Cypress CY7C63xxx device now attached\n");
  193. return 0;
  194. error:
  195. device_remove_file(&interface->dev, &dev_attr_port0);
  196. device_remove_file(&interface->dev, &dev_attr_port1);
  197. usb_set_intfdata(interface, NULL);
  198. usb_put_dev(dev->udev);
  199. kfree(dev);
  200. error_mem:
  201. return retval;
  202. }
  203. static void cypress_disconnect(struct usb_interface *interface)
  204. {
  205. struct cypress *dev;
  206. dev = usb_get_intfdata(interface);
  207. /* remove device attribute files */
  208. device_remove_file(&interface->dev, &dev_attr_port0);
  209. device_remove_file(&interface->dev, &dev_attr_port1);
  210. /* the intfdata can be set to NULL only after the
  211. * device files have been removed */
  212. usb_set_intfdata(interface, NULL);
  213. usb_put_dev(dev->udev);
  214. dev_info(&interface->dev,
  215. "Cypress CY7C63xxx device now disconnected\n");
  216. kfree(dev);
  217. }
  218. static struct usb_driver cypress_driver = {
  219. .name = "cypress_cy7c63",
  220. .probe = cypress_probe,
  221. .disconnect = cypress_disconnect,
  222. .id_table = cypress_table,
  223. };
  224. static int __init cypress_init(void)
  225. {
  226. int result;
  227. /* register this driver with the USB subsystem */
  228. result = usb_register(&cypress_driver);
  229. if (result)
  230. printk(KERN_ERR KBUILD_MODNAME ": usb_register failed! "
  231. "Error number: %d\n", result);
  232. return result;
  233. }
  234. static void __exit cypress_exit(void)
  235. {
  236. /* deregister this driver with the USB subsystem */
  237. usb_deregister(&cypress_driver);
  238. }
  239. module_init(cypress_init);
  240. module_exit(cypress_exit);
  241. MODULE_AUTHOR(DRIVER_AUTHOR);
  242. MODULE_DESCRIPTION(DRIVER_DESC);
  243. MODULE_LICENSE("GPL");