1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
|
#include "budget.h"
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,51)
#define KBUILD_MODNAME budget
#endif
int budget_debug = 0;
int budget_patch = 0;
/****************************************************************************
* General helper functions
****************************************************************************/
/* this is videobuf_vmalloc_to_sg() from video-buf.c */
struct scatterlist*
vmalloc_to_sg(unsigned char *virt, int nr_pages)
{
struct scatterlist *sglist;
struct page *pg;
int i;
sglist = kmalloc(sizeof(struct scatterlist)*nr_pages, GFP_KERNEL);
if (NULL == sglist)
return NULL;
memset(sglist,0,sizeof(struct scatterlist)*nr_pages);
for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
pg = vmalloc_to_page(virt);
if (NULL == pg)
goto err;
if (PageHighMem(pg))
BUG();
sglist[i].page = pg;
sglist[i].length = PAGE_SIZE;
}
return sglist;
err:
kfree(sglist);
return NULL;
}
static inline void ddelay(int i)
{
current->state=TASK_INTERRUPTIBLE;
schedule_timeout((HZ*i)/100);
}
/****************************************************************************
* TT budget / WinTV Nova
****************************************************************************/
static int
TTBStop(struct budget_s *budget)
{
DEB_EE(("budget: %p\n",budget));
if (--budget->feeding)
return budget->feeding;
saa7146_write(budget->dev, MC1, MASK_20); // DMA3 off
IER_DISABLE(budget->dev, MASK_07);
return 0;
}
static int
TTBStart(struct budget_s *budget)
{
struct saa7146_dev *dev=budget->dev;
DEB_EE(("budget: %p\n",budget));
if (budget->feeding)
return ++budget->feeding;
saa7146_write(dev, MC1, MASK_20); // DMA3 off
memset(budget->grabbing, 0x00, TS_HEIGHT*TS_WIDTH);
saa7146_write(dev, PCI_BT_V1, 0x001c0000 |
(saa7146_read(dev, PCI_BT_V1) & ~0x001f0000));
budget->tsf=0xff;
budget->ttbp=0;
saa7146_write(dev, DD1_INIT, 0x020006c0);
saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
saa7146_write(dev, BRS_CTRL, 0x60000000);
saa7146_write(dev, MC2, (MASK_08 | MASK_24));
mdelay(10);
saa7146_write(dev, BASE_ODD3, 0);
saa7146_write(dev, BASE_EVEN3, TS_WIDTH*TS_HEIGHT/2);
saa7146_write(dev, PROT_ADDR3, TS_WIDTH*TS_HEIGHT);
saa7146_write(dev, BASE_PAGE3, budget->pt.dma |ME1|0xb0);
saa7146_write(dev, PITCH3, TS_WIDTH);
saa7146_write(dev, NUM_LINE_BYTE3, ((TS_HEIGHT/2)<<16)|TS_WIDTH);
saa7146_write(dev, MC2, (MASK_04 | MASK_20));
saa7146_write(dev, MC1, (MASK_04 | MASK_20)); // DMA3 on
// FIDB
IER_ENABLE(budget->dev, MASK_07);
return ++budget->feeding;
}
/* TS_PACKETS is minumum number of accumulated 188-byte packets
** (TS_SIZE=188) that is considered 'complete' and can
** be delivered to the sw_demux in one 'quantum'.
** TS_QUANTUM is size in bytes of TS_PACKETS
** TS_MAX_PACKETS is number of TS_PACKETS in one DMA window
** TS_BUFLEN is 1024*188
*/
static
void fidbirq (unsigned long data)
{
struct budget_s *budget = (struct budget_s*) data;
u8 *mem=(u8 *)(budget->grabbing);
u8 oldtsf = budget->tsf;
u8 newtsf = 0x80 & saa7146_read(budget->dev, PSR);
u16 oldpkt = budget->ttbp;
u16 newpkt = saa7146_read(budget->dev, PCI_VDP3)/TS_QUANTUM; /* PCI_VDP3 = 0..TS_BUFLEN-1 */
u8 *olddma = mem + oldpkt*TS_QUANTUM; /* rounded to first complete packet */
DEB_EE(("budget: %p\n",budget));
/* Zero-Packet-Loss fidbirq by EMARD */
budget->ttbp = newpkt;
budget->tsf = newtsf;
/* exit immediately when the card is not set for feeding
** or when values are out of expected boundaries
*/
if(budget->feeding == 0 ||
oldpkt >= TS_MAX_PACKETS || newpkt >= TS_MAX_PACKETS)
return;
/* use heuristic methods for recovering from lost interrupt
*/
if(oldtsf == newtsf)
{
/* interrupt is probably lost, some data can't be
** retrieved because of DMA overwriting, but let's
** try to save what is left of good data in the DMA
** window.
*/
if(newpkt != oldpkt)
{ /* DMA is probably in progress right now,
** overwriting data. Shift oldpkt 1 packets
** forward from newpkt as a gap for racing with
** ongoing DMA transfer. Consider TS_MAX_PACKETS-1 packets
** are still valid in DMA window.
*/
oldpkt = (newpkt + 1) % TS_MAX_PACKETS;
olddma = mem + oldpkt*TS_QUANTUM;
/* from now on, proceed as usual */
}
}
if(oldpkt < newpkt)
{ /* no wraparound - dump linear data
** from olddma to newdma
*/
if(*olddma == 0x47)
dvb_dmx_swfilter_packets(&budget->demux, olddma,
TS_PACKETS*(newpkt-oldpkt));
}
else
{ /* wraparound - dump in two chunks,
** from olddma to end of DMA window
** and from begin of DMA window to newdma
*/
if(*olddma == 0x47)
dvb_dmx_swfilter_packets(&budget->demux, olddma,
TS_PACKETS*(TS_MAX_PACKETS - oldpkt));
if(*mem == 0x47 && newpkt > 0)
dvb_dmx_swfilter_packets(&budget->demux, mem,
TS_PACKETS*newpkt);
}
}
inline static void
Set22K(struct budget_s *budget, int state)
{
struct saa7146_dev *dev=budget->dev;
DEB_EE(("budget: %p\n",budget));
saa7146_setgpio(dev, 3, (state ? SAA7146_GPIO_OUTHI : SAA7146_GPIO_OUTLO));
}
/* Diseqc functions only for TT Budget card */
/* taken from the Skyvision DVB driver by
Ralph Metzler <rjkm@metzlerbros.de> */
inline static void
DiseqcSendBit(struct budget_s *budget, int data)
{
struct saa7146_dev *dev=budget->dev;
DEB_EE(("budget: %p\n",budget));
saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI);
udelay(data ? 500 : 1000);
saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
udelay(data ? 1000 : 500);
}
static void
DiseqcSendByte(struct budget_s *budget, int data)
{
int i, par=1, d;
DEB_EE(("budget: %p\n",budget));
for (i=7; i>=0; i--)
{
d=(data>>i)&1;
par^=d;
DiseqcSendBit(budget, d);
}
DiseqcSendBit(budget, par);
}
inline static int
SendDiSEqCMsg(struct budget_s *budget, int len, u8 *msg, int burst)
{
struct saa7146_dev *dev=budget->dev;
int i;
DEB_EE(("budget: %p\n",budget));
saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
mdelay(16);
for (i=0; i<len; i++)
DiseqcSendByte(budget, msg[i]);
mdelay(16);
if (burst!=-1) {
if (burst)
DiseqcSendByte(budget, 0xff);
else {
saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI);
udelay(12500);
saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
}
ddelay(2);
}
return 0;
}
/****************************************************************************
* DVB API SECTION
****************************************************************************/
static int
budget_start_feed(struct dvb_demux_feed *feed)
{
struct dvb_demux *demux = feed->demux;
struct budget_s *budget = (struct budget_s *) demux->priv;
DEB_EE(("budget: %p\n",budget));
if (!demux->dmx.frontend)
return -EINVAL;
return TTBStart(budget);
}
static int
budget_stop_feed(struct dvb_demux_feed *feed)
{
struct dvb_demux *demux = feed->demux;
struct budget_s *budget = (struct budget_s *) demux->priv;
DEB_EE(("budget: %p\n",budget));
return TTBStop(budget);
}
/******************************************************************************
* SEC device file operations
******************************************************************************/
static
int budget_diseqc_ioctl (struct dvb_frontend *fe, unsigned int cmd, void *arg)
{
struct budget_s *budget = fe->before_after_data;
DEB_EE(("budget: %p\n",budget));
switch (cmd) {
case FE_SET_TONE:
switch ((fe_sec_tone_mode_t) arg) {
case SEC_TONE_ON:
Set22K (budget, 1);
break;
case SEC_TONE_OFF:
Set22K (budget, 0);
break;
default:
return -EINVAL;
};
break;
case FE_DISEQC_SEND_MASTER_CMD:
{
struct dvb_diseqc_master_cmd *cmd = arg;
SendDiSEqCMsg (budget, cmd->msg_len, cmd->msg, 0);
break;
}
case FE_DISEQC_SEND_BURST:
SendDiSEqCMsg (budget, 0, NULL, (int) arg);
break;
default:
return -EOPNOTSUPP;
};
return 0;
}
int budget_register(struct budget_s *budget)
{
int ret;
dmx_frontend_t *dvbfront=&budget->hw_frontend;
struct dvb_demux *dvbdemux=&budget->demux;
DEB_EE(("budget: %p\n",budget));
if (budget->registered)
return -1;
budget->registered=1;
/* init DiSEqC stuff if necessary */
if(budget->card->type == BUDGET_TT)
dvb_add_frontend_ioctls (budget->dvb_adapter, budget_diseqc_ioctl, NULL, budget);
memcpy(budget->demux_id, "demux0_0", 9);
budget->demux_id[5] = budget->dvb_adapter->num + '0';
dvbdemux->priv = (void *) budget;
dvbdemux->filternum=256;
dvbdemux->feednum=256;
dvbdemux->start_feed=budget_start_feed;
dvbdemux->stop_feed=budget_stop_feed;
dvbdemux->write_to_decoder=NULL;
dvbdemux->dmx.vendor="CIM";
dvbdemux->dmx.model="sw";
dvbdemux->dmx.id=budget->demux_id;
dvbdemux->dmx.capabilities=(DMX_TS_FILTERING|
DMX_SECTION_FILTERING|
DMX_MEMORY_BASED_FILTERING);
dvb_dmx_init(&budget->demux);
dvbfront->id="hw_frontend";
dvbfront->vendor="VLSI";
dvbfront->model="DVB Frontend";
dvbfront->source=DMX_FRONTEND_0;
budget->dmxdev.filternum=256;
budget->dmxdev.demux=&dvbdemux->dmx;
budget->dmxdev.capabilities=0;
dvb_dmxdev_init(&budget->dmxdev, budget->dvb_adapter);
ret=dvbdemux->dmx.add_frontend(&dvbdemux->dmx,
&budget->hw_frontend);
if (ret<0)
return ret;
budget->mem_frontend.id="mem_frontend";
budget->mem_frontend.vendor="memory";
budget->mem_frontend.model="sw";
budget->mem_frontend.source=DMX_MEMORY_FE;
ret=dvbdemux->dmx.add_frontend(&dvbdemux->dmx,
&budget->mem_frontend);
if (ret<0)
return ret;
ret=dvbdemux->dmx.connect_frontend(&dvbdemux->dmx,
&budget->hw_frontend);
if (ret<0)
return ret;
budget->dvb_net.card_num=budget->dvb_adapter->num;
dvb_net_init(budget->dvb_adapter, &budget->dvb_net, &dvbdemux->dmx);
return 0;
}
static void
dvb_unregister(struct budget_s *budget)
{
struct dvb_demux *dvbdemux=&budget->demux;
DEB_EE(("budget: %p\n",budget));
if (!budget->registered)
return;
dvb_net_release(&budget->dvb_net);
dvbdemux->dmx.close(&dvbdemux->dmx);
dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->hw_frontend);
dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->mem_frontend);
dvb_dmxdev_release(&budget->dmxdev);
dvb_dmx_release(&budget->demux);
if(budget->card->type == BUDGET_TT)
dvb_remove_frontend_ioctls (budget->dvb_adapter, budget_diseqc_ioctl, NULL);
}
static
int master_xfer (struct dvb_i2c_bus *i2c, const struct i2c_msg msgs[], int num)
{
struct saa7146_dev *dev = i2c->data;
return saa7146_i2c_transfer(dev, msgs, num, 6);
}
int budget_attach (struct saa7146_dev* dev, struct saa7146_pci_extension_data *info)
{
struct budget_s *budget = NULL;
struct scatterlist *slist = NULL;
int slen = 0;
int length = TS_WIDTH*TS_HEIGHT;
int pages = (length+PAGE_SIZE-1)/PAGE_SIZE;
int ret = 0;
struct budget_info *bi = info->ext_priv;
if (bi->type == BUDGET_PATCH && budget_patch <= 0)
return -EOPNOTSUPP;
if (!(budget = kmalloc (sizeof (struct budget_s), GFP_KERNEL))) {
printk ("%s: out of memory!\n", __FUNCTION__);
return -ENOMEM;
}
memset(budget, 0, sizeof(struct budget_s));
DEB_EE(("dev: %p, budget: %p\n",dev,budget));
budget->card = bi;
budget->dev=(struct saa7146_dev *)dev;
(struct budget_s*)dev->ext_priv = budget;
dvb_register_adapter(&budget->dvb_adapter, budget->card->name);
/* set dd1 stream a & b */
saa7146_write(dev, DD1_STREAM_B, 0x00000000);
saa7146_write(dev, DD1_INIT, 0x02000000);
saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
/* the Siemens DVB needs this if you want to have the i2c chips
get recognized before the main driver is loaded */
saa7146_write(dev, GPIO_CTRL, 0x500000);
saa7146_i2c_adapter_prepare(dev, NULL, SAA7146_I2C_BUS_BIT_RATE_3200);
if (budget->card->type == BUDGET_PATCH && budget_patch > 0)
{
int cnt;
/*
** This code will setup the SAA7146_RPS1 to generate a square
** wave on GPIO3, changing when a field (TS_HEIGHT/2 "lines" of
** TS_WIDTH packets) has been acquired on SAA7146_D1B video port;
** then, this GPIO3 output which is connected to the D1B_VSYNC
** input, will trigger the acquisition of the alternate field
** and so on.
** Currently, the TT_budget / WinTV_Nova cards have two ICs
** (74HCT4040, LVC74) for the generation of this VSYNC signal,
** which seems that can be done perfectly without this :-)).
*/
cnt = 0; // Setup RPS1 "program" (p35)
// Wait reset Source Line Counter Threshold (p36)
dev->rps1[cnt++]=cpu_to_le32(CMD_PAUSE | RPS_INV | EVT_HS);
// Wait Source Line Counter Threshold (p36)
dev->rps1[cnt++]=cpu_to_le32(CMD_PAUSE | EVT_HS);
// Set GPIO3=1 (p42)
dev->rps1[cnt++]=cpu_to_le32(CMD_WR_REG_MASK | (GPIO_CTRL>>2));
dev->rps1[cnt++]=cpu_to_le32(GPIO3_MSK);
dev->rps1[cnt++]=cpu_to_le32(SAA7146_GPIO_OUTHI<<24);
// Wait reset Source Line Counter Threshold (p36)
dev->rps1[cnt++]=cpu_to_le32(CMD_PAUSE | RPS_INV | EVT_HS);
// Wait Source Line Counter Threshold
dev->rps1[cnt++]=cpu_to_le32(CMD_PAUSE | EVT_HS);
// Set GPIO3=0 (p42)
dev->rps1[cnt++]=cpu_to_le32(CMD_WR_REG_MASK | (GPIO_CTRL>>2));
dev->rps1[cnt++]=cpu_to_le32(GPIO3_MSK);
dev->rps1[cnt++]=cpu_to_le32(SAA7146_GPIO_OUTLO<<24);
// Jump to begin of RPS program (p37)
dev->rps1[cnt++]=cpu_to_le32(CMD_JUMP);
dev->rps1[cnt++]=cpu_to_le32(virt_to_bus(&dev->rps1[0]));
// Fix VSYNC level
saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
// Set RPS1 Address register to point to RPS code (r108 p42)
saa7146_write(dev, RPS_ADDR1, virt_to_bus(&dev->rps1[0]));
// Set Source Line Counter Threshold, using BRS (rCC p43)
saa7146_write(dev, RPS_THRESH1, ((TS_HEIGHT/2) | MASK_12));
// Enable RPS1 (rFC p33)
saa7146_write(dev, MC1, (MASK_13 | MASK_29));
}
budget->i2c_bus = dvb_register_i2c_bus (master_xfer, dev, budget->dvb_adapter, 0);
if (!budget->i2c_bus) {
dvb_unregister_adapter (budget->dvb_adapter);
kfree(budget);
return -ENOMEM;
}
budget->grabbing = vmalloc(length);
if (!budget->grabbing) {
printk(KERN_ERR "dvb: vmalloc() failed.\n");
ret = -ENOMEM;
goto err;
}
if (!(slist = vmalloc_to_sg(budget->grabbing, pages))) {
printk(KERN_ERR "dvb: vmalloc_to_sg() failed.\n");
ret = -ENOMEM;
goto err;
}
if (saa7146_pgtable_alloc(dev->pci, &budget->pt)) {
printk(KERN_ERR "dvb: saa7146_pgtable_alloc() failed.\n");
ret = -ENOMEM;
goto err;
}
slen = pci_map_sg(dev->pci,slist,pages,PCI_DMA_FROMDEVICE);
saa7146_pgtable_build_single(dev->pci, &budget->pt, slist, slen);
saa7146_write(dev, PCI_BT_V1, 0x001c0000);
/* upload all */
saa7146_write(dev, GPIO_CTRL, 0x000000);
tasklet_init (&budget->fidb_tasklet, fidbirq, (unsigned long) budget);
saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI); /* frontend power on */
return 0;
err:
if( NULL != budget->grabbing ) {
vfree(budget->grabbing);
}
if( NULL != slist ) {
kfree(slist);
}
dvb_unregister_i2c_bus (master_xfer,budget->i2c_bus->adapter, budget->i2c_bus->id);
dvb_unregister_adapter (budget->dvb_adapter);
if( NULL != budget ) {
kfree(budget);
}
return ret;
}
int budget_detach (struct saa7146_dev* saa)
{
struct budget_s *budget = (struct budget_s*)saa->ext_priv;
DEB_EE(("budget: %p\n",budget));
dvb_unregister(budget);
dvb_unregister_i2c_bus (master_xfer,budget->i2c_bus->adapter, budget->i2c_bus->id);
dvb_unregister_adapter (budget->dvb_adapter);
saa7146_pgtable_free(saa->pci, &budget->pt);
vfree(budget->grabbing);
kfree (budget);
saa->ext_priv = NULL;
return 0;
}
void budget_irq(struct saa7146_dev* dev, u32 *isr)
{
struct budget_s *budget = (struct budget_s*)dev->ext_priv;
DEB_EE(("dev: %p, budget: %p\n",dev,budget));
if (*isr & MASK_07)
tasklet_schedule (&budget->fidb_tasklet);
}
EXPORT_SYMBOL_GPL(budget_register);
EXPORT_SYMBOL_GPL(budget_irq);
EXPORT_SYMBOL_GPL(budget_attach);
EXPORT_SYMBOL_GPL(budget_detach);
EXPORT_SYMBOL_GPL(budget_debug);
MODULE_PARM(budget_debug,"i");
MODULE_PARM(budget_patch,"i");
MODULE_LICENSE("GPL");
|