cypress_cy7c63.c 7.0 KB

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