63 #include <linux/mutex.h>
64 #include <linux/kernel.h>
65 #include <linux/errno.h>
66 #include <linux/init.h>
67 #include <linux/slab.h>
68 #include <linux/module.h>
69 #include <linux/kref.h>
70 #include <linux/usb.h>
71 #include <asm/uaccess.h>
73 #define to_umb_driver(d) container_of(d, struct usb_umb, kref)
90 #define UMB_VENDOR_ID 0x12d1
91 #define UMB_PRODUCT_ID 0x140b
93 struct mutex umb_mutex;
95 MODULE_LICENSE(
"GPL");
97 static struct usb_device_id umb_table[] = {
98 { USB_DEVICE(UMB_VENDOR_ID, UMB_PRODUCT_ID)},
105 static ssize_t umb_read(
struct file* f,
char __user *buffer,
size_t count, loff_t* ppos);
106 static ssize_t umb_write(
struct file *f,
const char __user *user_buffer,
size_t count, loff_t *ppos);
107 static void umb_probe(
struct usb_interface *interface,
const struct usb_device_id*
id);
108 static void umb_disconnect(
struct usb_interface *interface);
109 static int umb_release(
struct inode* inode,
struct file* file);
110 static void umb_delete(
struct kref* kref);
111 static int umb_open(
struct inode* inode,
struct file* file);
112 static char* umb_devnode(
struct device *dev, mode_t *mode);
114 #define USB_UMB_MINOR_BASE 378
116 static struct usb_driver umb_driver = {
118 .id_table = umb_table,
120 .disconnect = umb_disconnect
123 static struct file_operations umb_fops = {
124 .owner = THIS_MODULE,
128 .release = umb_release
135 static struct usb_class_driver umb_class = {
148 struct usb_device* udev;
149 struct usb_interface* interface;
150 unsigned char* bulk_in_buffer;
155 unsigned char* bulk_out_buffer;
157 __u8 bulk_in_endpointAddr;
158 __u8 bulk_out_endpointAddr;
162 static ssize_t umb_read(
struct file* f,
char __user *buffer,
size_t count, loff_t* ppos)
167 printk(KERN_INFO
"umb_read(): before usb_bulk_msg\n");
168 dev = (
struct usb_umb*)f->private_data;
169 retval = usb_bulk_msg(dev->udev,
170 usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
172 min(dev->bulk_in_size, count),
176 printk(KERN_INFO
"umb_read(): before copy_to_user(): dev->bulk_in_buffer=%s\n", dev->bulk_in_buffer);
177 copy_to_user(buffer, dev->bulk_in_buffer, count);
182 static void umb_write_bulk_callback(
struct urb *urb,
struct pt_regs *regs)
186 printk(KERN_INFO
"in function %s and urb status is %d \n", urb->status);
189 static ssize_t umb_write(
struct file *f,
const char __user *user_buffer,
size_t count, loff_t *ppos)
193 struct urb *urb = NULL;
196 printk(KERN_INFO
"umb_write(): before usb_alloc_urb\n");
197 dev = (
struct usb_umb*)f->private_data;
203 urb = usb_alloc_urb(0, GFP_ATOMIC);
205 buf = usb_alloc_coherent(dev->udev, count, GFP_ATOMIC, &urb->transfer_dma);
206 printk(KERN_INFO
"umb_write(): before copy_from_user(): buf=%s\n", buf);
207 copy_from_user(buf, user_buffer, count);
210 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
211 buf, count, umb_write_bulk_callback, dev);
212 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
213 retval = usb_submit_urb(urb, GFP_ATOMIC);
218 static char* umb_devnode(
struct device *dev, mode_t *mode)
223 static int umb_init(
void)
226 result = usb_register(&umb_driver);
227 printk(KERN_INFO
"umb_init(): in init ...after usb_register(), result %d\n", result);
231 static void umb_exit(
void)
233 usb_deregister(&umb_driver);
234 printk(KERN_INFO
"umb_exit(): in exit\n");
238 static void umb_probe(
struct usb_interface *interface,
const struct usb_device_id*
id)
241 struct usb_host_interface* iface_desc;
242 struct usb_endpoint_descriptor* endpoint;
245 int retval = -ENOMEM;
247 printk(KERN_INFO
"umb_probe(): before probing\n");
248 dev=kzalloc(
sizeof(
struct usb_umb), GFP_ATOMIC);
249 memset(dev, 0x0,
sizeof(
struct usb_umb));
250 printk(KERN_INFO
"umb_probe(): before kref_init\n");
251 kref_init(&dev->kref);
253 printk(KERN_INFO
"umb_probe(): before usb_get_dev\n");
254 dev->udev = usb_get_dev(interface_to_usbdev(interface));
255 dev->interface = interface;
257 iface_desc = interface->cur_altsetting;
258 for(i=0; i < iface_desc->desc.bNumEndpoints; i++)
260 printk(KERN_INFO
"umb_probe(): looping over interface endpoints: i= %d\n",i);
261 endpoint = &iface_desc->endpoint[i].desc;
262 if(!dev->bulk_in_endpointAddr &&
263 (endpoint->bEndpointAddress & USB_DIR_IN) &&
264 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
265 == USB_ENDPOINT_XFER_BULK))
267 printk(KERN_INFO
"umb_probe(): before initializing dev bulk in data\n");
268 buffer_size = endpoint->wMaxPacketSize;
269 dev->bulk_in_size = buffer_size;
270 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
271 dev->bulk_in_buffer = kzalloc(buffer_size, GFP_ATOMIC);
273 if(!dev->bulk_out_endpointAddr &&
274 (endpoint->bEndpointAddress & USB_DIR_OUT) &&
275 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
276 == USB_ENDPOINT_XFER_BULK))
278 printk(KERN_INFO
"umb_probe(): before allocating dev bulk out data\n");
279 dev->bulk_out_buffer = kzalloc(buffer_size,GFP_ATOMIC);
280 dev->bulk_out_endpointAddr= endpoint->bEndpointAddress;
283 printk(KERN_INFO
"umb_probe(): before usb_set_intfdata\n");
284 usb_set_intfdata(interface, dev);
285 printk(KERN_INFO
"umb_probe(): before usb_register_dev, interface=%p, umb_class=%p\n", interface, &umb_class);
286 usb_register_dev(interface, &umb_class);
290 static void umb_disconnect(
struct usb_interface *interface)
293 int minor = interface->minor;
303 printk(KERN_INFO
"umb_disconnect(): before deregistering device");
304 dev = usb_get_intfdata(interface);
305 usb_set_intfdata(interface,NULL);
306 usb_deregister_dev(interface, &umb_class);
309 kref_put(&dev->kref, umb_delete);
312 static int umb_open(
struct inode* inode,
struct file* file)
315 struct usb_interface *interface;
319 subminor = iminor(inode);
321 interface = usb_find_interface(&umb_driver, subminor);
322 dev = usb_get_intfdata(interface);
324 kref_get(&dev->kref);
326 file->private_data = dev;
330 static int umb_release(
struct inode* inode,
struct file* file)
333 dev = (
struct usb_umb*)file->private_data;
334 kref_put(&dev->kref, umb_delete);
337 static void umb_delete(
struct kref* kref)
339 struct usb_umb* dev = to_umb_driver(kref);
340 usb_put_dev(dev->udev);
341 kfree(dev->bulk_in_buffer);
345 module_init(umb_init);
346 module_exit(umb_exit);