tm6000-video.c 41 KB

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