1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include <linux/module.h>
30#include <linux/kernel.h>
31#include <linux/slab.h>
32#include <linux/usb.h>
33
34#define DRIVER_AUTHOR "Oliver Bock (bock@tfh-berlin.de)"
35#define DRIVER_DESC "Cypress CY7C63xxx USB driver"
36
37#define CYPRESS_VENDOR_ID 0xa2c
38#define CYPRESS_PRODUCT_ID 0x8
39
40#define CYPRESS_READ_PORT 0x4
41#define CYPRESS_WRITE_PORT 0x5
42
43#define CYPRESS_READ_RAM 0x2
44#define CYPRESS_WRITE_RAM 0x3
45#define CYPRESS_READ_ROM 0x1
46
47#define CYPRESS_READ_PORT_ID0 0
48#define CYPRESS_WRITE_PORT_ID0 0
49#define CYPRESS_READ_PORT_ID1 0x2
50#define CYPRESS_WRITE_PORT_ID1 1
51
52#define CYPRESS_MAX_REQSIZE 8
53
54
55
56static const struct usb_device_id cypress_table[] = {
57 { USB_DEVICE(CYPRESS_VENDOR_ID, CYPRESS_PRODUCT_ID) },
58 { }
59};
60MODULE_DEVICE_TABLE(usb, cypress_table);
61
62
63struct cypress {
64 struct usb_device * udev;
65 unsigned char port[2];
66};
67
68
69static int vendor_command(struct cypress *dev, unsigned char request,
70 unsigned char address, unsigned char data)
71{
72 int retval = 0;
73 unsigned int pipe;
74 unsigned char *iobuf;
75
76
77 iobuf = kzalloc(CYPRESS_MAX_REQSIZE, GFP_KERNEL);
78 if (!iobuf) {
79 retval = -ENOMEM;
80 goto error;
81 }
82
83 dev_dbg(&dev->udev->dev, "Sending usb_control_msg (data: %d)\n", data);
84
85
86 pipe = usb_rcvctrlpipe(dev->udev, 0);
87 retval = usb_control_msg(dev->udev, pipe, request,
88 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
89 address, data, iobuf, CYPRESS_MAX_REQSIZE,
90 USB_CTRL_GET_TIMEOUT);
91
92
93 switch (request) {
94 case CYPRESS_READ_PORT:
95 if (address == CYPRESS_READ_PORT_ID0) {
96 dev->port[0] = iobuf[1];
97 dev_dbg(&dev->udev->dev,
98 "READ_PORT0 returned: %d\n",
99 dev->port[0]);
100 }
101 else if (address == CYPRESS_READ_PORT_ID1) {
102 dev->port[1] = iobuf[1];
103 dev_dbg(&dev->udev->dev,
104 "READ_PORT1 returned: %d\n",
105 dev->port[1]);
106 }
107 break;
108 }
109
110 kfree(iobuf);
111error:
112 return retval;
113}
114
115
116static ssize_t write_port(struct device *dev, struct device_attribute *attr,
117 const char *buf, size_t count,
118 int port_num, int write_id)
119{
120 int value = -1;
121 int result = 0;
122
123 struct usb_interface *intf = to_usb_interface(dev);
124 struct cypress *cyp = usb_get_intfdata(intf);
125
126 dev_dbg(&cyp->udev->dev, "WRITE_PORT%d called\n", port_num);
127
128
129 if (sscanf(buf, "%d", &value) < 1) {
130 result = -EINVAL;
131 goto error;
132 }
133 if (value < 0 || value > 255) {
134 result = -EINVAL;
135 goto error;
136 }
137
138 result = vendor_command(cyp, CYPRESS_WRITE_PORT, write_id,
139 (unsigned char)value);
140
141 dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result);
142error:
143 return result < 0 ? result : count;
144}
145
146
147static ssize_t port0_store(struct device *dev,
148 struct device_attribute *attr,
149 const char *buf, size_t count)
150{
151 return write_port(dev, attr, buf, count, 0, CYPRESS_WRITE_PORT_ID0);
152}
153
154
155static ssize_t port1_store(struct device *dev,
156 struct device_attribute *attr,
157 const char *buf, size_t count)
158{
159 return write_port(dev, attr, buf, count, 1, CYPRESS_WRITE_PORT_ID1);
160}
161
162
163static ssize_t read_port(struct device *dev, struct device_attribute *attr,
164 char *buf, int port_num, int read_id)
165{
166 int result = 0;
167
168 struct usb_interface *intf = to_usb_interface(dev);
169 struct cypress *cyp = usb_get_intfdata(intf);
170
171 dev_dbg(&cyp->udev->dev, "READ_PORT%d called\n", port_num);
172
173 result = vendor_command(cyp, CYPRESS_READ_PORT, read_id, 0);
174
175 dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result);
176
177 return sprintf(buf, "%d", cyp->port[port_num]);
178}
179
180
181static ssize_t port0_show(struct device *dev,
182 struct device_attribute *attr, char *buf)
183{
184 return read_port(dev, attr, buf, 0, CYPRESS_READ_PORT_ID0);
185}
186
187
188static ssize_t port1_show(struct device *dev,
189 struct device_attribute *attr, char *buf)
190{
191 return read_port(dev, attr, buf, 1, CYPRESS_READ_PORT_ID1);
192}
193
194static DEVICE_ATTR_RW(port0);
195
196static DEVICE_ATTR_RW(port1);
197
198
199static int cypress_probe(struct usb_interface *interface,
200 const struct usb_device_id *id)
201{
202 struct cypress *dev = NULL;
203 int retval = -ENOMEM;
204
205
206 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
207 if (!dev)
208 goto error_mem;
209
210 dev->udev = usb_get_dev(interface_to_usbdev(interface));
211
212
213 usb_set_intfdata(interface, dev);
214
215
216 retval = device_create_file(&interface->dev, &dev_attr_port0);
217 if (retval)
218 goto error;
219 retval = device_create_file(&interface->dev, &dev_attr_port1);
220 if (retval)
221 goto error;
222
223
224 dev_info(&interface->dev,
225 "Cypress CY7C63xxx device now attached\n");
226 return 0;
227
228error:
229 device_remove_file(&interface->dev, &dev_attr_port0);
230 device_remove_file(&interface->dev, &dev_attr_port1);
231 usb_set_intfdata(interface, NULL);
232 usb_put_dev(dev->udev);
233 kfree(dev);
234
235error_mem:
236 return retval;
237}
238
239static void cypress_disconnect(struct usb_interface *interface)
240{
241 struct cypress *dev;
242
243 dev = usb_get_intfdata(interface);
244
245
246 device_remove_file(&interface->dev, &dev_attr_port0);
247 device_remove_file(&interface->dev, &dev_attr_port1);
248
249
250 usb_set_intfdata(interface, NULL);
251
252 usb_put_dev(dev->udev);
253
254 dev_info(&interface->dev,
255 "Cypress CY7C63xxx device now disconnected\n");
256
257 kfree(dev);
258}
259
260static struct usb_driver cypress_driver = {
261 .name = "cypress_cy7c63",
262 .probe = cypress_probe,
263 .disconnect = cypress_disconnect,
264 .id_table = cypress_table,
265};
266
267module_usb_driver(cypress_driver);
268
269MODULE_AUTHOR(DRIVER_AUTHOR);
270MODULE_DESCRIPTION(DRIVER_DESC);
271
272MODULE_LICENSE("GPL");
273