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