usb_keyboard.c 12.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/* Support for the HID Boot Protocol.  */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2008, 2009  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/term.h>
#include <grub/time.h>
#include <grub/misc.h>
#include <grub/term.h>
#include <grub/usb.h>
#include <grub/dl.h>
#include <grub/time.h>
27
#include <grub/keyboard_layouts.h>
28

29 30
GRUB_MOD_LICENSE ("GPLv3+");

31 32


33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
enum
  {
    KEY_NO_KEY = 0x00,
    KEY_ERR_BUFFER = 0x01,
    KEY_ERR_POST  = 0x02,
    KEY_ERR_UNDEF = 0x03,
    KEY_CAPS_LOCK = 0x39,
    KEY_NUM_LOCK  = 0x53,
  };

enum
  {
    LED_NUM_LOCK = 0x01,
    LED_CAPS_LOCK = 0x02
  };
48 49 50 51 52 53 54 55 56

/* Valid values for bRequest.  See HID definition version 1.11 section 7.2. */
#define USB_HID_GET_REPORT	0x01
#define USB_HID_GET_IDLE	0x02
#define USB_HID_GET_PROTOCOL	0x03
#define USB_HID_SET_REPORT	0x09
#define USB_HID_SET_IDLE	0x0A
#define USB_HID_SET_PROTOCOL	0x0B

57 58 59
#define USB_HID_BOOT_SUBCLASS	0x01
#define USB_HID_KBD_PROTOCOL	0x01

60 61 62 63 64 65 66
#define GRUB_USB_KEYBOARD_LEFT_CTRL   0x01
#define GRUB_USB_KEYBOARD_LEFT_SHIFT  0x02
#define GRUB_USB_KEYBOARD_LEFT_ALT    0x04
#define GRUB_USB_KEYBOARD_RIGHT_CTRL  0x10
#define GRUB_USB_KEYBOARD_RIGHT_SHIFT 0x20
#define GRUB_USB_KEYBOARD_RIGHT_ALT   0x40

67 68 69 70
struct grub_usb_keyboard_data
{
  grub_usb_device_t usbdev;
  grub_uint8_t status;
71 72
  grub_uint16_t mods;
  int interfno;
73
  struct grub_usb_desc_endp *endp;
74 75 76
  grub_usb_transfer_t transfer;
  grub_uint8_t report[8];
  int dead;
77 78
  int last_key;
  grub_uint64_t repeat_time;
79 80 81 82
  grub_uint8_t current_report[8];
  grub_uint8_t last_report[8];
  int index;
  int max_index;
83 84
};

85 86 87 88 89 90 91 92 93 94
static int grub_usb_keyboard_getkey (struct grub_term_input *term);
static int grub_usb_keyboard_getkeystatus (struct grub_term_input *term);

static struct grub_term_input grub_usb_keyboard_term =
  {
    .getkey = grub_usb_keyboard_getkey,
    .getkeystatus = grub_usb_keyboard_getkeystatus,
    .next = 0
  };

95 96
static struct grub_term_input grub_usb_keyboards[16];

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
static int
interpret_status (grub_uint8_t data0)
{
  int mods = 0;

  /* Check Shift, Control, and Alt status.  */
  if (data0 & GRUB_USB_KEYBOARD_LEFT_SHIFT)
    mods |= GRUB_TERM_STATUS_LSHIFT;
  if (data0 & GRUB_USB_KEYBOARD_RIGHT_SHIFT)
    mods |= GRUB_TERM_STATUS_RSHIFT;
  if (data0 & GRUB_USB_KEYBOARD_LEFT_CTRL)
    mods |= GRUB_TERM_STATUS_LCTRL;
  if (data0 & GRUB_USB_KEYBOARD_RIGHT_CTRL)
    mods |= GRUB_TERM_STATUS_RCTRL;
  if (data0 & GRUB_USB_KEYBOARD_LEFT_ALT)
    mods |= GRUB_TERM_STATUS_LALT;
  if (data0 & GRUB_USB_KEYBOARD_RIGHT_ALT)
    mods |= GRUB_TERM_STATUS_RALT;

  return mods;
}

119
static void
120 121 122
grub_usb_keyboard_detach (grub_usb_device_t usbdev,
			  int config __attribute__ ((unused)),
			  int interface __attribute__ ((unused)))
