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