pvrusb2-sysfs.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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. struct pvr2_sysfs *chptr;
  69. struct pvr2_sysfs_ctl_item *item_next;
  70. struct attribute *attr_gen[7];
  71. struct attribute_group grp;
  72. int created_ok;
  73. char name[80];
  74. };
  75. struct pvr2_sysfs_class {
  76. struct class class;
  77. };
  78. static ssize_t show_name(int id,struct device *class_dev,char *buf)
  79. {
  80. struct pvr2_ctrl *cptr;
  81. struct pvr2_sysfs *sfp;
  82. const char *name;
  83. sfp = (struct pvr2_sysfs *)class_dev->driver_data;
  84. if (!sfp) return -EINVAL;
  85. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
  86. if (!cptr) return -EINVAL;
  87. name = pvr2_ctrl_get_desc(cptr);
  88. pvr2_sysfs_trace("pvr2_sysfs(%p) show_name(cid=%d) is %s",sfp,id,name);
  89. if (!name) return -EINVAL;
  90. return scnprintf(buf,PAGE_SIZE,"%s\n",name);
  91. }
  92. static ssize_t show_type(int id,struct device *class_dev,char *buf)
  93. {
  94. struct pvr2_ctrl *cptr;
  95. struct pvr2_sysfs *sfp;
  96. const char *name;
  97. enum pvr2_ctl_type tp;
  98. sfp = (struct pvr2_sysfs *)class_dev->driver_data;
  99. if (!sfp) return -EINVAL;
  100. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
  101. if (!cptr) return -EINVAL;
  102. tp = pvr2_ctrl_get_type(cptr);
  103. switch (tp) {
  104. case pvr2_ctl_int: name = "integer"; break;
  105. case pvr2_ctl_enum: name = "enum"; break;
  106. case pvr2_ctl_bitmask: name = "bitmask"; break;
  107. case pvr2_ctl_bool: name = "boolean"; break;
  108. default: name = "?"; break;
  109. }
  110. pvr2_sysfs_trace("pvr2_sysfs(%p) show_type(cid=%d) is %s",sfp,id,name);
  111. if (!name) return -EINVAL;
  112. return scnprintf(buf,PAGE_SIZE,"%s\n",name);
  113. }
  114. static ssize_t show_min(int id,struct device *class_dev,char *buf)
  115. {
  116. struct pvr2_ctrl *cptr;
  117. struct pvr2_sysfs *sfp;
  118. long val;
  119. sfp = (struct pvr2_sysfs *)class_dev->driver_data;
  120. if (!sfp) return -EINVAL;
  121. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
  122. if (!cptr) return -EINVAL;
  123. val = pvr2_ctrl_get_min(cptr);
  124. pvr2_sysfs_trace("pvr2_sysfs(%p) show_min(cid=%d) is %ld",sfp,id,val);
  125. return scnprintf(buf,PAGE_SIZE,"%ld\n",val);
  126. }
  127. static ssize_t show_max(int id,struct device *class_dev,char *buf)
  128. {
  129. struct pvr2_ctrl *cptr;
  130. struct pvr2_sysfs *sfp;
  131. long val;
  132. sfp = (struct pvr2_sysfs *)class_dev->driver_data;
  133. if (!sfp) return -EINVAL;
  134. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
  135. if (!cptr) return -EINVAL;
  136. val = pvr2_ctrl_get_max(cptr);
  137. pvr2_sysfs_trace("pvr2_sysfs(%p) show_max(cid=%d) is %ld",sfp,id,val);
  138. return scnprintf(buf,PAGE_SIZE,"%ld\n",val);
  139. }
  140. static ssize_t show_val_norm(int id,struct device *class_dev,char *buf)
  141. {
  142. struct pvr2_ctrl *cptr;
  143. struct pvr2_sysfs *sfp;
  144. int val,ret;
  145. unsigned int cnt = 0;
  146. sfp = (struct pvr2_sysfs *)class_dev->driver_data;
  147. if (!sfp) return -EINVAL;
  148. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
  149. if (!cptr) return -EINVAL;
  150. ret = pvr2_ctrl_get_value(cptr,&val);
  151. if (ret < 0) return ret;
  152. ret = pvr2_ctrl_value_to_sym(cptr,~0,val,
  153. buf,PAGE_SIZE-1,&cnt);
  154. pvr2_sysfs_trace("pvr2_sysfs(%p) show_val_norm(cid=%d) is %.*s (%d)",
  155. sfp,id,cnt,buf,val);
  156. buf[cnt] = '\n';
  157. return cnt+1;
  158. }
  159. static ssize_t show_val_custom(int id,struct device *class_dev,char *buf)
  160. {
  161. struct pvr2_ctrl *cptr;
  162. struct pvr2_sysfs *sfp;
  163. int val,ret;
  164. unsigned int cnt = 0;
  165. sfp = (struct pvr2_sysfs *)class_dev->driver_data;
  166. if (!sfp) return -EINVAL;
  167. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
  168. if (!cptr) return -EINVAL;
  169. ret = pvr2_ctrl_get_value(cptr,&val);
  170. if (ret < 0) return ret;
  171. ret = pvr2_ctrl_custom_value_to_sym(cptr,~0,val,
  172. buf,PAGE_SIZE-1,&cnt);
  173. pvr2_sysfs_trace("pvr2_sysfs(%p) show_val_custom(cid=%d) is %.*s (%d)",
  174. sfp,id,cnt,buf,val);
  175. buf[cnt] = '\n';
  176. return cnt+1;
  177. }
  178. static ssize_t show_enum(int id,struct device *class_dev,char *buf)
  179. {
  180. struct pvr2_ctrl *cptr;
  181. struct pvr2_sysfs *sfp;
  182. long val;
  183. unsigned int bcnt,ccnt,ecnt;
  184. sfp = (struct pvr2_sysfs *)class_dev->driver_data;
  185. if (!sfp) return -EINVAL;
  186. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
  187. if (!cptr) return -EINVAL;
  188. ecnt = pvr2_ctrl_get_cnt(cptr);
  189. bcnt = 0;
  190. for (val = 0; val < ecnt; val++) {
  191. pvr2_ctrl_get_valname(cptr,val,buf+bcnt,PAGE_SIZE-bcnt,&ccnt);
  192. if (!ccnt) continue;
  193. bcnt += ccnt;
  194. if (bcnt >= PAGE_SIZE) break;
  195. buf[bcnt] = '\n';
  196. bcnt++;
  197. }
  198. pvr2_sysfs_trace("pvr2_sysfs(%p) show_enum(cid=%d)",sfp,id);
  199. return bcnt;
  200. }
  201. static ssize_t show_bits(int id,struct device *class_dev,char *buf)
  202. {
  203. struct pvr2_ctrl *cptr;
  204. struct pvr2_sysfs *sfp;
  205. int valid_bits,msk;
  206. unsigned int bcnt,ccnt;
  207. sfp = (struct pvr2_sysfs *)class_dev->driver_data;
  208. if (!sfp) return -EINVAL;
  209. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
  210. if (!cptr) return -EINVAL;
  211. valid_bits = pvr2_ctrl_get_mask(cptr);
  212. bcnt = 0;
  213. for (msk = 1; valid_bits; msk <<= 1) {
  214. if (!(msk & valid_bits)) continue;
  215. valid_bits &= ~msk;
  216. pvr2_ctrl_get_valname(cptr,msk,buf+bcnt,PAGE_SIZE-bcnt,&ccnt);
  217. bcnt += ccnt;
  218. if (bcnt >= PAGE_SIZE) break;
  219. buf[bcnt] = '\n';
  220. bcnt++;
  221. }
  222. pvr2_sysfs_trace("pvr2_sysfs(%p) show_bits(cid=%d)",sfp,id);
  223. return bcnt;
  224. }
  225. static int store_val_any(int id,int customfl,struct pvr2_sysfs *sfp,
  226. const char *buf,unsigned int count)
  227. {
  228. struct pvr2_ctrl *cptr;
  229. int ret;
  230. int mask,val;
  231. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,id);
  232. if (customfl) {
  233. ret = pvr2_ctrl_custom_sym_to_value(cptr,buf,count,&mask,&val);
  234. } else {
  235. ret = pvr2_ctrl_sym_to_value(cptr,buf,count,&mask,&val);
  236. }
  237. if (ret < 0) return ret;
  238. ret = pvr2_ctrl_set_mask_value(cptr,mask,val);
  239. pvr2_hdw_commit_ctl(sfp->channel.hdw);
  240. return ret;
  241. }
  242. static ssize_t store_val_norm(int id,struct device *class_dev,
  243. const char *buf,size_t count)
  244. {
  245. struct pvr2_sysfs *sfp;
  246. int ret;
  247. sfp = (struct pvr2_sysfs *)class_dev->driver_data;
  248. pvr2_sysfs_trace("pvr2_sysfs(%p) store_val_norm(cid=%d) \"%.*s\"",
  249. sfp,id,(int)count,buf);
  250. ret = store_val_any(id,0,sfp,buf,count);
  251. if (!ret) ret = count;
  252. return ret;
  253. }
  254. static ssize_t store_val_custom(int id,struct device *class_dev,
  255. const char *buf,size_t count)
  256. {
  257. struct pvr2_sysfs *sfp;
  258. int ret;
  259. sfp = (struct pvr2_sysfs *)class_dev->driver_data;
  260. pvr2_sysfs_trace("pvr2_sysfs(%p) store_val_custom(cid=%d) \"%.*s\"",
  261. sfp,id,(int)count,buf);
  262. ret = store_val_any(id,1,sfp,buf,count);
  263. if (!ret) ret = count;
  264. return ret;
  265. }
  266. /*
  267. Mike Isely <isely@pobox.com> 30-April-2005
  268. This next batch of horrible preprocessor hackery is needed because the
  269. kernel's device_attribute mechanism fails to pass the actual
  270. attribute through to the show / store functions, which means we have no
  271. way to package up any attribute-specific parameters, like for example the
  272. control id. So we work around this brain-damage by encoding the control
  273. id into the show / store functions themselves and pick the function based
  274. on the control id we're setting up. These macros try to ease the pain.
  275. Yuck.
  276. */
  277. #define CREATE_SHOW_INSTANCE(sf_name,ctl_id) \
  278. static ssize_t sf_name##_##ctl_id(struct device *class_dev, \
  279. struct device_attribute *attr, char *buf) \
  280. { return sf_name(ctl_id,class_dev,buf); }
  281. #define CREATE_STORE_INSTANCE(sf_name,ctl_id) \
  282. static ssize_t sf_name##_##ctl_id(struct device *class_dev, \
  283. struct device_attribute *attr, const char *buf, size_t count) \
  284. { return sf_name(ctl_id,class_dev,buf,count); }
  285. #define CREATE_BATCH(ctl_id) \
  286. CREATE_SHOW_INSTANCE(show_name,ctl_id) \
  287. CREATE_SHOW_INSTANCE(show_type,ctl_id) \
  288. CREATE_SHOW_INSTANCE(show_min,ctl_id) \
  289. CREATE_SHOW_INSTANCE(show_max,ctl_id) \
  290. CREATE_SHOW_INSTANCE(show_val_norm,ctl_id) \
  291. CREATE_SHOW_INSTANCE(show_val_custom,ctl_id) \
  292. CREATE_SHOW_INSTANCE(show_enum,ctl_id) \
  293. CREATE_SHOW_INSTANCE(show_bits,ctl_id) \
  294. CREATE_STORE_INSTANCE(store_val_norm,ctl_id) \
  295. CREATE_STORE_INSTANCE(store_val_custom,ctl_id) \
  296. CREATE_BATCH(0)
  297. CREATE_BATCH(1)
  298. CREATE_BATCH(2)
  299. CREATE_BATCH(3)
  300. CREATE_BATCH(4)
  301. CREATE_BATCH(5)
  302. CREATE_BATCH(6)
  303. CREATE_BATCH(7)
  304. CREATE_BATCH(8)
  305. CREATE_BATCH(9)
  306. CREATE_BATCH(10)
  307. CREATE_BATCH(11)
  308. CREATE_BATCH(12)
  309. CREATE_BATCH(13)
  310. CREATE_BATCH(14)
  311. CREATE_BATCH(15)
  312. CREATE_BATCH(16)
  313. CREATE_BATCH(17)
  314. CREATE_BATCH(18)
  315. CREATE_BATCH(19)
  316. CREATE_BATCH(20)
  317. CREATE_BATCH(21)
  318. CREATE_BATCH(22)
  319. CREATE_BATCH(23)
  320. CREATE_BATCH(24)
  321. CREATE_BATCH(25)
  322. CREATE_BATCH(26)
  323. CREATE_BATCH(27)
  324. CREATE_BATCH(28)
  325. CREATE_BATCH(29)
  326. CREATE_BATCH(30)
  327. CREATE_BATCH(31)
  328. CREATE_BATCH(32)
  329. CREATE_BATCH(33)
  330. CREATE_BATCH(34)
  331. CREATE_BATCH(35)
  332. CREATE_BATCH(36)
  333. CREATE_BATCH(37)
  334. CREATE_BATCH(38)
  335. CREATE_BATCH(39)
  336. CREATE_BATCH(40)
  337. CREATE_BATCH(41)
  338. CREATE_BATCH(42)
  339. CREATE_BATCH(43)
  340. CREATE_BATCH(44)
  341. CREATE_BATCH(45)
  342. CREATE_BATCH(46)
  343. CREATE_BATCH(47)
  344. CREATE_BATCH(48)
  345. CREATE_BATCH(49)
  346. CREATE_BATCH(50)
  347. CREATE_BATCH(51)
  348. CREATE_BATCH(52)
  349. CREATE_BATCH(53)
  350. CREATE_BATCH(54)
  351. CREATE_BATCH(55)
  352. CREATE_BATCH(56)
  353. CREATE_BATCH(57)
  354. CREATE_BATCH(58)
  355. CREATE_BATCH(59)
  356. struct pvr2_sysfs_func_set {
  357. ssize_t (*show_name)(struct device *,
  358. struct device_attribute *attr, char *);
  359. ssize_t (*show_type)(struct device *,
  360. struct device_attribute *attr, char *);
  361. ssize_t (*show_min)(struct device *,
  362. struct device_attribute *attr, char *);
  363. ssize_t (*show_max)(struct device *,
  364. struct device_attribute *attr, char *);
  365. ssize_t (*show_enum)(struct device *,
  366. struct device_attribute *attr, char *);
  367. ssize_t (*show_bits)(struct device *,
  368. struct device_attribute *attr, char *);
  369. ssize_t (*show_val_norm)(struct device *,
  370. struct device_attribute *attr, char *);
  371. ssize_t (*store_val_norm)(struct device *,
  372. struct device_attribute *attr,
  373. const char *,size_t);
  374. ssize_t (*show_val_custom)(struct device *,
  375. struct device_attribute *attr, char *);
  376. ssize_t (*store_val_custom)(struct device *,
  377. struct device_attribute *attr,
  378. const char *,size_t);
  379. };
  380. #define INIT_BATCH(ctl_id) \
  381. [ctl_id] = { \
  382. .show_name = show_name_##ctl_id, \
  383. .show_type = show_type_##ctl_id, \
  384. .show_min = show_min_##ctl_id, \
  385. .show_max = show_max_##ctl_id, \
  386. .show_enum = show_enum_##ctl_id, \
  387. .show_bits = show_bits_##ctl_id, \
  388. .show_val_norm = show_val_norm_##ctl_id, \
  389. .store_val_norm = store_val_norm_##ctl_id, \
  390. .show_val_custom = show_val_custom_##ctl_id, \
  391. .store_val_custom = store_val_custom_##ctl_id, \
  392. } \
  393. static struct pvr2_sysfs_func_set funcs[] = {
  394. INIT_BATCH(0),
  395. INIT_BATCH(1),
  396. INIT_BATCH(2),
  397. INIT_BATCH(3),
  398. INIT_BATCH(4),
  399. INIT_BATCH(5),
  400. INIT_BATCH(6),
  401. INIT_BATCH(7),
  402. INIT_BATCH(8),
  403. INIT_BATCH(9),
  404. INIT_BATCH(10),
  405. INIT_BATCH(11),
  406. INIT_BATCH(12),
  407. INIT_BATCH(13),
  408. INIT_BATCH(14),
  409. INIT_BATCH(15),
  410. INIT_BATCH(16),
  411. INIT_BATCH(17),
  412. INIT_BATCH(18),
  413. INIT_BATCH(19),
  414. INIT_BATCH(20),
  415. INIT_BATCH(21),
  416. INIT_BATCH(22),
  417. INIT_BATCH(23),
  418. INIT_BATCH(24),
  419. INIT_BATCH(25),
  420. INIT_BATCH(26),
  421. INIT_BATCH(27),
  422. INIT_BATCH(28),
  423. INIT_BATCH(29),
  424. INIT_BATCH(30),
  425. INIT_BATCH(31),
  426. INIT_BATCH(32),
  427. INIT_BATCH(33),
  428. INIT_BATCH(34),
  429. INIT_BATCH(35),
  430. INIT_BATCH(36),
  431. INIT_BATCH(37),
  432. INIT_BATCH(38),
  433. INIT_BATCH(39),
  434. INIT_BATCH(40),
  435. INIT_BATCH(41),
  436. INIT_BATCH(42),
  437. INIT_BATCH(43),
  438. INIT_BATCH(44),
  439. INIT_BATCH(45),
  440. INIT_BATCH(46),
  441. INIT_BATCH(47),
  442. INIT_BATCH(48),
  443. INIT_BATCH(49),
  444. INIT_BATCH(50),
  445. INIT_BATCH(51),
  446. INIT_BATCH(52),
  447. INIT_BATCH(53),
  448. INIT_BATCH(54),
  449. INIT_BATCH(55),
  450. INIT_BATCH(56),
  451. INIT_BATCH(57),
  452. INIT_BATCH(58),
  453. INIT_BATCH(59),
  454. };
  455. static void pvr2_sysfs_add_control(struct pvr2_sysfs *sfp,int ctl_id)
  456. {
  457. struct pvr2_sysfs_ctl_item *cip;
  458. struct pvr2_sysfs_func_set *fp;
  459. struct pvr2_ctrl *cptr;
  460. unsigned int cnt,acnt;
  461. int ret;
  462. if ((ctl_id < 0) || (ctl_id >= ARRAY_SIZE(funcs))) {
  463. return;
  464. }
  465. fp = funcs + ctl_id;
  466. cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,ctl_id);
  467. if (!cptr) return;
  468. cip = kzalloc(sizeof(*cip),GFP_KERNEL);
  469. if (!cip) return;
  470. pvr2_sysfs_trace("Creating pvr2_sysfs_ctl_item id=%p",cip);
  471. cip->cptr = cptr;
  472. cip->chptr = sfp;
  473. cip->item_next = NULL;
  474. if (sfp->item_last) {
  475. sfp->item_last->item_next = cip;
  476. } else {
  477. sfp->item_first = cip;
  478. }
  479. sfp->item_last = cip;
  480. cip->attr_name.attr.name = "name";
  481. cip->attr_name.attr.mode = S_IRUGO;
  482. cip->attr_name.show = fp->show_name;
  483. cip->attr_type.attr.name = "type";
  484. cip->attr_type.attr.mode = S_IRUGO;
  485. cip->attr_type.show = fp->show_type;
  486. cip->attr_min.attr.name = "min_val";
  487. cip->attr_min.attr.mode = S_IRUGO;
  488. cip->attr_min.show = fp->show_min;
  489. cip->attr_max.attr.name = "max_val";
  490. cip->attr_max.attr.mode = S_IRUGO;
  491. cip->attr_max.show = fp->show_max;
  492. cip->attr_val.attr.name = "cur_val";
  493. cip->attr_val.attr.mode = S_IRUGO;
  494. cip->attr_custom.attr.name = "custom_val";
  495. cip->attr_custom.attr.mode = S_IRUGO;
  496. cip->attr_enum.attr.name = "enum_val";
  497. cip->attr_enum.attr.mode = S_IRUGO;
  498. cip->attr_enum.show = fp->show_enum;
  499. cip->attr_bits.attr.name = "bit_val";
  500. cip->attr_bits.attr.mode = S_IRUGO;
  501. cip->attr_bits.show = fp->show_bits;
  502. if (pvr2_ctrl_is_writable(cptr)) {
  503. cip->attr_val.attr.mode |= S_IWUSR|S_IWGRP;
  504. cip->attr_custom.attr.mode |= S_IWUSR|S_IWGRP;
  505. }
  506. acnt = 0;
  507. cip->attr_gen[acnt++] = &cip->attr_name.attr;
  508. cip->attr_gen[acnt++] = &cip->attr_type.attr;
  509. cip->attr_gen[acnt++] = &cip->attr_val.attr;
  510. cip->attr_val.show = fp->show_val_norm;
  511. cip->attr_val.store = fp->store_val_norm;
  512. if (pvr2_ctrl_has_custom_symbols(cptr)) {
  513. cip->attr_gen[acnt++] = &cip->attr_custom.attr;
  514. cip->attr_custom.show = fp->show_val_custom;
  515. cip->attr_custom.store = fp->store_val_custom;
  516. }
  517. switch (pvr2_ctrl_get_type(cptr)) {
  518. case pvr2_ctl_enum:
  519. // Control is an enumeration
  520. cip->attr_gen[acnt++] = &cip->attr_enum.attr;
  521. break;
  522. case pvr2_ctl_int:
  523. // Control is an integer
  524. cip->attr_gen[acnt++] = &cip->attr_min.attr;
  525. cip->attr_gen[acnt++] = &cip->attr_max.attr;
  526. break;
  527. case pvr2_ctl_bitmask:
  528. // Control is an bitmask
  529. cip->attr_gen[acnt++] = &cip->attr_bits.attr;
  530. break;
  531. default: break;
  532. }
  533. cnt = scnprintf(cip->name,sizeof(cip->name)-1,"ctl_%s",
  534. pvr2_ctrl_get_name(cptr));
  535. cip->name[cnt] = 0;
  536. cip->grp.name = cip->name;
  537. cip->grp.attrs = cip->attr_gen;
  538. ret = sysfs_create_group(&sfp->class_dev->kobj,&cip->grp);
  539. if (ret) {
  540. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  541. "sysfs_create_group error: %d",
  542. ret);
  543. return;
  544. }
  545. cip->created_ok = !0;
  546. }
  547. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  548. static ssize_t debuginfo_show(struct device *, struct device_attribute *,
  549. char *);
  550. static ssize_t debugcmd_show(struct device *, struct device_attribute *,
  551. char *);
  552. static ssize_t debugcmd_store(struct device *, struct device_attribute *,
  553. const char *, size_t count);
  554. static void pvr2_sysfs_add_debugifc(struct pvr2_sysfs *sfp)
  555. {
  556. struct pvr2_sysfs_debugifc *dip;
  557. int ret;
  558. dip = kzalloc(sizeof(*dip),GFP_KERNEL);
  559. if (!dip) return;
  560. dip->attr_debugcmd.attr.name = "debugcmd";
  561. dip->attr_debugcmd.attr.mode = S_IRUGO|S_IWUSR|S_IWGRP;
  562. dip->attr_debugcmd.show = debugcmd_show;
  563. dip->attr_debugcmd.store = debugcmd_store;
  564. dip->attr_debuginfo.attr.name = "debuginfo";
  565. dip->attr_debuginfo.attr.mode = S_IRUGO;
  566. dip->attr_debuginfo.show = debuginfo_show;
  567. sfp->debugifc = dip;
  568. ret = device_create_file(sfp->class_dev,&dip->attr_debugcmd);
  569. if (ret < 0) {
  570. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  571. "device_create_file error: %d",
  572. ret);
  573. } else {
  574. dip->debugcmd_created_ok = !0;
  575. }
  576. ret = device_create_file(sfp->class_dev,&dip->attr_debuginfo);
  577. if (ret < 0) {
  578. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  579. "device_create_file error: %d",
  580. ret);
  581. } else {
  582. dip->debuginfo_created_ok = !0;
  583. }
  584. }
  585. static void pvr2_sysfs_tear_down_debugifc(struct pvr2_sysfs *sfp)
  586. {
  587. if (!sfp->debugifc) return;
  588. if (sfp->debugifc->debuginfo_created_ok) {
  589. device_remove_file(sfp->class_dev,
  590. &sfp->debugifc->attr_debuginfo);
  591. }
  592. if (sfp->debugifc->debugcmd_created_ok) {
  593. device_remove_file(sfp->class_dev,
  594. &sfp->debugifc->attr_debugcmd);
  595. }
  596. kfree(sfp->debugifc);
  597. sfp->debugifc = NULL;
  598. }
  599. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  600. static void pvr2_sysfs_add_controls(struct pvr2_sysfs *sfp)
  601. {
  602. unsigned int idx,cnt;
  603. cnt = pvr2_hdw_get_ctrl_count(sfp->channel.hdw);
  604. for (idx = 0; idx < cnt; idx++) {
  605. pvr2_sysfs_add_control(sfp,idx);
  606. }
  607. }
  608. static void pvr2_sysfs_tear_down_controls(struct pvr2_sysfs *sfp)
  609. {
  610. struct pvr2_sysfs_ctl_item *cip1,*cip2;
  611. for (cip1 = sfp->item_first; cip1; cip1 = cip2) {
  612. cip2 = cip1->item_next;
  613. if (cip1->created_ok) {
  614. sysfs_remove_group(&sfp->class_dev->kobj,&cip1->grp);
  615. }
  616. pvr2_sysfs_trace("Destroying pvr2_sysfs_ctl_item id=%p",cip1);
  617. kfree(cip1);
  618. }
  619. }
  620. static void pvr2_sysfs_class_release(struct class *class)
  621. {
  622. struct pvr2_sysfs_class *clp;
  623. clp = container_of(class,struct pvr2_sysfs_class,class);
  624. pvr2_sysfs_trace("Destroying pvr2_sysfs_class id=%p",clp);
  625. kfree(clp);
  626. }
  627. static void pvr2_sysfs_release(struct device *class_dev)
  628. {
  629. pvr2_sysfs_trace("Releasing class_dev id=%p",class_dev);
  630. kfree(class_dev);
  631. }
  632. static void class_dev_destroy(struct pvr2_sysfs *sfp)
  633. {
  634. if (!sfp->class_dev) return;
  635. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  636. pvr2_sysfs_tear_down_debugifc(sfp);
  637. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  638. pvr2_sysfs_tear_down_controls(sfp);
  639. if (sfp->hdw_desc_created_ok) {
  640. device_remove_file(sfp->class_dev,
  641. &sfp->attr_hdw_desc);
  642. }
  643. if (sfp->hdw_name_created_ok) {
  644. device_remove_file(sfp->class_dev,
  645. &sfp->attr_hdw_name);
  646. }
  647. if (sfp->bus_info_created_ok) {
  648. device_remove_file(sfp->class_dev,
  649. &sfp->attr_bus_info);
  650. }
  651. if (sfp->v4l_minor_number_created_ok) {
  652. device_remove_file(sfp->class_dev,
  653. &sfp->attr_v4l_minor_number);
  654. }
  655. if (sfp->v4l_radio_minor_number_created_ok) {
  656. device_remove_file(sfp->class_dev,
  657. &sfp->attr_v4l_radio_minor_number);
  658. }
  659. if (sfp->unit_number_created_ok) {
  660. device_remove_file(sfp->class_dev,
  661. &sfp->attr_unit_number);
  662. }
  663. pvr2_sysfs_trace("Destroying class_dev id=%p",sfp->class_dev);
  664. sfp->class_dev->driver_data = NULL;
  665. device_unregister(sfp->class_dev);
  666. sfp->class_dev = NULL;
  667. }
  668. static ssize_t v4l_minor_number_show(struct device *class_dev,
  669. struct device_attribute *attr, char *buf)
  670. {
  671. struct pvr2_sysfs *sfp;
  672. sfp = (struct pvr2_sysfs *)class_dev->driver_data;
  673. if (!sfp) return -EINVAL;
  674. return scnprintf(buf,PAGE_SIZE,"%d\n",
  675. pvr2_hdw_v4l_get_minor_number(sfp->channel.hdw,
  676. pvr2_v4l_type_video));
  677. }
  678. static ssize_t bus_info_show(struct device *class_dev,
  679. struct device_attribute *attr, char *buf)
  680. {
  681. struct pvr2_sysfs *sfp;
  682. sfp = (struct pvr2_sysfs *)class_dev->driver_data;
  683. if (!sfp) return -EINVAL;
  684. return scnprintf(buf,PAGE_SIZE,"%s\n",
  685. pvr2_hdw_get_bus_info(sfp->channel.hdw));
  686. }
  687. static ssize_t hdw_name_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. return scnprintf(buf,PAGE_SIZE,"%s\n",
  694. pvr2_hdw_get_type(sfp->channel.hdw));
  695. }
  696. static ssize_t hdw_desc_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 scnprintf(buf,PAGE_SIZE,"%s\n",
  703. pvr2_hdw_get_desc(sfp->channel.hdw));
  704. }
  705. static ssize_t v4l_radio_minor_number_show(struct device *class_dev,
  706. struct device_attribute *attr,
  707. char *buf)
  708. {
  709. struct pvr2_sysfs *sfp;
  710. sfp = (struct pvr2_sysfs *)class_dev->driver_data;
  711. if (!sfp) return -EINVAL;
  712. return scnprintf(buf,PAGE_SIZE,"%d\n",
  713. pvr2_hdw_v4l_get_minor_number(sfp->channel.hdw,
  714. pvr2_v4l_type_radio));
  715. }
  716. static ssize_t unit_number_show(struct device *class_dev,
  717. struct device_attribute *attr, char *buf)
  718. {
  719. struct pvr2_sysfs *sfp;
  720. sfp = (struct pvr2_sysfs *)class_dev->driver_data;
  721. if (!sfp) return -EINVAL;
  722. return scnprintf(buf,PAGE_SIZE,"%d\n",
  723. pvr2_hdw_get_unit_number(sfp->channel.hdw));
  724. }
  725. static void class_dev_create(struct pvr2_sysfs *sfp,
  726. struct pvr2_sysfs_class *class_ptr)
  727. {
  728. struct usb_device *usb_dev;
  729. struct device *class_dev;
  730. int ret;
  731. usb_dev = pvr2_hdw_get_dev(sfp->channel.hdw);
  732. if (!usb_dev) return;
  733. class_dev = kzalloc(sizeof(*class_dev),GFP_KERNEL);
  734. if (!class_dev) return;
  735. pvr2_sysfs_trace("Creating class_dev id=%p",class_dev);
  736. class_dev->class = &class_ptr->class;
  737. if (pvr2_hdw_get_sn(sfp->channel.hdw)) {
  738. snprintf(class_dev->bus_id, BUS_ID_SIZE, "sn-%lu",
  739. pvr2_hdw_get_sn(sfp->channel.hdw));
  740. } else if (pvr2_hdw_get_unit_number(sfp->channel.hdw) >= 0) {
  741. snprintf(class_dev->bus_id, BUS_ID_SIZE, "unit-%c",
  742. pvr2_hdw_get_unit_number(sfp->channel.hdw) + 'a');
  743. } else {
  744. kfree(class_dev);
  745. return;
  746. }
  747. class_dev->parent = &usb_dev->dev;
  748. sfp->class_dev = class_dev;
  749. class_dev->driver_data = sfp;
  750. ret = device_register(class_dev);
  751. if (ret) {
  752. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  753. "device_register failed");
  754. kfree(class_dev);
  755. return;
  756. }
  757. sfp->attr_v4l_minor_number.attr.name = "v4l_minor_number";
  758. sfp->attr_v4l_minor_number.attr.mode = S_IRUGO;
  759. sfp->attr_v4l_minor_number.show = v4l_minor_number_show;
  760. sfp->attr_v4l_minor_number.store = NULL;
  761. ret = device_create_file(sfp->class_dev,
  762. &sfp->attr_v4l_minor_number);
  763. if (ret < 0) {
  764. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  765. "device_create_file error: %d",
  766. ret);
  767. } else {
  768. sfp->v4l_minor_number_created_ok = !0;
  769. }
  770. sfp->attr_v4l_radio_minor_number.attr.name = "v4l_radio_minor_number";
  771. sfp->attr_v4l_radio_minor_number.attr.mode = S_IRUGO;
  772. sfp->attr_v4l_radio_minor_number.show = v4l_radio_minor_number_show;
  773. sfp->attr_v4l_radio_minor_number.store = NULL;
  774. ret = device_create_file(sfp->class_dev,
  775. &sfp->attr_v4l_radio_minor_number);
  776. if (ret < 0) {
  777. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  778. "device_create_file error: %d",
  779. ret);
  780. } else {
  781. sfp->v4l_radio_minor_number_created_ok = !0;
  782. }
  783. sfp->attr_unit_number.attr.name = "unit_number";
  784. sfp->attr_unit_number.attr.mode = S_IRUGO;
  785. sfp->attr_unit_number.show = unit_number_show;
  786. sfp->attr_unit_number.store = NULL;
  787. ret = device_create_file(sfp->class_dev,&sfp->attr_unit_number);
  788. if (ret < 0) {
  789. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  790. "device_create_file error: %d",
  791. ret);
  792. } else {
  793. sfp->unit_number_created_ok = !0;
  794. }
  795. sfp->attr_bus_info.attr.name = "bus_info_str";
  796. sfp->attr_bus_info.attr.mode = S_IRUGO;
  797. sfp->attr_bus_info.show = bus_info_show;
  798. sfp->attr_bus_info.store = NULL;
  799. ret = device_create_file(sfp->class_dev,
  800. &sfp->attr_bus_info);
  801. if (ret < 0) {
  802. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  803. "device_create_file error: %d",
  804. ret);
  805. } else {
  806. sfp->bus_info_created_ok = !0;
  807. }
  808. sfp->attr_hdw_name.attr.name = "device_hardware_type";
  809. sfp->attr_hdw_name.attr.mode = S_IRUGO;
  810. sfp->attr_hdw_name.show = hdw_name_show;
  811. sfp->attr_hdw_name.store = NULL;
  812. ret = device_create_file(sfp->class_dev,
  813. &sfp->attr_hdw_name);
  814. if (ret < 0) {
  815. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  816. "device_create_file error: %d",
  817. ret);
  818. } else {
  819. sfp->hdw_name_created_ok = !0;
  820. }
  821. sfp->attr_hdw_desc.attr.name = "device_hardware_description";
  822. sfp->attr_hdw_desc.attr.mode = S_IRUGO;
  823. sfp->attr_hdw_desc.show = hdw_desc_show;
  824. sfp->attr_hdw_desc.store = NULL;
  825. ret = device_create_file(sfp->class_dev,
  826. &sfp->attr_hdw_desc);
  827. if (ret < 0) {
  828. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  829. "device_create_file error: %d",
  830. ret);
  831. } else {
  832. sfp->hdw_desc_created_ok = !0;
  833. }
  834. pvr2_sysfs_add_controls(sfp);
  835. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  836. pvr2_sysfs_add_debugifc(sfp);
  837. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  838. }
  839. static void pvr2_sysfs_internal_check(struct pvr2_channel *chp)
  840. {
  841. struct pvr2_sysfs *sfp;
  842. sfp = container_of(chp,struct pvr2_sysfs,channel);
  843. if (!sfp->channel.mc_head->disconnect_flag) return;
  844. pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_sysfs id=%p",sfp);
  845. class_dev_destroy(sfp);
  846. pvr2_channel_done(&sfp->channel);
  847. kfree(sfp);
  848. }
  849. struct pvr2_sysfs *pvr2_sysfs_create(struct pvr2_context *mp,
  850. struct pvr2_sysfs_class *class_ptr)
  851. {
  852. struct pvr2_sysfs *sfp;
  853. sfp = kzalloc(sizeof(*sfp),GFP_KERNEL);
  854. if (!sfp) return sfp;
  855. pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_sysfs id=%p",sfp);
  856. pvr2_channel_init(&sfp->channel,mp);
  857. sfp->channel.check_func = pvr2_sysfs_internal_check;
  858. class_dev_create(sfp,class_ptr);
  859. return sfp;
  860. }
  861. struct pvr2_sysfs_class *pvr2_sysfs_class_create(void)
  862. {
  863. struct pvr2_sysfs_class *clp;
  864. clp = kzalloc(sizeof(*clp),GFP_KERNEL);
  865. if (!clp) return clp;
  866. pvr2_sysfs_trace("Creating pvr2_sysfs_class id=%p",clp);
  867. clp->class.name = "pvrusb2";
  868. clp->class.class_release = pvr2_sysfs_class_release;
  869. clp->class.dev_release = pvr2_sysfs_release;
  870. if (class_register(&clp->class)) {
  871. pvr2_sysfs_trace(
  872. "Registration failed for pvr2_sysfs_class id=%p",clp);
  873. kfree(clp);
  874. clp = NULL;
  875. }
  876. return clp;
  877. }
  878. void pvr2_sysfs_class_destroy(struct pvr2_sysfs_class *clp)
  879. {
  880. class_unregister(&clp->class);
  881. }
  882. #ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
  883. static ssize_t debuginfo_show(struct device *class_dev,
  884. struct device_attribute *attr, char *buf)
  885. {
  886. struct pvr2_sysfs *sfp;
  887. sfp = (struct pvr2_sysfs *)class_dev->driver_data;
  888. if (!sfp) return -EINVAL;
  889. pvr2_hdw_trigger_module_log(sfp->channel.hdw);
  890. return pvr2_debugifc_print_info(sfp->channel.hdw,buf,PAGE_SIZE);
  891. }
  892. static ssize_t debugcmd_show(struct device *class_dev,
  893. struct device_attribute *attr, char *buf)
  894. {
  895. struct pvr2_sysfs *sfp;
  896. sfp = (struct pvr2_sysfs *)class_dev->driver_data;
  897. if (!sfp) return -EINVAL;
  898. return pvr2_debugifc_print_status(sfp->channel.hdw,buf,PAGE_SIZE);
  899. }
  900. static ssize_t debugcmd_store(struct device *class_dev,
  901. struct device_attribute *attr,
  902. const char *buf, size_t count)
  903. {
  904. struct pvr2_sysfs *sfp;
  905. int ret;
  906. sfp = (struct pvr2_sysfs *)class_dev->driver_data;
  907. if (!sfp) return -EINVAL;
  908. ret = pvr2_debugifc_docmd(sfp->channel.hdw,buf,count);
  909. if (ret < 0) return ret;
  910. return count;
  911. }
  912. #endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
  913. /*
  914. Stuff for Emacs to see, in order to encourage consistent editing style:
  915. *** Local Variables: ***
  916. *** mode: c ***
  917. *** fill-column: 75 ***
  918. *** tab-width: 8 ***
  919. *** c-basic-offset: 8 ***
  920. *** End: ***
  921. */