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