Version:  2.0.40 2.2.26 2.4.37 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us