Lines Matching refs:ctlr
221 static int joycon_hid_send_sync(struct joycon_ctlr *ctlr, u8 *data, size_t len) in joycon_hid_send_sync() argument
225 ret = __joycon_hid_send(ctlr->hdev, data, len); in joycon_hid_send_sync()
227 memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE); in joycon_hid_send_sync()
231 if (!wait_event_timeout(ctlr->wait, ctlr->received_resp, HZ)) { in joycon_hid_send_sync()
232 hid_dbg(ctlr->hdev, "synchronous send/receive timed out\n"); in joycon_hid_send_sync()
233 memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE); in joycon_hid_send_sync()
237 ctlr->received_resp = false; in joycon_hid_send_sync()
241 static int joycon_send_usb(struct joycon_ctlr *ctlr, u8 cmd) in joycon_send_usb() argument
247 ctlr->usb_ack_match = cmd; in joycon_send_usb()
248 ctlr->msg_type = JOYCON_MSG_TYPE_USB; in joycon_send_usb()
249 ret = joycon_hid_send_sync(ctlr, buf, sizeof(buf)); in joycon_send_usb()
251 hid_dbg(ctlr->hdev, "send usb command failed; ret=%d\n", ret); in joycon_send_usb()
255 static int joycon_send_subcmd(struct joycon_ctlr *ctlr, in joycon_send_subcmd() argument
262 subcmd->packet_num = ctlr->subcmd_num; in joycon_send_subcmd()
263 if (++ctlr->subcmd_num > 0xF) in joycon_send_subcmd()
264 ctlr->subcmd_num = 0; in joycon_send_subcmd()
265 ctlr->subcmd_ack_match = subcmd->subcmd_id; in joycon_send_subcmd()
266 ctlr->msg_type = JOYCON_MSG_TYPE_SUBCMD; in joycon_send_subcmd()
268 ret = joycon_hid_send_sync(ctlr, (u8 *)subcmd, in joycon_send_subcmd()
271 hid_dbg(ctlr->hdev, "send subcommand failed; ret=%d\n", ret); in joycon_send_subcmd()
278 static int joycon_set_player_leds(struct joycon_ctlr *ctlr, u8 flash, u8 on) in joycon_set_player_leds() argument
287 hid_dbg(ctlr->hdev, "setting player leds\n"); in joycon_set_player_leds()
288 return joycon_send_subcmd(ctlr, req, 1); in joycon_set_player_leds()
294 static int joycon_request_calibration(struct joycon_ctlr *ctlr) in joycon_request_calibration() argument
318 hid_dbg(ctlr->hdev, "requesting cal data\n"); in joycon_request_calibration()
319 ret = joycon_send_subcmd(ctlr, req, 5); in joycon_request_calibration()
321 hid_warn(ctlr->hdev, in joycon_request_calibration()
325 ctlr->left_stick_cal_x.center = DFLT_STICK_CAL_CEN; in joycon_request_calibration()
326 ctlr->left_stick_cal_x.max = DFLT_STICK_CAL_MAX; in joycon_request_calibration()
327 ctlr->left_stick_cal_x.min = DFLT_STICK_CAL_MIN; in joycon_request_calibration()
329 ctlr->left_stick_cal_y.center = DFLT_STICK_CAL_CEN; in joycon_request_calibration()
330 ctlr->left_stick_cal_y.max = DFLT_STICK_CAL_MAX; in joycon_request_calibration()
331 ctlr->left_stick_cal_y.min = DFLT_STICK_CAL_MIN; in joycon_request_calibration()
333 ctlr->right_stick_cal_x.center = DFLT_STICK_CAL_CEN; in joycon_request_calibration()
334 ctlr->right_stick_cal_x.max = DFLT_STICK_CAL_MAX; in joycon_request_calibration()
335 ctlr->right_stick_cal_x.min = DFLT_STICK_CAL_MIN; in joycon_request_calibration()
337 ctlr->right_stick_cal_y.center = DFLT_STICK_CAL_CEN; in joycon_request_calibration()
338 ctlr->right_stick_cal_y.max = DFLT_STICK_CAL_MAX; in joycon_request_calibration()
339 ctlr->right_stick_cal_y.min = DFLT_STICK_CAL_MIN; in joycon_request_calibration()
344 report = (struct joycon_input_report *)ctlr->input_buf; in joycon_request_calibration()
348 cal_x = &ctlr->left_stick_cal_x; in joycon_request_calibration()
349 cal_y = &ctlr->left_stick_cal_y; in joycon_request_calibration()
351 x_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 0), 0, 12); in joycon_request_calibration()
352 y_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 1), 4, 12); in joycon_request_calibration()
353 cal_x->center = hid_field_extract(ctlr->hdev, (raw_cal + 3), 0, 12); in joycon_request_calibration()
354 cal_y->center = hid_field_extract(ctlr->hdev, (raw_cal + 4), 4, 12); in joycon_request_calibration()
355 x_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 6), 0, 12); in joycon_request_calibration()
356 y_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 7), 4, 12); in joycon_request_calibration()
364 cal_x = &ctlr->right_stick_cal_x; in joycon_request_calibration()
365 cal_y = &ctlr->right_stick_cal_y; in joycon_request_calibration()
367 cal_x->center = hid_field_extract(ctlr->hdev, (raw_cal + 0), 0, 12); in joycon_request_calibration()
368 cal_y->center = hid_field_extract(ctlr->hdev, (raw_cal + 1), 4, 12); in joycon_request_calibration()
369 x_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 3), 0, 12); in joycon_request_calibration()
370 y_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 4), 4, 12); in joycon_request_calibration()
371 x_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 6), 0, 12); in joycon_request_calibration()
372 y_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 7), 4, 12); in joycon_request_calibration()
378 hid_dbg(ctlr->hdev, "calibration:\n" in joycon_request_calibration()
383 ctlr->left_stick_cal_x.center, in joycon_request_calibration()
384 ctlr->left_stick_cal_x.max, in joycon_request_calibration()
385 ctlr->left_stick_cal_x.min, in joycon_request_calibration()
386 ctlr->left_stick_cal_y.center, in joycon_request_calibration()
387 ctlr->left_stick_cal_y.max, in joycon_request_calibration()
388 ctlr->left_stick_cal_y.min, in joycon_request_calibration()
389 ctlr->right_stick_cal_x.center, in joycon_request_calibration()
390 ctlr->right_stick_cal_x.max, in joycon_request_calibration()
391 ctlr->right_stick_cal_x.min, in joycon_request_calibration()
392 ctlr->right_stick_cal_y.center, in joycon_request_calibration()
393 ctlr->right_stick_cal_y.max, in joycon_request_calibration()
394 ctlr->right_stick_cal_y.min); in joycon_request_calibration()
399 static int joycon_set_report_mode(struct joycon_ctlr *ctlr) in joycon_set_report_mode() argument
408 hid_dbg(ctlr->hdev, "setting controller report mode\n"); in joycon_set_report_mode()
409 return joycon_send_subcmd(ctlr, req, 1); in joycon_set_report_mode()
430 static void joycon_parse_report(struct joycon_ctlr *ctlr, in joycon_parse_report() argument
433 struct input_dev *dev = ctlr->input; in joycon_parse_report()
435 u32 id = ctlr->hdev->product; in joycon_parse_report()
437 btns = hid_field_extract(ctlr->hdev, rep->button_status, 0, 24); in joycon_parse_report()
446 raw_x = hid_field_extract(ctlr->hdev, rep->left_stick, 0, 12); in joycon_parse_report()
447 raw_y = hid_field_extract(ctlr->hdev, in joycon_parse_report()
450 x = joycon_map_stick_val(&ctlr->left_stick_cal_x, raw_x); in joycon_parse_report()
451 y = -joycon_map_stick_val(&ctlr->left_stick_cal_y, raw_y); in joycon_parse_report()
479 raw_x = hid_field_extract(ctlr->hdev, rep->right_stick, 0, 12); in joycon_parse_report()
480 raw_y = hid_field_extract(ctlr->hdev, in joycon_parse_report()
483 x = joycon_map_stick_val(&ctlr->right_stick_cal_x, raw_x); in joycon_parse_report()
484 y = -joycon_map_stick_val(&ctlr->right_stick_cal_y, raw_y); in joycon_parse_report()
525 static int joycon_input_create(struct joycon_ctlr *ctlr) in joycon_input_create() argument
533 hdev = ctlr->hdev; in joycon_input_create()
550 ctlr->input = devm_input_allocate_device(&hdev->dev); in joycon_input_create()
551 if (!ctlr->input) in joycon_input_create()
553 ctlr->input->id.bustype = hdev->bus; in joycon_input_create()
554 ctlr->input->id.vendor = hdev->vendor; in joycon_input_create()
555 ctlr->input->id.product = hdev->product; in joycon_input_create()
556 ctlr->input->id.version = hdev->version; in joycon_input_create()
557 ctlr->input->name = name; in joycon_input_create()
558 input_set_drvdata(ctlr->input, ctlr); in joycon_input_create()
563 input_set_abs_params(ctlr->input, ABS_X, in joycon_input_create()
566 input_set_abs_params(ctlr->input, ABS_Y, in joycon_input_create()
571 input_set_abs_params(ctlr->input, ABS_RX, in joycon_input_create()
574 input_set_abs_params(ctlr->input, ABS_RY, in joycon_input_create()
582 input_set_capability(ctlr->input, EV_KEY, in joycon_input_create()
587 input_set_capability(ctlr->input, EV_KEY, in joycon_input_create()
591 ret = input_register_device(ctlr->input); in joycon_input_create()
597 mutex_lock(&ctlr->output_mutex); in joycon_input_create()
598 ret = joycon_set_player_leds(ctlr, 0, 0xF >> (4 - input_num)); in joycon_input_create()
600 hid_warn(ctlr->hdev, "Failed to set leds; ret=%d\n", ret); in joycon_input_create()
601 mutex_unlock(&ctlr->output_mutex); in joycon_input_create()
610 static int joycon_ctlr_read_handler(struct joycon_ctlr *ctlr, u8 *data, in joycon_ctlr_read_handler() argument
618 joycon_parse_report(ctlr, in joycon_ctlr_read_handler()
625 static int joycon_ctlr_handle_event(struct joycon_ctlr *ctlr, u8 *data, in joycon_ctlr_handle_event() argument
632 if (unlikely(mutex_is_locked(&ctlr->output_mutex)) && in joycon_ctlr_handle_event()
633 ctlr->msg_type != JOYCON_MSG_TYPE_NONE) { in joycon_ctlr_handle_event()
634 switch (ctlr->msg_type) { in joycon_ctlr_handle_event()
639 data[1] == ctlr->usb_ack_match) in joycon_ctlr_handle_event()
647 if (report->reply.id == ctlr->subcmd_ack_match) in joycon_ctlr_handle_event()
655 memcpy(ctlr->input_buf, data, in joycon_ctlr_handle_event()
657 ctlr->msg_type = JOYCON_MSG_TYPE_NONE; in joycon_ctlr_handle_event()
658 ctlr->received_resp = true; in joycon_ctlr_handle_event()
659 wake_up(&ctlr->wait); in joycon_ctlr_handle_event()
666 if (ctlr->ctlr_state == JOYCON_CTLR_STATE_READ) in joycon_ctlr_handle_event()
667 ret = joycon_ctlr_read_handler(ctlr, data, size); in joycon_ctlr_handle_event()
675 struct joycon_ctlr *ctlr = hid_get_drvdata(hdev); in nintendo_hid_event() local
680 return joycon_ctlr_handle_event(ctlr, raw_data, size); in nintendo_hid_event()
687 struct joycon_ctlr *ctlr; in nintendo_hid_probe() local
691 ctlr = devm_kzalloc(&hdev->dev, sizeof(*ctlr), GFP_KERNEL); in nintendo_hid_probe()
692 if (!ctlr) { in nintendo_hid_probe()
697 ctlr->hdev = hdev; in nintendo_hid_probe()
698 ctlr->ctlr_state = JOYCON_CTLR_STATE_INIT; in nintendo_hid_probe()
699 hid_set_drvdata(hdev, ctlr); in nintendo_hid_probe()
700 mutex_init(&ctlr->output_mutex); in nintendo_hid_probe()
701 init_waitqueue_head(&ctlr->wait); in nintendo_hid_probe()
724 mutex_lock(&ctlr->output_mutex); in nintendo_hid_probe()
727 !joycon_send_usb(ctlr, JC_USB_CMD_HANDSHAKE)) { in nintendo_hid_probe()
730 ret = joycon_send_usb(ctlr, JC_USB_CMD_BAUDRATE_3M); in nintendo_hid_probe()
736 ret = joycon_send_usb(ctlr, JC_USB_CMD_HANDSHAKE); in nintendo_hid_probe()
745 joycon_send_usb(ctlr, JC_USB_CMD_NO_TIMEOUT); in nintendo_hid_probe()
749 ret = joycon_request_calibration(ctlr); in nintendo_hid_probe()
759 ret = joycon_set_report_mode(ctlr); in nintendo_hid_probe()
765 mutex_unlock(&ctlr->output_mutex); in nintendo_hid_probe()
767 ret = joycon_input_create(ctlr); in nintendo_hid_probe()
773 ctlr->ctlr_state = JOYCON_CTLR_STATE_READ; in nintendo_hid_probe()
779 mutex_unlock(&ctlr->output_mutex); in nintendo_hid_probe()