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