summaryrefstree
path: root/kernel/drivers/media/tdmb/tdmb_tsi_qc.c
blob: 75350b87d2be68457e464651ac01b7626130ccdd (plain)
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
/* drivers/media/tdmb/tdmb_qc_tsi.c
 *
 * Driver file for Qualcomm Transport Stream Interface
 *
 * Copyright (C) (2014, Samsung Electronics)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation version 2.
 *
 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
 * kind, whether express or implied; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
 * GNU General Public License for more details.
 */

#include <linux/init.h>
#include <linux/vmalloc.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/qcom_tspp.h>

#include "tdmb.h"

#define CHANNEL_ID 0
#define TSPP_CHANNEL_TIMEOUT 100	/* 100ms */
#define TSPP_BUFFER_SIZE (20 * 1024) /* 20KB */
#define TDMB_TS_SIZE 188

struct tsi_pkt {
	struct list_head list;
	void *buf;
	u32 len;
};

struct tsi_dev {
	spinlock_t tsi_lock;
	struct list_head free_list;
	struct list_head full_list;

	int tsi_running;
	int filter_exists_flag;

	u8 packet_buff[TSPP_BUFFER_SIZE];
};

struct tsi_dev *tsi_priv;

static void (*tsi_data_callback)(u8 *data, u32 length) = NULL;
static void tdmb_tsi_pull_data(struct work_struct *work);
static struct workqueue_struct *tdmb_tsi_workqueue;
static DECLARE_WORK(tdmb_tsi_work, tdmb_tsi_pull_data);

static struct tspp_select_source tdmb_tspp_set_source(void)
{
	struct tspp_select_source tspp_source;

	tspp_source.clk_inverse = 0;
	tspp_source.data_inverse = 0;
	tspp_source.sync_inverse = 0;
	tspp_source.enable_inverse = 0;
	tspp_source.mode = TSPP_TSIF_MODE_1;	/* whihout sync */
	tspp_source.source = TSPP_SOURCE_TSIF0;

	return tspp_source;
}

static struct tsi_pkt *tsi_get_pkt(struct tsi_dev *tsi, struct list_head *head)
{
	unsigned long flags;
	struct tsi_pkt *pkt;
	spin_lock_irqsave(&tsi->tsi_lock, flags);

	if (list_empty(head))	{
		/* DPRINTK("TSI %p list is null\n", head); */
		spin_unlock_irqrestore(&tsi->tsi_lock, flags);
		return NULL;
	}
	pkt = list_first_entry(head, struct tsi_pkt, list);
	spin_unlock_irqrestore(&tsi->tsi_lock, flags);

	return pkt;
}

static int tsi_setup_bufs(struct list_head *head, int packet_cnt, u8 *pkt_buff)
{
	struct tsi_pkt *pkt;
	u32 buf_size;
	u8 num_buf;
	int i;

	buf_size = TDMB_TS_SIZE * packet_cnt;
	num_buf = TSPP_BUFFER_SIZE / buf_size;

	for (i = 0; i < num_buf; i++) {
		pkt = kmalloc(sizeof(struct tsi_pkt), GFP_KERNEL);
		if (!pkt)
			return list_empty(head) ? -ENOMEM : 0 ;

		pkt->buf = (void *)(u8 *)(pkt_buff + i * buf_size);
		pkt->len = buf_size;
		list_add_tail(&pkt->list, head);
	}
	DPRINTK("total nodes calulated %d buf_size %d\n", num_buf, buf_size);

	return 0;
}

static void tsi_free_packets(struct tsi_dev *tsi)
{
	struct tsi_pkt *pkt;
	unsigned long flags;
	struct list_head *full = &tsi->full_list;
	struct list_head *head = &(tsi->free_list);

	spin_lock_irqsave(&tsi->tsi_lock, flags);
	/* move all the packets from full list to free list */
	while (!list_empty(full))	{
		pkt = list_entry(full->next, struct tsi_pkt, list);
		list_move_tail(&pkt->list, &tsi->free_list);
	}
	spin_unlock_irqrestore(&tsi->tsi_lock, flags);

	while (!list_empty(head))	{
		pkt = list_entry(head->next, struct tsi_pkt, list);
		list_del(&pkt->list);
		kfree(pkt);
	}
}

static bool tdmb_tsi_create_workqueue(void)
{
	tdmb_tsi_workqueue = create_singlethread_workqueue("ktdmbtsi");
	if (tdmb_tsi_workqueue)
		return true;
	else
		return false;
}

