Re: usb-skeleton.c query

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 10/16/06, Greg KH <greg@xxxxxxxxx> wrote:
On Mon, Oct 16, 2006 at 04:25:30PM +0530, pradeep singh wrote:
> Hi all
> I was reading the sample code for usb-skeleton.c  and i need to clear
> somethings.
>
> 1. in skel_probe()
> 247 struct usb_skel *dev = NULL;
> ...
>   254         /* allocate memory for our device state and initialize it */
> 255         dev = kmalloc(sizeof(*dev), GFP_KERNEL);
> 256         if (dev == NULL) {
> 257                 err("Out of memory");
> 258                 goto error;
> 259         }
> ...
>
> struct usb_skel *dev = NULL on line no. 247 but when we allocate
> memory for it we didnt use it like
>    dev = kmalloc(sizeof(struct usb_skel), GFP_KERNEL)
> instead we are using kmalloc(sizeof(*dev), GFP_KERNEL). Had it been
> allocated initially and then using *dev somewhere is understandable.
> but using *dev before allocating dev seems weird.
>
> Shouldnt it be dev = kmalloc(sizeof(struct usb_skel), GFP_KERNEL)
> instead of dev = kmalloc( sizeof(*dev), GFP_KERNEL)?
>
> Or am i missing something here?

Yes, "sizeof(*dev)" returns the size of the structure pointed to by
"dev", which is exactly what you want.  It's easier to do this at times
in case the type of the pointer changes, you do not have to go back and
change all "sizeof(struct usb_skel)" instances and make sure you have
gotten them all.
Got it, thanks a lot :)

> 2. What does le16_to_cpu(...)  does?

Changes from a little endian 16bit number to the cpu native type.

> Is it necessary to use it here?
> 278    buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);

Yes, because wMaxPacketSize is defined as a little endian 16bit number.
You need to convert it to the cpu native endian before being able to use
it properly.
great

Hope this helps,
yes it did a lot.
Thank you

greg k-h



--
play the game

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux