cypress_cy7c63.c 7.1 KB

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