blob: 2f6568e7e6f271ae2e47b5e0f9351c71c62ed9d4 [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 Isely08d41802006-07-22 21:26:30 -030045 int v4l_minor_number_created_ok;
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -030046 int v4l_radio_minor_number_created_ok;
Mike Isely08d41802006-07-22 21:26:30 -030047 int unit_number_created_ok;
Mike Iselyd8554972006-06-26 20:58:46 -030048};
49
50#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
51struct pvr2_sysfs_debugifc {
52 struct class_device_attribute attr_debugcmd;
53 struct class_device_attribute attr_debuginfo;
Mike Isely08d41802006-07-22 21:26:30 -030054 int debugcmd_created_ok;
55 int debuginfo_created_ok;
Mike Iselyd8554972006-06-26 20:58:46 -030056};
57#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
58
59struct pvr2_sysfs_ctl_item {
60 struct class_device_attribute attr_name;
Mike Isely33213962006-06-25 20:04:40 -030061 struct class_device_attribute attr_type;
Mike Iselyd8554972006-06-26 20:58:46 -030062 struct class_device_attribute attr_min;
63 struct class_device_attribute attr_max;
64 struct class_device_attribute attr_enum;
65 struct class_device_attribute attr_bits;
66 struct class_device_attribute attr_val;
67 struct class_device_attribute attr_custom;
68 struct pvr2_ctrl *cptr;
69 struct pvr2_sysfs *chptr;
70 struct pvr2_sysfs_ctl_item *item_next;
Mike Isely33213962006-06-25 20:04:40 -030071 struct attribute *attr_gen[7];
Mike Iselyd8554972006-06-26 20:58:46 -030072 struct attribute_group grp;
Mike Isely08d41802006-07-22 21:26:30 -030073 int created_ok;
Mike Iselyd8554972006-06-26 20:58:46 -030074 char name[80];
75};
76
77struct pvr2_sysfs_class {
78 struct class class;
79};
80
81static ssize_t show_name(int id,struct class_device *class_dev,char *buf)
82{
83 struct pvr2_ctrl *cptr;
84 struct pvr2_sysfs *sfp;
85 const char *name;
86
87 sfp = (struct pvr2_sysfs *)class_dev->class_data;
88 if (!sfp) return -EINVAL;
89 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
90 if (!cptr) return -EINVAL;
91
92 name = pvr2_ctrl_get_desc(cptr);
93 pvr2_sysfs_trace("pvr2_sysfs(%p) show_name(cid=%d) is %s",sfp,id,name);
94
95 if (!name) return -EINVAL;
96
97 return scnprintf(buf,PAGE_SIZE,"%s\n",name);
98}
99
Mike Isely33213962006-06-25 20:04:40 -0300100static ssize_t show_type(int id,struct class_device *class_dev,char *buf)
101{
102 struct pvr2_ctrl *cptr;
103 struct pvr2_sysfs *sfp;
104 const char *name;
105 enum pvr2_ctl_type tp;
106
107 sfp = (struct pvr2_sysfs *)class_dev->class_data;
108 if (!sfp) return -EINVAL;
109 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
110 if (!cptr) return -EINVAL;
111
112 tp = pvr2_ctrl_get_type(cptr);
113 switch (tp) {
114 case pvr2_ctl_int: name = "integer"; break;
115 case pvr2_ctl_enum: name = "enum"; break;
116 case pvr2_ctl_bitmask: name = "bitmask"; break;
117 case pvr2_ctl_bool: name = "boolean"; break;
118 default: name = "?"; break;
119 }
120 pvr2_sysfs_trace("pvr2_sysfs(%p) show_type(cid=%d) is %s",sfp,id,name);
121
122 if (!name) return -EINVAL;
123
124 return scnprintf(buf,PAGE_SIZE,"%s\n",name);
125}
126
Mike Iselyd8554972006-06-26 20:58:46 -0300127static ssize_t show_min(int id,struct class_device *class_dev,char *buf)
128{
129 struct pvr2_ctrl *cptr;
130 struct pvr2_sysfs *sfp;
131 long val;
132
133 sfp = (struct pvr2_sysfs *)class_dev->class_data;
134 if (!sfp) return -EINVAL;
135 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
136 if (!cptr) return -EINVAL;
137 val = pvr2_ctrl_get_min(cptr);
138
139 pvr2_sysfs_trace("pvr2_sysfs(%p) show_min(cid=%d) is %ld",sfp,id,val);
140
141 return scnprintf(buf,PAGE_SIZE,"%ld\n",val);
142}
143
144static ssize_t show_max(int id,struct class_device *class_dev,char *buf)
145{
146 struct pvr2_ctrl *cptr;
147 struct pvr2_sysfs *sfp;
148 long val;
149
150 sfp = (struct pvr2_sysfs *)class_dev->class_data;
151 if (!sfp) return -EINVAL;
152 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
153 if (!cptr) return -EINVAL;
154 val = pvr2_ctrl_get_max(cptr);
155
156 pvr2_sysfs_trace("pvr2_sysfs(%p) show_max(cid=%d) is %ld",sfp,id,val);
157
158 return scnprintf(buf,PAGE_SIZE,"%ld\n",val);
159}
160
161static ssize_t show_val_norm(int id,struct class_device *class_dev,char *buf)
162{
163 struct pvr2_ctrl *cptr;
164 struct pvr2_sysfs *sfp;
165 int val,ret;
166 unsigned int cnt = 0;
167
168 sfp = (struct pvr2_sysfs *)class_dev->class_data;
169 if (!sfp) return -EINVAL;
170 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
171 if (!cptr) return -EINVAL;
172
173 ret = pvr2_ctrl_get_value(cptr,&val);
174 if (ret < 0) return ret;
175
176 ret = pvr2_ctrl_value_to_sym(cptr,~0,val,
177 buf,PAGE_SIZE-1,&cnt);
178
179 pvr2_sysfs_trace("pvr2_sysfs(%p) show_val_norm(cid=%d) is %.*s (%d)",
180 sfp,id,cnt,buf,val);
181 buf[cnt] = '\n';
182 return cnt+1;
183}
184
185static ssize_t show_val_custom(int id,struct class_device *class_dev,char *buf)
186{
187 struct pvr2_ctrl *cptr;
188 struct pvr2_sysfs *sfp;
189 int val,ret;
190 unsigned int cnt = 0;
191
192 sfp = (struct pvr2_sysfs *)class_dev->class_data;
193 if (!sfp) return -EINVAL;
194 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
195 if (!cptr) return -EINVAL;
196
197 ret = pvr2_ctrl_get_value(cptr,&val);
198 if (ret < 0) return ret;
199
200 ret = pvr2_ctrl_custom_value_to_sym(cptr,~0,val,
201 buf,PAGE_SIZE-1,&cnt);
202
203 pvr2_sysfs_trace("pvr2_sysfs(%p) show_val_custom(cid=%d) is %.*s (%d)",
204 sfp,id,cnt,buf,val);
205 buf[cnt] = '\n';
206 return cnt+1;
207}
208
209static ssize_t show_enum(int id,struct class_device *class_dev,char *buf)
210{
211 struct pvr2_ctrl *cptr;
212 struct pvr2_sysfs *sfp;
213 long val;
214 unsigned int bcnt,ccnt,ecnt;
215
216 sfp = (struct pvr2_sysfs *)class_dev->class_data;
217 if (!sfp) return -EINVAL;
218 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
219 if (!cptr) return -EINVAL;
220 ecnt = pvr2_ctrl_get_cnt(cptr);
221 bcnt = 0;
222 for (val = 0; val < ecnt; val++) {
223 pvr2_ctrl_get_valname(cptr,val,buf+bcnt,PAGE_SIZE-bcnt,&ccnt);
Mike Isely45886772006-06-25 20:04:13 -0300224 if (!ccnt) continue;
Mike Iselyd8554972006-06-26 20:58:46 -0300225 bcnt += ccnt;
226 if (bcnt >= PAGE_SIZE) break;
227 buf[bcnt] = '\n';
228 bcnt++;
229 }
230 pvr2_sysfs_trace("pvr2_sysfs(%p) show_enum(cid=%d)",sfp,id);
231 return bcnt;
232}
233
234static ssize_t show_bits(int id,struct class_device *class_dev,char *buf)
235{
236 struct pvr2_ctrl *cptr;
237 struct pvr2_sysfs *sfp;
238 int valid_bits,msk;
239 unsigned int bcnt,ccnt;
240
241 sfp = (struct pvr2_sysfs *)class_dev->class_data;
242 if (!sfp) return -EINVAL;
243 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
244 if (!cptr) return -EINVAL;
245 valid_bits = pvr2_ctrl_get_mask(cptr);
246 bcnt = 0;
247 for (msk = 1; valid_bits; msk <<= 1) {
248 if (!(msk & valid_bits)) continue;
249 valid_bits &= ~msk;
250 pvr2_ctrl_get_valname(cptr,msk,buf+bcnt,PAGE_SIZE-bcnt,&ccnt);
251 bcnt += ccnt;
252 if (bcnt >= PAGE_SIZE) break;
253 buf[bcnt] = '\n';
254 bcnt++;
255 }
256 pvr2_sysfs_trace("pvr2_sysfs(%p) show_bits(cid=%d)",sfp,id);
257 return bcnt;
258}
259
260static int store_val_any(int id,int customfl,struct pvr2_sysfs *sfp,
261 const char *buf,unsigned int count)
262{
263 struct pvr2_ctrl *cptr;
264 int ret;
265 int mask,val;
266
267 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
268 if (customfl) {
269 ret = pvr2_ctrl_custom_sym_to_value(cptr,buf,count,&mask,&val);
270 } else {
271 ret = pvr2_ctrl_sym_to_value(cptr,buf,count,&mask,&val);
272 }
273 if (ret < 0) return ret;
274 ret = pvr2_ctrl_set_mask_value(cptr,mask,val);
275 pvr2_hdw_commit_ctl(sfp->channel.hdw);
276 return ret;
277}
278
279static ssize_t store_val_norm(int id,struct class_device *class_dev,
280 const char *buf,size_t count)
281{
282 struct pvr2_sysfs *sfp;
283 int ret;
284 sfp = (struct pvr2_sysfs *)class_dev->class_data;
285 ret = store_val_any(id,0,sfp,buf,count);
286 if (!ret) ret = count;
287 return ret;
288}
289
290static ssize_t store_val_custom(int id,struct class_device *class_dev,
291 const char *buf,size_t count)
292{
293 struct pvr2_sysfs *sfp;
294 int ret;
295 sfp = (struct pvr2_sysfs *)class_dev->class_data;
296 ret = store_val_any(id,1,sfp,buf,count);
297 if (!ret) ret = count;
298 return ret;
299}
300
301/*
302 Mike Isely <isely@pobox.com> 30-April-2005
303
304 This next batch of horrible preprocessor hackery is needed because the
305 kernel's class_device_attribute mechanism fails to pass the actual
306 attribute through to the show / store functions, which means we have no
307 way to package up any attribute-specific parameters, like for example the
308 control id. So we work around this brain-damage by encoding the control
309 id into the show / store functions themselves and pick the function based
310 on the control id we're setting up. These macros try to ease the pain.
311 Yuck.
312*/
313
314#define CREATE_SHOW_INSTANCE(sf_name,ctl_id) \
315static ssize_t sf_name##_##ctl_id(struct class_device *class_dev,char *buf) \
316{ return sf_name(ctl_id,class_dev,buf); }
317
318#define CREATE_STORE_INSTANCE(sf_name,ctl_id) \
319static ssize_t sf_name##_##ctl_id(struct class_device *class_dev,const char *buf,size_t count) \
320{ return sf_name(ctl_id,class_dev,buf,count); }
321
322#define CREATE_BATCH(ctl_id) \
323CREATE_SHOW_INSTANCE(show_name,ctl_id) \
Mike Isely33213962006-06-25 20:04:40 -0300324CREATE_SHOW_INSTANCE(show_type,ctl_id) \
Mike Iselyd8554972006-06-26 20:58:46 -0300325CREATE_SHOW_INSTANCE(show_min,ctl_id) \
326CREATE_SHOW_INSTANCE(show_max,ctl_id) \
327CREATE_SHOW_INSTANCE(show_val_norm,ctl_id) \
328CREATE_SHOW_INSTANCE(show_val_custom,ctl_id) \
329CREATE_SHOW_INSTANCE(show_enum,ctl_id) \
330CREATE_SHOW_INSTANCE(show_bits,ctl_id) \
331CREATE_STORE_INSTANCE(store_val_norm,ctl_id) \
332CREATE_STORE_INSTANCE(store_val_custom,ctl_id) \
333
334CREATE_BATCH(0)
335CREATE_BATCH(1)
336CREATE_BATCH(2)
337CREATE_BATCH(3)
338CREATE_BATCH(4)
339CREATE_BATCH(5)
340CREATE_BATCH(6)
341CREATE_BATCH(7)
342CREATE_BATCH(8)
343CREATE_BATCH(9)
344CREATE_BATCH(10)
345CREATE_BATCH(11)
346CREATE_BATCH(12)
347CREATE_BATCH(13)
348CREATE_BATCH(14)
349CREATE_BATCH(15)
350CREATE_BATCH(16)
351CREATE_BATCH(17)
352CREATE_BATCH(18)
353CREATE_BATCH(19)
354CREATE_BATCH(20)
355CREATE_BATCH(21)
356CREATE_BATCH(22)
357CREATE_BATCH(23)
358CREATE_BATCH(24)
359CREATE_BATCH(25)
360CREATE_BATCH(26)
361CREATE_BATCH(27)
362CREATE_BATCH(28)
363CREATE_BATCH(29)
364CREATE_BATCH(30)
365CREATE_BATCH(31)
366CREATE_BATCH(32)
367CREATE_BATCH(33)
Mike Isely39481992006-06-25 20:04:20 -0300368CREATE_BATCH(34)
369CREATE_BATCH(35)
370CREATE_BATCH(36)
371CREATE_BATCH(37)
372CREATE_BATCH(38)
373CREATE_BATCH(39)
374CREATE_BATCH(40)
375CREATE_BATCH(41)
376CREATE_BATCH(42)
377CREATE_BATCH(43)
378CREATE_BATCH(44)
379CREATE_BATCH(45)
380CREATE_BATCH(46)
381CREATE_BATCH(47)
382CREATE_BATCH(48)
383CREATE_BATCH(49)
384CREATE_BATCH(50)
385CREATE_BATCH(51)
386CREATE_BATCH(52)
387CREATE_BATCH(53)
388CREATE_BATCH(54)
389CREATE_BATCH(55)
390CREATE_BATCH(56)
391CREATE_BATCH(57)
392CREATE_BATCH(58)
393CREATE_BATCH(59)
Mike Iselyd8554972006-06-26 20:58:46 -0300394
395struct pvr2_sysfs_func_set {
396 ssize_t (*show_name)(struct class_device *,char *);
Mike Isely33213962006-06-25 20:04:40 -0300397 ssize_t (*show_type)(struct class_device *,char *);
Mike Iselyd8554972006-06-26 20:58:46 -0300398 ssize_t (*show_min)(struct class_device *,char *);
399 ssize_t (*show_max)(struct class_device *,char *);
400 ssize_t (*show_enum)(struct class_device *,char *);
401 ssize_t (*show_bits)(struct class_device *,char *);
402 ssize_t (*show_val_norm)(struct class_device *,char *);
403 ssize_t (*store_val_norm)(struct class_device *,
404 const char *,size_t);
405 ssize_t (*show_val_custom)(struct class_device *,char *);
406 ssize_t (*store_val_custom)(struct class_device *,
407 const char *,size_t);
408};
409
410#define INIT_BATCH(ctl_id) \
411[ctl_id] = { \
412 .show_name = show_name_##ctl_id, \
Mike Isely33213962006-06-25 20:04:40 -0300413 .show_type = show_type_##ctl_id, \
Mike Iselyd8554972006-06-26 20:58:46 -0300414 .show_min = show_min_##ctl_id, \
415 .show_max = show_max_##ctl_id, \
416 .show_enum = show_enum_##ctl_id, \
417 .show_bits = show_bits_##ctl_id, \
418 .show_val_norm = show_val_norm_##ctl_id, \
419 .store_val_norm = store_val_norm_##ctl_id, \
420 .show_val_custom = show_val_custom_##ctl_id, \
421 .store_val_custom = store_val_custom_##ctl_id, \
422} \
423
424static struct pvr2_sysfs_func_set funcs[] = {
425 INIT_BATCH(0),
426 INIT_BATCH(1),
427 INIT_BATCH(2),
428 INIT_BATCH(3),
429 INIT_BATCH(4),
430 INIT_BATCH(5),
431 INIT_BATCH(6),
432 INIT_BATCH(7),
433 INIT_BATCH(8),
434 INIT_BATCH(9),
435 INIT_BATCH(10),
436 INIT_BATCH(11),
437 INIT_BATCH(12),
438 INIT_BATCH(13),
439 INIT_BATCH(14),
440 INIT_BATCH(15),
441 INIT_BATCH(16),
442 INIT_BATCH(17),
443 INIT_BATCH(18),
444 INIT_BATCH(19),
445 INIT_BATCH(20),
446 INIT_BATCH(21),
447 INIT_BATCH(22),
448 INIT_BATCH(23),
449 INIT_BATCH(24),
450 INIT_BATCH(25),
451 INIT_BATCH(26),
452 INIT_BATCH(27),
453 INIT_BATCH(28),
454 INIT_BATCH(29),
455 INIT_BATCH(30),
456 INIT_BATCH(31),
457 INIT_BATCH(32),
458 INIT_BATCH(33),
Mike Isely39481992006-06-25 20:04:20 -0300459 INIT_BATCH(34),
460 INIT_BATCH(35),
461 INIT_BATCH(36),
462 INIT_BATCH(37),
463 INIT_BATCH(38),
464 INIT_BATCH(39),
465 INIT_BATCH(40),
466 INIT_BATCH(41),
467 INIT_BATCH(42),
468 INIT_BATCH(43),
469 INIT_BATCH(44),
470 INIT_BATCH(45),
471 INIT_BATCH(46),
472 INIT_BATCH(47),
473 INIT_BATCH(48),
474 INIT_BATCH(49),
475 INIT_BATCH(50),
476 INIT_BATCH(51),
477 INIT_BATCH(52),
478 INIT_BATCH(53),
479 INIT_BATCH(54),
480 INIT_BATCH(55),
481 INIT_BATCH(56),
482 INIT_BATCH(57),
483 INIT_BATCH(58),
484 INIT_BATCH(59),
Mike Iselyd8554972006-06-26 20:58:46 -0300485};
486
487
488static void pvr2_sysfs_add_control(struct pvr2_sysfs *sfp,int ctl_id)
489{
490 struct pvr2_sysfs_ctl_item *cip;
491 struct pvr2_sysfs_func_set *fp;
492 struct pvr2_ctrl *cptr;
493 unsigned int cnt,acnt;
Mike Isely08d41802006-07-22 21:26:30 -0300494 int ret;
Mike Iselyd8554972006-06-26 20:58:46 -0300495
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300496 if ((ctl_id < 0) || (ctl_id >= ARRAY_SIZE(funcs))) {
Mike Iselyd8554972006-06-26 20:58:46 -0300497 return;
498 }
499
500 fp = funcs + ctl_id;
501 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,ctl_id);
502 if (!cptr) return;
503
504 cip = kmalloc(sizeof(*cip),GFP_KERNEL);
505 if (!cip) return;
506 memset(cip,0,sizeof(*cip));
507 pvr2_sysfs_trace("Creating pvr2_sysfs_ctl_item id=%p",cip);
508
509 cip->cptr = cptr;
510
511 cip->chptr = sfp;
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300512 cip->item_next = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300513 if (sfp->item_last) {
514 sfp->item_last->item_next = cip;
515 } else {
516 sfp->item_first = cip;
517 }
518 sfp->item_last = cip;
519
520 cip->attr_name.attr.owner = THIS_MODULE;
521 cip->attr_name.attr.name = "name";
522 cip->attr_name.attr.mode = S_IRUGO;
523 cip->attr_name.show = fp->show_name;
524
Mike Isely33213962006-06-25 20:04:40 -0300525 cip->attr_type.attr.owner = THIS_MODULE;
526 cip->attr_type.attr.name = "type";
527 cip->attr_type.attr.mode = S_IRUGO;
528 cip->attr_type.show = fp->show_type;
529
Mike Iselyd8554972006-06-26 20:58:46 -0300530 cip->attr_min.attr.owner = THIS_MODULE;
531 cip->attr_min.attr.name = "min_val";
532 cip->attr_min.attr.mode = S_IRUGO;
533 cip->attr_min.show = fp->show_min;
534
535 cip->attr_max.attr.owner = THIS_MODULE;
536 cip->attr_max.attr.name = "max_val";
537 cip->attr_max.attr.mode = S_IRUGO;
538 cip->attr_max.show = fp->show_max;
539
540 cip->attr_val.attr.owner = THIS_MODULE;
541 cip->attr_val.attr.name = "cur_val";
542 cip->attr_val.attr.mode = S_IRUGO;
543
544 cip->attr_custom.attr.owner = THIS_MODULE;
545 cip->attr_custom.attr.name = "custom_val";
546 cip->attr_custom.attr.mode = S_IRUGO;
547
548 cip->attr_enum.attr.owner = THIS_MODULE;
549 cip->attr_enum.attr.name = "enum_val";
550 cip->attr_enum.attr.mode = S_IRUGO;
551 cip->attr_enum.show = fp->show_enum;
552
553 cip->attr_bits.attr.owner = THIS_MODULE;
554 cip->attr_bits.attr.name = "bit_val";
555 cip->attr_bits.attr.mode = S_IRUGO;
556 cip->attr_bits.show = fp->show_bits;
557
558 if (pvr2_ctrl_is_writable(cptr)) {
559 cip->attr_val.attr.mode |= S_IWUSR|S_IWGRP;
560 cip->attr_custom.attr.mode |= S_IWUSR|S_IWGRP;
561 }
562
563 acnt = 0;
564 cip->attr_gen[acnt++] = &cip->attr_name.attr;
Mike Isely33213962006-06-25 20:04:40 -0300565 cip->attr_gen[acnt++] = &cip->attr_type.attr;
Mike Iselyd8554972006-06-26 20:58:46 -0300566 cip->attr_gen[acnt++] = &cip->attr_val.attr;
567 cip->attr_val.show = fp->show_val_norm;
568 cip->attr_val.store = fp->store_val_norm;
569 if (pvr2_ctrl_has_custom_symbols(cptr)) {
570 cip->attr_gen[acnt++] = &cip->attr_custom.attr;
571 cip->attr_custom.show = fp->show_val_custom;
572 cip->attr_custom.store = fp->store_val_custom;
573 }
574 switch (pvr2_ctrl_get_type(cptr)) {
575 case pvr2_ctl_enum:
576 // Control is an enumeration
577 cip->attr_gen[acnt++] = &cip->attr_enum.attr;
578 break;
579 case pvr2_ctl_int:
580 // Control is an integer
581 cip->attr_gen[acnt++] = &cip->attr_min.attr;
582 cip->attr_gen[acnt++] = &cip->attr_max.attr;
583 break;
584 case pvr2_ctl_bitmask:
585 // Control is an bitmask
586 cip->attr_gen[acnt++] = &cip->attr_bits.attr;
587 break;
588 default: break;
589 }
590
591 cnt = scnprintf(cip->name,sizeof(cip->name)-1,"ctl_%s",
592 pvr2_ctrl_get_name(cptr));
593 cip->name[cnt] = 0;
594 cip->grp.name = cip->name;
595 cip->grp.attrs = cip->attr_gen;
596
Mike Isely08d41802006-07-22 21:26:30 -0300597 ret = sysfs_create_group(&sfp->class_dev->kobj,&cip->grp);
598 if (ret) {
599 printk(KERN_WARNING "%s: sysfs_create_group error: %d\n",
600 __FUNCTION__, ret);
601 return;
602 }
603 cip->created_ok = !0;
Mike Iselyd8554972006-06-26 20:58:46 -0300604}
605
606#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
607static ssize_t debuginfo_show(struct class_device *,char *);
608static ssize_t debugcmd_show(struct class_device *,char *);
609static ssize_t debugcmd_store(struct class_device *,const char *,size_t count);
610
611static void pvr2_sysfs_add_debugifc(struct pvr2_sysfs *sfp)
612{
613 struct pvr2_sysfs_debugifc *dip;
Michael Krufky3117bee2006-07-19 13:23:38 -0300614 int ret;
615
Mike Iselyd8554972006-06-26 20:58:46 -0300616 dip = kmalloc(sizeof(*dip),GFP_KERNEL);
617 if (!dip) return;
618 memset(dip,0,sizeof(*dip));
619 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 Isely08d41802006-07-22 21:26:30 -0300710 if (sfp->v4l_minor_number_created_ok) {
711 class_device_remove_file(sfp->class_dev,
712 &sfp->attr_v4l_minor_number);
713 }
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -0300714 if (sfp->v4l_radio_minor_number_created_ok) {
715 class_device_remove_file(sfp->class_dev,
716 &sfp->attr_v4l_radio_minor_number);
717 }
Mike Isely08d41802006-07-22 21:26:30 -0300718 if (sfp->unit_number_created_ok) {
719 class_device_remove_file(sfp->class_dev,
720 &sfp->attr_unit_number);
721 }
Mike Iselyd8554972006-06-26 20:58:46 -0300722 pvr2_sysfs_trace("Destroying class_dev id=%p",sfp->class_dev);
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300723 sfp->class_dev->class_data = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300724 class_device_unregister(sfp->class_dev);
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300725 sfp->class_dev = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300726}
727
728
729static ssize_t v4l_minor_number_show(struct class_device *class_dev,char *buf)
730{
731 struct pvr2_sysfs *sfp;
732 sfp = (struct pvr2_sysfs *)class_dev->class_data;
733 if (!sfp) return -EINVAL;
734 return scnprintf(buf,PAGE_SIZE,"%d\n",
Mike Iselyfd5a75f2006-12-27 23:11:22 -0300735 pvr2_hdw_v4l_get_minor_number(sfp->channel.hdw,
Mike Isely80793842006-12-27 23:12:28 -0300736 pvr2_v4l_type_video));
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -0300737}
738
739
740static ssize_t v4l_radio_minor_number_show(struct class_device *class_dev,
741 char *buf)
742{
743 struct pvr2_sysfs *sfp;
744 sfp = (struct pvr2_sysfs *)class_dev->class_data;
745 if (!sfp) return -EINVAL;
746 return scnprintf(buf,PAGE_SIZE,"%d\n",
Mike Iselyfd5a75f2006-12-27 23:11:22 -0300747 pvr2_hdw_v4l_get_minor_number(sfp->channel.hdw,
Mike Isely80793842006-12-27 23:12:28 -0300748 pvr2_v4l_type_radio));
Mike Iselyd8554972006-06-26 20:58:46 -0300749}
750
751
752static ssize_t unit_number_show(struct class_device *class_dev,char *buf)
753{
754 struct pvr2_sysfs *sfp;
755 sfp = (struct pvr2_sysfs *)class_dev->class_data;
756 if (!sfp) return -EINVAL;
757 return scnprintf(buf,PAGE_SIZE,"%d\n",
758 pvr2_hdw_get_unit_number(sfp->channel.hdw));
759}
760
761
762static void class_dev_create(struct pvr2_sysfs *sfp,
763 struct pvr2_sysfs_class *class_ptr)
764{
765 struct usb_device *usb_dev;
766 struct class_device *class_dev;
Michael Krufky3117bee2006-07-19 13:23:38 -0300767 int ret;
768
Mike Iselyd8554972006-06-26 20:58:46 -0300769 usb_dev = pvr2_hdw_get_dev(sfp->channel.hdw);
770 if (!usb_dev) return;
771 class_dev = kmalloc(sizeof(*class_dev),GFP_KERNEL);
772 if (!class_dev) return;
773 memset(class_dev,0,sizeof(*class_dev));
774
775 pvr2_sysfs_trace("Creating class_dev id=%p",class_dev);
776
777 class_dev->class = &class_ptr->class;
778 if (pvr2_hdw_get_sn(sfp->channel.hdw)) {
779 snprintf(class_dev->class_id,BUS_ID_SIZE,"sn-%lu",
780 pvr2_hdw_get_sn(sfp->channel.hdw));
781 } else if (pvr2_hdw_get_unit_number(sfp->channel.hdw) >= 0) {
782 snprintf(class_dev->class_id,BUS_ID_SIZE,"unit-%c",
783 pvr2_hdw_get_unit_number(sfp->channel.hdw) + 'a');
784 } else {
785 kfree(class_dev);
786 return;
787 }
788
789 class_dev->dev = &usb_dev->dev;
790
791 sfp->class_dev = class_dev;
792 class_dev->class_data = sfp;
Michael Krufky3117bee2006-07-19 13:23:38 -0300793 ret = class_device_register(class_dev);
794 if (ret) {
795 printk(KERN_ERR "%s: class_device_register failed\n",
796 __FUNCTION__);
797 kfree(class_dev);
798 return;
799 }
Mike Iselyd8554972006-06-26 20:58:46 -0300800
801 sfp->attr_v4l_minor_number.attr.owner = THIS_MODULE;
802 sfp->attr_v4l_minor_number.attr.name = "v4l_minor_number";
803 sfp->attr_v4l_minor_number.attr.mode = S_IRUGO;
804 sfp->attr_v4l_minor_number.show = v4l_minor_number_show;
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300805 sfp->attr_v4l_minor_number.store = NULL;
Mike Isely08d41802006-07-22 21:26:30 -0300806 ret = class_device_create_file(sfp->class_dev,
807 &sfp->attr_v4l_minor_number);
808 if (ret < 0) {
Michael Krufky3117bee2006-07-19 13:23:38 -0300809 printk(KERN_WARNING "%s: class_device_create_file error: %d\n",
810 __FUNCTION__, ret);
Mike Isely08d41802006-07-22 21:26:30 -0300811 } else {
812 sfp->v4l_minor_number_created_ok = !0;
813 }
Michael Krufky3117bee2006-07-19 13:23:38 -0300814
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -0300815 sfp->attr_v4l_radio_minor_number.attr.owner = THIS_MODULE;
816 sfp->attr_v4l_radio_minor_number.attr.name = "v4l_radio_minor_number";
817 sfp->attr_v4l_radio_minor_number.attr.mode = S_IRUGO;
818 sfp->attr_v4l_radio_minor_number.show = v4l_radio_minor_number_show;
819 sfp->attr_v4l_radio_minor_number.store = NULL;
820 ret = class_device_create_file(sfp->class_dev,
821 &sfp->attr_v4l_radio_minor_number);
822 if (ret < 0) {
823 printk(KERN_WARNING "%s: class_device_create_file error: %d\n",
824 __FUNCTION__, ret);
825 } else {
826 sfp->v4l_radio_minor_number_created_ok = !0;
827 }
828
Mike Iselyd8554972006-06-26 20:58:46 -0300829 sfp->attr_unit_number.attr.owner = THIS_MODULE;
830 sfp->attr_unit_number.attr.name = "unit_number";
831 sfp->attr_unit_number.attr.mode = S_IRUGO;
832 sfp->attr_unit_number.show = unit_number_show;
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300833 sfp->attr_unit_number.store = NULL;
Michael Krufky3117bee2006-07-19 13:23:38 -0300834 ret = class_device_create_file(sfp->class_dev,&sfp->attr_unit_number);
Mike Isely08d41802006-07-22 21:26:30 -0300835 if (ret < 0) {
Michael Krufky3117bee2006-07-19 13:23:38 -0300836 printk(KERN_WARNING "%s: class_device_create_file error: %d\n",
837 __FUNCTION__, ret);
Mike Isely08d41802006-07-22 21:26:30 -0300838 } else {
839 sfp->unit_number_created_ok = !0;
840 }
Mike Iselyd8554972006-06-26 20:58:46 -0300841
842 pvr2_sysfs_add_controls(sfp);
843#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
844 pvr2_sysfs_add_debugifc(sfp);
845#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
846}
847
848
849static void pvr2_sysfs_internal_check(struct pvr2_channel *chp)
850{
851 struct pvr2_sysfs *sfp;
852 sfp = container_of(chp,struct pvr2_sysfs,channel);
853 if (!sfp->channel.mc_head->disconnect_flag) return;
854 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_sysfs id=%p",sfp);
855 class_dev_destroy(sfp);
856 pvr2_channel_done(&sfp->channel);
857 kfree(sfp);
858}
859
860
861struct pvr2_sysfs *pvr2_sysfs_create(struct pvr2_context *mp,
862 struct pvr2_sysfs_class *class_ptr)
863{
864 struct pvr2_sysfs *sfp;
865 sfp = kmalloc(sizeof(*sfp),GFP_KERNEL);
866 if (!sfp) return sfp;
867 memset(sfp,0,sizeof(*sfp));
868 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_sysfs id=%p",sfp);
869 pvr2_channel_init(&sfp->channel,mp);
870 sfp->channel.check_func = pvr2_sysfs_internal_check;
871
872 class_dev_create(sfp,class_ptr);
873 return sfp;
874}
875
876
877static int pvr2_sysfs_hotplug(struct class_device *cd,char **envp,
878 int numenvp,char *buf,int size)
879{
880 /* Even though we don't do anything here, we still need this function
881 because sysfs will still try to call it. */
882 return 0;
883}
884
885struct pvr2_sysfs_class *pvr2_sysfs_class_create(void)
886{
887 struct pvr2_sysfs_class *clp;
888 clp = kmalloc(sizeof(*clp),GFP_KERNEL);
889 if (!clp) return clp;
890 memset(clp,0,sizeof(*clp));
891 pvr2_sysfs_trace("Creating pvr2_sysfs_class id=%p",clp);
892 clp->class.name = "pvrusb2";
893 clp->class.class_release = pvr2_sysfs_class_release;
894 clp->class.release = pvr2_sysfs_release;
895 clp->class.uevent = pvr2_sysfs_hotplug;
896 if (class_register(&clp->class)) {
897 pvr2_sysfs_trace(
898 "Registration failed for pvr2_sysfs_class id=%p",clp);
899 kfree(clp);
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300900 clp = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300901 }
902 return clp;
903}
904
905
906void pvr2_sysfs_class_destroy(struct pvr2_sysfs_class *clp)
907{
908 class_unregister(&clp->class);
909}
910
911
912#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
913static ssize_t debuginfo_show(struct class_device *class_dev,char *buf)
914{
915 struct pvr2_sysfs *sfp;
916 sfp = (struct pvr2_sysfs *)class_dev->class_data;
917 if (!sfp) return -EINVAL;
918 pvr2_hdw_trigger_module_log(sfp->channel.hdw);
919 return pvr2_debugifc_print_info(sfp->channel.hdw,buf,PAGE_SIZE);
920}
921
922
923static ssize_t debugcmd_show(struct class_device *class_dev,char *buf)
924{
925 struct pvr2_sysfs *sfp;
926 sfp = (struct pvr2_sysfs *)class_dev->class_data;
927 if (!sfp) return -EINVAL;
928 return pvr2_debugifc_print_status(sfp->channel.hdw,buf,PAGE_SIZE);
929}
930
931
932static ssize_t debugcmd_store(struct class_device *class_dev,
933 const char *buf,size_t count)
934{
935 struct pvr2_sysfs *sfp;
936 int ret;
937
938 sfp = (struct pvr2_sysfs *)class_dev->class_data;
939 if (!sfp) return -EINVAL;
940
941 ret = pvr2_debugifc_docmd(sfp->channel.hdw,buf,count);
942 if (ret < 0) return ret;
943 return count;
944}
945#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
946
947
948/*
949 Stuff for Emacs to see, in order to encourage consistent editing style:
950 *** Local Variables: ***
951 *** mode: c ***
952 *** fill-column: 75 ***
953 *** tab-width: 8 ***
954 *** c-basic-offset: 8 ***
955 *** End: ***
956 */