2 * dvb-dibusb-usb.c is part of the driver for mobile USB Budget DVB-T devices
3 * based on reference design made by DiBcom (http://www.dibcom.fr/)
5 * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@desy.de)
7 * see dvb-dibusb-core.c for more copyright details.
9 * This file contains functions for initializing and handling the
12 #include "dvb-dibusb.h"
14 #include <linux/version.h>
15 #include <linux/pci.h>
17 int dibusb_readwrite_usb(struct usb_dibusb
*dib
, u8
*wbuf
, u16 wlen
, u8
*rbuf
,
20 int actlen
,ret
= -ENOMEM
;
22 if (wbuf
== NULL
|| wlen
== 0)
25 if ((ret
= down_interruptible(&dib
->usb_sem
)))
28 debug_dump(wbuf
,wlen
);
30 ret
= usb_bulk_msg(dib
->udev
,usb_sndbulkpipe(dib
->udev
,
31 dib
->dibdev
->dev_cl
->pipe_cmd
), wbuf
,wlen
,&actlen
,
35 err("bulk message failed: %d (%d/%d)",ret
,wlen
,actlen
);
37 ret
= actlen
!= wlen
? -1 : 0;
39 /* an answer is expected, and no error before */
40 if (!ret
&& rbuf
&& rlen
) {
41 ret
= usb_bulk_msg(dib
->udev
,usb_rcvbulkpipe(dib
->udev
,
42 dib
->dibdev
->dev_cl
->pipe_cmd
),rbuf
,rlen
,&actlen
,
46 err("recv bulk message failed: %d",ret
);
48 deb_alot("rlen: %d\n",rlen
);
49 debug_dump(rbuf
,actlen
);
60 int dibusb_write_usb(struct usb_dibusb
*dib
, u8
*buf
, u16 len
)
62 return dibusb_readwrite_usb(dib
,buf
,len
,NULL
,0);
67 * #if 0'ing the following functions as they are not in use _now_,
68 * but probably will be sometime.
71 * do not use this, just a workaround for a bug,
72 * which will hopefully never occur :).
74 int dibusb_interrupt_read_loop(struct usb_dibusb
*dib
)
76 u8 b
[1] = { DIBUSB_REQ_INTR_READ
};
77 return dibusb_write_usb(dib
,b
,1);
82 * ioctl for the firmware
84 static int dibusb_ioctl_cmd(struct usb_dibusb
*dib
, u8 cmd
, u8
*param
, int plen
)
87 int size
= plen
> 32 ? 32 : plen
;
89 b
[0] = DIBUSB_REQ_SET_IOCTL
;
93 memcpy(&b
[2],param
,size
);
95 return dibusb_write_usb(dib
,b
,34); //2+size);
99 * ioctl for power control
101 int dibusb_hw_wakeup(struct dvb_frontend
*fe
)
103 struct usb_dibusb
*dib
= (struct usb_dibusb
*) fe
->dvb
->priv
;
104 u8 b
[1] = { DIBUSB_IOCTL_POWER_WAKEUP
};
105 deb_info("dibusb-device is getting up.\n");
107 switch (dib
->dibdev
->dev_cl
->id
) {
111 dibusb_ioctl_cmd(dib
,DIBUSB_IOCTL_CMD_POWER_MODE
, b
,1);
116 return dib
->fe_init(fe
);
121 int dibusb_hw_sleep(struct dvb_frontend
*fe
)
123 struct usb_dibusb
*dib
= (struct usb_dibusb
*) fe
->dvb
->priv
;
124 u8 b
[1] = { DIBUSB_IOCTL_POWER_SLEEP
};
125 deb_info("dibusb-device is going to bed.\n");
126 /* workaround, something is wrong, when dibusb 1.1 device are going to bed too late */
127 switch (dib
->dibdev
->dev_cl
->id
) {
133 dibusb_ioctl_cmd(dib
,DIBUSB_IOCTL_CMD_POWER_MODE
, b
,1);
137 return dib
->fe_sleep(fe
);
142 int dibusb_set_streaming_mode(struct usb_dibusb
*dib
,u8 mode
)
144 u8 b
[2] = { DIBUSB_REQ_SET_STREAMING_MODE
, mode
};
145 return dibusb_readwrite_usb(dib
,b
,2,NULL
,0);
148 static int dibusb_urb_kill(struct usb_dibusb
*dib
)
151 deb_info("trying to kill urbs\n");
152 if (dib
->init_state
& DIBUSB_STATE_URB_SUBMIT
) {
153 for (i
= 0; i
< dib
->dibdev
->dev_cl
->urb_count
; i
++) {
154 deb_info("killing URB no. %d.\n",i
);
157 usb_kill_urb(dib
->urb_list
[i
]);
160 deb_info(" URBs not killed.\n");
161 dib
->init_state
&= ~DIBUSB_STATE_URB_SUBMIT
;
165 static int dibusb_urb_submit(struct usb_dibusb
*dib
)
168 if (dib
->init_state
& DIBUSB_STATE_URB_INIT
) {
169 for (i
= 0; i
< dib
->dibdev
->dev_cl
->urb_count
; i
++) {
170 deb_info("submitting URB no. %d\n",i
);
171 if ((ret
= usb_submit_urb(dib
->urb_list
[i
],GFP_ATOMIC
))) {
172 err("could not submit buffer urb no. %d - get them all back\n",i
);
173 dibusb_urb_kill(dib
);
176 dib
->init_state
|= DIBUSB_STATE_URB_SUBMIT
;
182 int dibusb_streaming(struct usb_dibusb
*dib
,int onoff
)
185 dibusb_urb_submit(dib
);
187 dibusb_urb_kill(dib
);
189 switch (dib
->dibdev
->dev_cl
->id
) {
195 return dibusb_ioctl_cmd(dib
,DIBUSB_IOCTL_CMD_ENABLE_STREAM
,NULL
,0);
197 return dibusb_ioctl_cmd(dib
,DIBUSB_IOCTL_CMD_DISABLE_STREAM
,NULL
,0);
205 int dibusb_urb_init(struct usb_dibusb
*dib
)
207 int i
,bufsize
,def_pid_parse
= 1;
210 * when reloading the driver w/o replugging the device
211 * a timeout occures, this helps
213 usb_clear_halt(dib
->udev
,usb_sndbulkpipe(dib
->udev
,dib
->dibdev
->dev_cl
->pipe_cmd
));
214 usb_clear_halt(dib
->udev
,usb_rcvbulkpipe(dib
->udev
,dib
->dibdev
->dev_cl
->pipe_cmd
));
215 usb_clear_halt(dib
->udev
,usb_rcvbulkpipe(dib
->udev
,dib
->dibdev
->dev_cl
->pipe_data
));
217 /* allocate the array for the data transfer URBs */
218 dib
->urb_list
= kmalloc(dib
->dibdev
->dev_cl
->urb_count
*sizeof(struct urb
*),GFP_KERNEL
);
219 if (dib
->urb_list
== NULL
)
221 memset(dib
->urb_list
,0,dib
->dibdev
->dev_cl
->urb_count
*sizeof(struct urb
*));
223 dib
->init_state
|= DIBUSB_STATE_URB_LIST
;
225 bufsize
= dib
->dibdev
->dev_cl
->urb_count
*dib
->dibdev
->dev_cl
->urb_buffer_size
;
226 deb_info("allocate %d bytes as buffersize for all URBs\n",bufsize
);
227 /* allocate the actual buffer for the URBs */
228 if ((dib
->buffer
= pci_alloc_consistent(NULL
,bufsize
,&dib
->dma_handle
)) == NULL
) {
229 deb_info("not enough memory.\n");
232 deb_info("allocation complete\n");
233 memset(dib
->buffer
,0,bufsize
);
235 dib
->init_state
|= DIBUSB_STATE_URB_BUF
;
237 /* allocate and submit the URBs */
238 for (i
= 0; i
< dib
->dibdev
->dev_cl
->urb_count
; i
++) {
239 if (!(dib
->urb_list
[i
] = usb_alloc_urb(0,GFP_ATOMIC
))) {
243 usb_fill_bulk_urb( dib
->urb_list
[i
], dib
->udev
,
244 usb_rcvbulkpipe(dib
->udev
,dib
->dibdev
->dev_cl
->pipe_data
),
245 &dib
->buffer
[i
*dib
->dibdev
->dev_cl
->urb_buffer_size
],
246 dib
->dibdev
->dev_cl
->urb_buffer_size
,
247 dibusb_urb_complete
, dib
);
249 dib
->urb_list
[i
]->transfer_flags
= 0;
251 dib
->init_state
|= DIBUSB_STATE_URB_INIT
;
254 /* dib->pid_parse here contains the value of the module parameter */
255 /* decide if pid parsing can be deactivated:
256 * is possible (by device type) and wanted (by user)
258 switch (dib
->dibdev
->dev_cl
->id
) {
261 if (dib
->udev
->speed
== USB_SPEED_HIGH
&& !dib
->pid_parse
) {
263 info("running at HIGH speed, will deliver the complete TS.");
265 info("will use pid_parsing.");
270 /* from here on it contains the device and user decision */
271 dib
->pid_parse
= def_pid_parse
;
276 int dibusb_urb_exit(struct usb_dibusb
*dib
)
280 dibusb_urb_kill(dib
);
282 if (dib
->init_state
& DIBUSB_STATE_URB_LIST
) {
283 for (i
= 0; i
< dib
->dibdev
->dev_cl
->urb_count
; i
++) {
284 if (dib
->urb_list
[i
] != NULL
) {
285 deb_info("freeing URB no. %d.\n",i
);
287 usb_free_urb(dib
->urb_list
[i
]);
290 /* free the urb array */
291 kfree(dib
->urb_list
);
292 dib
->init_state
&= ~DIBUSB_STATE_URB_LIST
;
295 if (dib
->init_state
& DIBUSB_STATE_URB_BUF
)
296 pci_free_consistent(NULL
,
297 dib
->dibdev
->dev_cl
->urb_buffer_size
*dib
->dibdev
->dev_cl
->urb_count
,
298 dib
->buffer
,dib
->dma_handle
);
300 dib
->init_state
&= ~DIBUSB_STATE_URB_BUF
;
301 dib
->init_state
&= ~DIBUSB_STATE_URB_INIT
;