pvrusb2-sysfs.c 24 KB

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