cypress_cy7c63.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. retval = -ENOMEM;
  71. goto error;
  72. }
  73. dev_dbg(&dev->udev->dev, "Sending usb_control_msg (data: %d)\n", data);
  74. /* prepare usb control message and send it upstream */
  75. pipe = usb_rcvctrlpipe(dev->udev, 0);
  76. retval = usb_control_msg(dev->udev, pipe, request,
  77. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  78. address, data, iobuf, CYPRESS_MAX_REQSIZE,
  79. USB_CTRL_GET_TIMEOUT);
  80. /* store returned data (more READs to be added) */
  81. switch (request) {
  82. case CYPRESS_READ_PORT:
  83. if (address == CYPRESS_READ_PORT_ID0) {
  84. dev->port[0] = iobuf[1];
  85. dev_dbg(&dev->udev->dev,
  86. "READ_PORT0 returned: %d\n",
  87. dev->port[0]);
  88. }
  89. else if (address == CYPRESS_READ_PORT_ID1) {
  90. dev->port[1] = iobuf[1];
  91. dev_dbg(&dev->udev->dev,
  92. "READ_PORT1 returned: %d\n",
  93. dev->port[1]);
  94. }
  95. break;
  96. }
  97. kfree(iobuf);
  98. error:
  99. return retval;
  100. }
  101. /* write port value */
  102. static ssize_t write_port(struct device *dev, struct device_attribute *attr,
  103. const char *buf, size_t count,
  104. int port_num, int write_id)
  105. {
  106. int value = -1;
  107. int result = 0;
  108. struct usb_interface *intf = to_usb_interface(dev);
  109. struct cypress *cyp = usb_get_intfdata(intf);
  110. dev_dbg(&cyp->udev->dev, "WRITE_PORT%d called\n", port_num);
  111. /* validate input data */
  112. if (sscanf(buf, "%d", &value) < 1) {
  113. result = -EINVAL;
  114. goto error;
  115. }
  116. if (value < 0 || value > 255) {
  117. result = -EINVAL;
  118. goto error;
  119. }
  120. result = vendor_command(cyp, CYPRESS_WRITE_PORT, write_id,
  121. (unsigned char)value);
  122. dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result);
  123. error:
  124. return result < 0 ? result : count;
  125. }
  126. /* attribute callback handler (write) */
  127. static ssize_t set_port0_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, 0, CYPRESS_WRITE_PORT_ID0);
  132. }
  133. /* attribute callback handler (write) */
  134. static ssize_t set_port1_handler(struct device *dev,
  135. struct device_attribute *attr,
  136. const char *buf, size_t count)
  137. {
  138. return write_port(dev, attr, buf, count, 1, CYPRESS_WRITE_PORT_ID1);
  139. }
  140. /* read port value */
  141. static ssize_t read_port(struct device *dev, struct device_attribute *attr,
  142. char *buf, int port_num, int read_id)
  143. {
  144. int result = 0;
  145. struct usb_interface *intf = to_usb_interface(dev);
  146. struct cypress *cyp = usb_get_intfdata(intf);
  147. dev_dbg(&cyp->udev->dev, "READ_PORT%d called\n", port_num);
  148. result = vendor_command(cyp, CYPRESS_READ_PORT, read_id, 0);
  149. dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result);
  150. return sprintf(buf, "%d", cyp->port[port_num]);
  151. }
  152. /* attribute callback handler (read) */
  153. static ssize_t get_port0_handler(struct device *dev,
  154. struct device_attribute *attr, char *buf)
  155. {
  156. return read_port(dev, attr, buf, 0, CYPRESS_READ_PORT_ID0);
  157. }
  158. /* attribute callback handler (read) */
  159. static ssize_t get_port1_handler(struct device *dev,
  160. struct device_attribute *attr, char *buf)
  161. {
  162. return read_port(dev, attr, buf, 1, CYPRESS_READ_PORT_ID1);
  163. }
  164. static DEVICE_ATTR(port0, S_IRUGO | S_IWUSR, get_port0_handler, set_port0_handler);
  165. static DEVICE_ATTR(port1, S_IRUGO | S_IWUSR, 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)
  174. goto error_mem;
  175. dev->udev = usb_get_dev(interface_to_usbdev(interface));
  176. /* save our data pointer in this interface device */
  177. usb_set_intfdata(interface, dev);
  178. /* create device attribute files */
  179. retval = device_create_file(&interface->dev, &dev_attr_port0);
  180. if (retval)
  181. goto error;
  182. retval = device_create_file(&interface->dev, &dev_attr_port1);
  183. if (retval)
  184. goto error;
  185. /* let the user know that the device is now attached */
  186. dev_info(&interface->dev,
  187. "Cypress CY7C63xxx device now attached\n");
  188. return 0;
  189. error:
  190. device_remove_file(&interface->dev, &dev_attr_port0);
  191. device_remove_file(&interface->dev, &dev_attr_port1);
  192. usb_set_intfdata(interface, NULL);
  193. usb_put_dev(dev->udev);
  194. kfree(dev);
  195. error_mem:
  196. return retval;
  197. }
  198. static void cypress_disconnect(struct usb_interface *interface)
  199. {
  200. struct cypress *dev;
  201. dev = usb_get_intfdata(interface);
  202. /* remove device attribute files */
  203. device_remove_file(&interface->dev, &dev_attr_port0);
  204. device_remove_file(&interface->dev, &dev_attr_port1);
  205. /* the intfdata can be set to NULL only after the
  206. * device files have been removed */
  207. usb_set_intfdata(interface, NULL);
  208. usb_put_dev(dev->udev);
  209. dev_info(&interface->dev,
  210. "Cypress CY7C63xxx device now disconnected\n");
  211. kfree(dev);
  212. }
  213. static struct usb_driver cypress_driver = {
  214. .name = "cypress_cy7c63",
  215. .probe = cypress_probe,
  216. .disconnect = cypress_disconnect,
  217. .id_table = cypress_table,
  218. };
  219. module_usb_driver(cypress_driver);
  220. MODULE_AUTHOR(DRIVER_AUTHOR);
  221. MODULE_DESCRIPTION(DRIVER_DESC);
  222. MODULE_LICENSE("GPL");