cypress_cy7c63.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * cypress_cy7c63.c
  4. *
  5. * Copyright (c) 2006-2007 Oliver Bock ([email protected])
  6. *
  7. * This driver is based on the Cypress USB Driver by Marcus Maul
  8. * (cyport) and the 2.0 version of Greg Kroah-Hartman's
  9. * USB Skeleton driver.
  10. *
  11. * This is a generic driver for the Cypress CY7C63xxx family.
  12. * For the time being it enables you to read from and write to
  13. * the single I/O ports of the device.
  14. *
  15. * Supported vendors: AK Modul-Bus Computer GmbH
  16. * (Firmware "Port-Chip")
  17. *
  18. * Supported devices: CY7C63001A-PC
  19. * CY7C63001C-PXC
  20. * CY7C63001C-SXC
  21. *
  22. * Supported functions: Read/Write Ports
  23. *
  24. *
  25. * For up-to-date information please visit:
  26. * http://www.obock.de/kernel/cypress
  27. */
  28. #include <linux/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include <linux/usb.h>
  32. #define DRIVER_AUTHOR "Oliver Bock ([email protected])"
  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 const 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. retval = -ENOMEM;
  68. goto error;
  69. }
  70. dev_dbg(&dev->udev->dev, "Sending usb_control_msg (data: %d)\n", data);
  71. /* prepare usb control message and send it upstream */
  72. pipe = usb_rcvctrlpipe(dev->udev, 0);
  73. retval = usb_control_msg(dev->udev, pipe, request,
  74. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  75. address, data, iobuf, CYPRESS_MAX_REQSIZE,
  76. USB_CTRL_GET_TIMEOUT);
  77. /* store returned data (more READs to be added) */
  78. switch (request) {
  79. case CYPRESS_READ_PORT:
  80. if (address == CYPRESS_READ_PORT_ID0) {
  81. dev->port[0] = iobuf[1];
  82. dev_dbg(&dev->udev->dev,
  83. "READ_PORT0 returned: %d\n",
  84. dev->port[0]);
  85. }
  86. else if (address == CYPRESS_READ_PORT_ID1) {
  87. dev->port[1] = iobuf[1];
  88. dev_dbg(&dev->udev->dev,
  89. "READ_PORT1 returned: %d\n",
  90. dev->port[1]);
  91. }
  92. break;
  93. }
  94. kfree(iobuf);
  95. error:
  96. return retval;
  97. }
  98. /* write port value */
  99. static ssize_t write_port(struct device *dev, struct device_attribute *attr,
  100. const char *buf, size_t count,
  101. int port_num, int write_id)
  102. {
  103. int value = -1;
  104. int result = 0;
  105. struct usb_interface *intf = to_usb_interface(dev);
  106. struct cypress *cyp = usb_get_intfdata(intf);
  107. dev_dbg(&cyp->udev->dev, "WRITE_PORT%d called\n", port_num);
  108. /* validate input data */
  109. if (sscanf(buf, "%d", &value) < 1) {
  110. result = -EINVAL;
  111. goto error;
  112. }
  113. if (value < 0 || value > 255) {
  114. result = -EINVAL;
  115. goto error;
  116. }
  117. result = vendor_command(cyp, CYPRESS_WRITE_PORT, write_id,
  118. (unsigned char)value);
  119. dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result);
  120. error:
  121. return result < 0 ? result : count;
  122. }
  123. /* attribute callback handler (write) */
  124. static ssize_t port0_store(struct device *dev,
  125. struct device_attribute *attr,
  126. const char *buf, size_t count)
  127. {
  128. return write_port(dev, attr, buf, count, 0, CYPRESS_WRITE_PORT_ID0);
  129. }
  130. /* attribute callback handler (write) */
  131. static ssize_t port1_store(struct device *dev,
  132. struct device_attribute *attr,
  133. const char *buf, size_t count)
  134. {
  135. return write_port(dev, attr, buf, count, 1, CYPRESS_WRITE_PORT_ID1);
  136. }
  137. /* read port value */
  138. static ssize_t read_port(struct device *dev, struct device_attribute *attr,
  139. char *buf, int port_num, int read_id)
  140. {
  141. int result = 0;
  142. struct usb_interface *intf = to_usb_interface(dev);
  143. struct cypress *cyp = usb_get_intfdata(intf);
  144. dev_dbg(&cyp->udev->dev, "READ_PORT%d called\n", port_num);
  145. result = vendor_command(cyp, CYPRESS_READ_PORT, read_id, 0);
  146. dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result);
  147. return sprintf(buf, "%d", cyp->port[port_num]);
  148. }
  149. /* attribute callback handler (read) */
  150. static ssize_t port0_show(struct device *dev,
  151. struct device_attribute *attr, char *buf)
  152. {
  153. return read_port(dev, attr, buf, 0, CYPRESS_READ_PORT_ID0);
  154. }
  155. static DEVICE_ATTR_RW(port0);
  156. /* attribute callback handler (read) */
  157. static ssize_t port1_show(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_RW(port1);
  163. static struct attribute *cypress_attrs[] = {
  164. &dev_attr_port0.attr,
  165. &dev_attr_port1.attr,
  166. NULL,
  167. };
  168. ATTRIBUTE_GROUPS(cypress);
  169. static int cypress_probe(struct usb_interface *interface,
  170. const struct usb_device_id *id)
  171. {
  172. struct cypress *dev = NULL;
  173. int retval = -ENOMEM;
  174. /* allocate memory for our device state and initialize it */
  175. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  176. if (!dev)
  177. goto error_mem;
  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. /* let the user know that the device is now attached */
  182. dev_info(&interface->dev,
  183. "Cypress CY7C63xxx device now attached\n");
  184. return 0;
  185. error_mem:
  186. return retval;
  187. }
  188. static void cypress_disconnect(struct usb_interface *interface)
  189. {
  190. struct cypress *dev;
  191. dev = usb_get_intfdata(interface);
  192. /* the intfdata can be set to NULL only after the
  193. * device files have been removed */
  194. usb_set_intfdata(interface, NULL);
  195. usb_put_dev(dev->udev);
  196. dev_info(&interface->dev,
  197. "Cypress CY7C63xxx device now disconnected\n");
  198. kfree(dev);
  199. }
  200. static struct usb_driver cypress_driver = {
  201. .name = "cypress_cy7c63",
  202. .probe = cypress_probe,
  203. .disconnect = cypress_disconnect,
  204. .id_table = cypress_table,
  205. .dev_groups = cypress_groups,
  206. };
  207. module_usb_driver(cypress_driver);
  208. MODULE_AUTHOR(DRIVER_AUTHOR);
  209. MODULE_DESCRIPTION(DRIVER_DESC);
  210. MODULE_LICENSE("GPL");