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