2012-07-31

DVB power-management on suspend

Back to the power-management again

Last week I finished with DVB power-management, the issue I touched on earlier too when implementing general suspend / resume support for DVB USB. For now I looked how to implement real hardware power saving functionalities when device is suspended. Initial implementation was lacking all the power management, allowing only computer to enter suspend when television was still streaming. All the chips were still left full power state and thus continued draining full current which is simply against Kernel power-management rules.


In order to fix that problem nicely and general level enough I was forced to study issue from multiple perspectives. It is clear that DVB devices are much more complex than usual Kernel drivers. That is coming from the fact there is at least three sub-drivers (USB/PCI-interface, demodulator, RF-tuner) and each of those works quite independent. Fortunately there is existing power-management callbacks (named init and sleep) which are even often implemented. You can consider those equivalent to USB -framework PM callbacks that are named suspend, resume (and reset_resume). Another lucky point here is that DVB-frontend has cache structure, which basically stores all the transmissions parameters used currently. So as we has existing init/sleep callbacks for power-management and all transmission parameters stored, it should be relatively easy to recover device at the state where it was.
  • on suspend
    • stop remote controller polling (interface driver)
    • stop ongoing stream (interface driver)
    • sleep demodulator (demodulator driver)
    • sleep tuner (RF-tuner driver)
    • sleep interface (interface driver)
    • return with success to inform Kernel PM device is suspended
  • on resume
    • wake-up interface (interface driver)
    • wake-up tuner (RF-tuner driver)
    • wake-up demodulator (demodulator driver)
    • acquire DVB-frontend to retune (DVB-core driver)
    • resume ongoing stream (interface driver)
    • resume remote controller polling (interface driver)
    • return with success to inform Kernel PM device is resumed

Simple? Actually it is, due to no need to touch existing demodulator and tuner drivers. And seems to work nicely. What I tested with few USB devices using VLC, all those continued to show same TV-channel just after resumed!

I still have some thoughts to make it more general by moving that functionality partly to the DVB-frontend. Currently process is lead by DVB USB core parts and DVB-frontend is only asked to perform retune. It could be wise to add suspend and resume functions to the DVB-frontend and let the DVB-frontend then call demodulator and tuner driver init & sleep respectively.

sub-driver level power-management issues

During the development I made simple USB power meter in order to see some real life statistics about device current usage. Quick testing with few USB devices reveals we have quite much room for improvements in that area. I will go back to that topic soon when new multimeter I ordered arrives.

device draining around 280mA when in use


2012-07-20

learning learning and GPIOs...

Partytime!

I kept extra long weekend holiday, wow! Reason for that was Ilosaarirock festival I attended with friends. Heh, we were there from Friday to Sunday night and back to home late Monday... not surprise I was a little bit tired. There was very good reason to party as my Google Summer of Code project (that Kernel Media project for Linux Foundation) Midterm Evaluations successfully were passed!

GPIOs

GPIO, shorthand for General Purpose Input/Output

implement functionality of LNA API call in real life

I was forced to find out "proper" solution to control LNA (inside Kernel driver) after I added DVB API support for it. Very often those LNAs are controlled by GPIO line or two. Almost every chip has few GPIOs, for DVB -device this means there could be GPIOs on USB/PCI-bridge, demodulator and tuner. LNA commonly is very simple to control, ON or OFF, which means there is no own driver for LNA chip. Controlling this kind of general device specific properties belongs to the bridge driver.

LNA control path

I should write short article about anatomy of DVB-device but as there is no such article yet, lets explains some wiring as background. I used PCTV nanoStick T2 290e during development, but for most others it is just similar. That device LNA is controlled by demodulator GPIO. Here is the control path, from computer to LNA:

PC|bus=USB|chip=bridge|bus=I2C|chip=demodulator|bus=GPIO|chip=LNA

LNA controlling problem

The problem. Bridge wants to set LNA, but LNA is controlled by the demodulator. How to deliver that request to the demodulator driver? Simplest solution is to export function from demodulator driver to perform request. Function like set_lna() or set_gpio() and call that from the bridge driver. Yes, works, used widely, easy to implement, but so home-brew.

Kernel gpiolib framework

After digging Kernel code and documentation I found there is existing GPIO support. I decided to  try that. At the first glance it looked like only for the motherboard GPIO handling and my need is inside device driver. Looking code more I found it was extended to handle GPIO expanders using gpiolib.  This kind of DVB -device could be seen actually as a USB-I2C-GPIO-expander. For the bonus point we get userspace sysfs interface for free! It is very nice to have as it provides easy access for GPIOs during development.

gpiolib sysfs example

For example this is how I can enable (echo 0) and disable (echo 1) LNA in question:
echo 253 > /sys/class/gpio/export
echo 0 > /sys/class/gpio/gpio253/value
echo 1 > /sys/class/gpio/gpio253/value
echo 253 > /sys/class/gpio/unexport


