pvrusb2-sysfs.c 26 KB

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