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