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