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