stk-webcam.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. /*
  2. * stk-webcam.c : Driver for Syntek 1125 USB webcam controller
  3. *
  4. * Copyright (C) 2006 Nicolas VIVIEN
  5. * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
  6. *
  7. * Some parts are inspired from cafe_ccic.c
  8. * Copyright 2006-2007 Jonathan Corbet
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/errno.h>
  28. #include <linux/slab.h>
  29. #include <linux/usb.h>
  30. #include <linux/mm.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/videodev2.h>
  33. #include <media/v4l2-common.h>
  34. #include <media/v4l2-ioctl.h>
  35. #include "stk-webcam.h"
  36. static bool hflip = 1;
  37. module_param(hflip, bool, 0444);
  38. MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 1");
  39. static bool vflip = 1;
  40. module_param(vflip, bool, 0444);
  41. MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 1");
  42. static int debug;
  43. module_param(debug, int, 0444);
  44. MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
  45. MODULE_LICENSE("GPL");
  46. MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
  47. MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
  48. /* bool for webcam LED management */
  49. int first_init = 1;
  50. /* Some cameras have audio interfaces, we aren't interested in those */
  51. static struct usb_device_id stkwebcam_table[] = {
  52. { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
  53. { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
  54. { }
  55. };
  56. MODULE_DEVICE_TABLE(usb, stkwebcam_table);
  57. /*
  58. * Basic stuff
  59. */
  60. int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
  61. {
  62. struct usb_device *udev = dev->udev;
  63. int ret;
  64. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  65. 0x01,
  66. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  67. value,
  68. index,
  69. NULL,
  70. 0,
  71. 500);
  72. if (ret < 0)
  73. return ret;
  74. else
  75. return 0;
  76. }
  77. int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
  78. {
  79. struct usb_device *udev = dev->udev;
  80. int ret;
  81. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  82. 0x00,
  83. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  84. 0x00,
  85. index,
  86. (u8 *) value,
  87. sizeof(u8),
  88. 500);
  89. if (ret < 0)
  90. return ret;
  91. else
  92. return 0;
  93. }
  94. static int stk_start_stream(struct stk_camera *dev)
  95. {
  96. int value;
  97. int i, ret;
  98. int value_116, value_117;
  99. if (!is_present(dev))
  100. return -ENODEV;
  101. if (!is_memallocd(dev) || !is_initialised(dev)) {
  102. STK_ERROR("FIXME: Buffers are not allocated\n");
  103. return -EFAULT;
  104. }
  105. ret = usb_set_interface(dev->udev, 0, 5);
  106. if (ret < 0)
  107. STK_ERROR("usb_set_interface failed !\n");
  108. if (stk_sensor_wakeup(dev))
  109. STK_ERROR("error awaking the sensor\n");
  110. stk_camera_read_reg(dev, 0x0116, &value_116);
  111. stk_camera_read_reg(dev, 0x0117, &value_117);
  112. stk_camera_write_reg(dev, 0x0116, 0x0000);
  113. stk_camera_write_reg(dev, 0x0117, 0x0000);
  114. stk_camera_read_reg(dev, 0x0100, &value);
  115. stk_camera_write_reg(dev, 0x0100, value | 0x80);
  116. stk_camera_write_reg(dev, 0x0116, value_116);
  117. stk_camera_write_reg(dev, 0x0117, value_117);
  118. for (i = 0; i < MAX_ISO_BUFS; i++) {
  119. if (dev->isobufs[i].urb) {
  120. ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
  121. atomic_inc(&dev->urbs_used);
  122. if (ret)
  123. return ret;
  124. }
  125. }
  126. set_streaming(dev);
  127. return 0;
  128. }
  129. static int stk_stop_stream(struct stk_camera *dev)
  130. {
  131. int value;
  132. int i;
  133. if (is_present(dev)) {
  134. stk_camera_read_reg(dev, 0x0100, &value);
  135. stk_camera_write_reg(dev, 0x0100, value & ~0x80);
  136. if (dev->isobufs != NULL) {
  137. for (i = 0; i < MAX_ISO_BUFS; i++) {
  138. if (dev->isobufs[i].urb)
  139. usb_kill_urb(dev->isobufs[i].urb);
  140. }
  141. }
  142. unset_streaming(dev);
  143. if (usb_set_interface(dev->udev, 0, 0))
  144. STK_ERROR("usb_set_interface failed !\n");
  145. if (stk_sensor_sleep(dev))
  146. STK_ERROR("error suspending the sensor\n");
  147. }
  148. return 0;
  149. }
  150. /*
  151. * This seems to be the shortest init sequence we
  152. * must do in order to find the sensor
  153. * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
  154. * is also reset. Maybe powers down it?
  155. * Rest of values don't make a difference
  156. */
  157. static struct regval stk1125_initvals[] = {
  158. /*TODO: What means this sequence? */
  159. {0x0000, 0x24},
  160. {0x0100, 0x21},
  161. {0x0002, 0x68},
  162. {0x0003, 0x80},
  163. {0x0005, 0x00},
  164. {0x0007, 0x03},
  165. {0x000d, 0x00},
  166. {0x000f, 0x02},
  167. {0x0300, 0x12},
  168. {0x0350, 0x41},
  169. {0x0351, 0x00},
  170. {0x0352, 0x00},
  171. {0x0353, 0x00},
  172. {0x0018, 0x10},
  173. {0x0019, 0x00},
  174. {0x001b, 0x0e},
  175. {0x001c, 0x46},
  176. {0x0300, 0x80},
  177. {0x001a, 0x04},
  178. {0x0110, 0x00},
  179. {0x0111, 0x00},
  180. {0x0112, 0x00},
  181. {0x0113, 0x00},
  182. {0xffff, 0xff},
  183. };
  184. static int stk_initialise(struct stk_camera *dev)
  185. {
  186. struct regval *rv;
  187. int ret;
  188. if (!is_present(dev))
  189. return -ENODEV;
  190. if (is_initialised(dev))
  191. return 0;
  192. rv = stk1125_initvals;
  193. while (rv->reg != 0xffff) {
  194. ret = stk_camera_write_reg(dev, rv->reg, rv->val);
  195. if (ret)
  196. return ret;
  197. rv++;
  198. }
  199. if (stk_sensor_init(dev) == 0) {
  200. set_initialised(dev);
  201. return 0;
  202. } else
  203. return -1;
  204. }
  205. /* *********************************************** */
  206. /*
  207. * This function is called as an URB transfert is complete (Isochronous pipe).
  208. * So, the traitement is done in interrupt time, so it has be fast, not crash,
  209. * and not stall. Neat.
  210. */
  211. static void stk_isoc_handler(struct urb *urb)
  212. {
  213. int i;
  214. int ret;
  215. int framelen;
  216. unsigned long flags;
  217. unsigned char *fill = NULL;
  218. unsigned char *iso_buf = NULL;
  219. struct stk_camera *dev;
  220. struct stk_sio_buffer *fb;
  221. dev = (struct stk_camera *) urb->context;
  222. if (dev == NULL) {
  223. STK_ERROR("isoc_handler called with NULL device !\n");
  224. return;
  225. }
  226. if (urb->status == -ENOENT || urb->status == -ECONNRESET
  227. || urb->status == -ESHUTDOWN) {
  228. atomic_dec(&dev->urbs_used);
  229. return;
  230. }
  231. spin_lock_irqsave(&dev->spinlock, flags);
  232. if (urb->status != -EINPROGRESS && urb->status != 0) {
  233. STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
  234. goto resubmit;
  235. }
  236. if (list_empty(&dev->sio_avail)) {
  237. /*FIXME Stop streaming after a while */
  238. (void) (printk_ratelimit() &&
  239. STK_ERROR("isoc_handler without available buffer!\n"));
  240. goto resubmit;
  241. }
  242. fb = list_first_entry(&dev->sio_avail,
  243. struct stk_sio_buffer, list);
  244. fill = fb->buffer + fb->v4lbuf.bytesused;
  245. for (i = 0; i < urb->number_of_packets; i++) {
  246. if (urb->iso_frame_desc[i].status != 0) {
  247. if (urb->iso_frame_desc[i].status != -EXDEV)
  248. STK_ERROR("Frame %d has error %d\n", i,
  249. urb->iso_frame_desc[i].status);
  250. continue;
  251. }
  252. framelen = urb->iso_frame_desc[i].actual_length;
  253. iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  254. if (framelen <= 4)
  255. continue; /* no data */
  256. /*
  257. * we found something informational from there
  258. * the isoc frames have to type of headers
  259. * type1: 00 xx 00 00 or 20 xx 00 00
  260. * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
  261. * xx is a sequencer which has never been seen over 0x3f
  262. * imho data written down looks like bayer, i see similarities
  263. * after every 640 bytes
  264. */
  265. if (*iso_buf & 0x80) {
  266. framelen -= 8;
  267. iso_buf += 8;
  268. /* This marks a new frame */
  269. if (fb->v4lbuf.bytesused != 0
  270. && fb->v4lbuf.bytesused != dev->frame_size) {
  271. (void) (printk_ratelimit() &&
  272. STK_ERROR("frame %d, "
  273. "bytesused=%d, skipping\n",
  274. i, fb->v4lbuf.bytesused));
  275. fb->v4lbuf.bytesused = 0;
  276. fill = fb->buffer;
  277. } else if (fb->v4lbuf.bytesused == dev->frame_size) {
  278. if (list_is_singular(&dev->sio_avail)) {
  279. /* Always reuse the last buffer */
  280. fb->v4lbuf.bytesused = 0;
  281. fill = fb->buffer;
  282. } else {
  283. list_move_tail(dev->sio_avail.next,
  284. &dev->sio_full);
  285. wake_up(&dev->wait_frame);
  286. fb = list_first_entry(&dev->sio_avail,
  287. struct stk_sio_buffer, list);
  288. fb->v4lbuf.bytesused = 0;
  289. fill = fb->buffer;
  290. }
  291. }
  292. } else {
  293. framelen -= 4;
  294. iso_buf += 4;
  295. }
  296. /* Our buffer is full !!! */
  297. if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
  298. (void) (printk_ratelimit() &&
  299. STK_ERROR("Frame buffer overflow, lost sync\n"));
  300. /*FIXME Do something here? */
  301. continue;
  302. }
  303. spin_unlock_irqrestore(&dev->spinlock, flags);
  304. memcpy(fill, iso_buf, framelen);
  305. spin_lock_irqsave(&dev->spinlock, flags);
  306. fill += framelen;
  307. /* New size of our buffer */
  308. fb->v4lbuf.bytesused += framelen;
  309. }
  310. resubmit:
  311. spin_unlock_irqrestore(&dev->spinlock, flags);
  312. urb->dev = dev->udev;
  313. ret = usb_submit_urb(urb, GFP_ATOMIC);
  314. if (ret != 0) {
  315. STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
  316. ret);
  317. }
  318. }
  319. /* -------------------------------------------- */
  320. static int stk_prepare_iso(struct stk_camera *dev)
  321. {
  322. void *kbuf;
  323. int i, j;
  324. struct urb *urb;
  325. struct usb_device *udev;
  326. if (dev == NULL)
  327. return -ENXIO;
  328. udev = dev->udev;
  329. if (dev->isobufs)
  330. STK_ERROR("isobufs already allocated. Bad\n");
  331. else
  332. dev->isobufs = kcalloc(MAX_ISO_BUFS, sizeof(*dev->isobufs),
  333. GFP_KERNEL);
  334. if (dev->isobufs == NULL) {
  335. STK_ERROR("Unable to allocate iso buffers\n");
  336. return -ENOMEM;
  337. }
  338. for (i = 0; i < MAX_ISO_BUFS; i++) {
  339. if (dev->isobufs[i].data == NULL) {
  340. kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
  341. if (kbuf == NULL) {
  342. STK_ERROR("Failed to allocate iso buffer %d\n",
  343. i);
  344. goto isobufs_out;
  345. }
  346. dev->isobufs[i].data = kbuf;
  347. } else
  348. STK_ERROR("isobuf data already allocated\n");
  349. if (dev->isobufs[i].urb == NULL) {
  350. urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
  351. if (urb == NULL) {
  352. STK_ERROR("Failed to allocate URB %d\n", i);
  353. goto isobufs_out;
  354. }
  355. dev->isobufs[i].urb = urb;
  356. } else {
  357. STK_ERROR("Killing URB\n");
  358. usb_kill_urb(dev->isobufs[i].urb);
  359. urb = dev->isobufs[i].urb;
  360. }
  361. urb->interval = 1;
  362. urb->dev = udev;
  363. urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
  364. urb->transfer_flags = URB_ISO_ASAP;
  365. urb->transfer_buffer = dev->isobufs[i].data;
  366. urb->transfer_buffer_length = ISO_BUFFER_SIZE;
  367. urb->complete = stk_isoc_handler;
  368. urb->context = dev;
  369. urb->start_frame = 0;
  370. urb->number_of_packets = ISO_FRAMES_PER_DESC;
  371. for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
  372. urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
  373. urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
  374. }
  375. }
  376. set_memallocd(dev);
  377. return 0;
  378. isobufs_out:
  379. for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
  380. kfree(dev->isobufs[i].data);
  381. for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
  382. usb_free_urb(dev->isobufs[i].urb);
  383. kfree(dev->isobufs);
  384. dev->isobufs = NULL;
  385. return -ENOMEM;
  386. }
  387. static void stk_clean_iso(struct stk_camera *dev)
  388. {
  389. int i;
  390. if (dev == NULL || dev->isobufs == NULL)
  391. return;
  392. for (i = 0; i < MAX_ISO_BUFS; i++) {
  393. struct urb *urb;
  394. urb = dev->isobufs[i].urb;
  395. if (urb) {
  396. if (atomic_read(&dev->urbs_used) && is_present(dev))
  397. usb_kill_urb(urb);
  398. usb_free_urb(urb);
  399. }
  400. kfree(dev->isobufs[i].data);
  401. }
  402. kfree(dev->isobufs);
  403. dev->isobufs = NULL;
  404. unset_memallocd(dev);
  405. }
  406. static int stk_setup_siobuf(struct stk_camera *dev, int index)
  407. {
  408. struct stk_sio_buffer *buf = dev->sio_bufs + index;
  409. INIT_LIST_HEAD(&buf->list);
  410. buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
  411. buf->buffer = vmalloc_user(buf->v4lbuf.length);
  412. if (buf->buffer == NULL)
  413. return -ENOMEM;
  414. buf->mapcount = 0;
  415. buf->dev = dev;
  416. buf->v4lbuf.index = index;
  417. buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  418. buf->v4lbuf.field = V4L2_FIELD_NONE;
  419. buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
  420. buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
  421. return 0;
  422. }
  423. static int stk_free_sio_buffers(struct stk_camera *dev)
  424. {
  425. int i;
  426. int nbufs;
  427. unsigned long flags;
  428. if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
  429. return 0;
  430. /*
  431. * If any buffers are mapped, we cannot free them at all.
  432. */
  433. for (i = 0; i < dev->n_sbufs; i++) {
  434. if (dev->sio_bufs[i].mapcount > 0)
  435. return -EBUSY;
  436. }
  437. /*
  438. * OK, let's do it.
  439. */
  440. spin_lock_irqsave(&dev->spinlock, flags);
  441. INIT_LIST_HEAD(&dev->sio_avail);
  442. INIT_LIST_HEAD(&dev->sio_full);
  443. nbufs = dev->n_sbufs;
  444. dev->n_sbufs = 0;
  445. spin_unlock_irqrestore(&dev->spinlock, flags);
  446. for (i = 0; i < nbufs; i++) {
  447. if (dev->sio_bufs[i].buffer != NULL)
  448. vfree(dev->sio_bufs[i].buffer);
  449. }
  450. kfree(dev->sio_bufs);
  451. dev->sio_bufs = NULL;
  452. return 0;
  453. }
  454. static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
  455. {
  456. int i;
  457. if (dev->sio_bufs != NULL)
  458. STK_ERROR("sio_bufs already allocated\n");
  459. else {
  460. dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
  461. GFP_KERNEL);
  462. if (dev->sio_bufs == NULL)
  463. return -ENOMEM;
  464. for (i = 0; i < n_sbufs; i++) {
  465. if (stk_setup_siobuf(dev, i))
  466. return (dev->n_sbufs > 1 ? 0 : -ENOMEM);
  467. dev->n_sbufs = i+1;
  468. }
  469. }
  470. return 0;
  471. }
  472. static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
  473. {
  474. int err;
  475. err = stk_prepare_iso(dev);
  476. if (err) {
  477. stk_clean_iso(dev);
  478. return err;
  479. }
  480. err = stk_prepare_sio_buffers(dev, n_sbufs);
  481. if (err) {
  482. stk_free_sio_buffers(dev);
  483. return err;
  484. }
  485. return 0;
  486. }
  487. static void stk_free_buffers(struct stk_camera *dev)
  488. {
  489. stk_clean_iso(dev);
  490. stk_free_sio_buffers(dev);
  491. }
  492. /* -------------------------------------------- */
  493. /* v4l file operations */
  494. static int v4l_stk_open(struct file *fp)
  495. {
  496. struct stk_camera *dev;
  497. struct video_device *vdev;
  498. vdev = video_devdata(fp);
  499. dev = vdev_to_camera(vdev);
  500. if (dev == NULL || !is_present(dev))
  501. return -ENXIO;
  502. if (!first_init)
  503. stk_camera_write_reg(dev, 0x0, 0x24);
  504. else
  505. first_init = 0;
  506. fp->private_data = dev;
  507. usb_autopm_get_interface(dev->interface);
  508. return 0;
  509. }
  510. static int v4l_stk_release(struct file *fp)
  511. {
  512. struct stk_camera *dev = fp->private_data;
  513. if (dev->owner == fp) {
  514. stk_stop_stream(dev);
  515. stk_free_buffers(dev);
  516. stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
  517. unset_initialised(dev);
  518. dev->owner = NULL;
  519. }
  520. if (is_present(dev))
  521. usb_autopm_put_interface(dev->interface);
  522. return 0;
  523. }
  524. static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
  525. size_t count, loff_t *f_pos)
  526. {
  527. int i;
  528. int ret;
  529. unsigned long flags;
  530. struct stk_sio_buffer *sbuf;
  531. struct stk_camera *dev = fp->private_data;
  532. if (!is_present(dev))
  533. return -EIO;
  534. if (dev->owner && dev->owner != fp)
  535. return -EBUSY;
  536. dev->owner = fp;
  537. if (!is_streaming(dev)) {
  538. if (stk_initialise(dev)
  539. || stk_allocate_buffers(dev, 3)
  540. || stk_start_stream(dev))
  541. return -ENOMEM;
  542. spin_lock_irqsave(&dev->spinlock, flags);
  543. for (i = 0; i < dev->n_sbufs; i++) {
  544. list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
  545. dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
  546. }
  547. spin_unlock_irqrestore(&dev->spinlock, flags);
  548. }
  549. if (*f_pos == 0) {
  550. if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
  551. return -EWOULDBLOCK;
  552. ret = wait_event_interruptible(dev->wait_frame,
  553. !list_empty(&dev->sio_full) || !is_present(dev));
  554. if (ret)
  555. return ret;
  556. if (!is_present(dev))
  557. return -EIO;
  558. }
  559. if (count + *f_pos > dev->frame_size)
  560. count = dev->frame_size - *f_pos;
  561. spin_lock_irqsave(&dev->spinlock, flags);
  562. if (list_empty(&dev->sio_full)) {
  563. spin_unlock_irqrestore(&dev->spinlock, flags);
  564. STK_ERROR("BUG: No siobufs ready\n");
  565. return 0;
  566. }
  567. sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
  568. spin_unlock_irqrestore(&dev->spinlock, flags);
  569. if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
  570. return -EFAULT;
  571. *f_pos += count;
  572. if (*f_pos >= dev->frame_size) {
  573. *f_pos = 0;
  574. spin_lock_irqsave(&dev->spinlock, flags);
  575. list_move_tail(&sbuf->list, &dev->sio_avail);
  576. spin_unlock_irqrestore(&dev->spinlock, flags);
  577. }
  578. return count;
  579. }
  580. static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
  581. {
  582. struct stk_camera *dev = fp->private_data;
  583. poll_wait(fp, &dev->wait_frame, wait);
  584. if (!is_present(dev))
  585. return POLLERR;
  586. if (!list_empty(&dev->sio_full))
  587. return POLLIN | POLLRDNORM;
  588. return 0;
  589. }
  590. static void stk_v4l_vm_open(struct vm_area_struct *vma)
  591. {
  592. struct stk_sio_buffer *sbuf = vma->vm_private_data;
  593. sbuf->mapcount++;
  594. }
  595. static void stk_v4l_vm_close(struct vm_area_struct *vma)
  596. {
  597. struct stk_sio_buffer *sbuf = vma->vm_private_data;
  598. sbuf->mapcount--;
  599. if (sbuf->mapcount == 0)
  600. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
  601. }
  602. static const struct vm_operations_struct stk_v4l_vm_ops = {
  603. .open = stk_v4l_vm_open,
  604. .close = stk_v4l_vm_close
  605. };
  606. static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
  607. {
  608. unsigned int i;
  609. int ret;
  610. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  611. struct stk_camera *dev = fp->private_data;
  612. struct stk_sio_buffer *sbuf = NULL;
  613. if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
  614. return -EINVAL;
  615. for (i = 0; i < dev->n_sbufs; i++) {
  616. if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
  617. sbuf = dev->sio_bufs + i;
  618. break;
  619. }
  620. }
  621. if (sbuf == NULL)
  622. return -EINVAL;
  623. ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
  624. if (ret)
  625. return ret;
  626. vma->vm_flags |= VM_DONTEXPAND;
  627. vma->vm_private_data = sbuf;
  628. vma->vm_ops = &stk_v4l_vm_ops;
  629. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
  630. stk_v4l_vm_open(vma);
  631. return 0;
  632. }
  633. /* v4l ioctl handlers */
  634. static int stk_vidioc_querycap(struct file *filp,
  635. void *priv, struct v4l2_capability *cap)
  636. {
  637. strcpy(cap->driver, "stk");
  638. strcpy(cap->card, "stk");
  639. cap->version = DRIVER_VERSION_NUM;
  640. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
  641. | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  642. return 0;
  643. }
  644. static int stk_vidioc_enum_input(struct file *filp,
  645. void *priv, struct v4l2_input *input)
  646. {
  647. if (input->index != 0)
  648. return -EINVAL;
  649. strcpy(input->name, "Syntek USB Camera");
  650. input->type = V4L2_INPUT_TYPE_CAMERA;
  651. return 0;
  652. }
  653. static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  654. {
  655. *i = 0;
  656. return 0;
  657. }
  658. static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  659. {
  660. if (i != 0)
  661. return -EINVAL;
  662. else
  663. return 0;
  664. }
  665. /* from vivi.c */
  666. static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
  667. {
  668. return 0;
  669. }
  670. /* List of all V4Lv2 controls supported by the driver */
  671. static struct v4l2_queryctrl stk_controls[] = {
  672. {
  673. .id = V4L2_CID_BRIGHTNESS,
  674. .type = V4L2_CTRL_TYPE_INTEGER,
  675. .name = "Brightness",
  676. .minimum = 0,
  677. .maximum = 0xffff,
  678. .step = 0x0100,
  679. .default_value = 0x6000,
  680. },
  681. {
  682. .id = V4L2_CID_HFLIP,
  683. .type = V4L2_CTRL_TYPE_BOOLEAN,
  684. .name = "Horizontal Flip",
  685. .minimum = 0,
  686. .maximum = 1,
  687. .step = 1,
  688. .default_value = 1,
  689. },
  690. {
  691. .id = V4L2_CID_VFLIP,
  692. .type = V4L2_CTRL_TYPE_BOOLEAN,
  693. .name = "Vertical Flip",
  694. .minimum = 0,
  695. .maximum = 1,
  696. .step = 1,
  697. .default_value = 1,
  698. },
  699. };
  700. static int stk_vidioc_queryctrl(struct file *filp,
  701. void *priv, struct v4l2_queryctrl *c)
  702. {
  703. int i;
  704. int nbr;
  705. nbr = ARRAY_SIZE(stk_controls);
  706. for (i = 0; i < nbr; i++) {
  707. if (stk_controls[i].id == c->id) {
  708. memcpy(c, &stk_controls[i],
  709. sizeof(struct v4l2_queryctrl));
  710. return 0;
  711. }
  712. }
  713. return -EINVAL;
  714. }
  715. static int stk_vidioc_g_ctrl(struct file *filp,
  716. void *priv, struct v4l2_control *c)
  717. {
  718. struct stk_camera *dev = priv;
  719. switch (c->id) {
  720. case V4L2_CID_BRIGHTNESS:
  721. c->value = dev->vsettings.brightness;
  722. break;
  723. case V4L2_CID_HFLIP:
  724. c->value = dev->vsettings.hflip;
  725. break;
  726. case V4L2_CID_VFLIP:
  727. c->value = dev->vsettings.vflip;
  728. break;
  729. default:
  730. return -EINVAL;
  731. }
  732. return 0;
  733. }
  734. static int stk_vidioc_s_ctrl(struct file *filp,
  735. void *priv, struct v4l2_control *c)
  736. {
  737. struct stk_camera *dev = priv;
  738. switch (c->id) {
  739. case V4L2_CID_BRIGHTNESS:
  740. dev->vsettings.brightness = c->value;
  741. return stk_sensor_set_brightness(dev, c->value >> 8);
  742. case V4L2_CID_HFLIP:
  743. dev->vsettings.hflip = c->value;
  744. return 0;
  745. case V4L2_CID_VFLIP:
  746. dev->vsettings.vflip = c->value;
  747. return 0;
  748. default:
  749. return -EINVAL;
  750. }
  751. return 0;
  752. }
  753. static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
  754. void *priv, struct v4l2_fmtdesc *fmtd)
  755. {
  756. switch (fmtd->index) {
  757. case 0:
  758. fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
  759. strcpy(fmtd->description, "r5g6b5");
  760. break;
  761. case 1:
  762. fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
  763. strcpy(fmtd->description, "r5g6b5BE");
  764. break;
  765. case 2:
  766. fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
  767. strcpy(fmtd->description, "yuv4:2:2");
  768. break;
  769. case 3:
  770. fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
  771. strcpy(fmtd->description, "Raw bayer");
  772. break;
  773. case 4:
  774. fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
  775. strcpy(fmtd->description, "yuv4:2:2");
  776. break;
  777. default:
  778. return -EINVAL;
  779. }
  780. return 0;
  781. }
  782. static struct stk_size {
  783. unsigned w;
  784. unsigned h;
  785. enum stk_mode m;
  786. } stk_sizes[] = {
  787. { .w = 1280, .h = 1024, .m = MODE_SXGA, },
  788. { .w = 640, .h = 480, .m = MODE_VGA, },
  789. { .w = 352, .h = 288, .m = MODE_CIF, },
  790. { .w = 320, .h = 240, .m = MODE_QVGA, },
  791. { .w = 176, .h = 144, .m = MODE_QCIF, },
  792. };
  793. static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
  794. void *priv, struct v4l2_format *f)
  795. {
  796. struct v4l2_pix_format *pix_format = &f->fmt.pix;
  797. struct stk_camera *dev = priv;
  798. int i;
  799. for (i = 0; i < ARRAY_SIZE(stk_sizes) &&
  800. stk_sizes[i].m != dev->vsettings.mode; i++)
  801. ;
  802. if (i == ARRAY_SIZE(stk_sizes)) {
  803. STK_ERROR("ERROR: mode invalid\n");
  804. return -EINVAL;
  805. }
  806. pix_format->width = stk_sizes[i].w;
  807. pix_format->height = stk_sizes[i].h;
  808. pix_format->field = V4L2_FIELD_NONE;
  809. pix_format->colorspace = V4L2_COLORSPACE_SRGB;
  810. pix_format->pixelformat = dev->vsettings.palette;
  811. if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
  812. pix_format->bytesperline = pix_format->width;
  813. else
  814. pix_format->bytesperline = 2 * pix_format->width;
  815. pix_format->sizeimage = pix_format->bytesperline
  816. * pix_format->height;
  817. return 0;
  818. }
  819. static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
  820. void *priv, struct v4l2_format *fmtd)
  821. {
  822. int i;
  823. switch (fmtd->fmt.pix.pixelformat) {
  824. case V4L2_PIX_FMT_RGB565:
  825. case V4L2_PIX_FMT_RGB565X:
  826. case V4L2_PIX_FMT_UYVY:
  827. case V4L2_PIX_FMT_YUYV:
  828. case V4L2_PIX_FMT_SBGGR8:
  829. break;
  830. default:
  831. return -EINVAL;
  832. }
  833. for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
  834. if (fmtd->fmt.pix.width > stk_sizes[i].w)
  835. break;
  836. }
  837. if (i == ARRAY_SIZE(stk_sizes)
  838. || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
  839. < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
  840. fmtd->fmt.pix.height = stk_sizes[i-1].h;
  841. fmtd->fmt.pix.width = stk_sizes[i-1].w;
  842. fmtd->fmt.pix.priv = i - 1;
  843. } else {
  844. fmtd->fmt.pix.height = stk_sizes[i].h;
  845. fmtd->fmt.pix.width = stk_sizes[i].w;
  846. fmtd->fmt.pix.priv = i;
  847. }
  848. fmtd->fmt.pix.field = V4L2_FIELD_NONE;
  849. fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  850. if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
  851. fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
  852. else
  853. fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
  854. fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
  855. * fmtd->fmt.pix.height;
  856. return 0;
  857. }
  858. static int stk_setup_format(struct stk_camera *dev)
  859. {
  860. int i = 0;
  861. int depth;
  862. if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
  863. depth = 1;
  864. else
  865. depth = 2;
  866. while (i < ARRAY_SIZE(stk_sizes) &&
  867. stk_sizes[i].m != dev->vsettings.mode)
  868. i++;
  869. if (i == ARRAY_SIZE(stk_sizes)) {
  870. STK_ERROR("Something is broken in %s\n", __func__);
  871. return -EFAULT;
  872. }
  873. /* This registers controls some timings, not sure of what. */
  874. stk_camera_write_reg(dev, 0x001b, 0x0e);
  875. if (dev->vsettings.mode == MODE_SXGA)
  876. stk_camera_write_reg(dev, 0x001c, 0x0e);
  877. else
  878. stk_camera_write_reg(dev, 0x001c, 0x46);
  879. /*
  880. * Registers 0x0115 0x0114 are the size of each line (bytes),
  881. * regs 0x0117 0x0116 are the heigth of the image.
  882. */
  883. stk_camera_write_reg(dev, 0x0115,
  884. ((stk_sizes[i].w * depth) >> 8) & 0xff);
  885. stk_camera_write_reg(dev, 0x0114,
  886. (stk_sizes[i].w * depth) & 0xff);
  887. stk_camera_write_reg(dev, 0x0117,
  888. (stk_sizes[i].h >> 8) & 0xff);
  889. stk_camera_write_reg(dev, 0x0116,
  890. stk_sizes[i].h & 0xff);
  891. return stk_sensor_configure(dev);
  892. }
  893. static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
  894. void *priv, struct v4l2_format *fmtd)
  895. {
  896. int ret;
  897. struct stk_camera *dev = priv;
  898. if (dev == NULL)
  899. return -ENODEV;
  900. if (!is_present(dev))
  901. return -ENODEV;
  902. if (is_streaming(dev))
  903. return -EBUSY;
  904. if (dev->owner && dev->owner != filp)
  905. return -EBUSY;
  906. ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
  907. if (ret)
  908. return ret;
  909. dev->owner = filp;
  910. dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
  911. stk_free_buffers(dev);
  912. dev->frame_size = fmtd->fmt.pix.sizeimage;
  913. dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
  914. stk_initialise(dev);
  915. return stk_setup_format(dev);
  916. }
  917. static int stk_vidioc_reqbufs(struct file *filp,
  918. void *priv, struct v4l2_requestbuffers *rb)
  919. {
  920. struct stk_camera *dev = priv;
  921. if (dev == NULL)
  922. return -ENODEV;
  923. if (rb->memory != V4L2_MEMORY_MMAP)
  924. return -EINVAL;
  925. if (is_streaming(dev)
  926. || (dev->owner && dev->owner != filp))
  927. return -EBUSY;
  928. dev->owner = filp;
  929. /*FIXME If they ask for zero, we must stop streaming and free */
  930. if (rb->count < 3)
  931. rb->count = 3;
  932. /* Arbitrary limit */
  933. else if (rb->count > 5)
  934. rb->count = 5;
  935. stk_allocate_buffers(dev, rb->count);
  936. rb->count = dev->n_sbufs;
  937. return 0;
  938. }
  939. static int stk_vidioc_querybuf(struct file *filp,
  940. void *priv, struct v4l2_buffer *buf)
  941. {
  942. struct stk_camera *dev = priv;
  943. struct stk_sio_buffer *sbuf;
  944. if (buf->index >= dev->n_sbufs)
  945. return -EINVAL;
  946. sbuf = dev->sio_bufs + buf->index;
  947. *buf = sbuf->v4lbuf;
  948. return 0;
  949. }
  950. static int stk_vidioc_qbuf(struct file *filp,
  951. void *priv, struct v4l2_buffer *buf)
  952. {
  953. struct stk_camera *dev = priv;
  954. struct stk_sio_buffer *sbuf;
  955. unsigned long flags;
  956. if (buf->memory != V4L2_MEMORY_MMAP)
  957. return -EINVAL;
  958. if (buf->index >= dev->n_sbufs)
  959. return -EINVAL;
  960. sbuf = dev->sio_bufs + buf->index;
  961. if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
  962. return 0;
  963. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
  964. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
  965. spin_lock_irqsave(&dev->spinlock, flags);
  966. list_add_tail(&sbuf->list, &dev->sio_avail);
  967. *buf = sbuf->v4lbuf;
  968. spin_unlock_irqrestore(&dev->spinlock, flags);
  969. return 0;
  970. }
  971. static int stk_vidioc_dqbuf(struct file *filp,
  972. void *priv, struct v4l2_buffer *buf)
  973. {
  974. struct stk_camera *dev = priv;
  975. struct stk_sio_buffer *sbuf;
  976. unsigned long flags;
  977. int ret;
  978. if (!is_streaming(dev))
  979. return -EINVAL;
  980. if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
  981. return -EWOULDBLOCK;
  982. ret = wait_event_interruptible(dev->wait_frame,
  983. !list_empty(&dev->sio_full) || !is_present(dev));
  984. if (ret)
  985. return ret;
  986. if (!is_present(dev))
  987. return -EIO;
  988. spin_lock_irqsave(&dev->spinlock, flags);
  989. sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
  990. list_del_init(&sbuf->list);
  991. spin_unlock_irqrestore(&dev->spinlock, flags);
  992. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
  993. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
  994. sbuf->v4lbuf.sequence = ++dev->sequence;
  995. do_gettimeofday(&sbuf->v4lbuf.timestamp);
  996. *buf = sbuf->v4lbuf;
  997. return 0;
  998. }
  999. static int stk_vidioc_streamon(struct file *filp,
  1000. void *priv, enum v4l2_buf_type type)
  1001. {
  1002. struct stk_camera *dev = priv;
  1003. if (is_streaming(dev))
  1004. return 0;
  1005. if (dev->sio_bufs == NULL)
  1006. return -EINVAL;
  1007. dev->sequence = 0;
  1008. return stk_start_stream(dev);
  1009. }
  1010. static int stk_vidioc_streamoff(struct file *filp,
  1011. void *priv, enum v4l2_buf_type type)
  1012. {
  1013. struct stk_camera *dev = priv;
  1014. unsigned long flags;
  1015. int i;
  1016. stk_stop_stream(dev);
  1017. spin_lock_irqsave(&dev->spinlock, flags);
  1018. INIT_LIST_HEAD(&dev->sio_avail);
  1019. INIT_LIST_HEAD(&dev->sio_full);
  1020. for (i = 0; i < dev->n_sbufs; i++) {
  1021. INIT_LIST_HEAD(&dev->sio_bufs[i].list);
  1022. dev->sio_bufs[i].v4lbuf.flags = 0;
  1023. }
  1024. spin_unlock_irqrestore(&dev->spinlock, flags);
  1025. return 0;
  1026. }
  1027. static int stk_vidioc_g_parm(struct file *filp,
  1028. void *priv, struct v4l2_streamparm *sp)
  1029. {
  1030. /*FIXME This is not correct */
  1031. sp->parm.capture.timeperframe.numerator = 1;
  1032. sp->parm.capture.timeperframe.denominator = 30;
  1033. sp->parm.capture.readbuffers = 2;
  1034. return 0;
  1035. }
  1036. static int stk_vidioc_enum_framesizes(struct file *filp,
  1037. void *priv, struct v4l2_frmsizeenum *frms)
  1038. {
  1039. if (frms->index >= ARRAY_SIZE(stk_sizes))
  1040. return -EINVAL;
  1041. switch (frms->pixel_format) {
  1042. case V4L2_PIX_FMT_RGB565:
  1043. case V4L2_PIX_FMT_RGB565X:
  1044. case V4L2_PIX_FMT_UYVY:
  1045. case V4L2_PIX_FMT_YUYV:
  1046. case V4L2_PIX_FMT_SBGGR8:
  1047. frms->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1048. frms->discrete.width = stk_sizes[frms->index].w;
  1049. frms->discrete.height = stk_sizes[frms->index].h;
  1050. return 0;
  1051. default: return -EINVAL;
  1052. }
  1053. }
  1054. static struct v4l2_file_operations v4l_stk_fops = {
  1055. .owner = THIS_MODULE,
  1056. .open = v4l_stk_open,
  1057. .release = v4l_stk_release,
  1058. .read = v4l_stk_read,
  1059. .poll = v4l_stk_poll,
  1060. .mmap = v4l_stk_mmap,
  1061. .ioctl = video_ioctl2,
  1062. };
  1063. static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
  1064. .vidioc_querycap = stk_vidioc_querycap,
  1065. .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
  1066. .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
  1067. .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
  1068. .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
  1069. .vidioc_enum_input = stk_vidioc_enum_input,
  1070. .vidioc_s_input = stk_vidioc_s_input,
  1071. .vidioc_g_input = stk_vidioc_g_input,
  1072. .vidioc_s_std = stk_vidioc_s_std,
  1073. .vidioc_reqbufs = stk_vidioc_reqbufs,
  1074. .vidioc_querybuf = stk_vidioc_querybuf,
  1075. .vidioc_qbuf = stk_vidioc_qbuf,
  1076. .vidioc_dqbuf = stk_vidioc_dqbuf,
  1077. .vidioc_streamon = stk_vidioc_streamon,
  1078. .vidioc_streamoff = stk_vidioc_streamoff,
  1079. .vidioc_queryctrl = stk_vidioc_queryctrl,
  1080. .vidioc_g_ctrl = stk_vidioc_g_ctrl,
  1081. .vidioc_s_ctrl = stk_vidioc_s_ctrl,
  1082. .vidioc_g_parm = stk_vidioc_g_parm,
  1083. .vidioc_enum_framesizes = stk_vidioc_enum_framesizes,
  1084. };
  1085. static void stk_v4l_dev_release(struct video_device *vd)
  1086. {
  1087. struct stk_camera *dev = vdev_to_camera(vd);
  1088. if (dev->sio_bufs != NULL || dev->isobufs != NULL)
  1089. STK_ERROR("We are leaking memory\n");
  1090. usb_put_intf(dev->interface);
  1091. kfree(dev);
  1092. }
  1093. static struct video_device stk_v4l_data = {
  1094. .name = "stkwebcam",
  1095. .tvnorms = V4L2_STD_UNKNOWN,
  1096. .current_norm = V4L2_STD_UNKNOWN,
  1097. .fops = &v4l_stk_fops,
  1098. .ioctl_ops = &v4l_stk_ioctl_ops,
  1099. .release = stk_v4l_dev_release,
  1100. };
  1101. static int stk_register_video_device(struct stk_camera *dev)
  1102. {
  1103. int err;
  1104. dev->vdev = stk_v4l_data;
  1105. dev->vdev.debug = debug;
  1106. dev->vdev.parent = &dev->interface->dev;
  1107. err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
  1108. if (err)
  1109. STK_ERROR("v4l registration failed\n");
  1110. else
  1111. STK_INFO("Syntek USB2.0 Camera is now controlling device %s\n",
  1112. video_device_node_name(&dev->vdev));
  1113. return err;
  1114. }
  1115. /* USB Stuff */
  1116. static int stk_camera_probe(struct usb_interface *interface,
  1117. const struct usb_device_id *id)
  1118. {
  1119. int i;
  1120. int err = 0;
  1121. struct stk_camera *dev = NULL;
  1122. struct usb_device *udev = interface_to_usbdev(interface);
  1123. struct usb_host_interface *iface_desc;
  1124. struct usb_endpoint_descriptor *endpoint;
  1125. dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
  1126. if (dev == NULL) {
  1127. STK_ERROR("Out of memory !\n");
  1128. return -ENOMEM;
  1129. }
  1130. spin_lock_init(&dev->spinlock);
  1131. init_waitqueue_head(&dev->wait_frame);
  1132. dev->udev = udev;
  1133. dev->interface = interface;
  1134. usb_get_intf(interface);
  1135. dev->vsettings.vflip = vflip;
  1136. dev->vsettings.hflip = hflip;
  1137. dev->n_sbufs = 0;
  1138. set_present(dev);
  1139. /* Set up the endpoint information
  1140. * use only the first isoc-in endpoint
  1141. * for the current alternate setting */
  1142. iface_desc = interface->cur_altsetting;
  1143. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1144. endpoint = &iface_desc->endpoint[i].desc;
  1145. if (!dev->isoc_ep
  1146. && usb_endpoint_is_isoc_in(endpoint)) {
  1147. /* we found an isoc in endpoint */
  1148. dev->isoc_ep = usb_endpoint_num(endpoint);
  1149. break;
  1150. }
  1151. }
  1152. if (!dev->isoc_ep) {
  1153. STK_ERROR("Could not find isoc-in endpoint");
  1154. err = -ENODEV;
  1155. goto error;
  1156. }
  1157. dev->vsettings.brightness = 0x7fff;
  1158. dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
  1159. dev->vsettings.mode = MODE_VGA;
  1160. dev->frame_size = 640 * 480 * 2;
  1161. INIT_LIST_HEAD(&dev->sio_avail);
  1162. INIT_LIST_HEAD(&dev->sio_full);
  1163. usb_set_intfdata(interface, dev);
  1164. err = stk_register_video_device(dev);
  1165. if (err)
  1166. goto error;
  1167. return 0;
  1168. error:
  1169. kfree(dev);
  1170. return err;
  1171. }
  1172. static void stk_camera_disconnect(struct usb_interface *interface)
  1173. {
  1174. struct stk_camera *dev = usb_get_intfdata(interface);
  1175. usb_set_intfdata(interface, NULL);
  1176. unset_present(dev);
  1177. wake_up_interruptible(&dev->wait_frame);
  1178. STK_INFO("Syntek USB2.0 Camera release resources device %s\n",
  1179. video_device_node_name(&dev->vdev));
  1180. video_unregister_device(&dev->vdev);
  1181. }
  1182. #ifdef CONFIG_PM
  1183. static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
  1184. {
  1185. struct stk_camera *dev = usb_get_intfdata(intf);
  1186. if (is_streaming(dev)) {
  1187. stk_stop_stream(dev);
  1188. /* yes, this is ugly */
  1189. set_streaming(dev);
  1190. }
  1191. return 0;
  1192. }
  1193. static int stk_camera_resume(struct usb_interface *intf)
  1194. {
  1195. struct stk_camera *dev = usb_get_intfdata(intf);
  1196. if (!is_initialised(dev))
  1197. return 0;
  1198. unset_initialised(dev);
  1199. stk_initialise(dev);
  1200. stk_camera_write_reg(dev, 0x0, 0x49);
  1201. stk_setup_format(dev);
  1202. if (is_streaming(dev))
  1203. stk_start_stream(dev);
  1204. return 0;
  1205. }
  1206. #endif
  1207. static struct usb_driver stk_camera_driver = {
  1208. .name = "stkwebcam",
  1209. .probe = stk_camera_probe,
  1210. .disconnect = stk_camera_disconnect,
  1211. .id_table = stkwebcam_table,
  1212. #ifdef CONFIG_PM
  1213. .suspend = stk_camera_suspend,
  1214. .resume = stk_camera_resume,
  1215. #endif
  1216. };
  1217. module_usb_driver(stk_camera_driver);