hso.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997
  1. /******************************************************************************
  2. *
  3. * Driver for Option High Speed Mobile Devices.
  4. *
  5. * Copyright (C) 2008 Option International
  6. * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
  7. * <ajb@spheresystems.co.uk>
  8. * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
  9. * Copyright (C) 2008 Novell, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  23. * USA
  24. *
  25. *
  26. *****************************************************************************/
  27. /******************************************************************************
  28. *
  29. * Description of the device:
  30. *
  31. * Interface 0: Contains the IP network interface on the bulk end points.
  32. * The multiplexed serial ports are using the interrupt and
  33. * control endpoints.
  34. * Interrupt contains a bitmap telling which multiplexed
  35. * serialport needs servicing.
  36. *
  37. * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
  38. * port is opened, as this have a huge impact on the network port
  39. * throughput.
  40. *
  41. * Interface 2: Standard modem interface - circuit switched interface, should
  42. * not be used.
  43. *
  44. *****************************************************************************/
  45. #include <linux/sched.h>
  46. #include <linux/slab.h>
  47. #include <linux/init.h>
  48. #include <linux/delay.h>
  49. #include <linux/netdevice.h>
  50. #include <linux/module.h>
  51. #include <linux/ethtool.h>
  52. #include <linux/usb.h>
  53. #include <linux/timer.h>
  54. #include <linux/tty.h>
  55. #include <linux/tty_driver.h>
  56. #include <linux/tty_flip.h>
  57. #include <linux/kmod.h>
  58. #include <linux/rfkill.h>
  59. #include <linux/ip.h>
  60. #include <linux/uaccess.h>
  61. #include <linux/usb/cdc.h>
  62. #include <net/arp.h>
  63. #include <asm/byteorder.h>
  64. #define DRIVER_VERSION "1.2"
  65. #define MOD_AUTHOR "Option Wireless"
  66. #define MOD_DESCRIPTION "USB High Speed Option driver"
  67. #define MOD_LICENSE "GPL"
  68. #define HSO_MAX_NET_DEVICES 10
  69. #define HSO__MAX_MTU 2048
  70. #define DEFAULT_MTU 1500
  71. #define DEFAULT_MRU 1500
  72. #define CTRL_URB_RX_SIZE 1024
  73. #define CTRL_URB_TX_SIZE 64
  74. #define BULK_URB_RX_SIZE 4096
  75. #define BULK_URB_TX_SIZE 8192
  76. #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
  77. #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
  78. #define MUX_BULK_RX_BUF_COUNT 4
  79. #define USB_TYPE_OPTION_VENDOR 0x20
  80. /* These definitions are used with the struct hso_net flags element */
  81. /* - use *_bit operations on it. (bit indices not values.) */
  82. #define HSO_NET_RUNNING 0
  83. #define HSO_NET_TX_TIMEOUT (HZ*10)
  84. #define HSO_SERIAL_MAGIC 0x48534f31
  85. /* Number of ttys to handle */
  86. #define HSO_SERIAL_TTY_MINORS 256
  87. #define MAX_RX_URBS 2
  88. static inline struct hso_serial *get_serial_by_tty(struct tty_struct *tty)
  89. {
  90. if (tty)
  91. return tty->driver_data;
  92. return NULL;
  93. }
  94. /*****************************************************************************/
  95. /* Debugging functions */
  96. /*****************************************************************************/
  97. #define D__(lvl_, fmt, arg...) \
  98. do { \
  99. printk(lvl_ "[%d:%s]: " fmt "\n", \
  100. __LINE__, __func__, ## arg); \
  101. } while (0)
  102. #define D_(lvl, args...) \
  103. do { \
  104. if (lvl & debug) \
  105. D__(KERN_INFO, args); \
  106. } while (0)
  107. #define D1(args...) D_(0x01, ##args)
  108. #define D2(args...) D_(0x02, ##args)
  109. #define D3(args...) D_(0x04, ##args)
  110. #define D4(args...) D_(0x08, ##args)
  111. #define D5(args...) D_(0x10, ##args)
  112. /*****************************************************************************/
  113. /* Enumerators */
  114. /*****************************************************************************/
  115. enum pkt_parse_state {
  116. WAIT_IP,
  117. WAIT_DATA,
  118. WAIT_SYNC
  119. };
  120. /*****************************************************************************/
  121. /* Structs */
  122. /*****************************************************************************/
  123. struct hso_shared_int {
  124. struct usb_endpoint_descriptor *intr_endp;
  125. void *shared_intr_buf;
  126. struct urb *shared_intr_urb;
  127. struct usb_device *usb;
  128. int use_count;
  129. int ref_count;
  130. struct mutex shared_int_lock;
  131. };
  132. struct hso_net {
  133. struct hso_device *parent;
  134. struct net_device *net;
  135. struct rfkill *rfkill;
  136. struct usb_endpoint_descriptor *in_endp;
  137. struct usb_endpoint_descriptor *out_endp;
  138. struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
  139. struct urb *mux_bulk_tx_urb;
  140. void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
  141. void *mux_bulk_tx_buf;
  142. struct sk_buff *skb_rx_buf;
  143. struct sk_buff *skb_tx_buf;
  144. enum pkt_parse_state rx_parse_state;
  145. spinlock_t net_lock;
  146. unsigned short rx_buf_size;
  147. unsigned short rx_buf_missing;
  148. struct iphdr rx_ip_hdr;
  149. unsigned long flags;
  150. };
  151. enum rx_ctrl_state{
  152. RX_IDLE,
  153. RX_SENT,
  154. RX_PENDING
  155. };
  156. struct hso_serial {
  157. struct hso_device *parent;
  158. int magic;
  159. u8 minor;
  160. struct hso_shared_int *shared_int;
  161. /* rx/tx urb could be either a bulk urb or a control urb depending
  162. on which serial port it is used on. */
  163. struct urb *rx_urb[MAX_RX_URBS];
  164. u8 num_rx_urbs;
  165. u8 *rx_data[MAX_RX_URBS];
  166. u16 rx_data_length; /* should contain allocated length */
  167. struct urb *tx_urb;
  168. u8 *tx_data;
  169. u8 *tx_buffer;
  170. u16 tx_data_length; /* should contain allocated length */
  171. u16 tx_data_count;
  172. u16 tx_buffer_count;
  173. struct usb_ctrlrequest ctrl_req_tx;
  174. struct usb_ctrlrequest ctrl_req_rx;
  175. struct usb_endpoint_descriptor *in_endp;
  176. struct usb_endpoint_descriptor *out_endp;
  177. enum rx_ctrl_state rx_state;
  178. u8 rts_state;
  179. u8 dtr_state;
  180. unsigned tx_urb_used:1;
  181. /* from usb_serial_port */
  182. struct tty_struct *tty;
  183. int open_count;
  184. spinlock_t serial_lock;
  185. int (*write_data) (struct hso_serial *serial);
  186. /* Hacks required to get flow control
  187. * working on the serial receive buffers
  188. * so as not to drop characters on the floor.
  189. */
  190. int curr_rx_urb_idx;
  191. u16 curr_rx_urb_offset;
  192. u8 rx_urb_filled[MAX_RX_URBS];
  193. struct tasklet_struct unthrottle_tasklet;
  194. struct work_struct retry_unthrottle_workqueue;
  195. };
  196. struct hso_device {
  197. union {
  198. struct hso_serial *dev_serial;
  199. struct hso_net *dev_net;
  200. } port_data;
  201. u32 port_spec;
  202. u8 is_active;
  203. u8 usb_gone;
  204. struct work_struct async_get_intf;
  205. struct work_struct async_put_intf;
  206. struct usb_device *usb;
  207. struct usb_interface *interface;
  208. struct device *dev;
  209. struct kref ref;
  210. struct mutex mutex;
  211. };
  212. /* Type of interface */
  213. #define HSO_INTF_MASK 0xFF00
  214. #define HSO_INTF_MUX 0x0100
  215. #define HSO_INTF_BULK 0x0200
  216. /* Type of port */
  217. #define HSO_PORT_MASK 0xFF
  218. #define HSO_PORT_NO_PORT 0x0
  219. #define HSO_PORT_CONTROL 0x1
  220. #define HSO_PORT_APP 0x2
  221. #define HSO_PORT_GPS 0x3
  222. #define HSO_PORT_PCSC 0x4
  223. #define HSO_PORT_APP2 0x5
  224. #define HSO_PORT_GPS_CONTROL 0x6
  225. #define HSO_PORT_MSD 0x7
  226. #define HSO_PORT_VOICE 0x8
  227. #define HSO_PORT_DIAG2 0x9
  228. #define HSO_PORT_DIAG 0x10
  229. #define HSO_PORT_MODEM 0x11
  230. #define HSO_PORT_NETWORK 0x12
  231. /* Additional device info */
  232. #define HSO_INFO_MASK 0xFF000000
  233. #define HSO_INFO_CRC_BUG 0x01000000
  234. /*****************************************************************************/
  235. /* Prototypes */
  236. /*****************************************************************************/
  237. /* Serial driver functions */
  238. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  239. unsigned int set, unsigned int clear);
  240. static void ctrl_callback(struct urb *urb);
  241. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
  242. static void hso_kick_transmit(struct hso_serial *serial);
  243. /* Helper functions */
  244. static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
  245. struct usb_device *usb, gfp_t gfp);
  246. static void log_usb_status(int status, const char *function);
  247. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  248. int type, int dir);
  249. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
  250. static void hso_free_interface(struct usb_interface *intf);
  251. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
  252. static int hso_stop_serial_device(struct hso_device *hso_dev);
  253. static int hso_start_net_device(struct hso_device *hso_dev);
  254. static void hso_free_shared_int(struct hso_shared_int *shared_int);
  255. static int hso_stop_net_device(struct hso_device *hso_dev);
  256. static void hso_serial_ref_free(struct kref *ref);
  257. static void hso_std_serial_read_bulk_callback(struct urb *urb);
  258. static int hso_mux_serial_read(struct hso_serial *serial);
  259. static void async_get_intf(struct work_struct *data);
  260. static void async_put_intf(struct work_struct *data);
  261. static int hso_put_activity(struct hso_device *hso_dev);
  262. static int hso_get_activity(struct hso_device *hso_dev);
  263. /*****************************************************************************/
  264. /* Helping functions */
  265. /*****************************************************************************/
  266. /* #define DEBUG */
  267. static inline struct hso_net *dev2net(struct hso_device *hso_dev)
  268. {
  269. return hso_dev->port_data.dev_net;
  270. }
  271. static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
  272. {
  273. return hso_dev->port_data.dev_serial;
  274. }
  275. /* Debugging functions */
  276. #ifdef DEBUG
  277. static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
  278. unsigned int len)
  279. {
  280. static char name[255];
  281. sprintf(name, "hso[%d:%s]", line_count, func_name);
  282. print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
  283. }
  284. #define DUMP(buf_, len_) \
  285. dbg_dump(__LINE__, __func__, buf_, len_)
  286. #define DUMP1(buf_, len_) \
  287. do { \
  288. if (0x01 & debug) \
  289. DUMP(buf_, len_); \
  290. } while (0)
  291. #else
  292. #define DUMP(buf_, len_)
  293. #define DUMP1(buf_, len_)
  294. #endif
  295. /* module parameters */
  296. static int debug;
  297. static int tty_major;
  298. static int disable_net;
  299. /* driver info */
  300. static const char driver_name[] = "hso";
  301. static const char tty_filename[] = "ttyHS";
  302. static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
  303. /* the usb driver itself (registered in hso_init) */
  304. static struct usb_driver hso_driver;
  305. /* serial structures */
  306. static struct tty_driver *tty_drv;
  307. static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
  308. static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
  309. static spinlock_t serial_table_lock;
  310. static struct ktermios *hso_serial_termios[HSO_SERIAL_TTY_MINORS];
  311. static struct ktermios *hso_serial_termios_locked[HSO_SERIAL_TTY_MINORS];
  312. static const s32 default_port_spec[] = {
  313. HSO_INTF_MUX | HSO_PORT_NETWORK,
  314. HSO_INTF_BULK | HSO_PORT_DIAG,
  315. HSO_INTF_BULK | HSO_PORT_MODEM,
  316. 0
  317. };
  318. static const s32 icon321_port_spec[] = {
  319. HSO_INTF_MUX | HSO_PORT_NETWORK,
  320. HSO_INTF_BULK | HSO_PORT_DIAG2,
  321. HSO_INTF_BULK | HSO_PORT_MODEM,
  322. HSO_INTF_BULK | HSO_PORT_DIAG,
  323. 0
  324. };
  325. #define default_port_device(vendor, product) \
  326. USB_DEVICE(vendor, product), \
  327. .driver_info = (kernel_ulong_t)default_port_spec
  328. #define icon321_port_device(vendor, product) \
  329. USB_DEVICE(vendor, product), \
  330. .driver_info = (kernel_ulong_t)icon321_port_spec
  331. /* list of devices we support */
  332. static const struct usb_device_id hso_ids[] = {
  333. {default_port_device(0x0af0, 0x6711)},
  334. {default_port_device(0x0af0, 0x6731)},
  335. {default_port_device(0x0af0, 0x6751)},
  336. {default_port_device(0x0af0, 0x6771)},
  337. {default_port_device(0x0af0, 0x6791)},
  338. {default_port_device(0x0af0, 0x6811)},
  339. {default_port_device(0x0af0, 0x6911)},
  340. {default_port_device(0x0af0, 0x6951)},
  341. {default_port_device(0x0af0, 0x6971)},
  342. {default_port_device(0x0af0, 0x7011)},
  343. {default_port_device(0x0af0, 0x7031)},
  344. {default_port_device(0x0af0, 0x7051)},
  345. {default_port_device(0x0af0, 0x7071)},
  346. {default_port_device(0x0af0, 0x7111)},
  347. {default_port_device(0x0af0, 0x7211)},
  348. {default_port_device(0x0af0, 0x7251)},
  349. {default_port_device(0x0af0, 0x7271)},
  350. {default_port_device(0x0af0, 0x7311)},
  351. {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
  352. {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
  353. {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
  354. {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
  355. {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
  356. {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
  357. {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
  358. {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
  359. {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
  360. {USB_DEVICE(0x0af0, 0x7701)},
  361. {USB_DEVICE(0x0af0, 0x7801)},
  362. {USB_DEVICE(0x0af0, 0x7901)},
  363. {USB_DEVICE(0x0af0, 0x7361)},
  364. {icon321_port_device(0x0af0, 0xd051)},
  365. {}
  366. };
  367. MODULE_DEVICE_TABLE(usb, hso_ids);
  368. /* Sysfs attribute */
  369. static ssize_t hso_sysfs_show_porttype(struct device *dev,
  370. struct device_attribute *attr,
  371. char *buf)
  372. {
  373. struct hso_device *hso_dev = dev->driver_data;
  374. char *port_name;
  375. if (!hso_dev)
  376. return 0;
  377. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  378. case HSO_PORT_CONTROL:
  379. port_name = "Control";
  380. break;
  381. case HSO_PORT_APP:
  382. port_name = "Application";
  383. break;
  384. case HSO_PORT_APP2:
  385. port_name = "Application2";
  386. break;
  387. case HSO_PORT_GPS:
  388. port_name = "GPS";
  389. break;
  390. case HSO_PORT_GPS_CONTROL:
  391. port_name = "GPS Control";
  392. break;
  393. case HSO_PORT_PCSC:
  394. port_name = "PCSC";
  395. break;
  396. case HSO_PORT_DIAG:
  397. port_name = "Diagnostic";
  398. break;
  399. case HSO_PORT_DIAG2:
  400. port_name = "Diagnostic2";
  401. break;
  402. case HSO_PORT_MODEM:
  403. port_name = "Modem";
  404. break;
  405. case HSO_PORT_NETWORK:
  406. port_name = "Network";
  407. break;
  408. default:
  409. port_name = "Unknown";
  410. break;
  411. }
  412. return sprintf(buf, "%s\n", port_name);
  413. }
  414. static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
  415. static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
  416. {
  417. int idx;
  418. for (idx = 0; idx < serial->num_rx_urbs; idx++)
  419. if (serial->rx_urb[idx] == urb)
  420. return idx;
  421. dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
  422. return -1;
  423. }
  424. /* converts mux value to a port spec value */
  425. static u32 hso_mux_to_port(int mux)
  426. {
  427. u32 result;
  428. switch (mux) {
  429. case 0x1:
  430. result = HSO_PORT_CONTROL;
  431. break;
  432. case 0x2:
  433. result = HSO_PORT_APP;
  434. break;
  435. case 0x4:
  436. result = HSO_PORT_PCSC;
  437. break;
  438. case 0x8:
  439. result = HSO_PORT_GPS;
  440. break;
  441. case 0x10:
  442. result = HSO_PORT_APP2;
  443. break;
  444. default:
  445. result = HSO_PORT_NO_PORT;
  446. }
  447. return result;
  448. }
  449. /* converts port spec value to a mux value */
  450. static u32 hso_port_to_mux(int port)
  451. {
  452. u32 result;
  453. switch (port & HSO_PORT_MASK) {
  454. case HSO_PORT_CONTROL:
  455. result = 0x0;
  456. break;
  457. case HSO_PORT_APP:
  458. result = 0x1;
  459. break;
  460. case HSO_PORT_PCSC:
  461. result = 0x2;
  462. break;
  463. case HSO_PORT_GPS:
  464. result = 0x3;
  465. break;
  466. case HSO_PORT_APP2:
  467. result = 0x4;
  468. break;
  469. default:
  470. result = 0x0;
  471. }
  472. return result;
  473. }
  474. static struct hso_serial *get_serial_by_shared_int_and_type(
  475. struct hso_shared_int *shared_int,
  476. int mux)
  477. {
  478. int i, port;
  479. port = hso_mux_to_port(mux);
  480. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  481. if (serial_table[i]
  482. && (dev2ser(serial_table[i])->shared_int == shared_int)
  483. && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
  484. return dev2ser(serial_table[i]);
  485. }
  486. }
  487. return NULL;
  488. }
  489. static struct hso_serial *get_serial_by_index(unsigned index)
  490. {
  491. struct hso_serial *serial = NULL;
  492. unsigned long flags;
  493. spin_lock_irqsave(&serial_table_lock, flags);
  494. if (serial_table[index])
  495. serial = dev2ser(serial_table[index]);
  496. spin_unlock_irqrestore(&serial_table_lock, flags);
  497. return serial;
  498. }
  499. static int get_free_serial_index(void)
  500. {
  501. int index;
  502. unsigned long flags;
  503. spin_lock_irqsave(&serial_table_lock, flags);
  504. for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
  505. if (serial_table[index] == NULL) {
  506. spin_unlock_irqrestore(&serial_table_lock, flags);
  507. return index;
  508. }
  509. }
  510. spin_unlock_irqrestore(&serial_table_lock, flags);
  511. printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
  512. return -1;
  513. }
  514. static void set_serial_by_index(unsigned index, struct hso_serial *serial)
  515. {
  516. unsigned long flags;
  517. spin_lock_irqsave(&serial_table_lock, flags);
  518. if (serial)
  519. serial_table[index] = serial->parent;
  520. else
  521. serial_table[index] = NULL;
  522. spin_unlock_irqrestore(&serial_table_lock, flags);
  523. }
  524. /* log a meaningful explanation of an USB status */
  525. static void log_usb_status(int status, const char *function)
  526. {
  527. char *explanation;
  528. switch (status) {
  529. case -ENODEV:
  530. explanation = "no device";
  531. break;
  532. case -ENOENT:
  533. explanation = "endpoint not enabled";
  534. break;
  535. case -EPIPE:
  536. explanation = "endpoint stalled";
  537. break;
  538. case -ENOSPC:
  539. explanation = "not enough bandwidth";
  540. break;
  541. case -ESHUTDOWN:
  542. explanation = "device disabled";
  543. break;
  544. case -EHOSTUNREACH:
  545. explanation = "device suspended";
  546. break;
  547. case -EINVAL:
  548. case -EAGAIN:
  549. case -EFBIG:
  550. case -EMSGSIZE:
  551. explanation = "internal error";
  552. break;
  553. default:
  554. explanation = "unknown status";
  555. break;
  556. }
  557. D1("%s: received USB status - %s (%d)", function, explanation, status);
  558. }
  559. /* Network interface functions */
  560. /* called when net interface is brought up by ifconfig */
  561. static int hso_net_open(struct net_device *net)
  562. {
  563. struct hso_net *odev = netdev_priv(net);
  564. unsigned long flags = 0;
  565. if (!odev) {
  566. dev_err(&net->dev, "No net device !\n");
  567. return -ENODEV;
  568. }
  569. odev->skb_tx_buf = NULL;
  570. /* setup environment */
  571. spin_lock_irqsave(&odev->net_lock, flags);
  572. odev->rx_parse_state = WAIT_IP;
  573. odev->rx_buf_size = 0;
  574. odev->rx_buf_missing = sizeof(struct iphdr);
  575. spin_unlock_irqrestore(&odev->net_lock, flags);
  576. /* We are up and running. */
  577. set_bit(HSO_NET_RUNNING, &odev->flags);
  578. hso_start_net_device(odev->parent);
  579. /* Tell the kernel we are ready to start receiving from it */
  580. netif_start_queue(net);
  581. return 0;
  582. }
  583. /* called when interface is brought down by ifconfig */
  584. static int hso_net_close(struct net_device *net)
  585. {
  586. struct hso_net *odev = netdev_priv(net);
  587. /* we don't need the queue anymore */
  588. netif_stop_queue(net);
  589. /* no longer running */
  590. clear_bit(HSO_NET_RUNNING, &odev->flags);
  591. hso_stop_net_device(odev->parent);
  592. /* done */
  593. return 0;
  594. }
  595. /* USB tells is xmit done, we should start the netqueue again */
  596. static void write_bulk_callback(struct urb *urb)
  597. {
  598. struct hso_net *odev = urb->context;
  599. int status = urb->status;
  600. /* Sanity check */
  601. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  602. dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
  603. return;
  604. }
  605. /* Do we still have a valid kernel network device? */
  606. if (!netif_device_present(odev->net)) {
  607. dev_err(&urb->dev->dev, "%s: net device not present\n",
  608. __func__);
  609. return;
  610. }
  611. /* log status, but don't act on it, we don't need to resubmit anything
  612. * anyhow */
  613. if (status)
  614. log_usb_status(status, __func__);
  615. hso_put_activity(odev->parent);
  616. /* Tell the network interface we are ready for another frame */
  617. netif_wake_queue(odev->net);
  618. }
  619. /* called by kernel when we need to transmit a packet */
  620. static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
  621. {
  622. struct hso_net *odev = netdev_priv(net);
  623. int result;
  624. /* Tell the kernel, "No more frames 'til we are done with this one." */
  625. netif_stop_queue(net);
  626. if (hso_get_activity(odev->parent) == -EAGAIN) {
  627. odev->skb_tx_buf = skb;
  628. return 0;
  629. }
  630. /* log if asked */
  631. DUMP1(skb->data, skb->len);
  632. /* Copy it from kernel memory to OUR memory */
  633. memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
  634. D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
  635. /* Fill in the URB for shipping it out. */
  636. usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
  637. odev->parent->usb,
  638. usb_sndbulkpipe(odev->parent->usb,
  639. odev->out_endp->
  640. bEndpointAddress & 0x7F),
  641. odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
  642. odev);
  643. /* Deal with the Zero Length packet problem, I hope */
  644. odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
  645. /* Send the URB on its merry way. */
  646. result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
  647. if (result) {
  648. dev_warn(&odev->parent->interface->dev,
  649. "failed mux_bulk_tx_urb %d", result);
  650. net->stats.tx_errors++;
  651. netif_start_queue(net);
  652. } else {
  653. net->stats.tx_packets++;
  654. net->stats.tx_bytes += skb->len;
  655. /* And tell the kernel when the last transmit started. */
  656. net->trans_start = jiffies;
  657. }
  658. dev_kfree_skb(skb);
  659. /* we're done */
  660. return result;
  661. }
  662. static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
  663. {
  664. struct hso_net *odev = netdev_priv(net);
  665. strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
  666. strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
  667. usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
  668. }
  669. static struct ethtool_ops ops = {
  670. .get_drvinfo = hso_get_drvinfo,
  671. .get_link = ethtool_op_get_link
  672. };
  673. /* called when a packet did not ack after watchdogtimeout */
  674. static void hso_net_tx_timeout(struct net_device *net)
  675. {
  676. struct hso_net *odev = netdev_priv(net);
  677. if (!odev)
  678. return;
  679. /* Tell syslog we are hosed. */
  680. dev_warn(&net->dev, "Tx timed out.\n");
  681. /* Tear the waiting frame off the list */
  682. if (odev->mux_bulk_tx_urb
  683. && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
  684. usb_unlink_urb(odev->mux_bulk_tx_urb);
  685. /* Update statistics */
  686. net->stats.tx_errors++;
  687. }
  688. /* make a real packet from the received USB buffer */
  689. static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
  690. unsigned int count, unsigned char is_eop)
  691. {
  692. unsigned short temp_bytes;
  693. unsigned short buffer_offset = 0;
  694. unsigned short frame_len;
  695. unsigned char *tmp_rx_buf;
  696. /* log if needed */
  697. D1("Rx %d bytes", count);
  698. DUMP(ip_pkt, min(128, (int)count));
  699. while (count) {
  700. switch (odev->rx_parse_state) {
  701. case WAIT_IP:
  702. /* waiting for IP header. */
  703. /* wanted bytes - size of ip header */
  704. temp_bytes =
  705. (count <
  706. odev->rx_buf_missing) ? count : odev->
  707. rx_buf_missing;
  708. memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
  709. odev->rx_buf_size, ip_pkt + buffer_offset,
  710. temp_bytes);
  711. odev->rx_buf_size += temp_bytes;
  712. buffer_offset += temp_bytes;
  713. odev->rx_buf_missing -= temp_bytes;
  714. count -= temp_bytes;
  715. if (!odev->rx_buf_missing) {
  716. /* header is complete allocate an sk_buffer and
  717. * continue to WAIT_DATA */
  718. frame_len = ntohs(odev->rx_ip_hdr.tot_len);
  719. if ((frame_len > DEFAULT_MRU) ||
  720. (frame_len < sizeof(struct iphdr))) {
  721. dev_err(&odev->net->dev,
  722. "Invalid frame (%d) length\n",
  723. frame_len);
  724. odev->rx_parse_state = WAIT_SYNC;
  725. continue;
  726. }
  727. /* Allocate an sk_buff */
  728. odev->skb_rx_buf = dev_alloc_skb(frame_len);
  729. if (!odev->skb_rx_buf) {
  730. /* We got no receive buffer. */
  731. D1("could not allocate memory");
  732. odev->rx_parse_state = WAIT_SYNC;
  733. return;
  734. }
  735. /* Here's where it came from */
  736. odev->skb_rx_buf->dev = odev->net;
  737. /* Copy what we got so far. make room for iphdr
  738. * after tail. */
  739. tmp_rx_buf =
  740. skb_put(odev->skb_rx_buf,
  741. sizeof(struct iphdr));
  742. memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
  743. sizeof(struct iphdr));
  744. /* ETH_HLEN */
  745. odev->rx_buf_size = sizeof(struct iphdr);
  746. /* Filip actually use .tot_len */
  747. odev->rx_buf_missing =
  748. frame_len - sizeof(struct iphdr);
  749. odev->rx_parse_state = WAIT_DATA;
  750. }
  751. break;
  752. case WAIT_DATA:
  753. temp_bytes = (count < odev->rx_buf_missing)
  754. ? count : odev->rx_buf_missing;
  755. /* Copy the rest of the bytes that are left in the
  756. * buffer into the waiting sk_buf. */
  757. /* Make room for temp_bytes after tail. */
  758. tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
  759. memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
  760. odev->rx_buf_missing -= temp_bytes;
  761. count -= temp_bytes;
  762. buffer_offset += temp_bytes;
  763. odev->rx_buf_size += temp_bytes;
  764. if (!odev->rx_buf_missing) {
  765. /* Packet is complete. Inject into stack. */
  766. /* We have IP packet here */
  767. odev->skb_rx_buf->protocol =
  768. __constant_htons(ETH_P_IP);
  769. /* don't check it */
  770. odev->skb_rx_buf->ip_summed =
  771. CHECKSUM_UNNECESSARY;
  772. skb_reset_mac_header(odev->skb_rx_buf);
  773. /* Ship it off to the kernel */
  774. netif_rx(odev->skb_rx_buf);
  775. /* No longer our buffer. */
  776. odev->skb_rx_buf = NULL;
  777. /* update out statistics */
  778. odev->net->stats.rx_packets++;
  779. odev->net->stats.rx_bytes += odev->rx_buf_size;
  780. odev->rx_buf_size = 0;
  781. odev->rx_buf_missing = sizeof(struct iphdr);
  782. odev->rx_parse_state = WAIT_IP;
  783. }
  784. break;
  785. case WAIT_SYNC:
  786. D1(" W_S");
  787. count = 0;
  788. break;
  789. default:
  790. D1(" ");
  791. count--;
  792. break;
  793. }
  794. }
  795. /* Recovery mechanism for WAIT_SYNC state. */
  796. if (is_eop) {
  797. if (odev->rx_parse_state == WAIT_SYNC) {
  798. odev->rx_parse_state = WAIT_IP;
  799. odev->rx_buf_size = 0;
  800. odev->rx_buf_missing = sizeof(struct iphdr);
  801. }
  802. }
  803. }
  804. /* Moving data from usb to kernel (in interrupt state) */
  805. static void read_bulk_callback(struct urb *urb)
  806. {
  807. struct hso_net *odev = urb->context;
  808. struct net_device *net;
  809. int result;
  810. int status = urb->status;
  811. /* is al ok? (Filip: Who's Al ?) */
  812. if (status) {
  813. log_usb_status(status, __func__);
  814. return;
  815. }
  816. /* Sanity check */
  817. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  818. D1("BULK IN callback but driver is not active!");
  819. return;
  820. }
  821. usb_mark_last_busy(urb->dev);
  822. net = odev->net;
  823. if (!netif_device_present(net)) {
  824. /* Somebody killed our network interface... */
  825. return;
  826. }
  827. if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
  828. u32 rest;
  829. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  830. rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
  831. if (((rest == 5) || (rest == 6))
  832. && !memcmp(((u8 *) urb->transfer_buffer) +
  833. urb->actual_length - 4, crc_check, 4)) {
  834. urb->actual_length -= 4;
  835. }
  836. }
  837. /* do we even have a packet? */
  838. if (urb->actual_length) {
  839. /* Handle the IP stream, add header and push it onto network
  840. * stack if the packet is complete. */
  841. spin_lock(&odev->net_lock);
  842. packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
  843. (urb->transfer_buffer_length >
  844. urb->actual_length) ? 1 : 0);
  845. spin_unlock(&odev->net_lock);
  846. }
  847. /* We are done with this URB, resubmit it. Prep the USB to wait for
  848. * another frame. Reuse same as received. */
  849. usb_fill_bulk_urb(urb,
  850. odev->parent->usb,
  851. usb_rcvbulkpipe(odev->parent->usb,
  852. odev->in_endp->
  853. bEndpointAddress & 0x7F),
  854. urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
  855. read_bulk_callback, odev);
  856. /* Give this to the USB subsystem so it can tell us when more data
  857. * arrives. */
  858. result = usb_submit_urb(urb, GFP_ATOMIC);
  859. if (result)
  860. dev_warn(&odev->parent->interface->dev,
  861. "%s failed submit mux_bulk_rx_urb %d", __func__,
  862. result);
  863. }
  864. /* Serial driver functions */
  865. static void _hso_serial_set_termios(struct tty_struct *tty,
  866. struct ktermios *old)
  867. {
  868. struct hso_serial *serial = get_serial_by_tty(tty);
  869. struct ktermios *termios;
  870. if ((!tty) || (!tty->termios) || (!serial)) {
  871. printk(KERN_ERR "%s: no tty structures", __func__);
  872. return;
  873. }
  874. D4("port %d", serial->minor);
  875. /*
  876. * The default requirements for this device are:
  877. */
  878. termios = tty->termios;
  879. termios->c_iflag &=
  880. ~(IGNBRK /* disable ignore break */
  881. | BRKINT /* disable break causes interrupt */
  882. | PARMRK /* disable mark parity errors */
  883. | ISTRIP /* disable clear high bit of input characters */
  884. | INLCR /* disable translate NL to CR */
  885. | IGNCR /* disable ignore CR */
  886. | ICRNL /* disable translate CR to NL */
  887. | IXON); /* disable enable XON/XOFF flow control */
  888. /* disable postprocess output characters */
  889. termios->c_oflag &= ~OPOST;
  890. termios->c_lflag &=
  891. ~(ECHO /* disable echo input characters */
  892. | ECHONL /* disable echo new line */
  893. | ICANON /* disable erase, kill, werase, and rprnt
  894. special characters */
  895. | ISIG /* disable interrupt, quit, and suspend special
  896. characters */
  897. | IEXTEN); /* disable non-POSIX special characters */
  898. termios->c_cflag &=
  899. ~(CSIZE /* no size */
  900. | PARENB /* disable parity bit */
  901. | CBAUD /* clear current baud rate */
  902. | CBAUDEX); /* clear current buad rate */
  903. termios->c_cflag |= CS8; /* character size 8 bits */
  904. /* baud rate 115200 */
  905. tty_encode_baud_rate(serial->tty, 115200, 115200);
  906. /*
  907. * Force low_latency on; otherwise the pushes are scheduled;
  908. * this is bad as it opens up the possibility of dropping bytes
  909. * on the floor. We don't want to drop bytes on the floor. :)
  910. */
  911. serial->tty->low_latency = 1;
  912. return;
  913. }
  914. static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
  915. {
  916. int result;
  917. #ifdef CONFIG_HSO_AUTOPM
  918. usb_mark_last_busy(urb->dev);
  919. #endif
  920. /* We are done with this URB, resubmit it. Prep the USB to wait for
  921. * another frame */
  922. usb_fill_bulk_urb(urb, serial->parent->usb,
  923. usb_rcvbulkpipe(serial->parent->usb,
  924. serial->in_endp->
  925. bEndpointAddress & 0x7F),
  926. urb->transfer_buffer, serial->rx_data_length,
  927. hso_std_serial_read_bulk_callback, serial);
  928. /* Give this to the USB subsystem so it can tell us when more data
  929. * arrives. */
  930. result = usb_submit_urb(urb, GFP_ATOMIC);
  931. if (result) {
  932. dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
  933. __func__, result);
  934. }
  935. }
  936. static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
  937. {
  938. int count;
  939. struct urb *curr_urb;
  940. while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
  941. curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
  942. count = put_rxbuf_data(curr_urb, serial);
  943. if (count == -1)
  944. return;
  945. if (count == 0) {
  946. serial->curr_rx_urb_idx++;
  947. if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
  948. serial->curr_rx_urb_idx = 0;
  949. hso_resubmit_rx_bulk_urb(serial, curr_urb);
  950. }
  951. }
  952. }
  953. static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
  954. {
  955. int count = 0;
  956. struct urb *urb;
  957. urb = serial->rx_urb[0];
  958. if (serial->open_count > 0) {
  959. count = put_rxbuf_data(urb, serial);
  960. if (count == -1)
  961. return;
  962. }
  963. /* Re issue a read as long as we receive data. */
  964. if (count == 0 && ((urb->actual_length != 0) ||
  965. (serial->rx_state == RX_PENDING))) {
  966. serial->rx_state = RX_SENT;
  967. hso_mux_serial_read(serial);
  968. } else
  969. serial->rx_state = RX_IDLE;
  970. }
  971. /* read callback for Diag and CS port */
  972. static void hso_std_serial_read_bulk_callback(struct urb *urb)
  973. {
  974. struct hso_serial *serial = urb->context;
  975. int status = urb->status;
  976. /* sanity check */
  977. if (!serial) {
  978. D1("serial == NULL");
  979. return;
  980. } else if (status) {
  981. log_usb_status(status, __func__);
  982. return;
  983. }
  984. D4("\n--- Got serial_read_bulk callback %02x ---", status);
  985. D1("Actual length = %d\n", urb->actual_length);
  986. DUMP1(urb->transfer_buffer, urb->actual_length);
  987. /* Anyone listening? */
  988. if (serial->open_count == 0)
  989. return;
  990. if (status == 0) {
  991. if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
  992. u32 rest;
  993. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  994. rest =
  995. urb->actual_length %
  996. serial->in_endp->wMaxPacketSize;
  997. if (((rest == 5) || (rest == 6))
  998. && !memcmp(((u8 *) urb->transfer_buffer) +
  999. urb->actual_length - 4, crc_check, 4)) {
  1000. urb->actual_length -= 4;
  1001. }
  1002. }
  1003. /* Valid data, handle RX data */
  1004. spin_lock(&serial->serial_lock);
  1005. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
  1006. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1007. spin_unlock(&serial->serial_lock);
  1008. } else if (status == -ENOENT || status == -ECONNRESET) {
  1009. /* Unlinked - check for throttled port. */
  1010. D2("Port %d, successfully unlinked urb", serial->minor);
  1011. spin_lock(&serial->serial_lock);
  1012. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1013. hso_resubmit_rx_bulk_urb(serial, urb);
  1014. spin_unlock(&serial->serial_lock);
  1015. } else {
  1016. D2("Port %d, status = %d for read urb", serial->minor, status);
  1017. return;
  1018. }
  1019. }
  1020. /*
  1021. * This needs to be a tasklet otherwise we will
  1022. * end up recursively calling this function.
  1023. */
  1024. void hso_unthrottle_tasklet(struct hso_serial *serial)
  1025. {
  1026. unsigned long flags;
  1027. spin_lock_irqsave(&serial->serial_lock, flags);
  1028. if ((serial->parent->port_spec & HSO_INTF_MUX))
  1029. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1030. else
  1031. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1032. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1033. }
  1034. static void hso_unthrottle(struct tty_struct *tty)
  1035. {
  1036. struct hso_serial *serial = get_serial_by_tty(tty);
  1037. tasklet_hi_schedule(&serial->unthrottle_tasklet);
  1038. }
  1039. void hso_unthrottle_workfunc(struct work_struct *work)
  1040. {
  1041. struct hso_serial *serial =
  1042. container_of(work, struct hso_serial,
  1043. retry_unthrottle_workqueue);
  1044. hso_unthrottle_tasklet(serial);
  1045. }
  1046. /* open the requested serial port */
  1047. static int hso_serial_open(struct tty_struct *tty, struct file *filp)
  1048. {
  1049. struct hso_serial *serial = get_serial_by_index(tty->index);
  1050. int result;
  1051. /* sanity check */
  1052. if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
  1053. tty->driver_data = NULL;
  1054. D1("Failed to open port");
  1055. return -ENODEV;
  1056. }
  1057. mutex_lock(&serial->parent->mutex);
  1058. result = usb_autopm_get_interface(serial->parent->interface);
  1059. if (result < 0)
  1060. goto err_out;
  1061. D1("Opening %d", serial->minor);
  1062. kref_get(&serial->parent->ref);
  1063. /* setup */
  1064. tty->driver_data = serial;
  1065. serial->tty = tty;
  1066. /* check for port already opened, if not set the termios */
  1067. serial->open_count++;
  1068. if (serial->open_count == 1) {
  1069. tty->low_latency = 1;
  1070. serial->rx_state = RX_IDLE;
  1071. /* Force default termio settings */
  1072. _hso_serial_set_termios(tty, NULL);
  1073. tasklet_init(&serial->unthrottle_tasklet,
  1074. (void (*)(unsigned long))hso_unthrottle_tasklet,
  1075. (unsigned long)serial);
  1076. INIT_WORK(&serial->retry_unthrottle_workqueue,
  1077. hso_unthrottle_workfunc);
  1078. result = hso_start_serial_device(serial->parent, GFP_KERNEL);
  1079. if (result) {
  1080. hso_stop_serial_device(serial->parent);
  1081. serial->open_count--;
  1082. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1083. }
  1084. } else {
  1085. D1("Port was already open");
  1086. }
  1087. usb_autopm_put_interface(serial->parent->interface);
  1088. /* done */
  1089. if (result)
  1090. hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
  1091. err_out:
  1092. mutex_unlock(&serial->parent->mutex);
  1093. return result;
  1094. }
  1095. /* close the requested serial port */
  1096. static void hso_serial_close(struct tty_struct *tty, struct file *filp)
  1097. {
  1098. struct hso_serial *serial = tty->driver_data;
  1099. u8 usb_gone;
  1100. D1("Closing serial port");
  1101. mutex_lock(&serial->parent->mutex);
  1102. usb_gone = serial->parent->usb_gone;
  1103. if (!usb_gone)
  1104. usb_autopm_get_interface(serial->parent->interface);
  1105. /* reset the rts and dtr */
  1106. /* do the actual close */
  1107. serial->open_count--;
  1108. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1109. if (serial->open_count <= 0) {
  1110. serial->open_count = 0;
  1111. if (serial->tty) {
  1112. serial->tty->driver_data = NULL;
  1113. serial->tty = NULL;
  1114. }
  1115. if (!usb_gone)
  1116. hso_stop_serial_device(serial->parent);
  1117. tasklet_kill(&serial->unthrottle_tasklet);
  1118. cancel_work_sync(&serial->retry_unthrottle_workqueue);
  1119. }
  1120. if (!usb_gone)
  1121. usb_autopm_put_interface(serial->parent->interface);
  1122. mutex_unlock(&serial->parent->mutex);
  1123. }
  1124. /* close the requested serial port */
  1125. static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
  1126. int count)
  1127. {
  1128. struct hso_serial *serial = get_serial_by_tty(tty);
  1129. int space, tx_bytes;
  1130. unsigned long flags;
  1131. /* sanity check */
  1132. if (serial == NULL) {
  1133. printk(KERN_ERR "%s: serial is NULL\n", __func__);
  1134. return -ENODEV;
  1135. }
  1136. spin_lock_irqsave(&serial->serial_lock, flags);
  1137. space = serial->tx_data_length - serial->tx_buffer_count;
  1138. tx_bytes = (count < space) ? count : space;
  1139. if (!tx_bytes)
  1140. goto out;
  1141. memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
  1142. serial->tx_buffer_count += tx_bytes;
  1143. out:
  1144. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1145. hso_kick_transmit(serial);
  1146. /* done */
  1147. return tx_bytes;
  1148. }
  1149. /* how much room is there for writing */
  1150. static int hso_serial_write_room(struct tty_struct *tty)
  1151. {
  1152. struct hso_serial *serial = get_serial_by_tty(tty);
  1153. int room;
  1154. unsigned long flags;
  1155. spin_lock_irqsave(&serial->serial_lock, flags);
  1156. room = serial->tx_data_length - serial->tx_buffer_count;
  1157. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1158. /* return free room */
  1159. return room;
  1160. }
  1161. /* setup the term */
  1162. static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
  1163. {
  1164. struct hso_serial *serial = get_serial_by_tty(tty);
  1165. unsigned long flags;
  1166. if (old)
  1167. D5("Termios called with: cflags new[%d] - old[%d]",
  1168. tty->termios->c_cflag, old->c_cflag);
  1169. /* the actual setup */
  1170. spin_lock_irqsave(&serial->serial_lock, flags);
  1171. if (serial->open_count)
  1172. _hso_serial_set_termios(tty, old);
  1173. else
  1174. tty->termios = old;
  1175. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1176. /* done */
  1177. return;
  1178. }
  1179. /* how many characters in the buffer */
  1180. static int hso_serial_chars_in_buffer(struct tty_struct *tty)
  1181. {
  1182. struct hso_serial *serial = get_serial_by_tty(tty);
  1183. int chars;
  1184. unsigned long flags;
  1185. /* sanity check */
  1186. if (serial == NULL)
  1187. return 0;
  1188. spin_lock_irqsave(&serial->serial_lock, flags);
  1189. chars = serial->tx_buffer_count;
  1190. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1191. return chars;
  1192. }
  1193. static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
  1194. {
  1195. unsigned int value;
  1196. struct hso_serial *serial = get_serial_by_tty(tty);
  1197. unsigned long flags;
  1198. /* sanity check */
  1199. if (!serial) {
  1200. D1("no tty structures");
  1201. return -EINVAL;
  1202. }
  1203. spin_lock_irqsave(&serial->serial_lock, flags);
  1204. value = ((serial->rts_state) ? TIOCM_RTS : 0) |
  1205. ((serial->dtr_state) ? TIOCM_DTR : 0);
  1206. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1207. return value;
  1208. }
  1209. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  1210. unsigned int set, unsigned int clear)
  1211. {
  1212. int val = 0;
  1213. unsigned long flags;
  1214. int if_num;
  1215. struct hso_serial *serial = get_serial_by_tty(tty);
  1216. /* sanity check */
  1217. if (!serial) {
  1218. D1("no tty structures");
  1219. return -EINVAL;
  1220. }
  1221. if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
  1222. spin_lock_irqsave(&serial->serial_lock, flags);
  1223. if (set & TIOCM_RTS)
  1224. serial->rts_state = 1;
  1225. if (set & TIOCM_DTR)
  1226. serial->dtr_state = 1;
  1227. if (clear & TIOCM_RTS)
  1228. serial->rts_state = 0;
  1229. if (clear & TIOCM_DTR)
  1230. serial->dtr_state = 0;
  1231. if (serial->dtr_state)
  1232. val |= 0x01;
  1233. if (serial->rts_state)
  1234. val |= 0x02;
  1235. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1236. return usb_control_msg(serial->parent->usb,
  1237. usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
  1238. 0x21, val, if_num, NULL, 0,
  1239. USB_CTRL_SET_TIMEOUT);
  1240. }
  1241. /* starts a transmit */
  1242. static void hso_kick_transmit(struct hso_serial *serial)
  1243. {
  1244. u8 *temp;
  1245. unsigned long flags;
  1246. int res;
  1247. spin_lock_irqsave(&serial->serial_lock, flags);
  1248. if (!serial->tx_buffer_count)
  1249. goto out;
  1250. if (serial->tx_urb_used)
  1251. goto out;
  1252. /* Wakeup USB interface if necessary */
  1253. if (hso_get_activity(serial->parent) == -EAGAIN)
  1254. goto out;
  1255. /* Switch pointers around to avoid memcpy */
  1256. temp = serial->tx_buffer;
  1257. serial->tx_buffer = serial->tx_data;
  1258. serial->tx_data = temp;
  1259. serial->tx_data_count = serial->tx_buffer_count;
  1260. serial->tx_buffer_count = 0;
  1261. /* If temp is set, it means we switched buffers */
  1262. if (temp && serial->write_data) {
  1263. res = serial->write_data(serial);
  1264. if (res >= 0)
  1265. serial->tx_urb_used = 1;
  1266. }
  1267. out:
  1268. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1269. }
  1270. /* make a request (for reading and writing data to muxed serial port) */
  1271. static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
  1272. struct urb *ctrl_urb,
  1273. struct usb_ctrlrequest *ctrl_req,
  1274. u8 *ctrl_urb_data, u32 size)
  1275. {
  1276. int result;
  1277. int pipe;
  1278. /* Sanity check */
  1279. if (!serial || !ctrl_urb || !ctrl_req) {
  1280. printk(KERN_ERR "%s: Wrong arguments\n", __func__);
  1281. return -EINVAL;
  1282. }
  1283. /* initialize */
  1284. ctrl_req->wValue = 0;
  1285. ctrl_req->wIndex = hso_port_to_mux(port);
  1286. ctrl_req->wLength = size;
  1287. if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
  1288. /* Reading command */
  1289. ctrl_req->bRequestType = USB_DIR_IN |
  1290. USB_TYPE_OPTION_VENDOR |
  1291. USB_RECIP_INTERFACE;
  1292. ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  1293. pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
  1294. } else {
  1295. /* Writing command */
  1296. ctrl_req->bRequestType = USB_DIR_OUT |
  1297. USB_TYPE_OPTION_VENDOR |
  1298. USB_RECIP_INTERFACE;
  1299. ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  1300. pipe = usb_sndctrlpipe(serial->parent->usb, 0);
  1301. }
  1302. /* syslog */
  1303. D2("%s command (%02x) len: %d, port: %d",
  1304. type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
  1305. ctrl_req->bRequestType, ctrl_req->wLength, port);
  1306. /* Load ctrl urb */
  1307. ctrl_urb->transfer_flags = 0;
  1308. usb_fill_control_urb(ctrl_urb,
  1309. serial->parent->usb,
  1310. pipe,
  1311. (u8 *) ctrl_req,
  1312. ctrl_urb_data, size, ctrl_callback, serial);
  1313. /* Send it on merry way */
  1314. result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
  1315. if (result) {
  1316. dev_err(&ctrl_urb->dev->dev,
  1317. "%s failed submit ctrl_urb %d type %d", __func__,
  1318. result, type);
  1319. return result;
  1320. }
  1321. /* done */
  1322. return size;
  1323. }
  1324. /* called by intr_callback when read occurs */
  1325. static int hso_mux_serial_read(struct hso_serial *serial)
  1326. {
  1327. if (!serial)
  1328. return -EINVAL;
  1329. /* clean data */
  1330. memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
  1331. /* make the request */
  1332. if (serial->num_rx_urbs != 1) {
  1333. dev_err(&serial->parent->interface->dev,
  1334. "ERROR: mux'd reads with multiple buffers "
  1335. "not possible\n");
  1336. return 0;
  1337. }
  1338. return mux_device_request(serial,
  1339. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  1340. serial->parent->port_spec & HSO_PORT_MASK,
  1341. serial->rx_urb[0],
  1342. &serial->ctrl_req_rx,
  1343. serial->rx_data[0], serial->rx_data_length);
  1344. }
  1345. /* used for muxed serial port callback (muxed serial read) */
  1346. static void intr_callback(struct urb *urb)
  1347. {
  1348. struct hso_shared_int *shared_int = urb->context;
  1349. struct hso_serial *serial;
  1350. unsigned char *port_req;
  1351. int status = urb->status;
  1352. int i;
  1353. usb_mark_last_busy(urb->dev);
  1354. /* sanity check */
  1355. if (!shared_int)
  1356. return;
  1357. /* status check */
  1358. if (status) {
  1359. log_usb_status(status, __func__);
  1360. return;
  1361. }
  1362. D4("\n--- Got intr callback 0x%02X ---", status);
  1363. /* what request? */
  1364. port_req = urb->transfer_buffer;
  1365. D4(" port_req = 0x%.2X\n", *port_req);
  1366. /* loop over all muxed ports to find the one sending this */
  1367. for (i = 0; i < 8; i++) {
  1368. /* max 8 channels on MUX */
  1369. if (*port_req & (1 << i)) {
  1370. serial = get_serial_by_shared_int_and_type(shared_int,
  1371. (1 << i));
  1372. if (serial != NULL) {
  1373. D1("Pending read interrupt on port %d\n", i);
  1374. spin_lock(&serial->serial_lock);
  1375. if (serial->rx_state == RX_IDLE) {
  1376. /* Setup and send a ctrl req read on
  1377. * port i */
  1378. if (!serial->rx_urb_filled[0]) {
  1379. serial->rx_state = RX_SENT;
  1380. hso_mux_serial_read(serial);
  1381. } else
  1382. serial->rx_state = RX_PENDING;
  1383. } else {
  1384. D1("Already pending a read on "
  1385. "port %d\n", i);
  1386. }
  1387. spin_unlock(&serial->serial_lock);
  1388. }
  1389. }
  1390. }
  1391. /* Resubmit interrupt urb */
  1392. hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
  1393. }
  1394. /* called for writing to muxed serial port */
  1395. static int hso_mux_serial_write_data(struct hso_serial *serial)
  1396. {
  1397. if (NULL == serial)
  1398. return -EINVAL;
  1399. return mux_device_request(serial,
  1400. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  1401. serial->parent->port_spec & HSO_PORT_MASK,
  1402. serial->tx_urb,
  1403. &serial->ctrl_req_tx,
  1404. serial->tx_data, serial->tx_data_count);
  1405. }
  1406. /* write callback for Diag and CS port */
  1407. static void hso_std_serial_write_bulk_callback(struct urb *urb)
  1408. {
  1409. struct hso_serial *serial = urb->context;
  1410. int status = urb->status;
  1411. /* sanity check */
  1412. if (!serial) {
  1413. D1("serial == NULL");
  1414. return;
  1415. }
  1416. spin_lock(&serial->serial_lock);
  1417. serial->tx_urb_used = 0;
  1418. spin_unlock(&serial->serial_lock);
  1419. if (status) {
  1420. log_usb_status(status, __func__);
  1421. return;
  1422. }
  1423. hso_put_activity(serial->parent);
  1424. if (serial->tty)
  1425. tty_wakeup(serial->tty);
  1426. hso_kick_transmit(serial);
  1427. D1(" ");
  1428. return;
  1429. }
  1430. /* called for writing diag or CS serial port */
  1431. static int hso_std_serial_write_data(struct hso_serial *serial)
  1432. {
  1433. int count = serial->tx_data_count;
  1434. int result;
  1435. usb_fill_bulk_urb(serial->tx_urb,
  1436. serial->parent->usb,
  1437. usb_sndbulkpipe(serial->parent->usb,
  1438. serial->out_endp->
  1439. bEndpointAddress & 0x7F),
  1440. serial->tx_data, serial->tx_data_count,
  1441. hso_std_serial_write_bulk_callback, serial);
  1442. result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
  1443. if (result) {
  1444. dev_warn(&serial->parent->usb->dev,
  1445. "Failed to submit urb - res %d\n", result);
  1446. return result;
  1447. }
  1448. return count;
  1449. }
  1450. /* callback after read or write on muxed serial port */
  1451. static void ctrl_callback(struct urb *urb)
  1452. {
  1453. struct hso_serial *serial = urb->context;
  1454. struct usb_ctrlrequest *req;
  1455. int status = urb->status;
  1456. /* sanity check */
  1457. if (!serial)
  1458. return;
  1459. spin_lock(&serial->serial_lock);
  1460. serial->tx_urb_used = 0;
  1461. spin_unlock(&serial->serial_lock);
  1462. if (status) {
  1463. log_usb_status(status, __func__);
  1464. return;
  1465. }
  1466. /* what request? */
  1467. req = (struct usb_ctrlrequest *)(urb->setup_packet);
  1468. D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
  1469. D4("Actual length of urb = %d\n", urb->actual_length);
  1470. DUMP1(urb->transfer_buffer, urb->actual_length);
  1471. if (req->bRequestType ==
  1472. (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
  1473. /* response to a read command */
  1474. serial->rx_urb_filled[0] = 1;
  1475. spin_lock(&serial->serial_lock);
  1476. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1477. spin_unlock(&serial->serial_lock);
  1478. } else {
  1479. hso_put_activity(serial->parent);
  1480. if (serial->tty)
  1481. tty_wakeup(serial->tty);
  1482. /* response to a write command */
  1483. hso_kick_transmit(serial);
  1484. }
  1485. }
  1486. /* handle RX data for serial port */
  1487. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
  1488. {
  1489. struct tty_struct *tty = serial->tty;
  1490. int write_length_remaining = 0;
  1491. int curr_write_len;
  1492. /* Sanity check */
  1493. if (urb == NULL || serial == NULL) {
  1494. D1("serial = NULL");
  1495. return -2;
  1496. }
  1497. /* Push data to tty */
  1498. if (tty) {
  1499. write_length_remaining = urb->actual_length -
  1500. serial->curr_rx_urb_offset;
  1501. D1("data to push to tty");
  1502. while (write_length_remaining) {
  1503. if (test_bit(TTY_THROTTLED, &tty->flags))
  1504. return -1;
  1505. curr_write_len = tty_insert_flip_string
  1506. (tty, urb->transfer_buffer +
  1507. serial->curr_rx_urb_offset,
  1508. write_length_remaining);
  1509. serial->curr_rx_urb_offset += curr_write_len;
  1510. write_length_remaining -= curr_write_len;
  1511. tty_flip_buffer_push(tty);
  1512. }
  1513. }
  1514. if (write_length_remaining == 0) {
  1515. serial->curr_rx_urb_offset = 0;
  1516. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1517. }
  1518. return write_length_remaining;
  1519. }
  1520. /* Base driver functions */
  1521. static void hso_log_port(struct hso_device *hso_dev)
  1522. {
  1523. char *port_type;
  1524. char port_dev[20];
  1525. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  1526. case HSO_PORT_CONTROL:
  1527. port_type = "Control";
  1528. break;
  1529. case HSO_PORT_APP:
  1530. port_type = "Application";
  1531. break;
  1532. case HSO_PORT_GPS:
  1533. port_type = "GPS";
  1534. break;
  1535. case HSO_PORT_GPS_CONTROL:
  1536. port_type = "GPS control";
  1537. break;
  1538. case HSO_PORT_APP2:
  1539. port_type = "Application2";
  1540. break;
  1541. case HSO_PORT_PCSC:
  1542. port_type = "PCSC";
  1543. break;
  1544. case HSO_PORT_DIAG:
  1545. port_type = "Diagnostic";
  1546. break;
  1547. case HSO_PORT_DIAG2:
  1548. port_type = "Diagnostic2";
  1549. break;
  1550. case HSO_PORT_MODEM:
  1551. port_type = "Modem";
  1552. break;
  1553. case HSO_PORT_NETWORK:
  1554. port_type = "Network";
  1555. break;
  1556. default:
  1557. port_type = "Unknown";
  1558. break;
  1559. }
  1560. if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  1561. sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
  1562. } else
  1563. sprintf(port_dev, "/dev/%s%d", tty_filename,
  1564. dev2ser(hso_dev)->minor);
  1565. dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
  1566. port_type, port_dev);
  1567. }
  1568. static int hso_start_net_device(struct hso_device *hso_dev)
  1569. {
  1570. int i, result = 0;
  1571. struct hso_net *hso_net = dev2net(hso_dev);
  1572. if (!hso_net)
  1573. return -ENODEV;
  1574. /* send URBs for all read buffers */
  1575. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1576. /* Prep a receive URB */
  1577. usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1578. hso_dev->usb,
  1579. usb_rcvbulkpipe(hso_dev->usb,
  1580. hso_net->in_endp->
  1581. bEndpointAddress & 0x7F),
  1582. hso_net->mux_bulk_rx_buf_pool[i],
  1583. MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
  1584. hso_net);
  1585. /* Put it out there so the device can send us stuff */
  1586. result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1587. GFP_NOIO);
  1588. if (result)
  1589. dev_warn(&hso_dev->usb->dev,
  1590. "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
  1591. i, result);
  1592. }
  1593. return result;
  1594. }
  1595. static int hso_stop_net_device(struct hso_device *hso_dev)
  1596. {
  1597. int i;
  1598. struct hso_net *hso_net = dev2net(hso_dev);
  1599. if (!hso_net)
  1600. return -ENODEV;
  1601. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1602. if (hso_net->mux_bulk_rx_urb_pool[i])
  1603. usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1604. }
  1605. if (hso_net->mux_bulk_tx_urb)
  1606. usb_kill_urb(hso_net->mux_bulk_tx_urb);
  1607. return 0;
  1608. }
  1609. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
  1610. {
  1611. int i, result = 0;
  1612. struct hso_serial *serial = dev2ser(hso_dev);
  1613. if (!serial)
  1614. return -ENODEV;
  1615. /* If it is not the MUX port fill in and submit a bulk urb (already
  1616. * allocated in hso_serial_start) */
  1617. if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
  1618. for (i = 0; i < serial->num_rx_urbs; i++) {
  1619. usb_fill_bulk_urb(serial->rx_urb[i],
  1620. serial->parent->usb,
  1621. usb_rcvbulkpipe(serial->parent->usb,
  1622. serial->in_endp->
  1623. bEndpointAddress &
  1624. 0x7F),
  1625. serial->rx_data[i],
  1626. serial->rx_data_length,
  1627. hso_std_serial_read_bulk_callback,
  1628. serial);
  1629. result = usb_submit_urb(serial->rx_urb[i], flags);
  1630. if (result) {
  1631. dev_warn(&serial->parent->usb->dev,
  1632. "Failed to submit urb - res %d\n",
  1633. result);
  1634. break;
  1635. }
  1636. }
  1637. } else {
  1638. mutex_lock(&serial->shared_int->shared_int_lock);
  1639. if (!serial->shared_int->use_count) {
  1640. result =
  1641. hso_mux_submit_intr_urb(serial->shared_int,
  1642. hso_dev->usb, flags);
  1643. }
  1644. serial->shared_int->use_count++;
  1645. mutex_unlock(&serial->shared_int->shared_int_lock);
  1646. }
  1647. return result;
  1648. }
  1649. static int hso_stop_serial_device(struct hso_device *hso_dev)
  1650. {
  1651. int i;
  1652. struct hso_serial *serial = dev2ser(hso_dev);
  1653. if (!serial)
  1654. return -ENODEV;
  1655. for (i = 0; i < serial->num_rx_urbs; i++) {
  1656. if (serial->rx_urb[i]) {
  1657. usb_kill_urb(serial->rx_urb[i]);
  1658. serial->rx_urb_filled[i] = 0;
  1659. }
  1660. }
  1661. serial->curr_rx_urb_idx = 0;
  1662. serial->curr_rx_urb_offset = 0;
  1663. if (serial->tx_urb)
  1664. usb_kill_urb(serial->tx_urb);
  1665. if (serial->shared_int) {
  1666. mutex_lock(&serial->shared_int->shared_int_lock);
  1667. if (serial->shared_int->use_count &&
  1668. (--serial->shared_int->use_count == 0)) {
  1669. struct urb *urb;
  1670. urb = serial->shared_int->shared_intr_urb;
  1671. if (urb)
  1672. usb_kill_urb(urb);
  1673. }
  1674. mutex_unlock(&serial->shared_int->shared_int_lock);
  1675. }
  1676. return 0;
  1677. }
  1678. static void hso_serial_common_free(struct hso_serial *serial)
  1679. {
  1680. int i;
  1681. if (serial->parent->dev)
  1682. device_remove_file(serial->parent->dev, &dev_attr_hsotype);
  1683. tty_unregister_device(tty_drv, serial->minor);
  1684. for (i = 0; i < serial->num_rx_urbs; i++) {
  1685. /* unlink and free RX URB */
  1686. usb_free_urb(serial->rx_urb[i]);
  1687. /* free the RX buffer */
  1688. kfree(serial->rx_data[i]);
  1689. }
  1690. /* unlink and free TX URB */
  1691. usb_free_urb(serial->tx_urb);
  1692. kfree(serial->tx_data);
  1693. }
  1694. static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
  1695. int rx_size, int tx_size)
  1696. {
  1697. struct device *dev;
  1698. int minor;
  1699. int i;
  1700. minor = get_free_serial_index();
  1701. if (minor < 0)
  1702. goto exit;
  1703. /* register our minor number */
  1704. serial->parent->dev = tty_register_device(tty_drv, minor,
  1705. &serial->parent->interface->dev);
  1706. dev = serial->parent->dev;
  1707. dev->driver_data = serial->parent;
  1708. i = device_create_file(dev, &dev_attr_hsotype);
  1709. /* fill in specific data for later use */
  1710. serial->minor = minor;
  1711. serial->magic = HSO_SERIAL_MAGIC;
  1712. spin_lock_init(&serial->serial_lock);
  1713. serial->num_rx_urbs = num_urbs;
  1714. /* RX, allocate urb and initialize */
  1715. /* prepare our RX buffer */
  1716. serial->rx_data_length = rx_size;
  1717. for (i = 0; i < serial->num_rx_urbs; i++) {
  1718. serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  1719. if (!serial->rx_urb[i]) {
  1720. dev_err(dev, "Could not allocate urb?\n");
  1721. goto exit;
  1722. }
  1723. serial->rx_urb[i]->transfer_buffer = NULL;
  1724. serial->rx_urb[i]->transfer_buffer_length = 0;
  1725. serial->rx_data[i] = kzalloc(serial->rx_data_length,
  1726. GFP_KERNEL);
  1727. if (!serial->rx_data[i]) {
  1728. dev_err(dev, "%s - Out of memory\n", __func__);
  1729. goto exit;
  1730. }
  1731. }
  1732. /* TX, allocate urb and initialize */
  1733. serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1734. if (!serial->tx_urb) {
  1735. dev_err(dev, "Could not allocate urb?\n");
  1736. goto exit;
  1737. }
  1738. serial->tx_urb->transfer_buffer = NULL;
  1739. serial->tx_urb->transfer_buffer_length = 0;
  1740. /* prepare our TX buffer */
  1741. serial->tx_data_count = 0;
  1742. serial->tx_buffer_count = 0;
  1743. serial->tx_data_length = tx_size;
  1744. serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
  1745. if (!serial->tx_data) {
  1746. dev_err(dev, "%s - Out of memory", __func__);
  1747. goto exit;
  1748. }
  1749. serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
  1750. if (!serial->tx_buffer) {
  1751. dev_err(dev, "%s - Out of memory", __func__);
  1752. goto exit;
  1753. }
  1754. return 0;
  1755. exit:
  1756. hso_serial_common_free(serial);
  1757. return -1;
  1758. }
  1759. /* Frees a general hso device */
  1760. static void hso_free_device(struct hso_device *hso_dev)
  1761. {
  1762. kfree(hso_dev);
  1763. }
  1764. /* Creates a general hso device */
  1765. static struct hso_device *hso_create_device(struct usb_interface *intf,
  1766. int port_spec)
  1767. {
  1768. struct hso_device *hso_dev;
  1769. hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
  1770. if (!hso_dev)
  1771. return NULL;
  1772. hso_dev->port_spec = port_spec;
  1773. hso_dev->usb = interface_to_usbdev(intf);
  1774. hso_dev->interface = intf;
  1775. kref_init(&hso_dev->ref);
  1776. mutex_init(&hso_dev->mutex);
  1777. INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
  1778. INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
  1779. return hso_dev;
  1780. }
  1781. /* Removes a network device in the network device table */
  1782. static int remove_net_device(struct hso_device *hso_dev)
  1783. {
  1784. int i;
  1785. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  1786. if (network_table[i] == hso_dev) {
  1787. network_table[i] = NULL;
  1788. break;
  1789. }
  1790. }
  1791. if (i == HSO_MAX_NET_DEVICES)
  1792. return -1;
  1793. return 0;
  1794. }
  1795. /* Frees our network device */
  1796. static void hso_free_net_device(struct hso_device *hso_dev)
  1797. {
  1798. int i;
  1799. struct hso_net *hso_net = dev2net(hso_dev);
  1800. if (!hso_net)
  1801. return;
  1802. /* start freeing */
  1803. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1804. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1805. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  1806. }
  1807. usb_free_urb(hso_net->mux_bulk_tx_urb);
  1808. kfree(hso_net->mux_bulk_tx_buf);
  1809. remove_net_device(hso_net->parent);
  1810. if (hso_net->net) {
  1811. unregister_netdev(hso_net->net);
  1812. free_netdev(hso_net->net);
  1813. }
  1814. hso_free_device(hso_dev);
  1815. }
  1816. /* initialize the network interface */
  1817. static void hso_net_init(struct net_device *net)
  1818. {
  1819. struct hso_net *hso_net = netdev_priv(net);
  1820. D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
  1821. /* fill in the other fields */
  1822. net->open = hso_net_open;
  1823. net->stop = hso_net_close;
  1824. net->hard_start_xmit = hso_net_start_xmit;
  1825. net->tx_timeout = hso_net_tx_timeout;
  1826. net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
  1827. net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  1828. net->type = ARPHRD_NONE;
  1829. net->mtu = DEFAULT_MTU - 14;
  1830. net->tx_queue_len = 10;
  1831. SET_ETHTOOL_OPS(net, &ops);
  1832. /* and initialize the semaphore */
  1833. spin_lock_init(&hso_net->net_lock);
  1834. }
  1835. /* Adds a network device in the network device table */
  1836. static int add_net_device(struct hso_device *hso_dev)
  1837. {
  1838. int i;
  1839. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  1840. if (network_table[i] == NULL) {
  1841. network_table[i] = hso_dev;
  1842. break;
  1843. }
  1844. }
  1845. if (i == HSO_MAX_NET_DEVICES)
  1846. return -1;
  1847. return 0;
  1848. }
  1849. static int hso_radio_toggle(void *data, enum rfkill_state state)
  1850. {
  1851. struct hso_device *hso_dev = data;
  1852. int enabled = (state == RFKILL_STATE_ON);
  1853. int rv;
  1854. mutex_lock(&hso_dev->mutex);
  1855. if (hso_dev->usb_gone)
  1856. rv = 0;
  1857. else
  1858. rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
  1859. enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
  1860. USB_CTRL_SET_TIMEOUT);
  1861. mutex_unlock(&hso_dev->mutex);
  1862. return rv;
  1863. }
  1864. /* Creates and sets up everything for rfkill */
  1865. static void hso_create_rfkill(struct hso_device *hso_dev,
  1866. struct usb_interface *interface)
  1867. {
  1868. struct hso_net *hso_net = dev2net(hso_dev);
  1869. struct device *dev = &hso_net->net->dev;
  1870. char *rfkn;
  1871. hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
  1872. RFKILL_TYPE_WWAN);
  1873. if (!hso_net->rfkill) {
  1874. dev_err(dev, "%s - Out of memory\n", __func__);
  1875. return;
  1876. }
  1877. rfkn = kzalloc(20, GFP_KERNEL);
  1878. if (!rfkn) {
  1879. rfkill_free(hso_net->rfkill);
  1880. hso_net->rfkill = NULL;
  1881. dev_err(dev, "%s - Out of memory\n", __func__);
  1882. return;
  1883. }
  1884. snprintf(rfkn, 20, "hso-%d",
  1885. interface->altsetting->desc.bInterfaceNumber);
  1886. hso_net->rfkill->name = rfkn;
  1887. hso_net->rfkill->state = RFKILL_STATE_ON;
  1888. hso_net->rfkill->data = hso_dev;
  1889. hso_net->rfkill->toggle_radio = hso_radio_toggle;
  1890. if (rfkill_register(hso_net->rfkill) < 0) {
  1891. kfree(rfkn);
  1892. hso_net->rfkill->name = NULL;
  1893. rfkill_free(hso_net->rfkill);
  1894. hso_net->rfkill = NULL;
  1895. dev_err(dev, "%s - Failed to register rfkill\n", __func__);
  1896. return;
  1897. }
  1898. }
  1899. /* Creates our network device */
  1900. static struct hso_device *hso_create_net_device(struct usb_interface *interface)
  1901. {
  1902. int result, i;
  1903. struct net_device *net;
  1904. struct hso_net *hso_net;
  1905. struct hso_device *hso_dev;
  1906. hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
  1907. if (!hso_dev)
  1908. return NULL;
  1909. /* allocate our network device, then we can put in our private data */
  1910. /* call hso_net_init to do the basic initialization */
  1911. net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
  1912. if (!net) {
  1913. dev_err(&interface->dev, "Unable to create ethernet device\n");
  1914. goto exit;
  1915. }
  1916. hso_net = netdev_priv(net);
  1917. hso_dev->port_data.dev_net = hso_net;
  1918. hso_net->net = net;
  1919. hso_net->parent = hso_dev;
  1920. hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  1921. USB_DIR_IN);
  1922. if (!hso_net->in_endp) {
  1923. dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
  1924. goto exit;
  1925. }
  1926. hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  1927. USB_DIR_OUT);
  1928. if (!hso_net->out_endp) {
  1929. dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
  1930. goto exit;
  1931. }
  1932. SET_NETDEV_DEV(net, &interface->dev);
  1933. /* registering our net device */
  1934. result = register_netdev(net);
  1935. if (result) {
  1936. dev_err(&interface->dev, "Failed to register device\n");
  1937. goto exit;
  1938. }
  1939. /* start allocating */
  1940. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1941. hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
  1942. if (!hso_net->mux_bulk_rx_urb_pool[i]) {
  1943. dev_err(&interface->dev, "Could not allocate rx urb\n");
  1944. goto exit;
  1945. }
  1946. hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
  1947. GFP_KERNEL);
  1948. if (!hso_net->mux_bulk_rx_buf_pool[i]) {
  1949. dev_err(&interface->dev, "Could not allocate rx buf\n");
  1950. goto exit;
  1951. }
  1952. }
  1953. hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1954. if (!hso_net->mux_bulk_tx_urb) {
  1955. dev_err(&interface->dev, "Could not allocate tx urb\n");
  1956. goto exit;
  1957. }
  1958. hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
  1959. if (!hso_net->mux_bulk_tx_buf) {
  1960. dev_err(&interface->dev, "Could not allocate tx buf\n");
  1961. goto exit;
  1962. }
  1963. add_net_device(hso_dev);
  1964. hso_log_port(hso_dev);
  1965. hso_create_rfkill(hso_dev, interface);
  1966. return hso_dev;
  1967. exit:
  1968. hso_free_net_device(hso_dev);
  1969. return NULL;
  1970. }
  1971. /* Frees an AT channel ( goes for both mux and non-mux ) */
  1972. static void hso_free_serial_device(struct hso_device *hso_dev)
  1973. {
  1974. struct hso_serial *serial = dev2ser(hso_dev);
  1975. if (!serial)
  1976. return;
  1977. set_serial_by_index(serial->minor, NULL);
  1978. hso_serial_common_free(serial);
  1979. if (serial->shared_int) {
  1980. mutex_lock(&serial->shared_int->shared_int_lock);
  1981. if (--serial->shared_int->ref_count == 0)
  1982. hso_free_shared_int(serial->shared_int);
  1983. else
  1984. mutex_unlock(&serial->shared_int->shared_int_lock);
  1985. }
  1986. kfree(serial);
  1987. hso_free_device(hso_dev);
  1988. }
  1989. /* Creates a bulk AT channel */
  1990. static struct hso_device *hso_create_bulk_serial_device(
  1991. struct usb_interface *interface, int port)
  1992. {
  1993. struct hso_device *hso_dev;
  1994. struct hso_serial *serial;
  1995. int num_urbs;
  1996. hso_dev = hso_create_device(interface, port);
  1997. if (!hso_dev)
  1998. return NULL;
  1999. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2000. if (!serial)
  2001. goto exit;
  2002. serial->parent = hso_dev;
  2003. hso_dev->port_data.dev_serial = serial;
  2004. if (port & HSO_PORT_MODEM)
  2005. num_urbs = 2;
  2006. else
  2007. num_urbs = 1;
  2008. if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
  2009. BULK_URB_TX_SIZE))
  2010. goto exit;
  2011. serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2012. USB_DIR_IN);
  2013. if (!serial->in_endp) {
  2014. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2015. goto exit2;
  2016. }
  2017. if (!
  2018. (serial->out_endp =
  2019. hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
  2020. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2021. goto exit2;
  2022. }
  2023. serial->write_data = hso_std_serial_write_data;
  2024. /* and record this serial */
  2025. set_serial_by_index(serial->minor, serial);
  2026. /* setup the proc dirs and files if needed */
  2027. hso_log_port(hso_dev);
  2028. /* done, return it */
  2029. return hso_dev;
  2030. exit2:
  2031. hso_serial_common_free(serial);
  2032. exit:
  2033. kfree(serial);
  2034. hso_free_device(hso_dev);
  2035. return NULL;
  2036. }
  2037. /* Creates a multiplexed AT channel */
  2038. static
  2039. struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
  2040. int port,
  2041. struct hso_shared_int *mux)
  2042. {
  2043. struct hso_device *hso_dev;
  2044. struct hso_serial *serial;
  2045. int port_spec;
  2046. port_spec = HSO_INTF_MUX;
  2047. port_spec &= ~HSO_PORT_MASK;
  2048. port_spec |= hso_mux_to_port(port);
  2049. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
  2050. return NULL;
  2051. hso_dev = hso_create_device(interface, port_spec);
  2052. if (!hso_dev)
  2053. return NULL;
  2054. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2055. if (!serial)
  2056. goto exit;
  2057. hso_dev->port_data.dev_serial = serial;
  2058. serial->parent = hso_dev;
  2059. if (hso_serial_common_create
  2060. (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
  2061. goto exit;
  2062. serial->tx_data_length--;
  2063. serial->write_data = hso_mux_serial_write_data;
  2064. serial->shared_int = mux;
  2065. mutex_lock(&serial->shared_int->shared_int_lock);
  2066. serial->shared_int->ref_count++;
  2067. mutex_unlock(&serial->shared_int->shared_int_lock);
  2068. /* and record this serial */
  2069. set_serial_by_index(serial->minor, serial);
  2070. /* setup the proc dirs and files if needed */
  2071. hso_log_port(hso_dev);
  2072. /* done, return it */
  2073. return hso_dev;
  2074. exit:
  2075. if (serial) {
  2076. tty_unregister_device(tty_drv, serial->minor);
  2077. kfree(serial);
  2078. }
  2079. if (hso_dev)
  2080. hso_free_device(hso_dev);
  2081. return NULL;
  2082. }
  2083. static void hso_free_shared_int(struct hso_shared_int *mux)
  2084. {
  2085. usb_free_urb(mux->shared_intr_urb);
  2086. kfree(mux->shared_intr_buf);
  2087. mutex_unlock(&mux->shared_int_lock);
  2088. kfree(mux);
  2089. }
  2090. static
  2091. struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
  2092. {
  2093. struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  2094. if (!mux)
  2095. return NULL;
  2096. mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
  2097. USB_DIR_IN);
  2098. if (!mux->intr_endp) {
  2099. dev_err(&interface->dev, "Can't find INT IN endpoint\n");
  2100. goto exit;
  2101. }
  2102. mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  2103. if (!mux->shared_intr_urb) {
  2104. dev_err(&interface->dev, "Could not allocate intr urb?");
  2105. goto exit;
  2106. }
  2107. mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
  2108. GFP_KERNEL);
  2109. if (!mux->shared_intr_buf) {
  2110. dev_err(&interface->dev, "Could not allocate intr buf?");
  2111. goto exit;
  2112. }
  2113. mutex_init(&mux->shared_int_lock);
  2114. return mux;
  2115. exit:
  2116. kfree(mux->shared_intr_buf);
  2117. usb_free_urb(mux->shared_intr_urb);
  2118. kfree(mux);
  2119. return NULL;
  2120. }
  2121. /* Gets the port spec for a certain interface */
  2122. static int hso_get_config_data(struct usb_interface *interface)
  2123. {
  2124. struct usb_device *usbdev = interface_to_usbdev(interface);
  2125. u8 config_data[17];
  2126. u32 if_num = interface->altsetting->desc.bInterfaceNumber;
  2127. s32 result;
  2128. if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  2129. 0x86, 0xC0, 0, 0, config_data, 17,
  2130. USB_CTRL_SET_TIMEOUT) != 0x11) {
  2131. return -EIO;
  2132. }
  2133. switch (config_data[if_num]) {
  2134. case 0x0:
  2135. result = 0;
  2136. break;
  2137. case 0x1:
  2138. result = HSO_PORT_DIAG;
  2139. break;
  2140. case 0x2:
  2141. result = HSO_PORT_GPS;
  2142. break;
  2143. case 0x3:
  2144. result = HSO_PORT_GPS_CONTROL;
  2145. break;
  2146. case 0x4:
  2147. result = HSO_PORT_APP;
  2148. break;
  2149. case 0x5:
  2150. result = HSO_PORT_APP2;
  2151. break;
  2152. case 0x6:
  2153. result = HSO_PORT_CONTROL;
  2154. break;
  2155. case 0x7:
  2156. result = HSO_PORT_NETWORK;
  2157. break;
  2158. case 0x8:
  2159. result = HSO_PORT_MODEM;
  2160. break;
  2161. case 0x9:
  2162. result = HSO_PORT_MSD;
  2163. break;
  2164. case 0xa:
  2165. result = HSO_PORT_PCSC;
  2166. break;
  2167. case 0xb:
  2168. result = HSO_PORT_VOICE;
  2169. break;
  2170. default:
  2171. result = 0;
  2172. }
  2173. if (result)
  2174. result |= HSO_INTF_BULK;
  2175. if (config_data[16] & 0x1)
  2176. result |= HSO_INFO_CRC_BUG;
  2177. return result;
  2178. }
  2179. /* called once for each interface upon device insertion */
  2180. static int hso_probe(struct usb_interface *interface,
  2181. const struct usb_device_id *id)
  2182. {
  2183. int mux, i, if_num, port_spec;
  2184. unsigned char port_mask;
  2185. struct hso_device *hso_dev = NULL;
  2186. struct hso_shared_int *shared_int;
  2187. struct hso_device *tmp_dev = NULL;
  2188. if_num = interface->altsetting->desc.bInterfaceNumber;
  2189. /* Get the interface/port specification from either driver_info or from
  2190. * the device itself */
  2191. if (id->driver_info)
  2192. port_spec = ((u32 *)(id->driver_info))[if_num];
  2193. else
  2194. port_spec = hso_get_config_data(interface);
  2195. if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
  2196. dev_err(&interface->dev, "Not our interface\n");
  2197. return -ENODEV;
  2198. }
  2199. /* Check if we need to switch to alt interfaces prior to port
  2200. * configuration */
  2201. if (interface->num_altsetting > 1)
  2202. usb_set_interface(interface_to_usbdev(interface), if_num, 1);
  2203. interface->needs_remote_wakeup = 1;
  2204. /* Allocate new hso device(s) */
  2205. switch (port_spec & HSO_INTF_MASK) {
  2206. case HSO_INTF_MUX:
  2207. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2208. /* Create the network device */
  2209. if (!disable_net) {
  2210. hso_dev = hso_create_net_device(interface);
  2211. if (!hso_dev)
  2212. goto exit;
  2213. tmp_dev = hso_dev;
  2214. }
  2215. }
  2216. if (hso_get_mux_ports(interface, &port_mask))
  2217. /* TODO: de-allocate everything */
  2218. goto exit;
  2219. shared_int = hso_create_shared_int(interface);
  2220. if (!shared_int)
  2221. goto exit;
  2222. for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
  2223. if (port_mask & i) {
  2224. hso_dev = hso_create_mux_serial_device(
  2225. interface, i, shared_int);
  2226. if (!hso_dev)
  2227. goto exit;
  2228. }
  2229. }
  2230. if (tmp_dev)
  2231. hso_dev = tmp_dev;
  2232. break;
  2233. case HSO_INTF_BULK:
  2234. /* It's a regular bulk interface */
  2235. if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
  2236. && !disable_net)
  2237. hso_dev = hso_create_net_device(interface);
  2238. else
  2239. hso_dev =
  2240. hso_create_bulk_serial_device(interface, port_spec);
  2241. if (!hso_dev)
  2242. goto exit;
  2243. break;
  2244. default:
  2245. goto exit;
  2246. }
  2247. usb_driver_claim_interface(&hso_driver, interface, hso_dev);
  2248. /* save our data pointer in this device */
  2249. usb_set_intfdata(interface, hso_dev);
  2250. /* done */
  2251. return 0;
  2252. exit:
  2253. hso_free_interface(interface);
  2254. return -ENODEV;
  2255. }
  2256. /* device removed, cleaning up */
  2257. static void hso_disconnect(struct usb_interface *interface)
  2258. {
  2259. hso_free_interface(interface);
  2260. /* remove reference of our private data */
  2261. usb_set_intfdata(interface, NULL);
  2262. usb_driver_release_interface(&hso_driver, interface);
  2263. }
  2264. static void async_get_intf(struct work_struct *data)
  2265. {
  2266. struct hso_device *hso_dev =
  2267. container_of(data, struct hso_device, async_get_intf);
  2268. usb_autopm_get_interface(hso_dev->interface);
  2269. }
  2270. static void async_put_intf(struct work_struct *data)
  2271. {
  2272. struct hso_device *hso_dev =
  2273. container_of(data, struct hso_device, async_put_intf);
  2274. usb_autopm_put_interface(hso_dev->interface);
  2275. }
  2276. static int hso_get_activity(struct hso_device *hso_dev)
  2277. {
  2278. if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
  2279. if (!hso_dev->is_active) {
  2280. hso_dev->is_active = 1;
  2281. schedule_work(&hso_dev->async_get_intf);
  2282. }
  2283. }
  2284. if (hso_dev->usb->state != USB_STATE_CONFIGURED)
  2285. return -EAGAIN;
  2286. usb_mark_last_busy(hso_dev->usb);
  2287. return 0;
  2288. }
  2289. static int hso_put_activity(struct hso_device *hso_dev)
  2290. {
  2291. if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
  2292. if (hso_dev->is_active) {
  2293. hso_dev->is_active = 0;
  2294. schedule_work(&hso_dev->async_put_intf);
  2295. return -EAGAIN;
  2296. }
  2297. }
  2298. hso_dev->is_active = 0;
  2299. return 0;
  2300. }
  2301. /* called by kernel when we need to suspend device */
  2302. static int hso_suspend(struct usb_interface *iface, pm_message_t message)
  2303. {
  2304. int i, result;
  2305. /* Stop all serial ports */
  2306. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2307. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2308. result = hso_stop_serial_device(serial_table[i]);
  2309. if (result)
  2310. goto out;
  2311. }
  2312. }
  2313. /* Stop all network ports */
  2314. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2315. if (network_table[i] &&
  2316. (network_table[i]->interface == iface)) {
  2317. result = hso_stop_net_device(network_table[i]);
  2318. if (result)
  2319. goto out;
  2320. }
  2321. }
  2322. out:
  2323. return 0;
  2324. }
  2325. /* called by kernel when we need to resume device */
  2326. static int hso_resume(struct usb_interface *iface)
  2327. {
  2328. int i, result = 0;
  2329. struct hso_net *hso_net;
  2330. /* Start all serial ports */
  2331. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2332. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2333. if (dev2ser(serial_table[i])->open_count) {
  2334. result =
  2335. hso_start_serial_device(serial_table[i], GFP_NOIO);
  2336. hso_kick_transmit(dev2ser(serial_table[i]));
  2337. if (result)
  2338. goto out;
  2339. }
  2340. }
  2341. }
  2342. /* Start all network ports */
  2343. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2344. if (network_table[i] &&
  2345. (network_table[i]->interface == iface)) {
  2346. hso_net = dev2net(network_table[i]);
  2347. if (hso_net->flags & IFF_UP) {
  2348. /* First transmit any lingering data,
  2349. then restart the device. */
  2350. if (hso_net->skb_tx_buf) {
  2351. dev_dbg(&iface->dev,
  2352. "Transmitting"
  2353. " lingering data\n");
  2354. hso_net_start_xmit(hso_net->skb_tx_buf,
  2355. hso_net->net);
  2356. hso_net->skb_tx_buf = NULL;
  2357. }
  2358. result = hso_start_net_device(network_table[i]);
  2359. if (result)
  2360. goto out;
  2361. }
  2362. }
  2363. }
  2364. out:
  2365. return result;
  2366. }
  2367. static void hso_serial_ref_free(struct kref *ref)
  2368. {
  2369. struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
  2370. hso_free_serial_device(hso_dev);
  2371. }
  2372. static void hso_free_interface(struct usb_interface *interface)
  2373. {
  2374. struct hso_serial *hso_dev;
  2375. int i;
  2376. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2377. if (serial_table[i]
  2378. && (serial_table[i]->interface == interface)) {
  2379. hso_dev = dev2ser(serial_table[i]);
  2380. if (hso_dev->tty)
  2381. tty_hangup(hso_dev->tty);
  2382. mutex_lock(&hso_dev->parent->mutex);
  2383. hso_dev->parent->usb_gone = 1;
  2384. mutex_unlock(&hso_dev->parent->mutex);
  2385. kref_put(&serial_table[i]->ref, hso_serial_ref_free);
  2386. }
  2387. }
  2388. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2389. if (network_table[i]
  2390. && (network_table[i]->interface == interface)) {
  2391. struct rfkill *rfk = dev2net(network_table[i])->rfkill;
  2392. /* hso_stop_net_device doesn't stop the net queue since
  2393. * traffic needs to start it again when suspended */
  2394. netif_stop_queue(dev2net(network_table[i])->net);
  2395. hso_stop_net_device(network_table[i]);
  2396. cancel_work_sync(&network_table[i]->async_put_intf);
  2397. cancel_work_sync(&network_table[i]->async_get_intf);
  2398. if (rfk)
  2399. rfkill_unregister(rfk);
  2400. hso_free_net_device(network_table[i]);
  2401. }
  2402. }
  2403. }
  2404. /* Helper functions */
  2405. /* Get the endpoint ! */
  2406. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  2407. int type, int dir)
  2408. {
  2409. int i;
  2410. struct usb_host_interface *iface = intf->cur_altsetting;
  2411. struct usb_endpoint_descriptor *endp;
  2412. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2413. endp = &iface->endpoint[i].desc;
  2414. if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
  2415. ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type))
  2416. return endp;
  2417. }
  2418. return NULL;
  2419. }
  2420. /* Get the byte that describes which ports are enabled */
  2421. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
  2422. {
  2423. int i;
  2424. struct usb_host_interface *iface = intf->cur_altsetting;
  2425. if (iface->extralen == 3) {
  2426. *ports = iface->extra[2];
  2427. return 0;
  2428. }
  2429. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2430. if (iface->endpoint[i].extralen == 3) {
  2431. *ports = iface->endpoint[i].extra[2];
  2432. return 0;
  2433. }
  2434. }
  2435. return -1;
  2436. }
  2437. /* interrupt urb needs to be submitted, used for serial read of muxed port */
  2438. static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
  2439. struct usb_device *usb, gfp_t gfp)
  2440. {
  2441. int result;
  2442. usb_fill_int_urb(shared_int->shared_intr_urb, usb,
  2443. usb_rcvintpipe(usb,
  2444. shared_int->intr_endp->bEndpointAddress & 0x7F),
  2445. shared_int->shared_intr_buf,
  2446. shared_int->intr_endp->wMaxPacketSize,
  2447. intr_callback, shared_int,
  2448. shared_int->intr_endp->bInterval);
  2449. result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
  2450. if (result)
  2451. dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
  2452. result);
  2453. return result;
  2454. }
  2455. /* operations setup of the serial interface */
  2456. static const struct tty_operations hso_serial_ops = {
  2457. .open = hso_serial_open,
  2458. .close = hso_serial_close,
  2459. .write = hso_serial_write,
  2460. .write_room = hso_serial_write_room,
  2461. .set_termios = hso_serial_set_termios,
  2462. .chars_in_buffer = hso_serial_chars_in_buffer,
  2463. .tiocmget = hso_serial_tiocmget,
  2464. .tiocmset = hso_serial_tiocmset,
  2465. .unthrottle = hso_unthrottle
  2466. };
  2467. static struct usb_driver hso_driver = {
  2468. .name = driver_name,
  2469. .probe = hso_probe,
  2470. .disconnect = hso_disconnect,
  2471. .id_table = hso_ids,
  2472. .suspend = hso_suspend,
  2473. .resume = hso_resume,
  2474. .reset_resume = hso_resume,
  2475. .supports_autosuspend = 1,
  2476. };
  2477. static int __init hso_init(void)
  2478. {
  2479. int i;
  2480. int result;
  2481. /* put it in the log */
  2482. printk(KERN_INFO "hso: %s\n", version);
  2483. /* Initialise the serial table semaphore and table */
  2484. spin_lock_init(&serial_table_lock);
  2485. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
  2486. serial_table[i] = NULL;
  2487. /* allocate our driver using the proper amount of supported minors */
  2488. tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
  2489. if (!tty_drv)
  2490. return -ENOMEM;
  2491. /* fill in all needed values */
  2492. tty_drv->magic = TTY_DRIVER_MAGIC;
  2493. tty_drv->owner = THIS_MODULE;
  2494. tty_drv->driver_name = driver_name;
  2495. tty_drv->name = tty_filename;
  2496. /* if major number is provided as parameter, use that one */
  2497. if (tty_major)
  2498. tty_drv->major = tty_major;
  2499. tty_drv->minor_start = 0;
  2500. tty_drv->num = HSO_SERIAL_TTY_MINORS;
  2501. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  2502. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  2503. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  2504. tty_drv->init_termios = tty_std_termios;
  2505. tty_drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  2506. tty_drv->termios = hso_serial_termios;
  2507. tty_drv->termios_locked = hso_serial_termios_locked;
  2508. tty_set_operations(tty_drv, &hso_serial_ops);
  2509. /* register the tty driver */
  2510. result = tty_register_driver(tty_drv);
  2511. if (result) {
  2512. printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
  2513. __func__, result);
  2514. return result;
  2515. }
  2516. /* register this module as an usb driver */
  2517. result = usb_register(&hso_driver);
  2518. if (result) {
  2519. printk(KERN_ERR "Could not register hso driver? error: %d\n",
  2520. result);
  2521. /* cleanup serial interface */
  2522. tty_unregister_driver(tty_drv);
  2523. return result;
  2524. }
  2525. /* done */
  2526. return 0;
  2527. }
  2528. static void __exit hso_exit(void)
  2529. {
  2530. printk(KERN_INFO "hso: unloaded\n");
  2531. tty_unregister_driver(tty_drv);
  2532. /* deregister the usb driver */
  2533. usb_deregister(&hso_driver);
  2534. }
  2535. /* Module definitions */
  2536. module_init(hso_init);
  2537. module_exit(hso_exit);
  2538. MODULE_AUTHOR(MOD_AUTHOR);
  2539. MODULE_DESCRIPTION(MOD_DESCRIPTION);
  2540. MODULE_LICENSE(MOD_LICENSE);
  2541. MODULE_INFO(Version, DRIVER_VERSION);
  2542. /* change the debug level (eg: insmod hso.ko debug=0x04) */
  2543. MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
  2544. module_param(debug, int, S_IRUGO | S_IWUSR);
  2545. /* set the major tty number (eg: insmod hso.ko tty_major=245) */
  2546. MODULE_PARM_DESC(tty_major, "Set the major tty number");
  2547. module_param(tty_major, int, S_IRUGO | S_IWUSR);
  2548. /* disable network interface (eg: insmod hso.ko disable_net=1) */
  2549. MODULE_PARM_DESC(disable_net, "Disable the network interface");
  2550. module_param(disable_net, int, S_IRUGO | S_IWUSR);