V4L/DVB (4029): [PATCH] Genpix 8PSK->USB driver (Take 2)
[linux-2.6] / drivers / media / dvb / dvb-usb / gp8psk.c
1 /* DVB USB compliant Linux driver for the
2  *  - GENPIX 8pks/qpsk USB2.0 DVB-S module
3  *
4  * Copyright (C) 2006 Alan Nisota (alannisota@gmail.com)
5  *
6  * Thanks to GENPIX for the sample code used to implement this module.
7  *
8  * This module is based off the vp7045 and vp702x modules
9  *
10  *      This program is free software; you can redistribute it and/or modify it
11  *      under the terms of the GNU General Public License as published by the Free
12  *      Software Foundation, version 2.
13  *
14  * see Documentation/dvb/README.dvb-usb for more information
15  */
16 #include "gp8psk.h"
17
18 /* debug */
19 static char bcm4500_firmware[] = "dvb-usb-gp8psk-02.fw";
20 int dvb_usb_gp8psk_debug;
21 module_param_named(debug,dvb_usb_gp8psk_debug, int, 0644);
22 MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))." DVB_USB_DEBUG_STATUS);
23
24 int gp8psk_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value, u16 index, u8 *b, int blen)
25 {
26         int ret = 0,try = 0;
27
28         if ((ret = mutex_lock_interruptible(&d->usb_mutex)))
29                 return ret;
30
31         while (ret >= 0 && ret != blen && try < 3) {
32                 ret = usb_control_msg(d->udev,
33                         usb_rcvctrlpipe(d->udev,0),
34                         req,
35                         USB_TYPE_VENDOR | USB_DIR_IN,
36                         value,index,b,blen,
37                         2000);
38                 deb_info("reading number %d (ret: %d)\n",try,ret);
39                 try++;
40         }
41
42         if (ret < 0 || ret != blen) {
43                 warn("usb in operation failed.");
44                 ret = -EIO;
45         } else
46                 ret = 0;
47
48         deb_xfer("in: req. %x, val: %x, ind: %x, buffer: ",req,value,index);
49         debug_dump(b,blen,deb_xfer);
50
51         mutex_unlock(&d->usb_mutex);
52
53         return ret;
54 }
55
56 int gp8psk_usb_out_op(struct dvb_usb_device *d, u8 req, u16 value,
57                              u16 index, u8 *b, int blen)
58 {
59         int ret;
60
61         deb_xfer("out: req. %x, val: %x, ind: %x, buffer: ",req,value,index);
62         debug_dump(b,blen,deb_xfer);
63
64         if ((ret = mutex_lock_interruptible(&d->usb_mutex)))
65                 return ret;
66
67         if (usb_control_msg(d->udev,
68                         usb_sndctrlpipe(d->udev,0),
69                         req,
70                         USB_TYPE_VENDOR | USB_DIR_OUT,
71                         value,index,b,blen,
72                         2000) != blen) {
73                 warn("usb out operation failed.");
74                 ret = -EIO;
75         } else
76                 ret = 0;
77         mutex_unlock(&d->usb_mutex);
78
79         return ret;
80 }
81
82 static int gp8psk_load_bcm4500fw(struct dvb_usb_device *d)
83 {
84         int ret;
85         const struct firmware *fw = NULL;
86         u8 *ptr, *buf;
87         if ((ret = request_firmware(&fw, bcm4500_firmware,
88                                         &d->udev->dev)) != 0) {
89                 err("did not find the bcm4500 firmware file. (%s) "
90                         "Please see linux/Documentation/dvb/ for more details on firmware-problems. (%d)",
91                         bcm4500_firmware,ret);
92                 return ret;
93         }
94
95         if (gp8psk_usb_out_op(d, LOAD_BCM4500,1,0,NULL, 0)) {
96                 release_firmware(fw);
97                 return -EINVAL;
98         }
99
100         info("downloaidng bcm4500 firmware from file '%s'",bcm4500_firmware);
101
102         ptr = fw->data;
103         buf = (u8 *) kmalloc(512, GFP_KERNEL | GFP_DMA);
104
105         while (ptr[0] != 0xff) {
106                 u16 buflen = ptr[0] + 4;
107                 if (ptr + buflen >= fw->data + fw->size) {
108                         err("failed to load bcm4500 firmware.");
109                         release_firmware(fw);
110                         kfree(buf);
111                         return -EINVAL;
112                 }
113                 memcpy(buf, ptr, buflen);
114                 if (dvb_usb_generic_write(d, buf, buflen)) {
115                         err("failed to load bcm4500 firmware.");
116                         release_firmware(fw);
117                         kfree(buf);
118                         return -EIO;
119                 }
120                 ptr += buflen;
121         }
122         release_firmware(fw);
123         kfree(buf);
124         return 0;
125 }
126
127 static int gp8psk_power_ctrl(struct dvb_usb_device *d, int onoff)
128 {
129         u8 status, buf;
130         if (onoff) {
131                 gp8psk_usb_in_op(d, GET_8PSK_CONFIG,0,0,&status,1);
132                 if (! (status & 0x01))  /* started */
133                         if (gp8psk_usb_in_op(d, BOOT_8PSK, 1, 0, &buf, 1))
134                                 return -EINVAL;
135
136                 if (! (status & 0x02)) /* BCM4500 firmware loaded */
137                         if(gp8psk_load_bcm4500fw(d))
138                                 return EINVAL;
139
140                 if (! (status & 0x04)) /* LNB Power */
141                         if (gp8psk_usb_in_op(d, START_INTERSIL, 1, 0,
142                                         &buf, 1))
143                                 return EINVAL;
144
145                 /* Set DVB mode */
146                 if(gp8psk_usb_out_op(d, SET_DVB_MODE, 1, 0, NULL, 0))
147                         return -EINVAL;
148                 gp8psk_usb_in_op(d, GET_8PSK_CONFIG,0,0,&status,1);
149         } else {
150                 /* Turn off LNB power */
151                 if (gp8psk_usb_in_op(d, START_INTERSIL, 0, 0, &buf, 1))
152                         return EINVAL;
153                 /* Turn off 8psk power */
154                 if (gp8psk_usb_in_op(d, BOOT_8PSK, 0, 0, &buf, 1))
155                         return -EINVAL;
156
157         }
158         return 0;
159 }
160
161
162 static int gp8psk_streaming_ctrl(struct dvb_usb_device *d, int onoff)
163 {
164         return gp8psk_usb_out_op(d, ARM_TRANSFER, onoff, 0 , NULL, 0);
165 }
166
167 static int gp8psk_frontend_attach(struct dvb_usb_device *d)
168 {
169         d->fe = gp8psk_fe_attach(d);
170
171         return 0;
172 }
173
174 static struct dvb_usb_properties gp8psk_properties;
175
176 static int gp8psk_usb_probe(struct usb_interface *intf,
177                 const struct usb_device_id *id)
178 {
179         return dvb_usb_device_init(intf,&gp8psk_properties,THIS_MODULE,NULL);
180 }
181
182 static struct usb_device_id gp8psk_usb_table [] = {
183             { USB_DEVICE(USB_VID_GENPIX, USB_PID_GENPIX_8PSK_COLD) },
184             { USB_DEVICE(USB_VID_GENPIX, USB_PID_GENPIX_8PSK_WARM) },
185             { 0 },
186 };
187 MODULE_DEVICE_TABLE(usb, gp8psk_usb_table);
188
189 static struct dvb_usb_properties gp8psk_properties = {
190         .caps = 0,
191
192         .usb_ctrl = CYPRESS_FX2,
193         .firmware = "dvb-usb-gp8psk-01.fw",
194
195         .streaming_ctrl   = gp8psk_streaming_ctrl,
196         .power_ctrl       = gp8psk_power_ctrl,
197         .frontend_attach  = gp8psk_frontend_attach,
198
199         .generic_bulk_ctrl_endpoint = 0x01,
200         /* parameter for the MPEG2-data transfer */
201         .urb = {
202                 .type = DVB_USB_BULK,
203                 .count = 7,
204                 .endpoint = 0x82,
205                 .u = {
206                         .bulk = {
207                                 .buffersize = 8192,
208                         }
209                 }
210         },
211
212         .num_device_descs = 1,
213         .devices = {
214                 { .name = "Genpix 8PSK-USB DVB-S USB2.0 receiver",
215                   .cold_ids = { &gp8psk_usb_table[0], NULL },
216                   .warm_ids = { &gp8psk_usb_table[1], NULL },
217                 },
218                 { 0 },
219         }
220 };
221
222 /* usb specific object needed to register this driver with the usb subsystem */
223 static struct usb_driver gp8psk_usb_driver = {
224         .name           = "dvb_usb_gp8psk",
225         .probe          = gp8psk_usb_probe,
226         .disconnect = dvb_usb_device_exit,
227         .id_table       = gp8psk_usb_table,
228 };
229
230 /* module stuff */
231 static int __init gp8psk_usb_module_init(void)
232 {
233         int result;
234         if ((result = usb_register(&gp8psk_usb_driver))) {
235                 err("usb_register failed. (%d)",result);
236                 return result;
237         }
238
239         return 0;
240 }
241
242 static void __exit gp8psk_usb_module_exit(void)
243 {
244         /* deregister this driver from the USB subsystem */
245         usb_deregister(&gp8psk_usb_driver);
246 }
247
248 module_init(gp8psk_usb_module_init);
249 module_exit(gp8psk_usb_module_exit);
250
251 MODULE_AUTHOR("Alan Nisota <alannisota@gamil.com>");
252 MODULE_DESCRIPTION("Driver for Genpix 8psk-USB DVB-S USB2.0");
253 MODULE_VERSION("1.0");
254 MODULE_LICENSE("GPL");