stk-webcam.c 36 KB

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