konicawc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. /*
  2. * konicawc.c - konica webcam driver
  3. *
  4. * Author: Simon Evans <spse@secret.org.uk>
  5. *
  6. * Copyright (C) 2002 Simon Evans
  7. *
  8. * Licence: GPL
  9. *
  10. * Driver for USB webcams based on Konica chipset. This
  11. * chipset is used in Intel YC76 camera.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/usb/input.h>
  18. #include "usbvideo.h"
  19. #define MAX_BRIGHTNESS 108
  20. #define MAX_CONTRAST 108
  21. #define MAX_SATURATION 108
  22. #define MAX_SHARPNESS 108
  23. #define MAX_WHITEBAL 372
  24. #define MAX_SPEED 6
  25. #define MAX_CAMERAS 1
  26. #define DRIVER_VERSION "v1.4"
  27. #define DRIVER_DESC "Konica Webcam driver"
  28. enum ctrl_req {
  29. SetWhitebal = 0x01,
  30. SetBrightness = 0x02,
  31. SetSharpness = 0x03,
  32. SetContrast = 0x04,
  33. SetSaturation = 0x05,
  34. };
  35. enum frame_sizes {
  36. SIZE_160X120 = 0,
  37. SIZE_160X136 = 1,
  38. SIZE_176X144 = 2,
  39. SIZE_320X240 = 3,
  40. };
  41. #define MAX_FRAME_SIZE SIZE_320X240
  42. static struct usbvideo *cams;
  43. #ifdef CONFIG_USB_DEBUG
  44. static int debug;
  45. #define DEBUG(n, format, arg...) \
  46. if (n <= debug) { \
  47. printk(KERN_DEBUG __FILE__ ":%s(): " format "\n", __FUNCTION__ , ## arg); \
  48. }
  49. #else
  50. #define DEBUG(n, arg...)
  51. static const int debug = 0;
  52. #endif
  53. /* Some default values for initial camera settings,
  54. can be set by modprobe */
  55. static int size;
  56. static int speed = 6; /* Speed (fps) 0 (slowest) to 6 (fastest) */
  57. static int brightness = MAX_BRIGHTNESS/2;
  58. static int contrast = MAX_CONTRAST/2;
  59. static int saturation = MAX_SATURATION/2;
  60. static int sharpness = MAX_SHARPNESS/2;
  61. static int whitebal = 3*(MAX_WHITEBAL/4);
  62. static const int spd_to_iface[] = { 1, 0, 3, 2, 4, 5, 6 };
  63. /* These FPS speeds are from the windows config box. They are
  64. * indexed on size (0-2) and speed (0-6). Divide by 3 to get the
  65. * real fps.
  66. */
  67. static const int spd_to_fps[][7] = { { 24, 40, 48, 60, 72, 80, 100 },
  68. { 24, 40, 48, 60, 72, 80, 100 },
  69. { 18, 30, 36, 45, 54, 60, 75 },
  70. { 6, 10, 12, 15, 18, 21, 25 } };
  71. struct cam_size {
  72. u16 width;
  73. u16 height;
  74. u8 cmd;
  75. };
  76. static const struct cam_size camera_sizes[] = { { 160, 120, 0x7 },
  77. { 160, 136, 0xa },
  78. { 176, 144, 0x4 },
  79. { 320, 240, 0x5 } };
  80. struct konicawc {
  81. u8 brightness; /* camera uses 0 - 9, x11 for real value */
  82. u8 contrast; /* as above */
  83. u8 saturation; /* as above */
  84. u8 sharpness; /* as above */
  85. u8 white_bal; /* 0 - 33, x11 for real value */
  86. u8 speed; /* Stored as 0 - 6, used as index in spd_to_* (above) */
  87. u8 size; /* Frame Size */
  88. int height;
  89. int width;
  90. struct urb *sts_urb[USBVIDEO_NUMSBUF];
  91. u8 sts_buf[USBVIDEO_NUMSBUF][FRAMES_PER_DESC];
  92. struct urb *last_data_urb;
  93. int lastframe;
  94. int cur_frame_size; /* number of bytes in current frame size */
  95. int maxline; /* number of lines per frame */
  96. int yplanesz; /* Number of bytes in the Y plane */
  97. unsigned int buttonsts:1;
  98. #ifdef CONFIG_INPUT
  99. struct input_dev *input;
  100. char input_physname[64];
  101. #endif
  102. };
  103. #define konicawc_set_misc(uvd, req, value, index) konicawc_ctrl_msg(uvd, USB_DIR_OUT, req, value, index, NULL, 0)
  104. #define konicawc_get_misc(uvd, req, value, index, buf, sz) konicawc_ctrl_msg(uvd, USB_DIR_IN, req, value, index, buf, sz)
  105. #define konicawc_set_value(uvd, value, index) konicawc_ctrl_msg(uvd, USB_DIR_OUT, 2, value, index, NULL, 0)
  106. static int konicawc_ctrl_msg(struct uvd *uvd, u8 dir, u8 request, u16 value, u16 index, void *buf, int len)
  107. {
  108. int retval = usb_control_msg(uvd->dev,
  109. dir ? usb_rcvctrlpipe(uvd->dev, 0) : usb_sndctrlpipe(uvd->dev, 0),
  110. request, 0x40 | dir, value, index, buf, len, 1000);
  111. return retval < 0 ? retval : 0;
  112. }
  113. static inline void konicawc_camera_on(struct uvd *uvd)
  114. {
  115. DEBUG(0, "camera on");
  116. konicawc_set_misc(uvd, 0x2, 1, 0x0b);
  117. }
  118. static inline void konicawc_camera_off(struct uvd *uvd)
  119. {
  120. DEBUG(0, "camera off");
  121. konicawc_set_misc(uvd, 0x2, 0, 0x0b);
  122. }
  123. static void konicawc_set_camera_size(struct uvd *uvd)
  124. {
  125. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  126. konicawc_set_misc(uvd, 0x2, camera_sizes[cam->size].cmd, 0x08);
  127. cam->width = camera_sizes[cam->size].width;
  128. cam->height = camera_sizes[cam->size].height;
  129. cam->yplanesz = cam->height * cam->width;
  130. cam->cur_frame_size = (cam->yplanesz * 3) / 2;
  131. cam->maxline = cam->yplanesz / 256;
  132. uvd->videosize = VIDEOSIZE(cam->width, cam->height);
  133. }
  134. static int konicawc_setup_on_open(struct uvd *uvd)
  135. {
  136. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  137. DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
  138. cam->brightness * 11);
  139. konicawc_set_value(uvd, cam->brightness, SetBrightness);
  140. DEBUG(1, "setting white balance to %d (%d)", cam->white_bal,
  141. cam->white_bal * 11);
  142. konicawc_set_value(uvd, cam->white_bal, SetWhitebal);
  143. DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
  144. cam->contrast * 11);
  145. konicawc_set_value(uvd, cam->contrast, SetContrast);
  146. DEBUG(1, "setting saturation to %d (%d)", cam->saturation,
  147. cam->saturation * 11);
  148. konicawc_set_value(uvd, cam->saturation, SetSaturation);
  149. DEBUG(1, "setting sharpness to %d (%d)", cam->sharpness,
  150. cam->sharpness * 11);
  151. konicawc_set_value(uvd, cam->sharpness, SetSharpness);
  152. konicawc_set_camera_size(uvd);
  153. cam->lastframe = -2;
  154. cam->buttonsts = 0;
  155. return 0;
  156. }
  157. static void konicawc_adjust_picture(struct uvd *uvd)
  158. {
  159. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  160. konicawc_camera_off(uvd);
  161. DEBUG(1, "new brightness: %d", uvd->vpic.brightness);
  162. uvd->vpic.brightness = (uvd->vpic.brightness > MAX_BRIGHTNESS) ? MAX_BRIGHTNESS : uvd->vpic.brightness;
  163. if(cam->brightness != uvd->vpic.brightness / 11) {
  164. cam->brightness = uvd->vpic.brightness / 11;
  165. DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
  166. cam->brightness * 11);
  167. konicawc_set_value(uvd, cam->brightness, SetBrightness);
  168. }
  169. DEBUG(1, "new contrast: %d", uvd->vpic.contrast);
  170. uvd->vpic.contrast = (uvd->vpic.contrast > MAX_CONTRAST) ? MAX_CONTRAST : uvd->vpic.contrast;
  171. if(cam->contrast != uvd->vpic.contrast / 11) {
  172. cam->contrast = uvd->vpic.contrast / 11;
  173. DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
  174. cam->contrast * 11);
  175. konicawc_set_value(uvd, cam->contrast, SetContrast);
  176. }
  177. konicawc_camera_on(uvd);
  178. }
  179. #ifdef CONFIG_INPUT
  180. static void konicawc_register_input(struct konicawc *cam, struct usb_device *dev)
  181. {
  182. struct input_dev *input_dev;
  183. int error;
  184. usb_make_path(dev, cam->input_physname, sizeof(cam->input_physname));
  185. strncat(cam->input_physname, "/input0", sizeof(cam->input_physname));
  186. cam->input = input_dev = input_allocate_device();
  187. if (!input_dev) {
  188. warn("Not enough memory for camera's input device\n");
  189. return;
  190. }
  191. input_dev->name = "Konicawc snapshot button";
  192. input_dev->phys = cam->input_physname;
  193. usb_to_input_id(dev, &input_dev->id);
  194. input_dev->cdev.dev = &dev->dev;
  195. input_dev->evbit[0] = BIT(EV_KEY);
  196. input_dev->keybit[LONG(BTN_0)] = BIT(BTN_0);
  197. input_dev->private = cam;
  198. error = input_register_device(cam->input);
  199. if (error) {
  200. warn("Failed to register camera's input device, err: %d\n",
  201. error);
  202. input_free_device(cam->input);
  203. cam->input = NULL;
  204. }
  205. }
  206. static void konicawc_unregister_input(struct konicawc *cam)
  207. {
  208. if (cam->input) {
  209. input_unregister_device(cam->input);
  210. cam->input = NULL;
  211. }
  212. }
  213. static void konicawc_report_buttonstat(struct konicawc *cam)
  214. {
  215. if (cam->input) {
  216. input_report_key(cam->input, BTN_0, cam->buttonsts);
  217. input_sync(cam->input);
  218. }
  219. }
  220. #else
  221. static inline void konicawc_register_input(struct konicawc *cam, struct usb_device *dev) { }
  222. static inline void konicawc_unregister_input(struct konicawc *cam) { }
  223. static inline void konicawc_report_buttonstat(struct konicawc *cam) { }
  224. #endif /* CONFIG_INPUT */
  225. static int konicawc_compress_iso(struct uvd *uvd, struct urb *dataurb, struct urb *stsurb)
  226. {
  227. char *cdata;
  228. int i, totlen = 0;
  229. unsigned char *status = stsurb->transfer_buffer;
  230. int keep = 0, discard = 0, bad = 0;
  231. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  232. for (i = 0; i < dataurb->number_of_packets; i++) {
  233. int button = cam->buttonsts;
  234. unsigned char sts;
  235. int n = dataurb->iso_frame_desc[i].actual_length;
  236. int st = dataurb->iso_frame_desc[i].status;
  237. cdata = dataurb->transfer_buffer +
  238. dataurb->iso_frame_desc[i].offset;
  239. /* Detect and ignore errored packets */
  240. if (st < 0) {
  241. DEBUG(1, "Data error: packet=%d. len=%d. status=%d.",
  242. i, n, st);
  243. uvd->stats.iso_err_count++;
  244. continue;
  245. }
  246. /* Detect and ignore empty packets */
  247. if (n <= 0) {
  248. uvd->stats.iso_skip_count++;
  249. continue;
  250. }
  251. /* See what the status data said about the packet */
  252. sts = *(status+stsurb->iso_frame_desc[i].offset);
  253. /* sts: 0x80-0xff: frame start with frame number (ie 0-7f)
  254. * otherwise:
  255. * bit 0 0: keep packet
  256. * 1: drop packet (padding data)
  257. *
  258. * bit 4 0 button not clicked
  259. * 1 button clicked
  260. * button is used to `take a picture' (in software)
  261. */
  262. if(sts < 0x80) {
  263. button = !!(sts & 0x40);
  264. sts &= ~0x40;
  265. }
  266. /* work out the button status, but don't do
  267. anything with it for now */
  268. if(button != cam->buttonsts) {
  269. DEBUG(2, "button: %sclicked", button ? "" : "un");
  270. cam->buttonsts = button;
  271. konicawc_report_buttonstat(cam);
  272. }
  273. if(sts == 0x01) { /* drop frame */
  274. discard++;
  275. continue;
  276. }
  277. if((sts > 0x01) && (sts < 0x80)) {
  278. info("unknown status %2.2x", sts);
  279. bad++;
  280. continue;
  281. }
  282. if(!sts && cam->lastframe == -2) {
  283. DEBUG(2, "dropping frame looking for image start");
  284. continue;
  285. }
  286. keep++;
  287. if(sts & 0x80) { /* frame start */
  288. unsigned char marker[] = { 0, 0xff, 0, 0x00 };
  289. if(cam->lastframe == -2) {
  290. DEBUG(2, "found initial image");
  291. cam->lastframe = -1;
  292. }
  293. marker[3] = sts & 0x7F;
  294. RingQueue_Enqueue(&uvd->dp, marker, 4);
  295. totlen += 4;
  296. }
  297. totlen += n; /* Little local accounting */
  298. RingQueue_Enqueue(&uvd->dp, cdata, n);
  299. }
  300. DEBUG(8, "finished: keep = %d discard = %d bad = %d added %d bytes",
  301. keep, discard, bad, totlen);
  302. return totlen;
  303. }
  304. static void resubmit_urb(struct uvd *uvd, struct urb *urb)
  305. {
  306. int i, ret;
  307. for (i = 0; i < FRAMES_PER_DESC; i++) {
  308. urb->iso_frame_desc[i].status = 0;
  309. }
  310. urb->dev = uvd->dev;
  311. urb->status = 0;
  312. ret = usb_submit_urb(urb, GFP_ATOMIC);
  313. DEBUG(3, "submitting urb of length %d", urb->transfer_buffer_length);
  314. if(ret)
  315. err("usb_submit_urb error (%d)", ret);
  316. }
  317. static void konicawc_isoc_irq(struct urb *urb)
  318. {
  319. struct uvd *uvd = urb->context;
  320. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  321. /* We don't want to do anything if we are about to be removed! */
  322. if (!CAMERA_IS_OPERATIONAL(uvd))
  323. return;
  324. if (!uvd->streaming) {
  325. DEBUG(1, "Not streaming, but interrupt!");
  326. return;
  327. }
  328. DEBUG(3, "got frame %d len = %d buflen =%d", urb->start_frame, urb->actual_length, urb->transfer_buffer_length);
  329. uvd->stats.urb_count++;
  330. if (urb->transfer_buffer_length > 32) {
  331. cam->last_data_urb = urb;
  332. return;
  333. }
  334. /* Copy the data received into ring queue */
  335. if(cam->last_data_urb) {
  336. int len = 0;
  337. if(urb->start_frame != cam->last_data_urb->start_frame)
  338. err("Lost sync on frames");
  339. else if (!urb->status && !cam->last_data_urb->status)
  340. len = konicawc_compress_iso(uvd, cam->last_data_urb, urb);
  341. resubmit_urb(uvd, cam->last_data_urb);
  342. resubmit_urb(uvd, urb);
  343. cam->last_data_urb = NULL;
  344. uvd->stats.urb_length = len;
  345. uvd->stats.data_count += len;
  346. if(len)
  347. RingQueue_WakeUpInterruptible(&uvd->dp);
  348. return;
  349. }
  350. return;
  351. }
  352. static int konicawc_start_data(struct uvd *uvd)
  353. {
  354. struct usb_device *dev = uvd->dev;
  355. int i, errFlag;
  356. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  357. int pktsz;
  358. struct usb_interface *intf;
  359. struct usb_host_interface *interface = NULL;
  360. intf = usb_ifnum_to_if(dev, uvd->iface);
  361. if (intf)
  362. interface = usb_altnum_to_altsetting(intf,
  363. spd_to_iface[cam->speed]);
  364. if (!interface)
  365. return -ENXIO;
  366. pktsz = le16_to_cpu(interface->endpoint[1].desc.wMaxPacketSize);
  367. DEBUG(1, "pktsz = %d", pktsz);
  368. if (!CAMERA_IS_OPERATIONAL(uvd)) {
  369. err("Camera is not operational");
  370. return -EFAULT;
  371. }
  372. uvd->curframe = -1;
  373. konicawc_camera_on(uvd);
  374. /* Alternate interface 1 is is the biggest frame size */
  375. i = usb_set_interface(dev, uvd->iface, uvd->ifaceAltActive);
  376. if (i < 0) {
  377. err("usb_set_interface error");
  378. uvd->last_error = i;
  379. return -EBUSY;
  380. }
  381. /* We double buffer the Iso lists */
  382. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  383. int j, k;
  384. struct urb *urb = uvd->sbuf[i].urb;
  385. urb->dev = dev;
  386. urb->context = uvd;
  387. urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp);
  388. urb->interval = 1;
  389. urb->transfer_flags = URB_ISO_ASAP;
  390. urb->transfer_buffer = uvd->sbuf[i].data;
  391. urb->complete = konicawc_isoc_irq;
  392. urb->number_of_packets = FRAMES_PER_DESC;
  393. urb->transfer_buffer_length = pktsz * FRAMES_PER_DESC;
  394. for (j=k=0; j < FRAMES_PER_DESC; j++, k += pktsz) {
  395. urb->iso_frame_desc[j].offset = k;
  396. urb->iso_frame_desc[j].length = pktsz;
  397. }
  398. urb = cam->sts_urb[i];
  399. urb->dev = dev;
  400. urb->context = uvd;
  401. urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp-1);
  402. urb->interval = 1;
  403. urb->transfer_flags = URB_ISO_ASAP;
  404. urb->transfer_buffer = cam->sts_buf[i];
  405. urb->complete = konicawc_isoc_irq;
  406. urb->number_of_packets = FRAMES_PER_DESC;
  407. urb->transfer_buffer_length = FRAMES_PER_DESC;
  408. for (j=0; j < FRAMES_PER_DESC; j++) {
  409. urb->iso_frame_desc[j].offset = j;
  410. urb->iso_frame_desc[j].length = 1;
  411. }
  412. }
  413. cam->last_data_urb = NULL;
  414. /* Submit all URBs */
  415. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  416. errFlag = usb_submit_urb(cam->sts_urb[i], GFP_KERNEL);
  417. if (errFlag)
  418. err("usb_submit_isoc(%d) ret %d", i, errFlag);
  419. errFlag = usb_submit_urb(uvd->sbuf[i].urb, GFP_KERNEL);
  420. if (errFlag)
  421. err ("usb_submit_isoc(%d) ret %d", i, errFlag);
  422. }
  423. uvd->streaming = 1;
  424. DEBUG(1, "streaming=1 video_endp=$%02x", uvd->video_endp);
  425. return 0;
  426. }
  427. static void konicawc_stop_data(struct uvd *uvd)
  428. {
  429. int i, j;
  430. struct konicawc *cam;
  431. if ((uvd == NULL) || (!uvd->streaming) || (uvd->dev == NULL))
  432. return;
  433. konicawc_camera_off(uvd);
  434. uvd->streaming = 0;
  435. cam = (struct konicawc *)uvd->user_data;
  436. cam->last_data_urb = NULL;
  437. /* Unschedule all of the iso td's */
  438. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  439. usb_kill_urb(uvd->sbuf[i].urb);
  440. usb_kill_urb(cam->sts_urb[i]);
  441. }
  442. if (!uvd->remove_pending) {
  443. /* Set packet size to 0 */
  444. j = usb_set_interface(uvd->dev, uvd->iface, uvd->ifaceAltInactive);
  445. if (j < 0) {
  446. err("usb_set_interface() error %d.", j);
  447. uvd->last_error = j;
  448. }
  449. }
  450. }
  451. static void konicawc_process_isoc(struct uvd *uvd, struct usbvideo_frame *frame)
  452. {
  453. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  454. int maxline = cam->maxline;
  455. int yplanesz = cam->yplanesz;
  456. assert(frame != NULL);
  457. DEBUG(5, "maxline = %d yplanesz = %d", maxline, yplanesz);
  458. DEBUG(3, "Frame state = %d", frame->scanstate);
  459. if(frame->scanstate == ScanState_Scanning) {
  460. int drop = 0;
  461. int curframe;
  462. int fdrops = 0;
  463. DEBUG(3, "Searching for marker, queue len = %d", RingQueue_GetLength(&uvd->dp));
  464. while(RingQueue_GetLength(&uvd->dp) >= 4) {
  465. if ((RING_QUEUE_PEEK(&uvd->dp, 0) == 0x00) &&
  466. (RING_QUEUE_PEEK(&uvd->dp, 1) == 0xff) &&
  467. (RING_QUEUE_PEEK(&uvd->dp, 2) == 0x00) &&
  468. (RING_QUEUE_PEEK(&uvd->dp, 3) < 0x80)) {
  469. curframe = RING_QUEUE_PEEK(&uvd->dp, 3);
  470. if(cam->lastframe >= 0) {
  471. fdrops = (0x80 + curframe - cam->lastframe) & 0x7F;
  472. fdrops--;
  473. if(fdrops) {
  474. info("Dropped %d frames (%d -> %d)", fdrops,
  475. cam->lastframe, curframe);
  476. }
  477. }
  478. cam->lastframe = curframe;
  479. frame->curline = 0;
  480. frame->scanstate = ScanState_Lines;
  481. RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 4);
  482. break;
  483. }
  484. RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 1);
  485. drop++;
  486. }
  487. if(drop)
  488. DEBUG(2, "dropped %d bytes looking for new frame", drop);
  489. }
  490. if(frame->scanstate == ScanState_Scanning)
  491. return;
  492. /* Try to move data from queue into frame buffer
  493. * We get data in blocks of 384 bytes made up of:
  494. * 256 Y, 64 U, 64 V.
  495. * This needs to be written out as a Y plane, a U plane and a V plane.
  496. */
  497. while ( frame->curline < maxline && (RingQueue_GetLength(&uvd->dp) >= 384)) {
  498. /* Y */
  499. RingQueue_Dequeue(&uvd->dp, frame->data + (frame->curline * 256), 256);
  500. /* U */
  501. RingQueue_Dequeue(&uvd->dp, frame->data + yplanesz + (frame->curline * 64), 64);
  502. /* V */
  503. RingQueue_Dequeue(&uvd->dp, frame->data + (5 * yplanesz)/4 + (frame->curline * 64), 64);
  504. frame->seqRead_Length += 384;
  505. frame->curline++;
  506. }
  507. /* See if we filled the frame */
  508. if (frame->curline == maxline) {
  509. DEBUG(5, "got whole frame");
  510. frame->frameState = FrameState_Done_Hold;
  511. frame->curline = 0;
  512. uvd->curframe = -1;
  513. uvd->stats.frame_num++;
  514. }
  515. }
  516. static int konicawc_find_fps(int size, int fps)
  517. {
  518. int i;
  519. fps *= 3;
  520. DEBUG(1, "konica_find_fps: size = %d fps = %d", size, fps);
  521. if(fps <= spd_to_fps[size][0])
  522. return 0;
  523. if(fps >= spd_to_fps[size][MAX_SPEED])
  524. return MAX_SPEED;
  525. for(i = 0; i < MAX_SPEED; i++) {
  526. if((fps >= spd_to_fps[size][i]) && (fps <= spd_to_fps[size][i+1])) {
  527. DEBUG(2, "fps %d between %d and %d", fps, i, i+1);
  528. if( (fps - spd_to_fps[size][i]) < (spd_to_fps[size][i+1] - fps))
  529. return i;
  530. else
  531. return i+1;
  532. }
  533. }
  534. return MAX_SPEED+1;
  535. }
  536. static int konicawc_set_video_mode(struct uvd *uvd, struct video_window *vw)
  537. {
  538. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  539. int newspeed = cam->speed;
  540. int newsize;
  541. int x = vw->width;
  542. int y = vw->height;
  543. int fps = vw->flags;
  544. if(x > 0 && y > 0) {
  545. DEBUG(2, "trying to find size %d,%d", x, y);
  546. for(newsize = 0; newsize <= MAX_FRAME_SIZE; newsize++) {
  547. if((camera_sizes[newsize].width == x) && (camera_sizes[newsize].height == y))
  548. break;
  549. }
  550. } else {
  551. newsize = cam->size;
  552. }
  553. if(newsize > MAX_FRAME_SIZE) {
  554. DEBUG(1, "couldn't find size %d,%d", x, y);
  555. return -EINVAL;
  556. }
  557. if(fps > 0) {
  558. DEBUG(1, "trying to set fps to %d", fps);
  559. newspeed = konicawc_find_fps(newsize, fps);
  560. DEBUG(1, "find_fps returned %d (%d)", newspeed, spd_to_fps[newsize][newspeed]);
  561. }
  562. if(newspeed > MAX_SPEED)
  563. return -EINVAL;
  564. DEBUG(1, "setting size to %d speed to %d", newsize, newspeed);
  565. if((newsize == cam->size) && (newspeed == cam->speed)) {
  566. DEBUG(1, "Nothing to do");
  567. return 0;
  568. }
  569. DEBUG(0, "setting to %dx%d @ %d fps", camera_sizes[newsize].width,
  570. camera_sizes[newsize].height, spd_to_fps[newsize][newspeed]/3);
  571. konicawc_stop_data(uvd);
  572. uvd->ifaceAltActive = spd_to_iface[newspeed];
  573. DEBUG(1, "new interface = %d", uvd->ifaceAltActive);
  574. cam->speed = newspeed;
  575. if(cam->size != newsize) {
  576. cam->size = newsize;
  577. konicawc_set_camera_size(uvd);
  578. }
  579. /* Flush the input queue and clear any current frame in progress */
  580. RingQueue_Flush(&uvd->dp);
  581. cam->lastframe = -2;
  582. if(uvd->curframe != -1) {
  583. uvd->frame[uvd->curframe].curline = 0;
  584. uvd->frame[uvd->curframe].seqRead_Length = 0;
  585. uvd->frame[uvd->curframe].seqRead_Index = 0;
  586. }
  587. konicawc_start_data(uvd);
  588. return 0;
  589. }
  590. static int konicawc_calculate_fps(struct uvd *uvd)
  591. {
  592. struct konicawc *cam = uvd->user_data;
  593. return spd_to_fps[cam->size][cam->speed]/3;
  594. }
  595. static void konicawc_configure_video(struct uvd *uvd)
  596. {
  597. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  598. u8 buf[2];
  599. memset(&uvd->vpic, 0, sizeof(uvd->vpic));
  600. memset(&uvd->vpic_old, 0x55, sizeof(uvd->vpic_old));
  601. RESTRICT_TO_RANGE(brightness, 0, MAX_BRIGHTNESS);
  602. RESTRICT_TO_RANGE(contrast, 0, MAX_CONTRAST);
  603. RESTRICT_TO_RANGE(saturation, 0, MAX_SATURATION);
  604. RESTRICT_TO_RANGE(sharpness, 0, MAX_SHARPNESS);
  605. RESTRICT_TO_RANGE(whitebal, 0, MAX_WHITEBAL);
  606. cam->brightness = brightness / 11;
  607. cam->contrast = contrast / 11;
  608. cam->saturation = saturation / 11;
  609. cam->sharpness = sharpness / 11;
  610. cam->white_bal = whitebal / 11;
  611. uvd->vpic.colour = 108;
  612. uvd->vpic.hue = 108;
  613. uvd->vpic.brightness = brightness;
  614. uvd->vpic.contrast = contrast;
  615. uvd->vpic.whiteness = whitebal;
  616. uvd->vpic.depth = 6;
  617. uvd->vpic.palette = VIDEO_PALETTE_YUV420P;
  618. memset(&uvd->vcap, 0, sizeof(uvd->vcap));
  619. strcpy(uvd->vcap.name, "Konica Webcam");
  620. uvd->vcap.type = VID_TYPE_CAPTURE;
  621. uvd->vcap.channels = 1;
  622. uvd->vcap.audios = 0;
  623. uvd->vcap.minwidth = camera_sizes[SIZE_160X120].width;
  624. uvd->vcap.minheight = camera_sizes[SIZE_160X120].height;
  625. uvd->vcap.maxwidth = camera_sizes[SIZE_320X240].width;
  626. uvd->vcap.maxheight = camera_sizes[SIZE_320X240].height;
  627. memset(&uvd->vchan, 0, sizeof(uvd->vchan));
  628. uvd->vchan.flags = 0 ;
  629. uvd->vchan.tuners = 0;
  630. uvd->vchan.channel = 0;
  631. uvd->vchan.type = VIDEO_TYPE_CAMERA;
  632. strcpy(uvd->vchan.name, "Camera");
  633. /* Talk to device */
  634. DEBUG(1, "device init");
  635. if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
  636. DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
  637. if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
  638. DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
  639. if(konicawc_set_misc(uvd, 0x2, 0, 0xd))
  640. DEBUG(2, "2,0,d failed");
  641. DEBUG(1, "setting initial values");
  642. }
  643. static int konicawc_probe(struct usb_interface *intf, const struct usb_device_id *devid)
  644. {
  645. struct usb_device *dev = interface_to_usbdev(intf);
  646. struct uvd *uvd = NULL;
  647. int ix, i, nas;
  648. int actInterface=-1, inactInterface=-1, maxPS=0;
  649. unsigned char video_ep = 0;
  650. DEBUG(1, "konicawc_probe(%p)", intf);
  651. /* We don't handle multi-config cameras */
  652. if (dev->descriptor.bNumConfigurations != 1)
  653. return -ENODEV;
  654. info("Konica Webcam (rev. 0x%04x)", le16_to_cpu(dev->descriptor.bcdDevice));
  655. RESTRICT_TO_RANGE(speed, 0, MAX_SPEED);
  656. /* Validate found interface: must have one ISO endpoint */
  657. nas = intf->num_altsetting;
  658. if (nas != 8) {
  659. err("Incorrect number of alternate settings (%d) for this camera!", nas);
  660. return -ENODEV;
  661. }
  662. /* Validate all alternate settings */
  663. for (ix=0; ix < nas; ix++) {
  664. const struct usb_host_interface *interface;
  665. const struct usb_endpoint_descriptor *endpoint;
  666. interface = &intf->altsetting[ix];
  667. i = interface->desc.bAlternateSetting;
  668. if (interface->desc.bNumEndpoints != 2) {
  669. err("Interface %d. has %u. endpoints!",
  670. interface->desc.bInterfaceNumber,
  671. (unsigned)(interface->desc.bNumEndpoints));
  672. return -ENODEV;
  673. }
  674. endpoint = &interface->endpoint[1].desc;
  675. DEBUG(1, "found endpoint: addr: 0x%2.2x maxps = 0x%4.4x",
  676. endpoint->bEndpointAddress, le16_to_cpu(endpoint->wMaxPacketSize));
  677. if (video_ep == 0)
  678. video_ep = endpoint->bEndpointAddress;
  679. else if (video_ep != endpoint->bEndpointAddress) {
  680. err("Alternate settings have different endpoint addresses!");
  681. return -ENODEV;
  682. }
  683. if ((endpoint->bmAttributes & 0x03) != 0x01) {
  684. err("Interface %d. has non-ISO endpoint!",
  685. interface->desc.bInterfaceNumber);
  686. return -ENODEV;
  687. }
  688. if ((endpoint->bEndpointAddress & 0x80) == 0) {
  689. err("Interface %d. has ISO OUT endpoint!",
  690. interface->desc.bInterfaceNumber);
  691. return -ENODEV;
  692. }
  693. if (le16_to_cpu(endpoint->wMaxPacketSize) == 0) {
  694. if (inactInterface < 0)
  695. inactInterface = i;
  696. else {
  697. err("More than one inactive alt. setting!");
  698. return -ENODEV;
  699. }
  700. } else {
  701. if (i == spd_to_iface[speed]) {
  702. /* This one is the requested one */
  703. actInterface = i;
  704. }
  705. }
  706. if (le16_to_cpu(endpoint->wMaxPacketSize) > maxPS)
  707. maxPS = le16_to_cpu(endpoint->wMaxPacketSize);
  708. }
  709. if(actInterface == -1) {
  710. err("Cant find required endpoint");
  711. return -ENODEV;
  712. }
  713. DEBUG(1, "Selecting requested active setting=%d. maxPS=%d.", actInterface, maxPS);
  714. uvd = usbvideo_AllocateDevice(cams);
  715. if (uvd != NULL) {
  716. struct konicawc *cam = (struct konicawc *)(uvd->user_data);
  717. /* Here uvd is a fully allocated uvd object */
  718. for(i = 0; i < USBVIDEO_NUMSBUF; i++) {
  719. cam->sts_urb[i] = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
  720. if(cam->sts_urb[i] == NULL) {
  721. while(i--) {
  722. usb_free_urb(cam->sts_urb[i]);
  723. }
  724. err("can't allocate urbs");
  725. return -ENOMEM;
  726. }
  727. }
  728. cam->speed = speed;
  729. RESTRICT_TO_RANGE(size, SIZE_160X120, SIZE_320X240);
  730. cam->width = camera_sizes[size].width;
  731. cam->height = camera_sizes[size].height;
  732. cam->size = size;
  733. uvd->flags = 0;
  734. uvd->debug = debug;
  735. uvd->dev = dev;
  736. uvd->iface = intf->altsetting->desc.bInterfaceNumber;
  737. uvd->ifaceAltInactive = inactInterface;
  738. uvd->ifaceAltActive = actInterface;
  739. uvd->video_endp = video_ep;
  740. uvd->iso_packet_len = maxPS;
  741. uvd->paletteBits = 1L << VIDEO_PALETTE_YUV420P;
  742. uvd->defaultPalette = VIDEO_PALETTE_YUV420P;
  743. uvd->canvas = VIDEOSIZE(320, 240);
  744. uvd->videosize = VIDEOSIZE(cam->width, cam->height);
  745. /* Initialize konicawc specific data */
  746. konicawc_configure_video(uvd);
  747. i = usbvideo_RegisterVideoDevice(uvd);
  748. uvd->max_frame_size = (320 * 240 * 3)/2;
  749. if (i != 0) {
  750. err("usbvideo_RegisterVideoDevice() failed.");
  751. uvd = NULL;
  752. }
  753. konicawc_register_input(cam, dev);
  754. }
  755. if (uvd) {
  756. usb_set_intfdata (intf, uvd);
  757. return 0;
  758. }
  759. return -EIO;
  760. }
  761. static void konicawc_free_uvd(struct uvd *uvd)
  762. {
  763. int i;
  764. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  765. konicawc_unregister_input(cam);
  766. for (i = 0; i < USBVIDEO_NUMSBUF; i++) {
  767. usb_free_urb(cam->sts_urb[i]);
  768. cam->sts_urb[i] = NULL;
  769. }
  770. }
  771. static struct usb_device_id id_table[] = {
  772. { USB_DEVICE(0x04c8, 0x0720) }, /* Intel YC 76 */
  773. { } /* Terminating entry */
  774. };
  775. static int __init konicawc_init(void)
  776. {
  777. struct usbvideo_cb cbTbl;
  778. info(DRIVER_DESC " " DRIVER_VERSION);
  779. memset(&cbTbl, 0, sizeof(cbTbl));
  780. cbTbl.probe = konicawc_probe;
  781. cbTbl.setupOnOpen = konicawc_setup_on_open;
  782. cbTbl.processData = konicawc_process_isoc;
  783. cbTbl.getFPS = konicawc_calculate_fps;
  784. cbTbl.setVideoMode = konicawc_set_video_mode;
  785. cbTbl.startDataPump = konicawc_start_data;
  786. cbTbl.stopDataPump = konicawc_stop_data;
  787. cbTbl.adjustPicture = konicawc_adjust_picture;
  788. cbTbl.userFree = konicawc_free_uvd;
  789. return usbvideo_register(
  790. &cams,
  791. MAX_CAMERAS,
  792. sizeof(struct konicawc),
  793. "konicawc",
  794. &cbTbl,
  795. THIS_MODULE,
  796. id_table);
  797. }
  798. static void __exit konicawc_cleanup(void)
  799. {
  800. usbvideo_Deregister(&cams);
  801. }
  802. MODULE_DEVICE_TABLE(usb, id_table);
  803. MODULE_LICENSE("GPL");
  804. MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
  805. MODULE_DESCRIPTION(DRIVER_DESC);
  806. module_param(speed, int, 0);
  807. MODULE_PARM_DESC(speed, "Initial speed: 0 (slowest) - 6 (fastest)");
  808. module_param(size, int, 0);
  809. MODULE_PARM_DESC(size, "Initial Size 0: 160x120 1: 160x136 2: 176x144 3: 320x240");
  810. module_param(brightness, int, 0);
  811. MODULE_PARM_DESC(brightness, "Initial brightness 0 - 108");
  812. module_param(contrast, int, 0);
  813. MODULE_PARM_DESC(contrast, "Initial contrast 0 - 108");
  814. module_param(saturation, int, 0);
  815. MODULE_PARM_DESC(saturation, "Initial saturation 0 - 108");
  816. module_param(sharpness, int, 0);
  817. MODULE_PARM_DESC(sharpness, "Initial brightness 0 - 108");
  818. module_param(whitebal, int, 0);
  819. MODULE_PARM_DESC(whitebal, "Initial white balance 0 - 363");
  820. #ifdef CONFIG_USB_DEBUG
  821. module_param(debug, int, S_IRUGO | S_IWUSR);
  822. MODULE_PARM_DESC(debug, "Debug level: 0-9 (default=0)");
  823. #endif
  824. module_init(konicawc_init);
  825. module_exit(konicawc_cleanup);