cy7c63.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * cy7c63.c
  3. *
  4. * Copyright (c) 2006 Oliver Bock (bock@fh-wolfenbuettel.de)
  5. *
  6. * This driver is based on the Cypress Thermometer USB Driver by
  7. * Marcus Maul and the 2.0 version of Greg Kroah-Hartman's
  8. * USB Skeleton driver.
  9. *
  10. * Is is a generic driver for the Cypress CY7C63000 family.
  11. * For the time being it enables you to toggle the single I/O ports
  12. * of the device.
  13. *
  14. * Supported vendors: AK Modul-Bus Computer GmbH
  15. * Supported devices: CY7C63001A-PC (to be continued...)
  16. * Supported functions: Read/Write Ports (to be continued...)
  17. *
  18. * Chipsets families: CY7C63000, CY7C63001, CY7C63100, CY7C63101
  19. *
  20. *
  21. * This program is free software; you can redistribute it and/or
  22. * modify it under the terms of the GNU General Public License as
  23. * published by the Free Software Foundation, version 2.
  24. */
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/kernel.h>
  28. #include <linux/usb.h>
  29. #define DRIVER_AUTHOR "Oliver Bock (bock@fh-wolfenbuettel.de)"
  30. #define DRIVER_DESC "Cypress CY7C63xxx USB driver"
  31. #define CY7C63_VENDOR_ID 0xa2c
  32. #define CY7C63_PRODUCT_ID 0x8
  33. #define CY7C63_READ_PORT 0x4
  34. #define CY7C63_WRITE_PORT 0x5
  35. #define CY7C63_READ_RAM 0x2
  36. #define CY7C63_WRITE_RAM 0x3
  37. #define CY7C63_READ_ROM 0x1
  38. #define CY7C63_READ_PORT_ID0 0
  39. #define CY7C63_WRITE_PORT_ID0 0
  40. #define CY7C63_READ_PORT_ID1 0x2
  41. #define CY7C63_WRITE_PORT_ID1 1
  42. #define CY7C63_MAX_REQSIZE 8
  43. /* table of devices that work with this driver */
  44. static struct usb_device_id cy7c63_table [] = {
  45. { USB_DEVICE(CY7C63_VENDOR_ID, CY7C63_PRODUCT_ID) },
  46. { }
  47. };
  48. MODULE_DEVICE_TABLE(usb, cy7c63_table);
  49. /* structure to hold all of our device specific stuff */
  50. struct cy7c63 {
  51. struct usb_device * udev;
  52. char port0;
  53. char port1;
  54. };
  55. /* used to send usb control messages to device */
  56. int vendor_command(struct cy7c63 *dev, unsigned char request,
  57. unsigned char address, unsigned char data) {
  58. int retval = 0;
  59. unsigned int pipe;
  60. unsigned char *iobuf;
  61. /* allocate some memory for the i/o buffer*/
  62. iobuf = kzalloc(CY7C63_MAX_REQSIZE, GFP_KERNEL);
  63. if (!iobuf) {
  64. dev_err(&dev->udev->dev, "Out of memory!\n");
  65. retval = -ENOMEM;
  66. goto error;
  67. }
  68. dev_dbg(&dev->udev->dev, "Sending usb_control_msg (data: %d)\n", data);
  69. /* prepare usb control message and send it upstream */
  70. pipe = usb_rcvctrlpipe(dev->udev, 0);
  71. retval = usb_control_msg(dev->udev, pipe, request,
  72. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  73. address, data, iobuf, CY7C63_MAX_REQSIZE,
  74. USB_CTRL_GET_TIMEOUT);
  75. /* store returned data (more READs to be added!) */
  76. switch (request) {
  77. case CY7C63_READ_PORT:
  78. if (address == CY7C63_READ_PORT_ID0) {
  79. dev->port0 = iobuf[1];
  80. dev_dbg(&dev->udev->dev,
  81. "READ_PORT0 returned: %d\n",dev->port0);
  82. }
  83. else if (address == CY7C63_READ_PORT_ID1) {
  84. dev->port1 = iobuf[1];
  85. dev_dbg(&dev->udev->dev,
  86. "READ_PORT1 returned: %d\n",dev->port1);
  87. }
  88. break;
  89. }
  90. kfree(iobuf);
  91. error:
  92. return retval;
  93. }
  94. #define get_set_port(num,read_id,write_id) \
  95. static ssize_t set_port##num(struct device *dev, struct device_attribute *attr, \
  96. const char *buf, size_t count) { \
  97. \
  98. int value; \
  99. int result = 0; \
  100. \
  101. struct usb_interface *intf = to_usb_interface(dev); \
  102. struct cy7c63 *cyp = usb_get_intfdata(intf); \
  103. \
  104. dev_dbg(&cyp->udev->dev, "WRITE_PORT%d called\n", num); \
  105. \
  106. /* validate input data */ \
  107. if (sscanf(buf, "%d", &value) < 1) { \
  108. result = -EINVAL; \
  109. goto error; \
  110. } \
  111. if (value>255 || value<0) { \
  112. result = -EINVAL; \
  113. goto error; \
  114. } \
  115. \
  116. result = vendor_command(cyp, CY7C63_WRITE_PORT, write_id, \
  117. (unsigned char)value); \
  118. \
  119. dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n",result); \
  120. error: \
  121. return result < 0 ? result : count; \
  122. } \
  123. \
  124. static ssize_t get_port##num(struct device *dev, \
  125. struct device_attribute *attr, char *buf) { \
  126. \
  127. int result = 0; \
  128. \
  129. struct usb_interface *intf = to_usb_interface(dev); \
  130. struct cy7c63 *cyp = usb_get_intfdata(intf); \
  131. \
  132. dev_dbg(&cyp->udev->dev, "READ_PORT%d called\n", num); \
  133. \
  134. result = vendor_command(cyp, CY7C63_READ_PORT, read_id, 0); \
  135. \
  136. dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result); \
  137. \
  138. return sprintf(buf, "%d", cyp->port##num); \
  139. } \
  140. static DEVICE_ATTR(port##num, S_IWUGO | S_IRUGO, get_port##num, set_port##num);
  141. get_set_port(0, CY7C63_READ_PORT_ID0, CY7C63_WRITE_PORT_ID0);
  142. get_set_port(1, CY7C63_READ_PORT_ID1, CY7C63_WRITE_PORT_ID1);
  143. static int cy7c63_probe(struct usb_interface *interface,
  144. const struct usb_device_id *id) {
  145. struct cy7c63 *dev = NULL;
  146. int retval = -ENOMEM;
  147. /* allocate memory for our device state and initialize it */
  148. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  149. if (dev == NULL) {
  150. dev_err(&dev->udev->dev, "Out of memory!\n");
  151. goto error;
  152. }
  153. dev->udev = usb_get_dev(interface_to_usbdev(interface));
  154. /* save our data pointer in this interface device */
  155. usb_set_intfdata(interface, dev);
  156. /* create device attribute files */
  157. device_create_file(&interface->dev, &dev_attr_port0);
  158. device_create_file(&interface->dev, &dev_attr_port1);
  159. /* let the user know what node this device is now attached to */
  160. dev_info(&interface->dev,
  161. "Cypress CY7C63xxx device now attached\n");
  162. retval = 0;
  163. error:
  164. return retval;
  165. }
  166. static void cy7c63_disconnect(struct usb_interface *interface) {
  167. struct cy7c63 *dev;
  168. dev = usb_get_intfdata(interface);
  169. usb_set_intfdata(interface, NULL);
  170. /* remove device attribute files */
  171. device_remove_file(&interface->dev, &dev_attr_port0);
  172. device_remove_file(&interface->dev, &dev_attr_port1);
  173. usb_put_dev(dev->udev);
  174. dev_info(&interface->dev,
  175. "Cypress CY7C63xxx device now disconnected\n");
  176. kfree(dev);
  177. }
  178. static struct usb_driver cy7c63_driver = {
  179. .name = "cy7c63",
  180. .probe = cy7c63_probe,
  181. .disconnect = cy7c63_disconnect,
  182. .id_table = cy7c63_table,
  183. };
  184. static int __init cy7c63_init(void) {
  185. int result;
  186. /* register this driver with the USB subsystem */
  187. result = usb_register(&cy7c63_driver);
  188. if (result) {
  189. err("Function usb_register failed! Error number: %d\n", result);
  190. }
  191. return result;
  192. }
  193. static void __exit cy7c63_exit(void) {
  194. /* deregister this driver with the USB subsystem */
  195. usb_deregister(&cy7c63_driver);
  196. }
  197. module_init(cy7c63_init);
  198. module_exit(cy7c63_exit);
  199. MODULE_AUTHOR(DRIVER_AUTHOR);
  200. MODULE_DESCRIPTION(DRIVER_DESC);
  201. MODULE_LICENSE("GPL");