tm6000-video.c 41 KB

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