stk-webcam.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  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 = video_device_create_file(vdev, &dev_attr_brightness);
  290. ret += video_device_create_file(vdev, &dev_attr_hflip);
  291. ret += video_device_create_file(vdev, &dev_attr_vflip);
  292. return ret;
  293. }
  294. static void stk_remove_sysfs_files(struct video_device *vdev)
  295. {
  296. video_device_remove_file(vdev, &dev_attr_brightness);
  297. video_device_remove_file(vdev, &dev_attr_hflip);
  298. video_device_remove_file(vdev, &dev_attr_vflip);
  299. }
  300. #else
  301. #define stk_create_sysfs_files(a)
  302. #define stk_remove_sysfs_files(a)
  303. #endif
  304. /* *********************************************** */
  305. /*
  306. * This function is called as an URB transfert is complete (Isochronous pipe).
  307. * So, the traitement is done in interrupt time, so it has be fast, not crash,
  308. * and not stall. Neat.
  309. */
  310. static void stk_isoc_handler(struct urb *urb)
  311. {
  312. int i;
  313. int ret;
  314. int framelen;
  315. unsigned long flags;
  316. unsigned char *fill = NULL;
  317. unsigned char *iso_buf = NULL;
  318. struct stk_camera *dev;
  319. struct stk_sio_buffer *fb;
  320. dev = (struct stk_camera *) urb->context;
  321. if (dev == NULL) {
  322. STK_ERROR("isoc_handler called with NULL device !\n");
  323. return;
  324. }
  325. if (urb->status == -ENOENT || urb->status == -ECONNRESET
  326. || urb->status == -ESHUTDOWN) {
  327. atomic_dec(&dev->urbs_used);
  328. return;
  329. }
  330. spin_lock_irqsave(&dev->spinlock, flags);
  331. if (urb->status != -EINPROGRESS && urb->status != 0) {
  332. STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
  333. goto resubmit;
  334. }
  335. if (list_empty(&dev->sio_avail)) {
  336. /*FIXME Stop streaming after a while */
  337. (void) (printk_ratelimit() &&
  338. STK_ERROR("isoc_handler without available buffer!\n"));
  339. goto resubmit;
  340. }
  341. fb = list_first_entry(&dev->sio_avail,
  342. struct stk_sio_buffer, list);
  343. fill = fb->buffer + fb->v4lbuf.bytesused;
  344. for (i = 0; i < urb->number_of_packets; i++) {
  345. if (urb->iso_frame_desc[i].status != 0) {
  346. if (urb->iso_frame_desc[i].status != -EXDEV)
  347. STK_ERROR("Frame %d has error %d\n", i,
  348. urb->iso_frame_desc[i].status);
  349. continue;
  350. }
  351. framelen = urb->iso_frame_desc[i].actual_length;
  352. iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  353. if (framelen <= 4)
  354. continue; /* no data */
  355. /*
  356. * we found something informational from there
  357. * the isoc frames have to type of headers
  358. * type1: 00 xx 00 00 or 20 xx 00 00
  359. * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
  360. * xx is a sequencer which has never been seen over 0x3f
  361. * imho data written down looks like bayer, i see similarities
  362. * after every 640 bytes
  363. */
  364. if (*iso_buf & 0x80) {
  365. framelen -= 8;
  366. iso_buf += 8;
  367. /* This marks a new frame */
  368. if (fb->v4lbuf.bytesused != 0
  369. && fb->v4lbuf.bytesused != dev->frame_size) {
  370. (void) (printk_ratelimit() &&
  371. STK_ERROR("frame %d, "
  372. "bytesused=%d, skipping\n",
  373. i, fb->v4lbuf.bytesused));
  374. fb->v4lbuf.bytesused = 0;
  375. fill = fb->buffer;
  376. } else if (fb->v4lbuf.bytesused == dev->frame_size) {
  377. list_move_tail(dev->sio_avail.next,
  378. &dev->sio_full);
  379. wake_up(&dev->wait_frame);
  380. if (list_empty(&dev->sio_avail)) {
  381. (void) (printk_ratelimit() &&
  382. STK_ERROR("No buffer available\n"));
  383. goto resubmit;
  384. }
  385. fb = list_first_entry(&dev->sio_avail,
  386. struct stk_sio_buffer, list);
  387. fb->v4lbuf.bytesused = 0;
  388. fill = fb->buffer;
  389. }
  390. } else {
  391. framelen -= 4;
  392. iso_buf += 4;
  393. }
  394. /* Our buffer is full !!! */
  395. if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
  396. (void) (printk_ratelimit() &&
  397. STK_ERROR("Frame buffer overflow, lost sync\n"));
  398. /*FIXME Do something here? */
  399. continue;
  400. }
  401. spin_unlock_irqrestore(&dev->spinlock, flags);
  402. memcpy(fill, iso_buf, framelen);
  403. spin_lock_irqsave(&dev->spinlock, flags);
  404. fill += framelen;
  405. /* New size of our buffer */
  406. fb->v4lbuf.bytesused += framelen;
  407. }
  408. resubmit:
  409. spin_unlock_irqrestore(&dev->spinlock, flags);
  410. urb->dev = dev->udev;
  411. ret = usb_submit_urb(urb, GFP_ATOMIC);
  412. if (ret != 0) {
  413. STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
  414. ret);
  415. }
  416. }
  417. /* -------------------------------------------- */
  418. static int stk_prepare_iso(struct stk_camera *dev)
  419. {
  420. void *kbuf;
  421. int i, j;
  422. struct urb *urb;
  423. struct usb_device *udev;
  424. if (dev == NULL)
  425. return -ENXIO;
  426. udev = dev->udev;
  427. if (dev->isobufs)
  428. STK_ERROR("isobufs already allocated. Bad\n");
  429. else
  430. dev->isobufs = kzalloc(MAX_ISO_BUFS * sizeof(*dev->isobufs),
  431. GFP_KERNEL);
  432. if (dev->isobufs == NULL) {
  433. STK_ERROR("Unable to allocate iso buffers\n");
  434. return -ENOMEM;
  435. }
  436. for (i = 0; i < MAX_ISO_BUFS; i++) {
  437. if (dev->isobufs[i].data == NULL) {
  438. kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
  439. if (kbuf == NULL) {
  440. STK_ERROR("Failed to allocate iso buffer %d\n",
  441. i);
  442. goto isobufs_out;
  443. }
  444. dev->isobufs[i].data = kbuf;
  445. } else
  446. STK_ERROR("isobuf data already allocated\n");
  447. if (dev->isobufs[i].urb == NULL) {
  448. urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
  449. if (urb == NULL) {
  450. STK_ERROR("Failed to allocate URB %d\n", i);
  451. goto isobufs_out;
  452. }
  453. dev->isobufs[i].urb = urb;
  454. } else {
  455. STK_ERROR("Killing URB\n");
  456. usb_kill_urb(dev->isobufs[i].urb);
  457. urb = dev->isobufs[i].urb;
  458. }
  459. urb->interval = 1;
  460. urb->dev = udev;
  461. urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
  462. urb->transfer_flags = URB_ISO_ASAP;
  463. urb->transfer_buffer = dev->isobufs[i].data;
  464. urb->transfer_buffer_length = ISO_BUFFER_SIZE;
  465. urb->complete = stk_isoc_handler;
  466. urb->context = dev;
  467. urb->start_frame = 0;
  468. urb->number_of_packets = ISO_FRAMES_PER_DESC;
  469. for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
  470. urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
  471. urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
  472. }
  473. }
  474. set_memallocd(dev);
  475. return 0;
  476. isobufs_out:
  477. for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
  478. kfree(dev->isobufs[i].data);
  479. for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
  480. usb_free_urb(dev->isobufs[i].urb);
  481. kfree(dev->isobufs);
  482. dev->isobufs = NULL;
  483. return -ENOMEM;
  484. }
  485. static void stk_clean_iso(struct stk_camera *dev)
  486. {
  487. int i;
  488. if (dev == NULL || dev->isobufs == NULL)
  489. return;
  490. for (i = 0; i < MAX_ISO_BUFS; i++) {
  491. struct urb *urb;
  492. urb = dev->isobufs[i].urb;
  493. if (urb) {
  494. if (atomic_read(&dev->urbs_used))
  495. usb_kill_urb(urb);
  496. usb_free_urb(urb);
  497. }
  498. kfree(dev->isobufs[i].data);
  499. }
  500. kfree(dev->isobufs);
  501. dev->isobufs = NULL;
  502. unset_memallocd(dev);
  503. }
  504. static int stk_setup_siobuf(struct stk_camera *dev, int index)
  505. {
  506. struct stk_sio_buffer *buf = dev->sio_bufs + index;
  507. INIT_LIST_HEAD(&buf->list);
  508. buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
  509. buf->buffer = vmalloc_user(buf->v4lbuf.length);
  510. if (buf->buffer == NULL)
  511. return -ENOMEM;
  512. buf->mapcount = 0;
  513. buf->dev = dev;
  514. buf->v4lbuf.index = index;
  515. buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  516. buf->v4lbuf.field = V4L2_FIELD_NONE;
  517. buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
  518. buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
  519. return 0;
  520. }
  521. static int stk_free_sio_buffers(struct stk_camera *dev)
  522. {
  523. int i;
  524. int nbufs;
  525. unsigned long flags;
  526. if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
  527. return 0;
  528. /*
  529. * If any buffers are mapped, we cannot free them at all.
  530. */
  531. for (i = 0; i < dev->n_sbufs; i++) {
  532. if (dev->sio_bufs[i].mapcount > 0)
  533. return -EBUSY;
  534. }
  535. /*
  536. * OK, let's do it.
  537. */
  538. spin_lock_irqsave(&dev->spinlock, flags);
  539. INIT_LIST_HEAD(&dev->sio_avail);
  540. INIT_LIST_HEAD(&dev->sio_full);
  541. nbufs = dev->n_sbufs;
  542. dev->n_sbufs = 0;
  543. spin_unlock_irqrestore(&dev->spinlock, flags);
  544. for (i = 0; i < nbufs; i++) {
  545. if (dev->sio_bufs[i].buffer != NULL)
  546. vfree(dev->sio_bufs[i].buffer);
  547. }
  548. kfree(dev->sio_bufs);
  549. dev->sio_bufs = NULL;
  550. return 0;
  551. }
  552. static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
  553. {
  554. int i;
  555. if (dev->sio_bufs != NULL)
  556. STK_ERROR("sio_bufs already allocated\n");
  557. else {
  558. dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
  559. GFP_KERNEL);
  560. if (dev->sio_bufs == NULL)
  561. return -ENOMEM;
  562. for (i = 0; i < n_sbufs; i++) {
  563. if (stk_setup_siobuf(dev, i))
  564. return (dev->n_sbufs > 1)? 0 : -ENOMEM;
  565. dev->n_sbufs = i+1;
  566. }
  567. }
  568. return 0;
  569. }
  570. static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
  571. {
  572. int err;
  573. err = stk_prepare_iso(dev);
  574. if (err) {
  575. stk_clean_iso(dev);
  576. return err;
  577. }
  578. err = stk_prepare_sio_buffers(dev, n_sbufs);
  579. if (err) {
  580. stk_free_sio_buffers(dev);
  581. return err;
  582. }
  583. return 0;
  584. }
  585. static void stk_free_buffers(struct stk_camera *dev)
  586. {
  587. stk_clean_iso(dev);
  588. stk_free_sio_buffers(dev);
  589. }
  590. /* -------------------------------------------- */
  591. /* v4l file operations */
  592. static int v4l_stk_open(struct inode *inode, struct file *fp)
  593. {
  594. struct stk_camera *dev;
  595. struct video_device *vdev;
  596. vdev = video_devdata(fp);
  597. dev = vdev_to_camera(vdev);
  598. if (dev == NULL || !is_present(dev))
  599. return -ENXIO;
  600. fp->private_data = vdev;
  601. kref_get(&dev->kref);
  602. usb_autopm_get_interface(dev->interface);
  603. return 0;
  604. }
  605. static int v4l_stk_release(struct inode *inode, struct file *fp)
  606. {
  607. struct stk_camera *dev;
  608. struct video_device *vdev;
  609. vdev = video_devdata(fp);
  610. if (vdev == NULL) {
  611. STK_ERROR("v4l_release called w/o video devdata\n");
  612. return -EFAULT;
  613. }
  614. dev = vdev_to_camera(vdev);
  615. if (dev == NULL) {
  616. STK_ERROR("v4l_release called on removed device\n");
  617. return -ENODEV;
  618. }
  619. if (dev->owner != fp) {
  620. usb_autopm_put_interface(dev->interface);
  621. kref_put(&dev->kref, stk_camera_cleanup);
  622. return 0;
  623. }
  624. stk_stop_stream(dev);
  625. stk_free_buffers(dev);
  626. dev->owner = NULL;
  627. usb_autopm_put_interface(dev->interface);
  628. kref_put(&dev->kref, stk_camera_cleanup);
  629. return 0;
  630. }
  631. static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
  632. size_t count, loff_t *f_pos)
  633. {
  634. int i;
  635. int ret;
  636. unsigned long flags;
  637. struct stk_camera *dev;
  638. struct video_device *vdev;
  639. struct stk_sio_buffer *sbuf;
  640. vdev = video_devdata(fp);
  641. if (vdev == NULL)
  642. return -EFAULT;
  643. dev = vdev_to_camera(vdev);
  644. if (dev == NULL)
  645. return -EIO;
  646. if (!is_present(dev))
  647. return -EIO;
  648. if (dev->owner && dev->owner != fp)
  649. return -EBUSY;
  650. dev->owner = fp;
  651. if (!is_streaming(dev)) {
  652. if (stk_initialise(dev)
  653. || stk_allocate_buffers(dev, 3)
  654. || stk_start_stream(dev))
  655. return -ENOMEM;
  656. spin_lock_irqsave(&dev->spinlock, flags);
  657. for (i = 0; i < dev->n_sbufs; i++) {
  658. list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
  659. dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
  660. }
  661. spin_unlock_irqrestore(&dev->spinlock, flags);
  662. }
  663. if (*f_pos == 0) {
  664. if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
  665. return -EWOULDBLOCK;
  666. ret = wait_event_interruptible(dev->wait_frame,
  667. !list_empty(&dev->sio_full) || !is_present(dev));
  668. if (ret)
  669. return ret;
  670. if (!is_present(dev))
  671. return -EIO;
  672. }
  673. if (count + *f_pos > dev->frame_size)
  674. count = dev->frame_size - *f_pos;
  675. spin_lock_irqsave(&dev->spinlock, flags);
  676. if (list_empty(&dev->sio_full)) {
  677. spin_unlock_irqrestore(&dev->spinlock, flags);
  678. STK_ERROR("BUG: No siobufs ready\n");
  679. return 0;
  680. }
  681. sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
  682. spin_unlock_irqrestore(&dev->spinlock, flags);
  683. if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
  684. return -EFAULT;
  685. *f_pos += count;
  686. if (*f_pos >= dev->frame_size) {
  687. *f_pos = 0;
  688. spin_lock_irqsave(&dev->spinlock, flags);
  689. list_move_tail(&sbuf->list, &dev->sio_avail);
  690. spin_unlock_irqrestore(&dev->spinlock, flags);
  691. }
  692. return count;
  693. }
  694. static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
  695. {
  696. struct stk_camera *dev;
  697. struct video_device *vdev;
  698. vdev = video_devdata(fp);
  699. if (vdev == NULL)
  700. return -EFAULT;
  701. dev = vdev_to_camera(vdev);
  702. if (dev == NULL)
  703. return -ENODEV;
  704. poll_wait(fp, &dev->wait_frame, wait);
  705. if (!is_present(dev))
  706. return POLLERR;
  707. if (!list_empty(&dev->sio_full))
  708. return (POLLIN | POLLRDNORM);
  709. return 0;
  710. }
  711. static void stk_v4l_vm_open(struct vm_area_struct *vma)
  712. {
  713. struct stk_sio_buffer *sbuf = vma->vm_private_data;
  714. sbuf->mapcount++;
  715. }
  716. static void stk_v4l_vm_close(struct vm_area_struct *vma)
  717. {
  718. struct stk_sio_buffer *sbuf = vma->vm_private_data;
  719. sbuf->mapcount--;
  720. if (sbuf->mapcount == 0)
  721. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
  722. }
  723. static struct vm_operations_struct stk_v4l_vm_ops = {
  724. .open = stk_v4l_vm_open,
  725. .close = stk_v4l_vm_close
  726. };
  727. static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
  728. {
  729. unsigned int i;
  730. int ret;
  731. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  732. struct stk_camera *dev;
  733. struct video_device *vdev;
  734. struct stk_sio_buffer *sbuf = NULL;
  735. if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
  736. return -EINVAL;
  737. vdev = video_devdata(fp);
  738. dev = vdev_to_camera(vdev);
  739. for (i = 0; i < dev->n_sbufs; i++) {
  740. if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
  741. sbuf = dev->sio_bufs + i;
  742. break;
  743. }
  744. }
  745. if (sbuf == NULL)
  746. return -EINVAL;
  747. ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
  748. if (ret)
  749. return ret;
  750. vma->vm_flags |= VM_DONTEXPAND;
  751. vma->vm_private_data = sbuf;
  752. vma->vm_ops = &stk_v4l_vm_ops;
  753. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
  754. stk_v4l_vm_open(vma);
  755. return 0;
  756. }
  757. /* v4l ioctl handlers */
  758. static int stk_vidioc_querycap(struct file *filp,
  759. void *priv, struct v4l2_capability *cap)
  760. {
  761. strcpy(cap->driver, "stk");
  762. strcpy(cap->card, "stk");
  763. cap->version = DRIVER_VERSION_NUM;
  764. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
  765. | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  766. return 0;
  767. }
  768. static int stk_vidioc_enum_input(struct file *filp,
  769. void *priv, struct v4l2_input *input)
  770. {
  771. if (input->index != 0)
  772. return -EINVAL;
  773. strcpy(input->name, "Syntek USB Camera");
  774. input->type = V4L2_INPUT_TYPE_CAMERA;
  775. return 0;
  776. }
  777. static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  778. {
  779. *i = 0;
  780. return 0;
  781. }
  782. static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  783. {
  784. if (i != 0)
  785. return -EINVAL;
  786. else
  787. return 0;
  788. }
  789. /* from vivi.c */
  790. static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
  791. {
  792. return 0;
  793. }
  794. /* List of all V4Lv2 controls supported by the driver */
  795. static struct v4l2_queryctrl stk_controls[] = {
  796. {
  797. .id = V4L2_CID_BRIGHTNESS,
  798. .type = V4L2_CTRL_TYPE_INTEGER,
  799. .name = "Brightness",
  800. .minimum = 0,
  801. .maximum = 0xffff,
  802. .step = 0x0100,
  803. .default_value = 0x6000,
  804. },
  805. /*TODO: get more controls to work */
  806. };
  807. static int stk_vidioc_queryctrl(struct file *filp,
  808. void *priv, struct v4l2_queryctrl *c)
  809. {
  810. int i;
  811. int nbr;
  812. nbr = ARRAY_SIZE(stk_controls);
  813. for (i = 0; i < nbr; i++) {
  814. if (stk_controls[i].id == c->id) {
  815. memcpy(c, &stk_controls[i],
  816. sizeof(struct v4l2_queryctrl));
  817. return 0;
  818. }
  819. }
  820. return -EINVAL;
  821. }
  822. static int stk_vidioc_g_ctrl(struct file *filp,
  823. void *priv, struct v4l2_control *c)
  824. {
  825. struct stk_camera *dev = priv;
  826. switch (c->id) {
  827. case V4L2_CID_BRIGHTNESS:
  828. c->value = dev->vsettings.brightness;
  829. break;
  830. default:
  831. return -EINVAL;
  832. }
  833. return 0;
  834. }
  835. static int stk_vidioc_s_ctrl(struct file *filp,
  836. void *priv, struct v4l2_control *c)
  837. {
  838. struct stk_camera *dev = priv;
  839. switch (c->id) {
  840. case V4L2_CID_BRIGHTNESS:
  841. dev->vsettings.brightness = c->value;
  842. return stk_sensor_set_brightness(dev, c->value >> 8);
  843. default:
  844. return -EINVAL;
  845. }
  846. return 0;
  847. }
  848. static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
  849. void *priv, struct v4l2_fmtdesc *fmtd)
  850. {
  851. fmtd->flags = 0;
  852. switch (fmtd->index) {
  853. case 0:
  854. fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
  855. strcpy(fmtd->description, "r5g6b5");
  856. break;
  857. case 1:
  858. fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
  859. strcpy(fmtd->description, "r5g6b5BE");
  860. break;
  861. case 2:
  862. fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
  863. strcpy(fmtd->description, "yuv4:2:2");
  864. break;
  865. case 3:
  866. fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
  867. strcpy(fmtd->description, "Raw bayer");
  868. break;
  869. case 4:
  870. fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
  871. strcpy(fmtd->description, "yuv4:2:2");
  872. break;
  873. default:
  874. return -EINVAL;
  875. }
  876. return 0;
  877. }
  878. static struct stk_size {
  879. unsigned w;
  880. unsigned h;
  881. enum stk_mode m;
  882. } stk_sizes[] = {
  883. { .w = 1280, .h = 1024, .m = MODE_SXGA, },
  884. { .w = 640, .h = 480, .m = MODE_VGA, },
  885. { .w = 352, .h = 288, .m = MODE_CIF, },
  886. { .w = 320, .h = 240, .m = MODE_QVGA, },
  887. { .w = 176, .h = 144, .m = MODE_QCIF, },
  888. };
  889. static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
  890. void *priv, struct v4l2_format *f)
  891. {
  892. struct v4l2_pix_format *pix_format = &f->fmt.pix;
  893. struct stk_camera *dev = priv;
  894. int i;
  895. for (i = 0; i < ARRAY_SIZE(stk_sizes)
  896. && stk_sizes[i].m != dev->vsettings.mode;
  897. i++);
  898. if (i == ARRAY_SIZE(stk_sizes)) {
  899. STK_ERROR("ERROR: mode invalid\n");
  900. return -EINVAL;
  901. }
  902. pix_format->width = stk_sizes[i].w;
  903. pix_format->height = stk_sizes[i].h;
  904. pix_format->field = V4L2_FIELD_NONE;
  905. pix_format->colorspace = V4L2_COLORSPACE_SRGB;
  906. pix_format->priv = 0;
  907. pix_format->pixelformat = dev->vsettings.palette;
  908. if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
  909. pix_format->bytesperline = pix_format->width;
  910. else
  911. pix_format->bytesperline = 2 * pix_format->width;
  912. pix_format->sizeimage = pix_format->bytesperline
  913. * pix_format->height;
  914. return 0;
  915. }
  916. static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
  917. void *priv, struct v4l2_format *fmtd)
  918. {
  919. int i;
  920. switch (fmtd->fmt.pix.pixelformat) {
  921. case V4L2_PIX_FMT_RGB565:
  922. case V4L2_PIX_FMT_RGB565X:
  923. case V4L2_PIX_FMT_UYVY:
  924. case V4L2_PIX_FMT_YUYV:
  925. case V4L2_PIX_FMT_SBGGR8:
  926. break;
  927. default:
  928. return -EINVAL;
  929. }
  930. for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
  931. if (fmtd->fmt.pix.width > stk_sizes[i].w)
  932. break;
  933. }
  934. if (i == ARRAY_SIZE(stk_sizes)
  935. || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
  936. < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
  937. fmtd->fmt.pix.height = stk_sizes[i-1].h;
  938. fmtd->fmt.pix.width = stk_sizes[i-1].w;
  939. fmtd->fmt.pix.priv = i - 1;
  940. } else {
  941. fmtd->fmt.pix.height = stk_sizes[i].h;
  942. fmtd->fmt.pix.width = stk_sizes[i].w;
  943. fmtd->fmt.pix.priv = i;
  944. }
  945. fmtd->fmt.pix.field = V4L2_FIELD_NONE;
  946. fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  947. if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
  948. fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
  949. else
  950. fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
  951. fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
  952. * fmtd->fmt.pix.height;
  953. return 0;
  954. }
  955. static int stk_setup_format(struct stk_camera *dev)
  956. {
  957. int i = 0;
  958. int depth;
  959. if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
  960. depth = 1;
  961. else
  962. depth = 2;
  963. while (stk_sizes[i].m != dev->vsettings.mode
  964. && i < ARRAY_SIZE(stk_sizes))
  965. i++;
  966. if (i == ARRAY_SIZE(stk_sizes)) {
  967. STK_ERROR("Something is broken in %s\n", __func__);
  968. return -EFAULT;
  969. }
  970. /* This registers controls some timings, not sure of what. */
  971. stk_camera_write_reg(dev, 0x001b, 0x0e);
  972. if (dev->vsettings.mode == MODE_SXGA)
  973. stk_camera_write_reg(dev, 0x001c, 0x0e);
  974. else
  975. stk_camera_write_reg(dev, 0x001c, 0x46);
  976. /*
  977. * Registers 0x0115 0x0114 are the size of each line (bytes),
  978. * regs 0x0117 0x0116 are the heigth of the image.
  979. */
  980. stk_camera_write_reg(dev, 0x0115,
  981. ((stk_sizes[i].w * depth) >> 8) & 0xff);
  982. stk_camera_write_reg(dev, 0x0114,
  983. (stk_sizes[i].w * depth) & 0xff);
  984. stk_camera_write_reg(dev, 0x0117,
  985. (stk_sizes[i].h >> 8) & 0xff);
  986. stk_camera_write_reg(dev, 0x0116,
  987. stk_sizes[i].h & 0xff);
  988. return stk_sensor_configure(dev);
  989. }
  990. static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
  991. void *priv, struct v4l2_format *fmtd)
  992. {
  993. int ret;
  994. struct stk_camera *dev = priv;
  995. if (dev == NULL)
  996. return -ENODEV;
  997. if (!is_present(dev))
  998. return -ENODEV;
  999. if (is_streaming(dev))
  1000. return -EBUSY;
  1001. if (dev->owner && dev->owner != filp)
  1002. return -EBUSY;
  1003. ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
  1004. if (ret)
  1005. return ret;
  1006. dev->owner = filp;
  1007. dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
  1008. stk_free_buffers(dev);
  1009. dev->frame_size = fmtd->fmt.pix.sizeimage;
  1010. dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
  1011. stk_initialise(dev);
  1012. return stk_setup_format(dev);
  1013. }
  1014. static int stk_vidioc_reqbufs(struct file *filp,
  1015. void *priv, struct v4l2_requestbuffers *rb)
  1016. {
  1017. struct stk_camera *dev = priv;
  1018. if (dev == NULL)
  1019. return -ENODEV;
  1020. if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1021. return -EINVAL;
  1022. if (rb->memory != V4L2_MEMORY_MMAP)
  1023. return -EINVAL;
  1024. if (is_streaming(dev)
  1025. || (dev->owner && dev->owner != filp))
  1026. return -EBUSY;
  1027. dev->owner = filp;
  1028. /*FIXME If they ask for zero, we must stop streaming and free */
  1029. if (rb->count < 3)
  1030. rb->count = 3;
  1031. /* Arbitrary limit */
  1032. else if (rb->count > 5)
  1033. rb->count = 5;
  1034. stk_allocate_buffers(dev, rb->count);
  1035. rb->count = dev->n_sbufs;
  1036. return 0;
  1037. }
  1038. static int stk_vidioc_querybuf(struct file *filp,
  1039. void *priv, struct v4l2_buffer *buf)
  1040. {
  1041. int index;
  1042. struct stk_camera *dev = priv;
  1043. struct stk_sio_buffer *sbuf;
  1044. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1045. return -EINVAL;
  1046. index = buf->index;
  1047. if (index < 0 || index >= dev->n_sbufs)
  1048. return -EINVAL;
  1049. sbuf = dev->sio_bufs + buf->index;
  1050. *buf = sbuf->v4lbuf;
  1051. return 0;
  1052. }
  1053. static int stk_vidioc_qbuf(struct file *filp,
  1054. void *priv, struct v4l2_buffer *buf)
  1055. {
  1056. struct stk_camera *dev = priv;
  1057. struct stk_sio_buffer *sbuf;
  1058. unsigned long flags;
  1059. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1060. return -EINVAL;
  1061. if (buf->memory != V4L2_MEMORY_MMAP)
  1062. return -EINVAL;
  1063. if (buf->index < 0 || buf->index >= dev->n_sbufs)
  1064. return -EINVAL;
  1065. sbuf = dev->sio_bufs + buf->index;
  1066. if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
  1067. return 0;
  1068. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
  1069. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
  1070. spin_lock_irqsave(&dev->spinlock, flags);
  1071. list_add_tail(&sbuf->list, &dev->sio_avail);
  1072. *buf = sbuf->v4lbuf;
  1073. spin_unlock_irqrestore(&dev->spinlock, flags);
  1074. return 0;
  1075. }
  1076. static int stk_vidioc_dqbuf(struct file *filp,
  1077. void *priv, struct v4l2_buffer *buf)
  1078. {
  1079. struct stk_camera *dev = priv;
  1080. struct stk_sio_buffer *sbuf;
  1081. unsigned long flags;
  1082. int ret;
  1083. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  1084. || !is_streaming(dev))
  1085. return -EINVAL;
  1086. if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
  1087. return -EWOULDBLOCK;
  1088. ret = wait_event_interruptible(dev->wait_frame,
  1089. !list_empty(&dev->sio_full) || !is_present(dev));
  1090. if (ret)
  1091. return ret;
  1092. if (!is_present(dev))
  1093. return -EIO;
  1094. spin_lock_irqsave(&dev->spinlock, flags);
  1095. sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
  1096. list_del_init(&sbuf->list);
  1097. spin_unlock_irqrestore(&dev->spinlock, flags);
  1098. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
  1099. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
  1100. sbuf->v4lbuf.sequence = ++dev->sequence;
  1101. do_gettimeofday(&sbuf->v4lbuf.timestamp);
  1102. *buf = sbuf->v4lbuf;
  1103. return 0;
  1104. }
  1105. static int stk_vidioc_streamon(struct file *filp,
  1106. void *priv, enum v4l2_buf_type type)
  1107. {
  1108. struct stk_camera *dev = priv;
  1109. if (is_streaming(dev))
  1110. return 0;
  1111. if (dev->sio_bufs == NULL)
  1112. return -EINVAL;
  1113. dev->sequence = 0;
  1114. return stk_start_stream(dev);
  1115. }
  1116. static int stk_vidioc_streamoff(struct file *filp,
  1117. void *priv, enum v4l2_buf_type type)
  1118. {
  1119. struct stk_camera *dev = priv;
  1120. unsigned long flags;
  1121. int i;
  1122. stk_stop_stream(dev);
  1123. spin_lock_irqsave(&dev->spinlock, flags);
  1124. INIT_LIST_HEAD(&dev->sio_avail);
  1125. INIT_LIST_HEAD(&dev->sio_full);
  1126. for (i = 0; i < dev->n_sbufs; i++) {
  1127. INIT_LIST_HEAD(&dev->sio_bufs[i].list);
  1128. dev->sio_bufs[i].v4lbuf.flags = 0;
  1129. }
  1130. spin_unlock_irqrestore(&dev->spinlock, flags);
  1131. return 0;
  1132. }
  1133. static int stk_vidioc_g_parm(struct file *filp,
  1134. void *priv, struct v4l2_streamparm *sp)
  1135. {
  1136. if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1137. return -EINVAL;
  1138. sp->parm.capture.capability = 0;
  1139. sp->parm.capture.capturemode = 0;
  1140. /*FIXME This is not correct */
  1141. sp->parm.capture.timeperframe.numerator = 1;
  1142. sp->parm.capture.timeperframe.denominator = 30;
  1143. sp->parm.capture.readbuffers = 2;
  1144. sp->parm.capture.extendedmode = 0;
  1145. return 0;
  1146. }
  1147. static struct file_operations v4l_stk_fops = {
  1148. .owner = THIS_MODULE,
  1149. .open = v4l_stk_open,
  1150. .release = v4l_stk_release,
  1151. .read = v4l_stk_read,
  1152. .poll = v4l_stk_poll,
  1153. .mmap = v4l_stk_mmap,
  1154. .ioctl = video_ioctl2,
  1155. #ifdef CONFIG_COMPAT
  1156. .compat_ioctl = v4l_compat_ioctl32,
  1157. #endif
  1158. .llseek = no_llseek
  1159. };
  1160. static void stk_v4l_dev_release(struct video_device *vd)
  1161. {
  1162. }
  1163. static struct video_device stk_v4l_data = {
  1164. .name = "stkwebcam",
  1165. .type = VFL_TYPE_GRABBER,
  1166. .type2 = VID_TYPE_CAPTURE,
  1167. .minor = -1,
  1168. .tvnorms = V4L2_STD_UNKNOWN,
  1169. .current_norm = V4L2_STD_UNKNOWN,
  1170. .fops = &v4l_stk_fops,
  1171. .release = stk_v4l_dev_release,
  1172. .vidioc_querycap = stk_vidioc_querycap,
  1173. .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
  1174. .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
  1175. .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
  1176. .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
  1177. .vidioc_enum_input = stk_vidioc_enum_input,
  1178. .vidioc_s_input = stk_vidioc_s_input,
  1179. .vidioc_g_input = stk_vidioc_g_input,
  1180. .vidioc_s_std = stk_vidioc_s_std,
  1181. .vidioc_reqbufs = stk_vidioc_reqbufs,
  1182. .vidioc_querybuf = stk_vidioc_querybuf,
  1183. .vidioc_qbuf = stk_vidioc_qbuf,
  1184. .vidioc_dqbuf = stk_vidioc_dqbuf,
  1185. .vidioc_streamon = stk_vidioc_streamon,
  1186. .vidioc_streamoff = stk_vidioc_streamoff,
  1187. .vidioc_queryctrl = stk_vidioc_queryctrl,
  1188. .vidioc_g_ctrl = stk_vidioc_g_ctrl,
  1189. .vidioc_s_ctrl = stk_vidioc_s_ctrl,
  1190. .vidioc_g_parm = stk_vidioc_g_parm,
  1191. };
  1192. static int stk_register_video_device(struct stk_camera *dev)
  1193. {
  1194. int err;
  1195. dev->vdev = stk_v4l_data;
  1196. dev->vdev.debug = debug;
  1197. dev->vdev.parent = &dev->interface->dev;
  1198. dev->vdev.priv = dev;
  1199. err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
  1200. if (err)
  1201. STK_ERROR("v4l registration failed\n");
  1202. else
  1203. STK_INFO("Syntek USB2.0 Camera is now controlling video device"
  1204. " /dev/video%d\n", dev->vdev.minor);
  1205. return err;
  1206. }
  1207. /* USB Stuff */
  1208. static int stk_camera_probe(struct usb_interface *interface,
  1209. const struct usb_device_id *id)
  1210. {
  1211. int i;
  1212. int err;
  1213. struct stk_camera *dev = NULL;
  1214. struct usb_device *udev = interface_to_usbdev(interface);
  1215. struct usb_host_interface *iface_desc;
  1216. struct usb_endpoint_descriptor *endpoint;
  1217. dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
  1218. if (dev == NULL) {
  1219. STK_ERROR("Out of memory !\n");
  1220. return -ENOMEM;
  1221. }
  1222. kref_init(&dev->kref);
  1223. spin_lock_init(&dev->spinlock);
  1224. init_waitqueue_head(&dev->wait_frame);
  1225. dev->udev = udev;
  1226. dev->interface = interface;
  1227. usb_get_intf(interface);
  1228. dev->vsettings.vflip = vflip;
  1229. dev->vsettings.hflip = hflip;
  1230. dev->n_sbufs = 0;
  1231. set_present(dev);
  1232. /* Set up the endpoint information
  1233. * use only the first isoc-in endpoint
  1234. * for the current alternate setting */
  1235. iface_desc = interface->cur_altsetting;
  1236. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1237. endpoint = &iface_desc->endpoint[i].desc;
  1238. if (!dev->isoc_ep
  1239. && ((endpoint->bEndpointAddress
  1240. & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
  1241. && ((endpoint->bmAttributes
  1242. & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
  1243. /* we found an isoc in endpoint */
  1244. dev->isoc_ep = (endpoint->bEndpointAddress & 0xF);
  1245. break;
  1246. }
  1247. }
  1248. if (!dev->isoc_ep) {
  1249. STK_ERROR("Could not find isoc-in endpoint");
  1250. kref_put(&dev->kref, stk_camera_cleanup);
  1251. return -ENODEV;
  1252. }
  1253. dev->vsettings.brightness = 0x7fff;
  1254. dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
  1255. dev->vsettings.mode = MODE_VGA;
  1256. dev->frame_size = 640 * 480 * 2;
  1257. INIT_LIST_HEAD(&dev->sio_avail);
  1258. INIT_LIST_HEAD(&dev->sio_full);
  1259. usb_set_intfdata(interface, dev);
  1260. err = stk_register_video_device(dev);
  1261. if (err) {
  1262. kref_put(&dev->kref, stk_camera_cleanup);
  1263. return err;
  1264. }
  1265. stk_create_sysfs_files(&dev->vdev);
  1266. usb_autopm_enable(dev->interface);
  1267. return 0;
  1268. }
  1269. static void stk_camera_disconnect(struct usb_interface *interface)
  1270. {
  1271. struct stk_camera *dev = usb_get_intfdata(interface);
  1272. usb_set_intfdata(interface, NULL);
  1273. unset_present(dev);
  1274. wake_up_interruptible(&dev->wait_frame);
  1275. stk_remove_sysfs_files(&dev->vdev);
  1276. kref_put(&dev->kref, stk_camera_cleanup);
  1277. }
  1278. #ifdef CONFIG_PM
  1279. static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
  1280. {
  1281. struct stk_camera *dev = usb_get_intfdata(intf);
  1282. if (is_streaming(dev)) {
  1283. stk_stop_stream(dev);
  1284. /* yes, this is ugly */
  1285. set_streaming(dev);
  1286. }
  1287. return 0;
  1288. }
  1289. static int stk_camera_resume(struct usb_interface *intf)
  1290. {
  1291. struct stk_camera *dev = usb_get_intfdata(intf);
  1292. if (!is_initialised(dev))
  1293. return 0;
  1294. unset_initialised(dev);
  1295. stk_initialise(dev);
  1296. stk_setup_format(dev);
  1297. if (is_streaming(dev))
  1298. stk_start_stream(dev);
  1299. return 0;
  1300. }
  1301. #endif
  1302. static struct usb_driver stk_camera_driver = {
  1303. .name = "stkwebcam",
  1304. .probe = stk_camera_probe,
  1305. .disconnect = stk_camera_disconnect,
  1306. .id_table = stkwebcam_table,
  1307. #ifdef CONFIG_PM
  1308. .suspend = stk_camera_suspend,
  1309. .resume = stk_camera_resume,
  1310. #endif
  1311. };
  1312. static int __init stk_camera_init(void)
  1313. {
  1314. int result;
  1315. result = usb_register(&stk_camera_driver);
  1316. if (result)
  1317. STK_ERROR("usb_register failed ! Error number %d\n", result);
  1318. return result;
  1319. }
  1320. static void __exit stk_camera_exit(void)
  1321. {
  1322. usb_deregister(&stk_camera_driver);
  1323. }
  1324. module_init(stk_camera_init);
  1325. module_exit(stk_camera_exit);