1
2
3
4
5
6#define FC_LOG_PREFIX "flexcop_usb"
7#include "flexcop-usb.h"
8#include "flexcop-common.h"
9
10
11#define DRIVER_VERSION "0.1"
12#define DRIVER_NAME "Technisat/B2C2 FlexCop II/IIb/III Digital TV USB Driver"
13#define DRIVER_AUTHOR "Patrick Boettcher <patrick.boettcher@posteo.de>"
14
15
16#ifdef CONFIG_DVB_B2C2_FLEXCOP_DEBUG
17#define dprintk(level,args...) \
18 do { if ((debug & level)) printk(args); } while (0)
19
20#define debug_dump(b, l, method) do {\
21 int i; \
22 for (i = 0; i < l; i++) \
23 method("%02x ", b[i]); \
24 method("\n"); \
25} while (0)
26
27#define DEBSTATUS ""
28#else
29#define dprintk(level, args...)
30#define debug_dump(b, l, method)
31#define DEBSTATUS " (debugging is not enabled)"
32#endif
33
34static int debug;
35module_param(debug, int, 0644);
36MODULE_PARM_DESC(debug, "set debugging level (1=info,ts=2,ctrl=4,i2c=8,v8mem=16 (or-able))." DEBSTATUS);
37#undef DEBSTATUS
38
39#define deb_info(args...) dprintk(0x01, args)
40#define deb_ts(args...) dprintk(0x02, args)
41#define deb_ctrl(args...) dprintk(0x04, args)
42#define deb_i2c(args...) dprintk(0x08, args)
43#define deb_v8(args...) dprintk(0x10, args)
44
45
46
47
48
49
50
51
52
53
54#define B2C2_FLEX_PCIOFFSET_TO_INTERNALADDR(usPCI) (u8) \
55 (((usPCI >> 2) & 0x07) + ((usPCI >> 4) & 0x70))
56#define B2C2_FLEX_INTERNALADDR_TO_PCIOFFSET(ucAddr) (u16) \
57 (((ucAddr & 0x07) << 2) + ((ucAddr & 0x70) << 4))
58
59
60
61
62
63
64
65
66
67
68static int flexcop_usb_readwrite_dw(struct flexcop_device *fc, u16 wRegOffsPCI, u32 *val, u8 read)
69{
70 struct flexcop_usb *fc_usb = fc->bus_specific;
71 u8 request = read ? B2C2_USB_READ_REG : B2C2_USB_WRITE_REG;
72 u8 request_type = (read ? USB_DIR_IN : USB_DIR_OUT) | USB_TYPE_VENDOR;
73 u8 wAddress = B2C2_FLEX_PCIOFFSET_TO_INTERNALADDR(wRegOffsPCI) |
74 (read ? 0x80 : 0);
75 int ret;
76
77 mutex_lock(&fc_usb->data_mutex);
78 if (!read)
79 memcpy(fc_usb->data, val, sizeof(*val));
80
81 ret = usb_control_msg(fc_usb->udev,
82 read ? B2C2_USB_CTRL_PIPE_IN : B2C2_USB_CTRL_PIPE_OUT,
83 request,
84 request_type,
85 wAddress,
86 0,
87 fc_usb->data,
88 sizeof(u32),
89 B2C2_WAIT_FOR_OPERATION_RDW * HZ);
90
91 if (ret != sizeof(u32)) {
92 err("error while %s dword from %d (%d).", read ? "reading" :
93 "writing", wAddress, wRegOffsPCI);
94 if (ret >= 0)
95 ret = -EIO;
96 }
97
98 if (read && ret >= 0)
99 memcpy(val, fc_usb->data, sizeof(*val));
100 mutex_unlock(&fc_usb->data_mutex);
101
102 return ret;
103}
104
105
106
107static int flexcop_usb_v8_memory_req(struct flexcop_usb *fc_usb,
108 flexcop_usb_request_t req, u8 page, u16 wAddress,
109 u8 *pbBuffer, u32 buflen)
110{
111 u8 request_type = USB_TYPE_VENDOR;
112 u16 wIndex;
113 int nWaitTime, pipe, ret;
114 wIndex = page << 8;
115
116 if (buflen > sizeof(fc_usb->data)) {
117 err("Buffer size bigger than max URB control message\n");
118 return -EIO;
119 }
120
121 switch (req) {
122 case B2C2_USB_READ_V8_MEM:
123 nWaitTime = B2C2_WAIT_FOR_OPERATION_V8READ;
124 request_type |= USB_DIR_IN;
125 pipe = B2C2_USB_CTRL_PIPE_IN;
126 break;
127 case B2C2_USB_WRITE_V8_MEM:
128 wIndex |= pbBuffer[0];
129 request_type |= USB_DIR_OUT;
130 nWaitTime = B2C2_WAIT_FOR_OPERATION_V8WRITE;
131 pipe = B2C2_USB_CTRL_PIPE_OUT;
132 break;
133 case B2C2_USB_FLASH_BLOCK:
134 request_type |= USB_DIR_OUT;
135 nWaitTime = B2C2_WAIT_FOR_OPERATION_V8FLASH;
136 pipe = B2C2_USB_CTRL_PIPE_OUT;
137 break;
138 default:
139 deb_info("unsupported request for v8_mem_req %x.\n", req);
140 return -EINVAL;
141 }
142 deb_v8("v8mem: %02x %02x %04x %04x, len: %d\n", request_type, req,
143 wAddress, wIndex, buflen);
144
145 mutex_lock(&fc_usb->data_mutex);
146
147 if ((request_type & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)
148 memcpy(fc_usb->data, pbBuffer, buflen);
149
150 ret = usb_control_msg(fc_usb->udev, pipe,
151 req,
152 request_type,
153 wAddress,
154 wIndex,
155 fc_usb->data,
156 buflen,
157 nWaitTime * HZ);
158 if (ret != buflen)
159 ret = -EIO;
160
161 if (ret >= 0) {
162 ret = 0;
163 if ((request_type & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
164 memcpy(pbBuffer, fc_usb->data, buflen);
165 }
166
167 mutex_unlock(&fc_usb->data_mutex);
168
169 debug_dump(pbBuffer, ret, deb_v8);
170 return ret;
171}
172
173#define bytes_left_to_read_on_page(paddr,buflen) \
174 ((V8_MEMORY_PAGE_SIZE - (paddr & V8_MEMORY_PAGE_MASK)) > buflen \
175 ? buflen : (V8_MEMORY_PAGE_SIZE - (paddr & V8_MEMORY_PAGE_MASK)))
176
177static int flexcop_usb_memory_req(struct flexcop_usb *fc_usb,
178 flexcop_usb_request_t req, flexcop_usb_mem_page_t page_start,
179 u32 addr, int extended, u8 *buf, u32 len)
180{
181 int i,ret = 0;
182 u16 wMax;
183 u32 pagechunk = 0;
184
185 switch(req) {
186 case B2C2_USB_READ_V8_MEM:
187 wMax = USB_MEM_READ_MAX;
188 break;
189 case B2C2_USB_WRITE_V8_MEM:
190 wMax = USB_MEM_WRITE_MAX;
191 break;
192 case B2C2_USB_FLASH_BLOCK:
193 wMax = USB_FLASH_MAX;
194 break;
195 default:
196 return -EINVAL;
197 break;
198 }
199 for (i = 0; i < len;) {
200 pagechunk =
201 wMax < bytes_left_to_read_on_page(addr, len) ?
202 wMax :
203 bytes_left_to_read_on_page(addr, len);
204 deb_info("%x\n",
205 (addr & V8_MEMORY_PAGE_MASK) |
206 (V8_MEMORY_EXTENDED*extended));
207
208 ret = flexcop_usb_v8_memory_req(fc_usb, req,
209 page_start + (addr / V8_MEMORY_PAGE_SIZE),
210 (addr & V8_MEMORY_PAGE_MASK) |
211 (V8_MEMORY_EXTENDED*extended),
212 &buf[i], pagechunk);
213
214 if (ret < 0)
215 return ret;
216 addr += pagechunk;
217 len -= pagechunk;
218 }
219 return 0;
220}
221
222static int flexcop_usb_get_mac_addr(struct flexcop_device *fc, int extended)
223{
224 return flexcop_usb_memory_req(fc->bus_specific, B2C2_USB_READ_V8_MEM,
225 V8_MEMORY_PAGE_FLASH, 0x1f010, 1,
226 fc->dvb_adapter.proposed_mac, 6);
227}
228
229
230static int flexcop_usb_i2c_req(struct flexcop_i2c_adapter *i2c,
231 flexcop_usb_request_t req, flexcop_usb_i2c_function_t func,
232 u8 chipaddr, u8 addr, u8 *buf, u8 buflen)
233{
234 struct flexcop_usb *fc_usb = i2c->fc->bus_specific;
235 u16 wValue, wIndex;
236 int nWaitTime, pipe, ret;
237 u8 request_type = USB_TYPE_VENDOR;
238
239 if (buflen > sizeof(fc_usb->data)) {
240 err("Buffer size bigger than max URB control message\n");
241 return -EIO;
242 }
243
244 switch (func) {
245 case USB_FUNC_I2C_WRITE:
246 case USB_FUNC_I2C_MULTIWRITE:
247 case USB_FUNC_I2C_REPEATWRITE:
248
249 case USB_FUNC_I2C_CHECKWRITE:
250 pipe = B2C2_USB_CTRL_PIPE_OUT;
251 nWaitTime = 2;
252 request_type |= USB_DIR_OUT;
253 break;
254 case USB_FUNC_I2C_READ:
255 case USB_FUNC_I2C_REPEATREAD:
256 pipe = B2C2_USB_CTRL_PIPE_IN;
257 nWaitTime = 2;
258 request_type |= USB_DIR_IN;
259 break;
260 default:
261 deb_info("unsupported function for i2c_req %x\n", func);
262 return -EINVAL;
263 }
264 wValue = (func << 8) | (i2c->port << 4);
265 wIndex = (chipaddr << 8 ) | addr;
266
267 deb_i2c("i2c %2d: %02x %02x %02x %02x %02x %02x\n",
268 func, request_type, req,
269 wValue & 0xff, wValue >> 8,
270 wIndex & 0xff, wIndex >> 8);
271
272 mutex_lock(&fc_usb->data_mutex);
273
274 if ((request_type & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)
275 memcpy(fc_usb->data, buf, buflen);
276
277 ret = usb_control_msg(fc_usb->udev, pipe,
278 req,
279 request_type,
280 wValue,
281 wIndex,
282 fc_usb->data,
283 buflen,
284 nWaitTime * HZ);
285
286 if (ret != buflen)
287 ret = -EIO;
288
289 if (ret >= 0) {
290 ret = 0;
291 if ((request_type & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
292 memcpy(buf, fc_usb->data, buflen);
293 }
294
295 mutex_unlock(&fc_usb->data_mutex);
296
297 return 0;
298}
299
300
301
302static flexcop_ibi_value flexcop_usb_read_ibi_reg(struct flexcop_device *fc,
303 flexcop_ibi_register reg)
304{
305 flexcop_ibi_value val;
306 val.raw = 0;
307 flexcop_usb_readwrite_dw(fc, reg, &val.raw, 1);
308 return val;
309}
310
311static int flexcop_usb_write_ibi_reg(struct flexcop_device *fc,
312 flexcop_ibi_register reg, flexcop_ibi_value val)
313{
314 return flexcop_usb_readwrite_dw(fc, reg, &val.raw, 0);
315}
316
317static int flexcop_usb_i2c_request(struct flexcop_i2c_adapter *i2c,
318 flexcop_access_op_t op, u8 chipaddr, u8 addr, u8 *buf, u16 len)
319{
320 if (op == FC_READ)
321 return flexcop_usb_i2c_req(i2c, B2C2_USB_I2C_REQUEST,
322 USB_FUNC_I2C_READ, chipaddr, addr, buf, len);
323 else
324 return flexcop_usb_i2c_req(i2c, B2C2_USB_I2C_REQUEST,
325 USB_FUNC_I2C_WRITE, chipaddr, addr, buf, len);
326}
327
328static void flexcop_usb_process_frame(struct flexcop_usb *fc_usb,
329 u8 *buffer, int buffer_length)
330{
331 u8 *b;
332 int l;
333
334 deb_ts("tmp_buffer_length=%d, buffer_length=%d\n",
335 fc_usb->tmp_buffer_length, buffer_length);
336
337 if (fc_usb->tmp_buffer_length > 0) {
338 memcpy(fc_usb->tmp_buffer+fc_usb->tmp_buffer_length, buffer,
339 buffer_length);
340 fc_usb->tmp_buffer_length += buffer_length;
341 b = fc_usb->tmp_buffer;
342 l = fc_usb->tmp_buffer_length;
343 } else {
344 b=buffer;
345 l=buffer_length;
346 }
347
348 while (l >= 190) {
349 if (*b == 0xff) {
350 switch (*(b+1) & 0x03) {
351 case 0x01:
352 if (*(b+2) == 0x47)
353 flexcop_pass_dmx_packets(
354 fc_usb->fc_dev, b+2, 1);
355 else
356 deb_ts("not ts packet %*ph\n", 4, b+2);
357 b += 190;
358 l -= 190;
359 break;
360 default:
361 deb_ts("wrong packet type\n");
362 l = 0;
363 break;
364 }
365 } else {
366 deb_ts("wrong header\n");
367 l = 0;
368 }
369 }
370
371 if (l>0)
372 memcpy(fc_usb->tmp_buffer, b, l);
373 fc_usb->tmp_buffer_length = l;
374}
375
376static void flexcop_usb_urb_complete(struct urb *urb)
377{
378 struct flexcop_usb *fc_usb = urb->context;
379 int i;
380
381 if (urb->actual_length > 0)
382 deb_ts("urb completed, bufsize: %d actlen; %d\n",
383 urb->transfer_buffer_length, urb->actual_length);
384
385 for (i = 0; i < urb->number_of_packets; i++) {
386 if (urb->iso_frame_desc[i].status < 0) {
387 err("iso frame descriptor %d has an error: %d\n", i,
388 urb->iso_frame_desc[i].status);
389 } else
390 if (urb->iso_frame_desc[i].actual_length > 0) {
391 deb_ts("passed %d bytes to the demux\n",
392 urb->iso_frame_desc[i].actual_length);
393
394 flexcop_usb_process_frame(fc_usb,
395 urb->transfer_buffer +
396 urb->iso_frame_desc[i].offset,
397 urb->iso_frame_desc[i].actual_length);
398 }
399 urb->iso_frame_desc[i].status = 0;
400 urb->iso_frame_desc[i].actual_length = 0;
401 }
402 usb_submit_urb(urb,GFP_ATOMIC);
403}
404
405static int flexcop_usb_stream_control(struct flexcop_device *fc, int onoff)
406{
407
408 return 0;
409}
410
411static void flexcop_usb_transfer_exit(struct flexcop_usb *fc_usb)
412{
413 int i;
414 for (i = 0; i < B2C2_USB_NUM_ISO_URB; i++)
415 if (fc_usb->iso_urb[i] != NULL) {
416 deb_ts("unlinking/killing urb no. %d\n",i);
417 usb_kill_urb(fc_usb->iso_urb[i]);
418 usb_free_urb(fc_usb->iso_urb[i]);
419 }
420
421 if (fc_usb->iso_buffer != NULL)
422 usb_free_coherent(fc_usb->udev,
423 fc_usb->buffer_size, fc_usb->iso_buffer,
424 fc_usb->dma_addr);
425}
426
427static int flexcop_usb_transfer_init(struct flexcop_usb *fc_usb)
428{
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;
434
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);
438
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)
442 return -ENOMEM;
443
444 memset(fc_usb->iso_buffer, 0, bufsize);
445 fc_usb->buffer_size = bufsize;
446
447
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,
450 GFP_ATOMIC);
451 if (fc_usb->iso_urb[i] == NULL) {
452 ret = -ENOMEM;
453 goto urb_error;
454 }
455 }
456
457
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",
462 i, buffer_offset);
463
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;
469 urb->interval = 1;
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;
473
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",
477 i, j, frame_offset);
478 urb->iso_frame_desc[j].offset = frame_offset;
479 urb->iso_frame_desc[j].length = frame_size;
480 frame_offset += frame_size;
481 }
482
483 if ((ret = usb_submit_urb(fc_usb->iso_urb[i],GFP_ATOMIC))) {
484 err("submitting urb %d failed with %d.", i, ret);
485 goto urb_error;
486 }
487 deb_ts("submitted urb no. %d.\n",i);
488 }
489
490
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);
496 return 0;
497
498urb_error:
499 flexcop_usb_transfer_exit(fc_usb);
500 return ret;
501}
502
503static int flexcop_usb_init(struct flexcop_usb *fc_usb)
504{
505
506 usb_set_interface(fc_usb->udev,0,1);
507 switch (fc_usb->udev->speed) {
508 case USB_SPEED_LOW:
509 err("cannot handle USB speed because it is too slow.");
510 return -ENODEV;
511 break;
512 case USB_SPEED_FULL:
513 info("running at FULL speed.");
514 break;
515 case USB_SPEED_HIGH:
516 info("running at HIGH speed.");
517 break;
518 case USB_SPEED_UNKNOWN:
519 default:
520 err("cannot handle USB speed because it is unknown.");
521 return -ENODEV;
522 }
523 usb_set_intfdata(fc_usb->uintf, fc_usb);
524 return 0;
525}
526
527static void flexcop_usb_exit(struct flexcop_usb *fc_usb)
528{
529 usb_set_intfdata(fc_usb->uintf, NULL);
530}
531
532static int flexcop_usb_probe(struct usb_interface *intf,
533 const struct usb_device_id *id)
534{
535 struct usb_device *udev = interface_to_usbdev(intf);
536 struct flexcop_usb *fc_usb = NULL;
537 struct flexcop_device *fc = NULL;
538 int ret;
539
540 if ((fc = flexcop_device_kmalloc(sizeof(struct flexcop_usb))) == NULL) {
541 err("out of memory\n");
542 return -ENOMEM;
543 }
544
545
546 fc_usb = fc->bus_specific;
547 fc_usb->fc_dev = fc;
548 mutex_init(&fc_usb->data_mutex);
549
550 fc->read_ibi_reg = flexcop_usb_read_ibi_reg;
551 fc->write_ibi_reg = flexcop_usb_write_ibi_reg;
552 fc->i2c_request = flexcop_usb_i2c_request;
553 fc->get_mac_addr = flexcop_usb_get_mac_addr;
554
555 fc->stream_control = flexcop_usb_stream_control;
556
557 fc->pid_filtering = 1;
558 fc->bus_type = FC_USB;
559
560 fc->dev = &udev->dev;
561 fc->owner = THIS_MODULE;
562
563
564 fc_usb->udev = udev;
565 fc_usb->uintf = intf;
566 if ((ret = flexcop_usb_init(fc_usb)) != 0)
567 goto err_kfree;
568
569
570 if ((ret = flexcop_device_initialize(fc)) != 0)
571 goto err_usb_exit;
572
573
574 if ((ret = flexcop_usb_transfer_init(fc_usb)) != 0)
575 goto err_fc_exit;
576
577 info("%s successfully initialized and connected.", DRIVER_NAME);
578 return 0;
579
580err_fc_exit:
581 flexcop_device_exit(fc);
582err_usb_exit:
583 flexcop_usb_exit(fc_usb);
584err_kfree:
585 flexcop_device_kfree(fc);
586 return ret;
587}
588
589static void flexcop_usb_disconnect(struct usb_interface *intf)
590{
591 struct flexcop_usb *fc_usb = usb_get_intfdata(intf);
592 flexcop_usb_transfer_exit(fc_usb);
593 flexcop_device_exit(fc_usb->fc_dev);
594 flexcop_usb_exit(fc_usb);
595 flexcop_device_kfree(fc_usb->fc_dev);
596 info("%s successfully deinitialized and disconnected.", DRIVER_NAME);
597}
598
599static struct usb_device_id flexcop_usb_table [] = {
600 { USB_DEVICE(0x0af7, 0x0101) },
601 { }
602};
603MODULE_DEVICE_TABLE (usb, flexcop_usb_table);
604
605
606static struct usb_driver flexcop_usb_driver = {
607 .name = "b2c2_flexcop_usb",
608 .probe = flexcop_usb_probe,
609 .disconnect = flexcop_usb_disconnect,
610 .id_table = flexcop_usb_table,
611};
612
613module_usb_driver(flexcop_usb_driver);
614
615MODULE_AUTHOR(DRIVER_AUTHOR);
616MODULE_DESCRIPTION(DRIVER_NAME);
617MODULE_LICENSE("GPL");
618