hso.c 86 KB

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