LCOV - code coverage report
Current view: top level - drivers/hid/usbhid - hid-core.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 664 0.0 %
Date: 2014-02-18 Functions: 0 46 0.0 %
Branches: 0 440 0.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  *  USB HID support for Linux
       3                 :            :  *
       4                 :            :  *  Copyright (c) 1999 Andreas Gal
       5                 :            :  *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
       6                 :            :  *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
       7                 :            :  *  Copyright (c) 2007-2008 Oliver Neukum
       8                 :            :  *  Copyright (c) 2006-2010 Jiri Kosina
       9                 :            :  */
      10                 :            : 
      11                 :            : /*
      12                 :            :  * This program is free software; you can redistribute it and/or modify it
      13                 :            :  * under the terms of the GNU General Public License as published by the Free
      14                 :            :  * Software Foundation; either version 2 of the License, or (at your option)
      15                 :            :  * any later version.
      16                 :            :  */
      17                 :            : 
      18                 :            : #include <linux/module.h>
      19                 :            : #include <linux/slab.h>
      20                 :            : #include <linux/init.h>
      21                 :            : #include <linux/kernel.h>
      22                 :            : #include <linux/list.h>
      23                 :            : #include <linux/mm.h>
      24                 :            : #include <linux/mutex.h>
      25                 :            : #include <linux/spinlock.h>
      26                 :            : #include <asm/unaligned.h>
      27                 :            : #include <asm/byteorder.h>
      28                 :            : #include <linux/input.h>
      29                 :            : #include <linux/wait.h>
      30                 :            : #include <linux/workqueue.h>
      31                 :            : #include <linux/string.h>
      32                 :            : 
      33                 :            : #include <linux/usb.h>
      34                 :            : 
      35                 :            : #include <linux/hid.h>
      36                 :            : #include <linux/hiddev.h>
      37                 :            : #include <linux/hid-debug.h>
      38                 :            : #include <linux/hidraw.h>
      39                 :            : #include "usbhid.h"
      40                 :            : 
      41                 :            : /*
      42                 :            :  * Version Information
      43                 :            :  */
      44                 :            : 
      45                 :            : #define DRIVER_DESC "USB HID core driver"
      46                 :            : #define DRIVER_LICENSE "GPL"
      47                 :            : 
      48                 :            : /*
      49                 :            :  * Module parameters.
      50                 :            :  */
      51                 :            : 
      52                 :            : static unsigned int hid_mousepoll_interval;
      53                 :            : module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
      54                 :            : MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
      55                 :            : 
      56                 :            : static unsigned int ignoreled;
      57                 :            : module_param_named(ignoreled, ignoreled, uint, 0644);
      58                 :            : MODULE_PARM_DESC(ignoreled, "Autosuspend with active leds");
      59                 :            : 
      60                 :            : /* Quirks specified at module load time */
      61                 :            : static char *quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL };
      62                 :            : module_param_array_named(quirks, quirks_param, charp, NULL, 0444);
      63                 :            : MODULE_PARM_DESC(quirks, "Add/modify USB HID quirks by specifying "
      64                 :            :                 " quirks=vendorID:productID:quirks"
      65                 :            :                 " where vendorID, productID, and quirks are all in"
      66                 :            :                 " 0x-prefixed hex");
      67                 :            : /*
      68                 :            :  * Input submission and I/O error handler.
      69                 :            :  */
      70                 :            : static DEFINE_MUTEX(hid_open_mut);
      71                 :            : 
      72                 :            : static void hid_io_error(struct hid_device *hid);
      73                 :            : static int hid_submit_out(struct hid_device *hid);
      74                 :            : static int hid_submit_ctrl(struct hid_device *hid);
      75                 :            : static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid);
      76                 :            : 
      77                 :            : /* Start up the input URB */
      78                 :          0 : static int hid_start_in(struct hid_device *hid)
      79                 :            : {
      80                 :            :         unsigned long flags;
      81                 :            :         int rc = 0;
      82                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
      83                 :            : 
      84                 :          0 :         spin_lock_irqsave(&usbhid->lock, flags);
      85 [ #  # ][ #  # ]:          0 :         if (hid->open > 0 &&
      86         [ #  # ]:          0 :                         !test_bit(HID_DISCONNECTED, &usbhid->iofl) &&
      87         [ #  # ]:          0 :                         !test_bit(HID_SUSPENDED, &usbhid->iofl) &&
      88                 :          0 :                         !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
      89                 :          0 :                 rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
      90         [ #  # ]:          0 :                 if (rc != 0) {
      91                 :          0 :                         clear_bit(HID_IN_RUNNING, &usbhid->iofl);
      92         [ #  # ]:          0 :                         if (rc == -ENOSPC)
      93                 :          0 :                                 set_bit(HID_NO_BANDWIDTH, &usbhid->iofl);
      94                 :            :                 } else {
      95                 :          0 :                         clear_bit(HID_NO_BANDWIDTH, &usbhid->iofl);
      96                 :            :                 }
      97                 :            :         }
      98                 :            :         spin_unlock_irqrestore(&usbhid->lock, flags);
      99                 :          0 :         return rc;
     100                 :            : }
     101                 :            : 
     102                 :            : /* I/O retry timer routine */
     103                 :          0 : static void hid_retry_timeout(unsigned long _hid)
     104                 :            : {
     105                 :          0 :         struct hid_device *hid = (struct hid_device *) _hid;
     106                 :            :         struct usbhid_device *usbhid = hid->driver_data;
     107                 :            : 
     108                 :            :         dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
     109         [ #  # ]:          0 :         if (hid_start_in(hid))
     110                 :          0 :                 hid_io_error(hid);
     111                 :          0 : }
     112                 :            : 
     113                 :            : /* Workqueue routine to reset the device or clear a halt */
     114                 :          0 : static void hid_reset(struct work_struct *work)
     115                 :            : {
     116                 :            :         struct usbhid_device *usbhid =
     117                 :            :                 container_of(work, struct usbhid_device, reset_work);
     118                 :          0 :         struct hid_device *hid = usbhid->hid;
     119                 :            :         int rc = 0;
     120                 :            : 
     121         [ #  # ]:          0 :         if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
     122                 :            :                 dev_dbg(&usbhid->intf->dev, "clear halt\n");
     123                 :          0 :                 rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
     124                 :          0 :                 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
     125                 :          0 :                 hid_start_in(hid);
     126                 :            :         }
     127                 :            : 
     128         [ #  # ]:          0 :         else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
     129                 :            :                 dev_dbg(&usbhid->intf->dev, "resetting device\n");
     130                 :          0 :                 rc = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
     131         [ #  # ]:          0 :                 if (rc == 0) {
     132                 :          0 :                         rc = usb_reset_device(hid_to_usb_dev(hid));
     133                 :          0 :                         usb_unlock_device(hid_to_usb_dev(hid));
     134                 :            :                 }
     135                 :          0 :                 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
     136                 :            :         }
     137                 :            : 
     138      [ #  #  # ]:          0 :         switch (rc) {
     139                 :            :         case 0:
     140         [ #  # ]:          0 :                 if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
     141                 :          0 :                         hid_io_error(hid);
     142                 :            :                 break;
     143                 :            :         default:
     144                 :          0 :                 hid_err(hid, "can't reset device, %s-%s/input%d, status %d\n",
     145                 :            :                         hid_to_usb_dev(hid)->bus->bus_name,
     146                 :            :                         hid_to_usb_dev(hid)->devpath,
     147                 :            :                         usbhid->ifnum, rc);
     148                 :            :                 /* FALLTHROUGH */
     149                 :            :         case -EHOSTUNREACH:
     150                 :            :         case -ENODEV:
     151                 :            :         case -EINTR:
     152                 :            :                 break;
     153                 :            :         }
     154                 :          0 : }
     155                 :            : 
     156                 :            : /* Main I/O error handler */
     157                 :          0 : static void hid_io_error(struct hid_device *hid)
     158                 :            : {
     159                 :            :         unsigned long flags;
     160                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
     161                 :            : 
     162                 :          0 :         spin_lock_irqsave(&usbhid->lock, flags);
     163                 :            : 
     164                 :            :         /* Stop when disconnected */
     165         [ #  # ]:          0 :         if (test_bit(HID_DISCONNECTED, &usbhid->iofl))
     166                 :            :                 goto done;
     167                 :            : 
     168                 :            :         /* If it has been a while since the last error, we'll assume
     169                 :            :          * this a brand new error and reset the retry timeout. */
     170         [ #  # ]:          0 :         if (time_after(jiffies, usbhid->stop_retry + HZ/2))
     171                 :          0 :                 usbhid->retry_delay = 0;
     172                 :            : 
     173                 :            :         /* When an error occurs, retry at increasing intervals */
     174         [ #  # ]:          0 :         if (usbhid->retry_delay == 0) {
     175                 :          0 :                 usbhid->retry_delay = 13;    /* Then 26, 52, 104, 104, ... */
     176                 :          0 :                 usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
     177         [ #  # ]:          0 :         } else if (usbhid->retry_delay < 100)
     178                 :          0 :                 usbhid->retry_delay *= 2;
     179                 :            : 
     180         [ #  # ]:          0 :         if (time_after(jiffies, usbhid->stop_retry)) {
     181                 :            : 
     182                 :            :                 /* Retries failed, so do a port reset unless we lack bandwidth*/
     183         [ #  # ]:          0 :                 if (test_bit(HID_NO_BANDWIDTH, &usbhid->iofl)
     184         [ #  # ]:          0 :                      && !test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
     185                 :            : 
     186                 :          0 :                         schedule_work(&usbhid->reset_work);
     187                 :            :                         goto done;
     188                 :            :                 }
     189                 :            :         }
     190                 :            : 
     191                 :          0 :         mod_timer(&usbhid->io_retry,
     192                 :          0 :                         jiffies + msecs_to_jiffies(usbhid->retry_delay));
     193                 :            : done:
     194                 :            :         spin_unlock_irqrestore(&usbhid->lock, flags);
     195                 :          0 : }
     196                 :            : 
     197                 :            : static void usbhid_mark_busy(struct usbhid_device *usbhid)
     198                 :            : {
     199                 :            :         struct usb_interface *intf = usbhid->intf;
     200                 :            : 
     201                 :            :         usb_mark_last_busy(interface_to_usbdev(intf));
     202                 :            : }
     203                 :            : 
     204                 :          0 : static int usbhid_restart_out_queue(struct usbhid_device *usbhid)
     205                 :            : {
     206                 :          0 :         struct hid_device *hid = usb_get_intfdata(usbhid->intf);
     207                 :            :         int kicked;
     208                 :            :         int r;
     209                 :            : 
     210 [ #  # ][ #  # ]:          0 :         if (!hid || test_bit(HID_RESET_PENDING, &usbhid->iofl) ||
                 [ #  # ]
     211                 :            :                         test_bit(HID_SUSPENDED, &usbhid->iofl))
     212                 :            :                 return 0;
     213                 :            : 
     214         [ #  # ]:          0 :         if ((kicked = (usbhid->outhead != usbhid->outtail))) {
     215                 :            :                 hid_dbg(hid, "Kicking head %d tail %d", usbhid->outhead, usbhid->outtail);
     216                 :            : 
     217                 :            :                 /* Try to wake up from autosuspend... */
     218                 :            :                 r = usb_autopm_get_interface_async(usbhid->intf);
     219                 :            :                 if (r < 0)
     220                 :            :                         return r;
     221                 :            : 
     222                 :            :                 /*
     223                 :            :                  * If still suspended, don't submit.  Submission will
     224                 :            :                  * occur if/when resume drains the queue.
     225                 :            :                  */
     226         [ #  # ]:          0 :                 if (test_bit(HID_SUSPENDED, &usbhid->iofl)) {
     227                 :            :                         usb_autopm_put_interface_no_suspend(usbhid->intf);
     228                 :            :                         return r;
     229                 :            :                 }
     230                 :            : 
     231                 :            :                 /* Asynchronously flush queue. */
     232                 :          0 :                 set_bit(HID_OUT_RUNNING, &usbhid->iofl);
     233         [ #  # ]:          0 :                 if (hid_submit_out(hid)) {
     234                 :          0 :                         clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
     235                 :            :                         usb_autopm_put_interface_async(usbhid->intf);
     236                 :            :                 }
     237                 :          0 :                 wake_up(&usbhid->wait);
     238                 :            :         }
     239                 :          0 :         return kicked;
     240                 :            : }
     241                 :            : 
     242                 :          0 : static int usbhid_restart_ctrl_queue(struct usbhid_device *usbhid)
     243                 :            : {
     244                 :          0 :         struct hid_device *hid = usb_get_intfdata(usbhid->intf);
     245                 :            :         int kicked;
     246                 :            :         int r;
     247                 :            : 
     248         [ #  # ]:          0 :         WARN_ON(hid == NULL);
     249 [ #  # ][ #  # ]:          0 :         if (!hid || test_bit(HID_RESET_PENDING, &usbhid->iofl) ||
                 [ #  # ]
     250                 :            :                         test_bit(HID_SUSPENDED, &usbhid->iofl))
     251                 :            :                 return 0;
     252                 :            : 
     253         [ #  # ]:          0 :         if ((kicked = (usbhid->ctrlhead != usbhid->ctrltail))) {
     254                 :            :                 hid_dbg(hid, "Kicking head %d tail %d", usbhid->ctrlhead, usbhid->ctrltail);
     255                 :            : 
     256                 :            :                 /* Try to wake up from autosuspend... */
     257                 :            :                 r = usb_autopm_get_interface_async(usbhid->intf);
     258                 :            :                 if (r < 0)
     259                 :            :                         return r;
     260                 :            : 
     261                 :            :                 /*
     262                 :            :                  * If still suspended, don't submit.  Submission will
     263                 :            :                  * occur if/when resume drains the queue.
     264                 :            :                  */
     265         [ #  # ]:          0 :                 if (test_bit(HID_SUSPENDED, &usbhid->iofl)) {
     266                 :            :                         usb_autopm_put_interface_no_suspend(usbhid->intf);
     267                 :            :                         return r;
     268                 :            :                 }
     269                 :            : 
     270                 :            :                 /* Asynchronously flush queue. */
     271                 :          0 :                 set_bit(HID_CTRL_RUNNING, &usbhid->iofl);
     272         [ #  # ]:          0 :                 if (hid_submit_ctrl(hid)) {
     273                 :          0 :                         clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
     274                 :            :                         usb_autopm_put_interface_async(usbhid->intf);
     275                 :            :                 }
     276                 :          0 :                 wake_up(&usbhid->wait);
     277                 :            :         }
     278                 :          0 :         return kicked;
     279                 :            : }
     280                 :            : 
     281                 :            : /*
     282                 :            :  * Input interrupt completion handler.
     283                 :            :  */
     284                 :            : 
     285                 :          0 : static void hid_irq_in(struct urb *urb)
     286                 :            : {
     287                 :          0 :         struct hid_device       *hid = urb->context;
     288                 :          0 :         struct usbhid_device    *usbhid = hid->driver_data;
     289                 :            :         int                     status;
     290                 :            : 
     291   [ #  #  #  #  :          0 :         switch (urb->status) {
                      # ]
     292                 :            :         case 0:                 /* success */
     293                 :            :                 usbhid_mark_busy(usbhid);
     294                 :          0 :                 usbhid->retry_delay = 0;
     295                 :          0 :                 hid_input_report(urb->context, HID_INPUT_REPORT,
     296                 :          0 :                                  urb->transfer_buffer,
     297                 :          0 :                                  urb->actual_length, 1);
     298                 :            :                 /*
     299                 :            :                  * autosuspend refused while keys are pressed
     300                 :            :                  * because most keyboards don't wake up when
     301                 :            :                  * a key is released
     302                 :            :                  */
     303         [ #  # ]:          0 :                 if (hid_check_keys_pressed(hid))
     304                 :          0 :                         set_bit(HID_KEYS_PRESSED, &usbhid->iofl);
     305                 :            :                 else
     306                 :          0 :                         clear_bit(HID_KEYS_PRESSED, &usbhid->iofl);
     307                 :            :                 break;
     308                 :            :         case -EPIPE:            /* stall */
     309                 :            :                 usbhid_mark_busy(usbhid);
     310                 :          0 :                 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
     311                 :          0 :                 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
     312                 :          0 :                 schedule_work(&usbhid->reset_work);
     313                 :            :                 return;
     314                 :            :         case -ECONNRESET:       /* unlink */
     315                 :            :         case -ENOENT:
     316                 :            :         case -ESHUTDOWN:        /* unplug */
     317                 :          0 :                 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
     318                 :          0 :                 return;
     319                 :            :         case -EILSEQ:           /* protocol error or unplug */
     320                 :            :         case -EPROTO:           /* protocol error or unplug */
     321                 :            :         case -ETIME:            /* protocol error or unplug */
     322                 :            :         case -ETIMEDOUT:        /* Should never happen, but... */
     323                 :            :                 usbhid_mark_busy(usbhid);
     324                 :          0 :                 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
     325                 :          0 :                 hid_io_error(hid);
     326                 :          0 :                 return;
     327                 :            :         default:                /* error */
     328                 :          0 :                 hid_warn(urb->dev, "input irq status %d received\n",
     329                 :            :                          urb->status);
     330                 :            :         }
     331                 :            : 
     332                 :          0 :         status = usb_submit_urb(urb, GFP_ATOMIC);
     333         [ #  # ]:          0 :         if (status) {
     334                 :          0 :                 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
     335         [ #  # ]:          0 :                 if (status != -EPERM) {
     336                 :          0 :                         hid_err(hid, "can't resubmit intr, %s-%s/input%d, status %d\n",
     337                 :            :                                 hid_to_usb_dev(hid)->bus->bus_name,
     338                 :            :                                 hid_to_usb_dev(hid)->devpath,
     339                 :            :                                 usbhid->ifnum, status);
     340                 :          0 :                         hid_io_error(hid);
     341                 :            :                 }
     342                 :            :         }
     343                 :            : }
     344                 :            : 
     345                 :          0 : static int hid_submit_out(struct hid_device *hid)
     346                 :            : {
     347                 :            :         struct hid_report *report;
     348                 :            :         char *raw_report;
     349                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
     350                 :            :         int r;
     351                 :            : 
     352                 :          0 :         report = usbhid->out[usbhid->outtail].report;
     353                 :          0 :         raw_report = usbhid->out[usbhid->outtail].raw_report;
     354                 :            : 
     355                 :          0 :         usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) +
     356                 :          0 :                                                  1 + (report->id > 0);
     357                 :          0 :         usbhid->urbout->dev = hid_to_usb_dev(hid);
     358         [ #  # ]:          0 :         if (raw_report) {
     359                 :          0 :                 memcpy(usbhid->outbuf, raw_report,
     360                 :          0 :                                 usbhid->urbout->transfer_buffer_length);
     361                 :          0 :                 kfree(raw_report);
     362                 :          0 :                 usbhid->out[usbhid->outtail].raw_report = NULL;
     363                 :            :         }
     364                 :            : 
     365         [ #  # ]:          0 :         dbg_hid("submitting out urb\n");
     366                 :            : 
     367                 :          0 :         r = usb_submit_urb(usbhid->urbout, GFP_ATOMIC);
     368         [ #  # ]:          0 :         if (r < 0) {
     369                 :          0 :                 hid_err(hid, "usb_submit_urb(out) failed: %d\n", r);
     370                 :          0 :                 return r;
     371                 :            :         }
     372                 :          0 :         usbhid->last_out = jiffies;
     373                 :          0 :         return 0;
     374                 :            : }
     375                 :            : 
     376                 :          0 : static int hid_submit_ctrl(struct hid_device *hid)
     377                 :            : {
     378                 :            :         struct hid_report *report;
     379                 :            :         unsigned char dir;
     380                 :            :         char *raw_report;
     381                 :            :         int len, r;
     382                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
     383                 :            : 
     384                 :          0 :         report = usbhid->ctrl[usbhid->ctrltail].report;
     385                 :          0 :         raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
     386                 :          0 :         dir = usbhid->ctrl[usbhid->ctrltail].dir;
     387                 :            : 
     388                 :          0 :         len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
     389         [ #  # ]:          0 :         if (dir == USB_DIR_OUT) {
     390                 :          0 :                 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
     391                 :          0 :                 usbhid->urbctrl->transfer_buffer_length = len;
     392         [ #  # ]:          0 :                 if (raw_report) {
     393                 :          0 :                         memcpy(usbhid->ctrlbuf, raw_report, len);
     394                 :          0 :                         kfree(raw_report);
     395                 :          0 :                         usbhid->ctrl[usbhid->ctrltail].raw_report = NULL;
     396                 :            :                 }
     397                 :            :         } else {
     398                 :            :                 int maxpacket, padlen;
     399                 :            : 
     400                 :          0 :                 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
     401                 :          0 :                 maxpacket = usb_maxpacket(hid_to_usb_dev(hid),
     402                 :          0 :                                           usbhid->urbctrl->pipe, 0);
     403         [ #  # ]:          0 :                 if (maxpacket > 0) {
     404                 :          0 :                         padlen = DIV_ROUND_UP(len, maxpacket);
     405                 :          0 :                         padlen *= maxpacket;
     406         [ #  # ]:          0 :                         if (padlen > usbhid->bufsize)
     407                 :          0 :                                 padlen = usbhid->bufsize;
     408                 :            :                 } else
     409                 :            :                         padlen = 0;
     410                 :          0 :                 usbhid->urbctrl->transfer_buffer_length = padlen;
     411                 :            :         }
     412                 :          0 :         usbhid->urbctrl->dev = hid_to_usb_dev(hid);
     413                 :            : 
     414                 :          0 :         usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
     415         [ #  # ]:          0 :         usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT :
     416                 :            :                                                       HID_REQ_GET_REPORT;
     417                 :          0 :         usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) |
     418                 :            :                                          report->id);
     419                 :          0 :         usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
     420                 :          0 :         usbhid->cr->wLength = cpu_to_le16(len);
     421                 :            : 
     422 [ #  # ][ #  # ]:          0 :         dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
     423                 :            :                 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" :
     424                 :            :                                                              "Get_Report",
     425                 :            :                 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
     426                 :            : 
     427                 :          0 :         r = usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC);
     428         [ #  # ]:          0 :         if (r < 0) {
     429                 :          0 :                 hid_err(hid, "usb_submit_urb(ctrl) failed: %d\n", r);
     430                 :          0 :                 return r;
     431                 :            :         }
     432                 :          0 :         usbhid->last_ctrl = jiffies;
     433                 :          0 :         return 0;
     434                 :            : }
     435                 :            : 
     436                 :            : /*
     437                 :            :  * Output interrupt completion handler.
     438                 :            :  */
     439                 :            : 
     440                 :          0 : static void hid_irq_out(struct urb *urb)
     441                 :            : {
     442                 :          0 :         struct hid_device *hid = urb->context;
     443                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
     444                 :            :         unsigned long flags;
     445                 :            :         int unplug = 0;
     446                 :            : 
     447      [ #  #  # ]:          0 :         switch (urb->status) {
     448                 :            :         case 0:                 /* success */
     449                 :            :                 break;
     450                 :            :         case -ESHUTDOWN:        /* unplug */
     451                 :            :                 unplug = 1;
     452                 :            :         case -EILSEQ:           /* protocol error or unplug */
     453                 :            :         case -EPROTO:           /* protocol error or unplug */
     454                 :            :         case -ECONNRESET:       /* unlink */
     455                 :            :         case -ENOENT:
     456                 :            :                 break;
     457                 :            :         default:                /* error */
     458                 :          0 :                 hid_warn(urb->dev, "output irq status %d received\n",
     459                 :            :                          urb->status);
     460                 :            :         }
     461                 :            : 
     462                 :          0 :         spin_lock_irqsave(&usbhid->lock, flags);
     463                 :            : 
     464         [ #  # ]:          0 :         if (unplug) {
     465                 :          0 :                 usbhid->outtail = usbhid->outhead;
     466                 :            :         } else {
     467                 :          0 :                 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
     468                 :            : 
     469   [ #  #  #  # ]:          0 :                 if (usbhid->outhead != usbhid->outtail &&
     470                 :          0 :                                 hid_submit_out(hid) == 0) {
     471                 :            :                         /* Successfully submitted next urb in queue */
     472                 :            :                         spin_unlock_irqrestore(&usbhid->lock, flags);
     473                 :          0 :                         return;
     474                 :            :                 }
     475                 :            :         }
     476                 :            : 
     477                 :          0 :         clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
     478                 :            :         spin_unlock_irqrestore(&usbhid->lock, flags);
     479                 :            :         usb_autopm_put_interface_async(usbhid->intf);
     480                 :          0 :         wake_up(&usbhid->wait);
     481                 :            : }
     482                 :            : 
     483                 :            : /*
     484                 :            :  * Control pipe completion handler.
     485                 :            :  */
     486                 :            : 
     487                 :          0 : static void hid_ctrl(struct urb *urb)
     488                 :            : {
     489                 :          0 :         struct hid_device *hid = urb->context;
     490                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
     491                 :          0 :         int unplug = 0, status = urb->status;
     492                 :            : 
     493                 :            :         spin_lock(&usbhid->lock);
     494                 :            : 
     495   [ #  #  #  # ]:          0 :         switch (status) {
     496                 :            :         case 0:                 /* success */
     497         [ #  # ]:          0 :                 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
     498                 :          0 :                         hid_input_report(urb->context,
     499                 :          0 :                                 usbhid->ctrl[usbhid->ctrltail].report->type,
     500                 :          0 :                                 urb->transfer_buffer, urb->actual_length, 0);
     501                 :            :                 break;
     502                 :            :         case -ESHUTDOWN:        /* unplug */
     503                 :            :                 unplug = 1;
     504                 :            :         case -EILSEQ:           /* protocol error or unplug */
     505                 :            :         case -EPROTO:           /* protocol error or unplug */
     506                 :            :         case -ECONNRESET:       /* unlink */
     507                 :            :         case -ENOENT:
     508                 :            :         case -EPIPE:            /* report not available */
     509                 :            :                 break;
     510                 :            :         default:                /* error */
     511                 :          0 :                 hid_warn(urb->dev, "ctrl urb status %d received\n", status);
     512                 :            :         }
     513                 :            : 
     514         [ #  # ]:          0 :         if (unplug) {
     515                 :          0 :                 usbhid->ctrltail = usbhid->ctrlhead;
     516                 :            :         } else {
     517                 :          0 :                 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
     518                 :            : 
     519   [ #  #  #  # ]:          0 :                 if (usbhid->ctrlhead != usbhid->ctrltail &&
     520                 :          0 :                                 hid_submit_ctrl(hid) == 0) {
     521                 :            :                         /* Successfully submitted next urb in queue */
     522                 :            :                         spin_unlock(&usbhid->lock);
     523                 :          0 :                         return;
     524                 :            :                 }
     525                 :            :         }
     526                 :            : 
     527                 :          0 :         clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
     528                 :            :         spin_unlock(&usbhid->lock);
     529                 :            :         usb_autopm_put_interface_async(usbhid->intf);
     530                 :          0 :         wake_up(&usbhid->wait);
     531                 :            : }
     532                 :            : 
     533                 :          0 : static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report,
     534                 :            :                                    unsigned char dir)
     535                 :            : {
     536                 :            :         int head;
     537                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
     538                 :            : 
     539 [ #  # ][ #  # ]:          0 :         if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
     540                 :            :                 return;
     541                 :            : 
     542 [ #  # ][ #  # ]:          0 :         if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
                 [ #  # ]
     543         [ #  # ]:          0 :                 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
     544                 :          0 :                         hid_warn(hid, "output queue full\n");
     545                 :          0 :                         return;
     546                 :            :                 }
     547                 :            : 
     548                 :          0 :                 usbhid->out[usbhid->outhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC);
     549         [ #  # ]:          0 :                 if (!usbhid->out[usbhid->outhead].raw_report) {
     550                 :          0 :                         hid_warn(hid, "output queueing failed\n");
     551                 :          0 :                         return;
     552                 :            :                 }
     553                 :          0 :                 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
     554                 :          0 :                 usbhid->out[usbhid->outhead].report = report;
     555                 :          0 :                 usbhid->outhead = head;
     556                 :            : 
     557                 :            :                 /* If the queue isn't running, restart it */
     558         [ #  # ]:          0 :                 if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl)) {
     559                 :          0 :                         usbhid_restart_out_queue(usbhid);
     560                 :            : 
     561                 :            :                 /* Otherwise see if an earlier request has timed out */
     562         [ #  # ]:          0 :                 } else if (time_after(jiffies, usbhid->last_out + HZ * 5)) {
     563                 :            : 
     564                 :            :                         /* Prevent autosuspend following the unlink */
     565                 :            :                         usb_autopm_get_interface_no_resume(usbhid->intf);
     566                 :            : 
     567                 :            :                         /*
     568                 :            :                          * Prevent resubmission in case the URB completes
     569                 :            :                          * before we can unlink it.  We don't want to cancel
     570                 :            :                          * the wrong transfer!
     571                 :            :                          */
     572                 :          0 :                         usb_block_urb(usbhid->urbout);
     573                 :            : 
     574                 :            :                         /* Drop lock to avoid deadlock if the callback runs */
     575                 :            :                         spin_unlock(&usbhid->lock);
     576                 :            : 
     577                 :          0 :                         usb_unlink_urb(usbhid->urbout);
     578                 :            :                         spin_lock(&usbhid->lock);
     579                 :          0 :                         usb_unblock_urb(usbhid->urbout);
     580                 :            : 
     581                 :            :                         /* Unlink might have stopped the queue */
     582         [ #  # ]:          0 :                         if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl))
     583                 :          0 :                                 usbhid_restart_out_queue(usbhid);
     584                 :            : 
     585                 :            :                         /* Now we can allow autosuspend again */
     586                 :            :                         usb_autopm_put_interface_async(usbhid->intf);
     587                 :            :                 }
     588                 :            :                 return;
     589                 :            :         }
     590                 :            : 
     591         [ #  # ]:          0 :         if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
     592                 :          0 :                 hid_warn(hid, "control queue full\n");
     593                 :          0 :                 return;
     594                 :            :         }
     595                 :            : 
     596         [ #  # ]:          0 :         if (dir == USB_DIR_OUT) {
     597                 :          0 :                 usbhid->ctrl[usbhid->ctrlhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC);
     598         [ #  # ]:          0 :                 if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
     599                 :          0 :                         hid_warn(hid, "control queueing failed\n");
     600                 :          0 :                         return;
     601                 :            :                 }
     602                 :          0 :                 hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
     603                 :            :         }
     604                 :          0 :         usbhid->ctrl[usbhid->ctrlhead].report = report;
     605                 :          0 :         usbhid->ctrl[usbhid->ctrlhead].dir = dir;
     606                 :          0 :         usbhid->ctrlhead = head;
     607                 :            : 
     608                 :            :         /* If the queue isn't running, restart it */
     609         [ #  # ]:          0 :         if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl)) {
     610                 :          0 :                 usbhid_restart_ctrl_queue(usbhid);
     611                 :            : 
     612                 :            :         /* Otherwise see if an earlier request has timed out */
     613         [ #  # ]:          0 :         } else if (time_after(jiffies, usbhid->last_ctrl + HZ * 5)) {
     614                 :            : 
     615                 :            :                 /* Prevent autosuspend following the unlink */
     616                 :            :                 usb_autopm_get_interface_no_resume(usbhid->intf);
     617                 :            : 
     618                 :            :                 /*
     619                 :            :                  * Prevent resubmission in case the URB completes
     620                 :            :                  * before we can unlink it.  We don't want to cancel
     621                 :            :                  * the wrong transfer!
     622                 :            :                  */
     623                 :          0 :                 usb_block_urb(usbhid->urbctrl);
     624                 :            : 
     625                 :            :                 /* Drop lock to avoid deadlock if the callback runs */
     626                 :            :                 spin_unlock(&usbhid->lock);
     627                 :            : 
     628                 :          0 :                 usb_unlink_urb(usbhid->urbctrl);
     629                 :            :                 spin_lock(&usbhid->lock);
     630                 :          0 :                 usb_unblock_urb(usbhid->urbctrl);
     631                 :            : 
     632                 :            :                 /* Unlink might have stopped the queue */
     633         [ #  # ]:          0 :                 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
     634                 :          0 :                         usbhid_restart_ctrl_queue(usbhid);
     635                 :            : 
     636                 :            :                 /* Now we can allow autosuspend again */
     637                 :            :                 usb_autopm_put_interface_async(usbhid->intf);
     638                 :            :         }
     639                 :            : }
     640                 :            : 
     641                 :          0 : static void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
     642                 :            : {
     643                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
     644                 :            :         unsigned long flags;
     645                 :            : 
     646                 :          0 :         spin_lock_irqsave(&usbhid->lock, flags);
     647                 :          0 :         __usbhid_submit_report(hid, report, dir);
     648                 :            :         spin_unlock_irqrestore(&usbhid->lock, flags);
     649                 :          0 : }
     650                 :            : 
     651                 :          0 : static int usbhid_wait_io(struct hid_device *hid)
     652                 :            : {
     653                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
     654                 :            : 
     655 [ #  # ][ #  # ]:          0 :         if (!wait_event_timeout(usbhid->wait,
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     656                 :            :                                 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
     657                 :            :                                 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
     658                 :            :                                         10*HZ)) {
     659         [ #  # ]:          0 :                 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
     660                 :            :                 return -1;
     661                 :            :         }
     662                 :            : 
     663                 :            :         return 0;
     664                 :            : }
     665                 :            : 
     666                 :          0 : static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
     667                 :            : {
     668                 :          0 :         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
     669                 :          0 :                 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
     670                 :            :                 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
     671                 :            : }
     672                 :            : 
     673                 :          0 : static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
     674                 :            :                 unsigned char type, void *buf, int size)
     675                 :            : {
     676                 :            :         int result, retries = 4;
     677                 :            : 
     678         [ #  # ]:          0 :         memset(buf, 0, size);
     679                 :            : 
     680                 :            :         do {
     681                 :          0 :                 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
     682                 :            :                                 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
     683                 :            :                                 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
     684                 :          0 :                 retries--;
     685         [ #  # ]:          0 :         } while (result < size && retries);
     686                 :          0 :         return result;
     687                 :            : }
     688                 :            : 
     689                 :          0 : int usbhid_open(struct hid_device *hid)
     690                 :            : {
     691                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
     692                 :            :         int res = 0;
     693                 :            : 
     694                 :          0 :         mutex_lock(&hid_open_mut);
     695         [ #  # ]:          0 :         if (!hid->open++) {
     696                 :            :                 res = usb_autopm_get_interface(usbhid->intf);
     697                 :            :                 /* the device must be awake to reliably request remote wakeup */
     698                 :            :                 if (res < 0) {
     699                 :            :                         hid->open--;
     700                 :            :                         res = -EIO;
     701                 :            :                         goto done;
     702                 :            :                 }
     703                 :          0 :                 usbhid->intf->needs_remote_wakeup = 1;
     704                 :          0 :                 res = hid_start_in(hid);
     705         [ #  # ]:          0 :                 if (res) {
     706         [ #  # ]:          0 :                         if (res != -ENOSPC) {
     707                 :          0 :                                 hid_io_error(hid);
     708                 :            :                                 res = 0;
     709                 :            :                         } else {
     710                 :            :                                 /* no use opening if resources are insufficient */
     711                 :          0 :                                 hid->open--;
     712                 :            :                                 res = -EBUSY;
     713                 :          0 :                                 usbhid->intf->needs_remote_wakeup = 0;
     714                 :            :                         }
     715                 :            :                 }
     716                 :            :                 usb_autopm_put_interface(usbhid->intf);
     717                 :            :         }
     718                 :            : done:
     719                 :          0 :         mutex_unlock(&hid_open_mut);
     720                 :          0 :         return res;
     721                 :            : }
     722                 :            : 
     723                 :          0 : void usbhid_close(struct hid_device *hid)
     724                 :            : {
     725                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
     726                 :            : 
     727                 :          0 :         mutex_lock(&hid_open_mut);
     728                 :            : 
     729                 :            :         /* protecting hid->open to make sure we don't restart
     730                 :            :          * data acquistion due to a resumption we no longer
     731                 :            :          * care about
     732                 :            :          */
     733                 :            :         spin_lock_irq(&usbhid->lock);
     734         [ #  # ]:          0 :         if (!--hid->open) {
     735                 :            :                 spin_unlock_irq(&usbhid->lock);
     736                 :            :                 hid_cancel_delayed_stuff(usbhid);
     737                 :          0 :                 usb_kill_urb(usbhid->urbin);
     738                 :          0 :                 usbhid->intf->needs_remote_wakeup = 0;
     739                 :            :         } else {
     740                 :            :                 spin_unlock_irq(&usbhid->lock);
     741                 :            :         }
     742                 :          0 :         mutex_unlock(&hid_open_mut);
     743                 :          0 : }
     744                 :            : 
     745                 :            : /*
     746                 :            :  * Initialize all reports
     747                 :            :  */
     748                 :            : 
     749                 :          0 : void usbhid_init_reports(struct hid_device *hid)
     750                 :            : {
     751                 :            :         struct hid_report *report;
     752                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
     753                 :            :         struct hid_report_enum *report_enum;
     754                 :            :         int err, ret;
     755                 :            : 
     756         [ #  # ]:          0 :         if (!(hid->quirks & HID_QUIRK_NO_INIT_INPUT_REPORTS)) {
     757                 :            :                 report_enum = &hid->report_enum[HID_INPUT_REPORT];
     758         [ #  # ]:          0 :                 list_for_each_entry(report, &report_enum->report_list, list)
     759                 :          0 :                         usbhid_submit_report(hid, report, USB_DIR_IN);
     760                 :            :         }
     761                 :            : 
     762                 :            :         report_enum = &hid->report_enum[HID_FEATURE_REPORT];
     763         [ #  # ]:          0 :         list_for_each_entry(report, &report_enum->report_list, list)
     764                 :          0 :                 usbhid_submit_report(hid, report, USB_DIR_IN);
     765                 :            : 
     766                 :            :         err = 0;
     767                 :          0 :         ret = usbhid_wait_io(hid);
     768         [ #  # ]:          0 :         while (ret) {
     769                 :          0 :                 err |= ret;
     770         [ #  # ]:          0 :                 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
     771                 :          0 :                         usb_kill_urb(usbhid->urbctrl);
     772         [ #  # ]:          0 :                 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
     773                 :          0 :                         usb_kill_urb(usbhid->urbout);
     774                 :          0 :                 ret = usbhid_wait_io(hid);
     775                 :            :         }
     776                 :            : 
     777         [ #  # ]:          0 :         if (err)
     778                 :          0 :                 hid_warn(hid, "timeout initializing reports\n");
     779                 :          0 : }
     780                 :            : 
     781                 :            : /*
     782                 :            :  * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
     783                 :            :  */
     784                 :          0 : static int hid_find_field_early(struct hid_device *hid, unsigned int page,
     785                 :            :     unsigned int hid_code, struct hid_field **pfield)
     786                 :            : {
     787                 :            :         struct hid_report *report;
     788                 :            :         struct hid_field *field;
     789                 :            :         struct hid_usage *usage;
     790                 :            :         int i, j;
     791                 :            : 
     792         [ #  # ]:          0 :         list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
     793         [ #  # ]:          0 :                 for (i = 0; i < report->maxfield; i++) {
     794                 :          0 :                         field = report->field[i];
     795         [ #  # ]:          0 :                         for (j = 0; j < field->maxusage; j++) {
     796                 :          0 :                                 usage = &field->usage[j];
     797 [ #  # ][ #  # ]:          0 :                                 if ((usage->hid & HID_USAGE_PAGE) == page &&
     798                 :          0 :                                     (usage->hid & 0xFFFF) == hid_code) {
     799                 :          0 :                                         *pfield = field;
     800                 :          0 :                                         return j;
     801                 :            :                                 }
     802                 :            :                         }
     803                 :            :                 }
     804                 :            :         }
     805                 :            :         return -1;
     806                 :            : }
     807                 :            : 
     808                 :          0 : static void usbhid_set_leds(struct hid_device *hid)
     809                 :            : {
     810                 :            :         struct hid_field *field;
     811                 :            :         int offset;
     812                 :            : 
     813         [ #  # ]:          0 :         if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
     814                 :          0 :                 hid_set_field(field, offset, 0);
     815                 :          0 :                 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
     816                 :            :         }
     817                 :          0 : }
     818                 :            : 
     819                 :            : /*
     820                 :            :  * Traverse the supplied list of reports and find the longest
     821                 :            :  */
     822                 :            : static void hid_find_max_report(struct hid_device *hid, unsigned int type,
     823                 :            :                 unsigned int *max)
     824                 :            : {
     825                 :            :         struct hid_report *report;
     826                 :            :         unsigned int size;
     827                 :            : 
     828 [ #  # ][ #  # ]:          0 :         list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
         [ #  # ][ #  # ]
     829                 :          0 :                 size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
     830 [ #  # ][ #  # ]:          0 :                 if (*max < size)
         [ #  # ][ #  # ]
     831                 :          0 :                         *max = size;
     832                 :            :         }
     833                 :            : }
     834                 :            : 
     835                 :          0 : static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
     836                 :            : {
     837                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
     838                 :            : 
     839                 :          0 :         usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
     840                 :            :                         &usbhid->inbuf_dma);
     841                 :          0 :         usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
     842                 :            :                         &usbhid->outbuf_dma);
     843                 :          0 :         usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL);
     844                 :          0 :         usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
     845                 :            :                         &usbhid->ctrlbuf_dma);
     846 [ #  # ][ #  # ]:          0 :         if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
         [ #  # ][ #  # ]
     847                 :            :                         !usbhid->ctrlbuf)
     848                 :            :                 return -1;
     849                 :            : 
     850                 :            :         return 0;
     851                 :            : }
     852                 :            : 
     853                 :          0 : static int usbhid_get_raw_report(struct hid_device *hid,
     854                 :            :                 unsigned char report_number, __u8 *buf, size_t count,
     855                 :            :                 unsigned char report_type)
     856                 :            : {
     857                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
     858                 :          0 :         struct usb_device *dev = hid_to_usb_dev(hid);
     859                 :          0 :         struct usb_interface *intf = usbhid->intf;
     860                 :          0 :         struct usb_host_interface *interface = intf->cur_altsetting;
     861                 :            :         int skipped_report_id = 0;
     862                 :            :         int ret;
     863                 :            : 
     864                 :            :         /* Byte 0 is the report number. Report data starts at byte 1.*/
     865                 :          0 :         buf[0] = report_number;
     866         [ #  # ]:          0 :         if (report_number == 0x0) {
     867                 :            :                 /* Offset the return buffer by 1, so that the report ID
     868                 :            :                    will remain in byte 0. */
     869                 :          0 :                 buf++;
     870                 :          0 :                 count--;
     871                 :            :                 skipped_report_id = 1;
     872                 :            :         }
     873                 :          0 :         ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
     874                 :            :                 HID_REQ_GET_REPORT,
     875                 :            :                 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
     876                 :          0 :                 ((report_type + 1) << 8) | report_number,
     877                 :          0 :                 interface->desc.bInterfaceNumber, buf, count,
     878                 :            :                 USB_CTRL_SET_TIMEOUT);
     879                 :            : 
     880                 :            :         /* count also the report id */
     881         [ #  # ]:          0 :         if (ret > 0 && skipped_report_id)
     882                 :          0 :                 ret++;
     883                 :            : 
     884                 :          0 :         return ret;
     885                 :            : }
     886                 :            : 
     887                 :          0 : static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count,
     888                 :            :                 unsigned char report_type)
     889                 :            : {
     890                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
     891                 :          0 :         struct usb_device *dev = hid_to_usb_dev(hid);
     892                 :          0 :         struct usb_interface *intf = usbhid->intf;
     893                 :          0 :         struct usb_host_interface *interface = intf->cur_altsetting;
     894                 :            :         int ret;
     895                 :            : 
     896 [ #  # ][ #  # ]:          0 :         if (usbhid->urbout && report_type != HID_FEATURE_REPORT) {
     897                 :            :                 int actual_length;
     898                 :            :                 int skipped_report_id = 0;
     899                 :            : 
     900         [ #  # ]:          0 :                 if (buf[0] == 0x0) {
     901                 :            :                         /* Don't send the Report ID */
     902                 :          0 :                         buf++;
     903                 :          0 :                         count--;
     904                 :            :                         skipped_report_id = 1;
     905                 :            :                 }
     906                 :          0 :                 ret = usb_interrupt_msg(dev, usbhid->urbout->pipe,
     907                 :            :                         buf, count, &actual_length,
     908                 :            :                         USB_CTRL_SET_TIMEOUT);
     909                 :            :                 /* return the number of bytes transferred */
     910         [ #  # ]:          0 :                 if (ret == 0) {
     911                 :          0 :                         ret = actual_length;
     912                 :            :                         /* count also the report id */
     913         [ #  # ]:          0 :                         if (skipped_report_id)
     914                 :          0 :                                 ret++;
     915                 :            :                 }
     916                 :            :         } else {
     917                 :            :                 int skipped_report_id = 0;
     918                 :          0 :                 int report_id = buf[0];
     919         [ #  # ]:          0 :                 if (buf[0] == 0x0) {
     920                 :            :                         /* Don't send the Report ID */
     921                 :          0 :                         buf++;
     922                 :          0 :                         count--;
     923                 :            :                         skipped_report_id = 1;
     924                 :            :                 }
     925                 :          0 :                 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
     926                 :            :                         HID_REQ_SET_REPORT,
     927                 :            :                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
     928                 :          0 :                         ((report_type + 1) << 8) | report_id,
     929                 :          0 :                         interface->desc.bInterfaceNumber, buf, count,
     930                 :            :                         USB_CTRL_SET_TIMEOUT);
     931                 :            :                 /* count also the report id, if this was a numbered report. */
     932         [ #  # ]:          0 :                 if (ret > 0 && skipped_report_id)
     933                 :          0 :                         ret++;
     934                 :            :         }
     935                 :            : 
     936                 :          0 :         return ret;
     937                 :            : }
     938                 :            : 
     939                 :          0 : static void usbhid_restart_queues(struct usbhid_device *usbhid)
     940                 :            : {
     941 [ #  # ][ #  # ]:          0 :         if (usbhid->urbout && !test_bit(HID_OUT_RUNNING, &usbhid->iofl))
     942                 :          0 :                 usbhid_restart_out_queue(usbhid);
     943         [ #  # ]:          0 :         if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
     944                 :          0 :                 usbhid_restart_ctrl_queue(usbhid);
     945                 :          0 : }
     946                 :            : 
     947                 :          0 : static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
     948                 :            : {
     949                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
     950                 :            : 
     951                 :          0 :         usb_free_coherent(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
     952                 :          0 :         usb_free_coherent(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
     953                 :          0 :         kfree(usbhid->cr);
     954                 :          0 :         usb_free_coherent(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
     955                 :          0 : }
     956                 :            : 
     957                 :          0 : static int usbhid_parse(struct hid_device *hid)
     958                 :            : {
     959                 :          0 :         struct usb_interface *intf = to_usb_interface(hid->dev.parent);
     960                 :          0 :         struct usb_host_interface *interface = intf->cur_altsetting;
     961                 :          0 :         struct usb_device *dev = interface_to_usbdev (intf);
     962                 :            :         struct hid_descriptor *hdesc;
     963                 :            :         u32 quirks = 0;
     964                 :            :         unsigned int rsize = 0;
     965                 :            :         char *rdesc;
     966                 :            :         int ret, n;
     967                 :            : 
     968                 :          0 :         quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
     969                 :            :                         le16_to_cpu(dev->descriptor.idProduct));
     970                 :            : 
     971         [ #  # ]:          0 :         if (quirks & HID_QUIRK_IGNORE)
     972                 :            :                 return -ENODEV;
     973                 :            : 
     974                 :            :         /* Many keyboards and mice don't like to be polled for reports,
     975                 :            :          * so we will always set the HID_QUIRK_NOGET flag for them. */
     976         [ #  # ]:          0 :         if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
     977         [ #  # ]:          0 :                 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
     978                 :            :                         interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
     979                 :          0 :                                 quirks |= HID_QUIRK_NOGET;
     980                 :            :         }
     981                 :            : 
     982 [ #  # ][ #  # ]:          0 :         if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
     983         [ #  # ]:          0 :             (!interface->desc.bNumEndpoints ||
     984                 :          0 :              usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
     985         [ #  # ]:          0 :                 dbg_hid("class descriptor not present\n");
     986                 :            :                 return -ENODEV;
     987                 :            :         }
     988                 :            : 
     989                 :          0 :         hid->version = le16_to_cpu(hdesc->bcdHID);
     990                 :          0 :         hid->country = hdesc->bCountryCode;
     991                 :            : 
     992         [ #  # ]:          0 :         for (n = 0; n < hdesc->bNumDescriptors; n++)
     993         [ #  # ]:          0 :                 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
     994                 :          0 :                         rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
     995                 :            : 
     996         [ #  # ]:          0 :         if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
     997         [ #  # ]:          0 :                 dbg_hid("weird size of report descriptor (%u)\n", rsize);
     998                 :            :                 return -EINVAL;
     999                 :            :         }
    1000                 :            : 
    1001         [ #  # ]:          0 :         if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
    1002         [ #  # ]:          0 :                 dbg_hid("couldn't allocate rdesc memory\n");
    1003                 :            :                 return -ENOMEM;
    1004                 :            :         }
    1005                 :            : 
    1006                 :          0 :         hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
    1007                 :            : 
    1008                 :          0 :         ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
    1009                 :            :                         HID_DT_REPORT, rdesc, rsize);
    1010         [ #  # ]:          0 :         if (ret < 0) {
    1011         [ #  # ]:          0 :                 dbg_hid("reading report descriptor failed\n");
    1012                 :          0 :                 kfree(rdesc);
    1013                 :          0 :                 goto err;
    1014                 :            :         }
    1015                 :            : 
    1016                 :          0 :         ret = hid_parse_report(hid, rdesc, rsize);
    1017                 :          0 :         kfree(rdesc);
    1018         [ #  # ]:          0 :         if (ret) {
    1019         [ #  # ]:          0 :                 dbg_hid("parsing report descriptor failed\n");
    1020                 :            :                 goto err;
    1021                 :            :         }
    1022                 :            : 
    1023                 :          0 :         hid->quirks |= quirks;
    1024                 :            : 
    1025                 :          0 :         return 0;
    1026                 :            : err:
    1027                 :          0 :         return ret;
    1028                 :            : }
    1029                 :            : 
    1030                 :          0 : static int usbhid_start(struct hid_device *hid)
    1031                 :            : {
    1032                 :          0 :         struct usb_interface *intf = to_usb_interface(hid->dev.parent);
    1033                 :          0 :         struct usb_host_interface *interface = intf->cur_altsetting;
    1034                 :          0 :         struct usb_device *dev = interface_to_usbdev(intf);
    1035                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
    1036                 :            :         unsigned int n, insize = 0;
    1037                 :            :         int ret;
    1038                 :            : 
    1039                 :          0 :         clear_bit(HID_DISCONNECTED, &usbhid->iofl);
    1040                 :            : 
    1041                 :          0 :         usbhid->bufsize = HID_MIN_BUFFER_SIZE;
    1042                 :            :         hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
    1043                 :            :         hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
    1044                 :            :         hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
    1045                 :            : 
    1046         [ #  # ]:          0 :         if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
    1047                 :          0 :                 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
    1048                 :            : 
    1049                 :            :         hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
    1050                 :            : 
    1051         [ #  # ]:          0 :         if (insize > HID_MAX_BUFFER_SIZE)
    1052                 :            :                 insize = HID_MAX_BUFFER_SIZE;
    1053                 :            : 
    1054         [ #  # ]:          0 :         if (hid_alloc_buffers(dev, hid)) {
    1055                 :            :                 ret = -ENOMEM;
    1056                 :            :                 goto fail;
    1057                 :            :         }
    1058                 :            : 
    1059         [ #  # ]:          0 :         for (n = 0; n < interface->desc.bNumEndpoints; n++) {
    1060                 :          0 :                 struct usb_endpoint_descriptor *endpoint;
    1061                 :            :                 int pipe;
    1062                 :            :                 int interval;
    1063                 :            : 
    1064                 :          0 :                 endpoint = &interface->endpoint[n].desc;
    1065         [ #  # ]:          0 :                 if (!usb_endpoint_xfer_int(endpoint))
    1066                 :          0 :                         continue;
    1067                 :            : 
    1068                 :          0 :                 interval = endpoint->bInterval;
    1069                 :            : 
    1070                 :            :                 /* Some vendors give fullspeed interval on highspeed devides */
    1071 [ #  # ][ #  # ]:          0 :                 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
    1072                 :          0 :                     dev->speed == USB_SPEED_HIGH) {
    1073                 :          0 :                         interval = fls(endpoint->bInterval*8);
    1074                 :          0 :                         printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
    1075                 :          0 :                                hid->name, endpoint->bInterval, interval);
    1076                 :            :                 }
    1077                 :            : 
    1078                 :            :                 /* Change the polling interval of mice. */
    1079 [ #  # ][ #  # ]:          0 :                 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
    1080                 :          0 :                         interval = hid_mousepoll_interval;
    1081                 :            : 
    1082                 :            :                 ret = -ENOMEM;
    1083         [ #  # ]:          0 :                 if (usb_endpoint_dir_in(endpoint)) {
    1084         [ #  # ]:          0 :                         if (usbhid->urbin)
    1085                 :          0 :                                 continue;
    1086         [ #  # ]:          0 :                         if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
    1087                 :            :                                 goto fail;
    1088                 :          0 :                         pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
    1089                 :          0 :                         usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
    1090                 :            :                                          hid_irq_in, hid, interval);
    1091                 :          0 :                         usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
    1092                 :          0 :                         usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
    1093                 :            :                 } else {
    1094         [ #  # ]:          0 :                         if (usbhid->urbout)
    1095                 :          0 :                                 continue;
    1096         [ #  # ]:          0 :                         if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
    1097                 :            :                                 goto fail;
    1098                 :          0 :                         pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
    1099                 :          0 :                         usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
    1100                 :            :                                          hid_irq_out, hid, interval);
    1101                 :          0 :                         usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
    1102                 :          0 :                         usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
    1103                 :            :                 }
    1104                 :            :         }
    1105                 :            : 
    1106                 :          0 :         usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
    1107         [ #  # ]:          0 :         if (!usbhid->urbctrl) {
    1108                 :            :                 ret = -ENOMEM;
    1109                 :            :                 goto fail;
    1110                 :            :         }
    1111                 :            : 
    1112                 :          0 :         usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
    1113                 :          0 :                              usbhid->ctrlbuf, 1, hid_ctrl, hid);
    1114                 :          0 :         usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
    1115                 :          0 :         usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
    1116                 :            : 
    1117         [ #  # ]:          0 :         if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
    1118                 :          0 :                 usbhid_init_reports(hid);
    1119                 :            : 
    1120                 :          0 :         set_bit(HID_STARTED, &usbhid->iofl);
    1121                 :            : 
    1122                 :            :         /* Some keyboards don't work until their LEDs have been set.
    1123                 :            :          * Since BIOSes do set the LEDs, it must be safe for any device
    1124                 :            :          * that supports the keyboard boot protocol.
    1125                 :            :          * In addition, enable remote wakeup by default for all keyboard
    1126                 :            :          * devices supporting the boot protocol.
    1127                 :            :          */
    1128         [ #  # ]:          0 :         if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
    1129                 :            :                         interface->desc.bInterfaceProtocol ==
    1130                 :            :                                 USB_INTERFACE_PROTOCOL_KEYBOARD) {
    1131                 :          0 :                 usbhid_set_leds(hid);
    1132                 :          0 :                 device_set_wakeup_enable(&dev->dev, 1);
    1133                 :            :         }
    1134                 :            :         return 0;
    1135                 :            : 
    1136                 :            : fail:
    1137                 :          0 :         usb_free_urb(usbhid->urbin);
    1138                 :          0 :         usb_free_urb(usbhid->urbout);
    1139                 :          0 :         usb_free_urb(usbhid->urbctrl);
    1140                 :          0 :         usbhid->urbin = NULL;
    1141                 :          0 :         usbhid->urbout = NULL;
    1142                 :          0 :         usbhid->urbctrl = NULL;
    1143                 :          0 :         hid_free_buffers(dev, hid);
    1144                 :          0 :         return ret;
    1145                 :            : }
    1146                 :            : 
    1147                 :          0 : static void usbhid_stop(struct hid_device *hid)
    1148                 :            : {
    1149                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
    1150                 :            : 
    1151 [ #  # ][ #  # ]:          0 :         if (WARN_ON(!usbhid))
    1152                 :          0 :                 return;
    1153                 :            : 
    1154                 :          0 :         clear_bit(HID_STARTED, &usbhid->iofl);
    1155                 :            :         spin_lock_irq(&usbhid->lock);    /* Sync with error and led handlers */
    1156                 :          0 :         set_bit(HID_DISCONNECTED, &usbhid->iofl);
    1157                 :            :         spin_unlock_irq(&usbhid->lock);
    1158                 :          0 :         usb_kill_urb(usbhid->urbin);
    1159                 :          0 :         usb_kill_urb(usbhid->urbout);
    1160                 :          0 :         usb_kill_urb(usbhid->urbctrl);
    1161                 :            : 
    1162                 :            :         hid_cancel_delayed_stuff(usbhid);
    1163                 :            : 
    1164                 :          0 :         hid->claimed = 0;
    1165                 :            : 
    1166                 :          0 :         usb_free_urb(usbhid->urbin);
    1167                 :          0 :         usb_free_urb(usbhid->urbctrl);
    1168                 :          0 :         usb_free_urb(usbhid->urbout);
    1169                 :          0 :         usbhid->urbin = NULL; /* don't mess up next start */
    1170                 :          0 :         usbhid->urbctrl = NULL;
    1171                 :          0 :         usbhid->urbout = NULL;
    1172                 :            : 
    1173                 :          0 :         hid_free_buffers(hid_to_usb_dev(hid), hid);
    1174                 :            : }
    1175                 :            : 
    1176                 :          0 : static int usbhid_power(struct hid_device *hid, int lvl)
    1177                 :            : {
    1178                 :            :         int r = 0;
    1179                 :            : 
    1180                 :            :         switch (lvl) {
    1181                 :            :         case PM_HINT_FULLON:
    1182                 :            :                 r = usbhid_get_power(hid);
    1183                 :            :                 break;
    1184                 :            :         case PM_HINT_NORMAL:
    1185                 :            :                 usbhid_put_power(hid);
    1186                 :            :                 break;
    1187                 :            :         }
    1188                 :          0 :         return r;
    1189                 :            : }
    1190                 :            : 
    1191                 :          0 : static void usbhid_request(struct hid_device *hid, struct hid_report *rep, int reqtype)
    1192                 :            : {
    1193      [ #  #  # ]:          0 :         switch (reqtype) {
    1194                 :            :         case HID_REQ_GET_REPORT:
    1195                 :          0 :                 usbhid_submit_report(hid, rep, USB_DIR_IN);
    1196                 :          0 :                 break;
    1197                 :            :         case HID_REQ_SET_REPORT:
    1198                 :          0 :                 usbhid_submit_report(hid, rep, USB_DIR_OUT);
    1199                 :          0 :                 break;
    1200                 :            :         }
    1201                 :          0 : }
    1202                 :            : 
    1203                 :          0 : static int usbhid_idle(struct hid_device *hid, int report, int idle,
    1204                 :            :                 int reqtype)
    1205                 :            : {
    1206                 :          0 :         struct usb_device *dev = hid_to_usb_dev(hid);
    1207                 :            :         struct usb_interface *intf = to_usb_interface(hid->dev.parent);
    1208                 :          0 :         struct usb_host_interface *interface = intf->cur_altsetting;
    1209                 :          0 :         int ifnum = interface->desc.bInterfaceNumber;
    1210                 :            : 
    1211         [ #  # ]:          0 :         if (reqtype != HID_REQ_SET_IDLE)
    1212                 :            :                 return -EINVAL;
    1213                 :            : 
    1214                 :          0 :         return hid_set_idle(dev, ifnum, report, idle);
    1215                 :            : }
    1216                 :            : 
    1217                 :            : static struct hid_ll_driver usb_hid_driver = {
    1218                 :            :         .parse = usbhid_parse,
    1219                 :            :         .start = usbhid_start,
    1220                 :            :         .stop = usbhid_stop,
    1221                 :            :         .open = usbhid_open,
    1222                 :            :         .close = usbhid_close,
    1223                 :            :         .power = usbhid_power,
    1224                 :            :         .request = usbhid_request,
    1225                 :            :         .wait = usbhid_wait_io,
    1226                 :            :         .idle = usbhid_idle,
    1227                 :            : };
    1228                 :            : 
    1229                 :          0 : static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
    1230                 :            : {
    1231                 :          0 :         struct usb_host_interface *interface = intf->cur_altsetting;
    1232                 :          0 :         struct usb_device *dev = interface_to_usbdev(intf);
    1233                 :            :         struct usbhid_device *usbhid;
    1234                 :            :         struct hid_device *hid;
    1235                 :            :         unsigned int n, has_in = 0;
    1236                 :            :         size_t len;
    1237                 :            :         int ret;
    1238                 :            : 
    1239         [ #  # ]:          0 :         dbg_hid("HID probe called for ifnum %d\n",
    1240                 :            :                         intf->altsetting->desc.bInterfaceNumber);
    1241                 :            : 
    1242         [ #  # ]:          0 :         for (n = 0; n < interface->desc.bNumEndpoints; n++)
    1243         [ #  # ]:          0 :                 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
    1244                 :          0 :                         has_in++;
    1245         [ #  # ]:          0 :         if (!has_in) {
    1246                 :          0 :                 hid_err(intf, "couldn't find an input interrupt endpoint\n");
    1247                 :          0 :                 return -ENODEV;
    1248                 :            :         }
    1249                 :            : 
    1250                 :          0 :         hid = hid_allocate_device();
    1251         [ #  # ]:          0 :         if (IS_ERR(hid))
    1252                 :          0 :                 return PTR_ERR(hid);
    1253                 :            : 
    1254                 :            :         usb_set_intfdata(intf, hid);
    1255                 :          0 :         hid->ll_driver = &usb_hid_driver;
    1256                 :          0 :         hid->hid_get_raw_report = usbhid_get_raw_report;
    1257                 :          0 :         hid->hid_output_raw_report = usbhid_output_raw_report;
    1258                 :          0 :         hid->ff_init = hid_pidff_init;
    1259                 :            : #ifdef CONFIG_USB_HIDDEV
    1260                 :            :         hid->hiddev_connect = hiddev_connect;
    1261                 :            :         hid->hiddev_disconnect = hiddev_disconnect;
    1262                 :            :         hid->hiddev_hid_event = hiddev_hid_event;
    1263                 :            :         hid->hiddev_report_event = hiddev_report_event;
    1264                 :            : #endif
    1265                 :          0 :         hid->dev.parent = &intf->dev;
    1266                 :          0 :         hid->bus = BUS_USB;
    1267                 :          0 :         hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
    1268                 :          0 :         hid->product = le16_to_cpu(dev->descriptor.idProduct);
    1269                 :          0 :         hid->name[0] = 0;
    1270                 :          0 :         hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
    1271         [ #  # ]:          0 :         if (intf->cur_altsetting->desc.bInterfaceProtocol ==
    1272                 :            :                         USB_INTERFACE_PROTOCOL_MOUSE)
    1273                 :          0 :                 hid->type = HID_TYPE_USBMOUSE;
    1274         [ #  # ]:          0 :         else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
    1275                 :          0 :                 hid->type = HID_TYPE_USBNONE;
    1276                 :            : 
    1277         [ #  # ]:          0 :         if (dev->manufacturer)
    1278                 :          0 :                 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
    1279                 :            : 
    1280         [ #  # ]:          0 :         if (dev->product) {
    1281         [ #  # ]:          0 :                 if (dev->manufacturer)
    1282                 :          0 :                         strlcat(hid->name, " ", sizeof(hid->name));
    1283                 :          0 :                 strlcat(hid->name, dev->product, sizeof(hid->name));
    1284                 :            :         }
    1285                 :            : 
    1286         [ #  # ]:          0 :         if (!strlen(hid->name))
    1287                 :          0 :                 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
    1288                 :          0 :                          le16_to_cpu(dev->descriptor.idVendor),
    1289                 :          0 :                          le16_to_cpu(dev->descriptor.idProduct));
    1290                 :            : 
    1291                 :          0 :         usb_make_path(dev, hid->phys, sizeof(hid->phys));
    1292                 :          0 :         strlcat(hid->phys, "/input", sizeof(hid->phys));
    1293                 :          0 :         len = strlen(hid->phys);
    1294         [ #  # ]:          0 :         if (len < sizeof(hid->phys) - 1)
    1295                 :          0 :                 snprintf(hid->phys + len, sizeof(hid->phys) - len,
    1296                 :          0 :                          "%d", intf->altsetting[0].desc.bInterfaceNumber);
    1297                 :            : 
    1298         [ #  # ]:          0 :         if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
    1299                 :          0 :                 hid->uniq[0] = 0;
    1300                 :            : 
    1301                 :            :         usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
    1302         [ #  # ]:          0 :         if (usbhid == NULL) {
    1303                 :            :                 ret = -ENOMEM;
    1304                 :            :                 goto err;
    1305                 :            :         }
    1306                 :            : 
    1307                 :          0 :         hid->driver_data = usbhid;
    1308                 :          0 :         usbhid->hid = hid;
    1309                 :          0 :         usbhid->intf = intf;
    1310                 :          0 :         usbhid->ifnum = interface->desc.bInterfaceNumber;
    1311                 :            : 
    1312                 :          0 :         init_waitqueue_head(&usbhid->wait);
    1313                 :          0 :         INIT_WORK(&usbhid->reset_work, hid_reset);
    1314                 :          0 :         setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
    1315                 :          0 :         spin_lock_init(&usbhid->lock);
    1316                 :            : 
    1317                 :          0 :         ret = hid_add_device(hid);
    1318         [ #  # ]:          0 :         if (ret) {
    1319         [ #  # ]:          0 :                 if (ret != -ENODEV)
    1320                 :          0 :                         hid_err(intf, "can't add hid device: %d\n", ret);
    1321                 :            :                 goto err_free;
    1322                 :            :         }
    1323                 :            : 
    1324                 :            :         return 0;
    1325                 :            : err_free:
    1326                 :          0 :         kfree(usbhid);
    1327                 :            : err:
    1328                 :          0 :         hid_destroy_device(hid);
    1329                 :          0 :         return ret;
    1330                 :            : }
    1331                 :            : 
    1332                 :          0 : static void usbhid_disconnect(struct usb_interface *intf)
    1333                 :            : {
    1334                 :            :         struct hid_device *hid = usb_get_intfdata(intf);
    1335                 :            :         struct usbhid_device *usbhid;
    1336                 :            : 
    1337 [ #  # ][ #  # ]:          0 :         if (WARN_ON(!hid))
    1338                 :          0 :                 return;
    1339                 :            : 
    1340                 :          0 :         usbhid = hid->driver_data;
    1341                 :          0 :         hid_destroy_device(hid);
    1342                 :          0 :         kfree(usbhid);
    1343                 :            : }
    1344                 :            : 
    1345                 :            : static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
    1346                 :            : {
    1347                 :          0 :         del_timer_sync(&usbhid->io_retry);
    1348                 :          0 :         cancel_work_sync(&usbhid->reset_work);
    1349                 :            : }
    1350                 :            : 
    1351                 :          0 : static void hid_cease_io(struct usbhid_device *usbhid)
    1352                 :            : {
    1353                 :          0 :         del_timer_sync(&usbhid->io_retry);
    1354                 :          0 :         usb_kill_urb(usbhid->urbin);
    1355                 :          0 :         usb_kill_urb(usbhid->urbctrl);
    1356                 :          0 :         usb_kill_urb(usbhid->urbout);
    1357                 :          0 : }
    1358                 :            : 
    1359                 :            : /* Treat USB reset pretty much the same as suspend/resume */
    1360                 :          0 : static int hid_pre_reset(struct usb_interface *intf)
    1361                 :            : {
    1362                 :            :         struct hid_device *hid = usb_get_intfdata(intf);
    1363                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
    1364                 :            : 
    1365                 :            :         spin_lock_irq(&usbhid->lock);
    1366                 :          0 :         set_bit(HID_RESET_PENDING, &usbhid->iofl);
    1367                 :            :         spin_unlock_irq(&usbhid->lock);
    1368                 :          0 :         hid_cease_io(usbhid);
    1369                 :            : 
    1370                 :          0 :         return 0;
    1371                 :            : }
    1372                 :            : 
    1373                 :            : /* Same routine used for post_reset and reset_resume */
    1374                 :          0 : static int hid_post_reset(struct usb_interface *intf)
    1375                 :            : {
    1376                 :          0 :         struct usb_device *dev = interface_to_usbdev (intf);
    1377                 :          0 :         struct hid_device *hid = usb_get_intfdata(intf);
    1378                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
    1379                 :          0 :         struct usb_host_interface *interface = intf->cur_altsetting;
    1380                 :            :         int status;
    1381                 :            :         char *rdesc;
    1382                 :            : 
    1383                 :            :         /* Fetch and examine the HID report descriptor. If this
    1384                 :            :          * has changed, then rebind. Since usbcore's check of the
    1385                 :            :          * configuration descriptors passed, we already know that
    1386                 :            :          * the size of the HID report descriptor has not changed.
    1387                 :            :          */
    1388                 :          0 :         rdesc = kmalloc(hid->dev_rsize, GFP_KERNEL);
    1389         [ #  # ]:          0 :         if (!rdesc) {
    1390         [ #  # ]:          0 :                 dbg_hid("couldn't allocate rdesc memory (post_reset)\n");
    1391                 :            :                 return 1;
    1392                 :            :         }
    1393                 :          0 :         status = hid_get_class_descriptor(dev,
    1394                 :          0 :                                 interface->desc.bInterfaceNumber,
    1395                 :          0 :                                 HID_DT_REPORT, rdesc, hid->dev_rsize);
    1396         [ #  # ]:          0 :         if (status < 0) {
    1397         [ #  # ]:          0 :                 dbg_hid("reading report descriptor failed (post_reset)\n");
    1398                 :          0 :                 kfree(rdesc);
    1399                 :          0 :                 return 1;
    1400                 :            :         }
    1401                 :          0 :         status = memcmp(rdesc, hid->dev_rdesc, hid->dev_rsize);
    1402                 :          0 :         kfree(rdesc);
    1403         [ #  # ]:          0 :         if (status != 0) {
    1404         [ #  # ]:          0 :                 dbg_hid("report descriptor changed\n");
    1405                 :            :                 return 1;
    1406                 :            :         }
    1407                 :            : 
    1408                 :            :         spin_lock_irq(&usbhid->lock);
    1409                 :          0 :         clear_bit(HID_RESET_PENDING, &usbhid->iofl);
    1410                 :            :         spin_unlock_irq(&usbhid->lock);
    1411                 :          0 :         hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
    1412                 :          0 :         status = hid_start_in(hid);
    1413         [ #  # ]:          0 :         if (status < 0)
    1414                 :          0 :                 hid_io_error(hid);
    1415                 :          0 :         usbhid_restart_queues(usbhid);
    1416                 :            : 
    1417                 :          0 :         return 0;
    1418                 :            : }
    1419                 :            : 
    1420                 :          0 : int usbhid_get_power(struct hid_device *hid)
    1421                 :            : {
    1422                 :            :         struct usbhid_device *usbhid = hid->driver_data;
    1423                 :            : 
    1424                 :          0 :         return usb_autopm_get_interface(usbhid->intf);
    1425                 :            : }
    1426                 :            : 
    1427                 :          0 : void usbhid_put_power(struct hid_device *hid)
    1428                 :            : {
    1429                 :            :         struct usbhid_device *usbhid = hid->driver_data;
    1430                 :            : 
    1431                 :            :         usb_autopm_put_interface(usbhid->intf);
    1432                 :          0 : }
    1433                 :            : 
    1434                 :            : 
    1435                 :            : #ifdef CONFIG_PM
    1436                 :          0 : static int hid_resume_common(struct hid_device *hid, bool driver_suspended)
    1437                 :            : {
    1438                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
    1439                 :            :         int status;
    1440                 :            : 
    1441                 :            :         spin_lock_irq(&usbhid->lock);
    1442                 :          0 :         clear_bit(HID_SUSPENDED, &usbhid->iofl);
    1443                 :            :         usbhid_mark_busy(usbhid);
    1444                 :            : 
    1445 [ #  # ][ #  # ]:          0 :         if (test_bit(HID_CLEAR_HALT, &usbhid->iofl) ||
    1446                 :            :                         test_bit(HID_RESET_PENDING, &usbhid->iofl))
    1447                 :          0 :                 schedule_work(&usbhid->reset_work);
    1448                 :          0 :         usbhid->retry_delay = 0;
    1449                 :            : 
    1450                 :          0 :         usbhid_restart_queues(usbhid);
    1451                 :            :         spin_unlock_irq(&usbhid->lock);
    1452                 :            : 
    1453                 :          0 :         status = hid_start_in(hid);
    1454         [ #  # ]:          0 :         if (status < 0)
    1455                 :          0 :                 hid_io_error(hid);
    1456                 :            : 
    1457 [ #  # ][ #  # ]:          0 :         if (driver_suspended && hid->driver && hid->driver->resume)
                 [ #  # ]
    1458                 :          0 :                 status = hid->driver->resume(hid);
    1459                 :          0 :         return status;
    1460                 :            : }
    1461                 :            : 
    1462                 :          0 : static int hid_suspend(struct usb_interface *intf, pm_message_t message)
    1463                 :            : {
    1464                 :            :         struct hid_device *hid = usb_get_intfdata(intf);
    1465                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
    1466                 :            :         int status = 0;
    1467                 :            :         bool driver_suspended = false;
    1468                 :            :         unsigned int ledcount;
    1469                 :            : 
    1470         [ #  # ]:          0 :         if (PMSG_IS_AUTO(message)) {
    1471                 :          0 :                 ledcount = hidinput_count_leds(hid);
    1472                 :            :                 spin_lock_irq(&usbhid->lock);    /* Sync with error handler */
    1473         [ #  # ]:          0 :                 if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
    1474         [ #  # ]:          0 :                     && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
    1475         [ #  # ]:          0 :                     && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
    1476         [ #  # ]:          0 :                     && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
    1477         [ #  # ]:          0 :                     && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
    1478 [ #  # ][ #  # ]:          0 :                     && (!ledcount || ignoreled))
    1479                 :            :                 {
    1480                 :          0 :                         set_bit(HID_SUSPENDED, &usbhid->iofl);
    1481                 :            :                         spin_unlock_irq(&usbhid->lock);
    1482 [ #  # ][ #  # ]:          0 :                         if (hid->driver && hid->driver->suspend) {
    1483                 :          0 :                                 status = hid->driver->suspend(hid, message);
    1484         [ #  # ]:          0 :                                 if (status < 0)
    1485                 :            :                                         goto failed;
    1486                 :            :                         }
    1487                 :            :                         driver_suspended = true;
    1488                 :            :                 } else {
    1489                 :            :                         usbhid_mark_busy(usbhid);
    1490                 :            :                         spin_unlock_irq(&usbhid->lock);
    1491                 :          0 :                         return -EBUSY;
    1492                 :            :                 }
    1493                 :            : 
    1494                 :            :         } else {
    1495                 :            :                 /* TODO: resume() might need to handle suspend failure */
    1496 [ #  # ][ #  # ]:          0 :                 if (hid->driver && hid->driver->suspend)
    1497                 :          0 :                         status = hid->driver->suspend(hid, message);
    1498                 :            :                 driver_suspended = true;
    1499                 :            :                 spin_lock_irq(&usbhid->lock);
    1500                 :          0 :                 set_bit(HID_SUSPENDED, &usbhid->iofl);
    1501                 :            :                 spin_unlock_irq(&usbhid->lock);
    1502         [ #  # ]:          0 :                 if (usbhid_wait_io(hid) < 0)
    1503                 :            :                         status = -EIO;
    1504                 :            :         }
    1505                 :            : 
    1506                 :            :         hid_cancel_delayed_stuff(usbhid);
    1507                 :          0 :         hid_cease_io(usbhid);
    1508                 :            : 
    1509 [ #  # ][ #  # ]:          0 :         if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
    1510                 :            :                 /* lost race against keypresses */
    1511                 :            :                 status = -EBUSY;
    1512                 :            :                 goto failed;
    1513                 :            :         }
    1514                 :            :         dev_dbg(&intf->dev, "suspend\n");
    1515                 :            :         return status;
    1516                 :            : 
    1517                 :            :  failed:
    1518                 :          0 :         hid_resume_common(hid, driver_suspended);
    1519                 :          0 :         return status;
    1520                 :            : }
    1521                 :            : 
    1522                 :          0 : static int hid_resume(struct usb_interface *intf)
    1523                 :            : {
    1524                 :            :         struct hid_device *hid = usb_get_intfdata (intf);
    1525                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
    1526                 :            :         int status;
    1527                 :            : 
    1528         [ #  # ]:          0 :         if (!test_bit(HID_STARTED, &usbhid->iofl))
    1529                 :            :                 return 0;
    1530                 :            : 
    1531                 :          0 :         status = hid_resume_common(hid, true);
    1532                 :            :         dev_dbg(&intf->dev, "resume status %d\n", status);
    1533                 :          0 :         return 0;
    1534                 :            : }
    1535                 :            : 
    1536                 :          0 : static int hid_reset_resume(struct usb_interface *intf)
    1537                 :            : {
    1538                 :            :         struct hid_device *hid = usb_get_intfdata(intf);
    1539                 :          0 :         struct usbhid_device *usbhid = hid->driver_data;
    1540                 :            :         int status;
    1541                 :            : 
    1542                 :          0 :         clear_bit(HID_SUSPENDED, &usbhid->iofl);
    1543                 :          0 :         status = hid_post_reset(intf);
    1544 [ #  # ][ #  # ]:          0 :         if (status >= 0 && hid->driver && hid->driver->reset_resume) {
                 [ #  # ]
    1545                 :          0 :                 int ret = hid->driver->reset_resume(hid);
    1546         [ #  # ]:          0 :                 if (ret < 0)
    1547                 :            :                         status = ret;
    1548                 :            :         }
    1549                 :          0 :         return status;
    1550                 :            : }
    1551                 :            : 
    1552                 :            : #endif /* CONFIG_PM */
    1553                 :            : 
    1554                 :            : static const struct usb_device_id hid_usb_ids[] = {
    1555                 :            :         { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
    1556                 :            :                 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
    1557                 :            :         { }                                             /* Terminating entry */
    1558                 :            : };
    1559                 :            : 
    1560                 :            : MODULE_DEVICE_TABLE (usb, hid_usb_ids);
    1561                 :            : 
    1562                 :            : static struct usb_driver hid_driver = {
    1563                 :            :         .name =         "usbhid",
    1564                 :            :         .probe =        usbhid_probe,
    1565                 :            :         .disconnect =   usbhid_disconnect,
    1566                 :            : #ifdef CONFIG_PM
    1567                 :            :         .suspend =      hid_suspend,
    1568                 :            :         .resume =       hid_resume,
    1569                 :            :         .reset_resume = hid_reset_resume,
    1570                 :            : #endif
    1571                 :            :         .pre_reset =    hid_pre_reset,
    1572                 :            :         .post_reset =   hid_post_reset,
    1573                 :            :         .id_table =     hid_usb_ids,
    1574                 :            :         .supports_autosuspend = 1,
    1575                 :            : };
    1576                 :            : 
    1577                 :          0 : struct usb_interface *usbhid_find_interface(int minor)
    1578                 :            : {
    1579                 :          0 :         return usb_find_interface(&hid_driver, minor);
    1580                 :            : }
    1581                 :            : 
    1582                 :          0 : static int __init hid_init(void)
    1583                 :            : {
    1584                 :            :         int retval = -ENOMEM;
    1585                 :            : 
    1586                 :          0 :         retval = usbhid_quirks_init(quirks_param);
    1587         [ #  # ]:          0 :         if (retval)
    1588                 :            :                 goto usbhid_quirks_init_fail;
    1589                 :          0 :         retval = usb_register(&hid_driver);
    1590         [ #  # ]:          0 :         if (retval)
    1591                 :            :                 goto usb_register_fail;
    1592                 :          0 :         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
    1593                 :            : 
    1594                 :          0 :         return 0;
    1595                 :            : usb_register_fail:
    1596                 :          0 :         usbhid_quirks_exit();
    1597                 :            : usbhid_quirks_init_fail:
    1598                 :          0 :         return retval;
    1599                 :            : }
    1600                 :            : 
    1601                 :          0 : static void __exit hid_exit(void)
    1602                 :            : {
    1603                 :          0 :         usb_deregister(&hid_driver);
    1604                 :          0 :         usbhid_quirks_exit();
    1605                 :          0 : }
    1606                 :            : 
    1607                 :            : module_init(hid_init);
    1608                 :            : module_exit(hid_exit);
    1609                 :            : 
    1610                 :            : MODULE_AUTHOR("Andreas Gal");
    1611                 :            : MODULE_AUTHOR("Vojtech Pavlik");
    1612                 :            : MODULE_AUTHOR("Jiri Kosina");
    1613                 :            : MODULE_DESCRIPTION(DRIVER_DESC);
    1614                 :            : MODULE_LICENSE(DRIVER_LICENSE);

Generated by: LCOV version 1.9