4 * Copyright (c) 2006-2007 Oliver Bock (bock@tfh-berlin.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 * (Firmware "Port-Chip")
17 * Supported devices: CY7C63001A-PC
21 * Supported functions: Read/Write Ports
24 * For up-to-date information please visit:
25 * http://www.obock.de/kernel/cypress
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.
32 #include <linux/init.h>
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/usb.h>
37 #define DRIVER_AUTHOR "Oliver Bock (bock@tfh-berlin.de)"
38 #define DRIVER_DESC "Cypress CY7C63xxx USB driver"
40 #define CYPRESS_VENDOR_ID 0xa2c
41 #define CYPRESS_PRODUCT_ID 0x8
43 #define CYPRESS_READ_PORT 0x4
44 #define CYPRESS_WRITE_PORT 0x5
46 #define CYPRESS_READ_RAM 0x2
47 #define CYPRESS_WRITE_RAM 0x3
48 #define CYPRESS_READ_ROM 0x1
50 #define CYPRESS_READ_PORT_ID0 0
51 #define CYPRESS_WRITE_PORT_ID0 0
52 #define CYPRESS_READ_PORT_ID1 0x2
53 #define CYPRESS_WRITE_PORT_ID1 1
55 #define CYPRESS_MAX_REQSIZE 8
58 /* table of devices that work with this driver */
59 static struct usb_device_id cypress_table
[] = {
60 { USB_DEVICE(CYPRESS_VENDOR_ID
, CYPRESS_PRODUCT_ID
) },
63 MODULE_DEVICE_TABLE(usb
, cypress_table
);
65 /* structure to hold all of our device specific stuff */
67 struct usb_device
* udev
;
68 unsigned char port
[2];
71 /* used to send usb control messages to device */
72 static int vendor_command(struct cypress
*dev
, unsigned char request
,
73 unsigned char address
, unsigned char data
)
79 /* allocate some memory for the i/o buffer*/
80 iobuf
= kzalloc(CYPRESS_MAX_REQSIZE
, GFP_KERNEL
);
82 dev_err(&dev
->udev
->dev
, "Out of memory!\n");
87 dev_dbg(&dev
->udev
->dev
, "Sending usb_control_msg (data: %d)\n", data
);
89 /* prepare usb control message and send it upstream */
90 pipe
= usb_rcvctrlpipe(dev
->udev
, 0);
91 retval
= usb_control_msg(dev
->udev
, pipe
, request
,
92 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_OTHER
,
93 address
, data
, iobuf
, CYPRESS_MAX_REQSIZE
,
94 USB_CTRL_GET_TIMEOUT
);
96 /* store returned data (more READs to be added) */
98 case CYPRESS_READ_PORT
:
99 if (address
== CYPRESS_READ_PORT_ID0
) {
100 dev
->port
[0] = iobuf
[1];
101 dev_dbg(&dev
->udev
->dev
,
102 "READ_PORT0 returned: %d\n",
105 else if (address
== CYPRESS_READ_PORT_ID1
) {
106 dev
->port
[1] = iobuf
[1];
107 dev_dbg(&dev
->udev
->dev
,
108 "READ_PORT1 returned: %d\n",
119 /* write port value */
120 static ssize_t
write_port(struct device
*dev
, struct device_attribute
*attr
,
121 const char *buf
, size_t count
,
122 int port_num
, int write_id
)
127 struct usb_interface
*intf
= to_usb_interface(dev
);
128 struct cypress
*cyp
= usb_get_intfdata(intf
);
130 dev_dbg(&cyp
->udev
->dev
, "WRITE_PORT%d called\n", port_num
);
132 /* validate input data */
133 if (sscanf(buf
, "%d", &value
) < 1) {
137 if (value
< 0 || value
> 255) {
142 result
= vendor_command(cyp
, CYPRESS_WRITE_PORT
, write_id
,
143 (unsigned char)value
);
145 dev_dbg(&cyp
->udev
->dev
, "Result of vendor_command: %d\n\n", result
);
147 return result
< 0 ? result
: count
;
150 /* attribute callback handler (write) */
151 static ssize_t
set_port0_handler(struct device
*dev
,
152 struct device_attribute
*attr
,
153 const char *buf
, size_t count
)
155 return write_port(dev
, attr
, buf
, count
, 0, CYPRESS_WRITE_PORT_ID0
);
158 /* attribute callback handler (write) */
159 static ssize_t
set_port1_handler(struct device
*dev
,
160 struct device_attribute
*attr
,
161 const char *buf
, size_t count
)
163 return write_port(dev
, attr
, buf
, count
, 1, CYPRESS_WRITE_PORT_ID1
);
166 /* read port value */
167 static ssize_t
read_port(struct device
*dev
, struct device_attribute
*attr
,
168 char *buf
, int port_num
, int read_id
)
172 struct usb_interface
*intf
= to_usb_interface(dev
);
173 struct cypress
*cyp
= usb_get_intfdata(intf
);
175 dev_dbg(&cyp
->udev
->dev
, "READ_PORT%d called\n", port_num
);
177 result
= vendor_command(cyp
, CYPRESS_READ_PORT
, read_id
, 0);
179 dev_dbg(&cyp
->udev
->dev
, "Result of vendor_command: %d\n\n", result
);
181 return sprintf(buf
, "%d", cyp
->port
[port_num
]);
184 /* attribute callback handler (read) */
185 static ssize_t
get_port0_handler(struct device
*dev
,
186 struct device_attribute
*attr
, char *buf
)
188 return read_port(dev
, attr
, buf
, 0, CYPRESS_READ_PORT_ID0
);
191 /* attribute callback handler (read) */
192 static ssize_t
get_port1_handler(struct device
*dev
,
193 struct device_attribute
*attr
, char *buf
)
195 return read_port(dev
, attr
, buf
, 1, CYPRESS_READ_PORT_ID1
);
198 static DEVICE_ATTR(port0
, S_IWUGO
| S_IRUGO
,
199 get_port0_handler
, set_port0_handler
);
201 static DEVICE_ATTR(port1
, S_IWUGO
| S_IRUGO
,
202 get_port1_handler
, set_port1_handler
);
205 static int cypress_probe(struct usb_interface
*interface
,
206 const struct usb_device_id
*id
)
208 struct cypress
*dev
= NULL
;
209 int retval
= -ENOMEM
;
211 /* allocate memory for our device state and initialize it */
212 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
214 dev_err(&interface
->dev
, "Out of memory!\n");
218 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
220 /* save our data pointer in this interface device */
221 usb_set_intfdata(interface
, dev
);
223 /* create device attribute files */
224 retval
= device_create_file(&interface
->dev
, &dev_attr_port0
);
227 retval
= device_create_file(&interface
->dev
, &dev_attr_port1
);
231 /* let the user know that the device is now attached */
232 dev_info(&interface
->dev
,
233 "Cypress CY7C63xxx device now attached\n");
237 device_remove_file(&interface
->dev
, &dev_attr_port0
);
238 device_remove_file(&interface
->dev
, &dev_attr_port1
);
239 usb_set_intfdata(interface
, NULL
);
240 usb_put_dev(dev
->udev
);
247 static void cypress_disconnect(struct usb_interface
*interface
)
251 dev
= usb_get_intfdata(interface
);
253 /* remove device attribute files */
254 device_remove_file(&interface
->dev
, &dev_attr_port0
);
255 device_remove_file(&interface
->dev
, &dev_attr_port1
);
256 /* the intfdata can be set to NULL only after the
257 * device files have been removed */
258 usb_set_intfdata(interface
, NULL
);
260 usb_put_dev(dev
->udev
);
262 dev_info(&interface
->dev
,
263 "Cypress CY7C63xxx device now disconnected\n");
268 static struct usb_driver cypress_driver
= {
269 .name
= "cypress_cy7c63",
270 .probe
= cypress_probe
,
271 .disconnect
= cypress_disconnect
,
272 .id_table
= cypress_table
,
275 static int __init
cypress_init(void)
279 /* register this driver with the USB subsystem */
280 result
= usb_register(&cypress_driver
);
282 err("Function usb_register failed! Error number: %d\n", result
);
288 static void __exit
cypress_exit(void)
290 /* deregister this driver with the USB subsystem */
291 usb_deregister(&cypress_driver
);
294 module_init(cypress_init
);
295 module_exit(cypress_exit
);
297 MODULE_AUTHOR(DRIVER_AUTHOR
);
298 MODULE_DESCRIPTION(DRIVER_DESC
);
300 MODULE_LICENSE("GPL");