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