Final notes

I encourage all the other DVB driver developers use same solution for demodulator and tuner GPIOs. It is now learned and found to working at least some level. Implementation seems to be quite simple and interface is standard provided by Kernel gpiolib framework. Look example from the cxd2820r demodulator driver.

2012-07-12

[PATCH RFC] add LNA support for DVB API

LNA (Low-noise amplifier)

LNA is small antenna signal amplifier inside DVB device. Usually it is external component located between antenna connector and RF-tuner. There is usually amplifiers between whole signal path but LNAs and those which are inside of device. Normal LNA we have in digital television device is simple on/off. When it is off it bypass signal, causing still some loss, and when it is on it amplifies signal using some static factor. That factor is called gain and unit used usually is Decibel (dB).
There is also LNAs offering more than one gain level. For example external DVB-T/C LNA NXP BGU7033. It does have 3 modes, 10 dB, 5 dB and bypass -2 dB.

LNAs having even more adjustable gain levels are called variable gain amplifier (VGA). Also there is LNAs/VGAs integrated to the RF-tuner... But I am not going to go deeper these two cases as main need for that LNA API addition is simple external LNAs between RF-tuner and antenna connector.


comments for DVB LNA API


[PATCH RFC] add LNA support for DVB API


2012-07-11

DTMB support for DVB API

V4L2 SDR API

It has been rather slow progress week. Initially I started studying changes needed to support SDR (Software Defined Radio) via Kernel API. For me it sounds quite easy and logical to extend DVB API for SDR. Just adding new delivery system and implement other relative small changes for DVB-core. After some discussion on Linux-Media mailing list, idea of extending DVB API was refused as V4L2 API is more correct API for that kind of stuff. V4L2 API covers FM-radio, camera, web-camera and analogue television stuff whilst DVB API is used only for digital television. Unfortunately I have quite zero experience from V4L2 devices. After hacking with Silicon Laboratories Si472x based USB FM-radio I decided to leave whole SDR for a while. Have to get more suitable radio for V4L2 learning...

DTMB support for DVB API

After adversity with the SDR I decided to do easier DVB enhancements I planned. DTMB (Digital Terrestrial Multimedia Broadcast) is Chinese digital television standard and there is some existing Linux Kernel drivers for that standard. One of those drivers, HDIC HD29L2, was made by me in last autumn and initial interest is coming from there. Big work for whole DTMB API support was done during last Christmas holiday. I even ran some initial RFCs but never finalized work. Learning many research papers and listing all the used parameters, finding out equivalent ones from the existing API, was quite many working days in that time!


1. [PATCH RFCv3] add DTMB support for DVB API
2. [PATCH RFCv3] add DTMB support for DVB API

2012-07-04

PULL-requests for Kernel 3.6

[GIT PULL FOR v3.6] DVB USB v2

I pull requested all the DVB USB changes I did. Quite big patch bomb, 103 change-sets! Those are requested for next possible Kernel 3.6, but it could be delayed to 3.7 if some issues are raised. No comments so far...

[GIT PULL FOR v3.6] DVB USB v2


[GIT PULL FOR v3.6] rtl2832 and misc changes

2nd set I pull requested for the same Kernel was some maintenance stuff, bug fixes and minor enhancements to my existing drivers. I put also a long-awaited RTL2832 DVB-T demodulator driver by Thomas Mair to that set too as I did much work for reviewing it and it does have close relation to my dvb_usb_rtl28xxu driver. Hopefully it gets merged at that time...

[GIT PULL FOR v3.6] rtl2832 and misc changes


2012-06-29

DVB core enhancements - comments please?

As you could remember I initially scheduled my GSoC project to two phases. First one was DVB USB enhancements and second part was for DVB core issues. DVB USB changes are quite ready and I am going to sent upstream merge request in next few days. I will write article about that later in next few days.

I did some planning for the DVB core part recently. I ended-up listing known bugs, insufficient functionality and some new features and sent plan to the Linux-Media mailing list for the comments. No comments so far for that mail - but many of those are discussed earlier too.

DVB core enhancements - comments please?


DVB core enhancements - comments please?

