1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES.  All rights reserved.
4  *
5  *  HID driver for NVIDIA SHIELD peripherals.
6  */
7 
8 #include <linux/hid.h>
9 #include <linux/input-event-codes.h>
10 #include <linux/input.h>
11 #include <linux/leds.h>
12 #include <linux/module.h>
13 #include <linux/spinlock.h>
14 #include <linux/workqueue.h>
15 
16 #include "hid-ids.h"
17 
18 #define NOT_INIT_STR "NOT INITIALIZED"
19 #define android_map_key(c) hid_map_usage(hi, usage, bit, max, EV_KEY, (c))
20 
21 enum {
22 	HID_USAGE_ANDROID_PLAYPAUSE_BTN = 0xcd, /* Double-tap volume slider */
23 	HID_USAGE_ANDROID_VOLUMEUP_BTN = 0xe9,
24 	HID_USAGE_ANDROID_VOLUMEDOWN_BTN = 0xea,
25 	HID_USAGE_ANDROID_SEARCH_BTN = 0x221, /* NVIDIA btn on Thunderstrike */
26 	HID_USAGE_ANDROID_HOME_BTN = 0x223,
27 	HID_USAGE_ANDROID_BACK_BTN = 0x224,
28 };
29 
30 enum {
31 	SHIELD_FW_VERSION_INITIALIZED = 0,
32 	SHIELD_BOARD_INFO_INITIALIZED,
33 };
34 
35 enum {
36 	THUNDERSTRIKE_FW_VERSION_UPDATE = 0,
37 	THUNDERSTRIKE_BOARD_INFO_UPDATE,
38 	THUNDERSTRIKE_HAPTICS_UPDATE,
39 	THUNDERSTRIKE_LED_UPDATE,
40 };
41 
42 enum {
43 	THUNDERSTRIKE_HOSTCMD_REPORT_SIZE = 33,
44 	THUNDERSTRIKE_HOSTCMD_REQ_REPORT_ID = 0x4,
45 	THUNDERSTRIKE_HOSTCMD_RESP_REPORT_ID = 0x3,
46 };
47 
48 enum {
49 	THUNDERSTRIKE_HOSTCMD_ID_FW_VERSION = 1,
50 	THUNDERSTRIKE_HOSTCMD_ID_LED = 6,
51 	THUNDERSTRIKE_HOSTCMD_ID_BOARD_INFO = 16,
52 	THUNDERSTRIKE_HOSTCMD_ID_USB_INIT = 53,
53 	THUNDERSTRIKE_HOSTCMD_ID_HAPTICS = 57,
54 	THUNDERSTRIKE_HOSTCMD_ID_BLUETOOTH_INIT = 58,
55 };
56 
57 enum thunderstrike_led_state {
58 	THUNDERSTRIKE_LED_OFF = 1,
59 	THUNDERSTRIKE_LED_ON = 8,
60 } __packed;
61 static_assert(sizeof(enum thunderstrike_led_state) == 1);
62 
63 struct thunderstrike_hostcmd_board_info {
64 	__le16 revision;
65 	__le16 serial[7];
66 } __packed;
67 
68 struct thunderstrike_hostcmd_haptics {
69 	u8 motor_left;
70 	u8 motor_right;
71 } __packed;
72 
73 struct thunderstrike_hostcmd_resp_report {
74 	u8 report_id; /* THUNDERSTRIKE_HOSTCMD_RESP_REPORT_ID */
75 	u8 cmd_id;
76 	u8 reserved_at_10;
77 
78 	union {
79 		struct thunderstrike_hostcmd_board_info board_info;
80 		struct thunderstrike_hostcmd_haptics motors;
81 		__le16 fw_version;
82 		enum thunderstrike_led_state led_state;
83 		u8 payload[30];
84 	} __packed;
85 } __packed;
86 static_assert(sizeof(struct thunderstrike_hostcmd_resp_report) ==
87 	      THUNDERSTRIKE_HOSTCMD_REPORT_SIZE);
88 
89 struct thunderstrike_hostcmd_req_report {
90 	u8 report_id; /* THUNDERSTRIKE_HOSTCMD_REQ_REPORT_ID */
91 	u8 cmd_id;
92 	u8 reserved_at_10;
93 
94 	union {
95 		struct __packed {
96 			u8 update;
97 			enum thunderstrike_led_state state;
98 		} led;
99 		struct __packed {
100 			u8 update;
101 			struct thunderstrike_hostcmd_haptics motors;
102 		} haptics;
103 	} __packed;
104 	u8 reserved_at_30[27];
105 } __packed;
106 static_assert(sizeof(struct thunderstrike_hostcmd_req_report) ==
107 	      THUNDERSTRIKE_HOSTCMD_REPORT_SIZE);
108 
109 /* Common struct for shield accessories. */
110 struct shield_device {
111 	struct hid_device *hdev;
112 
113 	unsigned long initialized_flags;
114 	const char *codename;
115 	u16 fw_version;
116 	struct {
117 		u16 revision;
118 		char serial_number[15];
119 	} board_info;
120 };
121 
122 struct thunderstrike {
123 	struct shield_device base;
124 
125 	/* Sub-devices */
126 	struct input_dev *haptics_dev;
127 	struct led_classdev led_dev;
128 
129 	/* Resources */
130 	void *req_report_dmabuf;
131 	unsigned long update_flags;
132 	struct thunderstrike_hostcmd_haptics haptics_val;
133 	spinlock_t haptics_update_lock;
134 	u8 led_state : 1;
135 	enum thunderstrike_led_state led_value;
136 	struct work_struct hostcmd_req_work;
137 };
138 
139 static inline void thunderstrike_hostcmd_req_report_init(
140 	struct thunderstrike_hostcmd_req_report *report, u8 cmd_id)
141 {
142 	memset(report, 0, sizeof(*report));
143 	report->report_id = THUNDERSTRIKE_HOSTCMD_REQ_REPORT_ID;
144 	report->cmd_id = cmd_id;
145 }
146 
147 static inline void shield_strrev(char *dest, size_t len, u16 rev)
148 {
149 	dest[0] = ('A' - 1) + (rev >> 8);
150 	snprintf(&dest[1], len - 1, "%02X", 0xff & rev);
151 }
152 
153 static struct input_dev *shield_allocate_input_dev(struct hid_device *hdev,
154 						   const char *name_suffix)
155 {
156 	struct input_dev *idev;
157 
158 	idev = input_allocate_device();
159 	if (!idev)
160 		goto err_device;
161 
162 	idev->id.bustype = hdev->bus;
163 	idev->id.vendor = hdev->vendor;
164 	idev->id.product = hdev->product;
165 	idev->id.version = hdev->version;
166 	idev->uniq = hdev->uniq;
167 	idev->name = devm_kasprintf(&idev->dev, GFP_KERNEL, "%s %s", hdev->name,
168 				    name_suffix);
169 	if (!idev->name)
170 		goto err_name;
171 
172 	input_set_drvdata(idev, hdev);
173 
174 	return idev;
175 
176 err_name:
177 	input_free_device(idev);
178 err_device:
179 	return ERR_PTR(-ENOMEM);
180 }
181 
182 static struct input_dev *shield_haptics_create(
183 	struct shield_device *dev,
184 	int (*play_effect)(struct input_dev *, void *, struct ff_effect *))
185 {
186 	struct input_dev *haptics;
187 	int ret;
188 
189 	if (!IS_ENABLED(CONFIG_NVIDIA_SHIELD_FF))
190 		return NULL;
191 
192 	haptics = shield_allocate_input_dev(dev->hdev, "Haptics");
193 	if (IS_ERR(haptics))
194 		return haptics;
195 
196 	input_set_capability(haptics, EV_FF, FF_RUMBLE);
197 	input_ff_create_memless(haptics, NULL, play_effect);
198 
199 	ret = input_register_device(haptics);
200 	if (ret)
201 		goto err;
202 
203 	return haptics;
204 
205 err:
206 	input_free_device(haptics);
207 	return ERR_PTR(ret);
208 }
209 
210 static inline void thunderstrike_send_hostcmd_request(struct thunderstrike *ts)
211 {
212 	struct thunderstrike_hostcmd_req_report *report = ts->req_report_dmabuf;
213 	struct shield_device *shield_dev = &ts->base;
214 	int ret;
215 
216 	ret = hid_hw_raw_request(shield_dev->hdev, report->report_id,
217 				 ts->req_report_dmabuf,
218 				 THUNDERSTRIKE_HOSTCMD_REPORT_SIZE,
219 				 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
220 
221 	if (ret < 0) {
222 		hid_err(shield_dev->hdev,
223 			"Failed to output Thunderstrike HOSTCMD request HID report due to %pe\n",
224 			ERR_PTR(ret));
225 	}
226 }
227 
228 static void thunderstrike_hostcmd_req_work_handler(struct work_struct *work)
229 {
230 	struct thunderstrike *ts =
231 		container_of(work, struct thunderstrike, hostcmd_req_work);
232 	struct thunderstrike_hostcmd_req_report *report;
233 	unsigned long flags;
234 
235 	report = ts->req_report_dmabuf;
236 
237 	if (test_and_clear_bit(THUNDERSTRIKE_FW_VERSION_UPDATE, &ts->update_flags)) {
238 		thunderstrike_hostcmd_req_report_init(
239 			report, THUNDERSTRIKE_HOSTCMD_ID_FW_VERSION);
240 		thunderstrike_send_hostcmd_request(ts);
241 	}
242 
243 	if (test_and_clear_bit(THUNDERSTRIKE_LED_UPDATE, &ts->update_flags)) {
244 		thunderstrike_hostcmd_req_report_init(report, THUNDERSTRIKE_HOSTCMD_ID_LED);
245 		report->led.update = 1;
246 		report->led.state = ts->led_value;
247 		thunderstrike_send_hostcmd_request(ts);
248 	}
249 
250 	if (test_and_clear_bit(THUNDERSTRIKE_BOARD_INFO_UPDATE, &ts->update_flags)) {
251 		thunderstrike_hostcmd_req_report_init(
252 			report, THUNDERSTRIKE_HOSTCMD_ID_BOARD_INFO);
253 		thunderstrike_send_hostcmd_request(ts);
254 	}
255 
256 	if (test_and_clear_bit(THUNDERSTRIKE_HAPTICS_UPDATE, &ts->update_flags)) {
257 		thunderstrike_hostcmd_req_report_init(
258 			report, THUNDERSTRIKE_HOSTCMD_ID_HAPTICS);
259 
260 		report->haptics.update = 1;
261 		spin_lock_irqsave(&ts->haptics_update_lock, flags);
262 		report->haptics.motors = ts->haptics_val;
263 		spin_unlock_irqrestore(&ts->haptics_update_lock, flags);
264 
265 		thunderstrike_send_hostcmd_request(ts);
266 	}
267 }
268 
269 static inline void thunderstrike_request_firmware_version(struct thunderstrike *ts)
270 {
271 	set_bit(THUNDERSTRIKE_FW_VERSION_UPDATE, &ts->update_flags);
272 	schedule_work(&ts->hostcmd_req_work);
273 }
274 
275 static inline void thunderstrike_request_board_info(struct thunderstrike *ts)
276 {
277 	set_bit(THUNDERSTRIKE_BOARD_INFO_UPDATE, &ts->update_flags);
278 	schedule_work(&ts->hostcmd_req_work);
279 }
280 
281 static inline int
282 thunderstrike_update_haptics(struct thunderstrike *ts,
283 			     struct thunderstrike_hostcmd_haptics *motors)
284 {
285 	unsigned long flags;
286 
287 	spin_lock_irqsave(&ts->haptics_update_lock, flags);
288 	ts->haptics_val = *motors;
289 	spin_unlock_irqrestore(&ts->haptics_update_lock, flags);
290 
291 	set_bit(THUNDERSTRIKE_HAPTICS_UPDATE, &ts->update_flags);
292 	schedule_work(&ts->hostcmd_req_work);
293 
294 	return 0;
295 }
296 
297 static int thunderstrike_play_effect(struct input_dev *idev, void *data,
298 				     struct ff_effect *effect)
299 {
300 	struct hid_device *hdev = input_get_drvdata(idev);
301 	struct thunderstrike_hostcmd_haptics motors;
302 	struct shield_device *shield_dev;
303 	struct thunderstrike *ts;
304 
305 	if (effect->type != FF_RUMBLE)
306 		return 0;
307 
308 	shield_dev = hid_get_drvdata(hdev);
309 	ts = container_of(shield_dev, struct thunderstrike, base);
310 
311 	/* Thunderstrike motor values range from 0 to 32 inclusively */
312 	motors.motor_left = effect->u.rumble.strong_magnitude / 2047;
313 	motors.motor_right = effect->u.rumble.weak_magnitude / 2047;
314 
315 	hid_dbg(hdev, "Thunderstrike FF_RUMBLE request, left: %u right: %u\n",
316 		motors.motor_left, motors.motor_right);
317 
318 	return thunderstrike_update_haptics(ts, &motors);
319 }
320 
321 static enum led_brightness
322 thunderstrike_led_get_brightness(struct led_classdev *led)
323 {
324 	struct hid_device *hdev = to_hid_device(led->dev->parent);
325 	struct shield_device *shield_dev = hid_get_drvdata(hdev);
326 	struct thunderstrike *ts;
327 
328 	ts = container_of(shield_dev, struct thunderstrike, base);
329 
330 	return ts->led_state;
331 }
332 
333 static void thunderstrike_led_set_brightness(struct led_classdev *led,
334 					    enum led_brightness value)
335 {
336 	struct hid_device *hdev = to_hid_device(led->dev->parent);
337 	struct shield_device *shield_dev = hid_get_drvdata(hdev);
338 	struct thunderstrike *ts;
339 
340 	ts = container_of(shield_dev, struct thunderstrike, base);
341 
342 	switch (value) {
343 	case LED_OFF:
344 		ts->led_value = THUNDERSTRIKE_LED_OFF;
345 		break;
346 	default:
347 		ts->led_value = THUNDERSTRIKE_LED_ON;
348 		break;
349 	}
350 
351 	set_bit(THUNDERSTRIKE_LED_UPDATE, &ts->update_flags);
352 	schedule_work(&ts->hostcmd_req_work);
353 }
354 
355 static void
356 thunderstrike_parse_fw_version_payload(struct shield_device *shield_dev,
357 				       __le16 fw_version)
358 {
359 	shield_dev->fw_version = le16_to_cpu(fw_version);
360 
361 	set_bit(SHIELD_FW_VERSION_INITIALIZED, &shield_dev->initialized_flags);
362 
363 	hid_dbg(shield_dev->hdev, "Thunderstrike firmware version 0x%04X\n",
364 		shield_dev->fw_version);
365 }
366 
367 static void
368 thunderstrike_parse_board_info_payload(struct shield_device *shield_dev,
369 				       struct thunderstrike_hostcmd_board_info *board_info)
370 {
371 	char board_revision_str[4];
372 	int i;
373 
374 	shield_dev->board_info.revision = le16_to_cpu(board_info->revision);
375 	for (i = 0; i < 7; ++i) {
376 		u16 val = le16_to_cpu(board_info->serial[i]);
377 
378 		shield_dev->board_info.serial_number[2 * i] = val & 0xFF;
379 		shield_dev->board_info.serial_number[2 * i + 1] = val >> 8;
380 	}
381 	shield_dev->board_info.serial_number[14] = '\0';
382 
383 	set_bit(SHIELD_BOARD_INFO_INITIALIZED, &shield_dev->initialized_flags);
384 
385 	shield_strrev(board_revision_str, 4, shield_dev->board_info.revision);
386 	hid_dbg(shield_dev->hdev,
387 		"Thunderstrike BOARD_REVISION_%s (0x%04X) S/N: %s\n",
388 		board_revision_str, shield_dev->board_info.revision,
389 		shield_dev->board_info.serial_number);
390 }
391 
392 static inline void
393 thunderstrike_parse_haptics_payload(struct shield_device *shield_dev,
394 				    struct thunderstrike_hostcmd_haptics *haptics)
395 {
396 	hid_dbg(shield_dev->hdev,
397 		"Thunderstrike haptics HOSTCMD response, left: %u right: %u\n",
398 		haptics->motor_left, haptics->motor_right);
399 }
400 
401 static void
402 thunderstrike_parse_led_payload(struct shield_device *shield_dev,
403 				enum thunderstrike_led_state led_state)
404 {
405 	struct thunderstrike *ts = container_of(shield_dev, struct thunderstrike, base);
406 
407 	switch (led_state) {
408 	case THUNDERSTRIKE_LED_OFF:
409 		ts->led_state = 0;
410 		break;
411 	case THUNDERSTRIKE_LED_ON:
412 		ts->led_state = 1;
413 		break;
414 	}
415 
416 	hid_dbg(shield_dev->hdev, "Thunderstrike led HOSTCMD response, 0x%02X\n", led_state);
417 }
418 
419 static int thunderstrike_parse_report(struct shield_device *shield_dev,
420 				      struct hid_report *report, u8 *data,
421 				      int size)
422 {
423 	struct thunderstrike_hostcmd_resp_report *hostcmd_resp_report;
424 	struct thunderstrike *ts =
425 		container_of(shield_dev, struct thunderstrike, base);
426 	struct hid_device *hdev = shield_dev->hdev;
427 
428 	switch (report->id) {
429 	case THUNDERSTRIKE_HOSTCMD_RESP_REPORT_ID:
430 		if (size != THUNDERSTRIKE_HOSTCMD_REPORT_SIZE) {
431 			hid_err(hdev,
432 				"Encountered Thunderstrike HOSTCMD HID report with unexpected size %d\n",
433 				size);
434 			return -EINVAL;
435 		}
436 
437 		hostcmd_resp_report =
438 			(struct thunderstrike_hostcmd_resp_report *)data;
439 
440 		switch (hostcmd_resp_report->cmd_id) {
441 		case THUNDERSTRIKE_HOSTCMD_ID_FW_VERSION:
442 			thunderstrike_parse_fw_version_payload(
443 				shield_dev, hostcmd_resp_report->fw_version);
444 			break;
445 		case THUNDERSTRIKE_HOSTCMD_ID_LED:
446 			thunderstrike_parse_led_payload(shield_dev, hostcmd_resp_report->led_state);
447 			break;
448 		case THUNDERSTRIKE_HOSTCMD_ID_BOARD_INFO:
449 			thunderstrike_parse_board_info_payload(
450 				shield_dev, &hostcmd_resp_report->board_info);
451 			break;
452 		case THUNDERSTRIKE_HOSTCMD_ID_HAPTICS:
453 			thunderstrike_parse_haptics_payload(
454 				shield_dev, &hostcmd_resp_report->motors);
455 			break;
456 
457 		case THUNDERSTRIKE_HOSTCMD_ID_USB_INIT:
458 		case THUNDERSTRIKE_HOSTCMD_ID_BLUETOOTH_INIT:
459 			/* May block HOSTCMD requests till received initially */
460 			thunderstrike_request_firmware_version(ts);
461 			thunderstrike_request_board_info(ts);
462 			/* Only HOSTCMD that can be triggered without a request */
463 			return 0;
464 		default:
465 			hid_warn(hdev,
466 				 "Unhandled Thunderstrike HOSTCMD id %d\n",
467 				 hostcmd_resp_report->cmd_id);
468 			return -ENOENT;
469 		}
470 
471 		break;
472 	default:
473 		return 0;
474 	}
475 
476 	return 0;
477 }
478 
479 static inline int thunderstrike_led_create(struct thunderstrike *ts)
480 {
481 	struct led_classdev *led = &ts->led_dev;
482 
483 	led->name = "thunderstrike:blue:led";
484 	led->max_brightness = 1;
485 	led->flags = LED_CORE_SUSPENDRESUME;
486 	led->brightness_get = &thunderstrike_led_get_brightness;
487 	led->brightness_set = &thunderstrike_led_set_brightness;
488 
489 	return led_classdev_register(&ts->base.hdev->dev, led);
490 }
491 
492 static struct shield_device *thunderstrike_create(struct hid_device *hdev)
493 {
494 	struct shield_device *shield_dev;
495 	struct thunderstrike *ts;
496 	int ret;
497 
498 	ts = devm_kzalloc(&hdev->dev, sizeof(*ts), GFP_KERNEL);
499 	if (!ts)
500 		return ERR_PTR(-ENOMEM);
501 
502 	ts->req_report_dmabuf = devm_kzalloc(
503 		&hdev->dev, THUNDERSTRIKE_HOSTCMD_REPORT_SIZE, GFP_KERNEL);
504 	if (!ts->req_report_dmabuf)
505 		return ERR_PTR(-ENOMEM);
506 
507 	shield_dev = &ts->base;
508 	shield_dev->hdev = hdev;
509 	shield_dev->codename = "Thunderstrike";
510 
511 	spin_lock_init(&ts->haptics_update_lock);
512 	INIT_WORK(&ts->hostcmd_req_work, thunderstrike_hostcmd_req_work_handler);
513 
514 	hid_set_drvdata(hdev, shield_dev);
515 
516 	ret = thunderstrike_led_create(ts);
517 	if (ret) {
518 		hid_err(hdev, "Failed to create Thunderstrike LED instance\n");
519 		return ERR_PTR(ret);
520 	}
521 
522 	ts->haptics_dev = shield_haptics_create(shield_dev, thunderstrike_play_effect);
523 	if (IS_ERR(ts->haptics_dev))
524 		goto err;
525 
526 	hid_info(hdev, "Registered Thunderstrike controller\n");
527 	return shield_dev;
528 
529 err:
530 	led_classdev_unregister(&ts->led_dev);
531 	return ERR_CAST(ts->haptics_dev);
532 }
533 
534 static int android_input_mapping(struct hid_device *hdev, struct hid_input *hi,
535 				 struct hid_field *field,
536 				 struct hid_usage *usage, unsigned long **bit,
537 				 int *max)
538 {
539 	if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
540 		return 0;
541 
542 	switch (usage->hid & HID_USAGE) {
543 	case HID_USAGE_ANDROID_PLAYPAUSE_BTN:
544 		android_map_key(KEY_PLAYPAUSE);
545 		break;
546 	case HID_USAGE_ANDROID_VOLUMEUP_BTN:
547 		android_map_key(KEY_VOLUMEUP);
548 		break;
549 	case HID_USAGE_ANDROID_VOLUMEDOWN_BTN:
550 		android_map_key(KEY_VOLUMEDOWN);
551 		break;
552 	case HID_USAGE_ANDROID_SEARCH_BTN:
553 		android_map_key(BTN_Z);
554 		break;
555 	case HID_USAGE_ANDROID_HOME_BTN:
556 		android_map_key(BTN_MODE);
557 		break;
558 	case HID_USAGE_ANDROID_BACK_BTN:
559 		android_map_key(BTN_SELECT);
560 		break;
561 	default:
562 		return 0;
563 	}
564 
565 	return 1;
566 }
567 
568 static ssize_t firmware_version_show(struct device *dev,
569 				     struct device_attribute *attr, char *buf)
570 {
571 	struct hid_device *hdev = to_hid_device(dev);
572 	struct shield_device *shield_dev;
573 	int ret;
574 
575 	shield_dev = hid_get_drvdata(hdev);
576 
577 	if (test_bit(SHIELD_FW_VERSION_INITIALIZED, &shield_dev->initialized_flags))
578 		ret = sysfs_emit(buf, "0x%04X\n", shield_dev->fw_version);
579 	else
580 		ret = sysfs_emit(buf, NOT_INIT_STR "\n");
581 
582 	return ret;
583 }
584 
585 static DEVICE_ATTR_RO(firmware_version);
586 
587 static ssize_t hardware_version_show(struct device *dev,
588 				     struct device_attribute *attr, char *buf)
589 {
590 	struct hid_device *hdev = to_hid_device(dev);
591 	struct shield_device *shield_dev;
592 	char board_revision_str[4];
593 	int ret;
594 
595 	shield_dev = hid_get_drvdata(hdev);
596 
597 	if (test_bit(SHIELD_BOARD_INFO_INITIALIZED, &shield_dev->initialized_flags)) {
598 		shield_strrev(board_revision_str, 4, shield_dev->board_info.revision);
599 		ret = sysfs_emit(buf, "%s BOARD_REVISION_%s (0x%04X)\n",
600 				 shield_dev->codename, board_revision_str,
601 				 shield_dev->board_info.revision);
602 	} else
603 		ret = sysfs_emit(buf, NOT_INIT_STR "\n");
604 
605 	return ret;
606 }
607 
608 static DEVICE_ATTR_RO(hardware_version);
609 
610 static ssize_t serial_number_show(struct device *dev,
611 				  struct device_attribute *attr, char *buf)
612 {
613 	struct hid_device *hdev = to_hid_device(dev);
614 	struct shield_device *shield_dev;
615 	int ret;
616 
617 	shield_dev = hid_get_drvdata(hdev);
618 
619 	if (test_bit(SHIELD_BOARD_INFO_INITIALIZED, &shield_dev->initialized_flags))
620 		ret = sysfs_emit(buf, "%s\n", shield_dev->board_info.serial_number);
621 	else
622 		ret = sysfs_emit(buf, NOT_INIT_STR "\n");
623 
624 	return ret;
625 }
626 
627 static DEVICE_ATTR_RO(serial_number);
628 
629 static struct attribute *shield_device_attrs[] = {
630 	&dev_attr_firmware_version.attr,
631 	&dev_attr_hardware_version.attr,
632 	&dev_attr_serial_number.attr,
633 	NULL,
634 };
635 ATTRIBUTE_GROUPS(shield_device);
636 
637 static int shield_raw_event(struct hid_device *hdev, struct hid_report *report,
638 			    u8 *data, int size)
639 {
640 	struct shield_device *dev = hid_get_drvdata(hdev);
641 
642 	return thunderstrike_parse_report(dev, report, data, size);
643 }
644 
645 static int shield_probe(struct hid_device *hdev, const struct hid_device_id *id)
646 {
647 	struct shield_device *shield_dev = NULL;
648 	struct thunderstrike *ts;
649 	int ret;
650 
651 	ret = hid_parse(hdev);
652 	if (ret) {
653 		hid_err(hdev, "Parse failed\n");
654 		return ret;
655 	}
656 
657 	switch (id->product) {
658 	case USB_DEVICE_ID_NVIDIA_THUNDERSTRIKE_CONTROLLER:
659 		shield_dev = thunderstrike_create(hdev);
660 		break;
661 	}
662 
663 	if (unlikely(!shield_dev)) {
664 		hid_err(hdev, "Failed to identify SHIELD device\n");
665 		return -ENODEV;
666 	}
667 	if (IS_ERR(shield_dev)) {
668 		hid_err(hdev, "Failed to create SHIELD device\n");
669 		return PTR_ERR(shield_dev);
670 	}
671 
672 	ts = container_of(shield_dev, struct thunderstrike, base);
673 
674 	ret = hid_hw_start(hdev, HID_CONNECT_HIDINPUT);
675 	if (ret) {
676 		hid_err(hdev, "Failed to start HID device\n");
677 		goto err_haptics;
678 	}
679 
680 	ret = hid_hw_open(hdev);
681 	if (ret) {
682 		hid_err(hdev, "Failed to open HID device\n");
683 		goto err_stop;
684 	}
685 
686 	thunderstrike_request_firmware_version(ts);
687 	thunderstrike_request_board_info(ts);
688 
689 	return ret;
690 
691 err_stop:
692 	hid_hw_stop(hdev);
693 err_haptics:
694 	if (ts->haptics_dev)
695 		input_unregister_device(ts->haptics_dev);
696 	return ret;
697 }
698 
699 static void shield_remove(struct hid_device *hdev)
700 {
701 	struct shield_device *dev = hid_get_drvdata(hdev);
702 	struct thunderstrike *ts;
703 
704 	ts = container_of(dev, struct thunderstrike, base);
705 
706 	hid_hw_close(hdev);
707 	led_classdev_unregister(&ts->led_dev);
708 	if (ts->haptics_dev)
709 		input_unregister_device(ts->haptics_dev);
710 	cancel_work_sync(&ts->hostcmd_req_work);
711 	hid_hw_stop(hdev);
712 }
713 
714 static const struct hid_device_id shield_devices[] = {
715 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NVIDIA,
716 			       USB_DEVICE_ID_NVIDIA_THUNDERSTRIKE_CONTROLLER) },
717 	{ HID_USB_DEVICE(USB_VENDOR_ID_NVIDIA,
718 			 USB_DEVICE_ID_NVIDIA_THUNDERSTRIKE_CONTROLLER) },
719 	{ }
720 };
721 MODULE_DEVICE_TABLE(hid, shield_devices);
722 
723 static struct hid_driver shield_driver = {
724 	.name          = "shield",
725 	.id_table      = shield_devices,
726 	.input_mapping = android_input_mapping,
727 	.probe         = shield_probe,
728 	.remove        = shield_remove,
729 	.raw_event     = shield_raw_event,
730 	.driver = {
731 		.dev_groups = shield_device_groups,
732 	},
733 };
734 module_hid_driver(shield_driver);
735 
736 MODULE_AUTHOR("Rahul Rameshbabu <rrameshbabu@nvidia.com>");
737 MODULE_DESCRIPTION("HID Driver for NVIDIA SHIELD peripherals.");
738 MODULE_LICENSE("GPL");
739