Oliver Bock | 9189bfc | 2006-06-22 19:04:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * cypress_cy7c63.c |
| 3 | * |
Oliver Bock | d09d6a3 | 2007-11-12 00:42:16 +0100 | [diff] [blame] | 4 | * Copyright (c) 2006-2007 Oliver Bock (bock@tfh-berlin.de) |
Oliver Bock | 9189bfc | 2006-06-22 19:04:47 +0200 | [diff] [blame] | 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 |
Oliver Bock | 6ad576b | 2006-07-27 21:34:58 +0200 | [diff] [blame] | 15 | * (Firmware "Port-Chip") |
| 16 | * |
| 17 | * Supported devices: CY7C63001A-PC |
| 18 | * CY7C63001C-PXC |
| 19 | * CY7C63001C-SXC |
| 20 | * |
| 21 | * Supported functions: Read/Write Ports |
Oliver Bock | 9189bfc | 2006-06-22 19:04:47 +0200 | [diff] [blame] | 22 | * |
| 23 | * |
Oliver Bock | d09d6a3 | 2007-11-12 00:42:16 +0100 | [diff] [blame] | 24 | * For up-to-date information please visit: |
| 25 | * http://www.obock.de/kernel/cypress |
| 26 | * |
Oliver Bock | 9189bfc | 2006-06-22 19:04:47 +0200 | [diff] [blame] | 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 | |
Oliver Bock | 9189bfc | 2006-06-22 19:04:47 +0200 | [diff] [blame] | 32 | #include <linux/module.h> |
| 33 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Oliver Bock | 9189bfc | 2006-06-22 19:04:47 +0200 | [diff] [blame] | 35 | #include <linux/usb.h> |
| 36 | |
Oliver Bock | d09d6a3 | 2007-11-12 00:42:16 +0100 | [diff] [blame] | 37 | #define DRIVER_AUTHOR "Oliver Bock (bock@tfh-berlin.de)" |
Oliver Bock | 9189bfc | 2006-06-22 19:04:47 +0200 | [diff] [blame] | 38 | #define DRIVER_DESC "Cypress CY7C63xxx USB driver" |
| 39 | |
| 40 | #define CYPRESS_VENDOR_ID 0xa2c |
| 41 | #define CYPRESS_PRODUCT_ID 0x8 |
| 42 | |
| 43 | #define CYPRESS_READ_PORT 0x4 |
| 44 | #define CYPRESS_WRITE_PORT 0x5 |
| 45 | |
| 46 | #define CYPRESS_READ_RAM 0x2 |
| 47 | #define CYPRESS_WRITE_RAM 0x3 |
| 48 | #define CYPRESS_READ_ROM 0x1 |
| 49 | |
| 50 | #define CYPRESS_READ_PORT_ID0 0 |
| 51 | #define CYPRESS_WRITE_PORT_ID0 0 |
| 52 | #define CYPRESS_READ_PORT_ID1 0x2 |
| 53 | #define CYPRESS_WRITE_PORT_ID1 1 |
| 54 | |
| 55 | #define CYPRESS_MAX_REQSIZE 8 |
| 56 | |
| 57 | |
| 58 | /* table of devices that work with this driver */ |
Németh Márton | 33b9e16 | 2010-01-10 15:34:45 +0100 | [diff] [blame] | 59 | static const struct usb_device_id cypress_table[] = { |
Oliver Bock | 9189bfc | 2006-06-22 19:04:47 +0200 | [diff] [blame] | 60 | { USB_DEVICE(CYPRESS_VENDOR_ID, CYPRESS_PRODUCT_ID) }, |
| 61 | { } |
| 62 | }; |
| 63 | MODULE_DEVICE_TABLE(usb, cypress_table); |
| 64 | |
| 65 | /* structure to hold all of our device specific stuff */ |
| 66 | struct cypress { |
| 67 | struct usb_device * udev; |
| 68 | unsigned char port[2]; |
| 69 | }; |
| 70 | |
| 71 | /* used to send usb control messages to device */ |
| 72 | static int vendor_command(struct cypress *dev, unsigned char request, |
| 73 | unsigned char address, unsigned char data) |
| 74 | { |
| 75 | int retval = 0; |
| 76 | unsigned int pipe; |
| 77 | unsigned char *iobuf; |
| 78 | |
| 79 | /* allocate some memory for the i/o buffer*/ |
| 80 | iobuf = kzalloc(CYPRESS_MAX_REQSIZE, GFP_KERNEL); |
| 81 | if (!iobuf) { |
Oliver Bock | 9189bfc | 2006-06-22 19:04:47 +0200 | [diff] [blame] | 82 | retval = -ENOMEM; |
| 83 | goto error; |
| 84 | } |
| 85 | |
| 86 | dev_dbg(&dev->udev->dev, "Sending usb_control_msg (data: %d)\n", data); |
| 87 | |
| 88 | /* prepare usb control message and send it upstream */ |
| 89 | pipe = usb_rcvctrlpipe(dev->udev, 0); |
| 90 | retval = usb_control_msg(dev->udev, pipe, request, |
| 91 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER, |
| 92 | address, data, iobuf, CYPRESS_MAX_REQSIZE, |
| 93 | USB_CTRL_GET_TIMEOUT); |
| 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 | kfree(iobuf); |
| 114 | error: |
| 115 | return retval; |
| 116 | } |
| 117 | |
| 118 | /* write port value */ |
| 119 | static ssize_t write_port(struct device *dev, struct device_attribute *attr, |
| 120 | const char *buf, size_t count, |
| 121 | int port_num, int write_id) |
| 122 | { |
| 123 | int value = -1; |
| 124 | int result = 0; |
| 125 | |
| 126 | struct usb_interface *intf = to_usb_interface(dev); |
| 127 | struct cypress *cyp = usb_get_intfdata(intf); |
| 128 | |
| 129 | dev_dbg(&cyp->udev->dev, "WRITE_PORT%d called\n", port_num); |
| 130 | |
| 131 | /* validate input data */ |
| 132 | if (sscanf(buf, "%d", &value) < 1) { |
| 133 | result = -EINVAL; |
| 134 | goto error; |
| 135 | } |
| 136 | if (value < 0 || value > 255) { |
| 137 | result = -EINVAL; |
| 138 | goto error; |
| 139 | } |
| 140 | |
| 141 | result = vendor_command(cyp, CYPRESS_WRITE_PORT, write_id, |
| 142 | (unsigned char)value); |
| 143 | |
| 144 | dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result); |
| 145 | error: |
| 146 | return result < 0 ? result : count; |
| 147 | } |
| 148 | |
| 149 | /* attribute callback handler (write) */ |
| 150 | static ssize_t set_port0_handler(struct device *dev, |
| 151 | struct device_attribute *attr, |
| 152 | const char *buf, size_t count) |
| 153 | { |
| 154 | return write_port(dev, attr, buf, count, 0, CYPRESS_WRITE_PORT_ID0); |
| 155 | } |
| 156 | |
| 157 | /* attribute callback handler (write) */ |
| 158 | static ssize_t set_port1_handler(struct device *dev, |
| 159 | struct device_attribute *attr, |
| 160 | const char *buf, size_t count) |
| 161 | { |
| 162 | return write_port(dev, attr, buf, count, 1, CYPRESS_WRITE_PORT_ID1); |
| 163 | } |
| 164 | |
| 165 | /* read port value */ |
| 166 | static ssize_t read_port(struct device *dev, struct device_attribute *attr, |
| 167 | char *buf, int port_num, int read_id) |
| 168 | { |
| 169 | int result = 0; |
| 170 | |
| 171 | struct usb_interface *intf = to_usb_interface(dev); |
| 172 | struct cypress *cyp = usb_get_intfdata(intf); |
| 173 | |
| 174 | dev_dbg(&cyp->udev->dev, "READ_PORT%d called\n", port_num); |
| 175 | |
| 176 | result = vendor_command(cyp, CYPRESS_READ_PORT, read_id, 0); |
| 177 | |
| 178 | dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result); |
| 179 | |
| 180 | return sprintf(buf, "%d", cyp->port[port_num]); |
| 181 | } |
| 182 | |
| 183 | /* attribute callback handler (read) */ |
| 184 | static ssize_t get_port0_handler(struct device *dev, |
| 185 | struct device_attribute *attr, char *buf) |
| 186 | { |
| 187 | return read_port(dev, attr, buf, 0, CYPRESS_READ_PORT_ID0); |
| 188 | } |
| 189 | |
| 190 | /* attribute callback handler (read) */ |
| 191 | static ssize_t get_port1_handler(struct device *dev, |
| 192 | struct device_attribute *attr, char *buf) |
| 193 | { |
| 194 | return read_port(dev, attr, buf, 1, CYPRESS_READ_PORT_ID1); |
| 195 | } |
| 196 | |
Greg Kroah-Hartman | c990600 | 2010-11-15 11:32:38 -0800 | [diff] [blame] | 197 | static DEVICE_ATTR(port0, S_IRUGO | S_IWUSR, get_port0_handler, set_port0_handler); |
Oliver Bock | 9189bfc | 2006-06-22 19:04:47 +0200 | [diff] [blame] | 198 | |
Greg Kroah-Hartman | c990600 | 2010-11-15 11:32:38 -0800 | [diff] [blame] | 199 | static DEVICE_ATTR(port1, S_IRUGO | S_IWUSR, get_port1_handler, set_port1_handler); |
Oliver Bock | 9189bfc | 2006-06-22 19:04:47 +0200 | [diff] [blame] | 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); |
Wolfram Sang | e83c06e | 2016-08-25 19:39:13 +0200 | [diff] [blame] | 210 | if (!dev) |
Greg Kroah-Hartman | 4d42e1b | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 211 | goto error_mem; |
Oliver Bock | 9189bfc | 2006-06-22 19:04:47 +0200 | [diff] [blame] | 212 | |
| 213 | dev->udev = usb_get_dev(interface_to_usbdev(interface)); |
| 214 | |
| 215 | /* save our data pointer in this interface device */ |
| 216 | usb_set_intfdata(interface, dev); |
| 217 | |
| 218 | /* create device attribute files */ |
Greg Kroah-Hartman | 4d42e1b | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 219 | retval = device_create_file(&interface->dev, &dev_attr_port0); |
| 220 | if (retval) |
| 221 | goto error; |
| 222 | retval = device_create_file(&interface->dev, &dev_attr_port1); |
| 223 | if (retval) |
| 224 | goto error; |
Oliver Bock | 9189bfc | 2006-06-22 19:04:47 +0200 | [diff] [blame] | 225 | |
| 226 | /* let the user know that the device is now attached */ |
| 227 | dev_info(&interface->dev, |
| 228 | "Cypress CY7C63xxx device now attached\n"); |
Greg Kroah-Hartman | 4d42e1b | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 229 | return 0; |
Oliver Bock | 9189bfc | 2006-06-22 19:04:47 +0200 | [diff] [blame] | 230 | |
Oliver Bock | 9189bfc | 2006-06-22 19:04:47 +0200 | [diff] [blame] | 231 | error: |
Greg Kroah-Hartman | 4d42e1b | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 232 | device_remove_file(&interface->dev, &dev_attr_port0); |
| 233 | device_remove_file(&interface->dev, &dev_attr_port1); |
| 234 | usb_set_intfdata(interface, NULL); |
| 235 | usb_put_dev(dev->udev); |
| 236 | kfree(dev); |
| 237 | |
| 238 | error_mem: |
Oliver Bock | 9189bfc | 2006-06-22 19:04:47 +0200 | [diff] [blame] | 239 | return retval; |
| 240 | } |
| 241 | |
| 242 | static void cypress_disconnect(struct usb_interface *interface) |
| 243 | { |
| 244 | struct cypress *dev; |
| 245 | |
| 246 | dev = usb_get_intfdata(interface); |
Oliver Bock | 9189bfc | 2006-06-22 19:04:47 +0200 | [diff] [blame] | 247 | |
| 248 | /* remove device attribute files */ |
| 249 | device_remove_file(&interface->dev, &dev_attr_port0); |
| 250 | device_remove_file(&interface->dev, &dev_attr_port1); |
Oliver Neukum | 949ce47 | 2007-03-30 10:52:16 +0200 | [diff] [blame] | 251 | /* the intfdata can be set to NULL only after the |
| 252 | * device files have been removed */ |
| 253 | usb_set_intfdata(interface, NULL); |
Oliver Bock | 9189bfc | 2006-06-22 19:04:47 +0200 | [diff] [blame] | 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 | |
Greg Kroah-Hartman | 65db430 | 2011-11-18 09:34:02 -0800 | [diff] [blame] | 270 | module_usb_driver(cypress_driver); |
Oliver Bock | 9189bfc | 2006-06-22 19:04:47 +0200 | [diff] [blame] | 271 | |
| 272 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 273 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 274 | |
| 275 | MODULE_LICENSE("GPL"); |