pvrusb2-sysfs.c 24 KB

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