unsigned int i;
        int ret;
 
+       if (!lh->gdev->chip)
+               return -ENODEV;
+
        switch (cmd) {
        case GPIOHANDLE_GET_LINE_VALUES_IOCTL:
                /* NOTE: It's okay to read values of output lines */
        struct linereq *lr = file->private_data;
        void __user *ip = (void __user *)arg;
 
+       if (!lr->gdev->chip)
+               return -ENODEV;
+
        switch (cmd) {
        case GPIO_V2_LINE_GET_VALUES_IOCTL:
                return linereq_get_values(lr, ip);
        struct linereq *lr = file->private_data;
        __poll_t events = 0;
 
+       if (!lr->gdev->chip)
+               return EPOLLHUP | EPOLLERR;
+
        poll_wait(file, &lr->wait, wait);
 
        if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
        ssize_t bytes_read = 0;
        int ret;
 
+       if (!lr->gdev->chip)
+               return -ENODEV;
+
        if (count < sizeof(le))
                return -EINVAL;
 
        struct lineevent_state *le = file->private_data;
        __poll_t events = 0;
 
+       if (!le->gdev->chip)
+               return EPOLLHUP | EPOLLERR;
+
        poll_wait(file, &le->wait, wait);
 
        if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
        ssize_t ge_size;
        int ret;
 
+       if (!le->gdev->chip)
+               return -ENODEV;
+
        /*
         * When compatible system call is being used the struct gpioevent_data,
         * in case of at least ia32, has different size due to the alignment
        void __user *ip = (void __user *)arg;
        struct gpiohandle_data ghd;
 
+       if (!le->gdev->chip)
+               return -ENODEV;
+
        /*
         * We can get the value for an event line but not set it,
         * because it is input by definition.
        struct gpio_chardev_data *cdev = file->private_data;
        __poll_t events = 0;
 
+       if (!cdev->gdev->chip)
+               return EPOLLHUP | EPOLLERR;
+
        poll_wait(file, &cdev->wait, pollt);
 
        if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
        int ret;
        size_t event_size;
 
+       if (!cdev->gdev->chip)
+               return -ENODEV;
+
 #ifndef CONFIG_GPIO_CDEV_V1
        event_size = sizeof(struct gpio_v2_line_info_changed);
        if (count < event_size)