updated on Thu Jan 26 16:09:46 UTC 2012
[aur-mirror.git] / dnw / files / dnw-0.1 / secbulk / secbulk.c
blob759a0b10949dcabdf144bf574eac53f519cb78f1
1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/usb.h>
4 #include <linux/fs.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
15 struct secbulk_dev
17 struct usb_device *udev;
18 struct mutex io_mutex;
19 char* bulkout_buffer;
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)},
27 { }
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);
36 if( NULL != dev )
37 kfree(dev);
38 usb_deregister_dev(interface, &secbulk_class);
39 return;
42 static ssize_t secbulk_read(struct file *file, char __user *buf, size_t len, loff_t *loff)
44 return -EPERM;
47 static ssize_t secbulk_write(struct file *file, const char __user *buf, size_t len, loff_t *loff)
49 size_t to_write;
50 struct secbulk_dev *dev = file->private_data;
51 int ret;
52 int actual_length;
53 size_t total_writed;
55 total_writed = 0;
56 while(len > 0)
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");
63 return -EFAULT;
66 ret = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
67 dev->bulkout_buffer,
68 to_write,
69 &actual_length,
70 3*HZ);
71 if(ret || actual_length!=to_write)
73 printk(KERN_ERR "secbulk:usb_bulk_msg failed!\n");
74 return -EFAULT;
76 len -= to_write;
77 total_writed += to_write;
79 return total_writed;
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));
88 if(!interface)
89 return -ENODEV;
91 dev = usb_get_intfdata(interface);
92 dev->bulkout_buffer = kzalloc(BULKOUT_BUFFER_SIZE, GFP_KERNEL);
93 if(!(dev->bulkout_buffer))
94 return -ENOMEM;
95 if(!mutex_trylock(&dev->io_mutex))
96 return -EBUSY;
97 file->private_data = dev;
98 return 0;
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);
108 return 0;
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 = {
120 .name = "secbulk%d",
121 .fops = &secbulk_fops,
122 .minor_base= 100,
125 static int secbulk_probe(struct usb_interface *interface, const struct usb_device_id *id)
127 int ret;
128 struct secbulk_dev *dev;
129 struct usb_host_interface *iface_desc;
130 struct usb_endpoint_descriptor *endpoint;
131 int i;
133 printk(KERN_INFO "secbulk:secbulk probing...\n");
135 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
136 if(!dev)
138 ret = -ENOMEM;
139 goto error;
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;
151 break;
155 if(!(dev->bulk_out_endpointAddr))
157 ret = -EBUSY;
158 goto error;
161 ret = usb_register_dev(interface, &secbulk_class);
162 if(ret)
164 printk(KERN_ERR "secbulk: usb_register_dev failed!\n");
165 return ret;
167 dev->udev = usb_get_dev(interface_to_usbdev(interface));
169 usb_set_intfdata(interface, dev);
171 mutex_init(&dev->io_mutex);
172 return 0;
173 error:
174 if(!dev)
175 kfree(dev);
176 return ret;
179 static struct usb_driver secbulk_driver= {
180 .name= "secbulk",
181 .probe= secbulk_probe,
182 .disconnect= secbulk_disconnect,
183 .id_table= secbulk_table,
184 .supports_autosuspend=0,
186 static int __init secbulk_init(void)
188 int result;
189 printk(KERN_INFO "secbulk:secbulk loaded\n");
190 result = usb_register(&secbulk_driver);
191 if(result)
192 { printk(KERN_ERR "secbulk:usb_register failed: %d", result);
193 return result;
195 return 0;
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");