V4L/DVB (4518): Allow manually selecting the proper helper drivers
[linux-2.6] / drivers / media / video / pvrusb2 / pvrusb2-sysfs.c
1 /*
2  *
3  *  $Id$
4  *
5  *  Copyright (C) 2005 Mike Isely <isely@pobox.com>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include <linux/string.h>
23 #include <linux/slab.h>
24 #include <asm/semaphore.h>
25 #include "pvrusb2-sysfs.h"
26 #include "pvrusb2-hdw.h"
27 #include "pvrusb2-debug.h"
28 #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
29 #include "pvrusb2-debugifc.h"
30 #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
31
32 #define pvr2_sysfs_trace(...) pvr2_trace(PVR2_TRACE_SYSFS,__VA_ARGS__)
33
34 struct pvr2_sysfs {
35         struct pvr2_channel channel;
36         struct class_device *class_dev;
37 #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
38         struct pvr2_sysfs_debugifc *debugifc;
39 #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
40         struct pvr2_sysfs_ctl_item *item_first;
41         struct pvr2_sysfs_ctl_item *item_last;
42         struct sysfs_ops kops;
43         struct kobj_type ktype;
44         struct class_device_attribute attr_v4l_minor_number;
45         struct class_device_attribute attr_unit_number;
46         int v4l_minor_number_created_ok;
47         int unit_number_created_ok;
48 };
49
50 #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
51 struct pvr2_sysfs_debugifc {
52         struct class_device_attribute attr_debugcmd;
53         struct class_device_attribute attr_debuginfo;
54         int debugcmd_created_ok;
55         int debuginfo_created_ok;
56 };
57 #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
58
59 struct pvr2_sysfs_ctl_item {
60         struct class_device_attribute attr_name;
61         struct class_device_attribute attr_type;
62         struct class_device_attribute attr_min;
63         struct class_device_attribute attr_max;
64         struct class_device_attribute attr_enum;
65         struct class_device_attribute attr_bits;
66         struct class_device_attribute attr_val;
67         struct class_device_attribute attr_custom;
68         struct pvr2_ctrl *cptr;
69         struct pvr2_sysfs *chptr;
70         struct pvr2_sysfs_ctl_item *item_next;
71         struct attribute *attr_gen[7];
72         struct attribute_group grp;
73         int created_ok;
74         char name[80];
75 };
76
77 struct pvr2_sysfs_class {
78         struct class class;
79 };
80
81 static ssize_t show_name(int id,struct class_device *class_dev,char *buf)
82 {
83         struct pvr2_ctrl *cptr;
84         struct pvr2_sysfs *sfp;
85         const char *name;
86
87         sfp = (struct pvr2_sysfs *)class_dev->class_data;
88         if (!sfp) return -EINVAL;
89         cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
90         if (!cptr) return -EINVAL;
91
92         name = pvr2_ctrl_get_desc(cptr);
93         pvr2_sysfs_trace("pvr2_sysfs(%p) show_name(cid=%d) is %s",sfp,id,name);
94
95         if (!name) return -EINVAL;
96
97         return scnprintf(buf,PAGE_SIZE,"%s\n",name);
98 }
99
100 static ssize_t show_type(int id,struct class_device *class_dev,char *buf)
101 {
102         struct pvr2_ctrl *cptr;
103         struct pvr2_sysfs *sfp;
104         const char *name;
105         enum pvr2_ctl_type tp;
106
107         sfp = (struct pvr2_sysfs *)class_dev->class_data;
108         if (!sfp) return -EINVAL;
109         cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
110         if (!cptr) return -EINVAL;
111
112         tp = pvr2_ctrl_get_type(cptr);
113         switch (tp) {
114         case pvr2_ctl_int: name = "integer"; break;
115         case pvr2_ctl_enum: name = "enum"; break;
116         case pvr2_ctl_bitmask: name = "bitmask"; break;
117         case pvr2_ctl_bool: name = "boolean"; break;
118         default: name = "?"; break;
119         }
120         pvr2_sysfs_trace("pvr2_sysfs(%p) show_type(cid=%d) is %s",sfp,id,name);
121
122         if (!name) return -EINVAL;
123
124         return scnprintf(buf,PAGE_SIZE,"%s\n",name);
125 }
126
127 static ssize_t show_min(int id,struct class_device *class_dev,char *buf)
128 {
129         struct pvr2_ctrl *cptr;
130         struct pvr2_sysfs *sfp;
131         long val;
132
133         sfp = (struct pvr2_sysfs *)class_dev->class_data;
134         if (!sfp) return -EINVAL;
135         cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
136         if (!cptr) return -EINVAL;
137         val = pvr2_ctrl_get_min(cptr);
138
139         pvr2_sysfs_trace("pvr2_sysfs(%p) show_min(cid=%d) is %ld",sfp,id,val);
140
141         return scnprintf(buf,PAGE_SIZE,"%ld\n",val);
142 }
143
144 static ssize_t show_max(int id,struct class_device *class_dev,char *buf)
145 {
146         struct pvr2_ctrl *cptr;
147         struct pvr2_sysfs *sfp;
148         long val;
149
150         sfp = (struct pvr2_sysfs *)class_dev->class_data;
151         if (!sfp) return -EINVAL;
152         cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
153         if (!cptr) return -EINVAL;
154         val = pvr2_ctrl_get_max(cptr);
155
156         pvr2_sysfs_trace("pvr2_sysfs(%p) show_max(cid=%d) is %ld",sfp,id,val);
157
158         return scnprintf(buf,PAGE_SIZE,"%ld\n",val);
159 }
160
161 static ssize_t show_val_norm(int id,struct class_device *class_dev,char *buf)
162 {
163         struct pvr2_ctrl *cptr;
164         struct pvr2_sysfs *sfp;
165         int val,ret;
166         unsigned int cnt = 0;
167
168         sfp = (struct pvr2_sysfs *)class_dev->class_data;
169         if (!sfp) return -EINVAL;
170         cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
171         if (!cptr) return -EINVAL;
172
173         ret = pvr2_ctrl_get_value(cptr,&val);
174         if (ret < 0) return ret;
175
176         ret = pvr2_ctrl_value_to_sym(cptr,~0,val,
177                                      buf,PAGE_SIZE-1,&cnt);
178
179         pvr2_sysfs_trace("pvr2_sysfs(%p) show_val_norm(cid=%d) is %.*s (%d)",
180                          sfp,id,cnt,buf,val);
181         buf[cnt] = '\n';
182         return cnt+1;
183 }
184
185 static ssize_t show_val_custom(int id,struct class_device *class_dev,char *buf)
186 {
187         struct pvr2_ctrl *cptr;
188         struct pvr2_sysfs *sfp;
189         int val,ret;
190         unsigned int cnt = 0;
191
192         sfp = (struct pvr2_sysfs *)class_dev->class_data;
193         if (!sfp) return -EINVAL;
194         cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
195         if (!cptr) return -EINVAL;
196
197         ret = pvr2_ctrl_get_value(cptr,&val);
198         if (ret < 0) return ret;
199
200         ret = pvr2_ctrl_custom_value_to_sym(cptr,~0,val,
201                                             buf,PAGE_SIZE-1,&cnt);
202
203         pvr2_sysfs_trace("pvr2_sysfs(%p) show_val_custom(cid=%d) is %.*s (%d)",
204                          sfp,id,cnt,buf,val);
205         buf[cnt] = '\n';
206         return cnt+1;
207 }
208
209 static ssize_t show_enum(int id,struct class_device *class_dev,char *buf)
210 {
211         struct pvr2_ctrl *cptr;
212         struct pvr2_sysfs *sfp;
213         long val;
214         unsigned int bcnt,ccnt,ecnt;
215
216         sfp = (struct pvr2_sysfs *)class_dev->class_data;
217         if (!sfp) return -EINVAL;
218         cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
219         if (!cptr) return -EINVAL;
220         ecnt = pvr2_ctrl_get_cnt(cptr);
221         bcnt = 0;
222         for (val = 0; val < ecnt; val++) {
223                 pvr2_ctrl_get_valname(cptr,val,buf+bcnt,PAGE_SIZE-bcnt,&ccnt);
224                 if (!ccnt) continue;
225                 bcnt += ccnt;
226                 if (bcnt >= PAGE_SIZE) break;
227                 buf[bcnt] = '\n';
228                 bcnt++;
229         }
230         pvr2_sysfs_trace("pvr2_sysfs(%p) show_enum(cid=%d)",sfp,id);
231         return bcnt;
232 }
233
234 static ssize_t show_bits(int id,struct class_device *class_dev,char *buf)
235 {
236         struct pvr2_ctrl *cptr;
237         struct pvr2_sysfs *sfp;
238         int valid_bits,msk;
239         unsigned int bcnt,ccnt;
240
241         sfp = (struct pvr2_sysfs *)class_dev->class_data;
242         if (!sfp) return -EINVAL;
243         cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
244         if (!cptr) return -EINVAL;
245         valid_bits = pvr2_ctrl_get_mask(cptr);
246         bcnt = 0;
247         for (msk = 1; valid_bits; msk <<= 1) {
248                 if (!(msk & valid_bits)) continue;
249                 valid_bits &= ~msk;
250                 pvr2_ctrl_get_valname(cptr,msk,buf+bcnt,PAGE_SIZE-bcnt,&ccnt);
251                 bcnt += ccnt;
252                 if (bcnt >= PAGE_SIZE) break;
253                 buf[bcnt] = '\n';
254                 bcnt++;
255         }
256         pvr2_sysfs_trace("pvr2_sysfs(%p) show_bits(cid=%d)",sfp,id);
257         return bcnt;
258 }
259
260 static int store_val_any(int id,int customfl,struct pvr2_sysfs *sfp,
261                          const char *buf,unsigned int count)
262 {
263         struct pvr2_ctrl *cptr;
264         int ret;
265         int mask,val;
266
267         cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
268         if (customfl) {
269                 ret = pvr2_ctrl_custom_sym_to_value(cptr,buf,count,&mask,&val);
270         } else {
271                 ret = pvr2_ctrl_sym_to_value(cptr,buf,count,&mask,&val);
272         }
273         if (ret < 0) return ret;
274         ret = pvr2_ctrl_set_mask_value(cptr,mask,val);
275         pvr2_hdw_commit_ctl(sfp->channel.hdw);
276         return ret;
277 }
278
279 static ssize_t store_val_norm(int id,struct class_device *class_dev,
280                              const char *buf,size_t count)
281 {
282         struct pvr2_sysfs *sfp;
283         int ret;
284         sfp = (struct pvr2_sysfs *)class_dev->class_data;
285         ret = store_val_any(id,0,sfp,buf,count);
286         if (!ret) ret = count;
287         return ret;
288 }
289
290 static ssize_t store_val_custom(int id,struct class_device *class_dev,
291                                 const char *buf,size_t count)
292 {
293         struct pvr2_sysfs *sfp;
294         int ret;
295         sfp = (struct pvr2_sysfs *)class_dev->class_data;
296         ret = store_val_any(id,1,sfp,buf,count);
297         if (!ret) ret = count;
298         return ret;
299 }
300
301 /*
302   Mike Isely <isely@pobox.com> 30-April-2005
303
304   This next batch of horrible preprocessor hackery is needed because the
305   kernel's class_device_attribute mechanism fails to pass the actual
306   attribute through to the show / store functions, which means we have no
307   way to package up any attribute-specific parameters, like for example the
308   control id.  So we work around this brain-damage by encoding the control
309   id into the show / store functions themselves and pick the function based
310   on the control id we're setting up.  These macros try to ease the pain.
311   Yuck.
312 */
313
314 #define CREATE_SHOW_INSTANCE(sf_name,ctl_id) \
315 static ssize_t sf_name##_##ctl_id(struct class_device *class_dev,char *buf) \
316 { return sf_name(ctl_id,class_dev,buf); }
317
318 #define CREATE_STORE_INSTANCE(sf_name,ctl_id) \
319 static ssize_t sf_name##_##ctl_id(struct class_device *class_dev,const char *buf,size_t count) \
320 { return sf_name(ctl_id,class_dev,buf,count); }
321
322 #define CREATE_BATCH(ctl_id) \
323 CREATE_SHOW_INSTANCE(show_name,ctl_id) \
324 CREATE_SHOW_INSTANCE(show_type,ctl_id) \
325 CREATE_SHOW_INSTANCE(show_min,ctl_id) \
326 CREATE_SHOW_INSTANCE(show_max,ctl_id) \
327 CREATE_SHOW_INSTANCE(show_val_norm,ctl_id) \
328 CREATE_SHOW_INSTANCE(show_val_custom,ctl_id) \
329 CREATE_SHOW_INSTANCE(show_enum,ctl_id) \
330 CREATE_SHOW_INSTANCE(show_bits,ctl_id) \
331 CREATE_STORE_INSTANCE(store_val_norm,ctl_id) \
332 CREATE_STORE_INSTANCE(store_val_custom,ctl_id) \
333
334 CREATE_BATCH(0)
335 CREATE_BATCH(1)
336 CREATE_BATCH(2)
337 CREATE_BATCH(3)
338 CREATE_BATCH(4)
339 CREATE_BATCH(5)
340 CREATE_BATCH(6)
341 CREATE_BATCH(7)
342 CREATE_BATCH(8)
343 CREATE_BATCH(9)
344 CREATE_BATCH(10)
345 CREATE_BATCH(11)
346 CREATE_BATCH(12)
347 CREATE_BATCH(13)
348 CREATE_BATCH(14)
349 CREATE_BATCH(15)
350 CREATE_BATCH(16)
351 CREATE_BATCH(17)
352 CREATE_BATCH(18)
353 CREATE_BATCH(19)
354 CREATE_BATCH(20)
355 CREATE_BATCH(21)
356 CREATE_BATCH(22)
357 CREATE_BATCH(23)
358 CREATE_BATCH(24)
359 CREATE_BATCH(25)
360 CREATE_BATCH(26)
361 CREATE_BATCH(27)
362 CREATE_BATCH(28)
363 CREATE_BATCH(29)
364 CREATE_BATCH(30)
365 CREATE_BATCH(31)
366 CREATE_BATCH(32)
367 CREATE_BATCH(33)
368 CREATE_BATCH(34)
369 CREATE_BATCH(35)
370 CREATE_BATCH(36)
371 CREATE_BATCH(37)
372 CREATE_BATCH(38)
373 CREATE_BATCH(39)
374 CREATE_BATCH(40)
375 CREATE_BATCH(41)
376 CREATE_BATCH(42)
377 CREATE_BATCH(43)
378 CREATE_BATCH(44)
379 CREATE_BATCH(45)
380 CREATE_BATCH(46)
381 CREATE_BATCH(47)
382 CREATE_BATCH(48)
383 CREATE_BATCH(49)
384 CREATE_BATCH(50)
385 CREATE_BATCH(51)
386 CREATE_BATCH(52)
387 CREATE_BATCH(53)
388 CREATE_BATCH(54)
389 CREATE_BATCH(55)
390 CREATE_BATCH(56)
391 CREATE_BATCH(57)
392 CREATE_BATCH(58)
393 CREATE_BATCH(59)
394
395 struct pvr2_sysfs_func_set {
396         ssize_t (*show_name)(struct class_device *,char *);
397         ssize_t (*show_type)(struct class_device *,char *);
398         ssize_t (*show_min)(struct class_device *,char *);
399         ssize_t (*show_max)(struct class_device *,char *);
400         ssize_t (*show_enum)(struct class_device *,char *);
401         ssize_t (*show_bits)(struct class_device *,char *);
402         ssize_t (*show_val_norm)(struct class_device *,char *);
403         ssize_t (*store_val_norm)(struct class_device *,
404                                   const char *,size_t);
405         ssize_t (*show_val_custom)(struct class_device *,char *);
406         ssize_t (*store_val_custom)(struct class_device *,
407                                     const char *,size_t);
408 };
409
410 #define INIT_BATCH(ctl_id) \
411 [ctl_id] = { \
412     .show_name = show_name_##ctl_id, \
413     .show_type = show_type_##ctl_id, \
414     .show_min = show_min_##ctl_id, \
415     .show_max = show_max_##ctl_id, \
416     .show_enum = show_enum_##ctl_id, \
417     .show_bits = show_bits_##ctl_id, \
418     .show_val_norm = show_val_norm_##ctl_id, \
419     .store_val_norm = store_val_norm_##ctl_id, \
420     .show_val_custom = show_val_custom_##ctl_id, \
421     .store_val_custom = store_val_custom_##ctl_id, \
422 } \
423
424 static struct pvr2_sysfs_func_set funcs[] = {
425         INIT_BATCH(0),
426         INIT_BATCH(1),
427         INIT_BATCH(2),
428         INIT_BATCH(3),
429         INIT_BATCH(4),
430         INIT_BATCH(5),
431         INIT_BATCH(6),
432         INIT_BATCH(7),
433         INIT_BATCH(8),
434         INIT_BATCH(9),
435         INIT_BATCH(10),
436         INIT_BATCH(11),
437         INIT_BATCH(12),
438         INIT_BATCH(13),
439         INIT_BATCH(14),
440         INIT_BATCH(15),
441         INIT_BATCH(16),
442         INIT_BATCH(17),
443         INIT_BATCH(18),
444         INIT_BATCH(19),
445         INIT_BATCH(20),
446         INIT_BATCH(21),
447         INIT_BATCH(22),
448         INIT_BATCH(23),
449         INIT_BATCH(24),
450         INIT_BATCH(25),
451         INIT_BATCH(26),
452         INIT_BATCH(27),
453         INIT_BATCH(28),
454         INIT_BATCH(29),
455         INIT_BATCH(30),
456         INIT_BATCH(31),
457         INIT_BATCH(32),
458         INIT_BATCH(33),
459         INIT_BATCH(34),
460         INIT_BATCH(35),
461         INIT_BATCH(36),
462         INIT_BATCH(37),
463         INIT_BATCH(38),
464         INIT_BATCH(39),
465         INIT_BATCH(40),
466         INIT_BATCH(41),
467         INIT_BATCH(42),
468         INIT_BATCH(43),
469         INIT_BATCH(44),
470         INIT_BATCH(45),
471         INIT_BATCH(46),
472         INIT_BATCH(47),
473         INIT_BATCH(48),
474         INIT_BATCH(49),
475         INIT_BATCH(50),
476         INIT_BATCH(51),
477         INIT_BATCH(52),
478         INIT_BATCH(53),
479         INIT_BATCH(54),
480         INIT_BATCH(55),
481         INIT_BATCH(56),
482         INIT_BATCH(57),
483         INIT_BATCH(58),
484         INIT_BATCH(59),
485 };
486
487
488 static void pvr2_sysfs_add_control(struct pvr2_sysfs *sfp,int ctl_id)
489 {
490         struct pvr2_sysfs_ctl_item *cip;
491         struct pvr2_sysfs_func_set *fp;
492         struct pvr2_ctrl *cptr;
493         unsigned int cnt,acnt;
494         int ret;
495
496         if ((ctl_id < 0) || (ctl_id >= (sizeof(funcs)/sizeof(funcs[0])))) {
497                 return;
498         }
499
500         fp = funcs + ctl_id;
501         cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,ctl_id);
502         if (!cptr) return;
503
504         cip = kmalloc(sizeof(*cip),GFP_KERNEL);
505         if (!cip) return;
506         memset(cip,0,sizeof(*cip));
507         pvr2_sysfs_trace("Creating pvr2_sysfs_ctl_item id=%p",cip);
508
509         cip->cptr = cptr;
510
511         cip->chptr = sfp;
512         cip->item_next = NULL;
513         if (sfp->item_last) {
514                 sfp->item_last->item_next = cip;
515         } else {
516                 sfp->item_first = cip;
517         }
518         sfp->item_last = cip;
519
520         cip->attr_name.attr.owner = THIS_MODULE;
521         cip->attr_name.attr.name = "name";
522         cip->attr_name.attr.mode = S_IRUGO;
523         cip->attr_name.show = fp->show_name;
524
525         cip->attr_type.attr.owner = THIS_MODULE;
526         cip->attr_type.attr.name = "type";
527         cip->attr_type.attr.mode = S_IRUGO;
528         cip->attr_type.show = fp->show_type;
529
530         cip->attr_min.attr.owner = THIS_MODULE;
531         cip->attr_min.attr.name = "min_val";
532         cip->attr_min.attr.mode = S_IRUGO;
533         cip->attr_min.show = fp->show_min;
534
535         cip->attr_max.attr.owner = THIS_MODULE;
536         cip->attr_max.attr.name = "max_val";
537         cip->attr_max.attr.mode = S_IRUGO;
538         cip->attr_max.show = fp->show_max;
539
540         cip->attr_val.attr.owner = THIS_MODULE;
541         cip->attr_val.attr.name = "cur_val";
542         cip->attr_val.attr.mode = S_IRUGO;
543
544         cip->attr_custom.attr.owner = THIS_MODULE;
545         cip->attr_custom.attr.name = "custom_val";
546         cip->attr_custom.attr.mode = S_IRUGO;
547
548         cip->attr_enum.attr.owner = THIS_MODULE;
549         cip->attr_enum.attr.name = "enum_val";
550         cip->attr_enum.attr.mode = S_IRUGO;
551         cip->attr_enum.show = fp->show_enum;
552
553         cip->attr_bits.attr.owner = THIS_MODULE;
554         cip->attr_bits.attr.name = "bit_val";
555         cip->attr_bits.attr.mode = S_IRUGO;
556         cip->attr_bits.show = fp->show_bits;
557
558         if (pvr2_ctrl_is_writable(cptr)) {
559                 cip->attr_val.attr.mode |= S_IWUSR|S_IWGRP;
560                 cip->attr_custom.attr.mode |= S_IWUSR|S_IWGRP;
561         }
562
563         acnt = 0;
564         cip->attr_gen[acnt++] = &cip->attr_name.attr;
565         cip->attr_gen[acnt++] = &cip->attr_type.attr;
566         cip->attr_gen[acnt++] = &cip->attr_val.attr;
567         cip->attr_val.show = fp->show_val_norm;
568         cip->attr_val.store = fp->store_val_norm;
569         if (pvr2_ctrl_has_custom_symbols(cptr)) {
570                 cip->attr_gen[acnt++] = &cip->attr_custom.attr;
571                 cip->attr_custom.show = fp->show_val_custom;
572                 cip->attr_custom.store = fp->store_val_custom;
573         }
574         switch (pvr2_ctrl_get_type(cptr)) {
575         case pvr2_ctl_enum:
576                 // Control is an enumeration
577                 cip->attr_gen[acnt++] = &cip->attr_enum.attr;
578                 break;
579         case pvr2_ctl_int:
580                 // Control is an integer
581                 cip->attr_gen[acnt++] = &cip->attr_min.attr;
582                 cip->attr_gen[acnt++] = &cip->attr_max.attr;
583                 break;
584         case pvr2_ctl_bitmask:
585                 // Control is an bitmask
586                 cip->attr_gen[acnt++] = &cip->attr_bits.attr;
587                 break;
588         default: break;
589         }
590
591         cnt = scnprintf(cip->name,sizeof(cip->name)-1,"ctl_%s",
592                         pvr2_ctrl_get_name(cptr));
593         cip->name[cnt] = 0;
594         cip->grp.name = cip->name;
595         cip->grp.attrs = cip->attr_gen;
596
597         ret = sysfs_create_group(&sfp->class_dev->kobj,&cip->grp);
598         if (ret) {
599                 printk(KERN_WARNING "%s: sysfs_create_group error: %d\n",
600                        __FUNCTION__, ret);
601                 return;
602         }
603         cip->created_ok = !0;
604 }
605
606 #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
607 static ssize_t debuginfo_show(struct class_device *,char *);
608 static ssize_t debugcmd_show(struct class_device *,char *);
609 static ssize_t debugcmd_store(struct class_device *,const char *,size_t count);
610
611 static void pvr2_sysfs_add_debugifc(struct pvr2_sysfs *sfp)
612 {
613         struct pvr2_sysfs_debugifc *dip;
614         int ret;
615
616         dip = kmalloc(sizeof(*dip),GFP_KERNEL);
617         if (!dip) return;
618         memset(dip,0,sizeof(*dip));
619         dip->attr_debugcmd.attr.owner = THIS_MODULE;
620         dip->attr_debugcmd.attr.name = "debugcmd";
621         dip->attr_debugcmd.attr.mode = S_IRUGO|S_IWUSR|S_IWGRP;
622         dip->attr_debugcmd.show = debugcmd_show;
623         dip->attr_debugcmd.store = debugcmd_store;
624         dip->attr_debuginfo.attr.owner = THIS_MODULE;
625         dip->attr_debuginfo.attr.name = "debuginfo";
626         dip->attr_debuginfo.attr.mode = S_IRUGO;
627         dip->attr_debuginfo.show = debuginfo_show;
628         sfp->debugifc = dip;
629         ret = class_device_create_file(sfp->class_dev,&dip->attr_debugcmd);
630         if (ret < 0) {
631                 printk(KERN_WARNING "%s: class_device_create_file error: %d\n",
632                        __FUNCTION__, ret);
633         } else {
634                 dip->debugcmd_created_ok = !0;
635         }
636         ret = class_device_create_file(sfp->class_dev,&dip->attr_debuginfo);
637         if (ret < 0) {
638                 printk(KERN_WARNING "%s: class_device_create_file error: %d\n",
639                        __FUNCTION__, ret);
640         } else {
641                 dip->debuginfo_created_ok = !0;
642         }
643 }
644
645
646 static void pvr2_sysfs_tear_down_debugifc(struct pvr2_sysfs *sfp)
647 {
648         if (!sfp->debugifc) return;
649         if (sfp->debugifc->debuginfo_created_ok) {
650                 class_device_remove_file(sfp->class_dev,
651                                          &sfp->debugifc->attr_debuginfo);
652         }
653         if (sfp->debugifc->debugcmd_created_ok) {
654                 class_device_remove_file(sfp->class_dev,
655                                          &sfp->debugifc->attr_debugcmd);
656         }
657         kfree(sfp->debugifc);
658         sfp->debugifc = NULL;
659 }
660 #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
661
662
663 static void pvr2_sysfs_add_controls(struct pvr2_sysfs *sfp)
664 {
665         unsigned int idx,cnt;
666         cnt = pvr2_hdw_get_ctrl_count(sfp->channel.hdw);
667         for (idx = 0; idx < cnt; idx++) {
668                 pvr2_sysfs_add_control(sfp,idx);
669         }
670 }
671
672
673 static void pvr2_sysfs_tear_down_controls(struct pvr2_sysfs *sfp)
674 {
675         struct pvr2_sysfs_ctl_item *cip1,*cip2;
676         for (cip1 = sfp->item_first; cip1; cip1 = cip2) {
677                 cip2 = cip1->item_next;
678                 if (cip1->created_ok) {
679                         sysfs_remove_group(&sfp->class_dev->kobj,&cip1->grp);
680                 }
681                 pvr2_sysfs_trace("Destroying pvr2_sysfs_ctl_item id=%p",cip1);
682                 kfree(cip1);
683         }
684 }
685
686
687 static void pvr2_sysfs_class_release(struct class *class)
688 {
689         struct pvr2_sysfs_class *clp;
690         clp = container_of(class,struct pvr2_sysfs_class,class);
691         pvr2_sysfs_trace("Destroying pvr2_sysfs_class id=%p",clp);
692         kfree(clp);
693 }
694
695
696 static void pvr2_sysfs_release(struct class_device *class_dev)
697 {
698         pvr2_sysfs_trace("Releasing class_dev id=%p",class_dev);
699         kfree(class_dev);
700 }
701
702
703 static void class_dev_destroy(struct pvr2_sysfs *sfp)
704 {
705         if (!sfp->class_dev) return;
706 #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
707         pvr2_sysfs_tear_down_debugifc(sfp);
708 #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
709         pvr2_sysfs_tear_down_controls(sfp);
710         if (sfp->v4l_minor_number_created_ok) {
711                 class_device_remove_file(sfp->class_dev,
712                                          &sfp->attr_v4l_minor_number);
713         }
714         if (sfp->unit_number_created_ok) {
715                 class_device_remove_file(sfp->class_dev,
716                                          &sfp->attr_unit_number);
717         }
718         pvr2_sysfs_trace("Destroying class_dev id=%p",sfp->class_dev);
719         sfp->class_dev->class_data = NULL;
720         class_device_unregister(sfp->class_dev);
721         sfp->class_dev = NULL;
722 }
723
724
725 static ssize_t v4l_minor_number_show(struct class_device *class_dev,char *buf)
726 {
727         struct pvr2_sysfs *sfp;
728         sfp = (struct pvr2_sysfs *)class_dev->class_data;
729         if (!sfp) return -EINVAL;
730         return scnprintf(buf,PAGE_SIZE,"%d\n",
731                          pvr2_hdw_v4l_get_minor_number(sfp->channel.hdw));
732 }
733
734
735 static ssize_t unit_number_show(struct class_device *class_dev,char *buf)
736 {
737         struct pvr2_sysfs *sfp;
738         sfp = (struct pvr2_sysfs *)class_dev->class_data;
739         if (!sfp) return -EINVAL;
740         return scnprintf(buf,PAGE_SIZE,"%d\n",
741                          pvr2_hdw_get_unit_number(sfp->channel.hdw));
742 }
743
744
745 static void class_dev_create(struct pvr2_sysfs *sfp,
746                              struct pvr2_sysfs_class *class_ptr)
747 {
748         struct usb_device *usb_dev;
749         struct class_device *class_dev;
750         int ret;
751
752         usb_dev = pvr2_hdw_get_dev(sfp->channel.hdw);
753         if (!usb_dev) return;
754         class_dev = kmalloc(sizeof(*class_dev),GFP_KERNEL);
755         if (!class_dev) return;
756         memset(class_dev,0,sizeof(*class_dev));
757
758         pvr2_sysfs_trace("Creating class_dev id=%p",class_dev);
759
760         class_dev->class = &class_ptr->class;
761         if (pvr2_hdw_get_sn(sfp->channel.hdw)) {
762                 snprintf(class_dev->class_id,BUS_ID_SIZE,"sn-%lu",
763                          pvr2_hdw_get_sn(sfp->channel.hdw));
764         } else if (pvr2_hdw_get_unit_number(sfp->channel.hdw) >= 0) {
765                 snprintf(class_dev->class_id,BUS_ID_SIZE,"unit-%c",
766                          pvr2_hdw_get_unit_number(sfp->channel.hdw) + 'a');
767         } else {
768                 kfree(class_dev);
769                 return;
770         }
771
772         class_dev->dev = &usb_dev->dev;
773
774         sfp->class_dev = class_dev;
775         class_dev->class_data = sfp;
776         ret = class_device_register(class_dev);
777         if (ret) {
778                 printk(KERN_ERR "%s: class_device_register failed\n",
779                        __FUNCTION__);
780                 kfree(class_dev);
781                 return;
782         }
783
784         sfp->attr_v4l_minor_number.attr.owner = THIS_MODULE;
785         sfp->attr_v4l_minor_number.attr.name = "v4l_minor_number";
786         sfp->attr_v4l_minor_number.attr.mode = S_IRUGO;
787         sfp->attr_v4l_minor_number.show = v4l_minor_number_show;
788         sfp->attr_v4l_minor_number.store = NULL;
789         ret = class_device_create_file(sfp->class_dev,
790                                        &sfp->attr_v4l_minor_number);
791         if (ret < 0) {
792                 printk(KERN_WARNING "%s: class_device_create_file error: %d\n",
793                        __FUNCTION__, ret);
794         } else {
795                 sfp->v4l_minor_number_created_ok = !0;
796         }
797
798         sfp->attr_unit_number.attr.owner = THIS_MODULE;
799         sfp->attr_unit_number.attr.name = "unit_number";
800         sfp->attr_unit_number.attr.mode = S_IRUGO;
801         sfp->attr_unit_number.show = unit_number_show;
802         sfp->attr_unit_number.store = NULL;
803         ret = class_device_create_file(sfp->class_dev,&sfp->attr_unit_number);
804         if (ret < 0) {
805                 printk(KERN_WARNING "%s: class_device_create_file error: %d\n",
806                        __FUNCTION__, ret);
807         } else {
808                 sfp->unit_number_created_ok = !0;
809         }
810
811         pvr2_sysfs_add_controls(sfp);
812 #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
813         pvr2_sysfs_add_debugifc(sfp);
814 #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
815 }
816
817
818 static void pvr2_sysfs_internal_check(struct pvr2_channel *chp)
819 {
820         struct pvr2_sysfs *sfp;
821         sfp = container_of(chp,struct pvr2_sysfs,channel);
822         if (!sfp->channel.mc_head->disconnect_flag) return;
823         pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_sysfs id=%p",sfp);
824         class_dev_destroy(sfp);
825         pvr2_channel_done(&sfp->channel);
826         kfree(sfp);
827 }
828
829
830 struct pvr2_sysfs *pvr2_sysfs_create(struct pvr2_context *mp,
831                                      struct pvr2_sysfs_class *class_ptr)
832 {
833         struct pvr2_sysfs *sfp;
834         sfp = kmalloc(sizeof(*sfp),GFP_KERNEL);
835         if (!sfp) return sfp;
836         memset(sfp,0,sizeof(*sfp));
837         pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_sysfs id=%p",sfp);
838         pvr2_channel_init(&sfp->channel,mp);
839         sfp->channel.check_func = pvr2_sysfs_internal_check;
840
841         class_dev_create(sfp,class_ptr);
842         return sfp;
843 }
844
845
846 static int pvr2_sysfs_hotplug(struct class_device *cd,char **envp,
847                               int numenvp,char *buf,int size)
848 {
849         /* Even though we don't do anything here, we still need this function
850            because sysfs will still try to call it. */
851         return 0;
852 }
853
854 struct pvr2_sysfs_class *pvr2_sysfs_class_create(void)
855 {
856         struct pvr2_sysfs_class *clp;
857         clp = kmalloc(sizeof(*clp),GFP_KERNEL);
858         if (!clp) return clp;
859         memset(clp,0,sizeof(*clp));
860         pvr2_sysfs_trace("Creating pvr2_sysfs_class id=%p",clp);
861         clp->class.name = "pvrusb2";
862         clp->class.class_release = pvr2_sysfs_class_release;
863         clp->class.release = pvr2_sysfs_release;
864         clp->class.uevent = pvr2_sysfs_hotplug;
865         if (class_register(&clp->class)) {
866                 pvr2_sysfs_trace(
867                         "Registration failed for pvr2_sysfs_class id=%p",clp);
868                 kfree(clp);
869                 clp = NULL;
870         }
871         return clp;
872 }
873
874
875 void pvr2_sysfs_class_destroy(struct pvr2_sysfs_class *clp)
876 {
877         class_unregister(&clp->class);
878 }
879
880
881 #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
882 static ssize_t debuginfo_show(struct class_device *class_dev,char *buf)
883 {
884         struct pvr2_sysfs *sfp;
885         sfp = (struct pvr2_sysfs *)class_dev->class_data;
886         if (!sfp) return -EINVAL;
887         pvr2_hdw_trigger_module_log(sfp->channel.hdw);
888         return pvr2_debugifc_print_info(sfp->channel.hdw,buf,PAGE_SIZE);
889 }
890
891
892 static ssize_t debugcmd_show(struct class_device *class_dev,char *buf)
893 {
894         struct pvr2_sysfs *sfp;
895         sfp = (struct pvr2_sysfs *)class_dev->class_data;
896         if (!sfp) return -EINVAL;
897         return pvr2_debugifc_print_status(sfp->channel.hdw,buf,PAGE_SIZE);
898 }
899
900
901 static ssize_t debugcmd_store(struct class_device *class_dev,
902                               const char *buf,size_t count)
903 {
904         struct pvr2_sysfs *sfp;
905         int ret;
906
907         sfp = (struct pvr2_sysfs *)class_dev->class_data;
908         if (!sfp) return -EINVAL;
909
910         ret = pvr2_debugifc_docmd(sfp->channel.hdw,buf,count);
911         if (ret < 0) return ret;
912         return count;
913 }
914 #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
915
916
917 /*
918   Stuff for Emacs to see, in order to encourage consistent editing style:
919   *** Local Variables: ***
920   *** mode: c ***
921   *** fill-column: 75 ***
922   *** tab-width: 8 ***
923   *** c-basic-offset: 8 ***
924   *** End: ***
925   */