firewire: cdev: add more flexible cycle timer ioctl
PATCH 1/1 firewire: cdev: add more flexible cycle timer ioctl
The system time from CLOCK_REALTIME is not monotonic, hence problematic for the main user of the FW_CDEV_IOC_GET_CYCLE_TIMER ioctl. This issue exists in its successor ABI, i.e. raw1394, too.
http://subversion.ffado.org/ticket/242
We now offer an alternative ioctl which lets the caller choose between CLOCK_REALTIME, CLOCK_MONOTONIC, and CLOCK_MONOTONIC_RAW as source of the local time, very similar to the clock_gettime libc function. The format of the local time return value matches that of clock_gettime (seconds and nanoseconds, instead of a single microseconds value from the existing ioctl).
--- drivers/firewire/core-cdev.c | 37 ++++++++++++++++++++++++++++------ include/linux/firewire-cdev.h | 31 +++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 10 deletions(-)
drivers/firewire/core-cdev.c +31 -6
@@ -1030,22 +1030,46 @@ static int ioctl_stop_iso(struct clientreturn fw_iso_context_stop (client->iso_context); } -static int ioctl_get_cycle_timer (struct client *client,void *buffer) +static int ioctl_get_cycle_timer2 (struct client *client,void *buffer) { -struct fw_cdev_get_cycle_timer *request = buffer; +struct fw_cdev_get_cycle_timer2 *request = buffer;struct fw_card *card = client->device->card; -struct timeval tv; +struct timespec ts = {0 ,0 };u32 cycle_time; +int ret =0 ;local_irq_disable (); cycle_time = card->driver->get_cycle_time (card); -do_gettimeofday (&tv); + +switch (request->clk_id) { +case CLOCK_REALTIME :getnstimeofday (&ts);break ; +case CLOCK_MONOTONIC :do_posix_clock_monotonic_gettime (&ts);break ; +case CLOCK_MONOTONIC_RAW :getrawmonotonic (&ts);break ; +default : + ret = -EINVAL ; + }local_irq_enable (); - request->local_time = tv.tv_sec *1000000ULL + tv.tv_usec; - request->cycle_timer = cycle_time; + request->tv_sec = ts.tv_sec; + request->tv_nsec = ts.tv_nsec; + request->cycle_timer = cycle_time; + +return ret; +} + +static int ioctl_get_cycle_timer (struct client *client,void *buffer) +{ +struct fw_cdev_get_cycle_timer *request = buffer; +struct fw_cdev_get_cycle_timer2 ct2; + + ct2.clk_id =CLOCK_REALTIME ; +ioctl_get_cycle_timer2 (client, &ct2); + + request->local_time = ct2.tv_sec *USEC_PER_SEC + + ct2.tv_nsec /NSEC_PER_USEC ; + request->cycle_timer = ct2.cycle_timer;return 0 ; }
@@ -1319,6 +1343,7 @@ static int (* const ioctl_handlers[])(st ioctl_get_speed, ioctl_send_broadcast_request, ioctl_send_stream_packet, + ioctl_get_cycle_timer2, }; static int dispatch_ioctl(struct client *client,
include/linux/firewire-cdev.h +27 -4
@@ -248,6 +248,9 @@ union fw_cdev_event {#define FW_CDEV_IOC_SEND_BROADCAST_REQUEST _IOW('#', 0x12, struct fw_cdev_send_request)#define FW_CDEV_IOC_SEND_STREAM_PACKET _IOW('#', 0x13, struct fw_cdev_send_stream_packet) +/* available since kernel version 2.6.34 */ +#define FW_CDEV_IOC_GET_CYCLE_TIMER2 _IOWR('#', 0x14, struct fw_cdev_get_cycle_timer2) +/* * FW_CDEV_VERSION History * 1 (2.6.22) - initial version
@@ -544,14 +547,15 @@ struct fw_cdev_stop_iso {/** * struct fw_cdev_get_cycle_timer - read cycle timer register * @local_time: system time, in microseconds since the Epoch -* @cycle_timer: isochronous cycle timer, as per OHCI 1.1 clause 5.13 +* @cycle_timer: Cycle Time register contents * * The %FW_CDEV_IOC_GET_CYCLE_TIMER ioctl reads the isochronous cycle timer -* and also the system clock. This allows to express the receive time of an -* isochronous packet as a system time with microsecond accuracy. +* and also the system clock (%CLOCK_REALTIME). This allows to express the +* receive time of an isochronous packet as a system time. * * @cycle_timer consists of 7 bits cycleSeconds, 13 bits cycleCount, and -* 12 bits cycleOffset, in host byte order. +* 12 bits cycleOffset, in host byte order. Cf. the Cycle Time register +* per IEEE 1394 or Isochronous Cycle Timer register per OHCI-1394. */ struct fw_cdev_get_cycle_timer {__u64 local_time;
@@ -559,6 +563,25 @@ struct fw_cdev_get_cycle_timer { };/** +* struct fw_cdev_get_cycle_timer2 - read cycle timer register +* @tv_sec: system time, seconds +* @tv_nsec: system time, sub-seconds part in nanoseconds +* @clk_id: input parameter, clock from which to get the system time +* @cycle_timer: Cycle Time register contents +* +* The %FW_CDEV_IOC_GET_CYCLE_TIMER2 works like +* %FW_CDEV_IOC_GET_CYCLE_TIMER but lets you choose a clock like with POSIX' +* clock_gettime function. Supported @clk_id values are POSIX' %CLOCK_REALTIME +* and %CLOCK_MONOTONIC and Linux' %CLOCK_MONOTONIC_RAW. +*/ +struct fw_cdev_get_cycle_timer2 { +__s64 tv_sec; +__s32 tv_nsec; +__s32 clk_id; +__u32 cycle_timer; +}; + +/** * struct fw_cdev_allocate_iso_resource - (De)allocate a channel or bandwidth * @closure: Passed back to userspace in correponding iso resource events * @channels: Isochronous channels of which one is to be (de)allocated
Signature · 3 lines
Stefan Richter -=====-==-=- --=- =--== http://arcgraph.de/sr/
PATCH 1/1 firewire: cdev: increment ABI version number
so that clients can detect whether the FW_CDEV_IOC_GET_CYCLE_TIMER ioctl is reliable (on all tested controllers, especially the widely used VIA controllers, also NEC controllers, see commits b677532b and 1c1517ef). Also add a comment on the 2.6.32 iso xmit enhancement.
--- include/linux/firewire-cdev.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
include/linux/firewire-cdev.h +6 -1
@@ -256,8 +256,10 @@ union fw_cdev_event {* 1 (2.6.22) - initial version * 2 (2.6.30) - changed &fw_cdev_event_iso_interrupt.header if * &fw_cdev_create_iso_context.header_size is 8 or more +* (2.6.32) - added time stamp to xmit &fw_cdev_event_iso_interrupt +* 3 (2.6.34) - made &fw_cdev_get_cycle_timer reliable */ -#define FW_CDEV_VERSION 2 +#define FW_CDEV_VERSION 3/** * struct fw_cdev_get_info - General purpose information ioctl
@@ -556,6 +558,9 @@ struct fw_cdev_stop_iso {* @cycle_timer consists of 7 bits cycleSeconds, 13 bits cycleCount, and * 12 bits cycleOffset, in host byte order. Cf. the Cycle Time register * per IEEE 1394 or Isochronous Cycle Timer register per OHCI-1394. +* +* In version 1 and 2 of the ABI, this ioctl returned unreliable (non- +* monotonic) @cycle_timer values on certain controllers. */ struct fw_cdev_get_cycle_timer {__u64 local_time;
Signature · 3 lines
Stefan Richter -=====-==-=- --=- =--== http://arcgraph.de/sr/
PATCH 1/1 firewire: cdev: increment ABI version number
so that clients can detect whether the FW_CDEV_IOC_GET_CYCLE_TIMER ioctl is reliable (on all tested controllers, especially the widely used VIA controllers, also NEC controllers, see commits b677532b and 1c1517ef).
Also add a comment on the 2.6.32 iso xmit enhancement and on dual-buffer IR having been disabled in 2.6.33.
---
Update: added the 2.6.33 item
include/linux/firewire-cdev.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
include/linux/firewire-cdev.h +8 -1
@@ -256,8 +256,12 @@ union fw_cdev_event {* 1 (2.6.22) - initial version * 2 (2.6.30) - changed &fw_cdev_event_iso_interrupt.header if * &fw_cdev_create_iso_context.header_size is 8 or more +* (2.6.32) - added time stamp to xmit &fw_cdev_event_iso_interrupt +* (2.6.33) - IR has always packet-per-buffer semantics now, not one of +* dual-buffer or packet-per-buffer depending on hardware +* 3 (2.6.34) - made &fw_cdev_get_cycle_timer reliable */ -#define FW_CDEV_VERSION 2 +#define FW_CDEV_VERSION 3/** * struct fw_cdev_get_info - General purpose information ioctl
@@ -556,6 +560,9 @@ struct fw_cdev_stop_iso {* @cycle_timer consists of 7 bits cycleSeconds, 13 bits cycleCount, and * 12 bits cycleOffset, in host byte order. Cf. the Cycle Time register * per IEEE 1394 or Isochronous Cycle Timer register per OHCI-1394. +* +* In version 1 and 2 of the ABI, this ioctl returned unreliable (non- +* monotonic) @cycle_timer values on certain controllers. */ struct fw_cdev_get_cycle_timer {__u64 local_time;
Signature · 3 lines
Stefan Richter -=====-==-=- --=- =-=-= http://arcgraph.de/sr/
PATCH 1/1 firewire: cdev: add more flexible cycle timer ioctl
In reply to Stefan RichterThe system time from CLOCK_REALTIME is not monotonic, hence problematic for the main user of the FW_CDEV_IOC_GET_CYCLE_TIMER ioctl. This issue exists in its successor ABI, i.e. raw1394, too.
http://subversion.ffado.org/ticket/242
We now offer an alternative ioctl which lets the caller choose between CLOCK_REALTIME, CLOCK_MONOTONIC, and CLOCK_MONOTONIC_RAW as source of the local time, very similar to the clock_gettime libc function. The format of the local time return value matches that of clock_gettime (seconds and nanoseconds, instead of a single microseconds value from the existing ioctl).
---
Update: Added the core-cdev.c::dispatch_ioctl()::sizeof(union...) hunk.
drivers/firewire/core-cdev.c | 38 +++++++++++++++++++++++++++++----- include/linux/firewire-cdev.h | 31 ++++++++++++++++++++++++---- 2 files changed, 59 insertions(+), 10 deletions(-)
drivers/firewire/core-cdev.c +32 -6
@@ -1030,22 +1030,46 @@ static int ioctl_stop_iso(struct clientreturn fw_iso_context_stop (client->iso_context); } -static int ioctl_get_cycle_timer (struct client *client,void *buffer) +static int ioctl_get_cycle_timer2 (struct client *client,void *buffer) { -struct fw_cdev_get_cycle_timer *request = buffer; +struct fw_cdev_get_cycle_timer2 *request = buffer;struct fw_card *card = client->device->card; -struct timeval tv; +struct timespec ts = {0 ,0 };u32 cycle_time; +int ret =0 ;local_irq_disable (); cycle_time = card->driver->get_cycle_time (card); -do_gettimeofday (&tv); + +switch (request->clk_id) { +case CLOCK_REALTIME :getnstimeofday (&ts);break ; +case CLOCK_MONOTONIC :do_posix_clock_monotonic_gettime (&ts);break ; +case CLOCK_MONOTONIC_RAW :getrawmonotonic (&ts);break ; +default : + ret = -EINVAL ; + }local_irq_enable (); - request->local_time = tv.tv_sec *1000000ULL + tv.tv_usec; - request->cycle_timer = cycle_time; + request->tv_sec = ts.tv_sec; + request->tv_nsec = ts.tv_nsec; + request->cycle_timer = cycle_time; + +return ret; +} + +static int ioctl_get_cycle_timer (struct client *client,void *buffer) +{ +struct fw_cdev_get_cycle_timer *request = buffer; +struct fw_cdev_get_cycle_timer2 ct2; + + ct2.clk_id =CLOCK_REALTIME ; +ioctl_get_cycle_timer2 (client, &ct2); + + request->local_time = ct2.tv_sec *USEC_PER_SEC + + ct2.tv_nsec /NSEC_PER_USEC ; + request->cycle_timer = ct2.cycle_timer;return 0 ; }
@@ -1319,6 +1343,7 @@ static int (* const ioctl_handlers[])(st ioctl_get_speed, ioctl_send_broadcast_request, ioctl_send_stream_packet, + ioctl_get_cycle_timer2, }; static int dispatch_ioctl(struct client *client,
@@ -1340,6 +1365,7 @@ static int dispatch_ioctl(struct clientstruct fw_cdev_get_cycle_timer _0c;struct fw_cdev_allocate_iso_resource _0d;struct fw_cdev_send_stream_packet _13; +struct fw_cdev_get_cycle_timer2 _14; })];int ret;
include/linux/firewire-cdev.h +27 -4
@@ -248,6 +248,9 @@ union fw_cdev_event {#define FW_CDEV_IOC_SEND_BROADCAST_REQUEST _IOW('#', 0x12, struct fw_cdev_send_request)#define FW_CDEV_IOC_SEND_STREAM_PACKET _IOW('#', 0x13, struct fw_cdev_send_stream_packet) +/* available since kernel version 2.6.34 */ +#define FW_CDEV_IOC_GET_CYCLE_TIMER2 _IOWR('#', 0x14, struct fw_cdev_get_cycle_timer2) +/* * FW_CDEV_VERSION History * 1 (2.6.22) - initial version
@@ -544,14 +547,15 @@ struct fw_cdev_stop_iso {/** * struct fw_cdev_get_cycle_timer - read cycle timer register * @local_time: system time, in microseconds since the Epoch -* @cycle_timer: isochronous cycle timer, as per OHCI 1.1 clause 5.13 +* @cycle_timer: Cycle Time register contents * * The %FW_CDEV_IOC_GET_CYCLE_TIMER ioctl reads the isochronous cycle timer -* and also the system clock. This allows to express the receive time of an -* isochronous packet as a system time with microsecond accuracy. +* and also the system clock (%CLOCK_REALTIME). This allows to express the +* receive time of an isochronous packet as a system time. * * @cycle_timer consists of 7 bits cycleSeconds, 13 bits cycleCount, and -* 12 bits cycleOffset, in host byte order. +* 12 bits cycleOffset, in host byte order. Cf. the Cycle Time register +* per IEEE 1394 or Isochronous Cycle Timer register per OHCI-1394. */ struct fw_cdev_get_cycle_timer {__u64 local_time;
@@ -559,6 +563,25 @@ struct fw_cdev_get_cycle_timer { };/** +* struct fw_cdev_get_cycle_timer2 - read cycle timer register +* @tv_sec: system time, seconds +* @tv_nsec: system time, sub-seconds part in nanoseconds +* @clk_id: input parameter, clock from which to get the system time +* @cycle_timer: Cycle Time register contents +* +* The %FW_CDEV_IOC_GET_CYCLE_TIMER2 works like +* %FW_CDEV_IOC_GET_CYCLE_TIMER but lets you choose a clock like with POSIX' +* clock_gettime function. Supported @clk_id values are POSIX' %CLOCK_REALTIME +* and %CLOCK_MONOTONIC and Linux' %CLOCK_MONOTONIC_RAW. +*/ +struct fw_cdev_get_cycle_timer2 { +__s64 tv_sec; +__s32 tv_nsec; +__s32 clk_id; +__u32 cycle_timer; +}; + +/** * struct fw_cdev_allocate_iso_resource - (De)allocate a channel or bandwidth * @closure: Passed back to userspace in correponding iso resource events * @channels: Isochronous channels of which one is to be (de)allocated
Signature · 3 lines
Stefan Richter -=====-==-=- --=- =-=-- http://arcgraph.de/sr/