static bool tdmb_tsi_destroy_workqueue(void)
{
	if (tdmb_tsi_workqueue) {
		flush_workqueue(tdmb_tsi_workqueue);
		destroy_workqueue(tdmb_tsi_workqueue);
		tdmb_tsi_workqueue = NULL;
	}
	return true;
}


static void tdmb_tsi_pull_data(struct work_struct *work)
{
	struct tsi_pkt *pkt;
	unsigned long flags;
	const struct tspp_data_descriptor *tspp_data_desc;

	if (!tsi_priv->tsi_running) {
		DPRINTK("%s : tsi_runing : %d\n",
			__func__, tsi_priv->tsi_running);
		return;
	}
	while ((tspp_data_desc = tspp_get_buffer(0, CHANNEL_ID)) != NULL) {

		pkt = tsi_get_pkt(tsi_priv, &tsi_priv->free_list);
		if (pkt == NULL)	{
			DPRINTK("TSI..No more free bufs..\n");
			tspp_release_buffer(0, CHANNEL_ID, tspp_data_desc->id);
			return ;
		}

		if (tspp_data_desc->size == pkt->len)
			memcpy(pkt->buf, tspp_data_desc->virt_base, tspp_data_desc->size);
		else
			DPRINTK("Size err tspp_size:(%d) pkt_len:(%d) \n", \
						tspp_data_desc->size, pkt->len);

		spin_lock_irqsave(&tsi_priv->tsi_lock, flags);
		list_move_tail(&pkt->list, &tsi_priv->full_list);
		spin_unlock_irqrestore(&tsi_priv->tsi_lock, flags);

		tspp_release_buffer(0, CHANNEL_ID, tspp_data_desc->id);
	}

	while ((pkt = tsi_get_pkt(tsi_priv, &tsi_priv->full_list)) != NULL) {
#ifdef CONFIG_TSI_LIST_DEBUG
		DPRINTK("full_list virt:0x%p length:%d\n",pkt->buf, pkt->len);
#endif
		if (tsi_data_callback)
			tsi_data_callback(pkt->buf, pkt->len);
		spin_lock_irqsave(&tsi_priv->tsi_lock, flags);
		list_move(&pkt->list, &tsi_priv->free_list);
		spin_unlock_irqrestore(&tsi_priv->tsi_lock, flags);
	}
}

static void tdmb_tspp_callback(int channel_id, void *user)
{
	if (!tsi_priv->tsi_running) {
		DPRINTK("%s : tsi_runing : %d\n",
			__func__, tsi_priv->tsi_running);
		return;
	}
	if (tdmb_tsi_workqueue) {
		int ret;
		ret = queue_work(tdmb_tsi_workqueue, &tdmb_tsi_work);
		if (ret == 0)
			DPRINTK("failed in queue_work\n");
	}

}

static int tdmb_tspp_add_accept_all_filter(int channel_id,
				enum tspp_source source)
{
	struct tspp_filter tspp_filter;
	int ret;

	if (tsi_priv->filter_exists_flag) {
		DPRINTK("%s: accept all filter already exists\n",
				__func__);
		return 0;
	}

	tspp_filter.priority = 15;
	tspp_filter.pid = 0;
	tspp_filter.mask = 0;
	tspp_filter.mode = TSPP_MODE_RAW_NO_SUFFIX;
	tspp_filter.source = source;
	tspp_filter.decrypt = 0;

	ret = tspp_add_filter(0, channel_id, &tspp_filter);
	if (!ret) {
		tsi_priv->filter_exists_flag = 1;
		DPRINTK("accept all filter added successfully\n");
	}

	return ret;
}

static int tdmb_tspp_remove_accept_all_filter(int channel_id,
				enum tspp_source source)
{
	struct tspp_filter tspp_filter;
	int ret;
	if (tsi_priv->filter_exists_flag == 0) {
		DPRINTK("%s: accept all filter doesn't exist\n",
				__func__);
		return 0;
	}
	tspp_filter.priority = 15;

	ret = tspp_remove_filter(0, channel_id, &tspp_filter);
	if (!ret) {
		tsi_priv->filter_exists_flag = 0;
		DPRINTK("accept all filter removed successfully\n");
	}

	return ret;
}

