pvrusb2-sysfs.c 27 KB

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