23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/signal.h>
26 #include <linux/sched.h>
27 #include <linux/errno.h>
28 #include <linux/random.h>
29 #include <linux/poll.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/spinlock.h>
33 #include <linux/usb.h>
34 #ifdef CONFIG_DEVFS_FS
35 #include <linux/devfs_fs_kernel.h>
41 #define DRIVER_VERSION "1.2"
42 #define DRIVER_AUTHOR "CAEN Computing Division <support.computing@caen.it>"
43 #define DRIVER_DESC "CAEN USB driver"
45 #define V1718_MINOR_BASE 178
48 #define NAK_TIMEOUT (HZ)
50 #define IBUF_SIZE 0x10000
53 #define OBUF_SIZE 0x10000
56 #define MAX_DEVICES 16
59 #include <linux/version.h>
62 #define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
65 #if LINUX_VERSION_CODE >= VERSION(2,5,0)
66 #define USB_ST_NOERROR 0
67 #define USB_ST_CRC (-EILSEQ)
68 #define USB_ST_BITSTUFF (-EPROTO)
69 #define USB_ST_DATAOVERRUN (-EOVERFLOW)
70 #define USB_ST_DATAUNDERRUN (-EREMOTEIO)
71 #define USB_ST_BUFFEROVERRUN (-ECOMM)
72 #define USB_ST_BUFFERUNDERRUN (-ENOSR)
73 #define USB_ST_INTERNALERROR (-EPROTO)
74 #define USB_ST_SHORT_PACKET (-EREMOTEIO)
75 #define USB_ST_PARTIAL_ERROR (-EXDEV)
76 #define USB_ST_URB_KILLED (-ENOENT)
77 #define USB_ST_URB_PENDING (-EINPROGRESS)
78 #define USB_ST_REMOVED (-ENODEV)
79 #define USB_ST_TIMEOUT (-ETIMEDOUT)
80 #define USB_ST_NOTSUPPORTED (-ENOSYS)
81 #define USB_ST_BANDWIDTH_ERROR (-ENOSPC)
82 #define USB_ST_URB_INVALID_ERROR (-EINVAL)
83 #define USB_ST_URB_REQUEST_ERROR (-ENXIO)
84 #define USB_ST_STALL (-EPIPE)
86 #error ######################################################
87 #error ######################################################
88 #error THIS DRIVER NOT IS COMPATIBLE WITH YOUR KERNEL VERSION
89 #error ######################################################
90 #error ######################################################
94 #if LINUX_VERSION_CODE >= VERSION(2,6,11)
95 #include <linux/mutex.h>
98 #if defined(CONFIG_KERNEL_LOCK) || LINUX_VERSION_CODE < VERSION(2,6,38)
99 #include <linux/smp_lock.h>
117 #if LINUX_VERSION_CODE >= VERSION(2,6,11)
118 struct mutex ioctl_lock;
126 #if LINUX_VERSION_CODE <= VERSION(2,6,35)
129 static DEFINE_SEMAPHORE (minor_table_mutex);
134 #ifndef CONFIG_USB_DYNAMIC_MINORS
135 minor_table[v1718->
minor] = NULL;
139 if (i < MAX_DEVICES) minor_table[
i] = NULL;
141 if( v1718->
ibuf != NULL )
143 if( v1718->
obuf != NULL )
149 #if LINUX_VERSION_CODE >= VERSION(2,6,11)
150 static long unlocked_ioctl_v1718(
struct file *,
unsigned int,
unsigned long);
158 #ifndef CONFIG_USB_DYNAMIC_MINORS
161 for (subminor=0; subminor<
MAX_DEVICES; subminor++)
if (MINOR(inode->i_rdev) == minor_table[subminor]->
minor)
break;
164 if( (subminor < 0) || (subminor > MAX_DEVICES) )
167 down(&minor_table_mutex);
169 v1718 = minor_table[subminor];
170 if( v1718 == NULL ) {
171 up(&minor_table_mutex);
177 up(&minor_table_mutex);
181 init_waitqueue_head(&v1718->
wait_q);
184 #if LINUX_VERSION_CODE >= VERSION(2,6,11)
185 mutex_init(&v1718->ioctl_lock);
187 file->private_data = v1718;
197 if( v1718 == NULL ) {
198 printk(
"Close: object is null\n");
201 down(&minor_table_mutex);
205 printk(
"Device not opened\n");
207 goto exit_not_opened;
214 up(&minor_table_mutex);
222 up(&minor_table_mutex);
237 down(&(v1718->
lock));
239 if ( v1718 == NULL ||
251 if( copy_from_user(&rev, (
v1718_rev_t *)arg,
sizeof(rev)) > 0 ) {
256 if( copy_to_user((
v1718_rev_t *)arg, &rev,
sizeof(rev)) > 0) {
263 ret = usb_control_msg(v1718->
v1718_dev, usb_sndctrlpipe(v1718->
v1718_dev,0),0xB0, 0x20,1, 0, NULL, 0, 1 * HZ);
264 ret |= usb_control_msg(v1718->
v1718_dev, usb_sndctrlpipe(v1718->
v1718_dev,0),0xB0, 0x20,0, 0, NULL, 0, 1 * HZ);
267 ret = usb_control_msg(v1718->
v1718_dev, usb_sndctrlpipe(v1718->
v1718_dev,0),0xB0, 0x20,3, 0, NULL, 0, 1 * HZ);
268 ret |= usb_control_msg(v1718->
v1718_dev, usb_sndctrlpipe(v1718->
v1718_dev,0),0xB0, 0x20,2, 0, NULL, 0, 1 * HZ);
287 #if LINUX_VERSION_CODE >= VERSION(2,6,11)
288 static long unlocked_ioctl_v1718(
struct file *
file,
unsigned int cmd,
unsigned long arg)
290 struct inode *inode = file->f_dentry->d_inode;
301 mutex_lock(&s->ioctl_lock);
303 mutex_unlock(&s->ioctl_lock);
311 size_t count, loff_t * ppos)
315 unsigned long copy_size;
316 unsigned long bytes_written = 0;
317 unsigned int partial;
325 down(&(v1718->
lock));
327 if ( v1718 == NULL ||
336 unsigned long thistime;
339 thistime = copy_size =
341 if (copy_from_user(v1718->
obuf, buffer, copy_size)) {
351 if (signal_pending(current)) {
353 return bytes_written ? bytes_written : -EINTR;
358 obuf, thistime, &partial, 1 * HZ);
360 dbg(
"write stats: result:%d thistime:%lu partial:%u",
361 result, thistime, partial);
363 if (result == USB_ST_TIMEOUT) {
369 wait_event_interruptible_timeout(v1718->
wait_q,(result == USB_ST_TIMEOUT),
NAK_TIMEOUT);
371 }
else if (!result && partial) {
378 printk(
"Write Whoops - %x\n", result);
382 bytes_written += copy_size;
389 return bytes_written ? bytes_written : -EIO;
401 unsigned int partial;
409 down(&(v1718->
lock));
411 if ( v1718 == NULL ||
433 ibuf, this_read, &partial,
436 dbg(
"read stats: result:%d this_read:%u partial:%u",
437 result, this_read, partial);
439 count = this_read = partial;
442 case USB_ST_BUFFERUNDERRUN:
443 printk(
"DBG read_v1718 USB_ST_BUFFERUNDERRUN ...\n" );
451 printk(
"read: maxretry timeout\n");
458 case USB_ST_DATAOVERRUN:
460 int count_retry= 100;
462 printk(
"DATAOVERRUN\n");
467 (
int) (HZ * 1))!= USB_ST_DATAOVERRUN){
470 }
while( count_retry--);
474 case USB_ST_SHORT_PACKET:
475 case USB_ST_PARTIAL_ERROR:
476 case USB_ST_URB_KILLED:
477 case USB_ST_URB_PENDING:
480 case USB_ST_BITSTUFF:
481 case USB_ST_BUFFEROVERRUN:
482 case USB_ST_NOTSUPPORTED:
483 case USB_ST_BANDWIDTH_ERROR:
484 case USB_ST_URB_INVALID_ERROR:
485 case USB_ST_URB_REQUEST_ERROR:
490 printk(
"Read Whoops - result:0x%08x partial:%u this_read:%u\n",
491 result, partial, this_read);
496 if (copy_to_user(buffer, ibuf, this_read)) {
501 read_count += this_read;
514 #if LINUX_VERSION_CODE < VERSION(2,6,38)
518 #if LINUX_VERSION_CODE >= VERSION(2,6,11)
519 unlocked_ioctl: unlocked_ioctl_v1718,
526 .name =
"usb/v1718_%d",
528 #if LINUX_VERSION_CODE <= VERSION(2,6,13)
529 .mode = S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP ,
535 const struct usb_device_id *
id)
537 struct usb_device *
dev = interface_to_usbdev(intf);
543 switch(id->idProduct) {
545 printk(KERN_INFO
"CAEN Desktop Waveform Digitizers Carrier found at address %d\n", dev->devnum);
548 printk(KERN_INFO
"CAEN NIM Waveform Digitizers Carrier found at address %d\n", dev->devnum);
551 printk(KERN_INFO
"CAEN V1718 found at address %d\n", dev->devnum);
554 printk(KERN_INFO
"CAEN V1718 found at address %d\n", dev->devnum);
557 down(&minor_table_mutex);
559 if( minor_table[minor] == NULL )
562 if( minor >= MAX_DEVICES ) {
563 printk(KERN_INFO
"Too many devices\n");
569 printk(
"Out of memory\n");
572 memset(v1718, 0x00,
sizeof(*v1718));
573 minor_table[
minor] = v1718;
575 if( usb_register_dev(intf, &usb_v1718_class))
577 printk(
"probe: Not able to get a minor for this device.\n");
580 #ifdef CONFIG_USB_DYNAMIC_MINORS
587 if (!(v1718->
obuf = (
char *) kmalloc(
OBUF_SIZE, GFP_KERNEL))) {
588 printk(
"probe: Not enough memory for the output buffer\n");
591 dbg(
"probe: obuf address:%p", v1718->
obuf);
593 if (!(v1718->
ibuf = (
char *) kmalloc(
IBUF_SIZE, GFP_KERNEL))) {
594 printk(
"probe: Not enough memory for the input buffer\n");
597 dbg(
"probe: ibuf address:%p", v1718->
ibuf);
599 usb_set_intfdata (intf, v1718);
600 #if LINUX_VERSION_CODE <= VERSION(2,6,32)
601 init_MUTEX(&(v1718->
lock));
603 sema_init(&(v1718->
lock),1);
608 usb_deregister_dev(intf, &usb_v1718_class);
614 up(&minor_table_mutex);
625 down(&minor_table_mutex);
626 down(&(v1718->
lock));
628 minor = v1718->
minor;
630 usb_set_intfdata (intf, NULL);
632 usb_deregister_dev(intf, &usb_v1718_class);
644 printk(KERN_INFO
"CAEN #%d device disconnected\n", minor);
646 up(&minor_table_mutex);
650 { USB_DEVICE(0x0547, 0x1002) },
651 { USB_DEVICE(0x21e1, 0x0000) },
652 { USB_DEVICE(0x21e1, 0x0001) },
660 #if LINUX_VERSION_CODE < VERSION(2,6,15)
672 if (usb_register(&v1718_driver) < 0)
682 usb_deregister(&v1718_driver);