Merge branch 'tracing/urgent' into tracing/core
[linux-2.6] / drivers / media / dvb / dvb-usb / dw2102.c
1 /* DVB USB framework compliant Linux driver for the
2 *       DVBWorld DVB-S 2101, 2102, DVB-S2 2104 Card
3 *
4 * Copyright (C) 2008 Igor M. Liplianin (liplianin@me.by)
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
8 *       Free Software Foundation, version 2.
9 *
10 * see Documentation/dvb/README.dvb-usb for more information
11 */
12 #include "dw2102.h"
13 #include "si21xx.h"
14 #include "stv0299.h"
15 #include "z0194a.h"
16 #include "stv0288.h"
17 #include "stb6000.h"
18 #include "eds1547.h"
19 #include "cx24116.h"
20
21 #ifndef USB_PID_DW2102
22 #define USB_PID_DW2102 0x2102
23 #endif
24
25 #ifndef USB_PID_DW2104
26 #define USB_PID_DW2104 0x2104
27 #endif
28
29 #ifndef USB_PID_CINERGY_S
30 #define USB_PID_CINERGY_S 0x0064
31 #endif
32
33 #define DW210X_READ_MSG 0
34 #define DW210X_WRITE_MSG 1
35
36 #define REG_1F_SYMBOLRATE_BYTE0 0x1f
37 #define REG_20_SYMBOLRATE_BYTE1 0x20
38 #define REG_21_SYMBOLRATE_BYTE2 0x21
39 /* on my own*/
40 #define DW2102_VOLTAGE_CTRL (0x1800)
41 #define DW2102_RC_QUERY (0x1a00)
42
43 struct dw210x_state {
44         u32 last_key_pressed;
45 };
46 struct dw210x_rc_keys {
47         u32 keycode;
48         u32 event;
49 };
50
51 /* debug */
52 static int dvb_usb_dw2102_debug;
53 module_param_named(debug, dvb_usb_dw2102_debug, int, 0644);
54 MODULE_PARM_DESC(debug, "set debugging level (1=info 2=xfer (or-able))." DVB_USB_DEBUG_STATUS);
55
56 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
57
58 static int dw210x_op_rw(struct usb_device *dev, u8 request, u16 value,
59                         u16 index, u8 * data, u16 len, int flags)
60 {
61         int ret;
62         u8 u8buf[len];
63
64         unsigned int pipe = (flags == DW210X_READ_MSG) ?
65                                 usb_rcvctrlpipe(dev, 0) : usb_sndctrlpipe(dev, 0);
66         u8 request_type = (flags == DW210X_READ_MSG) ? USB_DIR_IN : USB_DIR_OUT;
67
68         if (flags == DW210X_WRITE_MSG)
69                 memcpy(u8buf, data, len);
70         ret = usb_control_msg(dev, pipe, request, request_type | USB_TYPE_VENDOR,
71                                 value, index , u8buf, len, 2000);
72
73         if (flags == DW210X_READ_MSG)
74                 memcpy(data, u8buf, len);
75         return ret;
76 }
77
78 /* I2C */
79 static int dw2102_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
80                 int num)
81 {
82 struct dvb_usb_device *d = i2c_get_adapdata(adap);
83         int i = 0, ret = 0;
84         u8 buf6[] = {0x2c, 0x05, 0xc0, 0, 0, 0, 0};
85         u16 value;
86
87         if (!d)
88                 return -ENODEV;
89         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
90                 return -EAGAIN;
91
92         switch (num) {
93         case 2:
94                 /* read stv0299 register */
95                 value = msg[0].buf[0];/* register */
96                 for (i = 0; i < msg[1].len; i++) {
97                         value = value + i;
98                         ret = dw210x_op_rw(d->udev, 0xb5, value, 0,
99                                         buf6, 2, DW210X_READ_MSG);
100                         msg[1].buf[i] = buf6[0];
101                 }
102                 break;
103         case 1:
104                 switch (msg[0].addr) {
105                 case 0x68:
106                         /* write to stv0299 register */
107                         buf6[0] = 0x2a;
108                         buf6[1] = msg[0].buf[0];
109                         buf6[2] = msg[0].buf[1];
110                         ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
111                                         buf6, 3, DW210X_WRITE_MSG);
112                         break;
113                 case 0x60:
114                         if (msg[0].flags == 0) {
115                         /* write to tuner pll */
116                                 buf6[0] = 0x2c;
117                                 buf6[1] = 5;
118                                 buf6[2] = 0xc0;
119                                 buf6[3] = msg[0].buf[0];
120                                 buf6[4] = msg[0].buf[1];
121                                 buf6[5] = msg[0].buf[2];
122                                 buf6[6] = msg[0].buf[3];
123                                 ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
124                                                 buf6, 7, DW210X_WRITE_MSG);
125                         } else {
126                         /* read from tuner */
127                                 ret = dw210x_op_rw(d->udev, 0xb5, 0, 0,
128                                                 buf6, 1, DW210X_READ_MSG);
129                                 msg[0].buf[0] = buf6[0];
130                         }
131                         break;
132                 case (DW2102_RC_QUERY):
133                         ret  = dw210x_op_rw(d->udev, 0xb8, 0, 0,
134                                         buf6, 2, DW210X_READ_MSG);
135                         msg[0].buf[0] = buf6[0];
136                         msg[0].buf[1] = buf6[1];
137                         break;
138                 case (DW2102_VOLTAGE_CTRL):
139                         buf6[0] = 0x30;
140                         buf6[1] = msg[0].buf[0];
141                         ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
142                                         buf6, 2, DW210X_WRITE_MSG);
143                         break;
144                 }
145
146                 break;
147         }
148
149         mutex_unlock(&d->i2c_mutex);
150         return num;
151 }
152
153 static int dw2102_serit_i2c_transfer(struct i2c_adapter *adap,
154                                                 struct i2c_msg msg[], int num)
155 {
156         struct dvb_usb_device *d = i2c_get_adapdata(adap);
157         int ret = 0;
158         u8 buf6[] = {0, 0, 0, 0, 0, 0, 0};
159
160         if (!d)
161                 return -ENODEV;
162         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
163                 return -EAGAIN;
164
165         switch (num) {
166         case 2:
167                 /* read si2109 register by number */
168                 buf6[0] = 0xd0;
169                 buf6[1] = msg[0].len;
170                 buf6[2] = msg[0].buf[0];
171                 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
172                                 buf6, msg[0].len + 2, DW210X_WRITE_MSG);
173                 /* read si2109 register */
174                 ret = dw210x_op_rw(d->udev, 0xc3, 0xd0, 0,
175                                 buf6, msg[1].len + 2, DW210X_READ_MSG);
176                 memcpy(msg[1].buf, buf6 + 2, msg[1].len);
177
178                 break;
179         case 1:
180                 switch (msg[0].addr) {
181                 case 0x68:
182                         /* write to si2109 register */
183                         buf6[0] = 0xd0;
184                         buf6[1] = msg[0].len;
185                         memcpy(buf6 + 2, msg[0].buf, msg[0].len);
186                         ret = dw210x_op_rw(d->udev, 0xc2, 0, 0, buf6,
187                                         msg[0].len + 2, DW210X_WRITE_MSG);
188                         break;
189                 case(DW2102_RC_QUERY):
190                         ret  = dw210x_op_rw(d->udev, 0xb8, 0, 0,
191                                         buf6, 2, DW210X_READ_MSG);
192                         msg[0].buf[0] = buf6[0];
193                         msg[0].buf[1] = buf6[1];
194                         break;
195                 case(DW2102_VOLTAGE_CTRL):
196                         buf6[0] = 0x30;
197                         buf6[1] = msg[0].buf[0];
198                         ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
199                                         buf6, 2, DW210X_WRITE_MSG);
200                         break;
201                 }
202                 break;
203         }
204
205         mutex_unlock(&d->i2c_mutex);
206         return num;
207 }
208 static int dw2102_earda_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
209 {
210         struct dvb_usb_device *d = i2c_get_adapdata(adap);
211         int ret = 0;
212
213         if (!d)
214                 return -ENODEV;
215         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
216                 return -EAGAIN;
217
218         switch (num) {
219         case 2: {
220                 /* read */
221                 /* first write first register number */
222                 u8 ibuf [msg[1].len + 2], obuf[3];
223                 obuf[0] = 0xd0;
224                 obuf[1] = msg[0].len;
225                 obuf[2] = msg[0].buf[0];
226                 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
227                                 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
228                 /* second read registers */
229                 ret = dw210x_op_rw(d->udev, 0xc3, 0xd1 , 0,
230                                 ibuf, msg[1].len + 2, DW210X_READ_MSG);
231                 memcpy(msg[1].buf, ibuf + 2, msg[1].len);
232
233                 break;
234         }
235         case 1:
236                 switch (msg[0].addr) {
237                 case 0x68: {
238                         /* write to register */
239                         u8 obuf[msg[0].len + 2];
240                         obuf[0] = 0xd0;
241                         obuf[1] = msg[0].len;
242                         memcpy(obuf + 2, msg[0].buf, msg[0].len);
243                         ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
244                                         obuf, msg[0].len + 2, DW210X_WRITE_MSG);
245                         break;
246                 }
247                 case 0x61: {
248                         /* write to tuner */
249                         u8 obuf[msg[0].len + 2];
250                         obuf[0] = 0xc2;
251                         obuf[1] = msg[0].len;
252                         memcpy(obuf + 2, msg[0].buf, msg[0].len);
253                         ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
254                                         obuf, msg[0].len + 2, DW210X_WRITE_MSG);
255                         break;
256                 }
257                 case(DW2102_RC_QUERY): {
258                         u8 ibuf[2];
259                         ret  = dw210x_op_rw(d->udev, 0xb8, 0, 0,
260                                         ibuf, 2, DW210X_READ_MSG);
261                         memcpy(msg[0].buf, ibuf , 2);
262                         break;
263                 }
264                 case(DW2102_VOLTAGE_CTRL): {
265                         u8 obuf[2];
266                         obuf[0] = 0x30;
267                         obuf[1] = msg[0].buf[0];
268                         ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
269                                         obuf, 2, DW210X_WRITE_MSG);
270                         break;
271                 }
272                 }
273
274                 break;
275         }
276
277         mutex_unlock(&d->i2c_mutex);
278         return num;
279 }
280
281 static int dw2104_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
282 {
283         struct dvb_usb_device *d = i2c_get_adapdata(adap);
284         int ret = 0;
285         int len, i;
286
287         if (!d)
288                 return -ENODEV;
289         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
290                 return -EAGAIN;
291
292         switch (num) {
293         case 2: {
294                 /* read */
295                 /* first write first register number */
296                 u8 ibuf [msg[1].len + 2], obuf[3];
297                 obuf[0] = 0xaa;
298                 obuf[1] = msg[0].len;
299                 obuf[2] = msg[0].buf[0];
300                 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
301                                 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
302                 /* second read registers */
303                 ret = dw210x_op_rw(d->udev, 0xc3, 0xab , 0,
304                                 ibuf, msg[1].len + 2, DW210X_READ_MSG);
305                 memcpy(msg[1].buf, ibuf + 2, msg[1].len);
306
307                 break;
308         }
309         case 1:
310                 switch (msg[0].addr) {
311                 case 0x55: {
312                         if (msg[0].buf[0] == 0xf7) {
313                                 /* firmware */
314                                 /* Write in small blocks */
315                                 u8 obuf[19];
316                                 obuf[0] = 0xaa;
317                                 obuf[1] = 0x11;
318                                 obuf[2] = 0xf7;
319                                 len = msg[0].len - 1;
320                                 i = 1;
321                                 do {
322                                         memcpy(obuf + 3, msg[0].buf + i, (len > 16 ? 16 : len));
323                                         ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
324                                                 obuf, (len > 16 ? 16 : len) + 3, DW210X_WRITE_MSG);
325                                         i += 16;
326                                         len -= 16;
327                                 } while (len > 0);
328                         } else {
329                                 /* write to register */
330                                 u8 obuf[msg[0].len + 2];
331                                 obuf[0] = 0xaa;
332                                 obuf[1] = msg[0].len;
333                                 memcpy(obuf + 2, msg[0].buf, msg[0].len);
334                                 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
335                                                 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
336                         }
337                         break;
338                 }
339                 case(DW2102_RC_QUERY): {
340                         u8 ibuf[2];
341                         ret  = dw210x_op_rw(d->udev, 0xb8, 0, 0,
342                                         ibuf, 2, DW210X_READ_MSG);
343                         memcpy(msg[0].buf, ibuf , 2);
344                         break;
345                 }
346                 case(DW2102_VOLTAGE_CTRL): {
347                         u8 obuf[2];
348                         obuf[0] = 0x30;
349                         obuf[1] = msg[0].buf[0];
350                         ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
351                                         obuf, 2, DW210X_WRITE_MSG);
352                         break;
353                 }
354                 }
355
356                 break;
357         }
358
359         mutex_unlock(&d->i2c_mutex);
360         return num;
361 }
362
363 static u32 dw210x_i2c_func(struct i2c_adapter *adapter)
364 {
365         return I2C_FUNC_I2C;
366 }
367
368 static struct i2c_algorithm dw2102_i2c_algo = {
369         .master_xfer = dw2102_i2c_transfer,
370         .functionality = dw210x_i2c_func,
371 };
372
373 static struct i2c_algorithm dw2102_serit_i2c_algo = {
374         .master_xfer = dw2102_serit_i2c_transfer,
375         .functionality = dw210x_i2c_func,
376 };
377
378 static struct i2c_algorithm dw2102_earda_i2c_algo = {
379         .master_xfer = dw2102_earda_i2c_transfer,
380         .functionality = dw210x_i2c_func,
381 };
382
383 static struct i2c_algorithm dw2104_i2c_algo = {
384         .master_xfer = dw2104_i2c_transfer,
385         .functionality = dw210x_i2c_func,
386 };
387
388 static int dw210x_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
389 {
390         int i;
391         u8 ibuf[] = {0, 0};
392         u8 eeprom[256], eepromline[16];
393
394         for (i = 0; i < 256; i++) {
395                 if (dw210x_op_rw(d->udev, 0xb6, 0xa0 , i, ibuf, 2, DW210X_READ_MSG) < 0) {
396                         err("read eeprom failed.");
397                         return -1;
398                 } else {
399                         eepromline[i%16] = ibuf[0];
400                         eeprom[i] = ibuf[0];
401                 }
402                 if ((i % 16) == 15) {
403                         deb_xfer("%02x: ", i - 15);
404                         debug_dump(eepromline, 16, deb_xfer);
405                 }
406         }
407         memcpy(mac, eeprom + 8, 6);
408         return 0;
409 };
410
411 static int dw210x_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
412 {
413         static u8 command_13v[1] = {0x00};
414         static u8 command_18v[1] = {0x01};
415         struct i2c_msg msg[] = {
416                 {.addr = DW2102_VOLTAGE_CTRL, .flags = 0,
417                         .buf = command_13v, .len = 1},
418         };
419
420         struct dvb_usb_adapter *udev_adap =
421                 (struct dvb_usb_adapter *)(fe->dvb->priv);
422         if (voltage == SEC_VOLTAGE_18)
423                 msg[0].buf = command_18v;
424         i2c_transfer(&udev_adap->dev->i2c_adap, msg, 1);
425         return 0;
426 }
427
428 static struct stv0299_config sharp_z0194a_config = {
429         .demod_address = 0x68,
430         .inittab = sharp_z0194a_inittab,
431         .mclk = 88000000UL,
432         .invert = 1,
433         .skip_reinit = 0,
434         .lock_output = STV0299_LOCKOUTPUT_1,
435         .volt13_op0_op1 = STV0299_VOLT13_OP1,
436         .min_delay_ms = 100,
437         .set_symbol_rate = sharp_z0194a_set_symbol_rate,
438 };
439
440 static struct cx24116_config dw2104_config = {
441         .demod_address = 0x55,
442         .mpg_clk_pos_pol = 0x01,
443 };
444
445 static struct si21xx_config serit_sp1511lhb_config = {
446         .demod_address = 0x68,
447         .min_delay_ms = 100,
448
449 };
450
451 static int dw2104_frontend_attach(struct dvb_usb_adapter *d)
452 {
453         if ((d->fe = dvb_attach(cx24116_attach, &dw2104_config,
454                         &d->dev->i2c_adap)) != NULL) {
455                 d->fe->ops.set_voltage = dw210x_set_voltage;
456                 info("Attached cx24116!\n");
457                 return 0;
458         }
459         return -EIO;
460 }
461
462 static struct dvb_usb_device_properties dw2102_properties;
463
464 static int dw2102_frontend_attach(struct dvb_usb_adapter *d)
465 {
466         if (dw2102_properties.i2c_algo == &dw2102_serit_i2c_algo) {
467                 /*dw2102_properties.adapter->tuner_attach = NULL;*/
468                 d->fe = dvb_attach(si21xx_attach, &serit_sp1511lhb_config,
469                                         &d->dev->i2c_adap);
470                 if (d->fe != NULL) {
471                         d->fe->ops.set_voltage = dw210x_set_voltage;
472                         info("Attached si21xx!\n");
473                         return 0;
474                 }
475         }
476         if (dw2102_properties.i2c_algo == &dw2102_earda_i2c_algo) {
477                 /*dw2102_properties.adapter->tuner_attach = dw2102_tuner_attach;*/
478                 d->fe = dvb_attach(stv0288_attach, &earda_config,
479                                         &d->dev->i2c_adap);
480                 if (d->fe != NULL) {
481                         d->fe->ops.set_voltage = dw210x_set_voltage;
482                         info("Attached stv0288!\n");
483                         return 0;
484                 }
485         }
486
487         if (dw2102_properties.i2c_algo == &dw2102_i2c_algo) {
488                 /*dw2102_properties.adapter->tuner_attach = dw2102_tuner_attach;*/
489                 d->fe = dvb_attach(stv0299_attach, &sharp_z0194a_config,
490                                         &d->dev->i2c_adap);
491                 if (d->fe != NULL) {
492                         d->fe->ops.set_voltage = dw210x_set_voltage;
493                         info("Attached stv0299!\n");
494                         return 0;
495                 }
496         }
497         return -EIO;
498 }
499
500 static int dw2102_tuner_attach(struct dvb_usb_adapter *adap)
501 {
502         dvb_attach(dvb_pll_attach, adap->fe, 0x60,
503                 &adap->dev->i2c_adap, DVB_PLL_OPERA1);
504         return 0;
505 }
506
507 static int dw2102_earda_tuner_attach(struct dvb_usb_adapter *adap)
508 {
509         dvb_attach(stb6000_attach, adap->fe, 0x61,
510                 &adap->dev->i2c_adap);
511
512         return 0;
513 }
514
515 static struct dvb_usb_rc_key dw210x_rc_keys[] = {
516         { 0xf8, 0x0a, KEY_Q },          /*power*/
517         { 0xf8, 0x0c, KEY_M },          /*mute*/
518         { 0xf8, 0x11, KEY_1 },
519         { 0xf8, 0x12, KEY_2 },
520         { 0xf8, 0x13, KEY_3 },
521         { 0xf8, 0x14, KEY_4 },
522         { 0xf8, 0x15, KEY_5 },
523         { 0xf8, 0x16, KEY_6 },
524         { 0xf8, 0x17, KEY_7 },
525         { 0xf8, 0x18, KEY_8 },
526         { 0xf8, 0x19, KEY_9 },
527         { 0xf8, 0x10, KEY_0 },
528         { 0xf8, 0x1c, KEY_PAGEUP },     /*ch+*/
529         { 0xf8, 0x0f, KEY_PAGEDOWN },   /*ch-*/
530         { 0xf8, 0x1a, KEY_O },          /*vol+*/
531         { 0xf8, 0x0e, KEY_Z },          /*vol-*/
532         { 0xf8, 0x04, KEY_R },          /*rec*/
533         { 0xf8, 0x09, KEY_D },          /*fav*/
534         { 0xf8, 0x08, KEY_BACKSPACE },  /*rewind*/
535         { 0xf8, 0x07, KEY_A },          /*fast*/
536         { 0xf8, 0x0b, KEY_P },          /*pause*/
537         { 0xf8, 0x02, KEY_ESC },        /*cancel*/
538         { 0xf8, 0x03, KEY_G },          /*tab*/
539         { 0xf8, 0x00, KEY_UP },         /*up*/
540         { 0xf8, 0x1f, KEY_ENTER },      /*ok*/
541         { 0xf8, 0x01, KEY_DOWN },       /*down*/
542         { 0xf8, 0x05, KEY_C },          /*cap*/
543         { 0xf8, 0x06, KEY_S },          /*stop*/
544         { 0xf8, 0x40, KEY_F },          /*full*/
545         { 0xf8, 0x1e, KEY_W },          /*tvmode*/
546         { 0xf8, 0x1b, KEY_B },          /*recall*/
547
548 };
549
550
551
552 static int dw2102_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
553 {
554         struct dw210x_state *st = d->priv;
555         u8 key[2];
556         struct i2c_msg msg[] = {
557                 {.addr = DW2102_RC_QUERY, .flags = I2C_M_RD, .buf = key,
558                 .len = 2},
559         };
560         int i;
561
562         *state = REMOTE_NO_KEY_PRESSED;
563         if (dw2102_i2c_transfer(&d->i2c_adap, msg, 1) == 1) {
564                 for (i = 0; i < ARRAY_SIZE(dw210x_rc_keys); i++) {
565                         if (dw210x_rc_keys[i].data == msg[0].buf[0]) {
566                                 *state = REMOTE_KEY_PRESSED;
567                                 *event = dw210x_rc_keys[i].event;
568                                 st->last_key_pressed =
569                                         dw210x_rc_keys[i].event;
570                                 break;
571                         }
572                 st->last_key_pressed = 0;
573                 }
574         }
575         /* info("key: %x %x\n",key[0],key[1]); */
576         return 0;
577 }
578
579 static struct usb_device_id dw2102_table[] = {
580         {USB_DEVICE(USB_VID_CYPRESS, USB_PID_DW2102)},
581         {USB_DEVICE(USB_VID_CYPRESS, 0x2101)},
582         {USB_DEVICE(USB_VID_CYPRESS, 0x2104)},
583         {USB_DEVICE(0x9022, 0xd650)},
584         {USB_DEVICE(USB_VID_TERRATEC, USB_PID_CINERGY_S)},
585         { }
586 };
587
588 MODULE_DEVICE_TABLE(usb, dw2102_table);
589
590 static int dw2102_load_firmware(struct usb_device *dev,
591                         const struct firmware *frmwr)
592 {
593         u8 *b, *p;
594         int ret = 0, i;
595         u8 reset;
596         u8 reset16[] = {0, 0, 0, 0, 0, 0, 0};
597         const struct firmware *fw;
598         const char *filename = "dvb-usb-dw2101.fw";
599         switch (dev->descriptor.idProduct) {
600         case 0x2101:
601                 ret = request_firmware(&fw, filename, &dev->dev);
602                 if (ret != 0) {
603                         err("did not find the firmware file. (%s) "
604                         "Please see linux/Documentation/dvb/ for more details "
605                         "on firmware-problems.", filename);
606                         return ret;
607                 }
608                 break;
609         default:
610                 fw = frmwr;
611                 break;
612         }
613         info("start downloading DW210X firmware");
614         p = kmalloc(fw->size, GFP_KERNEL);
615         reset = 1;
616         /*stop the CPU*/
617         dw210x_op_rw(dev, 0xa0, 0x7f92, 0, &reset, 1, DW210X_WRITE_MSG);
618         dw210x_op_rw(dev, 0xa0, 0xe600, 0, &reset, 1, DW210X_WRITE_MSG);
619
620         if (p != NULL) {
621                 memcpy(p, fw->data, fw->size);
622                 for (i = 0; i < fw->size; i += 0x40) {
623                         b = (u8 *) p + i;
624                         if (dw210x_op_rw(dev, 0xa0, i, 0, b , 0x40,
625                                         DW210X_WRITE_MSG) != 0x40) {
626                                 err("error while transferring firmware");
627                                 ret = -EINVAL;
628                                 break;
629                         }
630                 }
631                 /* restart the CPU */
632                 reset = 0;
633                 if (ret || dw210x_op_rw(dev, 0xa0, 0x7f92, 0, &reset, 1,
634                                         DW210X_WRITE_MSG) != 1) {
635                         err("could not restart the USB controller CPU.");
636                         ret = -EINVAL;
637                 }
638                 if (ret || dw210x_op_rw(dev, 0xa0, 0xe600, 0, &reset, 1,
639                                         DW210X_WRITE_MSG) != 1) {
640                         err("could not restart the USB controller CPU.");
641                         ret = -EINVAL;
642                 }
643                 /* init registers */
644                 switch (dev->descriptor.idProduct) {
645                 case USB_PID_DW2104:
646                 case 0xd650:
647                         reset = 1;
648                         dw210x_op_rw(dev, 0xc4, 0x0000, 0, &reset, 1,
649                                         DW210X_WRITE_MSG);
650                         reset = 0;
651                         dw210x_op_rw(dev, 0xbf, 0x0040, 0, &reset, 0,
652                                         DW210X_WRITE_MSG);
653                         break;
654                 case USB_PID_CINERGY_S:
655                 case USB_PID_DW2102:
656                         dw210x_op_rw(dev, 0xbf, 0x0040, 0, &reset, 0,
657                                         DW210X_WRITE_MSG);
658                         dw210x_op_rw(dev, 0xb9, 0x0000, 0, &reset16[0], 2,
659                                         DW210X_READ_MSG);
660                         /* check STV0299 frontend  */
661                         dw210x_op_rw(dev, 0xb5, 0, 0, &reset16[0], 2,
662                                         DW210X_READ_MSG);
663                         if ((reset16[0] == 0xa1) || (reset16[0] == 0x80)) {
664                                 dw2102_properties.i2c_algo = &dw2102_i2c_algo;
665                                 dw2102_properties.adapter->tuner_attach = &dw2102_tuner_attach;
666                                 break;
667                         } else {
668                                 /* check STV0288 frontend  */
669                                 reset16[0] = 0xd0;
670                                 reset16[1] = 1;
671                                 reset16[2] = 0;
672                                 dw210x_op_rw(dev, 0xc2, 0, 0, &reset16[0], 3,
673                                                 DW210X_WRITE_MSG);
674                                 dw210x_op_rw(dev, 0xc3, 0xd1, 0, &reset16[0], 3,
675                                                 DW210X_READ_MSG);
676                                 if (reset16[2] == 0x11) {
677                                         dw2102_properties.i2c_algo = &dw2102_earda_i2c_algo;
678                                         dw2102_properties.adapter->tuner_attach = &dw2102_earda_tuner_attach;
679                                         break;
680                                 }
681                         }
682                 case 0x2101:
683                         dw210x_op_rw(dev, 0xbc, 0x0030, 0, &reset16[0], 2,
684                                         DW210X_READ_MSG);
685                         dw210x_op_rw(dev, 0xba, 0x0000, 0, &reset16[0], 7,
686                                         DW210X_READ_MSG);
687                         dw210x_op_rw(dev, 0xba, 0x0000, 0, &reset16[0], 7,
688                                         DW210X_READ_MSG);
689                         dw210x_op_rw(dev, 0xb9, 0x0000, 0, &reset16[0], 2,
690                                         DW210X_READ_MSG);
691                         break;
692                 }
693                 msleep(100);
694                 kfree(p);
695         }
696         return ret;
697 }
698
699 static struct dvb_usb_device_properties dw2102_properties = {
700         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
701         .usb_ctrl = DEVICE_SPECIFIC,
702         .firmware = "dvb-usb-dw2102.fw",
703         .size_of_priv = sizeof(struct dw210x_state),
704         .no_reconnect = 1,
705
706         .i2c_algo = &dw2102_serit_i2c_algo,
707         .rc_key_map = dw210x_rc_keys,
708         .rc_key_map_size = ARRAY_SIZE(dw210x_rc_keys),
709         .rc_interval = 150,
710         .rc_query = dw2102_rc_query,
711
712         .generic_bulk_ctrl_endpoint = 0x81,
713         /* parameter for the MPEG2-data transfer */
714         .num_adapters = 1,
715         .download_firmware = dw2102_load_firmware,
716         .read_mac_address = dw210x_read_mac_address,
717                 .adapter = {
718                 {
719                         .frontend_attach = dw2102_frontend_attach,
720                         .streaming_ctrl = NULL,
721                         .tuner_attach = NULL,
722                         .stream = {
723                                 .type = USB_BULK,
724                                 .count = 8,
725                                 .endpoint = 0x82,
726                                 .u = {
727                                         .bulk = {
728                                                 .buffersize = 4096,
729                                         }
730                                 }
731                         },
732                 }
733         },
734         .num_device_descs = 3,
735         .devices = {
736                 {"DVBWorld DVB-S 2102 USB2.0",
737                         {&dw2102_table[0], NULL},
738                         {NULL},
739                 },
740                 {"DVBWorld DVB-S 2101 USB2.0",
741                         {&dw2102_table[1], NULL},
742                         {NULL},
743                 },
744                 {"TerraTec Cinergy S USB",
745                         {&dw2102_table[4], NULL},
746                         {NULL},
747                 },
748         }
749 };
750
751 static struct dvb_usb_device_properties dw2104_properties = {
752         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
753         .usb_ctrl = DEVICE_SPECIFIC,
754         .firmware = "dvb-usb-dw2104.fw",
755         .size_of_priv = sizeof(struct dw210x_state),
756         .no_reconnect = 1,
757
758         .i2c_algo = &dw2104_i2c_algo,
759         .rc_key_map = dw210x_rc_keys,
760         .rc_key_map_size = ARRAY_SIZE(dw210x_rc_keys),
761         .rc_interval = 150,
762         .rc_query = dw2102_rc_query,
763
764         .generic_bulk_ctrl_endpoint = 0x81,
765         /* parameter for the MPEG2-data transfer */
766         .num_adapters = 1,
767         .download_firmware = dw2102_load_firmware,
768         .read_mac_address = dw210x_read_mac_address,
769         .adapter = {
770                 {
771                         .frontend_attach = dw2104_frontend_attach,
772                         .streaming_ctrl = NULL,
773                         /*.tuner_attach = dw2104_tuner_attach,*/
774                         .stream = {
775                                 .type = USB_BULK,
776                                 .count = 8,
777                                 .endpoint = 0x82,
778                                 .u = {
779                                         .bulk = {
780                                                 .buffersize = 4096,
781                                         }
782                                 }
783                         },
784                 }
785         },
786         .num_device_descs = 2,
787         .devices = {
788                 { "DVBWorld DW2104 USB2.0",
789                         {&dw2102_table[2], NULL},
790                         {NULL},
791                 },
792                 { "TeVii S650 USB2.0",
793                         {&dw2102_table[3], NULL},
794                         {NULL},
795                 },
796         }
797 };
798
799 static int dw2102_probe(struct usb_interface *intf,
800                 const struct usb_device_id *id)
801 {
802         if (0 == dvb_usb_device_init(intf, &dw2102_properties,
803                         THIS_MODULE, NULL, adapter_nr) ||
804             0 == dvb_usb_device_init(intf, &dw2104_properties,
805                         THIS_MODULE, NULL, adapter_nr)) {
806                 return 0;
807         }
808         return -ENODEV;
809 }
810
811 static struct usb_driver dw2102_driver = {
812         .name = "dw2102",
813         .probe = dw2102_probe,
814         .disconnect = dvb_usb_device_exit,
815         .id_table = dw2102_table,
816 };
817
818 static int __init dw2102_module_init(void)
819 {
820         int ret =  usb_register(&dw2102_driver);
821         if (ret)
822                 err("usb_register failed. Error number %d", ret);
823
824         return ret;
825 }
826
827 static void __exit dw2102_module_exit(void)
828 {
829         usb_deregister(&dw2102_driver);
830 }
831
832 module_init(dw2102_module_init);
833 module_exit(dw2102_module_exit);
834
835 MODULE_AUTHOR("Igor M. Liplianin (c) liplianin@me.by");
836 MODULE_DESCRIPTION("Driver for DVBWorld DVB-S 2101, 2102, DVB-S2 2104 USB2.0 device");
837 MODULE_VERSION("0.1");
838 MODULE_LICENSE("GPL");