[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / media / dvb / b2c2 / flexcop-usb.c
blob0113449abd15107479b1722308309900f97bb0b5
1 /*
2 * This file is part of linux driver the digital TV devices equipped with B2C2 FlexcopII(b)/III
4 * flexcop-usb.c - covers the USB part.
6 * see flexcop.c for copyright information.
7 */
9 #define FC_LOG_PREFIX "flexcop_usb"
10 #include "flexcop-usb.h"
11 #include "flexcop-common.h"
13 /* Version information */
14 #define DRIVER_VERSION "0.1"
15 #define DRIVER_NAME "Technisat/B2C2 FlexCop II/IIb/III Digital TV USB Driver"
16 #define DRIVER_AUTHOR "Patrick Boettcher <patrick.boettcher@desy.de>"
18 /* debug */
19 #ifdef CONFIG_DVB_B2C2_FLEXCOP_DEBUG
20 #define dprintk(level,args...) \
21 do { if ((debug & level)) { printk(args); } } while (0)
22 #define debug_dump(b,l,method) {\
23 int i; \
24 for (i = 0; i < l; i++) method("%02x ", b[i]); \
25 method("\n");\
28 #define DEBSTATUS ""
29 #else
30 #define dprintk(level,args...)
31 #define debug_dump(b,l,method)
32 #define DEBSTATUS " (debugging is not enabled)"
33 #endif
35 static int debug;
36 module_param(debug, int, 0644);
37 MODULE_PARM_DESC(debug, "set debugging level (1=info,ts=2,ctrl=4,i2c=8,v8mem=16 (or-able))." DEBSTATUS);
38 #undef DEBSTATUS
40 #define deb_info(args...) dprintk(0x01,args)
41 #define deb_ts(args...) dprintk(0x02,args)
42 #define deb_ctrl(args...) dprintk(0x04,args)
43 #define deb_i2c(args...) dprintk(0x08,args)
44 #define deb_v8(args...) dprintk(0x10,args)
46 /* JLP 111700: we will include the 1 bit gap between the upper and lower 3 bits
47 * in the IBI address, to make the V8 code simpler.
48 * PCI ADDRESS FORMAT: 0x71C -> 0000 0111 0001 1100 (these are the six bits used)
49 * in general: 0000 0HHH 000L LL00
50 * IBI ADDRESS FORMAT: RHHH BLLL
52 * where R is the read(1)/write(0) bit, B is the busy bit
53 * and HHH and LLL are the two sets of three bits from the PCI address.
55 #define B2C2_FLEX_PCIOFFSET_TO_INTERNALADDR(usPCI) (u8) (((usPCI >> 2) & 0x07) + ((usPCI >> 4) & 0x70))
56 #define B2C2_FLEX_INTERNALADDR_TO_PCIOFFSET(ucAddr) (u16) (((ucAddr & 0x07) << 2) + ((ucAddr & 0x70) << 4))
59 * DKT 020228
60 * - forget about this VENDOR_BUFFER_SIZE, read and write register
61 * deal with DWORD or 4 bytes, that should be should from now on
62 * - from now on, we don't support anything older than firm 1.00
63 * I eliminated the write register as a 2 trip of writing hi word and lo word
64 * and force this to write only 4 bytes at a time.
65 * NOTE: this should work with all the firmware from 1.00 and newer
67 static int flexcop_usb_readwrite_dw(struct flexcop_device *fc, u16 wRegOffsPCI, u32 *val, u8 read)
69 struct flexcop_usb *fc_usb = fc->bus_specific;
70 u8 request = read ? B2C2_USB_READ_REG : B2C2_USB_WRITE_REG;
71 u8 request_type = (read ? USB_DIR_IN : USB_DIR_OUT) | USB_TYPE_VENDOR;
72 u8 wAddress = B2C2_FLEX_PCIOFFSET_TO_INTERNALADDR(wRegOffsPCI) | (read ? 0x80 : 0);
74 int len = usb_control_msg(fc_usb->udev,
75 read ? B2C2_USB_CTRL_PIPE_IN : B2C2_USB_CTRL_PIPE_OUT,
76 request,
77 request_type, /* 0xc0 read or 0x40 write*/
78 wAddress,
80 val,
81 sizeof(u32),
82 B2C2_WAIT_FOR_OPERATION_RDW * HZ);
84 if (len != sizeof(u32)) {
85 err("error while %s dword from %d (%d).",read ? "reading" : "writing",
86 wAddress,wRegOffsPCI);
87 return -EIO;
89 return 0;
93 * DKT 010817 - add support for V8 memory read/write and flash update
95 static int flexcop_usb_v8_memory_req(struct flexcop_usb *fc_usb,
96 flexcop_usb_request_t req, u8 page, u16 wAddress,
97 u8 *pbBuffer,u32 buflen)
99 // u8 dwRequestType;
100 u8 request_type = USB_TYPE_VENDOR;
101 u16 wIndex;
102 int nWaitTime,pipe,len;
104 wIndex = page << 8;
106 switch (req) {
107 case B2C2_USB_READ_V8_MEM:
108 nWaitTime = B2C2_WAIT_FOR_OPERATION_V8READ;
109 request_type |= USB_DIR_IN;
110 // dwRequestType = (u8) RTYPE_READ_V8_MEMORY;
111 pipe = B2C2_USB_CTRL_PIPE_IN;
112 break;
113 case B2C2_USB_WRITE_V8_MEM:
114 wIndex |= pbBuffer[0];
115 request_type |= USB_DIR_OUT;
116 nWaitTime = B2C2_WAIT_FOR_OPERATION_V8WRITE;
117 // dwRequestType = (u8) RTYPE_WRITE_V8_MEMORY;
118 pipe = B2C2_USB_CTRL_PIPE_OUT;
119 break;
120 case B2C2_USB_FLASH_BLOCK:
121 request_type |= USB_DIR_OUT;
122 nWaitTime = B2C2_WAIT_FOR_OPERATION_V8FLASH;
123 // dwRequestType = (u8) RTYPE_WRITE_V8_FLASH;
124 pipe = B2C2_USB_CTRL_PIPE_OUT;
125 break;
126 default:
127 deb_info("unsupported request for v8_mem_req %x.\n",req);
128 return -EINVAL;
130 deb_v8("v8mem: %02x %02x %04x %04x, len: %d\n",request_type,req,
131 wAddress,wIndex,buflen);
133 len = usb_control_msg(fc_usb->udev,pipe,
134 req,
135 request_type,
136 wAddress,
137 wIndex,
138 pbBuffer,
139 buflen,
140 nWaitTime * HZ);
142 debug_dump(pbBuffer,len,deb_v8);
144 return len == buflen ? 0 : -EIO;
147 #define bytes_left_to_read_on_page(paddr,buflen) \
148 ((V8_MEMORY_PAGE_SIZE - (paddr & V8_MEMORY_PAGE_MASK)) > buflen \
149 ? buflen : (V8_MEMORY_PAGE_SIZE - (paddr & V8_MEMORY_PAGE_MASK)))
151 static int flexcop_usb_memory_req(struct flexcop_usb *fc_usb,flexcop_usb_request_t req,
152 flexcop_usb_mem_page_t page_start, u32 addr, int extended, u8 *buf, u32 len)
154 int i,ret = 0;
155 u16 wMax;
156 u32 pagechunk = 0;
158 switch(req) {
159 case B2C2_USB_READ_V8_MEM: wMax = USB_MEM_READ_MAX; break;
160 case B2C2_USB_WRITE_V8_MEM: wMax = USB_MEM_WRITE_MAX; break;
161 case B2C2_USB_FLASH_BLOCK: wMax = USB_FLASH_MAX; break;
162 default:
163 return -EINVAL;
164 break;
166 for (i = 0; i < len;) {
167 pagechunk = wMax < bytes_left_to_read_on_page(addr,len) ? wMax : bytes_left_to_read_on_page(addr,len);
168 deb_info("%x\n",(addr & V8_MEMORY_PAGE_MASK) | (V8_MEMORY_EXTENDED*extended));
169 if ((ret = flexcop_usb_v8_memory_req(fc_usb,req,
170 page_start + (addr / V8_MEMORY_PAGE_SIZE), /* actual page */
171 (addr & V8_MEMORY_PAGE_MASK) | (V8_MEMORY_EXTENDED*extended),
172 &buf[i],pagechunk)) < 0)
173 return ret;
175 addr += pagechunk;
176 len -= pagechunk;
178 return 0;
181 static int flexcop_usb_get_mac_addr(struct flexcop_device *fc, int extended)
183 return flexcop_usb_memory_req(fc->bus_specific,B2C2_USB_READ_V8_MEM,
184 V8_MEMORY_PAGE_FLASH,0x1f010,1,fc->dvb_adapter.proposed_mac,6);
187 #if 0
188 static int flexcop_usb_utility_req(struct flexcop_usb *fc_usb, int set,
189 flexcop_usb_utility_function_t func, u8 extra, u16 wIndex,
190 u16 buflen, u8 *pvBuffer)
192 u16 wValue;
193 u8 request_type = (set ? USB_DIR_OUT : USB_DIR_IN) | USB_TYPE_VENDOR;
194 // u8 dwRequestType = (u8) RTYPE_GENERIC,
195 int nWaitTime = 2,
196 pipe = set ? B2C2_USB_CTRL_PIPE_OUT : B2C2_USB_CTRL_PIPE_IN,
197 len;
199 wValue = (func << 8) | extra;
201 len = usb_control_msg(fc_usb->udev,pipe,
202 B2C2_USB_UTILITY,
203 request_type,
204 wValue,
205 wIndex,
206 pvBuffer,
207 buflen,
208 nWaitTime * HZ);
209 return len == buflen ? 0 : -EIO;
211 #endif
213 /* usb i2c stuff */
214 static int flexcop_usb_i2c_req(struct flexcop_usb *fc_usb,
215 flexcop_usb_request_t req, flexcop_usb_i2c_function_t func,
216 flexcop_i2c_port_t port, u8 chipaddr, u8 addr, u8 *buf, u8 buflen)
218 u16 wValue, wIndex;
219 int nWaitTime,pipe,len;
220 // u8 dwRequestType;
221 u8 request_type = USB_TYPE_VENDOR;
223 switch (func) {
224 case USB_FUNC_I2C_WRITE:
225 case USB_FUNC_I2C_MULTIWRITE:
226 case USB_FUNC_I2C_REPEATWRITE:
227 /* DKT 020208 - add this to support special case of DiSEqC */
228 case USB_FUNC_I2C_CHECKWRITE:
229 pipe = B2C2_USB_CTRL_PIPE_OUT;
230 nWaitTime = 2;
231 // dwRequestType = (u8) RTYPE_GENERIC;
232 request_type |= USB_DIR_OUT;
233 break;
234 case USB_FUNC_I2C_READ:
235 case USB_FUNC_I2C_REPEATREAD:
236 pipe = B2C2_USB_CTRL_PIPE_IN;
237 nWaitTime = 2;
238 // dwRequestType = (u8) RTYPE_GENERIC;
239 request_type |= USB_DIR_IN;
240 break;
241 default:
242 deb_info("unsupported function for i2c_req %x\n",func);
243 return -EINVAL;
245 wValue = (func << 8 ) | (port << 4);
246 wIndex = (chipaddr << 8 ) | addr;
248 deb_i2c("i2c %2d: %02x %02x %02x %02x %02x %02x\n",func,request_type,req,
249 ((wValue && 0xff) << 8),wValue >> 8,((wIndex && 0xff) << 8),wIndex >> 8);
251 len = usb_control_msg(fc_usb->udev,pipe,
252 req,
253 request_type,
254 wValue,
255 wIndex,
256 buf,
257 buflen,
258 nWaitTime * HZ);
260 return len == buflen ? 0 : -EREMOTEIO;
263 /* actual bus specific access functions, make sure prototype are/will be equal to pci */
264 static flexcop_ibi_value flexcop_usb_read_ibi_reg(struct flexcop_device *fc, flexcop_ibi_register reg)
266 flexcop_ibi_value val;
267 val.raw = 0;
268 flexcop_usb_readwrite_dw(fc,reg, &val.raw, 1);
269 return val;
272 static int flexcop_usb_write_ibi_reg(struct flexcop_device *fc, flexcop_ibi_register reg, flexcop_ibi_value val)
274 return flexcop_usb_readwrite_dw(fc,reg, &val.raw, 0);
277 static int flexcop_usb_i2c_request(struct flexcop_device *fc, flexcop_access_op_t op,
278 flexcop_i2c_port_t port, u8 chipaddr, u8 addr, u8 *buf, u16 len)
280 if (op == FC_READ)
281 return flexcop_usb_i2c_req(fc->bus_specific,B2C2_USB_I2C_REQUEST,USB_FUNC_I2C_READ,port,chipaddr,addr,buf,len);
282 else
283 return flexcop_usb_i2c_req(fc->bus_specific,B2C2_USB_I2C_REQUEST,USB_FUNC_I2C_WRITE,port,chipaddr,addr,buf,len);
286 static void flexcop_usb_process_frame(struct flexcop_usb *fc_usb, u8 *buffer, int buffer_length)
288 u8 *b;
289 int l;
291 deb_ts("tmp_buffer_length=%d, buffer_length=%d\n", fc_usb->tmp_buffer_length, buffer_length);
293 if (fc_usb->tmp_buffer_length > 0) {
294 memcpy(fc_usb->tmp_buffer+fc_usb->tmp_buffer_length, buffer, buffer_length);
295 fc_usb->tmp_buffer_length += buffer_length;
296 b = fc_usb->tmp_buffer;
297 l = fc_usb->tmp_buffer_length;
298 } else {
299 b=buffer;
300 l=buffer_length;
303 while (l >= 190) {
304 if (*b == 0xff)
305 switch (*(b+1) & 0x03) {
306 case 0x01: /* media packet */
307 if ( *(b+2) == 0x47 )
308 flexcop_pass_dmx_packets(fc_usb->fc_dev, b+2, 1);
309 else
310 deb_ts("not ts packet %02x %02x %02x %02x \n", *(b+2), *(b+3), *(b+4), *(b+5) );
312 b += 190;
313 l -= 190;
314 break;
315 default:
316 deb_ts("wrong packet type\n");
317 l = 0;
318 break;
320 else {
321 deb_ts("wrong header\n");
322 l = 0;
326 if (l>0)
327 memcpy(fc_usb->tmp_buffer, b, l);
328 fc_usb->tmp_buffer_length = l;
331 static void flexcop_usb_urb_complete(struct urb *urb, struct pt_regs *ptregs)
333 struct flexcop_usb *fc_usb = urb->context;
334 int i;
336 if (urb->actual_length > 0)
337 deb_ts("urb completed, bufsize: %d actlen; %d\n",urb->transfer_buffer_length, urb->actual_length);
339 for (i = 0; i < urb->number_of_packets; i++) {
340 if (urb->iso_frame_desc[i].status < 0) {
341 err("iso frame descriptor %d has an error: %d\n",i,urb->iso_frame_desc[i].status);
342 } else
343 if (urb->iso_frame_desc[i].actual_length > 0) {
344 deb_ts("passed %d bytes to the demux\n",urb->iso_frame_desc[i].actual_length);
346 flexcop_usb_process_frame(fc_usb,
347 urb->transfer_buffer + urb->iso_frame_desc[i].offset,
348 urb->iso_frame_desc[i].actual_length);
350 urb->iso_frame_desc[i].status = 0;
351 urb->iso_frame_desc[i].actual_length = 0;
354 usb_submit_urb(urb,GFP_ATOMIC);
357 static int flexcop_usb_stream_control(struct flexcop_device *fc, int onoff)
359 /* submit/kill iso packets */
360 return 0;
363 static void flexcop_usb_transfer_exit(struct flexcop_usb *fc_usb)
365 int i;
366 for (i = 0; i < B2C2_USB_NUM_ISO_URB; i++)
367 if (fc_usb->iso_urb[i] != NULL) {
368 deb_ts("unlinking/killing urb no. %d\n",i);
369 usb_kill_urb(fc_usb->iso_urb[i]);
370 usb_free_urb(fc_usb->iso_urb[i]);
373 if (fc_usb->iso_buffer != NULL)
374 pci_free_consistent(NULL,fc_usb->buffer_size, fc_usb->iso_buffer, fc_usb->dma_addr);
377 static int flexcop_usb_transfer_init(struct flexcop_usb *fc_usb)
379 u16 frame_size = fc_usb->uintf->cur_altsetting->endpoint[0].desc.wMaxPacketSize;
380 int bufsize = B2C2_USB_NUM_ISO_URB * B2C2_USB_FRAMES_PER_ISO * frame_size,i,j,ret;
381 int buffer_offset = 0;
383 deb_ts("creating %d iso-urbs with %d frames each of %d bytes size = %d.\n",
384 B2C2_USB_NUM_ISO_URB, B2C2_USB_FRAMES_PER_ISO, frame_size,bufsize);
386 fc_usb->iso_buffer = pci_alloc_consistent(NULL,bufsize,&fc_usb->dma_addr);
387 if (fc_usb->iso_buffer == NULL)
388 return -ENOMEM;
389 memset(fc_usb->iso_buffer, 0, bufsize);
390 fc_usb->buffer_size = bufsize;
392 /* creating iso urbs */
393 for (i = 0; i < B2C2_USB_NUM_ISO_URB; i++)
394 if (!(fc_usb->iso_urb[i] = usb_alloc_urb(B2C2_USB_FRAMES_PER_ISO,GFP_ATOMIC))) {
395 ret = -ENOMEM;
396 goto urb_error;
398 /* initialising and submitting iso urbs */
399 for (i = 0; i < B2C2_USB_NUM_ISO_URB; i++) {
400 int frame_offset = 0;
401 struct urb *urb = fc_usb->iso_urb[i];
402 deb_ts("initializing and submitting urb no. %d (buf_offset: %d).\n",i,buffer_offset);
404 urb->dev = fc_usb->udev;
405 urb->context = fc_usb;
406 urb->complete = flexcop_usb_urb_complete;
407 urb->pipe = B2C2_USB_DATA_PIPE;
408 urb->transfer_flags = URB_ISO_ASAP;
409 urb->interval = 1;
410 urb->number_of_packets = B2C2_USB_FRAMES_PER_ISO;
411 urb->transfer_buffer_length = frame_size * B2C2_USB_FRAMES_PER_ISO;
412 urb->transfer_buffer = fc_usb->iso_buffer + buffer_offset;
414 buffer_offset += frame_size * B2C2_USB_FRAMES_PER_ISO;
415 for (j = 0; j < B2C2_USB_FRAMES_PER_ISO; j++) {
416 deb_ts("urb no: %d, frame: %d, frame_offset: %d\n",i,j,frame_offset);
417 urb->iso_frame_desc[j].offset = frame_offset;
418 urb->iso_frame_desc[j].length = frame_size;
419 frame_offset += frame_size;
422 if ((ret = usb_submit_urb(fc_usb->iso_urb[i],GFP_ATOMIC))) {
423 err("submitting urb %d failed with %d.",i,ret);
424 goto urb_error;
426 deb_ts("submitted urb no. %d.\n",i);
429 /* SRAM */
431 flexcop_sram_set_dest(fc_usb->fc_dev,FC_SRAM_DEST_MEDIA | FC_SRAM_DEST_NET |
432 FC_SRAM_DEST_CAO | FC_SRAM_DEST_CAI, FC_SRAM_DEST_TARGET_WAN_USB);
433 flexcop_wan_set_speed(fc_usb->fc_dev,FC_WAN_SPEED_8MBITS);
434 flexcop_sram_ctrl(fc_usb->fc_dev,1,1,1);
436 ret = 0;
437 goto success;
438 urb_error:
439 flexcop_usb_transfer_exit(fc_usb);
440 success:
441 return ret;
444 static int flexcop_usb_init(struct flexcop_usb *fc_usb)
446 /* use the alternate setting with the larges buffer */
447 usb_set_interface(fc_usb->udev,0,1);
448 switch (fc_usb->udev->speed) {
449 case USB_SPEED_LOW:
450 err("cannot handle USB speed because it is to sLOW.");
451 return -ENODEV;
452 break;
453 case USB_SPEED_FULL:
454 info("running at FULL speed.");
455 break;
456 case USB_SPEED_HIGH:
457 info("running at HIGH speed.");
458 break;
459 case USB_SPEED_UNKNOWN: /* fall through */
460 default:
461 err("cannot handle USB speed because it is unkown.");
462 return -ENODEV;
464 usb_set_intfdata(fc_usb->uintf, fc_usb);
465 return 0;
468 static void flexcop_usb_exit(struct flexcop_usb *fc_usb)
470 usb_set_intfdata(fc_usb->uintf, NULL);
473 static int flexcop_usb_probe(struct usb_interface *intf,
474 const struct usb_device_id *id)
476 struct usb_device *udev = interface_to_usbdev(intf);
477 struct flexcop_usb *fc_usb = NULL;
478 struct flexcop_device *fc = NULL;
479 int ret;
481 if ((fc = flexcop_device_kmalloc(sizeof(struct flexcop_usb))) == NULL) {
482 err("out of memory\n");
483 return -ENOMEM;
486 /* general flexcop init */
487 fc_usb = fc->bus_specific;
488 fc_usb->fc_dev = fc;
490 fc->read_ibi_reg = flexcop_usb_read_ibi_reg;
491 fc->write_ibi_reg = flexcop_usb_write_ibi_reg;
492 fc->i2c_request = flexcop_usb_i2c_request;
493 fc->get_mac_addr = flexcop_usb_get_mac_addr;
495 fc->stream_control = flexcop_usb_stream_control;
497 fc->pid_filtering = 1;
498 fc->bus_type = FC_USB;
500 fc->dev = &udev->dev;
501 fc->owner = THIS_MODULE;
503 /* bus specific part */
504 fc_usb->udev = udev;
505 fc_usb->uintf = intf;
506 if ((ret = flexcop_usb_init(fc_usb)) != 0)
507 goto err_kfree;
509 /* init flexcop */
510 if ((ret = flexcop_device_initialize(fc)) != 0)
511 goto err_usb_exit;
513 /* xfer init */
514 if ((ret = flexcop_usb_transfer_init(fc_usb)) != 0)
515 goto err_fc_exit;
517 info("%s successfully initialized and connected.",DRIVER_NAME);
518 ret = 0;
519 goto success;
520 err_fc_exit:
521 flexcop_device_exit(fc);
522 err_usb_exit:
523 flexcop_usb_exit(fc_usb);
524 err_kfree:
525 flexcop_device_kfree(fc);
526 success:
527 return ret;
530 static void flexcop_usb_disconnect(struct usb_interface *intf)
532 struct flexcop_usb *fc_usb = usb_get_intfdata(intf);
533 flexcop_usb_transfer_exit(fc_usb);
534 flexcop_device_exit(fc_usb->fc_dev);
535 flexcop_usb_exit(fc_usb);
536 flexcop_device_kfree(fc_usb->fc_dev);
537 info("%s successfully deinitialized and disconnected.",DRIVER_NAME);
540 static struct usb_device_id flexcop_usb_table [] = {
541 { USB_DEVICE(0x0af7, 0x0101) },
545 /* usb specific object needed to register this driver with the usb subsystem */
546 static struct usb_driver flexcop_usb_driver = {
547 .owner = THIS_MODULE,
548 .name = "Technisat/B2C2 FlexCop II/IIb/III USB",
549 .probe = flexcop_usb_probe,
550 .disconnect = flexcop_usb_disconnect,
551 .id_table = flexcop_usb_table,
554 /* module stuff */
555 static int __init flexcop_usb_module_init(void)
557 int result;
558 if ((result = usb_register(&flexcop_usb_driver))) {
559 err("usb_register failed. (%d)",result);
560 return result;
563 return 0;
566 static void __exit flexcop_usb_module_exit(void)
568 /* deregister this driver from the USB subsystem */
569 usb_deregister(&flexcop_usb_driver);
572 module_init(flexcop_usb_module_init);
573 module_exit(flexcop_usb_module_exit);
575 MODULE_AUTHOR(DRIVER_AUTHOR);
576 MODULE_DESCRIPTION(DRIVER_NAME);
577 MODULE_LICENSE("GPL");