123
{
124 125
  unsigned i;
  for (i = 0; i < ARRAY_SIZE (grub_usb_keyboards); i++)
126
    {
127 128 129 130 131 132 133 134
      struct grub_usb_keyboard_data *data = grub_usb_keyboards[i].data;

      if (!data)
	continue;

      if (data->usbdev != usbdev)
	continue;

135 136 137
      if (data->transfer)
	grub_usb_cancel_transfer (data->transfer);

138 139 140 141 142 143
      grub_term_unregister_input (&grub_usb_keyboards[i]);
      grub_free ((char *) grub_usb_keyboards[i].name);
      grub_usb_keyboards[i].name = NULL;
      grub_free (grub_usb_keyboards[i].data);
      grub_usb_keyboards[i].data = 0;
    }
144
}
145

146 147 148 149
static int
grub_usb_keyboard_attach (grub_usb_device_t usbdev, int configno, int interfno)
{
  unsigned curnum;
150
  struct grub_usb_keyboard_data *data;
151 152
  struct grub_usb_desc_endp *endp = NULL;
  int j;
153 154 155 156 157 158 159 160

  grub_dprintf ("usb_keyboard", "%x %x %x %d %d\n",
		usbdev->descdev.class, usbdev->descdev.subclass,
		usbdev->descdev.protocol, configno, interfno);

  for (curnum = 0; curnum < ARRAY_SIZE (grub_usb_keyboards); curnum++)
    if (!grub_usb_keyboards[curnum].data)
      break;
161

162 163
  if (curnum == ARRAY_SIZE (grub_usb_keyboards))
    return 0;
164

165 166 167
  if (usbdev->descdev.class != 0 
      || usbdev->descdev.subclass != 0 || usbdev->descdev.protocol != 0)
    return 0;
168

169 170 171 172 173
  if (usbdev->config[configno].interf[interfno].descif->subclass
      != USB_HID_BOOT_SUBCLASS
      || usbdev->config[configno].interf[interfno].descif->protocol
      != USB_HID_KBD_PROTOCOL)
    return 0;
174

175 176 177 178
  for (j = 0; j < usbdev->config[configno].interf[interfno].descif->endpointcnt;
       j++)
    {
      endp = &usbdev->config[configno].interf[interfno].descendp[j];
179

180 181 182 183 184 185
      if ((endp->endp_addr & 128) && grub_usb_get_ep_type(endp)
	  == GRUB_USB_EP_INTERRUPT)
	break;
    }
  if (j == usbdev->config[configno].interf[interfno].descif->endpointcnt)
    return 0;
186

187
  grub_dprintf ("usb_keyboard", "HID found!\n");
188

189 190 191 192 193
  data = grub_malloc (sizeof (*data));
  if (!data)
    {
      grub_print_error ();
      return 0;
194
    }
195 196

  data->usbdev = usbdev;
197
  data->interfno = interfno;
198
  data->endp = endp;
199

200 201 202
  /* Configure device */
  grub_usb_set_configuration (usbdev, configno + 1);
  
203
  /* Place the device in boot mode.  */
204
  grub_usb_control_msg (usbdev, GRUB_USB_REQTYPE_CLASS_INTERFACE_OUT,
205
  			USB_HID_SET_PROTOCOL, 0, interfno, 0, 0);
206

207
  /* Reports every time an event occurs and not more often than that.  */
208
  grub_usb_control_msg (usbdev, GRUB_USB_REQTYPE_CLASS_INTERFACE_OUT,
209
  			USB_HID_SET_IDLE, 0<<8, interfno, 0, 0);
210 211 212

  grub_memcpy (&grub_usb_keyboards[curnum], &grub_usb_keyboard_term,
	       sizeof (grub_usb_keyboards[curnum]));
213
  grub_usb_keyboards[curnum].data = data;
214 215 216 217
  usbdev->config[configno].interf[interfno].detach_hook
    = grub_usb_keyboard_detach;
  grub_usb_keyboards[curnum].name = grub_xasprintf ("usb_keyboard%d", curnum);
  if (!grub_usb_keyboards[curnum].name)
218 219 220 221
    {
      grub_print_error ();
      return 0;
    }
222

223 224 225 226 227 228
  /* Test showed that getting report may make the keyboard go nuts.
     Moreover since we're reattaching keyboard it usually sends
     an initial message on interrupt pipe and so we retrieve
     the same keystatus.
   */
#if 0
229 230 231 232 233
  {
    grub_uint8_t report[8];
    grub_usb_err_t err;
    grub_memset (report, 0, sizeof (report));
    err = grub_usb_control_msg (usbdev, GRUB_USB_REQTYPE_CLASS_INTERFACE_IN,
234
    				USB_HID_GET_REPORT, 0x0100, interfno,
235 236
				sizeof (report), (char *) report);
    if (err)
237
      data->status = 0;
238
    else
239
      data->status = report[0];
240
  }
241 242 243
#else
  data->status = 0;
#endif
244

245
  data->transfer = grub_usb_bulk_read_background (usbdev,
246
						  data->endp,
247 248 249 250 251 252 253
						  sizeof (data->report),
						  (char *) data->report);
  if (!data->transfer)
    {
      grub_print_error ();
      return 0;
    }
254

255
  data->last_key = -1;
256
  data->mods = 0;
257
  data->dead = 0;
258

259
  grub_term_register_input_active ("usb_keyboard", &grub_usb_keyboards[curnum]);
260

261
  return 1;
262 263 264 265
}



