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