16 #include <linux/config.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/module.h>
22 #include <linux/kref.h>
23 #include <linux/smp_lock.h>
24 #include <linux/usb.h>
25 #include <asm/uaccess.h>
29 #define USB_SKEL_VENDOR_ID 0xfff0
30 #define USB_SKEL_PRODUCT_ID 0xfff0
33 static struct usb_device_id skel_table [] = {
34 { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
37 MODULE_DEVICE_TABLE (usb, skel_table);
41 #define USB_SKEL_MINOR_BASE 192
45 struct usb_device * udev;
46 struct usb_interface * interface;
47 unsigned char * bulk_in_buffer;
49 __u8 bulk_in_endpointAddr;
50 __u8 bulk_out_endpointAddr;
53 #define to_skel_dev(d) container_of(d, struct usb_skel, kref)
55 static struct usb_driver skel_driver;
57 static void skel_delete(
struct kref *kref)
59 struct usb_skel *dev = to_skel_dev(kref);
61 usb_put_dev(dev->udev);
62 kfree (dev->bulk_in_buffer);
66 static int skel_open(
struct inode *inode,
struct file *file)
69 struct usb_interface *interface;
73 subminor = iminor(inode);
75 interface = usb_find_interface(&skel_driver, subminor);
77 err (
"%s - error, can't find device for minor %d",
78 __FUNCTION__, subminor);
83 dev = usb_get_intfdata(interface);
93 file->private_data = dev;
99 static int skel_release(
struct inode *inode,
struct file *file)
103 dev = (
struct usb_skel *)file->private_data;
108 kref_put(&dev->kref, skel_delete);
112 static ssize_t skel_read(
struct file *file,
char __user *buffer,
size_t count, loff_t *ppos)
117 dev = (
struct usb_skel *)file->private_data;
120 retval = usb_bulk_msg(dev->udev,
121 usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
123 min(dev->bulk_in_size, count),
128 if (copy_to_user(buffer, dev->bulk_in_buffer, count))
137 static void skel_write_bulk_callback(
struct urb *urb,
struct pt_regs *regs)
141 !(urb->status == -ENOENT ||
142 urb->status == -ECONNRESET ||
143 urb->status == -ESHUTDOWN)) {
144 dbg(
"%s - nonzero write bulk status received: %d",
145 __FUNCTION__, urb->status);
149 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
150 urb->transfer_buffer, urb->transfer_dma);
153 static ssize_t skel_write(
struct file *file,
const char __user *user_buffer,
size_t count, loff_t *ppos)
157 struct urb *urb = NULL;
160 dev = (
struct usb_skel *)file->private_data;
167 urb = usb_alloc_urb(0, GFP_KERNEL);
173 buf = usb_buffer_alloc(dev->udev, count, GFP_KERNEL, &urb->transfer_dma);
178 if (copy_from_user(buf, user_buffer, count)) {
184 usb_fill_bulk_urb(urb, dev->udev,
185 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
186 buf, count, skel_write_bulk_callback, dev);
187 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
190 retval = usb_submit_urb(urb, GFP_KERNEL);
192 err(
"%s - failed submitting write urb, error %d", __FUNCTION__, retval);
203 usb_buffer_free(dev->udev, count, buf, urb->transfer_dma);
209 static struct file_operations skel_fops = {
210 .owner = THIS_MODULE,
214 .release = skel_release,
221 static struct usb_class_driver skel_class = {
222 .name =
"usb/skel%d",
224 .mode = S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH,
225 .minor_base = USB_SKEL_MINOR_BASE,
228 static int skel_probe(
struct usb_interface *interface,
const struct usb_device_id *
id)
231 struct usb_host_interface *iface_desc;
232 struct usb_endpoint_descriptor *endpoint;
235 int retval = -ENOMEM;
238 dev = kmalloc(
sizeof(
struct usb_skel), GFP_KERNEL);
240 err(
"Out of memory");
243 memset(dev, 0x00,
sizeof (*dev));
244 kref_init(&dev->kref);
246 dev->udev = usb_get_dev(interface_to_usbdev(interface));
247 dev->interface = interface;
251 iface_desc = interface->cur_altsetting;
252 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
253 endpoint = &iface_desc->endpoint[i].desc;
255 if (!dev->bulk_in_endpointAddr &&
256 (endpoint->bEndpointAddress & USB_DIR_IN) &&
257 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
258 == USB_ENDPOINT_XFER_BULK)) {
260 buffer_size = endpoint->wMaxPacketSize;
261 dev->bulk_in_size = buffer_size;
262 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
263 dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
264 if (!dev->bulk_in_buffer) {
265 err(
"Could not allocate bulk_in_buffer");
270 if (!dev->bulk_out_endpointAddr &&
271 !(endpoint->bEndpointAddress & USB_DIR_IN) &&
272 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
273 == USB_ENDPOINT_XFER_BULK)) {
275 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
278 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
279 err(
"Could not find both bulk-in and bulk-out endpoints");
284 usb_set_intfdata(interface, dev);
287 retval = usb_register_dev(interface, &skel_class);
290 err(
"Not able to get a minor for this device.");
291 usb_set_intfdata(interface, NULL);
296 info(
"USB Skeleton device now attached to USBSkel-%d", interface->minor);
301 kref_put(&dev->kref, skel_delete);
305 static void skel_disconnect(
struct usb_interface *interface)
308 int minor = interface->minor;
313 dev = usb_get_intfdata(interface);
314 usb_set_intfdata(interface, NULL);
317 usb_deregister_dev(interface, &skel_class);
322 kref_put(&dev->kref, skel_delete);
324 info(
"USB Skeleton #%d now disconnected", minor);
327 static struct usb_driver skel_driver = {
328 .owner = THIS_MODULE,
330 .id_table = skel_table,
332 .disconnect = skel_disconnect,
335 static int __init usb_skel_init(
void)
340 result = usb_register(&skel_driver);
342 err(
"usb_register failed. Error number %d", result);
347 static void __exit usb_skel_exit(
void)
350 usb_deregister(&skel_driver);
353 module_init (usb_skel_init);
354 module_exit (usb_skel_exit);
356 MODULE_LICENSE(
"GPL");