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/slab.h>
36 #include <linux/usb.h>
38 #define DRIVER_AUTHOR "Oliver Bock (bock@tfh-berlin.de)"
39 #define DRIVER_DESC "Cypress CY7C63xxx USB driver"
41 #define CYPRESS_VENDOR_ID 0xa2c
42 #define CYPRESS_PRODUCT_ID 0x8
44 #define CYPRESS_READ_PORT 0x4
45 #define CYPRESS_WRITE_PORT 0x5
47 #define CYPRESS_READ_RAM 0x2
48 #define CYPRESS_WRITE_RAM 0x3
49 #define CYPRESS_READ_ROM 0x1
51 #define CYPRESS_READ_PORT_ID0 0
52 #define CYPRESS_WRITE_PORT_ID0 0
53 #define CYPRESS_READ_PORT_ID1 0x2
54 #define CYPRESS_WRITE_PORT_ID1 1
56 #define CYPRESS_MAX_REQSIZE 8
59 /* table of devices that work with this driver */
60 static const struct usb_device_id cypress_table
[] = {
61 { USB_DEVICE(CYPRESS_VENDOR_ID
, CYPRESS_PRODUCT_ID
) },
64 MODULE_DEVICE_TABLE(usb
, cypress_table
);
66 /* structure to hold all of our device specific stuff */
68 struct usb_device
* udev
;
69 unsigned char port
[2];
72 /* used to send usb control messages to device */
73 static int vendor_command(struct cypress
*dev
, unsigned char request
,
74 unsigned char address
, unsigned char data
)
80 /* allocate some memory for the i/o buffer*/
81 iobuf
= kzalloc(CYPRESS_MAX_REQSIZE
, GFP_KERNEL
);
83 dev_err(&dev
->udev
->dev
, "Out of memory!\n");
88 dev_dbg(&dev
->udev
->dev
, "Sending usb_control_msg (data: %d)\n", data
);
90 /* prepare usb control message and send it upstream */
91 pipe
= usb_rcvctrlpipe(dev
->udev
, 0);
92 retval
= usb_control_msg(dev
->udev
, pipe
, request
,
93 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_OTHER
,
94 address
, data
, iobuf
, CYPRESS_MAX_REQSIZE
,
95 USB_CTRL_GET_TIMEOUT
);
97 /* store returned data (more READs to be added) */
99 case CYPRESS_READ_PORT
:
100 if (address
== CYPRESS_READ_PORT_ID0
) {
101 dev
->port
[0] = iobuf
[1];
102 dev_dbg(&dev
->udev
->dev
,
103 "READ_PORT0 returned: %d\n",
106 else if (address
== CYPRESS_READ_PORT_ID1
) {
107 dev
->port
[1] = iobuf
[1];
108 dev_dbg(&dev
->udev
->dev
,
109 "READ_PORT1 returned: %d\n",
120 /* write port value */
121 static ssize_t
write_port(struct device
*dev
, struct device_attribute
*attr
,
122 const char *buf
, size_t count
,
123 int port_num
, int write_id
)
128 struct usb_interface
*intf
= to_usb_interface(dev
);
129 struct cypress
*cyp
= usb_get_intfdata(intf
);
131 dev_dbg(&cyp
->udev
->dev
, "WRITE_PORT%d called\n", port_num
);
133 /* validate input data */
134 if (sscanf(buf
, "%d", &value
) < 1) {
138 if (value
< 0 || value
> 255) {
143 result
= vendor_command(cyp
, CYPRESS_WRITE_PORT
, write_id
,
144 (unsigned char)value
);
146 dev_dbg(&cyp
->udev
->dev
, "Result of vendor_command: %d\n\n", result
);
148 return result
< 0 ? result
: count
;
151 /* attribute callback handler (write) */
152 static ssize_t
set_port0_handler(struct device
*dev
,
153 struct device_attribute
*attr
,
154 const char *buf
, size_t count
)
156 return write_port(dev
, attr
, buf
, count
, 0, CYPRESS_WRITE_PORT_ID0
);
159 /* attribute callback handler (write) */
160 static ssize_t
set_port1_handler(struct device
*dev
,
161 struct device_attribute
*attr
,
162 const char *buf
, size_t count
)
164 return write_port(dev
, attr
, buf
, count
, 1, CYPRESS_WRITE_PORT_ID1
);
167 /* read port value */
168 static ssize_t
read_port(struct device
*dev
, struct device_attribute
*attr
,
169 char *buf
, int port_num
, int read_id
)
173 struct usb_interface
*intf
= to_usb_interface(dev
);
174 struct cypress
*cyp
= usb_get_intfdata(intf
);
176 dev_dbg(&cyp
->udev
->dev
, "READ_PORT%d called\n", port_num
);
178 result
= vendor_command(cyp
, CYPRESS_READ_PORT
, read_id
, 0);
180 dev_dbg(&cyp
->udev
->dev
, "Result of vendor_command: %d\n\n", result
);
182 return sprintf(buf
, "%d", cyp
->port
[port_num
]);
185 /* attribute callback handler (read) */
186 static ssize_t
get_port0_handler(struct device
*dev
,
187 struct device_attribute
*attr
, char *buf
)
189 return read_port(dev
, attr
, buf
, 0, CYPRESS_READ_PORT_ID0
);
192 /* attribute callback handler (read) */
193 static ssize_t
get_port1_handler(struct device
*dev
,
194 struct device_attribute
*attr
, char *buf
)
196 return read_port(dev
, attr
, buf
, 1, CYPRESS_READ_PORT_ID1
);
199 static DEVICE_ATTR(port0
, S_IWUGO
| S_IRUGO
,
200 get_port0_handler
, set_port0_handler
);
202 static DEVICE_ATTR(port1
, S_IWUGO
| S_IRUGO
,
203 get_port1_handler
, set_port1_handler
);
206 static int cypress_probe(struct usb_interface
*interface
,
207 const struct usb_device_id
*id
)
209 struct cypress
*dev
= NULL
;
210 int retval
= -ENOMEM
;
212 /* allocate memory for our device state and initialize it */
213 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
215 dev_err(&interface
->dev
, "Out of memory!\n");
219 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
221 /* save our data pointer in this interface device */
222 usb_set_intfdata(interface
, dev
);
224 /* create device attribute files */
225 retval
= device_create_file(&interface
->dev
, &dev_attr_port0
);
228 retval
= device_create_file(&interface
->dev
, &dev_attr_port1
);
232 /* let the user know that the device is now attached */
233 dev_info(&interface
->dev
,
234 "Cypress CY7C63xxx device now attached\n");
238 device_remove_file(&interface
->dev
, &dev_attr_port0
);
239 device_remove_file(&interface
->dev
, &dev_attr_port1
);
240 usb_set_intfdata(interface
, NULL
);
241 usb_put_dev(dev
->udev
);
248 static void cypress_disconnect(struct usb_interface
*interface
)
252 dev
= usb_get_intfdata(interface
);
254 /* remove device attribute files */
255 device_remove_file(&interface
->dev
, &dev_attr_port0
);
256 device_remove_file(&interface
->dev
, &dev_attr_port1
);
257 /* the intfdata can be set to NULL only after the
258 * device files have been removed */
259 usb_set_intfdata(interface
, NULL
);
261 usb_put_dev(dev
->udev
);
263 dev_info(&interface
->dev
,
264 "Cypress CY7C63xxx device now disconnected\n");
269 static struct usb_driver cypress_driver
= {
270 .name
= "cypress_cy7c63",
271 .probe
= cypress_probe
,
272 .disconnect
= cypress_disconnect
,
273 .id_table
= cypress_table
,
276 static int __init
cypress_init(void)
280 /* register this driver with the USB subsystem */
281 result
= usb_register(&cypress_driver
);
283 printk(KERN_ERR KBUILD_MODNAME
": usb_register failed! "
284 "Error number: %d\n", result
);
289 static void __exit
cypress_exit(void)
291 /* deregister this driver with the USB subsystem */
292 usb_deregister(&cypress_driver
);
295 module_init(cypress_init
);
296 module_exit(cypress_exit
);
298 MODULE_AUTHOR(DRIVER_AUTHOR
);
299 MODULE_DESCRIPTION(DRIVER_DESC
);
301 MODULE_LICENSE("GPL");