pvrusb2-sysfs.c 23 KB

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