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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/slab.h>
  26. #include <linux/dmi.h>
  27. #include <linux/usb.h>
  28. #include <linux/mm.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/videodev2.h>
  31. #include <media/v4l2-common.h>
  32. #include <media/v4l2-ioctl.h>
  33. #include <media/v4l2-event.h>
  34. #include "stk-webcam.h"
  35. static int hflip = -1;
  36. module_param(hflip, int, 0444);
  37. MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 0");
  38. static int vflip = -1;
  39. module_param(vflip, int, 0444);
  40. MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 0");
  41. static int debug;
  42. module_param(debug, int, 0444);
  43. MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
  44. MODULE_LICENSE("GPL");
  45. MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
  46. MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
  47. /* Some cameras have audio interfaces, we aren't interested in those */
  48. static const struct usb_device_id stkwebcam_table[] = {
  49. { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
  50. { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
  51. { }
  52. };
  53. MODULE_DEVICE_TABLE(usb, stkwebcam_table);
  54. /*
  55. * The stk webcam laptop module is mounted upside down in some laptops :(
  56. *
  57. * Some background information (thanks to Hans de Goede for providing this):
  58. *
  59. * 1) Once upon a time the stkwebcam driver was written
  60. *
  61. * 2) The webcam in question was used mostly in Asus laptop models, including
  62. * the laptop of the original author of the driver, and in these models, in
  63. * typical Asus fashion (see the long long list for uvc cams inside v4l-utils),
  64. * they mounted the webcam-module the wrong way up. So the hflip and vflip
  65. * module options were given a default value of 1 (the correct value for
  66. * upside down mounted models)
  67. *
  68. * 3) Years later I got a bug report from a user with a laptop with stkwebcam,
  69. * where the module was actually mounted the right way up, and thus showed
  70. * upside down under Linux. So now I was facing the choice of 2 options:
  71. *
  72. * a) Add a not-upside-down list to stkwebcam, which overrules the default.
  73. *
  74. * b) Do it like all the other drivers do, and make the default right for
  75. * cams mounted the proper way and add an upside-down model list, with
  76. * models where we need to flip-by-default.
  77. *
  78. * Despite knowing that going b) would cause a period of pain where we were
  79. * building the table I opted to go for option b), since a) is just too ugly,
  80. * and worse different from how every other driver does it leading to
  81. * confusion in the long run. This change was made in kernel 3.6.
  82. *
  83. * So for any user report about upside-down images since kernel 3.6 ask them
  84. * to provide the output of 'sudo dmidecode' so the laptop can be added in
  85. * the table below.
  86. */
  87. static const struct dmi_system_id stk_upside_down_dmi_table[] = {
  88. {
  89. .ident = "ASUS G1",
  90. .matches = {
  91. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  92. DMI_MATCH(DMI_PRODUCT_NAME, "G1")
  93. }
  94. }, {
  95. .ident = "ASUS F3JC",
  96. .matches = {
  97. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  98. DMI_MATCH(DMI_PRODUCT_NAME, "F3JC")
  99. }
  100. },
  101. {
  102. .ident = "T12Rg-H",
  103. .matches = {
  104. DMI_MATCH(DMI_SYS_VENDOR, "HCL Infosystems Limited"),
  105. DMI_MATCH(DMI_PRODUCT_NAME, "T12Rg-H")
  106. }
  107. },
  108. {}
  109. };
  110. /*
  111. * Basic stuff
  112. */
  113. int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
  114. {
  115. struct usb_device *udev = dev->udev;
  116. int ret;
  117. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  118. 0x01,
  119. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  120. value,
  121. index,
  122. NULL,
  123. 0,
  124. 500);
  125. if (ret < 0)
  126. return ret;
  127. else
  128. return 0;
  129. }
  130. int stk_camera_read_reg(struct stk_camera *dev, u16 index, u8 *value)
  131. {
  132. struct usb_device *udev = dev->udev;
  133. unsigned char *buf;
  134. int ret;
  135. buf = kmalloc(sizeof(u8), GFP_KERNEL);
  136. if (!buf)
  137. return -ENOMEM;
  138. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  139. 0x00,
  140. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  141. 0x00,
  142. index,
  143. buf,
  144. sizeof(u8),
  145. 500);
  146. if (ret >= 0)
  147. *value = *buf;
  148. kfree(buf);
  149. return ret;
  150. }
  151. static int stk_start_stream(struct stk_camera *dev)
  152. {
  153. u8 value;
  154. int i, ret;
  155. u8 value_116, value_117;
  156. if (!is_present(dev))
  157. return -ENODEV;
  158. if (!is_memallocd(dev) || !is_initialised(dev)) {
  159. pr_err("FIXME: Buffers are not allocated\n");
  160. return -EFAULT;
  161. }
  162. ret = usb_set_interface(dev->udev, 0, 5);
  163. if (ret < 0)
  164. pr_err("usb_set_interface failed !\n");
  165. if (stk_sensor_wakeup(dev))
  166. pr_err("error awaking the sensor\n");
  167. stk_camera_read_reg(dev, 0x0116, &value_116);
  168. stk_camera_read_reg(dev, 0x0117, &value_117);
  169. stk_camera_write_reg(dev, 0x0116, 0x0000);
  170. stk_camera_write_reg(dev, 0x0117, 0x0000);
  171. stk_camera_read_reg(dev, 0x0100, &value);
  172. stk_camera_write_reg(dev, 0x0100, value | 0x80);
  173. stk_camera_write_reg(dev, 0x0116, value_116);
  174. stk_camera_write_reg(dev, 0x0117, value_117);
  175. for (i = 0; i < MAX_ISO_BUFS; i++) {
  176. if (dev->isobufs[i].urb) {
  177. ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
  178. atomic_inc(&dev->urbs_used);
  179. if (ret)
  180. return ret;
  181. }
  182. }
  183. set_streaming(dev);
  184. return 0;
  185. }
  186. static int stk_stop_stream(struct stk_camera *dev)
  187. {
  188. u8 value;
  189. int i;
  190. if (is_present(dev)) {
  191. stk_camera_read_reg(dev, 0x0100, &value);
  192. stk_camera_write_reg(dev, 0x0100, value & ~0x80);
  193. if (dev->isobufs != NULL) {
  194. for (i = 0; i < MAX_ISO_BUFS; i++) {
  195. if (dev->isobufs[i].urb)
  196. usb_kill_urb(dev->isobufs[i].urb);
  197. }
  198. }
  199. unset_streaming(dev);
  200. if (usb_set_interface(dev->udev, 0, 0))
  201. pr_err("usb_set_interface failed !\n");
  202. if (stk_sensor_sleep(dev))
  203. pr_err("error suspending the sensor\n");
  204. }
  205. return 0;
  206. }
  207. /*
  208. * This seems to be the shortest init sequence we
  209. * must do in order to find the sensor
  210. * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
  211. * is also reset. Maybe powers down it?
  212. * Rest of values don't make a difference
  213. */
  214. static struct regval stk1125_initvals[] = {
  215. /*TODO: What means this sequence? */
  216. {0x0000, 0x24},
  217. {0x0100, 0x21},
  218. {0x0002, 0x68},
  219. {0x0003, 0x80},
  220. {0x0005, 0x00},
  221. {0x0007, 0x03},
  222. {0x000d, 0x00},
  223. {0x000f, 0x02},
  224. {0x0300, 0x12},
  225. {0x0350, 0x41},
  226. {0x0351, 0x00},
  227. {0x0352, 0x00},
  228. {0x0353, 0x00},
  229. {0x0018, 0x10},
  230. {0x0019, 0x00},
  231. {0x001b, 0x0e},
  232. {0x001c, 0x46},
  233. {0x0300, 0x80},
  234. {0x001a, 0x04},
  235. {0x0110, 0x00},
  236. {0x0111, 0x00},
  237. {0x0112, 0x00},
  238. {0x0113, 0x00},
  239. {0xffff, 0xff},
  240. };
  241. static int stk_initialise(struct stk_camera *dev)
  242. {
  243. struct regval *rv;
  244. int ret;
  245. if (!is_present(dev))
  246. return -ENODEV;
  247. if (is_initialised(dev))
  248. return 0;
  249. rv = stk1125_initvals;
  250. while (rv->reg != 0xffff) {
  251. ret = stk_camera_write_reg(dev, rv->reg, rv->val);
  252. if (ret)
  253. return ret;
  254. rv++;
  255. }
  256. if (stk_sensor_init(dev) == 0) {
  257. set_initialised(dev);
  258. return 0;
  259. } else
  260. return -1;
  261. }
  262. /* *********************************************** */
  263. /*
  264. * This function is called as an URB transfert is complete (Isochronous pipe).
  265. * So, the traitement is done in interrupt time, so it has be fast, not crash,
  266. * and not stall. Neat.
  267. */
  268. static void stk_isoc_handler(struct urb *urb)
  269. {
  270. int i;
  271. int ret;
  272. int framelen;
  273. unsigned long flags;
  274. unsigned char *fill = NULL;
  275. unsigned char *iso_buf = NULL;
  276. struct stk_camera *dev;
  277. struct stk_sio_buffer *fb;
  278. dev = (struct stk_camera *) urb->context;
  279. if (dev == NULL) {
  280. pr_err("isoc_handler called with NULL device !\n");
  281. return;
  282. }
  283. if (urb->status == -ENOENT || urb->status == -ECONNRESET
  284. || urb->status == -ESHUTDOWN) {
  285. atomic_dec(&dev->urbs_used);
  286. return;
  287. }
  288. spin_lock_irqsave(&dev->spinlock, flags);
  289. if (urb->status != -EINPROGRESS && urb->status != 0) {
  290. pr_err("isoc_handler: urb->status == %d\n", urb->status);
  291. goto resubmit;
  292. }
  293. if (list_empty(&dev->sio_avail)) {
  294. /*FIXME Stop streaming after a while */
  295. pr_err_ratelimited("isoc_handler without available buffer!\n");
  296. goto resubmit;
  297. }
  298. fb = list_first_entry(&dev->sio_avail,
  299. struct stk_sio_buffer, list);
  300. fill = fb->buffer + fb->v4lbuf.bytesused;
  301. for (i = 0; i < urb->number_of_packets; i++) {
  302. if (urb->iso_frame_desc[i].status != 0) {
  303. if (urb->iso_frame_desc[i].status != -EXDEV)
  304. pr_err("Frame %d has error %d\n",
  305. i, urb->iso_frame_desc[i].status);
  306. continue;
  307. }
  308. framelen = urb->iso_frame_desc[i].actual_length;
  309. iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  310. if (framelen <= 4)
  311. continue; /* no data */
  312. /*
  313. * we found something informational from there
  314. * the isoc frames have to type of headers
  315. * type1: 00 xx 00 00 or 20 xx 00 00
  316. * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
  317. * xx is a sequencer which has never been seen over 0x3f
  318. * imho data written down looks like bayer, i see similarities
  319. * after every 640 bytes
  320. */
  321. if (*iso_buf & 0x80) {
  322. framelen -= 8;
  323. iso_buf += 8;
  324. /* This marks a new frame */
  325. if (fb->v4lbuf.bytesused != 0
  326. && fb->v4lbuf.bytesused != dev->frame_size) {
  327. pr_err_ratelimited("frame %d, bytesused=%d, skipping\n",
  328. i, fb->v4lbuf.bytesused);
  329. fb->v4lbuf.bytesused = 0;
  330. fill = fb->buffer;
  331. } else if (fb->v4lbuf.bytesused == dev->frame_size) {
  332. if (list_is_singular(&dev->sio_avail)) {
  333. /* Always reuse the last buffer */
  334. fb->v4lbuf.bytesused = 0;
  335. fill = fb->buffer;
  336. } else {
  337. list_move_tail(dev->sio_avail.next,
  338. &dev->sio_full);
  339. wake_up(&dev->wait_frame);
  340. fb = list_first_entry(&dev->sio_avail,
  341. struct stk_sio_buffer, list);
  342. fb->v4lbuf.bytesused = 0;
  343. fill = fb->buffer;
  344. }
  345. }
  346. } else {
  347. framelen -= 4;
  348. iso_buf += 4;
  349. }
  350. /* Our buffer is full !!! */
  351. if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
  352. pr_err_ratelimited("Frame buffer overflow, lost sync\n");
  353. /*FIXME Do something here? */
  354. continue;
  355. }
  356. spin_unlock_irqrestore(&dev->spinlock, flags);
  357. memcpy(fill, iso_buf, framelen);
  358. spin_lock_irqsave(&dev->spinlock, flags);
  359. fill += framelen;
  360. /* New size of our buffer */
  361. fb->v4lbuf.bytesused += framelen;
  362. }
  363. resubmit:
  364. spin_unlock_irqrestore(&dev->spinlock, flags);
  365. urb->dev = dev->udev;
  366. ret = usb_submit_urb(urb, GFP_ATOMIC);
  367. if (ret != 0) {
  368. pr_err("Error (%d) re-submitting urb in stk_isoc_handler\n",
  369. ret);
  370. }
  371. }
  372. /* -------------------------------------------- */
  373. static int stk_prepare_iso(struct stk_camera *dev)
  374. {
  375. void *kbuf;
  376. int i, j;
  377. struct urb *urb;
  378. struct usb_device *udev;
  379. if (dev == NULL)
  380. return -ENXIO;
  381. udev = dev->udev;
  382. if (dev->isobufs)
  383. pr_err("isobufs already allocated. Bad\n");
  384. else
  385. dev->isobufs = kcalloc(MAX_ISO_BUFS, sizeof(*dev->isobufs),
  386. GFP_KERNEL);
  387. if (dev->isobufs == NULL) {
  388. pr_err("Unable to allocate iso buffers\n");
  389. return -ENOMEM;
  390. }
  391. for (i = 0; i < MAX_ISO_BUFS; i++) {
  392. if (dev->isobufs[i].data == NULL) {
  393. kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
  394. if (kbuf == NULL) {
  395. pr_err("Failed to allocate iso buffer %d\n", i);
  396. goto isobufs_out;
  397. }
  398. dev->isobufs[i].data = kbuf;
  399. } else
  400. pr_err("isobuf data already allocated\n");
  401. if (dev->isobufs[i].urb == NULL) {
  402. urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
  403. if (urb == NULL)
  404. goto isobufs_out;
  405. dev->isobufs[i].urb = urb;
  406. } else {
  407. pr_err("Killing URB\n");
  408. usb_kill_urb(dev->isobufs[i].urb);
  409. urb = dev->isobufs[i].urb;
  410. }
  411. urb->interval = 1;
  412. urb->dev = udev;
  413. urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
  414. urb->transfer_flags = URB_ISO_ASAP;
  415. urb->transfer_buffer = dev->isobufs[i].data;
  416. urb->transfer_buffer_length = ISO_BUFFER_SIZE;
  417. urb->complete = stk_isoc_handler;
  418. urb->context = dev;
  419. urb->start_frame = 0;
  420. urb->number_of_packets = ISO_FRAMES_PER_DESC;
  421. for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
  422. urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
  423. urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
  424. }
  425. }
  426. set_memallocd(dev);
  427. return 0;
  428. isobufs_out:
  429. for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
  430. kfree(dev->isobufs[i].data);
  431. for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
  432. usb_free_urb(dev->isobufs[i].urb);
  433. kfree(dev->isobufs);
  434. dev->isobufs = NULL;
  435. return -ENOMEM;
  436. }
  437. static void stk_clean_iso(struct stk_camera *dev)
  438. {
  439. int i;
  440. if (dev == NULL || dev->isobufs == NULL)
  441. return;
  442. for (i = 0; i < MAX_ISO_BUFS; i++) {
  443. struct urb *urb;
  444. urb = dev->isobufs[i].urb;
  445. if (urb) {
  446. if (atomic_read(&dev->urbs_used) && is_present(dev))
  447. usb_kill_urb(urb);
  448. usb_free_urb(urb);
  449. }
  450. kfree(dev->isobufs[i].data);
  451. }
  452. kfree(dev->isobufs);
  453. dev->isobufs = NULL;
  454. unset_memallocd(dev);
  455. }
  456. static int stk_setup_siobuf(struct stk_camera *dev, int index)
  457. {
  458. struct stk_sio_buffer *buf = dev->sio_bufs + index;
  459. INIT_LIST_HEAD(&buf->list);
  460. buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
  461. buf->buffer = vmalloc_user(buf->v4lbuf.length);
  462. if (buf->buffer == NULL)
  463. return -ENOMEM;
  464. buf->mapcount = 0;
  465. buf->dev = dev;
  466. buf->v4lbuf.index = index;
  467. buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  468. buf->v4lbuf.flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  469. buf->v4lbuf.field = V4L2_FIELD_NONE;
  470. buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
  471. buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
  472. return 0;
  473. }
  474. static int stk_free_sio_buffers(struct stk_camera *dev)
  475. {
  476. int i;
  477. int nbufs;
  478. unsigned long flags;
  479. if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
  480. return 0;
  481. /*
  482. * If any buffers are mapped, we cannot free them at all.
  483. */
  484. for (i = 0; i < dev->n_sbufs; i++) {
  485. if (dev->sio_bufs[i].mapcount > 0)
  486. return -EBUSY;
  487. }
  488. /*
  489. * OK, let's do it.
  490. */
  491. spin_lock_irqsave(&dev->spinlock, flags);
  492. INIT_LIST_HEAD(&dev->sio_avail);
  493. INIT_LIST_HEAD(&dev->sio_full);
  494. nbufs = dev->n_sbufs;
  495. dev->n_sbufs = 0;
  496. spin_unlock_irqrestore(&dev->spinlock, flags);
  497. for (i = 0; i < nbufs; i++)
  498. vfree(dev->sio_bufs[i].buffer);
  499. kfree(dev->sio_bufs);
  500. dev->sio_bufs = NULL;
  501. return 0;
  502. }
  503. static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
  504. {
  505. int i;
  506. if (dev->sio_bufs != NULL)
  507. pr_err("sio_bufs already allocated\n");
  508. else {
  509. dev->sio_bufs = kcalloc(n_sbufs,
  510. 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. pr_err("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 __poll_t v4l_stk_poll(struct file *fp, poll_table *wait)
  647. {
  648. struct stk_camera *dev = video_drvdata(fp);
  649. __poll_t res = v4l2_ctrl_poll(fp, wait);
  650. poll_wait(fp, &dev->wait_frame, wait);
  651. if (!is_present(dev))
  652. return EPOLLERR;
  653. if (!list_empty(&dev->sio_full))
  654. return res | EPOLLIN | EPOLLRDNORM;
  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. pr_err("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. return 0;
  820. }
  821. static int stk_try_fmt_vid_cap(struct file *filp,
  822. struct v4l2_format *fmtd, int *idx)
  823. {
  824. int i;
  825. switch (fmtd->fmt.pix.pixelformat) {
  826. case V4L2_PIX_FMT_RGB565:
  827. case V4L2_PIX_FMT_RGB565X:
  828. case V4L2_PIX_FMT_UYVY:
  829. case V4L2_PIX_FMT_YUYV:
  830. case V4L2_PIX_FMT_SBGGR8:
  831. break;
  832. default:
  833. return -EINVAL;
  834. }
  835. for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
  836. if (fmtd->fmt.pix.width > stk_sizes[i].w)
  837. break;
  838. }
  839. if (i == ARRAY_SIZE(stk_sizes)
  840. || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
  841. < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
  842. fmtd->fmt.pix.height = stk_sizes[i-1].h;
  843. fmtd->fmt.pix.width = stk_sizes[i-1].w;
  844. if (idx)
  845. *idx = i - 1;
  846. } else {
  847. fmtd->fmt.pix.height = stk_sizes[i].h;
  848. fmtd->fmt.pix.width = stk_sizes[i].w;
  849. if (idx)
  850. *idx = i;
  851. }
  852. fmtd->fmt.pix.field = V4L2_FIELD_NONE;
  853. fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  854. if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
  855. fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
  856. else
  857. fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
  858. fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
  859. * fmtd->fmt.pix.height;
  860. return 0;
  861. }
  862. static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
  863. void *priv, struct v4l2_format *fmtd)
  864. {
  865. return stk_try_fmt_vid_cap(filp, fmtd, NULL);
  866. }
  867. static int stk_setup_format(struct stk_camera *dev)
  868. {
  869. int i = 0;
  870. int depth;
  871. if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
  872. depth = 1;
  873. else
  874. depth = 2;
  875. while (i < ARRAY_SIZE(stk_sizes) &&
  876. stk_sizes[i].m != dev->vsettings.mode)
  877. i++;
  878. if (i == ARRAY_SIZE(stk_sizes)) {
  879. pr_err("Something is broken in %s\n", __func__);
  880. return -EFAULT;
  881. }
  882. /* This registers controls some timings, not sure of what. */
  883. stk_camera_write_reg(dev, 0x001b, 0x0e);
  884. if (dev->vsettings.mode == MODE_SXGA)
  885. stk_camera_write_reg(dev, 0x001c, 0x0e);
  886. else
  887. stk_camera_write_reg(dev, 0x001c, 0x46);
  888. /*
  889. * Registers 0x0115 0x0114 are the size of each line (bytes),
  890. * regs 0x0117 0x0116 are the heigth of the image.
  891. */
  892. stk_camera_write_reg(dev, 0x0115,
  893. ((stk_sizes[i].w * depth) >> 8) & 0xff);
  894. stk_camera_write_reg(dev, 0x0114,
  895. (stk_sizes[i].w * depth) & 0xff);
  896. stk_camera_write_reg(dev, 0x0117,
  897. (stk_sizes[i].h >> 8) & 0xff);
  898. stk_camera_write_reg(dev, 0x0116,
  899. stk_sizes[i].h & 0xff);
  900. return stk_sensor_configure(dev);
  901. }
  902. static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
  903. void *priv, struct v4l2_format *fmtd)
  904. {
  905. int ret;
  906. int idx;
  907. struct stk_camera *dev = video_drvdata(filp);
  908. if (dev == NULL)
  909. return -ENODEV;
  910. if (!is_present(dev))
  911. return -ENODEV;
  912. if (is_streaming(dev))
  913. return -EBUSY;
  914. if (dev->owner)
  915. return -EBUSY;
  916. ret = stk_try_fmt_vid_cap(filp, fmtd, &idx);
  917. if (ret)
  918. return ret;
  919. dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
  920. stk_free_buffers(dev);
  921. dev->frame_size = fmtd->fmt.pix.sizeimage;
  922. dev->vsettings.mode = stk_sizes[idx].m;
  923. stk_initialise(dev);
  924. return stk_setup_format(dev);
  925. }
  926. static int stk_vidioc_reqbufs(struct file *filp,
  927. void *priv, struct v4l2_requestbuffers *rb)
  928. {
  929. struct stk_camera *dev = video_drvdata(filp);
  930. if (dev == NULL)
  931. return -ENODEV;
  932. if (rb->memory != V4L2_MEMORY_MMAP)
  933. return -EINVAL;
  934. if (is_streaming(dev)
  935. || (dev->owner && dev->owner != filp))
  936. return -EBUSY;
  937. stk_free_buffers(dev);
  938. if (rb->count == 0) {
  939. stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
  940. unset_initialised(dev);
  941. dev->owner = NULL;
  942. return 0;
  943. }
  944. dev->owner = filp;
  945. /*FIXME If they ask for zero, we must stop streaming and free */
  946. if (rb->count < 3)
  947. rb->count = 3;
  948. /* Arbitrary limit */
  949. else if (rb->count > 5)
  950. rb->count = 5;
  951. stk_allocate_buffers(dev, rb->count);
  952. rb->count = dev->n_sbufs;
  953. return 0;
  954. }
  955. static int stk_vidioc_querybuf(struct file *filp,
  956. void *priv, struct v4l2_buffer *buf)
  957. {
  958. struct stk_camera *dev = video_drvdata(filp);
  959. struct stk_sio_buffer *sbuf;
  960. if (buf->index >= dev->n_sbufs)
  961. return -EINVAL;
  962. sbuf = dev->sio_bufs + buf->index;
  963. *buf = sbuf->v4lbuf;
  964. return 0;
  965. }
  966. static int stk_vidioc_qbuf(struct file *filp,
  967. void *priv, struct v4l2_buffer *buf)
  968. {
  969. struct stk_camera *dev = video_drvdata(filp);
  970. struct stk_sio_buffer *sbuf;
  971. unsigned long flags;
  972. if (buf->memory != V4L2_MEMORY_MMAP)
  973. return -EINVAL;
  974. if (buf->index >= dev->n_sbufs)
  975. return -EINVAL;
  976. sbuf = dev->sio_bufs + buf->index;
  977. if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
  978. return 0;
  979. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
  980. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
  981. spin_lock_irqsave(&dev->spinlock, flags);
  982. list_add_tail(&sbuf->list, &dev->sio_avail);
  983. *buf = sbuf->v4lbuf;
  984. spin_unlock_irqrestore(&dev->spinlock, flags);
  985. return 0;
  986. }
  987. static int stk_vidioc_dqbuf(struct file *filp,
  988. void *priv, struct v4l2_buffer *buf)
  989. {
  990. struct stk_camera *dev = video_drvdata(filp);
  991. struct stk_sio_buffer *sbuf;
  992. unsigned long flags;
  993. int ret;
  994. if (!is_streaming(dev))
  995. return -EINVAL;
  996. if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
  997. return -EWOULDBLOCK;
  998. ret = wait_event_interruptible(dev->wait_frame,
  999. !list_empty(&dev->sio_full) || !is_present(dev));
  1000. if (ret)
  1001. return ret;
  1002. if (!is_present(dev))
  1003. return -EIO;
  1004. spin_lock_irqsave(&dev->spinlock, flags);
  1005. sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
  1006. list_del_init(&sbuf->list);
  1007. spin_unlock_irqrestore(&dev->spinlock, flags);
  1008. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
  1009. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
  1010. sbuf->v4lbuf.sequence = ++dev->sequence;
  1011. v4l2_get_timestamp(&sbuf->v4lbuf.timestamp);
  1012. *buf = sbuf->v4lbuf;
  1013. return 0;
  1014. }
  1015. static int stk_vidioc_streamon(struct file *filp,
  1016. void *priv, enum v4l2_buf_type type)
  1017. {
  1018. struct stk_camera *dev = video_drvdata(filp);
  1019. if (is_streaming(dev))
  1020. return 0;
  1021. if (dev->sio_bufs == NULL)
  1022. return -EINVAL;
  1023. dev->sequence = 0;
  1024. return stk_start_stream(dev);
  1025. }
  1026. static int stk_vidioc_streamoff(struct file *filp,
  1027. void *priv, enum v4l2_buf_type type)
  1028. {
  1029. struct stk_camera *dev = video_drvdata(filp);
  1030. unsigned long flags;
  1031. int i;
  1032. stk_stop_stream(dev);
  1033. spin_lock_irqsave(&dev->spinlock, flags);
  1034. INIT_LIST_HEAD(&dev->sio_avail);
  1035. INIT_LIST_HEAD(&dev->sio_full);
  1036. for (i = 0; i < dev->n_sbufs; i++) {
  1037. INIT_LIST_HEAD(&dev->sio_bufs[i].list);
  1038. dev->sio_bufs[i].v4lbuf.flags = 0;
  1039. }
  1040. spin_unlock_irqrestore(&dev->spinlock, flags);
  1041. return 0;
  1042. }
  1043. static int stk_vidioc_g_parm(struct file *filp,
  1044. void *priv, struct v4l2_streamparm *sp)
  1045. {
  1046. /*FIXME This is not correct */
  1047. sp->parm.capture.timeperframe.numerator = 1;
  1048. sp->parm.capture.timeperframe.denominator = 30;
  1049. sp->parm.capture.readbuffers = 2;
  1050. return 0;
  1051. }
  1052. static int stk_vidioc_enum_framesizes(struct file *filp,
  1053. void *priv, struct v4l2_frmsizeenum *frms)
  1054. {
  1055. if (frms->index >= ARRAY_SIZE(stk_sizes))
  1056. return -EINVAL;
  1057. switch (frms->pixel_format) {
  1058. case V4L2_PIX_FMT_RGB565:
  1059. case V4L2_PIX_FMT_RGB565X:
  1060. case V4L2_PIX_FMT_UYVY:
  1061. case V4L2_PIX_FMT_YUYV:
  1062. case V4L2_PIX_FMT_SBGGR8:
  1063. frms->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1064. frms->discrete.width = stk_sizes[frms->index].w;
  1065. frms->discrete.height = stk_sizes[frms->index].h;
  1066. return 0;
  1067. default: return -EINVAL;
  1068. }
  1069. }
  1070. static const struct v4l2_ctrl_ops stk_ctrl_ops = {
  1071. .s_ctrl = stk_s_ctrl,
  1072. };
  1073. static const struct v4l2_file_operations v4l_stk_fops = {
  1074. .owner = THIS_MODULE,
  1075. .open = v4l_stk_open,
  1076. .release = v4l_stk_release,
  1077. .read = v4l_stk_read,
  1078. .poll = v4l_stk_poll,
  1079. .mmap = v4l_stk_mmap,
  1080. .unlocked_ioctl = video_ioctl2,
  1081. };
  1082. static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
  1083. .vidioc_querycap = stk_vidioc_querycap,
  1084. .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
  1085. .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
  1086. .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
  1087. .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
  1088. .vidioc_enum_input = stk_vidioc_enum_input,
  1089. .vidioc_s_input = stk_vidioc_s_input,
  1090. .vidioc_g_input = stk_vidioc_g_input,
  1091. .vidioc_reqbufs = stk_vidioc_reqbufs,
  1092. .vidioc_querybuf = stk_vidioc_querybuf,
  1093. .vidioc_qbuf = stk_vidioc_qbuf,
  1094. .vidioc_dqbuf = stk_vidioc_dqbuf,
  1095. .vidioc_streamon = stk_vidioc_streamon,
  1096. .vidioc_streamoff = stk_vidioc_streamoff,
  1097. .vidioc_g_parm = stk_vidioc_g_parm,
  1098. .vidioc_enum_framesizes = stk_vidioc_enum_framesizes,
  1099. .vidioc_log_status = v4l2_ctrl_log_status,
  1100. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1101. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1102. };
  1103. static void stk_v4l_dev_release(struct video_device *vd)
  1104. {
  1105. struct stk_camera *dev = vdev_to_camera(vd);
  1106. if (dev->sio_bufs != NULL || dev->isobufs != NULL)
  1107. pr_err("We are leaking memory\n");
  1108. usb_put_intf(dev->interface);
  1109. }
  1110. static const struct video_device stk_v4l_data = {
  1111. .name = "stkwebcam",
  1112. .fops = &v4l_stk_fops,
  1113. .ioctl_ops = &v4l_stk_ioctl_ops,
  1114. .release = stk_v4l_dev_release,
  1115. };
  1116. static int stk_register_video_device(struct stk_camera *dev)
  1117. {
  1118. int err;
  1119. dev->vdev = stk_v4l_data;
  1120. dev->vdev.lock = &dev->lock;
  1121. dev->vdev.v4l2_dev = &dev->v4l2_dev;
  1122. video_set_drvdata(&dev->vdev, dev);
  1123. err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
  1124. if (err)
  1125. pr_err("v4l registration failed\n");
  1126. else
  1127. pr_info("Syntek USB2.0 Camera is now controlling device %s\n",
  1128. video_device_node_name(&dev->vdev));
  1129. return err;
  1130. }
  1131. /* USB Stuff */
  1132. static int stk_camera_probe(struct usb_interface *interface,
  1133. const struct usb_device_id *id)
  1134. {
  1135. struct v4l2_ctrl_handler *hdl;
  1136. int err = 0;
  1137. int i;
  1138. struct stk_camera *dev = NULL;
  1139. struct usb_device *udev = interface_to_usbdev(interface);
  1140. struct usb_host_interface *iface_desc;
  1141. struct usb_endpoint_descriptor *endpoint;
  1142. dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
  1143. if (dev == NULL) {
  1144. pr_err("Out of memory !\n");
  1145. return -ENOMEM;
  1146. }
  1147. err = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
  1148. if (err < 0) {
  1149. dev_err(&udev->dev, "couldn't register v4l2_device\n");
  1150. kfree(dev);
  1151. return err;
  1152. }
  1153. hdl = &dev->hdl;
  1154. v4l2_ctrl_handler_init(hdl, 3);
  1155. v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
  1156. V4L2_CID_BRIGHTNESS, 0, 0xff, 0x1, 0x60);
  1157. v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
  1158. V4L2_CID_HFLIP, 0, 1, 1, 1);
  1159. v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
  1160. V4L2_CID_VFLIP, 0, 1, 1, 1);
  1161. if (hdl->error) {
  1162. err = hdl->error;
  1163. dev_err(&udev->dev, "couldn't register control\n");
  1164. goto error;
  1165. }
  1166. dev->v4l2_dev.ctrl_handler = hdl;
  1167. spin_lock_init(&dev->spinlock);
  1168. mutex_init(&dev->lock);
  1169. init_waitqueue_head(&dev->wait_frame);
  1170. dev->first_init = 1; /* webcam LED management */
  1171. dev->udev = udev;
  1172. dev->interface = interface;
  1173. usb_get_intf(interface);
  1174. if (hflip != -1)
  1175. dev->vsettings.hflip = hflip;
  1176. else if (dmi_check_system(stk_upside_down_dmi_table))
  1177. dev->vsettings.hflip = 1;
  1178. else
  1179. dev->vsettings.hflip = 0;
  1180. if (vflip != -1)
  1181. dev->vsettings.vflip = vflip;
  1182. else if (dmi_check_system(stk_upside_down_dmi_table))
  1183. dev->vsettings.vflip = 1;
  1184. else
  1185. dev->vsettings.vflip = 0;
  1186. dev->n_sbufs = 0;
  1187. set_present(dev);
  1188. /* Set up the endpoint information
  1189. * use only the first isoc-in endpoint
  1190. * for the current alternate setting */
  1191. iface_desc = interface->cur_altsetting;
  1192. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1193. endpoint = &iface_desc->endpoint[i].desc;
  1194. if (!dev->isoc_ep
  1195. && usb_endpoint_is_isoc_in(endpoint)) {
  1196. /* we found an isoc in endpoint */
  1197. dev->isoc_ep = usb_endpoint_num(endpoint);
  1198. break;
  1199. }
  1200. }
  1201. if (!dev->isoc_ep) {
  1202. pr_err("Could not find isoc-in endpoint\n");
  1203. err = -ENODEV;
  1204. goto error;
  1205. }
  1206. dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
  1207. dev->vsettings.mode = MODE_VGA;
  1208. dev->frame_size = 640 * 480 * 2;
  1209. INIT_LIST_HEAD(&dev->sio_avail);
  1210. INIT_LIST_HEAD(&dev->sio_full);
  1211. usb_set_intfdata(interface, dev);
  1212. err = stk_register_video_device(dev);
  1213. if (err)
  1214. goto error;
  1215. return 0;
  1216. error:
  1217. v4l2_ctrl_handler_free(hdl);
  1218. v4l2_device_unregister(&dev->v4l2_dev);
  1219. kfree(dev);
  1220. return err;
  1221. }
  1222. static void stk_camera_disconnect(struct usb_interface *interface)
  1223. {
  1224. struct stk_camera *dev = usb_get_intfdata(interface);
  1225. usb_set_intfdata(interface, NULL);
  1226. unset_present(dev);
  1227. wake_up_interruptible(&dev->wait_frame);
  1228. pr_info("Syntek USB2.0 Camera release resources device %s\n",
  1229. video_device_node_name(&dev->vdev));
  1230. video_unregister_device(&dev->vdev);
  1231. v4l2_ctrl_handler_free(&dev->hdl);
  1232. v4l2_device_unregister(&dev->v4l2_dev);
  1233. kfree(dev);
  1234. }
  1235. #ifdef CONFIG_PM
  1236. static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
  1237. {
  1238. struct stk_camera *dev = usb_get_intfdata(intf);
  1239. if (is_streaming(dev)) {
  1240. stk_stop_stream(dev);
  1241. /* yes, this is ugly */
  1242. set_streaming(dev);
  1243. }
  1244. return 0;
  1245. }
  1246. static int stk_camera_resume(struct usb_interface *intf)
  1247. {
  1248. struct stk_camera *dev = usb_get_intfdata(intf);
  1249. if (!is_initialised(dev))
  1250. return 0;
  1251. unset_initialised(dev);
  1252. stk_initialise(dev);
  1253. stk_camera_write_reg(dev, 0x0, 0x49);
  1254. stk_setup_format(dev);
  1255. if (is_streaming(dev))
  1256. stk_start_stream(dev);
  1257. return 0;
  1258. }
  1259. #endif
  1260. static struct usb_driver stk_camera_driver = {
  1261. .name = "stkwebcam",
  1262. .probe = stk_camera_probe,
  1263. .disconnect = stk_camera_disconnect,
  1264. .id_table = stkwebcam_table,
  1265. #ifdef CONFIG_PM
  1266. .suspend = stk_camera_suspend,
  1267. .resume = stk_camera_resume,
  1268. #endif
  1269. };
  1270. module_usb_driver(stk_camera_driver);