USB: cxacru: Use appropriate logging for errors
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / usb / atm / cxacru.c
index 8bcf7fe..a51eeed 100644 (file)
@@ -171,7 +171,7 @@ struct cxacru_data {
        struct delayed_work poll_work;
        u32 card_info[CXINF_MAX];
        struct mutex poll_state_serialize;
-       int poll_state;
+       enum cxacru_poll_state poll_state;
 
        /* contol handles */
        struct mutex cm_serialize;
@@ -226,58 +226,48 @@ static ssize_t cxacru_sysfs_showattr_s8(s8 value, char *buf)
 
 static ssize_t cxacru_sysfs_showattr_dB(s16 value, char *buf)
 {
-       if (unlikely(value < 0)) {
-               return snprintf(buf, PAGE_SIZE, "%d.%02u\n",
-                                               value / 100, -value % 100);
-       } else {
-               return snprintf(buf, PAGE_SIZE, "%d.%02u\n",
-                                               value / 100, value % 100);
-       }
+       return snprintf(buf, PAGE_SIZE, "%d.%02u\n",
+                                       value / 100, abs(value) % 100);
 }
 
 static ssize_t cxacru_sysfs_showattr_bool(u32 value, char *buf)
 {
-       switch (value) {
-       case 0: return snprintf(buf, PAGE_SIZE, "no\n");
-       case 1: return snprintf(buf, PAGE_SIZE, "yes\n");
-       default: return 0;
-       }
+       static char *str[] = { "no", "yes" };
+       if (unlikely(value >= ARRAY_SIZE(str)))
+               return snprintf(buf, PAGE_SIZE, "%u\n", value);
+       return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
 }
 
 static ssize_t cxacru_sysfs_showattr_LINK(u32 value, char *buf)
 {
-       switch (value) {
-       case 1: return snprintf(buf, PAGE_SIZE, "not connected\n");
-       case 2: return snprintf(buf, PAGE_SIZE, "connected\n");
-       case 3: return snprintf(buf, PAGE_SIZE, "lost\n");
-       default: return snprintf(buf, PAGE_SIZE, "unknown (%u)\n", value);
-       }
+       static char *str[] = { NULL, "not connected", "connected", "lost" };
+       if (unlikely(value >= ARRAY_SIZE(str) || str[value] == NULL))
+               return snprintf(buf, PAGE_SIZE, "%u\n", value);
+       return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
 }
 
 static ssize_t cxacru_sysfs_showattr_LINE(u32 value, char *buf)
 {
-       switch (value) {
-       case 0: return snprintf(buf, PAGE_SIZE, "down\n");
-       case 1: return snprintf(buf, PAGE_SIZE, "attempting to activate\n");
-       case 2: return snprintf(buf, PAGE_SIZE, "training\n");
-       case 3: return snprintf(buf, PAGE_SIZE, "channel analysis\n");
-       case 4: return snprintf(buf, PAGE_SIZE, "exchange\n");
-       case 5: return snprintf(buf, PAGE_SIZE, "up\n");
-       case 6: return snprintf(buf, PAGE_SIZE, "waiting\n");
-       case 7: return snprintf(buf, PAGE_SIZE, "initialising\n");
-       default: return snprintf(buf, PAGE_SIZE, "unknown (%u)\n", value);
-       }
+       static char *str[] = { "down", "attempting to activate",
+               "training", "channel analysis", "exchange", "up",
+               "waiting", "initialising"
+       };
+       if (unlikely(value >= ARRAY_SIZE(str)))
+               return snprintf(buf, PAGE_SIZE, "%u\n", value);
+       return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
 }
 
 static ssize_t cxacru_sysfs_showattr_MODU(u32 value, char *buf)
 {
-       switch (value) {
-       case 0: return 0;
-       case 1: return snprintf(buf, PAGE_SIZE, "ANSI T1.413\n");
-       case 2: return snprintf(buf, PAGE_SIZE, "ITU-T G.992.1 (G.DMT)\n");
-       case 3: return snprintf(buf, PAGE_SIZE, "ITU-T G.992.2 (G.LITE)\n");
-       default: return snprintf(buf, PAGE_SIZE, "unknown (%u)\n", value);
-       }
+       static char *str[] = {
+                       NULL,
+                       "ANSI T1.413",
+                       "ITU-T G.992.1 (G.DMT)",
+                       "ITU-T G.992.2 (G.LITE)"
+       };
+       if (unlikely(value >= ARRAY_SIZE(str) || str[value] == NULL))
+               return snprintf(buf, PAGE_SIZE, "%u\n", value);
+       return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
 }
 
 /*
@@ -308,11 +298,10 @@ static ssize_t cxacru_sysfs_show_adsl_state(struct device *dev,
        struct cxacru_data *instance = usbatm_instance->driver_data;
        u32 value = instance->card_info[CXINF_LINE_STARTABLE];
 
-       switch (value) {
-       case 0: return snprintf(buf, PAGE_SIZE, "running\n");
-       case 1: return snprintf(buf, PAGE_SIZE, "stopped\n");
-       default: return snprintf(buf, PAGE_SIZE, "unknown (%u)\n", value);
-       }
+       static char *str[] = { "running", "stopped" };
+       if (unlikely(value >= ARRAY_SIZE(str)))
+               return snprintf(buf, PAGE_SIZE, "%u\n", value);
+       return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
 }
 
 static ssize_t cxacru_sysfs_store_adsl_state(struct device *dev,
@@ -467,7 +456,6 @@ static int cxacru_start_wait_urb(struct urb *urb, struct completion *done,
                                 int* actual_length)
 {
        struct timer_list timer;
-       int status;
 
        init_timer(&timer);
        timer.expires = jiffies + msecs_to_jiffies(CMD_TIMEOUT);
@@ -475,12 +463,11 @@ static int cxacru_start_wait_urb(struct urb *urb, struct completion *done,
        timer.function = cxacru_timeout_kill;
        add_timer(&timer);
        wait_for_completion(done);
-       status = urb->status;
        del_timer_sync(&timer);
 
        if (actual_length)
                *actual_length = urb->actual_length;
-       return status;
+       return urb->status; /* must read status after completion */
 }
 
 static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm,
