Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | /*****************************************************************************
* USBLCD Kernel Driver *
* See http://www.usblcd.de for Hardware and Documentation. *
* Version 1.03 *
* (C) 2002 Adams IT Services <info@usblcd.de> *
* *
* This file is licensed under the GPL. See COPYING in the package. *
* Based on rio500.c by Cesar Miquel (miquel@df.uba.ar) which is based on *
* hp_scanner.c by David E. Nelson (dnelson@jump.net) *
* *
* 23.7.02 RA changed minor device number to the official assigned one *
*****************************************************************************/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <asm/uaccess.h>
#include <linux/usb.h>
#define DRIVER_VERSION "USBLCD Driver Version 1.03"
#define USBLCD_MINOR 144
#define IOCTL_GET_HARD_VERSION 1
#define IOCTL_GET_DRV_VERSION 2
/* stall/wait timeout for USBLCD */
#define NAK_TIMEOUT (10*HZ)
#define IBUF_SIZE 0x1000
#define OBUF_SIZE 0x10000
struct lcd_usb_data {
struct usb_device *lcd_dev; /* init: probe_lcd */
unsigned int ifnum; /* Interface number of the USB device */
int minor; /* minor number for this device */
int isopen; /* nz if open */
int present; /* Device is present on the bus */
char *obuf, *ibuf; /* transfer buffers */
char bulk_in_ep, bulk_out_ep; /* Endpoint assignments */
wait_queue_head_t wait_q; /* for timeouts */
};
static struct lcd_usb_data lcd_instance;
static int open_lcd(struct inode *inode, struct file *file)
{
struct lcd_usb_data *lcd = &lcd_instance;
if (lcd->isopen || !lcd->present) {
return -EBUSY;
}
lcd->isopen = 1;
init_waitqueue_head(&lcd->wait_q);
info("USBLCD opened.");
return 0;
}
static int close_lcd(struct inode *inode, struct file *file)
{
struct lcd_usb_data *lcd = &lcd_instance;
lcd->isopen = 0;
info("USBLCD closed.");
return 0;
}
static int
ioctl_lcd(struct inode *inode, struct file *file, unsigned int cmd,
unsigned long arg)
{
struct lcd_usb_data *lcd = &lcd_instance;
int i;
char buf[30];
/* Sanity check to make sure lcd is connected, powered, etc */
if (lcd == NULL ||
lcd->present == 0 ||
lcd->lcd_dev == NULL)
return -1;
switch (cmd) {
case IOCTL_GET_HARD_VERSION:
i = (lcd->lcd_dev)->descriptor.bcdDevice;
sprintf(buf,"%1d%1d.%1d%1d",(i & 0xF000)>>12,(i & 0xF00)>>8,
(i & 0xF0)>>4,(i & 0xF));
if (copy_to_user((void *)arg,buf,strlen(buf))!=0)
return -EFAULT;
break;
case IOCTL_GET_DRV_VERSION:
sprintf(buf,DRIVER_VERSION);
if (copy_to_user((void *)arg,buf,strlen(buf))!=0)
return -EFAULT;
break;
default:
return -ENOTTY;
break;
}
return 0;
}
static ssize_t
write_lcd(struct file *file, const char *buffer,
size_t count, loff_t * ppos)
{
struct lcd_usb_data *lcd = &lcd_instance;
unsigned long copy_size;
unsigned long bytes_written = 0;
unsigned int partial;
int result = 0;
int maxretry;
/* Sanity check to make sure lcd is connected, powered, etc */
if (lcd == NULL ||
lcd->present == 0 ||
lcd->lcd_dev == NULL)
return -1;
do {
unsigned long thistime;
char *obuf = lcd->obuf;
thistime = copy_size =
(count >= OBUF_SIZE) ? OBUF_SIZE : count;
if (copy_from_user(lcd->obuf, buffer, copy_size))
return -EFAULT;
maxretry = 5;
while (thistime) {
if (!lcd->lcd_dev)
return -ENODEV;
if (signal_pending(current)) {
return bytes_written ? bytes_written : -EINTR;
}
result = usb_bulk_msg(lcd->lcd_dev,
usb_sndbulkpipe(lcd->lcd_dev, 1),
obuf, thistime, &partial, 10 * HZ);
dbg("write stats: result:%d thistime:%lu partial:%u",
result, thistime, partial);
if (result == -ETIMEDOUT) { /* NAK - so hold for a while */
if (!maxretry--) {
return -ETIME;
}
interruptible_sleep_on_timeout(&lcd-> wait_q, NAK_TIMEOUT);
continue;
} else if (!result & partial) {
obuf += partial;
thistime -= partial;
} else
break;
};
if (result) {
err("Write Whoops - %x", result);
return -EIO;
}
bytes_written += copy_size;
count -= copy_size;
buffer += copy_size;
} while (count > 0);
return bytes_written ? bytes_written : -EIO;
}
static ssize_t
read_lcd(struct file *file, char *buffer, size_t count, loff_t * ppos)
{
struct lcd_usb_data *lcd = &lcd_instance;
ssize_t read_count;
unsigned int partial;
int this_read;
int result;
int maxretry = 10;
char *ibuf = lcd->ibuf;
/* Sanity check to make sure lcd is connected, powered, etc */
if (lcd == NULL ||
lcd->present == 0 ||
lcd->lcd_dev == NULL)
return -1;
read_count = 0;
while (count > 0) {
if (signal_pending(current)) {
return read_count ? read_count : -EINTR;
}
if (!lcd->lcd_dev)
return -ENODEV;
this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count;
result = usb_bulk_msg(lcd->lcd_dev,
usb_rcvbulkpipe(lcd->lcd_dev, 0),
ibuf, this_read, &partial,
(int) (HZ * 8));
dbg(KERN_DEBUG "read stats: result:%d this_read:%u partial:%u",
result, this_read, partial);
if (partial) {
count = this_read = partial;
} else if (result == -ETIMEDOUT || result == 15) { /* FIXME: 15 ??? */
if (!maxretry--) {
err("read_lcd: maxretry timeout");
return -ETIME;
}
interruptible_sleep_on_timeout(&lcd->wait_q,
NAK_TIMEOUT);
continue;
} else if (result != -EREMOTEIO) {
err("Read Whoops - result:%u partial:%u this_read:%u",
result, partial, this_read);
return -EIO;
} else {
return (0);
}
if (this_read) {
if (copy_to_user(buffer, ibuf, this_read))
return -EFAULT;
count -= this_read;
read_count += this_read;
buffer += this_read;
}
}
return read_count;
}
static struct
file_operations usb_lcd_fops = {
.owner = THIS_MODULE,
.read = read_lcd,
.write = write_lcd,
.ioctl = ioctl_lcd,
.open = open_lcd,
.release = close_lcd,
};
static int probe_lcd(struct usb_interface *intf, const struct usb_device_id *id)
{
struct usb_device *dev = interface_to_usbdev(intf);
struct lcd_usb_data *lcd = &lcd_instance;
int i;
int retval;
if (dev->descriptor.idProduct != 0x0001 ) {
warn(KERN_INFO "USBLCD model not supported.");
return -ENODEV;
}
if (lcd->present == 1) {
warn(KERN_INFO "Multiple USBLCDs are not supported!");
return -ENODEV;
}
i = dev->descriptor.bcdDevice;
info("USBLCD Version %1d%1d.%1d%1d found at address %d",
(i & 0xF000)>>12,(i & 0xF00)>>8,(i & 0xF0)>>4,(i & 0xF),
dev->devnum);
retval = usb_register_dev(&usb_lcd_fops, USBLCD_MINOR, 1, &lcd->minor);
if (retval) {
err("Not able to get a minor for this device.");
return -ENOMEM;
}
lcd->present = 1;
lcd->lcd_dev = dev;
if (!(lcd->obuf = (char *) kmalloc(OBUF_SIZE, GFP_KERNEL))) {
err("probe_lcd: Not enough memory for the output buffer");
return -ENOMEM;
}
dbg("probe_lcd: obuf address:%p", lcd->obuf);
if (!(lcd->ibuf = (char *) kmalloc(IBUF_SIZE, GFP_KERNEL))) {
err("probe_lcd: Not enough memory for the input buffer");
kfree(lcd->obuf);
return -ENOMEM;
}
dbg("probe_lcd: ibuf address:%p", lcd->ibuf);
usb_set_intfdata (intf, lcd);
return 0;
}
static void disconnect_lcd(struct usb_interface *intf)
{
struct lcd_usb_data *lcd = usb_get_intfdata (intf);
usb_set_intfdata (intf, NULL);
if (lcd) {
usb_deregister_dev(1, lcd->minor);
if (lcd->isopen) {
lcd->isopen = 0;
/* better let it finish - the release will do whats needed */
lcd->lcd_dev = NULL;
return;
}
kfree(lcd->ibuf);
kfree(lcd->obuf);
info("USBLCD disconnected.");
lcd->present = 0;
}
}
static struct usb_device_id id_table [] = {
{ .idVendor = 0x10D2, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, },
{},
};
MODULE_DEVICE_TABLE (usb, id_table);
static struct
usb_driver lcd_driver = {
.name = "usblcd",
.probe = (void *)probe_lcd,
.disconnect = disconnect_lcd,
.id_table = id_table,
};
int usb_lcd_init(void)
{
if (usb_register(&lcd_driver) < 0)
return -1;
info("%s (C) Adams IT Services http://www.usblcd.de", DRIVER_VERSION);
info("USBLCD support registered.");
return 0;
}
void usb_lcd_cleanup(void)
{
struct lcd_usb_data *lcd = &lcd_instance;
lcd->present = 0;
usb_deregister(&lcd_driver);
}
module_init(usb_lcd_init);
module_exit(usb_lcd_cleanup);
MODULE_AUTHOR("Adams IT Services <info@usblcd.de>");
MODULE_DESCRIPTION(DRIVER_VERSION);
MODULE_LICENSE("GPL");
|