Oliver Bock | 4255e6f | 2006-06-10 00:42:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * cy7c63.c |
| 3 | * |
| 4 | * Copyright (c) 2006 Oliver Bock (bock@fh-wolfenbuettel.de) |
| 5 | * |
| 6 | * This driver is based on the Cypress Thermometer USB Driver by |
| 7 | * Marcus Maul and the 2.0 version of Greg Kroah-Hartman's |
| 8 | * USB Skeleton driver. |
| 9 | * |
| 10 | * Is is a generic driver for the Cypress CY7C63000 family. |
| 11 | * For the time being it enables you to toggle the single I/O ports |
| 12 | * of the device. |
| 13 | * |
| 14 | * Supported vendors: AK Modul-Bus Computer GmbH |
| 15 | * Supported devices: CY7C63001A-PC (to be continued...) |
| 16 | * Supported functions: Read/Write Ports (to be continued...) |
| 17 | * |
| 18 | * Chipsets families: CY7C63000, CY7C63001, CY7C63100, CY7C63101 |
| 19 | * |
| 20 | * |
| 21 | * This program is free software; you can redistribute it and/or |
| 22 | * modify it under the terms of the GNU General Public License as |
| 23 | * published by the Free Software Foundation, version 2. |
| 24 | */ |
| 25 | |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/usb.h> |
| 30 | |
| 31 | #define DRIVER_AUTHOR "Oliver Bock (bock@fh-wolfenbuettel.de)" |
| 32 | #define DRIVER_DESC "Cypress CY7C63xxx USB driver" |
| 33 | |
| 34 | #define CY7C63_VENDOR_ID 0xa2c |
| 35 | #define CY7C63_PRODUCT_ID 0x8 |
| 36 | |
| 37 | #define CY7C63_READ_PORT 0x4 |
| 38 | #define CY7C63_WRITE_PORT 0x5 |
| 39 | #define CY7C63_READ_RAM 0x2 |
| 40 | #define CY7C63_WRITE_RAM 0x3 |
| 41 | #define CY7C63_READ_ROM 0x1 |
| 42 | |
| 43 | #define CY7C63_READ_PORT_ID0 0 |
| 44 | #define CY7C63_WRITE_PORT_ID0 0 |
| 45 | #define CY7C63_READ_PORT_ID1 0x2 |
| 46 | #define CY7C63_WRITE_PORT_ID1 1 |
| 47 | |
| 48 | #define CY7C63_MAX_REQSIZE 8 |
| 49 | |
| 50 | |
| 51 | /* table of devices that work with this driver */ |
| 52 | static struct usb_device_id cy7c63_table [] = { |
| 53 | { USB_DEVICE(CY7C63_VENDOR_ID, CY7C63_PRODUCT_ID) }, |
| 54 | { } |
| 55 | }; |
| 56 | MODULE_DEVICE_TABLE(usb, cy7c63_table); |
| 57 | |
| 58 | /* structure to hold all of our device specific stuff */ |
| 59 | struct cy7c63 { |
| 60 | struct usb_device * udev; |
| 61 | char port0; |
| 62 | char port1; |
| 63 | }; |
| 64 | |
| 65 | /* used to send usb control messages to device */ |
| 66 | int vendor_command(struct cy7c63 *dev, unsigned char request, |
| 67 | unsigned char address, unsigned char data) { |
| 68 | |
| 69 | int retval = 0; |
| 70 | unsigned int pipe; |
| 71 | unsigned char *iobuf; |
| 72 | |
| 73 | /* allocate some memory for the i/o buffer*/ |
| 74 | iobuf = kzalloc(CY7C63_MAX_REQSIZE, GFP_KERNEL); |
| 75 | if (!iobuf) { |
| 76 | dev_err(&dev->udev->dev, "Out of memory!\n"); |
| 77 | retval = -ENOMEM; |
| 78 | goto error; |
| 79 | } |
| 80 | |
| 81 | dev_dbg(&dev->udev->dev, "Sending usb_control_msg (data: %d)\n", data); |
| 82 | |
| 83 | /* prepare usb control message and send it upstream */ |
| 84 | pipe = usb_rcvctrlpipe(dev->udev, 0); |
| 85 | retval = usb_control_msg(dev->udev, pipe, request, |
| 86 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER, |
| 87 | address, data, iobuf, CY7C63_MAX_REQSIZE, |
| 88 | USB_CTRL_GET_TIMEOUT); |
| 89 | |
| 90 | /* store returned data (more READs to be added!) */ |
| 91 | switch (request) { |
| 92 | case CY7C63_READ_PORT: |
| 93 | if (address == CY7C63_READ_PORT_ID0) { |
| 94 | dev->port0 = iobuf[1]; |
| 95 | dev_dbg(&dev->udev->dev, |
| 96 | "READ_PORT0 returned: %d\n",dev->port0); |
| 97 | } |
| 98 | else if (address == CY7C63_READ_PORT_ID1) { |
| 99 | dev->port1 = iobuf[1]; |
| 100 | dev_dbg(&dev->udev->dev, |
| 101 | "READ_PORT1 returned: %d\n",dev->port1); |
| 102 | } |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | kfree(iobuf); |
| 107 | error: |
| 108 | return retval; |
| 109 | } |
| 110 | |
| 111 | #define get_set_port(num,read_id,write_id) \ |
| 112 | static ssize_t set_port##num(struct device *dev, struct device_attribute *attr, \ |
| 113 | const char *buf, size_t count) { \ |
| 114 | \ |
| 115 | int value; \ |
| 116 | int result = 0; \ |
| 117 | \ |
| 118 | struct usb_interface *intf = to_usb_interface(dev); \ |
| 119 | struct cy7c63 *cyp = usb_get_intfdata(intf); \ |
| 120 | \ |
| 121 | dev_dbg(&cyp->udev->dev, "WRITE_PORT%d called\n", num); \ |
| 122 | \ |
| 123 | /* validate input data */ \ |
| 124 | if (sscanf(buf, "%d", &value) < 1) { \ |
| 125 | result = -EINVAL; \ |
| 126 | goto error; \ |
| 127 | } \ |
| 128 | if (value>255 || value<0) { \ |
| 129 | result = -EINVAL; \ |
| 130 | goto error; \ |
| 131 | } \ |
| 132 | \ |
| 133 | result = vendor_command(cyp, CY7C63_WRITE_PORT, write_id, \ |
| 134 | (unsigned char)value); \ |
| 135 | \ |
| 136 | dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n",result); \ |
| 137 | error: \ |
| 138 | return result < 0 ? result : count; \ |
| 139 | } \ |
| 140 | \ |
| 141 | static ssize_t get_port##num(struct device *dev, \ |
| 142 | struct device_attribute *attr, char *buf) { \ |
| 143 | \ |
| 144 | int result = 0; \ |
| 145 | \ |
| 146 | struct usb_interface *intf = to_usb_interface(dev); \ |
| 147 | struct cy7c63 *cyp = usb_get_intfdata(intf); \ |
| 148 | \ |
| 149 | dev_dbg(&cyp->udev->dev, "READ_PORT%d called\n", num); \ |
| 150 | \ |
| 151 | result = vendor_command(cyp, CY7C63_READ_PORT, read_id, 0); \ |
| 152 | \ |
| 153 | dev_dbg(&cyp->udev->dev, "Result of vendor_command: %d\n\n", result); \ |
| 154 | \ |
| 155 | return sprintf(buf, "%d", cyp->port##num); \ |
| 156 | } \ |
| 157 | static DEVICE_ATTR(port##num, S_IWUGO | S_IRUGO, get_port##num, set_port##num); |
| 158 | |
| 159 | get_set_port(0, CY7C63_READ_PORT_ID0, CY7C63_WRITE_PORT_ID0); |
| 160 | get_set_port(1, CY7C63_READ_PORT_ID1, CY7C63_WRITE_PORT_ID1); |
| 161 | |
| 162 | static int cy7c63_probe(struct usb_interface *interface, |
| 163 | const struct usb_device_id *id) { |
| 164 | |
| 165 | struct cy7c63 *dev = NULL; |
| 166 | int retval = -ENOMEM; |
| 167 | |
| 168 | /* allocate memory for our device state and initialize it */ |
| 169 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 170 | if (dev == NULL) { |
| 171 | dev_err(&dev->udev->dev, "Out of memory!\n"); |
| 172 | goto error; |
| 173 | } |
| 174 | |
| 175 | dev->udev = usb_get_dev(interface_to_usbdev(interface)); |
| 176 | |
| 177 | /* save our data pointer in this interface device */ |
| 178 | usb_set_intfdata(interface, dev); |
| 179 | |
| 180 | /* create device attribute files */ |
| 181 | device_create_file(&interface->dev, &dev_attr_port0); |
| 182 | device_create_file(&interface->dev, &dev_attr_port1); |
| 183 | |
| 184 | /* let the user know what node this device is now attached to */ |
| 185 | dev_info(&interface->dev, |
| 186 | "Cypress CY7C63xxx device now attached\n"); |
| 187 | |
| 188 | retval = 0; |
| 189 | error: |
| 190 | return retval; |
| 191 | } |
| 192 | |
| 193 | static void cy7c63_disconnect(struct usb_interface *interface) { |
| 194 | |
| 195 | struct cy7c63 *dev; |
| 196 | |
| 197 | dev = usb_get_intfdata(interface); |
| 198 | usb_set_intfdata(interface, NULL); |
| 199 | |
| 200 | /* remove device attribute files */ |
| 201 | device_remove_file(&interface->dev, &dev_attr_port0); |
| 202 | device_remove_file(&interface->dev, &dev_attr_port1); |
| 203 | |
| 204 | usb_put_dev(dev->udev); |
| 205 | |
| 206 | dev_info(&interface->dev, |
| 207 | "Cypress CY7C63xxx device now disconnected\n"); |
| 208 | |
| 209 | kfree(dev); |
| 210 | } |
| 211 | |
| 212 | static struct usb_driver cy7c63_driver = { |
| 213 | .name = "cy7c63", |
| 214 | .probe = cy7c63_probe, |
| 215 | .disconnect = cy7c63_disconnect, |
| 216 | .id_table = cy7c63_table, |
| 217 | }; |
| 218 | |
| 219 | static int __init cy7c63_init(void) { |
| 220 | |
| 221 | int result; |
| 222 | |
| 223 | /* register this driver with the USB subsystem */ |
| 224 | result = usb_register(&cy7c63_driver); |
| 225 | if (result) { |
| 226 | err("Function usb_register failed! Error number: %d\n", result); |
| 227 | } |
| 228 | |
| 229 | return result; |
| 230 | } |
| 231 | |
| 232 | static void __exit cy7c63_exit(void) { |
| 233 | |
| 234 | /* deregister this driver with the USB subsystem */ |
| 235 | usb_deregister(&cy7c63_driver); |
| 236 | } |
| 237 | |
| 238 | module_init(cy7c63_init); |
| 239 | module_exit(cy7c63_exit); |
| 240 | |
| 241 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 242 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 243 | |
| 244 | MODULE_LICENSE("GPL"); |