Loading...
1/*
2* cypress_cy7c63.c
3*
4* Copyright (c) 2006-2007 Oliver Bock (bock@tfh-berlin.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* For up-to-date information please visit:
25* http://www.obock.de/kernel/cypress
26*
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.
30*/
31
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>
37
38#define DRIVER_AUTHOR "Oliver Bock (bock@tfh-berlin.de)"
39#define DRIVER_DESC "Cypress CY7C63xxx USB driver"
40
41#define CYPRESS_VENDOR_ID 0xa2c
42#define CYPRESS_PRODUCT_ID 0x8
43
44#define CYPRESS_READ_PORT 0x4
45#define CYPRESS_WRITE_PORT 0x5
46
47#define CYPRESS_READ_RAM 0x2
48#define CYPRESS_WRITE_RAM 0x3
49#define CYPRESS_READ_ROM 0x1
50
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
55
56#define CYPRESS_MAX_REQSIZE 8
57
58
59/* table of devices that work with this driver */
60static const struct usb_device_id cypress_table[] = {
61 { USB_DEVICE(CYPRESS_VENDOR_ID, CYPRESS_PRODUCT_ID) },
62 { }
63};
64MODULE_DEVICE_TABLE(usb, cypress_table);
65
66/* structure to hold all of our device specific stuff */
67struct cypress {
68 struct usb_device * udev;
69 unsigned char port[2];
70};
71
72/* used to send usb control messages to device */
73static int vendor_command(struct cypress *dev, unsigned char request,
74 unsigned char address, unsigned char data)
75{
76 int retval = 0;
77 unsigned int pipe;
78 unsigned char *iobuf;
79
80 /* allocate some memory for the i/o buffer*/
81 iobuf = kzalloc(CYPRESS_MAX_REQSIZE, GFP_KERNEL);
82 if (!iobuf) {
83 dev_err(&dev->udev->dev, "Out of memory!\n");
84 retval = -ENOMEM;
85 goto error;
86 }
87
88 dev_dbg(&dev->udev->dev, "Sending usb_control_msg (data: %d)\n", data);
89
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);
96
97 /* store returned data (more READs to be added) */
98 switch (request) {
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",
104 dev->port[0]);
105 }
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",
110 dev->port[1]);
111 }
112 break;
113 }
114
115 kfree(iobuf);
116error:
117 return retval;
118}
119
120/* write port value */
121static 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)
124{
125 int value = -1;
126 int result = 0;
127
128 struct usb_interface *intf = to_usb_interface(dev);
129 struct cypress *cyp = usb_get_intfdata(intf);
130
131 dev_dbg(&cyp->udev->dev, "WRITE_PORT%d called\n", port_num);
132
133 /* validate input data */
134 if (sscanf(buf, "%d", &value) < 1) {
135 result = -EINVAL;
136 goto error;
137 }
138 if (value < 0 || value > 255) {
139 result = -EINVAL;
140 goto error;
141 }
142
143 result = vendor_command(cyp, CYPRESS_WRITE_PORT, write_id,
144 (unsigned char)value);
145
146 dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result);
147error:
148 return result < 0 ? result : count;
149}
150
151/* attribute callback handler (write) */
152static ssize_t set_port0_handler(struct device *dev,
153 struct device_attribute *attr,
154 const char *buf, size_t count)
155{
156 return write_port(dev, attr, buf, count, 0, CYPRESS_WRITE_PORT_ID0);
157}
158
159/* attribute callback handler (write) */
160static ssize_t set_port1_handler(struct device *dev,
161 struct device_attribute *attr,
162 const char *buf, size_t count)
163{
164 return write_port(dev, attr, buf, count, 1, CYPRESS_WRITE_PORT_ID1);
165}
166
167/* read port value */
168static ssize_t read_port(struct device *dev, struct device_attribute *attr,
169 char *buf, int port_num, int read_id)
170{
171 int result = 0;
172
173 struct usb_interface *intf = to_usb_interface(dev);
174 struct cypress *cyp = usb_get_intfdata(intf);
175
176 dev_dbg(&cyp->udev->dev, "READ_PORT%d called\n", port_num);
177
178 result = vendor_command(cyp, CYPRESS_READ_PORT, read_id, 0);
179
180 dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result);
181
182 return sprintf(buf, "%d", cyp->port[port_num]);
183}
184
185/* attribute callback handler (read) */
186static ssize_t get_port0_handler(struct device *dev,
187 struct device_attribute *attr, char *buf)
188{
189 return read_port(dev, attr, buf, 0, CYPRESS_READ_PORT_ID0);
190}
191
192/* attribute callback handler (read) */
193static ssize_t get_port1_handler(struct device *dev,
194 struct device_attribute *attr, char *buf)
195{
196 return read_port(dev, attr, buf, 1, CYPRESS_READ_PORT_ID1);
197}
198
199static DEVICE_ATTR(port0, S_IRUGO | S_IWUSR, get_port0_handler, set_port0_handler);
200
201static DEVICE_ATTR(port1, S_IRUGO | S_IWUSR, get_port1_handler, set_port1_handler);
202
203
204static int cypress_probe(struct usb_interface *interface,
205 const struct usb_device_id *id)
206{
207 struct cypress *dev = NULL;
208 int retval = -ENOMEM;
209
210 /* allocate memory for our device state and initialize it */
211 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
212 if (dev == NULL) {
213 dev_err(&interface->dev, "Out of memory!\n");
214 goto error_mem;
215 }
216
217 dev->udev = usb_get_dev(interface_to_usbdev(interface));
218
219 /* save our data pointer in this interface device */
220 usb_set_intfdata(interface, dev);
221
222 /* create device attribute files */
223 retval = device_create_file(&interface->dev, &dev_attr_port0);
224 if (retval)
225 goto error;
226 retval = device_create_file(&interface->dev, &dev_attr_port1);
227 if (retval)
228 goto error;
229
230 /* let the user know that the device is now attached */
231 dev_info(&interface->dev,
232 "Cypress CY7C63xxx device now attached\n");
233 return 0;
234
235error:
236 device_remove_file(&interface->dev, &dev_attr_port0);
237 device_remove_file(&interface->dev, &dev_attr_port1);
238 usb_set_intfdata(interface, NULL);
239 usb_put_dev(dev->udev);
240 kfree(dev);
241
242error_mem:
243 return retval;
244}
245
246static void cypress_disconnect(struct usb_interface *interface)
247{
248 struct cypress *dev;
249
250 dev = usb_get_intfdata(interface);
251
252 /* remove device attribute files */
253 device_remove_file(&interface->dev, &dev_attr_port0);
254 device_remove_file(&interface->dev, &dev_attr_port1);
255 /* the intfdata can be set to NULL only after the
256 * device files have been removed */
257 usb_set_intfdata(interface, NULL);
258
259 usb_put_dev(dev->udev);
260
261 dev_info(&interface->dev,
262 "Cypress CY7C63xxx device now disconnected\n");
263
264 kfree(dev);
265}
266
267static struct usb_driver cypress_driver = {
268 .name = "cypress_cy7c63",
269 .probe = cypress_probe,
270 .disconnect = cypress_disconnect,
271 .id_table = cypress_table,
272};
273
274static int __init cypress_init(void)
275{
276 int result;
277
278 /* register this driver with the USB subsystem */
279 result = usb_register(&cypress_driver);
280 if (result)
281 printk(KERN_ERR KBUILD_MODNAME ": usb_register failed! "
282 "Error number: %d\n", result);
283
284 return result;
285}
286
287static void __exit cypress_exit(void)
288{
289 /* deregister this driver with the USB subsystem */
290 usb_deregister(&cypress_driver);
291}
292
293module_init(cypress_init);
294module_exit(cypress_exit);
295
296MODULE_AUTHOR(DRIVER_AUTHOR);
297MODULE_DESCRIPTION(DRIVER_DESC);
298
299MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0
2/*
3* cypress_cy7c63.c
4*
5* Copyright (c) 2006-2007 Oliver Bock (bock@tfh-berlin.de)
6*
7* This driver is based on the Cypress USB Driver by Marcus Maul
8* (cyport) and the 2.0 version of Greg Kroah-Hartman's
9* USB Skeleton driver.
10*
11* This is a generic driver for the Cypress CY7C63xxx family.
12* For the time being it enables you to read from and write to
13* the single I/O ports of the device.
14*
15* Supported vendors: AK Modul-Bus Computer GmbH
16* (Firmware "Port-Chip")
17*
18* Supported devices: CY7C63001A-PC
19* CY7C63001C-PXC
20* CY7C63001C-SXC
21*
22* Supported functions: Read/Write Ports
23*
24*
25* For up-to-date information please visit:
26* http://www.obock.de/kernel/cypress
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/* table of devices that work with this driver */
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/* structure to hold all of our device specific stuff */
63struct cypress {
64 struct usb_device * udev;
65 unsigned char port[2];
66};
67
68/* used to send usb control messages to device */
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 /* allocate some memory for the i/o buffer*/
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 /* prepare usb control message and send it upstream */
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 /* store returned data (more READs to be added) */
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/* write port value */
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 /* validate input data */
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/* attribute callback handler (write) */
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/* attribute callback handler (write) */
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/* read port value */
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/* attribute callback handler (read) */
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}
186static DEVICE_ATTR_RW(port0);
187
188/* attribute callback handler (read) */
189static ssize_t port1_show(struct device *dev,
190 struct device_attribute *attr, char *buf)
191{
192 return read_port(dev, attr, buf, 1, CYPRESS_READ_PORT_ID1);
193}
194static DEVICE_ATTR_RW(port1);
195
196static struct attribute *cypress_attrs[] = {
197 &dev_attr_port0.attr,
198 &dev_attr_port1.attr,
199 NULL,
200};
201ATTRIBUTE_GROUPS(cypress);
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)
212 goto error_mem;
213
214 dev->udev = usb_get_dev(interface_to_usbdev(interface));
215
216 /* save our data pointer in this interface device */
217 usb_set_intfdata(interface, dev);
218
219 /* let the user know that the device is now attached */
220 dev_info(&interface->dev,
221 "Cypress CY7C63xxx device now attached\n");
222 return 0;
223
224error_mem:
225 return retval;
226}
227
228static void cypress_disconnect(struct usb_interface *interface)
229{
230 struct cypress *dev;
231
232 dev = usb_get_intfdata(interface);
233
234 /* the intfdata can be set to NULL only after the
235 * device files have been removed */
236 usb_set_intfdata(interface, NULL);
237
238 usb_put_dev(dev->udev);
239
240 dev_info(&interface->dev,
241 "Cypress CY7C63xxx device now disconnected\n");
242
243 kfree(dev);
244}
245
246static struct usb_driver cypress_driver = {
247 .name = "cypress_cy7c63",
248 .probe = cypress_probe,
249 .disconnect = cypress_disconnect,
250 .id_table = cypress_table,
251 .dev_groups = cypress_groups,
252};
253
254module_usb_driver(cypress_driver);
255
256MODULE_AUTHOR(DRIVER_AUTHOR);
257MODULE_DESCRIPTION(DRIVER_DESC);
258
259MODULE_LICENSE("GPL");