tm6000-video.c 44 KB

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