benq.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Benq DC E300 subdriver
  3. *
  4. * Copyright (C) 2009 Jean-Francois Moine (http://moinejf.free.fr)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #define MODULE_NAME "benq"
  22. #include "gspca.h"
  23. MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
  24. MODULE_DESCRIPTION("Benq DC E300 USB Camera Driver");
  25. MODULE_LICENSE("GPL");
  26. /* specific webcam descriptor */
  27. struct sd {
  28. struct gspca_dev gspca_dev; /* !! must be the first item */
  29. };
  30. /* V4L2 controls supported by the driver */
  31. static const struct ctrl sd_ctrls[] = {
  32. };
  33. static const struct v4l2_pix_format vga_mode[] = {
  34. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  35. .bytesperline = 320,
  36. .sizeimage = 320 * 240 * 3 / 8 + 590,
  37. .colorspace = V4L2_COLORSPACE_JPEG},
  38. };
  39. static void sd_isoc_irq(struct urb *urb);
  40. /* -- write a register -- */
  41. static void reg_w(struct gspca_dev *gspca_dev,
  42. u16 value, u16 index)
  43. {
  44. struct usb_device *dev = gspca_dev->dev;
  45. int ret;
  46. if (gspca_dev->usb_err < 0)
  47. return;
  48. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  49. 0x02,
  50. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  51. value,
  52. index,
  53. NULL,
  54. 0,
  55. 500);
  56. if (ret < 0) {
  57. pr_err("reg_w err %d\n", ret);
  58. gspca_dev->usb_err = ret;
  59. }
  60. }
  61. /* this function is called at probe time */
  62. static int sd_config(struct gspca_dev *gspca_dev,
  63. const struct usb_device_id *id)
  64. {
  65. gspca_dev->cam.cam_mode = vga_mode;
  66. gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
  67. gspca_dev->cam.no_urb_create = 1;
  68. gspca_dev->cam.reverse_alts = 1;
  69. return 0;
  70. }
  71. /* this function is called at probe and resume time */
  72. static int sd_init(struct gspca_dev *gspca_dev)
  73. {
  74. return 0;
  75. }
  76. /* -- start the camera -- */
  77. static int sd_start(struct gspca_dev *gspca_dev)
  78. {
  79. struct urb *urb;
  80. int i, n;
  81. /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
  82. #if MAX_NURBS < 4
  83. #error "Not enough URBs in the gspca table"
  84. #endif
  85. #define SD_PKT_SZ 64
  86. #define SD_NPKT 32
  87. for (n = 0; n < 4; n++) {
  88. urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
  89. if (!urb) {
  90. pr_err("usb_alloc_urb failed\n");
  91. return -ENOMEM;
  92. }
  93. gspca_dev->urb[n] = urb;
  94. urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
  95. SD_PKT_SZ * SD_NPKT,
  96. GFP_KERNEL,
  97. &urb->transfer_dma);
  98. if (urb->transfer_buffer == NULL) {
  99. pr_err("usb_alloc_coherent failed\n");
  100. return -ENOMEM;
  101. }
  102. urb->dev = gspca_dev->dev;
  103. urb->context = gspca_dev;
  104. urb->transfer_buffer_length = SD_PKT_SZ * SD_NPKT;
  105. urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
  106. n & 1 ? 0x82 : 0x83);
  107. urb->transfer_flags = URB_ISO_ASAP
  108. | URB_NO_TRANSFER_DMA_MAP;
  109. urb->interval = 1;
  110. urb->complete = sd_isoc_irq;
  111. urb->number_of_packets = SD_NPKT;
  112. for (i = 0; i < SD_NPKT; i++) {
  113. urb->iso_frame_desc[i].length = SD_PKT_SZ;
  114. urb->iso_frame_desc[i].offset = SD_PKT_SZ * i;
  115. }
  116. }
  117. return gspca_dev->usb_err;
  118. }
  119. static void sd_stopN(struct gspca_dev *gspca_dev)
  120. {
  121. reg_w(gspca_dev, 0x003c, 0x0003);
  122. reg_w(gspca_dev, 0x003c, 0x0004);
  123. reg_w(gspca_dev, 0x003c, 0x0005);
  124. reg_w(gspca_dev, 0x003c, 0x0006);
  125. reg_w(gspca_dev, 0x003c, 0x0007);
  126. usb_set_interface(gspca_dev->dev, gspca_dev->iface,
  127. gspca_dev->nbalt - 1);
  128. }
  129. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  130. u8 *data, /* isoc packet */
  131. int len) /* iso packet length */
  132. {
  133. /* unused */
  134. }
  135. /* reception of an URB */
  136. static void sd_isoc_irq(struct urb *urb)
  137. {
  138. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  139. struct urb *urb0;
  140. u8 *data;
  141. int i, st;
  142. PDEBUG(D_PACK, "sd isoc irq");
  143. if (!gspca_dev->streaming)
  144. return;
  145. if (urb->status != 0) {
  146. if (urb->status == -ESHUTDOWN)
  147. return; /* disconnection */
  148. #ifdef CONFIG_PM
  149. if (gspca_dev->frozen)
  150. return;
  151. #endif
  152. pr_err("urb status: %d\n", urb->status);
  153. return;
  154. }
  155. /* if this is a control URN (ep 0x83), wait */
  156. if (urb == gspca_dev->urb[0] || urb == gspca_dev->urb[2])
  157. return;
  158. /* scan both received URBs */
  159. if (urb == gspca_dev->urb[1])
  160. urb0 = gspca_dev->urb[0];
  161. else
  162. urb0 = gspca_dev->urb[2];
  163. for (i = 0; i < urb->number_of_packets; i++) {
  164. /* check the packet status and length */
  165. if (urb0->iso_frame_desc[i].actual_length != SD_PKT_SZ
  166. || urb->iso_frame_desc[i].actual_length != SD_PKT_SZ) {
  167. PDEBUG(D_ERR, "ISOC bad lengths %d / %d",
  168. urb0->iso_frame_desc[i].actual_length,
  169. urb->iso_frame_desc[i].actual_length);
  170. gspca_dev->last_packet_type = DISCARD_PACKET;
  171. continue;
  172. }
  173. st = urb0->iso_frame_desc[i].status;
  174. if (st == 0)
  175. st = urb->iso_frame_desc[i].status;
  176. if (st) {
  177. pr_err("ISOC data error: [%d] status=%d\n",
  178. i, st);
  179. gspca_dev->last_packet_type = DISCARD_PACKET;
  180. continue;
  181. }
  182. /*
  183. * The images are received in URBs of different endpoints
  184. * (0x83 and 0x82).
  185. * Image pieces in URBs of ep 0x83 are continuated in URBs of
  186. * ep 0x82 of the same index.
  187. * The packets in the URBs of endpoint 0x83 start with:
  188. * - 80 ba/bb 00 00 = start of image followed by 'ff d8'
  189. * - 04 ba/bb oo oo = image piece
  190. * where 'oo oo' is the image offset
  191. (not cheked)
  192. * - (other -> bad frame)
  193. * The images are JPEG encoded with full header and
  194. * normal ff escape.
  195. * The end of image ('ff d9') may occur in any URB.
  196. * (not cheked)
  197. */
  198. data = (u8 *) urb0->transfer_buffer
  199. + urb0->iso_frame_desc[i].offset;
  200. if (data[0] == 0x80 && (data[1] & 0xfe) == 0xba) {
  201. /* new image */
  202. gspca_frame_add(gspca_dev, LAST_PACKET,
  203. NULL, 0);
  204. gspca_frame_add(gspca_dev, FIRST_PACKET,
  205. data + 4, SD_PKT_SZ - 4);
  206. } else if (data[0] == 0x04 && (data[1] & 0xfe) == 0xba) {
  207. gspca_frame_add(gspca_dev, INTER_PACKET,
  208. data + 4, SD_PKT_SZ - 4);
  209. } else {
  210. gspca_dev->last_packet_type = DISCARD_PACKET;
  211. continue;
  212. }
  213. data = (u8 *) urb->transfer_buffer
  214. + urb->iso_frame_desc[i].offset;
  215. gspca_frame_add(gspca_dev, INTER_PACKET,
  216. data, SD_PKT_SZ);
  217. }
  218. /* resubmit the URBs */
  219. st = usb_submit_urb(urb0, GFP_ATOMIC);
  220. if (st < 0)
  221. pr_err("usb_submit_urb(0) ret %d\n", st);
  222. st = usb_submit_urb(urb, GFP_ATOMIC);
  223. if (st < 0)
  224. pr_err("usb_submit_urb() ret %d\n", st);
  225. }
  226. /* sub-driver description */
  227. static const struct sd_desc sd_desc = {
  228. .name = MODULE_NAME,
  229. .ctrls = sd_ctrls,
  230. .nctrls = ARRAY_SIZE(sd_ctrls),
  231. .config = sd_config,
  232. .init = sd_init,
  233. .start = sd_start,
  234. .stopN = sd_stopN,
  235. .pkt_scan = sd_pkt_scan,
  236. };
  237. /* -- module initialisation -- */
  238. static const struct usb_device_id device_table[] = {
  239. {USB_DEVICE(0x04a5, 0x3035)},
  240. {}
  241. };
  242. MODULE_DEVICE_TABLE(usb, device_table);
  243. /* -- device connect -- */
  244. static int sd_probe(struct usb_interface *intf,
  245. const struct usb_device_id *id)
  246. {
  247. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  248. THIS_MODULE);
  249. }
  250. static struct usb_driver sd_driver = {
  251. .name = MODULE_NAME,
  252. .id_table = device_table,
  253. .probe = sd_probe,
  254. .disconnect = gspca_disconnect,
  255. #ifdef CONFIG_PM
  256. .suspend = gspca_suspend,
  257. .resume = gspca_resume,
  258. #endif
  259. };
  260. module_usb_driver(sd_driver);