Pull ec into test branch
[linux-2.6] / drivers / usb / misc / cypress_cy7c63.c
1 /*
2 * cypress_cy7c63.c
3 *
4 * Copyright (c) 2006 Oliver Bock (o.bock@fh-wolfenbuettel.de)
5 *
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
15 *                               (Firmware "Port-Chip")
16 *
17 *       Supported devices:      CY7C63001A-PC
18 *                               CY7C63001C-PXC
19 *                               CY7C63001C-SXC
20 *
21 *       Supported functions:    Read/Write Ports
22 *
23 *
24 *       This program is free software; you can redistribute it and/or
25 *       modify it under the terms of the GNU General Public License as
26 *       published by the Free Software Foundation, version 2.
27 */
28
29 #include <linux/init.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/usb.h>
33
34 #define DRIVER_AUTHOR           "Oliver Bock (o.bock@fh-wolfenbuettel.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 /* table of devices that work with this driver */
56 static struct usb_device_id cypress_table [] = {
57         { USB_DEVICE(CYPRESS_VENDOR_ID, CYPRESS_PRODUCT_ID) },
58         { }
59 };
60 MODULE_DEVICE_TABLE(usb, cypress_table);
61
62 /* structure to hold all of our device specific stuff */
63 struct cypress {
64         struct usb_device *     udev;
65         unsigned char           port[2];
66 };
67
68 /* used to send usb control messages to device */
69 static 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         /* allocate some memory for the i/o buffer*/
77         iobuf = kzalloc(CYPRESS_MAX_REQSIZE, GFP_KERNEL);
78         if (!iobuf) {
79                 dev_err(&dev->udev->dev, "Out of memory!\n");
80                 retval = -ENOMEM;
81                 goto error;
82         }
83
84         dev_dbg(&dev->udev->dev, "Sending usb_control_msg (data: %d)\n", data);
85
86         /* prepare usb control message and send it upstream */
87         pipe = usb_rcvctrlpipe(dev->udev, 0);
88         retval = usb_control_msg(dev->udev, pipe, request,
89                                  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
90                                  address, data, iobuf, CYPRESS_MAX_REQSIZE,
91                                  USB_CTRL_GET_TIMEOUT);
92
93         /* store returned data (more READs to be added) */
94         switch (request) {
95                 case CYPRESS_READ_PORT:
96                         if (address == CYPRESS_READ_PORT_ID0) {
97                                 dev->port[0] = iobuf[1];
98                                 dev_dbg(&dev->udev->dev,
99                                         "READ_PORT0 returned: %d\n",
100                                         dev->port[0]);
101                         }
102                         else if (address == CYPRESS_READ_PORT_ID1) {
103                                 dev->port[1] = iobuf[1];
104                                 dev_dbg(&dev->udev->dev,
105                                         "READ_PORT1 returned: %d\n",
106                                         dev->port[1]);
107                         }
108                         break;
109         }
110
111         kfree(iobuf);
112 error:
113         return retval;
114 }
115
116 /* write port value */
117 static ssize_t write_port(struct device *dev, struct device_attribute *attr,
118                           const char *buf, size_t count,
119                           int port_num, int write_id)
120 {
121         int value = -1;
122         int result = 0;
123
124         struct usb_interface *intf = to_usb_interface(dev);
125         struct cypress *cyp = usb_get_intfdata(intf);
126
127         dev_dbg(&cyp->udev->dev, "WRITE_PORT%d called\n", port_num);
128
129         /* validate input data */
130         if (sscanf(buf, "%d", &value) < 1) {
131                 result = -EINVAL;
132                 goto error;
133         }
134         if (value < 0 || value > 255) {
135                 result = -EINVAL;
136                 goto error;
137         }
138
139         result = vendor_command(cyp, CYPRESS_WRITE_PORT, write_id,
140                                 (unsigned char)value);
141
142         dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result);
143 error:
144         return result < 0 ? result : count;
145 }
146
147 /* attribute callback handler (write) */
148 static ssize_t set_port0_handler(struct device *dev,
149                                  struct device_attribute *attr,
150                                  const char *buf, size_t count)
151 {
152         return write_port(dev, attr, buf, count, 0, CYPRESS_WRITE_PORT_ID0);
153 }
154
155 /* attribute callback handler (write) */
156 static ssize_t set_port1_handler(struct device *dev,
157                                  struct device_attribute *attr,
158                                  const char *buf, size_t count)
159 {
160         return write_port(dev, attr, buf, count, 1, CYPRESS_WRITE_PORT_ID1);
161 }
162
163 /* read port value */
164 static ssize_t read_port(struct device *dev, struct device_attribute *attr,
165                          char *buf, int port_num, int read_id)
166 {
167         int result = 0;
168
169         struct usb_interface *intf = to_usb_interface(dev);
170         struct cypress *cyp = usb_get_intfdata(intf);
171
172         dev_dbg(&cyp->udev->dev, "READ_PORT%d called\n", port_num);
173
174         result = vendor_command(cyp, CYPRESS_READ_PORT, read_id, 0);
175
176         dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result);
177
178         return sprintf(buf, "%d", cyp->port[port_num]);
179 }
180
181 /* attribute callback handler (read) */
182 static ssize_t get_port0_handler(struct device *dev,
183                                  struct device_attribute *attr, char *buf)
184 {
185         return read_port(dev, attr, buf, 0, CYPRESS_READ_PORT_ID0);
186 }
187
188 /* attribute callback handler (read) */
189 static ssize_t get_port1_handler(struct device *dev,
190                                  struct device_attribute *attr, char *buf)
191 {
192         return read_port(dev, attr, buf, 1, CYPRESS_READ_PORT_ID1);
193 }
194
195 static DEVICE_ATTR(port0, S_IWUGO | S_IRUGO,
196                    get_port0_handler, set_port0_handler);
197
198 static DEVICE_ATTR(port1, S_IWUGO | S_IRUGO,
199                    get_port1_handler, set_port1_handler);
200
201
202 static int cypress_probe(struct usb_interface *interface,
203                          const struct usb_device_id *id)
204 {
205         struct cypress *dev = NULL;
206         int retval = -ENOMEM;
207
208         /* allocate memory for our device state and initialize it */
209         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
210         if (dev == NULL) {
211                 dev_err(&interface->dev, "Out of memory!\n");
212                 goto error_mem;
213         }
214
215         dev->udev = usb_get_dev(interface_to_usbdev(interface));
216
217         /* save our data pointer in this interface device */
218         usb_set_intfdata(interface, dev);
219
220         /* create device attribute files */
221         retval = device_create_file(&interface->dev, &dev_attr_port0);
222         if (retval)
223                 goto error;
224         retval = device_create_file(&interface->dev, &dev_attr_port1);
225         if (retval)
226                 goto error;
227
228         /* let the user know that the device is now attached */
229         dev_info(&interface->dev,
230                  "Cypress CY7C63xxx device now attached\n");
231         return 0;
232
233 error:
234         device_remove_file(&interface->dev, &dev_attr_port0);
235         device_remove_file(&interface->dev, &dev_attr_port1);
236         usb_set_intfdata(interface, NULL);
237         usb_put_dev(dev->udev);
238         kfree(dev);
239
240 error_mem:
241         return retval;
242 }
243
244 static void cypress_disconnect(struct usb_interface *interface)
245 {
246         struct cypress *dev;
247
248         dev = usb_get_intfdata(interface);
249         usb_set_intfdata(interface, NULL);
250
251         /* remove device attribute files */
252         device_remove_file(&interface->dev, &dev_attr_port0);
253         device_remove_file(&interface->dev, &dev_attr_port1);
254
255         usb_put_dev(dev->udev);
256
257         dev_info(&interface->dev,
258                  "Cypress CY7C63xxx device now disconnected\n");
259
260         kfree(dev);
261 }
262
263 static struct usb_driver cypress_driver = {
264         .name = "cypress_cy7c63",
265         .probe = cypress_probe,
266         .disconnect = cypress_disconnect,
267         .id_table = cypress_table,
268 };
269
270 static int __init cypress_init(void)
271 {
272         int result;
273
274         /* register this driver with the USB subsystem */
275         result = usb_register(&cypress_driver);
276         if (result) {
277                 err("Function usb_register failed! Error number: %d\n", result);
278         }
279
280         return result;
281 }
282
283 static void __exit cypress_exit(void)
284 {
285         /* deregister this driver with the USB subsystem */
286         usb_deregister(&cypress_driver);
287 }
288
289 module_init(cypress_init);
290 module_exit(cypress_exit);
291
292 MODULE_AUTHOR(DRIVER_AUTHOR);
293 MODULE_DESCRIPTION(DRIVER_DESC);
294
295 MODULE_LICENSE("GPL");