pvrusb2-sysfs.c 23 KB

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