@@ -87,6 +87,7 @@ void iio_chrdev_buffer_release(struct iio_dev *indio_dev)
8787
8888void iio_buffer_init (struct iio_buffer * buffer , struct iio_dev * indio_dev )
8989{
90+ INIT_LIST_HEAD (& buffer -> demux_list );
9091 buffer -> indio_dev = indio_dev ;
9192 init_waitqueue_head (& buffer -> pollq );
9293}
@@ -128,10 +129,9 @@ static ssize_t iio_scan_el_show(struct device *dev,
128129 int ret ;
129130 struct iio_dev * indio_dev = dev_get_drvdata (dev );
130131
131- ret = iio_scan_mask_query (indio_dev -> buffer ,
132- to_iio_dev_attr (attr )-> address );
133- if (ret < 0 )
134- return ret ;
132+ ret = test_bit (to_iio_dev_attr (attr )-> address ,
133+ indio_dev -> buffer -> scan_mask );
134+
135135 return sprintf (buf , "%d\n" , ret );
136136}
137137
@@ -579,6 +579,12 @@ int iio_sw_buffer_preenable(struct iio_dev *indio_dev)
579579 buffer -> scan_mask );
580580 else
581581 indio_dev -> active_scan_mask = buffer -> scan_mask ;
582+ iio_update_demux (indio_dev );
583+
584+ if (indio_dev -> info -> update_scan_mode )
585+ return indio_dev -> info
586+ -> update_scan_mode (indio_dev ,
587+ indio_dev -> active_scan_mask );
582588 return 0 ;
583589}
584590EXPORT_SYMBOL (iio_sw_buffer_preenable );
@@ -648,3 +654,135 @@ int iio_scan_mask_query(struct iio_buffer *buffer, int bit)
648654 return test_bit (bit , mask );
649655};
650656EXPORT_SYMBOL_GPL (iio_scan_mask_query );
657+
658+ /**
659+ * struct iio_demux_table() - table describing demux memcpy ops
660+ * @from: index to copy from
661+ * @to: index to copy to
662+ * @length: how many bytes to copy
663+ * @l: list head used for management
664+ */
665+ struct iio_demux_table {
666+ unsigned from ;
667+ unsigned to ;
668+ unsigned length ;
669+ struct list_head l ;
670+ };
671+
672+ static unsigned char * iio_demux (struct iio_buffer * buffer ,
673+ unsigned char * datain )
674+ {
675+ struct iio_demux_table * t ;
676+
677+ if (list_empty (& buffer -> demux_list ))
678+ return datain ;
679+ list_for_each_entry (t , & buffer -> demux_list , l )
680+ memcpy (buffer -> demux_bounce + t -> to ,
681+ datain + t -> from , t -> length );
682+
683+ return buffer -> demux_bounce ;
684+ }
685+
686+ int iio_push_to_buffer (struct iio_buffer * buffer , unsigned char * data ,
687+ s64 timestamp )
688+ {
689+ unsigned char * dataout = iio_demux (buffer , data );
690+
691+ return buffer -> access -> store_to (buffer , dataout , timestamp );
692+ }
693+ EXPORT_SYMBOL_GPL (iio_push_to_buffer );
694+
695+ int iio_update_demux (struct iio_dev * indio_dev )
696+ {
697+ const struct iio_chan_spec * ch ;
698+ struct iio_buffer * buffer = indio_dev -> buffer ;
699+ int ret , in_ind = -1 , out_ind , length ;
700+ unsigned in_loc = 0 , out_loc = 0 ;
701+ struct iio_demux_table * p , * q ;
702+
703+ /* Clear out any old demux */
704+ list_for_each_entry_safe (p , q , & buffer -> demux_list , l ) {
705+ list_del (& p -> l );
706+ kfree (p );
707+ }
708+ kfree (buffer -> demux_bounce );
709+ buffer -> demux_bounce = NULL ;
710+
711+ /* First work out which scan mode we will actually have */
712+ if (bitmap_equal (indio_dev -> active_scan_mask ,
713+ buffer -> scan_mask ,
714+ indio_dev -> masklength ))
715+ return 0 ;
716+
717+ /* Now we have the two masks, work from least sig and build up sizes */
718+ for_each_set_bit (out_ind ,
719+ indio_dev -> active_scan_mask ,
720+ indio_dev -> masklength ) {
721+ in_ind = find_next_bit (indio_dev -> active_scan_mask ,
722+ indio_dev -> masklength ,
723+ in_ind + 1 );
724+ while (in_ind != out_ind ) {
725+ in_ind = find_next_bit (indio_dev -> active_scan_mask ,
726+ indio_dev -> masklength ,
727+ in_ind + 1 );
728+ ch = iio_find_channel_from_si (indio_dev , in_ind );
729+ length = ch -> scan_type .storagebits /8 ;
730+ /* Make sure we are aligned */
731+ in_loc += length ;
732+ if (in_loc % length )
733+ in_loc += length - in_loc % length ;
734+ }
735+ p = kmalloc (sizeof (* p ), GFP_KERNEL );
736+ if (p == NULL ) {
737+ ret = - ENOMEM ;
738+ goto error_clear_mux_table ;
739+ }
740+ ch = iio_find_channel_from_si (indio_dev , in_ind );
741+ length = ch -> scan_type .storagebits /8 ;
742+ if (out_loc % length )
743+ out_loc += length - out_loc % length ;
744+ if (in_loc % length )
745+ in_loc += length - in_loc % length ;
746+ p -> from = in_loc ;
747+ p -> to = out_loc ;
748+ p -> length = length ;
749+ list_add_tail (& p -> l , & buffer -> demux_list );
750+ out_loc += length ;
751+ in_loc += length ;
752+ }
753+ /* Relies on scan_timestamp being last */
754+ if (buffer -> scan_timestamp ) {
755+ p = kmalloc (sizeof (* p ), GFP_KERNEL );
756+ if (p == NULL ) {
757+ ret = - ENOMEM ;
758+ goto error_clear_mux_table ;
759+ }
760+ ch = iio_find_channel_from_si (indio_dev ,
761+ buffer -> scan_index_timestamp );
762+ length = ch -> scan_type .storagebits /8 ;
763+ if (out_loc % length )
764+ out_loc += length - out_loc % length ;
765+ if (in_loc % length )
766+ in_loc += length - in_loc % length ;
767+ p -> from = in_loc ;
768+ p -> to = out_loc ;
769+ p -> length = length ;
770+ list_add_tail (& p -> l , & buffer -> demux_list );
771+ out_loc += length ;
772+ in_loc += length ;
773+ }
774+ buffer -> demux_bounce = kzalloc (out_loc , GFP_KERNEL );
775+ if (buffer -> demux_bounce == NULL ) {
776+ ret = - ENOMEM ;
777+ goto error_clear_mux_table ;
778+ }
779+ return 0 ;
780+
781+ error_clear_mux_table :
782+ list_for_each_entry_safe (p , q , & buffer -> demux_list , l ) {
783+ list_del (& p -> l );
784+ kfree (p );
785+ }
786+ return ret ;
787+ }
788+ EXPORT_SYMBOL_GPL (iio_update_demux );
0 commit comments