stk-webcam.c 37 KB

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