hso.c 77 KB

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