summaryrefslogtreecommitdiff
path: root/drivers/media/dvb/dibusb/dvb-dibusb-usb.c
blob: 642f0596a5ba9e76c04c478f28168f0c480f54a6 (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
/*
 * dvb-dibusb-usb.c is part of the driver for mobile USB Budget DVB-T devices
 * based on reference design made by DiBcom (http://www.dibcom.fr/)
 *
 * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@desy.de)
 *
 * see dvb-dibusb-core.c for more copyright details.
 *
 * This file contains functions for initializing and handling the
 * usb specific stuff.
 */
#include "dvb-dibusb.h"

#include <linux/version.h>
#include <linux/pci.h>

int dibusb_readwrite_usb(struct usb_dibusb *dib, u8 *wbuf, u16 wlen, u8 *rbuf,
		u16 rlen)
{
	int actlen,ret = -ENOMEM;

	if (wbuf == NULL || wlen == 0)
		return -EINVAL;

	if ((ret = down_interruptible(&dib->usb_sem)))
		return ret;

	debug_dump(wbuf,wlen);

	ret = usb_bulk_msg(dib->udev,usb_sndbulkpipe(dib->udev,
			dib->dibdev->dev_cl->pipe_cmd), wbuf,wlen,&actlen,
			DIBUSB_I2C_TIMEOUT);

	if (ret)
		err("bulk message failed: %d (%d/%d)",ret,wlen,actlen);
	else
		ret = actlen != wlen ? -1 : 0;

	/* an answer is expected, and no error before */
	if (!ret && rbuf && rlen) {
		ret = usb_bulk_msg(dib->udev,usb_rcvbulkpipe(dib->udev,
				dib->dibdev->dev_cl->pipe_cmd),rbuf,rlen,&actlen,
				DIBUSB_I2C_TIMEOUT);

		if (ret)
			err("recv bulk message failed: %d",ret);
		else {
			deb_alot("rlen: %d\n",rlen);
			debug_dump(rbuf,actlen);
		}
	}

	up(&dib->usb_sem);
	return ret;
}

/*
 * Cypress controls
 */
int dibusb_write_usb(struct usb_dibusb *dib, u8 *buf, u16 len)
{
	return dibusb_readwrite_usb(dib,buf,len,NULL,0);
}

#if 0
/*
 * #if 0'ing the following functions as they are not in use _now_,
 * but probably will be sometime.
 */
/*
 * do not use this, just a workaround for a bug,
 * which will hopefully never occur :).
 */
int dibusb_interrupt_read_loop(struct usb_dibusb *dib)
{
	u8 b[1] = { DIBUSB_REQ_INTR_READ };
	return dibusb_write_usb(dib,b,1);
}
#endif

/*
 * ioctl for the firmware
 */
static int dibusb_ioctl_cmd(struct usb_dibusb *dib, u8 cmd, u8 *param, int plen)
{
	u8 b[34];
	int size = plen > 32 ? 32 : plen;
	memset(b,0,34);
	b[0] = DIBUSB_REQ_SET_IOCTL;
	b[1] = cmd;

	if (size > 0)
		memcpy(&b[2],param,size);

	return dibusb_write_usb(dib,b,34); //2+size);
}

/*
 * ioctl for power control
 */
int dibusb_hw_wakeup(struct dvb_frontend *fe)
{
	struct usb_dibusb *dib = (struct usb_dibusb *) fe->dvb->priv;
	u8 b[1] = { DIBUSB_IOCTL_POWER_WAKEUP };
	deb_info("dibusb-device is getting up.\n");

	switch (dib->dibdev->dev_cl->id) {
		case DTT200U:
			break;
		default:
			dibusb_ioctl_cmd(dib,DIBUSB_IOCTL_CMD_POWER_MODE, b,1);
			break;
	}

	if (dib->fe_init)
		return dib->fe_init(fe);

	return 0;
}

int dibusb_hw_sleep(struct dvb_frontend *fe)
{
	struct usb_dibusb *dib = (struct usb_dibusb *) fe->dvb->priv;
	u8 b[1] = { DIBUSB_IOCTL_POWER_SLEEP };
	deb_info("dibusb-device is going to bed.\n");
	/* workaround, something is wrong, when dibusb 1.1 device are going to bed too late */
	switch (dib->dibdev->dev_cl->id) {
		case DIBUSB1_1:
		case NOVAT_USB2:
		case DTT200U:
			break;
		default:
			dibusb_ioctl_cmd(dib,DIBUSB_IOCTL_CMD_POWER_MODE, b,1);
			break;
	}
	if (dib->fe_sleep)
		return dib->fe_sleep(fe);

	return 0;
}

int dibusb_set_streaming_mode(struct usb_dibusb *dib,u8 mode)
{
	u8 b[2] = { DIBUSB_REQ_SET_STREAMING_MODE, mode };
	return dibusb_readwrite_usb(dib,b,2,NULL,0);
}

static int dibusb_urb_kill(struct usb_dibusb *dib)
{
	int i;
deb_info("trying to kill urbs\n");
	if (dib->init_state & DIBUSB_STATE_URB_SUBMIT) {
		for (i = 0; i < dib->dibdev->dev_cl->urb_count; i++) {
			deb_info("killing URB no. %d.\n",i);

			/* stop the URB */
			usb_kill_urb(dib->urb_list[i]);
		}
	} else
	deb_info(" URBs not killed.\n");
	dib->init_state &= ~DIBUSB_STATE_URB_SUBMIT;
	return 0;
}

static int dibusb_urb_submit(struct usb_dibusb *dib)
{
	int i,ret;
	if (dib->init_state & DIBUSB_STATE_URB_INIT) {
		for (i = 0; i < dib->dibdev->dev_cl->urb_count; i++) {
			deb_info("submitting URB no. %d\n",i);
			if ((ret = usb_submit_urb(dib->urb_list[i],GFP_ATOMIC))) {
				err("could not submit buffer urb no. %d - get them all back\n",i);
				dibusb_urb_kill(dib);
				return ret;
			}
			dib->init_state |= DIBUSB_STATE_URB_SUBMIT;
		}
	}
	return 0;
}

int dibusb_streaming(struct usb_dibusb *dib,int onoff)
{
	if (onoff)
		dibusb_urb_submit(dib);
	else
		dibusb_urb_kill(dib);

	switch (dib->dibdev->dev_cl->id) {
		case DIBUSB2_0:
		case DIBUSB2_0B:
		case NOVAT_USB2:
		case UMT2_0:
			if (onoff)
				return dibusb_ioctl_cmd(dib,DIBUSB_IOCTL_CMD_ENABLE_STREAM,NULL,0);
			else
				return dibusb_ioctl_cmd(dib,DIBUSB_IOCTL_CMD_DISABLE_STREAM,NULL,0);
			break;
		default:
			break;
	}
	return 0;
}

int dibusb_urb_init(struct usb_dibusb *dib)
{
	int i,bufsize,def_pid_parse = 1;

	/*
	 * when reloading the driver w/o replugging the device
	 * a timeout occures, this helps
	 */
	usb_clear_halt(dib->udev,usb_sndbulkpipe(dib->udev,dib->dibdev->dev_cl->pipe_cmd));
	usb_clear_halt(dib->udev,usb_rcvbulkpipe(dib->udev,dib->dibdev->dev_cl->pipe_cmd));
	usb_clear_halt(dib->udev,usb_rcvbulkpipe(dib->udev,dib->dibdev->dev_cl->pipe_data));

	/* allocate the array for the data transfer URBs */
	dib->urb_list = kmalloc(dib->dibdev->dev_cl->urb_count*sizeof(struct urb *),GFP_KERNEL);
	if (dib->urb_list == NULL)
		return -ENOMEM;
	memset(dib->urb_list,0,dib->dibdev->dev_cl->urb_count*sizeof(struct urb *));

	dib->init_state |= DIBUSB_STATE_URB_LIST;

	bufsize = dib->dibdev->dev_cl->urb_count*dib->dibdev->dev_cl->urb_buffer_size;
	deb_info("allocate %d bytes as buffersize for all URBs\n",bufsize);
	/* allocate the actual buffer for the URBs */
	if ((dib->buffer = pci_alloc_consistent(NULL,bufsize,&dib->dma_handle)) == NULL) {
		deb_info("not enough memory.\n");
		return -ENOMEM;
	}
	deb_info("allocation complete\n");
	memset(dib->buffer,0,bufsize);

	dib->init_state |= DIBUSB_STATE_URB_BUF;

	/* allocate and submit the URBs */
	for (i = 0; i < dib->dibdev->dev_cl->urb_count; i++) {
		if (!(dib->urb_list[i] = usb_alloc_urb(0,GFP_ATOMIC))) {
			return -ENOMEM;
		}

		usb_fill_bulk_urb( dib->urb_list[i], dib->udev,
				usb_rcvbulkpipe(dib->udev,dib->dibdev->dev_cl->pipe_data),
				&dib->buffer[i*dib->dibdev->dev_cl->urb_buffer_size],
				dib->dibdev->dev_cl->urb_buffer_size,
				dibusb_urb_complete, dib);

		dib->urb_list[i]->transfer_flags = 0;

		dib->init_state |= DIBUSB_STATE_URB_INIT;
	}

	/* dib->pid_parse here contains the value of the module parameter */
	/* decide if pid parsing can be deactivated:
	 * is possible (by device type) and wanted (by user)
	 */
	switch (dib->dibdev->dev_cl->id) {
		case DIBUSB2_0:
		case DIBUSB2_0B:
			if (dib->udev->speed == USB_SPEED_HIGH && !dib->pid_parse) {
				def_pid_parse = 0;
				info("running at HIGH speed, will deliver the complete TS.");
			} else
				info("will use pid_parsing.");
			break;
		default:
			break;
	}
	/* from here on it contains the device and user decision */
	dib->pid_parse = def_pid_parse;

	return 0;
}

int dibusb_urb_exit(struct usb_dibusb *dib)
{
	int i;

	dibusb_urb_kill(dib);

	if (dib->init_state & DIBUSB_STATE_URB_LIST) {
		for (i = 0; i < dib->dibdev->dev_cl->urb_count; i++) {
			if (dib->urb_list[i] != NULL) {
				deb_info("freeing URB no. %d.\n",i);
				/* free the URBs */
				usb_free_urb(dib->urb_list[i]);
			}
		}
		/* free the urb array */
		kfree(dib->urb_list);
		dib->init_state &= ~DIBUSB_STATE_URB_LIST;
	}

	if (dib->init_state & DIBUSB_STATE_URB_BUF)
		pci_free_consistent(NULL,
			dib->dibdev->dev_cl->urb_buffer_size*dib->dibdev->dev_cl->urb_count,
			dib->buffer,dib->dma_handle);

	dib->init_state &= ~DIBUSB_STATE_URB_BUF;
	dib->init_state &= ~DIBUSB_STATE_URB_INIT;
	return 0;
}