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>
}

2012-06-04

DVB USB firmware download problems

DVB USB firmware download problems


suspend / resume

I listed fixing resume from the suspend as a one topic of the DVB USB to fix. There has been very long time nasty suspend / resume bugs which seems to be related for the firmware downloading. Reason has not been clear thus those were never fixed. I found one bug report back from the 2009 which was sent for me, as my af9015 driver crashes during resume. At that time I didn't have much experience how to hunt bug like that, which was out of my chip driver. I made simplest possible DVB USB driver which just requests firmware download - and crashes - just as a proof of concept. Unfortunately I did not get any help and it left unresolved.
Crash I made 2009: DVB USB resume from suspend crash

Fedora 17 installation to the SSD

As I got very many Kernel crashes during the development and every crash means booting which wastes time around 10 minutes per boot. Also Fedora 17 was just released and I wanted to install it, so I decided to buy new SSD and install Fedora 17! Install goes nicely, taking only minute or two, it is unbelievable how fast SSD is. I have to say hate Gnome3 UI, thus I was earlier running it "fallback" mode but now I decided to give opportunity for Cinnamon.

DVB USB devices on Fedora 17

Finally I started to test how DVB devices were working. It came as a small surprise that plugging DVB USB stick in hangs device around 30 seconds until it appeared. Looked the logs and there was udev errors. It was not totally broken as it still started working but takes 30 seconds and it happened for every DVB USB device which uses firmware.


Jun  1 19:24:24 localhost kernel: [   59.820552] dvb-usb: found a 'Afatech AF9015 DVB-T USB2.0 stick' in cold state, will try to load a firmware
Jun  1 19:24:54 localhost udevd[423]: worker [1581] timeout, kill it
Jun  1 19:24:54 localhost kernel: [   89.853168] dvb-usb: downloading firmware from file 'dvb-usb-af9015.fw'

udev and blocking firmware download at module_init()

Clear suspicion was udev as there was nothing changed inside DVB USB which could break it like that. I downgraded udev to Fedora 16 version and it started working. As I was not any familiar with the udev I decided to open ticket for the Fedora Bugzilla targeting it udev. It didn't take many minutes Kay Sievers replied to Bugzilla, explaining it is Kernel driver issue as it blocks module_init() by downloading firmware. The reason it now arises is udev. Its behavior was changed and we wasn't aware that it was needed to change Kernel drivers not to block module_init().
Fedora Bugzilla: DVB USB device firmware requested in module_init()

The fix

I implemented non-blocking module_init() / USB probe() for DVB USB and problem disappeared. It was done by delaying old probe functionality, which downloads firmware too, using Kernel workqueue and returning always success for the probe() / module_init(). Doing it that way is not prettiest option at least for my mind but there is no any better I am aware. Downside here is that we must deregister device in case of something fails during "old probe". I did that de-registering using usb_driver_release_interface().

suspend / resume fixed too?

I was unable to reproduce old, and very well known, suspend / resume bug with my current hardware and software. Anyhow, it is quite likely coming from the same blocking firmware download issue. Resume from the suspend behaves quite similarly as plugging DVB USB stick to the USB port.

There is still another existing suspend bug. If you are streaming DVB device, like watching television, it will never go to the sleep. It just hangs. That is DVB core issue as in my understanding there should be some mechanism to stop stream before going to sleep. I will fix that later this sumer after DVB USB issues are fixed.

2012-05-31

USB streaming buffers


USB streaming buffers

DVB USB allocates rather big chunks of coherent memory buffers for the USB streaming at the start and frees those on disconnect. One buffer is reserved for each used USB URB, which is around 5-7 buffers as there is that many URBs used generally. After the multi-frontend (MFE) implementation own USB streaming buffers were allocated for every frontend. Currently there is three frontends registered for the single adapter in worst case. And what is the problem: only one frontend can be used at the same time and we allocate buffers for all the three!

I resolved problem allocating biggest needed buffer at the start and changed all frontends in given adapter to use that. URB configuration is performed dynamically to fit needed stream. Quite lightweight solution. Maybe there is still room for the improvement as it allocs and frees URBs every time when streaming configuration is changed. Allocating and freeing few URBs is very cheap operation but it could be avoided unless USB endpoint type is changed (bulk or isochronous). And after all use case where frontend is changed all the time is very unusual. User just choose one frontend, which offer needed television standard, and uses that. So it must be fine :)

What is expensive in whole subject is coherent USB transfer buffer and now it is used 2/3 less than earlier. Someone would like to ask why not to alloc and frees all the memory, URBs and buffers, every time when streaming is started and stopped. There is very good reason to avoid continuous allocs and frees as memory goes fragmented and finally allocation of big coherent chunks of buffer memory will fail. That is realized few times in the recent future as some other DVB devices are stopped working. Limited memory is especially problem of embedded devices.  Believe or not, it is very common DVB sticks are used on embedded routers or STBs.

