blob: a09c4709d6139b9616e79804b39f0d1aa982ac1e [file] [log] [blame]
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -03001/*
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
21#define MODULE_NAME "benq"
22
23#include "gspca.h"
24
25MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
26MODULE_DESCRIPTION("Benq DC E300 USB Camera Driver");
27MODULE_LICENSE("GPL");
28
29/* specific webcam descriptor */
30struct sd {
31 struct gspca_dev gspca_dev; /* !! must be the first item */
32};
33
34/* V4L2 controls supported by the driver */
Marton Nemeth7e64dc42009-12-30 09:12:41 -030035static const struct ctrl sd_ctrls[] = {
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -030036};
37
38static 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},
43};
44
45static void sd_isoc_irq(struct urb *urb);
46
47/* -- write a register -- */
48static void reg_w(struct gspca_dev *gspca_dev,
49 u16 value, u16 index)
50{
51 struct usb_device *dev = gspca_dev->dev;
52 int ret;
53
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,
62 0,
63 500);
64 if (ret < 0) {
Jean-François Moine8dbf3e72010-09-25 03:19:33 -030065 err("reg_w err %d", ret);
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -030066 gspca_dev->usb_err = ret;
67 }
68}
69
70/* this function is called at probe time */
71static int sd_config(struct gspca_dev *gspca_dev,
72 const struct usb_device_id *id)
73{
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;
79}
80
81/* this function is called at probe and resume time */
82static int sd_init(struct gspca_dev *gspca_dev)
83{
84 return 0;
85}
86
87static int sd_isoc_init(struct gspca_dev *gspca_dev)
88{
89 int ret;
90
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;
96 }
97/* reg_w(gspca_dev, 0x0003, 0x0002); */
98 return 0;
99}
100
101/* -- start the camera -- */
102static int sd_start(struct gspca_dev *gspca_dev)
103{
104 struct urb *urb;
105 int i, n;
106
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;
118 }
119 gspca_dev->urb[n] = urb;
Daniel Mack997ea582010-04-12 13:17:25 +0200120 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -0300121 SD_PKT_SZ * SD_NPKT,
122 GFP_KERNEL,
123 &urb->transfer_dma);
124
125 if (urb->transfer_buffer == NULL) {
Daniel Mack997ea582010-04-12 13:17:25 +0200126 err("usb_alloc_coherent failed");
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -0300127 return -ENOMEM;
128 }
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;
142 }
143 }
144
145 return gspca_dev->usb_err;
146}
147
148static void sd_stopN(struct gspca_dev *gspca_dev)
149{
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);
Jean-François Moine780e3122010-10-19 04:29:10 -0300155 usb_set_interface(gspca_dev->dev, gspca_dev->iface,
156 gspca_dev->nbalt - 1);
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -0300157}
158
159static void sd_pkt_scan(struct gspca_dev *gspca_dev,
160 u8 *data, /* isoc packet */
161 int len) /* iso packet length */
162{
163 /* unused */
164}
165
166/* reception of an URB */
167static void sd_isoc_irq(struct urb *urb)
168{
169 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
170 struct urb *urb0;
171 u8 *data;
172 int i, st;
173
174 PDEBUG(D_PACK, "sd isoc irq");
175 if (!gspca_dev->streaming)
176 return;
177 if (urb->status != 0) {
178 if (urb->status == -ESHUTDOWN)
179 return; /* disconnection */
180#ifdef CONFIG_PM
181 if (gspca_dev->frozen)
182 return;
183#endif
Jean-François Moine8dbf3e72010-09-25 03:19:33 -0300184 err("urb status: %d", urb->status);
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -0300185 return;
186 }
187
188 /* if this is a control URN (ep 0x83), wait */
189 if (urb == gspca_dev->urb[0] || urb == gspca_dev->urb[2])
190 return;
191
192 /* scan both received URBs */
193 if (urb == gspca_dev->urb[1])
194 urb0 = gspca_dev->urb[0];
195 else
196 urb0 = gspca_dev->urb[2];
197 for (i = 0; i < urb->number_of_packets; i++) {
198
199 /* check the packet status and length */
200 if (urb0->iso_frame_desc[i].actual_length != SD_PKT_SZ
201 || urb->iso_frame_desc[i].actual_length != SD_PKT_SZ) {
202 PDEBUG(D_ERR, "ISOC bad lengths %d / %d",
203 urb0->iso_frame_desc[i].actual_length,
204 urb->iso_frame_desc[i].actual_length);
205 gspca_dev->last_packet_type = DISCARD_PACKET;
206 continue;
207 }
208 st = urb0->iso_frame_desc[i].status;
209 if (st == 0)
210 st = urb->iso_frame_desc[i].status;
211 if (st) {
Jean-François Moine8dbf3e72010-09-25 03:19:33 -0300212 err("ISOC data error: [%d] status=%d",
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -0300213 i, st);
214 gspca_dev->last_packet_type = DISCARD_PACKET;
215 continue;
216 }
217
218 /*
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)
233 */
234 data = (u8 *) urb0->transfer_buffer
235 + urb0->iso_frame_desc[i].offset;
236 if (data[0] == 0x80 && (data[1] & 0xfe) == 0xba) {
237
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;
249 }
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);
254 }
255
256 /* resubmit the URBs */
257 st = usb_submit_urb(urb0, GFP_ATOMIC);
258 if (st < 0)
Jean-François Moine8dbf3e72010-09-25 03:19:33 -0300259 err("usb_submit_urb(0) ret %d", st);
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -0300260 st = usb_submit_urb(urb, GFP_ATOMIC);
261 if (st < 0)
Jean-François Moine8dbf3e72010-09-25 03:19:33 -0300262 err("usb_submit_urb() ret %d", st);
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -0300263}
264
265/* sub-driver description */
266static 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,
276};
277
278/* -- module initialisation -- */
Jean-François Moine95c967c2011-01-13 05:20:29 -0300279static const struct usb_device_id device_table[] = {
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -0300280 {USB_DEVICE(0x04a5, 0x3035)},
281 {}
282};
283MODULE_DEVICE_TABLE(usb, device_table);
284
285/* -- device connect -- */
286static int sd_probe(struct usb_interface *intf,
287 const struct usb_device_id *id)
288{
289 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
290 THIS_MODULE);
291}
292
293static 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
302};
303
304/* -- module insert / remove -- */
305static int __init sd_mod_init(void)
306{
Jean-François Moine81298882010-09-25 03:20:45 -0300307 return usb_register(&sd_driver);
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -0300308}
309static void __exit sd_mod_exit(void)
310{
311 usb_deregister(&sd_driver);
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -0300312}
313
314module_init(sd_mod_init);
315module_exit(sd_mod_exit);