stk-webcam.c 35 KB

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