1 #include <linux/kernel.h>
2 #include <linux/module.h>
5 #include <linux/mutex.h>
6 #include <linux/slab.h>
7 #include <linux/uaccess.h>
9 #define SECBULK_MAJOR 102
10 #define SECBULK_MINOR 0
11 #define DRIVER_NAME "secbulk"
13 #define BULKOUT_BUFFER_SIZE 512
17 struct usb_device
*udev
;
18 struct mutex io_mutex
;
20 __u8 bulk_out_endpointAddr
;
23 static struct usb_class_driver secbulk_class
;
25 static struct usb_device_id secbulk_table
[]= {
26 { USB_DEVICE(0x5345, 0x1234)},
30 static struct usb_driver secbulk_driver
;
31 static void secbulk_disconnect(struct usb_interface
*interface
)
33 struct secbulk_dev
*dev
= NULL
;
34 printk(KERN_INFO
"secbulk:secbulk disconnected!\n");
35 dev
= usb_get_intfdata(interface
);
38 usb_deregister_dev(interface
, &secbulk_class
);
42 static ssize_t
secbulk_read(struct file
*file
, char __user
*buf
, size_t len
, loff_t
*loff
)
47 static ssize_t
secbulk_write(struct file
*file
, const char __user
*buf
, size_t len
, loff_t
*loff
)
50 struct secbulk_dev
*dev
= file
->private_data
;
58 to_write
= min(len
, BULKOUT_BUFFER_SIZE
);
60 if(copy_from_user(dev
->bulkout_buffer
, buf
+total_writed
, to_write
))
62 printk(KERN_ERR
"secbulk:copy_from_user failed!\n");
66 ret
= usb_bulk_msg(dev
->udev
, usb_sndbulkpipe(dev
->udev
, dev
->bulk_out_endpointAddr
),
71 if(ret
|| actual_length
!=to_write
)
73 printk(KERN_ERR
"secbulk:usb_bulk_msg failed!\n");
77 total_writed
+= to_write
;
82 static int secbulk_open(struct inode
*node
, struct file
*file
)
84 struct usb_interface
*interface
;
85 struct secbulk_dev
*dev
;
87 interface
= usb_find_interface(&secbulk_driver
, iminor(node
));
91 dev
= usb_get_intfdata(interface
);
92 dev
->bulkout_buffer
= kzalloc(BULKOUT_BUFFER_SIZE
, GFP_KERNEL
);
93 if(!(dev
->bulkout_buffer
))
95 if(!mutex_trylock(&dev
->io_mutex
))
97 file
->private_data
= dev
;
101 static int secbulk_release(struct inode
*node
, struct file
*file
)
103 struct secbulk_dev
*dev
;
105 dev
= (struct secbulk_dev
*)(file
->private_data
);
106 kfree(dev
->bulkout_buffer
);
107 mutex_unlock(&dev
->io_mutex
);
111 static struct file_operations secbulk_fops
= {
112 .owner
= THIS_MODULE
,
113 .read
= secbulk_read
,
114 .write
= secbulk_write
,
115 .open
= secbulk_open
,
116 .release
= secbulk_release
,
119 static struct usb_class_driver secbulk_class
= {
121 .fops
= &secbulk_fops
,
125 static int secbulk_probe(struct usb_interface
*interface
, const struct usb_device_id
*id
)
128 struct secbulk_dev
*dev
;
129 struct usb_host_interface
*iface_desc
;
130 struct usb_endpoint_descriptor
*endpoint
;
133 printk(KERN_INFO
"secbulk:secbulk probing...\n");
135 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
142 iface_desc
= interface
->cur_altsetting
;
143 for(i
=0; i
< iface_desc
->desc
.bNumEndpoints
; i
++)
145 endpoint
= &(iface_desc
->endpoint
[i
].desc
);
146 if(!dev
->bulk_out_endpointAddr
147 && usb_endpoint_is_bulk_out(endpoint
))
149 printk(KERN_INFO
"secbulk:bulk out endpoint found!\n");
150 dev
->bulk_out_endpointAddr
= endpoint
->bEndpointAddress
;
155 if(!(dev
->bulk_out_endpointAddr
))
161 ret
= usb_register_dev(interface
, &secbulk_class
);
164 printk(KERN_ERR
"secbulk: usb_register_dev failed!\n");
167 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
169 usb_set_intfdata(interface
, dev
);
171 mutex_init(&dev
->io_mutex
);
179 static struct usb_driver secbulk_driver
= {
181 .probe
= secbulk_probe
,
182 .disconnect
= secbulk_disconnect
,
183 .id_table
= secbulk_table
,
184 .supports_autosuspend
=0,
186 static int __init
secbulk_init(void)
189 printk(KERN_INFO
"secbulk:secbulk loaded\n");
190 result
= usb_register(&secbulk_driver
);
192 { printk(KERN_ERR
"secbulk:usb_register failed: %d", result
);
198 static void __exit
secbulk_exit(void)
200 usb_deregister(&secbulk_driver
);
201 printk(KERN_INFO
"secbulk:secbulk unloaded\n");
204 module_init(secbulk_init
);
205 module_exit(secbulk_exit
);
206 MODULE_LICENSE("GPL");