pvrusb2-sysfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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[7];
  73. struct attribute_group grp;
  74. int created_ok;
  75. char name[80];
  76. };
  77. struct pvr2_sysfs_class {
  78. struct class class;
  79. };
  80. static ssize_t show_name(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. if (!sfp->class_dev) return;
  464. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  465. pvr2_sysfs_tear_down_debugifc(sfp);
  466. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  467. pvr2_sysfs_tear_down_controls(sfp);
  468. if (sfp->hdw_desc_created_ok) {
  469. device_remove_file(sfp->class_dev,
  470. &sfp->attr_hdw_desc);
  471. }
  472. if (sfp->hdw_name_created_ok) {
  473. device_remove_file(sfp->class_dev,
  474. &sfp->attr_hdw_name);
  475. }
  476. if (sfp->bus_info_created_ok) {
  477. device_remove_file(sfp->class_dev,
  478. &sfp->attr_bus_info);
  479. }
  480. if (sfp->v4l_minor_number_created_ok) {
  481. device_remove_file(sfp->class_dev,
  482. &sfp->attr_v4l_minor_number);
  483. }
  484. if (sfp->v4l_radio_minor_number_created_ok) {
  485. device_remove_file(sfp->class_dev,
  486. &sfp->attr_v4l_radio_minor_number);
  487. }
  488. if (sfp->unit_number_created_ok) {
  489. device_remove_file(sfp->class_dev,
  490. &sfp->attr_unit_number);
  491. }
  492. pvr2_sysfs_trace("Destroying class_dev id=%p",sfp->class_dev);
  493. dev_set_drvdata(sfp->class_dev, NULL);
  494. device_unregister(sfp->class_dev);
  495. sfp->class_dev = NULL;
  496. }
  497. static ssize_t v4l_minor_number_show(struct device *class_dev,
  498. struct device_attribute *attr, char *buf)
  499. {
  500. struct pvr2_sysfs *sfp;
  501. sfp = dev_get_drvdata(class_dev);
  502. if (!sfp) return -EINVAL;
  503. return scnprintf(buf,PAGE_SIZE,"%d\n",
  504. pvr2_hdw_v4l_get_minor_number(sfp->channel.hdw,
  505. pvr2_v4l_type_video));
  506. }
  507. static ssize_t bus_info_show(struct device *class_dev,
  508. struct device_attribute *attr, char *buf)
  509. {
  510. struct pvr2_sysfs *sfp;
  511. sfp = dev_get_drvdata(class_dev);
  512. if (!sfp) return -EINVAL;
  513. return scnprintf(buf,PAGE_SIZE,"%s\n",
  514. pvr2_hdw_get_bus_info(sfp->channel.hdw));
  515. }
  516. static ssize_t hdw_name_show(struct device *class_dev,
  517. struct device_attribute *attr, char *buf)
  518. {
  519. struct pvr2_sysfs *sfp;
  520. sfp = dev_get_drvdata(class_dev);
  521. if (!sfp) return -EINVAL;
  522. return scnprintf(buf,PAGE_SIZE,"%s\n",
  523. pvr2_hdw_get_type(sfp->channel.hdw));
  524. }
  525. static ssize_t hdw_desc_show(struct device *class_dev,
  526. struct device_attribute *attr, char *buf)
  527. {
  528. struct pvr2_sysfs *sfp;
  529. sfp = dev_get_drvdata(class_dev);
  530. if (!sfp) return -EINVAL;
  531. return scnprintf(buf,PAGE_SIZE,"%s\n",
  532. pvr2_hdw_get_desc(sfp->channel.hdw));
  533. }
  534. static ssize_t v4l_radio_minor_number_show(struct device *class_dev,
  535. struct device_attribute *attr,
  536. char *buf)
  537. {
  538. struct pvr2_sysfs *sfp;
  539. sfp = dev_get_drvdata(class_dev);
  540. if (!sfp) return -EINVAL;
  541. return scnprintf(buf,PAGE_SIZE,"%d\n",
  542. pvr2_hdw_v4l_get_minor_number(sfp->channel.hdw,
  543. pvr2_v4l_type_radio));
  544. }
  545. static ssize_t unit_number_show(struct device *class_dev,
  546. struct device_attribute *attr, char *buf)
  547. {
  548. struct pvr2_sysfs *sfp;
  549. sfp = dev_get_drvdata(class_dev);
  550. if (!sfp) return -EINVAL;
  551. return scnprintf(buf,PAGE_SIZE,"%d\n",
  552. pvr2_hdw_get_unit_number(sfp->channel.hdw));
  553. }
  554. static void class_dev_create(struct pvr2_sysfs *sfp,
  555. struct pvr2_sysfs_class *class_ptr)
  556. {
  557. struct usb_device *usb_dev;
  558. struct device *class_dev;
  559. int ret;
  560. usb_dev = pvr2_hdw_get_dev(sfp->channel.hdw);
  561. if (!usb_dev) return;
  562. class_dev = kzalloc(sizeof(*class_dev),GFP_KERNEL);
  563. if (!class_dev) return;
  564. pvr2_sysfs_trace("Creating class_dev id=%p",class_dev);
  565. class_dev->class = &class_ptr->class;
  566. dev_set_name(class_dev, "%s",
  567. pvr2_hdw_get_device_identifier(sfp->channel.hdw));
  568. class_dev->parent = &usb_dev->dev;
  569. sfp->class_dev = class_dev;
  570. dev_set_drvdata(class_dev, sfp);
  571. ret = device_register(class_dev);
  572. if (ret) {
  573. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  574. "device_register failed");
  575. kfree(class_dev);
  576. return;
  577. }
  578. sysfs_attr_init(&sfp->attr_v4l_minor_number.attr);
  579. sfp->attr_v4l_minor_number.attr.name = "v4l_minor_number";
  580. sfp->attr_v4l_minor_number.attr.mode = S_IRUGO;
  581. sfp->attr_v4l_minor_number.show = v4l_minor_number_show;
  582. sfp->attr_v4l_minor_number.store = NULL;
  583. ret = device_create_file(sfp->class_dev,
  584. &sfp->attr_v4l_minor_number);
  585. if (ret < 0) {
  586. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  587. "device_create_file error: %d",
  588. ret);
  589. } else {
  590. sfp->v4l_minor_number_created_ok = !0;
  591. }
  592. sysfs_attr_init(&sfp->attr_v4l_radio_minor_number.attr);
  593. sfp->attr_v4l_radio_minor_number.attr.name = "v4l_radio_minor_number";
  594. sfp->attr_v4l_radio_minor_number.attr.mode = S_IRUGO;
  595. sfp->attr_v4l_radio_minor_number.show = v4l_radio_minor_number_show;
  596. sfp->attr_v4l_radio_minor_number.store = NULL;
  597. ret = device_create_file(sfp->class_dev,
  598. &sfp->attr_v4l_radio_minor_number);
  599. if (ret < 0) {
  600. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  601. "device_create_file error: %d",
  602. ret);
  603. } else {
  604. sfp->v4l_radio_minor_number_created_ok = !0;
  605. }
  606. sysfs_attr_init(&sfp->attr_unit_number.attr);
  607. sfp->attr_unit_number.attr.name = "unit_number";
  608. sfp->attr_unit_number.attr.mode = S_IRUGO;
  609. sfp->attr_unit_number.show = unit_number_show;
  610. sfp->attr_unit_number.store = NULL;
  611. ret = device_create_file(sfp->class_dev,&sfp->attr_unit_number);
  612. if (ret < 0) {
  613. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  614. "device_create_file error: %d",
  615. ret);
  616. } else {
  617. sfp->unit_number_created_ok = !0;
  618. }
  619. sysfs_attr_init(&sfp->attr_bus_info.attr);
  620. sfp->attr_bus_info.attr.name = "bus_info_str";
  621. sfp->attr_bus_info.attr.mode = S_IRUGO;
  622. sfp->attr_bus_info.show = bus_info_show;
  623. sfp->attr_bus_info.store = NULL;
  624. ret = device_create_file(sfp->class_dev,
  625. &sfp->attr_bus_info);
  626. if (ret < 0) {
  627. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  628. "device_create_file error: %d",
  629. ret);
  630. } else {
  631. sfp->bus_info_created_ok = !0;
  632. }
  633. sysfs_attr_init(&sfp->attr_hdw_name.attr);
  634. sfp->attr_hdw_name.attr.name = "device_hardware_type";
  635. sfp->attr_hdw_name.attr.mode = S_IRUGO;
  636. sfp->attr_hdw_name.show = hdw_name_show;
  637. sfp->attr_hdw_name.store = NULL;
  638. ret = device_create_file(sfp->class_dev,
  639. &sfp->attr_hdw_name);
  640. if (ret < 0) {
  641. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  642. "device_create_file error: %d",
  643. ret);
  644. } else {
  645. sfp->hdw_name_created_ok = !0;
  646. }
  647. sysfs_attr_init(&sfp->attr_hdw_desc.attr);
  648. sfp->attr_hdw_desc.attr.name = "device_hardware_description";
  649. sfp->attr_hdw_desc.attr.mode = S_IRUGO;
  650. sfp->attr_hdw_desc.show = hdw_desc_show;
  651. sfp->attr_hdw_desc.store = NULL;
  652. ret = device_create_file(sfp->class_dev,
  653. &sfp->attr_hdw_desc);
  654. if (ret < 0) {
  655. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  656. "device_create_file error: %d",
  657. ret);
  658. } else {
  659. sfp->hdw_desc_created_ok = !0;
  660. }
  661. pvr2_sysfs_add_controls(sfp);
  662. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  663. pvr2_sysfs_add_debugifc(sfp);
  664. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  665. }
  666. static void pvr2_sysfs_internal_check(struct pvr2_channel *chp)
  667. {
  668. struct pvr2_sysfs *sfp;
  669. sfp = container_of(chp,struct pvr2_sysfs,channel);
  670. if (!sfp->channel.mc_head->disconnect_flag) return;
  671. pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_sysfs id=%p",sfp);
  672. class_dev_destroy(sfp);
  673. pvr2_channel_done(&sfp->channel);
  674. kfree(sfp);
  675. }
  676. struct pvr2_sysfs *pvr2_sysfs_create(struct pvr2_context *mp,
  677. struct pvr2_sysfs_class *class_ptr)
  678. {
  679. struct pvr2_sysfs *sfp;
  680. sfp = kzalloc(sizeof(*sfp),GFP_KERNEL);
  681. if (!sfp) return sfp;
  682. pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_sysfs id=%p",sfp);
  683. pvr2_channel_init(&sfp->channel,mp);
  684. sfp->channel.check_func = pvr2_sysfs_internal_check;
  685. class_dev_create(sfp,class_ptr);
  686. return sfp;
  687. }
  688. struct pvr2_sysfs_class *pvr2_sysfs_class_create(void)
  689. {
  690. struct pvr2_sysfs_class *clp;
  691. clp = kzalloc(sizeof(*clp),GFP_KERNEL);
  692. if (!clp) return clp;
  693. pvr2_sysfs_trace("Creating pvr2_sysfs_class id=%p",clp);
  694. clp->class.name = "pvrusb2";
  695. clp->class.class_release = pvr2_sysfs_class_release;
  696. clp->class.dev_release = pvr2_sysfs_release;
  697. if (class_register(&clp->class)) {
  698. pvr2_sysfs_trace(
  699. "Registration failed for pvr2_sysfs_class id=%p",clp);
  700. kfree(clp);
  701. clp = NULL;
  702. }
  703. return clp;
  704. }
  705. void pvr2_sysfs_class_destroy(struct pvr2_sysfs_class *clp)
  706. {
  707. class_unregister(&clp->class);
  708. }
  709. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  710. static ssize_t debuginfo_show(struct device *class_dev,
  711. struct device_attribute *attr, char *buf)
  712. {
  713. struct pvr2_sysfs *sfp;
  714. sfp = dev_get_drvdata(class_dev);
  715. if (!sfp) return -EINVAL;
  716. pvr2_hdw_trigger_module_log(sfp->channel.hdw);
  717. return pvr2_debugifc_print_info(sfp->channel.hdw,buf,PAGE_SIZE);
  718. }
  719. static ssize_t debugcmd_show(struct device *class_dev,
  720. struct device_attribute *attr, char *buf)
  721. {
  722. struct pvr2_sysfs *sfp;
  723. sfp = dev_get_drvdata(class_dev);
  724. if (!sfp) return -EINVAL;
  725. return pvr2_debugifc_print_status(sfp->channel.hdw,buf,PAGE_SIZE);
  726. }
  727. static ssize_t debugcmd_store(struct device *class_dev,
  728. struct device_attribute *attr,
  729. const char *buf, size_t count)
  730. {
  731. struct pvr2_sysfs *sfp;
  732. int ret;
  733. sfp = dev_get_drvdata(class_dev);
  734. if (!sfp) return -EINVAL;
  735. ret = pvr2_debugifc_docmd(sfp->channel.hdw,buf,count);
  736. if (ret < 0) return ret;
  737. return count;
  738. }
  739. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  740. /*
  741. Stuff for Emacs to see, in order to encourage consistent editing style:
  742. *** Local Variables: ***
  743. *** mode: c ***
  744. *** fill-column: 75 ***
  745. *** tab-width: 8 ***
  746. *** c-basic-offset: 8 ***
  747. *** End: ***
  748. */