hso.c 71 KB

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