pvrusb2-sysfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /*
  2. *
  3. *
  4. * Copyright (C) 2005 Mike Isely <isely@pobox.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. #include <linux/string.h>
  21. #include <linux/slab.h>
  22. #include "pvrusb2-sysfs.h"
  23. #include "pvrusb2-hdw.h"
  24. #include "pvrusb2-debug.h"
  25. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  26. #include "pvrusb2-debugifc.h"
  27. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  28. #define pvr2_sysfs_trace(...) pvr2_trace(PVR2_TRACE_SYSFS,__VA_ARGS__)
  29. struct pvr2_sysfs {
  30. struct pvr2_channel channel;
  31. struct device *class_dev;
  32. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  33. struct pvr2_sysfs_debugifc *debugifc;
  34. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  35. struct pvr2_sysfs_ctl_item *item_first;
  36. struct pvr2_sysfs_ctl_item *item_last;
  37. struct device_attribute attr_v4l_minor_number;
  38. struct device_attribute attr_v4l_radio_minor_number;
  39. struct device_attribute attr_unit_number;
  40. struct device_attribute attr_bus_info;
  41. struct device_attribute attr_hdw_name;
  42. struct device_attribute attr_hdw_desc;
  43. int v4l_minor_number_created_ok;
  44. int v4l_radio_minor_number_created_ok;
  45. int unit_number_created_ok;
  46. int bus_info_created_ok;
  47. int hdw_name_created_ok;
  48. int hdw_desc_created_ok;
  49. };
  50. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  51. struct pvr2_sysfs_debugifc {
  52. struct device_attribute attr_debugcmd;
  53. struct device_attribute attr_debuginfo;
  54. int debugcmd_created_ok;
  55. int debuginfo_created_ok;
  56. };
  57. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  58. struct pvr2_sysfs_ctl_item {
  59. struct device_attribute attr_name;
  60. struct device_attribute attr_type;
  61. struct device_attribute attr_min;
  62. struct device_attribute attr_max;
  63. struct device_attribute attr_def;
  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. int ctl_id;
  70. struct pvr2_sysfs *chptr;
  71. struct pvr2_sysfs_ctl_item *item_next;
  72. struct attribute *attr_gen[8];
  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(struct device *class_dev,
  81. struct device_attribute *attr,
  82. char *buf)
  83. {
  84. struct pvr2_sysfs_ctl_item *cip;
  85. const char *name;
  86. cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_name);
  87. name = pvr2_ctrl_get_desc(cip->cptr);
  88. pvr2_sysfs_trace("pvr2_sysfs(%p) show_name(cid=%d) is %s",
  89. cip->chptr, cip->ctl_id, name);
  90. if (!name) return -EINVAL;
  91. return scnprintf(buf, PAGE_SIZE, "%s\n", name);
  92. }
  93. static ssize_t show_type(struct device *class_dev,
  94. struct device_attribute *attr,
  95. char *buf)
  96. {
  97. struct pvr2_sysfs_ctl_item *cip;
  98. const char *name;
  99. enum pvr2_ctl_type tp;
  100. cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_type);
  101. tp = pvr2_ctrl_get_type(cip->cptr);
  102. switch (tp) {
  103. case pvr2_ctl_int: name = "integer"; break;
  104. case pvr2_ctl_enum: name = "enum"; break;
  105. case pvr2_ctl_bitmask: name = "bitmask"; break;
  106. case pvr2_ctl_bool: name = "boolean"; break;
  107. default: name = "?"; break;
  108. }
  109. pvr2_sysfs_trace("pvr2_sysfs(%p) show_type(cid=%d) is %s",
  110. cip->chptr, cip->ctl_id, name);
  111. if (!name) return -EINVAL;
  112. return scnprintf(buf, PAGE_SIZE, "%s\n", name);
  113. }
  114. static ssize_t show_min(struct device *class_dev,
  115. struct device_attribute *attr,
  116. char *buf)
  117. {
  118. struct pvr2_sysfs_ctl_item *cip;
  119. long val;
  120. cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_min);
  121. val = pvr2_ctrl_get_min(cip->cptr);
  122. pvr2_sysfs_trace("pvr2_sysfs(%p) show_min(cid=%d) is %ld",
  123. cip->chptr, cip->ctl_id, val);
  124. return scnprintf(buf, PAGE_SIZE, "%ld\n", val);
  125. }
  126. static ssize_t show_max(struct device *class_dev,
  127. struct device_attribute *attr,
  128. char *buf)
  129. {
  130. struct pvr2_sysfs_ctl_item *cip;
  131. long val;
  132. cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_max);
  133. val = pvr2_ctrl_get_max(cip->cptr);
  134. pvr2_sysfs_trace("pvr2_sysfs(%p) show_max(cid=%d) is %ld",
  135. cip->chptr, cip->ctl_id, val);
  136. return scnprintf(buf, PAGE_SIZE, "%ld\n", val);
  137. }
  138. static ssize_t show_def(struct device *class_dev,
  139. struct device_attribute *attr,
  140. char *buf)
  141. {
  142. struct pvr2_sysfs_ctl_item *cip;
  143. int val;
  144. int ret;
  145. unsigned int cnt = 0;
  146. cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_def);
  147. ret = pvr2_ctrl_get_def(cip->cptr, &val);
  148. if (ret < 0) return ret;
  149. ret = pvr2_ctrl_value_to_sym(cip->cptr, ~0, val,
  150. buf, PAGE_SIZE - 1, &cnt);
  151. pvr2_sysfs_trace("pvr2_sysfs(%p) show_def(cid=%d) is %.*s (%d)",
  152. cip->chptr, cip->ctl_id, cnt, buf, val);
  153. buf[cnt] = '\n';
  154. return cnt + 1;
  155. }
  156. static ssize_t show_val_norm(struct device *class_dev,
  157. struct device_attribute *attr,
  158. char *buf)
  159. {
  160. struct pvr2_sysfs_ctl_item *cip;
  161. int val;
  162. int ret;
  163. unsigned int cnt = 0;
  164. cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_val);
  165. ret = pvr2_ctrl_get_value(cip->cptr, &val);
  166. if (ret < 0) return ret;
  167. ret = pvr2_ctrl_value_to_sym(cip->cptr, ~0, val,
  168. buf, PAGE_SIZE - 1, &cnt);
  169. pvr2_sysfs_trace("pvr2_sysfs(%p) show_val_norm(cid=%d) is %.*s (%d)",
  170. cip->chptr, cip->ctl_id, cnt, buf, val);
  171. buf[cnt] = '\n';
  172. return cnt+1;
  173. }
  174. static ssize_t show_val_custom(struct device *class_dev,
  175. struct device_attribute *attr,
  176. char *buf)
  177. {
  178. struct pvr2_sysfs_ctl_item *cip;
  179. int val;
  180. int ret;
  181. unsigned int cnt = 0;
  182. cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_custom);
  183. ret = pvr2_ctrl_get_value(cip->cptr, &val);
  184. if (ret < 0) return ret;
  185. ret = pvr2_ctrl_custom_value_to_sym(cip->cptr, ~0, val,
  186. buf, PAGE_SIZE - 1, &cnt);
  187. pvr2_sysfs_trace("pvr2_sysfs(%p) show_val_custom(cid=%d) is %.*s (%d)",
  188. cip->chptr, cip->ctl_id, cnt, buf, val);
  189. buf[cnt] = '\n';
  190. return cnt+1;
  191. }
  192. static ssize_t show_enum(struct device *class_dev,
  193. struct device_attribute *attr,
  194. char *buf)
  195. {
  196. struct pvr2_sysfs_ctl_item *cip;
  197. long val;
  198. unsigned int bcnt, ccnt, ecnt;
  199. cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_enum);
  200. ecnt = pvr2_ctrl_get_cnt(cip->cptr);
  201. bcnt = 0;
  202. for (val = 0; val < ecnt; val++) {
  203. pvr2_ctrl_get_valname(cip->cptr, val, buf + bcnt,
  204. PAGE_SIZE - bcnt, &ccnt);
  205. if (!ccnt) continue;
  206. bcnt += ccnt;
  207. if (bcnt >= PAGE_SIZE) break;
  208. buf[bcnt] = '\n';
  209. bcnt++;
  210. }
  211. pvr2_sysfs_trace("pvr2_sysfs(%p) show_enum(cid=%d)",
  212. cip->chptr, cip->ctl_id);
  213. return bcnt;
  214. }
  215. static ssize_t show_bits(struct device *class_dev,
  216. struct device_attribute *attr,
  217. char *buf)
  218. {
  219. struct pvr2_sysfs_ctl_item *cip;
  220. int valid_bits, msk;
  221. unsigned int bcnt, ccnt;
  222. cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_bits);
  223. valid_bits = pvr2_ctrl_get_mask(cip->cptr);
  224. bcnt = 0;
  225. for (msk = 1; valid_bits; msk <<= 1) {
  226. if (!(msk & valid_bits)) continue;
  227. valid_bits &= ~msk;
  228. pvr2_ctrl_get_valname(cip->cptr, msk, buf + bcnt,
  229. PAGE_SIZE - bcnt, &ccnt);
  230. bcnt += ccnt;
  231. if (bcnt >= PAGE_SIZE) break;
  232. buf[bcnt] = '\n';
  233. bcnt++;
  234. }
  235. pvr2_sysfs_trace("pvr2_sysfs(%p) show_bits(cid=%d)",
  236. cip->chptr, cip->ctl_id);
  237. return bcnt;
  238. }
  239. static int store_val_any(struct pvr2_sysfs_ctl_item *cip, int customfl,
  240. const char *buf,unsigned int count)
  241. {
  242. int ret;
  243. int mask,val;
  244. if (customfl) {
  245. ret = pvr2_ctrl_custom_sym_to_value(cip->cptr, buf, count,
  246. &mask, &val);
  247. } else {
  248. ret = pvr2_ctrl_sym_to_value(cip->cptr, buf, count,
  249. &mask, &val);
  250. }
  251. if (ret < 0) return ret;
  252. ret = pvr2_ctrl_set_mask_value(cip->cptr, mask, val);
  253. pvr2_hdw_commit_ctl(cip->chptr->channel.hdw);
  254. return ret;
  255. }
  256. static ssize_t store_val_norm(struct device *class_dev,
  257. struct device_attribute *attr,
  258. const char *buf, size_t count)
  259. {
  260. struct pvr2_sysfs_ctl_item *cip;
  261. int ret;
  262. cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_val);
  263. pvr2_sysfs_trace("pvr2_sysfs(%p) store_val_norm(cid=%d) \"%.*s\"",
  264. cip->chptr, cip->ctl_id, (int)count, buf);
  265. ret = store_val_any(cip, 0, buf, count);
  266. if (!ret) ret = count;
  267. return ret;
  268. }
  269. static ssize_t store_val_custom(struct device *class_dev,
  270. struct device_attribute *attr,
  271. const char *buf, size_t count)
  272. {
  273. struct pvr2_sysfs_ctl_item *cip;
  274. int ret;
  275. cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_custom);
  276. pvr2_sysfs_trace("pvr2_sysfs(%p) store_val_custom(cid=%d) \"%.*s\"",
  277. cip->chptr, cip->ctl_id, (int)count, buf);
  278. ret = store_val_any(cip, 1, buf, count);
  279. if (!ret) ret = count;
  280. return ret;
  281. }
  282. static void pvr2_sysfs_add_control(struct pvr2_sysfs *sfp,int ctl_id)
  283. {
  284. struct pvr2_sysfs_ctl_item *cip;
  285. struct pvr2_ctrl *cptr;
  286. unsigned int cnt,acnt;
  287. int ret;
  288. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,ctl_id);
  289. if (!cptr) return;
  290. cip = kzalloc(sizeof(*cip),GFP_KERNEL);
  291. if (!cip) return;
  292. pvr2_sysfs_trace("Creating pvr2_sysfs_ctl_item id=%p",cip);
  293. cip->cptr = cptr;
  294. cip->ctl_id = ctl_id;
  295. cip->chptr = sfp;
  296. cip->item_next = NULL;
  297. if (sfp->item_last) {
  298. sfp->item_last->item_next = cip;
  299. } else {
  300. sfp->item_first = cip;
  301. }
  302. sfp->item_last = cip;
  303. sysfs_attr_init(&cip->attr_name.attr);
  304. cip->attr_name.attr.name = "name";
  305. cip->attr_name.attr.mode = S_IRUGO;
  306. cip->attr_name.show = show_name;
  307. sysfs_attr_init(&cip->attr_type.attr);
  308. cip->attr_type.attr.name = "type";
  309. cip->attr_type.attr.mode = S_IRUGO;
  310. cip->attr_type.show = show_type;
  311. sysfs_attr_init(&cip->attr_min.attr);
  312. cip->attr_min.attr.name = "min_val";
  313. cip->attr_min.attr.mode = S_IRUGO;
  314. cip->attr_min.show = show_min;
  315. sysfs_attr_init(&cip->attr_max.attr);
  316. cip->attr_max.attr.name = "max_val";
  317. cip->attr_max.attr.mode = S_IRUGO;
  318. cip->attr_max.show = show_max;
  319. sysfs_attr_init(&cip->attr_def.attr);
  320. cip->attr_def.attr.name = "def_val";
  321. cip->attr_def.attr.mode = S_IRUGO;
  322. cip->attr_def.show = show_def;
  323. sysfs_attr_init(&cip->attr_val.attr);
  324. cip->attr_val.attr.name = "cur_val";
  325. cip->attr_val.attr.mode = S_IRUGO;
  326. sysfs_attr_init(&cip->attr_custom.attr);
  327. cip->attr_custom.attr.name = "custom_val";
  328. cip->attr_custom.attr.mode = S_IRUGO;
  329. sysfs_attr_init(&cip->attr_enum.attr);
  330. cip->attr_enum.attr.name = "enum_val";
  331. cip->attr_enum.attr.mode = S_IRUGO;
  332. cip->attr_enum.show = show_enum;
  333. sysfs_attr_init(&cip->attr_bits.attr);
  334. cip->attr_bits.attr.name = "bit_val";
  335. cip->attr_bits.attr.mode = S_IRUGO;
  336. cip->attr_bits.show = show_bits;
  337. if (pvr2_ctrl_is_writable(cptr)) {
  338. cip->attr_val.attr.mode |= S_IWUSR|S_IWGRP;
  339. cip->attr_custom.attr.mode |= S_IWUSR|S_IWGRP;
  340. }
  341. acnt = 0;
  342. cip->attr_gen[acnt++] = &cip->attr_name.attr;
  343. cip->attr_gen[acnt++] = &cip->attr_type.attr;
  344. cip->attr_gen[acnt++] = &cip->attr_val.attr;
  345. cip->attr_gen[acnt++] = &cip->attr_def.attr;
  346. cip->attr_val.show = show_val_norm;
  347. cip->attr_val.store = store_val_norm;
  348. if (pvr2_ctrl_has_custom_symbols(cptr)) {
  349. cip->attr_gen[acnt++] = &cip->attr_custom.attr;
  350. cip->attr_custom.show = show_val_custom;
  351. cip->attr_custom.store = store_val_custom;
  352. }
  353. switch (pvr2_ctrl_get_type(cptr)) {
  354. case pvr2_ctl_enum:
  355. // Control is an enumeration
  356. cip->attr_gen[acnt++] = &cip->attr_enum.attr;
  357. break;
  358. case pvr2_ctl_int:
  359. // Control is an integer
  360. cip->attr_gen[acnt++] = &cip->attr_min.attr;
  361. cip->attr_gen[acnt++] = &cip->attr_max.attr;
  362. break;
  363. case pvr2_ctl_bitmask:
  364. // Control is an bitmask
  365. cip->attr_gen[acnt++] = &cip->attr_bits.attr;
  366. break;
  367. default: break;
  368. }
  369. cnt = scnprintf(cip->name,sizeof(cip->name)-1,"ctl_%s",
  370. pvr2_ctrl_get_name(cptr));
  371. cip->name[cnt] = 0;
  372. cip->grp.name = cip->name;
  373. cip->grp.attrs = cip->attr_gen;
  374. ret = sysfs_create_group(&sfp->class_dev->kobj,&cip->grp);
  375. if (ret) {
  376. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  377. "sysfs_create_group error: %d",
  378. ret);
  379. return;
  380. }
  381. cip->created_ok = !0;
  382. }
  383. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  384. static ssize_t debuginfo_show(struct device *, struct device_attribute *,
  385. char *);
  386. static ssize_t debugcmd_show(struct device *, struct device_attribute *,
  387. char *);
  388. static ssize_t debugcmd_store(struct device *, struct device_attribute *,
  389. const char *, size_t count);
  390. static void pvr2_sysfs_add_debugifc(struct pvr2_sysfs *sfp)
  391. {
  392. struct pvr2_sysfs_debugifc *dip;
  393. int ret;
  394. dip = kzalloc(sizeof(*dip),GFP_KERNEL);
  395. if (!dip) return;
  396. sysfs_attr_init(&dip->attr_debugcmd.attr);
  397. dip->attr_debugcmd.attr.name = "debugcmd";
  398. dip->attr_debugcmd.attr.mode = S_IRUGO|S_IWUSR|S_IWGRP;
  399. dip->attr_debugcmd.show = debugcmd_show;
  400. dip->attr_debugcmd.store = debugcmd_store;
  401. sysfs_attr_init(&dip->attr_debuginfo.attr);
  402. dip->attr_debuginfo.attr.name = "debuginfo";
  403. dip->attr_debuginfo.attr.mode = S_IRUGO;
  404. dip->attr_debuginfo.show = debuginfo_show;
  405. sfp->debugifc = dip;
  406. ret = device_create_file(sfp->class_dev,&dip->attr_debugcmd);
  407. if (ret < 0) {
  408. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  409. "device_create_file error: %d",
  410. ret);
  411. } else {
  412. dip->debugcmd_created_ok = !0;
  413. }
  414. ret = device_create_file(sfp->class_dev,&dip->attr_debuginfo);
  415. if (ret < 0) {
  416. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  417. "device_create_file error: %d",
  418. ret);
  419. } else {
  420. dip->debuginfo_created_ok = !0;
  421. }
  422. }
  423. static void pvr2_sysfs_tear_down_debugifc(struct pvr2_sysfs *sfp)
  424. {
  425. if (!sfp->debugifc) return;
  426. if (sfp->debugifc->debuginfo_created_ok) {
  427. device_remove_file(sfp->class_dev,
  428. &sfp->debugifc->attr_debuginfo);
  429. }
  430. if (sfp->debugifc->debugcmd_created_ok) {
  431. device_remove_file(sfp->class_dev,
  432. &sfp->debugifc->attr_debugcmd);
  433. }
  434. kfree(sfp->debugifc);
  435. sfp->debugifc = NULL;
  436. }
  437. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  438. static void pvr2_sysfs_add_controls(struct pvr2_sysfs *sfp)
  439. {
  440. unsigned int idx,cnt;
  441. cnt = pvr2_hdw_get_ctrl_count(sfp->channel.hdw);
  442. for (idx = 0; idx < cnt; idx++) {
  443. pvr2_sysfs_add_control(sfp,idx);
  444. }
  445. }
  446. static void pvr2_sysfs_tear_down_controls(struct pvr2_sysfs *sfp)
  447. {
  448. struct pvr2_sysfs_ctl_item *cip1,*cip2;
  449. for (cip1 = sfp->item_first; cip1; cip1 = cip2) {
  450. cip2 = cip1->item_next;
  451. if (cip1->created_ok) {
  452. sysfs_remove_group(&sfp->class_dev->kobj,&cip1->grp);
  453. }
  454. pvr2_sysfs_trace("Destroying pvr2_sysfs_ctl_item id=%p",cip1);
  455. kfree(cip1);
  456. }
  457. }
  458. static void pvr2_sysfs_class_release(struct class *class)
  459. {
  460. struct pvr2_sysfs_class *clp;
  461. clp = container_of(class,struct pvr2_sysfs_class,class);
  462. pvr2_sysfs_trace("Destroying pvr2_sysfs_class id=%p",clp);
  463. kfree(clp);
  464. }
  465. static void pvr2_sysfs_release(struct device *class_dev)
  466. {
  467. pvr2_sysfs_trace("Releasing class_dev id=%p",class_dev);
  468. kfree(class_dev);
  469. }
  470. static void class_dev_destroy(struct pvr2_sysfs *sfp)
  471. {
  472. struct device *dev;
  473. if (!sfp->class_dev) return;
  474. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  475. pvr2_sysfs_tear_down_debugifc(sfp);
  476. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  477. pvr2_sysfs_tear_down_controls(sfp);
  478. if (sfp->hdw_desc_created_ok) {
  479. device_remove_file(sfp->class_dev,
  480. &sfp->attr_hdw_desc);
  481. }
  482. if (sfp->hdw_name_created_ok) {
  483. device_remove_file(sfp->class_dev,
  484. &sfp->attr_hdw_name);
  485. }
  486. if (sfp->bus_info_created_ok) {
  487. device_remove_file(sfp->class_dev,
  488. &sfp->attr_bus_info);
  489. }
  490. if (sfp->v4l_minor_number_created_ok) {
  491. device_remove_file(sfp->class_dev,
  492. &sfp->attr_v4l_minor_number);
  493. }
  494. if (sfp->v4l_radio_minor_number_created_ok) {
  495. device_remove_file(sfp->class_dev,
  496. &sfp->attr_v4l_radio_minor_number);
  497. }
  498. if (sfp->unit_number_created_ok) {
  499. device_remove_file(sfp->class_dev,
  500. &sfp->attr_unit_number);
  501. }
  502. pvr2_sysfs_trace("Destroying class_dev id=%p",sfp->class_dev);
  503. dev_set_drvdata(sfp->class_dev, NULL);
  504. dev = sfp->class_dev->parent;
  505. sfp->class_dev->parent = NULL;
  506. put_device(dev);
  507. device_unregister(sfp->class_dev);
  508. sfp->class_dev = NULL;
  509. }
  510. static ssize_t v4l_minor_number_show(struct device *class_dev,
  511. struct device_attribute *attr, char *buf)
  512. {
  513. struct pvr2_sysfs *sfp;
  514. sfp = dev_get_drvdata(class_dev);
  515. if (!sfp) return -EINVAL;
  516. return scnprintf(buf,PAGE_SIZE,"%d\n",
  517. pvr2_hdw_v4l_get_minor_number(sfp->channel.hdw,
  518. pvr2_v4l_type_video));
  519. }
  520. static ssize_t bus_info_show(struct device *class_dev,
  521. struct device_attribute *attr, char *buf)
  522. {
  523. struct pvr2_sysfs *sfp;
  524. sfp = dev_get_drvdata(class_dev);
  525. if (!sfp) return -EINVAL;
  526. return scnprintf(buf,PAGE_SIZE,"%s\n",
  527. pvr2_hdw_get_bus_info(sfp->channel.hdw));
  528. }
  529. static ssize_t hdw_name_show(struct device *class_dev,
  530. struct device_attribute *attr, char *buf)
  531. {
  532. struct pvr2_sysfs *sfp;
  533. sfp = dev_get_drvdata(class_dev);
  534. if (!sfp) return -EINVAL;
  535. return scnprintf(buf,PAGE_SIZE,"%s\n",
  536. pvr2_hdw_get_type(sfp->channel.hdw));
  537. }
  538. static ssize_t hdw_desc_show(struct device *class_dev,
  539. struct device_attribute *attr, char *buf)
  540. {
  541. struct pvr2_sysfs *sfp;
  542. sfp = dev_get_drvdata(class_dev);
  543. if (!sfp) return -EINVAL;
  544. return scnprintf(buf,PAGE_SIZE,"%s\n",
  545. pvr2_hdw_get_desc(sfp->channel.hdw));
  546. }
  547. static ssize_t v4l_radio_minor_number_show(struct device *class_dev,
  548. struct device_attribute *attr,
  549. char *buf)
  550. {
  551. struct pvr2_sysfs *sfp;
  552. sfp = dev_get_drvdata(class_dev);
  553. if (!sfp) return -EINVAL;
  554. return scnprintf(buf,PAGE_SIZE,"%d\n",
  555. pvr2_hdw_v4l_get_minor_number(sfp->channel.hdw,
  556. pvr2_v4l_type_radio));
  557. }
  558. static ssize_t unit_number_show(struct device *class_dev,
  559. struct device_attribute *attr, char *buf)
  560. {
  561. struct pvr2_sysfs *sfp;
  562. sfp = dev_get_drvdata(class_dev);
  563. if (!sfp) return -EINVAL;
  564. return scnprintf(buf,PAGE_SIZE,"%d\n",
  565. pvr2_hdw_get_unit_number(sfp->channel.hdw));
  566. }
  567. static void class_dev_create(struct pvr2_sysfs *sfp,
  568. struct pvr2_sysfs_class *class_ptr)
  569. {
  570. struct usb_device *usb_dev;
  571. struct device *class_dev;
  572. int ret;
  573. usb_dev = pvr2_hdw_get_dev(sfp->channel.hdw);
  574. if (!usb_dev) return;
  575. class_dev = kzalloc(sizeof(*class_dev),GFP_KERNEL);
  576. if (!class_dev) return;
  577. pvr2_sysfs_trace("Creating class_dev id=%p",class_dev);
  578. class_dev->class = &class_ptr->class;
  579. dev_set_name(class_dev, "%s",
  580. pvr2_hdw_get_device_identifier(sfp->channel.hdw));
  581. class_dev->parent = get_device(&usb_dev->dev);
  582. sfp->class_dev = class_dev;
  583. dev_set_drvdata(class_dev, sfp);
  584. ret = device_register(class_dev);
  585. if (ret) {
  586. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  587. "device_register failed");
  588. put_device(class_dev);
  589. return;
  590. }
  591. sysfs_attr_init(&sfp->attr_v4l_minor_number.attr);
  592. sfp->attr_v4l_minor_number.attr.name = "v4l_minor_number";
  593. sfp->attr_v4l_minor_number.attr.mode = S_IRUGO;
  594. sfp->attr_v4l_minor_number.show = v4l_minor_number_show;
  595. sfp->attr_v4l_minor_number.store = NULL;
  596. ret = device_create_file(sfp->class_dev,
  597. &sfp->attr_v4l_minor_number);
  598. if (ret < 0) {
  599. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  600. "device_create_file error: %d",
  601. ret);
  602. } else {
  603. sfp->v4l_minor_number_created_ok = !0;
  604. }
  605. sysfs_attr_init(&sfp->attr_v4l_radio_minor_number.attr);
  606. sfp->attr_v4l_radio_minor_number.attr.name = "v4l_radio_minor_number";
  607. sfp->attr_v4l_radio_minor_number.attr.mode = S_IRUGO;
  608. sfp->attr_v4l_radio_minor_number.show = v4l_radio_minor_number_show;
  609. sfp->attr_v4l_radio_minor_number.store = NULL;
  610. ret = device_create_file(sfp->class_dev,
  611. &sfp->attr_v4l_radio_minor_number);
  612. if (ret < 0) {
  613. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  614. "device_create_file error: %d",
  615. ret);
  616. } else {
  617. sfp->v4l_radio_minor_number_created_ok = !0;
  618. }
  619. sysfs_attr_init(&sfp->attr_unit_number.attr);
  620. sfp->attr_unit_number.attr.name = "unit_number";
  621. sfp->attr_unit_number.attr.mode = S_IRUGO;
  622. sfp->attr_unit_number.show = unit_number_show;
  623. sfp->attr_unit_number.store = NULL;
  624. ret = device_create_file(sfp->class_dev,&sfp->attr_unit_number);
  625. if (ret < 0) {
  626. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  627. "device_create_file error: %d",
  628. ret);
  629. } else {
  630. sfp->unit_number_created_ok = !0;
  631. }
  632. sysfs_attr_init(&sfp->attr_bus_info.attr);
  633. sfp->attr_bus_info.attr.name = "bus_info_str";
  634. sfp->attr_bus_info.attr.mode = S_IRUGO;
  635. sfp->attr_bus_info.show = bus_info_show;
  636. sfp->attr_bus_info.store = NULL;
  637. ret = device_create_file(sfp->class_dev,
  638. &sfp->attr_bus_info);
  639. if (ret < 0) {
  640. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  641. "device_create_file error: %d",
  642. ret);
  643. } else {
  644. sfp->bus_info_created_ok = !0;
  645. }
  646. sysfs_attr_init(&sfp->attr_hdw_name.attr);
  647. sfp->attr_hdw_name.attr.name = "device_hardware_type";
  648. sfp->attr_hdw_name.attr.mode = S_IRUGO;
  649. sfp->attr_hdw_name.show = hdw_name_show;
  650. sfp->attr_hdw_name.store = NULL;
  651. ret = device_create_file(sfp->class_dev,
  652. &sfp->attr_hdw_name);
  653. if (ret < 0) {
  654. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  655. "device_create_file error: %d",
  656. ret);
  657. } else {
  658. sfp->hdw_name_created_ok = !0;
  659. }
  660. sysfs_attr_init(&sfp->attr_hdw_desc.attr);
  661. sfp->attr_hdw_desc.attr.name = "device_hardware_description";
  662. sfp->attr_hdw_desc.attr.mode = S_IRUGO;
  663. sfp->attr_hdw_desc.show = hdw_desc_show;
  664. sfp->attr_hdw_desc.store = NULL;
  665. ret = device_create_file(sfp->class_dev,
  666. &sfp->attr_hdw_desc);
  667. if (ret < 0) {
  668. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  669. "device_create_file error: %d",
  670. ret);
  671. } else {
  672. sfp->hdw_desc_created_ok = !0;
  673. }
  674. pvr2_sysfs_add_controls(sfp);
  675. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  676. pvr2_sysfs_add_debugifc(sfp);
  677. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  678. }
  679. static void pvr2_sysfs_internal_check(struct pvr2_channel *chp)
  680. {
  681. struct pvr2_sysfs *sfp;
  682. sfp = container_of(chp,struct pvr2_sysfs,channel);
  683. if (!sfp->channel.mc_head->disconnect_flag) return;
  684. pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_sysfs id=%p",sfp);
  685. class_dev_destroy(sfp);
  686. pvr2_channel_done(&sfp->channel);
  687. kfree(sfp);
  688. }
  689. struct pvr2_sysfs *pvr2_sysfs_create(struct pvr2_context *mp,
  690. struct pvr2_sysfs_class *class_ptr)
  691. {
  692. struct pvr2_sysfs *sfp;
  693. sfp = kzalloc(sizeof(*sfp),GFP_KERNEL);
  694. if (!sfp) return sfp;
  695. pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_sysfs id=%p",sfp);
  696. pvr2_channel_init(&sfp->channel,mp);
  697. sfp->channel.check_func = pvr2_sysfs_internal_check;
  698. class_dev_create(sfp,class_ptr);
  699. return sfp;
  700. }
  701. struct pvr2_sysfs_class *pvr2_sysfs_class_create(void)
  702. {
  703. struct pvr2_sysfs_class *clp;
  704. clp = kzalloc(sizeof(*clp),GFP_KERNEL);
  705. if (!clp) return clp;
  706. pvr2_sysfs_trace("Creating and registering pvr2_sysfs_class id=%p",
  707. clp);
  708. clp->class.name = "pvrusb2";
  709. clp->class.class_release = pvr2_sysfs_class_release;
  710. clp->class.dev_release = pvr2_sysfs_release;
  711. if (class_register(&clp->class)) {
  712. pvr2_sysfs_trace(
  713. "Registration failed for pvr2_sysfs_class id=%p",clp);
  714. kfree(clp);
  715. clp = NULL;
  716. }
  717. return clp;
  718. }
  719. void pvr2_sysfs_class_destroy(struct pvr2_sysfs_class *clp)
  720. {
  721. pvr2_sysfs_trace("Unregistering pvr2_sysfs_class id=%p", clp);
  722. class_unregister(&clp->class);
  723. }
  724. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  725. static ssize_t debuginfo_show(struct device *class_dev,
  726. struct device_attribute *attr, char *buf)
  727. {
  728. struct pvr2_sysfs *sfp;
  729. sfp = dev_get_drvdata(class_dev);
  730. if (!sfp) return -EINVAL;
  731. pvr2_hdw_trigger_module_log(sfp->channel.hdw);
  732. return pvr2_debugifc_print_info(sfp->channel.hdw,buf,PAGE_SIZE);
  733. }
  734. static ssize_t debugcmd_show(struct device *class_dev,
  735. struct device_attribute *attr, char *buf)
  736. {
  737. struct pvr2_sysfs *sfp;
  738. sfp = dev_get_drvdata(class_dev);
  739. if (!sfp) return -EINVAL;
  740. return pvr2_debugifc_print_status(sfp->channel.hdw,buf,PAGE_SIZE);
  741. }
  742. static ssize_t debugcmd_store(struct device *class_dev,
  743. struct device_attribute *attr,
  744. const char *buf, size_t count)
  745. {
  746. struct pvr2_sysfs *sfp;
  747. int ret;
  748. sfp = dev_get_drvdata(class_dev);
  749. if (!sfp) return -EINVAL;
  750. ret = pvr2_debugifc_docmd(sfp->channel.hdw,buf,count);
  751. if (ret < 0) return ret;
  752. return count;
  753. }
  754. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */