-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsteps
More file actions
80 lines (65 loc) · 3.25 KB
/
steps
File metadata and controls
80 lines (65 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
Command to check if the usb is connected : ls /dev/ |grep sd
Every USB device has a vendor ID and major ID. This is how the system recognizes the device. The USB host controller takes all the binary information from the USB and sends to USB core. The USB driver then kernel (OS). Then kernel checks if there is a device driver to handle the device. Then the kernel makes the device available to the user.
The device is configured in such a way that every device has a profile. There are configurations, within these are interfaces. Describes the functionality of the device. Like printer , fax, scanner are the function and each of these have a interface. Each interface has 1 or more end point .Which decribes input and output. Endpoint ) is used to initialize the device. Types of end points : Control, Interrupt , Bulk and Isochronous. All the end points are uni directional except control which is bi directional.
Mass Storage devices use : Bulk and Isochronous
“lsusb” command lists all the USB connected to the device.
Shows vendor and product ID . for my pendrive Product ID :1000 vendor ID : 090c .This is needed in programming the device.
USB driver :
Have a structure which is the basic definition of USB.
ID table is used to match this driver with the device.
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/usb.h>
// probe function
// called on device insertion if and only if no other driver is using the device
Static int pen_probe(struct usb_interface *interface, const struct usb_device_id *id)
{
Printk(KERN_INFO “[*] pendrive (%04X : %04X) plugged \n”, id->idVendor, id->idProduct);
Return 0; //returning 0 will indicate that we will manage the device
}
// disconnect function
Static void pen_disconnect(struct usb_interface *interface){
Printk(KERN_INFO “[*] pendrive has been removed”);
}
//usb_device_id
Static struct usb_device_id pen_table[]={
//0781:5406
{USB_DEVICE(0x0781, 0x5406)}, //information used from lsusb command, vendor ID, product ID
{} // terminating entry
};
MODULE_DEVICE_TABLE (usb, pen_table);
//usb driver
Static struct usb_driver pendrive *
{
.name=”Transend USB”;
.id_table=pen_table; // used to match this driver with the any device which is attached to usb bus
.probe = pen_probe; // probe function
.disconnect = pen_disconnect; // clean up memory
};
// initializing the pen drive to the USB core
Static int __init pen_init(void){
Int ret = -1;
Printk(KERN_INFO “[*] pendrive constructor of driver”);
Printk(KERN_INFO “ \t Register Driver with kernel”);
Ret= usb_register(&pen_driver);
Printk(KERN_INFO “ \t Registeration is complete”);
Return ret;
}
Static void __exit pen_exit(void){
Printk(KERN_INFO “[*] pendrive destructor of driver”);
Usb_deregister(&open_driver);
Printk(KERN_INFO “\t unregisteration complete “);
}
Module_init(pen_init);
Module_exit(pen_exit);
MODULE_LICENSE(“GPL”);
MODULE_AUTHOR(“pendrive”);
MODULE_DESCRIPTION(“USB Pen Registration Driver”);
MAKE file :
Obj-m := stick_driver.o
KERNEL_DIR = /lib/modules/$(shell uname -r)/build // directory to the source file of the module
PWD = $(shell pwd)
all:
$(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules
clean:
rm -rf *.o *.ko *.mod *.symvers *.order *-