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