stk-webcam.c 34 KB

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