Merge branches 'x86/acpi', 'x86/asm', 'x86/cpudetect', 'x86/crashdump', 'x86/debug...
[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 #define USB1_BUFFER_SIZE                0x1000
31 #define USB2_BUFFER_SIZE                0x4000
32
33 #define MAX_BUFFERS             50
34 #define MAX_URBS                10
35
36 struct smsusb_device_t;
37
38 struct smsusb_urb_t {
39         struct smscore_buffer_t *cb;
40         struct smsusb_device_t  *dev;
41
42         struct urb urb;
43 };
44
45 struct smsusb_device_t {
46         struct usb_device *udev;
47         struct smscore_device_t *coredev;
48
49         struct smsusb_urb_t     surbs[MAX_URBS];
50
51         int             response_alignment;
52         int             buffer_size;
53 };
54
55 static int smsusb_submit_urb(struct smsusb_device_t *dev,
56                              struct smsusb_urb_t *surb);
57
58 static void smsusb_onresponse(struct urb *urb)
59 {
60         struct smsusb_urb_t *surb = (struct smsusb_urb_t *) urb->context;
61         struct smsusb_device_t *dev = surb->dev;
62
63         if (urb->status < 0) {
64                 sms_err("error, urb status %d, %d bytes",
65                         urb->status, urb->actual_length);
66                 return;
67         }
68
69         if (urb->actual_length > 0) {
70                 struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) surb->cb->p;
71
72                 if (urb->actual_length >= phdr->msgLength) {
73                         surb->cb->size = phdr->msgLength;
74
75                         if (dev->response_alignment &&
76                             (phdr->msgFlags & MSG_HDR_FLAG_SPLIT_MSG)) {
77
78                                 surb->cb->offset =
79                                         dev->response_alignment +
80                                         ((phdr->msgFlags >> 8) & 3);
81
82                                 /* sanity check */
83                                 if (((int) phdr->msgLength +
84                                      surb->cb->offset) > urb->actual_length) {
85                                         sms_err("invalid response "
86                                                 "msglen %d offset %d "
87                                                 "size %d",
88                                                 phdr->msgLength,
89                                                 surb->cb->offset,
90                                                 urb->actual_length);
91                                         goto exit_and_resubmit;
92                                 }
93
94                                 /* move buffer pointer and
95                                  * copy header to its new location */
96                                 memcpy((char *) phdr + surb->cb->offset,
97                                        phdr, sizeof(struct SmsMsgHdr_ST));
98                         } else
99                                 surb->cb->offset = 0;
100
101                         smscore_onresponse(dev->coredev, surb->cb);
102                         surb->cb = NULL;
103                 } else {
104                         sms_err("invalid response "
105                                 "msglen %d actual %d",
106                                 phdr->msgLength, urb->actual_length);
107                 }
108         }
109
110 exit_and_resubmit:
111         smsusb_submit_urb(dev, surb);
112 }
113
114 static int smsusb_submit_urb(struct smsusb_device_t *dev,
115                              struct smsusb_urb_t *surb)
116 {
117         if (!surb->cb) {
118                 surb->cb = smscore_getbuffer(dev->coredev);
119                 if (!surb->cb) {
120                         sms_err("smscore_getbuffer(...) returned NULL");
121                         return -ENOMEM;
122                 }
123         }
124
125         usb_fill_bulk_urb(
126                 &surb->urb,
127                 dev->udev,
128                 usb_rcvbulkpipe(dev->udev, 0x81),
129                 surb->cb->p,
130                 dev->buffer_size,
131                 smsusb_onresponse,
132                 surb
133         );
134         surb->urb.transfer_dma = surb->cb->phys;
135         surb->urb.transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
136
137         return usb_submit_urb(&surb->urb, GFP_ATOMIC);
138 }
139
140 static void smsusb_stop_streaming(struct smsusb_device_t *dev)
141 {
142         int i;
143
144         for (i = 0; i < MAX_URBS; i++) {
145                 usb_kill_urb(&dev->surbs[i].urb);
146
147                 if (dev->surbs[i].cb) {
148                         smscore_putbuffer(dev->coredev, dev->surbs[i].cb);
149                         dev->surbs[i].cb = NULL;
150                 }
151         }
152 }
153
154 static int smsusb_start_streaming(struct smsusb_device_t *dev)
155 {
156         int i, rc;
157
158         for (i = 0; i < MAX_URBS; i++) {
159                 rc = smsusb_submit_urb(dev, &dev->surbs[i]);
160                 if (rc < 0) {
161                         sms_err("smsusb_submit_urb(...) failed");
162                         smsusb_stop_streaming(dev);
163                         break;
164                 }
165         }
166
167         return rc;
168 }
169
170 static int smsusb_sendrequest(void *context, void *buffer, size_t size)
171 {
172         struct smsusb_device_t *dev = (struct smsusb_device_t *) context;
173         int dummy;
174
175         return usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 2),
176                             buffer, size, &dummy, 1000);
177 }
178
179 static char *smsusb1_fw_lkup[] = {
180         "dvbt_stellar_usb.inp",
181         "dvbh_stellar_usb.inp",
182         "tdmb_stellar_usb.inp",
183         "none",
184         "dvbt_bda_stellar_usb.inp",
185 };
186
187 static inline char *sms_get_fw_name(int mode, int board_id)
188 {
189         char **fw = sms_get_board(board_id)->fw;
190         return (fw && fw[mode]) ? fw[mode] : smsusb1_fw_lkup[mode];
191 }
192
193 static int smsusb1_load_firmware(struct usb_device *udev, int id, int board_id)
194 {
195         const struct firmware *fw;
196         u8 *fw_buffer;
197         int rc, dummy;
198         char *fw_filename;
199
200         if (id < DEVICE_MODE_DVBT || id > DEVICE_MODE_DVBT_BDA) {
201                 sms_err("invalid firmware id specified %d", id);
202                 return -EINVAL;
203         }
204
205         fw_filename = sms_get_fw_name(id, board_id);
206
207         rc = request_firmware(&fw, fw_filename, &udev->dev);
208         if (rc < 0) {
209                 sms_warn("failed to open \"%s\" mode %d, "
210                          "trying again with default firmware", fw_filename, id);
211
212                 fw_filename = smsusb1_fw_lkup[id];
213                 rc = request_firmware(&fw, fw_filename, &udev->dev);
214                 if (rc < 0) {
215                         sms_warn("failed to open \"%s\" mode %d",
216                                  fw_filename, id);
217
218                         return rc;
219                 }
220         }
221
222         fw_buffer = kmalloc(fw->size, GFP_KERNEL);
223         if (fw_buffer) {
224                 memcpy(fw_buffer, fw->data, fw->size);
225
226                 rc = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 2),
227                                   fw_buffer, fw->size, &dummy, 1000);
228
229                 sms_info("sent %zd(%d) bytes, rc %d", fw->size, dummy, rc);
230
231                 kfree(fw_buffer);
232         } else {
233                 sms_err("failed to allocate firmware buffer");
234                 rc = -ENOMEM;
235         }
236         sms_info("read FW %s, size=%zd", fw_filename, fw->size);
237
238         release_firmware(fw);
239
240         return rc;
241 }
242
243 static void smsusb1_detectmode(void *context, int *mode)
244 {
245         char *product_string =
246                 ((struct smsusb_device_t *) context)->udev->product;
247
248         *mode = DEVICE_MODE_NONE;
249
250         if (!product_string) {
251                 product_string = "none";
252                 sms_err("product string not found");
253         } else if (strstr(product_string, "DVBH"))
254                 *mode = 1;
255         else if (strstr(product_string, "BDA"))
256                 *mode = 4;
257         else if (strstr(product_string, "DVBT"))
258                 *mode = 0;
259         else if (strstr(product_string, "TDMB"))
260                 *mode = 2;
261
262         sms_info("%d \"%s\"", *mode, product_string);
263 }
264
265 static int smsusb1_setmode(void *context, int mode)
266 {
267         struct SmsMsgHdr_ST Msg = { MSG_SW_RELOAD_REQ, 0, HIF_TASK,
268                              sizeof(struct SmsMsgHdr_ST), 0 };
269
270         if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
271                 sms_err("invalid firmware id specified %d", mode);
272                 return -EINVAL;
273         }
274
275         return smsusb_sendrequest(context, &Msg, sizeof(Msg));
276 }
277
278 static void smsusb_term_device(struct usb_interface *intf)
279 {
280         struct smsusb_device_t *dev =
281                 (struct smsusb_device_t *) usb_get_intfdata(intf);
282
283         if (dev) {
284                 smsusb_stop_streaming(dev);
285
286                 /* unregister from smscore */
287                 if (dev->coredev)
288                         smscore_unregister_device(dev->coredev);
289
290                 kfree(dev);
291
292                 sms_info("device %p destroyed", dev);
293         }
294
295         usb_set_intfdata(intf, NULL);
296 }
297
298 static int smsusb_init_device(struct usb_interface *intf, int board_id)
299 {
300         struct smsdevice_params_t params;
301         struct smsusb_device_t *dev;
302         int i, rc;
303
304         /* create device object */
305         dev = kzalloc(sizeof(struct smsusb_device_t), GFP_KERNEL);
306         if (!dev) {
307                 sms_err("kzalloc(sizeof(struct smsusb_device_t) failed");
308                 return -ENOMEM;
309         }
310
311         memset(&params, 0, sizeof(params));
312         usb_set_intfdata(intf, dev);
313         dev->udev = interface_to_usbdev(intf);
314
315         params.device_type = sms_get_board(board_id)->type;
316
317         switch (params.device_type) {
318         case SMS_STELLAR:
319                 dev->buffer_size = USB1_BUFFER_SIZE;
320
321                 params.setmode_handler = smsusb1_setmode;
322                 params.detectmode_handler = smsusb1_detectmode;
323                 break;
324         default:
325                 sms_err("Unspecified sms device type!");
326                 /* fall-thru */
327         case SMS_NOVA_A0:
328         case SMS_NOVA_B0:
329         case SMS_VEGA:
330                 dev->buffer_size = USB2_BUFFER_SIZE;
331                 dev->response_alignment =
332                         dev->udev->ep_in[1]->desc.wMaxPacketSize -
333                         sizeof(struct SmsMsgHdr_ST);
334
335                 params.flags |= SMS_DEVICE_FAMILY2;
336                 break;
337         }
338
339         params.device = &dev->udev->dev;
340         params.buffer_size = dev->buffer_size;
341         params.num_buffers = MAX_BUFFERS;
342         params.sendrequest_handler = smsusb_sendrequest;
343         params.context = dev;
344         snprintf(params.devpath, sizeof(params.devpath),
345                  "usb\\%d-%s", dev->udev->bus->busnum, dev->udev->devpath);
346
347         /* register in smscore */
348         rc = smscore_register_device(&params, &dev->coredev);
349         if (rc < 0) {
350                 sms_err("smscore_register_device(...) failed, rc %d", rc);
351                 smsusb_term_device(intf);
352                 return rc;
353         }
354
355         smscore_set_board_id(dev->coredev, board_id);
356
357         /* initialize urbs */
358         for (i = 0; i < MAX_URBS; i++) {
359                 dev->surbs[i].dev = dev;
360                 usb_init_urb(&dev->surbs[i].urb);
361         }
362
363         sms_info("smsusb_start_streaming(...).");
364         rc = smsusb_start_streaming(dev);
365         if (rc < 0) {
366                 sms_err("smsusb_start_streaming(...) failed");
367                 smsusb_term_device(intf);
368                 return rc;
369         }
370
371         rc = smscore_start_device(dev->coredev);
372         if (rc < 0) {
373                 sms_err("smscore_start_device(...) failed");
374                 smsusb_term_device(intf);
375                 return rc;
376         }
377
378         sms_info("device %p created", dev);
379
380         return rc;
381 }
382
383 static int smsusb_probe(struct usb_interface *intf,
384                         const struct usb_device_id *id)
385 {
386         struct usb_device *udev = interface_to_usbdev(intf);
387         char devpath[32];
388         int i, rc;
389
390         rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x81));
391         rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x02));
392
393         if (intf->num_altsetting > 0) {
394                 rc = usb_set_interface(
395                         udev, intf->cur_altsetting->desc.bInterfaceNumber, 0);
396                 if (rc < 0) {
397                         sms_err("usb_set_interface failed, rc %d", rc);
398                         return rc;
399                 }
400         }
401
402         sms_info("smsusb_probe %d",
403                intf->cur_altsetting->desc.bInterfaceNumber);
404         for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++)
405                 sms_info("endpoint %d %02x %02x %d", i,
406                        intf->cur_altsetting->endpoint[i].desc.bEndpointAddress,
407                        intf->cur_altsetting->endpoint[i].desc.bmAttributes,
408                        intf->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
409
410         if ((udev->actconfig->desc.bNumInterfaces == 2) &&
411             (intf->cur_altsetting->desc.bInterfaceNumber == 0)) {
412                 sms_err("rom interface 0 is not used");
413                 return -ENODEV;
414         }
415
416         if (intf->cur_altsetting->desc.bInterfaceNumber == 1) {
417                 snprintf(devpath, sizeof(devpath), "usb\\%d-%s",
418                          udev->bus->busnum, udev->devpath);
419                 sms_info("stellar device was found.");
420                 return smsusb1_load_firmware(
421                                 udev, smscore_registry_getmode(devpath),
422                                 id->driver_info);
423         }
424
425         rc = smsusb_init_device(intf, id->driver_info);
426         sms_info("rc %d", rc);
427         return rc;
428 }
429
430 static void smsusb_disconnect(struct usb_interface *intf)
431 {
432         smsusb_term_device(intf);
433 }
434
435 static int smsusb_suspend(struct usb_interface *intf, pm_message_t msg)
436 {
437         struct smsusb_device_t *dev =
438                 (struct smsusb_device_t *)usb_get_intfdata(intf);
439         printk(KERN_INFO "%s  Entering status %d.\n", __func__, msg.event);
440         smsusb_stop_streaming(dev);
441         return 0;
442 }
443
444 static int smsusb_resume(struct usb_interface *intf)
445 {
446         int rc, i;
447         struct smsusb_device_t *dev =
448                 (struct smsusb_device_t *)usb_get_intfdata(intf);
449         struct usb_device *udev = interface_to_usbdev(intf);
450
451         printk(KERN_INFO "%s  Entering.\n", __func__);
452         usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x81));
453         usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x02));
454
455         for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++)
456                 printk(KERN_INFO "endpoint %d %02x %02x %d\n", i,
457                        intf->cur_altsetting->endpoint[i].desc.bEndpointAddress,
458                        intf->cur_altsetting->endpoint[i].desc.bmAttributes,
459                        intf->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
460
461         if (intf->num_altsetting > 0) {
462                 rc = usb_set_interface(udev,
463                                        intf->cur_altsetting->desc.
464                                        bInterfaceNumber, 0);
465                 if (rc < 0) {
466                         printk(KERN_INFO
467                                "%s usb_set_interface failed, rc %d\n",
468                                __func__, rc);
469                         return rc;
470                 }
471         }
472
473         smsusb_start_streaming(dev);
474         return 0;
475 }
476
477 static struct usb_driver smsusb_driver = {
478         .name                   = "sms1xxx",
479         .probe                  = smsusb_probe,
480         .disconnect             = smsusb_disconnect,
481         .id_table               = smsusb_id_table,
482
483         .suspend                = smsusb_suspend,
484         .resume                 = smsusb_resume,
485 };
486
487 int smsusb_register(void)
488 {
489         int rc = usb_register(&smsusb_driver);
490         if (rc)
491                 sms_err("usb_register failed. Error number %d", rc);
492
493         sms_debug("");
494
495         return rc;
496 }
497
498 void smsusb_unregister(void)
499 {
500         sms_debug("");
501         /* Regular USB Cleanup */
502         usb_deregister(&smsusb_driver);
503 }
504