hso.c 71 KB

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