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