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/driver-api/media/drivers/dvb-usb.rst for more information
8 */
9#include "dibusb.h"
10
11static int debug;
12module_param(debug, int, 0644);
13MODULE_PARM_DESC(debug, "set debugging level (1=rc,2=eeprom (|-able))." DVB_USB_DEBUG_STATUS);
14
15DVB_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 */
21static 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 */
72static 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
126ret:
127	kfree(buf);
128	return ret;
129}
130
131static int nova_t_read_mac_address (struct dvb_usb_device *d, u8 mac[6])
132{
133	int i, ret;
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		ret = dibusb_read_eeprom_byte(d, i, &b);
143		if (ret)
144			return ret;
145
146		mac[5 - (i - 136)] = b;
147	}
148
149	return 0;
150}
151
152/* USB Driver stuff */
153static struct dvb_usb_device_properties nova_t_properties;
154
155static int nova_t_probe(struct usb_interface *intf,
156		const struct usb_device_id *id)
157{
158	return dvb_usb_device_init(intf, &nova_t_properties,
159				   THIS_MODULE, NULL, adapter_nr);
160}
161
162/* do not change the order of the ID table */
163enum {
164	HAUPPAUGE_WINTV_NOVA_T_USB2_COLD,
165	HAUPPAUGE_WINTV_NOVA_T_USB2_WARM,
166};
167
168static struct usb_device_id nova_t_table[] = {
169	DVB_USB_DEV(HAUPPAUGE, HAUPPAUGE_WINTV_NOVA_T_USB2_COLD),
170	DVB_USB_DEV(HAUPPAUGE, HAUPPAUGE_WINTV_NOVA_T_USB2_WARM),
171	{ }
172};
173
174MODULE_DEVICE_TABLE(usb, nova_t_table);
175
176static struct dvb_usb_device_properties nova_t_properties = {
177	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
178
179	.usb_ctrl = CYPRESS_FX2,
180	.firmware = "dvb-usb-nova-t-usb2-02.fw",
181
182	.num_adapters     = 1,
183	.adapter          = {
184		{
185		.num_frontends = 1,
186		.fe = {{
187			.caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
188			.pid_filter_count = 32,
189
190			.streaming_ctrl   = dibusb2_0_streaming_ctrl,
191			.pid_filter       = dibusb_pid_filter,
192			.pid_filter_ctrl  = dibusb_pid_filter_ctrl,
193			.frontend_attach  = dibusb_dib3000mc_frontend_attach,
194			.tuner_attach     = dibusb_dib3000mc_tuner_attach,
195
196			/* parameter for the MPEG2-data transfer */
197					.stream = {
198						.type = USB_BULK,
199				.count = 7,
200				.endpoint = 0x06,
201				.u = {
202					.bulk = {
203						.buffersize = 4096,
204					}
205				}
206			},
207		}},
208			.size_of_priv     = sizeof(struct dibusb_state),
209		}
210	},
211	.size_of_priv     = sizeof(struct dibusb_device_state),
212
213	.power_ctrl       = dibusb2_0_power_ctrl,
214	.read_mac_address = nova_t_read_mac_address,
215
216	.rc.legacy = {
217		.rc_interval      = 100,
218		.rc_map_table     = rc_map_haupp_table,
219		.rc_map_size      = ARRAY_SIZE(rc_map_haupp_table),
220		.rc_query         = nova_t_rc_query,
221	},
222
223	.i2c_algo         = &dibusb_i2c_algo,
224
225	.generic_bulk_ctrl_endpoint = 0x01,
226
227	.num_device_descs = 1,
228	.devices = {
229		{   "Hauppauge WinTV-NOVA-T usb2",
230			{ &nova_t_table[HAUPPAUGE_WINTV_NOVA_T_USB2_COLD], NULL },
231			{ &nova_t_table[HAUPPAUGE_WINTV_NOVA_T_USB2_WARM], NULL },
232		},
233		{ NULL },
234	}
235};
236
237static struct usb_driver nova_t_driver = {
238	.name		= "dvb_usb_nova_t_usb2",
239	.probe		= nova_t_probe,
240	.disconnect = dvb_usb_device_exit,
241	.id_table	= nova_t_table,
242};
243
244module_usb_driver(nova_t_driver);
245
246MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
247MODULE_DESCRIPTION("Hauppauge WinTV-NOVA-T usb2");
248MODULE_VERSION("1.0");
249MODULE_LICENSE("GPL");
250