pvrusb2-sysfs.c 26 KB

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