pvrusb2-sysfs.c 22 KB

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