266 267 268 269 270 271
static void
send_leds (struct grub_usb_keyboard_data *termdata)
{
  char report[1];
  report[0] = 0;
  if (termdata->mods & GRUB_TERM_STATUS_CAPS)
272 273 274
    report[0] |= LED_CAPS_LOCK;
  if (termdata->mods & GRUB_TERM_STATUS_NUM)
    report[0] |= LED_NUM_LOCK;
275 276 277 278 279 280
  grub_usb_control_msg (termdata->usbdev, GRUB_USB_REQTYPE_CLASS_INTERFACE_OUT,
			USB_HID_SET_REPORT, 0x0200, termdata->interfno,
			sizeof (report), (char *) report);
  grub_errno = GRUB_ERR_NONE;
}

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
static int
parse_keycode (struct grub_usb_keyboard_data *termdata)
{
  int index = termdata->index;
  int i, keycode;

  /* Sanity check */
  if (index < 2)
    index = 2;

  for ( ; index < termdata->max_index; index++)
    {
      keycode = termdata->current_report[index];
      
      if (keycode == KEY_NO_KEY
          || keycode == KEY_ERR_BUFFER
          || keycode == KEY_ERR_POST
          || keycode == KEY_ERR_UNDEF)
        {
          /* Don't parse (rest of) this report */
          termdata->index = 0;
          if (keycode != KEY_NO_KEY)
          /* Don't replace last report with current faulty report
           * in future ! */
            grub_memcpy (termdata->current_report,
                         termdata->last_report,
                         sizeof (termdata->report));
          return GRUB_TERM_NO_KEY;
        }

      /* Try to find current keycode in last report. */
      for (i = 2; i < 8; i++)
        if (keycode == termdata->last_report[i])
          break;
      if (i < 8)
        /* Keycode is in last report, it means it was not released,
         * ignore it. */
        continue;
        
      if (keycode == KEY_CAPS_LOCK)
        {
          termdata->mods ^= GRUB_TERM_STATUS_CAPS;
          send_leds (termdata);
          continue;
        }

      if (keycode == KEY_NUM_LOCK)
        {
          termdata->mods ^= GRUB_TERM_STATUS_NUM;
          send_leds (termdata);
          continue;
        }

      termdata->last_key = grub_term_map_key (keycode,
                                              interpret_status (termdata->current_report[0])
					        | termdata->mods);
      termdata->repeat_time = grub_get_time_ms () + GRUB_TERM_REPEAT_PRE_INTERVAL;

      grub_errno = GRUB_ERR_NONE;

      index++;
      if (index >= termdata->max_index)
        termdata->index = 0;
      else
        termdata->index = index;

      return termdata->last_key;
    }

  /* All keycodes parsed */
  termdata->index = 0;
  return GRUB_TERM_NO_KEY;
}

