1 // SPDX-License-Identifier: GPL-2.0-only
3 * Linux driver for digital TV devices equipped with B2C2 FlexcopII(b)/III
4 * flexcop-usb.c - covers the USB part
5 * see flexcop.c for copyright information
7 #define FC_LOG_PREFIX "flexcop_usb"
8 #include "flexcop-usb.h"
9 #include "flexcop-common.h"
11 /* Version information */
12 #define DRIVER_VERSION "0.1"
13 #define DRIVER_NAME "Technisat/B2C2 FlexCop II/IIb/III Digital TV USB Driver"
14 #define DRIVER_AUTHOR "Patrick Boettcher <patrick.boettcher@posteo.de>"
17 #ifdef CONFIG_DVB_B2C2_FLEXCOP_DEBUG
18 #define dprintk(level,args...) \
19 do { if ((debug & level)) printk(args); } while (0)
21 #define debug_dump(b, l, method) do {\
23 for (i = 0; i < l; i++) \
24 method("%02x ", b[i]); \
30 #define dprintk(level, args...)
31 #define debug_dump(b, l, method)
32 #define DEBSTATUS " (debugging is not enabled)"
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
);
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 (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) \
56 (((usPCI >> 2) & 0x07) + ((usPCI >> 4) & 0x70))
57 #define B2C2_FLEX_INTERNALADDR_TO_PCIOFFSET(ucAddr) (u16) \
58 (((ucAddr & 0x07) << 2) + ((ucAddr & 0x70) << 4))
62 * - forget about this VENDOR_BUFFER_SIZE, read and write register
63 * deal with DWORD or 4 bytes, that should be should from now on
64 * - from now on, we don't support anything older than firm 1.00
65 * I eliminated the write register as a 2 trip of writing hi word and lo word
66 * and force this to write only 4 bytes at a time.
67 * NOTE: this should work with all the firmware from 1.00 and newer
69 static int flexcop_usb_readwrite_dw(struct flexcop_device
*fc
, u16 wRegOffsPCI
, u32
*val
, u8 read
)
71 struct flexcop_usb
*fc_usb
= fc
->bus_specific
;
72 u8 request
= read
? B2C2_USB_READ_REG
: B2C2_USB_WRITE_REG
;
73 u8 request_type
= (read
? USB_DIR_IN
: USB_DIR_OUT
) | USB_TYPE_VENDOR
;
74 u8 wAddress
= B2C2_FLEX_PCIOFFSET_TO_INTERNALADDR(wRegOffsPCI
) |
78 mutex_lock(&fc_usb
->data_mutex
);
80 memcpy(fc_usb
->data
, val
, sizeof(*val
));
82 ret
= usb_control_msg(fc_usb
->udev
,
83 read
? B2C2_USB_CTRL_PIPE_IN
: B2C2_USB_CTRL_PIPE_OUT
,
85 request_type
, /* 0xc0 read or 0x40 write */
90 B2C2_WAIT_FOR_OPERATION_RDW
* HZ
);
92 if (ret
!= sizeof(u32
)) {
93 err("error while %s dword from %d (%d).", read
? "reading" :
94 "writing", wAddress
, wRegOffsPCI
);
100 memcpy(val
, fc_usb
->data
, sizeof(*val
));
101 mutex_unlock(&fc_usb
->data_mutex
);
106 * DKT 010817 - add support for V8 memory read/write and flash update
108 static int flexcop_usb_v8_memory_req(struct flexcop_usb
*fc_usb
,
109 flexcop_usb_request_t req
, u8 page
, u16 wAddress
,
110 u8
*pbBuffer
, u32 buflen
)
112 u8 request_type
= USB_TYPE_VENDOR
;
114 int nWaitTime
, pipe
, ret
;
117 if (buflen
> sizeof(fc_usb
->data
)) {
118 err("Buffer size bigger than max URB control message\n");
123 case B2C2_USB_READ_V8_MEM
:
124 nWaitTime
= B2C2_WAIT_FOR_OPERATION_V8READ
;
125 request_type
|= USB_DIR_IN
;
126 pipe
= B2C2_USB_CTRL_PIPE_IN
;
128 case B2C2_USB_WRITE_V8_MEM
:
129 wIndex
|= pbBuffer
[0];
130 request_type
|= USB_DIR_OUT
;
131 nWaitTime
= B2C2_WAIT_FOR_OPERATION_V8WRITE
;
132 pipe
= B2C2_USB_CTRL_PIPE_OUT
;
134 case B2C2_USB_FLASH_BLOCK
:
135 request_type
|= USB_DIR_OUT
;
136 nWaitTime
= B2C2_WAIT_FOR_OPERATION_V8FLASH
;
137 pipe
= B2C2_USB_CTRL_PIPE_OUT
;
140 deb_info("unsupported request for v8_mem_req %x.\n", req
);
143 deb_v8("v8mem: %02x %02x %04x %04x, len: %d\n", request_type
, req
,
144 wAddress
, wIndex
, buflen
);
146 mutex_lock(&fc_usb
->data_mutex
);
148 if ((request_type
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
)
149 memcpy(fc_usb
->data
, pbBuffer
, buflen
);
151 ret
= usb_control_msg(fc_usb
->udev
, pipe
,
164 if ((request_type
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_IN
)
165 memcpy(pbBuffer
, fc_usb
->data
, buflen
);
168 mutex_unlock(&fc_usb
->data_mutex
);
170 debug_dump(pbBuffer
, ret
, deb_v8
);
174 #define bytes_left_to_read_on_page(paddr,buflen) \
175 ((V8_MEMORY_PAGE_SIZE - (paddr & V8_MEMORY_PAGE_MASK)) > buflen \
176 ? buflen : (V8_MEMORY_PAGE_SIZE - (paddr & V8_MEMORY_PAGE_MASK)))
178 static int flexcop_usb_memory_req(struct flexcop_usb
*fc_usb
,
179 flexcop_usb_request_t req
, flexcop_usb_mem_page_t page_start
,
180 u32 addr
, int extended
, u8
*buf
, u32 len
)
187 case B2C2_USB_READ_V8_MEM
:
188 wMax
= USB_MEM_READ_MAX
;
190 case B2C2_USB_WRITE_V8_MEM
:
191 wMax
= USB_MEM_WRITE_MAX
;
193 case B2C2_USB_FLASH_BLOCK
:
194 wMax
= USB_FLASH_MAX
;
200 for (i
= 0; i
< len
;) {
202 wMax
< bytes_left_to_read_on_page(addr
, len
) ?
204 bytes_left_to_read_on_page(addr
, len
);
206 (addr
& V8_MEMORY_PAGE_MASK
) |
207 (V8_MEMORY_EXTENDED
*extended
));
209 ret
= flexcop_usb_v8_memory_req(fc_usb
, req
,
210 page_start
+ (addr
/ V8_MEMORY_PAGE_SIZE
),
211 (addr
& V8_MEMORY_PAGE_MASK
) |
212 (V8_MEMORY_EXTENDED
*extended
),
223 static int flexcop_usb_get_mac_addr(struct flexcop_device
*fc
, int extended
)
225 return flexcop_usb_memory_req(fc
->bus_specific
, B2C2_USB_READ_V8_MEM
,
226 V8_MEMORY_PAGE_FLASH
, 0x1f010, 1,
227 fc
->dvb_adapter
.proposed_mac
, 6);
231 static int flexcop_usb_i2c_req(struct flexcop_i2c_adapter
*i2c
,
232 flexcop_usb_request_t req
, flexcop_usb_i2c_function_t func
,
233 u8 chipaddr
, u8 addr
, u8
*buf
, u8 buflen
)
235 struct flexcop_usb
*fc_usb
= i2c
->fc
->bus_specific
;
237 int nWaitTime
, pipe
, ret
;
238 u8 request_type
= USB_TYPE_VENDOR
;
240 if (buflen
> sizeof(fc_usb
->data
)) {
241 err("Buffer size bigger than max URB control message\n");
246 case USB_FUNC_I2C_WRITE
:
247 case USB_FUNC_I2C_MULTIWRITE
:
248 case USB_FUNC_I2C_REPEATWRITE
:
249 /* DKT 020208 - add this to support special case of DiSEqC */
250 case USB_FUNC_I2C_CHECKWRITE
:
251 pipe
= B2C2_USB_CTRL_PIPE_OUT
;
253 request_type
|= USB_DIR_OUT
;
255 case USB_FUNC_I2C_READ
:
256 case USB_FUNC_I2C_REPEATREAD
:
257 pipe
= B2C2_USB_CTRL_PIPE_IN
;
259 request_type
|= USB_DIR_IN
;
262 deb_info("unsupported function for i2c_req %x\n", func
);
265 wValue
= (func
<< 8) | (i2c
->port
<< 4);
266 wIndex
= (chipaddr
<< 8 ) | addr
;
268 deb_i2c("i2c %2d: %02x %02x %02x %02x %02x %02x\n",
269 func
, request_type
, req
,
270 wValue
& 0xff, wValue
>> 8,
271 wIndex
& 0xff, wIndex
>> 8);
273 mutex_lock(&fc_usb
->data_mutex
);
275 if ((request_type
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
)
276 memcpy(fc_usb
->data
, buf
, buflen
);
278 ret
= usb_control_msg(fc_usb
->udev
, pipe
,
292 if ((request_type
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_IN
)
293 memcpy(buf
, fc_usb
->data
, buflen
);
296 mutex_unlock(&fc_usb
->data_mutex
);
301 /* actual bus specific access functions,
302 make sure prototype are/will be equal to pci */
303 static flexcop_ibi_value
flexcop_usb_read_ibi_reg(struct flexcop_device
*fc
,
304 flexcop_ibi_register reg
)
306 flexcop_ibi_value val
;
308 flexcop_usb_readwrite_dw(fc
, reg
, &val
.raw
, 1);
312 static int flexcop_usb_write_ibi_reg(struct flexcop_device
*fc
,
313 flexcop_ibi_register reg
, flexcop_ibi_value val
)
315 return flexcop_usb_readwrite_dw(fc
, reg
, &val
.raw
, 0);
318 static int flexcop_usb_i2c_request(struct flexcop_i2c_adapter
*i2c
,
319 flexcop_access_op_t op
, u8 chipaddr
, u8 addr
, u8
*buf
, u16 len
)
322 return flexcop_usb_i2c_req(i2c
, B2C2_USB_I2C_REQUEST
,
323 USB_FUNC_I2C_READ
, chipaddr
, addr
, buf
, len
);
325 return flexcop_usb_i2c_req(i2c
, B2C2_USB_I2C_REQUEST
,
326 USB_FUNC_I2C_WRITE
, chipaddr
, addr
, buf
, len
);
329 static void flexcop_usb_process_frame(struct flexcop_usb
*fc_usb
,
330 u8
*buffer
, int buffer_length
)
335 deb_ts("tmp_buffer_length=%d, buffer_length=%d\n",
336 fc_usb
->tmp_buffer_length
, buffer_length
);
338 if (fc_usb
->tmp_buffer_length
> 0) {
339 memcpy(fc_usb
->tmp_buffer
+fc_usb
->tmp_buffer_length
, buffer
,
341 fc_usb
->tmp_buffer_length
+= buffer_length
;
342 b
= fc_usb
->tmp_buffer
;
343 l
= fc_usb
->tmp_buffer_length
;
351 switch (*(b
+1) & 0x03) {
352 case 0x01: /* media packet */
354 flexcop_pass_dmx_packets(
355 fc_usb
->fc_dev
, b
+2, 1);
357 deb_ts("not ts packet %*ph\n", 4, b
+2);
362 deb_ts("wrong packet type\n");
367 deb_ts("wrong header\n");
373 memcpy(fc_usb
->tmp_buffer
, b
, l
);
374 fc_usb
->tmp_buffer_length
= l
;
377 static void flexcop_usb_urb_complete(struct urb
*urb
)
379 struct flexcop_usb
*fc_usb
= urb
->context
;
382 if (urb
->actual_length
> 0)
383 deb_ts("urb completed, bufsize: %d actlen; %d\n",
384 urb
->transfer_buffer_length
, urb
->actual_length
);
386 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
387 if (urb
->iso_frame_desc
[i
].status
< 0) {
388 err("iso frame descriptor %d has an error: %d\n", i
,
389 urb
->iso_frame_desc
[i
].status
);
391 if (urb
->iso_frame_desc
[i
].actual_length
> 0) {
392 deb_ts("passed %d bytes to the demux\n",
393 urb
->iso_frame_desc
[i
].actual_length
);
395 flexcop_usb_process_frame(fc_usb
,
396 urb
->transfer_buffer
+
397 urb
->iso_frame_desc
[i
].offset
,
398 urb
->iso_frame_desc
[i
].actual_length
);
400 urb
->iso_frame_desc
[i
].status
= 0;
401 urb
->iso_frame_desc
[i
].actual_length
= 0;
403 usb_submit_urb(urb
,GFP_ATOMIC
);
406 static int flexcop_usb_stream_control(struct flexcop_device
*fc
, int onoff
)
408 /* submit/kill iso packets */
412 static void flexcop_usb_transfer_exit(struct flexcop_usb
*fc_usb
)
415 for (i
= 0; i
< B2C2_USB_NUM_ISO_URB
; i
++)
416 if (fc_usb
->iso_urb
[i
] != NULL
) {
417 deb_ts("unlinking/killing urb no. %d\n",i
);
418 usb_kill_urb(fc_usb
->iso_urb
[i
]);
419 usb_free_urb(fc_usb
->iso_urb
[i
]);
422 usb_free_coherent(fc_usb
->udev
, fc_usb
->buffer_size
,
423 fc_usb
->iso_buffer
, fc_usb
->dma_addr
);
427 static int flexcop_usb_transfer_init(struct flexcop_usb
*fc_usb
)
429 u16 frame_size
= le16_to_cpu(
430 fc_usb
->uintf
->cur_altsetting
->endpoint
[0].desc
.wMaxPacketSize
);
431 int bufsize
= B2C2_USB_NUM_ISO_URB
* B2C2_USB_FRAMES_PER_ISO
*
432 frame_size
, i
, j
, ret
;
433 int buffer_offset
= 0;
435 deb_ts("creating %d iso-urbs with %d frames each of %d bytes size = %d.\n",
436 B2C2_USB_NUM_ISO_URB
,
437 B2C2_USB_FRAMES_PER_ISO
, frame_size
, bufsize
);
439 fc_usb
->iso_buffer
= usb_alloc_coherent(fc_usb
->udev
,
440 bufsize
, GFP_KERNEL
, &fc_usb
->dma_addr
);
441 if (fc_usb
->iso_buffer
== NULL
)
444 memset(fc_usb
->iso_buffer
, 0, bufsize
);
445 fc_usb
->buffer_size
= bufsize
;
447 /* creating iso urbs */
448 for (i
= 0; i
< B2C2_USB_NUM_ISO_URB
; i
++) {
449 fc_usb
->iso_urb
[i
] = usb_alloc_urb(B2C2_USB_FRAMES_PER_ISO
,
451 if (fc_usb
->iso_urb
[i
] == NULL
) {
457 /* initialising and submitting iso urbs */
458 for (i
= 0; i
< B2C2_USB_NUM_ISO_URB
; i
++) {
459 int frame_offset
= 0;
460 struct urb
*urb
= fc_usb
->iso_urb
[i
];
461 deb_ts("initializing and submitting urb no. %d (buf_offset: %d).\n",
464 urb
->dev
= fc_usb
->udev
;
465 urb
->context
= fc_usb
;
466 urb
->complete
= flexcop_usb_urb_complete
;
467 urb
->pipe
= B2C2_USB_DATA_PIPE
;
468 urb
->transfer_flags
= URB_ISO_ASAP
;
470 urb
->number_of_packets
= B2C2_USB_FRAMES_PER_ISO
;
471 urb
->transfer_buffer_length
= frame_size
* B2C2_USB_FRAMES_PER_ISO
;
472 urb
->transfer_buffer
= fc_usb
->iso_buffer
+ buffer_offset
;
474 buffer_offset
+= frame_size
* B2C2_USB_FRAMES_PER_ISO
;
475 for (j
= 0; j
< B2C2_USB_FRAMES_PER_ISO
; j
++) {
476 deb_ts("urb no: %d, frame: %d, frame_offset: %d\n",
478 urb
->iso_frame_desc
[j
].offset
= frame_offset
;
479 urb
->iso_frame_desc
[j
].length
= frame_size
;
480 frame_offset
+= frame_size
;
483 if ((ret
= usb_submit_urb(fc_usb
->iso_urb
[i
],GFP_ATOMIC
))) {
484 err("submitting urb %d failed with %d.", i
, ret
);
487 deb_ts("submitted urb no. %d.\n",i
);
491 flexcop_sram_set_dest(fc_usb
->fc_dev
, FC_SRAM_DEST_MEDIA
|
492 FC_SRAM_DEST_NET
| FC_SRAM_DEST_CAO
| FC_SRAM_DEST_CAI
,
493 FC_SRAM_DEST_TARGET_WAN_USB
);
494 flexcop_wan_set_speed(fc_usb
->fc_dev
, FC_WAN_SPEED_8MBITS
);
495 flexcop_sram_ctrl(fc_usb
->fc_dev
, 1, 1, 1);
499 flexcop_usb_transfer_exit(fc_usb
);
503 static int flexcop_usb_init(struct flexcop_usb
*fc_usb
)
505 /* use the alternate setting with the larges buffer */
506 int ret
= usb_set_interface(fc_usb
->udev
, 0, 1);
509 err("set interface failed.");
513 if (fc_usb
->uintf
->cur_altsetting
->desc
.bNumEndpoints
< 1)
515 if (!usb_endpoint_is_isoc_in(&fc_usb
->uintf
->cur_altsetting
->endpoint
[1].desc
))
518 switch (fc_usb
->udev
->speed
) {
520 err("cannot handle USB speed because it is too slow.");
524 info("running at FULL speed.");
527 info("running at HIGH speed.");
529 case USB_SPEED_UNKNOWN
:
531 err("cannot handle USB speed because it is unknown.");
534 usb_set_intfdata(fc_usb
->uintf
, fc_usb
);
538 static void flexcop_usb_exit(struct flexcop_usb
*fc_usb
)
540 usb_set_intfdata(fc_usb
->uintf
, NULL
);
543 static int flexcop_usb_probe(struct usb_interface
*intf
,
544 const struct usb_device_id
*id
)
546 struct usb_device
*udev
= interface_to_usbdev(intf
);
547 struct flexcop_usb
*fc_usb
= NULL
;
548 struct flexcop_device
*fc
= NULL
;
551 if ((fc
= flexcop_device_kmalloc(sizeof(struct flexcop_usb
))) == NULL
) {
552 err("out of memory\n");
556 /* general flexcop init */
557 fc_usb
= fc
->bus_specific
;
559 mutex_init(&fc_usb
->data_mutex
);
561 fc
->read_ibi_reg
= flexcop_usb_read_ibi_reg
;
562 fc
->write_ibi_reg
= flexcop_usb_write_ibi_reg
;
563 fc
->i2c_request
= flexcop_usb_i2c_request
;
564 fc
->get_mac_addr
= flexcop_usb_get_mac_addr
;
566 fc
->stream_control
= flexcop_usb_stream_control
;
568 fc
->pid_filtering
= 1;
569 fc
->bus_type
= FC_USB
;
571 fc
->dev
= &udev
->dev
;
572 fc
->owner
= THIS_MODULE
;
574 /* bus specific part */
576 fc_usb
->uintf
= intf
;
577 if ((ret
= flexcop_usb_init(fc_usb
)) != 0)
581 if ((ret
= flexcop_device_initialize(fc
)) != 0)
585 if ((ret
= flexcop_usb_transfer_init(fc_usb
)) != 0)
588 info("%s successfully initialized and connected.", DRIVER_NAME
);
592 flexcop_device_exit(fc
);
594 flexcop_usb_exit(fc_usb
);
596 flexcop_device_kfree(fc
);
600 static void flexcop_usb_disconnect(struct usb_interface
*intf
)
602 struct flexcop_usb
*fc_usb
= usb_get_intfdata(intf
);
603 flexcop_usb_transfer_exit(fc_usb
);
604 flexcop_device_exit(fc_usb
->fc_dev
);
605 flexcop_usb_exit(fc_usb
);
606 flexcop_device_kfree(fc_usb
->fc_dev
);
607 info("%s successfully deinitialized and disconnected.", DRIVER_NAME
);
610 static const struct usb_device_id flexcop_usb_table
[] = {
611 { USB_DEVICE(0x0af7, 0x0101) },
614 MODULE_DEVICE_TABLE (usb
, flexcop_usb_table
);
616 /* usb specific object needed to register this driver with the usb subsystem */
617 static struct usb_driver flexcop_usb_driver
= {
618 .name
= "b2c2_flexcop_usb",
619 .probe
= flexcop_usb_probe
,
620 .disconnect
= flexcop_usb_disconnect
,
621 .id_table
= flexcop_usb_table
,
624 module_usb_driver(flexcop_usb_driver
);
626 MODULE_AUTHOR(DRIVER_AUTHOR
);
627 MODULE_DESCRIPTION(DRIVER_NAME
);
628 MODULE_LICENSE("GPL");