4 * Copyright (c) 2006 Oliver Bock (o.bock@fh-wolfenbuettel.de)
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
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.
14 * Supported vendors: AK Modul-Bus Computer GmbH
15 * Supported devices: CY7C63001A-PC (to be continued...)
16 * Supported functions: Read/Write Ports (to be continued...)
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License as
21 * published by the Free Software Foundation, version 2.
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/usb.h>
29 #define DRIVER_AUTHOR "Oliver Bock (o.bock@fh-wolfenbuettel.de)"
30 #define DRIVER_DESC "Cypress CY7C63xxx USB driver"
32 #define CYPRESS_VENDOR_ID 0xa2c
33 #define CYPRESS_PRODUCT_ID 0x8
35 #define CYPRESS_READ_PORT 0x4
36 #define CYPRESS_WRITE_PORT 0x5
38 #define CYPRESS_READ_RAM 0x2
39 #define CYPRESS_WRITE_RAM 0x3
40 #define CYPRESS_READ_ROM 0x1
42 #define CYPRESS_READ_PORT_ID0 0
43 #define CYPRESS_WRITE_PORT_ID0 0
44 #define CYPRESS_READ_PORT_ID1 0x2
45 #define CYPRESS_WRITE_PORT_ID1 1
47 #define CYPRESS_MAX_REQSIZE 8
50 /* table of devices that work with this driver */
51 static struct usb_device_id cypress_table [] = {
52 { USB_DEVICE(CYPRESS_VENDOR_ID, CYPRESS_PRODUCT_ID) },
55 MODULE_DEVICE_TABLE(usb, cypress_table);
57 /* structure to hold all of our device specific stuff */
59 struct usb_device * udev;
60 unsigned char port[2];
63 /* used to send usb control messages to device */
64 static int vendor_command(struct cypress *dev, unsigned char request,
65 unsigned char address, unsigned char data)
71 /* allocate some memory for the i/o buffer*/
72 iobuf = kzalloc(CYPRESS_MAX_REQSIZE, GFP_KERNEL);
74 dev_err(&dev->udev->dev, "Out of memory!\n");
79 dev_dbg(&dev->udev->dev, "Sending usb_control_msg (data: %d)\n", data);
81 /* prepare usb control message and send it upstream */
82 pipe = usb_rcvctrlpipe(dev->udev, 0);
83 retval = usb_control_msg(dev->udev, pipe, request,
84 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
85 address, data, iobuf, CYPRESS_MAX_REQSIZE,
86 USB_CTRL_GET_TIMEOUT);
88 /* store returned data (more READs to be added) */
90 case CYPRESS_READ_PORT:
91 if (address == CYPRESS_READ_PORT_ID0) {
92 dev->port[0] = iobuf[1];
93 dev_dbg(&dev->udev->dev,
94 "READ_PORT0 returned: %d\n",
97 else if (address == CYPRESS_READ_PORT_ID1) {
98 dev->port[1] = iobuf[1];
99 dev_dbg(&dev->udev->dev,
100 "READ_PORT1 returned: %d\n",
111 /* write port value */
112 static ssize_t write_port(struct device *dev, struct device_attribute *attr,
113 const char *buf, size_t count,
114 int port_num, int write_id)
119 struct usb_interface *intf = to_usb_interface(dev);
120 struct cypress *cyp = usb_get_intfdata(intf);
122 dev_dbg(&cyp->udev->dev, "WRITE_PORT%d called\n", port_num);
124 /* validate input data */
125 if (sscanf(buf, "%d", &value) < 1) {
129 if (value < 0 || value > 255) {
134 result = vendor_command(cyp, CYPRESS_WRITE_PORT, write_id,
135 (unsigned char)value);
137 dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result);
139 return result < 0 ? result : count;
142 /* attribute callback handler (write) */
143 static ssize_t set_port0_handler(struct device *dev,
144 struct device_attribute *attr,
145 const char *buf, size_t count)
147 return write_port(dev, attr, buf, count, 0, CYPRESS_WRITE_PORT_ID0);
150 /* attribute callback handler (write) */
151 static ssize_t set_port1_handler(struct device *dev,
152 struct device_attribute *attr,
153 const char *buf, size_t count)
155 return write_port(dev, attr, buf, count, 1, CYPRESS_WRITE_PORT_ID1);
158 /* read port value */
159 static ssize_t read_port(struct device *dev, struct device_attribute *attr,
160 char *buf, int port_num, int read_id)
164 struct usb_interface *intf = to_usb_interface(dev);
165 struct cypress *cyp = usb_get_intfdata(intf);
167 dev_dbg(&cyp->udev->dev, "READ_PORT%d called\n", port_num);
169 result = vendor_command(cyp, CYPRESS_READ_PORT, read_id, 0);
171 dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result);
173 return sprintf(buf, "%d", cyp->port[port_num]);
176 /* attribute callback handler (read) */
177 static ssize_t get_port0_handler(struct device *dev,
178 struct device_attribute *attr, char *buf)
180 return read_port(dev, attr, buf, 0, CYPRESS_READ_PORT_ID0);
183 /* attribute callback handler (read) */
184 static ssize_t get_port1_handler(struct device *dev,
185 struct device_attribute *attr, char *buf)
187 return read_port(dev, attr, buf, 1, CYPRESS_READ_PORT_ID1);
190 static DEVICE_ATTR(port0, S_IWUGO | S_IRUGO,
191 get_port0_handler, set_port0_handler);
193 static DEVICE_ATTR(port1, S_IWUGO | S_IRUGO,
194 get_port1_handler, set_port1_handler);
197 static int cypress_probe(struct usb_interface *interface,
198 const struct usb_device_id *id)
200 struct cypress *dev = NULL;
201 int retval = -ENOMEM;
203 /* allocate memory for our device state and initialize it */
204 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
206 dev_err(&dev->udev->dev, "Out of memory!\n");
210 dev->udev = usb_get_dev(interface_to_usbdev(interface));
212 /* save our data pointer in this interface device */
213 usb_set_intfdata(interface, dev);
215 /* create device attribute files */
216 device_create_file(&interface->dev, &dev_attr_port0);
217 device_create_file(&interface->dev, &dev_attr_port1);
219 /* let the user know that the device is now attached */
220 dev_info(&interface->dev,
221 "Cypress CY7C63xxx device now attached\n");
228 static void cypress_disconnect(struct usb_interface *interface)
232 dev = usb_get_intfdata(interface);
233 usb_set_intfdata(interface, NULL);
235 /* remove device attribute files */
236 device_remove_file(&interface->dev, &dev_attr_port0);
237 device_remove_file(&interface->dev, &dev_attr_port1);
239 usb_put_dev(dev->udev);
241 dev_info(&interface->dev,
242 "Cypress CY7C63xxx device now disconnected\n");
247 static struct usb_driver cypress_driver = {
248 .name = "cypress_cy7c63",
249 .probe = cypress_probe,
250 .disconnect = cypress_disconnect,
251 .id_table = cypress_table,
254 static int __init cypress_init(void)
258 /* register this driver with the USB subsystem */
259 result = usb_register(&cypress_driver);
261 err("Function usb_register failed! Error number: %d\n", result);
267 static void __exit cypress_exit(void)
269 /* deregister this driver with the USB subsystem */
270 usb_deregister(&cypress_driver);
273 module_init(cypress_init);
274 module_exit(cypress_exit);
276 MODULE_AUTHOR(DRIVER_AUTHOR);
277 MODULE_DESCRIPTION(DRIVER_DESC);
279 MODULE_LICENSE("GPL");