blob: a741c556a39a06119619f0ff7cea0fd7127d71ad [file] [log] [blame]
Mike Iselyd8554972006-06-26 20:58:46 -03001/*
2 *
3 * $Id$
4 *
5 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Mike Iselyd8554972006-06-26 20:58:46 -030022#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 */
31
32#define pvr2_sysfs_trace(...) pvr2_trace(PVR2_TRACE_SYSFS,__VA_ARGS__)
33
34struct pvr2_sysfs {
35 struct pvr2_channel channel;
36 struct class_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;
Mike Iselyd8554972006-06-26 20:58:46 -030042 struct class_device_attribute attr_v4l_minor_number;
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -030043 struct class_device_attribute attr_v4l_radio_minor_number;
Mike Iselyd8554972006-06-26 20:58:46 -030044 struct class_device_attribute attr_unit_number;
Mike Isely31a18542007-04-08 01:11:47 -030045 struct class_device_attribute attr_bus_info;
Mike Isely08d41802006-07-22 21:26:30 -030046 int v4l_minor_number_created_ok;
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -030047 int v4l_radio_minor_number_created_ok;
Mike Isely08d41802006-07-22 21:26:30 -030048 int unit_number_created_ok;
Mike Isely31a18542007-04-08 01:11:47 -030049 int bus_info_created_ok;
Mike Iselyd8554972006-06-26 20:58:46 -030050};
51
52#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
53struct pvr2_sysfs_debugifc {
54 struct class_device_attribute attr_debugcmd;
55 struct class_device_attribute attr_debuginfo;
Mike Isely08d41802006-07-22 21:26:30 -030056 int debugcmd_created_ok;
57 int debuginfo_created_ok;
Mike Iselyd8554972006-06-26 20:58:46 -030058};
59#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
60
61struct pvr2_sysfs_ctl_item {
62 struct class_device_attribute attr_name;
Mike Isely33213962006-06-25 20:04:40 -030063 struct class_device_attribute attr_type;
Mike Iselyd8554972006-06-26 20:58:46 -030064 struct class_device_attribute attr_min;
65 struct class_device_attribute attr_max;
66 struct class_device_attribute attr_enum;
67 struct class_device_attribute attr_bits;
68 struct class_device_attribute attr_val;
69 struct class_device_attribute attr_custom;
70 struct pvr2_ctrl *cptr;
71 struct pvr2_sysfs *chptr;
72 struct pvr2_sysfs_ctl_item *item_next;
Mike Isely33213962006-06-25 20:04:40 -030073 struct attribute *attr_gen[7];
Mike Iselyd8554972006-06-26 20:58:46 -030074 struct attribute_group grp;
Mike Isely08d41802006-07-22 21:26:30 -030075 int created_ok;
Mike Iselyd8554972006-06-26 20:58:46 -030076 char name[80];
77};
78
79struct pvr2_sysfs_class {
80 struct class class;
81};
82
83static ssize_t show_name(int id,struct class_device *class_dev,char *buf)
84{
85 struct pvr2_ctrl *cptr;
86 struct pvr2_sysfs *sfp;
87 const char *name;
88
89 sfp = (struct pvr2_sysfs *)class_dev->class_data;
90 if (!sfp) return -EINVAL;
91 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
92 if (!cptr) return -EINVAL;
93
94 name = pvr2_ctrl_get_desc(cptr);
95 pvr2_sysfs_trace("pvr2_sysfs(%p) show_name(cid=%d) is %s",sfp,id,name);
96
97 if (!name) return -EINVAL;
98
99 return scnprintf(buf,PAGE_SIZE,"%s\n",name);
100}
101
Mike Isely33213962006-06-25 20:04:40 -0300102static ssize_t show_type(int id,struct class_device *class_dev,char *buf)
103{
104 struct pvr2_ctrl *cptr;
105 struct pvr2_sysfs *sfp;
106 const char *name;
107 enum pvr2_ctl_type tp;
108
109 sfp = (struct pvr2_sysfs *)class_dev->class_data;
110 if (!sfp) return -EINVAL;
111 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
112 if (!cptr) return -EINVAL;
113
114 tp = pvr2_ctrl_get_type(cptr);
115 switch (tp) {
116 case pvr2_ctl_int: name = "integer"; break;
117 case pvr2_ctl_enum: name = "enum"; break;
118 case pvr2_ctl_bitmask: name = "bitmask"; break;
119 case pvr2_ctl_bool: name = "boolean"; break;
120 default: name = "?"; break;
121 }
122 pvr2_sysfs_trace("pvr2_sysfs(%p) show_type(cid=%d) is %s",sfp,id,name);
123
124 if (!name) return -EINVAL;
125
126 return scnprintf(buf,PAGE_SIZE,"%s\n",name);
127}
128
Mike Iselyd8554972006-06-26 20:58:46 -0300129static ssize_t show_min(int id,struct class_device *class_dev,char *buf)
130{
131 struct pvr2_ctrl *cptr;
132 struct pvr2_sysfs *sfp;
133 long val;
134
135 sfp = (struct pvr2_sysfs *)class_dev->class_data;
136 if (!sfp) return -EINVAL;
137 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
138 if (!cptr) return -EINVAL;
139 val = pvr2_ctrl_get_min(cptr);
140
141 pvr2_sysfs_trace("pvr2_sysfs(%p) show_min(cid=%d) is %ld",sfp,id,val);
142
143 return scnprintf(buf,PAGE_SIZE,"%ld\n",val);
144}
145
146static ssize_t show_max(int id,struct class_device *class_dev,char *buf)
147{
148 struct pvr2_ctrl *cptr;
149 struct pvr2_sysfs *sfp;
150 long val;
151
152 sfp = (struct pvr2_sysfs *)class_dev->class_data;
153 if (!sfp) return -EINVAL;
154 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
155 if (!cptr) return -EINVAL;
156 val = pvr2_ctrl_get_max(cptr);
157
158 pvr2_sysfs_trace("pvr2_sysfs(%p) show_max(cid=%d) is %ld",sfp,id,val);
159
160 return scnprintf(buf,PAGE_SIZE,"%ld\n",val);
161}
162
163static ssize_t show_val_norm(int id,struct class_device *class_dev,char *buf)
164{
165 struct pvr2_ctrl *cptr;
166 struct pvr2_sysfs *sfp;
167 int val,ret;
168 unsigned int cnt = 0;
169
170 sfp = (struct pvr2_sysfs *)class_dev->class_data;
171 if (!sfp) return -EINVAL;
172 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
173 if (!cptr) return -EINVAL;
174
175 ret = pvr2_ctrl_get_value(cptr,&val);
176 if (ret < 0) return ret;
177
178 ret = pvr2_ctrl_value_to_sym(cptr,~0,val,
179 buf,PAGE_SIZE-1,&cnt);
180
181 pvr2_sysfs_trace("pvr2_sysfs(%p) show_val_norm(cid=%d) is %.*s (%d)",
182 sfp,id,cnt,buf,val);
183 buf[cnt] = '\n';
184 return cnt+1;
185}
186
187static ssize_t show_val_custom(int id,struct class_device *class_dev,char *buf)
188{
189 struct pvr2_ctrl *cptr;
190 struct pvr2_sysfs *sfp;
191 int val,ret;
192 unsigned int cnt = 0;
193
194 sfp = (struct pvr2_sysfs *)class_dev->class_data;
195 if (!sfp) return -EINVAL;
196 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
197 if (!cptr) return -EINVAL;
198
199 ret = pvr2_ctrl_get_value(cptr,&val);
200 if (ret < 0) return ret;
201
202 ret = pvr2_ctrl_custom_value_to_sym(cptr,~0,val,
203 buf,PAGE_SIZE-1,&cnt);
204
205 pvr2_sysfs_trace("pvr2_sysfs(%p) show_val_custom(cid=%d) is %.*s (%d)",
206 sfp,id,cnt,buf,val);
207 buf[cnt] = '\n';
208 return cnt+1;
209}
210
211static ssize_t show_enum(int id,struct class_device *class_dev,char *buf)
212{
213 struct pvr2_ctrl *cptr;
214 struct pvr2_sysfs *sfp;
215 long val;
216 unsigned int bcnt,ccnt,ecnt;
217
218 sfp = (struct pvr2_sysfs *)class_dev->class_data;
219 if (!sfp) return -EINVAL;
220 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
221 if (!cptr) return -EINVAL;
222 ecnt = pvr2_ctrl_get_cnt(cptr);
223 bcnt = 0;
224 for (val = 0; val < ecnt; val++) {
225 pvr2_ctrl_get_valname(cptr,val,buf+bcnt,PAGE_SIZE-bcnt,&ccnt);
Mike Isely45886772006-06-25 20:04:13 -0300226 if (!ccnt) continue;
Mike Iselyd8554972006-06-26 20:58:46 -0300227 bcnt += ccnt;
228 if (bcnt >= PAGE_SIZE) break;
229 buf[bcnt] = '\n';
230 bcnt++;
231 }
232 pvr2_sysfs_trace("pvr2_sysfs(%p) show_enum(cid=%d)",sfp,id);
233 return bcnt;
234}
235
236static ssize_t show_bits(int id,struct class_device *class_dev,char *buf)
237{
238 struct pvr2_ctrl *cptr;
239 struct pvr2_sysfs *sfp;
240 int valid_bits,msk;
241 unsigned int bcnt,ccnt;
242
243 sfp = (struct pvr2_sysfs *)class_dev->class_data;
244 if (!sfp) return -EINVAL;
245 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
246 if (!cptr) return -EINVAL;
247 valid_bits = pvr2_ctrl_get_mask(cptr);
248 bcnt = 0;
249 for (msk = 1; valid_bits; msk <<= 1) {
250 if (!(msk & valid_bits)) continue;
251 valid_bits &= ~msk;
252 pvr2_ctrl_get_valname(cptr,msk,buf+bcnt,PAGE_SIZE-bcnt,&ccnt);
253 bcnt += ccnt;
254 if (bcnt >= PAGE_SIZE) break;
255 buf[bcnt] = '\n';
256 bcnt++;
257 }
258 pvr2_sysfs_trace("pvr2_sysfs(%p) show_bits(cid=%d)",sfp,id);
259 return bcnt;
260}
261
262static int store_val_any(int id,int customfl,struct pvr2_sysfs *sfp,
263 const char *buf,unsigned int count)
264{
265 struct pvr2_ctrl *cptr;
266 int ret;
267 int mask,val;
268
269 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
270 if (customfl) {
271 ret = pvr2_ctrl_custom_sym_to_value(cptr,buf,count,&mask,&val);
272 } else {
273 ret = pvr2_ctrl_sym_to_value(cptr,buf,count,&mask,&val);
274 }
275 if (ret < 0) return ret;
276 ret = pvr2_ctrl_set_mask_value(cptr,mask,val);
277 pvr2_hdw_commit_ctl(sfp->channel.hdw);
278 return ret;
279}
280
281static ssize_t store_val_norm(int id,struct class_device *class_dev,
282 const char *buf,size_t count)
283{
284 struct pvr2_sysfs *sfp;
285 int ret;
286 sfp = (struct pvr2_sysfs *)class_dev->class_data;
287 ret = store_val_any(id,0,sfp,buf,count);
288 if (!ret) ret = count;
289 return ret;
290}
291
292static ssize_t store_val_custom(int id,struct class_device *class_dev,
293 const char *buf,size_t count)
294{
295 struct pvr2_sysfs *sfp;
296 int ret;
297 sfp = (struct pvr2_sysfs *)class_dev->class_data;
298 ret = store_val_any(id,1,sfp,buf,count);
299 if (!ret) ret = count;
300 return ret;
301}
302
303/*
304 Mike Isely <isely@pobox.com> 30-April-2005
305
306 This next batch of horrible preprocessor hackery is needed because the
307 kernel's class_device_attribute mechanism fails to pass the actual
308 attribute through to the show / store functions, which means we have no
309 way to package up any attribute-specific parameters, like for example the
310 control id. So we work around this brain-damage by encoding the control
311 id into the show / store functions themselves and pick the function based
312 on the control id we're setting up. These macros try to ease the pain.
313 Yuck.
314*/
315
316#define CREATE_SHOW_INSTANCE(sf_name,ctl_id) \
317static ssize_t sf_name##_##ctl_id(struct class_device *class_dev,char *buf) \
318{ return sf_name(ctl_id,class_dev,buf); }
319
320#define CREATE_STORE_INSTANCE(sf_name,ctl_id) \
321static ssize_t sf_name##_##ctl_id(struct class_device *class_dev,const char *buf,size_t count) \
322{ return sf_name(ctl_id,class_dev,buf,count); }
323
324#define CREATE_BATCH(ctl_id) \
325CREATE_SHOW_INSTANCE(show_name,ctl_id) \
Mike Isely33213962006-06-25 20:04:40 -0300326CREATE_SHOW_INSTANCE(show_type,ctl_id) \
Mike Iselyd8554972006-06-26 20:58:46 -0300327CREATE_SHOW_INSTANCE(show_min,ctl_id) \
328CREATE_SHOW_INSTANCE(show_max,ctl_id) \
329CREATE_SHOW_INSTANCE(show_val_norm,ctl_id) \
330CREATE_SHOW_INSTANCE(show_val_custom,ctl_id) \
331CREATE_SHOW_INSTANCE(show_enum,ctl_id) \
332CREATE_SHOW_INSTANCE(show_bits,ctl_id) \
333CREATE_STORE_INSTANCE(store_val_norm,ctl_id) \
334CREATE_STORE_INSTANCE(store_val_custom,ctl_id) \
335
336CREATE_BATCH(0)
337CREATE_BATCH(1)
338CREATE_BATCH(2)
339CREATE_BATCH(3)
340CREATE_BATCH(4)
341CREATE_BATCH(5)
342CREATE_BATCH(6)
343CREATE_BATCH(7)
344CREATE_BATCH(8)
345CREATE_BATCH(9)
346CREATE_BATCH(10)
347CREATE_BATCH(11)
348CREATE_BATCH(12)
349CREATE_BATCH(13)
350CREATE_BATCH(14)
351CREATE_BATCH(15)
352CREATE_BATCH(16)
353CREATE_BATCH(17)
354CREATE_BATCH(18)
355CREATE_BATCH(19)
356CREATE_BATCH(20)
357CREATE_BATCH(21)
358CREATE_BATCH(22)
359CREATE_BATCH(23)
360CREATE_BATCH(24)
361CREATE_BATCH(25)
362CREATE_BATCH(26)
363CREATE_BATCH(27)
364CREATE_BATCH(28)
365CREATE_BATCH(29)
366CREATE_BATCH(30)
367CREATE_BATCH(31)
368CREATE_BATCH(32)
369CREATE_BATCH(33)
Mike Isely39481992006-06-25 20:04:20 -0300370CREATE_BATCH(34)
371CREATE_BATCH(35)
372CREATE_BATCH(36)
373CREATE_BATCH(37)
374CREATE_BATCH(38)
375CREATE_BATCH(39)
376CREATE_BATCH(40)
377CREATE_BATCH(41)
378CREATE_BATCH(42)
379CREATE_BATCH(43)
380CREATE_BATCH(44)
381CREATE_BATCH(45)
382CREATE_BATCH(46)
383CREATE_BATCH(47)
384CREATE_BATCH(48)
385CREATE_BATCH(49)
386CREATE_BATCH(50)
387CREATE_BATCH(51)
388CREATE_BATCH(52)
389CREATE_BATCH(53)
390CREATE_BATCH(54)
391CREATE_BATCH(55)
392CREATE_BATCH(56)
393CREATE_BATCH(57)
394CREATE_BATCH(58)
395CREATE_BATCH(59)
Mike Iselyd8554972006-06-26 20:58:46 -0300396
397struct pvr2_sysfs_func_set {
398 ssize_t (*show_name)(struct class_device *,char *);
Mike Isely33213962006-06-25 20:04:40 -0300399 ssize_t (*show_type)(struct class_device *,char *);
Mike Iselyd8554972006-06-26 20:58:46 -0300400 ssize_t (*show_min)(struct class_device *,char *);
401 ssize_t (*show_max)(struct class_device *,char *);
402 ssize_t (*show_enum)(struct class_device *,char *);
403 ssize_t (*show_bits)(struct class_device *,char *);
404 ssize_t (*show_val_norm)(struct class_device *,char *);
405 ssize_t (*store_val_norm)(struct class_device *,
406 const char *,size_t);
407 ssize_t (*show_val_custom)(struct class_device *,char *);
408 ssize_t (*store_val_custom)(struct class_device *,
409 const char *,size_t);
410};
411
412#define INIT_BATCH(ctl_id) \
413[ctl_id] = { \
414 .show_name = show_name_##ctl_id, \
Mike Isely33213962006-06-25 20:04:40 -0300415 .show_type = show_type_##ctl_id, \
Mike Iselyd8554972006-06-26 20:58:46 -0300416 .show_min = show_min_##ctl_id, \
417 .show_max = show_max_##ctl_id, \
418 .show_enum = show_enum_##ctl_id, \
419 .show_bits = show_bits_##ctl_id, \
420 .show_val_norm = show_val_norm_##ctl_id, \
421 .store_val_norm = store_val_norm_##ctl_id, \
422 .show_val_custom = show_val_custom_##ctl_id, \
423 .store_val_custom = store_val_custom_##ctl_id, \
424} \
425
426static struct pvr2_sysfs_func_set funcs[] = {
427 INIT_BATCH(0),
428 INIT_BATCH(1),
429 INIT_BATCH(2),
430 INIT_BATCH(3),
431 INIT_BATCH(4),
432 INIT_BATCH(5),
433 INIT_BATCH(6),
434 INIT_BATCH(7),
435 INIT_BATCH(8),
436 INIT_BATCH(9),
437 INIT_BATCH(10),
438 INIT_BATCH(11),
439 INIT_BATCH(12),
440 INIT_BATCH(13),
441 INIT_BATCH(14),
442 INIT_BATCH(15),
443 INIT_BATCH(16),
444 INIT_BATCH(17),
445 INIT_BATCH(18),
446 INIT_BATCH(19),
447 INIT_BATCH(20),
448 INIT_BATCH(21),
449 INIT_BATCH(22),
450 INIT_BATCH(23),
451 INIT_BATCH(24),
452 INIT_BATCH(25),
453 INIT_BATCH(26),
454 INIT_BATCH(27),
455 INIT_BATCH(28),
456 INIT_BATCH(29),
457 INIT_BATCH(30),
458 INIT_BATCH(31),
459 INIT_BATCH(32),
460 INIT_BATCH(33),
Mike Isely39481992006-06-25 20:04:20 -0300461 INIT_BATCH(34),
462 INIT_BATCH(35),
463 INIT_BATCH(36),
464 INIT_BATCH(37),
465 INIT_BATCH(38),
466 INIT_BATCH(39),
467 INIT_BATCH(40),
468 INIT_BATCH(41),
469 INIT_BATCH(42),
470 INIT_BATCH(43),
471 INIT_BATCH(44),
472 INIT_BATCH(45),
473 INIT_BATCH(46),
474 INIT_BATCH(47),
475 INIT_BATCH(48),
476 INIT_BATCH(49),
477 INIT_BATCH(50),
478 INIT_BATCH(51),
479 INIT_BATCH(52),
480 INIT_BATCH(53),
481 INIT_BATCH(54),
482 INIT_BATCH(55),
483 INIT_BATCH(56),
484 INIT_BATCH(57),
485 INIT_BATCH(58),
486 INIT_BATCH(59),
Mike Iselyd8554972006-06-26 20:58:46 -0300487};
488
489
490static void pvr2_sysfs_add_control(struct pvr2_sysfs *sfp,int ctl_id)
491{
492 struct pvr2_sysfs_ctl_item *cip;
493 struct pvr2_sysfs_func_set *fp;
494 struct pvr2_ctrl *cptr;
495 unsigned int cnt,acnt;
Mike Isely08d41802006-07-22 21:26:30 -0300496 int ret;
Mike Iselyd8554972006-06-26 20:58:46 -0300497
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300498 if ((ctl_id < 0) || (ctl_id >= ARRAY_SIZE(funcs))) {
Mike Iselyd8554972006-06-26 20:58:46 -0300499 return;
500 }
501
502 fp = funcs + ctl_id;
503 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,ctl_id);
504 if (!cptr) return;
505
Mike Iselyca545f72007-01-20 00:37:11 -0300506 cip = kzalloc(sizeof(*cip),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -0300507 if (!cip) return;
Mike Iselyd8554972006-06-26 20:58:46 -0300508 pvr2_sysfs_trace("Creating pvr2_sysfs_ctl_item id=%p",cip);
509
510 cip->cptr = cptr;
511
512 cip->chptr = sfp;
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300513 cip->item_next = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300514 if (sfp->item_last) {
515 sfp->item_last->item_next = cip;
516 } else {
517 sfp->item_first = cip;
518 }
519 sfp->item_last = cip;
520
521 cip->attr_name.attr.owner = THIS_MODULE;
522 cip->attr_name.attr.name = "name";
523 cip->attr_name.attr.mode = S_IRUGO;
524 cip->attr_name.show = fp->show_name;
525
Mike Isely33213962006-06-25 20:04:40 -0300526 cip->attr_type.attr.owner = THIS_MODULE;
527 cip->attr_type.attr.name = "type";
528 cip->attr_type.attr.mode = S_IRUGO;
529 cip->attr_type.show = fp->show_type;
530
Mike Iselyd8554972006-06-26 20:58:46 -0300531 cip->attr_min.attr.owner = THIS_MODULE;
532 cip->attr_min.attr.name = "min_val";
533 cip->attr_min.attr.mode = S_IRUGO;
534 cip->attr_min.show = fp->show_min;
535
536 cip->attr_max.attr.owner = THIS_MODULE;
537 cip->attr_max.attr.name = "max_val";
538 cip->attr_max.attr.mode = S_IRUGO;
539 cip->attr_max.show = fp->show_max;
540
541 cip->attr_val.attr.owner = THIS_MODULE;
542 cip->attr_val.attr.name = "cur_val";
543 cip->attr_val.attr.mode = S_IRUGO;
544
545 cip->attr_custom.attr.owner = THIS_MODULE;
546 cip->attr_custom.attr.name = "custom_val";
547 cip->attr_custom.attr.mode = S_IRUGO;
548
549 cip->attr_enum.attr.owner = THIS_MODULE;
550 cip->attr_enum.attr.name = "enum_val";
551 cip->attr_enum.attr.mode = S_IRUGO;
552 cip->attr_enum.show = fp->show_enum;
553
554 cip->attr_bits.attr.owner = THIS_MODULE;
555 cip->attr_bits.attr.name = "bit_val";
556 cip->attr_bits.attr.mode = S_IRUGO;
557 cip->attr_bits.show = fp->show_bits;
558
559 if (pvr2_ctrl_is_writable(cptr)) {
560 cip->attr_val.attr.mode |= S_IWUSR|S_IWGRP;
561 cip->attr_custom.attr.mode |= S_IWUSR|S_IWGRP;
562 }
563
564 acnt = 0;
565 cip->attr_gen[acnt++] = &cip->attr_name.attr;
Mike Isely33213962006-06-25 20:04:40 -0300566 cip->attr_gen[acnt++] = &cip->attr_type.attr;
Mike Iselyd8554972006-06-26 20:58:46 -0300567 cip->attr_gen[acnt++] = &cip->attr_val.attr;
568 cip->attr_val.show = fp->show_val_norm;
569 cip->attr_val.store = fp->store_val_norm;
570 if (pvr2_ctrl_has_custom_symbols(cptr)) {
571 cip->attr_gen[acnt++] = &cip->attr_custom.attr;
572 cip->attr_custom.show = fp->show_val_custom;
573 cip->attr_custom.store = fp->store_val_custom;
574 }
575 switch (pvr2_ctrl_get_type(cptr)) {
576 case pvr2_ctl_enum:
577 // Control is an enumeration
578 cip->attr_gen[acnt++] = &cip->attr_enum.attr;
579 break;
580 case pvr2_ctl_int:
581 // Control is an integer
582 cip->attr_gen[acnt++] = &cip->attr_min.attr;
583 cip->attr_gen[acnt++] = &cip->attr_max.attr;
584 break;
585 case pvr2_ctl_bitmask:
586 // Control is an bitmask
587 cip->attr_gen[acnt++] = &cip->attr_bits.attr;
588 break;
589 default: break;
590 }
591
592 cnt = scnprintf(cip->name,sizeof(cip->name)-1,"ctl_%s",
593 pvr2_ctrl_get_name(cptr));
594 cip->name[cnt] = 0;
595 cip->grp.name = cip->name;
596 cip->grp.attrs = cip->attr_gen;
597
Mike Isely08d41802006-07-22 21:26:30 -0300598 ret = sysfs_create_group(&sfp->class_dev->kobj,&cip->grp);
599 if (ret) {
600 printk(KERN_WARNING "%s: sysfs_create_group error: %d\n",
601 __FUNCTION__, ret);
602 return;
603 }
604 cip->created_ok = !0;
Mike Iselyd8554972006-06-26 20:58:46 -0300605}
606
607#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
608static ssize_t debuginfo_show(struct class_device *,char *);
609static ssize_t debugcmd_show(struct class_device *,char *);
610static ssize_t debugcmd_store(struct class_device *,const char *,size_t count);
611
612static void pvr2_sysfs_add_debugifc(struct pvr2_sysfs *sfp)
613{
614 struct pvr2_sysfs_debugifc *dip;
Michael Krufky3117bee2006-07-19 13:23:38 -0300615 int ret;
616
Mike Iselyca545f72007-01-20 00:37:11 -0300617 dip = kzalloc(sizeof(*dip),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -0300618 if (!dip) return;
Mike Iselyd8554972006-06-26 20:58:46 -0300619 dip->attr_debugcmd.attr.owner = THIS_MODULE;
620 dip->attr_debugcmd.attr.name = "debugcmd";
621 dip->attr_debugcmd.attr.mode = S_IRUGO|S_IWUSR|S_IWGRP;
622 dip->attr_debugcmd.show = debugcmd_show;
623 dip->attr_debugcmd.store = debugcmd_store;
624 dip->attr_debuginfo.attr.owner = THIS_MODULE;
625 dip->attr_debuginfo.attr.name = "debuginfo";
626 dip->attr_debuginfo.attr.mode = S_IRUGO;
627 dip->attr_debuginfo.show = debuginfo_show;
628 sfp->debugifc = dip;
Michael Krufky3117bee2006-07-19 13:23:38 -0300629 ret = class_device_create_file(sfp->class_dev,&dip->attr_debugcmd);
Mike Isely08d41802006-07-22 21:26:30 -0300630 if (ret < 0) {
Michael Krufky3117bee2006-07-19 13:23:38 -0300631 printk(KERN_WARNING "%s: class_device_create_file error: %d\n",
632 __FUNCTION__, ret);
Mike Isely08d41802006-07-22 21:26:30 -0300633 } else {
634 dip->debugcmd_created_ok = !0;
635 }
Michael Krufky3117bee2006-07-19 13:23:38 -0300636 ret = class_device_create_file(sfp->class_dev,&dip->attr_debuginfo);
Mike Isely08d41802006-07-22 21:26:30 -0300637 if (ret < 0) {
Michael Krufky3117bee2006-07-19 13:23:38 -0300638 printk(KERN_WARNING "%s: class_device_create_file error: %d\n",
639 __FUNCTION__, ret);
Mike Isely08d41802006-07-22 21:26:30 -0300640 } else {
641 dip->debuginfo_created_ok = !0;
642 }
Mike Iselyd8554972006-06-26 20:58:46 -0300643}
644
645
646static void pvr2_sysfs_tear_down_debugifc(struct pvr2_sysfs *sfp)
647{
648 if (!sfp->debugifc) return;
Mike Isely08d41802006-07-22 21:26:30 -0300649 if (sfp->debugifc->debuginfo_created_ok) {
650 class_device_remove_file(sfp->class_dev,
651 &sfp->debugifc->attr_debuginfo);
652 }
653 if (sfp->debugifc->debugcmd_created_ok) {
654 class_device_remove_file(sfp->class_dev,
655 &sfp->debugifc->attr_debugcmd);
656 }
Mike Iselyd8554972006-06-26 20:58:46 -0300657 kfree(sfp->debugifc);
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300658 sfp->debugifc = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300659}
660#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
661
662
663static void pvr2_sysfs_add_controls(struct pvr2_sysfs *sfp)
664{
665 unsigned int idx,cnt;
666 cnt = pvr2_hdw_get_ctrl_count(sfp->channel.hdw);
667 for (idx = 0; idx < cnt; idx++) {
668 pvr2_sysfs_add_control(sfp,idx);
669 }
670}
671
672
673static void pvr2_sysfs_tear_down_controls(struct pvr2_sysfs *sfp)
674{
675 struct pvr2_sysfs_ctl_item *cip1,*cip2;
676 for (cip1 = sfp->item_first; cip1; cip1 = cip2) {
677 cip2 = cip1->item_next;
Mike Isely08d41802006-07-22 21:26:30 -0300678 if (cip1->created_ok) {
679 sysfs_remove_group(&sfp->class_dev->kobj,&cip1->grp);
680 }
Mike Iselyd8554972006-06-26 20:58:46 -0300681 pvr2_sysfs_trace("Destroying pvr2_sysfs_ctl_item id=%p",cip1);
682 kfree(cip1);
683 }
684}
685
686
687static void pvr2_sysfs_class_release(struct class *class)
688{
689 struct pvr2_sysfs_class *clp;
690 clp = container_of(class,struct pvr2_sysfs_class,class);
691 pvr2_sysfs_trace("Destroying pvr2_sysfs_class id=%p",clp);
692 kfree(clp);
693}
694
695
696static void pvr2_sysfs_release(struct class_device *class_dev)
697{
698 pvr2_sysfs_trace("Releasing class_dev id=%p",class_dev);
699 kfree(class_dev);
700}
701
702
703static void class_dev_destroy(struct pvr2_sysfs *sfp)
704{
705 if (!sfp->class_dev) return;
706#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
707 pvr2_sysfs_tear_down_debugifc(sfp);
708#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
709 pvr2_sysfs_tear_down_controls(sfp);
Mike Isely31a18542007-04-08 01:11:47 -0300710 if (sfp->bus_info_created_ok) {
711 class_device_remove_file(sfp->class_dev,
712 &sfp->attr_bus_info);
713 }
Mike Isely08d41802006-07-22 21:26:30 -0300714 if (sfp->v4l_minor_number_created_ok) {
715 class_device_remove_file(sfp->class_dev,
716 &sfp->attr_v4l_minor_number);
717 }
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -0300718 if (sfp->v4l_radio_minor_number_created_ok) {
719 class_device_remove_file(sfp->class_dev,
720 &sfp->attr_v4l_radio_minor_number);
721 }
Mike Isely08d41802006-07-22 21:26:30 -0300722 if (sfp->unit_number_created_ok) {
723 class_device_remove_file(sfp->class_dev,
724 &sfp->attr_unit_number);
725 }
Mike Iselyd8554972006-06-26 20:58:46 -0300726 pvr2_sysfs_trace("Destroying class_dev id=%p",sfp->class_dev);
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300727 sfp->class_dev->class_data = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300728 class_device_unregister(sfp->class_dev);
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300729 sfp->class_dev = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300730}
731
732
733static ssize_t v4l_minor_number_show(struct class_device *class_dev,char *buf)
734{
735 struct pvr2_sysfs *sfp;
736 sfp = (struct pvr2_sysfs *)class_dev->class_data;
737 if (!sfp) return -EINVAL;
738 return scnprintf(buf,PAGE_SIZE,"%d\n",
Mike Iselyfd5a75f2006-12-27 23:11:22 -0300739 pvr2_hdw_v4l_get_minor_number(sfp->channel.hdw,
Mike Isely80793842006-12-27 23:12:28 -0300740 pvr2_v4l_type_video));
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -0300741}
742
743
Mike Isely31a18542007-04-08 01:11:47 -0300744static ssize_t bus_info_show(struct class_device *class_dev,char *buf)
745{
746 struct pvr2_sysfs *sfp;
747 sfp = (struct pvr2_sysfs *)class_dev->class_data;
748 if (!sfp) return -EINVAL;
749 return scnprintf(buf,PAGE_SIZE,"%s\n",
750 pvr2_hdw_get_bus_info(sfp->channel.hdw));
751}
752
753
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -0300754static ssize_t v4l_radio_minor_number_show(struct class_device *class_dev,
755 char *buf)
756{
757 struct pvr2_sysfs *sfp;
758 sfp = (struct pvr2_sysfs *)class_dev->class_data;
759 if (!sfp) return -EINVAL;
760 return scnprintf(buf,PAGE_SIZE,"%d\n",
Mike Iselyfd5a75f2006-12-27 23:11:22 -0300761 pvr2_hdw_v4l_get_minor_number(sfp->channel.hdw,
Mike Isely80793842006-12-27 23:12:28 -0300762 pvr2_v4l_type_radio));
Mike Iselyd8554972006-06-26 20:58:46 -0300763}
764
765
766static ssize_t unit_number_show(struct class_device *class_dev,char *buf)
767{
768 struct pvr2_sysfs *sfp;
769 sfp = (struct pvr2_sysfs *)class_dev->class_data;
770 if (!sfp) return -EINVAL;
771 return scnprintf(buf,PAGE_SIZE,"%d\n",
772 pvr2_hdw_get_unit_number(sfp->channel.hdw));
773}
774
775
776static void class_dev_create(struct pvr2_sysfs *sfp,
777 struct pvr2_sysfs_class *class_ptr)
778{
779 struct usb_device *usb_dev;
780 struct class_device *class_dev;
Michael Krufky3117bee2006-07-19 13:23:38 -0300781 int ret;
782
Mike Iselyd8554972006-06-26 20:58:46 -0300783 usb_dev = pvr2_hdw_get_dev(sfp->channel.hdw);
784 if (!usb_dev) return;
Mike Iselyca545f72007-01-20 00:37:11 -0300785 class_dev = kzalloc(sizeof(*class_dev),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -0300786 if (!class_dev) return;
Mike Iselyd8554972006-06-26 20:58:46 -0300787
788 pvr2_sysfs_trace("Creating class_dev id=%p",class_dev);
789
790 class_dev->class = &class_ptr->class;
791 if (pvr2_hdw_get_sn(sfp->channel.hdw)) {
792 snprintf(class_dev->class_id,BUS_ID_SIZE,"sn-%lu",
793 pvr2_hdw_get_sn(sfp->channel.hdw));
794 } else if (pvr2_hdw_get_unit_number(sfp->channel.hdw) >= 0) {
795 snprintf(class_dev->class_id,BUS_ID_SIZE,"unit-%c",
796 pvr2_hdw_get_unit_number(sfp->channel.hdw) + 'a');
797 } else {
798 kfree(class_dev);
799 return;
800 }
801
802 class_dev->dev = &usb_dev->dev;
803
804 sfp->class_dev = class_dev;
805 class_dev->class_data = sfp;
Michael Krufky3117bee2006-07-19 13:23:38 -0300806 ret = class_device_register(class_dev);
807 if (ret) {
808 printk(KERN_ERR "%s: class_device_register failed\n",
809 __FUNCTION__);
810 kfree(class_dev);
811 return;
812 }
Mike Iselyd8554972006-06-26 20:58:46 -0300813
814 sfp->attr_v4l_minor_number.attr.owner = THIS_MODULE;
815 sfp->attr_v4l_minor_number.attr.name = "v4l_minor_number";
816 sfp->attr_v4l_minor_number.attr.mode = S_IRUGO;
817 sfp->attr_v4l_minor_number.show = v4l_minor_number_show;
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300818 sfp->attr_v4l_minor_number.store = NULL;
Mike Isely08d41802006-07-22 21:26:30 -0300819 ret = class_device_create_file(sfp->class_dev,
820 &sfp->attr_v4l_minor_number);
821 if (ret < 0) {
Michael Krufky3117bee2006-07-19 13:23:38 -0300822 printk(KERN_WARNING "%s: class_device_create_file error: %d\n",
823 __FUNCTION__, ret);
Mike Isely08d41802006-07-22 21:26:30 -0300824 } else {
825 sfp->v4l_minor_number_created_ok = !0;
826 }
Michael Krufky3117bee2006-07-19 13:23:38 -0300827
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -0300828 sfp->attr_v4l_radio_minor_number.attr.owner = THIS_MODULE;
829 sfp->attr_v4l_radio_minor_number.attr.name = "v4l_radio_minor_number";
830 sfp->attr_v4l_radio_minor_number.attr.mode = S_IRUGO;
831 sfp->attr_v4l_radio_minor_number.show = v4l_radio_minor_number_show;
832 sfp->attr_v4l_radio_minor_number.store = NULL;
833 ret = class_device_create_file(sfp->class_dev,
834 &sfp->attr_v4l_radio_minor_number);
835 if (ret < 0) {
836 printk(KERN_WARNING "%s: class_device_create_file error: %d\n",
837 __FUNCTION__, ret);
838 } else {
839 sfp->v4l_radio_minor_number_created_ok = !0;
840 }
841
Mike Iselyd8554972006-06-26 20:58:46 -0300842 sfp->attr_unit_number.attr.owner = THIS_MODULE;
843 sfp->attr_unit_number.attr.name = "unit_number";
844 sfp->attr_unit_number.attr.mode = S_IRUGO;
845 sfp->attr_unit_number.show = unit_number_show;
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300846 sfp->attr_unit_number.store = NULL;
Michael Krufky3117bee2006-07-19 13:23:38 -0300847 ret = class_device_create_file(sfp->class_dev,&sfp->attr_unit_number);
Mike Isely08d41802006-07-22 21:26:30 -0300848 if (ret < 0) {
Michael Krufky3117bee2006-07-19 13:23:38 -0300849 printk(KERN_WARNING "%s: class_device_create_file error: %d\n",
850 __FUNCTION__, ret);
Mike Isely08d41802006-07-22 21:26:30 -0300851 } else {
852 sfp->unit_number_created_ok = !0;
853 }
Mike Iselyd8554972006-06-26 20:58:46 -0300854
Mike Isely31a18542007-04-08 01:11:47 -0300855 sfp->attr_bus_info.attr.owner = THIS_MODULE;
856 sfp->attr_bus_info.attr.name = "bus_info_str";
857 sfp->attr_bus_info.attr.mode = S_IRUGO;
858 sfp->attr_bus_info.show = bus_info_show;
859 sfp->attr_bus_info.store = NULL;
860 ret = class_device_create_file(sfp->class_dev,
861 &sfp->attr_bus_info);
862 if (ret < 0) {
863 printk(KERN_WARNING "%s: class_device_create_file error: %d\n",
864 __FUNCTION__, ret);
865 } else {
866 sfp->bus_info_created_ok = !0;
867 }
868
Mike Iselyd8554972006-06-26 20:58:46 -0300869 pvr2_sysfs_add_controls(sfp);
870#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
871 pvr2_sysfs_add_debugifc(sfp);
872#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
873}
874
875
876static void pvr2_sysfs_internal_check(struct pvr2_channel *chp)
877{
878 struct pvr2_sysfs *sfp;
879 sfp = container_of(chp,struct pvr2_sysfs,channel);
880 if (!sfp->channel.mc_head->disconnect_flag) return;
881 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_sysfs id=%p",sfp);
882 class_dev_destroy(sfp);
883 pvr2_channel_done(&sfp->channel);
884 kfree(sfp);
885}
886
887
888struct pvr2_sysfs *pvr2_sysfs_create(struct pvr2_context *mp,
889 struct pvr2_sysfs_class *class_ptr)
890{
891 struct pvr2_sysfs *sfp;
Mike Iselyca545f72007-01-20 00:37:11 -0300892 sfp = kzalloc(sizeof(*sfp),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -0300893 if (!sfp) return sfp;
Mike Iselyd8554972006-06-26 20:58:46 -0300894 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_sysfs id=%p",sfp);
895 pvr2_channel_init(&sfp->channel,mp);
896 sfp->channel.check_func = pvr2_sysfs_internal_check;
897
898 class_dev_create(sfp,class_ptr);
899 return sfp;
900}
901
902
903static int pvr2_sysfs_hotplug(struct class_device *cd,char **envp,
904 int numenvp,char *buf,int size)
905{
906 /* Even though we don't do anything here, we still need this function
907 because sysfs will still try to call it. */
908 return 0;
909}
910
911struct pvr2_sysfs_class *pvr2_sysfs_class_create(void)
912{
913 struct pvr2_sysfs_class *clp;
Mike Iselyca545f72007-01-20 00:37:11 -0300914 clp = kzalloc(sizeof(*clp),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -0300915 if (!clp) return clp;
Mike Iselyd8554972006-06-26 20:58:46 -0300916 pvr2_sysfs_trace("Creating pvr2_sysfs_class id=%p",clp);
917 clp->class.name = "pvrusb2";
918 clp->class.class_release = pvr2_sysfs_class_release;
919 clp->class.release = pvr2_sysfs_release;
920 clp->class.uevent = pvr2_sysfs_hotplug;
921 if (class_register(&clp->class)) {
922 pvr2_sysfs_trace(
923 "Registration failed for pvr2_sysfs_class id=%p",clp);
924 kfree(clp);
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300925 clp = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300926 }
927 return clp;
928}
929
930
931void pvr2_sysfs_class_destroy(struct pvr2_sysfs_class *clp)
932{
933 class_unregister(&clp->class);
934}
935
936
937#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
938static ssize_t debuginfo_show(struct class_device *class_dev,char *buf)
939{
940 struct pvr2_sysfs *sfp;
941 sfp = (struct pvr2_sysfs *)class_dev->class_data;
942 if (!sfp) return -EINVAL;
943 pvr2_hdw_trigger_module_log(sfp->channel.hdw);
944 return pvr2_debugifc_print_info(sfp->channel.hdw,buf,PAGE_SIZE);
945}
946
947
948static ssize_t debugcmd_show(struct class_device *class_dev,char *buf)
949{
950 struct pvr2_sysfs *sfp;
951 sfp = (struct pvr2_sysfs *)class_dev->class_data;
952 if (!sfp) return -EINVAL;
953 return pvr2_debugifc_print_status(sfp->channel.hdw,buf,PAGE_SIZE);
954}
955
956
957static ssize_t debugcmd_store(struct class_device *class_dev,
958 const char *buf,size_t count)
959{
960 struct pvr2_sysfs *sfp;
961 int ret;
962
963 sfp = (struct pvr2_sysfs *)class_dev->class_data;
964 if (!sfp) return -EINVAL;
965
966 ret = pvr2_debugifc_docmd(sfp->channel.hdw,buf,count);
967 if (ret < 0) return ret;
968 return count;
969}
970#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
971
972
973/*
974 Stuff for Emacs to see, in order to encourage consistent editing style:
975 *** Local Variables: ***
976 *** mode: c ***
977 *** fill-column: 75 ***
978 *** tab-width: 8 ***
979 *** c-basic-offset: 8 ***
980 *** End: ***
981 */