xref: /linux/drivers/usb/misc/cypress_cy7c63.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
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 */
56 static const 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 		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 	/* we must not process garbage */
92 	if (retval < 2)
93 		goto err_buf;
94 
95 	/* store returned data (more READs to be added) */
96 	switch (request) {
97 		case CYPRESS_READ_PORT:
98 			if (address == CYPRESS_READ_PORT_ID0) {
99 				dev->port[0] = iobuf[1];
100 				dev_dbg(&dev->udev->dev,
101 					"READ_PORT0 returned: %d\n",
102 					dev->port[0]);
103 			}
104 			else if (address == CYPRESS_READ_PORT_ID1) {
105 				dev->port[1] = iobuf[1];
106 				dev_dbg(&dev->udev->dev,
107 					"READ_PORT1 returned: %d\n",
108 					dev->port[1]);
109 			}
110 			break;
111 	}
112 
113 err_buf:
114 	kfree(iobuf);
115 error:
116 	return retval;
117 }
118 
119 /* write port value */
120 static 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);
146 error:
147 	return result < 0 ? result : count;
148 }
149 
150 /* attribute callback handler (write) */
151 static ssize_t port0_store(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) */
159 static ssize_t port1_store(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 */
167 static 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) */
185 static ssize_t port0_show(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 static DEVICE_ATTR_RW(port0);
191 
192 /* attribute callback handler (read) */
193 static ssize_t port1_show(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 static DEVICE_ATTR_RW(port1);
199 
200 static struct attribute *cypress_attrs[] = {
201 	&dev_attr_port0.attr,
202 	&dev_attr_port1.attr,
203 	NULL,
204 };
205 ATTRIBUTE_GROUPS(cypress);
206 
207 static int cypress_probe(struct usb_interface *interface,
208 			 const struct usb_device_id *id)
209 {
210 	struct cypress *dev;
211 	int retval = -ENOMEM;
212 
213 	/* allocate memory for our device state and initialize it */
214 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
215 	if (!dev)
216 		goto error_mem;
217 
218 	dev->udev = usb_get_dev(interface_to_usbdev(interface));
219 
220 	/* save our data pointer in this interface device */
221 	usb_set_intfdata(interface, dev);
222 
223 	/* let the user know that the device is now attached */
224 	dev_info(&interface->dev,
225 		 "Cypress CY7C63xxx device now attached\n");
226 	return 0;
227 
228 error_mem:
229 	return retval;
230 }
231 
232 static void cypress_disconnect(struct usb_interface *interface)
233 {
234 	struct cypress *dev;
235 
236 	dev = usb_get_intfdata(interface);
237 
238 	/* the intfdata can be set to NULL only after the
239 	 * device files have been removed */
240 	usb_set_intfdata(interface, NULL);
241 
242 	usb_put_dev(dev->udev);
243 
244 	dev_info(&interface->dev,
245 		 "Cypress CY7C63xxx device now disconnected\n");
246 
247 	kfree(dev);
248 }
249 
250 static struct usb_driver cypress_driver = {
251 	.name = "cypress_cy7c63",
252 	.probe = cypress_probe,
253 	.disconnect = cypress_disconnect,
254 	.id_table = cypress_table,
255 	.dev_groups = cypress_groups,
256 };
257 
258 module_usb_driver(cypress_driver);
259 
260 MODULE_AUTHOR(DRIVER_AUTHOR);
261 MODULE_DESCRIPTION(DRIVER_DESC);
262 
263 MODULE_LICENSE("GPL");
264