pvrusb2-ioread.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. *
  3. * $Id$
  4. *
  5. * Copyright (C) 2005 Mike Isely <isely@pobox.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include "pvrusb2-ioread.h"
  22. #include "pvrusb2-debug.h"
  23. #include <linux/errno.h>
  24. #include <linux/string.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 = kmalloc(sizeof(*cp),GFP_KERNEL);
  80. if (!cp) return NULL;
  81. pvr2_trace(PVR2_TRACE_STRUCT,"pvr2_ioread_create id=%p",cp);
  82. memset(cp,0,sizeof(*cp));
  83. if (pvr2_ioread_init(cp) < 0) {
  84. kfree(cp);
  85. return NULL;
  86. }
  87. return cp;
  88. }
  89. void pvr2_ioread_destroy(struct pvr2_ioread *cp)
  90. {
  91. if (!cp) return;
  92. pvr2_ioread_done(cp);
  93. pvr2_trace(PVR2_TRACE_STRUCT,"pvr2_ioread_destroy id=%p",cp);
  94. if (cp->sync_key_ptr) {
  95. kfree(cp->sync_key_ptr);
  96. cp->sync_key_ptr = NULL;
  97. }
  98. kfree(cp);
  99. }
  100. void pvr2_ioread_set_sync_key(struct pvr2_ioread *cp,
  101. const char *sync_key_ptr,
  102. unsigned int sync_key_len)
  103. {
  104. if (!cp) return;
  105. if (!sync_key_ptr) sync_key_len = 0;
  106. if ((sync_key_len == cp->sync_key_len) &&
  107. ((!sync_key_len) ||
  108. (!memcmp(sync_key_ptr,cp->sync_key_ptr,sync_key_len)))) return;
  109. if (sync_key_len != cp->sync_key_len) {
  110. if (cp->sync_key_ptr) {
  111. kfree(cp->sync_key_ptr);
  112. cp->sync_key_ptr = NULL;
  113. }
  114. cp->sync_key_len = 0;
  115. if (sync_key_len) {
  116. cp->sync_key_ptr = kmalloc(sync_key_len,GFP_KERNEL);
  117. if (cp->sync_key_ptr) {
  118. cp->sync_key_len = sync_key_len;
  119. }
  120. }
  121. }
  122. if (!cp->sync_key_len) return;
  123. memcpy(cp->sync_key_ptr,sync_key_ptr,cp->sync_key_len);
  124. }
  125. static void pvr2_ioread_stop(struct pvr2_ioread *cp)
  126. {
  127. if (!(cp->enabled)) return;
  128. pvr2_trace(PVR2_TRACE_START_STOP,
  129. "/*---TRACE_READ---*/ pvr2_ioread_stop id=%p",cp);
  130. pvr2_stream_kill(cp->stream);
  131. cp->c_buf = NULL;
  132. cp->c_data_ptr = NULL;
  133. cp->c_data_len = 0;
  134. cp->c_data_offs = 0;
  135. cp->enabled = 0;
  136. cp->stream_running = 0;
  137. cp->spigot_open = 0;
  138. if (cp->sync_state) {
  139. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  140. "/*---TRACE_READ---*/ sync_state <== 0");
  141. cp->sync_state = 0;
  142. }
  143. }
  144. static int pvr2_ioread_start(struct pvr2_ioread *cp)
  145. {
  146. int stat;
  147. struct pvr2_buffer *bp;
  148. if (cp->enabled) return 0;
  149. if (!(cp->stream)) return 0;
  150. pvr2_trace(PVR2_TRACE_START_STOP,
  151. "/*---TRACE_READ---*/ pvr2_ioread_start id=%p",cp);
  152. while ((bp = pvr2_stream_get_idle_buffer(cp->stream)) != 0) {
  153. stat = pvr2_buffer_queue(bp);
  154. if (stat < 0) {
  155. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  156. "/*---TRACE_READ---*/"
  157. " pvr2_ioread_start id=%p"
  158. " error=%d",
  159. cp,stat);
  160. pvr2_ioread_stop(cp);
  161. return stat;
  162. }
  163. }
  164. cp->enabled = !0;
  165. cp->c_buf = NULL;
  166. cp->c_data_ptr = NULL;
  167. cp->c_data_len = 0;
  168. cp->c_data_offs = 0;
  169. cp->stream_running = 0;
  170. if (cp->sync_key_len) {
  171. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  172. "/*---TRACE_READ---*/ sync_state <== 1");
  173. cp->sync_state = 1;
  174. cp->sync_trashed_count = 0;
  175. cp->sync_buf_offs = 0;
  176. }
  177. cp->spigot_open = 0;
  178. return 0;
  179. }
  180. struct pvr2_stream *pvr2_ioread_get_stream(struct pvr2_ioread *cp)
  181. {
  182. return cp->stream;
  183. }
  184. int pvr2_ioread_setup(struct pvr2_ioread *cp,struct pvr2_stream *sp)
  185. {
  186. int ret;
  187. unsigned int idx;
  188. struct pvr2_buffer *bp;
  189. mutex_lock(&cp->mutex); do {
  190. if (cp->stream) {
  191. pvr2_trace(PVR2_TRACE_START_STOP,
  192. "/*---TRACE_READ---*/"
  193. " pvr2_ioread_setup (tear-down) id=%p",cp);
  194. pvr2_ioread_stop(cp);
  195. pvr2_stream_kill(cp->stream);
  196. if (pvr2_stream_get_buffer_count(cp->stream)) {
  197. pvr2_stream_set_buffer_count(cp->stream,0);
  198. }
  199. cp->stream = NULL;
  200. }
  201. if (sp) {
  202. pvr2_trace(PVR2_TRACE_START_STOP,
  203. "/*---TRACE_READ---*/"
  204. " pvr2_ioread_setup (setup) id=%p",cp);
  205. pvr2_stream_kill(sp);
  206. ret = pvr2_stream_set_buffer_count(sp,BUFFER_COUNT);
  207. if (ret < 0) return ret;
  208. for (idx = 0; idx < BUFFER_COUNT; idx++) {
  209. bp = pvr2_stream_get_buffer(sp,idx);
  210. pvr2_buffer_set_buffer(bp,
  211. cp->buffer_storage[idx],
  212. BUFFER_SIZE);
  213. }
  214. cp->stream = sp;
  215. }
  216. } while (0); mutex_unlock(&cp->mutex);
  217. return 0;
  218. }
  219. int pvr2_ioread_set_enabled(struct pvr2_ioread *cp,int fl)
  220. {
  221. int ret = 0;
  222. if ((!fl) == (!(cp->enabled))) return ret;
  223. mutex_lock(&cp->mutex); do {
  224. if (fl) {
  225. ret = pvr2_ioread_start(cp);
  226. } else {
  227. pvr2_ioread_stop(cp);
  228. }
  229. } while (0); 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---*/"
  243. " pvr2_ioread_read id=%p"
  244. " queue_error=%d",
  245. cp,stat);
  246. pvr2_ioread_stop(cp);
  247. return 0;
  248. }
  249. cp->c_buf = NULL;
  250. cp->c_data_ptr = NULL;
  251. cp->c_data_len = 0;
  252. cp->c_data_offs = 0;
  253. }
  254. // Now get a freshly filled buffer.
  255. cp->c_buf = pvr2_stream_get_ready_buffer(cp->stream);
  256. if (!cp->c_buf) break; // Nothing ready; done.
  257. cp->c_data_len = pvr2_buffer_get_count(cp->c_buf);
  258. if (!cp->c_data_len) {
  259. // Nothing transferred. Was there an error?
  260. stat = pvr2_buffer_get_status(cp->c_buf);
  261. if (stat < 0) {
  262. // Streaming error...
  263. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  264. "/*---TRACE_READ---*/"
  265. " pvr2_ioread_read id=%p"
  266. " buffer_error=%d",
  267. cp,stat);
  268. pvr2_ioread_stop(cp);
  269. // Give up.
  270. return 0;
  271. }
  272. // Start over...
  273. continue;
  274. }
  275. cp->c_data_offs = 0;
  276. cp->c_data_ptr = cp->buffer_storage[
  277. pvr2_buffer_get_id(cp->c_buf)];
  278. }
  279. return !0;
  280. }
  281. static void pvr2_ioread_filter(struct pvr2_ioread *cp)
  282. {
  283. unsigned int idx;
  284. if (!cp->enabled) return;
  285. if (cp->sync_state != 1) return;
  286. // Search the stream for our synchronization key. This is made
  287. // complicated by the fact that in order to be honest with
  288. // ourselves here we must search across buffer boundaries...
  289. mutex_lock(&cp->mutex); while (1) {
  290. // Ensure we have a buffer
  291. if (!pvr2_ioread_get_buffer(cp)) break;
  292. if (!cp->c_data_len) break;
  293. // Now walk the buffer contents until we match the key or
  294. // run out of buffer data.
  295. for (idx = cp->c_data_offs; idx < cp->c_data_len; idx++) {
  296. if (cp->sync_buf_offs >= cp->sync_key_len) break;
  297. if (cp->c_data_ptr[idx] ==
  298. cp->sync_key_ptr[cp->sync_buf_offs]) {
  299. // Found the next key byte
  300. (cp->sync_buf_offs)++;
  301. } else {
  302. // Whoops, mismatched. Start key over...
  303. cp->sync_buf_offs = 0;
  304. }
  305. }
  306. // Consume what we've walked through
  307. cp->c_data_offs += idx;
  308. cp->sync_trashed_count += idx;
  309. // If we've found the key, then update state and get out.
  310. if (cp->sync_buf_offs >= cp->sync_key_len) {
  311. cp->sync_trashed_count -= cp->sync_key_len;
  312. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  313. "/*---TRACE_READ---*/"
  314. " sync_state <== 2 (skipped %u bytes)",
  315. cp->sync_trashed_count);
  316. cp->sync_state = 2;
  317. cp->sync_buf_offs = 0;
  318. break;
  319. }
  320. if (cp->c_data_offs < cp->c_data_len) {
  321. // Sanity check - should NEVER get here
  322. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  323. "ERROR: pvr2_ioread filter sync problem"
  324. " len=%u offs=%u",
  325. cp->c_data_len,cp->c_data_offs);
  326. // Get out so we don't get stuck in an infinite
  327. // loop.
  328. break;
  329. }
  330. continue; // (for clarity)
  331. } mutex_unlock(&cp->mutex);
  332. }
  333. int pvr2_ioread_avail(struct pvr2_ioread *cp)
  334. {
  335. int ret;
  336. if (!(cp->enabled)) {
  337. // Stream is not enabled; so this is an I/O error
  338. return -EIO;
  339. }
  340. if (cp->sync_state == 1) {
  341. pvr2_ioread_filter(cp);
  342. if (cp->sync_state == 1) return -EAGAIN;
  343. }
  344. ret = 0;
  345. if (cp->stream_running) {
  346. if (!pvr2_stream_get_ready_count(cp->stream)) {
  347. // No data available at all right now.
  348. ret = -EAGAIN;
  349. }
  350. } else {
  351. if (pvr2_stream_get_ready_count(cp->stream) < BUFFER_COUNT/2) {
  352. // Haven't buffered up enough yet; try again later
  353. ret = -EAGAIN;
  354. }
  355. }
  356. if ((!(cp->spigot_open)) != (!(ret == 0))) {
  357. cp->spigot_open = (ret == 0);
  358. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  359. "/*---TRACE_READ---*/ data is %s",
  360. cp->spigot_open ? "available" : "pending");
  361. }
  362. return ret;
  363. }
  364. int pvr2_ioread_read(struct pvr2_ioread *cp,void __user *buf,unsigned int cnt)
  365. {
  366. unsigned int copied_cnt;
  367. unsigned int bcnt;
  368. const char *src;
  369. int stat;
  370. int ret = 0;
  371. unsigned int req_cnt = cnt;
  372. if (!cnt) {
  373. pvr2_trace(PVR2_TRACE_TRAP,
  374. "/*---TRACE_READ---*/ pvr2_ioread_read id=%p"
  375. " ZERO Request? Returning zero.",cp);
  376. return 0;
  377. }
  378. stat = pvr2_ioread_avail(cp);
  379. if (stat < 0) return stat;
  380. cp->stream_running = !0;
  381. mutex_lock(&cp->mutex); do {
  382. // Suck data out of the buffers and copy to the user
  383. copied_cnt = 0;
  384. if (!buf) cnt = 0;
  385. while (1) {
  386. if (!pvr2_ioread_get_buffer(cp)) {
  387. ret = -EIO;
  388. break;
  389. }
  390. if (!cnt) break;
  391. if (cp->sync_state == 2) {
  392. // We're repeating the sync key data into
  393. // the stream.
  394. src = cp->sync_key_ptr + cp->sync_buf_offs;
  395. bcnt = cp->sync_key_len - cp->sync_buf_offs;
  396. } else {
  397. // Normal buffer copy
  398. src = cp->c_data_ptr + cp->c_data_offs;
  399. bcnt = cp->c_data_len - cp->c_data_offs;
  400. }
  401. if (!bcnt) break;
  402. // Don't run past user's buffer
  403. if (bcnt > cnt) bcnt = cnt;
  404. if (copy_to_user(buf,src,bcnt)) {
  405. // User supplied a bad pointer?
  406. // Give up - this *will* cause data
  407. // to be lost.
  408. ret = -EFAULT;
  409. break;
  410. }
  411. cnt -= bcnt;
  412. buf += bcnt;
  413. copied_cnt += bcnt;
  414. if (cp->sync_state == 2) {
  415. // Update offset inside sync key that we're
  416. // repeating back out.
  417. cp->sync_buf_offs += bcnt;
  418. if (cp->sync_buf_offs >= cp->sync_key_len) {
  419. // Consumed entire key; switch mode
  420. // to normal.
  421. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  422. "/*---TRACE_READ---*/"
  423. " sync_state <== 0");
  424. cp->sync_state = 0;
  425. }
  426. } else {
  427. // Update buffer offset.
  428. cp->c_data_offs += bcnt;
  429. }
  430. }
  431. } while (0); mutex_unlock(&cp->mutex);
  432. if (!ret) {
  433. if (copied_cnt) {
  434. // If anything was copied, return that count
  435. ret = copied_cnt;
  436. } else {
  437. // Nothing copied; suggest to caller that another
  438. // attempt should be tried again later
  439. ret = -EAGAIN;
  440. }
  441. }
  442. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  443. "/*---TRACE_READ---*/ pvr2_ioread_read"
  444. " id=%p request=%d result=%d",
  445. cp,req_cnt,ret);
  446. return ret;
  447. }
  448. /*
  449. Stuff for Emacs to see, in order to encourage consistent editing style:
  450. *** Local Variables: ***
  451. *** mode: c ***
  452. *** fill-column: 75 ***
  453. *** tab-width: 8 ***
  454. *** c-basic-offset: 8 ***
  455. *** End: ***
  456. */