xref: /openbmc/linux/drivers/usb/misc/cypress_cy7c63.c (revision 34d6f206a88c2651d216bd3487ac956a40b2ba8e)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
29189bfc2SOliver Bock /*
39189bfc2SOliver Bock * cypress_cy7c63.c
49189bfc2SOliver Bock *
5d09d6a35SOliver Bock * Copyright (c) 2006-2007 Oliver Bock (bock@tfh-berlin.de)
69189bfc2SOliver Bock *
79189bfc2SOliver Bock *	This driver is based on the Cypress USB Driver by Marcus Maul
89189bfc2SOliver Bock *	(cyport) and the 2.0 version of Greg Kroah-Hartman's
99189bfc2SOliver Bock *	USB Skeleton driver.
109189bfc2SOliver Bock *
119189bfc2SOliver Bock *	This is a generic driver for the Cypress CY7C63xxx family.
129189bfc2SOliver Bock *	For the time being it enables you to read from and write to
139189bfc2SOliver Bock *	the single I/O ports of the device.
149189bfc2SOliver Bock *
159189bfc2SOliver Bock *	Supported vendors:	AK Modul-Bus Computer GmbH
166ad576bbSOliver Bock *				(Firmware "Port-Chip")
176ad576bbSOliver Bock *
186ad576bbSOliver Bock *	Supported devices:	CY7C63001A-PC
196ad576bbSOliver Bock *				CY7C63001C-PXC
206ad576bbSOliver Bock *				CY7C63001C-SXC
216ad576bbSOliver Bock *
226ad576bbSOliver Bock *	Supported functions:	Read/Write Ports
239189bfc2SOliver Bock *
249189bfc2SOliver Bock *
25d09d6a35SOliver Bock *	For up-to-date information please visit:
26d09d6a35SOliver Bock *	http://www.obock.de/kernel/cypress
279189bfc2SOliver Bock */
289189bfc2SOliver Bock 
299189bfc2SOliver Bock #include <linux/module.h>
309189bfc2SOliver Bock #include <linux/kernel.h>
315a0e3ad6STejun Heo #include <linux/slab.h>
329189bfc2SOliver Bock #include <linux/usb.h>
339189bfc2SOliver Bock 
34d09d6a35SOliver Bock #define DRIVER_AUTHOR		"Oliver Bock (bock@tfh-berlin.de)"
359189bfc2SOliver Bock #define DRIVER_DESC		"Cypress CY7C63xxx USB driver"
369189bfc2SOliver Bock 
379189bfc2SOliver Bock #define CYPRESS_VENDOR_ID	0xa2c
389189bfc2SOliver Bock #define CYPRESS_PRODUCT_ID	0x8
399189bfc2SOliver Bock 
409189bfc2SOliver Bock #define CYPRESS_READ_PORT	0x4
419189bfc2SOliver Bock #define CYPRESS_WRITE_PORT	0x5
429189bfc2SOliver Bock 
439189bfc2SOliver Bock #define CYPRESS_READ_RAM	0x2
449189bfc2SOliver Bock #define CYPRESS_WRITE_RAM	0x3
459189bfc2SOliver Bock #define CYPRESS_READ_ROM	0x1
469189bfc2SOliver Bock 
479189bfc2SOliver Bock #define CYPRESS_READ_PORT_ID0	0
489189bfc2SOliver Bock #define CYPRESS_WRITE_PORT_ID0	0
499189bfc2SOliver Bock #define CYPRESS_READ_PORT_ID1	0x2
509189bfc2SOliver Bock #define CYPRESS_WRITE_PORT_ID1	1
519189bfc2SOliver Bock 
529189bfc2SOliver Bock #define CYPRESS_MAX_REQSIZE	8
539189bfc2SOliver Bock 
549189bfc2SOliver Bock 
559189bfc2SOliver Bock /* table of devices that work with this driver */
5633b9e162SNémeth Márton static const struct usb_device_id cypress_table[] = {
579189bfc2SOliver Bock 	{ USB_DEVICE(CYPRESS_VENDOR_ID, CYPRESS_PRODUCT_ID) },
589189bfc2SOliver Bock 	{ }
599189bfc2SOliver Bock };
609189bfc2SOliver Bock MODULE_DEVICE_TABLE(usb, cypress_table);
619189bfc2SOliver Bock 
629189bfc2SOliver Bock /* structure to hold all of our device specific stuff */
639189bfc2SOliver Bock struct cypress {
649189bfc2SOliver Bock 	struct usb_device *	udev;
659189bfc2SOliver Bock 	unsigned char		port[2];
669189bfc2SOliver Bock };
679189bfc2SOliver Bock 
689189bfc2SOliver Bock /* used to send usb control messages to device */
vendor_command(struct cypress * dev,unsigned char request,unsigned char address,unsigned char data)699189bfc2SOliver Bock static int vendor_command(struct cypress *dev, unsigned char request,
709189bfc2SOliver Bock 			  unsigned char address, unsigned char data)
719189bfc2SOliver Bock {
729189bfc2SOliver Bock 	int retval = 0;
739189bfc2SOliver Bock 	unsigned int pipe;
749189bfc2SOliver Bock 	unsigned char *iobuf;
759189bfc2SOliver Bock 
769189bfc2SOliver Bock 	/* allocate some memory for the i/o buffer*/
779189bfc2SOliver Bock 	iobuf = kzalloc(CYPRESS_MAX_REQSIZE, GFP_KERNEL);
789189bfc2SOliver Bock 	if (!iobuf) {
799189bfc2SOliver Bock 		retval = -ENOMEM;
809189bfc2SOliver Bock 		goto error;
819189bfc2SOliver Bock 	}
829189bfc2SOliver Bock 
839189bfc2SOliver Bock 	dev_dbg(&dev->udev->dev, "Sending usb_control_msg (data: %d)\n", data);
849189bfc2SOliver Bock 
859189bfc2SOliver Bock 	/* prepare usb control message and send it upstream */
869189bfc2SOliver Bock 	pipe = usb_rcvctrlpipe(dev->udev, 0);
879189bfc2SOliver Bock 	retval = usb_control_msg(dev->udev, pipe, request,
889189bfc2SOliver Bock 				 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
899189bfc2SOliver Bock 				 address, data, iobuf, CYPRESS_MAX_REQSIZE,
909189bfc2SOliver Bock 				 USB_CTRL_GET_TIMEOUT);
91*7bcd961dSOliver Neukum 	/* we must not process garbage */
92*7bcd961dSOliver Neukum 	if (retval < 2)
93*7bcd961dSOliver Neukum 		goto err_buf;
949189bfc2SOliver Bock 
959189bfc2SOliver Bock 	/* store returned data (more READs to be added) */
969189bfc2SOliver Bock 	switch (request) {
979189bfc2SOliver Bock 		case CYPRESS_READ_PORT:
989189bfc2SOliver Bock 			if (address == CYPRESS_READ_PORT_ID0) {
999189bfc2SOliver Bock 				dev->port[0] = iobuf[1];
1009189bfc2SOliver Bock 				dev_dbg(&dev->udev->dev,
1019189bfc2SOliver Bock 					"READ_PORT0 returned: %d\n",
1029189bfc2SOliver Bock 					dev->port[0]);
1039189bfc2SOliver Bock 			}
1049189bfc2SOliver Bock 			else if (address == CYPRESS_READ_PORT_ID1) {
1059189bfc2SOliver Bock 				dev->port[1] = iobuf[1];
1069189bfc2SOliver Bock 				dev_dbg(&dev->udev->dev,
1079189bfc2SOliver Bock 					"READ_PORT1 returned: %d\n",
1089189bfc2SOliver Bock 					dev->port[1]);
1099189bfc2SOliver Bock 			}
1109189bfc2SOliver Bock 			break;
1119189bfc2SOliver Bock 	}
1129189bfc2SOliver Bock 
113*7bcd961dSOliver Neukum err_buf:
1149189bfc2SOliver Bock 	kfree(iobuf);
1159189bfc2SOliver Bock error:
1169189bfc2SOliver Bock 	return retval;
1179189bfc2SOliver Bock }
1189189bfc2SOliver Bock 
1199189bfc2SOliver Bock /* write port value */
write_port(struct device * dev,struct device_attribute * attr,const char * buf,size_t count,int port_num,int write_id)1209189bfc2SOliver Bock static ssize_t write_port(struct device *dev, struct device_attribute *attr,
1219189bfc2SOliver Bock 			  const char *buf, size_t count,
1229189bfc2SOliver Bock 			  int port_num, int write_id)
1239189bfc2SOliver Bock {
1249189bfc2SOliver Bock 	int value = -1;
1259189bfc2SOliver Bock 	int result = 0;
1269189bfc2SOliver Bock 
1279189bfc2SOliver Bock 	struct usb_interface *intf = to_usb_interface(dev);
1289189bfc2SOliver Bock 	struct cypress *cyp = usb_get_intfdata(intf);
1299189bfc2SOliver Bock 
1309189bfc2SOliver Bock 	dev_dbg(&cyp->udev->dev, "WRITE_PORT%d called\n", port_num);
1319189bfc2SOliver Bock 
1329189bfc2SOliver Bock 	/* validate input data */
1339189bfc2SOliver Bock 	if (sscanf(buf, "%d", &value) < 1) {
1349189bfc2SOliver Bock 		result = -EINVAL;
1359189bfc2SOliver Bock 		goto error;
1369189bfc2SOliver Bock 	}
1379189bfc2SOliver Bock 	if (value < 0 || value > 255) {
1389189bfc2SOliver Bock 		result = -EINVAL;
1399189bfc2SOliver Bock 		goto error;
1409189bfc2SOliver Bock 	}
1419189bfc2SOliver Bock 
1429189bfc2SOliver Bock 	result = vendor_command(cyp, CYPRESS_WRITE_PORT, write_id,
1439189bfc2SOliver Bock 				(unsigned char)value);
1449189bfc2SOliver Bock 
1459189bfc2SOliver Bock 	dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result);
1469189bfc2SOliver Bock error:
1479189bfc2SOliver Bock 	return result < 0 ? result : count;
1489189bfc2SOliver Bock }
1499189bfc2SOliver Bock 
1509189bfc2SOliver Bock /* attribute callback handler (write) */
port0_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)151ed5bd7a4SGreg Kroah-Hartman static ssize_t port0_store(struct device *dev,
1529189bfc2SOliver Bock 				 struct device_attribute *attr,
1539189bfc2SOliver Bock 				 const char *buf, size_t count)
1549189bfc2SOliver Bock {
1559189bfc2SOliver Bock 	return write_port(dev, attr, buf, count, 0, CYPRESS_WRITE_PORT_ID0);
1569189bfc2SOliver Bock }
1579189bfc2SOliver Bock 
1589189bfc2SOliver Bock /* attribute callback handler (write) */
port1_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)159ed5bd7a4SGreg Kroah-Hartman static ssize_t port1_store(struct device *dev,
1609189bfc2SOliver Bock 				 struct device_attribute *attr,
1619189bfc2SOliver Bock 				 const char *buf, size_t count)
1629189bfc2SOliver Bock {
1639189bfc2SOliver Bock 	return write_port(dev, attr, buf, count, 1, CYPRESS_WRITE_PORT_ID1);
1649189bfc2SOliver Bock }
1659189bfc2SOliver Bock 
1669189bfc2SOliver Bock /* read port value */
read_port(struct device * dev,struct device_attribute * attr,char * buf,int port_num,int read_id)1679189bfc2SOliver Bock static ssize_t read_port(struct device *dev, struct device_attribute *attr,
1689189bfc2SOliver Bock 			 char *buf, int port_num, int read_id)
1699189bfc2SOliver Bock {
1709189bfc2SOliver Bock 	int result = 0;
1719189bfc2SOliver Bock 
1729189bfc2SOliver Bock 	struct usb_interface *intf = to_usb_interface(dev);
1739189bfc2SOliver Bock 	struct cypress *cyp = usb_get_intfdata(intf);
1749189bfc2SOliver Bock 
1759189bfc2SOliver Bock 	dev_dbg(&cyp->udev->dev, "READ_PORT%d called\n", port_num);
1769189bfc2SOliver Bock 
1779189bfc2SOliver Bock 	result = vendor_command(cyp, CYPRESS_READ_PORT, read_id, 0);
1789189bfc2SOliver Bock 
1799189bfc2SOliver Bock 	dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result);
1809189bfc2SOliver Bock 
1819189bfc2SOliver Bock 	return sprintf(buf, "%d", cyp->port[port_num]);
1829189bfc2SOliver Bock }
1839189bfc2SOliver Bock 
1849189bfc2SOliver Bock /* attribute callback handler (read) */
port0_show(struct device * dev,struct device_attribute * attr,char * buf)185ed5bd7a4SGreg Kroah-Hartman static ssize_t port0_show(struct device *dev,
1869189bfc2SOliver Bock 				 struct device_attribute *attr, char *buf)
1879189bfc2SOliver Bock {
1889189bfc2SOliver Bock 	return read_port(dev, attr, buf, 0, CYPRESS_READ_PORT_ID0);
1899189bfc2SOliver Bock }
190761ef1e4SGreg Kroah-Hartman static DEVICE_ATTR_RW(port0);
1919189bfc2SOliver Bock 
1929189bfc2SOliver Bock /* attribute callback handler (read) */
port1_show(struct device * dev,struct device_attribute * attr,char * buf)193ed5bd7a4SGreg Kroah-Hartman static ssize_t port1_show(struct device *dev,
1949189bfc2SOliver Bock 				 struct device_attribute *attr, char *buf)
1959189bfc2SOliver Bock {
1969189bfc2SOliver Bock 	return read_port(dev, attr, buf, 1, CYPRESS_READ_PORT_ID1);
1979189bfc2SOliver Bock }
198ed5bd7a4SGreg Kroah-Hartman static DEVICE_ATTR_RW(port1);
1999189bfc2SOliver Bock 
200761ef1e4SGreg Kroah-Hartman static struct attribute *cypress_attrs[] = {
201761ef1e4SGreg Kroah-Hartman 	&dev_attr_port0.attr,
202761ef1e4SGreg Kroah-Hartman 	&dev_attr_port1.attr,
203761ef1e4SGreg Kroah-Hartman 	NULL,
204761ef1e4SGreg Kroah-Hartman };
205761ef1e4SGreg Kroah-Hartman ATTRIBUTE_GROUPS(cypress);
2069189bfc2SOliver Bock 
cypress_probe(struct usb_interface * interface,const struct usb_device_id * id)2079189bfc2SOliver Bock static int cypress_probe(struct usb_interface *interface,
2089189bfc2SOliver Bock 			 const struct usb_device_id *id)
2099189bfc2SOliver Bock {
210f87ba66aSRuan Jinjie 	struct cypress *dev;
2119189bfc2SOliver Bock 	int retval = -ENOMEM;
2129189bfc2SOliver Bock 
2139189bfc2SOliver Bock 	/* allocate memory for our device state and initialize it */
2149189bfc2SOliver Bock 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
215e83c06e9SWolfram Sang 	if (!dev)
2164d42e1bbSGreg Kroah-Hartman 		goto error_mem;
2179189bfc2SOliver Bock 
2189189bfc2SOliver Bock 	dev->udev = usb_get_dev(interface_to_usbdev(interface));
2199189bfc2SOliver Bock 
2209189bfc2SOliver Bock 	/* save our data pointer in this interface device */
2219189bfc2SOliver Bock 	usb_set_intfdata(interface, dev);
2229189bfc2SOliver Bock 
2239189bfc2SOliver Bock 	/* let the user know that the device is now attached */
2249189bfc2SOliver Bock 	dev_info(&interface->dev,
2259189bfc2SOliver Bock 		 "Cypress CY7C63xxx device now attached\n");
2264d42e1bbSGreg Kroah-Hartman 	return 0;
2279189bfc2SOliver Bock 
2284d42e1bbSGreg Kroah-Hartman error_mem:
2299189bfc2SOliver Bock 	return retval;
2309189bfc2SOliver Bock }
2319189bfc2SOliver Bock 
cypress_disconnect(struct usb_interface * interface)2329189bfc2SOliver Bock static void cypress_disconnect(struct usb_interface *interface)
2339189bfc2SOliver Bock {
2349189bfc2SOliver Bock 	struct cypress *dev;
2359189bfc2SOliver Bock 
2369189bfc2SOliver Bock 	dev = usb_get_intfdata(interface);
2379189bfc2SOliver Bock 
238949ce471SOliver Neukum 	/* the intfdata can be set to NULL only after the
239949ce471SOliver Neukum 	 * device files have been removed */
240949ce471SOliver Neukum 	usb_set_intfdata(interface, NULL);
2419189bfc2SOliver Bock 
2429189bfc2SOliver Bock 	usb_put_dev(dev->udev);
2439189bfc2SOliver Bock 
2449189bfc2SOliver Bock 	dev_info(&interface->dev,
2459189bfc2SOliver Bock 		 "Cypress CY7C63xxx device now disconnected\n");
2469189bfc2SOliver Bock 
2479189bfc2SOliver Bock 	kfree(dev);
2489189bfc2SOliver Bock }
2499189bfc2SOliver Bock 
2509189bfc2SOliver Bock static struct usb_driver cypress_driver = {
2519189bfc2SOliver Bock 	.name = "cypress_cy7c63",
2529189bfc2SOliver Bock 	.probe = cypress_probe,
2539189bfc2SOliver Bock 	.disconnect = cypress_disconnect,
2549189bfc2SOliver Bock 	.id_table = cypress_table,
255761ef1e4SGreg Kroah-Hartman 	.dev_groups = cypress_groups,
2569189bfc2SOliver Bock };
2579189bfc2SOliver Bock 
25865db4305SGreg Kroah-Hartman module_usb_driver(cypress_driver);
2599189bfc2SOliver Bock 
2609189bfc2SOliver Bock MODULE_AUTHOR(DRIVER_AUTHOR);
2619189bfc2SOliver Bock MODULE_DESCRIPTION(DRIVER_DESC);
2629189bfc2SOliver Bock 
2639189bfc2SOliver Bock MODULE_LICENSE("GPL");
264