hso.c 84 KB

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