stk-webcam.c 33 KB

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