tm6000-video.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  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 (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. BUG_ON(in_interrupt());
  623. /* We used to wait for the buffer to finish here, but this didn't work
  624. because, as we were keeping the state as VIDEOBUF_QUEUED,
  625. videobuf_queue_cancel marked it as finished for us.
  626. (Also, it could wedge forever if the hardware was misconfigured.)
  627. This should be safe; by the time we get here, the buffer isn't
  628. queued anymore. If we ever start marking the buffers as
  629. VIDEOBUF_ACTIVE, it won't be, though.
  630. */
  631. spin_lock_irqsave(&dev->slock, flags);
  632. if (dev->isoc_ctl.buf == buf)
  633. dev->isoc_ctl.buf = NULL;
  634. spin_unlock_irqrestore(&dev->slock, flags);
  635. videobuf_vmalloc_free(&buf->vb);
  636. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  637. }
  638. static int
  639. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  640. enum v4l2_field field)
  641. {
  642. struct tm6000_fh *fh = vq->priv_data;
  643. struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
  644. struct tm6000_core *dev = fh->dev;
  645. int rc = 0;
  646. BUG_ON(NULL == fh->fmt);
  647. /* FIXME: It assumes depth=2 */
  648. /* The only currently supported format is 16 bits/pixel */
  649. buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
  650. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  651. return -EINVAL;
  652. if (buf->fmt != fh->fmt ||
  653. buf->vb.width != fh->width ||
  654. buf->vb.height != fh->height ||
  655. buf->vb.field != field) {
  656. buf->fmt = fh->fmt;
  657. buf->vb.width = fh->width;
  658. buf->vb.height = fh->height;
  659. buf->vb.field = field;
  660. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  661. }
  662. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  663. rc = videobuf_iolock(vq, &buf->vb, NULL);
  664. if (rc != 0)
  665. goto fail;
  666. }
  667. if (!dev->isoc_ctl.num_bufs) {
  668. rc = tm6000_prepare_isoc(dev);
  669. if (rc < 0)
  670. goto fail;
  671. rc = tm6000_start_thread(dev);
  672. if (rc < 0)
  673. goto fail;
  674. }
  675. buf->vb.state = VIDEOBUF_PREPARED;
  676. return 0;
  677. fail:
  678. free_buffer(vq, buf);
  679. return rc;
  680. }
  681. static void
  682. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  683. {
  684. struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
  685. struct tm6000_fh *fh = vq->priv_data;
  686. struct tm6000_core *dev = fh->dev;
  687. struct tm6000_dmaqueue *vidq = &dev->vidq;
  688. buf->vb.state = VIDEOBUF_QUEUED;
  689. list_add_tail(&buf->vb.queue, &vidq->active);
  690. }
  691. static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  692. {
  693. struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
  694. free_buffer(vq, buf);
  695. }
  696. static struct videobuf_queue_ops tm6000_video_qops = {
  697. .buf_setup = buffer_setup,
  698. .buf_prepare = buffer_prepare,
  699. .buf_queue = buffer_queue,
  700. .buf_release = buffer_release,
  701. };
  702. /* ------------------------------------------------------------------
  703. * IOCTL handling
  704. * ------------------------------------------------------------------
  705. */
  706. static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
  707. {
  708. /* Is the current fh handling it? if so, that's OK */
  709. if (dev->resources == fh && dev->is_res_read)
  710. return true;
  711. return false;
  712. }
  713. static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
  714. {
  715. /* Is the current fh handling it? if so, that's OK */
  716. if (dev->resources == fh)
  717. return true;
  718. return false;
  719. }
  720. static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
  721. bool is_res_read)
  722. {
  723. /* Is the current fh handling it? if so, that's OK */
  724. if (dev->resources == fh && dev->is_res_read == is_res_read)
  725. return true;
  726. /* is it free? */
  727. if (dev->resources)
  728. return false;
  729. /* grab it */
  730. dev->resources = fh;
  731. dev->is_res_read = is_res_read;
  732. dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
  733. return true;
  734. }
  735. static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
  736. {
  737. /* Is the current fh handling it? if so, that's OK */
  738. if (dev->resources != fh)
  739. return;
  740. dev->resources = NULL;
  741. dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
  742. }
  743. /* ------------------------------------------------------------------
  744. * IOCTL vidioc handling
  745. * ------------------------------------------------------------------
  746. */
  747. static int vidioc_querycap(struct file *file, void *priv,
  748. struct v4l2_capability *cap)
  749. {
  750. struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
  751. struct video_device *vdev = video_devdata(file);
  752. strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
  753. strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
  754. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  755. if (dev->tuner_type != TUNER_ABSENT)
  756. cap->device_caps |= V4L2_CAP_TUNER;
  757. if (vdev->vfl_type == VFL_TYPE_GRABBER)
  758. cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE |
  759. V4L2_CAP_STREAMING |
  760. V4L2_CAP_READWRITE;
  761. else
  762. cap->device_caps |= V4L2_CAP_RADIO;
  763. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
  764. V4L2_CAP_RADIO | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
  765. return 0;
  766. }
  767. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  768. struct v4l2_fmtdesc *f)
  769. {
  770. if (f->index >= ARRAY_SIZE(format))
  771. return -EINVAL;
  772. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  773. f->pixelformat = format[f->index].fourcc;
  774. return 0;
  775. }
  776. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  777. struct v4l2_format *f)
  778. {
  779. struct tm6000_fh *fh = priv;
  780. f->fmt.pix.width = fh->width;
  781. f->fmt.pix.height = fh->height;
  782. f->fmt.pix.field = fh->vb_vidq.field;
  783. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  784. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  785. f->fmt.pix.bytesperline =
  786. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  787. f->fmt.pix.sizeimage =
  788. f->fmt.pix.height * f->fmt.pix.bytesperline;
  789. return 0;
  790. }
  791. static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
  792. {
  793. unsigned int i;
  794. for (i = 0; i < ARRAY_SIZE(format); i++)
  795. if (format[i].fourcc == fourcc)
  796. return format+i;
  797. return NULL;
  798. }
  799. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  800. struct v4l2_format *f)
  801. {
  802. struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
  803. struct tm6000_fmt *fmt;
  804. enum v4l2_field field;
  805. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  806. if (NULL == fmt) {
  807. dprintk(dev, 2, "Fourcc format (0x%08x)"
  808. " invalid.\n", f->fmt.pix.pixelformat);
  809. return -EINVAL;
  810. }
  811. field = f->fmt.pix.field;
  812. field = V4L2_FIELD_INTERLACED;
  813. tm6000_get_std_res(dev);
  814. f->fmt.pix.width = dev->width;
  815. f->fmt.pix.height = dev->height;
  816. f->fmt.pix.width &= ~0x01;
  817. f->fmt.pix.field = field;
  818. f->fmt.pix.bytesperline =
  819. (f->fmt.pix.width * fmt->depth) >> 3;
  820. f->fmt.pix.sizeimage =
  821. f->fmt.pix.height * f->fmt.pix.bytesperline;
  822. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  823. return 0;
  824. }
  825. /*FIXME: This seems to be generic enough to be at videodev2 */
  826. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  827. struct v4l2_format *f)
  828. {
  829. struct tm6000_fh *fh = priv;
  830. struct tm6000_core *dev = fh->dev;
  831. int ret = vidioc_try_fmt_vid_cap(file, fh, f);
  832. if (ret < 0)
  833. return ret;
  834. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  835. fh->width = f->fmt.pix.width;
  836. fh->height = f->fmt.pix.height;
  837. fh->vb_vidq.field = f->fmt.pix.field;
  838. fh->type = f->type;
  839. dev->fourcc = f->fmt.pix.pixelformat;
  840. tm6000_set_fourcc_format(dev);
  841. return 0;
  842. }
  843. static int vidioc_reqbufs(struct file *file, void *priv,
  844. struct v4l2_requestbuffers *p)
  845. {
  846. struct tm6000_fh *fh = priv;
  847. return videobuf_reqbufs(&fh->vb_vidq, p);
  848. }
  849. static int vidioc_querybuf(struct file *file, void *priv,
  850. struct v4l2_buffer *p)
  851. {
  852. struct tm6000_fh *fh = priv;
  853. return videobuf_querybuf(&fh->vb_vidq, p);
  854. }
  855. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  856. {
  857. struct tm6000_fh *fh = priv;
  858. return videobuf_qbuf(&fh->vb_vidq, p);
  859. }
  860. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  861. {
  862. struct tm6000_fh *fh = priv;
  863. return videobuf_dqbuf(&fh->vb_vidq, p,
  864. file->f_flags & O_NONBLOCK);
  865. }
  866. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  867. {
  868. struct tm6000_fh *fh = priv;
  869. struct tm6000_core *dev = fh->dev;
  870. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  871. return -EINVAL;
  872. if (i != fh->type)
  873. return -EINVAL;
  874. if (!res_get(dev, fh, false))
  875. return -EBUSY;
  876. return videobuf_streamon(&fh->vb_vidq);
  877. }
  878. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  879. {
  880. struct tm6000_fh *fh = priv;
  881. struct tm6000_core *dev = fh->dev;
  882. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  883. return -EINVAL;
  884. if (i != fh->type)
  885. return -EINVAL;
  886. videobuf_streamoff(&fh->vb_vidq);
  887. res_free(dev, fh);
  888. return 0;
  889. }
  890. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
  891. {
  892. int rc = 0;
  893. struct tm6000_fh *fh = priv;
  894. struct tm6000_core *dev = fh->dev;
  895. dev->norm = norm;
  896. rc = tm6000_init_analog_mode(dev);
  897. fh->width = dev->width;
  898. fh->height = dev->height;
  899. if (rc < 0)
  900. return rc;
  901. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->norm);
  902. return 0;
  903. }
  904. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  905. {
  906. struct tm6000_fh *fh = priv;
  907. struct tm6000_core *dev = fh->dev;
  908. *norm = dev->norm;
  909. return 0;
  910. }
  911. static const char *iname[] = {
  912. [TM6000_INPUT_TV] = "Television",
  913. [TM6000_INPUT_COMPOSITE1] = "Composite 1",
  914. [TM6000_INPUT_COMPOSITE2] = "Composite 2",
  915. [TM6000_INPUT_SVIDEO] = "S-Video",
  916. };
  917. static int vidioc_enum_input(struct file *file, void *priv,
  918. struct v4l2_input *i)
  919. {
  920. struct tm6000_fh *fh = priv;
  921. struct tm6000_core *dev = fh->dev;
  922. unsigned int n;
  923. n = i->index;
  924. if (n >= 3)
  925. return -EINVAL;
  926. if (!dev->vinput[n].type)
  927. return -EINVAL;
  928. i->index = n;
  929. if (dev->vinput[n].type == TM6000_INPUT_TV)
  930. i->type = V4L2_INPUT_TYPE_TUNER;
  931. else
  932. i->type = V4L2_INPUT_TYPE_CAMERA;
  933. strcpy(i->name, iname[dev->vinput[n].type]);
  934. i->std = TM6000_STD;
  935. return 0;
  936. }
  937. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  938. {
  939. struct tm6000_fh *fh = priv;
  940. struct tm6000_core *dev = fh->dev;
  941. *i = dev->input;
  942. return 0;
  943. }
  944. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  945. {
  946. struct tm6000_fh *fh = priv;
  947. struct tm6000_core *dev = fh->dev;
  948. int rc = 0;
  949. if (i >= 3)
  950. return -EINVAL;
  951. if (!dev->vinput[i].type)
  952. return -EINVAL;
  953. dev->input = i;
  954. rc = vidioc_s_std(file, priv, dev->norm);
  955. return rc;
  956. }
  957. /* --- controls ---------------------------------------------- */
  958. static int tm6000_s_ctrl(struct v4l2_ctrl *ctrl)
  959. {
  960. struct tm6000_core *dev = container_of(ctrl->handler, struct tm6000_core, ctrl_handler);
  961. u8 val = ctrl->val;
  962. switch (ctrl->id) {
  963. case V4L2_CID_CONTRAST:
  964. tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
  965. return 0;
  966. case V4L2_CID_BRIGHTNESS:
  967. tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
  968. return 0;
  969. case V4L2_CID_SATURATION:
  970. tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
  971. return 0;
  972. case V4L2_CID_HUE:
  973. tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
  974. return 0;
  975. }
  976. return -EINVAL;
  977. }
  978. static const struct v4l2_ctrl_ops tm6000_ctrl_ops = {
  979. .s_ctrl = tm6000_s_ctrl,
  980. };
  981. static int tm6000_radio_s_ctrl(struct v4l2_ctrl *ctrl)
  982. {
  983. struct tm6000_core *dev = container_of(ctrl->handler,
  984. struct tm6000_core, radio_ctrl_handler);
  985. u8 val = ctrl->val;
  986. switch (ctrl->id) {
  987. case V4L2_CID_AUDIO_MUTE:
  988. dev->ctl_mute = val;
  989. tm6000_tvaudio_set_mute(dev, val);
  990. return 0;
  991. case V4L2_CID_AUDIO_VOLUME:
  992. dev->ctl_volume = val;
  993. tm6000_set_volume(dev, val);
  994. return 0;
  995. }
  996. return -EINVAL;
  997. }
  998. static const struct v4l2_ctrl_ops tm6000_radio_ctrl_ops = {
  999. .s_ctrl = tm6000_radio_s_ctrl,
  1000. };
  1001. static int vidioc_g_tuner(struct file *file, void *priv,
  1002. struct v4l2_tuner *t)
  1003. {
  1004. struct tm6000_fh *fh = priv;
  1005. struct tm6000_core *dev = fh->dev;
  1006. if (UNSET == dev->tuner_type)
  1007. return -ENOTTY;
  1008. if (0 != t->index)
  1009. return -EINVAL;
  1010. strcpy(t->name, "Television");
  1011. t->type = V4L2_TUNER_ANALOG_TV;
  1012. t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
  1013. t->rangehigh = 0xffffffffUL;
  1014. t->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1015. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1016. t->audmode = dev->amode;
  1017. return 0;
  1018. }
  1019. static int vidioc_s_tuner(struct file *file, void *priv,
  1020. const struct v4l2_tuner *t)
  1021. {
  1022. struct tm6000_fh *fh = priv;
  1023. struct tm6000_core *dev = fh->dev;
  1024. if (UNSET == dev->tuner_type)
  1025. return -ENOTTY;
  1026. if (0 != t->index)
  1027. return -EINVAL;
  1028. if (t->audmode > V4L2_TUNER_MODE_STEREO)
  1029. dev->amode = V4L2_TUNER_MODE_STEREO;
  1030. else
  1031. dev->amode = t->audmode;
  1032. dprintk(dev, 3, "audio mode: %x\n", t->audmode);
  1033. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1034. return 0;
  1035. }
  1036. static int vidioc_g_frequency(struct file *file, void *priv,
  1037. struct v4l2_frequency *f)
  1038. {
  1039. struct tm6000_fh *fh = priv;
  1040. struct tm6000_core *dev = fh->dev;
  1041. if (UNSET == dev->tuner_type)
  1042. return -ENOTTY;
  1043. if (f->tuner)
  1044. return -EINVAL;
  1045. f->frequency = dev->freq;
  1046. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
  1047. return 0;
  1048. }
  1049. static int vidioc_s_frequency(struct file *file, void *priv,
  1050. const struct v4l2_frequency *f)
  1051. {
  1052. struct tm6000_fh *fh = priv;
  1053. struct tm6000_core *dev = fh->dev;
  1054. if (UNSET == dev->tuner_type)
  1055. return -ENOTTY;
  1056. if (f->tuner != 0)
  1057. return -EINVAL;
  1058. dev->freq = f->frequency;
  1059. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
  1060. return 0;
  1061. }
  1062. static int radio_g_tuner(struct file *file, void *priv,
  1063. struct v4l2_tuner *t)
  1064. {
  1065. struct tm6000_fh *fh = file->private_data;
  1066. struct tm6000_core *dev = fh->dev;
  1067. if (0 != t->index)
  1068. return -EINVAL;
  1069. memset(t, 0, sizeof(*t));
  1070. strcpy(t->name, "Radio");
  1071. t->type = V4L2_TUNER_RADIO;
  1072. t->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
  1073. t->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1074. t->audmode = V4L2_TUNER_MODE_STEREO;
  1075. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1076. return 0;
  1077. }
  1078. static int radio_s_tuner(struct file *file, void *priv,
  1079. const struct v4l2_tuner *t)
  1080. {
  1081. struct tm6000_fh *fh = file->private_data;
  1082. struct tm6000_core *dev = fh->dev;
  1083. if (0 != t->index)
  1084. return -EINVAL;
  1085. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1086. return 0;
  1087. }
  1088. /* ------------------------------------------------------------------
  1089. File operations for the device
  1090. ------------------------------------------------------------------*/
  1091. static int __tm6000_open(struct file *file)
  1092. {
  1093. struct video_device *vdev = video_devdata(file);
  1094. struct tm6000_core *dev = video_drvdata(file);
  1095. struct tm6000_fh *fh;
  1096. enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1097. int rc;
  1098. int radio = 0;
  1099. dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
  1100. video_device_node_name(vdev));
  1101. switch (vdev->vfl_type) {
  1102. case VFL_TYPE_GRABBER:
  1103. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1104. break;
  1105. case VFL_TYPE_VBI:
  1106. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1107. break;
  1108. case VFL_TYPE_RADIO:
  1109. radio = 1;
  1110. break;
  1111. }
  1112. /* If more than one user, mutex should be added */
  1113. dev->users++;
  1114. dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
  1115. video_device_node_name(vdev), v4l2_type_names[type],
  1116. dev->users);
  1117. /* allocate + initialize per filehandle data */
  1118. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  1119. if (NULL == fh) {
  1120. dev->users--;
  1121. return -ENOMEM;
  1122. }
  1123. v4l2_fh_init(&fh->fh, vdev);
  1124. file->private_data = fh;
  1125. fh->dev = dev;
  1126. fh->radio = radio;
  1127. dev->radio = radio;
  1128. fh->type = type;
  1129. dev->fourcc = format[0].fourcc;
  1130. fh->fmt = format_by_fourcc(dev->fourcc);
  1131. tm6000_get_std_res(dev);
  1132. fh->width = dev->width;
  1133. fh->height = dev->height;
  1134. dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
  1135. "dev->vidq=0x%08lx\n",
  1136. (unsigned long)fh, (unsigned long)dev,
  1137. (unsigned long)&dev->vidq);
  1138. dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
  1139. "queued=%d\n", list_empty(&dev->vidq.queued));
  1140. dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
  1141. "active=%d\n", list_empty(&dev->vidq.active));
  1142. /* initialize hardware on analog mode */
  1143. rc = tm6000_init_analog_mode(dev);
  1144. if (rc < 0)
  1145. return rc;
  1146. dev->mode = TM6000_MODE_ANALOG;
  1147. if (!fh->radio) {
  1148. videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
  1149. NULL, &dev->slock,
  1150. fh->type,
  1151. V4L2_FIELD_INTERLACED,
  1152. sizeof(struct tm6000_buffer), fh, &dev->lock);
  1153. } else {
  1154. dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
  1155. tm6000_set_audio_rinput(dev);
  1156. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
  1157. tm6000_prepare_isoc(dev);
  1158. tm6000_start_thread(dev);
  1159. }
  1160. v4l2_fh_add(&fh->fh);
  1161. return 0;
  1162. }
  1163. static int tm6000_open(struct file *file)
  1164. {
  1165. struct video_device *vdev = video_devdata(file);
  1166. int res;
  1167. mutex_lock(vdev->lock);
  1168. res = __tm6000_open(file);
  1169. mutex_unlock(vdev->lock);
  1170. return res;
  1171. }
  1172. static ssize_t
  1173. tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
  1174. {
  1175. struct tm6000_fh *fh = file->private_data;
  1176. struct tm6000_core *dev = fh->dev;
  1177. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1178. int res;
  1179. if (!res_get(fh->dev, fh, true))
  1180. return -EBUSY;
  1181. if (mutex_lock_interruptible(&dev->lock))
  1182. return -ERESTARTSYS;
  1183. res = videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
  1184. file->f_flags & O_NONBLOCK);
  1185. mutex_unlock(&dev->lock);
  1186. return res;
  1187. }
  1188. return 0;
  1189. }
  1190. static unsigned int
  1191. __tm6000_poll(struct file *file, struct poll_table_struct *wait)
  1192. {
  1193. unsigned long req_events = poll_requested_events(wait);
  1194. struct tm6000_fh *fh = file->private_data;
  1195. struct tm6000_buffer *buf;
  1196. int res = 0;
  1197. if (v4l2_event_pending(&fh->fh))
  1198. res = POLLPRI;
  1199. else if (req_events & POLLPRI)
  1200. poll_wait(file, &fh->fh.wait, wait);
  1201. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  1202. return res | POLLERR;
  1203. if (!!is_res_streaming(fh->dev, fh))
  1204. return res | POLLERR;
  1205. if (!is_res_read(fh->dev, fh)) {
  1206. /* streaming capture */
  1207. if (list_empty(&fh->vb_vidq.stream))
  1208. return res | POLLERR;
  1209. buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream);
  1210. poll_wait(file, &buf->vb.done, wait);
  1211. if (buf->vb.state == VIDEOBUF_DONE ||
  1212. buf->vb.state == VIDEOBUF_ERROR)
  1213. return res | POLLIN | POLLRDNORM;
  1214. } else if (req_events & (POLLIN | POLLRDNORM)) {
  1215. /* read() capture */
  1216. return res | videobuf_poll_stream(file, &fh->vb_vidq, wait);
  1217. }
  1218. return res;
  1219. }
  1220. static unsigned int tm6000_poll(struct file *file, struct poll_table_struct *wait)
  1221. {
  1222. struct tm6000_fh *fh = file->private_data;
  1223. struct tm6000_core *dev = fh->dev;
  1224. unsigned int res;
  1225. mutex_lock(&dev->lock);
  1226. res = __tm6000_poll(file, wait);
  1227. mutex_unlock(&dev->lock);
  1228. return res;
  1229. }
  1230. static int tm6000_release(struct file *file)
  1231. {
  1232. struct tm6000_fh *fh = file->private_data;
  1233. struct tm6000_core *dev = fh->dev;
  1234. struct video_device *vdev = video_devdata(file);
  1235. dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
  1236. video_device_node_name(vdev), dev->users);
  1237. mutex_lock(&dev->lock);
  1238. dev->users--;
  1239. res_free(dev, fh);
  1240. if (!dev->users) {
  1241. tm6000_uninit_isoc(dev);
  1242. /* Stop interrupt USB pipe */
  1243. tm6000_ir_int_stop(dev);
  1244. usb_reset_configuration(dev->udev);
  1245. if (dev->int_in.endp)
  1246. usb_set_interface(dev->udev,
  1247. dev->isoc_in.bInterfaceNumber, 2);
  1248. else
  1249. usb_set_interface(dev->udev,
  1250. dev->isoc_in.bInterfaceNumber, 0);
  1251. /* Start interrupt USB pipe */
  1252. tm6000_ir_int_start(dev);
  1253. if (!fh->radio)
  1254. videobuf_mmap_free(&fh->vb_vidq);
  1255. }
  1256. v4l2_fh_del(&fh->fh);
  1257. v4l2_fh_exit(&fh->fh);
  1258. kfree(fh);
  1259. mutex_unlock(&dev->lock);
  1260. return 0;
  1261. }
  1262. static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
  1263. {
  1264. struct tm6000_fh *fh = file->private_data;
  1265. struct tm6000_core *dev = fh->dev;
  1266. int res;
  1267. if (mutex_lock_interruptible(&dev->lock))
  1268. return -ERESTARTSYS;
  1269. res = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1270. mutex_unlock(&dev->lock);
  1271. return res;
  1272. }
  1273. static struct v4l2_file_operations tm6000_fops = {
  1274. .owner = THIS_MODULE,
  1275. .open = tm6000_open,
  1276. .release = tm6000_release,
  1277. .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
  1278. .read = tm6000_read,
  1279. .poll = tm6000_poll,
  1280. .mmap = tm6000_mmap,
  1281. };
  1282. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1283. .vidioc_querycap = vidioc_querycap,
  1284. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1285. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1286. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1287. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1288. .vidioc_s_std = vidioc_s_std,
  1289. .vidioc_g_std = vidioc_g_std,
  1290. .vidioc_enum_input = vidioc_enum_input,
  1291. .vidioc_g_input = vidioc_g_input,
  1292. .vidioc_s_input = vidioc_s_input,
  1293. .vidioc_g_tuner = vidioc_g_tuner,
  1294. .vidioc_s_tuner = vidioc_s_tuner,
  1295. .vidioc_g_frequency = vidioc_g_frequency,
  1296. .vidioc_s_frequency = vidioc_s_frequency,
  1297. .vidioc_streamon = vidioc_streamon,
  1298. .vidioc_streamoff = vidioc_streamoff,
  1299. .vidioc_reqbufs = vidioc_reqbufs,
  1300. .vidioc_querybuf = vidioc_querybuf,
  1301. .vidioc_qbuf = vidioc_qbuf,
  1302. .vidioc_dqbuf = vidioc_dqbuf,
  1303. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1304. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1305. };
  1306. static struct video_device tm6000_template = {
  1307. .name = "tm6000",
  1308. .fops = &tm6000_fops,
  1309. .ioctl_ops = &video_ioctl_ops,
  1310. .release = video_device_release_empty,
  1311. .tvnorms = TM6000_STD,
  1312. };
  1313. static const struct v4l2_file_operations radio_fops = {
  1314. .owner = THIS_MODULE,
  1315. .open = tm6000_open,
  1316. .poll = v4l2_ctrl_poll,
  1317. .release = tm6000_release,
  1318. .unlocked_ioctl = video_ioctl2,
  1319. };
  1320. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1321. .vidioc_querycap = vidioc_querycap,
  1322. .vidioc_g_tuner = radio_g_tuner,
  1323. .vidioc_s_tuner = radio_s_tuner,
  1324. .vidioc_g_frequency = vidioc_g_frequency,
  1325. .vidioc_s_frequency = vidioc_s_frequency,
  1326. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1327. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1328. };
  1329. static struct video_device tm6000_radio_template = {
  1330. .name = "tm6000",
  1331. .fops = &radio_fops,
  1332. .ioctl_ops = &radio_ioctl_ops,
  1333. };
  1334. /* -----------------------------------------------------------------
  1335. * Initialization and module stuff
  1336. * ------------------------------------------------------------------
  1337. */
  1338. static void vdev_init(struct tm6000_core *dev,
  1339. struct video_device *vfd,
  1340. const struct video_device
  1341. *template, const char *type_name)
  1342. {
  1343. *vfd = *template;
  1344. vfd->v4l2_dev = &dev->v4l2_dev;
  1345. vfd->release = video_device_release_empty;
  1346. vfd->lock = &dev->lock;
  1347. snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
  1348. video_set_drvdata(vfd, dev);
  1349. }
  1350. int tm6000_v4l2_register(struct tm6000_core *dev)
  1351. {
  1352. int ret = 0;
  1353. v4l2_ctrl_handler_init(&dev->ctrl_handler, 6);
  1354. v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 2);
  1355. v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
  1356. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
  1357. v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
  1358. V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
  1359. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1360. V4L2_CID_BRIGHTNESS, 0, 255, 1, 54);
  1361. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1362. V4L2_CID_CONTRAST, 0, 255, 1, 119);
  1363. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1364. V4L2_CID_SATURATION, 0, 255, 1, 112);
  1365. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1366. V4L2_CID_HUE, -128, 127, 1, 0);
  1367. v4l2_ctrl_add_handler(&dev->ctrl_handler,
  1368. &dev->radio_ctrl_handler, NULL);
  1369. if (dev->radio_ctrl_handler.error)
  1370. ret = dev->radio_ctrl_handler.error;
  1371. if (!ret && dev->ctrl_handler.error)
  1372. ret = dev->ctrl_handler.error;
  1373. if (ret)
  1374. goto free_ctrl;
  1375. vdev_init(dev, &dev->vfd, &tm6000_template, "video");
  1376. dev->vfd.ctrl_handler = &dev->ctrl_handler;
  1377. /* init video dma queues */
  1378. INIT_LIST_HEAD(&dev->vidq.active);
  1379. INIT_LIST_HEAD(&dev->vidq.queued);
  1380. ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, video_nr);
  1381. if (ret < 0) {
  1382. printk(KERN_INFO "%s: can't register video device\n",
  1383. dev->name);
  1384. goto free_ctrl;
  1385. }
  1386. printk(KERN_INFO "%s: registered device %s\n",
  1387. dev->name, video_device_node_name(&dev->vfd));
  1388. if (dev->caps.has_radio) {
  1389. vdev_init(dev, &dev->radio_dev, &tm6000_radio_template,
  1390. "radio");
  1391. dev->radio_dev.ctrl_handler = &dev->radio_ctrl_handler;
  1392. ret = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
  1393. radio_nr);
  1394. if (ret < 0) {
  1395. printk(KERN_INFO "%s: can't register radio device\n",
  1396. dev->name);
  1397. goto unreg_video;
  1398. }
  1399. printk(KERN_INFO "%s: registered device %s\n",
  1400. dev->name, video_device_node_name(&dev->radio_dev));
  1401. }
  1402. printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
  1403. return ret;
  1404. unreg_video:
  1405. video_unregister_device(&dev->vfd);
  1406. free_ctrl:
  1407. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  1408. v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
  1409. return ret;
  1410. }
  1411. int tm6000_v4l2_unregister(struct tm6000_core *dev)
  1412. {
  1413. video_unregister_device(&dev->vfd);
  1414. /* if URB buffers are still allocated free them now */
  1415. tm6000_free_urb_buffers(dev);
  1416. video_unregister_device(&dev->radio_dev);
  1417. return 0;
  1418. }
  1419. int tm6000_v4l2_exit(void)
  1420. {
  1421. return 0;
  1422. }
  1423. module_param(video_nr, int, 0);
  1424. MODULE_PARM_DESC(video_nr, "Allow changing video device number");
  1425. module_param_named(debug, tm6000_debug, int, 0444);
  1426. MODULE_PARM_DESC(debug, "activates debug info");
  1427. module_param(vid_limit, int, 0644);
  1428. MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  1429. module_param(keep_urb, bool, 0);
  1430. MODULE_PARM_DESC(keep_urb, "Keep urb buffers allocated even when the device is closed by the user");