blob: 402b94dd25315959dec45d1196c05af227bc32f5 [file] [log] [blame]
Oliver Bock9189bfc2006-06-22 19:04:47 +02001/*
2* cypress_cy7c63.c
3*
Oliver Bockd09d6a32007-11-12 00:42:16 +01004* Copyright (c) 2006-2007 Oliver Bock (bock@tfh-berlin.de)
Oliver Bock9189bfc2006-06-22 19:04:47 +02005*
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
Oliver Bock6ad576b2006-07-27 21:34:58 +020015* (Firmware "Port-Chip")
16*
17* Supported devices: CY7C63001A-PC
18* CY7C63001C-PXC
19* CY7C63001C-SXC
20*
21* Supported functions: Read/Write Ports
Oliver Bock9189bfc2006-06-22 19:04:47 +020022*
23*
Oliver Bockd09d6a32007-11-12 00:42:16 +010024* For up-to-date information please visit:
25* http://www.obock.de/kernel/cypress
26*
Oliver Bock9189bfc2006-06-22 19:04:47 +020027* 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
Oliver Bock9189bfc2006-06-22 19:04:47 +020032#include <linux/module.h>
33#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Oliver Bock9189bfc2006-06-22 19:04:47 +020035#include <linux/usb.h>
36
Oliver Bockd09d6a32007-11-12 00:42:16 +010037#define DRIVER_AUTHOR "Oliver Bock (bock@tfh-berlin.de)"
Oliver Bock9189bfc2006-06-22 19:04:47 +020038#define DRIVER_DESC "Cypress CY7C63xxx USB driver"
39
40#define CYPRESS_VENDOR_ID 0xa2c
41#define CYPRESS_PRODUCT_ID 0x8
42
43#define CYPRESS_READ_PORT 0x4
44#define CYPRESS_WRITE_PORT 0x5
45
46#define CYPRESS_READ_RAM 0x2
47#define CYPRESS_WRITE_RAM 0x3
48#define CYPRESS_READ_ROM 0x1
49
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
54
55#define CYPRESS_MAX_REQSIZE 8
56
57
58/* table of devices that work with this driver */
Németh Márton33b9e162010-01-10 15:34:45 +010059static const struct usb_device_id cypress_table[] = {
Oliver Bock9189bfc2006-06-22 19:04:47 +020060 { USB_DEVICE(CYPRESS_VENDOR_ID, CYPRESS_PRODUCT_ID) },
61 { }
62};
63MODULE_DEVICE_TABLE(usb, cypress_table);
64
65/* structure to hold all of our device specific stuff */
66struct cypress {
67 struct usb_device * udev;
68 unsigned char port[2];
69};
70
71/* used to send usb control messages to device */
72static int vendor_command(struct cypress *dev, unsigned char request,
73 unsigned char address, unsigned char data)
74{
75 int retval = 0;
76 unsigned int pipe;
77 unsigned char *iobuf;
78
79 /* allocate some memory for the i/o buffer*/
80 iobuf = kzalloc(CYPRESS_MAX_REQSIZE, GFP_KERNEL);
81 if (!iobuf) {
82 dev_err(&dev->udev->dev, "Out of memory!\n");
83 retval = -ENOMEM;
84 goto error;
85 }
86
87 dev_dbg(&dev->udev->dev, "Sending usb_control_msg (data: %d)\n", data);
88
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);
95
96 /* store returned data (more READs to be added) */
97 switch (request) {
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",
103 dev->port[0]);
104 }
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",
109 dev->port[1]);
110 }
111 break;
112 }
113
114 kfree(iobuf);
115error:
116 return retval;
117}
118
119/* write port value */
120static 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)
123{
124 int value = -1;
125 int result = 0;
126
127 struct usb_interface *intf = to_usb_interface(dev);
128 struct cypress *cyp = usb_get_intfdata(intf);
129
130 dev_dbg(&cyp->udev->dev, "WRITE_PORT%d called\n", port_num);
131
132 /* validate input data */
133 if (sscanf(buf, "%d", &value) < 1) {
134 result = -EINVAL;
135 goto error;
136 }
137 if (value < 0 || value > 255) {
138 result = -EINVAL;
139 goto error;
140 }
141
142 result = vendor_command(cyp, CYPRESS_WRITE_PORT, write_id,
143 (unsigned char)value);
144
145 dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result);
146error:
147 return result < 0 ? result : count;
148}
149
150/* attribute callback handler (write) */
151static ssize_t set_port0_handler(struct device *dev,
152 struct device_attribute *attr,
153 const char *buf, size_t count)
154{
155 return write_port(dev, attr, buf, count, 0, CYPRESS_WRITE_PORT_ID0);
156}
157
158/* attribute callback handler (write) */
159static ssize_t set_port1_handler(struct device *dev,
160 struct device_attribute *attr,
161 const char *buf, size_t count)
162{
163 return write_port(dev, attr, buf, count, 1, CYPRESS_WRITE_PORT_ID1);
164}
165
166/* read port value */
167static ssize_t read_port(struct device *dev, struct device_attribute *attr,
168 char *buf, int port_num, int read_id)
169{
170 int result = 0;
171
172 struct usb_interface *intf = to_usb_interface(dev);
173 struct cypress *cyp = usb_get_intfdata(intf);
174
175 dev_dbg(&cyp->udev->dev, "READ_PORT%d called\n", port_num);
176
177 result = vendor_command(cyp, CYPRESS_READ_PORT, read_id, 0);
178
179 dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result);
180
181 return sprintf(buf, "%d", cyp->port[port_num]);
182}
183
184/* attribute callback handler (read) */
185static ssize_t get_port0_handler(struct device *dev,
186 struct device_attribute *attr, char *buf)
187{
188 return read_port(dev, attr, buf, 0, CYPRESS_READ_PORT_ID0);
189}
190
191/* attribute callback handler (read) */
192static ssize_t get_port1_handler(struct device *dev,
193 struct device_attribute *attr, char *buf)
194{
195 return read_port(dev, attr, buf, 1, CYPRESS_READ_PORT_ID1);
196}
197
Greg Kroah-Hartmanc9906002010-11-15 11:32:38 -0800198static DEVICE_ATTR(port0, S_IRUGO | S_IWUSR, get_port0_handler, set_port0_handler);
Oliver Bock9189bfc2006-06-22 19:04:47 +0200199
Greg Kroah-Hartmanc9906002010-11-15 11:32:38 -0800200static DEVICE_ATTR(port1, S_IRUGO | S_IWUSR, get_port1_handler, set_port1_handler);
Oliver Bock9189bfc2006-06-22 19:04:47 +0200201
202
203static int cypress_probe(struct usb_interface *interface,
204 const struct usb_device_id *id)
205{
206 struct cypress *dev = NULL;
207 int retval = -ENOMEM;
208
209 /* allocate memory for our device state and initialize it */
210 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
211 if (dev == NULL) {
Greg Kroah-Hartmanb93b58e2006-08-14 17:32:51 -0700212 dev_err(&interface->dev, "Out of memory!\n");
Greg Kroah-Hartman4d42e1b2006-08-28 11:43:25 -0700213 goto error_mem;
Oliver Bock9189bfc2006-06-22 19:04:47 +0200214 }
215
216 dev->udev = usb_get_dev(interface_to_usbdev(interface));
217
218 /* save our data pointer in this interface device */
219 usb_set_intfdata(interface, dev);
220
221 /* create device attribute files */
Greg Kroah-Hartman4d42e1b2006-08-28 11:43:25 -0700222 retval = device_create_file(&interface->dev, &dev_attr_port0);
223 if (retval)
224 goto error;
225 retval = device_create_file(&interface->dev, &dev_attr_port1);
226 if (retval)
227 goto error;
Oliver Bock9189bfc2006-06-22 19:04:47 +0200228
229 /* let the user know that the device is now attached */
230 dev_info(&interface->dev,
231 "Cypress CY7C63xxx device now attached\n");
Greg Kroah-Hartman4d42e1b2006-08-28 11:43:25 -0700232 return 0;
Oliver Bock9189bfc2006-06-22 19:04:47 +0200233
Oliver Bock9189bfc2006-06-22 19:04:47 +0200234error:
Greg Kroah-Hartman4d42e1b2006-08-28 11:43:25 -0700235 device_remove_file(&interface->dev, &dev_attr_port0);
236 device_remove_file(&interface->dev, &dev_attr_port1);
237 usb_set_intfdata(interface, NULL);
238 usb_put_dev(dev->udev);
239 kfree(dev);
240
241error_mem:
Oliver Bock9189bfc2006-06-22 19:04:47 +0200242 return retval;
243}
244
245static void cypress_disconnect(struct usb_interface *interface)
246{
247 struct cypress *dev;
248
249 dev = usb_get_intfdata(interface);
Oliver Bock9189bfc2006-06-22 19:04:47 +0200250
251 /* remove device attribute files */
252 device_remove_file(&interface->dev, &dev_attr_port0);
253 device_remove_file(&interface->dev, &dev_attr_port1);
Oliver Neukum949ce472007-03-30 10:52:16 +0200254 /* the intfdata can be set to NULL only after the
255 * device files have been removed */
256 usb_set_intfdata(interface, NULL);
Oliver Bock9189bfc2006-06-22 19:04:47 +0200257
258 usb_put_dev(dev->udev);
259
260 dev_info(&interface->dev,
261 "Cypress CY7C63xxx device now disconnected\n");
262
263 kfree(dev);
264}
265
266static struct usb_driver cypress_driver = {
267 .name = "cypress_cy7c63",
268 .probe = cypress_probe,
269 .disconnect = cypress_disconnect,
270 .id_table = cypress_table,
271};
272
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800273module_usb_driver(cypress_driver);
Oliver Bock9189bfc2006-06-22 19:04:47 +0200274
275MODULE_AUTHOR(DRIVER_AUTHOR);
276MODULE_DESCRIPTION(DRIVER_DESC);
277
278MODULE_LICENSE("GPL");