pvrusb2-sysfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /*
  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. #include <linux/config.h>
  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 */
  31. #define pvr2_sysfs_trace(...) pvr2_trace(PVR2_TRACE_SYSFS,__VA_ARGS__)
  32. struct pvr2_sysfs {
  33. struct pvr2_channel channel;
  34. struct class_device *class_dev;
  35. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  36. struct pvr2_sysfs_debugifc *debugifc;
  37. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  38. struct pvr2_sysfs_ctl_item *item_first;
  39. struct pvr2_sysfs_ctl_item *item_last;
  40. struct sysfs_ops kops;
  41. struct kobj_type ktype;
  42. struct class_device_attribute attr_v4l_minor_number;
  43. struct class_device_attribute attr_unit_number;
  44. };
  45. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  46. struct pvr2_sysfs_debugifc {
  47. struct class_device_attribute attr_debugcmd;
  48. struct class_device_attribute attr_debuginfo;
  49. };
  50. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  51. struct pvr2_sysfs_ctl_item {
  52. struct class_device_attribute attr_name;
  53. struct class_device_attribute attr_type;
  54. struct class_device_attribute attr_min;
  55. struct class_device_attribute attr_max;
  56. struct class_device_attribute attr_enum;
  57. struct class_device_attribute attr_bits;
  58. struct class_device_attribute attr_val;
  59. struct class_device_attribute attr_custom;
  60. struct pvr2_ctrl *cptr;
  61. struct pvr2_sysfs *chptr;
  62. struct pvr2_sysfs_ctl_item *item_next;
  63. struct attribute *attr_gen[7];
  64. struct attribute_group grp;
  65. char name[80];
  66. };
  67. struct pvr2_sysfs_class {
  68. struct class class;
  69. };
  70. static ssize_t show_name(int id,struct class_device *class_dev,char *buf)
  71. {
  72. struct pvr2_ctrl *cptr;
  73. struct pvr2_sysfs *sfp;
  74. const char *name;
  75. sfp = (struct pvr2_sysfs *)class_dev->class_data;
  76. if (!sfp) return -EINVAL;
  77. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
  78. if (!cptr) return -EINVAL;
  79. name = pvr2_ctrl_get_desc(cptr);
  80. pvr2_sysfs_trace("pvr2_sysfs(%p) show_name(cid=%d) is %s",sfp,id,name);
  81. if (!name) return -EINVAL;
  82. return scnprintf(buf,PAGE_SIZE,"%s\n",name);
  83. }
  84. static ssize_t show_type(int id,struct class_device *class_dev,char *buf)
  85. {
  86. struct pvr2_ctrl *cptr;
  87. struct pvr2_sysfs *sfp;
  88. const char *name;
  89. enum pvr2_ctl_type tp;
  90. sfp = (struct pvr2_sysfs *)class_dev->class_data;
  91. if (!sfp) return -EINVAL;
  92. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
  93. if (!cptr) return -EINVAL;
  94. tp = pvr2_ctrl_get_type(cptr);
  95. switch (tp) {
  96. case pvr2_ctl_int: name = "integer"; break;
  97. case pvr2_ctl_enum: name = "enum"; break;
  98. case pvr2_ctl_bitmask: name = "bitmask"; break;
  99. case pvr2_ctl_bool: name = "boolean"; break;
  100. default: name = "?"; break;
  101. }
  102. pvr2_sysfs_trace("pvr2_sysfs(%p) show_type(cid=%d) is %s",sfp,id,name);
  103. if (!name) return -EINVAL;
  104. return scnprintf(buf,PAGE_SIZE,"%s\n",name);
  105. }
  106. static ssize_t show_min(int id,struct class_device *class_dev,char *buf)
  107. {
  108. struct pvr2_ctrl *cptr;
  109. struct pvr2_sysfs *sfp;
  110. long val;
  111. sfp = (struct pvr2_sysfs *)class_dev->class_data;
  112. if (!sfp) return -EINVAL;
  113. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
  114. if (!cptr) return -EINVAL;
  115. val = pvr2_ctrl_get_min(cptr);
  116. pvr2_sysfs_trace("pvr2_sysfs(%p) show_min(cid=%d) is %ld",sfp,id,val);
  117. return scnprintf(buf,PAGE_SIZE,"%ld\n",val);
  118. }
  119. static ssize_t show_max(int id,struct class_device *class_dev,char *buf)
  120. {
  121. struct pvr2_ctrl *cptr;
  122. struct pvr2_sysfs *sfp;
  123. long val;
  124. sfp = (struct pvr2_sysfs *)class_dev->class_data;
  125. if (!sfp) return -EINVAL;
  126. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
  127. if (!cptr) return -EINVAL;
  128. val = pvr2_ctrl_get_max(cptr);
  129. pvr2_sysfs_trace("pvr2_sysfs(%p) show_max(cid=%d) is %ld",sfp,id,val);
  130. return scnprintf(buf,PAGE_SIZE,"%ld\n",val);
  131. }
  132. static ssize_t show_val_norm(int id,struct class_device *class_dev,char *buf)
  133. {
  134. struct pvr2_ctrl *cptr;
  135. struct pvr2_sysfs *sfp;
  136. int val,ret;
  137. unsigned int cnt = 0;
  138. sfp = (struct pvr2_sysfs *)class_dev->class_data;
  139. if (!sfp) return -EINVAL;
  140. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
  141. if (!cptr) return -EINVAL;
  142. ret = pvr2_ctrl_get_value(cptr,&val);
  143. if (ret < 0) return ret;
  144. ret = pvr2_ctrl_value_to_sym(cptr,~0,val,
  145. buf,PAGE_SIZE-1,&cnt);
  146. pvr2_sysfs_trace("pvr2_sysfs(%p) show_val_norm(cid=%d) is %.*s (%d)",
  147. sfp,id,cnt,buf,val);
  148. buf[cnt] = '\n';
  149. return cnt+1;
  150. }
  151. static ssize_t show_val_custom(int id,struct class_device *class_dev,char *buf)
  152. {
  153. struct pvr2_ctrl *cptr;
  154. struct pvr2_sysfs *sfp;
  155. int val,ret;
  156. unsigned int cnt = 0;
  157. sfp = (struct pvr2_sysfs *)class_dev->class_data;
  158. if (!sfp) return -EINVAL;
  159. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
  160. if (!cptr) return -EINVAL;
  161. ret = pvr2_ctrl_get_value(cptr,&val);
  162. if (ret < 0) return ret;
  163. ret = pvr2_ctrl_custom_value_to_sym(cptr,~0,val,
  164. buf,PAGE_SIZE-1,&cnt);
  165. pvr2_sysfs_trace("pvr2_sysfs(%p) show_val_custom(cid=%d) is %.*s (%d)",
  166. sfp,id,cnt,buf,val);
  167. buf[cnt] = '\n';
  168. return cnt+1;
  169. }
  170. static ssize_t show_enum(int id,struct class_device *class_dev,char *buf)
  171. {
  172. struct pvr2_ctrl *cptr;
  173. struct pvr2_sysfs *sfp;
  174. long val;
  175. unsigned int bcnt,ccnt,ecnt;
  176. sfp = (struct pvr2_sysfs *)class_dev->class_data;
  177. if (!sfp) return -EINVAL;
  178. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
  179. if (!cptr) return -EINVAL;
  180. ecnt = pvr2_ctrl_get_cnt(cptr);
  181. bcnt = 0;
  182. for (val = 0; val < ecnt; val++) {
  183. pvr2_ctrl_get_valname(cptr,val,buf+bcnt,PAGE_SIZE-bcnt,&ccnt);
  184. if (!ccnt) continue;
  185. bcnt += ccnt;
  186. if (bcnt >= PAGE_SIZE) break;
  187. buf[bcnt] = '\n';
  188. bcnt++;
  189. }
  190. pvr2_sysfs_trace("pvr2_sysfs(%p) show_enum(cid=%d)",sfp,id);
  191. return bcnt;
  192. }
  193. static ssize_t show_bits(int id,struct class_device *class_dev,char *buf)
  194. {
  195. struct pvr2_ctrl *cptr;
  196. struct pvr2_sysfs *sfp;
  197. int valid_bits,msk;
  198. unsigned int bcnt,ccnt;
  199. sfp = (struct pvr2_sysfs *)class_dev->class_data;
  200. if (!sfp) return -EINVAL;
  201. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
  202. if (!cptr) return -EINVAL;
  203. valid_bits = pvr2_ctrl_get_mask(cptr);
  204. bcnt = 0;
  205. for (msk = 1; valid_bits; msk <<= 1) {
  206. if (!(msk & valid_bits)) continue;
  207. valid_bits &= ~msk;
  208. pvr2_ctrl_get_valname(cptr,msk,buf+bcnt,PAGE_SIZE-bcnt,&ccnt);
  209. bcnt += ccnt;
  210. if (bcnt >= PAGE_SIZE) break;
  211. buf[bcnt] = '\n';
  212. bcnt++;
  213. }
  214. pvr2_sysfs_trace("pvr2_sysfs(%p) show_bits(cid=%d)",sfp,id);
  215. return bcnt;
  216. }
  217. static int store_val_any(int id,int customfl,struct pvr2_sysfs *sfp,
  218. const char *buf,unsigned int count)
  219. {
  220. struct pvr2_ctrl *cptr;
  221. int ret;
  222. int mask,val;
  223. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
  224. if (customfl) {
  225. ret = pvr2_ctrl_custom_sym_to_value(cptr,buf,count,&mask,&val);
  226. } else {
  227. ret = pvr2_ctrl_sym_to_value(cptr,buf,count,&mask,&val);
  228. }
  229. if (ret < 0) return ret;
  230. ret = pvr2_ctrl_set_mask_value(cptr,mask,val);
  231. pvr2_hdw_commit_ctl(sfp->channel.hdw);
  232. return ret;
  233. }
  234. static ssize_t store_val_norm(int id,struct class_device *class_dev,
  235. const char *buf,size_t count)
  236. {
  237. struct pvr2_sysfs *sfp;
  238. int ret;
  239. sfp = (struct pvr2_sysfs *)class_dev->class_data;
  240. ret = store_val_any(id,0,sfp,buf,count);
  241. if (!ret) ret = count;
  242. return ret;
  243. }
  244. static ssize_t store_val_custom(int id,struct class_device *class_dev,
  245. const char *buf,size_t count)
  246. {
  247. struct pvr2_sysfs *sfp;
  248. int ret;
  249. sfp = (struct pvr2_sysfs *)class_dev->class_data;
  250. ret = store_val_any(id,1,sfp,buf,count);
  251. if (!ret) ret = count;
  252. return ret;
  253. }
  254. /*
  255. Mike Isely <isely@pobox.com> 30-April-2005
  256. This next batch of horrible preprocessor hackery is needed because the
  257. kernel's class_device_attribute mechanism fails to pass the actual
  258. attribute through to the show / store functions, which means we have no
  259. way to package up any attribute-specific parameters, like for example the
  260. control id. So we work around this brain-damage by encoding the control
  261. id into the show / store functions themselves and pick the function based
  262. on the control id we're setting up. These macros try to ease the pain.
  263. Yuck.
  264. */
  265. #define CREATE_SHOW_INSTANCE(sf_name,ctl_id) \
  266. static ssize_t sf_name##_##ctl_id(struct class_device *class_dev,char *buf) \
  267. { return sf_name(ctl_id,class_dev,buf); }
  268. #define CREATE_STORE_INSTANCE(sf_name,ctl_id) \
  269. static ssize_t sf_name##_##ctl_id(struct class_device *class_dev,const char *buf,size_t count) \
  270. { return sf_name(ctl_id,class_dev,buf,count); }
  271. #define CREATE_BATCH(ctl_id) \
  272. CREATE_SHOW_INSTANCE(show_name,ctl_id) \
  273. CREATE_SHOW_INSTANCE(show_type,ctl_id) \
  274. CREATE_SHOW_INSTANCE(show_min,ctl_id) \
  275. CREATE_SHOW_INSTANCE(show_max,ctl_id) \
  276. CREATE_SHOW_INSTANCE(show_val_norm,ctl_id) \
  277. CREATE_SHOW_INSTANCE(show_val_custom,ctl_id) \
  278. CREATE_SHOW_INSTANCE(show_enum,ctl_id) \
  279. CREATE_SHOW_INSTANCE(show_bits,ctl_id) \
  280. CREATE_STORE_INSTANCE(store_val_norm,ctl_id) \
  281. CREATE_STORE_INSTANCE(store_val_custom,ctl_id) \
  282. CREATE_BATCH(0)
  283. CREATE_BATCH(1)
  284. CREATE_BATCH(2)
  285. CREATE_BATCH(3)
  286. CREATE_BATCH(4)
  287. CREATE_BATCH(5)
  288. CREATE_BATCH(6)
  289. CREATE_BATCH(7)
  290. CREATE_BATCH(8)
  291. CREATE_BATCH(9)
  292. CREATE_BATCH(10)
  293. CREATE_BATCH(11)
  294. CREATE_BATCH(12)
  295. CREATE_BATCH(13)
  296. CREATE_BATCH(14)
  297. CREATE_BATCH(15)
  298. CREATE_BATCH(16)
  299. CREATE_BATCH(17)
  300. CREATE_BATCH(18)
  301. CREATE_BATCH(19)
  302. CREATE_BATCH(20)
  303. CREATE_BATCH(21)
  304. CREATE_BATCH(22)
  305. CREATE_BATCH(23)
  306. CREATE_BATCH(24)
  307. CREATE_BATCH(25)
  308. CREATE_BATCH(26)
  309. CREATE_BATCH(27)
  310. CREATE_BATCH(28)
  311. CREATE_BATCH(29)
  312. CREATE_BATCH(30)
  313. CREATE_BATCH(31)
  314. CREATE_BATCH(32)
  315. CREATE_BATCH(33)
  316. CREATE_BATCH(34)
  317. CREATE_BATCH(35)
  318. CREATE_BATCH(36)
  319. CREATE_BATCH(37)
  320. CREATE_BATCH(38)
  321. CREATE_BATCH(39)
  322. CREATE_BATCH(40)
  323. CREATE_BATCH(41)
  324. CREATE_BATCH(42)
  325. CREATE_BATCH(43)
  326. CREATE_BATCH(44)
  327. CREATE_BATCH(45)
  328. CREATE_BATCH(46)
  329. CREATE_BATCH(47)
  330. CREATE_BATCH(48)
  331. CREATE_BATCH(49)
  332. CREATE_BATCH(50)
  333. CREATE_BATCH(51)
  334. CREATE_BATCH(52)
  335. CREATE_BATCH(53)
  336. CREATE_BATCH(54)
  337. CREATE_BATCH(55)
  338. CREATE_BATCH(56)
  339. CREATE_BATCH(57)
  340. CREATE_BATCH(58)
  341. CREATE_BATCH(59)
  342. struct pvr2_sysfs_func_set {
  343. ssize_t (*show_name)(struct class_device *,char *);
  344. ssize_t (*show_type)(struct class_device *,char *);
  345. ssize_t (*show_min)(struct class_device *,char *);
  346. ssize_t (*show_max)(struct class_device *,char *);
  347. ssize_t (*show_enum)(struct class_device *,char *);
  348. ssize_t (*show_bits)(struct class_device *,char *);
  349. ssize_t (*show_val_norm)(struct class_device *,char *);
  350. ssize_t (*store_val_norm)(struct class_device *,
  351. const char *,size_t);
  352. ssize_t (*show_val_custom)(struct class_device *,char *);
  353. ssize_t (*store_val_custom)(struct class_device *,
  354. const char *,size_t);
  355. };
  356. #define INIT_BATCH(ctl_id) \
  357. [ctl_id] = { \
  358. .show_name = show_name_##ctl_id, \
  359. .show_type = show_type_##ctl_id, \
  360. .show_min = show_min_##ctl_id, \
  361. .show_max = show_max_##ctl_id, \
  362. .show_enum = show_enum_##ctl_id, \
  363. .show_bits = show_bits_##ctl_id, \
  364. .show_val_norm = show_val_norm_##ctl_id, \
  365. .store_val_norm = store_val_norm_##ctl_id, \
  366. .show_val_custom = show_val_custom_##ctl_id, \
  367. .store_val_custom = store_val_custom_##ctl_id, \
  368. } \
  369. static struct pvr2_sysfs_func_set funcs[] = {
  370. INIT_BATCH(0),
  371. INIT_BATCH(1),
  372. INIT_BATCH(2),
  373. INIT_BATCH(3),
  374. INIT_BATCH(4),
  375. INIT_BATCH(5),
  376. INIT_BATCH(6),
  377. INIT_BATCH(7),
  378. INIT_BATCH(8),
  379. INIT_BATCH(9),
  380. INIT_BATCH(10),
  381. INIT_BATCH(11),
  382. INIT_BATCH(12),
  383. INIT_BATCH(13),
  384. INIT_BATCH(14),
  385. INIT_BATCH(15),
  386. INIT_BATCH(16),
  387. INIT_BATCH(17),
  388. INIT_BATCH(18),
  389. INIT_BATCH(19),
  390. INIT_BATCH(20),
  391. INIT_BATCH(21),
  392. INIT_BATCH(22),
  393. INIT_BATCH(23),
  394. INIT_BATCH(24),
  395. INIT_BATCH(25),
  396. INIT_BATCH(26),
  397. INIT_BATCH(27),
  398. INIT_BATCH(28),
  399. INIT_BATCH(29),
  400. INIT_BATCH(30),
  401. INIT_BATCH(31),
  402. INIT_BATCH(32),
  403. INIT_BATCH(33),
  404. INIT_BATCH(34),
  405. INIT_BATCH(35),
  406. INIT_BATCH(36),
  407. INIT_BATCH(37),
  408. INIT_BATCH(38),
  409. INIT_BATCH(39),
  410. INIT_BATCH(40),
  411. INIT_BATCH(41),
  412. INIT_BATCH(42),
  413. INIT_BATCH(43),
  414. INIT_BATCH(44),
  415. INIT_BATCH(45),
  416. INIT_BATCH(46),
  417. INIT_BATCH(47),
  418. INIT_BATCH(48),
  419. INIT_BATCH(49),
  420. INIT_BATCH(50),
  421. INIT_BATCH(51),
  422. INIT_BATCH(52),
  423. INIT_BATCH(53),
  424. INIT_BATCH(54),
  425. INIT_BATCH(55),
  426. INIT_BATCH(56),
  427. INIT_BATCH(57),
  428. INIT_BATCH(58),
  429. INIT_BATCH(59),
  430. };
  431. static void pvr2_sysfs_add_control(struct pvr2_sysfs *sfp,int ctl_id)
  432. {
  433. struct pvr2_sysfs_ctl_item *cip;
  434. struct pvr2_sysfs_func_set *fp;
  435. struct pvr2_ctrl *cptr;
  436. unsigned int cnt,acnt;
  437. if ((ctl_id < 0) || (ctl_id >= (sizeof(funcs)/sizeof(funcs[0])))) {
  438. return;
  439. }
  440. fp = funcs + ctl_id;
  441. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,ctl_id);
  442. if (!cptr) return;
  443. cip = kmalloc(sizeof(*cip),GFP_KERNEL);
  444. if (!cip) return;
  445. memset(cip,0,sizeof(*cip));
  446. pvr2_sysfs_trace("Creating pvr2_sysfs_ctl_item id=%p",cip);
  447. cip->cptr = cptr;
  448. cip->chptr = sfp;
  449. cip->item_next = 0;
  450. if (sfp->item_last) {
  451. sfp->item_last->item_next = cip;
  452. } else {
  453. sfp->item_first = cip;
  454. }
  455. sfp->item_last = cip;
  456. cip->attr_name.attr.owner = THIS_MODULE;
  457. cip->attr_name.attr.name = "name";
  458. cip->attr_name.attr.mode = S_IRUGO;
  459. cip->attr_name.show = fp->show_name;
  460. cip->attr_type.attr.owner = THIS_MODULE;
  461. cip->attr_type.attr.name = "type";
  462. cip->attr_type.attr.mode = S_IRUGO;
  463. cip->attr_type.show = fp->show_type;
  464. cip->attr_min.attr.owner = THIS_MODULE;
  465. cip->attr_min.attr.name = "min_val";
  466. cip->attr_min.attr.mode = S_IRUGO;
  467. cip->attr_min.show = fp->show_min;
  468. cip->attr_max.attr.owner = THIS_MODULE;
  469. cip->attr_max.attr.name = "max_val";
  470. cip->attr_max.attr.mode = S_IRUGO;
  471. cip->attr_max.show = fp->show_max;
  472. cip->attr_val.attr.owner = THIS_MODULE;
  473. cip->attr_val.attr.name = "cur_val";
  474. cip->attr_val.attr.mode = S_IRUGO;
  475. cip->attr_custom.attr.owner = THIS_MODULE;
  476. cip->attr_custom.attr.name = "custom_val";
  477. cip->attr_custom.attr.mode = S_IRUGO;
  478. cip->attr_enum.attr.owner = THIS_MODULE;
  479. cip->attr_enum.attr.name = "enum_val";
  480. cip->attr_enum.attr.mode = S_IRUGO;
  481. cip->attr_enum.show = fp->show_enum;
  482. cip->attr_bits.attr.owner = THIS_MODULE;
  483. cip->attr_bits.attr.name = "bit_val";
  484. cip->attr_bits.attr.mode = S_IRUGO;
  485. cip->attr_bits.show = fp->show_bits;
  486. if (pvr2_ctrl_is_writable(cptr)) {
  487. cip->attr_val.attr.mode |= S_IWUSR|S_IWGRP;
  488. cip->attr_custom.attr.mode |= S_IWUSR|S_IWGRP;
  489. }
  490. acnt = 0;
  491. cip->attr_gen[acnt++] = &cip->attr_name.attr;
  492. cip->attr_gen[acnt++] = &cip->attr_type.attr;
  493. cip->attr_gen[acnt++] = &cip->attr_val.attr;
  494. cip->attr_val.show = fp->show_val_norm;
  495. cip->attr_val.store = fp->store_val_norm;
  496. if (pvr2_ctrl_has_custom_symbols(cptr)) {
  497. cip->attr_gen[acnt++] = &cip->attr_custom.attr;
  498. cip->attr_custom.show = fp->show_val_custom;
  499. cip->attr_custom.store = fp->store_val_custom;
  500. }
  501. switch (pvr2_ctrl_get_type(cptr)) {
  502. case pvr2_ctl_enum:
  503. // Control is an enumeration
  504. cip->attr_gen[acnt++] = &cip->attr_enum.attr;
  505. break;
  506. case pvr2_ctl_int:
  507. // Control is an integer
  508. cip->attr_gen[acnt++] = &cip->attr_min.attr;
  509. cip->attr_gen[acnt++] = &cip->attr_max.attr;
  510. break;
  511. case pvr2_ctl_bitmask:
  512. // Control is an bitmask
  513. cip->attr_gen[acnt++] = &cip->attr_bits.attr;
  514. break;
  515. default: break;
  516. }
  517. cnt = scnprintf(cip->name,sizeof(cip->name)-1,"ctl_%s",
  518. pvr2_ctrl_get_name(cptr));
  519. cip->name[cnt] = 0;
  520. cip->grp.name = cip->name;
  521. cip->grp.attrs = cip->attr_gen;
  522. sysfs_create_group(&sfp->class_dev->kobj,&cip->grp);
  523. }
  524. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  525. static ssize_t debuginfo_show(struct class_device *,char *);
  526. static ssize_t debugcmd_show(struct class_device *,char *);
  527. static ssize_t debugcmd_store(struct class_device *,const char *,size_t count);
  528. static void pvr2_sysfs_add_debugifc(struct pvr2_sysfs *sfp)
  529. {
  530. struct pvr2_sysfs_debugifc *dip;
  531. dip = kmalloc(sizeof(*dip),GFP_KERNEL);
  532. if (!dip) return;
  533. memset(dip,0,sizeof(*dip));
  534. dip->attr_debugcmd.attr.owner = THIS_MODULE;
  535. dip->attr_debugcmd.attr.name = "debugcmd";
  536. dip->attr_debugcmd.attr.mode = S_IRUGO|S_IWUSR|S_IWGRP;
  537. dip->attr_debugcmd.show = debugcmd_show;
  538. dip->attr_debugcmd.store = debugcmd_store;
  539. dip->attr_debuginfo.attr.owner = THIS_MODULE;
  540. dip->attr_debuginfo.attr.name = "debuginfo";
  541. dip->attr_debuginfo.attr.mode = S_IRUGO;
  542. dip->attr_debuginfo.show = debuginfo_show;
  543. sfp->debugifc = dip;
  544. class_device_create_file(sfp->class_dev,&dip->attr_debugcmd);
  545. class_device_create_file(sfp->class_dev,&dip->attr_debuginfo);
  546. }
  547. static void pvr2_sysfs_tear_down_debugifc(struct pvr2_sysfs *sfp)
  548. {
  549. if (!sfp->debugifc) return;
  550. class_device_remove_file(sfp->class_dev,
  551. &sfp->debugifc->attr_debuginfo);
  552. class_device_remove_file(sfp->class_dev,&sfp->debugifc->attr_debugcmd);
  553. kfree(sfp->debugifc);
  554. sfp->debugifc = 0;
  555. }
  556. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  557. static void pvr2_sysfs_add_controls(struct pvr2_sysfs *sfp)
  558. {
  559. unsigned int idx,cnt;
  560. cnt = pvr2_hdw_get_ctrl_count(sfp->channel.hdw);
  561. for (idx = 0; idx < cnt; idx++) {
  562. pvr2_sysfs_add_control(sfp,idx);
  563. }
  564. }
  565. static void pvr2_sysfs_tear_down_controls(struct pvr2_sysfs *sfp)
  566. {
  567. struct pvr2_sysfs_ctl_item *cip1,*cip2;
  568. for (cip1 = sfp->item_first; cip1; cip1 = cip2) {
  569. cip2 = cip1->item_next;
  570. sysfs_remove_group(&sfp->class_dev->kobj,&cip1->grp);
  571. pvr2_sysfs_trace("Destroying pvr2_sysfs_ctl_item id=%p",cip1);
  572. kfree(cip1);
  573. }
  574. }
  575. static void pvr2_sysfs_class_release(struct class *class)
  576. {
  577. struct pvr2_sysfs_class *clp;
  578. clp = container_of(class,struct pvr2_sysfs_class,class);
  579. pvr2_sysfs_trace("Destroying pvr2_sysfs_class id=%p",clp);
  580. kfree(clp);
  581. }
  582. static void pvr2_sysfs_release(struct class_device *class_dev)
  583. {
  584. pvr2_sysfs_trace("Releasing class_dev id=%p",class_dev);
  585. kfree(class_dev);
  586. }
  587. static void class_dev_destroy(struct pvr2_sysfs *sfp)
  588. {
  589. if (!sfp->class_dev) return;
  590. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  591. pvr2_sysfs_tear_down_debugifc(sfp);
  592. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  593. pvr2_sysfs_tear_down_controls(sfp);
  594. class_device_remove_file(sfp->class_dev,&sfp->attr_v4l_minor_number);
  595. class_device_remove_file(sfp->class_dev,&sfp->attr_unit_number);
  596. pvr2_sysfs_trace("Destroying class_dev id=%p",sfp->class_dev);
  597. sfp->class_dev->class_data = 0;
  598. class_device_unregister(sfp->class_dev);
  599. sfp->class_dev = 0;
  600. }
  601. static ssize_t v4l_minor_number_show(struct class_device *class_dev,char *buf)
  602. {
  603. struct pvr2_sysfs *sfp;
  604. sfp = (struct pvr2_sysfs *)class_dev->class_data;
  605. if (!sfp) return -EINVAL;
  606. return scnprintf(buf,PAGE_SIZE,"%d\n",
  607. pvr2_hdw_v4l_get_minor_number(sfp->channel.hdw));
  608. }
  609. static ssize_t unit_number_show(struct class_device *class_dev,char *buf)
  610. {
  611. struct pvr2_sysfs *sfp;
  612. sfp = (struct pvr2_sysfs *)class_dev->class_data;
  613. if (!sfp) return -EINVAL;
  614. return scnprintf(buf,PAGE_SIZE,"%d\n",
  615. pvr2_hdw_get_unit_number(sfp->channel.hdw));
  616. }
  617. static void class_dev_create(struct pvr2_sysfs *sfp,
  618. struct pvr2_sysfs_class *class_ptr)
  619. {
  620. struct usb_device *usb_dev;
  621. struct class_device *class_dev;
  622. usb_dev = pvr2_hdw_get_dev(sfp->channel.hdw);
  623. if (!usb_dev) return;
  624. class_dev = kmalloc(sizeof(*class_dev),GFP_KERNEL);
  625. if (!class_dev) return;
  626. memset(class_dev,0,sizeof(*class_dev));
  627. pvr2_sysfs_trace("Creating class_dev id=%p",class_dev);
  628. class_dev->class = &class_ptr->class;
  629. if (pvr2_hdw_get_sn(sfp->channel.hdw)) {
  630. snprintf(class_dev->class_id,BUS_ID_SIZE,"sn-%lu",
  631. pvr2_hdw_get_sn(sfp->channel.hdw));
  632. } else if (pvr2_hdw_get_unit_number(sfp->channel.hdw) >= 0) {
  633. snprintf(class_dev->class_id,BUS_ID_SIZE,"unit-%c",
  634. pvr2_hdw_get_unit_number(sfp->channel.hdw) + 'a');
  635. } else {
  636. kfree(class_dev);
  637. return;
  638. }
  639. class_dev->dev = &usb_dev->dev;
  640. sfp->class_dev = class_dev;
  641. class_dev->class_data = sfp;
  642. class_device_register(class_dev);
  643. sfp->attr_v4l_minor_number.attr.owner = THIS_MODULE;
  644. sfp->attr_v4l_minor_number.attr.name = "v4l_minor_number";
  645. sfp->attr_v4l_minor_number.attr.mode = S_IRUGO;
  646. sfp->attr_v4l_minor_number.show = v4l_minor_number_show;
  647. sfp->attr_v4l_minor_number.store = 0;
  648. class_device_create_file(sfp->class_dev,&sfp->attr_v4l_minor_number);
  649. sfp->attr_unit_number.attr.owner = THIS_MODULE;
  650. sfp->attr_unit_number.attr.name = "unit_number";
  651. sfp->attr_unit_number.attr.mode = S_IRUGO;
  652. sfp->attr_unit_number.show = unit_number_show;
  653. sfp->attr_unit_number.store = 0;
  654. class_device_create_file(sfp->class_dev,&sfp->attr_unit_number);
  655. pvr2_sysfs_add_controls(sfp);
  656. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  657. pvr2_sysfs_add_debugifc(sfp);
  658. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  659. }
  660. static void pvr2_sysfs_internal_check(struct pvr2_channel *chp)
  661. {
  662. struct pvr2_sysfs *sfp;
  663. sfp = container_of(chp,struct pvr2_sysfs,channel);
  664. if (!sfp->channel.mc_head->disconnect_flag) return;
  665. pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_sysfs id=%p",sfp);
  666. class_dev_destroy(sfp);
  667. pvr2_channel_done(&sfp->channel);
  668. kfree(sfp);
  669. }
  670. struct pvr2_sysfs *pvr2_sysfs_create(struct pvr2_context *mp,
  671. struct pvr2_sysfs_class *class_ptr)
  672. {
  673. struct pvr2_sysfs *sfp;
  674. sfp = kmalloc(sizeof(*sfp),GFP_KERNEL);
  675. if (!sfp) return sfp;
  676. memset(sfp,0,sizeof(*sfp));
  677. pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_sysfs id=%p",sfp);
  678. pvr2_channel_init(&sfp->channel,mp);
  679. sfp->channel.check_func = pvr2_sysfs_internal_check;
  680. class_dev_create(sfp,class_ptr);
  681. return sfp;
  682. }
  683. static int pvr2_sysfs_hotplug(struct class_device *cd,char **envp,
  684. int numenvp,char *buf,int size)
  685. {
  686. /* Even though we don't do anything here, we still need this function
  687. because sysfs will still try to call it. */
  688. return 0;
  689. }
  690. struct pvr2_sysfs_class *pvr2_sysfs_class_create(void)
  691. {
  692. struct pvr2_sysfs_class *clp;
  693. clp = kmalloc(sizeof(*clp),GFP_KERNEL);
  694. if (!clp) return clp;
  695. memset(clp,0,sizeof(*clp));
  696. pvr2_sysfs_trace("Creating pvr2_sysfs_class id=%p",clp);
  697. clp->class.name = "pvrusb2";
  698. clp->class.class_release = pvr2_sysfs_class_release;
  699. clp->class.release = pvr2_sysfs_release;
  700. clp->class.uevent = pvr2_sysfs_hotplug;
  701. if (class_register(&clp->class)) {
  702. pvr2_sysfs_trace(
  703. "Registration failed for pvr2_sysfs_class id=%p",clp);
  704. kfree(clp);
  705. clp = 0;
  706. }
  707. return clp;
  708. }
  709. void pvr2_sysfs_class_destroy(struct pvr2_sysfs_class *clp)
  710. {
  711. class_unregister(&clp->class);
  712. }
  713. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  714. static ssize_t debuginfo_show(struct class_device *class_dev,char *buf)
  715. {
  716. struct pvr2_sysfs *sfp;
  717. sfp = (struct pvr2_sysfs *)class_dev->class_data;
  718. if (!sfp) return -EINVAL;
  719. pvr2_hdw_trigger_module_log(sfp->channel.hdw);
  720. return pvr2_debugifc_print_info(sfp->channel.hdw,buf,PAGE_SIZE);
  721. }
  722. static ssize_t debugcmd_show(struct class_device *class_dev,char *buf)
  723. {
  724. struct pvr2_sysfs *sfp;
  725. sfp = (struct pvr2_sysfs *)class_dev->class_data;
  726. if (!sfp) return -EINVAL;
  727. return pvr2_debugifc_print_status(sfp->channel.hdw,buf,PAGE_SIZE);
  728. }
  729. static ssize_t debugcmd_store(struct class_device *class_dev,
  730. const char *buf,size_t count)
  731. {
  732. struct pvr2_sysfs *sfp;
  733. int ret;
  734. sfp = (struct pvr2_sysfs *)class_dev->class_data;
  735. if (!sfp) return -EINVAL;
  736. ret = pvr2_debugifc_docmd(sfp->channel.hdw,buf,count);
  737. if (ret < 0) return ret;
  738. return count;
  739. }
  740. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  741. /*
  742. Stuff for Emacs to see, in order to encourage consistent editing style:
  743. *** Local Variables: ***
  744. *** mode: c ***
  745. *** fill-column: 75 ***
  746. *** tab-width: 8 ***
  747. *** c-basic-offset: 8 ***
  748. *** End: ***
  749. */