tm6000-video.c 43 KB

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