blob: 751c0d6d98b8ffeaa7752791b4cd77645461540e [file] [log] [blame]
Uri Shkolnik6d4e6972009-05-12 07:57:54 -03001/****************************************************************
2
3Siano Mobile Silicon, Inc.
4MDTV receiver kernel modules.
5Copyright (C) 2005-2009, Uri Shkolnik, Anatoly Greenblat
6
7This program is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 2 of the License, or
10(at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20****************************************************************/
Steven Toth8d4f9d02008-05-22 18:05:26 -030021
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030022#include <linux/kernel.h>
23#include <linux/init.h>
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030024#include <linux/usb.h>
25#include <linux/firmware.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040027#include <linux/module.h>
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030028
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030029#include "smscoreapi.h"
Michael Krufky1c11d542008-06-18 22:09:55 -030030#include "sms-cards.h"
Uri Shkolnikc7ce8d32009-05-12 08:24:07 -030031#include "smsendian.h"
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030032
Michael Krufky0d02efe2009-02-27 02:42:16 -030033static int sms_dbg;
34module_param_named(debug, sms_dbg, int, 0644);
35MODULE_PARM_DESC(debug, "set debug level (info=1, adv=2 (or-able))");
36
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030037#define USB1_BUFFER_SIZE 0x1000
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -030038#define USB2_BUFFER_SIZE 0x2000
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030039
40#define MAX_BUFFERS 50
41#define MAX_URBS 10
42
Michael Krufky18245e12008-06-15 17:52:24 -030043struct smsusb_device_t;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030044
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -030045enum smsusb_state {
46 SMSUSB_DISCONNECTED,
47 SMSUSB_SUSPENDED,
48 SMSUSB_ACTIVE
49};
50
Michael Krufky18245e12008-06-15 17:52:24 -030051struct smsusb_urb_t {
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -030052 struct list_head entry;
Michael Krufky18245e12008-06-15 17:52:24 -030053 struct smscore_buffer_t *cb;
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -030054 struct smsusb_device_t *dev;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030055
Michael Krufky18245e12008-06-15 17:52:24 -030056 struct urb urb;
57};
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030058
Michael Krufky18245e12008-06-15 17:52:24 -030059struct smsusb_device_t {
Michael Krufkya83ccdd2008-05-06 03:11:51 -030060 struct usb_device *udev;
Michael Krufky18245e12008-06-15 17:52:24 -030061 struct smscore_device_t *coredev;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030062
Michael Krufky18245e12008-06-15 17:52:24 -030063 struct smsusb_urb_t surbs[MAX_URBS];
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030064
Michael Krufky82237412008-06-15 15:14:13 -030065 int response_alignment;
66 int buffer_size;
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -030067
68 unsigned char in_ep;
69 unsigned char out_ep;
70 enum smsusb_state state;
Michael Krufky18245e12008-06-15 17:52:24 -030071};
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030072
Michael Krufky0c071f32008-06-21 02:44:02 -030073static int smsusb_submit_urb(struct smsusb_device_t *dev,
74 struct smsusb_urb_t *surb);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030075
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -030076/**
77 * Completing URB's callback handler - top half (interrupt context)
78 * adds completing sms urb to the global surbs list and activtes the worker
79 * thread the surb
80 * IMPORTANT - blocking functions must not be called from here !!!
81
82 * @param urb pointer to a completing urb object
83 */
Michael Krufky0c071f32008-06-21 02:44:02 -030084static void smsusb_onresponse(struct urb *urb)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030085{
Michael Krufky18245e12008-06-15 17:52:24 -030086 struct smsusb_urb_t *surb = (struct smsusb_urb_t *) urb->context;
87 struct smsusb_device_t *dev = surb->dev;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030088
Uri Shkolnik8f12d0a2009-05-12 11:28:41 -030089 if (urb->status == -ESHUTDOWN) {
90 sms_err("error, urb status %d (-ESHUTDOWN), %d bytes",
Michael Krufkyeb250942008-06-19 22:07:23 -030091 urb->status, urb->actual_length);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030092 return;
93 }
94
Uri Shkolnik8f12d0a2009-05-12 11:28:41 -030095 if ((urb->actual_length > 0) && (urb->status == 0)) {
96 struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *)surb->cb->p;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030097
Uri Shkolnik9c9c68a2009-05-14 16:27:16 -030098 smsendian_handle_message_header(phdr);
Michael Krufky82237412008-06-15 15:14:13 -030099 if (urb->actual_length >= phdr->msgLength) {
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300100 surb->cb->size = phdr->msgLength;
101
Michael Krufky82237412008-06-15 15:14:13 -0300102 if (dev->response_alignment &&
103 (phdr->msgFlags & MSG_HDR_FLAG_SPLIT_MSG)) {
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300104
Michael Krufky82237412008-06-15 15:14:13 -0300105 surb->cb->offset =
106 dev->response_alignment +
107 ((phdr->msgFlags >> 8) & 3);
108
109 /* sanity check */
110 if (((int) phdr->msgLength +
111 surb->cb->offset) > urb->actual_length) {
Michael Krufkyeb250942008-06-19 22:07:23 -0300112 sms_err("invalid response "
113 "msglen %d offset %d "
114 "size %d",
115 phdr->msgLength,
116 surb->cb->offset,
117 urb->actual_length);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300118 goto exit_and_resubmit;
119 }
120
Michael Krufky82237412008-06-15 15:14:13 -0300121 /* move buffer pointer and
122 * copy header to its new location */
Michael Krufky55ad3102008-05-06 03:52:44 -0300123 memcpy((char *) phdr + surb->cb->offset,
Michael Krufky18245e12008-06-15 17:52:24 -0300124 phdr, sizeof(struct SmsMsgHdr_ST));
Michael Krufky82237412008-06-15 15:14:13 -0300125 } else
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300126 surb->cb->offset = 0;
127
Mauro Carvalho Chehab4c3bdb52013-03-09 09:27:39 -0300128 sms_debug("received %s(%d) size: %d",
129 smscore_translate_msg(phdr->msgType),
130 phdr->msgType, phdr->msgLength);
131
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300132 smscore_onresponse(dev->coredev, surb->cb);
133 surb->cb = NULL;
Michael Krufky82237412008-06-15 15:14:13 -0300134 } else {
Michael Krufkyeb250942008-06-19 22:07:23 -0300135 sms_err("invalid response "
136 "msglen %d actual %d",
137 phdr->msgLength, urb->actual_length);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300138 }
Uri Shkolnik8f12d0a2009-05-12 11:28:41 -0300139 } else
140 sms_err("error, urb status %d, %d bytes",
141 urb->status, urb->actual_length);
142
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300143
144exit_and_resubmit:
145 smsusb_submit_urb(dev, surb);
146}
147
Michael Krufky0c071f32008-06-21 02:44:02 -0300148static int smsusb_submit_urb(struct smsusb_device_t *dev,
149 struct smsusb_urb_t *surb)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300150{
Michael Krufky82237412008-06-15 15:14:13 -0300151 if (!surb->cb) {
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300152 surb->cb = smscore_getbuffer(dev->coredev);
Michael Krufky82237412008-06-15 15:14:13 -0300153 if (!surb->cb) {
Michael Krufkyeb250942008-06-19 22:07:23 -0300154 sms_err("smscore_getbuffer(...) returned NULL");
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300155 return -ENOMEM;
156 }
157 }
158
159 usb_fill_bulk_urb(
160 &surb->urb,
161 dev->udev,
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300162 usb_rcvbulkpipe(dev->udev, dev->in_ep),
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300163 surb->cb->p,
164 dev->buffer_size,
165 smsusb_onresponse,
166 surb
167 );
168 surb->urb.transfer_dma = surb->cb->phys;
169 surb->urb.transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
170
171 return usb_submit_urb(&surb->urb, GFP_ATOMIC);
172}
173
Michael Krufky0c071f32008-06-21 02:44:02 -0300174static void smsusb_stop_streaming(struct smsusb_device_t *dev)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300175{
176 int i;
177
Michael Krufkyfa830e82008-06-15 15:52:43 -0300178 for (i = 0; i < MAX_URBS; i++) {
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300179 usb_kill_urb(&dev->surbs[i].urb);
180
Michael Krufky82237412008-06-15 15:14:13 -0300181 if (dev->surbs[i].cb) {
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300182 smscore_putbuffer(dev->coredev, dev->surbs[i].cb);
183 dev->surbs[i].cb = NULL;
184 }
185 }
186}
187
Michael Krufky0c071f32008-06-21 02:44:02 -0300188static int smsusb_start_streaming(struct smsusb_device_t *dev)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300189{
190 int i, rc;
191
Michael Krufkyfa830e82008-06-15 15:52:43 -0300192 for (i = 0; i < MAX_URBS; i++) {
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300193 rc = smsusb_submit_urb(dev, &dev->surbs[i]);
Michael Krufky82237412008-06-15 15:14:13 -0300194 if (rc < 0) {
Michael Krufkyeb250942008-06-19 22:07:23 -0300195 sms_err("smsusb_submit_urb(...) failed");
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300196 smsusb_stop_streaming(dev);
197 break;
198 }
199 }
200
201 return rc;
202}
203
Michael Krufky0c071f32008-06-21 02:44:02 -0300204static int smsusb_sendrequest(void *context, void *buffer, size_t size)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300205{
Michael Krufky18245e12008-06-15 17:52:24 -0300206 struct smsusb_device_t *dev = (struct smsusb_device_t *) context;
Mauro Carvalho Chehab4c3bdb52013-03-09 09:27:39 -0300207 struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) buffer;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300208 int dummy;
209
Mauro Carvalho Chehab4c3bdb52013-03-09 09:27:39 -0300210 sms_debug("sending %s(%d) size: %d",
211 smscore_translate_msg(phdr->msgType), phdr->msgType,
212 phdr->msgLength);
213
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300214 if (dev->state != SMSUSB_ACTIVE)
215 return -ENOENT;
216
Uri Shkolnikc7ce8d32009-05-12 08:24:07 -0300217 smsendian_handle_message_header((struct SmsMsgHdr_ST *)buffer);
Michael Krufky82237412008-06-15 15:14:13 -0300218 return usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 2),
219 buffer, size, &dummy, 1000);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300220}
221
Michael Krufky0c071f32008-06-21 02:44:02 -0300222static char *smsusb1_fw_lkup[] = {
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300223 "dvbt_stellar_usb.inp",
224 "dvbh_stellar_usb.inp",
225 "tdmb_stellar_usb.inp",
226 "none",
227 "dvbt_bda_stellar_usb.inp",
228};
229
Michael Krufky1fbc3ca2008-07-01 04:35:26 -0300230static inline char *sms_get_fw_name(int mode, int board_id)
231{
232 char **fw = sms_get_board(board_id)->fw;
233 return (fw && fw[mode]) ? fw[mode] : smsusb1_fw_lkup[mode];
234}
235
236static int smsusb1_load_firmware(struct usb_device *udev, int id, int board_id)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300237{
238 const struct firmware *fw;
Michael Krufkya83ccdd2008-05-06 03:11:51 -0300239 u8 *fw_buffer;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300240 int rc, dummy;
Michael Krufky1fbc3ca2008-07-01 04:35:26 -0300241 char *fw_filename;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300242
Michael Krufky82237412008-06-15 15:14:13 -0300243 if (id < DEVICE_MODE_DVBT || id > DEVICE_MODE_DVBT_BDA) {
Michael Krufkyeb250942008-06-19 22:07:23 -0300244 sms_err("invalid firmware id specified %d", id);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300245 return -EINVAL;
246 }
247
Michael Krufky1fbc3ca2008-07-01 04:35:26 -0300248 fw_filename = sms_get_fw_name(id, board_id);
249
250 rc = request_firmware(&fw, fw_filename, &udev->dev);
Michael Krufky82237412008-06-15 15:14:13 -0300251 if (rc < 0) {
Michael Krufky1fbc3ca2008-07-01 04:35:26 -0300252 sms_warn("failed to open \"%s\" mode %d, "
253 "trying again with default firmware", fw_filename, id);
254
255 fw_filename = smsusb1_fw_lkup[id];
256 rc = request_firmware(&fw, fw_filename, &udev->dev);
257 if (rc < 0) {
258 sms_warn("failed to open \"%s\" mode %d",
259 fw_filename, id);
260
261 return rc;
262 }
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300263 }
264
265 fw_buffer = kmalloc(fw->size, GFP_KERNEL);
Michael Krufky82237412008-06-15 15:14:13 -0300266 if (fw_buffer) {
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300267 memcpy(fw_buffer, fw->data, fw->size);
268
Michael Krufky82237412008-06-15 15:14:13 -0300269 rc = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 2),
270 fw_buffer, fw->size, &dummy, 1000);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300271
Michael Krufky0f2a1ee2008-06-28 20:53:45 -0300272 sms_info("sent %zd(%d) bytes, rc %d", fw->size, dummy, rc);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300273
274 kfree(fw_buffer);
Michael Krufky82237412008-06-15 15:14:13 -0300275 } else {
Michael Krufkyeb250942008-06-19 22:07:23 -0300276 sms_err("failed to allocate firmware buffer");
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300277 rc = -ENOMEM;
278 }
Michael Krufky1fbc3ca2008-07-01 04:35:26 -0300279 sms_info("read FW %s, size=%zd", fw_filename, fw->size);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300280
281 release_firmware(fw);
282
283 return rc;
284}
285
Michael Krufky0c071f32008-06-21 02:44:02 -0300286static void smsusb1_detectmode(void *context, int *mode)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300287{
Michael Krufky18245e12008-06-15 17:52:24 -0300288 char *product_string =
289 ((struct smsusb_device_t *) context)->udev->product;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300290
291 *mode = DEVICE_MODE_NONE;
292
Michael Krufky82237412008-06-15 15:14:13 -0300293 if (!product_string) {
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300294 product_string = "none";
Michael Krufkya0c0abc2008-06-19 20:35:21 -0300295 sms_err("product string not found");
Michael Krufky82237412008-06-15 15:14:13 -0300296 } else if (strstr(product_string, "DVBH"))
297 *mode = 1;
298 else if (strstr(product_string, "BDA"))
299 *mode = 4;
300 else if (strstr(product_string, "DVBT"))
301 *mode = 0;
302 else if (strstr(product_string, "TDMB"))
303 *mode = 2;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300304
Michael Krufkya0c0abc2008-06-19 20:35:21 -0300305 sms_info("%d \"%s\"", *mode, product_string);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300306}
307
Michael Krufky0c071f32008-06-21 02:44:02 -0300308static int smsusb1_setmode(void *context, int mode)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300309{
Michael Krufky18245e12008-06-15 17:52:24 -0300310 struct SmsMsgHdr_ST Msg = { MSG_SW_RELOAD_REQ, 0, HIF_TASK,
311 sizeof(struct SmsMsgHdr_ST), 0 };
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300312
Michael Krufky82237412008-06-15 15:14:13 -0300313 if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
Michael Krufkyeb250942008-06-19 22:07:23 -0300314 sms_err("invalid firmware id specified %d", mode);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300315 return -EINVAL;
316 }
317
318 return smsusb_sendrequest(context, &Msg, sizeof(Msg));
319}
320
Michael Krufky0c071f32008-06-21 02:44:02 -0300321static void smsusb_term_device(struct usb_interface *intf)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300322{
Joe Perches8350e152010-11-30 18:42:07 -0300323 struct smsusb_device_t *dev = usb_get_intfdata(intf);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300324
Michael Krufky82237412008-06-15 15:14:13 -0300325 if (dev) {
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300326 dev->state = SMSUSB_DISCONNECTED;
327
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300328 smsusb_stop_streaming(dev);
329
Michael Krufkyfa830e82008-06-15 15:52:43 -0300330 /* unregister from smscore */
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300331 if (dev->coredev)
332 smscore_unregister_device(dev->coredev);
333
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300334 sms_info("device 0x%p destroyed", dev);
Jesper Juhl08921ac2011-04-21 18:11:25 -0300335 kfree(dev);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300336 }
337
338 usb_set_intfdata(intf, NULL);
339}
340
Michael Krufky0c071f32008-06-21 02:44:02 -0300341static int smsusb_init_device(struct usb_interface *intf, int board_id)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300342{
Michael Krufky18245e12008-06-15 17:52:24 -0300343 struct smsdevice_params_t params;
344 struct smsusb_device_t *dev;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300345 int i, rc;
346
Michael Krufkyfa830e82008-06-15 15:52:43 -0300347 /* create device object */
Michael Krufky18245e12008-06-15 17:52:24 -0300348 dev = kzalloc(sizeof(struct smsusb_device_t), GFP_KERNEL);
Michael Krufky82237412008-06-15 15:14:13 -0300349 if (!dev) {
Michael Krufkyeb250942008-06-19 22:07:23 -0300350 sms_err("kzalloc(sizeof(struct smsusb_device_t) failed");
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300351 return -ENOMEM;
352 }
353
354 memset(&params, 0, sizeof(params));
355 usb_set_intfdata(intf, dev);
356 dev->udev = interface_to_usbdev(intf);
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300357 dev->state = SMSUSB_DISCONNECTED;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300358
Michael Krufky2708e882008-06-21 05:24:38 -0300359 params.device_type = sms_get_board(board_id)->type;
Michael Krufky73104fb2008-06-14 18:27:18 -0300360
Michael Krufky2708e882008-06-21 05:24:38 -0300361 switch (params.device_type) {
Michael Krufky1c11d542008-06-18 22:09:55 -0300362 case SMS_STELLAR:
Michael Krufkyf17407a2008-06-14 00:43:26 -0300363 dev->buffer_size = USB1_BUFFER_SIZE;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300364
Michael Krufkyf17407a2008-06-14 00:43:26 -0300365 params.setmode_handler = smsusb1_setmode;
366 params.detectmode_handler = smsusb1_detectmode;
Michael Krufkyf17407a2008-06-14 00:43:26 -0300367 break;
368 default:
Michael Krufky2708e882008-06-21 05:24:38 -0300369 sms_err("Unspecified sms device type!");
370 /* fall-thru */
371 case SMS_NOVA_A0:
372 case SMS_NOVA_B0:
373 case SMS_VEGA:
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300374 case SMS_VENICE:
375 case SMS_DENVER_1530:
Michael Krufkyf17407a2008-06-14 00:43:26 -0300376 dev->buffer_size = USB2_BUFFER_SIZE;
Michael Krufky82237412008-06-15 15:14:13 -0300377 dev->response_alignment =
Uri Shkolnikc7ce8d32009-05-12 08:24:07 -0300378 le16_to_cpu(dev->udev->ep_in[1]->desc.wMaxPacketSize) -
379 sizeof(struct SmsMsgHdr_ST);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300380
Michael Krufkyf17407a2008-06-14 00:43:26 -0300381 params.flags |= SMS_DEVICE_FAMILY2;
382 break;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300383 }
384
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300385 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
386 if (intf->cur_altsetting->endpoint[i].desc. bEndpointAddress & USB_DIR_IN)
387 dev->in_ep = intf->cur_altsetting->endpoint[i].desc.bEndpointAddress;
388 else
389 dev->out_ep = intf->cur_altsetting->endpoint[i].desc.bEndpointAddress;
390 }
391
392 sms_info("in_ep = %02x, out_ep = %02x",
393 dev->in_ep, dev->out_ep);
394
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300395 params.device = &dev->udev->dev;
396 params.buffer_size = dev->buffer_size;
397 params.num_buffers = MAX_BUFFERS;
398 params.sendrequest_handler = smsusb_sendrequest;
399 params.context = dev;
Mauro Carvalho Chehab1722f3b2010-08-01 15:30:50 -0300400 usb_make_path(dev->udev, params.devpath, sizeof(params.devpath));
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300401
Michael Krufky82237412008-06-15 15:14:13 -0300402 /* register in smscore */
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300403 rc = smscore_register_device(&params, &dev->coredev);
Michael Krufky82237412008-06-15 15:14:13 -0300404 if (rc < 0) {
Michael Krufkyeb250942008-06-19 22:07:23 -0300405 sms_err("smscore_register_device(...) failed, rc %d", rc);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300406 smsusb_term_device(intf);
407 return rc;
408 }
409
Michael Krufky1c11d542008-06-18 22:09:55 -0300410 smscore_set_board_id(dev->coredev, board_id);
411
Michael Krufkyfa830e82008-06-15 15:52:43 -0300412 /* initialize urbs */
Michael Krufky82237412008-06-15 15:14:13 -0300413 for (i = 0; i < MAX_URBS; i++) {
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300414 dev->surbs[i].dev = dev;
415 usb_init_urb(&dev->surbs[i].urb);
416 }
417
Michael Krufkya0c0abc2008-06-19 20:35:21 -0300418 sms_info("smsusb_start_streaming(...).");
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300419 rc = smsusb_start_streaming(dev);
Michael Krufky82237412008-06-15 15:14:13 -0300420 if (rc < 0) {
Michael Krufkyeb250942008-06-19 22:07:23 -0300421 sms_err("smsusb_start_streaming(...) failed");
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300422 smsusb_term_device(intf);
423 return rc;
424 }
425
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300426 dev->state = SMSUSB_ACTIVE;
427
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300428 rc = smscore_start_device(dev->coredev);
Michael Krufky82237412008-06-15 15:14:13 -0300429 if (rc < 0) {
Michael Krufkyeb250942008-06-19 22:07:23 -0300430 sms_err("smscore_start_device(...) failed");
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300431 smsusb_term_device(intf);
432 return rc;
433 }
434
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300435 sms_info("device 0x%p created", dev);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300436
437 return rc;
438}
439
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800440static int smsusb_probe(struct usb_interface *intf,
Michael Krufky0c071f32008-06-21 02:44:02 -0300441 const struct usb_device_id *id)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300442{
443 struct usb_device *udev = interface_to_usbdev(intf);
Mauro Carvalho Chehab347d8f12013-03-05 22:35:44 -0300444 char devpath[32];
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300445 int i, rc;
446
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300447 sms_info("interface number %d",
448 intf->cur_altsetting->desc.bInterfaceNumber);
Michael Krufkyf17407a2008-06-14 00:43:26 -0300449
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300450 if (sms_get_board(id->driver_info)->intf_num !=
451 intf->cur_altsetting->desc.bInterfaceNumber) {
452 sms_err("interface number is %d expecting %d",
453 sms_get_board(id->driver_info)->intf_num,
454 intf->cur_altsetting->desc.bInterfaceNumber);
455 return -ENODEV;
456 }
457
458 if (intf->num_altsetting > 1) {
459 rc = usb_set_interface(udev,
460 intf->cur_altsetting->desc.bInterfaceNumber,
461 0);
Michael Krufky82237412008-06-15 15:14:13 -0300462 if (rc < 0) {
Michael Krufkyeb250942008-06-19 22:07:23 -0300463 sms_err("usb_set_interface failed, rc %d", rc);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300464 return rc;
465 }
466 }
467
Michael Krufkya0c0abc2008-06-19 20:35:21 -0300468 sms_info("smsusb_probe %d",
Michael Krufky82237412008-06-15 15:14:13 -0300469 intf->cur_altsetting->desc.bInterfaceNumber);
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300470 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
Michael Krufkya0c0abc2008-06-19 20:35:21 -0300471 sms_info("endpoint %d %02x %02x %d", i,
Michael Krufky82237412008-06-15 15:14:13 -0300472 intf->cur_altsetting->endpoint[i].desc.bEndpointAddress,
473 intf->cur_altsetting->endpoint[i].desc.bmAttributes,
474 intf->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300475 if (intf->cur_altsetting->endpoint[i].desc.bEndpointAddress &
476 USB_DIR_IN)
477 rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev,
478 intf->cur_altsetting->endpoint[i].desc.bEndpointAddress));
479 else
480 rc = usb_clear_halt(udev, usb_sndbulkpipe(udev,
481 intf->cur_altsetting->endpoint[i].desc.bEndpointAddress));
482 }
Michael Krufky82237412008-06-15 15:14:13 -0300483 if ((udev->actconfig->desc.bNumInterfaces == 2) &&
484 (intf->cur_altsetting->desc.bInterfaceNumber == 0)) {
Michael Krufkyeb250942008-06-19 22:07:23 -0300485 sms_err("rom interface 0 is not used");
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300486 return -ENODEV;
487 }
488
Mauro Carvalho Chehab347d8f12013-03-05 22:35:44 -0300489 if (id->driver_info == SMS1XXX_BOARD_SIANO_STELLAR_ROM) {
490 sms_info("stellar device was found.");
491 snprintf(devpath, sizeof(devpath), "usb\\%d-%s",
492 udev->bus->busnum, udev->devpath);
493 sms_info("stellar device was found.");
494 return smsusb1_load_firmware(
495 udev, smscore_registry_getmode(devpath),
496 id->driver_info);
497 }
498
Michael Krufky1c11d542008-06-18 22:09:55 -0300499 rc = smsusb_init_device(intf, id->driver_info);
Michael Krufkya0c0abc2008-06-19 20:35:21 -0300500 sms_info("rc %d", rc);
Michael Krufky05860f22008-08-31 17:39:58 -0300501 sms_board_load_modules(id->driver_info);
Michael Krufkyf17407a2008-06-14 00:43:26 -0300502 return rc;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300503}
504
Michael Krufky0c071f32008-06-21 02:44:02 -0300505static void smsusb_disconnect(struct usb_interface *intf)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300506{
507 smsusb_term_device(intf);
508}
509
Uri Shkolnik2f56c342008-11-22 14:08:46 -0300510static int smsusb_suspend(struct usb_interface *intf, pm_message_t msg)
511{
Joe Perches8350e152010-11-30 18:42:07 -0300512 struct smsusb_device_t *dev = usb_get_intfdata(intf);
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300513 printk(KERN_INFO "%s Entering status %d.\n", __func__, msg.event);
514 dev->state = SMSUSB_SUSPENDED;
515 /*smscore_set_power_mode(dev, SMS_POWER_MODE_SUSPENDED);*/
Uri Shkolnik2f56c342008-11-22 14:08:46 -0300516 smsusb_stop_streaming(dev);
517 return 0;
518}
519
520static int smsusb_resume(struct usb_interface *intf)
521{
522 int rc, i;
Joe Perches8350e152010-11-30 18:42:07 -0300523 struct smsusb_device_t *dev = usb_get_intfdata(intf);
Uri Shkolnik2f56c342008-11-22 14:08:46 -0300524 struct usb_device *udev = interface_to_usbdev(intf);
525
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300526 printk(KERN_INFO "%s Entering.\n", __func__);
527 usb_clear_halt(udev, usb_rcvbulkpipe(udev, dev->in_ep));
528 usb_clear_halt(udev, usb_sndbulkpipe(udev, dev->out_ep));
Uri Shkolnik2f56c342008-11-22 14:08:46 -0300529
530 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++)
531 printk(KERN_INFO "endpoint %d %02x %02x %d\n", i,
532 intf->cur_altsetting->endpoint[i].desc.bEndpointAddress,
533 intf->cur_altsetting->endpoint[i].desc.bmAttributes,
534 intf->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
535
536 if (intf->num_altsetting > 0) {
537 rc = usb_set_interface(udev,
538 intf->cur_altsetting->desc.
539 bInterfaceNumber, 0);
540 if (rc < 0) {
Michael Krufkyb9391f42008-08-31 16:08:15 -0300541 printk(KERN_INFO "%s usb_set_interface failed, "
542 "rc %d\n", __func__, rc);
Uri Shkolnik2f56c342008-11-22 14:08:46 -0300543 return rc;
544 }
545 }
546
547 smsusb_start_streaming(dev);
548 return 0;
549}
550
Greg Kroah-Hartmand04dbd12012-08-17 17:48:26 -0700551static const struct usb_device_id smsusb_id_table[] = {
Michael Krufky841a3f02008-08-31 17:03:15 -0300552 { USB_DEVICE(0x187f, 0x0010),
553 .driver_info = SMS1XXX_BOARD_SIANO_STELLAR },
554 { USB_DEVICE(0x187f, 0x0100),
555 .driver_info = SMS1XXX_BOARD_SIANO_STELLAR },
556 { USB_DEVICE(0x187f, 0x0200),
557 .driver_info = SMS1XXX_BOARD_SIANO_NOVA_A },
558 { USB_DEVICE(0x187f, 0x0201),
559 .driver_info = SMS1XXX_BOARD_SIANO_NOVA_B },
560 { USB_DEVICE(0x187f, 0x0300),
561 .driver_info = SMS1XXX_BOARD_SIANO_VEGA },
Michael Krufky841a3f02008-08-31 17:03:15 -0300562 { USB_DEVICE(0x2040, 0x1700),
563 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_CATAMOUNT },
564 { USB_DEVICE(0x2040, 0x1800),
565 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_OKEMO_A },
566 { USB_DEVICE(0x2040, 0x1801),
567 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_OKEMO_B },
568 { USB_DEVICE(0x2040, 0x2000),
569 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
570 { USB_DEVICE(0x2040, 0x2009),
571 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD_R2 },
572 { USB_DEVICE(0x2040, 0x200a),
573 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
574 { USB_DEVICE(0x2040, 0x2010),
575 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
576 { USB_DEVICE(0x2040, 0x2011),
577 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
578 { USB_DEVICE(0x2040, 0x2019),
579 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
580 { USB_DEVICE(0x2040, 0x5500),
581 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
582 { USB_DEVICE(0x2040, 0x5510),
583 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
584 { USB_DEVICE(0x2040, 0x5520),
585 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
586 { USB_DEVICE(0x2040, 0x5530),
587 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
588 { USB_DEVICE(0x2040, 0x5580),
589 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
590 { USB_DEVICE(0x2040, 0x5590),
591 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
Uri Shkolnikd0b661802009-05-19 09:51:41 -0300592 { USB_DEVICE(0x187f, 0x0202),
593 .driver_info = SMS1XXX_BOARD_SIANO_NICE },
594 { USB_DEVICE(0x187f, 0x0301),
595 .driver_info = SMS1XXX_BOARD_SIANO_VENICE },
Michael Krufky78c948a2009-10-21 18:27:29 -0300596 { USB_DEVICE(0x2040, 0xb900),
597 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
598 { USB_DEVICE(0x2040, 0xb910),
599 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
Michael Krufky20d15a22009-11-30 18:22:10 -0300600 { USB_DEVICE(0x2040, 0xb980),
601 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
602 { USB_DEVICE(0x2040, 0xb990),
603 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
Michael Krufky78c948a2009-10-21 18:27:29 -0300604 { USB_DEVICE(0x2040, 0xc000),
605 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
Michael Krufky20d15a22009-11-30 18:22:10 -0300606 { USB_DEVICE(0x2040, 0xc010),
607 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
608 { USB_DEVICE(0x2040, 0xc080),
609 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
610 { USB_DEVICE(0x2040, 0xc090),
611 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
Michael Krufky4d1b58b2012-03-22 13:55:05 -0300612 { USB_DEVICE(0x2040, 0xc0a0),
613 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
Michael Krufky3e1141e2012-05-25 09:29:12 -0300614 { USB_DEVICE(0x2040, 0xf5a0),
615 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
Mauro Carvalho Chehab347d8f12013-03-05 22:35:44 -0300616 { USB_DEVICE(0x187f, 0x0202),
617 .driver_info = SMS1XXX_BOARD_SIANO_NICE },
618 { USB_DEVICE(0x187f, 0x0301),
619 .driver_info = SMS1XXX_BOARD_SIANO_VENICE },
620 { USB_DEVICE(0x187f, 0x0302),
621 .driver_info = SMS1XXX_BOARD_SIANO_VENICE },
622 { USB_DEVICE(0x187f, 0x0310),
623 .driver_info = SMS1XXX_BOARD_SIANO_MING },
624 { USB_DEVICE(0x187f, 0x0500),
625 .driver_info = SMS1XXX_BOARD_SIANO_PELE },
626 { USB_DEVICE(0x187f, 0x0600),
627 .driver_info = SMS1XXX_BOARD_SIANO_RIO },
628 { USB_DEVICE(0x187f, 0x0700),
629 .driver_info = SMS1XXX_BOARD_SIANO_DENVER_2160 },
630 { USB_DEVICE(0x187f, 0x0800),
631 .driver_info = SMS1XXX_BOARD_SIANO_DENVER_1530 },
632 { USB_DEVICE(0x19D2, 0x0086),
633 .driver_info = SMS1XXX_BOARD_ZTE_DVB_DATA_CARD },
634 { USB_DEVICE(0x19D2, 0x0078),
635 .driver_info = SMS1XXX_BOARD_ONDA_MDTV_DATA_CARD },
Uri Shkolnikd0b661802009-05-19 09:51:41 -0300636 { } /* Terminating entry */
637 };
638
Michael Krufky841a3f02008-08-31 17:03:15 -0300639MODULE_DEVICE_TABLE(usb, smsusb_id_table);
640
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300641static struct usb_driver smsusb_driver = {
Uri Shkolnikdba843a2008-08-31 00:44:04 -0300642 .name = "smsusb",
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300643 .probe = smsusb_probe,
Michael Krufky82237412008-06-15 15:14:13 -0300644 .disconnect = smsusb_disconnect,
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300645 .id_table = smsusb_id_table,
Uri Shkolnik2f56c342008-11-22 14:08:46 -0300646
647 .suspend = smsusb_suspend,
648 .resume = smsusb_resume,
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300649};
650
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800651module_usb_driver(smsusb_driver);
Uri Shkolnike0f14c22008-08-31 00:44:04 -0300652
Uri Shkolnik05a073762009-05-17 09:17:51 -0300653MODULE_DESCRIPTION("Driver for the Siano SMS1xxx USB dongle");
Uri Shkolnike0f14c22008-08-31 00:44:04 -0300654MODULE_AUTHOR("Siano Mobile Silicon, INC. (uris@siano-ms.com)");
655MODULE_LICENSE("GPL");