stk-webcam.c 36 KB

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