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