pvrusb2-ioread.c 12 KB

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