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 
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, 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 */
153 static struct dvb_usb_device_properties nova_t_properties;
154 
155 static 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 */
163 static struct usb_device_id nova_t_table [] = {
164 /* 00 */	{ USB_DEVICE(USB_VID_HAUPPAUGE,     USB_PID_WINTV_NOVA_T_USB2_COLD) },
165 /* 01 */	{ USB_DEVICE(USB_VID_HAUPPAUGE,     USB_PID_WINTV_NOVA_T_USB2_WARM) },
166 			{ }		/* Terminating entry */
167 };
168 MODULE_DEVICE_TABLE(usb, nova_t_table);
169 
170 static struct dvb_usb_device_properties nova_t_properties = {
171 	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
172 
173 	.usb_ctrl = CYPRESS_FX2,
174 	.firmware = "dvb-usb-nova-t-usb2-02.fw",
175 
176 	.num_adapters     = 1,
177 	.adapter          = {
178 		{
179 		.num_frontends = 1,
180 		.fe = {{
181 			.caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
182 			.pid_filter_count = 32,
183 
184 			.streaming_ctrl   = dibusb2_0_streaming_ctrl,
185 			.pid_filter       = dibusb_pid_filter,
186 			.pid_filter_ctrl  = dibusb_pid_filter_ctrl,
187 			.frontend_attach  = dibusb_dib3000mc_frontend_attach,
188 			.tuner_attach     = dibusb_dib3000mc_tuner_attach,
189 
190 			/* parameter for the MPEG2-data transfer */
191 					.stream = {
192 						.type = USB_BULK,
193 				.count = 7,
194 				.endpoint = 0x06,
195 				.u = {
196 					.bulk = {
197 						.buffersize = 4096,
198 					}
199 				}
200 			},
201 		}},
202 			.size_of_priv     = sizeof(struct dibusb_state),
203 		}
204 	},
205 	.size_of_priv     = sizeof(struct dibusb_device_state),
206 
207 	.power_ctrl       = dibusb2_0_power_ctrl,
208 	.read_mac_address = nova_t_read_mac_address,
209 
210 	.rc.legacy = {
211 		.rc_interval      = 100,
212 		.rc_map_table     = rc_map_haupp_table,
213 		.rc_map_size      = ARRAY_SIZE(rc_map_haupp_table),
214 		.rc_query         = nova_t_rc_query,
215 	},
216 
217 	.i2c_algo         = &dibusb_i2c_algo,
218 
219 	.generic_bulk_ctrl_endpoint = 0x01,
220 
221 	.num_device_descs = 1,
222 	.devices = {
223 		{   "Hauppauge WinTV-NOVA-T usb2",
224 			{ &nova_t_table[0], NULL },
225 			{ &nova_t_table[1], NULL },
226 		},
227 		{ NULL },
228 	}
229 };
230 
231 static struct usb_driver nova_t_driver = {
232 	.name		= "dvb_usb_nova_t_usb2",
233 	.probe		= nova_t_probe,
234 	.disconnect = dvb_usb_device_exit,
235 	.id_table	= nova_t_table,
236 };
237 
238 module_usb_driver(nova_t_driver);
239 
240 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
241 MODULE_DESCRIPTION("Hauppauge WinTV-NOVA-T usb2");
242 MODULE_VERSION("1.0");
243 MODULE_LICENSE("GPL");
244