Merge commit 'v2.6.30-rc5' into sched/core
[linux-2.6] / drivers / media / dvb / siano / smsusb.c
1 /*
2  *  Driver for the Siano SMS1xxx USB dongle
3  *
4  *  author: Anatoly Greenblat
5  *
6  *  Copyright (c), 2005-2008 Siano Mobile Silicon, Inc.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation;
11  *
12  *  Software distributed under the License is distributed on an "AS IS"
13  *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
14  *
15  *  See the GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/usb.h>
25 #include <linux/firmware.h>
26
27 #include "smscoreapi.h"
28 #include "sms-cards.h"
29
30 static int sms_dbg;
31 module_param_named(debug, sms_dbg, int, 0644);
32 MODULE_PARM_DESC(debug, "set debug level (info=1, adv=2 (or-able))");
33
34 #define USB1_BUFFER_SIZE                0x1000
35 #define USB2_BUFFER_SIZE                0x4000
36
37 #define MAX_BUFFERS             50
38 #define MAX_URBS                10
39
40 struct smsusb_device_t;
41
42 struct smsusb_urb_t {
43         struct smscore_buffer_t *cb;
44         struct smsusb_device_t  *dev;
45
46         struct urb urb;
47 };
48
49 struct smsusb_device_t {
50         struct usb_device *udev;
51         struct smscore_device_t *coredev;
52
53         struct smsusb_urb_t     surbs[MAX_URBS];
54
55         int             response_alignment;
56         int             buffer_size;
57 };
58
59 static int smsusb_submit_urb(struct smsusb_device_t *dev,
60                              struct smsusb_urb_t *surb);
61
62 static void smsusb_onresponse(struct urb *urb)
63 {
64         struct smsusb_urb_t *surb = (struct smsusb_urb_t *) urb->context;
65         struct smsusb_device_t *dev = surb->dev;
66
67         if (urb->status < 0) {
68                 sms_err("error, urb status %d, %d bytes",
69                         urb->status, urb->actual_length);
70                 return;
71         }
72
73         if (urb->actual_length > 0) {
74                 struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) surb->cb->p;
75
76                 if (urb->actual_length >= phdr->msgLength) {
77                         surb->cb->size = phdr->msgLength;
78
79                         if (dev->response_alignment &&
80                             (phdr->msgFlags & MSG_HDR_FLAG_SPLIT_MSG)) {
81
82                                 surb->cb->offset =
83                                         dev->response_alignment +
84                                         ((phdr->msgFlags >> 8) & 3);
85
86                                 /* sanity check */
87                                 if (((int) phdr->msgLength +
88                                      surb->cb->offset) > urb->actual_length) {
89                                         sms_err("invalid response "
90                                                 "msglen %d offset %d "
91                                                 "size %d",
92                                                 phdr->msgLength,
93                                                 surb->cb->offset,
94                                                 urb->actual_length);
95                                         goto exit_and_resubmit;
96                                 }
97
98                                 /* move buffer pointer and
99                                  * copy header to its new location */
100                                 memcpy((char *) phdr + surb->cb->offset,
101                                        phdr, sizeof(struct SmsMsgHdr_ST));
102                         } else
103                                 surb->cb->offset = 0;
104
105                         smscore_onresponse(dev->coredev, surb->cb);
106                         surb->cb = NULL;
107                 } else {
108                         sms_err("invalid response "
109                                 "msglen %d actual %d",
110                                 phdr->msgLength, urb->actual_length);
111                 }
112         }
113
114 exit_and_resubmit:
115         smsusb_submit_urb(dev, surb);
116 }
117
118 static int smsusb_submit_urb(struct smsusb_device_t *dev,
119                              struct smsusb_urb_t *surb)
120 {
121         if (!surb->cb) {
122                 surb->cb = smscore_getbuffer(dev->coredev);
123                 if (!surb->cb) {
124                         sms_err("smscore_getbuffer(...) returned NULL");
125                         return -ENOMEM;
126                 }
127         }
128
129         usb_fill_bulk_urb(
130                 &surb->urb,
131                 dev->udev,
132                 usb_rcvbulkpipe(dev->udev, 0x81),
133                 surb->cb->p,
134                 dev->buffer_size,
135                 smsusb_onresponse,
136                 surb
137         );
138         surb->urb.transfer_dma = surb->cb->phys;
139         surb->urb.transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
140
141         return usb_submit_urb(&surb->urb, GFP_ATOMIC);
142 }
143
144 static void smsusb_stop_streaming(struct smsusb_device_t *dev)
145 {
146         int i;
147
148         for (i = 0; i < MAX_URBS; i++) {
149                 usb_kill_urb(&dev->surbs[i].urb);
150
151                 if (dev->surbs[i].cb) {
152                         smscore_putbuffer(dev->coredev, dev->surbs[i].cb);
153                         dev->surbs[i].cb = NULL;
154                 }
155         }
156 }
157
158 static int smsusb_start_streaming(struct smsusb_device_t *dev)
159 {
160         int i, rc;
161
162         for (i = 0; i < MAX_URBS; i++) {
163                 rc = smsusb_submit_urb(dev, &dev->surbs[i]);
164                 if (rc < 0) {
165                         sms_err("smsusb_submit_urb(...) failed");
166                         smsusb_stop_streaming(dev);
167                         break;
168                 }
169         }
170
171         return rc;
172 }
173
174 static int smsusb_sendrequest(void *context, void *buffer, size_t size)
175 {
176         struct smsusb_device_t *dev = (struct smsusb_device_t *) context;
177         int dummy;
178
179         return usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 2),
180                             buffer, size, &dummy, 1000);
181 }
182
183 static char *smsusb1_fw_lkup[] = {
184         "dvbt_stellar_usb.inp",
185         "dvbh_stellar_usb.inp",
186         "tdmb_stellar_usb.inp",
187         "none",
188         "dvbt_bda_stellar_usb.inp",
189 };
190
191 static inline char *sms_get_fw_name(int mode, int board_id)
192 {
193         char **fw = sms_get_board(board_id)->fw;
194         return (fw && fw[mode]) ? fw[mode] : smsusb1_fw_lkup[mode];
195 }
196
197 static int smsusb1_load_firmware(struct usb_device *udev, int id, int board_id)
198 {
199         const struct firmware *fw;
200         u8 *fw_buffer;
201         int rc, dummy;
202         char *fw_filename;
203
204         if (id < DEVICE_MODE_DVBT || id > DEVICE_MODE_DVBT_BDA) {
205                 sms_err("invalid firmware id specified %d", id);
206                 return -EINVAL;
207         }
208
209         fw_filename = sms_get_fw_name(id, board_id);
210
211         rc = request_firmware(&fw, fw_filename, &udev->dev);
212         if (rc < 0) {
213                 sms_warn("failed to open \"%s\" mode %d, "
214                          "trying again with default firmware", fw_filename, id);
215
216                 fw_filename = smsusb1_fw_lkup[id];
217                 rc = request_firmware(&fw, fw_filename, &udev->dev);
218                 if (rc < 0) {
219                         sms_warn("failed to open \"%s\" mode %d",
220                                  fw_filename, id);
221
222                         return rc;
223                 }
224         }
225
226         fw_buffer = kmalloc(fw->size, GFP_KERNEL);
227         if (fw_buffer) {
228                 memcpy(fw_buffer, fw->data, fw->size);
229
230                 rc = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 2),
231                                   fw_buffer, fw->size, &dummy, 1000);
232
233                 sms_info("sent %zd(%d) bytes, rc %d", fw->size, dummy, rc);
234
235                 kfree(fw_buffer);
236         } else {
237                 sms_err("failed to allocate firmware buffer");
238                 rc = -ENOMEM;
239         }
240         sms_info("read FW %s, size=%zd", fw_filename, fw->size);
241
242         release_firmware(fw);
243
244         return rc;
245 }
246
247 static void smsusb1_detectmode(void *context, int *mode)
248 {
249         char *product_string =
250                 ((struct smsusb_device_t *) context)->udev->product;
251
252         *mode = DEVICE_MODE_NONE;
253
254         if (!product_string) {
255                 product_string = "none";
256                 sms_err("product string not found");
257         } else if (strstr(product_string, "DVBH"))
258                 *mode = 1;
259         else if (strstr(product_string, "BDA"))
260                 *mode = 4;
261         else if (strstr(product_string, "DVBT"))
262                 *mode = 0;
263         else if (strstr(product_string, "TDMB"))
264                 *mode = 2;
265
266         sms_info("%d \"%s\"", *mode, product_string);
267 }
268
269 static int smsusb1_setmode(void *context, int mode)
270 {
271         struct SmsMsgHdr_ST Msg = { MSG_SW_RELOAD_REQ, 0, HIF_TASK,
272                              sizeof(struct SmsMsgHdr_ST), 0 };
273
274         if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
275                 sms_err("invalid firmware id specified %d", mode);
276                 return -EINVAL;
277         }
278
279         return smsusb_sendrequest(context, &Msg, sizeof(Msg));
280 }
281
282 static void smsusb_term_device(struct usb_interface *intf)
283 {
284         struct smsusb_device_t *dev =
285                 (struct smsusb_device_t *) usb_get_intfdata(intf);
286
287         if (dev) {
288                 smsusb_stop_streaming(dev);
289
290                 /* unregister from smscore */
291                 if (dev->coredev)
292                         smscore_unregister_device(dev->coredev);
293
294                 kfree(dev);
295
296                 sms_info("device %p destroyed", dev);
297         }
298
299         usb_set_intfdata(intf, NULL);
300 }
301
302 static int smsusb_init_device(struct usb_interface *intf, int board_id)
303 {
304         struct smsdevice_params_t params;
305         struct smsusb_device_t *dev;
306         int i, rc;
307
308         /* create device object */
309         dev = kzalloc(sizeof(struct smsusb_device_t), GFP_KERNEL);
310         if (!dev) {
311                 sms_err("kzalloc(sizeof(struct smsusb_device_t) failed");
312                 return -ENOMEM;
313         }
314
315         memset(&params, 0, sizeof(params));
316         usb_set_intfdata(intf, dev);
317         dev->udev = interface_to_usbdev(intf);
318
319         params.device_type = sms_get_board(board_id)->type;
320
321         switch (params.device_type) {
322         case SMS_STELLAR:
323                 dev->buffer_size = USB1_BUFFER_SIZE;
324
325                 params.setmode_handler = smsusb1_setmode;
326                 params.detectmode_handler = smsusb1_detectmode;
327                 break;
328         default:
329                 sms_err("Unspecified sms device type!");
330                 /* fall-thru */
331         case SMS_NOVA_A0:
332         case SMS_NOVA_B0:
333         case SMS_VEGA:
334                 dev->buffer_size = USB2_BUFFER_SIZE;
335                 dev->response_alignment =
336                         dev->udev->ep_in[1]->desc.wMaxPacketSize -
337                         sizeof(struct SmsMsgHdr_ST);
338
339                 params.flags |= SMS_DEVICE_FAMILY2;
340                 break;
341         }
342
343         params.device = &dev->udev->dev;
344         params.buffer_size = dev->buffer_size;
345         params.num_buffers = MAX_BUFFERS;
346         params.sendrequest_handler = smsusb_sendrequest;
347         params.context = dev;
348         snprintf(params.devpath, sizeof(params.devpath),
349                  "usb\\%d-%s", dev->udev->bus->busnum, dev->udev->devpath);
350
351         /* register in smscore */
352         rc = smscore_register_device(&params, &dev->coredev);
353         if (rc < 0) {
354                 sms_err("smscore_register_device(...) failed, rc %d", rc);
355                 smsusb_term_device(intf);
356                 return rc;
357         }
358
359         smscore_set_board_id(dev->coredev, board_id);
360
361         /* initialize urbs */
362         for (i = 0; i < MAX_URBS; i++) {
363                 dev->surbs[i].dev = dev;
364                 usb_init_urb(&dev->surbs[i].urb);
365         }
366
367         sms_info("smsusb_start_streaming(...).");
368         rc = smsusb_start_streaming(dev);
369         if (rc < 0) {
370                 sms_err("smsusb_start_streaming(...) failed");
371                 smsusb_term_device(intf);
372                 return rc;
373         }
374
375         rc = smscore_start_device(dev->coredev);
376         if (rc < 0) {
377                 sms_err("smscore_start_device(...) failed");
378                 smsusb_term_device(intf);
379                 return rc;
380         }
381
382         sms_info("device %p created", dev);
383
384         return rc;
385 }
386
387 static int smsusb_probe(struct usb_interface *intf,
388                         const struct usb_device_id *id)
389 {
390         struct usb_device *udev = interface_to_usbdev(intf);
391         char devpath[32];
392         int i, rc;
393
394         rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x81));
395         rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x02));
396
397         if (intf->num_altsetting > 0) {
398                 rc = usb_set_interface(
399                         udev, intf->cur_altsetting->desc.bInterfaceNumber, 0);
400                 if (rc < 0) {
401                         sms_err("usb_set_interface failed, rc %d", rc);
402                         return rc;
403                 }
404         }
405
406         sms_info("smsusb_probe %d",
407                intf->cur_altsetting->desc.bInterfaceNumber);
408         for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++)
409                 sms_info("endpoint %d %02x %02x %d", i,
410                        intf->cur_altsetting->endpoint[i].desc.bEndpointAddress,
411                        intf->cur_altsetting->endpoint[i].desc.bmAttributes,
412                        intf->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
413
414         if ((udev->actconfig->desc.bNumInterfaces == 2) &&
415             (intf->cur_altsetting->desc.bInterfaceNumber == 0)) {
416                 sms_err("rom interface 0 is not used");
417                 return -ENODEV;
418         }
419
420         if (intf->cur_altsetting->desc.bInterfaceNumber == 1) {
421                 snprintf(devpath, sizeof(devpath), "usb\\%d-%s",
422                          udev->bus->busnum, udev->devpath);
423                 sms_info("stellar device was found.");
424                 return smsusb1_load_firmware(
425                                 udev, smscore_registry_getmode(devpath),
426                                 id->driver_info);
427         }
428
429         rc = smsusb_init_device(intf, id->driver_info);
430         sms_info("rc %d", rc);
431         sms_board_load_modules(id->driver_info);
432         return rc;
433 }
434
435 static void smsusb_disconnect(struct usb_interface *intf)
436 {
437         smsusb_term_device(intf);
438 }
439
440 static int smsusb_suspend(struct usb_interface *intf, pm_message_t msg)
441 {
442         struct smsusb_device_t *dev =
443                 (struct smsusb_device_t *)usb_get_intfdata(intf);
444         printk(KERN_INFO "%s: Entering status %d.\n", __func__, msg.event);
445         smsusb_stop_streaming(dev);
446         return 0;
447 }
448
449 static int smsusb_resume(struct usb_interface *intf)
450 {
451         int rc, i;
452         struct smsusb_device_t *dev =
453                 (struct smsusb_device_t *)usb_get_intfdata(intf);
454         struct usb_device *udev = interface_to_usbdev(intf);
455
456         printk(KERN_INFO "%s: Entering.\n", __func__);
457         usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x81));
458         usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x02));
459
460         for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++)
461                 printk(KERN_INFO "endpoint %d %02x %02x %d\n", i,
462                        intf->cur_altsetting->endpoint[i].desc.bEndpointAddress,
463                        intf->cur_altsetting->endpoint[i].desc.bmAttributes,
464                        intf->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
465
466         if (intf->num_altsetting > 0) {
467                 rc = usb_set_interface(udev,
468                                        intf->cur_altsetting->desc.
469                                        bInterfaceNumber, 0);
470                 if (rc < 0) {
471                         printk(KERN_INFO "%s usb_set_interface failed, "
472                                "rc %d\n", __func__, rc);
473                         return rc;
474                 }
475         }
476
477         smsusb_start_streaming(dev);
478         return 0;
479 }
480
481 struct usb_device_id smsusb_id_table[] = {
482 #ifdef CONFIG_DVB_SIANO_SMS1XXX_SMS_IDS
483         { USB_DEVICE(0x187f, 0x0010),
484                 .driver_info = SMS1XXX_BOARD_SIANO_STELLAR },
485         { USB_DEVICE(0x187f, 0x0100),
486                 .driver_info = SMS1XXX_BOARD_SIANO_STELLAR },
487         { USB_DEVICE(0x187f, 0x0200),
488                 .driver_info = SMS1XXX_BOARD_SIANO_NOVA_A },
489         { USB_DEVICE(0x187f, 0x0201),
490                 .driver_info = SMS1XXX_BOARD_SIANO_NOVA_B },
491         { USB_DEVICE(0x187f, 0x0300),
492                 .driver_info = SMS1XXX_BOARD_SIANO_VEGA },
493 #endif
494         { USB_DEVICE(0x2040, 0x1700),
495                 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_CATAMOUNT },
496         { USB_DEVICE(0x2040, 0x1800),
497                 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_OKEMO_A },
498         { USB_DEVICE(0x2040, 0x1801),
499                 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_OKEMO_B },
500         { USB_DEVICE(0x2040, 0x2000),
501                 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
502         { USB_DEVICE(0x2040, 0x2009),
503                 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD_R2 },
504         { USB_DEVICE(0x2040, 0x200a),
505                 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
506         { USB_DEVICE(0x2040, 0x2010),
507                 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
508         { USB_DEVICE(0x2040, 0x2011),
509                 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
510         { USB_DEVICE(0x2040, 0x2019),
511                 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
512         { USB_DEVICE(0x2040, 0x5500),
513                 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
514         { USB_DEVICE(0x2040, 0x5510),
515                 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
516         { USB_DEVICE(0x2040, 0x5520),
517                 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
518         { USB_DEVICE(0x2040, 0x5530),
519                 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
520         { USB_DEVICE(0x2040, 0x5580),
521                 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
522         { USB_DEVICE(0x2040, 0x5590),
523                 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
524         { }             /* Terminating entry */
525 };
526 MODULE_DEVICE_TABLE(usb, smsusb_id_table);
527
528 static struct usb_driver smsusb_driver = {
529         .name                   = "smsusb",
530         .probe                  = smsusb_probe,
531         .disconnect             = smsusb_disconnect,
532         .id_table               = smsusb_id_table,
533
534         .suspend                = smsusb_suspend,
535         .resume                 = smsusb_resume,
536 };
537
538 int smsusb_module_init(void)
539 {
540         int rc = usb_register(&smsusb_driver);
541         if (rc)
542                 sms_err("usb_register failed. Error number %d", rc);
543
544         sms_debug("");
545
546         return rc;
547 }
548
549 void smsusb_module_exit(void)
550 {
551         sms_debug("");
552         /* Regular USB Cleanup */
553         usb_deregister(&smsusb_driver);
554 }
555
556 module_init(smsusb_module_init);
557 module_exit(smsusb_module_exit);
558
559 MODULE_DESCRIPTION("Driver for the Siano SMS1XXX USB dongle");
560 MODULE_AUTHOR("Siano Mobile Silicon, INC. (uris@siano-ms.com)");
561 MODULE_LICENSE("GPL");