cypress_cy7c63.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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/module.h>
  32. #include <linux/kernel.h>
  33. #include <linux/slab.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 const 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_IRUGO | S_IWUSR, get_port0_handler, set_port0_handler);
  166. static DEVICE_ATTR(port1, S_IRUGO | S_IWUSR, get_port1_handler, set_port1_handler);
  167. static int cypress_probe(struct usb_interface *interface,
  168. const struct usb_device_id *id)
  169. {
  170. struct cypress *dev = NULL;
  171. int retval = -ENOMEM;
  172. /* allocate memory for our device state and initialize it */
  173. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  174. if (dev == NULL) {
  175. dev_err(&interface->dev, "Out of memory!\n");
  176. goto error_mem;
  177. }
  178. dev->udev = usb_get_dev(interface_to_usbdev(interface));
  179. /* save our data pointer in this interface device */
  180. usb_set_intfdata(interface, dev);
  181. /* create device attribute files */
  182. retval = device_create_file(&interface->dev, &dev_attr_port0);
  183. if (retval)
  184. goto error;
  185. retval = device_create_file(&interface->dev, &dev_attr_port1);
  186. if (retval)
  187. goto error;
  188. /* let the user know that the device is now attached */
  189. dev_info(&interface->dev,
  190. "Cypress CY7C63xxx device now attached\n");
  191. return 0;
  192. error:
  193. device_remove_file(&interface->dev, &dev_attr_port0);
  194. device_remove_file(&interface->dev, &dev_attr_port1);
  195. usb_set_intfdata(interface, NULL);
  196. usb_put_dev(dev->udev);
  197. kfree(dev);
  198. error_mem:
  199. return retval;
  200. }
  201. static void cypress_disconnect(struct usb_interface *interface)
  202. {
  203. struct cypress *dev;
  204. dev = usb_get_intfdata(interface);
  205. /* remove device attribute files */
  206. device_remove_file(&interface->dev, &dev_attr_port0);
  207. device_remove_file(&interface->dev, &dev_attr_port1);
  208. /* the intfdata can be set to NULL only after the
  209. * device files have been removed */
  210. usb_set_intfdata(interface, NULL);
  211. usb_put_dev(dev->udev);
  212. dev_info(&interface->dev,
  213. "Cypress CY7C63xxx device now disconnected\n");
  214. kfree(dev);
  215. }
  216. static struct usb_driver cypress_driver = {
  217. .name = "cypress_cy7c63",
  218. .probe = cypress_probe,
  219. .disconnect = cypress_disconnect,
  220. .id_table = cypress_table,
  221. };
  222. module_usb_driver(cypress_driver);
  223. MODULE_AUTHOR(DRIVER_AUTHOR);
  224. MODULE_DESCRIPTION(DRIVER_DESC);
  225. MODULE_LICENSE("GPL");