pvrusb2-sysfs.c 23 KB

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