@@ -495,7 +482,9 @@ static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm,
        int rbuflen = ((rsize - 1) / stride + 1) * CMD_PACKET_SIZE;
 
        if (wbuflen > PAGE_SIZE || rbuflen > PAGE_SIZE) {
-               dbg("too big transfer requested");
+               if (printk_ratelimit())
+                       usb_err(instance->usbatm, "requested transfer size too large (%d, %d)\n",
+                               wbuflen, rbuflen);
                ret = -ENOMEM;
                goto fail;
        }
@@ -506,8 +495,9 @@ static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm,
        init_completion(&instance->rcv_done);
        ret = usb_submit_urb(instance->rcv_urb, GFP_KERNEL);
        if (ret < 0) {
-               dbg("submitting read urb for cm %#x failed", cm);
-               ret = ret;
+               if (printk_ratelimit())
+                       usb_err(instance->usbatm, "submit of read urb for cm %#x failed (%d)\n",
+                               cm, ret);
                goto fail;
        }
 
@@ -523,27 +513,29 @@ static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm,
        init_completion(&instance->snd_done);
        ret = usb_submit_urb(instance->snd_urb, GFP_KERNEL);
        if (ret < 0) {
-               dbg("submitting write urb for cm %#x failed", cm);
-               ret = ret;
+               if (printk_ratelimit())
+                       usb_err(instance->usbatm, "submit of write urb for cm %#x failed (%d)\n",
+                               cm, ret);
                goto fail;
        }
 
        ret = cxacru_start_wait_urb(instance->snd_urb, &instance->snd_done, NULL);
        if (ret < 0) {
-               dbg("sending cm %#x failed", cm);
-               ret = ret;
+               if (printk_ratelimit())
+                       usb_err(instance->usbatm, "send of cm %#x failed (%d)\n", cm, ret);
                goto fail;
        }
 
        ret = cxacru_start_wait_urb(instance->rcv_urb, &instance->rcv_done, &actlen);
        if (ret < 0) {
-               dbg("receiving cm %#x failed", cm);
-               ret = ret;
+               if (printk_ratelimit())
+                       usb_err(instance->usbatm, "receive of cm %#x failed (%d)\n", cm, ret);
                goto fail;
        }
        if (actlen % CMD_PACKET_SIZE || !actlen) {
-               dbg("response is not a positive multiple of %d: %#x",
-                               CMD_PACKET_SIZE, actlen);
+               if (printk_ratelimit())
+                       usb_err(instance->usbatm, "invalid response length to cm %#x: %d\n",
+                               cm, actlen);
                ret = -EIO;
                goto fail;
        }
@@ -551,12 +543,16 @@ static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm,
        /* check the return status and copy the data to the output buffer, if needed */
        for (offb = offd = 0; offd < rsize && offb < actlen; offb += CMD_PACKET_SIZE) {
                if (rbuf[offb] != cm) {
-                       dbg("wrong cm %#x in response", rbuf[offb]);
+                       if (printk_ratelimit())
+                               usb_err(instance->usbatm, "wrong cm %#x in response to cm %#x\n",
+                                       rbuf[offb], cm);
                        ret = -EIO;
                        goto fail;
                }
                if (rbuf[offb + 1] != CM_STATUS_SUCCESS) {
-                       dbg("response failed: %#x", rbuf[offb + 1]);
+                       if (printk_ratelimit())
+                               usb_err(instance->usbatm, "response to cm %#x failed: %#x\n",
+                                       cm, rbuf[offb + 1]);
                        ret = -EIO;
                        goto fail;
                }
@@ -595,14 +591,18 @@ static int cxacru_cm_get_array(struct cxacru_data *instance, enum cxacru_cm_requ
        for (offb = 0; offb < len; ) {
                int l = le32_to_cpu(buf[offb++]);
                if (l > stride || l > (len - offb) / 2) {
-                       dbg("wrong data length %#x in response", l);
+                       if (printk_ratelimit())
+                               usb_err(instance->usbatm, "invalid data length from cm %#x: %d\n",
+                                       cm, l);
                        ret = -EIO;
                        goto cleanup;
                }
                while (l--) {
                        offd = le32_to_cpu(buf[offb++]);
                        if (offd >= size) {
-                               dbg("wrong index %#x in response", offd);
+                               if (printk_ratelimit())
+                                       usb_err(instance->usbatm, "wrong index #%x in response to cm #%x\n",
+                                               offd, cm);
                                ret = -EIO;
                                goto cleanup;
                        }