pvrusb2-sysfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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. dip->attr_debugcmd.attr.name = "debugcmd";
  388. dip->attr_debugcmd.attr.mode = S_IRUGO|S_IWUSR|S_IWGRP;
  389. dip->attr_debugcmd.show = debugcmd_show;
  390. dip->attr_debugcmd.store = debugcmd_store;
  391. dip->attr_debuginfo.attr.name = "debuginfo";
  392. dip->attr_debuginfo.attr.mode = S_IRUGO;
  393. dip->attr_debuginfo.show = debuginfo_show;
  394. sfp->debugifc = dip;
  395. ret = device_create_file(sfp->class_dev,&dip->attr_debugcmd);
  396. if (ret < 0) {
  397. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  398. "device_create_file error: %d",
  399. ret);
  400. } else {
  401. dip->debugcmd_created_ok = !0;
  402. }
  403. ret = device_create_file(sfp->class_dev,&dip->attr_debuginfo);
  404. if (ret < 0) {
  405. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  406. "device_create_file error: %d",
  407. ret);
  408. } else {
  409. dip->debuginfo_created_ok = !0;
  410. }
  411. }
  412. static void pvr2_sysfs_tear_down_debugifc(struct pvr2_sysfs *sfp)
  413. {
  414. if (!sfp->debugifc) return;
  415. if (sfp->debugifc->debuginfo_created_ok) {
  416. device_remove_file(sfp->class_dev,
  417. &sfp->debugifc->attr_debuginfo);
  418. }
  419. if (sfp->debugifc->debugcmd_created_ok) {
  420. device_remove_file(sfp->class_dev,
  421. &sfp->debugifc->attr_debugcmd);
  422. }
  423. kfree(sfp->debugifc);
  424. sfp->debugifc = NULL;
  425. }
  426. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  427. static void pvr2_sysfs_add_controls(struct pvr2_sysfs *sfp)
  428. {
  429. unsigned int idx,cnt;
  430. cnt = pvr2_hdw_get_ctrl_count(sfp->channel.hdw);
  431. for (idx = 0; idx < cnt; idx++) {
  432. pvr2_sysfs_add_control(sfp,idx);
  433. }
  434. }
  435. static void pvr2_sysfs_tear_down_controls(struct pvr2_sysfs *sfp)
  436. {
  437. struct pvr2_sysfs_ctl_item *cip1,*cip2;
  438. for (cip1 = sfp->item_first; cip1; cip1 = cip2) {
  439. cip2 = cip1->item_next;
  440. if (cip1->created_ok) {
  441. sysfs_remove_group(&sfp->class_dev->kobj,&cip1->grp);
  442. }
  443. pvr2_sysfs_trace("Destroying pvr2_sysfs_ctl_item id=%p",cip1);
  444. kfree(cip1);
  445. }
  446. }
  447. static void pvr2_sysfs_class_release(struct class *class)
  448. {
  449. struct pvr2_sysfs_class *clp;
  450. clp = container_of(class,struct pvr2_sysfs_class,class);
  451. pvr2_sysfs_trace("Destroying pvr2_sysfs_class id=%p",clp);
  452. kfree(clp);
  453. }
  454. static void pvr2_sysfs_release(struct device *class_dev)
  455. {
  456. pvr2_sysfs_trace("Releasing class_dev id=%p",class_dev);
  457. kfree(class_dev);
  458. }
  459. static void class_dev_destroy(struct pvr2_sysfs *sfp)
  460. {
  461. if (!sfp->class_dev) return;
  462. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  463. pvr2_sysfs_tear_down_debugifc(sfp);
  464. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  465. pvr2_sysfs_tear_down_controls(sfp);
  466. if (sfp->hdw_desc_created_ok) {
  467. device_remove_file(sfp->class_dev,
  468. &sfp->attr_hdw_desc);
  469. }
  470. if (sfp->hdw_name_created_ok) {
  471. device_remove_file(sfp->class_dev,
  472. &sfp->attr_hdw_name);
  473. }
  474. if (sfp->bus_info_created_ok) {
  475. device_remove_file(sfp->class_dev,
  476. &sfp->attr_bus_info);
  477. }
  478. if (sfp->v4l_minor_number_created_ok) {
  479. device_remove_file(sfp->class_dev,
  480. &sfp->attr_v4l_minor_number);
  481. }
  482. if (sfp->v4l_radio_minor_number_created_ok) {
  483. device_remove_file(sfp->class_dev,
  484. &sfp->attr_v4l_radio_minor_number);
  485. }
  486. if (sfp->unit_number_created_ok) {
  487. device_remove_file(sfp->class_dev,
  488. &sfp->attr_unit_number);
  489. }
  490. pvr2_sysfs_trace("Destroying class_dev id=%p",sfp->class_dev);
  491. dev_set_drvdata(sfp->class_dev, NULL);
  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 = &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. kfree(class_dev);
  574. return;
  575. }
  576. sfp->attr_v4l_minor_number.attr.name = "v4l_minor_number";
  577. sfp->attr_v4l_minor_number.attr.mode = S_IRUGO;
  578. sfp->attr_v4l_minor_number.show = v4l_minor_number_show;
  579. sfp->attr_v4l_minor_number.store = NULL;
  580. ret = device_create_file(sfp->class_dev,
  581. &sfp->attr_v4l_minor_number);
  582. if (ret < 0) {
  583. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  584. "device_create_file error: %d",
  585. ret);
  586. } else {
  587. sfp->v4l_minor_number_created_ok = !0;
  588. }
  589. sfp->attr_v4l_radio_minor_number.attr.name = "v4l_radio_minor_number";
  590. sfp->attr_v4l_radio_minor_number.attr.mode = S_IRUGO;
  591. sfp->attr_v4l_radio_minor_number.show = v4l_radio_minor_number_show;
  592. sfp->attr_v4l_radio_minor_number.store = NULL;
  593. ret = device_create_file(sfp->class_dev,
  594. &sfp->attr_v4l_radio_minor_number);
  595. if (ret < 0) {
  596. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  597. "device_create_file error: %d",
  598. ret);
  599. } else {
  600. sfp->v4l_radio_minor_number_created_ok = !0;
  601. }
  602. sfp->attr_unit_number.attr.name = "unit_number";
  603. sfp->attr_unit_number.attr.mode = S_IRUGO;
  604. sfp->attr_unit_number.show = unit_number_show;
  605. sfp->attr_unit_number.store = NULL;
  606. ret = device_create_file(sfp->class_dev,&sfp->attr_unit_number);
  607. if (ret < 0) {
  608. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  609. "device_create_file error: %d",
  610. ret);
  611. } else {
  612. sfp->unit_number_created_ok = !0;
  613. }
  614. sfp->attr_bus_info.attr.name = "bus_info_str";
  615. sfp->attr_bus_info.attr.mode = S_IRUGO;
  616. sfp->attr_bus_info.show = bus_info_show;
  617. sfp->attr_bus_info.store = NULL;
  618. ret = device_create_file(sfp->class_dev,
  619. &sfp->attr_bus_info);
  620. if (ret < 0) {
  621. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  622. "device_create_file error: %d",
  623. ret);
  624. } else {
  625. sfp->bus_info_created_ok = !0;
  626. }
  627. sfp->attr_hdw_name.attr.name = "device_hardware_type";
  628. sfp->attr_hdw_name.attr.mode = S_IRUGO;
  629. sfp->attr_hdw_name.show = hdw_name_show;
  630. sfp->attr_hdw_name.store = NULL;
  631. ret = device_create_file(sfp->class_dev,
  632. &sfp->attr_hdw_name);
  633. if (ret < 0) {
  634. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  635. "device_create_file error: %d",
  636. ret);
  637. } else {
  638. sfp->hdw_name_created_ok = !0;
  639. }
  640. sfp->attr_hdw_desc.attr.name = "device_hardware_description";
  641. sfp->attr_hdw_desc.attr.mode = S_IRUGO;
  642. sfp->attr_hdw_desc.show = hdw_desc_show;
  643. sfp->attr_hdw_desc.store = NULL;
  644. ret = device_create_file(sfp->class_dev,
  645. &sfp->attr_hdw_desc);
  646. if (ret < 0) {
  647. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  648. "device_create_file error: %d",
  649. ret);
  650. } else {
  651. sfp->hdw_desc_created_ok = !0;
  652. }
  653. pvr2_sysfs_add_controls(sfp);
  654. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  655. pvr2_sysfs_add_debugifc(sfp);
  656. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  657. }
  658. static void pvr2_sysfs_internal_check(struct pvr2_channel *chp)
  659. {
  660. struct pvr2_sysfs *sfp;
  661. sfp = container_of(chp,struct pvr2_sysfs,channel);
  662. if (!sfp->channel.mc_head->disconnect_flag) return;
  663. pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_sysfs id=%p",sfp);
  664. class_dev_destroy(sfp);
  665. pvr2_channel_done(&sfp->channel);
  666. kfree(sfp);
  667. }
  668. struct pvr2_sysfs *pvr2_sysfs_create(struct pvr2_context *mp,
  669. struct pvr2_sysfs_class *class_ptr)
  670. {
  671. struct pvr2_sysfs *sfp;
  672. sfp = kzalloc(sizeof(*sfp),GFP_KERNEL);
  673. if (!sfp) return sfp;
  674. pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_sysfs id=%p",sfp);
  675. pvr2_channel_init(&sfp->channel,mp);
  676. sfp->channel.check_func = pvr2_sysfs_internal_check;
  677. class_dev_create(sfp,class_ptr);
  678. return sfp;
  679. }
  680. struct pvr2_sysfs_class *pvr2_sysfs_class_create(void)
  681. {
  682. struct pvr2_sysfs_class *clp;
  683. clp = kzalloc(sizeof(*clp),GFP_KERNEL);
  684. if (!clp) return clp;
  685. pvr2_sysfs_trace("Creating pvr2_sysfs_class id=%p",clp);
  686. clp->class.name = "pvrusb2";
  687. clp->class.class_release = pvr2_sysfs_class_release;
  688. clp->class.dev_release = pvr2_sysfs_release;
  689. if (class_register(&clp->class)) {
  690. pvr2_sysfs_trace(
  691. "Registration failed for pvr2_sysfs_class id=%p",clp);
  692. kfree(clp);
  693. clp = NULL;
  694. }
  695. return clp;
  696. }
  697. void pvr2_sysfs_class_destroy(struct pvr2_sysfs_class *clp)
  698. {
  699. class_unregister(&clp->class);
  700. }
  701. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  702. static ssize_t debuginfo_show(struct device *class_dev,
  703. struct device_attribute *attr, char *buf)
  704. {
  705. struct pvr2_sysfs *sfp;
  706. sfp = dev_get_drvdata(class_dev);
  707. if (!sfp) return -EINVAL;
  708. pvr2_hdw_trigger_module_log(sfp->channel.hdw);
  709. return pvr2_debugifc_print_info(sfp->channel.hdw,buf,PAGE_SIZE);
  710. }
  711. static ssize_t debugcmd_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. return pvr2_debugifc_print_status(sfp->channel.hdw,buf,PAGE_SIZE);
  718. }
  719. static ssize_t debugcmd_store(struct device *class_dev,
  720. struct device_attribute *attr,
  721. const char *buf, size_t count)
  722. {
  723. struct pvr2_sysfs *sfp;
  724. int ret;
  725. sfp = dev_get_drvdata(class_dev);
  726. if (!sfp) return -EINVAL;
  727. ret = pvr2_debugifc_docmd(sfp->channel.hdw,buf,count);
  728. if (ret < 0) return ret;
  729. return count;
  730. }
  731. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  732. /*
  733. Stuff for Emacs to see, in order to encourage consistent editing style:
  734. *** Local Variables: ***
  735. *** mode: c ***
  736. *** fill-column: 75 ***
  737. *** tab-width: 8 ***
  738. *** c-basic-offset: 8 ***
  739. *** End: ***
  740. */