cypress_cy7c63.c 7.1 KB

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