hso.c 85 KB

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