bfin_capture.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. /*
  2. * Analog Devices video capture driver
  3. *
  4. * Copyright (c) 2011 Analog Devices Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/completion.h>
  20. #include <linux/delay.h>
  21. #include <linux/errno.h>
  22. #include <linux/fs.h>
  23. #include <linux/i2c.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/io.h>
  27. #include <linux/mm.h>
  28. #include <linux/module.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/slab.h>
  31. #include <linux/time.h>
  32. #include <linux/types.h>
  33. #include <media/v4l2-chip-ident.h>
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-ctrls.h>
  36. #include <media/v4l2-device.h>
  37. #include <media/v4l2-ioctl.h>
  38. #include <media/videobuf2-dma-contig.h>
  39. #include <asm/dma.h>
  40. #include <media/blackfin/bfin_capture.h>
  41. #include <media/blackfin/ppi.h>
  42. #define CAPTURE_DRV_NAME "bfin_capture"
  43. #define BCAP_MIN_NUM_BUF 2
  44. struct bcap_format {
  45. char *desc;
  46. u32 pixelformat;
  47. enum v4l2_mbus_pixelcode mbus_code;
  48. int bpp; /* bits per pixel */
  49. int dlen; /* data length for ppi in bits */
  50. };
  51. struct bcap_buffer {
  52. struct vb2_buffer vb;
  53. struct list_head list;
  54. };
  55. struct bcap_device {
  56. /* capture device instance */
  57. struct v4l2_device v4l2_dev;
  58. /* v4l2 control handler */
  59. struct v4l2_ctrl_handler ctrl_handler;
  60. /* device node data */
  61. struct video_device *video_dev;
  62. /* sub device instance */
  63. struct v4l2_subdev *sd;
  64. /* capture config */
  65. struct bfin_capture_config *cfg;
  66. /* ppi interface */
  67. struct ppi_if *ppi;
  68. /* current input */
  69. unsigned int cur_input;
  70. /* current selected standard */
  71. v4l2_std_id std;
  72. /* current selected dv_timings */
  73. struct v4l2_dv_timings dv_timings;
  74. /* used to store pixel format */
  75. struct v4l2_pix_format fmt;
  76. /* bits per pixel*/
  77. int bpp;
  78. /* data length for ppi in bits */
  79. int dlen;
  80. /* used to store sensor supported format */
  81. struct bcap_format *sensor_formats;
  82. /* number of sensor formats array */
  83. int num_sensor_formats;
  84. /* pointing to current video buffer */
  85. struct bcap_buffer *cur_frm;
  86. /* pointing to next video buffer */
  87. struct bcap_buffer *next_frm;
  88. /* buffer queue used in videobuf2 */
  89. struct vb2_queue buffer_queue;
  90. /* allocator-specific contexts for each plane */
  91. struct vb2_alloc_ctx *alloc_ctx;
  92. /* queue of filled frames */
  93. struct list_head dma_queue;
  94. /* used in videobuf2 callback */
  95. spinlock_t lock;
  96. /* used to access capture device */
  97. struct mutex mutex;
  98. /* used to wait ppi to complete one transfer */
  99. struct completion comp;
  100. /* prepare to stop */
  101. bool stop;
  102. };
  103. struct bcap_fh {
  104. struct v4l2_fh fh;
  105. /* indicates whether this file handle is doing IO */
  106. bool io_allowed;
  107. };
  108. static const struct bcap_format bcap_formats[] = {
  109. {
  110. .desc = "YCbCr 4:2:2 Interleaved UYVY",
  111. .pixelformat = V4L2_PIX_FMT_UYVY,
  112. .mbus_code = V4L2_MBUS_FMT_UYVY8_2X8,
  113. .bpp = 16,
  114. .dlen = 8,
  115. },
  116. {
  117. .desc = "YCbCr 4:2:2 Interleaved YUYV",
  118. .pixelformat = V4L2_PIX_FMT_YUYV,
  119. .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
  120. .bpp = 16,
  121. .dlen = 8,
  122. },
  123. {
  124. .desc = "YCbCr 4:2:2 Interleaved UYVY",
  125. .pixelformat = V4L2_PIX_FMT_UYVY,
  126. .mbus_code = V4L2_MBUS_FMT_UYVY8_1X16,
  127. .bpp = 16,
  128. .dlen = 16,
  129. },
  130. {
  131. .desc = "RGB 565",
  132. .pixelformat = V4L2_PIX_FMT_RGB565,
  133. .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE,
  134. .bpp = 16,
  135. .dlen = 8,
  136. },
  137. {
  138. .desc = "RGB 444",
  139. .pixelformat = V4L2_PIX_FMT_RGB444,
  140. .mbus_code = V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE,
  141. .bpp = 16,
  142. .dlen = 8,
  143. },
  144. };
  145. #define BCAP_MAX_FMTS ARRAY_SIZE(bcap_formats)
  146. static irqreturn_t bcap_isr(int irq, void *dev_id);
  147. static struct bcap_buffer *to_bcap_vb(struct vb2_buffer *vb)
  148. {
  149. return container_of(vb, struct bcap_buffer, vb);
  150. }
  151. static int bcap_init_sensor_formats(struct bcap_device *bcap_dev)
  152. {
  153. enum v4l2_mbus_pixelcode code;
  154. struct bcap_format *sf;
  155. unsigned int num_formats = 0;
  156. int i, j;
  157. while (!v4l2_subdev_call(bcap_dev->sd, video,
  158. enum_mbus_fmt, num_formats, &code))
  159. num_formats++;
  160. if (!num_formats)
  161. return -ENXIO;
  162. sf = kzalloc(num_formats * sizeof(*sf), GFP_KERNEL);
  163. if (!sf)
  164. return -ENOMEM;
  165. for (i = 0; i < num_formats; i++) {
  166. v4l2_subdev_call(bcap_dev->sd, video,
  167. enum_mbus_fmt, i, &code);
  168. for (j = 0; j < BCAP_MAX_FMTS; j++)
  169. if (code == bcap_formats[j].mbus_code)
  170. break;
  171. if (j == BCAP_MAX_FMTS) {
  172. /* we don't allow this sensor working with our bridge */
  173. kfree(sf);
  174. return -EINVAL;
  175. }
  176. sf[i] = bcap_formats[j];
  177. }
  178. bcap_dev->sensor_formats = sf;
  179. bcap_dev->num_sensor_formats = num_formats;
  180. return 0;
  181. }
  182. static void bcap_free_sensor_formats(struct bcap_device *bcap_dev)
  183. {
  184. bcap_dev->num_sensor_formats = 0;
  185. kfree(bcap_dev->sensor_formats);
  186. bcap_dev->sensor_formats = NULL;
  187. }
  188. static int bcap_open(struct file *file)
  189. {
  190. struct bcap_device *bcap_dev = video_drvdata(file);
  191. struct video_device *vfd = bcap_dev->video_dev;
  192. struct bcap_fh *bcap_fh;
  193. if (!bcap_dev->sd) {
  194. v4l2_err(&bcap_dev->v4l2_dev, "No sub device registered\n");
  195. return -ENODEV;
  196. }
  197. bcap_fh = kzalloc(sizeof(*bcap_fh), GFP_KERNEL);
  198. if (!bcap_fh) {
  199. v4l2_err(&bcap_dev->v4l2_dev,
  200. "unable to allocate memory for file handle object\n");
  201. return -ENOMEM;
  202. }
  203. v4l2_fh_init(&bcap_fh->fh, vfd);
  204. /* store pointer to v4l2_fh in private_data member of file */
  205. file->private_data = &bcap_fh->fh;
  206. v4l2_fh_add(&bcap_fh->fh);
  207. bcap_fh->io_allowed = false;
  208. return 0;
  209. }
  210. static int bcap_release(struct file *file)
  211. {
  212. struct bcap_device *bcap_dev = video_drvdata(file);
  213. struct v4l2_fh *fh = file->private_data;
  214. struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
  215. /* if this instance is doing IO */
  216. if (bcap_fh->io_allowed)
  217. vb2_queue_release(&bcap_dev->buffer_queue);
  218. file->private_data = NULL;
  219. v4l2_fh_del(&bcap_fh->fh);
  220. v4l2_fh_exit(&bcap_fh->fh);
  221. kfree(bcap_fh);
  222. return 0;
  223. }
  224. static int bcap_mmap(struct file *file, struct vm_area_struct *vma)
  225. {
  226. struct bcap_device *bcap_dev = video_drvdata(file);
  227. int ret;
  228. if (mutex_lock_interruptible(&bcap_dev->mutex))
  229. return -ERESTARTSYS;
  230. ret = vb2_mmap(&bcap_dev->buffer_queue, vma);
  231. mutex_unlock(&bcap_dev->mutex);
  232. return ret;
  233. }
  234. #ifndef CONFIG_MMU
  235. static unsigned long bcap_get_unmapped_area(struct file *file,
  236. unsigned long addr,
  237. unsigned long len,
  238. unsigned long pgoff,
  239. unsigned long flags)
  240. {
  241. struct bcap_device *bcap_dev = video_drvdata(file);
  242. return vb2_get_unmapped_area(&bcap_dev->buffer_queue,
  243. addr,
  244. len,
  245. pgoff,
  246. flags);
  247. }
  248. #endif
  249. static unsigned int bcap_poll(struct file *file, poll_table *wait)
  250. {
  251. struct bcap_device *bcap_dev = video_drvdata(file);
  252. unsigned int res;
  253. mutex_lock(&bcap_dev->mutex);
  254. res = vb2_poll(&bcap_dev->buffer_queue, file, wait);
  255. mutex_unlock(&bcap_dev->mutex);
  256. return res;
  257. }
  258. static int bcap_queue_setup(struct vb2_queue *vq,
  259. const struct v4l2_format *fmt,
  260. unsigned int *nbuffers, unsigned int *nplanes,
  261. unsigned int sizes[], void *alloc_ctxs[])
  262. {
  263. struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
  264. if (*nbuffers < BCAP_MIN_NUM_BUF)
  265. *nbuffers = BCAP_MIN_NUM_BUF;
  266. *nplanes = 1;
  267. sizes[0] = bcap_dev->fmt.sizeimage;
  268. alloc_ctxs[0] = bcap_dev->alloc_ctx;
  269. return 0;
  270. }
  271. static int bcap_buffer_init(struct vb2_buffer *vb)
  272. {
  273. struct bcap_buffer *buf = to_bcap_vb(vb);
  274. INIT_LIST_HEAD(&buf->list);
  275. return 0;
  276. }
  277. static int bcap_buffer_prepare(struct vb2_buffer *vb)
  278. {
  279. struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
  280. struct bcap_buffer *buf = to_bcap_vb(vb);
  281. unsigned long size;
  282. size = bcap_dev->fmt.sizeimage;
  283. if (vb2_plane_size(vb, 0) < size) {
  284. v4l2_err(&bcap_dev->v4l2_dev, "buffer too small (%lu < %lu)\n",
  285. vb2_plane_size(vb, 0), size);
  286. return -EINVAL;
  287. }
  288. vb2_set_plane_payload(&buf->vb, 0, size);
  289. return 0;
  290. }
  291. static void bcap_buffer_queue(struct vb2_buffer *vb)
  292. {
  293. struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
  294. struct bcap_buffer *buf = to_bcap_vb(vb);
  295. unsigned long flags;
  296. spin_lock_irqsave(&bcap_dev->lock, flags);
  297. list_add_tail(&buf->list, &bcap_dev->dma_queue);
  298. spin_unlock_irqrestore(&bcap_dev->lock, flags);
  299. }
  300. static void bcap_buffer_cleanup(struct vb2_buffer *vb)
  301. {
  302. struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
  303. struct bcap_buffer *buf = to_bcap_vb(vb);
  304. unsigned long flags;
  305. spin_lock_irqsave(&bcap_dev->lock, flags);
  306. list_del_init(&buf->list);
  307. spin_unlock_irqrestore(&bcap_dev->lock, flags);
  308. }
  309. static void bcap_lock(struct vb2_queue *vq)
  310. {
  311. struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
  312. mutex_lock(&bcap_dev->mutex);
  313. }
  314. static void bcap_unlock(struct vb2_queue *vq)
  315. {
  316. struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
  317. mutex_unlock(&bcap_dev->mutex);
  318. }
  319. static int bcap_start_streaming(struct vb2_queue *vq, unsigned int count)
  320. {
  321. struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
  322. struct ppi_if *ppi = bcap_dev->ppi;
  323. struct ppi_params params;
  324. int ret;
  325. /* enable streamon on the sub device */
  326. ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 1);
  327. if (ret && (ret != -ENOIOCTLCMD)) {
  328. v4l2_err(&bcap_dev->v4l2_dev, "stream on failed in subdev\n");
  329. return ret;
  330. }
  331. /* set ppi params */
  332. params.width = bcap_dev->fmt.width;
  333. params.height = bcap_dev->fmt.height;
  334. params.bpp = bcap_dev->bpp;
  335. params.dlen = bcap_dev->dlen;
  336. params.ppi_control = bcap_dev->cfg->ppi_control;
  337. params.int_mask = bcap_dev->cfg->int_mask;
  338. if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
  339. & V4L2_IN_CAP_CUSTOM_TIMINGS) {
  340. struct v4l2_bt_timings *bt = &bcap_dev->dv_timings.bt;
  341. params.hdelay = bt->hsync + bt->hbackporch;
  342. params.vdelay = bt->vsync + bt->vbackporch;
  343. params.line = bt->hfrontporch + bt->hsync
  344. + bt->hbackporch + bt->width;
  345. params.frame = bt->vfrontporch + bt->vsync
  346. + bt->vbackporch + bt->height;
  347. if (bt->interlaced)
  348. params.frame += bt->il_vfrontporch + bt->il_vsync
  349. + bt->il_vbackporch;
  350. } else if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
  351. & V4L2_IN_CAP_STD) {
  352. params.hdelay = 0;
  353. params.vdelay = 0;
  354. if (bcap_dev->std & V4L2_STD_525_60) {
  355. params.line = 858;
  356. params.frame = 525;
  357. } else {
  358. params.line = 864;
  359. params.frame = 625;
  360. }
  361. } else {
  362. params.hdelay = 0;
  363. params.vdelay = 0;
  364. params.line = params.width + bcap_dev->cfg->blank_pixels;
  365. params.frame = params.height;
  366. }
  367. ret = ppi->ops->set_params(ppi, &params);
  368. if (ret < 0) {
  369. v4l2_err(&bcap_dev->v4l2_dev,
  370. "Error in setting ppi params\n");
  371. return ret;
  372. }
  373. /* attach ppi DMA irq handler */
  374. ret = ppi->ops->attach_irq(ppi, bcap_isr);
  375. if (ret < 0) {
  376. v4l2_err(&bcap_dev->v4l2_dev,
  377. "Error in attaching interrupt handler\n");
  378. return ret;
  379. }
  380. INIT_COMPLETION(bcap_dev->comp);
  381. bcap_dev->stop = false;
  382. return 0;
  383. }
  384. static int bcap_stop_streaming(struct vb2_queue *vq)
  385. {
  386. struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
  387. struct ppi_if *ppi = bcap_dev->ppi;
  388. int ret;
  389. if (!vb2_is_streaming(vq))
  390. return 0;
  391. bcap_dev->stop = true;
  392. wait_for_completion(&bcap_dev->comp);
  393. ppi->ops->stop(ppi);
  394. ppi->ops->detach_irq(ppi);
  395. ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 0);
  396. if (ret && (ret != -ENOIOCTLCMD))
  397. v4l2_err(&bcap_dev->v4l2_dev,
  398. "stream off failed in subdev\n");
  399. /* release all active buffers */
  400. while (!list_empty(&bcap_dev->dma_queue)) {
  401. bcap_dev->next_frm = list_entry(bcap_dev->dma_queue.next,
  402. struct bcap_buffer, list);
  403. list_del(&bcap_dev->next_frm->list);
  404. vb2_buffer_done(&bcap_dev->next_frm->vb, VB2_BUF_STATE_ERROR);
  405. }
  406. return 0;
  407. }
  408. static struct vb2_ops bcap_video_qops = {
  409. .queue_setup = bcap_queue_setup,
  410. .buf_init = bcap_buffer_init,
  411. .buf_prepare = bcap_buffer_prepare,
  412. .buf_cleanup = bcap_buffer_cleanup,
  413. .buf_queue = bcap_buffer_queue,
  414. .wait_prepare = bcap_unlock,
  415. .wait_finish = bcap_lock,
  416. .start_streaming = bcap_start_streaming,
  417. .stop_streaming = bcap_stop_streaming,
  418. };
  419. static int bcap_reqbufs(struct file *file, void *priv,
  420. struct v4l2_requestbuffers *req_buf)
  421. {
  422. struct bcap_device *bcap_dev = video_drvdata(file);
  423. struct vb2_queue *vq = &bcap_dev->buffer_queue;
  424. struct v4l2_fh *fh = file->private_data;
  425. struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
  426. if (vb2_is_busy(vq))
  427. return -EBUSY;
  428. bcap_fh->io_allowed = true;
  429. return vb2_reqbufs(vq, req_buf);
  430. }
  431. static int bcap_querybuf(struct file *file, void *priv,
  432. struct v4l2_buffer *buf)
  433. {
  434. struct bcap_device *bcap_dev = video_drvdata(file);
  435. return vb2_querybuf(&bcap_dev->buffer_queue, buf);
  436. }
  437. static int bcap_qbuf(struct file *file, void *priv,
  438. struct v4l2_buffer *buf)
  439. {
  440. struct bcap_device *bcap_dev = video_drvdata(file);
  441. struct v4l2_fh *fh = file->private_data;
  442. struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
  443. if (!bcap_fh->io_allowed)
  444. return -EBUSY;
  445. return vb2_qbuf(&bcap_dev->buffer_queue, buf);
  446. }
  447. static int bcap_dqbuf(struct file *file, void *priv,
  448. struct v4l2_buffer *buf)
  449. {
  450. struct bcap_device *bcap_dev = video_drvdata(file);
  451. struct v4l2_fh *fh = file->private_data;
  452. struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
  453. if (!bcap_fh->io_allowed)
  454. return -EBUSY;
  455. return vb2_dqbuf(&bcap_dev->buffer_queue,
  456. buf, file->f_flags & O_NONBLOCK);
  457. }
  458. static irqreturn_t bcap_isr(int irq, void *dev_id)
  459. {
  460. struct ppi_if *ppi = dev_id;
  461. struct bcap_device *bcap_dev = ppi->priv;
  462. struct vb2_buffer *vb = &bcap_dev->cur_frm->vb;
  463. dma_addr_t addr;
  464. spin_lock(&bcap_dev->lock);
  465. if (bcap_dev->cur_frm != bcap_dev->next_frm) {
  466. v4l2_get_timestamp(&vb->v4l2_buf.timestamp);
  467. vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
  468. bcap_dev->cur_frm = bcap_dev->next_frm;
  469. }
  470. ppi->ops->stop(ppi);
  471. if (bcap_dev->stop) {
  472. complete(&bcap_dev->comp);
  473. } else {
  474. if (!list_empty(&bcap_dev->dma_queue)) {
  475. bcap_dev->next_frm = list_entry(bcap_dev->dma_queue.next,
  476. struct bcap_buffer, list);
  477. list_del(&bcap_dev->next_frm->list);
  478. addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->next_frm->vb, 0);
  479. ppi->ops->update_addr(ppi, (unsigned long)addr);
  480. }
  481. ppi->ops->start(ppi);
  482. }
  483. spin_unlock(&bcap_dev->lock);
  484. return IRQ_HANDLED;
  485. }
  486. static int bcap_streamon(struct file *file, void *priv,
  487. enum v4l2_buf_type buf_type)
  488. {
  489. struct bcap_device *bcap_dev = video_drvdata(file);
  490. struct bcap_fh *fh = file->private_data;
  491. struct ppi_if *ppi = bcap_dev->ppi;
  492. dma_addr_t addr;
  493. int ret;
  494. if (!fh->io_allowed)
  495. return -EBUSY;
  496. /* call streamon to start streaming in videobuf */
  497. ret = vb2_streamon(&bcap_dev->buffer_queue, buf_type);
  498. if (ret)
  499. return ret;
  500. /* if dma queue is empty, return error */
  501. if (list_empty(&bcap_dev->dma_queue)) {
  502. v4l2_err(&bcap_dev->v4l2_dev, "dma queue is empty\n");
  503. ret = -EINVAL;
  504. goto err;
  505. }
  506. /* get the next frame from the dma queue */
  507. bcap_dev->next_frm = list_entry(bcap_dev->dma_queue.next,
  508. struct bcap_buffer, list);
  509. bcap_dev->cur_frm = bcap_dev->next_frm;
  510. /* remove buffer from the dma queue */
  511. list_del(&bcap_dev->cur_frm->list);
  512. addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0);
  513. /* update DMA address */
  514. ppi->ops->update_addr(ppi, (unsigned long)addr);
  515. /* enable ppi */
  516. ppi->ops->start(ppi);
  517. return 0;
  518. err:
  519. vb2_streamoff(&bcap_dev->buffer_queue, buf_type);
  520. return ret;
  521. }
  522. static int bcap_streamoff(struct file *file, void *priv,
  523. enum v4l2_buf_type buf_type)
  524. {
  525. struct bcap_device *bcap_dev = video_drvdata(file);
  526. struct bcap_fh *fh = file->private_data;
  527. if (!fh->io_allowed)
  528. return -EBUSY;
  529. return vb2_streamoff(&bcap_dev->buffer_queue, buf_type);
  530. }
  531. static int bcap_querystd(struct file *file, void *priv, v4l2_std_id *std)
  532. {
  533. struct bcap_device *bcap_dev = video_drvdata(file);
  534. return v4l2_subdev_call(bcap_dev->sd, video, querystd, std);
  535. }
  536. static int bcap_g_std(struct file *file, void *priv, v4l2_std_id *std)
  537. {
  538. struct bcap_device *bcap_dev = video_drvdata(file);
  539. *std = bcap_dev->std;
  540. return 0;
  541. }
  542. static int bcap_s_std(struct file *file, void *priv, v4l2_std_id *std)
  543. {
  544. struct bcap_device *bcap_dev = video_drvdata(file);
  545. int ret;
  546. if (vb2_is_busy(&bcap_dev->buffer_queue))
  547. return -EBUSY;
  548. ret = v4l2_subdev_call(bcap_dev->sd, core, s_std, *std);
  549. if (ret < 0)
  550. return ret;
  551. bcap_dev->std = *std;
  552. return 0;
  553. }
  554. static int bcap_g_dv_timings(struct file *file, void *priv,
  555. struct v4l2_dv_timings *timings)
  556. {
  557. struct bcap_device *bcap_dev = video_drvdata(file);
  558. int ret;
  559. ret = v4l2_subdev_call(bcap_dev->sd, video,
  560. g_dv_timings, timings);
  561. if (ret < 0)
  562. return ret;
  563. bcap_dev->dv_timings = *timings;
  564. return 0;
  565. }
  566. static int bcap_s_dv_timings(struct file *file, void *priv,
  567. struct v4l2_dv_timings *timings)
  568. {
  569. struct bcap_device *bcap_dev = video_drvdata(file);
  570. int ret;
  571. if (vb2_is_busy(&bcap_dev->buffer_queue))
  572. return -EBUSY;
  573. ret = v4l2_subdev_call(bcap_dev->sd, video, s_dv_timings, timings);
  574. if (ret < 0)
  575. return ret;
  576. bcap_dev->dv_timings = *timings;
  577. return 0;
  578. }
  579. static int bcap_enum_input(struct file *file, void *priv,
  580. struct v4l2_input *input)
  581. {
  582. struct bcap_device *bcap_dev = video_drvdata(file);
  583. struct bfin_capture_config *config = bcap_dev->cfg;
  584. int ret;
  585. u32 status;
  586. if (input->index >= config->num_inputs)
  587. return -EINVAL;
  588. *input = config->inputs[input->index];
  589. /* get input status */
  590. ret = v4l2_subdev_call(bcap_dev->sd, video, g_input_status, &status);
  591. if (!ret)
  592. input->status = status;
  593. return 0;
  594. }
  595. static int bcap_g_input(struct file *file, void *priv, unsigned int *index)
  596. {
  597. struct bcap_device *bcap_dev = video_drvdata(file);
  598. *index = bcap_dev->cur_input;
  599. return 0;
  600. }
  601. static int bcap_s_input(struct file *file, void *priv, unsigned int index)
  602. {
  603. struct bcap_device *bcap_dev = video_drvdata(file);
  604. struct bfin_capture_config *config = bcap_dev->cfg;
  605. struct bcap_route *route;
  606. int ret;
  607. if (vb2_is_busy(&bcap_dev->buffer_queue))
  608. return -EBUSY;
  609. if (index >= config->num_inputs)
  610. return -EINVAL;
  611. route = &config->routes[index];
  612. ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
  613. route->input, route->output, 0);
  614. if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
  615. v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
  616. return ret;
  617. }
  618. bcap_dev->cur_input = index;
  619. /* if this route has specific config, update ppi control */
  620. if (route->ppi_control)
  621. config->ppi_control = route->ppi_control;
  622. return 0;
  623. }
  624. static int bcap_try_format(struct bcap_device *bcap,
  625. struct v4l2_pix_format *pixfmt,
  626. struct bcap_format *bcap_fmt)
  627. {
  628. struct bcap_format *sf = bcap->sensor_formats;
  629. struct bcap_format *fmt = NULL;
  630. struct v4l2_mbus_framefmt mbus_fmt;
  631. int ret, i;
  632. for (i = 0; i < bcap->num_sensor_formats; i++) {
  633. fmt = &sf[i];
  634. if (pixfmt->pixelformat == fmt->pixelformat)
  635. break;
  636. }
  637. if (i == bcap->num_sensor_formats)
  638. fmt = &sf[0];
  639. v4l2_fill_mbus_format(&mbus_fmt, pixfmt, fmt->mbus_code);
  640. ret = v4l2_subdev_call(bcap->sd, video,
  641. try_mbus_fmt, &mbus_fmt);
  642. if (ret < 0)
  643. return ret;
  644. v4l2_fill_pix_format(pixfmt, &mbus_fmt);
  645. if (bcap_fmt) {
  646. for (i = 0; i < bcap->num_sensor_formats; i++) {
  647. fmt = &sf[i];
  648. if (mbus_fmt.code == fmt->mbus_code)
  649. break;
  650. }
  651. *bcap_fmt = *fmt;
  652. }
  653. pixfmt->bytesperline = pixfmt->width * fmt->bpp / 8;
  654. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
  655. return 0;
  656. }
  657. static int bcap_enum_fmt_vid_cap(struct file *file, void *priv,
  658. struct v4l2_fmtdesc *fmt)
  659. {
  660. struct bcap_device *bcap_dev = video_drvdata(file);
  661. struct bcap_format *sf = bcap_dev->sensor_formats;
  662. if (fmt->index >= bcap_dev->num_sensor_formats)
  663. return -EINVAL;
  664. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  665. strlcpy(fmt->description,
  666. sf[fmt->index].desc,
  667. sizeof(fmt->description));
  668. fmt->pixelformat = sf[fmt->index].pixelformat;
  669. return 0;
  670. }
  671. static int bcap_try_fmt_vid_cap(struct file *file, void *priv,
  672. struct v4l2_format *fmt)
  673. {
  674. struct bcap_device *bcap_dev = video_drvdata(file);
  675. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  676. return bcap_try_format(bcap_dev, pixfmt, NULL);
  677. }
  678. static int bcap_g_fmt_vid_cap(struct file *file, void *priv,
  679. struct v4l2_format *fmt)
  680. {
  681. struct bcap_device *bcap_dev = video_drvdata(file);
  682. fmt->fmt.pix = bcap_dev->fmt;
  683. return 0;
  684. }
  685. static int bcap_s_fmt_vid_cap(struct file *file, void *priv,
  686. struct v4l2_format *fmt)
  687. {
  688. struct bcap_device *bcap_dev = video_drvdata(file);
  689. struct v4l2_mbus_framefmt mbus_fmt;
  690. struct bcap_format bcap_fmt;
  691. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  692. int ret;
  693. if (vb2_is_busy(&bcap_dev->buffer_queue))
  694. return -EBUSY;
  695. /* see if format works */
  696. ret = bcap_try_format(bcap_dev, pixfmt, &bcap_fmt);
  697. if (ret < 0)
  698. return ret;
  699. v4l2_fill_mbus_format(&mbus_fmt, pixfmt, bcap_fmt.mbus_code);
  700. ret = v4l2_subdev_call(bcap_dev->sd, video, s_mbus_fmt, &mbus_fmt);
  701. if (ret < 0)
  702. return ret;
  703. bcap_dev->fmt = *pixfmt;
  704. bcap_dev->bpp = bcap_fmt.bpp;
  705. bcap_dev->dlen = bcap_fmt.dlen;
  706. return 0;
  707. }
  708. static int bcap_querycap(struct file *file, void *priv,
  709. struct v4l2_capability *cap)
  710. {
  711. struct bcap_device *bcap_dev = video_drvdata(file);
  712. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  713. strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
  714. strlcpy(cap->bus_info, "Blackfin Platform", sizeof(cap->bus_info));
  715. strlcpy(cap->card, bcap_dev->cfg->card_name, sizeof(cap->card));
  716. return 0;
  717. }
  718. static int bcap_g_parm(struct file *file, void *fh,
  719. struct v4l2_streamparm *a)
  720. {
  721. struct bcap_device *bcap_dev = video_drvdata(file);
  722. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  723. return -EINVAL;
  724. return v4l2_subdev_call(bcap_dev->sd, video, g_parm, a);
  725. }
  726. static int bcap_s_parm(struct file *file, void *fh,
  727. struct v4l2_streamparm *a)
  728. {
  729. struct bcap_device *bcap_dev = video_drvdata(file);
  730. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  731. return -EINVAL;
  732. return v4l2_subdev_call(bcap_dev->sd, video, s_parm, a);
  733. }
  734. static int bcap_g_chip_ident(struct file *file, void *priv,
  735. struct v4l2_dbg_chip_ident *chip)
  736. {
  737. struct bcap_device *bcap_dev = video_drvdata(file);
  738. chip->ident = V4L2_IDENT_NONE;
  739. chip->revision = 0;
  740. if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
  741. chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  742. return -EINVAL;
  743. return v4l2_subdev_call(bcap_dev->sd, core,
  744. g_chip_ident, chip);
  745. }
  746. #ifdef CONFIG_VIDEO_ADV_DEBUG
  747. static int bcap_dbg_g_register(struct file *file, void *priv,
  748. struct v4l2_dbg_register *reg)
  749. {
  750. struct bcap_device *bcap_dev = video_drvdata(file);
  751. return v4l2_subdev_call(bcap_dev->sd, core,
  752. g_register, reg);
  753. }
  754. static int bcap_dbg_s_register(struct file *file, void *priv,
  755. struct v4l2_dbg_register *reg)
  756. {
  757. struct bcap_device *bcap_dev = video_drvdata(file);
  758. return v4l2_subdev_call(bcap_dev->sd, core,
  759. s_register, reg);
  760. }
  761. #endif
  762. static int bcap_log_status(struct file *file, void *priv)
  763. {
  764. struct bcap_device *bcap_dev = video_drvdata(file);
  765. /* status for sub devices */
  766. v4l2_device_call_all(&bcap_dev->v4l2_dev, 0, core, log_status);
  767. return 0;
  768. }
  769. static const struct v4l2_ioctl_ops bcap_ioctl_ops = {
  770. .vidioc_querycap = bcap_querycap,
  771. .vidioc_g_fmt_vid_cap = bcap_g_fmt_vid_cap,
  772. .vidioc_enum_fmt_vid_cap = bcap_enum_fmt_vid_cap,
  773. .vidioc_s_fmt_vid_cap = bcap_s_fmt_vid_cap,
  774. .vidioc_try_fmt_vid_cap = bcap_try_fmt_vid_cap,
  775. .vidioc_enum_input = bcap_enum_input,
  776. .vidioc_g_input = bcap_g_input,
  777. .vidioc_s_input = bcap_s_input,
  778. .vidioc_querystd = bcap_querystd,
  779. .vidioc_s_std = bcap_s_std,
  780. .vidioc_g_std = bcap_g_std,
  781. .vidioc_s_dv_timings = bcap_s_dv_timings,
  782. .vidioc_g_dv_timings = bcap_g_dv_timings,
  783. .vidioc_reqbufs = bcap_reqbufs,
  784. .vidioc_querybuf = bcap_querybuf,
  785. .vidioc_qbuf = bcap_qbuf,
  786. .vidioc_dqbuf = bcap_dqbuf,
  787. .vidioc_streamon = bcap_streamon,
  788. .vidioc_streamoff = bcap_streamoff,
  789. .vidioc_g_parm = bcap_g_parm,
  790. .vidioc_s_parm = bcap_s_parm,
  791. .vidioc_g_chip_ident = bcap_g_chip_ident,
  792. #ifdef CONFIG_VIDEO_ADV_DEBUG
  793. .vidioc_g_register = bcap_dbg_g_register,
  794. .vidioc_s_register = bcap_dbg_s_register,
  795. #endif
  796. .vidioc_log_status = bcap_log_status,
  797. };
  798. static struct v4l2_file_operations bcap_fops = {
  799. .owner = THIS_MODULE,
  800. .open = bcap_open,
  801. .release = bcap_release,
  802. .unlocked_ioctl = video_ioctl2,
  803. .mmap = bcap_mmap,
  804. #ifndef CONFIG_MMU
  805. .get_unmapped_area = bcap_get_unmapped_area,
  806. #endif
  807. .poll = bcap_poll
  808. };
  809. static int __devinit bcap_probe(struct platform_device *pdev)
  810. {
  811. struct bcap_device *bcap_dev;
  812. struct video_device *vfd;
  813. struct i2c_adapter *i2c_adap;
  814. struct bfin_capture_config *config;
  815. struct vb2_queue *q;
  816. struct bcap_route *route;
  817. int ret;
  818. config = pdev->dev.platform_data;
  819. if (!config) {
  820. v4l2_err(pdev->dev.driver, "Unable to get board config\n");
  821. return -ENODEV;
  822. }
  823. bcap_dev = kzalloc(sizeof(*bcap_dev), GFP_KERNEL);
  824. if (!bcap_dev) {
  825. v4l2_err(pdev->dev.driver, "Unable to alloc bcap_dev\n");
  826. return -ENOMEM;
  827. }
  828. bcap_dev->cfg = config;
  829. bcap_dev->ppi = ppi_create_instance(config->ppi_info);
  830. if (!bcap_dev->ppi) {
  831. v4l2_err(pdev->dev.driver, "Unable to create ppi\n");
  832. ret = -ENODEV;
  833. goto err_free_dev;
  834. }
  835. bcap_dev->ppi->priv = bcap_dev;
  836. bcap_dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  837. if (IS_ERR(bcap_dev->alloc_ctx)) {
  838. ret = PTR_ERR(bcap_dev->alloc_ctx);
  839. goto err_free_ppi;
  840. }
  841. vfd = video_device_alloc();
  842. if (!vfd) {
  843. ret = -ENOMEM;
  844. v4l2_err(pdev->dev.driver, "Unable to alloc video device\n");
  845. goto err_cleanup_ctx;
  846. }
  847. /* initialize field of video device */
  848. vfd->release = video_device_release;
  849. vfd->fops = &bcap_fops;
  850. vfd->ioctl_ops = &bcap_ioctl_ops;
  851. vfd->tvnorms = 0;
  852. vfd->v4l2_dev = &bcap_dev->v4l2_dev;
  853. set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  854. strncpy(vfd->name, CAPTURE_DRV_NAME, sizeof(vfd->name));
  855. bcap_dev->video_dev = vfd;
  856. ret = v4l2_device_register(&pdev->dev, &bcap_dev->v4l2_dev);
  857. if (ret) {
  858. v4l2_err(pdev->dev.driver,
  859. "Unable to register v4l2 device\n");
  860. goto err_release_vdev;
  861. }
  862. v4l2_info(&bcap_dev->v4l2_dev, "v4l2 device registered\n");
  863. bcap_dev->v4l2_dev.ctrl_handler = &bcap_dev->ctrl_handler;
  864. ret = v4l2_ctrl_handler_init(&bcap_dev->ctrl_handler, 0);
  865. if (ret) {
  866. v4l2_err(&bcap_dev->v4l2_dev,
  867. "Unable to init control handler\n");
  868. goto err_unreg_v4l2;
  869. }
  870. spin_lock_init(&bcap_dev->lock);
  871. /* initialize queue */
  872. q = &bcap_dev->buffer_queue;
  873. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  874. q->io_modes = VB2_MMAP;
  875. q->drv_priv = bcap_dev;
  876. q->buf_struct_size = sizeof(struct bcap_buffer);
  877. q->ops = &bcap_video_qops;
  878. q->mem_ops = &vb2_dma_contig_memops;
  879. vb2_queue_init(q);
  880. mutex_init(&bcap_dev->mutex);
  881. init_completion(&bcap_dev->comp);
  882. /* init video dma queues */
  883. INIT_LIST_HEAD(&bcap_dev->dma_queue);
  884. vfd->lock = &bcap_dev->mutex;
  885. /* register video device */
  886. ret = video_register_device(bcap_dev->video_dev, VFL_TYPE_GRABBER, -1);
  887. if (ret) {
  888. v4l2_err(&bcap_dev->v4l2_dev,
  889. "Unable to register video device\n");
  890. goto err_free_handler;
  891. }
  892. video_set_drvdata(bcap_dev->video_dev, bcap_dev);
  893. v4l2_info(&bcap_dev->v4l2_dev, "video device registered as: %s\n",
  894. video_device_node_name(vfd));
  895. /* load up the subdevice */
  896. i2c_adap = i2c_get_adapter(config->i2c_adapter_id);
  897. if (!i2c_adap) {
  898. v4l2_err(&bcap_dev->v4l2_dev,
  899. "Unable to find i2c adapter\n");
  900. ret = -ENODEV;
  901. goto err_unreg_vdev;
  902. }
  903. bcap_dev->sd = v4l2_i2c_new_subdev_board(&bcap_dev->v4l2_dev,
  904. i2c_adap,
  905. &config->board_info,
  906. NULL);
  907. if (bcap_dev->sd) {
  908. int i;
  909. if (!config->num_inputs) {
  910. v4l2_err(&bcap_dev->v4l2_dev,
  911. "Unable to work without input\n");
  912. goto err_unreg_vdev;
  913. }
  914. /* update tvnorms from the sub devices */
  915. for (i = 0; i < config->num_inputs; i++)
  916. vfd->tvnorms |= config->inputs[i].std;
  917. } else {
  918. v4l2_err(&bcap_dev->v4l2_dev,
  919. "Unable to register sub device\n");
  920. goto err_unreg_vdev;
  921. }
  922. v4l2_info(&bcap_dev->v4l2_dev, "v4l2 sub device registered\n");
  923. /*
  924. * explicitly set input, otherwise some boards
  925. * may not work at the state as we expected
  926. */
  927. route = &config->routes[0];
  928. ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
  929. route->input, route->output, 0);
  930. if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
  931. v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
  932. goto err_unreg_vdev;
  933. }
  934. bcap_dev->cur_input = 0;
  935. /* if this route has specific config, update ppi control */
  936. if (route->ppi_control)
  937. config->ppi_control = route->ppi_control;
  938. /* now we can probe the default state */
  939. if (config->inputs[0].capabilities & V4L2_IN_CAP_STD) {
  940. v4l2_std_id std;
  941. ret = v4l2_subdev_call(bcap_dev->sd, core, g_std, &std);
  942. if (ret) {
  943. v4l2_err(&bcap_dev->v4l2_dev,
  944. "Unable to get std\n");
  945. goto err_unreg_vdev;
  946. }
  947. bcap_dev->std = std;
  948. }
  949. if (config->inputs[0].capabilities & V4L2_IN_CAP_CUSTOM_TIMINGS) {
  950. struct v4l2_dv_timings dv_timings;
  951. ret = v4l2_subdev_call(bcap_dev->sd, video,
  952. g_dv_timings, &dv_timings);
  953. if (ret) {
  954. v4l2_err(&bcap_dev->v4l2_dev,
  955. "Unable to get dv timings\n");
  956. goto err_unreg_vdev;
  957. }
  958. bcap_dev->dv_timings = dv_timings;
  959. }
  960. ret = bcap_init_sensor_formats(bcap_dev);
  961. if (ret) {
  962. v4l2_err(&bcap_dev->v4l2_dev,
  963. "Unable to create sensor formats table\n");
  964. goto err_unreg_vdev;
  965. }
  966. return 0;
  967. err_unreg_vdev:
  968. video_unregister_device(bcap_dev->video_dev);
  969. bcap_dev->video_dev = NULL;
  970. err_free_handler:
  971. v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
  972. err_unreg_v4l2:
  973. v4l2_device_unregister(&bcap_dev->v4l2_dev);
  974. err_release_vdev:
  975. if (bcap_dev->video_dev)
  976. video_device_release(bcap_dev->video_dev);
  977. err_cleanup_ctx:
  978. vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
  979. err_free_ppi:
  980. ppi_delete_instance(bcap_dev->ppi);
  981. err_free_dev:
  982. kfree(bcap_dev);
  983. return ret;
  984. }
  985. static int __devexit bcap_remove(struct platform_device *pdev)
  986. {
  987. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  988. struct bcap_device *bcap_dev = container_of(v4l2_dev,
  989. struct bcap_device, v4l2_dev);
  990. bcap_free_sensor_formats(bcap_dev);
  991. video_unregister_device(bcap_dev->video_dev);
  992. v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
  993. v4l2_device_unregister(v4l2_dev);
  994. vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
  995. ppi_delete_instance(bcap_dev->ppi);
  996. kfree(bcap_dev);
  997. return 0;
  998. }
  999. static struct platform_driver bcap_driver = {
  1000. .driver = {
  1001. .name = CAPTURE_DRV_NAME,
  1002. .owner = THIS_MODULE,
  1003. },
  1004. .probe = bcap_probe,
  1005. .remove = __devexit_p(bcap_remove),
  1006. };
  1007. module_platform_driver(bcap_driver);
  1008. MODULE_DESCRIPTION("Analog Devices blackfin video capture driver");
  1009. MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
  1010. MODULE_LICENSE("GPL v2");