Jon West | 7d88140 | 2021-04-15 08:42:42 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Tim Theede <pez2001@voyagerproject.de> |
| 3 | * 2015 Terry Cain <terry@terrys-home.co.uk> |
| 4 | * |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the Free |
| 8 | * Software Foundation; either version 2 of the License, or (at your option) |
| 9 | * any later version. |
| 10 | */ |
| 11 | |
| 12 | #ifndef DRIVER_RAZERCOMMON_H_ |
| 13 | #define DRIVER_RAZERCOMMON_H_ |
| 14 | |
| 15 | #include <linux/usb/input.h> |
| 16 | |
| 17 | #define DRIVER_VERSION "2.7.0" |
| 18 | #define DRIVER_LICENSE "GPL v2" |
| 19 | #define DRIVER_AUTHOR "Terry Cain <terry@terrys-home.co.uk>" |
| 20 | |
| 21 | |
| 22 | // Macro to create device files |
| 23 | #define CREATE_DEVICE_FILE(dev, type) \ |
| 24 | do { \ |
| 25 | if(device_create_file(dev, type)) { \ |
| 26 | goto exit_free; \ |
| 27 | } \ |
| 28 | } while (0) |
| 29 | |
| 30 | |
| 31 | #define USB_VENDOR_ID_RAZER 0x1532 |
| 32 | |
| 33 | /* Each USB report has 90 bytes*/ |
| 34 | #define RAZER_USB_REPORT_LEN 0x5A |
| 35 | |
| 36 | // LED STATE |
| 37 | #define OFF 0x00 |
| 38 | #define ON 0x01 |
| 39 | |
| 40 | // LED STORAGE Options |
| 41 | #define NOSTORE 0x00 |
| 42 | #define VARSTORE 0x01 |
| 43 | |
| 44 | // LED definitions |
| 45 | #define ZERO_LED 0x00 |
| 46 | #define SCROLL_WHEEL_LED 0x01 |
| 47 | #define BATTERY_LED 0x03 |
| 48 | #define LOGO_LED 0x04 |
| 49 | #define BACKLIGHT_LED 0x05 |
| 50 | #define MACRO_LED 0x07 |
| 51 | #define GAME_LED 0x08 |
| 52 | #define RED_PROFILE_LED 0x0C |
| 53 | #define GREEN_PROFILE_LED 0x0D |
| 54 | #define BLUE_PROFILE_LED 0x0E |
| 55 | #define RIGHT_SIDE_LED 0x10 |
| 56 | #define LEFT_SIDE_LED 0x11 |
| 57 | |
| 58 | // LED Effect definitions |
| 59 | #define LED_STATIC 0x00 |
| 60 | #define LED_BLINKING 0x01 |
| 61 | #define LED_PULSATING 0x02 |
| 62 | #define LED_SPECTRUM_CYCLING 0x04 |
| 63 | |
| 64 | // Report Responses |
| 65 | #define RAZER_CMD_BUSY 0x01 |
| 66 | #define RAZER_CMD_SUCCESSFUL 0x02 |
| 67 | #define RAZER_CMD_FAILURE 0x03 |
| 68 | #define RAZER_CMD_TIMEOUT 0x04 |
| 69 | #define RAZER_CMD_NOT_SUPPORTED 0x05 |
| 70 | |
| 71 | struct razer_report; |
| 72 | |
| 73 | struct razer_rgb { |
| 74 | unsigned char r,g,b; |
| 75 | }; |
| 76 | |
| 77 | union transaction_id_union { |
| 78 | unsigned char id; |
| 79 | struct transaction_parts { |
| 80 | unsigned char device : 3; |
| 81 | unsigned char id : 5; |
| 82 | } parts; |
| 83 | }; |
| 84 | |
| 85 | union command_id_union { |
| 86 | unsigned char id; |
| 87 | struct command_id_parts { |
| 88 | unsigned char direction : 1; |
| 89 | unsigned char id : 7; |
| 90 | } parts; |
| 91 | }; |
| 92 | |
| 93 | /* Status: |
| 94 | * 0x00 New Command |
| 95 | * 0x01 Command Busy |
| 96 | * 0x02 Command Successful |
| 97 | * 0x03 Command Failure |
| 98 | * 0x04 Command No Response / Command Timeout |
| 99 | * 0x05 Command Not Support |
| 100 | * |
| 101 | * Transaction ID used to group request-response, device useful when multiple devices are on one usb |
| 102 | * Remaining Packets is the number of remaining packets in the sequence |
| 103 | * Protocol Type is always 0x00 |
| 104 | * Data Size is the size of payload, cannot be greater than 80. 90 = header (8B) + data + CRC (1B) + Reserved (1B) |
| 105 | * Command Class is the type of command being issued |
| 106 | * Command ID is the type of command being send. Direction 0 is Host->Device, Direction 1 is Device->Host. AKA Get LED 0x80, Set LED 0x00 |
| 107 | * |
| 108 | * */ |
| 109 | |
| 110 | struct razer_report { |
| 111 | unsigned char status; |
| 112 | union transaction_id_union transaction_id; /* */ |
| 113 | unsigned short remaining_packets; /* Big Endian */ |
| 114 | unsigned char protocol_type; /*0x0*/ |
| 115 | unsigned char data_size; |
| 116 | unsigned char command_class; |
| 117 | union command_id_union command_id; |
| 118 | unsigned char arguments[80]; |
| 119 | unsigned char crc;/*xor'ed bytes of report*/ |
| 120 | unsigned char reserved; /*0x0*/ |
| 121 | }; |
| 122 | |
| 123 | struct razer_key_translation { |
| 124 | u16 from; |
| 125 | u16 to; |
| 126 | u8 flags; |
| 127 | }; |
| 128 | |
| 129 | int razer_send_control_msg(struct usb_device *usb_dev,void const *data, unsigned int report_index, unsigned long wait_min, unsigned long wait_max); |
| 130 | int razer_send_control_msg_old_device(struct usb_device *usb_dev,void const *data, uint report_value, uint report_index, uint report_size, ulong wait_min, ulong wait_max); |
| 131 | int razer_get_usb_response(struct usb_device *usb_dev, unsigned int report_index, struct razer_report* request_report, unsigned int response_index, struct razer_report* response_report, unsigned long wait_min, unsigned long wait_max); |
| 132 | unsigned char razer_calculate_crc(struct razer_report *report); |
| 133 | struct razer_report get_razer_report(unsigned char command_class, unsigned char command_id, unsigned char data_size); |
| 134 | struct razer_report get_empty_razer_report(void); |
| 135 | void print_erroneous_report(struct razer_report* report, char* driver_name, char* message); |
| 136 | |
| 137 | // Convenience functions |
| 138 | unsigned char clamp_u8(unsigned char value, unsigned char min, unsigned char max); |
| 139 | unsigned short clamp_u16(unsigned short value, unsigned short min, unsigned short max); |
| 140 | |
| 141 | #endif /* DRIVER_RAZERCOMMON_H_ */ |