pvrusb2-ioread.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. */
  16. #include "pvrusb2-ioread.h"
  17. #include "pvrusb2-debug.h"
  18. #include <linux/errno.h>
  19. #include <linux/string.h>
  20. #include <linux/mm.h>
  21. #include <linux/slab.h>
  22. #include <linux/mutex.h>
  23. #include <linux/uaccess.h>
  24. #define BUFFER_COUNT 32
  25. #define BUFFER_SIZE PAGE_ALIGN(0x4000)
  26. struct pvr2_ioread {
  27. struct pvr2_stream *stream;
  28. char *buffer_storage[BUFFER_COUNT];
  29. char *sync_key_ptr;
  30. unsigned int sync_key_len;
  31. unsigned int sync_buf_offs;
  32. unsigned int sync_state;
  33. unsigned int sync_trashed_count;
  34. int enabled; // Streaming is on
  35. int spigot_open; // OK to pass data to client
  36. int stream_running; // Passing data to client now
  37. /* State relevant to current buffer being read */
  38. struct pvr2_buffer *c_buf;
  39. char *c_data_ptr;
  40. unsigned int c_data_len;
  41. unsigned int c_data_offs;
  42. struct mutex mutex;
  43. };
  44. static int pvr2_ioread_init(struct pvr2_ioread *cp)
  45. {
  46. unsigned int idx;
  47. cp->stream = NULL;
  48. mutex_init(&cp->mutex);
  49. for (idx = 0; idx < BUFFER_COUNT; idx++) {
  50. cp->buffer_storage[idx] = kmalloc(BUFFER_SIZE,GFP_KERNEL);
  51. if (!(cp->buffer_storage[idx])) break;
  52. }
  53. if (idx < BUFFER_COUNT) {
  54. // An allocation appears to have failed
  55. for (idx = 0; idx < BUFFER_COUNT; idx++) {
  56. if (!(cp->buffer_storage[idx])) continue;
  57. kfree(cp->buffer_storage[idx]);
  58. }
  59. return -ENOMEM;
  60. }
  61. return 0;
  62. }
  63. static void pvr2_ioread_done(struct pvr2_ioread *cp)
  64. {
  65. unsigned int idx;
  66. pvr2_ioread_setup(cp,NULL);
  67. for (idx = 0; idx < BUFFER_COUNT; idx++) {
  68. if (!(cp->buffer_storage[idx])) continue;
  69. kfree(cp->buffer_storage[idx]);
  70. }
  71. }
  72. struct pvr2_ioread *pvr2_ioread_create(void)
  73. {
  74. struct pvr2_ioread *cp;
  75. cp = kzalloc(sizeof(*cp),GFP_KERNEL);
  76. if (!cp) return NULL;
  77. pvr2_trace(PVR2_TRACE_STRUCT,"pvr2_ioread_create id=%p",cp);
  78. if (pvr2_ioread_init(cp) < 0) {
  79. kfree(cp);
  80. return NULL;
  81. }
  82. return cp;
  83. }
  84. void pvr2_ioread_destroy(struct pvr2_ioread *cp)
  85. {
  86. if (!cp) return;
  87. pvr2_ioread_done(cp);
  88. pvr2_trace(PVR2_TRACE_STRUCT,"pvr2_ioread_destroy id=%p",cp);
  89. if (cp->sync_key_ptr) {
  90. kfree(cp->sync_key_ptr);
  91. cp->sync_key_ptr = NULL;
  92. }
  93. kfree(cp);
  94. }
  95. void pvr2_ioread_set_sync_key(struct pvr2_ioread *cp,
  96. const char *sync_key_ptr,
  97. unsigned int sync_key_len)
  98. {
  99. if (!cp) return;
  100. if (!sync_key_ptr) sync_key_len = 0;
  101. if ((sync_key_len == cp->sync_key_len) &&
  102. ((!sync_key_len) ||
  103. (!memcmp(sync_key_ptr,cp->sync_key_ptr,sync_key_len)))) return;
  104. if (sync_key_len != cp->sync_key_len) {
  105. if (cp->sync_key_ptr) {
  106. kfree(cp->sync_key_ptr);
  107. cp->sync_key_ptr = NULL;
  108. }
  109. cp->sync_key_len = 0;
  110. if (sync_key_len) {
  111. cp->sync_key_ptr = kmalloc(sync_key_len,GFP_KERNEL);
  112. if (cp->sync_key_ptr) {
  113. cp->sync_key_len = sync_key_len;
  114. }
  115. }
  116. }
  117. if (!cp->sync_key_len) return;
  118. memcpy(cp->sync_key_ptr,sync_key_ptr,cp->sync_key_len);
  119. }
  120. static void pvr2_ioread_stop(struct pvr2_ioread *cp)
  121. {
  122. if (!(cp->enabled)) return;
  123. pvr2_trace(PVR2_TRACE_START_STOP,
  124. "/*---TRACE_READ---*/ pvr2_ioread_stop id=%p",cp);
  125. pvr2_stream_kill(cp->stream);
  126. cp->c_buf = NULL;
  127. cp->c_data_ptr = NULL;
  128. cp->c_data_len = 0;
  129. cp->c_data_offs = 0;
  130. cp->enabled = 0;
  131. cp->stream_running = 0;
  132. cp->spigot_open = 0;
  133. if (cp->sync_state) {
  134. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  135. "/*---TRACE_READ---*/ sync_state <== 0");
  136. cp->sync_state = 0;
  137. }
  138. }
  139. static int pvr2_ioread_start(struct pvr2_ioread *cp)
  140. {
  141. int stat;
  142. struct pvr2_buffer *bp;
  143. if (cp->enabled) return 0;
  144. if (!(cp->stream)) return 0;
  145. pvr2_trace(PVR2_TRACE_START_STOP,
  146. "/*---TRACE_READ---*/ pvr2_ioread_start id=%p",cp);
  147. while ((bp = pvr2_stream_get_idle_buffer(cp->stream)) != NULL) {
  148. stat = pvr2_buffer_queue(bp);
  149. if (stat < 0) {
  150. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  151. "/*---TRACE_READ---*/ pvr2_ioread_start id=%p error=%d",
  152. cp,stat);
  153. pvr2_ioread_stop(cp);
  154. return stat;
  155. }
  156. }
  157. cp->enabled = !0;
  158. cp->c_buf = NULL;
  159. cp->c_data_ptr = NULL;
  160. cp->c_data_len = 0;
  161. cp->c_data_offs = 0;
  162. cp->stream_running = 0;
  163. if (cp->sync_key_len) {
  164. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  165. "/*---TRACE_READ---*/ sync_state <== 1");
  166. cp->sync_state = 1;
  167. cp->sync_trashed_count = 0;
  168. cp->sync_buf_offs = 0;
  169. }
  170. cp->spigot_open = 0;
  171. return 0;
  172. }
  173. struct pvr2_stream *pvr2_ioread_get_stream(struct pvr2_ioread *cp)
  174. {
  175. return cp->stream;
  176. }
  177. int pvr2_ioread_setup(struct pvr2_ioread *cp,struct pvr2_stream *sp)
  178. {
  179. int ret;
  180. unsigned int idx;
  181. struct pvr2_buffer *bp;
  182. mutex_lock(&cp->mutex);
  183. do {
  184. if (cp->stream) {
  185. pvr2_trace(PVR2_TRACE_START_STOP,
  186. "/*---TRACE_READ---*/ pvr2_ioread_setup (tear-down) id=%p",
  187. cp);
  188. pvr2_ioread_stop(cp);
  189. pvr2_stream_kill(cp->stream);
  190. if (pvr2_stream_get_buffer_count(cp->stream)) {
  191. pvr2_stream_set_buffer_count(cp->stream,0);
  192. }
  193. cp->stream = NULL;
  194. }
  195. if (sp) {
  196. pvr2_trace(PVR2_TRACE_START_STOP,
  197. "/*---TRACE_READ---*/ pvr2_ioread_setup (setup) id=%p",
  198. cp);
  199. pvr2_stream_kill(sp);
  200. ret = pvr2_stream_set_buffer_count(sp,BUFFER_COUNT);
  201. if (ret < 0) {
  202. mutex_unlock(&cp->mutex);
  203. return ret;
  204. }
  205. for (idx = 0; idx < BUFFER_COUNT; idx++) {
  206. bp = pvr2_stream_get_buffer(sp,idx);
  207. pvr2_buffer_set_buffer(bp,
  208. cp->buffer_storage[idx],
  209. BUFFER_SIZE);
  210. }
  211. cp->stream = sp;
  212. }
  213. } while (0);
  214. mutex_unlock(&cp->mutex);
  215. return 0;
  216. }
  217. int pvr2_ioread_set_enabled(struct pvr2_ioread *cp,int fl)
  218. {
  219. int ret = 0;
  220. if ((!fl) == (!(cp->enabled))) return ret;
  221. mutex_lock(&cp->mutex);
  222. do {
  223. if (fl) {
  224. ret = pvr2_ioread_start(cp);
  225. } else {
  226. pvr2_ioread_stop(cp);
  227. }
  228. } while (0);
  229. mutex_unlock(&cp->mutex);
  230. return ret;
  231. }
  232. static int pvr2_ioread_get_buffer(struct pvr2_ioread *cp)
  233. {
  234. int stat;
  235. while (cp->c_data_len <= cp->c_data_offs) {
  236. if (cp->c_buf) {
  237. // Flush out current buffer first.
  238. stat = pvr2_buffer_queue(cp->c_buf);
  239. if (stat < 0) {
  240. // Streaming error...
  241. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  242. "/*---TRACE_READ---*/ pvr2_ioread_read id=%p queue_error=%d",
  243. cp,stat);
  244. pvr2_ioread_stop(cp);
  245. return 0;
  246. }
  247. cp->c_buf = NULL;
  248. cp->c_data_ptr = NULL;
  249. cp->c_data_len = 0;
  250. cp->c_data_offs = 0;
  251. }
  252. // Now get a freshly filled buffer.
  253. cp->c_buf = pvr2_stream_get_ready_buffer(cp->stream);
  254. if (!cp->c_buf) break; // Nothing ready; done.
  255. cp->c_data_len = pvr2_buffer_get_count(cp->c_buf);
  256. if (!cp->c_data_len) {
  257. // Nothing transferred. Was there an error?
  258. stat = pvr2_buffer_get_status(cp->c_buf);
  259. if (stat < 0) {
  260. // Streaming error...
  261. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  262. "/*---TRACE_READ---*/ pvr2_ioread_read id=%p buffer_error=%d",
  263. cp,stat);
  264. pvr2_ioread_stop(cp);
  265. // Give up.
  266. return 0;
  267. }
  268. // Start over...
  269. continue;
  270. }
  271. cp->c_data_offs = 0;
  272. cp->c_data_ptr = cp->buffer_storage[
  273. pvr2_buffer_get_id(cp->c_buf)];
  274. }
  275. return !0;
  276. }
  277. static void pvr2_ioread_filter(struct pvr2_ioread *cp)
  278. {
  279. unsigned int idx;
  280. if (!cp->enabled) return;
  281. if (cp->sync_state != 1) return;
  282. // Search the stream for our synchronization key. This is made
  283. // complicated by the fact that in order to be honest with
  284. // ourselves here we must search across buffer boundaries...
  285. mutex_lock(&cp->mutex);
  286. while (1) {
  287. // Ensure we have a buffer
  288. if (!pvr2_ioread_get_buffer(cp)) break;
  289. if (!cp->c_data_len) break;
  290. // Now walk the buffer contents until we match the key or
  291. // run out of buffer data.
  292. for (idx = cp->c_data_offs; idx < cp->c_data_len; idx++) {
  293. if (cp->sync_buf_offs >= cp->sync_key_len) break;
  294. if (cp->c_data_ptr[idx] ==
  295. cp->sync_key_ptr[cp->sync_buf_offs]) {
  296. // Found the next key byte
  297. (cp->sync_buf_offs)++;
  298. } else {
  299. // Whoops, mismatched. Start key over...
  300. cp->sync_buf_offs = 0;
  301. }
  302. }
  303. // Consume what we've walked through
  304. cp->c_data_offs += idx;
  305. cp->sync_trashed_count += idx;
  306. // If we've found the key, then update state and get out.
  307. if (cp->sync_buf_offs >= cp->sync_key_len) {
  308. cp->sync_trashed_count -= cp->sync_key_len;
  309. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  310. "/*---TRACE_READ---*/ sync_state <== 2 (skipped %u bytes)",
  311. cp->sync_trashed_count);
  312. cp->sync_state = 2;
  313. cp->sync_buf_offs = 0;
  314. break;
  315. }
  316. if (cp->c_data_offs < cp->c_data_len) {
  317. // Sanity check - should NEVER get here
  318. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  319. "ERROR: pvr2_ioread filter sync problem len=%u offs=%u",
  320. cp->c_data_len,cp->c_data_offs);
  321. // Get out so we don't get stuck in an infinite
  322. // loop.
  323. break;
  324. }
  325. continue; // (for clarity)
  326. }
  327. mutex_unlock(&cp->mutex);
  328. }
  329. int pvr2_ioread_avail(struct pvr2_ioread *cp)
  330. {
  331. int ret;
  332. if (!(cp->enabled)) {
  333. // Stream is not enabled; so this is an I/O error
  334. return -EIO;
  335. }
  336. if (cp->sync_state == 1) {
  337. pvr2_ioread_filter(cp);
  338. if (cp->sync_state == 1) return -EAGAIN;
  339. }
  340. ret = 0;
  341. if (cp->stream_running) {
  342. if (!pvr2_stream_get_ready_count(cp->stream)) {
  343. // No data available at all right now.
  344. ret = -EAGAIN;
  345. }
  346. } else {
  347. if (pvr2_stream_get_ready_count(cp->stream) < BUFFER_COUNT/2) {
  348. // Haven't buffered up enough yet; try again later
  349. ret = -EAGAIN;
  350. }
  351. }
  352. if ((!(cp->spigot_open)) != (!(ret == 0))) {
  353. cp->spigot_open = (ret == 0);
  354. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  355. "/*---TRACE_READ---*/ data is %s",
  356. cp->spigot_open ? "available" : "pending");
  357. }
  358. return ret;
  359. }
  360. int pvr2_ioread_read(struct pvr2_ioread *cp,void __user *buf,unsigned int cnt)
  361. {
  362. unsigned int copied_cnt;
  363. unsigned int bcnt;
  364. const char *src;
  365. int stat;
  366. int ret = 0;
  367. unsigned int req_cnt = cnt;
  368. if (!cnt) {
  369. pvr2_trace(PVR2_TRACE_TRAP,
  370. "/*---TRACE_READ---*/ pvr2_ioread_read id=%p ZERO Request? Returning zero.",
  371. cp);
  372. return 0;
  373. }
  374. stat = pvr2_ioread_avail(cp);
  375. if (stat < 0) return stat;
  376. cp->stream_running = !0;
  377. mutex_lock(&cp->mutex);
  378. do {
  379. // Suck data out of the buffers and copy to the user
  380. copied_cnt = 0;
  381. if (!buf) cnt = 0;
  382. while (1) {
  383. if (!pvr2_ioread_get_buffer(cp)) {
  384. ret = -EIO;
  385. break;
  386. }
  387. if (!cnt) break;
  388. if (cp->sync_state == 2) {
  389. // We're repeating the sync key data into
  390. // the stream.
  391. src = cp->sync_key_ptr + cp->sync_buf_offs;
  392. bcnt = cp->sync_key_len - cp->sync_buf_offs;
  393. } else {
  394. // Normal buffer copy
  395. src = cp->c_data_ptr + cp->c_data_offs;
  396. bcnt = cp->c_data_len - cp->c_data_offs;
  397. }
  398. if (!bcnt) break;
  399. // Don't run past user's buffer
  400. if (bcnt > cnt) bcnt = cnt;
  401. if (copy_to_user(buf,src,bcnt)) {
  402. // User supplied a bad pointer?
  403. // Give up - this *will* cause data
  404. // to be lost.
  405. ret = -EFAULT;
  406. break;
  407. }
  408. cnt -= bcnt;
  409. buf += bcnt;
  410. copied_cnt += bcnt;
  411. if (cp->sync_state == 2) {
  412. // Update offset inside sync key that we're
  413. // repeating back out.
  414. cp->sync_buf_offs += bcnt;
  415. if (cp->sync_buf_offs >= cp->sync_key_len) {
  416. // Consumed entire key; switch mode
  417. // to normal.
  418. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  419. "/*---TRACE_READ---*/ sync_state <== 0");
  420. cp->sync_state = 0;
  421. }
  422. } else {
  423. // Update buffer offset.
  424. cp->c_data_offs += bcnt;
  425. }
  426. }
  427. } while (0);
  428. mutex_unlock(&cp->mutex);
  429. if (!ret) {
  430. if (copied_cnt) {
  431. // If anything was copied, return that count
  432. ret = copied_cnt;
  433. } else {
  434. // Nothing copied; suggest to caller that another
  435. // attempt should be tried again later
  436. ret = -EAGAIN;
  437. }
  438. }
  439. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  440. "/*---TRACE_READ---*/ pvr2_ioread_read id=%p request=%d result=%d",
  441. cp,req_cnt,ret);
  442. return ret;
  443. }