stk-webcam.c 36 KB

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