Related links

[PATCH] em28xx: pre-allocate DVB isoc transfer buffers
http://comments.gmane.org/gmane.linux.drivers.video-input-infrastructure/44570

DVB USB: change USB stream settings dynamically
http://comments.gmane.org/gmane.linux.drivers.video-input-infrastructure/48861

2012-05-27

GSoC week 21.


Week 21 - planning

 

First week

First week is almost over. It is almost over since I am still going to do some USB streaming buffer tests tonight :-)

This week was scheduled as a RFC and planning for DVB USB changes. DVB USB means those general routines used by almost every digital television USB stick. I think it could be called as a sub-driver, framework, library or even DVB USB core. I still like to call it just DVB USB or DVB USB framework.

I did early start for the first week and sent two RFCs describing planned changes 1-2 weeks earlier. It was not a big surprise there was no many comments. Due to that I think changes I was introducing are not totally wrong :)

Implementing

As there was no much discussion about introduced changes I started implementing those. I copied current DVB USB implementation as a starting point, leaving old implantation still alive as a parallel. It is almost impossible to make such big changes for the existing one as there is about 30 individual DVB USB interface drivers using DVB USB core routines. It is still unknown how to make switch-over for the new implementation. I see two possibilities; 1) move driver by driver to the new 2) move all changes one by one to old implementation.

During the development I converted and tested all the time three of my drivers against new routines. Those drivers were au6610, ec168 and af9015. af9015 is one of the most complex DVB USB device drivers and full of hacks. Problems with the af9015 has been my main motivator for most of these DVB USB changes I planned.

Some improved numbers


Changes what I did this week removed many hacks from the af9015. Also those reduced binary size of the af9015 very much.

      text   data  bss    dec   hex  filename
old  14878  13232   40  28150  6df6  dvb-usb-af9015.ko
new  14575   3952    8  18535  4867  dvb-usb-af9015.ko

Space saving is coming from the new way of passing data to the DVB USB framework. Earlier all device data was put inside static struct as now it is passed using driver_info field of the struct usb_device_id.

Size of that configuration structure was always constant, no matter if there was one or 12 devices as current limit is. af9015 driver contains 3 of those big configurations structures. There is some drivers having ~30 similar structs - guess how much space is wasted?

Related links

[RFCv1] DVB-USB improvements
http://comments.gmane.org/gmane.linux.drivers.video-input-infrastructure/47994

[RFCv1] DVB-USB improvements [alternative 2]
http://comments.gmane.org/gmane.linux.drivers.video-input-infrastructure/48572

My DVB USB development tree
http://git.linuxtv.org/anttip/media_tree.git/shortlog/refs/heads/dvb-usb

Blog is open!

About the Blog 

 

Google Summer of Code 2012 attendee!

I have been planning own blog for my Kernel development stuff for a while but newer got enough time nor motivation to learn things to start blogging. Things gone ahead after I was chosen for the Google Summer of Code programme as there was some pressure for the blog to report progression of the project.

My GSoC project is under the Linux Foundation umbrella and it is Kernel development for my favourite area - Linux television interface. Traditionally I have been coding only new television chip drivers. Type of those chip are; USB-interface (aka DVB USB bridge), demodulator and RF tuner. There is around 20 chip drivers currently I have done which is huge amount of drivers for the single developer.

Current problems

During the development of those drivers I have gained very good overall understanding of the whole digital television side of the Linux Kernel and also that technology in general. It is not big surprise I have met a quite lot general issues, issues that are outside of individual driver, somewhere in our core routines. Those issues and limitations are raised up during a long time as hardware has gone more complex and old routines are not flexible enough for the new hardware.

Due to those core limitations there is many ugly hacks needed for the drivers in order to operate. Sometimes those hacks are even very wrong but still mainly working with a little bit luck. More often those hacks still means "only" duplicated code, complex code and wasted resources.

Usually I am too busy to hack support for new the hardware and due to that I have no possibility to allocate enough resources for fixing known general issues. Now that GSoC project gives me possibility to fix those!

GSoC schedule

That GSoC project is workload of three months. Initially I split it to the two big parts, DVB USB enhancements and DVB core enhancements. DVB core contains mainly DVB frontend changes.

Planned schedule is here:
week 21: DVB-USB plan driver interface + RFC
week 22: DVB-USB implementation
week 23: DVB-USB implementation
week 24: DVB-USB implementation
week 25: DVB-USB implementation
week 26: DVB-USB convert few existing drivers to use that new interface
week 27: DVB-CORE learn frontend handling
week 28: DVB-CORE plan needed changes + RFC
week 29: DVB-CORE implementation
week 30: DVB-CORE implementation
week 31: DVB-CORE implementation
week 22: DVB-CORE convert few existing drivers to use that new interface
week 33: pencils down, finalize
week 34: pencils down, finalize