355
static int
356
grub_usb_keyboard_getkey (struct grub_term_input *term)
357
{
358 359 360
  grub_usb_err_t err;
  struct grub_usb_keyboard_data *termdata = term->data;
  grub_size_t actual;
361
  int keycode = GRUB_TERM_NO_KEY;
362

363
  if (termdata->dead)
364
    return GRUB_TERM_NO_KEY;
365

366 367 368 369 370
  if (termdata->index)
    keycode = parse_keycode (termdata);
  if (keycode != GRUB_TERM_NO_KEY)
    return keycode;
    
371
  /* Poll interrupt pipe.  */
372 373 374
  err = grub_usb_check_transfer (termdata->transfer, &actual);

  if (err == GRUB_USB_ERR_WAIT)
375 376 377 378 379 380
    {
      if (termdata->last_key != -1
	  && grub_get_time_ms () > termdata->repeat_time)
	{
	  termdata->repeat_time = grub_get_time_ms ()
	    + GRUB_TERM_REPEAT_INTERVAL;
381
	  return termdata->last_key;
382
	}
383
      return GRUB_TERM_NO_KEY;
384
    }
385

386 387 388 389 390 391 392 393
  if (!err && (actual >= 3))
    grub_memcpy (termdata->last_report,
                 termdata->current_report,
                 sizeof (termdata->report));
                 
  grub_memcpy (termdata->current_report,
               termdata->report,
               sizeof (termdata->report));
394 395

  termdata->transfer = grub_usb_bulk_read_background (termdata->usbdev,
396
						      termdata->endp,
397 398 399 400 401 402 403 404
						      sizeof (termdata->report),
						      (char *) termdata->report);
  if (!termdata->transfer)
    {
      grub_printf ("%s failed. Stopped\n", term->name);
      termdata->dead = 1;
    }

405
  termdata->last_key = -1;
406 407

  grub_dprintf ("usb_keyboard",
408 409
		"err = %d, actual = %" PRIuGRUB_SIZE
		" report: 0x%02x 0x%02x 0x%02x 0x%02x"
410 411
		" 0x%02x 0x%02x 0x%02x 0x%02x\n",
		err, actual,
412 413 414 415
		termdata->current_report[0], termdata->current_report[1],
		termdata->current_report[2], termdata->current_report[3],
		termdata->current_report[4], termdata->current_report[5],
		termdata->current_report[6], termdata->current_report[7]);
416

417
  if (err || actual < 1)
418
    return GRUB_TERM_NO_KEY;
419

420
  termdata->status = termdata->current_report[0];
421

422
  if (actual < 3)
423
    return GRUB_TERM_NO_KEY;
424

425 426 427 428
  termdata->index = 2; /* New data received. */
  termdata->max_index = actual;
  
  return parse_keycode (termdata);
429 430
}

431
static int
432
grub_usb_keyboard_getkeystatus (struct grub_term_input *term)
433
{
434
  struct grub_usb_keyboard_data *termdata = term->data;
435

436
  return interpret_status (termdata->status) | termdata->mods;
437 438
}

439
static struct grub_usb_attach_desc attach_hook =
440 441 442 443
{
  .class = GRUB_USB_CLASS_HID,
  .hook = grub_usb_keyboard_attach
};
444 445 446

GRUB_MOD_INIT(usb_keyboard)
{
447
  grub_usb_register_attach_hook_class (&attach_hook);
448 449 450 451
}

GRUB_MOD_FINI(usb_keyboard)
{
452 453 454 455
  unsigned i;
  for (i = 0; i < ARRAY_SIZE (grub_usb_keyboards); i++)
    if (grub_usb_keyboards[i].data)
      {
456 457 458 459 460 461 462 463
	struct grub_usb_keyboard_data *data = grub_usb_keyboards[i].data;

	if (!data)
	  continue;
	
	if (data->transfer)
	  grub_usb_cancel_transfer (data->transfer);

464 465 466
	grub_term_unregister_input (&grub_usb_keyboards[i]);
	grub_free ((char *) grub_usb_keyboards[i].name);
	grub_usb_keyboards[i].name = NULL;
467
	grub_free (grub_usb_keyboards[i].data);
468 469 470
	grub_usb_keyboards[i].data = 0;
      }
  grub_usb_unregister_attach_hook_class (&attach_hook);
471
}