pvrusb2-sysfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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. cip->attr_name.attr.name = "name";
  304. cip->attr_name.attr.mode = S_IRUGO;
  305. cip->attr_name.show = show_name;
  306. cip->attr_type.attr.name = "type";
  307. cip->attr_type.attr.mode = S_IRUGO;
  308. cip->attr_type.show = show_type;
  309. cip->attr_min.attr.name = "min_val";
  310. cip->attr_min.attr.mode = S_IRUGO;
  311. cip->attr_min.show = show_min;
  312. cip->attr_max.attr.name = "max_val";
  313. cip->attr_max.attr.mode = S_IRUGO;
  314. cip->attr_max.show = show_max;
  315. cip->attr_def.attr.name = "def_val";
  316. cip->attr_def.attr.mode = S_IRUGO;
  317. cip->attr_def.show = show_def;
  318. cip->attr_val.attr.name = "cur_val";
  319. cip->attr_val.attr.mode = S_IRUGO;
  320. cip->attr_custom.attr.name = "custom_val";
  321. cip->attr_custom.attr.mode = S_IRUGO;
  322. cip->attr_enum.attr.name = "enum_val";
  323. cip->attr_enum.attr.mode = S_IRUGO;
  324. cip->attr_enum.show = show_enum;
  325. cip->attr_bits.attr.name = "bit_val";
  326. cip->attr_bits.attr.mode = S_IRUGO;
  327. cip->attr_bits.show = show_bits;
  328. if (pvr2_ctrl_is_writable(cptr)) {
  329. cip->attr_val.attr.mode |= S_IWUSR|S_IWGRP;
  330. cip->attr_custom.attr.mode |= S_IWUSR|S_IWGRP;
  331. }
  332. acnt = 0;
  333. cip->attr_gen[acnt++] = &cip->attr_name.attr;
  334. cip->attr_gen[acnt++] = &cip->attr_type.attr;
  335. cip->attr_gen[acnt++] = &cip->attr_val.attr;
  336. cip->attr_gen[acnt++] = &cip->attr_def.attr;
  337. cip->attr_val.show = show_val_norm;
  338. cip->attr_val.store = store_val_norm;
  339. if (pvr2_ctrl_has_custom_symbols(cptr)) {
  340. cip->attr_gen[acnt++] = &cip->attr_custom.attr;
  341. cip->attr_custom.show = show_val_custom;
  342. cip->attr_custom.store = store_val_custom;
  343. }
  344. switch (pvr2_ctrl_get_type(cptr)) {
  345. case pvr2_ctl_enum:
  346. // Control is an enumeration
  347. cip->attr_gen[acnt++] = &cip->attr_enum.attr;
  348. break;
  349. case pvr2_ctl_int:
  350. // Control is an integer
  351. cip->attr_gen[acnt++] = &cip->attr_min.attr;
  352. cip->attr_gen[acnt++] = &cip->attr_max.attr;
  353. break;
  354. case pvr2_ctl_bitmask:
  355. // Control is an bitmask
  356. cip->attr_gen[acnt++] = &cip->attr_bits.attr;
  357. break;
  358. default: break;
  359. }
  360. cnt = scnprintf(cip->name,sizeof(cip->name)-1,"ctl_%s",
  361. pvr2_ctrl_get_name(cptr));
  362. cip->name[cnt] = 0;
  363. cip->grp.name = cip->name;
  364. cip->grp.attrs = cip->attr_gen;
  365. ret = sysfs_create_group(&sfp->class_dev->kobj,&cip->grp);
  366. if (ret) {
  367. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  368. "sysfs_create_group error: %d",
  369. ret);
  370. return;
  371. }
  372. cip->created_ok = !0;
  373. }
  374. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  375. static ssize_t debuginfo_show(struct device *, struct device_attribute *,
  376. char *);
  377. static ssize_t debugcmd_show(struct device *, struct device_attribute *,
  378. char *);
  379. static ssize_t debugcmd_store(struct device *, struct device_attribute *,
  380. const char *, size_t count);
  381. static void pvr2_sysfs_add_debugifc(struct pvr2_sysfs *sfp)
  382. {
  383. struct pvr2_sysfs_debugifc *dip;
  384. int ret;
  385. dip = kzalloc(sizeof(*dip),GFP_KERNEL);
  386. if (!dip) return;
  387. sysfs_attr_init(&dip->attr_debugcmd.attr);
  388. dip->attr_debugcmd.attr.name = "debugcmd";
  389. dip->attr_debugcmd.attr.mode = S_IRUGO|S_IWUSR|S_IWGRP;
  390. dip->attr_debugcmd.show = debugcmd_show;
  391. dip->attr_debugcmd.store = debugcmd_store;
  392. sysfs_attr_init(&dip->attr_debuginfo.attr);
  393. dip->attr_debuginfo.attr.name = "debuginfo";
  394. dip->attr_debuginfo.attr.mode = S_IRUGO;
  395. dip->attr_debuginfo.show = debuginfo_show;
  396. sfp->debugifc = dip;
  397. ret = device_create_file(sfp->class_dev,&dip->attr_debugcmd);
  398. if (ret < 0) {
  399. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  400. "device_create_file error: %d",
  401. ret);
  402. } else {
  403. dip->debugcmd_created_ok = !0;
  404. }
  405. ret = device_create_file(sfp->class_dev,&dip->attr_debuginfo);
  406. if (ret < 0) {
  407. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  408. "device_create_file error: %d",
  409. ret);
  410. } else {
  411. dip->debuginfo_created_ok = !0;
  412. }
  413. }
  414. static void pvr2_sysfs_tear_down_debugifc(struct pvr2_sysfs *sfp)
  415. {
  416. if (!sfp->debugifc) return;
  417. if (sfp->debugifc->debuginfo_created_ok) {
  418. device_remove_file(sfp->class_dev,
  419. &sfp->debugifc->attr_debuginfo);
  420. }
  421. if (sfp->debugifc->debugcmd_created_ok) {
  422. device_remove_file(sfp->class_dev,
  423. &sfp->debugifc->attr_debugcmd);
  424. }
  425. kfree(sfp->debugifc);
  426. sfp->debugifc = NULL;
  427. }
  428. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  429. static void pvr2_sysfs_add_controls(struct pvr2_sysfs *sfp)
  430. {
  431. unsigned int idx,cnt;
  432. cnt = pvr2_hdw_get_ctrl_count(sfp->channel.hdw);
  433. for (idx = 0; idx < cnt; idx++) {
  434. pvr2_sysfs_add_control(sfp,idx);
  435. }
  436. }
  437. static void pvr2_sysfs_tear_down_controls(struct pvr2_sysfs *sfp)
  438. {
  439. struct pvr2_sysfs_ctl_item *cip1,*cip2;
  440. for (cip1 = sfp->item_first; cip1; cip1 = cip2) {
  441. cip2 = cip1->item_next;
  442. if (cip1->created_ok) {
  443. sysfs_remove_group(&sfp->class_dev->kobj,&cip1->grp);
  444. }
  445. pvr2_sysfs_trace("Destroying pvr2_sysfs_ctl_item id=%p",cip1);
  446. kfree(cip1);
  447. }
  448. }
  449. static void pvr2_sysfs_class_release(struct class *class)
  450. {
  451. struct pvr2_sysfs_class *clp;
  452. clp = container_of(class,struct pvr2_sysfs_class,class);
  453. pvr2_sysfs_trace("Destroying pvr2_sysfs_class id=%p",clp);
  454. kfree(clp);
  455. }
  456. static void pvr2_sysfs_release(struct device *class_dev)
  457. {
  458. pvr2_sysfs_trace("Releasing class_dev id=%p",class_dev);
  459. kfree(class_dev);
  460. }
  461. static void class_dev_destroy(struct pvr2_sysfs *sfp)
  462. {
  463. struct device *dev;
  464. if (!sfp->class_dev) return;
  465. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  466. pvr2_sysfs_tear_down_debugifc(sfp);
  467. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  468. pvr2_sysfs_tear_down_controls(sfp);
  469. if (sfp->hdw_desc_created_ok) {
  470. device_remove_file(sfp->class_dev,
  471. &sfp->attr_hdw_desc);
  472. }
  473. if (sfp->hdw_name_created_ok) {
  474. device_remove_file(sfp->class_dev,
  475. &sfp->attr_hdw_name);
  476. }
  477. if (sfp->bus_info_created_ok) {
  478. device_remove_file(sfp->class_dev,
  479. &sfp->attr_bus_info);
  480. }
  481. if (sfp->v4l_minor_number_created_ok) {
  482. device_remove_file(sfp->class_dev,
  483. &sfp->attr_v4l_minor_number);
  484. }
  485. if (sfp->v4l_radio_minor_number_created_ok) {
  486. device_remove_file(sfp->class_dev,
  487. &sfp->attr_v4l_radio_minor_number);
  488. }
  489. if (sfp->unit_number_created_ok) {
  490. device_remove_file(sfp->class_dev,
  491. &sfp->attr_unit_number);
  492. }
  493. pvr2_sysfs_trace("Destroying class_dev id=%p",sfp->class_dev);
  494. dev_set_drvdata(sfp->class_dev, NULL);
  495. dev = sfp->class_dev->parent;
  496. sfp->class_dev->parent = NULL;
  497. put_device(dev);
  498. device_unregister(sfp->class_dev);
  499. sfp->class_dev = NULL;
  500. }
  501. static ssize_t v4l_minor_number_show(struct device *class_dev,
  502. struct device_attribute *attr, char *buf)
  503. {
  504. struct pvr2_sysfs *sfp;
  505. sfp = dev_get_drvdata(class_dev);
  506. if (!sfp) return -EINVAL;
  507. return scnprintf(buf,PAGE_SIZE,"%d\n",
  508. pvr2_hdw_v4l_get_minor_number(sfp->channel.hdw,
  509. pvr2_v4l_type_video));
  510. }
  511. static ssize_t bus_info_show(struct device *class_dev,
  512. struct device_attribute *attr, char *buf)
  513. {
  514. struct pvr2_sysfs *sfp;
  515. sfp = dev_get_drvdata(class_dev);
  516. if (!sfp) return -EINVAL;
  517. return scnprintf(buf,PAGE_SIZE,"%s\n",
  518. pvr2_hdw_get_bus_info(sfp->channel.hdw));
  519. }
  520. static ssize_t hdw_name_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_type(sfp->channel.hdw));
  528. }
  529. static ssize_t hdw_desc_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_desc(sfp->channel.hdw));
  537. }
  538. static ssize_t v4l_radio_minor_number_show(struct device *class_dev,
  539. struct device_attribute *attr,
  540. char *buf)
  541. {
  542. struct pvr2_sysfs *sfp;
  543. sfp = dev_get_drvdata(class_dev);
  544. if (!sfp) return -EINVAL;
  545. return scnprintf(buf,PAGE_SIZE,"%d\n",
  546. pvr2_hdw_v4l_get_minor_number(sfp->channel.hdw,
  547. pvr2_v4l_type_radio));
  548. }
  549. static ssize_t unit_number_show(struct device *class_dev,
  550. struct device_attribute *attr, char *buf)
  551. {
  552. struct pvr2_sysfs *sfp;
  553. sfp = dev_get_drvdata(class_dev);
  554. if (!sfp) return -EINVAL;
  555. return scnprintf(buf,PAGE_SIZE,"%d\n",
  556. pvr2_hdw_get_unit_number(sfp->channel.hdw));
  557. }
  558. static void class_dev_create(struct pvr2_sysfs *sfp,
  559. struct pvr2_sysfs_class *class_ptr)
  560. {
  561. struct usb_device *usb_dev;
  562. struct device *class_dev;
  563. int ret;
  564. usb_dev = pvr2_hdw_get_dev(sfp->channel.hdw);
  565. if (!usb_dev) return;
  566. class_dev = kzalloc(sizeof(*class_dev),GFP_KERNEL);
  567. if (!class_dev) return;
  568. pvr2_sysfs_trace("Creating class_dev id=%p",class_dev);
  569. class_dev->class = &class_ptr->class;
  570. dev_set_name(class_dev, "%s",
  571. pvr2_hdw_get_device_identifier(sfp->channel.hdw));
  572. class_dev->parent = get_device(&usb_dev->dev);
  573. sfp->class_dev = class_dev;
  574. dev_set_drvdata(class_dev, sfp);
  575. ret = device_register(class_dev);
  576. if (ret) {
  577. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  578. "device_register failed");
  579. kfree(class_dev);
  580. return;
  581. }
  582. sysfs_attr_init(&sfp->attr_v4l_minor_number.attr);
  583. sfp->attr_v4l_minor_number.attr.name = "v4l_minor_number";
  584. sfp->attr_v4l_minor_number.attr.mode = S_IRUGO;
  585. sfp->attr_v4l_minor_number.show = v4l_minor_number_show;
  586. sfp->attr_v4l_minor_number.store = NULL;
  587. ret = device_create_file(sfp->class_dev,
  588. &sfp->attr_v4l_minor_number);
  589. if (ret < 0) {
  590. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  591. "device_create_file error: %d",
  592. ret);
  593. } else {
  594. sfp->v4l_minor_number_created_ok = !0;
  595. }
  596. sysfs_attr_init(&sfp->attr_v4l_radio_minor_number.attr);
  597. sfp->attr_v4l_radio_minor_number.attr.name = "v4l_radio_minor_number";
  598. sfp->attr_v4l_radio_minor_number.attr.mode = S_IRUGO;
  599. sfp->attr_v4l_radio_minor_number.show = v4l_radio_minor_number_show;
  600. sfp->attr_v4l_radio_minor_number.store = NULL;
  601. ret = device_create_file(sfp->class_dev,
  602. &sfp->attr_v4l_radio_minor_number);
  603. if (ret < 0) {
  604. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  605. "device_create_file error: %d",
  606. ret);
  607. } else {
  608. sfp->v4l_radio_minor_number_created_ok = !0;
  609. }
  610. sysfs_attr_init(&sfp->attr_unit_number.attr);
  611. sfp->attr_unit_number.attr.name = "unit_number";
  612. sfp->attr_unit_number.attr.mode = S_IRUGO;
  613. sfp->attr_unit_number.show = unit_number_show;
  614. sfp->attr_unit_number.store = NULL;
  615. ret = device_create_file(sfp->class_dev,&sfp->attr_unit_number);
  616. if (ret < 0) {
  617. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  618. "device_create_file error: %d",
  619. ret);
  620. } else {
  621. sfp->unit_number_created_ok = !0;
  622. }
  623. sysfs_attr_init(&sfp->attr_bus_info.attr);
  624. sfp->attr_bus_info.attr.name = "bus_info_str";
  625. sfp->attr_bus_info.attr.mode = S_IRUGO;
  626. sfp->attr_bus_info.show = bus_info_show;
  627. sfp->attr_bus_info.store = NULL;
  628. ret = device_create_file(sfp->class_dev,
  629. &sfp->attr_bus_info);
  630. if (ret < 0) {
  631. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  632. "device_create_file error: %d",
  633. ret);
  634. } else {
  635. sfp->bus_info_created_ok = !0;
  636. }
  637. sysfs_attr_init(&sfp->attr_hdw_name.attr);
  638. sfp->attr_hdw_name.attr.name = "device_hardware_type";
  639. sfp->attr_hdw_name.attr.mode = S_IRUGO;
  640. sfp->attr_hdw_name.show = hdw_name_show;
  641. sfp->attr_hdw_name.store = NULL;
  642. ret = device_create_file(sfp->class_dev,
  643. &sfp->attr_hdw_name);
  644. if (ret < 0) {
  645. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  646. "device_create_file error: %d",
  647. ret);
  648. } else {
  649. sfp->hdw_name_created_ok = !0;
  650. }
  651. sysfs_attr_init(&sfp->attr_hdw_desc.attr);
  652. sfp->attr_hdw_desc.attr.name = "device_hardware_description";
  653. sfp->attr_hdw_desc.attr.mode = S_IRUGO;
  654. sfp->attr_hdw_desc.show = hdw_desc_show;
  655. sfp->attr_hdw_desc.store = NULL;
  656. ret = device_create_file(sfp->class_dev,
  657. &sfp->attr_hdw_desc);
  658. if (ret < 0) {
  659. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  660. "device_create_file error: %d",
  661. ret);
  662. } else {
  663. sfp->hdw_desc_created_ok = !0;
  664. }
  665. pvr2_sysfs_add_controls(sfp);
  666. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  667. pvr2_sysfs_add_debugifc(sfp);
  668. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  669. }
  670. static void pvr2_sysfs_internal_check(struct pvr2_channel *chp)
  671. {
  672. struct pvr2_sysfs *sfp;
  673. sfp = container_of(chp,struct pvr2_sysfs,channel);
  674. if (!sfp->channel.mc_head->disconnect_flag) return;
  675. pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_sysfs id=%p",sfp);
  676. class_dev_destroy(sfp);
  677. pvr2_channel_done(&sfp->channel);
  678. kfree(sfp);
  679. }
  680. struct pvr2_sysfs *pvr2_sysfs_create(struct pvr2_context *mp,
  681. struct pvr2_sysfs_class *class_ptr)
  682. {
  683. struct pvr2_sysfs *sfp;
  684. sfp = kzalloc(sizeof(*sfp),GFP_KERNEL);
  685. if (!sfp) return sfp;
  686. pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_sysfs id=%p",sfp);
  687. pvr2_channel_init(&sfp->channel,mp);
  688. sfp->channel.check_func = pvr2_sysfs_internal_check;
  689. class_dev_create(sfp,class_ptr);
  690. return sfp;
  691. }
  692. struct pvr2_sysfs_class *pvr2_sysfs_class_create(void)
  693. {
  694. struct pvr2_sysfs_class *clp;
  695. clp = kzalloc(sizeof(*clp),GFP_KERNEL);
  696. if (!clp) return clp;
  697. pvr2_sysfs_trace("Creating and registering pvr2_sysfs_class id=%p",
  698. clp);
  699. clp->class.name = "pvrusb2";
  700. clp->class.class_release = pvr2_sysfs_class_release;
  701. clp->class.dev_release = pvr2_sysfs_release;
  702. if (class_register(&clp->class)) {
  703. pvr2_sysfs_trace(
  704. "Registration failed for pvr2_sysfs_class id=%p",clp);
  705. kfree(clp);
  706. clp = NULL;
  707. }
  708. return clp;
  709. }
  710. void pvr2_sysfs_class_destroy(struct pvr2_sysfs_class *clp)
  711. {
  712. pvr2_sysfs_trace("Unregistering pvr2_sysfs_class id=%p", clp);
  713. class_unregister(&clp->class);
  714. }
  715. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  716. static ssize_t debuginfo_show(struct device *class_dev,
  717. struct device_attribute *attr, char *buf)
  718. {
  719. struct pvr2_sysfs *sfp;
  720. sfp = dev_get_drvdata(class_dev);
  721. if (!sfp) return -EINVAL;
  722. pvr2_hdw_trigger_module_log(sfp->channel.hdw);
  723. return pvr2_debugifc_print_info(sfp->channel.hdw,buf,PAGE_SIZE);
  724. }
  725. static ssize_t debugcmd_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. return pvr2_debugifc_print_status(sfp->channel.hdw,buf,PAGE_SIZE);
  732. }
  733. static ssize_t debugcmd_store(struct device *class_dev,
  734. struct device_attribute *attr,
  735. const char *buf, size_t count)
  736. {
  737. struct pvr2_sysfs *sfp;
  738. int ret;
  739. sfp = dev_get_drvdata(class_dev);
  740. if (!sfp) return -EINVAL;
  741. ret = pvr2_debugifc_docmd(sfp->channel.hdw,buf,count);
  742. if (ret < 0) return ret;
  743. return count;
  744. }
  745. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  746. /*
  747. Stuff for Emacs to see, in order to encourage consistent editing style:
  748. *** Local Variables: ***
  749. *** mode: c ***
  750. *** fill-column: 75 ***
  751. *** tab-width: 8 ***
  752. *** c-basic-offset: 8 ***
  753. *** End: ***
  754. */