pvrusb2-sysfs.c 28 KB

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