Re: [PATCH 1/3] picolcd: driver for PicoLCD HID device

From: Rick L. Vinyard, Jr.
Date: Wed Feb 24 2010 - 16:45:19 EST



Oliver Neukum wrote:
> Am Mittwoch, 24. Februar 2010 17:00:49 schrieb Bruno Prémont:
>> +static int picolcd_raw_event(struct hid_device *hdev,
>> + struct hid_report *report, u8 *raw_data, int size)
>> +{
>> + struct picolcd_data *data = hid_get_drvdata(hdev);
>> + char hexdata[25];
>> + int i;
>> +
>> + if (data == NULL)
>> + return 1;
>> +
>> + for (i = 0; i < sizeof(hexdata) / 2; i++)
>> + sprintf(hexdata+2*i, "%02hhx", raw_data[i]);
>> + if (size >= sizeof(hexdata)/2) {
>> + hexdata[sizeof(hexdata)-4] = '.';
>> + hexdata[sizeof(hexdata)-3] = '.';
>> + hexdata[sizeof(hexdata)-2] = '.';
>> + hexdata[sizeof(hexdata)-1] = '\0';
>> + } else
>> + hexdata[size*2] = '\0';
>> +
>> + switch (report->id) {
>> + case REPORT_KEYPAD:
>> + if (size == 3 && raw_data[0] == 0x11 &&
>> data->input_keys) {
>> + return picolcd_raw_keypad(hdev, report,
>> raw_data+1, size-1);
>> + } else {
>> + dbg_hid(PICOLCD_NAME " unsupported key event (%d
>> bytes): 0x%s\n", size, hexdata);
>> + break;
>> + }
>> + break;
>> + case REPORT_VERSION:
>> + if (size == 3)
>> + dev_info(&hdev->dev, "Firmware version is
>> %hd.%hd\n", raw_data[1], raw_data[2]);
>> +
>> + spin_lock(&data->lock);
>
> If I recall correctly raw_event is called in interrupt. As you take a
> spinlock here,
> the lock in code not called in interrupt must disable interrupts, or you
> may
> deadlock.
>

The issue, as I understand it is that non-interrupt code may obtain the
lock and then the interrupt code is executed... hence the deadlock and the
need to use spin_lock_irqsave() and spin_unlock_irqrestore().

If that is correct, is there any problem with the following approach?

The key difference is the replacement of spin_lock() with spin_trylock()
such that if the non-interrupt code has already obtained the lock, the
interrupt will not deadlock but instead take the else path and schedule a
framebuffer update at the next interval.

static void g13_fb_urb_completion(struct urb *urb)
{
struct g13_data *data = urb->context;
spin_unlock(&data->fb_urb_lock);
}

static int g13_fb_send(struct hid_device *hdev)
{
struct usb_interface *intf;
struct usb_device *usb_dev;
struct g13_data *data = hid_get_g13data(hdev);

struct usb_host_endpoint *ep;
unsigned int pipe;
int retval = 0;

/*
* Try and lock the framebuffer urb to prevent access if we have
* submitted it. If we can't lock it we'll have to delay this update
* until the next framebuffer interval.
*
* Fortunately, we already have the infrastructure in place with the
* framebuffer deferred I/O driver to schedule the delayed update.
*/
if (likely(spin_trylock(&data->fb_urb_lock))) {
/* Get the usb device to send the image on */
intf = to_usb_interface(hdev->dev.parent);
usb_dev = interface_to_usbdev(intf);

pipe = usb_sndintpipe(usb_dev, 0x02);

ep = usb_dev->ep_out[usb_pipeendpoint(pipe)];

if (unlikely(!ep)) {
spin_unlock(&data->fb_urb_lock);
return -EINVAL;
}

pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
usb_fill_int_urb(data->fb_urb, usb_dev, pipe, data->fb_vbitmap,
G13_VBITMAP_SIZE, g13_fb_urb_completion, NULL,
ep->desc.bInterval);
data->fb_urb->context = data;
data->fb_urb->actual_length = 0;

retval = usb_submit_urb(data->fb_urb, GFP_NOIO);
if (unlikely(retval < 0)) {
/*
* We need to unlock the framebuffer urb lock since
* the urb submission failed and therefore
* g13_fb_urb_completion() won't be called.
*/
spin_unlock(&data->fb_urb_lock);
return retval;
}
} else {
schedule_delayed_work(&data->fb_info->deferred_work,
data->fb_defio.delay);
}

return retval;
}

Thanks,

--

Rick

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/