blob: 89c86ee2b2253ce79ce5a0ce0fb5a5bb7da92279 [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)) {
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -030096 struct sms_msg_hdr *phdr = (struct sms_msg_hdr *)surb->cb->p;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030097
Uri Shkolnik9c9c68a2009-05-14 16:27:16 -030098 smsendian_handle_message_header(phdr);
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -030099 if (urb->actual_length >= phdr->msg_length) {
100 surb->cb->size = phdr->msg_length;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300101
Michael Krufky82237412008-06-15 15:14:13 -0300102 if (dev->response_alignment &&
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300103 (phdr->msg_flags & 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 +
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300107 ((phdr->msg_flags >> 8) & 3);
Michael Krufky82237412008-06-15 15:14:13 -0300108
109 /* sanity check */
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300110 if (((int) phdr->msg_length +
Michael Krufky82237412008-06-15 15:14:13 -0300111 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",
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300115 phdr->msg_length,
Michael Krufkyeb250942008-06-19 22:07:23 -0300116 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,
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300124 phdr, sizeof(struct sms_msg_hdr));
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",
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300129 smscore_translate_msg(phdr->msg_type),
130 phdr->msg_type, phdr->msg_length);
Mauro Carvalho Chehab4c3bdb52013-03-09 09:27:39 -0300131
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300132 smsendian_handle_rx_message((struct sms_msg_data *) phdr);
Mauro Carvalho Chehab80ccb512013-03-09 11:34:56 -0300133
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300134 smscore_onresponse(dev->coredev, surb->cb);
135 surb->cb = NULL;
Michael Krufky82237412008-06-15 15:14:13 -0300136 } else {
Michael Krufkyeb250942008-06-19 22:07:23 -0300137 sms_err("invalid response "
138 "msglen %d actual %d",
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300139 phdr->msg_length, urb->actual_length);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300140 }
Uri Shkolnik8f12d0a2009-05-12 11:28:41 -0300141 } else
142 sms_err("error, urb status %d, %d bytes",
143 urb->status, urb->actual_length);
144
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300145
146exit_and_resubmit:
147 smsusb_submit_urb(dev, surb);
148}
149
Michael Krufky0c071f32008-06-21 02:44:02 -0300150static int smsusb_submit_urb(struct smsusb_device_t *dev,
151 struct smsusb_urb_t *surb)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300152{
Michael Krufky82237412008-06-15 15:14:13 -0300153 if (!surb->cb) {
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300154 surb->cb = smscore_getbuffer(dev->coredev);
Michael Krufky82237412008-06-15 15:14:13 -0300155 if (!surb->cb) {
Michael Krufkyeb250942008-06-19 22:07:23 -0300156 sms_err("smscore_getbuffer(...) returned NULL");
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300157 return -ENOMEM;
158 }
159 }
160
161 usb_fill_bulk_urb(
162 &surb->urb,
163 dev->udev,
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300164 usb_rcvbulkpipe(dev->udev, dev->in_ep),
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300165 surb->cb->p,
166 dev->buffer_size,
167 smsusb_onresponse,
168 surb
169 );
170 surb->urb.transfer_dma = surb->cb->phys;
171 surb->urb.transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
172
173 return usb_submit_urb(&surb->urb, GFP_ATOMIC);
174}
175
Michael Krufky0c071f32008-06-21 02:44:02 -0300176static void smsusb_stop_streaming(struct smsusb_device_t *dev)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300177{
178 int i;
179
Michael Krufkyfa830e82008-06-15 15:52:43 -0300180 for (i = 0; i < MAX_URBS; i++) {
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300181 usb_kill_urb(&dev->surbs[i].urb);
182
Michael Krufky82237412008-06-15 15:14:13 -0300183 if (dev->surbs[i].cb) {
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300184 smscore_putbuffer(dev->coredev, dev->surbs[i].cb);
185 dev->surbs[i].cb = NULL;
186 }
187 }
188}
189
Michael Krufky0c071f32008-06-21 02:44:02 -0300190static int smsusb_start_streaming(struct smsusb_device_t *dev)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300191{
192 int i, rc;
193
Michael Krufkyfa830e82008-06-15 15:52:43 -0300194 for (i = 0; i < MAX_URBS; i++) {
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300195 rc = smsusb_submit_urb(dev, &dev->surbs[i]);
Michael Krufky82237412008-06-15 15:14:13 -0300196 if (rc < 0) {
Michael Krufkyeb250942008-06-19 22:07:23 -0300197 sms_err("smsusb_submit_urb(...) failed");
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300198 smsusb_stop_streaming(dev);
199 break;
200 }
201 }
202
203 return rc;
204}
205
Michael Krufky0c071f32008-06-21 02:44:02 -0300206static int smsusb_sendrequest(void *context, void *buffer, size_t size)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300207{
Michael Krufky18245e12008-06-15 17:52:24 -0300208 struct smsusb_device_t *dev = (struct smsusb_device_t *) context;
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300209 struct sms_msg_hdr *phdr = (struct sms_msg_hdr *) buffer;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300210 int dummy;
211
Mauro Carvalho Chehabf43b3962013-09-12 16:59:59 -0300212 if (dev->state != SMSUSB_ACTIVE) {
213 sms_debug("Device not active yet");
Mauro Carvalho Chehab80ccb512013-03-09 11:34:56 -0300214 return -ENOENT;
Mauro Carvalho Chehabf43b3962013-09-12 16:59:59 -0300215 }
Mauro Carvalho Chehab80ccb512013-03-09 11:34:56 -0300216
Mauro Carvalho Chehab4c3bdb52013-03-09 09:27:39 -0300217 sms_debug("sending %s(%d) size: %d",
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300218 smscore_translate_msg(phdr->msg_type), phdr->msg_type,
219 phdr->msg_length);
Mauro Carvalho Chehab4c3bdb52013-03-09 09:27:39 -0300220
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300221 smsendian_handle_tx_message((struct sms_msg_data *) phdr);
222 smsendian_handle_message_header((struct sms_msg_hdr *)buffer);
Michael Krufky82237412008-06-15 15:14:13 -0300223 return usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 2),
224 buffer, size, &dummy, 1000);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300225}
226
Michael Krufky0c071f32008-06-21 02:44:02 -0300227static char *smsusb1_fw_lkup[] = {
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300228 "dvbt_stellar_usb.inp",
229 "dvbh_stellar_usb.inp",
230 "tdmb_stellar_usb.inp",
231 "none",
232 "dvbt_bda_stellar_usb.inp",
233};
234
Michael Krufky1fbc3ca2008-07-01 04:35:26 -0300235static inline char *sms_get_fw_name(int mode, int board_id)
236{
237 char **fw = sms_get_board(board_id)->fw;
238 return (fw && fw[mode]) ? fw[mode] : smsusb1_fw_lkup[mode];
239}
240
241static int smsusb1_load_firmware(struct usb_device *udev, int id, int board_id)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300242{
243 const struct firmware *fw;
Michael Krufkya83ccdd2008-05-06 03:11:51 -0300244 u8 *fw_buffer;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300245 int rc, dummy;
Michael Krufky1fbc3ca2008-07-01 04:35:26 -0300246 char *fw_filename;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300247
Mauro Carvalho Chehabb00ade22013-09-12 17:04:07 -0300248 if (id < 0)
249 id = sms_get_board(board_id)->default_mode;
250
Michael Krufky82237412008-06-15 15:14:13 -0300251 if (id < DEVICE_MODE_DVBT || id > DEVICE_MODE_DVBT_BDA) {
Michael Krufkyeb250942008-06-19 22:07:23 -0300252 sms_err("invalid firmware id specified %d", id);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300253 return -EINVAL;
254 }
255
Michael Krufky1fbc3ca2008-07-01 04:35:26 -0300256 fw_filename = sms_get_fw_name(id, board_id);
257
258 rc = request_firmware(&fw, fw_filename, &udev->dev);
Michael Krufky82237412008-06-15 15:14:13 -0300259 if (rc < 0) {
Michael Krufky1fbc3ca2008-07-01 04:35:26 -0300260 sms_warn("failed to open \"%s\" mode %d, "
261 "trying again with default firmware", fw_filename, id);
262
263 fw_filename = smsusb1_fw_lkup[id];
264 rc = request_firmware(&fw, fw_filename, &udev->dev);
265 if (rc < 0) {
266 sms_warn("failed to open \"%s\" mode %d",
267 fw_filename, id);
268
269 return rc;
270 }
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300271 }
272
273 fw_buffer = kmalloc(fw->size, GFP_KERNEL);
Michael Krufky82237412008-06-15 15:14:13 -0300274 if (fw_buffer) {
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300275 memcpy(fw_buffer, fw->data, fw->size);
276
Michael Krufky82237412008-06-15 15:14:13 -0300277 rc = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 2),
278 fw_buffer, fw->size, &dummy, 1000);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300279
Michael Krufky0f2a1ee2008-06-28 20:53:45 -0300280 sms_info("sent %zd(%d) bytes, rc %d", fw->size, dummy, rc);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300281
282 kfree(fw_buffer);
Michael Krufky82237412008-06-15 15:14:13 -0300283 } else {
Michael Krufkyeb250942008-06-19 22:07:23 -0300284 sms_err("failed to allocate firmware buffer");
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300285 rc = -ENOMEM;
286 }
Michael Krufky1fbc3ca2008-07-01 04:35:26 -0300287 sms_info("read FW %s, size=%zd", fw_filename, fw->size);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300288
289 release_firmware(fw);
290
291 return rc;
292}
293
Michael Krufky0c071f32008-06-21 02:44:02 -0300294static void smsusb1_detectmode(void *context, int *mode)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300295{
Michael Krufky18245e12008-06-15 17:52:24 -0300296 char *product_string =
297 ((struct smsusb_device_t *) context)->udev->product;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300298
299 *mode = DEVICE_MODE_NONE;
300
Michael Krufky82237412008-06-15 15:14:13 -0300301 if (!product_string) {
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300302 product_string = "none";
Michael Krufkya0c0abc2008-06-19 20:35:21 -0300303 sms_err("product string not found");
Michael Krufky82237412008-06-15 15:14:13 -0300304 } else if (strstr(product_string, "DVBH"))
305 *mode = 1;
306 else if (strstr(product_string, "BDA"))
307 *mode = 4;
308 else if (strstr(product_string, "DVBT"))
309 *mode = 0;
310 else if (strstr(product_string, "TDMB"))
311 *mode = 2;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300312
Michael Krufkya0c0abc2008-06-19 20:35:21 -0300313 sms_info("%d \"%s\"", *mode, product_string);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300314}
315
Michael Krufky0c071f32008-06-21 02:44:02 -0300316static int smsusb1_setmode(void *context, int mode)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300317{
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300318 struct sms_msg_hdr msg = { MSG_SW_RELOAD_REQ, 0, HIF_TASK,
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300319 sizeof(struct sms_msg_hdr), 0 };
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300320
Michael Krufky82237412008-06-15 15:14:13 -0300321 if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
Michael Krufkyeb250942008-06-19 22:07:23 -0300322 sms_err("invalid firmware id specified %d", mode);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300323 return -EINVAL;
324 }
325
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300326 return smsusb_sendrequest(context, &msg, sizeof(msg));
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300327}
328
Michael Krufky0c071f32008-06-21 02:44:02 -0300329static void smsusb_term_device(struct usb_interface *intf)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300330{
Joe Perches8350e152010-11-30 18:42:07 -0300331 struct smsusb_device_t *dev = usb_get_intfdata(intf);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300332
Michael Krufky82237412008-06-15 15:14:13 -0300333 if (dev) {
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300334 dev->state = SMSUSB_DISCONNECTED;
335
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300336 smsusb_stop_streaming(dev);
337
Michael Krufkyfa830e82008-06-15 15:52:43 -0300338 /* unregister from smscore */
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300339 if (dev->coredev)
340 smscore_unregister_device(dev->coredev);
341
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300342 sms_info("device 0x%p destroyed", dev);
Jesper Juhl08921ac2011-04-21 18:11:25 -0300343 kfree(dev);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300344 }
345
346 usb_set_intfdata(intf, NULL);
347}
348
Michael Krufky0c071f32008-06-21 02:44:02 -0300349static int smsusb_init_device(struct usb_interface *intf, int board_id)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300350{
Michael Krufky18245e12008-06-15 17:52:24 -0300351 struct smsdevice_params_t params;
352 struct smsusb_device_t *dev;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300353 int i, rc;
354
Michael Krufkyfa830e82008-06-15 15:52:43 -0300355 /* create device object */
Michael Krufky18245e12008-06-15 17:52:24 -0300356 dev = kzalloc(sizeof(struct smsusb_device_t), GFP_KERNEL);
Michael Krufky82237412008-06-15 15:14:13 -0300357 if (!dev) {
Michael Krufkyeb250942008-06-19 22:07:23 -0300358 sms_err("kzalloc(sizeof(struct smsusb_device_t) failed");
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300359 return -ENOMEM;
360 }
361
362 memset(&params, 0, sizeof(params));
363 usb_set_intfdata(intf, dev);
364 dev->udev = interface_to_usbdev(intf);
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300365 dev->state = SMSUSB_DISCONNECTED;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300366
Michael Krufky2708e882008-06-21 05:24:38 -0300367 params.device_type = sms_get_board(board_id)->type;
Michael Krufky73104fb2008-06-14 18:27:18 -0300368
Michael Krufky2708e882008-06-21 05:24:38 -0300369 switch (params.device_type) {
Michael Krufky1c11d542008-06-18 22:09:55 -0300370 case SMS_STELLAR:
Michael Krufkyf17407a2008-06-14 00:43:26 -0300371 dev->buffer_size = USB1_BUFFER_SIZE;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300372
Michael Krufkyf17407a2008-06-14 00:43:26 -0300373 params.setmode_handler = smsusb1_setmode;
374 params.detectmode_handler = smsusb1_detectmode;
Michael Krufkyf17407a2008-06-14 00:43:26 -0300375 break;
Mauro Carvalho Chehab07bb6bd2013-03-19 12:59:50 -0300376 case SMS_UNKNOWN_TYPE:
Michael Krufky2708e882008-06-21 05:24:38 -0300377 sms_err("Unspecified sms device type!");
378 /* fall-thru */
Mauro Carvalho Chehab07bb6bd2013-03-19 12:59:50 -0300379 default:
Michael Krufkyf17407a2008-06-14 00:43:26 -0300380 dev->buffer_size = USB2_BUFFER_SIZE;
Michael Krufky82237412008-06-15 15:14:13 -0300381 dev->response_alignment =
Uri Shkolnikc7ce8d32009-05-12 08:24:07 -0300382 le16_to_cpu(dev->udev->ep_in[1]->desc.wMaxPacketSize) -
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300383 sizeof(struct sms_msg_hdr);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300384
Michael Krufkyf17407a2008-06-14 00:43:26 -0300385 params.flags |= SMS_DEVICE_FAMILY2;
386 break;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300387 }
388
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300389 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
390 if (intf->cur_altsetting->endpoint[i].desc. bEndpointAddress & USB_DIR_IN)
391 dev->in_ep = intf->cur_altsetting->endpoint[i].desc.bEndpointAddress;
392 else
393 dev->out_ep = intf->cur_altsetting->endpoint[i].desc.bEndpointAddress;
394 }
395
396 sms_info("in_ep = %02x, out_ep = %02x",
397 dev->in_ep, dev->out_ep);
398
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300399 params.device = &dev->udev->dev;
400 params.buffer_size = dev->buffer_size;
401 params.num_buffers = MAX_BUFFERS;
402 params.sendrequest_handler = smsusb_sendrequest;
403 params.context = dev;
Mauro Carvalho Chehab1722f3b2010-08-01 15:30:50 -0300404 usb_make_path(dev->udev, params.devpath, sizeof(params.devpath));
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300405
Michael Krufky82237412008-06-15 15:14:13 -0300406 /* register in smscore */
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300407 rc = smscore_register_device(&params, &dev->coredev);
Michael Krufky82237412008-06-15 15:14:13 -0300408 if (rc < 0) {
Michael Krufkyeb250942008-06-19 22:07:23 -0300409 sms_err("smscore_register_device(...) failed, rc %d", rc);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300410 smsusb_term_device(intf);
411 return rc;
412 }
413
Michael Krufky1c11d542008-06-18 22:09:55 -0300414 smscore_set_board_id(dev->coredev, board_id);
415
Mauro Carvalho Chehab503efe52013-03-10 09:04:44 -0300416 dev->coredev->is_usb_device = true;
417
Michael Krufkyfa830e82008-06-15 15:52:43 -0300418 /* initialize urbs */
Michael Krufky82237412008-06-15 15:14:13 -0300419 for (i = 0; i < MAX_URBS; i++) {
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300420 dev->surbs[i].dev = dev;
421 usb_init_urb(&dev->surbs[i].urb);
422 }
423
Michael Krufkya0c0abc2008-06-19 20:35:21 -0300424 sms_info("smsusb_start_streaming(...).");
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300425 rc = smsusb_start_streaming(dev);
Michael Krufky82237412008-06-15 15:14:13 -0300426 if (rc < 0) {
Michael Krufkyeb250942008-06-19 22:07:23 -0300427 sms_err("smsusb_start_streaming(...) failed");
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300428 smsusb_term_device(intf);
429 return rc;
430 }
431
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300432 dev->state = SMSUSB_ACTIVE;
433
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300434 rc = smscore_start_device(dev->coredev);
Michael Krufky82237412008-06-15 15:14:13 -0300435 if (rc < 0) {
Michael Krufkyeb250942008-06-19 22:07:23 -0300436 sms_err("smscore_start_device(...) failed");
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300437 smsusb_term_device(intf);
438 return rc;
439 }
440
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300441 sms_info("device 0x%p created", dev);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300442
443 return rc;
444}
445
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800446static int smsusb_probe(struct usb_interface *intf,
Michael Krufky0c071f32008-06-21 02:44:02 -0300447 const struct usb_device_id *id)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300448{
449 struct usb_device *udev = interface_to_usbdev(intf);
Mauro Carvalho Chehab347d8f12013-03-05 22:35:44 -0300450 char devpath[32];
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300451 int i, rc;
452
Mauro Carvalho Chehabf43b3962013-09-12 16:59:59 -0300453 sms_info("board id=%lu, interface number %d",
454 id->driver_info,
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300455 intf->cur_altsetting->desc.bInterfaceNumber);
Michael Krufkyf17407a2008-06-14 00:43:26 -0300456
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300457 if (sms_get_board(id->driver_info)->intf_num !=
458 intf->cur_altsetting->desc.bInterfaceNumber) {
Mauro Carvalho Chehabf43b3962013-09-12 16:59:59 -0300459 sms_debug("interface %d won't be used. Expecting interface %d to popup",
460 intf->cur_altsetting->desc.bInterfaceNumber,
461 sms_get_board(id->driver_info)->intf_num);
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300462 return -ENODEV;
463 }
464
465 if (intf->num_altsetting > 1) {
466 rc = usb_set_interface(udev,
467 intf->cur_altsetting->desc.bInterfaceNumber,
468 0);
Michael Krufky82237412008-06-15 15:14:13 -0300469 if (rc < 0) {
Michael Krufkyeb250942008-06-19 22:07:23 -0300470 sms_err("usb_set_interface failed, rc %d", rc);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300471 return rc;
472 }
473 }
474
Michael Krufkya0c0abc2008-06-19 20:35:21 -0300475 sms_info("smsusb_probe %d",
Michael Krufky82237412008-06-15 15:14:13 -0300476 intf->cur_altsetting->desc.bInterfaceNumber);
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300477 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
Michael Krufkya0c0abc2008-06-19 20:35:21 -0300478 sms_info("endpoint %d %02x %02x %d", i,
Michael Krufky82237412008-06-15 15:14:13 -0300479 intf->cur_altsetting->endpoint[i].desc.bEndpointAddress,
480 intf->cur_altsetting->endpoint[i].desc.bmAttributes,
481 intf->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300482 if (intf->cur_altsetting->endpoint[i].desc.bEndpointAddress &
483 USB_DIR_IN)
484 rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev,
485 intf->cur_altsetting->endpoint[i].desc.bEndpointAddress));
486 else
487 rc = usb_clear_halt(udev, usb_sndbulkpipe(udev,
488 intf->cur_altsetting->endpoint[i].desc.bEndpointAddress));
489 }
Michael Krufky82237412008-06-15 15:14:13 -0300490 if ((udev->actconfig->desc.bNumInterfaces == 2) &&
491 (intf->cur_altsetting->desc.bInterfaceNumber == 0)) {
Mauro Carvalho Chehabf43b3962013-09-12 16:59:59 -0300492 sms_debug("rom interface 0 is not used");
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300493 return -ENODEV;
494 }
495
Mauro Carvalho Chehab347d8f12013-03-05 22:35:44 -0300496 if (id->driver_info == SMS1XXX_BOARD_SIANO_STELLAR_ROM) {
Mauro Carvalho Chehab811a4f62013-09-12 17:00:00 -0300497 /* Detected a Siano Stellar uninitialized */
498
Mauro Carvalho Chehab347d8f12013-03-05 22:35:44 -0300499 snprintf(devpath, sizeof(devpath), "usb\\%d-%s",
500 udev->bus->busnum, udev->devpath);
Mauro Carvalho Chehab811a4f62013-09-12 17:00:00 -0300501 sms_info("stellar device in cold state was found at %s.", devpath);
502 rc = smsusb1_load_firmware(
Mauro Carvalho Chehab347d8f12013-03-05 22:35:44 -0300503 udev, smscore_registry_getmode(devpath),
504 id->driver_info);
Mauro Carvalho Chehab811a4f62013-09-12 17:00:00 -0300505
506 /* This device will reset and gain another USB ID */
507 if (!rc)
508 sms_info("stellar device now in warm state");
509 else
510 sms_err("Failed to put stellar in warm state. Error: %d", rc);
511
512 return rc;
513 } else {
514 rc = smsusb_init_device(intf, id->driver_info);
Mauro Carvalho Chehab347d8f12013-03-05 22:35:44 -0300515 }
516
Mauro Carvalho Chehabf43b3962013-09-12 16:59:59 -0300517 sms_info("Device initialized with return code %d", rc);
Michael Krufky05860f22008-08-31 17:39:58 -0300518 sms_board_load_modules(id->driver_info);
Michael Krufkyf17407a2008-06-14 00:43:26 -0300519 return rc;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300520}
521
Michael Krufky0c071f32008-06-21 02:44:02 -0300522static void smsusb_disconnect(struct usb_interface *intf)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300523{
524 smsusb_term_device(intf);
525}
526
Uri Shkolnik2f56c342008-11-22 14:08:46 -0300527static int smsusb_suspend(struct usb_interface *intf, pm_message_t msg)
528{
Joe Perches8350e152010-11-30 18:42:07 -0300529 struct smsusb_device_t *dev = usb_get_intfdata(intf);
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300530 printk(KERN_INFO "%s Entering status %d.\n", __func__, msg.event);
531 dev->state = SMSUSB_SUSPENDED;
532 /*smscore_set_power_mode(dev, SMS_POWER_MODE_SUSPENDED);*/
Uri Shkolnik2f56c342008-11-22 14:08:46 -0300533 smsusb_stop_streaming(dev);
534 return 0;
535}
536
537static int smsusb_resume(struct usb_interface *intf)
538{
539 int rc, i;
Joe Perches8350e152010-11-30 18:42:07 -0300540 struct smsusb_device_t *dev = usb_get_intfdata(intf);
Uri Shkolnik2f56c342008-11-22 14:08:46 -0300541 struct usb_device *udev = interface_to_usbdev(intf);
542
Mauro Carvalho Chehab05f0ffb2013-03-06 09:53:50 -0300543 printk(KERN_INFO "%s Entering.\n", __func__);
544 usb_clear_halt(udev, usb_rcvbulkpipe(udev, dev->in_ep));
545 usb_clear_halt(udev, usb_sndbulkpipe(udev, dev->out_ep));
Uri Shkolnik2f56c342008-11-22 14:08:46 -0300546
547 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++)
548 printk(KERN_INFO "endpoint %d %02x %02x %d\n", i,
549 intf->cur_altsetting->endpoint[i].desc.bEndpointAddress,
550 intf->cur_altsetting->endpoint[i].desc.bmAttributes,
551 intf->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
552
553 if (intf->num_altsetting > 0) {
554 rc = usb_set_interface(udev,
555 intf->cur_altsetting->desc.
556 bInterfaceNumber, 0);
557 if (rc < 0) {
Michael Krufkyb9391f42008-08-31 16:08:15 -0300558 printk(KERN_INFO "%s usb_set_interface failed, "
559 "rc %d\n", __func__, rc);
Uri Shkolnik2f56c342008-11-22 14:08:46 -0300560 return rc;
561 }
562 }
563
564 smsusb_start_streaming(dev);
565 return 0;
566}
567
Greg Kroah-Hartmand04dbd12012-08-17 17:48:26 -0700568static const struct usb_device_id smsusb_id_table[] = {
Mauro Carvalho Chehab811a4f62013-09-12 17:00:00 -0300569 /* This device is only present before firmware load */
Michael Krufky841a3f02008-08-31 17:03:15 -0300570 { USB_DEVICE(0x187f, 0x0010),
Mauro Carvalho Chehab811a4f62013-09-12 17:00:00 -0300571 .driver_info = SMS1XXX_BOARD_SIANO_STELLAR_ROM },
572 /* This device pops up after firmware load */
Michael Krufky841a3f02008-08-31 17:03:15 -0300573 { USB_DEVICE(0x187f, 0x0100),
574 .driver_info = SMS1XXX_BOARD_SIANO_STELLAR },
Mauro Carvalho Chehab811a4f62013-09-12 17:00:00 -0300575
Michael Krufky841a3f02008-08-31 17:03:15 -0300576 { USB_DEVICE(0x187f, 0x0200),
577 .driver_info = SMS1XXX_BOARD_SIANO_NOVA_A },
578 { USB_DEVICE(0x187f, 0x0201),
579 .driver_info = SMS1XXX_BOARD_SIANO_NOVA_B },
580 { USB_DEVICE(0x187f, 0x0300),
581 .driver_info = SMS1XXX_BOARD_SIANO_VEGA },
Michael Krufky841a3f02008-08-31 17:03:15 -0300582 { USB_DEVICE(0x2040, 0x1700),
583 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_CATAMOUNT },
584 { USB_DEVICE(0x2040, 0x1800),
585 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_OKEMO_A },
586 { USB_DEVICE(0x2040, 0x1801),
587 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_OKEMO_B },
588 { USB_DEVICE(0x2040, 0x2000),
589 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
590 { USB_DEVICE(0x2040, 0x2009),
591 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD_R2 },
592 { USB_DEVICE(0x2040, 0x200a),
593 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
594 { USB_DEVICE(0x2040, 0x2010),
595 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
596 { USB_DEVICE(0x2040, 0x2011),
597 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
598 { USB_DEVICE(0x2040, 0x2019),
599 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
600 { USB_DEVICE(0x2040, 0x5500),
601 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
602 { USB_DEVICE(0x2040, 0x5510),
603 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
604 { USB_DEVICE(0x2040, 0x5520),
605 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
606 { USB_DEVICE(0x2040, 0x5530),
607 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
608 { USB_DEVICE(0x2040, 0x5580),
609 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
610 { USB_DEVICE(0x2040, 0x5590),
611 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
Uri Shkolnikd0b661802009-05-19 09:51:41 -0300612 { USB_DEVICE(0x187f, 0x0202),
613 .driver_info = SMS1XXX_BOARD_SIANO_NICE },
614 { USB_DEVICE(0x187f, 0x0301),
615 .driver_info = SMS1XXX_BOARD_SIANO_VENICE },
Michael Krufky78c948a2009-10-21 18:27:29 -0300616 { USB_DEVICE(0x2040, 0xb900),
617 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
618 { USB_DEVICE(0x2040, 0xb910),
619 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
Michael Krufky20d15a22009-11-30 18:22:10 -0300620 { USB_DEVICE(0x2040, 0xb980),
621 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
622 { USB_DEVICE(0x2040, 0xb990),
623 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
Michael Krufky78c948a2009-10-21 18:27:29 -0300624 { USB_DEVICE(0x2040, 0xc000),
625 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
Michael Krufky20d15a22009-11-30 18:22:10 -0300626 { USB_DEVICE(0x2040, 0xc010),
627 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
628 { USB_DEVICE(0x2040, 0xc080),
629 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
630 { USB_DEVICE(0x2040, 0xc090),
631 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
Michael Krufky4d1b58b2012-03-22 13:55:05 -0300632 { USB_DEVICE(0x2040, 0xc0a0),
633 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
Michael Krufky3e1141e2012-05-25 09:29:12 -0300634 { USB_DEVICE(0x2040, 0xf5a0),
635 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
Mauro Carvalho Chehab347d8f12013-03-05 22:35:44 -0300636 { USB_DEVICE(0x187f, 0x0202),
637 .driver_info = SMS1XXX_BOARD_SIANO_NICE },
638 { USB_DEVICE(0x187f, 0x0301),
639 .driver_info = SMS1XXX_BOARD_SIANO_VENICE },
640 { USB_DEVICE(0x187f, 0x0302),
641 .driver_info = SMS1XXX_BOARD_SIANO_VENICE },
642 { USB_DEVICE(0x187f, 0x0310),
643 .driver_info = SMS1XXX_BOARD_SIANO_MING },
644 { USB_DEVICE(0x187f, 0x0500),
645 .driver_info = SMS1XXX_BOARD_SIANO_PELE },
646 { USB_DEVICE(0x187f, 0x0600),
647 .driver_info = SMS1XXX_BOARD_SIANO_RIO },
648 { USB_DEVICE(0x187f, 0x0700),
649 .driver_info = SMS1XXX_BOARD_SIANO_DENVER_2160 },
650 { USB_DEVICE(0x187f, 0x0800),
651 .driver_info = SMS1XXX_BOARD_SIANO_DENVER_1530 },
652 { USB_DEVICE(0x19D2, 0x0086),
653 .driver_info = SMS1XXX_BOARD_ZTE_DVB_DATA_CARD },
654 { USB_DEVICE(0x19D2, 0x0078),
655 .driver_info = SMS1XXX_BOARD_ONDA_MDTV_DATA_CARD },
Satoshi Nagahamaf61e2262014-02-10 06:45:29 -0300656 { USB_DEVICE(0x3275, 0x0080),
657 .driver_info = SMS1XXX_BOARD_SIANO_RIO },
Mauro Carvalho Chehab29bbb7b2014-08-11 18:09:32 -0300658 { USB_DEVICE(0x2013, 0x0257),
659 .driver_info = SMS1XXX_BOARD_PCTV_77E },
Uri Shkolnikd0b661802009-05-19 09:51:41 -0300660 { } /* Terminating entry */
661 };
662
Michael Krufky841a3f02008-08-31 17:03:15 -0300663MODULE_DEVICE_TABLE(usb, smsusb_id_table);
664
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300665static struct usb_driver smsusb_driver = {
Uri Shkolnikdba843a2008-08-31 00:44:04 -0300666 .name = "smsusb",
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300667 .probe = smsusb_probe,
Michael Krufky82237412008-06-15 15:14:13 -0300668 .disconnect = smsusb_disconnect,
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300669 .id_table = smsusb_id_table,
Uri Shkolnik2f56c342008-11-22 14:08:46 -0300670
671 .suspend = smsusb_suspend,
672 .resume = smsusb_resume,
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300673};
674
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800675module_usb_driver(smsusb_driver);
Uri Shkolnike0f14c22008-08-31 00:44:04 -0300676
Uri Shkolnik05a073762009-05-17 09:17:51 -0300677MODULE_DESCRIPTION("Driver for the Siano SMS1xxx USB dongle");
Uri Shkolnike0f14c22008-08-31 00:44:04 -0300678MODULE_AUTHOR("Siano Mobile Silicon, INC. (uris@siano-ms.com)");
679MODULE_LICENSE("GPL");