int tdmb_tsi_start(void (*callback)(u8 *data, u32 length), int packet_cnt)
{
	struct tspp_select_source tspp_source = tdmb_tspp_set_source();

	int ret = 0;

	if (tsi_priv->tsi_running)
		return -1;

	tsi_priv->tsi_running = 1;

	if (tsi_setup_bufs(&tsi_priv->free_list, packet_cnt, tsi_priv->packet_buff)) {
		DPRINTK("TSI failed to setup pkt list");
		ret = -ENXIO;
		return ret;
	}

	if (tdmb_tsi_create_workqueue() == false) {
		DPRINTK("tdmb_tsi_create_workqueue fail\n");
		ret = ENXIO;
		goto err_create_workqueue;
	}
	tsi_data_callback = callback;

	ret = tspp_open_channel(0, CHANNEL_ID);
	if (ret < 0) {
		DPRINTK("%s: tspp_open_channel(%d) failed (%d)\n",
			__func__,
			CHANNEL_ID,
			ret);
		goto err_open_channel;
	}
	ret = tspp_open_stream(0, CHANNEL_ID, &tspp_source);
	if (ret < 0) {
		DPRINTK("%s: tspp_select_source(%d,%d) failed (%d)\n",
			__func__,
			CHANNEL_ID,
			tspp_source.source,
			ret);

		goto err_open_stream;
	}

	ret = tspp_register_notification(0,
						   CHANNEL_ID,
						   tdmb_tspp_callback,
						   NULL,
						   TSPP_CHANNEL_TIMEOUT); /* 100ms */
	if (ret < 0) {
		DPRINTK(
			"%s: tspp_register_notification(%d) failed (%d)\n",
			__func__, CHANNEL_ID, ret);

		goto err_channel_unregister_notif;
	}

	ret = tspp_allocate_buffers(0,
							CHANNEL_ID,
							TSPP_BUFFER_SIZE / TDMB_TS_SIZE,
							TDMB_TS_SIZE * packet_cnt,
							packet_cnt,  /* NOTIFICATION_SIZE */
							NULL, NULL, NULL);
	if (ret < 0) {
		DPRINTK(
			"%s: tspp_allocate_buffers(%d) failed (%d)\n",
			__func__, CHANNEL_ID, ret);

		goto err_allocate_buffers;
	}

	ret = tdmb_tspp_add_accept_all_filter(CHANNEL_ID,
						tspp_source.source);
	if (ret < 0) {
		DPRINTK(
			"%s: tdmb_tspp_add_accept_all_filter(%d, %d) failed\n",
			__func__, CHANNEL_ID, tspp_source.source);
		goto err_add_filter;
	}

	return ret;

err_add_filter:
err_allocate_buffers:
	tspp_unregister_notification(0, CHANNEL_ID);
err_channel_unregister_notif:
	tspp_close_stream(0, CHANNEL_ID);
err_open_stream:
	tspp_close_channel(0, CHANNEL_ID);
err_open_channel:
	tdmb_tsi_destroy_workqueue();
err_create_workqueue:
	tsi_free_packets(tsi_priv);

	tsi_priv->tsi_running = 0;

	return ret;
}
EXPORT_SYMBOL_GPL(tdmb_tsi_start);

int tdmb_tsi_stop(void)
{
	struct tspp_select_source tspp_source = tdmb_tspp_set_source();

	if (!tsi_priv->tsi_running)
		return -1;

	tsi_priv->tsi_running = 0;

	tdmb_tsi_destroy_workqueue();
	tsi_data_callback = NULL;
	tdmb_tspp_remove_accept_all_filter(CHANNEL_ID, tspp_source.source);
	tspp_unregister_notification(0, CHANNEL_ID);
	tspp_close_stream(0, CHANNEL_ID);
	tspp_close_channel(0, CHANNEL_ID);
	tsi_free_packets(tsi_priv);
	return 0;
}
EXPORT_SYMBOL_GPL(tdmb_tsi_stop);

int tdmb_tsi_init(void)
{
	tsi_priv = kmalloc(sizeof(struct tsi_dev), GFP_KERNEL);
	if (tsi_priv == NULL)	{
		DPRINTK("NO Memory for tsi allocation\n");
		return -ENOMEM;
	}

	INIT_LIST_HEAD(&tsi_priv->full_list);
	INIT_LIST_HEAD(&tsi_priv->free_list);
	spin_lock_init(&tsi_priv->tsi_lock);

	tsi_priv->tsi_running = 0;
	tsi_priv->filter_exists_flag=0;

	return 0;
}
EXPORT_SYMBOL_GPL(tdmb_tsi_init);

void tdmb_tsi_deinit(void)
{
	kfree(tsi_priv);
}
EXPORT_SYMBOL_GPL(tdmb_tsi_deinit);