RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / media / video / gspca / benq.c
blobfce8d9492641d31b762bbca7411660fca2f4e45d
1 /*
2 * Benq DC E300 subdriver
4 * Copyright (C) 2009 Jean-Francois Moine (http://moinejf.free.fr)
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.
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.
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
21 #define MODULE_NAME "benq"
23 #include "gspca.h"
25 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
26 MODULE_DESCRIPTION("Benq DC E300 USB Camera Driver");
27 MODULE_LICENSE("GPL");
29 /* specific webcam descriptor */
30 struct sd {
31 struct gspca_dev gspca_dev; /* !! must be the first item */
34 /* V4L2 controls supported by the driver */
35 static const struct ctrl sd_ctrls[] = {
38 static const struct v4l2_pix_format vga_mode[] = {
39 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
40 .bytesperline = 320,
41 .sizeimage = 320 * 240 * 3 / 8 + 590,
42 .colorspace = V4L2_COLORSPACE_JPEG},
45 static void sd_isoc_irq(struct urb *urb);
47 /* -- write a register -- */
48 static void reg_w(struct gspca_dev *gspca_dev,
49 u16 value, u16 index)
51 struct usb_device *dev = gspca_dev->dev;
52 int ret;
54 if (gspca_dev->usb_err < 0)
55 return;
56 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
57 0x02,
58 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
59 value,
60 index,
61 NULL,
63 500);
64 if (ret < 0) {
65 PDEBUG(D_ERR, "reg_w err %d", ret);
66 gspca_dev->usb_err = ret;
70 /* this function is called at probe time */
71 static int sd_config(struct gspca_dev *gspca_dev,
72 const struct usb_device_id *id)
74 gspca_dev->cam.cam_mode = vga_mode;
75 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
76 gspca_dev->cam.no_urb_create = 1;
77 gspca_dev->cam.reverse_alts = 1;
78 return 0;
81 /* this function is called at probe and resume time */
82 static int sd_init(struct gspca_dev *gspca_dev)
84 return 0;
87 static int sd_isoc_init(struct gspca_dev *gspca_dev)
89 int ret;
91 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface,
92 gspca_dev->nbalt - 1);
93 if (ret < 0) {
94 err("usb_set_interface failed");
95 return ret;
97 /* reg_w(gspca_dev, 0x0003, 0x0002); */
98 return 0;
101 /* -- start the camera -- */
102 static int sd_start(struct gspca_dev *gspca_dev)
104 struct urb *urb;
105 int i, n;
107 /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
108 #if MAX_NURBS < 4
109 #error "Not enough URBs in the gspca table"
110 #endif
111 #define SD_PKT_SZ 64
112 #define SD_NPKT 32
113 for (n = 0; n < 4; n++) {
114 urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
115 if (!urb) {
116 err("usb_alloc_urb failed");
117 return -ENOMEM;
119 gspca_dev->urb[n] = urb;
120 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
121 SD_PKT_SZ * SD_NPKT,
122 GFP_KERNEL,
123 &urb->transfer_dma);
125 if (urb->transfer_buffer == NULL) {
126 err("usb_alloc_coherent failed");
127 return -ENOMEM;
129 urb->dev = gspca_dev->dev;
130 urb->context = gspca_dev;
131 urb->transfer_buffer_length = SD_PKT_SZ * SD_NPKT;
132 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
133 n & 1 ? 0x82 : 0x83);
134 urb->transfer_flags = URB_ISO_ASAP
135 | URB_NO_TRANSFER_DMA_MAP;
136 urb->interval = 1;
137 urb->complete = sd_isoc_irq;
138 urb->number_of_packets = SD_NPKT;
139 for (i = 0; i < SD_NPKT; i++) {
140 urb->iso_frame_desc[i].length = SD_PKT_SZ;
141 urb->iso_frame_desc[i].offset = SD_PKT_SZ * i;
145 return gspca_dev->usb_err;
148 static void sd_stopN(struct gspca_dev *gspca_dev)
150 reg_w(gspca_dev, 0x003c, 0x0003);
151 reg_w(gspca_dev, 0x003c, 0x0004);
152 reg_w(gspca_dev, 0x003c, 0x0005);
153 reg_w(gspca_dev, 0x003c, 0x0006);
154 reg_w(gspca_dev, 0x003c, 0x0007);
155 usb_set_interface(gspca_dev->dev, gspca_dev->iface, gspca_dev->nbalt - 1);
158 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
159 u8 *data, /* isoc packet */
160 int len) /* iso packet length */
162 /* unused */
165 /* reception of an URB */
166 static void sd_isoc_irq(struct urb *urb)
168 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
169 struct urb *urb0;
170 u8 *data;
171 int i, st;
173 PDEBUG(D_PACK, "sd isoc irq");
174 if (!gspca_dev->streaming)
175 return;
176 if (urb->status != 0) {
177 if (urb->status == -ESHUTDOWN)
178 return; /* disconnection */
179 #ifdef CONFIG_PM
180 if (gspca_dev->frozen)
181 return;
182 #endif
183 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
184 return;
187 /* if this is a control URN (ep 0x83), wait */
188 if (urb == gspca_dev->urb[0] || urb == gspca_dev->urb[2])
189 return;
191 /* scan both received URBs */
192 if (urb == gspca_dev->urb[1])
193 urb0 = gspca_dev->urb[0];
194 else
195 urb0 = gspca_dev->urb[2];
196 for (i = 0; i < urb->number_of_packets; i++) {
198 /* check the packet status and length */
199 if (urb0->iso_frame_desc[i].actual_length != SD_PKT_SZ
200 || urb->iso_frame_desc[i].actual_length != SD_PKT_SZ) {
201 PDEBUG(D_ERR, "ISOC bad lengths %d / %d",
202 urb0->iso_frame_desc[i].actual_length,
203 urb->iso_frame_desc[i].actual_length);
204 gspca_dev->last_packet_type = DISCARD_PACKET;
205 continue;
207 st = urb0->iso_frame_desc[i].status;
208 if (st == 0)
209 st = urb->iso_frame_desc[i].status;
210 if (st) {
211 PDEBUG(D_ERR,
212 "ISOC data error: [%d] status=%d",
213 i, st);
214 gspca_dev->last_packet_type = DISCARD_PACKET;
215 continue;
219 * The images are received in URBs of different endpoints
220 * (0x83 and 0x82).
221 * Image pieces in URBs of ep 0x83 are continuated in URBs of
222 * ep 0x82 of the same index.
223 * The packets in the URBs of endpoint 0x83 start with:
224 * - 80 ba/bb 00 00 = start of image followed by 'ff d8'
225 * - 04 ba/bb oo oo = image piece
226 * where 'oo oo' is the image offset
227 (not cheked)
228 * - (other -> bad frame)
229 * The images are JPEG encoded with full header and
230 * normal ff escape.
231 * The end of image ('ff d9') may occur in any URB.
232 * (not cheked)
234 data = (u8 *) urb0->transfer_buffer
235 + urb0->iso_frame_desc[i].offset;
236 if (data[0] == 0x80 && (data[1] & 0xfe) == 0xba) {
238 /* new image */
239 gspca_frame_add(gspca_dev, LAST_PACKET,
240 NULL, 0);
241 gspca_frame_add(gspca_dev, FIRST_PACKET,
242 data + 4, SD_PKT_SZ - 4);
243 } else if (data[0] == 0x04 && (data[1] & 0xfe) == 0xba) {
244 gspca_frame_add(gspca_dev, INTER_PACKET,
245 data + 4, SD_PKT_SZ - 4);
246 } else {
247 gspca_dev->last_packet_type = DISCARD_PACKET;
248 continue;
250 data = (u8 *) urb->transfer_buffer
251 + urb->iso_frame_desc[i].offset;
252 gspca_frame_add(gspca_dev, INTER_PACKET,
253 data, SD_PKT_SZ);
256 /* resubmit the URBs */
257 st = usb_submit_urb(urb0, GFP_ATOMIC);
258 if (st < 0)
259 PDEBUG(D_ERR|D_PACK, "usb_submit_urb(0) ret %d", st);
260 st = usb_submit_urb(urb, GFP_ATOMIC);
261 if (st < 0)
262 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
265 /* sub-driver description */
266 static const struct sd_desc sd_desc = {
267 .name = MODULE_NAME,
268 .ctrls = sd_ctrls,
269 .nctrls = ARRAY_SIZE(sd_ctrls),
270 .config = sd_config,
271 .init = sd_init,
272 .isoc_init = sd_isoc_init,
273 .start = sd_start,
274 .stopN = sd_stopN,
275 .pkt_scan = sd_pkt_scan,
278 /* -- module initialisation -- */
279 static const __devinitdata struct usb_device_id device_table[] = {
280 {USB_DEVICE(0x04a5, 0x3035)},
283 MODULE_DEVICE_TABLE(usb, device_table);
285 /* -- device connect -- */
286 static int sd_probe(struct usb_interface *intf,
287 const struct usb_device_id *id)
289 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
290 THIS_MODULE);
293 static struct usb_driver sd_driver = {
294 .name = MODULE_NAME,
295 .id_table = device_table,
296 .probe = sd_probe,
297 .disconnect = gspca_disconnect,
298 #ifdef CONFIG_PM
299 .suspend = gspca_suspend,
300 .resume = gspca_resume,
301 #endif
304 /* -- module insert / remove -- */
305 static int __init sd_mod_init(void)
307 int ret;
309 ret = usb_register(&sd_driver);
310 if (ret < 0)
311 return ret;
312 info("registered");
313 return 0;
315 static void __exit sd_mod_exit(void)
317 usb_deregister(&sd_driver);
318 info("deregistered");
321 module_init(sd_mod_init);
322 module_exit(sd_mod_exit);