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