/*
 * DVB USB Linux driver for Anysee E30 DVB-C & DVB-T USB2.0 receiver
 *
 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * TODO:
 * - add smart card reader support for Conditional Access (CA)
 *
 * Card reader in Anysee is nothing more than ISO 7816 card reader.
 * There is no hardware CAM in any Anysee device sold.
 * In my understanding it should be implemented by making own module
 * for ISO 7816 card reader, like dvb_ca_en50221 is implemented. This
 * module registers serial interface that can be used to communicate
 * with any ISO 7816 smart card.
 *
 * Any help according to implement serial smart card reader support
 * is highly welcome!
 */

#ifndef _DVB_USB_ANYSEE_H_
#define _DVB_USB_ANYSEE_H_

#define DVB_USB_LOG_PREFIX "anysee"
#include "dvb-usb.h"

#define deb_info(args...) dprintk(dvb_usb_anysee_debug, 0x01, args)
#define deb_xfer(args...) dprintk(dvb_usb_anysee_debug, 0x02, args)
#define deb_rc(args...)   dprintk(dvb_usb_anysee_debug, 0x04, args)
#define deb_reg(args...)  dprintk(dvb_usb_anysee_debug, 0x08, args)
#define deb_i2c(args...)  dprintk(dvb_usb_anysee_debug, 0x10, args)
#define deb_fw(args...)   dprintk(dvb_usb_anysee_debug, 0x20, args)


#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/serial.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/uaccess.h>
#include <linux/usb.h>
#include <linux/usb/cdc.h>
#include <asm/byteorder.h>
#include <asm/unaligned.h>
#include <linux/list.h>

enum cmd {
	CMD_I2C_READ            = 0x33,
	CMD_I2C_WRITE           = 0x31,
	CMD_REG_READ            = 0xb0,
	CMD_REG_WRITE           = 0xb1,
	CMD_STREAMING_CTRL      = 0x12,
	CMD_LED_AND_IR_CTRL     = 0x16,
	CMD_GET_IR_CODE         = 0x41,
	CMD_GET_HW_INFO         = 0x19,
	CMD_SMARTCARD           = 0x34,
};

#define SERIAL_TTY_MAJOR	188	/* Nice legal number now */
#define SERIAL_TTY_MINORS	254	/* loads of devices :) */
#define SERIAL_TTY_NO_MINOR	255	/* No minor was assigned */

//#define ACM_TTY_MAJOR		166
//#define ACM_TTY_MINORS		32

#define ACM_TTY_MAJOR		SERIAL_TTY_MAJOR
#define ACM_TTY_MINORS		SERIAL_TTY_MINORS

/*
 * Output control lines.
 */

#define ACM_CTRL_DTR		0x01
#define ACM_CTRL_RTS		0x02

/*
 * Input control lines and line errors.
 */

#define ACM_CTRL_DCD		0x01
#define ACM_CTRL_DSR		0x02
#define ACM_CTRL_BRK		0x04
#define ACM_CTRL_RI		0x08

#define ACM_CTRL_FRAMING	0x10
#define ACM_CTRL_PARITY		0x20
#define ACM_CTRL_OVERRUN	0x40

struct acm {
	struct dvb_usb_device *dvb_dev;
//	struct usb_device *dev;				/* the corresponding usb device */
//	struct usb_interface *control;			/* control interface */
//	struct usb_interface *data;			/* data interface */
	struct tty_port port;			 	/* our tty port data */
//	struct urb *ctrlurb;				/* urbs */
	u8 *ctrl_buffer;				/* buffers of urbs */
	dma_addr_t ctrl_dma;				/* dma handles of buffers */
//	u8 *country_codes;				/* country codes from device */
//	unsigned int country_code_size;			/* size of this buffer */
//	struct acm_wb wb[ACM_NW];
//	struct acm_ru ru[ACM_NR];
//	struct acm_rb rb[ACM_NR];
	int rx_buflimit;
	int rx_endpoint;
	spinlock_t read_lock;
	struct list_head spare_read_urbs;
	struct list_head spare_read_bufs;
	struct list_head filled_read_bufs;
	int write_used;					/* number of non-empty write buffers */
	int processing;
	int transmitting;
	spinlock_t write_lock;
	struct mutex mutex;
	struct usb_cdc_line_coding line;		/* bits, stop, parity */
	struct work_struct work;			/* work queue entry for line discipline waking up */
	wait_queue_head_t drain_wait;			/* close processing */
	struct tasklet_struct urb_task;                 /* rx processing */
	spinlock_t throttle_lock;			/* synchronize throtteling and read callback */
	unsigned int ctrlin;				/* input control lines (DCD, DSR, RI, break, overruns) */
	unsigned int ctrlout;				/* output control lines (DTR, RTS) */
	unsigned int writesize;				/* max packet size for the output bulk endpoint */
	unsigned int readsize,ctrlsize;			/* buffer sizes for freeing */
	unsigned int minor;				/* acm minor number */
	unsigned char throttle;				/* throttled by tty layer */
	unsigned char clocal;				/* termios CLOCAL */
	unsigned int ctrl_caps;				/* control capabilities from the class specific header */
	unsigned int susp_count;			/* number of suspended interfaces */
	unsigned int combined_interfaces:1;		/* control and data collapsed */
//	unsigned int is_int_ep:1;			/* interrupt endpoints contrary to spec used */
//	u8 bInterval;
	struct acm_wb *delayed_wb;			/* write queued for a device about to be woken */
};

