iwlwifi: introduce host commands callbacks
[linux/fpc-iii.git] / drivers / media / video / pvrusb2 / pvrusb2-sysfs.c
blob7a1cd878e31a70f8423bbdf6810914fa2ec3192c
1 /*
3 * $Id$
5 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
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
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.
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
22 #include <linux/string.h>
23 #include <linux/slab.h>
24 #include <asm/semaphore.h>
25 #include "pvrusb2-sysfs.h"
26 #include "pvrusb2-hdw.h"
27 #include "pvrusb2-debug.h"
28 #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
29 #include "pvrusb2-debugifc.h"
30 #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
32 #define pvr2_sysfs_trace(...) pvr2_trace(PVR2_TRACE_SYSFS,__VA_ARGS__)
34 struct pvr2_sysfs {
35 struct pvr2_channel channel;
36 struct device *class_dev;
37 #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
38 struct pvr2_sysfs_debugifc *debugifc;
39 #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
40 struct pvr2_sysfs_ctl_item *item_first;
41 struct pvr2_sysfs_ctl_item *item_last;
42 struct device_attribute attr_v4l_minor_number;
43 struct device_attribute attr_v4l_radio_minor_number;
44 struct device_attribute attr_unit_number;
45 struct device_attribute attr_bus_info;
46 struct device_attribute attr_hdw_name;
47 struct device_attribute attr_hdw_desc;
48 int v4l_minor_number_created_ok;
49 int v4l_radio_minor_number_created_ok;
50 int unit_number_created_ok;
51 int bus_info_created_ok;
52 int hdw_name_created_ok;
53 int hdw_desc_created_ok;
56 #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
57 struct pvr2_sysfs_debugifc {
58 struct device_attribute attr_debugcmd;
59 struct device_attribute attr_debuginfo;
60 int debugcmd_created_ok;
61 int debuginfo_created_ok;
63 #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
65 struct pvr2_sysfs_ctl_item {
66 struct device_attribute attr_name;
67 struct device_attribute attr_type;
68 struct device_attribute attr_min;
69 struct device_attribute attr_max;
70 struct device_attribute attr_enum;
71 struct device_attribute attr_bits;
72 struct device_attribute attr_val;
73 struct device_attribute attr_custom;
74 struct pvr2_ctrl *cptr;
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];
83 struct pvr2_sysfs_class {
84 struct class class;
87 static ssize_t show_name(int id,struct device *class_dev,char *buf)
89 struct pvr2_ctrl *cptr;
90 struct pvr2_sysfs *sfp;
91 const char *name;
93 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
94 if (!sfp) return -EINVAL;
95 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
96 if (!cptr) return -EINVAL;
98 name = pvr2_ctrl_get_desc(cptr);
99 pvr2_sysfs_trace("pvr2_sysfs(%p) show_name(cid=%d) is %s",sfp,id,name);
101 if (!name) return -EINVAL;
103 return scnprintf(buf,PAGE_SIZE,"%s\n",name);
106 static ssize_t show_type(int id,struct device *class_dev,char *buf)
108 struct pvr2_ctrl *cptr;
109 struct pvr2_sysfs *sfp;
110 const char *name;
111 enum pvr2_ctl_type tp;
113 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
114 if (!sfp) return -EINVAL;
115 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
116 if (!cptr) return -EINVAL;
118 tp = pvr2_ctrl_get_type(cptr);
119 switch (tp) {
120 case pvr2_ctl_int: name = "integer"; break;
121 case pvr2_ctl_enum: name = "enum"; break;
122 case pvr2_ctl_bitmask: name = "bitmask"; break;
123 case pvr2_ctl_bool: name = "boolean"; break;
124 default: name = "?"; break;
126 pvr2_sysfs_trace("pvr2_sysfs(%p) show_type(cid=%d) is %s",sfp,id,name);
128 if (!name) return -EINVAL;
130 return scnprintf(buf,PAGE_SIZE,"%s\n",name);
133 static ssize_t show_min(int id,struct device *class_dev,char *buf)
135 struct pvr2_ctrl *cptr;
136 struct pvr2_sysfs *sfp;
137 long val;
139 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
140 if (!sfp) return -EINVAL;
141 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
142 if (!cptr) return -EINVAL;
143 val = pvr2_ctrl_get_min(cptr);
145 pvr2_sysfs_trace("pvr2_sysfs(%p) show_min(cid=%d) is %ld",sfp,id,val);
147 return scnprintf(buf,PAGE_SIZE,"%ld\n",val);
150 static ssize_t show_max(int id,struct device *class_dev,char *buf)
152 struct pvr2_ctrl *cptr;
153 struct pvr2_sysfs *sfp;
154 long val;
156 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
157 if (!sfp) return -EINVAL;
158 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
159 if (!cptr) return -EINVAL;
160 val = pvr2_ctrl_get_max(cptr);
162 pvr2_sysfs_trace("pvr2_sysfs(%p) show_max(cid=%d) is %ld",sfp,id,val);
164 return scnprintf(buf,PAGE_SIZE,"%ld\n",val);
167 static ssize_t show_val_norm(int id,struct device *class_dev,char *buf)
169 struct pvr2_ctrl *cptr;
170 struct pvr2_sysfs *sfp;
171 int val,ret;
172 unsigned int cnt = 0;
174 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
175 if (!sfp) return -EINVAL;
176 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
177 if (!cptr) return -EINVAL;
179 ret = pvr2_ctrl_get_value(cptr,&val);
180 if (ret < 0) return ret;
182 ret = pvr2_ctrl_value_to_sym(cptr,~0,val,
183 buf,PAGE_SIZE-1,&cnt);
185 pvr2_sysfs_trace("pvr2_sysfs(%p) show_val_norm(cid=%d) is %.*s (%d)",
186 sfp,id,cnt,buf,val);
187 buf[cnt] = '\n';
188 return cnt+1;
191 static ssize_t show_val_custom(int id,struct device *class_dev,char *buf)
193 struct pvr2_ctrl *cptr;
194 struct pvr2_sysfs *sfp;
195 int val,ret;
196 unsigned int cnt = 0;
198 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
199 if (!sfp) return -EINVAL;
200 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
201 if (!cptr) return -EINVAL;
203 ret = pvr2_ctrl_get_value(cptr,&val);
204 if (ret < 0) return ret;
206 ret = pvr2_ctrl_custom_value_to_sym(cptr,~0,val,
207 buf,PAGE_SIZE-1,&cnt);
209 pvr2_sysfs_trace("pvr2_sysfs(%p) show_val_custom(cid=%d) is %.*s (%d)",
210 sfp,id,cnt,buf,val);
211 buf[cnt] = '\n';
212 return cnt+1;
215 static ssize_t show_enum(int id,struct device *class_dev,char *buf)
217 struct pvr2_ctrl *cptr;
218 struct pvr2_sysfs *sfp;
219 long val;
220 unsigned int bcnt,ccnt,ecnt;
222 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
223 if (!sfp) return -EINVAL;
224 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
225 if (!cptr) return -EINVAL;
226 ecnt = pvr2_ctrl_get_cnt(cptr);
227 bcnt = 0;
228 for (val = 0; val < ecnt; val++) {
229 pvr2_ctrl_get_valname(cptr,val,buf+bcnt,PAGE_SIZE-bcnt,&ccnt);
230 if (!ccnt) continue;
231 bcnt += ccnt;
232 if (bcnt >= PAGE_SIZE) break;
233 buf[bcnt] = '\n';
234 bcnt++;
236 pvr2_sysfs_trace("pvr2_sysfs(%p) show_enum(cid=%d)",sfp,id);
237 return bcnt;
240 static ssize_t show_bits(int id,struct device *class_dev,char *buf)
242 struct pvr2_ctrl *cptr;
243 struct pvr2_sysfs *sfp;
244 int valid_bits,msk;
245 unsigned int bcnt,ccnt;
247 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
248 if (!sfp) return -EINVAL;
249 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
250 if (!cptr) return -EINVAL;
251 valid_bits = pvr2_ctrl_get_mask(cptr);
252 bcnt = 0;
253 for (msk = 1; valid_bits; msk <<= 1) {
254 if (!(msk & valid_bits)) continue;
255 valid_bits &= ~msk;
256 pvr2_ctrl_get_valname(cptr,msk,buf+bcnt,PAGE_SIZE-bcnt,&ccnt);
257 bcnt += ccnt;
258 if (bcnt >= PAGE_SIZE) break;
259 buf[bcnt] = '\n';
260 bcnt++;
262 pvr2_sysfs_trace("pvr2_sysfs(%p) show_bits(cid=%d)",sfp,id);
263 return bcnt;
266 static int store_val_any(int id,int customfl,struct pvr2_sysfs *sfp,
267 const char *buf,unsigned int count)
269 struct pvr2_ctrl *cptr;
270 int ret;
271 int mask,val;
273 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
274 if (customfl) {
275 ret = pvr2_ctrl_custom_sym_to_value(cptr,buf,count,&mask,&val);
276 } else {
277 ret = pvr2_ctrl_sym_to_value(cptr,buf,count,&mask,&val);
279 if (ret < 0) return ret;
280 ret = pvr2_ctrl_set_mask_value(cptr,mask,val);
281 pvr2_hdw_commit_ctl(sfp->channel.hdw);
282 return ret;
285 static ssize_t store_val_norm(int id,struct device *class_dev,
286 const char *buf,size_t count)
288 struct pvr2_sysfs *sfp;
289 int ret;
290 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
291 ret = store_val_any(id,0,sfp,buf,count);
292 if (!ret) ret = count;
293 return ret;
296 static ssize_t store_val_custom(int id,struct device *class_dev,
297 const char *buf,size_t count)
299 struct pvr2_sysfs *sfp;
300 int ret;
301 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
302 ret = store_val_any(id,1,sfp,buf,count);
303 if (!ret) ret = count;
304 return ret;
308 Mike Isely <isely@pobox.com> 30-April-2005
310 This next batch of horrible preprocessor hackery is needed because the
311 kernel's device_attribute mechanism fails to pass the actual
312 attribute through to the show / store functions, which means we have no
313 way to package up any attribute-specific parameters, like for example the
314 control id. So we work around this brain-damage by encoding the control
315 id into the show / store functions themselves and pick the function based
316 on the control id we're setting up. These macros try to ease the pain.
317 Yuck.
320 #define CREATE_SHOW_INSTANCE(sf_name,ctl_id) \
321 static ssize_t sf_name##_##ctl_id(struct device *class_dev, \
322 struct device_attribute *attr, char *buf) \
323 { return sf_name(ctl_id,class_dev,buf); }
325 #define CREATE_STORE_INSTANCE(sf_name,ctl_id) \
326 static ssize_t sf_name##_##ctl_id(struct device *class_dev, \
327 struct device_attribute *attr, const char *buf, size_t count) \
328 { return sf_name(ctl_id,class_dev,buf,count); }
330 #define CREATE_BATCH(ctl_id) \
331 CREATE_SHOW_INSTANCE(show_name,ctl_id) \
332 CREATE_SHOW_INSTANCE(show_type,ctl_id) \
333 CREATE_SHOW_INSTANCE(show_min,ctl_id) \
334 CREATE_SHOW_INSTANCE(show_max,ctl_id) \
335 CREATE_SHOW_INSTANCE(show_val_norm,ctl_id) \
336 CREATE_SHOW_INSTANCE(show_val_custom,ctl_id) \
337 CREATE_SHOW_INSTANCE(show_enum,ctl_id) \
338 CREATE_SHOW_INSTANCE(show_bits,ctl_id) \
339 CREATE_STORE_INSTANCE(store_val_norm,ctl_id) \
340 CREATE_STORE_INSTANCE(store_val_custom,ctl_id) \
342 CREATE_BATCH(0)
343 CREATE_BATCH(1)
344 CREATE_BATCH(2)
345 CREATE_BATCH(3)
346 CREATE_BATCH(4)
347 CREATE_BATCH(5)
348 CREATE_BATCH(6)
349 CREATE_BATCH(7)
350 CREATE_BATCH(8)
351 CREATE_BATCH(9)
352 CREATE_BATCH(10)
353 CREATE_BATCH(11)
354 CREATE_BATCH(12)
355 CREATE_BATCH(13)
356 CREATE_BATCH(14)
357 CREATE_BATCH(15)
358 CREATE_BATCH(16)
359 CREATE_BATCH(17)
360 CREATE_BATCH(18)
361 CREATE_BATCH(19)
362 CREATE_BATCH(20)
363 CREATE_BATCH(21)
364 CREATE_BATCH(22)
365 CREATE_BATCH(23)
366 CREATE_BATCH(24)
367 CREATE_BATCH(25)
368 CREATE_BATCH(26)
369 CREATE_BATCH(27)
370 CREATE_BATCH(28)
371 CREATE_BATCH(29)
372 CREATE_BATCH(30)
373 CREATE_BATCH(31)
374 CREATE_BATCH(32)
375 CREATE_BATCH(33)
376 CREATE_BATCH(34)
377 CREATE_BATCH(35)
378 CREATE_BATCH(36)
379 CREATE_BATCH(37)
380 CREATE_BATCH(38)
381 CREATE_BATCH(39)
382 CREATE_BATCH(40)
383 CREATE_BATCH(41)
384 CREATE_BATCH(42)
385 CREATE_BATCH(43)
386 CREATE_BATCH(44)
387 CREATE_BATCH(45)
388 CREATE_BATCH(46)
389 CREATE_BATCH(47)
390 CREATE_BATCH(48)
391 CREATE_BATCH(49)
392 CREATE_BATCH(50)
393 CREATE_BATCH(51)
394 CREATE_BATCH(52)
395 CREATE_BATCH(53)
396 CREATE_BATCH(54)
397 CREATE_BATCH(55)
398 CREATE_BATCH(56)
399 CREATE_BATCH(57)
400 CREATE_BATCH(58)
401 CREATE_BATCH(59)
403 struct pvr2_sysfs_func_set {
404 ssize_t (*show_name)(struct device *,
405 struct device_attribute *attr, char *);
406 ssize_t (*show_type)(struct device *,
407 struct device_attribute *attr, char *);
408 ssize_t (*show_min)(struct device *,
409 struct device_attribute *attr, char *);
410 ssize_t (*show_max)(struct device *,
411 struct device_attribute *attr, char *);
412 ssize_t (*show_enum)(struct device *,
413 struct device_attribute *attr, char *);
414 ssize_t (*show_bits)(struct device *,
415 struct device_attribute *attr, char *);
416 ssize_t (*show_val_norm)(struct device *,
417 struct device_attribute *attr, char *);
418 ssize_t (*store_val_norm)(struct device *,
419 struct device_attribute *attr,
420 const char *,size_t);
421 ssize_t (*show_val_custom)(struct device *,
422 struct device_attribute *attr, char *);
423 ssize_t (*store_val_custom)(struct device *,
424 struct device_attribute *attr,
425 const char *,size_t);
428 #define INIT_BATCH(ctl_id) \
429 [ctl_id] = { \
430 .show_name = show_name_##ctl_id, \
431 .show_type = show_type_##ctl_id, \
432 .show_min = show_min_##ctl_id, \
433 .show_max = show_max_##ctl_id, \
434 .show_enum = show_enum_##ctl_id, \
435 .show_bits = show_bits_##ctl_id, \
436 .show_val_norm = show_val_norm_##ctl_id, \
437 .store_val_norm = store_val_norm_##ctl_id, \
438 .show_val_custom = show_val_custom_##ctl_id, \
439 .store_val_custom = store_val_custom_##ctl_id, \
442 static struct pvr2_sysfs_func_set funcs[] = {
443 INIT_BATCH(0),
444 INIT_BATCH(1),
445 INIT_BATCH(2),
446 INIT_BATCH(3),
447 INIT_BATCH(4),
448 INIT_BATCH(5),
449 INIT_BATCH(6),
450 INIT_BATCH(7),
451 INIT_BATCH(8),
452 INIT_BATCH(9),
453 INIT_BATCH(10),
454 INIT_BATCH(11),
455 INIT_BATCH(12),
456 INIT_BATCH(13),
457 INIT_BATCH(14),
458 INIT_BATCH(15),
459 INIT_BATCH(16),
460 INIT_BATCH(17),
461 INIT_BATCH(18),
462 INIT_BATCH(19),
463 INIT_BATCH(20),
464 INIT_BATCH(21),
465 INIT_BATCH(22),
466 INIT_BATCH(23),
467 INIT_BATCH(24),
468 INIT_BATCH(25),
469 INIT_BATCH(26),
470 INIT_BATCH(27),
471 INIT_BATCH(28),
472 INIT_BATCH(29),
473 INIT_BATCH(30),
474 INIT_BATCH(31),
475 INIT_BATCH(32),
476 INIT_BATCH(33),
477 INIT_BATCH(34),
478 INIT_BATCH(35),
479 INIT_BATCH(36),
480 INIT_BATCH(37),
481 INIT_BATCH(38),
482 INIT_BATCH(39),
483 INIT_BATCH(40),
484 INIT_BATCH(41),
485 INIT_BATCH(42),
486 INIT_BATCH(43),
487 INIT_BATCH(44),
488 INIT_BATCH(45),
489 INIT_BATCH(46),
490 INIT_BATCH(47),
491 INIT_BATCH(48),
492 INIT_BATCH(49),
493 INIT_BATCH(50),
494 INIT_BATCH(51),
495 INIT_BATCH(52),
496 INIT_BATCH(53),
497 INIT_BATCH(54),
498 INIT_BATCH(55),
499 INIT_BATCH(56),
500 INIT_BATCH(57),
501 INIT_BATCH(58),
502 INIT_BATCH(59),
506 static void pvr2_sysfs_add_control(struct pvr2_sysfs *sfp,int ctl_id)
508 struct pvr2_sysfs_ctl_item *cip;
509 struct pvr2_sysfs_func_set *fp;
510 struct pvr2_ctrl *cptr;
511 unsigned int cnt,acnt;
512 int ret;
514 if ((ctl_id < 0) || (ctl_id >= ARRAY_SIZE(funcs))) {
515 return;
518 fp = funcs + ctl_id;
519 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,ctl_id);
520 if (!cptr) return;
522 cip = kzalloc(sizeof(*cip),GFP_KERNEL);
523 if (!cip) return;
524 pvr2_sysfs_trace("Creating pvr2_sysfs_ctl_item id=%p",cip);
526 cip->cptr = cptr;
528 cip->chptr = sfp;
529 cip->item_next = NULL;
530 if (sfp->item_last) {
531 sfp->item_last->item_next = cip;
532 } else {
533 sfp->item_first = cip;
535 sfp->item_last = cip;
537 cip->attr_name.attr.name = "name";
538 cip->attr_name.attr.mode = S_IRUGO;
539 cip->attr_name.show = fp->show_name;
541 cip->attr_type.attr.name = "type";
542 cip->attr_type.attr.mode = S_IRUGO;
543 cip->attr_type.show = fp->show_type;
545 cip->attr_min.attr.name = "min_val";
546 cip->attr_min.attr.mode = S_IRUGO;
547 cip->attr_min.show = fp->show_min;
549 cip->attr_max.attr.name = "max_val";
550 cip->attr_max.attr.mode = S_IRUGO;
551 cip->attr_max.show = fp->show_max;
553 cip->attr_val.attr.name = "cur_val";
554 cip->attr_val.attr.mode = S_IRUGO;
556 cip->attr_custom.attr.name = "custom_val";
557 cip->attr_custom.attr.mode = S_IRUGO;
559 cip->attr_enum.attr.name = "enum_val";
560 cip->attr_enum.attr.mode = S_IRUGO;
561 cip->attr_enum.show = fp->show_enum;
563 cip->attr_bits.attr.name = "bit_val";
564 cip->attr_bits.attr.mode = S_IRUGO;
565 cip->attr_bits.show = fp->show_bits;
567 if (pvr2_ctrl_is_writable(cptr)) {
568 cip->attr_val.attr.mode |= S_IWUSR|S_IWGRP;
569 cip->attr_custom.attr.mode |= S_IWUSR|S_IWGRP;
572 acnt = 0;
573 cip->attr_gen[acnt++] = &cip->attr_name.attr;
574 cip->attr_gen[acnt++] = &cip->attr_type.attr;
575 cip->attr_gen[acnt++] = &cip->attr_val.attr;
576 cip->attr_val.show = fp->show_val_norm;
577 cip->attr_val.store = fp->store_val_norm;
578 if (pvr2_ctrl_has_custom_symbols(cptr)) {
579 cip->attr_gen[acnt++] = &cip->attr_custom.attr;
580 cip->attr_custom.show = fp->show_val_custom;
581 cip->attr_custom.store = fp->store_val_custom;
583 switch (pvr2_ctrl_get_type(cptr)) {
584 case pvr2_ctl_enum:
585 // Control is an enumeration
586 cip->attr_gen[acnt++] = &cip->attr_enum.attr;
587 break;
588 case pvr2_ctl_int:
589 // Control is an integer
590 cip->attr_gen[acnt++] = &cip->attr_min.attr;
591 cip->attr_gen[acnt++] = &cip->attr_max.attr;
592 break;
593 case pvr2_ctl_bitmask:
594 // Control is an bitmask
595 cip->attr_gen[acnt++] = &cip->attr_bits.attr;
596 break;
597 default: break;
600 cnt = scnprintf(cip->name,sizeof(cip->name)-1,"ctl_%s",
601 pvr2_ctrl_get_name(cptr));
602 cip->name[cnt] = 0;
603 cip->grp.name = cip->name;
604 cip->grp.attrs = cip->attr_gen;
606 ret = sysfs_create_group(&sfp->class_dev->kobj,&cip->grp);
607 if (ret) {
608 printk(KERN_WARNING "%s: sysfs_create_group error: %d\n",
609 __FUNCTION__, ret);
610 return;
612 cip->created_ok = !0;
615 #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
616 static ssize_t debuginfo_show(struct device *, struct device_attribute *,
617 char *);
618 static ssize_t debugcmd_show(struct device *, struct device_attribute *,
619 char *);
620 static ssize_t debugcmd_store(struct device *, struct device_attribute *,
621 const char *, size_t count);
623 static void pvr2_sysfs_add_debugifc(struct pvr2_sysfs *sfp)
625 struct pvr2_sysfs_debugifc *dip;
626 int ret;
628 dip = kzalloc(sizeof(*dip),GFP_KERNEL);
629 if (!dip) return;
630 dip->attr_debugcmd.attr.name = "debugcmd";
631 dip->attr_debugcmd.attr.mode = S_IRUGO|S_IWUSR|S_IWGRP;
632 dip->attr_debugcmd.show = debugcmd_show;
633 dip->attr_debugcmd.store = debugcmd_store;
634 dip->attr_debuginfo.attr.name = "debuginfo";
635 dip->attr_debuginfo.attr.mode = S_IRUGO;
636 dip->attr_debuginfo.show = debuginfo_show;
637 sfp->debugifc = dip;
638 ret = device_create_file(sfp->class_dev,&dip->attr_debugcmd);
639 if (ret < 0) {
640 printk(KERN_WARNING "%s: device_create_file error: %d\n",
641 __FUNCTION__, ret);
642 } else {
643 dip->debugcmd_created_ok = !0;
645 ret = device_create_file(sfp->class_dev,&dip->attr_debuginfo);
646 if (ret < 0) {
647 printk(KERN_WARNING "%s: device_create_file error: %d\n",
648 __FUNCTION__, ret);
649 } else {
650 dip->debuginfo_created_ok = !0;
655 static void pvr2_sysfs_tear_down_debugifc(struct pvr2_sysfs *sfp)
657 if (!sfp->debugifc) return;
658 if (sfp->debugifc->debuginfo_created_ok) {
659 device_remove_file(sfp->class_dev,
660 &sfp->debugifc->attr_debuginfo);
662 if (sfp->debugifc->debugcmd_created_ok) {
663 device_remove_file(sfp->class_dev,
664 &sfp->debugifc->attr_debugcmd);
666 kfree(sfp->debugifc);
667 sfp->debugifc = NULL;
669 #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
672 static void pvr2_sysfs_add_controls(struct pvr2_sysfs *sfp)
674 unsigned int idx,cnt;
675 cnt = pvr2_hdw_get_ctrl_count(sfp->channel.hdw);
676 for (idx = 0; idx < cnt; idx++) {
677 pvr2_sysfs_add_control(sfp,idx);
682 static void pvr2_sysfs_tear_down_controls(struct pvr2_sysfs *sfp)
684 struct pvr2_sysfs_ctl_item *cip1,*cip2;
685 for (cip1 = sfp->item_first; cip1; cip1 = cip2) {
686 cip2 = cip1->item_next;
687 if (cip1->created_ok) {
688 sysfs_remove_group(&sfp->class_dev->kobj,&cip1->grp);
690 pvr2_sysfs_trace("Destroying pvr2_sysfs_ctl_item id=%p",cip1);
691 kfree(cip1);
696 static void pvr2_sysfs_class_release(struct class *class)
698 struct pvr2_sysfs_class *clp;
699 clp = container_of(class,struct pvr2_sysfs_class,class);
700 pvr2_sysfs_trace("Destroying pvr2_sysfs_class id=%p",clp);
701 kfree(clp);
705 static void pvr2_sysfs_release(struct device *class_dev)
707 pvr2_sysfs_trace("Releasing class_dev id=%p",class_dev);
708 kfree(class_dev);
712 static void class_dev_destroy(struct pvr2_sysfs *sfp)
714 if (!sfp->class_dev) return;
715 #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
716 pvr2_sysfs_tear_down_debugifc(sfp);
717 #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
718 pvr2_sysfs_tear_down_controls(sfp);
719 if (sfp->hdw_desc_created_ok) {
720 device_remove_file(sfp->class_dev,
721 &sfp->attr_hdw_desc);
723 if (sfp->hdw_name_created_ok) {
724 device_remove_file(sfp->class_dev,
725 &sfp->attr_hdw_name);
727 if (sfp->bus_info_created_ok) {
728 device_remove_file(sfp->class_dev,
729 &sfp->attr_bus_info);
731 if (sfp->v4l_minor_number_created_ok) {
732 device_remove_file(sfp->class_dev,
733 &sfp->attr_v4l_minor_number);
735 if (sfp->v4l_radio_minor_number_created_ok) {
736 device_remove_file(sfp->class_dev,
737 &sfp->attr_v4l_radio_minor_number);
739 if (sfp->unit_number_created_ok) {
740 device_remove_file(sfp->class_dev,
741 &sfp->attr_unit_number);
743 pvr2_sysfs_trace("Destroying class_dev id=%p",sfp->class_dev);
744 sfp->class_dev->driver_data = NULL;
745 device_unregister(sfp->class_dev);
746 sfp->class_dev = NULL;
750 static ssize_t v4l_minor_number_show(struct device *class_dev,
751 struct device_attribute *attr, char *buf)
753 struct pvr2_sysfs *sfp;
754 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
755 if (!sfp) return -EINVAL;
756 return scnprintf(buf,PAGE_SIZE,"%d\n",
757 pvr2_hdw_v4l_get_minor_number(sfp->channel.hdw,
758 pvr2_v4l_type_video));
762 static ssize_t bus_info_show(struct device *class_dev,
763 struct device_attribute *attr, char *buf)
765 struct pvr2_sysfs *sfp;
766 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
767 if (!sfp) return -EINVAL;
768 return scnprintf(buf,PAGE_SIZE,"%s\n",
769 pvr2_hdw_get_bus_info(sfp->channel.hdw));
773 static ssize_t hdw_name_show(struct device *class_dev,
774 struct device_attribute *attr, char *buf)
776 struct pvr2_sysfs *sfp;
777 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
778 if (!sfp) return -EINVAL;
779 return scnprintf(buf,PAGE_SIZE,"%s\n",
780 pvr2_hdw_get_type(sfp->channel.hdw));
784 static ssize_t hdw_desc_show(struct device *class_dev,
785 struct device_attribute *attr, char *buf)
787 struct pvr2_sysfs *sfp;
788 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
789 if (!sfp) return -EINVAL;
790 return scnprintf(buf,PAGE_SIZE,"%s\n",
791 pvr2_hdw_get_desc(sfp->channel.hdw));
795 static ssize_t v4l_radio_minor_number_show(struct device *class_dev,
796 struct device_attribute *attr,
797 char *buf)
799 struct pvr2_sysfs *sfp;
800 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
801 if (!sfp) return -EINVAL;
802 return scnprintf(buf,PAGE_SIZE,"%d\n",
803 pvr2_hdw_v4l_get_minor_number(sfp->channel.hdw,
804 pvr2_v4l_type_radio));
808 static ssize_t unit_number_show(struct device *class_dev,
809 struct device_attribute *attr, char *buf)
811 struct pvr2_sysfs *sfp;
812 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
813 if (!sfp) return -EINVAL;
814 return scnprintf(buf,PAGE_SIZE,"%d\n",
815 pvr2_hdw_get_unit_number(sfp->channel.hdw));
819 static void class_dev_create(struct pvr2_sysfs *sfp,
820 struct pvr2_sysfs_class *class_ptr)
822 struct usb_device *usb_dev;
823 struct device *class_dev;
824 int ret;
826 usb_dev = pvr2_hdw_get_dev(sfp->channel.hdw);
827 if (!usb_dev) return;
828 class_dev = kzalloc(sizeof(*class_dev),GFP_KERNEL);
829 if (!class_dev) return;
831 pvr2_sysfs_trace("Creating class_dev id=%p",class_dev);
833 class_dev->class = &class_ptr->class;
834 if (pvr2_hdw_get_sn(sfp->channel.hdw)) {
835 snprintf(class_dev->bus_id, BUS_ID_SIZE, "sn-%lu",
836 pvr2_hdw_get_sn(sfp->channel.hdw));
837 } else if (pvr2_hdw_get_unit_number(sfp->channel.hdw) >= 0) {
838 snprintf(class_dev->bus_id, BUS_ID_SIZE, "unit-%c",
839 pvr2_hdw_get_unit_number(sfp->channel.hdw) + 'a');
840 } else {
841 kfree(class_dev);
842 return;
845 class_dev->parent = &usb_dev->dev;
847 sfp->class_dev = class_dev;
848 class_dev->driver_data = sfp;
849 ret = device_register(class_dev);
850 if (ret) {
851 printk(KERN_ERR "%s: device_register failed\n",
852 __FUNCTION__);
853 kfree(class_dev);
854 return;
857 sfp->attr_v4l_minor_number.attr.name = "v4l_minor_number";
858 sfp->attr_v4l_minor_number.attr.mode = S_IRUGO;
859 sfp->attr_v4l_minor_number.show = v4l_minor_number_show;
860 sfp->attr_v4l_minor_number.store = NULL;
861 ret = device_create_file(sfp->class_dev,
862 &sfp->attr_v4l_minor_number);
863 if (ret < 0) {
864 printk(KERN_WARNING "%s: device_create_file error: %d\n",
865 __FUNCTION__, ret);
866 } else {
867 sfp->v4l_minor_number_created_ok = !0;
870 sfp->attr_v4l_radio_minor_number.attr.name = "v4l_radio_minor_number";
871 sfp->attr_v4l_radio_minor_number.attr.mode = S_IRUGO;
872 sfp->attr_v4l_radio_minor_number.show = v4l_radio_minor_number_show;
873 sfp->attr_v4l_radio_minor_number.store = NULL;
874 ret = device_create_file(sfp->class_dev,
875 &sfp->attr_v4l_radio_minor_number);
876 if (ret < 0) {
877 printk(KERN_WARNING "%s: device_create_file error: %d\n",
878 __FUNCTION__, ret);
879 } else {
880 sfp->v4l_radio_minor_number_created_ok = !0;
883 sfp->attr_unit_number.attr.name = "unit_number";
884 sfp->attr_unit_number.attr.mode = S_IRUGO;
885 sfp->attr_unit_number.show = unit_number_show;
886 sfp->attr_unit_number.store = NULL;
887 ret = device_create_file(sfp->class_dev,&sfp->attr_unit_number);
888 if (ret < 0) {
889 printk(KERN_WARNING "%s: device_create_file error: %d\n",
890 __FUNCTION__, ret);
891 } else {
892 sfp->unit_number_created_ok = !0;
895 sfp->attr_bus_info.attr.name = "bus_info_str";
896 sfp->attr_bus_info.attr.mode = S_IRUGO;
897 sfp->attr_bus_info.show = bus_info_show;
898 sfp->attr_bus_info.store = NULL;
899 ret = device_create_file(sfp->class_dev,
900 &sfp->attr_bus_info);
901 if (ret < 0) {
902 printk(KERN_WARNING "%s: device_create_file error: %d\n",
903 __FUNCTION__, ret);
904 } else {
905 sfp->bus_info_created_ok = !0;
908 sfp->attr_hdw_name.attr.name = "device_hardware_type";
909 sfp->attr_hdw_name.attr.mode = S_IRUGO;
910 sfp->attr_hdw_name.show = hdw_name_show;
911 sfp->attr_hdw_name.store = NULL;
912 ret = device_create_file(sfp->class_dev,
913 &sfp->attr_hdw_name);
914 if (ret < 0) {
915 printk(KERN_WARNING "%s: device_create_file error: %d\n",
916 __FUNCTION__, ret);
917 } else {
918 sfp->hdw_name_created_ok = !0;
921 sfp->attr_hdw_desc.attr.name = "device_hardware_description";
922 sfp->attr_hdw_desc.attr.mode = S_IRUGO;
923 sfp->attr_hdw_desc.show = hdw_desc_show;
924 sfp->attr_hdw_desc.store = NULL;
925 ret = device_create_file(sfp->class_dev,
926 &sfp->attr_hdw_desc);
927 if (ret < 0) {
928 printk(KERN_WARNING "%s: device_create_file error: %d\n",
929 __FUNCTION__, ret);
930 } else {
931 sfp->hdw_desc_created_ok = !0;
934 pvr2_sysfs_add_controls(sfp);
935 #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
936 pvr2_sysfs_add_debugifc(sfp);
937 #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
941 static void pvr2_sysfs_internal_check(struct pvr2_channel *chp)
943 struct pvr2_sysfs *sfp;
944 sfp = container_of(chp,struct pvr2_sysfs,channel);
945 if (!sfp->channel.mc_head->disconnect_flag) return;
946 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_sysfs id=%p",sfp);
947 class_dev_destroy(sfp);
948 pvr2_channel_done(&sfp->channel);
949 kfree(sfp);
953 struct pvr2_sysfs *pvr2_sysfs_create(struct pvr2_context *mp,
954 struct pvr2_sysfs_class *class_ptr)
956 struct pvr2_sysfs *sfp;
957 sfp = kzalloc(sizeof(*sfp),GFP_KERNEL);
958 if (!sfp) return sfp;
959 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_sysfs id=%p",sfp);
960 pvr2_channel_init(&sfp->channel,mp);
961 sfp->channel.check_func = pvr2_sysfs_internal_check;
963 class_dev_create(sfp,class_ptr);
964 return sfp;
969 struct pvr2_sysfs_class *pvr2_sysfs_class_create(void)
971 struct pvr2_sysfs_class *clp;
972 clp = kzalloc(sizeof(*clp),GFP_KERNEL);
973 if (!clp) return clp;
974 pvr2_sysfs_trace("Creating pvr2_sysfs_class id=%p",clp);
975 clp->class.name = "pvrusb2";
976 clp->class.class_release = pvr2_sysfs_class_release;
977 clp->class.dev_release = pvr2_sysfs_release;
978 if (class_register(&clp->class)) {
979 pvr2_sysfs_trace(
980 "Registration failed for pvr2_sysfs_class id=%p",clp);
981 kfree(clp);
982 clp = NULL;
984 return clp;
988 void pvr2_sysfs_class_destroy(struct pvr2_sysfs_class *clp)
990 class_unregister(&clp->class);
994 #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
995 static ssize_t debuginfo_show(struct device *class_dev,
996 struct device_attribute *attr, char *buf)
998 struct pvr2_sysfs *sfp;
999 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
1000 if (!sfp) return -EINVAL;
1001 pvr2_hdw_trigger_module_log(sfp->channel.hdw);
1002 return pvr2_debugifc_print_info(sfp->channel.hdw,buf,PAGE_SIZE);
1006 static ssize_t debugcmd_show(struct device *class_dev,
1007 struct device_attribute *attr, char *buf)
1009 struct pvr2_sysfs *sfp;
1010 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
1011 if (!sfp) return -EINVAL;
1012 return pvr2_debugifc_print_status(sfp->channel.hdw,buf,PAGE_SIZE);
1016 static ssize_t debugcmd_store(struct device *class_dev,
1017 struct device_attribute *attr,
1018 const char *buf, size_t count)
1020 struct pvr2_sysfs *sfp;
1021 int ret;
1023 sfp = (struct pvr2_sysfs *)class_dev->driver_data;
1024 if (!sfp) return -EINVAL;
1026 ret = pvr2_debugifc_docmd(sfp->channel.hdw,buf,count);
1027 if (ret < 0) return ret;
1028 return count;
1030 #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
1034 Stuff for Emacs to see, in order to encourage consistent editing style:
1035 *** Local Variables: ***
1036 *** mode: c ***
1037 *** fill-column: 75 ***
1038 *** tab-width: 8 ***
1039 *** c-basic-offset: 8 ***
1040 *** End: ***