tm6000-video.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755
  1. /*
  2. * tm6000-video.c - driver for TM5600/TM6000/TM6010 USB video capture devices
  3. *
  4. * Copyright (C) 2006-2007 Mauro Carvalho Chehab <mchehab@infradead.org>
  5. *
  6. * Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com>
  7. * - Fixed module load/unload
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation version 2
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/delay.h>
  24. #include <linux/errno.h>
  25. #include <linux/fs.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/mm.h>
  29. #include <linux/ioport.h>
  30. #include <linux/init.h>
  31. #include <linux/sched.h>
  32. #include <linux/random.h>
  33. #include <linux/usb.h>
  34. #include <linux/videodev2.h>
  35. #include <media/v4l2-ioctl.h>
  36. #include <media/v4l2-event.h>
  37. #include <media/tuner.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/kthread.h>
  40. #include <linux/highmem.h>
  41. #include <linux/freezer.h>
  42. #include "tm6000-regs.h"
  43. #include "tm6000.h"
  44. #define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */
  45. /* Limits minimum and default number of buffers */
  46. #define TM6000_MIN_BUF 4
  47. #define TM6000_DEF_BUF 8
  48. #define TM6000_NUM_URB_BUF 8
  49. #define TM6000_MAX_ISO_PACKETS 46 /* Max number of ISO packets */
  50. /* Declare static vars that will be used as parameters */
  51. static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
  52. static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
  53. static int radio_nr = -1; /* /dev/radioN, -1 for autodetect */
  54. static bool keep_urb; /* keep urb buffers allocated */
  55. /* Debug level */
  56. int tm6000_debug;
  57. EXPORT_SYMBOL_GPL(tm6000_debug);
  58. static struct tm6000_fmt format[] = {
  59. {
  60. .name = "4:2:2, packed, YVY2",
  61. .fourcc = V4L2_PIX_FMT_YUYV,
  62. .depth = 16,
  63. }, {
  64. .name = "4:2:2, packed, UYVY",
  65. .fourcc = V4L2_PIX_FMT_UYVY,
  66. .depth = 16,
  67. }, {
  68. .name = "A/V + VBI mux packet",
  69. .fourcc = V4L2_PIX_FMT_TM6000,
  70. .depth = 16,
  71. }
  72. };
  73. /* ------------------------------------------------------------------
  74. * DMA and thread functions
  75. * ------------------------------------------------------------------
  76. */
  77. #define norm_maxw(a) 720
  78. #define norm_maxh(a) 576
  79. #define norm_minw(a) norm_maxw(a)
  80. #define norm_minh(a) norm_maxh(a)
  81. /*
  82. * video-buf generic routine to get the next available buffer
  83. */
  84. static inline void get_next_buf(struct tm6000_dmaqueue *dma_q,
  85. struct tm6000_buffer **buf)
  86. {
  87. struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
  88. if (list_empty(&dma_q->active)) {
  89. dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
  90. *buf = NULL;
  91. return;
  92. }
  93. *buf = list_entry(dma_q->active.next,
  94. struct tm6000_buffer, vb.queue);
  95. }
  96. /*
  97. * Announces that a buffer were filled and request the next
  98. */
  99. static inline void buffer_filled(struct tm6000_core *dev,
  100. struct tm6000_dmaqueue *dma_q,
  101. struct tm6000_buffer *buf)
  102. {
  103. /* Advice that buffer was filled */
  104. dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
  105. buf->vb.state = VIDEOBUF_DONE;
  106. buf->vb.field_count++;
  107. v4l2_get_timestamp(&buf->vb.ts);
  108. list_del(&buf->vb.queue);
  109. wake_up(&buf->vb.done);
  110. }
  111. /*
  112. * Identify the tm5600/6000 buffer header type and properly handles
  113. */
  114. static int copy_streams(u8 *data, unsigned long len,
  115. struct urb *urb)
  116. {
  117. struct tm6000_dmaqueue *dma_q = urb->context;
  118. struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
  119. u8 *ptr = data, *endp = data+len;
  120. unsigned long header = 0;
  121. int rc = 0;
  122. unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
  123. struct tm6000_buffer *vbuf = NULL;
  124. char *voutp = NULL;
  125. unsigned int linewidth;
  126. if (!dev->radio) {
  127. /* get video buffer */
  128. get_next_buf(dma_q, &vbuf);
  129. if (!vbuf)
  130. return rc;
  131. voutp = videobuf_to_vmalloc(&vbuf->vb);
  132. if (!voutp)
  133. return 0;
  134. }
  135. for (ptr = data; ptr < endp;) {
  136. if (!dev->isoc_ctl.cmd) {
  137. /* Header */
  138. if (dev->isoc_ctl.tmp_buf_len > 0) {
  139. /* from last urb or packet */
  140. header = dev->isoc_ctl.tmp_buf;
  141. if (4 - dev->isoc_ctl.tmp_buf_len > 0) {
  142. memcpy((u8 *)&header +
  143. dev->isoc_ctl.tmp_buf_len,
  144. ptr,
  145. 4 - dev->isoc_ctl.tmp_buf_len);
  146. ptr += 4 - dev->isoc_ctl.tmp_buf_len;
  147. }
  148. dev->isoc_ctl.tmp_buf_len = 0;
  149. } else {
  150. if (ptr + 3 >= endp) {
  151. /* have incomplete header */
  152. dev->isoc_ctl.tmp_buf_len = endp - ptr;
  153. memcpy(&dev->isoc_ctl.tmp_buf, ptr,
  154. dev->isoc_ctl.tmp_buf_len);
  155. return rc;
  156. }
  157. /* Seek for sync */
  158. for (; ptr < endp - 3; ptr++) {
  159. if (*(ptr + 3) == 0x47)
  160. break;
  161. }
  162. /* Get message header */
  163. header = *(unsigned long *)ptr;
  164. ptr += 4;
  165. }
  166. /* split the header fields */
  167. size = ((header & 0x7e) << 1);
  168. if (size > 0)
  169. size -= 4;
  170. block = (header >> 7) & 0xf;
  171. field = (header >> 11) & 0x1;
  172. line = (header >> 12) & 0x1ff;
  173. cmd = (header >> 21) & 0x7;
  174. /* Validates haeder fields */
  175. if (size > TM6000_URB_MSG_LEN)
  176. size = TM6000_URB_MSG_LEN;
  177. pktsize = TM6000_URB_MSG_LEN;
  178. /*
  179. * calculate position in buffer and change the buffer
  180. */
  181. switch (cmd) {
  182. case TM6000_URB_MSG_VIDEO:
  183. if (!dev->radio) {
  184. if ((dev->isoc_ctl.vfield != field) &&
  185. (field == 1)) {
  186. /*
  187. * Announces that a new buffer
  188. * were filled
  189. */
  190. buffer_filled(dev, dma_q, vbuf);
  191. dprintk(dev, V4L2_DEBUG_ISOC,
  192. "new buffer filled\n");
  193. get_next_buf(dma_q, &vbuf);
  194. if (!vbuf)
  195. return rc;
  196. voutp = videobuf_to_vmalloc(&vbuf->vb);
  197. if (!voutp)
  198. return rc;
  199. memset(voutp, 0, vbuf->vb.size);
  200. }
  201. linewidth = vbuf->vb.width << 1;
  202. pos = ((line << 1) - field - 1) *
  203. linewidth + block * TM6000_URB_MSG_LEN;
  204. /* Don't allow to write out of the buffer */
  205. if (pos + size > vbuf->vb.size)
  206. cmd = TM6000_URB_MSG_ERR;
  207. dev->isoc_ctl.vfield = field;
  208. }
  209. break;
  210. case TM6000_URB_MSG_VBI:
  211. break;
  212. case TM6000_URB_MSG_AUDIO:
  213. case TM6000_URB_MSG_PTS:
  214. size = pktsize; /* Size is always 180 bytes */
  215. break;
  216. }
  217. } else {
  218. /* Continue the last copy */
  219. cmd = dev->isoc_ctl.cmd;
  220. size = dev->isoc_ctl.size;
  221. pos = dev->isoc_ctl.pos;
  222. pktsize = dev->isoc_ctl.pktsize;
  223. field = dev->isoc_ctl.field;
  224. }
  225. cpysize = (endp - ptr > size) ? size : endp - ptr;
  226. if (cpysize) {
  227. /* copy data in different buffers */
  228. switch (cmd) {
  229. case TM6000_URB_MSG_VIDEO:
  230. /* Fills video buffer */
  231. if (vbuf)
  232. memcpy(&voutp[pos], ptr, cpysize);
  233. break;
  234. case TM6000_URB_MSG_AUDIO: {
  235. int i;
  236. for (i = 0; i < cpysize; i += 2)
  237. swab16s((u16 *)(ptr + i));
  238. tm6000_call_fillbuf(dev, TM6000_AUDIO, ptr, cpysize);
  239. break;
  240. }
  241. case TM6000_URB_MSG_VBI:
  242. /* Need some code to copy vbi buffer */
  243. break;
  244. case TM6000_URB_MSG_PTS: {
  245. /* Need some code to copy pts */
  246. u32 pts;
  247. pts = *(u32 *)ptr;
  248. dprintk(dev, V4L2_DEBUG_ISOC, "field %d, PTS %x",
  249. field, pts);
  250. break;
  251. }
  252. }
  253. }
  254. if (ptr + pktsize > endp) {
  255. /*
  256. * End of URB packet, but cmd processing is not
  257. * complete. Preserve the state for a next packet
  258. */
  259. dev->isoc_ctl.pos = pos + cpysize;
  260. dev->isoc_ctl.size = size - cpysize;
  261. dev->isoc_ctl.cmd = cmd;
  262. dev->isoc_ctl.field = field;
  263. dev->isoc_ctl.pktsize = pktsize - (endp - ptr);
  264. ptr += endp - ptr;
  265. } else {
  266. dev->isoc_ctl.cmd = 0;
  267. ptr += pktsize;
  268. }
  269. }
  270. return 0;
  271. }
  272. /*
  273. * Identify the tm5600/6000 buffer header type and properly handles
  274. */
  275. static int copy_multiplexed(u8 *ptr, unsigned long len,
  276. struct urb *urb)
  277. {
  278. struct tm6000_dmaqueue *dma_q = urb->context;
  279. struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
  280. unsigned int pos = dev->isoc_ctl.pos, cpysize;
  281. int rc = 1;
  282. struct tm6000_buffer *buf;
  283. char *outp = NULL;
  284. get_next_buf(dma_q, &buf);
  285. if (buf)
  286. outp = videobuf_to_vmalloc(&buf->vb);
  287. if (!outp)
  288. return 0;
  289. while (len > 0) {
  290. cpysize = min(len, buf->vb.size-pos);
  291. memcpy(&outp[pos], ptr, cpysize);
  292. pos += cpysize;
  293. ptr += cpysize;
  294. len -= cpysize;
  295. if (pos >= buf->vb.size) {
  296. pos = 0;
  297. /* Announces that a new buffer were filled */
  298. buffer_filled(dev, dma_q, buf);
  299. dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
  300. get_next_buf(dma_q, &buf);
  301. if (!buf)
  302. break;
  303. outp = videobuf_to_vmalloc(&(buf->vb));
  304. if (!outp)
  305. return rc;
  306. pos = 0;
  307. }
  308. }
  309. dev->isoc_ctl.pos = pos;
  310. return rc;
  311. }
  312. static inline void print_err_status(struct tm6000_core *dev,
  313. int packet, int status)
  314. {
  315. char *errmsg = "Unknown";
  316. switch (status) {
  317. case -ENOENT:
  318. errmsg = "unlinked synchronuously";
  319. break;
  320. case -ECONNRESET:
  321. errmsg = "unlinked asynchronuously";
  322. break;
  323. case -ENOSR:
  324. errmsg = "Buffer error (overrun)";
  325. break;
  326. case -EPIPE:
  327. errmsg = "Stalled (device not responding)";
  328. break;
  329. case -EOVERFLOW:
  330. errmsg = "Babble (bad cable?)";
  331. break;
  332. case -EPROTO:
  333. errmsg = "Bit-stuff error (bad cable?)";
  334. break;
  335. case -EILSEQ:
  336. errmsg = "CRC/Timeout (could be anything)";
  337. break;
  338. case -ETIME:
  339. errmsg = "Device does not respond";
  340. break;
  341. }
  342. if (packet < 0) {
  343. dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
  344. status, errmsg);
  345. } else {
  346. dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
  347. packet, status, errmsg);
  348. }
  349. }
  350. /*
  351. * Controls the isoc copy of each urb packet
  352. */
  353. static inline int tm6000_isoc_copy(struct urb *urb)
  354. {
  355. struct tm6000_dmaqueue *dma_q = urb->context;
  356. struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
  357. int i, len = 0, rc = 1, status;
  358. char *p;
  359. if (urb->status < 0) {
  360. print_err_status(dev, -1, urb->status);
  361. return 0;
  362. }
  363. for (i = 0; i < urb->number_of_packets; i++) {
  364. status = urb->iso_frame_desc[i].status;
  365. if (status < 0) {
  366. print_err_status(dev, i, status);
  367. continue;
  368. }
  369. len = urb->iso_frame_desc[i].actual_length;
  370. if (len > 0) {
  371. p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  372. if (!urb->iso_frame_desc[i].status) {
  373. if ((dev->fourcc) == V4L2_PIX_FMT_TM6000) {
  374. rc = copy_multiplexed(p, len, urb);
  375. if (rc <= 0)
  376. return rc;
  377. } else {
  378. copy_streams(p, len, urb);
  379. }
  380. }
  381. }
  382. }
  383. return rc;
  384. }
  385. /* ------------------------------------------------------------------
  386. * URB control
  387. * ------------------------------------------------------------------
  388. */
  389. /*
  390. * IRQ callback, called by URB callback
  391. */
  392. static void tm6000_irq_callback(struct urb *urb)
  393. {
  394. struct tm6000_dmaqueue *dma_q = urb->context;
  395. struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
  396. int i;
  397. switch (urb->status) {
  398. case 0:
  399. case -ETIMEDOUT:
  400. break;
  401. case -ECONNRESET:
  402. case -ENOENT:
  403. case -ESHUTDOWN:
  404. return;
  405. default:
  406. tm6000_err("urb completion error %d.\n", urb->status);
  407. break;
  408. }
  409. spin_lock(&dev->slock);
  410. tm6000_isoc_copy(urb);
  411. spin_unlock(&dev->slock);
  412. /* Reset urb buffers */
  413. for (i = 0; i < urb->number_of_packets; i++) {
  414. urb->iso_frame_desc[i].status = 0;
  415. urb->iso_frame_desc[i].actual_length = 0;
  416. }
  417. urb->status = usb_submit_urb(urb, GFP_ATOMIC);
  418. if (urb->status)
  419. tm6000_err("urb resubmit failed (error=%i)\n",
  420. urb->status);
  421. }
  422. /*
  423. * Allocate URB buffers
  424. */
  425. static int tm6000_alloc_urb_buffers(struct tm6000_core *dev)
  426. {
  427. int num_bufs = TM6000_NUM_URB_BUF;
  428. int i;
  429. if (dev->urb_buffer != NULL)
  430. return 0;
  431. dev->urb_buffer = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
  432. if (!dev->urb_buffer) {
  433. tm6000_err("cannot allocate memory for urb buffers\n");
  434. return -ENOMEM;
  435. }
  436. dev->urb_dma = kmalloc(sizeof(dma_addr_t *)*num_bufs, GFP_KERNEL);
  437. if (!dev->urb_dma) {
  438. tm6000_err("cannot allocate memory for urb dma pointers\n");
  439. return -ENOMEM;
  440. }
  441. for (i = 0; i < num_bufs; i++) {
  442. dev->urb_buffer[i] = usb_alloc_coherent(
  443. dev->udev, dev->urb_size,
  444. GFP_KERNEL, &dev->urb_dma[i]);
  445. if (!dev->urb_buffer[i]) {
  446. tm6000_err("unable to allocate %i bytes for transfer buffer %i\n",
  447. dev->urb_size, i);
  448. return -ENOMEM;
  449. }
  450. memset(dev->urb_buffer[i], 0, dev->urb_size);
  451. }
  452. return 0;
  453. }
  454. /*
  455. * Free URB buffers
  456. */
  457. static int tm6000_free_urb_buffers(struct tm6000_core *dev)
  458. {
  459. int i;
  460. if (dev->urb_buffer == NULL)
  461. return 0;
  462. for (i = 0; i < TM6000_NUM_URB_BUF; i++) {
  463. if (dev->urb_buffer[i]) {
  464. usb_free_coherent(dev->udev,
  465. dev->urb_size,
  466. dev->urb_buffer[i],
  467. dev->urb_dma[i]);
  468. dev->urb_buffer[i] = NULL;
  469. }
  470. }
  471. kfree(dev->urb_buffer);
  472. kfree(dev->urb_dma);
  473. dev->urb_buffer = NULL;
  474. dev->urb_dma = NULL;
  475. return 0;
  476. }
  477. /*
  478. * Stop and Deallocate URBs
  479. */
  480. static void tm6000_uninit_isoc(struct tm6000_core *dev)
  481. {
  482. struct urb *urb;
  483. int i;
  484. dev->isoc_ctl.buf = NULL;
  485. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  486. urb = dev->isoc_ctl.urb[i];
  487. if (urb) {
  488. usb_kill_urb(urb);
  489. usb_unlink_urb(urb);
  490. usb_free_urb(urb);
  491. dev->isoc_ctl.urb[i] = NULL;
  492. }
  493. dev->isoc_ctl.transfer_buffer[i] = NULL;
  494. }
  495. if (!keep_urb)
  496. tm6000_free_urb_buffers(dev);
  497. kfree(dev->isoc_ctl.urb);
  498. kfree(dev->isoc_ctl.transfer_buffer);
  499. dev->isoc_ctl.urb = NULL;
  500. dev->isoc_ctl.transfer_buffer = NULL;
  501. dev->isoc_ctl.num_bufs = 0;
  502. }
  503. /*
  504. * Assign URBs and start IRQ
  505. */
  506. static int tm6000_prepare_isoc(struct tm6000_core *dev)
  507. {
  508. struct tm6000_dmaqueue *dma_q = &dev->vidq;
  509. int i, j, sb_size, pipe, size, max_packets;
  510. int num_bufs = TM6000_NUM_URB_BUF;
  511. struct urb *urb;
  512. /* De-allocates all pending stuff */
  513. tm6000_uninit_isoc(dev);
  514. /* Stop interrupt USB pipe */
  515. tm6000_ir_int_stop(dev);
  516. usb_set_interface(dev->udev,
  517. dev->isoc_in.bInterfaceNumber,
  518. dev->isoc_in.bAlternateSetting);
  519. /* Start interrupt USB pipe */
  520. tm6000_ir_int_start(dev);
  521. pipe = usb_rcvisocpipe(dev->udev,
  522. dev->isoc_in.endp->desc.bEndpointAddress &
  523. USB_ENDPOINT_NUMBER_MASK);
  524. size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
  525. if (size > dev->isoc_in.maxsize)
  526. size = dev->isoc_in.maxsize;
  527. dev->isoc_ctl.max_pkt_size = size;
  528. max_packets = TM6000_MAX_ISO_PACKETS;
  529. sb_size = max_packets * size;
  530. dev->urb_size = sb_size;
  531. dev->isoc_ctl.num_bufs = num_bufs;
  532. dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
  533. if (!dev->isoc_ctl.urb) {
  534. tm6000_err("cannot alloc memory for usb buffers\n");
  535. return -ENOMEM;
  536. }
  537. dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
  538. GFP_KERNEL);
  539. if (!dev->isoc_ctl.transfer_buffer) {
  540. tm6000_err("cannot allocate memory for usbtransfer\n");
  541. kfree(dev->isoc_ctl.urb);
  542. return -ENOMEM;
  543. }
  544. dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets"
  545. " (%d bytes) of %d bytes each to handle %u size\n",
  546. max_packets, num_bufs, sb_size,
  547. dev->isoc_in.maxsize, size);
  548. if (!dev->urb_buffer && tm6000_alloc_urb_buffers(dev) < 0) {
  549. tm6000_err("cannot allocate memory for urb buffers\n");
  550. /* call free, as some buffers might have been allocated */
  551. tm6000_free_urb_buffers(dev);
  552. kfree(dev->isoc_ctl.urb);
  553. kfree(dev->isoc_ctl.transfer_buffer);
  554. return -ENOMEM;
  555. }
  556. /* allocate urbs and transfer buffers */
  557. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  558. urb = usb_alloc_urb(max_packets, GFP_KERNEL);
  559. if (!urb) {
  560. tm6000_err("cannot alloc isoc_ctl.urb %i\n", i);
  561. tm6000_uninit_isoc(dev);
  562. usb_free_urb(urb);
  563. return -ENOMEM;
  564. }
  565. dev->isoc_ctl.urb[i] = urb;
  566. urb->transfer_dma = dev->urb_dma[i];
  567. dev->isoc_ctl.transfer_buffer[i] = dev->urb_buffer[i];
  568. usb_fill_bulk_urb(urb, dev->udev, pipe,
  569. dev->isoc_ctl.transfer_buffer[i], sb_size,
  570. tm6000_irq_callback, dma_q);
  571. urb->interval = dev->isoc_in.endp->desc.bInterval;
  572. urb->number_of_packets = max_packets;
  573. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  574. for (j = 0; j < max_packets; j++) {
  575. urb->iso_frame_desc[j].offset = size * j;
  576. urb->iso_frame_desc[j].length = size;
  577. }
  578. }
  579. return 0;
  580. }
  581. static int tm6000_start_thread(struct tm6000_core *dev)
  582. {
  583. struct tm6000_dmaqueue *dma_q = &dev->vidq;
  584. int i;
  585. dma_q->frame = 0;
  586. dma_q->ini_jiffies = jiffies;
  587. init_waitqueue_head(&dma_q->wq);
  588. /* submit urbs and enables IRQ */
  589. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  590. int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
  591. if (rc) {
  592. tm6000_err("submit of urb %i failed (error=%i)\n", i,
  593. rc);
  594. tm6000_uninit_isoc(dev);
  595. return rc;
  596. }
  597. }
  598. return 0;
  599. }
  600. /* ------------------------------------------------------------------
  601. * Videobuf operations
  602. * ------------------------------------------------------------------
  603. */
  604. static int
  605. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  606. {
  607. struct tm6000_fh *fh = vq->priv_data;
  608. *size = fh->fmt->depth * fh->width * fh->height >> 3;
  609. if (0 == *count)
  610. *count = TM6000_DEF_BUF;
  611. if (*count < TM6000_MIN_BUF)
  612. *count = TM6000_MIN_BUF;
  613. while (*size * *count > vid_limit * 1024 * 1024)
  614. (*count)--;
  615. return 0;
  616. }
  617. static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
  618. {
  619. struct tm6000_fh *fh = vq->priv_data;
  620. struct tm6000_core *dev = fh->dev;
  621. unsigned long flags;
  622. if (in_interrupt())
  623. BUG();
  624. /* We used to wait for the buffer to finish here, but this didn't work
  625. because, as we were keeping the state as VIDEOBUF_QUEUED,
  626. videobuf_queue_cancel marked it as finished for us.
  627. (Also, it could wedge forever if the hardware was misconfigured.)
  628. This should be safe; by the time we get here, the buffer isn't
  629. queued anymore. If we ever start marking the buffers as
  630. VIDEOBUF_ACTIVE, it won't be, though.
  631. */
  632. spin_lock_irqsave(&dev->slock, flags);
  633. if (dev->isoc_ctl.buf == buf)
  634. dev->isoc_ctl.buf = NULL;
  635. spin_unlock_irqrestore(&dev->slock, flags);
  636. videobuf_vmalloc_free(&buf->vb);
  637. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  638. }
  639. static int
  640. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  641. enum v4l2_field field)
  642. {
  643. struct tm6000_fh *fh = vq->priv_data;
  644. struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
  645. struct tm6000_core *dev = fh->dev;
  646. int rc = 0;
  647. BUG_ON(NULL == fh->fmt);
  648. /* FIXME: It assumes depth=2 */
  649. /* The only currently supported format is 16 bits/pixel */
  650. buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
  651. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  652. return -EINVAL;
  653. if (buf->fmt != fh->fmt ||
  654. buf->vb.width != fh->width ||
  655. buf->vb.height != fh->height ||
  656. buf->vb.field != field) {
  657. buf->fmt = fh->fmt;
  658. buf->vb.width = fh->width;
  659. buf->vb.height = fh->height;
  660. buf->vb.field = field;
  661. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  662. }
  663. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  664. rc = videobuf_iolock(vq, &buf->vb, NULL);
  665. if (rc != 0)
  666. goto fail;
  667. }
  668. if (!dev->isoc_ctl.num_bufs) {
  669. rc = tm6000_prepare_isoc(dev);
  670. if (rc < 0)
  671. goto fail;
  672. rc = tm6000_start_thread(dev);
  673. if (rc < 0)
  674. goto fail;
  675. }
  676. buf->vb.state = VIDEOBUF_PREPARED;
  677. return 0;
  678. fail:
  679. free_buffer(vq, buf);
  680. return rc;
  681. }
  682. static void
  683. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  684. {
  685. struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
  686. struct tm6000_fh *fh = vq->priv_data;
  687. struct tm6000_core *dev = fh->dev;
  688. struct tm6000_dmaqueue *vidq = &dev->vidq;
  689. buf->vb.state = VIDEOBUF_QUEUED;
  690. list_add_tail(&buf->vb.queue, &vidq->active);
  691. }
  692. static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  693. {
  694. struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
  695. free_buffer(vq, buf);
  696. }
  697. static struct videobuf_queue_ops tm6000_video_qops = {
  698. .buf_setup = buffer_setup,
  699. .buf_prepare = buffer_prepare,
  700. .buf_queue = buffer_queue,
  701. .buf_release = buffer_release,
  702. };
  703. /* ------------------------------------------------------------------
  704. * IOCTL handling
  705. * ------------------------------------------------------------------
  706. */
  707. static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
  708. {
  709. /* Is the current fh handling it? if so, that's OK */
  710. if (dev->resources == fh && dev->is_res_read)
  711. return true;
  712. return false;
  713. }
  714. static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
  715. {
  716. /* Is the current fh handling it? if so, that's OK */
  717. if (dev->resources == fh)
  718. return true;
  719. return false;
  720. }
  721. static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
  722. bool is_res_read)
  723. {
  724. /* Is the current fh handling it? if so, that's OK */
  725. if (dev->resources == fh && dev->is_res_read == is_res_read)
  726. return true;
  727. /* is it free? */
  728. if (dev->resources)
  729. return false;
  730. /* grab it */
  731. dev->resources = fh;
  732. dev->is_res_read = is_res_read;
  733. dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
  734. return true;
  735. }
  736. static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
  737. {
  738. /* Is the current fh handling it? if so, that's OK */
  739. if (dev->resources != fh)
  740. return;
  741. dev->resources = NULL;
  742. dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
  743. }
  744. /* ------------------------------------------------------------------
  745. * IOCTL vidioc handling
  746. * ------------------------------------------------------------------
  747. */
  748. static int vidioc_querycap(struct file *file, void *priv,
  749. struct v4l2_capability *cap)
  750. {
  751. struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
  752. struct video_device *vdev = video_devdata(file);
  753. strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
  754. strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
  755. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  756. if (dev->tuner_type != TUNER_ABSENT)
  757. cap->device_caps |= V4L2_CAP_TUNER;
  758. if (vdev->vfl_type == VFL_TYPE_GRABBER)
  759. cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE |
  760. V4L2_CAP_STREAMING |
  761. V4L2_CAP_READWRITE;
  762. else
  763. cap->device_caps |= V4L2_CAP_RADIO;
  764. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
  765. V4L2_CAP_RADIO | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
  766. return 0;
  767. }
  768. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  769. struct v4l2_fmtdesc *f)
  770. {
  771. if (f->index >= ARRAY_SIZE(format))
  772. return -EINVAL;
  773. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  774. f->pixelformat = format[f->index].fourcc;
  775. return 0;
  776. }
  777. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  778. struct v4l2_format *f)
  779. {
  780. struct tm6000_fh *fh = priv;
  781. f->fmt.pix.width = fh->width;
  782. f->fmt.pix.height = fh->height;
  783. f->fmt.pix.field = fh->vb_vidq.field;
  784. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  785. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  786. f->fmt.pix.bytesperline =
  787. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  788. f->fmt.pix.sizeimage =
  789. f->fmt.pix.height * f->fmt.pix.bytesperline;
  790. f->fmt.pix.priv = 0;
  791. return 0;
  792. }
  793. static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
  794. {
  795. unsigned int i;
  796. for (i = 0; i < ARRAY_SIZE(format); i++)
  797. if (format[i].fourcc == fourcc)
  798. return format+i;
  799. return NULL;
  800. }
  801. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  802. struct v4l2_format *f)
  803. {
  804. struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
  805. struct tm6000_fmt *fmt;
  806. enum v4l2_field field;
  807. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  808. if (NULL == fmt) {
  809. dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)"
  810. " invalid.\n", f->fmt.pix.pixelformat);
  811. return -EINVAL;
  812. }
  813. field = f->fmt.pix.field;
  814. field = V4L2_FIELD_INTERLACED;
  815. tm6000_get_std_res(dev);
  816. f->fmt.pix.width = dev->width;
  817. f->fmt.pix.height = dev->height;
  818. f->fmt.pix.width &= ~0x01;
  819. f->fmt.pix.field = field;
  820. f->fmt.pix.priv = 0;
  821. f->fmt.pix.bytesperline =
  822. (f->fmt.pix.width * fmt->depth) >> 3;
  823. f->fmt.pix.sizeimage =
  824. f->fmt.pix.height * f->fmt.pix.bytesperline;
  825. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  826. return 0;
  827. }
  828. /*FIXME: This seems to be generic enough to be at videodev2 */
  829. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  830. struct v4l2_format *f)
  831. {
  832. struct tm6000_fh *fh = priv;
  833. struct tm6000_core *dev = fh->dev;
  834. int ret = vidioc_try_fmt_vid_cap(file, fh, f);
  835. if (ret < 0)
  836. return ret;
  837. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  838. fh->width = f->fmt.pix.width;
  839. fh->height = f->fmt.pix.height;
  840. fh->vb_vidq.field = f->fmt.pix.field;
  841. fh->type = f->type;
  842. dev->fourcc = f->fmt.pix.pixelformat;
  843. tm6000_set_fourcc_format(dev);
  844. return 0;
  845. }
  846. static int vidioc_reqbufs(struct file *file, void *priv,
  847. struct v4l2_requestbuffers *p)
  848. {
  849. struct tm6000_fh *fh = priv;
  850. return videobuf_reqbufs(&fh->vb_vidq, p);
  851. }
  852. static int vidioc_querybuf(struct file *file, void *priv,
  853. struct v4l2_buffer *p)
  854. {
  855. struct tm6000_fh *fh = priv;
  856. return videobuf_querybuf(&fh->vb_vidq, p);
  857. }
  858. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  859. {
  860. struct tm6000_fh *fh = priv;
  861. return videobuf_qbuf(&fh->vb_vidq, p);
  862. }
  863. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  864. {
  865. struct tm6000_fh *fh = priv;
  866. return videobuf_dqbuf(&fh->vb_vidq, p,
  867. file->f_flags & O_NONBLOCK);
  868. }
  869. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  870. {
  871. struct tm6000_fh *fh = priv;
  872. struct tm6000_core *dev = fh->dev;
  873. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  874. return -EINVAL;
  875. if (i != fh->type)
  876. return -EINVAL;
  877. if (!res_get(dev, fh, false))
  878. return -EBUSY;
  879. return videobuf_streamon(&fh->vb_vidq);
  880. }
  881. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  882. {
  883. struct tm6000_fh *fh = priv;
  884. struct tm6000_core *dev = fh->dev;
  885. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  886. return -EINVAL;
  887. if (i != fh->type)
  888. return -EINVAL;
  889. videobuf_streamoff(&fh->vb_vidq);
  890. res_free(dev, fh);
  891. return 0;
  892. }
  893. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  894. {
  895. int rc = 0;
  896. struct tm6000_fh *fh = priv;
  897. struct tm6000_core *dev = fh->dev;
  898. dev->norm = *norm;
  899. rc = tm6000_init_analog_mode(dev);
  900. fh->width = dev->width;
  901. fh->height = dev->height;
  902. if (rc < 0)
  903. return rc;
  904. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  905. return 0;
  906. }
  907. static const char *iname[] = {
  908. [TM6000_INPUT_TV] = "Television",
  909. [TM6000_INPUT_COMPOSITE1] = "Composite 1",
  910. [TM6000_INPUT_COMPOSITE2] = "Composite 2",
  911. [TM6000_INPUT_SVIDEO] = "S-Video",
  912. };
  913. static int vidioc_enum_input(struct file *file, void *priv,
  914. struct v4l2_input *i)
  915. {
  916. struct tm6000_fh *fh = priv;
  917. struct tm6000_core *dev = fh->dev;
  918. unsigned int n;
  919. n = i->index;
  920. if (n >= 3)
  921. return -EINVAL;
  922. if (!dev->vinput[n].type)
  923. return -EINVAL;
  924. i->index = n;
  925. if (dev->vinput[n].type == TM6000_INPUT_TV)
  926. i->type = V4L2_INPUT_TYPE_TUNER;
  927. else
  928. i->type = V4L2_INPUT_TYPE_CAMERA;
  929. strcpy(i->name, iname[dev->vinput[n].type]);
  930. i->std = TM6000_STD;
  931. return 0;
  932. }
  933. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  934. {
  935. struct tm6000_fh *fh = priv;
  936. struct tm6000_core *dev = fh->dev;
  937. *i = dev->input;
  938. return 0;
  939. }
  940. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  941. {
  942. struct tm6000_fh *fh = priv;
  943. struct tm6000_core *dev = fh->dev;
  944. int rc = 0;
  945. if (i >= 3)
  946. return -EINVAL;
  947. if (!dev->vinput[i].type)
  948. return -EINVAL;
  949. dev->input = i;
  950. rc = vidioc_s_std(file, priv, &dev->vfd->current_norm);
  951. return rc;
  952. }
  953. /* --- controls ---------------------------------------------- */
  954. static int tm6000_s_ctrl(struct v4l2_ctrl *ctrl)
  955. {
  956. struct tm6000_core *dev = container_of(ctrl->handler, struct tm6000_core, ctrl_handler);
  957. u8 val = ctrl->val;
  958. switch (ctrl->id) {
  959. case V4L2_CID_CONTRAST:
  960. tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
  961. return 0;
  962. case V4L2_CID_BRIGHTNESS:
  963. tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
  964. return 0;
  965. case V4L2_CID_SATURATION:
  966. tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
  967. return 0;
  968. case V4L2_CID_HUE:
  969. tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
  970. return 0;
  971. }
  972. return -EINVAL;
  973. }
  974. static const struct v4l2_ctrl_ops tm6000_ctrl_ops = {
  975. .s_ctrl = tm6000_s_ctrl,
  976. };
  977. static int tm6000_radio_s_ctrl(struct v4l2_ctrl *ctrl)
  978. {
  979. struct tm6000_core *dev = container_of(ctrl->handler,
  980. struct tm6000_core, radio_ctrl_handler);
  981. u8 val = ctrl->val;
  982. switch (ctrl->id) {
  983. case V4L2_CID_AUDIO_MUTE:
  984. dev->ctl_mute = val;
  985. tm6000_tvaudio_set_mute(dev, val);
  986. return 0;
  987. case V4L2_CID_AUDIO_VOLUME:
  988. dev->ctl_volume = val;
  989. tm6000_set_volume(dev, val);
  990. return 0;
  991. }
  992. return -EINVAL;
  993. }
  994. static const struct v4l2_ctrl_ops tm6000_radio_ctrl_ops = {
  995. .s_ctrl = tm6000_radio_s_ctrl,
  996. };
  997. static int vidioc_g_tuner(struct file *file, void *priv,
  998. struct v4l2_tuner *t)
  999. {
  1000. struct tm6000_fh *fh = priv;
  1001. struct tm6000_core *dev = fh->dev;
  1002. if (UNSET == dev->tuner_type)
  1003. return -ENOTTY;
  1004. if (0 != t->index)
  1005. return -EINVAL;
  1006. strcpy(t->name, "Television");
  1007. t->type = V4L2_TUNER_ANALOG_TV;
  1008. t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
  1009. t->rangehigh = 0xffffffffUL;
  1010. t->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1011. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1012. t->audmode = dev->amode;
  1013. return 0;
  1014. }
  1015. static int vidioc_s_tuner(struct file *file, void *priv,
  1016. struct v4l2_tuner *t)
  1017. {
  1018. struct tm6000_fh *fh = priv;
  1019. struct tm6000_core *dev = fh->dev;
  1020. if (UNSET == dev->tuner_type)
  1021. return -ENOTTY;
  1022. if (0 != t->index)
  1023. return -EINVAL;
  1024. if (t->audmode > V4L2_TUNER_MODE_STEREO)
  1025. dev->amode = V4L2_TUNER_MODE_STEREO;
  1026. else
  1027. dev->amode = t->audmode;
  1028. dprintk(dev, 3, "audio mode: %x\n", t->audmode);
  1029. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1030. return 0;
  1031. }
  1032. static int vidioc_g_frequency(struct file *file, void *priv,
  1033. struct v4l2_frequency *f)
  1034. {
  1035. struct tm6000_fh *fh = priv;
  1036. struct tm6000_core *dev = fh->dev;
  1037. if (UNSET == dev->tuner_type)
  1038. return -ENOTTY;
  1039. if (f->tuner)
  1040. return -EINVAL;
  1041. f->frequency = dev->freq;
  1042. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
  1043. return 0;
  1044. }
  1045. static int vidioc_s_frequency(struct file *file, void *priv,
  1046. struct v4l2_frequency *f)
  1047. {
  1048. struct tm6000_fh *fh = priv;
  1049. struct tm6000_core *dev = fh->dev;
  1050. if (UNSET == dev->tuner_type)
  1051. return -ENOTTY;
  1052. if (f->tuner != 0)
  1053. return -EINVAL;
  1054. dev->freq = f->frequency;
  1055. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
  1056. return 0;
  1057. }
  1058. static int radio_g_tuner(struct file *file, void *priv,
  1059. struct v4l2_tuner *t)
  1060. {
  1061. struct tm6000_fh *fh = file->private_data;
  1062. struct tm6000_core *dev = fh->dev;
  1063. if (0 != t->index)
  1064. return -EINVAL;
  1065. memset(t, 0, sizeof(*t));
  1066. strcpy(t->name, "Radio");
  1067. t->type = V4L2_TUNER_RADIO;
  1068. t->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
  1069. t->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1070. t->audmode = V4L2_TUNER_MODE_STEREO;
  1071. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1072. return 0;
  1073. }
  1074. static int radio_s_tuner(struct file *file, void *priv,
  1075. struct v4l2_tuner *t)
  1076. {
  1077. struct tm6000_fh *fh = file->private_data;
  1078. struct tm6000_core *dev = fh->dev;
  1079. if (0 != t->index)
  1080. return -EINVAL;
  1081. if (t->audmode > V4L2_TUNER_MODE_STEREO)
  1082. t->audmode = V4L2_TUNER_MODE_STEREO;
  1083. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1084. return 0;
  1085. }
  1086. /* ------------------------------------------------------------------
  1087. File operations for the device
  1088. ------------------------------------------------------------------*/
  1089. static int __tm6000_open(struct file *file)
  1090. {
  1091. struct video_device *vdev = video_devdata(file);
  1092. struct tm6000_core *dev = video_drvdata(file);
  1093. struct tm6000_fh *fh;
  1094. enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1095. int rc;
  1096. int radio = 0;
  1097. dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
  1098. video_device_node_name(vdev));
  1099. switch (vdev->vfl_type) {
  1100. case VFL_TYPE_GRABBER:
  1101. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1102. break;
  1103. case VFL_TYPE_VBI:
  1104. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1105. break;
  1106. case VFL_TYPE_RADIO:
  1107. radio = 1;
  1108. break;
  1109. }
  1110. /* If more than one user, mutex should be added */
  1111. dev->users++;
  1112. dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
  1113. video_device_node_name(vdev), v4l2_type_names[type],
  1114. dev->users);
  1115. /* allocate + initialize per filehandle data */
  1116. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  1117. if (NULL == fh) {
  1118. dev->users--;
  1119. return -ENOMEM;
  1120. }
  1121. v4l2_fh_init(&fh->fh, vdev);
  1122. file->private_data = fh;
  1123. fh->dev = dev;
  1124. fh->radio = radio;
  1125. dev->radio = radio;
  1126. fh->type = type;
  1127. dev->fourcc = format[0].fourcc;
  1128. fh->fmt = format_by_fourcc(dev->fourcc);
  1129. tm6000_get_std_res(dev);
  1130. fh->width = dev->width;
  1131. fh->height = dev->height;
  1132. dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
  1133. "dev->vidq=0x%08lx\n",
  1134. (unsigned long)fh, (unsigned long)dev,
  1135. (unsigned long)&dev->vidq);
  1136. dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
  1137. "queued=%d\n", list_empty(&dev->vidq.queued));
  1138. dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
  1139. "active=%d\n", list_empty(&dev->vidq.active));
  1140. /* initialize hardware on analog mode */
  1141. rc = tm6000_init_analog_mode(dev);
  1142. if (rc < 0)
  1143. return rc;
  1144. dev->mode = TM6000_MODE_ANALOG;
  1145. if (!fh->radio) {
  1146. videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
  1147. NULL, &dev->slock,
  1148. fh->type,
  1149. V4L2_FIELD_INTERLACED,
  1150. sizeof(struct tm6000_buffer), fh, &dev->lock);
  1151. } else {
  1152. dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
  1153. tm6000_set_audio_rinput(dev);
  1154. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
  1155. tm6000_prepare_isoc(dev);
  1156. tm6000_start_thread(dev);
  1157. }
  1158. v4l2_fh_add(&fh->fh);
  1159. return 0;
  1160. }
  1161. static int tm6000_open(struct file *file)
  1162. {
  1163. struct video_device *vdev = video_devdata(file);
  1164. int res;
  1165. mutex_lock(vdev->lock);
  1166. res = __tm6000_open(file);
  1167. mutex_unlock(vdev->lock);
  1168. return res;
  1169. }
  1170. static ssize_t
  1171. tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
  1172. {
  1173. struct tm6000_fh *fh = file->private_data;
  1174. struct tm6000_core *dev = fh->dev;
  1175. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1176. int res;
  1177. if (!res_get(fh->dev, fh, true))
  1178. return -EBUSY;
  1179. if (mutex_lock_interruptible(&dev->lock))
  1180. return -ERESTARTSYS;
  1181. res = videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
  1182. file->f_flags & O_NONBLOCK);
  1183. mutex_unlock(&dev->lock);
  1184. return res;
  1185. }
  1186. return 0;
  1187. }
  1188. static unsigned int
  1189. __tm6000_poll(struct file *file, struct poll_table_struct *wait)
  1190. {
  1191. unsigned long req_events = poll_requested_events(wait);
  1192. struct tm6000_fh *fh = file->private_data;
  1193. struct tm6000_buffer *buf;
  1194. int res = 0;
  1195. if (v4l2_event_pending(&fh->fh))
  1196. res = POLLPRI;
  1197. else if (req_events & POLLPRI)
  1198. poll_wait(file, &fh->fh.wait, wait);
  1199. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  1200. return res | POLLERR;
  1201. if (!!is_res_streaming(fh->dev, fh))
  1202. return res | POLLERR;
  1203. if (!is_res_read(fh->dev, fh)) {
  1204. /* streaming capture */
  1205. if (list_empty(&fh->vb_vidq.stream))
  1206. return res | POLLERR;
  1207. buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream);
  1208. poll_wait(file, &buf->vb.done, wait);
  1209. if (buf->vb.state == VIDEOBUF_DONE ||
  1210. buf->vb.state == VIDEOBUF_ERROR)
  1211. return res | POLLIN | POLLRDNORM;
  1212. } else if (req_events & (POLLIN | POLLRDNORM)) {
  1213. /* read() capture */
  1214. return res | videobuf_poll_stream(file, &fh->vb_vidq, wait);
  1215. }
  1216. return res;
  1217. }
  1218. static unsigned int tm6000_poll(struct file *file, struct poll_table_struct *wait)
  1219. {
  1220. struct tm6000_fh *fh = file->private_data;
  1221. struct tm6000_core *dev = fh->dev;
  1222. unsigned int res;
  1223. mutex_lock(&dev->lock);
  1224. res = __tm6000_poll(file, wait);
  1225. mutex_unlock(&dev->lock);
  1226. return res;
  1227. }
  1228. static int tm6000_release(struct file *file)
  1229. {
  1230. struct tm6000_fh *fh = file->private_data;
  1231. struct tm6000_core *dev = fh->dev;
  1232. struct video_device *vdev = video_devdata(file);
  1233. dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
  1234. video_device_node_name(vdev), dev->users);
  1235. mutex_lock(&dev->lock);
  1236. dev->users--;
  1237. res_free(dev, fh);
  1238. if (!dev->users) {
  1239. tm6000_uninit_isoc(dev);
  1240. /* Stop interrupt USB pipe */
  1241. tm6000_ir_int_stop(dev);
  1242. usb_reset_configuration(dev->udev);
  1243. if (dev->int_in.endp)
  1244. usb_set_interface(dev->udev,
  1245. dev->isoc_in.bInterfaceNumber, 2);
  1246. else
  1247. usb_set_interface(dev->udev,
  1248. dev->isoc_in.bInterfaceNumber, 0);
  1249. /* Start interrupt USB pipe */
  1250. tm6000_ir_int_start(dev);
  1251. if (!fh->radio)
  1252. videobuf_mmap_free(&fh->vb_vidq);
  1253. }
  1254. v4l2_fh_del(&fh->fh);
  1255. v4l2_fh_exit(&fh->fh);
  1256. kfree(fh);
  1257. mutex_unlock(&dev->lock);
  1258. return 0;
  1259. }
  1260. static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
  1261. {
  1262. struct tm6000_fh *fh = file->private_data;
  1263. struct tm6000_core *dev = fh->dev;
  1264. int res;
  1265. if (mutex_lock_interruptible(&dev->lock))
  1266. return -ERESTARTSYS;
  1267. res = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1268. mutex_unlock(&dev->lock);
  1269. return res;
  1270. }
  1271. static struct v4l2_file_operations tm6000_fops = {
  1272. .owner = THIS_MODULE,
  1273. .open = tm6000_open,
  1274. .release = tm6000_release,
  1275. .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
  1276. .read = tm6000_read,
  1277. .poll = tm6000_poll,
  1278. .mmap = tm6000_mmap,
  1279. };
  1280. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1281. .vidioc_querycap = vidioc_querycap,
  1282. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1283. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1284. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1285. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1286. .vidioc_s_std = vidioc_s_std,
  1287. .vidioc_enum_input = vidioc_enum_input,
  1288. .vidioc_g_input = vidioc_g_input,
  1289. .vidioc_s_input = vidioc_s_input,
  1290. .vidioc_g_tuner = vidioc_g_tuner,
  1291. .vidioc_s_tuner = vidioc_s_tuner,
  1292. .vidioc_g_frequency = vidioc_g_frequency,
  1293. .vidioc_s_frequency = vidioc_s_frequency,
  1294. .vidioc_streamon = vidioc_streamon,
  1295. .vidioc_streamoff = vidioc_streamoff,
  1296. .vidioc_reqbufs = vidioc_reqbufs,
  1297. .vidioc_querybuf = vidioc_querybuf,
  1298. .vidioc_qbuf = vidioc_qbuf,
  1299. .vidioc_dqbuf = vidioc_dqbuf,
  1300. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1301. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1302. };
  1303. static struct video_device tm6000_template = {
  1304. .name = "tm6000",
  1305. .fops = &tm6000_fops,
  1306. .ioctl_ops = &video_ioctl_ops,
  1307. .release = video_device_release,
  1308. .tvnorms = TM6000_STD,
  1309. .current_norm = V4L2_STD_NTSC_M,
  1310. };
  1311. static const struct v4l2_file_operations radio_fops = {
  1312. .owner = THIS_MODULE,
  1313. .open = tm6000_open,
  1314. .poll = v4l2_ctrl_poll,
  1315. .release = tm6000_release,
  1316. .unlocked_ioctl = video_ioctl2,
  1317. };
  1318. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1319. .vidioc_querycap = vidioc_querycap,
  1320. .vidioc_g_tuner = radio_g_tuner,
  1321. .vidioc_s_tuner = radio_s_tuner,
  1322. .vidioc_g_frequency = vidioc_g_frequency,
  1323. .vidioc_s_frequency = vidioc_s_frequency,
  1324. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1325. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1326. };
  1327. static struct video_device tm6000_radio_template = {
  1328. .name = "tm6000",
  1329. .fops = &radio_fops,
  1330. .ioctl_ops = &radio_ioctl_ops,
  1331. };
  1332. /* -----------------------------------------------------------------
  1333. * Initialization and module stuff
  1334. * ------------------------------------------------------------------
  1335. */
  1336. static struct video_device *vdev_init(struct tm6000_core *dev,
  1337. const struct video_device
  1338. *template, const char *type_name)
  1339. {
  1340. struct video_device *vfd;
  1341. vfd = video_device_alloc();
  1342. if (NULL == vfd)
  1343. return NULL;
  1344. *vfd = *template;
  1345. vfd->v4l2_dev = &dev->v4l2_dev;
  1346. vfd->release = video_device_release;
  1347. vfd->debug = tm6000_debug;
  1348. vfd->lock = &dev->lock;
  1349. set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  1350. snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
  1351. video_set_drvdata(vfd, dev);
  1352. return vfd;
  1353. }
  1354. int tm6000_v4l2_register(struct tm6000_core *dev)
  1355. {
  1356. int ret = 0;
  1357. v4l2_ctrl_handler_init(&dev->ctrl_handler, 6);
  1358. v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 2);
  1359. v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
  1360. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
  1361. v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
  1362. V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
  1363. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1364. V4L2_CID_BRIGHTNESS, 0, 255, 1, 54);
  1365. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1366. V4L2_CID_CONTRAST, 0, 255, 1, 119);
  1367. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1368. V4L2_CID_SATURATION, 0, 255, 1, 112);
  1369. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1370. V4L2_CID_HUE, -128, 127, 1, 0);
  1371. v4l2_ctrl_add_handler(&dev->ctrl_handler,
  1372. &dev->radio_ctrl_handler, NULL);
  1373. if (dev->radio_ctrl_handler.error)
  1374. ret = dev->radio_ctrl_handler.error;
  1375. if (!ret && dev->ctrl_handler.error)
  1376. ret = dev->ctrl_handler.error;
  1377. if (ret)
  1378. goto free_ctrl;
  1379. dev->vfd = vdev_init(dev, &tm6000_template, "video");
  1380. if (!dev->vfd) {
  1381. printk(KERN_INFO "%s: can't register video device\n",
  1382. dev->name);
  1383. ret = -ENOMEM;
  1384. goto free_ctrl;
  1385. }
  1386. dev->vfd->ctrl_handler = &dev->ctrl_handler;
  1387. /* init video dma queues */
  1388. INIT_LIST_HEAD(&dev->vidq.active);
  1389. INIT_LIST_HEAD(&dev->vidq.queued);
  1390. ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
  1391. if (ret < 0) {
  1392. printk(KERN_INFO "%s: can't register video device\n",
  1393. dev->name);
  1394. video_device_release(dev->vfd);
  1395. dev->vfd = NULL;
  1396. goto free_ctrl;
  1397. }
  1398. printk(KERN_INFO "%s: registered device %s\n",
  1399. dev->name, video_device_node_name(dev->vfd));
  1400. if (dev->caps.has_radio) {
  1401. dev->radio_dev = vdev_init(dev, &tm6000_radio_template,
  1402. "radio");
  1403. if (!dev->radio_dev) {
  1404. printk(KERN_INFO "%s: can't register radio device\n",
  1405. dev->name);
  1406. ret = -ENXIO;
  1407. goto unreg_video;
  1408. }
  1409. dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler;
  1410. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1411. radio_nr);
  1412. if (ret < 0) {
  1413. printk(KERN_INFO "%s: can't register radio device\n",
  1414. dev->name);
  1415. video_device_release(dev->radio_dev);
  1416. goto unreg_video;
  1417. }
  1418. printk(KERN_INFO "%s: registered device %s\n",
  1419. dev->name, video_device_node_name(dev->radio_dev));
  1420. }
  1421. printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
  1422. return ret;
  1423. unreg_video:
  1424. video_unregister_device(dev->vfd);
  1425. free_ctrl:
  1426. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  1427. v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
  1428. return ret;
  1429. }
  1430. int tm6000_v4l2_unregister(struct tm6000_core *dev)
  1431. {
  1432. video_unregister_device(dev->vfd);
  1433. /* if URB buffers are still allocated free them now */
  1434. tm6000_free_urb_buffers(dev);
  1435. if (dev->radio_dev) {
  1436. if (video_is_registered(dev->radio_dev))
  1437. video_unregister_device(dev->radio_dev);
  1438. else
  1439. video_device_release(dev->radio_dev);
  1440. dev->radio_dev = NULL;
  1441. }
  1442. return 0;
  1443. }
  1444. int tm6000_v4l2_exit(void)
  1445. {
  1446. return 0;
  1447. }
  1448. module_param(video_nr, int, 0);
  1449. MODULE_PARM_DESC(video_nr, "Allow changing video device number");
  1450. module_param_named(debug, tm6000_debug, int, 0444);
  1451. MODULE_PARM_DESC(debug, "activates debug info");
  1452. module_param(vid_limit, int, 0644);
  1453. MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  1454. module_param(keep_urb, bool, 0);
  1455. MODULE_PARM_DESC(keep_urb, "Keep urb buffers allocated even when the device is closed by the user");