#define CDC_DATA_INTERFACE_TYPE	0x0a

/* constants describing various quirks and errors */
#define NO_UNION_NORMAL			1
#define SINGLE_RX_URB			2
#define NO_CAP_LINE			4
#define NOT_A_MODEM			8


struct anysee_state {
	u8 tuner;
	u8 seq;
//	struct usb_serial usb_serial;
};

#endif

/***************************************************************************
 * USB API description (reverse engineered)
 ***************************************************************************

Transaction flow:
=================
BULK[00001] >>> REQUEST PACKET 64 bytes
BULK[00081] <<< REPLY PACKET #1 64 bytes (PREVIOUS TRANSACTION REPLY)
BULK[00081] <<< REPLY PACKET #2 64 bytes (CURRENT TRANSACTION REPLY)

General reply packet(s) are always used if not own reply defined.

============================================================================
| 00-63 | GENERAL REPLY PACKET #1 (PREVIOUS REPLY)
============================================================================
|    00 | reply data (if any) from previous transaction
|       | Just same reply packet as returned during previous transaction.
|       | Needed only if reply is missed in previous transaction.
|       | Just skip normally.
----------------------------------------------------------------------------
| 01-59 | don't care
----------------------------------------------------------------------------
|    60 | packet sequence number
----------------------------------------------------------------------------
| 61-63 | don't care
----------------------------------------------------------------------------

============================================================================
| 00-63 | GENERAL REPLY PACKET #2 (CURRENT REPLY)
============================================================================
|    00 | reply data (if any)
----------------------------------------------------------------------------
| 01-59 | don't care
----------------------------------------------------------------------------
|    60 | packet sequence number
----------------------------------------------------------------------------
| 61-63 | don't care
----------------------------------------------------------------------------

============================================================================
| 00-63 | I2C WRITE REQUEST PACKET
============================================================================
|    00 | 0x31 I2C write command
----------------------------------------------------------------------------
|    01 | i2c address
----------------------------------------------------------------------------
|    02 | data length
|       | 0x02 (for typical I2C reg / val pair)
----------------------------------------------------------------------------
|    03 | 0x01
----------------------------------------------------------------------------
| 04-   | data
----------------------------------------------------------------------------
|   -59 | don't care
----------------------------------------------------------------------------
|    60 | packet sequence number
----------------------------------------------------------------------------
| 61-63 | don't care
----------------------------------------------------------------------------

============================================================================
| 00-63 | I2C READ REQUEST PACKET
============================================================================
|    00 | 0x33 I2C read command
----------------------------------------------------------------------------
|    01 | i2c address + 1
----------------------------------------------------------------------------
|    02 | register
----------------------------------------------------------------------------
|    03 | 0x00
----------------------------------------------------------------------------
|    04 | 0x00
----------------------------------------------------------------------------
|    05 | 0x01
----------------------------------------------------------------------------
| 06-59 | don't care
----------------------------------------------------------------------------
|    60 | packet sequence number
----------------------------------------------------------------------------
| 61-63 | don't care
----------------------------------------------------------------------------

============================================================================
| 00-63 | USB CONTROLLER REGISTER WRITE REQUEST PACKET
============================================================================
|    00 | 0xb1 register write command
----------------------------------------------------------------------------
| 01-02 | register
----------------------------------------------------------------------------
|    03 | 0x01
----------------------------------------------------------------------------
|    04 | value
----------------------------------------------------------------------------
| 05-59 | don't care
----------------------------------------------------------------------------
|    60 | packet sequence number
----------------------------------------------------------------------------
| 61-63 | don't care
----------------------------------------------------------------------------

============================================================================
| 00-63 | USB CONTROLLER REGISTER READ REQUEST PACKET
============================================================================
|    00 | 0xb0 register read command
----------------------------------------------------------------------------
| 01-02 | register
----------------------------------------------------------------------------
|    03 | 0x01
----------------------------------------------------------------------------
| 04-59 | don't care
----------------------------------------------------------------------------
|    60 | packet sequence number
----------------------------------------------------------------------------
| 61-63 | don't care
----------------------------------------------------------------------------

============================================================================
| 00-63 | LED CONTROL REQUEST PACKET
============================================================================
|    00 | 0x16 LED and IR control command
----------------------------------------------------------------------------
|    01 | 0x01 (LED)
----------------------------------------------------------------------------
|    03 | 0x00 blink
|       | 0x01 lights continuously
----------------------------------------------------------------------------
|    04 | blink interval
|       | 0x00 fastest (looks like LED lights continuously)
|       | 0xff slowest
----------------------------------------------------------------------------
| 05-59 | don't care
----------------------------------------------------------------------------
|    60 | packet sequence number
----------------------------------------------------------------------------
| 61-63 | don't care
----------------------------------------------------------------------------

============================================================================
| 00-63 | IR CONTROL REQUEST PACKET
============================================================================
|    00 | 0x16 LED and IR control command
----------------------------------------------------------------------------
|    01 | 0x02 (IR)
----------------------------------------------------------------------------
|    03 | 0x00 IR disabled
|       | 0x01 IR enabled
----------------------------------------------------------------------------
| 04-59 | don't care
----------------------------------------------------------------------------
|    60 | packet sequence number
----------------------------------------------------------------------------
| 61-63 | don't care
----------------------------------------------------------------------------

============================================================================
| 00-63 | STREAMING CONTROL REQUEST PACKET
============================================================================
|    00 | 0x12 streaming control command
----------------------------------------------------------------------------
|    01 | 0x00 streaming disabled
|       | 0x01 streaming enabled
----------------------------------------------------------------------------
|    02 | 0x00
----------------------------------------------------------------------------
| 03-59 | don't care
----------------------------------------------------------------------------
|    60 | packet sequence number
----------------------------------------------------------------------------
| 61-63 | don't care
----------------------------------------------------------------------------

============================================================================
| 00-63 | REMOTE CONTROL REQUEST PACKET
============================================================================
|    00 | 0x41 remote control command
----------------------------------------------------------------------------
| 01-59 | don't care
----------------------------------------------------------------------------
|    60 | packet sequence number
----------------------------------------------------------------------------
| 61-63 | don't care
----------------------------------------------------------------------------

============================================================================
| 00-63 | REMOTE CONTROL REPLY PACKET
============================================================================
|    00 | 0x00 code not received
|       | 0x01 code received
----------------------------------------------------------------------------
|    01 | remote control code
----------------------------------------------------------------------------
| 02-59 | don't care
----------------------------------------------------------------------------
|    60 | packet sequence number
----------------------------------------------------------------------------
| 61-63 | don't care
----------------------------------------------------------------------------

============================================================================
| 00-63 | GET HARDWARE INFO REQUEST PACKET
============================================================================
|    00 | 0x19 get hardware info command
----------------------------------------------------------------------------
| 01-59 | don't care
----------------------------------------------------------------------------
|    60 | packet sequence number
----------------------------------------------------------------------------
| 61-63 | don't care
----------------------------------------------------------------------------

============================================================================
| 00-63 | GET HARDWARE INFO REPLY PACKET
============================================================================
|    00 | hardware id
----------------------------------------------------------------------------
| 01-02 | firmware version
----------------------------------------------------------------------------
| 03-59 | don't care
----------------------------------------------------------------------------
|    60 | packet sequence number
----------------------------------------------------------------------------
| 61-63 | don't care
----------------------------------------------------------------------------

============================================================================
| 00-63 | SMART CARD READER PACKET
============================================================================
|    00 | 0x34 smart card reader command
----------------------------------------------------------------------------
|    xx |
----------------------------------------------------------------------------
| xx-59 | don't care
----------------------------------------------------------------------------
|    60 | packet sequence number
----------------------------------------------------------------------------
| 61-63 | don't care
----------------------------------------------------------------------------

*/
