1 /*
2  * Abilis Systems Single DVB-T Receiver
3  * Copyright (C) 2008 Pierrick Hascoet <pierrick.hascoet@abilis.com>
4  * Copyright (C) 2010 Devin Heitmueller <dheitmueller@kernellabs.com>
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, or (at your option)
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/mm.h>
24 #include <linux/usb.h>
25 
26 #include "as102_drv.h"
27 #include "as102_usb_drv.h"
28 #include "as102_fw.h"
29 
30 static void as102_usb_disconnect(struct usb_interface *interface);
31 static int as102_usb_probe(struct usb_interface *interface,
32 			   const struct usb_device_id *id);
33 
34 static int as102_usb_start_stream(struct as102_dev_t *dev);
35 static void as102_usb_stop_stream(struct as102_dev_t *dev);
36 
37 static int as102_open(struct inode *inode, struct file *file);
38 static int as102_release(struct inode *inode, struct file *file);
39 
40 static struct usb_device_id as102_usb_id_table[] = {
41 	{ USB_DEVICE(AS102_USB_DEVICE_VENDOR_ID, AS102_USB_DEVICE_PID_0001) },
42 	{ USB_DEVICE(PCTV_74E_USB_VID, PCTV_74E_USB_PID) },
43 	{ USB_DEVICE(ELGATO_EYETV_DTT_USB_VID, ELGATO_EYETV_DTT_USB_PID) },
44 	{ USB_DEVICE(NBOX_DVBT_DONGLE_USB_VID, NBOX_DVBT_DONGLE_USB_PID) },
45 	{ USB_DEVICE(SKY_IT_DIGITAL_KEY_USB_VID, SKY_IT_DIGITAL_KEY_USB_PID) },
46 	{ } /* Terminating entry */
47 };
48 
49 /* Note that this table must always have the same number of entries as the
50    as102_usb_id_table struct */
51 static const char * const as102_device_names[] = {
52 	AS102_REFERENCE_DESIGN,
53 	AS102_PCTV_74E,
54 	AS102_ELGATO_EYETV_DTT_NAME,
55 	AS102_NBOX_DVBT_DONGLE_NAME,
56 	AS102_SKY_IT_DIGITAL_KEY_NAME,
57 	NULL /* Terminating entry */
58 };
59 
60 struct usb_driver as102_usb_driver = {
61 	.name		= DRIVER_FULL_NAME,
62 	.probe		= as102_usb_probe,
63 	.disconnect	= as102_usb_disconnect,
64 	.id_table	= as102_usb_id_table
65 };
66 
67 static const struct file_operations as102_dev_fops = {
68 	.owner		= THIS_MODULE,
69 	.open		= as102_open,
70 	.release	= as102_release,
71 };
72 
73 static struct usb_class_driver as102_usb_class_driver = {
74 	.name		= "aton2-%d",
75 	.fops		= &as102_dev_fops,
76 	.minor_base	= AS102_DEVICE_MAJOR,
77 };
78 
as102_usb_xfer_cmd(struct as10x_bus_adapter_t * bus_adap,unsigned char * send_buf,int send_buf_len,unsigned char * recv_buf,int recv_buf_len)79 static int as102_usb_xfer_cmd(struct as10x_bus_adapter_t *bus_adap,
80 			      unsigned char *send_buf, int send_buf_len,
81 			      unsigned char *recv_buf, int recv_buf_len)
82 {
83 	int ret = 0;
84 	ENTER();
85 
86 	if (send_buf != NULL) {
87 		ret = usb_control_msg(bus_adap->usb_dev,
88 				      usb_sndctrlpipe(bus_adap->usb_dev, 0),
89 				      AS102_USB_DEVICE_TX_CTRL_CMD,
90 				      USB_DIR_OUT | USB_TYPE_VENDOR |
91 				      USB_RECIP_DEVICE,
92 				      bus_adap->cmd_xid, /* value */
93 				      0, /* index */
94 				      send_buf, send_buf_len,
95 				      USB_CTRL_SET_TIMEOUT /* 200 */);
96 		if (ret < 0) {
97 			dprintk(debug, "usb_control_msg(send) failed, err %i\n",
98 					ret);
99 			return ret;
100 		}
101 
102 		if (ret != send_buf_len) {
103 			dprintk(debug, "only wrote %d of %d bytes\n",
104 					ret, send_buf_len);
105 			return -1;
106 		}
107 	}
108 
109 	if (recv_buf != NULL) {
110 #ifdef TRACE
111 		dprintk(debug, "want to read: %d bytes\n", recv_buf_len);
112 #endif
113 		ret = usb_control_msg(bus_adap->usb_dev,
114 				      usb_rcvctrlpipe(bus_adap->usb_dev, 0),
115 				      AS102_USB_DEVICE_RX_CTRL_CMD,
116 				      USB_DIR_IN | USB_TYPE_VENDOR |
117 				      USB_RECIP_DEVICE,
118 				      bus_adap->cmd_xid, /* value */
119 				      0, /* index */
120 				      recv_buf, recv_buf_len,
121 				      USB_CTRL_GET_TIMEOUT /* 200 */);
122 		if (ret < 0) {
123 			dprintk(debug, "usb_control_msg(recv) failed, err %i\n",
124 					ret);
125 			return ret;
126 		}
127 #ifdef TRACE
128 		dprintk(debug, "read %d bytes\n", recv_buf_len);
129 #endif
130 	}
131 
132 	LEAVE();
133 	return ret;
134 }
135 
as102_send_ep1(struct as10x_bus_adapter_t * bus_adap,unsigned char * send_buf,int send_buf_len,int swap32)136 static int as102_send_ep1(struct as10x_bus_adapter_t *bus_adap,
137 			  unsigned char *send_buf,
138 			  int send_buf_len,
139 			  int swap32)
140 {
141 	int ret = 0, actual_len;
142 
143 	ret = usb_bulk_msg(bus_adap->usb_dev,
144 			   usb_sndbulkpipe(bus_adap->usb_dev, 1),
145 			   send_buf, send_buf_len, &actual_len, 200);
146 	if (ret) {
147 		dprintk(debug, "usb_bulk_msg(send) failed, err %i\n", ret);
148 		return ret;
149 	}
150 
151 	if (actual_len != send_buf_len) {
152 		dprintk(debug, "only wrote %d of %d bytes\n",
153 				actual_len, send_buf_len);
154 		return -1;
155 	}
156 	return ret ? ret : actual_len;
157 }
158 
as102_read_ep2(struct as10x_bus_adapter_t * bus_adap,unsigned char * recv_buf,int recv_buf_len)159 static int as102_read_ep2(struct as10x_bus_adapter_t *bus_adap,
160 		   unsigned char *recv_buf, int recv_buf_len)
161 {
162 	int ret = 0, actual_len;
163 
164 	if (recv_buf == NULL)
165 		return -EINVAL;
166 
167 	ret = usb_bulk_msg(bus_adap->usb_dev,
168 			   usb_rcvbulkpipe(bus_adap->usb_dev, 2),
169 			   recv_buf, recv_buf_len, &actual_len, 200);
170 	if (ret) {
171 		dprintk(debug, "usb_bulk_msg(recv) failed, err %i\n", ret);
172 		return ret;
173 	}
174 
175 	if (actual_len != recv_buf_len) {
176 		dprintk(debug, "only read %d of %d bytes\n",
177 				actual_len, recv_buf_len);
178 		return -1;
179 	}
180 	return ret ? ret : actual_len;
181 }
182 
183 struct as102_priv_ops_t as102_priv_ops = {
184 	.upload_fw_pkt	= as102_send_ep1,
185 	.xfer_cmd	= as102_usb_xfer_cmd,
186 	.as102_read_ep2	= as102_read_ep2,
187 	.start_stream	= as102_usb_start_stream,
188 	.stop_stream	= as102_usb_stop_stream,
189 };
190 
as102_submit_urb_stream(struct as102_dev_t * dev,struct urb * urb)191 static int as102_submit_urb_stream(struct as102_dev_t *dev, struct urb *urb)
192 {
193 	int err;
194 
195 	usb_fill_bulk_urb(urb,
196 			  dev->bus_adap.usb_dev,
197 			  usb_rcvbulkpipe(dev->bus_adap.usb_dev, 0x2),
198 			  urb->transfer_buffer,
199 			  AS102_USB_BUF_SIZE,
200 			  as102_urb_stream_irq,
201 			  dev);
202 
203 	err = usb_submit_urb(urb, GFP_ATOMIC);
204 	if (err)
205 		dprintk(debug, "%s: usb_submit_urb failed\n", __func__);
206 
207 	return err;
208 }
209 
as102_urb_stream_irq(struct urb * urb)210 void as102_urb_stream_irq(struct urb *urb)
211 {
212 	struct as102_dev_t *as102_dev = urb->context;
213 
214 	if (urb->actual_length > 0) {
215 		dvb_dmx_swfilter(&as102_dev->dvb_dmx,
216 				 urb->transfer_buffer,
217 				 urb->actual_length);
218 	} else {
219 		if (urb->actual_length == 0)
220 			memset(urb->transfer_buffer, 0, AS102_USB_BUF_SIZE);
221 	}
222 
223 	/* is not stopped, re-submit urb */
224 	if (as102_dev->streaming)
225 		as102_submit_urb_stream(as102_dev, urb);
226 }
227 
as102_free_usb_stream_buffer(struct as102_dev_t * dev)228 static void as102_free_usb_stream_buffer(struct as102_dev_t *dev)
229 {
230 	int i;
231 
232 	ENTER();
233 
234 	for (i = 0; i < MAX_STREAM_URB; i++)
235 		usb_free_urb(dev->stream_urb[i]);
236 
237 	usb_free_coherent(dev->bus_adap.usb_dev,
238 			MAX_STREAM_URB * AS102_USB_BUF_SIZE,
239 			dev->stream,
240 			dev->dma_addr);
241 	LEAVE();
242 }
243 
as102_alloc_usb_stream_buffer(struct as102_dev_t * dev)244 static int as102_alloc_usb_stream_buffer(struct as102_dev_t *dev)
245 {
246 	int i, ret = 0;
247 
248 	ENTER();
249 
250 	dev->stream = usb_alloc_coherent(dev->bus_adap.usb_dev,
251 				       MAX_STREAM_URB * AS102_USB_BUF_SIZE,
252 				       GFP_KERNEL,
253 				       &dev->dma_addr);
254 	if (!dev->stream) {
255 		dprintk(debug, "%s: usb_buffer_alloc failed\n", __func__);
256 		return -ENOMEM;
257 	}
258 
259 	memset(dev->stream, 0, MAX_STREAM_URB * AS102_USB_BUF_SIZE);
260 
261 	/* init urb buffers */
262 	for (i = 0; i < MAX_STREAM_URB; i++) {
263 		struct urb *urb;
264 
265 		urb = usb_alloc_urb(0, GFP_ATOMIC);
266 		if (urb == NULL) {
267 			dprintk(debug, "%s: usb_alloc_urb failed\n", __func__);
268 			as102_free_usb_stream_buffer(dev);
269 			return -ENOMEM;
270 		}
271 
272 		urb->transfer_buffer = dev->stream + (i * AS102_USB_BUF_SIZE);
273 		urb->transfer_buffer_length = AS102_USB_BUF_SIZE;
274 
275 		dev->stream_urb[i] = urb;
276 	}
277 	LEAVE();
278 	return ret;
279 }
280 
as102_usb_stop_stream(struct as102_dev_t * dev)281 static void as102_usb_stop_stream(struct as102_dev_t *dev)
282 {
283 	int i;
284 
285 	for (i = 0; i < MAX_STREAM_URB; i++)
286 		usb_kill_urb(dev->stream_urb[i]);
287 }
288 
as102_usb_start_stream(struct as102_dev_t * dev)289 static int as102_usb_start_stream(struct as102_dev_t *dev)
290 {
291 	int i, ret = 0;
292 
293 	for (i = 0; i < MAX_STREAM_URB; i++) {
294 		ret = as102_submit_urb_stream(dev, dev->stream_urb[i]);
295 		if (ret) {
296 			as102_usb_stop_stream(dev);
297 			return ret;
298 		}
299 	}
300 
301 	return 0;
302 }
303 
as102_usb_release(struct kref * kref)304 static void as102_usb_release(struct kref *kref)
305 {
306 	struct as102_dev_t *as102_dev;
307 
308 	ENTER();
309 
310 	as102_dev = container_of(kref, struct as102_dev_t, kref);
311 	if (as102_dev != NULL) {
312 		usb_put_dev(as102_dev->bus_adap.usb_dev);
313 		kfree(as102_dev);
314 	}
315 
316 	LEAVE();
317 }
318 
as102_usb_disconnect(struct usb_interface * intf)319 static void as102_usb_disconnect(struct usb_interface *intf)
320 {
321 	struct as102_dev_t *as102_dev;
322 
323 	ENTER();
324 
325 	/* extract as102_dev_t from usb_device private data */
326 	as102_dev = usb_get_intfdata(intf);
327 
328 	/* unregister dvb layer */
329 	as102_dvb_unregister(as102_dev);
330 
331 	/* free usb buffers */
332 	as102_free_usb_stream_buffer(as102_dev);
333 
334 	usb_set_intfdata(intf, NULL);
335 
336 	/* usb unregister device */
337 	usb_deregister_dev(intf, &as102_usb_class_driver);
338 
339 	/* decrement usage counter */
340 	kref_put(&as102_dev->kref, as102_usb_release);
341 
342 	pr_info("%s: device has been disconnected\n", DRIVER_NAME);
343 
344 	LEAVE();
345 }
346 
as102_usb_probe(struct usb_interface * intf,const struct usb_device_id * id)347 static int as102_usb_probe(struct usb_interface *intf,
348 			   const struct usb_device_id *id)
349 {
350 	int ret;
351 	struct as102_dev_t *as102_dev;
352 	int i;
353 
354 	ENTER();
355 
356 	/* This should never actually happen */
357 	if ((sizeof(as102_usb_id_table) / sizeof(struct usb_device_id)) !=
358 	    (sizeof(as102_device_names) / sizeof(const char *))) {
359 		pr_err("Device names table invalid size");
360 		return -EINVAL;
361 	}
362 
363 	as102_dev = kzalloc(sizeof(struct as102_dev_t), GFP_KERNEL);
364 	if (as102_dev == NULL) {
365 		err("%s: kzalloc failed", __func__);
366 		return -ENOMEM;
367 	}
368 
369 	/* Assign the user-friendly device name */
370 	for (i = 0; i < (sizeof(as102_usb_id_table) /
371 			 sizeof(struct usb_device_id)); i++) {
372 		if (id == &as102_usb_id_table[i])
373 			as102_dev->name = as102_device_names[i];
374 	}
375 
376 	if (as102_dev->name == NULL)
377 		as102_dev->name = "Unknown AS102 device";
378 
379 	/* set private callback functions */
380 	as102_dev->bus_adap.ops = &as102_priv_ops;
381 
382 	/* init cmd token for usb bus */
383 	as102_dev->bus_adap.cmd = &as102_dev->bus_adap.token.usb.c;
384 	as102_dev->bus_adap.rsp = &as102_dev->bus_adap.token.usb.r;
385 
386 	/* init kernel device reference */
387 	kref_init(&as102_dev->kref);
388 
389 	/* store as102 device to usb_device private data */
390 	usb_set_intfdata(intf, (void *) as102_dev);
391 
392 	/* store in as102 device the usb_device pointer */
393 	as102_dev->bus_adap.usb_dev = usb_get_dev(interface_to_usbdev(intf));
394 
395 	/* we can register the device now, as it is ready */
396 	ret = usb_register_dev(intf, &as102_usb_class_driver);
397 	if (ret < 0) {
398 		/* something prevented us from registering this driver */
399 		err("%s: usb_register_dev() failed (errno = %d)",
400 		    __func__, ret);
401 		goto failed;
402 	}
403 
404 	pr_info("%s: device has been detected\n", DRIVER_NAME);
405 
406 	/* request buffer allocation for streaming */
407 	ret = as102_alloc_usb_stream_buffer(as102_dev);
408 	if (ret != 0)
409 		goto failed;
410 
411 	/* register dvb layer */
412 	ret = as102_dvb_register(as102_dev);
413 
414 	LEAVE();
415 	return ret;
416 
417 failed:
418 	usb_set_intfdata(intf, NULL);
419 	kfree(as102_dev);
420 	return ret;
421 }
422 
as102_open(struct inode * inode,struct file * file)423 static int as102_open(struct inode *inode, struct file *file)
424 {
425 	int ret = 0, minor = 0;
426 	struct usb_interface *intf = NULL;
427 	struct as102_dev_t *dev = NULL;
428 
429 	ENTER();
430 
431 	/* read minor from inode */
432 	minor = iminor(inode);
433 
434 	/* fetch device from usb interface */
435 	intf = usb_find_interface(&as102_usb_driver, minor);
436 	if (intf == NULL) {
437 		pr_err("%s: can't find device for minor %d\n",
438 		       __func__, minor);
439 		ret = -ENODEV;
440 		goto exit;
441 	}
442 
443 	/* get our device */
444 	dev = usb_get_intfdata(intf);
445 	if (dev == NULL) {
446 		ret = -EFAULT;
447 		goto exit;
448 	}
449 
450 	/* save our device object in the file's private structure */
451 	file->private_data = dev;
452 
453 	/* increment our usage count for the device */
454 	kref_get(&dev->kref);
455 
456 exit:
457 	LEAVE();
458 	return ret;
459 }
460 
as102_release(struct inode * inode,struct file * file)461 static int as102_release(struct inode *inode, struct file *file)
462 {
463 	int ret = 0;
464 	struct as102_dev_t *dev = NULL;
465 
466 	ENTER();
467 
468 	dev = file->private_data;
469 	if (dev != NULL) {
470 		/* decrement the count on our device */
471 		kref_put(&dev->kref, as102_usb_release);
472 	}
473 
474 	LEAVE();
475 	return ret;
476 }
477 
478 MODULE_DEVICE_TABLE(usb, as102_usb_id_table);
479