tm6000-video.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760
  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 int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  908. {
  909. struct tm6000_fh *fh = priv;
  910. struct tm6000_core *dev = fh->dev;
  911. *norm = dev->norm;
  912. return 0;
  913. }
  914. static const char *iname[] = {
  915. [TM6000_INPUT_TV] = "Television",
  916. [TM6000_INPUT_COMPOSITE1] = "Composite 1",
  917. [TM6000_INPUT_COMPOSITE2] = "Composite 2",
  918. [TM6000_INPUT_SVIDEO] = "S-Video",
  919. };
  920. static int vidioc_enum_input(struct file *file, void *priv,
  921. struct v4l2_input *i)
  922. {
  923. struct tm6000_fh *fh = priv;
  924. struct tm6000_core *dev = fh->dev;
  925. unsigned int n;
  926. n = i->index;
  927. if (n >= 3)
  928. return -EINVAL;
  929. if (!dev->vinput[n].type)
  930. return -EINVAL;
  931. i->index = n;
  932. if (dev->vinput[n].type == TM6000_INPUT_TV)
  933. i->type = V4L2_INPUT_TYPE_TUNER;
  934. else
  935. i->type = V4L2_INPUT_TYPE_CAMERA;
  936. strcpy(i->name, iname[dev->vinput[n].type]);
  937. i->std = TM6000_STD;
  938. return 0;
  939. }
  940. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  941. {
  942. struct tm6000_fh *fh = priv;
  943. struct tm6000_core *dev = fh->dev;
  944. *i = dev->input;
  945. return 0;
  946. }
  947. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  948. {
  949. struct tm6000_fh *fh = priv;
  950. struct tm6000_core *dev = fh->dev;
  951. int rc = 0;
  952. if (i >= 3)
  953. return -EINVAL;
  954. if (!dev->vinput[i].type)
  955. return -EINVAL;
  956. dev->input = i;
  957. rc = vidioc_s_std(file, priv, dev->norm);
  958. return rc;
  959. }
  960. /* --- controls ---------------------------------------------- */
  961. static int tm6000_s_ctrl(struct v4l2_ctrl *ctrl)
  962. {
  963. struct tm6000_core *dev = container_of(ctrl->handler, struct tm6000_core, ctrl_handler);
  964. u8 val = ctrl->val;
  965. switch (ctrl->id) {
  966. case V4L2_CID_CONTRAST:
  967. tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
  968. return 0;
  969. case V4L2_CID_BRIGHTNESS:
  970. tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
  971. return 0;
  972. case V4L2_CID_SATURATION:
  973. tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
  974. return 0;
  975. case V4L2_CID_HUE:
  976. tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
  977. return 0;
  978. }
  979. return -EINVAL;
  980. }
  981. static const struct v4l2_ctrl_ops tm6000_ctrl_ops = {
  982. .s_ctrl = tm6000_s_ctrl,
  983. };
  984. static int tm6000_radio_s_ctrl(struct v4l2_ctrl *ctrl)
  985. {
  986. struct tm6000_core *dev = container_of(ctrl->handler,
  987. struct tm6000_core, radio_ctrl_handler);
  988. u8 val = ctrl->val;
  989. switch (ctrl->id) {
  990. case V4L2_CID_AUDIO_MUTE:
  991. dev->ctl_mute = val;
  992. tm6000_tvaudio_set_mute(dev, val);
  993. return 0;
  994. case V4L2_CID_AUDIO_VOLUME:
  995. dev->ctl_volume = val;
  996. tm6000_set_volume(dev, val);
  997. return 0;
  998. }
  999. return -EINVAL;
  1000. }
  1001. static const struct v4l2_ctrl_ops tm6000_radio_ctrl_ops = {
  1002. .s_ctrl = tm6000_radio_s_ctrl,
  1003. };
  1004. static int vidioc_g_tuner(struct file *file, void *priv,
  1005. struct v4l2_tuner *t)
  1006. {
  1007. struct tm6000_fh *fh = priv;
  1008. struct tm6000_core *dev = fh->dev;
  1009. if (UNSET == dev->tuner_type)
  1010. return -ENOTTY;
  1011. if (0 != t->index)
  1012. return -EINVAL;
  1013. strcpy(t->name, "Television");
  1014. t->type = V4L2_TUNER_ANALOG_TV;
  1015. t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
  1016. t->rangehigh = 0xffffffffUL;
  1017. t->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1018. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1019. t->audmode = dev->amode;
  1020. return 0;
  1021. }
  1022. static int vidioc_s_tuner(struct file *file, void *priv,
  1023. const struct v4l2_tuner *t)
  1024. {
  1025. struct tm6000_fh *fh = priv;
  1026. struct tm6000_core *dev = fh->dev;
  1027. if (UNSET == dev->tuner_type)
  1028. return -ENOTTY;
  1029. if (0 != t->index)
  1030. return -EINVAL;
  1031. if (t->audmode > V4L2_TUNER_MODE_STEREO)
  1032. dev->amode = V4L2_TUNER_MODE_STEREO;
  1033. else
  1034. dev->amode = t->audmode;
  1035. dprintk(dev, 3, "audio mode: %x\n", t->audmode);
  1036. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1037. return 0;
  1038. }
  1039. static int vidioc_g_frequency(struct file *file, void *priv,
  1040. struct v4l2_frequency *f)
  1041. {
  1042. struct tm6000_fh *fh = priv;
  1043. struct tm6000_core *dev = fh->dev;
  1044. if (UNSET == dev->tuner_type)
  1045. return -ENOTTY;
  1046. if (f->tuner)
  1047. return -EINVAL;
  1048. f->frequency = dev->freq;
  1049. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
  1050. return 0;
  1051. }
  1052. static int vidioc_s_frequency(struct file *file, void *priv,
  1053. const struct v4l2_frequency *f)
  1054. {
  1055. struct tm6000_fh *fh = priv;
  1056. struct tm6000_core *dev = fh->dev;
  1057. if (UNSET == dev->tuner_type)
  1058. return -ENOTTY;
  1059. if (f->tuner != 0)
  1060. return -EINVAL;
  1061. dev->freq = f->frequency;
  1062. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
  1063. return 0;
  1064. }
  1065. static int radio_g_tuner(struct file *file, void *priv,
  1066. struct v4l2_tuner *t)
  1067. {
  1068. struct tm6000_fh *fh = file->private_data;
  1069. struct tm6000_core *dev = fh->dev;
  1070. if (0 != t->index)
  1071. return -EINVAL;
  1072. memset(t, 0, sizeof(*t));
  1073. strcpy(t->name, "Radio");
  1074. t->type = V4L2_TUNER_RADIO;
  1075. t->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
  1076. t->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1077. t->audmode = V4L2_TUNER_MODE_STEREO;
  1078. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1079. return 0;
  1080. }
  1081. static int radio_s_tuner(struct file *file, void *priv,
  1082. const struct v4l2_tuner *t)
  1083. {
  1084. struct tm6000_fh *fh = file->private_data;
  1085. struct tm6000_core *dev = fh->dev;
  1086. if (0 != t->index)
  1087. return -EINVAL;
  1088. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1089. return 0;
  1090. }
  1091. /* ------------------------------------------------------------------
  1092. File operations for the device
  1093. ------------------------------------------------------------------*/
  1094. static int __tm6000_open(struct file *file)
  1095. {
  1096. struct video_device *vdev = video_devdata(file);
  1097. struct tm6000_core *dev = video_drvdata(file);
  1098. struct tm6000_fh *fh;
  1099. enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1100. int rc;
  1101. int radio = 0;
  1102. dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
  1103. video_device_node_name(vdev));
  1104. switch (vdev->vfl_type) {
  1105. case VFL_TYPE_GRABBER:
  1106. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1107. break;
  1108. case VFL_TYPE_VBI:
  1109. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1110. break;
  1111. case VFL_TYPE_RADIO:
  1112. radio = 1;
  1113. break;
  1114. }
  1115. /* If more than one user, mutex should be added */
  1116. dev->users++;
  1117. dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
  1118. video_device_node_name(vdev), v4l2_type_names[type],
  1119. dev->users);
  1120. /* allocate + initialize per filehandle data */
  1121. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  1122. if (NULL == fh) {
  1123. dev->users--;
  1124. return -ENOMEM;
  1125. }
  1126. v4l2_fh_init(&fh->fh, vdev);
  1127. file->private_data = fh;
  1128. fh->dev = dev;
  1129. fh->radio = radio;
  1130. dev->radio = radio;
  1131. fh->type = type;
  1132. dev->fourcc = format[0].fourcc;
  1133. fh->fmt = format_by_fourcc(dev->fourcc);
  1134. tm6000_get_std_res(dev);
  1135. fh->width = dev->width;
  1136. fh->height = dev->height;
  1137. dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
  1138. "dev->vidq=0x%08lx\n",
  1139. (unsigned long)fh, (unsigned long)dev,
  1140. (unsigned long)&dev->vidq);
  1141. dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
  1142. "queued=%d\n", list_empty(&dev->vidq.queued));
  1143. dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
  1144. "active=%d\n", list_empty(&dev->vidq.active));
  1145. /* initialize hardware on analog mode */
  1146. rc = tm6000_init_analog_mode(dev);
  1147. if (rc < 0)
  1148. return rc;
  1149. dev->mode = TM6000_MODE_ANALOG;
  1150. if (!fh->radio) {
  1151. videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
  1152. NULL, &dev->slock,
  1153. fh->type,
  1154. V4L2_FIELD_INTERLACED,
  1155. sizeof(struct tm6000_buffer), fh, &dev->lock);
  1156. } else {
  1157. dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
  1158. tm6000_set_audio_rinput(dev);
  1159. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
  1160. tm6000_prepare_isoc(dev);
  1161. tm6000_start_thread(dev);
  1162. }
  1163. v4l2_fh_add(&fh->fh);
  1164. return 0;
  1165. }
  1166. static int tm6000_open(struct file *file)
  1167. {
  1168. struct video_device *vdev = video_devdata(file);
  1169. int res;
  1170. mutex_lock(vdev->lock);
  1171. res = __tm6000_open(file);
  1172. mutex_unlock(vdev->lock);
  1173. return res;
  1174. }
  1175. static ssize_t
  1176. tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
  1177. {
  1178. struct tm6000_fh *fh = file->private_data;
  1179. struct tm6000_core *dev = fh->dev;
  1180. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1181. int res;
  1182. if (!res_get(fh->dev, fh, true))
  1183. return -EBUSY;
  1184. if (mutex_lock_interruptible(&dev->lock))
  1185. return -ERESTARTSYS;
  1186. res = videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
  1187. file->f_flags & O_NONBLOCK);
  1188. mutex_unlock(&dev->lock);
  1189. return res;
  1190. }
  1191. return 0;
  1192. }
  1193. static unsigned int
  1194. __tm6000_poll(struct file *file, struct poll_table_struct *wait)
  1195. {
  1196. unsigned long req_events = poll_requested_events(wait);
  1197. struct tm6000_fh *fh = file->private_data;
  1198. struct tm6000_buffer *buf;
  1199. int res = 0;
  1200. if (v4l2_event_pending(&fh->fh))
  1201. res = POLLPRI;
  1202. else if (req_events & POLLPRI)
  1203. poll_wait(file, &fh->fh.wait, wait);
  1204. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  1205. return res | POLLERR;
  1206. if (!!is_res_streaming(fh->dev, fh))
  1207. return res | POLLERR;
  1208. if (!is_res_read(fh->dev, fh)) {
  1209. /* streaming capture */
  1210. if (list_empty(&fh->vb_vidq.stream))
  1211. return res | POLLERR;
  1212. buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream);
  1213. poll_wait(file, &buf->vb.done, wait);
  1214. if (buf->vb.state == VIDEOBUF_DONE ||
  1215. buf->vb.state == VIDEOBUF_ERROR)
  1216. return res | POLLIN | POLLRDNORM;
  1217. } else if (req_events & (POLLIN | POLLRDNORM)) {
  1218. /* read() capture */
  1219. return res | videobuf_poll_stream(file, &fh->vb_vidq, wait);
  1220. }
  1221. return res;
  1222. }
  1223. static unsigned int tm6000_poll(struct file *file, struct poll_table_struct *wait)
  1224. {
  1225. struct tm6000_fh *fh = file->private_data;
  1226. struct tm6000_core *dev = fh->dev;
  1227. unsigned int res;
  1228. mutex_lock(&dev->lock);
  1229. res = __tm6000_poll(file, wait);
  1230. mutex_unlock(&dev->lock);
  1231. return res;
  1232. }
  1233. static int tm6000_release(struct file *file)
  1234. {
  1235. struct tm6000_fh *fh = file->private_data;
  1236. struct tm6000_core *dev = fh->dev;
  1237. struct video_device *vdev = video_devdata(file);
  1238. dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
  1239. video_device_node_name(vdev), dev->users);
  1240. mutex_lock(&dev->lock);
  1241. dev->users--;
  1242. res_free(dev, fh);
  1243. if (!dev->users) {
  1244. tm6000_uninit_isoc(dev);
  1245. /* Stop interrupt USB pipe */
  1246. tm6000_ir_int_stop(dev);
  1247. usb_reset_configuration(dev->udev);
  1248. if (dev->int_in.endp)
  1249. usb_set_interface(dev->udev,
  1250. dev->isoc_in.bInterfaceNumber, 2);
  1251. else
  1252. usb_set_interface(dev->udev,
  1253. dev->isoc_in.bInterfaceNumber, 0);
  1254. /* Start interrupt USB pipe */
  1255. tm6000_ir_int_start(dev);
  1256. if (!fh->radio)
  1257. videobuf_mmap_free(&fh->vb_vidq);
  1258. }
  1259. v4l2_fh_del(&fh->fh);
  1260. v4l2_fh_exit(&fh->fh);
  1261. kfree(fh);
  1262. mutex_unlock(&dev->lock);
  1263. return 0;
  1264. }
  1265. static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
  1266. {
  1267. struct tm6000_fh *fh = file->private_data;
  1268. struct tm6000_core *dev = fh->dev;
  1269. int res;
  1270. if (mutex_lock_interruptible(&dev->lock))
  1271. return -ERESTARTSYS;
  1272. res = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1273. mutex_unlock(&dev->lock);
  1274. return res;
  1275. }
  1276. static struct v4l2_file_operations tm6000_fops = {
  1277. .owner = THIS_MODULE,
  1278. .open = tm6000_open,
  1279. .release = tm6000_release,
  1280. .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
  1281. .read = tm6000_read,
  1282. .poll = tm6000_poll,
  1283. .mmap = tm6000_mmap,
  1284. };
  1285. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1286. .vidioc_querycap = vidioc_querycap,
  1287. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1288. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1289. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1290. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1291. .vidioc_s_std = vidioc_s_std,
  1292. .vidioc_g_std = vidioc_g_std,
  1293. .vidioc_enum_input = vidioc_enum_input,
  1294. .vidioc_g_input = vidioc_g_input,
  1295. .vidioc_s_input = vidioc_s_input,
  1296. .vidioc_g_tuner = vidioc_g_tuner,
  1297. .vidioc_s_tuner = vidioc_s_tuner,
  1298. .vidioc_g_frequency = vidioc_g_frequency,
  1299. .vidioc_s_frequency = vidioc_s_frequency,
  1300. .vidioc_streamon = vidioc_streamon,
  1301. .vidioc_streamoff = vidioc_streamoff,
  1302. .vidioc_reqbufs = vidioc_reqbufs,
  1303. .vidioc_querybuf = vidioc_querybuf,
  1304. .vidioc_qbuf = vidioc_qbuf,
  1305. .vidioc_dqbuf = vidioc_dqbuf,
  1306. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1307. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1308. };
  1309. static struct video_device tm6000_template = {
  1310. .name = "tm6000",
  1311. .fops = &tm6000_fops,
  1312. .ioctl_ops = &video_ioctl_ops,
  1313. .release = video_device_release,
  1314. .tvnorms = TM6000_STD,
  1315. };
  1316. static const struct v4l2_file_operations radio_fops = {
  1317. .owner = THIS_MODULE,
  1318. .open = tm6000_open,
  1319. .poll = v4l2_ctrl_poll,
  1320. .release = tm6000_release,
  1321. .unlocked_ioctl = video_ioctl2,
  1322. };
  1323. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1324. .vidioc_querycap = vidioc_querycap,
  1325. .vidioc_g_tuner = radio_g_tuner,
  1326. .vidioc_s_tuner = radio_s_tuner,
  1327. .vidioc_g_frequency = vidioc_g_frequency,
  1328. .vidioc_s_frequency = vidioc_s_frequency,
  1329. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1330. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1331. };
  1332. static struct video_device tm6000_radio_template = {
  1333. .name = "tm6000",
  1334. .fops = &radio_fops,
  1335. .ioctl_ops = &radio_ioctl_ops,
  1336. };
  1337. /* -----------------------------------------------------------------
  1338. * Initialization and module stuff
  1339. * ------------------------------------------------------------------
  1340. */
  1341. static struct video_device *vdev_init(struct tm6000_core *dev,
  1342. const struct video_device
  1343. *template, const char *type_name)
  1344. {
  1345. struct video_device *vfd;
  1346. vfd = video_device_alloc();
  1347. if (NULL == vfd)
  1348. return NULL;
  1349. *vfd = *template;
  1350. vfd->v4l2_dev = &dev->v4l2_dev;
  1351. vfd->release = video_device_release;
  1352. vfd->debug = tm6000_debug;
  1353. vfd->lock = &dev->lock;
  1354. set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  1355. snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
  1356. video_set_drvdata(vfd, dev);
  1357. return vfd;
  1358. }
  1359. int tm6000_v4l2_register(struct tm6000_core *dev)
  1360. {
  1361. int ret = 0;
  1362. v4l2_ctrl_handler_init(&dev->ctrl_handler, 6);
  1363. v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 2);
  1364. v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
  1365. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
  1366. v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
  1367. V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
  1368. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1369. V4L2_CID_BRIGHTNESS, 0, 255, 1, 54);
  1370. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1371. V4L2_CID_CONTRAST, 0, 255, 1, 119);
  1372. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1373. V4L2_CID_SATURATION, 0, 255, 1, 112);
  1374. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1375. V4L2_CID_HUE, -128, 127, 1, 0);
  1376. v4l2_ctrl_add_handler(&dev->ctrl_handler,
  1377. &dev->radio_ctrl_handler, NULL);
  1378. if (dev->radio_ctrl_handler.error)
  1379. ret = dev->radio_ctrl_handler.error;
  1380. if (!ret && dev->ctrl_handler.error)
  1381. ret = dev->ctrl_handler.error;
  1382. if (ret)
  1383. goto free_ctrl;
  1384. dev->vfd = vdev_init(dev, &tm6000_template, "video");
  1385. if (!dev->vfd) {
  1386. printk(KERN_INFO "%s: can't register video device\n",
  1387. dev->name);
  1388. ret = -ENOMEM;
  1389. goto free_ctrl;
  1390. }
  1391. dev->vfd->ctrl_handler = &dev->ctrl_handler;
  1392. /* init video dma queues */
  1393. INIT_LIST_HEAD(&dev->vidq.active);
  1394. INIT_LIST_HEAD(&dev->vidq.queued);
  1395. ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
  1396. if (ret < 0) {
  1397. printk(KERN_INFO "%s: can't register video device\n",
  1398. dev->name);
  1399. video_device_release(dev->vfd);
  1400. dev->vfd = NULL;
  1401. goto free_ctrl;
  1402. }
  1403. printk(KERN_INFO "%s: registered device %s\n",
  1404. dev->name, video_device_node_name(dev->vfd));
  1405. if (dev->caps.has_radio) {
  1406. dev->radio_dev = vdev_init(dev, &tm6000_radio_template,
  1407. "radio");
  1408. if (!dev->radio_dev) {
  1409. printk(KERN_INFO "%s: can't register radio device\n",
  1410. dev->name);
  1411. ret = -ENXIO;
  1412. goto unreg_video;
  1413. }
  1414. dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler;
  1415. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1416. radio_nr);
  1417. if (ret < 0) {
  1418. printk(KERN_INFO "%s: can't register radio device\n",
  1419. dev->name);
  1420. video_device_release(dev->radio_dev);
  1421. goto unreg_video;
  1422. }
  1423. printk(KERN_INFO "%s: registered device %s\n",
  1424. dev->name, video_device_node_name(dev->radio_dev));
  1425. }
  1426. printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
  1427. return ret;
  1428. unreg_video:
  1429. video_unregister_device(dev->vfd);
  1430. free_ctrl:
  1431. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  1432. v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
  1433. return ret;
  1434. }
  1435. int tm6000_v4l2_unregister(struct tm6000_core *dev)
  1436. {
  1437. video_unregister_device(dev->vfd);
  1438. /* if URB buffers are still allocated free them now */
  1439. tm6000_free_urb_buffers(dev);
  1440. if (dev->radio_dev) {
  1441. if (video_is_registered(dev->radio_dev))
  1442. video_unregister_device(dev->radio_dev);
  1443. else
  1444. video_device_release(dev->radio_dev);
  1445. dev->radio_dev = NULL;
  1446. }
  1447. return 0;
  1448. }
  1449. int tm6000_v4l2_exit(void)
  1450. {
  1451. return 0;
  1452. }
  1453. module_param(video_nr, int, 0);
  1454. MODULE_PARM_DESC(video_nr, "Allow changing video device number");
  1455. module_param_named(debug, tm6000_debug, int, 0444);
  1456. MODULE_PARM_DESC(debug, "activates debug info");
  1457. module_param(vid_limit, int, 0644);
  1458. MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  1459. module_param(keep_urb, bool, 0);
  1460. MODULE_PARM_DESC(keep_urb, "Keep urb buffers allocated even when the device is closed by the user");