hso.c 84 KB

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