Here is my list of needed DVB core related changes. Feel free to comment - what are not needed or what you would like to see instead. I will try to implement what I can (and what I like most interesting .

general validly checking for demodulator callback input values

  • currently each driver needs to validate those
  • values are highly hooked up to used television standard
  • we can do almost all validly checking inside core
  • we can also check if call is possible to perform in given condition
  • for example BER is not valid when demod is unlocked

suspend / resume support

  • support is currently quite missing, all what is done is on interface drivers
  • needs power management
  • streaming makes it hard
  • quite a lot work to get it working in case of streaming is ongoing

use Kernel power management instead of own

  • there seems to be Kernel services for power-management
  • study if it is wise to use Kernel services instead of own
  • own PM is still working very well, at least I dont know any problems

SDR - Softaware Defined Radio support DVB API

  • http://comments.gmane.org/gmane.linux.drivers.video-input-infrastructure/44461
  • there is existing devices that are SDR (RTL2832U "rtl-sdr")
  • SDR is quite near what is digital TV streaming
  • study what is needed
  • new delivery system for frontend API called SDR?
  • some core changes needed, like status (is locked etc)
  • how about demuxer?
  • stream conversion, inside Kernel?
  • what are new parameters needed for DVB API?

DTMB standard support for DVB API

  • it is Chinese DTV standard
  • I already ran RFC but have been too busy for implementing it :]

LNA (low-noise amplifier) support for DVB API

  • there is quite a lot of devices having LNA
  • currently not supported => LNA is configured off typically

offer polling method for statistics

  • many static counters could not be read as a "one go"
  • typical cycle is : start measurement => wait => read counters
  • some drivers starts own internal work-queue for polling (complexity)
  • some drivers blocks IOCTL when taking measurement (bad)

fix frontend properties

  • those has been broken since MFE => SFE change
  • currently implemented as a properties per driver
  • need to be properties per delivery system
  • are broken because driver/chip could support multiple DTV standard

2012-06-08

AF9015 driver converted to new DVB USB

AF9015 driver converted!

I finally decided to publish one rather complex DVB USB driver as a demonstration what it looks like. I selected Afatech AF9015 driver as it is one of the most complex and main motivator of whole DVB USB changes. I have also Anysee, AU6610 and EC168 drivers converted, but those are far away complexity compared for AF9015, since not so interesting.

Code size reduced a lot

All-in-all, code size goes down rather much. It was 2084 LOC and now it is 1609 LOC - it is near 500 LOC less. Basically it is all coming from the removed hacks. Now all needed functionality is generalized to the common DVB USB and ugly hacks are removed from the individual driver.

PATCH: af9015: switch to new DVB-USB

Near future plans

I am quite happy for features common DVB USB now supports. I think all but dynamic USB ID are implemented somehow for those I initially planned. There is still much work to do when implementing all correctly and test error paths. There is still at one big technical problem to solve. It is that delayed init which was added to fix firmware loading. I can get it crashing rather easily just repeatedly loading and unloading DVB USB driver... I suspect it is coming from the fact both workqueue running delayed device intialization and USB-core end up same routines. For example workqueue is downloading firmware and at the same time module is unloaded by user. Likely some locking is needed to prevent module unload in that case.

2012-06-05

Dynamic USB device ID

Dynamic USB device ID


Reference designs

Nowadays many DVB UDB devices are actually chip vendor reference designs which means those are very similar. Used chips are same and wired together similarly, meaning only new device id is needed for the driver in order to get it working.

It takes too long

There is always significant delay from the device release to the point support is arrived for the distribution. That delay is coming from the different schedules. First it takes time until driver author gets info about new device is needed to add driver. He has to find out hardware or at least someone who can test patch. After that there is Kernel schedule. Kernel has own merge window and release candidate cycle that takes many months. In my understanding simple USB IDs are allowed to add driver during release candidate phase, but usually it still goes through merge window which adds significant delay. And finally there distribution schedule. It is usually needed to upgrade used distribution as they do not upgrade new Kernel version during release cycle. All-in-all, it could take year or so.


Promote the device ID to driver


There is feature called dynamic USB device ID to tackle that delay. User can tell for the Kernel that he wants try if given device driver could drive his new device. For example we could say load dvb_usb_af9015 driver for the device having USB ID 15a4:9016:
echo 15a4 9016 > /sys/bus/usb/drivers/dvb_usb_af9015/new_id


DVB USB and dynamic IDs

Currently DVB USB doesn't support dynamic IDs. Today I did some work in order to add support for it. Unfortunately I did not find out as nice solution as I was hoping. As for now I use .driver_info field from the struct usb_device_id to pass all needed to data to the DVB USB. In practice .driver_info field carries pointer to the struct dvb_usb_device_properties. In normal case all needed data is inside MODULE_DEVICE_TABLE() but in case of dynamic ID there is no entry inside MODULE_DEVICE_TABLE() and thus no needed pointer inside .driver_info.

Solution is to implement own .probe() and set .driver_info for dynamic ID. And finally pass that all to the DVB USB. Not very ugly, nor nice.

What I would like to see is dynamic ID entry which could be added to the driver MODULE_DEVICE_TABLE() similarly as others. Maybe there is even some reserved USB vendor IDs for special purposes that could be used.

struct usb_device_id {
    .idVendor = VENDOR_ID_DYNAMIC
    .idProduct = PRODUCT_ID_DYNAMIC
    .driver_info = <own data>
}