1 // SPDX-License-Identifier: GPL-2.0-only
2 /* DVB USB framework compliant Linux driver for the Hauppauge WinTV-NOVA-T usb2
3  * DVB-T receiver.
4  *
5  * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@posteo.de)
6  *
7  * see Documentation/media/dvb-drivers/dvb-usb.rst for more information
8  */
9 #include "dibusb.h"
10 
11 static int debug;
12 module_param(debug, int, 0644);
13 MODULE_PARM_DESC(debug, "set debugging level (1=rc,2=eeprom (|-able))." DVB_USB_DEBUG_STATUS);
14 
15 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
16 
17 #define deb_rc(args...) dprintk(debug,0x01,args)
18 #define deb_ee(args...) dprintk(debug,0x02,args)
19 
20 /* Hauppauge NOVA-T USB2 keys */
21 static struct rc_map_table rc_map_haupp_table[] = {
22 	{ 0x1e00, KEY_0 },
23 	{ 0x1e01, KEY_1 },
24 	{ 0x1e02, KEY_2 },
25 	{ 0x1e03, KEY_3 },
26 	{ 0x1e04, KEY_4 },
27 	{ 0x1e05, KEY_5 },
28 	{ 0x1e06, KEY_6 },
29 	{ 0x1e07, KEY_7 },
30 	{ 0x1e08, KEY_8 },
31 	{ 0x1e09, KEY_9 },
32 	{ 0x1e0a, KEY_KPASTERISK },
33 	{ 0x1e0b, KEY_RED },
34 	{ 0x1e0c, KEY_RADIO },
35 	{ 0x1e0d, KEY_MENU },
36 	{ 0x1e0e, KEY_GRAVE }, /* # */
37 	{ 0x1e0f, KEY_MUTE },
38 	{ 0x1e10, KEY_VOLUMEUP },
39 	{ 0x1e11, KEY_VOLUMEDOWN },
40 	{ 0x1e12, KEY_CHANNEL },
41 	{ 0x1e14, KEY_UP },
42 	{ 0x1e15, KEY_DOWN },
43 	{ 0x1e16, KEY_LEFT },
44 	{ 0x1e17, KEY_RIGHT },
45 	{ 0x1e18, KEY_VIDEO },
46 	{ 0x1e19, KEY_AUDIO },
47 	{ 0x1e1a, KEY_IMAGES },
48 	{ 0x1e1b, KEY_EPG },
49 	{ 0x1e1c, KEY_TV },
50 	{ 0x1e1e, KEY_NEXT },
51 	{ 0x1e1f, KEY_BACK },
52 	{ 0x1e20, KEY_CHANNELUP },
53 	{ 0x1e21, KEY_CHANNELDOWN },
54 	{ 0x1e24, KEY_LAST }, /* Skip backwards */
55 	{ 0x1e25, KEY_OK },
56 	{ 0x1e29, KEY_BLUE},
57 	{ 0x1e2e, KEY_GREEN },
58 	{ 0x1e30, KEY_PAUSE },
59 	{ 0x1e32, KEY_REWIND },
60 	{ 0x1e34, KEY_FASTFORWARD },
61 	{ 0x1e35, KEY_PLAY },
62 	{ 0x1e36, KEY_STOP },
63 	{ 0x1e37, KEY_RECORD },
64 	{ 0x1e38, KEY_YELLOW },
65 	{ 0x1e3b, KEY_GOTO },
66 	{ 0x1e3d, KEY_POWER },
67 };
68 
69 /* Firmware bug? sometimes, when a new key is pressed, the previous pressed key
70  * is delivered. No workaround yet, maybe a new firmware.
71  */
72 static int nova_t_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
73 {
74 	u8 *buf, data, toggle, custom;
75 	u16 raw;
76 	int i, ret;
77 	struct dibusb_device_state *st = d->priv;
78 
79 	buf = kmalloc(5, GFP_KERNEL);
80 	if (!buf)
81 		return -ENOMEM;
82 
83 	buf[0] = DIBUSB_REQ_POLL_REMOTE;
84 	buf[1] = 0x35;
85 	ret = dvb_usb_generic_rw(d, buf, 2, buf, 5, 0);
86 	if (ret < 0)
87 		goto ret;
88 
89 	*state = REMOTE_NO_KEY_PRESSED;
90 	switch (buf[0]) {
91 		case DIBUSB_RC_HAUPPAUGE_KEY_PRESSED:
92 			raw = ((buf[1] << 8) | buf[2]) >> 3;
93 			toggle = !!(raw & 0x800);
94 			data = raw & 0x3f;
95 			custom = (raw >> 6) & 0x1f;
96 
97 			deb_rc("raw key code 0x%02x, 0x%02x, 0x%02x to c: %02x d: %02x toggle: %d\n",
98 			       buf[1], buf[2], buf[3], custom, data, toggle);
99 
100 			for (i = 0; i < ARRAY_SIZE(rc_map_haupp_table); i++) {
101 				if (rc5_data(&rc_map_haupp_table[i]) == data &&
102 					rc5_custom(&rc_map_haupp_table[i]) == custom) {
103 
104 					deb_rc("c: %x, d: %x\n", rc5_data(&rc_map_haupp_table[i]),
105 								 rc5_custom(&rc_map_haupp_table[i]));
106 
107 					*event = rc_map_haupp_table[i].keycode;
108 					*state = REMOTE_KEY_PRESSED;
109 					if (st->old_toggle == toggle) {
110 						if (st->last_repeat_count++ < 2)
111 							*state = REMOTE_NO_KEY_PRESSED;
112 					} else {
113 						st->last_repeat_count = 0;
114 						st->old_toggle = toggle;
115 					}
116 					break;
117 				}
118 			}
119 
120 			break;
121 		case DIBUSB_RC_HAUPPAUGE_KEY_EMPTY:
122 		default:
123 			break;
124 	}
125 
126 ret:
127 	kfree(buf);
128 	return ret;
129 }
130 
131 static int nova_t_read_mac_address (struct dvb_usb_device *d, u8 mac[6])
132 {
133 	int i;
134 	u8 b;
135 
136 	mac[0] = 0x00;
137 	mac[1] = 0x0d;
138 	mac[2] = 0xfe;
139 
140 	/* this is a complete guess, but works for my box */
141 	for (i = 136; i < 139; i++) {
142 		dibusb_read_eeprom_byte(d,i, &b);
143 
144 		mac[5 - (i - 136)] = b;
145 	}
146 
147 	return 0;
148 }
149 
150 /* USB Driver stuff */
151 static struct dvb_usb_device_properties nova_t_properties;
152 
153 static int nova_t_probe(struct usb_interface *intf,
154 		const struct usb_device_id *id)
155 {
156 	return dvb_usb_device_init(intf, &nova_t_properties,
157 				   THIS_MODULE, NULL, adapter_nr);
158 }
159 
160 /* do not change the order of the ID table */
161 static struct usb_device_id nova_t_table [] = {
162 /* 00 */	{ USB_DEVICE(USB_VID_HAUPPAUGE,     USB_PID_WINTV_NOVA_T_USB2_COLD) },
163 /* 01 */	{ USB_DEVICE(USB_VID_HAUPPAUGE,     USB_PID_WINTV_NOVA_T_USB2_WARM) },
164 			{ }		/* Terminating entry */
165 };
166 MODULE_DEVICE_TABLE(usb, nova_t_table);
167 
168 static struct dvb_usb_device_properties nova_t_properties = {
169 	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
170 
171 	.usb_ctrl = CYPRESS_FX2,
172 	.firmware = "dvb-usb-nova-t-usb2-02.fw",
173 
174 	.num_adapters     = 1,
175 	.adapter          = {
176 		{
177 		.num_frontends = 1,
178 		.fe = {{
179 			.caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
180 			.pid_filter_count = 32,
181 
182 			.streaming_ctrl   = dibusb2_0_streaming_ctrl,
183 			.pid_filter       = dibusb_pid_filter,
184 			.pid_filter_ctrl  = dibusb_pid_filter_ctrl,
185 			.frontend_attach  = dibusb_dib3000mc_frontend_attach,
186 			.tuner_attach     = dibusb_dib3000mc_tuner_attach,
187 
188 			/* parameter for the MPEG2-data transfer */
189 					.stream = {
190 						.type = USB_BULK,
191 				.count = 7,
192 				.endpoint = 0x06,
193 				.u = {
194 					.bulk = {
195 						.buffersize = 4096,
196 					}
197 				}
198 			},
199 		}},
200 			.size_of_priv     = sizeof(struct dibusb_state),
201 		}
202 	},
203 	.size_of_priv     = sizeof(struct dibusb_device_state),
204 
205 	.power_ctrl       = dibusb2_0_power_ctrl,
206 	.read_mac_address = nova_t_read_mac_address,
207 
208 	.rc.legacy = {
209 		.rc_interval      = 100,
210 		.rc_map_table     = rc_map_haupp_table,
211 		.rc_map_size      = ARRAY_SIZE(rc_map_haupp_table),
212 		.rc_query         = nova_t_rc_query,
213 	},
214 
215 	.i2c_algo         = &dibusb_i2c_algo,
216 
217 	.generic_bulk_ctrl_endpoint = 0x01,
218 
219 	.num_device_descs = 1,
220 	.devices = {
221 		{   "Hauppauge WinTV-NOVA-T usb2",
222 			{ &nova_t_table[0], NULL },
223 			{ &nova_t_table[1], NULL },
224 		},
225 		{ NULL },
226 	}
227 };
228 
229 static struct usb_driver nova_t_driver = {
230 	.name		= "dvb_usb_nova_t_usb2",
231 	.probe		= nova_t_probe,
232 	.disconnect = dvb_usb_device_exit,
233 	.id_table	= nova_t_table,
234 };
235 
236 module_usb_driver(nova_t_driver);
237 
238 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
239 MODULE_DESCRIPTION("Hauppauge WinTV-NOVA-T usb2");
240 MODULE_VERSION("1.0");
241 MODULE_LICENSE("GPL");
242