pvrusb2-ioread.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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);
  189. 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) {
  208. mutex_unlock(&cp->mutex);
  209. return ret;
  210. }
  211. for (idx = 0; idx < BUFFER_COUNT; idx++) {
  212. bp = pvr2_stream_get_buffer(sp,idx);
  213. pvr2_buffer_set_buffer(bp,
  214. cp->buffer_storage[idx],
  215. BUFFER_SIZE);
  216. }
  217. cp->stream = sp;
  218. }
  219. } while (0);
  220. mutex_unlock(&cp->mutex);
  221. return 0;
  222. }
  223. int pvr2_ioread_set_enabled(struct pvr2_ioread *cp,int fl)
  224. {
  225. int ret = 0;
  226. if ((!fl) == (!(cp->enabled))) return ret;
  227. mutex_lock(&cp->mutex);
  228. do {
  229. if (fl) {
  230. ret = pvr2_ioread_start(cp);
  231. } else {
  232. pvr2_ioread_stop(cp);
  233. }
  234. } while (0);
  235. mutex_unlock(&cp->mutex);
  236. return ret;
  237. }
  238. static int pvr2_ioread_get_buffer(struct pvr2_ioread *cp)
  239. {
  240. int stat;
  241. while (cp->c_data_len <= cp->c_data_offs) {
  242. if (cp->c_buf) {
  243. // Flush out current buffer first.
  244. stat = pvr2_buffer_queue(cp->c_buf);
  245. if (stat < 0) {
  246. // Streaming error...
  247. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  248. "/*---TRACE_READ---*/"
  249. " pvr2_ioread_read id=%p"
  250. " queue_error=%d",
  251. cp,stat);
  252. pvr2_ioread_stop(cp);
  253. return 0;
  254. }
  255. cp->c_buf = NULL;
  256. cp->c_data_ptr = NULL;
  257. cp->c_data_len = 0;
  258. cp->c_data_offs = 0;
  259. }
  260. // Now get a freshly filled buffer.
  261. cp->c_buf = pvr2_stream_get_ready_buffer(cp->stream);
  262. if (!cp->c_buf) break; // Nothing ready; done.
  263. cp->c_data_len = pvr2_buffer_get_count(cp->c_buf);
  264. if (!cp->c_data_len) {
  265. // Nothing transferred. Was there an error?
  266. stat = pvr2_buffer_get_status(cp->c_buf);
  267. if (stat < 0) {
  268. // Streaming error...
  269. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  270. "/*---TRACE_READ---*/"
  271. " pvr2_ioread_read id=%p"
  272. " buffer_error=%d",
  273. cp,stat);
  274. pvr2_ioread_stop(cp);
  275. // Give up.
  276. return 0;
  277. }
  278. // Start over...
  279. continue;
  280. }
  281. cp->c_data_offs = 0;
  282. cp->c_data_ptr = cp->buffer_storage[
  283. pvr2_buffer_get_id(cp->c_buf)];
  284. }
  285. return !0;
  286. }
  287. static void pvr2_ioread_filter(struct pvr2_ioread *cp)
  288. {
  289. unsigned int idx;
  290. if (!cp->enabled) return;
  291. if (cp->sync_state != 1) return;
  292. // Search the stream for our synchronization key. This is made
  293. // complicated by the fact that in order to be honest with
  294. // ourselves here we must search across buffer boundaries...
  295. mutex_lock(&cp->mutex);
  296. while (1) {
  297. // Ensure we have a buffer
  298. if (!pvr2_ioread_get_buffer(cp)) break;
  299. if (!cp->c_data_len) break;
  300. // Now walk the buffer contents until we match the key or
  301. // run out of buffer data.
  302. for (idx = cp->c_data_offs; idx < cp->c_data_len; idx++) {
  303. if (cp->sync_buf_offs >= cp->sync_key_len) break;
  304. if (cp->c_data_ptr[idx] ==
  305. cp->sync_key_ptr[cp->sync_buf_offs]) {
  306. // Found the next key byte
  307. (cp->sync_buf_offs)++;
  308. } else {
  309. // Whoops, mismatched. Start key over...
  310. cp->sync_buf_offs = 0;
  311. }
  312. }
  313. // Consume what we've walked through
  314. cp->c_data_offs += idx;
  315. cp->sync_trashed_count += idx;
  316. // If we've found the key, then update state and get out.
  317. if (cp->sync_buf_offs >= cp->sync_key_len) {
  318. cp->sync_trashed_count -= cp->sync_key_len;
  319. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  320. "/*---TRACE_READ---*/"
  321. " sync_state <== 2 (skipped %u bytes)",
  322. cp->sync_trashed_count);
  323. cp->sync_state = 2;
  324. cp->sync_buf_offs = 0;
  325. break;
  326. }
  327. if (cp->c_data_offs < cp->c_data_len) {
  328. // Sanity check - should NEVER get here
  329. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  330. "ERROR: pvr2_ioread filter sync problem"
  331. " len=%u offs=%u",
  332. cp->c_data_len,cp->c_data_offs);
  333. // Get out so we don't get stuck in an infinite
  334. // loop.
  335. break;
  336. }
  337. continue; // (for clarity)
  338. }
  339. mutex_unlock(&cp->mutex);
  340. }
  341. int pvr2_ioread_avail(struct pvr2_ioread *cp)
  342. {
  343. int ret;
  344. if (!(cp->enabled)) {
  345. // Stream is not enabled; so this is an I/O error
  346. return -EIO;
  347. }
  348. if (cp->sync_state == 1) {
  349. pvr2_ioread_filter(cp);
  350. if (cp->sync_state == 1) return -EAGAIN;
  351. }
  352. ret = 0;
  353. if (cp->stream_running) {
  354. if (!pvr2_stream_get_ready_count(cp->stream)) {
  355. // No data available at all right now.
  356. ret = -EAGAIN;
  357. }
  358. } else {
  359. if (pvr2_stream_get_ready_count(cp->stream) < BUFFER_COUNT/2) {
  360. // Haven't buffered up enough yet; try again later
  361. ret = -EAGAIN;
  362. }
  363. }
  364. if ((!(cp->spigot_open)) != (!(ret == 0))) {
  365. cp->spigot_open = (ret == 0);
  366. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  367. "/*---TRACE_READ---*/ data is %s",
  368. cp->spigot_open ? "available" : "pending");
  369. }
  370. return ret;
  371. }
  372. int pvr2_ioread_read(struct pvr2_ioread *cp,void __user *buf,unsigned int cnt)
  373. {
  374. unsigned int copied_cnt;
  375. unsigned int bcnt;
  376. const char *src;
  377. int stat;
  378. int ret = 0;
  379. unsigned int req_cnt = cnt;
  380. if (!cnt) {
  381. pvr2_trace(PVR2_TRACE_TRAP,
  382. "/*---TRACE_READ---*/ pvr2_ioread_read id=%p"
  383. " ZERO Request? Returning zero.",cp);
  384. return 0;
  385. }
  386. stat = pvr2_ioread_avail(cp);
  387. if (stat < 0) return stat;
  388. cp->stream_running = !0;
  389. mutex_lock(&cp->mutex);
  390. do {
  391. // Suck data out of the buffers and copy to the user
  392. copied_cnt = 0;
  393. if (!buf) cnt = 0;
  394. while (1) {
  395. if (!pvr2_ioread_get_buffer(cp)) {
  396. ret = -EIO;
  397. break;
  398. }
  399. if (!cnt) break;
  400. if (cp->sync_state == 2) {
  401. // We're repeating the sync key data into
  402. // the stream.
  403. src = cp->sync_key_ptr + cp->sync_buf_offs;
  404. bcnt = cp->sync_key_len - cp->sync_buf_offs;
  405. } else {
  406. // Normal buffer copy
  407. src = cp->c_data_ptr + cp->c_data_offs;
  408. bcnt = cp->c_data_len - cp->c_data_offs;
  409. }
  410. if (!bcnt) break;
  411. // Don't run past user's buffer
  412. if (bcnt > cnt) bcnt = cnt;
  413. if (copy_to_user(buf,src,bcnt)) {
  414. // User supplied a bad pointer?
  415. // Give up - this *will* cause data
  416. // to be lost.
  417. ret = -EFAULT;
  418. break;
  419. }
  420. cnt -= bcnt;
  421. buf += bcnt;
  422. copied_cnt += bcnt;
  423. if (cp->sync_state == 2) {
  424. // Update offset inside sync key that we're
  425. // repeating back out.
  426. cp->sync_buf_offs += bcnt;
  427. if (cp->sync_buf_offs >= cp->sync_key_len) {
  428. // Consumed entire key; switch mode
  429. // to normal.
  430. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  431. "/*---TRACE_READ---*/"
  432. " sync_state <== 0");
  433. cp->sync_state = 0;
  434. }
  435. } else {
  436. // Update buffer offset.
  437. cp->c_data_offs += bcnt;
  438. }
  439. }
  440. } while (0);
  441. mutex_unlock(&cp->mutex);
  442. if (!ret) {
  443. if (copied_cnt) {
  444. // If anything was copied, return that count
  445. ret = copied_cnt;
  446. } else {
  447. // Nothing copied; suggest to caller that another
  448. // attempt should be tried again later
  449. ret = -EAGAIN;
  450. }
  451. }
  452. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  453. "/*---TRACE_READ---*/ pvr2_ioread_read"
  454. " id=%p request=%d result=%d",
  455. cp,req_cnt,ret);
  456. return ret;
  457. }