hso.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408
  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. #ifdef CONFIG_HSO_AUTOPM
  994. usb_mark_last_busy(urb->dev);
  995. #endif
  996. /* We are done with this URB, resubmit it. Prep the USB to wait for
  997. * another frame */
  998. usb_fill_bulk_urb(urb, serial->parent->usb,
  999. usb_rcvbulkpipe(serial->parent->usb,
  1000. serial->in_endp->
  1001. bEndpointAddress & 0x7F),
  1002. urb->transfer_buffer, serial->rx_data_length,
  1003. hso_std_serial_read_bulk_callback, serial);
  1004. /* Give this to the USB subsystem so it can tell us when more data
  1005. * arrives. */
  1006. result = usb_submit_urb(urb, GFP_ATOMIC);
  1007. if (result) {
  1008. dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
  1009. __func__, result);
  1010. }
  1011. }
  1012. static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
  1013. {
  1014. int count;
  1015. struct urb *curr_urb;
  1016. while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
  1017. curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
  1018. count = put_rxbuf_data(curr_urb, serial);
  1019. if (count == -1)
  1020. return;
  1021. if (count == 0) {
  1022. serial->curr_rx_urb_idx++;
  1023. if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
  1024. serial->curr_rx_urb_idx = 0;
  1025. hso_resubmit_rx_bulk_urb(serial, curr_urb);
  1026. }
  1027. }
  1028. }
  1029. static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
  1030. {
  1031. int count = 0;
  1032. struct urb *urb;
  1033. urb = serial->rx_urb[0];
  1034. if (serial->open_count > 0) {
  1035. count = put_rxbuf_data(urb, serial);
  1036. if (count == -1)
  1037. return;
  1038. }
  1039. /* Re issue a read as long as we receive data. */
  1040. if (count == 0 && ((urb->actual_length != 0) ||
  1041. (serial->rx_state == RX_PENDING))) {
  1042. serial->rx_state = RX_SENT;
  1043. hso_mux_serial_read(serial);
  1044. } else
  1045. serial->rx_state = RX_IDLE;
  1046. }
  1047. /* read callback for Diag and CS port */
  1048. static void hso_std_serial_read_bulk_callback(struct urb *urb)
  1049. {
  1050. struct hso_serial *serial = urb->context;
  1051. int status = urb->status;
  1052. /* sanity check */
  1053. if (!serial) {
  1054. D1("serial == NULL");
  1055. return;
  1056. } else if (status) {
  1057. handle_usb_error(status, __func__, serial->parent);
  1058. return;
  1059. }
  1060. D4("\n--- Got serial_read_bulk callback %02x ---", status);
  1061. D1("Actual length = %d\n", urb->actual_length);
  1062. DUMP1(urb->transfer_buffer, urb->actual_length);
  1063. /* Anyone listening? */
  1064. if (serial->open_count == 0)
  1065. return;
  1066. if (status == 0) {
  1067. if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
  1068. u32 rest;
  1069. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  1070. rest =
  1071. urb->actual_length %
  1072. le16_to_cpu(serial->in_endp->wMaxPacketSize);
  1073. if (((rest == 5) || (rest == 6)) &&
  1074. !memcmp(((u8 *) urb->transfer_buffer) +
  1075. urb->actual_length - 4, crc_check, 4)) {
  1076. urb->actual_length -= 4;
  1077. }
  1078. }
  1079. /* Valid data, handle RX data */
  1080. spin_lock(&serial->serial_lock);
  1081. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
  1082. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1083. spin_unlock(&serial->serial_lock);
  1084. } else if (status == -ENOENT || status == -ECONNRESET) {
  1085. /* Unlinked - check for throttled port. */
  1086. D2("Port %d, successfully unlinked urb", serial->minor);
  1087. spin_lock(&serial->serial_lock);
  1088. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1089. hso_resubmit_rx_bulk_urb(serial, urb);
  1090. spin_unlock(&serial->serial_lock);
  1091. } else {
  1092. D2("Port %d, status = %d for read urb", serial->minor, status);
  1093. return;
  1094. }
  1095. }
  1096. /*
  1097. * This needs to be a tasklet otherwise we will
  1098. * end up recursively calling this function.
  1099. */
  1100. static void hso_unthrottle_tasklet(struct hso_serial *serial)
  1101. {
  1102. unsigned long flags;
  1103. spin_lock_irqsave(&serial->serial_lock, flags);
  1104. if ((serial->parent->port_spec & HSO_INTF_MUX))
  1105. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1106. else
  1107. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1108. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1109. }
  1110. static void hso_unthrottle(struct tty_struct *tty)
  1111. {
  1112. struct hso_serial *serial = get_serial_by_tty(tty);
  1113. tasklet_hi_schedule(&serial->unthrottle_tasklet);
  1114. }
  1115. static void hso_unthrottle_workfunc(struct work_struct *work)
  1116. {
  1117. struct hso_serial *serial =
  1118. container_of(work, struct hso_serial,
  1119. retry_unthrottle_workqueue);
  1120. hso_unthrottle_tasklet(serial);
  1121. }
  1122. /* open the requested serial port */
  1123. static int hso_serial_open(struct tty_struct *tty, struct file *filp)
  1124. {
  1125. struct hso_serial *serial = get_serial_by_index(tty->index);
  1126. int result;
  1127. /* sanity check */
  1128. if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
  1129. WARN_ON(1);
  1130. tty->driver_data = NULL;
  1131. D1("Failed to open port");
  1132. return -ENODEV;
  1133. }
  1134. mutex_lock(&serial->parent->mutex);
  1135. result = usb_autopm_get_interface(serial->parent->interface);
  1136. if (result < 0)
  1137. goto err_out;
  1138. D1("Opening %d", serial->minor);
  1139. kref_get(&serial->parent->ref);
  1140. /* setup */
  1141. spin_lock_irq(&serial->serial_lock);
  1142. tty->driver_data = serial;
  1143. tty_kref_put(serial->tty);
  1144. serial->tty = tty_kref_get(tty);
  1145. spin_unlock_irq(&serial->serial_lock);
  1146. /* check for port already opened, if not set the termios */
  1147. serial->open_count++;
  1148. if (serial->open_count == 1) {
  1149. tty->low_latency = 1;
  1150. serial->rx_state = RX_IDLE;
  1151. /* Force default termio settings */
  1152. _hso_serial_set_termios(tty, NULL);
  1153. tasklet_init(&serial->unthrottle_tasklet,
  1154. (void (*)(unsigned long))hso_unthrottle_tasklet,
  1155. (unsigned long)serial);
  1156. INIT_WORK(&serial->retry_unthrottle_workqueue,
  1157. hso_unthrottle_workfunc);
  1158. result = hso_start_serial_device(serial->parent, GFP_KERNEL);
  1159. if (result) {
  1160. hso_stop_serial_device(serial->parent);
  1161. serial->open_count--;
  1162. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1163. }
  1164. } else {
  1165. D1("Port was already open");
  1166. }
  1167. usb_autopm_put_interface(serial->parent->interface);
  1168. /* done */
  1169. if (result)
  1170. hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
  1171. err_out:
  1172. mutex_unlock(&serial->parent->mutex);
  1173. return result;
  1174. }
  1175. /* close the requested serial port */
  1176. static void hso_serial_close(struct tty_struct *tty, struct file *filp)
  1177. {
  1178. struct hso_serial *serial = tty->driver_data;
  1179. u8 usb_gone;
  1180. D1("Closing serial port");
  1181. /* Open failed, no close cleanup required */
  1182. if (serial == NULL)
  1183. return;
  1184. mutex_lock(&serial->parent->mutex);
  1185. usb_gone = serial->parent->usb_gone;
  1186. if (!usb_gone)
  1187. usb_autopm_get_interface(serial->parent->interface);
  1188. /* reset the rts and dtr */
  1189. /* do the actual close */
  1190. serial->open_count--;
  1191. if (serial->open_count <= 0) {
  1192. serial->open_count = 0;
  1193. spin_lock_irq(&serial->serial_lock);
  1194. if (serial->tty == tty) {
  1195. serial->tty->driver_data = NULL;
  1196. serial->tty = NULL;
  1197. tty_kref_put(tty);
  1198. }
  1199. spin_unlock_irq(&serial->serial_lock);
  1200. if (!usb_gone)
  1201. hso_stop_serial_device(serial->parent);
  1202. tasklet_kill(&serial->unthrottle_tasklet);
  1203. cancel_work_sync(&serial->retry_unthrottle_workqueue);
  1204. }
  1205. if (!usb_gone)
  1206. usb_autopm_put_interface(serial->parent->interface);
  1207. mutex_unlock(&serial->parent->mutex);
  1208. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1209. }
  1210. /* close the requested serial port */
  1211. static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
  1212. int count)
  1213. {
  1214. struct hso_serial *serial = get_serial_by_tty(tty);
  1215. int space, tx_bytes;
  1216. unsigned long flags;
  1217. /* sanity check */
  1218. if (serial == NULL) {
  1219. printk(KERN_ERR "%s: serial is NULL\n", __func__);
  1220. return -ENODEV;
  1221. }
  1222. spin_lock_irqsave(&serial->serial_lock, flags);
  1223. space = serial->tx_data_length - serial->tx_buffer_count;
  1224. tx_bytes = (count < space) ? count : space;
  1225. if (!tx_bytes)
  1226. goto out;
  1227. memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
  1228. serial->tx_buffer_count += tx_bytes;
  1229. out:
  1230. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1231. hso_kick_transmit(serial);
  1232. /* done */
  1233. return tx_bytes;
  1234. }
  1235. /* how much room is there for writing */
  1236. static int hso_serial_write_room(struct tty_struct *tty)
  1237. {
  1238. struct hso_serial *serial = get_serial_by_tty(tty);
  1239. int room;
  1240. unsigned long flags;
  1241. spin_lock_irqsave(&serial->serial_lock, flags);
  1242. room = serial->tx_data_length - serial->tx_buffer_count;
  1243. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1244. /* return free room */
  1245. return room;
  1246. }
  1247. /* setup the term */
  1248. static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
  1249. {
  1250. struct hso_serial *serial = get_serial_by_tty(tty);
  1251. unsigned long flags;
  1252. if (old)
  1253. D5("Termios called with: cflags new[%d] - old[%d]",
  1254. tty->termios->c_cflag, old->c_cflag);
  1255. /* the actual setup */
  1256. spin_lock_irqsave(&serial->serial_lock, flags);
  1257. if (serial->open_count)
  1258. _hso_serial_set_termios(tty, old);
  1259. else
  1260. tty->termios = old;
  1261. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1262. /* done */
  1263. return;
  1264. }
  1265. /* how many characters in the buffer */
  1266. static int hso_serial_chars_in_buffer(struct tty_struct *tty)
  1267. {
  1268. struct hso_serial *serial = get_serial_by_tty(tty);
  1269. int chars;
  1270. unsigned long flags;
  1271. /* sanity check */
  1272. if (serial == NULL)
  1273. return 0;
  1274. spin_lock_irqsave(&serial->serial_lock, flags);
  1275. chars = serial->tx_buffer_count;
  1276. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1277. return chars;
  1278. }
  1279. static int tiocmget_submit_urb(struct hso_serial *serial,
  1280. struct hso_tiocmget *tiocmget,
  1281. struct usb_device *usb)
  1282. {
  1283. int result;
  1284. if (serial->parent->usb_gone)
  1285. return -ENODEV;
  1286. usb_fill_int_urb(tiocmget->urb, usb,
  1287. usb_rcvintpipe(usb,
  1288. tiocmget->endp->
  1289. bEndpointAddress & 0x7F),
  1290. &tiocmget->serial_state_notification,
  1291. sizeof(struct hso_serial_state_notification),
  1292. tiocmget_intr_callback, serial,
  1293. tiocmget->endp->bInterval);
  1294. result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
  1295. if (result) {
  1296. dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
  1297. result);
  1298. }
  1299. return result;
  1300. }
  1301. static void tiocmget_intr_callback(struct urb *urb)
  1302. {
  1303. struct hso_serial *serial = urb->context;
  1304. struct hso_tiocmget *tiocmget;
  1305. int status = urb->status;
  1306. u16 UART_state_bitmap, prev_UART_state_bitmap;
  1307. struct uart_icount *icount;
  1308. struct hso_serial_state_notification *serial_state_notification;
  1309. struct usb_device *usb;
  1310. /* Sanity checks */
  1311. if (!serial)
  1312. return;
  1313. if (status) {
  1314. handle_usb_error(status, __func__, serial->parent);
  1315. return;
  1316. }
  1317. tiocmget = serial->tiocmget;
  1318. if (!tiocmget)
  1319. return;
  1320. usb = serial->parent->usb;
  1321. serial_state_notification = &tiocmget->serial_state_notification;
  1322. if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
  1323. serial_state_notification->bNotification != B_NOTIFICATION ||
  1324. le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
  1325. le16_to_cpu(serial_state_notification->wIndex) != W_INDEX ||
  1326. le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
  1327. dev_warn(&usb->dev,
  1328. "hso received invalid serial state notification\n");
  1329. DUMP(serial_state_notification,
  1330. sizeof(struct hso_serial_state_notification));
  1331. } else {
  1332. UART_state_bitmap = le16_to_cpu(serial_state_notification->
  1333. UART_state_bitmap);
  1334. prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
  1335. icount = &tiocmget->icount;
  1336. spin_lock(&serial->serial_lock);
  1337. if ((UART_state_bitmap & B_OVERRUN) !=
  1338. (prev_UART_state_bitmap & B_OVERRUN))
  1339. icount->parity++;
  1340. if ((UART_state_bitmap & B_PARITY) !=
  1341. (prev_UART_state_bitmap & B_PARITY))
  1342. icount->parity++;
  1343. if ((UART_state_bitmap & B_FRAMING) !=
  1344. (prev_UART_state_bitmap & B_FRAMING))
  1345. icount->frame++;
  1346. if ((UART_state_bitmap & B_RING_SIGNAL) &&
  1347. !(prev_UART_state_bitmap & B_RING_SIGNAL))
  1348. icount->rng++;
  1349. if ((UART_state_bitmap & B_BREAK) !=
  1350. (prev_UART_state_bitmap & B_BREAK))
  1351. icount->brk++;
  1352. if ((UART_state_bitmap & B_TX_CARRIER) !=
  1353. (prev_UART_state_bitmap & B_TX_CARRIER))
  1354. icount->dsr++;
  1355. if ((UART_state_bitmap & B_RX_CARRIER) !=
  1356. (prev_UART_state_bitmap & B_RX_CARRIER))
  1357. icount->dcd++;
  1358. tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
  1359. spin_unlock(&serial->serial_lock);
  1360. tiocmget->intr_completed = 1;
  1361. wake_up_interruptible(&tiocmget->waitq);
  1362. }
  1363. memset(serial_state_notification, 0,
  1364. sizeof(struct hso_serial_state_notification));
  1365. tiocmget_submit_urb(serial,
  1366. tiocmget,
  1367. serial->parent->usb);
  1368. }
  1369. /*
  1370. * next few functions largely stolen from drivers/serial/serial_core.c
  1371. */
  1372. /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1373. * - mask passed in arg for lines of interest
  1374. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1375. * Caller should use TIOCGICOUNT to see which one it was
  1376. */
  1377. static int
  1378. hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
  1379. {
  1380. DECLARE_WAITQUEUE(wait, current);
  1381. struct uart_icount cprev, cnow;
  1382. struct hso_tiocmget *tiocmget;
  1383. int ret;
  1384. tiocmget = serial->tiocmget;
  1385. if (!tiocmget)
  1386. return -ENOENT;
  1387. /*
  1388. * note the counters on entry
  1389. */
  1390. spin_lock_irq(&serial->serial_lock);
  1391. memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
  1392. spin_unlock_irq(&serial->serial_lock);
  1393. add_wait_queue(&tiocmget->waitq, &wait);
  1394. for (;;) {
  1395. spin_lock_irq(&serial->serial_lock);
  1396. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1397. spin_unlock_irq(&serial->serial_lock);
  1398. set_current_state(TASK_INTERRUPTIBLE);
  1399. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1400. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1401. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
  1402. ret = 0;
  1403. break;
  1404. }
  1405. schedule();
  1406. /* see if a signal did it */
  1407. if (signal_pending(current)) {
  1408. ret = -ERESTARTSYS;
  1409. break;
  1410. }
  1411. cprev = cnow;
  1412. }
  1413. current->state = TASK_RUNNING;
  1414. remove_wait_queue(&tiocmget->waitq, &wait);
  1415. return ret;
  1416. }
  1417. /*
  1418. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1419. * Return: write counters to the user passed counter struct
  1420. * NB: both 1->0 and 0->1 transitions are counted except for
  1421. * RI where only 0->1 is counted.
  1422. */
  1423. static int hso_get_count(struct hso_serial *serial,
  1424. struct serial_icounter_struct __user *icnt)
  1425. {
  1426. struct serial_icounter_struct icount;
  1427. struct uart_icount cnow;
  1428. struct hso_tiocmget *tiocmget = serial->tiocmget;
  1429. if (!tiocmget)
  1430. return -ENOENT;
  1431. spin_lock_irq(&serial->serial_lock);
  1432. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1433. spin_unlock_irq(&serial->serial_lock);
  1434. icount.cts = cnow.cts;
  1435. icount.dsr = cnow.dsr;
  1436. icount.rng = cnow.rng;
  1437. icount.dcd = cnow.dcd;
  1438. icount.rx = cnow.rx;
  1439. icount.tx = cnow.tx;
  1440. icount.frame = cnow.frame;
  1441. icount.overrun = cnow.overrun;
  1442. icount.parity = cnow.parity;
  1443. icount.brk = cnow.brk;
  1444. icount.buf_overrun = cnow.buf_overrun;
  1445. return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
  1446. }
  1447. static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
  1448. {
  1449. int retval;
  1450. struct hso_serial *serial = get_serial_by_tty(tty);
  1451. struct hso_tiocmget *tiocmget;
  1452. u16 UART_state_bitmap;
  1453. /* sanity check */
  1454. if (!serial) {
  1455. D1("no tty structures");
  1456. return -EINVAL;
  1457. }
  1458. spin_lock_irq(&serial->serial_lock);
  1459. retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
  1460. ((serial->dtr_state) ? TIOCM_DTR : 0);
  1461. tiocmget = serial->tiocmget;
  1462. if (tiocmget) {
  1463. UART_state_bitmap = le16_to_cpu(
  1464. tiocmget->prev_UART_state_bitmap);
  1465. if (UART_state_bitmap & B_RING_SIGNAL)
  1466. retval |= TIOCM_RNG;
  1467. if (UART_state_bitmap & B_RX_CARRIER)
  1468. retval |= TIOCM_CD;
  1469. if (UART_state_bitmap & B_TX_CARRIER)
  1470. retval |= TIOCM_DSR;
  1471. }
  1472. spin_unlock_irq(&serial->serial_lock);
  1473. return retval;
  1474. }
  1475. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  1476. unsigned int set, unsigned int clear)
  1477. {
  1478. int val = 0;
  1479. unsigned long flags;
  1480. int if_num;
  1481. struct hso_serial *serial = get_serial_by_tty(tty);
  1482. /* sanity check */
  1483. if (!serial) {
  1484. D1("no tty structures");
  1485. return -EINVAL;
  1486. }
  1487. if ((serial->parent->port_spec & HSO_PORT_MASK) != HSO_PORT_MODEM)
  1488. return -EINVAL;
  1489. if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
  1490. spin_lock_irqsave(&serial->serial_lock, flags);
  1491. if (set & TIOCM_RTS)
  1492. serial->rts_state = 1;
  1493. if (set & TIOCM_DTR)
  1494. serial->dtr_state = 1;
  1495. if (clear & TIOCM_RTS)
  1496. serial->rts_state = 0;
  1497. if (clear & TIOCM_DTR)
  1498. serial->dtr_state = 0;
  1499. if (serial->dtr_state)
  1500. val |= 0x01;
  1501. if (serial->rts_state)
  1502. val |= 0x02;
  1503. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1504. return usb_control_msg(serial->parent->usb,
  1505. usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
  1506. 0x21, val, if_num, NULL, 0,
  1507. USB_CTRL_SET_TIMEOUT);
  1508. }
  1509. static int hso_serial_ioctl(struct tty_struct *tty, struct file *file,
  1510. unsigned int cmd, unsigned long arg)
  1511. {
  1512. struct hso_serial *serial = get_serial_by_tty(tty);
  1513. void __user *uarg = (void __user *)arg;
  1514. int ret = 0;
  1515. D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
  1516. if (!serial)
  1517. return -ENODEV;
  1518. switch (cmd) {
  1519. case TIOCMIWAIT:
  1520. ret = hso_wait_modem_status(serial, arg);
  1521. break;
  1522. case TIOCGICOUNT:
  1523. ret = hso_get_count(serial, uarg);
  1524. break;
  1525. default:
  1526. ret = -ENOIOCTLCMD;
  1527. break;
  1528. }
  1529. return ret;
  1530. }
  1531. /* starts a transmit */
  1532. static void hso_kick_transmit(struct hso_serial *serial)
  1533. {
  1534. u8 *temp;
  1535. unsigned long flags;
  1536. int res;
  1537. spin_lock_irqsave(&serial->serial_lock, flags);
  1538. if (!serial->tx_buffer_count)
  1539. goto out;
  1540. if (serial->tx_urb_used)
  1541. goto out;
  1542. /* Wakeup USB interface if necessary */
  1543. if (hso_get_activity(serial->parent) == -EAGAIN)
  1544. goto out;
  1545. /* Switch pointers around to avoid memcpy */
  1546. temp = serial->tx_buffer;
  1547. serial->tx_buffer = serial->tx_data;
  1548. serial->tx_data = temp;
  1549. serial->tx_data_count = serial->tx_buffer_count;
  1550. serial->tx_buffer_count = 0;
  1551. /* If temp is set, it means we switched buffers */
  1552. if (temp && serial->write_data) {
  1553. res = serial->write_data(serial);
  1554. if (res >= 0)
  1555. serial->tx_urb_used = 1;
  1556. }
  1557. out:
  1558. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1559. }
  1560. /* make a request (for reading and writing data to muxed serial port) */
  1561. static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
  1562. struct urb *ctrl_urb,
  1563. struct usb_ctrlrequest *ctrl_req,
  1564. u8 *ctrl_urb_data, u32 size)
  1565. {
  1566. int result;
  1567. int pipe;
  1568. /* Sanity check */
  1569. if (!serial || !ctrl_urb || !ctrl_req) {
  1570. printk(KERN_ERR "%s: Wrong arguments\n", __func__);
  1571. return -EINVAL;
  1572. }
  1573. /* initialize */
  1574. ctrl_req->wValue = 0;
  1575. ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port));
  1576. ctrl_req->wLength = cpu_to_le16(size);
  1577. if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
  1578. /* Reading command */
  1579. ctrl_req->bRequestType = USB_DIR_IN |
  1580. USB_TYPE_OPTION_VENDOR |
  1581. USB_RECIP_INTERFACE;
  1582. ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  1583. pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
  1584. } else {
  1585. /* Writing command */
  1586. ctrl_req->bRequestType = USB_DIR_OUT |
  1587. USB_TYPE_OPTION_VENDOR |
  1588. USB_RECIP_INTERFACE;
  1589. ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  1590. pipe = usb_sndctrlpipe(serial->parent->usb, 0);
  1591. }
  1592. /* syslog */
  1593. D2("%s command (%02x) len: %d, port: %d",
  1594. type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
  1595. ctrl_req->bRequestType, ctrl_req->wLength, port);
  1596. /* Load ctrl urb */
  1597. ctrl_urb->transfer_flags = 0;
  1598. usb_fill_control_urb(ctrl_urb,
  1599. serial->parent->usb,
  1600. pipe,
  1601. (u8 *) ctrl_req,
  1602. ctrl_urb_data, size, ctrl_callback, serial);
  1603. /* Send it on merry way */
  1604. result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
  1605. if (result) {
  1606. dev_err(&ctrl_urb->dev->dev,
  1607. "%s failed submit ctrl_urb %d type %d\n", __func__,
  1608. result, type);
  1609. return result;
  1610. }
  1611. /* done */
  1612. return size;
  1613. }
  1614. /* called by intr_callback when read occurs */
  1615. static int hso_mux_serial_read(struct hso_serial *serial)
  1616. {
  1617. if (!serial)
  1618. return -EINVAL;
  1619. /* clean data */
  1620. memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
  1621. /* make the request */
  1622. if (serial->num_rx_urbs != 1) {
  1623. dev_err(&serial->parent->interface->dev,
  1624. "ERROR: mux'd reads with multiple buffers "
  1625. "not possible\n");
  1626. return 0;
  1627. }
  1628. return mux_device_request(serial,
  1629. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  1630. serial->parent->port_spec & HSO_PORT_MASK,
  1631. serial->rx_urb[0],
  1632. &serial->ctrl_req_rx,
  1633. serial->rx_data[0], serial->rx_data_length);
  1634. }
  1635. /* used for muxed serial port callback (muxed serial read) */
  1636. static void intr_callback(struct urb *urb)
  1637. {
  1638. struct hso_shared_int *shared_int = urb->context;
  1639. struct hso_serial *serial;
  1640. unsigned char *port_req;
  1641. int status = urb->status;
  1642. int i;
  1643. usb_mark_last_busy(urb->dev);
  1644. /* sanity check */
  1645. if (!shared_int)
  1646. return;
  1647. /* status check */
  1648. if (status) {
  1649. handle_usb_error(status, __func__, NULL);
  1650. return;
  1651. }
  1652. D4("\n--- Got intr callback 0x%02X ---", status);
  1653. /* what request? */
  1654. port_req = urb->transfer_buffer;
  1655. D4(" port_req = 0x%.2X\n", *port_req);
  1656. /* loop over all muxed ports to find the one sending this */
  1657. for (i = 0; i < 8; i++) {
  1658. /* max 8 channels on MUX */
  1659. if (*port_req & (1 << i)) {
  1660. serial = get_serial_by_shared_int_and_type(shared_int,
  1661. (1 << i));
  1662. if (serial != NULL) {
  1663. D1("Pending read interrupt on port %d\n", i);
  1664. spin_lock(&serial->serial_lock);
  1665. if (serial->rx_state == RX_IDLE &&
  1666. serial->open_count > 0) {
  1667. /* Setup and send a ctrl req read on
  1668. * port i */
  1669. if (!serial->rx_urb_filled[0]) {
  1670. serial->rx_state = RX_SENT;
  1671. hso_mux_serial_read(serial);
  1672. } else
  1673. serial->rx_state = RX_PENDING;
  1674. } else {
  1675. D1("Already a read pending on "
  1676. "port %d or port not open\n", i);
  1677. }
  1678. spin_unlock(&serial->serial_lock);
  1679. }
  1680. }
  1681. }
  1682. /* Resubmit interrupt urb */
  1683. hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
  1684. }
  1685. /* called for writing to muxed serial port */
  1686. static int hso_mux_serial_write_data(struct hso_serial *serial)
  1687. {
  1688. if (NULL == serial)
  1689. return -EINVAL;
  1690. return mux_device_request(serial,
  1691. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  1692. serial->parent->port_spec & HSO_PORT_MASK,
  1693. serial->tx_urb,
  1694. &serial->ctrl_req_tx,
  1695. serial->tx_data, serial->tx_data_count);
  1696. }
  1697. /* write callback for Diag and CS port */
  1698. static void hso_std_serial_write_bulk_callback(struct urb *urb)
  1699. {
  1700. struct hso_serial *serial = urb->context;
  1701. int status = urb->status;
  1702. struct tty_struct *tty;
  1703. /* sanity check */
  1704. if (!serial) {
  1705. D1("serial == NULL");
  1706. return;
  1707. }
  1708. spin_lock(&serial->serial_lock);
  1709. serial->tx_urb_used = 0;
  1710. tty = tty_kref_get(serial->tty);
  1711. spin_unlock(&serial->serial_lock);
  1712. if (status) {
  1713. handle_usb_error(status, __func__, serial->parent);
  1714. tty_kref_put(tty);
  1715. return;
  1716. }
  1717. hso_put_activity(serial->parent);
  1718. if (tty) {
  1719. tty_wakeup(tty);
  1720. tty_kref_put(tty);
  1721. }
  1722. hso_kick_transmit(serial);
  1723. D1(" ");
  1724. return;
  1725. }
  1726. /* called for writing diag or CS serial port */
  1727. static int hso_std_serial_write_data(struct hso_serial *serial)
  1728. {
  1729. int count = serial->tx_data_count;
  1730. int result;
  1731. usb_fill_bulk_urb(serial->tx_urb,
  1732. serial->parent->usb,
  1733. usb_sndbulkpipe(serial->parent->usb,
  1734. serial->out_endp->
  1735. bEndpointAddress & 0x7F),
  1736. serial->tx_data, serial->tx_data_count,
  1737. hso_std_serial_write_bulk_callback, serial);
  1738. result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
  1739. if (result) {
  1740. dev_warn(&serial->parent->usb->dev,
  1741. "Failed to submit urb - res %d\n", result);
  1742. return result;
  1743. }
  1744. return count;
  1745. }
  1746. /* callback after read or write on muxed serial port */
  1747. static void ctrl_callback(struct urb *urb)
  1748. {
  1749. struct hso_serial *serial = urb->context;
  1750. struct usb_ctrlrequest *req;
  1751. int status = urb->status;
  1752. struct tty_struct *tty;
  1753. /* sanity check */
  1754. if (!serial)
  1755. return;
  1756. spin_lock(&serial->serial_lock);
  1757. serial->tx_urb_used = 0;
  1758. tty = tty_kref_get(serial->tty);
  1759. spin_unlock(&serial->serial_lock);
  1760. if (status) {
  1761. handle_usb_error(status, __func__, serial->parent);
  1762. tty_kref_put(tty);
  1763. return;
  1764. }
  1765. /* what request? */
  1766. req = (struct usb_ctrlrequest *)(urb->setup_packet);
  1767. D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
  1768. D4("Actual length of urb = %d\n", urb->actual_length);
  1769. DUMP1(urb->transfer_buffer, urb->actual_length);
  1770. if (req->bRequestType ==
  1771. (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
  1772. /* response to a read command */
  1773. serial->rx_urb_filled[0] = 1;
  1774. spin_lock(&serial->serial_lock);
  1775. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1776. spin_unlock(&serial->serial_lock);
  1777. } else {
  1778. hso_put_activity(serial->parent);
  1779. if (tty)
  1780. tty_wakeup(tty);
  1781. /* response to a write command */
  1782. hso_kick_transmit(serial);
  1783. }
  1784. tty_kref_put(tty);
  1785. }
  1786. /* handle RX data for serial port */
  1787. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
  1788. {
  1789. struct tty_struct *tty;
  1790. int write_length_remaining = 0;
  1791. int curr_write_len;
  1792. /* Sanity check */
  1793. if (urb == NULL || serial == NULL) {
  1794. D1("serial = NULL");
  1795. return -2;
  1796. }
  1797. /* All callers to put_rxbuf_data hold serial_lock */
  1798. tty = tty_kref_get(serial->tty);
  1799. /* Push data to tty */
  1800. if (tty) {
  1801. write_length_remaining = urb->actual_length -
  1802. serial->curr_rx_urb_offset;
  1803. D1("data to push to tty");
  1804. while (write_length_remaining) {
  1805. if (test_bit(TTY_THROTTLED, &tty->flags)) {
  1806. tty_kref_put(tty);
  1807. return -1;
  1808. }
  1809. curr_write_len = tty_insert_flip_string
  1810. (tty, urb->transfer_buffer +
  1811. serial->curr_rx_urb_offset,
  1812. write_length_remaining);
  1813. serial->curr_rx_urb_offset += curr_write_len;
  1814. write_length_remaining -= curr_write_len;
  1815. tty_flip_buffer_push(tty);
  1816. }
  1817. }
  1818. if (write_length_remaining == 0) {
  1819. serial->curr_rx_urb_offset = 0;
  1820. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1821. }
  1822. tty_kref_put(tty);
  1823. return write_length_remaining;
  1824. }
  1825. /* Base driver functions */
  1826. static void hso_log_port(struct hso_device *hso_dev)
  1827. {
  1828. char *port_type;
  1829. char port_dev[20];
  1830. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  1831. case HSO_PORT_CONTROL:
  1832. port_type = "Control";
  1833. break;
  1834. case HSO_PORT_APP:
  1835. port_type = "Application";
  1836. break;
  1837. case HSO_PORT_GPS:
  1838. port_type = "GPS";
  1839. break;
  1840. case HSO_PORT_GPS_CONTROL:
  1841. port_type = "GPS control";
  1842. break;
  1843. case HSO_PORT_APP2:
  1844. port_type = "Application2";
  1845. break;
  1846. case HSO_PORT_PCSC:
  1847. port_type = "PCSC";
  1848. break;
  1849. case HSO_PORT_DIAG:
  1850. port_type = "Diagnostic";
  1851. break;
  1852. case HSO_PORT_DIAG2:
  1853. port_type = "Diagnostic2";
  1854. break;
  1855. case HSO_PORT_MODEM:
  1856. port_type = "Modem";
  1857. break;
  1858. case HSO_PORT_NETWORK:
  1859. port_type = "Network";
  1860. break;
  1861. default:
  1862. port_type = "Unknown";
  1863. break;
  1864. }
  1865. if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  1866. sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
  1867. } else
  1868. sprintf(port_dev, "/dev/%s%d", tty_filename,
  1869. dev2ser(hso_dev)->minor);
  1870. dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
  1871. port_type, port_dev);
  1872. }
  1873. static int hso_start_net_device(struct hso_device *hso_dev)
  1874. {
  1875. int i, result = 0;
  1876. struct hso_net *hso_net = dev2net(hso_dev);
  1877. if (!hso_net)
  1878. return -ENODEV;
  1879. /* send URBs for all read buffers */
  1880. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1881. /* Prep a receive URB */
  1882. usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1883. hso_dev->usb,
  1884. usb_rcvbulkpipe(hso_dev->usb,
  1885. hso_net->in_endp->
  1886. bEndpointAddress & 0x7F),
  1887. hso_net->mux_bulk_rx_buf_pool[i],
  1888. MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
  1889. hso_net);
  1890. /* Put it out there so the device can send us stuff */
  1891. result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1892. GFP_NOIO);
  1893. if (result)
  1894. dev_warn(&hso_dev->usb->dev,
  1895. "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
  1896. i, result);
  1897. }
  1898. return result;
  1899. }
  1900. static int hso_stop_net_device(struct hso_device *hso_dev)
  1901. {
  1902. int i;
  1903. struct hso_net *hso_net = dev2net(hso_dev);
  1904. if (!hso_net)
  1905. return -ENODEV;
  1906. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1907. if (hso_net->mux_bulk_rx_urb_pool[i])
  1908. usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1909. }
  1910. if (hso_net->mux_bulk_tx_urb)
  1911. usb_kill_urb(hso_net->mux_bulk_tx_urb);
  1912. return 0;
  1913. }
  1914. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
  1915. {
  1916. int i, result = 0;
  1917. struct hso_serial *serial = dev2ser(hso_dev);
  1918. if (!serial)
  1919. return -ENODEV;
  1920. /* If it is not the MUX port fill in and submit a bulk urb (already
  1921. * allocated in hso_serial_start) */
  1922. if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
  1923. for (i = 0; i < serial->num_rx_urbs; i++) {
  1924. usb_fill_bulk_urb(serial->rx_urb[i],
  1925. serial->parent->usb,
  1926. usb_rcvbulkpipe(serial->parent->usb,
  1927. serial->in_endp->
  1928. bEndpointAddress &
  1929. 0x7F),
  1930. serial->rx_data[i],
  1931. serial->rx_data_length,
  1932. hso_std_serial_read_bulk_callback,
  1933. serial);
  1934. result = usb_submit_urb(serial->rx_urb[i], flags);
  1935. if (result) {
  1936. dev_warn(&serial->parent->usb->dev,
  1937. "Failed to submit urb - res %d\n",
  1938. result);
  1939. break;
  1940. }
  1941. }
  1942. } else {
  1943. mutex_lock(&serial->shared_int->shared_int_lock);
  1944. if (!serial->shared_int->use_count) {
  1945. result =
  1946. hso_mux_submit_intr_urb(serial->shared_int,
  1947. hso_dev->usb, flags);
  1948. }
  1949. serial->shared_int->use_count++;
  1950. mutex_unlock(&serial->shared_int->shared_int_lock);
  1951. }
  1952. if (serial->tiocmget)
  1953. tiocmget_submit_urb(serial,
  1954. serial->tiocmget,
  1955. serial->parent->usb);
  1956. return result;
  1957. }
  1958. static int hso_stop_serial_device(struct hso_device *hso_dev)
  1959. {
  1960. int i;
  1961. struct hso_serial *serial = dev2ser(hso_dev);
  1962. struct hso_tiocmget *tiocmget;
  1963. if (!serial)
  1964. return -ENODEV;
  1965. for (i = 0; i < serial->num_rx_urbs; i++) {
  1966. if (serial->rx_urb[i]) {
  1967. usb_kill_urb(serial->rx_urb[i]);
  1968. serial->rx_urb_filled[i] = 0;
  1969. }
  1970. }
  1971. serial->curr_rx_urb_idx = 0;
  1972. serial->curr_rx_urb_offset = 0;
  1973. if (serial->tx_urb)
  1974. usb_kill_urb(serial->tx_urb);
  1975. if (serial->shared_int) {
  1976. mutex_lock(&serial->shared_int->shared_int_lock);
  1977. if (serial->shared_int->use_count &&
  1978. (--serial->shared_int->use_count == 0)) {
  1979. struct urb *urb;
  1980. urb = serial->shared_int->shared_intr_urb;
  1981. if (urb)
  1982. usb_kill_urb(urb);
  1983. }
  1984. mutex_unlock(&serial->shared_int->shared_int_lock);
  1985. }
  1986. tiocmget = serial->tiocmget;
  1987. if (tiocmget) {
  1988. wake_up_interruptible(&tiocmget->waitq);
  1989. usb_kill_urb(tiocmget->urb);
  1990. }
  1991. return 0;
  1992. }
  1993. static void hso_serial_common_free(struct hso_serial *serial)
  1994. {
  1995. int i;
  1996. if (serial->parent->dev)
  1997. device_remove_file(serial->parent->dev, &dev_attr_hsotype);
  1998. tty_unregister_device(tty_drv, serial->minor);
  1999. for (i = 0; i < serial->num_rx_urbs; i++) {
  2000. /* unlink and free RX URB */
  2001. usb_free_urb(serial->rx_urb[i]);
  2002. /* free the RX buffer */
  2003. kfree(serial->rx_data[i]);
  2004. }
  2005. /* unlink and free TX URB */
  2006. usb_free_urb(serial->tx_urb);
  2007. kfree(serial->tx_data);
  2008. }
  2009. static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
  2010. int rx_size, int tx_size)
  2011. {
  2012. struct device *dev;
  2013. int minor;
  2014. int i;
  2015. minor = get_free_serial_index();
  2016. if (minor < 0)
  2017. goto exit;
  2018. /* register our minor number */
  2019. serial->parent->dev = tty_register_device(tty_drv, minor,
  2020. &serial->parent->interface->dev);
  2021. dev = serial->parent->dev;
  2022. dev_set_drvdata(dev, serial->parent);
  2023. i = device_create_file(dev, &dev_attr_hsotype);
  2024. /* fill in specific data for later use */
  2025. serial->minor = minor;
  2026. serial->magic = HSO_SERIAL_MAGIC;
  2027. spin_lock_init(&serial->serial_lock);
  2028. serial->num_rx_urbs = num_urbs;
  2029. /* RX, allocate urb and initialize */
  2030. /* prepare our RX buffer */
  2031. serial->rx_data_length = rx_size;
  2032. for (i = 0; i < serial->num_rx_urbs; i++) {
  2033. serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  2034. if (!serial->rx_urb[i]) {
  2035. dev_err(dev, "Could not allocate urb?\n");
  2036. goto exit;
  2037. }
  2038. serial->rx_urb[i]->transfer_buffer = NULL;
  2039. serial->rx_urb[i]->transfer_buffer_length = 0;
  2040. serial->rx_data[i] = kzalloc(serial->rx_data_length,
  2041. GFP_KERNEL);
  2042. if (!serial->rx_data[i]) {
  2043. dev_err(dev, "%s - Out of memory\n", __func__);
  2044. goto exit;
  2045. }
  2046. }
  2047. /* TX, allocate urb and initialize */
  2048. serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2049. if (!serial->tx_urb) {
  2050. dev_err(dev, "Could not allocate urb?\n");
  2051. goto exit;
  2052. }
  2053. serial->tx_urb->transfer_buffer = NULL;
  2054. serial->tx_urb->transfer_buffer_length = 0;
  2055. /* prepare our TX buffer */
  2056. serial->tx_data_count = 0;
  2057. serial->tx_buffer_count = 0;
  2058. serial->tx_data_length = tx_size;
  2059. serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2060. if (!serial->tx_data) {
  2061. dev_err(dev, "%s - Out of memory\n", __func__);
  2062. goto exit;
  2063. }
  2064. serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2065. if (!serial->tx_buffer) {
  2066. dev_err(dev, "%s - Out of memory\n", __func__);
  2067. goto exit;
  2068. }
  2069. return 0;
  2070. exit:
  2071. hso_serial_common_free(serial);
  2072. return -1;
  2073. }
  2074. /* Creates a general hso device */
  2075. static struct hso_device *hso_create_device(struct usb_interface *intf,
  2076. int port_spec)
  2077. {
  2078. struct hso_device *hso_dev;
  2079. hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
  2080. if (!hso_dev)
  2081. return NULL;
  2082. hso_dev->port_spec = port_spec;
  2083. hso_dev->usb = interface_to_usbdev(intf);
  2084. hso_dev->interface = intf;
  2085. kref_init(&hso_dev->ref);
  2086. mutex_init(&hso_dev->mutex);
  2087. INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
  2088. INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
  2089. INIT_WORK(&hso_dev->reset_device, reset_device);
  2090. return hso_dev;
  2091. }
  2092. /* Removes a network device in the network device table */
  2093. static int remove_net_device(struct hso_device *hso_dev)
  2094. {
  2095. int i;
  2096. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2097. if (network_table[i] == hso_dev) {
  2098. network_table[i] = NULL;
  2099. break;
  2100. }
  2101. }
  2102. if (i == HSO_MAX_NET_DEVICES)
  2103. return -1;
  2104. return 0;
  2105. }
  2106. /* Frees our network device */
  2107. static void hso_free_net_device(struct hso_device *hso_dev)
  2108. {
  2109. int i;
  2110. struct hso_net *hso_net = dev2net(hso_dev);
  2111. if (!hso_net)
  2112. return;
  2113. remove_net_device(hso_net->parent);
  2114. if (hso_net->net) {
  2115. unregister_netdev(hso_net->net);
  2116. free_netdev(hso_net->net);
  2117. }
  2118. /* start freeing */
  2119. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2120. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  2121. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  2122. hso_net->mux_bulk_rx_buf_pool[i] = NULL;
  2123. }
  2124. usb_free_urb(hso_net->mux_bulk_tx_urb);
  2125. kfree(hso_net->mux_bulk_tx_buf);
  2126. hso_net->mux_bulk_tx_buf = NULL;
  2127. kfree(hso_dev);
  2128. }
  2129. static const struct net_device_ops hso_netdev_ops = {
  2130. .ndo_open = hso_net_open,
  2131. .ndo_stop = hso_net_close,
  2132. .ndo_start_xmit = hso_net_start_xmit,
  2133. .ndo_tx_timeout = hso_net_tx_timeout,
  2134. };
  2135. /* initialize the network interface */
  2136. static void hso_net_init(struct net_device *net)
  2137. {
  2138. struct hso_net *hso_net = netdev_priv(net);
  2139. D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
  2140. /* fill in the other fields */
  2141. net->netdev_ops = &hso_netdev_ops;
  2142. net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
  2143. net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  2144. net->type = ARPHRD_NONE;
  2145. net->mtu = DEFAULT_MTU - 14;
  2146. net->tx_queue_len = 10;
  2147. SET_ETHTOOL_OPS(net, &ops);
  2148. /* and initialize the semaphore */
  2149. spin_lock_init(&hso_net->net_lock);
  2150. }
  2151. /* Adds a network device in the network device table */
  2152. static int add_net_device(struct hso_device *hso_dev)
  2153. {
  2154. int i;
  2155. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2156. if (network_table[i] == NULL) {
  2157. network_table[i] = hso_dev;
  2158. break;
  2159. }
  2160. }
  2161. if (i == HSO_MAX_NET_DEVICES)
  2162. return -1;
  2163. return 0;
  2164. }
  2165. static int hso_rfkill_set_block(void *data, bool blocked)
  2166. {
  2167. struct hso_device *hso_dev = data;
  2168. int enabled = !blocked;
  2169. int rv;
  2170. mutex_lock(&hso_dev->mutex);
  2171. if (hso_dev->usb_gone)
  2172. rv = 0;
  2173. else
  2174. rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
  2175. enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
  2176. USB_CTRL_SET_TIMEOUT);
  2177. mutex_unlock(&hso_dev->mutex);
  2178. return rv;
  2179. }
  2180. static const struct rfkill_ops hso_rfkill_ops = {
  2181. .set_block = hso_rfkill_set_block,
  2182. };
  2183. /* Creates and sets up everything for rfkill */
  2184. static void hso_create_rfkill(struct hso_device *hso_dev,
  2185. struct usb_interface *interface)
  2186. {
  2187. struct hso_net *hso_net = dev2net(hso_dev);
  2188. struct device *dev = &hso_net->net->dev;
  2189. char *rfkn;
  2190. rfkn = kzalloc(20, GFP_KERNEL);
  2191. if (!rfkn)
  2192. dev_err(dev, "%s - Out of memory\n", __func__);
  2193. snprintf(rfkn, 20, "hso-%d",
  2194. interface->altsetting->desc.bInterfaceNumber);
  2195. hso_net->rfkill = rfkill_alloc(rfkn,
  2196. &interface_to_usbdev(interface)->dev,
  2197. RFKILL_TYPE_WWAN,
  2198. &hso_rfkill_ops, hso_dev);
  2199. if (!hso_net->rfkill) {
  2200. dev_err(dev, "%s - Out of memory\n", __func__);
  2201. kfree(rfkn);
  2202. return;
  2203. }
  2204. if (rfkill_register(hso_net->rfkill) < 0) {
  2205. rfkill_destroy(hso_net->rfkill);
  2206. kfree(rfkn);
  2207. hso_net->rfkill = NULL;
  2208. dev_err(dev, "%s - Failed to register rfkill\n", __func__);
  2209. return;
  2210. }
  2211. }
  2212. static struct device_type hso_type = {
  2213. .name = "wwan",
  2214. };
  2215. /* Creates our network device */
  2216. static struct hso_device *hso_create_net_device(struct usb_interface *interface,
  2217. int port_spec)
  2218. {
  2219. int result, i;
  2220. struct net_device *net;
  2221. struct hso_net *hso_net;
  2222. struct hso_device *hso_dev;
  2223. hso_dev = hso_create_device(interface, port_spec);
  2224. if (!hso_dev)
  2225. return NULL;
  2226. /* allocate our network device, then we can put in our private data */
  2227. /* call hso_net_init to do the basic initialization */
  2228. net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
  2229. if (!net) {
  2230. dev_err(&interface->dev, "Unable to create ethernet device\n");
  2231. goto exit;
  2232. }
  2233. hso_net = netdev_priv(net);
  2234. hso_dev->port_data.dev_net = hso_net;
  2235. hso_net->net = net;
  2236. hso_net->parent = hso_dev;
  2237. hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2238. USB_DIR_IN);
  2239. if (!hso_net->in_endp) {
  2240. dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
  2241. goto exit;
  2242. }
  2243. hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2244. USB_DIR_OUT);
  2245. if (!hso_net->out_endp) {
  2246. dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
  2247. goto exit;
  2248. }
  2249. SET_NETDEV_DEV(net, &interface->dev);
  2250. SET_NETDEV_DEVTYPE(net, &hso_type);
  2251. /* registering our net device */
  2252. result = register_netdev(net);
  2253. if (result) {
  2254. dev_err(&interface->dev, "Failed to register device\n");
  2255. goto exit;
  2256. }
  2257. /* start allocating */
  2258. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2259. hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
  2260. if (!hso_net->mux_bulk_rx_urb_pool[i]) {
  2261. dev_err(&interface->dev, "Could not allocate rx urb\n");
  2262. goto exit;
  2263. }
  2264. hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
  2265. GFP_KERNEL);
  2266. if (!hso_net->mux_bulk_rx_buf_pool[i]) {
  2267. dev_err(&interface->dev, "Could not allocate rx buf\n");
  2268. goto exit;
  2269. }
  2270. }
  2271. hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2272. if (!hso_net->mux_bulk_tx_urb) {
  2273. dev_err(&interface->dev, "Could not allocate tx urb\n");
  2274. goto exit;
  2275. }
  2276. hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
  2277. if (!hso_net->mux_bulk_tx_buf) {
  2278. dev_err(&interface->dev, "Could not allocate tx buf\n");
  2279. goto exit;
  2280. }
  2281. add_net_device(hso_dev);
  2282. hso_log_port(hso_dev);
  2283. hso_create_rfkill(hso_dev, interface);
  2284. return hso_dev;
  2285. exit:
  2286. hso_free_net_device(hso_dev);
  2287. return NULL;
  2288. }
  2289. static void hso_free_tiomget(struct hso_serial *serial)
  2290. {
  2291. struct hso_tiocmget *tiocmget = serial->tiocmget;
  2292. if (tiocmget) {
  2293. if (tiocmget->urb) {
  2294. usb_free_urb(tiocmget->urb);
  2295. tiocmget->urb = NULL;
  2296. }
  2297. serial->tiocmget = NULL;
  2298. kfree(tiocmget);
  2299. }
  2300. }
  2301. /* Frees an AT channel ( goes for both mux and non-mux ) */
  2302. static void hso_free_serial_device(struct hso_device *hso_dev)
  2303. {
  2304. struct hso_serial *serial = dev2ser(hso_dev);
  2305. if (!serial)
  2306. return;
  2307. set_serial_by_index(serial->minor, NULL);
  2308. hso_serial_common_free(serial);
  2309. if (serial->shared_int) {
  2310. mutex_lock(&serial->shared_int->shared_int_lock);
  2311. if (--serial->shared_int->ref_count == 0)
  2312. hso_free_shared_int(serial->shared_int);
  2313. else
  2314. mutex_unlock(&serial->shared_int->shared_int_lock);
  2315. }
  2316. hso_free_tiomget(serial);
  2317. kfree(serial);
  2318. kfree(hso_dev);
  2319. }
  2320. /* Creates a bulk AT channel */
  2321. static struct hso_device *hso_create_bulk_serial_device(
  2322. struct usb_interface *interface, int port)
  2323. {
  2324. struct hso_device *hso_dev;
  2325. struct hso_serial *serial;
  2326. int num_urbs;
  2327. struct hso_tiocmget *tiocmget;
  2328. hso_dev = hso_create_device(interface, port);
  2329. if (!hso_dev)
  2330. return NULL;
  2331. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2332. if (!serial)
  2333. goto exit;
  2334. serial->parent = hso_dev;
  2335. hso_dev->port_data.dev_serial = serial;
  2336. if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
  2337. num_urbs = 2;
  2338. serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
  2339. GFP_KERNEL);
  2340. /* it isn't going to break our heart if serial->tiocmget
  2341. * allocation fails don't bother checking this.
  2342. */
  2343. if (serial->tiocmget) {
  2344. tiocmget = serial->tiocmget;
  2345. tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
  2346. if (tiocmget->urb) {
  2347. mutex_init(&tiocmget->mutex);
  2348. init_waitqueue_head(&tiocmget->waitq);
  2349. tiocmget->endp = hso_get_ep(
  2350. interface,
  2351. USB_ENDPOINT_XFER_INT,
  2352. USB_DIR_IN);
  2353. } else
  2354. hso_free_tiomget(serial);
  2355. }
  2356. }
  2357. else
  2358. num_urbs = 1;
  2359. if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
  2360. BULK_URB_TX_SIZE))
  2361. goto exit;
  2362. serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2363. USB_DIR_IN);
  2364. if (!serial->in_endp) {
  2365. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2366. goto exit2;
  2367. }
  2368. if (!
  2369. (serial->out_endp =
  2370. hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
  2371. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2372. goto exit2;
  2373. }
  2374. serial->write_data = hso_std_serial_write_data;
  2375. /* and record this serial */
  2376. set_serial_by_index(serial->minor, serial);
  2377. /* setup the proc dirs and files if needed */
  2378. hso_log_port(hso_dev);
  2379. /* done, return it */
  2380. return hso_dev;
  2381. exit2:
  2382. hso_serial_common_free(serial);
  2383. exit:
  2384. hso_free_tiomget(serial);
  2385. kfree(serial);
  2386. kfree(hso_dev);
  2387. return NULL;
  2388. }
  2389. /* Creates a multiplexed AT channel */
  2390. static
  2391. struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
  2392. int port,
  2393. struct hso_shared_int *mux)
  2394. {
  2395. struct hso_device *hso_dev;
  2396. struct hso_serial *serial;
  2397. int port_spec;
  2398. port_spec = HSO_INTF_MUX;
  2399. port_spec &= ~HSO_PORT_MASK;
  2400. port_spec |= hso_mux_to_port(port);
  2401. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
  2402. return NULL;
  2403. hso_dev = hso_create_device(interface, port_spec);
  2404. if (!hso_dev)
  2405. return NULL;
  2406. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2407. if (!serial)
  2408. goto exit;
  2409. hso_dev->port_data.dev_serial = serial;
  2410. serial->parent = hso_dev;
  2411. if (hso_serial_common_create
  2412. (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
  2413. goto exit;
  2414. serial->tx_data_length--;
  2415. serial->write_data = hso_mux_serial_write_data;
  2416. serial->shared_int = mux;
  2417. mutex_lock(&serial->shared_int->shared_int_lock);
  2418. serial->shared_int->ref_count++;
  2419. mutex_unlock(&serial->shared_int->shared_int_lock);
  2420. /* and record this serial */
  2421. set_serial_by_index(serial->minor, serial);
  2422. /* setup the proc dirs and files if needed */
  2423. hso_log_port(hso_dev);
  2424. /* done, return it */
  2425. return hso_dev;
  2426. exit:
  2427. if (serial) {
  2428. tty_unregister_device(tty_drv, serial->minor);
  2429. kfree(serial);
  2430. }
  2431. if (hso_dev)
  2432. kfree(hso_dev);
  2433. return NULL;
  2434. }
  2435. static void hso_free_shared_int(struct hso_shared_int *mux)
  2436. {
  2437. usb_free_urb(mux->shared_intr_urb);
  2438. kfree(mux->shared_intr_buf);
  2439. mutex_unlock(&mux->shared_int_lock);
  2440. kfree(mux);
  2441. }
  2442. static
  2443. struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
  2444. {
  2445. struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  2446. if (!mux)
  2447. return NULL;
  2448. mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
  2449. USB_DIR_IN);
  2450. if (!mux->intr_endp) {
  2451. dev_err(&interface->dev, "Can't find INT IN endpoint\n");
  2452. goto exit;
  2453. }
  2454. mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  2455. if (!mux->shared_intr_urb) {
  2456. dev_err(&interface->dev, "Could not allocate intr urb?\n");
  2457. goto exit;
  2458. }
  2459. mux->shared_intr_buf =
  2460. kzalloc(le16_to_cpu(mux->intr_endp->wMaxPacketSize),
  2461. GFP_KERNEL);
  2462. if (!mux->shared_intr_buf) {
  2463. dev_err(&interface->dev, "Could not allocate intr buf?\n");
  2464. goto exit;
  2465. }
  2466. mutex_init(&mux->shared_int_lock);
  2467. return mux;
  2468. exit:
  2469. kfree(mux->shared_intr_buf);
  2470. usb_free_urb(mux->shared_intr_urb);
  2471. kfree(mux);
  2472. return NULL;
  2473. }
  2474. /* Gets the port spec for a certain interface */
  2475. static int hso_get_config_data(struct usb_interface *interface)
  2476. {
  2477. struct usb_device *usbdev = interface_to_usbdev(interface);
  2478. u8 config_data[17];
  2479. u32 if_num = interface->altsetting->desc.bInterfaceNumber;
  2480. s32 result;
  2481. if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  2482. 0x86, 0xC0, 0, 0, config_data, 17,
  2483. USB_CTRL_SET_TIMEOUT) != 0x11) {
  2484. return -EIO;
  2485. }
  2486. switch (config_data[if_num]) {
  2487. case 0x0:
  2488. result = 0;
  2489. break;
  2490. case 0x1:
  2491. result = HSO_PORT_DIAG;
  2492. break;
  2493. case 0x2:
  2494. result = HSO_PORT_GPS;
  2495. break;
  2496. case 0x3:
  2497. result = HSO_PORT_GPS_CONTROL;
  2498. break;
  2499. case 0x4:
  2500. result = HSO_PORT_APP;
  2501. break;
  2502. case 0x5:
  2503. result = HSO_PORT_APP2;
  2504. break;
  2505. case 0x6:
  2506. result = HSO_PORT_CONTROL;
  2507. break;
  2508. case 0x7:
  2509. result = HSO_PORT_NETWORK;
  2510. break;
  2511. case 0x8:
  2512. result = HSO_PORT_MODEM;
  2513. break;
  2514. case 0x9:
  2515. result = HSO_PORT_MSD;
  2516. break;
  2517. case 0xa:
  2518. result = HSO_PORT_PCSC;
  2519. break;
  2520. case 0xb:
  2521. result = HSO_PORT_VOICE;
  2522. break;
  2523. default:
  2524. result = 0;
  2525. }
  2526. if (result)
  2527. result |= HSO_INTF_BULK;
  2528. if (config_data[16] & 0x1)
  2529. result |= HSO_INFO_CRC_BUG;
  2530. return result;
  2531. }
  2532. /* called once for each interface upon device insertion */
  2533. static int hso_probe(struct usb_interface *interface,
  2534. const struct usb_device_id *id)
  2535. {
  2536. int mux, i, if_num, port_spec;
  2537. unsigned char port_mask;
  2538. struct hso_device *hso_dev = NULL;
  2539. struct hso_shared_int *shared_int;
  2540. struct hso_device *tmp_dev = NULL;
  2541. if_num = interface->altsetting->desc.bInterfaceNumber;
  2542. /* Get the interface/port specification from either driver_info or from
  2543. * the device itself */
  2544. if (id->driver_info)
  2545. port_spec = ((u32 *)(id->driver_info))[if_num];
  2546. else
  2547. port_spec = hso_get_config_data(interface);
  2548. if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
  2549. dev_err(&interface->dev, "Not our interface\n");
  2550. return -ENODEV;
  2551. }
  2552. /* Check if we need to switch to alt interfaces prior to port
  2553. * configuration */
  2554. if (interface->num_altsetting > 1)
  2555. usb_set_interface(interface_to_usbdev(interface), if_num, 1);
  2556. interface->needs_remote_wakeup = 1;
  2557. /* Allocate new hso device(s) */
  2558. switch (port_spec & HSO_INTF_MASK) {
  2559. case HSO_INTF_MUX:
  2560. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2561. /* Create the network device */
  2562. if (!disable_net) {
  2563. hso_dev = hso_create_net_device(interface,
  2564. port_spec);
  2565. if (!hso_dev)
  2566. goto exit;
  2567. tmp_dev = hso_dev;
  2568. }
  2569. }
  2570. if (hso_get_mux_ports(interface, &port_mask))
  2571. /* TODO: de-allocate everything */
  2572. goto exit;
  2573. shared_int = hso_create_shared_int(interface);
  2574. if (!shared_int)
  2575. goto exit;
  2576. for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
  2577. if (port_mask & i) {
  2578. hso_dev = hso_create_mux_serial_device(
  2579. interface, i, shared_int);
  2580. if (!hso_dev)
  2581. goto exit;
  2582. }
  2583. }
  2584. if (tmp_dev)
  2585. hso_dev = tmp_dev;
  2586. break;
  2587. case HSO_INTF_BULK:
  2588. /* It's a regular bulk interface */
  2589. if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) &&
  2590. !disable_net)
  2591. hso_dev = hso_create_net_device(interface, port_spec);
  2592. else
  2593. hso_dev =
  2594. hso_create_bulk_serial_device(interface, port_spec);
  2595. if (!hso_dev)
  2596. goto exit;
  2597. break;
  2598. default:
  2599. goto exit;
  2600. }
  2601. /* save our data pointer in this device */
  2602. usb_set_intfdata(interface, hso_dev);
  2603. /* done */
  2604. return 0;
  2605. exit:
  2606. hso_free_interface(interface);
  2607. return -ENODEV;
  2608. }
  2609. /* device removed, cleaning up */
  2610. static void hso_disconnect(struct usb_interface *interface)
  2611. {
  2612. hso_free_interface(interface);
  2613. /* remove reference of our private data */
  2614. usb_set_intfdata(interface, NULL);
  2615. }
  2616. static void async_get_intf(struct work_struct *data)
  2617. {
  2618. struct hso_device *hso_dev =
  2619. container_of(data, struct hso_device, async_get_intf);
  2620. usb_autopm_get_interface(hso_dev->interface);
  2621. }
  2622. static void async_put_intf(struct work_struct *data)
  2623. {
  2624. struct hso_device *hso_dev =
  2625. container_of(data, struct hso_device, async_put_intf);
  2626. usb_autopm_put_interface(hso_dev->interface);
  2627. }
  2628. static int hso_get_activity(struct hso_device *hso_dev)
  2629. {
  2630. if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
  2631. if (!hso_dev->is_active) {
  2632. hso_dev->is_active = 1;
  2633. schedule_work(&hso_dev->async_get_intf);
  2634. }
  2635. }
  2636. if (hso_dev->usb->state != USB_STATE_CONFIGURED)
  2637. return -EAGAIN;
  2638. usb_mark_last_busy(hso_dev->usb);
  2639. return 0;
  2640. }
  2641. static int hso_put_activity(struct hso_device *hso_dev)
  2642. {
  2643. if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
  2644. if (hso_dev->is_active) {
  2645. hso_dev->is_active = 0;
  2646. schedule_work(&hso_dev->async_put_intf);
  2647. return -EAGAIN;
  2648. }
  2649. }
  2650. hso_dev->is_active = 0;
  2651. return 0;
  2652. }
  2653. /* called by kernel when we need to suspend device */
  2654. static int hso_suspend(struct usb_interface *iface, pm_message_t message)
  2655. {
  2656. int i, result;
  2657. /* Stop all serial ports */
  2658. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2659. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2660. result = hso_stop_serial_device(serial_table[i]);
  2661. if (result)
  2662. goto out;
  2663. }
  2664. }
  2665. /* Stop all network ports */
  2666. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2667. if (network_table[i] &&
  2668. (network_table[i]->interface == iface)) {
  2669. result = hso_stop_net_device(network_table[i]);
  2670. if (result)
  2671. goto out;
  2672. }
  2673. }
  2674. out:
  2675. return 0;
  2676. }
  2677. /* called by kernel when we need to resume device */
  2678. static int hso_resume(struct usb_interface *iface)
  2679. {
  2680. int i, result = 0;
  2681. struct hso_net *hso_net;
  2682. /* Start all serial ports */
  2683. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2684. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2685. if (dev2ser(serial_table[i])->open_count) {
  2686. result =
  2687. hso_start_serial_device(serial_table[i], GFP_NOIO);
  2688. hso_kick_transmit(dev2ser(serial_table[i]));
  2689. if (result)
  2690. goto out;
  2691. }
  2692. }
  2693. }
  2694. /* Start all network ports */
  2695. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2696. if (network_table[i] &&
  2697. (network_table[i]->interface == iface)) {
  2698. hso_net = dev2net(network_table[i]);
  2699. if (hso_net->flags & IFF_UP) {
  2700. /* First transmit any lingering data,
  2701. then restart the device. */
  2702. if (hso_net->skb_tx_buf) {
  2703. dev_dbg(&iface->dev,
  2704. "Transmitting"
  2705. " lingering data\n");
  2706. hso_net_start_xmit(hso_net->skb_tx_buf,
  2707. hso_net->net);
  2708. hso_net->skb_tx_buf = NULL;
  2709. }
  2710. result = hso_start_net_device(network_table[i]);
  2711. if (result)
  2712. goto out;
  2713. }
  2714. }
  2715. }
  2716. out:
  2717. return result;
  2718. }
  2719. static void reset_device(struct work_struct *data)
  2720. {
  2721. struct hso_device *hso_dev =
  2722. container_of(data, struct hso_device, reset_device);
  2723. struct usb_device *usb = hso_dev->usb;
  2724. int result;
  2725. if (hso_dev->usb_gone) {
  2726. D1("No reset during disconnect\n");
  2727. } else {
  2728. result = usb_lock_device_for_reset(usb, hso_dev->interface);
  2729. if (result < 0)
  2730. D1("unable to lock device for reset: %d\n", result);
  2731. else {
  2732. usb_reset_device(usb);
  2733. usb_unlock_device(usb);
  2734. }
  2735. }
  2736. }
  2737. static void hso_serial_ref_free(struct kref *ref)
  2738. {
  2739. struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
  2740. hso_free_serial_device(hso_dev);
  2741. }
  2742. static void hso_free_interface(struct usb_interface *interface)
  2743. {
  2744. struct hso_serial *hso_dev;
  2745. struct tty_struct *tty;
  2746. int i;
  2747. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2748. if (serial_table[i] &&
  2749. (serial_table[i]->interface == interface)) {
  2750. hso_dev = dev2ser(serial_table[i]);
  2751. spin_lock_irq(&hso_dev->serial_lock);
  2752. tty = tty_kref_get(hso_dev->tty);
  2753. spin_unlock_irq(&hso_dev->serial_lock);
  2754. if (tty)
  2755. tty_hangup(tty);
  2756. mutex_lock(&hso_dev->parent->mutex);
  2757. tty_kref_put(tty);
  2758. hso_dev->parent->usb_gone = 1;
  2759. mutex_unlock(&hso_dev->parent->mutex);
  2760. kref_put(&serial_table[i]->ref, hso_serial_ref_free);
  2761. }
  2762. }
  2763. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2764. if (network_table[i] &&
  2765. (network_table[i]->interface == interface)) {
  2766. struct rfkill *rfk = dev2net(network_table[i])->rfkill;
  2767. /* hso_stop_net_device doesn't stop the net queue since
  2768. * traffic needs to start it again when suspended */
  2769. netif_stop_queue(dev2net(network_table[i])->net);
  2770. hso_stop_net_device(network_table[i]);
  2771. cancel_work_sync(&network_table[i]->async_put_intf);
  2772. cancel_work_sync(&network_table[i]->async_get_intf);
  2773. if (rfk) {
  2774. rfkill_unregister(rfk);
  2775. rfkill_destroy(rfk);
  2776. }
  2777. hso_free_net_device(network_table[i]);
  2778. }
  2779. }
  2780. }
  2781. /* Helper functions */
  2782. /* Get the endpoint ! */
  2783. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  2784. int type, int dir)
  2785. {
  2786. int i;
  2787. struct usb_host_interface *iface = intf->cur_altsetting;
  2788. struct usb_endpoint_descriptor *endp;
  2789. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2790. endp = &iface->endpoint[i].desc;
  2791. if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
  2792. (usb_endpoint_type(endp) == type))
  2793. return endp;
  2794. }
  2795. return NULL;
  2796. }
  2797. /* Get the byte that describes which ports are enabled */
  2798. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
  2799. {
  2800. int i;
  2801. struct usb_host_interface *iface = intf->cur_altsetting;
  2802. if (iface->extralen == 3) {
  2803. *ports = iface->extra[2];
  2804. return 0;
  2805. }
  2806. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2807. if (iface->endpoint[i].extralen == 3) {
  2808. *ports = iface->endpoint[i].extra[2];
  2809. return 0;
  2810. }
  2811. }
  2812. return -1;
  2813. }
  2814. /* interrupt urb needs to be submitted, used for serial read of muxed port */
  2815. static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
  2816. struct usb_device *usb, gfp_t gfp)
  2817. {
  2818. int result;
  2819. usb_fill_int_urb(shared_int->shared_intr_urb, usb,
  2820. usb_rcvintpipe(usb,
  2821. shared_int->intr_endp->bEndpointAddress & 0x7F),
  2822. shared_int->shared_intr_buf,
  2823. 1,
  2824. intr_callback, shared_int,
  2825. shared_int->intr_endp->bInterval);
  2826. result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
  2827. if (result)
  2828. dev_warn(&usb->dev, "%s failed mux_intr_urb %d\n", __func__,
  2829. result);
  2830. return result;
  2831. }
  2832. /* operations setup of the serial interface */
  2833. static const struct tty_operations hso_serial_ops = {
  2834. .open = hso_serial_open,
  2835. .close = hso_serial_close,
  2836. .write = hso_serial_write,
  2837. .write_room = hso_serial_write_room,
  2838. .ioctl = hso_serial_ioctl,
  2839. .set_termios = hso_serial_set_termios,
  2840. .chars_in_buffer = hso_serial_chars_in_buffer,
  2841. .tiocmget = hso_serial_tiocmget,
  2842. .tiocmset = hso_serial_tiocmset,
  2843. .unthrottle = hso_unthrottle
  2844. };
  2845. static struct usb_driver hso_driver = {
  2846. .name = driver_name,
  2847. .probe = hso_probe,
  2848. .disconnect = hso_disconnect,
  2849. .id_table = hso_ids,
  2850. .suspend = hso_suspend,
  2851. .resume = hso_resume,
  2852. .reset_resume = hso_resume,
  2853. .supports_autosuspend = 1,
  2854. };
  2855. static int __init hso_init(void)
  2856. {
  2857. int i;
  2858. int result;
  2859. /* put it in the log */
  2860. printk(KERN_INFO "hso: %s\n", version);
  2861. /* Initialise the serial table semaphore and table */
  2862. spin_lock_init(&serial_table_lock);
  2863. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
  2864. serial_table[i] = NULL;
  2865. /* allocate our driver using the proper amount of supported minors */
  2866. tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
  2867. if (!tty_drv)
  2868. return -ENOMEM;
  2869. /* fill in all needed values */
  2870. tty_drv->magic = TTY_DRIVER_MAGIC;
  2871. tty_drv->owner = THIS_MODULE;
  2872. tty_drv->driver_name = driver_name;
  2873. tty_drv->name = tty_filename;
  2874. /* if major number is provided as parameter, use that one */
  2875. if (tty_major)
  2876. tty_drv->major = tty_major;
  2877. tty_drv->minor_start = 0;
  2878. tty_drv->num = HSO_SERIAL_TTY_MINORS;
  2879. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  2880. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  2881. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  2882. tty_drv->init_termios = tty_std_termios;
  2883. hso_init_termios(&tty_drv->init_termios);
  2884. tty_set_operations(tty_drv, &hso_serial_ops);
  2885. /* register the tty driver */
  2886. result = tty_register_driver(tty_drv);
  2887. if (result) {
  2888. printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
  2889. __func__, result);
  2890. return result;
  2891. }
  2892. /* register this module as an usb driver */
  2893. result = usb_register(&hso_driver);
  2894. if (result) {
  2895. printk(KERN_ERR "Could not register hso driver? error: %d\n",
  2896. result);
  2897. /* cleanup serial interface */
  2898. tty_unregister_driver(tty_drv);
  2899. return result;
  2900. }
  2901. /* done */
  2902. return 0;
  2903. }
  2904. static void __exit hso_exit(void)
  2905. {
  2906. printk(KERN_INFO "hso: unloaded\n");
  2907. tty_unregister_driver(tty_drv);
  2908. /* deregister the usb driver */
  2909. usb_deregister(&hso_driver);
  2910. }
  2911. /* Module definitions */
  2912. module_init(hso_init);
  2913. module_exit(hso_exit);
  2914. MODULE_AUTHOR(MOD_AUTHOR);
  2915. MODULE_DESCRIPTION(MOD_DESCRIPTION);
  2916. MODULE_LICENSE(MOD_LICENSE);
  2917. MODULE_INFO(Version, DRIVER_VERSION);
  2918. /* change the debug level (eg: insmod hso.ko debug=0x04) */
  2919. MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
  2920. module_param(debug, int, S_IRUGO | S_IWUSR);
  2921. /* set the major tty number (eg: insmod hso.ko tty_major=245) */
  2922. MODULE_PARM_DESC(tty_major, "Set the major tty number");
  2923. module_param(tty_major, int, S_IRUGO | S_IWUSR);
  2924. /* disable network interface (eg: insmod hso.ko disable_net=1) */
  2925. MODULE_PARM_DESC(disable_net, "Disable the network interface");
  2926. module_param(disable_net, int, S_IRUGO | S_IWUSR);